add isl_aff_mod_val
[platform/upstream/isl.git] / isl_local_space.c
index 5c963d8..42f3e9f 100644 (file)
@@ -1,11 +1,13 @@
 /*
  * Copyright 2011      INRIA Saclay
+ * Copyright 2012      Ecole Normale Superieure
  *
- * Use of this software is governed by the GNU LGPLv2.1 license
+ * Use of this software is governed by the MIT license
  *
  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
  * 91893 Orsay, France
+ * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
  */
 
 #include <isl_ctx_private.h>
@@ -27,7 +29,7 @@ __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
        isl_ctx *ctx;
        isl_local_space *ls = NULL;
 
-       if (!dim)
+       if (!dim || !div)
                goto error;
 
        ctx = isl_space_get_ctx(dim);
@@ -41,6 +43,7 @@ __isl_give isl_local_space *isl_local_space_alloc_div(__isl_take isl_space *dim,
 
        return ls;
 error:
+       isl_mat_free(div);
        isl_space_free(dim);
        isl_local_space_free(ls);
        return NULL;
@@ -175,12 +178,32 @@ unsigned isl_local_space_offset(__isl_keep isl_local_space *ls,
        }
 }
 
+/* Does the given dimension have a name?
+ */
+int isl_local_space_has_dim_name(__isl_keep isl_local_space *ls,
+       enum isl_dim_type type, unsigned pos)
+{
+       return ls ? isl_space_has_dim_name(ls->dim, type, pos) : -1;
+}
+
 const char *isl_local_space_get_dim_name(__isl_keep isl_local_space *ls,
        enum isl_dim_type type, unsigned pos)
 {
        return ls ? isl_space_get_dim_name(ls->dim, type, pos) : NULL;
 }
 
