+
+__isl_give isl_local_space *isl_local_space_add_dims(
+ __isl_take isl_local_space *ls, enum isl_dim_type type, unsigned n)
+{
+ int pos;
+
+ if (!ls)
+ return NULL;
+ pos = isl_local_space_dim(ls, type);
+ return isl_local_space_insert_dims(ls, type, pos, n);
+}
+
+/* Remove common factor of non-constant terms and denominator.
+ */
+static void normalize_div(__isl_keep isl_local_space *ls, int div)
+{
+ isl_ctx *ctx = ls->div->ctx;
+ unsigned total = ls->div->n_col - 2;
+
+ isl_seq_gcd(ls->div->row[div] + 2, total, &ctx->normalize_gcd);
+ isl_int_gcd(ctx->normalize_gcd,
+ ctx->normalize_gcd, ls->div->row[div][0]);
+ if (isl_int_is_one(ctx->normalize_gcd))
+ return;
+
+ isl_seq_scale_down(ls->div->row[div] + 2, ls->div->row[div] + 2,
+ ctx->normalize_gcd, total);
+ isl_int_divexact(ls->div->row[div][0], ls->div->row[div][0],
+ ctx->normalize_gcd);
+ isl_int_fdiv_q(ls->div->row[div][1], ls->div->row[div][1],
+ ctx->normalize_gcd);
+}
+
+/* Exploit the equalities in "eq" to simplify the expressions of
+ * the integer divisions in "ls".
+ * The integer divisions in "ls" are assumed to appear as regular
+ * dimensions in "eq".
+ */
+__isl_give isl_local_space *isl_local_space_substitute_equalities(
+ __isl_take isl_local_space *ls, __isl_take isl_basic_set *eq)
+{
+ int i, j, k;
+ unsigned total;
+ unsigned n_div;
+
+ ls = isl_local_space_cow(ls);
+ if (!ls || !eq)
+ goto error;
+
+ total = isl_space_dim(eq->dim, isl_dim_all);
+ if (isl_local_space_dim(ls, isl_dim_all) != total)
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "dimensions don't match", goto error);
+ total++;
+ n_div = eq->n_div;
+ for (i = 0; i < eq->n_eq; ++i) {
+ j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
+ if (j < 0 || j == 0 || j >= total)
+ continue;
+
+ for (k = 0; k < ls->div->n_row; ++k) {
+ if (isl_int_is_zero(ls->div->row[k][1 + j]))
+ continue;
+ isl_seq_elim(ls->div->row[k] + 1, eq->eq[i], j, total,
+ &ls->div->row[k][0]);
+ normalize_div(ls, k);
+ }
+ }
+
+ isl_basic_set_free(eq);
+ return ls;
+error:
+ isl_basic_set_free(eq);
+ isl_local_space_free(ls);
+ return NULL;
+}
+
+/* Plug in the affine expressions "subs" of length "subs_len" (including
+ * the denominator and the constant term) into the variable at position "pos"
+ * of the "n" div expressions starting at "first".
+ *
+ * Let i be the dimension to replace and let "subs" be of the form
+ *
+ * f/d
+ *
+ * Any integer division starting at "first" with a non-zero coefficient for i,
+ *
+ * floor((a i + g)/m)
+ *
+ * is replaced by
+ *
+ * floor((a f + d g)/(m d))
+ */
+__isl_give isl_local_space *isl_local_space_substitute_seq(
+ __isl_take isl_local_space *ls,
+ enum isl_dim_type type, unsigned pos, isl_int *subs, int subs_len,
+ int first, int n)
+{
+ int i;
+ isl_int v;
+
+ if (n == 0)
+ return ls;
+ ls = isl_local_space_cow(ls);
+ if (!ls)
+ return NULL;
+ ls->div = isl_mat_cow(ls->div);
+ if (!ls->div)
+ return isl_local_space_free(ls);
+
+ if (first + n > ls->div->n_row)
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "index out of bounds", return isl_local_space_free(ls));
+
+ pos += isl_local_space_offset(ls, type);
+
+ isl_int_init(v);
+ for (i = first; i < ls->div->n_row; ++i) {
+ if (isl_int_is_zero(ls->div->row[i][1 + pos]))
+ continue;
+ isl_seq_substitute(ls->div->row[i], pos, subs,
+ ls->div->n_col, subs_len, v);
+ normalize_div(ls, i);
+ }
+ isl_int_clear(v);
+
+ return ls;
+}
+
+/* Plug in "subs" for dimension "type", "pos" in the integer divisions
+ * of "ls".
+ *
+ * Let i be the dimension to replace and let "subs" be of the form
+ *
+ * f/d
+ *
+ * Any integer division with a non-zero coefficient for i,
+ *
+ * floor((a i + g)/m)
+ *
+ * is replaced by
+ *
+ * floor((a f + d g)/(m d))
+ */
+__isl_give isl_local_space *isl_local_space_substitute(
+ __isl_take isl_local_space *ls,
+ enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
+{
+ ls = isl_local_space_cow(ls);
+ if (!ls || !subs)
+ return isl_local_space_free(ls);
+
+ if (!isl_space_is_equal(ls->dim, subs->ls->dim))
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "spaces don't match", return isl_local_space_free(ls));
+ if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
+ isl_die(isl_local_space_get_ctx(ls), isl_error_unsupported,
+ "cannot handle divs yet",
+ return isl_local_space_free(ls));
+
+ return isl_local_space_substitute_seq(ls, type, pos, subs->v->el,
+ subs->v->size, 0, ls->div->n_row);
+}
+
+int isl_local_space_is_named_or_nested(__isl_keep isl_local_space *ls,
+ enum isl_dim_type type)
+{
+ if (!ls)
+ return -1;
+ return isl_space_is_named_or_nested(ls->dim, type);
+}
+
+__isl_give isl_local_space *isl_local_space_drop_dims(
+ __isl_take isl_local_space *ls,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ isl_ctx *ctx;
+
+ if (!ls)
+ return NULL;
+ if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
+ return ls;
+
+ ctx = isl_local_space_get_ctx(ls);
+ if (first + n > isl_local_space_dim(ls, type))
+ isl_die(ctx, isl_error_invalid, "range out of bounds",
+ return isl_local_space_free(ls));
+
+ ls = isl_local_space_cow(ls);
+ if (!ls)
+ return NULL;
+
+ if (type == isl_dim_div) {
+ ls->div = isl_mat_drop_rows(ls->div, first, n);
+ } else {
+ ls->dim = isl_space_drop_dims(ls->dim, type, first, n);
+ if (!ls->dim)
+ return isl_local_space_free(ls);
+ }
+
+ first += 1 + isl_local_space_offset(ls, type);
+ ls->div = isl_mat_drop_cols(ls->div, first, n);
+ if (!ls->div)
+ return isl_local_space_free(ls);
+
+ return ls;
+}
+
+__isl_give isl_local_space *isl_local_space_insert_dims(
+ __isl_take isl_local_space *ls,
+ enum isl_dim_type type, unsigned first, unsigned n)
+{
+ isl_ctx *ctx;
+
+ if (!ls)
+ return NULL;
+ if (n == 0 && !isl_local_space_is_named_or_nested(ls, type))
+ return ls;
+
+ ctx = isl_local_space_get_ctx(ls);
+ if (first > isl_local_space_dim(ls, type))
+ isl_die(ctx, isl_error_invalid, "position out of bounds",
+ return isl_local_space_free(ls));
+
+ ls = isl_local_space_cow(ls);
+ if (!ls)
+ return NULL;
+
+ if (type == isl_dim_div) {
+ ls->div = isl_mat_insert_zero_rows(ls->div, first, n);
+ } else {
+ ls->dim = isl_space_insert_dims(ls->dim, type, first, n);
+ if (!ls->dim)
+ return isl_local_space_free(ls);
+ }
+
+ first += 1 + isl_local_space_offset(ls, type);
+ ls->div = isl_mat_insert_zero_cols(ls->div, first, n);
+ if (!ls->div)
+ return isl_local_space_free(ls);
+
+ return ls;
+}
+
+/* Check if the constraints pointed to by "constraint" is a div
+ * constraint corresponding to div "div" in "ls".
+ *
+ * That is, if div = floor(f/m), then check if the constraint is
+ *
+ * f - m d >= 0
+ * or
+ * -(f-(m-1)) + m d >= 0
+ */
+int isl_local_space_is_div_constraint(__isl_keep isl_local_space *ls,
+ isl_int *constraint, unsigned div)
+{
+ unsigned pos;
+
+ if (!ls)
+ return -1;
+
+ if (isl_int_is_zero(ls->div->row[div][0]))
+ return 0;
+
+ pos = isl_local_space_offset(ls, isl_dim_div) + div;
+
+ if (isl_int_eq(constraint[pos], ls->div->row[div][0])) {
+ int neg;
+ isl_int_sub(ls->div->row[div][1],
+ ls->div->row[div][1], ls->div->row[div][0]);
+ isl_int_add_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
+ neg = isl_seq_is_neg(constraint, ls->div->row[div]+1, pos);
+ isl_int_sub_ui(ls->div->row[div][1], ls->div->row[div][1], 1);
+ isl_int_add(ls->div->row[div][1],
+ ls->div->row[div][1], ls->div->row[div][0]);
+ if (!neg)
+ return 0;
+ if (isl_seq_first_non_zero(constraint+pos+1,
+ ls->div->n_row-div-1) != -1)
+ return 0;
+ } else if (isl_int_abs_eq(constraint[pos], ls->div->row[div][0])) {
+ if (!isl_seq_eq(constraint, ls->div->row[div]+1, pos))
+ return 0;
+ if (isl_seq_first_non_zero(constraint+pos+1,
+ ls->div->n_row-div-1) != -1)
+ return 0;
+ } else
+ return 0;
+
+ return 1;
+}
+
+/*
+ * Set active[i] to 1 if the dimension at position i is involved
+ * in the linear expression l.
+ */
+int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
+{
+ int i, j;
+ isl_ctx *ctx;
+ int *active = NULL;
+ unsigned total;
+ unsigned offset;
+
+ ctx = isl_local_space_get_ctx(ls);
+ total = isl_local_space_dim(ls, isl_dim_all);
+ active = isl_calloc_array(ctx, int, total);
+ if (!active)
+ return NULL;
+
+ for (i = 0; i < total; ++i)
+ active[i] = !isl_int_is_zero(l[i]);
+
+ offset = isl_local_space_offset(ls, isl_dim_div) - 1;
+ for (i = ls->div->n_row - 1; i >= 0; --i) {
+ if (!active[offset + i])
+ continue;
+ for (j = 0; j < total; ++j)
+ active[j] |= !isl_int_is_zero(ls->div->row[i][2 + j]);
+ }
+
+ return active;
+}
+
+/* Given a local space "ls" of a set, create a local space
+ * for the lift of the set. In particular, the result
+ * is of the form [dim -> local[..]], with ls->div->n_row variables in the
+ * range of the wrapped map.
+ */
+__isl_give isl_local_space *isl_local_space_lift(
+ __isl_take isl_local_space *ls)
+{
+ ls = isl_local_space_cow(ls);
+ if (!ls)
+ return NULL;
+
+ ls->dim = isl_space_lift(ls->dim, ls->div->n_row);
+ ls->div = isl_mat_drop_rows(ls->div, 0, ls->div->n_row);
+ if (!ls->dim || !ls->div)
+ return isl_local_space_free(ls);
+
+ return ls;
+}
+
+/* Construct a basic map that maps a set living in local space "ls"
+ * to the corresponding lifted local space.
+ */
+__isl_give isl_basic_map *isl_local_space_lifting(
+ __isl_take isl_local_space *ls)
+{
+ isl_basic_map *lifting;
+ isl_basic_set *bset;
+
+ if (!ls)
+ return NULL;
+ if (!isl_local_space_is_set(ls))
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "lifting only defined on set spaces",
+ return isl_local_space_free(ls));
+
+ bset = isl_basic_set_from_local_space(ls);
+ lifting = isl_basic_set_unwrap(isl_basic_set_lift(bset));
+ lifting = isl_basic_map_domain_map(lifting);
+ lifting = isl_basic_map_reverse(lifting);
+
+ return lifting;
+}
+
+/* Compute the preimage of "ls" under the function represented by "ma".
+ * In other words, plug in "ma" in "ls". The result is a local space
+ * that is part of the domain space of "ma".
+ *
+ * If the divs in "ls" are represented as
+ *
+ * floor((a_i(p) + b_i x + c_i(divs))/n_i)
+ *
+ * and ma is represented by
+ *
+ * x = D(p) + F(y) + G(divs')
+ *
+ * then the resulting divs are
+ *
+ * floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
+ *
+ * We first copy over the divs from "ma" and then
+ * we add the modified divs from "ls".
+ */
+__isl_give isl_local_space *isl_local_space_preimage_multi_aff(
+ __isl_take isl_local_space *ls, __isl_take isl_multi_aff *ma)
+{
+ int i;
+ isl_space *space;
+ isl_local_space *res = NULL;
+ int n_div_ls, n_div_ma;
+ isl_int f, c1, c2, g;
+
+ ma = isl_multi_aff_align_divs(ma);
+ if (!ls || !ma)
+ goto error;
+ if (!isl_space_is_range_internal(ls->dim, ma->space))
+ isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+ "spaces don't match", goto error);
+
+ n_div_ls = isl_local_space_dim(ls, isl_dim_div);
+ n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
+
+ space = isl_space_domain(isl_multi_aff_get_space(ma));
+ res = isl_local_space_alloc(space, n_div_ma + n_div_ls);
+ if (!res)
+ goto error;
+
+ if (n_div_ma) {
+ isl_mat_free(res->div);
+ res->div = isl_mat_copy(ma->p[0]->ls->div);
+ res->div = isl_mat_add_zero_cols(res->div, n_div_ls);
+ res->div = isl_mat_add_rows(res->div, n_div_ls);
+ if (!res->div)
+ goto error;
+ }
+
+ isl_int_init(f);
+ isl_int_init(c1);
+ isl_int_init(c2);
+ isl_int_init(g);
+
+ for (i = 0; i < ls->div->n_row; ++i) {
+ if (isl_int_is_zero(ls->div->row[i][0])) {
+ isl_int_set_si(res->div->row[n_div_ma + i][0], 0);
+ continue;
+ }
+ isl_seq_preimage(res->div->row[n_div_ma + i], ls->div->row[i],
+ ma, 0, 0, n_div_ma, n_div_ls, f, c1, c2, g, 1);
+ normalize_div(res, n_div_ma + i);
+ }
+
+ isl_int_clear(f);
+ isl_int_clear(c1);
+ isl_int_clear(c2);
+ isl_int_clear(g);
+
+ isl_local_space_free(ls);
+ isl_multi_aff_free(ma);
+ return res;
+error:
+ isl_local_space_free(ls);
+ isl_multi_aff_free(ma);
+ isl_local_space_free(res);
+ return NULL;
+}