add isl_basic_map_from_range
[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 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim,
8         enum isl_fold type, int n)
9 #else
10 static __isl_give PW *FN(PW,alloc_)(__isl_take isl_dim *dim, int n)
11 #endif
12 {
13         struct PW *pw;
14
15         if (!dim)
16                 return NULL;
17         isl_assert(dim->ctx, n >= 0, goto error);
18         pw = isl_alloc(dim->ctx, struct PW,
19                         sizeof(struct PW) + (n - 1) * sizeof(S(PW,piece)));
20         if (!pw)
21                 goto error;
22
23         pw->ref = 1;
24 #ifdef HAS_TYPE
25         pw->type = type;
26 #endif
27         pw->size = n;
28         pw->n = 0;
29         pw->dim = dim;
30         return pw;
31 error:
32         isl_dim_free(dim);
33         return NULL;
34 }
35
36 #ifdef HAS_TYPE
37 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim, enum isl_fold type)
38 {
39         return FN(PW,alloc_)(dim, type, 0);
40 }
41 #else
42 __isl_give PW *FN(PW,zero)(__isl_take isl_dim *dim)
43 {
44         return FN(PW,alloc_)(dim, 0);
45 }
46 #endif
47
48 __isl_give PW *FN(PW,add_piece)(__isl_take PW *pw,
49         __isl_take isl_set *set, __isl_take EL *el)
50 {
51         if (!pw || !set || !el)
52                 goto error;
53
54         if (isl_set_fast_is_empty(set) || FN(EL,IS_ZERO)(el)) {
55                 isl_set_free(set);
56                 FN(EL,free)(el);
57                 return pw;
58         }
59
60 #ifdef HAS_TYPE
61         if (pw->type != el->type)
62                 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
63                         goto error);
64 #endif
65         isl_assert(set->ctx, isl_dim_equal(pw->dim, el->dim), goto error);
66         isl_assert(set->ctx, pw->n < pw->size, goto error);
67
68         pw->p[pw->n].set = set;
69         pw->p[pw->n].FIELD = el;
70         pw->n++;
71         
72         return pw;
73 error:
74         FN(PW,free)(pw);
75         isl_set_free(set);
76         FN(EL,free)(el);
77         return NULL;
78 }
79
80 #ifdef HAS_TYPE
81 __isl_give PW *FN(PW,alloc)(enum isl_fold type,
82         __isl_take isl_set *set, __isl_take EL *el)
83 #else
84 __isl_give PW *FN(PW,alloc)(__isl_take isl_set *set, __isl_take EL *el)
85 #endif
86 {
87         PW *pw;
88
89         if (!set || !el)
90                 goto error;
91
92 #ifdef HAS_TYPE
93         pw = FN(PW,alloc_)(isl_set_get_dim(set), type, 1);
94 #else
95         pw = FN(PW,alloc_)(isl_set_get_dim(set), 1);
96 #endif
97
98         return FN(PW,add_piece)(pw, set, el);
99 error:
100         isl_set_free(set);
101         FN(EL,free)(el);
102         return NULL;
103 }
104
105 __isl_give PW *FN(PW,dup)(__isl_keep PW *pw)
106 {
107         int i;
108         PW *dup;
109
110         if (!pw)
111                 return NULL;
112
113 #ifdef HAS_TYPE
114         dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->type, pw->n);
115 #else
116         dup = FN(PW,alloc_)(isl_dim_copy(pw->dim), pw->n);
117 #endif
118         if (!dup)
119                 return NULL;
120
121         for (i = 0; i < pw->n; ++i)
122                 dup = FN(PW,add_piece)(dup, isl_set_copy(pw->p[i].set),
123                                             FN(EL,copy)(pw->p[i].FIELD));
124
125         return dup;
126 error:
127         FN(PW,free)(dup);
128         return NULL;
129 }
130
131 __isl_give PW *FN(PW,cow)(__isl_take PW *pw)
132 {
133         if (!pw)
134                 return NULL;
135
136         if (pw->ref == 1)
137                 return pw;
138         pw->ref--;
139         return FN(PW,dup)(pw);
140 }
141
142 __isl_give PW *FN(PW,copy)(__isl_keep PW *pw)
143 {
144         if (!pw)
145                 return NULL;
146
147         pw->ref++;
148         return pw;
149 }
150
151 void FN(PW,free)(__isl_take PW *pw)
152 {
153         int i;
154
155         if (!pw)
156                 return;
157         if (--pw->ref > 0)
158                 return;
159
160         for (i = 0; i < pw->n; ++i) {
161                 isl_set_free(pw->p[i].set);
162                 FN(EL,free)(pw->p[i].FIELD);
163         }
164         isl_dim_free(pw->dim);
165         free(pw);
166 }
167
168 int FN(PW,is_zero)(__isl_keep PW *pw)
169 {
170         if (!pw)
171                 return -1;
172
173         return pw->n == 0;
174 }
175
176 __isl_give PW *FN(PW,add)(__isl_take PW *pw1, __isl_take PW *pw2)
177 {
178         int i, j, n;
179         struct PW *res;
180         isl_set *set;
181
182         if (!pw1 || !pw2)
183                 goto error;
184
185 #ifdef HAS_TYPE
186         if (pw1->type != pw2->type)
187                 isl_die(pw1->dim->ctx, isl_error_invalid,
188                         "fold types don't match", goto error);
189 #endif
190         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
191
192         if (FN(PW,is_zero)(pw1)) {
193                 FN(PW,free)(pw1);
194                 return pw2;
195         }
196
197         if (FN(PW,is_zero)(pw2)) {
198                 FN(PW,free)(pw2);
199                 return pw1;
200         }
201
202         n = (pw1->n + 1) * (pw2->n + 1);
203 #ifdef HAS_TYPE
204         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, n);
205 #else
206         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), n);
207 #endif
208
209         for (i = 0; i < pw1->n; ++i) {
210                 set = isl_set_copy(pw1->p[i].set);
211                 for (j = 0; j < pw2->n; ++j) {
212                         struct isl_set *common;
213                         EL *sum;
214                         set = isl_set_subtract(set,
215                                         isl_set_copy(pw2->p[j].set));
216                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
217                                                 isl_set_copy(pw2->p[j].set));
218                         if (isl_set_fast_is_empty(common)) {
219                                 isl_set_free(common);
220                                 continue;
221                         }
222
223                         sum = FN(EL,add_on_domain)(common,
224                                                    FN(EL,copy)(pw1->p[i].FIELD),
225                                                    FN(EL,copy)(pw2->p[j].FIELD));
226
227                         res = FN(PW,add_piece)(res, common, sum);
228                 }
229                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw1->p[i].FIELD));
230         }
231
232         for (j = 0; j < pw2->n; ++j) {
233                 set = isl_set_copy(pw2->p[j].set);
234                 for (i = 0; i < pw1->n; ++i)
235                         set = isl_set_subtract(set,
236                                         isl_set_copy(pw1->p[i].set));
237                 res = FN(PW,add_piece)(res, set, FN(EL,copy)(pw2->p[j].FIELD));
238         }
239
240         FN(PW,free)(pw1);
241         FN(PW,free)(pw2);
242
243         return res;
244 error:
245         FN(PW,free)(pw1);
246         FN(PW,free)(pw2);
247         return NULL;
248 }
249
250 __isl_give PW *FN(PW,add_disjoint)(__isl_take PW *pw1, __isl_take PW *pw2)
251 {
252         int i;
253         PW *res;
254
255         if (!pw1 || !pw2)
256                 goto error;
257
258 #ifdef HAS_TYPE
259         if (pw1->type != pw2->type)
260                 isl_die(pw1->dim->ctx, isl_error_invalid,
261                         "fold types don't match", goto error);
262 #endif
263         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
264
265         if (FN(PW,is_zero)(pw1)) {
266                 FN(PW,free)(pw1);
267                 return pw2;
268         }
269
270         if (FN(PW,is_zero)(pw2)) {
271                 FN(PW,free)(pw2);
272                 return pw1;
273         }
274
275 #ifdef HAS_TYPE
276         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->type, pw1->n + pw2->n);
277 #else
278         res = FN(PW,alloc_)(isl_dim_copy(pw1->dim), pw1->n + pw2->n);
279 #endif
280
281         for (i = 0; i < pw1->n; ++i)
282                 res = FN(PW,add_piece)(res,
283                                 isl_set_copy(pw1->p[i].set),
284                                 FN(EL,copy)(pw1->p[i].FIELD));
285
286         for (i = 0; i < pw2->n; ++i)
287                 res = FN(PW,add_piece)(res,
288                                 isl_set_copy(pw2->p[i].set),
289                                 FN(EL,copy)(pw2->p[i].FIELD));
290
291         FN(PW,free)(pw1);
292         FN(PW,free)(pw2);
293
294         return res;
295 error:
296         FN(PW,free)(pw1);
297         FN(PW,free)(pw2);
298         return NULL;
299 }
300
301 __isl_give isl_qpolynomial *FN(PW,eval)(__isl_take PW *pw,
302         __isl_take isl_point *pnt)
303 {
304         int i;
305         int found = 0;
306         isl_qpolynomial *qp;
307
308         if (!pw || !pnt)
309                 goto error;
310         isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, pw->dim), goto error);
311
312         for (i = 0; i < pw->n; ++i) {
313                 found = isl_set_contains_point(pw->p[i].set, pnt);
314                 if (found < 0)
315                         goto error;
316                 if (found)
317                         break;
318         }
319         if (found)
320                 qp = FN(EL,eval)(FN(EL,copy)(pw->p[i].FIELD),
321                                             isl_point_copy(pnt));
322         else
323                 qp = isl_qpolynomial_zero(isl_dim_copy(pw->dim));
324         FN(PW,free)(pw);
325         isl_point_free(pnt);
326         return qp;
327 error:
328         FN(PW,free)(pw);
329         isl_point_free(pnt);
330         return NULL;
331 }
332
333 __isl_give isl_set *FN(PW,domain)(__isl_take PW *pw)
334 {
335         int i;
336         isl_set *dom;
337
338         if (!pw)
339                 return NULL;
340
341         dom = isl_set_empty(isl_dim_copy(pw->dim));
342         for (i = 0; i < pw->n; ++i)
343                 dom = isl_set_union_disjoint(dom, isl_set_copy(pw->p[i].set));
344
345         FN(PW,free)(pw);
346
347         return dom;
348 }
349
350 __isl_give PW *FN(PW,intersect_domain)(__isl_take PW *pw, __isl_take isl_set *set)
351 {
352         int i;
353
354         if (!pw || !set)
355                 goto error;
356
357         if (pw->n == 0) {
358                 isl_set_free(set);
359                 return pw;
360         }
361
362         pw = FN(PW,cow)(pw);
363         if (!pw)
364                 goto error;
365
366         for (i = pw->n - 1; i >= 0; --i) {
367                 isl_basic_set *aff;
368                 pw->p[i].set = isl_set_intersect(pw->p[i].set, isl_set_copy(set));
369                 if (!pw->p[i].set)
370                         goto error;
371                 aff = isl_set_affine_hull(isl_set_copy(pw->p[i].set));
372                 pw->p[i].FIELD = FN(EL,substitute_equalities)(pw->p[i].FIELD,
373                                                                 aff);
374                 if (isl_set_fast_is_empty(pw->p[i].set)) {
375                         isl_set_free(pw->p[i].set);
376                         FN(EL,free)(pw->p[i].FIELD);
377                         if (i != pw->n - 1)
378                                 pw->p[i] = pw->p[pw->n - 1];
379                         pw->n--;
380                 }
381         }
382         
383         isl_set_free(set);
384         return pw;
385 error:
386         isl_set_free(set);
387         FN(PW,free)(pw);
388         return NULL;
389 }
390
391 __isl_give PW *FN(PW,gist)(__isl_take PW *pw, __isl_take isl_set *context)
392 {
393         int i;
394         isl_basic_set *hull = NULL;
395
396         if (!pw || !context)
397                 goto error;
398
399         if (pw->n == 0) {
400                 isl_set_free(context);
401                 return pw;
402         }
403
404         hull = isl_set_simple_hull(isl_set_copy(context));
405
406         pw = FN(PW,cow)(pw);
407         if (!pw)
408                 goto error;
409
410         for (i = 0; i < pw->n; ++i) {
411                 pw->p[i].set = isl_set_gist_basic_set(pw->p[i].set,
412                                                 isl_basic_set_copy(hull));
413                 if (!pw->p[i].set)
414                         goto error;
415         }
416
417         isl_basic_set_free(hull);
418         isl_set_free(context);
419
420         return pw;
421 error:
422         FN(PW,free)(pw);
423         isl_basic_set_free(hull);
424         isl_set_free(context);
425         return NULL;
426 }
427
428 __isl_give PW *FN(PW,coalesce)(__isl_take PW *pw)
429 {
430         int i, j;
431
432         if (!pw)
433                 return NULL;
434         if (pw->n == 0)
435                 return pw;
436
437         for (i = pw->n - 1; i >= 0; --i) {
438                 for (j = i - 1; j >= 0; --j) {
439                         if (!FN(EL,is_equal)(pw->p[i].FIELD, pw->p[j].FIELD))
440                                 continue;
441                         pw->p[j].set = isl_set_union(pw->p[j].set,
442                                                         pw->p[i].set);
443                         FN(EL,free)(pw->p[i].FIELD);
444                         if (i != pw->n - 1)
445                                 pw->p[i] = pw->p[pw->n - 1];
446                         pw->n--;
447                         break;
448                 }
449                 if (j >= 0)
450                         continue;
451                 pw->p[i].set = isl_set_coalesce(pw->p[i].set);
452                 if (!pw->p[i].set)
453                         goto error;
454         }
455
456         return pw;
457 error:
458         FN(PW,free)(pw);
459         return NULL;
460 }
461
462 isl_ctx *FN(PW,get_ctx)(__isl_keep PW *pw)
463 {
464         return pw ? pw->dim->ctx : NULL;
465 }
466
467 int FN(PW,involves_dims)(__isl_keep PW *pw, enum isl_dim_type type,
468         unsigned first, unsigned n)
469 {
470         int i;
471
472         if (!pw)
473                 return -1;
474         if (pw->n == 0 || n == 0)
475                 return 0;
476         for (i = 0; i < pw->n; ++i) {
477                 int involves = FN(EL,involves_dims)(pw->p[i].FIELD,
478                                                         type, first, n);
479                 if (involves < 0 || involves)
480                         return involves;
481         }
482         return 0;
483 }
484
485 __isl_give PW *FN(PW,drop_dims)(__isl_take PW *pw,
486         enum isl_dim_type type, unsigned first, unsigned n)
487 {
488         int i;
489
490         if (!pw)
491                 return NULL;
492         if (n == 0 && !isl_dim_get_tuple_name(pw->dim, type))
493                 return pw;
494
495         pw = FN(PW,cow)(pw);
496         if (!pw)
497                 return NULL;
498         pw->dim = isl_dim_drop(pw->dim, type, first, n);
499         if (!pw->dim)
500                 goto error;
501         for (i = 0; i < pw->n; ++i) {
502                 pw->p[i].set = isl_set_drop(pw->p[i].set, type, first, n);
503                 if (!pw->p[i].set)
504                         goto error;
505                 pw->p[i].FIELD = FN(EL,drop_dims)(pw->p[i].FIELD, type, first, n);
506                 if (!pw->p[i].FIELD)
507                         goto error;
508         }
509
510         return pw;
511 error:
512         FN(PW,free)(pw);
513         return NULL;
514 }
515
516 __isl_give PW *FN(PW,insert_dims)(__isl_take PW *pw, enum isl_dim_type type,
517         unsigned first, unsigned n)
518 {
519         int i;
520
521         if (!pw)
522                 return NULL;
523         if (n == 0 && !isl_dim_is_named_or_nested(pw->dim, type))
524                 return pw;
525
526         pw = FN(PW,cow)(pw);
527         if (!pw)
528                 return NULL;
529
530         pw->dim = isl_dim_insert(pw->dim, type, first, n);
531         if (!pw->dim)
532                 goto error;
533
534         for (i = 0; i < pw->n; ++i) {
535                 pw->p[i].set = isl_set_insert(pw->p[i].set, type, first, n);
536                 if (!pw->p[i].set)
537                         goto error;
538                 pw->p[i].FIELD = FN(EL,insert_dims)(pw->p[i].FIELD,
539                                                                 type, first, n);
540                 if (!pw->p[i].FIELD)
541                         goto error;
542         }
543
544         return pw;
545 error:
546         FN(PW,free)(pw);
547         return NULL;
548 }
549
550 __isl_give PW *FN(PW,fix_dim)(__isl_take PW *pw,
551         enum isl_dim_type type, unsigned pos, isl_int v)
552 {
553         int i;
554
555         if (!pw)
556                 return NULL;
557
558         pw = FN(PW,cow)(pw);
559         if (!pw)
560                 return NULL;
561         for (i = 0; i < pw->n; ++i) {
562                 pw->p[i].set = isl_set_fix(pw->p[i].set, type, pos, v);
563                 if (!pw->p[i].set)
564                         goto error;
565         }
566
567         return pw;
568 error:
569         FN(PW,free)(pw);
570         return NULL;
571 }
572
573 unsigned FN(PW,dim)(__isl_keep PW *pw, enum isl_dim_type type)
574 {
575         return pw ? isl_dim_size(pw->dim, type) : 0;
576 }
577
578 __isl_give PW *FN(PW,split_dims)(__isl_take PW *pw,
579         enum isl_dim_type type, unsigned first, unsigned n)
580 {
581         int i;
582
583         if (!pw)
584                 return NULL;
585         if (n == 0)
586                 return pw;
587
588         pw = FN(PW,cow)(pw);
589         if (!pw)
590                 return NULL;
591         if (!pw->dim)
592                 goto error;
593         for (i = 0; i < pw->n; ++i) {
594                 pw->p[i].set = isl_set_split_dims(pw->p[i].set, type, first, n);
595                 if (!pw->p[i].set)
596                         goto error;
597         }
598
599         return pw;
600 error:
601         FN(PW,free)(pw);
602         return NULL;
603 }
604
605 /* Compute the maximal value attained by the piecewise quasipolynomial
606  * on its domain or zero if the domain is empty.
607  * In the worst case, the domain is scanned completely,
608  * so the domain is assumed to be bounded.
609  */
610 __isl_give isl_qpolynomial *FN(PW,opt)(__isl_take PW *pw, int max)
611 {
612         int i;
613         isl_qpolynomial *opt;
614
615         if (!pw)
616                 return NULL;
617
618         if (pw->n == 0) {
619                 isl_dim *dim = isl_dim_copy(pw->dim);
620                 FN(PW,free)(pw);
621                 return isl_qpolynomial_zero(dim);
622         }
623
624         opt = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[0].FIELD),
625                                         isl_set_copy(pw->p[0].set), max);
626         for (i = 1; i < pw->n; ++i) {
627                 isl_qpolynomial *opt_i;
628                 opt_i = FN(EL,opt_on_domain)(FN(EL,copy)(pw->p[i].FIELD),
629                                                 isl_set_copy(pw->p[i].set), max);
630                 if (max)
631                         opt = isl_qpolynomial_max_cst(opt, opt_i);
632                 else
633                         opt = isl_qpolynomial_min_cst(opt, opt_i);
634         }
635
636         FN(PW,free)(pw);
637         return opt;
638 }
639
640 __isl_give isl_qpolynomial *FN(PW,max)(__isl_take PW *pw)
641 {
642         return FN(PW,opt)(pw, 1);
643 }
644
645 __isl_give isl_qpolynomial *FN(PW,min)(__isl_take PW *pw)
646 {
647         return FN(PW,opt)(pw, 0);
648 }
649
650 __isl_give isl_dim *FN(PW,get_dim)(__isl_keep PW *pw)
651 {
652         return pw ? isl_dim_copy(pw->dim) : NULL;
653 }
654
655 __isl_give PW *FN(PW,reset_dim)(__isl_take PW *pw, __isl_take isl_dim *dim)
656 {
657         int i;
658
659         pw = FN(PW,cow)(pw);
660         if (!pw || !dim)
661                 goto error;
662
663         for (i = 0; i < pw->n; ++i) {
664                 pw->p[i].set = isl_set_reset_dim(pw->p[i].set,
665                                                  isl_dim_copy(dim));
666                 if (!pw->p[i].set)
667                         goto error;
668                 pw->p[i].FIELD = FN(EL,reset_dim)(pw->p[i].FIELD,
669                                                   isl_dim_copy(dim));
670                 if (!pw->p[i].FIELD)
671                         goto error;
672         }
673         isl_dim_free(pw->dim);
674         pw->dim = dim;
675
676         return pw;
677 error:
678         isl_dim_free(dim);
679         FN(PW,free)(pw);
680         return NULL;
681 }
682
683 int FN(PW,has_equal_dim)(__isl_keep PW *pw1, __isl_keep PW *pw2)
684 {
685         if (!pw1 || !pw2)
686                 return -1;
687
688         return isl_dim_equal(pw1->dim, pw2->dim);
689 }
690
691 __isl_give PW *FN(PW,morph)(__isl_take PW *pw, __isl_take isl_morph *morph)
692 {
693         int i;
694
695         if (!pw || !morph)
696                 goto error;
697
698         isl_assert(pw->dim->ctx, isl_dim_equal(pw->dim, morph->dom->dim),
699                 goto error);
700
701         pw = FN(PW,cow)(pw);
702         if (!pw)
703                 goto error;
704         isl_dim_free(pw->dim);
705         pw->dim = isl_dim_copy(morph->ran->dim);
706         if (!pw->dim)
707                 goto error;
708
709         for (i = 0; i < pw->n; ++i) {
710                 pw->p[i].set = isl_morph_set(isl_morph_copy(morph), pw->p[i].set);
711                 if (!pw->p[i].set)
712                         goto error;
713                 pw->p[i].FIELD = FN(EL,morph)(pw->p[i].FIELD,
714                                                 isl_morph_copy(morph));
715                 if (!pw->p[i].FIELD)
716                         goto error;
717         }
718
719         isl_morph_free(morph);
720
721         return pw;
722 error:
723         FN(PW,free)(pw);
724         isl_morph_free(morph);
725         return NULL;
726 }
727
728 int FN(PW,foreach_piece)(__isl_keep PW *pw,
729         int (*fn)(__isl_take isl_set *set, __isl_take EL *el, void *user),
730         void *user)
731 {
732         int i;
733
734         if (!pw)
735                 return -1;
736
737         for (i = 0; i < pw->n; ++i)
738                 if (fn(isl_set_copy(pw->p[i].set),
739                                 FN(EL,copy)(pw->p[i].FIELD), user) < 0)
740                         return -1;
741
742         return 0;
743 }
744
745 static int any_divs(__isl_keep isl_set *set)
746 {
747         int i;
748
749         if (!set)
750                 return -1;
751
752         for (i = 0; i < set->n; ++i)
753                 if (set->p[i]->n_div > 0)
754                         return 1;
755
756         return 0;
757 }
758
759 static int foreach_lifted_subset(__isl_take isl_set *set, __isl_take EL *el,
760         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
761                     void *user), void *user)
762 {
763         int i;
764
765         if (!set || !el)
766                 goto error;
767
768         for (i = 0; i < set->n; ++i) {
769                 isl_set *lift;
770                 EL *copy;
771
772                 lift = isl_set_from_basic_set(isl_basic_set_copy(set->p[i]));
773                 lift = isl_set_lift(lift);
774
775                 copy = FN(EL,copy)(el);
776                 copy = FN(EL,lift)(copy, isl_set_get_dim(lift));
777
778                 if (fn(lift, copy, user) < 0)
779                         goto error;
780         }
781
782         isl_set_free(set);
783         FN(EL,free)(el);
784
785         return 0;
786 error:
787         isl_set_free(set);
788         FN(EL,free)(el);
789         return -1;
790 }
791
792 int FN(PW,foreach_lifted_piece)(__isl_keep PW *pw,
793         int (*fn)(__isl_take isl_set *set, __isl_take EL *el,
794                     void *user), void *user)
795 {
796         int i;
797
798         if (!pw)
799                 return -1;
800
801         for (i = 0; i < pw->n; ++i) {
802                 isl_set *set;
803                 EL *el;
804
805                 set = isl_set_copy(pw->p[i].set);
806                 el = FN(EL,copy)(pw->p[i].FIELD);
807                 if (!any_divs(set)) {
808                         if (fn(set, el, user) < 0)
809                                 return -1;
810                         continue;
811                 }
812                 if (foreach_lifted_subset(set, el, fn, user) < 0)
813                         return -1;
814         }
815
816         return 0;
817 }
818
819 __isl_give PW *FN(PW,move_dims)(__isl_take PW *pw,
820         enum isl_dim_type dst_type, unsigned dst_pos,
821         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
822 {
823         int i;
824
825         pw = FN(PW,cow)(pw);
826         if (!pw)
827                 return NULL;
828
829         pw->dim = isl_dim_move(pw->dim, dst_type, dst_pos, src_type, src_pos, n);
830         if (!pw->dim)
831                 goto error;
832
833         for (i = 0; i < pw->n; ++i) {
834                 pw->p[i].set = isl_set_move_dims(pw->p[i].set,
835                                                 dst_type, dst_pos,
836                                                 src_type, src_pos, n);
837                 if (!pw->p[i].set)
838                         goto error;
839                 pw->p[i].FIELD = FN(EL,move_dims)(pw->p[i].FIELD,
840                                         dst_type, dst_pos, src_type, src_pos, n);
841                 if (!pw->p[i].FIELD)
842                         goto error;
843         }
844
845         return pw;
846 error:
847         FN(PW,free)(pw);
848         return NULL;
849 }
850
851 __isl_give PW *FN(PW,realign)(__isl_take PW *pw, __isl_take isl_reordering *exp)
852 {
853         int i;
854
855         pw = FN(PW,cow)(pw);
856         if (!pw || !exp)
857                 return NULL;
858
859         for (i = 0; i < pw->n; ++i) {
860                 pw->p[i].set = isl_set_realign(pw->p[i].set,
861                                                     isl_reordering_copy(exp));
862                 if (!pw->p[i].set)
863                         goto error;
864                 pw->p[i].FIELD = FN(EL,realign)(pw->p[i].FIELD,
865                                                     isl_reordering_copy(exp));
866                 if (!pw->p[i].FIELD)
867                         goto error;
868         }
869
870         pw = FN(PW,reset_dim)(pw, isl_dim_copy(exp->dim));
871
872         isl_reordering_free(exp);
873         return pw;
874 error:
875         isl_reordering_free(exp);
876         FN(PW,free)(pw);
877         return NULL;
878 }