+int isl_local_space_has_dim_id(__isl_keep isl_local_space *ls,
+       enum isl_dim_type type, unsigned pos)
+{
+       return ls ? isl_space_has_dim_id(ls->dim, type, pos) : -1;
+}
+
+__isl_give isl_id *isl_local_space_get_dim_id(__isl_keep isl_local_space *ls,
+       enum isl_dim_type type, unsigned pos)
+{
+       return ls ? isl_space_get_dim_id(ls->dim, type, pos) : NULL;
+}
+
 __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
        int pos)
 {
@@ -196,6 +219,9 @@ __isl_give isl_aff *isl_local_space_get_div(__isl_keep isl_local_space *ls,
        if (isl_int_is_zero(ls->div->row[pos][0]))
                isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
                        "expression of div unknown", return NULL);
+       if (!isl_local_space_is_set(ls))
+               isl_die(isl_local_space_get_ctx(ls), isl_error_invalid,
+                       "cannot represent divs of map spaces", return NULL);
 
        aff = isl_aff_alloc(isl_local_space_copy(ls));
        if (!aff)
@@ -380,23 +406,87 @@ static void expand_row(__isl_keep isl_mat *dst, int d,
 
 /* Compare (known) divs.
  * Return non-zero if at least one of the two divs is unknown.
+ * In particular, if both divs are unknown, we respect their
+ * current order.  Otherwise, we sort the known div after the unknown
+ * div only if the known div depends on the unknown div.
  */
-static int cmp_row(__isl_keep isl_mat *div, int i, int j)
+static int cmp_row(isl_int *row_i, isl_int *row_j, int i, int j,
+       unsigned n_row, unsigned n_col)
 {
        int li, lj;
+       int unknown_i, unknown_j;
 
-       if (isl_int_is_zero(div->row[j][0]))
-               return -1;
-       if (isl_int_is_zero(div->row[i][0]))
-               return 1;
+       unknown_i = isl_int_is_zero(row_i[0]);
+       unknown_j = isl_int_is_zero(row_j[0]);
+
+       if (unknown_i && unknown_j)
+               return i - j;
 
-       li = isl_seq_last_non_zero(div->row[i], div->n_col);
-       lj = isl_seq_last_non_zero(div->row[j], div->n_col);
+       if (unknown_i)
+               li = n_col - n_row + i;
+       else
+               li = isl_seq_last_non_zero(row_i, n_col);
+       if (unknown_j)
+               lj = n_col - n_row + j;
+       else
+               lj = isl_seq_last_non_zero(row_j, n_col);
 
        if (li != lj)
                return li - lj;
 
-       return isl_seq_cmp(div->row[i], div->row[j], div->n_col);
+       return isl_seq_cmp(row_i, row_j, n_col);
+}
+
+/* Call cmp_row for divs in a matrix.
+ */
+int isl_mat_cmp_div(__isl_keep isl_mat *div, int i, int j)
+{
+       return cmp_row(div->row[i], div->row[j], i, j, div->n_row, div->n_col);
+}
+
+/* Call cmp_row for divs in a basic map.
+ */
+static int bmap_cmp_row(__isl_keep isl_basic_map *bmap, int i, int j,
+       unsigned total)
+{
+       return cmp_row(bmap->div[i], bmap->div[j], i, j, bmap->n_div, total);
+}
+
+/* Sort the divs in "bmap".
+ *
+ * We first make sure divs are placed after divs on which they depend.
+ * Then we perform a simple insertion sort based on the same ordering
+ * that is used in isl_merge_divs.
+ */
+__isl_give isl_basic_map *isl_basic_map_sort_divs(
+       __isl_take isl_basic_map *bmap)
+{
+       int i, j;
+       unsigned total;
+
+       bmap = isl_basic_map_order_divs(bmap);
+       if (!bmap)
+               return NULL;
+       if (bmap->n_div <= 1)
+               return bmap;
+
+       total = 2 + isl_basic_map_total_dim(bmap);
+       for (i = 1; i < bmap->n_div; ++i) {
+               for (j = i - 1; j >= 0; --j) {
+                       if (bmap_cmp_row(bmap, j, j + 1, total) <= 0)
+                               break;
+                       isl_basic_map_swap_div(bmap, j, j + 1);
+               }
+       }
+
+       return bmap;
+}
+
+/* Sort the divs in the basic maps of "map".
+ */
+__isl_give isl_map *isl_map_sort_divs(__isl_take isl_map *map)
+{
+       return isl_map_inline_foreach_basic_map(map, &isl_basic_map_sort_divs);
 }
 
 /* Combine the two lists of divs into a single list.
@@ -412,8 +502,12 @@ __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
 {
        int i, j, k;
        isl_mat *div = NULL;
-       unsigned d = div1->n_col - div1->n_row;
+       unsigned d;
+
+       if (!div1 || !div2)
+               return NULL;
 
+       d = div1->n_col - div1->n_row;
        div = isl_mat_alloc(div1->ctx, 1 + div1->n_row + div2->n_row,
                                d + div1->n_row + div2->n_row);
        if (!div)
@@ -425,7 +519,7 @@ __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
                expand_row(div, k, div1, i, exp1);
                expand_row(div, k + 1, div2, j, exp2);
 
-               cmp = cmp_row(div, k, k + 1);
+               cmp = isl_mat_cmp_div(div, k, k + 1);
                if (cmp == 0) {
                        exp1[i++] = k;
                        exp2[j++] = k;
@@ -451,6 +545,80 @@ __isl_give isl_mat *isl_merge_divs(__isl_keep isl_mat *div1,
        return div;
 }
 
+/* Swap divs "a" and "b" in "ls".
+ */
+__isl_give isl_local_space *isl_local_space_swap_div(
+       __isl_take isl_local_space *ls, int a, int b)
+{
+       int offset;
+
+       ls = isl_local_space_cow(ls);
+       if (!ls)
+               return NULL;
+       if (a < 0 || a >= ls->div->n_row || b < 0 || b >= 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));
+       offset = ls->div->n_col - ls->div->n_row;
+       ls->div = isl_mat_swap_cols(ls->div, offset + a, offset + b);
+       ls->div = isl_mat_swap_rows(ls->div, a, b);
+       if (!ls->div)
+               return isl_local_space_free(ls);
+       return ls;
+}
+
+/* Construct a local space that contains all the divs in either
+ * "ls1" or "ls2".
+ */
+__isl_give isl_local_space *isl_local_space_intersect(
+       __isl_take isl_local_space *ls1, __isl_take isl_local_space *ls2)
+{
+       isl_ctx *ctx;
+       int *exp1 = NULL;
+       int *exp2 = NULL;
+       isl_mat *div;
+
+       if (!ls1 || !ls2)
+               goto error;
+
+       ctx = isl_local_space_get_ctx(ls1);
+       if (!isl_space_is_equal(ls1->dim, ls2->dim))
+               isl_die(ctx, isl_error_invalid,
+                       "spaces should be identical", goto error);
+
+       if (ls2->div->n_row == 0) {
+               isl_local_space_free(ls2);
+               return ls1;
+       }
+
+       if (ls1->div->n_row == 0) {
+               isl_local_space_free(ls1);
+               return ls2;
+       }
+
+       exp1 = isl_alloc_array(ctx, int, ls1->div->n_row);
+       exp2 = isl_alloc_array(ctx, int, ls2->div->n_row);
+       if (!exp1 || !exp2)
+               goto error;
+
+       div = isl_merge_divs(ls1->div, ls2->div, exp1, exp2);
+       if (!div)
+               goto error;
+
+       free(exp1);
+       free(exp2);
+       isl_local_space_free(ls2);
+       isl_mat_free(ls1->div);
+       ls1->div = div;
+
+       return ls1;
+error:
+       free(exp1);
+       free(exp2);
+       isl_local_space_free(ls1);
+       isl_local_space_free(ls2);
+       return NULL;
+}
+
 int isl_local_space_divs_known(__isl_keep isl_local_space *ls)
 {
        int i;
@@ -479,6 +647,21 @@ __isl_give isl_local_space *isl_local_space_domain(
        return ls;
 }
 
+__isl_give isl_local_space *isl_local_space_range(
+       __isl_take isl_local_space *ls)
+{
+       ls = isl_local_space_drop_dims(ls, isl_dim_in,
+                                       0, isl_local_space_dim(ls, isl_dim_in));
+       ls = isl_local_space_cow(ls);
+       if (!ls)
+               return NULL;
+
+       ls->dim = isl_space_range(ls->dim);
+       if (!ls->dim)
+               return isl_local_space_free(ls);
+       return ls;
+}
+
 /* Construct a local space for a map that has the given local
  * space as domain and that has a zero-dimensional range.
  */
@@ -570,14 +753,15 @@ error:
        return NULL;
 }
 
-/* Plug in "subs" for dimension "type", "pos" in the integer divisions
- * of "ls".
+/* 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 with a non-zero coefficient for i,
+ * Any integer division starting at "first" with a non-zero coefficient for i,
  *
  *     floor((a i + g)/m)
  *
@@ -585,38 +769,35 @@ error:
  *
  *     floor((a f + d g)/(m d))
  */
-__isl_give isl_local_space *isl_local_space_substitute(
+__isl_give isl_local_space *isl_local_space_substitute_seq(
        __isl_take isl_local_space *ls,
-       enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
+       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 || !subs)
+       if (!ls)
+               return NULL;
+       ls->div = isl_mat_cow(ls->div);
+       if (!ls->div)
                return isl_local_space_free(ls);
 
-       if (!isl_space_is_equal(ls->dim, subs->ls->dim))
+       if (first + n > ls->div->n_row)
                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));
