isl_union_templ.c: remove unused variables
[platform/upstream/isl.git] / isl_union_templ.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
12 #define FN(TYPE,NAME) xFN(TYPE,NAME)
13 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
14 #define S(TYPE,NAME) xS(TYPE,NAME)
15
16 struct UNION {
17         int ref;
18 #ifdef HAS_TYPE
19         enum isl_fold type;
20 #endif
21         isl_dim *dim;
22
23         struct isl_hash_table   table;
24 };
25
26 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);
27
28 isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
29 {
30         return u ? u->dim->ctx : NULL;
31 }
32
33 __isl_give isl_dim *FN(UNION,get_dim)(__isl_keep UNION *u)
34 {
35         if (!u)
36                 return NULL;
37         return isl_dim_copy(u->dim);
38 }
39
40 #ifdef HAS_TYPE
41 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_dim *dim,
42         enum isl_fold type, int size)
43 #else
44 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_dim *dim, int size)
45 #endif
46 {
47         UNION *u;
48
49         if (!dim)
50                 return NULL;
51
52         u = isl_calloc_type(dim->ctx, UNION);
53         if (!u)
54                 return NULL;
55
56         u->ref = 1;
57 #ifdef HAS_TYPE
58         u->type = type;
59 #endif
60         u->dim = dim;
61         if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
62                 goto error;
63
64         return u;
65 error:
66         isl_dim_free(dim);
67         FN(UNION,free)(u);
68         return NULL;
69 }
70
71 #ifdef HAS_TYPE
72 __isl_give UNION *FN(UNION,zero)(__isl_take isl_dim *dim, enum isl_fold type)
73 {
74         return FN(UNION,alloc)(dim, type, 16);
75 }
76 #else
77 __isl_give UNION *FN(UNION,zero)(__isl_take isl_dim *dim)
78 {
79         return FN(UNION,alloc)(dim, 16);
80 }
81 #endif
82
83 __isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
84 {
85         if (!u)
86                 return NULL;
87
88         u->ref++;
89         return u;
90 }
91
92 S(UNION,foreach_data)
93 {
94         int (*fn)(__isl_take PART *part, void *user);
95         void *user;
96 };
97
98 static int call_on_copy(void **entry, void *user)
99 {
100         PART *part = *entry;
101         S(UNION,foreach_data) *data = (S(UNION,foreach_data) *)user;
102
103         return data->fn(FN(PART,copy)(part), data->user);
104 }
105
106 int FN(FN(UNION,foreach),PARTS)(__isl_keep UNION *u,
107         int (*fn)(__isl_take PART *part, void *user), void *user)
108 {
109         S(UNION,foreach_data) data = { fn, user };
110
111         if (!u)
112                 return -1;
113
114         return isl_hash_table_foreach(u->dim->ctx, &u->table,
115                                       &call_on_copy, &data);
116 }
117
118 static int has_dim(const void *entry, const void *val)
119 {
120         PART *part = (PART *)entry;
121         isl_dim *dim = (isl_dim *)val;
122
123         return isl_dim_equal(part->dim, dim);
124 }
125
126 __isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
127         __isl_take isl_dim *dim)
128 {
129         uint32_t hash;
130         struct isl_hash_table_entry *entry;
131
132         if (!u || !dim)
133                 goto error;
134
135         hash = isl_dim_get_hash(dim);
136         entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
137                                     &has_dim, dim, 0);
138         if (!entry)
139 #ifdef HAS_TYPE
140                 return FN(PART,zero)(dim, u->type);
141 #else
142                 return FN(PART,zero)(dim);
143 #endif
144         isl_dim_free(dim);
145         return FN(PART,copy)(entry->data);
146 error:
147         isl_dim_free(dim);
148         return NULL;
149 }
150
151 __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
152         __isl_take PART *part)
153 {
154         uint32_t hash;
155         struct isl_hash_table_entry *entry;
156
157         if (!part)
158                 goto error;
159
160         if (FN(PART,is_zero)(part)) {
161                 FN(PART,free)(part);
162                 return u;
163         }
164
165         u = FN(UNION,cow)(u);
166
167         if (!u)
168                 goto error;
169
170         isl_assert(u->dim->ctx, isl_dim_match(part->dim, isl_dim_param, u->dim,
171                                               isl_dim_param), goto error);
172
173         hash = isl_dim_get_hash(part->dim);
174         entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
175                                     &has_dim, part->dim, 1);
176         if (!entry)
177                 goto error;
178
179         if (!entry->data)
180                 entry->data = part;
181         else {
182                 entry->data = FN(PART,add)(entry->data, FN(PART,copy)(part));
183                 if (!entry->data)
184                         goto error;
185                 FN(PART,free)(part);
186                 if (FN(PART,is_zero)(entry->data)) {
187                         FN(PART,free)(entry->data);
188                         isl_hash_table_remove(u->dim->ctx, &u->table, entry);
189                 }
190         }
191
192         return u;
193 error:
194         FN(PART,free)(part);
195         FN(UNION,free)(u);
196         return NULL;
197 }
198
199 static int add_part(__isl_take PART *part, void *user)
200 {
201         UNION **u = (UNION **)user;
202
203         *u = FN(FN(UNION,add),PARTS)(*u, part);
204
205         return 0;
206 }
207
208 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
209 {
210         UNION *dup;
211
212         if (!u)
213                 return NULL;
214
215 #ifdef HAS_TYPE
216         dup = FN(UNION,zero)(isl_dim_copy(u->dim), u->type);
217 #else
218         dup = FN(UNION,zero)(isl_dim_copy(u->dim));
219 #endif
220         if (FN(FN(UNION,foreach),PARTS)(u, &add_part, &dup) < 0)
221                 goto error;
222         return dup;
223 error:
224         FN(UNION,free)(dup);
225         return NULL;
226 }
227
228 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
229 {
230         if (!u)
231                 return NULL;
232
233         if (u->ref == 1)
234                 return u;
235         u->ref--;
236         return FN(UNION,dup)(u);
237 }
238
239 static int free_u_entry(void **entry, void *user)
240 {
241         PART *part = *entry;
242         FN(PART,free)(part);
243         return 0;
244 }
245
246 void FN(UNION,free)(__isl_take UNION *u)
247 {
248         if (!u)
249                 return;
250
251         if (--u->ref > 0)
252                 return;
253
254         isl_hash_table_foreach(u->dim->ctx, &u->table, &free_u_entry, NULL);
255         isl_hash_table_clear(&u->table);
256         isl_dim_free(u->dim);
257         free(u);
258 }
259
260 S(UNION,align) {
261         isl_reordering *exp;
262         UNION *res;
263 };
264
265 static int align_entry(__isl_take PART *part, void *user)
266 {
267         isl_reordering *exp;
268         S(UNION,align) *data = user;
269
270         exp = isl_reordering_extend_dim(isl_reordering_copy(data->exp),
271                                     FN(PART,get_dim)(part));
272
273         data->res = FN(FN(UNION,add),PARTS)(data->res,
274                                             FN(PART,realign)(part, exp));
275
276         return 0;
277 }
278
279 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
280         __isl_take isl_dim *model)
281 {
282         S(UNION,align) data = { NULL, NULL };
283
284         if (!u || !model)
285                 goto error;
286
287         if (isl_dim_match(u->dim, isl_dim_param, model, isl_dim_param)) {
288                 isl_dim_free(model);
289                 return u;
290         }
291
292         data.exp = isl_parameter_alignment_reordering(u->dim, model);
293         if (!data.exp)
294                 goto error;
295
296 #ifdef HAS_TYPE
297         data.res = FN(UNION,alloc)(isl_dim_copy(data.exp->dim),
298                                                 u->type, u->table.n);
299 #else
300         data.res = FN(UNION,alloc)(isl_dim_copy(data.exp->dim), u->table.n);
301 #endif
302         if (FN(FN(UNION,foreach),PARTS)(u, &align_entry, &data) < 0)
303                 goto error;
304
305         isl_reordering_free(data.exp);
306         FN(UNION,free)(u);
307         isl_dim_free(model);
308         return data.res;
309 error:
310         isl_reordering_free(data.exp);
311         FN(UNION,free)(u);
312         FN(UNION,free)(data.res);
313         isl_dim_free(model);
314         return NULL;
315 }
316
317 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
318 {
319         u1 = FN(UNION,align_params)(u1, FN(UNION,get_dim)(u2));
320         u2 = FN(UNION,align_params)(u2, FN(UNION,get_dim)(u1));
321
322         u1 = FN(UNION,cow)(u1);
323
324         if (!u1 || !u2)
325                 goto error;
326
327         if (FN(FN(UNION,foreach),PARTS)(u2, &add_part, &u1) < 0)
328                 goto error;
329
330         FN(UNION,free)(u2);
331
332         return u1;
333 error:
334         FN(UNION,free)(u1);
335         FN(UNION,free)(u2);
336         return NULL;
337 }
338
339 __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
340 {
341         isl_dim *dim;
342         UNION *u;
343
344         if (!part)
345                 return NULL;
346
347         dim = FN(PART,get_dim)(part);
348         dim = isl_dim_drop(dim, isl_dim_in, 0, isl_dim_size(dim, isl_dim_in));
349         dim = isl_dim_drop(dim, isl_dim_out, 0, isl_dim_size(dim, isl_dim_out));
350 #ifdef HAS_TYPE
351         u = FN(UNION,zero)(dim, part->type);
352 #else
353         u = FN(UNION,zero)(dim);
354 #endif
355         u = FN(FN(UNION,add),PARTS)(u, part);
356
357         return u;
358 }
359
360 S(UNION,match_bin_data) {
361         UNION *u2;
362         UNION *res;
363 };
364
365 static __isl_give UNION *match_bin_op(__isl_take UNION *u1,
366         __isl_take UNION *u2, int (*fn)(void **, void *))
367 {
368         S(UNION,match_bin_data) data = { NULL, NULL };
369
370         u1 = FN(UNION,align_params)(u1, FN(UNION,get_dim)(u2));
371         u2 = FN(UNION,align_params)(u2, FN(UNION,get_dim)(u1));
372
373         if (!u1 || !u2)
374                 goto error;
375
376         data.u2 = u2;
377 #ifdef HAS_TYPE
378         data.res = FN(UNION,alloc)(isl_dim_copy(u1->dim), u1->type, u1->table.n);
379 #else
380         data.res = FN(UNION,alloc)(isl_dim_copy(u1->dim), u1->table.n);
381 #endif
382         if (isl_hash_table_foreach(u1->dim->ctx, &u1->table, fn, &data) < 0)
383                 goto error;
384
385         FN(UNION,free)(u1);
386         FN(UNION,free)(u2);
387         return data.res;
388 error:
389         FN(UNION,free)(u1);
390         FN(UNION,free)(u2);
391         FN(UNION,free)(data.res);
392         return NULL;
393 }
394
395 S(UNION,match_set_data) {
396         isl_union_set *uset;
397         UNION *res;
398         __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
399 };
400
401 static int set_has_dim(const void *entry, const void *val)
402 {
403         isl_set *set = (isl_set *)entry;
404         isl_dim *dim = (isl_dim *)val;
405
406         return isl_dim_equal(set->dim, dim);
407 }
408
409 static int match_set_entry(void **entry, void *user)
410 {
411         S(UNION,match_set_data) *data = user;
412         uint32_t hash;
413         struct isl_hash_table_entry *entry2;
414         PW *pw = *entry;
415         int empty;
416
417         hash = isl_dim_get_hash(pw->dim);
418         entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
419                                      hash, &set_has_dim, pw->dim, 0);
420         if (!entry2)
421                 return 0;
422
423         pw = FN(PW,copy)(pw);
424         pw = data->fn(pw, isl_set_copy(entry2->data));
425
426         empty = FN(PW,is_zero)(pw);
427         if (empty < 0) {
428                 FN(PW,free)(pw);
429                 return -1;
430         }
431         if (empty) {
432                 FN(PW,free)(pw);
433                 return 0;
434         }
435
436         data->res = FN(FN(UNION,add),PARTS)(data->res, pw);
437
438         return 0;
439 }
440
441 static __isl_give UNION *match_set_op(__isl_take UNION *u,
442         __isl_take isl_union_set *uset,
443         __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
444 {
445         S(UNION,match_set_data) data = { NULL, NULL, fn };
446
447         u = FN(UNION,align_params)(u, isl_union_set_get_dim(uset));
448         uset = isl_union_set_align_params(uset, FN(UNION,get_dim)(u));
449
450         if (!u || !uset)
451                 goto error;
452
453         data.uset = uset;
454 #ifdef HAS_TYPE
455         data.res = FN(UNION,alloc)(isl_dim_copy(u->dim), u->type, u->table.n);
456 #else
457         data.res = FN(UNION,alloc)(isl_dim_copy(u->dim), u->table.n);
458 #endif
459         if (isl_hash_table_foreach(u->dim->ctx, &u->table,
460                                    &match_set_entry, &data) < 0)
461                 goto error;
462
463         FN(UNION,free)(u);
464         isl_union_set_free(uset);
465         return data.res;
466 error:
467         FN(UNION,free)(u);
468         isl_union_set_free(uset);
469         FN(UNION,free)(data.res);
470         return NULL;
471 }
472
473 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
474         __isl_take isl_union_set *uset)
475 {
476         return match_set_op(u, uset, &FN(PW,intersect_domain));
477 }
478
479 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
480         __isl_take isl_union_set *uset)
481 {
482         return match_set_op(u, uset, &FN(PW,gist));
483 }
484
485 __isl_give isl_qpolynomial *FN(UNION,eval)(__isl_take UNION *u,
486         __isl_take isl_point *pnt)
487 {
488         uint32_t hash;
489         struct isl_hash_table_entry *entry;
490         isl_qpolynomial *qp;
491
492         if (!u || !pnt)
493                 goto error;
494
495         hash = isl_dim_get_hash(pnt->dim);
496         entry = isl_hash_table_find(u->dim->ctx, &u->table,
497                                     hash, &has_dim, pnt->dim, 0);
498         if (!entry) {
499                 qp = isl_qpolynomial_zero(isl_dim_copy(pnt->dim));
500                 isl_point_free(pnt);
501         } else {
502                 qp = FN(PART,eval)(FN(PART,copy)(entry->data), pnt);
503         }
504         FN(UNION,free)(u);
505         return qp;
506 error:
507         FN(UNION,free)(u);
508         isl_point_free(pnt);
509         return NULL;
510 }
511
512 static int coalesce_entry(void **entry, void *user)
513 {
514         PW **pw = (PW **)entry;
515
516         *pw = FN(PW,coalesce)(*pw);
517         if (!*pw)
518                 return -1;
519
520         return 0;
521 }
522
523 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
524 {
525         if (!u)
526                 return NULL;
527
528         if (isl_hash_table_foreach(u->dim->ctx, &u->table,
529                                    &coalesce_entry, NULL) < 0)
530                 goto error;
531
532         return u;
533 error:
534         FN(UNION,free)(u);
535         return NULL;
536 }
537
538 static int domain(__isl_take PART *part, void *user)
539 {
540         isl_union_set **uset = (isl_union_set **)user;
541
542         *uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
543
544         return 0;
545 }
546
547 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
548 {
549         isl_union_set *uset;
550
551         uset = isl_union_set_empty(FN(UNION,get_dim)(u));
552         if (FN(FN(UNION,foreach),PARTS)(u, &domain, &uset) < 0)
553                 goto error;
554
555         FN(UNION,free)(u);
556         
557         return uset;
558 error:
559         isl_union_set_free(uset);
560         FN(UNION,free)(u);
561         return NULL;
562 }
563
564 static int mul_isl_int(void **entry, void *user)
565 {
566         PW **pw = (PW **)entry;
567         isl_int *v = user;
568
569         *pw = FN(PW,mul_isl_int)(*pw, *v);
570         if (!*pw)
571                 return -1;
572
573         return 0;
574 }
575
576 __isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
577 {
578         if (isl_int_is_one(v))
579                 return u;
580
581         if (u && isl_int_is_zero(v)) {
582                 UNION *zero;
583                 isl_dim *dim = FN(UNION,get_dim)(u);
584 #ifdef HAS_TYPE
585                 zero = FN(UNION,zero)(dim, u->type);
586 #else
587                 zero = FN(UNION,zero)(dim);
588 #endif
589                 FN(UNION,free)(u);
590                 return zero;
591         }
592
593         u = FN(UNION,cow)(u);
594         if (!u)
595                 return NULL;
596
597 #ifdef HAS_TYPE
598         if (isl_int_is_neg(v))
599                 u->type = isl_fold_type_negate(u->type);
600 #endif
601         if (isl_hash_table_foreach(u->dim->ctx, &u->table, &mul_isl_int, v) < 0)
602                 goto error;
603
604         return u;
605 error:
606         FN(UNION,free)(u);
607         return NULL;
608 }