extract out shared isl_point_dim
[isl.git] / isl_ast_build_expr.c
blob8704298c67c001983ebf58a608fe0b7c4195bd64
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 * "cst" is the constant term of the expression in which the added term
44 * appears. It may be modified by isl_ast_expr_add_term.
46 * "v" is the coefficient of the term that is being constructed and
47 * is set internally by isl_ast_expr_add_term.
49 struct isl_ast_add_term_data {
50 isl_ast_build *build;
51 isl_val *cst;
52 isl_val *v;
55 /* Given the numerator "aff" of the argument of an integer division
56 * with denominator "d", check if it can be made non-negative over
57 * data->build->domain by stealing part of the constant term of
58 * the expression in which the integer division appears.
60 * In particular, the outer expression is of the form
62 * v * floor(aff/d) + cst
64 * We already know that "aff" itself may attain negative values.
65 * Here we check if aff + d*floor(cst/v) is non-negative, such
66 * that we could rewrite the expression to
68 * v * floor((aff + d*floor(cst/v))/d) + cst - v*floor(cst/v)
70 * Note that aff + d*floor(cst/v) can only possibly be non-negative
71 * if data->cst and data->v have the same sign.
72 * Similarly, if floor(cst/v) is zero, then there is no point in
73 * checking again.
75 static int is_non_neg_after_stealing(__isl_keep isl_aff *aff,
76 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
78 isl_aff *shifted;
79 isl_val *shift;
80 int is_zero;
81 int non_neg;
83 if (isl_val_sgn(data->cst) != isl_val_sgn(data->v))
84 return 0;
86 shift = isl_val_div(isl_val_copy(data->cst), isl_val_copy(data->v));
87 shift = isl_val_floor(shift);
88 is_zero = isl_val_is_zero(shift);
89 if (is_zero < 0 || is_zero) {
90 isl_val_free(shift);
91 return is_zero < 0 ? -1 : 0;
93 shift = isl_val_mul(shift, isl_val_copy(d));
94 shifted = isl_aff_copy(aff);
95 shifted = isl_aff_add_constant_val(shifted, shift);
96 non_neg = isl_ast_build_aff_is_nonneg(data->build, shifted);
97 isl_aff_free(shifted);
99 return non_neg;
102 /* Given the numerator "aff' of the argument of an integer division
103 * with denominator "d", steal part of the constant term of
104 * the expression in which the integer division appears to make it
105 * non-negative over data->build->domain.
107 * In particular, the outer expression is of the form
109 * v * floor(aff/d) + cst
111 * We know that "aff" itself may attain negative values,
112 * but that aff + d*floor(cst/v) is non-negative.
113 * Find the minimal positive value that we need to add to "aff"
114 * to make it positive and adjust data->cst accordingly.
115 * That is, compute the minimal value "m" of "aff" over
116 * data->build->domain and take
118 * s = ceil(m/d)
120 * such that
122 * aff + d * s >= 0
124 * and rewrite the expression to
126 * v * floor((aff + s*d)/d) + (cst - v*s)
128 static __isl_give isl_aff *steal_from_cst(__isl_take isl_aff *aff,
129 __isl_keep isl_val *d, struct isl_ast_add_term_data *data)
131 isl_set *domain;
132 isl_val *shift, *t;
134 domain = isl_ast_build_get_domain(data->build);
135 shift = isl_set_min_val(domain, aff);
136 isl_set_free(domain);
138 shift = isl_val_neg(shift);
139 shift = isl_val_div(shift, isl_val_copy(d));
140 shift = isl_val_ceil(shift);
142 t = isl_val_copy(shift);
143 t = isl_val_mul(t, isl_val_copy(data->v));
144 data->cst = isl_val_sub(data->cst, t);
146 shift = isl_val_mul(shift, isl_val_copy(d));
147 return isl_aff_add_constant_val(aff, shift);
150 /* Create an isl_ast_expr evaluating the div at position "pos" in "ls".
151 * The result is simplified in terms of data->build->domain.
152 * This function may change (the sign of) data->v.
154 * "ls" is known to be non-NULL.
156 * Let the div be of the form floor(e/d).
157 * If the ast_build_prefer_pdiv option is set then we check if "e"
158 * is non-negative, so that we can generate
160 * (pdiv_q, expr(e), expr(d))
162 * instead of
164 * (fdiv_q, expr(e), expr(d))
166 * If the ast_build_prefer_pdiv option is set and
167 * if "e" is not non-negative, then we check if "-e + d - 1" is non-negative.
168 * If so, we can rewrite
170 * floor(e/d) = -ceil(-e/d) = -floor((-e + d - 1)/d)
172 * and still use pdiv_q, while changing the sign of data->v.
174 * Otherwise, we check if
176 * e + d*floor(cst/v)
178 * is non-negative and if so, replace floor(e/d) by
180 * floor((e + s*d)/d) - s
182 * with s the minimal shift that makes the argument non-negative.
184 static __isl_give isl_ast_expr *var_div(struct isl_ast_add_term_data *data,
185 __isl_keep isl_local_space *ls, int pos)
187 isl_ctx *ctx = isl_local_space_get_ctx(ls);
188 isl_aff *aff;
189 isl_ast_expr *num, *den;
190 isl_val *d;
191 enum isl_ast_op_type type;
193 aff = isl_local_space_get_div(ls, pos);
194 d = isl_aff_get_denominator_val(aff);
195 aff = isl_aff_scale_val(aff, isl_val_copy(d));
196 den = isl_ast_expr_from_val(isl_val_copy(d));
198 type = isl_ast_op_fdiv_q;
199 if (isl_options_get_ast_build_prefer_pdiv(ctx)) {
200 int non_neg = isl_ast_build_aff_is_nonneg(data->build, aff);
201 if (non_neg >= 0 && !non_neg) {
202 isl_aff *opp = oppose_div_arg(isl_aff_copy(aff),
203 isl_val_copy(d));
204 non_neg = isl_ast_build_aff_is_nonneg(data->build, opp);
205 if (non_neg >= 0 && non_neg) {
206 data->v = isl_val_neg(data->v);
207 isl_aff_free(aff);
208 aff = opp;
209 } else
210 isl_aff_free(opp);
212 if (non_neg >= 0 && !non_neg) {
213 non_neg = is_non_neg_after_stealing(aff, d, data);
214 if (non_neg >= 0 && non_neg)
215 aff = steal_from_cst(aff, d, data);
217 if (non_neg < 0)
218 aff = isl_aff_free(aff);
219 else if (non_neg)
220 type = isl_ast_op_pdiv_q;
223 isl_val_free(d);
224 num = isl_ast_expr_from_aff(aff, data->build);
225 return isl_ast_expr_alloc_binary(type, num, den);
228 /* Create an isl_ast_expr evaluating the specified dimension of "ls".
229 * The result is simplified in terms of data->build->domain.
230 * This function may change (the sign of) data->v.
232 * The isl_ast_expr is constructed based on the type of the dimension.
233 * - divs are constructed by var_div
234 * - set variables are constructed from the iterator isl_ids in data->build
235 * - parameters are constructed from the isl_ids in "ls"
237 static __isl_give isl_ast_expr *var(struct isl_ast_add_term_data *data,
238 __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos)
240 isl_ctx *ctx = isl_local_space_get_ctx(ls);
241 isl_id *id;
243 if (type == isl_dim_div)
244 return var_div(data, ls, pos);
246 if (type == isl_dim_set) {
247 id = isl_ast_build_get_iterator_id(data->build, pos);
248 return isl_ast_expr_from_id(id);
251 if (!isl_local_space_has_dim_id(ls, type, pos))
252 isl_die(ctx, isl_error_internal, "unnamed dimension",
253 return NULL);
254 id = isl_local_space_get_dim_id(ls, type, pos);
255 return isl_ast_expr_from_id(id);
258 /* Does "expr" represent the zero integer?
260 static int ast_expr_is_zero(__isl_keep isl_ast_expr *expr)
262 if (!expr)
263 return -1;
264 if (expr->type != isl_ast_expr_int)
265 return 0;
266 return isl_val_is_zero(expr->u.v);
269 /* Create an expression representing the sum of "expr1" and "expr2",
270 * provided neither of the two expressions is identically zero.
272 static __isl_give isl_ast_expr *ast_expr_add(__isl_take isl_ast_expr *expr1,
273 __isl_take isl_ast_expr *expr2)
275 if (!expr1 || !expr2)
276 goto error;
278 if (ast_expr_is_zero(expr1)) {
279 isl_ast_expr_free(expr1);
280 return expr2;
283 if (ast_expr_is_zero(expr2)) {
284 isl_ast_expr_free(expr2);
285 return expr1;
288 return isl_ast_expr_add(expr1, expr2);
289 error:
290 isl_ast_expr_free(expr1);
291 isl_ast_expr_free(expr2);
292 return NULL;
295 /* Subtract expr2 from expr1.
297 * If expr2 is zero, we simply return expr1.
298 * If expr1 is zero, we return
300 * (isl_ast_op_minus, expr2)
302 * Otherwise, we return
304 * (isl_ast_op_sub, expr1, expr2)
306 static __isl_give isl_ast_expr *ast_expr_sub(__isl_take isl_ast_expr *expr1,
307 __isl_take isl_ast_expr *expr2)
309 if (!expr1 || !expr2)
310 goto error;
312 if (ast_expr_is_zero(expr2)) {
313 isl_ast_expr_free(expr2);
314 return expr1;
317 if (ast_expr_is_zero(expr1)) {
318 isl_ast_expr_free(expr1);
319 return isl_ast_expr_neg(expr2);
322 return isl_ast_expr_sub(expr1, expr2);
323 error:
324 isl_ast_expr_free(expr1);
325 isl_ast_expr_free(expr2);
326 return NULL;
329 /* Return an isl_ast_expr that represents
331 * v * (aff mod d)
333 * v is assumed to be non-negative.
334 * The result is simplified in terms of build->domain.
336 static __isl_give isl_ast_expr *isl_ast_expr_mod(__isl_keep isl_val *v,
337 __isl_keep isl_aff *aff, __isl_keep isl_val *d,
338 __isl_keep isl_ast_build *build)
340 isl_ast_expr *expr;
341 isl_ast_expr *c;
343 if (!aff)
344 return NULL;
346 expr = isl_ast_expr_from_aff(isl_aff_copy(aff), build);
348 c = isl_ast_expr_from_val(isl_val_copy(d));
349 expr = isl_ast_expr_alloc_binary(isl_ast_op_pdiv_r, expr, c);
351 if (!isl_val_is_one(v)) {
352 c = isl_ast_expr_from_val(isl_val_copy(v));
353 expr = isl_ast_expr_mul(c, expr);
356 return expr;
359 /* Create an isl_ast_expr that scales "expr" by "v".
361 * If v is 1, we simply return expr.
362 * If v is -1, we return
364 * (isl_ast_op_minus, expr)
366 * Otherwise, we return
368 * (isl_ast_op_mul, expr(v), expr)
370 static __isl_give isl_ast_expr *scale(__isl_take isl_ast_expr *expr,
371 __isl_take isl_val *v)
373 isl_ast_expr *c;
375 if (!expr || !v)
376 goto error;
377 if (isl_val_is_one(v)) {
378 isl_val_free(v);
379 return expr;
382 if (isl_val_is_negone(v)) {
383 isl_val_free(v);
384 expr = isl_ast_expr_neg(expr);
385 } else {
386 c = isl_ast_expr_from_val(v);
387 expr = isl_ast_expr_mul(c, expr);
390 return expr;
391 error:
392 isl_val_free(v);
393 isl_ast_expr_free(expr);
394 return NULL;
397 /* Add an expression for "*v" times the specified dimension of "ls"
398 * to expr.
399 * If the dimension is an integer division, then this function
400 * may modify data->cst in order to make the numerator non-negative.
401 * The result is simplified in terms of data->build->domain.
403 * Let e be the expression for the specified dimension,
404 * multiplied by the absolute value of "*v".
405 * If "*v" is negative, we create
407 * (isl_ast_op_sub, expr, e)
409 * except when expr is trivially zero, in which case we create
411 * (isl_ast_op_minus, e)
413 * instead.
415 * If "*v" is positive, we simply create
417 * (isl_ast_op_add, expr, e)
420 static __isl_give isl_ast_expr *isl_ast_expr_add_term(
421 __isl_take isl_ast_expr *expr,
422 __isl_keep isl_local_space *ls, enum isl_dim_type type, int pos,
423 __isl_take isl_val *v, struct isl_ast_add_term_data *data)
425 isl_ast_expr *term;
427 if (!expr)
428 return NULL;
430 data->v = v;
431 term = var(data, ls, type, pos);
432 v = data->v;
434 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
435 v = isl_val_neg(v);
436 term = scale(term, v);
437 return ast_expr_sub(expr, term);
438 } else {
439 term = scale(term, v);
440 return ast_expr_add(expr, term);
444 /* Add an expression for "v" to expr.
446 static __isl_give isl_ast_expr *isl_ast_expr_add_int(
447 __isl_take isl_ast_expr *expr, __isl_take isl_val *v)
449 isl_ast_expr *expr_int;
451 if (!expr || !v)
452 goto error;
454 if (isl_val_is_zero(v)) {
455 isl_val_free(v);
456 return expr;
459 if (isl_val_is_neg(v) && !ast_expr_is_zero(expr)) {
460 v = isl_val_neg(v);
461 expr_int = isl_ast_expr_from_val(v);
462 return ast_expr_sub(expr, expr_int);
463 } else {
464 expr_int = isl_ast_expr_from_val(v);
465 return ast_expr_add(expr, expr_int);
467 error:
468 isl_ast_expr_free(expr);
469 isl_val_free(v);
470 return NULL;
473 /* Internal data structure used inside extract_modulos.
475 * If any modulo expressions are detected in "aff", then the
476 * expression is removed from "aff" and added to either "pos" or "neg"
477 * depending on the sign of the coefficient of the modulo expression
478 * inside "aff".
480 * "add" is an expression that needs to be added to "aff" at the end of
481 * the computation. It is NULL as long as no modulos have been extracted.
483 * "i" is the position in "aff" of the div under investigation
484 * "v" is the coefficient in "aff" of the div
485 * "div" is the argument of the div, with the denominator removed
486 * "d" is the original denominator of the argument of the div
488 * "nonneg" is an affine expression that is non-negative over "build"
489 * and that can be used to extract a modulo expression from "div".
490 * In particular, if "sign" is 1, then the coefficients of "nonneg"
491 * are equal to those of "div" modulo "d". If "sign" is -1, then
492 * the coefficients of "nonneg" are opposite to those of "div" modulo "d".
493 * If "sign" is 0, then no such affine expression has been found (yet).
495 struct isl_extract_mod_data {
496 isl_ast_build *build;
497 isl_aff *aff;
499 isl_ast_expr *pos;
500 isl_ast_expr *neg;
502 isl_aff *add;
504 int i;
505 isl_val *v;
506 isl_val *d;
507 isl_aff *div;
509 isl_aff *nonneg;
510 int sign;
513 /* Given that data->v * div_i in data->aff is equal to
515 * f * (term - (arg mod d))
517 * with data->d * f = data->v, add
519 * f * term
521 * to data->add and
523 * abs(f) * (arg mod d)
525 * to data->neg or data->pos depending on the sign of -f.
527 static int extract_term_and_mod(struct isl_extract_mod_data *data,
528 __isl_take isl_aff *term, __isl_take isl_aff *arg)
530 isl_ast_expr *expr;
531 int s;
533 data->v = isl_val_div(data->v, isl_val_copy(data->d));
534 s = isl_val_sgn(data->v);
535 data->v = isl_val_abs(data->v);
536 expr = isl_ast_expr_mod(data->v, arg, data->d, data->build);
537 isl_aff_free(arg);
538 if (s > 0)
539 data->neg = ast_expr_add(data->neg, expr);
540 else
541 data->pos = ast_expr_add(data->pos, expr);
542 data->aff = isl_aff_set_coefficient_si(data->aff,
543 isl_dim_div, data->i, 0);
544 if (s < 0)
545 data->v = isl_val_neg(data->v);
546 term = isl_aff_scale_val(term, isl_val_copy(data->v));
548 if (!data->add)
549 data->add = term;
550 else
551 data->add = isl_aff_add(data->add, term);
552 if (!data->add)
553 return -1;
555 return 0;
558 /* Given that data->v * div_i in data->aff is of the form
560 * f * d * floor(div/d)
562 * with div nonnegative on data->build, rewrite it as
564 * f * (div - (div mod d)) = f * div - f * (div mod d)
566 * and add
568 * f * div
570 * to data->add and
572 * abs(f) * (div mod d)
574 * to data->neg or data->pos depending on the sign of -f.
576 static int extract_mod(struct isl_extract_mod_data *data)
578 return extract_term_and_mod(data, isl_aff_copy(data->div),
579 isl_aff_copy(data->div));
582 /* Given that data->v * div_i in data->aff is of the form
584 * f * d * floor(div/d) (1)
586 * check if div is non-negative on data->build and, if so,
587 * extract the corresponding modulo from data->aff.
588 * If not, then check if
590 * -div + d - 1
592 * is non-negative on data->build. If so, replace (1) by
594 * -f * d * floor((-div + d - 1)/d)
596 * and extract the corresponding modulo from data->aff.
598 * This function may modify data->div.
600 static int extract_nonneg_mod(struct isl_extract_mod_data *data)
602 int mod;
604 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
605 if (mod < 0)
606 goto error;
607 if (mod)
608 return extract_mod(data);
610 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
611 mod = isl_ast_build_aff_is_nonneg(data->build, data->div);
612 if (mod < 0)
613 goto error;
614 if (mod) {
615 data->v = isl_val_neg(data->v);
616 return extract_mod(data);
619 return 0;
620 error:
621 data->aff = isl_aff_free(data->aff);
622 return -1;
625 /* Is the affine expression of constraint "c" "simpler" than data->nonneg
626 * for use in extracting a modulo expression?
628 * We currently only consider the constant term of the affine expression.
629 * In particular, we prefer the affine expression with the smallest constant
630 * term.
631 * This means that if there are two constraints, say x >= 0 and -x + 10 >= 0,
632 * then we would pick x >= 0
634 * More detailed heuristics could be used if it turns out that there is a need.
636 static int mod_constraint_is_simpler(struct isl_extract_mod_data *data,
637 __isl_keep isl_constraint *c)
639 isl_val *v1, *v2;
640 int simpler;
642 if (!data->nonneg)
643 return 1;
645 v1 = isl_val_abs(isl_constraint_get_constant_val(c));
646 v2 = isl_val_abs(isl_aff_get_constant_val(data->nonneg));
647 simpler = isl_val_lt(v1, v2);
648 isl_val_free(v1);
649 isl_val_free(v2);
651 return simpler;
654 /* Check if the coefficients of "c" are either equal or opposite to those
655 * of data->div modulo data->d. If so, and if "c" is "simpler" than
656 * data->nonneg, then replace data->nonneg by the affine expression of "c"
657 * and set data->sign accordingly.
659 * Both "c" and data->div are assumed not to involve any integer divisions.
661 * Before we start the actual comparison, we first quickly check if
662 * "c" and data->div have the same non-zero coefficients.
663 * If not, then we assume that "c" is not of the desired form.
664 * Note that while the coefficients of data->div can be reasonably expected
665 * not to involve any coefficients that are multiples of d, "c" may
666 * very well involve such coefficients. This means that we may actually
667 * miss some cases.
669 * If the constant term is "too large", then the constraint is rejected,
670 * where "too large" is fairly arbitrarily set to 1 << 15.
671 * We do this to avoid picking up constraints that bound a variable
672 * by a very large number, say the largest or smallest possible
673 * variable in the representation of some integer type.
675 static isl_stat check_parallel_or_opposite(__isl_take isl_constraint *c,
676 void *user)
678 struct isl_extract_mod_data *data = user;
679 enum isl_dim_type c_type[2] = { isl_dim_param, isl_dim_set };
680 enum isl_dim_type a_type[2] = { isl_dim_param, isl_dim_in };
681 int i, t;
682 int n[2];
683 int parallel = 1, opposite = 1;
685 for (t = 0; t < 2; ++t) {
686 n[t] = isl_constraint_dim(c, c_type[t]);
687 for (i = 0; i < n[t]; ++i) {
688 int a, b;
690 a = isl_constraint_involves_dims(c, c_type[t], i, 1);
691 b = isl_aff_involves_dims(data->div, a_type[t], i, 1);
692 if (a != b)
693 parallel = opposite = 0;
697 if (parallel || opposite) {
698 isl_val *v;
700 v = isl_val_abs(isl_constraint_get_constant_val(c));
701 if (isl_val_cmp_si(v, 1 << 15) > 0)
702 parallel = opposite = 0;
703 isl_val_free(v);
706 for (t = 0; t < 2; ++t) {
707 for (i = 0; i < n[t]; ++i) {
708 isl_val *v1, *v2;
710 if (!parallel && !opposite)
711 break;
712 v1 = isl_constraint_get_coefficient_val(c,
713 c_type[t], i);
714 v2 = isl_aff_get_coefficient_val(data->div,
715 a_type[t], i);
716 if (parallel) {
717 v1 = isl_val_sub(v1, isl_val_copy(v2));
718 parallel = isl_val_is_divisible_by(v1, data->d);
719 v1 = isl_val_add(v1, isl_val_copy(v2));
721 if (opposite) {
722 v1 = isl_val_add(v1, isl_val_copy(v2));
723 opposite = isl_val_is_divisible_by(v1, data->d);
725 isl_val_free(v1);
726 isl_val_free(v2);
730 if ((parallel || opposite) && mod_constraint_is_simpler(data, c)) {
731 isl_aff_free(data->nonneg);
732 data->nonneg = isl_constraint_get_aff(c);
733 data->sign = parallel ? 1 : -1;
736 isl_constraint_free(c);
738 if (data->sign != 0 && data->nonneg == NULL)
739 return isl_stat_error;
741 return isl_stat_ok;
744 /* Given that data->v * div_i in data->aff is of the form
746 * f * d * floor(div/d) (1)
748 * see if we can find an expression div' that is non-negative over data->build
749 * and that is related to div through
751 * div' = div + d * e
753 * or
755 * div' = -div + d - 1 + d * e
757 * with e some affine expression.
758 * If so, we write (1) as
760 * f * div + f * (div' mod d)
762 * or
764 * -f * (-div + d - 1) - f * (div' mod d)
766 * exploiting (in the second case) the fact that
768 * f * d * floor(div/d) = -f * d * floor((-div + d - 1)/d)
771 * We first try to find an appropriate expression for div'
772 * from the constraints of data->build->domain (which is therefore
773 * guaranteed to be non-negative on data->build), where we remove
774 * any integer divisions from the constraints and skip this step
775 * if "div" itself involves any integer divisions.
776 * If we cannot find an appropriate expression this way, then
777 * we pass control to extract_nonneg_mod where check
778 * if div or "-div + d -1" themselves happen to be
779 * non-negative on data->build.
781 * While looking for an appropriate constraint in data->build->domain,
782 * we ignore the constant term, so after finding such a constraint,
783 * we still need to fix up the constant term.
784 * In particular, if a is the constant term of "div"
785 * (or d - 1 - the constant term of "div" if data->sign < 0)
786 * and b is the constant term of the constraint, then we need to find
787 * a non-negative constant c such that
789 * b + c \equiv a mod d
791 * We therefore take
793 * c = (a - b) mod d
795 * and add it to b to obtain the constant term of div'.
796 * If this constant term is "too negative", then we add an appropriate
797 * multiple of d to make it positive.
800 * Note that the above is a only a very simple heuristic for finding an
801 * appropriate expression. We could try a bit harder by also considering
802 * sums of constraints that involve disjoint sets of variables or
803 * we could consider arbitrary linear combinations of constraints,
804 * although that could potentially be much more expensive as it involves
805 * the solution of an LP problem.
807 * In particular, if v_i is a column vector representing constraint i,
808 * w represents div and e_i is the i-th unit vector, then we are looking
809 * for a solution of the constraints
811 * \sum_i lambda_i v_i = w + \sum_i alpha_i d e_i
813 * with \lambda_i >= 0 and alpha_i of unrestricted sign.
814 * If we are not just interested in a non-negative expression, but
815 * also in one with a minimal range, then we don't just want
816 * c = \sum_i lambda_i v_i to be non-negative over the domain,
817 * but also beta - c = \sum_i mu_i v_i, where beta is a scalar
818 * that we want to minimize and we now also have to take into account
819 * the constant terms of the constraints.
820 * Alternatively, we could first compute the dual of the domain
821 * and plug in the constraints on the coefficients.
823 static int try_extract_mod(struct isl_extract_mod_data *data)
825 isl_basic_set *hull;
826 isl_val *v1, *v2;
827 isl_stat r;
828 int n;
830 if (!data->build)
831 goto error;
833 n = isl_aff_dim(data->div, isl_dim_div);
835 if (isl_aff_involves_dims(data->div, isl_dim_div, 0, n))
836 return extract_nonneg_mod(data);
838 hull = isl_set_simple_hull(isl_set_copy(data->build->domain));
839 hull = isl_basic_set_remove_divs(hull);
840 data->sign = 0;
841 data->nonneg = NULL;
842 r = isl_basic_set_foreach_constraint(hull, &check_parallel_or_opposite,
843 data);
844 isl_basic_set_free(hull);
846 if (!data->sign || r < 0) {
847 isl_aff_free(data->nonneg);
848 if (r < 0)
849 goto error;
850 return extract_nonneg_mod(data);
853 v1 = isl_aff_get_constant_val(data->div);
854 v2 = isl_aff_get_constant_val(data->nonneg);
855 if (data->sign < 0) {
856 v1 = isl_val_neg(v1);
857 v1 = isl_val_add(v1, isl_val_copy(data->d));
858 v1 = isl_val_sub_ui(v1, 1);
860 v1 = isl_val_sub(v1, isl_val_copy(v2));
861 v1 = isl_val_mod(v1, isl_val_copy(data->d));
862 v1 = isl_val_add(v1, v2);
863 v2 = isl_val_div(isl_val_copy(v1), isl_val_copy(data->d));
864 v2 = isl_val_ceil(v2);
865 if (isl_val_is_neg(v2)) {
866 v2 = isl_val_mul(v2, isl_val_copy(data->d));
867 v1 = isl_val_sub(v1, isl_val_copy(v2));
869 data->nonneg = isl_aff_set_constant_val(data->nonneg, v1);
870 isl_val_free(v2);
872 if (data->sign < 0) {
873 data->div = oppose_div_arg(data->div, isl_val_copy(data->d));
874 data->v = isl_val_neg(data->v);
877 return extract_term_and_mod(data,
878 isl_aff_copy(data->div), data->nonneg);
879 error:
880 data->aff = isl_aff_free(data->aff);
881 return -1;
884 /* Check if "data->aff" involves any (implicit) modulo computations based
885 * on div "data->i".
886 * If so, remove them from aff and add expressions corresponding
887 * to those modulo computations to data->pos and/or data->neg.
889 * "aff" is assumed to be an integer affine expression.
891 * In particular, check if (v * div_j) is of the form
893 * f * m * floor(a / m)
895 * and, if so, rewrite it as
897 * f * (a - (a mod m)) = f * a - f * (a mod m)
899 * and extract out -f * (a mod m).
900 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
901 * If f < 0, we add ((-f) * (a mod m)) to *pos.
903 * Note that in order to represent "a mod m" as
905 * (isl_ast_op_pdiv_r, a, m)
907 * we need to make sure that a is non-negative.
908 * If not, we check if "-a + m - 1" is non-negative.
909 * If so, we can rewrite
911 * floor(a/m) = -ceil(-a/m) = -floor((-a + m - 1)/m)
913 * and still extract a modulo.
915 static int extract_modulo(struct isl_extract_mod_data *data)
917 data->div = isl_aff_get_div(data->aff, data->i);
918 data->d = isl_aff_get_denominator_val(data->div);
919 if (isl_val_is_divisible_by(data->v, data->d)) {
920 data->div = isl_aff_scale_val(data->div, isl_val_copy(data->d));
921 if (try_extract_mod(data) < 0)
922 data->aff = isl_aff_free(data->aff);
924 isl_aff_free(data->div);
925 isl_val_free(data->d);
926 return 0;
929 /* Check if "aff" involves any (implicit) modulo computations.
930 * If so, remove them from aff and add expressions corresponding
931 * to those modulo computations to *pos and/or *neg.
932 * We only do this if the option ast_build_prefer_pdiv is set.
934 * "aff" is assumed to be an integer affine expression.
936 * A modulo expression is of the form
938 * a mod m = a - m * floor(a / m)
940 * To detect them in aff, we look for terms of the form
942 * f * m * floor(a / m)
944 * rewrite them as
946 * f * (a - (a mod m)) = f * a - f * (a mod m)
948 * and extract out -f * (a mod m).
949 * In particular, if f > 0, we add (f * (a mod m)) to *neg.
950 * If f < 0, we add ((-f) * (a mod m)) to *pos.
952 static __isl_give isl_aff *extract_modulos(__isl_take isl_aff *aff,
953 __isl_keep isl_ast_expr **pos, __isl_keep isl_ast_expr **neg,
954 __isl_keep isl_ast_build *build)
956 struct isl_extract_mod_data data = { build, aff, *pos, *neg };
957 isl_ctx *ctx;
958 int n;
960 if (!aff)
961 return NULL;
963 ctx = isl_aff_get_ctx(aff);
964 if (!isl_options_get_ast_build_prefer_pdiv(ctx))
965 return aff;
967 n = isl_aff_dim(data.aff, isl_dim_div);
968 for (data.i = 0; data.i < n; ++data.i) {
969 data.v = isl_aff_get_coefficient_val(data.aff,
970 isl_dim_div, data.i);
971 if (!data.v)
972 return isl_aff_free(aff);
973 if (isl_val_is_zero(data.v) ||
974 isl_val_is_one(data.v) || isl_val_is_negone(data.v)) {
975 isl_val_free(data.v);
976 continue;
978 if (extract_modulo(&data) < 0)
979 data.aff = isl_aff_free(data.aff);
980 isl_val_free(data.v);
981 if (!data.aff)
982 break;
985 if (data.add)
986 data.aff = isl_aff_add(data.aff, data.add);
988 *pos = data.pos;
989 *neg = data.neg;
990 return data.aff;
993 /* Check if aff involves any non-integer coefficients.
994 * If so, split aff into
996 * aff = aff1 + (aff2 / d)
998 * with both aff1 and aff2 having only integer coefficients.
999 * Return aff1 and add (aff2 / d) to *expr.
1001 static __isl_give isl_aff *extract_rational(__isl_take isl_aff *aff,
1002 __isl_keep isl_ast_expr **expr, __isl_keep isl_ast_build *build)
1004 int i, j, n;
1005 isl_aff *rat = NULL;
1006 isl_local_space *ls = NULL;
1007 isl_ast_expr *rat_expr;
1008 isl_val *v, *d;
1009 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1010 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1012 if (!aff)
1013 return NULL;
1014 d = isl_aff_get_denominator_val(aff);
1015 if (!d)
1016 goto error;
1017 if (isl_val_is_one(d)) {
1018 isl_val_free(d);
1019 return aff;
1022 aff = isl_aff_scale_val(aff, isl_val_copy(d));
1024 ls = isl_aff_get_domain_local_space(aff);
1025 rat = isl_aff_zero_on_domain(isl_local_space_copy(ls));
1027 for (i = 0; i < 3; ++i) {
1028 n = isl_aff_dim(aff, t[i]);
1029 for (j = 0; j < n; ++j) {
1030 isl_aff *rat_j;
1032 v = isl_aff_get_coefficient_val(aff, t[i], j);
1033 if (!v)
1034 goto error;
1035 if (isl_val_is_divisible_by(v, d)) {
1036 isl_val_free(v);
1037 continue;
1039 rat_j = isl_aff_var_on_domain(isl_local_space_copy(ls),
1040 l[i], j);
1041 rat_j = isl_aff_scale_val(rat_j, v);
1042 rat = isl_aff_add(rat, rat_j);
1046 v = isl_aff_get_constant_val(aff);
1047 if (isl_val_is_divisible_by(v, d)) {
1048 isl_val_free(v);
1049 } else {
1050 isl_aff *rat_0;
1052 rat_0 = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
1053 rat = isl_aff_add(rat, rat_0);
1056 isl_local_space_free(ls);
1058 aff = isl_aff_sub(aff, isl_aff_copy(rat));
1059 aff = isl_aff_scale_down_val(aff, isl_val_copy(d));
1061 rat_expr = isl_ast_expr_from_aff(rat, build);
1062 rat_expr = isl_ast_expr_div(rat_expr, isl_ast_expr_from_val(d));
1063 *expr = ast_expr_add(*expr, rat_expr);
1065 return aff;
1066 error:
1067 isl_aff_free(rat);
1068 isl_local_space_free(ls);
1069 isl_aff_free(aff);
1070 isl_val_free(d);
1071 return NULL;
1074 /* Construct an isl_ast_expr that evaluates the affine expression "aff",
1075 * The result is simplified in terms of build->domain.
1077 * We first extract hidden modulo computations from the affine expression
1078 * and then add terms for each variable with a non-zero coefficient.
1079 * Finally, if the affine expression has a non-trivial denominator,
1080 * we divide the resulting isl_ast_expr by this denominator.
1082 __isl_give isl_ast_expr *isl_ast_expr_from_aff(__isl_take isl_aff *aff,
1083 __isl_keep isl_ast_build *build)
1085 int i, j;
1086 int n;
1087 isl_val *v;
1088 isl_ctx *ctx = isl_aff_get_ctx(aff);
1089 isl_ast_expr *expr, *expr_neg;
1090 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1091 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1092 isl_local_space *ls;
1093 struct isl_ast_add_term_data data;
1095 if (!aff)
1096 return NULL;
1098 expr = isl_ast_expr_alloc_int_si(ctx, 0);
1099 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1101 aff = extract_rational(aff, &expr, build);
1103 aff = extract_modulos(aff, &expr, &expr_neg, build);
1104 expr = ast_expr_sub(expr, expr_neg);
1106 ls = isl_aff_get_domain_local_space(aff);
1108 data.build = build;
1109 data.cst = isl_aff_get_constant_val(aff);
1110 for (i = 0; i < 3; ++i) {
1111 n = isl_aff_dim(aff, t[i]);
1112 for (j = 0; j < n; ++j) {
1113 v = isl_aff_get_coefficient_val(aff, t[i], j);
1114 if (!v)
1115 expr = isl_ast_expr_free(expr);
1116 if (isl_val_is_zero(v)) {
1117 isl_val_free(v);
1118 continue;
1120 expr = isl_ast_expr_add_term(expr,
1121 ls, l[i], j, v, &data);
1125 expr = isl_ast_expr_add_int(expr, data.cst);
1127 isl_local_space_free(ls);
1128 isl_aff_free(aff);
1129 return expr;
1132 /* Add terms to "expr" for each variable in "aff" with a coefficient
1133 * with sign equal to "sign".
1134 * The result is simplified in terms of data->build->domain.
1136 static __isl_give isl_ast_expr *add_signed_terms(__isl_take isl_ast_expr *expr,
1137 __isl_keep isl_aff *aff, int sign, struct isl_ast_add_term_data *data)
1139 int i, j;
1140 isl_val *v;
1141 enum isl_dim_type t[] = { isl_dim_param, isl_dim_in, isl_dim_div };
1142 enum isl_dim_type l[] = { isl_dim_param, isl_dim_set, isl_dim_div };
1143 isl_local_space *ls;
1145 ls = isl_aff_get_domain_local_space(aff);
1147 for (i = 0; i < 3; ++i) {
1148 int n = isl_aff_dim(aff, t[i]);
1149 for (j = 0; j < n; ++j) {
1150 v = isl_aff_get_coefficient_val(aff, t[i], j);
1151 if (sign * isl_val_sgn(v) <= 0) {
1152 isl_val_free(v);
1153 continue;
1155 v = isl_val_abs(v);
1156 expr = isl_ast_expr_add_term(expr,
1157 ls, l[i], j, v, data);
1161 isl_local_space_free(ls);
1163 return expr;
1166 /* Should the constant term "v" be considered positive?
1168 * A positive constant will be added to "pos" by the caller,
1169 * while a negative constant will be added to "neg".
1170 * If either "pos" or "neg" is exactly zero, then we prefer
1171 * to add the constant "v" to that side, irrespective of the sign of "v".
1172 * This results in slightly shorter expressions and may reduce the risk
1173 * of overflows.
1175 static int constant_is_considered_positive(__isl_keep isl_val *v,
1176 __isl_keep isl_ast_expr *pos, __isl_keep isl_ast_expr *neg)
1178 if (ast_expr_is_zero(pos))
1179 return 1;
1180 if (ast_expr_is_zero(neg))
1181 return 0;
1182 return isl_val_is_pos(v);
1185 /* Check if the equality
1187 * aff = 0
1189 * represents a stride constraint on the integer division "pos".
1191 * In particular, if the integer division "pos" is equal to
1193 * floor(e/d)
1195 * then check if aff is equal to
1197 * e - d floor(e/d)
1199 * or its opposite.
1201 * If so, the equality is exactly
1203 * e mod d = 0
1205 * Note that in principle we could also accept
1207 * e - d floor(e'/d)
1209 * where e and e' differ by a constant.
1211 static int is_stride_constraint(__isl_keep isl_aff *aff, int pos)
1213 isl_aff *div;
1214 isl_val *c, *d;
1215 int eq;
1217 div = isl_aff_get_div(aff, pos);
1218 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1219 d = isl_aff_get_denominator_val(div);
1220 eq = isl_val_abs_eq(c, d);
1221 if (eq >= 0 && eq) {
1222 aff = isl_aff_copy(aff);
1223 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1224 div = isl_aff_scale_val(div, d);
1225 if (isl_val_is_pos(c))
1226 div = isl_aff_neg(div);
1227 eq = isl_aff_plain_is_equal(div, aff);
1228 isl_aff_free(aff);
1229 } else
1230 isl_val_free(d);
1231 isl_val_free(c);
1232 isl_aff_free(div);
1234 return eq;
1237 /* Are all coefficients of "aff" (zero or) negative?
1239 static int all_negative_coefficients(__isl_keep isl_aff *aff)
1241 int i, n;
1243 if (!aff)
1244 return 0;
1246 n = isl_aff_dim(aff, isl_dim_param);
1247 for (i = 0; i < n; ++i)
1248 if (isl_aff_coefficient_sgn(aff, isl_dim_param, i) > 0)
1249 return 0;
1251 n = isl_aff_dim(aff, isl_dim_in);
1252 for (i = 0; i < n; ++i)
1253 if (isl_aff_coefficient_sgn(aff, isl_dim_in, i) > 0)
1254 return 0;
1256 return 1;
1259 /* Give an equality of the form
1261 * aff = e - d floor(e/d) = 0
1263 * or
1265 * aff = -e + d floor(e/d) = 0
1267 * with the integer division "pos" equal to floor(e/d),
1268 * construct the AST expression
1270 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1272 * If e only has negative coefficients, then construct
1274 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(-e), expr(d)), expr(0))
1276 * instead.
1278 static __isl_give isl_ast_expr *extract_stride_constraint(
1279 __isl_take isl_aff *aff, int pos, __isl_keep isl_ast_build *build)
1281 isl_ctx *ctx;
1282 isl_val *c;
1283 isl_ast_expr *expr, *cst;
1285 if (!aff)
1286 return NULL;
1288 ctx = isl_aff_get_ctx(aff);
1290 c = isl_aff_get_coefficient_val(aff, isl_dim_div, pos);
1291 aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 0);
1293 if (all_negative_coefficients(aff))
1294 aff = isl_aff_neg(aff);
1296 cst = isl_ast_expr_from_val(isl_val_abs(c));
1297 expr = isl_ast_expr_from_aff(aff, build);
1299 expr = isl_ast_expr_alloc_binary(isl_ast_op_zdiv_r, expr, cst);
1300 cst = isl_ast_expr_alloc_int_si(ctx, 0);
1301 expr = isl_ast_expr_alloc_binary(isl_ast_op_eq, expr, cst);
1303 return expr;
1306 /* Construct an isl_ast_expr that evaluates the condition "constraint",
1307 * The result is simplified in terms of build->domain.
1309 * We first check if the constraint is an equality of the form
1311 * e - d floor(e/d) = 0
1313 * i.e.,
1315 * e mod d = 0
1317 * If so, we convert it to
1319 * (isl_ast_op_eq, (isl_ast_op_zdiv_r, expr(e), expr(d)), expr(0))
1321 * Otherwise, let the constraint by either "a >= 0" or "a == 0".
1322 * We first extract hidden modulo computations from "a"
1323 * and then collect all the terms with a positive coefficient in cons_pos
1324 * and the terms with a negative coefficient in cons_neg.
1326 * The result is then of the form
1328 * (isl_ast_op_ge, expr(pos), expr(-neg)))
1330 * or
1332 * (isl_ast_op_eq, expr(pos), expr(-neg)))
1334 * However, if the first expression is an integer constant (and the second
1335 * is not), then we swap the two expressions. This ensures that we construct,
1336 * e.g., "i <= 5" rather than "5 >= i".
1338 * Furthermore, is there are no terms with positive coefficients (or no terms
1339 * with negative coefficients), then the constant term is added to "pos"
1340 * (or "neg"), ignoring the sign of the constant term.
1342 static __isl_give isl_ast_expr *isl_ast_expr_from_constraint(
1343 __isl_take isl_constraint *constraint, __isl_keep isl_ast_build *build)
1345 int i, n;
1346 isl_ctx *ctx;
1347 isl_ast_expr *expr_pos;
1348 isl_ast_expr *expr_neg;
1349 isl_ast_expr *expr;
1350 isl_aff *aff;
1351 int eq;
1352 enum isl_ast_op_type type;
1353 struct isl_ast_add_term_data data;
1355 if (!constraint)
1356 return NULL;
1358 aff = isl_constraint_get_aff(constraint);
1359 eq = isl_constraint_is_equality(constraint);
1360 isl_constraint_free(constraint);
1362 n = isl_aff_dim(aff, isl_dim_div);
1363 if (eq && n > 0)
1364 for (i = 0; i < n; ++i) {
1365 int is_stride;
1366 is_stride = is_stride_constraint(aff, i);
1367 if (is_stride < 0)
1368 goto error;
1369 if (is_stride)
1370 return extract_stride_constraint(aff, i, build);
1373 ctx = isl_aff_get_ctx(aff);
1374 expr_pos = isl_ast_expr_alloc_int_si(ctx, 0);
1375 expr_neg = isl_ast_expr_alloc_int_si(ctx, 0);
1377 aff = extract_modulos(aff, &expr_pos, &expr_neg, build);
1379 data.build = build;
1380 data.cst = isl_aff_get_constant_val(aff);
1381 expr_pos = add_signed_terms(expr_pos, aff, 1, &data);
1382 data.cst = isl_val_neg(data.cst);
1383 expr_neg = add_signed_terms(expr_neg, aff, -1, &data);
1384 data.cst = isl_val_neg(data.cst);
1386 if (constant_is_considered_positive(data.cst, expr_pos, expr_neg)) {
1387 expr_pos = isl_ast_expr_add_int(expr_pos, data.cst);
1388 } else {
1389 data.cst = isl_val_neg(data.cst);
1390 expr_neg = isl_ast_expr_add_int(expr_neg, data.cst);
1393 if (isl_ast_expr_get_type(expr_pos) == isl_ast_expr_int &&
1394 isl_ast_expr_get_type(expr_neg) != isl_ast_expr_int) {
1395 type = eq ? isl_ast_op_eq : isl_ast_op_le;
1396 expr = isl_ast_expr_alloc_binary(type, expr_neg, expr_pos);
1397 } else {
1398 type = eq ? isl_ast_op_eq : isl_ast_op_ge;
1399 expr = isl_ast_expr_alloc_binary(type, expr_pos, expr_neg);
1402 isl_aff_free(aff);
1403 return expr;
1404 error:
1405 isl_aff_free(aff);
1406 return NULL;
1409 /* Wrapper around isl_constraint_cmp_last_non_zero for use
1410 * as a callback to isl_constraint_list_sort.
1411 * If isl_constraint_cmp_last_non_zero cannot tell the constraints
1412 * apart, then use isl_constraint_plain_cmp instead.
1414 static int cmp_constraint(__isl_keep isl_constraint *a,
1415 __isl_keep isl_constraint *b, void *user)
1417 int cmp;
1419 cmp = isl_constraint_cmp_last_non_zero(a, b);
1420 if (cmp != 0)
1421 return cmp;
1422 return isl_constraint_plain_cmp(a, b);
1425 /* Construct an isl_ast_expr that evaluates the conditions defining "bset".
1426 * The result is simplified in terms of build->domain.
1428 * If "bset" is not bounded by any constraint, then we construct
1429 * the expression "1", i.e., "true".
1431 * Otherwise, we sort the constraints, putting constraints that involve
1432 * integer divisions after those that do not, and construct an "and"
1433 * of the ast expressions of the individual constraints.
1435 * Each constraint is added to the generated constraints of the build
1436 * after it has been converted to an AST expression so that it can be used
1437 * to simplify the following constraints. This may change the truth value
1438 * of subsequent constraints that do not satisfy the earlier constraints,
1439 * but this does not affect the outcome of the conjunction as it is
1440 * only true if all the conjuncts are true (no matter in what order
1441 * they are evaluated). In particular, the constraints that do not
1442 * involve integer divisions may serve to simplify some constraints
1443 * that do involve integer divisions.
1445 __isl_give isl_ast_expr *isl_ast_build_expr_from_basic_set(
1446 __isl_keep isl_ast_build *build, __isl_take isl_basic_set *bset)
1448 int i, n;
1449 isl_constraint *c;
1450 isl_constraint_list *list;
1451 isl_ast_expr *res;
1452 isl_set *set;
1454 list = isl_basic_set_get_constraint_list(bset);
1455 isl_basic_set_free(bset);
1456 list = isl_constraint_list_sort(list, &cmp_constraint, NULL);
1457 if (!list)
1458 return NULL;
1459 n = isl_constraint_list_n_constraint(list);
1460 if (n == 0) {
1461 isl_ctx *ctx = isl_constraint_list_get_ctx(list);
1462 isl_constraint_list_free(list);
1463 return isl_ast_expr_alloc_int_si(ctx, 1);
1466 build = isl_ast_build_copy(build);
1468 c = isl_constraint_list_get_constraint(list, 0);
1469 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1470 set = isl_set_from_basic_set(bset);
1471 res = isl_ast_expr_from_constraint(c, build);
1472 build = isl_ast_build_restrict_generated(build, set);
1474 for (i = 1; i < n; ++i) {
1475 isl_ast_expr *expr;
1477 c = isl_constraint_list_get_constraint(list, i);
1478 bset = isl_basic_set_from_constraint(isl_constraint_copy(c));
1479 set = isl_set_from_basic_set(bset);
1480 expr = isl_ast_expr_from_constraint(c, build);
1481 build = isl_ast_build_restrict_generated(build, set);
1482 res = isl_ast_expr_and(res, expr);
1485 isl_constraint_list_free(list);
1486 isl_ast_build_free(build);
1487 return res;
1490 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1491 * The result is simplified in terms of build->domain.
1493 * If "set" is an (obviously) empty set, then return the expression "0".
1495 * If there are multiple disjuncts in the description of the set,
1496 * then subsequent disjuncts are simplified in a context where
1497 * the previous disjuncts have been removed from build->domain.
1498 * In particular, constraints that ensure that there is no overlap
1499 * with these previous disjuncts, can be removed.
1500 * This is mostly useful for disjuncts that are only defined by
1501 * a single constraint (relative to the build domain) as the opposite
1502 * of that single constraint can then be removed from the other disjuncts.
1503 * In order not to increase the number of disjuncts in the build domain
1504 * after subtracting the previous disjuncts of "set", the simple hull
1505 * is computed after taking the difference with each of these disjuncts.
1506 * This means that constraints that prevent overlap with a union
1507 * of multiple previous disjuncts are not removed.
1509 * "set" lives in the internal schedule space.
1511 __isl_give isl_ast_expr *isl_ast_build_expr_from_set_internal(
1512 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1514 int i, n;
1515 isl_basic_set *bset;
1516 isl_basic_set_list *list;
1517 isl_set *domain;
1518 isl_ast_expr *res;
1520 list = isl_set_get_basic_set_list(set);
1521 isl_set_free(set);
1523 if (!list)
1524 return NULL;
1525 n = isl_basic_set_list_n_basic_set(list);
1526 if (n == 0) {
1527 isl_ctx *ctx = isl_ast_build_get_ctx(build);
1528 isl_basic_set_list_free(list);
1529 return isl_ast_expr_from_val(isl_val_zero(ctx));
1532 domain = isl_ast_build_get_domain(build);
1534 bset = isl_basic_set_list_get_basic_set(list, 0);
1535 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1536 res = isl_ast_build_expr_from_basic_set(build, bset);
1538 for (i = 1; i < n; ++i) {
1539 isl_ast_expr *expr;
1540 isl_set *rest;
1542 rest = isl_set_subtract(isl_set_copy(domain), set);
1543 rest = isl_set_from_basic_set(isl_set_simple_hull(rest));
1544 domain = isl_set_intersect(domain, rest);
1545 bset = isl_basic_set_list_get_basic_set(list, i);
1546 set = isl_set_from_basic_set(isl_basic_set_copy(bset));
1547 bset = isl_basic_set_gist(bset,
1548 isl_set_simple_hull(isl_set_copy(domain)));
1549 expr = isl_ast_build_expr_from_basic_set(build, bset);
1550 res = isl_ast_expr_or(res, expr);
1553 isl_set_free(domain);
1554 isl_set_free(set);
1555 isl_basic_set_list_free(list);
1556 return res;
1559 /* Construct an isl_ast_expr that evaluates the conditions defining "set".
1560 * The result is simplified in terms of build->domain.
1562 * If "set" is an (obviously) empty set, then return the expression "0".
1564 * "set" lives in the external schedule space.
1566 * The internal AST expression generation assumes that there are
1567 * no unknown divs, so make sure an explicit representation is available.
1568 * Since the set comes from the outside, it may have constraints that
1569 * are redundant with respect to the build domain. Remove them first.
1571 __isl_give isl_ast_expr *isl_ast_build_expr_from_set(
1572 __isl_keep isl_ast_build *build, __isl_take isl_set *set)
1574 if (isl_ast_build_need_schedule_map(build)) {
1575 isl_multi_aff *ma;
1576 ma = isl_ast_build_get_schedule_map_multi_aff(build);
1577 set = isl_set_preimage_multi_aff(set, ma);
1580 set = isl_set_compute_divs(set);
1581 set = isl_ast_build_compute_gist(build, set);
1582 return isl_ast_build_expr_from_set_internal(build, set);
1585 /* State of data about previous pieces in
1586 * isl_ast_build_expr_from_pw_aff_internal.
1588 * isl_state_none: no data about previous pieces
1589 * isl_state_single: data about a single previous piece
1590 * isl_state_min: data represents minimum of several pieces
1591 * isl_state_max: data represents maximum of several pieces
1593 enum isl_from_pw_aff_state {
1594 isl_state_none,
1595 isl_state_single,
1596 isl_state_min,
1597 isl_state_max
1600 /* Internal date structure representing a single piece in the input of
1601 * isl_ast_build_expr_from_pw_aff_internal.
1603 * If "state" is isl_state_none, then "set_list" and "aff_list" are not used.
1604 * If "state" is isl_state_single, then "set_list" and "aff_list" contain the
1605 * single previous subpiece.
1606 * If "state" is isl_state_min, then "set_list" and "aff_list" contain
1607 * a sequence of several previous subpieces that are equal to the minimum
1608 * of the entries in "aff_list" over the union of "set_list"
1609 * If "state" is isl_state_max, then "set_list" and "aff_list" contain
1610 * a sequence of several previous subpieces that are equal to the maximum
1611 * of the entries in "aff_list" over the union of "set_list"
1613 * During the construction of the pieces, "set" is NULL.
1614 * After the construction, "set" is set to the union of the elements
1615 * in "set_list", at which point "set_list" is set to NULL.
1617 struct isl_from_pw_aff_piece {
1618 enum isl_from_pw_aff_state state;
1619 isl_set *set;
1620 isl_set_list *set_list;
1621 isl_aff_list *aff_list;
1624 /* Internal data structure for isl_ast_build_expr_from_pw_aff_internal.
1626 * "build" specifies the domain against which the result is simplified.
1627 * "dom" is the domain of the entire isl_pw_aff.
1629 * "n" is the number of pieces constructed already.
1630 * In particular, during the construction of the pieces, "n" points to
1631 * the piece that is being constructed. After the construction of the
1632 * pieces, "n" is set to the total number of pieces.
1633 * "max" is the total number of allocated entries.
1634 * "p" contains the individual pieces.
1636 struct isl_from_pw_aff_data {
1637 isl_ast_build *build;
1638 isl_set *dom;
1640 int n;
1641 int max;
1642 struct isl_from_pw_aff_piece *p;
1645 /* Initialize "data" based on "build" and "pa".
1647 static isl_stat isl_from_pw_aff_data_init(struct isl_from_pw_aff_data *data,
1648 __isl_keep isl_ast_build *build, __isl_keep isl_pw_aff *pa)
1650 int n;
1651 isl_ctx *ctx;
1653 ctx = isl_pw_aff_get_ctx(pa);
1654 n = isl_pw_aff_n_piece(pa);
1655 if (n == 0)
1656 isl_die(ctx, isl_error_invalid,
1657 "cannot handle void expression", return isl_stat_error);
1658 data->max = n;
1659 data->p = isl_calloc_array(ctx, struct isl_from_pw_aff_piece, n);
1660 if (!data->p)
1661 return isl_stat_error;
1662 data->build = build;
1663 data->dom = isl_pw_aff_domain(isl_pw_aff_copy(pa));
1664 data->n = 0;
1666 return isl_stat_ok;
1669 /* Free all memory allocated for "data".
1671 static void isl_from_pw_aff_data_clear(struct isl_from_pw_aff_data *data)
1673 int i;
1675 isl_set_free(data->dom);
1676 if (!data->p)
1677 return;
1679 for (i = 0; i < data->max; ++i) {
1680 isl_set_free(data->p[i].set);
1681 isl_set_list_free(data->p[i].set_list);
1682 isl_aff_list_free(data->p[i].aff_list);
1684 free(data->p);
1687 /* Initialize the current entry of "data" to an unused piece.
1689 static void set_none(struct isl_from_pw_aff_data *data)
1691 data->p[data->n].state = isl_state_none;
1692 data->p[data->n].set_list = NULL;
1693 data->p[data->n].aff_list = NULL;
1696 /* Store "set" and "aff" in the current entry of "data" as a single subpiece.
1698 static void set_single(struct isl_from_pw_aff_data *data,
1699 __isl_take isl_set *set, __isl_take isl_aff *aff)
1701 data->p[data->n].state = isl_state_single;
1702 data->p[data->n].set_list = isl_set_list_from_set(set);
1703 data->p[data->n].aff_list = isl_aff_list_from_aff(aff);
1706 /* Extend the current entry of "data" with "set" and "aff"
1707 * as a minimum expression.
1709 static isl_stat extend_min(struct isl_from_pw_aff_data *data,
1710 __isl_take isl_set *set, __isl_take isl_aff *aff)
1712 int n = data->n;
1713 data->p[n].state = isl_state_min;
1714 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1715 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1717 if (!data->p[n].set_list || !data->p[n].aff_list)
1718 return isl_stat_error;
1719 return isl_stat_ok;
1722 /* Extend the current entry of "data" with "set" and "aff"
1723 * as a maximum expression.
1725 static isl_stat extend_max(struct isl_from_pw_aff_data *data,
1726 __isl_take isl_set *set, __isl_take isl_aff *aff)
1728 int n = data->n;
1729 data->p[n].state = isl_state_max;
1730 data->p[n].set_list = isl_set_list_add(data->p[n].set_list, set);
1731 data->p[n].aff_list = isl_aff_list_add(data->p[n].aff_list, aff);
1733 if (!data->p[n].set_list || !data->p[n].aff_list)
1734 return isl_stat_error;
1735 return isl_stat_ok;
1738 /* Extend the domain of the current entry of "data", which is assumed
1739 * to contain a single subpiece, with "set". If "replace" is set,
1740 * then also replace the affine function by "aff". Otherwise,
1741 * simply free "aff".
1743 static isl_stat extend_domain(struct isl_from_pw_aff_data *data,
1744 __isl_take isl_set *set, __isl_take isl_aff *aff, int replace)
1746 int n = data->n;
1747 isl_set *set_n;
1749 set_n = isl_set_list_get_set(data->p[n].set_list, 0);
1750 set_n = isl_set_union(set_n, set);
1751 data->p[n].set_list =
1752 isl_set_list_set_set(data->p[n].set_list, 0, set_n);
1754 if (replace)
1755 data->p[n].aff_list =
1756 isl_aff_list_set_aff(data->p[n].aff_list, 0, aff);
1757 else
1758 isl_aff_free(aff);
1760 if (!data->p[n].set_list || !data->p[n].aff_list)
1761 return isl_stat_error;
1762 return isl_stat_ok;
1765 /* Construct an isl_ast_expr from "list" within "build".
1766 * If "state" is isl_state_single, then "list" contains a single entry and
1767 * an isl_ast_expr is constructed for that entry.
1768 * Otherwise a min or max expression is constructed from "list"
1769 * depending on "state".
1771 static __isl_give isl_ast_expr *ast_expr_from_aff_list(
1772 __isl_take isl_aff_list *list, enum isl_from_pw_aff_state state,
1773 __isl_keep isl_ast_build *build)
1775 int i, n;
1776 isl_aff *aff;
1777 isl_ast_expr *expr;
1778 enum isl_ast_op_type op_type;
1780 if (state == isl_state_single) {
1781 aff = isl_aff_list_get_aff(list, 0);
1782 isl_aff_list_free(list);
1783 return isl_ast_expr_from_aff(aff, build);
1785 n = isl_aff_list_n_aff(list);
1786 op_type = state == isl_state_min ? isl_ast_op_min : isl_ast_op_max;
1787 expr = isl_ast_expr_alloc_op(isl_ast_build_get_ctx(build), op_type, n);
1788 if (!expr)
1789 goto error;
1791 for (i = 0; i < n; ++i) {
1792 isl_ast_expr *expr_i;
1794 aff = isl_aff_list_get_aff(list, i);
1795 expr_i = isl_ast_expr_from_aff(aff, build);
1796 if (!expr_i)
1797 goto error;
1798 expr->u.op.args[i] = expr_i;
1801 isl_aff_list_free(list);
1802 return expr;
1803 error:
1804 isl_aff_list_free(list);
1805 isl_ast_expr_free(expr);
1806 return NULL;
1809 /* Extend the expression in "next" to take into account
1810 * the piece at position "pos" in "data", allowing for a further extension
1811 * for the next piece(s).
1812 * In particular, "next" is set to a select operation that selects
1813 * an isl_ast_expr corresponding to data->aff_list on data->set and
1814 * to an expression that will be filled in by later calls.
1815 * Return a pointer to this location.
1816 * Afterwards, the state of "data" is set to isl_state_none.
1818 * The constraints of data->set are added to the generated
1819 * constraints of the build such that they can be exploited to simplify
1820 * the AST expression constructed from data->aff_list.
1822 static isl_ast_expr **add_intermediate_piece(struct isl_from_pw_aff_data *data,
1823 int pos, isl_ast_expr **next)
1825 isl_ctx *ctx;
1826 isl_ast_build *build;
1827 isl_ast_expr *ternary, *arg;
1828 isl_set *set, *gist;
1830 set = data->p[pos].set;
1831 data->p[pos].set = NULL;
1832 ctx = isl_ast_build_get_ctx(data->build);
1833 ternary = isl_ast_expr_alloc_op(ctx, isl_ast_op_select, 3);
1834 gist = isl_set_gist(isl_set_copy(set), isl_set_copy(data->dom));
1835 arg = isl_ast_build_expr_from_set_internal(data->build, gist);
1836 ternary = isl_ast_expr_set_op_arg(ternary, 0, arg);
1837 build = isl_ast_build_copy(data->build);
1838 build = isl_ast_build_restrict_generated(build, set);
1839 arg = ast_expr_from_aff_list(data->p[pos].aff_list,
1840 data->p[pos].state, build);
1841 data->p[pos].aff_list = NULL;
1842 isl_ast_build_free(build);
1843 ternary = isl_ast_expr_set_op_arg(ternary, 1, arg);
1844 data->p[pos].state = isl_state_none;
1845 if (!ternary)
1846 return NULL;
1848 *next = ternary;
1849 return &ternary->u.op.args[2];
1852 /* Extend the expression in "next" to take into account
1853 * the final piece, located at position "pos" in "data".
1854 * In particular, "next" is set to evaluate data->aff_list
1855 * and the domain is ignored.
1856 * Return isl_stat_ok on success and isl_stat_error on failure.
1858 * The constraints of data->set are however added to the generated
1859 * constraints of the build such that they can be exploited to simplify
1860 * the AST expression constructed from data->aff_list.
1862 static isl_stat add_last_piece(struct isl_from_pw_aff_data *data,
1863 int pos, isl_ast_expr **next)
1865 isl_ast_build *build;
1867 if (data->p[pos].state == isl_state_none)
1868 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1869 "cannot handle void expression", return isl_stat_error);
1871 build = isl_ast_build_copy(data->build);
1872 build = isl_ast_build_restrict_generated(build, data->p[pos].set);
1873 data->p[pos].set = NULL;
1874 *next = ast_expr_from_aff_list(data->p[pos].aff_list,
1875 data->p[pos].state, build);
1876 data->p[pos].aff_list = NULL;
1877 isl_ast_build_free(build);
1878 data->p[pos].state = isl_state_none;
1879 if (!*next)
1880 return isl_stat_error;
1882 return isl_stat_ok;
1885 /* Return -1 if the piece "p1" should be sorted before "p2"
1886 * and 1 if it should be sorted after "p2".
1887 * Return 0 if they do not need to be sorted in a specific order.
1889 * Pieces are sorted according to the number of disjuncts
1890 * in their domains.
1892 static int sort_pieces_cmp(const void *p1, const void *p2, void *arg)
1894 const struct isl_from_pw_aff_piece *piece1 = p1;
1895 const struct isl_from_pw_aff_piece *piece2 = p2;
1896 int n1, n2;
1898 n1 = isl_set_n_basic_set(piece1->set);
1899 n2 = isl_set_n_basic_set(piece2->set);
1901 return n1 - n2;
1904 /* Construct an isl_ast_expr from the pieces in "data".
1905 * Return the result or NULL on failure.
1907 * When this function is called, data->n points to the current piece.
1908 * If this is an effective piece, then first increment data->n such
1909 * that data->n contains the number of pieces.
1910 * The "set_list" fields are subsequently replaced by the corresponding
1911 * "set" fields, after which the pieces are sorted according to
1912 * the number of disjuncts in these "set" fields.
1914 * Construct intermediate AST expressions for the initial pieces and
1915 * finish off with the final pieces.
1917 static isl_ast_expr *build_pieces(struct isl_from_pw_aff_data *data)
1919 int i;
1920 isl_ast_expr *res = NULL;
1921 isl_ast_expr **next = &res;
1923 if (data->p[data->n].state != isl_state_none)
1924 data->n++;
1925 if (data->n == 0)
1926 isl_die(isl_ast_build_get_ctx(data->build), isl_error_invalid,
1927 "cannot handle void expression", return NULL);
1929 for (i = 0; i < data->n; ++i) {
1930 data->p[i].set = isl_set_list_union(data->p[i].set_list);
1931 if (data->p[i].state != isl_state_single)
1932 data->p[i].set = isl_set_coalesce(data->p[i].set);
1933 data->p[i].set_list = NULL;
1936 if (isl_sort(data->p, data->n, sizeof(data->p[0]),
1937 &sort_pieces_cmp, NULL) < 0)
1938 return isl_ast_expr_free(res);
1940 for (i = 0; i + 1 < data->n; ++i) {
1941 next = add_intermediate_piece(data, i, next);
1942 if (!next)
1943 return isl_ast_expr_free(res);
1946 if (add_last_piece(data, data->n - 1, next) < 0)
1947 return isl_ast_expr_free(res);
1949 return res;
1952 /* Is the domain of the current entry of "data", which is assumed
1953 * to contain a single subpiece, a subset of "set"?
1955 static isl_bool single_is_subset(struct isl_from_pw_aff_data *data,
1956 __isl_keep isl_set *set)
1958 isl_bool subset;
1959 isl_set *set_n;
1961 set_n = isl_set_list_get_set(data->p[data->n].set_list, 0);
1962 subset = isl_set_is_subset(set_n, set);
1963 isl_set_free(set_n);
1965 return subset;
1968 /* Is "aff" a rational expression, i.e., does it have a denominator
1969 * different from one?
1971 static isl_bool aff_is_rational(__isl_keep isl_aff *aff)
1973 isl_bool rational;
1974 isl_val *den;
1976 den = isl_aff_get_denominator_val(aff);
1977 rational = isl_bool_not(isl_val_is_one(den));
1978 isl_val_free(den);
1980 return rational;
1983 /* Does "list" consist of a single rational affine expression?
1985 static isl_bool is_single_rational_aff(__isl_keep isl_aff_list *list)
1987 isl_bool rational;
1988 isl_aff *aff;
1990 if (isl_aff_list_n_aff(list) != 1)
1991 return isl_bool_false;
1992 aff = isl_aff_list_get_aff(list, 0);
1993 rational = aff_is_rational(aff);
1994 isl_aff_free(aff);
1996 return rational;
1999 /* Can the list of subpieces in the last piece of "data" be extended with
2000 * "set" and "aff" based on "test"?
2001 * In particular, is it the case for each entry (set_i, aff_i) that
2003 * test(aff, aff_i) holds on set_i, and
2004 * test(aff_i, aff) holds on set?
2006 * "test" returns the set of elements where the tests holds, meaning
2007 * that test(aff_i, aff) holds on set if set is a subset of test(aff_i, aff).
2009 * This function is used to detect min/max expressions.
2010 * If the ast_build_detect_min_max option is turned off, then
2011 * do not even try and perform any detection and return false instead.
2013 * Rational affine expressions are not considered for min/max expressions
2014 * since the combined expression will be defined on the union of the domains,
2015 * while a rational expression may only yield integer values
2016 * on its own definition domain.
2018 static isl_bool extends(struct isl_from_pw_aff_data *data,
2019 __isl_keep isl_set *set, __isl_keep isl_aff *aff,
2020 __isl_give isl_basic_set *(*test)(__isl_take isl_aff *aff1,
2021 __isl_take isl_aff *aff2))
2023 int i, n;
2024 isl_bool is_rational;
2025 isl_ctx *ctx;
2026 isl_set *dom;
2028 is_rational = aff_is_rational(aff);
2029 if (is_rational >= 0 && !is_rational)
2030 is_rational = is_single_rational_aff(data->p[data->n].aff_list);
2031 if (is_rational < 0 || is_rational)
2032 return isl_bool_not(is_rational);
2034 ctx = isl_ast_build_get_ctx(data->build);
2035 if (!isl_options_get_ast_build_detect_min_max(ctx))
2036 return isl_bool_false;
2038 dom = isl_ast_build_get_domain(data->build);
2039 set = isl_set_intersect(dom, isl_set_copy(set));
2041 n = isl_set_list_n_set(data->p[data->n].set_list);
2042 for (i = 0; i < n ; ++i) {
2043 isl_aff *aff_i;
2044 isl_set *valid;
2045 isl_set *dom, *required;
2046 isl_bool is_valid;
2048 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2049 valid = isl_set_from_basic_set(test(isl_aff_copy(aff), aff_i));
2050 required = isl_set_list_get_set(data->p[data->n].set_list, i);
2051 dom = isl_ast_build_get_domain(data->build);
2052 required = isl_set_intersect(dom, required);
2053 is_valid = isl_set_is_subset(required, valid);
2054 isl_set_free(required);
2055 isl_set_free(valid);
2056 if (is_valid < 0 || !is_valid) {
2057 isl_set_free(set);
2058 return is_valid;
2061 aff_i = isl_aff_list_get_aff(data->p[data->n].aff_list, i);
2062 valid = isl_set_from_basic_set(test(aff_i, isl_aff_copy(aff)));
2063 is_valid = isl_set_is_subset(set, valid);
2064 isl_set_free(valid);
2065 if (is_valid < 0 || !is_valid) {
2066 isl_set_free(set);
2067 return is_valid;
2071 isl_set_free(set);
2072 return isl_bool_true;
2075 /* Can the list of pieces in "data" be extended with "set" and "aff"
2076 * to form/preserve a minimum expression?
2077 * In particular, is it the case for each entry (set_i, aff_i) that
2079 * aff >= aff_i on set_i, and
2080 * aff_i >= aff on set?
2082 static isl_bool extends_min(struct isl_from_pw_aff_data *data,
2083 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
2085 return extends(data, set, aff, &isl_aff_ge_basic_set);
2088 /* Can the list of pieces in "data" be extended with "set" and "aff"
2089 * to form/preserve a maximum expression?
2090 * In particular, is it the case for each entry (set_i, aff_i) that
2092 * aff <= aff_i on set_i, and
2093 * aff_i <= aff on set?
2095 static isl_bool extends_max(struct isl_from_pw_aff_data *data,
2096 __isl_keep isl_set *set, __isl_keep isl_aff *aff)
2098 return extends(data, set, aff, &isl_aff_le_basic_set);
2101 /* This function is called during the construction of an isl_ast_expr
2102 * that evaluates an isl_pw_aff.
2103 * If the last piece of "data" contains a single subpiece and
2104 * if its affine function is equal to "aff" on a part of the domain
2105 * that includes either "set" or the domain of that single subpiece,
2106 * then extend the domain of that single subpiece with "set".
2107 * If it was the original domain of the single subpiece where
2108 * the two affine functions are equal, then also replace
2109 * the affine function of the single subpiece by "aff".
2110 * If the last piece of "data" contains either a single subpiece
2111 * or a minimum, then check if this minimum expression can be extended
2112 * with (set, aff).
2113 * If so, extend the sequence and return.
2114 * Perform the same operation for maximum expressions.
2115 * If no such extension can be performed, then move to the next piece
2116 * in "data" (if the current piece contains any data), and then store
2117 * the current subpiece in the current piece of "data" for later handling.
2119 static isl_stat ast_expr_from_pw_aff(__isl_take isl_set *set,
2120 __isl_take isl_aff *aff, void *user)
2122 struct isl_from_pw_aff_data *data = user;
2123 isl_bool test;
2124 enum isl_from_pw_aff_state state;
2126 state = data->p[data->n].state;
2127 if (state == isl_state_single) {
2128 isl_aff *aff0;
2129 isl_set *eq;
2130 isl_bool subset1, subset2 = isl_bool_false;
2131 aff0 = isl_aff_list_get_aff(data->p[data->n].aff_list, 0);
2132 eq = isl_aff_eq_set(isl_aff_copy(aff), aff0);
2133 subset1 = isl_set_is_subset(set, eq);
2134 if (subset1 >= 0 && !subset1)
2135 subset2 = single_is_subset(data, eq);
2136 isl_set_free(eq);
2137 if (subset1 < 0 || subset2 < 0)
2138 goto error;
2139 if (subset1)
2140 return extend_domain(data, set, aff, 0);
2141 if (subset2)
2142 return extend_domain(data, set, aff, 1);
2144 if (state == isl_state_single || state == isl_state_min) {
2145 test = extends_min(data, set, aff);
2146 if (test < 0)
2147 goto error;
2148 if (test)
2149 return extend_min(data, set, aff);
2151 if (state == isl_state_single || state == isl_state_max) {
2152 test = extends_max(data, set, aff);
2153 if (test < 0)
2154 goto error;
2155 if (test)
2156 return extend_max(data, set, aff);
2158 if (state != isl_state_none)
2159 data->n++;
2160 set_single(data, set, aff);
2162 return isl_stat_ok;
2163 error:
2164 isl_set_free(set);
2165 isl_aff_free(aff);
2166 return isl_stat_error;
2169 /* Construct an isl_ast_expr that evaluates "pa".
2170 * The result is simplified in terms of build->domain.
2172 * The domain of "pa" lives in the internal schedule space.
2174 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff_internal(
2175 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2177 struct isl_from_pw_aff_data data = { NULL };
2178 isl_ast_expr *res = NULL;
2180 pa = isl_ast_build_compute_gist_pw_aff(build, pa);
2181 pa = isl_pw_aff_coalesce(pa);
2182 if (!pa)
2183 return NULL;
2185 if (isl_from_pw_aff_data_init(&data, build, pa) < 0)
2186 goto error;
2187 set_none(&data);
2189 if (isl_pw_aff_foreach_piece(pa, &ast_expr_from_pw_aff, &data) >= 0)
2190 res = build_pieces(&data);
2192 isl_pw_aff_free(pa);
2193 isl_from_pw_aff_data_clear(&data);
2194 return res;
2195 error:
2196 isl_pw_aff_free(pa);
2197 isl_from_pw_aff_data_clear(&data);
2198 return NULL;
2201 /* Construct an isl_ast_expr that evaluates "pa".
2202 * The result is simplified in terms of build->domain.
2204 * The domain of "pa" lives in the external schedule space.
2206 __isl_give isl_ast_expr *isl_ast_build_expr_from_pw_aff(
2207 __isl_keep isl_ast_build *build, __isl_take isl_pw_aff *pa)
2209 isl_ast_expr *expr;
2211 if (isl_ast_build_need_schedule_map(build)) {
2212 isl_multi_aff *ma;
2213 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2214 pa = isl_pw_aff_pullback_multi_aff(pa, ma);
2216 expr = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2217 return expr;
2220 /* Set the ids of the input dimensions of "mpa" to the iterator ids
2221 * of "build".
2223 * The domain of "mpa" is assumed to live in the internal schedule domain.
2225 static __isl_give isl_multi_pw_aff *set_iterator_names(
2226 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2228 int i, n;
2230 n = isl_multi_pw_aff_dim(mpa, isl_dim_in);
2231 for (i = 0; i < n; ++i) {
2232 isl_id *id;
2234 id = isl_ast_build_get_iterator_id(build, i);
2235 mpa = isl_multi_pw_aff_set_dim_id(mpa, isl_dim_in, i, id);
2238 return mpa;
2241 /* Construct an isl_ast_expr of type "type" with as first argument "arg0" and
2242 * the remaining arguments derived from "mpa".
2243 * That is, construct a call or access expression that calls/accesses "arg0"
2244 * with arguments/indices specified by "mpa".
2246 static __isl_give isl_ast_expr *isl_ast_build_with_arguments(
2247 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2248 __isl_take isl_ast_expr *arg0, __isl_take isl_multi_pw_aff *mpa)
2250 int i, n;
2251 isl_ctx *ctx;
2252 isl_ast_expr *expr;
2254 ctx = isl_ast_build_get_ctx(build);
2256 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
2257 expr = isl_ast_expr_alloc_op(ctx, type, 1 + n);
2258 expr = isl_ast_expr_set_op_arg(expr, 0, arg0);
2259 for (i = 0; i < n; ++i) {
2260 isl_pw_aff *pa;
2261 isl_ast_expr *arg;
2263 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
2264 arg = isl_ast_build_expr_from_pw_aff_internal(build, pa);
2265 expr = isl_ast_expr_set_op_arg(expr, 1 + i, arg);
2268 isl_multi_pw_aff_free(mpa);
2269 return expr;
2272 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2273 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2274 __isl_take isl_multi_pw_aff *mpa);
2276 /* Construct an isl_ast_expr that accesses the member specified by "mpa".
2277 * The range of "mpa" is assumed to be wrapped relation.
2278 * The domain of this wrapped relation specifies the structure being
2279 * accessed, while the range of this wrapped relation spacifies the
2280 * member of the structure being accessed.
2282 * The domain of "mpa" is assumed to live in the internal schedule domain.
2284 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_member(
2285 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2287 isl_id *id;
2288 isl_multi_pw_aff *domain;
2289 isl_ast_expr *domain_expr, *expr;
2290 enum isl_ast_op_type type = isl_ast_op_access;
2292 domain = isl_multi_pw_aff_copy(mpa);
2293 domain = isl_multi_pw_aff_range_factor_domain(domain);
2294 domain_expr = isl_ast_build_from_multi_pw_aff_internal(build,
2295 type, domain);
2296 mpa = isl_multi_pw_aff_range_factor_range(mpa);
2297 if (!isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2298 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2299 "missing field name", goto error);
2300 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2301 expr = isl_ast_expr_from_id(id);
2302 expr = isl_ast_expr_alloc_binary(isl_ast_op_member, domain_expr, expr);
2303 return isl_ast_build_with_arguments(build, type, expr, mpa);
2304 error:
2305 isl_multi_pw_aff_free(mpa);
2306 return NULL;
2309 /* Construct an isl_ast_expr of type "type" that calls or accesses
2310 * the element specified by "mpa".
2311 * The first argument is obtained from the output tuple name.
2312 * The remaining arguments are given by the piecewise affine expressions.
2314 * If the range of "mpa" is a mapped relation, then we assume it
2315 * represents an access to a member of a structure.
2317 * The domain of "mpa" is assumed to live in the internal schedule domain.
2319 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff_internal(
2320 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2321 __isl_take isl_multi_pw_aff *mpa)
2323 isl_ctx *ctx;
2324 isl_id *id;
2325 isl_ast_expr *expr;
2327 if (!mpa)
2328 goto error;
2330 if (type == isl_ast_op_access &&
2331 isl_multi_pw_aff_range_is_wrapping(mpa))
2332 return isl_ast_build_from_multi_pw_aff_member(build, mpa);
2334 mpa = set_iterator_names(build, mpa);
2335 if (!build || !mpa)
2336 goto error;
2338 ctx = isl_ast_build_get_ctx(build);
2340 if (isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out))
2341 id = isl_multi_pw_aff_get_tuple_id(mpa, isl_dim_out);
2342 else
2343 id = isl_id_alloc(ctx, "", NULL);
2345 expr = isl_ast_expr_from_id(id);
2346 return isl_ast_build_with_arguments(build, type, expr, mpa);
2347 error:
2348 isl_multi_pw_aff_free(mpa);
2349 return NULL;
2352 /* Construct an isl_ast_expr of type "type" that calls or accesses
2353 * the element specified by "pma".
2354 * The first argument is obtained from the output tuple name.
2355 * The remaining arguments are given by the piecewise affine expressions.
2357 * The domain of "pma" is assumed to live in the internal schedule domain.
2359 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff_internal(
2360 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2361 __isl_take isl_pw_multi_aff *pma)
2363 isl_multi_pw_aff *mpa;
2365 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2366 return isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2369 /* Construct an isl_ast_expr of type "type" that calls or accesses
2370 * the element specified by "mpa".
2371 * The first argument is obtained from the output tuple name.
2372 * The remaining arguments are given by the piecewise affine expressions.
2374 * The domain of "mpa" is assumed to live in the external schedule domain.
2376 static __isl_give isl_ast_expr *isl_ast_build_from_multi_pw_aff(
2377 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2378 __isl_take isl_multi_pw_aff *mpa)
2380 isl_bool is_domain;
2381 isl_ast_expr *expr;
2382 isl_space *space_build, *space_mpa;
2384 space_build = isl_ast_build_get_space(build, 0);
2385 space_mpa = isl_multi_pw_aff_get_space(mpa);
2386 is_domain = isl_space_tuple_is_equal(space_build, isl_dim_set,
2387 space_mpa, isl_dim_in);
2388 isl_space_free(space_build);
2389 isl_space_free(space_mpa);
2390 if (is_domain < 0)
2391 goto error;
2392 if (!is_domain)
2393 isl_die(isl_ast_build_get_ctx(build), isl_error_invalid,
2394 "spaces don't match", goto error);
2396 if (isl_ast_build_need_schedule_map(build)) {
2397 isl_multi_aff *ma;
2398 ma = isl_ast_build_get_schedule_map_multi_aff(build);
2399 mpa = isl_multi_pw_aff_pullback_multi_aff(mpa, ma);
2402 expr = isl_ast_build_from_multi_pw_aff_internal(build, type, mpa);
2403 return expr;
2404 error:
2405 isl_multi_pw_aff_free(mpa);
2406 return NULL;
2409 /* Construct an isl_ast_expr that calls the domain element specified by "mpa".
2410 * The name of the function is obtained from the output tuple name.
2411 * The arguments are given by the piecewise affine expressions.
2413 * The domain of "mpa" is assumed to live in the external schedule domain.
2415 __isl_give isl_ast_expr *isl_ast_build_call_from_multi_pw_aff(
2416 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2418 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_call, mpa);
2421 /* Construct an isl_ast_expr that accesses the array element specified by "mpa".
2422 * The name of the array is obtained from the output tuple name.
2423 * The index expressions are given by the piecewise affine expressions.
2425 * The domain of "mpa" is assumed to live in the external schedule domain.
2427 __isl_give isl_ast_expr *isl_ast_build_access_from_multi_pw_aff(
2428 __isl_keep isl_ast_build *build, __isl_take isl_multi_pw_aff *mpa)
2430 return isl_ast_build_from_multi_pw_aff(build, isl_ast_op_access, mpa);
2433 /* Construct an isl_ast_expr of type "type" that calls or accesses
2434 * the element specified by "pma".
2435 * The first argument is obtained from the output tuple name.
2436 * The remaining arguments are given by the piecewise affine expressions.
2438 * The domain of "pma" is assumed to live in the external schedule domain.
2440 static __isl_give isl_ast_expr *isl_ast_build_from_pw_multi_aff(
2441 __isl_keep isl_ast_build *build, enum isl_ast_op_type type,
2442 __isl_take isl_pw_multi_aff *pma)
2444 isl_multi_pw_aff *mpa;
2446 mpa = isl_multi_pw_aff_from_pw_multi_aff(pma);
2447 return isl_ast_build_from_multi_pw_aff(build, type, mpa);
2450 /* Construct an isl_ast_expr that calls the domain element specified by "pma".
2451 * The name of the function is obtained from the output tuple name.
2452 * The arguments are given by the piecewise affine expressions.
2454 * The domain of "pma" is assumed to live in the external schedule domain.
2456 __isl_give isl_ast_expr *isl_ast_build_call_from_pw_multi_aff(
2457 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2459 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_call, pma);
2462 /* Construct an isl_ast_expr that accesses the array element specified by "pma".
2463 * The name of the array is obtained from the output tuple name.
2464 * The index expressions are given by the piecewise affine expressions.
2466 * The domain of "pma" is assumed to live in the external schedule domain.
2468 __isl_give isl_ast_expr *isl_ast_build_access_from_pw_multi_aff(
2469 __isl_keep isl_ast_build *build, __isl_take isl_pw_multi_aff *pma)
2471 return isl_ast_build_from_pw_multi_aff(build, isl_ast_op_access, pma);
2474 /* Construct an isl_ast_expr that calls the domain element
2475 * specified by "executed".
2477 * "executed" is assumed to be single-valued, with a domain that lives
2478 * in the internal schedule space.
2480 __isl_give isl_ast_node *isl_ast_build_call_from_executed(
2481 __isl_keep isl_ast_build *build, __isl_take isl_map *executed)
2483 isl_pw_multi_aff *iteration;
2484 isl_ast_expr *expr;
2486 iteration = isl_pw_multi_aff_from_map(executed);
2487 iteration = isl_ast_build_compute_gist_pw_multi_aff(build, iteration);
2488 iteration = isl_pw_multi_aff_intersect_domain(iteration,
2489 isl_ast_build_get_domain(build));
2490 expr = isl_ast_build_from_pw_multi_aff_internal(build, isl_ast_op_call,
2491 iteration);
2492 return isl_ast_node_alloc_user(expr);