add isl_map_power and isl_map_transitive_closure
[platform/upstream/isl.git] / isl_transitive_closure.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #include "isl_map.h"
12 #include "isl_map_private.h"
13  
14 /*
15  * The transitive closure implementation is based on the paper
16  * "Computing the Transitive Closure of a Union of Affine Integer
17  * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
18  * Albert Cohen.
19  */
20
21 /* Given a union of translations (uniform dependences), return a matrix
22  * with as many rows as there are disjuncts in the union and as many
23  * columns as there are input dimensions (which should be equal to
24  * the number of output dimensions).
25  * Each row contains the translation performed by the corresponding disjunct.
26  * If "map" turns out not to be a union of translations, then the contents
27  * of the returned matrix are undefined and *ok is set to 0.
28  */
29 static __isl_give isl_mat *extract_steps(__isl_keep isl_map *map, int *ok)
30 {
31         int i, j;
32         struct isl_mat *steps;
33         unsigned dim = isl_map_dim(map, isl_dim_in);
34
35         *ok = 1;
36
37         steps = isl_mat_alloc(map->ctx, map->n, dim);
38         if (!steps)
39                 return NULL;
40
41         for (i = 0; i < map->n; ++i) {
42                 struct isl_basic_set *delta;
43
44                 delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
45
46                 for (j = 0; j < dim; ++j) {
47                         int fixed;
48
49                         fixed = isl_basic_set_fast_dim_is_fixed(delta, j,
50                                                             &steps->row[i][j]);
51                         if (fixed < 0) {
52                                 isl_basic_set_free(delta);
53                                 goto error;
54                         }
55                         if (!fixed)
56                                 break;
57                 }
58
59                 isl_basic_set_free(delta);
60
61                 if (j < dim)
62                         break;
63         }
64
65         if (i < map->n)
66                 *ok = 0;
67
68         return steps;
69 error:
70         isl_mat_free(steps);
71         return NULL;
72 }
73
74 /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
75  * of the given dimension specification that maps a element x to any
76  * element that can be reached by taking a positive number of steps
77  * along any of the offsets, where the number of steps k is equal to
78  * parameter "param".  That is, construct
79  *
80  * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v_i, k = \sum_i k_i > 0 }
81  *
82  * If strict is non-negative, then at least one step should be taken
83  * along the given offset v_strict, i.e., k_strict > 0.
84  */
85 static __isl_give isl_map *path_along_steps(__isl_take isl_dim *dim,
86         __isl_keep isl_mat *steps, unsigned param, int strict)
87 {
88         int i, j, k;
89         struct isl_basic_map *path = NULL;
90         unsigned d;
91         unsigned n;
92         unsigned nparam;
93
94         if (!dim || !steps)
95                 goto error;
96
97         d = isl_dim_size(dim, isl_dim_in);
98         n = steps->n_row;
99         nparam = isl_dim_size(dim, isl_dim_param);
100
101         path = isl_basic_map_alloc_dim(isl_dim_copy(dim), n, d + 1, n + 1);
102
103         for (i = 0; i < n; ++i) {
104                 k = isl_basic_map_alloc_div(path);
105                 if (k < 0)
106                         goto error;
107                 isl_assert(steps->ctx, i == k, goto error);
108                 isl_int_set_si(path->div[k][0], 0);
109         }
110
111         for (i = 0; i < d; ++i) {
112                 k = isl_basic_map_alloc_equality(path);
113                 if (k < 0)
114                         goto error;
115                 isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
116                 isl_int_set_si(path->eq[k][1 + nparam + i], 1);
117                 isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
118                 for (j = 0; j < n; ++j)
119                         isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
120                                     steps->row[j][i]);
121         }
122
123         k = isl_basic_map_alloc_equality(path);
124         if (k < 0)
125                 goto error;
126         isl_seq_clr(path->eq[k], 1 + isl_basic_map_total_dim(path));
127         isl_int_set_si(path->eq[k][1 + param], -1);
128         for (j = 0; j < n; ++j)
129                 isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
130
131         for (i = 0; i < n; ++i) {
132                 k = isl_basic_map_alloc_inequality(path);
133                 if (k < 0)
134                         goto error;
135                 isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
136                 isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
137                 if (i == strict)
138                         isl_int_set_si(path->ineq[k][0], -1);
139         }
140
141         k = isl_basic_map_alloc_inequality(path);
142         if (k < 0)
143                 goto error;
144         isl_seq_clr(path->ineq[k], 1 + isl_basic_map_total_dim(path));
145         isl_int_set_si(path->ineq[k][1 + param], 1);
146         isl_int_set_si(path->ineq[k][0], -1);
147
148         isl_dim_free(dim);
149
150         path = isl_basic_map_simplify(path);
151         path = isl_basic_map_finalize(path);
152         return isl_map_from_basic_map(path);
153 error:
154         isl_dim_free(dim);
155         isl_basic_map_free(path);
156         return NULL;
157 }
158
159 /* Check whether the overapproximation of the power of "map" is exactly
160  * the power of "map".  In particular, for each path of a given length
161  * that starts of in domain or range and ends up in the range,
162  * check whether there is at least one path of the same length
163  * with a valid first segment, i.e., one in "map".
164  *
165  * "domain" and "range" are the domain and range of "map"
166  * "steps" represents the translations of "map"
167  * "path" is a path along "steps"
168  *
169  * "domain", "range", "steps" and "path" have been precomputed by the calling
170  * function.
171  */
172 static int check_exactness(__isl_take isl_map *map, __isl_take isl_set *domain,
173         __isl_take isl_set *range, __isl_take isl_map *path,
174         __isl_keep isl_mat *steps, unsigned param)
175 {
176         isl_map *test;
177         int ok;
178         int i;
179
180         if (!map)
181                 goto error;
182
183         test = isl_map_empty(isl_map_get_dim(map));
184         for (i = 0; i < map->n; ++i) {
185                 struct isl_map *path_i;
186                 struct isl_set *dom_i;
187                 path_i = path_along_steps(isl_map_get_dim(map), steps, param, i);
188                 dom_i = isl_set_from_basic_set(
189                         isl_basic_map_domain(isl_basic_map_copy(map->p[i])));
190                 path_i = isl_map_intersect_domain(path_i, dom_i);
191                 test = isl_map_union(test, path_i);
192         }
193         isl_map_free(map);
194         test = isl_map_intersect_range(test, isl_set_copy(range));
195
196         domain = isl_set_union(domain, isl_set_copy(range));
197         path = isl_map_intersect_domain(path, domain);
198         path = isl_map_intersect_range(path, range);
199
200         ok = isl_map_is_subset(path, test);
201
202         isl_map_free(path);
203         isl_map_free(test);
204
205         return ok;
206 error:
207         isl_map_free(map);
208         isl_set_free(domain);
209         isl_set_free(range);
210         isl_map_free(path);
211         return -1;
212 }
213
214 /* Compute the positive powers of "map", or an overapproximation.
215  * The power is given by parameter "param".  If the result is exact,
216  * then *exact is set to 1.
217  */
218 __isl_give isl_map *isl_map_power(__isl_take isl_map *map, unsigned param,
219         int *exact)
220 {
221         struct isl_mat *steps = NULL;
222         struct isl_set *domain = NULL;
223         struct isl_set *range = NULL;
224         struct isl_map *app = NULL;
225         struct isl_map *path = NULL;
226         int ok;
227
228         if (exact)
229                 *exact = 1;
230
231         map = isl_map_remove_empty_parts(map);
232         if (!map)
233                 return NULL;
234
235         if (isl_map_fast_is_empty(map))
236                 return map;
237
238         isl_assert(map->ctx, param < isl_map_dim(map, isl_dim_param), goto error);
239         isl_assert(map->ctx,
240                 isl_map_dim(map, isl_dim_in) == isl_map_dim(map, isl_dim_out),
241                 goto error);
242
243         domain = isl_map_domain(isl_map_copy(map));
244         range = isl_map_range(isl_map_copy(map));
245         app = isl_map_from_domain_and_range(isl_set_copy(domain),
246                                             isl_set_copy(range));
247
248         steps = extract_steps(map, &ok);
249         if (!ok)
250                 goto not_exact;
251
252         path = path_along_steps(isl_map_get_dim(map), steps, param, -1);
253         app = isl_map_intersect(app, isl_map_copy(path));
254
255         if (exact &&
256             (*exact = check_exactness(isl_map_copy(map), isl_set_copy(domain),
257                                   isl_set_copy(range), isl_map_copy(path),
258                                   steps, param)) < 0)
259                 goto error;
260
261         if (0) {
262 not_exact:
263                 if (exact)
264                         *exact = 0;
265         }
266         isl_set_free(domain);
267         isl_set_free(range);
268         isl_mat_free(steps);
269         isl_map_free(path);
270         isl_map_free(map);
271         return app;
272 error:
273         isl_set_free(domain);
274         isl_set_free(range);
275         isl_mat_free(steps);
276         isl_map_free(path);
277         isl_map_free(map);
278         isl_map_free(app);
279         return NULL;
280 }
281
282 /* Compute the transitive closure  of "map", or an overapproximation.
283  * If the result is exact, then *exact is set to 1.
284  * Simply compute the powers of map and then project out the parameter
285  * describing the power.
286  */
287 __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
288         int *exact)
289 {
290         unsigned param;
291
292         if (!map)
293                 goto error;
294
295         param = isl_map_dim(map, isl_dim_param);
296         map = isl_map_add(map, isl_dim_param, 1);
297         map = isl_map_power(map, param, exact);
298         map = isl_map_project_out(map, isl_dim_param, param, 1);
299
300         return map;
301 error:
302         isl_map_free(map);
303         return NULL;
304 }