2 * Copyright 2012-2014 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
14 #include <isl/space.h>
15 #include <isl/constraint.h>
18 #include <isl_ast_build_expr.h>
19 #include <isl_ast_private.h>
20 #include <isl_ast_build_private.h>
23 /* Compute the "opposite" of the (numerator of the) argument of a div
24 * with denominator "d".
26 * In particular, compute
30 static __isl_give isl_aff
*oppose_div_arg(__isl_take isl_aff
*aff
,
31 __isl_take isl_val
*d
)
33 aff
= isl_aff_neg(aff
);
34 aff
= isl_aff_add_constant_val(aff
, d
);
35 aff
= isl_aff_add_constant_si(aff
, -1);
40 /* Internal data structure used inside isl_ast_expr_add_term.
41 * The domain of "build" is used to simplify the expressions.
42 * "build" needs to be set by the caller of isl_ast_expr_add_term.
43 * "ls" is the domain local space of the affine expression
44 * of which a term is being added.
45 * "cst" is the constant term of the expression in which the added term
46 * appears. It may be modified by isl_ast_expr_add_term.
48 * "v" is the coefficient of the term that is being constructed and
49 * is set internally by isl_ast_expr_add_term.
51 struct isl_ast_add_term_data
{
58 /* Given the numerator "aff" of the argument of an integer division
59 * with denominator "d", check if it can be made non-negative over
60 * data->build->domain by stealing part of the constant term of
61 * the expression in which the integer division appears.
63 * In particular, the outer expression is of the form
65 * v * floor(aff/d) + cst
67 * We already know that "aff" itself may attain negative values.
68 * Here we check if aff + d*floor(cst/v) is non-negative, such
69 * that we could rewrite the expression to
71 * v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
73 * Note that aff + d*floor(cst/v) can only possibly be non-negative
74 * if data->cst and data->v have the same sign.
75 * Similarly, if floor(cst/v) is zero, then there is no point in
78 static isl_bool
is_non_neg_after_stealing(__isl_keep isl_aff
*aff
,
79 __isl_keep isl_val
*d
, struct isl_ast_add_term_data
*data
)
86 if (isl_val_sgn(data
->cst
) != isl_val_sgn(data
->v
))
87 return isl_bool_false
;
89 shift
= isl_val_div(isl_val_copy(data
->cst
), isl_val_copy(data
->v
));
90 shift
= isl_val_floor(shift
);
91 is_zero
= isl_val_is_zero(shift
);
92 if (is_zero
< 0 || is_zero
) {
94 return isl_bool_not(is_zero
);
96 shift
= isl_val_mul(shift
, isl_val_copy(d
));
97 shifted
= isl_aff_copy(aff
);
98 shifted
= isl_aff_add_constant_val(shifted
, shift
);
99 non_neg
= isl_ast_build_aff_is_nonneg(data
->build
, shifted
);
100 isl_aff_free(shifted
);
105 /* Given the numerator "aff" of the argument of an integer division
106 * with denominator "d", steal part of the constant term of
107 * the expression in which the integer division appears to make it
108 * non-negative over data->build->domain.
110 * In particular, the outer expression is of the form
112 * v * floor(aff/d) + cst
114 * We know that "aff" itself may attain negative values,
115 * but that aff + d*floor(cst/v) is non-negative.
116 * Find the minimal positive value that we need to add to "aff"
117 * to make it positive and adjust data->cst accordingly.
118 * That is, compute the minimal value "m" of "aff" over
119 * data->build->domain and take
127 * and rewrite the expression to
129 * v * floor((aff + s*d)/d) + (cst - v*s)
131 static __isl_give isl_aff
*steal_from_cst(__isl_take isl_aff
*aff
,
132 __isl_keep isl_val
*d
, struct isl_ast_add_term_data
*data
)
137 domain
= isl_ast_build_get_domain(data
->build
);
138 shift
= isl_set_min_val(domain
, aff
);
139 isl_set_free(domain
);
141 shift
= isl_val_neg(shift
);
142 shift
= isl_val_div(shift
, isl_val_copy(d
));
143 shift
= isl_val_ceil(shift
);
145 t
= isl_val_copy(shift
);
146 t
= isl_val_mul(t
, isl_val_copy(data
->v
));
147 data
->cst
= isl_val_sub(data
->cst
, t
);
149 shift
= isl_val_mul(shift
, isl_val_copy(d
));
150 return isl_aff_add_constant_val(aff
, shift
);
153 /* Construct an expression representing the binary operation "type"
154 * (some division or modulo) applied to the expressions
155 * constructed from "aff" and "v".
157 static __isl_give isl_ast_expr
*div_mod(enum isl_ast_expr_op_type type
,
158 __isl_take isl_aff
*aff
, __isl_take isl_val
*v
,
159 __isl_keep isl_ast_build
*build
)
161 isl_ast_expr
*expr1
, *expr2
;
163 expr1
= isl_ast_expr_from_aff(aff
, build
);
164 expr2
= isl_ast_expr_from_val(v
);
165 return isl_ast_expr_alloc_binary(type
, expr1
, expr2
);
168 /* Create an isl_ast_expr evaluating the div at position "pos" in data->ls.
169 * The result is simplified in terms of data->build->domain.
170 * This function may change (the sign of) data->v.
172 * data->ls is known to be non-NULL.
174 * Let the div be of the form floor(e/d).
175 * If the ast_build_prefer_pdiv option is set then we check if "e"
176 * is non-negative, so that we can generate
178 * (pdiv_q, expr(e), expr(d))
182 * (fdiv_q, expr(e), expr(d))
184 * If the ast_build_prefer_pdiv option is set and
185 * if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
186 * If so, we can rewrite
188 * floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
190 * and still use pdiv_q, while changing the sign of data->v.
192 * Otherwise, we check if
196 * is non-negative and if so, replace floor(e/d) by
198 * floor((e + s*d)/d) - s
200 * with s the minimal shift that makes the argument non-negative.
202 static __isl_give isl_ast_expr
*var_div(struct isl_ast_add_term_data
*data
,
205 isl_ctx
*ctx
= isl_local_space_get_ctx(data
->ls
);
208 enum isl_ast_expr_op_type type
;
210 aff
= isl_local_space_get_div(data
->ls
, pos
);
211 d
= isl_aff_get_denominator_val(aff
);
212 aff
= isl_aff_scale_val(aff
, isl_val_copy(d
));
214 type
= isl_ast_expr_op_fdiv_q
;
215 if (isl_options_get_ast_build_prefer_pdiv(ctx
)) {
217 non_neg
= isl_ast_build_aff_is_nonneg(data
->build
, aff
);
218 if (non_neg
>= 0 && !non_neg
) {
219 isl_aff
*opp
= oppose_div_arg(isl_aff_copy(aff
),
221 non_neg
= isl_ast_build_aff_is_nonneg(data
->build
, opp
);
222 if (non_neg
>= 0 && non_neg
) {
223 data
->v
= isl_val_neg(data
->v
);
229 if (non_neg
>= 0 && !non_neg
) {
230 non_neg
= is_non_neg_after_stealing(aff
, d
, data
);
231 if (non_neg
>= 0 && non_neg
)
232 aff
= steal_from_cst(aff
, d
, data
);
235 aff
= isl_aff_free(aff
);
237 type
= isl_ast_expr_op_pdiv_q
;
240 return div_mod(type
, aff
, d
, data
->build
);
243 /* Create an isl_ast_expr evaluating the specified dimension of data->ls.
244 * The result is simplified in terms of data->build->domain.
245 * This function may change (the sign of) data->v.
247 * The isl_ast_expr is constructed based on the type of the dimension.
248 * - divs are constructed by var_div
249 * - set variables are constructed from the iterator isl_ids in data->build
250 * - parameters are constructed from the isl_ids in data->ls
252 static __isl_give isl_ast_expr
*var(struct isl_ast_add_term_data
*data
,
253 enum isl_dim_type type
, int pos
)
255 isl_ctx
*ctx
= isl_local_space_get_ctx(data
->ls
);
258 if (type
== isl_dim_div
)
259 return var_div(data
, pos
);
261 if (type
== isl_dim_set
) {
262 id
= isl_ast_build_get_iterator_id(data
->build
, pos
);
263 return isl_ast_expr_from_id(id
);
266 if (!isl_local_space_has_dim_id(data
->ls
, type
, pos
))
267 isl_die(ctx
, isl_error_internal
, "unnamed dimension",
269 id
= isl_local_space_get_dim_id(data
->ls
, type
, pos
);
270 return isl_ast_expr_from_id(id
);
273 /* Does "expr" represent the zero integer?
275 static isl_bool
ast_expr_is_zero(__isl_keep isl_ast_expr
*expr
)
278 return isl_bool_error
;
279 if (expr
->type
!= isl_ast_expr_int
)
280 return isl_bool_false
;
281 return isl_val_is_zero(expr
->u
.v
);
284 /* Create an expression representing the sum of "expr1" and "expr2",
285 * provided neither of the two expressions is identically zero.
287 static __isl_give isl_ast_expr
*ast_expr_add(__isl_take isl_ast_expr
*expr1
,
288 __isl_take isl_ast_expr
*expr2
)
290 if (!expr1
|| !expr2
)
293 if (ast_expr_is_zero(expr1
)) {
294 isl_ast_expr_free(expr1
);
298 if (ast_expr_is_zero(expr2
)) {
299 isl_ast_expr_free(expr2
);
303 return isl_ast_expr_add(expr1
, expr2
);
305 isl_ast_expr_free(expr1
);
306 isl_ast_expr_free(expr2
);
310 /* Subtract expr2 from expr1.
312 * If expr2 is zero, we simply return expr1.
313 * If expr1 is zero, we return
315 * (isl_ast_expr_op_minus, expr2)
317 * Otherwise, we return
319 * (isl_ast_expr_op_sub, expr1, expr2)
321 static __isl_give isl_ast_expr
*ast_expr_sub(__isl_take isl_ast_expr
*expr1
,
322 __isl_take isl_ast_expr
*expr2
)
324 if (!expr1
|| !expr2
)
327 if (ast_expr_is_zero(expr2
)) {
328 isl_ast_expr_free(expr2
);
332 if (ast_expr_is_zero(expr1
)) {
333 isl_ast_expr_free(expr1
);
334 return isl_ast_expr_neg(expr2
);
337 return isl_ast_expr_sub(expr1
, expr2
);
339 isl_ast_expr_free(expr1
);
340 isl_ast_expr_free(expr2
);
344 /* Return an isl_ast_expr that represents
348 * v is assumed to be non-negative.
349 * The result is simplified in terms of build->domain.
351 static __isl_give isl_ast_expr
*isl_ast_expr_mod(__isl_keep isl_val
*v
,
352 __isl_keep isl_aff
*aff
, __isl_keep isl_val
*d
,
353 __isl_keep isl_ast_build
*build
)
361 expr
= div_mod(isl_ast_expr_op_pdiv_r
,
362 isl_aff_copy(aff
), isl_val_copy(d
), build
);
364 if (!isl_val_is_one(v
)) {
365 c
= isl_ast_expr_from_val(isl_val_copy(v
));
366 expr
= isl_ast_expr_mul(c
, expr
);
372 /* Create an isl_ast_expr that scales "expr" by "v".
374 * If v is 1, we simply return expr.
375 * If v is -1, we return
377 * (isl_ast_expr_op_minus, expr)
379 * Otherwise, we return
381 * (isl_ast_expr_op_mul, expr(v), expr)
383 static __isl_give isl_ast_expr
*scale(__isl_take isl_ast_expr
*expr
,
384 __isl_take isl_val
*v
)
390 if (isl_val_is_one(v
)) {
395 if (isl_val_is_negone(v
)) {
397 expr
= isl_ast_expr_neg(expr
);
399 c
= isl_ast_expr_from_val(v
);
400 expr
= isl_ast_expr_mul(c
, expr
);
406 isl_ast_expr_free(expr
);
410 /* Add an expression for "*v" times the specified dimension of data->ls
412 * If the dimension is an integer division, then this function
413 * may modify data->cst in order to make the numerator non-negative.
414 * The result is simplified in terms of data->build->domain.
416 * Let e be the expression for the specified dimension,
417 * multiplied by the absolute value of "*v".
418 * If "*v" is negative, we create
420 * (isl_ast_expr_op_sub, expr, e)
422 * except when expr is trivially zero, in which case we create
424 * (isl_ast_expr_op_minus, e)
428 * If "*v" is positive, we simply create
430 * (isl_ast_expr_op_add, expr, e)
433 static __isl_give isl_ast_expr
*isl_ast_expr_add_term(
434 __isl_take isl_ast_expr
*expr
, enum isl_dim_type type
, int pos
,
435 __isl_take isl_val
*v
, struct isl_ast_add_term_data
*data
)
443 term
= var(data
, type
, pos
);
446 if (isl_val_is_neg(v
) && !ast_expr_is_zero(expr
)) {
448 term
= scale(term
, v
);
449 return ast_expr_sub(expr
, term
);
451 term
= scale(term
, v
);
452 return ast_expr_add(expr
, term
);
456 /* Add an expression for "v" to expr.
458 static __isl_give isl_ast_expr
*isl_ast_expr_add_int(
459 __isl_take isl_ast_expr
*expr
, __isl_take isl_val
*v
)
461 isl_ast_expr
*expr_int
;
466 if (isl_val_is_zero(v
)) {
471 if (isl_val_is_neg(v
) && !ast_expr_is_zero(expr
)) {
473 expr_int
= isl_ast_expr_from_val(v
);
474 return ast_expr_sub(expr
, expr_int
);
476 expr_int
= isl_ast_expr_from_val(v
);
477 return ast_expr_add(expr
, expr_int
);
480 isl_ast_expr_free(expr
);
485 /* Internal data structure used inside extract_modulos.
487 * If any modulo expressions are detected in "aff", then the
488 * expression is removed from "aff" and added to either "pos" or "neg"
489 * depending on the sign of the coefficient of the modulo expression
492 * "add" is an expression that needs to be added to "aff" at the end of
493 * the computation. It is NULL as long as no modulos have been extracted.
495 * "i" is the position in "aff" of the div under investigation
496 * "v" is the coefficient in "aff" of the div
497 * "div" is the argument of the div, with the denominator removed
498 * "d" is the original denominator of the argument of the div
500 * "nonneg" is an affine expression that is non-negative over "build"
501 * and that can be used to extract a modulo expression from "div".
502 * In particular, if "sign" is 1, then the coefficients of "nonneg"
503 * are equal to those of "div" modulo "d". If "sign" is -1, then
504 * the coefficients of "nonneg" are opposite to those of "div" modulo "d".
505 * If "sign" is 0, then no such affine expression has been found (yet).
507 struct isl_extract_mod_data
{
508 isl_ast_build
*build
;
529 * represent (a special case of) a test for some linear expression
532 * In particular, is it of the form
538 static isl_bool
is_even_test(struct isl_extract_mod_data
*data
,
539 __isl_keep isl_aff
*arg
)
544 res
= isl_val_eq_si(data
->d
, 2);
548 cst
= isl_aff_get_constant_val(arg
);
549 res
= isl_val_eq_si(cst
, -1);
555 /* Given that data->v * div_i in data->aff is equal to
557 * f * (term - (arg mod d))
559 * with data->d * f = data->v and "arg" non-negative on data->build, add
565 * abs(f) * (arg mod d)
567 * to data->neg or data->pos depending on the sign of -f.
569 * In the special case that "arg mod d" is of the form "(lin - 1) mod 2",
570 * with "lin" some linear expression, first replace
572 * f * (term - ((lin - 1) mod 2))
576 * -f * (1 - term - (lin mod 2))
578 * These two are equal because
580 * ((lin - 1) mod 2) + (lin mod 2) = 1
582 * Also, if "lin - 1" is non-negative, then "lin" is non-negative too.
584 static isl_stat
extract_term_and_mod(struct isl_extract_mod_data
*data
,
585 __isl_take isl_aff
*term
, __isl_take isl_aff
*arg
)
591 even
= is_even_test(data
, arg
);
593 arg
= isl_aff_free(arg
);
595 term
= oppose_div_arg(term
, isl_val_copy(data
->d
));
596 data
->v
= isl_val_neg(data
->v
);
597 arg
= isl_aff_set_constant_si(arg
, 0);
600 data
->v
= isl_val_div(data
->v
, isl_val_copy(data
->d
));
601 s
= isl_val_sgn(data
->v
);
602 data
->v
= isl_val_abs(data
->v
);
603 expr
= isl_ast_expr_mod(data
->v
, arg
, data
->d
, data
->build
);
606 data
->neg
= ast_expr_add(data
->neg
, expr
);
608 data
->pos
= ast_expr_add(data
->pos
, expr
);
609 data
->aff
= isl_aff_set_coefficient_si(data
->aff
,
610 isl_dim_div
, data
->i
, 0);
612 data
->v
= isl_val_neg(data
->v
);
613 term
= isl_aff_scale_val(term
, isl_val_copy(data
->v
));
618 data
->add
= isl_aff_add(data
->add
, term
);
620 return isl_stat_error
;
625 /* Given that data->v * div_i in data->aff is of the form
627 * f * d * floor(div/d)
629 * with div nonnegative on data->build, rewrite it as
631 * f * (div - (div mod d)) = f * div - f * (div mod d)
639 * abs(f) * (div mod d)
641 * to data->neg or data->pos depending on the sign of -f.
643 static isl_stat
extract_mod(struct isl_extract_mod_data
*data
)
645 return extract_term_and_mod(data
, isl_aff_copy(data
->div
),
646 isl_aff_copy(data
->div
));
649 /* Given that data->v * div_i in data->aff is of the form
651 * f * d * floor(div/d) (1)
653 * check if div is non-negative on data->build and, if so,
654 * extract the corresponding modulo from data->aff.
655 * If not, then check if
659 * is non-negative on data->build. If so, replace (1) by
661 * -f * d * floor((-div + d - 1)/d)
663 * and extract the corresponding modulo from data->aff.
665 * This function may modify data->div.
667 static isl_stat
extract_nonneg_mod(struct isl_extract_mod_data
*data
)
671 mod
= isl_ast_build_aff_is_nonneg(data
->build
, data
->div
);
675 return extract_mod(data
);
677 data
->div
= oppose_div_arg(data
->div
, isl_val_copy(data
->d
));
678 mod
= isl_ast_build_aff_is_nonneg(data
->build
, data
->div
);
682 data
->v
= isl_val_neg(data
->v
);
683 return extract_mod(data
);
688 data
->aff
= isl_aff_free(data
->aff
);
689 return isl_stat_error
;
692 /* Does "c" have a constant term that is "too large"?
693 * Here, "too large" is fairly arbitrarily set to 1 << 15.
695 static isl_bool
has_large_constant_term(__isl_keep isl_constraint
*c
)
700 v
= isl_val_abs(isl_constraint_get_constant_val(c
));
702 return isl_bool_error
;
703 sign
= isl_val_cmp_si(v
, 1 << 15);
705 return isl_bool_ok(sign
> 0);
708 /* Is the affine expression of constraint "c" "simpler" than data->nonneg
709 * for use in extracting a modulo expression?
711 * We currently only consider the constant term of the affine expression.
712 * In particular, we prefer the affine expression with the smallest constant
714 * This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
715 * then we would pick x >= 0
717 * More detailed heuristics could be used if it turns out that there is a need.
719 static isl_bool
mod_constraint_is_simpler(struct isl_extract_mod_data
*data
,
720 __isl_keep isl_constraint
*c
)
726 return isl_bool_true
;
728 v1
= isl_val_abs(isl_constraint_get_constant_val(c
));
729 v2
= isl_val_abs(isl_aff_get_constant_val(data
->nonneg
));
730 simpler
= isl_val_lt(v1
, v2
);
737 /* If "c" is "simpler" than data->nonneg,
738 * then replace data->nonneg by the affine expression of "c" and
739 * set data->sign to "sign".
741 static isl_stat
replace_if_simpler(struct isl_extract_mod_data
*data
,
742 __isl_keep isl_constraint
*c
, int sign
)
746 simpler
= mod_constraint_is_simpler(data
, c
);
747 if (simpler
< 0 || !simpler
)
748 return isl_stat_non_error_bool(simpler
);
750 isl_aff_free(data
->nonneg
);
751 data
->nonneg
= isl_constraint_get_aff(c
);
754 return isl_stat_non_null(data
->nonneg
);
757 /* Internal data structure used inside check_parallel_or_opposite.
759 * "data" is the information passed down from the caller.
760 * "c" is the constraint being inspected.
762 * "n" contains the number of parameters and the number of input dimensions and
763 * is set by the first call to parallel_or_opposite_scan.
764 * "parallel" is set as long as the coefficients of "c" are still potentially
765 * equal to those of data->div modulo data->d.
766 * "opposite" is set as long as the coefficients of "c" are still potentially
767 * opposite to those of data->div modulo data->d.
769 struct isl_parallel_stat
{
770 struct isl_extract_mod_data
*data
;
778 /* Should the scan of coefficients be continued?
779 * That is, are the coefficients still (potentially) equal or opposite?
781 static isl_bool
parallel_or_opposite_continue(struct isl_parallel_stat
*stat
)
783 if (stat
->parallel
< 0 || stat
->opposite
< 0)
784 return isl_bool_error
;
786 return isl_bool_ok(stat
->parallel
|| stat
->opposite
);
789 /* Is coefficient "i" of type "c_type" of stat->c potentially equal or
790 * opposite to coefficient "i" of type "a_type" of stat->data->div
791 * modulo stat->data->div?
792 * In particular, are they both zero or both non-zero?
794 * Note that while the coefficients of stat->data->div can be reasonably
795 * expected not to involve any coefficients that are multiples of stat->data->d,
796 * "c" may very well involve such coefficients.
797 * This means that some cases of equal or opposite constraints can be missed
800 static isl_bool
parallel_or_opposite_feasible(struct isl_parallel_stat
*stat
,
801 enum isl_dim_type c_type
, enum isl_dim_type a_type
, int i
)
805 a
= isl_constraint_involves_dims(stat
->c
, c_type
, i
, 1);
806 b
= isl_aff_involves_dims(stat
->data
->div
, a_type
, i
, 1);
808 return isl_bool_error
;
810 stat
->parallel
= stat
->opposite
= isl_bool_false
;
812 return parallel_or_opposite_continue(stat
);
815 /* Is coefficient "i" of type "c_type" of stat->c equal or
816 * opposite to coefficient "i" of type "a_type" of stat->data->div
817 * modulo stat->data->div?
819 static isl_bool
is_parallel_or_opposite(struct isl_parallel_stat
*stat
,
820 enum isl_dim_type c_type
, enum isl_dim_type a_type
, int i
)
824 v1
= isl_constraint_get_coefficient_val(stat
->c
, c_type
, i
);
825 v2
= isl_aff_get_coefficient_val(stat
->data
->div
, a_type
, i
);
826 if (stat
->parallel
) {
827 v1
= isl_val_sub(v1
, isl_val_copy(v2
));
828 stat
->parallel
= isl_val_is_divisible_by(v1
, stat
->data
->d
);
829 v1
= isl_val_add(v1
, isl_val_copy(v2
));
831 if (stat
->opposite
) {
832 v1
= isl_val_add(v1
, isl_val_copy(v2
));
833 stat
->opposite
= isl_val_is_divisible_by(v1
, stat
->data
->d
);
838 return parallel_or_opposite_continue(stat
);
841 /* Scan the coefficients of stat->c to see if they are (potentially)
842 * equal or opposite to those of stat->data->div modulo stat->data->d,
843 * calling "fn" on each coefficient.
844 * IF "init" is set, then this is the first call to this function and
845 * then stat->n is initialized.
847 static isl_bool
parallel_or_opposite_scan(struct isl_parallel_stat
*stat
,
848 isl_bool (*fn
)(struct isl_parallel_stat
*stat
,
849 enum isl_dim_type c_type
, enum isl_dim_type a_type
, int i
),
852 enum isl_dim_type c_type
[2] = { isl_dim_param
, isl_dim_set
};
853 enum isl_dim_type a_type
[2] = { isl_dim_param
, isl_dim_in
};
856 for (t
= 0; t
< 2; ++t
) {
858 stat
->n
[t
] = isl_constraint_dim(stat
->c
, c_type
[t
]);
860 return isl_bool_error
;
862 for (i
= 0; i
< stat
->n
[t
]; ++i
) {
865 ok
= fn(stat
, c_type
[t
], a_type
[t
], i
);
871 return isl_bool_true
;
874 /* Check if the coefficients of "c" are either equal or opposite to those
875 * of data->div modulo data->d. If so, and if "c" is "simpler" than
876 * data->nonneg, then replace data->nonneg by the affine expression of "c"
877 * and set data->sign accordingly.
879 * Both "c" and data->div are assumed not to involve any integer divisions.
881 * Before we start the actual comparison, we first quickly check if
882 * "c" and data->div have the same non-zero coefficients.
883 * If not, then we assume that "c" is not of the desired form.
885 * If the constant term is "too large", then the constraint is rejected.
886 * We do this to avoid picking up constraints that bound a variable
887 * by a very large number, say the largest or smallest possible
888 * variable in the representation of some integer type.
890 static isl_stat
check_parallel_or_opposite(struct isl_extract_mod_data
*data
,
891 __isl_keep isl_constraint
*c
)
893 struct isl_parallel_stat stat
= {
896 .parallel
= isl_bool_true
,
897 .opposite
= isl_bool_true
,
901 ok
= parallel_or_opposite_scan(&stat
,
902 ¶llel_or_opposite_feasible
, 1);
904 return isl_stat_non_error_bool(ok
);
906 skip
= has_large_constant_term(c
);
907 if (skip
< 0 || skip
)
908 return isl_stat_non_error_bool(skip
);
910 ok
= parallel_or_opposite_scan(&stat
, &is_parallel_or_opposite
, 0);
912 return isl_stat_non_error_bool(ok
);
914 return replace_if_simpler(data
, c
, stat
.parallel
? 1 : -1);
917 /* Wrapper around check_parallel_or_opposite for use
918 * as a isl_basic_set_foreach_constraint callback.
920 static isl_stat
check_parallel_or_opposite_wrap(__isl_take isl_constraint
*c
,
923 struct isl_extract_mod_data
*data
= user
;
926 res
= check_parallel_or_opposite(data
, c
);
927 isl_constraint_free(c
);
932 /* Given that data->v * div_i in data->aff is of the form
934 * f * d * floor(div/d) (1)
936 * see if we can find an expression div' that is non-negative over data->build
937 * and that is related to div through
943 * div' = -div + d - 1 + d * e
945 * with e some affine expression.
946 * If so, we write (1) as
948 * f * div + f * (div' mod d)
952 * -f * (-div + d - 1) - f * (div' mod d)
954 * exploiting (in the second case) the fact that
956 * f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
959 * We first try to find an appropriate expression for div'
960 * from the constraints of data->build->domain (which is therefore
961 * guaranteed to be non-negative on data->build), where we remove
962 * any integer divisions from the constraints and skip this step
963 * if "div" itself involves any integer divisions.
964 * If we cannot find an appropriate expression this way, then
965 * we pass control to extract_nonneg_mod where check
966 * if div or "-div + d -1" themselves happen to be
967 * non-negative on data->build.
969 * While looking for an appropriate constraint in data->build->domain,
970 * we ignore the constant term, so after finding such a constraint,
971 * we still need to fix up the constant term.
972 * In particular, if a is the constant term of "div"
973 * (or d - 1 - the constant term of "div" if data->sign < 0)
974 * and b is the constant term of the constraint, then we need to find
975 * a non-negative constant c such that
977 * b + c \equiv a mod d
983 * and add it to b to obtain the constant term of div'.
984 * If this constant term is "too negative", then we add an appropriate
985 * multiple of d to make it positive.
988 * Note that the above is only a very simple heuristic for finding an
989 * appropriate expression. We could try a bit harder by also considering
990 * sums of constraints that involve disjoint sets of variables or
991 * we could consider arbitrary linear combinations of constraints,
992 * although that could potentially be much more expensive as it involves
993 * the solution of an LP problem.
995 * In particular, if v_i is a column vector representing constraint i,
996 * w represents div and e_i is the i-th unit vector, then we are looking
997 * for a solution of the constraints
999 * \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
1001 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
1002 * If we are not just interested in a non-negative expression, but
1003 * also in one with a minimal range, then we don't just want
1004 * c = \sum_i lambda_i v_i to be non-negative over the domain,
1005 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
1006 * that we want to minimize and we now also have to take into account
1007 * the constant terms of the constraints.
1008 * Alternatively, we could first compute the dual of the domain
1009 * and plug in the constraints on the coefficients.
1011 static isl_stat
try_extract_mod(struct isl_extract_mod_data
*data
)
1013 isl_basic_set
*hull
;
1021 n
= isl_aff_dim(data
->div
, isl_dim_div
);
1025 if (isl_aff_involves_dims(data
->div
, isl_dim_div
, 0, n
))
1026 return extract_nonneg_mod(data
);
1028 hull
= isl_set_simple_hull(isl_set_copy(data
->build
->domain
));
1029 hull
= isl_basic_set_remove_divs(hull
);
1031 data
->nonneg
= NULL
;
1032 r
= isl_basic_set_foreach_constraint(hull
,
1033 &check_parallel_or_opposite_wrap
, data
);
1034 isl_basic_set_free(hull
);
1036 if (!data
->sign
|| r
< 0) {
1037 isl_aff_free(data
->nonneg
);
1040 return extract_nonneg_mod(data
);
1043 v1
= isl_aff_get_constant_val(data
->div
);
1044 v2
= isl_aff_get_constant_val(data
->nonneg
);
1045 if (data
->sign
< 0) {
1046 v1
= isl_val_neg(v1
);
1047 v1
= isl_val_add(v1
, isl_val_copy(data
->d
));
1048 v1
= isl_val_sub_ui(v1
, 1);
1050 v1
= isl_val_sub(v1
, isl_val_copy(v2
));
1051 v1
= isl_val_mod(v1
, isl_val_copy(data
->d
));
1052 v1
= isl_val_add(v1
, v2
);
1053 v2
= isl_val_div(isl_val_copy(v1
), isl_val_copy(data
->d
));
1054 v2
= isl_val_ceil(v2
);
1055 if (isl_val_is_neg(v2
)) {
1056 v2
= isl_val_mul(v2
, isl_val_copy(data
->d
));
1057 v1
= isl_val_sub(v1
, isl_val_copy(v2
));
1059 data
->nonneg
= isl_aff_set_constant_val(data
->nonneg
, v1
);
1062 if (data
->sign
< 0) {
1063 data
->div
= oppose_div_arg(data
->div
, isl_val_copy(data
->d
));
1064 data
->v
= isl_val_neg(data
->v
);
1067 return extract_term_and_mod(data
,
1068 isl_aff_copy(data
->div
), data
->nonneg
);
1070 data
->aff
= isl_aff_free(data
->aff
);
1071 return isl_stat_error
;
1074 /* Check if "data->aff" involves any (implicit) modulo computations based
1076 * If so, remove them from aff and add expressions corresponding
1077 * to those modulo computations to data->pos and/or data->neg.
1079 * "aff" is assumed to be an integer affine expression.
1081 * In particular, check if (v * div_j) is of the form
1083 * f * m * floor(a / m)
1085 * and, if so, rewrite it as
1087 * f * (a - (a mod m)) = f * a - f * (a mod m)
1089 * and extract out -f * (a mod m).
1090 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
1091 * If f < 0, we add ((-f) * (a mod m)) to *pos.
1093 * Note that in order to represent "a mod m" as
1095 * (isl_ast_expr_op_pdiv_r, a, m)
1097 * we need to make sure that a is non-negative.
1098 * If not, we check if "-a + m - 1" is non-negative.
1099 * If so, we can rewrite
1101 * floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
1103 * and still extract a modulo.
1105 static int extract_modulo(struct isl_extract_mod_data
*data
)
1107 data
->div
= isl_aff_get_div(data
->aff
, data
->i
);
1108 data
->d
= isl_aff_get_denominator_val(data
->div
);
1109 if (isl_val_is_divisible_by(data
->v
, data
->d
)) {
1110 data
->div
= isl_aff_scale_val(data
->div
, isl_val_copy(data
->d
));
1111 if (try_extract_mod(data
) < 0)
1112 data
->aff
= isl_aff_free(data
->aff
);
1114 isl_aff_free(data
->div
);
1115 isl_val_free(data
->d
);
1119 /* Check if "aff" involves any (implicit) modulo computations.
1120 * If so, remove them from aff and add expressions corresponding
1121 * to those modulo computations to *pos and/or *neg.
1122 * We only do this if the option ast_build_prefer_pdiv is set.
1124 * "aff" is assumed to be an integer affine expression.
1126 * A modulo expression is of the form
1128 * a mod m = a - m * floor(a / m)
1130 * To detect them in aff, we look for terms of the form
1132 * f * m * floor(a / m)
1136 * f * (a - (a mod m)) = f * a - f * (a mod m)
1138 * and extract out -f * (a mod m).
1139 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
1140 * If f < 0, we add ((-f) * (a mod m)) to *pos.
1142 static __isl_give isl_aff
*extract_modulos(__isl_take isl_aff
*aff
,
1143 __isl_keep isl_ast_expr
**pos
, __isl_keep isl_ast_expr
**neg
,
1144 __isl_keep isl_ast_build
*build
)
1146 struct isl_extract_mod_data data
= { build
, aff
, *pos
, *neg
};
1153 ctx
= isl_aff_get_ctx(aff
);
1154 if (!isl_options_get_ast_build_prefer_pdiv(ctx
))
1157 n
= isl_aff_dim(data
.aff
, isl_dim_div
);
1159 return isl_aff_free(aff
);
1160 for (data
.i
= 0; data
.i
< n
; ++data
.i
) {
1161 data
.v
= isl_aff_get_coefficient_val(data
.aff
,
1162 isl_dim_div
, data
.i
);
1164 return isl_aff_free(aff
);
1165 if (isl_val_is_zero(data
.v
) ||
1166 isl_val_is_one(data
.v
) || isl_val_is_negone(data
.v
)) {
1167 isl_val_free(data
.v
);
1170 if (extract_modulo(&data
) < 0)
1171 data
.aff
= isl_aff_free(data
.aff
);
1172 isl_val_free(data
.v
);
1178 data
.aff
= isl_aff_add(data
.aff
, data
.add
);
1185 /* Call "fn" on every non-zero coefficient of "aff",
1186 * passing it in the type of dimension (in terms of the domain),
1187 * the position and the value, as long as "fn" returns isl_bool_true.
1188 * If "reverse" is set, then the coefficients are considered in reverse order
1191 static isl_bool
every_non_zero_coefficient(__isl_keep isl_aff
*aff
,
1193 isl_bool (*fn
)(enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
,
1198 enum isl_dim_type t
[] = { isl_dim_param
, isl_dim_in
, isl_dim_div
};
1199 enum isl_dim_type l
[] = { isl_dim_param
, isl_dim_set
, isl_dim_div
};
1202 for (i
= 0; i
< 3; ++i
) {
1205 n
= isl_aff_dim(aff
, t
[i
]);
1207 return isl_bool_error
;
1208 for (j
= 0; j
< n
; ++j
) {
1212 pos
= reverse
? n
- 1 - j
: j
;
1213 v
= isl_aff_get_coefficient_val(aff
, t
[i
], pos
);
1214 ok
= isl_val_is_zero(v
);
1216 ok
= fn(l
[i
], pos
, v
, user
);
1224 return isl_bool_true
;
1227 /* Internal data structure for extract_rational.
1229 * "d" is the denominator of the original affine expression.
1230 * "ls" is its domain local space.
1231 * "rat" collects the rational part.
1233 struct isl_ast_extract_rational_data
{
1235 isl_local_space
*ls
;
1240 /* Given a non-zero term in an affine expression equal to "v" times
1241 * the variable of type "type" at position "pos",
1242 * add it to data->rat if "v" is not a multiple of data->d.
1244 static isl_bool
add_rational(enum isl_dim_type type
, int pos
,
1245 __isl_take isl_val
*v
, void *user
)
1247 struct isl_ast_extract_rational_data
*data
= user
;
1250 if (isl_val_is_divisible_by(v
, data
->d
)) {
1252 return isl_bool_true
;
1254 rat
= isl_aff_var_on_domain(isl_local_space_copy(data
->ls
), type
, pos
);
1255 rat
= isl_aff_scale_val(rat
, v
);
1256 data
->rat
= isl_aff_add(data
->rat
, rat
);
1257 return isl_bool_true
;
1260 /* Check if aff involves any non-integer coefficients.
1261 * If so, split aff into
1263 * aff = aff1 + (aff2 / d)
1265 * with both aff1 and aff2 having only integer coefficients.
1266 * Return aff1 and add (aff2 / d) to *expr.
1268 static __isl_give isl_aff
*extract_rational(__isl_take isl_aff
*aff
,
1269 __isl_keep isl_ast_expr
**expr
, __isl_keep isl_ast_build
*build
)
1271 struct isl_ast_extract_rational_data data
= { NULL
};
1272 isl_ast_expr
*rat_expr
;
1277 data
.d
= isl_aff_get_denominator_val(aff
);
1280 if (isl_val_is_one(data
.d
)) {
1281 isl_val_free(data
.d
);
1285 aff
= isl_aff_scale_val(aff
, isl_val_copy(data
.d
));
1287 data
.ls
= isl_aff_get_domain_local_space(aff
);
1288 data
.rat
= isl_aff_zero_on_domain(isl_local_space_copy(data
.ls
));
1290 if (every_non_zero_coefficient(aff
, 0, &add_rational
, &data
) < 0)
1293 v
= isl_aff_get_constant_val(aff
);
1294 if (isl_val_is_divisible_by(v
, data
.d
)) {
1299 rat_0
= isl_aff_val_on_domain(isl_local_space_copy(data
.ls
), v
);
1300 data
.rat
= isl_aff_add(data
.rat
, rat_0
);
1303 isl_local_space_free(data
.ls
);
1305 aff
= isl_aff_sub(aff
, isl_aff_copy(data
.rat
));
1306 aff
= isl_aff_scale_down_val(aff
, isl_val_copy(data
.d
));
1308 rat_expr
= div_mod(isl_ast_expr_op_div
, data
.rat
, data
.d
, build
);
1309 *expr
= ast_expr_add(*expr
, rat_expr
);
1313 isl_aff_free(data
.rat
);
1314 isl_local_space_free(data
.ls
);
1316 isl_val_free(data
.d
);
1320 /* Internal data structure for isl_ast_expr_from_aff.
1322 * "term" contains the information for adding a term.
1323 * "expr" collects the results.
1325 struct isl_ast_add_terms_data
{
1326 struct isl_ast_add_term_data
*term
;
1330 /* Given a non-zero term in an affine expression equal to "v" times
1331 * the variable of type "type" at position "pos",
1332 * add the corresponding AST expression to data->expr.
1334 static isl_bool
add_term(enum isl_dim_type type
, int pos
,
1335 __isl_take isl_val
*v
, void *user
)
1337 struct isl_ast_add_terms_data
*data
= user
;
1340 isl_ast_expr_add_term(data
->expr
, type
, pos
, v
, data
->term
);
1342 return isl_bool_true
;
1345 /* Add terms to "expr" for each variable in "aff".
1346 * The result is simplified in terms of data->build->domain.
1348 static __isl_give isl_ast_expr
*add_terms(__isl_take isl_ast_expr
*expr
,
1349 __isl_keep isl_aff
*aff
, struct isl_ast_add_term_data
*data
)
1351 struct isl_ast_add_terms_data terms_data
= { data
, expr
};
1353 if (every_non_zero_coefficient(aff
, 0, &add_term
, &terms_data
) < 0)
1354 return isl_ast_expr_free(terms_data
.expr
);
1356 return terms_data
.expr
;
1359 /* Construct an isl_ast_expr that evaluates the affine expression "aff".
1360 * The result is simplified in terms of build->domain.
1362 * We first extract hidden modulo computations from the affine expression
1363 * and then add terms for each variable with a non-zero coefficient.
1364 * Finally, if the affine expression has a non-trivial denominator,
1365 * we divide the resulting isl_ast_expr by this denominator.
1367 __isl_give isl_ast_expr
*isl_ast_expr_from_aff(__isl_take isl_aff
*aff
,
1368 __isl_keep isl_ast_build
*build
)
1370 isl_ctx
*ctx
= isl_aff_get_ctx(aff
);
1371 isl_ast_expr
*expr
, *expr_neg
;
1372 struct isl_ast_add_term_data term_data
;
1377 expr
= isl_ast_expr_alloc_int_si(ctx
, 0);
1378 expr_neg
= isl_ast_expr_alloc_int_si(ctx
, 0);
1380 aff
= extract_rational(aff
, &expr
, build
);
1382 aff
= extract_modulos(aff
, &expr
, &expr_neg
, build
);
1383 expr
= ast_expr_sub(expr
, expr_neg
);
1385 term_data
.build
= build
;
1386 term_data
.ls
= isl_aff_get_domain_local_space(aff
);
1387 term_data
.cst
= isl_aff_get_constant_val(aff
);
1388 expr
= add_terms(expr
, aff
, &term_data
);
1390 expr
= isl_ast_expr_add_int(expr
, term_data
.cst
);
1391 isl_local_space_free(term_data
.ls
);
1397 /* Internal data structure for coefficients_of_sign.
1399 * "sign" is the sign of the coefficients that should be retained.
1400 * "aff" is the affine expression of which some coefficients are zeroed out.
1402 struct isl_ast_coefficients_of_sign_data
{
1407 /* Clear the specified coefficient of data->aff if the value "v"
1408 * does not have the required sign.
1410 static isl_bool
clear_opposite_sign(enum isl_dim_type type
, int pos
,
1411 __isl_take isl_val
*v
, void *user
)
1413 struct isl_ast_coefficients_of_sign_data
*data
= user
;
1415 if (type
== isl_dim_set
)
1417 if (data
->sign
* isl_val_sgn(v
) < 0)
1418 data
->aff
= isl_aff_set_coefficient_si(data
->aff
, type
, pos
, 0);
1421 return isl_bool_true
;
1424 /* Extract the coefficients of "aff" (excluding the constant term)
1425 * that have the given sign.
1427 * Take a copy of "aff" and clear the coefficients that do not have
1428 * the required sign.
1429 * Consider the coefficients in reverse order since clearing
1430 * the coefficient of an integer division in data.aff
1431 * could result in the removal of that integer division from data.aff,
1432 * changing the positions of all subsequent integer divisions of data.aff,
1433 * while those of "aff" remain the same.
1435 static __isl_give isl_aff
*coefficients_of_sign(__isl_take isl_aff
*aff
,
1438 struct isl_ast_coefficients_of_sign_data data
;
1441 data
.aff
= isl_aff_copy(aff
);
1442 if (every_non_zero_coefficient(aff
, 1, &clear_opposite_sign
, &data
) < 0)
1443 data
.aff
= isl_aff_free(data
.aff
);
1446 data
.aff
= isl_aff_set_constant_si(data
.aff
, 0);
1451 /* Should the constant term "v" be considered positive?
1453 * A positive constant will be added to "pos" by the caller,
1454 * while a negative constant will be added to "neg".
1455 * If either "pos" or "neg" is exactly zero, then we prefer
1456 * to add the constant "v" to that side, irrespective of the sign of "v".
1457 * This results in slightly shorter expressions and may reduce the risk
1460 static isl_bool
constant_is_considered_positive(__isl_keep isl_val
*v
,
1461 __isl_keep isl_ast_expr
*pos
, __isl_keep isl_ast_expr
*neg
)
1465 zero
= ast_expr_is_zero(pos
);
1466 if (zero
< 0 || zero
)
1468 zero
= ast_expr_is_zero(neg
);
1469 if (zero
< 0 || zero
)
1470 return isl_bool_not(zero
);
1471 return isl_val_is_pos(v
);
1474 /* Check if the equality
1478 * represents a stride constraint on the integer division "pos".
1480 * In particular, if the integer division "pos" is equal to
1484 * then check if aff is equal to
1490 * If so, the equality is exactly
1494 * Note that in principle we could also accept
1498 * where e and e' differ by a constant.
1500 static isl_bool
is_stride_constraint(__isl_keep isl_aff
*aff
, int pos
)
1506 div
= isl_aff_get_div(aff
, pos
);
1507 c
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, pos
);
1508 d
= isl_aff_get_denominator_val(div
);
1509 eq
= isl_val_abs_eq(c
, d
);
1510 if (eq
>= 0 && eq
) {
1511 aff
= isl_aff_copy(aff
);
1512 aff
= isl_aff_set_coefficient_si(aff
, isl_dim_div
, pos
, 0);
1513 div
= isl_aff_scale_val(div
, d
);
1514 if (isl_val_is_pos(c
))
1515 div
= isl_aff_neg(div
);
1516 eq
= isl_aff_plain_is_equal(div
, aff
);
1526 /* Are all coefficients of "aff" (zero or) negative?
1528 static isl_bool
all_negative_coefficients(__isl_keep isl_aff
*aff
)
1533 n
= isl_aff_dim(aff
, isl_dim_param
);
1535 return isl_bool_error
;
1536 for (i
= 0; i
< n
; ++i
)
1537 if (isl_aff_coefficient_sgn(aff
, isl_dim_param
, i
) > 0)
1538 return isl_bool_false
;
1540 n
= isl_aff_dim(aff
, isl_dim_in
);
1542 return isl_bool_error
;
1543 for (i
= 0; i
< n
; ++i
)
1544 if (isl_aff_coefficient_sgn(aff
, isl_dim_in
, i
) > 0)
1545 return isl_bool_false
;
1547 return isl_bool_true
;
1550 /* Give an equality of the form
1552 * aff = e - d floor(e/d) = 0
1556 * aff = -e + d floor(e/d) = 0
1558 * with the integer division "pos" equal to floor(e/d),
1559 * construct the AST expression
1561 * (isl_ast_expr_op_eq,
1562 * (isl_ast_expr_op_zdiv_r, expr(e), expr(d)), expr(0))
1564 * If e only has negative coefficients, then construct
1566 * (isl_ast_expr_op_eq,
1567 * (isl_ast_expr_op_zdiv_r, expr(-e), expr(d)), expr(0))
1571 static __isl_give isl_ast_expr
*extract_stride_constraint(
1572 __isl_take isl_aff
*aff
, int pos
, __isl_keep isl_ast_build
*build
)
1577 isl_ast_expr
*expr
, *cst
;
1582 ctx
= isl_aff_get_ctx(aff
);
1584 c
= isl_aff_get_coefficient_val(aff
, isl_dim_div
, pos
);
1585 aff
= isl_aff_set_coefficient_si(aff
, isl_dim_div
, pos
, 0);
1587 all_neg
= all_negative_coefficients(aff
);
1589 aff
= isl_aff_free(aff
);
1591 aff
= isl_aff_neg(aff
);
1593 cst
= isl_ast_expr_from_val(isl_val_abs(c
));
1594 expr
= isl_ast_expr_from_aff(aff
, build
);
1596 expr
= isl_ast_expr_alloc_binary(isl_ast_expr_op_zdiv_r
, expr
, cst
);
1597 cst
= isl_ast_expr_alloc_int_si(ctx
, 0);
1598 expr
= isl_ast_expr_alloc_binary(isl_ast_expr_op_eq
, expr
, cst
);
1603 /* Construct an isl_ast_expr evaluating
1605 * "expr_pos" == "expr_neg", if "eq" is set, or
1606 * "expr_pos" >= "expr_neg", if "eq" is not set
1608 * However, if "expr_pos" is an integer constant (and "expr_neg" is not),
1609 * then the two expressions are interchanged. This ensures that,
1610 * e.g., "i <= 5" is constructed rather than "5 >= i".
1612 static __isl_give isl_ast_expr
*construct_constraint_expr(int eq
,
1613 __isl_take isl_ast_expr
*expr_pos
, __isl_take isl_ast_expr
*expr_neg
)
1616 enum isl_ast_expr_op_type type
;
1617 int pos_is_cst
, neg_is_cst
;
1619 pos_is_cst
= isl_ast_expr_get_type(expr_pos
) == isl_ast_expr_int
;
1620 neg_is_cst
= isl_ast_expr_get_type(expr_neg
) == isl_ast_expr_int
;
1621 if (pos_is_cst
&& !neg_is_cst
) {
1622 type
= eq
? isl_ast_expr_op_eq
: isl_ast_expr_op_le
;
1623 expr
= isl_ast_expr_alloc_binary(type
, expr_neg
, expr_pos
);
1625 type
= eq
? isl_ast_expr_op_eq
: isl_ast_expr_op_ge
;
1626 expr
= isl_ast_expr_alloc_binary(type
, expr_pos
, expr_neg
);
1632 /* Construct an isl_ast_expr that evaluates the condition "aff" == 0
1633 * (if "eq" is set) or "aff" >= 0 (otherwise).
1634 * The result is simplified in terms of build->domain.
1636 * We first extract hidden modulo computations from "aff"
1637 * and then collect all the terms with a positive coefficient in cons_pos
1638 * and the terms with a negative coefficient in cons_neg.
1640 * The result is then essentially of the form
1642 * (isl_ast_expr_op_ge, expr(pos), expr(-neg)))
1646 * (isl_ast_expr_op_eq, expr(pos), expr(-neg)))
1648 * However, if there are no terms with positive coefficients (or no terms
1649 * with negative coefficients), then the constant term is added to "pos"
1650 * (or "neg"), ignoring the sign of the constant term.
1652 static __isl_give isl_ast_expr
*isl_ast_expr_from_constraint_no_stride(
1653 int eq
, __isl_take isl_aff
*aff
, __isl_keep isl_ast_build
*build
)
1655 isl_bool cst_is_pos
;
1657 isl_ast_expr
*expr_pos
;
1658 isl_ast_expr
*expr_neg
;
1659 isl_aff
*aff_pos
, *aff_neg
;
1660 struct isl_ast_add_term_data data
;
1662 ctx
= isl_aff_get_ctx(aff
);
1663 expr_pos
= isl_ast_expr_alloc_int_si(ctx
, 0);
1664 expr_neg
= isl_ast_expr_alloc_int_si(ctx
, 0);
1666 aff
= extract_modulos(aff
, &expr_pos
, &expr_neg
, build
);
1669 data
.ls
= isl_aff_get_domain_local_space(aff
);
1670 data
.cst
= isl_aff_get_constant_val(aff
);
1672 aff_pos
= coefficients_of_sign(isl_aff_copy(aff
), 1);
1673 aff_neg
= isl_aff_neg(coefficients_of_sign(aff
, -1));
1675 expr_pos
= add_terms(expr_pos
, aff_pos
, &data
);
1676 data
.cst
= isl_val_neg(data
.cst
);
1677 expr_neg
= add_terms(expr_neg
, aff_neg
, &data
);
1678 data
.cst
= isl_val_neg(data
.cst
);
1679 isl_local_space_free(data
.ls
);
1682 constant_is_considered_positive(data
.cst
, expr_pos
, expr_neg
);
1684 expr_pos
= isl_ast_expr_free(expr_pos
);
1687 expr_pos
= isl_ast_expr_add_int(expr_pos
, data
.cst
);
1689 data
.cst
= isl_val_neg(data
.cst
);
1690 expr_neg
= isl_ast_expr_add_int(expr_neg
, data
.cst
);
1693 isl_aff_free(aff_pos
);
1694 isl_aff_free(aff_neg
);
1695 return construct_constraint_expr(eq
, expr_pos
, expr_neg
);
1698 /* Construct an isl_ast_expr that evaluates the condition "constraint".
1699 * The result is simplified in terms of build->domain.
1701 * We first check if the constraint is an equality of the form
1703 * e - d floor(e/d) = 0
1709 * If so, we convert it to
1711 * (isl_ast_expr_op_eq,
1712 * (isl_ast_expr_op_zdiv_r, expr(e), expr(d)), expr(0))
1714 static __isl_give isl_ast_expr
*isl_ast_expr_from_constraint(
1715 __isl_take isl_constraint
*constraint
, __isl_keep isl_ast_build
*build
)
1722 aff
= isl_constraint_get_aff(constraint
);
1723 eq
= isl_constraint_is_equality(constraint
);
1724 isl_constraint_free(constraint
);
1728 n
= isl_aff_dim(aff
, isl_dim_div
);
1730 aff
= isl_aff_free(aff
);
1732 for (i
= 0; i
< n
; ++i
) {
1734 is_stride
= is_stride_constraint(aff
, i
);
1738 return extract_stride_constraint(aff
, i
, build
);
1741 return isl_ast_expr_from_constraint_no_stride(eq
, aff
, build
);
1747 /* Wrapper around isl_constraint_cmp_last_non_zero for use
1748 * as a callback to isl_constraint_list_sort.
1749 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1750 * apart, then use isl_constraint_plain_cmp instead.
1752 static int cmp_constraint(__isl_keep isl_constraint
*a
,
1753 __isl_keep isl_constraint
*b
, void *user
)
1757 cmp
= isl_constraint_cmp_last_non_zero(a
, b
);
1760 return isl_constraint_plain_cmp(a
, b
);
1763 /* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1764 * The result is simplified in terms of build->domain.
1766 * If "bset" is not bounded by any constraint, then we construct
1767 * the expression "1", i.e., "true".
1769 * Otherwise, we sort the constraints, putting constraints that involve
1770 * integer divisions after those that do not, and construct an "and"
1771 * of the ast expressions of the individual constraints.
1773 * Each constraint is added to the generated constraints of the build
1774 * after it has been converted to an AST expression so that it can be used
1775 * to simplify the following constraints. This may change the truth value
1776 * of subsequent constraints that do not satisfy the earlier constraints,
1777 * but this does not affect the outcome of the conjunction as it is
1778 * only true if all the conjuncts are true (no matter in what order
1779 * they are evaluated). In particular, the constraints that do not
1780 * involve integer divisions may serve to simplify some constraints
1781 * that do involve integer divisions.
1783 __isl_give isl_ast_expr
*isl_ast_build_expr_from_basic_set(
1784 __isl_keep isl_ast_build
*build
, __isl_take isl_basic_set
*bset
)
1789 isl_constraint_list
*list
;
1793 list
= isl_basic_set_get_constraint_list(bset
);
1794 isl_basic_set_free(bset
);
1795 list
= isl_constraint_list_sort(list
, &cmp_constraint
, NULL
);
1796 n
= isl_constraint_list_n_constraint(list
);
1800 isl_ctx
*ctx
= isl_constraint_list_get_ctx(list
);
1801 isl_constraint_list_free(list
);
1802 return isl_ast_expr_alloc_int_si(ctx
, 1);
1805 build
= isl_ast_build_copy(build
);
1807 c
= isl_constraint_list_get_constraint(list
, 0);
1808 bset
= isl_basic_set_from_constraint(isl_constraint_copy(c
));
1809 set
= isl_set_from_basic_set(bset
);
1810 res
= isl_ast_expr_from_constraint(c
, build
);
1811 build
= isl_ast_build_restrict_generated(build
, set
);
1813 for (i
= 1; i
< n
; ++i
) {
1816 c
= isl_constraint_list_get_constraint(list
, i
);
1817 bset
= isl_basic_set_from_constraint(isl_constraint_copy(c
));
1818 set
= isl_set_from_basic_set(bset
);
1819 expr
= isl_ast_expr_from_constraint(c
, build
);
1820 build
= isl_ast_build_restrict_generated(build
, set
);
1821 res
= isl_ast_expr_and(res
, expr
);
1824 isl_constraint_list_free(list
);
1825 isl_ast_build_free(build
);
1829 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1830 * The result is simplified in terms of build->domain.
1832 * If "set" is an (obviously) empty set, then return the expression "0".
1834 * If there are multiple disjuncts in the description of the set,
1835 * then subsequent disjuncts are simplified in a context where
1836 * the previous disjuncts have been removed from build->domain.
1837 * In particular, constraints that ensure that there is no overlap
1838 * with these previous disjuncts, can be removed.
1839 * This is mostly useful for disjuncts that are only defined by
1840 * a single constraint (relative to the build domain) as the opposite
1841 * of that single constraint can then be removed from the other disjuncts.
1842 * In order not to increase the number of disjuncts in the build domain
1843 * after subtracting the previous disjuncts of "set", the simple hull
1844 * is computed after taking the difference with each of these disjuncts.
1845 * This means that constraints that prevent overlap with a union
1846 * of multiple previous disjuncts are not removed.
1848 * "set" lives in the internal schedule space.
1850 __isl_give isl_ast_expr
*isl_ast_build_expr_from_set_internal(
1851 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
1855 isl_basic_set
*bset
;
1856 isl_basic_set_list
*list
;
1860 list
= isl_set_get_basic_set_list(set
);
1863 n
= isl_basic_set_list_n_basic_set(list
);
1867 isl_ctx
*ctx
= isl_ast_build_get_ctx(build
);
1868 isl_basic_set_list_free(list
);
1869 return isl_ast_expr_from_val(isl_val_zero(ctx
));
1872 domain
= isl_ast_build_get_domain(build
);
1874 bset
= isl_basic_set_list_get_basic_set(list
, 0);
1875 set
= isl_set_from_basic_set(isl_basic_set_copy(bset
));
1876 res
= isl_ast_build_expr_from_basic_set(build
, bset
);
1878 for (i
= 1; i
< n
; ++i
) {
1882 rest
= isl_set_subtract(isl_set_copy(domain
), set
);
1883 rest
= isl_set_from_basic_set(isl_set_simple_hull(rest
));
1884 domain
= isl_set_intersect(domain
, rest
);
1885 bset
= isl_basic_set_list_get_basic_set(list
, i
);
1886 set
= isl_set_from_basic_set(isl_basic_set_copy(bset
));
1887 bset
= isl_basic_set_gist(bset
,
1888 isl_set_simple_hull(isl_set_copy(domain
)));
1889 expr
= isl_ast_build_expr_from_basic_set(build
, bset
);
1890 res
= isl_ast_expr_or(res
, expr
);
1893 isl_set_free(domain
);
1895 isl_basic_set_list_free(list
);
1899 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1900 * The result is simplified in terms of build->domain.
1902 * If "set" is an (obviously) empty set, then return the expression "0".
1904 * "set" lives in the external schedule space.
1906 * The internal AST expression generation assumes that there are
1907 * no unknown divs, so make sure an explicit representation is available.
1908 * Since the set comes from the outside, it may have constraints that
1909 * are redundant with respect to the build domain. Remove them first.
1911 __isl_give isl_ast_expr
*isl_ast_build_expr_from_set(
1912 __isl_keep isl_ast_build
*build
, __isl_take isl_set
*set
)
1916 needs_map
= isl_ast_build_need_schedule_map(build
);
1917 if (needs_map
< 0) {
1918 set
= isl_set_free(set
);
1919 } else if (needs_map
) {
1921 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
1922 set
= isl_set_preimage_multi_aff(set
, ma
);
1925 set
= isl_set_compute_divs(set
);
1926 set
= isl_ast_build_compute_gist(build
, set
);
1927 return isl_ast_build_expr_from_set_internal(build
, set
);
1930 /* State of data about previous pieces in
1931 * isl_ast_build_expr_from_pw_aff_internal.
1933 * isl_state_none: no data about previous pieces
1934 * isl_state_single: data about a single previous piece
1935 * isl_state_min: data represents minimum of several pieces
1936 * isl_state_max: data represents maximum of several pieces
1938 enum isl_from_pw_aff_state
{
1945 /* Internal date structure representing a single piece in the input of
1946 * isl_ast_build_expr_from_pw_aff_internal.
1948 * If "state" is isl_state_none, then "set_list" and "aff_list" are not used.
1949 * If "state" is isl_state_single, then "set_list" and "aff_list" contain the
1950 * single previous subpiece.
1951 * If "state" is isl_state_min, then "set_list" and "aff_list" contain
1952 * a sequence of several previous subpieces that are equal to the minimum
1953 * of the entries in "aff_list" over the union of "set_list"
1954 * If "state" is isl_state_max, then "set_list" and "aff_list" contain
1955 * a sequence of several previous subpieces that are equal to the maximum
1956 * of the entries in "aff_list" over the union of "set_list"
1958 * During the construction of the pieces, "set" is NULL.
1959 * After the construction, "set" is set to the union of the elements
1960 * in "set_list", at which point "set_list" is set to NULL.
1962 struct isl_from_pw_aff_piece
{
1963 enum isl_from_pw_aff_state state
;
1965 isl_set_list
*set_list
;
1966 isl_aff_list
*aff_list
;
1969 /* Internal data structure for isl_ast_build_expr_from_pw_aff_internal.
1971 * "build" specifies the domain against which the result is simplified.
1972 * "dom" is the domain of the entire isl_pw_aff.
1974 * "n" is the number of pieces constructed already.
1975 * In particular, during the construction of the pieces, "n" points to
1976 * the piece that is being constructed. After the construction of the
1977 * pieces, "n" is set to the total number of pieces.
1978 * "max" is the total number of allocated entries.
1979 * "p" contains the individual pieces.
1981 struct isl_from_pw_aff_data
{
1982 isl_ast_build
*build
;
1987 struct isl_from_pw_aff_piece
*p
;
1990 /* Initialize "data" based on "build" and "pa".
1992 static isl_stat
isl_from_pw_aff_data_init(struct isl_from_pw_aff_data
*data
,
1993 __isl_keep isl_ast_build
*build
, __isl_keep isl_pw_aff
*pa
)
1998 ctx
= isl_pw_aff_get_ctx(pa
);
1999 n
= isl_pw_aff_n_piece(pa
);
2001 return isl_stat_error
;
2003 isl_die(ctx
, isl_error_invalid
,
2004 "cannot handle void expression", return isl_stat_error
);
2006 data
->p
= isl_calloc_array(ctx
, struct isl_from_pw_aff_piece
, n
);
2008 return isl_stat_error
;
2009 data
->build
= build
;
2010 data
->dom
= isl_pw_aff_domain(isl_pw_aff_copy(pa
));
2016 /* Free all memory allocated for "data".
2018 static void isl_from_pw_aff_data_clear(struct isl_from_pw_aff_data
*data
)
2022 isl_set_free(data
->dom
);
2026 for (i
= 0; i
< data
->max
; ++i
) {
2027 isl_set_free(data
->p
[i
].set
);
2028 isl_set_list_free(data
->p
[i
].set_list
);
2029 isl_aff_list_free(data
->p
[i
].aff_list
);
2034 /* Initialize the current entry of "data" to an unused piece.
2036 static void set_none(struct isl_from_pw_aff_data
*data
)
2038 data
->p
[data
->n
].state
= isl_state_none
;
2039 data
->p
[data
->n
].set_list
= NULL
;
2040 data
->p
[data
->n
].aff_list
= NULL
;
2043 /* Store "set" and "aff" in the current entry of "data" as a single subpiece.
2045 static void set_single(struct isl_from_pw_aff_data
*data
,
2046 __isl_take isl_set
*set
, __isl_take isl_aff
*aff
)
2048 data
->p
[data
->n
].state
= isl_state_single
;
2049 data
->p
[data
->n
].set_list
= isl_set_list_from_set(set
);
2050 data
->p
[data
->n
].aff_list
= isl_aff_list_from_aff(aff
);
2053 /* Extend the current entry of "data" with "set" and "aff"
2054 * as a minimum expression.
2056 static isl_stat
extend_min(struct isl_from_pw_aff_data
*data
,
2057 __isl_take isl_set
*set
, __isl_take isl_aff
*aff
)
2060 data
->p
[n
].state
= isl_state_min
;
2061 data
->p
[n
].set_list
= isl_set_list_add(data
->p
[n
].set_list
, set
);
2062 data
->p
[n
].aff_list
= isl_aff_list_add(data
->p
[n
].aff_list
, aff
);
2064 if (!data
->p
[n
].set_list
|| !data
->p
[n
].aff_list
)
2065 return isl_stat_error
;
2069 /* Extend the current entry of "data" with "set" and "aff"
2070 * as a maximum expression.
2072 static isl_stat
extend_max(struct isl_from_pw_aff_data
*data
,
2073 __isl_take isl_set
*set
, __isl_take isl_aff
*aff
)
2076 data
->p
[n
].state
= isl_state_max
;
2077 data
->p
[n
].set_list
= isl_set_list_add(data
->p
[n
].set_list
, set
);
2078 data
->p
[n
].aff_list
= isl_aff_list_add(data
->p
[n
].aff_list
, aff
);
2080 if (!data
->p
[n
].set_list
|| !data
->p
[n
].aff_list
)
2081 return isl_stat_error
;
2085 /* Extend the domain of the current entry of "data", which is assumed
2086 * to contain a single subpiece, with "set". If "replace" is set,
2087 * then also replace the affine function by "aff". Otherwise,
2088 * simply free "aff".
2090 static isl_stat
extend_domain(struct isl_from_pw_aff_data
*data
,
2091 __isl_take isl_set
*set
, __isl_take isl_aff
*aff
, int replace
)
2096 set_n
= isl_set_list_get_set(data
->p
[n
].set_list
, 0);
2097 set_n
= isl_set_union(set_n
, set
);
2098 data
->p
[n
].set_list
=
2099 isl_set_list_set_set(data
->p
[n
].set_list
, 0, set_n
);
2102 data
->p
[n
].aff_list
=
2103 isl_aff_list_set_aff(data
->p
[n
].aff_list
, 0, aff
);
2107 if (!data
->p
[n
].set_list
|| !data
->p
[n
].aff_list
)
2108 return isl_stat_error
;
2112 /* Construct an isl_ast_expr from "list" within "build".
2113 * If "state" is isl_state_single, then "list" contains a single entry and
2114 * an isl_ast_expr is constructed for that entry.
2115 * Otherwise a min or max expression is constructed from "list"
2116 * depending on "state".
2118 static __isl_give isl_ast_expr
*ast_expr_from_aff_list(
2119 __isl_take isl_aff_list
*list
, enum isl_from_pw_aff_state state
,
2120 __isl_keep isl_ast_build
*build
)
2125 isl_ast_expr
*expr
= NULL
;
2126 enum isl_ast_expr_op_type op_type
;
2128 if (state
== isl_state_single
) {
2129 aff
= isl_aff_list_get_aff(list
, 0);
2130 isl_aff_list_free(list
);
2131 return isl_ast_expr_from_aff(aff
, build
);
2133 n
= isl_aff_list_n_aff(list
);
2136 op_type
= state
== isl_state_min
? isl_ast_expr_op_min
2137 : isl_ast_expr_op_max
;
2138 expr
= isl_ast_expr_alloc_op(isl_ast_build_get_ctx(build
), op_type
, n
);
2140 for (i
= 0; i
< n
; ++i
) {
2141 isl_ast_expr
*expr_i
;
2143 aff
= isl_aff_list_get_aff(list
, i
);
2144 expr_i
= isl_ast_expr_from_aff(aff
, build
);
2145 expr
= isl_ast_expr_op_add_arg(expr
, expr_i
);
2148 isl_aff_list_free(list
);
2151 isl_aff_list_free(list
);
2152 isl_ast_expr_free(expr
);
2156 /* Extend the list of expressions in "next" to take into account
2157 * the piece at position "pos" in "data", allowing for a further extension
2158 * for the next piece(s).
2159 * In particular, "next" is extended with a select operation that selects
2160 * an isl_ast_expr corresponding to data->aff_list on data->set and
2161 * to an expression that will be filled in by later calls.
2162 * Return a pointer to the arguments of this select operation.
2163 * Afterwards, the state of "data" is set to isl_state_none.
2165 * The constraints of data->set are added to the generated
2166 * constraints of the build such that they can be exploited to simplify
2167 * the AST expression constructed from data->aff_list.
2169 static isl_ast_expr_list
**add_intermediate_piece(
2170 struct isl_from_pw_aff_data
*data
,
2171 int pos
, isl_ast_expr_list
**next
)
2174 isl_ast_build
*build
;
2175 isl_ast_expr
*ternary
, *arg
;
2176 isl_set
*set
, *gist
;
2178 set
= data
->p
[pos
].set
;
2179 data
->p
[pos
].set
= NULL
;
2180 ctx
= isl_ast_build_get_ctx(data
->build
);
2181 ternary
= isl_ast_expr_alloc_op(ctx
, isl_ast_expr_op_select
, 3);
2182 gist
= isl_set_gist(isl_set_copy(set
), isl_set_copy(data
->dom
));
2183 arg
= isl_ast_build_expr_from_set_internal(data
->build
, gist
);
2184 ternary
= isl_ast_expr_op_add_arg(ternary
, arg
);
2185 build
= isl_ast_build_copy(data
->build
);
2186 build
= isl_ast_build_restrict_generated(build
, set
);
2187 arg
= ast_expr_from_aff_list(data
->p
[pos
].aff_list
,
2188 data
->p
[pos
].state
, build
);
2189 data
->p
[pos
].aff_list
= NULL
;
2190 isl_ast_build_free(build
);
2191 ternary
= isl_ast_expr_op_add_arg(ternary
, arg
);
2192 data
->p
[pos
].state
= isl_state_none
;
2196 *next
= isl_ast_expr_list_add(*next
, ternary
);
2197 return &ternary
->u
.op
.args
;
2200 /* Extend the list of expressions in "next" to take into account
2201 * the final piece, located at position "pos" in "data".
2202 * In particular, "next" is extended with an expression
2203 * to evaluate data->aff_list and the domain is ignored.
2204 * Return isl_stat_ok on success and isl_stat_error on failure.
2206 * The constraints of data->set are however added to the generated
2207 * constraints of the build such that they can be exploited to simplify
2208 * the AST expression constructed from data->aff_list.
2210 static isl_stat
add_last_piece(struct isl_from_pw_aff_data
*data
,
2211 int pos
, isl_ast_expr_list
**next
)
2213 isl_ast_build
*build
;
2216 if (data
->p
[pos
].state
== isl_state_none
)
2217 isl_die(isl_ast_build_get_ctx(data
->build
), isl_error_invalid
,
2218 "cannot handle void expression", return isl_stat_error
);
2220 build
= isl_ast_build_copy(data
->build
);
2221 build
= isl_ast_build_restrict_generated(build
, data
->p
[pos
].set
);
2222 data
->p
[pos
].set
= NULL
;
2223 last
= ast_expr_from_aff_list(data
->p
[pos
].aff_list
,
2224 data
->p
[pos
].state
, build
);
2225 *next
= isl_ast_expr_list_add(*next
, last
);
2226 data
->p
[pos
].aff_list
= NULL
;
2227 isl_ast_build_free(build
);
2228 data
->p
[pos
].state
= isl_state_none
;
2230 return isl_stat_error
;
2235 /* Return -1 if the piece "p1" should be sorted before "p2"
2236 * and 1 if it should be sorted after "p2".
2237 * Return 0 if they do not need to be sorted in a specific order.
2239 * Pieces are sorted according to the number of disjuncts
2242 static int sort_pieces_cmp(const void *p1
, const void *p2
, void *arg
)
2244 const struct isl_from_pw_aff_piece
*piece1
= p1
;
2245 const struct isl_from_pw_aff_piece
*piece2
= p2
;
2248 n1
= isl_set_n_basic_set(piece1
->set
);
2249 n2
= isl_set_n_basic_set(piece2
->set
);
2254 /* Construct an isl_ast_expr from the pieces in "data".
2255 * Return the result or NULL on failure.
2257 * When this function is called, data->n points to the current piece.
2258 * If this is an effective piece, then first increment data->n such
2259 * that data->n contains the number of pieces.
2260 * The "set_list" fields are subsequently replaced by the corresponding
2261 * "set" fields, after which the pieces are sorted according to
2262 * the number of disjuncts in these "set" fields.
2264 * Construct intermediate AST expressions for the initial pieces and
2265 * finish off with the final pieces.
2267 * Any piece that is not the very first is added to the list of arguments
2268 * of the previously constructed piece.
2269 * In order not to have to special case the first piece,
2270 * an extra list is created to hold the final result.
2272 static isl_ast_expr
*build_pieces(struct isl_from_pw_aff_data
*data
)
2276 isl_ast_expr_list
*res_list
;
2277 isl_ast_expr_list
**next
= &res_list
;
2280 if (data
->p
[data
->n
].state
!= isl_state_none
)
2282 ctx
= isl_ast_build_get_ctx(data
->build
);
2284 isl_die(ctx
, isl_error_invalid
,
2285 "cannot handle void expression", return NULL
);
2287 for (i
= 0; i
< data
->n
; ++i
) {
2288 data
->p
[i
].set
= isl_set_list_union(data
->p
[i
].set_list
);
2289 if (data
->p
[i
].state
!= isl_state_single
)
2290 data
->p
[i
].set
= isl_set_coalesce(data
->p
[i
].set
);
2291 data
->p
[i
].set_list
= NULL
;
2294 if (isl_sort(data
->p
, data
->n
, sizeof(data
->p
[0]),
2295 &sort_pieces_cmp
, NULL
) < 0)
2298 res_list
= isl_ast_expr_list_alloc(ctx
, 1);
2301 for (i
= 0; i
+ 1 < data
->n
; ++i
) {
2302 next
= add_intermediate_piece(data
, i
, next
);
2307 if (add_last_piece(data
, data
->n
- 1, next
) < 0)
2310 res
= isl_ast_expr_list_get_at(res_list
, 0);
2311 isl_ast_expr_list_free(res_list
);
2314 isl_ast_expr_list_free(res_list
);
2318 /* Is the domain of the current entry of "data", which is assumed
2319 * to contain a single subpiece, a subset of "set"?
2321 static isl_bool
single_is_subset(struct isl_from_pw_aff_data
*data
,
2322 __isl_keep isl_set
*set
)
2327 set_n
= isl_set_list_get_set(data
->p
[data
->n
].set_list
, 0);
2328 subset
= isl_set_is_subset(set_n
, set
);
2329 isl_set_free(set_n
);
2334 /* Is "aff" a rational expression, i.e., does it have a denominator
2335 * different from one?
2337 static isl_bool
aff_is_rational(__isl_keep isl_aff
*aff
)
2342 den
= isl_aff_get_denominator_val(aff
);
2343 rational
= isl_bool_not(isl_val_is_one(den
));
2349 /* Does "list" consist of a single rational affine expression?
2351 static isl_bool
is_single_rational_aff(__isl_keep isl_aff_list
*list
)
2357 n
= isl_aff_list_n_aff(list
);
2359 return isl_bool_error
;
2361 return isl_bool_false
;
2362 aff
= isl_aff_list_get_aff(list
, 0);
2363 rational
= aff_is_rational(aff
);
2369 /* Can the list of subpieces in the last piece of "data" be extended with
2370 * "set" and "aff" based on "test"?
2371 * In particular, is it the case for each entry (set_i, aff_i) that
2373 * test(aff, aff_i) holds on set_i, and
2374 * test(aff_i, aff) holds on set?
2376 * "test" returns the set of elements where the tests holds, meaning
2377 * that test(aff_i, aff) holds on set if set is a subset of test(aff_i, aff).
2379 * This function is used to detect min/max expressions.
2380 * If the ast_build_detect_min_max option is turned off, then
2381 * do not even try and perform any detection and return false instead.
2383 * Rational affine expressions are not considered for min/max expressions
2384 * since the combined expression will be defined on the union of the domains,
2385 * while a rational expression may only yield integer values
2386 * on its own definition domain.
2388 static isl_bool
extends(struct isl_from_pw_aff_data
*data
,
2389 __isl_keep isl_set
*set
, __isl_keep isl_aff
*aff
,
2390 __isl_give isl_basic_set
*(*test
)(__isl_take isl_aff
*aff1
,
2391 __isl_take isl_aff
*aff2
))
2395 isl_bool is_rational
;
2399 is_rational
= aff_is_rational(aff
);
2400 if (is_rational
>= 0 && !is_rational
)
2401 is_rational
= is_single_rational_aff(data
->p
[data
->n
].aff_list
);
2402 if (is_rational
< 0 || is_rational
)
2403 return isl_bool_not(is_rational
);
2405 ctx
= isl_ast_build_get_ctx(data
->build
);
2406 if (!isl_options_get_ast_build_detect_min_max(ctx
))
2407 return isl_bool_false
;
2409 n
= isl_set_list_n_set(data
->p
[data
->n
].set_list
);
2411 return isl_bool_error
;
2413 dom
= isl_ast_build_get_domain(data
->build
);
2414 set
= isl_set_intersect(dom
, isl_set_copy(set
));
2416 for (i
= 0; i
< n
; ++i
) {
2419 isl_set
*dom
, *required
;
2422 aff_i
= isl_aff_list_get_aff(data
->p
[data
->n
].aff_list
, i
);
2423 valid
= isl_set_from_basic_set(test(isl_aff_copy(aff
), aff_i
));
2424 required
= isl_set_list_get_set(data
->p
[data
->n
].set_list
, i
);
2425 dom
= isl_ast_build_get_domain(data
->build
);
2426 required
= isl_set_intersect(dom
, required
);
2427 is_valid
= isl_set_is_subset(required
, valid
);
2428 isl_set_free(required
);
2429 isl_set_free(valid
);
2430 if (is_valid
< 0 || !is_valid
) {
2435 aff_i
= isl_aff_list_get_aff(data
->p
[data
->n
].aff_list
, i
);
2436 valid
= isl_set_from_basic_set(test(aff_i
, isl_aff_copy(aff
)));
2437 is_valid
= isl_set_is_subset(set
, valid
);
2438 isl_set_free(valid
);
2439 if (is_valid
< 0 || !is_valid
) {
2446 return isl_bool_true
;
2449 /* Can the list of pieces in "data" be extended with "set" and "aff"
2450 * to form/preserve a minimum expression?
2451 * In particular, is it the case for each entry (set_i, aff_i) that
2453 * aff >= aff_i on set_i, and
2454 * aff_i >= aff on set?
2456 static isl_bool
extends_min(struct isl_from_pw_aff_data
*data
,
2457 __isl_keep isl_set
*set
, __isl_keep isl_aff
*aff
)
2459 return extends(data
, set
, aff
, &isl_aff_ge_basic_set
);
2462 /* Can the list of pieces in "data" be extended with "set" and "aff"
2463 * to form/preserve a maximum expression?
2464 * In particular, is it the case for each entry (set_i, aff_i) that
2466 * aff <= aff_i on set_i, and
2467 * aff_i <= aff on set?
2469 static isl_bool
extends_max(struct isl_from_pw_aff_data
*data
,
2470 __isl_keep isl_set
*set
, __isl_keep isl_aff
*aff
)
2472 return extends(data
, set
, aff
, &isl_aff_le_basic_set
);
2475 /* This function is called during the construction of an isl_ast_expr
2476 * that evaluates an isl_pw_aff.
2477 * If the last piece of "data" contains a single subpiece and
2478 * if its affine function is equal to "aff" on a part of the domain
2479 * that includes either "set" or the domain of that single subpiece,
2480 * then extend the domain of that single subpiece with "set".
2481 * If it was the original domain of the single subpiece where
2482 * the two affine functions are equal, then also replace
2483 * the affine function of the single subpiece by "aff".
2484 * If the last piece of "data" contains either a single subpiece
2485 * or a minimum, then check if this minimum expression can be extended
2487 * If so, extend the sequence and return.
2488 * Perform the same operation for maximum expressions.
2489 * If no such extension can be performed, then move to the next piece
2490 * in "data" (if the current piece contains any data), and then store
2491 * the current subpiece in the current piece of "data" for later handling.
2493 static isl_stat
ast_expr_from_pw_aff(__isl_take isl_set
*set
,
2494 __isl_take isl_aff
*aff
, void *user
)
2496 struct isl_from_pw_aff_data
*data
= user
;
2498 enum isl_from_pw_aff_state state
;
2500 state
= data
->p
[data
->n
].state
;
2501 if (state
== isl_state_single
) {
2504 isl_bool subset1
, subset2
= isl_bool_false
;
2505 aff0
= isl_aff_list_get_aff(data
->p
[data
->n
].aff_list
, 0);
2506 eq
= isl_aff_eq_set(isl_aff_copy(aff
), aff0
);
2507 subset1
= isl_set_is_subset(set
, eq
);
2508 if (subset1
>= 0 && !subset1
)
2509 subset2
= single_is_subset(data
, eq
);
2511 if (subset1
< 0 || subset2
< 0)
2514 return extend_domain(data
, set
, aff
, 0);
2516 return extend_domain(data
, set
, aff
, 1);
2518 if (state
== isl_state_single
|| state
== isl_state_min
) {
2519 test
= extends_min(data
, set
, aff
);
2523 return extend_min(data
, set
, aff
);
2525 if (state
== isl_state_single
|| state
== isl_state_max
) {
2526 test
= extends_max(data
, set
, aff
);
2530 return extend_max(data
, set
, aff
);
2532 if (state
!= isl_state_none
)
2534 set_single(data
, set
, aff
);
2540 return isl_stat_error
;
2543 /* Construct an isl_ast_expr that evaluates "pa".
2544 * The result is simplified in terms of build->domain.
2546 * The domain of "pa" lives in the internal schedule space.
2548 __isl_give isl_ast_expr
*isl_ast_build_expr_from_pw_aff_internal(
2549 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_aff
*pa
)
2551 struct isl_from_pw_aff_data data
= { NULL
};
2552 isl_ast_expr
*res
= NULL
;
2554 pa
= isl_ast_build_compute_gist_pw_aff(build
, pa
);
2555 pa
= isl_pw_aff_coalesce(pa
);
2559 if (isl_from_pw_aff_data_init(&data
, build
, pa
) < 0)
2563 if (isl_pw_aff_foreach_piece(pa
, &ast_expr_from_pw_aff
, &data
) >= 0)
2564 res
= build_pieces(&data
);
2566 isl_pw_aff_free(pa
);
2567 isl_from_pw_aff_data_clear(&data
);
2570 isl_pw_aff_free(pa
);
2571 isl_from_pw_aff_data_clear(&data
);
2575 /* Construct an isl_ast_expr that evaluates "pa".
2576 * The result is simplified in terms of build->domain.
2578 * The domain of "pa" lives in the external schedule space.
2580 __isl_give isl_ast_expr
*isl_ast_build_expr_from_pw_aff(
2581 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_aff
*pa
)
2586 needs_map
= isl_ast_build_need_schedule_map(build
);
2587 if (needs_map
< 0) {
2588 pa
= isl_pw_aff_free(pa
);
2589 } else if (needs_map
) {
2591 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
2592 pa
= isl_pw_aff_pullback_multi_aff(pa
, ma
);
2594 expr
= isl_ast_build_expr_from_pw_aff_internal(build
, pa
);
2598 /* Set the ids of the input dimensions of "mpa" to the iterator ids
2601 * The domain of "mpa" is assumed to live in the internal schedule domain.
2603 static __isl_give isl_multi_pw_aff
*set_iterator_names(
2604 __isl_keep isl_ast_build
*build
, __isl_take isl_multi_pw_aff
*mpa
)
2609 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_in
);
2611 return isl_multi_pw_aff_free(mpa
);
2612 for (i
= 0; i
< n
; ++i
) {
2615 id
= isl_ast_build_get_iterator_id(build
, i
);
2616 mpa
= isl_multi_pw_aff_set_dim_id(mpa
, isl_dim_in
, i
, id
);
2622 /* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
2623 * the remaining arguments derived from "mpa".
2624 * That is, construct a call or access expression that calls/accesses "arg0"
2625 * with arguments/indices specified by "mpa".
2627 static __isl_give isl_ast_expr
*isl_ast_build_with_arguments(
2628 __isl_keep isl_ast_build
*build
, enum isl_ast_expr_op_type type
,
2629 __isl_take isl_ast_expr
*arg0
, __isl_take isl_multi_pw_aff
*mpa
)
2636 ctx
= isl_ast_build_get_ctx(build
);
2638 n
= isl_multi_pw_aff_dim(mpa
, isl_dim_out
);
2639 expr
= n
>= 0 ? isl_ast_expr_alloc_op(ctx
, type
, 1 + n
) : NULL
;
2640 expr
= isl_ast_expr_op_add_arg(expr
, arg0
);
2641 for (i
= 0; i
< n
; ++i
) {
2645 pa
= isl_multi_pw_aff_get_pw_aff(mpa
, i
);
2646 arg
= isl_ast_build_expr_from_pw_aff_internal(build
, pa
);
2647 expr
= isl_ast_expr_op_add_arg(expr
, arg
);
2650 isl_multi_pw_aff_free(mpa
);
2654 static __isl_give isl_ast_expr
*isl_ast_build_from_multi_pw_aff_internal(
2655 __isl_keep isl_ast_build
*build
, enum isl_ast_expr_op_type type
,
2656 __isl_take isl_multi_pw_aff
*mpa
);
2658 /* Construct an isl_ast_expr that accesses the member specified by "mpa".
2659 * The range of "mpa" is assumed to be wrapped relation.
2660 * The domain of this wrapped relation specifies the structure being
2661 * accessed, while the range of this wrapped relation spacifies the
2662 * member of the structure being accessed.
2664 * The domain of "mpa" is assumed to live in the internal schedule domain.
2666 static __isl_give isl_ast_expr
*isl_ast_build_from_multi_pw_aff_member(
2667 __isl_keep isl_ast_build
*build
, __isl_take isl_multi_pw_aff
*mpa
)
2670 isl_multi_pw_aff
*domain
;
2671 isl_ast_expr
*domain_expr
, *expr
;
2672 enum isl_ast_expr_op_type type
= isl_ast_expr_op_access
;
2674 domain
= isl_multi_pw_aff_copy(mpa
);
2675 domain
= isl_multi_pw_aff_range_factor_domain(domain
);
2676 domain_expr
= isl_ast_build_from_multi_pw_aff_internal(build
,
2678 mpa
= isl_multi_pw_aff_range_factor_range(mpa
);
2679 if (!isl_multi_pw_aff_has_tuple_id(mpa
, isl_dim_out
))
2680 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
2681 "missing field name", goto error
);
2682 id
= isl_multi_pw_aff_get_tuple_id(mpa
, isl_dim_out
);
2683 expr
= isl_ast_expr_from_id(id
);
2684 expr
= isl_ast_expr_alloc_binary(isl_ast_expr_op_member
,
2686 return isl_ast_build_with_arguments(build
, type
, expr
, mpa
);
2688 isl_multi_pw_aff_free(mpa
);
2692 /* Construct an isl_ast_expr of type "type" that calls or accesses
2693 * the element specified by "mpa".
2694 * The first argument is obtained from the output tuple name.
2695 * The remaining arguments are given by the piecewise affine expressions.
2697 * If the range of "mpa" is a mapped relation, then we assume it
2698 * represents an access to a member of a structure.
2700 * The domain of "mpa" is assumed to live in the internal schedule domain.
2702 static __isl_give isl_ast_expr
*isl_ast_build_from_multi_pw_aff_internal(
2703 __isl_keep isl_ast_build
*build
, enum isl_ast_expr_op_type type
,
2704 __isl_take isl_multi_pw_aff
*mpa
)
2713 if (type
== isl_ast_expr_op_access
&&
2714 isl_multi_pw_aff_range_is_wrapping(mpa
))
2715 return isl_ast_build_from_multi_pw_aff_member(build
, mpa
);
2717 mpa
= set_iterator_names(build
, mpa
);
2721 ctx
= isl_ast_build_get_ctx(build
);
2723 if (isl_multi_pw_aff_has_tuple_id(mpa
, isl_dim_out
))
2724 id
= isl_multi_pw_aff_get_tuple_id(mpa
, isl_dim_out
);
2726 id
= isl_id_alloc(ctx
, "", NULL
);
2728 expr
= isl_ast_expr_from_id(id
);
2729 return isl_ast_build_with_arguments(build
, type
, expr
, mpa
);
2731 isl_multi_pw_aff_free(mpa
);
2735 /* Construct an isl_ast_expr of type "type" that calls or accesses
2736 * the element specified by "pma".
2737 * The first argument is obtained from the output tuple name.
2738 * The remaining arguments are given by the piecewise affine expressions.
2740 * The domain of "pma" is assumed to live in the internal schedule domain.
2742 static __isl_give isl_ast_expr
*isl_ast_build_from_pw_multi_aff_internal(
2743 __isl_keep isl_ast_build
*build
, enum isl_ast_expr_op_type type
,
2744 __isl_take isl_pw_multi_aff
*pma
)
2746 isl_multi_pw_aff
*mpa
;
2748 mpa
= isl_multi_pw_aff_from_pw_multi_aff(pma
);
2749 return isl_ast_build_from_multi_pw_aff_internal(build
, type
, mpa
);
2752 /* Construct an isl_ast_expr of type "type" that calls or accesses
2753 * the element specified by "mpa".
2754 * The first argument is obtained from the output tuple name.
2755 * The remaining arguments are given by the piecewise affine expressions.
2757 * The domain of "mpa" is assumed to live in the external schedule domain.
2759 static __isl_give isl_ast_expr
*isl_ast_build_from_multi_pw_aff(
2760 __isl_keep isl_ast_build
*build
, enum isl_ast_expr_op_type type
,
2761 __isl_take isl_multi_pw_aff
*mpa
)
2766 isl_space
*space_build
, *space_mpa
;
2768 space_build
= isl_ast_build_get_space(build
, 0);
2769 space_mpa
= isl_multi_pw_aff_get_space(mpa
);
2770 is_domain
= isl_space_tuple_is_equal(space_build
, isl_dim_set
,
2771 space_mpa
, isl_dim_in
);
2772 isl_space_free(space_build
);
2773 isl_space_free(space_mpa
);
2777 isl_die(isl_ast_build_get_ctx(build
), isl_error_invalid
,
2778 "spaces don't match", goto error
);
2780 needs_map
= isl_ast_build_need_schedule_map(build
);
2785 ma
= isl_ast_build_get_schedule_map_multi_aff(build
);
2786 mpa
= isl_multi_pw_aff_pullback_multi_aff(mpa
, ma
);
2789 expr
= isl_ast_build_from_multi_pw_aff_internal(build
, type
, mpa
);
2792 isl_multi_pw_aff_free(mpa
);
2796 /* Construct an isl_ast_expr that calls the domain element specified by "mpa".
2797 * The name of the function is obtained from the output tuple name.
2798 * The arguments are given by the piecewise affine expressions.
2800 * The domain of "mpa" is assumed to live in the external schedule domain.
2802 __isl_give isl_ast_expr
*isl_ast_build_call_from_multi_pw_aff(
2803 __isl_keep isl_ast_build
*build
, __isl_take isl_multi_pw_aff
*mpa
)
2805 return isl_ast_build_from_multi_pw_aff(build
,
2806 isl_ast_expr_op_call
, mpa
);
2809 /* Construct an isl_ast_expr that accesses the array element specified by "mpa".
2810 * The name of the array is obtained from the output tuple name.
2811 * The index expressions are given by the piecewise affine expressions.
2813 * The domain of "mpa" is assumed to live in the external schedule domain.
2815 __isl_give isl_ast_expr
*isl_ast_build_access_from_multi_pw_aff(
2816 __isl_keep isl_ast_build
*build
, __isl_take isl_multi_pw_aff
*mpa
)
2818 return isl_ast_build_from_multi_pw_aff(build
,
2819 isl_ast_expr_op_access
, mpa
);
2822 /* Construct an isl_ast_expr of type "type" that calls or accesses
2823 * the element specified by "pma".
2824 * The first argument is obtained from the output tuple name.
2825 * The remaining arguments are given by the piecewise affine expressions.
2827 * The domain of "pma" is assumed to live in the external schedule domain.
2829 static __isl_give isl_ast_expr
*isl_ast_build_from_pw_multi_aff(
2830 __isl_keep isl_ast_build
*build
, enum isl_ast_expr_op_type type
,
2831 __isl_take isl_pw_multi_aff
*pma
)
2833 isl_multi_pw_aff
*mpa
;
2835 mpa
= isl_multi_pw_aff_from_pw_multi_aff(pma
);
2836 return isl_ast_build_from_multi_pw_aff(build
, type
, mpa
);
2839 /* Construct an isl_ast_expr that calls the domain element specified by "pma".
2840 * The name of the function is obtained from the output tuple name.
2841 * The arguments are given by the piecewise affine expressions.
2843 * The domain of "pma" is assumed to live in the external schedule domain.
2845 __isl_give isl_ast_expr
*isl_ast_build_call_from_pw_multi_aff(
2846 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_multi_aff
*pma
)
2848 return isl_ast_build_from_pw_multi_aff(build
,
2849 isl_ast_expr_op_call
, pma
);
2852 /* Construct an isl_ast_expr that accesses the array element specified by "pma".
2853 * The name of the array is obtained from the output tuple name.
2854 * The index expressions are given by the piecewise affine expressions.
2856 * The domain of "pma" is assumed to live in the external schedule domain.
2858 __isl_give isl_ast_expr
*isl_ast_build_access_from_pw_multi_aff(
2859 __isl_keep isl_ast_build
*build
, __isl_take isl_pw_multi_aff
*pma
)
2861 return isl_ast_build_from_pw_multi_aff(build
,
2862 isl_ast_expr_op_access
, pma
);
2865 /* Construct an isl_ast_expr that calls the domain element
2866 * specified by "executed".
2868 * "executed" is assumed to be single-valued, with a domain that lives
2869 * in the internal schedule space.
2871 __isl_give isl_ast_node
*isl_ast_build_call_from_executed(
2872 __isl_keep isl_ast_build
*build
, __isl_take isl_map
*executed
)
2874 isl_pw_multi_aff
*iteration
;
2877 iteration
= isl_pw_multi_aff_from_map(executed
);
2878 iteration
= isl_ast_build_compute_gist_pw_multi_aff(build
, iteration
);
2879 iteration
= isl_pw_multi_aff_intersect_domain(iteration
,
2880 isl_ast_build_get_domain(build
));
2881 expr
= isl_ast_build_from_pw_multi_aff_internal(build
,
2882 isl_ast_expr_op_call
, iteration
);
2883 return isl_ast_node_alloc_user(expr
);