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>
16 /* Compute a bound on the polynomial defined over the parametric polytope
17 * using either range propagation or bernstein expansion and
18 * store the result in bound->pwf and bound->pwf_tight.
19 * Since bernstein expansion requires bounded domains, we apply
20 * range propagation on unbounded domains. Otherwise, we respect the choice
23 static int compressed_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
24 __isl_take isl_qpolynomial
*poly
, void *user
)
26 struct isl_bound
*bound
= (struct isl_bound
*)user
;
32 if (bset
->ctx
->opt
->bound
== ISL_BOUND_RANGE
)
33 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
35 bounded
= isl_basic_set_is_bounded(bset
);
39 return isl_qpolynomial_bound_on_domain_bernstein(bset
, poly
, bound
);
41 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
43 isl_basic_set_free(bset
);
44 isl_qpolynomial_free(poly
);
48 static int unwrapped_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
49 __isl_take isl_qpolynomial
*poly
, void *user
)
51 struct isl_bound
*bound
= (struct isl_bound
*)user
;
52 isl_pw_qpolynomial_fold
*top_pwf
;
53 isl_pw_qpolynomial_fold
*top_pwf_tight
;
56 unsigned orig_nvar
, final_nvar
;
59 bset
= isl_basic_set_detect_equalities(bset
);
65 return compressed_guarded_poly_bound(bset
, poly
, user
);
67 orig_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
69 morph
= isl_basic_set_full_compression(bset
);
71 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
72 poly
= isl_qpolynomial_morph(poly
, isl_morph_copy(morph
));
74 final_nvar
= isl_basic_set_dim(bset
, isl_dim_set
);
76 dim
= isl_morph_get_ran_dim(morph
);
77 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, isl_dim_size(dim
, isl_dim_set
));
80 top_pwf_tight
= bound
->pwf_tight
;
82 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
),
84 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(dim
, bound
->type
);
86 r
= compressed_guarded_poly_bound(bset
, poly
, user
);
88 morph
= isl_morph_remove_dom_dims(morph
, isl_dim_set
, 0, orig_nvar
);
89 morph
= isl_morph_remove_ran_dims(morph
, isl_dim_set
, 0, final_nvar
);
90 morph
= isl_morph_inverse(morph
);
92 bound
->pwf
= isl_pw_qpolynomial_fold_morph(bound
->pwf
,
93 isl_morph_copy(morph
));
94 bound
->pwf_tight
= isl_pw_qpolynomial_fold_morph(bound
->pwf_tight
, morph
);
96 bound
->pwf
= isl_pw_qpolynomial_fold_fold(top_pwf
, bound
->pwf
);
97 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(top_pwf_tight
,
102 isl_basic_set_free(bset
);
103 isl_qpolynomial_free(poly
);
107 static int guarded_poly_bound(__isl_take isl_basic_set
*bset
,
108 __isl_take isl_qpolynomial
*poly
, void *user
)
110 struct isl_bound
*bound
= (struct isl_bound
*)user
;
113 isl_pw_qpolynomial_fold
*top_pwf
;
114 isl_pw_qpolynomial_fold
*top_pwf_tight
;
119 if (!isl_basic_set_is_wrapping(bset
))
120 return unwrapped_guarded_poly_bound(bset
, poly
, user
);
122 target_dim
= isl_basic_set_get_dim(bset
);
123 target_dim
= isl_dim_unwrap(target_dim
);
124 target_dim
= isl_dim_domain(target_dim
);
126 nparam
= isl_dim_size(target_dim
, isl_dim_param
);
127 n_in
= isl_dim_size(target_dim
, isl_dim_set
);
129 bset
= isl_basic_set_move_dims(bset
, isl_dim_param
, nparam
,
130 isl_dim_set
, 0, n_in
);
131 poly
= isl_qpolynomial_move_dims(poly
, isl_dim_param
, nparam
,
132 isl_dim_set
, 0, n_in
);
134 dim
= isl_basic_set_get_dim(bset
);
135 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, isl_dim_size(dim
, isl_dim_set
));
137 top_pwf
= bound
->pwf
;
138 top_pwf_tight
= bound
->pwf_tight
;
140 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
),
142 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(dim
, bound
->type
);
144 r
= unwrapped_guarded_poly_bound(bset
, poly
, user
);
146 bound
->pwf
= isl_pw_qpolynomial_fold_reset_dim(bound
->pwf
,
147 isl_dim_copy(target_dim
));
148 bound
->pwf_tight
= isl_pw_qpolynomial_fold_reset_dim(bound
->pwf_tight
,
151 bound
->pwf
= isl_pw_qpolynomial_fold_fold(top_pwf
, bound
->pwf
);
152 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(top_pwf_tight
,
158 static int guarded_qp(__isl_take isl_qpolynomial
*qp
, void *user
)
160 struct isl_bound
*bound
= (struct isl_bound
*)user
;
163 r
= isl_qpolynomial_as_polynomial_on_domain(qp
, bound
->bset
,
164 &guarded_poly_bound
, user
);
165 isl_qpolynomial_free(qp
);
169 static int basic_guarded_fold(__isl_take isl_basic_set
*bset
, void *user
)
171 struct isl_bound
*bound
= (struct isl_bound
*)user
;
175 r
= isl_qpolynomial_fold_foreach_qpolynomial(bound
->fold
,
177 isl_basic_set_free(bset
);
181 static int guarded_fold(__isl_take isl_set
*set
,
182 __isl_take isl_qpolynomial_fold
*fold
, void *user
)
184 struct isl_bound
*bound
= (struct isl_bound
*)user
;
189 set
= isl_set_make_disjoint(set
);
192 bound
->type
= isl_qpolynomial_fold_get_type(fold
);
194 if (isl_set_foreach_basic_set(set
, &basic_guarded_fold
, bound
) < 0)
198 isl_qpolynomial_fold_free(fold
);
203 isl_qpolynomial_fold_free(fold
);
207 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_bound(
208 __isl_take isl_pw_qpolynomial_fold
*pwf
, int *tight
)
212 struct isl_bound bound
;
218 dim
= isl_pw_qpolynomial_fold_get_dim(pwf
);
219 nvar
= isl_dim_size(dim
, isl_dim_set
);
221 if (isl_dim_is_wrapping(dim
)) {
222 dim
= isl_dim_unwrap(dim
);
223 nvar
= isl_dim_size(dim
, isl_dim_out
);
224 dim
= isl_dim_domain(dim
);
226 dim
= isl_dim_drop(dim
, isl_dim_set
, 0, nvar
);
231 return isl_pw_qpolynomial_fold_reset_dim(pwf
, dim
);
234 if (isl_pw_qpolynomial_fold_is_zero(pwf
)) {
235 isl_pw_qpolynomial_fold_free(pwf
);
238 return isl_pw_qpolynomial_fold_zero(dim
, pwf
->type
);
241 bound
.pwf
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
), pwf
->type
);
242 bound
.pwf_tight
= isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim
),
244 bound
.check_tight
= !!tight
;
246 if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf
,
247 guarded_fold
, &bound
) < 0)
250 covers
= isl_pw_qpolynomial_fold_covers(bound
.pwf_tight
, bound
.pwf
);
258 isl_pw_qpolynomial_fold_free(pwf
);
261 isl_pw_qpolynomial_fold_free(bound
.pwf
);
262 return bound
.pwf_tight
;
265 bound
.pwf
= isl_pw_qpolynomial_fold_fold(bound
.pwf
, bound
.pwf_tight
);
269 isl_pw_qpolynomial_fold_free(bound
.pwf_tight
);
270 isl_pw_qpolynomial_fold_free(bound
.pwf
);
271 isl_pw_qpolynomial_fold_free(pwf
);
276 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_bound(
277 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
, int *tight
)
279 isl_pw_qpolynomial_fold
*pwf
;
281 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(type
, pwqp
);
282 return isl_pw_qpolynomial_fold_bound(pwf
, tight
);
285 struct isl_union_bound_data
{
288 isl_union_pw_qpolynomial_fold
*res
;
291 static int bound_pw(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
293 struct isl_union_bound_data
*data
= user
;
294 isl_pw_qpolynomial_fold
*pwf
;
296 pwf
= isl_pw_qpolynomial_bound(pwqp
, data
->type
,
297 data
->tight
? &data
->tight
: NULL
);
298 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
304 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_bound(
305 __isl_take isl_union_pw_qpolynomial
*upwqp
,
306 enum isl_fold type
, int *tight
)
309 struct isl_union_bound_data data
= { type
, 1, NULL
};
317 dim
= isl_union_pw_qpolynomial_get_dim(upwqp
);
318 data
.res
= isl_union_pw_qpolynomial_fold_zero(dim
, type
);
319 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
,
320 &bound_pw
, &data
) < 0)
323 isl_union_pw_qpolynomial_free(upwqp
);
329 isl_union_pw_qpolynomial_free(upwqp
);
330 isl_union_pw_qpolynomial_fold_free(data
.res
);