2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
15 #include "isl_map_private.h"
16 #include "isl_equalities.h"
17 #include "isl_sample.h"
20 struct isl_basic_map *isl_basic_map_implicit_equalities(
21 struct isl_basic_map *bmap)
28 bmap = isl_basic_map_gauss(bmap, NULL);
29 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
31 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_IMPLICIT))
33 if (bmap->n_ineq <= 1)
36 tab = isl_tab_from_basic_map(bmap);
37 if (isl_tab_detect_implicit_equalities(tab) < 0)
39 bmap = isl_basic_map_update_from_tab(bmap, tab);
41 bmap = isl_basic_map_gauss(bmap, NULL);
42 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
46 isl_basic_map_free(bmap);
50 struct isl_basic_set *isl_basic_set_implicit_equalities(
51 struct isl_basic_set *bset)
53 return (struct isl_basic_set *)
54 isl_basic_map_implicit_equalities((struct isl_basic_map*)bset);
57 struct isl_map *isl_map_implicit_equalities(struct isl_map *map)
64 for (i = 0; i < map->n; ++i) {
65 map->p[i] = isl_basic_map_implicit_equalities(map->p[i]);
76 /* Make eq[row][col] of both bmaps equal so we can add the row
77 * add the column to the common matrix.
78 * Note that because of the echelon form, the columns of row row
79 * after column col are zero.
81 static void set_common_multiple(
82 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
83 unsigned row, unsigned col)
87 if (isl_int_eq(bset1->eq[row][col], bset2->eq[row][col]))
92 isl_int_lcm(m, bset1->eq[row][col], bset2->eq[row][col]);
93 isl_int_divexact(c, m, bset1->eq[row][col]);
94 isl_seq_scale(bset1->eq[row], bset1->eq[row], c, col+1);
95 isl_int_divexact(c, m, bset2->eq[row][col]);
96 isl_seq_scale(bset2->eq[row], bset2->eq[row], c, col+1);
101 /* Delete a given equality, moving all the following equalities one up.
103 static void delete_row(struct isl_basic_set *bset, unsigned row)
110 for (r = row; r < bset->n_eq; ++r)
111 bset->eq[r] = bset->eq[r+1];
112 bset->eq[bset->n_eq] = t;
115 /* Make first row entries in column col of bset1 identical to
116 * those of bset2, using the fact that entry bset1->eq[row][col]=a
117 * is non-zero. Initially, these elements of bset1 are all zero.
118 * For each row i < row, we set
119 * A[i] = a * A[i] + B[i][col] * A[row]
122 * A[i][col] = B[i][col] = a * old(B[i][col])
124 static void construct_column(
125 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
126 unsigned row, unsigned col)
135 total = 1 + isl_basic_set_n_dim(bset1);
136 for (r = 0; r < row; ++r) {
137 if (isl_int_is_zero(bset2->eq[r][col]))
139 isl_int_gcd(b, bset2->eq[r][col], bset1->eq[row][col]);
140 isl_int_divexact(a, bset1->eq[row][col], b);
141 isl_int_divexact(b, bset2->eq[r][col], b);
142 isl_seq_combine(bset1->eq[r], a, bset1->eq[r],
143 b, bset1->eq[row], total);
144 isl_seq_scale(bset2->eq[r], bset2->eq[r], a, total);
148 delete_row(bset1, row);
151 /* Make first row entries in column col of bset1 identical to
152 * those of bset2, using only these entries of the two matrices.
153 * Let t be the last row with different entries.
154 * For each row i < t, we set
155 * A[i] = (A[t][col]-B[t][col]) * A[i] + (B[i][col]-A[i][col) * A[t]
156 * B[i] = (A[t][col]-B[t][col]) * B[i] + (B[i][col]-A[i][col) * B[t]
158 * A[i][col] = B[i][col] = old(A[t][col]*B[i][col]-A[i][col]*B[t][col])
160 static int transform_column(
161 struct isl_basic_set *bset1, struct isl_basic_set *bset2,
162 unsigned row, unsigned col)
168 for (t = row-1; t >= 0; --t)
169 if (isl_int_ne(bset1->eq[t][col], bset2->eq[t][col]))
174 total = 1 + isl_basic_set_n_dim(bset1);
178 isl_int_sub(b, bset1->eq[t][col], bset2->eq[t][col]);
179 for (i = 0; i < t; ++i) {
180 isl_int_sub(a, bset2->eq[i][col], bset1->eq[i][col]);
181 isl_int_gcd(g, a, b);
182 isl_int_divexact(a, a, g);
183 isl_int_divexact(g, b, g);
184 isl_seq_combine(bset1->eq[i], g, bset1->eq[i], a, bset1->eq[t],
186 isl_seq_combine(bset2->eq[i], g, bset2->eq[i], a, bset2->eq[t],
192 delete_row(bset1, t);
193 delete_row(bset2, t);
197 /* The implementation is based on Section 5.2 of Michael Karr,
198 * "Affine Relationships Among Variables of a Program",
199 * except that the echelon form we use starts from the last column
200 * and that we are dealing with integer coefficients.
202 static struct isl_basic_set *affine_hull(
203 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
209 total = 1 + isl_basic_set_n_dim(bset1);
212 for (col = total-1; col >= 0; --col) {
213 int is_zero1 = row >= bset1->n_eq ||
214 isl_int_is_zero(bset1->eq[row][col]);
215 int is_zero2 = row >= bset2->n_eq ||
216 isl_int_is_zero(bset2->eq[row][col]);
217 if (!is_zero1 && !is_zero2) {
218 set_common_multiple(bset1, bset2, row, col);
220 } else if (!is_zero1 && is_zero2) {
221 construct_column(bset1, bset2, row, col);
222 } else if (is_zero1 && !is_zero2) {
223 construct_column(bset2, bset1, row, col);
225 if (transform_column(bset1, bset2, row, col))
229 isl_basic_set_free(bset2);
230 isl_assert(bset1->ctx, row == bset1->n_eq, goto error);
231 bset1 = isl_basic_set_normalize_constraints(bset1);
234 isl_basic_set_free(bset1);
238 /* Find an integer point in the set represented by "tab"
239 * that lies outside of the equality "eq" e(x) = 0.
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.
243 * If no point can be found, a zero-length vector 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 represented by "tab"
248 * yields another point inside the basic set.
250 * The caller of this function ensures that the tableau is bounded or
251 * that tab->basis and tab->n_unbounded have been set appropriately.
253 static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
256 struct isl_vec *sample = NULL;
257 struct isl_tab_undo *snap;
265 sample = isl_vec_alloc(ctx, 1 + dim);
268 isl_int_set_si(sample->el[0], 1);
269 isl_seq_combine(sample->el + 1,
270 ctx->one, tab->bmap->sample->el + 1,
271 up ? ctx->one : ctx->negone, eq + 1, dim);
272 if (isl_basic_map_contains(tab->bmap, sample))
274 isl_vec_free(sample);
277 snap = isl_tab_snap(tab);
280 isl_seq_neg(eq, eq, 1 + dim);
281 isl_int_sub_ui(eq[0], eq[0], 1);
283 if (isl_tab_extend_cons(tab, 1) < 0)
285 if (isl_tab_add_ineq(tab, eq) < 0)
288 sample = isl_tab_sample(tab);
290 isl_int_add_ui(eq[0], eq[0], 1);
292 isl_seq_neg(eq, eq, 1 + dim);
294 if (isl_tab_rollback(tab, snap) < 0)
299 isl_vec_free(sample);
303 struct isl_basic_set *isl_basic_set_recession_cone(struct isl_basic_set *bset)
307 bset = isl_basic_set_cow(bset);
310 isl_assert(bset->ctx, bset->n_div == 0, goto error);
312 for (i = 0; i < bset->n_eq; ++i)
313 isl_int_set_si(bset->eq[i][0], 0);
315 for (i = 0; i < bset->n_ineq; ++i)
316 isl_int_set_si(bset->ineq[i][0], 0);
318 ISL_F_CLR(bset, ISL_BASIC_SET_NO_IMPLICIT);
319 return isl_basic_set_implicit_equalities(bset);
321 isl_basic_set_free(bset);
325 __isl_give isl_set *isl_set_recession_cone(__isl_take isl_set *set)
334 set = isl_set_remove_divs(set);
335 set = isl_set_cow(set);
339 for (i = 0; i < set->n; ++i) {
340 set->p[i] = isl_basic_set_recession_cone(set->p[i]);
351 /* Extend an initial (under-)approximation of the affine hull of basic
352 * set represented by the tableau "tab"
353 * by looking for points that do not satisfy one of the equalities
354 * in the current approximation and adding them to that approximation
355 * until no such points can be found any more.
357 * The caller of this function ensures that "tab" is bounded or
358 * that tab->basis and tab->n_unbounded have been set appropriately.
360 static struct isl_basic_set *extend_affine_hull(struct isl_tab *tab,
361 struct isl_basic_set *hull)
371 if (isl_tab_extend_cons(tab, 2 * dim + 1) < 0)
374 for (i = 0; i < dim; ++i) {
375 struct isl_vec *sample;
376 struct isl_basic_set *point;
377 for (j = 0; j < hull->n_eq; ++j) {
378 sample = outside_point(tab, hull->eq[j], 1);
381 if (sample->size > 0)
383 isl_vec_free(sample);
384 sample = outside_point(tab, hull->eq[j], 0);
387 if (sample->size > 0)
389 isl_vec_free(sample);
391 tab = isl_tab_add_eq(tab, hull->eq[j]);
398 tab = isl_tab_add_sample(tab, isl_vec_copy(sample));
401 point = isl_basic_set_from_vec(sample);
402 hull = affine_hull(hull, point);
407 isl_basic_set_free(hull);
411 /* Drop all constraints in bset that involve any of the dimensions
412 * first to first+n-1.
414 static struct isl_basic_set *drop_constraints_involving
415 (struct isl_basic_set *bset, unsigned first, unsigned n)
422 bset = isl_basic_set_cow(bset);
424 for (i = bset->n_eq - 1; i >= 0; --i) {
425 if (isl_seq_first_non_zero(bset->eq[i] + 1 + first, n) == -1)
427 isl_basic_set_drop_equality(bset, i);
430 for (i = bset->n_ineq - 1; i >= 0; --i) {
431 if (isl_seq_first_non_zero(bset->ineq[i] + 1 + first, n) == -1)
433 isl_basic_set_drop_inequality(bset, i);
439 /* Look for all equalities satisfied by the integer points in bset,
440 * which is assumed to be bounded.
442 * The equalities are obtained by successively looking for
443 * a point that is affinely independent of the points found so far.
444 * In particular, for each equality satisfied by the points so far,
445 * we check if there is any point on a hyperplane parallel to the
446 * corresponding hyperplane shifted by at least one (in either direction).
448 static struct isl_basic_set *uset_affine_hull_bounded(struct isl_basic_set *bset)
450 struct isl_vec *sample = NULL;
451 struct isl_basic_set *hull;
452 struct isl_tab *tab = NULL;
455 if (isl_basic_set_fast_is_empty(bset))
458 dim = isl_basic_set_n_dim(bset);
460 if (bset->sample && bset->sample->size == 1 + dim) {
461 int contains = isl_basic_set_contains(bset, bset->sample);
467 sample = isl_vec_copy(bset->sample);
469 isl_vec_free(bset->sample);
474 tab = isl_tab_from_basic_set(bset);
479 isl_vec_free(sample);
480 return isl_basic_set_set_to_empty(bset);
482 if (isl_tab_track_bset(tab, isl_basic_set_copy(bset)) < 0)
486 struct isl_tab_undo *snap;
487 snap = isl_tab_snap(tab);
488 sample = isl_tab_sample(tab);
489 if (isl_tab_rollback(tab, snap) < 0)
491 isl_vec_free(tab->bmap->sample);
492 tab->bmap->sample = isl_vec_copy(sample);
497 if (sample->size == 0) {
499 isl_vec_free(sample);
500 return isl_basic_set_set_to_empty(bset);
503 hull = isl_basic_set_from_vec(sample);
505 isl_basic_set_free(bset);
506 hull = extend_affine_hull(tab, hull);
511 isl_vec_free(sample);
513 isl_basic_set_free(bset);
517 /* Given an unbounded tableau and an integer point satisfying the tableau,
518 * construct an intial affine hull containing the recession cone
519 * shifted to the given point.
521 * The unbounded directions are taken from the last rows of the basis,
522 * which is assumed to have been initialized appropriately.
524 static __isl_give isl_basic_set *initial_hull(struct isl_tab *tab,
525 __isl_take isl_vec *vec)
529 struct isl_basic_set *bset = NULL;
536 isl_assert(ctx, vec->size != 0, goto error);
538 bset = isl_basic_set_alloc(ctx, 0, vec->size - 1, 0, vec->size - 1, 0);
541 dim = isl_basic_set_n_dim(bset) - tab->n_unbounded;
542 for (i = 0; i < dim; ++i) {
543 k = isl_basic_set_alloc_equality(bset);
546 isl_seq_cpy(bset->eq[k] + 1, tab->basis->row[1 + i] + 1,
548 isl_seq_inner_product(bset->eq[k] + 1, vec->el +1,
549 vec->size - 1, &bset->eq[k][0]);
550 isl_int_neg(bset->eq[k][0], bset->eq[k][0]);
553 bset = isl_basic_set_gauss(bset, NULL);
557 isl_basic_set_free(bset);
562 /* Given a tableau of a set and a tableau of the corresponding
563 * recession cone, detect and add all equalities to the tableau.
564 * If the tableau is bounded, then we can simply keep the
565 * tableau in its state after the return from extend_affine_hull.
566 * However, if the tableau is unbounded, then
567 * isl_tab_set_initial_basis_with_cone will add some additional
568 * constraints to the tableau that have to be removed again.
569 * In this case, we therefore rollback to the state before
570 * any constraints were added and then add the eqaulities back in.
572 struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
573 struct isl_tab *tab_cone)
576 struct isl_vec *sample;
577 struct isl_basic_set *hull;
578 struct isl_tab_undo *snap;
580 if (!tab || !tab_cone)
583 snap = isl_tab_snap(tab);
585 isl_mat_free(tab->basis);
588 isl_assert(tab->mat->ctx, tab->bmap, goto error);
589 isl_assert(tab->mat->ctx, tab->samples, goto error);
590 isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
591 isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);
593 if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
596 sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
600 isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);
602 isl_vec_free(tab->bmap->sample);
603 tab->bmap->sample = isl_vec_copy(sample);
605 if (tab->n_unbounded == 0)
606 hull = isl_basic_set_from_vec(isl_vec_copy(sample));
608 hull = initial_hull(tab, isl_vec_copy(sample));
610 for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
611 isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
612 hull = affine_hull(hull,
613 isl_basic_set_from_vec(isl_vec_copy(sample)));
616 isl_vec_free(sample);
618 hull = extend_affine_hull(tab, hull);
622 if (tab->n_unbounded == 0) {
623 isl_basic_set_free(hull);
627 if (isl_tab_rollback(tab, snap) < 0)
630 if (hull->n_eq > tab->n_zero) {
631 for (j = 0; j < hull->n_eq; ++j) {
632 isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
633 tab = isl_tab_add_eq(tab, hull->eq[j]);
637 isl_basic_set_free(hull);
645 /* Compute the affine hull of "bset", where "cone" is the recession cone
648 * We first compute a unimodular transformation that puts the unbounded
649 * directions in the last dimensions. In particular, we take a transformation
650 * that maps all equalities to equalities (in HNF) on the first dimensions.
651 * Let x be the original dimensions and y the transformed, with y_1 bounded
654 * [ y_1 ] [ y_1 ] [ Q_1 ]
655 * x = U [ y_2 ] [ y_2 ] = [ Q_2 ] x
657 * Let's call the input basic set S. We compute S' = preimage(S, U)
658 * and drop the final dimensions including any constraints involving them.
659 * This results in set S''.
660 * Then we compute the affine hull A'' of S''.
661 * Let F y_1 >= g be the constraint system of A''. In the transformed
662 * space the y_2 are unbounded, so we can add them back without any constraints,
666 * [ F 0 ] [ y_2 ] >= g
669 * [ F 0 ] [ Q_2 ] x >= g
673 * The affine hull in the original space is then obtained as
674 * A = preimage(A'', Q_1).
676 static struct isl_basic_set *affine_hull_with_cone(struct isl_basic_set *bset,
677 struct isl_basic_set *cone)
681 struct isl_basic_set *hull;
682 struct isl_mat *M, *U, *Q;
687 total = isl_basic_set_total_dim(cone);
688 cone_dim = total - cone->n_eq;
690 M = isl_mat_sub_alloc(bset->ctx, cone->eq, 0, cone->n_eq, 1, total);
691 M = isl_mat_left_hermite(M, 0, &U, &Q);
696 U = isl_mat_lin_to_aff(U);
697 bset = isl_basic_set_preimage(bset, isl_mat_copy(U));
699 bset = drop_constraints_involving(bset, total - cone_dim, cone_dim);
700 bset = isl_basic_set_drop_dims(bset, total - cone_dim, cone_dim);
702 Q = isl_mat_lin_to_aff(Q);
703 Q = isl_mat_drop_rows(Q, 1 + total - cone_dim, cone_dim);
705 if (bset && bset->sample && bset->sample->size == 1 + total)
706 bset->sample = isl_mat_vec_product(isl_mat_copy(Q), bset->sample);
708 hull = uset_affine_hull_bounded(bset);
713 struct isl_vec *sample = isl_vec_copy(hull->sample);
714 U = isl_mat_drop_cols(U, 1 + total - cone_dim, cone_dim);
715 if (sample && sample->size > 0)
716 sample = isl_mat_vec_product(U, sample);
719 hull = isl_basic_set_preimage(hull, Q);
720 isl_vec_free(hull->sample);
721 hull->sample = sample;
724 isl_basic_set_free(cone);
728 isl_basic_set_free(bset);
729 isl_basic_set_free(cone);
733 /* Look for all equalities satisfied by the integer points in bset,
734 * which is assumed not to have any explicit equalities.
736 * The equalities are obtained by successively looking for
737 * a point that is affinely independent of the points found so far.
738 * In particular, for each equality satisfied by the points so far,
739 * we check if there is any point on a hyperplane parallel to the
740 * corresponding hyperplane shifted by at least one (in either direction).
742 * Before looking for any outside points, we first compute the recession
743 * cone. The directions of this recession cone will always be part
744 * of the affine hull, so there is no need for looking for any points
745 * in these directions.
746 * In particular, if the recession cone is full-dimensional, then
747 * the affine hull is simply the whole universe.
749 static struct isl_basic_set *uset_affine_hull(struct isl_basic_set *bset)
751 struct isl_basic_set *cone;
753 if (isl_basic_set_fast_is_empty(bset))
756 cone = isl_basic_set_recession_cone(isl_basic_set_copy(bset));
759 if (cone->n_eq == 0) {
760 struct isl_basic_set *hull;
761 isl_basic_set_free(cone);
762 hull = isl_basic_set_universe_like(bset);
763 isl_basic_set_free(bset);
767 if (cone->n_eq < isl_basic_set_total_dim(cone))
768 return affine_hull_with_cone(bset, cone);
770 isl_basic_set_free(cone);
771 return uset_affine_hull_bounded(bset);
773 isl_basic_set_free(bset);
777 /* Look for all equalities satisfied by the integer points in bmap
778 * that are independent of the equalities already explicitly available
781 * We first remove all equalities already explicitly available,
782 * then look for additional equalities in the reduced space
783 * and then transform the result to the original space.
784 * The original equalities are _not_ added to this set. This is
785 * the responsibility of the calling function.
786 * The resulting basic set has all meaning about the dimensions removed.
787 * In particular, dimensions that correspond to existential variables
788 * in bmap and that are found to be fixed are not removed.
790 static struct isl_basic_set *equalities_in_underlying_set(
791 struct isl_basic_map *bmap)
793 struct isl_mat *T1 = NULL;
794 struct isl_mat *T2 = NULL;
795 struct isl_basic_set *bset = NULL;
796 struct isl_basic_set *hull = NULL;
798 bset = isl_basic_map_underlying_set(bmap);
802 bset = isl_basic_set_remove_equalities(bset, &T1, &T2);
806 hull = uset_affine_hull(bset);
813 struct isl_vec *sample = isl_vec_copy(hull->sample);
814 if (sample && sample->size > 0)
815 sample = isl_mat_vec_product(T1, sample);
818 hull = isl_basic_set_preimage(hull, T2);
819 isl_vec_free(hull->sample);
820 hull->sample = sample;
826 isl_basic_set_free(bset);
827 isl_basic_set_free(hull);
831 /* Detect and make explicit all equalities satisfied by the (integer)
834 struct isl_basic_map *isl_basic_map_detect_equalities(
835 struct isl_basic_map *bmap)
838 struct isl_basic_set *hull = NULL;
842 if (bmap->n_ineq == 0)
844 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
846 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_ALL_EQUALITIES))
848 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
849 return isl_basic_map_implicit_equalities(bmap);
851 hull = equalities_in_underlying_set(isl_basic_map_copy(bmap));
854 if (ISL_F_ISSET(hull, ISL_BASIC_SET_EMPTY)) {
855 isl_basic_set_free(hull);
856 return isl_basic_map_set_to_empty(bmap);
858 bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim), 0,
860 for (i = 0; i < hull->n_eq; ++i) {
861 j = isl_basic_map_alloc_equality(bmap);
864 isl_seq_cpy(bmap->eq[j], hull->eq[i],
865 1 + isl_basic_set_total_dim(hull));
867 isl_vec_free(bmap->sample);
868 bmap->sample = isl_vec_copy(hull->sample);
869 isl_basic_set_free(hull);
870 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT | ISL_BASIC_MAP_ALL_EQUALITIES);
871 bmap = isl_basic_map_simplify(bmap);
872 return isl_basic_map_finalize(bmap);
874 isl_basic_set_free(hull);
875 isl_basic_map_free(bmap);
879 __isl_give isl_basic_set *isl_basic_set_detect_equalities(
880 __isl_take isl_basic_set *bset)
882 return (isl_basic_set *)
883 isl_basic_map_detect_equalities((isl_basic_map *)bset);
886 struct isl_map *isl_map_detect_equalities(struct isl_map *map)
888 struct isl_basic_map *bmap;
894 for (i = 0; i < map->n; ++i) {
895 bmap = isl_basic_map_copy(map->p[i]);
896 bmap = isl_basic_map_detect_equalities(bmap);
899 isl_basic_map_free(map->p[i]);
909 __isl_give isl_set *isl_set_detect_equalities(__isl_take isl_set *set)
911 return (isl_set *)isl_map_detect_equalities((isl_map *)set);
914 /* After computing the rational affine hull (by detecting the implicit
915 * equalities), we compute the additional equalities satisfied by
916 * the integer points (if any) and add the original equalities back in.
918 struct isl_basic_map *isl_basic_map_affine_hull(struct isl_basic_map *bmap)
920 bmap = isl_basic_map_detect_equalities(bmap);
921 bmap = isl_basic_map_cow(bmap);
922 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
926 struct isl_basic_set *isl_basic_set_affine_hull(struct isl_basic_set *bset)
928 return (struct isl_basic_set *)
929 isl_basic_map_affine_hull((struct isl_basic_map *)bset);
932 struct isl_basic_map *isl_map_affine_hull(struct isl_map *map)
935 struct isl_basic_map *model = NULL;
936 struct isl_basic_map *hull = NULL;
939 map = isl_map_detect_equalities(map);
940 map = isl_map_align_divs(map);
946 hull = isl_basic_map_empty_like_map(map);
951 model = isl_basic_map_copy(map->p[0]);
952 set = isl_map_underlying_set(map);
953 set = isl_set_cow(set);
957 for (i = 0; i < set->n; ++i) {
958 set->p[i] = isl_basic_set_cow(set->p[i]);
959 set->p[i] = isl_basic_set_affine_hull(set->p[i]);
960 set->p[i] = isl_basic_set_gauss(set->p[i], NULL);
964 set = isl_set_remove_empty_parts(set);
966 hull = isl_basic_map_empty_like(model);
967 isl_basic_map_free(model);
969 struct isl_basic_set *bset;
971 set->p[0] = affine_hull(set->p[0], set->p[--set->n]);
975 bset = isl_basic_set_copy(set->p[0]);
976 hull = isl_basic_map_overlying_set(bset, model);
979 hull = isl_basic_map_simplify(hull);
980 return isl_basic_map_finalize(hull);
982 isl_basic_map_free(model);
987 struct isl_basic_set *isl_set_affine_hull(struct isl_set *set)
989 return (struct isl_basic_set *)
990 isl_map_affine_hull((struct isl_map *)set);