isl_output.c: print_nested_tuple: rename "local_dim" argument to "local_space"
[isl.git] / isl_range.c
blobddf2636fc5c625211e2de08777b1c81d93b71609
1 #include <isl_ctx_private.h>
2 #include <isl/val.h>
3 #include <isl_constraint_private.h>
4 #include <isl/set.h>
5 #include <isl_polynomial_private.h>
6 #include <isl_morph.h>
7 #include <isl_range.h>
9 struct range_data {
10 struct isl_bound *bound;
11 int *signs;
12 int sign;
13 int test_monotonicity;
14 int monotonicity;
15 int tight;
16 isl_qpolynomial *poly;
17 isl_pw_qpolynomial_fold *pwf;
18 isl_pw_qpolynomial_fold *pwf_tight;
21 static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
22 __isl_take isl_qpolynomial *poly, struct range_data *data);
24 /* Check whether the polynomial "poly" has sign "sign" over "bset",
25 * i.e., if sign == 1, check that the lower bound on the polynomial
26 * is non-negative and if sign == -1, check that the upper bound on
27 * the polynomial is non-positive.
29 static int has_sign(__isl_keep isl_basic_set *bset,
30 __isl_keep isl_qpolynomial *poly, int sign, int *signs)
32 struct range_data data_m;
33 unsigned nparam;
34 isl_space *dim;
35 isl_val *opt;
36 int r;
37 enum isl_fold type;
39 nparam = isl_basic_set_dim(bset, isl_dim_param);
41 bset = isl_basic_set_copy(bset);
42 poly = isl_qpolynomial_copy(poly);
44 bset = isl_basic_set_move_dims(bset, isl_dim_set, 0,
45 isl_dim_param, 0, nparam);
46 poly = isl_qpolynomial_move_dims(poly, isl_dim_in, 0,
47 isl_dim_param, 0, nparam);
49 dim = isl_qpolynomial_get_space(poly);
50 dim = isl_space_params(dim);
51 dim = isl_space_from_domain(dim);
52 dim = isl_space_add_dims(dim, isl_dim_out, 1);
54 data_m.test_monotonicity = 0;
55 data_m.signs = signs;
56 data_m.sign = -sign;
57 type = data_m.sign < 0 ? isl_fold_min : isl_fold_max;
58 data_m.pwf = isl_pw_qpolynomial_fold_zero(dim, type);
59 data_m.tight = 0;
60 data_m.pwf_tight = NULL;
62 if (propagate_on_domain(bset, poly, &data_m) < 0)
63 goto error;
65 if (sign > 0)
66 opt = isl_pw_qpolynomial_fold_min(data_m.pwf);
67 else
68 opt = isl_pw_qpolynomial_fold_max(data_m.pwf);
70 if (!opt)
71 r = -1;
72 else if (isl_val_is_nan(opt) ||
73 isl_val_is_infty(opt) ||
74 isl_val_is_neginfty(opt))
75 r = 0;
76 else
77 r = sign * isl_val_sgn(opt) >= 0;
79 isl_val_free(opt);
81 return r;
82 error:
83 isl_pw_qpolynomial_fold_free(data_m.pwf);
84 return -1;
87 /* Return 1 if poly is monotonically increasing in the last set variable,
88 * -1 if poly is monotonically decreasing in the last set variable,
89 * 0 if no conclusion,
90 * -2 on error.
92 * We simply check the sign of p(x+1)-p(x)
94 static int monotonicity(__isl_keep isl_basic_set *bset,
95 __isl_keep isl_qpolynomial *poly, struct range_data *data)
97 isl_ctx *ctx;
98 isl_space *dim;
99 isl_qpolynomial *sub = NULL;
100 isl_qpolynomial *diff = NULL;
101 int result = 0;
102 int s;
103 unsigned nvar;
105 ctx = isl_qpolynomial_get_ctx(poly);
106 dim = isl_qpolynomial_get_domain_space(poly);
108 nvar = isl_basic_set_dim(bset, isl_dim_set);
110 sub = isl_qpolynomial_var_on_domain(isl_space_copy(dim), isl_dim_set, nvar - 1);
111 sub = isl_qpolynomial_add(sub,
112 isl_qpolynomial_rat_cst_on_domain(dim, ctx->one, ctx->one));
114 diff = isl_qpolynomial_substitute(isl_qpolynomial_copy(poly),
115 isl_dim_in, nvar - 1, 1, &sub);
116 diff = isl_qpolynomial_sub(diff, isl_qpolynomial_copy(poly));
118 s = has_sign(bset, diff, 1, data->signs);
119 if (s < 0)
120 goto error;
121 if (s)
122 result = 1;
123 else {
124 s = has_sign(bset, diff, -1, data->signs);
125 if (s < 0)
126 goto error;
127 if (s)
128 result = -1;
131 isl_qpolynomial_free(diff);
132 isl_qpolynomial_free(sub);
134 return result;
135 error:
136 isl_qpolynomial_free(diff);
137 isl_qpolynomial_free(sub);
138 return -2;
141 /* Return a positive ("sign" > 0) or negative ("sign" < 0) infinite polynomial
142 * with domain space "space".
144 static __isl_give isl_qpolynomial *signed_infty(__isl_take isl_space *space,
145 int sign)
147 if (sign > 0)
148 return isl_qpolynomial_infty_on_domain(space);
149 else
150 return isl_qpolynomial_neginfty_on_domain(space);
153 static __isl_give isl_qpolynomial *bound2poly(__isl_take isl_constraint *bound,
154 __isl_take isl_space *space, unsigned pos, int sign)
156 if (!bound)
157 return signed_infty(space, sign);
158 isl_space_free(space);
159 return isl_qpolynomial_from_constraint(bound, isl_dim_set, pos);
162 static int bound_is_integer(__isl_keep isl_constraint *bound, unsigned pos)
164 isl_int c;
165 int is_int;
167 if (!bound)
168 return 1;
170 isl_int_init(c);
171 isl_constraint_get_coefficient(bound, isl_dim_set, pos, &c);
172 is_int = isl_int_is_one(c) || isl_int_is_negone(c);
173 isl_int_clear(c);
175 return is_int;
178 struct isl_fixed_sign_data {
179 int *signs;
180 int sign;
181 isl_qpolynomial *poly;
184 /* Add term "term" to data->poly if it has sign data->sign.
185 * The sign is determined based on the signs of the parameters
186 * and variables in data->signs. The integer divisions, if
187 * any, are assumed to be non-negative.
189 static isl_stat collect_fixed_sign_terms(__isl_take isl_term *term, void *user)
191 struct isl_fixed_sign_data *data = (struct isl_fixed_sign_data *)user;
192 isl_int n;
193 int i;
194 int sign;
195 unsigned nparam;
196 unsigned nvar;
198 if (!term)
199 return isl_stat_error;
201 nparam = isl_term_dim(term, isl_dim_param);
202 nvar = isl_term_dim(term, isl_dim_set);
204 isl_int_init(n);
206 isl_term_get_num(term, &n);
208 sign = isl_int_sgn(n);
209 for (i = 0; i < nparam; ++i) {
210 if (data->signs[i] > 0)
211 continue;
212 if (isl_term_get_exp(term, isl_dim_param, i) % 2)
213 sign = -sign;
215 for (i = 0; i < nvar; ++i) {
216 if (data->signs[nparam + i] > 0)
217 continue;
218 if (isl_term_get_exp(term, isl_dim_set, i) % 2)
219 sign = -sign;
222 if (sign == data->sign) {
223 isl_qpolynomial *t = isl_qpolynomial_from_term(term);
225 data->poly = isl_qpolynomial_add(data->poly, t);
226 } else
227 isl_term_free(term);
229 isl_int_clear(n);
231 return isl_stat_ok;
234 /* Construct and return a polynomial that consists of the terms
235 * in "poly" that have sign "sign". The integer divisions, if
236 * any, are assumed to be non-negative.
238 __isl_give isl_qpolynomial *isl_qpolynomial_terms_of_sign(
239 __isl_keep isl_qpolynomial *poly, int *signs, int sign)
241 isl_space *space;
242 struct isl_fixed_sign_data data = { signs, sign };
244 space = isl_qpolynomial_get_domain_space(poly);
245 data.poly = isl_qpolynomial_zero_on_domain(space);
247 if (isl_qpolynomial_foreach_term(poly, collect_fixed_sign_terms, &data) < 0)
248 goto error;
250 return data.poly;
251 error:
252 isl_qpolynomial_free(data.poly);
253 return NULL;
256 /* Helper function to add a guarded polynomial to either pwf_tight or pwf,
257 * depending on whether the result has been determined to be tight.
259 static isl_stat add_guarded_poly(__isl_take isl_basic_set *bset,
260 __isl_take isl_qpolynomial *poly, struct range_data *data)
262 enum isl_fold type = data->sign < 0 ? isl_fold_min : isl_fold_max;
263 isl_set *set;
264 isl_qpolynomial_fold *fold;
265 isl_pw_qpolynomial_fold *pwf;
267 bset = isl_basic_set_params(bset);
268 poly = isl_qpolynomial_project_domain_on_params(poly);
270 fold = isl_qpolynomial_fold_alloc(type, poly);
271 set = isl_set_from_basic_set(bset);
272 pwf = isl_pw_qpolynomial_fold_alloc(type, set, fold);
273 if (data->tight)
274 data->pwf_tight = isl_pw_qpolynomial_fold_fold(
275 data->pwf_tight, pwf);
276 else
277 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
279 return isl_stat_ok;
282 /* Plug in "sub" for the variable at position "pos" in "poly".
284 * If "sub" is an infinite polynomial and if the variable actually
285 * appears in "poly", then calling isl_qpolynomial_substitute
286 * to perform the substitution may result in a NaN result.
287 * In such cases, return positive or negative infinity instead,
288 * depending on whether an upper bound or a lower bound is being computed,
289 * and mark the result as not being tight.
291 static __isl_give isl_qpolynomial *plug_in_at_pos(
292 __isl_take isl_qpolynomial *poly, int pos,
293 __isl_take isl_qpolynomial *sub, struct range_data *data)
295 isl_bool involves, infty;
297 involves = isl_qpolynomial_involves_dims(poly, isl_dim_in, pos, 1);
298 if (involves < 0)
299 goto error;
300 if (!involves) {
301 isl_qpolynomial_free(sub);
302 return poly;
305 infty = isl_qpolynomial_is_infty(sub);
306 if (infty >= 0 && !infty)
307 infty = isl_qpolynomial_is_neginfty(sub);
308 if (infty < 0)
309 goto error;
310 if (infty) {
311 isl_space *space = isl_qpolynomial_get_domain_space(poly);
312 data->tight = 0;
313 isl_qpolynomial_free(poly);
314 isl_qpolynomial_free(sub);
315 return signed_infty(space, data->sign);
318 poly = isl_qpolynomial_substitute(poly, isl_dim_in, pos, 1, &sub);
319 isl_qpolynomial_free(sub);
321 return poly;
322 error:
323 isl_qpolynomial_free(poly);
324 isl_qpolynomial_free(sub);
325 return NULL;
328 /* Given a lower and upper bound on the final variable and constraints
329 * on the remaining variables where these bounds are active,
330 * eliminate the variable from data->poly based on these bounds.
331 * If the polynomial has been determined to be monotonic
332 * in the variable, then simply plug in the appropriate bound.
333 * If the current polynomial is tight and if this bound is integer,
334 * then the result is still tight. In all other cases, the results
335 * may not be tight.
336 * Otherwise, plug in the largest bound (in absolute value) in
337 * the positive terms (if an upper bound is wanted) or the negative terms
338 * (if a lower bounded is wanted) and the other bound in the other terms.
340 * If all variables have been eliminated, then record the result.
341 * Ohterwise, recurse on the next variable.
343 static isl_stat propagate_on_bound_pair(__isl_take isl_constraint *lower,
344 __isl_take isl_constraint *upper, __isl_take isl_basic_set *bset,
345 void *user)
347 struct range_data *data = (struct range_data *)user;
348 int save_tight = data->tight;
349 isl_qpolynomial *poly;
350 isl_stat r;
351 unsigned nvar;
353 nvar = isl_basic_set_dim(bset, isl_dim_set);
355 if (data->monotonicity) {
356 isl_qpolynomial *sub;
357 isl_space *dim = isl_qpolynomial_get_domain_space(data->poly);
358 if (data->monotonicity * data->sign > 0) {
359 if (data->tight)
360 data->tight = bound_is_integer(upper, nvar);
361 sub = bound2poly(upper, dim, nvar, 1);
362 isl_constraint_free(lower);
363 } else {
364 if (data->tight)
365 data->tight = bound_is_integer(lower, nvar);
366 sub = bound2poly(lower, dim, nvar, -1);
367 isl_constraint_free(upper);
369 poly = isl_qpolynomial_copy(data->poly);
370 poly = plug_in_at_pos(poly, nvar, sub, data);
371 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
372 } else {
373 isl_qpolynomial *l, *u;
374 isl_qpolynomial *pos, *neg;
375 isl_space *dim = isl_qpolynomial_get_domain_space(data->poly);
376 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
377 int sign = data->sign * data->signs[nparam + nvar];
379 data->tight = 0;
381 u = bound2poly(upper, isl_space_copy(dim), nvar, 1);
382 l = bound2poly(lower, dim, nvar, -1);
384 pos = isl_qpolynomial_terms_of_sign(data->poly, data->signs, sign);
385 neg = isl_qpolynomial_terms_of_sign(data->poly, data->signs, -sign);
387 pos = plug_in_at_pos(pos, nvar, u, data);
388 neg = plug_in_at_pos(neg, nvar, l, data);
390 poly = isl_qpolynomial_add(pos, neg);
391 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
394 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
395 r = add_guarded_poly(bset, poly, data);
396 else
397 r = propagate_on_domain(bset, poly, data);
399 data->tight = save_tight;
401 return r;
404 /* Recursively perform range propagation on the polynomial "poly"
405 * defined over the basic set "bset" and collect the results in "data".
407 static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
408 __isl_take isl_qpolynomial *poly, struct range_data *data)
410 isl_ctx *ctx;
411 isl_qpolynomial *save_poly = data->poly;
412 int save_monotonicity = data->monotonicity;
413 unsigned d;
415 if (!bset || !poly)
416 goto error;
418 ctx = isl_basic_set_get_ctx(bset);
419 d = isl_basic_set_dim(bset, isl_dim_set);
420 isl_assert(ctx, d >= 1, goto error);
422 if (isl_qpolynomial_is_cst(poly, NULL, NULL)) {
423 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, d);
424 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, d);
425 return add_guarded_poly(bset, poly, data);
428 if (data->test_monotonicity)
429 data->monotonicity = monotonicity(bset, poly, data);
430 else
431 data->monotonicity = 0;
432 if (data->monotonicity < -1)
433 goto error;
435 data->poly = poly;
436 if (isl_basic_set_foreach_bound_pair(bset, isl_dim_set, d - 1,
437 &propagate_on_bound_pair, data) < 0)
438 goto error;
440 isl_basic_set_free(bset);
441 isl_qpolynomial_free(poly);
442 data->monotonicity = save_monotonicity;
443 data->poly = save_poly;
445 return isl_stat_ok;
446 error:
447 isl_basic_set_free(bset);
448 isl_qpolynomial_free(poly);
449 data->monotonicity = save_monotonicity;
450 data->poly = save_poly;
451 return isl_stat_error;
454 static isl_stat basic_guarded_poly_bound(__isl_take isl_basic_set *bset,
455 void *user)
457 struct range_data *data = (struct range_data *)user;
458 isl_ctx *ctx;
459 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
460 unsigned dim = isl_basic_set_dim(bset, isl_dim_set);
461 isl_stat r;
463 data->signs = NULL;
465 ctx = isl_basic_set_get_ctx(bset);
466 data->signs = isl_alloc_array(ctx, int,
467 isl_basic_set_dim(bset, isl_dim_all));
469 if (isl_basic_set_dims_get_sign(bset, isl_dim_set, 0, dim,
470 data->signs + nparam) < 0)
471 goto error;
472 if (isl_basic_set_dims_get_sign(bset, isl_dim_param, 0, nparam,
473 data->signs) < 0)
474 goto error;
476 r = propagate_on_domain(bset, isl_qpolynomial_copy(data->poly), data);
478 free(data->signs);
480 return r;
481 error:
482 free(data->signs);
483 isl_basic_set_free(bset);
484 return isl_stat_error;
487 static isl_stat qpolynomial_bound_on_domain_range(
488 __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
489 struct range_data *data)
491 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
492 unsigned nvar = isl_basic_set_dim(bset, isl_dim_set);
493 isl_set *set = NULL;
495 if (!bset)
496 goto error;
498 if (nvar == 0)
499 return add_guarded_poly(bset, poly, data);
501 set = isl_set_from_basic_set(bset);
502 set = isl_set_split_dims(set, isl_dim_param, 0, nparam);
503 set = isl_set_split_dims(set, isl_dim_set, 0, nvar);
505 data->poly = poly;
507 data->test_monotonicity = 1;
508 if (isl_set_foreach_basic_set(set, &basic_guarded_poly_bound, data) < 0)
509 goto error;
511 isl_set_free(set);
512 isl_qpolynomial_free(poly);
514 return isl_stat_ok;
515 error:
516 isl_set_free(set);
517 isl_qpolynomial_free(poly);
518 return isl_stat_error;
521 isl_stat isl_qpolynomial_bound_on_domain_range(__isl_take isl_basic_set *bset,
522 __isl_take isl_qpolynomial *poly, struct isl_bound *bound)
524 struct range_data data;
525 isl_stat r;
527 data.pwf = bound->pwf;
528 data.pwf_tight = bound->pwf_tight;
529 data.tight = bound->check_tight;
530 if (bound->type == isl_fold_min)
531 data.sign = -1;
532 else
533 data.sign = 1;
535 r = qpolynomial_bound_on_domain_range(bset, poly, &data);
537 bound->pwf = data.pwf;
538 bound->pwf_tight = data.pwf_tight;
540 return r;