isl_qpolynomial_div: further normalize divs by reducing coefficients
[platform/upstream/isl.git] / isl_flow.c
index d5dcb48..e8b9c58 100644 (file)
@@ -13,7 +13,7 @@
  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
  */
 
-#include <isl_flow.h>
+#include <isl/flow.h>
 
 /* A private structure to keep track of a mapping together with
  * a user-specified identifier and a boolean indicating whether
@@ -58,14 +58,16 @@ struct isl_flow {
 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
        void *sink_user, isl_access_level_before fn, int max_source)
 {
+       isl_ctx *ctx;
        struct isl_access_info *acc;
 
        if (!sink)
                return NULL;
 
-       isl_assert(sink->ctx, max_source >= 0, goto error);
+       ctx = isl_map_get_ctx(sink);
+       isl_assert(ctx, max_source >= 0, goto error);
 
-       acc = isl_alloc(sink->ctx, struct isl_access_info,
+       acc = isl_alloc(ctx, struct isl_access_info,
                        sizeof(struct isl_access_info) +
                        (max_source - 1) * sizeof(struct isl_labeled_map));
        if (!acc)
@@ -85,11 +87,8 @@ error:
 }
 
 /* Free the given isl_access_info structure.
- * This function is static because the user is expected to call
- * isl_access_info_compute_flow on any isl_access_info structure
- * he creates.
  */
