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 swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
17 bmap->ineq[i] = bmap->ineq[j];
22 /* Compute the convex hull of a basic map, by removing the redundant
23 * constraints. If the minimal value along the normal of a constraint
24 * is the same if the constraint is removed, then the constraint is redundant.
26 * Alternatively, we could have intersected the basic map with the
27 * corresponding equality and the checked if the dimension was that
30 struct isl_basic_map *isl_basic_map_convex_hull(struct isl_basic_map *bmap)
37 bmap = isl_basic_map_implicit_equalities(bmap);
41 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
43 if (F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
49 for (i = bmap->n_ineq-1; i >= 0; --i) {
50 enum isl_lp_result res;
51 swap_ineq(bmap, i, bmap->n_ineq-1);
53 res = isl_solve_lp(bmap, 0,
54 bmap->ineq[bmap->n_ineq]+1, ctx->one, &opt_n, &opt_d);
56 swap_ineq(bmap, i, bmap->n_ineq-1);
57 if (res == isl_lp_unbounded)
59 if (res == isl_lp_error)
61 if (res == isl_lp_empty) {
62 bmap = isl_basic_map_set_to_empty(bmap);
65 isl_int_addmul(opt_n, opt_d, bmap->ineq[i][0]);
66 if (!isl_int_is_neg(opt_n))
67 isl_basic_map_drop_inequality(bmap, i);
72 F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
77 isl_basic_map_free(bmap);
81 struct isl_basic_set *isl_basic_set_convex_hull(struct isl_basic_set *bset)
83 return (struct isl_basic_set *)
84 isl_basic_map_convex_hull((struct isl_basic_map *)bset);
87 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
88 isl_int *c, unsigned len)
96 isl_int_init(opt_denom);
98 for (j = 0; j < set->n; ++j) {
99 enum isl_lp_result res;
101 if (F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
104 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
105 0, c+1, ctx->one, &opt, &opt_denom);
106 if (res == isl_lp_unbounded)
108 if (res == isl_lp_error)
110 if (res == isl_lp_empty) {
111 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
116 if (!isl_int_is_one(opt_denom))
117 isl_seq_scale(c, c, opt_denom, len);
118 if (first || isl_int_lt(opt, c[0]))
119 isl_int_set(c[0], opt);
123 isl_int_clear(opt_denom);
124 isl_int_neg(c[0], c[0]);
128 isl_int_clear(opt_denom);
132 /* Check if "c" is a direction with both a lower bound and an upper
133 * bound in "set" that is independent of the previously found "n"
135 * If so, add it to the list, with the negative of the lower bound
136 * in the constant position, i.e., such that c corresponds to a bounding
137 * hyperplane (but not necessarily a facet).
139 static int is_independent_bound(struct isl_ctx *ctx,
140 struct isl_set *set, isl_int *c,
141 struct isl_mat *dirs, int n)
146 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
148 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
151 for (i = 0; i < n; ++i) {
153 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
158 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
159 dirs->n_col-1, NULL);
160 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
166 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
167 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
168 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
171 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
176 isl_int *t = dirs->row[n];
177 for (k = n; k > i; --k)
178 dirs->row[k] = dirs->row[k-1];
184 /* Compute and return a maximal set of linearly independent bounds
185 * on the set "set", based on the constraints of the basic sets
188 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
192 struct isl_mat *dirs = NULL;
194 dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
199 for (i = 0; n < set->dim && i < set->n; ++i) {
201 struct isl_basic_set *bset = set->p[i];
203 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
204 f = is_independent_bound(ctx, set, bset->eq[j],
211 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
212 f = is_independent_bound(ctx, set, bset->ineq[j],
223 isl_mat_free(ctx, dirs);
227 static struct isl_basic_set *isl_basic_set_set_rational(
228 struct isl_basic_set *bset)
233 if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
236 bset = isl_basic_set_cow(bset);
240 F_SET(bset, ISL_BASIC_MAP_RATIONAL);
242 return isl_basic_set_finalize(bset);
245 static struct isl_set *isl_set_set_rational(struct isl_set *set)
249 set = isl_set_cow(set);
252 for (i = 0; i < set->n; ++i) {
253 set->p[i] = isl_basic_set_set_rational(set->p[i]);
263 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
264 struct isl_basic_set *bset, isl_int *c)
269 if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
272 isl_assert(ctx, bset->nparam == 0, goto error);
273 isl_assert(ctx, bset->n_div == 0, goto error);
274 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
275 i = isl_basic_set_alloc_equality(bset);
278 isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
281 isl_basic_set_free(bset);
285 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
286 struct isl_set *set, isl_int *c)
290 set = isl_set_cow(set);
293 for (i = 0; i < set->n; ++i) {
294 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
304 /* Given a union of basic sets, construct the constraints for wrapping
305 * a facet around one of its ridges.
306 * In particular, if each of n the d-dimensional basic sets i in "set"
307 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
308 * and is defined by the constraints
312 * then the resulting set is of dimension n*(1+d) and has as contraints
321 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
324 struct isl_basic_set *lp;
336 for (i = 0; i < set->n; ++i) {
337 n_eq += set->p[i]->n_eq;
338 n_ineq += set->p[i]->n_ineq;
340 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
343 k = isl_basic_set_alloc_equality(lp);
344 isl_int_set_si(lp->eq[k][0], -1);
345 for (i = 0; i < set->n; ++i) {
346 isl_int_set_si(lp->eq[k][1+dim*i], 0);
347 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
348 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
350 for (i = 0; i < set->n; ++i) {
351 k = isl_basic_set_alloc_inequality(lp);
352 isl_seq_clr(lp->ineq[k], 1+lp->dim);
353 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
355 for (j = 0; j < set->p[i]->n_eq; ++j) {
356 k = isl_basic_set_alloc_equality(lp);
357 isl_seq_clr(lp->eq[k], 1+dim*i);
358 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
359 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
362 for (j = 0; j < set->p[i]->n_ineq; ++j) {
363 k = isl_basic_set_alloc_inequality(lp);
364 isl_seq_clr(lp->ineq[k], 1+dim*i);
365 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
366 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
372 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
373 * of that facet, compute the other facet of the convex hull that contains
376 * We first transform the set such that the facet constraint becomes
380 * I.e., the facet lies in
384 * and on that facet, the constraint that defines the ridge is
388 * (This transformation is not strictly needed, all that is needed is
389 * that the ridge contains the origin.)
391 * Since the ridge contains the origin, the cone of the convex hull
392 * will be of the form
397 * with this second constraint defining the new facet.
398 * The constant a is obtained by settting x_1 in the cone of the
399 * convex hull to 1 and minimizing x_2.
400 * Now, each element in the cone of the convex hull is the sum
401 * of elements in the cones of the basic sets.
402 * If a_i is the dilation factor of basic set i, then the problem
403 * we need to solve is
416 * the constraints of each (transformed) basic set.
417 * If a = n/d, then the constraint defining the new facet (in the transformed
420 * -n x_1 + d x_2 >= 0
422 * In the original space, we need to take the same combination of the
423 * corresponding constraints "facet" and "ridge".
425 * If a = -infty = "-1/0", then we just return the original facet constraint.
426 * This means that the facet is unbounded, but has a bounded intersection
427 * with the union of sets.
429 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
430 isl_int *facet, isl_int *ridge)
433 struct isl_mat *T = NULL;
434 struct isl_basic_set *lp = NULL;
436 enum isl_lp_result res;
440 set = isl_set_copy(set);
443 T = isl_mat_alloc(ctx, 3, 1 + set->dim);
446 isl_int_set_si(T->row[0][0], 1);
447 isl_seq_clr(T->row[0]+1, set->dim);
448 isl_seq_cpy(T->row[1], facet, 1+set->dim);
449 isl_seq_cpy(T->row[2], ridge, 1+set->dim);
450 T = isl_mat_right_inverse(ctx, T);
451 set = isl_set_preimage(ctx, set, T);
455 lp = wrap_constraints(ctx, set);
456 obj = isl_vec_alloc(ctx, dim*set->n);
459 for (i = 0; i < set->n; ++i) {
460 isl_seq_clr(obj->block.data+dim*i, 2);
461 isl_int_set_si(obj->block.data[dim*i+2], 1);
462 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
466 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
467 obj->block.data, ctx->one, &num, &den);
468 if (res == isl_lp_ok) {
469 isl_int_neg(num, num);
470 isl_seq_combine(facet, num, facet, den, ridge, dim);
474 isl_vec_free(ctx, obj);
475 isl_basic_set_free(lp);
477 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
481 isl_basic_set_free(lp);
482 isl_mat_free(ctx, T);
487 /* Given a set of d linearly independent bounding constraints of the
488 * convex hull of "set", compute the constraint of a facet of "set".
490 * We first compute the intersection with the first bounding hyperplane
491 * and remove the component corresponding to this hyperplane from
492 * other bounds (in homogeneous space).
493 * We then wrap around one of the remaining bounding constraints
494 * and continue the process until all bounding constraints have been
495 * taken into account.
496 * The resulting linear combination of the bounding constraints will
497 * correspond to a facet of the convex hull.
499 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
500 struct isl_set *set, struct isl_mat *bounds)
502 struct isl_set *slice = NULL;
503 struct isl_basic_set *face = NULL;
504 struct isl_mat *m, *U, *Q;
507 isl_assert(ctx, set->n > 0, goto error);
508 isl_assert(ctx, bounds->n_row == set->dim, goto error);
510 while (bounds->n_row > 1) {
511 slice = isl_set_copy(set);
512 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
513 face = isl_set_affine_hull(slice);
516 if (face->n_eq == 1) {
517 isl_basic_set_free(face);
520 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
523 isl_int_set_si(m->row[0][0], 1);
524 isl_seq_clr(m->row[0]+1, face->dim);
525 for (i = 0; i < face->n_eq; ++i)
526 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
527 U = isl_mat_right_inverse(ctx, m);
528 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
529 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
530 face->dim - face->n_eq);
531 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
532 face->dim - face->n_eq);
533 U = isl_mat_drop_cols(ctx, U, 0, 1);
534 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
535 bounds = isl_mat_product(ctx, bounds, U);
536 bounds = isl_mat_product(ctx, bounds, Q);
537 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
538 bounds->n_col) == -1) {
540 isl_assert(ctx, bounds->n_row > 1, goto error);
542 if (!wrap_facet(ctx, set, bounds->row[0],
543 bounds->row[bounds->n_row-1]))
545 isl_basic_set_free(face);
550 isl_basic_set_free(face);
551 isl_mat_free(ctx, bounds);
555 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
556 * compute a hyperplane description of the facet, i.e., compute the facets
559 * We compute an affine transformation that transforms the constraint
568 * by computing the right inverse U of a matrix that starts with the rows
581 * Since z_1 is zero, we can drop this variable as well as the corresponding
582 * column of U to obtain
590 * with Q' equal to Q, but without the corresponding row.
591 * After computing the facets of the facet in the z' space,
592 * we convert them back to the x space through Q.
594 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
595 struct isl_set *set, isl_int *c)
597 struct isl_mat *m, *U, *Q;
598 struct isl_basic_set *facet;
600 set = isl_set_copy(set);
601 m = isl_mat_alloc(ctx, 2, 1 + set->dim);
604 isl_int_set_si(m->row[0][0], 1);
605 isl_seq_clr(m->row[0]+1, set->dim);
606 isl_seq_cpy(m->row[1], c, 1+set->dim);
607 U = isl_mat_right_inverse(ctx, m);
608 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
609 U = isl_mat_drop_cols(ctx, U, 1, 1);
610 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
611 set = isl_set_preimage(ctx, set, U);
612 facet = uset_convex_hull_wrap(set);
613 facet = isl_basic_set_preimage(ctx, facet, Q);
620 /* Given an initial facet constraint, compute the remaining facets.
621 * We do this by running through all facets found so far and computing
622 * the adjacent facets through wrapping, adding those facets that we
623 * hadn't already found before.
625 * This function can still be significantly optimized by checking which of
626 * the facets of the basic sets are also facets of the convex hull and
627 * using all the facets so far to help in constructing the facets of the
630 * using the technique in section "3.1 Ridge Generation" of
631 * "Extended Convex Hull" by Fukuda et al.
633 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
634 struct isl_mat *initial)
638 struct isl_basic_set *hull = NULL;
639 struct isl_basic_set *facet = NULL;
643 isl_assert(ctx, set->n > 0, goto error);
646 for (i = 0; i < set->n; ++i) {
647 n_ineq += set->p[i]->n_eq;
648 n_ineq += set->p[i]->n_ineq;
650 isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
651 hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
652 hull = isl_basic_set_set_rational(hull);
655 k = isl_basic_set_alloc_inequality(hull);
658 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
659 for (i = 0; i < hull->n_ineq; ++i) {
660 facet = compute_facet(ctx, set, hull->ineq[i]);
663 if (facet->n_ineq + hull->n_ineq > n_ineq) {
664 hull = isl_basic_set_extend(hull,
665 hull->nparam, hull->dim, 0, 0, facet->n_ineq);
666 n_ineq = hull->n_ineq + facet->n_ineq;
668 for (j = 0; j < facet->n_ineq; ++j) {
669 k = isl_basic_set_alloc_inequality(hull);
672 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
673 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
675 for (f = 0; f < k; ++f)
676 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
680 isl_basic_set_free_inequality(hull, 1);
682 isl_basic_set_free(facet);
684 hull = isl_basic_set_simplify(hull);
685 hull = isl_basic_set_finalize(hull);
688 isl_basic_set_free(facet);
689 isl_basic_set_free(hull);
693 /* Special case for computing the convex hull of a one dimensional set.
694 * We simply collect the lower and upper bounds of each basic set
695 * and the biggest of those.
697 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
700 struct isl_mat *c = NULL;
701 isl_int *lower = NULL;
702 isl_int *upper = NULL;
705 struct isl_basic_set *hull;
707 for (i = 0; i < set->n; ++i) {
708 set->p[i] = isl_basic_set_simplify(set->p[i]);
712 set = isl_set_remove_empty_parts(set);
715 isl_assert(ctx, set->n > 0, goto error);
716 c = isl_mat_alloc(ctx, 2, 2);
720 if (set->p[0]->n_eq > 0) {
721 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
724 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
725 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
726 isl_seq_neg(upper, set->p[0]->eq[0], 2);
728 isl_seq_neg(lower, set->p[0]->eq[0], 2);
729 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
732 for (j = 0; j < set->p[0]->n_ineq; ++j) {
733 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
735 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
738 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
745 for (i = 0; i < set->n; ++i) {
746 struct isl_basic_set *bset = set->p[i];
750 for (j = 0; j < bset->n_eq; ++j) {
754 isl_int_mul(a, lower[0], bset->eq[j][1]);
755 isl_int_mul(b, lower[1], bset->eq[j][0]);
756 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
757 isl_seq_cpy(lower, bset->eq[j], 2);
758 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
759 isl_seq_neg(lower, bset->eq[j], 2);
762 isl_int_mul(a, upper[0], bset->eq[j][1]);
763 isl_int_mul(b, upper[1], bset->eq[j][0]);
764 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
765 isl_seq_neg(upper, bset->eq[j], 2);
766 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
767 isl_seq_cpy(upper, bset->eq[j], 2);
770 for (j = 0; j < bset->n_ineq; ++j) {
771 if (isl_int_is_pos(bset->ineq[j][1]))
773 if (isl_int_is_neg(bset->ineq[j][1]))
775 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
776 isl_int_mul(a, lower[0], bset->ineq[j][1]);
777 isl_int_mul(b, lower[1], bset->ineq[j][0]);
778 if (isl_int_lt(a, b))
779 isl_seq_cpy(lower, bset->ineq[j], 2);
781 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
782 isl_int_mul(a, upper[0], bset->ineq[j][1]);
783 isl_int_mul(b, upper[1], bset->ineq[j][0]);
784 if (isl_int_gt(a, b))
785 isl_seq_cpy(upper, bset->ineq[j], 2);
796 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
797 hull = isl_basic_set_set_rational(hull);
801 k = isl_basic_set_alloc_inequality(hull);
802 isl_seq_cpy(hull->ineq[k], lower, 2);
805 k = isl_basic_set_alloc_inequality(hull);
806 isl_seq_cpy(hull->ineq[k], upper, 2);
808 hull = isl_basic_set_finalize(hull);
810 isl_mat_free(ctx, c);
814 isl_mat_free(ctx, c);
818 /* Project out final n dimensions using Fourier-Motzkin */
819 static struct isl_set *set_project_out(struct isl_ctx *ctx,
820 struct isl_set *set, unsigned n)
822 return isl_set_remove_dims(set, set->dim - n, n);
825 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
827 struct isl_basic_set *convex_hull;
832 if (isl_set_is_empty(set))
833 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
835 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
840 /* Compute the convex hull of a pair of basic sets without any parameters or
841 * integer divisions using Fourier-Motzkin elimination.
842 * The convex hull is the set of all points that can be written as
843 * the sum of points from both basic sets (in homogeneous coordinates).
844 * We set up the constraints in a space with dimensions for each of
845 * the three sets and then project out the dimensions corresponding
846 * to the two original basic sets, retaining only those corresponding
847 * to the convex hull.
849 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
850 struct isl_basic_set *bset2)
853 struct isl_basic_set *bset[2];
854 struct isl_basic_set *hull = NULL;
857 if (!bset1 || !bset2)
861 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * bset1->dim, 0,
862 1 + bset1->dim + bset1->n_eq + bset2->n_eq,
863 2 + bset1->n_ineq + bset2->n_ineq);
866 for (i = 0; i < 2; ++i) {
867 for (j = 0; j < bset[i]->n_eq; ++j) {
868 k = isl_basic_set_alloc_equality(hull);
871 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
872 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
873 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
876 for (j = 0; j < bset[i]->n_ineq; ++j) {
877 k = isl_basic_set_alloc_inequality(hull);
880 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
881 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
882 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
883 bset[i]->ineq[j], 1+dim);
885 k = isl_basic_set_alloc_inequality(hull);
888 isl_seq_clr(hull->ineq[k], 1+hull->dim);
889 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
891 for (j = 0; j < 1+dim; ++j) {
892 k = isl_basic_set_alloc_equality(hull);
895 isl_seq_clr(hull->eq[k], 1+hull->dim);
896 isl_int_set_si(hull->eq[k][j], -1);
897 isl_int_set_si(hull->eq[k][1+dim+j], 1);
898 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
900 hull = isl_basic_set_set_rational(hull);
901 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
902 hull = isl_basic_set_convex_hull(hull);
903 isl_basic_set_free(bset1);
904 isl_basic_set_free(bset2);
907 isl_basic_set_free(bset1);
908 isl_basic_set_free(bset2);
909 isl_basic_set_free(hull);
913 /* Compute the convex hull of a set without any parameters or
914 * integer divisions using Fourier-Motzkin elimination.
915 * In each step, we combined two basic sets until only one
918 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
920 struct isl_basic_set *convex_hull = NULL;
922 convex_hull = isl_set_copy_basic_set(set);
923 set = isl_set_drop_basic_set(set, convex_hull);
927 struct isl_basic_set *t;
928 t = isl_set_copy_basic_set(set);
931 set = isl_set_drop_basic_set(set, t);
934 convex_hull = convex_hull_pair(convex_hull, t);
940 isl_basic_set_free(convex_hull);
944 static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
945 struct isl_set *set, struct isl_mat *bounds)
947 struct isl_basic_set *convex_hull = NULL;
949 isl_assert(set->ctx, bounds->n_row == set->dim, goto error);
950 bounds = initial_facet_constraint(set->ctx, set, bounds);
953 convex_hull = extend(set->ctx, set, bounds);
954 isl_mat_free(set->ctx, bounds);
963 /* Compute the convex hull of a set without any parameters or
964 * integer divisions. Depending on whether the set is bounded,
965 * we pass control to the wrapping based convex hull or
966 * the Fourier-Motzkin elimination based convex hull.
967 * We also handle a few special cases before checking the boundedness.
969 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
972 struct isl_basic_set *convex_hull = NULL;
973 struct isl_mat *bounds;
976 return convex_hull_0d(set);
978 set = isl_set_set_rational(set);
982 for (i = 0; i < set->n; ++i) {
983 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
987 set = isl_set_remove_empty_parts(set);
991 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
996 convex_hull = isl_basic_set_copy(set->p[0]);
1001 return convex_hull_1d(set->ctx, set);
1003 bounds = independent_bounds(set->ctx, set);
1006 if (bounds->n_row == set->dim)
1007 return uset_convex_hull_wrap_with_bounds(set, bounds);
1008 isl_mat_free(set->ctx, bounds);
1010 return uset_convex_hull_elim(set);
1013 isl_basic_set_free(convex_hull);
1017 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1018 * without parameters or divs and where the convex hull of set is
1019 * known to be full-dimensional.
1021 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1024 struct isl_basic_set *convex_hull = NULL;
1025 struct isl_mat *bounds;
1027 if (set->dim == 0) {
1028 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
1030 convex_hull = isl_basic_set_set_rational(convex_hull);
1034 set = isl_set_set_rational(set);
1038 for (i = 0; i < set->n; ++i) {
1039 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
1043 set = isl_set_remove_empty_parts(set);
1047 convex_hull = isl_basic_set_copy(set->p[0]);
1052 return convex_hull_1d(set->ctx, set);
1054 bounds = independent_bounds(set->ctx, set);
1057 return uset_convex_hull_wrap_with_bounds(set, bounds);
1063 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1064 * We first remove the equalities (transforming the set), compute the
1065 * convex hull of the transformed set and then add the equalities back
1066 * (after performing the inverse transformation.
1068 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1069 struct isl_set *set, struct isl_basic_set *affine_hull)
1073 struct isl_basic_set *dummy;
1074 struct isl_basic_set *convex_hull;
1076 dummy = isl_basic_set_remove_equalities(
1077 isl_basic_set_copy(affine_hull), &T, &T2);
1080 isl_basic_set_free(dummy);
1081 set = isl_set_preimage(ctx, set, T);
1082 convex_hull = uset_convex_hull(set);
1083 convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
1084 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1087 isl_basic_set_free(affine_hull);
1092 /* Compute the convex hull of a map.
1094 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1095 * specifically, the wrapping of facets to obtain new facets.
1097 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1099 struct isl_basic_set *bset;
1100 struct isl_basic_set *affine_hull = NULL;
1101 struct isl_basic_map *convex_hull = NULL;
1102 struct isl_set *set = NULL;
1103 struct isl_ctx *ctx;
1110 convex_hull = isl_basic_map_empty(ctx,
1111 map->nparam, map->n_in, map->n_out);
1116 set = isl_map_underlying_set(isl_map_copy(map));
1120 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1123 if (affine_hull->n_eq != 0)
1124 bset = modulo_affine_hull(ctx, set, affine_hull);
1126 isl_basic_set_free(affine_hull);
1127 bset = uset_convex_hull(set);
1130 convex_hull = isl_basic_map_overlying_set(bset,
1131 isl_basic_map_copy(map->p[0]));
1141 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1143 return (struct isl_basic_set *)
1144 isl_map_convex_hull((struct isl_map *)set);