#include "isl_ctx.h"
#include "isl_blk.h"
#include "isl_dim.h"
+#include "isl_equalities.h"
#include "isl_list.h"
#include "isl_lp.h"
#include "isl_seq.h"
case isl_dim_param: return dim->nparam;
case isl_dim_in: return dim->n_in;
case isl_dim_out: return dim->n_out;
+ case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
}
}
bmap->dim->nparam == bset->dim->nparam;
}
+struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
+{
+ if (!bmap)
+ return NULL;
+ return isl_dim_copy(bmap->dim);
+}
+
struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
{
if (!bset)
return isl_dim_copy(bset->dim);
}
+struct isl_dim *isl_map_get_dim(struct isl_map *map)
+{
+ if (!map)
+ return NULL;
+ return isl_dim_copy(map->dim);
+}
+
struct isl_dim *isl_set_get_dim(struct isl_set *set)
{
if (!set)
return NULL;
dup_constraints(dup, bmap);
dup->flags = bmap->flags;
- dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
+ dup->sample = isl_vec_copy(bmap->sample);
return dup;
}
isl_blk_free(bmap->ctx, bmap->block2);
free(bmap->ineq);
isl_blk_free(bmap->ctx, bmap->block);
- isl_vec_free(bmap->ctx, bmap->sample);
+ isl_vec_free(bmap->sample);
isl_dim_free(bmap->dim);
free(bmap);
}
return 0;
}
+int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
+{
+ return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
+}
+
/* Copy constraint from src to dst, putting the vars of src at offset
* dim_off in dst and the divs of src at offset div_off in dst.
* If both sets are actually map, then dim_off applies to the input
isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
}
-static void swap_div(struct isl_basic_map *bmap, int a, int b)
+void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
{
int i;
unsigned off = isl_dim_total(bmap->dim);
return set;
}
+/* Make sure "map" has room for at least "n" more basic maps.
+ */
+struct isl_map *isl_map_grow(struct isl_map *map, int n)
+{
+ int i;
+ struct isl_map *grown = NULL;
+
+ if (!map)
+ return NULL;
+ isl_assert(map->ctx, n >= 0, goto error);
+ if (map->n + n <= map->size)
+ return map;
+ grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
+ if (!grown)
+ goto error;
+ for (i = 0; i < map->n; ++i) {
+ grown->p[i] = isl_basic_map_copy(map->p[i]);
+ if (!grown->p[i])
+ goto error;
+ grown->n++;
+ }
+ isl_map_free(map);
+ return grown;
+error:
+ isl_map_free(grown);
+ isl_map_free(map);
+ return NULL;
+}
+
+/* Make sure "set" has room for at least "n" more basic sets.
+ */
+struct isl_set *isl_set_grow(struct isl_set *set, int n)
+{
+ return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
+}
+
struct isl_set *isl_set_dup(struct isl_set *set)
{
int i;
struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
{
- if (!bset || !set)
- goto error;
- isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
- isl_assert(set->ctx, set->n < set->size, goto error);
- set->p[set->n] = bset;
- set->n++;
- return set;
-error:
- if (set)
- isl_set_free(set);
- if (bset)
- isl_basic_set_free(bset);
- return NULL;
+ return (struct isl_set *)isl_map_add((struct isl_map *)set,
+ (struct isl_basic_map *)bset);
}
void isl_set_free(struct isl_set *set)
isl_int_init(s);
for (i = 0; i < bmap->n_eq; ++i) {
- isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
+ isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
if (!isl_int_is_zero(s)) {
isl_int_clear(s);
return 0;
}
for (i = 0; i < bmap->n_ineq; ++i) {
- isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
+ isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
if (isl_int_is_neg(s)) {
isl_int_clear(s);
return 0;
if (bmap1->sample &&
basic_map_contains(bmap1, bmap1->sample) > 0 &&
basic_map_contains(bmap2, bmap1->sample) > 0)
- sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
+ sample = isl_vec_copy(bmap1->sample);
else if (bmap2->sample &&
basic_map_contains(bmap1, bmap2->sample) > 0 &&
basic_map_contains(bmap2, bmap2->sample) > 0)
- sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
+ sample = isl_vec_copy(bmap2->sample);
bmap1 = isl_basic_map_cow(bmap1);
bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
bmap1 = add_constraints(bmap1, bmap2, 0, 0);
if (sample) {
- isl_vec_free(bmap1->ctx, bmap1->sample);
+ isl_vec_free(bmap1->sample);
bmap1->sample = sample;
}
return isl_basic_map_finalize(bmap1);
error:
if (sample)
- isl_vec_free(bmap1->ctx, sample);
+ isl_vec_free(sample);
isl_basic_map_free(bmap1);
isl_basic_map_free(bmap2);
return NULL;
return NULL;
}
-struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
+static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
{
int i, j;
{
if (!bmap || !map)
goto error;
+ if (isl_basic_map_fast_is_empty(bmap)) {
+ isl_basic_map_free(bmap);
+ return map;
+ }
isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
isl_assert(map->ctx, map->n < map->size, goto error);
map->p[map->n] = bmap;
j = isl_basic_map_alloc_equality(bmap);
if (j < 0)
goto error;
- isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
+ isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
isl_int_set_si(bmap->eq[j][pos], -1);
isl_int_set_si(bmap->eq[j][0], value);
bmap = isl_basic_map_simplify(bmap);
return NULL;
}
-struct isl_map *isl_basic_map_lexmax(
+static struct isl_map *isl_basic_map_partial_lexopt(
+ struct isl_basic_map *bmap, struct isl_basic_set *dom,
+ struct isl_set **empty, int max)
+{
+ return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
+}
+
+struct isl_map *isl_basic_map_partial_lexmax(
struct isl_basic_map *bmap, struct isl_basic_set *dom,
struct isl_set **empty)
{
- return isl_pip_basic_map_lexmax(bmap, dom, empty);
+ return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
}
-struct isl_map *isl_basic_map_lexmin(
+struct isl_map *isl_basic_map_partial_lexmin(
struct isl_basic_map *bmap, struct isl_basic_set *dom,
struct isl_set **empty)
{
- return isl_pip_basic_map_lexmin(bmap, dom, empty);
+ return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
+}
+
+struct isl_set *isl_basic_set_partial_lexmin(
+ struct isl_basic_set *bset, struct isl_basic_set *dom,
+ struct isl_set **empty)
+{
+ return (struct isl_set *)
+ isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
+ dom, empty);
+}
+
+struct isl_set *isl_basic_set_partial_lexmax(
+ struct isl_basic_set *bset, struct isl_basic_set *dom,
+ struct isl_set **empty)
+{
+ return (struct isl_set *)
+ isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
+ dom, empty);
}
struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
dom = isl_basic_set_universe(param_dim);
if (!dom)
goto error;
- min = isl_basic_map_lexmin(bmap, dom, NULL);
+ min = isl_basic_map_partial_lexmin(bmap, dom, NULL);
return isl_map_range(min);
error:
isl_basic_map_free(bmap);
return NULL;
}
+static struct isl_map *isl_map_reset_dim(struct isl_map *map,
+ struct isl_dim *dim)
+{
+ int i;
+
+ if (!map || !dim)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ isl_dim_free(map->p[i]->dim);
+ map->p[i]->dim = isl_dim_copy(dim);
+ }
+ isl_dim_free(map->dim);
+ map->dim = dim;
+
+ return map;
+error:
+ isl_map_free(map);
+ isl_dim_free(dim);
+ return NULL;
+}
+
+static struct isl_set *isl_set_reset_dim(struct isl_set *set,
+ struct isl_dim *dim)
+{
+ return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
+}
+
+/* Apply a preimage specified by "mat" on the parameters of "bset".
+ * bset is assumed to have only parameters and divs.
+ */
+static struct isl_basic_set *basic_set_parameter_preimage(
+ struct isl_basic_set *bset, struct isl_mat *mat)
+{
+ unsigned nparam;
+
+ if (!bset || !mat)
+ goto error;
+
+ bset->dim = isl_dim_cow(bset->dim);
+ if (!bset->dim)
+ goto error;
+
+ nparam = isl_basic_set_dim(bset, isl_dim_param);
+
+ isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
+
+ bset->dim->nparam = 0;
+ bset->dim->n_out = nparam;
+ bset = isl_basic_set_preimage(bset, mat);
+ if (bset) {
+ bset->dim->nparam = bset->dim->n_out;
+ bset->dim->n_out = 0;
+ }
+ return bset;
+error:
+ isl_mat_free(mat);
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+/* Apply a preimage specified by "mat" on the parameters of "set".
+ * set is assumed to have only parameters and divs.
+ */
+static struct isl_set *set_parameter_preimage(
+ struct isl_set *set, struct isl_mat *mat)
+{
+ struct isl_dim *dim = NULL;
+ unsigned nparam;
+
+ if (!set || !mat)
+ goto error;
+
+ dim = isl_dim_copy(set->dim);
+ dim = isl_dim_cow(dim);
+ if (!dim)
+ goto error;
+
+ nparam = isl_set_dim(set, isl_dim_param);
+
+ isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
+
+ dim->nparam = 0;
+ dim->n_out = nparam;
+ isl_set_reset_dim(set, dim);
+ set = isl_set_preimage(set, mat);
+ if (!set)
+ goto error2;
+ dim = isl_dim_copy(set->dim);
+ dim = isl_dim_cow(dim);
+ if (!dim)
+ goto error2;
+ dim->nparam = dim->n_out;
+ dim->n_out = 0;
+ isl_set_reset_dim(set, dim);
+ return set;
+error:
+ isl_dim_free(dim);
+ isl_mat_free(mat);
+error2:
+ isl_set_free(set);
+ return NULL;
+}
+
+/* Intersect the basic set "bset" with the affine space specified by the
+ * equalities in "eq".
+ */
+static struct isl_basic_set *basic_set_append_equalities(
+ struct isl_basic_set *bset, struct isl_mat *eq)
+{
+ int i, k;
+ unsigned len;
+
+ if (!bset || !eq)
+ goto error;
+
+ bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
+ eq->n_row, 0);
+ if (!bset)
+ goto error;
+
+ len = 1 + isl_dim_total(bset->dim) + bset->extra;
+ for (i = 0; i < eq->n_row; ++i) {
+ k = isl_basic_set_alloc_equality(bset);
+ if (k < 0)
+ goto error;
+ isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
+ isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
+ }
+ isl_mat_free(eq);
+
+ return bset;
+error:
+ isl_mat_free(eq);
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+/* Intersect the set "set" with the affine space specified by the
+ * equalities in "eq".
+ */
+static struct isl_set *set_append_equalities(struct isl_set *set,
+ struct isl_mat *eq)
+{
+ int i;
+
+ if (!set || !eq)
+ goto error;
+
+ for (i = 0; i < set->n; ++i) {
+ set->p[i] = basic_set_append_equalities(set->p[i],
+ isl_mat_copy(eq));
+ if (!set->p[i])
+ goto error;
+ }
+ isl_mat_free(eq);
+ return set;
+error:
+ isl_mat_free(eq);
+ isl_set_free(set);
+ return NULL;
+}
+
+/* Project the given basic set onto its parameter domain, possibly introducing
+ * new, explicit, existential variables in the constraints.
+ * The input has parameters and (possibly implicit) existential variables.
+ * The output has the same parameters, but only
+ * explicit existentially quantified variables.
+ *
+ * The actual projection is performed by pip, but pip doesn't seem
+ * to like equalities very much, so we first remove the equalities
+ * among the parameters by performing a variable compression on
+ * the parameters. Afterward, an inverse transformation is performed
+ * and the equalities among the parameters are inserted back in.
+ */
+static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
+{
+ int i, j;
+ struct isl_mat *eq;
+ struct isl_mat *T, *T2;
+ struct isl_set *set;
+ unsigned nparam, n_div;
+
+ bset = isl_basic_set_cow(bset);
+ if (!bset)
+ return NULL;
+
+ if (bset->n_eq == 0)
+ return isl_basic_set_lexmin(bset);
+
+ isl_basic_set_gauss(bset, NULL);
+
+ nparam = isl_basic_set_dim(bset, isl_dim_param);
+ n_div = isl_basic_set_dim(bset, isl_dim_div);
+
+ for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
+ if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
+ ++i;
+ }
+ if (i == bset->n_eq)
+ return isl_basic_set_lexmin(bset);
+
+ eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
+ 0, 1 + nparam);
+ eq = isl_mat_cow(eq);
+ T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
+ bset = basic_set_parameter_preimage(bset, T);
+
+ set = isl_basic_set_lexmin(bset);
+ set = set_parameter_preimage(set, T2);
+ set = set_append_equalities(set, eq);
+ return set;
+}
+
+/* Compute an explicit representation for all the existentially
+ * quantified variables.
+ * The input and output dimensions are first turned into parameters.
+ * compute_divs then returns a map with the same parameters and
+ * no input or output dimensions and the dimension specification
+ * is reset to that of the input.
+ */
+static struct isl_map *compute_divs(struct isl_basic_map *bmap)
+{
+ struct isl_basic_set *bset;
+ struct isl_set *set;
+ struct isl_map *map;
+ struct isl_dim *dim, *orig_dim = NULL;
+ unsigned nparam;
+ unsigned n_in;
+ unsigned n_out;
+
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
+
+ nparam = isl_basic_map_dim(bmap, isl_dim_param);
+ n_in = isl_basic_map_dim(bmap, isl_dim_in);
+ n_out = isl_basic_map_dim(bmap, isl_dim_out);
+ dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
+ if (!dim)
+ goto error;
+
+ orig_dim = bmap->dim;
+ bmap->dim = dim;
+ bset = (struct isl_basic_set *)bmap;
+
+ set = parameter_compute_divs(bset);
+ map = (struct isl_map *)set;
+ map = isl_map_reset_dim(map, orig_dim);
+
+ return map;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+/* If bmap contains any unknown divs, then compute explicit
+ * expressions for them. However, this computation may be
+ * quite expensive, so first try to remove divs that aren't
+ * strictly needed.
+ */
struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
{
int i;
off = isl_dim_total(bmap->dim);
for (i = 0; i < bmap->n_div; ++i) {
if (isl_int_is_zero(bmap->div[i][0]))
- return isl_pip_basic_map_compute_divs(bmap);
+ break;
isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
goto error);
}
- return isl_map_from_basic_map(bmap);
+ if (i == bmap->n_div)
+ return isl_map_from_basic_map(bmap);
+ bmap = isl_basic_map_drop_redundant_divs(bmap);
+ if (!bmap)
+ goto error;
+ for (i = 0; i < bmap->n_div; ++i)
+ if (isl_int_is_zero(bmap->div[i][0]))
+ break;
+ if (i == bmap->n_div)
+ return isl_map_from_basic_map(bmap);
+ struct isl_map *map = compute_divs(bmap);
+ return map;
error:
isl_basic_map_free(bmap);
return NULL;
return map_identity(isl_dim_copy(model->dim));
}
+/* Construct a basic set with all set dimensions having only non-negative
+ * values.
+ */
+struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
+{
+ int i;
+ unsigned nparam;
+ unsigned dim;
+ struct isl_basic_set *bset;
+
+ if (!dims)
+ return NULL;
+ nparam = dims->nparam;
+ dim = dims->n_out;
+ bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
+ if (!bset)
+ return NULL;
+ for (i = 0; i < dim; ++i) {
+ int k = isl_basic_set_alloc_inequality(bset);
+ if (k < 0)
+ goto error;
+ isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
+ isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
+ }
+ return bset;
+error:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
{
return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
if (contains)
return 0;
}
+ isl_vec_free(bmap->sample);
+ bmap->sample = NULL;
bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
if (!bset)
return -1;
if (!sample)
return -1;
empty = sample->size == 0;
- if (bmap->sample)
- isl_vec_free(bmap->ctx, bmap->sample);
+ isl_vec_free(bmap->sample);
bmap->sample = sample;
+ if (empty)
+ ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
return empty;
}
bmap->n_div-i);
if (pos == -1)
continue;
- swap_div(bmap, i, pos);
+ isl_basic_map_swap_div(bmap, i, i + pos);
--i;
}
return bmap;
goto error;
}
if (j != i)
- swap_div(dst, i, j);
+ isl_basic_map_swap_div(dst, i, j);
}
return dst;
error:
isl_int_clear(num);
isl_int_clear(den);
isl_basic_map_free(bmap1);
- isl_vec_free(ctx, obj);
+ isl_vec_free(obj);
return cmp;
error:
isl_basic_map_free(bmap1);
return fixed;
}
+static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
+ unsigned pos, isl_int *val)
+{
+ return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
+ pos, val);
+}
+
static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
isl_int *val)
{
/* Check if dimension dim has fixed value and if so and if val is not NULL,
* then return this fixed value in *val.
*/
+int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
+ isl_int *val)
+{
+ return isl_basic_set_fast_has_fixed_var(bset,
+ isl_basic_set_n_param(bset) + dim, val);
+}
+
+/* Check if dimension dim has fixed value and if so and if val is not NULL,
+ * then return this fixed value in *val.
+ */
int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
{
return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
return NULL;
}
+/* Given two set A and B, construct its Cartesian product A x B.
+ */
+struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
+{
+ return (struct isl_set *)isl_map_product((struct isl_map *)set1,
+ (struct isl_map *)set2);
+}
+
uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
{
int i;