xref: /netbsd-src/external/mit/isl/dist/isl_transitive_closure.c (revision 5971e316fdea024efff6be8f03536623db06833e)
1*5971e316Smrg /*
2*5971e316Smrg  * Copyright 2010      INRIA Saclay
3*5971e316Smrg  *
4*5971e316Smrg  * Use of this software is governed by the MIT license
5*5971e316Smrg  *
6*5971e316Smrg  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7*5971e316Smrg  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8*5971e316Smrg  * 91893 Orsay, France
9*5971e316Smrg  */
10*5971e316Smrg 
11*5971e316Smrg #include <isl_ctx_private.h>
12*5971e316Smrg #include <isl_map_private.h>
13*5971e316Smrg #include <isl/map.h>
14*5971e316Smrg #include <isl_seq.h>
15*5971e316Smrg #include <isl_space_private.h>
16*5971e316Smrg #include <isl_lp_private.h>
17*5971e316Smrg #include <isl/union_map.h>
18*5971e316Smrg #include <isl_mat_private.h>
19*5971e316Smrg #include <isl_vec_private.h>
20*5971e316Smrg #include <isl_options_private.h>
21*5971e316Smrg #include <isl_tarjan.h>
22*5971e316Smrg 
isl_map_is_transitively_closed(__isl_keep isl_map * map)23*5971e316Smrg isl_bool isl_map_is_transitively_closed(__isl_keep isl_map *map)
24*5971e316Smrg {
25*5971e316Smrg 	isl_map *map2;
26*5971e316Smrg 	isl_bool closed;
27*5971e316Smrg 
28*5971e316Smrg 	map2 = isl_map_apply_range(isl_map_copy(map), isl_map_copy(map));
29*5971e316Smrg 	closed = isl_map_is_subset(map2, map);
30*5971e316Smrg 	isl_map_free(map2);
31*5971e316Smrg 
32*5971e316Smrg 	return closed;
33*5971e316Smrg }
34*5971e316Smrg 
isl_union_map_is_transitively_closed(__isl_keep isl_union_map * umap)35*5971e316Smrg isl_bool isl_union_map_is_transitively_closed(__isl_keep isl_union_map *umap)
36*5971e316Smrg {
37*5971e316Smrg 	isl_union_map *umap2;
38*5971e316Smrg 	isl_bool closed;
39*5971e316Smrg 
40*5971e316Smrg 	umap2 = isl_union_map_apply_range(isl_union_map_copy(umap),
41*5971e316Smrg 					  isl_union_map_copy(umap));
42*5971e316Smrg 	closed = isl_union_map_is_subset(umap2, umap);
43*5971e316Smrg 	isl_union_map_free(umap2);
44*5971e316Smrg 
45*5971e316Smrg 	return closed;
46*5971e316Smrg }
47*5971e316Smrg 
48*5971e316Smrg /* Given a map that represents a path with the length of the path
49*5971e316Smrg  * encoded as the difference between the last output coordindate
50*5971e316Smrg  * and the last input coordinate, set this length to either
51*5971e316Smrg  * exactly "length" (if "exactly" is set) or at least "length"
52*5971e316Smrg  * (if "exactly" is not set).
53*5971e316Smrg  */
set_path_length(__isl_take isl_map * map,int exactly,int length)54*5971e316Smrg static __isl_give isl_map *set_path_length(__isl_take isl_map *map,
55*5971e316Smrg 	int exactly, int length)
56*5971e316Smrg {
57*5971e316Smrg 	isl_space *space;
58*5971e316Smrg 	struct isl_basic_map *bmap;
59*5971e316Smrg 	isl_size d;
60*5971e316Smrg 	isl_size nparam;
61*5971e316Smrg 	isl_size total;
62*5971e316Smrg 	int k;
63*5971e316Smrg 	isl_int *c;
64*5971e316Smrg 
65*5971e316Smrg 	if (!map)
66*5971e316Smrg 		return NULL;
67*5971e316Smrg 
68*5971e316Smrg 	space = isl_map_get_space(map);
69*5971e316Smrg 	d = isl_space_dim(space, isl_dim_in);
70*5971e316Smrg 	nparam = isl_space_dim(space, isl_dim_param);
71*5971e316Smrg 	total = isl_space_dim(space, isl_dim_all);
72*5971e316Smrg 	if (d < 0 || nparam < 0 || total < 0)
73*5971e316Smrg 		space = isl_space_free(space);
74*5971e316Smrg 	bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
75*5971e316Smrg 	if (exactly) {
76*5971e316Smrg 		k = isl_basic_map_alloc_equality(bmap);
77*5971e316Smrg 		if (k < 0)
78*5971e316Smrg 			goto error;
79*5971e316Smrg 		c = bmap->eq[k];
80*5971e316Smrg 	} else {
81*5971e316Smrg 		k = isl_basic_map_alloc_inequality(bmap);
82*5971e316Smrg 		if (k < 0)
83*5971e316Smrg 			goto error;
84*5971e316Smrg 		c = bmap->ineq[k];
85*5971e316Smrg 	}
86*5971e316Smrg 	isl_seq_clr(c, 1 + total);
87*5971e316Smrg 	isl_int_set_si(c[0], -length);
88*5971e316Smrg 	isl_int_set_si(c[1 + nparam + d - 1], -1);
89*5971e316Smrg 	isl_int_set_si(c[1 + nparam + d + d - 1], 1);
90*5971e316Smrg 
91*5971e316Smrg 	bmap = isl_basic_map_finalize(bmap);
92*5971e316Smrg 	map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
93*5971e316Smrg 
94*5971e316Smrg 	return map;
95*5971e316Smrg error:
96*5971e316Smrg 	isl_basic_map_free(bmap);
97*5971e316Smrg 	isl_map_free(map);
98*5971e316Smrg 	return NULL;
99*5971e316Smrg }
100*5971e316Smrg 
101*5971e316Smrg /* Check whether the overapproximation of the power of "map" is exactly
102*5971e316Smrg  * the power of "map".  Let R be "map" and A_k the overapproximation.
103*5971e316Smrg  * The approximation is exact if
104*5971e316Smrg  *
105*5971e316Smrg  *	A_1 = R
106*5971e316Smrg  *	A_k = A_{k-1} \circ R			k >= 2
107*5971e316Smrg  *
108*5971e316Smrg  * Since A_k is known to be an overapproximation, we only need to check
109*5971e316Smrg  *
110*5971e316Smrg  *	A_1 \subset R
111*5971e316Smrg  *	A_k \subset A_{k-1} \circ R		k >= 2
112*5971e316Smrg  *
113*5971e316Smrg  * In practice, "app" has an extra input and output coordinate
114*5971e316Smrg  * to encode the length of the path.  So, we first need to add
115*5971e316Smrg  * this coordinate to "map" and set the length of the path to
116*5971e316Smrg  * one.
117*5971e316Smrg  */
check_power_exactness(__isl_take isl_map * map,__isl_take isl_map * app)118*5971e316Smrg static isl_bool check_power_exactness(__isl_take isl_map *map,
119*5971e316Smrg 	__isl_take isl_map *app)
120*5971e316Smrg {
121*5971e316Smrg 	isl_bool exact;
122*5971e316Smrg 	isl_map *app_1;
123*5971e316Smrg 	isl_map *app_2;
124*5971e316Smrg 
125*5971e316Smrg 	map = isl_map_add_dims(map, isl_dim_in, 1);
126*5971e316Smrg 	map = isl_map_add_dims(map, isl_dim_out, 1);
127*5971e316Smrg 	map = set_path_length(map, 1, 1);
128*5971e316Smrg 
129*5971e316Smrg 	app_1 = set_path_length(isl_map_copy(app), 1, 1);
130*5971e316Smrg 
131*5971e316Smrg 	exact = isl_map_is_subset(app_1, map);
132*5971e316Smrg 	isl_map_free(app_1);
133*5971e316Smrg 
134*5971e316Smrg 	if (!exact || exact < 0) {
135*5971e316Smrg 		isl_map_free(app);
136*5971e316Smrg 		isl_map_free(map);
137*5971e316Smrg 		return exact;
138*5971e316Smrg 	}
139*5971e316Smrg 
140*5971e316Smrg 	app_1 = set_path_length(isl_map_copy(app), 0, 1);
141*5971e316Smrg 	app_2 = set_path_length(app, 0, 2);
142*5971e316Smrg 	app_1 = isl_map_apply_range(map, app_1);
143*5971e316Smrg 
144*5971e316Smrg 	exact = isl_map_is_subset(app_2, app_1);
145*5971e316Smrg 
146*5971e316Smrg 	isl_map_free(app_1);
147*5971e316Smrg 	isl_map_free(app_2);
148*5971e316Smrg 
149*5971e316Smrg 	return exact;
150*5971e316Smrg }
151*5971e316Smrg 
152*5971e316Smrg /* Check whether the overapproximation of the power of "map" is exactly
153*5971e316Smrg  * the power of "map", possibly after projecting out the power (if "project"
154*5971e316Smrg  * is set).
155*5971e316Smrg  *
156*5971e316Smrg  * If "project" is set and if "steps" can only result in acyclic paths,
157*5971e316Smrg  * then we check
158*5971e316Smrg  *
159*5971e316Smrg  *	A = R \cup (A \circ R)
160*5971e316Smrg  *
161*5971e316Smrg  * where A is the overapproximation with the power projected out, i.e.,
162*5971e316Smrg  * an overapproximation of the transitive closure.
163*5971e316Smrg  * More specifically, since A is known to be an overapproximation, we check
164*5971e316Smrg  *
165*5971e316Smrg  *	A \subset R \cup (A \circ R)
166*5971e316Smrg  *
167*5971e316Smrg  * Otherwise, we check if the power is exact.
168*5971e316Smrg  *
169*5971e316Smrg  * Note that "app" has an extra input and output coordinate to encode
170*5971e316Smrg  * the length of the part.  If we are only interested in the transitive
171*5971e316Smrg  * closure, then we can simply project out these coordinates first.
172*5971e316Smrg  */
check_exactness(__isl_take isl_map * map,__isl_take isl_map * app,int project)173*5971e316Smrg static isl_bool check_exactness(__isl_take isl_map *map,
174*5971e316Smrg 	__isl_take isl_map *app, int project)
175*5971e316Smrg {
176*5971e316Smrg 	isl_map *test;
177*5971e316Smrg 	isl_bool exact;
178*5971e316Smrg 	isl_size d;
179*5971e316Smrg 
180*5971e316Smrg 	if (!project)
181*5971e316Smrg 		return check_power_exactness(map, app);
182*5971e316Smrg 
183*5971e316Smrg 	d = isl_map_dim(map, isl_dim_in);
184*5971e316Smrg 	if (d < 0)
185*5971e316Smrg 		app = isl_map_free(app);
186*5971e316Smrg 	app = set_path_length(app, 0, 1);
187*5971e316Smrg 	app = isl_map_project_out(app, isl_dim_in, d, 1);
188*5971e316Smrg 	app = isl_map_project_out(app, isl_dim_out, d, 1);
189*5971e316Smrg 
190*5971e316Smrg 	app = isl_map_reset_space(app, isl_map_get_space(map));
191*5971e316Smrg 
192*5971e316Smrg 	test = isl_map_apply_range(isl_map_copy(map), isl_map_copy(app));
193*5971e316Smrg 	test = isl_map_union(test, isl_map_copy(map));
194*5971e316Smrg 
195*5971e316Smrg 	exact = isl_map_is_subset(app, test);
196*5971e316Smrg 
197*5971e316Smrg 	isl_map_free(app);
198*5971e316Smrg 	isl_map_free(test);
199*5971e316Smrg 
200*5971e316Smrg 	isl_map_free(map);
201*5971e316Smrg 
202*5971e316Smrg 	return exact;
203*5971e316Smrg }
204*5971e316Smrg 
205*5971e316Smrg /*
206*5971e316Smrg  * The transitive closure implementation is based on the paper
207*5971e316Smrg  * "Computing the Transitive Closure of a Union of Affine Integer
208*5971e316Smrg  * Tuple Relations" by Anna Beletska, Denis Barthou, Wlodzimierz Bielecki and
209*5971e316Smrg  * Albert Cohen.
210*5971e316Smrg  */
211*5971e316Smrg 
212*5971e316Smrg /* Given a set of n offsets v_i (the rows of "steps"), construct a relation
213*5971e316Smrg  * of the given dimension specification (Z^{n+1} -> Z^{n+1})
214*5971e316Smrg  * that maps an element x to any element that can be reached
215*5971e316Smrg  * by taking a non-negative number of steps along any of
216*5971e316Smrg  * the extended offsets v'_i = [v_i 1].
217*5971e316Smrg  * That is, construct
218*5971e316Smrg  *
219*5971e316Smrg  * { [x] -> [y] : exists k_i >= 0, y = x + \sum_i k_i v'_i }
220*5971e316Smrg  *
221*5971e316Smrg  * For any element in this relation, the number of steps taken
222*5971e316Smrg  * is equal to the difference in the final coordinates.
223*5971e316Smrg  */
path_along_steps(__isl_take isl_space * space,__isl_keep isl_mat * steps)224*5971e316Smrg static __isl_give isl_map *path_along_steps(__isl_take isl_space *space,
225*5971e316Smrg 	__isl_keep isl_mat *steps)
226*5971e316Smrg {
227*5971e316Smrg 	int i, j, k;
228*5971e316Smrg 	struct isl_basic_map *path = NULL;
229*5971e316Smrg 	isl_size d;
230*5971e316Smrg 	unsigned n;
231*5971e316Smrg 	isl_size nparam;
232*5971e316Smrg 	isl_size total;
233*5971e316Smrg 
234*5971e316Smrg 	d = isl_space_dim(space, isl_dim_in);
235*5971e316Smrg 	nparam = isl_space_dim(space, isl_dim_param);
236*5971e316Smrg 	if (d < 0 || nparam < 0 || !steps)
237*5971e316Smrg 		goto error;
238*5971e316Smrg 
239*5971e316Smrg 	n = steps->n_row;
240*5971e316Smrg 
241*5971e316Smrg 	path = isl_basic_map_alloc_space(isl_space_copy(space), n, d, n);
242*5971e316Smrg 
243*5971e316Smrg 	for (i = 0; i < n; ++i) {
244*5971e316Smrg 		k = isl_basic_map_alloc_div(path);
245*5971e316Smrg 		if (k < 0)
246*5971e316Smrg 			goto error;
247*5971e316Smrg 		isl_assert(steps->ctx, i == k, goto error);
248*5971e316Smrg 		isl_int_set_si(path->div[k][0], 0);
249*5971e316Smrg 	}
250*5971e316Smrg 
251*5971e316Smrg 	total = isl_basic_map_dim(path, isl_dim_all);
252*5971e316Smrg 	if (total < 0)
253*5971e316Smrg 		goto error;
254*5971e316Smrg 	for (i = 0; i < d; ++i) {
255*5971e316Smrg 		k = isl_basic_map_alloc_equality(path);
256*5971e316Smrg 		if (k < 0)
257*5971e316Smrg 			goto error;
258*5971e316Smrg 		isl_seq_clr(path->eq[k], 1 + total);
259*5971e316Smrg 		isl_int_set_si(path->eq[k][1 + nparam + i], 1);
260*5971e316Smrg 		isl_int_set_si(path->eq[k][1 + nparam + d + i], -1);
261*5971e316Smrg 		if (i == d - 1)
262*5971e316Smrg 			for (j = 0; j < n; ++j)
263*5971e316Smrg 				isl_int_set_si(path->eq[k][1 + nparam + 2 * d + j], 1);
264*5971e316Smrg 		else
265*5971e316Smrg 			for (j = 0; j < n; ++j)
266*5971e316Smrg 				isl_int_set(path->eq[k][1 + nparam + 2 * d + j],
267*5971e316Smrg 					    steps->row[j][i]);
268*5971e316Smrg 	}
269*5971e316Smrg 
270*5971e316Smrg 	for (i = 0; i < n; ++i) {
271*5971e316Smrg 		k = isl_basic_map_alloc_inequality(path);
272*5971e316Smrg 		if (k < 0)
273*5971e316Smrg 			goto error;
274*5971e316Smrg 		isl_seq_clr(path->ineq[k], 1 + total);
275*5971e316Smrg 		isl_int_set_si(path->ineq[k][1 + nparam + 2 * d + i], 1);
276*5971e316Smrg 	}
277*5971e316Smrg 
278*5971e316Smrg 	isl_space_free(space);
279*5971e316Smrg 
280*5971e316Smrg 	path = isl_basic_map_simplify(path);
281*5971e316Smrg 	path = isl_basic_map_finalize(path);
282*5971e316Smrg 	return isl_map_from_basic_map(path);
283*5971e316Smrg error:
284*5971e316Smrg 	isl_space_free(space);
285*5971e316Smrg 	isl_basic_map_free(path);
286*5971e316Smrg 	return NULL;
287*5971e316Smrg }
288*5971e316Smrg 
289*5971e316Smrg #define IMPURE		0
290*5971e316Smrg #define PURE_PARAM	1
291*5971e316Smrg #define PURE_VAR	2
292*5971e316Smrg #define MIXED		3
293*5971e316Smrg 
294*5971e316Smrg /* Check whether the parametric constant term of constraint c is never
295*5971e316Smrg  * positive in "bset".
296*5971e316Smrg  */
parametric_constant_never_positive(__isl_keep isl_basic_set * bset,isl_int * c,int * div_purity)297*5971e316Smrg static isl_bool parametric_constant_never_positive(
298*5971e316Smrg 	__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity)
299*5971e316Smrg {
300*5971e316Smrg 	isl_size d;
301*5971e316Smrg 	isl_size n_div;
302*5971e316Smrg 	isl_size nparam;
303*5971e316Smrg 	isl_size total;
304*5971e316Smrg 	int i;
305*5971e316Smrg 	int k;
306*5971e316Smrg 	isl_bool empty;
307*5971e316Smrg 
308*5971e316Smrg 	n_div = isl_basic_set_dim(bset, isl_dim_div);
309*5971e316Smrg 	d = isl_basic_set_dim(bset, isl_dim_set);
310*5971e316Smrg 	nparam = isl_basic_set_dim(bset, isl_dim_param);
311*5971e316Smrg 	total = isl_basic_set_dim(bset, isl_dim_all);
312*5971e316Smrg 	if (n_div < 0 || d < 0 || nparam < 0 || total < 0)
313*5971e316Smrg 		return isl_bool_error;
314*5971e316Smrg 
315*5971e316Smrg 	bset = isl_basic_set_copy(bset);
316*5971e316Smrg 	bset = isl_basic_set_cow(bset);
317*5971e316Smrg 	bset = isl_basic_set_extend_constraints(bset, 0, 1);
318*5971e316Smrg 	k = isl_basic_set_alloc_inequality(bset);
319*5971e316Smrg 	if (k < 0)
320*5971e316Smrg 		goto error;
321*5971e316Smrg 	isl_seq_clr(bset->ineq[k], 1 + total);
322*5971e316Smrg 	isl_seq_cpy(bset->ineq[k], c, 1 + nparam);
323*5971e316Smrg 	for (i = 0; i < n_div; ++i) {
324*5971e316Smrg 		if (div_purity[i] != PURE_PARAM)
325*5971e316Smrg 			continue;
326*5971e316Smrg 		isl_int_set(bset->ineq[k][1 + nparam + d + i],
327*5971e316Smrg 			    c[1 + nparam + d + i]);
328*5971e316Smrg 	}
329*5971e316Smrg 	isl_int_sub_ui(bset->ineq[k][0], bset->ineq[k][0], 1);
330*5971e316Smrg 	empty = isl_basic_set_is_empty(bset);
331*5971e316Smrg 	isl_basic_set_free(bset);
332*5971e316Smrg 
333*5971e316Smrg 	return empty;
334*5971e316Smrg error:
335*5971e316Smrg 	isl_basic_set_free(bset);
336*5971e316Smrg 	return isl_bool_error;
337*5971e316Smrg }
338*5971e316Smrg 
339*5971e316Smrg /* Return PURE_PARAM if only the coefficients of the parameters are non-zero.
340*5971e316Smrg  * Return PURE_VAR if only the coefficients of the set variables are non-zero.
341*5971e316Smrg  * Return MIXED if only the coefficients of the parameters and the set
342*5971e316Smrg  * 	variables are non-zero and if moreover the parametric constant
343*5971e316Smrg  * 	can never attain positive values.
344*5971e316Smrg  * Return IMPURE otherwise.
345*5971e316Smrg  */
purity(__isl_keep isl_basic_set * bset,isl_int * c,int * div_purity,int eq)346*5971e316Smrg static int purity(__isl_keep isl_basic_set *bset, isl_int *c, int *div_purity,
347*5971e316Smrg 	int eq)
348*5971e316Smrg {
349*5971e316Smrg 	isl_size d;
350*5971e316Smrg 	isl_size n_div;
351*5971e316Smrg 	isl_size nparam;
352*5971e316Smrg 	isl_bool empty;
353*5971e316Smrg 	int i;
354*5971e316Smrg 	int p = 0, v = 0;
355*5971e316Smrg 
356*5971e316Smrg 	n_div = isl_basic_set_dim(bset, isl_dim_div);
357*5971e316Smrg 	d = isl_basic_set_dim(bset, isl_dim_set);
358*5971e316Smrg 	nparam = isl_basic_set_dim(bset, isl_dim_param);
359*5971e316Smrg 	if (n_div < 0 || d < 0 || nparam < 0)
360*5971e316Smrg 		return -1;
361*5971e316Smrg 
362*5971e316Smrg 	for (i = 0; i < n_div; ++i) {
363*5971e316Smrg 		if (isl_int_is_zero(c[1 + nparam + d + i]))
364*5971e316Smrg 			continue;
365*5971e316Smrg 		switch (div_purity[i]) {
366*5971e316Smrg 		case PURE_PARAM: p = 1; break;
367*5971e316Smrg 		case PURE_VAR: v = 1; break;
368*5971e316Smrg 		default: return IMPURE;
369*5971e316Smrg 		}
370*5971e316Smrg 	}
371*5971e316Smrg 	if (!p && isl_seq_first_non_zero(c + 1, nparam) == -1)
372*5971e316Smrg 		return PURE_VAR;
373*5971e316Smrg 	if (!v && isl_seq_first_non_zero(c + 1 + nparam, d) == -1)
374*5971e316Smrg 		return PURE_PARAM;
375*5971e316Smrg 
376*5971e316Smrg 	empty = parametric_constant_never_positive(bset, c, div_purity);
377*5971e316Smrg 	if (eq && empty >= 0 && !empty) {
378*5971e316Smrg 		isl_seq_neg(c, c, 1 + nparam + d + n_div);
379*5971e316Smrg 		empty = parametric_constant_never_positive(bset, c, div_purity);
380*5971e316Smrg 	}
381*5971e316Smrg 
382*5971e316Smrg 	return empty < 0 ? -1 : empty ? MIXED : IMPURE;
383*5971e316Smrg }
384*5971e316Smrg 
385*5971e316Smrg /* Return an array of integers indicating the type of each div in bset.
386*5971e316Smrg  * If the div is (recursively) defined in terms of only the parameters,
387*5971e316Smrg  * then the type is PURE_PARAM.
388*5971e316Smrg  * If the div is (recursively) defined in terms of only the set variables,
389*5971e316Smrg  * then the type is PURE_VAR.
390*5971e316Smrg  * Otherwise, the type is IMPURE.
391*5971e316Smrg  */
get_div_purity(__isl_keep isl_basic_set * bset)392*5971e316Smrg static __isl_give int *get_div_purity(__isl_keep isl_basic_set *bset)
393*5971e316Smrg {
394*5971e316Smrg 	int i, j;
395*5971e316Smrg 	int *div_purity;
396*5971e316Smrg 	isl_size d;
397*5971e316Smrg 	isl_size n_div;
398*5971e316Smrg 	isl_size nparam;
399*5971e316Smrg 
400*5971e316Smrg 	n_div = isl_basic_set_dim(bset, isl_dim_div);
401*5971e316Smrg 	d = isl_basic_set_dim(bset, isl_dim_set);
402*5971e316Smrg 	nparam = isl_basic_set_dim(bset, isl_dim_param);
403*5971e316Smrg 	if (n_div < 0 || d < 0 || nparam < 0)
404*5971e316Smrg 		return NULL;
405*5971e316Smrg 
406*5971e316Smrg 	div_purity = isl_alloc_array(bset->ctx, int, n_div);
407*5971e316Smrg 	if (n_div && !div_purity)
408*5971e316Smrg 		return NULL;
409*5971e316Smrg 
410*5971e316Smrg 	for (i = 0; i < bset->n_div; ++i) {
411*5971e316Smrg 		int p = 0, v = 0;
412*5971e316Smrg 		if (isl_int_is_zero(bset->div[i][0])) {
413*5971e316Smrg 			div_purity[i] = IMPURE;
414*5971e316Smrg 			continue;
415*5971e316Smrg 		}
416*5971e316Smrg 		if (isl_seq_first_non_zero(bset->div[i] + 2, nparam) != -1)
417*5971e316Smrg 			p = 1;
418*5971e316Smrg 		if (isl_seq_first_non_zero(bset->div[i] + 2 + nparam, d) != -1)
419*5971e316Smrg 			v = 1;
420*5971e316Smrg 		for (j = 0; j < i; ++j) {
421*5971e316Smrg 			if (isl_int_is_zero(bset->div[i][2 + nparam + d + j]))
422*5971e316Smrg 				continue;
423*5971e316Smrg 			switch (div_purity[j]) {
424*5971e316Smrg 			case PURE_PARAM: p = 1; break;
425*5971e316Smrg 			case PURE_VAR: v = 1; break;
426*5971e316Smrg 			default: p = v = 1; break;
427*5971e316Smrg 			}
428*5971e316Smrg 		}
429*5971e316Smrg 		div_purity[i] = v ? p ? IMPURE : PURE_VAR : PURE_PARAM;
430*5971e316Smrg 	}
431*5971e316Smrg 
432*5971e316Smrg 	return div_purity;
433*5971e316Smrg }
434*5971e316Smrg 
435*5971e316Smrg /* Given a path with the as yet unconstrained length at div position "pos",
436*5971e316Smrg  * check if setting the length to zero results in only the identity
437*5971e316Smrg  * mapping.
438*5971e316Smrg  */
empty_path_is_identity(__isl_keep isl_basic_map * path,unsigned pos)439*5971e316Smrg static isl_bool empty_path_is_identity(__isl_keep isl_basic_map *path,
440*5971e316Smrg 	unsigned pos)
441*5971e316Smrg {
442*5971e316Smrg 	isl_basic_map *test = NULL;
443*5971e316Smrg 	isl_basic_map *id = NULL;
444*5971e316Smrg 	isl_bool is_id;
445*5971e316Smrg 
446*5971e316Smrg 	test = isl_basic_map_copy(path);
447*5971e316Smrg 	test = isl_basic_map_fix_si(test, isl_dim_div, pos, 0);
448*5971e316Smrg 	id = isl_basic_map_identity(isl_basic_map_get_space(path));
449*5971e316Smrg 	is_id = isl_basic_map_is_equal(test, id);
450*5971e316Smrg 	isl_basic_map_free(test);
451*5971e316Smrg 	isl_basic_map_free(id);
452*5971e316Smrg 	return is_id;
453*5971e316Smrg }
454*5971e316Smrg 
455*5971e316Smrg /* If any of the constraints is found to be impure then this function
456*5971e316Smrg  * sets *impurity to 1.
457*5971e316Smrg  *
458*5971e316Smrg  * If impurity is NULL then we are dealing with a non-parametric set
459*5971e316Smrg  * and so the constraints are obviously PURE_VAR.
460*5971e316Smrg  */
add_delta_constraints(__isl_take isl_basic_map * path,__isl_keep isl_basic_set * delta,unsigned off,unsigned nparam,unsigned d,int * div_purity,int eq,int * impurity)461*5971e316Smrg static __isl_give isl_basic_map *add_delta_constraints(
462*5971e316Smrg 	__isl_take isl_basic_map *path,
463*5971e316Smrg 	__isl_keep isl_basic_set *delta, unsigned off, unsigned nparam,
464*5971e316Smrg 	unsigned d, int *div_purity, int eq, int *impurity)
465*5971e316Smrg {
466*5971e316Smrg 	int i, k;
467*5971e316Smrg 	int n = eq ? delta->n_eq : delta->n_ineq;
468*5971e316Smrg 	isl_int **delta_c = eq ? delta->eq : delta->ineq;
469*5971e316Smrg 	isl_size n_div, total;
470*5971e316Smrg 
471*5971e316Smrg 	n_div = isl_basic_set_dim(delta, isl_dim_div);
472*5971e316Smrg 	total = isl_basic_map_dim(path, isl_dim_all);
473*5971e316Smrg 	if (n_div < 0 || total < 0)
474*5971e316Smrg 		return isl_basic_map_free(path);
475*5971e316Smrg 
476*5971e316Smrg 	for (i = 0; i < n; ++i) {
477*5971e316Smrg 		isl_int *path_c;
478*5971e316Smrg 		int p = PURE_VAR;
479*5971e316Smrg 		if (impurity)
480*5971e316Smrg 			p = purity(delta, delta_c[i], div_purity, eq);
481*5971e316Smrg 		if (p < 0)
482*5971e316Smrg 			goto error;
483*5971e316Smrg 		if (p != PURE_VAR && p != PURE_PARAM && !*impurity)
484*5971e316Smrg 			*impurity = 1;
485*5971e316Smrg 		if (p == IMPURE)
486*5971e316Smrg 			continue;
487*5971e316Smrg 		if (eq && p != MIXED) {
488*5971e316Smrg 			k = isl_basic_map_alloc_equality(path);
489*5971e316Smrg 			if (k < 0)
490*5971e316Smrg 				goto error;
491*5971e316Smrg 			path_c = path->eq[k];
492*5971e316Smrg 		} else {
493*5971e316Smrg 			k = isl_basic_map_alloc_inequality(path);
494*5971e316Smrg 			if (k < 0)
495*5971e316Smrg 				goto error;
496*5971e316Smrg 			path_c = path->ineq[k];
497*5971e316Smrg 		}
498*5971e316Smrg 		isl_seq_clr(path_c, 1 + total);
499*5971e316Smrg 		if (p == PURE_VAR) {
500*5971e316Smrg 			isl_seq_cpy(path_c + off,
501*5971e316Smrg 				    delta_c[i] + 1 + nparam, d);
502*5971e316Smrg 			isl_int_set(path_c[off + d], delta_c[i][0]);
503*5971e316Smrg 		} else if (p == PURE_PARAM) {
504*5971e316Smrg 			isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
505*5971e316Smrg 		} else {
506*5971e316Smrg 			isl_seq_cpy(path_c + off,
507*5971e316Smrg 				    delta_c[i] + 1 + nparam, d);
508*5971e316Smrg 			isl_seq_cpy(path_c, delta_c[i], 1 + nparam);
509*5971e316Smrg 		}
510*5971e316Smrg 		isl_seq_cpy(path_c + off - n_div,
511*5971e316Smrg 			    delta_c[i] + 1 + nparam + d, n_div);
512*5971e316Smrg 	}
513*5971e316Smrg 
514*5971e316Smrg 	return path;
515*5971e316Smrg error:
516*5971e316Smrg 	isl_basic_map_free(path);
517*5971e316Smrg 	return NULL;
518*5971e316Smrg }
519*5971e316Smrg 
520*5971e316Smrg /* Given a set of offsets "delta", construct a relation of the
521*5971e316Smrg  * given dimension specification (Z^{n+1} -> Z^{n+1}) that
522*5971e316Smrg  * is an overapproximation of the relations that
523*5971e316Smrg  * maps an element x to any element that can be reached
524*5971e316Smrg  * by taking a non-negative number of steps along any of
525*5971e316Smrg  * the elements in "delta".
526*5971e316Smrg  * That is, construct an approximation of
527*5971e316Smrg  *
528*5971e316Smrg  *	{ [x] -> [y] : exists f \in \delta, k \in Z :
529*5971e316Smrg  *					y = x + k [f, 1] and k >= 0 }
530*5971e316Smrg  *
531*5971e316Smrg  * For any element in this relation, the number of steps taken
532*5971e316Smrg  * is equal to the difference in the final coordinates.
533*5971e316Smrg  *
534*5971e316Smrg  * In particular, let delta be defined as
535*5971e316Smrg  *
536*5971e316Smrg  *	\delta = [p] -> { [x] : A x + a >= 0 and B p + b >= 0 and
537*5971e316Smrg  *				C x + C'p + c >= 0 and
538*5971e316Smrg  *				D x + D'p + d >= 0 }
539*5971e316Smrg  *
540*5971e316Smrg  * where the constraints C x + C'p + c >= 0 are such that the parametric
541*5971e316Smrg  * constant term of each constraint j, "C_j x + C'_j p + c_j",
542*5971e316Smrg  * can never attain positive values, then the relation is constructed as
543*5971e316Smrg  *
544*5971e316Smrg  *	{ [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
545*5971e316Smrg  *			A f + k a >= 0 and B p + b >= 0 and
546*5971e316Smrg  *			C f + C'p + c >= 0 and k >= 1 }
547*5971e316Smrg  *	union { [x] -> [x] }
548*5971e316Smrg  *
549*5971e316Smrg  * If the zero-length paths happen to correspond exactly to the identity
550*5971e316Smrg  * mapping, then we return
551*5971e316Smrg  *
552*5971e316Smrg  *	{ [x] -> [y] : exists [f, k] \in Z^{n+1} : y = x + f and
553*5971e316Smrg  *			A f + k a >= 0 and B p + b >= 0 and
554*5971e316Smrg  *			C f + C'p + c >= 0 and k >= 0 }
555*5971e316Smrg  *
556*5971e316Smrg  * instead.
557*5971e316Smrg  *
558*5971e316Smrg  * Existentially quantified variables in \delta are handled by
559*5971e316Smrg  * classifying them as independent of the parameters, purely
560*5971e316Smrg  * parameter dependent and others.  Constraints containing
561*5971e316Smrg  * any of the other existentially quantified variables are removed.
562*5971e316Smrg  * This is safe, but leads to an additional overapproximation.
563*5971e316Smrg  *
564*5971e316Smrg  * If there are any impure constraints, then we also eliminate
565*5971e316Smrg  * the parameters from \delta, resulting in a set
566*5971e316Smrg  *
567*5971e316Smrg  *	\delta' = { [x] : E x + e >= 0 }
568*5971e316Smrg  *
569*5971e316Smrg  * and add the constraints
570*5971e316Smrg  *
571*5971e316Smrg  *			E f + k e >= 0
572*5971e316Smrg  *
573*5971e316Smrg  * to the constructed relation.
574*5971e316Smrg  */
path_along_delta(__isl_take isl_space * space,__isl_take isl_basic_set * delta)575*5971e316Smrg static __isl_give isl_map *path_along_delta(__isl_take isl_space *space,
576*5971e316Smrg 	__isl_take isl_basic_set *delta)
577*5971e316Smrg {
578*5971e316Smrg 	isl_basic_map *path = NULL;
579*5971e316Smrg 	isl_size d;
580*5971e316Smrg 	isl_size n_div;
581*5971e316Smrg 	isl_size nparam;
582*5971e316Smrg 	isl_size total;
583*5971e316Smrg 	unsigned off;
584*5971e316Smrg 	int i, k;
585*5971e316Smrg 	isl_bool is_id;
586*5971e316Smrg 	int *div_purity = NULL;
587*5971e316Smrg 	int impurity = 0;
588*5971e316Smrg 
589*5971e316Smrg 	n_div = isl_basic_set_dim(delta, isl_dim_div);
590*5971e316Smrg 	d = isl_basic_set_dim(delta, isl_dim_set);
591*5971e316Smrg 	nparam = isl_basic_set_dim(delta, isl_dim_param);
592*5971e316Smrg 	if (n_div < 0 || d < 0 || nparam < 0)
593*5971e316Smrg 		goto error;
594*5971e316Smrg 	path = isl_basic_map_alloc_space(isl_space_copy(space), n_div + d + 1,
595*5971e316Smrg 			d + 1 + delta->n_eq, delta->n_eq + delta->n_ineq + 1);
596*5971e316Smrg 	off = 1 + nparam + 2 * (d + 1) + n_div;
597*5971e316Smrg 
598*5971e316Smrg 	for (i = 0; i < n_div + d + 1; ++i) {
599*5971e316Smrg 		k = isl_basic_map_alloc_div(path);
600*5971e316Smrg 		if (k < 0)
601*5971e316Smrg 			goto error;
602*5971e316Smrg 		isl_int_set_si(path->div[k][0], 0);
603*5971e316Smrg 	}
604*5971e316Smrg 
605*5971e316Smrg 	total = isl_basic_map_dim(path, isl_dim_all);
606*5971e316Smrg 	if (total < 0)
607*5971e316Smrg 		goto error;
608*5971e316Smrg 	for (i = 0; i < d + 1; ++i) {
609*5971e316Smrg 		k = isl_basic_map_alloc_equality(path);
610*5971e316Smrg 		if (k < 0)
611*5971e316Smrg 			goto error;
612*5971e316Smrg 		isl_seq_clr(path->eq[k], 1 + total);
613*5971e316Smrg 		isl_int_set_si(path->eq[k][1 + nparam + i], 1);
614*5971e316Smrg 		isl_int_set_si(path->eq[k][1 + nparam + d + 1 + i], -1);
615*5971e316Smrg 		isl_int_set_si(path->eq[k][off + i], 1);
616*5971e316Smrg 	}
617*5971e316Smrg 
618*5971e316Smrg 	div_purity = get_div_purity(delta);
619*5971e316Smrg 	if (n_div && !div_purity)
620*5971e316Smrg 		goto error;
621*5971e316Smrg 
622*5971e316Smrg 	path = add_delta_constraints(path, delta, off, nparam, d,
623*5971e316Smrg 				     div_purity, 1, &impurity);
624*5971e316Smrg 	path = add_delta_constraints(path, delta, off, nparam, d,
625*5971e316Smrg 				     div_purity, 0, &impurity);
626*5971e316Smrg 	if (impurity) {
627*5971e316Smrg 		isl_space *space = isl_basic_set_get_space(delta);
628*5971e316Smrg 		delta = isl_basic_set_project_out(delta,
629*5971e316Smrg 						  isl_dim_param, 0, nparam);
630*5971e316Smrg 		delta = isl_basic_set_add_dims(delta, isl_dim_param, nparam);
631*5971e316Smrg 		delta = isl_basic_set_reset_space(delta, space);
632*5971e316Smrg 		if (!delta)
633*5971e316Smrg 			goto error;
634*5971e316Smrg 		path = isl_basic_map_extend_constraints(path, delta->n_eq,
635*5971e316Smrg 							delta->n_ineq + 1);
636*5971e316Smrg 		path = add_delta_constraints(path, delta, off, nparam, d,
637*5971e316Smrg 					     NULL, 1, NULL);
638*5971e316Smrg 		path = add_delta_constraints(path, delta, off, nparam, d,
639*5971e316Smrg 					     NULL, 0, NULL);
640*5971e316Smrg 		path = isl_basic_map_gauss(path, NULL);
641*5971e316Smrg 	}
642*5971e316Smrg 
643*5971e316Smrg 	is_id = empty_path_is_identity(path, n_div + d);
644*5971e316Smrg 	if (is_id < 0)
645*5971e316Smrg 		goto error;
646*5971e316Smrg 
647*5971e316Smrg 	k = isl_basic_map_alloc_inequality(path);
648*5971e316Smrg 	if (k < 0)
649*5971e316Smrg 		goto error;
650*5971e316Smrg 	isl_seq_clr(path->ineq[k], 1 + total);
651*5971e316Smrg 	if (!is_id)
652*5971e316Smrg 		isl_int_set_si(path->ineq[k][0], -1);
653*5971e316Smrg 	isl_int_set_si(path->ineq[k][off + d], 1);
654*5971e316Smrg 
655*5971e316Smrg 	free(div_purity);
656*5971e316Smrg 	isl_basic_set_free(delta);
657*5971e316Smrg 	path = isl_basic_map_finalize(path);
658*5971e316Smrg 	if (is_id) {
659*5971e316Smrg 		isl_space_free(space);
660*5971e316Smrg 		return isl_map_from_basic_map(path);
661*5971e316Smrg 	}
662*5971e316Smrg 	return isl_basic_map_union(path, isl_basic_map_identity(space));
663*5971e316Smrg error:
664*5971e316Smrg 	free(div_purity);
665*5971e316Smrg 	isl_space_free(space);
666*5971e316Smrg 	isl_basic_set_free(delta);
667*5971e316Smrg 	isl_basic_map_free(path);
668*5971e316Smrg 	return NULL;
669*5971e316Smrg }
670*5971e316Smrg 
671*5971e316Smrg /* Given a dimension specification Z^{n+1} -> Z^{n+1} and a parameter "param",
672*5971e316Smrg  * construct a map that equates the parameter to the difference
673*5971e316Smrg  * in the final coordinates and imposes that this difference is positive.
674*5971e316Smrg  * That is, construct
675*5971e316Smrg  *
676*5971e316Smrg  *	{ [x,x_s] -> [y,y_s] : k = y_s - x_s > 0 }
677*5971e316Smrg  */
equate_parameter_to_length(__isl_take isl_space * space,unsigned param)678*5971e316Smrg static __isl_give isl_map *equate_parameter_to_length(
679*5971e316Smrg 	__isl_take isl_space *space, unsigned param)
680*5971e316Smrg {
681*5971e316Smrg 	struct isl_basic_map *bmap;
682*5971e316Smrg 	isl_size d;
683*5971e316Smrg 	isl_size nparam;
684*5971e316Smrg 	isl_size total;
685*5971e316Smrg 	int k;
686*5971e316Smrg 
687*5971e316Smrg 	d = isl_space_dim(space, isl_dim_in);
688*5971e316Smrg 	nparam = isl_space_dim(space, isl_dim_param);
689*5971e316Smrg 	total = isl_space_dim(space, isl_dim_all);
690*5971e316Smrg 	if (d < 0 || nparam < 0 || total < 0)
691*5971e316Smrg 		space = isl_space_free(space);
692*5971e316Smrg 	bmap = isl_basic_map_alloc_space(space, 0, 1, 1);
693*5971e316Smrg 	k = isl_basic_map_alloc_equality(bmap);
694*5971e316Smrg 	if (k < 0)
695*5971e316Smrg 		goto error;
696*5971e316Smrg 	isl_seq_clr(bmap->eq[k], 1 + total);
697*5971e316Smrg 	isl_int_set_si(bmap->eq[k][1 + param], -1);
698*5971e316Smrg 	isl_int_set_si(bmap->eq[k][1 + nparam + d - 1], -1);
699*5971e316Smrg 	isl_int_set_si(bmap->eq[k][1 + nparam + d + d - 1], 1);
700*5971e316Smrg 
701*5971e316Smrg 	k = isl_basic_map_alloc_inequality(bmap);
702*5971e316Smrg 	if (k < 0)
703*5971e316Smrg 		goto error;
704*5971e316Smrg 	isl_seq_clr(bmap->ineq[k], 1 + total);
705*5971e316Smrg 	isl_int_set_si(bmap->ineq[k][1 + param], 1);
706*5971e316Smrg 	isl_int_set_si(bmap->ineq[k][0], -1);
707*5971e316Smrg 
708*5971e316Smrg 	bmap = isl_basic_map_finalize(bmap);
709*5971e316Smrg 	return isl_map_from_basic_map(bmap);
710*5971e316Smrg error:
711*5971e316Smrg 	isl_basic_map_free(bmap);
712*5971e316Smrg 	return NULL;
713*5971e316Smrg }
714*5971e316Smrg 
715*5971e316Smrg /* Check whether "path" is acyclic, where the last coordinates of domain
716*5971e316Smrg  * and range of path encode the number of steps taken.
717*5971e316Smrg  * That is, check whether
718*5971e316Smrg  *
719*5971e316Smrg  *	{ d | d = y - x and (x,y) in path }
720*5971e316Smrg  *
721*5971e316Smrg  * does not contain any element with positive last coordinate (positive length)
722*5971e316Smrg  * and zero remaining coordinates (cycle).
723*5971e316Smrg  */
is_acyclic(__isl_take isl_map * path)724*5971e316Smrg static isl_bool is_acyclic(__isl_take isl_map *path)
725*5971e316Smrg {
726*5971e316Smrg 	int i;
727*5971e316Smrg 	isl_bool acyclic;
728*5971e316Smrg 	isl_size dim;
729*5971e316Smrg 	struct isl_set *delta;
730*5971e316Smrg 
731*5971e316Smrg 	delta = isl_map_deltas(path);
732*5971e316Smrg 	dim = isl_set_dim(delta, isl_dim_set);
733*5971e316Smrg 	if (dim < 0)
734*5971e316Smrg 		delta = isl_set_free(delta);
735*5971e316Smrg 	for (i = 0; i < dim; ++i) {
736*5971e316Smrg 		if (i == dim -1)
737*5971e316Smrg 			delta = isl_set_lower_bound_si(delta, isl_dim_set, i, 1);
738*5971e316Smrg 		else
739*5971e316Smrg 			delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
740*5971e316Smrg 	}
741*5971e316Smrg 
742*5971e316Smrg 	acyclic = isl_set_is_empty(delta);
743*5971e316Smrg 	isl_set_free(delta);
744*5971e316Smrg 
745*5971e316Smrg 	return acyclic;
746*5971e316Smrg }
747*5971e316Smrg 
748*5971e316Smrg /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
749*5971e316Smrg  * and a dimension specification (Z^{n+1} -> Z^{n+1}),
750*5971e316Smrg  * construct a map that is an overapproximation of the map
751*5971e316Smrg  * that takes an element from the space D \times Z to another
752*5971e316Smrg  * element from the same space, such that the first n coordinates of the
753*5971e316Smrg  * difference between them is a sum of differences between images
754*5971e316Smrg  * and pre-images in one of the R_i and such that the last coordinate
755*5971e316Smrg  * is equal to the number of steps taken.
756*5971e316Smrg  * That is, let
757*5971e316Smrg  *
758*5971e316Smrg  *	\Delta_i = { y - x | (x, y) in R_i }
759*5971e316Smrg  *
760*5971e316Smrg  * then the constructed map is an overapproximation of
761*5971e316Smrg  *
762*5971e316Smrg  *	{ (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
763*5971e316Smrg  *				d = (\sum_i k_i \delta_i, \sum_i k_i) }
764*5971e316Smrg  *
765*5971e316Smrg  * The elements of the singleton \Delta_i's are collected as the
766*5971e316Smrg  * rows of the steps matrix.  For all these \Delta_i's together,
767*5971e316Smrg  * a single path is constructed.
768*5971e316Smrg  * For each of the other \Delta_i's, we compute an overapproximation
769*5971e316Smrg  * of the paths along elements of \Delta_i.
770*5971e316Smrg  * Since each of these paths performs an addition, composition is
771*5971e316Smrg  * symmetric and we can simply compose all resulting paths in any order.
772*5971e316Smrg  */
construct_extended_path(__isl_take isl_space * space,__isl_keep isl_map * map,int * project)773*5971e316Smrg static __isl_give isl_map *construct_extended_path(__isl_take isl_space *space,
774*5971e316Smrg 	__isl_keep isl_map *map, int *project)
775*5971e316Smrg {
776*5971e316Smrg 	struct isl_mat *steps = NULL;
777*5971e316Smrg 	struct isl_map *path = NULL;
778*5971e316Smrg 	isl_size d;
779*5971e316Smrg 	int i, j, n;
780*5971e316Smrg 
781*5971e316Smrg 	d = isl_map_dim(map, isl_dim_in);
782*5971e316Smrg 	if (d < 0)
783*5971e316Smrg 		goto error;
784*5971e316Smrg 
785*5971e316Smrg 	path = isl_map_identity(isl_space_copy(space));
786*5971e316Smrg 
787*5971e316Smrg 	steps = isl_mat_alloc(map->ctx, map->n, d);
788*5971e316Smrg 	if (!steps)
789*5971e316Smrg 		goto error;
790*5971e316Smrg 
791*5971e316Smrg 	n = 0;
792*5971e316Smrg 	for (i = 0; i < map->n; ++i) {
793*5971e316Smrg 		struct isl_basic_set *delta;
794*5971e316Smrg 
795*5971e316Smrg 		delta = isl_basic_map_deltas(isl_basic_map_copy(map->p[i]));
796*5971e316Smrg 
797*5971e316Smrg 		for (j = 0; j < d; ++j) {
798*5971e316Smrg 			isl_bool fixed;
799*5971e316Smrg 
800*5971e316Smrg 			fixed = isl_basic_set_plain_dim_is_fixed(delta, j,
801*5971e316Smrg 							    &steps->row[n][j]);
802*5971e316Smrg 			if (fixed < 0) {
803*5971e316Smrg 				isl_basic_set_free(delta);
804*5971e316Smrg 				goto error;
805*5971e316Smrg 			}
806*5971e316Smrg 			if (!fixed)
807*5971e316Smrg 				break;
808*5971e316Smrg 		}
809*5971e316Smrg 
810*5971e316Smrg 
811*5971e316Smrg 		if (j < d) {
812*5971e316Smrg 			path = isl_map_apply_range(path,
813*5971e316Smrg 				path_along_delta(isl_space_copy(space), delta));
814*5971e316Smrg 			path = isl_map_coalesce(path);
815*5971e316Smrg 		} else {
816*5971e316Smrg 			isl_basic_set_free(delta);
817*5971e316Smrg 			++n;
818*5971e316Smrg 		}
819*5971e316Smrg 	}
820*5971e316Smrg 
821*5971e316Smrg 	if (n > 0) {
822*5971e316Smrg 		steps->n_row = n;
823*5971e316Smrg 		path = isl_map_apply_range(path,
824*5971e316Smrg 				path_along_steps(isl_space_copy(space), steps));
825*5971e316Smrg 	}
826*5971e316Smrg 
827*5971e316Smrg 	if (project && *project) {
828*5971e316Smrg 		*project = is_acyclic(isl_map_copy(path));
829*5971e316Smrg 		if (*project < 0)
830*5971e316Smrg 			goto error;
831*5971e316Smrg 	}
832*5971e316Smrg 
833*5971e316Smrg 	isl_space_free(space);
834*5971e316Smrg 	isl_mat_free(steps);
835*5971e316Smrg 	return path;
836*5971e316Smrg error:
837*5971e316Smrg 	isl_space_free(space);
838*5971e316Smrg 	isl_mat_free(steps);
839*5971e316Smrg 	isl_map_free(path);
840*5971e316Smrg 	return NULL;
841*5971e316Smrg }
842*5971e316Smrg 
isl_set_overlaps(__isl_keep isl_set * set1,__isl_keep isl_set * set2)843*5971e316Smrg static isl_bool isl_set_overlaps(__isl_keep isl_set *set1,
844*5971e316Smrg 	__isl_keep isl_set *set2)
845*5971e316Smrg {
846*5971e316Smrg 	return isl_bool_not(isl_set_is_disjoint(set1, set2));
847*5971e316Smrg }
848*5971e316Smrg 
849*5971e316Smrg /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
850*5971e316Smrg  * and a dimension specification (Z^{n+1} -> Z^{n+1}),
851*5971e316Smrg  * construct a map that is an overapproximation of the map
852*5971e316Smrg  * that takes an element from the dom R \times Z to an
853*5971e316Smrg  * element from ran R \times Z, such that the first n coordinates of the
854*5971e316Smrg  * difference between them is a sum of differences between images
855*5971e316Smrg  * and pre-images in one of the R_i and such that the last coordinate
856*5971e316Smrg  * is equal to the number of steps taken.
857*5971e316Smrg  * That is, let
858*5971e316Smrg  *
859*5971e316Smrg  *	\Delta_i = { y - x | (x, y) in R_i }
860*5971e316Smrg  *
861*5971e316Smrg  * then the constructed map is an overapproximation of
862*5971e316Smrg  *
863*5971e316Smrg  *	{ (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
864*5971e316Smrg  *				d = (\sum_i k_i \delta_i, \sum_i k_i) and
865*5971e316Smrg  *				x in dom R and x + d in ran R and
866*5971e316Smrg  *				\sum_i k_i >= 1 }
867*5971e316Smrg  */
construct_component(__isl_take isl_space * space,__isl_keep isl_map * map,isl_bool * exact,int project)868*5971e316Smrg static __isl_give isl_map *construct_component(__isl_take isl_space *space,
869*5971e316Smrg 	__isl_keep isl_map *map, isl_bool *exact, int project)
870*5971e316Smrg {
871*5971e316Smrg 	struct isl_set *domain = NULL;
872*5971e316Smrg 	struct isl_set *range = NULL;
873*5971e316Smrg 	struct isl_map *app = NULL;
874*5971e316Smrg 	struct isl_map *path = NULL;
875*5971e316Smrg 	isl_bool overlaps;
876*5971e316Smrg 	int check;
877*5971e316Smrg 
878*5971e316Smrg 	domain = isl_map_domain(isl_map_copy(map));
879*5971e316Smrg 	domain = isl_set_coalesce(domain);
880*5971e316Smrg 	range = isl_map_range(isl_map_copy(map));
881*5971e316Smrg 	range = isl_set_coalesce(range);
882*5971e316Smrg 	overlaps = isl_set_overlaps(domain, range);
883*5971e316Smrg 	if (overlaps < 0 || !overlaps) {
884*5971e316Smrg 		isl_set_free(domain);
885*5971e316Smrg 		isl_set_free(range);
886*5971e316Smrg 		isl_space_free(space);
887*5971e316Smrg 
888*5971e316Smrg 		if (overlaps < 0)
889*5971e316Smrg 			map = NULL;
890*5971e316Smrg 		map = isl_map_copy(map);
891*5971e316Smrg 		map = isl_map_add_dims(map, isl_dim_in, 1);
892*5971e316Smrg 		map = isl_map_add_dims(map, isl_dim_out, 1);
893*5971e316Smrg 		map = set_path_length(map, 1, 1);
894*5971e316Smrg 		return map;
895*5971e316Smrg 	}
896*5971e316Smrg 	app = isl_map_from_domain_and_range(domain, range);
897*5971e316Smrg 	app = isl_map_add_dims(app, isl_dim_in, 1);
898*5971e316Smrg 	app = isl_map_add_dims(app, isl_dim_out, 1);
899*5971e316Smrg 
900*5971e316Smrg 	check = exact && *exact == isl_bool_true;
901*5971e316Smrg 	path = construct_extended_path(isl_space_copy(space), map,
902*5971e316Smrg 					check ? &project : NULL);
903*5971e316Smrg 	app = isl_map_intersect(app, path);
904*5971e316Smrg 
905*5971e316Smrg 	if (check &&
906*5971e316Smrg 	    (*exact = check_exactness(isl_map_copy(map), isl_map_copy(app),
907*5971e316Smrg 				      project)) < 0)
908*5971e316Smrg 		goto error;
909*5971e316Smrg 
910*5971e316Smrg 	isl_space_free(space);
911*5971e316Smrg 	app = set_path_length(app, 0, 1);
912*5971e316Smrg 	return app;
913*5971e316Smrg error:
914*5971e316Smrg 	isl_space_free(space);
915*5971e316Smrg 	isl_map_free(app);
916*5971e316Smrg 	return NULL;
917*5971e316Smrg }
918*5971e316Smrg 
919*5971e316Smrg /* Call construct_component and, if "project" is set, project out
920*5971e316Smrg  * the final coordinates.
921*5971e316Smrg  */
construct_projected_component(__isl_take isl_space * space,__isl_keep isl_map * map,isl_bool * exact,int project)922*5971e316Smrg static __isl_give isl_map *construct_projected_component(
923*5971e316Smrg 	__isl_take isl_space *space,
924*5971e316Smrg 	__isl_keep isl_map *map, isl_bool *exact, int project)
925*5971e316Smrg {
926*5971e316Smrg 	isl_map *app;
927*5971e316Smrg 	unsigned d;
928*5971e316Smrg 
929*5971e316Smrg 	if (!space)
930*5971e316Smrg 		return NULL;
931*5971e316Smrg 	d = isl_space_dim(space, isl_dim_in);
932*5971e316Smrg 
933*5971e316Smrg 	app = construct_component(space, map, exact, project);
934*5971e316Smrg 	if (project) {
935*5971e316Smrg 		app = isl_map_project_out(app, isl_dim_in, d - 1, 1);
936*5971e316Smrg 		app = isl_map_project_out(app, isl_dim_out, d - 1, 1);
937*5971e316Smrg 	}
938*5971e316Smrg 	return app;
939*5971e316Smrg }
940*5971e316Smrg 
941*5971e316Smrg /* Compute an extended version, i.e., with path lengths, of
942*5971e316Smrg  * an overapproximation of the transitive closure of "bmap"
943*5971e316Smrg  * with path lengths greater than or equal to zero and with
944*5971e316Smrg  * domain and range equal to "dom".
945*5971e316Smrg  */
q_closure(__isl_take isl_space * space,__isl_take isl_set * dom,__isl_keep isl_basic_map * bmap,isl_bool * exact)946*5971e316Smrg static __isl_give isl_map *q_closure(__isl_take isl_space *space,
947*5971e316Smrg 	__isl_take isl_set *dom, __isl_keep isl_basic_map *bmap,
948*5971e316Smrg 	isl_bool *exact)
949*5971e316Smrg {
950*5971e316Smrg 	int project = 1;
951*5971e316Smrg 	isl_map *path;
952*5971e316Smrg 	isl_map *map;
953*5971e316Smrg 	isl_map *app;
954*5971e316Smrg 
955*5971e316Smrg 	dom = isl_set_add_dims(dom, isl_dim_set, 1);
956*5971e316Smrg 	app = isl_map_from_domain_and_range(dom, isl_set_copy(dom));
957*5971e316Smrg 	map = isl_map_from_basic_map(isl_basic_map_copy(bmap));
958*5971e316Smrg 	path = construct_extended_path(space, map, &project);
959*5971e316Smrg 	app = isl_map_intersect(app, path);
960*5971e316Smrg 
961*5971e316Smrg 	if ((*exact = check_exactness(map, isl_map_copy(app), project)) < 0)
962*5971e316Smrg 		goto error;
963*5971e316Smrg 
964*5971e316Smrg 	return app;
965*5971e316Smrg error:
966*5971e316Smrg 	isl_map_free(app);
967*5971e316Smrg 	return NULL;
968*5971e316Smrg }
969*5971e316Smrg 
970*5971e316Smrg /* Check whether qc has any elements of length at least one
971*5971e316Smrg  * with domain and/or range outside of dom and ran.
972*5971e316Smrg  */
has_spurious_elements(__isl_keep isl_map * qc,__isl_keep isl_set * dom,__isl_keep isl_set * ran)973*5971e316Smrg static isl_bool has_spurious_elements(__isl_keep isl_map *qc,
974*5971e316Smrg 	__isl_keep isl_set *dom, __isl_keep isl_set *ran)
975*5971e316Smrg {
976*5971e316Smrg 	isl_set *s;
977*5971e316Smrg 	isl_bool subset;
978*5971e316Smrg 	isl_size d;
979*5971e316Smrg 
980*5971e316Smrg 	d = isl_map_dim(qc, isl_dim_in);
981*5971e316Smrg 	if (d < 0 || !dom || !ran)
982*5971e316Smrg 		return isl_bool_error;
983*5971e316Smrg 
984*5971e316Smrg 	qc = isl_map_copy(qc);
985*5971e316Smrg 	qc = set_path_length(qc, 0, 1);
986*5971e316Smrg 	qc = isl_map_project_out(qc, isl_dim_in, d - 1, 1);
987*5971e316Smrg 	qc = isl_map_project_out(qc, isl_dim_out, d - 1, 1);
988*5971e316Smrg 
989*5971e316Smrg 	s = isl_map_domain(isl_map_copy(qc));
990*5971e316Smrg 	subset = isl_set_is_subset(s, dom);
991*5971e316Smrg 	isl_set_free(s);
992*5971e316Smrg 	if (subset < 0)
993*5971e316Smrg 		goto error;
994*5971e316Smrg 	if (!subset) {
995*5971e316Smrg 		isl_map_free(qc);
996*5971e316Smrg 		return isl_bool_true;
997*5971e316Smrg 	}
998*5971e316Smrg 
999*5971e316Smrg 	s = isl_map_range(qc);
1000*5971e316Smrg 	subset = isl_set_is_subset(s, ran);
1001*5971e316Smrg 	isl_set_free(s);
1002*5971e316Smrg 
1003*5971e316Smrg 	return isl_bool_not(subset);
1004*5971e316Smrg error:
1005*5971e316Smrg 	isl_map_free(qc);
1006*5971e316Smrg 	return isl_bool_error;
1007*5971e316Smrg }
1008*5971e316Smrg 
1009*5971e316Smrg #define LEFT	2
1010*5971e316Smrg #define RIGHT	1
1011*5971e316Smrg 
1012*5971e316Smrg /* For each basic map in "map", except i, check whether it combines
1013*5971e316Smrg  * with the transitive closure that is reflexive on C combines
1014*5971e316Smrg  * to the left and to the right.
1015*5971e316Smrg  *
1016*5971e316Smrg  * In particular, if
1017*5971e316Smrg  *
1018*5971e316Smrg  *	dom map_j \subseteq C
1019*5971e316Smrg  *
1020*5971e316Smrg  * then right[j] is set to 1.  Otherwise, if
1021*5971e316Smrg  *
1022*5971e316Smrg  *	ran map_i \cap dom map_j = \emptyset
1023*5971e316Smrg  *
1024*5971e316Smrg  * then right[j] is set to 0.  Otherwise, composing to the right
1025*5971e316Smrg  * is impossible.
1026*5971e316Smrg  *
1027*5971e316Smrg  * Similar, for composing to the left, we have if
1028*5971e316Smrg  *
1029*5971e316Smrg  *	ran map_j \subseteq C
1030*5971e316Smrg  *
1031*5971e316Smrg  * then left[j] is set to 1.  Otherwise, if
1032*5971e316Smrg  *
1033*5971e316Smrg  *	dom map_i \cap ran map_j = \emptyset
1034*5971e316Smrg  *
1035*5971e316Smrg  * then left[j] is set to 0.  Otherwise, composing to the left
1036*5971e316Smrg  * is impossible.
1037*5971e316Smrg  *
1038*5971e316Smrg  * The return value is or'd with LEFT if composing to the left
1039*5971e316Smrg  * is possible and with RIGHT if composing to the right is possible.
1040*5971e316Smrg  */
composability(__isl_keep isl_set * C,int i,isl_set ** dom,isl_set ** ran,int * left,int * right,__isl_keep isl_map * map)1041*5971e316Smrg static int composability(__isl_keep isl_set *C, int i,
1042*5971e316Smrg 	isl_set **dom, isl_set **ran, int *left, int *right,
1043*5971e316Smrg 	__isl_keep isl_map *map)
1044*5971e316Smrg {
1045*5971e316Smrg 	int j;
1046*5971e316Smrg 	int ok;
1047*5971e316Smrg 
1048*5971e316Smrg 	ok = LEFT | RIGHT;
1049*5971e316Smrg 	for (j = 0; j < map->n && ok; ++j) {
1050*5971e316Smrg 		isl_bool overlaps, subset;
1051*5971e316Smrg 		if (j == i)
1052*5971e316Smrg 			continue;
1053*5971e316Smrg 
1054*5971e316Smrg 		if (ok & RIGHT) {
1055*5971e316Smrg 			if (!dom[j])
1056*5971e316Smrg 				dom[j] = isl_set_from_basic_set(
1057*5971e316Smrg 					isl_basic_map_domain(
1058*5971e316Smrg 						isl_basic_map_copy(map->p[j])));
1059*5971e316Smrg 			if (!dom[j])
1060*5971e316Smrg 				return -1;
1061*5971e316Smrg 			overlaps = isl_set_overlaps(ran[i], dom[j]);
1062*5971e316Smrg 			if (overlaps < 0)
1063*5971e316Smrg 				return -1;
1064*5971e316Smrg 			if (!overlaps)
1065*5971e316Smrg 				right[j] = 0;
1066*5971e316Smrg 			else {
1067*5971e316Smrg 				subset = isl_set_is_subset(dom[j], C);
1068*5971e316Smrg 				if (subset < 0)
1069*5971e316Smrg 					return -1;
1070*5971e316Smrg 				if (subset)
1071*5971e316Smrg 					right[j] = 1;
1072*5971e316Smrg 				else
1073*5971e316Smrg 					ok &= ~RIGHT;
1074*5971e316Smrg 			}
1075*5971e316Smrg 		}
1076*5971e316Smrg 
1077*5971e316Smrg 		if (ok & LEFT) {
1078*5971e316Smrg 			if (!ran[j])
1079*5971e316Smrg 				ran[j] = isl_set_from_basic_set(
1080*5971e316Smrg 					isl_basic_map_range(
1081*5971e316Smrg 						isl_basic_map_copy(map->p[j])));
1082*5971e316Smrg 			if (!ran[j])
1083*5971e316Smrg 				return -1;
1084*5971e316Smrg 			overlaps = isl_set_overlaps(dom[i], ran[j]);
1085*5971e316Smrg 			if (overlaps < 0)
1086*5971e316Smrg 				return -1;
1087*5971e316Smrg 			if (!overlaps)
1088*5971e316Smrg 				left[j] = 0;
1089*5971e316Smrg 			else {
1090*5971e316Smrg 				subset = isl_set_is_subset(ran[j], C);
1091*5971e316Smrg 				if (subset < 0)
1092*5971e316Smrg 					return -1;
1093*5971e316Smrg 				if (subset)
1094*5971e316Smrg 					left[j] = 1;
1095*5971e316Smrg 				else
1096*5971e316Smrg 					ok &= ~LEFT;
1097*5971e316Smrg 			}
1098*5971e316Smrg 		}
1099*5971e316Smrg 	}
1100*5971e316Smrg 
1101*5971e316Smrg 	return ok;
1102*5971e316Smrg }
1103*5971e316Smrg 
anonymize(__isl_take isl_map * map)1104*5971e316Smrg static __isl_give isl_map *anonymize(__isl_take isl_map *map)
1105*5971e316Smrg {
1106*5971e316Smrg 	map = isl_map_reset(map, isl_dim_in);
1107*5971e316Smrg 	map = isl_map_reset(map, isl_dim_out);
1108*5971e316Smrg 	return map;
1109*5971e316Smrg }
1110*5971e316Smrg 
1111*5971e316Smrg /* Return a map that is a union of the basic maps in "map", except i,
1112*5971e316Smrg  * composed to left and right with qc based on the entries of "left"
1113*5971e316Smrg  * and "right".
1114*5971e316Smrg  */
compose(__isl_keep isl_map * map,int i,__isl_take isl_map * qc,int * left,int * right)1115*5971e316Smrg static __isl_give isl_map *compose(__isl_keep isl_map *map, int i,
1116*5971e316Smrg 	__isl_take isl_map *qc, int *left, int *right)
1117*5971e316Smrg {
1118*5971e316Smrg 	int j;
1119*5971e316Smrg 	isl_map *comp;
1120*5971e316Smrg 
1121*5971e316Smrg 	comp = isl_map_empty(isl_map_get_space(map));
1122*5971e316Smrg 	for (j = 0; j < map->n; ++j) {
1123*5971e316Smrg 		isl_map *map_j;
1124*5971e316Smrg 
1125*5971e316Smrg 		if (j == i)
1126*5971e316Smrg 			continue;
1127*5971e316Smrg 
1128*5971e316Smrg 		map_j = isl_map_from_basic_map(isl_basic_map_copy(map->p[j]));
1129*5971e316Smrg 		map_j = anonymize(map_j);
1130*5971e316Smrg 		if (left && left[j])
1131*5971e316Smrg 			map_j = isl_map_apply_range(map_j, isl_map_copy(qc));
1132*5971e316Smrg 		if (right && right[j])
1133*5971e316Smrg 			map_j = isl_map_apply_range(isl_map_copy(qc), map_j);
1134*5971e316Smrg 		comp = isl_map_union(comp, map_j);
1135*5971e316Smrg 	}
1136*5971e316Smrg 
1137*5971e316Smrg 	comp = isl_map_compute_divs(comp);
1138*5971e316Smrg 	comp = isl_map_coalesce(comp);
1139*5971e316Smrg 
1140*5971e316Smrg 	isl_map_free(qc);
1141*5971e316Smrg 
1142*5971e316Smrg 	return comp;
1143*5971e316Smrg }
1144*5971e316Smrg 
1145*5971e316Smrg /* Compute the transitive closure of "map" incrementally by
1146*5971e316Smrg  * computing
1147*5971e316Smrg  *
1148*5971e316Smrg  *	map_i^+ \cup qc^+
1149*5971e316Smrg  *
1150*5971e316Smrg  * or
1151*5971e316Smrg  *
1152*5971e316Smrg  *	map_i^+ \cup ((id \cup map_i^) \circ qc^+)
1153*5971e316Smrg  *
1154*5971e316Smrg  * or
1155*5971e316Smrg  *
1156*5971e316Smrg  *	map_i^+ \cup (qc^+ \circ (id \cup map_i^))
1157*5971e316Smrg  *
1158*5971e316Smrg  * depending on whether left or right are NULL.
1159*5971e316Smrg  */
compute_incremental(__isl_take isl_space * space,__isl_keep isl_map * map,int i,__isl_take isl_map * qc,int * left,int * right,isl_bool * exact)1160*5971e316Smrg static __isl_give isl_map *compute_incremental(
1161*5971e316Smrg 	__isl_take isl_space *space, __isl_keep isl_map *map,
1162*5971e316Smrg 	int i, __isl_take isl_map *qc, int *left, int *right, isl_bool *exact)
1163*5971e316Smrg {
1164*5971e316Smrg 	isl_map *map_i;
1165*5971e316Smrg 	isl_map *tc;
1166*5971e316Smrg 	isl_map *rtc = NULL;
1167*5971e316Smrg 
1168*5971e316Smrg 	if (!map)
1169*5971e316Smrg 		goto error;
1170*5971e316Smrg 	isl_assert(map->ctx, left || right, goto error);
1171*5971e316Smrg 
1172*5971e316Smrg 	map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
1173*5971e316Smrg 	tc = construct_projected_component(isl_space_copy(space), map_i,
1174*5971e316Smrg 						exact, 1);
1175*5971e316Smrg 	isl_map_free(map_i);
1176*5971e316Smrg 
1177*5971e316Smrg 	if (*exact)
1178*5971e316Smrg 		qc = isl_map_transitive_closure(qc, exact);
1179*5971e316Smrg 
1180*5971e316Smrg 	if (!*exact) {
1181*5971e316Smrg 		isl_space_free(space);
1182*5971e316Smrg 		isl_map_free(tc);
1183*5971e316Smrg 		isl_map_free(qc);
1184*5971e316Smrg 		return isl_map_universe(isl_map_get_space(map));
1185*5971e316Smrg 	}
1186*5971e316Smrg 
1187*5971e316Smrg 	if (!left || !right)
1188*5971e316Smrg 		rtc = isl_map_union(isl_map_copy(tc),
1189*5971e316Smrg 				    isl_map_identity(isl_map_get_space(tc)));
1190*5971e316Smrg 	if (!right)
1191*5971e316Smrg 		qc = isl_map_apply_range(rtc, qc);
1192*5971e316Smrg 	if (!left)
1193*5971e316Smrg 		qc = isl_map_apply_range(qc, rtc);
1194*5971e316Smrg 	qc = isl_map_union(tc, qc);
1195*5971e316Smrg 
1196*5971e316Smrg 	isl_space_free(space);
1197*5971e316Smrg 
1198*5971e316Smrg 	return qc;
1199*5971e316Smrg error:
1200*5971e316Smrg 	isl_space_free(space);
1201*5971e316Smrg 	isl_map_free(qc);
1202*5971e316Smrg 	return NULL;
1203*5971e316Smrg }
1204*5971e316Smrg 
1205*5971e316Smrg /* Given a map "map", try to find a basic map such that
1206*5971e316Smrg  * map^+ can be computed as
1207*5971e316Smrg  *
1208*5971e316Smrg  * map^+ = map_i^+ \cup
1209*5971e316Smrg  *    \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1210*5971e316Smrg  *
1211*5971e316Smrg  * with C the simple hull of the domain and range of the input map.
1212*5971e316Smrg  * map_i^ \cup Id_C is computed by allowing the path lengths to be zero
1213*5971e316Smrg  * and by intersecting domain and range with C.
1214*5971e316Smrg  * Of course, we need to check that this is actually equal to map_i^ \cup Id_C.
1215*5971e316Smrg  * Also, we only use the incremental computation if all the transitive
1216*5971e316Smrg  * closures are exact and if the number of basic maps in the union,
1217*5971e316Smrg  * after computing the integer divisions, is smaller than the number
1218*5971e316Smrg  * of basic maps in the input map.
1219*5971e316Smrg  */
incremental_on_entire_domain(__isl_keep isl_space * space,__isl_keep isl_map * map,isl_set ** dom,isl_set ** ran,int * left,int * right,__isl_give isl_map ** res)1220*5971e316Smrg static isl_bool incremental_on_entire_domain(__isl_keep isl_space *space,
1221*5971e316Smrg 	__isl_keep isl_map *map,
1222*5971e316Smrg 	isl_set **dom, isl_set **ran, int *left, int *right,
1223*5971e316Smrg 	__isl_give isl_map **res)
1224*5971e316Smrg {
1225*5971e316Smrg 	int i;
1226*5971e316Smrg 	isl_set *C;
1227*5971e316Smrg 	isl_size d;
1228*5971e316Smrg 
1229*5971e316Smrg 	*res = NULL;
1230*5971e316Smrg 
1231*5971e316Smrg 	d = isl_map_dim(map, isl_dim_in);
1232*5971e316Smrg 	if (d < 0)
1233*5971e316Smrg 		return isl_bool_error;
1234*5971e316Smrg 
1235*5971e316Smrg 	C = isl_set_union(isl_map_domain(isl_map_copy(map)),
1236*5971e316Smrg 			  isl_map_range(isl_map_copy(map)));
1237*5971e316Smrg 	C = isl_set_from_basic_set(isl_set_simple_hull(C));
1238*5971e316Smrg 	if (!C)
1239*5971e316Smrg 		return isl_bool_error;
1240*5971e316Smrg 	if (C->n != 1) {
1241*5971e316Smrg 		isl_set_free(C);
1242*5971e316Smrg 		return isl_bool_false;
1243*5971e316Smrg 	}
1244*5971e316Smrg 
1245*5971e316Smrg 	for (i = 0; i < map->n; ++i) {
1246*5971e316Smrg 		isl_map *qc;
1247*5971e316Smrg 		isl_bool exact_i;
1248*5971e316Smrg 		isl_bool spurious;
1249*5971e316Smrg 		int j;
1250*5971e316Smrg 		dom[i] = isl_set_from_basic_set(isl_basic_map_domain(
1251*5971e316Smrg 					isl_basic_map_copy(map->p[i])));
1252*5971e316Smrg 		ran[i] = isl_set_from_basic_set(isl_basic_map_range(
1253*5971e316Smrg 					isl_basic_map_copy(map->p[i])));
1254*5971e316Smrg 		qc = q_closure(isl_space_copy(space), isl_set_copy(C),
1255*5971e316Smrg 				map->p[i], &exact_i);
1256*5971e316Smrg 		if (!qc)
1257*5971e316Smrg 			goto error;
1258*5971e316Smrg 		if (!exact_i) {
1259*5971e316Smrg 			isl_map_free(qc);
1260*5971e316Smrg 			continue;
1261*5971e316Smrg 		}
1262*5971e316Smrg 		spurious = has_spurious_elements(qc, dom[i], ran[i]);
1263*5971e316Smrg 		if (spurious) {
1264*5971e316Smrg 			isl_map_free(qc);
1265*5971e316Smrg 			if (spurious < 0)
1266*5971e316Smrg 				goto error;
1267*5971e316Smrg 			continue;
1268*5971e316Smrg 		}
1269*5971e316Smrg 		qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1270*5971e316Smrg 		qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1271*5971e316Smrg 		qc = isl_map_compute_divs(qc);
1272*5971e316Smrg 		for (j = 0; j < map->n; ++j)
1273*5971e316Smrg 			left[j] = right[j] = 1;
1274*5971e316Smrg 		qc = compose(map, i, qc, left, right);
1275*5971e316Smrg 		if (!qc)
1276*5971e316Smrg 			goto error;
1277*5971e316Smrg 		if (qc->n >= map->n) {
1278*5971e316Smrg 			isl_map_free(qc);
1279*5971e316Smrg 			continue;
1280*5971e316Smrg 		}
1281*5971e316Smrg 		*res = compute_incremental(isl_space_copy(space), map, i, qc,
1282*5971e316Smrg 				left, right, &exact_i);
1283*5971e316Smrg 		if (!*res)
1284*5971e316Smrg 			goto error;
1285*5971e316Smrg 		if (exact_i)
1286*5971e316Smrg 			break;
1287*5971e316Smrg 		isl_map_free(*res);
1288*5971e316Smrg 		*res = NULL;
1289*5971e316Smrg 	}
1290*5971e316Smrg 
1291*5971e316Smrg 	isl_set_free(C);
1292*5971e316Smrg 
1293*5971e316Smrg 	return isl_bool_ok(*res != NULL);
1294*5971e316Smrg error:
1295*5971e316Smrg 	isl_set_free(C);
1296*5971e316Smrg 	return isl_bool_error;
1297*5971e316Smrg }
1298*5971e316Smrg 
1299*5971e316Smrg /* Try and compute the transitive closure of "map" as
1300*5971e316Smrg  *
1301*5971e316Smrg  * map^+ = map_i^+ \cup
1302*5971e316Smrg  *    \bigcup_j ((map_i^+ \cup Id_C)^+ \circ map_j \circ (map_i^+ \cup Id_C))^+
1303*5971e316Smrg  *
1304*5971e316Smrg  * with C either the simple hull of the domain and range of the entire
1305*5971e316Smrg  * map or the simple hull of domain and range of map_i.
1306*5971e316Smrg  */
incremental_closure(__isl_take isl_space * space,__isl_keep isl_map * map,isl_bool * exact,int project)1307*5971e316Smrg static __isl_give isl_map *incremental_closure(__isl_take isl_space *space,
1308*5971e316Smrg 	__isl_keep isl_map *map, isl_bool *exact, int project)
1309*5971e316Smrg {
1310*5971e316Smrg 	int i;
1311*5971e316Smrg 	isl_set **dom = NULL;
1312*5971e316Smrg 	isl_set **ran = NULL;
1313*5971e316Smrg 	int *left = NULL;
1314*5971e316Smrg 	int *right = NULL;
1315*5971e316Smrg 	isl_set *C;
1316*5971e316Smrg 	isl_size d;
1317*5971e316Smrg 	isl_map *res = NULL;
1318*5971e316Smrg 
1319*5971e316Smrg 	if (!project)
1320*5971e316Smrg 		return construct_projected_component(space, map, exact,
1321*5971e316Smrg 							project);
1322*5971e316Smrg 
1323*5971e316Smrg 	if (!map)
1324*5971e316Smrg 		goto error;
1325*5971e316Smrg 	if (map->n <= 1)
1326*5971e316Smrg 		return construct_projected_component(space, map, exact,
1327*5971e316Smrg 							project);
1328*5971e316Smrg 
1329*5971e316Smrg 	d = isl_map_dim(map, isl_dim_in);
1330*5971e316Smrg 	if (d < 0)
1331*5971e316Smrg 		goto error;
1332*5971e316Smrg 
1333*5971e316Smrg 	dom = isl_calloc_array(map->ctx, isl_set *, map->n);
1334*5971e316Smrg 	ran = isl_calloc_array(map->ctx, isl_set *, map->n);
1335*5971e316Smrg 	left = isl_calloc_array(map->ctx, int, map->n);
1336*5971e316Smrg 	right = isl_calloc_array(map->ctx, int, map->n);
1337*5971e316Smrg 	if (!ran || !dom || !left || !right)
1338*5971e316Smrg 		goto error;
1339*5971e316Smrg 
1340*5971e316Smrg 	if (incremental_on_entire_domain(space, map, dom, ran, left, right,
1341*5971e316Smrg 					&res) < 0)
1342*5971e316Smrg 		goto error;
1343*5971e316Smrg 
1344*5971e316Smrg 	for (i = 0; !res && i < map->n; ++i) {
1345*5971e316Smrg 		isl_map *qc;
1346*5971e316Smrg 		int comp;
1347*5971e316Smrg 		isl_bool exact_i, spurious;
1348*5971e316Smrg 		if (!dom[i])
1349*5971e316Smrg 			dom[i] = isl_set_from_basic_set(
1350*5971e316Smrg 					isl_basic_map_domain(
1351*5971e316Smrg 						isl_basic_map_copy(map->p[i])));
1352*5971e316Smrg 		if (!dom[i])
1353*5971e316Smrg 			goto error;
1354*5971e316Smrg 		if (!ran[i])
1355*5971e316Smrg 			ran[i] = isl_set_from_basic_set(
1356*5971e316Smrg 					isl_basic_map_range(
1357*5971e316Smrg 						isl_basic_map_copy(map->p[i])));
1358*5971e316Smrg 		if (!ran[i])
1359*5971e316Smrg 			goto error;
1360*5971e316Smrg 		C = isl_set_union(isl_set_copy(dom[i]),
1361*5971e316Smrg 				      isl_set_copy(ran[i]));
1362*5971e316Smrg 		C = isl_set_from_basic_set(isl_set_simple_hull(C));
1363*5971e316Smrg 		if (!C)
1364*5971e316Smrg 			goto error;
1365*5971e316Smrg 		if (C->n != 1) {
1366*5971e316Smrg 			isl_set_free(C);
1367*5971e316Smrg 			continue;
1368*5971e316Smrg 		}
1369*5971e316Smrg 		comp = composability(C, i, dom, ran, left, right, map);
1370*5971e316Smrg 		if (!comp || comp < 0) {
1371*5971e316Smrg 			isl_set_free(C);
1372*5971e316Smrg 			if (comp < 0)
1373*5971e316Smrg 				goto error;
1374*5971e316Smrg 			continue;
1375*5971e316Smrg 		}
1376*5971e316Smrg 		qc = q_closure(isl_space_copy(space), C, map->p[i], &exact_i);
1377*5971e316Smrg 		if (!qc)
1378*5971e316Smrg 			goto error;
1379*5971e316Smrg 		if (!exact_i) {
1380*5971e316Smrg 			isl_map_free(qc);
1381*5971e316Smrg 			continue;
1382*5971e316Smrg 		}
1383*5971e316Smrg 		spurious = has_spurious_elements(qc, dom[i], ran[i]);
1384*5971e316Smrg 		if (spurious) {
1385*5971e316Smrg 			isl_map_free(qc);
1386*5971e316Smrg 			if (spurious < 0)
1387*5971e316Smrg 				goto error;
1388*5971e316Smrg 			continue;
1389*5971e316Smrg 		}
1390*5971e316Smrg 		qc = isl_map_project_out(qc, isl_dim_in, d, 1);
1391*5971e316Smrg 		qc = isl_map_project_out(qc, isl_dim_out, d, 1);
1392*5971e316Smrg 		qc = isl_map_compute_divs(qc);
1393*5971e316Smrg 		qc = compose(map, i, qc, (comp & LEFT) ? left : NULL,
1394*5971e316Smrg 				(comp & RIGHT) ? right : NULL);
1395*5971e316Smrg 		if (!qc)
1396*5971e316Smrg 			goto error;
1397*5971e316Smrg 		if (qc->n >= map->n) {
1398*5971e316Smrg 			isl_map_free(qc);
1399*5971e316Smrg 			continue;
1400*5971e316Smrg 		}
1401*5971e316Smrg 		res = compute_incremental(isl_space_copy(space), map, i, qc,
1402*5971e316Smrg 				(comp & LEFT) ? left : NULL,
1403*5971e316Smrg 				(comp & RIGHT) ? right : NULL, &exact_i);
1404*5971e316Smrg 		if (!res)
1405*5971e316Smrg 			goto error;
1406*5971e316Smrg 		if (exact_i)
1407*5971e316Smrg 			break;
1408*5971e316Smrg 		isl_map_free(res);
1409*5971e316Smrg 		res = NULL;
1410*5971e316Smrg 	}
1411*5971e316Smrg 
1412*5971e316Smrg 	for (i = 0; i < map->n; ++i) {
1413*5971e316Smrg 		isl_set_free(dom[i]);
1414*5971e316Smrg 		isl_set_free(ran[i]);
1415*5971e316Smrg 	}
1416*5971e316Smrg 	free(dom);
1417*5971e316Smrg 	free(ran);
1418*5971e316Smrg 	free(left);
1419*5971e316Smrg 	free(right);
1420*5971e316Smrg 
1421*5971e316Smrg 	if (res) {
1422*5971e316Smrg 		isl_space_free(space);
1423*5971e316Smrg 		return res;
1424*5971e316Smrg 	}
1425*5971e316Smrg 
1426*5971e316Smrg 	return construct_projected_component(space, map, exact, project);
1427*5971e316Smrg error:
1428*5971e316Smrg 	if (dom)
1429*5971e316Smrg 		for (i = 0; i < map->n; ++i)
1430*5971e316Smrg 			isl_set_free(dom[i]);
1431*5971e316Smrg 	free(dom);
1432*5971e316Smrg 	if (ran)
1433*5971e316Smrg 		for (i = 0; i < map->n; ++i)
1434*5971e316Smrg 			isl_set_free(ran[i]);
1435*5971e316Smrg 	free(ran);
1436*5971e316Smrg 	free(left);
1437*5971e316Smrg 	free(right);
1438*5971e316Smrg 	isl_space_free(space);
1439*5971e316Smrg 	return NULL;
1440*5971e316Smrg }
1441*5971e316Smrg 
1442*5971e316Smrg /* Given an array of sets "set", add "dom" at position "pos"
1443*5971e316Smrg  * and search for elements at earlier positions that overlap with "dom".
1444*5971e316Smrg  * If any can be found, then merge all of them, together with "dom", into
1445*5971e316Smrg  * a single set and assign the union to the first in the array,
1446*5971e316Smrg  * which becomes the new group leader for all groups involved in the merge.
1447*5971e316Smrg  * During the search, we only consider group leaders, i.e., those with
1448*5971e316Smrg  * group[i] = i, as the other sets have already been combined
1449*5971e316Smrg  * with one of the group leaders.
1450*5971e316Smrg  */
merge(isl_set ** set,int * group,__isl_take isl_set * dom,int pos)1451*5971e316Smrg static int merge(isl_set **set, int *group, __isl_take isl_set *dom, int pos)
1452*5971e316Smrg {
1453*5971e316Smrg 	int i;
1454*5971e316Smrg 
1455*5971e316Smrg 	group[pos] = pos;
1456*5971e316Smrg 	set[pos] = isl_set_copy(dom);
1457*5971e316Smrg 
1458*5971e316Smrg 	for (i = pos - 1; i >= 0; --i) {
1459*5971e316Smrg 		isl_bool o;
1460*5971e316Smrg 
1461*5971e316Smrg 		if (group[i] != i)
1462*5971e316Smrg 			continue;
1463*5971e316Smrg 
1464*5971e316Smrg 		o = isl_set_overlaps(set[i], dom);
1465*5971e316Smrg 		if (o < 0)
1466*5971e316Smrg 			goto error;
1467*5971e316Smrg 		if (!o)
1468*5971e316Smrg 			continue;
1469*5971e316Smrg 
1470*5971e316Smrg 		set[i] = isl_set_union(set[i], set[group[pos]]);
1471*5971e316Smrg 		set[group[pos]] = NULL;
1472*5971e316Smrg 		if (!set[i])
1473*5971e316Smrg 			goto error;
1474*5971e316Smrg 		group[group[pos]] = i;
1475*5971e316Smrg 		group[pos] = i;
1476*5971e316Smrg 	}
1477*5971e316Smrg 
1478*5971e316Smrg 	isl_set_free(dom);
1479*5971e316Smrg 	return 0;
1480*5971e316Smrg error:
1481*5971e316Smrg 	isl_set_free(dom);
1482*5971e316Smrg 	return -1;
1483*5971e316Smrg }
1484*5971e316Smrg 
1485*5971e316Smrg /* Construct a map [x] -> [x+1], with parameters prescribed by "space".
1486*5971e316Smrg  */
increment(__isl_take isl_space * space)1487*5971e316Smrg static __isl_give isl_map *increment(__isl_take isl_space *space)
1488*5971e316Smrg {
1489*5971e316Smrg 	int k;
1490*5971e316Smrg 	isl_basic_map *bmap;
1491*5971e316Smrg 	isl_size total;
1492*5971e316Smrg 
1493*5971e316Smrg 	space = isl_space_set_from_params(space);
1494*5971e316Smrg 	space = isl_space_add_dims(space, isl_dim_set, 1);
1495*5971e316Smrg 	space = isl_space_map_from_set(space);
1496*5971e316Smrg 	bmap = isl_basic_map_alloc_space(space, 0, 1, 0);
1497*5971e316Smrg 	total = isl_basic_map_dim(bmap, isl_dim_all);
1498*5971e316Smrg 	k = isl_basic_map_alloc_equality(bmap);
1499*5971e316Smrg 	if (total < 0 || k < 0)
1500*5971e316Smrg 		goto error;
1501*5971e316Smrg 	isl_seq_clr(bmap->eq[k], 1 + total);
1502*5971e316Smrg 	isl_int_set_si(bmap->eq[k][0], 1);
1503*5971e316Smrg 	isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_in)], 1);
1504*5971e316Smrg 	isl_int_set_si(bmap->eq[k][isl_basic_map_offset(bmap, isl_dim_out)], -1);
1505*5971e316Smrg 	return isl_map_from_basic_map(bmap);
1506*5971e316Smrg error:
1507*5971e316Smrg 	isl_basic_map_free(bmap);
1508*5971e316Smrg 	return NULL;
1509*5971e316Smrg }
1510*5971e316Smrg 
1511*5971e316Smrg /* Replace each entry in the n by n grid of maps by the cross product
1512*5971e316Smrg  * with the relation { [i] -> [i + 1] }.
1513*5971e316Smrg  */
add_length(__isl_keep isl_map * map,isl_map *** grid,int n)1514*5971e316Smrg static isl_stat add_length(__isl_keep isl_map *map, isl_map ***grid, int n)
1515*5971e316Smrg {
1516*5971e316Smrg 	int i, j;
1517*5971e316Smrg 	isl_space *space;
1518*5971e316Smrg 	isl_map *step;
1519*5971e316Smrg 
1520*5971e316Smrg 	space = isl_space_params(isl_map_get_space(map));
1521*5971e316Smrg 	step = increment(space);
1522*5971e316Smrg 
1523*5971e316Smrg 	if (!step)
1524*5971e316Smrg 		return isl_stat_error;
1525*5971e316Smrg 
1526*5971e316Smrg 	for (i = 0; i < n; ++i)
1527*5971e316Smrg 		for (j = 0; j < n; ++j)
1528*5971e316Smrg 			grid[i][j] = isl_map_product(grid[i][j],
1529*5971e316Smrg 						     isl_map_copy(step));
1530*5971e316Smrg 
1531*5971e316Smrg 	isl_map_free(step);
1532*5971e316Smrg 
1533*5971e316Smrg 	return isl_stat_ok;
1534*5971e316Smrg }
1535*5971e316Smrg 
1536*5971e316Smrg /* The core of the Floyd-Warshall algorithm.
1537*5971e316Smrg  * Updates the given n x x matrix of relations in place.
1538*5971e316Smrg  *
1539*5971e316Smrg  * The algorithm iterates over all vertices.  In each step, the whole
1540*5971e316Smrg  * matrix is updated to include all paths that go to the current vertex,
1541*5971e316Smrg  * possibly stay there a while (including passing through earlier vertices)
1542*5971e316Smrg  * and then come back.  At the start of each iteration, the diagonal
1543*5971e316Smrg  * element corresponding to the current vertex is replaced by its
1544*5971e316Smrg  * transitive closure to account for all indirect paths that stay
1545*5971e316Smrg  * in the current vertex.
1546*5971e316Smrg  */
floyd_warshall_iterate(isl_map *** grid,int n,isl_bool * exact)1547*5971e316Smrg static void floyd_warshall_iterate(isl_map ***grid, int n, isl_bool *exact)
1548*5971e316Smrg {
1549*5971e316Smrg 	int r, p, q;
1550*5971e316Smrg 
1551*5971e316Smrg 	for (r = 0; r < n; ++r) {
1552*5971e316Smrg 		isl_bool r_exact;
1553*5971e316Smrg 		int check = exact && *exact == isl_bool_true;
1554*5971e316Smrg 		grid[r][r] = isl_map_transitive_closure(grid[r][r],
1555*5971e316Smrg 				check ? &r_exact : NULL);
1556*5971e316Smrg 		if (check && !r_exact)
1557*5971e316Smrg 			*exact = isl_bool_false;
1558*5971e316Smrg 
1559*5971e316Smrg 		for (p = 0; p < n; ++p)
1560*5971e316Smrg 			for (q = 0; q < n; ++q) {
1561*5971e316Smrg 				isl_map *loop;
1562*5971e316Smrg 				if (p == r && q == r)
1563*5971e316Smrg 					continue;
1564*5971e316Smrg 				loop = isl_map_apply_range(
1565*5971e316Smrg 						isl_map_copy(grid[p][r]),
1566*5971e316Smrg 						isl_map_copy(grid[r][q]));
1567*5971e316Smrg 				grid[p][q] = isl_map_union(grid[p][q], loop);
1568*5971e316Smrg 				loop = isl_map_apply_range(
1569*5971e316Smrg 						isl_map_copy(grid[p][r]),
1570*5971e316Smrg 					isl_map_apply_range(
1571*5971e316Smrg 						isl_map_copy(grid[r][r]),
1572*5971e316Smrg 						isl_map_copy(grid[r][q])));
1573*5971e316Smrg 				grid[p][q] = isl_map_union(grid[p][q], loop);
1574*5971e316Smrg 				grid[p][q] = isl_map_coalesce(grid[p][q]);
1575*5971e316Smrg 			}
1576*5971e316Smrg 	}
1577*5971e316Smrg }
1578*5971e316Smrg 
1579*5971e316Smrg /* Given a partition of the domains and ranges of the basic maps in "map",
1580*5971e316Smrg  * apply the Floyd-Warshall algorithm with the elements in the partition
1581*5971e316Smrg  * as vertices.
1582*5971e316Smrg  *
1583*5971e316Smrg  * In particular, there are "n" elements in the partition and "group" is
1584*5971e316Smrg  * an array of length 2 * map->n with entries in [0,n-1].
1585*5971e316Smrg  *
1586*5971e316Smrg  * We first construct a matrix of relations based on the partition information,
1587*5971e316Smrg  * apply Floyd-Warshall on this matrix of relations and then take the
1588*5971e316Smrg  * union of all entries in the matrix as the final result.
1589*5971e316Smrg  *
1590*5971e316Smrg  * If we are actually computing the power instead of the transitive closure,
1591*5971e316Smrg  * i.e., when "project" is not set, then the result should have the
1592*5971e316Smrg  * path lengths encoded as the difference between an extra pair of
1593*5971e316Smrg  * coordinates.  We therefore apply the nested transitive closures
1594*5971e316Smrg  * to relations that include these lengths.  In particular, we replace
1595*5971e316Smrg  * the input relation by the cross product with the unit length relation
1596*5971e316Smrg  * { [i] -> [i + 1] }.
1597*5971e316Smrg  */
floyd_warshall_with_groups(__isl_take isl_space * space,__isl_keep isl_map * map,isl_bool * exact,int project,int * group,int n)1598*5971e316Smrg static __isl_give isl_map *floyd_warshall_with_groups(
1599*5971e316Smrg 	__isl_take isl_space *space, __isl_keep isl_map *map,
1600*5971e316Smrg 	isl_bool *exact, int project, int *group, int n)
1601*5971e316Smrg {
1602*5971e316Smrg 	int i, j, k;
1603*5971e316Smrg 	isl_map ***grid = NULL;
1604*5971e316Smrg 	isl_map *app;
1605*5971e316Smrg 
1606*5971e316Smrg 	if (!map)
1607*5971e316Smrg 		goto error;
1608*5971e316Smrg 
1609*5971e316Smrg 	if (n == 1) {
1610*5971e316Smrg 		free(group);
1611*5971e316Smrg 		return incremental_closure(space, map, exact, project);
1612*5971e316Smrg 	}
1613*5971e316Smrg 
1614*5971e316Smrg 	grid = isl_calloc_array(map->ctx, isl_map **, n);
1615*5971e316Smrg 	if (!grid)
1616*5971e316Smrg 		goto error;
1617*5971e316Smrg 	for (i = 0; i < n; ++i) {
1618*5971e316Smrg 		grid[i] = isl_calloc_array(map->ctx, isl_map *, n);
1619*5971e316Smrg 		if (!grid[i])
1620*5971e316Smrg 			goto error;
1621*5971e316Smrg 		for (j = 0; j < n; ++j)
1622*5971e316Smrg 			grid[i][j] = isl_map_empty(isl_map_get_space(map));
1623*5971e316Smrg 	}
1624*5971e316Smrg 
1625*5971e316Smrg 	for (k = 0; k < map->n; ++k) {
1626*5971e316Smrg 		i = group[2 * k];
1627*5971e316Smrg 		j = group[2 * k + 1];
1628*5971e316Smrg 		grid[i][j] = isl_map_union(grid[i][j],
1629*5971e316Smrg 				isl_map_from_basic_map(
1630*5971e316Smrg 					isl_basic_map_copy(map->p[k])));
1631*5971e316Smrg 	}
1632*5971e316Smrg 
1633*5971e316Smrg 	if (!project && add_length(map, grid, n) < 0)
1634*5971e316Smrg 		goto error;
1635*5971e316Smrg 
1636*5971e316Smrg 	floyd_warshall_iterate(grid, n, exact);
1637*5971e316Smrg 
1638*5971e316Smrg 	app = isl_map_empty(isl_map_get_space(grid[0][0]));
1639*5971e316Smrg 
1640*5971e316Smrg 	for (i = 0; i < n; ++i) {
1641*5971e316Smrg 		for (j = 0; j < n; ++j)
1642*5971e316Smrg 			app = isl_map_union(app, grid[i][j]);
1643*5971e316Smrg 		free(grid[i]);
1644*5971e316Smrg 	}
1645*5971e316Smrg 	free(grid);
1646*5971e316Smrg 
1647*5971e316Smrg 	free(group);
1648*5971e316Smrg 	isl_space_free(space);
1649*5971e316Smrg 
1650*5971e316Smrg 	return app;
1651*5971e316Smrg error:
1652*5971e316Smrg 	if (grid)
1653*5971e316Smrg 		for (i = 0; i < n; ++i) {
1654*5971e316Smrg 			if (!grid[i])
1655*5971e316Smrg 				continue;
1656*5971e316Smrg 			for (j = 0; j < n; ++j)
1657*5971e316Smrg 				isl_map_free(grid[i][j]);
1658*5971e316Smrg 			free(grid[i]);
1659*5971e316Smrg 		}
1660*5971e316Smrg 	free(grid);
1661*5971e316Smrg 	free(group);
1662*5971e316Smrg 	isl_space_free(space);
1663*5971e316Smrg 	return NULL;
1664*5971e316Smrg }
1665*5971e316Smrg 
1666*5971e316Smrg /* Partition the domains and ranges of the n basic relations in list
1667*5971e316Smrg  * into disjoint cells.
1668*5971e316Smrg  *
1669*5971e316Smrg  * To find the partition, we simply consider all of the domains
1670*5971e316Smrg  * and ranges in turn and combine those that overlap.
1671*5971e316Smrg  * "set" contains the partition elements and "group" indicates
1672*5971e316Smrg  * to which partition element a given domain or range belongs.
1673*5971e316Smrg  * The domain of basic map i corresponds to element 2 * i in these arrays,
1674*5971e316Smrg  * while the domain corresponds to element 2 * i + 1.
1675*5971e316Smrg  * During the construction group[k] is either equal to k,
1676*5971e316Smrg  * in which case set[k] contains the union of all the domains and
1677*5971e316Smrg  * ranges in the corresponding group, or is equal to some l < k,
1678*5971e316Smrg  * with l another domain or range in the same group.
1679*5971e316Smrg  */
setup_groups(isl_ctx * ctx,__isl_keep isl_basic_map ** list,int n,isl_set *** set,int * n_group)1680*5971e316Smrg static int *setup_groups(isl_ctx *ctx, __isl_keep isl_basic_map **list, int n,
1681*5971e316Smrg 	isl_set ***set, int *n_group)
1682*5971e316Smrg {
1683*5971e316Smrg 	int i;
1684*5971e316Smrg 	int *group = NULL;
1685*5971e316Smrg 	int g;
1686*5971e316Smrg 
1687*5971e316Smrg 	*set = isl_calloc_array(ctx, isl_set *, 2 * n);
1688*5971e316Smrg 	group = isl_alloc_array(ctx, int, 2 * n);
1689*5971e316Smrg 
1690*5971e316Smrg 	if (!*set || !group)
1691*5971e316Smrg 		goto error;
1692*5971e316Smrg 
1693*5971e316Smrg 	for (i = 0; i < n; ++i) {
1694*5971e316Smrg 		isl_set *dom;
1695*5971e316Smrg 		dom = isl_set_from_basic_set(isl_basic_map_domain(
1696*5971e316Smrg 				isl_basic_map_copy(list[i])));
1697*5971e316Smrg 		if (merge(*set, group, dom, 2 * i) < 0)
1698*5971e316Smrg 			goto error;
1699*5971e316Smrg 		dom = isl_set_from_basic_set(isl_basic_map_range(
1700*5971e316Smrg 				isl_basic_map_copy(list[i])));
1701*5971e316Smrg 		if (merge(*set, group, dom, 2 * i + 1) < 0)
1702*5971e316Smrg 			goto error;
1703*5971e316Smrg 	}
1704*5971e316Smrg 
1705*5971e316Smrg 	g = 0;
1706*5971e316Smrg 	for (i = 0; i < 2 * n; ++i)
1707*5971e316Smrg 		if (group[i] == i) {
1708*5971e316Smrg 			if (g != i) {
1709*5971e316Smrg 				(*set)[g] = (*set)[i];
1710*5971e316Smrg 				(*set)[i] = NULL;
1711*5971e316Smrg 			}
1712*5971e316Smrg 			group[i] = g++;
1713*5971e316Smrg 		} else
1714*5971e316Smrg 			group[i] = group[group[i]];
1715*5971e316Smrg 
1716*5971e316Smrg 	*n_group = g;
1717*5971e316Smrg 
1718*5971e316Smrg 	return group;
1719*5971e316Smrg error:
1720*5971e316Smrg 	if (*set) {
1721*5971e316Smrg 		for (i = 0; i < 2 * n; ++i)
1722*5971e316Smrg 			isl_set_free((*set)[i]);
1723*5971e316Smrg 		free(*set);
1724*5971e316Smrg 		*set = NULL;
1725*5971e316Smrg 	}
1726*5971e316Smrg 	free(group);
1727*5971e316Smrg 	return NULL;
1728*5971e316Smrg }
1729*5971e316Smrg 
1730*5971e316Smrg /* Check if the domains and ranges of the basic maps in "map" can
1731*5971e316Smrg  * be partitioned, and if so, apply Floyd-Warshall on the elements
1732*5971e316Smrg  * of the partition.  Note that we also apply this algorithm
1733*5971e316Smrg  * if we want to compute the power, i.e., when "project" is not set.
1734*5971e316Smrg  * However, the results are unlikely to be exact since the recursive
1735*5971e316Smrg  * calls inside the Floyd-Warshall algorithm typically result in
1736*5971e316Smrg  * non-linear path lengths quite quickly.
1737*5971e316Smrg  */
floyd_warshall(__isl_take isl_space * space,__isl_keep isl_map * map,isl_bool * exact,int project)1738*5971e316Smrg static __isl_give isl_map *floyd_warshall(__isl_take isl_space *space,
1739*5971e316Smrg 	__isl_keep isl_map *map, isl_bool *exact, int project)
1740*5971e316Smrg {
1741*5971e316Smrg 	int i;
1742*5971e316Smrg 	isl_set **set = NULL;
1743*5971e316Smrg 	int *group = NULL;
1744*5971e316Smrg 	int n;
1745*5971e316Smrg 
1746*5971e316Smrg 	if (!map)
1747*5971e316Smrg 		goto error;
1748*5971e316Smrg 	if (map->n <= 1)
1749*5971e316Smrg 		return incremental_closure(space, map, exact, project);
1750*5971e316Smrg 
1751*5971e316Smrg 	group = setup_groups(map->ctx, map->p, map->n, &set, &n);
1752*5971e316Smrg 	if (!group)
1753*5971e316Smrg 		goto error;
1754*5971e316Smrg 
1755*5971e316Smrg 	for (i = 0; i < 2 * map->n; ++i)
1756*5971e316Smrg 		isl_set_free(set[i]);
1757*5971e316Smrg 
1758*5971e316Smrg 	free(set);
1759*5971e316Smrg 
1760*5971e316Smrg 	return floyd_warshall_with_groups(space, map, exact, project, group, n);
1761*5971e316Smrg error:
1762*5971e316Smrg 	isl_space_free(space);
1763*5971e316Smrg 	return NULL;
1764*5971e316Smrg }
1765*5971e316Smrg 
1766*5971e316Smrg /* Structure for representing the nodes of the graph of which
1767*5971e316Smrg  * strongly connected components are being computed.
1768*5971e316Smrg  *
1769*5971e316Smrg  * list contains the actual nodes
1770*5971e316Smrg  * check_closed is set if we may have used the fact that
1771*5971e316Smrg  * a pair of basic maps can be interchanged
1772*5971e316Smrg  */
1773*5971e316Smrg struct isl_tc_follows_data {
1774*5971e316Smrg 	isl_basic_map **list;
1775*5971e316Smrg 	int check_closed;
1776*5971e316Smrg };
1777*5971e316Smrg 
1778*5971e316Smrg /* Check whether in the computation of the transitive closure
1779*5971e316Smrg  * "list[i]" (R_1) should follow (or be part of the same component as)
1780*5971e316Smrg  * "list[j]" (R_2).
1781*5971e316Smrg  *
1782*5971e316Smrg  * That is check whether
1783*5971e316Smrg  *
1784*5971e316Smrg  *	R_1 \circ R_2
1785*5971e316Smrg  *
1786*5971e316Smrg  * is a subset of
1787*5971e316Smrg  *
1788*5971e316Smrg  *	R_2 \circ R_1
1789*5971e316Smrg  *
1790*5971e316Smrg  * If so, then there is no reason for R_1 to immediately follow R_2
1791*5971e316Smrg  * in any path.
1792*5971e316Smrg  *
1793*5971e316Smrg  * *check_closed is set if the subset relation holds while
1794*5971e316Smrg  * R_1 \circ R_2 is not empty.
1795*5971e316Smrg  */
basic_map_follows(int i,int j,void * user)1796*5971e316Smrg static isl_bool basic_map_follows(int i, int j, void *user)
1797*5971e316Smrg {
1798*5971e316Smrg 	struct isl_tc_follows_data *data = user;
1799*5971e316Smrg 	struct isl_map *map12 = NULL;
1800*5971e316Smrg 	struct isl_map *map21 = NULL;
1801*5971e316Smrg 	isl_bool applies, subset;
1802*5971e316Smrg 
1803*5971e316Smrg 	applies = isl_basic_map_applies_range(data->list[j], data->list[i]);
1804*5971e316Smrg 	if (applies < 0)
1805*5971e316Smrg 		return isl_bool_error;
1806*5971e316Smrg 	if (!applies)
1807*5971e316Smrg 		return isl_bool_false;
1808*5971e316Smrg 
1809*5971e316Smrg 	map21 = isl_map_from_basic_map(
1810*5971e316Smrg 			isl_basic_map_apply_range(
1811*5971e316Smrg 				isl_basic_map_copy(data->list[j]),
1812*5971e316Smrg 				isl_basic_map_copy(data->list[i])));
1813*5971e316Smrg 	subset = isl_map_is_empty(map21);
1814*5971e316Smrg 	if (subset < 0)
1815*5971e316Smrg 		goto error;
1816*5971e316Smrg 	if (subset) {
1817*5971e316Smrg 		isl_map_free(map21);
1818*5971e316Smrg 		return isl_bool_false;
1819*5971e316Smrg 	}
1820*5971e316Smrg 
1821*5971e316Smrg 	if (!isl_basic_map_is_transformation(data->list[i]) ||
1822*5971e316Smrg 	    !isl_basic_map_is_transformation(data->list[j])) {
1823*5971e316Smrg 		isl_map_free(map21);
1824*5971e316Smrg 		return isl_bool_true;
1825*5971e316Smrg 	}
1826*5971e316Smrg 
1827*5971e316Smrg 	map12 = isl_map_from_basic_map(
1828*5971e316Smrg 			isl_basic_map_apply_range(
1829*5971e316Smrg 				isl_basic_map_copy(data->list[i]),
1830*5971e316Smrg 				isl_basic_map_copy(data->list[j])));
1831*5971e316Smrg 
1832*5971e316Smrg 	subset = isl_map_is_subset(map21, map12);
1833*5971e316Smrg 
1834*5971e316Smrg 	isl_map_free(map12);
1835*5971e316Smrg 	isl_map_free(map21);
1836*5971e316Smrg 
1837*5971e316Smrg 	if (subset)
1838*5971e316Smrg 		data->check_closed = 1;
1839*5971e316Smrg 
1840*5971e316Smrg 	return isl_bool_not(subset);
1841*5971e316Smrg error:
1842*5971e316Smrg 	isl_map_free(map21);
1843*5971e316Smrg 	return isl_bool_error;
1844*5971e316Smrg }
1845*5971e316Smrg 
1846*5971e316Smrg /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D
1847*5971e316Smrg  * and a dimension specification (Z^{n+1} -> Z^{n+1}),
1848*5971e316Smrg  * construct a map that is an overapproximation of the map
1849*5971e316Smrg  * that takes an element from the dom R \times Z to an
1850*5971e316Smrg  * element from ran R \times Z, such that the first n coordinates of the
1851*5971e316Smrg  * difference between them is a sum of differences between images
1852*5971e316Smrg  * and pre-images in one of the R_i and such that the last coordinate
1853*5971e316Smrg  * is equal to the number of steps taken.
1854*5971e316Smrg  * If "project" is set, then these final coordinates are not included,
1855*5971e316Smrg  * i.e., a relation of type Z^n -> Z^n is returned.
1856*5971e316Smrg  * That is, let
1857*5971e316Smrg  *
1858*5971e316Smrg  *	\Delta_i = { y - x | (x, y) in R_i }
1859*5971e316Smrg  *
1860*5971e316Smrg  * then the constructed map is an overapproximation of
1861*5971e316Smrg  *
1862*5971e316Smrg  *	{ (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1863*5971e316Smrg  *				d = (\sum_i k_i \delta_i, \sum_i k_i) and
1864*5971e316Smrg  *				x in dom R and x + d in ran R }
1865*5971e316Smrg  *
1866*5971e316Smrg  * or
1867*5971e316Smrg  *
1868*5971e316Smrg  *	{ (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1869*5971e316Smrg  *				d = (\sum_i k_i \delta_i) and
1870*5971e316Smrg  *				x in dom R and x + d in ran R }
1871*5971e316Smrg  *
1872*5971e316Smrg  * if "project" is set.
1873*5971e316Smrg  *
1874*5971e316Smrg  * We first split the map into strongly connected components, perform
1875*5971e316Smrg  * the above on each component and then join the results in the correct
1876*5971e316Smrg  * order, at each join also taking in the union of both arguments
1877*5971e316Smrg  * to allow for paths that do not go through one of the two arguments.
1878*5971e316Smrg  */
construct_power_components(__isl_take isl_space * space,__isl_keep isl_map * map,isl_bool * exact,int project)1879*5971e316Smrg static __isl_give isl_map *construct_power_components(
1880*5971e316Smrg 	__isl_take isl_space *space, __isl_keep isl_map *map, isl_bool *exact,
1881*5971e316Smrg 	int project)
1882*5971e316Smrg {
1883*5971e316Smrg 	int i, n, c;
1884*5971e316Smrg 	struct isl_map *path = NULL;
1885*5971e316Smrg 	struct isl_tc_follows_data data;
1886*5971e316Smrg 	struct isl_tarjan_graph *g = NULL;
1887*5971e316Smrg 	isl_bool *orig_exact;
1888*5971e316Smrg 	isl_bool local_exact;
1889*5971e316Smrg 
1890*5971e316Smrg 	if (!map)
1891*5971e316Smrg 		goto error;
1892*5971e316Smrg 	if (map->n <= 1)
1893*5971e316Smrg 		return floyd_warshall(space, map, exact, project);
1894*5971e316Smrg 
1895*5971e316Smrg 	data.list = map->p;
1896*5971e316Smrg 	data.check_closed = 0;
1897*5971e316Smrg 	g = isl_tarjan_graph_init(map->ctx, map->n, &basic_map_follows, &data);
1898*5971e316Smrg 	if (!g)
1899*5971e316Smrg 		goto error;
1900*5971e316Smrg 
1901*5971e316Smrg 	orig_exact = exact;
1902*5971e316Smrg 	if (data.check_closed && !exact)
1903*5971e316Smrg 		exact = &local_exact;
1904*5971e316Smrg 
1905*5971e316Smrg 	c = 0;
1906*5971e316Smrg 	i = 0;
1907*5971e316Smrg 	n = map->n;
1908*5971e316Smrg 	if (project)
1909*5971e316Smrg 		path = isl_map_empty(isl_map_get_space(map));
1910*5971e316Smrg 	else
1911*5971e316Smrg 		path = isl_map_empty(isl_space_copy(space));
1912*5971e316Smrg 	path = anonymize(path);
1913*5971e316Smrg 	while (n) {
1914*5971e316Smrg 		struct isl_map *comp;
1915*5971e316Smrg 		isl_map *path_comp, *path_comb;
1916*5971e316Smrg 		comp = isl_map_alloc_space(isl_map_get_space(map), n, 0);
1917*5971e316Smrg 		while (g->order[i] != -1) {
1918*5971e316Smrg 			comp = isl_map_add_basic_map(comp,
1919*5971e316Smrg 				    isl_basic_map_copy(map->p[g->order[i]]));
1920*5971e316Smrg 			--n;
1921*5971e316Smrg 			++i;
1922*5971e316Smrg 		}
1923*5971e316Smrg 		path_comp = floyd_warshall(isl_space_copy(space),
1924*5971e316Smrg 						comp, exact, project);
1925*5971e316Smrg 		path_comp = anonymize(path_comp);
1926*5971e316Smrg 		path_comb = isl_map_apply_range(isl_map_copy(path),
1927*5971e316Smrg 						isl_map_copy(path_comp));
1928*5971e316Smrg 		path = isl_map_union(path, path_comp);
1929*5971e316Smrg 		path = isl_map_union(path, path_comb);
1930*5971e316Smrg 		isl_map_free(comp);
1931*5971e316Smrg 		++i;
1932*5971e316Smrg 		++c;
1933*5971e316Smrg 	}
1934*5971e316Smrg 
1935*5971e316Smrg 	if (c > 1 && data.check_closed && !*exact) {
1936*5971e316Smrg 		isl_bool closed;
1937*5971e316Smrg 
1938*5971e316Smrg 		closed = isl_map_is_transitively_closed(path);
1939*5971e316Smrg 		if (closed < 0)
1940*5971e316Smrg 			goto error;
1941*5971e316Smrg 		if (!closed) {
1942*5971e316Smrg 			isl_tarjan_graph_free(g);
1943*5971e316Smrg 			isl_map_free(path);
1944*5971e316Smrg 			return floyd_warshall(space, map, orig_exact, project);
1945*5971e316Smrg 		}
1946*5971e316Smrg 	}
1947*5971e316Smrg 
1948*5971e316Smrg 	isl_tarjan_graph_free(g);
1949*5971e316Smrg 	isl_space_free(space);
1950*5971e316Smrg 
1951*5971e316Smrg 	return path;
1952*5971e316Smrg error:
1953*5971e316Smrg 	isl_tarjan_graph_free(g);
1954*5971e316Smrg 	isl_space_free(space);
1955*5971e316Smrg 	isl_map_free(path);
1956*5971e316Smrg 	return NULL;
1957*5971e316Smrg }
1958*5971e316Smrg 
1959*5971e316Smrg /* Given a union of basic maps R = \cup_i R_i \subseteq D \times D,
1960*5971e316Smrg  * construct a map that is an overapproximation of the map
1961*5971e316Smrg  * that takes an element from the space D to another
1962*5971e316Smrg  * element from the same space, such that the difference between
1963*5971e316Smrg  * them is a strictly positive sum of differences between images
1964*5971e316Smrg  * and pre-images in one of the R_i.
1965*5971e316Smrg  * The number of differences in the sum is equated to parameter "param".
1966*5971e316Smrg  * That is, let
1967*5971e316Smrg  *
1968*5971e316Smrg  *	\Delta_i = { y - x | (x, y) in R_i }
1969*5971e316Smrg  *
1970*5971e316Smrg  * then the constructed map is an overapproximation of
1971*5971e316Smrg  *
1972*5971e316Smrg  *	{ (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1973*5971e316Smrg  *				d = \sum_i k_i \delta_i and k = \sum_i k_i > 0 }
1974*5971e316Smrg  * or
1975*5971e316Smrg  *
1976*5971e316Smrg  *	{ (x) -> (x + d) | \exists k_i >= 0, \delta_i \in \Delta_i :
1977*5971e316Smrg  *				d = \sum_i k_i \delta_i and \sum_i k_i > 0 }
1978*5971e316Smrg  *
1979*5971e316Smrg  * if "project" is set.
1980*5971e316Smrg  *
1981*5971e316Smrg  * If "project" is not set, then
1982*5971e316Smrg  * we construct an extended mapping with an extra coordinate
1983*5971e316Smrg  * that indicates the number of steps taken.  In particular,
1984*5971e316Smrg  * the difference in the last coordinate is equal to the number
1985*5971e316Smrg  * of steps taken to move from a domain element to the corresponding
1986*5971e316Smrg  * image element(s).
1987*5971e316Smrg  */
construct_power(__isl_keep isl_map * map,isl_bool * exact,int project)1988*5971e316Smrg static __isl_give isl_map *construct_power(__isl_keep isl_map *map,
1989*5971e316Smrg 	isl_bool *exact, int project)
1990*5971e316Smrg {
1991*5971e316Smrg 	struct isl_map *app = NULL;
1992*5971e316Smrg 	isl_space *space = NULL;
1993*5971e316Smrg 
1994*5971e316Smrg 	if (!map)
1995*5971e316Smrg 		return NULL;
1996*5971e316Smrg 
1997*5971e316Smrg 	space = isl_map_get_space(map);
1998*5971e316Smrg 
1999*5971e316Smrg 	space = isl_space_add_dims(space, isl_dim_in, 1);
2000*5971e316Smrg 	space = isl_space_add_dims(space, isl_dim_out, 1);
2001*5971e316Smrg 
2002*5971e316Smrg 	app = construct_power_components(isl_space_copy(space), map,
2003*5971e316Smrg 					exact, project);
2004*5971e316Smrg 
2005*5971e316Smrg 	isl_space_free(space);
2006*5971e316Smrg 
2007*5971e316Smrg 	return app;
2008*5971e316Smrg }
2009*5971e316Smrg 
2010*5971e316Smrg /* Compute the positive powers of "map", or an overapproximation.
2011*5971e316Smrg  * If the result is exact, then *exact is set to 1.
2012*5971e316Smrg  *
2013*5971e316Smrg  * If project is set, then we are actually interested in the transitive
2014*5971e316Smrg  * closure, so we can use a more relaxed exactness check.
2015*5971e316Smrg  * The lengths of the paths are also projected out instead of being
2016*5971e316Smrg  * encoded as the difference between an extra pair of final coordinates.
2017*5971e316Smrg  */
map_power(__isl_take isl_map * map,isl_bool * exact,int project)2018*5971e316Smrg static __isl_give isl_map *map_power(__isl_take isl_map *map,
2019*5971e316Smrg 	isl_bool *exact, int project)
2020*5971e316Smrg {
2021*5971e316Smrg 	struct isl_map *app = NULL;
2022*5971e316Smrg 
2023*5971e316Smrg 	if (exact)
2024*5971e316Smrg 		*exact = isl_bool_true;
2025*5971e316Smrg 
2026*5971e316Smrg 	if (isl_map_check_transformation(map) < 0)
2027*5971e316Smrg 		return isl_map_free(map);
2028*5971e316Smrg 
2029*5971e316Smrg 	app = construct_power(map, exact, project);
2030*5971e316Smrg 
2031*5971e316Smrg 	isl_map_free(map);
2032*5971e316Smrg 	return app;
2033*5971e316Smrg }
2034*5971e316Smrg 
2035*5971e316Smrg /* Compute the positive powers of "map", or an overapproximation.
2036*5971e316Smrg  * The result maps the exponent to a nested copy of the corresponding power.
2037*5971e316Smrg  * If the result is exact, then *exact is set to 1.
2038*5971e316Smrg  * map_power constructs an extended relation with the path lengths
2039*5971e316Smrg  * encoded as the difference between the final coordinates.
2040*5971e316Smrg  * In the final step, this difference is equated to an extra parameter
2041*5971e316Smrg  * and made positive.  The extra coordinates are subsequently projected out
2042*5971e316Smrg  * and the parameter is turned into the domain of the result.
2043*5971e316Smrg  */
isl_map_power(__isl_take isl_map * map,isl_bool * exact)2044*5971e316Smrg __isl_give isl_map *isl_map_power(__isl_take isl_map *map, isl_bool *exact)
2045*5971e316Smrg {
2046*5971e316Smrg 	isl_space *target_space;
2047*5971e316Smrg 	isl_space *space;
2048*5971e316Smrg 	isl_map *diff;
2049*5971e316Smrg 	isl_size d;
2050*5971e316Smrg 	isl_size param;
2051*5971e316Smrg 
2052*5971e316Smrg 	d = isl_map_dim(map, isl_dim_in);
2053*5971e316Smrg 	param = isl_map_dim(map, isl_dim_param);
2054*5971e316Smrg 	if (d < 0 || param < 0)
2055*5971e316Smrg 		return isl_map_free(map);
2056*5971e316Smrg 
2057*5971e316Smrg 	map = isl_map_compute_divs(map);
2058*5971e316Smrg 	map = isl_map_coalesce(map);
2059*5971e316Smrg 
2060*5971e316Smrg 	if (isl_map_plain_is_empty(map)) {
2061*5971e316Smrg 		map = isl_map_from_range(isl_map_wrap(map));
2062*5971e316Smrg 		map = isl_map_add_dims(map, isl_dim_in, 1);
2063*5971e316Smrg 		map = isl_map_set_dim_name(map, isl_dim_in, 0, "k");
2064*5971e316Smrg 		return map;
2065*5971e316Smrg 	}
2066*5971e316Smrg 
2067*5971e316Smrg 	target_space = isl_map_get_space(map);
2068*5971e316Smrg 	target_space = isl_space_from_range(isl_space_wrap(target_space));
2069*5971e316Smrg 	target_space = isl_space_add_dims(target_space, isl_dim_in, 1);
2070*5971e316Smrg 	target_space = isl_space_set_dim_name(target_space, isl_dim_in, 0, "k");
2071*5971e316Smrg 
2072*5971e316Smrg 	map = map_power(map, exact, 0);
2073*5971e316Smrg 
2074*5971e316Smrg 	map = isl_map_add_dims(map, isl_dim_param, 1);
2075*5971e316Smrg 	space = isl_map_get_space(map);
2076*5971e316Smrg 	diff = equate_parameter_to_length(space, param);
2077*5971e316Smrg 	map = isl_map_intersect(map, diff);
2078*5971e316Smrg 	map = isl_map_project_out(map, isl_dim_in, d, 1);
2079*5971e316Smrg 	map = isl_map_project_out(map, isl_dim_out, d, 1);
2080*5971e316Smrg 	map = isl_map_from_range(isl_map_wrap(map));
2081*5971e316Smrg 	map = isl_map_move_dims(map, isl_dim_in, 0, isl_dim_param, param, 1);
2082*5971e316Smrg 
2083*5971e316Smrg 	map = isl_map_reset_space(map, target_space);
2084*5971e316Smrg 
2085*5971e316Smrg 	return map;
2086*5971e316Smrg }
2087*5971e316Smrg 
2088*5971e316Smrg /* Compute a relation that maps each element in the range of the input
2089*5971e316Smrg  * relation to the lengths of all paths composed of edges in the input
2090*5971e316Smrg  * relation that end up in the given range element.
2091*5971e316Smrg  * The result may be an overapproximation, in which case *exact is set to 0.
2092*5971e316Smrg  * The resulting relation is very similar to the power relation.
2093*5971e316Smrg  * The difference are that the domain has been projected out, the
2094*5971e316Smrg  * range has become the domain and the exponent is the range instead
2095*5971e316Smrg  * of a parameter.
2096*5971e316Smrg  */
isl_map_reaching_path_lengths(__isl_take isl_map * map,isl_bool * exact)2097*5971e316Smrg __isl_give isl_map *isl_map_reaching_path_lengths(__isl_take isl_map *map,
2098*5971e316Smrg 	isl_bool *exact)
2099*5971e316Smrg {
2100*5971e316Smrg 	isl_space *space;
2101*5971e316Smrg 	isl_map *diff;
2102*5971e316Smrg 	isl_size d;
2103*5971e316Smrg 	isl_size param;
2104*5971e316Smrg 
2105*5971e316Smrg 	d = isl_map_dim(map, isl_dim_in);
2106*5971e316Smrg 	param = isl_map_dim(map, isl_dim_param);
2107*5971e316Smrg 	if (d < 0 || param < 0)
2108*5971e316Smrg 		return isl_map_free(map);
2109*5971e316Smrg 
2110*5971e316Smrg 	map = isl_map_compute_divs(map);
2111*5971e316Smrg 	map = isl_map_coalesce(map);
2112*5971e316Smrg 
2113*5971e316Smrg 	if (isl_map_plain_is_empty(map)) {
2114*5971e316Smrg 		if (exact)
2115*5971e316Smrg 			*exact = isl_bool_true;
2116*5971e316Smrg 		map = isl_map_project_out(map, isl_dim_out, 0, d);
2117*5971e316Smrg 		map = isl_map_add_dims(map, isl_dim_out, 1);
2118*5971e316Smrg 		return map;
2119*5971e316Smrg 	}
2120*5971e316Smrg 
2121*5971e316Smrg 	map = map_power(map, exact, 0);
2122*5971e316Smrg 
2123*5971e316Smrg 	map = isl_map_add_dims(map, isl_dim_param, 1);
2124*5971e316Smrg 	space = isl_map_get_space(map);
2125*5971e316Smrg 	diff = equate_parameter_to_length(space, param);
2126*5971e316Smrg 	map = isl_map_intersect(map, diff);
2127*5971e316Smrg 	map = isl_map_project_out(map, isl_dim_in, 0, d + 1);
2128*5971e316Smrg 	map = isl_map_project_out(map, isl_dim_out, d, 1);
2129*5971e316Smrg 	map = isl_map_reverse(map);
2130*5971e316Smrg 	map = isl_map_move_dims(map, isl_dim_out, 0, isl_dim_param, param, 1);
2131*5971e316Smrg 
2132*5971e316Smrg 	return map;
2133*5971e316Smrg }
2134*5971e316Smrg 
2135*5971e316Smrg /* Given a map, compute the smallest superset of this map that is of the form
2136*5971e316Smrg  *
2137*5971e316Smrg  *	{ i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2138*5971e316Smrg  *
2139*5971e316Smrg  * (where p ranges over the (non-parametric) dimensions),
2140*5971e316Smrg  * compute the transitive closure of this map, i.e.,
2141*5971e316Smrg  *
2142*5971e316Smrg  *	{ i -> j : exists k > 0:
2143*5971e316Smrg  *		k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2144*5971e316Smrg  *
2145*5971e316Smrg  * and intersect domain and range of this transitive closure with
2146*5971e316Smrg  * the given domain and range.
2147*5971e316Smrg  *
2148*5971e316Smrg  * If with_id is set, then try to include as much of the identity mapping
2149*5971e316Smrg  * as possible, by computing
2150*5971e316Smrg  *
2151*5971e316Smrg  *	{ i -> j : exists k >= 0:
2152*5971e316Smrg  *		k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2153*5971e316Smrg  *
2154*5971e316Smrg  * instead (i.e., allow k = 0).
2155*5971e316Smrg  *
2156*5971e316Smrg  * In practice, we compute the difference set
2157*5971e316Smrg  *
2158*5971e316Smrg  *	delta  = { j - i | i -> j in map },
2159*5971e316Smrg  *
2160*5971e316Smrg  * look for stride constraint on the individual dimensions and compute
2161*5971e316Smrg  * (constant) lower and upper bounds for each individual dimension,
2162*5971e316Smrg  * adding a constraint for each bound not equal to infinity.
2163*5971e316Smrg  */
box_closure_on_domain(__isl_take isl_map * map,__isl_take isl_set * dom,__isl_take isl_set * ran,int with_id)2164*5971e316Smrg static __isl_give isl_map *box_closure_on_domain(__isl_take isl_map *map,
2165*5971e316Smrg 	__isl_take isl_set *dom, __isl_take isl_set *ran, int with_id)
2166*5971e316Smrg {
2167*5971e316Smrg 	int i;
2168*5971e316Smrg 	int k;
2169*5971e316Smrg 	unsigned d;
2170*5971e316Smrg 	unsigned nparam;
2171*5971e316Smrg 	unsigned total;
2172*5971e316Smrg 	isl_space *space;
2173*5971e316Smrg 	isl_set *delta;
2174*5971e316Smrg 	isl_map *app = NULL;
2175*5971e316Smrg 	isl_basic_set *aff = NULL;
2176*5971e316Smrg 	isl_basic_map *bmap = NULL;
2177*5971e316Smrg 	isl_vec *obj = NULL;
2178*5971e316Smrg 	isl_int opt;
2179*5971e316Smrg 
2180*5971e316Smrg 	isl_int_init(opt);
2181*5971e316Smrg 
2182*5971e316Smrg 	delta = isl_map_deltas(isl_map_copy(map));
2183*5971e316Smrg 
2184*5971e316Smrg 	aff = isl_set_affine_hull(isl_set_copy(delta));
2185*5971e316Smrg 	if (!aff)
2186*5971e316Smrg 		goto error;
2187*5971e316Smrg 	space = isl_map_get_space(map);
2188*5971e316Smrg 	d = isl_space_dim(space, isl_dim_in);
2189*5971e316Smrg 	nparam = isl_space_dim(space, isl_dim_param);
2190*5971e316Smrg 	total = isl_space_dim(space, isl_dim_all);
2191*5971e316Smrg 	bmap = isl_basic_map_alloc_space(space,
2192*5971e316Smrg 					aff->n_div + 1, aff->n_div, 2 * d + 1);
2193*5971e316Smrg 	for (i = 0; i < aff->n_div + 1; ++i) {
2194*5971e316Smrg 		k = isl_basic_map_alloc_div(bmap);
2195*5971e316Smrg 		if (k < 0)
2196*5971e316Smrg 			goto error;
2197*5971e316Smrg 		isl_int_set_si(bmap->div[k][0], 0);
2198*5971e316Smrg 	}
2199*5971e316Smrg 	for (i = 0; i < aff->n_eq; ++i) {
2200*5971e316Smrg 		if (!isl_basic_set_eq_is_stride(aff, i))
2201*5971e316Smrg 			continue;
2202*5971e316Smrg 		k = isl_basic_map_alloc_equality(bmap);
2203*5971e316Smrg 		if (k < 0)
2204*5971e316Smrg 			goto error;
2205*5971e316Smrg 		isl_seq_clr(bmap->eq[k], 1 + nparam);
2206*5971e316Smrg 		isl_seq_cpy(bmap->eq[k] + 1 + nparam + d,
2207*5971e316Smrg 				aff->eq[i] + 1 + nparam, d);
2208*5971e316Smrg 		isl_seq_neg(bmap->eq[k] + 1 + nparam,
2209*5971e316Smrg 				aff->eq[i] + 1 + nparam, d);
2210*5971e316Smrg 		isl_seq_cpy(bmap->eq[k] + 1 + nparam + 2 * d,
2211*5971e316Smrg 				aff->eq[i] + 1 + nparam + d, aff->n_div);
2212*5971e316Smrg 		isl_int_set_si(bmap->eq[k][1 + total + aff->n_div], 0);
2213*5971e316Smrg 	}
2214*5971e316Smrg 	obj = isl_vec_alloc(map->ctx, 1 + nparam + d);
2215*5971e316Smrg 	if (!obj)
2216*5971e316Smrg 		goto error;
2217*5971e316Smrg 	isl_seq_clr(obj->el, 1 + nparam + d);
2218*5971e316Smrg 	for (i = 0; i < d; ++ i) {
2219*5971e316Smrg 		enum isl_lp_result res;
2220*5971e316Smrg 
2221*5971e316Smrg 		isl_int_set_si(obj->el[1 + nparam + i], 1);
2222*5971e316Smrg 
2223*5971e316Smrg 		res = isl_set_solve_lp(delta, 0, obj->el, map->ctx->one, &opt,
2224*5971e316Smrg 					NULL, NULL);
2225*5971e316Smrg 		if (res == isl_lp_error)
2226*5971e316Smrg 			goto error;
2227*5971e316Smrg 		if (res == isl_lp_ok) {
2228*5971e316Smrg 			k = isl_basic_map_alloc_inequality(bmap);
2229*5971e316Smrg 			if (k < 0)
2230*5971e316Smrg 				goto error;
2231*5971e316Smrg 			isl_seq_clr(bmap->ineq[k],
2232*5971e316Smrg 					1 + nparam + 2 * d + bmap->n_div);
2233*5971e316Smrg 			isl_int_set_si(bmap->ineq[k][1 + nparam + i], -1);
2234*5971e316Smrg 			isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], 1);
2235*5971e316Smrg 			isl_int_neg(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2236*5971e316Smrg 		}
2237*5971e316Smrg 
2238*5971e316Smrg 		res = isl_set_solve_lp(delta, 1, obj->el, map->ctx->one, &opt,
2239*5971e316Smrg 					NULL, NULL);
2240*5971e316Smrg 		if (res == isl_lp_error)
2241*5971e316Smrg 			goto error;
2242*5971e316Smrg 		if (res == isl_lp_ok) {
2243*5971e316Smrg 			k = isl_basic_map_alloc_inequality(bmap);
2244*5971e316Smrg 			if (k < 0)
2245*5971e316Smrg 				goto error;
2246*5971e316Smrg 			isl_seq_clr(bmap->ineq[k],
2247*5971e316Smrg 					1 + nparam + 2 * d + bmap->n_div);
2248*5971e316Smrg 			isl_int_set_si(bmap->ineq[k][1 + nparam + i], 1);
2249*5971e316Smrg 			isl_int_set_si(bmap->ineq[k][1 + nparam + d + i], -1);
2250*5971e316Smrg 			isl_int_set(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], opt);
2251*5971e316Smrg 		}
2252*5971e316Smrg 
2253*5971e316Smrg 		isl_int_set_si(obj->el[1 + nparam + i], 0);
2254*5971e316Smrg 	}
2255*5971e316Smrg 	k = isl_basic_map_alloc_inequality(bmap);
2256*5971e316Smrg 	if (k < 0)
2257*5971e316Smrg 		goto error;
2258*5971e316Smrg 	isl_seq_clr(bmap->ineq[k],
2259*5971e316Smrg 			1 + nparam + 2 * d + bmap->n_div);
2260*5971e316Smrg 	if (!with_id)
2261*5971e316Smrg 		isl_int_set_si(bmap->ineq[k][0], -1);
2262*5971e316Smrg 	isl_int_set_si(bmap->ineq[k][1 + nparam + 2 * d + aff->n_div], 1);
2263*5971e316Smrg 
2264*5971e316Smrg 	app = isl_map_from_domain_and_range(dom, ran);
2265*5971e316Smrg 
2266*5971e316Smrg 	isl_vec_free(obj);
2267*5971e316Smrg 	isl_basic_set_free(aff);
2268*5971e316Smrg 	isl_map_free(map);
2269*5971e316Smrg 	bmap = isl_basic_map_finalize(bmap);
2270*5971e316Smrg 	isl_set_free(delta);
2271*5971e316Smrg 	isl_int_clear(opt);
2272*5971e316Smrg 
2273*5971e316Smrg 	map = isl_map_from_basic_map(bmap);
2274*5971e316Smrg 	map = isl_map_intersect(map, app);
2275*5971e316Smrg 
2276*5971e316Smrg 	return map;
2277*5971e316Smrg error:
2278*5971e316Smrg 	isl_vec_free(obj);
2279*5971e316Smrg 	isl_basic_map_free(bmap);
2280*5971e316Smrg 	isl_basic_set_free(aff);
2281*5971e316Smrg 	isl_set_free(dom);
2282*5971e316Smrg 	isl_set_free(ran);
2283*5971e316Smrg 	isl_map_free(map);
2284*5971e316Smrg 	isl_set_free(delta);
2285*5971e316Smrg 	isl_int_clear(opt);
2286*5971e316Smrg 	return NULL;
2287*5971e316Smrg }
2288*5971e316Smrg 
2289*5971e316Smrg /* Given a map, compute the smallest superset of this map that is of the form
2290*5971e316Smrg  *
2291*5971e316Smrg  *	{ i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2292*5971e316Smrg  *
2293*5971e316Smrg  * (where p ranges over the (non-parametric) dimensions),
2294*5971e316Smrg  * compute the transitive closure of this map, i.e.,
2295*5971e316Smrg  *
2296*5971e316Smrg  *	{ i -> j : exists k > 0:
2297*5971e316Smrg  *		k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2298*5971e316Smrg  *
2299*5971e316Smrg  * and intersect domain and range of this transitive closure with
2300*5971e316Smrg  * domain and range of the original map.
2301*5971e316Smrg  */
box_closure(__isl_take isl_map * map)2302*5971e316Smrg static __isl_give isl_map *box_closure(__isl_take isl_map *map)
2303*5971e316Smrg {
2304*5971e316Smrg 	isl_set *domain;
2305*5971e316Smrg 	isl_set *range;
2306*5971e316Smrg 
2307*5971e316Smrg 	domain = isl_map_domain(isl_map_copy(map));
2308*5971e316Smrg 	domain = isl_set_coalesce(domain);
2309*5971e316Smrg 	range = isl_map_range(isl_map_copy(map));
2310*5971e316Smrg 	range = isl_set_coalesce(range);
2311*5971e316Smrg 
2312*5971e316Smrg 	return box_closure_on_domain(map, domain, range, 0);
2313*5971e316Smrg }
2314*5971e316Smrg 
2315*5971e316Smrg /* Given a map, compute the smallest superset of this map that is of the form
2316*5971e316Smrg  *
2317*5971e316Smrg  *	{ i -> j : L <= j - i <= U and exists a_p: j_p - i_p = M_p a_p }
2318*5971e316Smrg  *
2319*5971e316Smrg  * (where p ranges over the (non-parametric) dimensions),
2320*5971e316Smrg  * compute the transitive and partially reflexive closure of this map, i.e.,
2321*5971e316Smrg  *
2322*5971e316Smrg  *	{ i -> j : exists k >= 0:
2323*5971e316Smrg  *		k L <= j - i <= k U and exists a: j_p - i_p = M_p a_p }
2324*5971e316Smrg  *
2325*5971e316Smrg  * and intersect domain and range of this transitive closure with
2326*5971e316Smrg  * the given domain.
2327*5971e316Smrg  */
box_closure_with_identity(__isl_take isl_map * map,__isl_take isl_set * dom)2328*5971e316Smrg static __isl_give isl_map *box_closure_with_identity(__isl_take isl_map *map,
2329*5971e316Smrg 	__isl_take isl_set *dom)
2330*5971e316Smrg {
2331*5971e316Smrg 	return box_closure_on_domain(map, dom, isl_set_copy(dom), 1);
2332*5971e316Smrg }
2333*5971e316Smrg 
2334*5971e316Smrg /* Check whether app is the transitive closure of map.
2335*5971e316Smrg  * In particular, check that app is acyclic and, if so,
2336*5971e316Smrg  * check that
2337*5971e316Smrg  *
2338*5971e316Smrg  *	app \subset (map \cup (map \circ app))
2339*5971e316Smrg  */
check_exactness_omega(__isl_keep isl_map * map,__isl_keep isl_map * app)2340*5971e316Smrg static isl_bool check_exactness_omega(__isl_keep isl_map *map,
2341*5971e316Smrg 	__isl_keep isl_map *app)
2342*5971e316Smrg {
2343*5971e316Smrg 	isl_set *delta;
2344*5971e316Smrg 	int i;
2345*5971e316Smrg 	isl_bool is_empty, is_exact;
2346*5971e316Smrg 	isl_size d;
2347*5971e316Smrg 	isl_map *test;
2348*5971e316Smrg 
2349*5971e316Smrg 	delta = isl_map_deltas(isl_map_copy(app));
2350*5971e316Smrg 	d = isl_set_dim(delta, isl_dim_set);
2351*5971e316Smrg 	if (d < 0)
2352*5971e316Smrg 		delta = isl_set_free(delta);
2353*5971e316Smrg 	for (i = 0; i < d; ++i)
2354*5971e316Smrg 		delta = isl_set_fix_si(delta, isl_dim_set, i, 0);
2355*5971e316Smrg 	is_empty = isl_set_is_empty(delta);
2356*5971e316Smrg 	isl_set_free(delta);
2357*5971e316Smrg 	if (is_empty < 0 || !is_empty)
2358*5971e316Smrg 		return is_empty;
2359*5971e316Smrg 
2360*5971e316Smrg 	test = isl_map_apply_range(isl_map_copy(app), isl_map_copy(map));
2361*5971e316Smrg 	test = isl_map_union(test, isl_map_copy(map));
2362*5971e316Smrg 	is_exact = isl_map_is_subset(app, test);
2363*5971e316Smrg 	isl_map_free(test);
2364*5971e316Smrg 
2365*5971e316Smrg 	return is_exact;
2366*5971e316Smrg }
2367*5971e316Smrg 
2368*5971e316Smrg /* Check if basic map M_i can be combined with all the other
2369*5971e316Smrg  * basic maps such that
2370*5971e316Smrg  *
2371*5971e316Smrg  *	(\cup_j M_j)^+
2372*5971e316Smrg  *
2373*5971e316Smrg  * can be computed as
2374*5971e316Smrg  *
2375*5971e316Smrg  *	M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2376*5971e316Smrg  *
2377*5971e316Smrg  * In particular, check if we can compute a compact representation
2378*5971e316Smrg  * of
2379*5971e316Smrg  *
2380*5971e316Smrg  *		M_i^* \circ M_j \circ M_i^*
2381*5971e316Smrg  *
2382*5971e316Smrg  * for each j != i.
2383*5971e316Smrg  * Let M_i^? be an extension of M_i^+ that allows paths
2384*5971e316Smrg  * of length zero, i.e., the result of box_closure(., 1).
2385*5971e316Smrg  * The criterion, as proposed by Kelly et al., is that
2386*5971e316Smrg  * id = M_i^? - M_i^+ can be represented as a basic map
2387*5971e316Smrg  * and that
2388*5971e316Smrg  *
2389*5971e316Smrg  *	id \circ M_j \circ id = M_j
2390*5971e316Smrg  *
2391*5971e316Smrg  * for each j != i.
2392*5971e316Smrg  *
2393*5971e316Smrg  * If this function returns 1, then tc and qc are set to
2394*5971e316Smrg  * M_i^+ and M_i^?, respectively.
2395*5971e316Smrg  */
can_be_split_off(__isl_keep isl_map * map,int i,__isl_give isl_map ** tc,__isl_give isl_map ** qc)2396*5971e316Smrg static int can_be_split_off(__isl_keep isl_map *map, int i,
2397*5971e316Smrg 	__isl_give isl_map **tc, __isl_give isl_map **qc)
2398*5971e316Smrg {
2399*5971e316Smrg 	isl_map *map_i, *id = NULL;
2400*5971e316Smrg 	int j = -1;
2401*5971e316Smrg 	isl_set *C;
2402*5971e316Smrg 
2403*5971e316Smrg 	*tc = NULL;
2404*5971e316Smrg 	*qc = NULL;
2405*5971e316Smrg 
2406*5971e316Smrg 	C = isl_set_union(isl_map_domain(isl_map_copy(map)),
2407*5971e316Smrg 			  isl_map_range(isl_map_copy(map)));
2408*5971e316Smrg 	C = isl_set_from_basic_set(isl_set_simple_hull(C));
2409*5971e316Smrg 	if (!C)
2410*5971e316Smrg 		goto error;
2411*5971e316Smrg 
2412*5971e316Smrg 	map_i = isl_map_from_basic_map(isl_basic_map_copy(map->p[i]));
2413*5971e316Smrg 	*tc = box_closure(isl_map_copy(map_i));
2414*5971e316Smrg 	*qc = box_closure_with_identity(map_i, C);
2415*5971e316Smrg 	id = isl_map_subtract(isl_map_copy(*qc), isl_map_copy(*tc));
2416*5971e316Smrg 
2417*5971e316Smrg 	if (!id || !*qc)
2418*5971e316Smrg 		goto error;
2419*5971e316Smrg 	if (id->n != 1 || (*qc)->n != 1)
2420*5971e316Smrg 		goto done;
2421*5971e316Smrg 
2422*5971e316Smrg 	for (j = 0; j < map->n; ++j) {
2423*5971e316Smrg 		isl_map *map_j, *test;
2424*5971e316Smrg 		int is_ok;
2425*5971e316Smrg 
2426*5971e316Smrg 		if (i == j)
2427*5971e316Smrg 			continue;
2428*5971e316Smrg 		map_j = isl_map_from_basic_map(
2429*5971e316Smrg 					isl_basic_map_copy(map->p[j]));
2430*5971e316Smrg 		test = isl_map_apply_range(isl_map_copy(id),
2431*5971e316Smrg 						isl_map_copy(map_j));
2432*5971e316Smrg 		test = isl_map_apply_range(test, isl_map_copy(id));
2433*5971e316Smrg 		is_ok = isl_map_is_equal(test, map_j);
2434*5971e316Smrg 		isl_map_free(map_j);
2435*5971e316Smrg 		isl_map_free(test);
2436*5971e316Smrg 		if (is_ok < 0)
2437*5971e316Smrg 			goto error;
2438*5971e316Smrg 		if (!is_ok)
2439*5971e316Smrg 			break;
2440*5971e316Smrg 	}
2441*5971e316Smrg 
2442*5971e316Smrg done:
2443*5971e316Smrg 	isl_map_free(id);
2444*5971e316Smrg 	if (j == map->n)
2445*5971e316Smrg 		return 1;
2446*5971e316Smrg 
2447*5971e316Smrg 	isl_map_free(*qc);
2448*5971e316Smrg 	isl_map_free(*tc);
2449*5971e316Smrg 	*qc = NULL;
2450*5971e316Smrg 	*tc = NULL;
2451*5971e316Smrg 
2452*5971e316Smrg 	return 0;
2453*5971e316Smrg error:
2454*5971e316Smrg 	isl_map_free(id);
2455*5971e316Smrg 	isl_map_free(*qc);
2456*5971e316Smrg 	isl_map_free(*tc);
2457*5971e316Smrg 	*qc = NULL;
2458*5971e316Smrg 	*tc = NULL;
2459*5971e316Smrg 	return -1;
2460*5971e316Smrg }
2461*5971e316Smrg 
box_closure_with_check(__isl_take isl_map * map,isl_bool * exact)2462*5971e316Smrg static __isl_give isl_map *box_closure_with_check(__isl_take isl_map *map,
2463*5971e316Smrg 	isl_bool *exact)
2464*5971e316Smrg {
2465*5971e316Smrg 	isl_map *app;
2466*5971e316Smrg 
2467*5971e316Smrg 	app = box_closure(isl_map_copy(map));
2468*5971e316Smrg 	if (exact) {
2469*5971e316Smrg 		isl_bool is_exact = check_exactness_omega(map, app);
2470*5971e316Smrg 
2471*5971e316Smrg 		if (is_exact < 0)
2472*5971e316Smrg 			app = isl_map_free(app);
2473*5971e316Smrg 		else
2474*5971e316Smrg 			*exact = is_exact;
2475*5971e316Smrg 	}
2476*5971e316Smrg 
2477*5971e316Smrg 	isl_map_free(map);
2478*5971e316Smrg 	return app;
2479*5971e316Smrg }
2480*5971e316Smrg 
2481*5971e316Smrg /* Compute an overapproximation of the transitive closure of "map"
2482*5971e316Smrg  * using a variation of the algorithm from
2483*5971e316Smrg  * "Transitive Closure of Infinite Graphs and its Applications"
2484*5971e316Smrg  * by Kelly et al.
2485*5971e316Smrg  *
2486*5971e316Smrg  * We first check whether we can can split of any basic map M_i and
2487*5971e316Smrg  * compute
2488*5971e316Smrg  *
2489*5971e316Smrg  *	(\cup_j M_j)^+
2490*5971e316Smrg  *
2491*5971e316Smrg  * as
2492*5971e316Smrg  *
2493*5971e316Smrg  *	M_i \cup (\cup_{j \ne i} M_i^* \circ M_j \circ M_i^*)^+
2494*5971e316Smrg  *
2495*5971e316Smrg  * using a recursive call on the remaining map.
2496*5971e316Smrg  *
2497*5971e316Smrg  * If not, we simply call box_closure on the whole map.
2498*5971e316Smrg  */
transitive_closure_omega(__isl_take isl_map * map,isl_bool * exact)2499*5971e316Smrg static __isl_give isl_map *transitive_closure_omega(__isl_take isl_map *map,
2500*5971e316Smrg 	isl_bool *exact)
2501*5971e316Smrg {
2502*5971e316Smrg 	int i, j;
2503*5971e316Smrg 	isl_bool exact_i;
2504*5971e316Smrg 	isl_map *app;
2505*5971e316Smrg 
2506*5971e316Smrg 	if (!map)
2507*5971e316Smrg 		return NULL;
2508*5971e316Smrg 	if (map->n == 1)
2509*5971e316Smrg 		return box_closure_with_check(map, exact);
2510*5971e316Smrg 
2511*5971e316Smrg 	for (i = 0; i < map->n; ++i) {
2512*5971e316Smrg 		int ok;
2513*5971e316Smrg 		isl_map *qc, *tc;
2514*5971e316Smrg 		ok = can_be_split_off(map, i, &tc, &qc);
2515*5971e316Smrg 		if (ok < 0)
2516*5971e316Smrg 			goto error;
2517*5971e316Smrg 		if (!ok)
2518*5971e316Smrg 			continue;
2519*5971e316Smrg 
2520*5971e316Smrg 		app = isl_map_alloc_space(isl_map_get_space(map), map->n - 1, 0);
2521*5971e316Smrg 
2522*5971e316Smrg 		for (j = 0; j < map->n; ++j) {
2523*5971e316Smrg 			if (j == i)
2524*5971e316Smrg 				continue;
2525*5971e316Smrg 			app = isl_map_add_basic_map(app,
2526*5971e316Smrg 						isl_basic_map_copy(map->p[j]));
2527*5971e316Smrg 		}
2528*5971e316Smrg 
2529*5971e316Smrg 		app = isl_map_apply_range(isl_map_copy(qc), app);
2530*5971e316Smrg 		app = isl_map_apply_range(app, qc);
2531*5971e316Smrg 
2532*5971e316Smrg 		app = isl_map_union(tc, transitive_closure_omega(app, NULL));
2533*5971e316Smrg 		exact_i = check_exactness_omega(map, app);
2534*5971e316Smrg 		if (exact_i == isl_bool_true) {
2535*5971e316Smrg 			if (exact)
2536*5971e316Smrg 				*exact = exact_i;
2537*5971e316Smrg 			isl_map_free(map);
2538*5971e316Smrg 			return app;
2539*5971e316Smrg 		}
2540*5971e316Smrg 		isl_map_free(app);
2541*5971e316Smrg 		if (exact_i < 0)
2542*5971e316Smrg 			goto error;
2543*5971e316Smrg 	}
2544*5971e316Smrg 
2545*5971e316Smrg 	return box_closure_with_check(map, exact);
2546*5971e316Smrg error:
2547*5971e316Smrg 	isl_map_free(map);
2548*5971e316Smrg 	return NULL;
2549*5971e316Smrg }
2550*5971e316Smrg 
2551*5971e316Smrg /* Compute the transitive closure  of "map", or an overapproximation.
2552*5971e316Smrg  * If the result is exact, then *exact is set to 1.
2553*5971e316Smrg  * Simply use map_power to compute the powers of map, but tell
2554*5971e316Smrg  * it to project out the lengths of the paths instead of equating
2555*5971e316Smrg  * the length to a parameter.
2556*5971e316Smrg  */
isl_map_transitive_closure(__isl_take isl_map * map,isl_bool * exact)2557*5971e316Smrg __isl_give isl_map *isl_map_transitive_closure(__isl_take isl_map *map,
2558*5971e316Smrg 	isl_bool *exact)
2559*5971e316Smrg {
2560*5971e316Smrg 	isl_space *target_dim;
2561*5971e316Smrg 	isl_bool closed;
2562*5971e316Smrg 
2563*5971e316Smrg 	if (!map)
2564*5971e316Smrg 		goto error;
2565*5971e316Smrg 
2566*5971e316Smrg 	if (map->ctx->opt->closure == ISL_CLOSURE_BOX)
2567*5971e316Smrg 		return transitive_closure_omega(map, exact);
2568*5971e316Smrg 
2569*5971e316Smrg 	map = isl_map_compute_divs(map);
2570*5971e316Smrg 	map = isl_map_coalesce(map);
2571*5971e316Smrg 	closed = isl_map_is_transitively_closed(map);
2572*5971e316Smrg 	if (closed < 0)
2573*5971e316Smrg 		goto error;
2574*5971e316Smrg 	if (closed) {
2575*5971e316Smrg 		if (exact)
2576*5971e316Smrg 			*exact = isl_bool_true;
2577*5971e316Smrg 		return map;
2578*5971e316Smrg 	}
2579*5971e316Smrg 
2580*5971e316Smrg 	target_dim = isl_map_get_space(map);
2581*5971e316Smrg 	map = map_power(map, exact, 1);
2582*5971e316Smrg 	map = isl_map_reset_space(map, target_dim);
2583*5971e316Smrg 
2584*5971e316Smrg 	return map;
2585*5971e316Smrg error:
2586*5971e316Smrg 	isl_map_free(map);
2587*5971e316Smrg 	return NULL;
2588*5971e316Smrg }
2589*5971e316Smrg 
inc_count(__isl_take isl_map * map,void * user)2590*5971e316Smrg static isl_stat inc_count(__isl_take isl_map *map, void *user)
2591*5971e316Smrg {
2592*5971e316Smrg 	int *n = user;
2593*5971e316Smrg 
2594*5971e316Smrg 	*n += map->n;
2595*5971e316Smrg 
2596*5971e316Smrg 	isl_map_free(map);
2597*5971e316Smrg 
2598*5971e316Smrg 	return isl_stat_ok;
2599*5971e316Smrg }
2600*5971e316Smrg 
collect_basic_map(__isl_take isl_map * map,void * user)2601*5971e316Smrg static isl_stat collect_basic_map(__isl_take isl_map *map, void *user)
2602*5971e316Smrg {
2603*5971e316Smrg 	int i;
2604*5971e316Smrg 	isl_basic_map ***next = user;
2605*5971e316Smrg 
2606*5971e316Smrg 	for (i = 0; i < map->n; ++i) {
2607*5971e316Smrg 		**next = isl_basic_map_copy(map->p[i]);
2608*5971e316Smrg 		if (!**next)
2609*5971e316Smrg 			goto error;
2610*5971e316Smrg 		(*next)++;
2611*5971e316Smrg 	}
2612*5971e316Smrg 
2613*5971e316Smrg 	isl_map_free(map);
2614*5971e316Smrg 	return isl_stat_ok;
2615*5971e316Smrg error:
2616*5971e316Smrg 	isl_map_free(map);
2617*5971e316Smrg 	return isl_stat_error;
2618*5971e316Smrg }
2619*5971e316Smrg 
2620*5971e316Smrg /* Perform Floyd-Warshall on the given list of basic relations.
2621*5971e316Smrg  * The basic relations may live in different dimensions,
2622*5971e316Smrg  * but basic relations that get assigned to the diagonal of the
2623*5971e316Smrg  * grid have domains and ranges of the same dimension and so
2624*5971e316Smrg  * the standard algorithm can be used because the nested transitive
2625*5971e316Smrg  * closures are only applied to diagonal elements and because all
2626*5971e316Smrg  * compositions are performed on relations with compatible domains and ranges.
2627*5971e316Smrg  */
union_floyd_warshall_on_list(isl_ctx * ctx,__isl_keep isl_basic_map ** list,int n,isl_bool * exact)2628*5971e316Smrg static __isl_give isl_union_map *union_floyd_warshall_on_list(isl_ctx *ctx,
2629*5971e316Smrg 	__isl_keep isl_basic_map **list, int n, isl_bool *exact)
2630*5971e316Smrg {
2631*5971e316Smrg 	int i, j, k;
2632*5971e316Smrg 	int n_group;
2633*5971e316Smrg 	int *group = NULL;
2634*5971e316Smrg 	isl_set **set = NULL;
2635*5971e316Smrg 	isl_map ***grid = NULL;
2636*5971e316Smrg 	isl_union_map *app;
2637*5971e316Smrg 
2638*5971e316Smrg 	group = setup_groups(ctx, list, n, &set, &n_group);
2639*5971e316Smrg 	if (!group)
2640*5971e316Smrg 		goto error;
2641*5971e316Smrg 
2642*5971e316Smrg 	grid = isl_calloc_array(ctx, isl_map **, n_group);
2643*5971e316Smrg 	if (!grid)
2644*5971e316Smrg 		goto error;
2645*5971e316Smrg 	for (i = 0; i < n_group; ++i) {
2646*5971e316Smrg 		grid[i] = isl_calloc_array(ctx, isl_map *, n_group);
2647*5971e316Smrg 		if (!grid[i])
2648*5971e316Smrg 			goto error;
2649*5971e316Smrg 		for (j = 0; j < n_group; ++j) {
2650*5971e316Smrg 			isl_space *space1, *space2, *space;
2651*5971e316Smrg 			space1 = isl_space_reverse(isl_set_get_space(set[i]));
2652*5971e316Smrg 			space2 = isl_set_get_space(set[j]);
2653*5971e316Smrg 			space = isl_space_join(space1, space2);
2654*5971e316Smrg 			grid[i][j] = isl_map_empty(space);
2655*5971e316Smrg 		}
2656*5971e316Smrg 	}
2657*5971e316Smrg 
2658*5971e316Smrg 	for (k = 0; k < n; ++k) {
2659*5971e316Smrg 		i = group[2 * k];
2660*5971e316Smrg 		j = group[2 * k + 1];
2661*5971e316Smrg 		grid[i][j] = isl_map_union(grid[i][j],
2662*5971e316Smrg 				isl_map_from_basic_map(
2663*5971e316Smrg 					isl_basic_map_copy(list[k])));
2664*5971e316Smrg 	}
2665*5971e316Smrg 
2666*5971e316Smrg 	floyd_warshall_iterate(grid, n_group, exact);
2667*5971e316Smrg 
2668*5971e316Smrg 	app = isl_union_map_empty(isl_map_get_space(grid[0][0]));
2669*5971e316Smrg 
2670*5971e316Smrg 	for (i = 0; i < n_group; ++i) {
2671*5971e316Smrg 		for (j = 0; j < n_group; ++j)
2672*5971e316Smrg 			app = isl_union_map_add_map(app, grid[i][j]);
2673*5971e316Smrg 		free(grid[i]);
2674*5971e316Smrg 	}
2675*5971e316Smrg 	free(grid);
2676*5971e316Smrg 
2677*5971e316Smrg 	for (i = 0; i < 2 * n; ++i)
2678*5971e316Smrg 		isl_set_free(set[i]);
2679*5971e316Smrg 	free(set);
2680*5971e316Smrg 
2681*5971e316Smrg 	free(group);
2682*5971e316Smrg 	return app;
2683*5971e316Smrg error:
2684*5971e316Smrg 	if (grid)
2685*5971e316Smrg 		for (i = 0; i < n_group; ++i) {
2686*5971e316Smrg 			if (!grid[i])
2687*5971e316Smrg 				continue;
2688*5971e316Smrg 			for (j = 0; j < n_group; ++j)
2689*5971e316Smrg 				isl_map_free(grid[i][j]);
2690*5971e316Smrg 			free(grid[i]);
2691*5971e316Smrg 		}
2692*5971e316Smrg 	free(grid);
2693*5971e316Smrg 	if (set) {
2694*5971e316Smrg 		for (i = 0; i < 2 * n; ++i)
2695*5971e316Smrg 			isl_set_free(set[i]);
2696*5971e316Smrg 		free(set);
2697*5971e316Smrg 	}
2698*5971e316Smrg 	free(group);
2699*5971e316Smrg 	return NULL;
2700*5971e316Smrg }
2701*5971e316Smrg 
2702*5971e316Smrg /* Perform Floyd-Warshall on the given union relation.
2703*5971e316Smrg  * The implementation is very similar to that for non-unions.
2704*5971e316Smrg  * The main difference is that it is applied unconditionally.
2705*5971e316Smrg  * We first extract a list of basic maps from the union map
2706*5971e316Smrg  * and then perform the algorithm on this list.
2707*5971e316Smrg  */
union_floyd_warshall(__isl_take isl_union_map * umap,isl_bool * exact)2708*5971e316Smrg static __isl_give isl_union_map *union_floyd_warshall(
2709*5971e316Smrg 	__isl_take isl_union_map *umap, isl_bool *exact)
2710*5971e316Smrg {
2711*5971e316Smrg 	int i, n;
2712*5971e316Smrg 	isl_ctx *ctx;
2713*5971e316Smrg 	isl_basic_map **list = NULL;
2714*5971e316Smrg 	isl_basic_map **next;
2715*5971e316Smrg 	isl_union_map *res;
2716*5971e316Smrg 
2717*5971e316Smrg 	n = 0;
2718*5971e316Smrg 	if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2719*5971e316Smrg 		goto error;
2720*5971e316Smrg 
2721*5971e316Smrg 	ctx = isl_union_map_get_ctx(umap);
2722*5971e316Smrg 	list = isl_calloc_array(ctx, isl_basic_map *, n);
2723*5971e316Smrg 	if (!list)
2724*5971e316Smrg 		goto error;
2725*5971e316Smrg 
2726*5971e316Smrg 	next = list;
2727*5971e316Smrg 	if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2728*5971e316Smrg 		goto error;
2729*5971e316Smrg 
2730*5971e316Smrg 	res = union_floyd_warshall_on_list(ctx, list, n, exact);
2731*5971e316Smrg 
2732*5971e316Smrg 	if (list) {
2733*5971e316Smrg 		for (i = 0; i < n; ++i)
2734*5971e316Smrg 			isl_basic_map_free(list[i]);
2735*5971e316Smrg 		free(list);
2736*5971e316Smrg 	}
2737*5971e316Smrg 
2738*5971e316Smrg 	isl_union_map_free(umap);
2739*5971e316Smrg 	return res;
2740*5971e316Smrg error:
2741*5971e316Smrg 	if (list) {
2742*5971e316Smrg 		for (i = 0; i < n; ++i)
2743*5971e316Smrg 			isl_basic_map_free(list[i]);
2744*5971e316Smrg 		free(list);
2745*5971e316Smrg 	}
2746*5971e316Smrg 	isl_union_map_free(umap);
2747*5971e316Smrg 	return NULL;
2748*5971e316Smrg }
2749*5971e316Smrg 
2750*5971e316Smrg /* Decompose the give union relation into strongly connected components.
2751*5971e316Smrg  * The implementation is essentially the same as that of
2752*5971e316Smrg  * construct_power_components with the major difference that all
2753*5971e316Smrg  * operations are performed on union maps.
2754*5971e316Smrg  */
union_components(__isl_take isl_union_map * umap,isl_bool * exact)2755*5971e316Smrg static __isl_give isl_union_map *union_components(
2756*5971e316Smrg 	__isl_take isl_union_map *umap, isl_bool *exact)
2757*5971e316Smrg {
2758*5971e316Smrg 	int i;
2759*5971e316Smrg 	int n;
2760*5971e316Smrg 	isl_ctx *ctx;
2761*5971e316Smrg 	isl_basic_map **list = NULL;
2762*5971e316Smrg 	isl_basic_map **next;
2763*5971e316Smrg 	isl_union_map *path = NULL;
2764*5971e316Smrg 	struct isl_tc_follows_data data;
2765*5971e316Smrg 	struct isl_tarjan_graph *g = NULL;
2766*5971e316Smrg 	int c, l;
2767*5971e316Smrg 	int recheck = 0;
2768*5971e316Smrg 
2769*5971e316Smrg 	n = 0;
2770*5971e316Smrg 	if (isl_union_map_foreach_map(umap, inc_count, &n) < 0)
2771*5971e316Smrg 		goto error;
2772*5971e316Smrg 
2773*5971e316Smrg 	if (n == 0)
2774*5971e316Smrg 		return umap;
2775*5971e316Smrg 	if (n <= 1)
2776*5971e316Smrg 		return union_floyd_warshall(umap, exact);
2777*5971e316Smrg 
2778*5971e316Smrg 	ctx = isl_union_map_get_ctx(umap);
2779*5971e316Smrg 	list = isl_calloc_array(ctx, isl_basic_map *, n);
2780*5971e316Smrg 	if (!list)
2781*5971e316Smrg 		goto error;
2782*5971e316Smrg 
2783*5971e316Smrg 	next = list;
2784*5971e316Smrg 	if (isl_union_map_foreach_map(umap, collect_basic_map, &next) < 0)
2785*5971e316Smrg 		goto error;
2786*5971e316Smrg 
2787*5971e316Smrg 	data.list = list;
2788*5971e316Smrg 	data.check_closed = 0;
2789*5971e316Smrg 	g = isl_tarjan_graph_init(ctx, n, &basic_map_follows, &data);
2790*5971e316Smrg 	if (!g)
2791*5971e316Smrg 		goto error;
2792*5971e316Smrg 
2793*5971e316Smrg 	c = 0;
2794*5971e316Smrg 	i = 0;
2795*5971e316Smrg 	l = n;
2796*5971e316Smrg 	path = isl_union_map_empty(isl_union_map_get_space(umap));
2797*5971e316Smrg 	while (l) {
2798*5971e316Smrg 		isl_union_map *comp;
2799*5971e316Smrg 		isl_union_map *path_comp, *path_comb;
2800*5971e316Smrg 		comp = isl_union_map_empty(isl_union_map_get_space(umap));
2801*5971e316Smrg 		while (g->order[i] != -1) {
2802*5971e316Smrg 			comp = isl_union_map_add_map(comp,
2803*5971e316Smrg 				    isl_map_from_basic_map(
2804*5971e316Smrg 					isl_basic_map_copy(list[g->order[i]])));
2805*5971e316Smrg 			--l;
2806*5971e316Smrg 			++i;
2807*5971e316Smrg 		}
2808*5971e316Smrg 		path_comp = union_floyd_warshall(comp, exact);
2809*5971e316Smrg 		path_comb = isl_union_map_apply_range(isl_union_map_copy(path),
2810*5971e316Smrg 						isl_union_map_copy(path_comp));
2811*5971e316Smrg 		path = isl_union_map_union(path, path_comp);
2812*5971e316Smrg 		path = isl_union_map_union(path, path_comb);
2813*5971e316Smrg 		++i;
2814*5971e316Smrg 		++c;
2815*5971e316Smrg 	}
2816*5971e316Smrg 
2817*5971e316Smrg 	if (c > 1 && data.check_closed && !*exact) {
2818*5971e316Smrg 		isl_bool closed;
2819*5971e316Smrg 
2820*5971e316Smrg 		closed = isl_union_map_is_transitively_closed(path);
2821*5971e316Smrg 		if (closed < 0)
2822*5971e316Smrg 			goto error;
2823*5971e316Smrg 		recheck = !closed;
2824*5971e316Smrg 	}
2825*5971e316Smrg 
2826*5971e316Smrg 	isl_tarjan_graph_free(g);
2827*5971e316Smrg 
2828*5971e316Smrg 	for (i = 0; i < n; ++i)
2829*5971e316Smrg 		isl_basic_map_free(list[i]);
2830*5971e316Smrg 	free(list);
2831*5971e316Smrg 
2832*5971e316Smrg 	if (recheck) {
2833*5971e316Smrg 		isl_union_map_free(path);
2834*5971e316Smrg 		return union_floyd_warshall(umap, exact);
2835*5971e316Smrg 	}
2836*5971e316Smrg 
2837*5971e316Smrg 	isl_union_map_free(umap);
2838*5971e316Smrg 
2839*5971e316Smrg 	return path;
2840*5971e316Smrg error:
2841*5971e316Smrg 	isl_tarjan_graph_free(g);
2842*5971e316Smrg 	if (list) {
2843*5971e316Smrg 		for (i = 0; i < n; ++i)
2844*5971e316Smrg 			isl_basic_map_free(list[i]);
2845*5971e316Smrg 		free(list);
2846*5971e316Smrg 	}
2847*5971e316Smrg 	isl_union_map_free(umap);
2848*5971e316Smrg 	isl_union_map_free(path);
2849*5971e316Smrg 	return NULL;
2850*5971e316Smrg }
2851*5971e316Smrg 
2852*5971e316Smrg /* Compute the transitive closure  of "umap", or an overapproximation.
2853*5971e316Smrg  * If the result is exact, then *exact is set to 1.
2854*5971e316Smrg  */
isl_union_map_transitive_closure(__isl_take isl_union_map * umap,isl_bool * exact)2855*5971e316Smrg __isl_give isl_union_map *isl_union_map_transitive_closure(
2856*5971e316Smrg 	__isl_take isl_union_map *umap, isl_bool *exact)
2857*5971e316Smrg {
2858*5971e316Smrg 	isl_bool closed;
2859*5971e316Smrg 
2860*5971e316Smrg 	if (!umap)
2861*5971e316Smrg 		return NULL;
2862*5971e316Smrg 
2863*5971e316Smrg 	if (exact)
2864*5971e316Smrg 		*exact = isl_bool_true;
2865*5971e316Smrg 
2866*5971e316Smrg 	umap = isl_union_map_compute_divs(umap);
2867*5971e316Smrg 	umap = isl_union_map_coalesce(umap);
2868*5971e316Smrg 	closed = isl_union_map_is_transitively_closed(umap);
2869*5971e316Smrg 	if (closed < 0)
2870*5971e316Smrg 		goto error;
2871*5971e316Smrg 	if (closed)
2872*5971e316Smrg 		return umap;
2873*5971e316Smrg 	umap = union_components(umap, exact);
2874*5971e316Smrg 	return umap;
2875*5971e316Smrg error:
2876*5971e316Smrg 	isl_union_map_free(umap);
2877*5971e316Smrg 	return NULL;
2878*5971e316Smrg }
2879*5971e316Smrg 
2880*5971e316Smrg struct isl_union_power {
2881*5971e316Smrg 	isl_union_map *pow;
2882*5971e316Smrg 	isl_bool *exact;
2883*5971e316Smrg };
2884*5971e316Smrg 
power(__isl_take isl_map * map,void * user)2885*5971e316Smrg static isl_stat power(__isl_take isl_map *map, void *user)
2886*5971e316Smrg {
2887*5971e316Smrg 	struct isl_union_power *up = user;
2888*5971e316Smrg 
2889*5971e316Smrg 	map = isl_map_power(map, up->exact);
2890*5971e316Smrg 	up->pow = isl_union_map_from_map(map);
2891*5971e316Smrg 
2892*5971e316Smrg 	return isl_stat_error;
2893*5971e316Smrg }
2894*5971e316Smrg 
2895*5971e316Smrg /* Construct a map [[x]->[y]] -> [y-x], with parameters prescribed by "space".
2896*5971e316Smrg  */
deltas_map(__isl_take isl_space * space)2897*5971e316Smrg static __isl_give isl_union_map *deltas_map(__isl_take isl_space *space)
2898*5971e316Smrg {
2899*5971e316Smrg 	isl_basic_map *bmap;
2900*5971e316Smrg 
2901*5971e316Smrg 	space = isl_space_add_dims(space, isl_dim_in, 1);
2902*5971e316Smrg 	space = isl_space_add_dims(space, isl_dim_out, 1);
2903*5971e316Smrg 	bmap = isl_basic_map_universe(space);
2904*5971e316Smrg 	bmap = isl_basic_map_deltas_map(bmap);
2905*5971e316Smrg 
2906*5971e316Smrg 	return isl_union_map_from_map(isl_map_from_basic_map(bmap));
2907*5971e316Smrg }
2908*5971e316Smrg 
2909*5971e316Smrg /* Compute the positive powers of "map", or an overapproximation.
2910*5971e316Smrg  * The result maps the exponent to a nested copy of the corresponding power.
2911*5971e316Smrg  * If the result is exact, then *exact is set to 1.
2912*5971e316Smrg  */
isl_union_map_power(__isl_take isl_union_map * umap,isl_bool * exact)2913*5971e316Smrg __isl_give isl_union_map *isl_union_map_power(__isl_take isl_union_map *umap,
2914*5971e316Smrg 	isl_bool *exact)
2915*5971e316Smrg {
2916*5971e316Smrg 	isl_size n;
2917*5971e316Smrg 	isl_union_map *inc;
2918*5971e316Smrg 	isl_union_map *dm;
2919*5971e316Smrg 
2920*5971e316Smrg 	n = isl_union_map_n_map(umap);
2921*5971e316Smrg 	if (n < 0)
2922*5971e316Smrg 		return isl_union_map_free(umap);
2923*5971e316Smrg 	if (n == 0)
2924*5971e316Smrg 		return umap;
2925*5971e316Smrg 	if (n == 1) {
2926*5971e316Smrg 		struct isl_union_power up = { NULL, exact };
2927*5971e316Smrg 		isl_union_map_foreach_map(umap, &power, &up);
2928*5971e316Smrg 		isl_union_map_free(umap);
2929*5971e316Smrg 		return up.pow;
2930*5971e316Smrg 	}
2931*5971e316Smrg 	inc = isl_union_map_from_map(increment(isl_union_map_get_space(umap)));
2932*5971e316Smrg 	umap = isl_union_map_product(inc, umap);
2933*5971e316Smrg 	umap = isl_union_map_transitive_closure(umap, exact);
2934*5971e316Smrg 	umap = isl_union_map_zip(umap);
2935*5971e316Smrg 	dm = deltas_map(isl_union_map_get_space(umap));
2936*5971e316Smrg 	umap = isl_union_map_apply_domain(umap, dm);
2937*5971e316Smrg 
2938*5971e316Smrg 	return umap;
2939*5971e316Smrg }
2940*5971e316Smrg 
2941*5971e316Smrg #undef TYPE
2942*5971e316Smrg #define TYPE isl_map
2943*5971e316Smrg #include "isl_power_templ.c"
2944*5971e316Smrg 
2945*5971e316Smrg #undef TYPE
2946*5971e316Smrg #define TYPE isl_union_map
2947*5971e316Smrg #include "isl_power_templ.c"
2948