isl_local_space_divs_known: extract out isl_local_divs_known
[isl.git] / isl_ast_build_expr.c
blob76cd57f021acd941485f8196be02e9b975a32110
1 /*
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
13 #include <isl/space.h>
14 #include <isl/constraint.h>
15 #include <isl/ilp.h>
16 #include <isl/val.h>
17 #include <isl_ast_build_expr.h>
18 #include <isl_ast_private.h>
19 #include <isl_ast_build_private.h>
20 #include <isl_sort.h>
22 /* Compute the "opposite" of the (numerator of the) argument of a div
23 * with denominator "d".
25 * In particular, compute
27 * -aff + (d - 1)
29 static __isl_give isl_aff *oppose_div_arg(__isl_take isl_aff *aff,
30 __isl_take isl_val *d)
32 aff = isl_aff_neg(aff);
33 aff = isl_aff_add_constant_val(aff, d);
34 aff = isl_aff_add_constant_si(aff, -1);
36 return aff;
39 /* Internal data structure used inside isl_ast_expr_add_term.
40 * The domain of "build" is used to simplify the expressions.
41 * "build" needs to be set by the caller of isl_ast_expr_add_term.
42 * "cst" is the constant term of the expression in which the added term
43 * appears. It may be modified by isl_ast_expr_add_term.
45 * "v" is the coefficient of the term that is being constructed and
46 * is set internally by isl_ast_expr_add_term.
48 struct isl_ast_add_term_data {
49 isl_ast_build *build;
50 isl_val *cst;
51 isl_val *v;
54 /* Given the numerator "aff" of the argument of an integer division
55 * with denominator "d", check if it can be made non-negative over
56 * data->build->domain by stealing part of the constant term of
57 * the expression in which the integer division appears.
59 * In particular, the outer expression is of the form
61 * v * floor(aff/d) + cst
63 * We already know that "aff" itself may attain negative values.
64 * Here we check if aff + d*floor(cst/v) is non-negative, such
65 * that we could rewrite the expression to
67 * v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
69 * Note that aff + d*floor(cst/v) can only possibly be non-negative
70 * if data->cst and data->v have the same sign.
71 * Similarly, if floor(cst/v) is zero, then there is no point in
72 * checking again.
74 static int is_non_neg_after_stealing(__isl_keep isl_aff *aff,
75 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
77 isl_aff *shifted;
78 isl_val *shift;
79 int is_zero;
80 int non_neg;
82 if (isl_val_sgn(data->cst) != isl_val_sgn(data->v))
83 return 0;
85 shift = isl_val_div(isl_val_copy(data->cst), isl_val_copy(data->v));
86 shift = isl_val_floor(shift);
87 is_zero = isl_val_is_zero(shift);
88 if (is_zero < 0 || is_zero) {
89 isl_val_free(shift);
90 return is_zero < 0 ? -1 : 0;
92 shift = isl_val_mul(shift, isl_val_copy(d));
93 shifted = isl_aff_copy(aff);
94 shifted = isl_aff_add_constant_val(shifted, shift);
95 non_neg = isl_ast_build_aff_is_nonneg(data->build, shifted);
96 isl_aff_free(shifted);
98 return non_neg;
101 /* Given the numerator "aff' of the argument of an integer division
102 * with denominator "d", steal part of the constant term of
103 * the expression in which the integer division appears to make it
104 * non-negative over data->build->domain.
106 * In particular, the outer expression is of the form
108 * v * floor(aff/d) + cst
110 * We know that "aff" itself may attain negative values,
111 * but that aff + d*floor(cst/v) is non-negative.
112 * Find the minimal positive value that we need to add to "aff"
113 * to make it positive and adjust data->cst accordingly.
114 * That is, compute the minimal value "m" of "aff" over
115 * data->build->domain and take
117 * s = ceil(m/d)
119 * such that
121 * aff + d * s >= 0
123 * and rewrite the expression to
125 * v * floor((aff + s*d)/d) + (cst - v*s)
127 static __isl_give isl_aff *steal_from_cst(__isl_take isl_aff *aff,
128 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
130 isl_set *domain;
131 isl_val *shift, *t;
133 domain = isl_ast_build_get_domain(data->build);
134 shift = isl_set_min_val(domain, aff);
135 isl_set_free(domain);
137 shift = isl_val_neg(shift);
138 shift = isl_val_div(shift, isl_val_copy(d));
139 shift = isl_val_ceil(shift);
141 t = isl_val_copy(shift);
142 t = isl_val_mul(t, isl_val_copy(data->v));
143 data->cst = isl_val_sub(data->cst, t);
145 shift = isl_val_mul(shift, isl_val_copy(d));
146 return isl_aff_add_constant_val(aff, shift);
149 /* Create an isl_ast_expr evaluating the div at position "pos" in "ls".
150 * The result is simplified in terms of data->build->domain.
151 * This function may change (the sign of) data->v.
153 * "ls" is known to be non-NULL.
155 * Let the div be of the form floor(e/d).
156 * If the ast_build_prefer_pdiv option is set then we check if "e"
157 * is non-negative, so that we can generate
159 * (pdiv_q, expr(e), expr(d))
161 * instead of
163 * (fdiv_q, expr(e), expr(d))
165 * If the ast_build_prefer_pdiv option is set and
166 * if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
167 * If so, we can rewrite
169 * floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
171 * and still use pdiv_q, while changing the sign of data->v.
173 * Otherwise, we check if
175 * e + d*floor(cst/v)
177 * is non-negative and if so, replace floor(e/d) by
179 * floor((e + s*d)/d) - s
181 * with s the minimal shift that makes the argument non-negative.
183 static __isl_give isl_ast_expr *var_div(struct isl_ast_add_term_data *data,
184 __isl_keep isl_local_space *ls, int pos)
186 isl_ctx *ctx = isl_local_space_get_ctx(ls);
187 isl_aff *aff;
188 isl_ast_expr *num, *den;
189 isl_val *d;
190 enum isl_ast_op_type type;
192 aff = isl_local_space_get_div(ls, pos);
193 d = isl_aff_get_denominator_val(aff);
194 aff = isl_aff_scale_val(aff, isl_val_copy(d));
195 den = isl_ast_expr_from_val(isl_val_copy(d));
197 type = isl_ast_op_fdiv_q;
198 if (isl_options_get_ast_build_prefer_pdiv(ctx)) {
199 int non_neg = isl_ast_build_aff_is_nonneg(data->build, aff);
200 if (non_neg >= 0 && !non_neg) {
201 isl_aff *opp = oppose_div_arg(isl_aff_copy(aff),
202 isl_val_copy(d));
203 non_neg = isl_ast_build_aff_is_nonneg(data->build, opp);
204 if (non_neg >= 0 && non_neg) {
205 data->v = isl_val_neg(data->v);
206 isl_aff_free(aff);
207 aff = opp;
208 } else
209 isl_aff_free(opp);
211 if (non_neg >= 0 && !non_neg) {
212 non_neg = is_non_neg_after_stealing(aff, d, data);
213 if (non_neg >= 0 && non_neg)
214 aff = steal_from_cst(aff, d, data);
216 if (non_neg < 0)
217 aff = isl_aff_free(aff);
218 else if (non_neg)
219 type = isl_ast_op_pdiv_q;
222 isl_val_free(d);
223 num = isl_ast_expr_from_aff(aff, data->build);
224 return isl_ast_expr_alloc_binary(type, num, den);
227 /* Create an isl_ast_expr evaluating the specified dimension of "ls".
228 * The result is simplified in terms of data->build->domain.
229 * This function may change (the sign of) data->v.
231 * The isl_ast_expr is constructed based on the type of the dimension.
232 * - divs are constructed by var_div
233 * - set variables are constructed from the iterator isl_ids in data->build
234 * - parameters are constructed from the isl_ids in "ls"
236 static __isl_give isl_ast_expr *var(struct isl_ast_add_term_data *data,
237 __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos)
239 isl_ctx *ctx = isl_local_space_get_ctx(ls);
240 isl_id *id;
242 if (type == isl_dim_div)
243 return var_div(data, ls, pos);
245 if (type == isl_dim_set) {
246 id = isl_ast_build_get_iterator_id(data->build, pos);
247 return isl_ast_expr_from_id(id);
250 if (!isl_local_space_has_dim_id(ls, type, pos))
251 isl_die(ctx, isl_error_internal, "unnamed dimension",
252 return NULL);
253 id = isl_local_space_get_dim_id(ls, type, pos);
254 return isl_ast_expr_from_id(id);
257 /* Does "expr" represent the zero integer?
259 static int ast_expr_is_zero(__isl_keep isl_ast_expr *expr)
261 if (!expr)
262 return -1;
263 if (expr->type != isl_ast_expr_int)
264 return 0;
265 return isl_val_is_zero(expr->u.v);
268 /* Create an expression representing the sum of "expr1" and "expr2",
269 * provided neither of the two expressions is identically zero.
271 static __isl_give isl_ast_expr *ast_expr_add(__isl_take isl_ast_expr *expr1,
272 __isl_take isl_ast_expr *expr2)
274 if (!expr1 || !expr2)
275 goto error;
277 if (ast_expr_is_zero(expr1)) {
278 isl_ast_expr_free(expr1);
279 return expr2;
282 if (ast_expr_is_zero(expr2)) {
283 isl_ast_expr_free(expr2);
284 return expr1;
287 return isl_ast_expr_add(expr1, expr2);
288 error:
289 isl_ast_expr_free(expr1);
290 isl_ast_expr_free(expr2);
291 return NULL;
294 /* Subtract expr2 from expr1.
296 * If expr2 is zero, we simply return expr1.
297 * If expr1 is zero, we return
299 * (isl_ast_op_minus, expr2)
301 * Otherwise, we return
303 * (isl_ast_op_sub, expr1, expr2)
305 static __isl_give isl_ast_expr *ast_expr_sub(__isl_take isl_ast_expr *expr1,
306 __isl_take isl_ast_expr *expr2)
308 if (!expr1 || !expr2)
309 goto error;
311 if (ast_expr_is_zero(expr2)) {
312 isl_ast_expr_free(expr2);
313 return expr1;
316 if (ast_expr_is_zero(expr1)) {
317 isl_ast_expr_free(expr1);
318 return isl_ast_expr_neg(expr2);
321 return isl_ast_expr_sub(expr1, expr2);
322 error:
323 isl_ast_expr_free(expr1);
324 isl_ast_expr_free(expr2);
325 return NULL;
328 /* Return an isl_ast_expr that represents
330 * v * (aff mod d)
332 * v is assumed to be non-negative.
333 * The result is simplified in terms of build->domain.
335 static __isl_give isl_ast_expr *isl_ast_expr_mod(__isl_keep isl_val *v,
336 __isl_keep isl_aff *aff, __isl_keep isl_val *d,
337 __isl_keep isl_ast_build *build)
339 isl_ast_expr *expr;
340 isl_ast_expr *c;
342 if (!aff)
343 return NULL;
345 expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
347 c = isl_ast_expr_from_val(isl_val_copy(d));
348 expr = isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr, c);
350 if (!isl_val_is_one(v)) {
351 c = isl_ast_expr_from_val(isl_val_copy(v));
352 expr = isl_ast_expr_mul(c, expr);
355 return expr;
358 /* Create an isl_ast_expr that scales "expr" by "v".
360 * If v is 1, we simply return expr.
361 * If v is -1, we return
363 * (isl_ast_op_minus, expr)
365 * Otherwise, we return
367 * (isl_ast_op_mul, expr(v), expr)
369 static __isl_give isl_ast_expr *scale(__isl_take isl_ast_expr *expr,
370 __isl_take isl_val *v)
372 isl_ast_expr *c;
374 if (!expr || !v)
375 goto error;
376 if (isl_val_is_one(v)) {
377 isl_val_free(v);
378 return expr;
381 if (isl_val_is_negone(v)) {
382 isl_val_free(v);
383 expr = isl_ast_expr_neg(expr);
384 } else {
385 c = isl_ast_expr_from_val(v);
386 expr = isl_ast_expr_mul(c, expr);
389 return expr;
390 error:
391 isl_val_free(v);
392 isl_ast_expr_free(expr);
393 return NULL;
396 /* Add an expression for "*v" times the specified dimension of "ls"
397 * to expr.
398 * If the dimension is an integer division, then this function
399 * may modify data->cst in order to make the numerator non-negative.
400 * The result is simplified in terms of data->build->domain.
402 * Let e be the expression for the specified dimension,
403 * multiplied by the absolute value of "*v".
404 * If "*v" is negative, we create
406 * (isl_ast_op_sub, expr, e)
408 * except when expr is trivially zero, in which case we create
410 * (isl_ast_op_minus, e)
412 * instead.
414 * If "*v" is positive, we simply create
416 * (isl_ast_op_add, expr, e)
419 static __isl_give isl_ast_expr *isl_ast_expr_add_term(
420 __isl_take isl_ast_expr *expr,
421 __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos,
422 __isl_take isl_val *v, struct isl_ast_add_term_data *data)
424 isl_ast_expr *term;
426 if (!expr)
427 return NULL;
429 data->v = v;
430 term = var(data, ls, type, pos);
431 v = data->v;
433 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
434 v = isl_val_neg(v);
435 term = scale(term, v);
436 return ast_expr_sub(expr, term);
437 } else {
438 term = scale(term, v);
439 return ast_expr_add(expr, term);
443 /* Add an expression for "v" to expr.
445 static __isl_give isl_ast_expr *isl_ast_expr_add_int(
446 __isl_take isl_ast_expr *expr, __isl_take isl_val *v)
448 isl_ast_expr *expr_int;
450 if (!expr || !v)
451 goto error;
453 if (isl_val_is_zero(v)) {
454 isl_val_free(v);
455 return expr;
458 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
459 v = isl_val_neg(v);
460 expr_int = isl_ast_expr_from_val(v);
461 return ast_expr_sub(expr, expr_int);
462 } else {
463 expr_int = isl_ast_expr_from_val(v);
464 return ast_expr_add(expr, expr_int);
466 error:
467 isl_ast_expr_free(expr);
468 isl_val_free(v);
469 return NULL;
472 /* Internal data structure used inside extract_modulos.
474 * If any modulo expressions are detected in "aff", then the
475 * expression is removed from "aff" and added to either "pos" or "neg"
476 * depending on the sign of the coefficient of the modulo expression
477 * inside "aff".
479 * "add" is an expression that needs to be added to "aff" at the end of
480 * the computation. It is NULL as long as no modulos have been extracted.
482 * "i" is the position in "aff" of the div under investigation
483 * "v" is the coefficient in "aff" of the div
484 * "div" is the argument of the div, with the denominator removed
485 * "d" is the original denominator of the argument of the div
487 * "nonneg" is an affine expression that is non-negative over "build"
488 * and that can be used to extract a modulo expression from "div".
489 * In particular, if "sign" is 1, then the coefficients of "nonneg"
490 * are equal to those of "div" modulo "d". If "sign" is -1, then
491 * the coefficients of "nonneg" are opposite to those of "div" modulo "d".
492 * If "sign" is 0, then no such affine expression has been found (yet).
494 struct isl_extract_mod_data {
495 isl_ast_build *build;
496 isl_aff *aff;
498 isl_ast_expr *pos;
499 isl_ast_expr *neg;
501 isl_aff *add;
503 int i;
504 isl_val *v;
505 isl_val *d;
506 isl_aff *div;
508 isl_aff *nonneg;
509 int sign;
512 /* Given that data->v * div_i in data->aff is equal to
514 * f * (term - (arg mod d))
516 * with data->d * f = data->v, add
518 * f * term
520 * to data->add and
522 * abs(f) * (arg mod d)
524 * to data->neg or data->pos depending on the sign of -f.
526 static int extract_term_and_mod(struct isl_extract_mod_data *data,
527 __isl_take isl_aff *term, __isl_take isl_aff *arg)
529 isl_ast_expr *expr;
530 int s;
532 data->v = isl_val_div(data->v, isl_val_copy(data->d));
533 s = isl_val_sgn(data->v);
534 data->v = isl_val_abs(data->v);
535 expr = isl_ast_expr_mod(data->v, arg, data->d, data->build);
536 isl_aff_free(arg);
537 if (s > 0)
538 data->neg = ast_expr_add(data->neg, expr);
539 else
540 data->pos = ast_expr_add(data->pos, expr);
541 data->aff = isl_aff_set_coefficient_si(data->aff,
542 isl_dim_div, data->i, 0);
543 if (s < 0)
544 data->v = isl_val_neg(data->v);
545 term = isl_aff_scale_val(term, isl_val_copy(data->v));
547 if (!data->add)
548 data->add = term;
549 else
550 data->add = isl_aff_add(data->add, term);
551 if (!data->add)
552 return -1;
554 return 0;
557 /* Given that data->v * div_i in data->aff is of the form
559 * f * d * floor(div/d)
561 * with div nonnegative on data->build, rewrite it as
563 * f * (div - (div mod d)) = f * div - f * (div mod d)
565 * and add
567 * f * div
569 * to data->add and
571 * abs(f) * (div mod d)
573 * to data->neg or data->pos depending on the sign of -f.
575 static int extract_mod(struct isl_extract_mod_data *data)
577 return extract_term_and_mod(data, isl_aff_copy(data->div),
578 isl_aff_copy(data->div));
581 /* Given that data->v * div_i in data->aff is of the form
583 * f * d * floor(div/d) (1)
585 * check if div is non-negative on data->build and, if so,
586 * extract the corresponding modulo from data->aff.
587 * If not, then check if
589 * -div + d - 1
591 * is non-negative on data->build. If so, replace (1) by
593 * -f * d * floor((-div + d - 1)/d)
595 * and extract the corresponding modulo from data->aff.
597 * This function may modify data->div.
599 static int extract_nonneg_mod(struct isl_extract_mod_data *data)
601 int mod;
603 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
604 if (mod < 0)
605 goto error;
606 if (mod)
607 return extract_mod(data);
609 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
610 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
611 if (mod < 0)
612 goto error;
613 if (mod) {
614 data->v = isl_val_neg(data->v);
615 return extract_mod(data);
618 return 0;
619 error:
620 data->aff = isl_aff_free(data->aff);
621 return -1;
624 /* Is the affine expression of constraint "c" "simpler" than data->nonneg
625 * for use in extracting a modulo expression?
627 * We currently only consider the constant term of the affine expression.
628 * In particular, we prefer the affine expression with the smallest constant
629 * term.
630 * This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
631 * then we would pick x >= 0
633 * More detailed heuristics could be used if it turns out that there is a need.
635 static int mod_constraint_is_simpler(struct isl_extract_mod_data *data,
636 __isl_keep isl_constraint *c)
638 isl_val *v1, *v2;
639 int simpler;
641 if (!data->nonneg)
642 return 1;
644 v1 = isl_val_abs(isl_constraint_get_constant_val(c));
645 v2 = isl_val_abs(isl_aff_get_constant_val(data->nonneg));
646 simpler = isl_val_lt(v1, v2);
647 isl_val_free(v1);
648 isl_val_free(v2);
650 return simpler;
653 /* Check if the coefficients of "c" are either equal or opposite to those
654 * of data->div modulo data->d. If so, and if "c" is "simpler" than
655 * data->nonneg, then replace data->nonneg by the affine expression of "c"
656 * and set data->sign accordingly.
658 * Both "c" and data->div are assumed not to involve any integer divisions.
660 * Before we start the actual comparison, we first quickly check if
661 * "c" and data->div have the same non-zero coefficients.
662 * If not, then we assume that "c" is not of the desired form.
663 * Note that while the coefficients of data->div can be reasonably expected
664 * not to involve any coefficients that are multiples of d, "c" may
665 * very well involve such coefficients. This means that we may actually
666 * miss some cases.
668 * If the constant term is "too large", then the constraint is rejected,
669 * where "too large" is fairly arbitrarily set to 1 << 15.
670 * We do this to avoid picking up constraints that bound a variable
671 * by a very large number, say the largest or smallest possible
672 * variable in the representation of some integer type.
674 static isl_stat check_parallel_or_opposite(__isl_take isl_constraint *c,
675 void *user)
677 struct isl_extract_mod_data *data = user;
678 enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
679 enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
680 int i, t;
681 int n[2];
682 int parallel = 1, opposite = 1;
684 for (t = 0; t < 2; ++t) {
685 n[t] = isl_constraint_dim(c, c_type[t]);
686 for (i = 0; i < n[t]; ++i) {
687 int a, b;
689 a = isl_constraint_involves_dims(c, c_type[t], i, 1);
690 b = isl_aff_involves_dims(data->div, a_type[t], i, 1);
691 if (a != b)
692 parallel = opposite = 0;
696 if (parallel || opposite) {
697 isl_val *v;
699 v = isl_val_abs(isl_constraint_get_constant_val(c));
700 if (isl_val_cmp_si(v, 1 << 15) > 0)
701 parallel = opposite = 0;
702 isl_val_free(v);
705 for (t = 0; t < 2; ++t) {
706 for (i = 0; i < n[t]; ++i) {
707 isl_val *v1, *v2;
709 if (!parallel && !opposite)
710 break;
711 v1 = isl_constraint_get_coefficient_val(c,
712 c_type[t], i);
713 v2 = isl_aff_get_coefficient_val(data->div,
714 a_type[t], i);
715 if (parallel) {
716 v1 = isl_val_sub(v1, isl_val_copy(v2));
717 parallel = isl_val_is_divisible_by(v1, data->d);
718 v1 = isl_val_add(v1, isl_val_copy(v2));
720 if (opposite) {
721 v1 = isl_val_add(v1, isl_val_copy(v2));
722 opposite = isl_val_is_divisible_by(v1, data->d);
724 isl_val_free(v1);
725 isl_val_free(v2);
729 if ((parallel || opposite) && mod_constraint_is_simpler(data, c)) {
730 isl_aff_free(data->nonneg);
731 data->nonneg = isl_constraint_get_aff(c);
732 data->sign = parallel ? 1 : -1;
735 isl_constraint_free(c);
737 if (data->sign != 0 && data->nonneg == NULL)
738 return isl_stat_error;
740 return isl_stat_ok;
743 /* Given that data->v * div_i in data->aff is of the form
745 * f * d * floor(div/d) (1)
747 * see if we can find an expression div' that is non-negative over data->build
748 * and that is related to div through
750 * div' = div + d * e
752 * or
754 * div' = -div + d - 1 + d * e
756 * with e some affine expression.
757 * If so, we write (1) as
759 * f * div + f * (div' mod d)
761 * or
763 * -f * (-div + d - 1) - f * (div' mod d)
765 * exploiting (in the second case) the fact that
767 * f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
770 * We first try to find an appropriate expression for div'
771 * from the constraints of data->build->domain (which is therefore
772 * guaranteed to be non-negative on data->build), where we remove
773 * any integer divisions from the constraints and skip this step
774 * if "div" itself involves any integer divisions.
775 * If we cannot find an appropriate expression this way, then
776 * we pass control to extract_nonneg_mod where check
777 * if div or "-div + d -1" themselves happen to be
778 * non-negative on data->build.
780 * While looking for an appropriate constraint in data->build->domain,
781 * we ignore the constant term, so after finding such a constraint,
782 * we still need to fix up the constant term.
783 * In particular, if a is the constant term of "div"
784 * (or d - 1 - the constant term of "div" if data->sign < 0)
785 * and b is the constant term of the constraint, then we need to find
786 * a non-negative constant c such that
788 * b + c \equiv a mod d
790 * We therefore take
792 * c = (a - b) mod d
794 * and add it to b to obtain the constant term of div'.
795 * If this constant term is "too negative", then we add an appropriate
796 * multiple of d to make it positive.
799 * Note that the above is a only a very simple heuristic for finding an
800 * appropriate expression. We could try a bit harder by also considering
801 * sums of constraints that involve disjoint sets of variables or
802 * we could consider arbitrary linear combinations of constraints,
803 * although that could potentially be much more expensive as it involves
804 * the solution of an LP problem.
806 * In particular, if v_i is a column vector representing constraint i,
807 * w represents div and e_i is the i-th unit vector, then we are looking
808 * for a solution of the constraints
810 * \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
812 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
813 * If we are not just interested in a non-negative expression, but
814 * also in one with a minimal range, then we don't just want
815 * c = \sum_i lambda_i v_i to be non-negative over the domain,
816 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
817 * that we want to minimize and we now also have to take into account
818 * the constant terms of the constraints.
819 * Alternatively, we could first compute the dual of the domain
820 * and plug in the constraints on the coefficients.
822 static int try_extract_mod(struct isl_extract_mod_data *data)
824 isl_basic_set *hull;
825 isl_val *v1, *v2;
826 int r, n;
828 if (!data->build)
829 goto error;
831 n = isl_aff_dim(data->div, isl_dim_div);
833 if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
834 return extract_nonneg_mod(data);
836 hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
837 hull = isl_basic_set_remove_divs(hull);
838 data->sign = 0;
839 data->nonneg = NULL;
840 r = isl_basic_set_foreach_constraint(hull, &check_parallel_or_opposite,
841 data);
842 isl_basic_set_free(hull);
844 if (!data->sign || r < 0) {
845 isl_aff_free(data->nonneg);
846 if (r < 0)
847 goto error;
848 return extract_nonneg_mod(data);
851 v1 = isl_aff_get_constant_val(data->div);
852 v2 = isl_aff_get_constant_val(data->nonneg);
853 if (data->sign < 0) {
854 v1 = isl_val_neg(v1);
855 v1 = isl_val_add(v1, isl_val_copy(data->d));
856 v1 = isl_val_sub_ui(v1, 1);
858 v1 = isl_val_sub(v1, isl_val_copy(v2));
859 v1 = isl_val_mod(v1, isl_val_copy(data->d));
860 v1 = isl_val_add(v1, v2);
861 v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
862 v2 = isl_val_ceil(v2);
863 if (isl_val_is_neg(v2)) {
864 v2 = isl_val_mul(v2, isl_val_copy(data->d));
865 v1 = isl_val_sub(v1, isl_val_copy(v2));
867 data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
868 isl_val_free(v2);
870 if (data->sign < 0) {
871 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
872 data->v = isl_val_neg(data->v);
875 return extract_term_and_mod(data,
876 isl_aff_copy(data->div), data->nonneg);
877 error:
878 data->aff = isl_aff_free(data->aff);
879 return -1;
882 /* Check if "data->aff" involves any (implicit) modulo computations based
883 * on div "data->i".
884 * If so, remove them from aff and add expressions corresponding
885 * to those modulo computations to data->pos and/or data->neg.
887 * "aff" is assumed to be an integer affine expression.
889 * In particular, check if (v * div_j) is of the form
891 * f * m * floor(a / m)
893 * and, if so, rewrite it as
895 * f * (a - (a mod m)) = f * a - f * (a mod m)
897 * and extract out -f * (a mod m).
898 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
899 * If f < 0, we add ((-f) * (a mod m)) to *pos.
901 * Note that in order to represent "a mod m" as
903 * (isl_ast_op_pdiv_r, a, m)
905 * we need to make sure that a is non-negative.
906 * If not, we check if "-a + m - 1" is non-negative.
907 * If so, we can rewrite
909 * floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
911 * and still extract a modulo.
913 static int extract_modulo(struct isl_extract_mod_data *data)
915 data->div = isl_aff_get_div(data->aff, data->i);
916 data->d = isl_aff_get_denominator_val(data->div);
917 if (isl_val_is_divisible_by(data->v, data->d)) {
918 data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
919 if (try_extract_mod(data) < 0)
920 data->aff = isl_aff_free(data->aff);
922 isl_aff_free(data->div);
923 isl_val_free(data->d);
924 return 0;
927 /* Check if "aff" involves any (implicit) modulo computations.
928 * If so, remove them from aff and add expressions corresponding
929 * to those modulo computations to *pos and/or *neg.
930 * We only do this if the option ast_build_prefer_pdiv is set.
932 * "aff" is assumed to be an integer affine expression.
934 * A modulo expression is of the form
936 * a mod m = a - m * floor(a / m)
938 * To detect them in aff, we look for terms of the form
940 * f * m * floor(a / m)
942 * rewrite them as
944 * f * (a - (a mod m)) = f * a - f * (a mod m)
946 * and extract out -f * (a mod m).
947 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
948 * If f < 0, we add ((-f) * (a mod m)) to *pos.
950 static __isl_give isl_aff *extract_modulos(__isl_take isl_aff *aff,
951 __isl_keep isl_ast_expr **pos, __isl_keep isl_ast_expr **neg,
952 __isl_keep isl_ast_build *build)
954 struct isl_extract_mod_data data = { build, aff, *pos, *neg };
955 isl_ctx *ctx;
956 int n;
958 if (!aff)
959 return NULL;
961 ctx = isl_aff_get_ctx(aff);
962 if (!isl_options_get_ast_build_prefer_pdiv(ctx))
963 return aff;
965 n = isl_aff_dim(data.aff, isl_dim_div);
966 for (data.i = 0; data.i < n; ++data.i) {
967 data.v = isl_aff_get_coefficient_val(data.aff,
968 isl_dim_div, data.i);
969 if (!data.v)
970 return isl_aff_free(aff);
971 if (isl_val_is_zero(data.v) ||
972 isl_val_is_one(data.v) || isl_val_is_negone(data.v)) {
973 isl_val_free(data.v);
974 continue;
976 if (extract_modulo(&data) < 0)
977 data.aff = isl_aff_free(data.aff);
978 isl_val_free(data.v);
979 if (!data.aff)
980 break;
983 if (data.add)
984 data.aff = isl_aff_add(data.aff, data.add);
986 *pos = data.pos;
987 *neg = data.neg;
988 return data.aff;
991 /* Check if aff involves any non-integer coefficients.
992 * If so, split aff into
994 * aff = aff1 + (aff2 / d)
996 * with both aff1 and aff2 having only integer coefficients.
997 * Return aff1 and add (aff2 / d) to *expr.
999 static __isl_give isl_aff *extract_rational(__isl_take isl_aff *aff,
1000 __isl_keep isl_ast_expr **expr, __isl_keep isl_ast_build *build)
1002 int i, j, n;
1003 isl_aff *rat = NULL;
1004 isl_local_space *ls = NULL;
1005 isl_ast_expr *rat_expr;
1006 isl_val *v, *d;
1007 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1008 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1010 if (!aff)
1011 return NULL;
1012 d = isl_aff_get_denominator_val(aff);
1013 if (!d)
1014 goto error;
1015 if (isl_val_is_one(d)) {
1016 isl_val_free(d);
1017 return aff;
1020 aff = isl_aff_scale_val(aff, isl_val_copy(d));
1022 ls = isl_aff_get_domain_local_space(aff);
1023 rat = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1025 for (i = 0; i < 3; ++i) {
1026 n = isl_aff_dim(aff, t[i]);
1027 for (j = 0; j < n; ++j) {
1028 isl_aff *rat_j;
1030 v = isl_aff_get_coefficient_val(aff, t[i], j);
1031 if (!v)
1032 goto error;
1033 if (isl_val_is_divisible_by(v, d)) {
1034 isl_val_free(v);
1035 continue;
1037 rat_j = isl_aff_var_on_domain(isl_local_space_copy(ls),
1038 l[i], j);
1039 rat_j = isl_aff_scale_val(rat_j, v);
1040 rat = isl_aff_add(rat, rat_j);
1044 v = isl_aff_get_constant_val(aff);
1045 if (isl_val_is_divisible_by(v, d)) {
1046 isl_val_free(v);
1047 } else {
1048 isl_aff *rat_0;
1050 rat_0 = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
1051 rat = isl_aff_add(rat, rat_0);
1054 isl_local_space_free(ls);
1056 aff = isl_aff_sub(aff, isl_aff_copy(rat));
1057 aff = isl_aff_scale_down_val(aff, isl_val_copy(d));
1059 rat_expr = isl_ast_expr_from_aff(rat, build);
1060 rat_expr = isl_ast_expr_div(rat_expr, isl_ast_expr_from_val(d));
1061 *expr = ast_expr_add(*expr, rat_expr);
1063 return aff;
1064 error:
1065 isl_aff_free(rat);
1066 isl_local_space_free(ls);
1067 isl_aff_free(aff);
1068 isl_val_free(d);
1069 return NULL;
1072 /* Construct an isl_ast_expr that evaluates the affine expression "aff",
1073 * The result is simplified in terms of build->domain.
1075 * We first extract hidden modulo computations from the affine expression
1076 * and then add terms for each variable with a non-zero coefficient.
1077 * Finally, if the affine expression has a non-trivial denominator,
1078 * we divide the resulting isl_ast_expr by this denominator.
1080 __isl_give isl_ast_expr *isl_ast_expr_from_aff(__isl_take isl_aff *aff,
1081 __isl_keep isl_ast_build *build)
1083 int i, j;
1084 int n;
1085 isl_val *v;
1086 isl_ctx *ctx = isl_aff_get_ctx(aff);
1087 isl_ast_expr *expr, *expr_neg;
1088 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1089 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1090 isl_local_space *ls;
1091 struct isl_ast_add_term_data data;
1093 if (!aff)
1094 return NULL;
1096 expr = isl_ast_expr_alloc_int_si(ctx, 0);
1097 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1099 aff = extract_rational(aff, &expr, build);
1101 aff = extract_modulos(aff, &expr, &expr_neg, build);
1102 expr = ast_expr_sub(expr, expr_neg);
1104 ls = isl_aff_get_domain_local_space(aff);
1106 data.build = build;
1107 data.cst = isl_aff_get_constant_val(aff);
1108 for (i = 0; i < 3; ++i) {
1109 n = isl_aff_dim(aff, t[i]);
1110 for (j = 0; j < n; ++j) {
1111 v = isl_aff_get_coefficient_val(aff, t[i], j);
1112 if (!v)
1113 expr = isl_ast_expr_free(expr);
1114 if (isl_val_is_zero(v)) {
1115 isl_val_free(v);
1116 continue;
1118 expr = isl_ast_expr_add_term(expr,
1119 ls, l[i], j, v, &data);
1123 expr = isl_ast_expr_add_int(expr, data.cst);
1125 isl_local_space_free(ls);
1126 isl_aff_free(aff);
1127 return expr;
1130 /* Add terms to "expr" for each variable in "aff" with a coefficient
1131 * with sign equal to "sign".
1132 * The result is simplified in terms of data->build->domain.
1134 static __isl_give isl_ast_expr *add_signed_terms(__isl_take isl_ast_expr *expr,
1135 __isl_keep isl_aff *aff, int sign, struct isl_ast_add_term_data *data)
1137 int i, j;
1138 isl_val *v;
1139 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1140 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1141 isl_local_space *ls;
1143 ls = isl_aff_get_domain_local_space(aff);
1145 for (i = 0; i < 3; ++i) {
1146 int n = isl_aff_dim(aff, t[i]);
1147 for (j = 0; j < n; ++j) {
1148 v = isl_aff_get_coefficient_val(aff, t[i], j);
1149 if (sign * isl_val_sgn(v) <= 0) {
1150 isl_val_free(v);
1151 continue;
1153 v = isl_val_abs(v);
1154 expr = isl_ast_expr_add_term(expr,
1155 ls, l[i], j, v, data);
1159 isl_local_space_free(ls);
1161 return expr;
1164 /* Should the constant term "v" be considered positive?
1166 * A positive constant will be added to "pos" by the caller,
1167 * while a negative constant will be added to "neg".
1168 * If either "pos" or "neg" is exactly zero, then we prefer
1169 * to add the constant "v" to that side, irrespective of the sign of "v".
1170 * This results in slightly shorter expressions and may reduce the risk
1171 * of overflows.
1173 static int constant_is_considered_positive(__isl_keep isl_val *v,
1174 __isl_keep isl_ast_expr *pos, __isl_keep isl_ast_expr *neg)
1176 if (ast_expr_is_zero(pos))
1177 return 1;
1178 if (ast_expr_is_zero(neg))
1179 return 0;
1180 return isl_val_is_pos(v);
1183 /* Check if the equality
1185 * aff = 0
1187 * represents a stride constraint on the integer division "pos".
1189 * In particular, if the integer division "pos" is equal to
1191 * floor(e/d)
1193 * then check if aff is equal to
1195 * e - d floor(e/d)
1197 * or its opposite.
1199 * If so, the equality is exactly
1201 * e mod d = 0
1203 * Note that in principle we could also accept
1205 * e - d floor(e'/d)
1207 * where e and e' differ by a constant.
1209 static int is_stride_constraint(__isl_keep isl_aff *aff, int pos)
1211 isl_aff *div;
1212 isl_val *c, *d;
1213 int eq;
1215 div = isl_aff_get_div(aff, pos);
1216 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1217 d = isl_aff_get_denominator_val(div);
1218 eq = isl_val_abs_eq(c, d);
1219 if (eq >= 0 && eq) {
1220 aff = isl_aff_copy(aff);
1221 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1222 div = isl_aff_scale_val(div, d);
1223 if (isl_val_is_pos(c))
1224 div = isl_aff_neg(div);
1225 eq = isl_aff_plain_is_equal(div, aff);
1226 isl_aff_free(aff);
1227 } else
1228 isl_val_free(d);
1229 isl_val_free(c);
1230 isl_aff_free(div);
1232 return eq;
1235 /* Are all coefficients of "aff" (zero or) negative?
1237 static int all_negative_coefficients(__isl_keep isl_aff *aff)
1239 int i, n;
1241 if (!aff)
1242 return 0;
1244 n = isl_aff_dim(aff, isl_dim_param);
1245 for (i = 0; i < n; ++i)
1246 if (isl_aff_coefficient_sgn(aff, isl_dim_param, i) > 0)
1247 return 0;
1249 n = isl_aff_dim(aff, isl_dim_in);
1250 for (i = 0; i < n; ++i)
1251 if (isl_aff_coefficient_sgn(aff, isl_dim_in, i) > 0)
1252 return 0;
1254 return 1;
1257 /* Give an equality of the form
1259 * aff = e - d floor(e/d) = 0
1261 * or
1263 * aff = -e + d floor(e/d) = 0
1265 * with the integer division "pos" equal to floor(e/d),
1266 * construct the AST expression
1268 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1270 * If e only has negative coefficients, then construct
1272 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(-e), expr(d)), expr(0))
1274 * instead.
1276 static __isl_give isl_ast_expr *extract_stride_constraint(
1277 __isl_take isl_aff *aff, int pos, __isl_keep isl_ast_build *build)
1279 isl_ctx *ctx;
1280 isl_val *c;
1281 isl_ast_expr *expr, *cst;
1283 if (!aff)
1284 return NULL;
1286 ctx = isl_aff_get_ctx(aff);
1288 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1289 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1291 if (all_negative_coefficients(aff))
1292 aff = isl_aff_neg(aff);
1294 cst = isl_ast_expr_from_val(isl_val_abs(c));
1295 expr = isl_ast_expr_from_aff(aff, build);
1297 expr = isl_ast_expr_alloc_binary(isl_ast_op_zdiv_r, expr, cst);
1298 cst = isl_ast_expr_alloc_int_si(ctx, 0);
1299 expr = isl_ast_expr_alloc_binary(isl_ast_op_eq, expr, cst);
1301 return expr;
1304 /* Construct an isl_ast_expr that evaluates the condition "constraint",
1305 * The result is simplified in terms of build->domain.
1307 * We first check if the constraint is an equality of the form
1309 * e - d floor(e/d) = 0
1311 * i.e.,
1313 * e mod d = 0
1315 * If so, we convert it to
1317 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1319 * Otherwise, let the constraint by either "a >= 0" or "a == 0".
1320 * We first extract hidden modulo computations from "a"
1321 * and then collect all the terms with a positive coefficient in cons_pos
1322 * and the terms with a negative coefficient in cons_neg.
1324 * The result is then of the form
1326 * (isl_ast_op_ge, expr(pos), expr(-neg)))
1328 * or
1330 * (isl_ast_op_eq, expr(pos), expr(-neg)))
1332 * However, if the first expression is an integer constant (and the second
1333 * is not), then we swap the two expressions. This ensures that we construct,
1334 * e.g., "i <= 5" rather than "5 >= i".
1336 * Furthermore, is there are no terms with positive coefficients (or no terms
1337 * with negative coefficients), then the constant term is added to "pos"
1338 * (or "neg"), ignoring the sign of the constant term.
1340 static __isl_give isl_ast_expr *isl_ast_expr_from_constraint(
1341 __isl_take isl_constraint *constraint, __isl_keep isl_ast_build *build)
1343 int i, n;
1344 isl_ctx *ctx;
1345 isl_ast_expr *expr_pos;
1346 isl_ast_expr *expr_neg;
1347 isl_ast_expr *expr;
1348 isl_aff *aff;
1349 int eq;
1350 enum isl_ast_op_type type;
1351 struct isl_ast_add_term_data data;
1353 if (!constraint)
1354 return NULL;
1356 aff = isl_constraint_get_aff(constraint);
1357 eq = isl_constraint_is_equality(constraint);
1358 isl_constraint_free(constraint);
1360 n = isl_aff_dim(aff, isl_dim_div);
1361 if (eq && n > 0)
1362 for (i = 0; i < n; ++i) {
1363 int is_stride;
1364 is_stride = is_stride_constraint(aff, i);
1365 if (is_stride < 0)
1366 goto error;
1367 if (is_stride)
1368 return extract_stride_constraint(aff, i, build);
1371 ctx = isl_aff_get_ctx(aff);
1372 expr_pos = isl_ast_expr_alloc_int_si(ctx, 0);
1373 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1375 aff = extract_modulos(aff, &expr_pos, &expr_neg, build);
1377 data.build = build;
1378 data.cst = isl_aff_get_constant_val(aff);
1379 expr_pos = add_signed_terms(expr_pos, aff, 1, &data);
1380 data.cst = isl_val_neg(data.cst);
1381 expr_neg = add_signed_terms(expr_neg, aff, -1, &data);
1382 data.cst = isl_val_neg(data.cst);
1384 if (constant_is_considered_positive(data.cst, expr_pos, expr_neg)) {
1385 expr_pos = isl_ast_expr_add_int(expr_pos, data.cst);
1386 } else {
1387 data.cst = isl_val_neg(data.cst);
1388 expr_neg = isl_ast_expr_add_int(expr_neg, data.cst);
1391 if (isl_ast_expr_get_type(expr_pos) == isl_ast_expr_int &&
1392 isl_ast_expr_get_type(expr_neg) != isl_ast_expr_int) {
1393 type = eq ? isl_ast_op_eq : isl_ast_op_le;
1394 expr = isl_ast_expr_alloc_binary(type, expr_neg, expr_pos);
1395 } else {
1396 type = eq ? isl_ast_op_eq : isl_ast_op_ge;
1397 expr = isl_ast_expr_alloc_binary(type, expr_pos, expr_neg);
1400 isl_aff_free(aff);
1401 return expr;
1402 error:
1403 isl_aff_free(aff);
1404 return NULL;
1407 /* Wrapper around isl_constraint_cmp_last_non_zero for use
1408 * as a callback to isl_constraint_list_sort.
1409 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1410 * apart, then use isl_constraint_plain_cmp instead.
1412 static int cmp_constraint(__isl_keep isl_constraint *a,
1413 __isl_keep isl_constraint *b, void *user)
1415 int cmp;
1417 cmp = isl_constraint_cmp_last_non_zero(a, b);
1418 if (cmp != 0)
1419 return cmp;
1420 return isl_constraint_plain_cmp(a, b);
1423 /* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1424 * The result is simplified in terms of build->domain.
1426 * If "bset" is not bounded by any constraint, then we contruct
1427 * the expression "1", i.e., "true".
1429 * Otherwise, we sort the constraints, putting constraints that involve
1430 * integer divisions after those that do not, and construct an "and"
1431 * of the ast expressions of the individual constraints.
1433 * Each constraint is added to the generated constraints of the build
1434 * after it has been converted to an AST expression so that it can be used
1435 * to simplify the following constraints. This may change the truth value
1436 * of subsequent constraints that do not satisfy the earlier constraints,
1437 * but this does not affect the outcome of the conjunction as it is
1438 * only true if all the conjuncts are true (no matter in what order
1439 * they are evaluated). In particular, the constraints that do not
1440 * involve integer divisions may serve to simplify some constraints
1441 * that do involve integer divisions.
1443 __isl_give isl_ast_expr *isl_ast_build_expr_from_basic_set(
1444 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1446 int i, n;
1447 isl_constraint *c;
1448 isl_constraint_list *list;
1449 isl_ast_expr *res;
1450 isl_set *set;
1452 list = isl_basic_set_get_constraint_list(bset);
1453 isl_basic_set_free(bset);
1454 list = isl_constraint_list_sort(list, &cmp_constraint, NULL);
1455 if (!list)
1456 return NULL;
1457 n = isl_constraint_list_n_constraint(list);
1458 if (n == 0) {
1459 isl_ctx *ctx = isl_constraint_list_get_ctx(list);
1460 isl_constraint_list_free(list);
1461 return isl_ast_expr_alloc_int_si(ctx, 1);
1464 build = isl_ast_build_copy(build);
1466 c = isl_constraint_list_get_constraint(list, 0);
1467 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1468 set = isl_set_from_basic_set(bset);
1469 res = isl_ast_expr_from_constraint(c, build);
1470 build = isl_ast_build_restrict_generated(build, set);
1472 for (i = 1; i < n; ++i) {
1473 isl_ast_expr *expr;
1475 c = isl_constraint_list_get_constraint(list, i);
1476 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1477 set = isl_set_from_basic_set(bset);
1478 expr = isl_ast_expr_from_constraint(c, build);
1479 build = isl_ast_build_restrict_generated(build, set);
1480 res = isl_ast_expr_and(res, expr);
1483 isl_constraint_list_free(list);
1484 isl_ast_build_free(build);
1485 return res;
1488 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1489 * The result is simplified in terms of build->domain.
1491 * If "set" is an (obviously) empty set, then return the expression "0".
1493 * If there are multiple disjuncts in the description of the set,
1494 * then subsequent disjuncts are simplified in a context where
1495 * the previous disjuncts have been removed from build->domain.
1496 * In particular, constraints that ensure that there is no overlap
1497 * with these previous disjuncts, can be removed.
1498 * This is mostly useful for disjuncts that are only defined by
1499 * a single constraint (relative to the build domain) as the opposite
1500 * of that single constraint can then be removed from the other disjuncts.
1501 * In order not to increase the number of disjuncts in the build domain
1502 * after subtracting the previous disjuncts of "set", the simple hull
1503 * is computed after taking the difference with each of these disjuncts.
1504 * This means that constraints that prevent overlap with a union
1505 * of multiple previous disjuncts are not removed.
1507 * "set" lives in the internal schedule space.
1509 __isl_give isl_ast_expr *isl_ast_build_expr_from_set_internal(
1510 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1512 int i, n;
1513 isl_basic_set *bset;
1514 isl_basic_set_list *list;
1515 isl_set *domain;
1516 isl_ast_expr *res;
1518 list = isl_set_get_basic_set_list(set);
1519 isl_set_free(set);
1521 if (!list)
1522 return NULL;
1523 n = isl_basic_set_list_n_basic_set(list);
1524 if (n == 0) {
1525 isl_ctx *ctx = isl_ast_build_get_ctx(build);
1526 isl_basic_set_list_free(list);
1527 return isl_ast_expr_from_val(isl_val_zero(ctx));
1530 domain = isl_ast_build_get_domain(build);
1532 bset = isl_basic_set_list_get_basic_set(list, 0);
1533 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1534 res = isl_ast_build_expr_from_basic_set(build, bset);
1536 for (i = 1; i < n; ++i) {
1537 isl_ast_expr *expr;
1538 isl_set *rest;
1540 rest = isl_set_subtract(isl_set_copy(domain), set);
1541 rest = isl_set_from_basic_set(isl_set_simple_hull(rest));
1542 domain = isl_set_intersect(domain, rest);
1543 bset = isl_basic_set_list_get_basic_set(list, i);
1544 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1545 bset = isl_basic_set_gist(bset,
1546 isl_set_simple_hull(isl_set_copy(domain)));
1547 expr = isl_ast_build_expr_from_basic_set(build, bset);
1548 res = isl_ast_expr_or(res, expr);
1551 isl_set_free(domain);
1552 isl_set_free(set);
1553 isl_basic_set_list_free(list);
1554 return res;
1557 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1558 * The result is simplified in terms of build->domain.
1560 * If "set" is an (obviously) empty set, then return the expression "0".
1562 * "set" lives in the external schedule space.
1564 * The internal AST expression generation assumes that there are
1565 * no unknown divs, so make sure an explicit representation is available.
1566 * Since the set comes from the outside, it may have constraints that
1567 * are redundant with respect to the build domain. Remove them first.
1569 __isl_give isl_ast_expr *isl_ast_build_expr_from_set(
1570 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1572 if (isl_ast_build_need_schedule_map(build)) {
1573 isl_multi_aff *ma;
1574 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1575 set = isl_set_preimage_multi_aff(set, ma);
1578 set = isl_set_compute_divs(set);
1579 set = isl_ast_build_compute_gist(build, set);
1580 return isl_ast_build_expr_from_set_internal(build, set);
1583 /* State of data about previous pieces in
1584 * isl_ast_build_expr_from_pw_aff_internal.
1586 * isl_state_none: no data about previous pieces
1587 * isl_state_single: data about a single previous piece
1588 * isl_state_min: data represents minimum of several pieces
1589 * isl_state_max: data represents maximum of several pieces
1591 enum isl_from_pw_aff_state {
1592 isl_state_none,
1593 isl_state_single,
1594 isl_state_min,
1595 isl_state_max
1598 /* Internal date structure representing a single piece in the input of
1599 * isl_ast_build_expr_from_pw_aff_internal.
1601 * If "state" is isl_state_none, then "set_list" and "aff_list" are not used.
1602 * If "state" is isl_state_single, then "set_list" and "aff_list" contain the
1603 * single previous subpiece.
1604 * If "state" is isl_state_min, then "set_list" and "aff_list" contain
1605 * a sequence of several previous subpieces that are equal to the minimum
1606 * of the entries in "aff_list" over the union of "set_list"
1607 * If "state" is isl_state_max, then "set_list" and "aff_list" contain
1608 * a sequence of several previous subpieces that are equal to the maximum
1609 * of the entries in "aff_list" over the union of "set_list"
1611 * During the construction of the pieces, "set" is NULL.
1612 * After the construction, "set" is set to the union of the elements
1613 * in "set_list", at which point "set_list" is set to NULL.
1615 struct isl_from_pw_aff_piece {
1616 enum isl_from_pw_aff_state state;
1617 isl_set *set;
1618 isl_set_list *set_list;
1619 isl_aff_list *aff_list;
1622 /* Internal data structure for isl_ast_build_expr_from_pw_aff_internal.
1624 * "build" specifies the domain against which the result is simplified.
1625 * "dom" is the domain of the entire isl_pw_aff.
1627 * "n" is the number of pieces constructed already.
1628 * In particular, during the construction of the pieces, "n" points to
1629 * the piece that is being constructed. After the construction of the
1630 * pieces, "n" is set to the total number of pieces.
1631 * "max" is the total number of allocated entries.
1632 * "p" contains the individual pieces.
1634 struct isl_from_pw_aff_data {
1635 isl_ast_build *build;
1636 isl_set *dom;
1638 int n;
1639 int max;
1640 struct isl_from_pw_aff_piece *p;
1643 /* Initialize "data" based on "build" and "pa".
1645 static isl_stat isl_from_pw_aff_data_init(struct isl_from_pw_aff_data *data,
1646 __isl_keep isl_ast_build *build, __isl_keep isl_pw_aff *pa)
1648 int n;
1649 isl_ctx *ctx;
1651 ctx = isl_pw_aff_get_ctx(pa);
1652 n = isl_pw_aff_n_piece(pa);
1653 if (n == 0)
1654 isl_die(ctx, isl_error_invalid,
1655 "cannot handle void expression", return isl_stat_error);
1656 data->max = n;
1657 data->p = isl_calloc_array(ctx, struct isl_from_pw_aff_piece, n);
1658 if (!data->p)
1659 return isl_stat_error;
1660 data->build = build;
1661 data->dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1662 data->n = 0;
1664 return isl_stat_ok;
1667 /* Free all memory allocated for "data".
1669 static void isl_from_pw_aff_data_clear(struct isl_from_pw_aff_data *data)
1671 int i;
1673 isl_set_free(data->dom);
1674 if (!data->p)
1675 return;
1677 for (i = 0; i < data->max; ++i) {
1678 isl_set_free(data->p[i].set);
1679 isl_set_list_free(data->p[i].set_list);
1680 isl_aff_list_free(data->p[i].aff_list);
1682 free(data->p);
1685 /* Initialize the current entry of "data" to an unused piece.
1687 static void set_none(struct isl_from_pw_aff_data *data)
1689 data->p[data->n].state = isl_state_none;
1690 data->p[data->n].set_list = NULL;
1691 data->p[data->n].aff_list = NULL;
1694 /* Store "set" and "aff" in the current entry of "data" as a single subpiece.
1696 static void set_single(struct isl_from_pw_aff_data *data,
1697 __isl_take isl_set *set, __isl_take isl_aff *aff)
1699 data->p[data->n].state = isl_state_single;
1700 data->p[data->n].set_list = isl_set_list_from_set(set);
1701 data->p[data->n].aff_list = isl_aff_list_from_aff(aff);
1704 /* Extend the current entry of "data" with "set" and "aff"
1705 * as a minimum expression.
1707 static isl_stat extend_min(struct isl_from_pw_aff_data *data,
1708 __isl_take isl_set *set, __isl_take isl_aff *aff)
1710 int n = data->n;
1711 data->p[n].state = isl_state_min;
1712 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1713 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1715 if (!data->p[n].set_list || !data->p[n].aff_list)
1716 return isl_stat_error;
1717 return isl_stat_ok;
1720 /* Extend the current entry of "data" with "set" and "aff"
1721 * as a maximum expression.
1723 static isl_stat extend_max(struct isl_from_pw_aff_data *data,
1724 __isl_take isl_set *set, __isl_take isl_aff *aff)
1726 int n = data->n;
1727 data->p[n].state = isl_state_max;
1728 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1729 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1731 if (!data->p[n].set_list || !data->p[n].aff_list)
1732 return isl_stat_error;
1733 return isl_stat_ok;
1736 /* Extend the domain of the current entry of "data", which is assumed
1737 * to contain a single subpiece, with "set". If "replace" is set,
1738 * then also replace the affine function by "aff". Otherwise,
1739 * simply free "aff".
1741 static isl_stat extend_domain(struct isl_from_pw_aff_data *data,
1742 __isl_take isl_set *set, __isl_take isl_aff *aff, int replace)
1744 int n = data->n;
1745 isl_set *set_n;
1747 set_n = isl_set_list_get_set(data->p[n].set_list, 0);
1748 set_n = isl_set_union(set_n, set);
1749 data->p[n].set_list =
1750 isl_set_list_set_set(data->p[n].set_list, 0, set_n);
1752 if (replace)
1753 data->p[n].aff_list =
1754 isl_aff_list_set_aff(data->p[n].aff_list, 0, aff);
1755 else
1756 isl_aff_free(aff);
1758 if (!data->p[n].set_list || !data->p[n].aff_list)
1759 return isl_stat_error;
1760 return isl_stat_ok;
1763 /* Construct an isl_ast_expr from "list" within "build".
1764 * If "state" is isl_state_single, then "list" contains a single entry and
1765 * an isl_ast_expr is constructed for that entry.
1766 * Otherwise a min or max expression is constructed from "list"
1767 * depending on "state".
1769 static __isl_give isl_ast_expr *ast_expr_from_aff_list(
1770 __isl_take isl_aff_list *list, enum isl_from_pw_aff_state state,
1771 __isl_keep isl_ast_build *build)
1773 int i, n;
1774 isl_aff *aff;
1775 isl_ast_expr *expr;
1776 enum isl_ast_op_type op_type;
1778 if (state == isl_state_single) {
1779 aff = isl_aff_list_get_aff(list, 0);
1780 isl_aff_list_free(list);
1781 return isl_ast_expr_from_aff(aff, build);
1783 n = isl_aff_list_n_aff(list);
1784 op_type = state == isl_state_min ? isl_ast_op_min : isl_ast_op_max;
1785 expr = isl_ast_expr_alloc_op(isl_ast_build_get_ctx(build), op_type, n);
1786 if (!expr)
1787 goto error;
1789 for (i = 0; i < n; ++i) {
1790 isl_ast_expr *expr_i;
1792 aff = isl_aff_list_get_aff(list, i);
1793 expr_i = isl_ast_expr_from_aff(aff, build);
1794 if (!expr_i)
1795 goto error;
1796 expr->u.op.args[i] = expr_i;
1799 isl_aff_list_free(list);
1800 return expr;
1801 error:
1802 isl_aff_list_free(list);
1803 isl_ast_expr_free(expr);
1804 return NULL;
1807 /* Extend the expression in "next" to take into account
1808 * the piece at position "pos" in "data", allowing for a further extension
1809 * for the next piece(s).
1810 * In particular, "next" is set to a select operation that selects
1811 * an isl_ast_expr corresponding to data->aff_list on data->set and
1812 * to an expression that will be filled in by later calls.
1813 * Return a pointer to this location.
1814 * Afterwards, the state of "data" is set to isl_state_none.
1816 * The constraints of data->set are added to the generated
1817 * constraints of the build such that they can be exploited to simplify
1818 * the AST expression constructed from data->aff_list.
1820 static isl_ast_expr **add_intermediate_piece(struct isl_from_pw_aff_data *data,
1821 int pos, isl_ast_expr **next)
1823 isl_ctx *ctx;
1824 isl_ast_build *build;
1825 isl_ast_expr *ternary, *arg;
1826 isl_set *set, *gist;
1828 set = data->p[pos].set;
1829 data->p[pos].set = NULL;
1830 ctx = isl_ast_build_get_ctx(data->build);
1831 ternary = isl_ast_expr_alloc_op(ctx, isl_ast_op_select, 3);
1832 gist = isl_set_gist(isl_set_copy(set), isl_set_copy(data->dom));
1833 arg = isl_ast_build_expr_from_set_internal(data->build, gist);
1834 ternary = isl_ast_expr_set_op_arg(ternary, 0, arg);
1835 build = isl_ast_build_copy(data->build);
1836 build = isl_ast_build_restrict_generated(build, set);
1837 arg = ast_expr_from_aff_list(data->p[pos].aff_list,
1838 data->p[pos].state, build);
1839 data->p[pos].aff_list = NULL;
1840 isl_ast_build_free(build);
1841 ternary = isl_ast_expr_set_op_arg(ternary, 1, arg);
1842 data->p[pos].state = isl_state_none;
1843 if (!ternary)
1844 return NULL;
1846 *next = ternary;
1847 return &ternary->u.op.args[2];
1850 /* Extend the expression in "next" to take into account
1851 * the final piece, located at position "pos" in "data".
1852 * In particular, "next" is set to evaluate data->aff_list
1853 * and the domain is ignored.
1854 * Return isl_stat_ok on success and isl_stat_error on failure.
1856 * The constraints of data->set are however added to the generated
1857 * constraints of the build such that they can be exploited to simplify
1858 * the AST expression constructed from data->aff_list.
1860 static isl_stat add_last_piece(struct isl_from_pw_aff_data *data,
1861 int pos, isl_ast_expr **next)
1863 isl_ast_build *build;
1865 if (data->p[pos].state == isl_state_none)
1866 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1867 "cannot handle void expression", return isl_stat_error);
1869 build = isl_ast_build_copy(data->build);
1870 build = isl_ast_build_restrict_generated(build, data->p[pos].set);
1871 data->p[pos].set = NULL;
1872 *next = ast_expr_from_aff_list(data->p[pos].aff_list,
1873 data->p[pos].state, build);
1874 data->p[pos].aff_list = NULL;
1875 isl_ast_build_free(build);
1876 data->p[pos].state = isl_state_none;
1877 if (!*next)
1878 return isl_stat_error;
1880 return isl_stat_ok;
1883 /* Return -1 if the piece "p1" should be sorted before "p2"
1884 * and 1 if it should be sorted after "p2".
1885 * Return 0 if they do not need to be sorted in a specific order.
1887 * Pieces are sorted according to the number of disjuncts
1888 * in their domains.
1890 static int sort_pieces_cmp(const void *p1, const void *p2, void *arg)
1892 const struct isl_from_pw_aff_piece *piece1 = p1;
1893 const struct isl_from_pw_aff_piece *piece2 = p2;
1894 int n1, n2;
1896 n1 = isl_set_n_basic_set(piece1->set);
1897 n2 = isl_set_n_basic_set(piece2->set);
1899 return n1 - n2;
1902 /* Construct an isl_ast_expr from the pieces in "data".
1903 * Return the result or NULL on failure.
1905 * When this function is called, data->n points to the current piece.
1906 * If this is an effective piece, then first increment data->n such
1907 * that data->n contains the number of pieces.
1908 * The "set_list" fields are subsequently replaced by the corresponding
1909 * "set" fields, after which the pieces are sorted according to
1910 * the number of disjuncts in these "set" fields.
1912 * Construct intermediate AST expressions for the initial pieces and
1913 * finish off with the final pieces.
1915 static isl_ast_expr *build_pieces(struct isl_from_pw_aff_data *data)
1917 int i;
1918 isl_ast_expr *res = NULL;
1919 isl_ast_expr **next = &res;
1921 if (data->p[data->n].state != isl_state_none)
1922 data->n++;
1923 if (data->n == 0)
1924 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1925 "cannot handle void expression", return NULL);
1927 for (i = 0; i < data->n; ++i) {
1928 data->p[i].set = isl_set_list_union(data->p[i].set_list);
1929 if (data->p[i].state != isl_state_single)
1930 data->p[i].set = isl_set_coalesce(data->p[i].set);
1931 data->p[i].set_list = NULL;
1934 if (isl_sort(data->p, data->n, sizeof(data->p[0]),
1935 &sort_pieces_cmp, NULL) < 0)
1936 return isl_ast_expr_free(res);
1938 for (i = 0; i + 1 < data->n; ++i) {
1939 next = add_intermediate_piece(data, i, next);
1940 if (!next)
1941 return isl_ast_expr_free(res);
1944 if (add_last_piece(data, data->n - 1, next) < 0)
1945 return isl_ast_expr_free(res);
1947 return res;
1950 /* Is the domain of the current entry of "data", which is assumed
1951 * to contain a single subpiece, a subset of "set"?
1953 static isl_bool single_is_subset(struct isl_from_pw_aff_data *data,
1954 __isl_keep isl_set *set)
1956 isl_bool subset;
1957 isl_set *set_n;
1959 set_n = isl_set_list_get_set(data->p[data->n].set_list, 0);
1960 subset = isl_set_is_subset(set_n, set);
1961 isl_set_free(set_n);
1963 return subset;
1966 /* Is "aff" a rational expression, i.e., does it have a denominator
1967 * different from one?
1969 static isl_bool aff_is_rational(__isl_keep isl_aff *aff)
1971 isl_bool rational;
1972 isl_val *den;
1974 den = isl_aff_get_denominator_val(aff);
1975 rational = isl_bool_not(isl_val_is_one(den));
1976 isl_val_free(den);
1978 return rational;
1981 /* Does "list" consist of a single rational affine expression?
1983 static isl_bool is_single_rational_aff(__isl_keep isl_aff_list *list)
1985 isl_bool rational;
1986 isl_aff *aff;
1988 if (isl_aff_list_n_aff(list) != 1)
1989 return isl_bool_false;
1990 aff = isl_aff_list_get_aff(list, 0);
1991 rational = aff_is_rational(aff);
1992 isl_aff_free(aff);
1994 return rational;
1997 /* Can the list of subpieces in the last piece of "data" be extended with
1998 * "set" and "aff" based on "test"?
1999 * In particular, is it the case for each entry (set_i, aff_i) that
2001 * test(aff, aff_i) holds on set_i, and
2002 * test(aff_i, aff) holds on set?
2004 * "test" returns the set of elements where the tests holds, meaning
2005 * that test(aff_i, aff) holds on set if set is a subset of test(aff_i, aff).
2007 * This function is used to detect min/max expressions.
2008 * If the ast_build_detect_min_max option is turned off, then
2009 * do not even try and perform any detection and return false instead.
2011 * Rational affine expressions are not considered for min/max expressions
2012 * since the combined expression will be defined on the union of the domains,
2013 * while a rational expression may only yield integer values
2014 * on its own definition domain.
2016 static isl_bool extends(struct isl_from_pw_aff_data *data,
2017 __isl_keep isl_set *set, __isl_keep isl_aff *aff,
2018 __isl_give isl_basic_set *(*test)(__isl_take isl_aff *aff1,
2019 __isl_take isl_aff *aff2))
2021 int i, n;
2022 isl_bool is_rational;
2023 isl_ctx *ctx;
2024 isl_set *dom;
2026 is_rational = aff_is_rational(aff);
2027 if (is_rational >= 0 && !is_rational)
2028 is_rational = is_single_rational_aff(data->p[data->n].aff_list);
2029 if (is_rational < 0 || is_rational)
2030 return isl_bool_not(is_rational);
2032 ctx = isl_ast_build_get_ctx(data->build);
2033 if (!isl_options_get_ast_build_detect_min_max(ctx))
2034 return isl_bool_false;
2036 dom = isl_ast_build_get_domain(data->build);
2037 set = isl_set_intersect(dom, isl_set_copy(set));
2039 n = isl_set_list_n_set(data->p[data->n].set_list);
2040 for (i = 0; i < n ; ++i) {
2041 isl_aff *aff_i;
2042 isl_set *valid;
2043 isl_set *dom, *required;
2044 isl_bool is_valid;
2046 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2047 valid = isl_set_from_basic_set(test(isl_aff_copy(aff), aff_i));
2048 required = isl_set_list_get_set(data->p[data->n].set_list, i);
2049 dom = isl_ast_build_get_domain(data->build);
2050 required = isl_set_intersect(dom, required);
2051 is_valid = isl_set_is_subset(required, valid);
2052 isl_set_free(required);
2053 isl_set_free(valid);
2054 if (is_valid < 0 || !is_valid) {
2055 isl_set_free(set);
2056 return is_valid;
2059 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2060 valid = isl_set_from_basic_set(test(aff_i, isl_aff_copy(aff)));
2061 is_valid = isl_set_is_subset(set, valid);
2062 isl_set_free(valid);
2063 if (is_valid < 0 || !is_valid) {
2064 isl_set_free(set);
2065 return is_valid;
2069 isl_set_free(set);
2070 return isl_bool_true;
2073 /* Can the list of pieces in "data" be extended with "set" and "aff"
2074 * to form/preserve a minimum expression?
2075 * In particular, is it the case for each entry (set_i, aff_i) that
2077 * aff >= aff_i on set_i, and
2078 * aff_i >= aff on set?
2080 static isl_bool extends_min(struct isl_from_pw_aff_data *data,
2081 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
2083 return extends(data, set, aff, &isl_aff_ge_basic_set);
2086 /* Can the list of pieces in "data" be extended with "set" and "aff"
2087 * to form/preserve a maximum expression?
2088 * In particular, is it the case for each entry (set_i, aff_i) that
2090 * aff <= aff_i on set_i, and
2091 * aff_i <= aff on set?
2093 static isl_bool extends_max(struct isl_from_pw_aff_data *data,
2094 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
2096 return extends(data, set, aff, &isl_aff_le_basic_set);
2099 /* This function is called during the construction of an isl_ast_expr
2100 * that evaluates an isl_pw_aff.
2101 * If the last piece of "data" contains a single subpiece and
2102 * if its affine function is equal to "aff" on a part of the domain
2103 * that includes either "set" or the domain of that single subpiece,
2104 * then extend the domain of that single subpiece with "set".
2105 * If it was the original domain of the single subpiece where
2106 * the two affine functions are equal, then also replace
2107 * the affine function of the single subpiece by "aff".
2108 * If the last piece of "data" contains either a single subpiece
2109 * or a minimum, then check if this minimum expression can be extended
2110 * with (set, aff).
2111 * If so, extend the sequence and return.
2112 * Perform the same operation for maximum expressions.
2113 * If no such extension can be performed, then move to the next piece
2114 * in "data" (if the current piece contains any data), and then store
2115 * the current subpiece in the current piece of "data" for later handling.
2117 static isl_stat ast_expr_from_pw_aff(__isl_take isl_set *set,
2118 __isl_take isl_aff *aff, void *user)
2120 struct isl_from_pw_aff_data *data = user;
2121 isl_bool test;
2122 enum isl_from_pw_aff_state state;
2124 state = data->p[data->n].state;
2125 if (state == isl_state_single) {
2126 isl_aff *aff0;
2127 isl_set *eq;
2128 isl_bool subset1, subset2 = isl_bool_false;
2129 aff0 = isl_aff_list_get_aff(data->p[data->n].aff_list, 0);
2130 eq = isl_aff_eq_set(isl_aff_copy(aff), aff0);
2131 subset1 = isl_set_is_subset(set, eq);
2132 if (subset1 >= 0 && !subset1)
2133 subset2 = single_is_subset(data, eq);
2134 isl_set_free(eq);
2135 if (subset1 < 0 || subset2 < 0)
2136 goto error;
2137 if (subset1)
2138 return extend_domain(data, set, aff, 0);
2139 if (subset2)
2140 return extend_domain(data, set, aff, 1);
2142 if (state == isl_state_single || state == isl_state_min) {
2143 test = extends_min(data, set, aff);
2144 if (test < 0)
2145 goto error;
2146 if (test)
2147 return extend_min(data, set, aff);
2149 if (state == isl_state_single || state == isl_state_max) {
2150 test = extends_max(data, set, aff);
2151 if (test < 0)
2152 goto error;
2153 if (test)
2154 return extend_max(data, set, aff);
2156 if (state != isl_state_none)
2157 data->n++;
2158 set_single(data, set, aff);
2160 return isl_stat_ok;
2161 error:
2162 isl_set_free(set);
2163 isl_aff_free(aff);
2164 return isl_stat_error;
2167 /* Construct an isl_ast_expr that evaluates "pa".
2168 * The result is simplified in terms of build->domain.
2170 * The domain of "pa" lives in the internal schedule space.
2172 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff_internal(
2173 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2175 struct isl_from_pw_aff_data data = { NULL };
2176 isl_ast_expr *res = NULL;
2178 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
2179 pa = isl_pw_aff_coalesce(pa);
2180 if (!pa)
2181 return NULL;
2183 if (isl_from_pw_aff_data_init(&data, build, pa) < 0)
2184 goto error;
2185 set_none(&data);
2187 if (isl_pw_aff_foreach_piece(pa, &ast_expr_from_pw_aff, &data) >= 0)
2188 res = build_pieces(&data);
2190 isl_pw_aff_free(pa);
2191 isl_from_pw_aff_data_clear(&data);
2192 return res;
2193 error:
2194 isl_pw_aff_free(pa);
2195 isl_from_pw_aff_data_clear(&data);
2196 return NULL;
2199 /* Construct an isl_ast_expr that evaluates "pa".
2200 * The result is simplified in terms of build->domain.
2202 * The domain of "pa" lives in the external schedule space.
2204 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff(
2205 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2207 isl_ast_expr *expr;
2209 if (isl_ast_build_need_schedule_map(build)) {
2210 isl_multi_aff *ma;
2211 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2212 pa = isl_pw_aff_pullback_multi_aff(pa, ma);
2214 expr = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2215 return expr;
2218 /* Set the ids of the input dimensions of "mpa" to the iterator ids
2219 * of "build".
2221 * The domain of "mpa" is assumed to live in the internal schedule domain.
2223 static __isl_give isl_multi_pw_aff *set_iterator_names(
2224 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2226 int i, n;
2228 n = isl_multi_pw_aff_dim(mpa, isl_dim_in);
2229 for (i = 0; i < n; ++i) {
2230 isl_id *id;
2232 id = isl_ast_build_get_iterator_id(build, i);
2233 mpa = isl_multi_pw_aff_set_dim_id(mpa, isl_dim_in, i, id);
2236 return mpa;
2239 /* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
2240 * the remaining arguments derived from "mpa".
2241 * That is, construct a call or access expression that calls/accesses "arg0"
2242 * with arguments/indices specified by "mpa".
2244 static __isl_give isl_ast_expr *isl_ast_build_with_arguments(
2245 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2246 __isl_take isl_ast_expr *arg0, __isl_take isl_multi_pw_aff *mpa)
2248 int i, n;
2249 isl_ctx *ctx;
2250 isl_ast_expr *expr;
2252 ctx = isl_ast_build_get_ctx(build);
2254 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
2255 expr = isl_ast_expr_alloc_op(ctx, type, 1 + n);
2256 expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
2257 for (i = 0; i < n; ++i) {
2258 isl_pw_aff *pa;
2259 isl_ast_expr *arg;
2261 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
2262 arg = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2263 expr = isl_ast_expr_set_op_arg(expr, 1 + i, arg);
2266 isl_multi_pw_aff_free(mpa);
2267 return expr;
2270 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2271 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2272 __isl_take isl_multi_pw_aff *mpa);
2274 /* Construct an isl_ast_expr that accesses the member specified by "mpa".
2275 * The range of "mpa" is assumed to be wrapped relation.
2276 * The domain of this wrapped relation specifies the structure being
2277 * accessed, while the range of this wrapped relation spacifies the
2278 * member of the structure being accessed.
2280 * The domain of "mpa" is assumed to live in the internal schedule domain.
2282 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_member(
2283 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2285 isl_id *id;
2286 isl_multi_pw_aff *domain;
2287 isl_ast_expr *domain_expr, *expr;
2288 enum isl_ast_op_type type = isl_ast_op_access;
2290 domain = isl_multi_pw_aff_copy(mpa);
2291 domain = isl_multi_pw_aff_range_factor_domain(domain);
2292 domain_expr = isl_ast_build_from_multi_pw_aff_internal(build,
2293 type, domain);
2294 mpa = isl_multi_pw_aff_range_factor_range(mpa);
2295 if (!isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2296 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2297 "missing field name", goto error);
2298 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2299 expr = isl_ast_expr_from_id(id);
2300 expr = isl_ast_expr_alloc_binary(isl_ast_op_member, domain_expr, expr);
2301 return isl_ast_build_with_arguments(build, type, expr, mpa);
2302 error:
2303 isl_multi_pw_aff_free(mpa);
2304 return NULL;
2307 /* Construct an isl_ast_expr of type "type" that calls or accesses
2308 * the element specified by "mpa".
2309 * The first argument is obtained from the output tuple name.
2310 * The remaining arguments are given by the piecewise affine expressions.
2312 * If the range of "mpa" is a mapped relation, then we assume it
2313 * represents an access to a member of a structure.
2315 * The domain of "mpa" is assumed to live in the internal schedule domain.
2317 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2318 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2319 __isl_take isl_multi_pw_aff *mpa)
2321 isl_ctx *ctx;
2322 isl_id *id;
2323 isl_ast_expr *expr;
2325 if (!mpa)
2326 goto error;
2328 if (type == isl_ast_op_access &&
2329 isl_multi_pw_aff_range_is_wrapping(mpa))
2330 return isl_ast_build_from_multi_pw_aff_member(build, mpa);
2332 mpa = set_iterator_names(build, mpa);
2333 if (!build || !mpa)
2334 goto error;
2336 ctx = isl_ast_build_get_ctx(build);
2338 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2339 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2340 else
2341 id = isl_id_alloc(ctx, "", NULL);
2343 expr = isl_ast_expr_from_id(id);
2344 return isl_ast_build_with_arguments(build, type, expr, mpa);
2345 error:
2346 isl_multi_pw_aff_free(mpa);
2347 return NULL;
2350 /* Construct an isl_ast_expr of type "type" that calls or accesses
2351 * the element specified by "pma".
2352 * The first argument is obtained from the output tuple name.
2353 * The remaining arguments are given by the piecewise affine expressions.
2355 * The domain of "pma" is assumed to live in the internal schedule domain.
2357 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff_internal(
2358 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2359 __isl_take isl_pw_multi_aff *pma)
2361 isl_multi_pw_aff *mpa;
2363 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2364 return isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2367 /* Construct an isl_ast_expr of type "type" that calls or accesses
2368 * the element specified by "mpa".
2369 * The first argument is obtained from the output tuple name.
2370 * The remaining arguments are given by the piecewise affine expressions.
2372 * The domain of "mpa" is assumed to live in the external schedule domain.
2374 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff(
2375 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2376 __isl_take isl_multi_pw_aff *mpa)
2378 int is_domain;
2379 isl_ast_expr *expr;
2380 isl_space *space_build, *space_mpa;
2382 space_build = isl_ast_build_get_space(build, 0);
2383 space_mpa = isl_multi_pw_aff_get_space(mpa);
2384 is_domain = isl_space_tuple_is_equal(space_build, isl_dim_set,
2385 space_mpa, isl_dim_in);
2386 isl_space_free(space_build);
2387 isl_space_free(space_mpa);
2388 if (is_domain < 0)
2389 goto error;
2390 if (!is_domain)
2391 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2392 "spaces don't match", goto error);
2394 if (isl_ast_build_need_schedule_map(build)) {
2395 isl_multi_aff *ma;
2396 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2397 mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
2400 expr = isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2401 return expr;
2402 error:
2403 isl_multi_pw_aff_free(mpa);
2404 return NULL;
2407 /* Construct an isl_ast_expr that calls the domain element specified by "mpa".
2408 * The name of the function is obtained from the output tuple name.
2409 * The arguments are given by the piecewise affine expressions.
2411 * The domain of "mpa" is assumed to live in the external schedule domain.
2413 __isl_give isl_ast_expr *isl_ast_build_call_from_multi_pw_aff(
2414 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2416 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_call, mpa);
2419 /* Construct an isl_ast_expr that accesses the array element specified by "mpa".
2420 * The name of the array is obtained from the output tuple name.
2421 * The index expressions are given by the piecewise affine expressions.
2423 * The domain of "mpa" is assumed to live in the external schedule domain.
2425 __isl_give isl_ast_expr *isl_ast_build_access_from_multi_pw_aff(
2426 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2428 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_access, mpa);
2431 /* Construct an isl_ast_expr of type "type" that calls or accesses
2432 * the element specified by "pma".
2433 * The first argument is obtained from the output tuple name.
2434 * The remaining arguments are given by the piecewise affine expressions.
2436 * The domain of "pma" is assumed to live in the external schedule domain.
2438 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff(
2439 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2440 __isl_take isl_pw_multi_aff *pma)
2442 isl_multi_pw_aff *mpa;
2444 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2445 return isl_ast_build_from_multi_pw_aff(build, type, mpa);
2448 /* Construct an isl_ast_expr that calls the domain element specified by "pma".
2449 * The name of the function is obtained from the output tuple name.
2450 * The arguments are given by the piecewise affine expressions.
2452 * The domain of "pma" is assumed to live in the external schedule domain.
2454 __isl_give isl_ast_expr *isl_ast_build_call_from_pw_multi_aff(
2455 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2457 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_call, pma);
2460 /* Construct an isl_ast_expr that accesses the array element specified by "pma".
2461 * The name of the array is obtained from the output tuple name.
2462 * The index expressions are given by the piecewise affine expressions.
2464 * The domain of "pma" is assumed to live in the external schedule domain.
2466 __isl_give isl_ast_expr *isl_ast_build_access_from_pw_multi_aff(
2467 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2469 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_access, pma);
2472 /* Construct an isl_ast_expr that calls the domain element
2473 * specified by "executed".
2475 * "executed" is assumed to be single-valued, with a domain that lives
2476 * in the internal schedule space.
2478 __isl_give isl_ast_node *isl_ast_build_call_from_executed(
2479 __isl_keep isl_ast_build *build, __isl_take isl_map *executed)
2481 isl_pw_multi_aff *iteration;
2482 isl_ast_expr *expr;
2484 iteration = isl_pw_multi_aff_from_map(executed);
2485 iteration = isl_ast_build_compute_gist_pw_multi_aff(build, iteration);
2486 iteration = isl_pw_multi_aff_intersect_domain(iteration,
2487 isl_ast_build_get_domain(build));
2488 expr = isl_ast_build_from_pw_multi_aff_internal(build, isl_ast_op_call,
2489 iteration);
2490 return isl_ast_node_alloc_user(expr);