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
12 #include "isl_map_private.h"
16 #include "isl_equalities.h"
19 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
21 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
27 bmap->ineq[i] = bmap->ineq[j];
32 /* Return 1 if constraint c is redundant with respect to the constraints
33 * in bmap. If c is a lower [upper] bound in some variable and bmap
34 * does not have a lower [upper] bound in that variable, then c cannot
35 * be redundant and we do not need solve any lp.
37 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
38 isl_int *c, isl_int *opt_n, isl_int *opt_d)
40 enum isl_lp_result res;
47 total = isl_basic_map_total_dim(*bmap);
48 for (i = 0; i < total; ++i) {
50 if (isl_int_is_zero(c[1+i]))
52 sign = isl_int_sgn(c[1+i]);
53 for (j = 0; j < (*bmap)->n_ineq; ++j)
54 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
56 if (j == (*bmap)->n_ineq)
62 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
64 if (res == isl_lp_unbounded)
66 if (res == isl_lp_error)
68 if (res == isl_lp_empty) {
69 *bmap = isl_basic_map_set_to_empty(*bmap);
72 return !isl_int_is_neg(*opt_n);
75 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
76 isl_int *c, isl_int *opt_n, isl_int *opt_d)
78 return isl_basic_map_constraint_is_redundant(
79 (struct isl_basic_map **)bset, c, opt_n, opt_d);
82 /* Compute the convex hull of a basic map, by removing the redundant
83 * constraints. If the minimal value along the normal of a constraint
84 * is the same if the constraint is removed, then the constraint is redundant.
86 * Alternatively, we could have intersected the basic map with the
87 * corresponding equality and the checked if the dimension was that
90 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
97 bmap = isl_basic_map_gauss(bmap, NULL);
98 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
100 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
102 if (bmap->n_ineq <= 1)
105 tab = isl_tab_from_basic_map(bmap);
106 tab = isl_tab_detect_implicit_equalities(tab);
107 if (isl_tab_detect_redundant(tab) < 0)
109 bmap = isl_basic_map_update_from_tab(bmap, tab);
111 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
112 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
116 isl_basic_map_free(bmap);
120 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
122 return (struct isl_basic_set *)
123 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
126 /* Check if the set set is bound in the direction of the affine
127 * constraint c and if so, set the constant term such that the
128 * resulting constraint is a bounding constraint for the set.
130 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
138 isl_int_init(opt_denom);
140 for (j = 0; j < set->n; ++j) {
141 enum isl_lp_result res;
143 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
146 res = isl_basic_set_solve_lp(set->p[j],
147 0, c, set->ctx->one, &opt, &opt_denom, NULL);
148 if (res == isl_lp_unbounded)
150 if (res == isl_lp_error)
152 if (res == isl_lp_empty) {
153 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
158 if (!isl_int_is_one(opt_denom))
159 isl_seq_scale(c, c, opt_denom, len);
160 if (first || isl_int_is_neg(opt))
161 isl_int_sub(c[0], c[0], opt);
165 isl_int_clear(opt_denom);
169 isl_int_clear(opt_denom);
173 /* Check if "c" is a direction that is independent of the previously found "n"
175 * If so, add it to the list, with the negative of the lower bound
176 * in the constant position, i.e., such that c corresponds to a bounding
177 * hyperplane (but not necessarily a facet).
178 * Assumes set "set" is bounded.
180 static int is_independent_bound(struct isl_set *set, isl_int *c,
181 struct isl_mat *dirs, int n)
186 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
188 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
191 for (i = 0; i < n; ++i) {
193 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
198 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
199 dirs->n_col-1, NULL);
200 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
206 is_bound = uset_is_bound(set, dirs->row[n], dirs->n_col);
211 isl_int *t = dirs->row[n];
212 for (k = n; k > i; --k)
213 dirs->row[k] = dirs->row[k-1];
219 /* Compute and return a maximal set of linearly independent bounds
220 * on the set "set", based on the constraints of the basic sets
223 static struct isl_mat *independent_bounds(struct isl_set *set)
226 struct isl_mat *dirs = NULL;
227 unsigned dim = isl_set_n_dim(set);
229 dirs = isl_mat_alloc(set->ctx, dim, 1+dim);
234 for (i = 0; n < dim && i < set->n; ++i) {
236 struct isl_basic_set *bset = set->p[i];
238 for (j = 0; n < dim && j < bset->n_eq; ++j) {
239 f = is_independent_bound(set, bset->eq[j], dirs, n);
245 for (j = 0; n < dim && j < bset->n_ineq; ++j) {
246 f = is_independent_bound(set, bset->ineq[j], dirs, n);
260 struct isl_basic_set *isl_basic_set_set_rational(struct isl_basic_set *bset)
265 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
268 bset = isl_basic_set_cow(bset);
272 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
274 return isl_basic_set_finalize(bset);
277 static struct isl_set *isl_set_set_rational(struct isl_set *set)
281 set = isl_set_cow(set);
284 for (i = 0; i < set->n; ++i) {
285 set->p[i] = isl_basic_set_set_rational(set->p[i]);
295 static struct isl_basic_set *isl_basic_set_add_equality(
296 struct isl_basic_set *bset, isl_int *c)
301 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
304 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
305 isl_assert(bset->ctx, bset->n_div == 0, goto error);
306 dim = isl_basic_set_n_dim(bset);
307 bset = isl_basic_set_cow(bset);
308 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
309 i = isl_basic_set_alloc_equality(bset);
312 isl_seq_cpy(bset->eq[i], c, 1 + dim);
315 isl_basic_set_free(bset);
319 static struct isl_set *isl_set_add_equality(struct isl_set *set, isl_int *c)
323 set = isl_set_cow(set);
326 for (i = 0; i < set->n; ++i) {
327 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
337 /* Given a union of basic sets, construct the constraints for wrapping
338 * a facet around one of its ridges.
339 * In particular, if each of n the d-dimensional basic sets i in "set"
340 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
341 * and is defined by the constraints
345 * then the resulting set is of dimension n*(1+d) and has as constraints
354 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
356 struct isl_basic_set *lp;
360 unsigned dim, lp_dim;
365 dim = 1 + isl_set_n_dim(set);
368 for (i = 0; i < set->n; ++i) {
369 n_eq += set->p[i]->n_eq;
370 n_ineq += set->p[i]->n_ineq;
372 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
375 lp_dim = isl_basic_set_n_dim(lp);
376 k = isl_basic_set_alloc_equality(lp);
377 isl_int_set_si(lp->eq[k][0], -1);
378 for (i = 0; i < set->n; ++i) {
379 isl_int_set_si(lp->eq[k][1+dim*i], 0);
380 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
381 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
383 for (i = 0; i < set->n; ++i) {
384 k = isl_basic_set_alloc_inequality(lp);
385 isl_seq_clr(lp->ineq[k], 1+lp_dim);
386 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
388 for (j = 0; j < set->p[i]->n_eq; ++j) {
389 k = isl_basic_set_alloc_equality(lp);
390 isl_seq_clr(lp->eq[k], 1+dim*i);
391 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
392 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
395 for (j = 0; j < set->p[i]->n_ineq; ++j) {
396 k = isl_basic_set_alloc_inequality(lp);
397 isl_seq_clr(lp->ineq[k], 1+dim*i);
398 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
399 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
405 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
406 * of that facet, compute the other facet of the convex hull that contains
409 * We first transform the set such that the facet constraint becomes
413 * I.e., the facet lies in
417 * and on that facet, the constraint that defines the ridge is
421 * (This transformation is not strictly needed, all that is needed is
422 * that the ridge contains the origin.)
424 * Since the ridge contains the origin, the cone of the convex hull
425 * will be of the form
430 * with this second constraint defining the new facet.
431 * The constant a is obtained by settting x_1 in the cone of the
432 * convex hull to 1 and minimizing x_2.
433 * Now, each element in the cone of the convex hull is the sum
434 * of elements in the cones of the basic sets.
435 * If a_i is the dilation factor of basic set i, then the problem
436 * we need to solve is
449 * the constraints of each (transformed) basic set.
450 * If a = n/d, then the constraint defining the new facet (in the transformed
453 * -n x_1 + d x_2 >= 0
455 * In the original space, we need to take the same combination of the
456 * corresponding constraints "facet" and "ridge".
458 * Note that a is always finite, since we only apply the wrapping
459 * technique to a union of polytopes.
461 static isl_int *wrap_facet(struct isl_set *set, isl_int *facet, isl_int *ridge)
464 struct isl_mat *T = NULL;
465 struct isl_basic_set *lp = NULL;
467 enum isl_lp_result res;
471 set = isl_set_copy(set);
473 dim = 1 + isl_set_n_dim(set);
474 T = isl_mat_alloc(set->ctx, 3, dim);
477 isl_int_set_si(T->row[0][0], 1);
478 isl_seq_clr(T->row[0]+1, dim - 1);
479 isl_seq_cpy(T->row[1], facet, dim);
480 isl_seq_cpy(T->row[2], ridge, dim);
481 T = isl_mat_right_inverse(T);
482 set = isl_set_preimage(set, T);
486 lp = wrap_constraints(set);
487 obj = isl_vec_alloc(set->ctx, 1 + dim*set->n);
490 isl_int_set_si(obj->block.data[0], 0);
491 for (i = 0; i < set->n; ++i) {
492 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
493 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
494 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
498 res = isl_basic_set_solve_lp(lp, 0,
499 obj->block.data, set->ctx->one, &num, &den, NULL);
500 if (res == isl_lp_ok) {
501 isl_int_neg(num, num);
502 isl_seq_combine(facet, num, facet, den, ridge, dim);
507 isl_basic_set_free(lp);
509 isl_assert(set->ctx, res == isl_lp_ok, return NULL);
512 isl_basic_set_free(lp);
518 /* Given a set of d linearly independent bounding constraints of the
519 * convex hull of "set", compute the constraint of a facet of "set".
521 * We first compute the intersection with the first bounding hyperplane
522 * and remove the component corresponding to this hyperplane from
523 * other bounds (in homogeneous space).
524 * We then wrap around one of the remaining bounding constraints
525 * and continue the process until all bounding constraints have been
526 * taken into account.
527 * The resulting linear combination of the bounding constraints will
528 * correspond to a facet of the convex hull.
530 static struct isl_mat *initial_facet_constraint(struct isl_set *set,
531 struct isl_mat *bounds)
533 struct isl_set *slice = NULL;
534 struct isl_basic_set *face = NULL;
535 struct isl_mat *m, *U, *Q;
537 unsigned dim = isl_set_n_dim(set);
539 isl_assert(set->ctx, set->n > 0, goto error);
540 isl_assert(set->ctx, bounds->n_row == dim, goto error);
542 while (bounds->n_row > 1) {
543 slice = isl_set_copy(set);
544 slice = isl_set_add_equality(slice, bounds->row[0]);
545 face = isl_set_affine_hull(slice);
548 if (face->n_eq == 1) {
549 isl_basic_set_free(face);
552 m = isl_mat_alloc(set->ctx, 1 + face->n_eq, 1 + dim);
555 isl_int_set_si(m->row[0][0], 1);
556 isl_seq_clr(m->row[0]+1, dim);
557 for (i = 0; i < face->n_eq; ++i)
558 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
559 U = isl_mat_right_inverse(m);
560 Q = isl_mat_right_inverse(isl_mat_copy(U));
561 U = isl_mat_drop_cols(U, 1 + face->n_eq, dim - face->n_eq);
562 Q = isl_mat_drop_rows(Q, 1 + face->n_eq, dim - face->n_eq);
563 U = isl_mat_drop_cols(U, 0, 1);
564 Q = isl_mat_drop_rows(Q, 0, 1);
565 bounds = isl_mat_product(bounds, U);
566 bounds = isl_mat_product(bounds, Q);
567 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
568 bounds->n_col) == -1) {
570 isl_assert(set->ctx, bounds->n_row > 1, goto error);
572 if (!wrap_facet(set, bounds->row[0],
573 bounds->row[bounds->n_row-1]))
575 isl_basic_set_free(face);
580 isl_basic_set_free(face);
581 isl_mat_free(bounds);
585 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
586 * compute a hyperplane description of the facet, i.e., compute the facets
589 * We compute an affine transformation that transforms the constraint
598 * by computing the right inverse U of a matrix that starts with the rows
611 * Since z_1 is zero, we can drop this variable as well as the corresponding
612 * column of U to obtain
620 * with Q' equal to Q, but without the corresponding row.
621 * After computing the facets of the facet in the z' space,
622 * we convert them back to the x space through Q.
624 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
626 struct isl_mat *m, *U, *Q;
627 struct isl_basic_set *facet = NULL;
632 set = isl_set_copy(set);
633 dim = isl_set_n_dim(set);
634 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
637 isl_int_set_si(m->row[0][0], 1);
638 isl_seq_clr(m->row[0]+1, dim);
639 isl_seq_cpy(m->row[1], c, 1+dim);
640 U = isl_mat_right_inverse(m);
641 Q = isl_mat_right_inverse(isl_mat_copy(U));
642 U = isl_mat_drop_cols(U, 1, 1);
643 Q = isl_mat_drop_rows(Q, 1, 1);
644 set = isl_set_preimage(set, U);
645 facet = uset_convex_hull_wrap_bounded(set);
646 facet = isl_basic_set_preimage(facet, Q);
647 isl_assert(ctx, facet->n_eq == 0, goto error);
650 isl_basic_set_free(facet);
655 /* Given an initial facet constraint, compute the remaining facets.
656 * We do this by running through all facets found so far and computing
657 * the adjacent facets through wrapping, adding those facets that we
658 * hadn't already found before.
660 * For each facet we have found so far, we first compute its facets
661 * in the resulting convex hull. That is, we compute the ridges
662 * of the resulting convex hull contained in the facet.
663 * We also compute the corresponding facet in the current approximation
664 * of the convex hull. There is no need to wrap around the ridges
665 * in this facet since that would result in a facet that is already
666 * present in the current approximation.
668 * This function can still be significantly optimized by checking which of
669 * the facets of the basic sets are also facets of the convex hull and
670 * using all the facets so far to help in constructing the facets of the
673 * using the technique in section "3.1 Ridge Generation" of
674 * "Extended Convex Hull" by Fukuda et al.
676 static struct isl_basic_set *extend(struct isl_basic_set *hull,
681 struct isl_basic_set *facet = NULL;
682 struct isl_basic_set *hull_facet = NULL;
685 isl_assert(set->ctx, set->n > 0, goto error);
687 dim = isl_set_n_dim(set);
689 for (i = 0; i < hull->n_ineq; ++i) {
690 facet = compute_facet(set, hull->ineq[i]);
691 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
692 facet = isl_basic_set_gauss(facet, NULL);
693 facet = isl_basic_set_normalize_constraints(facet);
694 hull_facet = isl_basic_set_copy(hull);
695 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
696 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
697 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
700 hull = isl_basic_set_cow(hull);
701 hull = isl_basic_set_extend_dim(hull,
702 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
703 for (j = 0; j < facet->n_ineq; ++j) {
704 for (f = 0; f < hull_facet->n_ineq; ++f)
705 if (isl_seq_eq(facet->ineq[j],
706 hull_facet->ineq[f], 1 + dim))
708 if (f < hull_facet->n_ineq)
710 k = isl_basic_set_alloc_inequality(hull);
713 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
714 if (!wrap_facet(set, hull->ineq[k], facet->ineq[j]))
717 isl_basic_set_free(hull_facet);
718 isl_basic_set_free(facet);
720 hull = isl_basic_set_simplify(hull);
721 hull = isl_basic_set_finalize(hull);
724 isl_basic_set_free(hull_facet);
725 isl_basic_set_free(facet);
726 isl_basic_set_free(hull);
730 /* Special case for computing the convex hull of a one dimensional set.
731 * We simply collect the lower and upper bounds of each basic set
732 * and the biggest of those.
734 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
736 struct isl_mat *c = NULL;
737 isl_int *lower = NULL;
738 isl_int *upper = NULL;
741 struct isl_basic_set *hull;
743 for (i = 0; i < set->n; ++i) {
744 set->p[i] = isl_basic_set_simplify(set->p[i]);
748 set = isl_set_remove_empty_parts(set);
751 isl_assert(set->ctx, set->n > 0, goto error);
752 c = isl_mat_alloc(set->ctx, 2, 2);
756 if (set->p[0]->n_eq > 0) {
757 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
760 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
761 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
762 isl_seq_neg(upper, set->p[0]->eq[0], 2);
764 isl_seq_neg(lower, set->p[0]->eq[0], 2);
765 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
768 for (j = 0; j < set->p[0]->n_ineq; ++j) {
769 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
771 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
774 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
781 for (i = 0; i < set->n; ++i) {
782 struct isl_basic_set *bset = set->p[i];
786 for (j = 0; j < bset->n_eq; ++j) {
790 isl_int_mul(a, lower[0], bset->eq[j][1]);
791 isl_int_mul(b, lower[1], bset->eq[j][0]);
792 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
793 isl_seq_cpy(lower, bset->eq[j], 2);
794 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
795 isl_seq_neg(lower, bset->eq[j], 2);
798 isl_int_mul(a, upper[0], bset->eq[j][1]);
799 isl_int_mul(b, upper[1], bset->eq[j][0]);
800 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
801 isl_seq_neg(upper, bset->eq[j], 2);
802 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
803 isl_seq_cpy(upper, bset->eq[j], 2);
806 for (j = 0; j < bset->n_ineq; ++j) {
807 if (isl_int_is_pos(bset->ineq[j][1]))
809 if (isl_int_is_neg(bset->ineq[j][1]))
811 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
812 isl_int_mul(a, lower[0], bset->ineq[j][1]);
813 isl_int_mul(b, lower[1], bset->ineq[j][0]);
814 if (isl_int_lt(a, b))
815 isl_seq_cpy(lower, bset->ineq[j], 2);
817 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
818 isl_int_mul(a, upper[0], bset->ineq[j][1]);
819 isl_int_mul(b, upper[1], bset->ineq[j][0]);
820 if (isl_int_gt(a, b))
821 isl_seq_cpy(upper, bset->ineq[j], 2);
832 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
833 hull = isl_basic_set_set_rational(hull);
837 k = isl_basic_set_alloc_inequality(hull);
838 isl_seq_cpy(hull->ineq[k], lower, 2);
841 k = isl_basic_set_alloc_inequality(hull);
842 isl_seq_cpy(hull->ineq[k], upper, 2);
844 hull = isl_basic_set_finalize(hull);
854 /* Project out final n dimensions using Fourier-Motzkin */
855 static struct isl_set *set_project_out(struct isl_ctx *ctx,
856 struct isl_set *set, unsigned n)
858 return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
861 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
863 struct isl_basic_set *convex_hull;
868 if (isl_set_is_empty(set))
869 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
871 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
876 /* Compute the convex hull of a pair of basic sets without any parameters or
877 * integer divisions using Fourier-Motzkin elimination.
878 * The convex hull is the set of all points that can be written as
879 * the sum of points from both basic sets (in homogeneous coordinates).
880 * We set up the constraints in a space with dimensions for each of
881 * the three sets and then project out the dimensions corresponding
882 * to the two original basic sets, retaining only those corresponding
883 * to the convex hull.
885 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
886 struct isl_basic_set *bset2)
889 struct isl_basic_set *bset[2];
890 struct isl_basic_set *hull = NULL;
893 if (!bset1 || !bset2)
896 dim = isl_basic_set_n_dim(bset1);
897 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
898 1 + dim + bset1->n_eq + bset2->n_eq,
899 2 + bset1->n_ineq + bset2->n_ineq);
902 for (i = 0; i < 2; ++i) {
903 for (j = 0; j < bset[i]->n_eq; ++j) {
904 k = isl_basic_set_alloc_equality(hull);
907 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
908 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
909 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
912 for (j = 0; j < bset[i]->n_ineq; ++j) {
913 k = isl_basic_set_alloc_inequality(hull);
916 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
917 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
918 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
919 bset[i]->ineq[j], 1+dim);
921 k = isl_basic_set_alloc_inequality(hull);
924 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
925 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
927 for (j = 0; j < 1+dim; ++j) {
928 k = isl_basic_set_alloc_equality(hull);
931 isl_seq_clr(hull->eq[k], 1+2+3*dim);
932 isl_int_set_si(hull->eq[k][j], -1);
933 isl_int_set_si(hull->eq[k][1+dim+j], 1);
934 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
936 hull = isl_basic_set_set_rational(hull);
937 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
938 hull = isl_basic_set_convex_hull(hull);
939 isl_basic_set_free(bset1);
940 isl_basic_set_free(bset2);
943 isl_basic_set_free(bset1);
944 isl_basic_set_free(bset2);
945 isl_basic_set_free(hull);
949 static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
954 tab = isl_tab_from_recession_cone(bset);
955 bounded = isl_tab_cone_is_bounded(tab);
960 static int isl_set_is_bounded(struct isl_set *set)
964 for (i = 0; i < set->n; ++i) {
965 int bounded = isl_basic_set_is_bounded(set->p[i]);
966 if (!bounded || bounded < 0)
972 /* Compute the lineality space of the convex hull of bset1 and bset2.
974 * We first compute the intersection of the recession cone of bset1
975 * with the negative of the recession cone of bset2 and then compute
976 * the linear hull of the resulting cone.
978 static struct isl_basic_set *induced_lineality_space(
979 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
982 struct isl_basic_set *lin = NULL;
985 if (!bset1 || !bset2)
988 dim = isl_basic_set_total_dim(bset1);
989 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
990 bset1->n_eq + bset2->n_eq,
991 bset1->n_ineq + bset2->n_ineq);
992 lin = isl_basic_set_set_rational(lin);
995 for (i = 0; i < bset1->n_eq; ++i) {
996 k = isl_basic_set_alloc_equality(lin);
999 isl_int_set_si(lin->eq[k][0], 0);
1000 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
1002 for (i = 0; i < bset1->n_ineq; ++i) {
1003 k = isl_basic_set_alloc_inequality(lin);
1006 isl_int_set_si(lin->ineq[k][0], 0);
1007 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
1009 for (i = 0; i < bset2->n_eq; ++i) {
1010 k = isl_basic_set_alloc_equality(lin);
1013 isl_int_set_si(lin->eq[k][0], 0);
1014 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
1016 for (i = 0; i < bset2->n_ineq; ++i) {
1017 k = isl_basic_set_alloc_inequality(lin);
1020 isl_int_set_si(lin->ineq[k][0], 0);
1021 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1024 isl_basic_set_free(bset1);
1025 isl_basic_set_free(bset2);
1026 return isl_basic_set_affine_hull(lin);
1028 isl_basic_set_free(lin);
1029 isl_basic_set_free(bset1);
1030 isl_basic_set_free(bset2);
1034 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1036 /* Given a set and a linear space "lin" of dimension n > 0,
1037 * project the linear space from the set, compute the convex hull
1038 * and then map the set back to the original space.
1044 * describe the linear space. We first compute the Hermite normal
1045 * form H = M U of M = H Q, to obtain
1049 * The last n rows of H will be zero, so the last n variables of x' = Q x
1050 * are the one we want to project out. We do this by transforming each
1051 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1052 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1053 * we transform the hull back to the original space as A' Q_1 x >= b',
1054 * with Q_1 all but the last n rows of Q.
1056 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1057 struct isl_basic_set *lin)
1059 unsigned total = isl_basic_set_total_dim(lin);
1061 struct isl_basic_set *hull;
1062 struct isl_mat *M, *U, *Q;
1066 lin_dim = total - lin->n_eq;
1067 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1068 M = isl_mat_left_hermite(M, 0, &U, &Q);
1072 isl_basic_set_free(lin);
1074 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1076 U = isl_mat_lin_to_aff(U);
1077 Q = isl_mat_lin_to_aff(Q);
1079 set = isl_set_preimage(set, U);
1080 set = isl_set_remove_dims(set, total - lin_dim, lin_dim);
1081 hull = uset_convex_hull(set);
1082 hull = isl_basic_set_preimage(hull, Q);
1086 isl_basic_set_free(lin);
1091 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1092 * set up an LP for solving
1094 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1096 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1097 * The next \alpha{ij} correspond to the equalities and come in pairs.
1098 * The final \alpha{ij} correspond to the inequalities.
1100 static struct isl_basic_set *valid_direction_lp(
1101 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1103 struct isl_dim *dim;
1104 struct isl_basic_set *lp;
1109 if (!bset1 || !bset2)
1111 d = 1 + isl_basic_set_total_dim(bset1);
1113 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1114 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1115 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1118 for (i = 0; i < n; ++i) {
1119 k = isl_basic_set_alloc_inequality(lp);
1122 isl_seq_clr(lp->ineq[k] + 1, n);
1123 isl_int_set_si(lp->ineq[k][0], -1);
1124 isl_int_set_si(lp->ineq[k][1 + i], 1);
1126 for (i = 0; i < d; ++i) {
1127 k = isl_basic_set_alloc_equality(lp);
1131 isl_int_set_si(lp->eq[k][n++], 0);
1132 /* positivity constraint 1 >= 0 */
1133 isl_int_set_si(lp->eq[k][n++], i == 0);
1134 for (j = 0; j < bset1->n_eq; ++j) {
1135 isl_int_set(lp->eq[k][n++], bset1->eq[j][i]);
1136 isl_int_neg(lp->eq[k][n++], bset1->eq[j][i]);
1138 for (j = 0; j < bset1->n_ineq; ++j)
1139 isl_int_set(lp->eq[k][n++], bset1->ineq[j][i]);
1140 /* positivity constraint 1 >= 0 */
1141 isl_int_set_si(lp->eq[k][n++], -(i == 0));
1142 for (j = 0; j < bset2->n_eq; ++j) {
1143 isl_int_neg(lp->eq[k][n++], bset2->eq[j][i]);
1144 isl_int_set(lp->eq[k][n++], bset2->eq[j][i]);
1146 for (j = 0; j < bset2->n_ineq; ++j)
1147 isl_int_neg(lp->eq[k][n++], bset2->ineq[j][i]);
1149 lp = isl_basic_set_gauss(lp, NULL);
1150 isl_basic_set_free(bset1);
1151 isl_basic_set_free(bset2);
1154 isl_basic_set_free(bset1);
1155 isl_basic_set_free(bset2);
1159 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1160 * for all rays in the homogeneous space of the two cones that correspond
1161 * to the input polyhedra bset1 and bset2.
1163 * We compute s as a vector that satisfies
1165 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1167 * with h_{ij} the normals of the facets of polyhedron i
1168 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1169 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1170 * We first set up an LP with as variables the \alpha{ij}.
1171 * In this formulateion, for each polyhedron i,
1172 * the first constraint is the positivity constraint, followed by pairs
1173 * of variables for the equalities, followed by variables for the inequalities.
1174 * We then simply pick a feasible solution and compute s using (*).
1176 * Note that we simply pick any valid direction and make no attempt
1177 * to pick a "good" or even the "best" valid direction.
1179 static struct isl_vec *valid_direction(
1180 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1182 struct isl_basic_set *lp;
1183 struct isl_tab *tab;
1184 struct isl_vec *sample = NULL;
1185 struct isl_vec *dir;
1190 if (!bset1 || !bset2)
1192 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1193 isl_basic_set_copy(bset2));
1194 tab = isl_tab_from_basic_set(lp);
1195 sample = isl_tab_get_sample_value(tab);
1197 isl_basic_set_free(lp);
1200 d = isl_basic_set_total_dim(bset1);
1201 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1204 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1206 /* positivity constraint 1 >= 0 */
1207 isl_int_set(dir->block.data[0], sample->block.data[n++]);
1208 for (i = 0; i < bset1->n_eq; ++i) {
1209 isl_int_sub(sample->block.data[n],
1210 sample->block.data[n], sample->block.data[n+1]);
1211 isl_seq_combine(dir->block.data,
1212 bset1->ctx->one, dir->block.data,
1213 sample->block.data[n], bset1->eq[i], 1 + d);
1217 for (i = 0; i < bset1->n_ineq; ++i)
1218 isl_seq_combine(dir->block.data,
1219 bset1->ctx->one, dir->block.data,
1220 sample->block.data[n++], bset1->ineq[i], 1 + d);
1221 isl_vec_free(sample);
1222 isl_seq_normalize(bset1->ctx, dir->block.data + 1, dir->size - 1);
1223 isl_basic_set_free(bset1);
1224 isl_basic_set_free(bset2);
1227 isl_vec_free(sample);
1228 isl_basic_set_free(bset1);
1229 isl_basic_set_free(bset2);
1233 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1234 * compute b_i' + A_i' x' >= 0, with
1236 * [ b_i A_i ] [ y' ] [ y' ]
1237 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1239 * In particular, add the "positivity constraint" and then perform
1242 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1249 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1250 k = isl_basic_set_alloc_inequality(bset);
1253 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1254 isl_int_set_si(bset->ineq[k][0], 1);
1255 bset = isl_basic_set_preimage(bset, T);
1259 isl_basic_set_free(bset);
1263 /* Compute the convex hull of a pair of basic sets without any parameters or
1264 * integer divisions, where the convex hull is known to be pointed,
1265 * but the basic sets may be unbounded.
1267 * We turn this problem into the computation of a convex hull of a pair
1268 * _bounded_ polyhedra by "changing the direction of the homogeneous
1269 * dimension". This idea is due to Matthias Koeppe.
1271 * Consider the cones in homogeneous space that correspond to the
1272 * input polyhedra. The rays of these cones are also rays of the
1273 * polyhedra if the coordinate that corresponds to the homogeneous
1274 * dimension is zero. That is, if the inner product of the rays
1275 * with the homogeneous direction is zero.
1276 * The cones in the homogeneous space can also be considered to
1277 * correspond to other pairs of polyhedra by chosing a different
1278 * homogeneous direction. To ensure that both of these polyhedra
1279 * are bounded, we need to make sure that all rays of the cones
1280 * correspond to vertices and not to rays.
1281 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1282 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1283 * The vector s is computed in valid_direction.
1285 * Note that we need to consider _all_ rays of the cones and not just
1286 * the rays that correspond to rays in the polyhedra. If we were to
1287 * only consider those rays and turn them into vertices, then we
1288 * may inadvertently turn some vertices into rays.
1290 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1291 * We therefore transform the two polyhedra such that the selected
1292 * direction is mapped onto this standard direction and then proceed
1293 * with the normal computation.
1294 * Let S be a non-singular square matrix with s as its first row,
1295 * then we want to map the polyhedra to the space
1297 * [ y' ] [ y ] [ y ] [ y' ]
1298 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1300 * We take S to be the unimodular completion of s to limit the growth
1301 * of the coefficients in the following computations.
1303 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1304 * We first move to the homogeneous dimension
1306 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1307 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1309 * Then we change directoin
1311 * [ b_i A_i ] [ y' ] [ y' ]
1312 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1314 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1315 * resulting in b' + A' x' >= 0, which we then convert back
1318 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1320 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1322 static struct isl_basic_set *convex_hull_pair_pointed(
1323 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1325 struct isl_ctx *ctx = NULL;
1326 struct isl_vec *dir = NULL;
1327 struct isl_mat *T = NULL;
1328 struct isl_mat *T2 = NULL;
1329 struct isl_basic_set *hull;
1330 struct isl_set *set;
1332 if (!bset1 || !bset2)
1335 dir = valid_direction(isl_basic_set_copy(bset1),
1336 isl_basic_set_copy(bset2));
1339 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1342 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1343 T = isl_mat_unimodular_complete(T, 1);
1344 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1346 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1347 bset2 = homogeneous_map(bset2, T2);
1348 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1349 set = isl_set_add(set, bset1);
1350 set = isl_set_add(set, bset2);
1351 hull = uset_convex_hull(set);
1352 hull = isl_basic_set_preimage(hull, T);
1359 isl_basic_set_free(bset1);
1360 isl_basic_set_free(bset2);
1364 /* Compute the convex hull of a pair of basic sets without any parameters or
1365 * integer divisions.
1367 * If the convex hull of the two basic sets would have a non-trivial
1368 * lineality space, we first project out this lineality space.
1370 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1371 struct isl_basic_set *bset2)
1373 struct isl_basic_set *lin;
1375 if (isl_basic_set_is_bounded(bset1) || isl_basic_set_is_bounded(bset2))
1376 return convex_hull_pair_pointed(bset1, bset2);
1378 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1379 isl_basic_set_copy(bset2));
1382 if (isl_basic_set_is_universe(lin)) {
1383 isl_basic_set_free(bset1);
1384 isl_basic_set_free(bset2);
1387 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1388 struct isl_set *set;
1389 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1390 set = isl_set_add(set, bset1);
1391 set = isl_set_add(set, bset2);
1392 return modulo_lineality(set, lin);
1394 isl_basic_set_free(lin);
1396 return convex_hull_pair_pointed(bset1, bset2);
1398 isl_basic_set_free(bset1);
1399 isl_basic_set_free(bset2);
1403 /* Compute the lineality space of a basic set.
1404 * We currently do not allow the basic set to have any divs.
1405 * We basically just drop the constants and turn every inequality
1408 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1411 struct isl_basic_set *lin = NULL;
1416 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1417 dim = isl_basic_set_total_dim(bset);
1419 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1422 for (i = 0; i < bset->n_eq; ++i) {
1423 k = isl_basic_set_alloc_equality(lin);
1426 isl_int_set_si(lin->eq[k][0], 0);
1427 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1429 lin = isl_basic_set_gauss(lin, NULL);
1432 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1433 k = isl_basic_set_alloc_equality(lin);
1436 isl_int_set_si(lin->eq[k][0], 0);
1437 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1438 lin = isl_basic_set_gauss(lin, NULL);
1442 isl_basic_set_free(bset);
1445 isl_basic_set_free(lin);
1446 isl_basic_set_free(bset);
1450 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1451 * "underlying" set "set".
1453 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1456 struct isl_set *lin = NULL;
1461 struct isl_dim *dim = isl_set_get_dim(set);
1463 return isl_basic_set_empty(dim);
1466 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1467 for (i = 0; i < set->n; ++i)
1468 lin = isl_set_add(lin,
1469 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1471 return isl_set_affine_hull(lin);
1474 /* Compute the convex hull of a set without any parameters or
1475 * integer divisions.
1476 * In each step, we combined two basic sets until only one
1477 * basic set is left.
1478 * The input basic sets are assumed not to have a non-trivial
1479 * lineality space. If any of the intermediate results has
1480 * a non-trivial lineality space, it is projected out.
1482 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1484 struct isl_basic_set *convex_hull = NULL;
1486 convex_hull = isl_set_copy_basic_set(set);
1487 set = isl_set_drop_basic_set(set, convex_hull);
1490 while (set->n > 0) {
1491 struct isl_basic_set *t;
1492 t = isl_set_copy_basic_set(set);
1495 set = isl_set_drop_basic_set(set, t);
1498 convex_hull = convex_hull_pair(convex_hull, t);
1501 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1504 if (isl_basic_set_is_universe(t)) {
1505 isl_basic_set_free(convex_hull);
1509 if (t->n_eq < isl_basic_set_total_dim(t)) {
1510 set = isl_set_add(set, convex_hull);
1511 return modulo_lineality(set, t);
1513 isl_basic_set_free(t);
1519 isl_basic_set_free(convex_hull);
1523 /* Compute an initial hull for wrapping containing a single initial
1524 * facet by first computing bounds on the set and then using these
1525 * bounds to construct an initial facet.
1526 * This function is a remnant of an older implementation where the
1527 * bounds were also used to check whether the set was bounded.
1528 * Since this function will now only be called when we know the
1529 * set to be bounded, the initial facet should probably be constructed
1530 * by simply using the coordinate directions instead.
1532 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1533 struct isl_set *set)
1535 struct isl_mat *bounds = NULL;
1541 bounds = independent_bounds(set);
1544 isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
1545 bounds = initial_facet_constraint(set, bounds);
1548 k = isl_basic_set_alloc_inequality(hull);
1551 dim = isl_set_n_dim(set);
1552 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1553 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1554 isl_mat_free(bounds);
1558 isl_basic_set_free(hull);
1559 isl_mat_free(bounds);
1563 struct max_constraint {
1569 static int max_constraint_equal(const void *entry, const void *val)
1571 struct max_constraint *a = (struct max_constraint *)entry;
1572 isl_int *b = (isl_int *)val;
1574 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1577 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1578 isl_int *con, unsigned len, int n, int ineq)
1580 struct isl_hash_table_entry *entry;
1581 struct max_constraint *c;
1584 c_hash = isl_seq_get_hash(con + 1, len);
1585 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1591 isl_hash_table_remove(ctx, table, entry);
1595 if (isl_int_gt(c->c->row[0][0], con[0]))
1597 if (isl_int_eq(c->c->row[0][0], con[0])) {
1602 c->c = isl_mat_cow(c->c);
1603 isl_int_set(c->c->row[0][0], con[0]);
1607 /* Check whether the constraint hash table "table" constains the constraint
1610 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1611 isl_int *con, unsigned len, int n)
1613 struct isl_hash_table_entry *entry;
1614 struct max_constraint *c;
1617 c_hash = isl_seq_get_hash(con + 1, len);
1618 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1625 return isl_int_eq(c->c->row[0][0], con[0]);
1628 /* Check for inequality constraints of a basic set without equalities
1629 * such that the same or more stringent copies of the constraint appear
1630 * in all of the basic sets. Such constraints are necessarily facet
1631 * constraints of the convex hull.
1633 * If the resulting basic set is by chance identical to one of
1634 * the basic sets in "set", then we know that this basic set contains
1635 * all other basic sets and is therefore the convex hull of set.
1636 * In this case we set *is_hull to 1.
1638 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1639 struct isl_set *set, int *is_hull)
1642 int min_constraints;
1644 struct max_constraint *constraints = NULL;
1645 struct isl_hash_table *table = NULL;
1650 for (i = 0; i < set->n; ++i)
1651 if (set->p[i]->n_eq == 0)
1655 min_constraints = set->p[i]->n_ineq;
1657 for (i = best + 1; i < set->n; ++i) {
1658 if (set->p[i]->n_eq != 0)
1660 if (set->p[i]->n_ineq >= min_constraints)
1662 min_constraints = set->p[i]->n_ineq;
1665 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1669 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1670 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1673 total = isl_dim_total(set->dim);
1674 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1675 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1676 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1677 if (!constraints[i].c)
1679 constraints[i].ineq = 1;
1681 for (i = 0; i < min_constraints; ++i) {
1682 struct isl_hash_table_entry *entry;
1684 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1685 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1686 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1689 isl_assert(hull->ctx, !entry->data, goto error);
1690 entry->data = &constraints[i];
1694 for (s = 0; s < set->n; ++s) {
1698 for (i = 0; i < set->p[s]->n_eq; ++i) {
1699 isl_int *eq = set->p[s]->eq[i];
1700 for (j = 0; j < 2; ++j) {
1701 isl_seq_neg(eq, eq, 1 + total);
1702 update_constraint(hull->ctx, table,
1706 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1707 isl_int *ineq = set->p[s]->ineq[i];
1708 update_constraint(hull->ctx, table, ineq, total, n,
1709 set->p[s]->n_eq == 0);
1714 for (i = 0; i < min_constraints; ++i) {
1715 if (constraints[i].count < n)
1717 if (!constraints[i].ineq)
1719 j = isl_basic_set_alloc_inequality(hull);
1722 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1725 for (s = 0; s < set->n; ++s) {
1726 if (set->p[s]->n_eq)
1728 if (set->p[s]->n_ineq != hull->n_ineq)
1730 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1731 isl_int *ineq = set->p[s]->ineq[i];
1732 if (!has_constraint(hull->ctx, table, ineq, total, n))
1735 if (i == set->p[s]->n_ineq)
1739 isl_hash_table_clear(table);
1740 for (i = 0; i < min_constraints; ++i)
1741 isl_mat_free(constraints[i].c);
1746 isl_hash_table_clear(table);
1749 for (i = 0; i < min_constraints; ++i)
1750 isl_mat_free(constraints[i].c);
1755 /* Create a template for the convex hull of "set" and fill it up
1756 * obvious facet constraints, if any. If the result happens to
1757 * be the convex hull of "set" then *is_hull is set to 1.
1759 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1761 struct isl_basic_set *hull;
1766 for (i = 0; i < set->n; ++i) {
1767 n_ineq += set->p[i]->n_eq;
1768 n_ineq += set->p[i]->n_ineq;
1770 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1771 hull = isl_basic_set_set_rational(hull);
1774 return common_constraints(hull, set, is_hull);
1777 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1779 struct isl_basic_set *hull;
1782 hull = proto_hull(set, &is_hull);
1783 if (hull && !is_hull) {
1784 if (hull->n_ineq == 0)
1785 hull = initial_hull(hull, set);
1786 hull = extend(hull, set);
1793 /* Compute the convex hull of a set without any parameters or
1794 * integer divisions. Depending on whether the set is bounded,
1795 * we pass control to the wrapping based convex hull or
1796 * the Fourier-Motzkin elimination based convex hull.
1797 * We also handle a few special cases before checking the boundedness.
1799 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1801 struct isl_basic_set *convex_hull = NULL;
1802 struct isl_basic_set *lin;
1804 if (isl_set_n_dim(set) == 0)
1805 return convex_hull_0d(set);
1807 set = isl_set_coalesce(set);
1808 set = isl_set_set_rational(set);
1815 convex_hull = isl_basic_set_copy(set->p[0]);
1819 if (isl_set_n_dim(set) == 1)
1820 return convex_hull_1d(set);
1822 if (isl_set_is_bounded(set))
1823 return uset_convex_hull_wrap(set);
1825 lin = uset_combined_lineality_space(isl_set_copy(set));
1828 if (isl_basic_set_is_universe(lin)) {
1832 if (lin->n_eq < isl_basic_set_total_dim(lin))
1833 return modulo_lineality(set, lin);
1834 isl_basic_set_free(lin);
1836 return uset_convex_hull_unbounded(set);
1839 isl_basic_set_free(convex_hull);
1843 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1844 * without parameters or divs and where the convex hull of set is
1845 * known to be full-dimensional.
1847 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1849 struct isl_basic_set *convex_hull = NULL;
1851 if (isl_set_n_dim(set) == 0) {
1852 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1854 convex_hull = isl_basic_set_set_rational(convex_hull);
1858 set = isl_set_set_rational(set);
1862 set = isl_set_coalesce(set);
1866 convex_hull = isl_basic_set_copy(set->p[0]);
1870 if (isl_set_n_dim(set) == 1)
1871 return convex_hull_1d(set);
1873 return uset_convex_hull_wrap(set);
1879 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1880 * We first remove the equalities (transforming the set), compute the
1881 * convex hull of the transformed set and then add the equalities back
1882 * (after performing the inverse transformation.
1884 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1885 struct isl_set *set, struct isl_basic_set *affine_hull)
1889 struct isl_basic_set *dummy;
1890 struct isl_basic_set *convex_hull;
1892 dummy = isl_basic_set_remove_equalities(
1893 isl_basic_set_copy(affine_hull), &T, &T2);
1896 isl_basic_set_free(dummy);
1897 set = isl_set_preimage(set, T);
1898 convex_hull = uset_convex_hull(set);
1899 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1900 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1903 isl_basic_set_free(affine_hull);
1908 /* Compute the convex hull of a map.
1910 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1911 * specifically, the wrapping of facets to obtain new facets.
1913 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1915 struct isl_basic_set *bset;
1916 struct isl_basic_map *model = NULL;
1917 struct isl_basic_set *affine_hull = NULL;
1918 struct isl_basic_map *convex_hull = NULL;
1919 struct isl_set *set = NULL;
1920 struct isl_ctx *ctx;
1927 convex_hull = isl_basic_map_empty_like_map(map);
1932 map = isl_map_detect_equalities(map);
1933 map = isl_map_align_divs(map);
1934 model = isl_basic_map_copy(map->p[0]);
1935 set = isl_map_underlying_set(map);
1939 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1942 if (affine_hull->n_eq != 0)
1943 bset = modulo_affine_hull(ctx, set, affine_hull);
1945 isl_basic_set_free(affine_hull);
1946 bset = uset_convex_hull(set);
1949 convex_hull = isl_basic_map_overlying_set(bset, model);
1951 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1952 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1953 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1957 isl_basic_map_free(model);
1961 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1963 return (struct isl_basic_set *)
1964 isl_map_convex_hull((struct isl_map *)set);
1967 struct sh_data_entry {
1968 struct isl_hash_table *table;
1969 struct isl_tab *tab;
1972 /* Holds the data needed during the simple hull computation.
1974 * n the number of basic sets in the original set
1975 * hull_table a hash table of already computed constraints
1976 * in the simple hull
1977 * p for each basic set,
1978 * table a hash table of the constraints
1979 * tab the tableau corresponding to the basic set
1982 struct isl_ctx *ctx;
1984 struct isl_hash_table *hull_table;
1985 struct sh_data_entry p[1];
1988 static void sh_data_free(struct sh_data *data)
1994 isl_hash_table_free(data->ctx, data->hull_table);
1995 for (i = 0; i < data->n; ++i) {
1996 isl_hash_table_free(data->ctx, data->p[i].table);
1997 isl_tab_free(data->p[i].tab);
2002 struct ineq_cmp_data {
2007 static int has_ineq(const void *entry, const void *val)
2009 isl_int *row = (isl_int *)entry;
2010 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2012 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2013 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2016 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2017 isl_int *ineq, unsigned len)
2020 struct ineq_cmp_data v;
2021 struct isl_hash_table_entry *entry;
2025 c_hash = isl_seq_get_hash(ineq + 1, len);
2026 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2033 /* Fill hash table "table" with the constraints of "bset".
2034 * Equalities are added as two inequalities.
2035 * The value in the hash table is a pointer to the (in)equality of "bset".
2037 static int hash_basic_set(struct isl_hash_table *table,
2038 struct isl_basic_set *bset)
2041 unsigned dim = isl_basic_set_total_dim(bset);
2043 for (i = 0; i < bset->n_eq; ++i) {
2044 for (j = 0; j < 2; ++j) {
2045 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2046 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2050 for (i = 0; i < bset->n_ineq; ++i) {
2051 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2057 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2059 struct sh_data *data;
2062 data = isl_calloc(set->ctx, struct sh_data,
2063 sizeof(struct sh_data) +
2064 (set->n - 1) * sizeof(struct sh_data_entry));
2067 data->ctx = set->ctx;
2069 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2070 if (!data->hull_table)
2072 for (i = 0; i < set->n; ++i) {
2073 data->p[i].table = isl_hash_table_alloc(set->ctx,
2074 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2075 if (!data->p[i].table)
2077 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2086 /* Check if inequality "ineq" is a bound for basic set "j" or if
2087 * it can be relaxed (by increasing the constant term) to become
2088 * a bound for that basic set. In the latter case, the constant
2090 * Return 1 if "ineq" is a bound
2091 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2092 * -1 if some error occurred
2094 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2097 enum isl_lp_result res;
2100 if (!data->p[j].tab) {
2101 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2102 if (!data->p[j].tab)
2108 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2110 if (res == isl_lp_ok && isl_int_is_neg(opt))
2111 isl_int_sub(ineq[0], ineq[0], opt);
2115 return res == isl_lp_ok ? 1 :
2116 res == isl_lp_unbounded ? 0 : -1;
2119 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2120 * become a bound on the whole set. If so, add the (relaxed) inequality
2123 * We first check if "hull" already contains a translate of the inequality.
2124 * If so, we are done.
2125 * Then, we check if any of the previous basic sets contains a translate
2126 * of the inequality. If so, then we have already considered this
2127 * inequality and we are done.
2128 * Otherwise, for each basic set other than "i", we check if the inequality
2129 * is a bound on the basic set.
2130 * For previous basic sets, we know that they do not contain a translate
2131 * of the inequality, so we directly call is_bound.
2132 * For following basic sets, we first check if a translate of the
2133 * inequality appears in its description and if so directly update
2134 * the inequality accordingly.
2136 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2137 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2140 struct ineq_cmp_data v;
2141 struct isl_hash_table_entry *entry;
2147 v.len = isl_basic_set_total_dim(hull);
2149 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2151 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2156 for (j = 0; j < i; ++j) {
2157 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2158 c_hash, has_ineq, &v, 0);
2165 k = isl_basic_set_alloc_inequality(hull);
2166 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2170 for (j = 0; j < i; ++j) {
2172 bound = is_bound(data, set, j, hull->ineq[k]);
2179 isl_basic_set_free_inequality(hull, 1);
2183 for (j = i + 1; j < set->n; ++j) {
2186 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2187 c_hash, has_ineq, &v, 0);
2189 ineq_j = entry->data;
2190 neg = isl_seq_is_neg(ineq_j + 1,
2191 hull->ineq[k] + 1, v.len);
2193 isl_int_neg(ineq_j[0], ineq_j[0]);
2194 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2195 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2197 isl_int_neg(ineq_j[0], ineq_j[0]);
2200 bound = is_bound(data, set, j, hull->ineq[k]);
2207 isl_basic_set_free_inequality(hull, 1);
2211 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2215 entry->data = hull->ineq[k];
2219 isl_basic_set_free(hull);
2223 /* Check if any inequality from basic set "i" can be relaxed to
2224 * become a bound on the whole set. If so, add the (relaxed) inequality
2227 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2228 struct sh_data *data, struct isl_set *set, int i)
2231 unsigned dim = isl_basic_set_total_dim(bset);
2233 for (j = 0; j < set->p[i]->n_eq; ++j) {
2234 for (k = 0; k < 2; ++k) {
2235 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2236 add_bound(bset, data, set, i, set->p[i]->eq[j]);
2239 for (j = 0; j < set->p[i]->n_ineq; ++j)
2240 add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2244 /* Compute a superset of the convex hull of set that is described
2245 * by only translates of the constraints in the constituents of set.
2247 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2249 struct sh_data *data = NULL;
2250 struct isl_basic_set *hull = NULL;
2258 for (i = 0; i < set->n; ++i) {
2261 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2264 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2268 data = sh_data_alloc(set, n_ineq);
2272 for (i = 0; i < set->n; ++i)
2273 hull = add_bounds(hull, data, set, i);
2281 isl_basic_set_free(hull);
2286 /* Compute a superset of the convex hull of map that is described
2287 * by only translates of the constraints in the constituents of map.
2289 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2291 struct isl_set *set = NULL;
2292 struct isl_basic_map *model = NULL;
2293 struct isl_basic_map *hull;
2294 struct isl_basic_map *affine_hull;
2295 struct isl_basic_set *bset = NULL;
2300 hull = isl_basic_map_empty_like_map(map);
2305 hull = isl_basic_map_copy(map->p[0]);
2310 map = isl_map_detect_equalities(map);
2311 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2312 map = isl_map_align_divs(map);
2313 model = isl_basic_map_copy(map->p[0]);
2315 set = isl_map_underlying_set(map);
2317 bset = uset_simple_hull(set);
2319 hull = isl_basic_map_overlying_set(bset, model);
2321 hull = isl_basic_map_intersect(hull, affine_hull);
2322 hull = isl_basic_map_convex_hull(hull);
2323 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2324 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2329 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2331 return (struct isl_basic_set *)
2332 isl_map_simple_hull((struct isl_map *)set);
2335 /* Given a set "set", return parametric bounds on the dimension "dim".
2337 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2339 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2340 set = isl_set_copy(set);
2341 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2342 set = isl_set_eliminate_dims(set, 0, dim);
2343 return isl_set_convex_hull(set);
2346 /* Computes a "simple hull" and then check if each dimension in the
2347 * resulting hull is bounded by a symbolic constant. If not, the
2348 * hull is intersected with the corresponding bounds on the whole set.
2350 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2353 struct isl_basic_set *hull;
2354 unsigned nparam, left;
2355 int removed_divs = 0;
2357 hull = isl_set_simple_hull(isl_set_copy(set));
2361 nparam = isl_basic_set_dim(hull, isl_dim_param);
2362 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2363 int lower = 0, upper = 0;
2364 struct isl_basic_set *bounds;
2366 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2367 for (j = 0; j < hull->n_eq; ++j) {
2368 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2370 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2377 for (j = 0; j < hull->n_ineq; ++j) {
2378 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2380 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2382 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2385 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2396 if (!removed_divs) {
2397 set = isl_set_remove_divs(set);
2402 bounds = set_bounds(set, i);
2403 hull = isl_basic_set_intersect(hull, bounds);