-static void isl_access_info_free(__isl_take isl_access_info *acc)
+void isl_access_info_free(__isl_take isl_access_info *acc)
 {
        int i;
 
@@ -111,10 +110,12 @@ __isl_give isl_access_info *isl_access_info_add_source(
        __isl_take isl_access_info *acc, __isl_take isl_map *source,
        int must, void *source_user)
 {
+       isl_ctx *ctx;
+
        if (!acc)
                return NULL;
-       isl_assert(acc->sink.map->ctx,
-                   acc->n_must + acc->n_may < acc->max_source, goto error);
+       ctx = isl_map_get_ctx(acc->sink.map);
+       isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
        
        if (must) {
                if (acc->n_may)
@@ -183,6 +184,7 @@ 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)
@@ -190,8 +192,8 @@ static __isl_give isl_access_info *isl_access_info_sort_sources(
        if (acc->n_must <= 1)
                return acc;
 
-       array = isl_alloc_array(acc->sink.map->ctx,
-                               struct isl_access_sort_info, acc->n_must);
+       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;
 
@@ -236,12 +238,12 @@ static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
        if (!acc)
                return NULL;
 
-       ctx = acc->sink.map->ctx;
+       ctx = isl_map_get_ctx(acc->sink.map);
        dep = isl_calloc_type(ctx, struct isl_flow);
        if (!dep)
                return NULL;
 
-       dep->dep = isl_alloc_array(ctx, struct isl_labeled_map,
+       dep->dep = isl_calloc_array(ctx, struct isl_labeled_map,
                                        2 * acc->n_must + acc->n_may);
        if (!dep->dep)
                goto error;
@@ -249,22 +251,26 @@ static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
        dep->n_source = 2 * acc->n_must + acc->n_may;
        for (i = 0; i < acc->n_must; ++i) {
                struct isl_dim *dim;
-               dim = isl_dim_join(isl_dim_copy(acc->source[i].map->dim),
-                           isl_dim_reverse(isl_dim_copy(acc->sink.map->dim)));
+               dim = isl_dim_join(isl_map_get_dim(acc->source[i].map),
+                           isl_dim_reverse(isl_map_get_dim(acc->sink.map)));
                dep->dep[2 * i].map = isl_map_empty(dim);
                dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
                dep->dep[2 * i].data = acc->source[i].data;
                dep->dep[2 * i + 1].data = acc->source[i].data;
                dep->dep[2 * i].must = 1;
                dep->dep[2 * i + 1].must = 0;
+               if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
+                       goto error;
        }
        for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
                struct isl_dim *dim;
-               dim = isl_dim_join(isl_dim_copy(acc->source[i].map->dim),
-                           isl_dim_reverse(isl_dim_copy(acc->sink.map->dim)));
+               dim = isl_dim_join(isl_map_get_dim(acc->source[i].map),
+                           isl_dim_reverse(isl_map_get_dim(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;
+               if (!dep->dep[acc->n_must + i].map)
+                       goto error;
        }
 
        return dep;
@@ -369,7 +375,7 @@ static struct isl_map *last_source(struct isl_access_info *acc,
        write_map = isl_map_copy(acc->source[j].map);
        write_map = isl_map_reverse(write_map);
        dep_map = isl_map_apply_range(read_map, write_map);
-       after = after_at_level(isl_dim_copy(dep_map->dim), level);
+       after = after_at_level(isl_map_get_dim(dep_map), level);
        dep_map = isl_map_intersect(dep_map, after);
        result = isl_map_partial_lexmax(dep_map, set_C, empty);
        result = isl_map_reverse(result);
@@ -404,13 +410,13 @@ static struct isl_map *last_later_source(struct isl_access_info *acc,
 
        write_map = isl_map_reverse(write_map);
        dep_map = isl_map_apply_range(read_map, write_map);
-       dim = isl_dim_join(isl_dim_copy(acc->source[k].map->dim),
-                   isl_dim_reverse(isl_dim_copy(acc->source[j].map->dim)));
+       dim = isl_dim_join(isl_map_get_dim(acc->source[k].map),
+                   isl_dim_reverse(isl_map_get_dim(acc->source[j].map)));
        after_write = after_at_level(dim, after_level);
        after_write = isl_map_apply_range(after_write, old_map);
        after_write = isl_map_reverse(after_write);
        dep_map = isl_map_intersect(dep_map, after_write);
-       before_read = after_at_level(isl_dim_copy(dep_map->dim), before_level);
+       before_read = after_at_level(isl_map_get_dim(dep_map), before_level);
        dep_map = isl_map_intersect(dep_map, before_read);
        result = isl_map_partial_lexmax(dep_map, set_C, empty);
        result = isl_map_reverse(result);
@@ -505,7 +511,7 @@ static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
        write_map = isl_map_copy(acc->source[acc->n_must + j].map);
        write_map = isl_map_reverse(write_map);
        dep_map = isl_map_apply_range(read_map, write_map);
-       after = after_at_level(isl_dim_copy(dep_map->dim), level);
+       after = after_at_level(isl_map_get_dim(dep_map), level);
        dep_map = isl_map_intersect(dep_map, after);
 
        return isl_map_reverse(dep_map);
@@ -536,13 +542,13 @@ static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
 
        write_map = isl_map_reverse(write_map);
        dep_map = isl_map_apply_range(read_map, write_map);
-       dim = isl_dim_join(isl_dim_copy(acc->source[acc->n_must + j].map->dim),
-                   isl_dim_reverse(isl_dim_copy(acc->source[k].map->dim)));
+       dim = isl_dim_join(isl_map_get_dim(acc->source[acc->n_must + j].map),
+                   isl_dim_reverse(isl_map_get_dim(acc->source[k].map)));
        after_write = after_at_level(dim, after_level);
        after_write = isl_map_apply_range(after_write, old_map);
        after_write = isl_map_reverse(after_write);
        dep_map = isl_map_intersect(dep_map, after_write);
-       before_read = after_at_level(isl_dim_copy(dep_map->dim), before_level);
+       before_read = after_at_level(isl_map_get_dim(dep_map), before_level);
        dep_map = isl_map_intersect(dep_map, before_read);
        return isl_map_reverse(dep_map);
 }
@@ -698,12 +704,12 @@ static __isl_give isl_flow *compute_val_based_dependences(
 {
        isl_ctx *ctx;
        isl_flow *res;
-       isl_set *mustdo;
-       isl_set *maydo;
+       isl_set *mustdo = NULL;
+       isl_set *maydo = NULL;
        int level, j;
        int depth;
-       isl_map **must_rel;
-       isl_map **may_rel;
+       isl_map **must_rel = NULL;
+       isl_map **may_rel = NULL;
 
        acc = isl_access_info_sort_sources(acc);
        if (!acc)
@@ -712,16 +718,20 @@ static __isl_give isl_flow *compute_val_based_dependences(
        res = isl_flow_alloc(acc);
        if (!res)
                goto error;
-       ctx = acc->sink.map->ctx;
+       ctx = isl_map_get_ctx(acc->sink.map);
 
        depth = 2 * isl_map_dim(acc->sink.map, isl_dim_in) + 1;
        mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
        maydo = isl_set_empty_like(mustdo);
+       if (!mustdo || !maydo)
+               goto error;
        if (isl_set_fast_is_empty(mustdo))
                goto done;
 
        must_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
        may_rel = isl_alloc_array(ctx, struct isl_map *, acc->n_must);
+       if (!must_rel || !may_rel)
+               goto error;
 
        for (level = depth; level >= 1; --level) {
                for (j = acc->n_must-1; j >=0; --j) {
@@ -816,6 +826,11 @@ done:
        return res;
 error:
        isl_access_info_free(acc);
+       isl_flow_free(res);
+       isl_set_free(mustdo);
+       isl_set_free(maydo);
+       free(must_rel);
+       free(may_rel);
        return NULL;
 }
 
@@ -840,22 +855,13 @@ __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *ac
 {
        int j;
        struct isl_flow *res;
-       isl_dim *dim;
-       isl_map *id;
-       unsigned n_sink;
-       unsigned n_data;
+       isl_map *domain_map = NULL;
 
        if (!acc)
                return NULL;
 
-       n_sink = isl_map_dim(acc->sink.map, isl_dim_in);
-       n_data = isl_map_dim(acc->sink.map, isl_dim_out);
-       dim = isl_dim_range(isl_map_get_dim(acc->sink.map));
-       id = isl_map_identity(dim);
-       id = isl_map_insert(id, isl_dim_in, 0, n_sink);
-       acc->sink.map = isl_map_insert(acc->sink.map, isl_dim_in,
-                                       n_sink, n_data);
-       acc->sink.map = isl_map_intersect(acc->sink.map, id);
+       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;
 
@@ -863,15 +869,324 @@ __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *ac
                res = compute_mem_based_dependences(acc);
        else
                res = compute_val_based_dependences(acc);
+       if (!res)
+               return NULL;
 
-       for (j = 0; j < res->n_source; ++j)
-               res->dep[j].map = isl_map_project_out(res->dep[j].map,
-                                       isl_dim_out, n_sink, n_data);
-       res->must_no_source = isl_set_project_out(res->must_no_source, isl_dim_set, n_sink, n_data);
-       res->may_no_source = isl_set_project_out(res->may_no_source, isl_dim_set, n_sink, n_data);
-
+       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));
+               if (!res->dep[j].map)
+                       goto error2;
+       }
+       res->must_no_source = isl_set_apply(res->must_no_source,
+                                       isl_map_copy(domain_map));
+       res->may_no_source = isl_set_apply(res->may_no_source,
+                                       isl_map_copy(domain_map));
+       if (!res->must_no_source || !res->may_no_source)
+               goto error2;
+
+       isl_map_free(domain_map);
        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;
+}
+
+
+struct isl_compute_flow_data {
+       isl_union_map *must_source;
+       isl_union_map *may_source;
+       isl_union_map *must_dep;
+       isl_union_map *may_dep;
+       isl_union_set *must_no_source;
+       isl_union_set *may_no_source;
+
+       int count;
+       int must;
+       isl_dim *dim;
+       isl_dim *sink_dim;
+       isl_dim **source_dim;
+       isl_access_info *accesses;
+};
+
+static int count_matching_array(__isl_take isl_map *map, void *user)
+{
+       int eq;
+       isl_dim *dim;
+       struct isl_compute_flow_data *data;
+
+       data = (struct isl_compute_flow_data *)user;
+
+       dim = isl_dim_range(isl_map_get_dim(map));
+
+       eq = isl_dim_equal(dim, data->dim);
+
+       isl_dim_free(dim);
+       isl_map_free(map);
+
+       if (eq < 0)
+               return -1;
+       if (eq)
+               data->count++;
+
+       return 0;
+}
+
+static int collect_matching_array(__isl_take isl_map *map, void *user)
+{
+       int eq;
+       isl_dim *dim;
+       struct isl_compute_flow_data *data;
+
+       data = (struct isl_compute_flow_data *)user;
+
+       dim = isl_dim_range(isl_map_get_dim(map));
+
+       eq = isl_dim_equal(dim, data->dim);
+
+       isl_dim_free(dim);
+
+       if (eq < 0)
+               goto error;
+       if (!eq) {
+               isl_map_free(map);
+               return 0;
+       }
+
+       dim = isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map)));
+       data->source_dim[data->count] = dim;
+
+       data->accesses = isl_access_info_add_source(data->accesses,
+                                                   map, data->must, dim);
+
+       data->count++;
+
+       return 0;
+error:
+       isl_map_free(map);
+       return -1;
+}
+
+static int before(void *first, void *second)
+{
+       isl_dim *dim1 = first;
+       isl_dim *dim2 = second;
+       int n1, n2;
+
+       n1 = isl_dim_size(dim1, isl_dim_in);
+       n2 = isl_dim_size(dim2, isl_dim_in);
+
+       if (n2 < n1)
+               n1 = n2;
+
+       return 2 * n1 + (dim1 < dim2);
+}
+
+/* Given a sink access, look for all the source accesses that access
+ * the same array and perform dataflow analysis on them using
+ * isl_access_info_compute_flow.
+ */
+static int compute_flow(__isl_take isl_map *map, void *user)
+{
+       int i;
+       isl_ctx *ctx;
+       struct isl_compute_flow_data *data;
+       isl_flow *flow;
+
+       data = (struct isl_compute_flow_data *)user;
+
+       ctx = isl_map_get_ctx(map);
+
+       data->accesses = NULL;
+       data->sink_dim = NULL;
+       data->source_dim = NULL;
+       data->count = 0;
+       data->dim = isl_dim_range(isl_map_get_dim(map));
+
+       if (isl_union_map_foreach_map(data->must_source,
+                                       &count_matching_array, data) < 0)
+               goto error;
+       if (isl_union_map_foreach_map(data->may_source,
+                                       &count_matching_array, data) < 0)
+               goto error;
+
+       data->sink_dim = isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map)));
+       data->source_dim = isl_calloc_array(ctx, isl_dim *, data->count);
+
+       data->accesses = isl_access_info_alloc(isl_map_copy(map),
+                               data->sink_dim, &before, data->count);
+       data->count = 0;
+       data->must = 1;
+       if (isl_union_map_foreach_map(data->must_source,
+                                       &collect_matching_array, data) < 0)
+               goto error;
+       data->must = 0;
+       if (isl_union_map_foreach_map(data->may_source,
+                                       &collect_matching_array, data) < 0)
+               goto error;
+
+       flow = isl_access_info_compute_flow(data->accesses);
+       data->accesses = NULL;
+
+       if (!flow)
+               goto error;
+
+       data->must_no_source = isl_union_set_union(data->must_no_source,
+                   isl_union_set_from_set(isl_set_copy(flow->must_no_source)));
+       data->may_no_source = isl_union_set_union(data->may_no_source,
+                   isl_union_set_from_set(isl_set_copy(flow->may_no_source)));
+
+       for (i = 0; i < flow->n_source; ++i) {
+               isl_union_map *dep;
+               dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
+               if (flow->dep[i].must)
+                       data->must_dep = isl_union_map_union(data->must_dep, dep);
+               else
+                       data->may_dep = isl_union_map_union(data->may_dep, dep);
+       }
+
+       isl_flow_free(flow);
+
+       isl_dim_free(data->sink_dim);
+       if (data->source_dim) {
+               for (i = 0; i < data->count; ++i)
+                       isl_dim_free(data->source_dim[i]);
+               free(data->source_dim);
+       }
+       isl_dim_free(data->dim);
+       isl_map_free(map);
+
+       return 0;
+error:
+       isl_access_info_free(data->accesses);
+       isl_dim_free(data->sink_dim);
+       if (data->source_dim) {
+               for (i = 0; i < data->count; ++i)
+                       isl_dim_free(data->source_dim[i]);
+               free(data->source_dim);
+       }
+       isl_dim_free(data->dim);
+       isl_map_free(map);
+
+       return -1;
+}
+
+/* Given a collection of "sink" and "source" accesses,
+ * compute for each iteration of a sink access
+ * and for each element accessed by that iteration,
+ * the source access in the list that last accessed the
+ * element accessed by the sink access before this sink access.
+ * Each access is given as a map from the loop iterators
+ * to the array indices.
+ * The result is a relations between source and sink
+ * iterations and a subset of the domain of the sink accesses,
+ * corresponding to those iterations that access an element
+ * not previously accessed.
+ *
+ * We first prepend the schedule dimensions to the domain
+ * of the accesses so that we can easily compare their relative order.
+ * Then we consider each sink access individually in compute_flow.
+ */
+int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
+       __isl_take isl_union_map *must_source,
+       __isl_take isl_union_map *may_source,
+       __isl_take isl_union_map *schedule,
+       __isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
+       __isl_give isl_union_set **must_no_source,
+       __isl_give isl_union_set **may_no_source)
+{
+       isl_dim *dim;
+       isl_union_map *range_map = NULL;
+       struct isl_compute_flow_data data;
+
+       sink = isl_union_map_align_params(sink,
+                                           isl_union_map_get_dim(must_source));
+       sink = isl_union_map_align_params(sink,
+                                           isl_union_map_get_dim(may_source));
+       sink = isl_union_map_align_params(sink,
+                                           isl_union_map_get_dim(schedule));
+       dim = isl_union_map_get_dim(sink);
+       must_source = isl_union_map_align_params(must_source, isl_dim_copy(dim));
+       may_source = isl_union_map_align_params(may_source, isl_dim_copy(dim));
+       schedule = isl_union_map_align_params(schedule, isl_dim_copy(dim));
+
+       schedule = isl_union_map_reverse(schedule);
+       range_map = isl_union_map_range_map(schedule);
+       schedule = isl_union_map_reverse(isl_union_map_copy(range_map));
+       sink = isl_union_map_apply_domain(sink, isl_union_map_copy(schedule));
+       must_source = isl_union_map_apply_domain(must_source,
+                                               isl_union_map_copy(schedule));
+       may_source = isl_union_map_apply_domain(may_source, schedule);
+
+       data.must_source = must_source;
+       data.may_source = may_source;
+       data.must_dep = must_dep ?
+               isl_union_map_empty(isl_dim_copy(dim)) : NULL;
+       data.may_dep = may_dep ? isl_union_map_empty(isl_dim_copy(dim)) : NULL;
+       data.must_no_source = must_no_source ?
+               isl_union_set_empty(isl_dim_copy(dim)) : NULL;
+       data.may_no_source = may_no_source ?
+               isl_union_set_empty(isl_dim_copy(dim)) : NULL;
+
+       isl_dim_free(dim);
+
+       if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
+               goto error;
+
+       isl_union_map_free(sink);
+       isl_union_map_free(must_source);
+       isl_union_map_free(may_source);
+
+       if (must_dep) {
+               data.must_dep = isl_union_map_apply_domain(data.must_dep,
+                                       isl_union_map_copy(range_map));
+               data.must_dep = isl_union_map_apply_range(data.must_dep,
+                                       isl_union_map_copy(range_map));
+               *must_dep = data.must_dep;
+       }
+       if (may_dep) {
+               data.may_dep = isl_union_map_apply_domain(data.may_dep,
+                                       isl_union_map_copy(range_map));
+               data.may_dep = isl_union_map_apply_range(data.may_dep,
+                                       isl_union_map_copy(range_map));
+               *may_dep = data.may_dep;
+       }
+       if (must_no_source) {
+               data.must_no_source = isl_union_set_apply(data.must_no_source,
+                                       isl_union_map_copy(range_map));
+               *must_no_source = data.must_no_source;
+       }
+       if (may_no_source) {
+               data.may_no_source = isl_union_set_apply(data.may_no_source,
+                                       isl_union_map_copy(range_map));
+               *may_no_source = data.may_no_source;
+       }
+
+       isl_union_map_free(range_map);
+
+       return 0;
+error:
+       isl_union_map_free(range_map);
+       isl_union_map_free(sink);
+       isl_union_map_free(must_source);
+       isl_union_map_free(may_source);
+       isl_union_map_free(data.must_dep);
+       isl_union_map_free(data.may_dep);
+       isl_union_set_free(data.must_no_source);
+       isl_union_set_free(data.may_no_source);
+
+       if (must_dep)
+               *must_dep = NULL;
+       if (may_dep)
+               *may_dep = NULL;
+       if (must_no_source)
+               *must_no_source = NULL;
+       if (may_no_source)
+               *may_no_source = NULL;
+       return -1;
 }