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);
62 isl_int_addmul(*opt_n, *opt_d, c[0]);
64 isl_int_add(*opt_n, *opt_n, c[0]);
65 return !isl_int_is_neg(*opt_n);
68 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
69 isl_int *c, isl_int *opt_n, isl_int *opt_d)
71 return isl_basic_map_constraint_is_redundant(
72 (struct isl_basic_map **)bset, c, opt_n, opt_d);
75 /* Compute the convex hull of a basic map, by removing the redundant
76 * constraints. If the minimal value along the normal of a constraint
77 * is the same if the constraint is removed, then the constraint is redundant.
79 * Alternatively, we could have intersected the basic map with the
80 * corresponding equality and the checked if the dimension was that
83 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
90 bmap = isl_basic_map_implicit_equalities(bmap);
94 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
96 if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
102 for (i = bmap->n_ineq-1; i >= 0; --i) {
104 swap_ineq(bmap, i, bmap->n_ineq-1);
106 redundant = isl_basic_map_constraint_is_redundant(&bmap,
107 bmap->ineq[bmap->n_ineq], &opt_n, &opt_d);
110 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
113 swap_ineq(bmap, i, bmap->n_ineq-1);
115 isl_basic_map_drop_inequality(bmap, i);
117 isl_int_clear(opt_n);
118 isl_int_clear(opt_d);
120 F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
123 isl_int_clear(opt_n);
124 isl_int_clear(opt_d);
125 isl_basic_map_free(bmap);
129 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
131 return (struct isl_basic_set *)
132 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
135 /* Check if the set set is bound in the direction of the affine
136 * constraint c and if so, set the constant term such that the
137 * resulting constraint is a bounding constraint for the set.
139 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
140 isl_int *c, unsigned len)
148 isl_int_init(opt_denom);
150 for (j = 0; j < set->n; ++j) {
151 enum isl_lp_result res;
153 if (F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
156 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
157 0, c+1, ctx->one, &opt, &opt_denom);
158 if (res == isl_lp_unbounded)
160 if (res == isl_lp_error)
162 if (res == isl_lp_empty) {
163 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
168 if (!isl_int_is_one(opt_denom))
169 isl_seq_scale(c, c, opt_denom, len);
170 if (first || isl_int_lt(opt, c[0]))
171 isl_int_set(c[0], opt);
175 isl_int_clear(opt_denom);
176 isl_int_neg(c[0], c[0]);
180 isl_int_clear(opt_denom);
184 /* Check if "c" is a direction with both a lower bound and an upper
185 * bound in "set" that is independent of the previously found "n"
187 * If so, add it to the list, with the negative of the lower bound
188 * in the constant position, i.e., such that c corresponds to a bounding
189 * hyperplane (but not necessarily a facet).
191 static int is_independent_bound(struct isl_ctx *ctx,
192 struct isl_set *set, isl_int *c,
193 struct isl_mat *dirs, int n)
198 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
200 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
203 for (i = 0; i < n; ++i) {
205 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
210 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
211 dirs->n_col-1, NULL);
212 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
218 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
219 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
220 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
223 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
228 isl_int *t = dirs->row[n];
229 for (k = n; k > i; --k)
230 dirs->row[k] = dirs->row[k-1];
236 /* Compute and return a maximal set of linearly independent bounds
237 * on the set "set", based on the constraints of the basic sets
240 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
244 struct isl_mat *dirs = NULL;
246 dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
251 for (i = 0; n < set->dim && i < set->n; ++i) {
253 struct isl_basic_set *bset = set->p[i];
255 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
256 f = is_independent_bound(ctx, set, bset->eq[j],
263 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
264 f = is_independent_bound(ctx, set, bset->ineq[j],
275 isl_mat_free(ctx, dirs);
279 static struct isl_basic_set *isl_basic_set_set_rational(
280 struct isl_basic_set *bset)
285 if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
288 bset = isl_basic_set_cow(bset);
292 F_SET(bset, ISL_BASIC_MAP_RATIONAL);
294 return isl_basic_set_finalize(bset);
297 static struct isl_set *isl_set_set_rational(struct isl_set *set)
301 set = isl_set_cow(set);
304 for (i = 0; i < set->n; ++i) {
305 set->p[i] = isl_basic_set_set_rational(set->p[i]);
315 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
316 struct isl_basic_set *bset, isl_int *c)
321 if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
324 isl_assert(ctx, bset->nparam == 0, goto error);
325 isl_assert(ctx, bset->n_div == 0, goto error);
326 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
327 i = isl_basic_set_alloc_equality(bset);
330 isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
333 isl_basic_set_free(bset);
337 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
338 struct isl_set *set, isl_int *c)
342 set = isl_set_cow(set);
345 for (i = 0; i < set->n; ++i) {
346 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
356 /* Given a union of basic sets, construct the constraints for wrapping
357 * a facet around one of its ridges.
358 * In particular, if each of n the d-dimensional basic sets i in "set"
359 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
360 * and is defined by the constraints
364 * then the resulting set is of dimension n*(1+d) and has as contraints
373 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
376 struct isl_basic_set *lp;
388 for (i = 0; i < set->n; ++i) {
389 n_eq += set->p[i]->n_eq;
390 n_ineq += set->p[i]->n_ineq;
392 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
395 k = isl_basic_set_alloc_equality(lp);
396 isl_int_set_si(lp->eq[k][0], -1);
397 for (i = 0; i < set->n; ++i) {
398 isl_int_set_si(lp->eq[k][1+dim*i], 0);
399 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
400 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
402 for (i = 0; i < set->n; ++i) {
403 k = isl_basic_set_alloc_inequality(lp);
404 isl_seq_clr(lp->ineq[k], 1+lp->dim);
405 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
407 for (j = 0; j < set->p[i]->n_eq; ++j) {
408 k = isl_basic_set_alloc_equality(lp);
409 isl_seq_clr(lp->eq[k], 1+dim*i);
410 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
411 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
414 for (j = 0; j < set->p[i]->n_ineq; ++j) {
415 k = isl_basic_set_alloc_inequality(lp);
416 isl_seq_clr(lp->ineq[k], 1+dim*i);
417 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
418 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
424 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
425 * of that facet, compute the other facet of the convex hull that contains
428 * We first transform the set such that the facet constraint becomes
432 * I.e., the facet lies in
436 * and on that facet, the constraint that defines the ridge is
440 * (This transformation is not strictly needed, all that is needed is
441 * that the ridge contains the origin.)
443 * Since the ridge contains the origin, the cone of the convex hull
444 * will be of the form
449 * with this second constraint defining the new facet.
450 * The constant a is obtained by settting x_1 in the cone of the
451 * convex hull to 1 and minimizing x_2.
452 * Now, each element in the cone of the convex hull is the sum
453 * of elements in the cones of the basic sets.
454 * If a_i is the dilation factor of basic set i, then the problem
455 * we need to solve is
468 * the constraints of each (transformed) basic set.
469 * If a = n/d, then the constraint defining the new facet (in the transformed
472 * -n x_1 + d x_2 >= 0
474 * In the original space, we need to take the same combination of the
475 * corresponding constraints "facet" and "ridge".
477 * If a = -infty = "-1/0", then we just return the original facet constraint.
478 * This means that the facet is unbounded, but has a bounded intersection
479 * with the union of sets.
481 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
482 isl_int *facet, isl_int *ridge)
485 struct isl_mat *T = NULL;
486 struct isl_basic_set *lp = NULL;
488 enum isl_lp_result res;
492 set = isl_set_copy(set);
495 T = isl_mat_alloc(ctx, 3, 1 + set->dim);
498 isl_int_set_si(T->row[0][0], 1);
499 isl_seq_clr(T->row[0]+1, set->dim);
500 isl_seq_cpy(T->row[1], facet, 1+set->dim);
501 isl_seq_cpy(T->row[2], ridge, 1+set->dim);
502 T = isl_mat_right_inverse(ctx, T);
503 set = isl_set_preimage(ctx, set, T);
507 lp = wrap_constraints(ctx, set);
508 obj = isl_vec_alloc(ctx, dim*set->n);
511 for (i = 0; i < set->n; ++i) {
512 isl_seq_clr(obj->block.data+dim*i, 2);
513 isl_int_set_si(obj->block.data[dim*i+2], 1);
514 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
518 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
519 obj->block.data, ctx->one, &num, &den);
520 if (res == isl_lp_ok) {
521 isl_int_neg(num, num);
522 isl_seq_combine(facet, num, facet, den, ridge, dim);
526 isl_vec_free(ctx, obj);
527 isl_basic_set_free(lp);
529 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
533 isl_basic_set_free(lp);
534 isl_mat_free(ctx, T);
539 /* Given a set of d linearly independent bounding constraints of the
540 * convex hull of "set", compute the constraint of a facet of "set".
542 * We first compute the intersection with the first bounding hyperplane
543 * and remove the component corresponding to this hyperplane from
544 * other bounds (in homogeneous space).
545 * We then wrap around one of the remaining bounding constraints
546 * and continue the process until all bounding constraints have been
547 * taken into account.
548 * The resulting linear combination of the bounding constraints will
549 * correspond to a facet of the convex hull.
551 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
552 struct isl_set *set, struct isl_mat *bounds)
554 struct isl_set *slice = NULL;
555 struct isl_basic_set *face = NULL;
556 struct isl_mat *m, *U, *Q;
559 isl_assert(ctx, set->n > 0, goto error);
560 isl_assert(ctx, bounds->n_row == set->dim, goto error);
562 while (bounds->n_row > 1) {
563 slice = isl_set_copy(set);
564 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
565 face = isl_set_affine_hull(slice);
568 if (face->n_eq == 1) {
569 isl_basic_set_free(face);
572 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
575 isl_int_set_si(m->row[0][0], 1);
576 isl_seq_clr(m->row[0]+1, face->dim);
577 for (i = 0; i < face->n_eq; ++i)
578 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
579 U = isl_mat_right_inverse(ctx, m);
580 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
581 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
582 face->dim - face->n_eq);
583 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
584 face->dim - face->n_eq);
585 U = isl_mat_drop_cols(ctx, U, 0, 1);
586 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
587 bounds = isl_mat_product(ctx, bounds, U);
588 bounds = isl_mat_product(ctx, bounds, Q);
589 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
590 bounds->n_col) == -1) {
592 isl_assert(ctx, bounds->n_row > 1, goto error);
594 if (!wrap_facet(ctx, set, bounds->row[0],
595 bounds->row[bounds->n_row-1]))
597 isl_basic_set_free(face);
602 isl_basic_set_free(face);
603 isl_mat_free(ctx, bounds);
607 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
608 * compute a hyperplane description of the facet, i.e., compute the facets
611 * We compute an affine transformation that transforms the constraint
620 * by computing the right inverse U of a matrix that starts with the rows
633 * Since z_1 is zero, we can drop this variable as well as the corresponding
634 * column of U to obtain
642 * with Q' equal to Q, but without the corresponding row.
643 * After computing the facets of the facet in the z' space,
644 * we convert them back to the x space through Q.
646 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
647 struct isl_set *set, isl_int *c)
649 struct isl_mat *m, *U, *Q;
650 struct isl_basic_set *facet;
652 set = isl_set_copy(set);
653 m = isl_mat_alloc(ctx, 2, 1 + set->dim);
656 isl_int_set_si(m->row[0][0], 1);
657 isl_seq_clr(m->row[0]+1, set->dim);
658 isl_seq_cpy(m->row[1], c, 1+set->dim);
659 U = isl_mat_right_inverse(ctx, m);
660 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
661 U = isl_mat_drop_cols(ctx, U, 1, 1);
662 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
663 set = isl_set_preimage(ctx, set, U);
664 facet = uset_convex_hull_wrap(set);
665 facet = isl_basic_set_preimage(ctx, facet, Q);
672 /* Given an initial facet constraint, compute the remaining facets.
673 * We do this by running through all facets found so far and computing
674 * the adjacent facets through wrapping, adding those facets that we
675 * hadn't already found before.
677 * This function can still be significantly optimized by checking which of
678 * the facets of the basic sets are also facets of the convex hull and
679 * using all the facets so far to help in constructing the facets of the
682 * using the technique in section "3.1 Ridge Generation" of
683 * "Extended Convex Hull" by Fukuda et al.
685 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
686 struct isl_mat *initial)
690 struct isl_basic_set *hull = NULL;
691 struct isl_basic_set *facet = NULL;
695 isl_assert(ctx, set->n > 0, goto error);
698 for (i = 0; i < set->n; ++i) {
699 n_ineq += set->p[i]->n_eq;
700 n_ineq += set->p[i]->n_ineq;
702 isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
703 hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
704 hull = isl_basic_set_set_rational(hull);
707 k = isl_basic_set_alloc_inequality(hull);
710 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
711 for (i = 0; i < hull->n_ineq; ++i) {
712 facet = compute_facet(ctx, set, hull->ineq[i]);
715 if (facet->n_ineq + hull->n_ineq > n_ineq) {
716 hull = isl_basic_set_extend(hull,
717 hull->nparam, hull->dim, 0, 0, facet->n_ineq);
718 n_ineq = hull->n_ineq + facet->n_ineq;
720 for (j = 0; j < facet->n_ineq; ++j) {
721 k = isl_basic_set_alloc_inequality(hull);
724 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
725 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
727 for (f = 0; f < k; ++f)
728 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
732 isl_basic_set_free_inequality(hull, 1);
734 isl_basic_set_free(facet);
736 hull = isl_basic_set_simplify(hull);
737 hull = isl_basic_set_finalize(hull);
740 isl_basic_set_free(facet);
741 isl_basic_set_free(hull);
745 /* Special case for computing the convex hull of a one dimensional set.
746 * We simply collect the lower and upper bounds of each basic set
747 * and the biggest of those.
749 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
752 struct isl_mat *c = NULL;
753 isl_int *lower = NULL;
754 isl_int *upper = NULL;
757 struct isl_basic_set *hull;
759 for (i = 0; i < set->n; ++i) {
760 set->p[i] = isl_basic_set_simplify(set->p[i]);
764 set = isl_set_remove_empty_parts(set);
767 isl_assert(ctx, set->n > 0, goto error);
768 c = isl_mat_alloc(ctx, 2, 2);
772 if (set->p[0]->n_eq > 0) {
773 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
776 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
777 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
778 isl_seq_neg(upper, set->p[0]->eq[0], 2);
780 isl_seq_neg(lower, set->p[0]->eq[0], 2);
781 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
784 for (j = 0; j < set->p[0]->n_ineq; ++j) {
785 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
787 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
790 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
797 for (i = 0; i < set->n; ++i) {
798 struct isl_basic_set *bset = set->p[i];
802 for (j = 0; j < bset->n_eq; ++j) {
806 isl_int_mul(a, lower[0], bset->eq[j][1]);
807 isl_int_mul(b, lower[1], bset->eq[j][0]);
808 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
809 isl_seq_cpy(lower, bset->eq[j], 2);
810 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
811 isl_seq_neg(lower, bset->eq[j], 2);
814 isl_int_mul(a, upper[0], bset->eq[j][1]);
815 isl_int_mul(b, upper[1], bset->eq[j][0]);
816 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
817 isl_seq_neg(upper, bset->eq[j], 2);
818 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
819 isl_seq_cpy(upper, bset->eq[j], 2);
822 for (j = 0; j < bset->n_ineq; ++j) {
823 if (isl_int_is_pos(bset->ineq[j][1]))
825 if (isl_int_is_neg(bset->ineq[j][1]))
827 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
828 isl_int_mul(a, lower[0], bset->ineq[j][1]);
829 isl_int_mul(b, lower[1], bset->ineq[j][0]);
830 if (isl_int_lt(a, b))
831 isl_seq_cpy(lower, bset->ineq[j], 2);
833 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
834 isl_int_mul(a, upper[0], bset->ineq[j][1]);
835 isl_int_mul(b, upper[1], bset->ineq[j][0]);
836 if (isl_int_gt(a, b))
837 isl_seq_cpy(upper, bset->ineq[j], 2);
848 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
849 hull = isl_basic_set_set_rational(hull);
853 k = isl_basic_set_alloc_inequality(hull);
854 isl_seq_cpy(hull->ineq[k], lower, 2);
857 k = isl_basic_set_alloc_inequality(hull);
858 isl_seq_cpy(hull->ineq[k], upper, 2);
860 hull = isl_basic_set_finalize(hull);
862 isl_mat_free(ctx, c);
866 isl_mat_free(ctx, c);
870 /* Project out final n dimensions using Fourier-Motzkin */
871 static struct isl_set *set_project_out(struct isl_ctx *ctx,
872 struct isl_set *set, unsigned n)
874 return isl_set_remove_dims(set, set->dim - n, n);
877 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
879 struct isl_basic_set *convex_hull;
884 if (isl_set_is_empty(set))
885 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
887 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
892 /* Compute the convex hull of a pair of basic sets without any parameters or
893 * integer divisions using Fourier-Motzkin elimination.
894 * The convex hull is the set of all points that can be written as
895 * the sum of points from both basic sets (in homogeneous coordinates).
896 * We set up the constraints in a space with dimensions for each of
897 * the three sets and then project out the dimensions corresponding
898 * to the two original basic sets, retaining only those corresponding
899 * to the convex hull.
901 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
902 struct isl_basic_set *bset2)
905 struct isl_basic_set *bset[2];
906 struct isl_basic_set *hull = NULL;
909 if (!bset1 || !bset2)
913 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * bset1->dim, 0,
914 1 + bset1->dim + bset1->n_eq + bset2->n_eq,
915 2 + bset1->n_ineq + bset2->n_ineq);
918 for (i = 0; i < 2; ++i) {
919 for (j = 0; j < bset[i]->n_eq; ++j) {
920 k = isl_basic_set_alloc_equality(hull);
923 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
924 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
925 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
928 for (j = 0; j < bset[i]->n_ineq; ++j) {
929 k = isl_basic_set_alloc_inequality(hull);
932 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
933 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
934 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
935 bset[i]->ineq[j], 1+dim);
937 k = isl_basic_set_alloc_inequality(hull);
940 isl_seq_clr(hull->ineq[k], 1+hull->dim);
941 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
943 for (j = 0; j < 1+dim; ++j) {
944 k = isl_basic_set_alloc_equality(hull);
947 isl_seq_clr(hull->eq[k], 1+hull->dim);
948 isl_int_set_si(hull->eq[k][j], -1);
949 isl_int_set_si(hull->eq[k][1+dim+j], 1);
950 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
952 hull = isl_basic_set_set_rational(hull);
953 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
954 hull = isl_basic_set_convex_hull(hull);
955 isl_basic_set_free(bset1);
956 isl_basic_set_free(bset2);
959 isl_basic_set_free(bset1);
960 isl_basic_set_free(bset2);
961 isl_basic_set_free(hull);
965 /* Compute the convex hull of a set without any parameters or
966 * integer divisions using Fourier-Motzkin elimination.
967 * In each step, we combined two basic sets until only one
970 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
972 struct isl_basic_set *convex_hull = NULL;
974 convex_hull = isl_set_copy_basic_set(set);
975 set = isl_set_drop_basic_set(set, convex_hull);
979 struct isl_basic_set *t;
980 t = isl_set_copy_basic_set(set);
983 set = isl_set_drop_basic_set(set, t);
986 convex_hull = convex_hull_pair(convex_hull, t);
992 isl_basic_set_free(convex_hull);
996 static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
997 struct isl_set *set, struct isl_mat *bounds)
999 struct isl_basic_set *convex_hull = NULL;
1001 isl_assert(set->ctx, bounds->n_row == set->dim, goto error);
1002 bounds = initial_facet_constraint(set->ctx, set, bounds);
1005 convex_hull = extend(set->ctx, set, bounds);
1006 isl_mat_free(set->ctx, bounds);
1015 /* Compute the convex hull of a set without any parameters or
1016 * integer divisions. Depending on whether the set is bounded,
1017 * we pass control to the wrapping based convex hull or
1018 * the Fourier-Motzkin elimination based convex hull.
1019 * We also handle a few special cases before checking the boundedness.
1021 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1024 struct isl_basic_set *convex_hull = NULL;
1025 struct isl_mat *bounds;
1028 return convex_hull_0d(set);
1030 set = isl_set_set_rational(set);
1034 for (i = 0; i < set->n; ++i) {
1035 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
1039 set = isl_set_remove_empty_parts(set);
1043 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
1048 convex_hull = isl_basic_set_copy(set->p[0]);
1053 return convex_hull_1d(set->ctx, set);
1055 bounds = independent_bounds(set->ctx, set);
1058 if (bounds->n_row == set->dim)
1059 return uset_convex_hull_wrap_with_bounds(set, bounds);
1060 isl_mat_free(set->ctx, bounds);
1062 return uset_convex_hull_elim(set);
1065 isl_basic_set_free(convex_hull);
1069 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1070 * without parameters or divs and where the convex hull of set is
1071 * known to be full-dimensional.
1073 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1076 struct isl_basic_set *convex_hull = NULL;
1077 struct isl_mat *bounds;
1079 if (set->dim == 0) {
1080 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
1082 convex_hull = isl_basic_set_set_rational(convex_hull);
1086 set = isl_set_set_rational(set);
1090 for (i = 0; i < set->n; ++i) {
1091 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
1095 set = isl_set_remove_empty_parts(set);
1099 convex_hull = isl_basic_set_copy(set->p[0]);
1104 return convex_hull_1d(set->ctx, set);
1106 bounds = independent_bounds(set->ctx, set);
1109 return uset_convex_hull_wrap_with_bounds(set, bounds);
1115 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1116 * We first remove the equalities (transforming the set), compute the
1117 * convex hull of the transformed set and then add the equalities back
1118 * (after performing the inverse transformation.
1120 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1121 struct isl_set *set, struct isl_basic_set *affine_hull)
1125 struct isl_basic_set *dummy;
1126 struct isl_basic_set *convex_hull;
1128 dummy = isl_basic_set_remove_equalities(
1129 isl_basic_set_copy(affine_hull), &T, &T2);
1132 isl_basic_set_free(dummy);
1133 set = isl_set_preimage(ctx, set, T);
1134 convex_hull = uset_convex_hull(set);
1135 convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
1136 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1139 isl_basic_set_free(affine_hull);
1144 /* Compute the convex hull of a map.
1146 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1147 * specifically, the wrapping of facets to obtain new facets.
1149 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1151 struct isl_basic_set *bset;
1152 struct isl_basic_set *affine_hull = NULL;
1153 struct isl_basic_map *convex_hull = NULL;
1154 struct isl_set *set = NULL;
1155 struct isl_ctx *ctx;
1162 convex_hull = isl_basic_map_empty(ctx,
1163 map->nparam, map->n_in, map->n_out);
1168 set = isl_map_underlying_set(isl_map_copy(map));
1172 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1175 if (affine_hull->n_eq != 0)
1176 bset = modulo_affine_hull(ctx, set, affine_hull);
1178 isl_basic_set_free(affine_hull);
1179 bset = uset_convex_hull(set);
1182 convex_hull = isl_basic_map_overlying_set(bset,
1183 isl_basic_map_copy(map->p[0]));
1186 F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1194 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1196 return (struct isl_basic_set *)
1197 isl_map_convex_hull((struct isl_map *)set);
1200 /* Compute a superset of the convex hull of map that is described
1201 * by only translates of the constraints in the constituents of map.
1203 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1205 struct isl_set *set = NULL;
1206 struct isl_basic_map *hull;
1207 struct isl_basic_set *bset = NULL;
1212 hull = isl_basic_map_empty(map->ctx,
1213 map->nparam, map->n_in, map->n_out);
1218 hull = isl_basic_map_copy(map->p[0]);
1224 for (i = 0; i < map->n; ++i) {
1227 n_ineq += map->p[i]->n_ineq;
1230 set = isl_map_underlying_set(isl_map_copy(map));
1234 bset = isl_set_affine_hull(isl_set_copy(set));
1237 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 0, n_ineq);
1241 for (i = 0; i < set->n; ++i) {
1242 for (j = 0; j < set->p[i]->n_ineq; ++j) {
1246 k = isl_basic_set_alloc_inequality(bset);
1249 isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j],
1251 is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
1256 isl_basic_set_free_inequality(bset, 1);
1260 bset = isl_basic_set_simplify(bset);
1261 bset = isl_basic_set_finalize(bset);
1262 bset = isl_basic_set_convex_hull(bset);
1264 hull = isl_basic_map_overlying_set(bset, isl_basic_map_copy(map->p[0]));
1270 isl_basic_set_free(bset);
1276 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1278 return (struct isl_basic_set *)
1279 isl_map_simple_hull((struct isl_map *)set);