+
+struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
+{
+ struct isl_basic_set *bset;
+ if (!set || set->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);
+}
+
+struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
+ struct isl_basic_set *bset)
+{
+ int i;
+
+ if (!set || !bset)
+ goto error;
+ for (i = set->n-1; i >= 0; --i) {
+ if (set->p[i] != bset)
+ continue;
+ set = isl_set_cow(set);
+ if (!set)
+ 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];
+ }
+ set->n--;
+ return set;
+ }
+ isl_basic_set_free(bset);
+ return set;
+error:
+ isl_set_free(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;
+}