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;
}
}
{
struct isl_dim *dim = bmap->dim;
switch (type) {
- 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_param:
+ case isl_dim_in:
+ case isl_dim_out: return isl_dim_size(bmap->dim, type);
case isl_dim_div: return bmap->n_div;
case isl_dim_all: return isl_basic_map_total_dim(bmap);
}
}
+unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
+{
+ return n(map->dim, type);
+}
+
+unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
+{
+ return n(set->dim, type);
+}
+
+unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
+ enum isl_dim_type type)
+{
+ struct isl_dim *dim = bmap->dim;
+ switch (type) {
+ case isl_dim_param: return 1;
+ case isl_dim_in: return 1 + dim->nparam;
+ case isl_dim_out: return 1 + dim->nparam + dim->n_in;
+ case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
+ }
+}
+
+static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
+{
+ return pos(map->dim, type);
+}
+
unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
enum isl_dim_type type)
{
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 NULL;
+ 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;
+ return isl_dim_copy(set->dim);
+}
+
static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
struct isl_basic_map *bmap, unsigned extra,
unsigned n_eq, unsigned n_ineq)
int i;
size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
- bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
+ bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
if (isl_blk_is_error(bmap->block)) {
free(bmap);
return NULL;
}
- bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
- if (!bmap->eq) {
+ bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
+ if (!bmap->ineq) {
isl_blk_free(ctx, bmap->block);
free(bmap);
return NULL;
} else {
bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
if (isl_blk_is_error(bmap->block2)) {
- free(bmap->eq);
+ free(bmap->ineq);
isl_blk_free(ctx, bmap->block);
free(bmap);
return NULL;
bmap->div = isl_alloc_array(ctx, isl_int *, extra);
if (!bmap->div) {
isl_blk_free(ctx, bmap->block2);
- free(bmap->eq);
+ free(bmap->ineq);
isl_blk_free(ctx, bmap->block);
free(bmap);
return NULL;
}
}
- for (i = 0; i < n_eq + n_ineq; ++i)
- bmap->eq[i] = bmap->block.data + i * row_size;
+ for (i = 0; i < n_ineq + n_eq; ++i)
+ bmap->ineq[i] = bmap->block.data + i * row_size;
for (i = 0; i < extra; ++i)
bmap->div[i] = bmap->block2.data + i * (1 + row_size);
bmap->ref = 1;
bmap->flags = 0;
bmap->c_size = n_eq + n_ineq;
- bmap->ineq = bmap->eq + n_eq;
+ bmap->eq = bmap->ineq + n_ineq;
bmap->extra = extra;
bmap->n_eq = 0;
bmap->n_ineq = 0;
int j = isl_basic_map_alloc_div(dst);
isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
}
- F_SET(dst, ISL_BASIC_SET_FINAL);
+ ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
}
struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
bmap->n_div, bmap->n_eq, bmap->n_ineq);
if (!dup)
return NULL;
- dup->flags = bmap->flags;
dup_constraints(dup, bmap);
- dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
+ dup->flags = bmap->flags;
+ dup->sample = isl_vec_copy(bmap->sample);
return dup;
}
if (!bset)
return NULL;
- if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
+ if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
bset->ref++;
return bset;
}
if (!bmap)
return NULL;
- if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
+ if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
bmap->ref++;
return bmap;
}
isl_ctx_deref(bmap->ctx);
free(bmap->div);
isl_blk_free(bmap->ctx, bmap->block2);
- free(bmap->eq);
+ 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);
}
isl_basic_map_free((struct isl_basic_map *)bset);
}
+static int room_for_con(struct isl_basic_map *bmap, unsigned n)
+{
+ return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
+}
+
int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
{
struct isl_ctx *ctx;
if (!bmap)
return -1;
ctx = bmap->ctx;
- isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
- isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
- if (bmap->eq + bmap->n_eq == bmap->ineq) {
+ isl_assert(ctx, room_for_con(bmap, 1), return -1);
+ isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
+ return -1);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
+ if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
isl_int *t;
int j = isl_basic_map_alloc_inequality(bmap);
if (j < 0)
return -1;
- t = bmap->ineq[0];
- bmap->ineq[0] = bmap->ineq[j];
- bmap->ineq[j] = t;
+ t = bmap->ineq[j];
+ bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
+ bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
+ bmap->eq[-1] = t;
+ bmap->n_eq++;
bmap->n_ineq--;
- bmap->ineq++;
- } else
- isl_seq_clr(bmap->eq[bmap->n_eq] +
- 1 + isl_basic_map_total_dim(bmap),
+ bmap->eq--;
+ return 0;
+ }
+ isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
bmap->extra - bmap->n_div);
return bmap->n_eq++;
}
return 0;
}
+int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
+{
+ return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
+}
+
int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
{
isl_int *t;
isl_int *t;
t = bmap->ineq[pos];
- bmap->ineq[pos] = bmap->ineq[0];
- bmap->ineq[0] = bmap->eq[bmap->n_eq];
- bmap->eq[bmap->n_eq] = t;
+ bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
+ bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
+ bmap->eq[-1] = t;
bmap->n_eq++;
bmap->n_ineq--;
- bmap->ineq++;
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ bmap->eq--;
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
+}
+
+static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
+{
+ return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
}
int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
if (!bmap)
return -1;
ctx = bmap->ctx;
- isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
- return -1);
- F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
- F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1 + isl_basic_map_total_dim(bmap),
bmap->extra - bmap->n_div);
t = bmap->ineq[pos];
bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
bmap->ineq[bmap->n_ineq - 1] = t;
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
}
bmap->n_ineq--;
return 0;
isl_seq_clr(bmap->div[bmap->n_div] +
1 + 1 + isl_basic_map_total_dim(bmap),
bmap->extra - bmap->n_div);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
return bmap->n_div++;
}
+int isl_basic_set_alloc_div(struct isl_basic_set *bset)
+{
+ return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
+}
+
int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
{
if (!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
return NULL;
}
-static struct isl_basic_set *set_add_constraints(struct isl_basic_set *bset1,
+struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
struct isl_basic_set *bset2, unsigned pos)
{
return (struct isl_basic_set *)
if (!dim)
goto error;
- base = isl_basic_map_cow(base);
if (!base)
goto error;
dims_ok = isl_dim_equal(base->dim, dim) &&
base->extra >= base->n_div + extra;
- if (dims_ok && n_eq == 0 && n_ineq == 0) {
+ if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
+ room_for_ineq(base, n_ineq)) {
isl_dim_free(dim);
return base;
}
ext = add_constraints(ext, base, 0, 0);
if (ext) {
ext->flags = flags;
- F_CLR(ext, ISL_BASIC_SET_FINAL);
+ ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
}
return ext;
return NULL;
}
+struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
+ struct isl_dim *dim, unsigned extra,
+ unsigned n_eq, unsigned n_ineq)
+{
+ return (struct isl_basic_set *)
+ isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
+ extra, n_eq, n_ineq);
+}
+
struct isl_basic_map *isl_basic_map_extend_constraints(
struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
{
bmap->ref--;
bmap = isl_basic_map_dup(bmap);
}
- F_CLR(bmap, ISL_BASIC_SET_FINAL);
+ ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
return bmap;
}
isl_blk_free(bset->ctx, blk);
- F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
+ ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
return bset;
error:
return NULL;
}
}
- F_CLR(set, ISL_SET_NORMALIZED);
+ ISL_F_CLR(set, ISL_SET_NORMALIZED);
return set;
}
-static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
+struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
{
- isl_seq_cpy(c, c + n, rem);
- isl_seq_clr(c + rem, n);
+ int i = 0;
+ unsigned total;
+ if (!bmap)
+ goto error;
+ total = isl_basic_map_total_dim(bmap);
+ isl_basic_map_free_div(bmap, bmap->n_div);
+ isl_basic_map_free_inequality(bmap, bmap->n_ineq);
+ if (bmap->n_eq > 0)
+ isl_basic_map_free_equality(bmap, bmap->n_eq-1);
+ else {
+ isl_basic_map_alloc_equality(bmap);
+ if (i < 0)
+ goto error;
+ }
+ isl_int_set_si(bmap->eq[i][0], 1);
+ isl_seq_clr(bmap->eq[i]+1, total);
+ ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
}
-/* Drop n dimensions starting at first.
- *
- * In principle, this frees up some extra variables as the number
- * of columns remains constant, but we would have to extend
- * the div array too as the number of rows in this array is assumed
- * to be equal to extra.
- */
-struct isl_basic_set *isl_basic_set_drop_dims(
- struct isl_basic_set *bset, unsigned first, unsigned n)
+struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
{
- int i;
-
- if (!bset)
- goto error;
-
- isl_assert(bset->ctx, first + n <= bset->dim->n_out, goto error);
+ return (struct isl_basic_set *)
+ isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
+}
- if (n == 0)
- return bset;
+void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
+{
+ int i;
+ unsigned off = isl_dim_total(bmap->dim);
+ isl_int *t = bmap->div[a];
+ bmap->div[a] = bmap->div[b];
+ bmap->div[b] = t;
- bset = isl_basic_set_cow(bset);
- if (!bset)
- return NULL;
+ for (i = 0; i < bmap->n_eq; ++i)
+ isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
- for (i = 0; i < bset->n_eq; ++i)
- constraint_drop_vars(bset->eq[i]+1+bset->dim->nparam+first, n,
- (bset->dim->n_out-first-n)+bset->extra);
+ for (i = 0; i < bmap->n_ineq; ++i)
+ isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
- for (i = 0; i < bset->n_ineq; ++i)
- constraint_drop_vars(bset->ineq[i]+1+bset->dim->nparam+first, n,
- (bset->dim->n_out-first-n)+bset->extra);
+ for (i = 0; i < bmap->n_div; ++i)
+ isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+}
- for (i = 0; i < bset->n_div; ++i)
- constraint_drop_vars(bset->div[i]+1+1+bset->dim->nparam+first, n,
- (bset->dim->n_out-first-n)+bset->extra);
+/* Eliminate the specified n dimensions starting at first from the
+ * constraints using Fourier-Motzkin, The dimensions themselves
+ * are not removed.
+ */
+struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
+ unsigned first, unsigned n)
+{
+ int i;
+ unsigned nparam;
- bset->dim = isl_dim_drop_outputs(bset->dim, first, n);
- if (!bset->dim)
- goto error;
+ if (!set)
+ return NULL;
+ if (n == 0)
+ return set;
- F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
- bset = isl_basic_set_simplify(bset);
- return isl_basic_set_finalize(bset);
+ set = isl_set_cow(set);
+ if (!set)
+ return NULL;
+ isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
+ nparam = isl_set_n_param(set);
+
+ for (i = 0; i < set->n; ++i) {
+ set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
+ nparam + first, n);
+ if (!set->p[i])
+ goto error;
+ }
+ return set;
error:
- isl_basic_set_free(bset);
+ isl_set_free(set);
return NULL;
}
-static struct isl_set *isl_set_drop_dims(
- struct isl_set *set, unsigned first, unsigned n)
+/* Project out n dimensions starting at first using Fourier-Motzkin */
+struct isl_set *isl_set_remove_dims(struct isl_set *set,
+ unsigned first, unsigned n)
{
- int i;
+ set = isl_set_eliminate_dims(set, first, n);
+ set = isl_set_drop_dims(set, first, n);
+ return set;
+}
- if (!set)
- goto error;
+struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
+{
+ bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
+ bset->n_div);
+ if (!bset)
+ return NULL;
+ bset->n_div = 0;
+ return bset;
+}
- isl_assert(set->ctx, first + n <= set->dim->n_out, goto error);
+struct isl_set *isl_set_remove_divs(struct isl_set *set)
+{
+ int i;
- if (n == 0)
+ if (!set)
+ return NULL;
+ if (set->n == 0)
return set;
+
set = isl_set_cow(set);
if (!set)
- goto error;
- set->dim = isl_dim_drop_outputs(set->dim, first, n);
- if (!set->dim)
- goto error;
-
+ return NULL;
+
for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_drop_dims(set->p[i], first, n);
+ set->p[i] = isl_basic_set_remove_divs(set->p[i]);
if (!set->p[i])
goto error;
}
-
- F_CLR(set, ISL_SET_NORMALIZED);
return set;
error:
isl_set_free(set);
return NULL;
}
-/* Drop n input dimensions starting at first.
- *
- * In principle, this frees up some extra variables as the number
- * of columns remains constant, but we would have to extend
- * the div array too as the number of rows in this array is assumed
- * to be equal to extra.
- */
-struct isl_basic_map *isl_basic_map_drop_inputs(
- struct isl_basic_map *bmap, unsigned first, unsigned n)
+struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned first, unsigned n)
{
- int i;
- unsigned nparam;
- unsigned n_in;
- unsigned n_out;
-
if (!bmap)
- goto error;
-
- nparam = isl_basic_map_n_param(bmap);
- n_in = isl_basic_map_n_in(bmap);
- n_out = isl_basic_map_n_out(bmap);
- isl_assert(bmap->ctx, first + n <= n_in, goto error);
-
+ return NULL;
+ isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
+ goto error);
if (n == 0)
return bmap;
-
- bmap = isl_basic_map_cow(bmap);
- if (!bmap)
- return NULL;
-
- for (i = 0; i < bmap->n_eq; ++i)
- constraint_drop_vars(bmap->eq[i]+1+nparam+first, n,
- (n_in-first-n)+n_out+bmap->extra);
-
- for (i = 0; i < bmap->n_ineq; ++i)
- constraint_drop_vars(bmap->ineq[i]+1+nparam+first, n,
- (n_in-first-n)+n_out+bmap->extra);
-
- for (i = 0; i < bmap->n_div; ++i)
- constraint_drop_vars(bmap->div[i]+1+1+nparam+first, n,
- (n_in-first-n)+n_out+bmap->extra);
-
- bmap->dim = isl_dim_drop_inputs(bmap->dim, first, n);
- if (!bmap->dim)
- goto error;
-
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- bmap = isl_basic_map_simplify(bmap);
- return isl_basic_map_finalize(bmap);
+ bmap = isl_basic_map_eliminate_vars(bmap,
+ isl_basic_map_offset(bmap, type) - 1 + first, n);
+ bmap = isl_basic_map_drop(bmap, type, first, n);
+ return bmap;
error:
isl_basic_map_free(bmap);
return NULL;
}
-static struct isl_map *isl_map_drop_inputs(
- struct isl_map *map, unsigned first, unsigned n)
+struct isl_map *isl_map_remove(struct isl_map *map,
+ enum isl_dim_type type, unsigned first, unsigned n)
{
int i;
-
- if (!map)
- goto error;
-
- isl_assert(map->ctx, first + n <= map->dim->n_in, goto error);
+ unsigned nparam;
if (n == 0)
return map;
+
map = isl_map_cow(map);
if (!map)
- goto error;
- map->dim = isl_dim_drop_inputs(map->dim, first, n);
- if (!map->dim)
- goto error;
-
+ return NULL;
+ isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
+
for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
+ map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
+ isl_basic_map_offset(map->p[i], type) - 1 + first, n);
if (!map->p[i])
goto error;
}
- F_CLR(map, ISL_MAP_NORMALIZED);
-
+ map = isl_map_drop(map, type, first, n);
return map;
error:
isl_map_free(map);
return NULL;
}
-/*
- * We don't cow, as the div is assumed to be redundant.
- */
-static struct isl_basic_map *isl_basic_map_drop_div(
- struct isl_basic_map *bmap, unsigned div)
+/* Project out n inputs starting at first using Fourier-Motzkin */
+struct isl_map *isl_map_remove_inputs(struct isl_map *map,
+ unsigned first, unsigned n)
{
- int i;
- unsigned pos;
-
- if (!bmap)
- goto error;
-
- pos = 1 + isl_dim_total(bmap->dim) + div;
+ return isl_map_remove(map, isl_dim_in, first, n);
+}
- isl_assert(bmap->ctx, div < bmap->n_div, goto error);
-
- for (i = 0; i < bmap->n_eq; ++i)
- constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
-
- for (i = 0; i < bmap->n_ineq; ++i) {
- if (!isl_int_is_zero(bmap->ineq[i][pos])) {
- isl_basic_map_drop_inequality(bmap, i);
- --i;
- continue;
- }
- constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
- }
-
- for (i = 0; i < bmap->n_div; ++i)
- constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
-
- if (div != bmap->n_div - 1) {
- int j;
- isl_int *t = bmap->div[div];
-
- for (j = div; j < bmap->n_div - 1; ++j)
- bmap->div[j] = bmap->div[j+1];
-
- bmap->div[bmap->n_div - 1] = t;
- }
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- isl_basic_map_free_div(bmap, 1);
-
- return bmap;
-error:
- isl_basic_map_free(bmap);
- return NULL;
-}
-
-static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
-{
- int i;
- unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
- unsigned len;
- len = 1 + isl_basic_map_total_dim(bmap);
-
- for (i = 0; i < bmap->n_eq; ++i)
- if (bmap->eq[i] != eq)
- isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
-
- for (i = 0; i < bmap->n_ineq; ++i)
- isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
-
- /* We need to be careful about circular definitions,
- * so for now we just remove the definitions of other divs that
- * depend on this div and (possibly) recompute them later.
- */
- for (i = 0; i < bmap->n_div; ++i)
- if (!isl_int_is_zero(bmap->div[i][0]) &&
- !isl_int_is_zero(bmap->div[i][1 + pos]))
- isl_seq_clr(bmap->div[i], 1 + len);
-
- isl_basic_map_drop_div(bmap, div);
-}
-
-struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
-{
- int i = 0;
- unsigned total;
- if (!bmap)
- goto error;
- total = isl_basic_map_total_dim(bmap);
- isl_basic_map_free_div(bmap, bmap->n_div);
- isl_basic_map_free_inequality(bmap, bmap->n_ineq);
- if (bmap->n_eq > 0)
- isl_basic_map_free_equality(bmap, bmap->n_eq-1);
- else {
- isl_basic_map_alloc_equality(bmap);
- if (i < 0)
- goto error;
- }
- isl_int_set_si(bmap->eq[i][0], 1);
- isl_seq_clr(bmap->eq[i]+1, total);
- F_SET(bmap, ISL_BASIC_MAP_EMPTY);
- return isl_basic_map_finalize(bmap);
-error:
- isl_basic_map_free(bmap);
- return NULL;
-}
-
-struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
-{
- return (struct isl_basic_set *)
- isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
-}
-
-static void swap_equality(struct isl_basic_map *bmap, int a, int b)
-{
- isl_int *t = bmap->eq[a];
- bmap->eq[a] = bmap->eq[b];
- bmap->eq[b] = t;
-}
-
-static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
-{
- if (a != b) {
- isl_int *t = bmap->ineq[a];
- bmap->ineq[a] = bmap->ineq[b];
- bmap->ineq[b] = t;
- }
-}
-
-static void set_swap_inequality(struct isl_basic_set *bset, int a, int b)
-{
- swap_inequality((struct isl_basic_map *)bset, a, b);
-}
-
-static void swap_div(struct isl_basic_map *bmap, int a, int b)
-{
- int i;
- unsigned off = isl_dim_total(bmap->dim);
- isl_int *t = bmap->div[a];
- bmap->div[a] = bmap->div[b];
- bmap->div[b] = t;
-
- for (i = 0; i < bmap->n_eq; ++i)
- isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
-
- for (i = 0; i < bmap->n_ineq; ++i)
- isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
-
- for (i = 0; i < bmap->n_div; ++i)
- isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
-}
-
-static void eliminate_var_using_equality(struct isl_basic_map *bmap,
- unsigned pos, isl_int *eq, int *progress)
-{
- unsigned total;
- int k;
-
- total = isl_basic_map_total_dim(bmap);
- for (k = 0; k < bmap->n_eq; ++k) {
- if (bmap->eq[k] == eq)
- continue;
- if (isl_int_is_zero(bmap->eq[k][1+pos]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->eq[k], eq, 1+pos, 1+total, NULL);
- }
-
- for (k = 0; k < bmap->n_ineq; ++k) {
- if (isl_int_is_zero(bmap->ineq[k][1+pos]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->ineq[k], eq, 1+pos, 1+total, NULL);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- }
-
- for (k = 0; k < bmap->n_div; ++k) {
- if (isl_int_is_zero(bmap->div[k][0]))
- continue;
- if (isl_int_is_zero(bmap->div[k][1+1+pos]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->div[k]+1, eq,
- 1+pos, 1+total, &bmap->div[k][0]);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- }
-}
-
-
-struct isl_basic_map *isl_basic_map_gauss(
- struct isl_basic_map *bmap, int *progress)
-{
- int k;
- int done;
- int last_var;
- unsigned total_var;
- unsigned total;
-
- if (!bmap)
- return NULL;
-
- total = isl_basic_map_total_dim(bmap);
- total_var = total - bmap->n_div;
-
- last_var = total - 1;
- for (done = 0; done < bmap->n_eq; ++done) {
- for (; last_var >= 0; --last_var) {
- for (k = done; k < bmap->n_eq; ++k)
- if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
- break;
- if (k < bmap->n_eq)
- break;
- }
- if (last_var < 0)
- break;
- if (k != done)
- swap_equality(bmap, k, done);
- if (isl_int_is_neg(bmap->eq[done][1+last_var]))
- isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
-
- eliminate_var_using_equality(bmap, last_var, bmap->eq[done],
- progress);
-
- if (last_var >= total_var &&
- isl_int_is_zero(bmap->div[last_var - total_var][0])) {
- unsigned div = last_var - total_var;
- isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
- 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]);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- }
- }
- if (done == bmap->n_eq)
- return bmap;
- for (k = done; k < bmap->n_eq; ++k) {
- if (isl_int_is_zero(bmap->eq[k][0]))
- continue;
- return isl_basic_map_set_to_empty(bmap);
- }
- isl_basic_map_free_equality(bmap, bmap->n_eq-done);
- return bmap;
-}
-
-struct isl_basic_set *isl_basic_set_gauss(
- struct isl_basic_set *bset, int *progress)
-{
- return (struct isl_basic_set*)isl_basic_map_gauss(
- (struct isl_basic_map *)bset, progress);
-}
-
-static unsigned int round_up(unsigned int v)
-{
- int old_v = v;
-
- while (v) {
- old_v = v;
- v ^= v & -v;
- }
- return old_v << 1;
-}
-
-static int hash_index(isl_int ***index, unsigned int size, int bits,
- struct isl_basic_map *bmap, int k)
-{
- int h;
- unsigned total = isl_basic_map_total_dim(bmap);
- uint32_t hash = isl_seq_get_hash_bits(bmap->ineq[k]+1, total, bits);
- for (h = hash; index[h]; h = (h+1) % size)
- if (&bmap->ineq[k] != index[h] &&
- isl_seq_eq(bmap->ineq[k]+1, index[h][0]+1, total))
- break;
- return h;
-}
-
-static int set_hash_index(isl_int ***index, unsigned int size, int bits,
- struct isl_basic_set *bset, int k)
-{
- return hash_index(index, size, bits, (struct isl_basic_map *)bset, k);
-}
-
-static struct isl_basic_map *remove_duplicate_constraints(
- struct isl_basic_map *bmap, int *progress)
-{
- unsigned int size;
- isl_int ***index;
- int k, l, h;
- int bits;
- unsigned total = isl_basic_map_total_dim(bmap);
- isl_int sum;
-
- if (bmap->n_ineq <= 1)
- return bmap;
-
- size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
- bits = ffs(size) - 1;
- index = isl_calloc_array(ctx, isl_int **, size);
- if (!index)
- return bmap;
-
- index[isl_seq_get_hash_bits(bmap->ineq[0]+1, total, bits)] = &bmap->ineq[0];
- for (k = 1; k < bmap->n_ineq; ++k) {
- h = hash_index(index, size, bits, bmap, k);
- if (!index[h]) {
- index[h] = &bmap->ineq[k];
- continue;
- }
- if (progress)
- *progress = 1;
- l = index[h] - &bmap->ineq[0];
- if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
- swap_inequality(bmap, k, l);
- isl_basic_map_drop_inequality(bmap, k);
- --k;
- }
- isl_int_init(sum);
- for (k = 0; k < bmap->n_ineq-1; ++k) {
- isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
- h = hash_index(index, size, bits, bmap, k);
- isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
- if (!index[h])
- continue;
- l = index[h] - &bmap->ineq[0];
- isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
- if (isl_int_is_pos(sum))
- continue;
- if (isl_int_is_zero(sum)) {
- /* We need to break out of the loop after these
- * changes since the contents of the hash
- * will no longer be valid.
- * Plus, we probably we want to regauss first.
- */
- isl_basic_map_drop_inequality(bmap, l);
- isl_basic_map_inequality_to_equality(bmap, k);
- } else
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_int_clear(sum);
-
- free(index);
- return bmap;
-}
-
-static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
-{
- int d, i;
- unsigned total;
-
- total = isl_dim_total(bmap->dim);
- for (d = 0; d < total; ++d)
- elim[d] = -1;
- for (d = total - 1, i = 0; d >= 0 && i < bmap->n_eq; ++i) {
- for (; d >= 0; --d) {
- if (isl_int_is_zero(bmap->eq[i][1+d]))
- continue;
- elim[d] = i;
- break;
- }
- }
-}
-
-static void set_compute_elimination_index(struct isl_basic_set *bset, int *elim)
-{
- return compute_elimination_index((struct isl_basic_map *)bset, elim);
-}
-
-static int reduced_using_equalities(isl_int *dst, isl_int *src,
- struct isl_basic_map *bmap, int *elim)
-{
- int d, i;
- int copied = 0;
- unsigned total;
-
- total = isl_dim_total(bmap->dim);
- for (d = total - 1; d >= 0; --d) {
- if (isl_int_is_zero(src[1+d]))
- continue;
- if (elim[d] == -1)
- continue;
- if (!copied) {
- isl_seq_cpy(dst, src, 1 + total);
- copied = 1;
- }
- isl_seq_elim(dst, bmap->eq[elim[d]], 1 + d, 1 + total, NULL);
- }
- return copied;
-}
-
-static int set_reduced_using_equalities(isl_int *dst, isl_int *src,
- struct isl_basic_set *bset, int *elim)
-{
- return reduced_using_equalities(dst, src,
- (struct isl_basic_map *)bset, elim);
-}
-
-/* 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
- * basic map and check if we get a contradiction.
- */
-int isl_basic_map_fast_is_disjoint(struct isl_basic_map *bmap1,
- struct isl_basic_map *bmap2)
-{
- struct isl_vec *v = NULL;
- int *elim = NULL;
- unsigned total;
- int d, i;
-
- if (!bmap1 || !bmap2)
- return -1;
- isl_assert(bmap1->ctx, isl_dim_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);
- if (total == 0)
- return 0;
- v = isl_vec_alloc(bmap1->ctx, 1 + total);
- if (!v)
- goto error;
- elim = isl_alloc_array(bmap1->ctx, int, total);
- if (!elim)
- goto error;
- compute_elimination_index(bmap1, elim);
- for (i = 0; i < bmap2->n_eq; ++i) {
- int reduced;
- reduced = reduced_using_equalities(v->block.data, bmap2->eq[i],
- bmap1, elim);
- if (reduced && !isl_int_is_zero(v->block.data[0]) &&
- isl_seq_first_non_zero(v->block.data + 1, total) == -1)
- goto disjoint;
- }
- for (i = 0; i < bmap2->n_ineq; ++i) {
- int reduced;
- reduced = reduced_using_equalities(v->block.data,
- bmap2->ineq[i], bmap1, elim);
- if (reduced && isl_int_is_neg(v->block.data[0]) &&
- isl_seq_first_non_zero(v->block.data + 1, total) == -1)
- goto disjoint;
- }
- compute_elimination_index(bmap2, elim);
- for (i = 0; i < bmap1->n_ineq; ++i) {
- int reduced;
- reduced = reduced_using_equalities(v->block.data,
- bmap1->ineq[i], bmap2, elim);
- if (reduced && isl_int_is_neg(v->block.data[0]) &&
- isl_seq_first_non_zero(v->block.data + 1, total) == -1)
- goto disjoint;
- }
- isl_vec_free(bmap1->ctx, v);
- free(elim);
- return 0;
-disjoint:
- isl_vec_free(bmap1->ctx, v);
- free(elim);
- return 1;
-error:
- isl_vec_free(bmap1->ctx, v);
- free(elim);
- return -1;
-}
-
-int isl_basic_set_fast_is_disjoint(struct isl_basic_set *bset1,
- struct isl_basic_set *bset2)
-{
- return isl_basic_map_fast_is_disjoint((struct isl_basic_map *)bset1,
- (struct isl_basic_map *)bset2);
-}
-
-int isl_map_fast_is_disjoint(struct isl_map *map1, struct isl_map *map2)
-{
- int i, j;
-
- if (!map1 || !map2)
- return -1;
-
- if (isl_map_fast_is_equal(map1, map2))
- return 0;
-
- for (i = 0; i < map1->n; ++i) {
- for (j = 0; j < map2->n; ++j) {
- int d = isl_basic_map_fast_is_disjoint(map1->p[i],
- map2->p[j]);
- if (d != 1)
- return d;
- }
- }
- return 1;
-}
-
-int isl_set_fast_is_disjoint(struct isl_set *set1, struct isl_set *set2)
-{
- return isl_map_fast_is_disjoint((struct isl_map *)set1,
- (struct isl_map *)set2);
-}
-
-static struct isl_basic_map *remove_duplicate_divs(
- struct isl_basic_map *bmap, int *progress)
-{
- unsigned int size;
- int *index;
- int k, l, h;
- int bits;
- struct isl_blk eq;
- unsigned total_var = isl_dim_total(bmap->dim);
- unsigned total = total_var + bmap->n_div;
- struct isl_ctx *ctx;
-
- if (bmap->n_div <= 1)
- return bmap;
-
- ctx = bmap->ctx;
- for (k = bmap->n_div - 1; k >= 0; --k)
- if (!isl_int_is_zero(bmap->div[k][0]))
- break;
- if (k <= 0)
- return bmap;
-
- size = round_up(4 * bmap->n_div / 3 - 1);
- bits = ffs(size) - 1;
- index = isl_calloc_array(ctx, int, size);
- if (!index)
- return bmap;
- eq = isl_blk_alloc(ctx, 1+total);
- if (isl_blk_is_error(eq))
- goto out;
-
- isl_seq_clr(eq.data, 1+total);
- index[isl_seq_get_hash_bits(bmap->div[k], 2+total, bits)] = k + 1;
- for (--k; k >= 0; --k) {
- uint32_t hash;
-
- if (isl_int_is_zero(bmap->div[k][0]))
- continue;
-
- hash = isl_seq_get_hash_bits(bmap->div[k], 2+total, bits);
- for (h = hash; index[h]; h = (h+1) % size)
- if (isl_seq_eq(bmap->div[k],
- bmap->div[index[h]-1], 2+total))
- break;
- if (index[h]) {
- *progress = 1;
- l = index[h] - 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);
- isl_int_set_si(eq.data[1+total_var+k], 0);
- isl_int_set_si(eq.data[1+total_var+l], 0);
- }
- index[h] = k+1;
- }
-
- isl_blk_free(ctx, eq);
-out:
- free(index);
- return bmap;
-}
-
-/* Elimininate divs based on equalities
- */
-static struct isl_basic_map *eliminate_divs_eq(
- struct isl_basic_map *bmap, int *progress)
-{
- int d;
- int i;
- int modified = 0;
- unsigned off;
-
- if (!bmap)
- return NULL;
-
- off = 1 + isl_dim_total(bmap->dim);
-
- for (d = bmap->n_div - 1; d >= 0 ; --d) {
- for (i = 0; i < bmap->n_eq; ++i) {
- if (!isl_int_is_one(bmap->eq[i][off + d]) &&
- !isl_int_is_negone(bmap->eq[i][off + d]))
- continue;
- modified = 1;
- *progress = 1;
- eliminate_div(bmap, bmap->eq[i], d);
- isl_basic_map_drop_equality(bmap, i);
- break;
- }
- }
- if (modified)
- return eliminate_divs_eq(bmap, progress);
- return bmap;
-}
-
-static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
-{
- int i;
- isl_int gcd;
- unsigned total = isl_basic_map_total_dim(bmap);
-
- isl_int_init(gcd);
- for (i = bmap->n_eq - 1; i >= 0; --i) {
- isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
- if (isl_int_is_zero(gcd)) {
- if (!isl_int_is_zero(bmap->eq[i][0])) {
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_basic_map_drop_equality(bmap, i);
- continue;
- }
- if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
- isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
- if (isl_int_is_one(gcd))
- continue;
- if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
- }
-
- for (i = bmap->n_ineq - 1; i >= 0; --i) {
- isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
- if (isl_int_is_zero(gcd)) {
- if (isl_int_is_neg(bmap->ineq[i][0])) {
- bmap = isl_basic_map_set_to_empty(bmap);
- break;
- }
- isl_basic_map_drop_inequality(bmap, i);
- continue;
- }
- if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
- isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
- if (isl_int_is_one(gcd))
- continue;
- isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
- isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
- }
- isl_int_clear(gcd);
-
- return bmap;
-}
-
-
-/* Remove any div that is defined in terms of the given variable.
- */
-static struct isl_basic_map *remove_dependent_vars(struct isl_basic_map *bmap,
- int pos)
-{
- int i;
- unsigned dim = isl_dim_total(bmap->dim);
-
- for (i = 0; i < bmap->n_div; ++i) {
- if (isl_int_is_zero(bmap->div[i][0]))
- continue;
- if (isl_int_is_zero(bmap->div[i][1+1+pos]))
- continue;
- bmap = isl_basic_map_eliminate_vars(bmap, dim + i, 1);
- if (!bmap)
- return NULL;
- }
- return bmap;
-}
-
-
-/* Eliminate the specified variables from the constraints using
- * Fourier-Motzkin. The variables themselves are not removed.
- */
-struct isl_basic_map *isl_basic_map_eliminate_vars(
- struct isl_basic_map *bmap, unsigned pos, unsigned n)
-{
- int d;
- int i, j, k;
- unsigned total;
-
- if (n == 0)
- return bmap;
- if (!bmap)
- return NULL;
- total = isl_basic_map_total_dim(bmap);
-
- bmap = isl_basic_map_cow(bmap);
- for (d = pos + n - 1; d >= 0 && d >= pos; --d) {
- int n_lower, n_upper;
- bmap = remove_dependent_vars(bmap, d);
- if (!bmap)
- return NULL;
- if (d >= total - bmap->n_div)
- isl_seq_clr(bmap->div[d-(total-bmap->n_div)], 2+total);
- for (i = 0; i < bmap->n_eq; ++i) {
- if (isl_int_is_zero(bmap->eq[i][1+d]))
- continue;
- eliminate_var_using_equality(bmap, d, bmap->eq[i], NULL);
- isl_basic_map_drop_equality(bmap, i);
- break;
- }
- if (i < bmap->n_eq)
- continue;
- n_lower = 0;
- n_upper = 0;
- for (i = 0; i < bmap->n_ineq; ++i) {
- if (isl_int_is_pos(bmap->ineq[i][1+d]))
- n_lower++;
- else if (isl_int_is_neg(bmap->ineq[i][1+d]))
- n_upper++;
- }
- bmap = isl_basic_map_extend_constraints(bmap,
- 0, n_lower * n_upper);
- for (i = bmap->n_ineq - 1; i >= 0; --i) {
- int last;
- if (isl_int_is_zero(bmap->ineq[i][1+d]))
- continue;
- last = -1;
- for (j = 0; j < i; ++j) {
- if (isl_int_is_zero(bmap->ineq[j][1+d]))
- continue;
- last = j;
- if (isl_int_sgn(bmap->ineq[i][1+d]) ==
- isl_int_sgn(bmap->ineq[j][1+d]))
- continue;
- k = isl_basic_map_alloc_inequality(bmap);
- if (k < 0)
- goto error;
- isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
- 1+total);
- isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
- 1+d, 1+total, NULL);
- }
- isl_basic_map_drop_inequality(bmap, i);
- i = last + 1;
- }
- if (n_lower > 0 && n_upper > 0) {
- bmap = normalize_constraints(bmap);
- bmap = remove_duplicate_constraints(bmap, NULL);
- bmap = isl_basic_map_gauss(bmap, NULL);
- bmap = isl_basic_map_convex_hull(bmap);
- if (!bmap)
- goto error;
- if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
- break;
- }
- }
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
- return bmap;
-error:
- isl_basic_map_free(bmap);
- return NULL;
-}
-
-struct isl_basic_set *isl_basic_set_eliminate_vars(
- struct isl_basic_set *bset, unsigned pos, unsigned n)
-{
- return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
- (struct isl_basic_map *)bset, pos, n);
-}
-
-/* Eliminate the specified n dimensions starting at first from the
- * constraints using Fourier-Motzkin, The dimensions themselves
- * are not removed.
- */
-struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
- unsigned first, unsigned n)
-{
- int i;
- unsigned nparam;
-
- if (!set)
- return NULL;
- if (n == 0)
- return set;
-
- set = isl_set_cow(set);
- if (!set)
- return NULL;
- isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
- nparam = isl_set_n_param(set);
-
- for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
- nparam + first, n);
- if (!set->p[i])
- goto error;
- }
- return set;
-error:
- isl_set_free(set);
- return NULL;
-}
-
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_set *isl_set_remove_dims(struct isl_set *set,
- unsigned first, unsigned n)
-{
- set = isl_set_eliminate_dims(set, first, n);
- set = isl_set_drop_dims(set, first, n);
- return set;
-}
-
-struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
-{
- bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
- bset->n_div);
- if (!bset)
- return NULL;
- bset->n_div = 0;
- return bset;
-}
-
-struct isl_set *isl_set_remove_divs(struct isl_set *set)
-{
- int i;
-
- if (!set)
- return NULL;
- if (set->n == 0)
- return set;
-
- set = isl_set_cow(set);
- if (!set)
- return NULL;
-
- for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_remove_divs(set->p[i]);
- if (!set->p[i])
- goto error;
- }
- return set;
-error:
- isl_set_free(set);
- return NULL;
-}
-
-/* Project out n inputs starting at first using Fourier-Motzkin */
-struct isl_map *isl_map_remove_inputs(struct isl_map *map,
- unsigned first, unsigned n)
-{
- int i;
- unsigned nparam;
-
- if (n == 0)
- return map;
-
- map = isl_map_cow(map);
- if (!map)
- return NULL;
- nparam = isl_map_n_param(map);
- isl_assert(map->ctx, first+n <= isl_map_n_in(map), goto error);
-
- for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
- nparam + first, n);
- if (!map->p[i])
- goto error;
- }
- map = isl_map_drop_inputs(map, first, n);
- return map;
-error:
- isl_map_free(map);
- return NULL;
-}
-
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
- unsigned first, unsigned n)
-{
- unsigned nparam = isl_basic_set_n_param(bset);
- bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
- bset = isl_basic_set_drop_dims(bset, first, n);
- return bset;
-}
-
-/* Elimininate divs based on inequalities
- */
-static struct isl_basic_map *eliminate_divs_ineq(
- struct isl_basic_map *bmap, int *progress)
-{
- int d;
- int i;
- unsigned off;
- struct isl_ctx *ctx;
-
- if (!bmap)
- return NULL;
-
- ctx = bmap->ctx;
- off = 1 + isl_dim_total(bmap->dim);
-
- for (d = bmap->n_div - 1; d >= 0 ; --d) {
- for (i = 0; i < bmap->n_eq; ++i)
- if (!isl_int_is_zero(bmap->eq[i][off + d]))
- break;
- if (i < bmap->n_eq)
- continue;
- for (i = 0; i < bmap->n_ineq; ++i)
- if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
- break;
- if (i < bmap->n_ineq)
- continue;
- *progress = 1;
- bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
- if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
- break;
- bmap = isl_basic_map_drop_div(bmap, d);
- if (!bmap)
- break;
- }
- return bmap;
-}
-
-struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
-{
- int progress = 1;
- if (!bmap)
- return NULL;
- while (progress) {
- progress = 0;
- bmap = normalize_constraints(bmap);
- bmap = eliminate_divs_eq(bmap, &progress);
- bmap = eliminate_divs_ineq(bmap, &progress);
- bmap = isl_basic_map_gauss(bmap, &progress);
- bmap = remove_duplicate_divs(bmap, &progress);
- bmap = remove_duplicate_constraints(bmap, &progress);
- }
- return bmap;
-}
-
-struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
-{
- return (struct isl_basic_set *)
- isl_basic_map_simplify((struct isl_basic_map *)bset);
-}
+/* Project out n dimensions starting at first using Fourier-Motzkin */
+struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
+ unsigned first, unsigned n)
+{
+ unsigned nparam = isl_basic_set_n_param(bset);
+ bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
+ bset = isl_basic_set_drop_dims(bset, first, n);
+ return bset;
+}
static void dump_term(struct isl_basic_map *bmap,
isl_int c, int pos, FILE *out)
isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
- F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
return 0;
}
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)
if (!bmap || !bset)
goto error;
- isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
- goto error);
+ isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
+ bset->dim, isl_dim_param), goto error);
+
+ if (isl_dim_size(bset->dim, isl_dim_set) != 0)
+ isl_assert(bset->ctx,
+ isl_basic_map_compatible_domain(bmap, bset), goto error);
+ bmap = isl_basic_map_cow(bmap);
bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
bset->n_div, bset->n_eq, bset->n_ineq);
if (!bmap)
if (!bmap || !bset)
goto error;
- isl_assert(bset->ctx, isl_basic_map_compatible_range(bmap, bset),
- goto error);
+ isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
+ bset->dim, isl_dim_param), goto error);
+
+ if (isl_dim_size(bset->dim, isl_dim_set) != 0)
+ isl_assert(bset->ctx,
+ isl_basic_map_compatible_range(bmap, bset), goto error);
+ bmap = isl_basic_map_cow(bmap);
bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
bset->n_div, bset->n_eq, bset->n_ineq);
if (!bmap)
return NULL;
}
+static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
+{
+ int i;
+ unsigned total;
+ isl_int s;
+
+ total = 1 + isl_basic_map_total_dim(bmap);
+ if (total != vec->size)
+ return -1;
+
+ isl_int_init(s);
+
+ for (i = 0; i < bmap->n_eq; ++i) {
+ 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->el, bmap->ineq[i], total, &s);
+ if (isl_int_is_neg(s)) {
+ isl_int_clear(s);
+ return 0;
+ }
+ }
+
+ isl_int_clear(s);
+
+ return 1;
+}
+
+int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
+{
+ return basic_map_contains((struct isl_basic_map *)bset, vec);
+}
+
struct isl_basic_map *isl_basic_map_intersect(
struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
{
+ struct isl_vec *sample = NULL;
+
if (!bmap1 || !bmap2)
goto error;
- isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
-
+ isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
+ bmap2->dim, isl_dim_param), goto error);
+ if (isl_dim_total(bmap1->dim) ==
+ isl_dim_size(bmap1->dim, isl_dim_param) &&
+ isl_dim_total(bmap2->dim) !=
+ isl_dim_size(bmap2->dim, isl_dim_param))
+ return isl_basic_map_intersect(bmap2, bmap1);
+
+ if (isl_dim_total(bmap2->dim) !=
+ isl_dim_size(bmap2->dim, isl_dim_param))
+ isl_assert(bmap1->ctx,
+ isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
+
+ if (bmap1->sample &&
+ basic_map_contains(bmap1, bmap1->sample) > 0 &&
+ basic_map_contains(bmap2, bmap1->sample) > 0)
+ 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->sample);
+
+ bmap1 = isl_basic_map_cow(bmap1);
bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
if (!bmap1)
goto error;
bmap1 = add_constraints(bmap1, bmap2, 0, 0);
+ if (sample) {
+ isl_vec_free(bmap1->sample);
+ bmap1->sample = sample;
+ }
+
bmap1 = isl_basic_map_simplify(bmap1);
return isl_basic_map_finalize(bmap1);
error:
+ if (sample)
+ isl_vec_free(sample);
isl_basic_map_free(bmap1);
isl_basic_map_free(bmap2);
return NULL;
if (!map1 || !map2)
goto error;
- if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
- F_ISSET(map2, ISL_MAP_DISJOINT))
- FL_SET(flags, ISL_MAP_DISJOINT);
+ isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
+ map2->dim, isl_dim_param), goto error);
+ if (isl_dim_total(map1->dim) ==
+ isl_dim_size(map1->dim, isl_dim_param) &&
+ isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
+ return isl_map_intersect(map2, map1);
+
+ if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
+ isl_assert(map1->ctx,
+ isl_dim_equal(map1->dim, map2->dim), goto error);
+
+ if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
+ ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
+ ISL_FL_SET(flags, ISL_MAP_DISJOINT);
result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
map1->n * map2->n, flags);
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;
return NULL;
}
-struct isl_basic_set *isl_basic_set_apply(
- struct isl_basic_set *bset, struct isl_basic_map *bmap)
+struct isl_basic_set *isl_basic_set_apply(
+ struct isl_basic_set *bset, struct isl_basic_map *bmap)
+{
+ if (!bset || !bmap)
+ goto error;
+
+ isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
+ goto error);
+
+ return (struct isl_basic_set *)
+ isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
+error:
+ isl_basic_set_free(bset);
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+struct isl_basic_map *isl_basic_map_apply_domain(
+ struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
+{
+ if (!bmap1 || !bmap2)
+ goto error;
+
+ isl_assert(ctx,
+ isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
+ isl_assert(ctx,
+ isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
+ goto error);
+
+ bmap1 = isl_basic_map_reverse(bmap1);
+ bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
+ return isl_basic_map_reverse(bmap1);
+error:
+ isl_basic_map_free(bmap1);
+ isl_basic_map_free(bmap2);
+ return NULL;
+}
+
+/* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
+ * A \cap B -> f(A) + f(B)
+ */
+struct isl_basic_map *isl_basic_map_sum(
+ struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
+{
+ unsigned n_in, n_out, nparam, total, pos;
+ struct isl_basic_map *bmap = NULL;
+ struct isl_dim_map *dim_map1, *dim_map2;
+ int i;
+
+ if (!bmap1 || !bmap2)
+ goto error;
+
+ isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
+ goto error);
+
+ nparam = isl_basic_map_n_param(bmap1);
+ n_in = isl_basic_map_n_in(bmap1);
+ n_out = isl_basic_map_n_out(bmap1);
+
+ total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
+ dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
+ dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
+ isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
+ isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
+
+ bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
+ bmap1->n_div + bmap2->n_div + 2 * n_out,
+ bmap1->n_eq + bmap2->n_eq + n_out,
+ bmap1->n_ineq + bmap2->n_ineq);
+ for (i = 0; i < n_out; ++i) {
+ int j = isl_basic_map_alloc_equality(bmap);
+ if (j < 0)
+ goto error;
+ isl_seq_clr(bmap->eq[j], 1+total);
+ isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
+ isl_int_set_si(bmap->eq[j][1+pos+i], 1);
+ isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
+ }
+ bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
+ bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
+ bmap = add_divs(bmap, 2 * n_out);
+
+ bmap = isl_basic_map_simplify(bmap);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_map_free(bmap);
+ isl_basic_map_free(bmap1);
+ isl_basic_map_free(bmap2);
+ return NULL;
+}
+
+/* Given a basic map A -> f(A), construct A -> -f(A).
+ */
+struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
{
- if (!bset || !bmap)
- goto error;
+ int i, j;
+ unsigned off, n;
- isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
- goto error);
+ bmap = isl_basic_map_cow(bmap);
+ if (!bmap)
+ return NULL;
- return (struct isl_basic_set *)
- isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
-error:
- isl_basic_set_free(bset);
- isl_basic_map_free(bmap);
- return NULL;
+ n = isl_basic_map_dim(bmap, isl_dim_out);
+ off = isl_basic_map_offset(bmap, isl_dim_out);
+ for (i = 0; i < bmap->n_eq; ++i)
+ for (j = 0; j < n; ++j)
+ isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
+ for (i = 0; i < bmap->n_ineq; ++i)
+ for (j = 0; j < n; ++j)
+ isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
+ for (i = 0; i < bmap->n_div; ++i)
+ for (j = 0; j < n; ++j)
+ isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
+ return isl_basic_map_finalize(bmap);
}
-struct isl_basic_map *isl_basic_map_apply_domain(
- struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
+/* Given a basic map A -> f(A) and an integer d, construct a basic map
+ * A -> floor(f(A)/d).
+ */
+struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
+ isl_int d)
{
- if (!bmap1 || !bmap2)
- goto error;
+ unsigned n_in, n_out, nparam, total, pos;
+ struct isl_basic_map *result = NULL;
+ struct isl_dim_map *dim_map;
+ int i;
- isl_assert(ctx,
- isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
- isl_assert(ctx,
- isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
- goto error);
+ if (!bmap)
+ return NULL;
- bmap1 = isl_basic_map_reverse(bmap1);
- bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
- return isl_basic_map_reverse(bmap1);
+ nparam = isl_basic_map_n_param(bmap);
+ n_in = isl_basic_map_n_in(bmap);
+ n_out = isl_basic_map_n_out(bmap);
+
+ total = nparam + n_in + n_out + bmap->n_div + n_out;
+ dim_map = isl_dim_map_alloc(bmap->ctx, total);
+ isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
+ isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
+ isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
+ isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
+
+ result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
+ bmap->n_div + n_out,
+ bmap->n_eq, bmap->n_ineq + 2 * n_out);
+ result = add_constraints_dim_map(result, bmap, dim_map);
+ result = add_divs(result, n_out);
+ for (i = 0; i < n_out; ++i) {
+ int j;
+ j = isl_basic_map_alloc_inequality(result);
+ if (j < 0)
+ goto error;
+ isl_seq_clr(result->ineq[j], 1+total);
+ isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
+ isl_int_set_si(result->ineq[j][1+pos+i], 1);
+ j = isl_basic_map_alloc_inequality(result);
+ if (j < 0)
+ goto error;
+ isl_seq_clr(result->ineq[j], 1+total);
+ isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
+ isl_int_set_si(result->ineq[j][1+pos+i], -1);
+ isl_int_sub_ui(result->ineq[j][0], d, 1);
+ }
+
+ result = isl_basic_map_simplify(result);
+ return isl_basic_map_finalize(result);
error:
- isl_basic_map_free(bmap1);
- isl_basic_map_free(bmap2);
+ isl_basic_map_free(result);
return NULL;
}
-static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
- struct isl_basic_map *bmap, unsigned pos)
+static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
{
int i;
unsigned nparam;
return NULL;
}
-static struct isl_basic_map *var_more(struct isl_ctx *ctx,
- struct isl_basic_map *bmap, unsigned pos)
+static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
{
int i;
unsigned nparam;
return NULL;
}
-static struct isl_basic_map *var_less(struct isl_ctx *ctx,
- struct isl_basic_map *bmap, unsigned pos)
+static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
{
int i;
unsigned nparam;
return NULL;
}
-struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
- unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
+struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
{
int i;
struct isl_basic_map *bmap;
- bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
+ bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
if (!bmap)
return NULL;
for (i = 0; i < n_equal && bmap; ++i)
- bmap = var_equal(ctx, bmap, i);
+ bmap = var_equal(bmap, i);
return isl_basic_map_finalize(bmap);
}
-struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
- unsigned nparam, unsigned in, unsigned out, unsigned pos)
+struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
{
int i;
struct isl_basic_map *bmap;
- bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
+ bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
if (!bmap)
return NULL;
for (i = 0; i < pos && bmap; ++i)
- bmap = var_equal(ctx, bmap, i);
+ bmap = var_equal(bmap, i);
if (bmap)
- bmap = var_less(ctx, bmap, pos);
+ bmap = var_less(bmap, pos);
return isl_basic_map_finalize(bmap);
}
-struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
- unsigned nparam, unsigned in, unsigned out, unsigned pos)
+struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
{
int i;
struct isl_basic_map *bmap;
- bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
+ bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
if (!bmap)
return NULL;
for (i = 0; i < pos && bmap; ++i)
- bmap = var_equal(ctx, bmap, i);
+ bmap = var_equal(bmap, i);
if (bmap)
- bmap = var_more(ctx, bmap, pos);
+ bmap = var_more(bmap, pos);
return isl_basic_map_finalize(bmap);
}
bmap = isl_basic_map_cow(bmap);
if (!bmap)
goto error;
- bmap->dim = isl_dim_cow(bmap->dim);
+ bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
if (!bmap->dim)
goto error;
- bmap->dim->n_out += bmap->dim->nparam + bmap->dim->n_in + bmap->n_div;
- bmap->dim->nparam = 0;
- bmap->dim->n_in = 0;
bmap->extra -= bmap->n_div;
bmap->n_div = 0;
bmap = isl_basic_map_finalize(bmap);
}
bmap = isl_basic_map_extend_constraints(bmap,
0, 2 * like->n_div);
- for (i = 0; i < like->n_div; ++i)
+ for (i = 0; i < like->n_div; ++i) {
+ if (isl_int_is_zero(bmap->div[i][0]))
+ continue;
if (add_div_constraints(bmap, i) < 0)
goto error;
+ }
}
isl_basic_map_free(like);
bmap = isl_basic_map_simplify(bmap);
goto error;
}
if (map->n == 0)
- map->dim->n_out += map->dim->nparam + map->dim->n_in;
- else
- map->dim->n_out = map->p[0]->dim->n_out;
- map->dim->nparam = 0;
- map->dim->n_in = 0;
+ map->dim = isl_dim_underlying(map->dim, 0);
+ else {
+ isl_dim_free(map->dim);
+ map->dim = isl_dim_copy(map->p[0]->dim);
+ }
+ if (!map->dim)
+ goto error;
return (struct isl_set *)map;
error:
isl_map_free(map);
if (!set->p[i])
goto error;
}
- F_CLR(set, ISL_MAP_DISJOINT);
- F_CLR(set, ISL_SET_NORMALIZED);
+ ISL_F_CLR(set, ISL_MAP_DISJOINT);
+ ISL_F_CLR(set, ISL_SET_NORMALIZED);
return set;
error:
isl_map_free(map);
return NULL;
}
+struct isl_map *isl_map_from_range(struct isl_set *set)
+{
+ return (struct isl_map *)set;
+}
+
struct isl_set *isl_set_from_map(struct isl_map *map)
{
int i;
return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
}
-struct isl_map *isl_map_empty(struct isl_ctx *ctx,
- unsigned nparam, unsigned in, unsigned out)
+struct isl_map *isl_map_empty(struct isl_dim *dim)
+{
+ return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
+}
+
+struct isl_map *isl_map_empty_like(struct isl_map *model)
{
- return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
+ if (!model)
+ return NULL;
+ return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
}
struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
return isl_set_empty(isl_dim_copy(model->dim));
}
+struct isl_map *isl_map_universe(struct isl_dim *dim)
+{
+ struct isl_map *map;
+ if (!dim)
+ return NULL;
+ map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
+ map = isl_map_add(map, isl_basic_map_universe(dim));
+ return map;
+}
+
struct isl_set *isl_set_universe(struct isl_dim *dim)
{
struct isl_set *set;
{
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;
map->n++;
- F_CLR(map, ISL_MAP_NORMALIZED);
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
return map;
error:
if (map)
nparam, 0, dim);
}
-static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
- unsigned var, int value)
+static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
+ unsigned pos, int value)
{
int j;
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_int_set_si(bmap->eq[j][1 + var], -1);
+ 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 isl_basic_map_finalize(bmap);
return NULL;
}
-struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
- unsigned input, int value)
+struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned pos, int value)
{
if (!bmap)
return NULL;
- isl_assert(bmap->ctx, input < isl_basic_map_n_in(bmap), goto error);
- return isl_basic_map_fix_var(bmap, isl_basic_map_n_param(bmap) + input,
+ isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
+ return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
value);
error:
isl_basic_map_free(bmap);
return NULL;
}
+struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
+ enum isl_dim_type type, unsigned pos, int value)
+{
+ return (struct isl_basic_set *)
+ isl_basic_map_fix_si((struct isl_basic_map *)bset,
+ type, pos, value);
+}
+
+struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
+ unsigned input, int value)
+{
+ return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
+}
+
struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
unsigned dim, int value)
{
- if (!bset)
- return NULL;
- isl_assert(bset->ctx, dim < isl_basic_set_n_dim(bset), goto error);
return (struct isl_basic_set *)
- isl_basic_map_fix_var((struct isl_basic_map *)bset,
- isl_basic_set_n_param(bset) + dim, value);
+ isl_basic_map_fix_si((struct isl_basic_map *)bset,
+ isl_dim_set, dim, value);
+}
+
+struct isl_map *isl_map_fix_si(struct isl_map *map,
+ enum isl_dim_type type, unsigned pos, int value)
+{
+ int i;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+
+ isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
+ if (!map->p[i])
+ goto error;
+ }
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+ return map;
error:
- isl_basic_set_free(bset);
+ isl_map_free(map);
return NULL;
}
struct isl_map *isl_map_fix_input_si(struct isl_map *map,
unsigned input, int value)
{
+ return isl_map_fix_si(map, isl_dim_in, input, value);
+}
+
+struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
+{
+ return (struct isl_set *)
+ isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
+}
+
+struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
+ unsigned dim, isl_int value)
+{
+ int j;
+ unsigned nparam;
+
+ bset = isl_basic_set_cow(bset);
+ bset = isl_basic_set_extend_constraints(bset, 0, 1);
+ j = isl_basic_set_alloc_inequality(bset);
+ if (j < 0)
+ goto error;
+ isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
+ isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
+ isl_int_neg(bset->ineq[j][0], value);
+ bset = isl_basic_set_simplify(bset);
+ return isl_basic_set_finalize(bset);
+error:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
+ isl_int value)
+{
+ int i;
+
+ set = isl_set_cow(set);
+ if (!set)
+ return NULL;
+
+ isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
+ for (i = 0; i < set->n; ++i) {
+ set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
+ if (!set->p[i])
+ goto error;
+ }
+ return set;
+error:
+ isl_set_free(set);
+ return NULL;
+}
+
+struct isl_map *isl_map_reverse(struct isl_map *map)
+{
int i;
+ unsigned t;
map = isl_map_cow(map);
if (!map)
return NULL;
- isl_assert(ctx, input < isl_map_n_in(map), goto error);
+ map->dim = isl_dim_reverse(map->dim);
+ if (!map->dim)
+ goto error;
for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
+ map->p[i] = isl_basic_map_reverse(map->p[i]);
if (!map->p[i])
goto error;
}
- F_CLR(map, ISL_MAP_NORMALIZED);
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
+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_basic_map_partial_lexopt(bmap, dom, empty, 1);
+}
+
+struct isl_map *isl_basic_map_partial_lexmin(
+ struct isl_basic_map *bmap, struct isl_basic_set *dom,
+ struct isl_set **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)
+{
+ struct isl_basic_map *bmap = NULL;
+ struct isl_basic_set *dom = NULL;
+ struct isl_map *min;
+ struct isl_dim *param_dim;
+
+ if (!bset)
+ goto error;
+ bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
+ if (!bmap)
+ goto error;
+ param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
+ dom = isl_basic_set_universe(param_dim);
+ if (!dom)
+ goto error;
+ 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_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;
}
-struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
+/* 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)
{
- int i;
+ struct isl_dim *dim = NULL;
+ unsigned nparam;
- set = isl_set_cow(set);
- if (!set)
- return NULL;
+ if (!set || !mat)
+ goto error;
- isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
- for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
- if (!set->p[i])
- 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;
}
-struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
- unsigned dim, isl_int value)
+/* 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 j;
- unsigned nparam;
+ int i, k;
+ unsigned len;
- bset = isl_basic_set_cow(bset);
- bset = isl_basic_set_extend_constraints(bset, 0, 1);
- j = isl_basic_set_alloc_inequality(bset);
- if (j < 0)
+ if (!bset || !eq)
goto error;
- isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
- isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
- isl_int_neg(bset->ineq[j][0], value);
- bset = isl_basic_set_simplify(bset);
- return isl_basic_set_finalize(bset);
+
+ 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;
}
-struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
- isl_int value)
+/* 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;
- set = isl_set_cow(set);
- if (!set)
- return NULL;
+ if (!set || !eq)
+ goto error;
- isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
+ 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;
}
-struct isl_map *isl_map_reverse(struct isl_map *map)
+/* 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;
- unsigned t;
+ int i, j;
+ struct isl_mat *eq;
+ struct isl_mat *T, *T2;
+ struct isl_set *set;
+ unsigned nparam, n_div;
- map = isl_map_cow(map);
- if (!map)
+ bset = isl_basic_set_cow(bset);
+ if (!bset)
return NULL;
- map->dim = isl_dim_reverse(map->dim);
- if (!map->dim)
- goto error;
- for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_reverse(map->p[i]);
- if (!map->p[i])
- goto error;
+ 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;
}
- F_CLR(map, ISL_MAP_NORMALIZED);
- return map;
-error:
- isl_map_free(map);
- return NULL;
-}
+ if (i == bset->n_eq)
+ return isl_basic_set_lexmin(bset);
-struct isl_map *isl_basic_map_lexmax(
- struct isl_basic_map *bmap, struct isl_basic_set *dom,
- struct isl_set **empty)
-{
- return isl_pip_basic_map_lexmax(bmap, dom, empty);
-}
+ 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);
-struct isl_map *isl_basic_map_lexmin(
- struct isl_basic_map *bmap, struct isl_basic_set *dom,
- struct isl_set **empty)
-{
- return isl_pip_basic_map_lexmin(bmap, dom, empty);
+ set = isl_basic_set_lexmin(bset);
+ set = set_parameter_preimage(set, T2);
+ set = set_append_equalities(set, eq);
+ return set;
}
-struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
+/* 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_map *bmap = NULL;
- struct isl_basic_set *dom = NULL;
- struct isl_map *min;
- struct isl_dim *param_dim;
+ 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;
- if (!bset)
- goto error;
- bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
+ 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;
- param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
- dom = isl_basic_set_universe(param_dim);
- if (!dom)
- goto error;
- min = isl_basic_map_lexmin(bmap, dom, NULL);
- return isl_map_range(min);
+
+ 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;
for (i = 1 ; i < map->n; ++i) {
struct isl_map *r2;
r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
- if (F_ISSET(map, ISL_MAP_DISJOINT))
+ if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
res = isl_map_union_disjoint(res, r2);
else
res = isl_map_union(res, r2);
if (!set->p[i])
goto error;
}
- F_CLR(set, ISL_MAP_DISJOINT);
- F_CLR(set, ISL_SET_NORMALIZED);
+ ISL_F_CLR(set, ISL_MAP_DISJOINT);
+ ISL_F_CLR(set, ISL_SET_NORMALIZED);
return set;
error:
isl_map_free(map);
isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
- if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
- F_ISSET(map2, ISL_MAP_DISJOINT))
- FL_SET(flags, ISL_MAP_DISJOINT);
+ if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
+ ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
+ ISL_FL_SET(flags, ISL_MAP_DISJOINT);
map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
map1->n + map2->n, flags);
if (!map1)
return NULL;
if (map1->n > 1)
- F_CLR(map1, ISL_MAP_DISJOINT);
+ ISL_F_CLR(map1, ISL_MAP_DISJOINT);
return map1;
}
if (!map || !set)
goto error;
- if (F_ISSET(map, ISL_MAP_DISJOINT) &&
- F_ISSET(set, ISL_MAP_DISJOINT))
- FL_SET(flags, ISL_MAP_DISJOINT);
+ if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
+ ISL_F_ISSET(set, ISL_MAP_DISJOINT))
+ ISL_FL_SET(flags, ISL_MAP_DISJOINT);
result = isl_map_alloc_dim(isl_dim_copy(map->dim),
map->n * set->n, flags);
isl_map_free(map1);
isl_map_free(map2);
if (result && result->n <= 1)
- F_SET(result, ISL_MAP_DISJOINT);
+ ISL_F_SET(result, ISL_MAP_DISJOINT);
return result;
error:
isl_map_free(map1);
nparam = isl_basic_map_n_param(bmap);
isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
bset = isl_basic_set_from_basic_map(bmap);
+ bset = isl_basic_set_cow(bset);
bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
bset = isl_basic_set_swap_vars(bset, 2*dim);
for (i = 0; i < dim; ++i) {
return NULL;
}
-/* If the only constraints a div d=floor(f/m)
- * appears in are its two defining constraints
- *
- * f - m d >=0
- * -(f - (m - 1)) + m d >= 0
- *
- * then it can safely be removed.
- */
-static int div_is_redundant(struct isl_basic_map *bmap, int div)
-{
- int i;
- unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
-
- for (i = 0; i < bmap->n_eq; ++i)
- if (!isl_int_is_zero(bmap->eq[i][pos]))
- return 0;
-
- for (i = 0; i < bmap->n_ineq; ++i) {
- if (isl_int_is_zero(bmap->ineq[i][pos]))
- continue;
- if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
- int neg;
- isl_int_sub(bmap->div[div][1],
- bmap->div[div][1], bmap->div[div][0]);
- isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
- neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
- isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
- isl_int_add(bmap->div[div][1],
- bmap->div[div][1], bmap->div[div][0]);
- if (!neg)
- return 0;
- if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
- bmap->n_div-div-1) != -1)
- return 0;
- } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
- if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
- return 0;
- if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
- bmap->n_div-div-1) != -1)
- return 0;
- } else
- return 0;
- }
-
- for (i = 0; i < bmap->n_div; ++i)
- if (!isl_int_is_zero(bmap->div[i][1+pos]))
- return 0;
-
- return 1;
-}
-
-/*
- * Remove divs that don't occur in any of the constraints or other divs.
- * These can arise when dropping some of the variables in a quast
- * returned by piplib.
- */
-static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
-{
- int i;
-
- if (!bmap)
- return NULL;
-
- for (i = bmap->n_div-1; i >= 0; --i) {
- if (!div_is_redundant(bmap, i))
- continue;
- bmap = isl_basic_map_drop_div(bmap, i);
- }
- return bmap;
-}
-
-struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
-{
- bmap = remove_redundant_divs(bmap);
- if (!bmap)
- return NULL;
- F_SET(bmap, ISL_BASIC_SET_FINAL);
- return bmap;
-}
-
-struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
-{
- return (struct isl_basic_set *)
- isl_basic_map_finalize((struct isl_basic_map *)bset);
-}
-
-struct isl_set *isl_set_finalize(struct isl_set *set)
-{
- int i;
-
- if (!set)
- return NULL;
- for (i = 0; i < set->n; ++i) {
- set->p[i] = isl_basic_set_finalize(set->p[i]);
- if (!set->p[i])
- goto error;
- }
- return set;
-error:
- isl_set_free(set);
- return NULL;
-}
-
-struct isl_map *isl_map_finalize(struct isl_map *map)
-{
- int i;
-
- if (!map)
- return NULL;
- for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_finalize(map->p[i]);
- if (!map->p[i])
- goto error;
- }
- F_CLR(map, ISL_MAP_NORMALIZED);
- return map;
-error:
- isl_map_free(map);
- return NULL;
-}
-
static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
{
struct isl_basic_map *bmap;
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);
return 1;
}
+int isl_map_fast_is_empty(struct isl_map *map)
+{
+ return map->n == 0;
+}
+
int isl_set_is_empty(struct isl_set *set)
{
return isl_map_is_empty((struct isl_map *)set);
return is_subset;
is_subset = isl_basic_map_is_subset(bmap2, bmap1);
if (is_subset == -1)
- return is_subset;
- return !is_subset;
-}
-
-static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
-{
- int i;
- unsigned total;
- isl_int s;
-
- total = 1 + isl_basic_map_total_dim(bmap);
- if (total != vec->size)
- return -1;
-
- isl_int_init(s);
-
- for (i = 0; i < bmap->n_eq; ++i) {
- isl_seq_inner_product(vec->block.data, 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);
- if (isl_int_is_neg(s)) {
- isl_int_clear(s);
- return 0;
- }
- }
-
- isl_int_clear(s);
-
- return 1;
+ return is_subset;
+ return !is_subset;
}
int isl_basic_map_is_universe(struct isl_basic_map *bmap)
return bmap->n_eq == 0 && bmap->n_ineq == 0;
}
+int isl_basic_set_is_universe(struct isl_basic_set *bset)
+{
+ if (!bset)
+ return -1;
+ return bset->n_eq == 0 && bset->n_ineq == 0;
+}
+
int isl_basic_map_is_empty(struct isl_basic_map *bmap)
{
struct isl_basic_set *bset = NULL;
if (!bmap)
return -1;
- if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
+ if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
return 1;
- if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
+ if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
struct isl_basic_map *copy = isl_basic_map_copy(bmap);
copy = isl_basic_map_convex_hull(copy);
- empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
+ empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
isl_basic_map_free(copy);
return empty;
}
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;
}
+int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
+{
+ if (!bmap)
+ return -1;
+ return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
+}
+
+int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
+{
+ if (!bset)
+ return -1;
+ return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
+}
+
+int isl_basic_set_is_empty(struct isl_basic_set *bset)
+{
+ return isl_basic_map_is_empty((struct isl_basic_map *)bset);
+}
+
struct isl_map *isl_basic_map_union(
struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
{
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;
isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
src = order_divs(src);
+ dst = isl_basic_map_cow(dst);
dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
src->n_div, 0, 2 * src->n_div);
if (!dst)
goto error;
}
if (j != i)
- swap_div(dst, i, j);
+ isl_basic_map_swap_div(dst, i, j);
}
return dst;
error:
for (i = 1; i < map->n; ++i)
map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
- F_CLR(map, ISL_MAP_NORMALIZED);
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
return map;
}
+struct isl_set *isl_set_align_divs(struct isl_set *set)
+{
+ return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
+}
+
static struct isl_map *add_cut_constraint(struct isl_map *dst,
struct isl_basic_map *src, isl_int *c,
unsigned len, int oppose)
if (!map)
goto error;
- if (F_ISSET(map, ISL_MAP_DISJOINT))
- FL_SET(flags, ISL_MAP_DISJOINT);
+ if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
+ ISL_FL_SET(flags, ISL_MAP_DISJOINT);
max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
return NULL;
for (i = map->n-1; i >= 0; --i) {
- if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
+ if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
continue;
isl_basic_map_free(map->p[i]);
if (i != map->n-1) {
- F_CLR(map, ISL_MAP_NORMALIZED);
+ ISL_F_CLR(map, ISL_MAP_NORMALIZED);
map->p[i] = map->p[map->n-1];
}
map->n--;
isl_map_remove_empty_parts((struct isl_map *)set);
}
-struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
+struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
{
- struct isl_basic_set *bset;
- if (!set || set->n == 0)
+ struct isl_basic_map *bmap;
+ if (!map || map->n == 0)
return NULL;
- bset = set->p[set->n-1];
- isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
- return isl_basic_set_copy(bset);
+ bmap = map->p[map->n-1];
+ isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
+ return isl_basic_map_copy(bmap);
}
-struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
- struct isl_basic_set *bset)
+struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
+{
+ (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
+}
+
+struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
+ struct isl_basic_map *bmap)
{
int i;
- if (!set || !bset)
+ if (!map || !bmap)
goto error;
- for (i = set->n-1; i >= 0; --i) {
- if (set->p[i] != bset)
+ for (i = map->n-1; i >= 0; --i) {
+ if (map->p[i] != bmap)
continue;
- set = isl_set_cow(set);
- if (!set)
+ map = isl_map_cow(map);
+ if (!map)
goto error;
- isl_basic_set_free(set->p[i]);
- if (i != set->n-1) {
- F_CLR(set, ISL_SET_NORMALIZED);
- set->p[i] = set->p[set->n-1];
+ isl_basic_map_free(map->p[i]);
+ if (i != map->n-1) {
+ ISL_F_CLR(map, ISL_SET_NORMALIZED);
+ map->p[i] = map->p[map->n-1];
}
- set->n--;
- return set;
+ map->n--;
+ return map;
}
- isl_basic_set_free(bset);
- return set;
+ isl_basic_map_free(bmap);
+ return map;
error:
- isl_set_free(set);
- isl_basic_set_free(bset);
+ isl_map_free(map);
+ isl_basic_map_free(bmap);
return NULL;
}
+struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
+ struct isl_basic_set *bset)
+{
+ (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
+ (struct isl_basic_map *)bset);
+}
+
/* Given two _disjoint_ basic sets bset1 and bset2, check whether
* for any common value of the parameters and dimensions preceding dim
* in both basic sets, the values of dimension pos in bset1 are
bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
if (!bmap1 || !bmap2)
goto error;
+ bmap1 = isl_basic_map_cow(bmap1);
bmap1 = isl_basic_map_extend(bmap1, nparam,
pos, (dim1 - pos) + (dim2 - pos),
bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
goto error;
total = isl_basic_map_total_dim(bmap1);
ctx = bmap1->ctx;
- obj = isl_vec_alloc(ctx, total);
- isl_seq_clr(obj->block.data, total);
- isl_int_set_si(obj->block.data[nparam+pos], 1);
- isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
+ obj = isl_vec_alloc(ctx, 1 + total);
+ isl_seq_clr(obj->block.data, 1 + total);
+ isl_int_set_si(obj->block.data[1+nparam+pos], 1);
+ isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
if (!obj)
goto error;
isl_int_init(num);
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)
{
return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
}
+int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
+ enum isl_dim_type type, unsigned pos, isl_int *val)
+{
+ if (pos >= isl_basic_map_dim(bmap, type))
+ return -1;
+ return isl_basic_map_fast_has_fixed_var(bmap,
+ isl_basic_map_offset(bmap, type) - 1 + pos, 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.
*/
return fixed;
}
-static struct isl_basic_set *isl_basic_set_reduce_using_equalities(
- struct isl_basic_set *bset, struct isl_basic_set *context)
-{
- int i;
- int *elim;
-
- if (!bset || !context)
- goto error;
-
- bset = isl_basic_set_cow(bset);
- if (!bset)
- goto error;
-
- elim = isl_alloc_array(ctx, int, isl_basic_set_n_dim(bset));
- if (!elim)
- goto error;
- set_compute_elimination_index(context, elim);
- for (i = 0; i < bset->n_eq; ++i)
- set_reduced_using_equalities(bset->eq[i], bset->eq[i],
- context, elim);
- for (i = 0; i < bset->n_ineq; ++i)
- set_reduced_using_equalities(bset->ineq[i], bset->ineq[i],
- context, elim);
- isl_basic_set_free(context);
- free(elim);
- bset = isl_basic_set_simplify(bset);
- bset = isl_basic_set_finalize(bset);
- return bset;
-error:
- isl_basic_set_free(bset);
- isl_basic_set_free(context);
- return NULL;
-}
-
-static struct isl_basic_set *remove_shifted_constraints(
- struct isl_basic_set *bset, struct isl_basic_set *context)
-{
- unsigned int size;
- isl_int ***index;
- int bits;
- int k, h, l;
-
- if (!bset)
- return NULL;
-
- size = round_up(4 * (context->n_ineq+1) / 3 - 1);
- bits = ffs(size) - 1;
- index = isl_calloc_array(ctx, isl_int **, size);
- if (!index)
- return bset;
-
- for (k = 0; k < context->n_ineq; ++k) {
- h = set_hash_index(index, size, bits, context, k);
- index[h] = &context->ineq[k];
- }
- for (k = 0; k < bset->n_ineq; ++k) {
- h = set_hash_index(index, size, bits, bset, k);
- if (!index[h])
- continue;
- l = index[h] - &context->ineq[0];
- if (isl_int_lt(bset->ineq[k][0], context->ineq[l][0]))
- continue;
- bset = isl_basic_set_cow(bset);
- if (!bset)
- goto error;
- isl_basic_set_drop_inequality(bset, k);
- --k;
- }
- free(index);
- return bset;
-error:
- free(index);
- return bset;
-}
-
-static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
- struct isl_basic_set *context);
-
-static struct isl_basic_set *uset_gist_context_eq(struct isl_basic_set *bset,
- struct isl_basic_set *context)
-{
- struct isl_mat *T;
- struct isl_mat *T2;
- struct isl_ctx *ctx = context->ctx;
- struct isl_basic_set *reduced_context;
- reduced_context = isl_basic_set_remove_equalities(
- isl_basic_set_copy(context), &T, &T2);
- if (!reduced_context)
- goto error;
- bset = isl_basic_set_preimage(ctx, bset, T);
- bset = uset_gist(bset, reduced_context);
- bset = isl_basic_set_preimage(ctx, bset, T2);
- bset = isl_basic_set_reduce_using_equalities(bset, context);
- return bset;
-error:
- isl_basic_set_free(context);
- isl_basic_set_free(bset);
- return NULL;
-}
-
-static struct isl_basic_set *uset_gist_set_eq(struct isl_basic_set *bset,
- struct isl_basic_set *context)
-{
- struct isl_mat *T;
- struct isl_mat *T2;
- struct isl_ctx *ctx = context->ctx;
- struct isl_basic_set *affine_hull = NULL;
-
- affine_hull = isl_basic_set_copy(bset);
- affine_hull = isl_basic_set_cow(affine_hull);
- if (!affine_hull)
- goto error;
- isl_basic_set_free_inequality(affine_hull, affine_hull->n_ineq);
-
- bset = isl_basic_set_remove_equalities(bset, &T, &T2);
- if (!bset)
- goto error;
- context = isl_basic_set_preimage(ctx, context, T);
- bset = uset_gist(bset, context);
- bset = isl_basic_set_preimage(ctx, bset, T2);
- bset = isl_basic_set_intersect(bset, affine_hull);
- return bset;
-error:
- isl_basic_set_free(affine_hull);
- isl_basic_set_free(context);
- isl_basic_set_free(bset);
- return NULL;
-}
-
-/* Remove all information from bset that is redundant in the context
- * of context. In particular, equalities that are linear combinations
- * of those in context are removed. Then the inequalities that are
- * redundant in the context of the equalities and inequalities of
- * context are removed.
- */
-static struct isl_basic_set *uset_gist(struct isl_basic_set *bset,
- struct isl_basic_set *context)
-{
- int i;
- isl_int opt;
- struct isl_basic_set *combined;
- struct isl_ctx *ctx;
-
- if (!bset || !context)
- goto error;
-
- if (context->n_eq > 0)
- return uset_gist_context_eq(bset, context);
- if (!context->n_ineq)
- goto done;
- if (bset->n_eq > 0)
- return uset_gist_set_eq(bset, context);
- bset = remove_shifted_constraints(bset, context);
- combined = isl_basic_set_extend_constraints(isl_basic_set_copy(bset),
- context->n_eq, context->n_ineq);
- context = set_add_constraints(combined, context, 0);
- if (!context)
- goto error;
- ctx = context->ctx;
- isl_int_init(opt);
- for (i = bset->n_ineq-1; i >= 0; --i) {
- int redundant;
- set_swap_inequality(context, i, context->n_ineq-1);
- context->n_ineq--;
- redundant = isl_basic_set_constraint_is_redundant(&context,
- context->ineq[context->n_ineq], &opt, NULL);
- if (redundant == -1) {
- isl_int_clear(opt);
- goto error;
- }
- if (F_ISSET(context, ISL_BASIC_MAP_EMPTY)) {
- bset = isl_basic_set_set_to_empty(bset);
- break;
- }
- context->n_ineq++;
- set_swap_inequality(context, i, context->n_ineq-1);
- if (redundant) {
- isl_basic_set_drop_inequality(context, i);
- isl_basic_set_drop_inequality(bset, i);
- }
- }
- isl_int_clear(opt);
-done:
- isl_basic_set_free(context);
- return bset;
-error:
- isl_basic_set_free(context);
- isl_basic_set_free(bset);
- return NULL;
-}
-
-struct isl_basic_map *isl_basic_map_gist(struct isl_basic_map *bmap,
- struct isl_basic_map *context)
-{
- struct isl_basic_set *bset;
-
- if (!bmap || !context)
- goto error;
-
- context = isl_basic_map_align_divs(context, bmap);
- bmap = isl_basic_map_align_divs(bmap, context);
-
- bset = uset_gist(isl_basic_map_underlying_set(isl_basic_map_copy(bmap)),
- isl_basic_map_underlying_set(context));
-
- return isl_basic_map_overlying_set(bset, bmap);
-error:
- isl_basic_map_free(bmap);
- isl_basic_map_free(context);
- return NULL;
-}
-
-/*
- * Assumes context has no implicit divs.
- */
-struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
-{
- int i;
-
- map = isl_map_cow(map);
- if (!map || !context)
- return NULL;
- isl_assert(map->ctx, isl_dim_equal(map->dim, context->dim), goto error);
- map = isl_map_compute_divs(map);
- for (i = 0; i < map->n; ++i)
- context = isl_basic_map_align_divs(context, map->p[i]);
- for (i = 0; i < map->n; ++i) {
- map->p[i] = isl_basic_map_gist(map->p[i],
- isl_basic_map_copy(context));
- if (!map->p[i])
- goto error;
- }
- isl_basic_map_free(context);
- F_CLR(map, ISL_MAP_NORMALIZED);
- return map;
-error:
- isl_map_free(map);
- isl_basic_map_free(context);
- return NULL;
-}
-
-struct isl_basic_set *isl_basic_set_gist(struct isl_basic_set *bset,
- struct isl_basic_set *context)
-{
- return (struct isl_basic_set *)isl_basic_map_gist(
- (struct isl_basic_map *)bset, (struct isl_basic_map *)context);
-}
-
-struct isl_set *isl_set_gist(struct isl_set *set, struct isl_basic_set *context)
-{
- return (struct isl_set *)isl_map_gist((struct isl_map *)set,
- (struct isl_basic_map *)context);
-}
-
struct constraint {
unsigned size;
isl_int *c;
{
if (!bmap)
return NULL;
- if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
+ if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
return bmap;
bmap = isl_basic_map_convex_hull(bmap);
bmap = isl_basic_map_sort_constraints(bmap);
- F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
+ ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
return bmap;
}
return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
- if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
- F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
+ if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
+ ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
return 0;
- if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
+ if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
return 1;
- if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
+ if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
return -1;
if (bmap1->n_eq != bmap2->n_eq)
return bmap1->n_eq - bmap2->n_eq;
if (!map)
return NULL;
- if (F_ISSET(map, ISL_MAP_NORMALIZED))
+ if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
return map;
for (i = 0; i < map->n; ++i) {
bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
map->p[i] = bmap;
}
qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
- F_SET(map, ISL_MAP_NORMALIZED);
+ ISL_F_SET(map, ISL_MAP_NORMALIZED);
map = isl_map_remove_empty_parts(map);
if (!map)
return NULL;
goto error;
dim = 0;
for (i = 0; i < list->n; ++i) {
- set_add_constraints(product,
+ isl_basic_set_add_constraints(product,
isl_basic_set_copy(list->p[i]), dim);
dim += isl_basic_set_n_dim(list->p[i]);
}
return NULL;
}
+struct isl_basic_map *isl_basic_map_product(
+ struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
+{
+ struct isl_dim *dim_result = NULL;
+ struct isl_basic_map *bmap;
+ unsigned in1, in2, out1, out2, nparam, total, pos;
+ struct isl_dim_map *dim_map1, *dim_map2;
+
+ if (!bmap1 || !bmap2)
+ goto error;
+
+ isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
+ bmap2->dim, isl_dim_param), goto error);
+ dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
+ isl_dim_copy(bmap2->dim));
+
+ in1 = isl_basic_map_n_in(bmap1);
+ in2 = isl_basic_map_n_in(bmap2);
+ out1 = isl_basic_map_n_out(bmap1);
+ out2 = isl_basic_map_n_out(bmap2);
+ nparam = isl_basic_map_n_param(bmap1);
+
+ total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
+ dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
+ dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
+ isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
+ isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
+ isl_dim_map_div(dim_map1, bmap1, pos += out2);
+ isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
+
+ bmap = isl_basic_map_alloc_dim(dim_result,
+ bmap1->n_div + bmap2->n_div,
+ bmap1->n_eq + bmap2->n_eq,
+ bmap1->n_ineq + bmap2->n_ineq);
+ bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
+ bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
+ bmap = isl_basic_map_simplify(bmap);
+ return isl_basic_map_finalize(bmap);
+error:
+ isl_basic_map_free(bmap1);
+ isl_basic_map_free(bmap2);
+ return NULL;
+}
+
+/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
+ */
+struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
+{
+ unsigned flags = 0;
+ struct isl_map *result;
+ int i, j;
+
+ if (!map1 || !map2)
+ goto error;
+
+ isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
+ map2->dim, isl_dim_param), goto error);
+
+ if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
+ ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
+ ISL_FL_SET(flags, ISL_MAP_DISJOINT);
+
+ result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
+ isl_dim_copy(map2->dim)),
+ map1->n * map2->n, flags);
+ if (!result)
+ goto error;
+ for (i = 0; i < map1->n; ++i)
+ for (j = 0; j < map2->n; ++j) {
+ struct isl_basic_map *part;
+ part = isl_basic_map_product(
+ isl_basic_map_copy(map1->p[i]),
+ isl_basic_map_copy(map2->p[j]));
+ if (isl_basic_map_is_empty(part))
+ isl_basic_map_free(part);
+ else
+ result = isl_map_add(result, part);
+ if (!result)
+ goto error;
+ }
+ isl_map_free(map1);
+ isl_map_free(map2);
+ return result;
+error:
+ isl_map_free(map1);
+ isl_map_free(map2);
+ 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;