6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
8 #include "isl_sample.h"
11 struct isl_basic_map *isl_basic_map_implicit_equalities(
12 struct isl_basic_map *bmap)
19 bmap = isl_basic_map_gauss(bmap, NULL);
20 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
22 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
24 if (bmap->n_ineq <= 1)
27 tab = isl_tab_from_basic_map(bmap);
28 tab = isl_tab_detect_equalities(bmap->ctx, tab);
29 bmap = isl_basic_map_update_from_tab(bmap, tab);
30 isl_tab_free(bmap->ctx, tab);
31 bmap = isl_basic_map_gauss(bmap, NULL);
32 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
36 /* Make eq[row][col] of both bmaps equal so we can add the row
37 * add the column to the common matrix.
38 * Note that because of the echelon form, the columns of row row
39 * after column col are zero.
41 static void set_common_multiple(
42 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
43 unsigned row, unsigned col)
47 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
52 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
53 isl_int_divexact(c, m, bset1->eq[row][col]);
54 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
55 isl_int_divexact(c, m, bset2->eq[row][col]);
56 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
61 /* Delete a given equality, moving all the following equalities one up.
63 static void delete_row(struct isl_basic_set *bset, unsigned row)
70 for (r = row; r < bset->n_eq; ++r)
71 bset->eq[r] = bset->eq[r+1];
72 bset->eq[bset->n_eq] = t;
75 /* Make first row entries in column col of bset1 identical to
76 * those of bset2, using the fact that entry bset1->eq[row][col]=a
77 * is non-zero. Initially, these elements of bset1 are all zero.
78 * For each row i < row, we set
79 * A[i] = a * A[i] + B[i][col] * A[row]
82 * A[i][col] = B[i][col] = a * old(B[i][col])
84 static void construct_column(
85 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
86 unsigned row, unsigned col)
95 total = 1 + isl_basic_set_n_dim(bset1);
96 for (r = 0; r < row; ++r) {
97 if (isl_int_is_zero(bset2->eq[r][col]))
99 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
100 isl_int_divexact(a, bset1->eq[row][col], b);
101 isl_int_divexact(b, bset2->eq[r][col], b);
102 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
103 b, bset1->eq[row], total);
104 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
108 delete_row(bset1, row);
111 /* Make first row entries in column col of bset1 identical to
112 * those of bset2, using only these entries of the two matrices.
113 * Let t be the last row with different entries.
114 * For each row i < t, we set
115 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
116 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
118 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
120 static int transform_column(
121 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
122 unsigned row, unsigned col)
128 for (t = row-1; t >= 0; --t)
129 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
134 total = 1 + isl_basic_set_n_dim(bset1);
138 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
139 for (i = 0; i < t; ++i) {
140 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
141 isl_int_gcd(g, a, b);
142 isl_int_divexact(a, a, g);
143 isl_int_divexact(g, b, g);
144 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
146 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
152 delete_row(bset1, t);
153 delete_row(bset2, t);
157 /* The implementation is based on Section 5.2 of Michael Karr,
158 * "Affine Relationships Among Variables of a Program",
159 * except that the echelon form we use starts from the last column
160 * and that we are dealing with integer coefficients.
162 static struct isl_basic_set *affine_hull(
163 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
169 total = 1 + isl_basic_set_n_dim(bset1);
172 for (col = total-1; col >= 0; --col) {
173 int is_zero1 = row >= bset1->n_eq ||
174 isl_int_is_zero(bset1->eq[row][col]);
175 int is_zero2 = row >= bset2->n_eq ||
176 isl_int_is_zero(bset2->eq[row][col]);
177 if (!is_zero1 && !is_zero2) {
178 set_common_multiple(bset1, bset2, row, col);
180 } else if (!is_zero1 && is_zero2) {
181 construct_column(bset1, bset2, row, col);
182 } else if (is_zero1 && !is_zero2) {
183 construct_column(bset2, bset1, row, col);
185 if (transform_column(bset1, bset2, row, col))
189 isl_basic_set_free(bset2);
190 isl_assert(ctx, row == bset1->n_eq, goto error);
193 isl_basic_set_free(bset1);
197 static struct isl_basic_set *isl_basic_set_from_vec(struct isl_ctx *ctx,
202 struct isl_basic_set *bset = NULL;
207 isl_assert(ctx, vec->size != 0, goto error);
209 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
212 dim = isl_basic_set_n_dim(bset);
213 for (i = dim - 1; i >= 0; --i) {
214 k = isl_basic_set_alloc_equality(bset);
217 isl_seq_clr(bset->eq[k], 1 + dim);
218 isl_int_neg(bset->eq[k][0], vec->block.data[1 + i]);
219 isl_int_set(bset->eq[k][1 + i], vec->block.data[0]);
221 isl_vec_free(ctx, vec);
225 isl_basic_set_free(bset);
226 isl_vec_free(ctx, vec);
230 /* Find an integer point in "bset" that lies outside of the equality
232 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
233 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
234 * The point, if found, is returned as a singleton set.
235 * If no point can be found, the empty set is returned.
237 * Before solving an ILP problem, we first check if simply
238 * adding the normal of the constraint to one of the known
239 * integer points in the basic set yields another point
240 * inside the basic set.
242 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
243 struct isl_basic_set *bset, isl_int *eq, int up)
245 struct isl_basic_set *slice = NULL;
246 struct isl_vec *sample;
247 struct isl_basic_set *point;
251 dim = isl_basic_set_n_dim(bset);
252 sample = isl_vec_alloc(ctx, 1 + dim);
255 isl_int_set_si(sample->block.data[0], 1);
256 isl_seq_combine(sample->block.data + 1,
257 ctx->one, bset->sample->block.data + 1,
258 up ? ctx->one : ctx->negone, eq + 1, dim);
259 if (isl_basic_set_contains(bset, sample))
260 return isl_basic_set_from_vec(ctx, sample);
261 isl_vec_free(ctx, sample);
264 slice = isl_basic_set_copy(bset);
267 slice = isl_basic_set_cow(slice);
268 slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
269 k = isl_basic_set_alloc_inequality(slice);
273 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
275 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
276 isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
278 sample = isl_basic_set_sample(slice);
281 if (sample->size == 0) {
282 isl_vec_free(ctx, sample);
283 point = isl_basic_set_empty_like(bset);
285 point = isl_basic_set_from_vec(ctx, sample);
289 isl_basic_set_free(slice);
293 /* Look for all equalities satisfied by the integer points in bset,
294 * which is assume not to have any explicit equalities.
296 * The equalities are obtained by successively looking for
297 * a point that is affinely independent of the points found so far.
298 * In particular, for each equality satisfied by the points so far,
299 * we check if there is any point on a hyperplane parallel to the
300 * corresponding hyperplane shifted by at least one (in either direction).
302 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
305 struct isl_basic_set *hull = NULL;
306 struct isl_vec *sample;
310 if (isl_basic_set_is_empty(bset))
314 sample = isl_basic_set_sample(isl_basic_set_copy(bset));
317 if (sample->size == 0) {
318 isl_vec_free(ctx, sample);
319 hull = isl_basic_set_empty_like(bset);
320 isl_basic_set_free(bset);
323 hull = isl_basic_set_from_vec(ctx, sample);
325 dim = isl_basic_set_n_dim(bset);
326 for (i = 0; i < dim; ++i) {
327 struct isl_basic_set *point;
328 for (j = 0; j < hull->n_eq; ++j) {
329 point = outside_point(ctx, bset, hull->eq[j], 1);
332 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
334 isl_basic_set_free(point);
335 point = outside_point(ctx, bset, hull->eq[j], 0);
338 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
340 isl_basic_set_free(point);
344 hull = affine_hull(hull, point);
346 isl_basic_set_free(bset);
350 isl_basic_set_free(bset);
351 isl_basic_set_free(hull);
355 /* Look for all equalities satisfied by the integer points in bmap
356 * that are independent of the equalities already explicitly available
359 * We first remove all equalities already explicitly available,
360 * then look for additional equalities in the reduced space
361 * and then transform the result to the original space.
362 * The original equalities are _not_ added to this set. This is
363 * the responsibility of the calling function.
364 * The resulting basic set has all meaning about the dimensions removed.
365 * In particular, dimensions that correspond to existential variables
366 * in bmap and that are found to be fixed are not removed.
368 static struct isl_basic_set *equalities_in_underlying_set(
369 struct isl_basic_map *bmap)
371 struct isl_mat *T2 = NULL;
372 struct isl_basic_set *bset = NULL;
373 struct isl_basic_set *hull = NULL;
377 bset = isl_basic_map_underlying_set(bmap);
378 bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
382 hull = uset_affine_hull(bset);
384 hull = isl_basic_set_preimage(hull, T2);
388 isl_mat_free(ctx, T2);
389 isl_basic_set_free(bset);
390 isl_basic_set_free(hull);
394 /* Detect and make explicit all equalities satisfied by the (integer)
397 struct isl_basic_map *isl_basic_map_detect_equalities(
398 struct isl_basic_map *bmap)
401 struct isl_basic_set *hull = NULL;
405 if (bmap->n_ineq == 0)
407 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
409 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
411 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
412 return isl_basic_map_implicit_equalities(bmap);
414 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
417 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
419 for (i = 0; i < hull->n_eq; ++i) {
420 j = isl_basic_map_alloc_equality(bmap);
423 isl_seq_cpy(bmap->eq[j], hull->eq[i],
424 1 + isl_basic_set_total_dim(hull));
426 isl_basic_set_free(hull);
427 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
428 bmap = isl_basic_map_simplify(bmap);
429 return isl_basic_map_finalize(bmap);
431 isl_basic_set_free(hull);
432 isl_basic_map_free(bmap);
436 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
438 struct isl_basic_map *bmap;
444 for (i = 0; i < map->n; ++i) {
445 bmap = isl_basic_map_copy(map->p[i]);
446 bmap = isl_basic_map_detect_equalities(bmap);
449 isl_basic_map_free(map->p[i]);
459 /* After computing the rational affine hull (by detecting the implicit
460 * equalities), we compute the additional equalities satisfied by
461 * the integer points (if any) and add the original equalities back in.
463 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
465 struct isl_basic_set *hull = NULL;
467 bmap = isl_basic_map_detect_equalities(bmap);
468 bmap = isl_basic_map_cow(bmap);
469 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
473 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
475 return (struct isl_basic_set *)
476 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
479 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
482 struct isl_basic_map *model = NULL;
483 struct isl_basic_map *hull = NULL;
490 hull = isl_basic_map_empty_like_map(map);
495 map = isl_map_detect_equalities(map);
496 map = isl_map_align_divs(map);
499 model = isl_basic_map_copy(map->p[0]);
500 set = isl_map_underlying_set(map);
501 set = isl_set_cow(set);
505 for (i = 0; i < set->n; ++i) {
506 set->p[i] = isl_basic_set_cow(set->p[i]);
507 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
508 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
512 set = isl_set_remove_empty_parts(set);
514 hull = isl_basic_map_empty_like(model);
515 isl_basic_map_free(model);
517 struct isl_basic_set *bset;
519 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
523 bset = isl_basic_set_copy(set->p[0]);
524 hull = isl_basic_map_overlying_set(bset, model);
527 hull = isl_basic_map_simplify(hull);
528 return isl_basic_map_finalize(hull);
530 isl_basic_map_free(model);
535 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
537 return (struct isl_basic_set *)
538 isl_map_affine_hull((struct isl_map *)set);