add ISL_CTX_{GET,SET}_STR_DEF macros
[platform/upstream/isl.git] / isl_map_simplify.c
index c07b211..90b36f4 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  * 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, K.U.Leuven, Departement
  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
@@ -372,6 +372,38 @@ struct isl_basic_set *isl_basic_set_normalize_constraints(
                (struct isl_basic_map *)bset);
 }
 
+/* Remove any common factor in numerator and denominator of the div expression,
+ * not taking into account the constant term.
+ * That is, if the div is of the form
+ *
+ *     floor((a + m f(x))/(m d))
+ *
+ * then replace it by
+ *
+ *     floor((floor(a/m) + f(x))/d)
+ *
+ * The difference {a/m}/d in the argument satisfies 0 <= {a/m}/d < 1/d
+ * and can therefore not influence the result of the floor.
+ */
+static void normalize_div_expression(__isl_keep isl_basic_map *bmap, int div)
+{
+       unsigned total = isl_basic_map_total_dim(bmap);
+       isl_ctx *ctx = bmap->ctx;
+
+       if (isl_int_is_zero(bmap->div[div][0]))
+               return;
+       isl_seq_gcd(bmap->div[div] + 2, total, &ctx->normalize_gcd);
+       isl_int_gcd(ctx->normalize_gcd, ctx->normalize_gcd, bmap->div[div][0]);
+       if (isl_int_is_one(ctx->normalize_gcd))
+               return;
+       isl_int_fdiv_q(bmap->div[div][1], bmap->div[div][1],
+                       ctx->normalize_gcd);
+       isl_int_divexact(bmap->div[div][0], bmap->div[div][0],
+                       ctx->normalize_gcd);
+       isl_seq_scale_down(bmap->div[div] + 2, bmap->div[div] + 2,
+                       ctx->normalize_gcd, total);
+}
+
 /* Remove any common factor in numerator and denominator of a div expression,
  * not taking into account the constant term.
  * That is, look for any div of the form
@@ -389,28 +421,14 @@ static __isl_give isl_basic_map *normalize_div_expressions(
        __isl_take isl_basic_map *bmap)
 {
        int i;
-       isl_int gcd;
-       unsigned total = isl_basic_map_total_dim(bmap);
 
        if (!bmap)
                return NULL;
        if (bmap->n_div == 0)
                return bmap;
 
-       isl_int_init(gcd);
-       for (i = 0; i < bmap->n_div; ++i) {
-               if (isl_int_is_zero(bmap->div[i][0]))
-                       continue;
-               isl_seq_gcd(bmap->div[i] + 2, total, &gcd);
-               isl_int_gcd(gcd, gcd, bmap->div[i][0]);
-               if (isl_int_is_one(gcd))
-                       continue;
-               isl_int_fdiv_q(bmap->div[i][1], bmap->div[i][1], gcd);
-               isl_int_divexact(bmap->div[i][0], bmap->div[i][0], gcd);
-               isl_seq_scale_down(bmap->div[i] + 2, bmap->div[i] + 2, gcd,
-                                       total);
-       }
-       isl_int_clear(gcd);
+       for (i = 0; i < bmap->n_div; ++i)
+               normalize_div_expression(bmap, i);
 
        return bmap;
 }
@@ -463,10 +481,11 @@ static void eliminate_var_using_equality(struct isl_basic_map *bmap,
                 * and we can keep the definition as long as the result
                 * is still ordered.
                 */
-               if (last_div == -1 || (keep_divs && last_div < k))
+               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
+                       normalize_div_expression(bmap, k);
+               } else
                        isl_seq_clr(bmap->div[k], 1 + total);
                ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
        }
@@ -626,6 +645,8 @@ struct isl_basic_map *isl_basic_map_gauss(
                        isl_int_set_si(bmap->div[div][1+1+last_var], 0);
                        isl_int_set(bmap->div[div][0],
                                    bmap->eq[done][1+last_var]);
+                       if (progress)
+                               *progress = 1;
                        ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
                }
        }
@@ -696,6 +717,7 @@ static struct isl_basic_map *remove_duplicate_divs(
        unsigned total;
        struct isl_ctx *ctx;
 
+       bmap = isl_basic_map_order_divs(bmap);
        if (!bmap || bmap->n_div <= 1)
                return bmap;
 
@@ -745,7 +767,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, 0);
+               eliminate_div(bmap, eq.data, l, 1);
                isl_int_set_si(eq.data[1+total_var+k], 0);
                isl_int_set_si(eq.data[1+total_var+l], 0);
        }
@@ -1533,7 +1555,7 @@ __isl_give isl_basic_map *isl_basic_map_eliminate(
 
        space = isl_basic_map_get_space(bmap);
        bmap = isl_basic_map_project_out(bmap, type, first, n);
-       bmap = isl_basic_map_insert(bmap, type, first, n);
+       bmap = isl_basic_map_insert_dims(bmap, type, first, n);
        bmap = isl_basic_map_reset_space(bmap, space);
        return bmap;
 error:
@@ -1541,6 +1563,13 @@ error:
        return NULL;
 }
 
+__isl_give isl_basic_set *isl_basic_set_eliminate(
+       __isl_take isl_basic_set *bset,
+       enum isl_dim_type type, unsigned first, unsigned n)
+{
+       return isl_basic_map_eliminate(bset, type, first, n);
+}
+
 /* Don't assume equalities are in order, because align_divs
  * may have changed the order of the divs.
  */
@@ -1987,11 +2016,49 @@ error:
        return NULL;
 }
 
