rudimentary support for asynchronous termination of computations
[platform/upstream/isl.git] / isl_transitive_closure.c
index 5b97a07..629eb74 100644 (file)
@@ -8,12 +8,13 @@
  * 91893 Orsay, France 
  */
 
-#include "isl_map.h"
-#include "isl_map_private.h"
-#include "isl_seq.h"
+#include <isl_ctx_private.h>
+#include <isl_map_private.h>
+#include <isl/map.h>
+#include <isl/seq.h>
 #include <isl_dim_private.h>
-#include <isl_lp.h>
-#include <isl_union_map.h>
+#include <isl/lp.h>
+#include <isl/union_map.h>
 #include <isl_mat_private.h>
 
 int isl_map_is_transitively_closed(__isl_keep isl_map *map)
@@ -112,8 +113,8 @@ static int check_power_exactness(__isl_take isl_map *map,
        isl_map *app_1;
        isl_map *app_2;
 
-       map = isl_map_add(map, isl_dim_in, 1);
-       map = isl_map_add(map, isl_dim_out, 1);
+       map = isl_map_add_dims(map, isl_dim_in, 1);
+       map = isl_map_add_dims(map, isl_dim_out, 1);
        map = set_path_length(map, 1, 1);
 
        app_1 = set_path_length(isl_map_copy(app), 1, 1);
@@ -322,6 +323,9 @@ error:
  *     variables are non-zero and if moreover the parametric constant
  *     can never attain positive values.
  * Return IMPURE otherwise.
+ *
+ * If div_purity is NULL then we are dealing with a non-parametric set
+ * and so the constraint is obviously PURE_VAR.
  */
 static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
        int eq)
@@ -333,6 +337,9 @@ static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
        int i;
        int p = 0, v = 0;
 
+       if (!div_purity)
+               return PURE_VAR;
+
        n_div = isl_basic_set_dim(bset, isl_dim_div);
        d = isl_basic_set_dim(bset, isl_dim_set);
        nparam = isl_basic_set_dim(bset, isl_dim_param);
@@ -415,7 +422,7 @@ static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
  * check if setting the length to zero results in only the identity
  * mapping.
  */
-int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
+static int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
 {
        isl_basic_map *test = NULL;
        isl_basic_map *id = NULL;
@@ -429,7 +436,7 @@ int empty_path_is_identity(__isl_keep isl_basic_map *path, unsigned pos)
                goto error;
        isl_seq_clr(test->eq[k], 1 + isl_basic_map_total_dim(test));
        isl_int_set_si(test->eq[k][pos], 1);
-       id = isl_basic_map_identity(isl_dim_domain(isl_basic_map_get_dim(path)));
+       id = isl_basic_map_identity(isl_basic_map_get_dim(path));
        is_id = isl_basic_map_is_equal(test, id);
        isl_basic_map_free(test);
        isl_basic_map_free(id);
@@ -439,9 +446,13 @@ error:
        return -1;
 }
 
