export isl_qpolynomial_gist
[platform/upstream/isl.git] / isl_flow.c
index 0e681dc..d27937c 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
@@ -42,8 +42,8 @@ struct isl_access_info {
 
 /* A structure containing the output of dependence analysis:
  * - n_source dependences
- * - a subset of the sink for which definitely no source could be found
- * - a subset of the sink for which possibly no source could be found
+ * - a wrapped subset of the sink for which definitely no source could be found
+ * - a wrapped subset of the sink for which possibly no source could be found
  */
 struct isl_flow {
        isl_set                 *must_no_source;
@@ -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)
@@ -155,11 +156,19 @@ struct isl_access_sort_info {
  * 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.
+ * 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
+ * should be sorted first.
+ * If not, we try to order the two statements based on the description
+ * of the iteration domains.  This results in an arbitrary, but fairly
+ * stable ordering.
  */
 static int access_sort_cmp(const void *p1, const void *p2)
 {
        const struct isl_access_sort_info *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;
 
@@ -170,8 +179,16 @@ static int access_sort_cmp(const void *p1, const void *p2)
                return level1 - level2;
 
        level1 = i1->acc->level_before(i1->source_data, i2->source_data);
+       if (level1 % 2)
+               return -1;
+
+       level2 = i1->acc->level_before(i2->source_data, i1->source_data);
+       if (level2 % 2)
+               return 1;
 
-       return (level1 % 2) ? -1 : 1;
+       h1 = isl_map_get_hash(i1->source_map);
+       h2 = isl_map_get_hash(i2->source_map);
+       return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
 }
 
 /* Sort the must source accesses in order of increasing number of shared
@@ -183,6 +200,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 +208,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 +254,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;
@@ -293,7 +311,7 @@ int isl_flow_foreach(__isl_keep isl_flow *deps,
                return -1;
 
        for (i = 0; i < deps->n_source; ++i) {
-               if (isl_map_fast_is_empty(deps->dep[i].map))
+               if (isl_map_plain_is_empty(deps->dep[i].map))
                        continue;
                if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
                                deps->dep[i].data, user) < 0)
@@ -305,15 +323,15 @@ int isl_flow_foreach(__isl_keep isl_flow *deps,
 
 /* Return a copy of the subset of the sink for which no source could be found.
  */
-__isl_give isl_set *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
+__isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
 {
        if (!deps)
                return NULL;
        
        if (must)
-               return isl_set_copy(deps->must_no_source);
+               return isl_set_unwrap(isl_set_copy(deps->must_no_source));
        else
-               return isl_set_copy(deps->may_no_source);
+               return isl_set_unwrap(isl_set_copy(deps->may_no_source));
 }
 
 void isl_flow_free(__isl_take isl_flow *deps)
@@ -457,7 +475,7 @@ static int intermediate_sources(__isl_keep isl_access_info *acc,
        int k, level;
        int depth = 2 * isl_map_dim(acc->source[j].map, isl_dim_in) + 1;
 
-       if (isl_map_fast_is_empty(temp_rel[j]))
+       if (isl_map_plain_is_empty(temp_rel[j]))
                return 0;
 
        for (k = j - 1; k >= 0; --k) {
@@ -480,7 +498,7 @@ static int intermediate_sources(__isl_keep isl_access_info *acc,
                        copy = isl_map_copy(temp_rel[j]);
                        T = last_later_source(acc, copy, j, sink_level, k,
                                              level, &trest);
-                       if (isl_map_fast_is_empty(T)) {
+                       if (isl_map_plain_is_empty(T)) {
                                isl_set_free(trest);
                                isl_map_free(T);
                                continue;
@@ -570,8 +588,8 @@ static __isl_give isl_map *all_intermediate_sources(
        for (k = 0; k < acc->n_must; ++k) {
                int plevel;
 
-               if (isl_map_fast_is_empty(may_rel[k]) &&
-                   isl_map_fast_is_empty(must_rel[k]))
+               if (isl_map_plain_is_empty(may_rel[k]) &&
+                   isl_map_plain_is_empty(must_rel[k]))
                        continue;
 
                plevel = acc->level_before(acc->source[k].data,
@@ -702,12 +720,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)
@@ -716,16 +734,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 (isl_set_fast_is_empty(mustdo))
+       if (!mustdo || !maydo)
+               goto error;
+       if (isl_set_plain_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) {
@@ -755,8 +777,8 @@ static __isl_give isl_flow *compute_val_based_dependences(
 
                        intermediate_sources(acc, may_rel, j, level);
 
-                       if (isl_set_fast_is_empty(mustdo) &&
-                           isl_set_fast_is_empty(maydo))
+                       if (isl_set_plain_is_empty(mustdo) &&
+                           isl_set_plain_is_empty(maydo))
                                break;
                }
                for (j = j - 1; j >= 0; --j) {
@@ -806,8 +828,8 @@ static __isl_give isl_flow *compute_val_based_dependences(
                                                             may_rel[j]);
                }
 
-               if (isl_set_fast_is_empty(mustdo) &&
-                   isl_set_fast_is_empty(maydo))
+               if (isl_set_plain_is_empty(mustdo) &&
+                   isl_set_plain_is_empty(maydo))
                        break;
        }
 
@@ -820,6 +842,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;
 }
 
@@ -844,22 +871,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;
 
@@ -871,21 +889,404 @@ __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *ac
                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->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_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);
        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;
 }
+
+
+/* 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_dim *dim;
+       struct isl_sched_info *info;
+       int i, n;
+
+       if (!map)
+               return NULL;
+
+       dim = isl_dim_unwrap(isl_dim_domain(isl_map_get_dim(map)));
+       if (!dim)
+               return NULL;
+       n = isl_dim_size(dim, isl_dim_in);
+       isl_dim_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;
+
+       for (i = 0; i < n; ++i)
+               info->is_cst[i] = isl_map_plain_is_fixed(map, isl_dim_in, i,
+                                                       &info->cst->el[i]);
+
+       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_dim *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_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_sched_info *info;
+       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;
+       }
+
+       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;
+
+       n1 = info1->cst->size;
+       n2 = info2->cst->size;
+
+       if (n2 < n1)
+               n1 = n2;
+
+       for (i = 0; i < n1; ++i) {
+               if (!info1->is_cst[i])
+                       continue;
+               if (!info2->is_cst[i])
+                       continue;
+               if (isl_int_eq(info1->cst->el[i], info2->cst->el[i]))
+                       continue;
+               return 2 * i + isl_int_lt(info1->cst->el[i], info2->cst->el[i]);
+       }
+
+       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_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_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_dim_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_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_map **must_no_source,
+       __isl_give isl_union_map **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_map_empty(isl_dim_copy(dim)) : NULL;
+       data.may_no_source = may_no_source ?
+               isl_union_map_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_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;
+}