2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_bound.h>
12 #include <isl_bernstein.h>
13 #include <isl_range.h>
14 #include <isl_polynomial_private.h>
15 #include <isl_map_private.h>
17 /* Compute a bound on the polynomial defined over the parametric polytope
18 * using either range propagation or bernstein expansion and
19 * store the result in bound->pwf and bound->pwf_tight.
20 * Since bernstein expansion requires bounded domains, we apply
21 * range propagation on unbounded domains. Otherwise, we respect the choice
24 static int compressed_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
25 __isl_take isl_qpolynomial
*poly
, void *user
)
27 struct isl_bound
*bound
= (struct isl_bound
*)user
;
33 if (bset
->ctx
->opt
->bound
== ISL_BOUND_RANGE
)
34 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
36 bounded
= isl_basic_set_is_bounded(bset
);
40 return isl_qpolynomial_bound_on_domain_bernstein(bset
, poly
, bound
);
42 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
44 isl_basic_set_free(bset
);
45 isl_qpolynomial_free(poly
);
49 static int unwrapped_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
50 __isl_take isl_qpolynomial
*poly
, void *user
)
52 struct isl_bound
*bound
= (struct isl_bound
*)user
;
53 isl_pw_qpolynomial_fold
*top_pwf
;
54 isl_pw_qpolynomial_fold
*top_pwf_tight
;
57 unsigned orig_nvar
, final_nvar
;
60 bset
= isl_basic_set_detect_equalities(bset
);
66 return compressed_guarded_poly_bound(bset
, poly
, user
);
68 orig_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
70 morph
= isl_basic_set_full_compression(bset
);
72 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
73 poly
= isl_qpolynomial_morph(poly
, isl_morph_copy(morph
));
75 final_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
77 dim
= isl_morph_get_ran_dim(morph
);
78 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, isl_dim_size(dim
, isl_dim_set
));
81 top_pwf_tight
= bound
->pwf_tight
;
83 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
),
85 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(dim
, bound
->type
);
87 r
= compressed_guarded_poly_bound(bset
, poly
, user
);
89 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, orig_nvar
);
90 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, final_nvar
);
91 morph
= isl_morph_inverse(morph
);
93 bound
->pwf
= isl_pw_qpolynomial_fold_morph(bound
->pwf
,
94 isl_morph_copy(morph
));
95 bound
->pwf_tight
= isl_pw_qpolynomial_fold_morph(bound
->pwf_tight
, morph
);
97 bound
->pwf
= isl_pw_qpolynomial_fold_fold(top_pwf
, bound
->pwf
);
98 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(top_pwf_tight
,
103 isl_basic_set_free(bset
);
104 isl_qpolynomial_free(poly
);
108 static int guarded_poly_bound(__isl_take isl_basic_set
*bset
,
109 __isl_take isl_qpolynomial
*poly
, void *user
)
111 struct isl_bound
*bound
= (struct isl_bound
*)user
;
114 isl_pw_qpolynomial_fold
*top_pwf
;
115 isl_pw_qpolynomial_fold
*top_pwf_tight
;
120 if (!isl_basic_set_is_wrapping(bset
))
121 return unwrapped_guarded_poly_bound(bset
, poly
, user
);
123 target_dim
= isl_basic_set_get_dim(bset
);
124 target_dim
= isl_dim_unwrap(target_dim
);
125 target_dim
= isl_dim_domain(target_dim
);
127 nparam
= isl_dim_size(target_dim
, isl_dim_param
);
128 n_in
= isl_dim_size(target_dim
, isl_dim_set
);
130 bset
= isl_basic_set_move_dims(bset
, isl_dim_param
, nparam
,
131 isl_dim_set
, 0, n_in
);
132 poly
= isl_qpolynomial_move_dims(poly
, isl_dim_param
, nparam
,
133 isl_dim_set
, 0, n_in
);
135 dim
= isl_basic_set_get_dim(bset
);
136 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, isl_dim_size(dim
, isl_dim_set
));
138 top_pwf
= bound
->pwf
;
139 top_pwf_tight
= bound
->pwf_tight
;
141 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
),
143 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(dim
, bound
->type
);
145 r
= unwrapped_guarded_poly_bound(bset
, poly
, user
);
147 bound
->pwf
= isl_pw_qpolynomial_fold_reset_dim(bound
->pwf
,
148 isl_dim_copy(target_dim
));
149 bound
->pwf_tight
= isl_pw_qpolynomial_fold_reset_dim(bound
->pwf_tight
,
152 bound
->pwf
= isl_pw_qpolynomial_fold_fold(top_pwf
, bound
->pwf
);
153 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(top_pwf_tight
,
159 static int guarded_qp(__isl_take isl_qpolynomial
*qp
, void *user
)
161 struct isl_bound
*bound
= (struct isl_bound
*)user
;
164 r
= isl_qpolynomial_as_polynomial_on_domain(qp
, bound
->bset
,
165 &guarded_poly_bound
, user
);
166 isl_qpolynomial_free(qp
);
170 static int basic_guarded_fold(__isl_take isl_basic_set
*bset
, void *user
)
172 struct isl_bound
*bound
= (struct isl_bound
*)user
;
176 r
= isl_qpolynomial_fold_foreach_qpolynomial(bound
->fold
,
178 isl_basic_set_free(bset
);
182 static int guarded_fold(__isl_take isl_set
*set
,
183 __isl_take isl_qpolynomial_fold
*fold
, void *user
)
185 struct isl_bound
*bound
= (struct isl_bound
*)user
;
190 set
= isl_set_make_disjoint(set
);
193 bound
->type
= isl_qpolynomial_fold_get_type(fold
);
195 if (isl_set_foreach_basic_set(set
, &basic_guarded_fold
, bound
) < 0)
199 isl_qpolynomial_fold_free(fold
);
204 isl_qpolynomial_fold_free(fold
);
208 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_bound(
209 __isl_take isl_pw_qpolynomial_fold
*pwf
, int *tight
)
213 struct isl_bound bound
;
219 dim
= isl_pw_qpolynomial_fold_get_dim(pwf
);
220 nvar
= isl_dim_size(dim
, isl_dim_set
);
222 if (isl_dim_is_wrapping(dim
)) {
223 dim
= isl_dim_unwrap(dim
);
224 nvar
= isl_dim_size(dim
, isl_dim_out
);
225 dim
= isl_dim_domain(dim
);
227 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, nvar
);
232 return isl_pw_qpolynomial_fold_reset_dim(pwf
, dim
);
235 if (isl_pw_qpolynomial_fold_is_zero(pwf
)) {
236 enum isl_fold type
= pwf
->type
;
237 isl_pw_qpolynomial_fold_free(pwf
);
240 return isl_pw_qpolynomial_fold_zero(dim
, type
);
243 bound
.pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
), pwf
->type
);
244 bound
.pwf_tight
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
),
246 bound
.check_tight
= !!tight
;
248 if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf
,
249 guarded_fold
, &bound
) < 0)
252 covers
= isl_pw_qpolynomial_fold_covers(bound
.pwf_tight
, bound
.pwf
);
260 isl_pw_qpolynomial_fold_free(pwf
);
263 isl_pw_qpolynomial_fold_free(bound
.pwf
);
264 return bound
.pwf_tight
;
267 bound
.pwf
= isl_pw_qpolynomial_fold_fold(bound
.pwf
, bound
.pwf_tight
);
271 isl_pw_qpolynomial_fold_free(bound
.pwf_tight
);
272 isl_pw_qpolynomial_fold_free(bound
.pwf
);
273 isl_pw_qpolynomial_fold_free(pwf
);
278 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_bound(
279 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
, int *tight
)
281 isl_pw_qpolynomial_fold
*pwf
;
283 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(type
, pwqp
);
284 return isl_pw_qpolynomial_fold_bound(pwf
, tight
);
287 struct isl_union_bound_data
{
290 isl_union_pw_qpolynomial_fold
*res
;
293 static int bound_pw(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
295 struct isl_union_bound_data
*data
= user
;
296 isl_pw_qpolynomial_fold
*pwf
;
298 pwf
= isl_pw_qpolynomial_bound(pwqp
, data
->type
,
299 data
->tight
? &data
->tight
: NULL
);
300 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
306 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_bound(
307 __isl_take isl_union_pw_qpolynomial
*upwqp
,
308 enum isl_fold type
, int *tight
)
311 struct isl_union_bound_data data
= { type
, 1, NULL
};
319 dim
= isl_union_pw_qpolynomial_get_dim(upwqp
);
320 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
321 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
,
322 &bound_pw
, &data
) < 0)
325 isl_union_pw_qpolynomial_free(upwqp
);
331 isl_union_pw_qpolynomial_free(upwqp
);
332 isl_union_pw_qpolynomial_fold_free(data
.res
);