2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT 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_ctx_private.h>
12 #include <isl_map_private.h>
13 #include <isl_bound.h>
14 #include <isl_bernstein.h>
15 #include <isl_range.h>
16 #include <isl_polynomial_private.h>
17 #include <isl_options_private.h>
19 /* Add the bound "pwf", which is not known to be tight,
20 * to the output of "bound".
22 isl_stat
isl_bound_add(struct isl_bound
*bound
,
23 __isl_take isl_pw_qpolynomial_fold
*pwf
)
25 bound
->pwf
= isl_pw_qpolynomial_fold_fold(bound
->pwf
, pwf
);
26 return isl_stat_non_null(bound
->pwf
);
29 /* Add the bound "pwf", which is known to be tight,
30 * to the output of "bound".
32 isl_stat
isl_bound_add_tight(struct isl_bound
*bound
,
33 __isl_take isl_pw_qpolynomial_fold
*pwf
)
35 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(bound
->pwf_tight
, pwf
);
36 return isl_stat_non_null(bound
->pwf
);
39 /* Compute a bound on the polynomial defined over the parametric polytope
40 * using either range propagation or bernstein expansion and
41 * store the result in bound->pwf and bound->pwf_tight.
42 * Since bernstein expansion requires bounded domains, we apply
43 * range propagation on unbounded domains. Otherwise, we respect the choice
46 static isl_stat
compressed_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
47 __isl_take isl_qpolynomial
*poly
, void *user
)
49 struct isl_bound
*bound
= (struct isl_bound
*)user
;
56 ctx
= isl_basic_set_get_ctx(bset
);
57 if (ctx
->opt
->bound
== ISL_BOUND_RANGE
)
58 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
60 bounded
= isl_basic_set_is_bounded(bset
);
64 return isl_qpolynomial_bound_on_domain_bernstein(bset
, poly
, bound
);
66 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
68 isl_basic_set_free(bset
);
69 isl_qpolynomial_free(poly
);
70 return isl_stat_error
;
73 static isl_stat
unwrapped_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
74 __isl_take isl_qpolynomial
*poly
, void *user
)
76 struct isl_bound
*bound
= (struct isl_bound
*)user
;
77 isl_pw_qpolynomial_fold
*top_pwf
;
78 isl_pw_qpolynomial_fold
*top_pwf_tight
;
83 bset
= isl_basic_set_detect_equalities(bset
);
89 return compressed_guarded_poly_bound(bset
, poly
, user
);
91 morph
= isl_basic_set_full_compression(bset
);
93 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
94 poly
= isl_qpolynomial_morph_domain(poly
, isl_morph_copy(morph
));
96 space
= isl_morph_get_ran_space(morph
);
97 space
= isl_space_params(space
);
100 top_pwf_tight
= bound
->pwf_tight
;
102 space
= isl_space_from_domain(space
);
103 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
104 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_space_copy(space
),
106 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(space
, bound
->type
);
108 r
= compressed_guarded_poly_bound(bset
, poly
, user
);
110 morph
= isl_morph_dom_params(morph
);
111 morph
= isl_morph_ran_params(morph
);
112 morph
= isl_morph_inverse(morph
);
114 bound
->pwf
= isl_pw_qpolynomial_fold_morph_domain(bound
->pwf
,
115 isl_morph_copy(morph
));
116 bound
->pwf_tight
= isl_pw_qpolynomial_fold_morph_domain(
117 bound
->pwf_tight
, morph
);
119 isl_bound_add(bound
, top_pwf
);
120 isl_bound_add_tight(bound
, top_pwf_tight
);
124 isl_basic_set_free(bset
);
125 isl_qpolynomial_free(poly
);
126 return isl_stat_error
;
129 /* Update bound->pwf and bound->pwf_tight with a bound
130 * of type bound->type on the polynomial "poly" over the domain "bset".
132 * If the original problem had a wrapped relation in the domain,
133 * meaning that the bound should be computed over the range
134 * of this relation, then temporarily treat the domain dimensions
135 * of this wrapped relation as parameters, compute a bound
136 * in terms of these and the original parameters,
137 * turn the parameters back into set dimensions and
138 * add the results to bound->pwf and bound->pwf_tight.
140 * Note that even though "bset" is known to live in the same space
141 * as the domain of "poly", the names of the set dimensions
142 * may be different (or missing). Make sure the naming is exactly
143 * the same before turning these dimensions into parameters
144 * to ensure that the spaces are still the same after
147 static isl_stat
guarded_poly_bound(__isl_take isl_basic_set
*bset
,
148 __isl_take isl_qpolynomial
*poly
, void *user
)
150 struct isl_bound
*bound
= (struct isl_bound
*)user
;
152 isl_pw_qpolynomial_fold
*top_pwf
;
153 isl_pw_qpolynomial_fold
*top_pwf_tight
;
158 if (!bound
->wrapping
)
159 return unwrapped_guarded_poly_bound(bset
, poly
, user
);
161 nparam
= isl_space_dim(bound
->dim
, isl_dim_param
);
162 n_in
= isl_space_dim(bound
->dim
, isl_dim_in
);
163 if (nparam
< 0 || n_in
< 0)
166 space
= isl_qpolynomial_get_domain_space(poly
);
167 bset
= isl_basic_set_reset_space(bset
, space
);
169 bset
= isl_basic_set_move_dims(bset
, isl_dim_param
, nparam
,
170 isl_dim_set
, 0, n_in
);
171 poly
= isl_qpolynomial_move_dims(poly
, isl_dim_param
, nparam
,
172 isl_dim_in
, 0, n_in
);
174 space
= isl_basic_set_get_space(bset
);
175 space
= isl_space_params(space
);
177 top_pwf
= bound
->pwf
;
178 top_pwf_tight
= bound
->pwf_tight
;
180 space
= isl_space_from_domain(space
);
181 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
182 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_space_copy(space
),
184 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(space
, bound
->type
);
186 r
= unwrapped_guarded_poly_bound(bset
, poly
, user
);
188 bound
->pwf
= isl_pw_qpolynomial_fold_reset_space(bound
->pwf
,
189 isl_space_copy(bound
->dim
));
190 bound
->pwf_tight
= isl_pw_qpolynomial_fold_reset_space(bound
->pwf_tight
,
191 isl_space_copy(bound
->dim
));
193 isl_bound_add(bound
, top_pwf
);
194 isl_bound_add_tight(bound
, top_pwf_tight
);
198 isl_basic_set_free(bset
);
199 isl_qpolynomial_free(poly
);
200 return isl_stat_error
;
203 static isl_stat
guarded_qp(__isl_take isl_qpolynomial
*qp
, void *user
)
205 struct isl_bound
*bound
= (struct isl_bound
*)user
;
208 r
= isl_qpolynomial_as_polynomial_on_domain(qp
, bound
->bset
,
209 &guarded_poly_bound
, user
);
210 isl_qpolynomial_free(qp
);
214 static isl_stat
basic_guarded_fold(__isl_take isl_basic_set
*bset
, void *user
)
216 struct isl_bound
*bound
= (struct isl_bound
*)user
;
220 r
= isl_qpolynomial_fold_foreach_qpolynomial(bound
->fold
,
222 isl_basic_set_free(bset
);
226 static isl_stat
guarded_fold(__isl_take isl_set
*set
,
227 __isl_take isl_qpolynomial_fold
*fold
, void *user
)
229 struct isl_bound
*bound
= (struct isl_bound
*)user
;
234 set
= isl_set_make_disjoint(set
);
237 bound
->type
= isl_qpolynomial_fold_get_type(fold
);
239 if (isl_set_foreach_basic_set(set
, &basic_guarded_fold
, bound
) < 0)
243 isl_qpolynomial_fold_free(fold
);
248 isl_qpolynomial_fold_free(fold
);
249 return isl_stat_error
;
252 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_bound(
253 __isl_take isl_pw_qpolynomial_fold
*pwf
, isl_bool
*tight
)
256 struct isl_bound bound
;
262 bound
.dim
= isl_pw_qpolynomial_fold_get_domain_space(pwf
);
264 bound
.wrapping
= isl_space_is_wrapping(bound
.dim
);
266 bound
.dim
= isl_space_unwrap(bound
.dim
);
267 nvar
= isl_space_dim(bound
.dim
, isl_dim_out
);
269 bound
.dim
= isl_space_free(bound
.dim
);
270 bound
.dim
= isl_space_domain(bound
.dim
);
271 bound
.dim
= isl_space_from_domain(bound
.dim
);
272 bound
.dim
= isl_space_add_dims(bound
.dim
, isl_dim_out
, 1);
276 *tight
= isl_bool_true
;
277 return isl_pw_qpolynomial_fold_reset_space(pwf
, bound
.dim
);
280 if (isl_pw_qpolynomial_fold_is_zero(pwf
)) {
281 enum isl_fold type
= pwf
->type
;
282 isl_pw_qpolynomial_fold_free(pwf
);
284 *tight
= isl_bool_true
;
285 return isl_pw_qpolynomial_fold_zero(bound
.dim
, type
);
288 bound
.pwf
= isl_pw_qpolynomial_fold_zero(isl_space_copy(bound
.dim
),
290 bound
.pwf_tight
= isl_pw_qpolynomial_fold_zero(isl_space_copy(bound
.dim
),
292 bound
.check_tight
= !!tight
;
294 if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf
,
295 guarded_fold
, &bound
) < 0)
298 covers
= isl_pw_qpolynomial_fold_covers(bound
.pwf_tight
, bound
.pwf
);
305 isl_space_free(bound
.dim
);
306 isl_pw_qpolynomial_fold_free(pwf
);
309 isl_pw_qpolynomial_fold_free(bound
.pwf
);
310 return bound
.pwf_tight
;
313 bound
.pwf
= isl_pw_qpolynomial_fold_fold(bound
.pwf
, bound
.pwf_tight
);
317 isl_pw_qpolynomial_fold_free(bound
.pwf_tight
);
318 isl_pw_qpolynomial_fold_free(bound
.pwf
);
319 isl_pw_qpolynomial_fold_free(pwf
);
320 isl_space_free(bound
.dim
);
324 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_bound(
325 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
,
328 isl_pw_qpolynomial_fold
*pwf
;
330 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(type
, pwqp
);
331 return isl_pw_qpolynomial_fold_bound(pwf
, tight
);
334 struct isl_union_bound_data
{
337 isl_union_pw_qpolynomial_fold
*res
;
340 static isl_stat
bound_pw(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
342 struct isl_union_bound_data
*data
= user
;
343 isl_pw_qpolynomial_fold
*pwf
;
345 pwf
= isl_pw_qpolynomial_bound(pwqp
, data
->type
,
346 data
->tight
? &data
->tight
: NULL
);
347 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
353 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_bound(
354 __isl_take isl_union_pw_qpolynomial
*upwqp
,
355 enum isl_fold type
, isl_bool
*tight
)
358 struct isl_union_bound_data data
= { type
, 1, NULL
};
364 data
.tight
= isl_bool_false
;
366 space
= isl_union_pw_qpolynomial_get_space(upwqp
);
367 data
.res
= isl_union_pw_qpolynomial_fold_zero(space
, type
);
368 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
,
369 &bound_pw
, &data
) < 0)
372 isl_union_pw_qpolynomial_free(upwqp
);
378 isl_union_pw_qpolynomial_free(upwqp
);
379 isl_union_pw_qpolynomial_fold_free(data
.res
);