add isl_set_indicator_function
[platform/upstream/isl.git] / isl_flow.c
index bb513c5..2678f0f 100644 (file)
@@ -32,14 +32,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 +77,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 +104,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 +116,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
@@ -302,8 +320,9 @@ static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
        }
        for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
                isl_space *dim;
-               dim = isl_space_join(isl_map_get_space(acc->source[i].map),
-                           isl_space_reverse(isl_map_get_space(acc->sink.map)));
+               dim = space_align_and_join(
+                       isl_map_get_space(acc->source[i].map),
+                       isl_space_reverse(isl_map_get_space(acc->sink.map)));
                dep->dep[acc->n_must + i].map = isl_map_empty(dim);
                dep->dep[acc->n_must + i].data = acc->source[i].data;
                dep->dep[acc->n_must + i].must = 0;
@@ -399,6 +418,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
@@ -420,6 +465,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);
 
@@ -461,6 +507,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);
 
@@ -658,7 +705,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;
@@ -667,7 +714,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);
@@ -699,12 +746,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
@@ -743,7 +785,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;
@@ -754,7 +796,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;
 
@@ -865,10 +906,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);
@@ -897,41 +936,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;
 }