#include <strings.h>
#include "isl_ctx.h"
#include "isl_blk.h"
+#include "isl_equalities.h"
+#include "isl_list.h"
+#include "isl_lp.h"
#include "isl_seq.h"
#include "isl_set.h"
#include "isl_map.h"
bmap->n_eq++;
bmap->n_ineq--;
bmap->ineq++;
+ F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
}
int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
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);
return bmap->n_ineq++;
}
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);
}
bmap->n_ineq--;
return 0;
struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
{
int i;
- unsigned div_off = bmap1->n_div;
+ unsigned div_off;
+
+ if (!bmap1 || !bmap2)
+ goto error;
+
+ div_off = bmap1->n_div;
for (i = 0; i < bmap2->n_eq; ++i) {
int i1 = isl_basic_map_alloc_equality(bmap1);
return NULL;
}
+static struct isl_basic_set *set_add_constraints(struct isl_basic_set *bset1,
+ struct isl_basic_set *bset2, unsigned pos)
+{
+ return (struct isl_basic_set *)
+ add_constraints((struct isl_basic_map *)bset1,
+ (struct isl_basic_map *)bset2, 0, pos);
+}
+
struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
unsigned n_eq, unsigned n_ineq)
isl_blk_free(bset->ctx, blk);
+ F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
return bset;
error:
return NULL;
}
}
+ F_CLR(set, ISL_SET_NORMALIZED);
return set;
}
bset->dim -= n;
bset->extra += n;
+ F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
+ bset = isl_basic_set_simplify(bset);
return isl_basic_set_finalize(bset);
error:
isl_basic_set_free(bset);
}
set->dim -= n;
+ F_CLR(set, ISL_SET_NORMALIZED);
return set;
error:
isl_set_free(set);
return NULL;
}
+struct isl_basic_map *isl_basic_map_drop_inputs(
+ struct isl_basic_map *bmap, unsigned first, unsigned n)
+{
+ int i;
+
+ if (!bmap)
+ goto error;
+
+ isl_assert(bmap->ctx, first + n <= bmap->n_in, 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+bmap->nparam+first, n,
+ (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
+
+ for (i = 0; i < bmap->n_ineq; ++i)
+ constraint_drop_vars(bmap->ineq[i]+1+bmap->nparam+first, n,
+ (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
+
+ for (i = 0; i < bmap->n_div; ++i)
+ constraint_drop_vars(bmap->div[i]+1+1+bmap->nparam+first, n,
+ (bmap->n_in-first-n)+bmap->n_out+bmap->extra);
+
+ bmap->n_in -= n;
+ bmap->extra += n;
+
+ F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+ bmap = isl_basic_map_simplify(bmap);
+ return isl_basic_map_finalize(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)
+{
+ int i;
+
+ if (!map)
+ goto error;
+
+ isl_assert(map->ctx, first + n <= map->n_in, goto error);
+
+ if (n == 0)
+ return map;
+ map = isl_map_cow(map);
+ if (!map)
+ goto error;
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_drop_inputs(map->p[i], first, n);
+ if (!map->p[i])
+ goto error;
+ }
+ map->n_in -= n;
+ F_CLR(map, ISL_MAP_NORMALIZED);
+
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
/*
* We don't cow, as the div is assumed to be redundant.
*/
bmap->div[bmap->n_div - 1] = t;
}
+ F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
isl_basic_map_free_div(bmap, 1);
return bmap;
static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
{
- isl_int *t = bmap->ineq[a];
- bmap->ineq[a] = bmap->ineq[b];
- bmap->ineq[b] = t;
+ 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)
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 = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
+ 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)
if (isl_int_is_neg(bmap->eq[done][1+last_var]))
isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
- for (k = 0; k < bmap->n_eq; ++k) {
- if (k == done)
- continue;
- if (isl_int_is_zero(bmap->eq[k][1+last_var]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->eq[k], bmap->eq[done],
- 1+last_var, 1+total, NULL);
- }
-
- for (k = 0; k < bmap->n_ineq; ++k) {
- if (isl_int_is_zero(bmap->ineq[k][1+last_var]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->ineq[k], bmap->eq[done],
- 1+last_var, 1+total, NULL);
- }
-
- 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+last_var]))
- continue;
- if (progress)
- *progress = 1;
- isl_seq_elim(bmap->div[k]+1, bmap->eq[done],
- 1+last_var, 1+total, &bmap->div[k][0]);
- }
+ 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])) {
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;
}
+static void compute_elimination_index(struct isl_basic_map *bmap, int *elim)
+{
+ int d, i;
+ unsigned total;
+
+ total = bmap->nparam + bmap->n_in + bmap->n_out;
+ 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 int reduced_using_equalities(isl_int *c, struct isl_vec *v,
+ struct isl_basic_map *bmap, int *elim)
+{
+ int d, i;
+ int copied = 0;
+ unsigned total;
+
+ total = bmap->nparam + bmap->n_in + bmap->n_out;
+ for (d = total - 1; d >= 0; --d) {
+ if (isl_int_is_zero(c[1+d]))
+ continue;
+ if (elim[d] == -1)
+ continue;
+ if (!copied) {
+ isl_seq_cpy(v->block.data, c, 1 + total);
+ copied = 1;
+ }
+ isl_seq_elim(v->block.data, bmap->eq[elim[d]],
+ 1 + d, 1 + total, NULL);
+ }
+ return copied;
+}
+
+/* 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, bmap1->nparam == bmap2->nparam, return -1);
+ isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, return -1);
+ isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, return -1);
+ if (bmap1->n_div || bmap2->n_div)
+ return 0;
+ if (!bmap1->n_eq && !bmap2->n_eq)
+ return 0;
+
+ total = bmap1->nparam + bmap1->n_in + bmap1->n_out;
+ 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(bmap2->eq[i], v, 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(bmap2->ineq[i], v, 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(bmap1->ineq[i], v, 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)
{
extra = bmap->extra - bmap->n_div;
bmap = isl_basic_map_cow(bmap);
- bmap = isl_basic_map_gauss(bmap, NULL);
for (d = pos + n - 1; d >= pos; --d) {
int n_lower, n_upper;
if (!bmap)
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;
}
break;
}
}
+ F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
return bmap;
error:
isl_basic_map_free(bmap);
(struct isl_basic_map *)bset, pos, n);
}
-/* Project out n dimensions starting at first using Fourier-Motzkin */
-struct isl_set *isl_set_remove_dims(struct isl_set *set,
+/* 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;
if (!set->p[i])
goto error;
}
- set = isl_set_drop_dims(set, first, n);
return set;
error:
isl_set_free(set);
}
/* 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;
+}
+
+/* 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;
+
+ if (n == 0)
+ return map;
+
+ map = isl_map_cow(map);
+ if (!map)
+ return NULL;
+ isl_assert(map->ctx, first+n <= map->n_in, goto error);
+
+ for (i = 0; i < map->n; ++i) {
+ map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
+ map->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 in = bmap->n_in;
unsigned dim = bmap->n_in + bmap->n_out;
if (!pos)
- isl_int_print(out, c);
+ isl_int_print(out, c, 0);
else {
if (!isl_int_is_one(c))
- isl_int_print(out, c);
+ isl_int_print(out, c, 0);
if (pos < 1 + bmap->nparam)
fprintf(out, "p%d", pos - 1);
else if (pos < 1 + bmap->nparam + in)
fprintf(out, "e%d = [(", i);
dump_affine(bmap, bmap->div[i]+1, out);
fprintf(out, ")/");
- isl_int_print(out, bmap->div[i][0]);
+ isl_int_print(out, bmap->div[i][0], 0);
fprintf(out, "]\n");
}
}
}
fprintf(out, "%*s", indent, "");
- fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d\n",
- bset->ref, bset->nparam, bset->dim, bset->extra);
+ fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
+ bset->ref, bset->nparam, bset->dim, bset->extra,
+ bset->flags);
dump((struct isl_basic_map *)bset, out, indent);
}
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);
return 0;
}
goto error;
}
F_CLR(set, ISL_MAP_DISJOINT);
+ F_CLR(set, ISL_SET_NORMALIZED);
return set;
error:
isl_map_free(map);
isl_assert(map->ctx, map->n < map->size, goto error);
map->p[map->n] = bmap;
map->n++;
+ F_CLR(map, ISL_MAP_NORMALIZED);
return map;
error:
if (map)
if (!map->p[i])
goto error;
}
+ F_CLR(map, ISL_MAP_NORMALIZED);
return map;
error:
isl_map_free(map);
return NULL;
}
+struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
+ unsigned dim, isl_int value)
+{
+ int j;
+
+ bset = isl_basic_set_cow(bset);
+ bset = isl_basic_set_extend(bset, bset->nparam, bset->dim, 0, 0, 1);
+ j = isl_basic_set_alloc_inequality(bset);
+ if (j < 0)
+ goto error;
+ isl_seq_clr(bset->ineq[j], 1 + bset->nparam + bset->dim + bset->extra);
+ isl_int_set_si(bset->ineq[j][1 + bset->nparam + 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 < set->dim, 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;
if (!map->p[i])
goto error;
}
+ F_CLR(map, ISL_MAP_NORMALIZED);
return map;
error:
isl_map_free(map);
goto error;
}
F_CLR(set, ISL_MAP_DISJOINT);
+ F_CLR(set, ISL_SET_NORMALIZED);
return set;
error:
isl_map_free(map);
return NULL;
}
+/*
+ * returns range - domain
+ */
+struct isl_set *isl_map_deltas(struct isl_map *map)
+{
+ int i;
+ struct isl_set *result;
+
+ if (!map)
+ return NULL;
+
+ isl_assert(map->ctx, map->n_in == map->n_out, goto error);
+ result = isl_set_alloc(map->ctx, map->nparam, map->n_in, map->n, map->flags);
+ if (!result)
+ goto error;
+ for (i = 0; i < map->n; ++i)
+ result = isl_set_add(result,
+ isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
+ isl_map_free(map);
+ return result;
+error:
+ isl_map_free(map);
+ return NULL;
+}
+
/* If the only constraints a div d=floor(f/m)
* appears in are its two defining constraints
*
if (!map->p[i])
goto error;
}
+ F_CLR(map, ISL_MAP_NORMALIZED);
return map;
error:
isl_map_free(map);
if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
return 1;
+ if (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);
+ isl_basic_map_free(copy);
+ return empty;
+ }
+
total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
if (bmap->sample && bmap->sample->size == total) {
int contains = basic_map_contains(bmap, bmap->sample);
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);
return map;
}
if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
continue;
isl_basic_map_free(map->p[i]);
- if (i != map->n-1)
+ if (i != map->n-1) {
+ F_CLR(map, ISL_MAP_NORMALIZED);
map->p[i] = map->p[map->n-1];
+ }
map->n--;
}
if (!set)
goto error;
isl_basic_set_free(set->p[i]);
- if (i != set->n-1)
+ if (i != set->n-1) {
+ F_CLR(set, ISL_SET_NORMALIZED);
set->p[i] = set->p[set->n-1];
+ }
set->n--;
return set;
}
isl_basic_set_free(bset);
return NULL;
}
+
+/* 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
+ * smaller or larger then those in bset2.
+ *
+ * Returns
+ * 1 if bset1 follows bset2
+ * -1 if bset1 precedes bset2
+ * 0 if bset1 and bset2 are incomparable
+ * -2 if some error occurred.
+ */
+int isl_basic_set_compare_at(struct isl_basic_set *bset1,
+ struct isl_basic_set *bset2, int pos)
+{
+ struct isl_basic_map *bmap1 = NULL;
+ struct isl_basic_map *bmap2 = NULL;
+ struct isl_ctx *ctx;
+ struct isl_vec *obj;
+ unsigned total;
+ isl_int num, den;
+ enum isl_lp_result res;
+ int cmp;
+
+ if (!bset1 || !bset2)
+ return -2;
+
+ bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1),
+ pos, bset1->dim - pos);
+ bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2),
+ pos, bset2->dim - pos);
+ if (!bmap1 || !bmap2)
+ goto error;
+ bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
+ bmap1->n_in, bmap1->n_out + bmap2->n_out,
+ bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
+ if (!bmap1)
+ goto error;
+ total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
+ ctx = bmap1->ctx;
+ obj = isl_vec_alloc(ctx, total);
+ isl_seq_clr(obj->block.data, total);
+ isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in], 1);
+ isl_int_set_si(obj->block.data[bmap1->nparam+bmap1->n_in+
+ bmap1->n_out-bmap2->n_out], -1);
+ if (!obj)
+ goto error;
+ bmap1 = add_constraints(bmap1, bmap2, 0, bmap1->n_out - bmap2->n_out);
+ isl_int_init(num);
+ isl_int_init(den);
+ res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
+ if (res == isl_lp_empty)
+ cmp = 0;
+ else if (res == isl_lp_ok && isl_int_is_pos(num))
+ cmp = 1;
+ else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
+ res == isl_lp_unbounded)
+ cmp = -1;
+ else
+ cmp = -2;
+ isl_int_clear(num);
+ isl_int_clear(den);
+ isl_basic_map_free(bmap1);
+ isl_vec_free(ctx, obj);
+ return cmp;
+error:
+ isl_basic_map_free(bmap1);
+ isl_basic_map_free(bmap2);
+ return -2;
+}
+
+static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
+ unsigned pos, isl_int *val)
+{
+ int i;
+ int d;
+ unsigned total;
+
+ if (!bmap)
+ return -1;
+ total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
+ for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
+ for (; d+1 > pos; --d)
+ if (!isl_int_is_zero(bmap->eq[i][1+d]))
+ break;
+ if (d != pos)
+ continue;
+ if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
+ return 0;
+ if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
+ return 0;
+ if (!isl_int_is_one(bmap->eq[i][1+d]))
+ return 0;
+ if (val)
+ isl_int_neg(*val, bmap->eq[i][0]);
+ return 1;
+ }
+ return 0;
+}
+
+static int isl_map_fast_has_fixed_var(struct isl_map *map,
+ unsigned pos, isl_int *val)
+{
+ int i;
+ isl_int v;
+ isl_int tmp;
+ int fixed;
+
+ if (!map)
+ return -1;
+ if (map->n == 0)
+ return 0;
+ if (map->n == 1)
+ return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val);
+ isl_int_init(v);
+ isl_int_init(tmp);
+ fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v);
+ for (i = 1; fixed == 1 && i < map->n; ++i) {
+ fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp);
+ if (fixed == 1 && isl_int_ne(tmp, v))
+ fixed = 0;
+ }
+ if (val)
+ isl_int_set(*val, v);
+ isl_int_clear(tmp);
+ isl_int_clear(v);
+ return fixed;
+}
+
+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);
+}
+
+/* Check if dimension dim has fixed value and if so and if val is not NULL,
+ * then return this fixed value in *val.
+ */
+int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
+{
+ return isl_set_fast_has_fixed_var(set, set->nparam + dim, val);
+}
+
+/* Check if input variable in has fixed value and if so and if val is not NULL,
+ * then return this fixed value in *val.
+ */
+int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
+{
+ return isl_map_fast_has_fixed_var(map, map->nparam + in, val);
+}
+
+/* Check if dimension dim has an (obvious) fixed lower bound and if so
+ * and if val is not NULL, then return this lower bound in *val.
+ */
+int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
+ unsigned dim, isl_int *val)
+{
+ int i, i_eq = -1, i_ineq = -1;
+ isl_int *c;
+ unsigned total;
+
+ if (!bset)
+ return -1;
+ total = bset->nparam + bset->dim + bset->n_div;
+ for (i = 0; i < bset->n_eq; ++i) {
+ if (isl_int_is_zero(bset->eq[i][1+bset->nparam+dim]))
+ continue;
+ if (i_eq != -1)
+ return 0;
+ i_eq = i;
+ }
+ for (i = 0; i < bset->n_ineq; ++i) {
+ if (!isl_int_is_pos(bset->ineq[i][1+bset->nparam+dim]))
+ continue;
+ if (i_eq != -1 || i_ineq != -1)
+ return 0;
+ i_ineq = i;
+ }
+ if (i_eq == -1 && i_ineq == -1)
+ return 0;
+ c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
+ /* The coefficient should always be one due to normalization. */
+ if (!isl_int_is_one(c[1+bset->nparam+dim]))
+ return 0;
+ if (isl_seq_first_non_zero(c+1, bset->nparam+dim) != -1)
+ return 0;
+ if (isl_seq_first_non_zero(c+1+bset->nparam+dim+1,
+ total - bset->nparam - dim - 1) != -1)
+ return 0;
+ if (val)
+ isl_int_neg(*val, c[0]);
+ return 1;
+}
+
+int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
+ unsigned dim, isl_int *val)
+{
+ int i;
+ isl_int v;
+ isl_int tmp;
+ int fixed;
+
+ if (!set)
+ return -1;
+ if (set->n == 0)
+ return 0;
+ if (set->n == 1)
+ return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
+ dim, val);
+ isl_int_init(v);
+ isl_int_init(tmp);
+ fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
+ dim, &v);
+ for (i = 1; fixed == 1 && i < set->n; ++i) {
+ fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
+ dim, &tmp);
+ if (fixed == 1 && isl_int_ne(tmp, v))
+ fixed = 0;
+ }
+ if (val)
+ isl_int_set(*val, v);
+ isl_int_clear(tmp);
+ isl_int_clear(v);
+ return fixed;
+}
+
+/* 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 remobed. 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;
+ enum isl_lp_result res = isl_lp_ok;
+
+ if (!bset || !context)
+ goto error;
+
+ if (context->n_eq > 0) {
+ struct isl_mat *T;
+ struct isl_mat *T2;
+ struct isl_ctx *ctx = context->ctx;
+ context = isl_basic_set_remove_equalities(context, &T, &T2);
+ if (!context)
+ goto error;
+ bset = isl_basic_set_preimage(ctx, bset, T);
+ bset = uset_gist(bset, context);
+ bset = isl_basic_set_preimage(ctx, bset, T2);
+ return bset;
+ }
+ combined = isl_basic_set_extend(isl_basic_set_copy(bset),
+ 0, bset->dim, 0, 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) {
+ set_swap_inequality(context, i, context->n_ineq-1);
+ context->n_ineq--;
+ res = isl_solve_lp((struct isl_basic_map *)context, 0,
+ context->ineq[context->n_ineq]+1, ctx->one, &opt, NULL);
+ context->n_ineq++;
+ set_swap_inequality(context, i, context->n_ineq-1);
+ if (res == isl_lp_unbounded)
+ continue;
+ if (res == isl_lp_error)
+ break;
+ if (res == isl_lp_empty) {
+ bset = isl_basic_set_set_to_empty(bset);
+ break;
+ }
+ isl_int_add(opt, opt, context->ineq[i][0]);
+ if (!isl_int_is_neg(opt)) {
+ isl_basic_set_drop_inequality(context, i);
+ isl_basic_set_drop_inequality(bset, i);
+ }
+ }
+ isl_int_clear(opt);
+ if (res == isl_lp_error)
+ goto error;
+ 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;
+}
+
+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, map->nparam == context->nparam, goto error);
+ isl_assert(map->ctx, map->n_in == context->n_in, goto error);
+ isl_assert(map->ctx, map->n_out == context->n_out, goto error);
+ 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_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;
+};
+
+static int qsort_constraint_cmp(const void *p1, const void *p2)
+{
+ const struct constraint *c1 = (const struct constraint *)p1;
+ const struct constraint *c2 = (const struct constraint *)p2;
+ unsigned size = isl_min(c1->size, c2->size);
+ return isl_seq_cmp(c1->c, c2->c, size);
+}
+
+static struct isl_basic_map *isl_basic_map_sort_constraints(
+ struct isl_basic_map *bmap)
+{
+ int i;
+ struct constraint *c;
+ unsigned total;
+
+ if (!bmap)
+ return NULL;
+ total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
+ c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
+ if (!c)
+ goto error;
+ for (i = 0; i < bmap->n_ineq; ++i) {
+ c[i].size = total;
+ c[i].c = bmap->ineq[i];
+ }
+ qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
+ for (i = 0; i < bmap->n_ineq; ++i)
+ bmap->ineq[i] = c[i].c;
+ free(c);
+ return bmap;
+error:
+ isl_basic_map_free(bmap);
+ return NULL;
+}
+
+struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
+{
+ if (!bmap)
+ return NULL;
+ if (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);
+ return bmap;
+}
+
+static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
+ const struct isl_basic_map *bmap2)
+{
+ int i, cmp;
+ unsigned total;
+
+ if (bmap1 == bmap2)
+ return 0;
+ if (bmap1->nparam != bmap2->nparam)
+ return bmap1->nparam - bmap2->nparam;
+ if (bmap1->n_in != bmap2->n_in)
+ return bmap1->n_in - bmap2->n_in;
+ if (bmap1->n_out != bmap2->n_out)
+ return bmap1->n_out - bmap2->n_out;
+ if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
+ F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
+ return 0;
+ if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
+ return 1;
+ if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
+ return -1;
+ if (bmap1->n_eq != bmap2->n_eq)
+ return bmap1->n_eq - bmap2->n_eq;
+ if (bmap1->n_ineq != bmap2->n_ineq)
+ return bmap1->n_ineq - bmap2->n_ineq;
+ if (bmap1->n_div != bmap2->n_div)
+ return bmap1->n_div - bmap2->n_div;
+ total = bmap1->nparam + bmap1->n_in + bmap1->n_out + bmap1->n_div;
+ for (i = 0; i < bmap1->n_eq; ++i) {
+ cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
+ if (cmp)
+ return cmp;
+ }
+ for (i = 0; i < bmap1->n_ineq; ++i) {
+ cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
+ if (cmp)
+ return cmp;
+ }
+ for (i = 0; i < bmap1->n_div; ++i) {
+ cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
+ if (cmp)
+ return cmp;
+ }
+ return 0;
+}
+
+static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
+ struct isl_basic_map *bmap2)
+{
+ return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
+}
+
+static int qsort_bmap_cmp(const void *p1, const void *p2)
+{
+ const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
+ const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
+
+ return isl_basic_map_fast_cmp(bmap1, bmap2);
+}
+
+/* We normalize in place, but if anything goes wrong we need
+ * to return NULL, so we need to make sure we don't change the
+ * meaning of any possible other copies of map.
+ */
+struct isl_map *isl_map_normalize(struct isl_map *map)
+{
+ int i, j;
+ struct isl_basic_map *bmap;
+
+ if (!map)
+ return NULL;
+ if (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]));
+ if (!bmap)
+ goto error;
+ isl_basic_map_free(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);
+ map = isl_map_remove_empty_parts(map);
+ if (!map)
+ return NULL;
+ for (i = map->n - 1; i >= 1; --i) {
+ if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
+ continue;
+ isl_basic_map_free(map->p[i-1]);
+ for (j = i; j < map->n; ++j)
+ map->p[j-1] = map->p[j];
+ map->n--;
+ }
+ return map;
+error:
+ isl_map_free(map);
+ return NULL;
+
+}
+
+struct isl_set *isl_set_normalize(struct isl_set *set)
+{
+ return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
+}
+
+int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
+{
+ int i;
+ int equal;
+
+ if (!map1 || !map2)
+ return -1;
+
+ if (map1 == map2)
+ return 1;
+ if (map1->nparam != map2->nparam)
+ return 0;
+ if (map1->n_in != map2->n_in)
+ return 0;
+ if (map1->n_out != map2->n_out)
+ return 0;
+
+ map1 = isl_map_copy(map1);
+ map2 = isl_map_copy(map2);
+ map1 = isl_map_normalize(map1);
+ map2 = isl_map_normalize(map2);
+ if (!map1 || !map2)
+ goto error;
+ equal = map1->n == map2->n;
+ for (i = 0; equal && i < map1->n; ++i) {
+ equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
+ if (equal < 0)
+ goto error;
+ }
+ isl_map_free(map1);
+ isl_map_free(map2);
+ return equal;
+error:
+ isl_map_free(map1);
+ isl_map_free(map2);
+ return -1;
+}
+
+int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
+{
+ return isl_map_fast_is_equal((struct isl_map *)set1,
+ (struct isl_map *)set2);
+}
+
+/* Return an interval that ranges from min to max (inclusive)
+ */
+struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
+ isl_int min, isl_int max)
+{
+ int k;
+ struct isl_basic_set *bset = NULL;
+
+ bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
+ if (!bset)
+ goto error;
+
+ k = isl_basic_set_alloc_inequality(bset);
+ if (k < 0)
+ goto error;
+ isl_int_set_si(bset->ineq[k][1], 1);
+ isl_int_neg(bset->ineq[k][0], min);
+
+ k = isl_basic_set_alloc_inequality(bset);
+ if (k < 0)
+ goto error;
+ isl_int_set_si(bset->ineq[k][1], -1);
+ isl_int_set(bset->ineq[k][0], max);
+
+ return bset;
+error:
+ isl_basic_set_free(bset);
+ return NULL;
+}
+
+/* Return the Cartesian product of the basic sets in list (in the given order).
+ */
+struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
+{
+ int i;
+ unsigned dim;
+ unsigned nparam;
+ unsigned extra;
+ unsigned n_eq;
+ unsigned n_ineq;
+ struct isl_basic_set *product = NULL;
+
+ if (!list)
+ goto error;
+ isl_assert(list->ctx, list->n > 0, goto error);
+ isl_assert(list->ctx, list->p[0], goto error);
+ nparam = list->p[0]->nparam;
+ dim = list->p[0]->dim;
+ extra = list->p[0]->n_div;
+ n_eq = list->p[0]->n_eq;
+ n_ineq = list->p[0]->n_ineq;
+ for (i = 1; i < list->n; ++i) {
+ isl_assert(list->ctx, list->p[i], goto error);
+ isl_assert(list->ctx, nparam == list->p[i]->nparam, goto error);
+ dim += list->p[i]->dim;
+ extra += list->p[i]->n_div;
+ n_eq += list->p[i]->n_eq;
+ n_ineq += list->p[i]->n_ineq;
+ }
+ product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
+ n_eq, n_ineq);
+ if (!product)
+ goto error;
+ dim = 0;
+ for (i = 0; i < list->n; ++i) {
+ set_add_constraints(product,
+ isl_basic_set_copy(list->p[i]), dim);
+ dim += list->p[i]->dim;
+ }
+ isl_basic_set_list_free(list);
+ return product;
+error:
+ isl_basic_set_free(product);
+ isl_basic_set_list_free(list);
+ return NULL;
+}