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_set_dim_name(
93 __isl_take isl_qpolynomial_fold *fold,
94 enum isl_dim_type type, unsigned pos, const char *s)
98 fold = isl_qpolynomial_fold_cow(fold);
101 fold->dim = isl_dim_set_name(fold->dim, type, pos, s);
105 for (i = 0; i < fold->n; ++i) {
106 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
114 isl_qpolynomial_fold_free(fold);
118 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_drop_dims(
119 __isl_take isl_qpolynomial_fold *fold,
120 enum isl_dim_type type, unsigned first, unsigned n)
129 fold = isl_qpolynomial_fold_cow(fold);
132 fold->dim = isl_dim_drop(fold->dim, type, first, n);
136 for (i = 0; i < fold->n; ++i) {
137 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
145 isl_qpolynomial_fold_free(fold);
149 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_insert_dims(
150 __isl_take isl_qpolynomial_fold *fold,
151 enum isl_dim_type type, unsigned first, unsigned n)
157 if (n == 0 && !isl_dim_is_named_or_nested(fold->dim, type))
160 fold = isl_qpolynomial_fold_cow(fold);
163 fold->dim = isl_dim_insert(fold->dim, type, first, n);
167 for (i = 0; i < fold->n; ++i) {
168 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
176 isl_qpolynomial_fold_free(fold);
180 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
182 struct isl_upoly_cst *cst;
184 cst = isl_upoly_as_cst(qp->upoly);
188 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
191 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
192 __isl_keep isl_qpolynomial *qp)
194 enum isl_lp_result res;
199 aff = isl_qpolynomial_extract_affine(qp);
205 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
207 if (res == isl_lp_error)
209 if (res == isl_lp_empty ||
210 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
215 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
217 if (res == isl_lp_ok && !isl_int_is_pos(opt))
226 /* Determine, if possible, the sign of the quasipolynomial "qp" on
229 * If qp is a constant, then the problem is trivial.
230 * If qp is linear, then we check if the minimum of the corresponding
231 * affine constraint is non-negative or if the maximum is non-positive.
233 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
234 * in "set". If so, we write qp(v,v') as
236 * q(v,v') * (v - l) + r(v')
238 * if q(v,v') and r(v') have the same known sign, then the original
239 * quasipolynomial has the same sign as well.
246 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
247 __isl_keep isl_qpolynomial *qp)
252 struct isl_upoly_rec *rec;
255 enum isl_lp_result res;
258 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
262 return isl_qpolynomial_cst_sign(qp);
264 is = isl_qpolynomial_is_affine(qp);
268 return isl_qpolynomial_aff_sign(set, qp);
270 if (qp->div->n_row > 0)
273 rec = isl_upoly_as_rec(qp->upoly);
277 d = isl_dim_total(qp->dim);
278 v = isl_vec_alloc(set->ctx, 2 + d);
282 isl_seq_clr(v->el + 1, 1 + d);
283 isl_int_set_si(v->el[0], 1);
284 isl_int_set_si(v->el[2 + qp->upoly->var], 1);
288 res = isl_set_solve_lp(set, 0, v->el + 1, v->el[0], &l, NULL, NULL);
289 if (res == isl_lp_ok) {
290 isl_qpolynomial *min;
291 isl_qpolynomial *base;
292 isl_qpolynomial *r, *q;
295 min = isl_qpolynomial_cst(isl_dim_copy(qp->dim), l);
296 base = isl_qpolynomial_pow(isl_dim_copy(qp->dim),
299 r = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
300 isl_upoly_copy(rec->p[rec->n - 1]));
301 q = isl_qpolynomial_copy(r);
303 for (i = rec->n - 2; i >= 0; --i) {
304 r = isl_qpolynomial_mul(r, isl_qpolynomial_copy(min));
305 t = isl_qpolynomial_alloc(isl_dim_copy(qp->dim), 0,
306 isl_upoly_copy(rec->p[i]));
307 r = isl_qpolynomial_add(r, t);
310 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
311 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
314 if (isl_qpolynomial_is_zero(q))
315 sgn = isl_qpolynomial_sign(set, r);
316 else if (isl_qpolynomial_is_zero(r))
317 sgn = isl_qpolynomial_sign(set, q);
320 sgn_r = isl_qpolynomial_sign(set, r);
321 sgn_q = isl_qpolynomial_sign(set, q);
326 isl_qpolynomial_free(min);
327 isl_qpolynomial_free(base);
328 isl_qpolynomial_free(q);
329 isl_qpolynomial_free(r);
339 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold_on_domain(
340 __isl_keep isl_set *set,
341 __isl_take isl_qpolynomial_fold *fold1,
342 __isl_take isl_qpolynomial_fold *fold2)
346 struct isl_qpolynomial_fold *res = NULL;
349 if (!fold1 || !fold2)
352 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
353 isl_assert(fold1->dim->ctx, isl_dim_equal(fold1->dim, fold2->dim),
356 better = fold1->type == isl_fold_max ? -1 : 1;
358 if (isl_qpolynomial_fold_is_empty(fold1)) {
359 isl_qpolynomial_fold_free(fold1);
363 if (isl_qpolynomial_fold_is_empty(fold2)) {
364 isl_qpolynomial_fold_free(fold2);
368 res = qpolynomial_fold_alloc(fold1->type, isl_dim_copy(fold1->dim),
369 fold1->n + fold2->n);
373 for (i = 0; i < fold1->n; ++i) {
374 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
375 if (!res->qp[res->n])
381 for (i = 0; i < fold2->n; ++i) {
382 for (j = n1 - 1; j >= 0; --j) {
385 d = isl_qpolynomial_sub(
386 isl_qpolynomial_copy(res->qp[j]),
387 isl_qpolynomial_copy(fold2->qp[i]));
388 sgn = isl_qpolynomial_sign(set, d);
389 isl_qpolynomial_free(d);
394 isl_qpolynomial_free(res->qp[j]);
396 res->qp[j] = res->qp[n1 - 1];
398 if (n1 != res->n - 1)
399 res->qp[n1] = res->qp[res->n - 1];
404 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
405 if (!res->qp[res->n])
410 isl_qpolynomial_fold_free(fold1);
411 isl_qpolynomial_fold_free(fold2);
415 isl_qpolynomial_fold_free(res);
416 isl_qpolynomial_fold_free(fold1);
417 isl_qpolynomial_fold_free(fold2);
421 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
422 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
429 if (isl_qpolynomial_is_zero(qp)) {
430 isl_qpolynomial_free(qp);
434 fold = isl_qpolynomial_fold_cow(fold);
438 for (i = 0; i < fold->n; ++i) {
439 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
440 isl_qpolynomial_copy(qp));
445 isl_qpolynomial_free(qp);
448 isl_qpolynomial_fold_free(fold);
449 isl_qpolynomial_free(qp);
453 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_on_domain(
454 __isl_keep isl_set *dom,
455 __isl_take isl_qpolynomial_fold *fold1,
456 __isl_take isl_qpolynomial_fold *fold2)
459 isl_qpolynomial_fold *res = NULL;
461 if (!fold1 || !fold2)
464 if (isl_qpolynomial_fold_is_empty(fold1)) {
465 isl_qpolynomial_fold_free(fold1);
469 if (isl_qpolynomial_fold_is_empty(fold2)) {
470 isl_qpolynomial_fold_free(fold2);
474 if (fold1->n == 1 && fold2->n != 1)
475 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
478 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
479 isl_qpolynomial_copy(fold2->qp[0]));
480 isl_qpolynomial_fold_free(fold2);
484 res = isl_qpolynomial_fold_add_qpolynomial(
485 isl_qpolynomial_fold_copy(fold1),
486 isl_qpolynomial_copy(fold2->qp[0]));
488 for (i = 1; i < fold2->n; ++i) {
489 isl_qpolynomial_fold *res_i;
490 res_i = isl_qpolynomial_fold_add_qpolynomial(
491 isl_qpolynomial_fold_copy(fold1),
492 isl_qpolynomial_copy(fold2->qp[i]));
493 res = isl_qpolynomial_fold_fold_on_domain(dom, res, res_i);
496 isl_qpolynomial_fold_free(fold1);
497 isl_qpolynomial_fold_free(fold2);
500 isl_qpolynomial_fold_free(res);
501 isl_qpolynomial_fold_free(fold1);
502 isl_qpolynomial_fold_free(fold2);
506 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
507 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
514 fold = isl_qpolynomial_fold_cow(fold);
518 for (i = 0; i < fold->n; ++i) {
519 fold->qp[i] = isl_qpolynomial_substitute_equalities(fold->qp[i],
520 isl_basic_set_copy(eq));
525 isl_basic_set_free(eq);
528 isl_basic_set_free(eq);
529 isl_qpolynomial_fold_free(fold);
536 #define PW isl_pw_qpolynomial_fold
538 #define EL isl_qpolynomial_fold
540 #define IS_ZERO is_empty
544 #include <isl_pw_templ.c>
547 #define UNION isl_union_pw_qpolynomial_fold
549 #define PART isl_pw_qpolynomial_fold
551 #define PARTS pw_qpolynomial_fold
553 #include <isl_union_templ.c>
555 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
556 __isl_take isl_dim *dim)
558 return qpolynomial_fold_alloc(type, dim, 0);
561 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
562 enum isl_fold type, __isl_take isl_qpolynomial *qp)
564 isl_qpolynomial_fold *fold;
569 fold = qpolynomial_fold_alloc(type, isl_dim_copy(qp->dim), 1);
578 isl_qpolynomial_fold_free(fold);
579 isl_qpolynomial_free(qp);
583 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
584 __isl_keep isl_qpolynomial_fold *fold)
593 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
594 __isl_keep isl_qpolynomial_fold *fold)
597 isl_qpolynomial_fold *dup;
601 dup = qpolynomial_fold_alloc(fold->type,
602 isl_dim_copy(fold->dim), fold->n);
607 for (i = 0; i < fold->n; ++i) {
608 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
615 isl_qpolynomial_fold_free(dup);
619 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
620 __isl_take isl_qpolynomial_fold *fold)
628 return isl_qpolynomial_fold_dup(fold);
631 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
640 for (i = 0; i < fold->n; ++i)
641 isl_qpolynomial_free(fold->qp[i]);
642 isl_dim_free(fold->dim);
646 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
654 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
655 __isl_take isl_qpolynomial_fold *fold1,
656 __isl_take isl_qpolynomial_fold *fold2)
659 struct isl_qpolynomial_fold *res = NULL;
661 if (!fold1 || !fold2)
664 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
665 isl_assert(fold1->dim->ctx, isl_dim_equal(fold1->dim, fold2->dim),
668 if (isl_qpolynomial_fold_is_empty(fold1)) {
669 isl_qpolynomial_fold_free(fold1);
673 if (isl_qpolynomial_fold_is_empty(fold2)) {
674 isl_qpolynomial_fold_free(fold2);
678 res = qpolynomial_fold_alloc(fold1->type, isl_dim_copy(fold1->dim),
679 fold1->n + fold2->n);
683 for (i = 0; i < fold1->n; ++i) {
684 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
685 if (!res->qp[res->n])
690 for (i = 0; i < fold2->n; ++i) {
691 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
692 if (!res->qp[res->n])
697 isl_qpolynomial_fold_free(fold1);
698 isl_qpolynomial_fold_free(fold2);
702 isl_qpolynomial_fold_free(res);
703 isl_qpolynomial_fold_free(fold1);
704 isl_qpolynomial_fold_free(fold2);
708 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
709 __isl_take isl_pw_qpolynomial_fold *pw1,
710 __isl_take isl_pw_qpolynomial_fold *pw2)
713 struct isl_pw_qpolynomial_fold *res;
719 isl_assert(pw1->dim->ctx, isl_dim_equal(pw1->dim, pw2->dim), goto error);
721 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
722 isl_pw_qpolynomial_fold_free(pw1);
726 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
727 isl_pw_qpolynomial_fold_free(pw2);
731 if (pw1->type != pw2->type)
732 isl_die(set->ctx, isl_error_invalid, "fold types don't match",
735 n = (pw1->n + 1) * (pw2->n + 1);
736 res = isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pw1->dim),
739 for (i = 0; i < pw1->n; ++i) {
740 set = isl_set_copy(pw1->p[i].set);
741 for (j = 0; j < pw2->n; ++j) {
742 struct isl_set *common;
743 isl_qpolynomial_fold *sum;
744 set = isl_set_subtract(set,
745 isl_set_copy(pw2->p[j].set));
746 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
747 isl_set_copy(pw2->p[j].set));
748 if (isl_set_fast_is_empty(common)) {
749 isl_set_free(common);
753 sum = isl_qpolynomial_fold_fold_on_domain(common,
754 isl_qpolynomial_fold_copy(pw1->p[i].fold),
755 isl_qpolynomial_fold_copy(pw2->p[j].fold));
757 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
759 res = isl_pw_qpolynomial_fold_add_piece(res, set,
760 isl_qpolynomial_fold_copy(pw1->p[i].fold));
763 for (j = 0; j < pw2->n; ++j) {
764 set = isl_set_copy(pw2->p[j].set);
765 for (i = 0; i < pw1->n; ++i)
766 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
767 res = isl_pw_qpolynomial_fold_add_piece(res, set,
768 isl_qpolynomial_fold_copy(pw2->p[j].fold));
771 isl_pw_qpolynomial_fold_free(pw1);
772 isl_pw_qpolynomial_fold_free(pw2);
776 isl_pw_qpolynomial_fold_free(pw1);
777 isl_pw_qpolynomial_fold_free(pw2);
781 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
782 __isl_take isl_union_pw_qpolynomial_fold *u,
783 __isl_take isl_pw_qpolynomial_fold *part)
786 struct isl_hash_table_entry *entry;
788 u = isl_union_pw_qpolynomial_fold_cow(u);
793 isl_assert(u->dim->ctx, isl_dim_match(part->dim, isl_dim_param, u->dim,
794 isl_dim_param), goto error);
796 hash = isl_dim_get_hash(part->dim);
797 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
798 &has_dim, part->dim, 1);
805 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
806 isl_pw_qpolynomial_fold_copy(part));
809 isl_pw_qpolynomial_fold_free(part);
814 isl_pw_qpolynomial_fold_free(part);
815 isl_union_pw_qpolynomial_fold_free(u);
819 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
821 isl_union_pw_qpolynomial_fold **u;
822 u = (isl_union_pw_qpolynomial_fold **)user;
824 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
829 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
830 __isl_take isl_union_pw_qpolynomial_fold *u1,
831 __isl_take isl_union_pw_qpolynomial_fold *u2)
833 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
838 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
839 &fold_part, &u1) < 0)
842 isl_union_pw_qpolynomial_fold_free(u2);
846 isl_union_pw_qpolynomial_fold_free(u1);
847 isl_union_pw_qpolynomial_fold_free(u2);
851 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
852 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
855 isl_pw_qpolynomial_fold *pwf;
860 pwf = isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp->dim), type,
863 for (i = 0; i < pwqp->n; ++i)
864 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
865 isl_set_copy(pwqp->p[i].set),
866 isl_qpolynomial_fold_alloc(type,
867 isl_qpolynomial_copy(pwqp->p[i].qp)));
869 isl_pw_qpolynomial_free(pwqp);
874 int isl_qpolynomial_fold_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
875 __isl_keep isl_qpolynomial_fold *fold2)
879 if (!fold1 || !fold2)
882 if (fold1->n != fold2->n)
885 /* We probably want to sort the qps first... */
886 for (i = 0; i < fold1->n; ++i) {
887 int eq = isl_qpolynomial_is_equal(fold1->qp[i], fold2->qp[i]);
895 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
896 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
902 isl_assert(pnt->dim->ctx, isl_dim_equal(pnt->dim, fold->dim), goto error);
903 isl_assert(pnt->dim->ctx,
904 fold->type == isl_fold_max || fold->type == isl_fold_min,
908 qp = isl_qpolynomial_zero(isl_dim_copy(fold->dim));
911 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
912 isl_point_copy(pnt));
913 for (i = 1; i < fold->n; ++i) {
914 isl_qpolynomial *qp_i;
915 qp_i = isl_qpolynomial_eval(
916 isl_qpolynomial_copy(fold->qp[i]),
917 isl_point_copy(pnt));
918 if (fold->type == isl_fold_max)
919 qp = isl_qpolynomial_max_cst(qp, qp_i);
921 qp = isl_qpolynomial_min_cst(qp, qp_i);
924 isl_qpolynomial_fold_free(fold);
929 isl_qpolynomial_fold_free(fold);
934 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
939 for (i = 0; i < pwf->n; ++i)
940 n += pwf->p[i].fold->n;
945 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
946 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
949 isl_qpolynomial *opt;
955 isl_dim *dim = isl_dim_copy(fold->dim);
957 isl_qpolynomial_fold_free(fold);
958 return isl_qpolynomial_zero(dim);
961 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
962 isl_set_copy(set), max);
963 for (i = 1; i < fold->n; ++i) {
964 isl_qpolynomial *opt_i;
965 opt_i = isl_qpolynomial_opt_on_domain(
966 isl_qpolynomial_copy(fold->qp[i]),
967 isl_set_copy(set), max);
969 opt = isl_qpolynomial_max_cst(opt, opt_i);
971 opt = isl_qpolynomial_min_cst(opt, opt_i);
975 isl_qpolynomial_fold_free(fold);
980 isl_qpolynomial_fold_free(fold);
984 /* Check whether for each quasi-polynomial in "fold2" there is
985 * a quasi-polynomial in "fold1" that dominates it on "set".
987 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
988 __isl_keep isl_qpolynomial_fold *fold1,
989 __isl_keep isl_qpolynomial_fold *fold2)
994 if (!set || !fold1 || !fold2)
997 covers = fold1->type == isl_fold_max ? 1 : -1;
999 for (i = 0; i < fold2->n; ++i) {
1000 for (j = 0; j < fold1->n; ++j) {
1004 d = isl_qpolynomial_sub(
1005 isl_qpolynomial_copy(fold1->qp[j]),
1006 isl_qpolynomial_copy(fold2->qp[i]));
1007 sgn = isl_qpolynomial_sign(set, d);
1008 isl_qpolynomial_free(d);
1019 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1020 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1023 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1024 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1027 isl_set *dom1, *dom2;
1038 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1039 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1040 is_subset = isl_set_is_subset(dom2, dom1);
1044 if (is_subset < 0 || !is_subset)
1047 for (i = 0; i < pwf2->n; ++i) {
1048 for (j = 0; j < pwf1->n; ++j) {
1053 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1054 isl_set_copy(pwf2->p[i].set));
1055 is_empty = isl_set_is_empty(common);
1056 if (is_empty < 0 || is_empty) {
1057 isl_set_free(common);
1062 covers = qpolynomial_fold_covers_on_domain(common,
1063 pwf1->p[j].fold, pwf2->p[i].fold);
1064 isl_set_free(common);
1065 if (covers < 0 || !covers)
1073 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph(
1074 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1079 if (!fold || !morph)
1082 ctx = fold->dim->ctx;
1083 isl_assert(ctx, isl_dim_equal(fold->dim, morph->dom->dim), goto error);
1085 fold = isl_qpolynomial_fold_cow(fold);
1089 isl_dim_free(fold->dim);
1090 fold->dim = isl_dim_copy(morph->ran->dim);
1094 for (i = 0; i < fold->n; ++i) {
1095 fold->qp[i] = isl_qpolynomial_morph(fold->qp[i],
1096 isl_morph_copy(morph));
1101 isl_morph_free(morph);
1105 isl_qpolynomial_fold_free(fold);
1106 isl_morph_free(morph);
1110 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1113 return isl_fold_list;
1117 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1118 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1121 return isl_fold_list;
1125 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1126 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_dim *dim)
1134 if (isl_dim_equal(fold->dim, dim)) {
1139 fold = isl_qpolynomial_fold_cow(fold);
1143 isl_dim_free(fold->dim);
1144 fold->dim = isl_dim_copy(dim);
1148 for (i = 0; i < fold->n; ++i) {
1149 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1159 isl_qpolynomial_fold_free(fold);
1164 int isl_qpolynomial_fold_foreach_qpolynomial(
1165 __isl_keep isl_qpolynomial_fold *fold,
1166 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1173 for (i = 0; i < fold->n; ++i)
1174 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1180 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1181 __isl_take isl_qpolynomial_fold *fold,
1182 enum isl_dim_type dst_type, unsigned dst_pos,
1183 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1190 fold = isl_qpolynomial_fold_cow(fold);
1194 fold->dim = isl_dim_move(fold->dim, dst_type, dst_pos,
1195 src_type, src_pos, n);
1199 for (i = 0; i < fold->n; ++i) {
1200 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1201 dst_type, dst_pos, src_type, src_pos, n);
1208 isl_qpolynomial_fold_free(fold);
1212 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1213 * in fold->qp[k] by subs[i].
1215 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1216 __isl_take isl_qpolynomial_fold *fold,
1217 enum isl_dim_type type, unsigned first, unsigned n,
1218 __isl_keep isl_qpolynomial **subs)
1225 fold = isl_qpolynomial_fold_cow(fold);
1229 for (i = 0; i < fold->n; ++i) {
1230 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1231 type, first, n, subs);
1238 isl_qpolynomial_fold_free(fold);
1242 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1245 isl_pw_qpolynomial_fold *pwf;
1246 isl_union_pw_qpolynomial_fold **upwf;
1248 struct isl_hash_table_entry *entry;
1250 upwf = (isl_union_pw_qpolynomial_fold **)user;
1252 ctx = pwqp->dim->ctx;
1253 hash = isl_dim_get_hash(pwqp->dim);
1254 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1255 hash, &has_dim, pwqp->dim, 1);
1259 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1263 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1266 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1267 isl_pw_qpolynomial_fold_free(entry->data);
1268 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1274 isl_pw_qpolynomial_free(pwqp);
1278 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1279 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1280 __isl_take isl_union_pw_qpolynomial *upwqp)
1282 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1283 isl_union_pw_qpolynomial_get_dim(upwqp));
1284 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1285 isl_union_pw_qpolynomial_fold_get_dim(upwf));
1287 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1288 if (!upwf || !upwqp)
1291 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1295 isl_union_pw_qpolynomial_free(upwqp);
1299 isl_union_pw_qpolynomial_fold_free(upwf);
1300 isl_union_pw_qpolynomial_free(upwqp);
1304 static int compatible_range(__isl_keep isl_dim *dim1, __isl_keep isl_dim *dim2)
1307 m = isl_dim_match(dim1, isl_dim_param, dim2, isl_dim_param);
1310 return isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_set);
1313 /* Compute the intersection of the range of the map and the domain
1314 * of the piecewise quasipolynomial reduction and then compute a bound
1315 * on the associated quasipolynomial reduction over all elements
1316 * in this intersection.
1318 * We first introduce some unconstrained dimensions in the
1319 * piecewise quasipolynomial, intersect the resulting domain
1320 * with the wrapped map and the compute the sum.
1322 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1323 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1333 ctx = isl_map_get_ctx(map);
1337 map_dim = isl_map_get_dim(map);
1338 pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
1339 ok = compatible_range(map_dim, pwf_dim);
1340 isl_dim_free(map_dim);
1341 isl_dim_free(pwf_dim);
1343 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1346 n_in = isl_map_dim(map, isl_dim_in);
1347 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_set, 0, n_in);
1349 dom = isl_map_wrap(map);
1350 pwf = isl_pw_qpolynomial_fold_reset_dim(pwf, isl_set_get_dim(dom));
1352 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1353 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1358 isl_pw_qpolynomial_fold_free(pwf);
1362 struct isl_apply_fold_data {
1363 isl_union_pw_qpolynomial_fold *upwf;
1364 isl_union_pw_qpolynomial_fold *res;
1369 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1374 struct isl_apply_fold_data *data = user;
1377 map_dim = isl_map_get_dim(data->map);
1378 pwf_dim = isl_pw_qpolynomial_fold_get_dim(pwf);
1379 ok = compatible_range(map_dim, pwf_dim);
1380 isl_dim_free(map_dim);
1381 isl_dim_free(pwf_dim);
1384 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1385 pwf, data->tight ? &data->tight : NULL);
1386 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1389 isl_pw_qpolynomial_fold_free(pwf);
1394 static int map_apply(__isl_take isl_map *map, void *user)
1396 struct isl_apply_fold_data *data = user;
1400 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1401 data->upwf, &pw_qpolynomial_fold_apply, data);
1407 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1408 __isl_take isl_union_map *umap,
1409 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1413 struct isl_apply_fold_data data;
1415 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1416 isl_union_map_get_dim(umap));
1417 umap = isl_union_map_align_params(umap,
1418 isl_union_pw_qpolynomial_fold_get_dim(upwf));
1421 data.tight = tight ? 1 : 0;
1422 dim = isl_union_pw_qpolynomial_fold_get_dim(upwf);
1423 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1424 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1425 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1428 isl_union_map_free(umap);
1429 isl_union_pw_qpolynomial_fold_free(upwf);
1432 *tight = data.tight;
1436 isl_union_map_free(umap);
1437 isl_union_pw_qpolynomial_fold_free(upwf);
1438 isl_union_pw_qpolynomial_fold_free(data.res);
1442 /* Reorder the dimension of "fold" according to the given reordering.
1444 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign(
1445 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1449 fold = isl_qpolynomial_fold_cow(fold);
1453 for (i = 0; i < fold->n; ++i) {
1454 fold->qp[i] = isl_qpolynomial_realign(fold->qp[i],
1455 isl_reordering_copy(r));
1460 fold = isl_qpolynomial_fold_reset_dim(fold, isl_dim_copy(r->dim));
1462 isl_reordering_free(r);
1466 isl_qpolynomial_fold_free(fold);
1467 isl_reordering_free(r);