isl_basic_map_simplify: avoid removal of div definitions in eliminate_divs_eq
authorSven Verdoolaege <skimo@kotnet.org>
Wed, 21 Oct 2009 16:13:19 +0000 (18:13 +0200)
committerSven Verdoolaege <skimo@kotnet.org>
Wed, 21 Oct 2009 20:58:47 +0000 (22:58 +0200)
isl_map_simplify.c

index c004832..c9f997e 100644 (file)
@@ -346,28 +346,68 @@ struct isl_basic_set *isl_basic_set_normalize_constraints(
                (struct isl_basic_map *)bset);
 }
 
-static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
+/* Assumes divs have been ordered if keep_divs is set.
+ */
+static void eliminate_var_using_equality(struct isl_basic_map *bmap,
+       unsigned pos, isl_int *eq, int keep_divs, int *progress)
 {
-       int i;
-       unsigned pos = 1 + isl_dim_total(bmap->dim) + div;
-       unsigned len;
-       len = 1 + isl_basic_map_total_dim(bmap);
+       unsigned total;
+       int k;
+       int last_div;
 
-       for (i = 0; i < bmap->n_eq; ++i)
-               if (bmap->eq[i] != eq)
-                       isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
+       total = isl_basic_map_total_dim(bmap);
+       last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
+                                               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 (i = 0; i < bmap->n_ineq; ++i)
-               isl_seq_elim(bmap->ineq[i], eq, pos, len, 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);
+               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+       }
 
-       /* We need to be careful about circular definitions,
-        * so for now we just remove the definitions of other divs that
-        * depend on this div and (possibly) recompute them later.
-        */
-       for (i = 0; i < bmap->n_div; ++i)
-               if (!isl_int_is_zero(bmap->div[i][0]) &&
-                   !isl_int_is_zero(bmap->div[i][1 + pos]))
-                       isl_seq_clr(bmap->div[i], 1 + len);
+       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;
+               /* We need to be careful about circular definitions,
+                * so for now we just remove the definition of div k
+                * if the equality contains any divs.
+                * If keep_divs is set, then the divs have been ordered
+                * and we can keep the definition as long as the result
+                * is still ordered.
+                */
+               if (last_div == -1 || (keep_divs && last_div < k))
+                       isl_seq_elim(bmap->div[k]+1, eq,
+                                       1+pos, 1+total, &bmap->div[k][0]);
+               else
+                       isl_seq_clr(bmap->div[k], 1 + total);
+               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
+       }
+}
+
+/* Assumes divs have been ordered if keep_divs is set.
+ */
+static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq,
+       unsigned div, int keep_divs)
+{
+       unsigned pos = isl_dim_total(bmap->dim) + div;
+
+       eliminate_var_using_equality(bmap, pos, eq, keep_divs, NULL);
 
        isl_basic_map_drop_div(bmap, div);
 }
@@ -382,6 +422,8 @@ static struct isl_basic_map *eliminate_divs_eq(
        int modified = 0;
        unsigned off;
 
+       bmap = isl_basic_map_order_divs(bmap);
+
        if (!bmap)
                return NULL;
 
@@ -394,7 +436,7 @@ static struct isl_basic_map *eliminate_divs_eq(
                                continue;
                        modified = 1;
                        *progress = 1;
-                       eliminate_div(bmap, bmap->eq[i], d);
+                       eliminate_div(bmap, bmap->eq[i], d, 1);
                        isl_basic_map_drop_equality(bmap, i);
                        break;
                }
@@ -442,60 +484,6 @@ static struct isl_basic_map *eliminate_divs_ineq(
        return bmap;
 }
 
-/* Assumes divs have been ordered if keep_divs is set.
- */
-static void eliminate_var_using_equality(struct isl_basic_map *bmap,
-       unsigned pos, isl_int *eq, int keep_divs, int *progress)
-{
-       unsigned total;
-       int k;
-       int last_div;
-
-       total = isl_basic_map_total_dim(bmap);
-       last_div = isl_seq_last_non_zero(eq + 1 + isl_dim_total(bmap->dim),
-                                               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);
-               ISL_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;
-               /* We need to be careful about circular definitions,
-                * so for now we just remove the definition of div k
-                * if the equality contains any divs.
-                * If keep_divs is set, then the divs have been ordered
-                * and we can keep the definition as long as the result
-                * is still ordered.
-                */
-               if (last_div == -1 || (keep_divs && last_div < k))
-                       isl_seq_elim(bmap->div[k]+1, eq,
-                                       1+pos, 1+total, &bmap->div[k][0]);
-               else
-                       isl_seq_clr(bmap->div[k], 1 + total);
-               ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
-       }
-}
-
 struct isl_basic_map *isl_basic_map_gauss(
        struct isl_basic_map *bmap, int *progress)
 {
@@ -655,7 +643,7 @@ static struct isl_basic_map *remove_duplicate_divs(
                k = elim_for[l] - 1;
                isl_int_set_si(eq.data[1+total_var+k], -1);
                isl_int_set_si(eq.data[1+total_var+l], 1);
-               eliminate_div(bmap, eq.data, l);
+               eliminate_div(bmap, eq.data, l, 0);
                isl_int_set_si(eq.data[1+total_var+k], 0);
                isl_int_set_si(eq.data[1+total_var+l], 0);
        }