isl_ast_codegen.c: create_node: avoid invalid access on error
[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)(space, pw1->type, n);
542 #else
543         res = FN(PW,alloc_size)(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         FN(PW,free)(pw1);
572         FN(PW,free)(pw2);
573         return res;
574 error:
575         isl_space_free(space);
576         FN(PW,free)(pw1);
577         FN(PW,free)(pw2);
578         FN(PW,free)(res);
579         return NULL;
580 }
581
582 /* This function is currently only used from isl_aff.c
583  */
584 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
585         __isl_take PW *pw2,
586         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
587         __attribute__ ((unused));
588
589 /* Apply "fn" to pairs of elements from pw1 and pw2 on shared domains.
590  * The result of "fn" is assumed to live in the same space as "pw1" and "pw2".
591  */
592 static __isl_give PW *FN(PW,on_shared_domain)(__isl_take PW *pw1,
593         __isl_take PW *pw2,
594         __isl_give EL *(*fn)(__isl_take EL *el1, __isl_take EL *el2))
595 {
596         isl_space *space;
597
598         if (!pw1 || !pw2)
599                 goto error;
600
601         space = isl_space_copy(pw1->dim);
602         return FN(PW,on_shared_domain_in)(pw1, pw2, space, fn);
603 error:
604         FN(PW,free)(pw1);
605         FN(PW,free)(pw2);
606         return NULL;
607 }
608
609 #ifndef NO_NEG
610 __isl_give PW *FN(PW,neg)(__isl_take PW *pw)
611 {
612         int i;
613
614         if (!pw)
615                 return NULL;
616
617         if (FN(PW,IS_ZERO)(pw))
618                 return pw;
619
620         pw = FN(PW,cow)(pw);
621         if (!pw)
622                 return NULL;
623
624         for (i = 0; i < pw->n; ++i) {
625                 pw->p[i].FIELD = FN(EL,neg)(pw->p[i].FIELD);
626                 if (!pw->p[i].FIELD)
627                         return FN(PW,free)(pw);
628         }
629
630         return pw;
631 }
632
633 __isl_give PW *FN(PW,sub)(__isl_take PW *pw1, __isl_take PW *pw2)
634 {
635         return FN(PW,add)(pw1, FN(PW,neg)(pw2));
636 }
637 #endif
638
639 #ifndef NO_EVAL
640 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
641         __isl_take isl_point *pnt)
642 {
643         int i;
644         int found = 0;
645         isl_ctx *ctx;
646         isl_space *pnt_dim = NULL;
647         isl_qpolynomial *qp;
648
649         if (!pw || !pnt)
650                 goto error;
651         ctx = isl_point_get_ctx(pnt);
652         pnt_dim = isl_point_get_space(pnt);
653         isl_assert(ctx, isl_space_is_domain_internal(pnt_dim, pw->dim),
654                     goto error);
655
656         for (i = 0; i < pw->n; ++i) {
657                 found = isl_set_contains_point(pw->p[i].set, pnt);
658                 if (found < 0)
659                         goto error;
660                 if (found)
661                         break;
662         }
663         if (found)
664                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
665                                             isl_point_copy(pnt));
666         else
667                 qp = isl_qpolynomial_zero_on_domain(FN(PW,get_domain_space)(pw));
668         FN(PW,free)(pw);
669         isl_space_free(pnt_dim);
670         isl_point_free(pnt);
671         return qp;
672 error:
673         FN(PW,free)(pw);
674         isl_space_free(pnt_dim);
675         isl_point_free(pnt);
676         return NULL;
677 }
678 #endif
679
680 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
681 {
682         int i;
683         isl_set *dom;
684
685         if (!pw)
686                 return NULL;
687
688         dom = isl_set_empty(FN(PW,get_domain_space)(pw));
689         for (i = 0; i < pw->n; ++i)
690                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
691
692         FN(PW,free)(pw);
693
694         return dom;
695 }
696
697 /* Restrict the domain of "pw" by combining each cell
698  * with "set" through a call to "fn", where "fn" may be
699  * isl_set_intersect or isl_set_intersect_params.
700  */
701 static __isl_give PW *FN(PW,intersect_aligned)(__isl_take PW *pw,
702         __isl_take isl_set *set,
703         __isl_give isl_set *(*fn)(__isl_take isl_set *set1,
704                                     __isl_take isl_set *set2))
705 {
706         int i;
707
708         if (!pw || !set)
709                 goto error;
710
711         if (pw->n == 0) {
712                 isl_set_free(set);
713                 return pw;
714         }
715
716         pw = FN(PW,cow)(pw);
717         if (!pw)
718                 goto error;
719
720         for (i = pw->n - 1; i >= 0; --i) {
721                 isl_basic_set *aff;
722                 pw->p[i].set = fn(pw->p[i].set, isl_set_copy(set));
723                 if (!pw->p[i].set)
724                         goto error;
725                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
726                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
727                                                                 aff);
728                 if (!pw->p[i].FIELD)
729                         goto error;
730                 if (isl_set_plain_is_empty(pw->p[i].set)) {
731                         isl_set_free(pw->p[i].set);
732                         FN(EL,free)(pw->p[i].FIELD);
733                         if (i != pw->n - 1)
734                                 pw->p[i] = pw->p[pw->n - 1];
735                         pw->n--;
736                 }
737         }
738         
739         isl_set_free(set);
740         return pw;
741 error:
742         isl_set_free(set);
743         FN(PW,free)(pw);
744         return NULL;
745 }
746
747 static __isl_give PW *FN(PW,intersect_domain_aligned)(__isl_take PW *pw,
748         __isl_take isl_set *set)
749 {
750         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect);
751 }
752
753 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw,
754         __isl_take isl_set *context)
755 {
756         return FN(PW,align_params_pw_set_and)(pw, context,
757                                         &FN(PW,intersect_domain_aligned));
758 }
759
760 static __isl_give PW *FN(PW,intersect_params_aligned)(__isl_take PW *pw,
761         __isl_take isl_set *set)
762 {
763         return FN(PW,intersect_aligned)(pw, set, &isl_set_intersect_params);
764 }
765
766 /* Intersect the domain of "pw" with the parameter domain "context".
767  */
768 __isl_give PW *FN(PW,intersect_params)(__isl_take PW *pw,
769         __isl_take isl_set *context)
770 {
771         return FN(PW,align_params_pw_set_and)(pw, context,
772                                         &FN(PW,intersect_params_aligned));
773 }
774
775 static __isl_give PW *FN(PW,gist_aligned)(__isl_take PW *pw,
776         __isl_take isl_set *context,
777         __isl_give EL *(*fn_el)(__isl_take EL *el,
778                                     __isl_take isl_set *set),
779         __isl_give isl_set *(*fn_dom)(__isl_take isl_set *set,
780                                     __isl_take isl_basic_set *bset))
781 {
782         int i;
783         isl_basic_set *hull = NULL;
784
785         if (!pw || !context)
786                 goto error;
787
788         if (pw->n == 0) {
789                 isl_set_free(context);
790                 return pw;
791         }
792
793         if (!isl_space_match(pw->dim, isl_dim_param,
794                                 context->dim, isl_dim_param)) {
795                 pw = FN(PW,align_params)(pw, isl_set_get_space(context));
796                 context = isl_set_align_params(context, FN(PW,get_space)(pw));
797         }
798
799         context = isl_set_compute_divs(context);
800         hull = isl_set_simple_hull(isl_set_copy(context));
801
802         pw = FN(PW,cow)(pw);
803         if (!pw)
804                 goto error;
805
806         for (i = pw->n - 1; i >= 0; --i) {
807                 isl_set *set_i;
808                 int empty;
809
810                 set_i = isl_set_intersect(isl_set_copy(pw->p[i].set),
811                                                  isl_set_copy(context));
812                 empty = isl_set_plain_is_empty(set_i);
813                 pw->p[i].FIELD = fn_el(pw->p[i].FIELD, set_i);
814                 pw->p[i].set = fn_dom(pw->p[i].set, isl_basic_set_copy(hull));
815                 if (!pw->p[i].FIELD || !pw->p[i].set)
816                         goto error;
817                 if (empty) {
818                         isl_set_free(pw->p[i].set);
819                         FN(EL,free)(pw->p[i].FIELD);
820                         if (i != pw->n - 1)
821                                 pw->p[i] = pw->p[pw->n - 1];
822                         pw->n--;
823                 }
824         }
825
826         isl_basic_set_free(hull);
827         isl_set_free(context);
828
829         return pw;
830 error:
831         FN(PW,free)(pw);
832         isl_basic_set_free(hull);
833         isl_set_free(context);
834         return NULL;
835 }
836
837 static __isl_give PW *FN(PW,gist_domain_aligned)(__isl_take PW *pw,
838         __isl_take isl_set *set)
839 {
840         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist),
841                                         &isl_set_gist_basic_set);
842 }
843
844 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
845 {
846         return FN(PW,align_params_pw_set_and)(pw, context,
847                                                 &FN(PW,gist_domain_aligned));
848 }
849
850 static __isl_give PW *FN(PW,gist_params_aligned)(__isl_take PW *pw,
851         __isl_take isl_set *set)
852 {
853         return FN(PW,gist_aligned)(pw, set, &FN(EL,gist_params),
854                                         &isl_set_gist_params_basic_set);
855 }
856
857 __isl_give PW *FN(PW,gist_params)(__isl_take PW *pw,
858         __isl_take isl_set *context)
859 {
860         return FN(PW,align_params_pw_set_and)(pw, context,
861                                                 &FN(PW,gist_params_aligned));
862 }
863
864 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
865 {
866         int i, j;
867
868         if (!pw)
869                 return NULL;
870         if (pw->n == 0)
871                 return pw;
872
873         for (i = pw->n - 1; i >= 0; --i) {
874                 for (j = i - 1; j >= 0; --j) {
875                         if (!FN(EL,plain_is_equal)(pw->p[i].FIELD,
876                                                         pw->p[j].FIELD))
877                                 continue;
878                         pw->p[j].set = isl_set_union(pw->p[j].set,
879                                                         pw->p[i].set);
880                         FN(EL,free)(pw->p[i].FIELD);
881                         if (i != pw->n - 1)
882                                 pw->p[i] = pw->p[pw->n - 1];
883                         pw->n--;
884                         break;
885                 }
886                 if (j >= 0)
887                         continue;
888                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
889                 if (!pw->p[i].set)
890                         goto error;
891         }
892
893         return pw;
894 error:
895         FN(PW,free)(pw);
896         return NULL;
897 }
898
899 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
900 {
901         return pw ? isl_space_get_ctx(pw->dim) : NULL;
902 }
903
904 #ifndef NO_INVOLVES_DIMS
905 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
906         unsigned first, unsigned n)
907 {
908         int i;
909         enum isl_dim_type set_type;
910
911         if (!pw)
912                 return -1;
913         if (pw->n == 0 || n == 0)
914                 return 0;
915
916         set_type = type == isl_dim_in ? isl_dim_set : type;
917
918         for (i = 0; i < pw->n; ++i) {
919                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
920                                                         type, first, n);
921                 if (involves < 0 || involves)
922                         return involves;
923                 involves = isl_set_involves_dims(pw->p[i].set,
924                                                         set_type, first, n);
925                 if (involves < 0 || involves)
926                         return involves;
927         }
928         return 0;
929 }
930 #endif
931
932 __isl_give PW *FN(PW,set_dim_name)(__isl_take PW *pw,
933         enum isl_dim_type type, unsigned pos, const char *s)
934 {
935         int i;
936         enum isl_dim_type set_type;
937
938         pw = FN(PW,cow)(pw);
939         if (!pw)
940                 return NULL;
941
942         set_type = type == isl_dim_in ? isl_dim_set : type;
943
944         pw->dim = isl_space_set_dim_name(pw->dim, type, pos, s);
945         if (!pw->dim)
946                 goto error;
947
948         for (i = 0; i < pw->n; ++i) {
949                 pw->p[i].set = isl_set_set_dim_name(pw->p[i].set,
950                                                         set_type, pos, s);
951                 if (!pw->p[i].set)
952                         goto error;
953                 pw->p[i].FIELD = FN(EL,set_dim_name)(pw->p[i].FIELD, type, pos, s);
954                 if (!pw->p[i].FIELD)
955                         goto error;
956         }
957
958         return pw;
959 error:
960         FN(PW,free)(pw);
961         return NULL;
962 }
963
964 #ifndef NO_DROP_DIMS
965 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
966         enum isl_dim_type type, unsigned first, unsigned n)
967 {
968         int i;
969         enum isl_dim_type set_type;
970
971         if (!pw)
972                 return NULL;
973         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
974                 return pw;
975
976         set_type = type == isl_dim_in ? isl_dim_set : type;
977
978         pw = FN(PW,cow)(pw);
979         if (!pw)
980                 return NULL;
981         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
982         if (!pw->dim)
983                 goto error;
984         for (i = 0; i < pw->n; ++i) {
985                 pw->p[i].set = isl_set_drop(pw->p[i].set, set_type, first, n);
986                 if (!pw->p[i].set)
987                         goto error;
988                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
989                 if (!pw->p[i].FIELD)
990                         goto error;
991         }
992
993         return pw;
994 error:
995         FN(PW,free)(pw);
996         return NULL;
997 }
998
999 /* This function is very similar to drop_dims.
1000  * The only difference is that the cells may still involve
1001  * the specified dimensions.  They are removed using
1002  * isl_set_project_out instead of isl_set_drop.
1003  */
1004 __isl_give PW *FN(PW,project_out)(__isl_take PW *pw,
1005         enum isl_dim_type type, unsigned first, unsigned n)
1006 {
1007         int i;
1008         enum isl_dim_type set_type;
1009
1010         if (!pw)
1011                 return NULL;
1012         if (n == 0 && !isl_space_get_tuple_name(pw->dim, type))
1013                 return pw;
1014
1015         set_type = type == isl_dim_in ? isl_dim_set : type;
1016
1017         pw = FN(PW,cow)(pw);
1018         if (!pw)
1019                 return NULL;
1020         pw->dim = isl_space_drop_dims(pw->dim, type, first, n);
1021         if (!pw->dim)
1022                 goto error;
1023         for (i = 0; i < pw->n; ++i) {
1024                 pw->p[i].set = isl_set_project_out(pw->p[i].set,
1025                                                         set_type, first, n);
1026                 if (!pw->p[i].set)
1027                         goto error;
1028                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
1029                 if (!pw->p[i].FIELD)
1030                         goto error;
1031         }
1032
1033         return pw;
1034 error:
1035         FN(PW,free)(pw);
1036         return NULL;
1037 }
1038
1039 /* Project the domain of pw onto its parameter space.
1040  */
1041 __isl_give PW *FN(PW,project_domain_on_params)(__isl_take PW *pw)
1042 {
1043         isl_space *space;
1044         unsigned n;
1045
1046         n = FN(PW,dim)(pw, isl_dim_in);
1047         pw = FN(PW,project_out)(pw, isl_dim_in, 0, n);
1048         space = FN(PW,get_domain_space)(pw);
1049         space = isl_space_params(space);
1050         pw = FN(PW,reset_domain_space)(pw, space);
1051         return pw;
1052 }
1053 #endif
1054
1055 #ifndef NO_INSERT_DIMS
1056 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
1057         unsigned first, unsigned n)
1058 {
1059         int i;
1060         enum isl_dim_type set_type;
1061
1062         if (!pw)
1063                 return NULL;
1064         if (n == 0 && !isl_space_is_named_or_nested(pw->dim, type))
1065                 return pw;
1066
1067         set_type = type == isl_dim_in ? isl_dim_set : type;
1068
1069         pw = FN(PW,cow)(pw);
1070         if (!pw)
1071                 return NULL;
1072
1073         pw->dim = isl_space_insert_dims(pw->dim, type, first, n);
1074         if (!pw->dim)
1075                 goto error;
1076
1077         for (i = 0; i < pw->n; ++i) {
1078                 pw->p[i].set = isl_set_insert_dims(pw->p[i].set,
1079                                                             set_type, first, n);
1080                 if (!pw->p[i].set)
1081                         goto error;
1082                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
1083                                                                 type, first, n);
1084                 if (!pw->p[i].FIELD)
1085                         goto error;
1086         }
1087
1088         return pw;
1089 error:
1090         FN(PW,free)(pw);
1091         return NULL;
1092 }
1093 #endif
1094
1095 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
1096         enum isl_dim_type type, unsigned pos, isl_int v)
1097 {
1098         int i;
1099
1100         if (!pw)
1101                 return NULL;
1102
1103         if (type == isl_dim_in)
1104                 type = isl_dim_set;
1105
1106         pw = FN(PW,cow)(pw);
1107         if (!pw)
1108                 return NULL;
1109         for (i = 0; i < pw->n; ++i) {
1110                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
1111                 if (!pw->p[i].set)
1112                         goto error;
1113         }
1114
1115         return pw;
1116 error:
1117         FN(PW,free)(pw);
1118         return NULL;
1119 }
1120
1121 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
1122 {
1123         return pw ? isl_space_dim(pw->dim, type) : 0;
1124 }
1125
1126 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
1127         enum isl_dim_type type, unsigned first, unsigned n)
1128 {
1129         int i;
1130
1131         if (!pw)
1132                 return NULL;
1133         if (n == 0)
1134                 return pw;
1135
1136         if (type == isl_dim_in)
1137                 type = isl_dim_set;
1138
1139         pw = FN(PW,cow)(pw);
1140         if (!pw)
1141                 return NULL;
1142         if (!pw->dim)
1143                 goto error;
1144         for (i = 0; i < pw->n; ++i) {
1145                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
1146                 if (!pw->p[i].set)
1147                         goto error;
1148         }
1149
1150         return pw;
1151 error:
1152         FN(PW,free)(pw);
1153         return NULL;
1154 }
1155
1156 #ifndef NO_OPT
1157 /* Compute the maximal value attained by the piecewise quasipolynomial
1158  * on its domain or zero if the domain is empty.
1159  * In the worst case, the domain is scanned completely,
1160  * so the domain is assumed to be bounded.
1161  */
1162 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
1163 {
1164         int i;
1165         isl_qpolynomial *opt;
1166
1167         if (!pw)
1168                 return NULL;
1169
1170         if (pw->n == 0) {
1171                 isl_space *dim = isl_space_copy(pw->dim);
1172                 FN(PW,free)(pw);
1173                 return isl_qpolynomial_zero_on_domain(isl_space_domain(dim));
1174         }
1175
1176         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
1177                                         isl_set_copy(pw->p[0].set), max);
1178         for (i = 1; i < pw->n; ++i) {
1179                 isl_qpolynomial *opt_i;
1180                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
1181                                                 isl_set_copy(pw->p[i].set), max);
1182                 if (max)
1183                         opt = isl_qpolynomial_max_cst(opt, opt_i);
1184                 else
1185                         opt = isl_qpolynomial_min_cst(opt, opt_i);
1186         }
1187
1188         FN(PW,free)(pw);
1189         return opt;
1190 }
1191
1192 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
1193 {
1194         return FN(PW,opt)(pw, 1);
1195 }
1196
1197 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
1198 {
1199         return FN(PW,opt)(pw, 0);
1200 }
1201 #endif
1202
1203 __isl_give isl_space *FN(PW,get_space)(__isl_keep PW *pw)
1204 {
1205         return pw ? isl_space_copy(pw->dim) : NULL;
1206 }
1207
1208 __isl_give isl_space *FN(PW,get_domain_space)(__isl_keep PW *pw)
1209 {
1210         return pw ? isl_space_domain(isl_space_copy(pw->dim)) : NULL;
1211 }
1212
1213 #ifndef NO_RESET_DIM
1214 /* Reset the space of "pw".  Since we don't know if the elements
1215  * represent the spaces themselves or their domains, we pass along
1216  * both when we call their reset_space_and_domain.
1217  */
1218 static __isl_give PW *FN(PW,reset_space_and_domain)(__isl_take PW *pw,
1219         __isl_take isl_space *space, __isl_take isl_space *domain)
1220 {
1221         int i;
1222
1223         pw = FN(PW,cow)(pw);
1224         if (!pw || !space || !domain)
1225                 goto error;
1226
1227         for (i = 0; i < pw->n; ++i) {
1228                 pw->p[i].set = isl_set_reset_space(pw->p[i].set,
1229                                                  isl_space_copy(domain));
1230                 if (!pw->p[i].set)
1231                         goto error;
1232                 pw->p[i].FIELD = FN(EL,reset_space_and_domain)(pw->p[i].FIELD,
1233                               isl_space_copy(space), isl_space_copy(domain));
1234                 if (!pw->p[i].FIELD)
1235                         goto error;
1236         }
1237
1238         isl_space_free(domain);
1239
1240         isl_space_free(pw->dim);
1241         pw->dim = space;
1242
1243         return pw;
1244 error:
1245         isl_space_free(domain);
1246         isl_space_free(space);
1247         FN(PW,free)(pw);
1248         return NULL;
1249 }
1250
1251 __isl_give PW *FN(PW,reset_domain_space)(__isl_take PW *pw,
1252         __isl_take isl_space *domain)
1253 {
1254         isl_space *space;
1255
1256         space = isl_space_extend_domain_with_range(isl_space_copy(domain),
1257                                                    FN(PW,get_space)(pw));
1258         return FN(PW,reset_space_and_domain)(pw, space, domain);
1259 }
1260
1261 __isl_give PW *FN(PW,reset_space)(__isl_take PW *pw, __isl_take isl_space *dim)
1262 {
1263         isl_space *domain;
1264
1265         domain = isl_space_domain(isl_space_copy(dim));
1266         return FN(PW,reset_space_and_domain)(pw, dim, domain);
1267 }
1268
1269 __isl_give PW *FN(PW,set_tuple_id)(__isl_keep PW *pw, enum isl_dim_type type,
1270         __isl_take isl_id *id)
1271 {
1272         isl_space *space;
1273
1274         pw = FN(PW,cow)(pw);
1275         if (!pw)
1276                 return isl_id_free(id);
1277
1278         space = FN(PW,get_space)(pw);
1279         space = isl_space_set_tuple_id(space, type, id);
1280
1281         return FN(PW,reset_space)(pw, space);
1282 }
1283
1284 __isl_give PW *FN(PW,set_dim_id)(__isl_take PW *pw,
1285         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1286 {
1287         pw = FN(PW,cow)(pw);
1288         if (!pw)
1289                 return isl_id_free(id);
1290         pw->dim = isl_space_set_dim_id(pw->dim, type, pos, id);
1291         return FN(PW,reset_space)(pw, isl_space_copy(pw->dim));
1292 }
1293 #endif
1294
1295 int FN(PW,has_equal_space)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1296 {
1297         if (!pw1 || !pw2)
1298                 return -1;
1299
1300         return isl_space_is_equal(pw1->dim, pw2->dim);
1301 }
1302
1303 #ifndef NO_MORPH
1304 __isl_give PW *FN(PW,morph_domain)(__isl_take PW *pw,
1305         __isl_take isl_morph *morph)
1306 {
1307         int i;
1308         isl_ctx *ctx;
1309
1310         if (!pw || !morph)
1311                 goto error;
1312
1313         ctx = isl_space_get_ctx(pw->dim);
1314         isl_assert(ctx, isl_space_is_domain_internal(morph->dom->dim, pw->dim),
1315                 goto error);
1316
1317         pw = FN(PW,cow)(pw);
1318         if (!pw)
1319                 goto error;
1320         pw->dim = isl_space_extend_domain_with_range(
1321                         isl_space_copy(morph->ran->dim), pw->dim);
1322         if (!pw->dim)
1323                 goto error;
1324
1325         for (i = 0; i < pw->n; ++i) {
1326                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
1327                 if (!pw->p[i].set)
1328                         goto error;
1329                 pw->p[i].FIELD = FN(EL,morph_domain)(pw->p[i].FIELD,
1330                                                 isl_morph_copy(morph));
1331                 if (!pw->p[i].FIELD)
1332                         goto error;
1333         }
1334
1335         isl_morph_free(morph);
1336
1337         return pw;
1338 error:
1339         FN(PW,free)(pw);
1340         isl_morph_free(morph);
1341         return NULL;
1342 }
1343 #endif
1344
1345 int FN(PW,n_piece)(__isl_keep PW *pw)
1346 {
1347         return pw ? pw->n : 0;
1348 }
1349
1350 int FN(PW,foreach_piece)(__isl_keep PW *pw,
1351         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
1352         void *user)
1353 {
1354         int i;
1355
1356         if (!pw)
1357                 return -1;
1358
1359         for (i = 0; i < pw->n; ++i)
1360                 if (fn(isl_set_copy(pw->p[i].set),
1361                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
1362                         return -1;
1363
1364         return 0;
1365 }
1366
1367 #ifndef NO_LIFT
1368 static int any_divs(__isl_keep isl_set *set)
1369 {
1370         int i;
1371
1372         if (!set)
1373                 return -1;
1374
1375         for (i = 0; i < set->n; ++i)
1376                 if (set->p[i]->n_div > 0)
1377                         return 1;
1378
1379         return 0;
1380 }
1381
1382 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
1383         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1384                     void *user), void *user)
1385 {
1386         int i;
1387
1388         if (!set || !el)
1389                 goto error;
1390
1391         for (i = 0; i < set->n; ++i) {
1392                 isl_set *lift;
1393                 EL *copy;
1394
1395                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
1396                 lift = isl_set_lift(lift);
1397
1398                 copy = FN(EL,copy)(el);
1399                 copy = FN(EL,lift)(copy, isl_set_get_space(lift));
1400
1401                 if (fn(lift, copy, user) < 0)
1402                         goto error;
1403         }
1404
1405         isl_set_free(set);
1406         FN(EL,free)(el);
1407
1408         return 0;
1409 error:
1410         isl_set_free(set);
1411         FN(EL,free)(el);
1412         return -1;
1413 }
1414
1415 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
1416         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
1417                     void *user), void *user)
1418 {
1419         int i;
1420
1421         if (!pw)
1422                 return -1;
1423
1424         for (i = 0; i < pw->n; ++i) {
1425                 isl_set *set;
1426                 EL *el;
1427
1428                 set = isl_set_copy(pw->p[i].set);
1429                 el = FN(EL,copy)(pw->p[i].FIELD);
1430                 if (!any_divs(set)) {
1431                         if (fn(set, el, user) < 0)
1432                                 return -1;
1433                         continue;
1434                 }
1435                 if (foreach_lifted_subset(set, el, fn, user) < 0)
1436                         return -1;
1437         }
1438
1439         return 0;
1440 }
1441 #endif
1442
1443 #ifndef NO_MOVE_DIMS
1444 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
1445         enum isl_dim_type dst_type, unsigned dst_pos,
1446         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1447 {
1448         int i;
1449
1450         pw = FN(PW,cow)(pw);
1451         if (!pw)
1452                 return NULL;
1453
1454         pw->dim = isl_space_move_dims(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
1455         if (!pw->dim)
1456                 goto error;
1457
1458         for (i = 0; i < pw->n; ++i) {
1459                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
1460                                         dst_type, dst_pos, src_type, src_pos, n);
1461                 if (!pw->p[i].FIELD)
1462                         goto error;
1463         }
1464
1465         if (dst_type == isl_dim_in)
1466                 dst_type = isl_dim_set;
1467         if (src_type == isl_dim_in)
1468                 src_type = isl_dim_set;
1469
1470         for (i = 0; i < pw->n; ++i) {
1471                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
1472                                                 dst_type, dst_pos,
1473                                                 src_type, src_pos, n);
1474                 if (!pw->p[i].set)
1475                         goto error;
1476         }
1477
1478         return pw;
1479 error:
1480         FN(PW,free)(pw);
1481         return NULL;
1482 }
1483 #endif
1484
1485 __isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
1486 {
1487         int i;
1488
1489         if (isl_int_is_one(v))
1490                 return pw;
1491         if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
1492                 PW *zero;
1493                 isl_space *dim = FN(PW,get_space)(pw);
1494 #ifdef HAS_TYPE
1495                 zero = FN(PW,ZERO)(dim, pw->type);
1496 #else
1497                 zero = FN(PW,ZERO)(dim);
1498 #endif
1499                 FN(PW,free)(pw);
1500                 return zero;
1501         }
1502         pw = FN(PW,cow)(pw);
1503         if (!pw)
1504                 return NULL;
1505         if (pw->n == 0)
1506                 return pw;
1507
1508 #ifdef HAS_TYPE
1509         if (isl_int_is_neg(v))
1510                 pw->type = isl_fold_type_negate(pw->type);
1511 #endif
1512         for (i = 0; i < pw->n; ++i) {
1513                 pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
1514                 if (!pw->p[i].FIELD)
1515                         goto error;
1516         }
1517
1518         return pw;
1519 error:
1520         FN(PW,free)(pw);
1521         return NULL;
1522 }
1523
1524 __isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
1525 {
1526         return FN(PW,mul_isl_int)(pw, v);
1527 }
1528
1529 static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
1530 {
1531         isl_set *set1 = *(isl_set * const *)p1;
1532         isl_set *set2 = *(isl_set * const *)p2;
1533
1534         return isl_set_plain_cmp(set1, set2);
1535 }
1536
1537 /* We normalize in place, but if anything goes wrong we need
1538  * to return NULL, so we need to make sure we don't change the
1539  * meaning of any possible other copies of map.
1540  */
1541 __isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
1542 {
1543         int i, j;
1544         isl_set *set;
1545
1546         if (!pw)
1547                 return NULL;
1548         for (i = 0; i < pw->n; ++i) {
1549                 set = isl_set_normalize(isl_set_copy(pw->p[i].set));
1550                 if (!set)
1551                         return FN(PW,free)(pw);
1552                 isl_set_free(pw->p[i].set);
1553                 pw->p[i].set = set;
1554         }
1555         qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
1556         for (i = pw->n - 1; i >= 1; --i) {
1557                 if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
1558                         continue;
1559                 if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
1560                         continue;
1561                 set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
1562                                     isl_set_copy(pw->p[i].set));
1563                 if (!set)
1564                         return FN(PW,free)(pw);
1565                 isl_set_free(pw->p[i].set);
1566                 FN(EL,free)(pw->p[i].FIELD);
1567                 isl_set_free(pw->p[i - 1].set);
1568                 pw->p[i - 1].set = set;
1569                 for (j = i + 1; j < pw->n; ++j)
1570                         pw->p[j - 1] = pw->p[j];
1571                 pw->n--;
1572         }
1573
1574         return pw;
1575 }
1576
1577 /* Is pw1 obviously equal to pw2?
1578  * That is, do they have obviously identical cells and obviously identical
1579  * elements on each cell?
1580  */
1581 int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
1582 {
1583         int i;
1584         int equal;
1585
1586         if (!pw1 || !pw2)
1587                 return -1;
1588
1589         if (pw1 == pw2)
1590                 return 1;
1591         if (!isl_space_is_equal(pw1->dim, pw2->dim))
1592                 return 0;
1593
1594         pw1 = FN(PW,copy)(pw1);
1595         pw2 = FN(PW,copy)(pw2);
1596         pw1 = FN(PW,normalize)(pw1);
1597         pw2 = FN(PW,normalize)(pw2);
1598         if (!pw1 || !pw2)
1599                 goto error;
1600
1601         equal = pw1->n == pw2->n;
1602         for (i = 0; equal && i < pw1->n; ++i) {
1603                 equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
1604                 if (equal < 0)
1605                         goto error;
1606                 if (!equal)
1607                         break;
1608                 equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
1609                 if (equal < 0)
1610                         goto error;
1611         }
1612
1613         FN(PW,free)(pw1);
1614         FN(PW,free)(pw2);
1615         return equal;
1616 error:
1617         FN(PW,free)(pw1);
1618         FN(PW,free)(pw2);
1619         return -1;
1620 }
1621
1622 #ifndef NO_PULLBACK
1623 static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
1624         __isl_take isl_multi_aff *ma,
1625         __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
1626 {
1627         isl_ctx *ctx;
1628         isl_space *ma_space;
1629
1630         ma_space = isl_multi_aff_get_space(ma);
1631         if (!pw || !ma || !ma_space)
1632                 goto error;
1633         if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
1634                 isl_space_free(ma_space);
1635                 return fn(pw, ma);
1636         }
1637         ctx = FN(PW,get_ctx)(pw);
1638         if (!isl_space_has_named_params(pw->dim) ||
1639             !isl_space_has_named_params(ma_space))
1640                 isl_die(ctx, isl_error_invalid,
1641                         "unaligned unnamed parameters", goto error);
1642         pw = FN(PW,align_params)(pw, ma_space);
1643         ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
1644         return fn(pw, ma);
1645 error:
1646         isl_space_free(ma_space);
1647         FN(PW,free)(pw);
1648         isl_multi_aff_free(ma);
1649         return NULL;
1650 }
1651
1652 static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
1653         __isl_take isl_pw_multi_aff *pma,
1654         __isl_give PW *(*fn)(__isl_take PW *pw,
1655                 __isl_take isl_pw_multi_aff *ma))
1656 {
1657         isl_ctx *ctx;
1658         isl_space *pma_space;
1659
1660         pma_space = isl_pw_multi_aff_get_space(pma);
1661         if (!pw || !pma || !pma_space)
1662                 goto error;
1663         if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
1664                 isl_space_free(pma_space);
1665                 return fn(pw, pma);
1666         }
1667         ctx = FN(PW,get_ctx)(pw);
1668         if (!isl_space_has_named_params(pw->dim) ||
1669             !isl_space_has_named_params(pma_space))
1670                 isl_die(ctx, isl_error_invalid,
1671                         "unaligned unnamed parameters", goto error);
1672         pw = FN(PW,align_params)(pw, pma_space);
1673         pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
1674         return fn(pw, pma);
1675 error:
1676         isl_space_free(pma_space);
1677         FN(PW,free)(pw);
1678         isl_pw_multi_aff_free(pma);
1679         return NULL;
1680 }
1681
1682 /* Compute the pullback of "pw" by the function represented by "ma".
1683  * In other words, plug in "ma" in "pw".
1684  */
1685 static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
1686         __isl_take isl_multi_aff *ma)
1687 {
1688         int i;
1689         isl_space *space = NULL;
1690
1691         ma = isl_multi_aff_align_divs(ma);
1692         pw = FN(PW,cow)(pw);
1693         if (!pw || !ma)
1694                 goto error;
1695
1696         space = isl_space_join(isl_multi_aff_get_space(ma),
1697                                 FN(PW,get_space)(pw));
1698
1699         for (i = 0; i < pw->n; ++i) {
1700                 pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
1701                                                     isl_multi_aff_copy(ma));
1702                 if (!pw->p[i].set)
1703                         goto error;
1704                 pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
1705                                                     isl_multi_aff_copy(ma));
1706                 if (!pw->p[i].FIELD)
1707                         goto error;
1708         }
1709
1710         pw = FN(PW,reset_space)(pw, space);
1711         isl_multi_aff_free(ma);
1712         return pw;
1713 error:
1714         isl_space_free(space);
1715         isl_multi_aff_free(ma);
1716         FN(PW,free)(pw);
1717         return NULL;
1718 }
1719
1720 __isl_give PW *FN(PW,pullback_multi_aff)(__isl_take PW *pw,
1721         __isl_take isl_multi_aff *ma)
1722 {
1723         return FN(PW,align_params_pw_multi_aff_and)(pw, ma,
1724                                         &FN(PW,pullback_multi_aff_aligned));
1725 }
1726
1727 /* Compute the pullback of "pw" by the function represented by "pma".
1728  * In other words, plug in "pma" in "pw".
1729  */
1730 static __isl_give PW *FN(PW,pullback_pw_multi_aff_aligned)(__isl_take PW *pw,
1731         __isl_take isl_pw_multi_aff *pma)
1732 {
1733         int i;
1734         PW *res;
1735
1736         if (!pma)
1737                 goto error;
1738
1739         if (pma->n == 0) {
1740                 isl_pw_multi_aff_free(pma);
1741                 res = FN(PW,empty)(FN(PW,get_space)(pw));
1742                 FN(PW,free)(pw);
1743                 return res;
1744         }
1745
1746         res = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1747                                         isl_multi_aff_copy(pma->p[0].maff));
1748         res = FN(PW,intersect_domain)(res, isl_set_copy(pma->p[0].set));
1749
1750         for (i = 1; i < pma->n; ++i) {
1751                 PW *res_i;
1752
1753                 res_i = FN(PW,pullback_multi_aff)(FN(PW,copy)(pw),
1754                                         isl_multi_aff_copy(pma->p[i].maff));
1755                 res_i = FN(PW,intersect_domain)(res_i,
1756                                         isl_set_copy(pma->p[i].set));
1757                 res = FN(PW,add_disjoint)(res, res_i);
1758         }
1759
1760         isl_pw_multi_aff_free(pma);
1761         FN(PW,free)(pw);
1762         return res;
1763 error:
1764         isl_pw_multi_aff_free(pma);
1765         FN(PW,free)(pw);
1766         return NULL;
1767 }
1768
1769 __isl_give PW *FN(PW,pullback_pw_multi_aff)(__isl_take PW *pw,
1770         __isl_take isl_pw_multi_aff *pma)
1771 {
1772         return FN(PW,align_params_pw_pw_multi_aff_and)(pw, pma,
1773                                         &FN(PW,pullback_pw_multi_aff_aligned));
1774 }
1775 #endif