add isl_aff_mod_val
[platform/upstream/isl.git] / isl_flow.c
index 1056ab2..8dca5c4 100644 (file)
@@ -2,8 +2,9 @@
  * Copyright 2005-2007 Universiteit Leiden
  * Copyright 2008-2009 Katholieke Universiteit Leuven
  * Copyright 2010      INRIA Saclay
+ * Copyright 2012      Universiteit Leiden
  *
- * 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, Leiden Institute of Advanced Computer Science,
  * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
 #include <isl/set.h>
 #include <isl/map.h>
 #include <isl/flow.h>
+#include <isl_sort.h>
+
+enum isl_restriction_type {
+       isl_restriction_type_empty,
+       isl_restriction_type_none,
+       isl_restriction_type_input,
+       isl_restriction_type_output
+};
+
+struct isl_restriction {
+       enum isl_restriction_type type;
+
+       isl_set *source;
+       isl_set *sink;
+};
+
+/* Create a restriction of the given type.
+ */
+static __isl_give isl_restriction *isl_restriction_alloc(
+       __isl_take isl_map *source_map, enum isl_restriction_type type)
+{
+       isl_ctx *ctx;
+       isl_restriction *restr;
+
+       if (!source_map)
+               return NULL;
+
+       ctx = isl_map_get_ctx(source_map);
+       restr = isl_calloc_type(ctx, struct isl_restriction);
+       if (!restr)
+               goto error;
+
+       restr->type = type;
+
+       isl_map_free(source_map);
+       return restr;
+error:
+       isl_map_free(source_map);
+       return NULL;
+}
+
+/* Create a restriction that doesn't restrict anything.
+ */
+__isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
+{
+       return isl_restriction_alloc(source_map, isl_restriction_type_none);
+}
+
+/* Create a restriction that removes everything.
+ */
+__isl_give isl_restriction *isl_restriction_empty(
+       __isl_take isl_map *source_map)
+{
+       return isl_restriction_alloc(source_map, isl_restriction_type_empty);
+}
+
+/* Create a restriction on the input of the maximization problem
+ * based on the given source and sink restrictions.
+ */
+__isl_give isl_restriction *isl_restriction_input(
+       __isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
+{
+       isl_ctx *ctx;
+       isl_restriction *restr;
+
+       if (!source_restr || !sink_restr)
+               goto error;
+
+       ctx = isl_set_get_ctx(source_restr);
+       restr = isl_calloc_type(ctx, struct isl_restriction);
+       if (!restr)
+               goto error;
+
+       restr->type = isl_restriction_type_input;
+       restr->source = source_restr;
+       restr->sink = sink_restr;
+
+       return restr;
+error:
+       isl_set_free(source_restr);
+       isl_set_free(sink_restr);
+       return NULL;
+}
+
+/* Create a restriction on the output of the maximization problem
+ * based on the given source restriction.
+ */
+__isl_give isl_restriction *isl_restriction_output(
+       __isl_take isl_set *source_restr)
+{
+       isl_ctx *ctx;
+       isl_restriction *restr;
+
+       if (!source_restr)
+               return NULL;
+
+       ctx = isl_set_get_ctx(source_restr);
+       restr = isl_calloc_type(ctx, struct isl_restriction);
+       if (!restr)
+               goto error;
+
+       restr->type = isl_restriction_type_output;
+       restr->source = source_restr;
+
+       return restr;
+error:
+       isl_set_free(source_restr);
+       return NULL;
+}
+
+void *isl_restriction_free(__isl_take isl_restriction *restr)
+{
+       if (!restr)
+               return NULL;
+
+       isl_set_free(restr->source);
+       isl_set_free(restr->sink);
+       free(restr);
+       return NULL;
+}
+
+isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
+{
+       return restr ? isl_set_get_ctx(restr->source) : NULL;
+}
 
 /* A private structure to keep track of a mapping together with
  * a user-specified identifier and a boolean indicating whether
@@ -32,14 +158,25 @@ struct isl_labeled_map {
  * - n_must + n_may (<= max_source) sources
  * - a function for determining the relative order of sources and sink
  * The must sources are placed before the may sources.
+ *
+ * domain_map is an auxiliary map that maps the sink access relation
+ * to the domain of this access relation.
+ *
+ * restrict_fn is a callback that (if not NULL) will be called
+ * right before any lexicographical maximization.
  */
 struct isl_access_info {
-       struct isl_labeled_map  sink;
-       isl_access_level_before level_before;
-       int                     max_source;
-       int                     n_must;
-       int                     n_may;
-       struct isl_labeled_map  source[1];
+       isl_map                         *domain_map;
+       struct isl_labeled_map          sink;
+       isl_access_level_before         level_before;
+
+       isl_access_restrict             restrict_fn;
+       void                            *restrict_user;
+
+       int                             max_source;
+       int                             n_must;
+       int                             n_may;
+       struct isl_labeled_map          source[1];
 };
 
 /* A structure containing the output of dependence analysis:
@@ -69,7 +206,7 @@ __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
        ctx = isl_map_get_ctx(sink);
        isl_assert(ctx, max_source >= 0, goto error);
 
-       acc = isl_alloc(ctx, struct isl_access_info,
+       acc = isl_calloc(ctx, struct isl_access_info,
                        sizeof(struct isl_access_info) +
                        (max_source - 1) * sizeof(struct isl_labeled_map));
        if (!acc)
@@ -90,16 +227,18 @@ error:
 
 /* Free the given isl_access_info structure.
  */
-void isl_access_info_free(__isl_take isl_access_info *acc)
+void *isl_access_info_free(__isl_take isl_access_info *acc)
 {
        int i;
 
        if (!acc)
-               return;
+               return NULL;
+       isl_map_free(acc->domain_map);
        isl_map_free(acc->sink.map);
        for (i = 0; i < acc->n_must + acc->n_may; ++i)
                isl_map_free(acc->source[i].map);
        free(acc);
+       return NULL;
 }
 
 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
@@ -107,6 +246,16 @@ isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
        return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
 }
 
