+
+
+/* Keep track of some information about a schedule for a given
+ * access. In particular, keep track of which dimensions
+ * have a constant value and of the actual constant values.
+ */
+struct isl_sched_info {
+ int *is_cst;
+ isl_vec *cst;
+};
+
+static void sched_info_free(__isl_take struct isl_sched_info *info)
+{
+ if (!info)
+ return;
+ isl_vec_free(info->cst);
+ free(info->is_cst);
+ free(info);
+}
+
+/* Extract information on the constant dimensions of the schedule
+ * for a given access. The "map" is of the form
+ *
+ * [S -> D] -> A
+ *
+ * with S the schedule domain, D the iteration domain and A the data domain.
+ */
+static __isl_give struct isl_sched_info *sched_info_alloc(
+ __isl_keep isl_map *map)
+{
+ isl_ctx *ctx;
+ isl_space *dim;
+ struct isl_sched_info *info;
+ int i, n;
+ isl_int v;
+
+ if (!map)
+ return NULL;
+
+ dim = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
+ if (!dim)
+ return NULL;
+ n = isl_space_dim(dim, isl_dim_in);
+ isl_space_free(dim);
+
+ ctx = isl_map_get_ctx(map);
+ info = isl_alloc_type(ctx, struct isl_sched_info);
+ if (!info)
+ return NULL;
+ info->is_cst = isl_alloc_array(ctx, int, n);
+ info->cst = isl_vec_alloc(ctx, n);
+ if (!info->is_cst || !info->cst)
+ goto error;
+
+ isl_int_init(v);
+ for (i = 0; i < n; ++i) {
+ info->is_cst[i] = isl_map_plain_is_fixed(map, isl_dim_in, i,
+ &v);
+ info->cst = isl_vec_set_element(info->cst, i, v);
+ }
+ isl_int_clear(v);
+
+ return info;
+error:
+ sched_info_free(info);
+ 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_map *must_no_source;
+ isl_union_map *may_no_source;
+
+ int count;
+ int must;
+ isl_space *dim;
+ struct isl_sched_info *sink_info;
+ struct isl_sched_info **source_info;
+ isl_access_info *accesses;
+};
+
+static int count_matching_array(__isl_take isl_map *map, void *user)
+{
+ int eq;
+ isl_space *dim;
+ struct isl_compute_flow_data *data;
+
+ data = (struct isl_compute_flow_data *)user;
+
+ dim = isl_space_range(isl_map_get_space(map));
+
+ eq = isl_space_is_equal(dim, data->dim);
+
+ isl_space_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_space *dim;
+ struct isl_sched_info *info;
+ struct isl_compute_flow_data *data;
+
+ data = (struct isl_compute_flow_data *)user;
+
+ dim = isl_space_range(isl_map_get_space(map));
+
+ eq = isl_space_is_equal(dim, data->dim);
+
+ isl_space_free(dim);
+
+ if (eq < 0)
+ goto error;
+ if (!eq) {
+ isl_map_free(map);
+ return 0;
+ }
+
+ info = sched_info_alloc(map);
+ data->source_info[data->count] = info;
+
+ data->accesses = isl_access_info_add_source(data->accesses,
+ map, data->must, info);
+
+ data->count++;
+
+ return 0;
+error:
+ isl_map_free(map);
+ return -1;
+}
+
+/* Determine the shared nesting level and the "textual order" of
+ * the given accesses.
+ *
+ * We first determine the minimal schedule dimension for both accesses.
+ *
+ * If among those dimensions, we can find one where both have a fixed
+ * value and if moreover those values are different, then the previous
+ * dimension is the last shared nesting level and the textual order
+ * is determined based on the order of the fixed values.
+ * If no such fixed values can be found, then we set the shared
+ * nesting level to the minimal schedule dimension, with no textual ordering.
+ */
+static int before(void *first, void *second)
+{
+ struct isl_sched_info *info1 = first;
+ struct isl_sched_info *info2 = second;
+ int n1, n2;
+ int i;
+ isl_int v1, v2;
+
+ 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;
+ isl_vec_get_element(info1->cst, i, &v1);
+ isl_vec_get_element(info2->cst, i, &v2);
+ if (isl_int_eq(v1, v2))
+ continue;
+
+ 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;
+}
+
+/* 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_info = NULL;
+ data->source_info = NULL;
+ data->count = 0;
+ data->dim = isl_space_range(isl_map_get_space(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_info = sched_info_alloc(map);
+ data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
+ data->count);
+
+ data->accesses = isl_access_info_alloc(isl_map_copy(map),
+ data->sink_info, &before, data->count);
+ if (!data->sink_info || !data->source_info || !data->accesses)
+ goto error;
+ 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_map_union(data->must_no_source,
+ isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
+ data->may_no_source = isl_union_map_union(data->may_no_source,
+ isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
+
+ 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);
+
+ sched_info_free(data->sink_info);
+ if (data->source_info) {
+ for (i = 0; i < data->count; ++i)
+ sched_info_free(data->source_info[i]);
+ free(data->source_info);
+ }
+ isl_space_free(data->dim);
+ isl_map_free(map);
+
+ return 0;
+error:
+ isl_access_info_free(data->accesses);
+ sched_info_free(data->sink_info);
+ if (data->source_info) {
+ for (i = 0; i < data->count; ++i)
+ sched_info_free(data->source_info[i]);
+ free(data->source_info);
+ }
+ isl_space_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_map **must_no_source,
+ __isl_give isl_union_map **may_no_source)
+{
+ isl_space *dim;
+ isl_union_map *range_map = NULL;
+ struct isl_compute_flow_data data;
+
+ sink = isl_union_map_align_params(sink,
+ isl_union_map_get_space(must_source));
+ sink = isl_union_map_align_params(sink,
+ isl_union_map_get_space(may_source));
+ sink = isl_union_map_align_params(sink,
+ isl_union_map_get_space(schedule));
+ dim = isl_union_map_get_space(sink);
+ must_source = isl_union_map_align_params(must_source, isl_space_copy(dim));
+ may_source = isl_union_map_align_params(may_source, isl_space_copy(dim));
+ schedule = isl_union_map_align_params(schedule, isl_space_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_space_copy(dim)) : NULL;
+ data.may_dep = may_dep ? isl_union_map_empty(isl_space_copy(dim)) : NULL;
+ data.must_no_source = must_no_source ?
+ isl_union_map_empty(isl_space_copy(dim)) : NULL;
+ data.may_no_source = may_no_source ?
+ isl_union_map_empty(isl_space_copy(dim)) : NULL;
+
+ isl_space_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_map_apply_domain(
+ 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_map_apply_domain(
+ 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_map_free(data.must_no_source);
+ isl_union_map_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;
+}