drop deprecated isl_basic_map_n_*
[isl.git] / isl_range.c
blob0b6769811f31ba30f3d102d7571a37fb93fc563d
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 isl_bool 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 *space;
35 isl_val *opt;
36 isl_bool 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 space = isl_qpolynomial_get_space(poly);
50 space = isl_space_params(space);
51 space = isl_space_from_domain(space);
52 space = isl_space_add_dims(space, 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(space, 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 = isl_bool_error;
72 else if (isl_val_is_nan(opt) ||
73 isl_val_is_infty(opt) ||
74 isl_val_is_neginfty(opt))
75 r = isl_bool_false;
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 isl_bool_error;
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 *space;
99 isl_qpolynomial *sub = NULL;
100 isl_qpolynomial *diff = NULL;
101 int result = 0;
102 isl_bool s;
103 unsigned nvar;
105 ctx = isl_qpolynomial_get_ctx(poly);
106 space = 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(space),
111 isl_dim_set, nvar - 1);
112 sub = isl_qpolynomial_add(sub,
113 isl_qpolynomial_rat_cst_on_domain(space, ctx->one, ctx->one));
115 diff = isl_qpolynomial_substitute(isl_qpolynomial_copy(poly),
116 isl_dim_in, nvar - 1, 1, &sub);
117 diff = isl_qpolynomial_sub(diff, isl_qpolynomial_copy(poly));
119 s = has_sign(bset, diff, 1, data->signs);
120 if (s < 0)
121 goto error;
122 if (s)
123 result = 1;
124 else {
125 s = has_sign(bset, diff, -1, data->signs);
126 if (s < 0)
127 goto error;
128 if (s)
129 result = -1;
132 isl_qpolynomial_free(diff);
133 isl_qpolynomial_free(sub);
135 return result;
136 error:
137 isl_qpolynomial_free(diff);
138 isl_qpolynomial_free(sub);
139 return -2;
142 /* Return a positive ("sign" > 0) or negative ("sign" < 0) infinite polynomial
143 * with domain space "space".
145 static __isl_give isl_qpolynomial *signed_infty(__isl_take isl_space *space,
146 int sign)
148 if (sign > 0)
149 return isl_qpolynomial_infty_on_domain(space);
150 else
151 return isl_qpolynomial_neginfty_on_domain(space);
154 static __isl_give isl_qpolynomial *bound2poly(__isl_take isl_constraint *bound,
155 __isl_take isl_space *space, unsigned pos, int sign)
157 if (!bound)
158 return signed_infty(space, sign);
159 isl_space_free(space);
160 return isl_qpolynomial_from_constraint(bound, isl_dim_set, pos);
163 static int bound_is_integer(__isl_keep isl_constraint *bound, unsigned pos)
165 isl_int c;
166 int is_int;
168 if (!bound)
169 return 1;
171 isl_int_init(c);
172 isl_constraint_get_coefficient(bound, isl_dim_set, pos, &c);
173 is_int = isl_int_is_one(c) || isl_int_is_negone(c);
174 isl_int_clear(c);
176 return is_int;
179 struct isl_fixed_sign_data {
180 int *signs;
181 int sign;
182 isl_qpolynomial *poly;
185 /* Add term "term" to data->poly if it has sign data->sign.
186 * The sign is determined based on the signs of the parameters
187 * and variables in data->signs. The integer divisions, if
188 * any, are assumed to be non-negative.
190 static isl_stat collect_fixed_sign_terms(__isl_take isl_term *term, void *user)
192 struct isl_fixed_sign_data *data = (struct isl_fixed_sign_data *)user;
193 isl_int n;
194 int i;
195 int sign;
196 unsigned nparam;
197 unsigned nvar;
199 if (!term)
200 return isl_stat_error;
202 nparam = isl_term_dim(term, isl_dim_param);
203 nvar = isl_term_dim(term, isl_dim_set);
205 isl_int_init(n);
206 isl_term_get_num(term, &n);
207 sign = isl_int_sgn(n);
208 isl_int_clear(n);
210 for (i = 0; i < nparam; ++i) {
211 if (data->signs[i] > 0)
212 continue;
213 if (isl_term_get_exp(term, isl_dim_param, i) % 2)
214 sign = -sign;
216 for (i = 0; i < nvar; ++i) {
217 if (data->signs[nparam + i] > 0)
218 continue;
219 if (isl_term_get_exp(term, isl_dim_set, i) % 2)
220 sign = -sign;
223 if (sign == data->sign) {
224 isl_qpolynomial *t = isl_qpolynomial_from_term(term);
226 data->poly = isl_qpolynomial_add(data->poly, t);
227 } else
228 isl_term_free(term);
230 return isl_stat_ok;
233 /* Construct and return a polynomial that consists of the terms
234 * in "poly" that have sign "sign". The integer divisions, if
235 * any, are assumed to be non-negative.
237 __isl_give isl_qpolynomial *isl_qpolynomial_terms_of_sign(
238 __isl_keep isl_qpolynomial *poly, int *signs, int sign)
240 isl_space *space;
241 struct isl_fixed_sign_data data = { signs, sign };
243 space = isl_qpolynomial_get_domain_space(poly);
244 data.poly = isl_qpolynomial_zero_on_domain(space);
246 if (isl_qpolynomial_foreach_term(poly, collect_fixed_sign_terms, &data) < 0)
247 goto error;
249 return data.poly;
250 error:
251 isl_qpolynomial_free(data.poly);
252 return NULL;
255 /* Helper function to add a guarded polynomial to either pwf_tight or pwf,
256 * depending on whether the result has been determined to be tight.
258 static isl_stat add_guarded_poly(__isl_take isl_basic_set *bset,
259 __isl_take isl_qpolynomial *poly, struct range_data *data)
261 enum isl_fold type = data->sign < 0 ? isl_fold_min : isl_fold_max;
262 isl_set *set;
263 isl_qpolynomial_fold *fold;
264 isl_pw_qpolynomial_fold *pwf;
266 bset = isl_basic_set_params(bset);
267 poly = isl_qpolynomial_project_domain_on_params(poly);
269 fold = isl_qpolynomial_fold_alloc(type, poly);
270 set = isl_set_from_basic_set(bset);
271 pwf = isl_pw_qpolynomial_fold_alloc(type, set, fold);
272 if (data->tight)
273 data->pwf_tight = isl_pw_qpolynomial_fold_fold(
274 data->pwf_tight, pwf);
275 else
276 data->pwf = isl_pw_qpolynomial_fold_fold(data->pwf, pwf);
278 return isl_stat_ok;
281 /* Plug in "sub" for the variable at position "pos" in "poly".
283 * If "sub" is an infinite polynomial and if the variable actually
284 * appears in "poly", then calling isl_qpolynomial_substitute
285 * to perform the substitution may result in a NaN result.
286 * In such cases, return positive or negative infinity instead,
287 * depending on whether an upper bound or a lower bound is being computed,
288 * and mark the result as not being tight.
290 static __isl_give isl_qpolynomial *plug_in_at_pos(
291 __isl_take isl_qpolynomial *poly, int pos,
292 __isl_take isl_qpolynomial *sub, struct range_data *data)
294 isl_bool involves, infty;
296 involves = isl_qpolynomial_involves_dims(poly, isl_dim_in, pos, 1);
297 if (involves < 0)
298 goto error;
299 if (!involves) {
300 isl_qpolynomial_free(sub);
301 return poly;
304 infty = isl_qpolynomial_is_infty(sub);
305 if (infty >= 0 && !infty)
306 infty = isl_qpolynomial_is_neginfty(sub);
307 if (infty < 0)
308 goto error;
309 if (infty) {
310 isl_space *space = isl_qpolynomial_get_domain_space(poly);
311 data->tight = 0;
312 isl_qpolynomial_free(poly);
313 isl_qpolynomial_free(sub);
314 return signed_infty(space, data->sign);
317 poly = isl_qpolynomial_substitute(poly, isl_dim_in, pos, 1, &sub);
318 isl_qpolynomial_free(sub);
320 return poly;
321 error:
322 isl_qpolynomial_free(poly);
323 isl_qpolynomial_free(sub);
324 return NULL;
327 /* Given a lower and upper bound on the final variable and constraints
328 * on the remaining variables where these bounds are active,
329 * eliminate the variable from data->poly based on these bounds.
330 * If the polynomial has been determined to be monotonic
331 * in the variable, then simply plug in the appropriate bound.
332 * If the current polynomial is tight and if this bound is integer,
333 * then the result is still tight. In all other cases, the results
334 * may not be tight.
335 * Otherwise, plug in the largest bound (in absolute value) in
336 * the positive terms (if an upper bound is wanted) or the negative terms
337 * (if a lower bounded is wanted) and the other bound in the other terms.
339 * If all variables have been eliminated, then record the result.
340 * Ohterwise, recurse on the next variable.
342 static isl_stat propagate_on_bound_pair(__isl_take isl_constraint *lower,
343 __isl_take isl_constraint *upper, __isl_take isl_basic_set *bset,
344 void *user)
346 struct range_data *data = (struct range_data *)user;
347 int save_tight = data->tight;
348 isl_qpolynomial *poly;
349 isl_stat r;
350 unsigned nvar;
352 nvar = isl_basic_set_dim(bset, isl_dim_set);
354 if (data->monotonicity) {
355 isl_qpolynomial *sub;
356 isl_space *space = isl_qpolynomial_get_domain_space(data->poly);
357 if (data->monotonicity * data->sign > 0) {
358 if (data->tight)
359 data->tight = bound_is_integer(upper, nvar);
360 sub = bound2poly(upper, space, nvar, 1);
361 isl_constraint_free(lower);
362 } else {
363 if (data->tight)
364 data->tight = bound_is_integer(lower, nvar);
365 sub = bound2poly(lower, space, nvar, -1);
366 isl_constraint_free(upper);
368 poly = isl_qpolynomial_copy(data->poly);
369 poly = plug_in_at_pos(poly, nvar, sub, data);
370 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
371 } else {
372 isl_qpolynomial *l, *u;
373 isl_qpolynomial *pos, *neg;
374 isl_space *space = isl_qpolynomial_get_domain_space(data->poly);
375 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
376 int sign = data->sign * data->signs[nparam + nvar];
378 data->tight = 0;
380 u = bound2poly(upper, isl_space_copy(space), nvar, 1);
381 l = bound2poly(lower, space, nvar, -1);
383 pos = isl_qpolynomial_terms_of_sign(data->poly, data->signs, sign);
384 neg = isl_qpolynomial_terms_of_sign(data->poly, data->signs, -sign);
386 pos = plug_in_at_pos(pos, nvar, u, data);
387 neg = plug_in_at_pos(neg, nvar, l, data);
389 poly = isl_qpolynomial_add(pos, neg);
390 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, nvar, 1);
393 if (isl_basic_set_dim(bset, isl_dim_set) == 0)
394 r = add_guarded_poly(bset, poly, data);
395 else
396 r = propagate_on_domain(bset, poly, data);
398 data->tight = save_tight;
400 return r;
403 /* Recursively perform range propagation on the polynomial "poly"
404 * defined over the basic set "bset" and collect the results in "data".
406 static isl_stat propagate_on_domain(__isl_take isl_basic_set *bset,
407 __isl_take isl_qpolynomial *poly, struct range_data *data)
409 isl_bool is_cst;
410 isl_ctx *ctx;
411 isl_qpolynomial *save_poly = data->poly;
412 int save_monotonicity = data->monotonicity;
413 unsigned d;
415 is_cst = isl_qpolynomial_is_cst(poly, NULL, NULL);
416 if (!bset || is_cst < 0)
417 goto error;
419 ctx = isl_basic_set_get_ctx(bset);
420 d = isl_basic_set_dim(bset, isl_dim_set);
421 isl_assert(ctx, d >= 1, goto error);
423 if (is_cst) {
424 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, d);
425 poly = isl_qpolynomial_drop_dims(poly, isl_dim_in, 0, d);
426 return add_guarded_poly(bset, poly, data);
429 if (data->test_monotonicity)
430 data->monotonicity = monotonicity(bset, poly, data);
431 else
432 data->monotonicity = 0;
433 if (data->monotonicity < -1)
434 goto error;
436 data->poly = poly;
437 if (isl_basic_set_foreach_bound_pair(bset, isl_dim_set, d - 1,
438 &propagate_on_bound_pair, data) < 0)
439 goto error;
441 isl_basic_set_free(bset);
442 isl_qpolynomial_free(poly);
443 data->monotonicity = save_monotonicity;
444 data->poly = save_poly;
446 return isl_stat_ok;
447 error:
448 isl_basic_set_free(bset);
449 isl_qpolynomial_free(poly);
450 data->monotonicity = save_monotonicity;
451 data->poly = save_poly;
452 return isl_stat_error;
455 static isl_stat basic_guarded_poly_bound(__isl_take isl_basic_set *bset,
456 void *user)
458 struct range_data *data = (struct range_data *)user;
459 isl_ctx *ctx;
460 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
461 unsigned dim = isl_basic_set_dim(bset, isl_dim_set);
462 isl_stat r;
464 data->signs = NULL;
466 ctx = isl_basic_set_get_ctx(bset);
467 data->signs = isl_alloc_array(ctx, int,
468 isl_basic_set_dim(bset, isl_dim_all));
470 if (isl_basic_set_dims_get_sign(bset, isl_dim_set, 0, dim,
471 data->signs + nparam) < 0)
472 goto error;
473 if (isl_basic_set_dims_get_sign(bset, isl_dim_param, 0, nparam,
474 data->signs) < 0)
475 goto error;
477 r = propagate_on_domain(bset, isl_qpolynomial_copy(data->poly), data);
479 free(data->signs);
481 return r;
482 error:
483 free(data->signs);
484 isl_basic_set_free(bset);
485 return isl_stat_error;
488 static isl_stat qpolynomial_bound_on_domain_range(
489 __isl_take isl_basic_set *bset, __isl_take isl_qpolynomial *poly,
490 struct range_data *data)
492 unsigned nparam = isl_basic_set_dim(bset, isl_dim_param);
493 unsigned nvar = isl_basic_set_dim(bset, isl_dim_set);
494 isl_set *set = NULL;
496 if (!bset)
497 goto error;
499 if (nvar == 0)
500 return add_guarded_poly(bset, poly, data);
502 set = isl_set_from_basic_set(bset);
503 set = isl_set_split_dims(set, isl_dim_param, 0, nparam);
504 set = isl_set_split_dims(set, isl_dim_set, 0, nvar);
506 data->poly = poly;
508 data->test_monotonicity = 1;
509 if (isl_set_foreach_basic_set(set, &basic_guarded_poly_bound, data) < 0)
510 goto error;
512 isl_set_free(set);
513 isl_qpolynomial_free(poly);
515 return isl_stat_ok;
516 error:
517 isl_set_free(set);
518 isl_qpolynomial_free(poly);
519 return isl_stat_error;
522 isl_stat isl_qpolynomial_bound_on_domain_range(__isl_take isl_basic_set *bset,
523 __isl_take isl_qpolynomial *poly, struct isl_bound *bound)
525 struct range_data data;
526 isl_stat r;
528 data.pwf = bound->pwf;
529 data.pwf_tight = bound->pwf_tight;
530 data.tight = bound->check_tight;
531 if (bound->type == isl_fold_min)
532 data.sign = -1;
533 else
534 data.sign = 1;
536 r = qpolynomial_bound_on_domain_range(bset, poly, &data);
538 bound->pwf = data.pwf;
539 bound->pwf_tight = data.pwf_tight;
541 return r;