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
12 #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 struct isl_basic_set *isl_basic_set_set_rational(struct isl_basic_set *bset)
182 if (ISL_F_ISSET(bset, ISL_BASIC_MAP_RATIONAL))
185 bset = isl_basic_set_cow(bset);
189 ISL_F_SET(bset, ISL_BASIC_MAP_RATIONAL);
191 return isl_basic_set_finalize(bset);
194 static struct isl_set *isl_set_set_rational(struct isl_set *set)
198 set = isl_set_cow(set);
201 for (i = 0; i < set->n; ++i) {
202 set->p[i] = isl_basic_set_set_rational(set->p[i]);
212 static struct isl_basic_set *isl_basic_set_add_equality(
213 struct isl_basic_set *bset, isl_int *c)
221 if (ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY))
224 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
225 isl_assert(bset->ctx, bset->n_div == 0, goto error);
226 dim = isl_basic_set_n_dim(bset);
227 bset = isl_basic_set_cow(bset);
228 bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
229 i = isl_basic_set_alloc_equality(bset);
232 isl_seq_cpy(bset->eq[i], c, 1 + dim);
235 isl_basic_set_free(bset);
239 static struct isl_set *isl_set_add_basic_set_equality(struct isl_set *set, isl_int *c)
243 set = isl_set_cow(set);
246 for (i = 0; i < set->n; ++i) {
247 set->p[i] = isl_basic_set_add_equality(set->p[i], c);
257 /* Given a union of basic sets, construct the constraints for wrapping
258 * a facet around one of its ridges.
259 * In particular, if each of n the d-dimensional basic sets i in "set"
260 * contains the origin, satisfies the constraints x_1 >= 0 and x_2 >= 0
261 * and is defined by the constraints
265 * then the resulting set is of dimension n*(1+d) and has as constraints
274 static struct isl_basic_set *wrap_constraints(struct isl_set *set)
276 struct isl_basic_set *lp;
280 unsigned dim, lp_dim;
285 dim = 1 + isl_set_n_dim(set);
288 for (i = 0; i < set->n; ++i) {
289 n_eq += set->p[i]->n_eq;
290 n_ineq += set->p[i]->n_ineq;
292 lp = isl_basic_set_alloc(set->ctx, 0, dim * set->n, 0, n_eq, n_ineq);
295 lp_dim = isl_basic_set_n_dim(lp);
296 k = isl_basic_set_alloc_equality(lp);
297 isl_int_set_si(lp->eq[k][0], -1);
298 for (i = 0; i < set->n; ++i) {
299 isl_int_set_si(lp->eq[k][1+dim*i], 0);
300 isl_int_set_si(lp->eq[k][1+dim*i+1], 1);
301 isl_seq_clr(lp->eq[k]+1+dim*i+2, dim-2);
303 for (i = 0; i < set->n; ++i) {
304 k = isl_basic_set_alloc_inequality(lp);
305 isl_seq_clr(lp->ineq[k], 1+lp_dim);
306 isl_int_set_si(lp->ineq[k][1+dim*i], 1);
308 for (j = 0; j < set->p[i]->n_eq; ++j) {
309 k = isl_basic_set_alloc_equality(lp);
310 isl_seq_clr(lp->eq[k], 1+dim*i);
311 isl_seq_cpy(lp->eq[k]+1+dim*i, set->p[i]->eq[j], dim);
312 isl_seq_clr(lp->eq[k]+1+dim*(i+1), dim*(set->n-i-1));
315 for (j = 0; j < set->p[i]->n_ineq; ++j) {
316 k = isl_basic_set_alloc_inequality(lp);
317 isl_seq_clr(lp->ineq[k], 1+dim*i);
318 isl_seq_cpy(lp->ineq[k]+1+dim*i, set->p[i]->ineq[j], dim);
319 isl_seq_clr(lp->ineq[k]+1+dim*(i+1), dim*(set->n-i-1));
325 /* Given a facet "facet" of the convex hull of "set" and a facet "ridge"
326 * of that facet, compute the other facet of the convex hull that contains
329 * We first transform the set such that the facet constraint becomes
333 * I.e., the facet lies in
337 * and on that facet, the constraint that defines the ridge is
341 * (This transformation is not strictly needed, all that is needed is
342 * that the ridge contains the origin.)
344 * Since the ridge contains the origin, the cone of the convex hull
345 * will be of the form
350 * with this second constraint defining the new facet.
351 * The constant a is obtained by settting x_1 in the cone of the
352 * convex hull to 1 and minimizing x_2.
353 * Now, each element in the cone of the convex hull is the sum
354 * of elements in the cones of the basic sets.
355 * If a_i is the dilation factor of basic set i, then the problem
356 * we need to solve is
369 * the constraints of each (transformed) basic set.
370 * If a = n/d, then the constraint defining the new facet (in the transformed
373 * -n x_1 + d x_2 >= 0
375 * In the original space, we need to take the same combination of the
376 * corresponding constraints "facet" and "ridge".
378 * If a = -infty = "-1/0", then we just return the original facet constraint.
379 * This means that the facet is unbounded, but has a bounded intersection
380 * with the union of sets.
382 isl_int *isl_set_wrap_facet(__isl_keep isl_set *set,
383 isl_int *facet, isl_int *ridge)
387 struct isl_mat *T = NULL;
388 struct isl_basic_set *lp = NULL;
390 enum isl_lp_result res;
397 set = isl_set_copy(set);
398 set = isl_set_set_rational(set);
400 dim = 1 + isl_set_n_dim(set);
401 T = isl_mat_alloc(ctx, 3, dim);
404 isl_int_set_si(T->row[0][0], 1);
405 isl_seq_clr(T->row[0]+1, dim - 1);
406 isl_seq_cpy(T->row[1], facet, dim);
407 isl_seq_cpy(T->row[2], ridge, dim);
408 T = isl_mat_right_inverse(T);
409 set = isl_set_preimage(set, T);
413 lp = wrap_constraints(set);
414 obj = isl_vec_alloc(ctx, 1 + dim*set->n);
417 isl_int_set_si(obj->block.data[0], 0);
418 for (i = 0; i < set->n; ++i) {
419 isl_seq_clr(obj->block.data + 1 + dim*i, 2);
420 isl_int_set_si(obj->block.data[1 + dim*i+2], 1);
421 isl_seq_clr(obj->block.data + 1 + dim*i+3, dim-3);
425 res = isl_basic_set_solve_lp(lp, 0,
426 obj->block.data, ctx->one, &num, &den, NULL);
427 if (res == isl_lp_ok) {
428 isl_int_neg(num, num);
429 isl_seq_combine(facet, num, facet, den, ridge, dim);
430 isl_seq_normalize(ctx, facet, dim);
435 isl_basic_set_free(lp);
437 if (res == isl_lp_error)
439 isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
443 isl_basic_set_free(lp);
449 /* Compute the constraint of a facet of "set".
451 * We first compute the intersection with a bounding constraint
452 * that is orthogonal to one of the coordinate axes.
453 * If the affine hull of this intersection has only one equality,
454 * we have found a facet.
455 * Otherwise, we wrap the current bounding constraint around
456 * one of the equalities of the face (one that is not equal to
457 * the current bounding constraint).
458 * This process continues until we have found a facet.
459 * The dimension of the intersection increases by at least
460 * one on each iteration, so termination is guaranteed.
462 static __isl_give isl_mat *initial_facet_constraint(__isl_keep isl_set *set)
464 struct isl_set *slice = NULL;
465 struct isl_basic_set *face = NULL;
467 unsigned dim = isl_set_n_dim(set);
471 isl_assert(set->ctx, set->n > 0, goto error);
472 bounds = isl_mat_alloc(set->ctx, 1, 1 + dim);
476 isl_seq_clr(bounds->row[0], dim);
477 isl_int_set_si(bounds->row[0][1 + dim - 1], 1);
478 is_bound = uset_is_bound(set, bounds->row[0], 1 + dim);
481 isl_assert(set->ctx, is_bound, goto error);
482 isl_seq_normalize(set->ctx, bounds->row[0], 1 + dim);
486 slice = isl_set_copy(set);
487 slice = isl_set_add_basic_set_equality(slice, bounds->row[0]);
488 face = isl_set_affine_hull(slice);
491 if (face->n_eq == 1) {
492 isl_basic_set_free(face);
495 for (i = 0; i < face->n_eq; ++i)
496 if (!isl_seq_eq(bounds->row[0], face->eq[i], 1 + dim) &&
497 !isl_seq_is_neg(bounds->row[0],
498 face->eq[i], 1 + dim))
500 isl_assert(set->ctx, i < face->n_eq, goto error);
501 if (!isl_set_wrap_facet(set, bounds->row[0], face->eq[i]))
503 isl_seq_normalize(set->ctx, bounds->row[0], bounds->n_col);
504 isl_basic_set_free(face);
509 isl_basic_set_free(face);
510 isl_mat_free(bounds);
514 /* Given the bounding constraint "c" of a facet of the convex hull of "set",
515 * compute a hyperplane description of the facet, i.e., compute the facets
518 * We compute an affine transformation that transforms the constraint
527 * by computing the right inverse U of a matrix that starts with the rows
540 * Since z_1 is zero, we can drop this variable as well as the corresponding
541 * column of U to obtain
549 * with Q' equal to Q, but without the corresponding row.
550 * After computing the facets of the facet in the z' space,
551 * we convert them back to the x space through Q.
553 static struct isl_basic_set *compute_facet(struct isl_set *set, isl_int *c)
555 struct isl_mat *m, *U, *Q;
556 struct isl_basic_set *facet = NULL;
561 set = isl_set_copy(set);
562 dim = isl_set_n_dim(set);
563 m = isl_mat_alloc(set->ctx, 2, 1 + dim);
566 isl_int_set_si(m->row[0][0], 1);
567 isl_seq_clr(m->row[0]+1, dim);
568 isl_seq_cpy(m->row[1], c, 1+dim);
569 U = isl_mat_right_inverse(m);
570 Q = isl_mat_right_inverse(isl_mat_copy(U));
571 U = isl_mat_drop_cols(U, 1, 1);
572 Q = isl_mat_drop_rows(Q, 1, 1);
573 set = isl_set_preimage(set, U);
574 facet = uset_convex_hull_wrap_bounded(set);
575 facet = isl_basic_set_preimage(facet, Q);
577 isl_assert(ctx, facet->n_eq == 0, goto error);
580 isl_basic_set_free(facet);
585 /* Given an initial facet constraint, compute the remaining facets.
586 * We do this by running through all facets found so far and computing
587 * the adjacent facets through wrapping, adding those facets that we
588 * hadn't already found before.
590 * For each facet we have found so far, we first compute its facets
591 * in the resulting convex hull. That is, we compute the ridges
592 * of the resulting convex hull contained in the facet.
593 * We also compute the corresponding facet in the current approximation
594 * of the convex hull. There is no need to wrap around the ridges
595 * in this facet since that would result in a facet that is already
596 * present in the current approximation.
598 * This function can still be significantly optimized by checking which of
599 * the facets of the basic sets are also facets of the convex hull and
600 * using all the facets so far to help in constructing the facets of the
603 * using the technique in section "3.1 Ridge Generation" of
604 * "Extended Convex Hull" by Fukuda et al.
606 static struct isl_basic_set *extend(struct isl_basic_set *hull,
611 struct isl_basic_set *facet = NULL;
612 struct isl_basic_set *hull_facet = NULL;
618 isl_assert(set->ctx, set->n > 0, goto error);
620 dim = isl_set_n_dim(set);
622 for (i = 0; i < hull->n_ineq; ++i) {
623 facet = compute_facet(set, hull->ineq[i]);
624 facet = isl_basic_set_add_equality(facet, hull->ineq[i]);
625 facet = isl_basic_set_gauss(facet, NULL);
626 facet = isl_basic_set_normalize_constraints(facet);
627 hull_facet = isl_basic_set_copy(hull);
628 hull_facet = isl_basic_set_add_equality(hull_facet, hull->ineq[i]);
629 hull_facet = isl_basic_set_gauss(hull_facet, NULL);
630 hull_facet = isl_basic_set_normalize_constraints(hull_facet);
631 if (!facet || !hull_facet)
633 hull = isl_basic_set_cow(hull);
634 hull = isl_basic_set_extend_dim(hull,
635 isl_dim_copy(hull->dim), 0, 0, facet->n_ineq);
638 for (j = 0; j < facet->n_ineq; ++j) {
639 for (f = 0; f < hull_facet->n_ineq; ++f)
640 if (isl_seq_eq(facet->ineq[j],
641 hull_facet->ineq[f], 1 + dim))
643 if (f < hull_facet->n_ineq)
645 k = isl_basic_set_alloc_inequality(hull);
648 isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
649 if (!isl_set_wrap_facet(set, hull->ineq[k], facet->ineq[j]))
652 isl_basic_set_free(hull_facet);
653 isl_basic_set_free(facet);
655 hull = isl_basic_set_simplify(hull);
656 hull = isl_basic_set_finalize(hull);
659 isl_basic_set_free(hull_facet);
660 isl_basic_set_free(facet);
661 isl_basic_set_free(hull);
665 /* Special case for computing the convex hull of a one dimensional set.
666 * We simply collect the lower and upper bounds of each basic set
667 * and the biggest of those.
669 static struct isl_basic_set *convex_hull_1d(struct isl_set *set)
671 struct isl_mat *c = NULL;
672 isl_int *lower = NULL;
673 isl_int *upper = NULL;
676 struct isl_basic_set *hull;
678 for (i = 0; i < set->n; ++i) {
679 set->p[i] = isl_basic_set_simplify(set->p[i]);
683 set = isl_set_remove_empty_parts(set);
686 isl_assert(set->ctx, set->n > 0, goto error);
687 c = isl_mat_alloc(set->ctx, 2, 2);
691 if (set->p[0]->n_eq > 0) {
692 isl_assert(set->ctx, set->p[0]->n_eq == 1, goto error);
695 if (isl_int_is_pos(set->p[0]->eq[0][1])) {
696 isl_seq_cpy(lower, set->p[0]->eq[0], 2);
697 isl_seq_neg(upper, set->p[0]->eq[0], 2);
699 isl_seq_neg(lower, set->p[0]->eq[0], 2);
700 isl_seq_cpy(upper, set->p[0]->eq[0], 2);
703 for (j = 0; j < set->p[0]->n_ineq; ++j) {
704 if (isl_int_is_pos(set->p[0]->ineq[j][1])) {
706 isl_seq_cpy(lower, set->p[0]->ineq[j], 2);
709 isl_seq_cpy(upper, set->p[0]->ineq[j], 2);
716 for (i = 0; i < set->n; ++i) {
717 struct isl_basic_set *bset = set->p[i];
721 for (j = 0; j < bset->n_eq; ++j) {
725 isl_int_mul(a, lower[0], bset->eq[j][1]);
726 isl_int_mul(b, lower[1], bset->eq[j][0]);
727 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
728 isl_seq_cpy(lower, bset->eq[j], 2);
729 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
730 isl_seq_neg(lower, bset->eq[j], 2);
733 isl_int_mul(a, upper[0], bset->eq[j][1]);
734 isl_int_mul(b, upper[1], bset->eq[j][0]);
735 if (isl_int_lt(a, b) && isl_int_is_pos(bset->eq[j][1]))
736 isl_seq_neg(upper, bset->eq[j], 2);
737 if (isl_int_gt(a, b) && isl_int_is_neg(bset->eq[j][1]))
738 isl_seq_cpy(upper, bset->eq[j], 2);
741 for (j = 0; j < bset->n_ineq; ++j) {
742 if (isl_int_is_pos(bset->ineq[j][1]))
744 if (isl_int_is_neg(bset->ineq[j][1]))
746 if (lower && isl_int_is_pos(bset->ineq[j][1])) {
747 isl_int_mul(a, lower[0], bset->ineq[j][1]);
748 isl_int_mul(b, lower[1], bset->ineq[j][0]);
749 if (isl_int_lt(a, b))
750 isl_seq_cpy(lower, bset->ineq[j], 2);
752 if (upper && isl_int_is_neg(bset->ineq[j][1])) {
753 isl_int_mul(a, upper[0], bset->ineq[j][1]);
754 isl_int_mul(b, upper[1], bset->ineq[j][0]);
755 if (isl_int_gt(a, b))
756 isl_seq_cpy(upper, bset->ineq[j], 2);
767 hull = isl_basic_set_alloc(set->ctx, 0, 1, 0, 0, 2);
768 hull = isl_basic_set_set_rational(hull);
772 k = isl_basic_set_alloc_inequality(hull);
773 isl_seq_cpy(hull->ineq[k], lower, 2);
776 k = isl_basic_set_alloc_inequality(hull);
777 isl_seq_cpy(hull->ineq[k], upper, 2);
779 hull = isl_basic_set_finalize(hull);
789 /* Project out final n dimensions using Fourier-Motzkin */
790 static struct isl_set *set_project_out(struct isl_ctx *ctx,
791 struct isl_set *set, unsigned n)
793 return isl_set_remove_dims(set, isl_dim_set, isl_set_n_dim(set) - n, n);
796 static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
798 struct isl_basic_set *convex_hull;
803 if (isl_set_is_empty(set))
804 convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
806 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
811 /* Compute the convex hull of a pair of basic sets without any parameters or
812 * integer divisions using Fourier-Motzkin elimination.
813 * The convex hull is the set of all points that can be written as
814 * the sum of points from both basic sets (in homogeneous coordinates).
815 * We set up the constraints in a space with dimensions for each of
816 * the three sets and then project out the dimensions corresponding
817 * to the two original basic sets, retaining only those corresponding
818 * to the convex hull.
820 static struct isl_basic_set *convex_hull_pair_elim(struct isl_basic_set *bset1,
821 struct isl_basic_set *bset2)
824 struct isl_basic_set *bset[2];
825 struct isl_basic_set *hull = NULL;
828 if (!bset1 || !bset2)
831 dim = isl_basic_set_n_dim(bset1);
832 hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
833 1 + dim + bset1->n_eq + bset2->n_eq,
834 2 + bset1->n_ineq + bset2->n_ineq);
837 for (i = 0; i < 2; ++i) {
838 for (j = 0; j < bset[i]->n_eq; ++j) {
839 k = isl_basic_set_alloc_equality(hull);
842 isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
843 isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
844 isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
847 for (j = 0; j < bset[i]->n_ineq; ++j) {
848 k = isl_basic_set_alloc_inequality(hull);
851 isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
852 isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
853 isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
854 bset[i]->ineq[j], 1+dim);
856 k = isl_basic_set_alloc_inequality(hull);
859 isl_seq_clr(hull->ineq[k], 1+2+3*dim);
860 isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
862 for (j = 0; j < 1+dim; ++j) {
863 k = isl_basic_set_alloc_equality(hull);
866 isl_seq_clr(hull->eq[k], 1+2+3*dim);
867 isl_int_set_si(hull->eq[k][j], -1);
868 isl_int_set_si(hull->eq[k][1+dim+j], 1);
869 isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
871 hull = isl_basic_set_set_rational(hull);
872 hull = isl_basic_set_remove_dims(hull, isl_dim_set, dim, 2*(1+dim));
873 hull = isl_basic_set_remove_redundancies(hull);
874 isl_basic_set_free(bset1);
875 isl_basic_set_free(bset2);
878 isl_basic_set_free(bset1);
879 isl_basic_set_free(bset2);
880 isl_basic_set_free(hull);
884 /* Is the set bounded for each value of the parameters?
886 int isl_basic_set_is_bounded(__isl_keep isl_basic_set *bset)
893 if (isl_basic_set_fast_is_empty(bset))
896 tab = isl_tab_from_recession_cone(bset, 1);
897 bounded = isl_tab_cone_is_bounded(tab);
902 /* Is the image bounded for each value of the parameters and
903 * the domain variables?
905 int isl_basic_map_image_is_bounded(__isl_keep isl_basic_map *bmap)
907 unsigned nparam = isl_basic_map_dim(bmap, isl_dim_param);
908 unsigned n_in = isl_basic_map_dim(bmap, isl_dim_in);
911 bmap = isl_basic_map_copy(bmap);
912 bmap = isl_basic_map_cow(bmap);
913 bmap = isl_basic_map_move_dims(bmap, isl_dim_param, nparam,
914 isl_dim_in, 0, n_in);
915 bounded = isl_basic_set_is_bounded((isl_basic_set *)bmap);
916 isl_basic_map_free(bmap);
921 /* Is the set bounded for each value of the parameters?
923 int isl_set_is_bounded(__isl_keep isl_set *set)
930 for (i = 0; i < set->n; ++i) {
931 int bounded = isl_basic_set_is_bounded(set->p[i]);
932 if (!bounded || bounded < 0)
938 /* Compute the lineality space of the convex hull of bset1 and bset2.
940 * We first compute the intersection of the recession cone of bset1
941 * with the negative of the recession cone of bset2 and then compute
942 * the linear hull of the resulting cone.
944 static struct isl_basic_set *induced_lineality_space(
945 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
948 struct isl_basic_set *lin = NULL;
951 if (!bset1 || !bset2)
954 dim = isl_basic_set_total_dim(bset1);
955 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset1), 0,
956 bset1->n_eq + bset2->n_eq,
957 bset1->n_ineq + bset2->n_ineq);
958 lin = isl_basic_set_set_rational(lin);
961 for (i = 0; i < bset1->n_eq; ++i) {
962 k = isl_basic_set_alloc_equality(lin);
965 isl_int_set_si(lin->eq[k][0], 0);
966 isl_seq_cpy(lin->eq[k] + 1, bset1->eq[i] + 1, dim);
968 for (i = 0; i < bset1->n_ineq; ++i) {
969 k = isl_basic_set_alloc_inequality(lin);
972 isl_int_set_si(lin->ineq[k][0], 0);
973 isl_seq_cpy(lin->ineq[k] + 1, bset1->ineq[i] + 1, dim);
975 for (i = 0; i < bset2->n_eq; ++i) {
976 k = isl_basic_set_alloc_equality(lin);
979 isl_int_set_si(lin->eq[k][0], 0);
980 isl_seq_neg(lin->eq[k] + 1, bset2->eq[i] + 1, dim);
982 for (i = 0; i < bset2->n_ineq; ++i) {
983 k = isl_basic_set_alloc_inequality(lin);
986 isl_int_set_si(lin->ineq[k][0], 0);
987 isl_seq_neg(lin->ineq[k] + 1, bset2->ineq[i] + 1, dim);
990 isl_basic_set_free(bset1);
991 isl_basic_set_free(bset2);
992 return isl_basic_set_affine_hull(lin);
994 isl_basic_set_free(lin);
995 isl_basic_set_free(bset1);
996 isl_basic_set_free(bset2);
1000 static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
1002 /* Given a set and a linear space "lin" of dimension n > 0,
1003 * project the linear space from the set, compute the convex hull
1004 * and then map the set back to the original space.
1010 * describe the linear space. We first compute the Hermite normal
1011 * form H = M U of M = H Q, to obtain
1015 * The last n rows of H will be zero, so the last n variables of x' = Q x
1016 * are the one we want to project out. We do this by transforming each
1017 * basic set A x >= b to A U x' >= b and then removing the last n dimensions.
1018 * After computing the convex hull in x'_1, i.e., A' x'_1 >= b',
1019 * we transform the hull back to the original space as A' Q_1 x >= b',
1020 * with Q_1 all but the last n rows of Q.
1022 static struct isl_basic_set *modulo_lineality(struct isl_set *set,
1023 struct isl_basic_set *lin)
1025 unsigned total = isl_basic_set_total_dim(lin);
1027 struct isl_basic_set *hull;
1028 struct isl_mat *M, *U, *Q;
1032 lin_dim = total - lin->n_eq;
1033 M = isl_mat_sub_alloc(set->ctx, lin->eq, 0, lin->n_eq, 1, total);
1034 M = isl_mat_left_hermite(M, 0, &U, &Q);
1038 isl_basic_set_free(lin);
1040 Q = isl_mat_drop_rows(Q, Q->n_row - lin_dim, lin_dim);
1042 U = isl_mat_lin_to_aff(U);
1043 Q = isl_mat_lin_to_aff(Q);
1045 set = isl_set_preimage(set, U);
1046 set = isl_set_remove_dims(set, isl_dim_set, total - lin_dim, lin_dim);
1047 hull = uset_convex_hull(set);
1048 hull = isl_basic_set_preimage(hull, Q);
1052 isl_basic_set_free(lin);
1057 /* Given two polyhedra with as constraints h_{ij} x >= 0 in homegeneous space,
1058 * set up an LP for solving
1060 * \sum_j \alpha_{1j} h_{1j} = \sum_j \alpha_{2j} h_{2j}
1062 * \alpha{i0} corresponds to the (implicit) positivity constraint 1 >= 0
1063 * The next \alpha{ij} correspond to the equalities and come in pairs.
1064 * The final \alpha{ij} correspond to the inequalities.
1066 static struct isl_basic_set *valid_direction_lp(
1067 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1069 struct isl_dim *dim;
1070 struct isl_basic_set *lp;
1075 if (!bset1 || !bset2)
1077 d = 1 + isl_basic_set_total_dim(bset1);
1079 2 * bset1->n_eq + bset1->n_ineq + 2 * bset2->n_eq + bset2->n_ineq;
1080 dim = isl_dim_set_alloc(bset1->ctx, 0, n);
1081 lp = isl_basic_set_alloc_dim(dim, 0, d, n);
1084 for (i = 0; i < n; ++i) {
1085 k = isl_basic_set_alloc_inequality(lp);
1088 isl_seq_clr(lp->ineq[k] + 1, n);
1089 isl_int_set_si(lp->ineq[k][0], -1);
1090 isl_int_set_si(lp->ineq[k][1 + i], 1);
1092 for (i = 0; i < d; ++i) {
1093 k = isl_basic_set_alloc_equality(lp);
1097 isl_int_set_si(lp->eq[k][n], 0); n++;
1098 /* positivity constraint 1 >= 0 */
1099 isl_int_set_si(lp->eq[k][n], i == 0); n++;
1100 for (j = 0; j < bset1->n_eq; ++j) {
1101 isl_int_set(lp->eq[k][n], bset1->eq[j][i]); n++;
1102 isl_int_neg(lp->eq[k][n], bset1->eq[j][i]); n++;
1104 for (j = 0; j < bset1->n_ineq; ++j) {
1105 isl_int_set(lp->eq[k][n], bset1->ineq[j][i]); n++;
1107 /* positivity constraint 1 >= 0 */
1108 isl_int_set_si(lp->eq[k][n], -(i == 0)); n++;
1109 for (j = 0; j < bset2->n_eq; ++j) {
1110 isl_int_neg(lp->eq[k][n], bset2->eq[j][i]); n++;
1111 isl_int_set(lp->eq[k][n], bset2->eq[j][i]); n++;
1113 for (j = 0; j < bset2->n_ineq; ++j) {
1114 isl_int_neg(lp->eq[k][n], bset2->ineq[j][i]); n++;
1117 lp = isl_basic_set_gauss(lp, NULL);
1118 isl_basic_set_free(bset1);
1119 isl_basic_set_free(bset2);
1122 isl_basic_set_free(bset1);
1123 isl_basic_set_free(bset2);
1127 /* Compute a vector s in the homogeneous space such that <s, r> > 0
1128 * for all rays in the homogeneous space of the two cones that correspond
1129 * to the input polyhedra bset1 and bset2.
1131 * We compute s as a vector that satisfies
1133 * s = \sum_j \alpha_{ij} h_{ij} for i = 1,2 (*)
1135 * with h_{ij} the normals of the facets of polyhedron i
1136 * (including the "positivity constraint" 1 >= 0) and \alpha_{ij}
1137 * strictly positive numbers. For simplicity we impose \alpha_{ij} >= 1.
1138 * We first set up an LP with as variables the \alpha{ij}.
1139 * In this formulation, for each polyhedron i,
1140 * the first constraint is the positivity constraint, followed by pairs
1141 * of variables for the equalities, followed by variables for the inequalities.
1142 * We then simply pick a feasible solution and compute s using (*).
1144 * Note that we simply pick any valid direction and make no attempt
1145 * to pick a "good" or even the "best" valid direction.
1147 static struct isl_vec *valid_direction(
1148 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1150 struct isl_basic_set *lp;
1151 struct isl_tab *tab;
1152 struct isl_vec *sample = NULL;
1153 struct isl_vec *dir;
1158 if (!bset1 || !bset2)
1160 lp = valid_direction_lp(isl_basic_set_copy(bset1),
1161 isl_basic_set_copy(bset2));
1162 tab = isl_tab_from_basic_set(lp);
1163 sample = isl_tab_get_sample_value(tab);
1165 isl_basic_set_free(lp);
1168 d = isl_basic_set_total_dim(bset1);
1169 dir = isl_vec_alloc(bset1->ctx, 1 + d);
1172 isl_seq_clr(dir->block.data + 1, dir->size - 1);
1174 /* positivity constraint 1 >= 0 */
1175 isl_int_set(dir->block.data[0], sample->block.data[n]); n++;
1176 for (i = 0; i < bset1->n_eq; ++i) {
1177 isl_int_sub(sample->block.data[n],
1178 sample->block.data[n], sample->block.data[n+1]);
1179 isl_seq_combine(dir->block.data,
1180 bset1->ctx->one, dir->block.data,
1181 sample->block.data[n], bset1->eq[i], 1 + d);
1185 for (i = 0; i < bset1->n_ineq; ++i)
1186 isl_seq_combine(dir->block.data,
1187 bset1->ctx->one, dir->block.data,
1188 sample->block.data[n++], bset1->ineq[i], 1 + d);
1189 isl_vec_free(sample);
1190 isl_seq_normalize(bset1->ctx, dir->el, dir->size);
1191 isl_basic_set_free(bset1);
1192 isl_basic_set_free(bset2);
1195 isl_vec_free(sample);
1196 isl_basic_set_free(bset1);
1197 isl_basic_set_free(bset2);
1201 /* Given a polyhedron b_i + A_i x >= 0 and a map T = S^{-1},
1202 * compute b_i' + A_i' x' >= 0, with
1204 * [ b_i A_i ] [ y' ] [ y' ]
1205 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1207 * In particular, add the "positivity constraint" and then perform
1210 static struct isl_basic_set *homogeneous_map(struct isl_basic_set *bset,
1217 bset = isl_basic_set_extend_constraints(bset, 0, 1);
1218 k = isl_basic_set_alloc_inequality(bset);
1221 isl_seq_clr(bset->ineq[k] + 1, isl_basic_set_total_dim(bset));
1222 isl_int_set_si(bset->ineq[k][0], 1);
1223 bset = isl_basic_set_preimage(bset, T);
1227 isl_basic_set_free(bset);
1231 /* Compute the convex hull of a pair of basic sets without any parameters or
1232 * integer divisions, where the convex hull is known to be pointed,
1233 * but the basic sets may be unbounded.
1235 * We turn this problem into the computation of a convex hull of a pair
1236 * _bounded_ polyhedra by "changing the direction of the homogeneous
1237 * dimension". This idea is due to Matthias Koeppe.
1239 * Consider the cones in homogeneous space that correspond to the
1240 * input polyhedra. The rays of these cones are also rays of the
1241 * polyhedra if the coordinate that corresponds to the homogeneous
1242 * dimension is zero. That is, if the inner product of the rays
1243 * with the homogeneous direction is zero.
1244 * The cones in the homogeneous space can also be considered to
1245 * correspond to other pairs of polyhedra by chosing a different
1246 * homogeneous direction. To ensure that both of these polyhedra
1247 * are bounded, we need to make sure that all rays of the cones
1248 * correspond to vertices and not to rays.
1249 * Let s be a direction such that <s, r> > 0 for all rays r of both cones.
1250 * Then using s as a homogeneous direction, we obtain a pair of polytopes.
1251 * The vector s is computed in valid_direction.
1253 * Note that we need to consider _all_ rays of the cones and not just
1254 * the rays that correspond to rays in the polyhedra. If we were to
1255 * only consider those rays and turn them into vertices, then we
1256 * may inadvertently turn some vertices into rays.
1258 * The standard homogeneous direction is the unit vector in the 0th coordinate.
1259 * We therefore transform the two polyhedra such that the selected
1260 * direction is mapped onto this standard direction and then proceed
1261 * with the normal computation.
1262 * Let S be a non-singular square matrix with s as its first row,
1263 * then we want to map the polyhedra to the space
1265 * [ y' ] [ y ] [ y ] [ y' ]
1266 * [ x' ] = S [ x ] i.e., [ x ] = S^{-1} [ x' ]
1268 * We take S to be the unimodular completion of s to limit the growth
1269 * of the coefficients in the following computations.
1271 * Let b_i + A_i x >= 0 be the constraints of polyhedron i.
1272 * We first move to the homogeneous dimension
1274 * b_i y + A_i x >= 0 [ b_i A_i ] [ y ] [ 0 ]
1275 * y >= 0 or [ 1 0 ] [ x ] >= [ 0 ]
1277 * Then we change directoin
1279 * [ b_i A_i ] [ y' ] [ y' ]
1280 * [ 1 0 ] S^{-1} [ x' ] >= 0 or [ b_i' A_i' ] [ x' ] >= 0
1282 * Then we compute the convex hull of the polytopes b_i' + A_i' x' >= 0
1283 * resulting in b' + A' x' >= 0, which we then convert back
1286 * [ b' A' ] S [ x ] >= 0 or [ b A ] [ x ] >= 0
1288 * The polyhedron b + A x >= 0 is then the convex hull of the input polyhedra.
1290 static struct isl_basic_set *convex_hull_pair_pointed(
1291 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1293 struct isl_ctx *ctx = NULL;
1294 struct isl_vec *dir = NULL;
1295 struct isl_mat *T = NULL;
1296 struct isl_mat *T2 = NULL;
1297 struct isl_basic_set *hull;
1298 struct isl_set *set;
1300 if (!bset1 || !bset2)
1303 dir = valid_direction(isl_basic_set_copy(bset1),
1304 isl_basic_set_copy(bset2));
1307 T = isl_mat_alloc(bset1->ctx, dir->size, dir->size);
1310 isl_seq_cpy(T->row[0], dir->block.data, dir->size);
1311 T = isl_mat_unimodular_complete(T, 1);
1312 T2 = isl_mat_right_inverse(isl_mat_copy(T));
1314 bset1 = homogeneous_map(bset1, isl_mat_copy(T2));
1315 bset2 = homogeneous_map(bset2, T2);
1316 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1317 set = isl_set_add_basic_set(set, bset1);
1318 set = isl_set_add_basic_set(set, bset2);
1319 hull = uset_convex_hull(set);
1320 hull = isl_basic_set_preimage(hull, T);
1327 isl_basic_set_free(bset1);
1328 isl_basic_set_free(bset2);
1332 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
1333 static struct isl_basic_set *modulo_affine_hull(
1334 struct isl_set *set, struct isl_basic_set *affine_hull);
1336 /* Compute the convex hull of a pair of basic sets without any parameters or
1337 * integer divisions.
1339 * This function is called from uset_convex_hull_unbounded, which
1340 * means that the complete convex hull is unbounded. Some pairs
1341 * of basic sets may still be bounded, though.
1342 * They may even lie inside a lower dimensional space, in which
1343 * case they need to be handled inside their affine hull since
1344 * the main algorithm assumes that the result is full-dimensional.
1346 * If the convex hull of the two basic sets would have a non-trivial
1347 * lineality space, we first project out this lineality space.
1349 static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
1350 struct isl_basic_set *bset2)
1352 isl_basic_set *lin, *aff;
1353 int bounded1, bounded2;
1355 if (bset1->ctx->opt->convex == ISL_CONVEX_HULL_FM)
1356 return convex_hull_pair_elim(bset1, bset2);
1358 aff = isl_set_affine_hull(isl_basic_set_union(isl_basic_set_copy(bset1),
1359 isl_basic_set_copy(bset2)));
1363 return modulo_affine_hull(isl_basic_set_union(bset1, bset2), aff);
1364 isl_basic_set_free(aff);
1366 bounded1 = isl_basic_set_is_bounded(bset1);
1367 bounded2 = isl_basic_set_is_bounded(bset2);
1369 if (bounded1 < 0 || bounded2 < 0)
1372 if (bounded1 && bounded2)
1373 uset_convex_hull_wrap(isl_basic_set_union(bset1, bset2));
1375 if (bounded1 || bounded2)
1376 return convex_hull_pair_pointed(bset1, bset2);
1378 lin = induced_lineality_space(isl_basic_set_copy(bset1),
1379 isl_basic_set_copy(bset2));
1382 if (isl_basic_set_is_universe(lin)) {
1383 isl_basic_set_free(bset1);
1384 isl_basic_set_free(bset2);
1387 if (lin->n_eq < isl_basic_set_total_dim(lin)) {
1388 struct isl_set *set;
1389 set = isl_set_alloc_dim(isl_basic_set_get_dim(bset1), 2, 0);
1390 set = isl_set_add_basic_set(set, bset1);
1391 set = isl_set_add_basic_set(set, bset2);
1392 return modulo_lineality(set, lin);
1394 isl_basic_set_free(lin);
1396 return convex_hull_pair_pointed(bset1, bset2);
1398 isl_basic_set_free(bset1);
1399 isl_basic_set_free(bset2);
1403 /* Compute the lineality space of a basic set.
1404 * We currently do not allow the basic set to have any divs.
1405 * We basically just drop the constants and turn every inequality
1408 struct isl_basic_set *isl_basic_set_lineality_space(struct isl_basic_set *bset)
1411 struct isl_basic_set *lin = NULL;
1416 isl_assert(bset->ctx, bset->n_div == 0, goto error);
1417 dim = isl_basic_set_total_dim(bset);
1419 lin = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset), 0, dim, 0);
1422 for (i = 0; i < bset->n_eq; ++i) {
1423 k = isl_basic_set_alloc_equality(lin);
1426 isl_int_set_si(lin->eq[k][0], 0);
1427 isl_seq_cpy(lin->eq[k] + 1, bset->eq[i] + 1, dim);
1429 lin = isl_basic_set_gauss(lin, NULL);
1432 for (i = 0; i < bset->n_ineq && lin->n_eq < dim; ++i) {
1433 k = isl_basic_set_alloc_equality(lin);
1436 isl_int_set_si(lin->eq[k][0], 0);
1437 isl_seq_cpy(lin->eq[k] + 1, bset->ineq[i] + 1, dim);
1438 lin = isl_basic_set_gauss(lin, NULL);
1442 isl_basic_set_free(bset);
1445 isl_basic_set_free(lin);
1446 isl_basic_set_free(bset);
1450 /* Compute the (linear) hull of the lineality spaces of the basic sets in the
1451 * "underlying" set "set".
1453 static struct isl_basic_set *uset_combined_lineality_space(struct isl_set *set)
1456 struct isl_set *lin = NULL;
1461 struct isl_dim *dim = isl_set_get_dim(set);
1463 return isl_basic_set_empty(dim);
1466 lin = isl_set_alloc_dim(isl_set_get_dim(set), set->n, 0);
1467 for (i = 0; i < set->n; ++i)
1468 lin = isl_set_add_basic_set(lin,
1469 isl_basic_set_lineality_space(isl_basic_set_copy(set->p[i])));
1471 return isl_set_affine_hull(lin);
1474 /* Compute the convex hull of a set without any parameters or
1475 * integer divisions.
1476 * In each step, we combined two basic sets until only one
1477 * basic set is left.
1478 * The input basic sets are assumed not to have a non-trivial
1479 * lineality space. If any of the intermediate results has
1480 * a non-trivial lineality space, it is projected out.
1482 static struct isl_basic_set *uset_convex_hull_unbounded(struct isl_set *set)
1484 struct isl_basic_set *convex_hull = NULL;
1486 convex_hull = isl_set_copy_basic_set(set);
1487 set = isl_set_drop_basic_set(set, convex_hull);
1490 while (set->n > 0) {
1491 struct isl_basic_set *t;
1492 t = isl_set_copy_basic_set(set);
1495 set = isl_set_drop_basic_set(set, t);
1498 convex_hull = convex_hull_pair(convex_hull, t);
1501 t = isl_basic_set_lineality_space(isl_basic_set_copy(convex_hull));
1504 if (isl_basic_set_is_universe(t)) {
1505 isl_basic_set_free(convex_hull);
1509 if (t->n_eq < isl_basic_set_total_dim(t)) {
1510 set = isl_set_add_basic_set(set, convex_hull);
1511 return modulo_lineality(set, t);
1513 isl_basic_set_free(t);
1519 isl_basic_set_free(convex_hull);
1523 /* Compute an initial hull for wrapping containing a single initial
1525 * This function assumes that the given set is bounded.
1527 static struct isl_basic_set *initial_hull(struct isl_basic_set *hull,
1528 struct isl_set *set)
1530 struct isl_mat *bounds = NULL;
1536 bounds = initial_facet_constraint(set);
1539 k = isl_basic_set_alloc_inequality(hull);
1542 dim = isl_set_n_dim(set);
1543 isl_assert(set->ctx, 1 + dim == bounds->n_col, goto error);
1544 isl_seq_cpy(hull->ineq[k], bounds->row[0], bounds->n_col);
1545 isl_mat_free(bounds);
1549 isl_basic_set_free(hull);
1550 isl_mat_free(bounds);
1554 struct max_constraint {
1560 static int max_constraint_equal(const void *entry, const void *val)
1562 struct max_constraint *a = (struct max_constraint *)entry;
1563 isl_int *b = (isl_int *)val;
1565 return isl_seq_eq(a->c->row[0] + 1, b, a->c->n_col - 1);
1568 static void update_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1569 isl_int *con, unsigned len, int n, int ineq)
1571 struct isl_hash_table_entry *entry;
1572 struct max_constraint *c;
1575 c_hash = isl_seq_get_hash(con + 1, len);
1576 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1582 isl_hash_table_remove(ctx, table, entry);
1586 if (isl_int_gt(c->c->row[0][0], con[0]))
1588 if (isl_int_eq(c->c->row[0][0], con[0])) {
1593 c->c = isl_mat_cow(c->c);
1594 isl_int_set(c->c->row[0][0], con[0]);
1598 /* Check whether the constraint hash table "table" constains the constraint
1601 static int has_constraint(struct isl_ctx *ctx, struct isl_hash_table *table,
1602 isl_int *con, unsigned len, int n)
1604 struct isl_hash_table_entry *entry;
1605 struct max_constraint *c;
1608 c_hash = isl_seq_get_hash(con + 1, len);
1609 entry = isl_hash_table_find(ctx, table, c_hash, max_constraint_equal,
1616 return isl_int_eq(c->c->row[0][0], con[0]);
1619 /* Check for inequality constraints of a basic set without equalities
1620 * such that the same or more stringent copies of the constraint appear
1621 * in all of the basic sets. Such constraints are necessarily facet
1622 * constraints of the convex hull.
1624 * If the resulting basic set is by chance identical to one of
1625 * the basic sets in "set", then we know that this basic set contains
1626 * all other basic sets and is therefore the convex hull of set.
1627 * In this case we set *is_hull to 1.
1629 static struct isl_basic_set *common_constraints(struct isl_basic_set *hull,
1630 struct isl_set *set, int *is_hull)
1633 int min_constraints;
1635 struct max_constraint *constraints = NULL;
1636 struct isl_hash_table *table = NULL;
1641 for (i = 0; i < set->n; ++i)
1642 if (set->p[i]->n_eq == 0)
1646 min_constraints = set->p[i]->n_ineq;
1648 for (i = best + 1; i < set->n; ++i) {
1649 if (set->p[i]->n_eq != 0)
1651 if (set->p[i]->n_ineq >= min_constraints)
1653 min_constraints = set->p[i]->n_ineq;
1656 constraints = isl_calloc_array(hull->ctx, struct max_constraint,
1660 table = isl_alloc_type(hull->ctx, struct isl_hash_table);
1661 if (isl_hash_table_init(hull->ctx, table, min_constraints))
1664 total = isl_dim_total(set->dim);
1665 for (i = 0; i < set->p[best]->n_ineq; ++i) {
1666 constraints[i].c = isl_mat_sub_alloc(hull->ctx,
1667 set->p[best]->ineq + i, 0, 1, 0, 1 + total);
1668 if (!constraints[i].c)
1670 constraints[i].ineq = 1;
1672 for (i = 0; i < min_constraints; ++i) {
1673 struct isl_hash_table_entry *entry;
1675 c_hash = isl_seq_get_hash(constraints[i].c->row[0] + 1, total);
1676 entry = isl_hash_table_find(hull->ctx, table, c_hash,
1677 max_constraint_equal, constraints[i].c->row[0] + 1, 1);
1680 isl_assert(hull->ctx, !entry->data, goto error);
1681 entry->data = &constraints[i];
1685 for (s = 0; s < set->n; ++s) {
1689 for (i = 0; i < set->p[s]->n_eq; ++i) {
1690 isl_int *eq = set->p[s]->eq[i];
1691 for (j = 0; j < 2; ++j) {
1692 isl_seq_neg(eq, eq, 1 + total);
1693 update_constraint(hull->ctx, table,
1697 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1698 isl_int *ineq = set->p[s]->ineq[i];
1699 update_constraint(hull->ctx, table, ineq, total, n,
1700 set->p[s]->n_eq == 0);
1705 for (i = 0; i < min_constraints; ++i) {
1706 if (constraints[i].count < n)
1708 if (!constraints[i].ineq)
1710 j = isl_basic_set_alloc_inequality(hull);
1713 isl_seq_cpy(hull->ineq[j], constraints[i].c->row[0], 1 + total);
1716 for (s = 0; s < set->n; ++s) {
1717 if (set->p[s]->n_eq)
1719 if (set->p[s]->n_ineq != hull->n_ineq)
1721 for (i = 0; i < set->p[s]->n_ineq; ++i) {
1722 isl_int *ineq = set->p[s]->ineq[i];
1723 if (!has_constraint(hull->ctx, table, ineq, total, n))
1726 if (i == set->p[s]->n_ineq)
1730 isl_hash_table_clear(table);
1731 for (i = 0; i < min_constraints; ++i)
1732 isl_mat_free(constraints[i].c);
1737 isl_hash_table_clear(table);
1740 for (i = 0; i < min_constraints; ++i)
1741 isl_mat_free(constraints[i].c);
1746 /* Create a template for the convex hull of "set" and fill it up
1747 * obvious facet constraints, if any. If the result happens to
1748 * be the convex hull of "set" then *is_hull is set to 1.
1750 static struct isl_basic_set *proto_hull(struct isl_set *set, int *is_hull)
1752 struct isl_basic_set *hull;
1757 for (i = 0; i < set->n; ++i) {
1758 n_ineq += set->p[i]->n_eq;
1759 n_ineq += set->p[i]->n_ineq;
1761 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
1762 hull = isl_basic_set_set_rational(hull);
1765 return common_constraints(hull, set, is_hull);
1768 static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
1770 struct isl_basic_set *hull;
1773 hull = proto_hull(set, &is_hull);
1774 if (hull && !is_hull) {
1775 if (hull->n_ineq == 0)
1776 hull = initial_hull(hull, set);
1777 hull = extend(hull, set);
1784 /* Compute the convex hull of a set without any parameters or
1785 * integer divisions. Depending on whether the set is bounded,
1786 * we pass control to the wrapping based convex hull or
1787 * the Fourier-Motzkin elimination based convex hull.
1788 * We also handle a few special cases before checking the boundedness.
1790 static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
1792 struct isl_basic_set *convex_hull = NULL;
1793 struct isl_basic_set *lin;
1795 if (isl_set_n_dim(set) == 0)
1796 return convex_hull_0d(set);
1798 set = isl_set_coalesce(set);
1799 set = isl_set_set_rational(set);
1806 convex_hull = isl_basic_set_copy(set->p[0]);
1810 if (isl_set_n_dim(set) == 1)
1811 return convex_hull_1d(set);
1813 if (isl_set_is_bounded(set) &&
1814 set->ctx->opt->convex == ISL_CONVEX_HULL_WRAP)
1815 return uset_convex_hull_wrap(set);
1817 lin = uset_combined_lineality_space(isl_set_copy(set));
1820 if (isl_basic_set_is_universe(lin)) {
1824 if (lin->n_eq < isl_basic_set_total_dim(lin))
1825 return modulo_lineality(set, lin);
1826 isl_basic_set_free(lin);
1828 return uset_convex_hull_unbounded(set);
1831 isl_basic_set_free(convex_hull);
1835 /* This is the core procedure, where "set" is a "pure" set, i.e.,
1836 * without parameters or divs and where the convex hull of set is
1837 * known to be full-dimensional.
1839 static struct isl_basic_set *uset_convex_hull_wrap_bounded(struct isl_set *set)
1841 struct isl_basic_set *convex_hull = NULL;
1846 if (isl_set_n_dim(set) == 0) {
1847 convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
1849 convex_hull = isl_basic_set_set_rational(convex_hull);
1853 set = isl_set_set_rational(set);
1854 set = isl_set_coalesce(set);
1858 convex_hull = isl_basic_set_copy(set->p[0]);
1862 if (isl_set_n_dim(set) == 1)
1863 return convex_hull_1d(set);
1865 return uset_convex_hull_wrap(set);
1871 /* Compute the convex hull of set "set" with affine hull "affine_hull",
1872 * We first remove the equalities (transforming the set), compute the
1873 * convex hull of the transformed set and then add the equalities back
1874 * (after performing the inverse transformation.
1876 static struct isl_basic_set *modulo_affine_hull(
1877 struct isl_set *set, struct isl_basic_set *affine_hull)
1881 struct isl_basic_set *dummy;
1882 struct isl_basic_set *convex_hull;
1884 dummy = isl_basic_set_remove_equalities(
1885 isl_basic_set_copy(affine_hull), &T, &T2);
1888 isl_basic_set_free(dummy);
1889 set = isl_set_preimage(set, T);
1890 convex_hull = uset_convex_hull(set);
1891 convex_hull = isl_basic_set_preimage(convex_hull, T2);
1892 convex_hull = isl_basic_set_intersect(convex_hull, affine_hull);
1895 isl_basic_set_free(affine_hull);
1900 /* Compute the convex hull of a map.
1902 * The implementation was inspired by "Extended Convex Hull" by Fukuda et al.,
1903 * specifically, the wrapping of facets to obtain new facets.
1905 struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
1907 struct isl_basic_set *bset;
1908 struct isl_basic_map *model = NULL;
1909 struct isl_basic_set *affine_hull = NULL;
1910 struct isl_basic_map *convex_hull = NULL;
1911 struct isl_set *set = NULL;
1912 struct isl_ctx *ctx;
1919 convex_hull = isl_basic_map_empty_like_map(map);
1924 map = isl_map_detect_equalities(map);
1925 map = isl_map_align_divs(map);
1928 model = isl_basic_map_copy(map->p[0]);
1929 set = isl_map_underlying_set(map);
1933 affine_hull = isl_set_affine_hull(isl_set_copy(set));
1936 if (affine_hull->n_eq != 0)
1937 bset = modulo_affine_hull(set, affine_hull);
1939 isl_basic_set_free(affine_hull);
1940 bset = uset_convex_hull(set);
1943 convex_hull = isl_basic_map_overlying_set(bset, model);
1947 ISL_F_SET(convex_hull, ISL_BASIC_MAP_NO_IMPLICIT);
1948 ISL_F_SET(convex_hull, ISL_BASIC_MAP_ALL_EQUALITIES);
1949 ISL_F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
1953 isl_basic_map_free(model);
1957 struct isl_basic_set *isl_set_convex_hull(struct isl_set *set)
1959 return (struct isl_basic_set *)
1960 isl_map_convex_hull((struct isl_map *)set);
1963 __isl_give isl_basic_map *isl_map_polyhedral_hull(__isl_take isl_map *map)
1965 isl_basic_map *hull;
1967 hull = isl_map_convex_hull(map);
1968 return isl_basic_map_remove_divs(hull);
1971 __isl_give isl_basic_set *isl_set_polyhedral_hull(__isl_take isl_set *set)
1973 return (isl_basic_set *)isl_map_polyhedral_hull((isl_map *)set);
1976 struct sh_data_entry {
1977 struct isl_hash_table *table;
1978 struct isl_tab *tab;
1981 /* Holds the data needed during the simple hull computation.
1983 * n the number of basic sets in the original set
1984 * hull_table a hash table of already computed constraints
1985 * in the simple hull
1986 * p for each basic set,
1987 * table a hash table of the constraints
1988 * tab the tableau corresponding to the basic set
1991 struct isl_ctx *ctx;
1993 struct isl_hash_table *hull_table;
1994 struct sh_data_entry p[1];
1997 static void sh_data_free(struct sh_data *data)
2003 isl_hash_table_free(data->ctx, data->hull_table);
2004 for (i = 0; i < data->n; ++i) {
2005 isl_hash_table_free(data->ctx, data->p[i].table);
2006 isl_tab_free(data->p[i].tab);
2011 struct ineq_cmp_data {
2016 static int has_ineq(const void *entry, const void *val)
2018 isl_int *row = (isl_int *)entry;
2019 struct ineq_cmp_data *v = (struct ineq_cmp_data *)val;
2021 return isl_seq_eq(row + 1, v->p + 1, v->len) ||
2022 isl_seq_is_neg(row + 1, v->p + 1, v->len);
2025 static int hash_ineq(struct isl_ctx *ctx, struct isl_hash_table *table,
2026 isl_int *ineq, unsigned len)
2029 struct ineq_cmp_data v;
2030 struct isl_hash_table_entry *entry;
2034 c_hash = isl_seq_get_hash(ineq + 1, len);
2035 entry = isl_hash_table_find(ctx, table, c_hash, has_ineq, &v, 1);
2042 /* Fill hash table "table" with the constraints of "bset".
2043 * Equalities are added as two inequalities.
2044 * The value in the hash table is a pointer to the (in)equality of "bset".
2046 static int hash_basic_set(struct isl_hash_table *table,
2047 struct isl_basic_set *bset)
2050 unsigned dim = isl_basic_set_total_dim(bset);
2052 for (i = 0; i < bset->n_eq; ++i) {
2053 for (j = 0; j < 2; ++j) {
2054 isl_seq_neg(bset->eq[i], bset->eq[i], 1 + dim);
2055 if (hash_ineq(bset->ctx, table, bset->eq[i], dim) < 0)
2059 for (i = 0; i < bset->n_ineq; ++i) {
2060 if (hash_ineq(bset->ctx, table, bset->ineq[i], dim) < 0)
2066 static struct sh_data *sh_data_alloc(struct isl_set *set, unsigned n_ineq)
2068 struct sh_data *data;
2071 data = isl_calloc(set->ctx, struct sh_data,
2072 sizeof(struct sh_data) +
2073 (set->n - 1) * sizeof(struct sh_data_entry));
2076 data->ctx = set->ctx;
2078 data->hull_table = isl_hash_table_alloc(set->ctx, n_ineq);
2079 if (!data->hull_table)
2081 for (i = 0; i < set->n; ++i) {
2082 data->p[i].table = isl_hash_table_alloc(set->ctx,
2083 2 * set->p[i]->n_eq + set->p[i]->n_ineq);
2084 if (!data->p[i].table)
2086 if (hash_basic_set(data->p[i].table, set->p[i]) < 0)
2095 /* Check if inequality "ineq" is a bound for basic set "j" or if
2096 * it can be relaxed (by increasing the constant term) to become
2097 * a bound for that basic set. In the latter case, the constant
2099 * Return 1 if "ineq" is a bound
2100 * 0 if "ineq" may attain arbitrarily small values on basic set "j"
2101 * -1 if some error occurred
2103 static int is_bound(struct sh_data *data, struct isl_set *set, int j,
2106 enum isl_lp_result res;
2109 if (!data->p[j].tab) {
2110 data->p[j].tab = isl_tab_from_basic_set(set->p[j]);
2111 if (!data->p[j].tab)
2117 res = isl_tab_min(data->p[j].tab, ineq, data->ctx->one,
2119 if (res == isl_lp_ok && isl_int_is_neg(opt))
2120 isl_int_sub(ineq[0], ineq[0], opt);
2124 return (res == isl_lp_ok || res == isl_lp_empty) ? 1 :
2125 res == isl_lp_unbounded ? 0 : -1;
2128 /* Check if inequality "ineq" from basic set "i" can be relaxed to
2129 * become a bound on the whole set. If so, add the (relaxed) inequality
2132 * We first check if "hull" already contains a translate of the inequality.
2133 * If so, we are done.
2134 * Then, we check if any of the previous basic sets contains a translate
2135 * of the inequality. If so, then we have already considered this
2136 * inequality and we are done.
2137 * Otherwise, for each basic set other than "i", we check if the inequality
2138 * is a bound on the basic set.
2139 * For previous basic sets, we know that they do not contain a translate
2140 * of the inequality, so we directly call is_bound.
2141 * For following basic sets, we first check if a translate of the
2142 * inequality appears in its description and if so directly update
2143 * the inequality accordingly.
2145 static struct isl_basic_set *add_bound(struct isl_basic_set *hull,
2146 struct sh_data *data, struct isl_set *set, int i, isl_int *ineq)
2149 struct ineq_cmp_data v;
2150 struct isl_hash_table_entry *entry;
2156 v.len = isl_basic_set_total_dim(hull);
2158 c_hash = isl_seq_get_hash(ineq + 1, v.len);
2160 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2165 for (j = 0; j < i; ++j) {
2166 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2167 c_hash, has_ineq, &v, 0);
2174 k = isl_basic_set_alloc_inequality(hull);
2175 isl_seq_cpy(hull->ineq[k], ineq, 1 + v.len);
2179 for (j = 0; j < i; ++j) {
2181 bound = is_bound(data, set, j, hull->ineq[k]);
2188 isl_basic_set_free_inequality(hull, 1);
2192 for (j = i + 1; j < set->n; ++j) {
2195 entry = isl_hash_table_find(hull->ctx, data->p[j].table,
2196 c_hash, has_ineq, &v, 0);
2198 ineq_j = entry->data;
2199 neg = isl_seq_is_neg(ineq_j + 1,
2200 hull->ineq[k] + 1, v.len);
2202 isl_int_neg(ineq_j[0], ineq_j[0]);
2203 if (isl_int_gt(ineq_j[0], hull->ineq[k][0]))
2204 isl_int_set(hull->ineq[k][0], ineq_j[0]);
2206 isl_int_neg(ineq_j[0], ineq_j[0]);
2209 bound = is_bound(data, set, j, hull->ineq[k]);
2216 isl_basic_set_free_inequality(hull, 1);
2220 entry = isl_hash_table_find(hull->ctx, data->hull_table, c_hash,
2224 entry->data = hull->ineq[k];
2228 isl_basic_set_free(hull);
2232 /* Check if any inequality from basic set "i" can be relaxed to
2233 * become a bound on the whole set. If so, add the (relaxed) inequality
2236 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
2237 struct sh_data *data, struct isl_set *set, int i)
2240 unsigned dim = isl_basic_set_total_dim(bset);
2242 for (j = 0; j < set->p[i]->n_eq; ++j) {
2243 for (k = 0; k < 2; ++k) {
2244 isl_seq_neg(set->p[i]->eq[j], set->p[i]->eq[j], 1+dim);
2245 bset = add_bound(bset, data, set, i, set->p[i]->eq[j]);
2248 for (j = 0; j < set->p[i]->n_ineq; ++j)
2249 bset = add_bound(bset, data, set, i, set->p[i]->ineq[j]);
2253 /* Compute a superset of the convex hull of set that is described
2254 * by only translates of the constraints in the constituents of set.
2256 static struct isl_basic_set *uset_simple_hull(struct isl_set *set)
2258 struct sh_data *data = NULL;
2259 struct isl_basic_set *hull = NULL;
2267 for (i = 0; i < set->n; ++i) {
2270 n_ineq += 2 * set->p[i]->n_eq + set->p[i]->n_ineq;
2273 hull = isl_basic_set_alloc_dim(isl_dim_copy(set->dim), 0, 0, n_ineq);
2277 data = sh_data_alloc(set, n_ineq);
2281 for (i = 0; i < set->n; ++i)
2282 hull = add_bounds(hull, data, set, i);
2290 isl_basic_set_free(hull);
2295 /* Compute a superset of the convex hull of map that is described
2296 * by only translates of the constraints in the constituents of map.
2298 struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
2300 struct isl_set *set = NULL;
2301 struct isl_basic_map *model = NULL;
2302 struct isl_basic_map *hull;
2303 struct isl_basic_map *affine_hull;
2304 struct isl_basic_set *bset = NULL;
2309 hull = isl_basic_map_empty_like_map(map);
2314 hull = isl_basic_map_copy(map->p[0]);
2319 map = isl_map_detect_equalities(map);
2320 affine_hull = isl_map_affine_hull(isl_map_copy(map));
2321 map = isl_map_align_divs(map);
2322 model = isl_basic_map_copy(map->p[0]);
2324 set = isl_map_underlying_set(map);
2326 bset = uset_simple_hull(set);
2328 hull = isl_basic_map_overlying_set(bset, model);
2330 hull = isl_basic_map_intersect(hull, affine_hull);
2331 hull = isl_basic_map_remove_redundancies(hull);
2332 ISL_F_SET(hull, ISL_BASIC_MAP_NO_IMPLICIT);
2333 ISL_F_SET(hull, ISL_BASIC_MAP_ALL_EQUALITIES);
2338 struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
2340 return (struct isl_basic_set *)
2341 isl_map_simple_hull((struct isl_map *)set);
2344 /* Given a set "set", return parametric bounds on the dimension "dim".
2346 static struct isl_basic_set *set_bounds(struct isl_set *set, int dim)
2348 unsigned set_dim = isl_set_dim(set, isl_dim_set);
2349 set = isl_set_copy(set);
2350 set = isl_set_eliminate_dims(set, dim + 1, set_dim - (dim + 1));
2351 set = isl_set_eliminate_dims(set, 0, dim);
2352 return isl_set_convex_hull(set);
2355 /* Computes a "simple hull" and then check if each dimension in the
2356 * resulting hull is bounded by a symbolic constant. If not, the
2357 * hull is intersected with the corresponding bounds on the whole set.
2359 struct isl_basic_set *isl_set_bounded_simple_hull(struct isl_set *set)
2362 struct isl_basic_set *hull;
2363 unsigned nparam, left;
2364 int removed_divs = 0;
2366 hull = isl_set_simple_hull(isl_set_copy(set));
2370 nparam = isl_basic_set_dim(hull, isl_dim_param);
2371 for (i = 0; i < isl_basic_set_dim(hull, isl_dim_set); ++i) {
2372 int lower = 0, upper = 0;
2373 struct isl_basic_set *bounds;
2375 left = isl_basic_set_total_dim(hull) - nparam - i - 1;
2376 for (j = 0; j < hull->n_eq; ++j) {
2377 if (isl_int_is_zero(hull->eq[j][1 + nparam + i]))
2379 if (isl_seq_first_non_zero(hull->eq[j]+1+nparam+i+1,
2386 for (j = 0; j < hull->n_ineq; ++j) {
2387 if (isl_int_is_zero(hull->ineq[j][1 + nparam + i]))
2389 if (isl_seq_first_non_zero(hull->ineq[j]+1+nparam+i+1,
2391 isl_seq_first_non_zero(hull->ineq[j]+1+nparam,
2394 if (isl_int_is_pos(hull->ineq[j][1 + nparam + i]))
2405 if (!removed_divs) {
2406 set = isl_set_remove_divs(set);
2411 bounds = set_bounds(set, i);
2412 hull = isl_basic_set_intersect(hull, bounds);