isl_qpolynomial_div: further normalize divs by reducing coefficients
[platform/upstream/isl.git] / isl_flow.c
index 1535ae7..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,7 +238,7 @@ 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;
@@ -716,7 +718,7 @@ 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));
@@ -894,3 +896,297 @@ error2:
        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;
+}