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