isl_map_gist: ensure divs of map are known
[platform/upstream/isl.git] / isl_map.c
index 5ef0d1b..281a92b 100644 (file)
--- a/isl_map.c
+++ b/isl_map.c
@@ -1746,11 +1746,32 @@ static struct isl_basic_map *normalize_constraints(struct isl_basic_map *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, int pos, unsigned n)
+       struct isl_basic_map *bmap, unsigned pos, unsigned n)
 {
        int d;
        int i, j, k;
@@ -1763,10 +1784,13 @@ struct isl_basic_map *isl_basic_map_eliminate_vars(
        total = isl_basic_map_total_dim(bmap);
 
        bmap = isl_basic_map_cow(bmap);
-       for (d = pos + n - 1; d >= pos; --d) {
+       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;
@@ -2840,11 +2864,13 @@ struct isl_basic_map *isl_basic_map_overlying_set(
        struct isl_basic_map *bmap;
        struct isl_ctx *ctx;
        unsigned total;
-       int i, k;
+       int i;
 
        if (!bset || !like)
                goto error;
        ctx = bset->ctx;
+       isl_assert(ctx, bset->n_div == 0, goto error);
+       isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
        isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
                        goto error);
        if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
@@ -2860,6 +2886,7 @@ struct isl_basic_map *isl_basic_map_overlying_set(
        bmap->dim = isl_dim_copy(like->dim);
        if (!bmap->dim)
                goto error;
+       bmap->n_div = like->n_div;
        bmap->extra += like->n_div;
        if (bmap->extra) {
                unsigned ltotal;
@@ -2874,17 +2901,17 @@ struct isl_basic_map *isl_basic_map_overlying_set(
                                                bmap->extra);
                if (!bmap->div)
                        goto error;
+               for (i = 0; i < bmap->extra; ++i)
+                       bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
+               for (i = 0; i < like->n_div; ++i) {
+                       isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
+                       isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
+               }
                bmap = isl_basic_map_extend_constraints(bmap, 
                                                        0, 2 * like->n_div);
-               for (i = 0; i < like->n_div; ++i) {
-                       k = isl_basic_map_alloc_div(bmap);
-                       if (k < 0)
+               for (i = 0; i < like->n_div; ++i)
+                       if (add_div_constraints(bmap, i) < 0)
                                goto error;
-                       isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
-                       isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
-                       if (add_div_constraints(bmap, k) < 0)
-                               goto error;
-               }
        }
        isl_basic_map_free(like);
        bmap = isl_basic_map_finalize(bmap);
@@ -2940,7 +2967,6 @@ struct isl_set *isl_map_underlying_set(struct isl_map *map)
 {
        int i;
 
-       map = isl_map_align_divs(map);
        map = isl_map_cow(map);
        if (!map)
                return NULL;
@@ -2948,6 +2974,9 @@ struct isl_set *isl_map_underlying_set(struct isl_map *map)
        if (!map->dim)
                goto error;
 
+       for (i = 1; i < map->n; ++i)
+               isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
+                               goto error);
        for (i = 0; i < map->n; ++i) {
                map->p[i] = (struct isl_basic_map *)
                                isl_basic_map_underlying_set(map->p[i]);
@@ -3486,11 +3515,22 @@ error:
 
 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
 {
+       int i;
+       unsigned off;
+
        if (!bmap)
                return NULL;
-       if (bmap->n_div == 0)
-               return isl_map_from_basic_map(bmap);
-       return isl_pip_basic_map_compute_divs(bmap);
+       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);
+               isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
+                               goto error);
+       }
+       return isl_map_from_basic_map(bmap);
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
 }
 
 struct isl_map *isl_map_compute_divs(struct isl_map *map)
@@ -4238,17 +4278,24 @@ static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
        return bmap;
 }
 
+/* Look for a div in dst that corresponds to the div "div" in src.
+ * The divs before "div" in src and dst are assumed to be the same.
+ * 
+ * Returns -1 if no corresponding div was found and the position
+ * of the corresponding div in dst otherwise.
+ */
 static int find_div(struct isl_basic_map *dst,
                        struct isl_basic_map *src, unsigned div)
 {
        int i;
 
-       unsigned total = isl_basic_map_total_dim(src);
+       unsigned total = isl_dim_total(src->dim);
 
-       for (i = 0; i < dst->n_div; ++i)
-               if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
-                   isl_seq_first_non_zero(dst->div[i]+1+1+total,
-                                               dst->n_div - src->n_div) == -1)
+       isl_assert(dst->ctx, div <= dst->n_div, return -1);
+       for (i = div; i < dst->n_div; ++i)
+               if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
+                   isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
+                                               dst->n_div - div) == -1)
                        return i;
        return -1;
 }
@@ -4257,7 +4304,7 @@ struct isl_basic_map *isl_basic_map_align_divs(
                struct isl_basic_map *dst, struct isl_basic_map *src)
 {
        int i;
-       unsigned total = isl_basic_map_total_dim(src);
+       unsigned total = isl_dim_total(src->dim);
 
        if (!dst || !src)
                goto error;
@@ -4276,9 +4323,8 @@ struct isl_basic_map *isl_basic_map_align_divs(
                        j = isl_basic_map_alloc_div(dst);
                        if (j < 0)
                                goto error;
-                       isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
-                       isl_seq_clr(dst->div[j]+1+1+total,
-                                                   dst->n_div - src->n_div);
+                       isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
+                       isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
                        if (add_div_constraints(dst, j) < 0)
                                goto error;
                }
@@ -4599,6 +4645,7 @@ int isl_basic_set_compare_at(struct isl_basic_set *bset1,
        bmap1 = isl_basic_map_extend(bmap1, nparam,
                        pos, (dim1 - pos) + (dim2 - pos),
                        bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
+       bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
        if (!bmap1)
                goto error;
        total = isl_basic_map_total_dim(bmap1);
@@ -4609,7 +4656,6 @@ int isl_basic_set_compare_at(struct isl_basic_set *bset1,
        isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
        if (!obj)
                goto error;
-       bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
        isl_int_init(num);
        isl_int_init(den);
        res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
@@ -4959,6 +5005,9 @@ error:
        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;
@@ -4967,6 +5016,7 @@ struct isl_map *isl_map_gist(struct isl_map *map, struct isl_basic_map *context)
        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) {