isl_ctx_free: properly free allocated options
[platform/upstream/isl.git] / isl_pw_templ.c
1 #include <isl/aff.h>
2
3 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
4 #define FN(TYPE,NAME) xFN(TYPE,NAME)
5 #define xS(TYPE,NAME) struct TYPE ## _ ## NAME
6 #define S(TYPE,NAME) xS(TYPE,NAME)
7
8 #ifdef HAS_TYPE
9 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim,
10         enum isl_fold type, int n)
11 #else
12 __isl_give PW *FN(PW,alloc_size)(__isl_take isl_space *dim, int n)
13 #endif
14 {
15         isl_ctx *ctx;
16         struct PW *pw;
17
18         if (!dim)
19                 return NULL;
20         ctx = isl_space_get_ctx(dim);
21         isl_assert(ctx, n >= 0, goto error);
22         pw = isl_alloc(ctx, struct PW,
23                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
24         if (!pw)
25                 goto error;
26
27         pw->ref = 1;
28 #ifdef HAS_TYPE
29         pw->type = type;
30 #endif
31         pw->size = n;
32         pw->n = 0;
33         pw->dim = dim;
34         return pw;
35 error:
36         isl_space_free(dim);
37         return NULL;
38 }
39
40 #ifdef HAS_TYPE
41 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
42 {
43         return FN(PW,alloc_size)(dim, type, 0);
44 }
45 #else
46 __isl_give PW *FN(PW,ZERO)(__isl_take isl_space *dim)
47 {
48         return FN(PW,alloc_size)(dim, 0);
49 }
50 #endif
51
52 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
53         __isl_take isl_set *set, __isl_take EL *el)
54 {
55         isl_ctx *ctx;
56         isl_space *el_dim = NULL;
57
58         if (!pw || !set || !el)
59                 goto error;
60
61         if (isl_set_plain_is_empty(set) || FN(EL,EL_IS_ZERO)(el)) {
62                 isl_set_free(set);
63                 FN(EL,free)(el);
64                 return pw;
65         }
66
67         ctx = isl_set_get_ctx(set);
68 #ifdef HAS_TYPE
69         if (pw->type != el->type)
70                 isl_die(ctx, isl_error_invalid, "fold types don't match",
71                         goto error);
72 #endif
73         el_dim = FN(EL,get_space(el));
74         isl_assert(ctx, isl_space_is_equal(pw->dim, el_dim), goto error);
75         isl_assert(ctx, pw->n < pw->size, goto error);
76
77         pw->p[pw->n].set = set;
78         pw->p[pw->n].FIELD = el;
79         pw->n++;
80         
81         isl_space_free(el_dim);
82         return pw;
83 error:
84         isl_space_free(el_dim);
85         FN(PW,free)(pw);
86         isl_set_free(set);
87         FN(EL,free)(el);
88         return NULL;
89 }
90
91 #ifdef HAS_TYPE
92 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
93         __isl_take isl_set *set, __isl_take EL *el)
94 #else
95 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
96 #endif
97 {
98         PW *pw;
99
100         if (!set || !el)
101                 goto error;
102
103 #ifdef HAS_TYPE
104         pw = FN(PW,alloc_size)(FN(EL,get_space)(el), type, 1);
105 #else
106         pw = FN(PW,alloc_size)(FN(EL,get_space)(el), 1);
107 #endif
108
109         return FN(PW,add_piece)(pw, set, el);
110 error:
111         isl_set_free(set);
112         FN(EL,free)(el);
113         return NULL;
114 }
115
116 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
117 {
118         int i;
119         PW *dup;
120
121         if (!pw)
122                 return NULL;
123
124 #ifdef HAS_TYPE
125         dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, pw->n);
126 #else
127         dup = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->n);
128 #endif
129         if (!dup)
130                 return NULL;
131
132         for (i = 0; i < pw->n; ++i)
133                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
134                                             FN(EL,copy)(pw->p[i].FIELD));
135
136         return dup;
137 }
138
139 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
140 {
141         if (!pw)
142                 return NULL;
143
144         if (pw->ref == 1)
145                 return pw;
146         pw->ref--;
147         return FN(PW,dup)(pw);
148 }
149
150 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
151 {
152         if (!pw)
153                 return NULL;
154
155         pw->ref++;
156         return pw;
157 }
158
159 void *FN(PW,free)(__isl_take PW *pw)
160 {
161         int i;
162
163         if (!pw)
164                 return NULL;
165         if (--pw->ref > 0)
166                 return NULL;
167
168         for (i = 0; i < pw->n; ++i) {
169                 isl_set_free(pw->p[i].set);
170                 FN(EL,free)(pw->p[i].FIELD);
171         }
172         isl_space_free(pw->dim);
173         free(pw);
174
175         return NULL;
176 }
177
178 const char *FN(PW,get_dim_name)(__isl_keep PW *pw, enum isl_dim_type type,
179         unsigned pos)
180 {
181         return pw ? isl_space_get_dim_name(pw->dim, type, pos) : NULL;
182 }
183
184 int FN(PW,has_dim_id)(__isl_keep PW *pw, enum isl_dim_type type, unsigned pos)
185 {
186         return pw ? isl_space_has_dim_id(pw->dim, type, pos) : -1;
187 }
188
189 __isl_give isl_id *FN(PW,get_dim_id)(__isl_keep PW *pw, enum isl_dim_type type,
190         unsigned pos)
191 {
192         return pw ? isl_space_get_dim_id(pw->dim, type, pos) : NULL;
193 }
194
195 int FN(PW,has_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
196 {
197         return pw ? isl_space_has_tuple_name(pw->dim, type) : -1;
198 }
199
200 const char *FN(PW,get_tuple_name)(__isl_keep PW *pw, enum isl_dim_type type)
201 {
202         return pw ? isl_space_get_tuple_name(pw->dim, type) : NULL;
203 }
204
205 int FN(PW,has_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
206 {
207         return pw ? isl_space_has_tuple_id(pw->dim, type) : -1;
208 }
209
210 __isl_give isl_id *FN(PW,get_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type)
211 {
212         return pw ? isl_space_get_tuple_id(pw->dim, type) : NULL;
213 }
214
215 int FN(PW,IS_ZERO)(__isl_keep PW *pw)
216 {
217         if (!pw)
218                 return -1;
219
220         return pw->n == 0;
221 }
222
223 #ifndef NO_REALIGN
224 __isl_give PW *FN(PW,realign_domain)(__isl_take PW *pw,
225         __isl_take isl_reordering *exp)
226 {
227         int i;
228
229         pw = FN(PW,cow)(pw);
230         if (!pw || !exp)
231                 goto error;
232
233         for (i = 0; i < pw->n; ++i) {
234                 pw->p[i].set = isl_set_realign(pw->p[i].set,
235                                                     isl_reordering_copy(exp));
236                 if (!pw->p[i].set)
237                         goto error;
238                 pw->p[i].FIELD = FN(EL,realign_domain)(pw->p[i].FIELD,
239                                                     isl_reordering_copy(exp));
240                 if (!pw->p[i].FIELD)
241                         goto error;
242         }
243
244         pw = FN(PW,reset_domain_space)(pw, isl_space_copy(exp->dim));
245
246         isl_reordering_free(exp);
247         return pw;
248 error:
249         isl_reordering_free(exp);
250         FN(PW,free)(pw);
251         return NULL;
252 }
253
254 /* Align the parameters of "pw" to those of "model".
255  */
256 __isl_give PW *FN(PW,align_params)(__isl_take PW *pw, __isl_take isl_space *model)
257 {
258         isl_ctx *ctx;
259
260         if (!pw || !model)
261                 goto error;
262
263         ctx = isl_space_get_ctx(model);
264         if (!isl_space_has_named_params(model))
265                 isl_die(ctx, isl_error_invalid,
266                         "model has unnamed parameters", goto error);
267         if (!isl_space_has_named_params(pw->dim))
268                 isl_die(ctx, isl_error_invalid,
269                         "input has unnamed parameters", goto error);
270         if (!isl_space_match(pw->dim, isl_dim_param, model, isl_dim_param)) {
271                 isl_reordering *exp;
272
273                 model = isl_space_drop_dims(model, isl_dim_in,
274                                         0, isl_space_dim(model, isl_dim_in));
275                 model = isl_space_drop_dims(model, isl_dim_out,
276                                         0, isl_space_dim(model, isl_dim_out));
277                 exp = isl_parameter_alignment_reordering(pw->dim, model);
278                 exp = isl_reordering_extend_space(exp,
279                                         FN(PW,get_domain_space)(pw));
280                 pw = FN(PW,realign_domain)(pw, exp);
281         }
282
283         isl_space_free(model);
284         return pw;
285 error:
286         isl_space_free(model);
287         FN(PW,free)(pw);
288         return NULL;
289 }
290
291 static __isl_give PW *FN(PW,align_params_pw_pw_and)(__isl_take PW *pw1,
292         __isl_take PW *pw2,
293         __isl_give PW *(*fn)(__isl_take PW *pw1, __isl_take PW *pw2))
294 {
295         isl_ctx *ctx;
296
297         if (!pw1 || !pw2)
298                 goto error;
299         if (isl_space_match(pw1->dim, isl_dim_param, pw2->dim, isl_dim_param))
300                 return fn(pw1, pw2);
301         ctx = FN(PW,get_ctx)(pw1);
302         if (!isl_space_has_named_params(pw1->dim) ||
303             !isl_space_has_named_params(pw2->dim))
304                 isl_die(ctx, isl_error_invalid,
305                         "unaligned unnamed parameters", goto error);
306         pw1 = FN(PW,align_params)(pw1, FN(PW,get_space)(pw2));
307         pw2 = FN(PW,align_params)(pw2, FN(PW,get_space)(pw1));
308         return fn(pw1, pw2);
309 error:
310         FN(PW,free)(pw1);
311         FN(PW,free)(pw2);
312         return NULL;
313 }
314
315 static __isl_give PW *FN(PW,align_params_pw_set_and)(__isl_take PW *pw,
316         __isl_take isl_set *set,
317         __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_set *set))
318 {
319         isl_ctx *ctx;
320
321         if (!pw || !set)
322                 goto error;
323         if (isl_space_match(pw->dim, isl_dim_param, set->dim, isl_dim_param))
324                 return fn(pw, set);
325         ctx = FN(PW,get_ctx)(pw);
326         if (!isl_space_has_named_params(pw->dim) ||
327             !isl_space_has_named_params(set->dim))
328                 isl_die(ctx, isl_error_invalid,
329                         "unaligned unnamed parameters", goto error);
330         pw = FN(PW,align_params)(pw, isl_set_get_space(set));
331         set = isl_set_align_params(set, FN(PW,get_space)(pw));
332         return fn(pw, set);
333 error:
334         FN(PW,free)(pw);
335         isl_set_free(set);
336         return NULL;
337 }
338 #endif
339
340 static __isl_give PW *FN(PW,union_add_aligned)(__isl_take PW *pw1,
341         __isl_take PW *pw2)
342 {
343         int i, j, n;
344         struct PW *res;
345         isl_ctx *ctx;
346         isl_set *set;
347
348         if (!pw1 || !pw2)
349                 goto error;
350
351         ctx = isl_space_get_ctx(pw1->dim);
352 #ifdef HAS_TYPE
353         if (pw1->type != pw2->type)
354                 isl_die(ctx, isl_error_invalid,
355                         "fold types don't match", goto error);
356 #endif
357         isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
358
359         if (FN(PW,IS_ZERO)(pw1)) {
360                 FN(PW,free)(pw1);
361                 return pw2;
362         }
363
364         if (FN(PW,IS_ZERO)(pw2)) {
365                 FN(PW,free)(pw2);
366                 return pw1;
367         }
368
369         n = (pw1->n + 1) * (pw2->n + 1);
370 #ifdef HAS_TYPE
371         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), pw1->type, n);
372 #else
373         res = FN(PW,alloc_size)(isl_space_copy(pw1->dim), n);
374 #endif
375
376         for (i = 0; i < pw1->n; ++i) {
377                 set = isl_set_copy(pw1->p[i].set);
378                 for (j = 0; j < pw2->n; ++j) {
379                         struct isl_set *common;
380                         EL *sum;
381                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
382                                                 isl_set_copy(pw2->p[j].set));
383                         if (isl_set_plain_is_empty(common)) {
384                                 isl_set_free(common);
385                                 continue;
386                         }
387                         set = isl_set_subtract(set,
388                                         isl_set_copy(pw2->p[j].set));
389
390                         sum = FN(EL,add_on_domain)(common,
391                                                    FN(EL,copy)(pw1->p[i].FIELD),
392                                                    FN(EL,copy)(pw2->p[j].FIELD));
393
394                         res = FN(PW,add_piece)(res, common, sum);
395                 }
396                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
397         }
398
399         for (j = 0; j < pw2->n; ++j) {
400                 set = isl_set_copy(pw2->p[j].set);
401                 for (i = 0; i < pw1->n; ++i)
402                         set = isl_set_subtract(set,
403                                         isl_set_copy(pw1->p[i].set));
404                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
405         }
406
407         FN(PW,free)(pw1);
408         FN(PW,free)(pw2);
409
410         return res;
411 error:
412         FN(PW,free)(pw1);
413         FN(PW,free)(pw2);
414         return NULL;
415 }
416
417 /* Private version of "union_add".  For isl_pw_qpolynomial and
418  * isl_pw_qpolynomial_fold, we prefer to simply call it "add".
419  */
420 static __isl_give PW *FN(PW,union_add_)(__isl_take PW *pw1, __isl_take PW *pw2)
421 {
422         return FN(PW,align_params_pw_pw_and)(pw1, pw2,
423                                                 &FN(PW,union_add_aligned));
424 }
425
426 /* Make sure "pw" has room for at least "n" more pieces.
427  *
428  * If there is only one reference to pw, we extend it in place.
429  * Otherwise, we create a new PW and copy the pieces.
430  */
431 static __isl_give PW *FN(PW,grow)(__isl_take PW *pw, int n)
432 {
433         int i;
434         isl_ctx *ctx;
435         PW *res;
436
437         if (!pw)
438                 return NULL;
439         if (pw->n + n <= pw->size)
440                 return pw;
441         ctx = FN(PW,get_ctx)(pw);
442         n += pw->n;
443         if (pw->ref == 1) {
444                 res = isl_realloc(ctx, pw, struct PW,
445                             sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
446                 if (!res)
447                         return FN(PW,free)(pw);
448                 res->size = n;
449                 return res;
450         }
451 #ifdef HAS_TYPE
452         res = FN(PW,alloc_size)(isl_space_copy(pw->dim), pw->type, n);
453 #else
454         res = FN(PW,alloc_size)(isl_space_copy(pw->dim), n);
455 #endif
456         if (!res)
457                 return FN(PW,free)(pw);
458         for (i = 0; i < pw->n; ++i)
459                 res = FN(PW,add_piece)(res, isl_set_copy(pw->p[i].set),
460                                             FN(EL,copy)(pw->p[i].FIELD));
461         FN(PW,free)(pw);
462         return res;
463 }
464
465 static __isl_give PW *FN(PW,add_disjoint_aligned)(__isl_take PW *pw1,
466         __isl_take PW *pw2)
467 {
468         int i;
469         isl_ctx *ctx;
470
471         if (!pw1 || !pw2)
472                 goto error;
473
474         if (pw1->size < pw1->n + pw2->n && pw1->n < pw2->n)
475                 return FN(PW,add_disjoint_aligned)(pw2, pw1);
476
477         ctx = isl_space_get_ctx(pw1->dim);
478 #ifdef HAS_TYPE
479         if (pw1->type != pw2->type)
480                 isl_die(ctx, isl_error_invalid,
481                         "fold types don't match", goto error);
482 #endif
483         isl_assert(ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
484
485         if (FN(PW,IS_ZERO)(pw1)) {
486                 FN(PW,free)(pw1);
487                 return pw2;
488         }
489
490         if (FN(PW,IS_ZERO)(pw2)) {
491                 FN(PW,free)(pw2);
492                 return pw1;
493         }
494
495         pw1 = FN(PW,grow)(pw1, pw2->n);
496         if (!pw1)
497                 goto error;
498
499         for (i = 0; i < pw2->n; ++i)
500                 pw1 = FN(PW,add_piece)(pw1,
501                                 isl_set_copy(pw2->p[i].set),
502                                 FN(EL,copy)(pw2->p[i].FIELD));
503
504         FN(PW,free)(pw2);
505
506         return pw1;
507 error:
508         FN(PW,free)(pw1);
509         FN(PW,free)(pw2);
510         return NULL;
511 }
512
513 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
514 {
515         return FN(PW,align_params_pw_pw_and)(pw1, pw2,
516                                                 &FN(PW,add_disjoint_aligned));
517 }
518
519 /* This function is currently only used from isl_aff.c
520  */
521 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
522         __isl_take PW *pw2, __isl_take isl_space *space,
523         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
524         __attribute__ ((unused));
525
526 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
527  * The result of "fn" (and therefore also of this function) lives in "space".
528  */
529 static __isl_give PW *FN(PW,on_shared_domain_in)(__isl_take PW *pw1,
530         __isl_take PW *pw2, __isl_take isl_space *space,
531         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
532 {
533         int i, j, n;
534         PW *res = NULL;
535
536         if (!pw1 || !pw2)
537                 goto error;
538
539         n = pw1->n * pw2->n;
540 #ifdef HAS_TYPE
541         res = FN(PW,alloc_size)(isl_space_copy(space), pw1->type, n);
542 #else
543         res = FN(PW,alloc_size)(isl_space_copy(space), n);
544 #endif
545
546         for (i = 0; i < pw1->n; ++i) {
547                 for (j = 0; j < pw2->n; ++j) {
548                         isl_set *common;
549                         EL *res_ij;
550                         int empty;
551
552                         common = isl_set_intersect(
553                                         isl_set_copy(pw1->p[i].set),
554                                         isl_set_copy(pw2->p[j].set));
555                         empty = isl_set_plain_is_empty(common);
556                         if (empty < 0 || empty) {
557                                 isl_set_free(common);
558                                 if (empty < 0)
559                                         goto error;
560                                 continue;
561                         }
562
563                         res_ij = fn(FN(EL,copy)(pw1->p[i].FIELD),
564                                     FN(EL,copy)(pw2->p[j].FIELD));
565                         res_ij = FN(EL,gist)(res_ij, isl_set_copy(common));
566
567                         res = FN(PW,add_piece)(res, common, res_ij);
568                 }
569         }
570
571         isl_space_free(space);
572         FN(PW,free)(pw1);
573         FN(PW,free)(pw2);
574         return res;
575 error:
576         isl_space_free(space);
577         FN(PW,free)(pw1);
578         FN(PW,free)(pw2);
579         FN(PW,free)(res);
580         return NULL;
581 }
582
583 /* This function is currently only used from isl_aff.c
584  */
585 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
586         __isl_take PW *pw2,
587         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
588         __attribute__ ((unused));
589
590 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
591  * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
592  */
593 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
594         __isl_take PW *pw2,
595         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
596 {
597         isl_space *space;
598
599         if (!pw1 || !pw2)
600                 goto error;
601
602         space = isl_space_copy(pw1->dim);
603         return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
604 error:
605         FN(PW,free)(pw1);
606         FN(PW,free)(pw2);
607         return NULL;
608 }
609
610 #ifndef NO_NEG
611 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
612 {
613         int i;
614
615         if (!pw)
616                 return NULL;
617
618         if (FN(PW,IS_ZERO)(pw))
619                 return pw;
620
621         pw = FN(PW,cow)(pw);
622         if (!pw)
623                 return NULL;
624
625         for (i = 0; i < pw->n; ++i) {
626                 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
627                 if (!pw->p[i].FIELD)
628                         return FN(PW,free)(pw);
629         }
630
631         return pw;
632 }
633
634 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
635 {
636         return FN(PW,add)(pw1, FN(PW,neg)(pw2));
637 }
638 #endif
639
640 #ifndef NO_EVAL
641 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
642         __isl_take isl_point *pnt)
643 {
644         int i;
645         int found = 0;
646         isl_ctx *ctx;
647         isl_space *pnt_dim = NULL;
648         isl_qpolynomial *qp;
649
650         if (!pw || !pnt)
651                 goto error;
652         ctx = isl_point_get_ctx(pnt);
653         pnt_dim = isl_point_get_space(pnt);
654         isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
655                     goto error);
656
657         for (i = 0; i < pw->n; ++i) {
658                 found = isl_set_contains_point(pw->p[i].set, pnt);
659                 if (found < 0)
660                         goto error;
661                 if (found)
662                         break;
663         }
664         if (found)
665                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
666                                             isl_point_copy(pnt));
667         else
668                 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
669         FN(PW,free)(pw);
670         isl_space_free(pnt_dim);
671         isl_point_free(pnt);
672         return qp;
673 error:
674         FN(PW,free)(pw);
675         isl_space_free(pnt_dim);
676         isl_point_free(pnt);
677         return NULL;
678 }
679 #endif
680
681 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
682 {
683         int i;
684         isl_set *dom;
685
686         if (!pw)
687                 return NULL;
688
689         dom = isl_set_empty(FN(PW,get_domain_space)(pw));
690         for (i = 0; i < pw->n; ++i)
691                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
692
693         FN(PW,free)(pw);
694
695         return dom;
696 }
697
698 /* Restrict the domain of "pw" by combining each cell
699  * with "set" through a call to "fn", where "fn" may be
700  * isl_set_intersect or isl_set_intersect_params.
701  */
702 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
703         __isl_take isl_set *set,
704         __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
705                                     __isl_take isl_set *set2))
706 {
707         int i;
708
709         if (!pw || !set)
710                 goto error;
711
712         if (pw->n == 0) {
713                 isl_set_free(set);
714                 return pw;
715         }
716
717         pw = FN(PW,cow)(pw);
718         if (!pw)
719                 goto error;
720
721         for (i = pw->n - 1; i >= 0; --i) {
722                 isl_basic_set *aff;
723                 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
724                 if (!pw->p[i].set)
725                         goto error;
726                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
727                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
728                                                                 aff);
729                 if (!pw->p[i].FIELD)
730                         goto error;
731                 if (isl_set_plain_is_empty(pw->p[i].set)) {
732                         isl_set_free(pw->p[i].set);
733                         FN(EL,free)(pw->p[i].FIELD);
734                         if (i != pw->n - 1)
735                                 pw->p[i] = pw->p[pw->n - 1];
736                         pw->n--;
737                 }
738         }
739         
740         isl_set_free(set);
741         return pw;
742 error:
743         isl_set_free(set);
744         FN(PW,free)(pw);
745         return NULL;
746 }
747
748 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
749         __isl_take isl_set *set)
750 {
751         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
752 }
753
754 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
755         __isl_take isl_set *context)
756 {
757         return FN(PW,align_params_pw_set_and)(pw, context,
758                                         &FN(PW,intersect_domain_aligned));
759 }
760
761 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
762         __isl_take isl_set *set)
763 {
764         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
765 }
766
767 /* Intersect the domain of "pw" with the parameter domain "context".
768  */
769 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
770         __isl_take isl_set *context)
771 {
772         return FN(PW,align_params_pw_set_and)(pw, context,
773                                         &FN(PW,intersect_params_aligned));
774 }
775
776 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
777         __isl_take isl_set *context,
778         __isl_give EL *(*fn_el)(__isl_take EL *el,
779                                     __isl_take isl_set *set),
780         __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
781                                     __isl_take isl_basic_set *bset))
782 {
783         int i;
784         isl_basic_set *hull = NULL;
785
786         if (!pw || !context)
787                 goto error;
788
789         if (pw->n == 0) {
790                 isl_set_free(context);
791                 return pw;
792         }
793
794         if (!isl_space_match(pw->dim, isl_dim_param,
795                                 context->dim, isl_dim_param)) {
796                 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
797                 context = isl_set_align_params(context, FN(PW,get_space)(pw));
798         }
799
800         context = isl_set_compute_divs(context);
801         hull = isl_set_simple_hull(isl_set_copy(context));
802
803         pw = FN(PW,cow)(pw);
804         if (!pw)
805                 goto error;
806
807         for (i = pw->n - 1; i >= 0; --i) {
808                 isl_set *set_i;
809                 int empty;
810
811                 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
812                                                  isl_set_copy(context));
813                 empty = isl_set_plain_is_empty(set_i);
814                 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
815                 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
816                 if (!pw->p[i].FIELD || !pw->p[i].set)
817                         goto error;
818                 if (empty) {
819                         isl_set_free(pw->p[i].set);
820                         FN(EL,free)(pw->p[i].FIELD);
821                         if (i != pw->n - 1)
822                                 pw->p[i] = pw->p[pw->n - 1];
823                         pw->n--;
824                 }
825         }
826
827         isl_basic_set_free(hull);
828         isl_set_free(context);
829
830         return pw;
831 error:
832         FN(PW,free)(pw);
833         isl_basic_set_free(hull);
834         isl_set_free(context);
835         return NULL;
836 }
837
838 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
839         __isl_take isl_set *set)
840 {
841         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
842                                         &isl_set_gist_basic_set);
843 }
844
845 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
846 {
847         return FN(PW,align_params_pw_set_and)(pw, context,
848                                                 &FN(PW,gist_domain_aligned));
849 }
850
851 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
852         __isl_take isl_set *set)
853 {
854         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
855                                         &isl_set_gist_params_basic_set);
856 }
857
858 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
859         __isl_take isl_set *context)
860 {
861         return FN(PW,align_params_pw_set_and)(pw, context,
862                                                 &FN(PW,gist_params_aligned));
863 }
864
865 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
866 {
867         int i, j;
868
869         if (!pw)
870                 return NULL;
871         if (pw->n == 0)
872                 return pw;
873
874         for (i = pw->n - 1; i >= 0; --i) {
875                 for (j = i - 1; j >= 0; --j) {
876                         if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
877                                                         pw->p[j].FIELD))
878                                 continue;
879                         pw->p[j].set = isl_set_union(pw->p[j].set,
880                                                         pw->p[i].set);
881                         FN(EL,free)(pw->p[i].FIELD);
882                         if (i != pw->n - 1)
883                                 pw->p[i] = pw->p[pw->n - 1];
884                         pw->n--;
885                         break;
886                 }
887                 if (j >= 0)
888                         continue;
889                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
890                 if (!pw->p[i].set)
891                         goto error;
892         }
893
894         return pw;
895 error:
896         FN(PW,free)(pw);
897         return NULL;
898 }
899
900 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
901 {
902         return pw ? isl_space_get_ctx(pw->dim) : NULL;
903 }
904
905 #ifndef NO_INVOLVES_DIMS
906 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
907         unsigned first, unsigned n)
908 {
909         int i;
910         enum isl_dim_type set_type;
911
912         if (!pw)
913                 return -1;
914         if (pw->n == 0 || n == 0)
915                 return 0;
916
917         set_type = type == isl_dim_in ? isl_dim_set : type;
918
919         for (i = 0; i < pw->n; ++i) {
920                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
921                                                         type, first, n);
922                 if (involves < 0 || involves)
923                         return involves;
924                 involves = isl_set_involves_dims(pw->p[i].set,
925                                                         set_type, first, n);
926                 if (involves < 0 || involves)
927                         return involves;
928         }
929         return 0;
930 }
931 #endif
932
933 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
934         enum isl_dim_type type, unsigned pos, const char *s)
935 {
936         int i;
937         enum isl_dim_type set_type;
938
939         pw = FN(PW,cow)(pw);
940         if (!pw)
941                 return NULL;
942
943         set_type = type == isl_dim_in ? isl_dim_set : type;
944
945         pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
946         if (!pw->dim)
947                 goto error;
948
949         for (i = 0; i < pw->n; ++i) {
950                 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
951                                                         set_type, pos, s);
952                 if (!pw->p[i].set)
953                         goto error;
954                 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
955                 if (!pw->p[i].FIELD)
956                         goto error;
957         }
958
959         return pw;
960 error:
961         FN(PW,free)(pw);
962         return NULL;
963 }
964
965 #ifndef NO_DROP_DIMS
966 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
967         enum isl_dim_type type, unsigned first, unsigned n)
968 {
969         int i;
970         enum isl_dim_type set_type;
971
972         if (!pw)
973                 return NULL;
974         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
975                 return pw;
976
977         set_type = type == isl_dim_in ? isl_dim_set : type;
978
979         pw = FN(PW,cow)(pw);
980         if (!pw)
981                 return NULL;
982         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
983         if (!pw->dim)
984                 goto error;
985         for (i = 0; i < pw->n; ++i) {
986                 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
987                 if (!pw->p[i].set)
988                         goto error;
989                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
990                 if (!pw->p[i].FIELD)
991                         goto error;
992         }
993
994         return pw;
995 error:
996         FN(PW,free)(pw);
997         return NULL;
998 }
999
1000 /* This function is very similar to drop_dims.
1001  * The only difference is that the cells may still involve
1002  * the specified dimensions.  They are removed using
1003  * isl_set_project_out instead of isl_set_drop.
1004  */
1005 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1006         enum isl_dim_type type, unsigned first, unsigned n)
1007 {
1008         int i;
1009         enum isl_dim_type set_type;
1010
1011         if (!pw)
1012                 return NULL;
1013         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1014                 return pw;
1015
1016         set_type = type == isl_dim_in ? isl_dim_set : type;
1017
1018         pw = FN(PW,cow)(pw);
1019         if (!pw)
1020                 return NULL;
1021         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1022         if (!pw->dim)
1023                 goto error;
1024         for (i = 0; i < pw->n; ++i) {
1025                 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1026                                                         set_type, first, n);
1027                 if (!pw->p[i].set)
1028                         goto error;
1029                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1030                 if (!pw->p[i].FIELD)
1031                         goto error;
1032         }
1033
1034         return pw;
1035 error:
1036         FN(PW,free)(pw);
1037         return NULL;
1038 }
1039
1040 /* Project the domain of pw onto its parameter space.
1041  */
1042 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1043 {
1044         isl_space *space;
1045         unsigned n;
1046
1047         n = FN(PW,dim)(pw, isl_dim_in);
1048         pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1049         space = FN(PW,get_domain_space)(pw);
1050         space = isl_space_params(space);
1051         pw = FN(PW,reset_domain_space)(pw, space);
1052         return pw;
1053 }
1054 #endif
1055
1056 #ifndef NO_INSERT_DIMS
1057 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1058         unsigned first, unsigned n)
1059 {
1060         int i;
1061         enum isl_dim_type set_type;
1062
1063         if (!pw)
1064                 return NULL;
1065         if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1066                 return pw;
1067
1068         set_type = type == isl_dim_in ? isl_dim_set : type;
1069
1070         pw = FN(PW,cow)(pw);
1071         if (!pw)
1072                 return NULL;
1073
1074         pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1075         if (!pw->dim)
1076                 goto error;
1077
1078         for (i = 0; i < pw->n; ++i) {
1079                 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1080                                                             set_type, first, n);
1081                 if (!pw->p[i].set)
1082                         goto error;
1083                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1084                                                                 type, first, n);
1085                 if (!pw->p[i].FIELD)
1086                         goto error;
1087         }
1088
1089         return pw;
1090 error:
1091         FN(PW,free)(pw);
1092         return NULL;
1093 }
1094 #endif
1095
1096 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1097         enum isl_dim_type type, unsigned pos, isl_int v)
1098 {
1099         int i;
1100
1101         if (!pw)
1102                 return NULL;
1103
1104         if (type == isl_dim_in)
1105                 type = isl_dim_set;
1106
1107         pw = FN(PW,cow)(pw);
1108         if (!pw)
1109                 return NULL;
1110         for (i = 0; i < pw->n; ++i) {
1111                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1112                 if (!pw->p[i].set)
1113                         goto error;
1114         }
1115
1116         return pw;
1117 error:
1118         FN(PW,free)(pw);
1119         return NULL;
1120 }
1121
1122 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1123 {
1124         return pw ? isl_space_dim(pw->dim, type) : 0;
1125 }
1126
1127 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1128         enum isl_dim_type type, unsigned first, unsigned n)
1129 {
1130         int i;
1131
1132         if (!pw)
1133                 return NULL;
1134         if (n == 0)
1135                 return pw;
1136
1137         if (type == isl_dim_in)
1138                 type = isl_dim_set;
1139
1140         pw = FN(PW,cow)(pw);
1141         if (!pw)
1142                 return NULL;
1143         if (!pw->dim)
1144                 goto error;
1145         for (i = 0; i < pw->n; ++i) {
1146                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1147                 if (!pw->p[i].set)
1148                         goto error;
1149         }
1150
1151         return pw;
1152 error:
1153         FN(PW,free)(pw);
1154         return NULL;
1155 }
1156
1157 #ifndef NO_OPT
1158 /* Compute the maximal value attained by the piecewise quasipolynomial
1159  * on its domain or zero if the domain is empty.
1160  * In the worst case, the domain is scanned completely,
1161  * so the domain is assumed to be bounded.
1162  */
1163 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1164 {
1165         int i;
1166         isl_qpolynomial *opt;
1167
1168         if (!pw)
1169                 return NULL;
1170
1171         if (pw->n == 0) {
1172                 isl_space *dim = isl_space_copy(pw->dim);
1173                 FN(PW,free)(pw);
1174                 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1175         }
1176
1177         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1178                                         isl_set_copy(pw->p[0].set), max);
1179         for (i = 1; i < pw->n; ++i) {
1180                 isl_qpolynomial *opt_i;
1181                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1182                                                 isl_set_copy(pw->p[i].set), max);
1183                 if (max)
1184                         opt = isl_qpolynomial_max_cst(opt, opt_i);
1185                 else
1186                         opt = isl_qpolynomial_min_cst(opt, opt_i);
1187         }
1188
1189         FN(PW,free)(pw);
1190         return opt;
1191 }
1192
1193 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1194 {
1195         return FN(PW,opt)(pw, 1);
1196 }
1197
1198 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1199 {
1200         return FN(PW,opt)(pw, 0);
1201 }
1202 #endif
1203
1204 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1205 {
1206         return pw ? isl_space_copy(pw->dim) : NULL;
1207 }
1208
1209 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1210 {
1211         return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1212 }
1213
1214 #ifndef NO_RESET_DIM
1215 /* Reset the space of "pw".  Since we don't know if the elements
1216  * represent the spaces themselves or their domains, we pass along
1217  * both when we call their reset_space_and_domain.
1218  */
1219 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1220         __isl_take isl_space *space, __isl_take isl_space *domain)
1221 {
1222         int i;
1223
1224         pw = FN(PW,cow)(pw);
1225         if (!pw || !space || !domain)
1226                 goto error;
1227
1228         for (i = 0; i < pw->n; ++i) {
1229                 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1230                                                  isl_space_copy(domain));
1231                 if (!pw->p[i].set)
1232                         goto error;
1233                 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1234                               isl_space_copy(space), isl_space_copy(domain));
1235                 if (!pw->p[i].FIELD)
1236                         goto error;
1237         }
1238
1239         isl_space_free(domain);
1240
1241         isl_space_free(pw->dim);
1242         pw->dim = space;
1243
1244         return pw;
1245 error:
1246         isl_space_free(domain);
1247         isl_space_free(space);
1248         FN(PW,free)(pw);
1249         return NULL;
1250 }
1251
1252 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1253         __isl_take isl_space *domain)
1254 {
1255         isl_space *space;
1256
1257         space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1258                                                    FN(PW,get_space)(pw));
1259         return FN(PW,reset_space_and_domain)(pw, space, domain);
1260 }
1261
1262 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1263 {
1264         isl_space *domain;
1265
1266         domain = isl_space_domain(isl_space_copy(dim));
1267         return FN(PW,reset_space_and_domain)(pw, dim, domain);
1268 }
1269
1270 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1271         __isl_take isl_id *id)
1272 {
1273         isl_space *space;
1274
1275         pw = FN(PW,cow)(pw);
1276         if (!pw)
1277                 return isl_id_free(id);
1278
1279         space = FN(PW,get_space)(pw);
1280         space = isl_space_set_tuple_id(space, type, id);
1281
1282         return FN(PW,reset_space)(pw, space);
1283 }
1284
1285 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1286         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1287 {
1288         pw = FN(PW,cow)(pw);
1289         if (!pw)
1290                 return isl_id_free(id);
1291         pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1292         return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1293 }
1294 #endif
1295
1296 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1297 {
1298         if (!pw1 || !pw2)
1299                 return -1;
1300
1301         return isl_space_is_equal(pw1->dim, pw2->dim);
1302 }
1303
1304 #ifndef NO_MORPH
1305 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1306         __isl_take isl_morph *morph)
1307 {
1308         int i;
1309         isl_ctx *ctx;
1310
1311         if (!pw || !morph)
1312                 goto error;
1313
1314         ctx = isl_space_get_ctx(pw->dim);
1315         isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1316                 goto error);
1317
1318         pw = FN(PW,cow)(pw);
1319         if (!pw)
1320                 goto error;
1321         pw->dim = isl_space_extend_domain_with_range(
1322                         isl_space_copy(morph->ran->dim), pw->dim);
1323         if (!pw->dim)
1324                 goto error;
1325
1326         for (i = 0; i < pw->n; ++i) {
1327                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1328                 if (!pw->p[i].set)
1329                         goto error;
1330                 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1331                                                 isl_morph_copy(morph));
1332                 if (!pw->p[i].FIELD)
1333                         goto error;
1334         }
1335
1336         isl_morph_free(morph);
1337
1338         return pw;
1339 error:
1340         FN(PW,free)(pw);
1341         isl_morph_free(morph);
1342         return NULL;
1343 }
1344 #endif
1345
1346 int FN(PW,n_piece)(__isl_keep PW *pw)
1347 {
1348         return pw ? pw->n : 0;
1349 }
1350
1351 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1352         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1353         void *user)
1354 {
1355         int i;
1356
1357         if (!pw)
1358                 return -1;
1359
1360         for (i = 0; i < pw->n; ++i)
1361                 if (fn(isl_set_copy(pw->p[i].set),
1362                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1363                         return -1;
1364
1365         return 0;
1366 }
1367
1368 #ifndef NO_LIFT
1369 static int any_divs(__isl_keep isl_set *set)
1370 {
1371         int i;
1372
1373         if (!set)
1374                 return -1;
1375
1376         for (i = 0; i < set->n; ++i)
1377                 if (set->p[i]->n_div > 0)
1378                         return 1;
1379
1380         return 0;
1381 }
1382
1383 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1384         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1385                     void *user), void *user)
1386 {
1387         int i;
1388
1389         if (!set || !el)
1390                 goto error;
1391
1392         for (i = 0; i < set->n; ++i) {
1393                 isl_set *lift;
1394                 EL *copy;
1395
1396                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1397                 lift = isl_set_lift(lift);
1398
1399                 copy = FN(EL,copy)(el);
1400                 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1401
1402                 if (fn(lift, copy, user) < 0)
1403                         goto error;
1404         }
1405
1406         isl_set_free(set);
1407         FN(EL,free)(el);
1408
1409         return 0;
1410 error:
1411         isl_set_free(set);
1412         FN(EL,free)(el);
1413         return -1;
1414 }
1415
1416 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1417         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1418                     void *user), void *user)
1419 {
1420         int i;
1421
1422         if (!pw)
1423                 return -1;
1424
1425         for (i = 0; i < pw->n; ++i) {
1426                 isl_set *set;
1427                 EL *el;
1428
1429                 set = isl_set_copy(pw->p[i].set);
1430                 el = FN(EL,copy)(pw->p[i].FIELD);
1431                 if (!any_divs(set)) {
1432                         if (fn(set, el, user) < 0)
1433                                 return -1;
1434                         continue;
1435                 }
1436                 if (foreach_lifted_subset(set, el, fn, user) < 0)
1437                         return -1;
1438         }
1439
1440         return 0;
1441 }
1442 #endif
1443
1444 #ifndef NO_MOVE_DIMS
1445 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1446         enum isl_dim_type dst_type, unsigned dst_pos,
1447         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1448 {
1449         int i;
1450
1451         pw = FN(PW,cow)(pw);
1452         if (!pw)
1453                 return NULL;
1454
1455         pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1456         if (!pw->dim)
1457                 goto error;
1458
1459         for (i = 0; i < pw->n; ++i) {
1460                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1461                                         dst_type, dst_pos, src_type, src_pos, n);
1462                 if (!pw->p[i].FIELD)
1463                         goto error;
1464         }
1465
1466         if (dst_type == isl_dim_in)
1467                 dst_type = isl_dim_set;
1468         if (src_type == isl_dim_in)
1469                 src_type = isl_dim_set;
1470
1471         for (i = 0; i < pw->n; ++i) {
1472                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1473                                                 dst_type, dst_pos,
1474                                                 src_type, src_pos, n);
1475                 if (!pw->p[i].set)
1476                         goto error;
1477         }
1478
1479         return pw;
1480 error:
1481         FN(PW,free)(pw);
1482         return NULL;
1483 }
1484 #endif
1485
1486 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1487 {
1488         int i;
1489
1490         if (isl_int_is_one(v))
1491                 return pw;
1492         if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1493                 PW *zero;
1494                 isl_space *dim = FN(PW,get_space)(pw);
1495 #ifdef HAS_TYPE
1496                 zero = FN(PW,ZERO)(dim, pw->type);
1497 #else
1498                 zero = FN(PW,ZERO)(dim);
1499 #endif
1500                 FN(PW,free)(pw);
1501                 return zero;
1502         }
1503         pw = FN(PW,cow)(pw);
1504         if (!pw)
1505                 return NULL;
1506         if (pw->n == 0)
1507                 return pw;
1508
1509 #ifdef HAS_TYPE
1510         if (isl_int_is_neg(v))
1511                 pw->type = isl_fold_type_negate(pw->type);
1512 #endif
1513         for (i = 0; i < pw->n; ++i) {
1514                 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1515                 if (!pw->p[i].FIELD)
1516                         goto error;
1517         }
1518
1519         return pw;
1520 error:
1521         FN(PW,free)(pw);
1522         return NULL;
1523 }
1524
1525 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1526 {
1527         return FN(PW,mul_isl_int)(pw, v);
1528 }
1529
1530 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1531 {
1532         isl_set *set1 = *(isl_set * const *)p1;
1533         isl_set *set2 = *(isl_set * const *)p2;
1534
1535         return isl_set_plain_cmp(set1, set2);
1536 }
1537
1538 /* We normalize in place, but if anything goes wrong we need
1539  * to return NULL, so we need to make sure we don't change the
1540  * meaning of any possible other copies of map.
1541  */
1542 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1543 {
1544         int i, j;
1545         isl_set *set;
1546
1547         if (!pw)
1548                 return NULL;
1549         for (i = 0; i < pw->n; ++i) {
1550                 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1551                 if (!set)
1552                         return FN(PW,free)(pw);
1553                 isl_set_free(pw->p[i].set);
1554                 pw->p[i].set = set;
1555         }
1556         qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1557         for (i = pw->n - 1; i >= 1; --i) {
1558                 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1559                         continue;
1560                 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1561                         continue;
1562                 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1563                                     isl_set_copy(pw->p[i].set));
1564                 if (!set)
1565                         return FN(PW,free)(pw);
1566                 isl_set_free(pw->p[i].set);
1567                 FN(EL,free)(pw->p[i].FIELD);
1568                 isl_set_free(pw->p[i - 1].set);
1569                 pw->p[i - 1].set = set;
1570                 for (j = i + 1; j < pw->n; ++j)
1571                         pw->p[j - 1] = pw->p[j];
1572                 pw->n--;
1573         }
1574
1575         return pw;
1576 }
1577
1578 /* Is pw1 obviously equal to pw2?
1579  * That is, do they have obviously identical cells and obviously identical
1580  * elements on each cell?
1581  */
1582 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1583 {
1584         int i;
1585         int equal;
1586
1587         if (!pw1 || !pw2)
1588                 return -1;
1589
1590         if (pw1 == pw2)
1591                 return 1;
1592         if (!isl_space_is_equal(pw1->dim, pw2->dim))
1593                 return 0;
1594
1595         pw1 = FN(PW,copy)(pw1);
1596         pw2 = FN(PW,copy)(pw2);
1597         pw1 = FN(PW,normalize)(pw1);
1598         pw2 = FN(PW,normalize)(pw2);
1599         if (!pw1 || !pw2)
1600                 goto error;
1601
1602         equal = pw1->n == pw2->n;
1603         for (i = 0; equal && i < pw1->n; ++i) {
1604                 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1605                 if (equal < 0)
1606                         goto error;
1607                 if (!equal)
1608                         break;
1609                 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1610                 if (equal < 0)
1611                         goto error;
1612         }
1613
1614         FN(PW,free)(pw1);
1615         FN(PW,free)(pw2);
1616         return equal;
1617 error:
1618         FN(PW,free)(pw1);
1619         FN(PW,free)(pw2);
1620         return -1;
1621 }
1622
1623 #ifndef NO_PULLBACK
1624 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1625         __isl_take isl_multi_aff *ma,
1626         __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1627 {
1628         isl_ctx *ctx;
1629         isl_space *ma_space;
1630
1631         ma_space = isl_multi_aff_get_space(ma);
1632         if (!pw || !ma || !ma_space)
1633                 goto error;
1634         if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1635                 isl_space_free(ma_space);
1636                 return fn(pw, ma);
1637         }
1638         ctx = FN(PW,get_ctx)(pw);
1639         if (!isl_space_has_named_params(pw->dim) ||
1640             !isl_space_has_named_params(ma_space))
1641                 isl_die(ctx, isl_error_invalid,
1642                         "unaligned unnamed parameters", goto error);
1643         pw = FN(PW,align_params)(pw, ma_space);
1644         ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
1645         return fn(pw, ma);
1646 error:
1647         isl_space_free(ma_space);
1648         FN(PW,free)(pw);
1649         isl_multi_aff_free(ma);
1650         return NULL;
1651 }
1652
1653 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
1654         __isl_take isl_pw_multi_aff *pma,
1655         __isl_give PW *(*fn)(__isl_take PW *pw,
1656                 __isl_take isl_pw_multi_aff *ma))
1657 {
1658         isl_ctx *ctx;
1659         isl_space *pma_space;
1660
1661         pma_space = isl_pw_multi_aff_get_space(pma);
1662         if (!pw || !pma || !pma_space)
1663                 goto error;
1664         if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
1665                 isl_space_free(pma_space);
1666                 return fn(pw, pma);
1667         }
1668         ctx = FN(PW,get_ctx)(pw);
1669         if (!isl_space_has_named_params(pw->dim) ||
1670             !isl_space_has_named_params(pma_space))
1671                 isl_die(ctx, isl_error_invalid,
1672                         "unaligned unnamed parameters", goto error);
1673         pw = FN(PW,align_params)(pw, pma_space);
1674         pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
1675         return fn(pw, pma);
1676 error:
1677         isl_space_free(pma_space);
1678         FN(PW,free)(pw);
1679         isl_pw_multi_aff_free(pma);
1680         return NULL;
1681 }
1682
1683 /* Compute the pullback of "pw" by the function represented by "ma".
1684  * In other words, plug in "ma" in "pw".
1685  */
1686 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
1687         __isl_take isl_multi_aff *ma)
1688 {
1689         int i;
1690         isl_space *space = NULL;
1691
1692         ma = isl_multi_aff_align_divs(ma);
1693         pw = FN(PW,cow)(pw);
1694         if (!pw || !ma)
1695                 goto error;
1696
1697         space = isl_space_join(isl_multi_aff_get_space(ma),
1698                                 FN(PW,get_space)(pw));
1699
1700         for (i = 0; i < pw->n; ++i) {
1701                 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
1702                                                     isl_multi_aff_copy(ma));
1703                 if (!pw->p[i].set)
1704                         goto error;
1705                 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
1706                                                     isl_multi_aff_copy(ma));
1707                 if (!pw->p[i].FIELD)
1708                         goto error;
1709         }
1710
1711         pw = FN(PW,reset_space)(pw, space);
1712         isl_multi_aff_free(ma);
1713         return pw;
1714 error:
1715         isl_space_free(space);
1716         isl_multi_aff_free(ma);
1717         FN(PW,free)(pw);
1718         return NULL;
1719 }
1720
1721 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
1722         __isl_take isl_multi_aff *ma)
1723 {
1724         return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
1725                                         &FN(PW,pullback_multi_aff_aligned));
1726 }
1727
1728 /* Compute the pullback of "pw" by the function represented by "pma".
1729  * In other words, plug in "pma" in "pw".
1730  */
1731 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
1732         __isl_take isl_pw_multi_aff *pma)
1733 {
1734         int i;
1735         PW *res;
1736
1737         if (!pma)
1738                 goto error;
1739
1740         if (pma->n == 0) {
1741                 isl_pw_multi_aff_free(pma);
1742                 res = FN(PW,empty)(FN(PW,get_space)(pw));
1743                 FN(PW,free)(pw);
1744                 return res;
1745         }
1746
1747         res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1748                                         isl_multi_aff_copy(pma->p[0].maff));
1749         res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
1750
1751         for (i = 1; i < pma->n; ++i) {
1752                 PW *res_i;
1753
1754                 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1755                                         isl_multi_aff_copy(pma->p[i].maff));
1756                 res_i = FN(PW,intersect_domain)(res_i,
1757                                         isl_set_copy(pma->p[i].set));
1758                 res = FN(PW,add_disjoint)(res, res_i);
1759         }
1760
1761         isl_pw_multi_aff_free(pma);
1762         FN(PW,free)(pw);
1763         return res;
1764 error:
1765         isl_pw_multi_aff_free(pma);
1766         FN(PW,free)(pw);
1767         return NULL;
1768 }
1769
1770 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
1771         __isl_take isl_pw_multi_aff *pma)
1772 {
1773         return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
1774                                         &FN(PW,pullback_pw_multi_aff_aligned));
1775 }
1776 #endif