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>
19 #include <isl_config.h>
21 enum isl_fold
isl_fold_type_negate(enum isl_fold type
)
32 isl_die(NULL
, isl_error_internal
, "unhandled isl_fold type", abort());
35 static __isl_give isl_qpolynomial_fold
*qpolynomial_fold_alloc(
36 enum isl_fold type
, __isl_take isl_dim
*dim
, int n
)
38 isl_qpolynomial_fold
*fold
;
43 isl_assert(dim
->ctx
, n
>= 0, goto error
);
44 fold
= isl_calloc(dim
->ctx
, struct isl_qpolynomial_fold
,
45 sizeof(struct isl_qpolynomial_fold
) +
46 (n
- 1) * sizeof(struct isl_qpolynomial
*));
62 isl_ctx
*isl_qpolynomial_fold_get_ctx(__isl_keep isl_qpolynomial_fold
*fold
)
64 return fold
? fold
->dim
->ctx
: NULL
;
67 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_reset_dim(
68 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_dim
*dim
)
72 fold
= isl_qpolynomial_fold_cow(fold
);
76 for (i
= 0; i
< fold
->n
; ++i
) {
77 fold
->qp
[i
] = isl_qpolynomial_reset_dim(fold
->qp
[i
],
83 isl_dim_free(fold
->dim
);
88 isl_qpolynomial_fold_free(fold
);
93 int isl_qpolynomial_fold_involves_dims(__isl_keep isl_qpolynomial_fold
*fold
,
94 enum isl_dim_type type
, unsigned first
, unsigned n
)
100 if (fold
->n
== 0 || n
== 0)
103 for (i
= 0; i
< fold
->n
; ++i
) {
104 int involves
= isl_qpolynomial_involves_dims(fold
->qp
[i
],
106 if (involves
< 0 || involves
)
112 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_set_dim_name(
113 __isl_take isl_qpolynomial_fold
*fold
,
114 enum isl_dim_type type
, unsigned pos
, const char *s
)
118 fold
= isl_qpolynomial_fold_cow(fold
);
121 fold
->dim
= isl_dim_set_name(fold
->dim
, type
, pos
, s
);
125 for (i
= 0; i
< fold
->n
; ++i
) {
126 fold
->qp
[i
] = isl_qpolynomial_set_dim_name(fold
->qp
[i
],
134 isl_qpolynomial_fold_free(fold
);
138 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_drop_dims(
139 __isl_take isl_qpolynomial_fold
*fold
,
140 enum isl_dim_type type
, unsigned first
, unsigned n
)
149 fold
= isl_qpolynomial_fold_cow(fold
);
152 fold
->dim
= isl_dim_drop(fold
->dim
, type
, first
, n
);
156 for (i
= 0; i
< fold
->n
; ++i
) {
157 fold
->qp
[i
] = isl_qpolynomial_drop_dims(fold
->qp
[i
],
165 isl_qpolynomial_fold_free(fold
);
169 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_insert_dims(
170 __isl_take isl_qpolynomial_fold
*fold
,
171 enum isl_dim_type type
, unsigned first
, unsigned n
)
177 if (n
== 0 && !isl_dim_is_named_or_nested(fold
->dim
, type
))
180 fold
= isl_qpolynomial_fold_cow(fold
);
183 fold
->dim
= isl_dim_insert(fold
->dim
, type
, first
, n
);
187 for (i
= 0; i
< fold
->n
; ++i
) {
188 fold
->qp
[i
] = isl_qpolynomial_insert_dims(fold
->qp
[i
],
196 isl_qpolynomial_fold_free(fold
);
200 static int isl_qpolynomial_cst_sign(__isl_keep isl_qpolynomial
*qp
)
202 struct isl_upoly_cst
*cst
;
204 cst
= isl_upoly_as_cst(qp
->upoly
);
208 return isl_int_sgn(cst
->n
) < 0 ? -1 : 1;
211 static int isl_qpolynomial_aff_sign(__isl_keep isl_set
*set
,
212 __isl_keep isl_qpolynomial
*qp
)
214 enum isl_lp_result res
;
219 aff
= isl_qpolynomial_extract_affine(qp
);
225 res
= isl_set_solve_lp(set
, 0, aff
->el
+ 1, aff
->el
[0],
227 if (res
== isl_lp_error
)
229 if (res
== isl_lp_empty
||
230 (res
== isl_lp_ok
&& !isl_int_is_neg(opt
))) {
235 res
= isl_set_solve_lp(set
, 1, aff
->el
+ 1, aff
->el
[0],
237 if (res
== isl_lp_ok
&& !isl_int_is_pos(opt
))
246 /* Determine, if possible, the sign of the quasipolynomial "qp" on
249 * If qp is a constant, then the problem is trivial.
250 * If qp is linear, then we check if the minimum of the corresponding
251 * affine constraint is non-negative or if the maximum is non-positive.
253 * Otherwise, we check if the outermost variable "v" has a lower bound "l"
254 * in "set". If so, we write qp(v,v') as
256 * q(v,v') * (v - l) + r(v')
258 * if q(v,v') and r(v') have the same known sign, then the original
259 * quasipolynomial has the same sign as well.
266 static int isl_qpolynomial_sign(__isl_keep isl_set
*set
,
267 __isl_keep isl_qpolynomial
*qp
)
272 struct isl_upoly_rec
*rec
;
275 enum isl_lp_result res
;
278 is
= isl_qpolynomial_is_cst(qp
, NULL
, NULL
);
282 return isl_qpolynomial_cst_sign(qp
);
284 is
= isl_qpolynomial_is_affine(qp
);
288 return isl_qpolynomial_aff_sign(set
, qp
);
290 if (qp
->div
->n_row
> 0)
293 rec
= isl_upoly_as_rec(qp
->upoly
);
297 d
= isl_dim_total(qp
->dim
);
298 v
= isl_vec_alloc(set
->ctx
, 2 + d
);
302 isl_seq_clr(v
->el
+ 1, 1 + d
);
303 isl_int_set_si(v
->el
[0], 1);
304 isl_int_set_si(v
->el
[2 + qp
->upoly
->var
], 1);
308 res
= isl_set_solve_lp(set
, 0, v
->el
+ 1, v
->el
[0], &l
, NULL
, NULL
);
309 if (res
== isl_lp_ok
) {
310 isl_qpolynomial
*min
;
311 isl_qpolynomial
*base
;
312 isl_qpolynomial
*r
, *q
;
315 min
= isl_qpolynomial_cst(isl_dim_copy(qp
->dim
), l
);
316 base
= isl_qpolynomial_var_pow(isl_dim_copy(qp
->dim
),
319 r
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), 0,
320 isl_upoly_copy(rec
->p
[rec
->n
- 1]));
321 q
= isl_qpolynomial_copy(r
);
323 for (i
= rec
->n
- 2; i
>= 0; --i
) {
324 r
= isl_qpolynomial_mul(r
, isl_qpolynomial_copy(min
));
325 t
= isl_qpolynomial_alloc(isl_dim_copy(qp
->dim
), 0,
326 isl_upoly_copy(rec
->p
[i
]));
327 r
= isl_qpolynomial_add(r
, t
);
330 q
= isl_qpolynomial_mul(q
, isl_qpolynomial_copy(base
));
331 q
= isl_qpolynomial_add(q
, isl_qpolynomial_copy(r
));
334 if (isl_qpolynomial_is_zero(q
))
335 sgn
= isl_qpolynomial_sign(set
, r
);
336 else if (isl_qpolynomial_is_zero(r
))
337 sgn
= isl_qpolynomial_sign(set
, q
);
340 sgn_r
= isl_qpolynomial_sign(set
, r
);
341 sgn_q
= isl_qpolynomial_sign(set
, q
);
346 isl_qpolynomial_free(min
);
347 isl_qpolynomial_free(base
);
348 isl_qpolynomial_free(q
);
349 isl_qpolynomial_free(r
);
359 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold_on_domain(
360 __isl_keep isl_set
*set
,
361 __isl_take isl_qpolynomial_fold
*fold1
,
362 __isl_take isl_qpolynomial_fold
*fold2
)
366 struct isl_qpolynomial_fold
*res
= NULL
;
369 if (!fold1
|| !fold2
)
372 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
373 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
376 better
= fold1
->type
== isl_fold_max
? -1 : 1;
378 if (isl_qpolynomial_fold_is_empty(fold1
)) {
379 isl_qpolynomial_fold_free(fold1
);
383 if (isl_qpolynomial_fold_is_empty(fold2
)) {
384 isl_qpolynomial_fold_free(fold2
);
388 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
389 fold1
->n
+ fold2
->n
);
393 for (i
= 0; i
< fold1
->n
; ++i
) {
394 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
395 if (!res
->qp
[res
->n
])
401 for (i
= 0; i
< fold2
->n
; ++i
) {
402 for (j
= n1
- 1; j
>= 0; --j
) {
405 d
= isl_qpolynomial_sub(
406 isl_qpolynomial_copy(res
->qp
[j
]),
407 isl_qpolynomial_copy(fold2
->qp
[i
]));
408 sgn
= isl_qpolynomial_sign(set
, d
);
409 isl_qpolynomial_free(d
);
414 isl_qpolynomial_free(res
->qp
[j
]);
416 res
->qp
[j
] = res
->qp
[n1
- 1];
418 if (n1
!= res
->n
- 1)
419 res
->qp
[n1
] = res
->qp
[res
->n
- 1];
424 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
425 if (!res
->qp
[res
->n
])
430 isl_qpolynomial_fold_free(fold1
);
431 isl_qpolynomial_fold_free(fold2
);
435 isl_qpolynomial_fold_free(res
);
436 isl_qpolynomial_fold_free(fold1
);
437 isl_qpolynomial_fold_free(fold2
);
441 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_qpolynomial(
442 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_qpolynomial
*qp
)
449 if (isl_qpolynomial_is_zero(qp
)) {
450 isl_qpolynomial_free(qp
);
454 fold
= isl_qpolynomial_fold_cow(fold
);
458 for (i
= 0; i
< fold
->n
; ++i
) {
459 fold
->qp
[i
] = isl_qpolynomial_add(fold
->qp
[i
],
460 isl_qpolynomial_copy(qp
));
465 isl_qpolynomial_free(qp
);
468 isl_qpolynomial_fold_free(fold
);
469 isl_qpolynomial_free(qp
);
473 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_add_on_domain(
474 __isl_keep isl_set
*dom
,
475 __isl_take isl_qpolynomial_fold
*fold1
,
476 __isl_take isl_qpolynomial_fold
*fold2
)
479 isl_qpolynomial_fold
*res
= NULL
;
481 if (!fold1
|| !fold2
)
484 if (isl_qpolynomial_fold_is_empty(fold1
)) {
485 isl_qpolynomial_fold_free(fold1
);
489 if (isl_qpolynomial_fold_is_empty(fold2
)) {
490 isl_qpolynomial_fold_free(fold2
);
494 if (fold1
->n
== 1 && fold2
->n
!= 1)
495 return isl_qpolynomial_fold_add_on_domain(dom
, fold2
, fold1
);
498 res
= isl_qpolynomial_fold_add_qpolynomial(fold1
,
499 isl_qpolynomial_copy(fold2
->qp
[0]));
500 isl_qpolynomial_fold_free(fold2
);
504 res
= isl_qpolynomial_fold_add_qpolynomial(
505 isl_qpolynomial_fold_copy(fold1
),
506 isl_qpolynomial_copy(fold2
->qp
[0]));
508 for (i
= 1; i
< fold2
->n
; ++i
) {
509 isl_qpolynomial_fold
*res_i
;
510 res_i
= isl_qpolynomial_fold_add_qpolynomial(
511 isl_qpolynomial_fold_copy(fold1
),
512 isl_qpolynomial_copy(fold2
->qp
[i
]));
513 res
= isl_qpolynomial_fold_fold_on_domain(dom
, res
, res_i
);
516 isl_qpolynomial_fold_free(fold1
);
517 isl_qpolynomial_fold_free(fold2
);
520 isl_qpolynomial_fold_free(res
);
521 isl_qpolynomial_fold_free(fold1
);
522 isl_qpolynomial_fold_free(fold2
);
526 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute_equalities(
527 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_basic_set
*eq
)
534 fold
= isl_qpolynomial_fold_cow(fold
);
538 for (i
= 0; i
< fold
->n
; ++i
) {
539 fold
->qp
[i
] = isl_qpolynomial_substitute_equalities(fold
->qp
[i
],
540 isl_basic_set_copy(eq
));
545 isl_basic_set_free(eq
);
548 isl_basic_set_free(eq
);
549 isl_qpolynomial_fold_free(fold
);
553 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_gist(
554 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*context
)
558 if (!fold
|| !context
)
561 fold
= isl_qpolynomial_fold_cow(fold
);
565 for (i
= 0; i
< fold
->n
; ++i
) {
566 fold
->qp
[i
] = isl_qpolynomial_gist(fold
->qp
[i
],
567 isl_set_copy(context
));
572 isl_set_free(context
);
575 isl_set_free(context
);
576 isl_qpolynomial_fold_free(fold
);
583 #define PW isl_pw_qpolynomial_fold
585 #define EL isl_qpolynomial_fold
587 #define IS_ZERO is_empty
591 #include <isl_pw_templ.c>
594 #define UNION isl_union_pw_qpolynomial_fold
596 #define PART isl_pw_qpolynomial_fold
598 #define PARTS pw_qpolynomial_fold
600 #include <isl_union_templ.c>
602 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_empty(enum isl_fold type
,
603 __isl_take isl_dim
*dim
)
605 return qpolynomial_fold_alloc(type
, dim
, 0);
608 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_alloc(
609 enum isl_fold type
, __isl_take isl_qpolynomial
*qp
)
611 isl_qpolynomial_fold
*fold
;
616 fold
= qpolynomial_fold_alloc(type
, isl_dim_copy(qp
->dim
), 1);
625 isl_qpolynomial_fold_free(fold
);
626 isl_qpolynomial_free(qp
);
630 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_copy(
631 __isl_keep isl_qpolynomial_fold
*fold
)
640 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_dup(
641 __isl_keep isl_qpolynomial_fold
*fold
)
644 isl_qpolynomial_fold
*dup
;
648 dup
= qpolynomial_fold_alloc(fold
->type
,
649 isl_dim_copy(fold
->dim
), fold
->n
);
654 for (i
= 0; i
< fold
->n
; ++i
) {
655 dup
->qp
[i
] = isl_qpolynomial_copy(fold
->qp
[i
]);
662 isl_qpolynomial_fold_free(dup
);
666 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_cow(
667 __isl_take isl_qpolynomial_fold
*fold
)
675 return isl_qpolynomial_fold_dup(fold
);
678 void isl_qpolynomial_fold_free(__isl_take isl_qpolynomial_fold
*fold
)
687 for (i
= 0; i
< fold
->n
; ++i
)
688 isl_qpolynomial_free(fold
->qp
[i
]);
689 isl_dim_free(fold
->dim
);
693 int isl_qpolynomial_fold_is_empty(__isl_keep isl_qpolynomial_fold
*fold
)
701 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_fold(
702 __isl_take isl_qpolynomial_fold
*fold1
,
703 __isl_take isl_qpolynomial_fold
*fold2
)
706 struct isl_qpolynomial_fold
*res
= NULL
;
708 if (!fold1
|| !fold2
)
711 isl_assert(fold1
->dim
->ctx
, fold1
->type
== fold2
->type
, goto error
);
712 isl_assert(fold1
->dim
->ctx
, isl_dim_equal(fold1
->dim
, fold2
->dim
),
715 if (isl_qpolynomial_fold_is_empty(fold1
)) {
716 isl_qpolynomial_fold_free(fold1
);
720 if (isl_qpolynomial_fold_is_empty(fold2
)) {
721 isl_qpolynomial_fold_free(fold2
);
725 res
= qpolynomial_fold_alloc(fold1
->type
, isl_dim_copy(fold1
->dim
),
726 fold1
->n
+ fold2
->n
);
730 for (i
= 0; i
< fold1
->n
; ++i
) {
731 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold1
->qp
[i
]);
732 if (!res
->qp
[res
->n
])
737 for (i
= 0; i
< fold2
->n
; ++i
) {
738 res
->qp
[res
->n
] = isl_qpolynomial_copy(fold2
->qp
[i
]);
739 if (!res
->qp
[res
->n
])
744 isl_qpolynomial_fold_free(fold1
);
745 isl_qpolynomial_fold_free(fold2
);
749 isl_qpolynomial_fold_free(res
);
750 isl_qpolynomial_fold_free(fold1
);
751 isl_qpolynomial_fold_free(fold2
);
755 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_fold(
756 __isl_take isl_pw_qpolynomial_fold
*pw1
,
757 __isl_take isl_pw_qpolynomial_fold
*pw2
)
760 struct isl_pw_qpolynomial_fold
*res
;
766 isl_assert(pw1
->dim
->ctx
, isl_dim_equal(pw1
->dim
, pw2
->dim
), goto error
);
768 if (isl_pw_qpolynomial_fold_is_zero(pw1
)) {
769 isl_pw_qpolynomial_fold_free(pw1
);
773 if (isl_pw_qpolynomial_fold_is_zero(pw2
)) {
774 isl_pw_qpolynomial_fold_free(pw2
);
778 if (pw1
->type
!= pw2
->type
)
779 isl_die(pw1
->dim
->ctx
, isl_error_invalid
,
780 "fold types don't match", goto error
);
782 n
= (pw1
->n
+ 1) * (pw2
->n
+ 1);
783 res
= isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pw1
->dim
),
786 for (i
= 0; i
< pw1
->n
; ++i
) {
787 set
= isl_set_copy(pw1
->p
[i
].set
);
788 for (j
= 0; j
< pw2
->n
; ++j
) {
789 struct isl_set
*common
;
790 isl_qpolynomial_fold
*sum
;
791 set
= isl_set_subtract(set
,
792 isl_set_copy(pw2
->p
[j
].set
));
793 common
= isl_set_intersect(isl_set_copy(pw1
->p
[i
].set
),
794 isl_set_copy(pw2
->p
[j
].set
));
795 if (isl_set_plain_is_empty(common
)) {
796 isl_set_free(common
);
800 sum
= isl_qpolynomial_fold_fold_on_domain(common
,
801 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
),
802 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
804 res
= isl_pw_qpolynomial_fold_add_piece(res
, common
, sum
);
806 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
807 isl_qpolynomial_fold_copy(pw1
->p
[i
].fold
));
810 for (j
= 0; j
< pw2
->n
; ++j
) {
811 set
= isl_set_copy(pw2
->p
[j
].set
);
812 for (i
= 0; i
< pw1
->n
; ++i
)
813 set
= isl_set_subtract(set
, isl_set_copy(pw1
->p
[i
].set
));
814 res
= isl_pw_qpolynomial_fold_add_piece(res
, set
,
815 isl_qpolynomial_fold_copy(pw2
->p
[j
].fold
));
818 isl_pw_qpolynomial_fold_free(pw1
);
819 isl_pw_qpolynomial_fold_free(pw2
);
823 isl_pw_qpolynomial_fold_free(pw1
);
824 isl_pw_qpolynomial_fold_free(pw2
);
828 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
829 __isl_take isl_union_pw_qpolynomial_fold
*u
,
830 __isl_take isl_pw_qpolynomial_fold
*part
)
833 struct isl_hash_table_entry
*entry
;
835 u
= isl_union_pw_qpolynomial_fold_cow(u
);
840 isl_assert(u
->dim
->ctx
, isl_dim_match(part
->dim
, isl_dim_param
, u
->dim
,
841 isl_dim_param
), goto error
);
843 hash
= isl_dim_get_hash(part
->dim
);
844 entry
= isl_hash_table_find(u
->dim
->ctx
, &u
->table
, hash
,
845 &has_dim
, part
->dim
, 1);
852 entry
->data
= isl_pw_qpolynomial_fold_fold(entry
->data
,
853 isl_pw_qpolynomial_fold_copy(part
));
856 isl_pw_qpolynomial_fold_free(part
);
861 isl_pw_qpolynomial_fold_free(part
);
862 isl_union_pw_qpolynomial_fold_free(u
);
866 static int fold_part(__isl_take isl_pw_qpolynomial_fold
*part
, void *user
)
868 isl_union_pw_qpolynomial_fold
**u
;
869 u
= (isl_union_pw_qpolynomial_fold
**)user
;
871 *u
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(*u
, part
);
876 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_fold(
877 __isl_take isl_union_pw_qpolynomial_fold
*u1
,
878 __isl_take isl_union_pw_qpolynomial_fold
*u2
)
880 u1
= isl_union_pw_qpolynomial_fold_cow(u1
);
885 if (isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(u2
,
886 &fold_part
, &u1
) < 0)
889 isl_union_pw_qpolynomial_fold_free(u2
);
893 isl_union_pw_qpolynomial_fold_free(u1
);
894 isl_union_pw_qpolynomial_fold_free(u2
);
898 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_from_pw_qpolynomial(
899 enum isl_fold type
, __isl_take isl_pw_qpolynomial
*pwqp
)
902 isl_pw_qpolynomial_fold
*pwf
;
907 pwf
= isl_pw_qpolynomial_fold_alloc_(isl_dim_copy(pwqp
->dim
), type
,
910 for (i
= 0; i
< pwqp
->n
; ++i
)
911 pwf
= isl_pw_qpolynomial_fold_add_piece(pwf
,
912 isl_set_copy(pwqp
->p
[i
].set
),
913 isl_qpolynomial_fold_alloc(type
,
914 isl_qpolynomial_copy(pwqp
->p
[i
].qp
)));
916 isl_pw_qpolynomial_free(pwqp
);
921 int isl_qpolynomial_fold_is_equal(__isl_keep isl_qpolynomial_fold
*fold1
,
922 __isl_keep isl_qpolynomial_fold
*fold2
)
926 if (!fold1
|| !fold2
)
929 if (fold1
->n
!= fold2
->n
)
932 /* We probably want to sort the qps first... */
933 for (i
= 0; i
< fold1
->n
; ++i
) {
934 int eq
= isl_qpolynomial_is_equal(fold1
->qp
[i
], fold2
->qp
[i
]);
942 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_eval(
943 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_point
*pnt
)
949 isl_assert(pnt
->dim
->ctx
, isl_dim_equal(pnt
->dim
, fold
->dim
), goto error
);
950 isl_assert(pnt
->dim
->ctx
,
951 fold
->type
== isl_fold_max
|| fold
->type
== isl_fold_min
,
955 qp
= isl_qpolynomial_zero(isl_dim_copy(fold
->dim
));
958 qp
= isl_qpolynomial_eval(isl_qpolynomial_copy(fold
->qp
[0]),
959 isl_point_copy(pnt
));
960 for (i
= 1; i
< fold
->n
; ++i
) {
961 isl_qpolynomial
*qp_i
;
962 qp_i
= isl_qpolynomial_eval(
963 isl_qpolynomial_copy(fold
->qp
[i
]),
964 isl_point_copy(pnt
));
965 if (fold
->type
== isl_fold_max
)
966 qp
= isl_qpolynomial_max_cst(qp
, qp_i
);
968 qp
= isl_qpolynomial_min_cst(qp
, qp_i
);
971 isl_qpolynomial_fold_free(fold
);
976 isl_qpolynomial_fold_free(fold
);
981 size_t isl_pw_qpolynomial_fold_size(__isl_keep isl_pw_qpolynomial_fold
*pwf
)
986 for (i
= 0; i
< pwf
->n
; ++i
)
987 n
+= pwf
->p
[i
].fold
->n
;
992 __isl_give isl_qpolynomial
*isl_qpolynomial_fold_opt_on_domain(
993 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_set
*set
, int max
)
996 isl_qpolynomial
*opt
;
1002 isl_dim
*dim
= isl_dim_copy(fold
->dim
);
1004 isl_qpolynomial_fold_free(fold
);
1005 return isl_qpolynomial_zero(dim
);
1008 opt
= isl_qpolynomial_opt_on_domain(isl_qpolynomial_copy(fold
->qp
[0]),
1009 isl_set_copy(set
), max
);
1010 for (i
= 1; i
< fold
->n
; ++i
) {
1011 isl_qpolynomial
*opt_i
;
1012 opt_i
= isl_qpolynomial_opt_on_domain(
1013 isl_qpolynomial_copy(fold
->qp
[i
]),
1014 isl_set_copy(set
), max
);
1016 opt
= isl_qpolynomial_max_cst(opt
, opt_i
);
1018 opt
= isl_qpolynomial_min_cst(opt
, opt_i
);
1022 isl_qpolynomial_fold_free(fold
);
1027 isl_qpolynomial_fold_free(fold
);
1031 /* Check whether for each quasi-polynomial in "fold2" there is
1032 * a quasi-polynomial in "fold1" that dominates it on "set".
1034 static int qpolynomial_fold_covers_on_domain(__isl_keep isl_set
*set
,
1035 __isl_keep isl_qpolynomial_fold
*fold1
,
1036 __isl_keep isl_qpolynomial_fold
*fold2
)
1041 if (!set
|| !fold1
|| !fold2
)
1044 covers
= fold1
->type
== isl_fold_max
? 1 : -1;
1046 for (i
= 0; i
< fold2
->n
; ++i
) {
1047 for (j
= 0; j
< fold1
->n
; ++j
) {
1051 d
= isl_qpolynomial_sub(
1052 isl_qpolynomial_copy(fold1
->qp
[j
]),
1053 isl_qpolynomial_copy(fold2
->qp
[i
]));
1054 sgn
= isl_qpolynomial_sign(set
, d
);
1055 isl_qpolynomial_free(d
);
1066 /* Check whether "pwf1" dominated "pwf2", i.e., the domain of "pwf1" contains
1067 * that of "pwf2" and on each cell, the corresponding fold from pwf1 dominates
1070 int isl_pw_qpolynomial_fold_covers(__isl_keep isl_pw_qpolynomial_fold
*pwf1
,
1071 __isl_keep isl_pw_qpolynomial_fold
*pwf2
)
1074 isl_set
*dom1
, *dom2
;
1085 dom1
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf1
));
1086 dom2
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf2
));
1087 is_subset
= isl_set_is_subset(dom2
, dom1
);
1091 if (is_subset
< 0 || !is_subset
)
1094 for (i
= 0; i
< pwf2
->n
; ++i
) {
1095 for (j
= 0; j
< pwf1
->n
; ++j
) {
1100 common
= isl_set_intersect(isl_set_copy(pwf1
->p
[j
].set
),
1101 isl_set_copy(pwf2
->p
[i
].set
));
1102 is_empty
= isl_set_is_empty(common
);
1103 if (is_empty
< 0 || is_empty
) {
1104 isl_set_free(common
);
1109 covers
= qpolynomial_fold_covers_on_domain(common
,
1110 pwf1
->p
[j
].fold
, pwf2
->p
[i
].fold
);
1111 isl_set_free(common
);
1112 if (covers
< 0 || !covers
)
1120 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_morph(
1121 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_morph
*morph
)
1126 if (!fold
|| !morph
)
1129 ctx
= fold
->dim
->ctx
;
1130 isl_assert(ctx
, isl_dim_equal(fold
->dim
, morph
->dom
->dim
), goto error
);
1132 fold
= isl_qpolynomial_fold_cow(fold
);
1136 isl_dim_free(fold
->dim
);
1137 fold
->dim
= isl_dim_copy(morph
->ran
->dim
);
1141 for (i
= 0; i
< fold
->n
; ++i
) {
1142 fold
->qp
[i
] = isl_qpolynomial_morph(fold
->qp
[i
],
1143 isl_morph_copy(morph
));
1148 isl_morph_free(morph
);
1152 isl_qpolynomial_fold_free(fold
);
1153 isl_morph_free(morph
);
1157 enum isl_fold
isl_qpolynomial_fold_get_type(__isl_keep isl_qpolynomial_fold
*fold
)
1160 return isl_fold_list
;
1164 enum isl_fold
isl_union_pw_qpolynomial_fold_get_type(
1165 __isl_keep isl_union_pw_qpolynomial_fold
*upwf
)
1168 return isl_fold_list
;
1172 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_lift(
1173 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_dim
*dim
)
1180 if (isl_dim_equal(fold
->dim
, dim
)) {
1185 fold
= isl_qpolynomial_fold_cow(fold
);
1189 isl_dim_free(fold
->dim
);
1190 fold
->dim
= isl_dim_copy(dim
);
1194 for (i
= 0; i
< fold
->n
; ++i
) {
1195 fold
->qp
[i
] = isl_qpolynomial_lift(fold
->qp
[i
],
1205 isl_qpolynomial_fold_free(fold
);
1210 int isl_qpolynomial_fold_foreach_qpolynomial(
1211 __isl_keep isl_qpolynomial_fold
*fold
,
1212 int (*fn
)(__isl_take isl_qpolynomial
*qp
, void *user
), void *user
)
1219 for (i
= 0; i
< fold
->n
; ++i
)
1220 if (fn(isl_qpolynomial_copy(fold
->qp
[i
]), user
) < 0)
1226 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_move_dims(
1227 __isl_take isl_qpolynomial_fold
*fold
,
1228 enum isl_dim_type dst_type
, unsigned dst_pos
,
1229 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1236 fold
= isl_qpolynomial_fold_cow(fold
);
1240 fold
->dim
= isl_dim_move(fold
->dim
, dst_type
, dst_pos
,
1241 src_type
, src_pos
, n
);
1245 for (i
= 0; i
< fold
->n
; ++i
) {
1246 fold
->qp
[i
] = isl_qpolynomial_move_dims(fold
->qp
[i
],
1247 dst_type
, dst_pos
, src_type
, src_pos
, n
);
1254 isl_qpolynomial_fold_free(fold
);
1258 /* For each 0 <= i < "n", replace variable "first" + i of type "type"
1259 * in fold->qp[k] by subs[i].
1261 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_substitute(
1262 __isl_take isl_qpolynomial_fold
*fold
,
1263 enum isl_dim_type type
, unsigned first
, unsigned n
,
1264 __isl_keep isl_qpolynomial
**subs
)
1271 fold
= isl_qpolynomial_fold_cow(fold
);
1275 for (i
= 0; i
< fold
->n
; ++i
) {
1276 fold
->qp
[i
] = isl_qpolynomial_substitute(fold
->qp
[i
],
1277 type
, first
, n
, subs
);
1284 isl_qpolynomial_fold_free(fold
);
1288 static int add_pwqp(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
1291 isl_pw_qpolynomial_fold
*pwf
;
1292 isl_union_pw_qpolynomial_fold
**upwf
;
1294 struct isl_hash_table_entry
*entry
;
1296 upwf
= (isl_union_pw_qpolynomial_fold
**)user
;
1298 ctx
= pwqp
->dim
->ctx
;
1299 hash
= isl_dim_get_hash(pwqp
->dim
);
1300 entry
= isl_hash_table_find(ctx
, &(*upwf
)->table
,
1301 hash
, &has_dim
, pwqp
->dim
, 1);
1305 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial((*upwf
)->type
, pwqp
);
1309 entry
->data
= isl_pw_qpolynomial_fold_add(entry
->data
, pwf
);
1312 if (isl_pw_qpolynomial_fold_is_zero(entry
->data
)) {
1313 isl_pw_qpolynomial_fold_free(entry
->data
);
1314 isl_hash_table_remove(ctx
, &(*upwf
)->table
, entry
);
1320 isl_pw_qpolynomial_free(pwqp
);
1324 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_fold_add_union_pw_qpolynomial(
1325 __isl_take isl_union_pw_qpolynomial_fold
*upwf
,
1326 __isl_take isl_union_pw_qpolynomial
*upwqp
)
1328 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1329 isl_union_pw_qpolynomial_get_dim(upwqp
));
1330 upwqp
= isl_union_pw_qpolynomial_align_params(upwqp
,
1331 isl_union_pw_qpolynomial_fold_get_dim(upwf
));
1333 upwf
= isl_union_pw_qpolynomial_fold_cow(upwf
);
1334 if (!upwf
|| !upwqp
)
1337 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
, &add_pwqp
,
1341 isl_union_pw_qpolynomial_free(upwqp
);
1345 isl_union_pw_qpolynomial_fold_free(upwf
);
1346 isl_union_pw_qpolynomial_free(upwqp
);
1350 static int compatible_range(__isl_keep isl_dim
*dim1
, __isl_keep isl_dim
*dim2
)
1353 m
= isl_dim_match(dim1
, isl_dim_param
, dim2
, isl_dim_param
);
1356 return isl_dim_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_set
);
1359 /* Compute the intersection of the range of the map and the domain
1360 * of the piecewise quasipolynomial reduction and then compute a bound
1361 * on the associated quasipolynomial reduction over all elements
1362 * in this intersection.
1364 * We first introduce some unconstrained dimensions in the
1365 * piecewise quasipolynomial, intersect the resulting domain
1366 * with the wrapped map and the compute the sum.
1368 __isl_give isl_pw_qpolynomial_fold
*isl_map_apply_pw_qpolynomial_fold(
1369 __isl_take isl_map
*map
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1379 ctx
= isl_map_get_ctx(map
);
1383 map_dim
= isl_map_get_dim(map
);
1384 pwf_dim
= isl_pw_qpolynomial_fold_get_dim(pwf
);
1385 ok
= compatible_range(map_dim
, pwf_dim
);
1386 isl_dim_free(map_dim
);
1387 isl_dim_free(pwf_dim
);
1389 isl_die(ctx
, isl_error_invalid
, "incompatible dimensions",
1392 n_in
= isl_map_dim(map
, isl_dim_in
);
1393 pwf
= isl_pw_qpolynomial_fold_insert_dims(pwf
, isl_dim_set
, 0, n_in
);
1395 dom
= isl_map_wrap(map
);
1396 pwf
= isl_pw_qpolynomial_fold_reset_dim(pwf
, isl_set_get_dim(dom
));
1398 pwf
= isl_pw_qpolynomial_fold_intersect_domain(pwf
, dom
);
1399 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, tight
);
1404 isl_pw_qpolynomial_fold_free(pwf
);
1408 __isl_give isl_pw_qpolynomial_fold
*isl_set_apply_pw_qpolynomial_fold(
1409 __isl_take isl_set
*set
, __isl_take isl_pw_qpolynomial_fold
*pwf
,
1414 map
= isl_map_from_range(set
);
1415 return isl_map_apply_pw_qpolynomial_fold(map
, pwf
, tight
);
1418 struct isl_apply_fold_data
{
1419 isl_union_pw_qpolynomial_fold
*upwf
;
1420 isl_union_pw_qpolynomial_fold
*res
;
1425 static int pw_qpolynomial_fold_apply(__isl_take isl_pw_qpolynomial_fold
*pwf
,
1430 struct isl_apply_fold_data
*data
= user
;
1433 map_dim
= isl_map_get_dim(data
->map
);
1434 pwf_dim
= isl_pw_qpolynomial_fold_get_dim(pwf
);
1435 ok
= compatible_range(map_dim
, pwf_dim
);
1436 isl_dim_free(map_dim
);
1437 isl_dim_free(pwf_dim
);
1440 pwf
= isl_map_apply_pw_qpolynomial_fold(isl_map_copy(data
->map
),
1441 pwf
, data
->tight
? &data
->tight
: NULL
);
1442 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
1445 isl_pw_qpolynomial_fold_free(pwf
);
1450 static int map_apply(__isl_take isl_map
*map
, void *user
)
1452 struct isl_apply_fold_data
*data
= user
;
1456 r
= isl_union_pw_qpolynomial_fold_foreach_pw_qpolynomial_fold(
1457 data
->upwf
, &pw_qpolynomial_fold_apply
, data
);
1463 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_map_apply_union_pw_qpolynomial_fold(
1464 __isl_take isl_union_map
*umap
,
1465 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1469 struct isl_apply_fold_data data
;
1471 upwf
= isl_union_pw_qpolynomial_fold_align_params(upwf
,
1472 isl_union_map_get_dim(umap
));
1473 umap
= isl_union_map_align_params(umap
,
1474 isl_union_pw_qpolynomial_fold_get_dim(upwf
));
1477 data
.tight
= tight
? 1 : 0;
1478 dim
= isl_union_pw_qpolynomial_fold_get_dim(upwf
);
1479 type
= isl_union_pw_qpolynomial_fold_get_type(upwf
);
1480 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
1481 if (isl_union_map_foreach_map(umap
, &map_apply
, &data
) < 0)
1484 isl_union_map_free(umap
);
1485 isl_union_pw_qpolynomial_fold_free(upwf
);
1488 *tight
= data
.tight
;
1492 isl_union_map_free(umap
);
1493 isl_union_pw_qpolynomial_fold_free(upwf
);
1494 isl_union_pw_qpolynomial_fold_free(data
.res
);
1498 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_set_apply_union_pw_qpolynomial_fold(
1499 __isl_take isl_union_set
*uset
,
1500 __isl_take isl_union_pw_qpolynomial_fold
*upwf
, int *tight
)
1502 return isl_union_map_apply_union_pw_qpolynomial_fold(uset
, upwf
, tight
);
1505 /* Reorder the dimension of "fold" according to the given reordering.
1507 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_realign(
1508 __isl_take isl_qpolynomial_fold
*fold
, __isl_take isl_reordering
*r
)
1512 fold
= isl_qpolynomial_fold_cow(fold
);
1516 for (i
= 0; i
< fold
->n
; ++i
) {
1517 fold
->qp
[i
] = isl_qpolynomial_realign(fold
->qp
[i
],
1518 isl_reordering_copy(r
));
1523 fold
= isl_qpolynomial_fold_reset_dim(fold
, isl_dim_copy(r
->dim
));
1525 isl_reordering_free(r
);
1529 isl_qpolynomial_fold_free(fold
);
1530 isl_reordering_free(r
);
1534 __isl_give isl_qpolynomial_fold
*isl_qpolynomial_fold_mul_isl_int(
1535 __isl_take isl_qpolynomial_fold
*fold
, isl_int v
)
1539 if (isl_int_is_one(v
))
1541 if (fold
&& isl_int_is_zero(v
)) {
1542 isl_qpolynomial_fold
*zero
;
1543 isl_dim
*dim
= isl_dim_copy(fold
->dim
);
1544 zero
= isl_qpolynomial_fold_empty(fold
->type
, dim
);
1545 isl_qpolynomial_fold_free(fold
);
1549 fold
= isl_qpolynomial_fold_cow(fold
);
1553 if (isl_int_is_neg(v
))
1554 fold
->type
= isl_fold_type_negate(fold
->type
);
1555 for (i
= 0; i
< fold
->n
; ++i
) {
1556 fold
->qp
[i
] = isl_qpolynomial_mul_isl_int(fold
->qp
[i
], v
);
1563 isl_qpolynomial_fold_free(fold
);