add isl_basic_map_get_divs
[platform/upstream/isl.git] / isl_flow.c
index 08b173b..bbaee6e 100644 (file)
@@ -16,6 +16,7 @@
 #include <isl/set.h>
 #include <isl/map.h>
 #include <isl/flow.h>
+#include <isl_qsort.h>
 
 /* A private structure to keep track of a mapping together with
  * a user-specified identifier and a boolean indicating whether
@@ -32,14 +33,22 @@ 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_sources 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_sources     restrict_sources;
+       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 +78,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)
@@ -96,6 +105,7 @@ void isl_access_info_free(__isl_take isl_access_info *acc)
 
        if (!acc)
                return;
+       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);
@@ -107,6 +117,15 @@ 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_sources(
+       __isl_take isl_access_info *acc, isl_access_restrict_sources fn)
+{
+       if (!acc)
+               return NULL;
+       acc->restrict_sources = fn;
+       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
@@ -146,23 +165,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 +178,42 @@ 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;
-
-       level1 = i1->acc->level_before(i1->source_data, i1->acc->sink.data);
-       level2 = i2->acc->level_before(i2->source_data, i2->acc->sink.data);
+       i1 = (const struct isl_labeled_map *) p1;
+       i2 = (const struct isl_labeled_map *) p2;
 
-       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);
+       isl_quicksort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
+               access_sort_cmp, 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 +374,32 @@ static __isl_give isl_map *after_at_level(__isl_take isl_space *dim, int level)
        return isl_map_from_basic_map(bmap);
 }
 
+/* Check if the user has set acc->restrict_sources and if so
+ * intersect the range of "dep" with the result of a call to this function.
+ *
+ * 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.
+ */
+static __isl_give isl_map *restrict_sources(__isl_take isl_map *dep,
+       struct isl_access_info *acc, int source)
+{
+       isl_map *source_map;
+       isl_set *param;
+
+       if (!acc->restrict_sources)
+               return dep;
+
+       source_map = isl_map_copy(dep);
+       source_map = isl_map_apply_domain(source_map,
+                                           isl_map_copy(acc->domain_map));
+       param = acc->restrict_sources(source_map, acc->sink.data,
+                                   acc->source[source].data);
+       dep = isl_map_intersect_range(dep, param);
+       return dep;
+}
+
 /* 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,6 +421,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);
+       dep_map = restrict_sources(dep_map, acc, j);
        result = isl_map_partial_lexmax(dep_map, set_C, empty);
        result = isl_map_reverse(result);
 
@@ -462,6 +463,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);
+       dep_map = restrict_sources(dep_map, acc, k);
        result = isl_map_partial_lexmax(dep_map, set_C, empty);
        result = isl_map_reverse(result);
 
@@ -891,12 +893,11 @@ __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *ac
 {
        int j;
        struct isl_flow *res = NULL;
-       isl_map *domain_map = 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;
@@ -912,18 +913,16 @@ __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *ac
 
        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 error;
        }
        if (!res->must_no_source || !res->may_no_source)
                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);
        isl_flow_free(res);
        return NULL;