3 #include "isl_map_private.h"
7 #include "isl_equalities.h"
10 static struct isl_basic_set *uset_convex_hull_wrap(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_solve_lp(*bmap, 0, c+1, (*bmap)->ctx->one, opt_n, opt_d);
54 if (res == isl_lp_unbounded)
56 if (res == isl_lp_error)
58 if (res == isl_lp_empty) {
59 *bmap = isl_basic_map_set_to_empty(*bmap);
63 isl_int_addmul(*opt_n, *opt_d, c[0]);
65 isl_int_add(*opt_n, *opt_n, c[0]);
66 return !isl_int_is_neg(*opt_n);
69 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
70 isl_int *c, isl_int *opt_n, isl_int *opt_d)
72 return isl_basic_map_constraint_is_redundant(
73 (struct isl_basic_map **)bset, c, opt_n, opt_d);
76 /* Compute the convex hull of a basic map, by removing the redundant
77 * constraints. If the minimal value along the normal of a constraint
78 * is the same if the constraint is removed, then the constraint is redundant.
80 * Alternatively, we could have intersected the basic map with the
81 * corresponding equality and the checked if the dimension was that
84 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
91 bmap = isl_basic_map_gauss(bmap, NULL);
92 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
94 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
96 if (bmap->n_ineq <= 1)
99 tab = isl_tab_from_basic_map(bmap);
100 tab = isl_tab_detect_equalities(bmap->ctx, tab);
101 tab = isl_tab_detect_redundant(bmap->ctx, tab);
102 bmap = isl_basic_map_update_from_tab(bmap, tab);
103 isl_tab_free(bmap->ctx, tab);
104 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
105 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
109 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
111 return (struct isl_basic_set *)
112 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
115 /* Check if the set set is bound in the direction of the affine
116 * constraint c and if so, set the constant term such that the
117 * resulting constraint is a bounding constraint for the set.
119 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
120 isl_int *c, unsigned len)
128 isl_int_init(opt_denom);
130 for (j = 0; j < set->n; ++j) {
131 enum isl_lp_result res;
133 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
136 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
137 0, c+1, ctx->one, &opt, &opt_denom);
138 if (res == isl_lp_unbounded)
140 if (res == isl_lp_error)
142 if (res == isl_lp_empty) {
143 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
148 if (!isl_int_is_one(opt_denom))
149 isl_seq_scale(c, c, opt_denom, len);
150 if (first || isl_int_lt(opt, c[0]))
151 isl_int_set(c[0], opt);
155 isl_int_clear(opt_denom);
156 isl_int_neg(c[0], c[0]);
160 isl_int_clear(opt_denom);
164 /* Check if "c" is a direction with both a lower bound and an upper
165 * bound in "set" that is independent of the previously found "n"
167 * If so, add it to the list, with the negative of the lower bound
168 * in the constant position, i.e., such that c corresponds to a bounding
169 * hyperplane (but not necessarily a facet).
171 static int is_independent_bound(struct isl_ctx *ctx,
172 struct isl_set *set, isl_int *c,
173 struct isl_mat *dirs, int n)
178 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
180 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
183 for (i = 0; i < n; ++i) {
185 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
190 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
191 dirs->n_col-1, NULL);
192 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
198 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
199 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
200 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
203 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
208 isl_int *t = dirs->row[n];
209 for (k = n; k > i; --k)
210 dirs->row[k] = dirs->row[k-1];
216 /* Compute and return a maximal set of linearly independent bounds
217 * on the set "set", based on the constraints of the basic sets
220 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
224 struct isl_mat *dirs = NULL;
225 unsigned dim = isl_set_n_dim(set);
227 dirs = isl_mat_alloc(ctx, dim, 1+dim);
232 for (i = 0; n < dim && i < set->n; ++i) {
234 struct isl_basic_set *bset = set->p[i];
236 for (j = 0; n < dim && j < bset->n_eq; ++j) {
237 f = is_independent_bound(ctx, set, bset->eq[j],
244 for (j = 0; n < dim && j < bset->n_ineq; ++j) {
245 f = is_independent_bound(ctx, set, bset->ineq[j],
256 isl_mat_free(ctx, dirs);
260 static struct isl_basic_set *isl_basic_set_set_rational(
261 struct isl_basic_set *bset)
266 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
269 bset = isl_basic_set_cow(bset);
273 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
275 return isl_basic_set_finalize(bset);
278 static struct isl_set *isl_set_set_rational(struct isl_set *set)
282 set = isl_set_cow(set);
285 for (i = 0; i < set->n; ++i) {
286 set->p[i] = isl_basic_set_set_rational(set->p[i]);
296 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
297 struct isl_basic_set *bset, isl_int *c)
303 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
306 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
307 isl_assert(ctx, bset->n_div == 0, goto error);
308 dim = isl_basic_set_n_dim(bset);
309 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
310 i = isl_basic_set_alloc_equality(bset);
313 isl_seq_cpy(bset->eq[i], c, 1 + dim);
316 isl_basic_set_free(bset);
320 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
321 struct isl_set *set, isl_int *c)
325 set = isl_set_cow(set);
328 for (i = 0; i < set->n; ++i) {
329 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
339 /* Given a union of basic sets, construct the constraints for wrapping
340 * a facet around one of its ridges.
341 * In particular, if each of n the d-dimensional basic sets i in "set"
342 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
343 * and is defined by the constraints
347 * then the resulting set is of dimension n*(1+d) and has as contraints
356 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
359 struct isl_basic_set *lp;
363 unsigned dim, lp_dim;
368 dim = 1 + isl_set_n_dim(set);
371 for (i = 0; i < set->n; ++i) {
372 n_eq += set->p[i]->n_eq;
373 n_ineq += set->p[i]->n_ineq;
375 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
378 lp_dim = isl_basic_set_n_dim(lp);
379 k = isl_basic_set_alloc_equality(lp);
380 isl_int_set_si(lp->eq[k][0], -1);
381 for (i = 0; i < set->n; ++i) {
382 isl_int_set_si(lp->eq[k][1+dim*i], 0);
383 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
384 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
386 for (i = 0; i < set->n; ++i) {
387 k = isl_basic_set_alloc_inequality(lp);
388 isl_seq_clr(lp->ineq[k], 1+lp_dim);
389 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
391 for (j = 0; j < set->p[i]->n_eq; ++j) {
392 k = isl_basic_set_alloc_equality(lp);
393 isl_seq_clr(lp->eq[k], 1+dim*i);
394 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
395 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
398 for (j = 0; j < set->p[i]->n_ineq; ++j) {
399 k = isl_basic_set_alloc_inequality(lp);
400 isl_seq_clr(lp->ineq[k], 1+dim*i);
401 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
402 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
408 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
409 * of that facet, compute the other facet of the convex hull that contains
412 * We first transform the set such that the facet constraint becomes
416 * I.e., the facet lies in
420 * and on that facet, the constraint that defines the ridge is
424 * (This transformation is not strictly needed, all that is needed is
425 * that the ridge contains the origin.)
427 * Since the ridge contains the origin, the cone of the convex hull
428 * will be of the form
433 * with this second constraint defining the new facet.
434 * The constant a is obtained by settting x_1 in the cone of the
435 * convex hull to 1 and minimizing x_2.
436 * Now, each element in the cone of the convex hull is the sum
437 * of elements in the cones of the basic sets.
438 * If a_i is the dilation factor of basic set i, then the problem
439 * we need to solve is
452 * the constraints of each (transformed) basic set.
453 * If a = n/d, then the constraint defining the new facet (in the transformed
456 * -n x_1 + d x_2 >= 0
458 * In the original space, we need to take the same combination of the
459 * corresponding constraints "facet" and "ridge".
461 * If a = -infty = "-1/0", then we just return the original facet constraint.
462 * This means that the facet is unbounded, but has a bounded intersection
463 * with the union of sets.
465 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
466 isl_int *facet, isl_int *ridge)
469 struct isl_mat *T = NULL;
470 struct isl_basic_set *lp = NULL;
472 enum isl_lp_result res;
476 set = isl_set_copy(set);
478 dim = 1 + isl_set_n_dim(set);
479 T = isl_mat_alloc(ctx, 3, dim);
482 isl_int_set_si(T->row[0][0], 1);
483 isl_seq_clr(T->row[0]+1, dim - 1);
484 isl_seq_cpy(T->row[1], facet, dim);
485 isl_seq_cpy(T->row[2], ridge, dim);
486 T = isl_mat_right_inverse(ctx, T);
487 set = isl_set_preimage(set, T);
491 lp = wrap_constraints(ctx, set);
492 obj = isl_vec_alloc(ctx, dim*set->n);
495 for (i = 0; i < set->n; ++i) {
496 isl_seq_clr(obj->block.data+dim*i, 2);
497 isl_int_set_si(obj->block.data[dim*i+2], 1);
498 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
502 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
503 obj->block.data, ctx->one, &num, &den);
504 if (res == isl_lp_ok) {
505 isl_int_neg(num, num);
506 isl_seq_combine(facet, num, facet, den, ridge, dim);
510 isl_vec_free(ctx, obj);
511 isl_basic_set_free(lp);
513 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
517 isl_basic_set_free(lp);
518 isl_mat_free(ctx, T);
523 /* Given a set of d linearly independent bounding constraints of the
524 * convex hull of "set", compute the constraint of a facet of "set".
526 * We first compute the intersection with the first bounding hyperplane
527 * and remove the component corresponding to this hyperplane from
528 * other bounds (in homogeneous space).
529 * We then wrap around one of the remaining bounding constraints
530 * and continue the process until all bounding constraints have been
531 * taken into account.
532 * The resulting linear combination of the bounding constraints will
533 * correspond to a facet of the convex hull.
535 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
536 struct isl_set *set, struct isl_mat *bounds)
538 struct isl_set *slice = NULL;
539 struct isl_basic_set *face = NULL;
540 struct isl_mat *m, *U, *Q;
542 unsigned dim = isl_set_n_dim(set);
544 isl_assert(ctx, set->n > 0, goto error);
545 isl_assert(ctx, bounds->n_row == dim, goto error);
547 while (bounds->n_row > 1) {
548 slice = isl_set_copy(set);
549 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
550 face = isl_set_affine_hull(slice);
553 if (face->n_eq == 1) {
554 isl_basic_set_free(face);
557 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
560 isl_int_set_si(m->row[0][0], 1);
561 isl_seq_clr(m->row[0]+1, dim);
562 for (i = 0; i < face->n_eq; ++i)
563 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
564 U = isl_mat_right_inverse(ctx, m);
565 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
566 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
568 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
570 U = isl_mat_drop_cols(ctx, U, 0, 1);
571 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
572 bounds = isl_mat_product(ctx, bounds, U);
573 bounds = isl_mat_product(ctx, bounds, Q);
574 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
575 bounds->n_col) == -1) {
577 isl_assert(ctx, bounds->n_row > 1, goto error);
579 if (!wrap_facet(ctx, set, bounds->row[0],
580 bounds->row[bounds->n_row-1]))
582 isl_basic_set_free(face);
587 isl_basic_set_free(face);
588 isl_mat_free(ctx, bounds);
592 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
593 * compute a hyperplane description of the facet, i.e., compute the facets
596 * We compute an affine transformation that transforms the constraint
605 * by computing the right inverse U of a matrix that starts with the rows
618 * Since z_1 is zero, we can drop this variable as well as the corresponding
619 * column of U to obtain
627 * with Q' equal to Q, but without the corresponding row.
628 * After computing the facets of the facet in the z' space,
629 * we convert them back to the x space through Q.
631 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
632 struct isl_set *set, isl_int *c)
634 struct isl_mat *m, *U, *Q;
635 struct isl_basic_set *facet;
638 set = isl_set_copy(set);
639 dim = isl_set_n_dim(set);
640 m = isl_mat_alloc(ctx, 2, 1 + dim);
643 isl_int_set_si(m->row[0][0], 1);
644 isl_seq_clr(m->row[0]+1, dim);
645 isl_seq_cpy(m->row[1], c, 1+dim);
646 U = isl_mat_right_inverse(ctx, m);
647 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
648 U = isl_mat_drop_cols(ctx, U, 1, 1);
649 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
650 set = isl_set_preimage(set, U);
651 facet = uset_convex_hull_wrap(set);
652 facet = isl_basic_set_preimage(facet, Q);
659 /* Given an initial facet constraint, compute the remaining facets.
660 * We do this by running through all facets found so far and computing
661 * the adjacent facets through wrapping, adding those facets that we
662 * hadn't already found before.
664 * This function can still be significantly optimized by checking which of
665 * the facets of the basic sets are also facets of the convex hull and
666 * using all the facets so far to help in constructing the facets of the
669 * using the technique in section "3.1 Ridge Generation" of
670 * "Extended Convex Hull" by Fukuda et al.
672 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
673 struct isl_mat *initial)
677 struct isl_basic_set *hull = NULL;
678 struct isl_basic_set *facet = NULL;
683 isl_assert(ctx, set->n > 0, goto error);
686 for (i = 0; i < set->n; ++i) {
687 n_ineq += set->p[i]->n_eq;
688 n_ineq += set->p[i]->n_ineq;
690 dim = isl_set_n_dim(set);
691 isl_assert(ctx, 1 + dim == initial->n_col, goto error);
692 hull = isl_basic_set_alloc(ctx, 0, dim, 0, 0, n_ineq);
693 hull = isl_basic_set_set_rational(hull);
696 k = isl_basic_set_alloc_inequality(hull);
699 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
700 for (i = 0; i < hull->n_ineq; ++i) {
701 facet = compute_facet(ctx, set, hull->ineq[i]);
704 if (facet->n_ineq + hull->n_ineq > n_ineq) {
705 hull = isl_basic_set_extend(hull,
706 0, dim, 0, 0, facet->n_ineq);
707 n_ineq = hull->n_ineq + facet->n_ineq;
709 for (j = 0; j < facet->n_ineq; ++j) {
710 k = isl_basic_set_alloc_inequality(hull);
713 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
714 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
716 for (f = 0; f < k; ++f)
717 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
721 isl_basic_set_free_inequality(hull, 1);
723 isl_basic_set_free(facet);
725 hull = isl_basic_set_simplify(hull);
726 hull = isl_basic_set_finalize(hull);
729 isl_basic_set_free(facet);
730 isl_basic_set_free(hull);
734 /* Special case for computing the convex hull of a one dimensional set.
735 * We simply collect the lower and upper bounds of each basic set
736 * and the biggest of those.
738 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
741 struct isl_mat *c = NULL;
742 isl_int *lower = NULL;
743 isl_int *upper = NULL;
746 struct isl_basic_set *hull;
748 for (i = 0; i < set->n; ++i) {
749 set->p[i] = isl_basic_set_simplify(set->p[i]);
753 set = isl_set_remove_empty_parts(set);
756 isl_assert(ctx, set->n > 0, goto error);
757 c = isl_mat_alloc(ctx, 2, 2);
761 if (set->p[0]->n_eq > 0) {
762 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
765 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
766 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
767 isl_seq_neg(upper, set->p[0]->eq[0], 2);
769 isl_seq_neg(lower, set->p[0]->eq[0], 2);
770 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
773 for (j = 0; j < set->p[0]->n_ineq; ++j) {
774 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
776 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
779 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
786 for (i = 0; i < set->n; ++i) {
787 struct isl_basic_set *bset = set->p[i];
791 for (j = 0; j < bset->n_eq; ++j) {
795 isl_int_mul(a, lower[0], bset->eq[j][1]);
796 isl_int_mul(b, lower[1], bset->eq[j][0]);
797 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
798 isl_seq_cpy(lower, bset->eq[j], 2);
799 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
800 isl_seq_neg(lower, bset->eq[j], 2);
803 isl_int_mul(a, upper[0], bset->eq[j][1]);
804 isl_int_mul(b, upper[1], bset->eq[j][0]);
805 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
806 isl_seq_neg(upper, bset->eq[j], 2);
807 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
808 isl_seq_cpy(upper, bset->eq[j], 2);
811 for (j = 0; j < bset->n_ineq; ++j) {
812 if (isl_int_is_pos(bset->ineq[j][1]))
814 if (isl_int_is_neg(bset->ineq[j][1]))
816 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
817 isl_int_mul(a, lower[0], bset->ineq[j][1]);
818 isl_int_mul(b, lower[1], bset->ineq[j][0]);
819 if (isl_int_lt(a, b))
820 isl_seq_cpy(lower, bset->ineq[j], 2);
822 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
823 isl_int_mul(a, upper[0], bset->ineq[j][1]);
824 isl_int_mul(b, upper[1], bset->ineq[j][0]);
825 if (isl_int_gt(a, b))
826 isl_seq_cpy(upper, bset->ineq[j], 2);
837 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
838 hull = isl_basic_set_set_rational(hull);
842 k = isl_basic_set_alloc_inequality(hull);
843 isl_seq_cpy(hull->ineq[k], lower, 2);
846 k = isl_basic_set_alloc_inequality(hull);
847 isl_seq_cpy(hull->ineq[k], upper, 2);
849 hull = isl_basic_set_finalize(hull);
851 isl_mat_free(ctx, c);
855 isl_mat_free(ctx, c);
859 /* Project out final n dimensions using Fourier-Motzkin */
860 static struct isl_set *set_project_out(struct isl_ctx *ctx,
861 struct isl_set *set, unsigned n)
863 return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
866 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
868 struct isl_basic_set *convex_hull;
873 if (isl_set_is_empty(set))
874 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
876 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
881 /* Compute the convex hull of a pair of basic sets without any parameters or
882 * integer divisions using Fourier-Motzkin elimination.
883 * The convex hull is the set of all points that can be written as
884 * the sum of points from both basic sets (in homogeneous coordinates).
885 * We set up the constraints in a space with dimensions for each of
886 * the three sets and then project out the dimensions corresponding
887 * to the two original basic sets, retaining only those corresponding
888 * to the convex hull.
890 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
891 struct isl_basic_set *bset2)
894 struct isl_basic_set *bset[2];
895 struct isl_basic_set *hull = NULL;
898 if (!bset1 || !bset2)
901 dim = isl_basic_set_n_dim(bset1);
902 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
903 1 + dim + bset1->n_eq + bset2->n_eq,
904 2 + bset1->n_ineq + bset2->n_ineq);
907 for (i = 0; i < 2; ++i) {
908 for (j = 0; j < bset[i]->n_eq; ++j) {
909 k = isl_basic_set_alloc_equality(hull);
912 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
913 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
914 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
917 for (j = 0; j < bset[i]->n_ineq; ++j) {
918 k = isl_basic_set_alloc_inequality(hull);
921 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
922 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
923 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
924 bset[i]->ineq[j], 1+dim);
926 k = isl_basic_set_alloc_inequality(hull);
929 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
930 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
932 for (j = 0; j < 1+dim; ++j) {
933 k = isl_basic_set_alloc_equality(hull);
936 isl_seq_clr(hull->eq[k], 1+2+3*dim);
937 isl_int_set_si(hull->eq[k][j], -1);
938 isl_int_set_si(hull->eq[k][1+dim+j], 1);
939 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
941 hull = isl_basic_set_set_rational(hull);
942 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
943 hull = isl_basic_set_convex_hull(hull);
944 isl_basic_set_free(bset1);
945 isl_basic_set_free(bset2);
948 isl_basic_set_free(bset1);
949 isl_basic_set_free(bset2);
950 isl_basic_set_free(hull);
954 /* Compute the convex hull of a set without any parameters or
955 * integer divisions using Fourier-Motzkin elimination.
956 * In each step, we combined two basic sets until only one
959 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
961 struct isl_basic_set *convex_hull = NULL;
963 convex_hull = isl_set_copy_basic_set(set);
964 set = isl_set_drop_basic_set(set, convex_hull);
968 struct isl_basic_set *t;
969 t = isl_set_copy_basic_set(set);
972 set = isl_set_drop_basic_set(set, t);
975 convex_hull = convex_hull_pair(convex_hull, t);
981 isl_basic_set_free(convex_hull);
985 static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
986 struct isl_set *set, struct isl_mat *bounds)
988 struct isl_basic_set *convex_hull = NULL;
990 isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
991 bounds = initial_facet_constraint(set->ctx, set, bounds);
994 convex_hull = extend(set->ctx, set, bounds);
995 isl_mat_free(set->ctx, bounds);
1004 /* Compute the convex hull of a set without any parameters or
1005 * integer divisions. Depending on whether the set is bounded,
1006 * we pass control to the wrapping based convex hull or
1007 * the Fourier-Motzkin elimination based convex hull.
1008 * We also handle a few special cases before checking the boundedness.
1010 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1013 struct isl_basic_set *convex_hull = NULL;
1014 struct isl_mat *bounds;
1016 if (isl_set_n_dim(set) == 0)
1017 return convex_hull_0d(set);
1019 set = isl_set_set_rational(set);
1023 set = isl_set_normalize(set);
1027 convex_hull = isl_basic_set_copy(set->p[0]);
1031 if (isl_set_n_dim(set) == 1)
1032 return convex_hull_1d(set->ctx, set);
1034 bounds = independent_bounds(set->ctx, set);
1037 if (bounds->n_row == isl_set_n_dim(set))
1038 return uset_convex_hull_wrap_with_bounds(set, bounds);
1039 isl_mat_free(set->ctx, bounds);
1041 return uset_convex_hull_elim(set);
1044 isl_basic_set_free(convex_hull);
1048 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1049 * without parameters or divs and where the convex hull of set is
1050 * known to be full-dimensional.
1052 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1055 struct isl_basic_set *convex_hull = NULL;
1056 struct isl_mat *bounds;
1058 if (isl_set_n_dim(set) == 0) {
1059 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1061 convex_hull = isl_basic_set_set_rational(convex_hull);
1065 set = isl_set_set_rational(set);
1069 set = isl_set_normalize(set);
1073 convex_hull = isl_basic_set_copy(set->p[0]);
1077 if (isl_set_n_dim(set) == 1)
1078 return convex_hull_1d(set->ctx, set);
1080 bounds = independent_bounds(set->ctx, set);
1083 return uset_convex_hull_wrap_with_bounds(set, bounds);
1089 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1090 * We first remove the equalities (transforming the set), compute the
1091 * convex hull of the transformed set and then add the equalities back
1092 * (after performing the inverse transformation.
1094 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1095 struct isl_set *set, struct isl_basic_set *affine_hull)
1099 struct isl_basic_set *dummy;
1100 struct isl_basic_set *convex_hull;
1102 dummy = isl_basic_set_remove_equalities(
1103 isl_basic_set_copy(affine_hull), &T, &T2);
1106 isl_basic_set_free(dummy);
1107 set = isl_set_preimage(set, T);
1108 convex_hull = uset_convex_hull(set);
1109 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1110 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1113 isl_basic_set_free(affine_hull);
1118 /* Compute the convex hull of a map.
1120 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1121 * specifically, the wrapping of facets to obtain new facets.
1123 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1125 struct isl_basic_set *bset;
1126 struct isl_basic_map *model = NULL;
1127 struct isl_basic_set *affine_hull = NULL;
1128 struct isl_basic_map *convex_hull = NULL;
1129 struct isl_set *set = NULL;
1130 struct isl_ctx *ctx;
1137 convex_hull = isl_basic_map_empty_like_map(map);
1142 map = isl_map_align_divs(map);
1143 model = isl_basic_map_copy(map->p[0]);
1144 set = isl_map_underlying_set(map);
1148 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1151 if (affine_hull->n_eq != 0)
1152 bset = modulo_affine_hull(ctx, set, affine_hull);
1154 isl_basic_set_free(affine_hull);
1155 bset = uset_convex_hull(set);
1158 convex_hull = isl_basic_map_overlying_set(bset, model);
1160 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1164 isl_basic_map_free(model);
1168 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1170 return (struct isl_basic_set *)
1171 isl_map_convex_hull((struct isl_map *)set);
1174 /* Compute a superset of the convex hull of map that is described
1175 * by only translates of the constraints in the constituents of map.
1177 * The implementation is not very efficient. In particular, if
1178 * constraints with the same normal appear in more than one
1179 * basic map, they will be (re)examined each time.
1181 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1183 struct isl_set *set = NULL;
1184 struct isl_basic_map *model = NULL;
1185 struct isl_basic_map *hull;
1186 struct isl_basic_set *bset = NULL;
1194 hull = isl_basic_map_empty_like_map(map);
1199 hull = isl_basic_map_copy(map->p[0]);
1204 map = isl_map_align_divs(map);
1205 model = isl_basic_map_copy(map->p[0]);
1208 for (i = 0; i < map->n; ++i) {
1211 n_ineq += map->p[i]->n_ineq;
1214 set = isl_map_underlying_set(map);
1218 bset = isl_set_affine_hull(isl_set_copy(set));
1221 dim = isl_basic_set_n_dim(bset);
1222 bset = isl_basic_set_extend(bset, 0, dim, 0, 0, n_ineq);
1226 for (i = 0; i < set->n; ++i) {
1227 for (j = 0; j < set->p[i]->n_ineq; ++j) {
1231 k = isl_basic_set_alloc_inequality(bset);
1234 isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j], 1 + dim);
1235 is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
1240 isl_basic_set_free_inequality(bset, 1);
1244 bset = isl_basic_set_simplify(bset);
1245 bset = isl_basic_set_finalize(bset);
1246 bset = isl_basic_set_convex_hull(bset);
1248 hull = isl_basic_map_overlying_set(bset, model);
1253 isl_basic_set_free(bset);
1255 isl_basic_map_free(model);
1259 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1261 return (struct isl_basic_set *)
1262 isl_map_simple_hull((struct isl_map *)set);