#include "isl_seq.h"
#include "isl_equalities.h"
-static struct isl_basic_set *uset_convex_hull(struct isl_set *set);
+static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set);
-static swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
+static void swap_ineq(struct isl_basic_map *bmap, unsigned i, unsigned j)
{
isl_int *t;
}
}
+/* Return 1 if constraint c is redundant with respect to the constraints
+ * in bmap. If c is a lower [upper] bound in some variable and bmap
+ * does not have a lower [upper] bound in that variable, then c cannot
+ * be redundant and we do not need solve any lp.
+ */
+int isl_basic_map_constraint_is_redundant(struct isl_basic_map **bmap,
+ isl_int *c, isl_int *opt_n, isl_int *opt_d)
+{
+ enum isl_lp_result res;
+ unsigned total;
+ int i, j;
+
+ if (!bmap)
+ return -1;
+
+ total = isl_basic_map_total_dim(*bmap);
+ for (i = 0; i < total; ++i) {
+ int sign;
+ if (isl_int_is_zero(c[1+i]))
+ continue;
+ sign = isl_int_sgn(c[1+i]);
+ for (j = 0; j < (*bmap)->n_ineq; ++j)
+ if (sign == isl_int_sgn((*bmap)->ineq[j][1+i]))
+ break;
+ if (j == (*bmap)->n_ineq)
+ break;
+ }
+ if (i < total)
+ return 0;
+
+ res = isl_solve_lp(*bmap, 0, c+1, (*bmap)->ctx->one, opt_n, opt_d);
+ if (res == isl_lp_unbounded)
+ return 0;
+ if (res == isl_lp_error)
+ return -1;
+ if (res == isl_lp_empty) {
+ *bmap = isl_basic_map_set_to_empty(*bmap);
+ return 0;
+ }
+ if (opt_d)
+ isl_int_addmul(*opt_n, *opt_d, c[0]);
+ else
+ isl_int_add(*opt_n, *opt_n, c[0]);
+ return !isl_int_is_neg(*opt_n);
+}
+
+int isl_basic_set_constraint_is_redundant(struct isl_basic_set **bset,
+ isl_int *c, isl_int *opt_n, isl_int *opt_d)
+{
+ return isl_basic_map_constraint_is_redundant(
+ (struct isl_basic_map **)bset, c, opt_n, opt_d);
+}
+
/* Compute the convex hull of a basic map, by removing the redundant
* constraints. If the minimal value along the normal of a constraint
* is the same if the constraint is removed, then the constraint is redundant.
isl_int_init(opt_n);
isl_int_init(opt_d);
for (i = bmap->n_ineq-1; i >= 0; --i) {
- enum isl_lp_result res;
+ int redundant;
swap_ineq(bmap, i, bmap->n_ineq-1);
bmap->n_ineq--;
- res = isl_solve_lp(bmap, 0,
- bmap->ineq[bmap->n_ineq]+1, ctx->one, &opt_n, &opt_d);
- bmap->n_ineq++;
- swap_ineq(bmap, i, bmap->n_ineq-1);
- if (res == isl_lp_unbounded)
- continue;
- if (res == isl_lp_error)
+ redundant = isl_basic_map_constraint_is_redundant(&bmap,
+ bmap->ineq[bmap->n_ineq], &opt_n, &opt_d);
+ if (redundant == -1)
goto error;
- if (res == isl_lp_empty) {
- bmap = isl_basic_map_set_to_empty(bmap);
+ if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
break;
- }
- isl_int_addmul(opt_n, opt_d, bmap->ineq[i][0]);
- if (!isl_int_is_neg(opt_n))
+ bmap->n_ineq++;
+ swap_ineq(bmap, i, bmap->n_ineq-1);
+ if (redundant)
isl_basic_map_drop_inequality(bmap, i);
}
isl_int_clear(opt_n);
isl_basic_map_convex_hull((struct isl_basic_map *)bset);
}
-/* Check if "c" is a direction with a lower bound in "set" that is independent
- * of the previously found "n" bounds in "dirs".
- * If so, add it to the list, with the negative of the lower bound
- * in the constant position, i.e., such that c correspond to a bounding
- * hyperplane (but not necessarily a facet).
+/* Check if the set set is bound in the direction of the affine
+ * constraint c and if so, set the constant term such that the
+ * resulting constraint is a bounding constraint for the set.
*/
-static int is_independent_bound(struct isl_ctx *ctx,
- struct isl_set *set, isl_int *c,
- struct isl_mat *dirs, int n)
+static int uset_is_bound(struct isl_ctx *ctx, struct isl_set *set,
+ isl_int *c, unsigned len)
{
int first;
- int i = 0, j;
+ int j;
isl_int opt;
isl_int opt_denom;
- isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
- if (n != 0) {
- int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
- if (pos < 0)
- return 0;
- for (i = 0; i < n; ++i) {
- int pos_i;
- pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
- if (pos_i < pos)
- continue;
- if (pos_i > pos)
- break;
- isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
- dirs->n_col-1, NULL);
- pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
- if (pos < 0)
- return 0;
- }
- }
-
isl_int_init(opt);
isl_int_init(opt_denom);
first = 1;
for (j = 0; j < set->n; ++j) {
enum isl_lp_result res;
- if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
+ if (F_ISSET(set->p[j], ISL_BASIC_SET_EMPTY))
continue;
res = isl_solve_lp((struct isl_basic_map*)set->p[j],
- 0, dirs->row[n]+1, ctx->one, &opt, &opt_denom);
+ 0, c+1, ctx->one, &opt, &opt_denom);
if (res == isl_lp_unbounded)
break;
if (res == isl_lp_error)
continue;
}
if (!isl_int_is_one(opt_denom))
- isl_seq_scale(dirs->row[n], dirs->row[n], opt_denom,
- dirs->n_col);
- if (first || isl_int_lt(opt, dirs->row[n][0]))
- isl_int_set(dirs->row[n][0], opt);
+ isl_seq_scale(c, c, opt_denom, len);
+ if (first || isl_int_lt(opt, c[0]))
+ isl_int_set(c[0], opt);
first = 0;
}
isl_int_clear(opt);
isl_int_clear(opt_denom);
- if (j < set->n)
- return 0;
- isl_int_neg(dirs->row[n][0], dirs->row[n][0]);
+ isl_int_neg(c[0], c[0]);
+ return j >= set->n;
+error:
+ isl_int_clear(opt);
+ isl_int_clear(opt_denom);
+ return -1;
+}
+
+/* Check if "c" is a direction with both a lower bound and an upper
+ * bound in "set" that is independent of the previously found "n"
+ * bounds in "dirs".
+ * If so, add it to the list, with the negative of the lower bound
+ * in the constant position, i.e., such that c corresponds to a bounding
+ * hyperplane (but not necessarily a facet).
+ */
+static int is_independent_bound(struct isl_ctx *ctx,
+ struct isl_set *set, isl_int *c,
+ struct isl_mat *dirs, int n)
+{
+ int is_bound;
+ int i = 0;
+
+ isl_seq_cpy(dirs->row[n]+1, c+1, dirs->n_col-1);
+ if (n != 0) {
+ int pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
+ if (pos < 0)
+ return 0;
+ for (i = 0; i < n; ++i) {
+ int pos_i;
+ pos_i = isl_seq_first_non_zero(dirs->row[i]+1, dirs->n_col-1);
+ if (pos_i < pos)
+ continue;
+ if (pos_i > pos)
+ break;
+ isl_seq_elim(dirs->row[n]+1, dirs->row[i]+1, pos,
+ dirs->n_col-1, NULL);
+ pos = isl_seq_first_non_zero(dirs->row[n]+1, dirs->n_col-1);
+ if (pos < 0)
+ return 0;
+ }
+ }
+
+ isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
+ is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
+ isl_seq_neg(dirs->row[n] + 1, dirs->row[n] + 1, dirs->n_col - 1);
+ if (is_bound != 1)
+ return is_bound;
+ is_bound = uset_is_bound(ctx, set, dirs->row[n], dirs->n_col);
+ if (is_bound != 1)
+ return is_bound;
if (i < n) {
int k;
isl_int *t = dirs->row[n];
dirs->row[i] = t;
}
return 1;
-error:
- isl_int_clear(opt);
- isl_int_clear(opt_denom);
- return -1;
}
/* Compute and return a maximal set of linearly independent bounds
{
int i, j, n;
struct isl_mat *dirs = NULL;
+ unsigned dim = isl_set_n_dim(set);
- dirs = isl_mat_alloc(ctx, set->dim, 1+set->dim);
+ dirs = isl_mat_alloc(ctx, dim, 1+dim);
if (!dirs)
goto error;
n = 0;
- for (i = 0; n < set->dim && i < set->n; ++i) {
+ for (i = 0; n < dim && i < set->n; ++i) {
int f;
struct isl_basic_set *bset = set->p[i];
- for (j = 0; n < set->dim && j < bset->n_eq; ++j) {
+ for (j = 0; n < dim && j < bset->n_eq; ++j) {
f = is_independent_bound(ctx, set, bset->eq[j],
dirs, n);
if (f < 0)
goto error;
- if (f) {
- ++n;
- continue;
- }
- isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
- f = is_independent_bound(ctx, set, bset->eq[j],
- dirs, n);
- isl_seq_neg(bset->eq[j], bset->eq[j], 1+set->dim);
- if (f < 0)
- goto error;
if (f)
++n;
}
- for (j = 0; n < set->dim && j < bset->n_ineq; ++j) {
+ for (j = 0; n < dim && j < bset->n_ineq; ++j) {
f = is_independent_bound(ctx, set, bset->ineq[j],
dirs, n);
if (f < 0)
F_SET(bset, ISL_BASIC_MAP_RATIONAL);
- return bset;
+ return isl_basic_set_finalize(bset);
}
static struct isl_set *isl_set_set_rational(struct isl_set *set)
{
int i;
unsigned total;
+ unsigned dim;
+
+ if (F_ISSET(bset, ISL_BASIC_SET_EMPTY))
+ return bset;
- isl_assert(ctx, bset->nparam == 0, goto error);
+ isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
isl_assert(ctx, bset->n_div == 0, goto error);
- bset = isl_basic_set_extend(bset, 0, bset->dim, 0, 1, 0);
+ dim = isl_basic_set_n_dim(bset);
+ bset = isl_basic_set_extend(bset, 0, dim, 0, 1, 0);
i = isl_basic_set_alloc_equality(bset);
if (i < 0)
goto error;
- isl_seq_cpy(bset->eq[i], c, 1 + bset->dim);
+ isl_seq_cpy(bset->eq[i], c, 1 + dim);
return bset;
error:
isl_basic_set_free(bset);
unsigned n_eq;
unsigned n_ineq;
int i, j, k;
- unsigned dim;
+ unsigned dim, lp_dim;
if (!set)
return NULL;
- dim = 1 + set->dim;
+ dim = 1 + isl_set_n_dim(set);
n_eq = 1;
n_ineq = set->n;
for (i = 0; i < set->n; ++i) {
lp = isl_basic_set_alloc(ctx, 0, dim * set->n, 0, n_eq, n_ineq);
if (!lp)
return NULL;
+ lp_dim = isl_basic_set_n_dim(lp);
k = isl_basic_set_alloc_equality(lp);
isl_int_set_si(lp->eq[k][0], -1);
for (i = 0; i < set->n; ++i) {
}
for (i = 0; i < set->n; ++i) {
k = isl_basic_set_alloc_inequality(lp);
- isl_seq_clr(lp->ineq[k], 1+lp->dim);
+ isl_seq_clr(lp->ineq[k], 1+lp_dim);
isl_int_set_si(lp->ineq[k][1+dim*i], 1);
for (j = 0; j < set->p[i]->n_eq; ++j) {
*
* x_1 >= 0
*
- * I.e., the facet is
+ * I.e., the facet lies in
*
* x_1 = 0
*
* A_i [ x_i ] >= 0
*
* the constraints of each (transformed) basic set.
- * If a = n/d, then the consstraint defining the new facet (in the transformed
+ * If a = n/d, then the constraint defining the new facet (in the transformed
* space) is
*
* -n x_1 + d x_2 >= 0
*
* In the original space, we need to take the same combination of the
* corresponding constraints "facet" and "ridge".
+ *
+ * If a = -infty = "-1/0", then we just return the original facet constraint.
+ * This means that the facet is unbounded, but has a bounded intersection
+ * with the union of sets.
*/
static isl_int *wrap_facet(struct isl_ctx *ctx, struct isl_set *set,
isl_int *facet, isl_int *ridge)
set = isl_set_copy(set);
- dim = 1 + set->dim;
- T = isl_mat_alloc(ctx, 3, 1 + set->dim);
+ dim = 1 + isl_set_n_dim(set);
+ T = isl_mat_alloc(ctx, 3, dim);
if (!T)
goto error;
isl_int_set_si(T->row[0][0], 1);
- isl_seq_clr(T->row[0]+1, set->dim);
- isl_seq_cpy(T->row[1], facet, 1+set->dim);
- isl_seq_cpy(T->row[2], ridge, 1+set->dim);
+ isl_seq_clr(T->row[0]+1, dim - 1);
+ isl_seq_cpy(T->row[1], facet, dim);
+ isl_seq_cpy(T->row[2], ridge, dim);
T = isl_mat_right_inverse(ctx, T);
set = isl_set_preimage(ctx, set, T);
T = NULL;
+ if (!set)
+ goto error;
lp = wrap_constraints(ctx, set);
obj = isl_vec_alloc(ctx, dim*set->n);
if (!obj)
isl_vec_free(ctx, obj);
isl_basic_set_free(lp);
isl_set_free(set);
- return (res == isl_lp_ok) ? facet : NULL;
+ isl_assert(ctx, res == isl_lp_ok || res == isl_lp_unbounded,
+ return NULL);
+ return facet;
error:
isl_basic_set_free(lp);
isl_mat_free(ctx, T);
return NULL;
}
-/* Given a direction of a constraint, compute the constant term
- * such that the resulting constraint is a bounding constraint
- * of the set "set" (which just happens to be a face of the
- * original set).
- */
-static int compute_bound_on_face(struct isl_ctx *ctx,
- struct isl_set *set, isl_int *c)
-{
- int first = 1;
- int j;
- isl_int opt;
- isl_int opt_denom;
-
- isl_int_init(opt);
- isl_int_init(opt_denom);
- for (j = 0; j < set->n; ++j) {
- enum isl_lp_result res;
-
- if (F_ISSET(set->p[j], ISL_BASIC_MAP_EMPTY))
- continue;
-
- res = isl_solve_lp((struct isl_basic_map*)set->p[j],
- 0, c+1, ctx->one, &opt, &opt_denom);
- if (res == isl_lp_unbounded)
- goto error;
- if (res == isl_lp_error)
- goto error;
- if (res == isl_lp_empty) {
- set->p[j] = isl_basic_set_set_to_empty(set->p[j]);
- if (!set->p[j])
- goto error;
- continue;
- }
- if (!isl_int_is_one(opt_denom))
- isl_seq_scale(c, c, opt_denom, 1+set->dim);
- if (first || isl_int_lt(opt, c[0]))
- isl_int_set(c[0], opt);
- first = 0;
- }
- isl_assert(ctx, !first, goto error);
- isl_int_clear(opt);
- isl_int_clear(opt_denom);
- isl_int_neg(c[0], c[0]);
- return 0;
-error:
- isl_int_clear(opt);
- isl_int_clear(opt_denom);
- return -1;
-}
-
/* Given a set of d linearly independent bounding constraints of the
* convex hull of "set", compute the constraint of a facet of "set".
*
* We first compute the intersection with the first bounding hyperplane
- * and shift the second bounding constraint to be a bounding constraint
- * of the resulting face. We then wrap around the next bounding constraint
+ * and remove the component corresponding to this hyperplane from
+ * other bounds (in homogeneous space).
+ * We then wrap around one of the remaining bounding constraints
* and continue the process until all bounding constraints have been
* taken into account.
* The resulting linear combination of the bounding constraints will
static struct isl_mat *initial_facet_constraint(struct isl_ctx *ctx,
struct isl_set *set, struct isl_mat *bounds)
{
- struct isl_set *face = NULL;
+ struct isl_set *slice = NULL;
+ struct isl_basic_set *face = NULL;
+ struct isl_mat *m, *U, *Q;
int i;
+ unsigned dim = isl_set_n_dim(set);
isl_assert(ctx, set->n > 0, goto error);
- isl_assert(ctx, bounds->n_row == set->dim, goto error);
+ isl_assert(ctx, bounds->n_row == dim, goto error);
- face = isl_set_copy(set);
- if (!face)
- goto error;
- for (i = 1; i < set->dim; ++i) {
- face = isl_set_add_equality(ctx, face, bounds->row[i-1]);
- if (compute_bound_on_face(ctx, face, bounds->row[i]) < 0)
+ while (bounds->n_row > 1) {
+ slice = isl_set_copy(set);
+ slice = isl_set_add_equality(ctx, slice, bounds->row[0]);
+ face = isl_set_affine_hull(slice);
+ if (!face)
+ goto error;
+ if (face->n_eq == 1) {
+ isl_basic_set_free(face);
+ break;
+ }
+ m = isl_mat_alloc(ctx, 1 + face->n_eq, 1 + dim);
+ if (!m)
goto error;
- if (!wrap_facet(ctx, set, bounds->row[0], bounds->row[i]))
+ isl_int_set_si(m->row[0][0], 1);
+ isl_seq_clr(m->row[0]+1, dim);
+ for (i = 0; i < face->n_eq; ++i)
+ isl_seq_cpy(m->row[1 + i], face->eq[i], 1 + dim);
+ U = isl_mat_right_inverse(ctx, m);
+ Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
+ U = isl_mat_drop_cols(ctx, U, 1 + face->n_eq,
+ dim - face->n_eq);
+ Q = isl_mat_drop_rows(ctx, Q, 1 + face->n_eq,
+ dim - face->n_eq);
+ U = isl_mat_drop_cols(ctx, U, 0, 1);
+ Q = isl_mat_drop_rows(ctx, Q, 0, 1);
+ bounds = isl_mat_product(ctx, bounds, U);
+ bounds = isl_mat_product(ctx, bounds, Q);
+ while (isl_seq_first_non_zero(bounds->row[bounds->n_row-1],
+ bounds->n_col) == -1) {
+ bounds->n_row--;
+ isl_assert(ctx, bounds->n_row > 1, goto error);
+ }
+ if (!wrap_facet(ctx, set, bounds->row[0],
+ bounds->row[bounds->n_row-1]))
goto error;
+ isl_basic_set_free(face);
+ bounds->n_row--;
}
- isl_set_free(face);
return bounds;
error:
- isl_set_free(face);
+ isl_basic_set_free(face);
isl_mat_free(ctx, bounds);
return NULL;
}
{
struct isl_mat *m, *U, *Q;
struct isl_basic_set *facet;
+ unsigned dim;
set = isl_set_copy(set);
- m = isl_mat_alloc(ctx, 2, 1 + set->dim);
+ dim = isl_set_n_dim(set);
+ m = isl_mat_alloc(ctx, 2, 1 + dim);
if (!m)
goto error;
isl_int_set_si(m->row[0][0], 1);
- isl_seq_clr(m->row[0]+1, set->dim);
- isl_seq_cpy(m->row[1], c, 1+set->dim);
- m = isl_mat_left_hermite(ctx, m, 0, &U, &Q);
- if (!m)
- goto error;
+ isl_seq_clr(m->row[0]+1, dim);
+ isl_seq_cpy(m->row[1], c, 1+dim);
+ U = isl_mat_right_inverse(ctx, m);
+ Q = isl_mat_right_inverse(ctx, isl_mat_copy(ctx, U));
U = isl_mat_drop_cols(ctx, U, 1, 1);
Q = isl_mat_drop_rows(ctx, Q, 1, 1);
set = isl_set_preimage(ctx, set, U);
- facet = uset_convex_hull(set);
+ facet = uset_convex_hull_wrap(set);
facet = isl_basic_set_preimage(ctx, facet, Q);
- isl_mat_free(ctx, m);
return facet;
error:
isl_set_free(set);
struct isl_basic_set *facet = NULL;
unsigned n_ineq;
unsigned total;
+ unsigned dim;
isl_assert(ctx, set->n > 0, goto error);
n_ineq += set->p[i]->n_eq;
n_ineq += set->p[i]->n_ineq;
}
- isl_assert(ctx, 1 + set->dim == initial->n_col, goto error);
- hull = isl_basic_set_alloc(ctx, 0, set->dim, 0,
- 0, n_ineq + 2 * set->p[0]->n_div);
+ dim = isl_set_n_dim(set);
+ isl_assert(ctx, 1 + dim == initial->n_col, goto error);
+ hull = isl_basic_set_alloc(ctx, 0, dim, 0, 0, n_ineq);
+ hull = isl_basic_set_set_rational(hull);
if (!hull)
goto error;
k = isl_basic_set_alloc_inequality(hull);
facet = compute_facet(ctx, set, hull->ineq[i]);
if (!facet)
goto error;
+ if (facet->n_ineq + hull->n_ineq > n_ineq) {
+ hull = isl_basic_set_extend(hull,
+ 0, dim, 0, 0, facet->n_ineq);
+ n_ineq = hull->n_ineq + facet->n_ineq;
+ }
for (j = 0; j < facet->n_ineq; ++j) {
k = isl_basic_set_alloc_inequality(hull);
if (k < 0)
goto error;
- isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+hull->dim);
+ isl_seq_cpy(hull->ineq[k], hull->ineq[i], 1+dim);
if (!wrap_facet(ctx, set, hull->ineq[k], facet->ineq[j]))
goto error;
for (f = 0; f < k; ++f)
if (isl_seq_eq(hull->ineq[f], hull->ineq[k],
- 1+hull->dim))
+ 1+dim))
break;
if (f < k)
isl_basic_set_free_inequality(hull, 1);
hull = isl_basic_set_finalize(hull);
return hull;
error:
+ isl_basic_set_free(facet);
isl_basic_set_free(hull);
return NULL;
}
isl_int_clear(b);
hull = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
+ hull = isl_basic_set_set_rational(hull);
if (!hull)
goto error;
if (lower) {
static struct isl_set *set_project_out(struct isl_ctx *ctx,
struct isl_set *set, unsigned n)
{
- int i;
+ return isl_set_remove_dims(set, isl_set_n_dim(set) - n, n);
+}
+
+static struct isl_basic_set *convex_hull_0d(struct isl_set *set)
+{
+ struct isl_basic_set *convex_hull;
- set = isl_set_cow(set);
if (!set)
return NULL;
-
- for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
- set->dim - n, n);
- if (!set->p[i])
+
+ if (isl_set_is_empty(set))
+ convex_hull = isl_basic_set_empty(isl_dim_copy(set->dim));
+ else
+ convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
+ isl_set_free(set);
+ return convex_hull;
+}
+
+/* Compute the convex hull of a pair of basic sets without any parameters or
+ * integer divisions using Fourier-Motzkin elimination.
+ * The convex hull is the set of all points that can be written as
+ * the sum of points from both basic sets (in homogeneous coordinates).
+ * We set up the constraints in a space with dimensions for each of
+ * the three sets and then project out the dimensions corresponding
+ * to the two original basic sets, retaining only those corresponding
+ * to the convex hull.
+ */
+static struct isl_basic_set *convex_hull_pair(struct isl_basic_set *bset1,
+ struct isl_basic_set *bset2)
+{
+ int i, j, k;
+ struct isl_basic_set *bset[2];
+ struct isl_basic_set *hull = NULL;
+ unsigned dim;
+
+ if (!bset1 || !bset2)
+ goto error;
+
+ dim = isl_basic_set_n_dim(bset1);
+ hull = isl_basic_set_alloc(bset1->ctx, 0, 2 + 3 * dim, 0,
+ 1 + dim + bset1->n_eq + bset2->n_eq,
+ 2 + bset1->n_ineq + bset2->n_ineq);
+ bset[0] = bset1;
+ bset[1] = bset2;
+ for (i = 0; i < 2; ++i) {
+ for (j = 0; j < bset[i]->n_eq; ++j) {
+ k = isl_basic_set_alloc_equality(hull);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(hull->eq[k], (i+1) * (1+dim));
+ isl_seq_clr(hull->eq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
+ isl_seq_cpy(hull->eq[k]+(i+1)*(1+dim), bset[i]->eq[j],
+ 1+dim);
+ }
+ for (j = 0; j < bset[i]->n_ineq; ++j) {
+ k = isl_basic_set_alloc_inequality(hull);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(hull->ineq[k], (i+1) * (1+dim));
+ isl_seq_clr(hull->ineq[k]+(i+2)*(1+dim), (1-i)*(1+dim));
+ isl_seq_cpy(hull->ineq[k]+(i+1)*(1+dim),
+ bset[i]->ineq[j], 1+dim);
+ }
+ k = isl_basic_set_alloc_inequality(hull);
+ if (k < 0)
goto error;
+ isl_seq_clr(hull->ineq[k], 1+2+3*dim);
+ isl_int_set_si(hull->ineq[k][(i+1)*(1+dim)], 1);
}
- set = isl_set_drop_vars(set, set->dim - n, n);
- return set;
+ for (j = 0; j < 1+dim; ++j) {
+ k = isl_basic_set_alloc_equality(hull);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(hull->eq[k], 1+2+3*dim);
+ isl_int_set_si(hull->eq[k][j], -1);
+ isl_int_set_si(hull->eq[k][1+dim+j], 1);
+ isl_int_set_si(hull->eq[k][2*(1+dim)+j], 1);
+ }
+ hull = isl_basic_set_set_rational(hull);
+ hull = isl_basic_set_remove_dims(hull, dim, 2*(1+dim));
+ hull = isl_basic_set_convex_hull(hull);
+ isl_basic_set_free(bset1);
+ isl_basic_set_free(bset2);
+ return hull;
error:
- isl_set_free(set);
+ isl_basic_set_free(bset1);
+ isl_basic_set_free(bset2);
+ isl_basic_set_free(hull);
return NULL;
}
-/* If the number of linearly independent bounds we found is smaller
- * than the dimension, then the convex hull will have a lineality space,
- * so we may as well project out this lineality space.
- * We first transform the set such that the first variables correspond
- * to the directions of the linearly independent bounds and then
- * project out the remaining variables.
+/* Compute the convex hull of a set without any parameters or
+ * integer divisions using Fourier-Motzkin elimination.
+ * In each step, we combined two basic sets until only one
+ * basic set is left.
*/
-static struct isl_basic_set *modulo_lineality(struct isl_ctx *ctx,
+static struct isl_basic_set *uset_convex_hull_elim(struct isl_set *set)
+{
+ struct isl_basic_set *convex_hull = NULL;
+
+ convex_hull = isl_set_copy_basic_set(set);
+ set = isl_set_drop_basic_set(set, convex_hull);
+ if (!set)
+ goto error;
+ while (set->n > 0) {
+ struct isl_basic_set *t;
+ t = isl_set_copy_basic_set(set);
+ if (!t)
+ goto error;
+ set = isl_set_drop_basic_set(set, t);
+ if (!set)
+ goto error;
+ convex_hull = convex_hull_pair(convex_hull, t);
+ }
+ isl_set_free(set);
+ return convex_hull;
+error:
+ isl_set_free(set);
+ isl_basic_set_free(convex_hull);
+ return NULL;
+}
+
+static struct isl_basic_set *uset_convex_hull_wrap_with_bounds(
struct isl_set *set, struct isl_mat *bounds)
{
- int i, j;
- unsigned old_dim, new_dim;
- struct isl_mat *H = NULL, *U = NULL, *Q = NULL;
- struct isl_basic_set *hull;
+ struct isl_basic_set *convex_hull = NULL;
- old_dim = set->dim;
- new_dim = bounds->n_row;
- H = isl_mat_sub_alloc(ctx, bounds->row, 0, bounds->n_row, 1, set->dim);
- H = isl_mat_left_hermite(ctx, H, 0, &U, &Q);
- if (!H)
+ isl_assert(set->ctx, bounds->n_row == isl_set_n_dim(set), goto error);
+ bounds = initial_facet_constraint(set->ctx, set, bounds);
+ if (!bounds)
goto error;
- U = isl_mat_lin_to_aff(ctx, U);
- Q = isl_mat_lin_to_aff(ctx, Q);
- Q->n_row = 1 + new_dim;
- isl_mat_free(ctx, H);
- set = isl_set_preimage(ctx, set, U);
- set = set_project_out(ctx, set, old_dim - new_dim);
- hull = uset_convex_hull(set);
- hull = isl_basic_set_preimage(ctx, hull, Q);
- isl_mat_free(ctx, bounds);
- return hull;
+ convex_hull = extend(set->ctx, set, bounds);
+ isl_mat_free(set->ctx, bounds);
+ isl_set_free(set);
+
+ return convex_hull;
error:
- isl_mat_free(ctx, bounds);
- isl_mat_free(ctx, Q);
isl_set_free(set);
return NULL;
}
+/* Compute the convex hull of a set without any parameters or
+ * integer divisions. Depending on whether the set is bounded,
+ * we pass control to the wrapping based convex hull or
+ * the Fourier-Motzkin elimination based convex hull.
+ * We also handle a few special cases before checking the boundedness.
+ */
+static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
+{
+ int i;
+ struct isl_basic_set *convex_hull = NULL;
+ struct isl_mat *bounds;
+
+ if (isl_set_n_dim(set) == 0)
+ return convex_hull_0d(set);
+
+ set = isl_set_set_rational(set);
+
+ if (!set)
+ goto error;
+ for (i = 0; i < set->n; ++i) {
+ set->p[i] = isl_basic_set_convex_hull(set->p[i]);
+ if (!set->p[i])
+ goto error;
+ }
+ set = isl_set_remove_empty_parts(set);
+ if (!set)
+ return NULL;
+ if (set->n == 1) {
+ convex_hull = isl_basic_set_copy(set->p[0]);
+ isl_set_free(set);
+ return convex_hull;
+ }
+ if (isl_set_n_dim(set) == 1)
+ return convex_hull_1d(set->ctx, set);
+
+ bounds = independent_bounds(set->ctx, set);
+ if (!bounds)
+ goto error;
+ if (bounds->n_row == isl_set_n_dim(set))
+ return uset_convex_hull_wrap_with_bounds(set, bounds);
+ isl_mat_free(set->ctx, bounds);
+
+ return uset_convex_hull_elim(set);
+error:
+ isl_set_free(set);
+ isl_basic_set_free(convex_hull);
+ return NULL;
+}
+
/* This is the core procedure, where "set" is a "pure" set, i.e.,
* without parameters or divs and where the convex hull of set is
* known to be full-dimensional.
*/
-static struct isl_basic_set *uset_convex_hull(struct isl_set *set)
+static struct isl_basic_set *uset_convex_hull_wrap(struct isl_set *set)
{
int i;
struct isl_basic_set *convex_hull = NULL;
struct isl_mat *bounds;
- if (set->dim == 0) {
- convex_hull = isl_basic_set_universe(set->ctx, 0, 0);
+ if (isl_set_n_dim(set) == 0) {
+ convex_hull = isl_basic_set_universe(isl_dim_copy(set->dim));
isl_set_free(set);
+ convex_hull = isl_basic_set_set_rational(convex_hull);
return convex_hull;
}
isl_set_free(set);
return convex_hull;
}
- if (set->dim == 1)
+ if (isl_set_n_dim(set) == 1)
return convex_hull_1d(set->ctx, set);
bounds = independent_bounds(set->ctx, set);
if (!bounds)
goto error;
- if (bounds->n_row < set->dim)
- return modulo_lineality(set->ctx, set, bounds);
- bounds = initial_facet_constraint(set->ctx, set, bounds);
- if (!bounds)
- goto error;
- convex_hull = extend(set->ctx, set, bounds);
- isl_mat_free(set->ctx, bounds);
- isl_set_free(set);
-
- return convex_hull;
+ return uset_convex_hull_wrap_with_bounds(set, bounds);
error:
isl_set_free(set);
return NULL;
struct isl_basic_map *isl_map_convex_hull(struct isl_map *map)
{
struct isl_basic_set *bset;
+ struct isl_basic_map *model = NULL;
struct isl_basic_set *affine_hull = NULL;
struct isl_basic_map *convex_hull = NULL;
struct isl_set *set = NULL;
ctx = map->ctx;
if (map->n == 0) {
- convex_hull = isl_basic_map_empty(ctx,
- map->nparam, map->n_in, map->n_out);
+ convex_hull = isl_basic_map_empty_like_map(map);
isl_map_free(map);
return convex_hull;
}
- set = isl_map_underlying_set(isl_map_copy(map));
+ map = isl_map_align_divs(map);
+ model = isl_basic_map_copy(map->p[0]);
+ set = isl_map_underlying_set(map);
if (!set)
goto error;
bset = uset_convex_hull(set);
}
- convex_hull = isl_basic_map_overlying_set(bset,
- isl_basic_map_copy(map->p[0]));
+ convex_hull = isl_basic_map_overlying_set(bset, model);
- isl_map_free(map);
+ F_CLR(convex_hull, ISL_BASIC_MAP_RATIONAL);
return convex_hull;
error:
isl_set_free(set);
- isl_map_free(map);
+ isl_basic_map_free(model);
return NULL;
}
return (struct isl_basic_set *)
isl_map_convex_hull((struct isl_map *)set);
}
+
+/* Compute a superset of the convex hull of map that is described
+ * by only translates of the constraints in the constituents of map.
+ *
+ * The implementation is not very efficient. In particular, if
+ * constraints with the same normal appear in more than one
+ * basic map, they will be (re)examined each time.
+ */
+struct isl_basic_map *isl_map_simple_hull(struct isl_map *map)
+{
+ struct isl_set *set = NULL;
+ struct isl_basic_map *model = NULL;
+ struct isl_basic_map *hull;
+ struct isl_basic_set *bset = NULL;
+ int i, j;
+ unsigned n_ineq;
+ unsigned dim;
+
+ if (!map)
+ return NULL;
+ if (map->n == 0) {
+ hull = isl_basic_map_empty_like_map(map);
+ isl_map_free(map);
+ return hull;
+ }
+ if (map->n == 1) {
+ hull = isl_basic_map_copy(map->p[0]);
+ isl_map_free(map);
+ return hull;
+ }
+
+ map = isl_map_align_divs(map);
+ model = isl_basic_map_copy(map->p[0]);
+
+ n_ineq = 0;
+ for (i = 0; i < map->n; ++i) {
+ if (!map->p[i])
+ goto error;
+ n_ineq += map->p[i]->n_ineq;
+ }
+
+ set = isl_map_underlying_set(map);
+ if (!set)
+ goto error;
+
+ bset = isl_set_affine_hull(isl_set_copy(set));
+ if (!bset)
+ goto error;
+ dim = isl_basic_set_n_dim(bset);
+ bset = isl_basic_set_extend(bset, 0, dim, 0, 0, n_ineq);
+ if (!bset)
+ goto error;
+
+ for (i = 0; i < set->n; ++i) {
+ for (j = 0; j < set->p[i]->n_ineq; ++j) {
+ int k;
+ int is_bound;
+
+ k = isl_basic_set_alloc_inequality(bset);
+ if (k < 0)
+ goto error;
+ isl_seq_cpy(bset->ineq[k], set->p[i]->ineq[j], 1 + dim);
+ is_bound = uset_is_bound(set->ctx, set, bset->ineq[k],
+ 1 + dim);
+ if (is_bound < 0)
+ goto error;
+ if (!is_bound)
+ isl_basic_set_free_inequality(bset, 1);
+ }
+ }
+
+ bset = isl_basic_set_simplify(bset);
+ bset = isl_basic_set_finalize(bset);
+ bset = isl_basic_set_convex_hull(bset);
+
+ hull = isl_basic_map_overlying_set(bset, isl_basic_map_copy(model));
+
+ isl_set_free(set);
+ return hull;
+error:
+ isl_basic_set_free(bset);
+ isl_set_free(set);
+ isl_basic_map_free(model);
+ return NULL;
+}
+
+struct isl_basic_set *isl_set_simple_hull(struct isl_set *set)
+{
+ return (struct isl_basic_set *)
+ isl_map_simple_hull((struct isl_map *)set);
+}