isl_ast_codegen.c: add_domain: only take single copy of inverse schedule
[isl.git] / isl_ast_build_expr.c
blob43b731b08fa1031bed6e7eda340768f6b95ed776
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/id.h>
14 #include <isl/space.h>
15 #include <isl/constraint.h>
16 #include <isl/ilp.h>
17 #include <isl/val.h>
18 #include <isl_ast_build_expr.h>
19 #include <isl_ast_private.h>
20 #include <isl_ast_build_private.h>
21 #include <isl_sort.h>
23 /* Compute the "opposite" of the (numerator of the) argument of a div
24 * with denominator "d".
26 * In particular, compute
28 * -aff + (d - 1)
30 static __isl_give isl_aff *oppose_div_arg(__isl_take isl_aff *aff,
31 __isl_take isl_val *d)
33 aff = isl_aff_neg(aff);
34 aff = isl_aff_add_constant_val(aff, d);
35 aff = isl_aff_add_constant_si(aff, -1);
37 return aff;
40 /* Internal data structure used inside isl_ast_expr_add_term.
41 * The domain of "build" is used to simplify the expressions.
42 * "build" needs to be set by the caller of isl_ast_expr_add_term.
43 * "ls" is the domain local space of the affine expression
44 * of which a term is being added.
45 * "cst" is the constant term of the expression in which the added term
46 * appears. It may be modified by isl_ast_expr_add_term.
48 * "v" is the coefficient of the term that is being constructed and
49 * is set internally by isl_ast_expr_add_term.
51 struct isl_ast_add_term_data {
52 isl_ast_build *build;
53 isl_local_space *ls;
54 isl_val *cst;
55 isl_val *v;
58 /* Given the numerator "aff" of the argument of an integer division
59 * with denominator "d", check if it can be made non-negative over
60 * data->build->domain by stealing part of the constant term of
61 * the expression in which the integer division appears.
63 * In particular, the outer expression is of the form
65 * v * floor(aff/d) + cst
67 * We already know that "aff" itself may attain negative values.
68 * Here we check if aff + d*floor(cst/v) is non-negative, such
69 * that we could rewrite the expression to
71 * v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
73 * Note that aff + d*floor(cst/v) can only possibly be non-negative
74 * if data->cst and data->v have the same sign.
75 * Similarly, if floor(cst/v) is zero, then there is no point in
76 * checking again.
78 static isl_bool is_non_neg_after_stealing(__isl_keep isl_aff *aff,
79 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
81 isl_aff *shifted;
82 isl_val *shift;
83 isl_bool is_zero;
84 isl_bool non_neg;
86 if (isl_val_sgn(data->cst) != isl_val_sgn(data->v))
87 return isl_bool_false;
89 shift = isl_val_div(isl_val_copy(data->cst), isl_val_copy(data->v));
90 shift = isl_val_floor(shift);
91 is_zero = isl_val_is_zero(shift);
92 if (is_zero < 0 || is_zero) {
93 isl_val_free(shift);
94 return isl_bool_not(is_zero);
96 shift = isl_val_mul(shift, isl_val_copy(d));
97 shifted = isl_aff_copy(aff);
98 shifted = isl_aff_add_constant_val(shifted, shift);
99 non_neg = isl_ast_build_aff_is_nonneg(data->build, shifted);
100 isl_aff_free(shifted);
102 return non_neg;
105 /* Given the numerator "aff" of the argument of an integer division
106 * with denominator "d", steal part of the constant term of
107 * the expression in which the integer division appears to make it
108 * non-negative over data->build->domain.
110 * In particular, the outer expression is of the form
112 * v * floor(aff/d) + cst
114 * We know that "aff" itself may attain negative values,
115 * but that aff + d*floor(cst/v) is non-negative.
116 * Find the minimal positive value that we need to add to "aff"
117 * to make it positive and adjust data->cst accordingly.
118 * That is, compute the minimal value "m" of "aff" over
119 * data->build->domain and take
121 * s = ceil(-m/d)
123 * such that
125 * aff + d * s >= 0
127 * and rewrite the expression to
129 * v * floor((aff + s*d)/d) + (cst - v*s)
131 static __isl_give isl_aff *steal_from_cst(__isl_take isl_aff *aff,
132 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
134 isl_set *domain;
135 isl_val *shift, *t;
137 domain = isl_ast_build_get_domain(data->build);
138 shift = isl_set_min_val(domain, aff);
139 isl_set_free(domain);
141 shift = isl_val_neg(shift);
142 shift = isl_val_div(shift, isl_val_copy(d));
143 shift = isl_val_ceil(shift);
145 t = isl_val_copy(shift);
146 t = isl_val_mul(t, isl_val_copy(data->v));
147 data->cst = isl_val_sub(data->cst, t);
149 shift = isl_val_mul(shift, isl_val_copy(d));
150 return isl_aff_add_constant_val(aff, shift);
153 /* Construct an expression representing the binary operation "type"
154 * (some division or modulo) applied to the expressions
155 * constructed from "aff" and "v".
157 static __isl_give isl_ast_expr *div_mod(enum isl_ast_expr_op_type type,
158 __isl_take isl_aff *aff, __isl_take isl_val *v,
159 __isl_keep isl_ast_build *build)
161 isl_ast_expr *expr1, *expr2;
163 expr1 = isl_ast_expr_from_aff(aff, build);
164 expr2 = isl_ast_expr_from_val(v);
165 return isl_ast_expr_alloc_binary(type, expr1, expr2);
168 /* Create an isl_ast_expr evaluating the div at position "pos" in data->ls.
169 * The result is simplified in terms of data->build->domain.
170 * This function may change (the sign of) data->v.
172 * data->ls is known to be non-NULL.
174 * Let the div be of the form floor(e/d).
175 * If the ast_build_prefer_pdiv option is set then we check if "e"
176 * is non-negative, so that we can generate
178 * (pdiv_q, expr(e), expr(d))
180 * instead of
182 * (fdiv_q, expr(e), expr(d))
184 * If the ast_build_prefer_pdiv option is set and
185 * if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
186 * If so, we can rewrite
188 * floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
190 * and still use pdiv_q, while changing the sign of data->v.
192 * Otherwise, we check if
194 * e + d*floor(cst/v)
196 * is non-negative and if so, replace floor(e/d) by
198 * floor((e + s*d)/d) - s
200 * with s the minimal shift that makes the argument non-negative.
202 static __isl_give isl_ast_expr *var_div(struct isl_ast_add_term_data *data,
203 int pos)
205 isl_ctx *ctx = isl_local_space_get_ctx(data->ls);
206 isl_aff *aff;
207 isl_val *d;
208 enum isl_ast_expr_op_type type;
210 aff = isl_local_space_get_div(data->ls, pos);
211 d = isl_aff_get_denominator_val(aff);
212 aff = isl_aff_scale_val(aff, isl_val_copy(d));
214 type = isl_ast_expr_op_fdiv_q;
215 if (isl_options_get_ast_build_prefer_pdiv(ctx)) {
216 isl_bool non_neg;
217 non_neg = isl_ast_build_aff_is_nonneg(data->build, aff);
218 if (non_neg >= 0 && !non_neg) {
219 isl_aff *opp = oppose_div_arg(isl_aff_copy(aff),
220 isl_val_copy(d));
221 non_neg = isl_ast_build_aff_is_nonneg(data->build, opp);
222 if (non_neg >= 0 && non_neg) {
223 data->v = isl_val_neg(data->v);
224 isl_aff_free(aff);
225 aff = opp;
226 } else
227 isl_aff_free(opp);
229 if (non_neg >= 0 && !non_neg) {
230 non_neg = is_non_neg_after_stealing(aff, d, data);
231 if (non_neg >= 0 && non_neg)
232 aff = steal_from_cst(aff, d, data);
234 if (non_neg < 0)
235 aff = isl_aff_free(aff);
236 else if (non_neg)
237 type = isl_ast_expr_op_pdiv_q;
240 return div_mod(type, aff, d, data->build);
243 /* Create an isl_ast_expr evaluating the specified dimension of data->ls.
244 * The result is simplified in terms of data->build->domain.
245 * This function may change (the sign of) data->v.
247 * The isl_ast_expr is constructed based on the type of the dimension.
248 * - divs are constructed by var_div
249 * - set variables are constructed from the iterator isl_ids in data->build
250 * - parameters are constructed from the isl_ids in data->ls
252 static __isl_give isl_ast_expr *var(struct isl_ast_add_term_data *data,
253 enum isl_dim_type type, int pos)
255 isl_ctx *ctx = isl_local_space_get_ctx(data->ls);
256 isl_id *id;
258 if (type == isl_dim_div)
259 return var_div(data, pos);
261 if (type == isl_dim_set) {
262 id = isl_ast_build_get_iterator_id(data->build, pos);
263 return isl_ast_expr_from_id(id);
266 if (!isl_local_space_has_dim_id(data->ls, type, pos))
267 isl_die(ctx, isl_error_internal, "unnamed dimension",
268 return NULL);
269 id = isl_local_space_get_dim_id(data->ls, type, pos);
270 return isl_ast_expr_from_id(id);
273 /* Does "expr" represent the zero integer?
275 static isl_bool ast_expr_is_zero(__isl_keep isl_ast_expr *expr)
277 if (!expr)
278 return isl_bool_error;
279 if (expr->type != isl_ast_expr_int)
280 return isl_bool_false;
281 return isl_val_is_zero(expr->u.v);
284 /* Create an expression representing the sum of "expr1" and "expr2",
285 * provided neither of the two expressions is identically zero.
287 static __isl_give isl_ast_expr *ast_expr_add(__isl_take isl_ast_expr *expr1,
288 __isl_take isl_ast_expr *expr2)
290 if (!expr1 || !expr2)
291 goto error;
293 if (ast_expr_is_zero(expr1)) {
294 isl_ast_expr_free(expr1);
295 return expr2;
298 if (ast_expr_is_zero(expr2)) {
299 isl_ast_expr_free(expr2);
300 return expr1;
303 return isl_ast_expr_add(expr1, expr2);
304 error:
305 isl_ast_expr_free(expr1);
306 isl_ast_expr_free(expr2);
307 return NULL;
310 /* Subtract expr2 from expr1.
312 * If expr2 is zero, we simply return expr1.
313 * If expr1 is zero, we return
315 * (isl_ast_expr_op_minus, expr2)
317 * Otherwise, we return
319 * (isl_ast_expr_op_sub, expr1, expr2)
321 static __isl_give isl_ast_expr *ast_expr_sub(__isl_take isl_ast_expr *expr1,
322 __isl_take isl_ast_expr *expr2)
324 if (!expr1 || !expr2)
325 goto error;
327 if (ast_expr_is_zero(expr2)) {
328 isl_ast_expr_free(expr2);
329 return expr1;
332 if (ast_expr_is_zero(expr1)) {
333 isl_ast_expr_free(expr1);
334 return isl_ast_expr_neg(expr2);
337 return isl_ast_expr_sub(expr1, expr2);
338 error:
339 isl_ast_expr_free(expr1);
340 isl_ast_expr_free(expr2);
341 return NULL;
344 /* Return an isl_ast_expr that represents
346 * v * (aff mod d)
348 * v is assumed to be non-negative.
349 * The result is simplified in terms of build->domain.
351 static __isl_give isl_ast_expr *isl_ast_expr_mod(__isl_keep isl_val *v,
352 __isl_keep isl_aff *aff, __isl_keep isl_val *d,
353 __isl_keep isl_ast_build *build)
355 isl_ast_expr *expr;
356 isl_ast_expr *c;
358 if (!aff)
359 return NULL;
361 expr = div_mod(isl_ast_expr_op_pdiv_r,
362 isl_aff_copy(aff), isl_val_copy(d), build);
364 if (!isl_val_is_one(v)) {
365 c = isl_ast_expr_from_val(isl_val_copy(v));
366 expr = isl_ast_expr_mul(c, expr);
369 return expr;
372 /* Create an isl_ast_expr that scales "expr" by "v".
374 * If v is 1, we simply return expr.
375 * If v is -1, we return
377 * (isl_ast_expr_op_minus, expr)
379 * Otherwise, we return
381 * (isl_ast_expr_op_mul, expr(v), expr)
383 static __isl_give isl_ast_expr *scale(__isl_take isl_ast_expr *expr,
384 __isl_take isl_val *v)
386 isl_ast_expr *c;
388 if (!expr || !v)
389 goto error;
390 if (isl_val_is_one(v)) {
391 isl_val_free(v);
392 return expr;
395 if (isl_val_is_negone(v)) {
396 isl_val_free(v);
397 expr = isl_ast_expr_neg(expr);
398 } else {
399 c = isl_ast_expr_from_val(v);
400 expr = isl_ast_expr_mul(c, expr);
403 return expr;
404 error:
405 isl_val_free(v);
406 isl_ast_expr_free(expr);
407 return NULL;
410 /* Add an expression for "*v" times the specified dimension of data->ls
411 * to expr.
412 * If the dimension is an integer division, then this function
413 * may modify data->cst in order to make the numerator non-negative.
414 * The result is simplified in terms of data->build->domain.
416 * Let e be the expression for the specified dimension,
417 * multiplied by the absolute value of "*v".
418 * If "*v" is negative, we create
420 * (isl_ast_expr_op_sub, expr, e)
422 * except when expr is trivially zero, in which case we create
424 * (isl_ast_expr_op_minus, e)
426 * instead.
428 * If "*v" is positive, we simply create
430 * (isl_ast_expr_op_add, expr, e)
433 static __isl_give isl_ast_expr *isl_ast_expr_add_term(
434 __isl_take isl_ast_expr *expr, enum isl_dim_type type, int pos,
435 __isl_take isl_val *v, struct isl_ast_add_term_data *data)
437 isl_ast_expr *term;
439 if (!expr)
440 return NULL;
442 data->v = v;
443 term = var(data, type, pos);
444 v = data->v;
446 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
447 v = isl_val_neg(v);
448 term = scale(term, v);
449 return ast_expr_sub(expr, term);
450 } else {
451 term = scale(term, v);
452 return ast_expr_add(expr, term);
456 /* Add an expression for "v" to expr.
458 static __isl_give isl_ast_expr *isl_ast_expr_add_int(
459 __isl_take isl_ast_expr *expr, __isl_take isl_val *v)
461 isl_ast_expr *expr_int;
463 if (!expr || !v)
464 goto error;
466 if (isl_val_is_zero(v)) {
467 isl_val_free(v);
468 return expr;
471 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
472 v = isl_val_neg(v);
473 expr_int = isl_ast_expr_from_val(v);
474 return ast_expr_sub(expr, expr_int);
475 } else {
476 expr_int = isl_ast_expr_from_val(v);
477 return ast_expr_add(expr, expr_int);
479 error:
480 isl_ast_expr_free(expr);
481 isl_val_free(v);
482 return NULL;
485 /* Internal data structure used inside extract_modulos.
487 * If any modulo expressions are detected in "aff", then the
488 * expression is removed from "aff" and added to either "pos" or "neg"
489 * depending on the sign of the coefficient of the modulo expression
490 * inside "aff".
492 * "add" is an expression that needs to be added to "aff" at the end of
493 * the computation. It is NULL as long as no modulos have been extracted.
495 * "i" is the position in "aff" of the div under investigation
496 * "v" is the coefficient in "aff" of the div
497 * "div" is the argument of the div, with the denominator removed
498 * "d" is the original denominator of the argument of the div
500 * If set, then "partial" is the (positively weighted) sum
501 * of the affine expressions of one or more previously considered constraints
502 * that could still be complemented to an expression equal to "div".
503 * "nonneg" is an affine expression that is non-negative over "build"
504 * and that can be used to extract a modulo expression from "div".
505 * In particular, if "sign" is 1, then the coefficients of "nonneg"
506 * are equal to those of "div" modulo "d". If "sign" is -1, then
507 * the coefficients of "nonneg" are opposite to those of "div" modulo "d".
508 * If "sign" is 0, then no such affine expression has been found (yet).
510 struct isl_extract_mod_data {
511 isl_ast_build *build;
512 isl_aff *aff;
514 isl_ast_expr *pos;
515 isl_ast_expr *neg;
517 isl_aff *add;
519 int i;
520 isl_val *v;
521 isl_val *d;
522 isl_aff *div;
524 isl_aff *partial;
525 isl_aff *nonneg;
526 int sign;
529 /* Does
531 * arg mod data->d
533 * represent (a special case of) a test for some linear expression
534 * being even?
536 * In particular, is it of the form
538 * (lin - 1) mod 2
542 static isl_bool is_even_test(struct isl_extract_mod_data *data,
543 __isl_keep isl_aff *arg)
545 isl_bool res;
546 isl_val *cst;
548 res = isl_val_eq_si(data->d, 2);
549 if (res < 0 || !res)
550 return res;
552 cst = isl_aff_get_constant_val(arg);
553 res = isl_val_eq_si(cst, -1);
554 isl_val_free(cst);
556 return res;
559 /* Given that data->v * div_i in data->aff is equal to
561 * f * (term - (arg mod d))
563 * with data->d * f = data->v and "arg" non-negative on data->build, add
565 * f * term
567 * to data->add and
569 * abs(f) * (arg mod d)
571 * to data->neg or data->pos depending on the sign of -f.
573 * In the special case that "arg mod d" is of the form "(lin - 1) mod 2",
574 * with "lin" some linear expression, first replace
576 * f * (term - ((lin - 1) mod 2))
578 * by
580 * -f * (1 - term - (lin mod 2))
582 * These two are equal because
584 * ((lin - 1) mod 2) + (lin mod 2) = 1
586 * Also, if "lin - 1" is non-negative, then "lin" is non-negative too.
588 static isl_stat extract_term_and_mod(struct isl_extract_mod_data *data,
589 __isl_take isl_aff *term, __isl_take isl_aff *arg)
591 isl_bool even;
592 isl_ast_expr *expr;
593 int s;
595 even = is_even_test(data, arg);
596 if (even < 0) {
597 arg = isl_aff_free(arg);
598 } else if (even) {
599 term = oppose_div_arg(term, isl_val_copy(data->d));
600 data->v = isl_val_neg(data->v);
601 arg = isl_aff_set_constant_si(arg, 0);
604 data->v = isl_val_div(data->v, isl_val_copy(data->d));
605 s = isl_val_sgn(data->v);
606 data->v = isl_val_abs(data->v);
607 expr = isl_ast_expr_mod(data->v, arg, data->d, data->build);
608 isl_aff_free(arg);
609 if (s > 0)
610 data->neg = ast_expr_add(data->neg, expr);
611 else
612 data->pos = ast_expr_add(data->pos, expr);
613 data->aff = isl_aff_set_coefficient_si(data->aff,
614 isl_dim_div, data->i, 0);
615 if (s < 0)
616 data->v = isl_val_neg(data->v);
617 term = isl_aff_scale_val(term, isl_val_copy(data->v));
619 if (!data->add)
620 data->add = term;
621 else
622 data->add = isl_aff_add(data->add, term);
623 if (!data->add)
624 return isl_stat_error;
626 return isl_stat_ok;
629 /* Given that data->v * div_i in data->aff is of the form
631 * f * d * floor(div/d)
633 * with div nonnegative on data->build, rewrite it as
635 * f * (div - (div mod d)) = f * div - f * (div mod d)
637 * and add
639 * f * div
641 * to data->add and
643 * abs(f) * (div mod d)
645 * to data->neg or data->pos depending on the sign of -f.
647 static isl_stat extract_mod(struct isl_extract_mod_data *data)
649 return extract_term_and_mod(data, isl_aff_copy(data->div),
650 isl_aff_copy(data->div));
653 /* Given that data->v * div_i in data->aff is of the form
655 * f * d * floor(div/d) (1)
657 * check if div is non-negative on data->build and, if so,
658 * extract the corresponding modulo from data->aff.
659 * If not, then check if
661 * -div + d - 1
663 * is non-negative on data->build. If so, replace (1) by
665 * -f * d * floor((-div + d - 1)/d)
667 * and extract the corresponding modulo from data->aff.
669 * This function may modify data->div.
671 static isl_stat extract_nonneg_mod(struct isl_extract_mod_data *data)
673 isl_bool mod;
675 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
676 if (mod < 0)
677 goto error;
678 if (mod)
679 return extract_mod(data);
681 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
682 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
683 if (mod < 0)
684 goto error;
685 if (mod) {
686 data->v = isl_val_neg(data->v);
687 return extract_mod(data);
690 return isl_stat_ok;
691 error:
692 data->aff = isl_aff_free(data->aff);
693 return isl_stat_error;
696 /* Does "c" have a constant term that is "too large"?
697 * Here, "too large" is fairly arbitrarily set to 1 << 15.
699 static isl_bool has_large_constant_term(__isl_keep isl_constraint *c)
701 isl_val *v;
702 int sign;
704 v = isl_val_abs(isl_constraint_get_constant_val(c));
705 if (!v)
706 return isl_bool_error;
707 sign = isl_val_cmp_si(v, 1 << 15);
708 isl_val_free(v);
709 return isl_bool_ok(sign > 0);
712 /* Is the affine expression with constant term returned by "get_constant"
713 * "simpler" than data->nonneg
714 * for use in extracting a modulo expression?
716 * Currently, only this constant term is considered.
717 * In particular, we prefer the affine expression with the smallest constant
718 * term.
719 * This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
720 * then we would pick x >= 0
722 * More detailed heuristics could be used if it turns out that there is a need.
724 static isl_bool is_simpler(struct isl_extract_mod_data *data,
725 __isl_give isl_val *get_constant(struct isl_extract_mod_data *data,
726 void *user), void *user)
728 isl_val *v1, *v2;
729 isl_bool simpler;
731 if (!data->nonneg)
732 return isl_bool_true;
734 v1 = isl_val_abs(get_constant(data, user));
735 v2 = isl_val_abs(isl_aff_get_constant_val(data->nonneg));
736 simpler = isl_val_lt(v1, v2);
737 isl_val_free(v1);
738 isl_val_free(v2);
740 return simpler;
743 /* Return the constant term of "c".
745 static __isl_give isl_val *get_constraint_constant(
746 struct isl_extract_mod_data *data, void *user)
748 isl_constraint *c = user;
750 return isl_constraint_get_constant_val(c);
753 /* Is the affine expression of constraint "c" "simpler" than data->nonneg
754 * for use in extracting a modulo expression?
756 * The test is based on the constant term of "c".
758 static isl_bool mod_constraint_is_simpler(struct isl_extract_mod_data *data,
759 __isl_keep isl_constraint *c)
761 return is_simpler(data, &get_constraint_constant, c);
764 /* Replace data->nonneg by the affine expression "aff" and
765 * set data->sign to "sign".
767 static isl_stat replace_nonneg(struct isl_extract_mod_data *data,
768 __isl_take isl_aff *aff, int sign)
770 isl_aff_free(data->nonneg);
771 data->nonneg = aff;
772 data->sign = sign;
774 return isl_stat_non_null(data->nonneg);
777 /* If "c" is "simpler" than data->nonneg,
778 * then replace data->nonneg by the affine expression of "c" and
779 * set data->sign to "sign".
781 static isl_stat replace_if_simpler(struct isl_extract_mod_data *data,
782 __isl_keep isl_constraint *c, int sign)
784 isl_bool simpler;
786 simpler = mod_constraint_is_simpler(data, c);
787 if (simpler < 0 || !simpler)
788 return isl_stat_non_error_bool(simpler);
790 return replace_nonneg(data, isl_constraint_get_aff(c), sign);
793 /* Internal data structure used inside check_parallel_or_opposite.
795 * "data" is the information passed down from the caller.
796 * "c" is the constraint being inspected.
798 * "n" contains the number of parameters and the number of input dimensions and
799 * is set by the first call to parallel_or_opposite_scan.
800 * "parallel" is set as long as the coefficients of "c" are still potentially
801 * equal to those of data->div modulo data->d.
802 * "opposite" is set as long as the coefficients of "c" are still potentially
803 * opposite to those of data->div modulo data->d.
804 * "partial" is set if the coefficients of "c" are still potentially
805 * a subset of those of data->div.
806 * "final" is set is the coefficients in data->partial together with those
807 * of "c" still cover the coefficients of data->div.
809 * If "f" is set, then it is the factor with which the coefficients
810 * of "c" need to be multiplied to match those of data->div.
812 struct isl_parallel_stat {
813 struct isl_extract_mod_data *data;
814 isl_constraint *c;
816 isl_size n[2];
817 isl_bool parallel;
818 isl_bool opposite;
819 isl_bool partial;
820 int final;
822 isl_val *f;
825 /* Should the scan of coefficients be continued?
826 * That is, are the coefficients still (potentially) (partially) equal or
827 * opposite?
829 static isl_bool parallel_or_opposite_continue(struct isl_parallel_stat *stat)
831 if (stat->parallel < 0 || stat->opposite < 0 || stat->partial < 0)
832 return isl_bool_error;
834 return isl_bool_ok(stat->parallel || stat->opposite || stat->partial);
837 /* Is coefficient "i" of type "c_type" of stat->c potentially equal or
838 * opposite to coefficient "i" of type "a_type" of stat->data->div
839 * modulo stat->data->div?
840 * In particular, are they both zero or both non-zero?
842 * Note that while the coefficients of stat->data->div can be reasonably
843 * expected not to involve any coefficients that are multiples of stat->data->d,
844 * "c" may very well involve such coefficients.
845 * This means that some cases of equal or opposite constraints can be missed
846 * this way.
848 * If the coefficient of stat->data->div is zero, but that of "c" is not,
849 * then the coefficients of "c" cannot form a subset of those
850 * of stat->data->div.
851 * If the coefficient of stat->data->div is not zero,
852 * then check that it does not appear in both "c" and stat->data->partial.
853 * If it does not appear in either, then it must appear in some later constraint
854 * and "c" can therefore not be the last in the sequence of constraints
855 * that sum up to stat->data->div.
857 static isl_bool parallel_or_opposite_feasible(struct isl_parallel_stat *stat,
858 enum isl_dim_type c_type, enum isl_dim_type a_type, int i)
860 isl_bool a, b;
862 a = isl_constraint_involves_dims(stat->c, c_type, i, 1);
863 b = isl_aff_involves_dims(stat->data->div, a_type, i, 1);
864 if (a < 0 || b < 0)
865 return isl_bool_error;
866 if (a != b)
867 stat->parallel = stat->opposite = isl_bool_false;
868 if (!stat->partial)
869 return parallel_or_opposite_continue(stat);
870 if (!b && a)
871 stat->partial = isl_bool_false;
872 if (b && (a || stat->final) && stat->data->partial) {
873 isl_bool c;
875 c = isl_aff_involves_dims(stat->data->partial, a_type, i, 1);
876 if (c < 0)
877 return isl_bool_error;
878 if (a && c)
879 stat->partial = isl_bool_false;
880 if (!a && !c)
881 stat->final = 0;
884 return parallel_or_opposite_continue(stat);
887 /* Update stat->partial based on the coefficient "v1" of stat->c and
888 * "v2" of stat->data->div, where "v2" is known not to be zero.
889 * "v1" may be modified by this function and the modified value is returned.
890 * This function may also set stat->f.
892 * If "v1" is zero, then no update needs to be performed.
893 * Otherwise, stat->partial can only remain set if "c" is part
894 * of some positively weighted sum that is equal to stat->data->div.
895 * This means that v2 divided by v1 needs to be a positive integer.
896 * This quotient is stored in stat->f. If this quotient has already
897 * been set for a previous coefficient, then it needs to be the same.
899 static __isl_give isl_val *update_is_partial(struct isl_parallel_stat *stat,
900 __isl_take isl_val *v1, __isl_keep isl_val *v2)
902 if (!stat->partial)
903 return v1;
904 if (isl_val_is_zero(v1))
905 return v1;
907 stat->partial = isl_val_is_divisible_by(v2, v1);
908 if (stat->partial < 0 || !stat->partial)
909 return v1;
911 v1 = isl_val_div(isl_val_copy(v2), v1);
912 stat->partial = isl_val_is_pos(v1);
913 if (stat->partial < 0 || !stat->partial)
914 return v1;
915 if (!stat->f)
916 stat->f = isl_val_copy(v1);
917 stat->partial = isl_val_eq(v1, stat->f);
918 return v1;
921 /* Is coefficient "i" of type "c_type" of stat->c equal or
922 * opposite to coefficient "i" of type "a_type" of stat->data->div
923 * modulo stat->data->div, or
924 * could stat->c be part of a positively weighted sum equal to stat->data->div?
925 * This function may set stat->f (at most once).
927 * If the coefficient of stat->data->div is zero,
928 * then parallel_or_opposite_feasible has already checked
929 * that the coefficient of stat->c is zero as well,
930 * so no further checks are needed.
932 static isl_bool is_parallel_or_opposite(struct isl_parallel_stat *stat,
933 enum isl_dim_type c_type, enum isl_dim_type a_type, int i)
935 isl_val *v1, *v2;
936 isl_bool b;
938 b = isl_aff_involves_dims(stat->data->div, a_type, i, 1);
939 if (b < 0 || !b)
940 return isl_bool_not(b);
942 v1 = isl_constraint_get_coefficient_val(stat->c, c_type, i);
943 v2 = isl_aff_get_coefficient_val(stat->data->div, a_type, i);
944 if (stat->parallel) {
945 v1 = isl_val_sub(v1, isl_val_copy(v2));
946 stat->parallel = isl_val_is_divisible_by(v1, stat->data->d);
947 v1 = isl_val_add(v1, isl_val_copy(v2));
949 if (stat->opposite) {
950 v1 = isl_val_add(v1, isl_val_copy(v2));
951 stat->opposite = isl_val_is_divisible_by(v1, stat->data->d);
953 v1 = update_is_partial(stat, v1, v2);
954 isl_val_free(v1);
955 isl_val_free(v2);
957 return parallel_or_opposite_continue(stat);
960 /* Scan the coefficients of stat->c to see if they are (potentially)
961 * equal or opposite to those of stat->data->div modulo stat->data->d,
962 * calling "fn" on each coefficient.
963 * IF "init" is set, then this is the first call to this function and
964 * then stat->n is initialized.
966 static isl_bool parallel_or_opposite_scan(struct isl_parallel_stat *stat,
967 isl_bool (*fn)(struct isl_parallel_stat *stat,
968 enum isl_dim_type c_type, enum isl_dim_type a_type, int i),
969 int init)
971 enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
972 enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
973 int i, t;
975 for (t = 0; t < 2; ++t) {
976 if (init) {
977 stat->n[t] = isl_constraint_dim(stat->c, c_type[t]);
978 if (stat->n[t] < 0)
979 return isl_bool_error;
981 for (i = 0; i < stat->n[t]; ++i) {
982 isl_bool ok;
984 ok = fn(stat, c_type[t], a_type[t], i);
985 if (ok < 0 || !ok)
986 return ok;
990 return isl_bool_true;
993 /* Update stat->data->partial with stat->c.
995 * In particular, if stat->c with weight stat->f turns out
996 * to potentially be a part of a weighted sum equal to stat->data->div
997 * (i.e., stat->partial is set), then add this scaled version of stat->c
998 * to stat->data->partial or initialize stat->data->partial if it has not
999 * been set yet.
1001 static isl_stat update_partial(struct isl_parallel_stat *stat)
1003 isl_aff *aff;
1005 if (!stat->partial)
1006 return isl_stat_ok;
1008 aff = isl_constraint_get_aff(stat->c);
1009 aff = isl_aff_scale_val(aff, isl_val_copy(stat->f));
1010 if (!stat->data->partial)
1011 stat->data->partial = aff;
1012 else
1013 stat->data->partial = isl_aff_add(stat->data->partial, aff);
1015 return isl_stat_non_null(stat->data->partial);
1018 /* Return the constant term of data->partial.
1020 static __isl_give isl_val *get_partial_constant(
1021 struct isl_extract_mod_data *data, void *user)
1023 return isl_aff_get_constant_val(data->partial);
1026 /* Is the affine expression data->partial "simpler" than data->nonneg
1027 * for use in extracting a modulo expression?
1029 * The test is based on the constant term of data->partial.
1031 static isl_bool partial_is_simpler(struct isl_extract_mod_data *data)
1033 return is_simpler(data, &get_partial_constant, NULL);
1036 /* If stat->data->partial is complete and is "simpler" than data->nonneg,
1037 * then replace stat->data->nonneg by stat->data->partial.
1039 static isl_stat replace_by_partial_if_simpler(struct isl_parallel_stat *stat)
1041 isl_bool simpler;
1042 isl_aff *partial;
1044 if (!stat->final)
1045 return isl_stat_ok;
1047 simpler = partial_is_simpler(stat->data);
1048 if (simpler < 0 || !simpler)
1049 return isl_stat_non_error_bool(simpler);
1051 partial = stat->data->partial;
1052 stat->data->partial = NULL;
1054 return replace_nonneg(stat->data, partial, 1);
1057 /* Check if the coefficients of "c" are either equal or opposite to those
1058 * of data->div modulo data->d. If so, and if "c" is "simpler" than
1059 * data->nonneg, then replace data->nonneg by the affine expression of "c"
1060 * and set data->sign accordingly.
1061 * Also check if "c" is part of a positively weighted sum of constraints
1062 * that is equal to data->div, where each constraint has distinct non-zero
1063 * coefficients. If "c" is the last constraint in this sum
1064 * (and the sum is "simpler" than data->nonneg)
1065 * then also replace data->nonneg by this sum.
1066 * If "c" is equal or opposite to data->div, then it is not considered
1067 * to be part of a sum.
1069 * Both "c" and data->div are assumed not to involve any integer divisions.
1071 * Before we start the actual comparison, we first quickly check if
1072 * "c" and data->div have the same non-zero coefficients.
1073 * If not, then we assume that "c" is not of the desired form.
1075 * If the constant term is "too large", then the constraint is rejected.
1076 * We do this to avoid picking up constraints that bound a variable
1077 * by a very large number, say the largest or smallest possible
1078 * variable in the representation of some integer type.
1080 static isl_stat check_parallel_or_opposite(struct isl_extract_mod_data *data,
1081 __isl_keep isl_constraint *c)
1083 struct isl_parallel_stat stat = {
1084 .data = data,
1085 .c = c,
1086 .parallel = isl_bool_true,
1087 .opposite = isl_bool_true,
1088 .partial = isl_bool_true,
1089 .final = data->partial != NULL,
1090 .f = NULL,
1092 isl_bool skip, ok;
1094 ok = parallel_or_opposite_scan(&stat,
1095 &parallel_or_opposite_feasible, 1);
1096 if (ok < 0 || !ok)
1097 return isl_stat_non_error_bool(ok);
1099 skip = has_large_constant_term(c);
1100 if (skip < 0 || skip)
1101 return isl_stat_non_error_bool(skip);
1103 if (stat.parallel || stat.opposite)
1104 stat.partial = isl_bool_false;
1106 ok = parallel_or_opposite_scan(&stat, &is_parallel_or_opposite, 0);
1107 if (ok >= 0 && ok)
1108 if (update_partial(&stat) < 0)
1109 ok = isl_bool_error;
1110 isl_val_free(stat.f);
1111 if (ok < 0 || !ok)
1112 return isl_stat_non_error_bool(ok);
1114 if (stat.partial)
1115 return replace_by_partial_if_simpler(&stat);
1117 return replace_if_simpler(data, c, stat.parallel ? 1 : -1);
1120 /* Wrapper around check_parallel_or_opposite for use
1121 * as a isl_basic_set_foreach_constraint callback.
1123 static isl_stat check_parallel_or_opposite_wrap(__isl_take isl_constraint *c,
1124 void *user)
1126 struct isl_extract_mod_data *data = user;
1127 isl_stat res;
1129 res = check_parallel_or_opposite(data, c);
1130 isl_constraint_free(c);
1132 return res;
1135 /* Given that data->v * div_i in data->aff is of the form
1137 * f * d * floor(div/d) (1)
1139 * see if we can find an expression div' that is non-negative over data->build
1140 * and that is related to div through
1142 * div' = div + d * e
1144 * or
1146 * div' = -div + d - 1 + d * e
1148 * with e some affine expression.
1149 * If so, we write (1) as
1151 * f * div + f * (div' mod d)
1153 * or
1155 * -f * (-div + d - 1) - f * (div' mod d)
1157 * exploiting (in the second case) the fact that
1159 * f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
1162 * We first try to find an appropriate expression for div'
1163 * from the constraints of data->build->domain (which is therefore
1164 * guaranteed to be non-negative on data->build), where we remove
1165 * any integer divisions from the constraints and skip this step
1166 * if "div" itself involves any integer divisions.
1167 * The following cases are considered for div':
1168 * - individual constraints, or
1169 * - a sum of constraints that involve disjoint sets of variables and
1170 * where the sum is exactly equal to div (i.e., e = 0).
1171 * If we cannot find an appropriate expression this way, then
1172 * we pass control to extract_nonneg_mod where check
1173 * if div or "-div + d -1" themselves happen to be
1174 * non-negative on data->build.
1176 * While looking for an appropriate constraint in data->build->domain,
1177 * we ignore the constant term, so after finding such a constraint,
1178 * we still need to fix up the constant term.
1179 * In particular, if a is the constant term of "div"
1180 * (or d - 1 - the constant term of "div" if data->sign < 0)
1181 * and b is the constant term of the constraint, then we need to find
1182 * a non-negative constant c such that
1184 * b + c \equiv a mod d
1186 * We therefore take
1188 * c = (a - b) mod d
1190 * and add it to b to obtain the constant term of div'.
1191 * If this constant term is "too negative", then we add an appropriate
1192 * multiple of d to make it positive.
1195 * Note that the above is only a very simple heuristic for finding an
1196 * appropriate expression. We could try a bit harder by also considering
1197 * arbitrary linear combinations of constraints,
1198 * although that could potentially be much more expensive as it involves
1199 * the solution of an LP problem.
1201 * In particular, if v_i is a column vector representing constraint i,
1202 * w represents div and e_i is the i-th unit vector, then we are looking
1203 * for a solution of the constraints
1205 * \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
1207 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
1208 * If we are not just interested in a non-negative expression, but
1209 * also in one with a minimal range, then we don't just want
1210 * c = \sum_i lambda_i v_i to be non-negative over the domain,
1211 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
1212 * that we want to minimize and we now also have to take into account
1213 * the constant terms of the constraints.
1214 * Alternatively, we could first compute the dual of the domain
1215 * and plug in the constraints on the coefficients.
1217 static isl_stat try_extract_mod(struct isl_extract_mod_data *data)
1219 isl_basic_set *hull;
1220 isl_val *v1, *v2;
1221 isl_stat r;
1222 isl_size n;
1224 if (!data->build)
1225 goto error;
1227 n = isl_aff_dim(data->div, isl_dim_div);
1228 if (n < 0)
1229 goto error;
1231 if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
1232 return extract_nonneg_mod(data);
1234 hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
1235 hull = isl_basic_set_remove_divs(hull);
1236 data->sign = 0;
1237 data->nonneg = NULL;
1238 data->partial = NULL;
1239 r = isl_basic_set_foreach_constraint(hull,
1240 &check_parallel_or_opposite_wrap, data);
1241 isl_aff_free(data->partial);
1242 isl_basic_set_free(hull);
1244 if (!data->sign || r < 0) {
1245 isl_aff_free(data->nonneg);
1246 if (r < 0)
1247 goto error;
1248 return extract_nonneg_mod(data);
1251 v1 = isl_aff_get_constant_val(data->div);
1252 v2 = isl_aff_get_constant_val(data->nonneg);
1253 if (data->sign < 0) {
1254 v1 = isl_val_neg(v1);
1255 v1 = isl_val_add(v1, isl_val_copy(data->d));
1256 v1 = isl_val_sub_ui(v1, 1);
1258 v1 = isl_val_sub(v1, isl_val_copy(v2));
1259 v1 = isl_val_mod(v1, isl_val_copy(data->d));
1260 v1 = isl_val_add(v1, v2);
1261 v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
1262 v2 = isl_val_ceil(v2);
1263 if (isl_val_is_neg(v2)) {
1264 v2 = isl_val_mul(v2, isl_val_copy(data->d));
1265 v1 = isl_val_sub(v1, isl_val_copy(v2));
1267 data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
1268 isl_val_free(v2);
1270 if (data->sign < 0) {
1271 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
1272 data->v = isl_val_neg(data->v);
1275 return extract_term_and_mod(data,
1276 isl_aff_copy(data->div), data->nonneg);
1277 error:
1278 data->aff = isl_aff_free(data->aff);
1279 return isl_stat_error;
1282 /* Check if "data->aff" involves any (implicit) modulo computations based
1283 * on div "data->i".
1284 * If so, remove them from aff and add expressions corresponding
1285 * to those modulo computations to data->pos and/or data->neg.
1287 * "aff" is assumed to be an integer affine expression.
1289 * In particular, check if (v * div_j) is of the form
1291 * f * m * floor(a / m)
1293 * and, if so, rewrite it as
1295 * f * (a - (a mod m)) = f * a - f * (a mod m)
1297 * and extract out -f * (a mod m).
1298 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
1299 * If f < 0, we add ((-f) * (a mod m)) to *pos.
1301 * Note that in order to represent "a mod m" as
1303 * (isl_ast_expr_op_pdiv_r, a, m)
1305 * we need to make sure that a is non-negative.
1306 * If not, we check if "-a + m - 1" is non-negative.
1307 * If so, we can rewrite
1309 * floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
1311 * and still extract a modulo.
1313 static int extract_modulo(struct isl_extract_mod_data *data)
1315 data->div = isl_aff_get_div(data->aff, data->i);
1316 data->d = isl_aff_get_denominator_val(data->div);
1317 if (isl_val_is_divisible_by(data->v, data->d)) {
1318 data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
1319 if (try_extract_mod(data) < 0)
1320 data->aff = isl_aff_free(data->aff);
1322 isl_aff_free(data->div);
1323 isl_val_free(data->d);
1324 return 0;
1327 /* Check if "aff" involves any (implicit) modulo computations.
1328 * If so, remove them from aff and add expressions corresponding
1329 * to those modulo computations to *pos and/or *neg.
1330 * We only do this if the option ast_build_prefer_pdiv is set.
1332 * "aff" is assumed to be an integer affine expression.
1334 * A modulo expression is of the form
1336 * a mod m = a - m * floor(a / m)
1338 * To detect them in aff, we look for terms of the form
1340 * f * m * floor(a / m)
1342 * rewrite them as
1344 * f * (a - (a mod m)) = f * a - f * (a mod m)
1346 * and extract out -f * (a mod m).
1347 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
1348 * If f < 0, we add ((-f) * (a mod m)) to *pos.
1350 static __isl_give isl_aff *extract_modulos(__isl_take isl_aff *aff,
1351 __isl_keep isl_ast_expr **pos, __isl_keep isl_ast_expr **neg,
1352 __isl_keep isl_ast_build *build)
1354 struct isl_extract_mod_data data = { build, aff, *pos, *neg };
1355 isl_ctx *ctx;
1356 isl_size n;
1358 if (!aff)
1359 return NULL;
1361 ctx = isl_aff_get_ctx(aff);
1362 if (!isl_options_get_ast_build_prefer_pdiv(ctx))
1363 return aff;
1365 n = isl_aff_dim(data.aff, isl_dim_div);
1366 if (n < 0)
1367 return isl_aff_free(aff);
1368 for (data.i = 0; data.i < n; ++data.i) {
1369 data.v = isl_aff_get_coefficient_val(data.aff,
1370 isl_dim_div, data.i);
1371 if (!data.v)
1372 return isl_aff_free(aff);
1373 if (isl_val_is_zero(data.v) ||
1374 isl_val_is_one(data.v) || isl_val_is_negone(data.v)) {
1375 isl_val_free(data.v);
1376 continue;
1378 if (extract_modulo(&data) < 0)
1379 data.aff = isl_aff_free(data.aff);
1380 isl_val_free(data.v);
1381 if (!data.aff)
1382 break;
1385 if (data.add)
1386 data.aff = isl_aff_add(data.aff, data.add);
1388 *pos = data.pos;
1389 *neg = data.neg;
1390 return data.aff;
1393 /* Call "fn" on every non-zero coefficient of "aff",
1394 * passing it in the type of dimension (in terms of the domain),
1395 * the position and the value, as long as "fn" returns isl_bool_true.
1396 * If "reverse" is set, then the coefficients are considered in reverse order
1397 * within each type.
1399 static isl_bool every_non_zero_coefficient(__isl_keep isl_aff *aff,
1400 int reverse,
1401 isl_bool (*fn)(enum isl_dim_type type, int pos, __isl_take isl_val *v,
1402 void *user),
1403 void *user)
1405 int i, j;
1406 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1407 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1408 isl_val *v;
1410 for (i = 0; i < 3; ++i) {
1411 isl_size n;
1413 n = isl_aff_dim(aff, t[i]);
1414 if (n < 0)
1415 return isl_bool_error;
1416 for (j = 0; j < n; ++j) {
1417 isl_bool ok;
1418 int pos;
1420 pos = reverse ? n - 1 - j : j;
1421 v = isl_aff_get_coefficient_val(aff, t[i], pos);
1422 ok = isl_val_is_zero(v);
1423 if (ok >= 0 && !ok)
1424 ok = fn(l[i], pos, v, user);
1425 else
1426 isl_val_free(v);
1427 if (ok < 0 || !ok)
1428 return ok;
1432 return isl_bool_true;
1435 /* Internal data structure for extract_rational.
1437 * "d" is the denominator of the original affine expression.
1438 * "ls" is its domain local space.
1439 * "rat" collects the rational part.
1441 struct isl_ast_extract_rational_data {
1442 isl_val *d;
1443 isl_local_space *ls;
1445 isl_aff *rat;
1448 /* Given a non-zero term in an affine expression equal to "v" times
1449 * the variable of type "type" at position "pos",
1450 * add it to data->rat if "v" is not a multiple of data->d.
1452 static isl_bool add_rational(enum isl_dim_type type, int pos,
1453 __isl_take isl_val *v, void *user)
1455 struct isl_ast_extract_rational_data *data = user;
1456 isl_aff *rat;
1458 if (isl_val_is_divisible_by(v, data->d)) {
1459 isl_val_free(v);
1460 return isl_bool_true;
1462 rat = isl_aff_var_on_domain(isl_local_space_copy(data->ls), type, pos);
1463 rat = isl_aff_scale_val(rat, v);
1464 data->rat = isl_aff_add(data->rat, rat);
1465 return isl_bool_true;
1468 /* Check if aff involves any non-integer coefficients.
1469 * If so, split aff into
1471 * aff = aff1 + (aff2 / d)
1473 * with both aff1 and aff2 having only integer coefficients.
1474 * Return aff1 and add (aff2 / d) to *expr.
1476 static __isl_give isl_aff *extract_rational(__isl_take isl_aff *aff,
1477 __isl_keep isl_ast_expr **expr, __isl_keep isl_ast_build *build)
1479 struct isl_ast_extract_rational_data data = { NULL };
1480 isl_ast_expr *rat_expr;
1481 isl_val *v;
1483 if (!aff)
1484 return NULL;
1485 data.d = isl_aff_get_denominator_val(aff);
1486 if (!data.d)
1487 goto error;
1488 if (isl_val_is_one(data.d)) {
1489 isl_val_free(data.d);
1490 return aff;
1493 aff = isl_aff_scale_val(aff, isl_val_copy(data.d));
1495 data.ls = isl_aff_get_domain_local_space(aff);
1496 data.rat = isl_aff_zero_on_domain(isl_local_space_copy(data.ls));
1498 if (every_non_zero_coefficient(aff, 0, &add_rational, &data) < 0)
1499 goto error;
1501 v = isl_aff_get_constant_val(aff);
1502 if (isl_val_is_divisible_by(v, data.d)) {
1503 isl_val_free(v);
1504 } else {
1505 isl_aff *rat_0;
1507 rat_0 = isl_aff_val_on_domain(isl_local_space_copy(data.ls), v);
1508 data.rat = isl_aff_add(data.rat, rat_0);
1511 isl_local_space_free(data.ls);
1513 aff = isl_aff_sub(aff, isl_aff_copy(data.rat));
1514 aff = isl_aff_scale_down_val(aff, isl_val_copy(data.d));
1516 rat_expr = div_mod(isl_ast_expr_op_div, data.rat, data.d, build);
1517 *expr = ast_expr_add(*expr, rat_expr);
1519 return aff;
1520 error:
1521 isl_aff_free(data.rat);
1522 isl_local_space_free(data.ls);
1523 isl_aff_free(aff);
1524 isl_val_free(data.d);
1525 return NULL;
1528 /* Internal data structure for isl_ast_expr_from_aff.
1530 * "term" contains the information for adding a term.
1531 * "expr" collects the results.
1533 struct isl_ast_add_terms_data {
1534 struct isl_ast_add_term_data *term;
1535 isl_ast_expr *expr;
1538 /* Given a non-zero term in an affine expression equal to "v" times
1539 * the variable of type "type" at position "pos",
1540 * add the corresponding AST expression to data->expr.
1542 static isl_bool add_term(enum isl_dim_type type, int pos,
1543 __isl_take isl_val *v, void *user)
1545 struct isl_ast_add_terms_data *data = user;
1547 data->expr =
1548 isl_ast_expr_add_term(data->expr, type, pos, v, data->term);
1550 return isl_bool_true;
1553 /* Add terms to "expr" for each variable in "aff".
1554 * The result is simplified in terms of data->build->domain.
1556 static __isl_give isl_ast_expr *add_terms(__isl_take isl_ast_expr *expr,
1557 __isl_keep isl_aff *aff, struct isl_ast_add_term_data *data)
1559 struct isl_ast_add_terms_data terms_data = { data, expr };
1561 if (every_non_zero_coefficient(aff, 0, &add_term, &terms_data) < 0)
1562 return isl_ast_expr_free(terms_data.expr);
1564 return terms_data.expr;
1567 /* Construct an isl_ast_expr that evaluates the affine expression "aff".
1568 * The result is simplified in terms of build->domain.
1570 * We first extract hidden modulo computations from the affine expression
1571 * and then add terms for each variable with a non-zero coefficient.
1572 * Finally, if the affine expression has a non-trivial denominator,
1573 * we divide the resulting isl_ast_expr by this denominator.
1575 __isl_give isl_ast_expr *isl_ast_expr_from_aff(__isl_take isl_aff *aff,
1576 __isl_keep isl_ast_build *build)
1578 isl_ctx *ctx = isl_aff_get_ctx(aff);
1579 isl_ast_expr *expr, *expr_neg;
1580 struct isl_ast_add_term_data term_data;
1582 if (!aff)
1583 return NULL;
1585 expr = isl_ast_expr_alloc_int_si(ctx, 0);
1586 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1588 aff = extract_rational(aff, &expr, build);
1590 aff = extract_modulos(aff, &expr, &expr_neg, build);
1591 expr = ast_expr_sub(expr, expr_neg);
1593 term_data.build = build;
1594 term_data.ls = isl_aff_get_domain_local_space(aff);
1595 term_data.cst = isl_aff_get_constant_val(aff);
1596 expr = add_terms(expr, aff, &term_data);
1598 expr = isl_ast_expr_add_int(expr, term_data.cst);
1599 isl_local_space_free(term_data.ls);
1601 isl_aff_free(aff);
1602 return expr;
1605 /* Internal data structure for coefficients_of_sign.
1607 * "sign" is the sign of the coefficients that should be retained.
1608 * "aff" is the affine expression of which some coefficients are zeroed out.
1610 struct isl_ast_coefficients_of_sign_data {
1611 int sign;
1612 isl_aff *aff;
1615 /* Clear the specified coefficient of data->aff if the value "v"
1616 * does not have the required sign.
1618 static isl_bool clear_opposite_sign(enum isl_dim_type type, int pos,
1619 __isl_take isl_val *v, void *user)
1621 struct isl_ast_coefficients_of_sign_data *data = user;
1623 if (type == isl_dim_set)
1624 type = isl_dim_in;
1625 if (data->sign * isl_val_sgn(v) < 0)
1626 data->aff = isl_aff_set_coefficient_si(data->aff, type, pos, 0);
1627 isl_val_free(v);
1629 return isl_bool_true;
1632 /* Extract the coefficients of "aff" (excluding the constant term)
1633 * that have the given sign.
1635 * Take a copy of "aff" and clear the coefficients that do not have
1636 * the required sign.
1637 * Consider the coefficients in reverse order since clearing
1638 * the coefficient of an integer division in data.aff
1639 * could result in the removal of that integer division from data.aff,
1640 * changing the positions of all subsequent integer divisions of data.aff,
1641 * while those of "aff" remain the same.
1643 static __isl_give isl_aff *coefficients_of_sign(__isl_take isl_aff *aff,
1644 int sign)
1646 struct isl_ast_coefficients_of_sign_data data;
1648 data.sign = sign;
1649 data.aff = isl_aff_copy(aff);
1650 if (every_non_zero_coefficient(aff, 1, &clear_opposite_sign, &data) < 0)
1651 data.aff = isl_aff_free(data.aff);
1652 isl_aff_free(aff);
1654 data.aff = isl_aff_set_constant_si(data.aff, 0);
1656 return data.aff;
1659 /* Should the constant term "v" be considered positive?
1661 * A positive constant will be added to "pos" by the caller,
1662 * while a negative constant will be added to "neg".
1663 * If either "pos" or "neg" is exactly zero, then we prefer
1664 * to add the constant "v" to that side, irrespective of the sign of "v".
1665 * This results in slightly shorter expressions and may reduce the risk
1666 * of overflows.
1668 static isl_bool constant_is_considered_positive(__isl_keep isl_val *v,
1669 __isl_keep isl_ast_expr *pos, __isl_keep isl_ast_expr *neg)
1671 isl_bool zero;
1673 zero = ast_expr_is_zero(pos);
1674 if (zero < 0 || zero)
1675 return zero;
1676 zero = ast_expr_is_zero(neg);
1677 if (zero < 0 || zero)
1678 return isl_bool_not(zero);
1679 return isl_val_is_pos(v);
1682 /* Check if the equality
1684 * aff = 0
1686 * represents a stride constraint on the integer division "pos".
1688 * In particular, if the integer division "pos" is equal to
1690 * floor(e/d)
1692 * then check if aff is equal to
1694 * e - d floor(e/d)
1696 * or its opposite.
1698 * If so, the equality is exactly
1700 * e mod d = 0
1702 * Note that in principle we could also accept
1704 * e - d floor(e'/d)
1706 * where e and e' differ by a constant.
1708 static isl_bool is_stride_constraint(__isl_keep isl_aff *aff, int pos)
1710 isl_aff *div;
1711 isl_val *c, *d;
1712 isl_bool eq;
1714 div = isl_aff_get_div(aff, pos);
1715 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1716 d = isl_aff_get_denominator_val(div);
1717 eq = isl_val_abs_eq(c, d);
1718 if (eq >= 0 && eq) {
1719 aff = isl_aff_copy(aff);
1720 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1721 div = isl_aff_scale_val(div, d);
1722 if (isl_val_is_pos(c))
1723 div = isl_aff_neg(div);
1724 eq = isl_aff_plain_is_equal(div, aff);
1725 isl_aff_free(aff);
1726 } else
1727 isl_val_free(d);
1728 isl_val_free(c);
1729 isl_aff_free(div);
1731 return eq;
1734 /* Are all coefficients of "aff" (zero or) negative?
1736 static isl_bool all_negative_coefficients(__isl_keep isl_aff *aff)
1738 int i;
1739 isl_size n;
1741 n = isl_aff_dim(aff, isl_dim_param);
1742 if (n < 0)
1743 return isl_bool_error;
1744 for (i = 0; i < n; ++i)
1745 if (isl_aff_coefficient_sgn(aff, isl_dim_param, i) > 0)
1746 return isl_bool_false;
1748 n = isl_aff_dim(aff, isl_dim_in);
1749 if (n < 0)
1750 return isl_bool_error;
1751 for (i = 0; i < n; ++i)
1752 if (isl_aff_coefficient_sgn(aff, isl_dim_in, i) > 0)
1753 return isl_bool_false;
1755 return isl_bool_true;
1758 /* Give an equality of the form
1760 * aff = e - d floor(e/d) = 0
1762 * or
1764 * aff = -e + d floor(e/d) = 0
1766 * with the integer division "pos" equal to floor(e/d),
1767 * construct the AST expression
1769 * (isl_ast_expr_op_eq,
1770 * (isl_ast_expr_op_zdiv_r, expr(e), expr(d)), expr(0))
1772 * If e only has negative coefficients, then construct
1774 * (isl_ast_expr_op_eq,
1775 * (isl_ast_expr_op_zdiv_r, expr(-e), expr(d)), expr(0))
1777 * instead.
1779 static __isl_give isl_ast_expr *extract_stride_constraint(
1780 __isl_take isl_aff *aff, int pos, __isl_keep isl_ast_build *build)
1782 isl_bool all_neg;
1783 isl_ctx *ctx;
1784 isl_val *c;
1785 isl_ast_expr *expr, *cst;
1787 if (!aff)
1788 return NULL;
1790 ctx = isl_aff_get_ctx(aff);
1792 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1793 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1795 all_neg = all_negative_coefficients(aff);
1796 if (all_neg < 0)
1797 aff = isl_aff_free(aff);
1798 else if (all_neg)
1799 aff = isl_aff_neg(aff);
1801 cst = isl_ast_expr_from_val(isl_val_abs(c));
1802 expr = isl_ast_expr_from_aff(aff, build);
1804 expr = isl_ast_expr_alloc_binary(isl_ast_expr_op_zdiv_r, expr, cst);
1805 cst = isl_ast_expr_alloc_int_si(ctx, 0);
1806 expr = isl_ast_expr_alloc_binary(isl_ast_expr_op_eq, expr, cst);
1808 return expr;
1811 /* Construct an isl_ast_expr evaluating
1813 * "expr_pos" == "expr_neg", if "eq" is set, or
1814 * "expr_pos" >= "expr_neg", if "eq" is not set
1816 * However, if "expr_pos" is an integer constant (and "expr_neg" is not),
1817 * then the two expressions are interchanged. This ensures that,
1818 * e.g., "i <= 5" is constructed rather than "5 >= i".
1820 static __isl_give isl_ast_expr *construct_constraint_expr(int eq,
1821 __isl_take isl_ast_expr *expr_pos, __isl_take isl_ast_expr *expr_neg)
1823 isl_ast_expr *expr;
1824 enum isl_ast_expr_op_type type;
1825 int pos_is_cst, neg_is_cst;
1827 pos_is_cst = isl_ast_expr_get_type(expr_pos) == isl_ast_expr_int;
1828 neg_is_cst = isl_ast_expr_get_type(expr_neg) == isl_ast_expr_int;
1829 if (pos_is_cst && !neg_is_cst) {
1830 type = eq ? isl_ast_expr_op_eq : isl_ast_expr_op_le;
1831 expr = isl_ast_expr_alloc_binary(type, expr_neg, expr_pos);
1832 } else {
1833 type = eq ? isl_ast_expr_op_eq : isl_ast_expr_op_ge;
1834 expr = isl_ast_expr_alloc_binary(type, expr_pos, expr_neg);
1837 return expr;
1840 /* Construct an isl_ast_expr that evaluates the condition "aff" == 0
1841 * (if "eq" is set) or "aff" >= 0 (otherwise).
1842 * The result is simplified in terms of build->domain.
1844 * We first extract hidden modulo computations from "aff"
1845 * and then collect all the terms with a positive coefficient in cons_pos
1846 * and the terms with a negative coefficient in cons_neg.
1848 * The result is then essentially of the form
1850 * (isl_ast_expr_op_ge, expr(pos), expr(-neg)))
1852 * or
1854 * (isl_ast_expr_op_eq, expr(pos), expr(-neg)))
1856 * However, if there are no terms with positive coefficients (or no terms
1857 * with negative coefficients), then the constant term is added to "pos"
1858 * (or "neg"), ignoring the sign of the constant term.
1860 static __isl_give isl_ast_expr *isl_ast_expr_from_constraint_no_stride(
1861 int eq, __isl_take isl_aff *aff, __isl_keep isl_ast_build *build)
1863 isl_bool cst_is_pos;
1864 isl_ctx *ctx;
1865 isl_ast_expr *expr_pos;
1866 isl_ast_expr *expr_neg;
1867 isl_aff *aff_pos, *aff_neg;
1868 struct isl_ast_add_term_data data;
1870 ctx = isl_aff_get_ctx(aff);
1871 expr_pos = isl_ast_expr_alloc_int_si(ctx, 0);
1872 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1874 aff = extract_modulos(aff, &expr_pos, &expr_neg, build);
1876 data.build = build;
1877 data.ls = isl_aff_get_domain_local_space(aff);
1878 data.cst = isl_aff_get_constant_val(aff);
1880 aff_pos = coefficients_of_sign(isl_aff_copy(aff), 1);
1881 aff_neg = isl_aff_neg(coefficients_of_sign(aff, -1));
1883 expr_pos = add_terms(expr_pos, aff_pos, &data);
1884 data.cst = isl_val_neg(data.cst);
1885 expr_neg = add_terms(expr_neg, aff_neg, &data);
1886 data.cst = isl_val_neg(data.cst);
1887 isl_local_space_free(data.ls);
1889 cst_is_pos =
1890 constant_is_considered_positive(data.cst, expr_pos, expr_neg);
1891 if (cst_is_pos < 0)
1892 expr_pos = isl_ast_expr_free(expr_pos);
1894 if (cst_is_pos) {
1895 expr_pos = isl_ast_expr_add_int(expr_pos, data.cst);
1896 } else {
1897 data.cst = isl_val_neg(data.cst);
1898 expr_neg = isl_ast_expr_add_int(expr_neg, data.cst);
1901 isl_aff_free(aff_pos);
1902 isl_aff_free(aff_neg);
1903 return construct_constraint_expr(eq, expr_pos, expr_neg);
1906 /* Construct an isl_ast_expr that evaluates the condition "constraint".
1907 * The result is simplified in terms of build->domain.
1909 * We first check if the constraint is an equality of the form
1911 * e - d floor(e/d) = 0
1913 * i.e.,
1915 * e mod d = 0
1917 * If so, we convert it to
1919 * (isl_ast_expr_op_eq,
1920 * (isl_ast_expr_op_zdiv_r, expr(e), expr(d)), expr(0))
1922 static __isl_give isl_ast_expr *isl_ast_expr_from_constraint(
1923 __isl_take isl_constraint *constraint, __isl_keep isl_ast_build *build)
1925 int i;
1926 isl_size n;
1927 isl_aff *aff;
1928 isl_bool eq;
1930 aff = isl_constraint_get_aff(constraint);
1931 eq = isl_constraint_is_equality(constraint);
1932 isl_constraint_free(constraint);
1933 if (eq < 0)
1934 goto error;
1936 n = isl_aff_dim(aff, isl_dim_div);
1937 if (n < 0)
1938 aff = isl_aff_free(aff);
1939 if (eq && n > 0)
1940 for (i = 0; i < n; ++i) {
1941 isl_bool is_stride;
1942 is_stride = is_stride_constraint(aff, i);
1943 if (is_stride < 0)
1944 goto error;
1945 if (is_stride)
1946 return extract_stride_constraint(aff, i, build);
1949 return isl_ast_expr_from_constraint_no_stride(eq, aff, build);
1950 error:
1951 isl_aff_free(aff);
1952 return NULL;
1955 /* Wrapper around isl_constraint_cmp_last_non_zero for use
1956 * as a callback to isl_constraint_list_sort.
1957 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1958 * apart, then use isl_constraint_plain_cmp instead.
1960 static int cmp_constraint(__isl_keep isl_constraint *a,
1961 __isl_keep isl_constraint *b, void *user)
1963 int cmp;
1965 cmp = isl_constraint_cmp_last_non_zero(a, b);
1966 if (cmp != 0)
1967 return cmp;
1968 return isl_constraint_plain_cmp(a, b);
1971 /* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1972 * The result is simplified in terms of build->domain.
1974 * If "bset" is not bounded by any constraint, then we construct
1975 * the expression "1", i.e., "true".
1977 * Otherwise, we sort the constraints, putting constraints that involve
1978 * integer divisions after those that do not, and construct an "and"
1979 * of the ast expressions of the individual constraints.
1981 * Each constraint is added to the generated constraints of the build
1982 * after it has been converted to an AST expression so that it can be used
1983 * to simplify the following constraints. This may change the truth value
1984 * of subsequent constraints that do not satisfy the earlier constraints,
1985 * but this does not affect the outcome of the conjunction as it is
1986 * only true if all the conjuncts are true (no matter in what order
1987 * they are evaluated). In particular, the constraints that do not
1988 * involve integer divisions may serve to simplify some constraints
1989 * that do involve integer divisions.
1991 __isl_give isl_ast_expr *isl_ast_build_expr_from_basic_set(
1992 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1994 int i;
1995 isl_size n;
1996 isl_constraint *c;
1997 isl_constraint_list *list;
1998 isl_ast_expr *res;
1999 isl_set *set;
2001 list = isl_basic_set_get_constraint_list(bset);
2002 isl_basic_set_free(bset);
2003 list = isl_constraint_list_sort(list, &cmp_constraint, NULL);
2004 n = isl_constraint_list_n_constraint(list);
2005 if (n < 0)
2006 build = NULL;
2007 if (n == 0) {
2008 isl_ctx *ctx = isl_constraint_list_get_ctx(list);
2009 isl_constraint_list_free(list);
2010 return isl_ast_expr_alloc_int_si(ctx, 1);
2013 build = isl_ast_build_copy(build);
2015 c = isl_constraint_list_get_constraint(list, 0);
2016 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
2017 set = isl_set_from_basic_set(bset);
2018 res = isl_ast_expr_from_constraint(c, build);
2019 build = isl_ast_build_restrict_generated(build, set);
2021 for (i = 1; i < n; ++i) {
2022 isl_ast_expr *expr;
2024 c = isl_constraint_list_get_constraint(list, i);
2025 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
2026 set = isl_set_from_basic_set(bset);
2027 expr = isl_ast_expr_from_constraint(c, build);
2028 build = isl_ast_build_restrict_generated(build, set);
2029 res = isl_ast_expr_and(res, expr);
2032 isl_constraint_list_free(list);
2033 isl_ast_build_free(build);
2034 return res;
2037 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
2038 * The result is simplified in terms of build->domain.
2040 * If "set" is an (obviously) empty set, then return the expression "0".
2042 * If there are multiple disjuncts in the description of the set,
2043 * then subsequent disjuncts are simplified in a context where
2044 * the previous disjuncts have been removed from build->domain.
2045 * In particular, constraints that ensure that there is no overlap
2046 * with these previous disjuncts, can be removed.
2047 * This is mostly useful for disjuncts that are only defined by
2048 * a single constraint (relative to the build domain) as the opposite
2049 * of that single constraint can then be removed from the other disjuncts.
2050 * In order not to increase the number of disjuncts in the build domain
2051 * after subtracting the previous disjuncts of "set", the simple hull
2052 * is computed after taking the difference with each of these disjuncts.
2053 * This means that constraints that prevent overlap with a union
2054 * of multiple previous disjuncts are not removed.
2056 * "set" lives in the internal schedule space.
2058 __isl_give isl_ast_expr *isl_ast_build_expr_from_set_internal(
2059 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2061 int i;
2062 isl_size n;
2063 isl_basic_set *bset;
2064 isl_basic_set_list *list;
2065 isl_set *domain;
2066 isl_ast_expr *res;
2068 list = isl_set_get_basic_set_list(set);
2069 isl_set_free(set);
2071 n = isl_basic_set_list_n_basic_set(list);
2072 if (n < 0)
2073 build = NULL;
2074 if (n == 0) {
2075 isl_ctx *ctx = isl_ast_build_get_ctx(build);
2076 isl_basic_set_list_free(list);
2077 return isl_ast_expr_from_val(isl_val_zero(ctx));
2080 domain = isl_ast_build_get_domain(build);
2082 bset = isl_basic_set_list_get_basic_set(list, 0);
2083 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
2084 res = isl_ast_build_expr_from_basic_set(build, bset);
2086 for (i = 1; i < n; ++i) {
2087 isl_ast_expr *expr;
2088 isl_set *rest;
2090 rest = isl_set_subtract(isl_set_copy(domain), set);
2091 rest = isl_set_from_basic_set(isl_set_simple_hull(rest));
2092 domain = isl_set_intersect(domain, rest);
2093 bset = isl_basic_set_list_get_basic_set(list, i);
2094 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
2095 bset = isl_basic_set_gist(bset,
2096 isl_set_simple_hull(isl_set_copy(domain)));
2097 expr = isl_ast_build_expr_from_basic_set(build, bset);
2098 res = isl_ast_expr_or(res, expr);
2101 isl_set_free(domain);
2102 isl_set_free(set);
2103 isl_basic_set_list_free(list);
2104 return res;
2107 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
2108 * The result is simplified in terms of build->domain.
2110 * If "set" is an (obviously) empty set, then return the expression "0".
2112 * "set" lives in the external schedule space.
2114 * The internal AST expression generation assumes that there are
2115 * no unknown divs, so make sure an explicit representation is available.
2116 * Since the set comes from the outside, it may have constraints that
2117 * are redundant with respect to the build domain. Remove them first.
2119 __isl_give isl_ast_expr *isl_ast_build_expr_from_set(
2120 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
2122 isl_bool needs_map;
2124 needs_map = isl_ast_build_need_schedule_map(build);
2125 if (needs_map < 0) {
2126 set = isl_set_free(set);
2127 } else if (needs_map) {
2128 isl_multi_aff *ma;
2129 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2130 set = isl_set_preimage_multi_aff(set, ma);
2133 set = isl_set_compute_divs(set);
2134 set = isl_ast_build_compute_gist(build, set);
2135 return isl_ast_build_expr_from_set_internal(build, set);
2138 /* State of data about previous pieces in
2139 * isl_ast_build_expr_from_pw_aff_internal.
2141 * isl_state_none: no data about previous pieces
2142 * isl_state_single: data about a single previous piece
2143 * isl_state_min: data represents minimum of several pieces
2144 * isl_state_max: data represents maximum of several pieces
2146 enum isl_from_pw_aff_state {
2147 isl_state_none,
2148 isl_state_single,
2149 isl_state_min,
2150 isl_state_max
2153 /* Internal date structure representing a single piece in the input of
2154 * isl_ast_build_expr_from_pw_aff_internal.
2156 * If "state" is isl_state_none, then "set_list" and "aff_list" are not used.
2157 * If "state" is isl_state_single, then "set_list" and "aff_list" contain the
2158 * single previous subpiece.
2159 * If "state" is isl_state_min, then "set_list" and "aff_list" contain
2160 * a sequence of several previous subpieces that are equal to the minimum
2161 * of the entries in "aff_list" over the union of "set_list"
2162 * If "state" is isl_state_max, then "set_list" and "aff_list" contain
2163 * a sequence of several previous subpieces that are equal to the maximum
2164 * of the entries in "aff_list" over the union of "set_list"
2166 * During the construction of the pieces, "set" is NULL.
2167 * After the construction, "set" is set to the union of the elements
2168 * in "set_list", at which point "set_list" is set to NULL.
2170 struct isl_from_pw_aff_piece {
2171 enum isl_from_pw_aff_state state;
2172 isl_set *set;
2173 isl_set_list *set_list;
2174 isl_aff_list *aff_list;
2177 /* Internal data structure for isl_ast_build_expr_from_pw_aff_internal.
2179 * "build" specifies the domain against which the result is simplified.
2180 * "dom" is the domain of the entire isl_pw_aff.
2182 * "n" is the number of pieces constructed already.
2183 * In particular, during the construction of the pieces, "n" points to
2184 * the piece that is being constructed. After the construction of the
2185 * pieces, "n" is set to the total number of pieces.
2186 * "max" is the total number of allocated entries.
2187 * "p" contains the individual pieces.
2189 struct isl_from_pw_aff_data {
2190 isl_ast_build *build;
2191 isl_set *dom;
2193 int n;
2194 int max;
2195 struct isl_from_pw_aff_piece *p;
2198 /* Initialize "data" based on "build" and "pa".
2200 static isl_stat isl_from_pw_aff_data_init(struct isl_from_pw_aff_data *data,
2201 __isl_keep isl_ast_build *build, __isl_keep isl_pw_aff *pa)
2203 isl_size n;
2204 isl_ctx *ctx;
2206 ctx = isl_pw_aff_get_ctx(pa);
2207 n = isl_pw_aff_n_piece(pa);
2208 if (n < 0)
2209 return isl_stat_error;
2210 if (n == 0)
2211 isl_die(ctx, isl_error_invalid,
2212 "cannot handle void expression", return isl_stat_error);
2213 data->max = n;
2214 data->p = isl_calloc_array(ctx, struct isl_from_pw_aff_piece, n);
2215 if (!data->p)
2216 return isl_stat_error;
2217 data->build = build;
2218 data->dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
2219 data->n = 0;
2221 return isl_stat_ok;
2224 /* Free all memory allocated for "data".
2226 static void isl_from_pw_aff_data_clear(struct isl_from_pw_aff_data *data)
2228 int i;
2230 isl_set_free(data->dom);
2231 if (!data->p)
2232 return;
2234 for (i = 0; i < data->max; ++i) {
2235 isl_set_free(data->p[i].set);
2236 isl_set_list_free(data->p[i].set_list);
2237 isl_aff_list_free(data->p[i].aff_list);
2239 free(data->p);
2242 /* Initialize the current entry of "data" to an unused piece.
2244 static void set_none(struct isl_from_pw_aff_data *data)
2246 data->p[data->n].state = isl_state_none;
2247 data->p[data->n].set_list = NULL;
2248 data->p[data->n].aff_list = NULL;
2251 /* Store "set" and "aff" in the current entry of "data" as a single subpiece.
2253 static void set_single(struct isl_from_pw_aff_data *data,
2254 __isl_take isl_set *set, __isl_take isl_aff *aff)
2256 data->p[data->n].state = isl_state_single;
2257 data->p[data->n].set_list = isl_set_list_from_set(set);
2258 data->p[data->n].aff_list = isl_aff_list_from_aff(aff);
2261 /* Extend the current entry of "data" with "set" and "aff"
2262 * as a minimum expression.
2264 static isl_stat extend_min(struct isl_from_pw_aff_data *data,
2265 __isl_take isl_set *set, __isl_take isl_aff *aff)
2267 int n = data->n;
2268 data->p[n].state = isl_state_min;
2269 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
2270 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
2272 if (!data->p[n].set_list || !data->p[n].aff_list)
2273 return isl_stat_error;
2274 return isl_stat_ok;
2277 /* Extend the current entry of "data" with "set" and "aff"
2278 * as a maximum expression.
2280 static isl_stat extend_max(struct isl_from_pw_aff_data *data,
2281 __isl_take isl_set *set, __isl_take isl_aff *aff)
2283 int n = data->n;
2284 data->p[n].state = isl_state_max;
2285 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
2286 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
2288 if (!data->p[n].set_list || !data->p[n].aff_list)
2289 return isl_stat_error;
2290 return isl_stat_ok;
2293 /* Extend the domain of the current entry of "data", which is assumed
2294 * to contain a single subpiece, with "set". If "replace" is set,
2295 * then also replace the affine function by "aff". Otherwise,
2296 * simply free "aff".
2298 static isl_stat extend_domain(struct isl_from_pw_aff_data *data,
2299 __isl_take isl_set *set, __isl_take isl_aff *aff, int replace)
2301 int n = data->n;
2302 isl_set *set_n;
2304 set_n = isl_set_list_get_set(data->p[n].set_list, 0);
2305 set_n = isl_set_union(set_n, set);
2306 data->p[n].set_list =
2307 isl_set_list_set_set(data->p[n].set_list, 0, set_n);
2309 if (replace)
2310 data->p[n].aff_list =
2311 isl_aff_list_set_aff(data->p[n].aff_list, 0, aff);
2312 else
2313 isl_aff_free(aff);
2315 if (!data->p[n].set_list || !data->p[n].aff_list)
2316 return isl_stat_error;
2317 return isl_stat_ok;
2320 /* Construct an isl_ast_expr from "list" within "build".
2321 * If "state" is isl_state_single, then "list" contains a single entry and
2322 * an isl_ast_expr is constructed for that entry.
2323 * Otherwise a min or max expression is constructed from "list"
2324 * depending on "state".
2326 static __isl_give isl_ast_expr *ast_expr_from_aff_list(
2327 __isl_take isl_aff_list *list, enum isl_from_pw_aff_state state,
2328 __isl_keep isl_ast_build *build)
2330 int i;
2331 isl_size n;
2332 isl_aff *aff;
2333 isl_ast_expr *expr = NULL;
2334 enum isl_ast_expr_op_type op_type;
2336 if (state == isl_state_single) {
2337 aff = isl_aff_list_get_aff(list, 0);
2338 isl_aff_list_free(list);
2339 return isl_ast_expr_from_aff(aff, build);
2341 n = isl_aff_list_n_aff(list);
2342 if (n < 0)
2343 goto error;
2344 op_type = state == isl_state_min ? isl_ast_expr_op_min
2345 : isl_ast_expr_op_max;
2346 expr = isl_ast_expr_alloc_op(isl_ast_build_get_ctx(build), op_type, n);
2348 for (i = 0; i < n; ++i) {
2349 isl_ast_expr *expr_i;
2351 aff = isl_aff_list_get_aff(list, i);
2352 expr_i = isl_ast_expr_from_aff(aff, build);
2353 expr = isl_ast_expr_op_add_arg(expr, expr_i);
2356 isl_aff_list_free(list);
2357 return expr;
2358 error:
2359 isl_aff_list_free(list);
2360 isl_ast_expr_free(expr);
2361 return NULL;
2364 /* Extend the list of expressions in "next" to take into account
2365 * the piece at position "pos" in "data", allowing for a further extension
2366 * for the next piece(s).
2367 * In particular, "next" is extended with a select operation that selects
2368 * an isl_ast_expr corresponding to data->aff_list on data->set and
2369 * to an expression that will be filled in by later calls.
2370 * Return a pointer to the arguments of this select operation.
2371 * Afterwards, the state of "data" is set to isl_state_none.
2373 * The constraints of data->set are added to the generated
2374 * constraints of the build such that they can be exploited to simplify
2375 * the AST expression constructed from data->aff_list.
2377 static isl_ast_expr_list **add_intermediate_piece(
2378 struct isl_from_pw_aff_data *data,
2379 int pos, isl_ast_expr_list **next)
2381 isl_ctx *ctx;
2382 isl_ast_build *build;
2383 isl_ast_expr *ternary, *arg;
2384 isl_set *set, *gist;
2386 set = data->p[pos].set;
2387 data->p[pos].set = NULL;
2388 ctx = isl_ast_build_get_ctx(data->build);
2389 ternary = isl_ast_expr_alloc_op(ctx, isl_ast_expr_op_select, 3);
2390 gist = isl_set_gist(isl_set_copy(set), isl_set_copy(data->dom));
2391 arg = isl_ast_build_expr_from_set_internal(data->build, gist);
2392 ternary = isl_ast_expr_op_add_arg(ternary, arg);
2393 build = isl_ast_build_copy(data->build);
2394 build = isl_ast_build_restrict_generated(build, set);
2395 arg = ast_expr_from_aff_list(data->p[pos].aff_list,
2396 data->p[pos].state, build);
2397 data->p[pos].aff_list = NULL;
2398 isl_ast_build_free(build);
2399 ternary = isl_ast_expr_op_add_arg(ternary, arg);
2400 data->p[pos].state = isl_state_none;
2401 if (!ternary)
2402 return NULL;
2404 *next = isl_ast_expr_list_add(*next, ternary);
2405 return &ternary->u.op.args;
2408 /* Extend the list of expressions in "next" to take into account
2409 * the final piece, located at position "pos" in "data".
2410 * In particular, "next" is extended with an expression
2411 * to evaluate data->aff_list and the domain is ignored.
2412 * Return isl_stat_ok on success and isl_stat_error on failure.
2414 * The constraints of data->set are however added to the generated
2415 * constraints of the build such that they can be exploited to simplify
2416 * the AST expression constructed from data->aff_list.
2418 static isl_stat add_last_piece(struct isl_from_pw_aff_data *data,
2419 int pos, isl_ast_expr_list **next)
2421 isl_ast_build *build;
2422 isl_ast_expr *last;
2424 if (data->p[pos].state == isl_state_none)
2425 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
2426 "cannot handle void expression", return isl_stat_error);
2428 build = isl_ast_build_copy(data->build);
2429 build = isl_ast_build_restrict_generated(build, data->p[pos].set);
2430 data->p[pos].set = NULL;
2431 last = ast_expr_from_aff_list(data->p[pos].aff_list,
2432 data->p[pos].state, build);
2433 *next = isl_ast_expr_list_add(*next, last);
2434 data->p[pos].aff_list = NULL;
2435 isl_ast_build_free(build);
2436 data->p[pos].state = isl_state_none;
2437 if (!*next)
2438 return isl_stat_error;
2440 return isl_stat_ok;
2443 /* Return -1 if the piece "p1" should be sorted before "p2"
2444 * and 1 if it should be sorted after "p2".
2445 * Return 0 if they do not need to be sorted in a specific order.
2447 * Pieces are sorted according to the number of disjuncts
2448 * in their domains.
2450 static int sort_pieces_cmp(const void *p1, const void *p2, void *arg)
2452 const struct isl_from_pw_aff_piece *piece1 = p1;
2453 const struct isl_from_pw_aff_piece *piece2 = p2;
2454 isl_size n1, n2;
2456 n1 = isl_set_n_basic_set(piece1->set);
2457 n2 = isl_set_n_basic_set(piece2->set);
2459 return n1 - n2;
2462 /* Construct an isl_ast_expr from the pieces in "data".
2463 * Return the result or NULL on failure.
2465 * When this function is called, data->n points to the current piece.
2466 * If this is an effective piece, then first increment data->n such
2467 * that data->n contains the number of pieces.
2468 * The "set_list" fields are subsequently replaced by the corresponding
2469 * "set" fields, after which the pieces are sorted according to
2470 * the number of disjuncts in these "set" fields.
2472 * Construct intermediate AST expressions for the initial pieces and
2473 * finish off with the final pieces.
2475 * Any piece that is not the very first is added to the list of arguments
2476 * of the previously constructed piece.
2477 * In order not to have to special case the first piece,
2478 * an extra list is created to hold the final result.
2480 static isl_ast_expr *build_pieces(struct isl_from_pw_aff_data *data)
2482 int i;
2483 isl_ctx *ctx;
2484 isl_ast_expr_list *res_list;
2485 isl_ast_expr_list **next = &res_list;
2486 isl_ast_expr *res;
2488 if (data->p[data->n].state != isl_state_none)
2489 data->n++;
2490 ctx = isl_ast_build_get_ctx(data->build);
2491 if (data->n == 0)
2492 isl_die(ctx, isl_error_invalid,
2493 "cannot handle void expression", return NULL);
2495 for (i = 0; i < data->n; ++i) {
2496 data->p[i].set = isl_set_list_union(data->p[i].set_list);
2497 if (data->p[i].state != isl_state_single)
2498 data->p[i].set = isl_set_coalesce(data->p[i].set);
2499 data->p[i].set_list = NULL;
2502 if (isl_sort(data->p, data->n, sizeof(data->p[0]),
2503 &sort_pieces_cmp, NULL) < 0)
2504 return NULL;
2506 res_list = isl_ast_expr_list_alloc(ctx, 1);
2507 if (!res_list)
2508 return NULL;
2509 for (i = 0; i + 1 < data->n; ++i) {
2510 next = add_intermediate_piece(data, i, next);
2511 if (!next)
2512 goto error;
2515 if (add_last_piece(data, data->n - 1, next) < 0)
2516 goto error;
2518 res = isl_ast_expr_list_get_at(res_list, 0);
2519 isl_ast_expr_list_free(res_list);
2520 return res;
2521 error:
2522 isl_ast_expr_list_free(res_list);
2523 return NULL;
2526 /* Is the domain of the current entry of "data", which is assumed
2527 * to contain a single subpiece, a subset of "set"?
2529 static isl_bool single_is_subset(struct isl_from_pw_aff_data *data,
2530 __isl_keep isl_set *set)
2532 isl_bool subset;
2533 isl_set *set_n;
2535 set_n = isl_set_list_get_set(data->p[data->n].set_list, 0);
2536 subset = isl_set_is_subset(set_n, set);
2537 isl_set_free(set_n);
2539 return subset;
2542 /* Is "aff" a rational expression, i.e., does it have a denominator
2543 * different from one?
2545 static isl_bool aff_is_rational(__isl_keep isl_aff *aff)
2547 isl_bool rational;
2548 isl_val *den;
2550 den = isl_aff_get_denominator_val(aff);
2551 rational = isl_bool_not(isl_val_is_one(den));
2552 isl_val_free(den);
2554 return rational;
2557 /* Does "list" consist of a single rational affine expression?
2559 static isl_bool is_single_rational_aff(__isl_keep isl_aff_list *list)
2561 isl_size n;
2562 isl_bool rational;
2563 isl_aff *aff;
2565 n = isl_aff_list_n_aff(list);
2566 if (n < 0)
2567 return isl_bool_error;
2568 if (n != 1)
2569 return isl_bool_false;
2570 aff = isl_aff_list_get_aff(list, 0);
2571 rational = aff_is_rational(aff);
2572 isl_aff_free(aff);
2574 return rational;
2577 /* Can the list of subpieces in the last piece of "data" be extended with
2578 * "set" and "aff" based on "test"?
2579 * In particular, is it the case for each entry (set_i, aff_i) that
2581 * test(aff, aff_i) holds on set_i, and
2582 * test(aff_i, aff) holds on set?
2584 * "test" returns the set of elements where the tests holds, meaning
2585 * that test(aff_i, aff) holds on set if set is a subset of test(aff_i, aff).
2587 * This function is used to detect min/max expressions.
2588 * If the ast_build_detect_min_max option is turned off, then
2589 * do not even try and perform any detection and return false instead.
2591 * Rational affine expressions are not considered for min/max expressions
2592 * since the combined expression will be defined on the union of the domains,
2593 * while a rational expression may only yield integer values
2594 * on its own definition domain.
2596 static isl_bool extends(struct isl_from_pw_aff_data *data,
2597 __isl_keep isl_set *set, __isl_keep isl_aff *aff,
2598 __isl_give isl_basic_set *(*test)(__isl_take isl_aff *aff1,
2599 __isl_take isl_aff *aff2))
2601 int i;
2602 isl_size n;
2603 isl_bool is_rational;
2604 isl_ctx *ctx;
2605 isl_set *dom;
2607 is_rational = aff_is_rational(aff);
2608 if (is_rational >= 0 && !is_rational)
2609 is_rational = is_single_rational_aff(data->p[data->n].aff_list);
2610 if (is_rational < 0 || is_rational)
2611 return isl_bool_not(is_rational);
2613 ctx = isl_ast_build_get_ctx(data->build);
2614 if (!isl_options_get_ast_build_detect_min_max(ctx))
2615 return isl_bool_false;
2617 n = isl_set_list_n_set(data->p[data->n].set_list);
2618 if (n < 0)
2619 return isl_bool_error;
2621 dom = isl_ast_build_get_domain(data->build);
2622 set = isl_set_intersect(dom, isl_set_copy(set));
2624 for (i = 0; i < n ; ++i) {
2625 isl_aff *aff_i;
2626 isl_set *valid;
2627 isl_set *dom, *required;
2628 isl_bool is_valid;
2630 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2631 valid = isl_set_from_basic_set(test(isl_aff_copy(aff), aff_i));
2632 required = isl_set_list_get_set(data->p[data->n].set_list, i);
2633 dom = isl_ast_build_get_domain(data->build);
2634 required = isl_set_intersect(dom, required);
2635 is_valid = isl_set_is_subset(required, valid);
2636 isl_set_free(required);
2637 isl_set_free(valid);
2638 if (is_valid < 0 || !is_valid) {
2639 isl_set_free(set);
2640 return is_valid;
2643 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2644 valid = isl_set_from_basic_set(test(aff_i, isl_aff_copy(aff)));
2645 is_valid = isl_set_is_subset(set, valid);
2646 isl_set_free(valid);
2647 if (is_valid < 0 || !is_valid) {
2648 isl_set_free(set);
2649 return is_valid;
2653 isl_set_free(set);
2654 return isl_bool_true;
2657 /* Can the list of pieces in "data" be extended with "set" and "aff"
2658 * to form/preserve a minimum expression?
2659 * In particular, is it the case for each entry (set_i, aff_i) that
2661 * aff >= aff_i on set_i, and
2662 * aff_i >= aff on set?
2664 static isl_bool extends_min(struct isl_from_pw_aff_data *data,
2665 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
2667 return extends(data, set, aff, &isl_aff_ge_basic_set);
2670 /* Can the list of pieces in "data" be extended with "set" and "aff"
2671 * to form/preserve a maximum expression?
2672 * In particular, is it the case for each entry (set_i, aff_i) that
2674 * aff <= aff_i on set_i, and
2675 * aff_i <= aff on set?
2677 static isl_bool extends_max(struct isl_from_pw_aff_data *data,
2678 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
2680 return extends(data, set, aff, &isl_aff_le_basic_set);
2683 /* This function is called during the construction of an isl_ast_expr
2684 * that evaluates an isl_pw_aff.
2685 * If the last piece of "data" contains a single subpiece and
2686 * if its affine function is equal to "aff" on a part of the domain
2687 * that includes either "set" or the domain of that single subpiece,
2688 * then extend the domain of that single subpiece with "set".
2689 * If it was the original domain of the single subpiece where
2690 * the two affine functions are equal, then also replace
2691 * the affine function of the single subpiece by "aff".
2692 * If the last piece of "data" contains either a single subpiece
2693 * or a minimum, then check if this minimum expression can be extended
2694 * with (set, aff).
2695 * If so, extend the sequence and return.
2696 * Perform the same operation for maximum expressions.
2697 * If no such extension can be performed, then move to the next piece
2698 * in "data" (if the current piece contains any data), and then store
2699 * the current subpiece in the current piece of "data" for later handling.
2701 static isl_stat ast_expr_from_pw_aff(__isl_take isl_set *set,
2702 __isl_take isl_aff *aff, void *user)
2704 struct isl_from_pw_aff_data *data = user;
2705 isl_bool test;
2706 enum isl_from_pw_aff_state state;
2708 state = data->p[data->n].state;
2709 if (state == isl_state_single) {
2710 isl_aff *aff0;
2711 isl_set *eq;
2712 isl_bool subset1, subset2 = isl_bool_false;
2713 aff0 = isl_aff_list_get_aff(data->p[data->n].aff_list, 0);
2714 eq = isl_aff_eq_set(isl_aff_copy(aff), aff0);
2715 subset1 = isl_set_is_subset(set, eq);
2716 if (subset1 >= 0 && !subset1)
2717 subset2 = single_is_subset(data, eq);
2718 isl_set_free(eq);
2719 if (subset1 < 0 || subset2 < 0)
2720 goto error;
2721 if (subset1)
2722 return extend_domain(data, set, aff, 0);
2723 if (subset2)
2724 return extend_domain(data, set, aff, 1);
2726 if (state == isl_state_single || state == isl_state_min) {
2727 test = extends_min(data, set, aff);
2728 if (test < 0)
2729 goto error;
2730 if (test)
2731 return extend_min(data, set, aff);
2733 if (state == isl_state_single || state == isl_state_max) {
2734 test = extends_max(data, set, aff);
2735 if (test < 0)
2736 goto error;
2737 if (test)
2738 return extend_max(data, set, aff);
2740 if (state != isl_state_none)
2741 data->n++;
2742 set_single(data, set, aff);
2744 return isl_stat_ok;
2745 error:
2746 isl_set_free(set);
2747 isl_aff_free(aff);
2748 return isl_stat_error;
2751 /* Construct an isl_ast_expr that evaluates "pa".
2752 * The result is simplified in terms of build->domain.
2754 * The domain of "pa" lives in the internal schedule space.
2756 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff_internal(
2757 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2759 struct isl_from_pw_aff_data data = { NULL };
2760 isl_ast_expr *res = NULL;
2762 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
2763 pa = isl_pw_aff_coalesce(pa);
2764 if (!pa)
2765 return NULL;
2767 if (isl_from_pw_aff_data_init(&data, build, pa) < 0)
2768 goto error;
2769 set_none(&data);
2771 if (isl_pw_aff_foreach_piece(pa, &ast_expr_from_pw_aff, &data) >= 0)
2772 res = build_pieces(&data);
2774 isl_pw_aff_free(pa);
2775 isl_from_pw_aff_data_clear(&data);
2776 return res;
2777 error:
2778 isl_pw_aff_free(pa);
2779 isl_from_pw_aff_data_clear(&data);
2780 return NULL;
2783 /* Construct an isl_ast_expr that evaluates "pa".
2784 * The result is simplified in terms of build->domain.
2786 * The domain of "pa" lives in the external schedule space.
2788 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff(
2789 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2791 isl_ast_expr *expr;
2792 isl_bool needs_map;
2794 needs_map = isl_ast_build_need_schedule_map(build);
2795 if (needs_map < 0) {
2796 pa = isl_pw_aff_free(pa);
2797 } else if (needs_map) {
2798 isl_multi_aff *ma;
2799 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2800 pa = isl_pw_aff_pullback_multi_aff(pa, ma);
2802 expr = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2803 return expr;
2806 /* Set the ids of the input dimensions of "mpa" to the iterator ids
2807 * of "build".
2809 * The domain of "mpa" is assumed to live in the internal schedule domain.
2811 static __isl_give isl_multi_pw_aff *set_iterator_names(
2812 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2814 int i;
2815 isl_size n;
2817 n = isl_multi_pw_aff_dim(mpa, isl_dim_in);
2818 if (n < 0)
2819 return isl_multi_pw_aff_free(mpa);
2820 for (i = 0; i < n; ++i) {
2821 isl_id *id;
2823 id = isl_ast_build_get_iterator_id(build, i);
2824 mpa = isl_multi_pw_aff_set_dim_id(mpa, isl_dim_in, i, id);
2827 return mpa;
2830 /* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
2831 * the remaining arguments derived from "mpa".
2832 * That is, construct a call or access expression that calls/accesses "arg0"
2833 * with arguments/indices specified by "mpa".
2835 static __isl_give isl_ast_expr *isl_ast_build_with_arguments(
2836 __isl_keep isl_ast_build *build, enum isl_ast_expr_op_type type,
2837 __isl_take isl_ast_expr *arg0, __isl_take isl_multi_pw_aff *mpa)
2839 int i;
2840 isl_size n;
2841 isl_ctx *ctx;
2842 isl_ast_expr *expr;
2844 ctx = isl_ast_build_get_ctx(build);
2846 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
2847 expr = n >= 0 ? isl_ast_expr_alloc_op(ctx, type, 1 + n) : NULL;
2848 expr = isl_ast_expr_op_add_arg(expr, arg0);
2849 for (i = 0; i < n; ++i) {
2850 isl_pw_aff *pa;
2851 isl_ast_expr *arg;
2853 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
2854 arg = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2855 expr = isl_ast_expr_op_add_arg(expr, arg);
2858 isl_multi_pw_aff_free(mpa);
2859 return expr;
2862 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2863 __isl_keep isl_ast_build *build, enum isl_ast_expr_op_type type,
2864 __isl_take isl_multi_pw_aff *mpa);
2866 /* Construct an isl_ast_expr that accesses the member specified by "mpa".
2867 * The range of "mpa" is assumed to be wrapped relation.
2868 * The domain of this wrapped relation specifies the structure being
2869 * accessed, while the range of this wrapped relation spacifies the
2870 * member of the structure being accessed.
2872 * The domain of "mpa" is assumed to live in the internal schedule domain.
2874 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_member(
2875 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2877 isl_id *id;
2878 isl_multi_pw_aff *domain;
2879 isl_ast_expr *domain_expr, *expr;
2880 enum isl_ast_expr_op_type type = isl_ast_expr_op_access;
2882 domain = isl_multi_pw_aff_copy(mpa);
2883 domain = isl_multi_pw_aff_range_factor_domain(domain);
2884 domain_expr = isl_ast_build_from_multi_pw_aff_internal(build,
2885 type, domain);
2886 mpa = isl_multi_pw_aff_range_factor_range(mpa);
2887 if (!isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2888 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2889 "missing field name", goto error);
2890 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2891 expr = isl_ast_expr_from_id(id);
2892 expr = isl_ast_expr_alloc_binary(isl_ast_expr_op_member,
2893 domain_expr, expr);
2894 return isl_ast_build_with_arguments(build, type, expr, mpa);
2895 error:
2896 isl_multi_pw_aff_free(mpa);
2897 return NULL;
2900 /* Construct an isl_ast_expr of type "type" that calls or accesses
2901 * the element specified by "mpa".
2902 * The first argument is obtained from the output tuple name.
2903 * The remaining arguments are given by the piecewise affine expressions.
2905 * If the range of "mpa" is a mapped relation, then we assume it
2906 * represents an access to a member of a structure.
2908 * The domain of "mpa" is assumed to live in the internal schedule domain.
2910 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2911 __isl_keep isl_ast_build *build, enum isl_ast_expr_op_type type,
2912 __isl_take isl_multi_pw_aff *mpa)
2914 isl_ctx *ctx;
2915 isl_id *id;
2916 isl_ast_expr *expr;
2918 if (!mpa)
2919 goto error;
2921 if (type == isl_ast_expr_op_access &&
2922 isl_multi_pw_aff_range_is_wrapping(mpa))
2923 return isl_ast_build_from_multi_pw_aff_member(build, mpa);
2925 mpa = set_iterator_names(build, mpa);
2926 if (!build || !mpa)
2927 goto error;
2929 ctx = isl_ast_build_get_ctx(build);
2931 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2932 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2933 else
2934 id = isl_id_alloc(ctx, "", NULL);
2936 expr = isl_ast_expr_from_id(id);
2937 return isl_ast_build_with_arguments(build, type, expr, mpa);
2938 error:
2939 isl_multi_pw_aff_free(mpa);
2940 return NULL;
2943 /* Construct an isl_ast_expr of type "type" that calls or accesses
2944 * the element specified by "pma".
2945 * The first argument is obtained from the output tuple name.
2946 * The remaining arguments are given by the piecewise affine expressions.
2948 * The domain of "pma" is assumed to live in the internal schedule domain.
2950 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff_internal(
2951 __isl_keep isl_ast_build *build, enum isl_ast_expr_op_type type,
2952 __isl_take isl_pw_multi_aff *pma)
2954 isl_multi_pw_aff *mpa;
2956 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2957 return isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2960 /* Construct an isl_ast_expr of type "type" that calls or accesses
2961 * the element specified by "mpa".
2962 * The first argument is obtained from the output tuple name.
2963 * The remaining arguments are given by the piecewise affine expressions.
2965 * The domain of "mpa" is assumed to live in the external schedule domain.
2967 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff(
2968 __isl_keep isl_ast_build *build, enum isl_ast_expr_op_type type,
2969 __isl_take isl_multi_pw_aff *mpa)
2971 isl_bool is_domain;
2972 isl_bool needs_map;
2973 isl_ast_expr *expr;
2974 isl_space *space_build, *space_mpa;
2976 space_build = isl_ast_build_get_space(build, 0);
2977 space_mpa = isl_multi_pw_aff_get_space(mpa);
2978 is_domain = isl_space_tuple_is_equal(space_build, isl_dim_set,
2979 space_mpa, isl_dim_in);
2980 isl_space_free(space_build);
2981 isl_space_free(space_mpa);
2982 if (is_domain < 0)
2983 goto error;
2984 if (!is_domain)
2985 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2986 "spaces don't match", goto error);
2988 needs_map = isl_ast_build_need_schedule_map(build);
2989 if (needs_map < 0)
2990 goto error;
2991 if (needs_map) {
2992 isl_multi_aff *ma;
2993 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2994 mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
2997 expr = isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2998 return expr;
2999 error:
3000 isl_multi_pw_aff_free(mpa);
3001 return NULL;
3004 /* Construct an isl_ast_expr that calls the domain element specified by "mpa".
3005 * The name of the function is obtained from the output tuple name.
3006 * The arguments are given by the piecewise affine expressions.
3008 * The domain of "mpa" is assumed to live in the external schedule domain.
3010 __isl_give isl_ast_expr *isl_ast_build_call_from_multi_pw_aff(
3011 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
3013 return isl_ast_build_from_multi_pw_aff(build,
3014 isl_ast_expr_op_call, mpa);
3017 /* Construct an isl_ast_expr that accesses the array element specified by "mpa".
3018 * The name of the array is obtained from the output tuple name.
3019 * The index expressions are given by the piecewise affine expressions.
3021 * The domain of "mpa" is assumed to live in the external schedule domain.
3023 __isl_give isl_ast_expr *isl_ast_build_access_from_multi_pw_aff(
3024 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
3026 return isl_ast_build_from_multi_pw_aff(build,
3027 isl_ast_expr_op_access, mpa);
3030 /* Construct an isl_ast_expr of type "type" that calls or accesses
3031 * the element specified by "pma".
3032 * The first argument is obtained from the output tuple name.
3033 * The remaining arguments are given by the piecewise affine expressions.
3035 * The domain of "pma" is assumed to live in the external schedule domain.
3037 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff(
3038 __isl_keep isl_ast_build *build, enum isl_ast_expr_op_type type,
3039 __isl_take isl_pw_multi_aff *pma)
3041 isl_multi_pw_aff *mpa;
3043 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
3044 return isl_ast_build_from_multi_pw_aff(build, type, mpa);
3047 /* Construct an isl_ast_expr that calls the domain element specified by "pma".
3048 * The name of the function is obtained from the output tuple name.
3049 * The arguments are given by the piecewise affine expressions.
3051 * The domain of "pma" is assumed to live in the external schedule domain.
3053 __isl_give isl_ast_expr *isl_ast_build_call_from_pw_multi_aff(
3054 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
3056 return isl_ast_build_from_pw_multi_aff(build,
3057 isl_ast_expr_op_call, pma);
3060 /* Construct an isl_ast_expr that accesses the array element specified by "pma".
3061 * The name of the array is obtained from the output tuple name.
3062 * The index expressions are given by the piecewise affine expressions.
3064 * The domain of "pma" is assumed to live in the external schedule domain.
3066 __isl_give isl_ast_expr *isl_ast_build_access_from_pw_multi_aff(
3067 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
3069 return isl_ast_build_from_pw_multi_aff(build,
3070 isl_ast_expr_op_access, pma);
3073 /* Construct an isl_ast_expr that calls the domain element
3074 * specified by "executed".
3076 * "executed" is assumed to be single-valued, with a domain that lives
3077 * in the internal schedule space.
3079 __isl_give isl_ast_node *isl_ast_build_call_from_executed(
3080 __isl_keep isl_ast_build *build, __isl_take isl_map *executed)
3082 isl_pw_multi_aff *iteration;
3083 isl_ast_expr *expr;
3085 iteration = isl_pw_multi_aff_from_map(executed);
3086 iteration = isl_ast_build_compute_gist_pw_multi_aff(build, iteration);
3087 iteration = isl_pw_multi_aff_intersect_domain(iteration,
3088 isl_ast_build_get_domain(build));
3089 expr = isl_ast_build_from_pw_multi_aff_internal(build,
3090 isl_ast_expr_op_call, iteration);
3091 return isl_ast_node_alloc_user(expr);