+__isl_give isl_access_info *isl_access_info_set_restrict(
+       __isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
+{
+       if (!acc)
+               return NULL;
+       acc->restrict_fn = fn;
+       acc->restrict_user = user;
+       return acc;
+}
+
 /* Add another source to an isl_access_info structure, making
  * sure the "must" sources are placed before the "may" sources.
  * This function may be called at most max_source times on a
@@ -120,7 +269,7 @@ __isl_give isl_access_info *isl_access_info_add_source(
        isl_ctx *ctx;
 
        if (!acc)
-               return NULL;
+               goto error;
        ctx = isl_map_get_ctx(acc->sink.map);
        isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
        
@@ -146,23 +295,11 @@ error:
        return NULL;
 }
 
-/* A temporary structure used while sorting the accesses in an isl_access_info.
- */
-struct isl_access_sort_info {
-       struct isl_map          *source_map;
-       void                    *source_data;
-       struct isl_access_info  *acc;
-};
-
 /* Return -n, 0 or n (with n a positive value), depending on whether
  * the source access identified by p1 should be sorted before, together
  * or after that identified by p2.
  *
- * If p1 and p2 share a different number of levels with the sink,
- * then the one with the lowest number of shared levels should be
- * sorted first.
- * If they both share no levels, then the order is irrelevant.
- * Otherwise, if p1 appears before p2, then it should be sorted first.
+ * If p1 appears before p2, then it should be sorted first.
  * For more generic initial schedules, it is possible that neither
  * p1 nor p2 appears before the other, or at least not in any obvious way.
  * We therefore also check if p2 appears before p1, in which case p2
@@ -171,75 +308,43 @@ struct isl_access_sort_info {
  * of the iteration domains.  This results in an arbitrary, but fairly
  * stable ordering.
  */
-static int access_sort_cmp(const void *p1, const void *p2)
+static int access_sort_cmp(const void *p1, const void *p2, void *user)
 {
-       const struct isl_access_sort_info *i1, *i2;
+       isl_access_info *acc = user;
+       const struct isl_labeled_map *i1, *i2;
        int level1, level2;
        uint32_t h1, h2;
-       i1 = (const struct isl_access_sort_info *) p1;
-       i2 = (const struct isl_access_sort_info *) p2;
+       i1 = (const struct isl_labeled_map *) p1;
+       i2 = (const struct isl_labeled_map *) p2;
 
-       level1 = i1->acc->level_before(i1->source_data, i1->acc->sink.data);
-       level2 = i2->acc->level_before(i2->source_data, i2->acc->sink.data);
-
-       if (level1 != level2 || !level1)
-               return level1 - level2;
-
-       level1 = i1->acc->level_before(i1->source_data, i2->source_data);
+       level1 = acc->level_before(i1->data, i2->data);
        if (level1 % 2)
                return -1;
 
-       level2 = i1->acc->level_before(i2->source_data, i1->source_data);
+       level2 = acc->level_before(i2->data, i1->data);
        if (level2 % 2)
                return 1;
 
-       h1 = isl_map_get_hash(i1->source_map);
-       h2 = isl_map_get_hash(i2->source_map);
+       h1 = isl_map_get_hash(i1->map);
+       h2 = isl_map_get_hash(i2->map);
        return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
 }
 
-/* Sort the must source accesses in order of increasing number of shared
- * levels with the sink access.
- * Source accesses with the same number of shared levels are sorted
- * in their textual order.
+/* Sort the must source accesses in their textual order.
  */
 static __isl_give isl_access_info *isl_access_info_sort_sources(
        __isl_take isl_access_info *acc)
 {
-       int i;
-       isl_ctx *ctx;
-       struct isl_access_sort_info *array;
-
        if (!acc)
                return NULL;
        if (acc->n_must <= 1)
                return acc;
 
-       ctx = isl_map_get_ctx(acc->sink.map);
-       array = isl_alloc_array(ctx, struct isl_access_sort_info, acc->n_must);
-       if (!array)
-               goto error;
-
-       for (i = 0; i < acc->n_must; ++i) {
-               array[i].source_map = acc->source[i].map;
-               array[i].source_data = acc->source[i].data;
-               array[i].acc = acc;
-       }
-
-       qsort(array, acc->n_must, sizeof(struct isl_access_sort_info),
-               access_sort_cmp);
-
-       for (i = 0; i < acc->n_must; ++i) {
-               acc->source[i].map = array[i].source_map;
-               acc->source[i].data = array[i].source_data;
-       }
-
-       free(array);
+       if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
+                   access_sort_cmp, acc) < 0)
+               return isl_access_info_free(acc);
 
        return acc;
