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 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;
57 type
= data_m
.sign
< 0 ? isl_fold_min
: isl_fold_max
;
58 data_m
.pwf
= isl_pw_qpolynomial_fold_zero(dim
, 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 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
);
124 s
= has_sign(bset
, diff
, -1, data
->signs
);
131 isl_qpolynomial_free(diff
);
132 isl_qpolynomial_free(sub
);
136 isl_qpolynomial_free(diff
);
137 isl_qpolynomial_free(sub
);
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
,
148 return isl_qpolynomial_infty_on_domain(space
);
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
)
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
)
171 isl_constraint_get_coefficient(bound
, isl_dim_set
, pos
, &c
);
172 is_int
= isl_int_is_one(c
) || isl_int_is_negone(c
);
178 struct isl_fixed_sign_data
{
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
;
199 return isl_stat_error
;
201 nparam
= isl_term_dim(term
, isl_dim_param
);
202 nvar
= isl_term_dim(term
, isl_dim_set
);
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)
212 if (isl_term_get_exp(term
, isl_dim_param
, i
) % 2)
215 for (i
= 0; i
< nvar
; ++i
) {
216 if (data
->signs
[nparam
+ i
] > 0)
218 if (isl_term_get_exp(term
, isl_dim_set
, i
) % 2)
222 if (sign
== data
->sign
) {
223 isl_qpolynomial
*t
= isl_qpolynomial_from_term(term
);
225 data
->poly
= isl_qpolynomial_add(data
->poly
, t
);
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
)
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)
252 isl_qpolynomial_free(data
.poly
);
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
;
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
);
274 data
->pwf_tight
= isl_pw_qpolynomial_fold_fold(
275 data
->pwf_tight
, pwf
);
277 data
->pwf
= isl_pw_qpolynomial_fold_fold(data
->pwf
, pwf
);
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);
301 isl_qpolynomial_free(sub
);
305 infty
= isl_qpolynomial_is_infty(sub
);
306 if (infty
>= 0 && !infty
)
307 infty
= isl_qpolynomial_is_neginfty(sub
);
311 isl_space
*space
= isl_qpolynomial_get_domain_space(poly
);
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
);
323 isl_qpolynomial_free(poly
);
324 isl_qpolynomial_free(sub
);
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
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
,
347 struct range_data
*data
= (struct range_data
*)user
;
348 int save_tight
= data
->tight
;
349 isl_qpolynomial
*poly
;
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) {
360 data
->tight
= bound_is_integer(upper
, nvar
);
361 sub
= bound2poly(upper
, dim
, nvar
, 1);
362 isl_constraint_free(lower
);
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);
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
];
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
);
397 r
= propagate_on_domain(bset
, poly
, data
);
399 data
->tight
= save_tight
;
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
)
411 isl_qpolynomial
*save_poly
= data
->poly
;
412 int save_monotonicity
= data
->monotonicity
;
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
);
431 data
->monotonicity
= 0;
432 if (data
->monotonicity
< -1)
436 if (isl_basic_set_foreach_bound_pair(bset
, isl_dim_set
, d
- 1,
437 &propagate_on_bound_pair
, data
) < 0)
440 isl_basic_set_free(bset
);
441 isl_qpolynomial_free(poly
);
442 data
->monotonicity
= save_monotonicity
;
443 data
->poly
= save_poly
;
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
,
457 struct range_data
*data
= (struct range_data
*)user
;
459 unsigned nparam
= isl_basic_set_dim(bset
, isl_dim_param
);
460 unsigned dim
= isl_basic_set_dim(bset
, isl_dim_set
);
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)
472 if (isl_basic_set_dims_get_sign(bset
, isl_dim_param
, 0, nparam
,
476 r
= propagate_on_domain(bset
, isl_qpolynomial_copy(data
->poly
), data
);
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
);
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
);
507 data
->test_monotonicity
= 1;
508 if (isl_set_foreach_basic_set(set
, &basic_guarded_poly_bound
, data
) < 0)
512 isl_qpolynomial_free(poly
);
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
;
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
)
535 r
= qpolynomial_bound_on_domain_range(bset
, poly
, &data
);
537 bound
->pwf
= data
.pwf
;
538 bound
->pwf_tight
= data
.pwf_tight
;