1 #include <isl_ctx_private.h>
3 #include <isl_constraint_private.h>
5 #include <isl_polynomial_private.h>
10 struct isl_bound
*bound
;
13 int test_monotonicity
;
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
;
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;
57 type
= data_m
.sign
< 0 ? isl_fold_min
: isl_fold_max
;
58 data_m
.pwf
= isl_pw_qpolynomial_fold_zero(space
, type
);
60 data_m
.pwf_tight
= NULL
;
62 if (propagate_on_domain(bset
, poly
, &data_m
) < 0)
66 opt
= isl_pw_qpolynomial_fold_min(data_m
.pwf
);
68 opt
= isl_pw_qpolynomial_fold_max(data_m
.pwf
);
72 else if (isl_val_is_nan(opt
) ||
73 isl_val_is_infty(opt
) ||
74 isl_val_is_neginfty(opt
))
77 r
= sign
* isl_val_sgn(opt
) >= 0;
83 isl_pw_qpolynomial_fold_free(data_m
.pwf
);
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,
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
)
99 isl_qpolynomial
*sub
= NULL
;
100 isl_qpolynomial
*diff
= NULL
;
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
);
125 s
= has_sign(bset
, diff
, -1, data
->signs
);
132 isl_qpolynomial_free(diff
);
133 isl_qpolynomial_free(sub
);
137 isl_qpolynomial_free(diff
);
138 isl_qpolynomial_free(sub
);
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
,
149 return isl_qpolynomial_infty_on_domain(space
);
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
)
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
)
172 isl_constraint_get_coefficient(bound
, isl_dim_set
, pos
, &c
);
173 is_int
= isl_int_is_one(c
) || isl_int_is_negone(c
);
179 struct isl_fixed_sign_data
{
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
;
200 return isl_stat_error
;
202 nparam
= isl_term_dim(term
, isl_dim_param
);
203 nvar
= isl_term_dim(term
, isl_dim_set
);
207 isl_term_get_num(term
, &n
);
209 sign
= isl_int_sgn(n
);
210 for (i
= 0; i
< nparam
; ++i
) {
211 if (data
->signs
[i
] > 0)
213 if (isl_term_get_exp(term
, isl_dim_param
, i
) % 2)
216 for (i
= 0; i
< nvar
; ++i
) {
217 if (data
->signs
[nparam
+ i
] > 0)
219 if (isl_term_get_exp(term
, isl_dim_set
, i
) % 2)
223 if (sign
== data
->sign
) {
224 isl_qpolynomial
*t
= isl_qpolynomial_from_term(term
);
226 data
->poly
= isl_qpolynomial_add(data
->poly
, t
);
235 /* Construct and return a polynomial that consists of the terms
236 * in "poly" that have sign "sign". The integer divisions, if
237 * any, are assumed to be non-negative.
239 __isl_give isl_qpolynomial
*isl_qpolynomial_terms_of_sign(
240 __isl_keep isl_qpolynomial
*poly
, int *signs
, int sign
)
243 struct isl_fixed_sign_data data
= { signs
, sign
};
245 space
= isl_qpolynomial_get_domain_space(poly
);
246 data
.poly
= isl_qpolynomial_zero_on_domain(space
);
248 if (isl_qpolynomial_foreach_term(poly
, collect_fixed_sign_terms
, &data
) < 0)
253 isl_qpolynomial_free(data
.poly
);
257 /* Helper function to add a guarded polynomial to either pwf_tight or pwf,
258 * depending on whether the result has been determined to be tight.
260 static isl_stat
add_guarded_poly(__isl_take isl_basic_set
*bset
,
261 __isl_take isl_qpolynomial
*poly
, struct range_data
*data
)
263 enum isl_fold type
= data
->sign
< 0 ? isl_fold_min
: isl_fold_max
;
265 isl_qpolynomial_fold
*fold
;
266 isl_pw_qpolynomial_fold
*pwf
;
268 bset
= isl_basic_set_params(bset
);
269 poly
= isl_qpolynomial_project_domain_on_params(poly
);
271 fold
= isl_qpolynomial_fold_alloc(type
, poly
);
272 set
= isl_set_from_basic_set(bset
);
273 pwf
= isl_pw_qpolynomial_fold_alloc(type
, set
, fold
);
275 data
->pwf_tight
= isl_pw_qpolynomial_fold_fold(
276 data
->pwf_tight
, pwf
);
278 data
->pwf
= isl_pw_qpolynomial_fold_fold(data
->pwf
, pwf
);
283 /* Plug in "sub" for the variable at position "pos" in "poly".
285 * If "sub" is an infinite polynomial and if the variable actually
286 * appears in "poly", then calling isl_qpolynomial_substitute
287 * to perform the substitution may result in a NaN result.
288 * In such cases, return positive or negative infinity instead,
289 * depending on whether an upper bound or a lower bound is being computed,
290 * and mark the result as not being tight.
292 static __isl_give isl_qpolynomial
*plug_in_at_pos(
293 __isl_take isl_qpolynomial
*poly
, int pos
,
294 __isl_take isl_qpolynomial
*sub
, struct range_data
*data
)
296 isl_bool involves
, infty
;
298 involves
= isl_qpolynomial_involves_dims(poly
, isl_dim_in
, pos
, 1);
302 isl_qpolynomial_free(sub
);
306 infty
= isl_qpolynomial_is_infty(sub
);
307 if (infty
>= 0 && !infty
)
308 infty
= isl_qpolynomial_is_neginfty(sub
);
312 isl_space
*space
= isl_qpolynomial_get_domain_space(poly
);
314 isl_qpolynomial_free(poly
);
315 isl_qpolynomial_free(sub
);
316 return signed_infty(space
, data
->sign
);
319 poly
= isl_qpolynomial_substitute(poly
, isl_dim_in
, pos
, 1, &sub
);
320 isl_qpolynomial_free(sub
);
324 isl_qpolynomial_free(poly
);
325 isl_qpolynomial_free(sub
);
329 /* Given a lower and upper bound on the final variable and constraints
330 * on the remaining variables where these bounds are active,
331 * eliminate the variable from data->poly based on these bounds.
332 * If the polynomial has been determined to be monotonic
333 * in the variable, then simply plug in the appropriate bound.
334 * If the current polynomial is tight and if this bound is integer,
335 * then the result is still tight. In all other cases, the results
337 * Otherwise, plug in the largest bound (in absolute value) in
338 * the positive terms (if an upper bound is wanted) or the negative terms
339 * (if a lower bounded is wanted) and the other bound in the other terms.
341 * If all variables have been eliminated, then record the result.
342 * Ohterwise, recurse on the next variable.
344 static isl_stat
propagate_on_bound_pair(__isl_take isl_constraint
*lower
,
345 __isl_take isl_constraint
*upper
, __isl_take isl_basic_set
*bset
,
348 struct range_data
*data
= (struct range_data
*)user
;
349 int save_tight
= data
->tight
;
350 isl_qpolynomial
*poly
;
354 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
356 if (data
->monotonicity
) {
357 isl_qpolynomial
*sub
;
358 isl_space
*space
= isl_qpolynomial_get_domain_space(data
->poly
);
359 if (data
->monotonicity
* data
->sign
> 0) {
361 data
->tight
= bound_is_integer(upper
, nvar
);
362 sub
= bound2poly(upper
, space
, nvar
, 1);
363 isl_constraint_free(lower
);
366 data
->tight
= bound_is_integer(lower
, nvar
);
367 sub
= bound2poly(lower
, space
, nvar
, -1);
368 isl_constraint_free(upper
);
370 poly
= isl_qpolynomial_copy(data
->poly
);
371 poly
= plug_in_at_pos(poly
, nvar
, sub
, data
);
372 poly
= isl_qpolynomial_drop_dims(poly
, isl_dim_in
, nvar
, 1);
374 isl_qpolynomial
*l
, *u
;
375 isl_qpolynomial
*pos
, *neg
;
376 isl_space
*space
= isl_qpolynomial_get_domain_space(data
->poly
);
377 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
378 int sign
= data
->sign
* data
->signs
[nparam
+ nvar
];
382 u
= bound2poly(upper
, isl_space_copy(space
), nvar
, 1);
383 l
= bound2poly(lower
, space
, nvar
, -1);
385 pos
= isl_qpolynomial_terms_of_sign(data
->poly
, data
->signs
, sign
);
386 neg
= isl_qpolynomial_terms_of_sign(data
->poly
, data
->signs
, -sign
);
388 pos
= plug_in_at_pos(pos
, nvar
, u
, data
);
389 neg
= plug_in_at_pos(neg
, nvar
, l
, data
);
391 poly
= isl_qpolynomial_add(pos
, neg
);
392 poly
= isl_qpolynomial_drop_dims(poly
, isl_dim_in
, nvar
, 1);
395 if (isl_basic_set_dim(bset
, isl_dim_set
) == 0)
396 r
= add_guarded_poly(bset
, poly
, data
);
398 r
= propagate_on_domain(bset
, poly
, data
);
400 data
->tight
= save_tight
;
405 /* Recursively perform range propagation on the polynomial "poly"
406 * defined over the basic set "bset" and collect the results in "data".
408 static isl_stat
propagate_on_domain(__isl_take isl_basic_set
*bset
,
409 __isl_take isl_qpolynomial
*poly
, struct range_data
*data
)
413 isl_qpolynomial
*save_poly
= data
->poly
;
414 int save_monotonicity
= data
->monotonicity
;
417 is_cst
= isl_qpolynomial_is_cst(poly
, NULL
, NULL
);
418 if (!bset
|| is_cst
< 0)
421 ctx
= isl_basic_set_get_ctx(bset
);
422 d
= isl_basic_set_dim(bset
, isl_dim_set
);
423 isl_assert(ctx
, d
>= 1, goto error
);
426 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, d
);
427 poly
= isl_qpolynomial_drop_dims(poly
, isl_dim_in
, 0, d
);
428 return add_guarded_poly(bset
, poly
, data
);
431 if (data
->test_monotonicity
)
432 data
->monotonicity
= monotonicity(bset
, poly
, data
);
434 data
->monotonicity
= 0;
435 if (data
->monotonicity
< -1)
439 if (isl_basic_set_foreach_bound_pair(bset
, isl_dim_set
, d
- 1,
440 &propagate_on_bound_pair
, data
) < 0)
443 isl_basic_set_free(bset
);
444 isl_qpolynomial_free(poly
);
445 data
->monotonicity
= save_monotonicity
;
446 data
->poly
= save_poly
;
450 isl_basic_set_free(bset
);
451 isl_qpolynomial_free(poly
);
452 data
->monotonicity
= save_monotonicity
;
453 data
->poly
= save_poly
;
454 return isl_stat_error
;
457 static isl_stat
basic_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
460 struct range_data
*data
= (struct range_data
*)user
;
462 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
463 unsigned dim
= isl_basic_set_dim(bset
, isl_dim_set
);
468 ctx
= isl_basic_set_get_ctx(bset
);
469 data
->signs
= isl_alloc_array(ctx
, int,
470 isl_basic_set_dim(bset
, isl_dim_all
));
472 if (isl_basic_set_dims_get_sign(bset
, isl_dim_set
, 0, dim
,
473 data
->signs
+ nparam
) < 0)
475 if (isl_basic_set_dims_get_sign(bset
, isl_dim_param
, 0, nparam
,
479 r
= propagate_on_domain(bset
, isl_qpolynomial_copy(data
->poly
), data
);
486 isl_basic_set_free(bset
);
487 return isl_stat_error
;
490 static isl_stat
qpolynomial_bound_on_domain_range(
491 __isl_take isl_basic_set
*bset
, __isl_take isl_qpolynomial
*poly
,
492 struct range_data
*data
)
494 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
495 unsigned nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
502 return add_guarded_poly(bset
, poly
, data
);
504 set
= isl_set_from_basic_set(bset
);
505 set
= isl_set_split_dims(set
, isl_dim_param
, 0, nparam
);
506 set
= isl_set_split_dims(set
, isl_dim_set
, 0, nvar
);
510 data
->test_monotonicity
= 1;
511 if (isl_set_foreach_basic_set(set
, &basic_guarded_poly_bound
, data
) < 0)
515 isl_qpolynomial_free(poly
);
520 isl_qpolynomial_free(poly
);
521 return isl_stat_error
;
524 isl_stat
isl_qpolynomial_bound_on_domain_range(__isl_take isl_basic_set
*bset
,
525 __isl_take isl_qpolynomial
*poly
, struct isl_bound
*bound
)
527 struct range_data data
;
530 data
.pwf
= bound
->pwf
;
531 data
.pwf_tight
= bound
->pwf_tight
;
532 data
.tight
= bound
->check_tight
;
533 if (bound
->type
== isl_fold_min
)
538 r
= qpolynomial_bound_on_domain_range(bset
, poly
, &data
);
540 bound
->pwf
= data
.pwf
;
541 bound
->pwf_tight
= data
.pwf_tight
;