isl_tab: optionally only mark strictly redundant rows
[isl.git] / isl_bound.c
blob40e4d5e271447970ed9cf3c9cdb2c836a22ffe47
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #include <isl_bound.h>
12 #include <isl_range.h>
13 #include <isl_polynomial_private.h>
15 static int compressed_guarded_poly_bound(__isl_take isl_basic_set *bset,
16 __isl_take isl_qpolynomial *poly, void *user)
18 struct isl_bound *bound = (struct isl_bound *)user;
19 return isl_qpolynomial_bound_on_domain_range(bset, poly, bound);
22 static int guarded_poly_bound(__isl_take isl_basic_set *bset,
23 __isl_take isl_qpolynomial *poly, void *user)
25 struct isl_bound *bound = (struct isl_bound *)user;
26 isl_pw_qpolynomial_fold *top_pwf;
27 isl_pw_qpolynomial_fold *top_pwf_tight;
28 isl_dim *dim;
29 isl_morph *morph;
30 unsigned orig_nvar, final_nvar;
31 int r;
33 bset = isl_basic_set_detect_equalities(bset);
35 if (!bset)
36 goto error;
38 if (bset->n_eq == 0)
39 return compressed_guarded_poly_bound(bset, poly, user);
41 orig_nvar = isl_basic_set_dim(bset, isl_dim_set);
43 morph = isl_basic_set_full_compression(bset);
45 bset = isl_morph_basic_set(isl_morph_copy(morph), bset);
46 poly = isl_qpolynomial_morph(poly, isl_morph_copy(morph));
48 final_nvar = isl_basic_set_dim(bset, isl_dim_set);
50 dim = isl_morph_get_ran_dim(morph);
51 dim = isl_dim_drop(dim, isl_dim_set, 0, isl_dim_size(dim, isl_dim_set));
53 top_pwf = bound->pwf;
54 top_pwf_tight = bound->pwf_tight;
56 bound->pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim));
57 bound->pwf_tight = isl_pw_qpolynomial_fold_zero(dim);
59 r = compressed_guarded_poly_bound(bset, poly, user);
61 morph = isl_morph_remove_dom_dims(morph, isl_dim_set, 0, orig_nvar);
62 morph = isl_morph_remove_ran_dims(morph, isl_dim_set, 0, final_nvar);
63 morph = isl_morph_inverse(morph);
65 bound->pwf = isl_pw_qpolynomial_fold_morph(bound->pwf,
66 isl_morph_copy(morph));
67 bound->pwf_tight = isl_pw_qpolynomial_fold_morph(bound->pwf_tight, morph);
69 bound->pwf = isl_pw_qpolynomial_fold_add(top_pwf, bound->pwf);
70 bound->pwf_tight = isl_pw_qpolynomial_fold_add(top_pwf_tight,
71 bound->pwf_tight);
73 return r;
74 error:
75 isl_basic_set_free(bset);
76 isl_qpolynomial_free(poly);
77 return -1;
80 static int guarded_qp(__isl_take isl_qpolynomial *qp, void *user)
82 struct isl_bound *bound = (struct isl_bound *)user;
83 int r;
85 r = isl_qpolynomial_as_polynomial_on_domain(qp, bound->bset,
86 &guarded_poly_bound, user);
87 isl_qpolynomial_free(qp);
88 return r;
91 static int basic_guarded_fold(__isl_take isl_basic_set *bset, void *user)
93 struct isl_bound *bound = (struct isl_bound *)user;
94 int r;
96 bound->bset = bset;
97 r = isl_qpolynomial_fold_foreach_qpolynomial(bound->fold,
98 &guarded_qp, user);
99 isl_basic_set_free(bset);
100 return r;
103 static int guarded_fold(__isl_take isl_set *set,
104 __isl_take isl_qpolynomial_fold *fold, void *user)
106 struct isl_bound *bound = (struct isl_bound *)user;
108 if (!set || !fold)
109 goto error;
111 set = isl_set_make_disjoint(set);
113 bound->fold = fold;
114 bound->type = isl_qpolynomial_fold_get_type(fold);
116 if (isl_set_foreach_basic_set(set, &basic_guarded_fold, bound) < 0)
117 goto error;
119 isl_set_free(set);
120 isl_qpolynomial_fold_free(fold);
122 return 0;
123 error:
124 isl_set_free(set);
125 isl_qpolynomial_fold_free(fold);
126 return -1;
129 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_fold_bound(
130 __isl_take isl_pw_qpolynomial_fold *pwf, int *tight)
132 isl_dim *dim;
133 unsigned nvar;
134 struct isl_bound bound;
135 int covers;
137 if (!pwf)
138 return NULL;
140 dim = isl_pw_qpolynomial_fold_get_dim(pwf);
141 nvar = isl_dim_size(dim, isl_dim_set);
143 if (isl_pw_qpolynomial_fold_is_zero(pwf)) {
144 isl_pw_qpolynomial_fold_free(pwf);
145 dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
146 if (tight)
147 *tight = 1;
148 return isl_pw_qpolynomial_fold_zero(dim);
151 if (nvar == 0) {
152 isl_dim_free(dim);
153 if (tight)
154 *tight = 1;
155 return pwf;
158 dim = isl_dim_drop(dim, isl_dim_set, 0, nvar);
160 bound.pwf = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim));
161 bound.pwf_tight = isl_pw_qpolynomial_fold_zero(isl_dim_copy(dim));
162 bound.check_tight = !!tight;
164 if (isl_pw_qpolynomial_fold_foreach_lifted_piece(pwf,
165 guarded_fold, &bound) < 0)
166 goto error;
168 covers = isl_pw_qpolynomial_fold_covers(bound.pwf_tight, bound.pwf);
169 if (covers < 0)
170 goto error;
172 if (tight)
173 *tight = covers;
175 isl_dim_free(dim);
176 isl_pw_qpolynomial_fold_free(pwf);
178 if (covers) {
179 isl_pw_qpolynomial_fold_free(bound.pwf);
180 return bound.pwf_tight;
183 bound.pwf = isl_pw_qpolynomial_fold_add(bound.pwf, bound.pwf_tight);
185 return bound.pwf;
186 error:
187 isl_pw_qpolynomial_fold_free(bound.pwf_tight);
188 isl_pw_qpolynomial_fold_free(bound.pwf);
189 isl_pw_qpolynomial_fold_free(pwf);
190 isl_dim_free(dim);
191 return NULL;
194 __isl_give isl_pw_qpolynomial_fold *isl_pw_qpolynomial_bound(
195 __isl_take isl_pw_qpolynomial *pwqp, enum isl_fold type, int *tight)
197 isl_pw_qpolynomial_fold *pwf;
199 pwf = isl_pw_qpolynomial_fold_from_pw_qpolynomial(type, pwqp);
200 return isl_pw_qpolynomial_fold_bound(pwf, tight);