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 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
;
39 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
41 return isl_bool_error
;
43 bset
= isl_basic_set_copy(bset
);
44 poly
= isl_qpolynomial_copy(poly
);
46 bset
= isl_basic_set_move_dims(bset
, isl_dim_set
, 0,
47 isl_dim_param
, 0, nparam
);
48 poly
= isl_qpolynomial_move_dims(poly
, isl_dim_in
, 0,
49 isl_dim_param
, 0, nparam
);
51 space
= isl_qpolynomial_get_space(poly
);
52 space
= isl_space_params(space
);
53 space
= isl_space_from_domain(space
);
54 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
56 data_m
.test_monotonicity
= 0;
59 type
= data_m
.sign
< 0 ? isl_fold_min
: isl_fold_max
;
60 data_m
.pwf
= isl_pw_qpolynomial_fold_zero(space
, type
);
62 data_m
.pwf_tight
= NULL
;
64 if (propagate_on_domain(bset
, poly
, &data_m
) < 0)
68 opt
= isl_pw_qpolynomial_fold_min(data_m
.pwf
);
70 opt
= isl_pw_qpolynomial_fold_max(data_m
.pwf
);
74 else if (isl_val_is_nan(opt
) ||
75 isl_val_is_infty(opt
) ||
76 isl_val_is_neginfty(opt
))
79 r
= isl_bool_ok(sign
* isl_val_sgn(opt
) >= 0);
85 isl_pw_qpolynomial_fold_free(data_m
.pwf
);
86 return isl_bool_error
;
89 /* Return 1 if poly is monotonically increasing in the last set variable,
90 * -1 if poly is monotonically decreasing in the last set variable,
94 * We simply check the sign of p(x+1)-p(x)
96 static int monotonicity(__isl_keep isl_basic_set
*bset
,
97 __isl_keep isl_qpolynomial
*poly
, struct range_data
*data
)
101 isl_qpolynomial
*sub
= NULL
;
102 isl_qpolynomial
*diff
= NULL
;
107 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
111 ctx
= isl_qpolynomial_get_ctx(poly
);
112 space
= isl_qpolynomial_get_domain_space(poly
);
114 sub
= isl_qpolynomial_var_on_domain(isl_space_copy(space
),
115 isl_dim_set
, nvar
- 1);
116 sub
= isl_qpolynomial_add(sub
,
117 isl_qpolynomial_rat_cst_on_domain(space
, ctx
->one
, ctx
->one
));
119 diff
= isl_qpolynomial_substitute(isl_qpolynomial_copy(poly
),
120 isl_dim_in
, nvar
- 1, 1, &sub
);
121 diff
= isl_qpolynomial_sub(diff
, isl_qpolynomial_copy(poly
));
123 s
= has_sign(bset
, diff
, 1, data
->signs
);
129 s
= has_sign(bset
, diff
, -1, data
->signs
);
136 isl_qpolynomial_free(diff
);
137 isl_qpolynomial_free(sub
);
141 isl_qpolynomial_free(diff
);
142 isl_qpolynomial_free(sub
);
146 /* Return a positive ("sign" > 0) or negative ("sign" < 0) infinite polynomial
147 * with domain space "space".
149 static __isl_give isl_qpolynomial
*signed_infty(__isl_take isl_space
*space
,
153 return isl_qpolynomial_infty_on_domain(space
);
155 return isl_qpolynomial_neginfty_on_domain(space
);
158 static __isl_give isl_qpolynomial
*bound2poly(__isl_take isl_constraint
*bound
,
159 __isl_take isl_space
*space
, unsigned pos
, int sign
)
162 return signed_infty(space
, sign
);
163 isl_space_free(space
);
164 return isl_qpolynomial_from_constraint(bound
, isl_dim_set
, pos
);
167 static int bound_is_integer(__isl_keep isl_constraint
*bound
, unsigned pos
)
176 isl_constraint_get_coefficient(bound
, isl_dim_set
, pos
, &c
);
177 is_int
= isl_int_is_one(c
) || isl_int_is_negone(c
);
183 struct isl_fixed_sign_data
{
186 isl_qpolynomial
*poly
;
189 /* Add term "term" to data->poly if it has sign data->sign.
190 * The sign is determined based on the signs of the parameters
191 * and variables in data->signs. The integer divisions, if
192 * any, are assumed to be non-negative.
194 static isl_stat
collect_fixed_sign_terms(__isl_take isl_term
*term
, void *user
)
196 struct isl_fixed_sign_data
*data
= (struct isl_fixed_sign_data
*)user
;
204 nparam
= isl_term_dim(term
, isl_dim_param
);
205 nvar
= isl_term_dim(term
, isl_dim_set
);
206 if (nparam
< 0 || nvar
< 0)
207 return isl_stat_error
;
210 isl_term_get_num(term
, &n
);
211 sign
= isl_int_sgn(n
);
214 for (i
= 0; i
< nparam
; ++i
) {
215 if (data
->signs
[i
] > 0)
217 exp
= isl_term_get_exp(term
, isl_dim_param
, i
);
219 return isl_stat_error
;
223 for (i
= 0; i
< nvar
; ++i
) {
224 if (data
->signs
[nparam
+ i
] > 0)
226 exp
= isl_term_get_exp(term
, isl_dim_set
, i
);
228 return isl_stat_error
;
233 if (sign
== data
->sign
) {
234 isl_qpolynomial
*t
= isl_qpolynomial_from_term(term
);
236 data
->poly
= isl_qpolynomial_add(data
->poly
, t
);
243 /* Construct and return a polynomial that consists of the terms
244 * in "poly" that have sign "sign". The integer divisions, if
245 * any, are assumed to be non-negative.
247 __isl_give isl_qpolynomial
*isl_qpolynomial_terms_of_sign(
248 __isl_keep isl_qpolynomial
*poly
, int *signs
, int sign
)
251 struct isl_fixed_sign_data data
= { signs
, sign
};
253 space
= isl_qpolynomial_get_domain_space(poly
);
254 data
.poly
= isl_qpolynomial_zero_on_domain(space
);
256 if (isl_qpolynomial_foreach_term(poly
, collect_fixed_sign_terms
, &data
) < 0)
261 isl_qpolynomial_free(data
.poly
);
265 /* Helper function to add a guarded polynomial to either pwf_tight or pwf,
266 * depending on whether the result has been determined to be tight.
268 static isl_stat
add_guarded_poly(__isl_take isl_basic_set
*bset
,
269 __isl_take isl_qpolynomial
*poly
, struct range_data
*data
)
271 enum isl_fold type
= data
->sign
< 0 ? isl_fold_min
: isl_fold_max
;
273 isl_qpolynomial_fold
*fold
;
274 isl_pw_qpolynomial_fold
*pwf
;
276 bset
= isl_basic_set_params(bset
);
277 poly
= isl_qpolynomial_project_domain_on_params(poly
);
279 fold
= isl_qpolynomial_fold_alloc(type
, poly
);
280 set
= isl_set_from_basic_set(bset
);
281 pwf
= isl_pw_qpolynomial_fold_alloc(type
, set
, fold
);
283 data
->pwf_tight
= isl_pw_qpolynomial_fold_fold(
284 data
->pwf_tight
, pwf
);
286 data
->pwf
= isl_pw_qpolynomial_fold_fold(data
->pwf
, pwf
);
291 /* Plug in "sub" for the variable at position "pos" in "poly".
293 * If "sub" is an infinite polynomial and if the variable actually
294 * appears in "poly", then calling isl_qpolynomial_substitute
295 * to perform the substitution may result in a NaN result.
296 * In such cases, return positive or negative infinity instead,
297 * depending on whether an upper bound or a lower bound is being computed,
298 * and mark the result as not being tight.
300 static __isl_give isl_qpolynomial
*plug_in_at_pos(
301 __isl_take isl_qpolynomial
*poly
, int pos
,
302 __isl_take isl_qpolynomial
*sub
, struct range_data
*data
)
304 isl_bool involves
, infty
;
306 involves
= isl_qpolynomial_involves_dims(poly
, isl_dim_in
, pos
, 1);
310 isl_qpolynomial_free(sub
);
314 infty
= isl_qpolynomial_is_infty(sub
);
315 if (infty
>= 0 && !infty
)
316 infty
= isl_qpolynomial_is_neginfty(sub
);
320 isl_space
*space
= isl_qpolynomial_get_domain_space(poly
);
322 isl_qpolynomial_free(poly
);
323 isl_qpolynomial_free(sub
);
324 return signed_infty(space
, data
->sign
);
327 poly
= isl_qpolynomial_substitute(poly
, isl_dim_in
, pos
, 1, &sub
);
328 isl_qpolynomial_free(sub
);
332 isl_qpolynomial_free(poly
);
333 isl_qpolynomial_free(sub
);
337 /* Given a lower and upper bound on the final variable and constraints
338 * on the remaining variables where these bounds are active,
339 * eliminate the variable from data->poly based on these bounds.
340 * If the polynomial has been determined to be monotonic
341 * in the variable, then simply plug in the appropriate bound.
342 * If the current polynomial is tight and if this bound is integer,
343 * then the result is still tight. In all other cases, the results
345 * Otherwise, plug in the largest bound (in absolute value) in
346 * the positive terms (if an upper bound is wanted) or the negative terms
347 * (if a lower bounded is wanted) and the other bound in the other terms.
349 * If all variables have been eliminated, then record the result.
350 * Ohterwise, recurse on the next variable.
352 static isl_stat
propagate_on_bound_pair(__isl_take isl_constraint
*lower
,
353 __isl_take isl_constraint
*upper
, __isl_take isl_basic_set
*bset
,
356 struct range_data
*data
= (struct range_data
*)user
;
357 int save_tight
= data
->tight
;
358 isl_qpolynomial
*poly
;
360 isl_size nvar
, nparam
;
362 nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
363 nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
364 if (nvar
< 0 || nparam
< 0)
367 if (data
->monotonicity
) {
368 isl_qpolynomial
*sub
;
369 isl_space
*space
= isl_qpolynomial_get_domain_space(data
->poly
);
370 if (data
->monotonicity
* data
->sign
> 0) {
372 data
->tight
= bound_is_integer(upper
, nvar
);
373 sub
= bound2poly(upper
, space
, nvar
, 1);
374 isl_constraint_free(lower
);
377 data
->tight
= bound_is_integer(lower
, nvar
);
378 sub
= bound2poly(lower
, space
, nvar
, -1);
379 isl_constraint_free(upper
);
381 poly
= isl_qpolynomial_copy(data
->poly
);
382 poly
= plug_in_at_pos(poly
, nvar
, sub
, data
);
383 poly
= isl_qpolynomial_drop_dims(poly
, isl_dim_in
, nvar
, 1);
385 isl_qpolynomial
*l
, *u
;
386 isl_qpolynomial
*pos
, *neg
;
387 isl_space
*space
= isl_qpolynomial_get_domain_space(data
->poly
);
388 int sign
= data
->sign
* data
->signs
[nparam
+ nvar
];
392 u
= bound2poly(upper
, isl_space_copy(space
), nvar
, 1);
393 l
= bound2poly(lower
, space
, nvar
, -1);
395 pos
= isl_qpolynomial_terms_of_sign(data
->poly
, data
->signs
, sign
);
396 neg
= isl_qpolynomial_terms_of_sign(data
->poly
, data
->signs
, -sign
);
398 pos
= plug_in_at_pos(pos
, nvar
, u
, data
);
399 neg
= plug_in_at_pos(neg
, nvar
, l
, data
);
401 poly
= isl_qpolynomial_add(pos
, neg
);
402 poly
= isl_qpolynomial_drop_dims(poly
, isl_dim_in
, nvar
, 1);
406 r
= add_guarded_poly(bset
, poly
, data
);
408 r
= propagate_on_domain(bset
, poly
, data
);
410 data
->tight
= save_tight
;
414 isl_constraint_free(lower
);
415 isl_constraint_free(upper
);
416 isl_basic_set_free(bset
);
417 return isl_stat_error
;
420 /* Recursively perform range propagation on the polynomial "poly"
421 * defined over the basic set "bset" and collect the results in "data".
423 static isl_stat
propagate_on_domain(__isl_take isl_basic_set
*bset
,
424 __isl_take isl_qpolynomial
*poly
, struct range_data
*data
)
428 isl_qpolynomial
*save_poly
= data
->poly
;
429 int save_monotonicity
= data
->monotonicity
;
432 d
= isl_basic_set_dim(bset
, isl_dim_set
);
433 is_cst
= isl_qpolynomial_is_cst(poly
, NULL
, NULL
);
434 if (d
< 0 || is_cst
< 0)
437 ctx
= isl_basic_set_get_ctx(bset
);
438 isl_assert(ctx
, d
>= 1, goto error
);
441 bset
= isl_basic_set_project_out(bset
, isl_dim_set
, 0, d
);
442 poly
= isl_qpolynomial_drop_dims(poly
, isl_dim_in
, 0, d
);
443 return add_guarded_poly(bset
, poly
, data
);
446 if (data
->test_monotonicity
)
447 data
->monotonicity
= monotonicity(bset
, poly
, data
);
449 data
->monotonicity
= 0;
450 if (data
->monotonicity
< -1)
454 if (isl_basic_set_foreach_bound_pair(bset
, isl_dim_set
, d
- 1,
455 &propagate_on_bound_pair
, data
) < 0)
458 isl_basic_set_free(bset
);
459 isl_qpolynomial_free(poly
);
460 data
->monotonicity
= save_monotonicity
;
461 data
->poly
= save_poly
;
465 isl_basic_set_free(bset
);
466 isl_qpolynomial_free(poly
);
467 data
->monotonicity
= save_monotonicity
;
468 data
->poly
= save_poly
;
469 return isl_stat_error
;
472 static isl_stat
basic_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
475 struct range_data
*data
= (struct range_data
*)user
;
477 isl_size nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
478 isl_size dim
= isl_basic_set_dim(bset
, isl_dim_set
);
479 isl_size total
= isl_basic_set_dim(bset
, isl_dim_all
);
484 if (nparam
< 0 || dim
< 0 || total
< 0)
487 ctx
= isl_basic_set_get_ctx(bset
);
488 data
->signs
= isl_alloc_array(ctx
, int, total
);
490 if (isl_basic_set_dims_get_sign(bset
, isl_dim_set
, 0, dim
,
491 data
->signs
+ nparam
) < 0)
493 if (isl_basic_set_dims_get_sign(bset
, isl_dim_param
, 0, nparam
,
497 r
= propagate_on_domain(bset
, isl_qpolynomial_copy(data
->poly
), data
);
504 isl_basic_set_free(bset
);
505 return isl_stat_error
;
508 static isl_stat
qpolynomial_bound_on_domain_range(
509 __isl_take isl_basic_set
*bset
, __isl_take isl_qpolynomial
*poly
,
510 struct range_data
*data
)
512 isl_size nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
513 isl_size nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
516 if (nparam
< 0 || nvar
< 0)
520 return add_guarded_poly(bset
, poly
, data
);
522 set
= isl_set_from_basic_set(bset
);
523 set
= isl_set_split_dims(set
, isl_dim_param
, 0, nparam
);
524 set
= isl_set_split_dims(set
, isl_dim_set
, 0, nvar
);
528 data
->test_monotonicity
= 1;
529 if (isl_set_foreach_basic_set(set
, &basic_guarded_poly_bound
, data
) < 0)
533 isl_qpolynomial_free(poly
);
538 isl_qpolynomial_free(poly
);
539 return isl_stat_error
;
542 isl_stat
isl_qpolynomial_bound_on_domain_range(__isl_take isl_basic_set
*bset
,
543 __isl_take isl_qpolynomial
*poly
, struct isl_bound
*bound
)
545 struct range_data data
;
548 data
.pwf
= bound
->pwf
;
549 data
.pwf_tight
= bound
->pwf_tight
;
550 data
.tight
= bound
->check_tight
;
551 if (bound
->type
== isl_fold_min
)
556 r
= qpolynomial_bound_on_domain_range(bset
, poly
, &data
);
558 bound
->pwf
= data
.pwf
;
559 bound
->pwf_tight
= data
.pwf_tight
;