3 #include "isl_map_private.h"
7 #include "isl_equalities.h"
10 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
12 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
18 bmap->ineq[i] = bmap->ineq[j];
23 /* Return 1 if constraint c is redundant with respect to the constraints
24 * in bmap. If c is a lower [upper] bound in some variable and bmap
25 * does not have a lower [upper] bound in that variable, then c cannot
26 * be redundant and we do not need solve any lp.
28 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
29 isl_int *c, isl_int *opt_n, isl_int *opt_d)
31 enum isl_lp_result res;
38 total = isl_basic_map_total_dim(*bmap);
39 for (i = 0; i < total; ++i) {
41 if (isl_int_is_zero(c[1+i]))
43 sign = isl_int_sgn(c[1+i]);
44 for (j = 0; j < (*bmap)->n_ineq; ++j)
45 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
47 if (j == (*bmap)->n_ineq)
53 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
55 if (res == isl_lp_unbounded)
57 if (res == isl_lp_error)
59 if (res == isl_lp_empty) {
60 *bmap = isl_basic_map_set_to_empty(*bmap);
63 return !isl_int_is_neg(*opt_n);
66 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
67 isl_int *c, isl_int *opt_n, isl_int *opt_d)
69 return isl_basic_map_constraint_is_redundant(
70 (struct isl_basic_map **)bset, c, opt_n, opt_d);
73 /* Compute the convex hull of a basic map, by removing the redundant
74 * constraints. If the minimal value along the normal of a constraint
75 * is the same if the constraint is removed, then the constraint is redundant.
77 * Alternatively, we could have intersected the basic map with the
78 * corresponding equality and the checked if the dimension was that
81 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
88 bmap = isl_basic_map_gauss(bmap, NULL);
89 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
91 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
93 if (bmap->n_ineq <= 1)
96 tab = isl_tab_from_basic_map(bmap);
97 tab = isl_tab_detect_implicit_equalities(tab);
98 tab = isl_tab_detect_redundant(tab);
99 bmap = isl_basic_map_update_from_tab(bmap, tab);
101 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
102 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
106 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
108 return (struct isl_basic_set *)
109 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
112 /* Check if the set set is bound in the direction of the affine
113 * constraint c and if so, set the constant term such that the
114 * resulting constraint is a bounding constraint for the set.
116 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
124 isl_int_init(opt_denom);
126 for (j = 0; j < set->n; ++j) {
127 enum isl_lp_result res;
129 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
132 res = isl_basic_set_solve_lp(set->p[j],
133 0, c, set->ctx->one, &opt, &opt_denom, NULL);
134 if (res == isl_lp_unbounded)
136 if (res == isl_lp_error)
138 if (res == isl_lp_empty) {
139 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
144 if (!isl_int_is_one(opt_denom))
145 isl_seq_scale(c, c, opt_denom, len);
146 if (first || isl_int_is_neg(opt))
147 isl_int_sub(c[0], c[0], opt);
151 isl_int_clear(opt_denom);
155 isl_int_clear(opt_denom);
159 /* Check if "c" is a direction that is independent of the previously found "n"
161 * If so, add it to the list, with the negative of the lower bound
162 * in the constant position, i.e., such that c corresponds to a bounding
163 * hyperplane (but not necessarily a facet).
164 * Assumes set "set" is bounded.
166 static int is_independent_bound(struct isl_set *set, isl_int *c,
167 struct isl_mat *dirs, int n)
172 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
174 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
177 for (i = 0; i < n; ++i) {
179 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
184 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
185 dirs->n_col-1, NULL);
186 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
192 is_bound = uset_is_bound(set, dirs->row[n], dirs->n_col);
197 isl_int *t = dirs->row[n];
198 for (k = n; k > i; --k)
199 dirs->row[k] = dirs->row[k-1];
205 /* Compute and return a maximal set of linearly independent bounds
206 * on the set "set", based on the constraints of the basic sets
209 static struct isl_mat *independent_bounds(struct isl_set *set)
212 struct isl_mat *dirs = NULL;
213 unsigned dim = isl_set_n_dim(set);
215 dirs = isl_mat_alloc(set->ctx, dim, 1+dim);
220 for (i = 0; n < dim && i < set->n; ++i) {
222 struct isl_basic_set *bset = set->p[i];
224 for (j = 0; n < dim && j < bset->n_eq; ++j) {
225 f = is_independent_bound(set, bset->eq[j], dirs, n);
231 for (j = 0; n < dim && j < bset->n_ineq; ++j) {
232 f = is_independent_bound(set, bset->ineq[j], dirs, n);
246 struct isl_basic_set *isl_basic_set_set_rational(struct isl_basic_set *bset)
251 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
254 bset = isl_basic_set_cow(bset);
258 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
260 return isl_basic_set_finalize(bset);
263 static struct isl_set *isl_set_set_rational(struct isl_set *set)
267 set = isl_set_cow(set);
270 for (i = 0; i < set->n; ++i) {
271 set->p[i] = isl_basic_set_set_rational(set->p[i]);
281 static struct isl_basic_set *isl_basic_set_add_equality(
282 struct isl_basic_set *bset, isl_int *c)
287 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
290 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
291 isl_assert(bset->ctx, bset->n_div == 0, goto error);
292 dim = isl_basic_set_n_dim(bset);
293 bset = isl_basic_set_cow(bset);
294 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
295 i = isl_basic_set_alloc_equality(bset);
298 isl_seq_cpy(bset->eq[i], c, 1 + dim);
301 isl_basic_set_free(bset);
305 static struct isl_set *isl_set_add_equality(struct isl_set *set, isl_int *c)
309 set = isl_set_cow(set);
312 for (i = 0; i < set->n; ++i) {
313 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
323 /* Given a union of basic sets, construct the constraints for wrapping
324 * a facet around one of its ridges.
325 * In particular, if each of n the d-dimensional basic sets i in "set"
326 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
327 * and is defined by the constraints
331 * then the resulting set is of dimension n*(1+d) and has as constraints
340 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
342 struct isl_basic_set *lp;
346 unsigned dim, lp_dim;
351 dim = 1 + isl_set_n_dim(set);
354 for (i = 0; i < set->n; ++i) {
355 n_eq += set->p[i]->n_eq;
356 n_ineq += set->p[i]->n_ineq;
358 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
361 lp_dim = isl_basic_set_n_dim(lp);
362 k = isl_basic_set_alloc_equality(lp);
363 isl_int_set_si(lp->eq[k][0], -1);
364 for (i = 0; i < set->n; ++i) {
365 isl_int_set_si(lp->eq[k][1+dim*i], 0);
366 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
367 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
369 for (i = 0; i < set->n; ++i) {
370 k = isl_basic_set_alloc_inequality(lp);
371 isl_seq_clr(lp->ineq[k], 1+lp_dim);
372 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
374 for (j = 0; j < set->p[i]->n_eq; ++j) {
375 k = isl_basic_set_alloc_equality(lp);
376 isl_seq_clr(lp->eq[k], 1+dim*i);
377 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
378 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
381 for (j = 0; j < set->p[i]->n_ineq; ++j) {
382 k = isl_basic_set_alloc_inequality(lp);
383 isl_seq_clr(lp->ineq[k], 1+dim*i);
384 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
385 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
391 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
392 * of that facet, compute the other facet of the convex hull that contains
395 * We first transform the set such that the facet constraint becomes
399 * I.e., the facet lies in
403 * and on that facet, the constraint that defines the ridge is
407 * (This transformation is not strictly needed, all that is needed is
408 * that the ridge contains the origin.)
410 * Since the ridge contains the origin, the cone of the convex hull
411 * will be of the form
416 * with this second constraint defining the new facet.
417 * The constant a is obtained by settting x_1 in the cone of the
418 * convex hull to 1 and minimizing x_2.
419 * Now, each element in the cone of the convex hull is the sum
420 * of elements in the cones of the basic sets.
421 * If a_i is the dilation factor of basic set i, then the problem
422 * we need to solve is
435 * the constraints of each (transformed) basic set.
436 * If a = n/d, then the constraint defining the new facet (in the transformed
439 * -n x_1 + d x_2 >= 0
441 * In the original space, we need to take the same combination of the
442 * corresponding constraints "facet" and "ridge".
444 * Note that a is always finite, since we only apply the wrapping
445 * technique to a union of polytopes.
447 static isl_int *wrap_facet(struct isl_set *set, isl_int *facet, isl_int *ridge)
450 struct isl_mat *T = NULL;
451 struct isl_basic_set *lp = NULL;
453 enum isl_lp_result res;
457 set = isl_set_copy(set);
459 dim = 1 + isl_set_n_dim(set);
460 T = isl_mat_alloc(set->ctx, 3, dim);
463 isl_int_set_si(T->row[0][0], 1);
464 isl_seq_clr(T->row[0]+1, dim - 1);
465 isl_seq_cpy(T->row[1], facet, dim);
466 isl_seq_cpy(T->row[2], ridge, dim);
467 T = isl_mat_right_inverse(T);
468 set = isl_set_preimage(set, T);
472 lp = wrap_constraints(set);
473 obj = isl_vec_alloc(set->ctx, 1 + dim*set->n);
476 isl_int_set_si(obj->block.data[0], 0);
477 for (i = 0; i < set->n; ++i) {
478 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
479 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
480 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
484 res = isl_basic_set_solve_lp(lp, 0,
485 obj->block.data, set->ctx->one, &num, &den, NULL);
486 if (res == isl_lp_ok) {
487 isl_int_neg(num, num);
488 isl_seq_combine(facet, num, facet, den, ridge, dim);
493 isl_basic_set_free(lp);
495 isl_assert(set->ctx, res == isl_lp_ok, return NULL);
498 isl_basic_set_free(lp);
504 /* Given a set of d linearly independent bounding constraints of the
505 * convex hull of "set", compute the constraint of a facet of "set".
507 * We first compute the intersection with the first bounding hyperplane
508 * and remove the component corresponding to this hyperplane from
509 * other bounds (in homogeneous space).
510 * We then wrap around one of the remaining bounding constraints
511 * and continue the process until all bounding constraints have been
512 * taken into account.
513 * The resulting linear combination of the bounding constraints will
514 * correspond to a facet of the convex hull.
516 static struct isl_mat *initial_facet_constraint(struct isl_set *set,
517 struct isl_mat *bounds)
519 struct isl_set *slice = NULL;
520 struct isl_basic_set *face = NULL;
521 struct isl_mat *m, *U, *Q;
523 unsigned dim = isl_set_n_dim(set);
525 isl_assert(set->ctx, set->n > 0, goto error);
526 isl_assert(set->ctx, bounds->n_row == dim, goto error);
528 while (bounds->n_row > 1) {
529 slice = isl_set_copy(set);
530 slice = isl_set_add_equality(slice, bounds->row[0]);
531 face = isl_set_affine_hull(slice);
534 if (face->n_eq == 1) {
535 isl_basic_set_free(face);
538 m = isl_mat_alloc(set->ctx, 1 + face->n_eq, 1 + dim);
541 isl_int_set_si(m->row[0][0], 1);
542 isl_seq_clr(m->row[0]+1, dim);
543 for (i = 0; i < face->n_eq; ++i)
544 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
545 U = isl_mat_right_inverse(m);
546 Q = isl_mat_right_inverse(isl_mat_copy(U));
547 U = isl_mat_drop_cols(U, 1 + face->n_eq, dim - face->n_eq);
548 Q = isl_mat_drop_rows(Q, 1 + face->n_eq, dim - face->n_eq);
549 U = isl_mat_drop_cols(U, 0, 1);
550 Q = isl_mat_drop_rows(Q, 0, 1);
551 bounds = isl_mat_product(bounds, U);
552 bounds = isl_mat_product(bounds, Q);
553 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
554 bounds->n_col) == -1) {
556 isl_assert(set->ctx, bounds->n_row > 1, goto error);
558 if (!wrap_facet(set, bounds->row[0],
559 bounds->row[bounds->n_row-1]))
561 isl_basic_set_free(face);
566 isl_basic_set_free(face);
567 isl_mat_free(bounds);
571 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
572 * compute a hyperplane description of the facet, i.e., compute the facets
575 * We compute an affine transformation that transforms the constraint
584 * by computing the right inverse U of a matrix that starts with the rows
597 * Since z_1 is zero, we can drop this variable as well as the corresponding
598 * column of U to obtain
606 * with Q' equal to Q, but without the corresponding row.
607 * After computing the facets of the facet in the z' space,
608 * we convert them back to the x space through Q.
610 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
612 struct isl_mat *m, *U, *Q;
613 struct isl_basic_set *facet = NULL;
618 set = isl_set_copy(set);
619 dim = isl_set_n_dim(set);
620 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
623 isl_int_set_si(m->row[0][0], 1);
624 isl_seq_clr(m->row[0]+1, dim);
625 isl_seq_cpy(m->row[1], c, 1+dim);
626 U = isl_mat_right_inverse(m);
627 Q = isl_mat_right_inverse(isl_mat_copy(U));
628 U = isl_mat_drop_cols(U, 1, 1);
629 Q = isl_mat_drop_rows(Q, 1, 1);
630 set = isl_set_preimage(set, U);
631 facet = uset_convex_hull_wrap_bounded(set);
632 facet = isl_basic_set_preimage(facet, Q);
633 isl_assert(ctx, facet->n_eq == 0, goto error);
636 isl_basic_set_free(facet);
641 /* Given an initial facet constraint, compute the remaining facets.
642 * We do this by running through all facets found so far and computing
643 * the adjacent facets through wrapping, adding those facets that we
644 * hadn't already found before.
646 * For each facet we have found so far, we first compute its facets
647 * in the resulting convex hull. That is, we compute the ridges
648 * of the resulting convex hull contained in the facet.
649 * We also compute the corresponding facet in the current approximation
650 * of the convex hull. There is no need to wrap around the ridges
651 * in this facet since that would result in a facet that is already
652 * present in the current approximation.
654 * This function can still be significantly optimized by checking which of
655 * the facets of the basic sets are also facets of the convex hull and
656 * using all the facets so far to help in constructing the facets of the
659 * using the technique in section "3.1 Ridge Generation" of
660 * "Extended Convex Hull" by Fukuda et al.
662 static struct isl_basic_set *extend(struct isl_basic_set *hull,
667 struct isl_basic_set *facet = NULL;
668 struct isl_basic_set *hull_facet = NULL;
671 isl_assert(set->ctx, set->n > 0, goto error);
673 dim = isl_set_n_dim(set);
675 for (i = 0; i < hull->n_ineq; ++i) {
676 facet = compute_facet(set, hull->ineq[i]);
677 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
678 facet = isl_basic_set_gauss(facet, NULL);
679 facet = isl_basic_set_normalize_constraints(facet);
680 hull_facet = isl_basic_set_copy(hull);
681 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
682 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
683 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
686 hull = isl_basic_set_cow(hull);
687 hull = isl_basic_set_extend_dim(hull,
688 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
689 for (j = 0; j < facet->n_ineq; ++j) {
690 for (f = 0; f < hull_facet->n_ineq; ++f)
691 if (isl_seq_eq(facet->ineq[j],
692 hull_facet->ineq[f], 1 + dim))
694 if (f < hull_facet->n_ineq)
696 k = isl_basic_set_alloc_inequality(hull);
699 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
700 if (!wrap_facet(set, hull->ineq[k], facet->ineq[j]))
703 isl_basic_set_free(hull_facet);
704 isl_basic_set_free(facet);
706 hull = isl_basic_set_simplify(hull);
707 hull = isl_basic_set_finalize(hull);
710 isl_basic_set_free(hull_facet);
711 isl_basic_set_free(facet);
712 isl_basic_set_free(hull);
716 /* Special case for computing the convex hull of a one dimensional set.
717 * We simply collect the lower and upper bounds of each basic set
718 * and the biggest of those.
720 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
722 struct isl_mat *c = NULL;
723 isl_int *lower = NULL;
724 isl_int *upper = NULL;
727 struct isl_basic_set *hull;
729 for (i = 0; i < set->n; ++i) {
730 set->p[i] = isl_basic_set_simplify(set->p[i]);
734 set = isl_set_remove_empty_parts(set);
737 isl_assert(set->ctx, set->n > 0, goto error);
738 c = isl_mat_alloc(set->ctx, 2, 2);
742 if (set->p[0]->n_eq > 0) {
743 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
746 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
747 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
748 isl_seq_neg(upper, set->p[0]->eq[0], 2);
750 isl_seq_neg(lower, set->p[0]->eq[0], 2);
751 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
754 for (j = 0; j < set->p[0]->n_ineq; ++j) {
755 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
757 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
760 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
767 for (i = 0; i < set->n; ++i) {
768 struct isl_basic_set *bset = set->p[i];
772 for (j = 0; j < bset->n_eq; ++j) {
776 isl_int_mul(a, lower[0], bset->eq[j][1]);
777 isl_int_mul(b, lower[1], bset->eq[j][0]);
778 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
779 isl_seq_cpy(lower, bset->eq[j], 2);
780 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
781 isl_seq_neg(lower, bset->eq[j], 2);
784 isl_int_mul(a, upper[0], bset->eq[j][1]);
785 isl_int_mul(b, upper[1], bset->eq[j][0]);
786 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
787 isl_seq_neg(upper, bset->eq[j], 2);
788 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
789 isl_seq_cpy(upper, bset->eq[j], 2);
792 for (j = 0; j < bset->n_ineq; ++j) {
793 if (isl_int_is_pos(bset->ineq[j][1]))
795 if (isl_int_is_neg(bset->ineq[j][1]))
797 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
798 isl_int_mul(a, lower[0], bset->ineq[j][1]);
799 isl_int_mul(b, lower[1], bset->ineq[j][0]);
800 if (isl_int_lt(a, b))
801 isl_seq_cpy(lower, bset->ineq[j], 2);
803 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
804 isl_int_mul(a, upper[0], bset->ineq[j][1]);
805 isl_int_mul(b, upper[1], bset->ineq[j][0]);
806 if (isl_int_gt(a, b))
807 isl_seq_cpy(upper, bset->ineq[j], 2);
818 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
819 hull = isl_basic_set_set_rational(hull);
823 k = isl_basic_set_alloc_inequality(hull);
824 isl_seq_cpy(hull->ineq[k], lower, 2);
827 k = isl_basic_set_alloc_inequality(hull);
828 isl_seq_cpy(hull->ineq[k], upper, 2);
830 hull = isl_basic_set_finalize(hull);
840 /* Project out final n dimensions using Fourier-Motzkin */
841 static struct isl_set *set_project_out(struct isl_ctx *ctx,
842 struct isl_set *set, unsigned n)
844 return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
847 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
849 struct isl_basic_set *convex_hull;
854 if (isl_set_is_empty(set))
855 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
857 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
862 /* Compute the convex hull of a pair of basic sets without any parameters or
863 * integer divisions using Fourier-Motzkin elimination.
864 * The convex hull is the set of all points that can be written as
865 * the sum of points from both basic sets (in homogeneous coordinates).
866 * We set up the constraints in a space with dimensions for each of
867 * the three sets and then project out the dimensions corresponding
868 * to the two original basic sets, retaining only those corresponding
869 * to the convex hull.
871 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
872 struct isl_basic_set *bset2)
875 struct isl_basic_set *bset[2];
876 struct isl_basic_set *hull = NULL;
879 if (!bset1 || !bset2)
882 dim = isl_basic_set_n_dim(bset1);
883 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
884 1 + dim + bset1->n_eq + bset2->n_eq,
885 2 + bset1->n_ineq + bset2->n_ineq);
888 for (i = 0; i < 2; ++i) {
889 for (j = 0; j < bset[i]->n_eq; ++j) {
890 k = isl_basic_set_alloc_equality(hull);
893 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
894 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
895 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
898 for (j = 0; j < bset[i]->n_ineq; ++j) {
899 k = isl_basic_set_alloc_inequality(hull);
902 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
903 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
904 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
905 bset[i]->ineq[j], 1+dim);
907 k = isl_basic_set_alloc_inequality(hull);
910 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
911 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
913 for (j = 0; j < 1+dim; ++j) {
914 k = isl_basic_set_alloc_equality(hull);
917 isl_seq_clr(hull->eq[k], 1+2+3*dim);
918 isl_int_set_si(hull->eq[k][j], -1);
919 isl_int_set_si(hull->eq[k][1+dim+j], 1);
920 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
922 hull = isl_basic_set_set_rational(hull);
923 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
924 hull = isl_basic_set_convex_hull(hull);
925 isl_basic_set_free(bset1);
926 isl_basic_set_free(bset2);
929 isl_basic_set_free(bset1);
930 isl_basic_set_free(bset2);
931 isl_basic_set_free(hull);
935 static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
940 tab = isl_tab_from_recession_cone(bset);
941 bounded = isl_tab_cone_is_bounded(tab);
946 static int isl_set_is_bounded(struct isl_set *set)
950 for (i = 0; i < set->n; ++i) {
951 int bounded = isl_basic_set_is_bounded(set->p[i]);
952 if (!bounded || bounded < 0)
958 /* Compute the lineality space of the convex hull of bset1 and bset2.
960 * We first compute the intersection of the recession cone of bset1
961 * with the negative of the recession cone of bset2 and then compute
962 * the linear hull of the resulting cone.
964 static struct isl_basic_set *induced_lineality_space(
965 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
968 struct isl_basic_set *lin = NULL;
971 if (!bset1 || !bset2)
974 dim = isl_basic_set_total_dim(bset1);
975 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
976 bset1->n_eq + bset2->n_eq,
977 bset1->n_ineq + bset2->n_ineq);
978 lin = isl_basic_set_set_rational(lin);
981 for (i = 0; i < bset1->n_eq; ++i) {
982 k = isl_basic_set_alloc_equality(lin);
985 isl_int_set_si(lin->eq[k][0], 0);
986 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
988 for (i = 0; i < bset1->n_ineq; ++i) {
989 k = isl_basic_set_alloc_inequality(lin);
992 isl_int_set_si(lin->ineq[k][0], 0);
993 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
995 for (i = 0; i < bset2->n_eq; ++i) {
996 k = isl_basic_set_alloc_equality(lin);
999 isl_int_set_si(lin->eq[k][0], 0);
1000 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
1002 for (i = 0; i < bset2->n_ineq; ++i) {
1003 k = isl_basic_set_alloc_inequality(lin);
1006 isl_int_set_si(lin->ineq[k][0], 0);
1007 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
1010 isl_basic_set_free(bset1);
1011 isl_basic_set_free(bset2);
1012 return isl_basic_set_affine_hull(lin);
1014 isl_basic_set_free(lin);
1015 isl_basic_set_free(bset1);
1016 isl_basic_set_free(bset2);
1020 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1022 /* Given a set and a linear space "lin" of dimension n > 0,
1023 * project the linear space from the set, compute the convex hull
1024 * and then map the set back to the original space.
1030 * describe the linear space. We first compute the Hermite normal
1031 * form H = M U of M = H Q, to obtain
1035 * The last n rows of H will be zero, so the last n variables of x' = Q x
1036 * are the one we want to project out. We do this by transforming each
1037 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1038 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1039 * we transform the hull back to the original space as A' Q_1 x >= b',
1040 * with Q_1 all but the last n rows of Q.
1042 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1043 struct isl_basic_set *lin)
1045 unsigned total = isl_basic_set_total_dim(lin);
1047 struct isl_basic_set *hull;
1048 struct isl_mat *M, *U, *Q;
1052 lin_dim = total - lin->n_eq;
1053 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1054 M = isl_mat_left_hermite(M, 0, &U, &Q);
1058 isl_basic_set_free(lin);
1060 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1062 U = isl_mat_lin_to_aff(U);
1063 Q = isl_mat_lin_to_aff(Q);
1065 set = isl_set_preimage(set, U);
1066 set = isl_set_remove_dims(set, total - lin_dim, lin_dim);
1067 hull = uset_convex_hull(set);
1068 hull = isl_basic_set_preimage(hull, Q);
1072 isl_basic_set_free(lin);
1077 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1078 * set up an LP for solving
1080 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1082 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1083 * The next \alpha{ij} correspond to the equalities and come in pairs.
1084 * The final \alpha{ij} correspond to the inequalities.
1086 static struct isl_basic_set *valid_direction_lp(
1087 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1089 struct isl_dim *dim;
1090 struct isl_basic_set *lp;
1095 if (!bset1 || !bset2)
1097 d = 1 + isl_basic_set_total_dim(bset1);
1099 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1100 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1101 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1104 for (i = 0; i < n; ++i) {
1105 k = isl_basic_set_alloc_inequality(lp);
1108 isl_seq_clr(lp->ineq[k] + 1, n);
1109 isl_int_set_si(lp->ineq[k][0], -1);
1110 isl_int_set_si(lp->ineq[k][1 + i], 1);
1112 for (i = 0; i < d; ++i) {
1113 k = isl_basic_set_alloc_equality(lp);
1117 isl_int_set_si(lp->eq[k][n++], 0);
1118 /* positivity constraint 1 >= 0 */
1119 isl_int_set_si(lp->eq[k][n++], i == 0);
1120 for (j = 0; j < bset1->n_eq; ++j) {
1121 isl_int_set(lp->eq[k][n++], bset1->eq[j][i]);
1122 isl_int_neg(lp->eq[k][n++], bset1->eq[j][i]);
1124 for (j = 0; j < bset1->n_ineq; ++j)
1125 isl_int_set(lp->eq[k][n++], bset1->ineq[j][i]);
1126 /* positivity constraint 1 >= 0 */
1127 isl_int_set_si(lp->eq[k][n++], -(i == 0));
1128 for (j = 0; j < bset2->n_eq; ++j) {
1129 isl_int_neg(lp->eq[k][n++], bset2->eq[j][i]);
1130 isl_int_set(lp->eq[k][n++], bset2->eq[j][i]);
1132 for (j = 0; j < bset2->n_ineq; ++j)
1133 isl_int_neg(lp->eq[k][n++], bset2->ineq[j][i]);
1135 lp = isl_basic_set_gauss(lp, NULL);
1136 isl_basic_set_free(bset1);
1137 isl_basic_set_free(bset2);
1140 isl_basic_set_free(bset1);
1141 isl_basic_set_free(bset2);
1145 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1146 * for all rays in the homogeneous space of the two cones that correspond
1147 * to the input polyhedra bset1 and bset2.
1149 * We compute s as a vector that satisfies
1151 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1153 * with h_{ij} the normals of the facets of polyhedron i
1154 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1155 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1156 * We first set up an LP with as variables the \alpha{ij}.
1157 * In this formulateion, for each polyhedron i,
1158 * the first constraint is the positivity constraint, followed by pairs
1159 * of variables for the equalities, followed by variables for the inequalities.
1160 * We then simply pick a feasible solution and compute s using (*).
1162 * Note that we simply pick any valid direction and make no attempt
1163 * to pick a "good" or even the "best" valid direction.
1165 static struct isl_vec *valid_direction(
1166 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1168 struct isl_basic_set *lp;
1169 struct isl_tab *tab;
1170 struct isl_vec *sample = NULL;
1171 struct isl_vec *dir;
1176 if (!bset1 || !bset2)
1178 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1179 isl_basic_set_copy(bset2));
1180 tab = isl_tab_from_basic_set(lp);
1181 sample = isl_tab_get_sample_value(tab);
1183 isl_basic_set_free(lp);
1186 d = isl_basic_set_total_dim(bset1);
1187 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1190 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1192 /* positivity constraint 1 >= 0 */
1193 isl_int_set(dir->block.data[0], sample->block.data[n++]);
1194 for (i = 0; i < bset1->n_eq; ++i) {
1195 isl_int_sub(sample->block.data[n],
1196 sample->block.data[n], sample->block.data[n+1]);
1197 isl_seq_combine(dir->block.data,
1198 bset1->ctx->one, dir->block.data,
1199 sample->block.data[n], bset1->eq[i], 1 + d);
1203 for (i = 0; i < bset1->n_ineq; ++i)
1204 isl_seq_combine(dir->block.data,
1205 bset1->ctx->one, dir->block.data,
1206 sample->block.data[n++], bset1->ineq[i], 1 + d);
1207 isl_vec_free(sample);
1208 isl_seq_normalize(bset1->ctx, dir->block.data + 1, dir->size - 1);
1209 isl_basic_set_free(bset1);
1210 isl_basic_set_free(bset2);
1213 isl_vec_free(sample);
1214 isl_basic_set_free(bset1);
1215 isl_basic_set_free(bset2);
1219 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1220 * compute b_i' + A_i' x' >= 0, with
1222 * [ b_i A_i ] [ y' ] [ y' ]
1223 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1225 * In particular, add the "positivity constraint" and then perform
1228 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1235 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1236 k = isl_basic_set_alloc_inequality(bset);
1239 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1240 isl_int_set_si(bset->ineq[k][0], 1);
1241 bset = isl_basic_set_preimage(bset, T);
1245 isl_basic_set_free(bset);
1249 /* Compute the convex hull of a pair of basic sets without any parameters or
1250 * integer divisions, where the convex hull is known to be pointed,
1251 * but the basic sets may be unbounded.
1253 * We turn this problem into the computation of a convex hull of a pair
1254 * _bounded_ polyhedra by "changing the direction of the homogeneous
1255 * dimension". This idea is due to Matthias Koeppe.
1257 * Consider the cones in homogeneous space that correspond to the
1258 * input polyhedra. The rays of these cones are also rays of the
1259 * polyhedra if the coordinate that corresponds to the homogeneous
1260 * dimension is zero. That is, if the inner product of the rays
1261 * with the homogeneous direction is zero.
1262 * The cones in the homogeneous space can also be considered to
1263 * correspond to other pairs of polyhedra by chosing a different
1264 * homogeneous direction. To ensure that both of these polyhedra
1265 * are bounded, we need to make sure that all rays of the cones
1266 * correspond to vertices and not to rays.
1267 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1268 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1269 * The vector s is computed in valid_direction.
1271 * Note that we need to consider _all_ rays of the cones and not just
1272 * the rays that correspond to rays in the polyhedra. If we were to
1273 * only consider those rays and turn them into vertices, then we
1274 * may inadvertently turn some vertices into rays.
1276 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1277 * We therefore transform the two polyhedra such that the selected
1278 * direction is mapped onto this standard direction and then proceed
1279 * with the normal computation.
1280 * Let S be a non-singular square matrix with s as its first row,
1281 * then we want to map the polyhedra to the space
1283 * [ y' ] [ y ] [ y ] [ y' ]
1284 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1286 * We take S to be the unimodular completion of s to limit the growth
1287 * of the coefficients in the following computations.
1289 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1290 * We first move to the homogeneous dimension
1292 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1293 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1295 * Then we change directoin
1297 * [ b_i A_i ] [ y' ] [ y' ]
1298 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1300 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1301 * resulting in b' + A' x' >= 0, which we then convert back
1304 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1306 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1308 static struct isl_basic_set *convex_hull_pair_pointed(
1309 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1311 struct isl_ctx *ctx = NULL;
1312 struct isl_vec *dir = NULL;
1313 struct isl_mat *T = NULL;
1314 struct isl_mat *T2 = NULL;
1315 struct isl_basic_set *hull;
1316 struct isl_set *set;
1318 if (!bset1 || !bset2)
1321 dir = valid_direction(isl_basic_set_copy(bset1),
1322 isl_basic_set_copy(bset2));
1325 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1328 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1329 T = isl_mat_unimodular_complete(T, 1);
1330 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1332 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1333 bset2 = homogeneous_map(bset2, T2);
1334 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1335 set = isl_set_add(set, bset1);
1336 set = isl_set_add(set, bset2);
1337 hull = uset_convex_hull(set);
1338 hull = isl_basic_set_preimage(hull, T);
1345 isl_basic_set_free(bset1);
1346 isl_basic_set_free(bset2);
1350 /* Compute the convex hull of a pair of basic sets without any parameters or
1351 * integer divisions.
1353 * If the convex hull of the two basic sets would have a non-trivial
1354 * lineality space, we first project out this lineality space.
1356 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1357 struct isl_basic_set *bset2)
1359 struct isl_basic_set *lin;
1361 if (isl_basic_set_is_bounded(bset1) || isl_basic_set_is_bounded(bset2))
1362 return convex_hull_pair_pointed(bset1, bset2);
1364 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1365 isl_basic_set_copy(bset2));
1368 if (isl_basic_set_is_universe(lin)) {
1369 isl_basic_set_free(bset1);
1370 isl_basic_set_free(bset2);
1373 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1374 struct isl_set *set;
1375 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1376 set = isl_set_add(set, bset1);
1377 set = isl_set_add(set, bset2);
1378 return modulo_lineality(set, lin);
1380 isl_basic_set_free(lin);
1382 return convex_hull_pair_pointed(bset1, bset2);
1384 isl_basic_set_free(bset1);
1385 isl_basic_set_free(bset2);
1389 /* Compute the lineality space of a basic set.
1390 * We currently do not allow the basic set to have any divs.
1391 * We basically just drop the constants and turn every inequality
1394 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1397 struct isl_basic_set *lin = NULL;
1402 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1403 dim = isl_basic_set_total_dim(bset);
1405 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1408 for (i = 0; i < bset->n_eq; ++i) {
1409 k = isl_basic_set_alloc_equality(lin);
1412 isl_int_set_si(lin->eq[k][0], 0);
1413 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1415 lin = isl_basic_set_gauss(lin, NULL);
1418 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1419 k = isl_basic_set_alloc_equality(lin);
1422 isl_int_set_si(lin->eq[k][0], 0);
1423 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1424 lin = isl_basic_set_gauss(lin, NULL);
1428 isl_basic_set_free(bset);
1431 isl_basic_set_free(lin);
1432 isl_basic_set_free(bset);
1436 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1437 * "underlying" set "set".
1439 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1442 struct isl_set *lin = NULL;
1447 struct isl_dim *dim = isl_set_get_dim(set);
1449 return isl_basic_set_empty(dim);
1452 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1453 for (i = 0; i < set->n; ++i)
1454 lin = isl_set_add(lin,
1455 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1457 return isl_set_affine_hull(lin);
1460 /* Compute the convex hull of a set without any parameters or
1461 * integer divisions.
1462 * In each step, we combined two basic sets until only one
1463 * basic set is left.
1464 * The input basic sets are assumed not to have a non-trivial
1465 * lineality space. If any of the intermediate results has
1466 * a non-trivial lineality space, it is projected out.
1468 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1470 struct isl_basic_set *convex_hull = NULL;
1472 convex_hull = isl_set_copy_basic_set(set);
1473 set = isl_set_drop_basic_set(set, convex_hull);
1476 while (set->n > 0) {
1477 struct isl_basic_set *t;
1478 t = isl_set_copy_basic_set(set);
1481 set = isl_set_drop_basic_set(set, t);
1484 convex_hull = convex_hull_pair(convex_hull, t);
1487 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1490 if (isl_basic_set_is_universe(t)) {
1491 isl_basic_set_free(convex_hull);
1495 if (t->n_eq < isl_basic_set_total_dim(t)) {
1496 set = isl_set_add(set, convex_hull);
1497 return modulo_lineality(set, t);
1499 isl_basic_set_free(t);
1505 isl_basic_set_free(convex_hull);
1509 /* Compute an initial hull for wrapping containing a single initial
1510 * facet by first computing bounds on the set and then using these
1511 * bounds to construct an initial facet.
1512 * This function is a remnant of an older implementation where the
1513 * bounds were also used to check whether the set was bounded.
1514 * Since this function will now only be called when we know the
1515 * set to be bounded, the initial facet should probably be constructed
1516 * by simply using the coordinate directions instead.
1518 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1519 struct isl_set *set)
1521 struct isl_mat *bounds = NULL;
1527 bounds = independent_bounds(set);
1530 isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
1531 bounds = initial_facet_constraint(set, bounds);
1534 k = isl_basic_set_alloc_inequality(hull);
1537 dim = isl_set_n_dim(set);
1538 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1539 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1540 isl_mat_free(bounds);
1544 isl_basic_set_free(hull);
1545 isl_mat_free(bounds);
1549 struct max_constraint {
1555 static int max_constraint_equal(const void *entry, const void *val)
1557 struct max_constraint *a = (struct max_constraint *)entry;
1558 isl_int *b = (isl_int *)val;
1560 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1563 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1564 isl_int *con, unsigned len, int n, int ineq)
1566 struct isl_hash_table_entry *entry;
1567 struct max_constraint *c;
1570 c_hash = isl_seq_get_hash(con + 1, len);
1571 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1577 isl_hash_table_remove(ctx, table, entry);
1581 if (isl_int_gt(c->c->row[0][0], con[0]))
1583 if (isl_int_eq(c->c->row[0][0], con[0])) {
1588 c->c = isl_mat_cow(c->c);
1589 isl_int_set(c->c->row[0][0], con[0]);
1593 /* Check whether the constraint hash table "table" constains the constraint
1596 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1597 isl_int *con, unsigned len, int n)
1599 struct isl_hash_table_entry *entry;
1600 struct max_constraint *c;
1603 c_hash = isl_seq_get_hash(con + 1, len);
1604 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1611 return isl_int_eq(c->c->row[0][0], con[0]);
1614 /* Check for inequality constraints of a basic set without equalities
1615 * such that the same or more stringent copies of the constraint appear
1616 * in all of the basic sets. Such constraints are necessarily facet
1617 * constraints of the convex hull.
1619 * If the resulting basic set is by chance identical to one of
1620 * the basic sets in "set", then we know that this basic set contains
1621 * all other basic sets and is therefore the convex hull of set.
1622 * In this case we set *is_hull to 1.
1624 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1625 struct isl_set *set, int *is_hull)
1628 int min_constraints;
1630 struct max_constraint *constraints = NULL;
1631 struct isl_hash_table *table = NULL;
1636 for (i = 0; i < set->n; ++i)
1637 if (set->p[i]->n_eq == 0)
1641 min_constraints = set->p[i]->n_ineq;
1643 for (i = best + 1; i < set->n; ++i) {
1644 if (set->p[i]->n_eq != 0)
1646 if (set->p[i]->n_ineq >= min_constraints)
1648 min_constraints = set->p[i]->n_ineq;
1651 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1655 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1656 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1659 total = isl_dim_total(set->dim);
1660 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1661 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1662 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1663 if (!constraints[i].c)
1665 constraints[i].ineq = 1;
1667 for (i = 0; i < min_constraints; ++i) {
1668 struct isl_hash_table_entry *entry;
1670 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1671 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1672 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1675 isl_assert(hull->ctx, !entry->data, goto error);
1676 entry->data = &constraints[i];
1680 for (s = 0; s < set->n; ++s) {
1684 for (i = 0; i < set->p[s]->n_eq; ++i) {
1685 isl_int *eq = set->p[s]->eq[i];
1686 for (j = 0; j < 2; ++j) {
1687 isl_seq_neg(eq, eq, 1 + total);
1688 update_constraint(hull->ctx, table,
1692 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1693 isl_int *ineq = set->p[s]->ineq[i];
1694 update_constraint(hull->ctx, table, ineq, total, n,
1695 set->p[s]->n_eq == 0);
1700 for (i = 0; i < min_constraints; ++i) {
1701 if (constraints[i].count < n)
1703 if (!constraints[i].ineq)
1705 j = isl_basic_set_alloc_inequality(hull);
1708 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1711 for (s = 0; s < set->n; ++s) {
1712 if (set->p[s]->n_eq)
1714 if (set->p[s]->n_ineq != hull->n_ineq)
1716 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1717 isl_int *ineq = set->p[s]->ineq[i];
1718 if (!has_constraint(hull->ctx, table, ineq, total, n))
1721 if (i == set->p[s]->n_ineq)
1725 isl_hash_table_clear(table);
1726 for (i = 0; i < min_constraints; ++i)
1727 isl_mat_free(constraints[i].c);
1732 isl_hash_table_clear(table);
1735 for (i = 0; i < min_constraints; ++i)
1736 isl_mat_free(constraints[i].c);
1741 /* Create a template for the convex hull of "set" and fill it up
1742 * obvious facet constraints, if any. If the result happens to
1743 * be the convex hull of "set" then *is_hull is set to 1.
1745 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1747 struct isl_basic_set *hull;
1752 for (i = 0; i < set->n; ++i) {
1753 n_ineq += set->p[i]->n_eq;
1754 n_ineq += set->p[i]->n_ineq;
1756 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1757 hull = isl_basic_set_set_rational(hull);
1760 return common_constraints(hull, set, is_hull);
1763 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1765 struct isl_basic_set *hull;
1768 hull = proto_hull(set, &is_hull);
1769 if (hull && !is_hull) {
1770 if (hull->n_ineq == 0)
1771 hull = initial_hull(hull, set);
1772 hull = extend(hull, set);
1779 /* Compute the convex hull of a set without any parameters or
1780 * integer divisions. Depending on whether the set is bounded,
1781 * we pass control to the wrapping based convex hull or
1782 * the Fourier-Motzkin elimination based convex hull.
1783 * We also handle a few special cases before checking the boundedness.
1785 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1787 struct isl_basic_set *convex_hull = NULL;
1788 struct isl_basic_set *lin;
1790 if (isl_set_n_dim(set) == 0)
1791 return convex_hull_0d(set);
1793 set = isl_set_coalesce(set);
1794 set = isl_set_set_rational(set);
1801 convex_hull = isl_basic_set_copy(set->p[0]);
1805 if (isl_set_n_dim(set) == 1)
1806 return convex_hull_1d(set);
1808 if (isl_set_is_bounded(set))
1809 return uset_convex_hull_wrap(set);
1811 lin = uset_combined_lineality_space(isl_set_copy(set));
1814 if (isl_basic_set_is_universe(lin)) {
1818 if (lin->n_eq < isl_basic_set_total_dim(lin))
1819 return modulo_lineality(set, lin);
1820 isl_basic_set_free(lin);
1822 return uset_convex_hull_unbounded(set);
1825 isl_basic_set_free(convex_hull);
1829 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1830 * without parameters or divs and where the convex hull of set is
1831 * known to be full-dimensional.
1833 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1835 struct isl_basic_set *convex_hull = NULL;
1837 if (isl_set_n_dim(set) == 0) {
1838 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1840 convex_hull = isl_basic_set_set_rational(convex_hull);
1844 set = isl_set_set_rational(set);
1848 set = isl_set_coalesce(set);
1852 convex_hull = isl_basic_set_copy(set->p[0]);
1856 if (isl_set_n_dim(set) == 1)
1857 return convex_hull_1d(set);
1859 return uset_convex_hull_wrap(set);
1865 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1866 * We first remove the equalities (transforming the set), compute the
1867 * convex hull of the transformed set and then add the equalities back
1868 * (after performing the inverse transformation.
1870 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1871 struct isl_set *set, struct isl_basic_set *affine_hull)
1875 struct isl_basic_set *dummy;
1876 struct isl_basic_set *convex_hull;
1878 dummy = isl_basic_set_remove_equalities(
1879 isl_basic_set_copy(affine_hull), &T, &T2);
1882 isl_basic_set_free(dummy);
1883 set = isl_set_preimage(set, T);
1884 convex_hull = uset_convex_hull(set);
1885 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1886 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1889 isl_basic_set_free(affine_hull);
1894 /* Compute the convex hull of a map.
1896 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1897 * specifically, the wrapping of facets to obtain new facets.
1899 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1901 struct isl_basic_set *bset;
1902 struct isl_basic_map *model = NULL;
1903 struct isl_basic_set *affine_hull = NULL;
1904 struct isl_basic_map *convex_hull = NULL;
1905 struct isl_set *set = NULL;
1906 struct isl_ctx *ctx;
1913 convex_hull = isl_basic_map_empty_like_map(map);
1918 map = isl_map_detect_equalities(map);
1919 map = isl_map_align_divs(map);
1920 model = isl_basic_map_copy(map->p[0]);
1921 set = isl_map_underlying_set(map);
1925 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1928 if (affine_hull->n_eq != 0)
1929 bset = modulo_affine_hull(ctx, set, affine_hull);
1931 isl_basic_set_free(affine_hull);
1932 bset = uset_convex_hull(set);
1935 convex_hull = isl_basic_map_overlying_set(bset, model);
1937 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1938 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1939 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1943 isl_basic_map_free(model);
1947 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1949 return (struct isl_basic_set *)
1950 isl_map_convex_hull((struct isl_map *)set);
1953 struct sh_data_entry {
1954 struct isl_hash_table *table;
1955 struct isl_tab *tab;
1958 /* Holds the data needed during the simple hull computation.
1960 * n the number of basic sets in the original set
1961 * hull_table a hash table of already computed constraints
1962 * in the simple hull
1963 * p for each basic set,
1964 * table a hash table of the constraints
1965 * tab the tableau corresponding to the basic set
1968 struct isl_ctx *ctx;
1970 struct isl_hash_table *hull_table;
1971 struct sh_data_entry p[1];
1974 static void sh_data_free(struct sh_data *data)
1980 isl_hash_table_free(data->ctx, data->hull_table);
1981 for (i = 0; i < data->n; ++i) {
1982 isl_hash_table_free(data->ctx, data->p[i].table);
1983 isl_tab_free(data->p[i].tab);
1988 struct ineq_cmp_data {
1993 static int has_ineq(const void *entry, const void *val)
1995 isl_int *row = (isl_int *)entry;
1996 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
1998 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
1999 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2002 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2003 isl_int *ineq, unsigned len)
2006 struct ineq_cmp_data v;
2007 struct isl_hash_table_entry *entry;
2011 c_hash = isl_seq_get_hash(ineq + 1, len);
2012 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2019 /* Fill hash table "table" with the constraints of "bset".
2020 * Equalities are added as two inequalities.
2021 * The value in the hash table is a pointer to the (in)equality of "bset".
2023 static int hash_basic_set(struct isl_hash_table *table,
2024 struct isl_basic_set *bset)
2027 unsigned dim = isl_basic_set_total_dim(bset);
2029 for (i = 0; i < bset->n_eq; ++i) {
2030 for (j = 0; j < 2; ++j) {
2031 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2032 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2036 for (i = 0; i < bset->n_ineq; ++i) {
2037 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2043 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2045 struct sh_data *data;
2048 data = isl_calloc(set->ctx, struct sh_data,
2049 sizeof(struct sh_data) +
2050 (set->n - 1) * sizeof(struct sh_data_entry));
2053 data->ctx = set->ctx;
2055 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2056 if (!data->hull_table)
2058 for (i = 0; i < set->n; ++i) {
2059 data->p[i].table = isl_hash_table_alloc(set->ctx,
2060 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2061 if (!data->p[i].table)
2063 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2072 /* Check if inequality "ineq" is a bound for basic set "j" or if
2073 * it can be relaxed (by increasing the constant term) to become
2074 * a bound for that basic set. In the latter case, the constant
2076 * Return 1 if "ineq" is a bound
2077 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2078 * -1 if some error occurred
2080 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2083 enum isl_lp_result res;
2086 if (!data->p[j].tab) {
2087 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2088 if (!data->p[j].tab)
2094 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2096 if (res == isl_lp_ok && isl_int_is_neg(opt))
2097 isl_int_sub(ineq[0], ineq[0], opt);
2101 return res == isl_lp_ok ? 1 :
2102 res == isl_lp_unbounded ? 0 : -1;
2105 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2106 * become a bound on the whole set. If so, add the (relaxed) inequality
2109 * We first check if "hull" already contains a translate of the inequality.
2110 * If so, we are done.
2111 * Then, we check if any of the previous basic sets contains a translate
2112 * of the inequality. If so, then we have already considered this
2113 * inequality and we are done.
2114 * Otherwise, for each basic set other than "i", we check if the inequality
2115 * is a bound on the basic set.
2116 * For previous basic sets, we know that they do not contain a translate
2117 * of the inequality, so we directly call is_bound.
2118 * For following basic sets, we first check if a translate of the
2119 * inequality appears in its description and if so directly update
2120 * the inequality accordingly.
2122 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2123 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2126 struct ineq_cmp_data v;
2127 struct isl_hash_table_entry *entry;
2133 v.len = isl_basic_set_total_dim(hull);
2135 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2137 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2142 for (j = 0; j < i; ++j) {
2143 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2144 c_hash, has_ineq, &v, 0);
2151 k = isl_basic_set_alloc_inequality(hull);
2152 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2156 for (j = 0; j < i; ++j) {
2158 bound = is_bound(data, set, j, hull->ineq[k]);
2165 isl_basic_set_free_inequality(hull, 1);
2169 for (j = i + 1; j < set->n; ++j) {
2172 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2173 c_hash, has_ineq, &v, 0);
2175 ineq_j = entry->data;
2176 neg = isl_seq_is_neg(ineq_j + 1,
2177 hull->ineq[k] + 1, v.len);
2179 isl_int_neg(ineq_j[0], ineq_j[0]);
2180 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2181 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2183 isl_int_neg(ineq_j[0], ineq_j[0]);
2186 bound = is_bound(data, set, j, hull->ineq[k]);
2193 isl_basic_set_free_inequality(hull, 1);
2197 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2201 entry->data = hull->ineq[k];
2205 isl_basic_set_free(hull);
2209 /* Check if any inequality from basic set "i" can be relaxed to
2210 * become a bound on the whole set. If so, add the (relaxed) inequality
2213 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2214 struct sh_data *data, struct isl_set *set, int i)
2217 unsigned dim = isl_basic_set_total_dim(bset);
2219 for (j = 0; j < set->p[i]->n_eq; ++j) {
2220 for (k = 0; k < 2; ++k) {
2221 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2222 add_bound(bset, data, set, i, set->p[i]->eq[j]);
2225 for (j = 0; j < set->p[i]->n_ineq; ++j)
2226 add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2230 /* Compute a superset of the convex hull of set that is described
2231 * by only translates of the constraints in the constituents of set.
2233 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2235 struct sh_data *data = NULL;
2236 struct isl_basic_set *hull = NULL;
2244 for (i = 0; i < set->n; ++i) {
2247 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2250 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2254 data = sh_data_alloc(set, n_ineq);
2258 for (i = 0; i < set->n; ++i)
2259 hull = add_bounds(hull, data, set, i);
2267 isl_basic_set_free(hull);
2272 /* Compute a superset of the convex hull of map that is described
2273 * by only translates of the constraints in the constituents of map.
2275 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2277 struct isl_set *set = NULL;
2278 struct isl_basic_map *model = NULL;
2279 struct isl_basic_map *hull;
2280 struct isl_basic_map *affine_hull;
2281 struct isl_basic_set *bset = NULL;
2286 hull = isl_basic_map_empty_like_map(map);
2291 hull = isl_basic_map_copy(map->p[0]);
2296 map = isl_map_detect_equalities(map);
2297 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2298 map = isl_map_align_divs(map);
2299 model = isl_basic_map_copy(map->p[0]);
2301 set = isl_map_underlying_set(map);
2303 bset = uset_simple_hull(set);
2305 hull = isl_basic_map_overlying_set(bset, model);
2307 hull = isl_basic_map_intersect(hull, affine_hull);
2308 hull = isl_basic_map_convex_hull(hull);
2309 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2310 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2315 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2317 return (struct isl_basic_set *)
2318 isl_map_simple_hull((struct isl_map *)set);
2321 /* Given a set "set", return parametric bounds on the dimension "dim".
2323 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2325 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2326 set = isl_set_copy(set);
2327 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2328 set = isl_set_eliminate_dims(set, 0, dim);
2329 return isl_set_convex_hull(set);
2332 /* Computes a "simple hull" and then check if each dimension in the
2333 * resulting hull is bounded by a symbolic constant. If not, the
2334 * hull is intersected with the corresponding bounds on the whole set.
2336 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2339 struct isl_basic_set *hull;
2340 unsigned nparam, left;
2341 int removed_divs = 0;
2343 hull = isl_set_simple_hull(isl_set_copy(set));
2347 nparam = isl_basic_set_dim(hull, isl_dim_param);
2348 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2349 int lower = 0, upper = 0;
2350 struct isl_basic_set *bounds;
2352 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2353 for (j = 0; j < hull->n_eq; ++j) {
2354 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2356 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2363 for (j = 0; j < hull->n_ineq; ++j) {
2364 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2366 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2368 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2371 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2382 if (!removed_divs) {
2383 set = isl_set_remove_divs(set);
2388 bounds = set_bounds(set, i);
2389 hull = isl_basic_set_intersect(hull, bounds);