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(tab);
29 bmap = isl_basic_map_update_from_tab(bmap, 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 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
50 for (i = 0; i < map->n; ++i) {
51 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
62 /* Make eq[row][col] of both bmaps equal so we can add the row
63 * add the column to the common matrix.
64 * Note that because of the echelon form, the columns of row row
65 * after column col are zero.
67 static void set_common_multiple(
68 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
69 unsigned row, unsigned col)
73 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
78 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
79 isl_int_divexact(c, m, bset1->eq[row][col]);
80 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
81 isl_int_divexact(c, m, bset2->eq[row][col]);
82 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
87 /* Delete a given equality, moving all the following equalities one up.
89 static void delete_row(struct isl_basic_set *bset, unsigned row)
96 for (r = row; r < bset->n_eq; ++r)
97 bset->eq[r] = bset->eq[r+1];
98 bset->eq[bset->n_eq] = t;
101 /* Make first row entries in column col of bset1 identical to
102 * those of bset2, using the fact that entry bset1->eq[row][col]=a
103 * is non-zero. Initially, these elements of bset1 are all zero.
104 * For each row i < row, we set
105 * A[i] = a * A[i] + B[i][col] * A[row]
108 * A[i][col] = B[i][col] = a * old(B[i][col])
110 static void construct_column(
111 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
112 unsigned row, unsigned col)
121 total = 1 + isl_basic_set_n_dim(bset1);
122 for (r = 0; r < row; ++r) {
123 if (isl_int_is_zero(bset2->eq[r][col]))
125 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
126 isl_int_divexact(a, bset1->eq[row][col], b);
127 isl_int_divexact(b, bset2->eq[r][col], b);
128 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
129 b, bset1->eq[row], total);
130 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
134 delete_row(bset1, row);
137 /* Make first row entries in column col of bset1 identical to
138 * those of bset2, using only these entries of the two matrices.
139 * Let t be the last row with different entries.
140 * For each row i < t, we set
141 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
142 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
144 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
146 static int transform_column(
147 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
148 unsigned row, unsigned col)
154 for (t = row-1; t >= 0; --t)
155 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
160 total = 1 + isl_basic_set_n_dim(bset1);
164 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
165 for (i = 0; i < t; ++i) {
166 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
167 isl_int_gcd(g, a, b);
168 isl_int_divexact(a, a, g);
169 isl_int_divexact(g, b, g);
170 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
172 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
178 delete_row(bset1, t);
179 delete_row(bset2, t);
183 /* The implementation is based on Section 5.2 of Michael Karr,
184 * "Affine Relationships Among Variables of a Program",
185 * except that the echelon form we use starts from the last column
186 * and that we are dealing with integer coefficients.
188 static struct isl_basic_set *affine_hull(
189 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
195 total = 1 + isl_basic_set_n_dim(bset1);
198 for (col = total-1; col >= 0; --col) {
199 int is_zero1 = row >= bset1->n_eq ||
200 isl_int_is_zero(bset1->eq[row][col]);
201 int is_zero2 = row >= bset2->n_eq ||
202 isl_int_is_zero(bset2->eq[row][col]);
203 if (!is_zero1 && !is_zero2) {
204 set_common_multiple(bset1, bset2, row, col);
206 } else if (!is_zero1 && is_zero2) {
207 construct_column(bset1, bset2, row, col);
208 } else if (is_zero1 && !is_zero2) {
209 construct_column(bset2, bset1, row, col);
211 if (transform_column(bset1, bset2, row, col))
215 isl_basic_set_free(bset2);
216 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
217 bset1 = isl_basic_set_normalize_constraints(bset1);
220 isl_basic_set_free(bset1);
224 /* Find an integer point in "bset" that lies outside of the equality
226 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
227 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
228 * The point, if found, is returned as a singleton set.
229 * If no point can be found, the empty set is returned.
231 * Before solving an ILP problem, we first check if simply
232 * adding the normal of the constraint to one of the known
233 * integer points in the basic set yields another point
234 * inside the basic set.
236 * The caller of this function ensures that "bset" is bounded.
238 static struct isl_basic_set *outside_point(struct isl_ctx *ctx,
239 struct isl_basic_set *bset, isl_int *eq, int up)
241 struct isl_basic_set *slice = NULL;
242 struct isl_vec *sample;
243 struct isl_basic_set *point;
247 dim = isl_basic_set_n_dim(bset);
248 sample = isl_vec_alloc(ctx, 1 + dim);
251 isl_int_set_si(sample->block.data[0], 1);
252 isl_seq_combine(sample->block.data + 1,
253 ctx->one, bset->sample->block.data + 1,
254 up ? ctx->one : ctx->negone, eq + 1, dim);
255 if (isl_basic_set_contains(bset, sample))
256 return isl_basic_set_from_vec(sample);
257 isl_vec_free(sample);
260 slice = isl_basic_set_copy(bset);
263 slice = isl_basic_set_cow(slice);
264 slice = isl_basic_set_extend(slice, 0, dim, 0, 0, 1);
265 k = isl_basic_set_alloc_inequality(slice);
269 isl_seq_cpy(slice->ineq[k], eq, 1 + dim);
271 isl_seq_neg(slice->ineq[k], eq, 1 + dim);
272 isl_int_sub_ui(slice->ineq[k][0], slice->ineq[k][0], 1);
274 sample = isl_basic_set_sample_bounded(slice);
277 if (sample->size == 0) {
278 isl_vec_free(sample);
279 point = isl_basic_set_empty_like(bset);
281 point = isl_basic_set_from_vec(sample);
285 isl_basic_set_free(slice);
289 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
293 bset = isl_basic_set_cow(bset);
296 isl_assert(bset->ctx, bset->n_div == 0, goto error);
298 for (i = 0; i < bset->n_eq; ++i)
299 isl_int_set_si(bset->eq[i][0], 0);
301 for (i = 0; i < bset->n_ineq; ++i)
302 isl_int_set_si(bset->ineq[i][0], 0);
304 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
305 return isl_basic_set_implicit_equalities(bset);
307 isl_basic_set_free(bset);
311 /* Extend an initial (under-)approximation of the affine hull of "bset"
312 * by looking for points that do not satisfy one of the equalities
313 * in the current approximation and adding them to that approximation
314 * until no such points can be found any more.
316 * The caller of this function ensures that "bset" is bounded.
318 static struct isl_basic_set *extend_affine_hull(struct isl_basic_set *bset,
319 struct isl_basic_set *hull)
326 dim = isl_basic_set_n_dim(bset);
327 for (i = 0; i < dim; ++i) {
328 struct isl_basic_set *point;
329 for (j = 0; j < hull->n_eq; ++j) {
330 point = outside_point(ctx, bset, hull->eq[j], 1);
333 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
335 isl_basic_set_free(point);
336 point = outside_point(ctx, bset, hull->eq[j], 0);
339 if (!ISL_F_ISSET(point, ISL_BASIC_SET_EMPTY))
341 isl_basic_set_free(point);
343 bset = isl_basic_set_extend_constraints(bset, 1, 0);
344 k = isl_basic_set_alloc_equality(bset);
347 isl_seq_cpy(bset->eq[k], hull->eq[j],
348 1 + isl_basic_set_total_dim(hull));
349 bset = isl_basic_set_gauss(bset, NULL);
355 hull = affine_hull(hull, point);
357 isl_basic_set_free(bset);
361 isl_basic_set_free(bset);
362 isl_basic_set_free(hull);
366 /* Drop all constraints in bset that involve any of the dimensions
367 * first to first+n-1.
369 static struct isl_basic_set *drop_constraints_involving
370 (struct isl_basic_set *bset, unsigned first, unsigned n)
377 bset = isl_basic_set_cow(bset);
379 for (i = bset->n_eq - 1; i >= 0; --i) {
380 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
382 isl_basic_set_drop_equality(bset, i);
385 for (i = bset->n_ineq - 1; i >= 0; --i) {
386 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
388 isl_basic_set_drop_inequality(bset, i);
394 /* Compute the affine hull of "bset", where "hull" is an initial approximation
395 * with only a single point of "bset" and "cone" is the recession cone
398 * We first compute a unimodular transformation that puts the unbounded
399 * directions in the last dimensions. In particular, we take a transformation
400 * that maps all equalities to equalities (in HNF) on the first dimensions.
401 * Let x be the original dimensions and y the transformed, with y_1 bounded
404 * [ y_1 ] [ y_1 ] [ Q_1 ]
405 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
407 * Let's call the input basic set S and the initial hull H.
408 * We compute S' = preimage(S, U) and H' = preimage(H, U)
409 * and drop the final dimensions including any constraints involving them.
410 * This results in sets S'' and H''.
411 * Then we extend H'' to the affine hull A'' of S''.
412 * Let F y_1 >= g be the constraint system of A''. In the transformed
413 * space the y_2 are unbounded, so we can add them back without any constraints,
417 * [ F 0 ] [ y_2 ] >= g
420 * [ F 0 ] [ Q_2 ] x >= g
424 * The affine hull in the original space is then obtained as
425 * A = preimage(A'', Q_1).
427 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
428 struct isl_basic_set *hull, struct isl_basic_set *cone)
432 struct isl_mat *M, *U, *Q;
434 if (!bset || !hull || !cone)
437 total = isl_basic_set_total_dim(cone);
438 cone_dim = total - cone->n_eq;
440 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
441 M = isl_mat_left_hermite(M, 0, &U, &Q);
446 U = isl_mat_lin_to_aff(U);
447 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
448 hull = isl_basic_set_preimage(hull, U);
450 bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
451 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
452 hull = drop_constraints_involving(hull, total - cone_dim, cone_dim);
453 hull = isl_basic_set_drop_dims(hull, total - cone_dim, cone_dim);
455 Q = isl_mat_lin_to_aff(Q);
456 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
458 if (bset && bset->sample)
459 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
461 hull = extend_affine_hull(bset, hull);
463 hull = isl_basic_set_preimage(hull, Q);
465 isl_basic_set_free(cone);
469 isl_basic_set_free(bset);
470 isl_basic_set_free(hull);
471 isl_basic_set_free(cone);
475 /* Look for all equalities satisfied by the integer points in bset,
476 * which is assumed not to have any explicit equalities.
478 * The equalities are obtained by successively looking for
479 * a point that is affinely independent of the points found so far.
480 * In particular, for each equality satisfied by the points so far,
481 * we check if there is any point on a hyperplane parallel to the
482 * corresponding hyperplane shifted by at least one (in either direction).
484 * Before looking for any outside points, we first compute the recession
485 * cone. The directions of this recession cone will always be part
486 * of the affine hull, so there is no need for looking for any points
487 * in these directions.
488 * In particular, if the recession cone is full-dimensional, then
489 * the affine hull is simply the whole universe.
491 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
493 struct isl_basic_set *hull = NULL;
494 struct isl_vec *sample = NULL;
495 struct isl_basic_set *cone;
497 if (isl_basic_set_is_empty(bset))
500 sample = isl_basic_set_sample_vec(isl_basic_set_copy(bset));
503 if (sample->size == 0) {
504 isl_vec_free(sample);
505 hull = isl_basic_set_empty_like(bset);
506 isl_basic_set_free(bset);
509 if (sample->size == 1) {
510 isl_vec_free(sample);
514 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
517 if (cone->n_eq == 0) {
518 isl_basic_set_free(cone);
519 isl_vec_free(sample);
520 hull = isl_basic_set_universe_like(bset);
521 isl_basic_set_free(bset);
525 hull = isl_basic_set_from_vec(sample);
526 if (cone->n_eq < isl_basic_set_total_dim(cone))
527 return affine_hull_with_cone(bset, hull, cone);
529 isl_basic_set_free(cone);
530 return extend_affine_hull(bset, hull);
532 isl_vec_free(sample);
533 isl_basic_set_free(bset);
534 isl_basic_set_free(hull);
538 /* Look for all equalities satisfied by the integer points in bmap
539 * that are independent of the equalities already explicitly available
542 * We first remove all equalities already explicitly available,
543 * then look for additional equalities in the reduced space
544 * and then transform the result to the original space.
545 * The original equalities are _not_ added to this set. This is
546 * the responsibility of the calling function.
547 * The resulting basic set has all meaning about the dimensions removed.
548 * In particular, dimensions that correspond to existential variables
549 * in bmap and that are found to be fixed are not removed.
551 static struct isl_basic_set *equalities_in_underlying_set(
552 struct isl_basic_map *bmap)
554 struct isl_mat *T2 = NULL;
555 struct isl_basic_set *bset = NULL;
556 struct isl_basic_set *hull = NULL;
558 bset = isl_basic_map_underlying_set(bmap);
559 bset = isl_basic_set_remove_equalities(bset, NULL, &T2);
563 hull = uset_affine_hull(bset);
565 hull = isl_basic_set_preimage(hull, T2);
570 isl_basic_set_free(bset);
571 isl_basic_set_free(hull);
575 /* Detect and make explicit all equalities satisfied by the (integer)
578 struct isl_basic_map *isl_basic_map_detect_equalities(
579 struct isl_basic_map *bmap)
582 struct isl_basic_set *hull = NULL;
586 if (bmap->n_ineq == 0)
588 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
590 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
592 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
593 return isl_basic_map_implicit_equalities(bmap);
595 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
598 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
599 isl_basic_set_free(hull);
600 return isl_basic_map_set_to_empty(bmap);
602 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
604 for (i = 0; i < hull->n_eq; ++i) {
605 j = isl_basic_map_alloc_equality(bmap);
608 isl_seq_cpy(bmap->eq[j], hull->eq[i],
609 1 + isl_basic_set_total_dim(hull));
611 isl_basic_set_free(hull);
612 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
613 bmap = isl_basic_map_simplify(bmap);
614 return isl_basic_map_finalize(bmap);
616 isl_basic_set_free(hull);
617 isl_basic_map_free(bmap);
621 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
622 __isl_take isl_basic_set *bset)
624 return (isl_basic_set *)
625 isl_basic_map_detect_equalities((isl_basic_map *)bset);
628 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
630 struct isl_basic_map *bmap;
636 for (i = 0; i < map->n; ++i) {
637 bmap = isl_basic_map_copy(map->p[i]);
638 bmap = isl_basic_map_detect_equalities(bmap);
641 isl_basic_map_free(map->p[i]);
651 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
653 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
656 /* After computing the rational affine hull (by detecting the implicit
657 * equalities), we compute the additional equalities satisfied by
658 * the integer points (if any) and add the original equalities back in.
660 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
662 bmap = isl_basic_map_detect_equalities(bmap);
663 bmap = isl_basic_map_cow(bmap);
664 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
668 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
670 return (struct isl_basic_set *)
671 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
674 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
677 struct isl_basic_map *model = NULL;
678 struct isl_basic_map *hull = NULL;
685 hull = isl_basic_map_empty_like_map(map);
690 map = isl_map_detect_equalities(map);
691 map = isl_map_align_divs(map);
694 model = isl_basic_map_copy(map->p[0]);
695 set = isl_map_underlying_set(map);
696 set = isl_set_cow(set);
700 for (i = 0; i < set->n; ++i) {
701 set->p[i] = isl_basic_set_cow(set->p[i]);
702 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
703 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
707 set = isl_set_remove_empty_parts(set);
709 hull = isl_basic_map_empty_like(model);
710 isl_basic_map_free(model);
712 struct isl_basic_set *bset;
714 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
718 bset = isl_basic_set_copy(set->p[0]);
719 hull = isl_basic_map_overlying_set(bset, model);
722 hull = isl_basic_map_simplify(hull);
723 return isl_basic_map_finalize(hull);
725 isl_basic_map_free(model);
730 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
732 return (struct isl_basic_set *)
733 isl_map_affine_hull((struct isl_map *)set);