3 #include "isl_map_private.h"
7 #include "isl_equalities.h"
9 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
11 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
17 bmap->ineq[i] = bmap->ineq[j];
22 /* Return 1 if constraint c is redundant with respect to the constraints
23 * in bmap. If c is a lower [upper] bound in some variable and bmap
24 * does not have a lower [upper] bound in that variable, then c cannot
25 * be redundant and we do not need solve any lp.
27 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
28 isl_int *c, isl_int *opt_n, isl_int *opt_d)
30 enum isl_lp_result res;
37 total = (*bmap)->nparam + (*bmap)->n_in + (*bmap)->n_out + (*bmap)->n_div;
38 for (i = 0; i < total; ++i) {
40 if (isl_int_is_zero(c[1+i]))
42 sign = isl_int_sgn(c[1+i]);
43 for (j = 0; j < (*bmap)->n_ineq; ++j)
44 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
46 if (j == (*bmap)->n_ineq)
52 res = isl_solve_lp(*bmap, 0, c+1, (*bmap)->ctx->one, opt_n, opt_d);
53 if (res == isl_lp_unbounded)
55 if (res == isl_lp_error)
57 if (res == isl_lp_empty) {
58 *bmap = isl_basic_map_set_to_empty(*bmap);
61 isl_int_addmul(*opt_n, *opt_d, c[0]);
62 return !isl_int_is_neg(*opt_n);
65 /* Compute the convex hull of a basic map, by removing the redundant
66 * constraints. If the minimal value along the normal of a constraint
67 * is the same if the constraint is removed, then the constraint is redundant.
69 * Alternatively, we could have intersected the basic map with the
70 * corresponding equality and the checked if the dimension was that
73 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
80 bmap = isl_basic_map_implicit_equalities(bmap);
84 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
86 if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
92 for (i = bmap->n_ineq-1; i >= 0; --i) {
94 swap_ineq(bmap, i, bmap->n_ineq-1);
96 redundant = isl_basic_map_constraint_is_redundant(&bmap,
97 bmap->ineq[bmap->n_ineq], &opt_n, &opt_d);
100 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
103 swap_ineq(bmap, i, bmap->n_ineq-1);
105 isl_basic_map_drop_inequality(bmap, i);
107 isl_int_clear(opt_n);
108 isl_int_clear(opt_d);
110 F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
113 isl_int_clear(opt_n);
114 isl_int_clear(opt_d);
115 isl_basic_map_free(bmap);
119 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
121 return (struct isl_basic_set *)
122 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
125 /* Check if the set set is bound in the direction of the affine
126 * constraint c and if so, set the constant term such that the
127 * resulting constraint is a bounding constraint for the set.
129 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
130 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 (F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
146 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
147 0, c+1, ctx->one, &opt, &opt_denom);
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_lt(opt, c[0]))
161 isl_int_set(c[0], opt);
165 isl_int_clear(opt_denom);
166 isl_int_neg(c[0], c[0]);
170 isl_int_clear(opt_denom);
174 /* Check if "c" is a direction with both a lower bound and an upper
175 * bound in "set" that is independent of the previously found "n"
177 * If so, add it to the list, with the negative of the lower bound
178 * in the constant position, i.e., such that c corresponds to a bounding
179 * hyperplane (but not necessarily a facet).
181 static int is_independent_bound(struct isl_ctx *ctx,
182 struct isl_set *set, isl_int *c,
183 struct isl_mat *dirs, int n)
188 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
190 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
193 for (i = 0; i < n; ++i) {
195 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
200 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
201 dirs->n_col-1, NULL);
202 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
208 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
209 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
210 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
213 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
218 isl_int *t = dirs->row[n];
219 for (k = n; k > i; --k)
220 dirs->row[k] = dirs->row[k-1];
226 /* Compute and return a maximal set of linearly independent bounds
227 * on the set "set", based on the constraints of the basic sets
230 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
234 struct isl_mat *dirs = NULL;
236 dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
241 for (i = 0; n < set->dim && i < set->n; ++i) {
243 struct isl_basic_set *bset = set->p[i];
245 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
246 f = is_independent_bound(ctx, set, bset->eq[j],
253 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
254 f = is_independent_bound(ctx, set, bset->ineq[j],
265 isl_mat_free(ctx, dirs);
269 static struct isl_basic_set *isl_basic_set_set_rational(
270 struct isl_basic_set *bset)
275 if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
278 bset = isl_basic_set_cow(bset);
282 F_SET(bset, ISL_BASIC_MAP_RATIONAL);
284 return isl_basic_set_finalize(bset);
287 static struct isl_set *isl_set_set_rational(struct isl_set *set)
291 set = isl_set_cow(set);
294 for (i = 0; i < set->n; ++i) {
295 set->p[i] = isl_basic_set_set_rational(set->p[i]);
305 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
306 struct isl_basic_set *bset, isl_int *c)
311 if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
314 isl_assert(ctx, bset->nparam == 0, goto error);
315 isl_assert(ctx, bset->n_div == 0, goto error);
316 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
317 i = isl_basic_set_alloc_equality(bset);
320 isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
323 isl_basic_set_free(bset);
327 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
328 struct isl_set *set, isl_int *c)
332 set = isl_set_cow(set);
335 for (i = 0; i < set->n; ++i) {
336 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
346 /* Given a union of basic sets, construct the constraints for wrapping
347 * a facet around one of its ridges.
348 * In particular, if each of n the d-dimensional basic sets i in "set"
349 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
350 * and is defined by the constraints
354 * then the resulting set is of dimension n*(1+d) and has as contraints
363 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
366 struct isl_basic_set *lp;
378 for (i = 0; i < set->n; ++i) {
379 n_eq += set->p[i]->n_eq;
380 n_ineq += set->p[i]->n_ineq;
382 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
385 k = isl_basic_set_alloc_equality(lp);
386 isl_int_set_si(lp->eq[k][0], -1);
387 for (i = 0; i < set->n; ++i) {
388 isl_int_set_si(lp->eq[k][1+dim*i], 0);
389 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
390 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
392 for (i = 0; i < set->n; ++i) {
393 k = isl_basic_set_alloc_inequality(lp);
394 isl_seq_clr(lp->ineq[k], 1+lp->dim);
395 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
397 for (j = 0; j < set->p[i]->n_eq; ++j) {
398 k = isl_basic_set_alloc_equality(lp);
399 isl_seq_clr(lp->eq[k], 1+dim*i);
400 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
401 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
404 for (j = 0; j < set->p[i]->n_ineq; ++j) {
405 k = isl_basic_set_alloc_inequality(lp);
406 isl_seq_clr(lp->ineq[k], 1+dim*i);
407 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
408 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
414 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
415 * of that facet, compute the other facet of the convex hull that contains
418 * We first transform the set such that the facet constraint becomes
422 * I.e., the facet lies in
426 * and on that facet, the constraint that defines the ridge is
430 * (This transformation is not strictly needed, all that is needed is
431 * that the ridge contains the origin.)
433 * Since the ridge contains the origin, the cone of the convex hull
434 * will be of the form
439 * with this second constraint defining the new facet.
440 * The constant a is obtained by settting x_1 in the cone of the
441 * convex hull to 1 and minimizing x_2.
442 * Now, each element in the cone of the convex hull is the sum
443 * of elements in the cones of the basic sets.
444 * If a_i is the dilation factor of basic set i, then the problem
445 * we need to solve is
458 * the constraints of each (transformed) basic set.
459 * If a = n/d, then the constraint defining the new facet (in the transformed
462 * -n x_1 + d x_2 >= 0
464 * In the original space, we need to take the same combination of the
465 * corresponding constraints "facet" and "ridge".
467 * If a = -infty = "-1/0", then we just return the original facet constraint.
468 * This means that the facet is unbounded, but has a bounded intersection
469 * with the union of sets.
471 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
472 isl_int *facet, isl_int *ridge)
475 struct isl_mat *T = NULL;
476 struct isl_basic_set *lp = NULL;
478 enum isl_lp_result res;
482 set = isl_set_copy(set);
485 T = isl_mat_alloc(ctx, 3, 1 + set->dim);
488 isl_int_set_si(T->row[0][0], 1);
489 isl_seq_clr(T->row[0]+1, set->dim);
490 isl_seq_cpy(T->row[1], facet, 1+set->dim);
491 isl_seq_cpy(T->row[2], ridge, 1+set->dim);
492 T = isl_mat_right_inverse(ctx, T);
493 set = isl_set_preimage(ctx, set, T);
497 lp = wrap_constraints(ctx, set);
498 obj = isl_vec_alloc(ctx, dim*set->n);
501 for (i = 0; i < set->n; ++i) {
502 isl_seq_clr(obj->block.data+dim*i, 2);
503 isl_int_set_si(obj->block.data[dim*i+2], 1);
504 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
508 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
509 obj->block.data, ctx->one, &num, &den);
510 if (res == isl_lp_ok) {
511 isl_int_neg(num, num);
512 isl_seq_combine(facet, num, facet, den, ridge, dim);
516 isl_vec_free(ctx, obj);
517 isl_basic_set_free(lp);
519 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
523 isl_basic_set_free(lp);
524 isl_mat_free(ctx, T);
529 /* Given a set of d linearly independent bounding constraints of the
530 * convex hull of "set", compute the constraint of a facet of "set".
532 * We first compute the intersection with the first bounding hyperplane
533 * and remove the component corresponding to this hyperplane from
534 * other bounds (in homogeneous space).
535 * We then wrap around one of the remaining bounding constraints
536 * and continue the process until all bounding constraints have been
537 * taken into account.
538 * The resulting linear combination of the bounding constraints will
539 * correspond to a facet of the convex hull.
541 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
542 struct isl_set *set, struct isl_mat *bounds)
544 struct isl_set *slice = NULL;
545 struct isl_basic_set *face = NULL;
546 struct isl_mat *m, *U, *Q;
549 isl_assert(ctx, set->n > 0, goto error);
550 isl_assert(ctx, bounds->n_row == set->dim, goto error);
552 while (bounds->n_row > 1) {
553 slice = isl_set_copy(set);
554 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
555 face = isl_set_affine_hull(slice);
558 if (face->n_eq == 1) {
559 isl_basic_set_free(face);
562 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
565 isl_int_set_si(m->row[0][0], 1);
566 isl_seq_clr(m->row[0]+1, face->dim);
567 for (i = 0; i < face->n_eq; ++i)
568 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
569 U = isl_mat_right_inverse(ctx, m);
570 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
571 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
572 face->dim - face->n_eq);
573 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
574 face->dim - face->n_eq);
575 U = isl_mat_drop_cols(ctx, U, 0, 1);
576 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
577 bounds = isl_mat_product(ctx, bounds, U);
578 bounds = isl_mat_product(ctx, bounds, Q);
579 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
580 bounds->n_col) == -1) {
582 isl_assert(ctx, bounds->n_row > 1, goto error);
584 if (!wrap_facet(ctx, set, bounds->row[0],
585 bounds->row[bounds->n_row-1]))
587 isl_basic_set_free(face);
592 isl_basic_set_free(face);
593 isl_mat_free(ctx, bounds);
597 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
598 * compute a hyperplane description of the facet, i.e., compute the facets
601 * We compute an affine transformation that transforms the constraint
610 * by computing the right inverse U of a matrix that starts with the rows
623 * Since z_1 is zero, we can drop this variable as well as the corresponding
624 * column of U to obtain
632 * with Q' equal to Q, but without the corresponding row.
633 * After computing the facets of the facet in the z' space,
634 * we convert them back to the x space through Q.
636 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
637 struct isl_set *set, isl_int *c)
639 struct isl_mat *m, *U, *Q;
640 struct isl_basic_set *facet;
642 set = isl_set_copy(set);
643 m = isl_mat_alloc(ctx, 2, 1 + set->dim);
646 isl_int_set_si(m->row[0][0], 1);
647 isl_seq_clr(m->row[0]+1, set->dim);
648 isl_seq_cpy(m->row[1], c, 1+set->dim);
649 U = isl_mat_right_inverse(ctx, m);
650 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
651 U = isl_mat_drop_cols(ctx, U, 1, 1);
652 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
653 set = isl_set_preimage(ctx, set, U);
654 facet = uset_convex_hull_wrap(set);
655 facet = isl_basic_set_preimage(ctx, facet, Q);
662 /* Given an initial facet constraint, compute the remaining facets.
663 * We do this by running through all facets found so far and computing
664 * the adjacent facets through wrapping, adding those facets that we
665 * hadn't already found before.
667 * This function can still be significantly optimized by checking which of
668 * the facets of the basic sets are also facets of the convex hull and
669 * using all the facets so far to help in constructing the facets of the
672 * using the technique in section "3.1 Ridge Generation" of
673 * "Extended Convex Hull" by Fukuda et al.
675 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
676 struct isl_mat *initial)
680 struct isl_basic_set *hull = NULL;
681 struct isl_basic_set *facet = NULL;
685 isl_assert(ctx, set->n > 0, goto error);
688 for (i = 0; i < set->n; ++i) {
689 n_ineq += set->p[i]->n_eq;
690 n_ineq += set->p[i]->n_ineq;
692 isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
693 hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
694 hull = isl_basic_set_set_rational(hull);
697 k = isl_basic_set_alloc_inequality(hull);
700 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
701 for (i = 0; i < hull->n_ineq; ++i) {
702 facet = compute_facet(ctx, set, hull->ineq[i]);
705 if (facet->n_ineq + hull->n_ineq > n_ineq) {
706 hull = isl_basic_set_extend(hull,
707 hull->nparam, hull->dim, 0, 0, facet->n_ineq);
708 n_ineq = hull->n_ineq + facet->n_ineq;
710 for (j = 0; j < facet->n_ineq; ++j) {
711 k = isl_basic_set_alloc_inequality(hull);
714 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
715 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
717 for (f = 0; f < k; ++f)
718 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
722 isl_basic_set_free_inequality(hull, 1);
724 isl_basic_set_free(facet);
726 hull = isl_basic_set_simplify(hull);
727 hull = isl_basic_set_finalize(hull);
730 isl_basic_set_free(facet);
731 isl_basic_set_free(hull);
735 /* Special case for computing the convex hull of a one dimensional set.
736 * We simply collect the lower and upper bounds of each basic set
737 * and the biggest of those.
739 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
742 struct isl_mat *c = NULL;
743 isl_int *lower = NULL;
744 isl_int *upper = NULL;
747 struct isl_basic_set *hull;
749 for (i = 0; i < set->n; ++i) {
750 set->p[i] = isl_basic_set_simplify(set->p[i]);
754 set = isl_set_remove_empty_parts(set);
757 isl_assert(ctx, set->n > 0, goto error);
758 c = isl_mat_alloc(ctx, 2, 2);
762 if (set->p[0]->n_eq > 0) {
763 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
766 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
767 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
768 isl_seq_neg(upper, set->p[0]->eq[0], 2);
770 isl_seq_neg(lower, set->p[0]->eq[0], 2);
771 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
774 for (j = 0; j < set->p[0]->n_ineq; ++j) {
775 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
777 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
780 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
787 for (i = 0; i < set->n; ++i) {
788 struct isl_basic_set *bset = set->p[i];
792 for (j = 0; j < bset->n_eq; ++j) {
796 isl_int_mul(a, lower[0], bset->eq[j][1]);
797 isl_int_mul(b, lower[1], bset->eq[j][0]);
798 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
799 isl_seq_cpy(lower, bset->eq[j], 2);
800 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
801 isl_seq_neg(lower, bset->eq[j], 2);
804 isl_int_mul(a, upper[0], bset->eq[j][1]);
805 isl_int_mul(b, upper[1], bset->eq[j][0]);
806 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
807 isl_seq_neg(upper, bset->eq[j], 2);
808 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
809 isl_seq_cpy(upper, bset->eq[j], 2);
812 for (j = 0; j < bset->n_ineq; ++j) {
813 if (isl_int_is_pos(bset->ineq[j][1]))
815 if (isl_int_is_neg(bset->ineq[j][1]))
817 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
818 isl_int_mul(a, lower[0], bset->ineq[j][1]);
819 isl_int_mul(b, lower[1], bset->ineq[j][0]);
820 if (isl_int_lt(a, b))
821 isl_seq_cpy(lower, bset->ineq[j], 2);
823 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
824 isl_int_mul(a, upper[0], bset->ineq[j][1]);
825 isl_int_mul(b, upper[1], bset->ineq[j][0]);
826 if (isl_int_gt(a, b))
827 isl_seq_cpy(upper, bset->ineq[j], 2);
838 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
839 hull = isl_basic_set_set_rational(hull);
843 k = isl_basic_set_alloc_inequality(hull);
844 isl_seq_cpy(hull->ineq[k], lower, 2);
847 k = isl_basic_set_alloc_inequality(hull);
848 isl_seq_cpy(hull->ineq[k], upper, 2);
850 hull = isl_basic_set_finalize(hull);
852 isl_mat_free(ctx, c);
856 isl_mat_free(ctx, c);
860 /* Project out final n dimensions using Fourier-Motzkin */
861 static struct isl_set *set_project_out(struct isl_ctx *ctx,
862 struct isl_set *set, unsigned n)
864 return isl_set_remove_dims(set, set->dim - n, n);
867 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
869 struct isl_basic_set *convex_hull;
874 if (isl_set_is_empty(set))
875 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
877 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
882 /* Compute the convex hull of a pair of basic sets without any parameters or
883 * integer divisions using Fourier-Motzkin elimination.
884 * The convex hull is the set of all points that can be written as
885 * the sum of points from both basic sets (in homogeneous coordinates).
886 * We set up the constraints in a space with dimensions for each of
887 * the three sets and then project out the dimensions corresponding
888 * to the two original basic sets, retaining only those corresponding
889 * to the convex hull.
891 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
892 struct isl_basic_set *bset2)
895 struct isl_basic_set *bset[2];
896 struct isl_basic_set *hull = NULL;
899 if (!bset1 || !bset2)
903 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * bset1->dim, 0,
904 1 + bset1->dim + bset1->n_eq + bset2->n_eq,
905 2 + bset1->n_ineq + bset2->n_ineq);
908 for (i = 0; i < 2; ++i) {
909 for (j = 0; j < bset[i]->n_eq; ++j) {
910 k = isl_basic_set_alloc_equality(hull);
913 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
914 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
915 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
918 for (j = 0; j < bset[i]->n_ineq; ++j) {
919 k = isl_basic_set_alloc_inequality(hull);
922 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
923 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
924 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
925 bset[i]->ineq[j], 1+dim);
927 k = isl_basic_set_alloc_inequality(hull);
930 isl_seq_clr(hull->ineq[k], 1+hull->dim);
931 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
933 for (j = 0; j < 1+dim; ++j) {
934 k = isl_basic_set_alloc_equality(hull);
937 isl_seq_clr(hull->eq[k], 1+hull->dim);
938 isl_int_set_si(hull->eq[k][j], -1);
939 isl_int_set_si(hull->eq[k][1+dim+j], 1);
940 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
942 hull = isl_basic_set_set_rational(hull);
943 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
944 hull = isl_basic_set_convex_hull(hull);
945 isl_basic_set_free(bset1);
946 isl_basic_set_free(bset2);
949 isl_basic_set_free(bset1);
950 isl_basic_set_free(bset2);
951 isl_basic_set_free(hull);
955 /* Compute the convex hull of a set without any parameters or
956 * integer divisions using Fourier-Motzkin elimination.
957 * In each step, we combined two basic sets until only one
960 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
962 struct isl_basic_set *convex_hull = NULL;
964 convex_hull = isl_set_copy_basic_set(set);
965 set = isl_set_drop_basic_set(set, convex_hull);
969 struct isl_basic_set *t;
970 t = isl_set_copy_basic_set(set);
973 set = isl_set_drop_basic_set(set, t);
976 convex_hull = convex_hull_pair(convex_hull, t);
982 isl_basic_set_free(convex_hull);
986 static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
987 struct isl_set *set, struct isl_mat *bounds)
989 struct isl_basic_set *convex_hull = NULL;
991 isl_assert(set->ctx, bounds->n_row == set->dim, goto error);
992 bounds = initial_facet_constraint(set->ctx, set, bounds);
995 convex_hull = extend(set->ctx, set, bounds);
996 isl_mat_free(set->ctx, bounds);
1005 /* Compute the convex hull of a set without any parameters or
1006 * integer divisions. Depending on whether the set is bounded,
1007 * we pass control to the wrapping based convex hull or
1008 * the Fourier-Motzkin elimination based convex hull.
1009 * We also handle a few special cases before checking the boundedness.
1011 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1014 struct isl_basic_set *convex_hull = NULL;
1015 struct isl_mat *bounds;
1018 return convex_hull_0d(set);
1020 set = isl_set_set_rational(set);
1024 for (i = 0; i < set->n; ++i) {
1025 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
1029 set = isl_set_remove_empty_parts(set);
1033 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
1038 convex_hull = isl_basic_set_copy(set->p[0]);
1043 return convex_hull_1d(set->ctx, set);
1045 bounds = independent_bounds(set->ctx, set);
1048 if (bounds->n_row == set->dim)
1049 return uset_convex_hull_wrap_with_bounds(set, bounds);
1050 isl_mat_free(set->ctx, bounds);
1052 return uset_convex_hull_elim(set);
1055 isl_basic_set_free(convex_hull);
1059 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1060 * without parameters or divs and where the convex hull of set is
1061 * known to be full-dimensional.
1063 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1066 struct isl_basic_set *convex_hull = NULL;
1067 struct isl_mat *bounds;
1069 if (set->dim == 0) {
1070 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
1072 convex_hull = isl_basic_set_set_rational(convex_hull);
1076 set = isl_set_set_rational(set);
1080 for (i = 0; i < set->n; ++i) {
1081 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
1085 set = isl_set_remove_empty_parts(set);
1089 convex_hull = isl_basic_set_copy(set->p[0]);
1094 return convex_hull_1d(set->ctx, set);
1096 bounds = independent_bounds(set->ctx, set);
1099 return uset_convex_hull_wrap_with_bounds(set, bounds);
1105 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1106 * We first remove the equalities (transforming the set), compute the
1107 * convex hull of the transformed set and then add the equalities back
1108 * (after performing the inverse transformation.
1110 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1111 struct isl_set *set, struct isl_basic_set *affine_hull)
1115 struct isl_basic_set *dummy;
1116 struct isl_basic_set *convex_hull;
1118 dummy = isl_basic_set_remove_equalities(
1119 isl_basic_set_copy(affine_hull), &T, &T2);
1122 isl_basic_set_free(dummy);
1123 set = isl_set_preimage(ctx, set, T);
1124 convex_hull = uset_convex_hull(set);
1125 convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
1126 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1129 isl_basic_set_free(affine_hull);
1134 /* Compute the convex hull of a map.
1136 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1137 * specifically, the wrapping of facets to obtain new facets.
1139 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1141 struct isl_basic_set *bset;
1142 struct isl_basic_set *affine_hull = NULL;
1143 struct isl_basic_map *convex_hull = NULL;
1144 struct isl_set *set = NULL;
1145 struct isl_ctx *ctx;
1152 convex_hull = isl_basic_map_empty(ctx,
1153 map->nparam, map->n_in, map->n_out);
1158 set = isl_map_underlying_set(isl_map_copy(map));
1162 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1165 if (affine_hull->n_eq != 0)
1166 bset = modulo_affine_hull(ctx, set, affine_hull);
1168 isl_basic_set_free(affine_hull);
1169 bset = uset_convex_hull(set);
1172 convex_hull = isl_basic_map_overlying_set(bset,
1173 isl_basic_map_copy(map->p[0]));
1176 F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1184 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1186 return (struct isl_basic_set *)
1187 isl_map_convex_hull((struct isl_map *)set);
1190 /* Compute a superset of the convex hull of map that is described
1191 * by only translates of the constraints in the constituents of map.
1193 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1195 struct isl_set *set = NULL;
1196 struct isl_basic_map *hull;
1197 struct isl_basic_set *bset = NULL;
1202 hull = isl_basic_map_empty(map->ctx,
1203 map->nparam, map->n_in, map->n_out);
1208 hull = isl_basic_map_copy(map->p[0]);
1214 for (i = 0; i < map->n; ++i) {
1217 n_ineq += map->p[i]->n_ineq;
1220 set = isl_map_underlying_set(isl_map_copy(map));
1224 bset = isl_set_affine_hull(isl_set_copy(set));
1227 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 0, n_ineq);
1231 for (i = 0; i < set->n; ++i) {
1232 for (j = 0; j < set->p[i]->n_ineq; ++j) {
1236 k = isl_basic_set_alloc_inequality(bset);
1239 isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j],
1241 is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
1246 isl_basic_set_free_inequality(bset, 1);
1250 bset = isl_basic_set_simplify(bset);
1251 bset = isl_basic_set_finalize(bset);
1252 bset = isl_basic_set_convex_hull(bset);
1254 hull = isl_basic_map_overlying_set(bset, isl_basic_map_copy(map->p[0]));
1260 isl_basic_set_free(bset);
1266 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1268 return (struct isl_basic_set *)
1269 isl_map_simple_hull((struct isl_map *)set);