+                       "index out of bounds", return isl_local_space_free(ls));
 
        pos += isl_local_space_offset(ls, type);
 
        isl_int_init(v);
-       for (i = 0; i < ls->div->n_row; ++i) {
+       for (i = first; i < ls->div->n_row; ++i) {
                if (isl_int_is_zero(ls->div->row[i][1 + pos]))
                        continue;
-               isl_int_set(v, ls->div->row[i][1 + pos]);
-               isl_int_set_si(ls->div->row[i][1 + pos], 0);
-               isl_seq_combine(ls->div->row[i] + 1,
-                               subs->v->el[0], ls->div->row[i] + 1,
-                               v, subs->v->el + 1, subs->v->size - 1);
-               isl_int_mul(ls->div->row[i][0],
-                           ls->div->row[i][0], subs->v->el[0]);
+               isl_seq_substitute(ls->div->row[i], pos, subs,
+                       ls->div->n_col, subs_len, v);
                normalize_div(ls, i);
        }
        isl_int_clear(v);
@@ -624,6 +805,41 @@ __isl_give isl_local_space *isl_local_space_substitute(
        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)
 {
@@ -783,3 +999,129 @@ int *isl_local_space_get_active(__isl_keep isl_local_space *ls, isl_int *l)
 
        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;
+}