2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
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>
19 #include <isl_mat_private.h>
20 #include <isl_config.h>
22 enum isl_fold isl_fold_type_negate(enum isl_fold type)
33 isl_die(NULL, isl_error_internal, "unhandled isl_fold type", abort());
36 static __isl_give isl_qpolynomial_fold *qpolynomial_fold_alloc(
37 enum isl_fold type, __isl_take isl_space *dim, int n)
39 isl_qpolynomial_fold *fold;
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 *));
63 isl_ctx *isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold *fold)
65 return fold ? fold->dim->ctx : NULL;
68 __isl_give isl_space *isl_qpolynomial_fold_get_domain_space(
69 __isl_keep isl_qpolynomial_fold *fold)
71 return fold ? isl_space_copy(fold->dim) : NULL;
74 __isl_give isl_space *isl_qpolynomial_fold_get_space(
75 __isl_keep isl_qpolynomial_fold *fold)
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);
86 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_reset_domain_space(
87 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
91 fold = isl_qpolynomial_fold_cow(fold);
95 for (i = 0; i < fold->n; ++i) {
96 fold->qp[i] = isl_qpolynomial_reset_domain_space(fold->qp[i],
102 isl_space_free(fold->dim);
107 isl_qpolynomial_fold_free(fold);
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.
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)
120 isl_space_free(space);
121 return isl_qpolynomial_fold_reset_domain_space(fold, domain);
124 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold *fold,
125 enum isl_dim_type type, unsigned first, unsigned n)
131 if (fold->n == 0 || n == 0)
134 for (i = 0; i < fold->n; ++i) {
135 int involves = isl_qpolynomial_involves_dims(fold->qp[i],
137 if (involves < 0 || involves)
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)
149 fold = isl_qpolynomial_fold_cow(fold);
152 fold->dim = isl_space_set_dim_name(fold->dim, type, pos, s);
156 for (i = 0; i < fold->n; ++i) {
157 fold->qp[i] = isl_qpolynomial_set_dim_name(fold->qp[i],
165 isl_qpolynomial_fold_free(fold);
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)
174 enum isl_dim_type set_type;
181 set_type = type == isl_dim_in ? isl_dim_set : type;
183 fold = isl_qpolynomial_fold_cow(fold);
186 fold->dim = isl_space_drop_dims(fold->dim, set_type, first, n);
190 for (i = 0; i < fold->n; ++i) {
191 fold->qp[i] = isl_qpolynomial_drop_dims(fold->qp[i],
199 isl_qpolynomial_fold_free(fold);
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)
211 if (n == 0 && !isl_space_is_named_or_nested(fold->dim, type))
214 fold = isl_qpolynomial_fold_cow(fold);
217 fold->dim = isl_space_insert_dims(fold->dim, type, first, n);
221 for (i = 0; i < fold->n; ++i) {
222 fold->qp[i] = isl_qpolynomial_insert_dims(fold->qp[i],
230 isl_qpolynomial_fold_free(fold);
234 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial *qp)
236 struct isl_upoly_cst *cst;
238 cst = isl_upoly_as_cst(qp->upoly);
242 return isl_int_sgn(cst->n) < 0 ? -1 : 1;
245 static int isl_qpolynomial_aff_sign(__isl_keep isl_set *set,
246 __isl_keep isl_qpolynomial *qp)
248 enum isl_lp_result res;
253 aff = isl_qpolynomial_extract_affine(qp);
259 res = isl_set_solve_lp(set, 0, aff->el + 1, aff->el[0],
261 if (res == isl_lp_error)
263 if (res == isl_lp_empty ||
264 (res == isl_lp_ok && !isl_int_is_neg(opt))) {
269 res = isl_set_solve_lp(set, 1, aff->el + 1, aff->el[0],
271 if (res == isl_lp_ok && !isl_int_is_pos(opt))
280 /* Determine, if possible, the sign of the quasipolynomial "qp" on
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.
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
290 * q(v,v') * (v - l) + r(v')
292 * if q(v,v') and r(v') have the same known sign, then the original
293 * quasipolynomial has the same sign as well.
300 static int isl_qpolynomial_sign(__isl_keep isl_set *set,
301 __isl_keep isl_qpolynomial *qp)
306 struct isl_upoly_rec *rec;
309 enum isl_lp_result res;
312 is = isl_qpolynomial_is_cst(qp, NULL, NULL);
316 return isl_qpolynomial_cst_sign(qp);
318 is = isl_qpolynomial_is_affine(qp);
322 return isl_qpolynomial_aff_sign(set, qp);
324 if (qp->div->n_row > 0)
327 rec = isl_upoly_as_rec(qp->upoly);
331 d = isl_space_dim(qp->dim, isl_dim_all);
332 v = isl_vec_alloc(set->ctx, 2 + d);
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);
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;
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),
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);
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);
364 q = isl_qpolynomial_mul(q, isl_qpolynomial_copy(base));
365 q = isl_qpolynomial_add(q, isl_qpolynomial_copy(r));
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);
374 sgn_r = isl_qpolynomial_sign(set, r);
375 sgn_q = isl_qpolynomial_sign(set, q);
380 isl_qpolynomial_free(min);
381 isl_qpolynomial_free(base);
382 isl_qpolynomial_free(q);
383 isl_qpolynomial_free(r);
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)
400 struct isl_qpolynomial_fold *res = NULL;
403 if (!fold1 || !fold2)
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),
410 better = fold1->type == isl_fold_max ? -1 : 1;
412 if (isl_qpolynomial_fold_is_empty(fold1)) {
413 isl_qpolynomial_fold_free(fold1);
417 if (isl_qpolynomial_fold_is_empty(fold2)) {
418 isl_qpolynomial_fold_free(fold2);
422 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
423 fold1->n + fold2->n);
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])
435 for (i = 0; i < fold2->n; ++i) {
436 for (j = n1 - 1; j >= 0; --j) {
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);
448 isl_qpolynomial_free(res->qp[j]);
450 res->qp[j] = res->qp[n1 - 1];
452 if (n1 != res->n - 1)
453 res->qp[n1] = res->qp[res->n - 1];
458 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
459 if (!res->qp[res->n])
464 isl_qpolynomial_fold_free(fold1);
465 isl_qpolynomial_fold_free(fold2);
469 isl_qpolynomial_fold_free(res);
470 isl_qpolynomial_fold_free(fold1);
471 isl_qpolynomial_fold_free(fold2);
475 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_add_qpolynomial(
476 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_qpolynomial *qp)
483 if (isl_qpolynomial_is_zero(qp)) {
484 isl_qpolynomial_free(qp);
488 fold = isl_qpolynomial_fold_cow(fold);
492 for (i = 0; i < fold->n; ++i) {
493 fold->qp[i] = isl_qpolynomial_add(fold->qp[i],
494 isl_qpolynomial_copy(qp));
499 isl_qpolynomial_free(qp);
502 isl_qpolynomial_fold_free(fold);
503 isl_qpolynomial_free(qp);
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)
513 isl_qpolynomial_fold *res = NULL;
515 if (!fold1 || !fold2)
518 if (isl_qpolynomial_fold_is_empty(fold1)) {
519 isl_qpolynomial_fold_free(fold1);
523 if (isl_qpolynomial_fold_is_empty(fold2)) {
524 isl_qpolynomial_fold_free(fold2);
528 if (fold1->n == 1 && fold2->n != 1)
529 return isl_qpolynomial_fold_add_on_domain(dom, fold2, fold1);
532 res = isl_qpolynomial_fold_add_qpolynomial(fold1,
533 isl_qpolynomial_copy(fold2->qp[0]));
534 isl_qpolynomial_fold_free(fold2);
538 res = isl_qpolynomial_fold_add_qpolynomial(
539 isl_qpolynomial_fold_copy(fold1),
540 isl_qpolynomial_copy(fold2->qp[0]));
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);
550 isl_qpolynomial_fold_free(fold1);
551 isl_qpolynomial_fold_free(fold2);
554 isl_qpolynomial_fold_free(res);
555 isl_qpolynomial_fold_free(fold1);
556 isl_qpolynomial_fold_free(fold2);
560 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute_equalities(
561 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_basic_set *eq)
568 fold = isl_qpolynomial_fold_cow(fold);
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));
579 isl_basic_set_free(eq);
582 isl_basic_set_free(eq);
583 isl_qpolynomial_fold_free(fold);
587 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist(
588 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
592 if (!fold || !context)
595 fold = isl_qpolynomial_fold_cow(fold);
599 for (i = 0; i < fold->n; ++i) {
600 fold->qp[i] = isl_qpolynomial_gist(fold->qp[i],
601 isl_set_copy(context));
606 isl_set_free(context);
609 isl_set_free(context);
610 isl_qpolynomial_fold_free(fold);
614 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_gist_params(
615 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *context)
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);
626 #define PW isl_pw_qpolynomial_fold
628 #define EL isl_qpolynomial_fold
630 #define EL_IS_ZERO is_empty
634 #define IS_ZERO is_zero
637 #undef DEFAULT_IS_ZERO
638 #define DEFAULT_IS_ZERO 1
642 #include <isl_pw_templ.c>
645 #define UNION isl_union_pw_qpolynomial_fold
647 #define PART isl_pw_qpolynomial_fold
649 #define PARTS pw_qpolynomial_fold
652 #include <isl_union_templ.c>
654 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_empty(enum isl_fold type,
655 __isl_take isl_space *dim)
657 return qpolynomial_fold_alloc(type, dim, 0);
660 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_alloc(
661 enum isl_fold type, __isl_take isl_qpolynomial *qp)
663 isl_qpolynomial_fold *fold;
668 fold = qpolynomial_fold_alloc(type, isl_space_copy(qp->dim), 1);
677 isl_qpolynomial_fold_free(fold);
678 isl_qpolynomial_free(qp);
682 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_copy(
683 __isl_keep isl_qpolynomial_fold *fold)
692 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_dup(
693 __isl_keep isl_qpolynomial_fold *fold)
696 isl_qpolynomial_fold *dup;
700 dup = qpolynomial_fold_alloc(fold->type,
701 isl_space_copy(fold->dim), fold->n);
706 for (i = 0; i < fold->n; ++i) {
707 dup->qp[i] = isl_qpolynomial_copy(fold->qp[i]);
714 isl_qpolynomial_fold_free(dup);
718 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_cow(
719 __isl_take isl_qpolynomial_fold *fold)
727 return isl_qpolynomial_fold_dup(fold);
730 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold *fold)
739 for (i = 0; i < fold->n; ++i)
740 isl_qpolynomial_free(fold->qp[i]);
741 isl_space_free(fold->dim);
745 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold *fold)
753 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_fold(
754 __isl_take isl_qpolynomial_fold *fold1,
755 __isl_take isl_qpolynomial_fold *fold2)
758 struct isl_qpolynomial_fold *res = NULL;
760 if (!fold1 || !fold2)
763 isl_assert(fold1->dim->ctx, fold1->type == fold2->type, goto error);
764 isl_assert(fold1->dim->ctx, isl_space_is_equal(fold1->dim, fold2->dim),
767 if (isl_qpolynomial_fold_is_empty(fold1)) {
768 isl_qpolynomial_fold_free(fold1);
772 if (isl_qpolynomial_fold_is_empty(fold2)) {
773 isl_qpolynomial_fold_free(fold2);
777 res = qpolynomial_fold_alloc(fold1->type, isl_space_copy(fold1->dim),
778 fold1->n + fold2->n);
782 for (i = 0; i < fold1->n; ++i) {
783 res->qp[res->n] = isl_qpolynomial_copy(fold1->qp[i]);
784 if (!res->qp[res->n])
789 for (i = 0; i < fold2->n; ++i) {
790 res->qp[res->n] = isl_qpolynomial_copy(fold2->qp[i]);
791 if (!res->qp[res->n])
796 isl_qpolynomial_fold_free(fold1);
797 isl_qpolynomial_fold_free(fold2);
801 isl_qpolynomial_fold_free(res);
802 isl_qpolynomial_fold_free(fold1);
803 isl_qpolynomial_fold_free(fold2);
807 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_fold(
808 __isl_take isl_pw_qpolynomial_fold *pw1,
809 __isl_take isl_pw_qpolynomial_fold *pw2)
812 struct isl_pw_qpolynomial_fold *res;
818 isl_assert(pw1->dim->ctx, isl_space_is_equal(pw1->dim, pw2->dim), goto error);
820 if (isl_pw_qpolynomial_fold_is_zero(pw1)) {
821 isl_pw_qpolynomial_fold_free(pw1);
825 if (isl_pw_qpolynomial_fold_is_zero(pw2)) {
826 isl_pw_qpolynomial_fold_free(pw2);
830 if (pw1->type != pw2->type)
831 isl_die(pw1->dim->ctx, isl_error_invalid,
832 "fold types don't match", goto error);
834 n = (pw1->n + 1) * (pw2->n + 1);
835 res = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pw1->dim),
838 for (i = 0; i < pw1->n; ++i) {
839 set = isl_set_copy(pw1->p[i].set);
840 for (j = 0; j < pw2->n; ++j) {
841 struct isl_set *common;
842 isl_qpolynomial_fold *sum;
843 set = isl_set_subtract(set,
844 isl_set_copy(pw2->p[j].set));
845 common = isl_set_intersect(isl_set_copy(pw1->p[i].set),
846 isl_set_copy(pw2->p[j].set));
847 if (isl_set_plain_is_empty(common)) {
848 isl_set_free(common);
852 sum = isl_qpolynomial_fold_fold_on_domain(common,
853 isl_qpolynomial_fold_copy(pw1->p[i].fold),
854 isl_qpolynomial_fold_copy(pw2->p[j].fold));
856 res = isl_pw_qpolynomial_fold_add_piece(res, common, sum);
858 res = isl_pw_qpolynomial_fold_add_piece(res, set,
859 isl_qpolynomial_fold_copy(pw1->p[i].fold));
862 for (j = 0; j < pw2->n; ++j) {
863 set = isl_set_copy(pw2->p[j].set);
864 for (i = 0; i < pw1->n; ++i)
865 set = isl_set_subtract(set, isl_set_copy(pw1->p[i].set));
866 res = isl_pw_qpolynomial_fold_add_piece(res, set,
867 isl_qpolynomial_fold_copy(pw2->p[j].fold));
870 isl_pw_qpolynomial_fold_free(pw1);
871 isl_pw_qpolynomial_fold_free(pw2);
875 isl_pw_qpolynomial_fold_free(pw1);
876 isl_pw_qpolynomial_fold_free(pw2);
880 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
881 __isl_take isl_union_pw_qpolynomial_fold *u,
882 __isl_take isl_pw_qpolynomial_fold *part)
885 struct isl_hash_table_entry *entry;
887 u = isl_union_pw_qpolynomial_fold_cow(u);
892 isl_assert(u->dim->ctx, isl_space_match(part->dim, isl_dim_param, u->dim,
893 isl_dim_param), goto error);
895 hash = isl_space_get_hash(part->dim);
896 entry = isl_hash_table_find(u->dim->ctx, &u->table, hash,
897 &has_dim, part->dim, 1);
904 entry->data = isl_pw_qpolynomial_fold_fold(entry->data,
905 isl_pw_qpolynomial_fold_copy(part));
908 isl_pw_qpolynomial_fold_free(part);
913 isl_pw_qpolynomial_fold_free(part);
914 isl_union_pw_qpolynomial_fold_free(u);
918 static int fold_part(__isl_take isl_pw_qpolynomial_fold *part, void *user)
920 isl_union_pw_qpolynomial_fold **u;
921 u = (isl_union_pw_qpolynomial_fold **)user;
923 *u = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u, part);
928 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_fold(
929 __isl_take isl_union_pw_qpolynomial_fold *u1,
930 __isl_take isl_union_pw_qpolynomial_fold *u2)
932 u1 = isl_union_pw_qpolynomial_fold_cow(u1);
937 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2,
938 &fold_part, &u1) < 0)
941 isl_union_pw_qpolynomial_fold_free(u2);
945 isl_union_pw_qpolynomial_fold_free(u1);
946 isl_union_pw_qpolynomial_fold_free(u2);
950 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_from_pw_qpolynomial(
951 enum isl_fold type, __isl_take isl_pw_qpolynomial *pwqp)
954 isl_pw_qpolynomial_fold *pwf;
959 pwf = isl_pw_qpolynomial_fold_alloc_size(isl_space_copy(pwqp->dim),
962 for (i = 0; i < pwqp->n; ++i)
963 pwf = isl_pw_qpolynomial_fold_add_piece(pwf,
964 isl_set_copy(pwqp->p[i].set),
965 isl_qpolynomial_fold_alloc(type,
966 isl_qpolynomial_copy(pwqp->p[i].qp)));
968 isl_pw_qpolynomial_free(pwqp);
973 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_add(
974 __isl_take isl_pw_qpolynomial_fold *pwf1,
975 __isl_take isl_pw_qpolynomial_fold *pwf2)
977 return isl_pw_qpolynomial_fold_union_add_(pwf1, pwf2);
980 int isl_qpolynomial_fold_plain_is_equal(__isl_keep isl_qpolynomial_fold *fold1,
981 __isl_keep isl_qpolynomial_fold *fold2)
985 if (!fold1 || !fold2)
988 if (fold1->n != fold2->n)
991 /* We probably want to sort the qps first... */
992 for (i = 0; i < fold1->n; ++i) {
993 int eq = isl_qpolynomial_plain_is_equal(fold1->qp[i], fold2->qp[i]);
1001 __isl_give isl_qpolynomial *isl_qpolynomial_fold_eval(
1002 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_point *pnt)
1004 isl_qpolynomial *qp;
1008 isl_assert(pnt->dim->ctx, isl_space_is_equal(pnt->dim, fold->dim), goto error);
1009 isl_assert(pnt->dim->ctx,
1010 fold->type == isl_fold_max || fold->type == isl_fold_min,
1014 qp = isl_qpolynomial_zero_on_domain(isl_space_copy(fold->dim));
1017 qp = isl_qpolynomial_eval(isl_qpolynomial_copy(fold->qp[0]),
1018 isl_point_copy(pnt));
1019 for (i = 1; i < fold->n; ++i) {
1020 isl_qpolynomial *qp_i;
1021 qp_i = isl_qpolynomial_eval(
1022 isl_qpolynomial_copy(fold->qp[i]),
1023 isl_point_copy(pnt));
1024 if (fold->type == isl_fold_max)
1025 qp = isl_qpolynomial_max_cst(qp, qp_i);
1027 qp = isl_qpolynomial_min_cst(qp, qp_i);
1030 isl_qpolynomial_fold_free(fold);
1031 isl_point_free(pnt);
1035 isl_qpolynomial_fold_free(fold);
1036 isl_point_free(pnt);
1040 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold *pwf)
1045 for (i = 0; i < pwf->n; ++i)
1046 n += pwf->p[i].fold->n;
1051 __isl_give isl_qpolynomial *isl_qpolynomial_fold_opt_on_domain(
1052 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_set *set, int max)
1055 isl_qpolynomial *opt;
1061 isl_space *dim = isl_space_copy(fold->dim);
1063 isl_qpolynomial_fold_free(fold);
1064 return isl_qpolynomial_zero_on_domain(dim);
1067 opt = isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold->qp[0]),
1068 isl_set_copy(set), max);
1069 for (i = 1; i < fold->n; ++i) {
1070 isl_qpolynomial *opt_i;
1071 opt_i = isl_qpolynomial_opt_on_domain(
1072 isl_qpolynomial_copy(fold->qp[i]),
1073 isl_set_copy(set), max);
1075 opt = isl_qpolynomial_max_cst(opt, opt_i);
1077 opt = isl_qpolynomial_min_cst(opt, opt_i);
1081 isl_qpolynomial_fold_free(fold);
1086 isl_qpolynomial_fold_free(fold);
1090 /* Check whether for each quasi-polynomial in "fold2" there is
1091 * a quasi-polynomial in "fold1" that dominates it on "set".
1093 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set *set,
1094 __isl_keep isl_qpolynomial_fold *fold1,
1095 __isl_keep isl_qpolynomial_fold *fold2)
1100 if (!set || !fold1 || !fold2)
1103 covers = fold1->type == isl_fold_max ? 1 : -1;
1105 for (i = 0; i < fold2->n; ++i) {
1106 for (j = 0; j < fold1->n; ++j) {
1110 d = isl_qpolynomial_sub(
1111 isl_qpolynomial_copy(fold1->qp[j]),
1112 isl_qpolynomial_copy(fold2->qp[i]));
1113 sgn = isl_qpolynomial_sign(set, d);
1114 isl_qpolynomial_free(d);
1125 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1126 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1129 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold *pwf1,
1130 __isl_keep isl_pw_qpolynomial_fold *pwf2)
1133 isl_set *dom1, *dom2;
1144 dom1 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1));
1145 dom2 = isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2));
1146 is_subset = isl_set_is_subset(dom2, dom1);
1150 if (is_subset < 0 || !is_subset)
1153 for (i = 0; i < pwf2->n; ++i) {
1154 for (j = 0; j < pwf1->n; ++j) {
1159 common = isl_set_intersect(isl_set_copy(pwf1->p[j].set),
1160 isl_set_copy(pwf2->p[i].set));
1161 is_empty = isl_set_is_empty(common);
1162 if (is_empty < 0 || is_empty) {
1163 isl_set_free(common);
1168 covers = qpolynomial_fold_covers_on_domain(common,
1169 pwf1->p[j].fold, pwf2->p[i].fold);
1170 isl_set_free(common);
1171 if (covers < 0 || !covers)
1179 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_morph_domain(
1180 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_morph *morph)
1185 if (!fold || !morph)
1188 ctx = fold->dim->ctx;
1189 isl_assert(ctx, isl_space_is_equal(fold->dim, morph->dom->dim), goto error);
1191 fold = isl_qpolynomial_fold_cow(fold);
1195 isl_space_free(fold->dim);
1196 fold->dim = isl_space_copy(morph->ran->dim);
1200 for (i = 0; i < fold->n; ++i) {
1201 fold->qp[i] = isl_qpolynomial_morph_domain(fold->qp[i],
1202 isl_morph_copy(morph));
1207 isl_morph_free(morph);
1211 isl_qpolynomial_fold_free(fold);
1212 isl_morph_free(morph);
1216 enum isl_fold isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold *fold)
1219 return isl_fold_list;
1223 enum isl_fold isl_union_pw_qpolynomial_fold_get_type(
1224 __isl_keep isl_union_pw_qpolynomial_fold *upwf)
1227 return isl_fold_list;
1231 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_lift(
1232 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_space *dim)
1239 if (isl_space_is_equal(fold->dim, dim)) {
1240 isl_space_free(dim);
1244 fold = isl_qpolynomial_fold_cow(fold);
1248 isl_space_free(fold->dim);
1249 fold->dim = isl_space_copy(dim);
1253 for (i = 0; i < fold->n; ++i) {
1254 fold->qp[i] = isl_qpolynomial_lift(fold->qp[i],
1255 isl_space_copy(dim));
1260 isl_space_free(dim);
1264 isl_qpolynomial_fold_free(fold);
1265 isl_space_free(dim);
1269 int isl_qpolynomial_fold_foreach_qpolynomial(
1270 __isl_keep isl_qpolynomial_fold *fold,
1271 int (*fn)(__isl_take isl_qpolynomial *qp, void *user), void *user)
1278 for (i = 0; i < fold->n; ++i)
1279 if (fn(isl_qpolynomial_copy(fold->qp[i]), user) < 0)
1285 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_move_dims(
1286 __isl_take isl_qpolynomial_fold *fold,
1287 enum isl_dim_type dst_type, unsigned dst_pos,
1288 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1295 fold = isl_qpolynomial_fold_cow(fold);
1299 fold->dim = isl_space_move_dims(fold->dim, dst_type, dst_pos,
1300 src_type, src_pos, n);
1304 for (i = 0; i < fold->n; ++i) {
1305 fold->qp[i] = isl_qpolynomial_move_dims(fold->qp[i],
1306 dst_type, dst_pos, src_type, src_pos, n);
1313 isl_qpolynomial_fold_free(fold);
1317 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1318 * in fold->qp[k] by subs[i].
1320 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_substitute(
1321 __isl_take isl_qpolynomial_fold *fold,
1322 enum isl_dim_type type, unsigned first, unsigned n,
1323 __isl_keep isl_qpolynomial **subs)
1330 fold = isl_qpolynomial_fold_cow(fold);
1334 for (i = 0; i < fold->n; ++i) {
1335 fold->qp[i] = isl_qpolynomial_substitute(fold->qp[i],
1336 type, first, n, subs);
1343 isl_qpolynomial_fold_free(fold);
1347 static int add_pwqp(__isl_take isl_pw_qpolynomial *pwqp, void *user)
1350 isl_pw_qpolynomial_fold *pwf;
1351 isl_union_pw_qpolynomial_fold **upwf;
1353 struct isl_hash_table_entry *entry;
1355 upwf = (isl_union_pw_qpolynomial_fold **)user;
1357 ctx = pwqp->dim->ctx;
1358 hash = isl_space_get_hash(pwqp->dim);
1359 entry = isl_hash_table_find(ctx, &(*upwf)->table,
1360 hash, &has_dim, pwqp->dim, 1);
1364 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf)->type, pwqp);
1368 entry->data = isl_pw_qpolynomial_fold_add(entry->data, pwf);
1371 if (isl_pw_qpolynomial_fold_is_zero(entry->data)) {
1372 isl_pw_qpolynomial_fold_free(entry->data);
1373 isl_hash_table_remove(ctx, &(*upwf)->table, entry);
1379 isl_pw_qpolynomial_free(pwqp);
1383 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1384 __isl_take isl_union_pw_qpolynomial_fold *upwf,
1385 __isl_take isl_union_pw_qpolynomial *upwqp)
1387 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1388 isl_union_pw_qpolynomial_get_space(upwqp));
1389 upwqp = isl_union_pw_qpolynomial_align_params(upwqp,
1390 isl_union_pw_qpolynomial_fold_get_space(upwf));
1392 upwf = isl_union_pw_qpolynomial_fold_cow(upwf);
1393 if (!upwf || !upwqp)
1396 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp, &add_pwqp,
1400 isl_union_pw_qpolynomial_free(upwqp);
1404 isl_union_pw_qpolynomial_fold_free(upwf);
1405 isl_union_pw_qpolynomial_free(upwqp);
1409 static int join_compatible(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1412 m = isl_space_match(dim1, isl_dim_param, dim2, isl_dim_param);
1415 return isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_in);
1418 /* Compute the intersection of the range of the map and the domain
1419 * of the piecewise quasipolynomial reduction and then compute a bound
1420 * on the associated quasipolynomial reduction over all elements
1421 * in this intersection.
1423 * We first introduce some unconstrained dimensions in the
1424 * piecewise quasipolynomial, intersect the resulting domain
1425 * with the wrapped map and the compute the sum.
1427 __isl_give isl_pw_qpolynomial_fold *isl_map_apply_pw_qpolynomial_fold(
1428 __isl_take isl_map *map, __isl_take isl_pw_qpolynomial_fold *pwf,
1438 ctx = isl_map_get_ctx(map);
1442 map_dim = isl_map_get_space(map);
1443 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1444 ok = join_compatible(map_dim, pwf_dim);
1445 isl_space_free(map_dim);
1446 isl_space_free(pwf_dim);
1448 isl_die(ctx, isl_error_invalid, "incompatible dimensions",
1451 n_in = isl_map_dim(map, isl_dim_in);
1452 pwf = isl_pw_qpolynomial_fold_insert_dims(pwf, isl_dim_in, 0, n_in);
1454 dom = isl_map_wrap(map);
1455 pwf = isl_pw_qpolynomial_fold_reset_domain_space(pwf,
1456 isl_set_get_space(dom));
1458 pwf = isl_pw_qpolynomial_fold_intersect_domain(pwf, dom);
1459 pwf = isl_pw_qpolynomial_fold_bound(pwf, tight);
1464 isl_pw_qpolynomial_fold_free(pwf);
1468 __isl_give isl_pw_qpolynomial_fold *isl_set_apply_pw_qpolynomial_fold(
1469 __isl_take isl_set *set, __isl_take isl_pw_qpolynomial_fold *pwf,
1472 return isl_map_apply_pw_qpolynomial_fold(set, pwf, tight);
1475 struct isl_apply_fold_data {
1476 isl_union_pw_qpolynomial_fold *upwf;
1477 isl_union_pw_qpolynomial_fold *res;
1482 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold *pwf,
1487 struct isl_apply_fold_data *data = user;
1490 map_dim = isl_map_get_space(data->map);
1491 pwf_dim = isl_pw_qpolynomial_fold_get_space(pwf);
1492 ok = join_compatible(map_dim, pwf_dim);
1493 isl_space_free(map_dim);
1494 isl_space_free(pwf_dim);
1497 pwf = isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data->map),
1498 pwf, data->tight ? &data->tight : NULL);
1499 data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1502 isl_pw_qpolynomial_fold_free(pwf);
1507 static int map_apply(__isl_take isl_map *map, void *user)
1509 struct isl_apply_fold_data *data = user;
1513 r = isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1514 data->upwf, &pw_qpolynomial_fold_apply, data);
1520 __isl_give isl_union_pw_qpolynomial_fold *isl_union_map_apply_union_pw_qpolynomial_fold(
1521 __isl_take isl_union_map *umap,
1522 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1526 struct isl_apply_fold_data data;
1528 upwf = isl_union_pw_qpolynomial_fold_align_params(upwf,
1529 isl_union_map_get_space(umap));
1530 umap = isl_union_map_align_params(umap,
1531 isl_union_pw_qpolynomial_fold_get_space(upwf));
1534 data.tight = tight ? 1 : 0;
1535 dim = isl_union_pw_qpolynomial_fold_get_space(upwf);
1536 type = isl_union_pw_qpolynomial_fold_get_type(upwf);
1537 data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
1538 if (isl_union_map_foreach_map(umap, &map_apply, &data) < 0)
1541 isl_union_map_free(umap);
1542 isl_union_pw_qpolynomial_fold_free(upwf);
1545 *tight = data.tight;
1549 isl_union_map_free(umap);
1550 isl_union_pw_qpolynomial_fold_free(upwf);
1551 isl_union_pw_qpolynomial_fold_free(data.res);
1555 __isl_give isl_union_pw_qpolynomial_fold *isl_union_set_apply_union_pw_qpolynomial_fold(
1556 __isl_take isl_union_set *uset,
1557 __isl_take isl_union_pw_qpolynomial_fold *upwf, int *tight)
1559 return isl_union_map_apply_union_pw_qpolynomial_fold(uset, upwf, tight);
1562 /* Reorder the dimension of "fold" according to the given reordering.
1564 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_realign_domain(
1565 __isl_take isl_qpolynomial_fold *fold, __isl_take isl_reordering *r)
1569 fold = isl_qpolynomial_fold_cow(fold);
1573 for (i = 0; i < fold->n; ++i) {
1574 fold->qp[i] = isl_qpolynomial_realign_domain(fold->qp[i],
1575 isl_reordering_copy(r));
1580 fold = isl_qpolynomial_fold_reset_domain_space(fold,
1581 isl_space_copy(r->dim));
1583 isl_reordering_free(r);
1587 isl_qpolynomial_fold_free(fold);
1588 isl_reordering_free(r);
1592 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_mul_isl_int(
1593 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1597 if (isl_int_is_one(v))
1599 if (fold && isl_int_is_zero(v)) {
1600 isl_qpolynomial_fold *zero;
1601 isl_space *dim = isl_space_copy(fold->dim);
1602 zero = isl_qpolynomial_fold_empty(fold->type, dim);
1603 isl_qpolynomial_fold_free(fold);
1607 fold = isl_qpolynomial_fold_cow(fold);
1611 if (isl_int_is_neg(v))
1612 fold->type = isl_fold_type_negate(fold->type);
1613 for (i = 0; i < fold->n; ++i) {
1614 fold->qp[i] = isl_qpolynomial_mul_isl_int(fold->qp[i], v);
1621 isl_qpolynomial_fold_free(fold);
1625 __isl_give isl_qpolynomial_fold *isl_qpolynomial_fold_scale(
1626 __isl_take isl_qpolynomial_fold *fold, isl_int v)
1628 return isl_qpolynomial_fold_mul_isl_int(fold, v);