2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_union_map_private.h>
12 #include <isl_polynomial_private.h>
13 #include <isl_point_private.h>
14 #include <isl_dim_private.h>
15 #include <isl_map_private.h>
18 #include <isl_mat_private.h>
20 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
21 enum isl_fold type, __isl_take isl_dim *dim, int n)
23 isl_qpolynomial_fold *fold;
28 isl_assert(dim->ctx, n >= 0, goto error);
29 fold = isl_calloc(dim->ctx, struct isl_qpolynomial_fold,
30 sizeof(struct isl_qpolynomial_fold) +
31 (n - 1) * sizeof(struct isl_qpolynomial *));
47 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_dim(
48 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
52 fold = isl_qpolynomial_fold_cow(fold);
56 for (i = 0; i < fold->n; ++i) {
57 fold->qp[i] = isl_qpolynomial_reset_dim(fold->qp[i],
63 isl_dim_free(fold->dim);
68 isl_qpolynomial_fold_free(fold);
73 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
74 enum isl_dim_type type, unsigned first, unsigned n)
80 if (fold->n == 0 || n == 0)
83 for (i = 0; i < fold->n; ++i) {
84 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
86 if (involves < 0 || involves)
92 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
93 __isl_take isl_qpolynomial_fold *fold,
94 enum isl_dim_type type, unsigned first, unsigned n)
103 fold = isl_qpolynomial_fold_cow(fold);
106 fold->dim = isl_dim_drop(fold->dim, type, first, n);
110 for (i = 0; i < fold->n; ++i) {
111 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
119 isl_qpolynomial_fold_free(fold);
123 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
124 __isl_take isl_qpolynomial_fold *fold,
125 enum isl_dim_type type, unsigned first, unsigned n)
131 if (n == 0 && !isl_dim_is_named_or_nested(fold->dim, type))
134 fold = isl_qpolynomial_fold_cow(fold);
137 fold->dim = isl_dim_insert(fold->dim, type, first, n);
141 for (i = 0; i < fold->n; ++i) {
142 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
150 isl_qpolynomial_fold_free(fold);
154 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
156 struct isl_upoly_cst *cst;
158 cst = isl_upoly_as_cst(qp->upoly);
162 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
165 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
166 __isl_keep isl_qpolynomial *qp)
168 enum isl_lp_result res;
173 aff = isl_qpolynomial_extract_affine(qp);
179 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
181 if (res == isl_lp_error)
183 if (res == isl_lp_empty ||
184 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
189 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
191 if (res == isl_lp_ok && !isl_int_is_pos(opt))
200 /* Determine, if possible, the sign of the quasipolynomial "qp" on
203 * If qp is a constant, then the problem is trivial.
204 * If qp is linear, then we check if the minimum of the corresponding
205 * affine constraint is non-negative or if the maximum is non-positive.
207 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
208 * in "set". If so, we write qp(v,v') as
210 * q(v,v') * (v - l) + r(v')
212 * if q(v,v') and r(v') have the same known sign, then the original
213 * quasipolynomial has the same sign as well.
220 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
221 __isl_keep isl_qpolynomial *qp)
226 struct isl_upoly_rec *rec;
229 enum isl_lp_result res;
232 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
236 return isl_qpolynomial_cst_sign(qp);
238 is = isl_qpolynomial_is_affine(qp);
242 return isl_qpolynomial_aff_sign(set, qp);
244 if (qp->div->n_row > 0)
247 rec = isl_upoly_as_rec(qp->upoly);
251 d = isl_dim_total(qp->dim);
252 v = isl_vec_alloc(set->ctx, 2 + d);
256 isl_seq_clr(v->el + 1, 1 + d);
257 isl_int_set_si(v->el[0], 1);
258 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
262 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
263 if (res == isl_lp_ok) {
264 isl_qpolynomial *min;
265 isl_qpolynomial *base;
266 isl_qpolynomial *r, *q;
269 min = isl_qpolynomial_cst(isl_dim_copy(qp->dim), l);
270 base = isl_qpolynomial_pow(isl_dim_copy(qp->dim),
273 r = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
274 isl_upoly_copy(rec->p[rec->n - 1]));
275 q = isl_qpolynomial_copy(r);
277 for (i = rec->n - 2; i >= 0; --i) {
278 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
279 t = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
280 isl_upoly_copy(rec->p[i]));
281 r = isl_qpolynomial_add(r, t);
284 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
285 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
288 if (isl_qpolynomial_is_zero(q))
289 sgn = isl_qpolynomial_sign(set, r);
290 else if (isl_qpolynomial_is_zero(r))
291 sgn = isl_qpolynomial_sign(set, q);
294 sgn_r = isl_qpolynomial_sign(set, r);
295 sgn_q = isl_qpolynomial_sign(set, q);
300 isl_qpolynomial_free(min);
301 isl_qpolynomial_free(base);
302 isl_qpolynomial_free(q);
303 isl_qpolynomial_free(r);
313 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
314 __isl_keep isl_set *set,
315 __isl_take isl_qpolynomial_fold *fold1,
316 __isl_take isl_qpolynomial_fold *fold2)
320 struct isl_qpolynomial_fold *res = NULL;
323 if (!fold1 || !fold2)
326 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
327 isl_assert(fold1->dim->ctx, isl_dim_equal(fold1->dim, fold2->dim),
330 better = fold1->type == isl_fold_max ? -1 : 1;
332 if (isl_qpolynomial_fold_is_empty(fold1)) {
333 isl_qpolynomial_fold_free(fold1);
337 if (isl_qpolynomial_fold_is_empty(fold2)) {
338 isl_qpolynomial_fold_free(fold2);
342 res = qpolynomial_fold_alloc(fold1->type, isl_dim_copy(fold1->dim),
343 fold1->n + fold2->n);
347 for (i = 0; i < fold1->n; ++i) {
348 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
349 if (!res->qp[res->n])
355 for (i = 0; i < fold2->n; ++i) {
356 for (j = n1 - 1; j >= 0; --j) {
359 d = isl_qpolynomial_sub(
360 isl_qpolynomial_copy(res->qp[j]),
361 isl_qpolynomial_copy(fold2->qp[i]));
362 sgn = isl_qpolynomial_sign(set, d);
363 isl_qpolynomial_free(d);
368 isl_qpolynomial_free(res->qp[j]);
370 res->qp[j] = res->qp[n1 - 1];
372 if (n1 != res->n - 1)
373 res->qp[n1] = res->qp[res->n - 1];
378 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
379 if (!res->qp[res->n])
384 isl_qpolynomial_fold_free(fold1);
385 isl_qpolynomial_fold_free(fold2);
389 isl_qpolynomial_fold_free(res);
390 isl_qpolynomial_fold_free(fold1);
391 isl_qpolynomial_fold_free(fold2);
395 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
396 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
403 if (isl_qpolynomial_is_zero(qp)) {
404 isl_qpolynomial_free(qp);
408 fold = isl_qpolynomial_fold_cow(fold);
412 for (i = 0; i < fold->n; ++i) {
413 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
414 isl_qpolynomial_copy(qp));
419 isl_qpolynomial_free(qp);
422 isl_qpolynomial_fold_free(fold);
423 isl_qpolynomial_free(qp);
427 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
428 __isl_keep isl_set *dom,
429 __isl_take isl_qpolynomial_fold *fold1,
430 __isl_take isl_qpolynomial_fold *fold2)
433 isl_qpolynomial_fold *res = NULL;
435 if (!fold1 || !fold2)
438 if (isl_qpolynomial_fold_is_empty(fold1)) {
439 isl_qpolynomial_fold_free(fold1);
443 if (isl_qpolynomial_fold_is_empty(fold2)) {
444 isl_qpolynomial_fold_free(fold2);
448 if (fold1->n == 1 && fold2->n != 1)
449 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
452 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
453 isl_qpolynomial_copy(fold2->qp[0]));
454 isl_qpolynomial_fold_free(fold2);
458 res = isl_qpolynomial_fold_add_qpolynomial(
459 isl_qpolynomial_fold_copy(fold1),
460 isl_qpolynomial_copy(fold2->qp[0]));
462 for (i = 1; i < fold2->n; ++i) {
463 isl_qpolynomial_fold *res_i;
464 res_i = isl_qpolynomial_fold_add_qpolynomial(
465 isl_qpolynomial_fold_copy(fold1),
466 isl_qpolynomial_copy(fold2->qp[i]));
467 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
470 isl_qpolynomial_fold_free(fold1);
471 isl_qpolynomial_fold_free(fold2);
474 isl_qpolynomial_fold_free(res);
475 isl_qpolynomial_fold_free(fold1);
476 isl_qpolynomial_fold_free(fold2);
480 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
481 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
488 fold = isl_qpolynomial_fold_cow(fold);
492 for (i = 0; i < fold->n; ++i) {
493 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
494 isl_basic_set_copy(eq));
499 isl_basic_set_free(eq);
502 isl_basic_set_free(eq);
503 isl_qpolynomial_fold_free(fold);
510 #define PW isl_pw_qpolynomial_fold
512 #define EL isl_qpolynomial_fold
514 #define IS_ZERO is_empty
518 #include <isl_pw_templ.c>
521 #define UNION isl_union_pw_qpolynomial_fold
523 #define PART isl_pw_qpolynomial_fold
525 #define PARTS pw_qpolynomial_fold
527 #include <isl_union_templ.c>
529 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
530 __isl_take isl_dim *dim)
532 return qpolynomial_fold_alloc(type, dim, 0);
535 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
536 enum isl_fold type, __isl_take isl_qpolynomial *qp)
538 isl_qpolynomial_fold *fold;
543 fold = qpolynomial_fold_alloc(type, isl_dim_copy(qp->dim), 1);
552 isl_qpolynomial_fold_free(fold);
553 isl_qpolynomial_free(qp);
557 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
558 __isl_keep isl_qpolynomial_fold *fold)
567 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
568 __isl_keep isl_qpolynomial_fold *fold)
571 isl_qpolynomial_fold *dup;
575 dup = qpolynomial_fold_alloc(fold->type,
576 isl_dim_copy(fold->dim), fold->n);
581 for (i = 0; i < fold->n; ++i) {
582 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
589 isl_qpolynomial_fold_free(dup);
593 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
594 __isl_take isl_qpolynomial_fold *fold)
602 return isl_qpolynomial_fold_dup(fold);
605 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
614 for (i = 0; i < fold->n; ++i)
615 isl_qpolynomial_free(fold->qp[i]);
616 isl_dim_free(fold->dim);
620 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
628 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
629 __isl_take isl_qpolynomial_fold *fold1,
630 __isl_take isl_qpolynomial_fold *fold2)
633 struct isl_qpolynomial_fold *res = NULL;
635 if (!fold1 || !fold2)
638 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
639 isl_assert(fold1->dim->ctx, isl_dim_equal(fold1->dim, fold2->dim),
642 if (isl_qpolynomial_fold_is_empty(fold1)) {
643 isl_qpolynomial_fold_free(fold1);
647 if (isl_qpolynomial_fold_is_empty(fold2)) {
648 isl_qpolynomial_fold_free(fold2);
652 res = qpolynomial_fold_alloc(fold1->type, isl_dim_copy(fold1->dim),
653 fold1->n + fold2->n);
657 for (i = 0; i < fold1->n; ++i) {
658 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
659 if (!res->qp[res->n])
664 for (i = 0; i < fold2->n; ++i) {
665 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
666 if (!res->qp[res->n])
671 isl_qpolynomial_fold_free(fold1);
672 isl_qpolynomial_fold_free(fold2);
676 isl_qpolynomial_fold_free(res);
677 isl_qpolynomial_fold_free(fold1);
678 isl_qpolynomial_fold_free(fold2);
682 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
683 __isl_take isl_pw_qpolynomial_fold *pw1,
684 __isl_take isl_pw_qpolynomial_fold *pw2)
687 struct isl_pw_qpolynomial_fold *res;
693 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
695 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
696 isl_pw_qpolynomial_fold_free(pw1);
700 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
701 isl_pw_qpolynomial_fold_free(pw2);
705 if (pw1->type != pw2->type)
706 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
709 n = (pw1->n + 1) * (pw2->n + 1);
710 res = isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pw1->dim),
713 for (i = 0; i < pw1->n; ++i) {
714 set = isl_set_copy(pw1->p[i].set);
715 for (j = 0; j < pw2->n; ++j) {
716 struct isl_set *common;
717 isl_qpolynomial_fold *sum;
718 set = isl_set_subtract(set,
719 isl_set_copy(pw2->p[j].set));
720 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
721 isl_set_copy(pw2->p[j].set));
722 if (isl_set_fast_is_empty(common)) {
723 isl_set_free(common);
727 sum = isl_qpolynomial_fold_fold_on_domain(common,
728 isl_qpolynomial_fold_copy(pw1->p[i].fold),
729 isl_qpolynomial_fold_copy(pw2->p[j].fold));
731 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
733 res = isl_pw_qpolynomial_fold_add_piece(res, set,
734 isl_qpolynomial_fold_copy(pw1->p[i].fold));
737 for (j = 0; j < pw2->n; ++j) {
738 set = isl_set_copy(pw2->p[j].set);
739 for (i = 0; i < pw1->n; ++i)
740 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
741 res = isl_pw_qpolynomial_fold_add_piece(res, set,
742 isl_qpolynomial_fold_copy(pw2->p[j].fold));
745 isl_pw_qpolynomial_fold_free(pw1);
746 isl_pw_qpolynomial_fold_free(pw2);
750 isl_pw_qpolynomial_fold_free(pw1);
751 isl_pw_qpolynomial_fold_free(pw2);
755 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
756 __isl_take isl_union_pw_qpolynomial_fold *u,
757 __isl_take isl_pw_qpolynomial_fold *part)
760 struct isl_hash_table_entry *entry;
762 u = isl_union_pw_qpolynomial_fold_cow(u);
767 isl_assert(u->dim->ctx, isl_dim_match(part->dim, isl_dim_param, u->dim,
768 isl_dim_param), goto error);
770 hash = isl_dim_get_hash(part->dim);
771 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
772 &has_dim, part->dim, 1);
779 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
780 isl_pw_qpolynomial_fold_copy(part));
783 isl_pw_qpolynomial_fold_free(part);
788 isl_pw_qpolynomial_fold_free(part);
789 isl_union_pw_qpolynomial_fold_free(u);
793 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
795 isl_union_pw_qpolynomial_fold **u;
796 u = (isl_union_pw_qpolynomial_fold **)user;
798 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
803 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
804 __isl_take isl_union_pw_qpolynomial_fold *u1,
805 __isl_take isl_union_pw_qpolynomial_fold *u2)
807 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
812 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
813 &fold_part, &u1) < 0)
816 isl_union_pw_qpolynomial_fold_free(u2);
820 isl_union_pw_qpolynomial_fold_free(u1);
821 isl_union_pw_qpolynomial_fold_free(u2);
825 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
826 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
829 isl_pw_qpolynomial_fold *pwf;
834 pwf = isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp->dim), type,
837 for (i = 0; i < pwqp->n; ++i)
838 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
839 isl_set_copy(pwqp->p[i].set),
840 isl_qpolynomial_fold_alloc(type,
841 isl_qpolynomial_copy(pwqp->p[i].qp)));
843 isl_pw_qpolynomial_free(pwqp);
848 int isl_qpolynomial_fold_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
849 __isl_keep isl_qpolynomial_fold *fold2)
853 if (!fold1 || !fold2)
856 if (fold1->n != fold2->n)
859 /* We probably want to sort the qps first... */
860 for (i = 0; i < fold1->n; ++i) {
861 int eq = isl_qpolynomial_is_equal(fold1->qp[i], fold2->qp[i]);
869 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
870 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
876 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, fold->dim), goto error);
877 isl_assert(pnt->dim->ctx,
878 fold->type == isl_fold_max || fold->type == isl_fold_min,
882 qp = isl_qpolynomial_zero(isl_dim_copy(fold->dim));
885 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
886 isl_point_copy(pnt));
887 for (i = 1; i < fold->n; ++i) {
888 isl_qpolynomial *qp_i;
889 qp_i = isl_qpolynomial_eval(
890 isl_qpolynomial_copy(fold->qp[i]),
891 isl_point_copy(pnt));
892 if (fold->type == isl_fold_max)
893 qp = isl_qpolynomial_max_cst(qp, qp_i);
895 qp = isl_qpolynomial_min_cst(qp, qp_i);
898 isl_qpolynomial_fold_free(fold);
903 isl_qpolynomial_fold_free(fold);
908 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
913 for (i = 0; i < pwf->n; ++i)
914 n += pwf->p[i].fold->n;
919 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
920 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
923 isl_qpolynomial *opt;
929 isl_dim *dim = isl_dim_copy(fold->dim);
931 isl_qpolynomial_fold_free(fold);
932 return isl_qpolynomial_zero(dim);
935 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
936 isl_set_copy(set), max);
937 for (i = 1; i < fold->n; ++i) {
938 isl_qpolynomial *opt_i;
939 opt_i = isl_qpolynomial_opt_on_domain(
940 isl_qpolynomial_copy(fold->qp[i]),
941 isl_set_copy(set), max);
943 opt = isl_qpolynomial_max_cst(opt, opt_i);
945 opt = isl_qpolynomial_min_cst(opt, opt_i);
949 isl_qpolynomial_fold_free(fold);
954 isl_qpolynomial_fold_free(fold);
958 /* Check whether for each quasi-polynomial in "fold2" there is
959 * a quasi-polynomial in "fold1" that dominates it on "set".
961 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
962 __isl_keep isl_qpolynomial_fold *fold1,
963 __isl_keep isl_qpolynomial_fold *fold2)
968 if (!set || !fold1 || !fold2)
971 covers = fold1->type == isl_fold_max ? 1 : -1;
973 for (i = 0; i < fold2->n; ++i) {
974 for (j = 0; j < fold1->n; ++j) {
978 d = isl_qpolynomial_sub(
979 isl_qpolynomial_copy(fold1->qp[j]),
980 isl_qpolynomial_copy(fold2->qp[i]));
981 sgn = isl_qpolynomial_sign(set, d);
982 isl_qpolynomial_free(d);
993 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
994 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
997 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
998 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1001 isl_set *dom1, *dom2;
1012 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1013 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1014 is_subset = isl_set_is_subset(dom2, dom1);
1018 if (is_subset < 0 || !is_subset)
1021 for (i = 0; i < pwf2->n; ++i) {
1022 for (j = 0; j < pwf1->n; ++j) {
1027 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1028 isl_set_copy(pwf2->p[i].set));
1029 is_empty = isl_set_is_empty(common);
1030 if (is_empty < 0 || is_empty) {
1031 isl_set_free(common);
1036 covers = qpolynomial_fold_covers_on_domain(common,
1037 pwf1->p[j].fold, pwf2->p[i].fold);
1038 isl_set_free(common);
1039 if (covers < 0 || !covers)
1047 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph(
1048 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1053 if (!fold || !morph)
1056 ctx = fold->dim->ctx;
1057 isl_assert(ctx, isl_dim_equal(fold->dim, morph->dom->dim), goto error);
1059 fold = isl_qpolynomial_fold_cow(fold);
1063 isl_dim_free(fold->dim);
1064 fold->dim = isl_dim_copy(morph->ran->dim);
1068 for (i = 0; i < fold->n; ++i) {
1069 fold->qp[i] = isl_qpolynomial_morph(fold->qp[i],
1070 isl_morph_copy(morph));
1075 isl_morph_free(morph);
1079 isl_qpolynomial_fold_free(fold);
1080 isl_morph_free(morph);
1084 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1087 return isl_fold_list;
1091 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1092 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1095 return isl_fold_list;
1099 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1100 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
1108 if (isl_dim_equal(fold->dim, dim)) {
1113 fold = isl_qpolynomial_fold_cow(fold);
1117 isl_dim_free(fold->dim);
1118 fold->dim = isl_dim_copy(dim);
1122 for (i = 0; i < fold->n; ++i) {
1123 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1133 isl_qpolynomial_fold_free(fold);
1138 int isl_qpolynomial_fold_foreach_qpolynomial(
1139 __isl_keep isl_qpolynomial_fold *fold,
1140 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1147 for (i = 0; i < fold->n; ++i)
1148 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1154 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1155 __isl_take isl_qpolynomial_fold *fold,
1156 enum isl_dim_type dst_type, unsigned dst_pos,
1157 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1164 fold = isl_qpolynomial_fold_cow(fold);
1168 fold->dim = isl_dim_move(fold->dim, dst_type, dst_pos,
1169 src_type, src_pos, n);
1173 for (i = 0; i < fold->n; ++i) {
1174 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1175 dst_type, dst_pos, src_type, src_pos, n);
1182 isl_qpolynomial_fold_free(fold);
1186 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1187 * in fold->qp[k] by subs[i].
1189 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1190 __isl_take isl_qpolynomial_fold *fold,
1191 enum isl_dim_type type, unsigned first, unsigned n,
1192 __isl_keep isl_qpolynomial **subs)
1199 fold = isl_qpolynomial_fold_cow(fold);
1203 for (i = 0; i < fold->n; ++i) {
1204 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1205 type, first, n, subs);
1212 isl_qpolynomial_fold_free(fold);
1216 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1219 isl_pw_qpolynomial_fold *pwf;
1220 isl_union_pw_qpolynomial_fold **upwf;
1222 struct isl_hash_table_entry *entry;
1224 upwf = (isl_union_pw_qpolynomial_fold **)user;
1226 ctx = pwqp->dim->ctx;
1227 hash = isl_dim_get_hash(pwqp->dim);
1228 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1229 hash, &has_dim, pwqp->dim, 1);
1233 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1237 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1240 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1241 isl_pw_qpolynomial_fold_free(entry->data);
1242 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1248 isl_pw_qpolynomial_free(pwqp);
1252 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1253 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1254 __isl_take isl_union_pw_qpolynomial *upwqp)
1256 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1257 isl_union_pw_qpolynomial_get_dim(upwqp));
1258 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1259 isl_union_pw_qpolynomial_fold_get_dim(upwf));
1261 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1262 if (!upwf || !upwqp)
1265 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1269 isl_union_pw_qpolynomial_free(upwqp);
1273 isl_union_pw_qpolynomial_fold_free(upwf);
1274 isl_union_pw_qpolynomial_free(upwqp);
1278 static int compatible_range(__isl_keep isl_dim *dim1, __isl_keep isl_dim *dim2)
1281 m = isl_dim_match(dim1, isl_dim_param, dim2, isl_dim_param);
1284 return isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_set);
1287 /* Compute the intersection of the range of the map and the domain
1288 * of the piecewise quasipolynomial reduction and then compute a bound
1289 * on the associated quasipolynomial reduction over all elements
1290 * in this intersection.
1292 * We first introduce some unconstrained dimensions in the
1293 * piecewise quasipolynomial, intersect the resulting domain
1294 * with the wrapped map and the compute the sum.
1296 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1297 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1307 ctx = isl_map_get_ctx(map);
1311 map_dim = isl_map_get_dim(map);
1312 pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
1313 ok = compatible_range(map_dim, pwf_dim);
1314 isl_dim_free(map_dim);
1315 isl_dim_free(pwf_dim);
1317 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1320 n_in = isl_map_dim(map, isl_dim_in);
1321 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_set, 0, n_in);
1323 dom = isl_map_wrap(map);
1324 pwf = isl_pw_qpolynomial_fold_reset_dim(pwf, isl_set_get_dim(dom));
1326 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1327 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1332 isl_pw_qpolynomial_fold_free(pwf);
1336 struct isl_apply_fold_data {
1337 isl_union_pw_qpolynomial_fold *upwf;
1338 isl_union_pw_qpolynomial_fold *res;
1343 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1348 struct isl_apply_fold_data *data = user;
1351 map_dim = isl_map_get_dim(data->map);
1352 pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
1353 ok = compatible_range(map_dim, pwf_dim);
1354 isl_dim_free(map_dim);
1355 isl_dim_free(pwf_dim);
1358 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1359 pwf, data->tight ? &data->tight : NULL);
1360 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1363 isl_pw_qpolynomial_fold_free(pwf);
1368 static int map_apply(__isl_take isl_map *map, void *user)
1370 struct isl_apply_fold_data *data = user;
1374 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1375 data->upwf, &pw_qpolynomial_fold_apply, data);
1381 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1382 __isl_take isl_union_map *umap,
1383 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1387 struct isl_apply_fold_data data;
1389 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1390 isl_union_map_get_dim(umap));
1391 umap = isl_union_map_align_params(umap,
1392 isl_union_pw_qpolynomial_fold_get_dim(upwf));
1395 data.tight = tight ? 1 : 0;
1396 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
1397 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1398 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1399 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1402 isl_union_map_free(umap);
1403 isl_union_pw_qpolynomial_fold_free(upwf);
1406 *tight = data.tight;
1410 isl_union_map_free(umap);
1411 isl_union_pw_qpolynomial_fold_free(upwf);
1412 isl_union_pw_qpolynomial_fold_free(data.res);
1416 /* Reorder the dimension of "fold" according to the given reordering.
1418 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign(
1419 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1423 fold = isl_qpolynomial_fold_cow(fold);
1427 for (i = 0; i < fold->n; ++i) {
1428 fold->qp[i] = isl_qpolynomial_realign(fold->qp[i],
1429 isl_reordering_copy(r));
1434 fold = isl_qpolynomial_fold_reset_dim(fold, isl_dim_copy(r->dim));
1436 isl_reordering_free(r);
1440 isl_qpolynomial_fold_free(fold);
1441 isl_reordering_free(r);