3 #include "isl_map_private.h"
7 #include "isl_equalities.h"
10 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
12 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
18 bmap->ineq[i] = bmap->ineq[j];
23 /* Return 1 if constraint c is redundant with respect to the constraints
24 * in bmap. If c is a lower [upper] bound in some variable and bmap
25 * does not have a lower [upper] bound in that variable, then c cannot
26 * be redundant and we do not need solve any lp.
28 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
29 isl_int *c, isl_int *opt_n, isl_int *opt_d)
31 enum isl_lp_result res;
38 total = isl_basic_map_total_dim(*bmap);
39 for (i = 0; i < total; ++i) {
41 if (isl_int_is_zero(c[1+i]))
43 sign = isl_int_sgn(c[1+i]);
44 for (j = 0; j < (*bmap)->n_ineq; ++j)
45 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
47 if (j == (*bmap)->n_ineq)
53 res = isl_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 that is independent of the previously found "n"
166 * If so, add it to the list, with the negative of the lower bound
167 * in the constant position, i.e., such that c corresponds to a bounding
168 * hyperplane (but not necessarily a facet).
169 * Assumes set "set" is bounded.
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 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
203 isl_int *t = dirs->row[n];
204 for (k = n; k > i; --k)
205 dirs->row[k] = dirs->row[k-1];
211 /* Compute and return a maximal set of linearly independent bounds
212 * on the set "set", based on the constraints of the basic sets
215 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
219 struct isl_mat *dirs = NULL;
220 unsigned dim = isl_set_n_dim(set);
222 dirs = isl_mat_alloc(ctx, dim, 1+dim);
227 for (i = 0; n < dim && i < set->n; ++i) {
229 struct isl_basic_set *bset = set->p[i];
231 for (j = 0; n < dim && j < bset->n_eq; ++j) {
232 f = is_independent_bound(ctx, set, bset->eq[j],
239 for (j = 0; n < dim && j < bset->n_ineq; ++j) {
240 f = is_independent_bound(ctx, set, bset->ineq[j],
251 isl_mat_free(ctx, dirs);
255 static struct isl_basic_set *isl_basic_set_set_rational(
256 struct isl_basic_set *bset)
261 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
264 bset = isl_basic_set_cow(bset);
268 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
270 return isl_basic_set_finalize(bset);
273 static struct isl_set *isl_set_set_rational(struct isl_set *set)
277 set = isl_set_cow(set);
280 for (i = 0; i < set->n; ++i) {
281 set->p[i] = isl_basic_set_set_rational(set->p[i]);
291 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
292 struct isl_basic_set *bset, isl_int *c)
298 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
301 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
302 isl_assert(ctx, bset->n_div == 0, goto error);
303 dim = isl_basic_set_n_dim(bset);
304 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
305 i = isl_basic_set_alloc_equality(bset);
308 isl_seq_cpy(bset->eq[i], c, 1 + dim);
311 isl_basic_set_free(bset);
315 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
316 struct isl_set *set, isl_int *c)
320 set = isl_set_cow(set);
323 for (i = 0; i < set->n; ++i) {
324 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
334 /* Given a union of basic sets, construct the constraints for wrapping
335 * a facet around one of its ridges.
336 * In particular, if each of n the d-dimensional basic sets i in "set"
337 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
338 * and is defined by the constraints
342 * then the resulting set is of dimension n*(1+d) and has as contraints
351 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
353 struct isl_basic_set *lp;
357 unsigned dim, lp_dim;
362 dim = 1 + isl_set_n_dim(set);
365 for (i = 0; i < set->n; ++i) {
366 n_eq += set->p[i]->n_eq;
367 n_ineq += set->p[i]->n_ineq;
369 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
372 lp_dim = isl_basic_set_n_dim(lp);
373 k = isl_basic_set_alloc_equality(lp);
374 isl_int_set_si(lp->eq[k][0], -1);
375 for (i = 0; i < set->n; ++i) {
376 isl_int_set_si(lp->eq[k][1+dim*i], 0);
377 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
378 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
380 for (i = 0; i < set->n; ++i) {
381 k = isl_basic_set_alloc_inequality(lp);
382 isl_seq_clr(lp->ineq[k], 1+lp_dim);
383 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
385 for (j = 0; j < set->p[i]->n_eq; ++j) {
386 k = isl_basic_set_alloc_equality(lp);
387 isl_seq_clr(lp->eq[k], 1+dim*i);
388 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
389 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
392 for (j = 0; j < set->p[i]->n_ineq; ++j) {
393 k = isl_basic_set_alloc_inequality(lp);
394 isl_seq_clr(lp->ineq[k], 1+dim*i);
395 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
396 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
402 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
403 * of that facet, compute the other facet of the convex hull that contains
406 * We first transform the set such that the facet constraint becomes
410 * I.e., the facet lies in
414 * and on that facet, the constraint that defines the ridge is
418 * (This transformation is not strictly needed, all that is needed is
419 * that the ridge contains the origin.)
421 * Since the ridge contains the origin, the cone of the convex hull
422 * will be of the form
427 * with this second constraint defining the new facet.
428 * The constant a is obtained by settting x_1 in the cone of the
429 * convex hull to 1 and minimizing x_2.
430 * Now, each element in the cone of the convex hull is the sum
431 * of elements in the cones of the basic sets.
432 * If a_i is the dilation factor of basic set i, then the problem
433 * we need to solve is
446 * the constraints of each (transformed) basic set.
447 * If a = n/d, then the constraint defining the new facet (in the transformed
450 * -n x_1 + d x_2 >= 0
452 * In the original space, we need to take the same combination of the
453 * corresponding constraints "facet" and "ridge".
455 * If a = -infty = "-1/0", then we just return the original facet constraint.
456 * This means that the facet is unbounded, but has a bounded intersection
457 * with the union of sets.
459 static isl_int *wrap_facet(struct isl_set *set, isl_int *facet, isl_int *ridge)
462 struct isl_mat *T = NULL;
463 struct isl_basic_set *lp = NULL;
465 enum isl_lp_result res;
469 set = isl_set_copy(set);
471 dim = 1 + isl_set_n_dim(set);
472 T = isl_mat_alloc(set->ctx, 3, dim);
475 isl_int_set_si(T->row[0][0], 1);
476 isl_seq_clr(T->row[0]+1, dim - 1);
477 isl_seq_cpy(T->row[1], facet, dim);
478 isl_seq_cpy(T->row[2], ridge, dim);
479 T = isl_mat_right_inverse(set->ctx, T);
480 set = isl_set_preimage(set, T);
484 lp = wrap_constraints(set);
485 obj = isl_vec_alloc(set->ctx, dim*set->n);
488 for (i = 0; i < set->n; ++i) {
489 isl_seq_clr(obj->block.data+dim*i, 2);
490 isl_int_set_si(obj->block.data[dim*i+2], 1);
491 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
495 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
496 obj->block.data, set->ctx->one, &num, &den);
497 if (res == isl_lp_ok) {
498 isl_int_neg(num, num);
499 isl_seq_combine(facet, num, facet, den, ridge, dim);
503 isl_vec_free(set->ctx, obj);
504 isl_basic_set_free(lp);
506 isl_assert(set->ctx, res == isl_lp_ok || res == isl_lp_unbounded,
510 isl_basic_set_free(lp);
511 isl_mat_free(set->ctx, T);
516 /* Given a set of d linearly independent bounding constraints of the
517 * convex hull of "set", compute the constraint of a facet of "set".
519 * We first compute the intersection with the first bounding hyperplane
520 * and remove the component corresponding to this hyperplane from
521 * other bounds (in homogeneous space).
522 * We then wrap around one of the remaining bounding constraints
523 * and continue the process until all bounding constraints have been
524 * taken into account.
525 * The resulting linear combination of the bounding constraints will
526 * correspond to a facet of the convex hull.
528 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
529 struct isl_set *set, struct isl_mat *bounds)
531 struct isl_set *slice = NULL;
532 struct isl_basic_set *face = NULL;
533 struct isl_mat *m, *U, *Q;
535 unsigned dim = isl_set_n_dim(set);
537 isl_assert(ctx, set->n > 0, goto error);
538 isl_assert(ctx, bounds->n_row == dim, goto error);
540 while (bounds->n_row > 1) {
541 slice = isl_set_copy(set);
542 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
543 face = isl_set_affine_hull(slice);
546 if (face->n_eq == 1) {
547 isl_basic_set_free(face);
550 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
553 isl_int_set_si(m->row[0][0], 1);
554 isl_seq_clr(m->row[0]+1, dim);
555 for (i = 0; i < face->n_eq; ++i)
556 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
557 U = isl_mat_right_inverse(ctx, m);
558 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
559 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
561 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
563 U = isl_mat_drop_cols(ctx, U, 0, 1);
564 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
565 bounds = isl_mat_product(ctx, bounds, U);
566 bounds = isl_mat_product(ctx, bounds, Q);
567 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
568 bounds->n_col) == -1) {
570 isl_assert(ctx, bounds->n_row > 1, goto error);
572 if (!wrap_facet(set, bounds->row[0],
573 bounds->row[bounds->n_row-1]))
575 isl_basic_set_free(face);
580 isl_basic_set_free(face);
581 isl_mat_free(ctx, bounds);
585 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
586 * compute a hyperplane description of the facet, i.e., compute the facets
589 * We compute an affine transformation that transforms the constraint
598 * by computing the right inverse U of a matrix that starts with the rows
611 * Since z_1 is zero, we can drop this variable as well as the corresponding
612 * column of U to obtain
620 * with Q' equal to Q, but without the corresponding row.
621 * After computing the facets of the facet in the z' space,
622 * we convert them back to the x space through Q.
624 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
626 struct isl_mat *m, *U, *Q;
627 struct isl_basic_set *facet = NULL;
632 set = isl_set_copy(set);
633 dim = isl_set_n_dim(set);
634 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
637 isl_int_set_si(m->row[0][0], 1);
638 isl_seq_clr(m->row[0]+1, dim);
639 isl_seq_cpy(m->row[1], c, 1+dim);
640 U = isl_mat_right_inverse(set->ctx, m);
641 Q = isl_mat_right_inverse(set->ctx, isl_mat_copy(set->ctx, U));
642 U = isl_mat_drop_cols(set->ctx, U, 1, 1);
643 Q = isl_mat_drop_rows(set->ctx, Q, 1, 1);
644 set = isl_set_preimage(set, U);
645 facet = uset_convex_hull_wrap_bounded(set);
646 facet = isl_basic_set_preimage(facet, Q);
647 isl_assert(ctx, facet->n_eq == 0, goto error);
650 isl_basic_set_free(facet);
655 /* Given an initial facet constraint, compute the remaining facets.
656 * We do this by running through all facets found so far and computing
657 * the adjacent facets through wrapping, adding those facets that we
658 * hadn't already found before.
660 * This function can still be significantly optimized by checking which of
661 * the facets of the basic sets are also facets of the convex hull and
662 * using all the facets so far to help in constructing the facets of the
665 * using the technique in section "3.1 Ridge Generation" of
666 * "Extended Convex Hull" by Fukuda et al.
668 static struct isl_basic_set *extend(struct isl_basic_set *hull,
673 struct isl_basic_set *facet = NULL;
677 isl_assert(set->ctx, set->n > 0, goto error);
679 dim = isl_set_n_dim(set);
681 for (i = 0; i < hull->n_ineq; ++i) {
682 facet = compute_facet(set, hull->ineq[i]);
685 if (facet->n_ineq + hull->n_ineq > hull->c_size)
686 hull = isl_basic_set_extend_dim(hull,
687 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
688 for (j = 0; j < facet->n_ineq; ++j) {
689 k = isl_basic_set_alloc_inequality(hull);
692 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
693 if (!wrap_facet(set, hull->ineq[k], facet->ineq[j]))
695 for (f = 0; f < k; ++f)
696 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
700 isl_basic_set_free_inequality(hull, 1);
702 isl_basic_set_free(facet);
704 hull = isl_basic_set_simplify(hull);
705 hull = isl_basic_set_finalize(hull);
708 isl_basic_set_free(facet);
709 isl_basic_set_free(hull);
713 /* Special case for computing the convex hull of a one dimensional set.
714 * We simply collect the lower and upper bounds of each basic set
715 * and the biggest of those.
717 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
720 struct isl_mat *c = NULL;
721 isl_int *lower = NULL;
722 isl_int *upper = NULL;
725 struct isl_basic_set *hull;
727 for (i = 0; i < set->n; ++i) {
728 set->p[i] = isl_basic_set_simplify(set->p[i]);
732 set = isl_set_remove_empty_parts(set);
735 isl_assert(ctx, set->n > 0, goto error);
736 c = isl_mat_alloc(ctx, 2, 2);
740 if (set->p[0]->n_eq > 0) {
741 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
744 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
745 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
746 isl_seq_neg(upper, set->p[0]->eq[0], 2);
748 isl_seq_neg(lower, set->p[0]->eq[0], 2);
749 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
752 for (j = 0; j < set->p[0]->n_ineq; ++j) {
753 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
755 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
758 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
765 for (i = 0; i < set->n; ++i) {
766 struct isl_basic_set *bset = set->p[i];
770 for (j = 0; j < bset->n_eq; ++j) {
774 isl_int_mul(a, lower[0], bset->eq[j][1]);
775 isl_int_mul(b, lower[1], bset->eq[j][0]);
776 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
777 isl_seq_cpy(lower, bset->eq[j], 2);
778 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
779 isl_seq_neg(lower, bset->eq[j], 2);
782 isl_int_mul(a, upper[0], bset->eq[j][1]);
783 isl_int_mul(b, upper[1], bset->eq[j][0]);
784 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
785 isl_seq_neg(upper, bset->eq[j], 2);
786 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
787 isl_seq_cpy(upper, bset->eq[j], 2);
790 for (j = 0; j < bset->n_ineq; ++j) {
791 if (isl_int_is_pos(bset->ineq[j][1]))
793 if (isl_int_is_neg(bset->ineq[j][1]))
795 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
796 isl_int_mul(a, lower[0], bset->ineq[j][1]);
797 isl_int_mul(b, lower[1], bset->ineq[j][0]);
798 if (isl_int_lt(a, b))
799 isl_seq_cpy(lower, bset->ineq[j], 2);
801 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
802 isl_int_mul(a, upper[0], bset->ineq[j][1]);
803 isl_int_mul(b, upper[1], bset->ineq[j][0]);
804 if (isl_int_gt(a, b))
805 isl_seq_cpy(upper, bset->ineq[j], 2);
816 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
817 hull = isl_basic_set_set_rational(hull);
821 k = isl_basic_set_alloc_inequality(hull);
822 isl_seq_cpy(hull->ineq[k], lower, 2);
825 k = isl_basic_set_alloc_inequality(hull);
826 isl_seq_cpy(hull->ineq[k], upper, 2);
828 hull = isl_basic_set_finalize(hull);
830 isl_mat_free(ctx, c);
834 isl_mat_free(ctx, c);
838 /* Project out final n dimensions using Fourier-Motzkin */
839 static struct isl_set *set_project_out(struct isl_ctx *ctx,
840 struct isl_set *set, unsigned n)
842 return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
845 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
847 struct isl_basic_set *convex_hull;
852 if (isl_set_is_empty(set))
853 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
855 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
860 /* Compute the convex hull of a pair of basic sets without any parameters or
861 * integer divisions using Fourier-Motzkin elimination.
862 * The convex hull is the set of all points that can be written as
863 * the sum of points from both basic sets (in homogeneous coordinates).
864 * We set up the constraints in a space with dimensions for each of
865 * the three sets and then project out the dimensions corresponding
866 * to the two original basic sets, retaining only those corresponding
867 * to the convex hull.
869 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
870 struct isl_basic_set *bset2)
873 struct isl_basic_set *bset[2];
874 struct isl_basic_set *hull = NULL;
877 if (!bset1 || !bset2)
880 dim = isl_basic_set_n_dim(bset1);
881 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
882 1 + dim + bset1->n_eq + bset2->n_eq,
883 2 + bset1->n_ineq + bset2->n_ineq);
886 for (i = 0; i < 2; ++i) {
887 for (j = 0; j < bset[i]->n_eq; ++j) {
888 k = isl_basic_set_alloc_equality(hull);
891 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
892 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
893 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
896 for (j = 0; j < bset[i]->n_ineq; ++j) {
897 k = isl_basic_set_alloc_inequality(hull);
900 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
901 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
902 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
903 bset[i]->ineq[j], 1+dim);
905 k = isl_basic_set_alloc_inequality(hull);
908 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
909 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
911 for (j = 0; j < 1+dim; ++j) {
912 k = isl_basic_set_alloc_equality(hull);
915 isl_seq_clr(hull->eq[k], 1+2+3*dim);
916 isl_int_set_si(hull->eq[k][j], -1);
917 isl_int_set_si(hull->eq[k][1+dim+j], 1);
918 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
920 hull = isl_basic_set_set_rational(hull);
921 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
922 hull = isl_basic_set_convex_hull(hull);
923 isl_basic_set_free(bset1);
924 isl_basic_set_free(bset2);
927 isl_basic_set_free(bset1);
928 isl_basic_set_free(bset2);
929 isl_basic_set_free(hull);
933 /* Compute the convex hull of a set without any parameters or
934 * integer divisions using Fourier-Motzkin elimination.
935 * In each step, we combined two basic sets until only one
938 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
940 struct isl_basic_set *convex_hull = NULL;
942 convex_hull = isl_set_copy_basic_set(set);
943 set = isl_set_drop_basic_set(set, convex_hull);
947 struct isl_basic_set *t;
948 t = isl_set_copy_basic_set(set);
951 set = isl_set_drop_basic_set(set, t);
954 convex_hull = convex_hull_pair(convex_hull, t);
960 isl_basic_set_free(convex_hull);
964 /* Compute an initial hull for wrapping containing a single initial
965 * facet by first computing bounds on the set and then using these
966 * bounds to construct an initial facet.
967 * This function is a remnant of an older implementation where the
968 * bounds were also used to check whether the set was bounded.
969 * Since this function will now only be called when we know the
970 * set to be bounded, the initial facet should probably be constructed
971 * by simply using the coordinate directions instead.
973 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
976 struct isl_mat *bounds = NULL;
982 bounds = independent_bounds(set->ctx, set);
985 isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
986 bounds = initial_facet_constraint(set->ctx, set, bounds);
989 k = isl_basic_set_alloc_inequality(hull);
992 dim = isl_set_n_dim(set);
993 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
994 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
995 isl_mat_free(set->ctx, bounds);
999 isl_basic_set_free(hull);
1000 isl_mat_free(set->ctx, bounds);
1004 struct max_constraint {
1010 static int max_constraint_equal(const void *entry, const void *val)
1012 struct max_constraint *a = (struct max_constraint *)entry;
1013 isl_int *b = (isl_int *)val;
1015 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1018 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1019 isl_int *con, unsigned len, int n, int ineq)
1021 struct isl_hash_table_entry *entry;
1022 struct max_constraint *c;
1025 c_hash = isl_seq_hash(con + 1, len, isl_hash_init());
1026 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1032 isl_hash_table_remove(ctx, table, entry);
1036 if (isl_int_gt(c->c->row[0][0], con[0]))
1038 if (isl_int_eq(c->c->row[0][0], con[0])) {
1043 c->c = isl_mat_cow(ctx, c->c);
1044 isl_int_set(c->c->row[0][0], con[0]);
1048 /* Check whether the constraint hash table "table" constains the constraint
1051 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1052 isl_int *con, unsigned len, int n)
1054 struct isl_hash_table_entry *entry;
1055 struct max_constraint *c;
1058 c_hash = isl_seq_hash(con + 1, len, isl_hash_init());
1059 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1066 return isl_int_eq(c->c->row[0][0], con[0]);
1069 /* Check for inequality constraints of a basic set without equalities
1070 * such that the same or more stringent copies of the constraint appear
1071 * in all of the basic sets. Such constraints are necessarily facet
1072 * constraints of the convex hull.
1074 * If the resulting basic set is by chance identical to one of
1075 * the basic sets in "set", then we know that this basic set contains
1076 * all other basic sets and is therefore the convex hull of set.
1077 * In this case we set *is_hull to 1.
1079 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1080 struct isl_set *set, int *is_hull)
1083 int min_constraints;
1085 struct max_constraint *constraints = NULL;
1086 struct isl_hash_table *table = NULL;
1091 for (i = 0; i < set->n; ++i)
1092 if (set->p[i]->n_eq == 0)
1096 min_constraints = set->p[i]->n_ineq;
1098 for (i = best + 1; i < set->n; ++i) {
1099 if (set->p[i]->n_eq != 0)
1101 if (set->p[i]->n_ineq >= min_constraints)
1103 min_constraints = set->p[i]->n_ineq;
1106 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1110 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1111 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1114 total = isl_dim_total(set->dim);
1115 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1116 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1117 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1118 if (!constraints[i].c)
1120 constraints[i].ineq = 1;
1122 for (i = 0; i < min_constraints; ++i) {
1123 struct isl_hash_table_entry *entry;
1125 c_hash = isl_seq_hash(constraints[i].c->row[0] + 1, total,
1127 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1128 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1131 isl_assert(hull->ctx, !entry->data, goto error);
1132 entry->data = &constraints[i];
1136 for (s = 0; s < set->n; ++s) {
1140 for (i = 0; i < set->p[s]->n_eq; ++i) {
1141 isl_int *eq = set->p[s]->eq[i];
1142 for (j = 0; j < 2; ++j) {
1143 isl_seq_neg(eq, eq, 1 + total);
1144 update_constraint(hull->ctx, table,
1148 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1149 isl_int *ineq = set->p[s]->ineq[i];
1150 update_constraint(hull->ctx, table, ineq, total, n,
1151 set->p[s]->n_eq == 0);
1156 for (i = 0; i < min_constraints; ++i) {
1157 if (constraints[i].count < n)
1159 if (!constraints[i].ineq)
1161 j = isl_basic_set_alloc_inequality(hull);
1164 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1167 for (s = 0; s < set->n; ++s) {
1168 if (set->p[s]->n_eq)
1170 if (set->p[s]->n_ineq != hull->n_ineq)
1172 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1173 isl_int *ineq = set->p[s]->ineq[i];
1174 if (!has_constraint(hull->ctx, table, ineq, total, n))
1177 if (i == set->p[s]->n_ineq)
1181 isl_hash_table_clear(table);
1182 for (i = 0; i < min_constraints; ++i)
1183 isl_mat_free(hull->ctx, constraints[i].c);
1188 isl_hash_table_clear(table);
1191 for (i = 0; i < min_constraints; ++i)
1192 isl_mat_free(hull->ctx, constraints[i].c);
1197 /* Create a template for the convex hull of "set" and fill it up
1198 * obvious facet constraints, if any. If the result happens to
1199 * be the convex hull of "set" then *is_hull is set to 1.
1201 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1203 struct isl_basic_set *hull;
1208 for (i = 0; i < set->n; ++i) {
1209 n_ineq += set->p[i]->n_eq;
1210 n_ineq += set->p[i]->n_ineq;
1212 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1213 hull = isl_basic_set_set_rational(hull);
1216 return common_constraints(hull, set, is_hull);
1219 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1221 struct isl_basic_set *hull;
1224 hull = proto_hull(set, &is_hull);
1225 if (hull && !is_hull) {
1226 if (hull->n_ineq == 0)
1227 hull = initial_hull(hull, set);
1228 hull = extend(hull, set);
1235 static int isl_basic_set_is_bounded(struct isl_basic_set *bset)
1237 struct isl_tab *tab;
1240 tab = isl_tab_from_recession_cone((struct isl_basic_map *)bset);
1241 bounded = isl_tab_cone_is_bounded(bset->ctx, tab);
1242 isl_tab_free(bset->ctx, tab);
1246 static int isl_set_is_bounded(struct isl_set *set)
1250 for (i = 0; i < set->n; ++i) {
1251 int bounded = isl_basic_set_is_bounded(set->p[i]);
1252 if (!bounded || bounded < 0)
1258 /* Compute the convex hull of a set without any parameters or
1259 * integer divisions. Depending on whether the set is bounded,
1260 * we pass control to the wrapping based convex hull or
1261 * the Fourier-Motzkin elimination based convex hull.
1262 * We also handle a few special cases before checking the boundedness.
1264 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1267 struct isl_basic_set *convex_hull = NULL;
1269 if (isl_set_n_dim(set) == 0)
1270 return convex_hull_0d(set);
1272 set = isl_set_set_rational(set);
1276 set = isl_set_normalize(set);
1280 convex_hull = isl_basic_set_copy(set->p[0]);
1284 if (isl_set_n_dim(set) == 1)
1285 return convex_hull_1d(set->ctx, set);
1287 if (!isl_set_is_bounded(set))
1288 return uset_convex_hull_elim(set);
1290 return uset_convex_hull_wrap(set);
1293 isl_basic_set_free(convex_hull);
1297 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1298 * without parameters or divs and where the convex hull of set is
1299 * known to be full-dimensional.
1301 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1304 struct isl_basic_set *convex_hull = NULL;
1306 if (isl_set_n_dim(set) == 0) {
1307 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1309 convex_hull = isl_basic_set_set_rational(convex_hull);
1313 set = isl_set_set_rational(set);
1317 set = isl_set_normalize(set);
1321 convex_hull = isl_basic_set_copy(set->p[0]);
1325 if (isl_set_n_dim(set) == 1)
1326 return convex_hull_1d(set->ctx, set);
1328 return uset_convex_hull_wrap(set);
1334 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1335 * We first remove the equalities (transforming the set), compute the
1336 * convex hull of the transformed set and then add the equalities back
1337 * (after performing the inverse transformation.
1339 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1340 struct isl_set *set, struct isl_basic_set *affine_hull)
1344 struct isl_basic_set *dummy;
1345 struct isl_basic_set *convex_hull;
1347 dummy = isl_basic_set_remove_equalities(
1348 isl_basic_set_copy(affine_hull), &T, &T2);
1351 isl_basic_set_free(dummy);
1352 set = isl_set_preimage(set, T);
1353 convex_hull = uset_convex_hull(set);
1354 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1355 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1358 isl_basic_set_free(affine_hull);
1363 /* Compute the convex hull of a map.
1365 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1366 * specifically, the wrapping of facets to obtain new facets.
1368 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1370 struct isl_basic_set *bset;
1371 struct isl_basic_map *model = NULL;
1372 struct isl_basic_set *affine_hull = NULL;
1373 struct isl_basic_map *convex_hull = NULL;
1374 struct isl_set *set = NULL;
1375 struct isl_ctx *ctx;
1382 convex_hull = isl_basic_map_empty_like_map(map);
1387 map = isl_map_align_divs(map);
1388 model = isl_basic_map_copy(map->p[0]);
1389 set = isl_map_underlying_set(map);
1393 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1396 if (affine_hull->n_eq != 0)
1397 bset = modulo_affine_hull(ctx, set, affine_hull);
1399 isl_basic_set_free(affine_hull);
1400 bset = uset_convex_hull(set);
1403 convex_hull = isl_basic_map_overlying_set(bset, model);
1405 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1409 isl_basic_map_free(model);
1413 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1415 return (struct isl_basic_set *)
1416 isl_map_convex_hull((struct isl_map *)set);
1419 /* Compute a superset of the convex hull of map that is described
1420 * by only translates of the constraints in the constituents of map.
1422 * The implementation is not very efficient. In particular, if
1423 * constraints with the same normal appear in more than one
1424 * basic map, they will be (re)examined each time.
1426 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1428 struct isl_set *set = NULL;
1429 struct isl_basic_map *model = NULL;
1430 struct isl_basic_map *hull;
1431 struct isl_basic_set *bset = NULL;
1439 hull = isl_basic_map_empty_like_map(map);
1444 hull = isl_basic_map_copy(map->p[0]);
1449 map = isl_map_align_divs(map);
1450 model = isl_basic_map_copy(map->p[0]);
1453 for (i = 0; i < map->n; ++i) {
1456 n_ineq += map->p[i]->n_ineq;
1459 set = isl_map_underlying_set(map);
1463 bset = isl_set_affine_hull(isl_set_copy(set));
1466 dim = isl_basic_set_n_dim(bset);
1467 bset = isl_basic_set_extend(bset, 0, dim, 0, 0, n_ineq);
1471 for (i = 0; i < set->n; ++i) {
1472 for (j = 0; j < set->p[i]->n_ineq; ++j) {
1476 k = isl_basic_set_alloc_inequality(bset);
1479 isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j], 1 + dim);
1480 is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
1485 isl_basic_set_free_inequality(bset, 1);
1489 bset = isl_basic_set_simplify(bset);
1490 bset = isl_basic_set_finalize(bset);
1491 bset = isl_basic_set_convex_hull(bset);
1493 hull = isl_basic_map_overlying_set(bset, model);
1498 isl_basic_set_free(bset);
1500 isl_basic_map_free(model);
1504 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1506 return (struct isl_basic_set *)
1507 isl_map_simple_hull((struct isl_map *)set);