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