-error:
-       isl_access_info_free(acc);
-       return NULL;
 }
 
 /* Align the parameters of the two spaces if needed and then call
@@ -400,6 +505,69 @@ static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
        return isl_map_from_basic_map(bmap);
 }
 
+/* Compute the partial lexicographic maximum of "dep" on domain "sink",
+ * but first check if the user has set acc->restrict_fn and if so
+ * update either the input or the output of the maximization problem
+ * with respect to the resulting restriction.
+ *
+ * Since the user expects a mapping from sink iterations to source iterations,
+ * whereas the domain of "dep" is a wrapped map, mapping sink iterations
+ * to accessed array elements, we first need to project out the accessed
+ * sink array elements by applying acc->domain_map.
+ * Similarly, the sink restriction specified by the user needs to be
+ * converted back to the wrapped map.
+ */
+static __isl_give isl_map *restricted_partial_lexmax(
+       __isl_keep isl_access_info *acc, __isl_take isl_map *dep,
+       int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
+{
+       isl_map *source_map;
+       isl_restriction *restr;
+       isl_set *sink_domain;
+       isl_set *sink_restr;
+       isl_map *res;
+
+       if (!acc->restrict_fn)
+               return isl_map_partial_lexmax(dep, sink, empty);
+
+       source_map = isl_map_copy(dep);
+       source_map = isl_map_apply_domain(source_map,
+                                           isl_map_copy(acc->domain_map));
+       sink_domain = isl_set_copy(sink);
+       sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
+       restr = acc->restrict_fn(source_map, sink_domain,
+                               acc->source[source].data, acc->restrict_user);
+       isl_set_free(sink_domain);
+       isl_map_free(source_map);
+
+       if (!restr)
+               goto error;
+       if (restr->type == isl_restriction_type_input) {
+               dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
+               sink_restr = isl_set_copy(restr->sink);
+               sink_restr = isl_set_apply(sink_restr,
+                               isl_map_reverse(isl_map_copy(acc->domain_map)));
+               sink = isl_set_intersect(sink, sink_restr);
+       } else if (restr->type == isl_restriction_type_empty) {
+               isl_space *space = isl_map_get_space(dep);
+               isl_map_free(dep);
+               dep = isl_map_empty(space);
+       }
+
+       res = isl_map_partial_lexmax(dep, sink, empty);
+
+       if (restr->type == isl_restriction_type_output)
+               res = isl_map_intersect_range(res, isl_set_copy(restr->source));
+
+       isl_restriction_free(restr);
+       return res;
+error:
+       isl_map_free(dep);
+       isl_set_free(sink);
+       *empty = NULL;
+       return NULL;
+}
+
 /* Compute the last iteration of must source j that precedes the sink
  * at the given level for sink iterations in set_C.
  * The subset of set_C for which no such iteration can be found is returned
@@ -421,7 +589,7 @@ static struct isl_map *last_source(struct isl_access_info *acc,
        dep_map = isl_map_apply_range(read_map, write_map);
        after = after_at_level(isl_map_get_space(dep_map), level);
        dep_map = isl_map_intersect(dep_map, after);
-       result = isl_map_partial_lexmax(dep_map, set_C, empty);
+       result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
        result = isl_map_reverse(result);
 
        return result;
@@ -462,7 +630,7 @@ static struct isl_map *last_later_source(struct isl_access_info *acc,
        dep_map = isl_map_intersect(dep_map, after_write);
        before_read = after_at_level(isl_map_get_space(dep_map), before_level);
        dep_map = isl_map_intersect(dep_map, before_read);
-       result = isl_map_partial_lexmax(dep_map, set_C, empty);
+       result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
        result = isl_map_reverse(result);
 
        return result;
@@ -659,7 +827,7 @@ static __isl_give isl_map *all_intermediate_sources(
  * be overkill to use it.
  */
 static __isl_give isl_flow *compute_mem_based_dependences(
-       __isl_take isl_access_info *acc)
+       __isl_keep isl_access_info *acc)
 {
        int i;
        isl_set *mustdo;
@@ -668,7 +836,7 @@ static __isl_give isl_flow *compute_mem_based_dependences(
 
        res = isl_flow_alloc(acc);
        if (!res)
-               goto error;
+               return NULL;
 
        mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
        maydo = isl_set_copy(mustdo);
@@ -700,12 +868,7 @@ static __isl_give isl_flow *compute_mem_based_dependences(
        res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
        res->must_no_source = mustdo;
 
-       isl_access_info_free(acc);
-
        return res;
-error:
-       isl_access_info_free(acc);
-       return NULL;
 }
 
 /* Compute dependences for the case where there is at least one
@@ -744,7 +907,7 @@ error:
  * 
  */
 static __isl_give isl_flow *compute_val_based_dependences(
-       __isl_take isl_access_info *acc)
+       __isl_keep isl_access_info *acc)
 {
        isl_ctx *ctx;
        isl_flow *res;
@@ -755,7 +918,6 @@ static __isl_give isl_flow *compute_val_based_dependences(
        isl_map **must_rel = NULL;
        isl_map **may_rel = NULL;
 
-       acc = isl_access_info_sort_sources(acc);
        if (!acc)
                return NULL;
 
@@ -866,10 +1028,8 @@ static __isl_give isl_flow *compute_val_based_dependences(
 done:
        res->must_no_source = mustdo;
        res->may_no_source = maydo;
-       isl_access_info_free(acc);
        return res;
 error:
-       isl_access_info_free(acc);
        isl_flow_free(res);
        isl_set_free(mustdo);
        isl_set_free(maydo);
@@ -898,41 +1058,38 @@ error:
 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
 {
        int j;
-       struct isl_flow *res;
-       isl_map *domain_map = NULL;
+       struct isl_flow *res = NULL;
 
        if (!acc)
                return NULL;
 
-       domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
+       acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
        acc->sink.map = isl_map_range_map(acc->sink.map);
        if (!acc->sink.map)
                goto error;
 
        if (acc->n_must == 0)
                res = compute_mem_based_dependences(acc);
-       else
+       else {
+               acc = isl_access_info_sort_sources(acc);
                res = compute_val_based_dependences(acc);
+       }
        if (!res)
-               goto error2;
+               goto error;
 
        for (j = 0; j < res->n_source; ++j) {
                res->dep[j].map = isl_map_apply_range(res->dep[j].map,
-                                       isl_map_copy(domain_map));
+                                       isl_map_copy(acc->domain_map));
                if (!res->dep[j].map)
-                       goto error2;
+                       goto error;
        }
        if (!res->must_no_source || !res->may_no_source)
-               goto error2;
+               goto error;
 
-       isl_map_free(domain_map);
+       isl_access_info_free(acc);
        return res;
 error:
-       isl_map_free(domain_map);
        isl_access_info_free(acc);
-       return NULL;
-error2:
-       isl_map_free(domain_map);
        isl_flow_free(res);
        return NULL;
 }
@@ -970,6 +1127,7 @@ static __isl_give struct isl_sched_info *sched_info_alloc(
        isl_space *dim;
        struct isl_sched_info *info;
        int i, n;
+       isl_int v;
 
        if (!map)
                return NULL;
@@ -989,9 +1147,13 @@ static __isl_give struct isl_sched_info *sched_info_alloc(
        if (!info->is_cst || !info->cst)
                goto error;
 
-       for (i = 0; i < n; ++i)
+       isl_int_init(v);
+       for (i = 0; i < n; ++i) {
                info->is_cst[i] = isl_map_plain_is_fixed(map, isl_dim_in, i,
-                                                       &info->cst->el[i]);
+                                                        &v);
+               info->cst = isl_vec_set_element(info->cst, i, v);
+       }
+       isl_int_clear(v);
 
        return info;
 error:
@@ -1092,22 +1254,36 @@ static int before(void *first, void *second)
        struct isl_sched_info *info2 = second;
        int n1, n2;
        int i;
+       isl_int v1, v2;
 
-       n1 = info1->cst->size;
-       n2 = info2->cst->size;
+       n1 = isl_vec_size(info1->cst);
+       n2 = isl_vec_size(info2->cst);
 
        if (n2 < n1)
                n1 = n2;
 
+       isl_int_init(v1);
+       isl_int_init(v2);
        for (i = 0; i < n1; ++i) {
+               int r;
+
                if (!info1->is_cst[i])
                        continue;
                if (!info2->is_cst[i])
                        continue;
-               if (isl_int_eq(info1->cst->el[i], info2->cst->el[i]))
+               isl_vec_get_element(info1->cst, i, &v1);
+               isl_vec_get_element(info2->cst, i, &v2);
+               if (isl_int_eq(v1, v2))
                        continue;
-               return 2 * i + isl_int_lt(info1->cst->el[i], info2->cst->el[i]);
+
+               r = 2 * i + isl_int_lt(v1, v2);
+
+               isl_int_clear(v1);
+               isl_int_clear(v2);
+               return r;
        }
+       isl_int_clear(v1);
+       isl_int_clear(v2);
 
        return 2 * n1;
 }