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 struct isl_basic_set *isl_basic_set_implicit_equalities(
37 struct isl_basic_set *bset)
39 return (struct isl_basic_set *)
40 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
43 /* Make eq[row][col] of both bmaps equal so we can add the row
44 * add the column to the common matrix.
45 * Note that because of the echelon form, the columns of row row
46 * after column col are zero.
48 static void set_common_multiple(
49 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
50 unsigned row, unsigned col)
54 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
59 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
60 isl_int_divexact(c, m, bset1->eq[row][col]);
61 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
62 isl_int_divexact(c, m, bset2->eq[row][col]);
63 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
68 /* Delete a given equality, moving all the following equalities one up.
70 static void delete_row(struct isl_basic_set *bset, unsigned row)
77 for (r = row; r < bset->n_eq; ++r)
78 bset->eq[r] = bset->eq[r+1];
79 bset->eq[bset->n_eq] = t;
82 /* Make first row entries in column col of bset1 identical to
83 * those of bset2, using the fact that entry bset1->eq[row][col]=a
84 * is non-zero. Initially, these elements of bset1 are all zero.
85 * For each row i < row, we set
86 * A[i] = a * A[i] + B[i][col] * A[row]
89 * A[i][col] = B[i][col] = a * old(B[i][col])
91 static void construct_column(
92 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
93 unsigned row, unsigned col)
102 total = 1 + isl_basic_set_n_dim(bset1);
103 for (r = 0; r < row; ++r) {
104 if (isl_int_is_zero(bset2->eq[r][col]))
106 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
107 isl_int_divexact(a, bset1->eq[row][col], b);
108 isl_int_divexact(b, bset2->eq[r][col], b);
109 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
110 b, bset1->eq[row], total);
111 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
115 delete_row(bset1, row);
118 /* Make first row entries in column col of bset1 identical to
119 * those of bset2, using only these entries of the two matrices.
120 * Let t be the last row with different entries.
121 * For each row i < t, we set
122 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
123 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
125 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
127 static int transform_column(
128 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
129 unsigned row, unsigned col)
135 for (t = row-1; t >= 0; --t)
136 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
141 total = 1 + isl_basic_set_n_dim(bset1);
145 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
146 for (i = 0; i < t; ++i) {
147 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
148 isl_int_gcd(g, a, b);
149 isl_int_divexact(a, a, g);
150 isl_int_divexact(g, b, g);
151 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
153 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
159 delete_row(bset1, t);
160 delete_row(bset2, t);
164 /* The implementation is based on Section 5.2 of Michael Karr,
165 * "Affine Relationships Among Variables of a Program",
166 * except that the echelon form we use starts from the last column
167 * and that we are dealing with integer coefficients.
169 static struct isl_basic_set *affine_hull(
170 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
176 total = 1 + isl_basic_set_n_dim(bset1);
179 for (col = total-1; col >= 0; --col) {
180 int is_zero1 = row >= bset1->n_eq ||
181 isl_int_is_zero(bset1->eq[row][col]);
182 int is_zero2 = row >= bset2->n_eq ||
183 isl_int_is_zero(bset2->eq[row][col]);
184 if (!is_zero1 && !is_zero2) {
185 set_common_multiple(bset1, bset2, row, col);
187 } else if (!is_zero1 && is_zero2) {
188 construct_column(bset1, bset2, row, col);
189 } else if (is_zero1 && !is_zero2) {
190 construct_column(bset2, bset1, row, col);
192 if (transform_column(bset1, bset2, row, col))
196 isl_basic_set_free(bset2);
197 isl_assert(ctx, row == bset1->n_eq, goto error);
198 bset1 = isl_basic_set_normalize_constraints(bset1);
201 isl_basic_set_free(bset1);
205 static struct isl_basic_set *isl_basic_set_from_vec(struct isl_ctx *ctx,
210 struct isl_basic_set *bset = NULL;
215 isl_assert(ctx, vec->size != 0, goto error);
217 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
220 dim = isl_basic_set_n_dim(bset);
221 for (i = dim - 1; i >= 0; --i) {
222 k = isl_basic_set_alloc_equality(bset);
225 isl_seq_clr(bset->eq[k], 1 + dim);
226 isl_int_neg(bset->eq[k][0], vec->block.data[1 + i]);
227 isl_int_set(bset->eq[k][1 + i], vec->block.data[0]);
229 isl_vec_free(ctx, vec);
233 isl_basic_set_free(bset);
234 isl_vec_free(ctx, vec);
238 /* Find an integer point in "bset" that lies outside of the equality
240 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
241 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
242 * The point, if found, is returned as a singleton set.
243 * If no point can be found, the empty set is returned.
245 * Before solving an ILP problem, we first check if simply
246 * adding the normal of the constraint to one of the known
247 * integer points in the basic set yields another point
248 * inside the basic set.
250 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
251 struct isl_basic_set *bset, isl_int *eq, int up)
253 struct isl_basic_set *slice = NULL;
254 struct isl_vec *sample;
255 struct isl_basic_set *point;
259 dim = isl_basic_set_n_dim(bset);
260 sample = isl_vec_alloc(ctx, 1 + dim);
263 isl_int_set_si(sample->block.data[0], 1);
264 isl_seq_combine(sample->block.data + 1,
265 ctx->one, bset->sample->block.data + 1,
266 up ? ctx->one : ctx->negone, eq + 1, dim);
267 if (isl_basic_set_contains(bset, sample))
268 return isl_basic_set_from_vec(ctx, sample);
269 isl_vec_free(ctx, sample);
272 slice = isl_basic_set_copy(bset);
275 slice = isl_basic_set_cow(slice);
276 slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
277 k = isl_basic_set_alloc_inequality(slice);
281 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
283 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
284 isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
286 sample = isl_basic_set_sample(slice);
289 if (sample->size == 0) {
290 isl_vec_free(ctx, sample);
291 point = isl_basic_set_empty_like(bset);
293 point = isl_basic_set_from_vec(ctx, sample);
297 isl_basic_set_free(slice);
301 static struct isl_basic_set *recession_cone(struct isl_basic_set *bset)
305 bset = isl_basic_set_cow(bset);
309 for (i = 0; i < bset->n_eq; ++i)
310 isl_int_set_si(bset->eq[i][0], 0);
312 for (i = 0; i < bset->n_ineq; ++i)
313 isl_int_set_si(bset->ineq[i][0], 0);
315 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
316 return isl_basic_set_implicit_equalities(bset);
319 static struct isl_basic_set *shift(struct isl_basic_set *bset, isl_int *point)
324 bset = isl_basic_set_cow(bset);
328 dim = isl_basic_set_n_dim(bset);
329 for (i = 0; i < bset->n_eq; ++i) {
330 isl_seq_inner_product(bset->eq[i]+1, point+1, dim,
332 isl_int_neg(bset->eq[i][0], bset->eq[i][0]);
335 for (i = 0; i < bset->n_ineq; ++i) {
336 isl_seq_inner_product(bset->ineq[i]+1, point+1, dim,
338 isl_int_neg(bset->ineq[i][0], bset->ineq[i][0]);
344 /* Look for all equalities satisfied by the integer points in bset,
345 * which is assume not to have any explicit equalities.
347 * The equalities are obtained by successively looking for
348 * a point that is affinely independent of the points found so far.
349 * In particular, for each equality satisfied by the points so far,
350 * we check if there is any point on a hyperplane parallel to the
351 * corresponding hyperplane shifted by at least one (in either direction).
353 * Before looking for any outside points, we first remove the equalities
354 * that correspond to the affine hull of the recession cone.
355 * These equalities will never be equalities over the whols basic set.
357 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
360 struct isl_basic_set *hull = NULL;
361 struct isl_vec *sample;
365 if (isl_basic_set_is_empty(bset))
369 sample = isl_basic_set_sample(isl_basic_set_copy(bset));
372 if (sample->size == 0) {
373 isl_vec_free(ctx, sample);
374 hull = isl_basic_set_empty_like(bset);
375 isl_basic_set_free(bset);
378 hull = isl_basic_set_from_vec(ctx, sample);
380 if (hull->n_eq > 0) {
381 struct isl_basic_set *cone;
382 cone = recession_cone(isl_basic_set_copy(bset));
383 isl_basic_set_free_inequality(cone, cone->n_ineq);
384 cone = isl_basic_set_normalize_constraints(cone);
385 cone = shift(cone, bset->sample->block.data);
386 hull = affine_hull(hull, cone);
389 dim = isl_basic_set_n_dim(bset);
390 for (i = 0; i < dim; ++i) {
391 struct isl_basic_set *point;
392 for (j = 0; j < hull->n_eq; ++j) {
393 point = outside_point(ctx, bset, hull->eq[j], 1);
396 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
398 isl_basic_set_free(point);
399 point = outside_point(ctx, bset, hull->eq[j], 0);
402 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
404 isl_basic_set_free(point);
408 hull = affine_hull(hull, point);
410 isl_basic_set_free(bset);
414 isl_basic_set_free(bset);
415 isl_basic_set_free(hull);
419 /* Look for all equalities satisfied by the integer points in bmap
420 * that are independent of the equalities already explicitly available
423 * We first remove all equalities already explicitly available,
424 * then look for additional equalities in the reduced space
425 * and then transform the result to the original space.
426 * The original equalities are _not_ added to this set. This is
427 * the responsibility of the calling function.
428 * The resulting basic set has all meaning about the dimensions removed.
429 * In particular, dimensions that correspond to existential variables
430 * in bmap and that are found to be fixed are not removed.
432 static struct isl_basic_set *equalities_in_underlying_set(
433 struct isl_basic_map *bmap)
435 struct isl_mat *T2 = NULL;
436 struct isl_basic_set *bset = NULL;
437 struct isl_basic_set *hull = NULL;
441 bset = isl_basic_map_underlying_set(bmap);
442 bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
446 hull = uset_affine_hull(bset);
448 hull = isl_basic_set_preimage(hull, T2);
452 isl_mat_free(ctx, T2);
453 isl_basic_set_free(bset);
454 isl_basic_set_free(hull);
458 /* Detect and make explicit all equalities satisfied by the (integer)
461 struct isl_basic_map *isl_basic_map_detect_equalities(
462 struct isl_basic_map *bmap)
465 struct isl_basic_set *hull = NULL;
469 if (bmap->n_ineq == 0)
471 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
473 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
475 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
476 return isl_basic_map_implicit_equalities(bmap);
478 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
481 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
483 for (i = 0; i < hull->n_eq; ++i) {
484 j = isl_basic_map_alloc_equality(bmap);
487 isl_seq_cpy(bmap->eq[j], hull->eq[i],
488 1 + isl_basic_set_total_dim(hull));
490 isl_basic_set_free(hull);
491 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
492 bmap = isl_basic_map_simplify(bmap);
493 return isl_basic_map_finalize(bmap);
495 isl_basic_set_free(hull);
496 isl_basic_map_free(bmap);
500 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
502 struct isl_basic_map *bmap;
508 for (i = 0; i < map->n; ++i) {
509 bmap = isl_basic_map_copy(map->p[i]);
510 bmap = isl_basic_map_detect_equalities(bmap);
513 isl_basic_map_free(map->p[i]);
523 /* After computing the rational affine hull (by detecting the implicit
524 * equalities), we compute the additional equalities satisfied by
525 * the integer points (if any) and add the original equalities back in.
527 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
529 struct isl_basic_set *hull = NULL;
531 bmap = isl_basic_map_detect_equalities(bmap);
532 bmap = isl_basic_map_cow(bmap);
533 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
537 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
539 return (struct isl_basic_set *)
540 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
543 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
546 struct isl_basic_map *model = NULL;
547 struct isl_basic_map *hull = NULL;
554 hull = isl_basic_map_empty_like_map(map);
559 map = isl_map_detect_equalities(map);
560 map = isl_map_align_divs(map);
563 model = isl_basic_map_copy(map->p[0]);
564 set = isl_map_underlying_set(map);
565 set = isl_set_cow(set);
569 for (i = 0; i < set->n; ++i) {
570 set->p[i] = isl_basic_set_cow(set->p[i]);
571 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
572 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
576 set = isl_set_remove_empty_parts(set);
578 hull = isl_basic_map_empty_like(model);
579 isl_basic_map_free(model);
581 struct isl_basic_set *bset;
583 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
587 bset = isl_basic_set_copy(set->p[0]);
588 hull = isl_basic_map_overlying_set(bset, model);
591 hull = isl_basic_map_simplify(hull);
592 return isl_basic_map_finalize(hull);
594 isl_basic_map_free(model);
599 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
601 return (struct isl_basic_set *)
602 isl_map_affine_hull((struct isl_map *)set);