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 /* Compute a bound on the polynomial defined over the parametric polytope
20 * using either range propagation or bernstein expansion and
21 * store the result in bound->pwf and bound->pwf_tight.
22 * Since bernstein expansion requires bounded domains, we apply
23 * range propagation on unbounded domains. Otherwise, we respect the choice
26 static isl_stat
compressed_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
27 __isl_take isl_qpolynomial
*poly
, void *user
)
29 struct isl_bound
*bound
= (struct isl_bound
*)user
;
35 if (bset
->ctx
->opt
->bound
== ISL_BOUND_RANGE
)
36 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
38 bounded
= isl_basic_set_is_bounded(bset
);
42 return isl_qpolynomial_bound_on_domain_bernstein(bset
, poly
, bound
);
44 return isl_qpolynomial_bound_on_domain_range(bset
, poly
, bound
);
46 isl_basic_set_free(bset
);
47 isl_qpolynomial_free(poly
);
48 return isl_stat_error
;
51 static isl_stat
unwrapped_guarded_poly_bound(__isl_take isl_basic_set
*bset
,
52 __isl_take isl_qpolynomial
*poly
, void *user
)
54 struct isl_bound
*bound
= (struct isl_bound
*)user
;
55 isl_pw_qpolynomial_fold
*top_pwf
;
56 isl_pw_qpolynomial_fold
*top_pwf_tight
;
61 bset
= isl_basic_set_detect_equalities(bset
);
67 return compressed_guarded_poly_bound(bset
, poly
, user
);
69 morph
= isl_basic_set_full_compression(bset
);
71 bset
= isl_morph_basic_set(isl_morph_copy(morph
), bset
);
72 poly
= isl_qpolynomial_morph_domain(poly
, isl_morph_copy(morph
));
74 space
= isl_morph_get_ran_space(morph
);
75 space
= isl_space_params(space
);
78 top_pwf_tight
= bound
->pwf_tight
;
80 space
= isl_space_from_domain(space
);
81 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
82 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_space_copy(space
),
84 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(space
, bound
->type
);
86 r
= compressed_guarded_poly_bound(bset
, poly
, user
);
88 morph
= isl_morph_dom_params(morph
);
89 morph
= isl_morph_ran_params(morph
);
90 morph
= isl_morph_inverse(morph
);
92 bound
->pwf
= isl_pw_qpolynomial_fold_morph_domain(bound
->pwf
,
93 isl_morph_copy(morph
));
94 bound
->pwf_tight
= isl_pw_qpolynomial_fold_morph_domain(
95 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
);
105 return isl_stat_error
;
108 /* Update bound->pwf and bound->pwf_tight with a bound
109 * of type bound->type on the polynomial "poly" over the domain "bset".
111 * If the original problem had a wrapped relation in the domain,
112 * meaning that the bound should be computed over the range
113 * of this relation, then temporarily treat the domain dimensions
114 * of this wrapped relation as parameters, compute a bound
115 * in terms of these and the original parameters,
116 * turn the parameters back into set dimensions and
117 * add the results to bound->pwf and bound->pwf_tight.
119 * Note that even though "bset" is known to live in the same space
120 * as the domain of "poly", the names of the set dimensions
121 * may be different (or missing). Make sure the naming is exactly
122 * the same before turning these dimensions into parameters
123 * to ensure that the spaces are still the same after
126 static isl_stat
guarded_poly_bound(__isl_take isl_basic_set
*bset
,
127 __isl_take isl_qpolynomial
*poly
, void *user
)
129 struct isl_bound
*bound
= (struct isl_bound
*)user
;
131 isl_pw_qpolynomial_fold
*top_pwf
;
132 isl_pw_qpolynomial_fold
*top_pwf_tight
;
137 if (!bound
->wrapping
)
138 return unwrapped_guarded_poly_bound(bset
, poly
, user
);
140 nparam
= isl_space_dim(bound
->dim
, isl_dim_param
);
141 n_in
= isl_space_dim(bound
->dim
, isl_dim_in
);
142 if (nparam
< 0 || n_in
< 0)
145 space
= isl_qpolynomial_get_domain_space(poly
);
146 bset
= isl_basic_set_reset_space(bset
, space
);
148 bset
= isl_basic_set_move_dims(bset
, isl_dim_param
, nparam
,
149 isl_dim_set
, 0, n_in
);
150 poly
= isl_qpolynomial_move_dims(poly
, isl_dim_param
, nparam
,
151 isl_dim_in
, 0, n_in
);
153 space
= isl_basic_set_get_space(bset
);
154 space
= isl_space_params(space
);
156 top_pwf
= bound
->pwf
;
157 top_pwf_tight
= bound
->pwf_tight
;
159 space
= isl_space_from_domain(space
);
160 space
= isl_space_add_dims(space
, isl_dim_out
, 1);
161 bound
->pwf
= isl_pw_qpolynomial_fold_zero(isl_space_copy(space
),
163 bound
->pwf_tight
= isl_pw_qpolynomial_fold_zero(space
, bound
->type
);
165 r
= unwrapped_guarded_poly_bound(bset
, poly
, user
);
167 bound
->pwf
= isl_pw_qpolynomial_fold_reset_space(bound
->pwf
,
168 isl_space_copy(bound
->dim
));
169 bound
->pwf_tight
= isl_pw_qpolynomial_fold_reset_space(bound
->pwf_tight
,
170 isl_space_copy(bound
->dim
));
172 bound
->pwf
= isl_pw_qpolynomial_fold_fold(top_pwf
, bound
->pwf
);
173 bound
->pwf_tight
= isl_pw_qpolynomial_fold_fold(top_pwf_tight
,
178 isl_basic_set_free(bset
);
179 isl_qpolynomial_free(poly
);
180 return isl_stat_error
;
183 static isl_stat
guarded_qp(__isl_take isl_qpolynomial
*qp
, void *user
)
185 struct isl_bound
*bound
= (struct isl_bound
*)user
;
188 r
= isl_qpolynomial_as_polynomial_on_domain(qp
, bound
->bset
,
189 &guarded_poly_bound
, user
);
190 isl_qpolynomial_free(qp
);
194 static isl_stat
basic_guarded_fold(__isl_take isl_basic_set
*bset
, void *user
)
196 struct isl_bound
*bound
= (struct isl_bound
*)user
;
200 r
= isl_qpolynomial_fold_foreach_qpolynomial(bound
->fold
,
202 isl_basic_set_free(bset
);
206 static isl_stat
guarded_fold(__isl_take isl_set
*set
,
207 __isl_take isl_qpolynomial_fold
*fold
, void *user
)
209 struct isl_bound
*bound
= (struct isl_bound
*)user
;
214 set
= isl_set_make_disjoint(set
);
217 bound
->type
= isl_qpolynomial_fold_get_type(fold
);
219 if (isl_set_foreach_basic_set(set
, &basic_guarded_fold
, bound
) < 0)
223 isl_qpolynomial_fold_free(fold
);
228 isl_qpolynomial_fold_free(fold
);
229 return isl_stat_error
;
232 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_fold_bound(
233 __isl_take isl_pw_qpolynomial_fold
*pwf
, isl_bool
*tight
)
236 struct isl_bound bound
;
242 bound
.dim
= isl_pw_qpolynomial_fold_get_domain_space(pwf
);
244 bound
.wrapping
= isl_space_is_wrapping(bound
.dim
);
246 bound
.dim
= isl_space_unwrap(bound
.dim
);
247 nvar
= isl_space_dim(bound
.dim
, isl_dim_out
);
249 bound
.dim
= isl_space_free(bound
.dim
);
250 bound
.dim
= isl_space_domain(bound
.dim
);
251 bound
.dim
= isl_space_from_domain(bound
.dim
);
252 bound
.dim
= isl_space_add_dims(bound
.dim
, isl_dim_out
, 1);
256 *tight
= isl_bool_true
;
257 return isl_pw_qpolynomial_fold_reset_space(pwf
, bound
.dim
);
260 if (isl_pw_qpolynomial_fold_is_zero(pwf
)) {
261 enum isl_fold type
= pwf
->type
;
262 isl_pw_qpolynomial_fold_free(pwf
);
264 *tight
= isl_bool_true
;
265 return isl_pw_qpolynomial_fold_zero(bound
.dim
, type
);
268 bound
.pwf
= isl_pw_qpolynomial_fold_zero(isl_space_copy(bound
.dim
),
270 bound
.pwf_tight
= isl_pw_qpolynomial_fold_zero(isl_space_copy(bound
.dim
),
272 bound
.check_tight
= !!tight
;
274 if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf
,
275 guarded_fold
, &bound
) < 0)
278 covers
= isl_pw_qpolynomial_fold_covers(bound
.pwf_tight
, bound
.pwf
);
285 isl_space_free(bound
.dim
);
286 isl_pw_qpolynomial_fold_free(pwf
);
289 isl_pw_qpolynomial_fold_free(bound
.pwf
);
290 return bound
.pwf_tight
;
293 bound
.pwf
= isl_pw_qpolynomial_fold_fold(bound
.pwf
, bound
.pwf_tight
);
297 isl_pw_qpolynomial_fold_free(bound
.pwf_tight
);
298 isl_pw_qpolynomial_fold_free(bound
.pwf
);
299 isl_pw_qpolynomial_fold_free(pwf
);
300 isl_space_free(bound
.dim
);
304 __isl_give isl_pw_qpolynomial_fold
*isl_pw_qpolynomial_bound(
305 __isl_take isl_pw_qpolynomial
*pwqp
, enum isl_fold type
,
308 isl_pw_qpolynomial_fold
*pwf
;
310 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(type
, pwqp
);
311 return isl_pw_qpolynomial_fold_bound(pwf
, tight
);
314 struct isl_union_bound_data
{
317 isl_union_pw_qpolynomial_fold
*res
;
320 static isl_stat
bound_pw(__isl_take isl_pw_qpolynomial
*pwqp
, void *user
)
322 struct isl_union_bound_data
*data
= user
;
323 isl_pw_qpolynomial_fold
*pwf
;
325 pwf
= isl_pw_qpolynomial_bound(pwqp
, data
->type
,
326 data
->tight
? &data
->tight
: NULL
);
327 data
->res
= isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
333 __isl_give isl_union_pw_qpolynomial_fold
*isl_union_pw_qpolynomial_bound(
334 __isl_take isl_union_pw_qpolynomial
*upwqp
,
335 enum isl_fold type
, isl_bool
*tight
)
338 struct isl_union_bound_data data
= { type
, 1, NULL
};
344 data
.tight
= isl_bool_false
;
346 space
= isl_union_pw_qpolynomial_get_space(upwqp
);
347 data
.res
= isl_union_pw_qpolynomial_fold_zero(space
, type
);
348 if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp
,
349 &bound_pw
, &data
) < 0)
352 isl_union_pw_qpolynomial_free(upwqp
);
358 isl_union_pw_qpolynomial_free(upwqp
);
359 isl_union_pw_qpolynomial_fold_free(data
.res
);