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 /* Check if the set set is bound in the direction of the affine
88 * constraint c and if so, set the constant term such that the
89 * resulting constraint is a bounding constraint for the set.
91 static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
92 isl_int *c, unsigned len)
100 isl_int_init(opt_denom);
102 for (j = 0; j < set->n; ++j) {
103 enum isl_lp_result res;
105 if (F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
108 res = isl_solve_lp((struct isl_basic_map*)set->p[j],
109 0, c+1, ctx->one, &opt, &opt_denom);
110 if (res == isl_lp_unbounded)
112 if (res == isl_lp_error)
114 if (res == isl_lp_empty) {
115 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
120 if (!isl_int_is_one(opt_denom))
121 isl_seq_scale(c, c, opt_denom, len);
122 if (first || isl_int_lt(opt, c[0]))
123 isl_int_set(c[0], opt);
127 isl_int_clear(opt_denom);
128 isl_int_neg(c[0], c[0]);
132 isl_int_clear(opt_denom);
136 /* Check if "c" is a direction with both a lower bound and an upper
137 * bound in "set" that is independent of the previously found "n"
139 * If so, add it to the list, with the negative of the lower bound
140 * in the constant position, i.e., such that c corresponds to a bounding
141 * hyperplane (but not necessarily a facet).
143 static int is_independent_bound(struct isl_ctx *ctx,
144 struct isl_set *set, isl_int *c,
145 struct isl_mat *dirs, int n)
150 isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
152 int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
155 for (i = 0; i < n; ++i) {
157 pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
162 isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
163 dirs->n_col-1, NULL);
164 pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
170 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);
172 isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
175 is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
180 isl_int *t = dirs->row[n];
181 for (k = n; k > i; --k)
182 dirs->row[k] = dirs->row[k-1];
188 /* Compute and return a maximal set of linearly independent bounds
189 * on the set "set", based on the constraints of the basic sets
192 static struct isl_mat *independent_bounds(struct isl_ctx *ctx,
196 struct isl_mat *dirs = NULL;
198 dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
203 for (i = 0; n < set->dim && i < set->n; ++i) {
205 struct isl_basic_set *bset = set->p[i];
207 for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
208 f = is_independent_bound(ctx, set, bset->eq[j],
215 for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
216 f = is_independent_bound(ctx, set, bset->ineq[j],
227 isl_mat_free(ctx, dirs);
231 static struct isl_basic_set *isl_basic_set_set_rational(
232 struct isl_basic_set *bset)
237 if (F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
240 bset = isl_basic_set_cow(bset);
244 F_SET(bset, ISL_BASIC_MAP_RATIONAL);
246 return isl_basic_set_finalize(bset);
249 static struct isl_set *isl_set_set_rational(struct isl_set *set)
253 set = isl_set_cow(set);
256 for (i = 0; i < set->n; ++i) {
257 set->p[i] = isl_basic_set_set_rational(set->p[i]);
267 static struct isl_basic_set *isl_basic_set_add_equality(struct isl_ctx *ctx,
268 struct isl_basic_set *bset, isl_int *c)
273 if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
276 isl_assert(ctx, bset->nparam == 0, goto error);
277 isl_assert(ctx, bset->n_div == 0, goto error);
278 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
279 i = isl_basic_set_alloc_equality(bset);
282 isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
285 isl_basic_set_free(bset);
289 static struct isl_set *isl_set_add_equality(struct isl_ctx *ctx,
290 struct isl_set *set, isl_int *c)
294 set = isl_set_cow(set);
297 for (i = 0; i < set->n; ++i) {
298 set->p[i] = isl_basic_set_add_equality(ctx, set->p[i], c);
308 /* Given a union of basic sets, construct the constraints for wrapping
309 * a facet around one of its ridges.
310 * In particular, if each of n the d-dimensional basic sets i in "set"
311 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
312 * and is defined by the constraints
316 * then the resulting set is of dimension n*(1+d) and has as contraints
325 static struct isl_basic_set *wrap_constraints(struct isl_ctx *ctx,
328 struct isl_basic_set *lp;
340 for (i = 0; i < set->n; ++i) {
341 n_eq += set->p[i]->n_eq;
342 n_ineq += set->p[i]->n_ineq;
344 lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
347 k = isl_basic_set_alloc_equality(lp);
348 isl_int_set_si(lp->eq[k][0], -1);
349 for (i = 0; i < set->n; ++i) {
350 isl_int_set_si(lp->eq[k][1+dim*i], 0);
351 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
352 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
354 for (i = 0; i < set->n; ++i) {
355 k = isl_basic_set_alloc_inequality(lp);
356 isl_seq_clr(lp->ineq[k], 1+lp->dim);
357 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
359 for (j = 0; j < set->p[i]->n_eq; ++j) {
360 k = isl_basic_set_alloc_equality(lp);
361 isl_seq_clr(lp->eq[k], 1+dim*i);
362 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
363 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
366 for (j = 0; j < set->p[i]->n_ineq; ++j) {
367 k = isl_basic_set_alloc_inequality(lp);
368 isl_seq_clr(lp->ineq[k], 1+dim*i);
369 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
370 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
376 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
377 * of that facet, compute the other facet of the convex hull that contains
380 * We first transform the set such that the facet constraint becomes
384 * I.e., the facet lies in
388 * and on that facet, the constraint that defines the ridge is
392 * (This transformation is not strictly needed, all that is needed is
393 * that the ridge contains the origin.)
395 * Since the ridge contains the origin, the cone of the convex hull
396 * will be of the form
401 * with this second constraint defining the new facet.
402 * The constant a is obtained by settting x_1 in the cone of the
403 * convex hull to 1 and minimizing x_2.
404 * Now, each element in the cone of the convex hull is the sum
405 * of elements in the cones of the basic sets.
406 * If a_i is the dilation factor of basic set i, then the problem
407 * we need to solve is
420 * the constraints of each (transformed) basic set.
421 * If a = n/d, then the constraint defining the new facet (in the transformed
424 * -n x_1 + d x_2 >= 0
426 * In the original space, we need to take the same combination of the
427 * corresponding constraints "facet" and "ridge".
429 * If a = -infty = "-1/0", then we just return the original facet constraint.
430 * This means that the facet is unbounded, but has a bounded intersection
431 * with the union of sets.
433 static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
434 isl_int *facet, isl_int *ridge)
437 struct isl_mat *T = NULL;
438 struct isl_basic_set *lp = NULL;
440 enum isl_lp_result res;
444 set = isl_set_copy(set);
447 T = isl_mat_alloc(ctx, 3, 1 + set->dim);
450 isl_int_set_si(T->row[0][0], 1);
451 isl_seq_clr(T->row[0]+1, set->dim);
452 isl_seq_cpy(T->row[1], facet, 1+set->dim);
453 isl_seq_cpy(T->row[2], ridge, 1+set->dim);
454 T = isl_mat_right_inverse(ctx, T);
455 set = isl_set_preimage(ctx, set, T);
459 lp = wrap_constraints(ctx, set);
460 obj = isl_vec_alloc(ctx, dim*set->n);
463 for (i = 0; i < set->n; ++i) {
464 isl_seq_clr(obj->block.data+dim*i, 2);
465 isl_int_set_si(obj->block.data[dim*i+2], 1);
466 isl_seq_clr(obj->block.data+dim*i+3, dim-3);
470 res = isl_solve_lp((struct isl_basic_map *)lp, 0,
471 obj->block.data, ctx->one, &num, &den);
472 if (res == isl_lp_ok) {
473 isl_int_neg(num, num);
474 isl_seq_combine(facet, num, facet, den, ridge, dim);
478 isl_vec_free(ctx, obj);
479 isl_basic_set_free(lp);
481 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
485 isl_basic_set_free(lp);
486 isl_mat_free(ctx, T);
491 /* Given a set of d linearly independent bounding constraints of the
492 * convex hull of "set", compute the constraint of a facet of "set".
494 * We first compute the intersection with the first bounding hyperplane
495 * and remove the component corresponding to this hyperplane from
496 * other bounds (in homogeneous space).
497 * We then wrap around one of the remaining bounding constraints
498 * and continue the process until all bounding constraints have been
499 * taken into account.
500 * The resulting linear combination of the bounding constraints will
501 * correspond to a facet of the convex hull.
503 static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
504 struct isl_set *set, struct isl_mat *bounds)
506 struct isl_set *slice = NULL;
507 struct isl_basic_set *face = NULL;
508 struct isl_mat *m, *U, *Q;
511 isl_assert(ctx, set->n > 0, goto error);
512 isl_assert(ctx, bounds->n_row == set->dim, goto error);
514 while (bounds->n_row > 1) {
515 slice = isl_set_copy(set);
516 slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
517 face = isl_set_affine_hull(slice);
520 if (face->n_eq == 1) {
521 isl_basic_set_free(face);
524 m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + face->dim);
527 isl_int_set_si(m->row[0][0], 1);
528 isl_seq_clr(m->row[0]+1, face->dim);
529 for (i = 0; i < face->n_eq; ++i)
530 isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + face->dim);
531 U = isl_mat_right_inverse(ctx, m);
532 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
533 U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
534 face->dim - face->n_eq);
535 Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
536 face->dim - face->n_eq);
537 U = isl_mat_drop_cols(ctx, U, 0, 1);
538 Q = isl_mat_drop_rows(ctx, Q, 0, 1);
539 bounds = isl_mat_product(ctx, bounds, U);
540 bounds = isl_mat_product(ctx, bounds, Q);
541 while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
542 bounds->n_col) == -1) {
544 isl_assert(ctx, bounds->n_row > 1, goto error);
546 if (!wrap_facet(ctx, set, bounds->row[0],
547 bounds->row[bounds->n_row-1]))
549 isl_basic_set_free(face);
554 isl_basic_set_free(face);
555 isl_mat_free(ctx, bounds);
559 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
560 * compute a hyperplane description of the facet, i.e., compute the facets
563 * We compute an affine transformation that transforms the constraint
572 * by computing the right inverse U of a matrix that starts with the rows
585 * Since z_1 is zero, we can drop this variable as well as the corresponding
586 * column of U to obtain
594 * with Q' equal to Q, but without the corresponding row.
595 * After computing the facets of the facet in the z' space,
596 * we convert them back to the x space through Q.
598 static struct isl_basic_set *compute_facet(struct isl_ctx *ctx,
599 struct isl_set *set, isl_int *c)
601 struct isl_mat *m, *U, *Q;
602 struct isl_basic_set *facet;
604 set = isl_set_copy(set);
605 m = isl_mat_alloc(ctx, 2, 1 + set->dim);
608 isl_int_set_si(m->row[0][0], 1);
609 isl_seq_clr(m->row[0]+1, set->dim);
610 isl_seq_cpy(m->row[1], c, 1+set->dim);
611 U = isl_mat_right_inverse(ctx, m);
612 Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
613 U = isl_mat_drop_cols(ctx, U, 1, 1);
614 Q = isl_mat_drop_rows(ctx, Q, 1, 1);
615 set = isl_set_preimage(ctx, set, U);
616 facet = uset_convex_hull_wrap(set);
617 facet = isl_basic_set_preimage(ctx, facet, Q);
624 /* Given an initial facet constraint, compute the remaining facets.
625 * We do this by running through all facets found so far and computing
626 * the adjacent facets through wrapping, adding those facets that we
627 * hadn't already found before.
629 * This function can still be significantly optimized by checking which of
630 * the facets of the basic sets are also facets of the convex hull and
631 * using all the facets so far to help in constructing the facets of the
634 * using the technique in section "3.1 Ridge Generation" of
635 * "Extended Convex Hull" by Fukuda et al.
637 static struct isl_basic_set *extend(struct isl_ctx *ctx, struct isl_set *set,
638 struct isl_mat *initial)
642 struct isl_basic_set *hull = NULL;
643 struct isl_basic_set *facet = NULL;
647 isl_assert(ctx, set->n > 0, goto error);
650 for (i = 0; i < set->n; ++i) {
651 n_ineq += set->p[i]->n_eq;
652 n_ineq += set->p[i]->n_ineq;
654 isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
655 hull = isl_basic_set_alloc(ctx, 0, set->dim, 0, 0, n_ineq);
656 hull = isl_basic_set_set_rational(hull);
659 k = isl_basic_set_alloc_inequality(hull);
662 isl_seq_cpy(hull->ineq[k], initial->row[0], initial->n_col);
663 for (i = 0; i < hull->n_ineq; ++i) {
664 facet = compute_facet(ctx, set, hull->ineq[i]);
667 if (facet->n_ineq + hull->n_ineq > n_ineq) {
668 hull = isl_basic_set_extend(hull,
669 hull->nparam, hull->dim, 0, 0, facet->n_ineq);
670 n_ineq = hull->n_ineq + facet->n_ineq;
672 for (j = 0; j < facet->n_ineq; ++j) {
673 k = isl_basic_set_alloc_inequality(hull);
676 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
677 if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
679 for (f = 0; f < k; ++f)
680 if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
684 isl_basic_set_free_inequality(hull, 1);
686 isl_basic_set_free(facet);
688 hull = isl_basic_set_simplify(hull);
689 hull = isl_basic_set_finalize(hull);
692 isl_basic_set_free(facet);
693 isl_basic_set_free(hull);
697 /* Special case for computing the convex hull of a one dimensional set.
698 * We simply collect the lower and upper bounds of each basic set
699 * and the biggest of those.
701 static struct isl_basic_set *convex_hull_1d(struct isl_ctx *ctx,
704 struct isl_mat *c = NULL;
705 isl_int *lower = NULL;
706 isl_int *upper = NULL;
709 struct isl_basic_set *hull;
711 for (i = 0; i < set->n; ++i) {
712 set->p[i] = isl_basic_set_simplify(set->p[i]);
716 set = isl_set_remove_empty_parts(set);
719 isl_assert(ctx, set->n > 0, goto error);
720 c = isl_mat_alloc(ctx, 2, 2);
724 if (set->p[0]->n_eq > 0) {
725 isl_assert(ctx, set->p[0]->n_eq == 1, goto error);
728 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
729 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
730 isl_seq_neg(upper, set->p[0]->eq[0], 2);
732 isl_seq_neg(lower, set->p[0]->eq[0], 2);
733 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
736 for (j = 0; j < set->p[0]->n_ineq; ++j) {
737 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
739 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
742 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
749 for (i = 0; i < set->n; ++i) {
750 struct isl_basic_set *bset = set->p[i];
754 for (j = 0; j < bset->n_eq; ++j) {
758 isl_int_mul(a, lower[0], bset->eq[j][1]);
759 isl_int_mul(b, lower[1], bset->eq[j][0]);
760 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
761 isl_seq_cpy(lower, bset->eq[j], 2);
762 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
763 isl_seq_neg(lower, bset->eq[j], 2);
766 isl_int_mul(a, upper[0], bset->eq[j][1]);
767 isl_int_mul(b, upper[1], bset->eq[j][0]);
768 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
769 isl_seq_neg(upper, bset->eq[j], 2);
770 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
771 isl_seq_cpy(upper, bset->eq[j], 2);
774 for (j = 0; j < bset->n_ineq; ++j) {
775 if (isl_int_is_pos(bset->ineq[j][1]))
777 if (isl_int_is_neg(bset->ineq[j][1]))
779 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
780 isl_int_mul(a, lower[0], bset->ineq[j][1]);
781 isl_int_mul(b, lower[1], bset->ineq[j][0]);
782 if (isl_int_lt(a, b))
783 isl_seq_cpy(lower, bset->ineq[j], 2);
785 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
786 isl_int_mul(a, upper[0], bset->ineq[j][1]);
787 isl_int_mul(b, upper[1], bset->ineq[j][0]);
788 if (isl_int_gt(a, b))
789 isl_seq_cpy(upper, bset->ineq[j], 2);
800 hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
801 hull = isl_basic_set_set_rational(hull);
805 k = isl_basic_set_alloc_inequality(hull);
806 isl_seq_cpy(hull->ineq[k], lower, 2);
809 k = isl_basic_set_alloc_inequality(hull);
810 isl_seq_cpy(hull->ineq[k], upper, 2);
812 hull = isl_basic_set_finalize(hull);
814 isl_mat_free(ctx, c);
818 isl_mat_free(ctx, c);
822 /* Project out final n dimensions using Fourier-Motzkin */
823 static struct isl_set *set_project_out(struct isl_ctx *ctx,
824 struct isl_set *set, unsigned n)
826 return isl_set_remove_dims(set, set->dim - n, n);
829 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
831 struct isl_basic_set *convex_hull;
836 if (isl_set_is_empty(set))
837 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
839 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
844 /* Compute the convex hull of a pair of basic sets without any parameters or
845 * integer divisions using Fourier-Motzkin elimination.
846 * The convex hull is the set of all points that can be written as
847 * the sum of points from both basic sets (in homogeneous coordinates).
848 * We set up the constraints in a space with dimensions for each of
849 * the three sets and then project out the dimensions corresponding
850 * to the two original basic sets, retaining only those corresponding
851 * to the convex hull.
853 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
854 struct isl_basic_set *bset2)
857 struct isl_basic_set *bset[2];
858 struct isl_basic_set *hull = NULL;
861 if (!bset1 || !bset2)
865 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * bset1->dim, 0,
866 1 + bset1->dim + bset1->n_eq + bset2->n_eq,
867 2 + bset1->n_ineq + bset2->n_ineq);
870 for (i = 0; i < 2; ++i) {
871 for (j = 0; j < bset[i]->n_eq; ++j) {
872 k = isl_basic_set_alloc_equality(hull);
875 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
876 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
877 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
880 for (j = 0; j < bset[i]->n_ineq; ++j) {
881 k = isl_basic_set_alloc_inequality(hull);
884 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
885 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
886 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
887 bset[i]->ineq[j], 1+dim);
889 k = isl_basic_set_alloc_inequality(hull);
892 isl_seq_clr(hull->ineq[k], 1+hull->dim);
893 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
895 for (j = 0; j < 1+dim; ++j) {
896 k = isl_basic_set_alloc_equality(hull);
899 isl_seq_clr(hull->eq[k], 1+hull->dim);
900 isl_int_set_si(hull->eq[k][j], -1);
901 isl_int_set_si(hull->eq[k][1+dim+j], 1);
902 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
904 hull = isl_basic_set_set_rational(hull);
905 hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
906 hull = isl_basic_set_convex_hull(hull);
907 isl_basic_set_free(bset1);
908 isl_basic_set_free(bset2);
911 isl_basic_set_free(bset1);
912 isl_basic_set_free(bset2);
913 isl_basic_set_free(hull);
917 /* Compute the convex hull of a set without any parameters or
918 * integer divisions using Fourier-Motzkin elimination.
919 * In each step, we combined two basic sets until only one
922 static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
924 struct isl_basic_set *convex_hull = NULL;
926 convex_hull = isl_set_copy_basic_set(set);
927 set = isl_set_drop_basic_set(set, convex_hull);
931 struct isl_basic_set *t;
932 t = isl_set_copy_basic_set(set);
935 set = isl_set_drop_basic_set(set, t);
938 convex_hull = convex_hull_pair(convex_hull, t);
944 isl_basic_set_free(convex_hull);
948 static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
949 struct isl_set *set, struct isl_mat *bounds)
951 struct isl_basic_set *convex_hull = NULL;
953 isl_assert(set->ctx, bounds->n_row == set->dim, goto error);
954 bounds = initial_facet_constraint(set->ctx, set, bounds);
957 convex_hull = extend(set->ctx, set, bounds);
958 isl_mat_free(set->ctx, bounds);
967 /* Compute the convex hull of a set without any parameters or
968 * integer divisions. Depending on whether the set is bounded,
969 * we pass control to the wrapping based convex hull or
970 * the Fourier-Motzkin elimination based convex hull.
971 * We also handle a few special cases before checking the boundedness.
973 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
976 struct isl_basic_set *convex_hull = NULL;
977 struct isl_mat *bounds;
980 return convex_hull_0d(set);
982 set = isl_set_set_rational(set);
986 for (i = 0; i < set->n; ++i) {
987 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
991 set = isl_set_remove_empty_parts(set);
995 convex_hull = isl_basic_set_empty(set->ctx, 0, 0);
1000 convex_hull = isl_basic_set_copy(set->p[0]);
1005 return convex_hull_1d(set->ctx, set);
1007 bounds = independent_bounds(set->ctx, set);
1010 if (bounds->n_row == set->dim)
1011 return uset_convex_hull_wrap_with_bounds(set, bounds);
1012 isl_mat_free(set->ctx, bounds);
1014 return uset_convex_hull_elim(set);
1017 isl_basic_set_free(convex_hull);
1021 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1022 * without parameters or divs and where the convex hull of set is
1023 * known to be full-dimensional.
1025 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1028 struct isl_basic_set *convex_hull = NULL;
1029 struct isl_mat *bounds;
1031 if (set->dim == 0) {
1032 convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
1034 convex_hull = isl_basic_set_set_rational(convex_hull);
1038 set = isl_set_set_rational(set);
1042 for (i = 0; i < set->n; ++i) {
1043 set->p[i] = isl_basic_set_convex_hull(set->p[i]);
1047 set = isl_set_remove_empty_parts(set);
1051 convex_hull = isl_basic_set_copy(set->p[0]);
1056 return convex_hull_1d(set->ctx, set);
1058 bounds = independent_bounds(set->ctx, set);
1061 return uset_convex_hull_wrap_with_bounds(set, bounds);
1067 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1068 * We first remove the equalities (transforming the set), compute the
1069 * convex hull of the transformed set and then add the equalities back
1070 * (after performing the inverse transformation.
1072 static struct isl_basic_set *modulo_affine_hull(struct isl_ctx *ctx,
1073 struct isl_set *set, struct isl_basic_set *affine_hull)
1077 struct isl_basic_set *dummy;
1078 struct isl_basic_set *convex_hull;
1080 dummy = isl_basic_set_remove_equalities(
1081 isl_basic_set_copy(affine_hull), &T, &T2);
1084 isl_basic_set_free(dummy);
1085 set = isl_set_preimage(ctx, set, T);
1086 convex_hull = uset_convex_hull(set);
1087 convex_hull = isl_basic_set_preimage(ctx, convex_hull, T2);
1088 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1091 isl_basic_set_free(affine_hull);
1096 /* Compute the convex hull of a map.
1098 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1099 * specifically, the wrapping of facets to obtain new facets.
1101 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1103 struct isl_basic_set *bset;
1104 struct isl_basic_set *affine_hull = NULL;
1105 struct isl_basic_map *convex_hull = NULL;
1106 struct isl_set *set = NULL;
1107 struct isl_ctx *ctx;
1114 convex_hull = isl_basic_map_empty(ctx,
1115 map->nparam, map->n_in, map->n_out);
1120 set = isl_map_underlying_set(isl_map_copy(map));
1124 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1127 if (affine_hull->n_eq != 0)
1128 bset = modulo_affine_hull(ctx, set, affine_hull);
1130 isl_basic_set_free(affine_hull);
1131 bset = uset_convex_hull(set);
1134 convex_hull = isl_basic_map_overlying_set(bset,
1135 isl_basic_map_copy(map->p[0]));
1138 F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1146 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1148 return (struct isl_basic_set *)
1149 isl_map_convex_hull((struct isl_map *)set);
1152 /* Compute a superset of the convex hull of map that is described
1153 * by only translates of the constraints in the constituents of map.
1155 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
1157 struct isl_set *set = NULL;
1158 struct isl_basic_map *hull;
1159 struct isl_basic_set *bset = NULL;
1164 hull = isl_basic_map_empty(map->ctx,
1165 map->nparam, map->n_in, map->n_out);
1170 hull = isl_basic_map_copy(map->p[0]);
1176 for (i = 0; i < map->n; ++i) {
1179 n_ineq += map->p[i]->n_ineq;
1182 set = isl_map_underlying_set(isl_map_copy(map));
1186 bset = isl_set_affine_hull(isl_set_copy(set));
1189 bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 0, n_ineq);
1193 for (i = 0; i < set->n; ++i) {
1194 for (j = 0; j < set->p[i]->n_ineq; ++j) {
1198 k = isl_basic_set_alloc_inequality(bset);
1201 isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j],
1203 is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
1208 isl_basic_set_free_inequality(bset, 1);
1212 bset = isl_basic_set_simplify(bset);
1213 bset = isl_basic_set_finalize(bset);
1214 bset = isl_basic_set_convex_hull(bset);
1216 hull = isl_basic_map_overlying_set(bset, isl_basic_map_copy(map->p[0]));
1222 isl_basic_set_free(bset);
1228 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
1230 return (struct isl_basic_set *)
1231 isl_map_simple_hull((struct isl_map *)set);