isl_flow.c: compute_val_based_dependences: use correct space for partial results
[isl.git] / isl_scan.c
blobeb8423c6e35364caef506b68509e98fd107d94fe
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include "isl_basis_reduction.h"
13 #include "isl_scan.h"
14 #include <isl_seq.h>
15 #include "isl_tab.h"
16 #include <isl_val_private.h>
17 #include <isl_vec_private.h>
19 struct isl_counter {
20 struct isl_scan_callback callback;
21 isl_int count;
22 isl_int max;
25 static int increment_counter(struct isl_scan_callback *cb,
26 __isl_take isl_vec *sample)
28 struct isl_counter *cnt = (struct isl_counter *)cb;
30 isl_int_add_ui(cnt->count, cnt->count, 1);
32 isl_vec_free(sample);
34 if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
35 return 0;
36 return -1;
39 static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max)
41 struct isl_counter *cnt = (struct isl_counter *)cb;
43 isl_int_add(cnt->count, cnt->count, max);
44 isl_int_sub(cnt->count, cnt->count, min);
45 isl_int_add_ui(cnt->count, cnt->count, 1);
47 if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max))
48 return 0;
49 isl_int_set(cnt->count, cnt->max);
50 return -1;
53 /* Call callback->add with the current sample value of the tableau "tab".
55 static int add_solution(struct isl_tab *tab, struct isl_scan_callback *callback)
57 struct isl_vec *sample;
59 if (!tab)
60 return -1;
61 sample = isl_tab_get_sample_value(tab);
62 if (!sample)
63 return -1;
65 return callback->add(callback, sample);
68 static int scan_0D(struct isl_basic_set *bset,
69 struct isl_scan_callback *callback)
71 struct isl_vec *sample;
73 sample = isl_vec_alloc(bset->ctx, 1);
74 isl_basic_set_free(bset);
76 if (!sample)
77 return -1;
79 isl_int_set_si(sample->el[0], 1);
81 return callback->add(callback, sample);
84 /* Look for all integer points in "bset", which is assumed to be bounded,
85 * and call callback->add on each of them.
87 * We first compute a reduced basis for the set and then scan
88 * the set in the directions of this basis.
89 * We basically perform a depth first search, where in each level i
90 * we compute the range in the i-th basis vector direction, given
91 * fixed values in the directions of the previous basis vector.
92 * We then add an equality to the tableau fixing the value in the
93 * direction of the current basis vector to each value in the range
94 * in turn and then continue to the next level.
96 * The search is implemented iteratively. "level" identifies the current
97 * basis vector. "init" is true if we want the first value at the current
98 * level and false if we want the next value.
99 * Solutions are added in the leaves of the search tree, i.e., after
100 * we have fixed a value in each direction of the basis.
102 int isl_basic_set_scan(struct isl_basic_set *bset,
103 struct isl_scan_callback *callback)
105 unsigned dim;
106 struct isl_mat *B = NULL;
107 struct isl_tab *tab = NULL;
108 struct isl_vec *min;
109 struct isl_vec *max;
110 struct isl_tab_undo **snap;
111 int level;
112 int init;
113 enum isl_lp_result res;
115 if (!bset)
116 return -1;
118 dim = isl_basic_set_total_dim(bset);
119 if (dim == 0)
120 return scan_0D(bset, callback);
122 min = isl_vec_alloc(bset->ctx, dim);
123 max = isl_vec_alloc(bset->ctx, dim);
124 snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);
126 if (!min || !max || !snap)
127 goto error;
129 tab = isl_tab_from_basic_set(bset, 0);
130 if (!tab)
131 goto error;
132 if (isl_tab_extend_cons(tab, dim + 1) < 0)
133 goto error;
135 tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
136 if (1)
137 tab = isl_tab_compute_reduced_basis(tab);
138 if (!tab)
139 goto error;
140 B = isl_mat_copy(tab->basis);
141 if (!B)
142 goto error;
144 level = 0;
145 init = 1;
147 while (level >= 0) {
148 int empty = 0;
149 if (init) {
150 res = isl_tab_min(tab, B->row[1 + level],
151 bset->ctx->one, &min->el[level], NULL, 0);
152 if (res == isl_lp_empty)
153 empty = 1;
154 if (res == isl_lp_error || res == isl_lp_unbounded)
155 goto error;
156 isl_seq_neg(B->row[1 + level] + 1,
157 B->row[1 + level] + 1, dim);
158 res = isl_tab_min(tab, B->row[1 + level],
159 bset->ctx->one, &max->el[level], NULL, 0);
160 isl_seq_neg(B->row[1 + level] + 1,
161 B->row[1 + level] + 1, dim);
162 isl_int_neg(max->el[level], max->el[level]);
163 if (res == isl_lp_empty)
164 empty = 1;
165 if (res == isl_lp_error || res == isl_lp_unbounded)
166 goto error;
167 snap[level] = isl_tab_snap(tab);
168 } else
169 isl_int_add_ui(min->el[level], min->el[level], 1);
171 if (empty || isl_int_gt(min->el[level], max->el[level])) {
172 level--;
173 init = 0;
174 if (level >= 0)
175 if (isl_tab_rollback(tab, snap[level]) < 0)
176 goto error;
177 continue;
179 if (level == dim - 1 && callback->add == increment_counter) {
180 if (increment_range(callback,
181 min->el[level], max->el[level]))
182 goto error;
183 level--;
184 init = 0;
185 if (level >= 0)
186 if (isl_tab_rollback(tab, snap[level]) < 0)
187 goto error;
188 continue;
190 isl_int_neg(B->row[1 + level][0], min->el[level]);
191 if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
192 goto error;
193 isl_int_set_si(B->row[1 + level][0], 0);
194 if (level < dim - 1) {
195 ++level;
196 init = 1;
197 continue;
199 if (add_solution(tab, callback) < 0)
200 goto error;
201 init = 0;
202 if (isl_tab_rollback(tab, snap[level]) < 0)
203 goto error;
206 isl_tab_free(tab);
207 free(snap);
208 isl_vec_free(min);
209 isl_vec_free(max);
210 isl_basic_set_free(bset);
211 isl_mat_free(B);
212 return 0;
213 error:
214 isl_tab_free(tab);
215 free(snap);
216 isl_vec_free(min);
217 isl_vec_free(max);
218 isl_basic_set_free(bset);
219 isl_mat_free(B);
220 return -1;
223 int isl_set_scan(__isl_take isl_set *set, struct isl_scan_callback *callback)
225 int i;
227 if (!set || !callback)
228 goto error;
230 set = isl_set_cow(set);
231 set = isl_set_make_disjoint(set);
232 set = isl_set_compute_divs(set);
233 if (!set)
234 goto error;
236 for (i = 0; i < set->n; ++i)
237 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
238 callback) < 0)
239 goto error;
241 isl_set_free(set);
242 return 0;
243 error:
244 isl_set_free(set);
245 return -1;
248 int isl_basic_set_count_upto(__isl_keep isl_basic_set *bset,
249 isl_int max, isl_int *count)
251 struct isl_counter cnt = { { &increment_counter } };
253 if (!bset)
254 return -1;
256 isl_int_init(cnt.count);
257 isl_int_init(cnt.max);
259 isl_int_set_si(cnt.count, 0);
260 isl_int_set(cnt.max, max);
261 if (isl_basic_set_scan(isl_basic_set_copy(bset), &cnt.callback) < 0 &&
262 isl_int_lt(cnt.count, cnt.max))
263 goto error;
265 isl_int_set(*count, cnt.count);
266 isl_int_clear(cnt.max);
267 isl_int_clear(cnt.count);
269 return 0;
270 error:
271 isl_int_clear(cnt.count);
272 return -1;
275 int isl_set_count_upto(__isl_keep isl_set *set, isl_int max, isl_int *count)
277 struct isl_counter cnt = { { &increment_counter } };
279 if (!set)
280 return -1;
282 isl_int_init(cnt.count);
283 isl_int_init(cnt.max);
285 isl_int_set_si(cnt.count, 0);
286 isl_int_set(cnt.max, max);
287 if (isl_set_scan(isl_set_copy(set), &cnt.callback) < 0 &&
288 isl_int_lt(cnt.count, cnt.max))
289 goto error;
291 isl_int_set(*count, cnt.count);
292 isl_int_clear(cnt.max);
293 isl_int_clear(cnt.count);
295 return 0;
296 error:
297 isl_int_clear(cnt.count);
298 return -1;
301 int isl_set_count(__isl_keep isl_set *set, isl_int *count)
303 if (!set)
304 return -1;
305 return isl_set_count_upto(set, set->ctx->zero, count);
308 /* Count the total number of elements in "set" (in an inefficient way) and
309 * return the result.
311 __isl_give isl_val *isl_set_count_val(__isl_keep isl_set *set)
313 isl_val *v;
315 if (!set)
316 return NULL;
317 v = isl_val_zero(isl_set_get_ctx(set));
318 v = isl_val_cow(v);
319 if (!v)
320 return NULL;
321 if (isl_set_count(set, &v->n) < 0)
322 v = isl_val_free(v);
323 return v;