/*
* Copyright 2008-2009 Katholieke Universiteit Leuven
+ * Copyright 2012 Ecole Normale Superieure
*
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege, K.U.Leuven, Departement
* Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
+ * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
*/
#include <strings.h>
#include <isl/map.h>
#include <isl/seq.h>
#include "isl_tab.h"
-#include <isl_dim_private.h>
+#include <isl_space_private.h>
#include <isl_mat_private.h>
static void swap_equality(struct isl_basic_map *bmap, int a, int b)
isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
- if (n == 0 && !isl_dim_get_tuple_name(bset->dim, isl_dim_set))
+ if (n == 0 && !isl_space_get_tuple_name(bset->dim, isl_dim_set))
return bset;
bset = isl_basic_set_cow(bset);
constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
(bset->dim->n_out-first-n)+bset->extra);
- bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
+ bset->dim = isl_space_drop_outputs(bset->dim, first, n);
if (!bset->dim)
goto error;
isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
- if (n == 0 && !isl_dim_get_tuple_name(set->dim, isl_dim_set))
+ if (n == 0 && !isl_space_get_tuple_name(set->dim, isl_dim_set))
return set;
set = isl_set_cow(set);
if (!set)
goto error;
- set->dim = isl_dim_drop_outputs(set->dim, first, n);
+ set->dim = isl_space_drop_outputs(set->dim, first, n);
if (!set->dim)
goto error;
dim = isl_basic_map_dim(bmap, type);
isl_assert(bmap->ctx, first + n <= dim, goto error);
- if (n == 0 && !isl_dim_is_named_or_nested(bmap->dim, type))
+ if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
return bmap;
bmap = isl_basic_map_cow(bmap);
goto error;
isl_basic_map_free_div(bmap, n);
} else
- bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
+ bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
if (!bmap->dim)
goto error;
isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
- if (n == 0 && !isl_dim_get_tuple_name(map->dim, type))
+ if (n == 0 && !isl_space_get_tuple_name(map->dim, type))
return map;
map = isl_map_cow(map);
if (!map)
goto error;
- map->dim = isl_dim_drop(map->dim, type, first, n);
+ map->dim = isl_space_drop_dims(map->dim, type, first, n);
if (!map->dim)
goto error;
if (!bmap)
goto error;
- pos = 1 + isl_dim_total(bmap->dim) + div;
+ pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
isl_assert(bmap->ctx, div < bmap->n_div, goto error);
(struct isl_basic_map *)bset);
}
+/* Remove any common factor in numerator and denominator of the div expression,
+ * not taking into account the constant term.
+ * That is, if the div is of the form
+ *
+ * floor((a + m f(x))/(m d))
+ *
+ * then replace it by
+ *
+ * floor((floor(a/m) + f(x))/d)
+ *
+ * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
+ * and can therefore not influence the result of the floor.
+ */
+static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
+{
+ unsigned total = isl_basic_map_total_dim(bmap);
+ isl_ctx *ctx = bmap->ctx;
+
+ if (isl_int_is_zero(bmap->div[div][0]))
+ return;
+ isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
+ isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
+ if (isl_int_is_one(ctx->normalize_gcd))
+ return;
+ isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
+ ctx->normalize_gcd);
+ isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
+ ctx->normalize_gcd);
+ isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
+ ctx->normalize_gcd, total);
+}
+
+/* Remove any common factor in numerator and denominator of a div expression,
+ * not taking into account the constant term.
+ * That is, look for any div of the form
+ *
+ * floor((a + m f(x))/(m d))
+ *
+ * and replace it by
+ *
+ * floor((floor(a/m) + f(x))/d)
+ *
+ * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
+ * and can therefore not influence the result of the floor.
+ */
+static __isl_give isl_basic_map *normalize_div_expressions(
+ __isl_take isl_basic_map *bmap)
+{
+ int i;
+
+ if (!bmap)
+ return NULL;
+ if (bmap->n_div == 0)
+ return bmap;
+
+ for (i = 0; i < bmap->n_div; ++i)
+ normalize_div_expression(bmap, i);
+
+ return bmap;
+}
+
/* Assumes divs have been ordered if keep_divs is set.
*/
static void eliminate_var_using_equality(struct isl_basic_map *bmap,
unsigned pos, isl_int *eq, int keep_divs, int *progress)
{
unsigned total;
+ unsigned space_total;
int k;
int last_div;
total = isl_basic_map_total_dim(bmap);
- last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
- bmap->n_div);
+ space_total = isl_space_dim(bmap->dim, isl_dim_all);
+ last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
for (k = 0; k < bmap->n_eq; ++k) {
if (bmap->eq[k] == eq)
continue;
* and we can keep the definition as long as the result
* is still ordered.
*/
- if (last_div == -1 || (keep_divs && last_div < k))
+ if (last_div == -1 || (keep_divs && last_div < k)) {
isl_seq_elim(bmap->div[k]+1, eq,
1+pos, 1+total, &bmap->div[k][0]);
- else
+ normalize_div_expression(bmap, k);
+ } else
isl_seq_clr(bmap->div[k], 1 + total);
ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
}
static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
unsigned div, int keep_divs)
{
- unsigned pos = isl_dim_total(bmap->dim) + div;
+ unsigned pos = isl_space_dim(bmap->dim, isl_dim_all) + div;
eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
{
int k;
int last_div;
- unsigned pos = isl_dim_total(bmap->dim) + div;
+ unsigned space_total = isl_space_dim(bmap->dim, isl_dim_all);
+ unsigned pos = space_total + div;
- last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
- bmap->n_div);
+ last_div = isl_seq_last_non_zero(eq + 1 + space_total, bmap->n_div);
if (last_div < 0 || last_div <= div)
return 1;
if (!bmap)
return NULL;
- off = 1 + isl_dim_total(bmap->dim);
+ off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
for (d = bmap->n_div - 1; d >= 0 ; --d) {
for (i = 0; i < bmap->n_eq; ++i) {
return NULL;
ctx = bmap->ctx;
- off = 1 + isl_dim_total(bmap->dim);
+ off = 1 + isl_space_dim(bmap->dim, isl_dim_all);
for (d = bmap->n_div - 1; d >= 0 ; --d) {
for (i = 0; i < bmap->n_eq; ++i)
isl_int_set_si(bmap->div[div][1+1+last_var], 0);
isl_int_set(bmap->div[div][0],
bmap->eq[done][1+last_var]);
+ if (progress)
+ *progress = 1;
ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
}
}
unsigned total;
struct isl_ctx *ctx;
+ bmap = isl_basic_map_order_divs(bmap);
if (!bmap || bmap->n_div <= 1)
return bmap;
- total_var = isl_dim_total(bmap->dim);
+ total_var = isl_space_dim(bmap->dim, isl_dim_all);
total = total_var + bmap->n_div;
ctx = bmap->ctx;
k = elim_for[l] - 1;
isl_int_set_si(eq.data[1+total_var+k], -1);
isl_int_set_si(eq.data[1+total_var+l], 1);
- eliminate_div(bmap, eq.data, l, 0);
+ eliminate_div(bmap, eq.data, l, 1);
isl_int_set_si(eq.data[1+total_var+k], 0);
isl_int_set_si(eq.data[1+total_var+l], 0);
}
int i, j;
unsigned total;
- total = isl_dim_total(bmap->dim);
+ total = isl_space_dim(bmap->dim, isl_dim_all);
for (i = 0, j = bmap->n_div-1; i < bmap->n_eq; ++i) {
while (j >= 0 && isl_int_is_zero(bmap->eq[i][1 + total + j]))
--j;
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS))
return bmap;
- total = isl_dim_total(bmap->dim);
+ total = isl_space_dim(bmap->dim, isl_dim_all);
div_eq = n_pure_div_eq(bmap);
if (div_eq == 0)
return bmap;
needed++;
}
if (needed > dropped) {
- bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
+ bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
needed, needed, 0);
if (!bmap)
goto error;
static struct isl_basic_map *set_div_from_lower_bound(
struct isl_basic_map *bmap, int div, int ineq)
{
- unsigned total = 1 + isl_dim_total(bmap->dim);
+ unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
isl_seq_neg(bmap->div[div] + 1, bmap->ineq[ineq], total + bmap->n_div);
isl_int_set(bmap->div[div][0], bmap->ineq[ineq][total + div]);
int div, int ineq)
{
int j;
- unsigned total = 1 + isl_dim_total(bmap->dim);
+ unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
/* Not defined in terms of unknown divs */
for (j = 0; j < bmap->n_div; ++j) {
return 1;
}
+/* Would an expression for div "div" based on inequality "ineq" of "bmap"
+ * be a better expression than the current one?
+ *
+ * If we do not have any expression yet, then any expression would be better.
+ * Otherwise we check if the last variable involved in the inequality
+ * (disregarding the div that it would define) is in an earlier position
+ * than the last variable involved in the current div expression.
+ */
+static int better_div_constraint(__isl_keep isl_basic_map *bmap,
+ int div, int ineq)
+{
+ unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
+ int last_div;
+ int last_ineq;
+
+ if (isl_int_is_zero(bmap->div[div][0]))
+ return 1;
+
+ if (isl_seq_last_non_zero(bmap->ineq[ineq] + total + div + 1,
+ bmap->n_div - (div + 1)) >= 0)
+ return 0;
+
+ last_ineq = isl_seq_last_non_zero(bmap->ineq[ineq], total + div);
+ last_div = isl_seq_last_non_zero(bmap->div[div] + 1,
+ total + bmap->n_div);
+
+ return last_ineq < last_div;
+}
+
/* Given two constraints "k" and "l" that are opposite to each other,
* except for the constant term, check if we can use them
- * to obtain an expression for one of the hitherto unknown divs.
+ * to obtain an expression for one of the hitherto unknown divs or
+ * a "better" expression for a div for which we already have an expression.
* "sum" is the sum of the constant terms of the constraints.
* If this sum is strictly smaller than the coefficient of one
* of the divs, then this pair can be used define the div.
struct isl_basic_map *bmap, int k, int l, isl_int sum, int *progress)
{
int i;
- unsigned total = 1 + isl_dim_total(bmap->dim);
+ unsigned total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
for (i = 0; i < bmap->n_div; ++i) {
- if (!isl_int_is_zero(bmap->div[i][0]))
- continue;
if (isl_int_is_zero(bmap->ineq[k][total + i]))
continue;
if (isl_int_abs_ge(sum, bmap->ineq[k][total + i]))
continue;
+ if (!better_div_constraint(bmap, i, k))
+ continue;
if (!ok_to_set_div_from_bound(bmap, i, k))
break;
if (isl_int_is_pos(bmap->ineq[k][total + i]))
}
+/* Eliminate knowns divs from constraints where they appear with
+ * a (positive or negative) unit coefficient.
+ *
+ * That is, replace
+ *
+ * floor(e/m) + f >= 0
+ *
+ * by
+ *
+ * e + m f >= 0
+ *
+ * and
+ *
+ * -floor(e/m) + f >= 0
+ *
+ * by
+ *
+ * -e + m f + m - 1 >= 0
+ *
+ * The first conversion is valid because floor(e/m) >= -f is equivalent
+ * to e/m >= -f because -f is an integral expression.
+ * The second conversion follows from the fact that
+ *
+ * -floor(e/m) = ceil(-e/m) = floor((-e + m - 1)/m)
+ *
+ *
+ * We skip integral divs, i.e., those with denominator 1, as we would
+ * risk eliminating the div from the div constraints. We do not need
+ * to handle those divs here anyway since the div constraints will turn
+ * out to form an equality and this equality can then be use to eliminate
+ * the div from all constraints.
+ */
+static __isl_give isl_basic_map *eliminate_unit_divs(
+ __isl_take isl_basic_map *bmap, int *progress)
+{
+ int i, j;
+ isl_ctx *ctx;
+ unsigned total;
+
+ if (!bmap)
+ return NULL;
+
+ ctx = isl_basic_map_get_ctx(bmap);
+ total = 1 + isl_space_dim(bmap->dim, isl_dim_all);
+
+ for (i = 0; i < bmap->n_div; ++i) {
+ if (isl_int_is_zero(bmap->div[i][0]))
+ continue;
+ if (isl_int_is_one(bmap->div[i][0]))
+ continue;
+ for (j = 0; j < bmap->n_ineq; ++j) {
+ int s;
+
+ if (!isl_int_is_one(bmap->ineq[j][total + i]) &&
+ !isl_int_is_negone(bmap->ineq[j][total + i]))
+ continue;
+
+ *progress = 1;
+
+ s = isl_int_sgn(bmap->ineq[j][total + i]);
+ isl_int_set_si(bmap->ineq[j][total + i], 0);
+ if (s < 0)
+ isl_seq_combine(bmap->ineq[j],
+ ctx->negone, bmap->div[i] + 1,
+ bmap->div[i][0], bmap->ineq[j],
+ total + bmap->n_div);
+ else
+ isl_seq_combine(bmap->ineq[j],
+ ctx->one, bmap->div[i] + 1,
+ bmap->div[i][0], bmap->ineq[j],
+ total + bmap->n_div);
+ if (s < 0) {
+ isl_int_add(bmap->ineq[j][0],
+ bmap->ineq[j][0], bmap->div[i][0]);
+ isl_int_sub_ui(bmap->ineq[j][0],
+ bmap->ineq[j][0], 1);
+ }
+ }
+ }
+
+ return bmap;
+}
+
struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
{
int progress = 1;
return NULL;
while (progress) {
progress = 0;
+ if (!bmap)
+ break;
+ if (isl_basic_map_plain_is_empty(bmap))
+ break;
bmap = isl_basic_map_normalize_constraints(bmap);
+ bmap = normalize_div_expressions(bmap);
bmap = remove_duplicate_divs(bmap, &progress);
+ bmap = eliminate_unit_divs(bmap, &progress);
bmap = eliminate_divs_eq(bmap, &progress);
bmap = eliminate_divs_ineq(bmap, &progress);
bmap = isl_basic_map_gauss(bmap, &progress);
if (!bmap)
return -1;
- pos = 1 + isl_dim_total(bmap->dim) + div;
+ pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
if (isl_int_eq(constraint[pos], bmap->div[div][0])) {
int neg;
return 1;
}
+int isl_basic_set_is_div_constraint(__isl_keep isl_basic_set *bset,
+ isl_int *constraint, unsigned div)
+{
+ return isl_basic_map_is_div_constraint(bset, constraint, div);
+}
+
/* If the only constraints a div d=floor(f/m)
* appears in are its two defining constraints
static int div_is_redundant(struct isl_basic_map *bmap, int div)
{
int i;
- unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
+ unsigned pos = 1 + isl_space_dim(bmap->dim, isl_dim_all) + div;
for (i = 0; i < bmap->n_eq; ++i)
if (!isl_int_is_zero(bmap->eq[i][pos]))
{
int i;
+ if (!bmap)
+ return NULL;
+
for (i = 0; i < bmap->n_div; ++i) {
if (isl_int_is_zero(bmap->div[i][0]))
continue;
int d;
int i, j, k;
unsigned total;
+ int need_gauss = 0;
if (n == 0)
return bmap;
bmap = isl_basic_map_cow(bmap);
for (d = pos + n - 1; d >= 0 && d >= pos; --d)
bmap = remove_dependent_vars(bmap, d);
+ if (!bmap)
+ return NULL;
for (d = pos + n - 1;
d >= 0 && d >= total - bmap->n_div && d >= pos; --d)
continue;
eliminate_var_using_equality(bmap, d, bmap->eq[i], 0, NULL);
isl_basic_map_drop_equality(bmap, i);
+ need_gauss = 1;
break;
}
if (i < bmap->n_eq)
bmap = remove_duplicate_constraints(bmap, NULL, 0);
bmap = isl_basic_map_gauss(bmap, NULL);
bmap = isl_basic_map_remove_redundancies(bmap);
+ need_gauss = 0;
if (!bmap)
goto error;
if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
}
}
ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ if (need_gauss)
+ bmap = isl_basic_map_gauss(bmap, NULL);
return bmap;
error:
isl_basic_map_free(bmap);
(struct isl_basic_map *)bset, pos, n);
}
+/* Eliminate the specified n dimensions starting at first from the
+ * constraints, without removing the dimensions from the space.
+ * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
+ * Otherwise, they are projected out and the original space is restored.
+ */
+__isl_give isl_basic_map *isl_basic_map_eliminate(
+ __isl_take isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ isl_space *space;
+
+ if (!bmap)
+ return NULL;
+ if (n == 0)
+ return bmap;
+
+ if (first + n > isl_basic_map_dim(bmap, type) || first + n < first)
+ isl_die(bmap->ctx, isl_error_invalid,
+ "index out of bounds", goto error);
+
+ if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
+ first += isl_basic_map_offset(bmap, type) - 1;
+ bmap = isl_basic_map_eliminate_vars(bmap, first, n);
+ return isl_basic_map_finalize(bmap);
+ }
+
+ space = isl_basic_map_get_space(bmap);
+ bmap = isl_basic_map_project_out(bmap, type, first, n);
+ bmap = isl_basic_map_insert_dims(bmap, type, first, n);
+ bmap = isl_basic_map_reset_space(bmap, space);
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+__isl_give isl_basic_set *isl_basic_set_eliminate(
+ __isl_take isl_basic_set *bset,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ return isl_basic_map_eliminate(bset, type, first, n);
+}
+
/* Don't assume equalities are in order, because align_divs
* may have changed the order of the divs.
*/
int d, i;
unsigned total;
- total = isl_dim_total(bmap->dim);
+ total = isl_space_dim(bmap->dim, isl_dim_all);
for (d = 0; d < total; ++d)
elim[d] = -1;
for (i = 0; i < bmap->n_eq; ++i) {
int copied = 0;
unsigned total;
- total = isl_dim_total(bmap->dim);
+ total = isl_space_dim(bmap->dim, isl_dim_all);
for (d = total - 1; d >= 0; --d) {
if (isl_int_is_zero(src[1+d]))
continue;
return bset;
}
+/* Does the (linear part of a) constraint "c" involve any of the "len"
+ * "relevant" dimensions?
+ */
+static int is_related(isl_int *c, int len, int *relevant)
+{
+ int i;
+
+ for (i = 0; i < len; ++i) {
+ if (!relevant[i])
+ continue;
+ if (!isl_int_is_zero(c[i]))
+ return 1;
+ }
+
+ return 0;
+}
+
+/* Drop constraints from "bset" that do not involve any of
+ * the dimensions marked "relevant".
+ */
+static __isl_give isl_basic_set *drop_unrelated_constraints(
+ __isl_take isl_basic_set *bset, int *relevant)
+{
+ int i, dim;
+
+ dim = isl_basic_set_dim(bset, isl_dim_set);
+ for (i = 0; i < dim; ++i)
+ if (!relevant[i])
+ break;
+ if (i >= dim)
+ return bset;
+
+ for (i = bset->n_eq - 1; i >= 0; --i)
+ if (!is_related(bset->eq[i] + 1, dim, relevant))
+ isl_basic_set_drop_equality(bset, i);
+
+ for (i = bset->n_ineq - 1; i >= 0; --i)
+ if (!is_related(bset->ineq[i] + 1, dim, relevant))
+ isl_basic_set_drop_inequality(bset, i);
+
+ return bset;
+}
+
+/* Update the groups in "group" based on the (linear part of a) constraint "c".
+ *
+ * In particular, for any variable involved in the constraint,
+ * find the actual group id from before and replace the group
+ * of the corresponding variable by the minimal group of all
+ * the variables involved in the constraint considered so far
+ * (if this minimum is smaller) or replace the minimum by this group
+ * (if the minimum is larger).
+ *
+ * At the end, all the variables in "c" will (indirectly) point
+ * to the minimal of the groups that they referred to originally.
+ */
+static void update_groups(int dim, int *group, isl_int *c)
+{
+ int j;
+ int min = dim;
+
+ for (j = 0; j < dim; ++j) {
+ if (isl_int_is_zero(c[j]))
+ continue;
+ while (group[j] >= 0 && group[group[j]] != group[j])
+ group[j] = group[group[j]];
+ if (group[j] == min)
+ continue;
+ if (group[j] < min) {
+ if (min >= 0 && min < dim)
+ group[min] = group[j];
+ min = group[j];
+ } else
+ group[group[j]] = min;
+ }
+}
+
+/* Drop constraints from "context" that are irrelevant for computing
+ * the gist of "bset".
+ *
+ * In particular, drop constraints in variables that are not related
+ * to any of the variables involved in the constraints of "bset"
+ * in the sense that there is no sequence of constraints that connects them.
+ *
+ * We construct groups of variables that collect variables that
+ * (indirectly) appear in some common constraint of "context".
+ * Each group is identified by the first variable in the group,
+ * except for the special group of variables that appear in "bset"
+ * (or are related to those variables), which is identified by -1.
+ * If group[i] is equal to i (or -1), then the group of i is i (or -1),
+ * otherwise the group of i is the group of group[i].
+ *
+ * We first initialize the -1 group with the variables that appear in "bset".
+ * Then we initialize groups for the remaining variables.
+ * Then we iterate over the constraints of "context" and update the
+ * group of the variables in the constraint by the smallest group.
+ * Finally, we resolve indirect references to groups by running over
+ * the variables.
+ *
+ * After computing the groups, we drop constraints that do not involve
+ * any variables in the -1 group.
+ */
+static __isl_give isl_basic_set *drop_irrelevant_constraints(
+ __isl_take isl_basic_set *context, __isl_keep isl_basic_set *bset)
+{
+ isl_ctx *ctx;
+ int *group;
+ int dim;
+ int i, j;
+ int last;
+
+ if (!context || !bset)
+ return isl_basic_set_free(context);
+
+ dim = isl_basic_set_dim(bset, isl_dim_set);
+ ctx = isl_basic_set_get_ctx(bset);
+ group = isl_calloc_array(ctx, int, dim);
+
+ if (!group)
+ goto error;
+
+ for (i = 0; i < dim; ++i) {
+ for (j = 0; j < bset->n_eq; ++j)
+ if (!isl_int_is_zero(bset->eq[j][1 + i]))
+ break;
+ if (j < bset->n_eq) {
+ group[i] = -1;
+ continue;
+ }
+ for (j = 0; j < bset->n_ineq; ++j)
+ if (!isl_int_is_zero(bset->ineq[j][1 + i]))
+ break;
+ if (j < bset->n_ineq)
+ group[i] = -1;
+ }
+
+ last = -1;
+ for (i = 0; i < dim; ++i)
+ if (group[i] >= 0)
+ last = group[i] = i;
+ if (last < 0) {
+ free(group);
+ return context;
+ }
+
+ for (i = 0; i < context->n_eq; ++i)
+ update_groups(dim, group, context->eq[i] + 1);
+ for (i = 0; i < context->n_ineq; ++i)
+ update_groups(dim, group, context->ineq[i] + 1);
+
+ for (i = 0; i < dim; ++i)
+ if (group[i] >= 0)
+ group[i] = group[group[i]];
+
+ for (i = 0; i < dim; ++i)
+ group[i] = group[i] == -1;
+
+ context = drop_unrelated_constraints(context, group);
+
+ free(group);
+ return context;
+error:
+ free(group);
+ return isl_basic_set_free(context);
+}
+
/* Remove all information from bset that is redundant in the context
* of context. Both bset and context are assumed to be full-dimensional.
*
- * We first * remove the inequalities from "bset"
+ * We first remove the inequalities from "bset"
* that are obviously redundant with respect to some inequality in "context".
+ * Then we remove those constraints from "context" that have become
+ * irrelevant for computing the gist of "bset".
+ * Note that this removal of constraints cannot be replaced by
+ * a factorization because factors in "bset" may still be connected
+ * to each other through constraints in "context".
*
* If there are any inequalities left, we construct a tableau for
* the context and then add the inequalities of "bset".
if (bset->n_ineq == 0)
goto done;
+ context = drop_irrelevant_constraints(context, bset);
+ if (!context)
+ goto error;
+ if (isl_basic_set_is_universe(context)) {
+ isl_basic_set_free(context);
+ return bset;
+ }
+
context_ineq = context->n_ineq;
combined = isl_basic_set_cow(isl_basic_set_copy(context));
combined = isl_basic_set_extend_constraints(combined, 0, bset->n_ineq);
- tab = isl_tab_from_basic_set(combined);
+ tab = isl_tab_from_basic_set(combined, 0);
for (i = 0; i < context_ineq; ++i)
if (isl_tab_freeze_constraint(tab, i) < 0)
goto error;
* redundant in the context of the equalities and inequalities of
* context are removed.
*
+ * First of all, we drop those constraints from "context"
+ * that are irrelevant for computing the gist of "bset".
+ * Alternatively, we could factorize the intersection of "context" and "bset".
+ *
* We first compute the integer affine hull of the intersection,
* compute the gist inside this affine hull and then add back
* those equalities that are not implied by the context.
if (!bset || !context)
goto error;
+ context = drop_irrelevant_constraints(context, bset);
+
bset = isl_basic_set_intersect(bset, isl_basic_set_copy(context));
if (isl_basic_set_plain_is_empty(bset)) {
isl_basic_set_free(context);
return bmap;
}
if (isl_basic_map_plain_is_empty(context)) {
- struct isl_dim *dim = isl_dim_copy(bmap->dim);
- isl_basic_map_free(context);
isl_basic_map_free(bmap);
- return isl_basic_map_universe(dim);
+ return context;
}
if (isl_basic_map_plain_is_empty(bmap)) {
isl_basic_map_free(context);
goto error;;
if (isl_basic_map_plain_is_empty(context)) {
- struct isl_dim *dim = isl_dim_copy(map->dim);
- isl_basic_map_free(context);
isl_map_free(map);
- return isl_map_universe(dim);
+ return isl_map_from_basic_map(context);
}
context = isl_basic_map_remove_redundancies(context);
map = isl_map_cow(map);
if (!map || !context)
goto error;;
- isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
+ isl_assert(map->ctx, isl_space_is_equal(map->dim, context->dim), goto error);
map = isl_map_compute_divs(map);
+ if (!map)
+ goto error;
for (i = 0; i < map->n; ++i)
context = isl_basic_map_align_divs(context, map->p[i]);
for (i = map->n - 1; i >= 0; --i) {
return NULL;
}
+/* Return a map that has the same intersection with "context" as "map"
+ * and that as "simple" as possible.
+ *
+ * If "map" is already the universe, then we cannot make it any simpler.
+ * Similarly, if "context" is the universe, then we cannot exploit it
+ * to simplify "map"
+ * If "map" and "context" are identical to each other, then we can
+ * return the corresponding universe.
+ *
+ * If none of these cases apply, we have to work a bit harder.
+ */
static __isl_give isl_map *map_gist(__isl_take isl_map *map,
__isl_take isl_map *context)
{
+ int equal;
+ int is_universe;
+
+ is_universe = isl_map_plain_is_universe(map);
+ if (is_universe >= 0 && !is_universe)
+ is_universe = isl_map_plain_is_universe(context);
+ if (is_universe < 0)
+ goto error;
+ if (is_universe) {
+ isl_map_free(context);
+ return map;
+ }
+
+ equal = isl_map_plain_is_equal(map, context);
+ if (equal < 0)
+ goto error;
+ if (equal) {
+ isl_map *res = isl_map_universe(isl_map_get_space(map));
+ isl_map_free(map);
+ isl_map_free(context);
+ return res;
+ }
+
context = isl_map_compute_divs(context);
return isl_map_gist_basic_map(map, isl_map_simple_hull(context));
+error:
+ isl_map_free(map);
+ isl_map_free(context);
+ return NULL;
}
__isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
(struct isl_basic_map *)context);
}
+__isl_give isl_set *isl_set_gist_params_basic_set(__isl_take isl_set *set,
+ __isl_take isl_basic_set *context)
+{
+ isl_space *space = isl_set_get_space(set);
+ isl_basic_set *dom_context = isl_basic_set_universe(space);
+ dom_context = isl_basic_set_intersect_params(dom_context, context);
+ return isl_set_gist_basic_set(set, dom_context);
+}
+
__isl_give isl_set *isl_set_gist(__isl_take isl_set *set,
__isl_take isl_set *context)
{
(struct isl_map *)context);
}
+__isl_give isl_map *isl_map_gist_domain(__isl_take isl_map *map,
+ __isl_take isl_set *context)
+{
+ isl_map *map_context = isl_map_universe(isl_map_get_space(map));
+ map_context = isl_map_intersect_domain(map_context, context);
+ return isl_map_gist(map, map_context);
+}
+
+__isl_give isl_map *isl_map_gist_range(__isl_take isl_map *map,
+ __isl_take isl_set *context)
+{
+ isl_map *map_context = isl_map_universe(isl_map_get_space(map));
+ map_context = isl_map_intersect_range(map_context, context);
+ return isl_map_gist(map, map_context);
+}
+
+__isl_give isl_map *isl_map_gist_params(__isl_take isl_map *map,
+ __isl_take isl_set *context)
+{
+ isl_map *map_context = isl_map_universe(isl_map_get_space(map));
+ map_context = isl_map_intersect_params(map_context, context);
+ return isl_map_gist(map, map_context);
+}
+
+__isl_give isl_set *isl_set_gist_params(__isl_take isl_set *set,
+ __isl_take isl_set *context)
+{
+ return isl_map_gist_params(set, context);
+}
+
/* Quick check to see if two basic maps are disjoint.
* In particular, we reduce the equalities and inequalities of
* one basic map in the context of the equalities of the other
if (!bmap1 || !bmap2)
return -1;
- isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
+ isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
return -1);
if (bmap1->n_div || bmap2->n_div)
return 0;
if (!bmap1->n_eq && !bmap2->n_eq)
return 0;
- total = isl_dim_total(bmap1->dim);
+ total = isl_space_dim(bmap1->dim, isl_dim_all);
if (total == 0)
return 0;
v = isl_vec_alloc(bmap1->ctx, 1 + total);
(struct isl_basic_map *)bset2);
}
+/* Are "map1" and "map2" obviously disjoint?
+ *
+ * If one of them is empty or if they live in different spaces (ignoring
+ * parameters), then they are clearly disjoint.
+ *
+ * If they have different parameters, then we skip any further tests.
+ *
+ * If they are obviously equal, but not obviously empty, then we will
+ * not be able to detect if they are disjoint.
+ *
+ * Otherwise we check if each basic map in "map1" is obviously disjoint
+ * from each basic map in "map2".
+ */
int isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
__isl_keep isl_map *map2)
{
int i, j;
+ int disjoint;
+ int intersect;
+ int match;
if (!map1 || !map2)
return -1;
- if (isl_map_plain_is_equal(map1, map2))
- return 0;
+ disjoint = isl_map_plain_is_empty(map1);
+ if (disjoint < 0 || disjoint)
+ return disjoint;
+
+ disjoint = isl_map_plain_is_empty(map2);
+ if (disjoint < 0 || disjoint)
+ return disjoint;
+
+ match = isl_space_tuple_match(map1->dim, isl_dim_in,
+ map2->dim, isl_dim_in);
+ if (match < 0 || !match)
+ return match < 0 ? -1 : 1;
+
+ match = isl_space_tuple_match(map1->dim, isl_dim_out,
+ map2->dim, isl_dim_out);
+ if (match < 0 || !match)
+ return match < 0 ? -1 : 1;
+
+ match = isl_space_match(map1->dim, isl_dim_param,
+ map2->dim, isl_dim_param);
+ if (match < 0 || !match)
+ return match < 0 ? -1 : 0;
+
+ intersect = isl_map_plain_is_equal(map1, map2);
+ if (intersect < 0 || intersect)
+ return intersect < 0 ? -1 : 0;
for (i = 0; i < map1->n; ++i) {
for (j = 0; j < map2->n; ++j) {
return 1;
}
+/* Are "map1" and "map2" disjoint?
+ *
+ * They are disjoint if they are "obviously disjoint" or if one of them
+ * is empty. Otherwise, they are not disjoint if one of them is universal.
+ * If none of these cases apply, we compute the intersection and see if
+ * the result is empty.
+ */
+int isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
+{
+ int disjoint;
+ int intersect;
+ isl_map *test;
+
+ disjoint = isl_map_plain_is_disjoint(map1, map2);
+ if (disjoint < 0 || disjoint)
+ return disjoint;
+
+ disjoint = isl_map_is_empty(map1);
+ if (disjoint < 0 || disjoint)
+ return disjoint;
+
+ disjoint = isl_map_is_empty(map2);
+ if (disjoint < 0 || disjoint)
+ return disjoint;
+
+ intersect = isl_map_plain_is_universe(map1);
+ if (intersect < 0 || intersect)
+ return intersect < 0 ? -1 : 0;
+
+ intersect = isl_map_plain_is_universe(map2);
+ if (intersect < 0 || intersect)
+ return intersect < 0 ? -1 : 0;
+
+ test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
+ disjoint = isl_map_is_empty(test);
+ isl_map_free(test);
+
+ return disjoint;
+}
+
int isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
__isl_keep isl_set *set2)
{
(struct isl_map *)set2);
}
+/* Are "set1" and "set2" disjoint?
+ */
+int isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
+{
+ return isl_map_is_disjoint(set1, set2);
+}
+
int isl_set_fast_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
{
return isl_set_plain_is_disjoint(set1, set2);
if (bmap->n_div <= 1)
return -1;
- dim = isl_dim_total(bmap->dim);
+ dim = isl_space_dim(bmap->dim, isl_dim_all);
if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim, div) != -1)
return -1;
if (isl_seq_first_non_zero(bmap->ineq[l] + 1 + dim + div + 1,
int l, int u, isl_int *ineq, isl_int g, isl_int fl, isl_int fu)
{
unsigned dim;
- dim = isl_dim_total(bmap->dim);
+ dim = isl_space_dim(bmap->dim, isl_dim_all);
isl_int_gcd(g, bmap->ineq[l][1 + dim + i], bmap->ineq[u][1 + dim + i]);
isl_int_divexact(fl, bmap->ineq[l][1 + dim + i], g);
if (!bmap)
goto error;
- dim = isl_dim_total(bmap->dim);
+ dim = isl_space_dim(bmap->dim, isl_dim_all);
vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
if (!vec)
goto error;
- tab = isl_tab_from_basic_map(bmap);
+ tab = isl_tab_from_basic_map(bmap, 0);
while (n > 0) {
int i, l, u;
unsigned dim, total;
int i;
- dim = isl_dim_total(bmap->dim);
+ dim = isl_space_dim(bmap->dim, isl_dim_all);
total = 1 + dim + bmap->n_div;
isl_int_init(a);
int i, l, u;
unsigned dim;
- dim = isl_dim_total(bmap->dim);
+ dim = isl_space_dim(bmap->dim, isl_dim_all);
for (i = 0; i < bmap->n_div; ++i) {
if (!pairs[i])
/* Remove divs that are not strictly needed.
* In particular, if a div only occurs positively (or negatively)
* in constraints, then it can simply be dropped.
- * Also, if a div occurs only occurs in two constraints and if moreover
+ * Also, if a div occurs in only two constraints and if moreover
* those two constraints are opposite to each other, except for the constant
* term and if the sum of the constant terms is such that for any value
* of the other values, there is always at least one integer value of the
* the (absolute value) of the coefficent of the div in the constraints,
* then we can also simply drop the div.
*
+ * We skip divs that appear in equalities or in the definition of other divs.
+ * Divs that appear in the definition of other divs usually occur in at least
+ * 4 constraints, but the constraints may have been simplified.
+ *
* If any divs are left after these simple checks then we move on
* to more complicated cases in drop_more_redundant_divs.
*/
if (!bmap)
goto error;
- off = isl_dim_total(bmap->dim);
+ off = isl_space_dim(bmap->dim, isl_dim_all);
pairs = isl_calloc_array(bmap->ctx, int, bmap->n_div);
if (!pairs)
goto error;
int defined;
defined = !isl_int_is_zero(bmap->div[i][0]);
+ for (j = i; j < bmap->n_div; ++j)
+ if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
+ break;
+ if (j < bmap->n_div)
+ continue;
for (j = 0; j < bmap->n_eq; ++j)
if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
break;