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