+/* Return a map that has the same intersection with "context" as "map"
+ * and that as "simple" as possible.
+ *
+ * If "map" is already the universe, then we cannot make it any simpler.
+ * Similarly, if "context" is the universe, then we cannot exploit it
+ * to simplify "map"
+ * If "map" and "context" are identical to each other, then we can
+ * return the corresponding universe.
+ *
+ * If none of these cases apply, we have to work a bit harder.
+ */
 static __isl_give isl_map *map_gist(__isl_take isl_map *map,
        __isl_take isl_map *context)
 {
+       int equal;
+       int is_universe;
+
+       is_universe = isl_map_plain_is_universe(map);
+       if (is_universe >= 0 && !is_universe)
+               is_universe = isl_map_plain_is_universe(context);
+       if (is_universe < 0)
+               goto error;
+       if (is_universe) {
+               isl_map_free(context);
+               return map;
+       }
+
+       equal = isl_map_plain_is_equal(map, context);
+       if (equal < 0)
+               goto error;
+       if (equal) {
+               isl_map *res = isl_map_universe(isl_map_get_space(map));
+               isl_map_free(map);
+               isl_map_free(context);
+               return res;
+       }
+
        context = isl_map_compute_divs(context);
        return isl_map_gist_basic_map(map, isl_map_simple_hull(context));
+error:
+       isl_map_free(map);
+       isl_map_free(context);
+       return NULL;
 }
 
 __isl_give isl_map *isl_map_gist(__isl_take isl_map *map,
@@ -2141,12 +2208,23 @@ int isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
        __isl_keep isl_map *map2)
 {
        int i, j;
+       int disjoint;
+       int intersect;
 
        if (!map1 || !map2)
                return -1;
 
-       if (isl_map_plain_is_equal(map1, map2))
-               return 0;
+       disjoint = isl_map_plain_is_empty(map1);
+       if (disjoint < 0 || disjoint)
+               return disjoint;
+
+       disjoint = isl_map_plain_is_empty(map2);
+       if (disjoint < 0 || disjoint)
+               return disjoint;
+
+       intersect = isl_map_plain_is_equal(map1, map2);
+       if (intersect < 0 || intersect)
+               return intersect < 0 ? -1 : 0;
 
        for (i = 0; i < map1->n; ++i) {
                for (j = 0; j < map2->n; ++j) {
@@ -2159,6 +2237,46 @@ int isl_map_plain_is_disjoint(__isl_keep isl_map *map1,
        return 1;
 }
 
+/* Are "map1" and "map2" disjoint?
+ *
+ * They are disjoint if they are "obviously disjoint" or if one of them
+ * is empty.  Otherwise, they are not disjoint if one of them is universal.
+ * If none of these cases apply, we compute the intersection and see if
+ * the result is empty.
+ */
+int isl_map_is_disjoint(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
+{
+       int disjoint;
+       int intersect;
+       isl_map *test;
+
+       disjoint = isl_map_plain_is_disjoint(map1, map2);
+       if (disjoint < 0 || disjoint)
+               return disjoint;
+
+       disjoint = isl_map_is_empty(map1);
+       if (disjoint < 0 || disjoint)
+               return disjoint;
+
+       disjoint = isl_map_is_empty(map2);
+       if (disjoint < 0 || disjoint)
+               return disjoint;
+
+       intersect = isl_map_plain_is_universe(map1);
+       if (intersect < 0 || intersect)
+               return intersect < 0 ? -1 : 0;
+
+       intersect = isl_map_plain_is_universe(map2);
+       if (intersect < 0 || intersect)
+               return intersect < 0 ? -1 : 0;
+
+       test = isl_map_intersect(isl_map_copy(map1), isl_map_copy(map2));
+       disjoint = isl_map_is_empty(test);
+       isl_map_free(test);
+
+       return disjoint;
+}
+
 int isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
        __isl_keep isl_set *set2)
 {
@@ -2166,6 +2284,13 @@ int isl_set_plain_is_disjoint(__isl_keep isl_set *set1,
                                        (struct isl_map *)set2);
 }
 
+/* Are "set1" and "set2" disjoint?
+ */
+int isl_set_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
+{
+       return isl_map_is_disjoint(set1, set2);
+}
+
 int isl_set_fast_is_disjoint(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
 {
        return isl_set_plain_is_disjoint(set1, set2);
@@ -2572,7 +2697,7 @@ static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
 /* Remove divs that are not strictly needed.
  * In particular, if a div only occurs positively (or negatively)
  * in constraints, then it can simply be dropped.
- * Also, if a div occurs only occurs in two constraints and if moreover
+ * Also, if a div occurs in only two constraints and if moreover
  * those two constraints are opposite to each other, except for the constant
  * term and if the sum of the constant terms is such that for any value
  * of the other values, there is always at least one integer value of the
@@ -2580,6 +2705,10 @@ static struct isl_basic_map *coalesce_or_drop_more_redundant_divs(
  * the (absolute value) of the coefficent of the div in the constraints,
  * then we can also simply drop the div.
  *
+ * We skip divs that appear in equalities or in the definition of other divs.
+ * Divs that appear in the definition of other divs usually occur in at least
+ * 4 constraints, but the constraints may have been simplified.
+ *
  * If any divs are left after these simple checks then we move on
  * to more complicated cases in drop_more_redundant_divs.
  */
@@ -2606,6 +2735,11 @@ struct isl_basic_map *isl_basic_map_drop_redundant_divs(
                int defined;
 
                defined = !isl_int_is_zero(bmap->div[i][0]);
+               for (j = i; j < bmap->n_div; ++j)
+                       if (!isl_int_is_zero(bmap->div[j][1 + 1 + off + i]))
+                               break;
+               if (j < bmap->n_div)
+                       continue;
                for (j = 0; j < bmap->n_eq; ++j)
                        if (!isl_int_is_zero(bmap->eq[j][1 + off + i]))
                                break;