isl_basic_map_gist: prefer contraints without existentially quantified variables
[platform/upstream/isl.git] / isl_fold.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #include <isl_map_private.h>
12 #include <isl_union_map_private.h>
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
15 #include <isl_dim_private.h>
16 #include <isl/lp.h>
17 #include <isl/seq.h>
18 #include <isl_mat_private.h>
19
20 enum isl_fold isl_fold_type_negate(enum isl_fold type)
21 {
22         switch (type) {
23         case isl_fold_min:
24                 return isl_fold_max;
25         case isl_fold_max:
26                 return isl_fold_min;
27         case isl_fold_list:
28                 return isl_fold_list;
29         }
30 }
31
32 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
33         enum isl_fold type, __isl_take isl_dim *dim, int n)
34 {
35         isl_qpolynomial_fold *fold;
36
37         if (!dim)
38                 goto error;
39
40         isl_assert(dim->ctx, n >= 0, goto error);
41         fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,
42                         sizeof(struct isl_qpolynomial_fold) +
43                         (n - 1) * sizeof(struct isl_qpolynomial *));
44         if (!fold)
45                 goto error;
46
47         fold->ref = 1;
48         fold->size = n;
49         fold->n = 0;
50         fold->type = type;
51         fold->dim = dim;
52
53         return fold;
54 error:
55         isl_dim_free(dim);
56         return NULL;
57 }
58
59 isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
60 {
61         return fold ? fold->dim->ctx : NULL;
62 }
63
64 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_dim(
65         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
66 {
67         int i;
68
69         fold = isl_qpolynomial_fold_cow(fold);
70         if (!fold || !dim)
71                 goto error;
72
73         for (i = 0; i < fold->n; ++i) {
74                 fold->qp[i] = isl_qpolynomial_reset_dim(fold->qp[i],
75                                                         isl_dim_copy(dim));
76                 if (!fold->qp[i])
77                         goto error;
78         }
79
80         isl_dim_free(fold->dim);
81         fold->dim = dim;
82
83         return fold;
84 error:
85         isl_qpolynomial_fold_free(fold);
86         isl_dim_free(dim);
87         return NULL;
88 }
89
90 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
91         enum isl_dim_type type, unsigned first, unsigned n)
92 {
93         int i;
94
95         if (!fold)
96                 return -1;
97         if (fold->n == 0 || n == 0)
98                 return 0;
99
100         for (i = 0; i < fold->n; ++i) {
101                 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
102                                                             type, first, n);
103                 if (involves < 0 || involves)
104                         return involves;
105         }
106         return 0;
107 }
108
109 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_set_dim_name(
110         __isl_take isl_qpolynomial_fold *fold,
111         enum isl_dim_type type, unsigned pos, const char *s)
112 {
113         int i;
114
115         fold = isl_qpolynomial_fold_cow(fold);
116         if (!fold)
117                 return NULL;
118         fold->dim = isl_dim_set_name(fold->dim, type, pos, s);
119         if (!fold->dim)
120                 goto error;
121
122         for (i = 0; i < fold->n; ++i) {
123                 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
124                                                             type, pos, s);
125                 if (!fold->qp[i])
126                         goto error;
127         }
128
129         return fold;
130 error:
131         isl_qpolynomial_fold_free(fold);
132         return NULL;
133 }
134
135 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
136         __isl_take isl_qpolynomial_fold *fold,
137         enum isl_dim_type type, unsigned first, unsigned n)
138 {
139         int i;
140
141         if (!fold)
142                 return NULL;
143         if (n == 0)
144                 return fold;
145
146         fold = isl_qpolynomial_fold_cow(fold);
147         if (!fold)
148                 return NULL;
149         fold->dim = isl_dim_drop(fold->dim, type, first, n);
150         if (!fold->dim)
151                 goto error;
152
153         for (i = 0; i < fold->n; ++i) {
154                 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
155                                                             type, first, n);
156                 if (!fold->qp[i])
157                         goto error;
158         }
159
160         return fold;
161 error:
162         isl_qpolynomial_fold_free(fold);
163         return NULL;
164 }
165
166 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
167         __isl_take isl_qpolynomial_fold *fold,
168         enum isl_dim_type type, unsigned first, unsigned n)
169 {
170         int i;
171
172         if (!fold)
173                 return NULL;
174         if (n == 0 && !isl_dim_is_named_or_nested(fold->dim, type))
175                 return fold;
176
177         fold = isl_qpolynomial_fold_cow(fold);
178         if (!fold)
179                 return NULL;
180         fold->dim = isl_dim_insert(fold->dim, type, first, n);
181         if (!fold->dim)
182                 goto error;
183
184         for (i = 0; i < fold->n; ++i) {
185                 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
186                                                             type, first, n);
187                 if (!fold->qp[i])
188                         goto error;
189         }
190
191         return fold;
192 error:
193         isl_qpolynomial_fold_free(fold);
194         return NULL;
195 }
196
197 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
198 {
199         struct isl_upoly_cst *cst;
200
201         cst = isl_upoly_as_cst(qp->upoly);
202         if (!cst)
203                 return 0;
204
205         return isl_int_sgn(cst->n) < 0 ? -1 : 1;
206 }
207
208 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
209         __isl_keep isl_qpolynomial *qp)
210 {
211         enum isl_lp_result res;
212         isl_vec *aff;
213         isl_int opt;
214         int sgn = 0;
215
216         aff = isl_qpolynomial_extract_affine(qp);
217         if (!aff)
218                 return 0;
219
220         isl_int_init(opt);
221
222         res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
223                                 &opt, NULL, NULL);
224         if (res == isl_lp_error)
225                 goto done;
226         if (res == isl_lp_empty ||
227             (res == isl_lp_ok && !isl_int_is_neg(opt))) {
228                 sgn = 1;
229                 goto done;
230         }
231
232         res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
233                                 &opt, NULL, NULL);
234         if (res == isl_lp_ok && !isl_int_is_pos(opt))
235                 sgn = -1;
236
237 done:
238         isl_int_clear(opt);
239         isl_vec_free(aff);
240         return sgn;
241 }
242
243 /* Determine, if possible, the sign of the quasipolynomial "qp" on
244  * the domain "set".
245  *
246  * If qp is a constant, then the problem is trivial.
247  * If qp is linear, then we check if the minimum of the corresponding
248  * affine constraint is non-negative or if the maximum is non-positive.
249  *
250  * Otherwise, we check if the outermost variable "v" has a lower bound "l"
251  * in "set".  If so, we write qp(v,v') as
252  *
253  *      q(v,v') * (v - l) + r(v')
254  *
255  * if q(v,v') and r(v') have the same known sign, then the original
256  * quasipolynomial has the same sign as well.
257  *
258  * Return
259  *      -1 if qp <= 0
260  *       1 if qp >= 0
261  *       0 if unknown
262  */
263 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
264         __isl_keep isl_qpolynomial *qp)
265 {
266         int d;
267         int i;
268         int is;
269         struct isl_upoly_rec *rec;
270         isl_vec *v;
271         isl_int l;
272         enum isl_lp_result res;
273         int sgn = 0;
274
275         is = isl_qpolynomial_is_cst(qp, NULL, NULL);
276         if (is < 0)
277                 return 0;
278         if (is)
279                 return isl_qpolynomial_cst_sign(qp);
280
281         is = isl_qpolynomial_is_affine(qp);
282         if (is < 0)
283                 return 0;
284         if (is)
285                 return isl_qpolynomial_aff_sign(set, qp);
286
287         if (qp->div->n_row > 0)
288                 return 0;
289
290         rec = isl_upoly_as_rec(qp->upoly);
291         if (!rec)
292                 return 0;
293
294         d = isl_dim_total(qp->dim);
295         v = isl_vec_alloc(set->ctx, 2 + d);
296         if (!v)
297                 return 0;
298
299         isl_seq_clr(v->el + 1, 1 + d);
300         isl_int_set_si(v->el[0], 1);
301         isl_int_set_si(v->el[2 + qp->upoly->var], 1);
302
303         isl_int_init(l);
304
305         res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
306         if (res == isl_lp_ok) {
307                 isl_qpolynomial *min;
308                 isl_qpolynomial *base;
309                 isl_qpolynomial *r, *q;
310                 isl_qpolynomial *t;
311
312                 min = isl_qpolynomial_cst(isl_dim_copy(qp->dim), l);
313                 base = isl_qpolynomial_var_pow(isl_dim_copy(qp->dim),
314                                                 qp->upoly->var, 1);
315
316                 r = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
317                                           isl_upoly_copy(rec->p[rec->n - 1]));
318                 q = isl_qpolynomial_copy(r);
319
320                 for (i = rec->n - 2; i >= 0; --i) {
321                         r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
322                         t = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
323                                                   isl_upoly_copy(rec->p[i]));
324                         r = isl_qpolynomial_add(r, t);
325                         if (i == 0)
326                                 break;
327                         q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
328                         q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
329                 }
330
331                 if (isl_qpolynomial_is_zero(q))
332                         sgn = isl_qpolynomial_sign(set, r);
333                 else if (isl_qpolynomial_is_zero(r))
334                         sgn = isl_qpolynomial_sign(set, q);
335                 else {
336                         int sgn_q, sgn_r;
337                         sgn_r = isl_qpolynomial_sign(set, r);
338                         sgn_q = isl_qpolynomial_sign(set, q);
339                         if (sgn_r == sgn_q)
340                                 sgn = sgn_r;
341                 }
342
343                 isl_qpolynomial_free(min);
344                 isl_qpolynomial_free(base);
345                 isl_qpolynomial_free(q);
346                 isl_qpolynomial_free(r);
347         }
348
349         isl_int_clear(l);
350
351         isl_vec_free(v);
352
353         return sgn;
354 }
355
356 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
357         __isl_keep isl_set *set,
358         __isl_take isl_qpolynomial_fold *fold1,
359         __isl_take isl_qpolynomial_fold *fold2)
360 {
361         int i, j;
362         int n1;
363         struct isl_qpolynomial_fold *res = NULL;
364         int better;
365
366         if (!fold1 || !fold2)
367                 goto error;
368
369         isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
370         isl_assert(fold1->dim->ctx, isl_dim_equal(fold1->dim, fold2->dim),
371                         goto error);
372
373         better = fold1->type == isl_fold_max ? -1 : 1;
374
375         if (isl_qpolynomial_fold_is_empty(fold1)) {
376                 isl_qpolynomial_fold_free(fold1);
377                 return fold2;
378         }
379
380         if (isl_qpolynomial_fold_is_empty(fold2)) {
381                 isl_qpolynomial_fold_free(fold2);
382                 return fold1;
383         }
384
385         res = qpolynomial_fold_alloc(fold1->type, isl_dim_copy(fold1->dim),
386                                         fold1->n + fold2->n);
387         if (!res)
388                 goto error;
389
390         for (i = 0; i < fold1->n; ++i) {
391                 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
392                 if (!res->qp[res->n])
393                         goto error;
394                 res->n++;
395         }
396         n1 = res->n;
397
398         for (i = 0; i < fold2->n; ++i) {
399                 for (j = n1 - 1; j >= 0; --j) {
400                         isl_qpolynomial *d;
401                         int sgn;
402                         d = isl_qpolynomial_sub(
403                                 isl_qpolynomial_copy(res->qp[j]),
404                                 isl_qpolynomial_copy(fold2->qp[i]));
405                         sgn = isl_qpolynomial_sign(set, d);
406                         isl_qpolynomial_free(d);
407                         if (sgn == 0)
408                                 continue;
409                         if (sgn != better)
410                                 break;
411                         isl_qpolynomial_free(res->qp[j]);
412                         if (j != n1 - 1)
413                                 res->qp[j] = res->qp[n1 - 1];
414                         n1--;
415                         if (n1 != res->n - 1)
416                                 res->qp[n1] = res->qp[res->n - 1];
417                         res->n--;
418                 }
419                 if (j >= 0)
420                         continue;
421                 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
422                 if (!res->qp[res->n])
423                         goto error;
424                 res->n++;
425         }
426
427         isl_qpolynomial_fold_free(fold1);
428         isl_qpolynomial_fold_free(fold2);
429
430         return res;
431 error:
432         isl_qpolynomial_fold_free(res);
433         isl_qpolynomial_fold_free(fold1);
434         isl_qpolynomial_fold_free(fold2);
435         return NULL;
436 }
437
438 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
439         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
440 {
441         int i;
442
443         if (!fold || !qp)
444                 goto error;
445
446         if (isl_qpolynomial_is_zero(qp)) {
447                 isl_qpolynomial_free(qp);
448                 return fold;
449         }
450
451         fold = isl_qpolynomial_fold_cow(fold);
452         if (!fold)
453                 goto error;
454
455         for (i = 0; i < fold->n; ++i) {
456                 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
457                                                 isl_qpolynomial_copy(qp));
458                 if (!fold->qp[i])
459                         goto error;
460         }
461
462         isl_qpolynomial_free(qp);
463         return fold;
464 error:
465         isl_qpolynomial_fold_free(fold);
466         isl_qpolynomial_free(qp);
467         return NULL;
468 }
469
470 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
471         __isl_keep isl_set *dom,
472         __isl_take isl_qpolynomial_fold *fold1,
473         __isl_take isl_qpolynomial_fold *fold2)
474 {
475         int i;
476         isl_qpolynomial_fold *res = NULL;
477
478         if (!fold1 || !fold2)
479                 goto error;
480
481         if (isl_qpolynomial_fold_is_empty(fold1)) {
482                 isl_qpolynomial_fold_free(fold1);
483                 return fold2;
484         }
485
486         if (isl_qpolynomial_fold_is_empty(fold2)) {
487                 isl_qpolynomial_fold_free(fold2);
488                 return fold1;
489         }
490
491         if (fold1->n == 1 && fold2->n != 1)
492                 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
493
494         if (fold2->n == 1) {
495                 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
496                                         isl_qpolynomial_copy(fold2->qp[0]));
497                 isl_qpolynomial_fold_free(fold2);
498                 return res;
499         }
500
501         res = isl_qpolynomial_fold_add_qpolynomial(
502                                 isl_qpolynomial_fold_copy(fold1),
503                                 isl_qpolynomial_copy(fold2->qp[0]));
504
505         for (i = 1; i < fold2->n; ++i) {
506                 isl_qpolynomial_fold *res_i;
507                 res_i = isl_qpolynomial_fold_add_qpolynomial(
508                                         isl_qpolynomial_fold_copy(fold1),
509                                         isl_qpolynomial_copy(fold2->qp[i]));
510                 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
511         }
512
513         isl_qpolynomial_fold_free(fold1);
514         isl_qpolynomial_fold_free(fold2);
515         return res;
516 error:
517         isl_qpolynomial_fold_free(res);
518         isl_qpolynomial_fold_free(fold1);
519         isl_qpolynomial_fold_free(fold2);
520         return NULL;
521 }
522
523 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
524         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
525 {
526         int i;
527
528         if (!fold || !eq)
529                 goto error;
530
531         fold = isl_qpolynomial_fold_cow(fold);
532         if (!fold)
533                 return NULL;
534
535         for (i = 0; i < fold->n; ++i) {
536                 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
537                                                         isl_basic_set_copy(eq));
538                 if (!fold->qp[i])
539                         goto error;
540         }
541
542         isl_basic_set_free(eq);
543         return fold;
544 error:
545         isl_basic_set_free(eq);
546         isl_qpolynomial_fold_free(fold);
547         return NULL;
548 }
549
550 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
551         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
552 {
553         int i;
554
555         if (!fold || !context)
556                 goto error;
557
558         fold = isl_qpolynomial_fold_cow(fold);
559         if (!fold)
560                 return NULL;
561
562         for (i = 0; i < fold->n; ++i) {
563                 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
564                                                         isl_set_copy(context));
565                 if (!fold->qp[i])
566                         goto error;
567         }
568
569         isl_set_free(context);
570         return fold;
571 error:
572         isl_set_free(context);
573         isl_qpolynomial_fold_free(fold);
574         return NULL;
575 }
576
577 #define HAS_TYPE
578
579 #undef PW
580 #define PW isl_pw_qpolynomial_fold
581 #undef EL
582 #define EL isl_qpolynomial_fold
583 #undef IS_ZERO
584 #define IS_ZERO is_empty
585 #undef FIELD
586 #define FIELD fold
587
588 #include <isl_pw_templ.c>
589
590 #undef UNION
591 #define UNION isl_union_pw_qpolynomial_fold
592 #undef PART
593 #define PART isl_pw_qpolynomial_fold
594 #undef PARTS
595 #define PARTS pw_qpolynomial_fold
596
597 #include <isl_union_templ.c>
598
599 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
600         __isl_take isl_dim *dim)
601 {
602         return qpolynomial_fold_alloc(type, dim, 0);
603 }
604
605 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
606         enum isl_fold type, __isl_take isl_qpolynomial *qp)
607 {
608         isl_qpolynomial_fold *fold;
609
610         if (!qp)
611                 return NULL;
612
613         fold = qpolynomial_fold_alloc(type, isl_dim_copy(qp->dim), 1);
614         if (!fold)
615                 goto error;
616
617         fold->qp[0] = qp;
618         fold->n++;
619
620         return fold;
621 error:
622         isl_qpolynomial_fold_free(fold);
623         isl_qpolynomial_free(qp);
624         return NULL;
625 }
626
627 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
628         __isl_keep isl_qpolynomial_fold *fold)
629 {
630         if (!fold)
631                 return NULL;
632
633         fold->ref++;
634         return fold;
635 }
636
637 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
638         __isl_keep isl_qpolynomial_fold *fold)
639 {
640         int i;
641         isl_qpolynomial_fold *dup;
642
643         if (!fold)
644                 return NULL;
645         dup = qpolynomial_fold_alloc(fold->type,
646                                         isl_dim_copy(fold->dim), fold->n);
647         if (!dup)
648                 return NULL;
649         
650         dup->n = fold->n;
651         for (i = 0; i < fold->n; ++i) {
652                 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
653                 if (!dup->qp[i])
654                         goto error;
655         }
656
657         return dup;
658 error:
659         isl_qpolynomial_fold_free(dup);
660         return NULL;
661 }
662
663 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
664         __isl_take isl_qpolynomial_fold *fold)
665 {
666         if (!fold)
667                 return NULL;
668
669         if (fold->ref == 1)
670                 return fold;
671         fold->ref--;
672         return isl_qpolynomial_fold_dup(fold);
673 }
674
675 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
676 {
677         int i;
678
679         if (!fold)
680                 return;
681         if (--fold->ref > 0)
682                 return;
683
684         for (i = 0; i < fold->n; ++i)
685                 isl_qpolynomial_free(fold->qp[i]);
686         isl_dim_free(fold->dim);
687         free(fold);
688 }
689
690 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
691 {
692         if (!fold)
693                 return -1;
694
695         return fold->n == 0;
696 }
697
698 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
699         __isl_take isl_qpolynomial_fold *fold1,
700         __isl_take isl_qpolynomial_fold *fold2)
701 {
702         int i;
703         struct isl_qpolynomial_fold *res = NULL;
704
705         if (!fold1 || !fold2)
706                 goto error;
707
708         isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
709         isl_assert(fold1->dim->ctx, isl_dim_equal(fold1->dim, fold2->dim),
710                         goto error);
711
712         if (isl_qpolynomial_fold_is_empty(fold1)) {
713                 isl_qpolynomial_fold_free(fold1);
714                 return fold2;
715         }
716
717         if (isl_qpolynomial_fold_is_empty(fold2)) {
718                 isl_qpolynomial_fold_free(fold2);
719                 return fold1;
720         }
721
722         res = qpolynomial_fold_alloc(fold1->type, isl_dim_copy(fold1->dim),
723                                         fold1->n + fold2->n);
724         if (!res)
725                 goto error;
726
727         for (i = 0; i < fold1->n; ++i) {
728                 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
729                 if (!res->qp[res->n])
730                         goto error;
731                 res->n++;
732         }
733
734         for (i = 0; i < fold2->n; ++i) {
735                 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
736                 if (!res->qp[res->n])
737                         goto error;
738                 res->n++;
739         }
740
741         isl_qpolynomial_fold_free(fold1);
742         isl_qpolynomial_fold_free(fold2);
743
744         return res;
745 error:
746         isl_qpolynomial_fold_free(res);
747         isl_qpolynomial_fold_free(fold1);
748         isl_qpolynomial_fold_free(fold2);
749         return NULL;
750 }
751
752 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
753         __isl_take isl_pw_qpolynomial_fold *pw1,
754         __isl_take isl_pw_qpolynomial_fold *pw2)
755 {
756         int i, j, n;
757         struct isl_pw_qpolynomial_fold *res;
758         isl_set *set;
759
760         if (!pw1 || !pw2)
761                 goto error;
762
763         isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
764
765         if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
766                 isl_pw_qpolynomial_fold_free(pw1);
767                 return pw2;
768         }
769
770         if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
771                 isl_pw_qpolynomial_fold_free(pw2);
772                 return pw1;
773         }
774
775         if (pw1->type != pw2->type)
776                 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
777                         goto error);
778
779         n = (pw1->n + 1) * (pw2->n + 1);
780         res = isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pw1->dim),
781                                                 pw1->type, n);
782
783         for (i = 0; i < pw1->n; ++i) {
784                 set = isl_set_copy(pw1->p[i].set);
785                 for (j = 0; j < pw2->n; ++j) {
786                         struct isl_set *common;
787                         isl_qpolynomial_fold *sum;
788                         set = isl_set_subtract(set,
789                                         isl_set_copy(pw2->p[j].set));
790                         common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
791                                                 isl_set_copy(pw2->p[j].set));
792                         if (isl_set_fast_is_empty(common)) {
793                                 isl_set_free(common);
794                                 continue;
795                         }
796
797                         sum = isl_qpolynomial_fold_fold_on_domain(common,
798                                isl_qpolynomial_fold_copy(pw1->p[i].fold),
799                                isl_qpolynomial_fold_copy(pw2->p[j].fold));
800
801                         res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
802                 }
803                 res = isl_pw_qpolynomial_fold_add_piece(res, set,
804                         isl_qpolynomial_fold_copy(pw1->p[i].fold));
805         }
806
807         for (j = 0; j < pw2->n; ++j) {
808                 set = isl_set_copy(pw2->p[j].set);
809                 for (i = 0; i < pw1->n; ++i)
810                         set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
811                 res = isl_pw_qpolynomial_fold_add_piece(res, set,
812                                     isl_qpolynomial_fold_copy(pw2->p[j].fold));
813         }
814
815         isl_pw_qpolynomial_fold_free(pw1);
816         isl_pw_qpolynomial_fold_free(pw2);
817
818         return res;
819 error:
820         isl_pw_qpolynomial_fold_free(pw1);
821         isl_pw_qpolynomial_fold_free(pw2);
822         return NULL;
823 }
824
825 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
826         __isl_take isl_union_pw_qpolynomial_fold *u,
827         __isl_take isl_pw_qpolynomial_fold *part)
828 {
829         uint32_t hash;
830         struct isl_hash_table_entry *entry;
831
832         u = isl_union_pw_qpolynomial_fold_cow(u);
833
834         if (!part || !u)
835                 goto error;
836
837         isl_assert(u->dim->ctx, isl_dim_match(part->dim, isl_dim_param, u->dim,
838                                               isl_dim_param), goto error);
839
840         hash = isl_dim_get_hash(part->dim);
841         entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
842                                     &has_dim, part->dim, 1);
843         if (!entry)
844                 goto error;
845
846         if (!entry->data)
847                 entry->data = part;
848         else {
849                 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
850                                             isl_pw_qpolynomial_fold_copy(part));
851                 if (!entry->data)
852                         goto error;
853                 isl_pw_qpolynomial_fold_free(part);
854         }
855
856         return u;
857 error:
858         isl_pw_qpolynomial_fold_free(part);
859         isl_union_pw_qpolynomial_fold_free(u);
860         return NULL;
861 }
862
863 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
864 {
865         isl_union_pw_qpolynomial_fold **u;
866         u = (isl_union_pw_qpolynomial_fold **)user;
867
868         *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
869
870         return 0;
871 }
872
873 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
874         __isl_take isl_union_pw_qpolynomial_fold *u1,
875         __isl_take isl_union_pw_qpolynomial_fold *u2)
876 {
877         u1 = isl_union_pw_qpolynomial_fold_cow(u1);
878
879         if (!u1 || !u2)
880                 goto error;
881
882         if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
883                                                         &fold_part, &u1) < 0)
884                 goto error;
885
886         isl_union_pw_qpolynomial_fold_free(u2);
887
888         return u1;
889 error:
890         isl_union_pw_qpolynomial_fold_free(u1);
891         isl_union_pw_qpolynomial_fold_free(u2);
892         return NULL;
893 }
894
895 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
896         enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
897 {
898         int i;
899         isl_pw_qpolynomial_fold *pwf;
900
901         if (!pwqp)
902                 return NULL;
903         
904         pwf = isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp->dim), type,
905                                                 pwqp->n);
906
907         for (i = 0; i < pwqp->n; ++i)
908                 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
909                         isl_set_copy(pwqp->p[i].set),
910                         isl_qpolynomial_fold_alloc(type,
911                                 isl_qpolynomial_copy(pwqp->p[i].qp)));
912
913         isl_pw_qpolynomial_free(pwqp);
914
915         return pwf;
916 }
917
918 int isl_qpolynomial_fold_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
919         __isl_keep isl_qpolynomial_fold *fold2)
920 {
921         int i;
922
923         if (!fold1 || !fold2)
924                 return -1;
925
926         if (fold1->n != fold2->n)
927                 return 0;
928
929         /* We probably want to sort the qps first... */
930         for (i = 0; i < fold1->n; ++i) {
931                 int eq = isl_qpolynomial_is_equal(fold1->qp[i], fold2->qp[i]);
932                 if (eq < 0 || !eq)
933                         return eq;
934         }
935
936         return 1;
937 }
938
939 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
940         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
941 {
942         isl_qpolynomial *qp;
943
944         if (!fold || !pnt)
945                 goto error;
946         isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, fold->dim), goto error);
947         isl_assert(pnt->dim->ctx,
948                 fold->type == isl_fold_max || fold->type == isl_fold_min,
949                 goto error);
950
951         if (fold->n == 0)
952                 qp = isl_qpolynomial_zero(isl_dim_copy(fold->dim));
953         else {
954                 int i;
955                 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
956                                                 isl_point_copy(pnt));
957                 for (i = 1; i < fold->n; ++i) {
958                         isl_qpolynomial *qp_i;
959                         qp_i = isl_qpolynomial_eval(
960                                             isl_qpolynomial_copy(fold->qp[i]),
961                                             isl_point_copy(pnt));
962                         if (fold->type == isl_fold_max)
963                                 qp = isl_qpolynomial_max_cst(qp, qp_i);
964                         else
965                                 qp = isl_qpolynomial_min_cst(qp, qp_i);
966                 }
967         }
968         isl_qpolynomial_fold_free(fold);
969         isl_point_free(pnt);
970
971         return qp;
972 error:
973         isl_qpolynomial_fold_free(fold);
974         isl_point_free(pnt);
975         return NULL;
976 }
977
978 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
979 {
980         int i;
981         size_t n = 0;
982
983         for (i = 0; i < pwf->n; ++i)
984                 n += pwf->p[i].fold->n;
985
986         return n;
987 }
988
989 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
990         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
991 {
992         int i;
993         isl_qpolynomial *opt;
994
995         if (!set || !fold)
996                 goto error;
997
998         if (fold->n == 0) {
999                 isl_dim *dim = isl_dim_copy(fold->dim);
1000                 isl_set_free(set);
1001                 isl_qpolynomial_fold_free(fold);
1002                 return isl_qpolynomial_zero(dim);
1003         }
1004
1005         opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1006                                                 isl_set_copy(set), max);
1007         for (i = 1; i < fold->n; ++i) {
1008                 isl_qpolynomial *opt_i;
1009                 opt_i = isl_qpolynomial_opt_on_domain(
1010                                 isl_qpolynomial_copy(fold->qp[i]),
1011                                 isl_set_copy(set), max);
1012                 if (max)
1013                         opt = isl_qpolynomial_max_cst(opt, opt_i);
1014                 else
1015                         opt = isl_qpolynomial_min_cst(opt, opt_i);
1016         }
1017
1018         isl_set_free(set);
1019         isl_qpolynomial_fold_free(fold);
1020
1021         return opt;
1022 error:
1023         isl_set_free(set);
1024         isl_qpolynomial_fold_free(fold);
1025         return NULL;
1026 }
1027
1028 /* Check whether for each quasi-polynomial in "fold2" there is
1029  * a quasi-polynomial in "fold1" that dominates it on "set".
1030  */
1031 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1032         __isl_keep isl_qpolynomial_fold *fold1,
1033         __isl_keep isl_qpolynomial_fold *fold2)
1034 {
1035         int i, j;
1036         int covers;
1037
1038         if (!set || !fold1 || !fold2)
1039                 return -1;
1040
1041         covers = fold1->type == isl_fold_max ? 1 : -1;
1042
1043         for (i = 0; i < fold2->n; ++i) {
1044                 for (j = 0; j < fold1->n; ++j) {
1045                         isl_qpolynomial *d;
1046                         int sgn;
1047
1048                         d = isl_qpolynomial_sub(
1049                                 isl_qpolynomial_copy(fold1->qp[j]),
1050                                 isl_qpolynomial_copy(fold2->qp[i]));
1051                         sgn = isl_qpolynomial_sign(set, d);
1052                         isl_qpolynomial_free(d);
1053                         if (sgn == covers)
1054                                 break;
1055                 }
1056                 if (j >= fold1->n)
1057                         return 0;
1058         }
1059
1060         return 1;
1061 }
1062
1063 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1064  * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1065  * that of pwf2.
1066  */
1067 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1068         __isl_keep isl_pw_qpolynomial_fold *pwf2)
1069 {
1070         int i, j;
1071         isl_set *dom1, *dom2;
1072         int is_subset;
1073
1074         if (!pwf1 || !pwf2)
1075                 return -1;
1076
1077         if (pwf2->n == 0)
1078                 return 1;
1079         if (pwf1->n == 0)
1080                 return 0;
1081
1082         dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1083         dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1084         is_subset = isl_set_is_subset(dom2, dom1);
1085         isl_set_free(dom1);
1086         isl_set_free(dom2);
1087
1088         if (is_subset < 0 || !is_subset)
1089                 return is_subset;
1090
1091         for (i = 0; i < pwf2->n; ++i) {
1092                 for (j = 0; j < pwf1->n; ++j) {
1093                         int is_empty;
1094                         isl_set *common;
1095                         int covers;
1096
1097                         common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1098                                                    isl_set_copy(pwf2->p[i].set));
1099                         is_empty = isl_set_is_empty(common);
1100                         if (is_empty < 0 || is_empty) {
1101                                 isl_set_free(common);
1102                                 if (is_empty < 0)
1103                                         return -1;
1104                                 continue;
1105                         }
1106                         covers = qpolynomial_fold_covers_on_domain(common,
1107                                         pwf1->p[j].fold, pwf2->p[i].fold);
1108                         isl_set_free(common);
1109                         if (covers < 0 || !covers)
1110                                 return covers;
1111                 }
1112         }
1113
1114         return 1;
1115 }
1116
1117 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph(
1118         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1119 {
1120         int i;
1121         isl_ctx *ctx;
1122
1123         if (!fold || !morph)
1124                 goto error;
1125
1126         ctx = fold->dim->ctx;
1127         isl_assert(ctx, isl_dim_equal(fold->dim, morph->dom->dim), goto error);
1128
1129         fold = isl_qpolynomial_fold_cow(fold);
1130         if (!fold)
1131                 goto error;
1132
1133         isl_dim_free(fold->dim);
1134         fold->dim = isl_dim_copy(morph->ran->dim);
1135         if (!fold->dim)
1136                 goto error;
1137
1138         for (i = 0; i < fold->n; ++i) {
1139                 fold->qp[i] = isl_qpolynomial_morph(fold->qp[i],
1140                                                 isl_morph_copy(morph));
1141                 if (!fold->qp[i])
1142                         goto error;
1143         }
1144
1145         isl_morph_free(morph);
1146
1147         return fold;
1148 error:
1149         isl_qpolynomial_fold_free(fold);
1150         isl_morph_free(morph);
1151         return NULL;
1152 }
1153
1154 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1155 {
1156         if (!fold)
1157                 return isl_fold_list;
1158         return fold->type;
1159 }
1160
1161 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1162         __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1163 {
1164         if (!upwf)
1165                 return isl_fold_list;
1166         return upwf->type;
1167 }
1168
1169 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1170         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
1171 {
1172         int i;
1173         isl_ctx *ctx;
1174
1175         if (!fold || !dim)
1176                 goto error;
1177
1178         if (isl_dim_equal(fold->dim, dim)) {
1179                 isl_dim_free(dim);
1180                 return fold;
1181         }
1182
1183         fold = isl_qpolynomial_fold_cow(fold);
1184         if (!fold)
1185                 goto error;
1186
1187         isl_dim_free(fold->dim);
1188         fold->dim = isl_dim_copy(dim);
1189         if (!fold->dim)
1190                 goto error;
1191
1192         for (i = 0; i < fold->n; ++i) {
1193                 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1194                                                 isl_dim_copy(dim));
1195                 if (!fold->qp[i])
1196                         goto error;
1197         }
1198
1199         isl_dim_free(dim);
1200
1201         return fold;
1202 error:
1203         isl_qpolynomial_fold_free(fold);
1204         isl_dim_free(dim);
1205         return NULL;
1206 }
1207
1208 int isl_qpolynomial_fold_foreach_qpolynomial(
1209         __isl_keep isl_qpolynomial_fold *fold,
1210         int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1211 {
1212         int i;
1213
1214         if (!fold)
1215                 return -1;
1216
1217         for (i = 0; i < fold->n; ++i)
1218                 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1219                         return -1;
1220
1221         return 0;
1222 }
1223
1224 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1225         __isl_take isl_qpolynomial_fold *fold,
1226         enum isl_dim_type dst_type, unsigned dst_pos,
1227         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1228 {
1229         int i;
1230
1231         if (n == 0)
1232                 return fold;
1233
1234         fold = isl_qpolynomial_fold_cow(fold);
1235         if (!fold)
1236                 return NULL;
1237
1238         fold->dim = isl_dim_move(fold->dim, dst_type, dst_pos,
1239                                                 src_type, src_pos, n);
1240         if (!fold->dim)
1241                 goto error;
1242
1243         for (i = 0; i < fold->n; ++i) {
1244                 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1245                                 dst_type, dst_pos, src_type, src_pos, n);
1246                 if (!fold->qp[i])
1247                         goto error;
1248         }
1249
1250         return fold;
1251 error:
1252         isl_qpolynomial_fold_free(fold);
1253         return NULL;
1254 }
1255
1256 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1257  * in fold->qp[k] by subs[i].
1258  */
1259 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1260         __isl_take isl_qpolynomial_fold *fold,
1261         enum isl_dim_type type, unsigned first, unsigned n,
1262         __isl_keep isl_qpolynomial **subs)
1263 {
1264         int i;
1265
1266         if (n == 0)
1267                 return fold;
1268
1269         fold = isl_qpolynomial_fold_cow(fold);
1270         if (!fold)
1271                 return NULL;
1272
1273         for (i = 0; i < fold->n; ++i) {
1274                 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1275                                 type, first, n, subs);
1276                 if (!fold->qp[i])
1277                         goto error;
1278         }
1279
1280         return fold;
1281 error:
1282         isl_qpolynomial_fold_free(fold);
1283         return NULL;
1284 }
1285
1286 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1287 {
1288         isl_ctx *ctx;
1289         isl_pw_qpolynomial_fold *pwf;
1290         isl_union_pw_qpolynomial_fold **upwf;
1291         uint32_t hash;
1292         struct isl_hash_table_entry *entry;
1293
1294         upwf = (isl_union_pw_qpolynomial_fold **)user;
1295
1296         ctx = pwqp->dim->ctx;
1297         hash = isl_dim_get_hash(pwqp->dim);
1298         entry = isl_hash_table_find(ctx, &(*upwf)->table,
1299                                      hash, &has_dim, pwqp->dim, 1);
1300         if (!entry)
1301                 goto error;
1302
1303         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1304         if (!entry->data)
1305                 entry->data = pwf;
1306         else {
1307                 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1308                 if (!entry->data)
1309                         return -1;
1310                 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1311                         isl_pw_qpolynomial_fold_free(entry->data);
1312                         isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1313                 }
1314         }
1315
1316         return 0;
1317 error:
1318         isl_pw_qpolynomial_free(pwqp);
1319         return -1;
1320 }
1321
1322 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1323         __isl_take isl_union_pw_qpolynomial_fold *upwf,
1324         __isl_take isl_union_pw_qpolynomial *upwqp)
1325 {
1326         upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1327                                 isl_union_pw_qpolynomial_get_dim(upwqp));
1328         upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1329                                 isl_union_pw_qpolynomial_fold_get_dim(upwf));
1330
1331         upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1332         if (!upwf || !upwqp)
1333                 goto error;
1334
1335         if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1336                                                          &upwf) < 0)
1337                 goto error;
1338
1339         isl_union_pw_qpolynomial_free(upwqp);
1340
1341         return upwf;
1342 error:
1343         isl_union_pw_qpolynomial_fold_free(upwf);
1344         isl_union_pw_qpolynomial_free(upwqp);
1345         return NULL;
1346 }
1347
1348 static int compatible_range(__isl_keep isl_dim *dim1, __isl_keep isl_dim *dim2)
1349 {
1350         int m;
1351         m = isl_dim_match(dim1, isl_dim_param, dim2, isl_dim_param);
1352         if (m < 0 || !m)
1353                 return m;
1354         return isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_set);
1355 }
1356
1357 /* Compute the intersection of the range of the map and the domain
1358  * of the piecewise quasipolynomial reduction and then compute a bound
1359  * on the associated quasipolynomial reduction over all elements
1360  * in this intersection.
1361  *
1362  * We first introduce some unconstrained dimensions in the
1363  * piecewise quasipolynomial, intersect the resulting domain
1364  * with the wrapped map and the compute the sum.
1365  */
1366 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1367         __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1368         int *tight)
1369 {
1370         isl_ctx *ctx;
1371         isl_set *dom;
1372         isl_dim *map_dim;
1373         isl_dim *pwf_dim;
1374         unsigned n_in;
1375         int ok;
1376
1377         ctx = isl_map_get_ctx(map);
1378         if (!ctx)
1379                 goto error;
1380
1381         map_dim = isl_map_get_dim(map);
1382         pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
1383         ok = compatible_range(map_dim, pwf_dim);
1384         isl_dim_free(map_dim);
1385         isl_dim_free(pwf_dim);
1386         if (!ok)
1387                 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1388                         goto error);
1389
1390         n_in = isl_map_dim(map, isl_dim_in);
1391         pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_set, 0, n_in);
1392
1393         dom = isl_map_wrap(map);
1394         pwf = isl_pw_qpolynomial_fold_reset_dim(pwf, isl_set_get_dim(dom));
1395
1396         pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1397         pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1398         
1399         return pwf;
1400 error:
1401         isl_map_free(map);
1402         isl_pw_qpolynomial_fold_free(pwf);
1403         return NULL;
1404 }
1405
1406 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1407         __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1408         int *tight)
1409 {
1410         isl_map *map;
1411
1412         map = isl_map_from_range(set);
1413         return isl_map_apply_pw_qpolynomial_fold(map, pwf, tight);
1414 }
1415
1416 struct isl_apply_fold_data {
1417         isl_union_pw_qpolynomial_fold *upwf;
1418         isl_union_pw_qpolynomial_fold *res;
1419         isl_map *map;
1420         int tight;
1421 };
1422
1423 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1424         void *user)
1425 {
1426         isl_dim *map_dim;
1427         isl_dim *pwf_dim;
1428         struct isl_apply_fold_data *data = user;
1429         int ok;
1430
1431         map_dim = isl_map_get_dim(data->map);
1432         pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
1433         ok = compatible_range(map_dim, pwf_dim);
1434         isl_dim_free(map_dim);
1435         isl_dim_free(pwf_dim);
1436
1437         if (ok) {
1438                 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1439                                     pwf, data->tight ? &data->tight : NULL);
1440                 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1441                                                         data->res, pwf);
1442         } else
1443                 isl_pw_qpolynomial_fold_free(pwf);
1444
1445         return 0;
1446 }
1447
1448 static int map_apply(__isl_take isl_map *map, void *user)
1449 {
1450         struct isl_apply_fold_data *data = user;
1451         int r;
1452
1453         data->map = map;
1454         r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1455                                 data->upwf, &pw_qpolynomial_fold_apply, data);
1456
1457         isl_map_free(map);
1458         return r;
1459 }
1460
1461 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1462         __isl_take isl_union_map *umap,
1463         __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1464 {
1465         isl_dim *dim;
1466         enum isl_fold type;
1467         struct isl_apply_fold_data data;
1468
1469         upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1470                                 isl_union_map_get_dim(umap));
1471         umap = isl_union_map_align_params(umap,
1472                                 isl_union_pw_qpolynomial_fold_get_dim(upwf));
1473
1474         data.upwf = upwf;
1475         data.tight = tight ? 1 : 0;
1476         dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
1477         type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1478         data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1479         if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1480                 goto error;
1481
1482         isl_union_map_free(umap);
1483         isl_union_pw_qpolynomial_fold_free(upwf);
1484
1485         if (tight)
1486                 *tight = data.tight;
1487
1488         return data.res;
1489 error:
1490         isl_union_map_free(umap);
1491         isl_union_pw_qpolynomial_fold_free(upwf);
1492         isl_union_pw_qpolynomial_fold_free(data.res);
1493         return NULL;
1494 }
1495
1496 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1497         __isl_take isl_union_set *uset,
1498         __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1499 {
1500         return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1501 }
1502
1503 /* Reorder the dimension of "fold" according to the given reordering.
1504  */
1505 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign(
1506         __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1507 {
1508         int i;
1509
1510         fold = isl_qpolynomial_fold_cow(fold);
1511         if (!fold || !r)
1512                 goto error;
1513
1514         for (i = 0; i < fold->n; ++i) {
1515                 fold->qp[i] = isl_qpolynomial_realign(fold->qp[i],
1516                                                     isl_reordering_copy(r));
1517                 if (!fold->qp[i])
1518                         goto error;
1519         }
1520
1521         fold = isl_qpolynomial_fold_reset_dim(fold, isl_dim_copy(r->dim));
1522
1523         isl_reordering_free(r);
1524
1525         return fold;
1526 error:
1527         isl_qpolynomial_fold_free(fold);
1528         isl_reordering_free(r);
1529         return NULL;
1530 }
1531
1532 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1533         __isl_take isl_qpolynomial_fold *fold, isl_int v)
1534 {
1535         int i;
1536
1537         if (isl_int_is_one(v))
1538                 return fold;
1539         if (fold && isl_int_is_zero(v)) {
1540                 isl_qpolynomial_fold *zero;
1541                 isl_dim *dim = isl_dim_copy(fold->dim);
1542                 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1543                 isl_qpolynomial_fold_free(fold);
1544                 return zero;
1545         }
1546
1547         fold = isl_qpolynomial_fold_cow(fold);
1548         if (!fold)
1549                 return NULL;
1550
1551         if (isl_int_is_neg(v))
1552                 fold->type = isl_fold_type_negate(fold->type);
1553         for (i = 0; i < fold->n; ++i) {
1554                 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1555                 if (!fold->qp[i])
1556                         goto error;
1557         }
1558
1559         return fold;
1560 error:
1561         isl_qpolynomial_fold_free(fold);
1562         return NULL;
1563 }