-__isl_give isl_basic_map *add_delta_constraints(__isl_take isl_basic_map *path,
+/* If any of the constraints is found to be impure then this function
+ * sets *impurity to 1.
+ */
+static __isl_give isl_basic_map *add_delta_constraints(
+       __isl_take isl_basic_map *path,
        __isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
-       unsigned d, int *div_purity, int eq)
+       unsigned d, int *div_purity, int eq, int *impurity)
 {
        int i, k;
        int n = eq ? delta->n_eq : delta->n_ineq;
@@ -455,6 +466,8 @@ __isl_give isl_basic_map *add_delta_constraints(__isl_take isl_basic_map *path,
                int p = purity(delta, delta_c[i], div_purity, eq);
                if (p < 0)
                        goto error;
+               if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
+                       *impurity = 1;
                if (p == IMPURE)
                        continue;
                if (eq && p != MIXED) {
@@ -504,7 +517,7 @@ error:
  *
  * In particular, let delta be defined as
  *
- *     \delta = [p] -> { [x] : A x + a >= and B p + b >= 0 and
+ *     \delta = [p] -> { [x] : A x + a >= and B p + b >= 0 and
  *                             C x + C'p + c >= 0 and
  *                             D x + D'p + d >= 0 }
  *
@@ -531,6 +544,17 @@ error:
  * parameter dependent and others.  Constraints containing
  * any of the other existentially quantified variables are removed.
  * This is safe, but leads to an additional overapproximation.
+ *
+ * If there are any impure constraints, then we also eliminate
+ * the parameters from \delta, resulting in a set
+ *
+ *     \delta' = { [x] : E x + e >= 0 }
+ *
+ * and add the constraints
+ *
+ *                     E f + k e >= 0
+ *
+ * to the constructed relation.
  */
 static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
        __isl_take isl_basic_set *delta)
@@ -543,6 +567,7 @@ static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
        int i, k;
        int is_id;
        int *div_purity = NULL;
+       int impurity = 0;
 
        if (!delta)
                goto error;
@@ -574,8 +599,26 @@ static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
        if (!div_purity)
                goto error;
 
-       path = add_delta_constraints(path, delta, off, nparam, d, div_purity, 1);
-       path = add_delta_constraints(path, delta, off, nparam, d, div_purity, 0);
+       path = add_delta_constraints(path, delta, off, nparam, d,
+                                    div_purity, 1, &impurity);
+       path = add_delta_constraints(path, delta, off, nparam, d,
+                                    div_purity, 0, &impurity);
+       if (impurity) {
+               isl_dim *dim = isl_basic_set_get_dim(delta);
+               delta = isl_basic_set_project_out(delta,
+                                                 isl_dim_param, 0, nparam);
+               delta = isl_basic_set_add(delta, isl_dim_param, nparam);
+               delta = isl_basic_set_reset_dim(delta, dim);
+               if (!delta)
+                       goto error;
+               path = isl_basic_map_extend_constraints(path, delta->n_eq,
+                                                       delta->n_ineq + 1);
+               path = add_delta_constraints(path, delta, off, nparam, d,
+                                            NULL, 1, &impurity);
+               path = add_delta_constraints(path, delta, off, nparam, d,
+                                            NULL, 0, &impurity);
+               path = isl_basic_map_gauss(path, NULL);
+       }
 
        is_id = empty_path_is_identity(path, off + d);
        if (is_id < 0)
@@ -596,8 +639,7 @@ static __isl_give isl_map *path_along_delta(__isl_take isl_dim *dim,
                isl_dim_free(dim);
                return isl_map_from_basic_map(path);
        }
-       return isl_basic_map_union(path,
-                               isl_basic_map_identity(isl_dim_domain(dim)));
+       return isl_basic_map_union(path, isl_basic_map_identity(dim));
 error:
        free(div_purity);
        isl_dim_free(dim);
@@ -712,7 +754,7 @@ static __isl_give isl_map *construct_extended_path(__isl_take isl_dim *dim,
 
        d = isl_map_dim(map, isl_dim_in);
 
-       path = isl_map_identity(isl_dim_domain(isl_dim_copy(dim)));
+       path = isl_map_identity(isl_dim_copy(dim));
 
        steps = isl_mat_alloc(map->ctx, map->n, d);
        if (!steps)
@@ -822,14 +864,14 @@ static __isl_give isl_map *construct_component(__isl_take isl_dim *dim,
                isl_dim_free(dim);
 
                map = isl_map_copy(map);
-               map = isl_map_add(map, isl_dim_in, 1);
-               map = isl_map_add(map, isl_dim_out, 1);
+               map = isl_map_add_dims(map, isl_dim_in, 1);
+               map = isl_map_add_dims(map, isl_dim_out, 1);
                map = set_path_length(map, 1, 1);
                return map;
        }
        app = isl_map_from_domain_and_range(domain, range);
-       app = isl_map_add(app, isl_dim_in, 1);
-       app = isl_map_add(app, isl_dim_out, 1);
+       app = isl_map_add_dims(app, isl_dim_in, 1);
+       app = isl_map_add_dims(app, isl_dim_out, 1);
 
        path = construct_extended_path(isl_dim_copy(dim), map,
                                        exact && *exact ? &project : NULL);
@@ -884,7 +926,7 @@ static __isl_give isl_map *q_closure(__isl_take isl_dim *dim,
        isl_map *map;
        isl_map *app;
 
-       dom = isl_set_add(dom, isl_dim_set, 1);
+       dom = isl_set_add_dims(dom, isl_dim_set, 1);
        app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
        map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
        path = construct_extended_path(dim, map, &project);
@@ -1119,7 +1161,7 @@ static __isl_give isl_map *compute_incremental(
 
        if (!left || !right)
                rtc = isl_map_union(isl_map_copy(tc),
-                       isl_map_identity(isl_dim_domain(isl_map_get_dim(tc))));
+                                   isl_map_identity(isl_map_get_dim(tc)));
        if (!right)
                qc = isl_map_apply_range(rtc, qc);
        if (!left)
@@ -1973,6 +2015,7 @@ static __isl_give isl_map *construct_power_components(__isl_take isl_dim *dim,
                }
                path_comp = floyd_warshall(isl_dim_copy(dim),
                                                comp, exact, project);
+               path_comp = anonymize(path_comp);
                path_comb = isl_map_apply_range(isl_map_copy(path),
                                                isl_map_copy(path_comp));
                path = isl_map_union(path, path_comp);
@@ -2093,50 +2136,57 @@ error:
 }
 
 /* Compute the positive powers of "map", or an overapproximation.
- * The power is given by parameter "param".  If the result is exact,
- * then *exact is set to 1.
+ * The result maps the exponent to a nested copy of the corresponding power.
+ * If the result is exact, then *exact is set to 1.
  * map_power constructs an extended relation with the path lengths
  * encoded as the difference between the final coordinates.
- * In the final step, this difference is equated to the parameter "param"
- * and made positive.  The extra coordinates are subsequently projected out.
+ * In the final step, this difference is equated to an extra parameter
+ * and made positive.  The extra coordinates are subsequently projected out
+ * and the parameter is turned into the domain of the result.
  */
-__isl_give isl_map *isl_map_power(__isl_take isl_map *map, unsigned param,
-       int *exact)
+__isl_give isl_map *isl_map_power(__isl_take isl_map *map, int *exact)
 {
        isl_dim *target_dim;
        isl_dim *dim;
        isl_map *diff;
        unsigned d;
+       unsigned param;
 
        if (!map)
                return NULL;
 
-       isl_assert(map->ctx, param < isl_map_dim(map, isl_dim_param),
-               goto error);
-
        d = isl_map_dim(map, isl_dim_in);
+       param = isl_map_dim(map, isl_dim_param);
 
        map = isl_map_compute_divs(map);
        map = isl_map_coalesce(map);
 
-       if (isl_map_fast_is_empty(map))
+       if (isl_map_fast_is_empty(map)) {
+               map = isl_map_from_range(isl_map_wrap(map));
+               map = isl_map_add_dims(map, isl_dim_in, 1);
+               map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
                return map;
+       }
 
        target_dim = isl_map_get_dim(map);
+       target_dim = isl_dim_from_range(isl_dim_wrap(target_dim));
+       target_dim = isl_dim_add(target_dim, isl_dim_in, 1);
+       target_dim = isl_dim_set_name(target_dim, isl_dim_in, 0, "k");
+
        map = map_power(map, exact, 0);
 
+       map = isl_map_add_dims(map, isl_dim_param, 1);
        dim = isl_map_get_dim(map);
        diff = equate_parameter_to_length(dim, param);
        map = isl_map_intersect(map, diff);
        map = isl_map_project_out(map, isl_dim_in, d, 1);
        map = isl_map_project_out(map, isl_dim_out, d, 1);
+       map = isl_map_from_range(isl_map_wrap(map));
+       map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
 
        map = isl_map_reset_dim(map, target_dim);
 
        return map;
-error:
-       isl_map_free(map);
-       return NULL;
 }
 
 /* Compute a relation that maps each element in the range of the input
@@ -2169,13 +2219,13 @@ __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
                if (exact)
                        *exact = 1;
                map = isl_map_project_out(map, isl_dim_out, 0, d);
-               map = isl_map_add(map, isl_dim_out, 1);
+               map = isl_map_add_dims(map, isl_dim_out, 1);
                return map;
        }
 
        map = map_power(map, exact, 0);
 
-       map = isl_map_add(map, isl_dim_param, 1);
+       map = isl_map_add_dims(map, isl_dim_param, 1);
        dim = isl_map_get_dim(map);
        diff = equate_parameter_to_length(dim, param);
        map = isl_map_intersect(map, diff);
@@ -2660,7 +2710,7 @@ __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
        if (!map)
                goto error;
 
-       if (map->ctx->opt->closure == ISL_CLOSURE_OMEGA)
+       if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
                return transitive_closure_omega(map, exact);
 
        map = isl_map_compute_divs(map);
@@ -2968,3 +3018,87 @@ error:
        isl_union_map_free(umap);
        return NULL;
 }
+
+struct isl_union_power {
+       isl_union_map *pow;
+       int *exact;
+};
+
+static int power(__isl_take isl_map *map, void *user)
+{
+       struct isl_union_power *up = user;
+
+       map = isl_map_power(map, up->exact);
+       up->pow = isl_union_map_from_map(map);
+
+       return -1;
+}
+
+/* Construct a map [x] -> [x+1], with parameters prescribed by "dim".
+ */
+static __isl_give isl_union_map *increment(__isl_take isl_dim *dim)
+{
+       int k;
+       isl_basic_map *bmap;
+
+       dim = isl_dim_add(dim, isl_dim_in, 1);
+       dim = isl_dim_add(dim, isl_dim_out, 1);
+       bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
+       k = isl_basic_map_alloc_equality(bmap);
+       if (k < 0)
+               goto error;
+       isl_seq_clr(bmap->eq[k], isl_basic_map_total_dim(bmap));
+       isl_int_set_si(bmap->eq[k][0], 1);
+       isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
+       isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
+       return isl_union_map_from_map(isl_map_from_basic_map(bmap));
+error:
+       isl_basic_map_free(bmap);
+       return NULL;
+}
+
+/* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "dim".
+ */
+static __isl_give isl_union_map *deltas_map(__isl_take isl_dim *dim)
+{
+       isl_basic_map *bmap;
+
+       dim = isl_dim_add(dim, isl_dim_in, 1);
+       dim = isl_dim_add(dim, isl_dim_out, 1);
+       bmap = isl_basic_map_universe(dim);
+       bmap = isl_basic_map_deltas_map(bmap);
+
+       return isl_union_map_from_map(isl_map_from_basic_map(bmap));
+}
+
+/* Compute the positive powers of "map", or an overapproximation.
+ * The result maps the exponent to a nested copy of the corresponding power.
+ * If the result is exact, then *exact is set to 1.
+ */
+__isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
+       int *exact)
+{
+       int n;
+       isl_union_map *inc;
+       isl_union_map *dm;
+
+       if (!umap)
+               return NULL;
+       n = isl_union_map_n_map(umap);
+       if (n == 0)
+               return umap;
+       if (n == 1) {
+               struct isl_union_power up = { NULL, exact };
+               isl_union_map_foreach_map(umap, &power, &up);
+               isl_union_map_free(umap);
+               return up.pow;
+       }
+       inc = increment(isl_union_map_get_dim(umap));
+       umap = isl_union_map_product(inc, umap);
+       umap = isl_union_map_transitive_closure(umap, exact);
+       umap = isl_union_map_zip(umap);
+       dm = deltas_map(isl_union_map_get_dim(umap));
+       umap = isl_union_map_apply_domain(umap, dm);
+       
+       return umap;
+}