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_map_private.h>
12 #include <isl_union_map_private.h>
13 #include <isl_polynomial_private.h>
14 #include <isl_point_private.h>
15 #include <isl_dim_private.h>
18 #include <isl_mat_private.h>
20 enum isl_fold
isl_fold_type_negate(enum isl_fold type
)
32 static __isl_give isl_qpolynomial_fold
*qpolynomial_fold_alloc(
33 enum isl_fold type
, __isl_take isl_dim
*dim
, int n
)
35 isl_qpolynomial_fold
*fold
;
40 isl_assert(dim
->ctx
, n
>= 0, goto error
);
41 fold
= isl_calloc(dim
->ctx
, struct isl_qpolynomial_fold
,
42 sizeof(struct isl_qpolynomial_fold
) +
43 (n
- 1) * sizeof(struct isl_qpolynomial
*));
59 isl_ctx
*isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold
*fold
)
61 return fold
? fold
->dim
->ctx
: NULL
;
64 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_reset_dim(
65 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_dim
*dim
)
69 fold
= isl_qpolynomial_fold_cow(fold
);
73 for (i
= 0; i
< fold
->n
; ++i
) {
74 fold
->qp
[i
] = isl_qpolynomial_reset_dim(fold
->qp
[i
],
80 isl_dim_free(fold
->dim
);
85 isl_qpolynomial_fold_free(fold
);
90 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold
*fold
,
91 enum isl_dim_type type
, unsigned first
, unsigned n
)
97 if (fold
->n
== 0 || n
== 0)
100 for (i
= 0; i
< fold
->n
; ++i
) {
101 int involves
= isl_qpolynomial_involves_dims(fold
->qp
[i
],
103 if (involves
< 0 || involves
)
109 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_set_dim_name(
110 __isl_take isl_qpolynomial_fold
*fold
,
111 enum isl_dim_type type
, unsigned pos
, const char *s
)
115 fold
= isl_qpolynomial_fold_cow(fold
);
118 fold
->dim
= isl_dim_set_name(fold
->dim
, type
, pos
, s
);
122 for (i
= 0; i
< fold
->n
; ++i
) {
123 fold
->qp
[i
] = isl_qpolynomial_set_dim_name(fold
->qp
[i
],
131 isl_qpolynomial_fold_free(fold
);
135 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_drop_dims(
136 __isl_take isl_qpolynomial_fold
*fold
,
137 enum isl_dim_type type
, unsigned first
, unsigned n
)
146 fold
= isl_qpolynomial_fold_cow(fold
);
149 fold
->dim
= isl_dim_drop(fold
->dim
, type
, first
, n
);
153 for (i
= 0; i
< fold
->n
; ++i
) {
154 fold
->qp
[i
] = isl_qpolynomial_drop_dims(fold
->qp
[i
],
162 isl_qpolynomial_fold_free(fold
);
166 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_insert_dims(
167 __isl_take isl_qpolynomial_fold
*fold
,
168 enum isl_dim_type type
, unsigned first
, unsigned n
)
174 if (n
== 0 && !isl_dim_is_named_or_nested(fold
->dim
, type
))
177 fold
= isl_qpolynomial_fold_cow(fold
);
180 fold
->dim
= isl_dim_insert(fold
->dim
, type
, first
, n
);
184 for (i
= 0; i
< fold
->n
; ++i
) {
185 fold
->qp
[i
] = isl_qpolynomial_insert_dims(fold
->qp
[i
],
193 isl_qpolynomial_fold_free(fold
);
197 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial
*qp
)
199 struct isl_upoly_cst
*cst
;
201 cst
= isl_upoly_as_cst(qp
->upoly
);
205 return isl_int_sgn(cst
->n
) < 0 ? -1 : 1;
208 static int isl_qpolynomial_aff_sign(__isl_keep isl_set
*set
,
209 __isl_keep isl_qpolynomial
*qp
)
211 enum isl_lp_result res
;
216 aff
= isl_qpolynomial_extract_affine(qp
);
222 res
= isl_set_solve_lp(set
, 0, aff
->el
+ 1, aff
->el
[0],
224 if (res
== isl_lp_error
)
226 if (res
== isl_lp_empty
||
227 (res
== isl_lp_ok
&& !isl_int_is_neg(opt
))) {
232 res
= isl_set_solve_lp(set
, 1, aff
->el
+ 1, aff
->el
[0],
234 if (res
== isl_lp_ok
&& !isl_int_is_pos(opt
))
243 /* Determine, if possible, the sign of the quasipolynomial "qp" on
246 * If qp is a constant, then the problem is trivial.
247 * If qp is linear, then we check if the minimum of the corresponding
248 * affine constraint is non-negative or if the maximum is non-positive.
250 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
251 * in "set". If so, we write qp(v,v') as
253 * q(v,v') * (v - l) + r(v')
255 * if q(v,v') and r(v') have the same known sign, then the original
256 * quasipolynomial has the same sign as well.
263 static int isl_qpolynomial_sign(__isl_keep isl_set
*set
,
264 __isl_keep isl_qpolynomial
*qp
)
269 struct isl_upoly_rec
*rec
;
272 enum isl_lp_result res
;
275 is
= isl_qpolynomial_is_cst(qp
, NULL
, NULL
);
279 return isl_qpolynomial_cst_sign(qp
);
281 is
= isl_qpolynomial_is_affine(qp
);
285 return isl_qpolynomial_aff_sign(set
, qp
);
287 if (qp
->div
->n_row
> 0)
290 rec
= isl_upoly_as_rec(qp
->upoly
);
294 d
= isl_dim_total(qp
->dim
);
295 v
= isl_vec_alloc(set
->ctx
, 2 + d
);
299 isl_seq_clr(v
->el
+ 1, 1 + d
);
300 isl_int_set_si(v
->el
[0], 1);
301 isl_int_set_si(v
->el
[2 + qp
->upoly
->var
], 1);
305 res
= isl_set_solve_lp(set
, 0, v
->el
+ 1, v
->el
[0], &l
, NULL
, NULL
);
306 if (res
== isl_lp_ok
) {
307 isl_qpolynomial
*min
;
308 isl_qpolynomial
*base
;
309 isl_qpolynomial
*r
, *q
;
312 min
= isl_qpolynomial_cst(isl_dim_copy(qp
->dim
), l
);
313 base
= isl_qpolynomial_var_pow(isl_dim_copy(qp
->dim
),
316 r
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), 0,
317 isl_upoly_copy(rec
->p
[rec
->n
- 1]));
318 q
= isl_qpolynomial_copy(r
);
320 for (i
= rec
->n
- 2; i
>= 0; --i
) {
321 r
= isl_qpolynomial_mul(r
, isl_qpolynomial_copy(min
));
322 t
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), 0,
323 isl_upoly_copy(rec
->p
[i
]));
324 r
= isl_qpolynomial_add(r
, t
);
327 q
= isl_qpolynomial_mul(q
, isl_qpolynomial_copy(base
));
328 q
= isl_qpolynomial_add(q
, isl_qpolynomial_copy(r
));
331 if (isl_qpolynomial_is_zero(q
))
332 sgn
= isl_qpolynomial_sign(set
, r
);
333 else if (isl_qpolynomial_is_zero(r
))
334 sgn
= isl_qpolynomial_sign(set
, q
);
337 sgn_r
= isl_qpolynomial_sign(set
, r
);
338 sgn_q
= isl_qpolynomial_sign(set
, q
);
343 isl_qpolynomial_free(min
);
344 isl_qpolynomial_free(base
);
345 isl_qpolynomial_free(q
);
346 isl_qpolynomial_free(r
);
356 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold_on_domain(
357 __isl_keep isl_set
*set
,
358 __isl_take isl_qpolynomial_fold
*fold1
,
359 __isl_take isl_qpolynomial_fold
*fold2
)
363 struct isl_qpolynomial_fold
*res
= NULL
;
366 if (!fold1
|| !fold2
)
369 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
370 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
373 better
= fold1
->type
== isl_fold_max
? -1 : 1;
375 if (isl_qpolynomial_fold_is_empty(fold1
)) {
376 isl_qpolynomial_fold_free(fold1
);
380 if (isl_qpolynomial_fold_is_empty(fold2
)) {
381 isl_qpolynomial_fold_free(fold2
);
385 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
386 fold1
->n
+ fold2
->n
);
390 for (i
= 0; i
< fold1
->n
; ++i
) {
391 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
392 if (!res
->qp
[res
->n
])
398 for (i
= 0; i
< fold2
->n
; ++i
) {
399 for (j
= n1
- 1; j
>= 0; --j
) {
402 d
= isl_qpolynomial_sub(
403 isl_qpolynomial_copy(res
->qp
[j
]),
404 isl_qpolynomial_copy(fold2
->qp
[i
]));
405 sgn
= isl_qpolynomial_sign(set
, d
);
406 isl_qpolynomial_free(d
);
411 isl_qpolynomial_free(res
->qp
[j
]);
413 res
->qp
[j
] = res
->qp
[n1
- 1];
415 if (n1
!= res
->n
- 1)
416 res
->qp
[n1
] = res
->qp
[res
->n
- 1];
421 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
422 if (!res
->qp
[res
->n
])
427 isl_qpolynomial_fold_free(fold1
);
428 isl_qpolynomial_fold_free(fold2
);
432 isl_qpolynomial_fold_free(res
);
433 isl_qpolynomial_fold_free(fold1
);
434 isl_qpolynomial_fold_free(fold2
);
438 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_qpolynomial(
439 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_qpolynomial
*qp
)
446 if (isl_qpolynomial_is_zero(qp
)) {
447 isl_qpolynomial_free(qp
);
451 fold
= isl_qpolynomial_fold_cow(fold
);
455 for (i
= 0; i
< fold
->n
; ++i
) {
456 fold
->qp
[i
] = isl_qpolynomial_add(fold
->qp
[i
],
457 isl_qpolynomial_copy(qp
));
462 isl_qpolynomial_free(qp
);
465 isl_qpolynomial_fold_free(fold
);
466 isl_qpolynomial_free(qp
);
470 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_on_domain(
471 __isl_keep isl_set
*dom
,
472 __isl_take isl_qpolynomial_fold
*fold1
,
473 __isl_take isl_qpolynomial_fold
*fold2
)
476 isl_qpolynomial_fold
*res
= NULL
;
478 if (!fold1
|| !fold2
)
481 if (isl_qpolynomial_fold_is_empty(fold1
)) {
482 isl_qpolynomial_fold_free(fold1
);
486 if (isl_qpolynomial_fold_is_empty(fold2
)) {
487 isl_qpolynomial_fold_free(fold2
);
491 if (fold1
->n
== 1 && fold2
->n
!= 1)
492 return isl_qpolynomial_fold_add_on_domain(dom
, fold2
, fold1
);
495 res
= isl_qpolynomial_fold_add_qpolynomial(fold1
,
496 isl_qpolynomial_copy(fold2
->qp
[0]));
497 isl_qpolynomial_fold_free(fold2
);
501 res
= isl_qpolynomial_fold_add_qpolynomial(
502 isl_qpolynomial_fold_copy(fold1
),
503 isl_qpolynomial_copy(fold2
->qp
[0]));
505 for (i
= 1; i
< fold2
->n
; ++i
) {
506 isl_qpolynomial_fold
*res_i
;
507 res_i
= isl_qpolynomial_fold_add_qpolynomial(
508 isl_qpolynomial_fold_copy(fold1
),
509 isl_qpolynomial_copy(fold2
->qp
[i
]));
510 res
= isl_qpolynomial_fold_fold_on_domain(dom
, res
, res_i
);
513 isl_qpolynomial_fold_free(fold1
);
514 isl_qpolynomial_fold_free(fold2
);
517 isl_qpolynomial_fold_free(res
);
518 isl_qpolynomial_fold_free(fold1
);
519 isl_qpolynomial_fold_free(fold2
);
523 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute_equalities(
524 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_basic_set
*eq
)
531 fold
= isl_qpolynomial_fold_cow(fold
);
535 for (i
= 0; i
< fold
->n
; ++i
) {
536 fold
->qp
[i
] = isl_qpolynomial_substitute_equalities(fold
->qp
[i
],
537 isl_basic_set_copy(eq
));
542 isl_basic_set_free(eq
);
545 isl_basic_set_free(eq
);
546 isl_qpolynomial_fold_free(fold
);
550 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist(
551 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
555 if (!fold
|| !context
)
558 fold
= isl_qpolynomial_fold_cow(fold
);
562 for (i
= 0; i
< fold
->n
; ++i
) {
563 fold
->qp
[i
] = isl_qpolynomial_gist(fold
->qp
[i
],
564 isl_set_copy(context
));
569 isl_set_free(context
);
572 isl_set_free(context
);
573 isl_qpolynomial_fold_free(fold
);
580 #define PW isl_pw_qpolynomial_fold
582 #define EL isl_qpolynomial_fold
584 #define IS_ZERO is_empty
588 #include <isl_pw_templ.c>
591 #define UNION isl_union_pw_qpolynomial_fold
593 #define PART isl_pw_qpolynomial_fold
595 #define PARTS pw_qpolynomial_fold
597 #include <isl_union_templ.c>
599 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_empty(enum isl_fold type
,
600 __isl_take isl_dim
*dim
)
602 return qpolynomial_fold_alloc(type
, dim
, 0);
605 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_alloc(
606 enum isl_fold type
, __isl_take isl_qpolynomial
*qp
)
608 isl_qpolynomial_fold
*fold
;
613 fold
= qpolynomial_fold_alloc(type
, isl_dim_copy(qp
->dim
), 1);
622 isl_qpolynomial_fold_free(fold
);
623 isl_qpolynomial_free(qp
);
627 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_copy(
628 __isl_keep isl_qpolynomial_fold
*fold
)
637 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_dup(
638 __isl_keep isl_qpolynomial_fold
*fold
)
641 isl_qpolynomial_fold
*dup
;
645 dup
= qpolynomial_fold_alloc(fold
->type
,
646 isl_dim_copy(fold
->dim
), fold
->n
);
651 for (i
= 0; i
< fold
->n
; ++i
) {
652 dup
->qp
[i
] = isl_qpolynomial_copy(fold
->qp
[i
]);
659 isl_qpolynomial_fold_free(dup
);
663 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_cow(
664 __isl_take isl_qpolynomial_fold
*fold
)
672 return isl_qpolynomial_fold_dup(fold
);
675 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold
*fold
)
684 for (i
= 0; i
< fold
->n
; ++i
)
685 isl_qpolynomial_free(fold
->qp
[i
]);
686 isl_dim_free(fold
->dim
);
690 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
698 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
699 __isl_take isl_qpolynomial_fold
*fold1
,
700 __isl_take isl_qpolynomial_fold
*fold2
)
703 struct isl_qpolynomial_fold
*res
= NULL
;
705 if (!fold1
|| !fold2
)
708 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
709 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
712 if (isl_qpolynomial_fold_is_empty(fold1
)) {
713 isl_qpolynomial_fold_free(fold1
);
717 if (isl_qpolynomial_fold_is_empty(fold2
)) {
718 isl_qpolynomial_fold_free(fold2
);
722 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
723 fold1
->n
+ fold2
->n
);
727 for (i
= 0; i
< fold1
->n
; ++i
) {
728 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
729 if (!res
->qp
[res
->n
])
734 for (i
= 0; i
< fold2
->n
; ++i
) {
735 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
736 if (!res
->qp
[res
->n
])
741 isl_qpolynomial_fold_free(fold1
);
742 isl_qpolynomial_fold_free(fold2
);
746 isl_qpolynomial_fold_free(res
);
747 isl_qpolynomial_fold_free(fold1
);
748 isl_qpolynomial_fold_free(fold2
);
752 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_fold(
753 __isl_take isl_pw_qpolynomial_fold
*pw1
,
754 __isl_take isl_pw_qpolynomial_fold
*pw2
)
757 struct isl_pw_qpolynomial_fold
*res
;
763 isl_assert(pw1
->dim
->ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
765 if (isl_pw_qpolynomial_fold_is_zero(pw1
)) {
766 isl_pw_qpolynomial_fold_free(pw1
);
770 if (isl_pw_qpolynomial_fold_is_zero(pw2
)) {
771 isl_pw_qpolynomial_fold_free(pw2
);
775 if (pw1
->type
!= pw2
->type
)
776 isl_die(set
->ctx
, isl_error_invalid
, "fold types don't match",
779 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
780 res
= isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pw1
->dim
),
783 for (i
= 0; i
< pw1
->n
; ++i
) {
784 set
= isl_set_copy(pw1
->p
[i
].set
);
785 for (j
= 0; j
< pw2
->n
; ++j
) {
786 struct isl_set
*common
;
787 isl_qpolynomial_fold
*sum
;
788 set
= isl_set_subtract(set
,
789 isl_set_copy(pw2
->p
[j
].set
));
790 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
791 isl_set_copy(pw2
->p
[j
].set
));
792 if (isl_set_plain_is_empty(common
)) {
793 isl_set_free(common
);
797 sum
= isl_qpolynomial_fold_fold_on_domain(common
,
798 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
),
799 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
801 res
= isl_pw_qpolynomial_fold_add_piece(res
, common
, sum
);
803 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
804 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
));
807 for (j
= 0; j
< pw2
->n
; ++j
) {
808 set
= isl_set_copy(pw2
->p
[j
].set
);
809 for (i
= 0; i
< pw1
->n
; ++i
)
810 set
= isl_set_subtract(set
, isl_set_copy(pw1
->p
[i
].set
));
811 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
812 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
815 isl_pw_qpolynomial_fold_free(pw1
);
816 isl_pw_qpolynomial_fold_free(pw2
);
820 isl_pw_qpolynomial_fold_free(pw1
);
821 isl_pw_qpolynomial_fold_free(pw2
);
825 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
826 __isl_take isl_union_pw_qpolynomial_fold
*u
,
827 __isl_take isl_pw_qpolynomial_fold
*part
)
830 struct isl_hash_table_entry
*entry
;
832 u
= isl_union_pw_qpolynomial_fold_cow(u
);
837 isl_assert(u
->dim
->ctx
, isl_dim_match(part
->dim
, isl_dim_param
, u
->dim
,
838 isl_dim_param
), goto error
);
840 hash
= isl_dim_get_hash(part
->dim
);
841 entry
= isl_hash_table_find(u
->dim
->ctx
, &u
->table
, hash
,
842 &has_dim
, part
->dim
, 1);
849 entry
->data
= isl_pw_qpolynomial_fold_fold(entry
->data
,
850 isl_pw_qpolynomial_fold_copy(part
));
853 isl_pw_qpolynomial_fold_free(part
);
858 isl_pw_qpolynomial_fold_free(part
);
859 isl_union_pw_qpolynomial_fold_free(u
);
863 static int fold_part(__isl_take isl_pw_qpolynomial_fold
*part
, void *user
)
865 isl_union_pw_qpolynomial_fold
**u
;
866 u
= (isl_union_pw_qpolynomial_fold
**)user
;
868 *u
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u
, part
);
873 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold(
874 __isl_take isl_union_pw_qpolynomial_fold
*u1
,
875 __isl_take isl_union_pw_qpolynomial_fold
*u2
)
877 u1
= isl_union_pw_qpolynomial_fold_cow(u1
);
882 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2
,
883 &fold_part
, &u1
) < 0)
886 isl_union_pw_qpolynomial_fold_free(u2
);
890 isl_union_pw_qpolynomial_fold_free(u1
);
891 isl_union_pw_qpolynomial_fold_free(u2
);
895 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
896 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
899 isl_pw_qpolynomial_fold
*pwf
;
904 pwf
= isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp
->dim
), type
,
907 for (i
= 0; i
< pwqp
->n
; ++i
)
908 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
909 isl_set_copy(pwqp
->p
[i
].set
),
910 isl_qpolynomial_fold_alloc(type
,
911 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
913 isl_pw_qpolynomial_free(pwqp
);
918 int isl_qpolynomial_fold_is_equal(__isl_keep isl_qpolynomial_fold
*fold1
,
919 __isl_keep isl_qpolynomial_fold
*fold2
)
923 if (!fold1
|| !fold2
)
926 if (fold1
->n
!= fold2
->n
)
929 /* We probably want to sort the qps first... */
930 for (i
= 0; i
< fold1
->n
; ++i
) {
931 int eq
= isl_qpolynomial_is_equal(fold1
->qp
[i
], fold2
->qp
[i
]);
939 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_eval(
940 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
946 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, fold
->dim
), goto error
);
947 isl_assert(pnt
->dim
->ctx
,
948 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
952 qp
= isl_qpolynomial_zero(isl_dim_copy(fold
->dim
));
955 qp
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
956 isl_point_copy(pnt
));
957 for (i
= 1; i
< fold
->n
; ++i
) {
958 isl_qpolynomial
*qp_i
;
959 qp_i
= isl_qpolynomial_eval(
960 isl_qpolynomial_copy(fold
->qp
[i
]),
961 isl_point_copy(pnt
));
962 if (fold
->type
== isl_fold_max
)
963 qp
= isl_qpolynomial_max_cst(qp
, qp_i
);
965 qp
= isl_qpolynomial_min_cst(qp
, qp_i
);
968 isl_qpolynomial_fold_free(fold
);
973 isl_qpolynomial_fold_free(fold
);
978 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
983 for (i
= 0; i
< pwf
->n
; ++i
)
984 n
+= pwf
->p
[i
].fold
->n
;
989 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_opt_on_domain(
990 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*set
, int max
)
993 isl_qpolynomial
*opt
;
999 isl_dim
*dim
= isl_dim_copy(fold
->dim
);
1001 isl_qpolynomial_fold_free(fold
);
1002 return isl_qpolynomial_zero(dim
);
1005 opt
= isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold
->qp
[0]),
1006 isl_set_copy(set
), max
);
1007 for (i
= 1; i
< fold
->n
; ++i
) {
1008 isl_qpolynomial
*opt_i
;
1009 opt_i
= isl_qpolynomial_opt_on_domain(
1010 isl_qpolynomial_copy(fold
->qp
[i
]),
1011 isl_set_copy(set
), max
);
1013 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
1015 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
1019 isl_qpolynomial_fold_free(fold
);
1024 isl_qpolynomial_fold_free(fold
);
1028 /* Check whether for each quasi-polynomial in "fold2" there is
1029 * a quasi-polynomial in "fold1" that dominates it on "set".
1031 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set
*set
,
1032 __isl_keep isl_qpolynomial_fold
*fold1
,
1033 __isl_keep isl_qpolynomial_fold
*fold2
)
1038 if (!set
|| !fold1
|| !fold2
)
1041 covers
= fold1
->type
== isl_fold_max
? 1 : -1;
1043 for (i
= 0; i
< fold2
->n
; ++i
) {
1044 for (j
= 0; j
< fold1
->n
; ++j
) {
1048 d
= isl_qpolynomial_sub(
1049 isl_qpolynomial_copy(fold1
->qp
[j
]),
1050 isl_qpolynomial_copy(fold2
->qp
[i
]));
1051 sgn
= isl_qpolynomial_sign(set
, d
);
1052 isl_qpolynomial_free(d
);
1063 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1064 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1067 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold
*pwf1
,
1068 __isl_keep isl_pw_qpolynomial_fold
*pwf2
)
1071 isl_set
*dom1
, *dom2
;
1082 dom1
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1
));
1083 dom2
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2
));
1084 is_subset
= isl_set_is_subset(dom2
, dom1
);
1088 if (is_subset
< 0 || !is_subset
)
1091 for (i
= 0; i
< pwf2
->n
; ++i
) {
1092 for (j
= 0; j
< pwf1
->n
; ++j
) {
1097 common
= isl_set_intersect(isl_set_copy(pwf1
->p
[j
].set
),
1098 isl_set_copy(pwf2
->p
[i
].set
));
1099 is_empty
= isl_set_is_empty(common
);
1100 if (is_empty
< 0 || is_empty
) {
1101 isl_set_free(common
);
1106 covers
= qpolynomial_fold_covers_on_domain(common
,
1107 pwf1
->p
[j
].fold
, pwf2
->p
[i
].fold
);
1108 isl_set_free(common
);
1109 if (covers
< 0 || !covers
)
1117 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_morph(
1118 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_morph
*morph
)
1123 if (!fold
|| !morph
)
1126 ctx
= fold
->dim
->ctx
;
1127 isl_assert(ctx
, isl_dim_equal(fold
->dim
, morph
->dom
->dim
), goto error
);
1129 fold
= isl_qpolynomial_fold_cow(fold
);
1133 isl_dim_free(fold
->dim
);
1134 fold
->dim
= isl_dim_copy(morph
->ran
->dim
);
1138 for (i
= 0; i
< fold
->n
; ++i
) {
1139 fold
->qp
[i
] = isl_qpolynomial_morph(fold
->qp
[i
],
1140 isl_morph_copy(morph
));
1145 isl_morph_free(morph
);
1149 isl_qpolynomial_fold_free(fold
);
1150 isl_morph_free(morph
);
1154 enum isl_fold
isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold
*fold
)
1157 return isl_fold_list
;
1161 enum isl_fold
isl_union_pw_qpolynomial_fold_get_type(
1162 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1165 return isl_fold_list
;
1169 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_lift(
1170 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_dim
*dim
)
1178 if (isl_dim_equal(fold
->dim
, dim
)) {
1183 fold
= isl_qpolynomial_fold_cow(fold
);
1187 isl_dim_free(fold
->dim
);
1188 fold
->dim
= isl_dim_copy(dim
);
1192 for (i
= 0; i
< fold
->n
; ++i
) {
1193 fold
->qp
[i
] = isl_qpolynomial_lift(fold
->qp
[i
],
1203 isl_qpolynomial_fold_free(fold
);
1208 int isl_qpolynomial_fold_foreach_qpolynomial(
1209 __isl_keep isl_qpolynomial_fold
*fold
,
1210 int (*fn
)(__isl_take isl_qpolynomial
*qp
, void *user
), void *user
)
1217 for (i
= 0; i
< fold
->n
; ++i
)
1218 if (fn(isl_qpolynomial_copy(fold
->qp
[i
]), user
) < 0)
1224 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_move_dims(
1225 __isl_take isl_qpolynomial_fold
*fold
,
1226 enum isl_dim_type dst_type
, unsigned dst_pos
,
1227 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1234 fold
= isl_qpolynomial_fold_cow(fold
);
1238 fold
->dim
= isl_dim_move(fold
->dim
, dst_type
, dst_pos
,
1239 src_type
, src_pos
, n
);
1243 for (i
= 0; i
< fold
->n
; ++i
) {
1244 fold
->qp
[i
] = isl_qpolynomial_move_dims(fold
->qp
[i
],
1245 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1252 isl_qpolynomial_fold_free(fold
);
1256 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1257 * in fold->qp[k] by subs[i].
1259 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute(
1260 __isl_take isl_qpolynomial_fold
*fold
,
1261 enum isl_dim_type type
, unsigned first
, unsigned n
,
1262 __isl_keep isl_qpolynomial
**subs
)
1269 fold
= isl_qpolynomial_fold_cow(fold
);
1273 for (i
= 0; i
< fold
->n
; ++i
) {
1274 fold
->qp
[i
] = isl_qpolynomial_substitute(fold
->qp
[i
],
1275 type
, first
, n
, subs
);
1282 isl_qpolynomial_fold_free(fold
);
1286 static int add_pwqp(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1289 isl_pw_qpolynomial_fold
*pwf
;
1290 isl_union_pw_qpolynomial_fold
**upwf
;
1292 struct isl_hash_table_entry
*entry
;
1294 upwf
= (isl_union_pw_qpolynomial_fold
**)user
;
1296 ctx
= pwqp
->dim
->ctx
;
1297 hash
= isl_dim_get_hash(pwqp
->dim
);
1298 entry
= isl_hash_table_find(ctx
, &(*upwf
)->table
,
1299 hash
, &has_dim
, pwqp
->dim
, 1);
1303 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf
)->type
, pwqp
);
1307 entry
->data
= isl_pw_qpolynomial_fold_add(entry
->data
, pwf
);
1310 if (isl_pw_qpolynomial_fold_is_zero(entry
->data
)) {
1311 isl_pw_qpolynomial_fold_free(entry
->data
);
1312 isl_hash_table_remove(ctx
, &(*upwf
)->table
, entry
);
1318 isl_pw_qpolynomial_free(pwqp
);
1322 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1323 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
1324 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1326 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1327 isl_union_pw_qpolynomial_get_dim(upwqp
));
1328 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1329 isl_union_pw_qpolynomial_fold_get_dim(upwf
));
1331 upwf
= isl_union_pw_qpolynomial_fold_cow(upwf
);
1332 if (!upwf
|| !upwqp
)
1335 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &add_pwqp
,
1339 isl_union_pw_qpolynomial_free(upwqp
);
1343 isl_union_pw_qpolynomial_fold_free(upwf
);
1344 isl_union_pw_qpolynomial_free(upwqp
);
1348 static int compatible_range(__isl_keep isl_dim
*dim1
, __isl_keep isl_dim
*dim2
)
1351 m
= isl_dim_match(dim1
, isl_dim_param
, dim2
, isl_dim_param
);
1354 return isl_dim_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_set
);
1357 /* Compute the intersection of the range of the map and the domain
1358 * of the piecewise quasipolynomial reduction and then compute a bound
1359 * on the associated quasipolynomial reduction over all elements
1360 * in this intersection.
1362 * We first introduce some unconstrained dimensions in the
1363 * piecewise quasipolynomial, intersect the resulting domain
1364 * with the wrapped map and the compute the sum.
1366 __isl_give isl_pw_qpolynomial_fold
*isl_map_apply_pw_qpolynomial_fold(
1367 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1377 ctx
= isl_map_get_ctx(map
);
1381 map_dim
= isl_map_get_dim(map
);
1382 pwf_dim
= isl_pw_qpolynomial_fold_get_dim(pwf
);
1383 ok
= compatible_range(map_dim
, pwf_dim
);
1384 isl_dim_free(map_dim
);
1385 isl_dim_free(pwf_dim
);
1387 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
1390 n_in
= isl_map_dim(map
, isl_dim_in
);
1391 pwf
= isl_pw_qpolynomial_fold_insert_dims(pwf
, isl_dim_set
, 0, n_in
);
1393 dom
= isl_map_wrap(map
);
1394 pwf
= isl_pw_qpolynomial_fold_reset_dim(pwf
, isl_set_get_dim(dom
));
1396 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, dom
);
1397 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, tight
);
1402 isl_pw_qpolynomial_fold_free(pwf
);
1406 __isl_give isl_pw_qpolynomial_fold
*isl_set_apply_pw_qpolynomial_fold(
1407 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1412 map
= isl_map_from_range(set
);
1413 return isl_map_apply_pw_qpolynomial_fold(map
, pwf
, tight
);
1416 struct isl_apply_fold_data
{
1417 isl_union_pw_qpolynomial_fold
*upwf
;
1418 isl_union_pw_qpolynomial_fold
*res
;
1423 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold
*pwf
,
1428 struct isl_apply_fold_data
*data
= user
;
1431 map_dim
= isl_map_get_dim(data
->map
);
1432 pwf_dim
= isl_pw_qpolynomial_fold_get_dim(pwf
);
1433 ok
= compatible_range(map_dim
, pwf_dim
);
1434 isl_dim_free(map_dim
);
1435 isl_dim_free(pwf_dim
);
1438 pwf
= isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data
->map
),
1439 pwf
, data
->tight
? &data
->tight
: NULL
);
1440 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1443 isl_pw_qpolynomial_fold_free(pwf
);
1448 static int map_apply(__isl_take isl_map
*map
, void *user
)
1450 struct isl_apply_fold_data
*data
= user
;
1454 r
= isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1455 data
->upwf
, &pw_qpolynomial_fold_apply
, data
);
1461 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_map_apply_union_pw_qpolynomial_fold(
1462 __isl_take isl_union_map
*umap
,
1463 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1467 struct isl_apply_fold_data data
;
1469 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1470 isl_union_map_get_dim(umap
));
1471 umap
= isl_union_map_align_params(umap
,
1472 isl_union_pw_qpolynomial_fold_get_dim(upwf
));
1475 data
.tight
= tight
? 1 : 0;
1476 dim
= isl_union_pw_qpolynomial_fold_get_dim(upwf
);
1477 type
= isl_union_pw_qpolynomial_fold_get_type(upwf
);
1478 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
1479 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1482 isl_union_map_free(umap
);
1483 isl_union_pw_qpolynomial_fold_free(upwf
);
1486 *tight
= data
.tight
;
1490 isl_union_map_free(umap
);
1491 isl_union_pw_qpolynomial_fold_free(upwf
);
1492 isl_union_pw_qpolynomial_fold_free(data
.res
);
1496 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_set_apply_union_pw_qpolynomial_fold(
1497 __isl_take isl_union_set
*uset
,
1498 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1500 return isl_union_map_apply_union_pw_qpolynomial_fold(uset
, upwf
, tight
);
1503 /* Reorder the dimension of "fold" according to the given reordering.
1505 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_realign(
1506 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_reordering
*r
)
1510 fold
= isl_qpolynomial_fold_cow(fold
);
1514 for (i
= 0; i
< fold
->n
; ++i
) {
1515 fold
->qp
[i
] = isl_qpolynomial_realign(fold
->qp
[i
],
1516 isl_reordering_copy(r
));
1521 fold
= isl_qpolynomial_fold_reset_dim(fold
, isl_dim_copy(r
->dim
));
1523 isl_reordering_free(r
);
1527 isl_qpolynomial_fold_free(fold
);
1528 isl_reordering_free(r
);
1532 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_mul_isl_int(
1533 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1537 if (isl_int_is_one(v
))
1539 if (fold
&& isl_int_is_zero(v
)) {
1540 isl_qpolynomial_fold
*zero
;
1541 isl_dim
*dim
= isl_dim_copy(fold
->dim
);
1542 zero
= isl_qpolynomial_fold_empty(fold
->type
, dim
);
1543 isl_qpolynomial_fold_free(fold
);
1547 fold
= isl_qpolynomial_fold_cow(fold
);
1551 if (isl_int_is_neg(v
))
1552 fold
->type
= isl_fold_type_negate(fold
->type
);
1553 for (i
= 0; i
< fold
->n
; ++i
) {
1554 fold
->qp
[i
] = isl_qpolynomial_mul_isl_int(fold
->qp
[i
], v
);
1561 isl_qpolynomial_fold_free(fold
);