isl_ast_build_expr_from_basic_set: do not use freed bset to get ctx
[isl.git] / isl_ast_build_expr.c
blob85a5453137e88c7e0dcb186acdfc8e4ef5a30fd3
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/constraint.h>
14 #include <isl/ilp.h>
15 #include <isl_ast_build_expr.h>
16 #include <isl_ast_private.h>
17 #include <isl_ast_build_private.h>
19 /* Compute the "opposite" of the (numerator of the) argument of a div
20 * with denonimator "d".
22 * In particular, compute
24 * -aff + (d - 1)
26 static __isl_give isl_aff *oppose_div_arg(__isl_take isl_aff *aff,
27 __isl_take isl_val *d)
29 aff = isl_aff_neg(aff);
30 aff = isl_aff_add_constant_val(aff, d);
31 aff = isl_aff_add_constant_si(aff, -1);
33 return aff;
36 /* Internal data structure used inside isl_ast_expr_add_term.
37 * The domain of "build" is used to simplify the expressions.
38 * "build" needs to be set by the caller of isl_ast_expr_add_term.
39 * "cst" is the constant term of the expression in which the added term
40 * appears. It may be modified by isl_ast_expr_add_term.
42 * "v" is the coefficient of the term that is being constructed and
43 * is set internally by isl_ast_expr_add_term.
45 struct isl_ast_add_term_data {
46 isl_ast_build *build;
47 isl_val *cst;
48 isl_val *v;
51 /* Given the numerator "aff" of the argument of an integer division
52 * with denominator "d", check if it can be made non-negative over
53 * data->build->domain by stealing part of the constant term of
54 * the expression in which the integer division appears.
56 * In particular, the outer expression is of the form
58 * v * floor(aff/d) + cst
60 * We already know that "aff" itself may attain negative values.
61 * Here we check if aff + d*floor(cst/v) is non-negative, such
62 * that we could rewrite the expression to
64 * v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
66 * Note that aff + d*floor(cst/v) can only possibly be non-negative
67 * if data->cst and data->v have the same sign.
68 * Similarly, if floor(cst/v) is zero, then there is no point in
69 * checking again.
71 static int is_non_neg_after_stealing(__isl_keep isl_aff *aff,
72 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
74 isl_aff *shifted;
75 isl_val *shift;
76 int is_zero;
77 int non_neg;
79 if (isl_val_sgn(data->cst) != isl_val_sgn(data->v))
80 return 0;
82 shift = isl_val_div(isl_val_copy(data->cst), isl_val_copy(data->v));
83 shift = isl_val_floor(shift);
84 is_zero = isl_val_is_zero(shift);
85 if (is_zero < 0 || is_zero) {
86 isl_val_free(shift);
87 return is_zero < 0 ? -1 : 0;
89 shift = isl_val_mul(shift, isl_val_copy(d));
90 shifted = isl_aff_copy(aff);
91 shifted = isl_aff_add_constant_val(shifted, shift);
92 non_neg = isl_ast_build_aff_is_nonneg(data->build, shifted);
93 isl_aff_free(shifted);
95 return non_neg;
98 /* Given the numerator "aff' of the argument of an integer division
99 * with denominator "d", steal part of the constant term of
100 * the expression in which the integer division appears to make it
101 * non-negative over data->build->domain.
103 * In particular, the outer expression is of the form
105 * v * floor(aff/d) + cst
107 * We know that "aff" itself may attain negative values,
108 * but that aff + d*floor(cst/v) is non-negative.
109 * Find the minimal positive value that we need to add to "aff"
110 * to make it positive and adjust data->cst accordingly.
111 * That is, compute the minimal value "m" of "aff" over
112 * data->build->domain and take
114 * s = ceil(m/d)
116 * such that
118 * aff + d * s >= 0
120 * and rewrite the expression to
122 * v * floor((aff + s*d)/d) + (cst - v*s)
124 static __isl_give isl_aff *steal_from_cst(__isl_take isl_aff *aff,
125 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
127 isl_set *domain;
128 isl_val *shift, *t;
130 domain = isl_ast_build_get_domain(data->build);
131 shift = isl_set_min_val(domain, aff);
132 isl_set_free(domain);
134 shift = isl_val_neg(shift);
135 shift = isl_val_div(shift, isl_val_copy(d));
136 shift = isl_val_ceil(shift);
138 t = isl_val_copy(shift);
139 t = isl_val_mul(t, isl_val_copy(data->v));
140 data->cst = isl_val_sub(data->cst, t);
142 shift = isl_val_mul(shift, isl_val_copy(d));
143 return isl_aff_add_constant_val(aff, shift);
146 /* Create an isl_ast_expr evaluating the div at position "pos" in "ls".
147 * The result is simplified in terms of data->build->domain.
148 * This function may change (the sign of) data->v.
150 * "ls" is known to be non-NULL.
152 * Let the div be of the form floor(e/d).
153 * If the ast_build_prefer_pdiv option is set then we check if "e"
154 * is non-negative, so that we can generate
156 * (pdiv_q, expr(e), expr(d))
158 * instead of
160 * (fdiv_q, expr(e), expr(d))
162 * If the ast_build_prefer_pdiv option is set and
163 * if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
164 * If so, we can rewrite
166 * floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
168 * and still use pdiv_q, while changing the sign of data->v.
170 * Otherwise, we check if
172 * e + d*floor(cst/v)
174 * is non-negative and if so, replace floor(e/d) by
176 * floor((e + s*d)/d) - s
178 * with s the minimal shift that makes the argument non-negative.
180 static __isl_give isl_ast_expr *var_div(struct isl_ast_add_term_data *data,
181 __isl_keep isl_local_space *ls, int pos)
183 isl_ctx *ctx = isl_local_space_get_ctx(ls);
184 isl_aff *aff;
185 isl_ast_expr *num, *den;
186 isl_val *d;
187 enum isl_ast_op_type type;
189 aff = isl_local_space_get_div(ls, pos);
190 d = isl_aff_get_denominator_val(aff);
191 aff = isl_aff_scale_val(aff, isl_val_copy(d));
192 den = isl_ast_expr_from_val(isl_val_copy(d));
194 type = isl_ast_op_fdiv_q;
195 if (isl_options_get_ast_build_prefer_pdiv(ctx)) {
196 int non_neg = isl_ast_build_aff_is_nonneg(data->build, aff);
197 if (non_neg >= 0 && !non_neg) {
198 isl_aff *opp = oppose_div_arg(isl_aff_copy(aff),
199 isl_val_copy(d));
200 non_neg = isl_ast_build_aff_is_nonneg(data->build, opp);
201 if (non_neg >= 0 && non_neg) {
202 data->v = isl_val_neg(data->v);
203 isl_aff_free(aff);
204 aff = opp;
205 } else
206 isl_aff_free(opp);
208 if (non_neg >= 0 && !non_neg) {
209 non_neg = is_non_neg_after_stealing(aff, d, data);
210 if (non_neg >= 0 && non_neg)
211 aff = steal_from_cst(aff, d, data);
213 if (non_neg < 0)
214 aff = isl_aff_free(aff);
215 else if (non_neg)
216 type = isl_ast_op_pdiv_q;
219 isl_val_free(d);
220 num = isl_ast_expr_from_aff(aff, data->build);
221 return isl_ast_expr_alloc_binary(type, num, den);
224 /* Create an isl_ast_expr evaluating the specified dimension of "ls".
225 * The result is simplified in terms of data->build->domain.
226 * This function may change (the sign of) data->v.
228 * The isl_ast_expr is constructed based on the type of the dimension.
229 * - divs are constructed by var_div
230 * - set variables are constructed from the iterator isl_ids in data->build
231 * - parameters are constructed from the isl_ids in "ls"
233 static __isl_give isl_ast_expr *var(struct isl_ast_add_term_data *data,
234 __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos)
236 isl_ctx *ctx = isl_local_space_get_ctx(ls);
237 isl_id *id;
239 if (type == isl_dim_div)
240 return var_div(data, ls, pos);
242 if (type == isl_dim_set) {
243 id = isl_ast_build_get_iterator_id(data->build, pos);
244 return isl_ast_expr_from_id(id);
247 if (!isl_local_space_has_dim_id(ls, type, pos))
248 isl_die(ctx, isl_error_internal, "unnamed dimension",
249 return NULL);
250 id = isl_local_space_get_dim_id(ls, type, pos);
251 return isl_ast_expr_from_id(id);
254 /* Does "expr" represent the zero integer?
256 static int ast_expr_is_zero(__isl_keep isl_ast_expr *expr)
258 if (!expr)
259 return -1;
260 if (expr->type != isl_ast_expr_int)
261 return 0;
262 return isl_val_is_zero(expr->u.v);
265 /* Create an expression representing the sum of "expr1" and "expr2",
266 * provided neither of the two expressions is identically zero.
268 static __isl_give isl_ast_expr *ast_expr_add(__isl_take isl_ast_expr *expr1,
269 __isl_take isl_ast_expr *expr2)
271 if (!expr1 || !expr2)
272 goto error;
274 if (ast_expr_is_zero(expr1)) {
275 isl_ast_expr_free(expr1);
276 return expr2;
279 if (ast_expr_is_zero(expr2)) {
280 isl_ast_expr_free(expr2);
281 return expr1;
284 return isl_ast_expr_add(expr1, expr2);
285 error:
286 isl_ast_expr_free(expr1);
287 isl_ast_expr_free(expr2);
288 return NULL;
291 /* Subtract expr2 from expr1.
293 * If expr2 is zero, we simply return expr1.
294 * If expr1 is zero, we return
296 * (isl_ast_op_minus, expr2)
298 * Otherwise, we return
300 * (isl_ast_op_sub, expr1, expr2)
302 static __isl_give isl_ast_expr *ast_expr_sub(__isl_take isl_ast_expr *expr1,
303 __isl_take isl_ast_expr *expr2)
305 if (!expr1 || !expr2)
306 goto error;
308 if (ast_expr_is_zero(expr2)) {
309 isl_ast_expr_free(expr2);
310 return expr1;
313 if (ast_expr_is_zero(expr1)) {
314 isl_ast_expr_free(expr1);
315 return isl_ast_expr_neg(expr2);
318 return isl_ast_expr_sub(expr1, expr2);
319 error:
320 isl_ast_expr_free(expr1);
321 isl_ast_expr_free(expr2);
322 return NULL;
325 /* Return an isl_ast_expr that represents
327 * v * (aff mod d)
329 * v is assumed to be non-negative.
330 * The result is simplified in terms of build->domain.
332 static __isl_give isl_ast_expr *isl_ast_expr_mod(__isl_keep isl_val *v,
333 __isl_keep isl_aff *aff, __isl_keep isl_val *d,
334 __isl_keep isl_ast_build *build)
336 isl_ast_expr *expr;
337 isl_ast_expr *c;
339 if (!aff)
340 return NULL;
342 expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
344 c = isl_ast_expr_from_val(isl_val_copy(d));
345 expr = isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr, c);
347 if (!isl_val_is_one(v)) {
348 c = isl_ast_expr_from_val(isl_val_copy(v));
349 expr = isl_ast_expr_mul(c, expr);
352 return expr;
355 /* Create an isl_ast_expr that scales "expr" by "v".
357 * If v is 1, we simply return expr.
358 * If v is -1, we return
360 * (isl_ast_op_minus, expr)
362 * Otherwise, we return
364 * (isl_ast_op_mul, expr(v), expr)
366 static __isl_give isl_ast_expr *scale(__isl_take isl_ast_expr *expr,
367 __isl_take isl_val *v)
369 isl_ast_expr *c;
371 if (!expr || !v)
372 goto error;
373 if (isl_val_is_one(v)) {
374 isl_val_free(v);
375 return expr;
378 if (isl_val_is_negone(v)) {
379 isl_val_free(v);
380 expr = isl_ast_expr_neg(expr);
381 } else {
382 c = isl_ast_expr_from_val(v);
383 expr = isl_ast_expr_mul(c, expr);
386 return expr;
387 error:
388 isl_val_free(v);
389 isl_ast_expr_free(expr);
390 return NULL;
393 /* Add an expression for "*v" times the specified dimension of "ls"
394 * to expr.
395 * If the dimension is an integer division, then this function
396 * may modify data->cst in order to make the numerator non-negative.
397 * The result is simplified in terms of data->build->domain.
399 * Let e be the expression for the specified dimension,
400 * multiplied by the absolute value of "*v".
401 * If "*v" is negative, we create
403 * (isl_ast_op_sub, expr, e)
405 * except when expr is trivially zero, in which case we create
407 * (isl_ast_op_minus, e)
409 * instead.
411 * If "*v" is positive, we simply create
413 * (isl_ast_op_add, expr, e)
416 static __isl_give isl_ast_expr *isl_ast_expr_add_term(
417 __isl_take isl_ast_expr *expr,
418 __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos,
419 __isl_take isl_val *v, struct isl_ast_add_term_data *data)
421 isl_ast_expr *term;
423 if (!expr)
424 return NULL;
426 data->v = v;
427 term = var(data, ls, type, pos);
428 v = data->v;
430 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
431 v = isl_val_neg(v);
432 term = scale(term, v);
433 return ast_expr_sub(expr, term);
434 } else {
435 term = scale(term, v);
436 return ast_expr_add(expr, term);
440 /* Add an expression for "v" to expr.
442 static __isl_give isl_ast_expr *isl_ast_expr_add_int(
443 __isl_take isl_ast_expr *expr, __isl_take isl_val *v)
445 isl_ast_expr *expr_int;
447 if (!expr || !v)
448 goto error;
450 if (isl_val_is_zero(v)) {
451 isl_val_free(v);
452 return expr;
455 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
456 v = isl_val_neg(v);
457 expr_int = isl_ast_expr_from_val(v);
458 return ast_expr_sub(expr, expr_int);
459 } else {
460 expr_int = isl_ast_expr_from_val(v);
461 return ast_expr_add(expr, expr_int);
463 error:
464 isl_ast_expr_free(expr);
465 isl_val_free(v);
466 return NULL;
469 /* Internal data structure used inside extract_modulos.
471 * If any modulo expressions are detected in "aff", then the
472 * expression is removed from "aff" and added to either "pos" or "neg"
473 * depending on the sign of the coefficient of the modulo expression
474 * inside "aff".
476 * "add" is an expression that needs to be added to "aff" at the end of
477 * the computation. It is NULL as long as no modulos have been extracted.
479 * "i" is the position in "aff" of the div under investigation
480 * "v" is the coefficient in "aff" of the div
481 * "div" is the argument of the div, with the denominator removed
482 * "d" is the original denominator of the argument of the div
484 * "nonneg" is an affine expression that is non-negative over "build"
485 * and that can be used to extract a modulo expression from "div".
486 * In particular, if "sign" is 1, then the coefficients of "nonneg"
487 * are equal to those of "div" modulo "d". If "sign" is -1, then
488 * the coefficients of "nonneg" are opposite to those of "div" modulo "d".
489 * If "sign" is 0, then no such affine expression has been found (yet).
491 struct isl_extract_mod_data {
492 isl_ast_build *build;
493 isl_aff *aff;
495 isl_ast_expr *pos;
496 isl_ast_expr *neg;
498 isl_aff *add;
500 int i;
501 isl_val *v;
502 isl_val *d;
503 isl_aff *div;
505 isl_aff *nonneg;
506 int sign;
509 /* Given that data->v * div_i in data->aff is equal to
511 * f * (term - (arg mod d))
513 * with data->d * f = data->v, add
515 * f * term
517 * to data->add and
519 * abs(f) * (arg mod d)
521 * to data->neg or data->pos depending on the sign of -f.
523 static int extract_term_and_mod(struct isl_extract_mod_data *data,
524 __isl_take isl_aff *term, __isl_take isl_aff *arg)
526 isl_ast_expr *expr;
527 int s;
529 data->v = isl_val_div(data->v, isl_val_copy(data->d));
530 s = isl_val_sgn(data->v);
531 data->v = isl_val_abs(data->v);
532 expr = isl_ast_expr_mod(data->v, arg, data->d, data->build);
533 isl_aff_free(arg);
534 if (s > 0)
535 data->neg = ast_expr_add(data->neg, expr);
536 else
537 data->pos = ast_expr_add(data->pos, expr);
538 data->aff = isl_aff_set_coefficient_si(data->aff,
539 isl_dim_div, data->i, 0);
540 if (s < 0)
541 data->v = isl_val_neg(data->v);
542 term = isl_aff_scale_val(data->div, isl_val_copy(data->v));
544 if (!data->add)
545 data->add = term;
546 else
547 data->add = isl_aff_add(data->add, term);
548 if (!data->add)
549 return -1;
551 return 0;
554 /* Given that data->v * div_i in data->aff is of the form
556 * f * d * floor(div/d)
558 * with div nonnegative on data->build, rewrite it as
560 * f * (div - (div mod d)) = f * div - f * (div mod d)
562 * and add
564 * f * div
566 * to data->add and
568 * abs(f) * (div mod d)
570 * to data->neg or data->pos depending on the sign of -f.
572 static int extract_mod(struct isl_extract_mod_data *data)
574 return extract_term_and_mod(data, isl_aff_copy(data->div),
575 isl_aff_copy(data->div));
578 /* Given that data->v * div_i in data->aff is of the form
580 * f * d * floor(div/d) (1)
582 * check if div is non-negative on data->build and, if so,
583 * extract the corresponding modulo from data->aff.
584 * If not, then check if
586 * -div + d - 1
588 * is non-negative on data->build. If so, replace (1) by
590 * -f * d * floor((-div + d - 1)/d)
592 * and extract the corresponding modulo from data->aff.
594 * This function may modify data->div.
596 static int extract_nonneg_mod(struct isl_extract_mod_data *data)
598 int mod;
600 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
601 if (mod < 0)
602 goto error;
603 if (mod)
604 return extract_mod(data);
606 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
607 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
608 if (mod < 0)
609 goto error;
610 if (mod) {
611 data->v = isl_val_neg(data->v);
612 return extract_mod(data);
615 return 0;
616 error:
617 data->aff = isl_aff_free(data->aff);
618 return -1;
621 /* Is the affine expression of constraint "c" "simpler" than data->nonneg
622 * for use in extracting a modulo expression?
624 * We currently only consider the constant term of the affine expression.
625 * In particular, we prefer the affine expression with the smallest constant
626 * term.
627 * This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
628 * then we would pick x >= 0
630 * More detailed heuristics could be used if it turns out that there is a need.
632 static int mod_constraint_is_simpler(struct isl_extract_mod_data *data,
633 __isl_keep isl_constraint *c)
635 isl_val *v1, *v2;
636 int simpler;
638 if (!data->nonneg)
639 return 1;
641 v1 = isl_val_abs(isl_constraint_get_constant_val(c));
642 v2 = isl_val_abs(isl_aff_get_constant_val(data->nonneg));
643 simpler = isl_val_lt(v1, v2);
644 isl_val_free(v1);
645 isl_val_free(v2);
647 return simpler;
650 /* Check if the coefficients of "c" are either equal or opposite to those
651 * of data->div modulo data->d. If so, and if "c" is "simpler" than
652 * data->nonneg, then replace data->nonneg by the affine expression of "c"
653 * and set data->sign accordingly.
655 * Both "c" and data->div are assumed not to involve any integer divisions.
657 * Before we start the actual comparison, we first quickly check if
658 * "c" and data->div have the same non-zero coefficients.
659 * If not, then we assume that "c" is not of the desired form.
660 * Note that while the coefficients of data->div can be reasonably expected
661 * not to involve any coefficients that are multiples of d, "c" may
662 * very well involve such coefficients. This means that we may actually
663 * miss some cases.
665 static isl_stat check_parallel_or_opposite(__isl_take isl_constraint *c,
666 void *user)
668 struct isl_extract_mod_data *data = user;
669 enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
670 enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
671 int i, t;
672 int n[2];
673 int parallel = 1, opposite = 1;
675 for (t = 0; t < 2; ++t) {
676 n[t] = isl_constraint_dim(c, c_type[t]);
677 for (i = 0; i < n[t]; ++i) {
678 int a, b;
680 a = isl_constraint_involves_dims(c, c_type[t], i, 1);
681 b = isl_aff_involves_dims(data->div, a_type[t], i, 1);
682 if (a != b)
683 parallel = opposite = 0;
687 for (t = 0; t < 2; ++t) {
688 for (i = 0; i < n[t]; ++i) {
689 isl_val *v1, *v2;
691 if (!parallel && !opposite)
692 break;
693 v1 = isl_constraint_get_coefficient_val(c,
694 c_type[t], i);
695 v2 = isl_aff_get_coefficient_val(data->div,
696 a_type[t], i);
697 if (parallel) {
698 v1 = isl_val_sub(v1, isl_val_copy(v2));
699 parallel = isl_val_is_divisible_by(v1, data->d);
700 v1 = isl_val_add(v1, isl_val_copy(v2));
702 if (opposite) {
703 v1 = isl_val_add(v1, isl_val_copy(v2));
704 opposite = isl_val_is_divisible_by(v1, data->d);
706 isl_val_free(v1);
707 isl_val_free(v2);
711 if ((parallel || opposite) && mod_constraint_is_simpler(data, c)) {
712 isl_aff_free(data->nonneg);
713 data->nonneg = isl_constraint_get_aff(c);
714 data->sign = parallel ? 1 : -1;
717 isl_constraint_free(c);
719 if (data->sign != 0 && data->nonneg == NULL)
720 return isl_stat_error;
722 return isl_stat_ok;
725 /* Given that data->v * div_i in data->aff is of the form
727 * f * d * floor(div/d) (1)
729 * see if we can find an expression div' that is non-negative over data->build
730 * and that is related to div through
732 * div' = div + d * e
734 * or
736 * div' = -div + d - 1 + d * e
738 * with e some affine expression.
739 * If so, we write (1) as
741 * f * div + f * (div' mod d)
743 * or
745 * -f * (-div + d - 1) - f * (div' mod d)
747 * exploiting (in the second case) the fact that
749 * f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
752 * We first try to find an appropriate expression for div'
753 * from the constraints of data->build->domain (which is therefore
754 * guaranteed to be non-negative on data->build), where we remove
755 * any integer divisions from the constraints and skip this step
756 * if "div" itself involves any integer divisions.
757 * If we cannot find an appropriate expression this way, then
758 * we pass control to extract_nonneg_mod where check
759 * if div or "-div + d -1" themselves happen to be
760 * non-negative on data->build.
762 * While looking for an appropriate constraint in data->build->domain,
763 * we ignore the constant term, so after finding such a constraint,
764 * we still need to fix up the constant term.
765 * In particular, if a is the constant term of "div"
766 * (or d - 1 - the constant term of "div" if data->sign < 0)
767 * and b is the constant term of the constraint, then we need to find
768 * a non-negative constant c such that
770 * b + c \equiv a mod d
772 * We therefore take
774 * c = (a - b) mod d
776 * and add it to b to obtain the constant term of div'.
777 * If this constant term is "too negative", then we add an appropriate
778 * multiple of d to make it positive.
781 * Note that the above is a only a very simple heuristic for finding an
782 * appropriate expression. We could try a bit harder by also considering
783 * sums of constraints that involve disjoint sets of variables or
784 * we could consider arbitrary linear combinations of constraints,
785 * although that could potentially be much more expensive as it involves
786 * the solution of an LP problem.
788 * In particular, if v_i is a column vector representing constraint i,
789 * w represents div and e_i is the i-th unit vector, then we are looking
790 * for a solution of the constraints
792 * \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
794 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
795 * If we are not just interested in a non-negative expression, but
796 * also in one with a minimal range, then we don't just want
797 * c = \sum_i lambda_i v_i to be non-negative over the domain,
798 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
799 * that we want to minimize and we now also have to take into account
800 * the constant terms of the constraints.
801 * Alternatively, we could first compute the dual of the domain
802 * and plug in the constraints on the coefficients.
804 static int try_extract_mod(struct isl_extract_mod_data *data)
806 isl_basic_set *hull;
807 isl_val *v1, *v2;
808 int r, n;
810 if (!data->build)
811 goto error;
813 n = isl_aff_dim(data->div, isl_dim_div);
815 if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
816 return extract_nonneg_mod(data);
818 hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
819 hull = isl_basic_set_remove_divs(hull);
820 data->sign = 0;
821 data->nonneg = NULL;
822 r = isl_basic_set_foreach_constraint(hull, &check_parallel_or_opposite,
823 data);
824 isl_basic_set_free(hull);
826 if (!data->sign || r < 0) {
827 isl_aff_free(data->nonneg);
828 if (r < 0)
829 goto error;
830 return extract_nonneg_mod(data);
833 v1 = isl_aff_get_constant_val(data->div);
834 v2 = isl_aff_get_constant_val(data->nonneg);
835 if (data->sign < 0) {
836 v1 = isl_val_neg(v1);
837 v1 = isl_val_add(v1, isl_val_copy(data->d));
838 v1 = isl_val_sub_ui(v1, 1);
840 v1 = isl_val_sub(v1, isl_val_copy(v2));
841 v1 = isl_val_mod(v1, isl_val_copy(data->d));
842 v1 = isl_val_add(v1, v2);
843 v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
844 v2 = isl_val_ceil(v2);
845 if (isl_val_is_neg(v2)) {
846 v2 = isl_val_mul(v2, isl_val_copy(data->d));
847 v1 = isl_val_sub(v1, isl_val_copy(v2));
849 data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
850 isl_val_free(v2);
852 if (data->sign < 0) {
853 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
854 data->v = isl_val_neg(data->v);
857 return extract_term_and_mod(data,
858 isl_aff_copy(data->div), data->nonneg);
859 error:
860 data->aff = isl_aff_free(data->aff);
861 return -1;
864 /* Check if "data->aff" involves any (implicit) modulo computations based
865 * on div "data->i".
866 * If so, remove them from aff and add expressions corresponding
867 * to those modulo computations to data->pos and/or data->neg.
869 * "aff" is assumed to be an integer affine expression.
871 * In particular, check if (v * div_j) is of the form
873 * f * m * floor(a / m)
875 * and, if so, rewrite it as
877 * f * (a - (a mod m)) = f * a - f * (a mod m)
879 * and extract out -f * (a mod m).
880 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
881 * If f < 0, we add ((-f) * (a mod m)) to *pos.
883 * Note that in order to represent "a mod m" as
885 * (isl_ast_op_pdiv_r, a, m)
887 * we need to make sure that a is non-negative.
888 * If not, we check if "-a + m - 1" is non-negative.
889 * If so, we can rewrite
891 * floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
893 * and still extract a modulo.
895 static int extract_modulo(struct isl_extract_mod_data *data)
897 data->div = isl_aff_get_div(data->aff, data->i);
898 data->d = isl_aff_get_denominator_val(data->div);
899 if (isl_val_is_divisible_by(data->v, data->d)) {
900 data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
901 if (try_extract_mod(data) < 0)
902 data->aff = isl_aff_free(data->aff);
904 isl_aff_free(data->div);
905 isl_val_free(data->d);
906 return 0;
909 /* Check if "aff" involves any (implicit) modulo computations.
910 * If so, remove them from aff and add expressions corresponding
911 * to those modulo computations to *pos and/or *neg.
912 * We only do this if the option ast_build_prefer_pdiv is set.
914 * "aff" is assumed to be an integer affine expression.
916 * A modulo expression is of the form
918 * a mod m = a - m * floor(a / m)
920 * To detect them in aff, we look for terms of the form
922 * f * m * floor(a / m)
924 * rewrite them as
926 * f * (a - (a mod m)) = f * a - f * (a mod m)
928 * and extract out -f * (a mod m).
929 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
930 * If f < 0, we add ((-f) * (a mod m)) to *pos.
932 static __isl_give isl_aff *extract_modulos(__isl_take isl_aff *aff,
933 __isl_keep isl_ast_expr **pos, __isl_keep isl_ast_expr **neg,
934 __isl_keep isl_ast_build *build)
936 struct isl_extract_mod_data data = { build, aff, *pos, *neg };
937 isl_ctx *ctx;
938 int n;
940 if (!aff)
941 return NULL;
943 ctx = isl_aff_get_ctx(aff);
944 if (!isl_options_get_ast_build_prefer_pdiv(ctx))
945 return aff;
947 n = isl_aff_dim(data.aff, isl_dim_div);
948 for (data.i = 0; data.i < n; ++data.i) {
949 data.v = isl_aff_get_coefficient_val(data.aff,
950 isl_dim_div, data.i);
951 if (!data.v)
952 return isl_aff_free(aff);
953 if (isl_val_is_zero(data.v) ||
954 isl_val_is_one(data.v) || isl_val_is_negone(data.v)) {
955 isl_val_free(data.v);
956 continue;
958 if (extract_modulo(&data) < 0)
959 data.aff = isl_aff_free(data.aff);
960 isl_val_free(data.v);
961 if (!data.aff)
962 break;
965 if (data.add)
966 data.aff = isl_aff_add(data.aff, data.add);
968 *pos = data.pos;
969 *neg = data.neg;
970 return data.aff;
973 /* Check if aff involves any non-integer coefficients.
974 * If so, split aff into
976 * aff = aff1 + (aff2 / d)
978 * with both aff1 and aff2 having only integer coefficients.
979 * Return aff1 and add (aff2 / d) to *expr.
981 static __isl_give isl_aff *extract_rational(__isl_take isl_aff *aff,
982 __isl_keep isl_ast_expr **expr, __isl_keep isl_ast_build *build)
984 int i, j, n;
985 isl_aff *rat = NULL;
986 isl_local_space *ls = NULL;
987 isl_ast_expr *rat_expr;
988 isl_val *v, *d;
989 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
990 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
992 if (!aff)
993 return NULL;
994 d = isl_aff_get_denominator_val(aff);
995 if (!d)
996 goto error;
997 if (isl_val_is_one(d)) {
998 isl_val_free(d);
999 return aff;
1002 aff = isl_aff_scale_val(aff, isl_val_copy(d));
1004 ls = isl_aff_get_domain_local_space(aff);
1005 rat = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1007 for (i = 0; i < 3; ++i) {
1008 n = isl_aff_dim(aff, t[i]);
1009 for (j = 0; j < n; ++j) {
1010 isl_aff *rat_j;
1012 v = isl_aff_get_coefficient_val(aff, t[i], j);
1013 if (!v)
1014 goto error;
1015 if (isl_val_is_divisible_by(v, d)) {
1016 isl_val_free(v);
1017 continue;
1019 rat_j = isl_aff_var_on_domain(isl_local_space_copy(ls),
1020 l[i], j);
1021 rat_j = isl_aff_scale_val(rat_j, v);
1022 rat = isl_aff_add(rat, rat_j);
1026 v = isl_aff_get_constant_val(aff);
1027 if (isl_val_is_divisible_by(v, d)) {
1028 isl_val_free(v);
1029 } else {
1030 isl_aff *rat_0;
1032 rat_0 = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
1033 rat = isl_aff_add(rat, rat_0);
1036 isl_local_space_free(ls);
1038 aff = isl_aff_sub(aff, isl_aff_copy(rat));
1039 aff = isl_aff_scale_down_val(aff, isl_val_copy(d));
1041 rat_expr = isl_ast_expr_from_aff(rat, build);
1042 rat_expr = isl_ast_expr_div(rat_expr, isl_ast_expr_from_val(d));
1043 *expr = ast_expr_add(*expr, rat_expr);
1045 return aff;
1046 error:
1047 isl_aff_free(rat);
1048 isl_local_space_free(ls);
1049 isl_aff_free(aff);
1050 isl_val_free(d);
1051 return NULL;
1054 /* Construct an isl_ast_expr that evaluates the affine expression "aff",
1055 * The result is simplified in terms of build->domain.
1057 * We first extract hidden modulo computations from the affine expression
1058 * and then add terms for each variable with a non-zero coefficient.
1059 * Finally, if the affine expression has a non-trivial denominator,
1060 * we divide the resulting isl_ast_expr by this denominator.
1062 __isl_give isl_ast_expr *isl_ast_expr_from_aff(__isl_take isl_aff *aff,
1063 __isl_keep isl_ast_build *build)
1065 int i, j;
1066 int n;
1067 isl_val *v;
1068 isl_ctx *ctx = isl_aff_get_ctx(aff);
1069 isl_ast_expr *expr, *expr_neg;
1070 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1071 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1072 isl_local_space *ls;
1073 struct isl_ast_add_term_data data;
1075 if (!aff)
1076 return NULL;
1078 expr = isl_ast_expr_alloc_int_si(ctx, 0);
1079 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1081 aff = extract_rational(aff, &expr, build);
1083 aff = extract_modulos(aff, &expr, &expr_neg, build);
1084 expr = ast_expr_sub(expr, expr_neg);
1086 ls = isl_aff_get_domain_local_space(aff);
1088 data.build = build;
1089 data.cst = isl_aff_get_constant_val(aff);
1090 for (i = 0; i < 3; ++i) {
1091 n = isl_aff_dim(aff, t[i]);
1092 for (j = 0; j < n; ++j) {
1093 v = isl_aff_get_coefficient_val(aff, t[i], j);
1094 if (!v)
1095 expr = isl_ast_expr_free(expr);
1096 if (isl_val_is_zero(v)) {
1097 isl_val_free(v);
1098 continue;
1100 expr = isl_ast_expr_add_term(expr,
1101 ls, l[i], j, v, &data);
1105 expr = isl_ast_expr_add_int(expr, data.cst);
1107 isl_local_space_free(ls);
1108 isl_aff_free(aff);
1109 return expr;
1112 /* Add terms to "expr" for each variable in "aff" with a coefficient
1113 * with sign equal to "sign".
1114 * The result is simplified in terms of data->build->domain.
1116 static __isl_give isl_ast_expr *add_signed_terms(__isl_take isl_ast_expr *expr,
1117 __isl_keep isl_aff *aff, int sign, struct isl_ast_add_term_data *data)
1119 int i, j;
1120 isl_val *v;
1121 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1122 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1123 isl_local_space *ls;
1125 ls = isl_aff_get_domain_local_space(aff);
1127 for (i = 0; i < 3; ++i) {
1128 int n = isl_aff_dim(aff, t[i]);
1129 for (j = 0; j < n; ++j) {
1130 v = isl_aff_get_coefficient_val(aff, t[i], j);
1131 if (sign * isl_val_sgn(v) <= 0) {
1132 isl_val_free(v);
1133 continue;
1135 v = isl_val_abs(v);
1136 expr = isl_ast_expr_add_term(expr,
1137 ls, l[i], j, v, data);
1141 isl_local_space_free(ls);
1143 return expr;
1146 /* Should the constant term "v" be considered positive?
1148 * A positive constant will be added to "pos" by the caller,
1149 * while a negative constant will be added to "neg".
1150 * If either "pos" or "neg" is exactly zero, then we prefer
1151 * to add the constant "v" to that side, irrespective of the sign of "v".
1152 * This results in slightly shorter expressions and may reduce the risk
1153 * of overflows.
1155 static int constant_is_considered_positive(__isl_keep isl_val *v,
1156 __isl_keep isl_ast_expr *pos, __isl_keep isl_ast_expr *neg)
1158 if (ast_expr_is_zero(pos))
1159 return 1;
1160 if (ast_expr_is_zero(neg))
1161 return 0;
1162 return isl_val_is_pos(v);
1165 /* Check if the equality
1167 * aff = 0
1169 * represents a stride constraint on the integer division "pos".
1171 * In particular, if the integer division "pos" is equal to
1173 * floor(e/d)
1175 * then check if aff is equal to
1177 * e - d floor(e/d)
1179 * or its opposite.
1181 * If so, the equality is exactly
1183 * e mod d = 0
1185 * Note that in principle we could also accept
1187 * e - d floor(e'/d)
1189 * where e and e' differ by a constant.
1191 static int is_stride_constraint(__isl_keep isl_aff *aff, int pos)
1193 isl_aff *div;
1194 isl_val *c, *d;
1195 int eq;
1197 div = isl_aff_get_div(aff, pos);
1198 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1199 d = isl_aff_get_denominator_val(div);
1200 eq = isl_val_abs_eq(c, d);
1201 if (eq >= 0 && eq) {
1202 aff = isl_aff_copy(aff);
1203 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1204 div = isl_aff_scale_val(div, d);
1205 if (isl_val_is_pos(c))
1206 div = isl_aff_neg(div);
1207 eq = isl_aff_plain_is_equal(div, aff);
1208 isl_aff_free(aff);
1209 } else
1210 isl_val_free(d);
1211 isl_val_free(c);
1212 isl_aff_free(div);
1214 return eq;
1217 /* Are all coefficients of "aff" (zero or) negative?
1219 static int all_negative_coefficients(__isl_keep isl_aff *aff)
1221 int i, n;
1223 if (!aff)
1224 return 0;
1226 n = isl_aff_dim(aff, isl_dim_param);
1227 for (i = 0; i < n; ++i)
1228 if (isl_aff_coefficient_sgn(aff, isl_dim_param, i) > 0)
1229 return 0;
1231 n = isl_aff_dim(aff, isl_dim_in);
1232 for (i = 0; i < n; ++i)
1233 if (isl_aff_coefficient_sgn(aff, isl_dim_in, i) > 0)
1234 return 0;
1236 return 1;
1239 /* Give an equality of the form
1241 * aff = e - d floor(e/d) = 0
1243 * or
1245 * aff = -e + d floor(e/d) = 0
1247 * with the integer division "pos" equal to floor(e/d),
1248 * construct the AST expression
1250 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1252 * If e only has negative coefficients, then construct
1254 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(-e), expr(d)), expr(0))
1256 * instead.
1258 static __isl_give isl_ast_expr *extract_stride_constraint(
1259 __isl_take isl_aff *aff, int pos, __isl_keep isl_ast_build *build)
1261 isl_ctx *ctx;
1262 isl_val *c;
1263 isl_ast_expr *expr, *cst;
1265 if (!aff)
1266 return NULL;
1268 ctx = isl_aff_get_ctx(aff);
1270 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1271 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1273 if (all_negative_coefficients(aff))
1274 aff = isl_aff_neg(aff);
1276 cst = isl_ast_expr_from_val(isl_val_abs(c));
1277 expr = isl_ast_expr_from_aff(aff, build);
1279 expr = isl_ast_expr_alloc_binary(isl_ast_op_zdiv_r, expr, cst);
1280 cst = isl_ast_expr_alloc_int_si(ctx, 0);
1281 expr = isl_ast_expr_alloc_binary(isl_ast_op_eq, expr, cst);
1283 return expr;
1286 /* Construct an isl_ast_expr that evaluates the condition "constraint",
1287 * The result is simplified in terms of build->domain.
1289 * We first check if the constraint is an equality of the form
1291 * e - d floor(e/d) = 0
1293 * i.e.,
1295 * e mod d = 0
1297 * If so, we convert it to
1299 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1301 * Otherwise, let the constraint by either "a >= 0" or "a == 0".
1302 * We first extract hidden modulo computations from "a"
1303 * and then collect all the terms with a positive coefficient in cons_pos
1304 * and the terms with a negative coefficient in cons_neg.
1306 * The result is then of the form
1308 * (isl_ast_op_ge, expr(pos), expr(-neg)))
1310 * or
1312 * (isl_ast_op_eq, expr(pos), expr(-neg)))
1314 * However, if the first expression is an integer constant (and the second
1315 * is not), then we swap the two expressions. This ensures that we construct,
1316 * e.g., "i <= 5" rather than "5 >= i".
1318 * Furthermore, is there are no terms with positive coefficients (or no terms
1319 * with negative coefficients), then the constant term is added to "pos"
1320 * (or "neg"), ignoring the sign of the constant term.
1322 static __isl_give isl_ast_expr *isl_ast_expr_from_constraint(
1323 __isl_take isl_constraint *constraint, __isl_keep isl_ast_build *build)
1325 int i, n;
1326 isl_ctx *ctx;
1327 isl_ast_expr *expr_pos;
1328 isl_ast_expr *expr_neg;
1329 isl_ast_expr *expr;
1330 isl_aff *aff;
1331 int eq;
1332 enum isl_ast_op_type type;
1333 struct isl_ast_add_term_data data;
1335 if (!constraint)
1336 return NULL;
1338 aff = isl_constraint_get_aff(constraint);
1339 eq = isl_constraint_is_equality(constraint);
1340 isl_constraint_free(constraint);
1342 n = isl_aff_dim(aff, isl_dim_div);
1343 if (eq && n > 0)
1344 for (i = 0; i < n; ++i) {
1345 int is_stride;
1346 is_stride = is_stride_constraint(aff, i);
1347 if (is_stride < 0)
1348 goto error;
1349 if (is_stride)
1350 return extract_stride_constraint(aff, i, build);
1353 ctx = isl_aff_get_ctx(aff);
1354 expr_pos = isl_ast_expr_alloc_int_si(ctx, 0);
1355 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1357 aff = extract_modulos(aff, &expr_pos, &expr_neg, build);
1359 data.build = build;
1360 data.cst = isl_aff_get_constant_val(aff);
1361 expr_pos = add_signed_terms(expr_pos, aff, 1, &data);
1362 data.cst = isl_val_neg(data.cst);
1363 expr_neg = add_signed_terms(expr_neg, aff, -1, &data);
1364 data.cst = isl_val_neg(data.cst);
1366 if (constant_is_considered_positive(data.cst, expr_pos, expr_neg)) {
1367 expr_pos = isl_ast_expr_add_int(expr_pos, data.cst);
1368 } else {
1369 data.cst = isl_val_neg(data.cst);
1370 expr_neg = isl_ast_expr_add_int(expr_neg, data.cst);
1373 if (isl_ast_expr_get_type(expr_pos) == isl_ast_expr_int &&
1374 isl_ast_expr_get_type(expr_neg) != isl_ast_expr_int) {
1375 type = eq ? isl_ast_op_eq : isl_ast_op_le;
1376 expr = isl_ast_expr_alloc_binary(type, expr_neg, expr_pos);
1377 } else {
1378 type = eq ? isl_ast_op_eq : isl_ast_op_ge;
1379 expr = isl_ast_expr_alloc_binary(type, expr_pos, expr_neg);
1382 isl_aff_free(aff);
1383 return expr;
1384 error:
1385 isl_aff_free(aff);
1386 return NULL;
1389 /* Wrapper around isl_constraint_cmp_last_non_zero for use
1390 * as a callback to isl_constraint_list_sort.
1391 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1392 * apart, then use isl_constraint_plain_cmp instead.
1394 static int cmp_constraint(__isl_keep isl_constraint *a,
1395 __isl_keep isl_constraint *b, void *user)
1397 int cmp;
1399 cmp = isl_constraint_cmp_last_non_zero(a, b);
1400 if (cmp != 0)
1401 return cmp;
1402 return isl_constraint_plain_cmp(a, b);
1405 /* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1406 * The result is simplified in terms of build->domain.
1408 * If "bset" is not bounded by any constraint, then we contruct
1409 * the expression "1", i.e., "true".
1411 * Otherwise, we sort the constraints, putting constraints that involve
1412 * integer divisions after those that do not, and construct an "and"
1413 * of the ast expressions of the individual constraints.
1415 * Each constraint is added to the generated constraints of the build
1416 * after it has been converted to an AST expression so that it can be used
1417 * to simplify the following constraints. This may change the truth value
1418 * of subsequent constraints that do not satisfy the earlier constraints,
1419 * but this does not affect the outcome of the conjunction as it is
1420 * only true if all the conjuncts are true (no matter in what order
1421 * they are evaluated). In particular, the constraints that do not
1422 * involve integer divisions may serve to simplify some constraints
1423 * that do involve integer divisions.
1425 __isl_give isl_ast_expr *isl_ast_build_expr_from_basic_set(
1426 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1428 int i, n;
1429 isl_constraint *c;
1430 isl_constraint_list *list;
1431 isl_ast_expr *res;
1432 isl_set *set;
1434 list = isl_basic_set_get_constraint_list(bset);
1435 isl_basic_set_free(bset);
1436 list = isl_constraint_list_sort(list, &cmp_constraint, NULL);
1437 if (!list)
1438 return NULL;
1439 n = isl_constraint_list_n_constraint(list);
1440 if (n == 0) {
1441 isl_ctx *ctx = isl_constraint_list_get_ctx(list);
1442 isl_constraint_list_free(list);
1443 return isl_ast_expr_alloc_int_si(ctx, 1);
1446 build = isl_ast_build_copy(build);
1448 c = isl_constraint_list_get_constraint(list, 0);
1449 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1450 set = isl_set_from_basic_set(bset);
1451 res = isl_ast_expr_from_constraint(c, build);
1452 build = isl_ast_build_restrict_generated(build, set);
1454 for (i = 1; i < n; ++i) {
1455 isl_ast_expr *expr;
1457 c = isl_constraint_list_get_constraint(list, i);
1458 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1459 set = isl_set_from_basic_set(bset);
1460 expr = isl_ast_expr_from_constraint(c, build);
1461 build = isl_ast_build_restrict_generated(build, set);
1462 res = isl_ast_expr_and(res, expr);
1465 isl_constraint_list_free(list);
1466 isl_ast_build_free(build);
1467 return res;
1470 struct isl_expr_from_set_data {
1471 isl_ast_build *build;
1472 int first;
1473 isl_ast_expr *res;
1476 /* Construct an isl_ast_expr that evaluates the conditions defining "bset"
1477 * and add it to data->res.
1478 * The result is simplified in terms of data->build->domain.
1480 static isl_stat expr_from_set(__isl_take isl_basic_set *bset, void *user)
1482 struct isl_expr_from_set_data *data = user;
1483 isl_ast_expr *expr;
1485 expr = isl_ast_build_expr_from_basic_set(data->build, bset);
1486 if (data->first)
1487 data->res = expr;
1488 else
1489 data->res = isl_ast_expr_or(data->res, expr);
1491 data->first = 0;
1493 if (!data->res)
1494 return isl_stat_error;
1495 return isl_stat_ok;
1498 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1499 * The result is simplified in terms of build->domain.
1501 * If "set" is an (obviously) empty set, then return the expression "0".
1503 * "set" lives in the internal schedule space.
1505 __isl_give isl_ast_expr *isl_ast_build_expr_from_set_internal(
1506 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1508 struct isl_expr_from_set_data data = { build, 1, NULL };
1510 if (isl_set_foreach_basic_set(set, &expr_from_set, &data) < 0)
1511 data.res = isl_ast_expr_free(data.res);
1512 else if (data.first) {
1513 isl_ctx *ctx = isl_ast_build_get_ctx(build);
1514 data.res = isl_ast_expr_from_val(isl_val_zero(ctx));
1517 isl_set_free(set);
1518 return data.res;
1521 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1522 * The result is simplified in terms of build->domain.
1524 * If "set" is an (obviously) empty set, then return the expression "0".
1526 * "set" lives in the external schedule space.
1528 * The internal AST expression generation assumes that there are
1529 * no unknown divs, so make sure an explicit representation is available.
1530 * Since the set comes from the outside, it may have constraints that
1531 * are redundant with respect to the build domain. Remove them first.
1533 __isl_give isl_ast_expr *isl_ast_build_expr_from_set(
1534 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1536 if (isl_ast_build_need_schedule_map(build)) {
1537 isl_multi_aff *ma;
1538 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1539 set = isl_set_preimage_multi_aff(set, ma);
1542 set = isl_set_compute_divs(set);
1543 set = isl_ast_build_compute_gist(build, set);
1544 return isl_ast_build_expr_from_set_internal(build, set);
1547 struct isl_from_pw_aff_data {
1548 isl_ast_build *build;
1549 int n;
1550 isl_ast_expr **next;
1551 isl_set *dom;
1554 /* This function is called during the construction of an isl_ast_expr
1555 * that evaluates an isl_pw_aff.
1556 * Adjust data->next to take into account this piece.
1558 * data->n is the number of pairs of set and aff to go.
1559 * data->dom is the domain of the entire isl_pw_aff.
1561 * If this is the last pair, then data->next is set to evaluate aff
1562 * and the domain is ignored.
1563 * Otherwise, data->next is set to a select operation that selects
1564 * an isl_ast_expr corresponding to "aff" on "set" and to an expression
1565 * that will be filled in by later calls otherwise.
1567 * In both cases, the constraints of "set" are added to the generated
1568 * constraints of the build such that they can be exploited to simplify
1569 * the AST expression constructed from "aff".
1571 static isl_stat ast_expr_from_pw_aff(__isl_take isl_set *set,
1572 __isl_take isl_aff *aff, void *user)
1574 struct isl_from_pw_aff_data *data = user;
1575 isl_ctx *ctx;
1576 isl_ast_build *build;
1578 ctx = isl_set_get_ctx(set);
1579 data->n--;
1580 if (data->n == 0) {
1581 build = isl_ast_build_copy(data->build);
1582 build = isl_ast_build_restrict_generated(build, set);
1583 *data->next = isl_ast_expr_from_aff(aff, build);
1584 isl_ast_build_free(build);
1585 if (!*data->next)
1586 return isl_stat_error;
1587 } else {
1588 isl_ast_expr *ternary, *arg;
1589 isl_set *gist;
1591 ternary = isl_ast_expr_alloc_op(ctx, isl_ast_op_select, 3);
1592 gist = isl_set_gist(isl_set_copy(set), isl_set_copy(data->dom));
1593 arg = isl_ast_build_expr_from_set_internal(data->build, gist);
1594 ternary = isl_ast_expr_set_op_arg(ternary, 0, arg);
1595 build = isl_ast_build_copy(data->build);
1596 build = isl_ast_build_restrict_generated(build, set);
1597 arg = isl_ast_expr_from_aff(aff, build);
1598 isl_ast_build_free(build);
1599 ternary = isl_ast_expr_set_op_arg(ternary, 1, arg);
1600 if (!ternary)
1601 return isl_stat_error;
1603 *data->next = ternary;
1604 data->next = &ternary->u.op.args[2];
1607 return isl_stat_ok;
1610 /* Construct an isl_ast_expr that evaluates "pa".
1611 * The result is simplified in terms of build->domain.
1613 * The domain of "pa" lives in the internal schedule space.
1615 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff_internal(
1616 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
1618 struct isl_from_pw_aff_data data;
1619 isl_ast_expr *res = NULL;
1621 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
1622 pa = isl_pw_aff_coalesce(pa);
1623 if (!pa)
1624 return NULL;
1626 data.build = build;
1627 data.n = isl_pw_aff_n_piece(pa);
1628 data.next = &res;
1629 data.dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1631 if (isl_pw_aff_foreach_piece(pa, &ast_expr_from_pw_aff, &data) < 0)
1632 res = isl_ast_expr_free(res);
1633 else if (!res)
1634 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1635 "cannot handle void expression", res = NULL);
1637 isl_pw_aff_free(pa);
1638 isl_set_free(data.dom);
1639 return res;
1642 /* Construct an isl_ast_expr that evaluates "pa".
1643 * The result is simplified in terms of build->domain.
1645 * The domain of "pa" lives in the external schedule space.
1647 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff(
1648 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
1650 isl_ast_expr *expr;
1652 if (isl_ast_build_need_schedule_map(build)) {
1653 isl_multi_aff *ma;
1654 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1655 pa = isl_pw_aff_pullback_multi_aff(pa, ma);
1657 expr = isl_ast_build_expr_from_pw_aff_internal(build, pa);
1658 return expr;
1661 /* Set the ids of the input dimensions of "mpa" to the iterator ids
1662 * of "build".
1664 * The domain of "mpa" is assumed to live in the internal schedule domain.
1666 static __isl_give isl_multi_pw_aff *set_iterator_names(
1667 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
1669 int i, n;
1671 n = isl_multi_pw_aff_dim(mpa, isl_dim_in);
1672 for (i = 0; i < n; ++i) {
1673 isl_id *id;
1675 id = isl_ast_build_get_iterator_id(build, i);
1676 mpa = isl_multi_pw_aff_set_dim_id(mpa, isl_dim_in, i, id);
1679 return mpa;
1682 /* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
1683 * the remaining arguments derived from "mpa".
1684 * That is, construct a call or access expression that calls/accesses "arg0"
1685 * with arguments/indices specified by "mpa".
1687 static __isl_give isl_ast_expr *isl_ast_build_with_arguments(
1688 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
1689 __isl_take isl_ast_expr *arg0, __isl_take isl_multi_pw_aff *mpa)
1691 int i, n;
1692 isl_ctx *ctx;
1693 isl_ast_expr *expr;
1695 ctx = isl_ast_build_get_ctx(build);
1697 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
1698 expr = isl_ast_expr_alloc_op(ctx, type, 1 + n);
1699 expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
1700 for (i = 0; i < n; ++i) {
1701 isl_pw_aff *pa;
1702 isl_ast_expr *arg;
1704 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
1705 arg = isl_ast_build_expr_from_pw_aff_internal(build, pa);
1706 expr = isl_ast_expr_set_op_arg(expr, 1 + i, arg);
1709 isl_multi_pw_aff_free(mpa);
1710 return expr;
1713 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
1714 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
1715 __isl_take isl_multi_pw_aff *mpa);
1717 /* Construct an isl_ast_expr that accesses the member specified by "mpa".
1718 * The range of "mpa" is assumed to be wrapped relation.
1719 * The domain of this wrapped relation specifies the structure being
1720 * accessed, while the range of this wrapped relation spacifies the
1721 * member of the structure being accessed.
1723 * The domain of "mpa" is assumed to live in the internal schedule domain.
1725 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_member(
1726 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
1728 isl_id *id;
1729 isl_multi_pw_aff *domain;
1730 isl_ast_expr *domain_expr, *expr;
1731 enum isl_ast_op_type type = isl_ast_op_access;
1733 domain = isl_multi_pw_aff_copy(mpa);
1734 domain = isl_multi_pw_aff_range_factor_domain(domain);
1735 domain_expr = isl_ast_build_from_multi_pw_aff_internal(build,
1736 type, domain);
1737 mpa = isl_multi_pw_aff_range_factor_range(mpa);
1738 if (!isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
1739 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
1740 "missing field name", goto error);
1741 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
1742 expr = isl_ast_expr_from_id(id);
1743 expr = isl_ast_expr_alloc_binary(isl_ast_op_member, domain_expr, expr);
1744 return isl_ast_build_with_arguments(build, type, expr, mpa);
1745 error:
1746 isl_multi_pw_aff_free(mpa);
1747 return NULL;
1750 /* Construct an isl_ast_expr of type "type" that calls or accesses
1751 * the element specified by "mpa".
1752 * The first argument is obtained from the output tuple name.
1753 * The remaining arguments are given by the piecewise affine expressions.
1755 * If the range of "mpa" is a mapped relation, then we assume it
1756 * represents an access to a member of a structure.
1758 * The domain of "mpa" is assumed to live in the internal schedule domain.
1760 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
1761 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
1762 __isl_take isl_multi_pw_aff *mpa)
1764 isl_ctx *ctx;
1765 isl_id *id;
1766 isl_ast_expr *expr;
1768 if (!mpa)
1769 goto error;
1771 if (type == isl_ast_op_access &&
1772 isl_multi_pw_aff_range_is_wrapping(mpa))
1773 return isl_ast_build_from_multi_pw_aff_member(build, mpa);
1775 mpa = set_iterator_names(build, mpa);
1776 if (!build || !mpa)
1777 goto error;
1779 ctx = isl_ast_build_get_ctx(build);
1781 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
1782 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
1783 else
1784 id = isl_id_alloc(ctx, "", NULL);
1786 expr = isl_ast_expr_from_id(id);
1787 return isl_ast_build_with_arguments(build, type, expr, mpa);
1788 error:
1789 isl_multi_pw_aff_free(mpa);
1790 return NULL;
1793 /* Construct an isl_ast_expr of type "type" that calls or accesses
1794 * the element specified by "pma".
1795 * The first argument is obtained from the output tuple name.
1796 * The remaining arguments are given by the piecewise affine expressions.
1798 * The domain of "pma" is assumed to live in the internal schedule domain.
1800 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff_internal(
1801 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
1802 __isl_take isl_pw_multi_aff *pma)
1804 isl_multi_pw_aff *mpa;
1806 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
1807 return isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
1810 /* Construct an isl_ast_expr of type "type" that calls or accesses
1811 * the element specified by "mpa".
1812 * The first argument is obtained from the output tuple name.
1813 * The remaining arguments are given by the piecewise affine expressions.
1815 * The domain of "mpa" is assumed to live in the external schedule domain.
1817 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff(
1818 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
1819 __isl_take isl_multi_pw_aff *mpa)
1821 int is_domain;
1822 isl_ast_expr *expr;
1823 isl_space *space_build, *space_mpa;
1825 space_build = isl_ast_build_get_space(build, 0);
1826 space_mpa = isl_multi_pw_aff_get_space(mpa);
1827 is_domain = isl_space_tuple_is_equal(space_build, isl_dim_set,
1828 space_mpa, isl_dim_in);
1829 isl_space_free(space_build);
1830 isl_space_free(space_mpa);
1831 if (is_domain < 0)
1832 goto error;
1833 if (!is_domain)
1834 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
1835 "spaces don't match", goto error);
1837 if (isl_ast_build_need_schedule_map(build)) {
1838 isl_multi_aff *ma;
1839 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1840 mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
1843 expr = isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
1844 return expr;
1845 error:
1846 isl_multi_pw_aff_free(mpa);
1847 return NULL;
1850 /* Construct an isl_ast_expr that calls the domain element specified by "mpa".
1851 * The name of the function is obtained from the output tuple name.
1852 * The arguments are given by the piecewise affine expressions.
1854 * The domain of "mpa" is assumed to live in the external schedule domain.
1856 __isl_give isl_ast_expr *isl_ast_build_call_from_multi_pw_aff(
1857 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
1859 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_call, mpa);
1862 /* Construct an isl_ast_expr that accesses the array element specified by "mpa".
1863 * The name of the array is obtained from the output tuple name.
1864 * The index expressions are given by the piecewise affine expressions.
1866 * The domain of "mpa" is assumed to live in the external schedule domain.
1868 __isl_give isl_ast_expr *isl_ast_build_access_from_multi_pw_aff(
1869 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
1871 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_access, mpa);
1874 /* Construct an isl_ast_expr of type "type" that calls or accesses
1875 * the element specified by "pma".
1876 * The first argument is obtained from the output tuple name.
1877 * The remaining arguments are given by the piecewise affine expressions.
1879 * The domain of "pma" is assumed to live in the external schedule domain.
1881 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff(
1882 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
1883 __isl_take isl_pw_multi_aff *pma)
1885 isl_multi_pw_aff *mpa;
1887 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
1888 return isl_ast_build_from_multi_pw_aff(build, type, mpa);
1891 /* Construct an isl_ast_expr that calls the domain element specified by "pma".
1892 * The name of the function is obtained from the output tuple name.
1893 * The arguments are given by the piecewise affine expressions.
1895 * The domain of "pma" is assumed to live in the external schedule domain.
1897 __isl_give isl_ast_expr *isl_ast_build_call_from_pw_multi_aff(
1898 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
1900 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_call, pma);
1903 /* Construct an isl_ast_expr that accesses the array element specified by "pma".
1904 * The name of the array is obtained from the output tuple name.
1905 * The index expressions are given by the piecewise affine expressions.
1907 * The domain of "pma" is assumed to live in the external schedule domain.
1909 __isl_give isl_ast_expr *isl_ast_build_access_from_pw_multi_aff(
1910 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
1912 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_access, pma);
1915 /* Construct an isl_ast_expr that calls the domain element
1916 * specified by "executed".
1918 * "executed" is assumed to be single-valued, with a domain that lives
1919 * in the internal schedule space.
1921 __isl_give isl_ast_node *isl_ast_build_call_from_executed(
1922 __isl_keep isl_ast_build *build, __isl_take isl_map *executed)
1924 isl_pw_multi_aff *iteration;
1925 isl_ast_expr *expr;
1927 iteration = isl_pw_multi_aff_from_map(executed);
1928 iteration = isl_ast_build_compute_gist_pw_multi_aff(build, iteration);
1929 iteration = isl_pw_multi_aff_intersect_domain(iteration,
1930 isl_ast_build_get_domain(build));
1931 expr = isl_ast_build_from_pw_multi_aff_internal(build, isl_ast_op_call,
1932 iteration);
1933 return isl_ast_node_alloc_user(expr);