Merge branch 'maint'
[platform/upstream/isl.git] / isl_bound.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
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
18 /* Compute a bound on the polynomial defined over the parametric polytope
19  * using either range propagation or bernstein expansion and
20  * store the result in bound->pwf and bound->pwf_tight.
21  * Since bernstein expansion requires bounded domains, we apply
22  * range propagation on unbounded domains.  Otherwise, we respect the choice
23  * of the user.
24  */
25 static int compressed_guarded_poly_bound(__isl_take isl_basic_set *bset,
26         __isl_take isl_qpolynomial *poly, void *user)
27 {
28         struct isl_bound *bound = (struct isl_bound *)user;
29         int bounded;
30
31         if (!bset || !poly)
32                 goto error;
33
34         if (bset->ctx->opt->bound == ISL_BOUND_RANGE)
35                 return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
36
37         bounded = isl_basic_set_is_bounded(bset);
38         if (bounded < 0)
39                 goto error;
40         if (bounded)
41                 return isl_qpolynomial_bound_on_domain_bernstein(bset, poly, bound);
42         else
43                 return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
44 error:
45         isl_basic_set_free(bset);
46         isl_qpolynomial_free(poly);
47         return -1;
48 }
49
50 static int unwrapped_guarded_poly_bound(__isl_take isl_basic_set *bset,
51         __isl_take isl_qpolynomial *poly, void *user)
52 {
53         struct isl_bound *bound = (struct isl_bound *)user;
54         isl_pw_qpolynomial_fold *top_pwf;
55         isl_pw_qpolynomial_fold *top_pwf_tight;
56         isl_space *dim;
57         isl_morph *morph;
58         int r;
59
60         bset = isl_basic_set_detect_equalities(bset);
61
62         if (!bset)
63                 goto error;
64
65         if (bset->n_eq == 0)
66                 return compressed_guarded_poly_bound(bset, poly, user);
67
68         morph = isl_basic_set_full_compression(bset);
69
70         bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
71         poly = isl_qpolynomial_morph_domain(poly, isl_morph_copy(morph));
72
73         dim = isl_morph_get_ran_space(morph);
74         dim = isl_space_params(dim);
75
76         top_pwf = bound->pwf;
77         top_pwf_tight = bound->pwf_tight;
78
79         dim = isl_space_from_domain(dim);
80         dim = isl_space_add_dims(dim, isl_dim_out, 1);
81         bound->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(dim),
82                                                   bound->type);
83         bound->pwf_tight = isl_pw_qpolynomial_fold_zero(dim, bound->type);
84
85         r = compressed_guarded_poly_bound(bset, poly, user);
86
87         morph = isl_morph_dom_params(morph);
88         morph = isl_morph_ran_params(morph);
89         morph = isl_morph_inverse(morph);
90
91         bound->pwf = isl_pw_qpolynomial_fold_morph_domain(bound->pwf,
92                                                         isl_morph_copy(morph));
93         bound->pwf_tight = isl_pw_qpolynomial_fold_morph_domain(
94                                                 bound->pwf_tight, morph);
95
96         bound->pwf = isl_pw_qpolynomial_fold_fold(top_pwf, bound->pwf);
97         bound->pwf_tight = isl_pw_qpolynomial_fold_fold(top_pwf_tight,
98                                                         bound->pwf_tight);
99
100         return r;
101 error:
102         isl_basic_set_free(bset);
103         isl_qpolynomial_free(poly);
104         return -1;
105 }
106
107 static int guarded_poly_bound(__isl_take isl_basic_set *bset,
108         __isl_take isl_qpolynomial *poly, void *user)
109 {
110         struct isl_bound *bound = (struct isl_bound *)user;
111         isl_space *dim;
112         isl_pw_qpolynomial_fold *top_pwf;
113         isl_pw_qpolynomial_fold *top_pwf_tight;
114         int nparam;
115         int n_in;
116         int r;
117
118         if (!bound->wrapping)
119                 return unwrapped_guarded_poly_bound(bset, poly, user);
120
121         nparam = isl_space_dim(bound->dim, isl_dim_param);
122         n_in = isl_space_dim(bound->dim, isl_dim_in);
123
124         bset = isl_basic_set_move_dims(bset, isl_dim_param, nparam,
125                                         isl_dim_set, 0, n_in);
126         poly = isl_qpolynomial_move_dims(poly, isl_dim_param, nparam,
127                                         isl_dim_in, 0, n_in);
128
129         dim = isl_basic_set_get_space(bset);
130         dim = isl_space_params(dim);
131
132         top_pwf = bound->pwf;
133         top_pwf_tight = bound->pwf_tight;
134
135         dim = isl_space_from_domain(dim);
136         dim = isl_space_add_dims(dim, isl_dim_out, 1);
137         bound->pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(dim),
138                                                   bound->type);
139         bound->pwf_tight = isl_pw_qpolynomial_fold_zero(dim, bound->type);
140
141         r = unwrapped_guarded_poly_bound(bset, poly, user);
142
143         bound->pwf = isl_pw_qpolynomial_fold_reset_space(bound->pwf,
144                                                     isl_space_copy(bound->dim));
145         bound->pwf_tight = isl_pw_qpolynomial_fold_reset_space(bound->pwf_tight,
146                                                     isl_space_copy(bound->dim));
147
148         bound->pwf = isl_pw_qpolynomial_fold_fold(top_pwf, bound->pwf);
149         bound->pwf_tight = isl_pw_qpolynomial_fold_fold(top_pwf_tight,
150                                                         bound->pwf_tight);
151
152         return r;
153 }
154
155 static int guarded_qp(__isl_take isl_qpolynomial *qp, void *user)
156 {
157         struct isl_bound *bound = (struct isl_bound *)user;
158         int r;
159
160         r = isl_qpolynomial_as_polynomial_on_domain(qp, bound->bset,
161                                                     &guarded_poly_bound, user);
162         isl_qpolynomial_free(qp);
163         return r;
164 }
165
166 static int basic_guarded_fold(__isl_take isl_basic_set *bset, void *user)
167 {
168         struct isl_bound *bound = (struct isl_bound *)user;
169         int r;
170
171         bound->bset = bset;
172         r = isl_qpolynomial_fold_foreach_qpolynomial(bound->fold,
173                                                         &guarded_qp, user);
174         isl_basic_set_free(bset);
175         return r;
176 }
177
178 static int guarded_fold(__isl_take isl_set *set,
179         __isl_take isl_qpolynomial_fold *fold, void *user)
180 {
181         struct isl_bound *bound = (struct isl_bound *)user;
182
183         if (!set || !fold)
184                 goto error;
185
186         set = isl_set_make_disjoint(set);
187
188         bound->fold = fold;
189         bound->type = isl_qpolynomial_fold_get_type(fold);
190
191         if (isl_set_foreach_basic_set(set, &basic_guarded_fold, bound) < 0)
192                 goto error;
193
194         isl_set_free(set);
195         isl_qpolynomial_fold_free(fold);
196
197         return 0;
198 error:
199         isl_set_free(set);
200         isl_qpolynomial_fold_free(fold);
201         return -1;
202 }
203
204 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_bound(
205         __isl_take isl_pw_qpolynomial_fold *pwf, int *tight)
206 {
207         unsigned nvar;
208         struct isl_bound bound;
209         int covers;
210
211         if (!pwf)
212                 return NULL;
213
214         bound.dim = isl_pw_qpolynomial_fold_get_domain_space(pwf);
215
216         bound.wrapping = isl_space_is_wrapping(bound.dim);
217         if (bound.wrapping)
218                 bound.dim = isl_space_unwrap(bound.dim);
219         nvar = isl_space_dim(bound.dim, isl_dim_out);
220         bound.dim = isl_space_domain(bound.dim);
221         bound.dim = isl_space_from_domain(bound.dim);
222         bound.dim = isl_space_add_dims(bound.dim, isl_dim_out, 1);
223
224         if (nvar == 0) {
225                 if (tight)
226                         *tight = 1;
227                 return isl_pw_qpolynomial_fold_reset_space(pwf, bound.dim);
228         }
229
230         if (isl_pw_qpolynomial_fold_is_zero(pwf)) {
231                 enum isl_fold type = pwf->type;
232                 isl_pw_qpolynomial_fold_free(pwf);
233                 if (tight)
234                         *tight = 1;
235                 return isl_pw_qpolynomial_fold_zero(bound.dim, type);
236         }
237
238         bound.pwf = isl_pw_qpolynomial_fold_zero(isl_space_copy(bound.dim),
239                                                         pwf->type);
240         bound.pwf_tight = isl_pw_qpolynomial_fold_zero(isl_space_copy(bound.dim),
241                                                         pwf->type);
242         bound.check_tight = !!tight;
243
244         if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf,
245                                                         guarded_fold, &bound) < 0)
246                 goto error;
247
248         covers = isl_pw_qpolynomial_fold_covers(bound.pwf_tight, bound.pwf);
249         if (covers < 0)
250                 goto error;
251
252         if (tight)
253                 *tight = covers;
254
255         isl_space_free(bound.dim);
256         isl_pw_qpolynomial_fold_free(pwf);
257
258         if (covers) {
259                 isl_pw_qpolynomial_fold_free(bound.pwf);
260                 return bound.pwf_tight;
261         }
262
263         bound.pwf = isl_pw_qpolynomial_fold_fold(bound.pwf, bound.pwf_tight);
264
265         return bound.pwf;
266 error:
267         isl_pw_qpolynomial_fold_free(bound.pwf_tight);
268         isl_pw_qpolynomial_fold_free(bound.pwf);
269         isl_pw_qpolynomial_fold_free(pwf);
270         isl_space_free(bound.dim);
271         return NULL;
272 }
273
274 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_bound(
275         __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type, int *tight)
276 {
277         isl_pw_qpolynomial_fold *pwf;
278
279         pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(type, pwqp);
280         return isl_pw_qpolynomial_fold_bound(pwf, tight);
281 }
282
283 struct isl_union_bound_data {
284         enum isl_fold type;
285         int tight;
286         isl_union_pw_qpolynomial_fold *res;
287 };
288
289 static int bound_pw(__isl_take isl_pw_qpolynomial *pwqp, void *user)
290 {
291         struct isl_union_bound_data *data = user;
292         isl_pw_qpolynomial_fold *pwf;
293
294         pwf = isl_pw_qpolynomial_bound(pwqp, data->type,
295                                         data->tight ? &data->tight : NULL);
296         data->res = isl_union_pw_qpolynomial_fold_fold_pw_qpolynomial_fold(
297                                                                 data->res, pwf);
298
299         return 0;
300 }
301
302 __isl_give isl_union_pw_qpolynomial_fold *isl_union_pw_qpolynomial_bound(
303         __isl_take isl_union_pw_qpolynomial *upwqp,
304         enum isl_fold type, int *tight)
305 {
306         isl_space *dim;
307         struct isl_union_bound_data data = { type, 1, NULL };
308
309         if (!upwqp)
310                 return NULL;
311
312         if (!tight)
313                 data.tight = 0;
314
315         dim = isl_union_pw_qpolynomial_get_space(upwqp);
316         data.res = isl_union_pw_qpolynomial_fold_zero(dim, type);
317         if (isl_union_pw_qpolynomial_foreach_pw_qpolynomial(upwqp,
318                                                     &bound_pw, &data) < 0)
319                 goto error;
320
321         isl_union_pw_qpolynomial_free(upwqp);
322         if (tight)
323                 *tight = data.tight;
324
325         return data.res;
326 error:
327         isl_union_pw_qpolynomial_free(upwqp);
328         isl_union_pw_qpolynomial_fold_free(data.res);
329         return NULL;
330 }