add union_pw_*_intersect_params
[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_space *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_space *FN(UNION,get_space)(__isl_keep UNION *u)
34 {
35         if (!u)
36                 return NULL;
37         return isl_space_copy(u->dim);
38 }
39
40 #ifdef HAS_TYPE
41 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim,
42         enum isl_fold type, int size)
43 #else
44 static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim, int size)
45 #endif
46 {
47         UNION *u;
48
49         dim = isl_space_params(dim);
50         if (!dim)
51                 return NULL;
52
53         u = isl_calloc_type(dim->ctx, UNION);
54         if (!u)
55                 return NULL;
56
57         u->ref = 1;
58 #ifdef HAS_TYPE
59         u->type = type;
60 #endif
61         u->dim = dim;
62         if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
63                 goto error;
64
65         return u;
66 error:
67         isl_space_free(dim);
68         FN(UNION,free)(u);
69         return NULL;
70 }
71
72 #ifdef HAS_TYPE
73 __isl_give UNION *FN(UNION,zero)(__isl_take isl_space *dim, enum isl_fold type)
74 {
75         return FN(UNION,alloc)(dim, type, 16);
76 }
77 #else
78 __isl_give UNION *FN(UNION,zero)(__isl_take isl_space *dim)
79 {
80         return FN(UNION,alloc)(dim, 16);
81 }
82 #endif
83
84 __isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
85 {
86         if (!u)
87                 return NULL;
88
89         u->ref++;
90         return u;
91 }
92
93 S(UNION,foreach_data)
94 {
95         int (*fn)(__isl_take PART *part, void *user);
96         void *user;
97 };
98
99 static int call_on_copy(void **entry, void *user)
100 {
101         PART *part = *entry;
102         S(UNION,foreach_data) *data = (S(UNION,foreach_data) *)user;
103
104         return data->fn(FN(PART,copy)(part), data->user);
105 }
106
107 int FN(FN(UNION,foreach),PARTS)(__isl_keep UNION *u,
108         int (*fn)(__isl_take PART *part, void *user), void *user)
109 {
110         S(UNION,foreach_data) data = { fn, user };
111
112         if (!u)
113                 return -1;
114
115         return isl_hash_table_foreach(u->dim->ctx, &u->table,
116                                       &call_on_copy, &data);
117 }
118
119 static int has_dim(const void *entry, const void *val)
120 {
121         PART *part = (PART *)entry;
122         isl_space *dim = (isl_space *)val;
123
124         return isl_space_is_equal(part->dim, dim);
125 }
126
127 __isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
128         __isl_take isl_space *dim)
129 {
130         uint32_t hash;
131         struct isl_hash_table_entry *entry;
132
133         if (!u || !dim)
134                 goto error;
135
136         hash = isl_space_get_hash(dim);
137         entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
138                                     &has_dim, dim, 0);
139         if (!entry)
140 #ifdef HAS_TYPE
141                 return FN(PART,zero)(dim, u->type);
142 #else
143                 return FN(PART,zero)(dim);
144 #endif
145         isl_space_free(dim);
146         return FN(PART,copy)(entry->data);
147 error:
148         isl_space_free(dim);
149         return NULL;
150 }
151
152 __isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
153         __isl_take PART *part)
154 {
155         uint32_t hash;
156         struct isl_hash_table_entry *entry;
157
158         if (!part)
159                 goto error;
160
161         if (FN(PART,is_zero)(part)) {
162                 FN(PART,free)(part);
163                 return u;
164         }
165
166         u = FN(UNION,cow)(u);
167
168         if (!u)
169                 goto error;
170
171         isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
172                                               isl_dim_param), goto error);
173
174         hash = isl_space_get_hash(part->dim);
175         entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
176                                     &has_dim, part->dim, 1);
177         if (!entry)
178                 goto error;
179
180         if (!entry->data)
181                 entry->data = part;
182         else {
183                 entry->data = FN(PART,add)(entry->data, FN(PART,copy)(part));
184                 if (!entry->data)
185                         goto error;
186                 FN(PART,free)(part);
187                 if (FN(PART,is_zero)(entry->data)) {
188                         FN(PART,free)(entry->data);
189                         isl_hash_table_remove(u->dim->ctx, &u->table, entry);
190                 }
191         }
192
193         return u;
194 error:
195         FN(PART,free)(part);
196         FN(UNION,free)(u);
197         return NULL;
198 }
199
200 static int add_part(__isl_take PART *part, void *user)
201 {
202         UNION **u = (UNION **)user;
203
204         *u = FN(FN(UNION,add),PARTS)(*u, part);
205
206         return 0;
207 }
208
209 __isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
210 {
211         UNION *dup;
212
213         if (!u)
214                 return NULL;
215
216 #ifdef HAS_TYPE
217         dup = FN(UNION,zero)(isl_space_copy(u->dim), u->type);
218 #else
219         dup = FN(UNION,zero)(isl_space_copy(u->dim));
220 #endif
221         if (FN(FN(UNION,foreach),PARTS)(u, &add_part, &dup) < 0)
222                 goto error;
223         return dup;
224 error:
225         FN(UNION,free)(dup);
226         return NULL;
227 }
228
229 __isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
230 {
231         if (!u)
232                 return NULL;
233
234         if (u->ref == 1)
235                 return u;
236         u->ref--;
237         return FN(UNION,dup)(u);
238 }
239
240 static int free_u_entry(void **entry, void *user)
241 {
242         PART *part = *entry;
243         FN(PART,free)(part);
244         return 0;
245 }
246
247 void FN(UNION,free)(__isl_take UNION *u)
248 {
249         if (!u)
250                 return;
251
252         if (--u->ref > 0)
253                 return;
254
255         isl_hash_table_foreach(u->dim->ctx, &u->table, &free_u_entry, NULL);
256         isl_hash_table_clear(&u->table);
257         isl_space_free(u->dim);
258         free(u);
259 }
260
261 S(UNION,align) {
262         isl_reordering *exp;
263         UNION *res;
264 };
265
266 #ifdef ALIGN_DOMAIN
267 static int align_entry(__isl_take PART *part, void *user)
268 {
269         isl_reordering *exp;
270         S(UNION,align) *data = user;
271
272         exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
273                                     FN(PART,get_domain_space)(part));
274
275         data->res = FN(FN(UNION,add),PARTS)(data->res,
276                                             FN(PART,realign_domain)(part, exp));
277
278         return 0;
279 }
280 #else
281 static int align_entry(__isl_take PART *part, void *user)
282 {
283         isl_reordering *exp;
284         S(UNION,align) *data = user;
285
286         exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
287                                     FN(PART,get_space)(part));
288
289         data->res = FN(FN(UNION,add),PARTS)(data->res,
290                                             FN(PART,realign)(part, exp));
291
292         return 0;
293 }
294 #endif
295
296 __isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
297         __isl_take isl_space *model)
298 {
299         S(UNION,align) data = { NULL, NULL };
300
301         if (!u || !model)
302                 goto error;
303
304         if (isl_space_match(u->dim, isl_dim_param, model, isl_dim_param)) {
305                 isl_space_free(model);
306                 return u;
307         }
308
309         data.exp = isl_parameter_alignment_reordering(u->dim, model);
310         if (!data.exp)
311                 goto error;
312
313 #ifdef HAS_TYPE
314         data.res = FN(UNION,alloc)(isl_space_copy(data.exp->dim),
315                                                 u->type, u->table.n);
316 #else
317         data.res = FN(UNION,alloc)(isl_space_copy(data.exp->dim), u->table.n);
318 #endif
319         if (FN(FN(UNION,foreach),PARTS)(u, &align_entry, &data) < 0)
320                 goto error;
321
322         isl_reordering_free(data.exp);
323         FN(UNION,free)(u);
324         isl_space_free(model);
325         return data.res;
326 error:
327         isl_reordering_free(data.exp);
328         FN(UNION,free)(u);
329         FN(UNION,free)(data.res);
330         isl_space_free(model);
331         return NULL;
332 }
333
334 __isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
335 {
336         u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
337         u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
338
339         u1 = FN(UNION,cow)(u1);
340
341         if (!u1 || !u2)
342                 goto error;
343
344         if (FN(FN(UNION,foreach),PARTS)(u2, &add_part, &u1) < 0)
345                 goto error;
346
347         FN(UNION,free)(u2);
348
349         return u1;
350 error:
351         FN(UNION,free)(u1);
352         FN(UNION,free)(u2);
353         return NULL;
354 }
355
356 __isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
357 {
358         isl_space *dim;
359         UNION *u;
360
361         if (!part)
362                 return NULL;
363
364         dim = FN(PART,get_space)(part);
365         dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
366         dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
367 #ifdef HAS_TYPE
368         u = FN(UNION,zero)(dim, part->type);
369 #else
370         u = FN(UNION,zero)(dim);
371 #endif
372         u = FN(FN(UNION,add),PARTS)(u, part);
373
374         return u;
375 }
376
377 S(UNION,match_bin_data) {
378         UNION *u2;
379         UNION *res;
380 };
381
382 /* This function is currently only used from isl_polynomial.c
383  * and not from isl_fold.c.
384  */
385 static __isl_give UNION *match_bin_op(__isl_take UNION *u1,
386         __isl_take UNION *u2,
387         int (*fn)(void **, void *)) __attribute__ ((unused));
388 static __isl_give UNION *match_bin_op(__isl_take UNION *u1,
389         __isl_take UNION *u2, int (*fn)(void **, void *))
390 {
391         S(UNION,match_bin_data) data = { NULL, NULL };
392
393         u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
394         u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
395
396         if (!u1 || !u2)
397                 goto error;
398
399         data.u2 = u2;
400 #ifdef HAS_TYPE
401         data.res = FN(UNION,alloc)(isl_space_copy(u1->dim), u1->type, u1->table.n);
402 #else
403         data.res = FN(UNION,alloc)(isl_space_copy(u1->dim), u1->table.n);
404 #endif
405         if (isl_hash_table_foreach(u1->dim->ctx, &u1->table, fn, &data) < 0)
406                 goto error;
407
408         FN(UNION,free)(u1);
409         FN(UNION,free)(u2);
410         return data.res;
411 error:
412         FN(UNION,free)(u1);
413         FN(UNION,free)(u2);
414         FN(UNION,free)(data.res);
415         return NULL;
416 }
417
418 S(UNION,any_set_data) {
419         isl_set *set;
420         UNION *res;
421         __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
422 };
423
424 static int any_set_entry(void **entry, void *user)
425 {
426         S(UNION,any_set_data) *data = user;
427         PW *pw = *entry;
428         int empty;
429
430         pw = FN(PW,copy)(pw);
431         pw = data->fn(pw, isl_set_copy(data->set));
432
433         empty = FN(PW,is_zero)(pw);
434         if (empty < 0) {
435                 FN(PW,free)(pw);
436                 return -1;
437         }
438         if (empty) {
439                 FN(PW,free)(pw);
440                 return 0;
441         }
442
443         data->res = FN(FN(UNION,add),PARTS)(data->res, pw);
444
445         return 0;
446 }
447
448 /* Update each element of "u" by calling "fn" on the element and "set".
449  */
450 static __isl_give UNION *any_set_op(__isl_take UNION *u,
451         __isl_take isl_set *set,
452         __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
453 {
454         S(UNION,any_set_data) data = { NULL, NULL, fn };
455
456         u = FN(UNION,align_params)(u, isl_set_get_space(set));
457         set = isl_set_align_params(set, FN(UNION,get_space)(u));
458
459         if (!u || !set)
460                 goto error;
461
462         data.set = set;
463 #ifdef HAS_TYPE
464         data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->type, u->table.n);
465 #else
466         data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->table.n);
467 #endif
468         if (isl_hash_table_foreach(u->dim->ctx, &u->table,
469                                    &any_set_entry, &data) < 0)
470                 goto error;
471
472         FN(UNION,free)(u);
473         isl_set_free(set);
474         return data.res;
475 error:
476         FN(UNION,free)(u);
477         isl_set_free(set);
478         FN(UNION,free)(data.res);
479         return NULL;
480 }
481
482 /* Intersect the domain of "u" with the parameter domain "context".
483  */
484 __isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
485         __isl_take isl_set *set)
486 {
487         return any_set_op(u, set, &FN(PW,intersect_params));
488 }
489
490 S(UNION,match_set_data) {
491         isl_union_set *uset;
492         UNION *res;
493         __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
494 };
495
496 static int set_has_dim(const void *entry, const void *val)
497 {
498         isl_set *set = (isl_set *)entry;
499         isl_space *dim = (isl_space *)val;
500
501         return isl_space_is_equal(set->dim, dim);
502 }
503
504 static int match_set_entry(void **entry, void *user)
505 {
506         S(UNION,match_set_data) *data = user;
507         uint32_t hash;
508         struct isl_hash_table_entry *entry2;
509         PW *pw = *entry;
510         int empty;
511
512         hash = isl_space_get_hash(pw->dim);
513         entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
514                                      hash, &set_has_dim, pw->dim, 0);
515         if (!entry2)
516                 return 0;
517
518         pw = FN(PW,copy)(pw);
519         pw = data->fn(pw, isl_set_copy(entry2->data));
520
521         empty = FN(PW,is_zero)(pw);
522         if (empty < 0) {
523                 FN(PW,free)(pw);
524                 return -1;
525         }
526         if (empty) {
527                 FN(PW,free)(pw);
528                 return 0;
529         }
530
531         data->res = FN(FN(UNION,add),PARTS)(data->res, pw);
532
533         return 0;
534 }
535
536 static __isl_give UNION *match_set_op(__isl_take UNION *u,
537         __isl_take isl_union_set *uset,
538         __isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
539 {
540         S(UNION,match_set_data) data = { NULL, NULL, fn };
541
542         u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
543         uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
544
545         if (!u || !uset)
546                 goto error;
547
548         data.uset = uset;
549 #ifdef HAS_TYPE
550         data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->type, u->table.n);
551 #else
552         data.res = FN(UNION,alloc)(isl_space_copy(u->dim), u->table.n);
553 #endif
554         if (isl_hash_table_foreach(u->dim->ctx, &u->table,
555                                    &match_set_entry, &data) < 0)
556                 goto error;
557
558         FN(UNION,free)(u);
559         isl_union_set_free(uset);
560         return data.res;
561 error:
562         FN(UNION,free)(u);
563         isl_union_set_free(uset);
564         FN(UNION,free)(data.res);
565         return NULL;
566 }
567
568 __isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
569         __isl_take isl_union_set *uset)
570 {
571         return match_set_op(u, uset, &FN(PW,intersect_domain));
572 }
573
574 __isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
575         __isl_take isl_union_set *uset)
576 {
577         return match_set_op(u, uset, &FN(PW,gist));
578 }
579
580 __isl_give isl_qpolynomial *FN(UNION,eval)(__isl_take UNION *u,
581         __isl_take isl_point *pnt)
582 {
583         uint32_t hash;
584         struct isl_hash_table_entry *entry;
585         isl_space *space;
586         isl_qpolynomial *qp;
587
588         if (!u || !pnt)
589                 goto error;
590
591         space = isl_space_copy(pnt->dim);
592         space = isl_space_from_domain(space);
593         space = isl_space_add_dims(space, isl_dim_out, 1);
594         if (!space)
595                 goto error;
596         hash = isl_space_get_hash(space);
597         entry = isl_hash_table_find(u->dim->ctx, &u->table,
598                                     hash, &has_dim, space, 0);
599         isl_space_free(space);
600         if (!entry) {
601                 qp = isl_qpolynomial_zero_on_domain(isl_space_copy(pnt->dim));
602                 isl_point_free(pnt);
603         } else {
604                 qp = FN(PART,eval)(FN(PART,copy)(entry->data), pnt);
605         }
606         FN(UNION,free)(u);
607         return qp;
608 error:
609         FN(UNION,free)(u);
610         isl_point_free(pnt);
611         return NULL;
612 }
613
614 static int coalesce_entry(void **entry, void *user)
615 {
616         PW **pw = (PW **)entry;
617
618         *pw = FN(PW,coalesce)(*pw);
619         if (!*pw)
620                 return -1;
621
622         return 0;
623 }
624
625 __isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
626 {
627         if (!u)
628                 return NULL;
629
630         if (isl_hash_table_foreach(u->dim->ctx, &u->table,
631                                    &coalesce_entry, NULL) < 0)
632                 goto error;
633
634         return u;
635 error:
636         FN(UNION,free)(u);
637         return NULL;
638 }
639
640 static int domain(__isl_take PART *part, void *user)
641 {
642         isl_union_set **uset = (isl_union_set **)user;
643
644         *uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
645
646         return 0;
647 }
648
649 __isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
650 {
651         isl_union_set *uset;
652
653         uset = isl_union_set_empty(FN(UNION,get_space)(u));
654         if (FN(FN(UNION,foreach),PARTS)(u, &domain, &uset) < 0)
655                 goto error;
656
657         FN(UNION,free)(u);
658         
659         return uset;
660 error:
661         isl_union_set_free(uset);
662         FN(UNION,free)(u);
663         return NULL;
664 }
665
666 static int mul_isl_int(void **entry, void *user)
667 {
668         PW **pw = (PW **)entry;
669         isl_int *v = user;
670
671         *pw = FN(PW,mul_isl_int)(*pw, *v);
672         if (!*pw)
673                 return -1;
674
675         return 0;
676 }
677
678 __isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
679 {
680         if (isl_int_is_one(v))
681                 return u;
682
683         if (u && isl_int_is_zero(v)) {
684                 UNION *zero;
685                 isl_space *dim = FN(UNION,get_space)(u);
686 #ifdef HAS_TYPE
687                 zero = FN(UNION,zero)(dim, u->type);
688 #else
689                 zero = FN(UNION,zero)(dim);
690 #endif
691                 FN(UNION,free)(u);
692                 return zero;
693         }
694
695         u = FN(UNION,cow)(u);
696         if (!u)
697                 return NULL;
698
699 #ifdef HAS_TYPE
700         if (isl_int_is_neg(v))
701                 u->type = isl_fold_type_negate(u->type);
702 #endif
703         if (isl_hash_table_foreach(u->dim->ctx, &u->table, &mul_isl_int, v) < 0)
704                 goto error;
705
706         return u;
707 error:
708         FN(UNION,free)(u);
709         return NULL;
710 }
711
712 S(UNION,plain_is_equal_data)
713 {
714         UNION *u2;
715         int is_equal;
716 };
717
718 static int plain_is_equal_entry(void **entry, void *user)
719 {
720         S(UNION,plain_is_equal_data) *data = user;
721         uint32_t hash;
722         struct isl_hash_table_entry *entry2;
723         PW *pw = *entry;
724
725         hash = isl_space_get_hash(pw->dim);
726         entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
727                                      hash, &has_dim, pw->dim, 0);
728         if (!entry2) {
729                 data->is_equal = 0;
730                 return -1;
731         }
732
733         data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
734         if (data->is_equal < 0 || !data->is_equal)
735                 return -1;
736
737         return 0;
738 }
739
740 int FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
741 {
742         S(UNION,plain_is_equal_data) data = { NULL, 1 };
743
744         if (!u1 || !u2)
745                 return -1;
746         if (u1 == u2)
747                 return 1;
748         if (u1->table.n != u2->table.n)
749                 return 0;
750
751         u1 = FN(UNION,copy)(u1);
752         u2 = FN(UNION,copy)(u2);
753         u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
754         u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
755         if (!u1 || !u2)
756                 goto error;
757
758         data.u2 = u2;
759         if (isl_hash_table_foreach(u1->dim->ctx, &u1->table,
760                                    &plain_is_equal_entry, &data) < 0 &&
761             data.is_equal)
762                 goto error;
763
764         FN(UNION,free)(u1);
765         FN(UNION,free)(u2);
766
767         return data.is_equal;
768 error:
769         FN(UNION,free)(u1);
770         FN(UNION,free)(u2);
771         return -1;
772 }