isl_ast_build_expr_from_pw_aff: collect all pieces before construction
[isl.git] / isl_ast_build_expr.c
blob7666b23ae8b66a34729a8eee706e3a08591ddee4
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 denominator "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 * If the constant term is "too large", then the constraint is rejected,
666 * where "too large" is fairly arbitrarily set to 1 << 15.
667 * We do this to avoid picking up constraints that bound a variable
668 * by a very large number, say the largest or smallest possible
669 * variable in the representation of some integer type.
671 static isl_stat check_parallel_or_opposite(__isl_take isl_constraint *c,
672 void *user)
674 struct isl_extract_mod_data *data = user;
675 enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
676 enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
677 int i, t;
678 int n[2];
679 int parallel = 1, opposite = 1;
681 for (t = 0; t < 2; ++t) {
682 n[t] = isl_constraint_dim(c, c_type[t]);
683 for (i = 0; i < n[t]; ++i) {
684 int a, b;
686 a = isl_constraint_involves_dims(c, c_type[t], i, 1);
687 b = isl_aff_involves_dims(data->div, a_type[t], i, 1);
688 if (a != b)
689 parallel = opposite = 0;
693 if (parallel || opposite) {
694 isl_val *v;
696 v = isl_val_abs(isl_constraint_get_constant_val(c));
697 if (isl_val_cmp_si(v, 1 << 15) > 0)
698 parallel = opposite = 0;
699 isl_val_free(v);
702 for (t = 0; t < 2; ++t) {
703 for (i = 0; i < n[t]; ++i) {
704 isl_val *v1, *v2;
706 if (!parallel && !opposite)
707 break;
708 v1 = isl_constraint_get_coefficient_val(c,
709 c_type[t], i);
710 v2 = isl_aff_get_coefficient_val(data->div,
711 a_type[t], i);
712 if (parallel) {
713 v1 = isl_val_sub(v1, isl_val_copy(v2));
714 parallel = isl_val_is_divisible_by(v1, data->d);
715 v1 = isl_val_add(v1, isl_val_copy(v2));
717 if (opposite) {
718 v1 = isl_val_add(v1, isl_val_copy(v2));
719 opposite = isl_val_is_divisible_by(v1, data->d);
721 isl_val_free(v1);
722 isl_val_free(v2);
726 if ((parallel || opposite) && mod_constraint_is_simpler(data, c)) {
727 isl_aff_free(data->nonneg);
728 data->nonneg = isl_constraint_get_aff(c);
729 data->sign = parallel ? 1 : -1;
732 isl_constraint_free(c);
734 if (data->sign != 0 && data->nonneg == NULL)
735 return isl_stat_error;
737 return isl_stat_ok;
740 /* Given that data->v * div_i in data->aff is of the form
742 * f * d * floor(div/d) (1)
744 * see if we can find an expression div' that is non-negative over data->build
745 * and that is related to div through
747 * div' = div + d * e
749 * or
751 * div' = -div + d - 1 + d * e
753 * with e some affine expression.
754 * If so, we write (1) as
756 * f * div + f * (div' mod d)
758 * or
760 * -f * (-div + d - 1) - f * (div' mod d)
762 * exploiting (in the second case) the fact that
764 * f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
767 * We first try to find an appropriate expression for div'
768 * from the constraints of data->build->domain (which is therefore
769 * guaranteed to be non-negative on data->build), where we remove
770 * any integer divisions from the constraints and skip this step
771 * if "div" itself involves any integer divisions.
772 * If we cannot find an appropriate expression this way, then
773 * we pass control to extract_nonneg_mod where check
774 * if div or "-div + d -1" themselves happen to be
775 * non-negative on data->build.
777 * While looking for an appropriate constraint in data->build->domain,
778 * we ignore the constant term, so after finding such a constraint,
779 * we still need to fix up the constant term.
780 * In particular, if a is the constant term of "div"
781 * (or d - 1 - the constant term of "div" if data->sign < 0)
782 * and b is the constant term of the constraint, then we need to find
783 * a non-negative constant c such that
785 * b + c \equiv a mod d
787 * We therefore take
789 * c = (a - b) mod d
791 * and add it to b to obtain the constant term of div'.
792 * If this constant term is "too negative", then we add an appropriate
793 * multiple of d to make it positive.
796 * Note that the above is a only a very simple heuristic for finding an
797 * appropriate expression. We could try a bit harder by also considering
798 * sums of constraints that involve disjoint sets of variables or
799 * we could consider arbitrary linear combinations of constraints,
800 * although that could potentially be much more expensive as it involves
801 * the solution of an LP problem.
803 * In particular, if v_i is a column vector representing constraint i,
804 * w represents div and e_i is the i-th unit vector, then we are looking
805 * for a solution of the constraints
807 * \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
809 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
810 * If we are not just interested in a non-negative expression, but
811 * also in one with a minimal range, then we don't just want
812 * c = \sum_i lambda_i v_i to be non-negative over the domain,
813 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
814 * that we want to minimize and we now also have to take into account
815 * the constant terms of the constraints.
816 * Alternatively, we could first compute the dual of the domain
817 * and plug in the constraints on the coefficients.
819 static int try_extract_mod(struct isl_extract_mod_data *data)
821 isl_basic_set *hull;
822 isl_val *v1, *v2;
823 int r, n;
825 if (!data->build)
826 goto error;
828 n = isl_aff_dim(data->div, isl_dim_div);
830 if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
831 return extract_nonneg_mod(data);
833 hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
834 hull = isl_basic_set_remove_divs(hull);
835 data->sign = 0;
836 data->nonneg = NULL;
837 r = isl_basic_set_foreach_constraint(hull, &check_parallel_or_opposite,
838 data);
839 isl_basic_set_free(hull);
841 if (!data->sign || r < 0) {
842 isl_aff_free(data->nonneg);
843 if (r < 0)
844 goto error;
845 return extract_nonneg_mod(data);
848 v1 = isl_aff_get_constant_val(data->div);
849 v2 = isl_aff_get_constant_val(data->nonneg);
850 if (data->sign < 0) {
851 v1 = isl_val_neg(v1);
852 v1 = isl_val_add(v1, isl_val_copy(data->d));
853 v1 = isl_val_sub_ui(v1, 1);
855 v1 = isl_val_sub(v1, isl_val_copy(v2));
856 v1 = isl_val_mod(v1, isl_val_copy(data->d));
857 v1 = isl_val_add(v1, v2);
858 v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
859 v2 = isl_val_ceil(v2);
860 if (isl_val_is_neg(v2)) {
861 v2 = isl_val_mul(v2, isl_val_copy(data->d));
862 v1 = isl_val_sub(v1, isl_val_copy(v2));
864 data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
865 isl_val_free(v2);
867 if (data->sign < 0) {
868 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
869 data->v = isl_val_neg(data->v);
872 return extract_term_and_mod(data,
873 isl_aff_copy(data->div), data->nonneg);
874 error:
875 data->aff = isl_aff_free(data->aff);
876 return -1;
879 /* Check if "data->aff" involves any (implicit) modulo computations based
880 * on div "data->i".
881 * If so, remove them from aff and add expressions corresponding
882 * to those modulo computations to data->pos and/or data->neg.
884 * "aff" is assumed to be an integer affine expression.
886 * In particular, check if (v * div_j) is of the form
888 * f * m * floor(a / m)
890 * and, if so, rewrite it as
892 * f * (a - (a mod m)) = f * a - f * (a mod m)
894 * and extract out -f * (a mod m).
895 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
896 * If f < 0, we add ((-f) * (a mod m)) to *pos.
898 * Note that in order to represent "a mod m" as
900 * (isl_ast_op_pdiv_r, a, m)
902 * we need to make sure that a is non-negative.
903 * If not, we check if "-a + m - 1" is non-negative.
904 * If so, we can rewrite
906 * floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
908 * and still extract a modulo.
910 static int extract_modulo(struct isl_extract_mod_data *data)
912 data->div = isl_aff_get_div(data->aff, data->i);
913 data->d = isl_aff_get_denominator_val(data->div);
914 if (isl_val_is_divisible_by(data->v, data->d)) {
915 data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
916 if (try_extract_mod(data) < 0)
917 data->aff = isl_aff_free(data->aff);
919 isl_aff_free(data->div);
920 isl_val_free(data->d);
921 return 0;
924 /* Check if "aff" involves any (implicit) modulo computations.
925 * If so, remove them from aff and add expressions corresponding
926 * to those modulo computations to *pos and/or *neg.
927 * We only do this if the option ast_build_prefer_pdiv is set.
929 * "aff" is assumed to be an integer affine expression.
931 * A modulo expression is of the form
933 * a mod m = a - m * floor(a / m)
935 * To detect them in aff, we look for terms of the form
937 * f * m * floor(a / m)
939 * rewrite them as
941 * f * (a - (a mod m)) = f * a - f * (a mod m)
943 * and extract out -f * (a mod m).
944 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
945 * If f < 0, we add ((-f) * (a mod m)) to *pos.
947 static __isl_give isl_aff *extract_modulos(__isl_take isl_aff *aff,
948 __isl_keep isl_ast_expr **pos, __isl_keep isl_ast_expr **neg,
949 __isl_keep isl_ast_build *build)
951 struct isl_extract_mod_data data = { build, aff, *pos, *neg };
952 isl_ctx *ctx;
953 int n;
955 if (!aff)
956 return NULL;
958 ctx = isl_aff_get_ctx(aff);
959 if (!isl_options_get_ast_build_prefer_pdiv(ctx))
960 return aff;
962 n = isl_aff_dim(data.aff, isl_dim_div);
963 for (data.i = 0; data.i < n; ++data.i) {
964 data.v = isl_aff_get_coefficient_val(data.aff,
965 isl_dim_div, data.i);
966 if (!data.v)
967 return isl_aff_free(aff);
968 if (isl_val_is_zero(data.v) ||
969 isl_val_is_one(data.v) || isl_val_is_negone(data.v)) {
970 isl_val_free(data.v);
971 continue;
973 if (extract_modulo(&data) < 0)
974 data.aff = isl_aff_free(data.aff);
975 isl_val_free(data.v);
976 if (!data.aff)
977 break;
980 if (data.add)
981 data.aff = isl_aff_add(data.aff, data.add);
983 *pos = data.pos;
984 *neg = data.neg;
985 return data.aff;
988 /* Check if aff involves any non-integer coefficients.
989 * If so, split aff into
991 * aff = aff1 + (aff2 / d)
993 * with both aff1 and aff2 having only integer coefficients.
994 * Return aff1 and add (aff2 / d) to *expr.
996 static __isl_give isl_aff *extract_rational(__isl_take isl_aff *aff,
997 __isl_keep isl_ast_expr **expr, __isl_keep isl_ast_build *build)
999 int i, j, n;
1000 isl_aff *rat = NULL;
1001 isl_local_space *ls = NULL;
1002 isl_ast_expr *rat_expr;
1003 isl_val *v, *d;
1004 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1005 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1007 if (!aff)
1008 return NULL;
1009 d = isl_aff_get_denominator_val(aff);
1010 if (!d)
1011 goto error;
1012 if (isl_val_is_one(d)) {
1013 isl_val_free(d);
1014 return aff;
1017 aff = isl_aff_scale_val(aff, isl_val_copy(d));
1019 ls = isl_aff_get_domain_local_space(aff);
1020 rat = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1022 for (i = 0; i < 3; ++i) {
1023 n = isl_aff_dim(aff, t[i]);
1024 for (j = 0; j < n; ++j) {
1025 isl_aff *rat_j;
1027 v = isl_aff_get_coefficient_val(aff, t[i], j);
1028 if (!v)
1029 goto error;
1030 if (isl_val_is_divisible_by(v, d)) {
1031 isl_val_free(v);
1032 continue;
1034 rat_j = isl_aff_var_on_domain(isl_local_space_copy(ls),
1035 l[i], j);
1036 rat_j = isl_aff_scale_val(rat_j, v);
1037 rat = isl_aff_add(rat, rat_j);
1041 v = isl_aff_get_constant_val(aff);
1042 if (isl_val_is_divisible_by(v, d)) {
1043 isl_val_free(v);
1044 } else {
1045 isl_aff *rat_0;
1047 rat_0 = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
1048 rat = isl_aff_add(rat, rat_0);
1051 isl_local_space_free(ls);
1053 aff = isl_aff_sub(aff, isl_aff_copy(rat));
1054 aff = isl_aff_scale_down_val(aff, isl_val_copy(d));
1056 rat_expr = isl_ast_expr_from_aff(rat, build);
1057 rat_expr = isl_ast_expr_div(rat_expr, isl_ast_expr_from_val(d));
1058 *expr = ast_expr_add(*expr, rat_expr);
1060 return aff;
1061 error:
1062 isl_aff_free(rat);
1063 isl_local_space_free(ls);
1064 isl_aff_free(aff);
1065 isl_val_free(d);
1066 return NULL;
1069 /* Construct an isl_ast_expr that evaluates the affine expression "aff",
1070 * The result is simplified in terms of build->domain.
1072 * We first extract hidden modulo computations from the affine expression
1073 * and then add terms for each variable with a non-zero coefficient.
1074 * Finally, if the affine expression has a non-trivial denominator,
1075 * we divide the resulting isl_ast_expr by this denominator.
1077 __isl_give isl_ast_expr *isl_ast_expr_from_aff(__isl_take isl_aff *aff,
1078 __isl_keep isl_ast_build *build)
1080 int i, j;
1081 int n;
1082 isl_val *v;
1083 isl_ctx *ctx = isl_aff_get_ctx(aff);
1084 isl_ast_expr *expr, *expr_neg;
1085 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1086 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1087 isl_local_space *ls;
1088 struct isl_ast_add_term_data data;
1090 if (!aff)
1091 return NULL;
1093 expr = isl_ast_expr_alloc_int_si(ctx, 0);
1094 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1096 aff = extract_rational(aff, &expr, build);
1098 aff = extract_modulos(aff, &expr, &expr_neg, build);
1099 expr = ast_expr_sub(expr, expr_neg);
1101 ls = isl_aff_get_domain_local_space(aff);
1103 data.build = build;
1104 data.cst = isl_aff_get_constant_val(aff);
1105 for (i = 0; i < 3; ++i) {
1106 n = isl_aff_dim(aff, t[i]);
1107 for (j = 0; j < n; ++j) {
1108 v = isl_aff_get_coefficient_val(aff, t[i], j);
1109 if (!v)
1110 expr = isl_ast_expr_free(expr);
1111 if (isl_val_is_zero(v)) {
1112 isl_val_free(v);
1113 continue;
1115 expr = isl_ast_expr_add_term(expr,
1116 ls, l[i], j, v, &data);
1120 expr = isl_ast_expr_add_int(expr, data.cst);
1122 isl_local_space_free(ls);
1123 isl_aff_free(aff);
1124 return expr;
1127 /* Add terms to "expr" for each variable in "aff" with a coefficient
1128 * with sign equal to "sign".
1129 * The result is simplified in terms of data->build->domain.
1131 static __isl_give isl_ast_expr *add_signed_terms(__isl_take isl_ast_expr *expr,
1132 __isl_keep isl_aff *aff, int sign, struct isl_ast_add_term_data *data)
1134 int i, j;
1135 isl_val *v;
1136 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1137 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1138 isl_local_space *ls;
1140 ls = isl_aff_get_domain_local_space(aff);
1142 for (i = 0; i < 3; ++i) {
1143 int n = isl_aff_dim(aff, t[i]);
1144 for (j = 0; j < n; ++j) {
1145 v = isl_aff_get_coefficient_val(aff, t[i], j);
1146 if (sign * isl_val_sgn(v) <= 0) {
1147 isl_val_free(v);
1148 continue;
1150 v = isl_val_abs(v);
1151 expr = isl_ast_expr_add_term(expr,
1152 ls, l[i], j, v, data);
1156 isl_local_space_free(ls);
1158 return expr;
1161 /* Should the constant term "v" be considered positive?
1163 * A positive constant will be added to "pos" by the caller,
1164 * while a negative constant will be added to "neg".
1165 * If either "pos" or "neg" is exactly zero, then we prefer
1166 * to add the constant "v" to that side, irrespective of the sign of "v".
1167 * This results in slightly shorter expressions and may reduce the risk
1168 * of overflows.
1170 static int constant_is_considered_positive(__isl_keep isl_val *v,
1171 __isl_keep isl_ast_expr *pos, __isl_keep isl_ast_expr *neg)
1173 if (ast_expr_is_zero(pos))
1174 return 1;
1175 if (ast_expr_is_zero(neg))
1176 return 0;
1177 return isl_val_is_pos(v);
1180 /* Check if the equality
1182 * aff = 0
1184 * represents a stride constraint on the integer division "pos".
1186 * In particular, if the integer division "pos" is equal to
1188 * floor(e/d)
1190 * then check if aff is equal to
1192 * e - d floor(e/d)
1194 * or its opposite.
1196 * If so, the equality is exactly
1198 * e mod d = 0
1200 * Note that in principle we could also accept
1202 * e - d floor(e'/d)
1204 * where e and e' differ by a constant.
1206 static int is_stride_constraint(__isl_keep isl_aff *aff, int pos)
1208 isl_aff *div;
1209 isl_val *c, *d;
1210 int eq;
1212 div = isl_aff_get_div(aff, pos);
1213 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1214 d = isl_aff_get_denominator_val(div);
1215 eq = isl_val_abs_eq(c, d);
1216 if (eq >= 0 && eq) {
1217 aff = isl_aff_copy(aff);
1218 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1219 div = isl_aff_scale_val(div, d);
1220 if (isl_val_is_pos(c))
1221 div = isl_aff_neg(div);
1222 eq = isl_aff_plain_is_equal(div, aff);
1223 isl_aff_free(aff);
1224 } else
1225 isl_val_free(d);
1226 isl_val_free(c);
1227 isl_aff_free(div);
1229 return eq;
1232 /* Are all coefficients of "aff" (zero or) negative?
1234 static int all_negative_coefficients(__isl_keep isl_aff *aff)
1236 int i, n;
1238 if (!aff)
1239 return 0;
1241 n = isl_aff_dim(aff, isl_dim_param);
1242 for (i = 0; i < n; ++i)
1243 if (isl_aff_coefficient_sgn(aff, isl_dim_param, i) > 0)
1244 return 0;
1246 n = isl_aff_dim(aff, isl_dim_in);
1247 for (i = 0; i < n; ++i)
1248 if (isl_aff_coefficient_sgn(aff, isl_dim_in, i) > 0)
1249 return 0;
1251 return 1;
1254 /* Give an equality of the form
1256 * aff = e - d floor(e/d) = 0
1258 * or
1260 * aff = -e + d floor(e/d) = 0
1262 * with the integer division "pos" equal to floor(e/d),
1263 * construct the AST expression
1265 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1267 * If e only has negative coefficients, then construct
1269 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(-e), expr(d)), expr(0))
1271 * instead.
1273 static __isl_give isl_ast_expr *extract_stride_constraint(
1274 __isl_take isl_aff *aff, int pos, __isl_keep isl_ast_build *build)
1276 isl_ctx *ctx;
1277 isl_val *c;
1278 isl_ast_expr *expr, *cst;
1280 if (!aff)
1281 return NULL;
1283 ctx = isl_aff_get_ctx(aff);
1285 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1286 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1288 if (all_negative_coefficients(aff))
1289 aff = isl_aff_neg(aff);
1291 cst = isl_ast_expr_from_val(isl_val_abs(c));
1292 expr = isl_ast_expr_from_aff(aff, build);
1294 expr = isl_ast_expr_alloc_binary(isl_ast_op_zdiv_r, expr, cst);
1295 cst = isl_ast_expr_alloc_int_si(ctx, 0);
1296 expr = isl_ast_expr_alloc_binary(isl_ast_op_eq, expr, cst);
1298 return expr;
1301 /* Construct an isl_ast_expr that evaluates the condition "constraint",
1302 * The result is simplified in terms of build->domain.
1304 * We first check if the constraint is an equality of the form
1306 * e - d floor(e/d) = 0
1308 * i.e.,
1310 * e mod d = 0
1312 * If so, we convert it to
1314 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1316 * Otherwise, let the constraint by either "a >= 0" or "a == 0".
1317 * We first extract hidden modulo computations from "a"
1318 * and then collect all the terms with a positive coefficient in cons_pos
1319 * and the terms with a negative coefficient in cons_neg.
1321 * The result is then of the form
1323 * (isl_ast_op_ge, expr(pos), expr(-neg)))
1325 * or
1327 * (isl_ast_op_eq, expr(pos), expr(-neg)))
1329 * However, if the first expression is an integer constant (and the second
1330 * is not), then we swap the two expressions. This ensures that we construct,
1331 * e.g., "i <= 5" rather than "5 >= i".
1333 * Furthermore, is there are no terms with positive coefficients (or no terms
1334 * with negative coefficients), then the constant term is added to "pos"
1335 * (or "neg"), ignoring the sign of the constant term.
1337 static __isl_give isl_ast_expr *isl_ast_expr_from_constraint(
1338 __isl_take isl_constraint *constraint, __isl_keep isl_ast_build *build)
1340 int i, n;
1341 isl_ctx *ctx;
1342 isl_ast_expr *expr_pos;
1343 isl_ast_expr *expr_neg;
1344 isl_ast_expr *expr;
1345 isl_aff *aff;
1346 int eq;
1347 enum isl_ast_op_type type;
1348 struct isl_ast_add_term_data data;
1350 if (!constraint)
1351 return NULL;
1353 aff = isl_constraint_get_aff(constraint);
1354 eq = isl_constraint_is_equality(constraint);
1355 isl_constraint_free(constraint);
1357 n = isl_aff_dim(aff, isl_dim_div);
1358 if (eq && n > 0)
1359 for (i = 0; i < n; ++i) {
1360 int is_stride;
1361 is_stride = is_stride_constraint(aff, i);
1362 if (is_stride < 0)
1363 goto error;
1364 if (is_stride)
1365 return extract_stride_constraint(aff, i, build);
1368 ctx = isl_aff_get_ctx(aff);
1369 expr_pos = isl_ast_expr_alloc_int_si(ctx, 0);
1370 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1372 aff = extract_modulos(aff, &expr_pos, &expr_neg, build);
1374 data.build = build;
1375 data.cst = isl_aff_get_constant_val(aff);
1376 expr_pos = add_signed_terms(expr_pos, aff, 1, &data);
1377 data.cst = isl_val_neg(data.cst);
1378 expr_neg = add_signed_terms(expr_neg, aff, -1, &data);
1379 data.cst = isl_val_neg(data.cst);
1381 if (constant_is_considered_positive(data.cst, expr_pos, expr_neg)) {
1382 expr_pos = isl_ast_expr_add_int(expr_pos, data.cst);
1383 } else {
1384 data.cst = isl_val_neg(data.cst);
1385 expr_neg = isl_ast_expr_add_int(expr_neg, data.cst);
1388 if (isl_ast_expr_get_type(expr_pos) == isl_ast_expr_int &&
1389 isl_ast_expr_get_type(expr_neg) != isl_ast_expr_int) {
1390 type = eq ? isl_ast_op_eq : isl_ast_op_le;
1391 expr = isl_ast_expr_alloc_binary(type, expr_neg, expr_pos);
1392 } else {
1393 type = eq ? isl_ast_op_eq : isl_ast_op_ge;
1394 expr = isl_ast_expr_alloc_binary(type, expr_pos, expr_neg);
1397 isl_aff_free(aff);
1398 return expr;
1399 error:
1400 isl_aff_free(aff);
1401 return NULL;
1404 /* Wrapper around isl_constraint_cmp_last_non_zero for use
1405 * as a callback to isl_constraint_list_sort.
1406 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1407 * apart, then use isl_constraint_plain_cmp instead.
1409 static int cmp_constraint(__isl_keep isl_constraint *a,
1410 __isl_keep isl_constraint *b, void *user)
1412 int cmp;
1414 cmp = isl_constraint_cmp_last_non_zero(a, b);
1415 if (cmp != 0)
1416 return cmp;
1417 return isl_constraint_plain_cmp(a, b);
1420 /* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1421 * The result is simplified in terms of build->domain.
1423 * If "bset" is not bounded by any constraint, then we contruct
1424 * the expression "1", i.e., "true".
1426 * Otherwise, we sort the constraints, putting constraints that involve
1427 * integer divisions after those that do not, and construct an "and"
1428 * of the ast expressions of the individual constraints.
1430 * Each constraint is added to the generated constraints of the build
1431 * after it has been converted to an AST expression so that it can be used
1432 * to simplify the following constraints. This may change the truth value
1433 * of subsequent constraints that do not satisfy the earlier constraints,
1434 * but this does not affect the outcome of the conjunction as it is
1435 * only true if all the conjuncts are true (no matter in what order
1436 * they are evaluated). In particular, the constraints that do not
1437 * involve integer divisions may serve to simplify some constraints
1438 * that do involve integer divisions.
1440 __isl_give isl_ast_expr *isl_ast_build_expr_from_basic_set(
1441 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1443 int i, n;
1444 isl_constraint *c;
1445 isl_constraint_list *list;
1446 isl_ast_expr *res;
1447 isl_set *set;
1449 list = isl_basic_set_get_constraint_list(bset);
1450 isl_basic_set_free(bset);
1451 list = isl_constraint_list_sort(list, &cmp_constraint, NULL);
1452 if (!list)
1453 return NULL;
1454 n = isl_constraint_list_n_constraint(list);
1455 if (n == 0) {
1456 isl_ctx *ctx = isl_constraint_list_get_ctx(list);
1457 isl_constraint_list_free(list);
1458 return isl_ast_expr_alloc_int_si(ctx, 1);
1461 build = isl_ast_build_copy(build);
1463 c = isl_constraint_list_get_constraint(list, 0);
1464 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1465 set = isl_set_from_basic_set(bset);
1466 res = isl_ast_expr_from_constraint(c, build);
1467 build = isl_ast_build_restrict_generated(build, set);
1469 for (i = 1; i < n; ++i) {
1470 isl_ast_expr *expr;
1472 c = isl_constraint_list_get_constraint(list, i);
1473 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1474 set = isl_set_from_basic_set(bset);
1475 expr = isl_ast_expr_from_constraint(c, build);
1476 build = isl_ast_build_restrict_generated(build, set);
1477 res = isl_ast_expr_and(res, expr);
1480 isl_constraint_list_free(list);
1481 isl_ast_build_free(build);
1482 return res;
1485 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1486 * The result is simplified in terms of build->domain.
1488 * If "set" is an (obviously) empty set, then return the expression "0".
1490 * If there are multiple disjuncts in the description of the set,
1491 * then subsequent disjuncts are simplified in a context where
1492 * the previous disjuncts have been removed from build->domain.
1493 * In particular, constraints that ensure that there is no overlap
1494 * with these previous disjuncts, can be removed.
1495 * This is mostly useful for disjuncts that are only defined by
1496 * a single constraint (relative to the build domain) as the opposite
1497 * of that single constraint can then be removed from the other disjuncts.
1498 * In order not to increase the number of disjuncts in the build domain
1499 * after subtracting the previous disjuncts of "set", the simple hull
1500 * is computed after taking the difference with each of these disjuncts.
1501 * This means that constraints that prevent overlap with a union
1502 * of multiple previous disjuncts are not removed.
1504 * "set" lives in the internal schedule space.
1506 __isl_give isl_ast_expr *isl_ast_build_expr_from_set_internal(
1507 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1509 int i, n;
1510 isl_basic_set *bset;
1511 isl_basic_set_list *list;
1512 isl_set *domain;
1513 isl_ast_expr *res;
1515 list = isl_set_get_basic_set_list(set);
1516 isl_set_free(set);
1518 if (!list)
1519 return NULL;
1520 n = isl_basic_set_list_n_basic_set(list);
1521 if (n == 0) {
1522 isl_ctx *ctx = isl_ast_build_get_ctx(build);
1523 isl_basic_set_list_free(list);
1524 return isl_ast_expr_from_val(isl_val_zero(ctx));
1527 domain = isl_ast_build_get_domain(build);
1529 bset = isl_basic_set_list_get_basic_set(list, 0);
1530 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1531 res = isl_ast_build_expr_from_basic_set(build, bset);
1533 for (i = 1; i < n; ++i) {
1534 isl_ast_expr *expr;
1535 isl_set *rest;
1537 rest = isl_set_subtract(isl_set_copy(domain), set);
1538 rest = isl_set_from_basic_set(isl_set_simple_hull(rest));
1539 domain = isl_set_intersect(domain, rest);
1540 bset = isl_basic_set_list_get_basic_set(list, i);
1541 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1542 bset = isl_basic_set_gist(bset,
1543 isl_set_simple_hull(isl_set_copy(domain)));
1544 expr = isl_ast_build_expr_from_basic_set(build, bset);
1545 res = isl_ast_expr_or(res, expr);
1548 isl_set_free(domain);
1549 isl_set_free(set);
1550 isl_basic_set_list_free(list);
1551 return res;
1554 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1555 * The result is simplified in terms of build->domain.
1557 * If "set" is an (obviously) empty set, then return the expression "0".
1559 * "set" lives in the external schedule space.
1561 * The internal AST expression generation assumes that there are
1562 * no unknown divs, so make sure an explicit representation is available.
1563 * Since the set comes from the outside, it may have constraints that
1564 * are redundant with respect to the build domain. Remove them first.
1566 __isl_give isl_ast_expr *isl_ast_build_expr_from_set(
1567 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1569 if (isl_ast_build_need_schedule_map(build)) {
1570 isl_multi_aff *ma;
1571 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1572 set = isl_set_preimage_multi_aff(set, ma);
1575 set = isl_set_compute_divs(set);
1576 set = isl_ast_build_compute_gist(build, set);
1577 return isl_ast_build_expr_from_set_internal(build, set);
1580 /* State of data about previous pieces in
1581 * isl_ast_build_expr_from_pw_aff_internal.
1583 * isl_state_none: no data about previous pieces
1584 * isl_state_single: data about a single previous piece
1585 * isl_state_min: data represents minimum of several pieces
1586 * isl_state_max: data represents maximum of several pieces
1588 enum isl_from_pw_aff_state {
1589 isl_state_none,
1590 isl_state_single,
1591 isl_state_min,
1592 isl_state_max
1595 /* Internal date structure representing a single piece in the input of
1596 * isl_ast_build_expr_from_pw_aff_internal.
1598 * If "state" is isl_state_none, then "set_list" and "aff_list" are not used.
1599 * If "state" is isl_state_single, then "set_list" and "aff_list" contain the
1600 * single previous subpiece.
1601 * If "state" is isl_state_min, then "set_list" and "aff_list" contain
1602 * a sequence of several previous subpieces that are equal to the minimum
1603 * of the entries in "aff_list" over the union of "set_list"
1604 * If "state" is isl_state_max, then "set_list" and "aff_list" contain
1605 * a sequence of several previous subpieces that are equal to the maximum
1606 * of the entries in "aff_list" over the union of "set_list"
1608 struct isl_from_pw_aff_piece {
1609 enum isl_from_pw_aff_state state;
1610 isl_set_list *set_list;
1611 isl_aff_list *aff_list;
1614 /* Internal data structure for isl_ast_build_expr_from_pw_aff_internal.
1616 * "build" specifies the domain against which the result is simplified.
1617 * "dom" is the domain of the entire isl_pw_aff.
1619 * "n" is the number of pieces constructed already.
1620 * In particular, during the construction of the pieces, "n" points to
1621 * the piece that is being constructed. After the construction of the
1622 * pieces, "n" is set to the total number of pieces.
1623 * "max" is the total number of allocated entries.
1624 * "p" contains the individual pieces.
1626 struct isl_from_pw_aff_data {
1627 isl_ast_build *build;
1628 isl_set *dom;
1630 int n;
1631 int max;
1632 struct isl_from_pw_aff_piece *p;
1635 /* Initialize "data" based on "build" and "pa".
1637 static isl_stat isl_from_pw_aff_data_init(struct isl_from_pw_aff_data *data,
1638 __isl_keep isl_ast_build *build, __isl_keep isl_pw_aff *pa)
1640 int n;
1641 isl_ctx *ctx;
1643 ctx = isl_pw_aff_get_ctx(pa);
1644 n = isl_pw_aff_n_piece(pa);
1645 if (n == 0)
1646 isl_die(ctx, isl_error_invalid,
1647 "cannot handle void expression", return isl_stat_error);
1648 data->max = n;
1649 data->p = isl_calloc_array(ctx, struct isl_from_pw_aff_piece, n);
1650 if (!data->p)
1651 return isl_stat_error;
1652 data->build = build;
1653 data->dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1654 data->n = 0;
1656 return isl_stat_ok;
1659 /* Free all memory allocated for "data".
1661 static void isl_from_pw_aff_data_clear(struct isl_from_pw_aff_data *data)
1663 int i;
1665 isl_set_free(data->dom);
1666 if (!data->p)
1667 return;
1669 for (i = 0; i < data->max; ++i) {
1670 isl_set_list_free(data->p[i].set_list);
1671 isl_aff_list_free(data->p[i].aff_list);
1673 free(data->p);
1676 /* Initialize the current entry of "data" to an unused piece.
1678 static void set_none(struct isl_from_pw_aff_data *data)
1680 data->p[data->n].state = isl_state_none;
1681 data->p[data->n].set_list = NULL;
1682 data->p[data->n].aff_list = NULL;
1685 /* Store "set" and "aff" in the current entry of "data" as a single subpiece.
1687 static void set_single(struct isl_from_pw_aff_data *data,
1688 __isl_take isl_set *set, __isl_take isl_aff *aff)
1690 data->p[data->n].state = isl_state_single;
1691 data->p[data->n].set_list = isl_set_list_from_set(set);
1692 data->p[data->n].aff_list = isl_aff_list_from_aff(aff);
1695 /* Extend the current entry of "data" with "set" and "aff"
1696 * as a minimum expression.
1698 static isl_stat extend_min(struct isl_from_pw_aff_data *data,
1699 __isl_take isl_set *set, __isl_take isl_aff *aff)
1701 int n = data->n;
1702 data->p[n].state = isl_state_min;
1703 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1704 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1706 if (!data->p[n].set_list || !data->p[n].aff_list)
1707 return isl_stat_error;
1708 return isl_stat_ok;
1711 /* Extend the current entry of "data" with "set" and "aff"
1712 * as a maximum expression.
1714 static isl_stat extend_max(struct isl_from_pw_aff_data *data,
1715 __isl_take isl_set *set, __isl_take isl_aff *aff)
1717 int n = data->n;
1718 data->p[n].state = isl_state_max;
1719 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1720 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1722 if (!data->p[n].set_list || !data->p[n].aff_list)
1723 return isl_stat_error;
1724 return isl_stat_ok;
1727 /* Construct an isl_ast_expr from "list" within "build".
1728 * If "state" is isl_state_single, then "list" contains a single entry and
1729 * an isl_ast_expr is constructed for that entry.
1730 * Otherwise a min or max expression is constructed from "list"
1731 * depending on "state".
1733 static __isl_give isl_ast_expr *ast_expr_from_aff_list(
1734 __isl_take isl_aff_list *list, enum isl_from_pw_aff_state state,
1735 __isl_keep isl_ast_build *build)
1737 int i, n;
1738 isl_aff *aff;
1739 isl_ast_expr *expr;
1740 enum isl_ast_op_type op_type;
1742 if (state == isl_state_single) {
1743 aff = isl_aff_list_get_aff(list, 0);
1744 isl_aff_list_free(list);
1745 return isl_ast_expr_from_aff(aff, build);
1747 n = isl_aff_list_n_aff(list);
1748 op_type = state == isl_state_min ? isl_ast_op_min : isl_ast_op_max;
1749 expr = isl_ast_expr_alloc_op(isl_ast_build_get_ctx(build), op_type, n);
1750 if (!expr)
1751 goto error;
1753 for (i = 0; i < n; ++i) {
1754 isl_ast_expr *expr_i;
1756 aff = isl_aff_list_get_aff(list, i);
1757 expr_i = isl_ast_expr_from_aff(aff, build);
1758 if (!expr_i)
1759 goto error;
1760 expr->u.op.args[i] = expr_i;
1763 isl_aff_list_free(list);
1764 return expr;
1765 error:
1766 isl_aff_list_free(list);
1767 isl_ast_expr_free(expr);
1768 return NULL;
1771 /* Extend the expression in "next" to take into account
1772 * the piece at position "pos" in "data", allowing for a further extension
1773 * for the next piece(s).
1774 * In particular, "next" is set to a select operation that selects
1775 * an isl_ast_expr corresponding to data->aff_list on data->set_list and
1776 * to an expression that will be filled in by later calls.
1777 * Return a pointer to this location.
1778 * Afterwards, the state of "data" is set to isl_state_none.
1780 * The constraints of data->set_list are added to the generated
1781 * constraints of the build such that they can be exploited to simplify
1782 * the AST expression constructed from data->aff_list.
1784 static isl_ast_expr **add_intermediate_piece(struct isl_from_pw_aff_data *data,
1785 int pos, isl_ast_expr **next)
1787 isl_ctx *ctx;
1788 isl_ast_build *build;
1789 isl_ast_expr *ternary, *arg;
1790 isl_set *set, *gist;
1792 set = isl_set_list_union(data->p[pos].set_list);
1793 if (data->p[pos].state != isl_state_single)
1794 set = isl_set_coalesce(set);
1795 data->p[pos].set_list = NULL;
1796 ctx = isl_ast_build_get_ctx(data->build);
1797 ternary = isl_ast_expr_alloc_op(ctx, isl_ast_op_select, 3);
1798 gist = isl_set_gist(isl_set_copy(set), isl_set_copy(data->dom));
1799 arg = isl_ast_build_expr_from_set_internal(data->build, gist);
1800 ternary = isl_ast_expr_set_op_arg(ternary, 0, arg);
1801 build = isl_ast_build_copy(data->build);
1802 build = isl_ast_build_restrict_generated(build, set);
1803 arg = ast_expr_from_aff_list(data->p[pos].aff_list,
1804 data->p[pos].state, build);
1805 data->p[pos].aff_list = NULL;
1806 isl_ast_build_free(build);
1807 ternary = isl_ast_expr_set_op_arg(ternary, 1, arg);
1808 data->p[pos].state = isl_state_none;
1809 if (!ternary)
1810 return NULL;
1812 *next = ternary;
1813 return &ternary->u.op.args[2];
1816 /* Extend the expression in "next" to take into account
1817 * the final piece, located at position "pos" in "data".
1818 * In particular, "next" is set to evaluate data->aff_list
1819 * and the domain is ignored.
1820 * Return isl_stat_ok on success and isl_stat_error on failure.
1822 * The constraints of data->set_list are however added to the generated
1823 * constraints of the build such that they can be exploited to simplify
1824 * the AST expression constructed from data->aff_list.
1826 static isl_stat add_last_piece(struct isl_from_pw_aff_data *data,
1827 int pos, isl_ast_expr **next)
1829 isl_ast_build *build;
1830 isl_set *set;
1832 if (data->p[pos].state == isl_state_none)
1833 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1834 "cannot handle void expression", return isl_stat_error);
1836 set = isl_set_list_union(data->p[pos].set_list);
1837 if (data->p[pos].state != isl_state_single)
1838 set = isl_set_coalesce(set);
1839 data->p[pos].set_list = NULL;
1840 build = isl_ast_build_copy(data->build);
1841 build = isl_ast_build_restrict_generated(build, set);
1842 *next = ast_expr_from_aff_list(data->p[pos].aff_list,
1843 data->p[pos].state, build);
1844 data->p[pos].aff_list = NULL;
1845 isl_ast_build_free(build);
1846 data->p[pos].state = isl_state_none;
1847 if (!*next)
1848 return isl_stat_error;
1850 return isl_stat_ok;
1853 /* Construct an isl_ast_expr from the pieces in "data".
1854 * Return the result or NULL on failure.
1856 * When this function is called, data->n points to the current piece.
1857 * If this is an effective piece, then first increment data->n such
1858 * that data->n contains the number of pieces.
1860 * Construct intermediate AST expressions for the initial pieces and
1861 * finish off with the final pieces.
1863 static isl_ast_expr *build_pieces(struct isl_from_pw_aff_data *data)
1865 int i;
1866 isl_ast_expr *res = NULL;
1867 isl_ast_expr **next = &res;
1869 if (data->p[data->n].state != isl_state_none)
1870 data->n++;
1871 if (data->n == 0)
1872 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1873 "cannot handle void expression", return NULL);
1875 for (i = 0; i + 1 < data->n; ++i) {
1876 next = add_intermediate_piece(data, i, next);
1877 if (!next)
1878 return isl_ast_expr_free(res);
1881 if (add_last_piece(data, data->n - 1, next) < 0)
1882 return isl_ast_expr_free(res);
1884 return res;
1887 /* Can the list of subpieces in the last piece of "data" be extended with
1888 * "set" and "aff" based on "test"?
1889 * In particular, is it the case for each entry (set_i, aff_i) that
1891 * test(aff, aff_i) holds on set_i, and
1892 * test(aff_i, aff) holds on set?
1894 * "test" returns the set of elements where the tests holds, meaning
1895 * that test(aff_i, aff) holds on set if set is a subset of test(aff_i, aff).
1897 * This function is used to detect min/max expressions.
1898 * If the ast_build_detect_min_max option is turned off, then
1899 * do not even try and perform any detection and return false instead.
1901 static isl_bool extends(struct isl_from_pw_aff_data *data,
1902 __isl_keep isl_set *set, __isl_keep isl_aff *aff,
1903 __isl_give isl_basic_set *(*test)(__isl_take isl_aff *aff1,
1904 __isl_take isl_aff *aff2))
1906 int i, n;
1907 isl_ctx *ctx;
1908 isl_set *dom;
1910 ctx = isl_ast_build_get_ctx(data->build);
1911 if (!isl_options_get_ast_build_detect_min_max(ctx))
1912 return isl_bool_false;
1914 dom = isl_ast_build_get_domain(data->build);
1915 set = isl_set_intersect(dom, isl_set_copy(set));
1917 n = isl_set_list_n_set(data->p[data->n].set_list);
1918 for (i = 0; i < n ; ++i) {
1919 isl_aff *aff_i;
1920 isl_set *valid;
1921 isl_set *dom, *required;
1922 isl_bool is_valid;
1924 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
1925 valid = isl_set_from_basic_set(test(isl_aff_copy(aff), aff_i));
1926 required = isl_set_list_get_set(data->p[data->n].set_list, i);
1927 dom = isl_ast_build_get_domain(data->build);
1928 required = isl_set_intersect(dom, required);
1929 is_valid = isl_set_is_subset(required, valid);
1930 isl_set_free(required);
1931 isl_set_free(valid);
1932 if (is_valid < 0 || !is_valid) {
1933 isl_set_free(set);
1934 return is_valid;
1937 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
1938 valid = isl_set_from_basic_set(test(aff_i, isl_aff_copy(aff)));
1939 is_valid = isl_set_is_subset(set, valid);
1940 isl_set_free(valid);
1941 if (is_valid < 0 || !is_valid) {
1942 isl_set_free(set);
1943 return is_valid;
1947 isl_set_free(set);
1948 return isl_bool_true;
1951 /* Can the list of pieces in "data" be extended with "set" and "aff"
1952 * to form/preserve a minimum expression?
1953 * In particular, is it the case for each entry (set_i, aff_i) that
1955 * aff >= aff_i on set_i, and
1956 * aff_i >= aff on set?
1958 static isl_bool extends_min(struct isl_from_pw_aff_data *data,
1959 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
1961 return extends(data, set, aff, &isl_aff_ge_basic_set);
1964 /* Can the list of pieces in "data" be extended with "set" and "aff"
1965 * to form/preserve a maximum expression?
1966 * In particular, is it the case for each entry (set_i, aff_i) that
1968 * aff <= aff_i on set_i, and
1969 * aff_i <= aff on set?
1971 static isl_bool extends_max(struct isl_from_pw_aff_data *data,
1972 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
1974 return extends(data, set, aff, &isl_aff_le_basic_set);
1977 /* This function is called during the construction of an isl_ast_expr
1978 * that evaluates an isl_pw_aff.
1979 * If the last piece of "data" contains either a single subpiece
1980 * or a minimum, then check if this minimum expression can be extended
1981 * with (set, aff).
1982 * If so, extend the sequence and return.
1983 * Perform the same operation for maximum expressions.
1984 * If no such extension can be performed, then move to the next piece
1985 * in "data" (if the current piece contains any data), and then store
1986 * the current subpiece in the current piece of "data" for later handling.
1988 static isl_stat ast_expr_from_pw_aff(__isl_take isl_set *set,
1989 __isl_take isl_aff *aff, void *user)
1991 struct isl_from_pw_aff_data *data = user;
1992 isl_bool test;
1993 enum isl_from_pw_aff_state state;
1995 state = data->p[data->n].state;
1996 if (state == isl_state_single || state == isl_state_min) {
1997 test = extends_min(data, set, aff);
1998 if (test < 0)
1999 goto error;
2000 if (test)
2001 return extend_min(data, set, aff);
2003 if (state == isl_state_single || state == isl_state_max) {
2004 test = extends_max(data, set, aff);
2005 if (test < 0)
2006 goto error;
2007 if (test)
2008 return extend_max(data, set, aff);
2010 if (state != isl_state_none)
2011 data->n++;
2012 set_single(data, set, aff);
2014 return isl_stat_ok;
2015 error:
2016 isl_set_free(set);
2017 isl_aff_free(aff);
2018 return isl_stat_error;
2021 /* Construct an isl_ast_expr that evaluates "pa".
2022 * The result is simplified in terms of build->domain.
2024 * The domain of "pa" lives in the internal schedule space.
2026 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff_internal(
2027 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2029 struct isl_from_pw_aff_data data = { NULL };
2030 isl_ast_expr *res = NULL;
2032 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
2033 pa = isl_pw_aff_coalesce(pa);
2034 if (!pa)
2035 return NULL;
2037 if (isl_from_pw_aff_data_init(&data, build, pa) < 0)
2038 goto error;
2039 set_none(&data);
2041 if (isl_pw_aff_foreach_piece(pa, &ast_expr_from_pw_aff, &data) >= 0)
2042 res = build_pieces(&data);
2044 isl_pw_aff_free(pa);
2045 isl_from_pw_aff_data_clear(&data);
2046 return res;
2047 error:
2048 isl_pw_aff_free(pa);
2049 isl_from_pw_aff_data_clear(&data);
2050 return NULL;
2053 /* Construct an isl_ast_expr that evaluates "pa".
2054 * The result is simplified in terms of build->domain.
2056 * The domain of "pa" lives in the external schedule space.
2058 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff(
2059 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2061 isl_ast_expr *expr;
2063 if (isl_ast_build_need_schedule_map(build)) {
2064 isl_multi_aff *ma;
2065 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2066 pa = isl_pw_aff_pullback_multi_aff(pa, ma);
2068 expr = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2069 return expr;
2072 /* Set the ids of the input dimensions of "mpa" to the iterator ids
2073 * of "build".
2075 * The domain of "mpa" is assumed to live in the internal schedule domain.
2077 static __isl_give isl_multi_pw_aff *set_iterator_names(
2078 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2080 int i, n;
2082 n = isl_multi_pw_aff_dim(mpa, isl_dim_in);
2083 for (i = 0; i < n; ++i) {
2084 isl_id *id;
2086 id = isl_ast_build_get_iterator_id(build, i);
2087 mpa = isl_multi_pw_aff_set_dim_id(mpa, isl_dim_in, i, id);
2090 return mpa;
2093 /* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
2094 * the remaining arguments derived from "mpa".
2095 * That is, construct a call or access expression that calls/accesses "arg0"
2096 * with arguments/indices specified by "mpa".
2098 static __isl_give isl_ast_expr *isl_ast_build_with_arguments(
2099 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2100 __isl_take isl_ast_expr *arg0, __isl_take isl_multi_pw_aff *mpa)
2102 int i, n;
2103 isl_ctx *ctx;
2104 isl_ast_expr *expr;
2106 ctx = isl_ast_build_get_ctx(build);
2108 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
2109 expr = isl_ast_expr_alloc_op(ctx, type, 1 + n);
2110 expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
2111 for (i = 0; i < n; ++i) {
2112 isl_pw_aff *pa;
2113 isl_ast_expr *arg;
2115 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
2116 arg = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2117 expr = isl_ast_expr_set_op_arg(expr, 1 + i, arg);
2120 isl_multi_pw_aff_free(mpa);
2121 return expr;
2124 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2125 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2126 __isl_take isl_multi_pw_aff *mpa);
2128 /* Construct an isl_ast_expr that accesses the member specified by "mpa".
2129 * The range of "mpa" is assumed to be wrapped relation.
2130 * The domain of this wrapped relation specifies the structure being
2131 * accessed, while the range of this wrapped relation spacifies the
2132 * member of the structure being accessed.
2134 * The domain of "mpa" is assumed to live in the internal schedule domain.
2136 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_member(
2137 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2139 isl_id *id;
2140 isl_multi_pw_aff *domain;
2141 isl_ast_expr *domain_expr, *expr;
2142 enum isl_ast_op_type type = isl_ast_op_access;
2144 domain = isl_multi_pw_aff_copy(mpa);
2145 domain = isl_multi_pw_aff_range_factor_domain(domain);
2146 domain_expr = isl_ast_build_from_multi_pw_aff_internal(build,
2147 type, domain);
2148 mpa = isl_multi_pw_aff_range_factor_range(mpa);
2149 if (!isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2150 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2151 "missing field name", goto error);
2152 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2153 expr = isl_ast_expr_from_id(id);
2154 expr = isl_ast_expr_alloc_binary(isl_ast_op_member, domain_expr, expr);
2155 return isl_ast_build_with_arguments(build, type, expr, mpa);
2156 error:
2157 isl_multi_pw_aff_free(mpa);
2158 return NULL;
2161 /* Construct an isl_ast_expr of type "type" that calls or accesses
2162 * the element specified by "mpa".
2163 * The first argument is obtained from the output tuple name.
2164 * The remaining arguments are given by the piecewise affine expressions.
2166 * If the range of "mpa" is a mapped relation, then we assume it
2167 * represents an access to a member of a structure.
2169 * The domain of "mpa" is assumed to live in the internal schedule domain.
2171 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2172 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2173 __isl_take isl_multi_pw_aff *mpa)
2175 isl_ctx *ctx;
2176 isl_id *id;
2177 isl_ast_expr *expr;
2179 if (!mpa)
2180 goto error;
2182 if (type == isl_ast_op_access &&
2183 isl_multi_pw_aff_range_is_wrapping(mpa))
2184 return isl_ast_build_from_multi_pw_aff_member(build, mpa);
2186 mpa = set_iterator_names(build, mpa);
2187 if (!build || !mpa)
2188 goto error;
2190 ctx = isl_ast_build_get_ctx(build);
2192 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2193 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2194 else
2195 id = isl_id_alloc(ctx, "", NULL);
2197 expr = isl_ast_expr_from_id(id);
2198 return isl_ast_build_with_arguments(build, type, expr, mpa);
2199 error:
2200 isl_multi_pw_aff_free(mpa);
2201 return NULL;
2204 /* Construct an isl_ast_expr of type "type" that calls or accesses
2205 * the element specified by "pma".
2206 * The first argument is obtained from the output tuple name.
2207 * The remaining arguments are given by the piecewise affine expressions.
2209 * The domain of "pma" is assumed to live in the internal schedule domain.
2211 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff_internal(
2212 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2213 __isl_take isl_pw_multi_aff *pma)
2215 isl_multi_pw_aff *mpa;
2217 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2218 return isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2221 /* Construct an isl_ast_expr of type "type" that calls or accesses
2222 * the element specified by "mpa".
2223 * The first argument is obtained from the output tuple name.
2224 * The remaining arguments are given by the piecewise affine expressions.
2226 * The domain of "mpa" is assumed to live in the external schedule domain.
2228 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff(
2229 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2230 __isl_take isl_multi_pw_aff *mpa)
2232 int is_domain;
2233 isl_ast_expr *expr;
2234 isl_space *space_build, *space_mpa;
2236 space_build = isl_ast_build_get_space(build, 0);
2237 space_mpa = isl_multi_pw_aff_get_space(mpa);
2238 is_domain = isl_space_tuple_is_equal(space_build, isl_dim_set,
2239 space_mpa, isl_dim_in);
2240 isl_space_free(space_build);
2241 isl_space_free(space_mpa);
2242 if (is_domain < 0)
2243 goto error;
2244 if (!is_domain)
2245 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2246 "spaces don't match", goto error);
2248 if (isl_ast_build_need_schedule_map(build)) {
2249 isl_multi_aff *ma;
2250 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2251 mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
2254 expr = isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2255 return expr;
2256 error:
2257 isl_multi_pw_aff_free(mpa);
2258 return NULL;
2261 /* Construct an isl_ast_expr that calls the domain element specified by "mpa".
2262 * The name of the function is obtained from the output tuple name.
2263 * The arguments are given by the piecewise affine expressions.
2265 * The domain of "mpa" is assumed to live in the external schedule domain.
2267 __isl_give isl_ast_expr *isl_ast_build_call_from_multi_pw_aff(
2268 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2270 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_call, mpa);
2273 /* Construct an isl_ast_expr that accesses the array element specified by "mpa".
2274 * The name of the array is obtained from the output tuple name.
2275 * The index expressions are given by the piecewise affine expressions.
2277 * The domain of "mpa" is assumed to live in the external schedule domain.
2279 __isl_give isl_ast_expr *isl_ast_build_access_from_multi_pw_aff(
2280 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2282 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_access, mpa);
2285 /* Construct an isl_ast_expr of type "type" that calls or accesses
2286 * the element specified by "pma".
2287 * The first argument is obtained from the output tuple name.
2288 * The remaining arguments are given by the piecewise affine expressions.
2290 * The domain of "pma" is assumed to live in the external schedule domain.
2292 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff(
2293 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2294 __isl_take isl_pw_multi_aff *pma)
2296 isl_multi_pw_aff *mpa;
2298 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2299 return isl_ast_build_from_multi_pw_aff(build, type, mpa);
2302 /* Construct an isl_ast_expr that calls the domain element specified by "pma".
2303 * The name of the function is obtained from the output tuple name.
2304 * The arguments are given by the piecewise affine expressions.
2306 * The domain of "pma" is assumed to live in the external schedule domain.
2308 __isl_give isl_ast_expr *isl_ast_build_call_from_pw_multi_aff(
2309 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2311 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_call, pma);
2314 /* Construct an isl_ast_expr that accesses the array element specified by "pma".
2315 * The name of the array is obtained from the output tuple name.
2316 * The index expressions are given by the piecewise affine expressions.
2318 * The domain of "pma" is assumed to live in the external schedule domain.
2320 __isl_give isl_ast_expr *isl_ast_build_access_from_pw_multi_aff(
2321 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2323 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_access, pma);
2326 /* Construct an isl_ast_expr that calls the domain element
2327 * specified by "executed".
2329 * "executed" is assumed to be single-valued, with a domain that lives
2330 * in the internal schedule space.
2332 __isl_give isl_ast_node *isl_ast_build_call_from_executed(
2333 __isl_keep isl_ast_build *build, __isl_take isl_map *executed)
2335 isl_pw_multi_aff *iteration;
2336 isl_ast_expr *expr;
2338 iteration = isl_pw_multi_aff_from_map(executed);
2339 iteration = isl_ast_build_compute_gist_pw_multi_aff(build, iteration);
2340 iteration = isl_pw_multi_aff_intersect_domain(iteration,
2341 isl_ast_build_get_domain(build));
2342 expr = isl_ast_build_from_pw_multi_aff_internal(build, isl_ast_op_call,
2343 iteration);
2344 return isl_ast_node_alloc_user(expr);