2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_map_private.h>
13 #include <isl_mat_private.h>
16 #include "isl_equalities.h"
19 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set);
21 static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
27 bmap->ineq[i] = bmap->ineq[j];
32 /* Return 1 if constraint c is redundant with respect to the constraints
33 * in bmap. If c is a lower [upper] bound in some variable and bmap
34 * does not have a lower [upper] bound in that variable, then c cannot
35 * be redundant and we do not need solve any lp.
37 int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
38 isl_int *c, isl_int *opt_n, isl_int *opt_d)
40 enum isl_lp_result res;
47 total = isl_basic_map_total_dim(*bmap);
48 for (i = 0; i < total; ++i) {
50 if (isl_int_is_zero(c[1+i]))
52 sign = isl_int_sgn(c[1+i]);
53 for (j = 0; j < (*bmap)->n_ineq; ++j)
54 if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
56 if (j == (*bmap)->n_ineq)
62 res = isl_basic_map_solve_lp(*bmap, 0, c, (*bmap)->ctx->one,
64 if (res == isl_lp_unbounded)
66 if (res == isl_lp_error)
68 if (res == isl_lp_empty) {
69 *bmap = isl_basic_map_set_to_empty(*bmap);
72 return !isl_int_is_neg(*opt_n);
75 int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
76 isl_int *c, isl_int *opt_n, isl_int *opt_d)
78 return isl_basic_map_constraint_is_redundant(
79 (struct isl_basic_map **)bset, c, opt_n, opt_d);
83 * constraints. If the minimal value along the normal of a constraint
84 * is the same if the constraint is removed, then the constraint is redundant.
86 * Alternatively, we could have intersected the basic map with the
87 * corresponding equality and the checked if the dimension was that
90 __isl_give isl_basic_map *isl_basic_map_remove_redundancies(
91 __isl_take isl_basic_map *bmap)
98 bmap = isl_basic_map_gauss(bmap, NULL);
99 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
101 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NO_REDUNDANT))
103 if (bmap->n_ineq <= 1)
106 tab = isl_tab_from_basic_map(bmap);
107 if (isl_tab_detect_implicit_equalities(tab) < 0)
109 if (isl_tab_detect_redundant(tab) < 0)
111 bmap = isl_basic_map_update_from_tab(bmap, tab);
113 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
114 ISL_F_SET(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
118 isl_basic_map_free(bmap);
122 __isl_give isl_basic_set *isl_basic_set_remove_redundancies(
123 __isl_take isl_basic_set *bset)
125 return (struct isl_basic_set *)
126 isl_basic_map_remove_redundancies((struct isl_basic_map *)bset);
129 /* Check if the set set is bound in the direction of the affine
130 * constraint c and if so, set the constant term such that the
131 * resulting constraint is a bounding constraint for the set.
133 static int uset_is_bound(struct isl_set *set, isl_int *c, unsigned len)
141 isl_int_init(opt_denom);
143 for (j = 0; j < set->n; ++j) {
144 enum isl_lp_result res;
146 if (ISL_F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
149 res = isl_basic_set_solve_lp(set->p[j],
150 0, c, set->ctx->one, &opt, &opt_denom, NULL);
151 if (res == isl_lp_unbounded)
153 if (res == isl_lp_error)
155 if (res == isl_lp_empty) {
156 set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
161 if (first || isl_int_is_neg(opt)) {
162 if (!isl_int_is_one(opt_denom))
163 isl_seq_scale(c, c, opt_denom, len);
164 isl_int_sub(c[0], c[0], opt);
169 isl_int_clear(opt_denom);
173 isl_int_clear(opt_denom);
177 __isl_give isl_basic_map *isl_basic_map_set_rational(
178 __isl_take isl_basic_set *bmap)
183 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
186 bmap = isl_basic_map_cow(bmap);
190 ISL_F_SET(bmap, ISL_BASIC_MAP_RATIONAL);
192 return isl_basic_map_finalize(bmap);
195 __isl_give isl_basic_set *isl_basic_set_set_rational(
196 __isl_take isl_basic_set *bset)
198 return isl_basic_map_set_rational(bset);
201 static struct isl_set *isl_set_set_rational(struct isl_set *set)
205 set = isl_set_cow(set);
208 for (i = 0; i < set->n; ++i) {
209 set->p[i] = isl_basic_set_set_rational(set->p[i]);
219 static struct isl_basic_set *isl_basic_set_add_equality(
220 struct isl_basic_set *bset, isl_int *c)
228 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
231 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
232 isl_assert(bset->ctx, bset->n_div == 0, goto error);
233 dim = isl_basic_set_n_dim(bset);
234 bset = isl_basic_set_cow(bset);
235 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
236 i = isl_basic_set_alloc_equality(bset);
239 isl_seq_cpy(bset->eq[i], c, 1 + dim);
242 isl_basic_set_free(bset);
246 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
250 set = isl_set_cow(set);
253 for (i = 0; i < set->n; ++i) {
254 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
264 /* Given a union of basic sets, construct the constraints for wrapping
265 * a facet around one of its ridges.
266 * In particular, if each of n the d-dimensional basic sets i in "set"
267 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
268 * and is defined by the constraints
272 * then the resulting set is of dimension n*(1+d) and has as constraints
281 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
283 struct isl_basic_set *lp;
287 unsigned dim, lp_dim;
292 dim = 1 + isl_set_n_dim(set);
295 for (i = 0; i < set->n; ++i) {
296 n_eq += set->p[i]->n_eq;
297 n_ineq += set->p[i]->n_ineq;
299 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
300 lp = isl_basic_set_set_rational(lp);
303 lp_dim = isl_basic_set_n_dim(lp);
304 k = isl_basic_set_alloc_equality(lp);
305 isl_int_set_si(lp->eq[k][0], -1);
306 for (i = 0; i < set->n; ++i) {
307 isl_int_set_si(lp->eq[k][1+dim*i], 0);
308 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
309 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
311 for (i = 0; i < set->n; ++i) {
312 k = isl_basic_set_alloc_inequality(lp);
313 isl_seq_clr(lp->ineq[k], 1+lp_dim);
314 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
316 for (j = 0; j < set->p[i]->n_eq; ++j) {
317 k = isl_basic_set_alloc_equality(lp);
318 isl_seq_clr(lp->eq[k], 1+dim*i);
319 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
320 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
323 for (j = 0; j < set->p[i]->n_ineq; ++j) {
324 k = isl_basic_set_alloc_inequality(lp);
325 isl_seq_clr(lp->ineq[k], 1+dim*i);
326 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
327 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
333 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
334 * of that facet, compute the other facet of the convex hull that contains
337 * We first transform the set such that the facet constraint becomes
341 * I.e., the facet lies in
345 * and on that facet, the constraint that defines the ridge is
349 * (This transformation is not strictly needed, all that is needed is
350 * that the ridge contains the origin.)
352 * Since the ridge contains the origin, the cone of the convex hull
353 * will be of the form
358 * with this second constraint defining the new facet.
359 * The constant a is obtained by settting x_1 in the cone of the
360 * convex hull to 1 and minimizing x_2.
361 * Now, each element in the cone of the convex hull is the sum
362 * of elements in the cones of the basic sets.
363 * If a_i is the dilation factor of basic set i, then the problem
364 * we need to solve is
377 * the constraints of each (transformed) basic set.
378 * If a = n/d, then the constraint defining the new facet (in the transformed
381 * -n x_1 + d x_2 >= 0
383 * In the original space, we need to take the same combination of the
384 * corresponding constraints "facet" and "ridge".
386 * If a = -infty = "-1/0", then we just return the original facet constraint.
387 * This means that the facet is unbounded, but has a bounded intersection
388 * with the union of sets.
390 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
391 isl_int *facet, isl_int *ridge)
395 struct isl_mat *T = NULL;
396 struct isl_basic_set *lp = NULL;
398 enum isl_lp_result res;
405 set = isl_set_copy(set);
406 set = isl_set_set_rational(set);
408 dim = 1 + isl_set_n_dim(set);
409 T = isl_mat_alloc(ctx, 3, dim);
412 isl_int_set_si(T->row[0][0], 1);
413 isl_seq_clr(T->row[0]+1, dim - 1);
414 isl_seq_cpy(T->row[1], facet, dim);
415 isl_seq_cpy(T->row[2], ridge, dim);
416 T = isl_mat_right_inverse(T);
417 set = isl_set_preimage(set, T);
421 lp = wrap_constraints(set);
422 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
425 isl_int_set_si(obj->block.data[0], 0);
426 for (i = 0; i < set->n; ++i) {
427 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
428 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
429 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
433 res = isl_basic_set_solve_lp(lp, 0,
434 obj->block.data, ctx->one, &num, &den, NULL);
435 if (res == isl_lp_ok) {
436 isl_int_neg(num, num);
437 isl_seq_combine(facet, num, facet, den, ridge, dim);
438 isl_seq_normalize(ctx, facet, dim);
443 isl_basic_set_free(lp);
445 if (res == isl_lp_error)
447 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
451 isl_basic_set_free(lp);
457 /* Compute the constraint of a facet of "set".
459 * We first compute the intersection with a bounding constraint
460 * that is orthogonal to one of the coordinate axes.
461 * If the affine hull of this intersection has only one equality,
462 * we have found a facet.
463 * Otherwise, we wrap the current bounding constraint around
464 * one of the equalities of the face (one that is not equal to
465 * the current bounding constraint).
466 * This process continues until we have found a facet.
467 * The dimension of the intersection increases by at least
468 * one on each iteration, so termination is guaranteed.
470 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
472 struct isl_set *slice = NULL;
473 struct isl_basic_set *face = NULL;
475 unsigned dim = isl_set_n_dim(set);
479 isl_assert(set->ctx, set->n > 0, goto error);
480 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
484 isl_seq_clr(bounds->row[0], dim);
485 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
486 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
489 isl_assert(set->ctx, is_bound, goto error);
490 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
494 slice = isl_set_copy(set);
495 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
496 face = isl_set_affine_hull(slice);
499 if (face->n_eq == 1) {
500 isl_basic_set_free(face);
503 for (i = 0; i < face->n_eq; ++i)
504 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
505 !isl_seq_is_neg(bounds->row[0],
506 face->eq[i], 1 + dim))
508 isl_assert(set->ctx, i < face->n_eq, goto error);
509 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
511 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
512 isl_basic_set_free(face);
517 isl_basic_set_free(face);
518 isl_mat_free(bounds);
522 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
523 * compute a hyperplane description of the facet, i.e., compute the facets
526 * We compute an affine transformation that transforms the constraint
535 * by computing the right inverse U of a matrix that starts with the rows
548 * Since z_1 is zero, we can drop this variable as well as the corresponding
549 * column of U to obtain
557 * with Q' equal to Q, but without the corresponding row.
558 * After computing the facets of the facet in the z' space,
559 * we convert them back to the x space through Q.
561 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
563 struct isl_mat *m, *U, *Q;
564 struct isl_basic_set *facet = NULL;
569 set = isl_set_copy(set);
570 dim = isl_set_n_dim(set);
571 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
574 isl_int_set_si(m->row[0][0], 1);
575 isl_seq_clr(m->row[0]+1, dim);
576 isl_seq_cpy(m->row[1], c, 1+dim);
577 U = isl_mat_right_inverse(m);
578 Q = isl_mat_right_inverse(isl_mat_copy(U));
579 U = isl_mat_drop_cols(U, 1, 1);
580 Q = isl_mat_drop_rows(Q, 1, 1);
581 set = isl_set_preimage(set, U);
582 facet = uset_convex_hull_wrap_bounded(set);
583 facet = isl_basic_set_preimage(facet, Q);
585 isl_assert(ctx, facet->n_eq == 0, goto error);
588 isl_basic_set_free(facet);
593 /* Given an initial facet constraint, compute the remaining facets.
594 * We do this by running through all facets found so far and computing
595 * the adjacent facets through wrapping, adding those facets that we
596 * hadn't already found before.
598 * For each facet we have found so far, we first compute its facets
599 * in the resulting convex hull. That is, we compute the ridges
600 * of the resulting convex hull contained in the facet.
601 * We also compute the corresponding facet in the current approximation
602 * of the convex hull. There is no need to wrap around the ridges
603 * in this facet since that would result in a facet that is already
604 * present in the current approximation.
606 * This function can still be significantly optimized by checking which of
607 * the facets of the basic sets are also facets of the convex hull and
608 * using all the facets so far to help in constructing the facets of the
611 * using the technique in section "3.1 Ridge Generation" of
612 * "Extended Convex Hull" by Fukuda et al.
614 static struct isl_basic_set *extend(struct isl_basic_set *hull,
619 struct isl_basic_set *facet = NULL;
620 struct isl_basic_set *hull_facet = NULL;
626 isl_assert(set->ctx, set->n > 0, goto error);
628 dim = isl_set_n_dim(set);
630 for (i = 0; i < hull->n_ineq; ++i) {
631 facet = compute_facet(set, hull->ineq[i]);
632 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
633 facet = isl_basic_set_gauss(facet, NULL);
634 facet = isl_basic_set_normalize_constraints(facet);
635 hull_facet = isl_basic_set_copy(hull);
636 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
637 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
638 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
639 if (!facet || !hull_facet)
641 hull = isl_basic_set_cow(hull);
642 hull = isl_basic_set_extend_dim(hull,
643 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
646 for (j = 0; j < facet->n_ineq; ++j) {
647 for (f = 0; f < hull_facet->n_ineq; ++f)
648 if (isl_seq_eq(facet->ineq[j],
649 hull_facet->ineq[f], 1 + dim))
651 if (f < hull_facet->n_ineq)
653 k = isl_basic_set_alloc_inequality(hull);
656 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
657 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
660 isl_basic_set_free(hull_facet);
661 isl_basic_set_free(facet);
663 hull = isl_basic_set_simplify(hull);
664 hull = isl_basic_set_finalize(hull);
667 isl_basic_set_free(hull_facet);
668 isl_basic_set_free(facet);
669 isl_basic_set_free(hull);
673 /* Special case for computing the convex hull of a one dimensional set.
674 * We simply collect the lower and upper bounds of each basic set
675 * and the biggest of those.
677 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
679 struct isl_mat *c = NULL;
680 isl_int *lower = NULL;
681 isl_int *upper = NULL;
684 struct isl_basic_set *hull;
686 for (i = 0; i < set->n; ++i) {
687 set->p[i] = isl_basic_set_simplify(set->p[i]);
691 set = isl_set_remove_empty_parts(set);
694 isl_assert(set->ctx, set->n > 0, goto error);
695 c = isl_mat_alloc(set->ctx, 2, 2);
699 if (set->p[0]->n_eq > 0) {
700 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
703 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
704 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
705 isl_seq_neg(upper, set->p[0]->eq[0], 2);
707 isl_seq_neg(lower, set->p[0]->eq[0], 2);
708 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
711 for (j = 0; j < set->p[0]->n_ineq; ++j) {
712 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
714 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
717 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
724 for (i = 0; i < set->n; ++i) {
725 struct isl_basic_set *bset = set->p[i];
729 for (j = 0; j < bset->n_eq; ++j) {
733 isl_int_mul(a, lower[0], bset->eq[j][1]);
734 isl_int_mul(b, lower[1], bset->eq[j][0]);
735 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
736 isl_seq_cpy(lower, bset->eq[j], 2);
737 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
738 isl_seq_neg(lower, bset->eq[j], 2);
741 isl_int_mul(a, upper[0], bset->eq[j][1]);
742 isl_int_mul(b, upper[1], bset->eq[j][0]);
743 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
744 isl_seq_neg(upper, bset->eq[j], 2);
745 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
746 isl_seq_cpy(upper, bset->eq[j], 2);
749 for (j = 0; j < bset->n_ineq; ++j) {
750 if (isl_int_is_pos(bset->ineq[j][1]))
752 if (isl_int_is_neg(bset->ineq[j][1]))
754 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
755 isl_int_mul(a, lower[0], bset->ineq[j][1]);
756 isl_int_mul(b, lower[1], bset->ineq[j][0]);
757 if (isl_int_lt(a, b))
758 isl_seq_cpy(lower, bset->ineq[j], 2);
760 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
761 isl_int_mul(a, upper[0], bset->ineq[j][1]);
762 isl_int_mul(b, upper[1], bset->ineq[j][0]);
763 if (isl_int_gt(a, b))
764 isl_seq_cpy(upper, bset->ineq[j], 2);
775 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
776 hull = isl_basic_set_set_rational(hull);
780 k = isl_basic_set_alloc_inequality(hull);
781 isl_seq_cpy(hull->ineq[k], lower, 2);
784 k = isl_basic_set_alloc_inequality(hull);
785 isl_seq_cpy(hull->ineq[k], upper, 2);
787 hull = isl_basic_set_finalize(hull);
797 /* Project out final n dimensions using Fourier-Motzkin */
798 static struct isl_set *set_project_out(struct isl_ctx *ctx,
799 struct isl_set *set, unsigned n)
801 return isl_set_remove_dims(set, isl_dim_set, isl_set_n_dim(set) - n, n);
804 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
806 struct isl_basic_set *convex_hull;
811 if (isl_set_is_empty(set))
812 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
814 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
819 /* Compute the convex hull of a pair of basic sets without any parameters or
820 * integer divisions using Fourier-Motzkin elimination.
821 * The convex hull is the set of all points that can be written as
822 * the sum of points from both basic sets (in homogeneous coordinates).
823 * We set up the constraints in a space with dimensions for each of
824 * the three sets and then project out the dimensions corresponding
825 * to the two original basic sets, retaining only those corresponding
826 * to the convex hull.
828 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
829 struct isl_basic_set *bset2)
832 struct isl_basic_set *bset[2];
833 struct isl_basic_set *hull = NULL;
836 if (!bset1 || !bset2)
839 dim = isl_basic_set_n_dim(bset1);
840 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
841 1 + dim + bset1->n_eq + bset2->n_eq,
842 2 + bset1->n_ineq + bset2->n_ineq);
845 for (i = 0; i < 2; ++i) {
846 for (j = 0; j < bset[i]->n_eq; ++j) {
847 k = isl_basic_set_alloc_equality(hull);
850 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
851 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
852 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
855 for (j = 0; j < bset[i]->n_ineq; ++j) {
856 k = isl_basic_set_alloc_inequality(hull);
859 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
860 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
861 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
862 bset[i]->ineq[j], 1+dim);
864 k = isl_basic_set_alloc_inequality(hull);
867 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
868 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
870 for (j = 0; j < 1+dim; ++j) {
871 k = isl_basic_set_alloc_equality(hull);
874 isl_seq_clr(hull->eq[k], 1+2+3*dim);
875 isl_int_set_si(hull->eq[k][j], -1);
876 isl_int_set_si(hull->eq[k][1+dim+j], 1);
877 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
879 hull = isl_basic_set_set_rational(hull);
880 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
881 hull = isl_basic_set_remove_redundancies(hull);
882 isl_basic_set_free(bset1);
883 isl_basic_set_free(bset2);
886 isl_basic_set_free(bset1);
887 isl_basic_set_free(bset2);
888 isl_basic_set_free(hull);
892 /* Is the set bounded for each value of the parameters?
894 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
901 if (isl_basic_set_fast_is_empty(bset))
904 tab = isl_tab_from_recession_cone(bset, 1);
905 bounded = isl_tab_cone_is_bounded(tab);
910 /* Is the image bounded for each value of the parameters and
911 * the domain variables?
913 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
915 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
916 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
919 bmap = isl_basic_map_copy(bmap);
920 bmap = isl_basic_map_cow(bmap);
921 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
922 isl_dim_in, 0, n_in);
923 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
924 isl_basic_map_free(bmap);
929 /* Is the set bounded for each value of the parameters?
931 int isl_set_is_bounded(__isl_keep isl_set *set)
938 for (i = 0; i < set->n; ++i) {
939 int bounded = isl_basic_set_is_bounded(set->p[i]);
940 if (!bounded || bounded < 0)
946 /* Compute the lineality space of the convex hull of bset1 and bset2.
948 * We first compute the intersection of the recession cone of bset1
949 * with the negative of the recession cone of bset2 and then compute
950 * the linear hull of the resulting cone.
952 static struct isl_basic_set *induced_lineality_space(
953 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
956 struct isl_basic_set *lin = NULL;
959 if (!bset1 || !bset2)
962 dim = isl_basic_set_total_dim(bset1);
963 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
964 bset1->n_eq + bset2->n_eq,
965 bset1->n_ineq + bset2->n_ineq);
966 lin = isl_basic_set_set_rational(lin);
969 for (i = 0; i < bset1->n_eq; ++i) {
970 k = isl_basic_set_alloc_equality(lin);
973 isl_int_set_si(lin->eq[k][0], 0);
974 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
976 for (i = 0; i < bset1->n_ineq; ++i) {
977 k = isl_basic_set_alloc_inequality(lin);
980 isl_int_set_si(lin->ineq[k][0], 0);
981 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
983 for (i = 0; i < bset2->n_eq; ++i) {
984 k = isl_basic_set_alloc_equality(lin);
987 isl_int_set_si(lin->eq[k][0], 0);
988 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
990 for (i = 0; i < bset2->n_ineq; ++i) {
991 k = isl_basic_set_alloc_inequality(lin);
994 isl_int_set_si(lin->ineq[k][0], 0);
995 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
998 isl_basic_set_free(bset1);
999 isl_basic_set_free(bset2);
1000 return isl_basic_set_affine_hull(lin);
1002 isl_basic_set_free(lin);
1003 isl_basic_set_free(bset1);
1004 isl_basic_set_free(bset2);
1008 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1010 /* Given a set and a linear space "lin" of dimension n > 0,
1011 * project the linear space from the set, compute the convex hull
1012 * and then map the set back to the original space.
1018 * describe the linear space. We first compute the Hermite normal
1019 * form H = M U of M = H Q, to obtain
1023 * The last n rows of H will be zero, so the last n variables of x' = Q x
1024 * are the one we want to project out. We do this by transforming each
1025 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1026 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1027 * we transform the hull back to the original space as A' Q_1 x >= b',
1028 * with Q_1 all but the last n rows of Q.
1030 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1031 struct isl_basic_set *lin)
1033 unsigned total = isl_basic_set_total_dim(lin);
1035 struct isl_basic_set *hull;
1036 struct isl_mat *M, *U, *Q;
1040 lin_dim = total - lin->n_eq;
1041 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1042 M = isl_mat_left_hermite(M, 0, &U, &Q);
1046 isl_basic_set_free(lin);
1048 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1050 U = isl_mat_lin_to_aff(U);
1051 Q = isl_mat_lin_to_aff(Q);
1053 set = isl_set_preimage(set, U);
1054 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1055 hull = uset_convex_hull(set);
1056 hull = isl_basic_set_preimage(hull, Q);
1060 isl_basic_set_free(lin);
1065 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1066 * set up an LP for solving
1068 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1070 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1071 * The next \alpha{ij} correspond to the equalities and come in pairs.
1072 * The final \alpha{ij} correspond to the inequalities.
1074 static struct isl_basic_set *valid_direction_lp(
1075 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1077 struct isl_dim *dim;
1078 struct isl_basic_set *lp;
1083 if (!bset1 || !bset2)
1085 d = 1 + isl_basic_set_total_dim(bset1);
1087 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1088 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1089 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1092 for (i = 0; i < n; ++i) {
1093 k = isl_basic_set_alloc_inequality(lp);
1096 isl_seq_clr(lp->ineq[k] + 1, n);
1097 isl_int_set_si(lp->ineq[k][0], -1);
1098 isl_int_set_si(lp->ineq[k][1 + i], 1);
1100 for (i = 0; i < d; ++i) {
1101 k = isl_basic_set_alloc_equality(lp);
1105 isl_int_set_si(lp->eq[k][n], 0); n++;
1106 /* positivity constraint 1 >= 0 */
1107 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1108 for (j = 0; j < bset1->n_eq; ++j) {
1109 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1110 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1112 for (j = 0; j < bset1->n_ineq; ++j) {
1113 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1115 /* positivity constraint 1 >= 0 */
1116 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1117 for (j = 0; j < bset2->n_eq; ++j) {
1118 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1119 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1121 for (j = 0; j < bset2->n_ineq; ++j) {
1122 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1125 lp = isl_basic_set_gauss(lp, NULL);
1126 isl_basic_set_free(bset1);
1127 isl_basic_set_free(bset2);
1130 isl_basic_set_free(bset1);
1131 isl_basic_set_free(bset2);
1135 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1136 * for all rays in the homogeneous space of the two cones that correspond
1137 * to the input polyhedra bset1 and bset2.
1139 * We compute s as a vector that satisfies
1141 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1143 * with h_{ij} the normals of the facets of polyhedron i
1144 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1145 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1146 * We first set up an LP with as variables the \alpha{ij}.
1147 * In this formulation, for each polyhedron i,
1148 * the first constraint is the positivity constraint, followed by pairs
1149 * of variables for the equalities, followed by variables for the inequalities.
1150 * We then simply pick a feasible solution and compute s using (*).
1152 * Note that we simply pick any valid direction and make no attempt
1153 * to pick a "good" or even the "best" valid direction.
1155 static struct isl_vec *valid_direction(
1156 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1158 struct isl_basic_set *lp;
1159 struct isl_tab *tab;
1160 struct isl_vec *sample = NULL;
1161 struct isl_vec *dir;
1166 if (!bset1 || !bset2)
1168 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1169 isl_basic_set_copy(bset2));
1170 tab = isl_tab_from_basic_set(lp);
1171 sample = isl_tab_get_sample_value(tab);
1173 isl_basic_set_free(lp);
1176 d = isl_basic_set_total_dim(bset1);
1177 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1180 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1182 /* positivity constraint 1 >= 0 */
1183 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1184 for (i = 0; i < bset1->n_eq; ++i) {
1185 isl_int_sub(sample->block.data[n],
1186 sample->block.data[n], sample->block.data[n+1]);
1187 isl_seq_combine(dir->block.data,
1188 bset1->ctx->one, dir->block.data,
1189 sample->block.data[n], bset1->eq[i], 1 + d);
1193 for (i = 0; i < bset1->n_ineq; ++i)
1194 isl_seq_combine(dir->block.data,
1195 bset1->ctx->one, dir->block.data,
1196 sample->block.data[n++], bset1->ineq[i], 1 + d);
1197 isl_vec_free(sample);
1198 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1199 isl_basic_set_free(bset1);
1200 isl_basic_set_free(bset2);
1203 isl_vec_free(sample);
1204 isl_basic_set_free(bset1);
1205 isl_basic_set_free(bset2);
1209 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1210 * compute b_i' + A_i' x' >= 0, with
1212 * [ b_i A_i ] [ y' ] [ y' ]
1213 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1215 * In particular, add the "positivity constraint" and then perform
1218 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1225 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1226 k = isl_basic_set_alloc_inequality(bset);
1229 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1230 isl_int_set_si(bset->ineq[k][0], 1);
1231 bset = isl_basic_set_preimage(bset, T);
1235 isl_basic_set_free(bset);
1239 /* Compute the convex hull of a pair of basic sets without any parameters or
1240 * integer divisions, where the convex hull is known to be pointed,
1241 * but the basic sets may be unbounded.
1243 * We turn this problem into the computation of a convex hull of a pair
1244 * _bounded_ polyhedra by "changing the direction of the homogeneous
1245 * dimension". This idea is due to Matthias Koeppe.
1247 * Consider the cones in homogeneous space that correspond to the
1248 * input polyhedra. The rays of these cones are also rays of the
1249 * polyhedra if the coordinate that corresponds to the homogeneous
1250 * dimension is zero. That is, if the inner product of the rays
1251 * with the homogeneous direction is zero.
1252 * The cones in the homogeneous space can also be considered to
1253 * correspond to other pairs of polyhedra by chosing a different
1254 * homogeneous direction. To ensure that both of these polyhedra
1255 * are bounded, we need to make sure that all rays of the cones
1256 * correspond to vertices and not to rays.
1257 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1258 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1259 * The vector s is computed in valid_direction.
1261 * Note that we need to consider _all_ rays of the cones and not just
1262 * the rays that correspond to rays in the polyhedra. If we were to
1263 * only consider those rays and turn them into vertices, then we
1264 * may inadvertently turn some vertices into rays.
1266 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1267 * We therefore transform the two polyhedra such that the selected
1268 * direction is mapped onto this standard direction and then proceed
1269 * with the normal computation.
1270 * Let S be a non-singular square matrix with s as its first row,
1271 * then we want to map the polyhedra to the space
1273 * [ y' ] [ y ] [ y ] [ y' ]
1274 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1276 * We take S to be the unimodular completion of s to limit the growth
1277 * of the coefficients in the following computations.
1279 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1280 * We first move to the homogeneous dimension
1282 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1283 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1285 * Then we change directoin
1287 * [ b_i A_i ] [ y' ] [ y' ]
1288 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1290 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1291 * resulting in b' + A' x' >= 0, which we then convert back
1294 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1296 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1298 static struct isl_basic_set *convex_hull_pair_pointed(
1299 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1301 struct isl_ctx *ctx = NULL;
1302 struct isl_vec *dir = NULL;
1303 struct isl_mat *T = NULL;
1304 struct isl_mat *T2 = NULL;
1305 struct isl_basic_set *hull;
1306 struct isl_set *set;
1308 if (!bset1 || !bset2)
1311 dir = valid_direction(isl_basic_set_copy(bset1),
1312 isl_basic_set_copy(bset2));
1315 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1318 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1319 T = isl_mat_unimodular_complete(T, 1);
1320 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1322 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1323 bset2 = homogeneous_map(bset2, T2);
1324 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1325 set = isl_set_add_basic_set(set, bset1);
1326 set = isl_set_add_basic_set(set, bset2);
1327 hull = uset_convex_hull(set);
1328 hull = isl_basic_set_preimage(hull, T);
1335 isl_basic_set_free(bset1);
1336 isl_basic_set_free(bset2);
1340 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1341 static struct isl_basic_set *modulo_affine_hull(
1342 struct isl_set *set, struct isl_basic_set *affine_hull);
1344 /* Compute the convex hull of a pair of basic sets without any parameters or
1345 * integer divisions.
1347 * This function is called from uset_convex_hull_unbounded, which
1348 * means that the complete convex hull is unbounded. Some pairs
1349 * of basic sets may still be bounded, though.
1350 * They may even lie inside a lower dimensional space, in which
1351 * case they need to be handled inside their affine hull since
1352 * the main algorithm assumes that the result is full-dimensional.
1354 * If the convex hull of the two basic sets would have a non-trivial
1355 * lineality space, we first project out this lineality space.
1357 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1358 struct isl_basic_set *bset2)
1360 isl_basic_set *lin, *aff;
1361 int bounded1, bounded2;
1363 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1364 return convex_hull_pair_elim(bset1, bset2);
1366 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1367 isl_basic_set_copy(bset2)));
1371 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1372 isl_basic_set_free(aff);
1374 bounded1 = isl_basic_set_is_bounded(bset1);
1375 bounded2 = isl_basic_set_is_bounded(bset2);
1377 if (bounded1 < 0 || bounded2 < 0)
1380 if (bounded1 && bounded2)
1381 uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1383 if (bounded1 || bounded2)
1384 return convex_hull_pair_pointed(bset1, bset2);
1386 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1387 isl_basic_set_copy(bset2));
1390 if (isl_basic_set_is_universe(lin)) {
1391 isl_basic_set_free(bset1);
1392 isl_basic_set_free(bset2);
1395 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1396 struct isl_set *set;
1397 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1398 set = isl_set_add_basic_set(set, bset1);
1399 set = isl_set_add_basic_set(set, bset2);
1400 return modulo_lineality(set, lin);
1402 isl_basic_set_free(lin);
1404 return convex_hull_pair_pointed(bset1, bset2);
1406 isl_basic_set_free(bset1);
1407 isl_basic_set_free(bset2);
1411 /* Compute the lineality space of a basic set.
1412 * We currently do not allow the basic set to have any divs.
1413 * We basically just drop the constants and turn every inequality
1416 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1419 struct isl_basic_set *lin = NULL;
1424 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1425 dim = isl_basic_set_total_dim(bset);
1427 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1430 for (i = 0; i < bset->n_eq; ++i) {
1431 k = isl_basic_set_alloc_equality(lin);
1434 isl_int_set_si(lin->eq[k][0], 0);
1435 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1437 lin = isl_basic_set_gauss(lin, NULL);
1440 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1441 k = isl_basic_set_alloc_equality(lin);
1444 isl_int_set_si(lin->eq[k][0], 0);
1445 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1446 lin = isl_basic_set_gauss(lin, NULL);
1450 isl_basic_set_free(bset);
1453 isl_basic_set_free(lin);
1454 isl_basic_set_free(bset);
1458 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1459 * "underlying" set "set".
1461 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1464 struct isl_set *lin = NULL;
1469 struct isl_dim *dim = isl_set_get_dim(set);
1471 return isl_basic_set_empty(dim);
1474 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1475 for (i = 0; i < set->n; ++i)
1476 lin = isl_set_add_basic_set(lin,
1477 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1479 return isl_set_affine_hull(lin);
1482 /* Compute the convex hull of a set without any parameters or
1483 * integer divisions.
1484 * In each step, we combined two basic sets until only one
1485 * basic set is left.
1486 * The input basic sets are assumed not to have a non-trivial
1487 * lineality space. If any of the intermediate results has
1488 * a non-trivial lineality space, it is projected out.
1490 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1492 struct isl_basic_set *convex_hull = NULL;
1494 convex_hull = isl_set_copy_basic_set(set);
1495 set = isl_set_drop_basic_set(set, convex_hull);
1498 while (set->n > 0) {
1499 struct isl_basic_set *t;
1500 t = isl_set_copy_basic_set(set);
1503 set = isl_set_drop_basic_set(set, t);
1506 convex_hull = convex_hull_pair(convex_hull, t);
1509 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1512 if (isl_basic_set_is_universe(t)) {
1513 isl_basic_set_free(convex_hull);
1517 if (t->n_eq < isl_basic_set_total_dim(t)) {
1518 set = isl_set_add_basic_set(set, convex_hull);
1519 return modulo_lineality(set, t);
1521 isl_basic_set_free(t);
1527 isl_basic_set_free(convex_hull);
1531 /* Compute an initial hull for wrapping containing a single initial
1533 * This function assumes that the given set is bounded.
1535 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1536 struct isl_set *set)
1538 struct isl_mat *bounds = NULL;
1544 bounds = initial_facet_constraint(set);
1547 k = isl_basic_set_alloc_inequality(hull);
1550 dim = isl_set_n_dim(set);
1551 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1552 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1553 isl_mat_free(bounds);
1557 isl_basic_set_free(hull);
1558 isl_mat_free(bounds);
1562 struct max_constraint {
1568 static int max_constraint_equal(const void *entry, const void *val)
1570 struct max_constraint *a = (struct max_constraint *)entry;
1571 isl_int *b = (isl_int *)val;
1573 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1576 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1577 isl_int *con, unsigned len, int n, int ineq)
1579 struct isl_hash_table_entry *entry;
1580 struct max_constraint *c;
1583 c_hash = isl_seq_get_hash(con + 1, len);
1584 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1590 isl_hash_table_remove(ctx, table, entry);
1594 if (isl_int_gt(c->c->row[0][0], con[0]))
1596 if (isl_int_eq(c->c->row[0][0], con[0])) {
1601 c->c = isl_mat_cow(c->c);
1602 isl_int_set(c->c->row[0][0], con[0]);
1606 /* Check whether the constraint hash table "table" constains the constraint
1609 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1610 isl_int *con, unsigned len, int n)
1612 struct isl_hash_table_entry *entry;
1613 struct max_constraint *c;
1616 c_hash = isl_seq_get_hash(con + 1, len);
1617 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1624 return isl_int_eq(c->c->row[0][0], con[0]);
1627 /* Check for inequality constraints of a basic set without equalities
1628 * such that the same or more stringent copies of the constraint appear
1629 * in all of the basic sets. Such constraints are necessarily facet
1630 * constraints of the convex hull.
1632 * If the resulting basic set is by chance identical to one of
1633 * the basic sets in "set", then we know that this basic set contains
1634 * all other basic sets and is therefore the convex hull of set.
1635 * In this case we set *is_hull to 1.
1637 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1638 struct isl_set *set, int *is_hull)
1641 int min_constraints;
1643 struct max_constraint *constraints = NULL;
1644 struct isl_hash_table *table = NULL;
1649 for (i = 0; i < set->n; ++i)
1650 if (set->p[i]->n_eq == 0)
1654 min_constraints = set->p[i]->n_ineq;
1656 for (i = best + 1; i < set->n; ++i) {
1657 if (set->p[i]->n_eq != 0)
1659 if (set->p[i]->n_ineq >= min_constraints)
1661 min_constraints = set->p[i]->n_ineq;
1664 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1668 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1669 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1672 total = isl_dim_total(set->dim);
1673 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1674 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1675 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1676 if (!constraints[i].c)
1678 constraints[i].ineq = 1;
1680 for (i = 0; i < min_constraints; ++i) {
1681 struct isl_hash_table_entry *entry;
1683 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1684 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1685 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1688 isl_assert(hull->ctx, !entry->data, goto error);
1689 entry->data = &constraints[i];
1693 for (s = 0; s < set->n; ++s) {
1697 for (i = 0; i < set->p[s]->n_eq; ++i) {
1698 isl_int *eq = set->p[s]->eq[i];
1699 for (j = 0; j < 2; ++j) {
1700 isl_seq_neg(eq, eq, 1 + total);
1701 update_constraint(hull->ctx, table,
1705 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1706 isl_int *ineq = set->p[s]->ineq[i];
1707 update_constraint(hull->ctx, table, ineq, total, n,
1708 set->p[s]->n_eq == 0);
1713 for (i = 0; i < min_constraints; ++i) {
1714 if (constraints[i].count < n)
1716 if (!constraints[i].ineq)
1718 j = isl_basic_set_alloc_inequality(hull);
1721 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1724 for (s = 0; s < set->n; ++s) {
1725 if (set->p[s]->n_eq)
1727 if (set->p[s]->n_ineq != hull->n_ineq)
1729 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1730 isl_int *ineq = set->p[s]->ineq[i];
1731 if (!has_constraint(hull->ctx, table, ineq, total, n))
1734 if (i == set->p[s]->n_ineq)
1738 isl_hash_table_clear(table);
1739 for (i = 0; i < min_constraints; ++i)
1740 isl_mat_free(constraints[i].c);
1745 isl_hash_table_clear(table);
1748 for (i = 0; i < min_constraints; ++i)
1749 isl_mat_free(constraints[i].c);
1754 /* Create a template for the convex hull of "set" and fill it up
1755 * obvious facet constraints, if any. If the result happens to
1756 * be the convex hull of "set" then *is_hull is set to 1.
1758 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1760 struct isl_basic_set *hull;
1765 for (i = 0; i < set->n; ++i) {
1766 n_ineq += set->p[i]->n_eq;
1767 n_ineq += set->p[i]->n_ineq;
1769 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1770 hull = isl_basic_set_set_rational(hull);
1773 return common_constraints(hull, set, is_hull);
1776 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1778 struct isl_basic_set *hull;
1781 hull = proto_hull(set, &is_hull);
1782 if (hull && !is_hull) {
1783 if (hull->n_ineq == 0)
1784 hull = initial_hull(hull, set);
1785 hull = extend(hull, set);
1792 /* Compute the convex hull of a set without any parameters or
1793 * integer divisions. Depending on whether the set is bounded,
1794 * we pass control to the wrapping based convex hull or
1795 * the Fourier-Motzkin elimination based convex hull.
1796 * We also handle a few special cases before checking the boundedness.
1798 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1800 struct isl_basic_set *convex_hull = NULL;
1801 struct isl_basic_set *lin;
1803 if (isl_set_n_dim(set) == 0)
1804 return convex_hull_0d(set);
1806 set = isl_set_coalesce(set);
1807 set = isl_set_set_rational(set);
1814 convex_hull = isl_basic_set_copy(set->p[0]);
1818 if (isl_set_n_dim(set) == 1)
1819 return convex_hull_1d(set);
1821 if (isl_set_is_bounded(set) &&
1822 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1823 return uset_convex_hull_wrap(set);
1825 lin = uset_combined_lineality_space(isl_set_copy(set));
1828 if (isl_basic_set_is_universe(lin)) {
1832 if (lin->n_eq < isl_basic_set_total_dim(lin))
1833 return modulo_lineality(set, lin);
1834 isl_basic_set_free(lin);
1836 return uset_convex_hull_unbounded(set);
1839 isl_basic_set_free(convex_hull);
1843 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1844 * without parameters or divs and where the convex hull of set is
1845 * known to be full-dimensional.
1847 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1849 struct isl_basic_set *convex_hull = NULL;
1854 if (isl_set_n_dim(set) == 0) {
1855 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1857 convex_hull = isl_basic_set_set_rational(convex_hull);
1861 set = isl_set_set_rational(set);
1862 set = isl_set_coalesce(set);
1866 convex_hull = isl_basic_set_copy(set->p[0]);
1870 if (isl_set_n_dim(set) == 1)
1871 return convex_hull_1d(set);
1873 return uset_convex_hull_wrap(set);
1879 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1880 * We first remove the equalities (transforming the set), compute the
1881 * convex hull of the transformed set and then add the equalities back
1882 * (after performing the inverse transformation.
1884 static struct isl_basic_set *modulo_affine_hull(
1885 struct isl_set *set, struct isl_basic_set *affine_hull)
1889 struct isl_basic_set *dummy;
1890 struct isl_basic_set *convex_hull;
1892 dummy = isl_basic_set_remove_equalities(
1893 isl_basic_set_copy(affine_hull), &T, &T2);
1896 isl_basic_set_free(dummy);
1897 set = isl_set_preimage(set, T);
1898 convex_hull = uset_convex_hull(set);
1899 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1900 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1903 isl_basic_set_free(affine_hull);
1908 /* Compute the convex hull of a map.
1910 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1911 * specifically, the wrapping of facets to obtain new facets.
1913 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1915 struct isl_basic_set *bset;
1916 struct isl_basic_map *model = NULL;
1917 struct isl_basic_set *affine_hull = NULL;
1918 struct isl_basic_map *convex_hull = NULL;
1919 struct isl_set *set = NULL;
1920 struct isl_ctx *ctx;
1927 convex_hull = isl_basic_map_empty_like_map(map);
1932 map = isl_map_detect_equalities(map);
1933 map = isl_map_align_divs(map);
1936 model = isl_basic_map_copy(map->p[0]);
1937 set = isl_map_underlying_set(map);
1941 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1944 if (affine_hull->n_eq != 0)
1945 bset = modulo_affine_hull(set, affine_hull);
1947 isl_basic_set_free(affine_hull);
1948 bset = uset_convex_hull(set);
1951 convex_hull = isl_basic_map_overlying_set(bset, model);
1955 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1956 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1957 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1961 isl_basic_map_free(model);
1965 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1967 return (struct isl_basic_set *)
1968 isl_map_convex_hull((struct isl_map *)set);
1971 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1973 isl_basic_map *hull;
1975 hull = isl_map_convex_hull(map);
1976 return isl_basic_map_remove_divs(hull);
1979 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1981 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
1984 struct sh_data_entry {
1985 struct isl_hash_table *table;
1986 struct isl_tab *tab;
1989 /* Holds the data needed during the simple hull computation.
1991 * n the number of basic sets in the original set
1992 * hull_table a hash table of already computed constraints
1993 * in the simple hull
1994 * p for each basic set,
1995 * table a hash table of the constraints
1996 * tab the tableau corresponding to the basic set
1999 struct isl_ctx *ctx;
2001 struct isl_hash_table *hull_table;
2002 struct sh_data_entry p[1];
2005 static void sh_data_free(struct sh_data *data)
2011 isl_hash_table_free(data->ctx, data->hull_table);
2012 for (i = 0; i < data->n; ++i) {
2013 isl_hash_table_free(data->ctx, data->p[i].table);
2014 isl_tab_free(data->p[i].tab);
2019 struct ineq_cmp_data {
2024 static int has_ineq(const void *entry, const void *val)
2026 isl_int *row = (isl_int *)entry;
2027 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2029 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2030 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2033 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2034 isl_int *ineq, unsigned len)
2037 struct ineq_cmp_data v;
2038 struct isl_hash_table_entry *entry;
2042 c_hash = isl_seq_get_hash(ineq + 1, len);
2043 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2050 /* Fill hash table "table" with the constraints of "bset".
2051 * Equalities are added as two inequalities.
2052 * The value in the hash table is a pointer to the (in)equality of "bset".
2054 static int hash_basic_set(struct isl_hash_table *table,
2055 struct isl_basic_set *bset)
2058 unsigned dim = isl_basic_set_total_dim(bset);
2060 for (i = 0; i < bset->n_eq; ++i) {
2061 for (j = 0; j < 2; ++j) {
2062 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2063 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2067 for (i = 0; i < bset->n_ineq; ++i) {
2068 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2074 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2076 struct sh_data *data;
2079 data = isl_calloc(set->ctx, struct sh_data,
2080 sizeof(struct sh_data) +
2081 (set->n - 1) * sizeof(struct sh_data_entry));
2084 data->ctx = set->ctx;
2086 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2087 if (!data->hull_table)
2089 for (i = 0; i < set->n; ++i) {
2090 data->p[i].table = isl_hash_table_alloc(set->ctx,
2091 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2092 if (!data->p[i].table)
2094 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2103 /* Check if inequality "ineq" is a bound for basic set "j" or if
2104 * it can be relaxed (by increasing the constant term) to become
2105 * a bound for that basic set. In the latter case, the constant
2107 * Return 1 if "ineq" is a bound
2108 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2109 * -1 if some error occurred
2111 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2114 enum isl_lp_result res;
2117 if (!data->p[j].tab) {
2118 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2119 if (!data->p[j].tab)
2125 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2127 if (res == isl_lp_ok && isl_int_is_neg(opt))
2128 isl_int_sub(ineq[0], ineq[0], opt);
2132 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2133 res == isl_lp_unbounded ? 0 : -1;
2136 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2137 * become a bound on the whole set. If so, add the (relaxed) inequality
2140 * We first check if "hull" already contains a translate of the inequality.
2141 * If so, we are done.
2142 * Then, we check if any of the previous basic sets contains a translate
2143 * of the inequality. If so, then we have already considered this
2144 * inequality and we are done.
2145 * Otherwise, for each basic set other than "i", we check if the inequality
2146 * is a bound on the basic set.
2147 * For previous basic sets, we know that they do not contain a translate
2148 * of the inequality, so we directly call is_bound.
2149 * For following basic sets, we first check if a translate of the
2150 * inequality appears in its description and if so directly update
2151 * the inequality accordingly.
2153 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2154 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2157 struct ineq_cmp_data v;
2158 struct isl_hash_table_entry *entry;
2164 v.len = isl_basic_set_total_dim(hull);
2166 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2168 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2173 for (j = 0; j < i; ++j) {
2174 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2175 c_hash, has_ineq, &v, 0);
2182 k = isl_basic_set_alloc_inequality(hull);
2183 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2187 for (j = 0; j < i; ++j) {
2189 bound = is_bound(data, set, j, hull->ineq[k]);
2196 isl_basic_set_free_inequality(hull, 1);
2200 for (j = i + 1; j < set->n; ++j) {
2203 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2204 c_hash, has_ineq, &v, 0);
2206 ineq_j = entry->data;
2207 neg = isl_seq_is_neg(ineq_j + 1,
2208 hull->ineq[k] + 1, v.len);
2210 isl_int_neg(ineq_j[0], ineq_j[0]);
2211 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2212 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2214 isl_int_neg(ineq_j[0], ineq_j[0]);
2217 bound = is_bound(data, set, j, hull->ineq[k]);
2224 isl_basic_set_free_inequality(hull, 1);
2228 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2232 entry->data = hull->ineq[k];
2236 isl_basic_set_free(hull);
2240 /* Check if any inequality from basic set "i" can be relaxed to
2241 * become a bound on the whole set. If so, add the (relaxed) inequality
2244 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2245 struct sh_data *data, struct isl_set *set, int i)
2248 unsigned dim = isl_basic_set_total_dim(bset);
2250 for (j = 0; j < set->p[i]->n_eq; ++j) {
2251 for (k = 0; k < 2; ++k) {
2252 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2253 bset = add_bound(bset, data, set, i, set->p[i]->eq[j]);
2256 for (j = 0; j < set->p[i]->n_ineq; ++j)
2257 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2261 /* Compute a superset of the convex hull of set that is described
2262 * by only translates of the constraints in the constituents of set.
2264 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2266 struct sh_data *data = NULL;
2267 struct isl_basic_set *hull = NULL;
2275 for (i = 0; i < set->n; ++i) {
2278 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2281 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2285 data = sh_data_alloc(set, n_ineq);
2289 for (i = 0; i < set->n; ++i)
2290 hull = add_bounds(hull, data, set, i);
2298 isl_basic_set_free(hull);
2303 /* Compute a superset of the convex hull of map that is described
2304 * by only translates of the constraints in the constituents of map.
2306 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2308 struct isl_set *set = NULL;
2309 struct isl_basic_map *model = NULL;
2310 struct isl_basic_map *hull;
2311 struct isl_basic_map *affine_hull;
2312 struct isl_basic_set *bset = NULL;
2317 hull = isl_basic_map_empty_like_map(map);
2322 hull = isl_basic_map_copy(map->p[0]);
2327 map = isl_map_detect_equalities(map);
2328 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2329 map = isl_map_align_divs(map);
2330 model = isl_basic_map_copy(map->p[0]);
2332 set = isl_map_underlying_set(map);
2334 bset = uset_simple_hull(set);
2336 hull = isl_basic_map_overlying_set(bset, model);
2338 hull = isl_basic_map_intersect(hull, affine_hull);
2339 hull = isl_basic_map_remove_redundancies(hull);
2340 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2341 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2346 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2348 return (struct isl_basic_set *)
2349 isl_map_simple_hull((struct isl_map *)set);
2352 /* Given a set "set", return parametric bounds on the dimension "dim".
2354 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2356 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2357 set = isl_set_copy(set);
2358 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2359 set = isl_set_eliminate_dims(set, 0, dim);
2360 return isl_set_convex_hull(set);
2363 /* Computes a "simple hull" and then check if each dimension in the
2364 * resulting hull is bounded by a symbolic constant. If not, the
2365 * hull is intersected with the corresponding bounds on the whole set.
2367 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2370 struct isl_basic_set *hull;
2371 unsigned nparam, left;
2372 int removed_divs = 0;
2374 hull = isl_set_simple_hull(isl_set_copy(set));
2378 nparam = isl_basic_set_dim(hull, isl_dim_param);
2379 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2380 int lower = 0, upper = 0;
2381 struct isl_basic_set *bounds;
2383 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2384 for (j = 0; j < hull->n_eq; ++j) {
2385 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2387 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2394 for (j = 0; j < hull->n_ineq; ++j) {
2395 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2397 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2399 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2402 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2413 if (!removed_divs) {
2414 set = isl_set_remove_divs(set);
2419 bounds = set_bounds(set, i);
2420 hull = isl_basic_set_intersect(hull, bounds);