1 /*
2  * Copyright 2005-2007 Universiteit Leiden
3  * Copyright 2008-2009 Katholieke Universiteit Leuven
4  * Copyright 2010      INRIA Saclay
5  * Copyright 2012      Universiteit Leiden
6  * Copyright 2014      Ecole Normale Superieure
7  *
8  * Use of this software is governed by the MIT license
9  *
10  * Written by Sven Verdoolaege, Leiden Institute of Advanced Computer Science,
11  * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
12  * and K.U.Leuven, Departement Computerwetenschappen, Celestijnenlaan 200A,
13  * B-3001 Leuven, Belgium
14  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
15  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
16  * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
17  */
18 
19 #include <isl/val.h>
20 #include <isl/space.h>
21 #include <isl/set.h>
22 #include <isl/map.h>
23 #include <isl/union_set.h>
24 #include <isl/union_map.h>
25 #include <isl/flow.h>
26 #include <isl/schedule_node.h>
27 #include <isl_sort.h>
28 #include <isl/stream.h>
29 
30 enum isl_restriction_type {
31 	isl_restriction_type_empty,
32 	isl_restriction_type_none,
33 	isl_restriction_type_input,
34 	isl_restriction_type_output
35 };
36 
37 struct isl_restriction {
38 	enum isl_restriction_type type;
39 
40 	isl_set *source;
41 	isl_set *sink;
42 };
43 
44 /* Create a restriction of the given type.
45  */
isl_restriction_alloc(__isl_take isl_map * source_map,enum isl_restriction_type type)46 static __isl_give isl_restriction *isl_restriction_alloc(
47 	__isl_take isl_map *source_map, enum isl_restriction_type type)
48 {
49 	isl_ctx *ctx;
50 	isl_restriction *restr;
51 
52 	if (!source_map)
53 		return NULL;
54 
55 	ctx = isl_map_get_ctx(source_map);
56 	restr = isl_calloc_type(ctx, struct isl_restriction);
57 	if (!restr)
58 		goto error;
59 
60 	restr->type = type;
61 
62 	isl_map_free(source_map);
63 	return restr;
64 error:
65 	isl_map_free(source_map);
66 	return NULL;
67 }
68 
69 /* Create a restriction that doesn't restrict anything.
70  */
isl_restriction_none(__isl_take isl_map * source_map)71 __isl_give isl_restriction *isl_restriction_none(__isl_take isl_map *source_map)
72 {
73 	return isl_restriction_alloc(source_map, isl_restriction_type_none);
74 }
75 
76 /* Create a restriction that removes everything.
77  */
isl_restriction_empty(__isl_take isl_map * source_map)78 __isl_give isl_restriction *isl_restriction_empty(
79 	__isl_take isl_map *source_map)
80 {
81 	return isl_restriction_alloc(source_map, isl_restriction_type_empty);
82 }
83 
84 /* Create a restriction on the input of the maximization problem
85  * based on the given source and sink restrictions.
86  */
isl_restriction_input(__isl_take isl_set * source_restr,__isl_take isl_set * sink_restr)87 __isl_give isl_restriction *isl_restriction_input(
88 	__isl_take isl_set *source_restr, __isl_take isl_set *sink_restr)
89 {
90 	isl_ctx *ctx;
91 	isl_restriction *restr;
92 
93 	if (!source_restr || !sink_restr)
94 		goto error;
95 
96 	ctx = isl_set_get_ctx(source_restr);
97 	restr = isl_calloc_type(ctx, struct isl_restriction);
98 	if (!restr)
99 		goto error;
100 
101 	restr->type = isl_restriction_type_input;
102 	restr->source = source_restr;
103 	restr->sink = sink_restr;
104 
105 	return restr;
106 error:
107 	isl_set_free(source_restr);
108 	isl_set_free(sink_restr);
109 	return NULL;
110 }
111 
112 /* Create a restriction on the output of the maximization problem
113  * based on the given source restriction.
114  */
isl_restriction_output(__isl_take isl_set * source_restr)115 __isl_give isl_restriction *isl_restriction_output(
116 	__isl_take isl_set *source_restr)
117 {
118 	isl_ctx *ctx;
119 	isl_restriction *restr;
120 
121 	if (!source_restr)
122 		return NULL;
123 
124 	ctx = isl_set_get_ctx(source_restr);
125 	restr = isl_calloc_type(ctx, struct isl_restriction);
126 	if (!restr)
127 		goto error;
128 
129 	restr->type = isl_restriction_type_output;
130 	restr->source = source_restr;
131 
132 	return restr;
133 error:
134 	isl_set_free(source_restr);
135 	return NULL;
136 }
137 
isl_restriction_free(__isl_take isl_restriction * restr)138 __isl_null isl_restriction *isl_restriction_free(
139 	__isl_take isl_restriction *restr)
140 {
141 	if (!restr)
142 		return NULL;
143 
144 	isl_set_free(restr->source);
145 	isl_set_free(restr->sink);
146 	free(restr);
147 	return NULL;
148 }
149 
isl_restriction_get_ctx(__isl_keep isl_restriction * restr)150 isl_ctx *isl_restriction_get_ctx(__isl_keep isl_restriction *restr)
151 {
152 	return restr ? isl_set_get_ctx(restr->source) : NULL;
153 }
154 
155 /* A private structure to keep track of a mapping together with
156  * a user-specified identifier and a boolean indicating whether
157  * the map represents a must or may access/dependence.
158  */
159 struct isl_labeled_map {
160 	struct isl_map	*map;
161 	void		*data;
162 	int		must;
163 };
164 
165 typedef isl_bool (*isl_access_coscheduled)(void *first, void *second);
166 
167 /* A structure containing the input for dependence analysis:
168  * - a sink
169  * - n_must + n_may (<= max_source) sources
170  * - a function for determining the relative order of sources and sink
171  * - an optional function "coscheduled" for determining whether sources
172  *   may be coscheduled.  If "coscheduled" is NULL, then the sources
173  *   are assumed not to be coscheduled.
174  * The must sources are placed before the may sources.
175  *
176  * domain_map is an auxiliary map that maps the sink access relation
177  * to the domain of this access relation.
178  * This field is only needed when restrict_fn is set and
179  * the field itself is set by isl_access_info_compute_flow.
180  *
181  * restrict_fn is a callback that (if not NULL) will be called
182  * right before any lexicographical maximization.
183  */
184 struct isl_access_info {
185 	isl_map				*domain_map;
186 	struct isl_labeled_map		sink;
187 	isl_access_level_before		level_before;
188 	isl_access_coscheduled		coscheduled;
189 
190 	isl_access_restrict		restrict_fn;
191 	void				*restrict_user;
192 
193 	int		    		max_source;
194 	int		    		n_must;
195 	int		    		n_may;
196 	struct isl_labeled_map		source[1];
197 };
198 
199 /* A structure containing the output of dependence analysis:
200  * - n_source dependences
201  * - a wrapped subset of the sink for which definitely no source could be found
202  * - a wrapped subset of the sink for which possibly no source could be found
203  */
204 struct isl_flow {
205 	isl_set			*must_no_source;
206 	isl_set			*may_no_source;
207 	int			n_source;
208 	struct isl_labeled_map	*dep;
209 };
210 
211 /* Construct an isl_access_info structure and fill it up with
212  * the given data.  The number of sources is set to 0.
213  */
isl_access_info_alloc(__isl_take isl_map * sink,void * sink_user,isl_access_level_before fn,int max_source)214 __isl_give isl_access_info *isl_access_info_alloc(__isl_take isl_map *sink,
215 	void *sink_user, isl_access_level_before fn, int max_source)
216 {
217 	isl_ctx *ctx;
218 	struct isl_access_info *acc;
219 
220 	if (!sink)
221 		return NULL;
222 
223 	ctx = isl_map_get_ctx(sink);
224 	isl_assert(ctx, max_source >= 0, goto error);
225 
226 	acc = isl_calloc(ctx, struct isl_access_info,
227 			sizeof(struct isl_access_info) +
228 			(max_source - 1) * sizeof(struct isl_labeled_map));
229 	if (!acc)
230 		goto error;
231 
232 	acc->sink.map = sink;
233 	acc->sink.data = sink_user;
234 	acc->level_before = fn;
235 	acc->max_source = max_source;
236 	acc->n_must = 0;
237 	acc->n_may = 0;
238 
239 	return acc;
240 error:
241 	isl_map_free(sink);
242 	return NULL;
243 }
244 
245 /* Free the given isl_access_info structure.
246  */
isl_access_info_free(__isl_take isl_access_info * acc)247 __isl_null isl_access_info *isl_access_info_free(
248 	__isl_take isl_access_info *acc)
249 {
250 	int i;
251 
252 	if (!acc)
253 		return NULL;
254 	isl_map_free(acc->domain_map);
255 	isl_map_free(acc->sink.map);
256 	for (i = 0; i < acc->n_must + acc->n_may; ++i)
257 		isl_map_free(acc->source[i].map);
258 	free(acc);
259 	return NULL;
260 }
261 
isl_access_info_get_ctx(__isl_keep isl_access_info * acc)262 isl_ctx *isl_access_info_get_ctx(__isl_keep isl_access_info *acc)
263 {
264 	return acc ? isl_map_get_ctx(acc->sink.map) : NULL;
265 }
266 
isl_access_info_set_restrict(__isl_take isl_access_info * acc,isl_access_restrict fn,void * user)267 __isl_give isl_access_info *isl_access_info_set_restrict(
268 	__isl_take isl_access_info *acc, isl_access_restrict fn, void *user)
269 {
270 	if (!acc)
271 		return NULL;
272 	acc->restrict_fn = fn;
273 	acc->restrict_user = user;
274 	return acc;
275 }
276 
277 /* Add another source to an isl_access_info structure, making
278  * sure the "must" sources are placed before the "may" sources.
279  * This function may be called at most max_source times on a
280  * given isl_access_info structure, with max_source as specified
281  * in the call to isl_access_info_alloc that constructed the structure.
282  */
isl_access_info_add_source(__isl_take isl_access_info * acc,__isl_take isl_map * source,int must,void * source_user)283 __isl_give isl_access_info *isl_access_info_add_source(
284 	__isl_take isl_access_info *acc, __isl_take isl_map *source,
285 	int must, void *source_user)
286 {
287 	isl_ctx *ctx;
288 
289 	if (!acc)
290 		goto error;
291 	ctx = isl_map_get_ctx(acc->sink.map);
292 	isl_assert(ctx, acc->n_must + acc->n_may < acc->max_source, goto error);
293 
294 	if (must) {
295 		if (acc->n_may)
296 			acc->source[acc->n_must + acc->n_may] =
297 				acc->source[acc->n_must];
298 		acc->source[acc->n_must].map = source;
299 		acc->source[acc->n_must].data = source_user;
300 		acc->source[acc->n_must].must = 1;
301 		acc->n_must++;
302 	} else {
303 		acc->source[acc->n_must + acc->n_may].map = source;
304 		acc->source[acc->n_must + acc->n_may].data = source_user;
305 		acc->source[acc->n_must + acc->n_may].must = 0;
306 		acc->n_may++;
307 	}
308 
309 	return acc;
310 error:
311 	isl_map_free(source);
312 	isl_access_info_free(acc);
313 	return NULL;
314 }
315 
316 /* A helper struct carrying the isl_access_info and an error condition.
317  */
318 struct access_sort_info {
319 	isl_access_info *access_info;
320 	int error;
321 };
322 
323 /* Return -n, 0 or n (with n a positive value), depending on whether
324  * the source access identified by p1 should be sorted before, together
325  * or after that identified by p2.
326  *
327  * If p1 appears before p2, then it should be sorted first.
328  * For more generic initial schedules, it is possible that neither
329  * p1 nor p2 appears before the other, or at least not in any obvious way.
330  * We therefore also check if p2 appears before p1, in which case p2
331  * should be sorted first.
332  * If not, we try to order the two statements based on the description
333  * of the iteration domains.  This results in an arbitrary, but fairly
334  * stable ordering.
335  *
336  * In case of an error, sort_info.error is set to true and all elements are
337  * reported to be equal.
338  */
access_sort_cmp(const void * p1,const void * p2,void * user)339 static int access_sort_cmp(const void *p1, const void *p2, void *user)
340 {
341 	struct access_sort_info *sort_info = user;
342 	isl_access_info *acc = sort_info->access_info;
343 
344 	if (sort_info->error)
345 		return 0;
346 
347 	const struct isl_labeled_map *i1, *i2;
348 	int level1, level2;
349 	uint32_t h1, h2;
350 	i1 = (const struct isl_labeled_map *) p1;
351 	i2 = (const struct isl_labeled_map *) p2;
352 
353 	level1 = acc->level_before(i1->data, i2->data);
354 	if (level1 < 0)
355 		goto error;
356 	if (level1 % 2)
357 		return -1;
358 
359 	level2 = acc->level_before(i2->data, i1->data);
360 	if (level2 < 0)
361 		goto error;
362 	if (level2 % 2)
363 		return 1;
364 
365 	h1 = isl_map_get_hash(i1->map);
366 	h2 = isl_map_get_hash(i2->map);
367 	return h1 > h2 ? 1 : h1 < h2 ? -1 : 0;
368 error:
369 	sort_info->error = 1;
370 	return 0;
371 }
372 
373 /* Sort the must source accesses in their textual order.
374  */
isl_access_info_sort_sources(__isl_take isl_access_info * acc)375 static __isl_give isl_access_info *isl_access_info_sort_sources(
376 	__isl_take isl_access_info *acc)
377 {
378 	struct access_sort_info sort_info;
379 
380 	sort_info.access_info = acc;
381 	sort_info.error = 0;
382 
383 	if (!acc)
384 		return NULL;
385 	if (acc->n_must <= 1)
386 		return acc;
387 
388 	if (isl_sort(acc->source, acc->n_must, sizeof(struct isl_labeled_map),
389 		    access_sort_cmp, &sort_info) < 0)
390 		return isl_access_info_free(acc);
391 	if (sort_info.error)
392 		return isl_access_info_free(acc);
393 
394 	return acc;
395 }
396 
397 /* Align the parameters of the two spaces if needed and then call
398  * isl_space_join.
399  */
space_align_and_join(__isl_take isl_space * left,__isl_take isl_space * right)400 static __isl_give isl_space *space_align_and_join(__isl_take isl_space *left,
401 	__isl_take isl_space *right)
402 {
403 	isl_bool equal_params;
404 
405 	equal_params = isl_space_has_equal_params(left, right);
406 	if (equal_params < 0)
407 		goto error;
408 	if (equal_params)
409 		return isl_space_join(left, right);
410 
411 	left = isl_space_align_params(left, isl_space_copy(right));
412 	right = isl_space_align_params(right, isl_space_copy(left));
413 	return isl_space_join(left, right);
414 error:
415 	isl_space_free(left);
416 	isl_space_free(right);
417 	return NULL;
418 }
419 
420 /* Initialize an empty isl_flow structure corresponding to a given
421  * isl_access_info structure.
422  * For each must access, two dependences are created (initialized
423  * to the empty relation), one for the resulting must dependences
424  * and one for the resulting may dependences.  May accesses can
425  * only lead to may dependences, so only one dependence is created
426  * for each of them.
427  * This function is private as isl_flow structures are only supposed
428  * to be created by isl_access_info_compute_flow.
429  */
isl_flow_alloc(__isl_keep isl_access_info * acc)430 static __isl_give isl_flow *isl_flow_alloc(__isl_keep isl_access_info *acc)
431 {
432 	int i, n;
433 	struct isl_ctx *ctx;
434 	struct isl_flow *dep;
435 
436 	if (!acc)
437 		return NULL;
438 
439 	ctx = isl_map_get_ctx(acc->sink.map);
440 	dep = isl_calloc_type(ctx, struct isl_flow);
441 	if (!dep)
442 		return NULL;
443 
444 	n = 2 * acc->n_must + acc->n_may;
445 	dep->dep = isl_calloc_array(ctx, struct isl_labeled_map, n);
446 	if (n && !dep->dep)
447 		goto error;
448 
449 	dep->n_source = n;
450 	for (i = 0; i < acc->n_must; ++i) {
451 		isl_space *space;
452 		space = space_align_and_join(
453 			isl_map_get_space(acc->source[i].map),
454 			isl_space_reverse(isl_map_get_space(acc->sink.map)));
455 		dep->dep[2 * i].map = isl_map_empty(space);
456 		dep->dep[2 * i + 1].map = isl_map_copy(dep->dep[2 * i].map);
457 		dep->dep[2 * i].data = acc->source[i].data;
458 		dep->dep[2 * i + 1].data = acc->source[i].data;
459 		dep->dep[2 * i].must = 1;
460 		dep->dep[2 * i + 1].must = 0;
461 		if (!dep->dep[2 * i].map || !dep->dep[2 * i + 1].map)
462 			goto error;
463 	}
464 	for (i = acc->n_must; i < acc->n_must + acc->n_may; ++i) {
465 		isl_space *space;
466 		space = space_align_and_join(
467 			isl_map_get_space(acc->source[i].map),
468 			isl_space_reverse(isl_map_get_space(acc->sink.map)));
469 		dep->dep[acc->n_must + i].map = isl_map_empty(space);
470 		dep->dep[acc->n_must + i].data = acc->source[i].data;
471 		dep->dep[acc->n_must + i].must = 0;
472 		if (!dep->dep[acc->n_must + i].map)
473 			goto error;
474 	}
475 
476 	return dep;
477 error:
478 	isl_flow_free(dep);
479 	return NULL;
480 }
481 
482 /* Iterate over all sources and for each resulting flow dependence
483  * that is not empty, call the user specfied function.
484  * The second argument in this function call identifies the source,
485  * while the third argument correspond to the final argument of
486  * the isl_flow_foreach call.
487  */
isl_flow_foreach(__isl_keep isl_flow * deps,isl_stat (* fn)(__isl_take isl_map * dep,int must,void * dep_user,void * user),void * user)488 isl_stat isl_flow_foreach(__isl_keep isl_flow *deps,
489 	isl_stat (*fn)(__isl_take isl_map *dep, int must, void *dep_user,
490 		void *user),
491 	void *user)
492 {
493 	int i;
494 
495 	if (!deps)
496 		return isl_stat_error;
497 
498 	for (i = 0; i < deps->n_source; ++i) {
499 		if (isl_map_plain_is_empty(deps->dep[i].map))
500 			continue;
501 		if (fn(isl_map_copy(deps->dep[i].map), deps->dep[i].must,
502 				deps->dep[i].data, user) < 0)
503 			return isl_stat_error;
504 	}
505 
506 	return isl_stat_ok;
507 }
508 
509 /* Return a copy of the subset of the sink for which no source could be found.
510  */
isl_flow_get_no_source(__isl_keep isl_flow * deps,int must)511 __isl_give isl_map *isl_flow_get_no_source(__isl_keep isl_flow *deps, int must)
512 {
513 	if (!deps)
514 		return NULL;
515 
516 	if (must)
517 		return isl_set_unwrap(isl_set_copy(deps->must_no_source));
518 	else
519 		return isl_set_unwrap(isl_set_copy(deps->may_no_source));
520 }
521 
isl_flow_free(__isl_take isl_flow * deps)522 __isl_null isl_flow *isl_flow_free(__isl_take isl_flow *deps)
523 {
524 	int i;
525 
526 	if (!deps)
527 		return NULL;
528 	isl_set_free(deps->must_no_source);
529 	isl_set_free(deps->may_no_source);
530 	if (deps->dep) {
531 		for (i = 0; i < deps->n_source; ++i)
532 			isl_map_free(deps->dep[i].map);
533 		free(deps->dep);
534 	}
535 	free(deps);
536 
537 	return NULL;
538 }
539 
isl_flow_get_ctx(__isl_keep isl_flow * deps)540 isl_ctx *isl_flow_get_ctx(__isl_keep isl_flow *deps)
541 {
542 	return deps ? isl_set_get_ctx(deps->must_no_source) : NULL;
543 }
544 
545 /* Return a map that enforces that the domain iteration occurs after
546  * the range iteration at the given level.
547  * If level is odd, then the domain iteration should occur after
548  * the target iteration in their shared level/2 outermost loops.
549  * In this case we simply need to enforce that these outermost
550  * loop iterations are the same.
551  * If level is even, then the loop iterator of the domain should
552  * be greater than the loop iterator of the range at the last
553  * of the level/2 shared loops, i.e., loop level/2 - 1.
554  */
after_at_level(__isl_take isl_space * space,int level)555 static __isl_give isl_map *after_at_level(__isl_take isl_space *space,
556 	int level)
557 {
558 	struct isl_basic_map *bmap;
559 
560 	if (level % 2)
561 		bmap = isl_basic_map_equal(space, level/2);
562 	else
563 		bmap = isl_basic_map_more_at(space, level/2 - 1);
564 
565 	return isl_map_from_basic_map(bmap);
566 }
567 
568 /* Compute the partial lexicographic maximum of "dep" on domain "sink",
569  * but first check if the user has set acc->restrict_fn and if so
570  * update either the input or the output of the maximization problem
571  * with respect to the resulting restriction.
572  *
573  * Since the user expects a mapping from sink iterations to source iterations,
574  * whereas the domain of "dep" is a wrapped map, mapping sink iterations
575  * to accessed array elements, we first need to project out the accessed
576  * sink array elements by applying acc->domain_map.
577  * Similarly, the sink restriction specified by the user needs to be
578  * converted back to the wrapped map.
579  */
restricted_partial_lexmax(__isl_keep isl_access_info * acc,__isl_take isl_map * dep,int source,__isl_take isl_set * sink,__isl_give isl_set ** empty)580 static __isl_give isl_map *restricted_partial_lexmax(
581 	__isl_keep isl_access_info *acc, __isl_take isl_map *dep,
582 	int source, __isl_take isl_set *sink, __isl_give isl_set **empty)
583 {
584 	isl_map *source_map;
585 	isl_restriction *restr;
586 	isl_set *sink_domain;
587 	isl_set *sink_restr;
588 	isl_map *res;
589 
590 	if (!acc->restrict_fn)
591 		return isl_map_partial_lexmax(dep, sink, empty);
592 
593 	source_map = isl_map_copy(dep);
594 	source_map = isl_map_apply_domain(source_map,
595 					    isl_map_copy(acc->domain_map));
596 	sink_domain = isl_set_copy(sink);
597 	sink_domain = isl_set_apply(sink_domain, isl_map_copy(acc->domain_map));
598 	restr = acc->restrict_fn(source_map, sink_domain,
599 				acc->source[source].data, acc->restrict_user);
600 	isl_set_free(sink_domain);
601 	isl_map_free(source_map);
602 
603 	if (!restr)
604 		goto error;
605 	if (restr->type == isl_restriction_type_input) {
606 		dep = isl_map_intersect_range(dep, isl_set_copy(restr->source));
607 		sink_restr = isl_set_copy(restr->sink);
608 		sink_restr = isl_set_apply(sink_restr,
609 				isl_map_reverse(isl_map_copy(acc->domain_map)));
610 		sink = isl_set_intersect(sink, sink_restr);
611 	} else if (restr->type == isl_restriction_type_empty) {
612 		isl_space *space = isl_map_get_space(dep);
613 		isl_map_free(dep);
614 		dep = isl_map_empty(space);
615 	}
616 
617 	res = isl_map_partial_lexmax(dep, sink, empty);
618 
619 	if (restr->type == isl_restriction_type_output)
620 		res = isl_map_intersect_range(res, isl_set_copy(restr->source));
621 
622 	isl_restriction_free(restr);
623 	return res;
624 error:
625 	isl_map_free(dep);
626 	isl_set_free(sink);
627 	*empty = NULL;
628 	return NULL;
629 }
630 
631 /* Compute the last iteration of must source j that precedes the sink
632  * at the given level for sink iterations in set_C.
633  * The subset of set_C for which no such iteration can be found is returned
634  * in *empty.
635  */
last_source(struct isl_access_info * acc,struct isl_set * set_C,int j,int level,struct isl_set ** empty)636 static struct isl_map *last_source(struct isl_access_info *acc,
637 				    struct isl_set *set_C,
638 				    int j, int level, struct isl_set **empty)
639 {
640 	struct isl_map *read_map;
641 	struct isl_map *write_map;
642 	struct isl_map *dep_map;
643 	struct isl_map *after;
644 	struct isl_map *result;
645 
646 	read_map = isl_map_copy(acc->sink.map);
647 	write_map = isl_map_copy(acc->source[j].map);
648 	write_map = isl_map_reverse(write_map);
649 	dep_map = isl_map_apply_range(read_map, write_map);
650 	after = after_at_level(isl_map_get_space(dep_map), level);
651 	dep_map = isl_map_intersect(dep_map, after);
652 	result = restricted_partial_lexmax(acc, dep_map, j, set_C, empty);
653 	result = isl_map_reverse(result);
654 
655 	return result;
656 }
657 
658 /* For a given mapping between iterations of must source j and iterations
659  * of the sink, compute the last iteration of must source k preceding
660  * the sink at level before_level for any of the sink iterations,
661  * but following the corresponding iteration of must source j at level
662  * after_level.
663  */
last_later_source(struct isl_access_info * acc,struct isl_map * old_map,int j,int before_level,int k,int after_level,struct isl_set ** empty)664 static struct isl_map *last_later_source(struct isl_access_info *acc,
665 					 struct isl_map *old_map,
666 					 int j, int before_level,
667 					 int k, int after_level,
668 					 struct isl_set **empty)
669 {
670 	isl_space *space;
671 	struct isl_set *set_C;
672 	struct isl_map *read_map;
673 	struct isl_map *write_map;
674 	struct isl_map *dep_map;
675 	struct isl_map *after_write;
676 	struct isl_map *before_read;
677 	struct isl_map *result;
678 
679 	set_C = isl_map_range(isl_map_copy(old_map));
680 	read_map = isl_map_copy(acc->sink.map);
681 	write_map = isl_map_copy(acc->source[k].map);
682 
683 	write_map = isl_map_reverse(write_map);
684 	dep_map = isl_map_apply_range(read_map, write_map);
685 	space = space_align_and_join(isl_map_get_space(acc->source[k].map),
686 		    isl_space_reverse(isl_map_get_space(acc->source[j].map)));
687 	after_write = after_at_level(space, after_level);
688 	after_write = isl_map_apply_range(after_write, old_map);
689 	after_write = isl_map_reverse(after_write);
690 	dep_map = isl_map_intersect(dep_map, after_write);
691 	before_read = after_at_level(isl_map_get_space(dep_map), before_level);
692 	dep_map = isl_map_intersect(dep_map, before_read);
693 	result = restricted_partial_lexmax(acc, dep_map, k, set_C, empty);
694 	result = isl_map_reverse(result);
695 
696 	return result;
697 }
698 
699 /* Given a shared_level between two accesses, return 1 if the
700  * the first can precede the second at the requested target_level.
701  * If the target level is odd, i.e., refers to a statement level
702  * dimension, then first needs to precede second at the requested
703  * level, i.e., shared_level must be equal to target_level.
704  * If the target level is odd, then the two loops should share
705  * at least the requested number of outer loops.
706  */
can_precede_at_level(int shared_level,int target_level)707 static int can_precede_at_level(int shared_level, int target_level)
708 {
709 	if (shared_level < target_level)
710 		return 0;
711 	if ((target_level % 2) && shared_level > target_level)
712 		return 0;
713 	return 1;
714 }
715 
716 /* Given a possible flow dependence temp_rel[j] between source j and the sink
717  * at level sink_level, remove those elements for which
718  * there is an iteration of another source k < j that is closer to the sink.
719  * The flow dependences temp_rel[k] are updated with the improved sources.
720  * Any improved source needs to precede the sink at the same level
721  * and needs to follow source j at the same or a deeper level.
722  * The lower this level, the later the execution date of source k.
723  * We therefore consider lower levels first.
724  *
725  * If temp_rel[j] is empty, then there can be no improvement and
726  * we return immediately.
727  *
728  * This function returns isl_stat_ok in case it was executed successfully and
729  * isl_stat_error in case of errors during the execution of this function.
730  */
intermediate_sources(__isl_keep isl_access_info * acc,struct isl_map ** temp_rel,int j,int sink_level)731 static isl_stat intermediate_sources(__isl_keep isl_access_info *acc,
732 	struct isl_map **temp_rel, int j, int sink_level)
733 {
734 	int k, level;
735 	isl_size n_in = isl_map_dim(acc->source[j].map, isl_dim_in);
736 	int depth = 2 * n_in + 1;
737 
738 	if (n_in < 0)
739 		return isl_stat_error;
740 	if (isl_map_plain_is_empty(temp_rel[j]))
741 		return isl_stat_ok;
742 
743 	for (k = j - 1; k >= 0; --k) {
744 		int plevel, plevel2;
745 		plevel = acc->level_before(acc->source[k].data, acc->sink.data);
746 		if (plevel < 0)
747 			return isl_stat_error;
748 		if (!can_precede_at_level(plevel, sink_level))
749 			continue;
750 
751 		plevel2 = acc->level_before(acc->source[j].data,
752 						acc->source[k].data);
753 		if (plevel2 < 0)
754 			return isl_stat_error;
755 
756 		for (level = sink_level; level <= depth; ++level) {
757 			struct isl_map *T;
758 			struct isl_set *trest;
759 			struct isl_map *copy;
760 
761 			if (!can_precede_at_level(plevel2, level))
762 				continue;
763 
764 			copy = isl_map_copy(temp_rel[j]);
765 			T = last_later_source(acc, copy, j, sink_level, k,
766 					      level, &trest);
767 			if (isl_map_plain_is_empty(T)) {
768 				isl_set_free(trest);
769 				isl_map_free(T);
770 				continue;
771 			}
772 			temp_rel[j] = isl_map_intersect_range(temp_rel[j], trest);
773 			temp_rel[k] = isl_map_union_disjoint(temp_rel[k], T);
774 		}
775 	}
776 
777 	return isl_stat_ok;
778 }
779 
780 /* Compute all iterations of may source j that precedes the sink at the given
781  * level for sink iterations in set_C.
782  */
all_sources(__isl_keep isl_access_info * acc,__isl_take isl_set * set_C,int j,int level)783 static __isl_give isl_map *all_sources(__isl_keep isl_access_info *acc,
784 				    __isl_take isl_set *set_C, int j, int level)
785 {
786 	isl_map *read_map;
787 	isl_map *write_map;
788 	isl_map *dep_map;
789 	isl_map *after;
790 
791 	read_map = isl_map_copy(acc->sink.map);
792 	read_map = isl_map_intersect_domain(read_map, set_C);
793 	write_map = isl_map_copy(acc->source[acc->n_must + j].map);
794 	write_map = isl_map_reverse(write_map);
795 	dep_map = isl_map_apply_range(read_map, write_map);
796 	after = after_at_level(isl_map_get_space(dep_map), level);
797 	dep_map = isl_map_intersect(dep_map, after);
798 
799 	return isl_map_reverse(dep_map);
800 }
801 
802 /* For a given mapping between iterations of must source k and iterations
803  * of the sink, compute all iterations of may source j preceding
804  * the sink at level before_level for any of the sink iterations,
805  * but following the corresponding iteration of must source k at level
806  * after_level.
807  */
all_later_sources(__isl_keep isl_access_info * acc,__isl_take isl_map * old_map,int j,int before_level,int k,int after_level)808 static __isl_give isl_map *all_later_sources(__isl_keep isl_access_info *acc,
809 	__isl_take isl_map *old_map,
810 	int j, int before_level, int k, int after_level)
811 {
812 	isl_space *space;
813 	isl_set *set_C;
814 	isl_map *read_map;
815 	isl_map *write_map;
816 	isl_map *dep_map;
817 	isl_map *after_write;
818 	isl_map *before_read;
819 
820 	set_C = isl_map_range(isl_map_copy(old_map));
821 	read_map = isl_map_copy(acc->sink.map);
822 	read_map = isl_map_intersect_domain(read_map, set_C);
823 	write_map = isl_map_copy(acc->source[acc->n_must + j].map);
824 
825 	write_map = isl_map_reverse(write_map);
826 	dep_map = isl_map_apply_range(read_map, write_map);
827 	space = isl_space_join(isl_map_get_space(
828 		    acc->source[acc->n_must + j].map),
829 		    isl_space_reverse(isl_map_get_space(acc->source[k].map)));
830 	after_write = after_at_level(space, after_level);
831 	after_write = isl_map_apply_range(after_write, old_map);
832 	after_write = isl_map_reverse(after_write);
833 	dep_map = isl_map_intersect(dep_map, after_write);
834 	before_read = after_at_level(isl_map_get_space(dep_map), before_level);
835 	dep_map = isl_map_intersect(dep_map, before_read);
836 	return isl_map_reverse(dep_map);
837 }
838 
839 /* Given the must and may dependence relations for the must accesses
840  * for level sink_level, check if there are any accesses of may access j
841  * that occur in between and return their union.
842  * If some of these accesses are intermediate with respect to
843  * (previously thought to be) must dependences, then these
844  * must dependences are turned into may dependences.
845  */
all_intermediate_sources(__isl_keep isl_access_info * acc,__isl_take isl_map * map,struct isl_map ** must_rel,struct isl_map ** may_rel,int j,int sink_level)846 static __isl_give isl_map *all_intermediate_sources(
847 	__isl_keep isl_access_info *acc, __isl_take isl_map *map,
848 	struct isl_map **must_rel, struct isl_map **may_rel,
849 	int j, int sink_level)
850 {
851 	int k, level;
852 	isl_size n_in = isl_map_dim(acc->source[acc->n_must + j].map,
853 				    isl_dim_in);
854 	int depth = 2 * n_in + 1;
855 
856 	if (n_in < 0)
857 		return isl_map_free(map);
858 	for (k = 0; k < acc->n_must; ++k) {
859 		int plevel;
860 
861 		if (isl_map_plain_is_empty(may_rel[k]) &&
862 		    isl_map_plain_is_empty(must_rel[k]))
863 			continue;
864 
865 		plevel = acc->level_before(acc->source[k].data,
866 					acc->source[acc->n_must + j].data);
867 		if (plevel < 0)
868 			return isl_map_free(map);
869 
870 		for (level = sink_level; level <= depth; ++level) {
871 			isl_map *T;
872 			isl_map *copy;
873 			isl_set *ran;
874 
875 			if (!can_precede_at_level(plevel, level))
876 				continue;
877 
878 			copy = isl_map_copy(may_rel[k]);
879 			T = all_later_sources(acc, copy, j, sink_level, k, level);
880 			map = isl_map_union(map, T);
881 
882 			copy = isl_map_copy(must_rel[k]);
883 			T = all_later_sources(acc, copy, j, sink_level, k, level);
884 			ran = isl_map_range(isl_map_copy(T));
885 			map = isl_map_union(map, T);
886 			may_rel[k] = isl_map_union_disjoint(may_rel[k],
887 			    isl_map_intersect_range(isl_map_copy(must_rel[k]),
888 						    isl_set_copy(ran)));
889 			T = isl_map_from_domain_and_range(
890 			    isl_set_universe(
891 				isl_space_domain(isl_map_get_space(must_rel[k]))),
892 			    ran);
893 			must_rel[k] = isl_map_subtract(must_rel[k], T);
894 		}
895 	}
896 
897 	return map;
898 }
899 
900 /* Given a dependence relation "old_map" between a must-source and the sink,
901  * return a subset of the dependences, augmented with instances
902  * of the source at position "pos" in "acc" that are coscheduled
903  * with the must-source and that access the same element.
904  * That is, if the input lives in a space T -> K, then the output
905  * lives in the space [T -> S] -> K, with S the space of source "pos", and
906  * the domain factor of the domain product is a subset of the input.
907  * The sources are considered to be coscheduled if they have the same values
908  * for the initial "depth" coordinates.
909  *
910  * First construct a dependence relation S -> K and a mapping
911  * between coscheduled sources T -> S.
912  * The second is combined with the original dependence relation T -> K
913  * to form a relation in T -> [S -> K], which is subsequently
914  * uncurried to [T -> S] -> K.
915  * This result is then intersected with the dependence relation S -> K
916  * to form the output.
917  *
918  * In case a negative depth is given, NULL is returned to indicate an error.
919  */
coscheduled_source(__isl_keep isl_access_info * acc,__isl_keep isl_map * old_map,int pos,int depth)920 static __isl_give isl_map *coscheduled_source(__isl_keep isl_access_info *acc,
921 	__isl_keep isl_map *old_map, int pos, int depth)
922 {
923 	isl_space *space;
924 	isl_set *set_C;
925 	isl_map *read_map;
926 	isl_map *write_map;
927 	isl_map *dep_map;
928 	isl_map *equal;
929 	isl_map *map;
930 
931 	if (depth < 0)
932 		return NULL;
933 
934 	set_C = isl_map_range(isl_map_copy(old_map));
935 	read_map = isl_map_copy(acc->sink.map);
936 	read_map = isl_map_intersect_domain(read_map, set_C);
937 	write_map = isl_map_copy(acc->source[pos].map);
938 	dep_map = isl_map_domain_product(write_map, read_map);
939 	dep_map = isl_set_unwrap(isl_map_domain(dep_map));
940 	space = isl_space_join(isl_map_get_space(old_map),
941 				isl_space_reverse(isl_map_get_space(dep_map)));
942 	equal = isl_map_from_basic_map(isl_basic_map_equal(space, depth));
943 	map = isl_map_range_product(equal, isl_map_copy(old_map));
944 	map = isl_map_uncurry(map);
945 	map = isl_map_intersect_domain_factor_range(map, dep_map);
946 
947 	return map;
948 }
949 
950 /* After the dependences derived from a must-source have been computed
951  * at a certain level, check if any of the sources of the must-dependences
952  * may be coscheduled with other sources.
953  * If they are any such sources, then there is no way of determining
954  * which of the sources actually comes last and the must-dependences
955  * need to be turned into may-dependences, while dependences from
956  * the other sources need to be added to the may-dependences as well.
957  * "acc" describes the sources and a callback for checking whether
958  * two sources may be coscheduled.  If acc->coscheduled is NULL then
959  * the sources are assumed not to be coscheduled.
960  * "must_rel" and "may_rel" describe the must and may-dependence relations
961  * computed at the current level for the must-sources.  Some of the dependences
962  * may be moved from "must_rel" to "may_rel".
963  * "flow" contains all dependences computed so far (apart from those
964  * in "must_rel" and "may_rel") and may be updated with additional
965  * dependences derived from may-sources.
966  *
967  * In particular, consider all the must-sources with a non-empty
968  * dependence relation in "must_rel".  They are considered in reverse
969  * order because that is the order in which they are considered in the caller.
970  * If any of the must-sources are coscheduled, then the last one
971  * is the one that will have a corresponding dependence relation.
972  * For each must-source i, consider both all the previous must-sources
973  * and all the may-sources.  If any of those may be coscheduled with
974  * must-source i, then compute the coscheduled instances that access
975  * the same memory elements.  The result is a relation [T -> S] -> K.
976  * The projection onto T -> K is a subset of the must-dependence relation
977  * that needs to be turned into may-dependences.
978  * The projection onto S -> K needs to be added to the may-dependences
979  * of source S.
980  * Since a given must-source instance may be coscheduled with several
981  * other source instances, the dependences that need to be turned
982  * into may-dependences are first collected and only actually removed
983  * from the must-dependences after all other sources have been considered.
984  */
handle_coscheduled(__isl_keep isl_access_info * acc,__isl_keep isl_map ** must_rel,__isl_keep isl_map ** may_rel,__isl_take isl_flow * flow)985 static __isl_give isl_flow *handle_coscheduled(__isl_keep isl_access_info *acc,
986 	__isl_keep isl_map **must_rel, __isl_keep isl_map **may_rel,
987 	__isl_take isl_flow *flow)
988 {
989 	int i, j;
990 
991 	if (!acc->coscheduled)
992 		return flow;
993 	for (i = acc->n_must - 1; i >= 0; --i) {
994 		isl_map *move;
995 
996 		if (isl_map_plain_is_empty(must_rel[i]))
997 			continue;
998 		move = isl_map_empty(isl_map_get_space(must_rel[i]));
999 		for (j = i - 1; j >= 0; --j) {
1000 			int depth;
1001 			isl_bool coscheduled;
1002 			isl_map *map, *factor;
1003 
1004 			coscheduled = acc->coscheduled(acc->source[i].data,
1005 							acc->source[j].data);
1006 			if (coscheduled < 0) {
1007 				isl_map_free(move);
1008 				return isl_flow_free(flow);
1009 			}
1010 			if (!coscheduled)
1011 				continue;
1012 			depth = acc->level_before(acc->source[i].data,
1013 						acc->source[j].data) / 2;
1014 			map = coscheduled_source(acc, must_rel[i], j, depth);
1015 			factor = isl_map_domain_factor_range(isl_map_copy(map));
1016 			may_rel[j] = isl_map_union(may_rel[j], factor);
1017 			map = isl_map_domain_factor_domain(map);
1018 			move = isl_map_union(move, map);
1019 		}
1020 		for (j = 0; j < acc->n_may; ++j) {
1021 			int depth, pos;
1022 			isl_bool coscheduled;
1023 			isl_map *map, *factor;
1024 
1025 			pos = acc->n_must + j;
1026 			coscheduled = acc->coscheduled(acc->source[i].data,
1027 							acc->source[pos].data);
1028 			if (coscheduled < 0) {
1029 				isl_map_free(move);
1030 				return isl_flow_free(flow);
1031 			}
1032 			if (!coscheduled)
1033 				continue;
1034 			depth = acc->level_before(acc->source[i].data,
1035 						acc->source[pos].data) / 2;
1036 			map = coscheduled_source(acc, must_rel[i], pos, depth);
1037 			factor = isl_map_domain_factor_range(isl_map_copy(map));
1038 			pos = 2 * acc->n_must + j;
1039 			flow->dep[pos].map = isl_map_union(flow->dep[pos].map,
1040 							    factor);
1041 			map = isl_map_domain_factor_domain(map);
1042 			move = isl_map_union(move, map);
1043 		}
1044 		must_rel[i] = isl_map_subtract(must_rel[i], isl_map_copy(move));
1045 		may_rel[i] = isl_map_union(may_rel[i], move);
1046 	}
1047 
1048 	return flow;
1049 }
1050 
1051 /* Compute dependences for the case where all accesses are "may"
1052  * accesses, which boils down to computing memory based dependences.
1053  * The generic algorithm would also work in this case, but it would
1054  * be overkill to use it.
1055  */
compute_mem_based_dependences(__isl_keep isl_access_info * acc)1056 static __isl_give isl_flow *compute_mem_based_dependences(
1057 	__isl_keep isl_access_info *acc)
1058 {
1059 	int i;
1060 	isl_set *mustdo;
1061 	isl_set *maydo;
1062 	isl_flow *res;
1063 
1064 	res = isl_flow_alloc(acc);
1065 	if (!res)
1066 		return NULL;
1067 
1068 	mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1069 	maydo = isl_set_copy(mustdo);
1070 
1071 	for (i = 0; i < acc->n_may; ++i) {
1072 		int plevel;
1073 		int is_before;
1074 		isl_space *space;
1075 		isl_map *before;
1076 		isl_map *dep;
1077 
1078 		plevel = acc->level_before(acc->source[i].data, acc->sink.data);
1079 		if (plevel < 0)
1080 			goto error;
1081 
1082 		is_before = plevel & 1;
1083 		plevel >>= 1;
1084 
1085 		space = isl_map_get_space(res->dep[i].map);
1086 		if (is_before)
1087 			before = isl_map_lex_le_first(space, plevel);
1088 		else
1089 			before = isl_map_lex_lt_first(space, plevel);
1090 		dep = isl_map_apply_range(isl_map_copy(acc->source[i].map),
1091 			isl_map_reverse(isl_map_copy(acc->sink.map)));
1092 		dep = isl_map_intersect(dep, before);
1093 		mustdo = isl_set_subtract(mustdo,
1094 					    isl_map_range(isl_map_copy(dep)));
1095 		res->dep[i].map = isl_map_union(res->dep[i].map, dep);
1096 	}
1097 
1098 	res->may_no_source = isl_set_subtract(maydo, isl_set_copy(mustdo));
1099 	res->must_no_source = mustdo;
1100 
1101 	return res;
1102 error:
1103 	isl_set_free(mustdo);
1104 	isl_set_free(maydo);
1105 	isl_flow_free(res);
1106 	return NULL;
1107 }
1108 
1109 /* Compute dependences for the case where there is at least one
1110  * "must" access.
1111  *
1112  * The core algorithm considers all levels in which a source may precede
1113  * the sink, where a level may either be a statement level or a loop level.
1114  * The outermost statement level is 1, the first loop level is 2, etc...
1115  * The algorithm basically does the following:
1116  * for all levels l of the read access from innermost to outermost
1117  *	for all sources w that may precede the sink access at that level
1118  *	    compute the last iteration of the source that precedes the sink access
1119  *					    at that level
1120  *	    add result to possible last accesses at level l of source w
1121  *	    for all sources w2 that we haven't considered yet at this level that may
1122  *					    also precede the sink access
1123  *		for all levels l2 of w from l to innermost
1124  *		    for all possible last accesses dep of w at l
1125  *			compute last iteration of w2 between the source and sink
1126  *								of dep
1127  *			add result to possible last accesses at level l of write w2
1128  *			and replace possible last accesses dep by the remainder
1129  *
1130  *
1131  * The above algorithm is applied to the must access.  During the course
1132  * of the algorithm, we keep track of sink iterations that still
1133  * need to be considered.  These iterations are split into those that
1134  * haven't been matched to any source access (mustdo) and those that have only
1135  * been matched to may accesses (maydo).
1136  * At the end of each level, must-sources and may-sources that are coscheduled
1137  * with the sources of the must-dependences at that level are considered.
1138  * If any coscheduled instances are found, then corresponding may-dependences
1139  * are added and the original must-dependences are turned into may-dependences.
1140  * Afterwards, the may accesses that occur after must-dependence sources
1141  * are considered.
1142  * In particular, we consider may accesses that precede the remaining
1143  * sink iterations, moving elements from mustdo to maydo when appropriate,
1144  * and may accesses that occur between a must source and a sink of any
1145  * dependences found at the current level, turning must dependences into
1146  * may dependences when appropriate.
1147  *
1148  */
compute_val_based_dependences(__isl_keep isl_access_info * acc)1149 static __isl_give isl_flow *compute_val_based_dependences(
1150 	__isl_keep isl_access_info *acc)
1151 {
1152 	isl_ctx *ctx;
1153 	isl_flow *res;
1154 	isl_set *mustdo = NULL;
1155 	isl_set *maydo = NULL;
1156 	int level, j;
1157 	isl_size n_in;
1158 	int depth;
1159 	isl_map **must_rel = NULL;
1160 	isl_map **may_rel = NULL;
1161 
1162 	if (!acc)
1163 		return NULL;
1164 
1165 	res = isl_flow_alloc(acc);
1166 	if (!res)
1167 		goto error;
1168 	ctx = isl_map_get_ctx(acc->sink.map);
1169 
1170 	n_in = isl_map_dim(acc->sink.map, isl_dim_in);
1171 	if (n_in < 0)
1172 		goto error;
1173 	depth = 2 * n_in + 1;
1174 	mustdo = isl_map_domain(isl_map_copy(acc->sink.map));
1175 	maydo = isl_set_empty(isl_set_get_space(mustdo));
1176 	if (!mustdo || !maydo)
1177 		goto error;
1178 	if (isl_set_plain_is_empty(mustdo))
1179 		goto done;
1180 
1181 	must_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1182 	may_rel = isl_calloc_array(ctx, struct isl_map *, acc->n_must);
1183 	if (!must_rel || !may_rel)
1184 		goto error;
1185 
1186 	for (level = depth; level >= 1; --level) {
1187 		for (j = acc->n_must-1; j >=0; --j) {
1188 			isl_space *space;
1189 			space = isl_map_get_space(res->dep[2 * j].map);
1190 			must_rel[j] = isl_map_empty(space);
1191 			may_rel[j] = isl_map_copy(must_rel[j]);
1192 		}
1193 
1194 		for (j = acc->n_must - 1; j >= 0; --j) {
1195 			struct isl_map *T;
1196 			struct isl_set *rest;
1197 			int plevel;
1198 
1199 			plevel = acc->level_before(acc->source[j].data,
1200 						     acc->sink.data);
1201 			if (plevel < 0)
1202 				goto error;
1203 			if (!can_precede_at_level(plevel, level))
1204 				continue;
1205 
1206 			T = last_source(acc, mustdo, j, level, &rest);
1207 			must_rel[j] = isl_map_union_disjoint(must_rel[j], T);
1208 			mustdo = rest;
1209 
1210 			if (intermediate_sources(acc, must_rel, j, level) < 0)
1211 				goto error;
1212 
1213 			T = last_source(acc, maydo, j, level, &rest);
1214 			may_rel[j] = isl_map_union_disjoint(may_rel[j], T);
1215 			maydo = rest;
1216 
1217 			if (intermediate_sources(acc, may_rel, j, level) < 0)
1218 				goto error;
1219 
1220 			if (isl_set_plain_is_empty(mustdo) &&
1221 			    isl_set_plain_is_empty(maydo))
1222 				break;
1223 		}
1224 		for (j = j - 1; j >= 0; --j) {
1225 			int plevel;
1226 
1227 			plevel = acc->level_before(acc->source[j].data,
1228 						     acc->sink.data);
1229 			if (plevel < 0)
1230 				goto error;
1231 			if (!can_precede_at_level(plevel, level))
1232 				continue;
1233 
1234 			if (intermediate_sources(acc, must_rel, j, level) < 0)
1235 				goto error;
1236 			if (intermediate_sources(acc, may_rel, j, level) < 0)
1237 				goto error;
1238 		}
1239 
1240 		res = handle_coscheduled(acc, must_rel, may_rel, res);
1241 		if (!res)
1242 			goto error;
1243 
1244 		for (j = 0; j < acc->n_may; ++j) {
1245 			int plevel;
1246 			isl_map *T;
1247 			isl_set *ran;
1248 
1249 			plevel = acc->level_before(acc->source[acc->n_must + j].data,
1250 						     acc->sink.data);
1251 			if (plevel < 0)
1252 				goto error;
1253 			if (!can_precede_at_level(plevel, level))
1254 				continue;
1255 
1256 			T = all_sources(acc, isl_set_copy(maydo), j, level);
1257 			res->dep[2 * acc->n_must + j].map =
1258 			    isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1259 			T = all_sources(acc, isl_set_copy(mustdo), j, level);
1260 			ran = isl_map_range(isl_map_copy(T));
1261 			res->dep[2 * acc->n_must + j].map =
1262 			    isl_map_union(res->dep[2 * acc->n_must + j].map, T);
1263 			mustdo = isl_set_subtract(mustdo, isl_set_copy(ran));
1264 			maydo = isl_set_union_disjoint(maydo, ran);
1265 
1266 			T = res->dep[2 * acc->n_must + j].map;
1267 			T = all_intermediate_sources(acc, T, must_rel, may_rel,
1268 							j, level);
1269 			res->dep[2 * acc->n_must + j].map = T;
1270 		}
1271 
1272 		for (j = acc->n_must - 1; j >= 0; --j) {
1273 			res->dep[2 * j].map =
1274 				isl_map_union_disjoint(res->dep[2 * j].map,
1275 							     must_rel[j]);
1276 			res->dep[2 * j + 1].map =
1277 				isl_map_union_disjoint(res->dep[2 * j + 1].map,
1278 							     may_rel[j]);
1279 		}
1280 
1281 		if (isl_set_plain_is_empty(mustdo) &&
1282 		    isl_set_plain_is_empty(maydo))
1283 			break;
1284 	}
1285 
1286 	free(must_rel);
1287 	free(may_rel);
1288 done:
1289 	res->must_no_source = mustdo;
1290 	res->may_no_source = maydo;
1291 	return res;
1292 error:
1293 	if (must_rel)
1294 		for (j = 0; j < acc->n_must; ++j)
1295 			isl_map_free(must_rel[j]);
1296 	if (may_rel)
1297 		for (j = 0; j < acc->n_must; ++j)
1298 			isl_map_free(may_rel[j]);
1299 	isl_flow_free(res);
1300 	isl_set_free(mustdo);
1301 	isl_set_free(maydo);
1302 	free(must_rel);
1303 	free(may_rel);
1304 	return NULL;
1305 }
1306 
1307 /* Given a "sink" access, a list of n "source" accesses,
1308  * compute for each iteration of the sink access
1309  * and for each element accessed by that iteration,
1310  * the source access in the list that last accessed the
1311  * element accessed by the sink access before this sink access.
1312  * Each access is given as a map from the loop iterators
1313  * to the array indices.
1314  * The result is a list of n relations between source and sink
1315  * iterations and a subset of the domain of the sink access,
1316  * corresponding to those iterations that access an element
1317  * not previously accessed.
1318  *
1319  * To deal with multi-valued sink access relations, the sink iteration
1320  * domain is first extended with dimensions that correspond to the data
1321  * space.  However, these extra dimensions are not projected out again.
1322  * It is up to the caller to decide whether these dimensions should be kept.
1323  */
access_info_compute_flow_core(__isl_take isl_access_info * acc)1324 static __isl_give isl_flow *access_info_compute_flow_core(
1325 	__isl_take isl_access_info *acc)
1326 {
1327 	struct isl_flow *res = NULL;
1328 
1329 	if (!acc)
1330 		return NULL;
1331 
1332 	acc->sink.map = isl_map_range_map(acc->sink.map);
1333 	if (!acc->sink.map)
1334 		goto error;
1335 
1336 	if (acc->n_must == 0)
1337 		res = compute_mem_based_dependences(acc);
1338 	else {
1339 		acc = isl_access_info_sort_sources(acc);
1340 		res = compute_val_based_dependences(acc);
1341 	}
1342 	acc = isl_access_info_free(acc);
1343 	if (!res)
1344 		return NULL;
1345 	if (!res->must_no_source || !res->may_no_source)
1346 		goto error;
1347 	return res;
1348 error:
1349 	isl_access_info_free(acc);
1350 	isl_flow_free(res);
1351 	return NULL;
1352 }
1353 
1354 /* Given a "sink" access, a list of n "source" accesses,
1355  * compute for each iteration of the sink access
1356  * and for each element accessed by that iteration,
1357  * the source access in the list that last accessed the
1358  * element accessed by the sink access before this sink access.
1359  * Each access is given as a map from the loop iterators
1360  * to the array indices.
1361  * The result is a list of n relations between source and sink
1362  * iterations and a subset of the domain of the sink access,
1363  * corresponding to those iterations that access an element
1364  * not previously accessed.
1365  *
1366  * To deal with multi-valued sink access relations,
1367  * access_info_compute_flow_core extends the sink iteration domain
1368  * with dimensions that correspond to the data space.  These extra dimensions
1369  * are projected out from the result of access_info_compute_flow_core.
1370  */
isl_access_info_compute_flow(__isl_take isl_access_info * acc)1371 __isl_give isl_flow *isl_access_info_compute_flow(__isl_take isl_access_info *acc)
1372 {
1373 	int j;
1374 	struct isl_flow *res;
1375 
1376 	if (!acc)
1377 		return NULL;
1378 
1379 	acc->domain_map = isl_map_domain_map(isl_map_copy(acc->sink.map));
1380 	res = access_info_compute_flow_core(acc);
1381 	if (!res)
1382 		return NULL;
1383 
1384 	for (j = 0; j < res->n_source; ++j) {
1385 		res->dep[j].map = isl_map_range_factor_domain(res->dep[j].map);
1386 		if (!res->dep[j].map)
1387 			goto error;
1388 	}
1389 
1390 	return res;
1391 error:
1392 	isl_flow_free(res);
1393 	return NULL;
1394 }
1395 
1396 
1397 /* Keep track of some information about a schedule for a given
1398  * access.  In particular, keep track of which dimensions
1399  * have a constant value and of the actual constant values.
1400  */
1401 struct isl_sched_info {
1402 	int *is_cst;
1403 	isl_vec *cst;
1404 };
1405 
sched_info_free(__isl_take struct isl_sched_info * info)1406 static void sched_info_free(__isl_take struct isl_sched_info *info)
1407 {
1408 	if (!info)
1409 		return;
1410 	isl_vec_free(info->cst);
1411 	free(info->is_cst);
1412 	free(info);
1413 }
1414 
1415 /* Extract information on the constant dimensions of the schedule
1416  * for a given access.  The "map" is of the form
1417  *
1418  *	[S -> D] -> A
1419  *
1420  * with S the schedule domain, D the iteration domain and A the data domain.
1421  */
sched_info_alloc(__isl_keep isl_map * map)1422 static __isl_give struct isl_sched_info *sched_info_alloc(
1423 	__isl_keep isl_map *map)
1424 {
1425 	isl_ctx *ctx;
1426 	isl_space *space;
1427 	struct isl_sched_info *info;
1428 	int i;
1429 	isl_size n;
1430 
1431 	if (!map)
1432 		return NULL;
1433 
1434 	space = isl_space_unwrap(isl_space_domain(isl_map_get_space(map)));
1435 	if (!space)
1436 		return NULL;
1437 	n = isl_space_dim(space, isl_dim_in);
1438 	isl_space_free(space);
1439 	if (n < 0)
1440 		return NULL;
1441 
1442 	ctx = isl_map_get_ctx(map);
1443 	info = isl_alloc_type(ctx, struct isl_sched_info);
1444 	if (!info)
1445 		return NULL;
1446 	info->is_cst = isl_alloc_array(ctx, int, n);
1447 	info->cst = isl_vec_alloc(ctx, n);
1448 	if (n && (!info->is_cst || !info->cst))
1449 		goto error;
1450 
1451 	for (i = 0; i < n; ++i) {
1452 		isl_val *v;
1453 
1454 		v = isl_map_plain_get_val_if_fixed(map, isl_dim_in, i);
1455 		if (!v)
1456 			goto error;
1457 		info->is_cst[i] = !isl_val_is_nan(v);
1458 		if (info->is_cst[i])
1459 			info->cst = isl_vec_set_element_val(info->cst, i, v);
1460 		else
1461 			isl_val_free(v);
1462 	}
1463 
1464 	return info;
1465 error:
1466 	sched_info_free(info);
1467 	return NULL;
1468 }
1469 
1470 /* The different types of access relations that isl_union_access_info
1471  * keeps track of.
1472 
1473  * "isl_access_sink" represents the sink accesses.
1474  * "isl_access_must_source" represents the definite source accesses.
1475  * "isl_access_may_source" represents the possible source accesses.
1476  * "isl_access_kill" represents the kills.
1477  *
1478  * isl_access_sink is sometimes treated differently and
1479  * should therefore appear first.
1480  */
1481 enum isl_access_type {
1482 	isl_access_sink,
1483 	isl_access_must_source,
1484 	isl_access_may_source,
1485 	isl_access_kill,
1486 	isl_access_end
1487 };
1488 
1489 /* This structure represents the input for a dependence analysis computation.
1490  *
1491  * "access" contains the access relations.
1492  *
1493  * "schedule" or "schedule_map" represents the execution order.
1494  * Exactly one of these fields should be NULL.  The other field
1495  * determines the execution order.
1496  *
1497  * The domains of these four maps refer to the same iteration spaces(s).
1498  * The ranges of the first three maps also refer to the same data space(s).
1499  *
1500  * After a call to isl_union_access_info_introduce_schedule,
1501  * the "schedule_map" field no longer contains useful information.
1502  */
1503 struct isl_union_access_info {
1504 	isl_union_map *access[isl_access_end];
1505 
1506 	isl_schedule *schedule;
1507 	isl_union_map *schedule_map;
1508 };
1509 
1510 /* Free "access" and return NULL.
1511  */
isl_union_access_info_free(__isl_take isl_union_access_info * access)1512 __isl_null isl_union_access_info *isl_union_access_info_free(
1513 	__isl_take isl_union_access_info *access)
1514 {
1515 	enum isl_access_type i;
1516 
1517 	if (!access)
1518 		return NULL;
1519 
1520 	for (i = isl_access_sink; i < isl_access_end; ++i)
1521 		isl_union_map_free(access->access[i]);
1522 	isl_schedule_free(access->schedule);
1523 	isl_union_map_free(access->schedule_map);
1524 	free(access);
1525 
1526 	return NULL;
1527 }
1528 
1529 /* Return the isl_ctx to which "access" belongs.
1530  */
isl_union_access_info_get_ctx(__isl_keep isl_union_access_info * access)1531 isl_ctx *isl_union_access_info_get_ctx(__isl_keep isl_union_access_info *access)
1532 {
1533 	if (!access)
1534 		return NULL;
1535 	return isl_union_map_get_ctx(access->access[isl_access_sink]);
1536 }
1537 
1538 /* Construct an empty (invalid) isl_union_access_info object.
1539  * The caller is responsible for setting the sink access relation and
1540  * initializing all the other fields, e.g., by calling
1541  * isl_union_access_info_init.
1542  */
isl_union_access_info_alloc(isl_ctx * ctx)1543 static __isl_give isl_union_access_info *isl_union_access_info_alloc(
1544 	isl_ctx *ctx)
1545 {
1546 	return isl_calloc_type(ctx, isl_union_access_info);
1547 }
1548 
1549 /* Initialize all the fields of "info", except the sink access relation,
1550  * which is assumed to have been set by the caller.
1551  *
1552  * By default, we use the schedule field of the isl_union_access_info,
1553  * but this may be overridden by a call
1554  * to isl_union_access_info_set_schedule_map.
1555  */
isl_union_access_info_init(__isl_take isl_union_access_info * info)1556 static __isl_give isl_union_access_info *isl_union_access_info_init(
1557 	__isl_take isl_union_access_info *info)
1558 {
1559 	isl_space *space;
1560 	isl_union_map *empty;
1561 	enum isl_access_type i;
1562 
1563 	if (!info)
1564 		return NULL;
1565 	if (!info->access[isl_access_sink])
1566 		return isl_union_access_info_free(info);
1567 
1568 	space = isl_union_map_get_space(info->access[isl_access_sink]);
1569 	empty = isl_union_map_empty(isl_space_copy(space));
1570 	for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1571 		if (!info->access[i])
1572 			info->access[i] = isl_union_map_copy(empty);
1573 	isl_union_map_free(empty);
1574 	if (!info->schedule && !info->schedule_map)
1575 		info->schedule = isl_schedule_empty(isl_space_copy(space));
1576 	isl_space_free(space);
1577 
1578 	for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1579 		if (!info->access[i])
1580 			return isl_union_access_info_free(info);
1581 	if (!info->schedule && !info->schedule_map)
1582 		return isl_union_access_info_free(info);
1583 
1584 	return info;
1585 }
1586 
1587 /* Create a new isl_union_access_info with the given sink accesses and
1588  * and no other accesses or schedule information.
1589  */
isl_union_access_info_from_sink(__isl_take isl_union_map * sink)1590 __isl_give isl_union_access_info *isl_union_access_info_from_sink(
1591 	__isl_take isl_union_map *sink)
1592 {
1593 	isl_ctx *ctx;
1594 	isl_union_access_info *access;
1595 
1596 	if (!sink)
1597 		return NULL;
1598 	ctx = isl_union_map_get_ctx(sink);
1599 	access = isl_union_access_info_alloc(ctx);
1600 	if (!access)
1601 		goto error;
1602 	access->access[isl_access_sink] = sink;
1603 	return isl_union_access_info_init(access);
1604 error:
1605 	isl_union_map_free(sink);
1606 	return NULL;
1607 }
1608 
1609 /* Replace the access relation of type "type" of "info" by "access".
1610  */
isl_union_access_info_set(__isl_take isl_union_access_info * info,enum isl_access_type type,__isl_take isl_union_map * access)1611 static __isl_give isl_union_access_info *isl_union_access_info_set(
1612 	__isl_take isl_union_access_info *info,
1613 	enum isl_access_type type, __isl_take isl_union_map *access)
1614 {
1615 	if (!info || !access)
1616 		goto error;
1617 
1618 	isl_union_map_free(info->access[type]);
1619 	info->access[type] = access;
1620 
1621 	return info;
1622 error:
1623 	isl_union_access_info_free(info);
1624 	isl_union_map_free(access);
1625 	return NULL;
1626 }
1627 
1628 /* Replace the definite source accesses of "access" by "must_source".
1629  */
isl_union_access_info_set_must_source(__isl_take isl_union_access_info * access,__isl_take isl_union_map * must_source)1630 __isl_give isl_union_access_info *isl_union_access_info_set_must_source(
1631 	__isl_take isl_union_access_info *access,
1632 	__isl_take isl_union_map *must_source)
1633 {
1634 	return isl_union_access_info_set(access, isl_access_must_source,
1635 					must_source);
1636 }
1637 
1638 /* Replace the possible source accesses of "access" by "may_source".
1639  */
isl_union_access_info_set_may_source(__isl_take isl_union_access_info * access,__isl_take isl_union_map * may_source)1640 __isl_give isl_union_access_info *isl_union_access_info_set_may_source(
1641 	__isl_take isl_union_access_info *access,
1642 	__isl_take isl_union_map *may_source)
1643 {
1644 	return isl_union_access_info_set(access, isl_access_may_source,
1645 					may_source);
1646 }
1647 
1648 /* Replace the kills of "info" by "kill".
1649  */
isl_union_access_info_set_kill(__isl_take isl_union_access_info * info,__isl_take isl_union_map * kill)1650 __isl_give isl_union_access_info *isl_union_access_info_set_kill(
1651 	__isl_take isl_union_access_info *info, __isl_take isl_union_map *kill)
1652 {
1653 	return isl_union_access_info_set(info, isl_access_kill, kill);
1654 }
1655 
1656 /* Return the access relation of type "type" of "info".
1657  */
isl_union_access_info_get(__isl_keep isl_union_access_info * info,enum isl_access_type type)1658 static __isl_give isl_union_map *isl_union_access_info_get(
1659 	__isl_keep isl_union_access_info *info, enum isl_access_type type)
1660 {
1661 	if (!info)
1662 		return NULL;
1663 	return isl_union_map_copy(info->access[type]);
1664 }
1665 
1666 /* Return the definite source accesses of "info".
1667  */
isl_union_access_info_get_must_source(__isl_keep isl_union_access_info * info)1668 __isl_give isl_union_map *isl_union_access_info_get_must_source(
1669 	__isl_keep isl_union_access_info *info)
1670 {
1671 	return isl_union_access_info_get(info, isl_access_must_source);
1672 }
1673 
1674 /* Return the possible source accesses of "info".
1675  */
isl_union_access_info_get_may_source(__isl_keep isl_union_access_info * info)1676 __isl_give isl_union_map *isl_union_access_info_get_may_source(
1677 	__isl_keep isl_union_access_info *info)
1678 {
1679 	return isl_union_access_info_get(info, isl_access_may_source);
1680 }
1681 
1682 /* Return the kills of "info".
1683  */
isl_union_access_info_get_kill(__isl_keep isl_union_access_info * info)1684 __isl_give isl_union_map *isl_union_access_info_get_kill(
1685 	__isl_keep isl_union_access_info *info)
1686 {
1687 	return isl_union_access_info_get(info, isl_access_kill);
1688 }
1689 
1690 /* Does "info" specify any kills?
1691  */
isl_union_access_has_kill(__isl_keep isl_union_access_info * info)1692 static isl_bool isl_union_access_has_kill(
1693 	__isl_keep isl_union_access_info *info)
1694 {
1695 	isl_bool empty;
1696 
1697 	if (!info)
1698 		return isl_bool_error;
1699 	empty = isl_union_map_is_empty(info->access[isl_access_kill]);
1700 	return isl_bool_not(empty);
1701 }
1702 
1703 /* Replace the schedule of "access" by "schedule".
1704  * Also free the schedule_map in case it was set last.
1705  */
isl_union_access_info_set_schedule(__isl_take isl_union_access_info * access,__isl_take isl_schedule * schedule)1706 __isl_give isl_union_access_info *isl_union_access_info_set_schedule(
1707 	__isl_take isl_union_access_info *access,
1708 	__isl_take isl_schedule *schedule)
1709 {
1710 	if (!access || !schedule)
1711 		goto error;
1712 
1713 	access->schedule_map = isl_union_map_free(access->schedule_map);
1714 	isl_schedule_free(access->schedule);
1715 	access->schedule = schedule;
1716 
1717 	return access;
1718 error:
1719 	isl_union_access_info_free(access);
1720 	isl_schedule_free(schedule);
1721 	return NULL;
1722 }
1723 
1724 /* Replace the schedule map of "access" by "schedule_map".
1725  * Also free the schedule in case it was set last.
1726  */
isl_union_access_info_set_schedule_map(__isl_take isl_union_access_info * access,__isl_take isl_union_map * schedule_map)1727 __isl_give isl_union_access_info *isl_union_access_info_set_schedule_map(
1728 	__isl_take isl_union_access_info *access,
1729 	__isl_take isl_union_map *schedule_map)
1730 {
1731 	if (!access || !schedule_map)
1732 		goto error;
1733 
1734 	isl_union_map_free(access->schedule_map);
1735 	access->schedule = isl_schedule_free(access->schedule);
1736 	access->schedule_map = schedule_map;
1737 
1738 	return access;
1739 error:
1740 	isl_union_access_info_free(access);
1741 	isl_union_map_free(schedule_map);
1742 	return NULL;
1743 }
1744 
isl_union_access_info_copy(__isl_keep isl_union_access_info * access)1745 __isl_give isl_union_access_info *isl_union_access_info_copy(
1746 	__isl_keep isl_union_access_info *access)
1747 {
1748 	isl_union_access_info *copy;
1749 	enum isl_access_type i;
1750 
1751 	if (!access)
1752 		return NULL;
1753 	copy = isl_union_access_info_from_sink(
1754 		    isl_union_map_copy(access->access[isl_access_sink]));
1755 	for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1756 		copy = isl_union_access_info_set(copy, i,
1757 					isl_union_map_copy(access->access[i]));
1758 	if (access->schedule)
1759 		copy = isl_union_access_info_set_schedule(copy,
1760 				isl_schedule_copy(access->schedule));
1761 	else
1762 		copy = isl_union_access_info_set_schedule_map(copy,
1763 				isl_union_map_copy(access->schedule_map));
1764 
1765 	return copy;
1766 }
1767 
1768 #undef BASE
1769 #define BASE union_map
1770 #include "print_yaml_field_templ.c"
1771 
1772 /* An enumeration of the various keys that may appear in a YAML mapping
1773  * of an isl_union_access_info object.
1774  * The keys for the access relation types are assumed to have the same values
1775  * as the access relation types in isl_access_type.
1776  */
1777 enum isl_ai_key {
1778 	isl_ai_key_error = -1,
1779 	isl_ai_key_sink = isl_access_sink,
1780 	isl_ai_key_must_source = isl_access_must_source,
1781 	isl_ai_key_may_source = isl_access_may_source,
1782 	isl_ai_key_kill = isl_access_kill,
1783 	isl_ai_key_schedule_map,
1784 	isl_ai_key_schedule,
1785 	isl_ai_key_end
1786 };
1787 
1788 /* Textual representations of the YAML keys for an isl_union_access_info
1789  * object.
1790  */
1791 static char *key_str[] = {
1792 	[isl_ai_key_sink] = "sink",
1793 	[isl_ai_key_must_source] = "must_source",
1794 	[isl_ai_key_may_source] = "may_source",
1795 	[isl_ai_key_kill] = "kill",
1796 	[isl_ai_key_schedule_map] = "schedule_map",
1797 	[isl_ai_key_schedule] = "schedule",
1798 };
1799 
1800 /* Print a key-value pair corresponding to the access relation of type "type"
1801  * of a YAML mapping of "info" to "p".
1802  *
1803  * The sink access relation is always printed, but any other access relation
1804  * is only printed if it is non-empty.
1805  */
print_access_field(__isl_take isl_printer * p,__isl_keep isl_union_access_info * info,enum isl_access_type type)1806 static __isl_give isl_printer *print_access_field(__isl_take isl_printer *p,
1807 	__isl_keep isl_union_access_info *info, enum isl_access_type type)
1808 {
1809 	if (type != isl_access_sink) {
1810 		isl_bool empty;
1811 
1812 		empty = isl_union_map_is_empty(info->access[type]);
1813 		if (empty < 0)
1814 			return isl_printer_free(p);
1815 		if (empty)
1816 			return p;
1817 	}
1818 	return print_yaml_field_union_map(p, key_str[type], info->access[type]);
1819 }
1820 
1821 /* Print the information contained in "access" to "p".
1822  * The information is printed as a YAML document.
1823  */
isl_printer_print_union_access_info(__isl_take isl_printer * p,__isl_keep isl_union_access_info * access)1824 __isl_give isl_printer *isl_printer_print_union_access_info(
1825 	__isl_take isl_printer *p, __isl_keep isl_union_access_info *access)
1826 {
1827 	enum isl_access_type i;
1828 
1829 	if (!access)
1830 		return isl_printer_free(p);
1831 
1832 	p = isl_printer_yaml_start_mapping(p);
1833 	for (i = isl_access_sink; i < isl_access_end; ++i)
1834 		p = print_access_field(p, access, i);
1835 	if (access->schedule) {
1836 		p = isl_printer_print_str(p, key_str[isl_ai_key_schedule]);
1837 		p = isl_printer_yaml_next(p);
1838 		p = isl_printer_print_schedule(p, access->schedule);
1839 		p = isl_printer_yaml_next(p);
1840 	} else {
1841 		p = print_yaml_field_union_map(p,
1842 			key_str[isl_ai_key_schedule_map], access->schedule_map);
1843 	}
1844 	p = isl_printer_yaml_end_mapping(p);
1845 
1846 	return p;
1847 }
1848 
1849 /* Return a string representation of the information in "access".
1850  * The information is printed in flow format.
1851  */
isl_union_access_info_to_str(__isl_keep isl_union_access_info * access)1852 __isl_give char *isl_union_access_info_to_str(
1853 	__isl_keep isl_union_access_info *access)
1854 {
1855 	isl_printer *p;
1856 	char *s;
1857 
1858 	if (!access)
1859 		return NULL;
1860 
1861 	p = isl_printer_to_str(isl_union_access_info_get_ctx(access));
1862 	p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
1863 	p = isl_printer_print_union_access_info(p, access);
1864 	s = isl_printer_get_str(p);
1865 	isl_printer_free(p);
1866 
1867 	return s;
1868 }
1869 
1870 #undef KEY
1871 #define KEY enum isl_ai_key
1872 #undef KEY_ERROR
1873 #define KEY_ERROR isl_ai_key_error
1874 #undef KEY_END
1875 #define KEY_END isl_ai_key_end
1876 #include "extract_key.c"
1877 
1878 #undef BASE
1879 #define BASE union_map
1880 #include "read_in_string_templ.c"
1881 
1882 /* Read an isl_union_access_info object from "s".
1883  *
1884  * Start off with an empty (invalid) isl_union_access_info object and
1885  * then fill up the fields based on the input.
1886  * The input needs to contain at least a description of the sink
1887  * access relation as well as some form of schedule.
1888  * The other access relations are set to empty relations
1889  * by isl_union_access_info_init if they are not specified in the input.
1890  */
isl_stream_read_union_access_info(isl_stream * s)1891 __isl_give isl_union_access_info *isl_stream_read_union_access_info(
1892 	isl_stream *s)
1893 {
1894 	isl_ctx *ctx;
1895 	isl_union_access_info *info;
1896 	int more;
1897 	int sink_set = 0;
1898 	int schedule_set = 0;
1899 
1900 	if (isl_stream_yaml_read_start_mapping(s))
1901 		return NULL;
1902 
1903 	ctx = isl_stream_get_ctx(s);
1904 	info = isl_union_access_info_alloc(ctx);
1905 	while ((more = isl_stream_yaml_next(s)) > 0) {
1906 		enum isl_ai_key key;
1907 		isl_union_map *access, *schedule_map;
1908 		isl_schedule *schedule;
1909 
1910 		key = get_key(s);
1911 		if (isl_stream_yaml_next(s) < 0)
1912 			return isl_union_access_info_free(info);
1913 		switch (key) {
1914 		case isl_ai_key_end:
1915 		case isl_ai_key_error:
1916 			return isl_union_access_info_free(info);
1917 		case isl_ai_key_sink:
1918 			sink_set = 1;
1919 		case isl_ai_key_must_source:
1920 		case isl_ai_key_may_source:
1921 		case isl_ai_key_kill:
1922 			access = read_union_map(s);
1923 			info = isl_union_access_info_set(info, key, access);
1924 			if (!info)
1925 				return NULL;
1926 			break;
1927 		case isl_ai_key_schedule_map:
1928 			schedule_set = 1;
1929 			schedule_map = read_union_map(s);
1930 			info = isl_union_access_info_set_schedule_map(info,
1931 								schedule_map);
1932 			if (!info)
1933 				return NULL;
1934 			break;
1935 		case isl_ai_key_schedule:
1936 			schedule_set = 1;
1937 			schedule = isl_stream_read_schedule(s);
1938 			info = isl_union_access_info_set_schedule(info,
1939 								schedule);
1940 			if (!info)
1941 				return NULL;
1942 			break;
1943 		}
1944 	}
1945 	if (more < 0)
1946 		return isl_union_access_info_free(info);
1947 
1948 	if (isl_stream_yaml_read_end_mapping(s) < 0) {
1949 		isl_stream_error(s, NULL, "unexpected extra elements");
1950 		return isl_union_access_info_free(info);
1951 	}
1952 
1953 	if (!sink_set) {
1954 		isl_stream_error(s, NULL, "no sink specified");
1955 		return isl_union_access_info_free(info);
1956 	}
1957 
1958 	if (!schedule_set) {
1959 		isl_stream_error(s, NULL, "no schedule specified");
1960 		return isl_union_access_info_free(info);
1961 	}
1962 
1963 	return isl_union_access_info_init(info);
1964 }
1965 
1966 /* Read an isl_union_access_info object from the file "input".
1967  */
isl_union_access_info_read_from_file(isl_ctx * ctx,FILE * input)1968 __isl_give isl_union_access_info *isl_union_access_info_read_from_file(
1969 	isl_ctx *ctx, FILE *input)
1970 {
1971 	isl_stream *s;
1972 	isl_union_access_info *access;
1973 
1974 	s = isl_stream_new_file(ctx, input);
1975 	if (!s)
1976 		return NULL;
1977 	access = isl_stream_read_union_access_info(s);
1978 	isl_stream_free(s);
1979 
1980 	return access;
1981 }
1982 
1983 /* Update the fields of "access" such that they all have the same parameters,
1984  * keeping in mind that the schedule_map field may be NULL and ignoring
1985  * the schedule field.
1986  */
isl_union_access_info_align_params(__isl_take isl_union_access_info * access)1987 static __isl_give isl_union_access_info *isl_union_access_info_align_params(
1988 	__isl_take isl_union_access_info *access)
1989 {
1990 	isl_space *space;
1991 	enum isl_access_type i;
1992 
1993 	if (!access)
1994 		return NULL;
1995 
1996 	space = isl_union_map_get_space(access->access[isl_access_sink]);
1997 	for (i = isl_access_sink + 1; i < isl_access_end; ++i)
1998 		space = isl_space_align_params(space,
1999 				isl_union_map_get_space(access->access[i]));
2000 	if (access->schedule_map)
2001 		space = isl_space_align_params(space,
2002 				isl_union_map_get_space(access->schedule_map));
2003 	for (i = isl_access_sink; i < isl_access_end; ++i)
2004 		access->access[i] =
2005 			isl_union_map_align_params(access->access[i],
2006 							isl_space_copy(space));
2007 	if (!access->schedule_map) {
2008 		isl_space_free(space);
2009 	} else {
2010 		access->schedule_map =
2011 		    isl_union_map_align_params(access->schedule_map, space);
2012 		if (!access->schedule_map)
2013 			return isl_union_access_info_free(access);
2014 	}
2015 
2016 	for (i = isl_access_sink; i < isl_access_end; ++i)
2017 		if (!access->access[i])
2018 			return isl_union_access_info_free(access);
2019 
2020 	return access;
2021 }
2022 
2023 /* Prepend the schedule dimensions to the iteration domains.
2024  *
2025  * That is, if the schedule is of the form
2026  *
2027  *	D -> S
2028  *
2029  * while the access relations are of the form
2030  *
2031  *	D -> A
2032  *
2033  * then the updated access relations are of the form
2034  *
2035  *	[S -> D] -> A
2036  *
2037  * The schedule map is also replaced by the map
2038  *
2039  *	[S -> D] -> D
2040  *
2041  * that is used during the internal computation.
2042  * Neither the original schedule map nor this updated schedule map
2043  * are used after the call to this function.
2044  */
2045 static __isl_give isl_union_access_info *
isl_union_access_info_introduce_schedule(__isl_take isl_union_access_info * access)2046 isl_union_access_info_introduce_schedule(
2047 	__isl_take isl_union_access_info *access)
2048 {
2049 	isl_union_map *sm;
2050 	enum isl_access_type i;
2051 
2052 	if (!access)
2053 		return NULL;
2054 
2055 	sm = isl_union_map_reverse(access->schedule_map);
2056 	sm = isl_union_map_range_map(sm);
2057 	for (i = isl_access_sink; i < isl_access_end; ++i)
2058 		access->access[i] =
2059 			isl_union_map_apply_range(isl_union_map_copy(sm),
2060 						access->access[i]);
2061 	access->schedule_map = sm;
2062 
2063 	for (i = isl_access_sink; i < isl_access_end; ++i)
2064 		if (!access->access[i])
2065 			return isl_union_access_info_free(access);
2066 	if (!access->schedule_map)
2067 		return isl_union_access_info_free(access);
2068 
2069 	return access;
2070 }
2071 
2072 /* This structure represents the result of a dependence analysis computation.
2073  *
2074  * "must_dep" represents the full definite dependences
2075  * "may_dep" represents the full non-definite dependences.
2076  * Both are of the form
2077  *
2078  *	[Source] -> [[Sink -> Data]]
2079  *
2080  * (after the schedule dimensions have been projected out).
2081  * "must_no_source" represents the subset of the sink accesses for which
2082  * definitely no source was found.
2083  * "may_no_source" represents the subset of the sink accesses for which
2084  * possibly, but not definitely, no source was found.
2085  */
2086 struct isl_union_flow {
2087 	isl_union_map *must_dep;
2088 	isl_union_map *may_dep;
2089 	isl_union_map *must_no_source;
2090 	isl_union_map *may_no_source;
2091 };
2092 
2093 /* Return the isl_ctx to which "flow" belongs.
2094  */
isl_union_flow_get_ctx(__isl_keep isl_union_flow * flow)2095 isl_ctx *isl_union_flow_get_ctx(__isl_keep isl_union_flow *flow)
2096 {
2097 	return flow ? isl_union_map_get_ctx(flow->must_dep) : NULL;
2098 }
2099 
2100 /* Free "flow" and return NULL.
2101  */
isl_union_flow_free(__isl_take isl_union_flow * flow)2102 __isl_null isl_union_flow *isl_union_flow_free(__isl_take isl_union_flow *flow)
2103 {
2104 	if (!flow)
2105 		return NULL;
2106 	isl_union_map_free(flow->must_dep);
2107 	isl_union_map_free(flow->may_dep);
2108 	isl_union_map_free(flow->must_no_source);
2109 	isl_union_map_free(flow->may_no_source);
2110 	free(flow);
2111 	return NULL;
2112 }
2113 
isl_union_flow_dump(__isl_keep isl_union_flow * flow)2114 void isl_union_flow_dump(__isl_keep isl_union_flow *flow)
2115 {
2116 	if (!flow)
2117 		return;
2118 
2119 	fprintf(stderr, "must dependences: ");
2120 	isl_union_map_dump(flow->must_dep);
2121 	fprintf(stderr, "may dependences: ");
2122 	isl_union_map_dump(flow->may_dep);
2123 	fprintf(stderr, "must no source: ");
2124 	isl_union_map_dump(flow->must_no_source);
2125 	fprintf(stderr, "may no source: ");
2126 	isl_union_map_dump(flow->may_no_source);
2127 }
2128 
2129 /* Return the full definite dependences in "flow", with accessed elements.
2130  */
isl_union_flow_get_full_must_dependence(__isl_keep isl_union_flow * flow)2131 __isl_give isl_union_map *isl_union_flow_get_full_must_dependence(
2132 	__isl_keep isl_union_flow *flow)
2133 {
2134 	if (!flow)
2135 		return NULL;
2136 	return isl_union_map_copy(flow->must_dep);
2137 }
2138 
2139 /* Return the full possible dependences in "flow", including the definite
2140  * dependences, with accessed elements.
2141  */
isl_union_flow_get_full_may_dependence(__isl_keep isl_union_flow * flow)2142 __isl_give isl_union_map *isl_union_flow_get_full_may_dependence(
2143 	__isl_keep isl_union_flow *flow)
2144 {
2145 	if (!flow)
2146 		return NULL;
2147 	return isl_union_map_union(isl_union_map_copy(flow->must_dep),
2148 				    isl_union_map_copy(flow->may_dep));
2149 }
2150 
2151 /* Return the definite dependences in "flow", without the accessed elements.
2152  */
isl_union_flow_get_must_dependence(__isl_keep isl_union_flow * flow)2153 __isl_give isl_union_map *isl_union_flow_get_must_dependence(
2154 	__isl_keep isl_union_flow *flow)
2155 {
2156 	isl_union_map *dep;
2157 
2158 	if (!flow)
2159 		return NULL;
2160 	dep = isl_union_map_copy(flow->must_dep);
2161 	return isl_union_map_range_factor_domain(dep);
2162 }
2163 
2164 /* Return the possible dependences in "flow", including the definite
2165  * dependences, without the accessed elements.
2166  */
isl_union_flow_get_may_dependence(__isl_keep isl_union_flow * flow)2167 __isl_give isl_union_map *isl_union_flow_get_may_dependence(
2168 	__isl_keep isl_union_flow *flow)
2169 {
2170 	isl_union_map *dep;
2171 
2172 	if (!flow)
2173 		return NULL;
2174 	dep = isl_union_map_union(isl_union_map_copy(flow->must_dep),
2175 				    isl_union_map_copy(flow->may_dep));
2176 	return isl_union_map_range_factor_domain(dep);
2177 }
2178 
2179 /* Return the non-definite dependences in "flow".
2180  */
isl_union_flow_get_non_must_dependence(__isl_keep isl_union_flow * flow)2181 static __isl_give isl_union_map *isl_union_flow_get_non_must_dependence(
2182 	__isl_keep isl_union_flow *flow)
2183 {
2184 	if (!flow)
2185 		return NULL;
2186 	return isl_union_map_copy(flow->may_dep);
2187 }
2188 
2189 /* Return the subset of the sink accesses for which definitely
2190  * no source was found.
2191  */
isl_union_flow_get_must_no_source(__isl_keep isl_union_flow * flow)2192 __isl_give isl_union_map *isl_union_flow_get_must_no_source(
2193 	__isl_keep isl_union_flow *flow)
2194 {
2195 	if (!flow)
2196 		return NULL;
2197 	return isl_union_map_copy(flow->must_no_source);
2198 }
2199 
2200 /* Return the subset of the sink accesses for which possibly
2201  * no source was found, including those for which definitely
2202  * no source was found.
2203  */
isl_union_flow_get_may_no_source(__isl_keep isl_union_flow * flow)2204 __isl_give isl_union_map *isl_union_flow_get_may_no_source(
2205 	__isl_keep isl_union_flow *flow)
2206 {
2207 	if (!flow)
2208 		return NULL;
2209 	return isl_union_map_union(isl_union_map_copy(flow->must_no_source),
2210 				    isl_union_map_copy(flow->may_no_source));
2211 }
2212 
2213 /* Return the subset of the sink accesses for which possibly, but not
2214  * definitely, no source was found.
2215  */
isl_union_flow_get_non_must_no_source(__isl_keep isl_union_flow * flow)2216 static __isl_give isl_union_map *isl_union_flow_get_non_must_no_source(
2217 	__isl_keep isl_union_flow *flow)
2218 {
2219 	if (!flow)
2220 		return NULL;
2221 	return isl_union_map_copy(flow->may_no_source);
2222 }
2223 
2224 /* Create a new isl_union_flow object, initialized with empty
2225  * dependence relations and sink subsets.
2226  */
isl_union_flow_alloc(__isl_take isl_space * space)2227 static __isl_give isl_union_flow *isl_union_flow_alloc(
2228 	__isl_take isl_space *space)
2229 {
2230 	isl_ctx *ctx;
2231 	isl_union_map *empty;
2232 	isl_union_flow *flow;
2233 
2234 	if (!space)
2235 		return NULL;
2236 	ctx = isl_space_get_ctx(space);
2237 	flow = isl_alloc_type(ctx, isl_union_flow);
2238 	if (!flow)
2239 		goto error;
2240 
2241 	empty = isl_union_map_empty(space);
2242 	flow->must_dep = isl_union_map_copy(empty);
2243 	flow->may_dep = isl_union_map_copy(empty);
2244 	flow->must_no_source = isl_union_map_copy(empty);
2245 	flow->may_no_source = empty;
2246 
2247 	if (!flow->must_dep || !flow->may_dep ||
2248 	    !flow->must_no_source || !flow->may_no_source)
2249 		return isl_union_flow_free(flow);
2250 
2251 	return flow;
2252 error:
2253 	isl_space_free(space);
2254 	return NULL;
2255 }
2256 
2257 /* Copy this isl_union_flow object.
2258  */
isl_union_flow_copy(__isl_keep isl_union_flow * flow)2259 __isl_give isl_union_flow *isl_union_flow_copy(__isl_keep isl_union_flow *flow)
2260 {
2261 	isl_union_flow *copy;
2262 
2263 	if (!flow)
2264 		return NULL;
2265 
2266 	copy = isl_union_flow_alloc(isl_union_map_get_space(flow->must_dep));
2267 
2268 	if (!copy)
2269 		return NULL;
2270 
2271 	copy->must_dep = isl_union_map_union(copy->must_dep,
2272 		isl_union_map_copy(flow->must_dep));
2273 	copy->may_dep = isl_union_map_union(copy->may_dep,
2274 		isl_union_map_copy(flow->may_dep));
2275 	copy->must_no_source = isl_union_map_union(copy->must_no_source,
2276 		isl_union_map_copy(flow->must_no_source));
2277 	copy->may_no_source = isl_union_map_union(copy->may_no_source,
2278 		isl_union_map_copy(flow->may_no_source));
2279 
2280 	if (!copy->must_dep || !copy->may_dep ||
2281 	    !copy->must_no_source || !copy->may_no_source)
2282 		return isl_union_flow_free(copy);
2283 
2284 	return copy;
2285 }
2286 
2287 /* Drop the schedule dimensions from the iteration domains in "flow".
2288  * In particular, the schedule dimensions have been prepended
2289  * to the iteration domains prior to the dependence analysis by
2290  * replacing the iteration domain D, by the wrapped map [S -> D].
2291  * Replace these wrapped maps by the original D.
2292  *
2293  * In particular, the dependences computed by access_info_compute_flow_core
2294  * are of the form
2295  *
2296  *	[S -> D] -> [[S' -> D'] -> A]
2297  *
2298  * The schedule dimensions are projected out by first currying the range,
2299  * resulting in
2300  *
2301  *	[S -> D] -> [S' -> [D' -> A]]
2302  *
2303  * and then computing the factor range
2304  *
2305  *	D -> [D' -> A]
2306  */
isl_union_flow_drop_schedule(__isl_take isl_union_flow * flow)2307 static __isl_give isl_union_flow *isl_union_flow_drop_schedule(
2308 	__isl_take isl_union_flow *flow)
2309 {
2310 	if (!flow)
2311 		return NULL;
2312 
2313 	flow->must_dep = isl_union_map_range_curry(flow->must_dep);
2314 	flow->must_dep = isl_union_map_factor_range(flow->must_dep);
2315 	flow->may_dep = isl_union_map_range_curry(flow->may_dep);
2316 	flow->may_dep = isl_union_map_factor_range(flow->may_dep);
2317 	flow->must_no_source =
2318 		isl_union_map_domain_factor_range(flow->must_no_source);
2319 	flow->may_no_source =
2320 		isl_union_map_domain_factor_range(flow->may_no_source);
2321 
2322 	if (!flow->must_dep || !flow->may_dep ||
2323 	    !flow->must_no_source || !flow->may_no_source)
2324 		return isl_union_flow_free(flow);
2325 
2326 	return flow;
2327 }
2328 
2329 struct isl_compute_flow_data {
2330 	isl_union_map *must_source;
2331 	isl_union_map *may_source;
2332 	isl_union_flow *flow;
2333 
2334 	int count;
2335 	int must;
2336 	isl_space *dim;
2337 	struct isl_sched_info *sink_info;
2338 	struct isl_sched_info **source_info;
2339 	isl_access_info *accesses;
2340 };
2341 
count_matching_array(__isl_take isl_map * map,void * user)2342 static isl_stat count_matching_array(__isl_take isl_map *map, void *user)
2343 {
2344 	int eq;
2345 	isl_space *space;
2346 	struct isl_compute_flow_data *data;
2347 
2348 	data = (struct isl_compute_flow_data *)user;
2349 
2350 	space = isl_space_range(isl_map_get_space(map));
2351 
2352 	eq = isl_space_is_equal(space, data->dim);
2353 
2354 	isl_space_free(space);
2355 	isl_map_free(map);
2356 
2357 	if (eq < 0)
2358 		return isl_stat_error;
2359 	if (eq)
2360 		data->count++;
2361 
2362 	return isl_stat_ok;
2363 }
2364 
collect_matching_array(__isl_take isl_map * map,void * user)2365 static isl_stat collect_matching_array(__isl_take isl_map *map, void *user)
2366 {
2367 	int eq;
2368 	isl_space *space;
2369 	struct isl_sched_info *info;
2370 	struct isl_compute_flow_data *data;
2371 
2372 	data = (struct isl_compute_flow_data *)user;
2373 
2374 	space = isl_space_range(isl_map_get_space(map));
2375 
2376 	eq = isl_space_is_equal(space, data->dim);
2377 
2378 	isl_space_free(space);
2379 
2380 	if (eq < 0)
2381 		goto error;
2382 	if (!eq) {
2383 		isl_map_free(map);
2384 		return isl_stat_ok;
2385 	}
2386 
2387 	info = sched_info_alloc(map);
2388 	data->source_info[data->count] = info;
2389 
2390 	data->accesses = isl_access_info_add_source(data->accesses,
2391 						    map, data->must, info);
2392 
2393 	data->count++;
2394 
2395 	return isl_stat_ok;
2396 error:
2397 	isl_map_free(map);
2398 	return isl_stat_error;
2399 }
2400 
2401 /* Determine the shared nesting level and the "textual order" of
2402  * the given accesses.
2403  *
2404  * We first determine the minimal schedule dimension for both accesses.
2405  *
2406  * If among those dimensions, we can find one where both have a fixed
2407  * value and if moreover those values are different, then the previous
2408  * dimension is the last shared nesting level and the textual order
2409  * is determined based on the order of the fixed values.
2410  * If no such fixed values can be found, then we set the shared
2411  * nesting level to the minimal schedule dimension, with no textual ordering.
2412  */
before(void * first,void * second)2413 static int before(void *first, void *second)
2414 {
2415 	struct isl_sched_info *info1 = first;
2416 	struct isl_sched_info *info2 = second;
2417 	isl_size n1, n2;
2418 	int i;
2419 
2420 	n1 = isl_vec_size(info1->cst);
2421 	n2 = isl_vec_size(info2->cst);
2422 	if (n1 < 0 || n2 < 0)
2423 		return -1;
2424 
2425 	if (n2 < n1)
2426 		n1 = n2;
2427 
2428 	for (i = 0; i < n1; ++i) {
2429 		int r;
2430 		int cmp;
2431 
2432 		if (!info1->is_cst[i])
2433 			continue;
2434 		if (!info2->is_cst[i])
2435 			continue;
2436 		cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2437 		if (cmp == 0)
2438 			continue;
2439 
2440 		r = 2 * i + (cmp < 0);
2441 
2442 		return r;
2443 	}
2444 
2445 	return 2 * n1;
2446 }
2447 
2448 /* Check if the given two accesses may be coscheduled.
2449  * If so, return isl_bool_true.  Otherwise return isl_bool_false.
2450  *
2451  * Two accesses may only be coscheduled if the fixed schedule
2452  * coordinates have the same values.
2453  */
coscheduled(void * first,void * second)2454 static isl_bool coscheduled(void *first, void *second)
2455 {
2456 	struct isl_sched_info *info1 = first;
2457 	struct isl_sched_info *info2 = second;
2458 	isl_size n1, n2;
2459 	int i;
2460 
2461 	n1 = isl_vec_size(info1->cst);
2462 	n2 = isl_vec_size(info2->cst);
2463 	if (n1 < 0 || n2 < 0)
2464 		return isl_bool_error;
2465 
2466 	if (n2 < n1)
2467 		n1 = n2;
2468 
2469 	for (i = 0; i < n1; ++i) {
2470 		int cmp;
2471 
2472 		if (!info1->is_cst[i])
2473 			continue;
2474 		if (!info2->is_cst[i])
2475 			continue;
2476 		cmp = isl_vec_cmp_element(info1->cst, info2->cst, i);
2477 		if (cmp != 0)
2478 			return isl_bool_false;
2479 	}
2480 
2481 	return isl_bool_true;
2482 }
2483 
2484 /* Given a sink access, look for all the source accesses that access
2485  * the same array and perform dataflow analysis on them using
2486  * isl_access_info_compute_flow_core.
2487  */
compute_flow(__isl_take isl_map * map,void * user)2488 static isl_stat compute_flow(__isl_take isl_map *map, void *user)
2489 {
2490 	int i;
2491 	isl_ctx *ctx;
2492 	struct isl_compute_flow_data *data;
2493 	isl_flow *flow;
2494 	isl_union_flow *df;
2495 
2496 	data = (struct isl_compute_flow_data *)user;
2497 	df = data->flow;
2498 
2499 	ctx = isl_map_get_ctx(map);
2500 
2501 	data->accesses = NULL;
2502 	data->sink_info = NULL;
2503 	data->source_info = NULL;
2504 	data->count = 0;
2505 	data->dim = isl_space_range(isl_map_get_space(map));
2506 
2507 	if (isl_union_map_foreach_map(data->must_source,
2508 					&count_matching_array, data) < 0)
2509 		goto error;
2510 	if (isl_union_map_foreach_map(data->may_source,
2511 					&count_matching_array, data) < 0)
2512 		goto error;
2513 
2514 	data->sink_info = sched_info_alloc(map);
2515 	data->source_info = isl_calloc_array(ctx, struct isl_sched_info *,
2516 					     data->count);
2517 
2518 	data->accesses = isl_access_info_alloc(isl_map_copy(map),
2519 				data->sink_info, &before, data->count);
2520 	if (!data->sink_info || (data->count && !data->source_info) ||
2521 	    !data->accesses)
2522 		goto error;
2523 	data->accesses->coscheduled = &coscheduled;
2524 	data->count = 0;
2525 	data->must = 1;
2526 	if (isl_union_map_foreach_map(data->must_source,
2527 					&collect_matching_array, data) < 0)
2528 		goto error;
2529 	data->must = 0;
2530 	if (isl_union_map_foreach_map(data->may_source,
2531 					&collect_matching_array, data) < 0)
2532 		goto error;
2533 
2534 	flow = access_info_compute_flow_core(data->accesses);
2535 	data->accesses = NULL;
2536 
2537 	if (!flow)
2538 		goto error;
2539 
2540 	df->must_no_source = isl_union_map_union(df->must_no_source,
2541 		    isl_union_map_from_map(isl_flow_get_no_source(flow, 1)));
2542 	df->may_no_source = isl_union_map_union(df->may_no_source,
2543 		    isl_union_map_from_map(isl_flow_get_no_source(flow, 0)));
2544 
2545 	for (i = 0; i < flow->n_source; ++i) {
2546 		isl_union_map *dep;
2547 		dep = isl_union_map_from_map(isl_map_copy(flow->dep[i].map));
2548 		if (flow->dep[i].must)
2549 			df->must_dep = isl_union_map_union(df->must_dep, dep);
2550 		else
2551 			df->may_dep = isl_union_map_union(df->may_dep, dep);
2552 	}
2553 
2554 	isl_flow_free(flow);
2555 
2556 	sched_info_free(data->sink_info);
2557 	if (data->source_info) {
2558 		for (i = 0; i < data->count; ++i)
2559 			sched_info_free(data->source_info[i]);
2560 		free(data->source_info);
2561 	}
2562 	isl_space_free(data->dim);
2563 	isl_map_free(map);
2564 
2565 	return isl_stat_ok;
2566 error:
2567 	isl_access_info_free(data->accesses);
2568 	sched_info_free(data->sink_info);
2569 	if (data->source_info) {
2570 		for (i = 0; i < data->count; ++i)
2571 			sched_info_free(data->source_info[i]);
2572 		free(data->source_info);
2573 	}
2574 	isl_space_free(data->dim);
2575 	isl_map_free(map);
2576 
2577 	return isl_stat_error;
2578 }
2579 
2580 /* Add the kills of "info" to the must-sources.
2581  */
2582 static __isl_give isl_union_access_info *
isl_union_access_info_add_kill_to_must_source(__isl_take isl_union_access_info * info)2583 isl_union_access_info_add_kill_to_must_source(
2584 	__isl_take isl_union_access_info *info)
2585 {
2586 	isl_union_map *must, *kill;
2587 
2588 	must = isl_union_access_info_get_must_source(info);
2589 	kill = isl_union_access_info_get_kill(info);
2590 	must = isl_union_map_union(must, kill);
2591 	return isl_union_access_info_set_must_source(info, must);
2592 }
2593 
2594 /* Drop dependences from "flow" that purely originate from kills.
2595  * That is, only keep those dependences that originate from
2596  * the original must-sources "must" and/or the original may-sources "may".
2597  * In particular, "must" contains the must-sources from before
2598  * the kills were added and "may" contains the may-source from before
2599  * the kills were removed.
2600  *
2601  * The dependences are of the form
2602  *
2603  *	Source -> [Sink -> Data]
2604  *
2605  * Only those dependences are kept where the Source -> Data part
2606  * is a subset of the original may-sources or must-sources.
2607  * Of those, only the must-dependences that intersect with the must-sources
2608  * remain must-dependences.
2609  * If there is some overlap between the may-sources and the must-sources,
2610  * then the may-dependences and must-dependences may also overlap.
2611  * This should be fine since the may-dependences are only kept
2612  * disjoint from the must-dependences for the isl_union_map_compute_flow
2613  * interface.  This interface does not support kills, so it will
2614  * not end up calling this function.
2615  */
isl_union_flow_drop_kill_source(__isl_take isl_union_flow * flow,__isl_take isl_union_map * must,__isl_take isl_union_map * may)2616 static __isl_give isl_union_flow *isl_union_flow_drop_kill_source(
2617 	__isl_take isl_union_flow *flow, __isl_take isl_union_map *must,
2618 	__isl_take isl_union_map *may)
2619 {
2620 	isl_union_map *move;
2621 
2622 	if (!flow)
2623 		goto error;
2624 	move = isl_union_map_copy(flow->must_dep);
2625 	move = isl_union_map_intersect_range_factor_range(move,
2626 				isl_union_map_copy(may));
2627 	may = isl_union_map_union(may, isl_union_map_copy(must));
2628 	flow->may_dep = isl_union_map_intersect_range_factor_range(
2629 				flow->may_dep, may);
2630 	flow->must_dep = isl_union_map_intersect_range_factor_range(
2631 				flow->must_dep, must);
2632 	flow->may_dep = isl_union_map_union(flow->may_dep, move);
2633 	if (!flow->must_dep || !flow->may_dep)
2634 		return isl_union_flow_free(flow);
2635 
2636 	return flow;
2637 error:
2638 	isl_union_map_free(must);
2639 	isl_union_map_free(may);
2640 	return NULL;
2641 }
2642 
2643 /* Remove the must accesses from the may accesses.
2644  *
2645  * A must access always trumps a may access, so there is no need
2646  * for a must access to also be considered as a may access.  Doing so
2647  * would only cost extra computations only to find out that
2648  * the duplicated may access does not make any difference.
2649  */
isl_union_access_info_normalize(__isl_take isl_union_access_info * access)2650 static __isl_give isl_union_access_info *isl_union_access_info_normalize(
2651 	__isl_take isl_union_access_info *access)
2652 {
2653 	if (!access)
2654 		return NULL;
2655 	access->access[isl_access_may_source] =
2656 		isl_union_map_subtract(access->access[isl_access_may_source],
2657 		    isl_union_map_copy(access->access[isl_access_must_source]));
2658 	if (!access->access[isl_access_may_source])
2659 		return isl_union_access_info_free(access);
2660 
2661 	return access;
2662 }
2663 
2664 /* Given a description of the "sink" accesses, the "source" accesses and
2665  * a schedule, compute for each instance of a sink access
2666  * and for each element accessed by that instance,
2667  * the possible or definite source accesses that last accessed the
2668  * element accessed by the sink access before this sink access
2669  * in the sense that there is no intermediate definite source access.
2670  *
2671  * The must_no_source and may_no_source elements of the result
2672  * are subsets of access->sink.  The elements must_dep and may_dep
2673  * map domain elements of access->{may,must)_source to
2674  * domain elements of access->sink.
2675  *
2676  * This function is used when only the schedule map representation
2677  * is available.
2678  *
2679  * We first prepend the schedule dimensions to the domain
2680  * of the accesses so that we can easily compare their relative order.
2681  * Then we consider each sink access individually in compute_flow.
2682  */
compute_flow_union_map(__isl_take isl_union_access_info * access)2683 static __isl_give isl_union_flow *compute_flow_union_map(
2684 	__isl_take isl_union_access_info *access)
2685 {
2686 	struct isl_compute_flow_data data;
2687 	isl_union_map *sink;
2688 
2689 	access = isl_union_access_info_align_params(access);
2690 	access = isl_union_access_info_introduce_schedule(access);
2691 	if (!access)
2692 		return NULL;
2693 
2694 	data.must_source = access->access[isl_access_must_source];
2695 	data.may_source = access->access[isl_access_may_source];
2696 
2697 	sink = access->access[isl_access_sink];
2698 	data.flow = isl_union_flow_alloc(isl_union_map_get_space(sink));
2699 
2700 	if (isl_union_map_foreach_map(sink, &compute_flow, &data) < 0)
2701 		goto error;
2702 
2703 	data.flow = isl_union_flow_drop_schedule(data.flow);
2704 
2705 	isl_union_access_info_free(access);
2706 	return data.flow;
2707 error:
2708 	isl_union_access_info_free(access);
2709 	isl_union_flow_free(data.flow);
2710 	return NULL;
2711 }
2712 
2713 /* A schedule access relation.
2714  *
2715  * The access relation "access" is of the form [S -> D] -> A,
2716  * where S corresponds to the prefix schedule at "node".
2717  * "must" is only relevant for source accesses and indicates
2718  * whether the access is a must source or a may source.
2719  */
2720 struct isl_scheduled_access {
2721 	isl_map *access;
2722 	int must;
2723 	isl_schedule_node *node;
2724 };
2725 
2726 /* Data structure for keeping track of individual scheduled sink and source
2727  * accesses when computing dependence analysis based on a schedule tree.
2728  *
2729  * "n_sink" is the number of used entries in "sink"
2730  * "n_source" is the number of used entries in "source"
2731  *
2732  * "set_sink", "must" and "node" are only used inside collect_sink_source,
2733  * to keep track of the current node and
2734  * of what extract_sink_source needs to do.
2735  */
2736 struct isl_compute_flow_schedule_data {
2737 	isl_union_access_info *access;
2738 
2739 	int n_sink;
2740 	int n_source;
2741 
2742 	struct isl_scheduled_access *sink;
2743 	struct isl_scheduled_access *source;
2744 
2745 	int set_sink;
2746 	int must;
2747 	isl_schedule_node *node;
2748 };
2749 
2750 /* Align the parameters of all sinks with all sources.
2751  *
2752  * If there are no sinks or no sources, then no alignment is needed.
2753  */
isl_compute_flow_schedule_data_align_params(struct isl_compute_flow_schedule_data * data)2754 static void isl_compute_flow_schedule_data_align_params(
2755 	struct isl_compute_flow_schedule_data *data)
2756 {
2757 	int i;
2758 	isl_space *space;
2759 
2760 	if (data->n_sink == 0 || data->n_source == 0)
2761 		return;
2762 
2763 	space = isl_map_get_space(data->sink[0].access);
2764 
2765 	for (i = 1; i < data->n_sink; ++i)
2766 		space = isl_space_align_params(space,
2767 				isl_map_get_space(data->sink[i].access));
2768 	for (i = 0; i < data->n_source; ++i)
2769 		space = isl_space_align_params(space,
2770 				isl_map_get_space(data->source[i].access));
2771 
2772 	for (i = 0; i < data->n_sink; ++i)
2773 		data->sink[i].access =
2774 			isl_map_align_params(data->sink[i].access,
2775 							isl_space_copy(space));
2776 	for (i = 0; i < data->n_source; ++i)
2777 		data->source[i].access =
2778 			isl_map_align_params(data->source[i].access,
2779 							isl_space_copy(space));
2780 
2781 	isl_space_free(space);
2782 }
2783 
2784 /* Free all the memory referenced from "data".
2785  * Do not free "data" itself as it may be allocated on the stack.
2786  */
isl_compute_flow_schedule_data_clear(struct isl_compute_flow_schedule_data * data)2787 static void isl_compute_flow_schedule_data_clear(
2788 	struct isl_compute_flow_schedule_data *data)
2789 {
2790 	int i;
2791 
2792 	if (!data->sink)
2793 		return;
2794 
2795 	for (i = 0; i < data->n_sink; ++i) {
2796 		isl_map_free(data->sink[i].access);
2797 		isl_schedule_node_free(data->sink[i].node);
2798 	}
2799 
2800 	for (i = 0; i < data->n_source; ++i) {
2801 		isl_map_free(data->source[i].access);
2802 		isl_schedule_node_free(data->source[i].node);
2803 	}
2804 
2805 	free(data->sink);
2806 }
2807 
2808 /* isl_schedule_foreach_schedule_node_top_down callback for counting
2809  * (an upper bound on) the number of sinks and sources.
2810  *
2811  * Sinks and sources are only extracted at leaves of the tree,
2812  * so we skip the node if it is not a leaf.
2813  * Otherwise we increment data->n_sink and data->n_source with
2814  * the number of spaces in the sink and source access domains
2815  * that reach this node.
2816  */
count_sink_source(__isl_keep isl_schedule_node * node,void * user)2817 static isl_bool count_sink_source(__isl_keep isl_schedule_node *node,
2818 	void *user)
2819 {
2820 	struct isl_compute_flow_schedule_data *data = user;
2821 	isl_union_set *domain;
2822 	isl_union_map *umap;
2823 	isl_bool r = isl_bool_false;
2824 	isl_size n;
2825 
2826 	if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2827 		return isl_bool_true;
2828 
2829 	domain = isl_schedule_node_get_universe_domain(node);
2830 
2831 	umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2832 	umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2833 	data->n_sink += n = isl_union_map_n_map(umap);
2834 	isl_union_map_free(umap);
2835 	if (n < 0)
2836 		r = isl_bool_error;
2837 
2838 	umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2839 	umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2840 	data->n_source += n = isl_union_map_n_map(umap);
2841 	isl_union_map_free(umap);
2842 	if (n < 0)
2843 		r = isl_bool_error;
2844 
2845 	umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2846 	umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(domain));
2847 	data->n_source += n = isl_union_map_n_map(umap);
2848 	isl_union_map_free(umap);
2849 	if (n < 0)
2850 		r = isl_bool_error;
2851 
2852 	isl_union_set_free(domain);
2853 
2854 	return r;
2855 }
2856 
2857 /* Add a single scheduled sink or source (depending on data->set_sink)
2858  * with scheduled access relation "map", must property data->must and
2859  * schedule node data->node to the list of sinks or sources.
2860  */
extract_sink_source(__isl_take isl_map * map,void * user)2861 static isl_stat extract_sink_source(__isl_take isl_map *map, void *user)
2862 {
2863 	struct isl_compute_flow_schedule_data *data = user;
2864 	struct isl_scheduled_access *access;
2865 
2866 	if (data->set_sink)
2867 		access = data->sink + data->n_sink++;
2868 	else
2869 		access = data->source + data->n_source++;
2870 
2871 	access->access = map;
2872 	access->must = data->must;
2873 	access->node = isl_schedule_node_copy(data->node);
2874 
2875 	return isl_stat_ok;
2876 }
2877 
2878 /* isl_schedule_foreach_schedule_node_top_down callback for collecting
2879  * individual scheduled source and sink accesses (taking into account
2880  * the domain of the schedule).
2881  *
2882  * We only collect accesses at the leaves of the schedule tree.
2883  * We prepend the schedule dimensions at the leaf to the iteration
2884  * domains of the source and sink accesses and then extract
2885  * the individual accesses (per space).
2886  *
2887  * In particular, if the prefix schedule at the node is of the form
2888  *
2889  *	D -> S
2890  *
2891  * while the access relations are of the form
2892  *
2893  *	D -> A
2894  *
2895  * then the updated access relations are of the form
2896  *
2897  *	[S -> D] -> A
2898  *
2899  * Note that S consists of a single space such that introducing S
2900  * in the access relations does not increase the number of spaces.
2901  */
collect_sink_source(__isl_keep isl_schedule_node * node,void * user)2902 static isl_bool collect_sink_source(__isl_keep isl_schedule_node *node,
2903 	void *user)
2904 {
2905 	struct isl_compute_flow_schedule_data *data = user;
2906 	isl_union_map *prefix;
2907 	isl_union_map *umap;
2908 	isl_bool r = isl_bool_false;
2909 
2910 	if (isl_schedule_node_get_type(node) != isl_schedule_node_leaf)
2911 		return isl_bool_true;
2912 
2913 	data->node = node;
2914 
2915 	prefix = isl_schedule_node_get_prefix_schedule_relation(node);
2916 	prefix = isl_union_map_reverse(prefix);
2917 	prefix = isl_union_map_range_map(prefix);
2918 
2919 	data->set_sink = 1;
2920 	umap = isl_union_map_copy(data->access->access[isl_access_sink]);
2921 	umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2922 	if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2923 		r = isl_bool_error;
2924 	isl_union_map_free(umap);
2925 
2926 	data->set_sink = 0;
2927 	data->must = 1;
2928 	umap = isl_union_map_copy(data->access->access[isl_access_must_source]);
2929 	umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2930 	if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2931 		r = isl_bool_error;
2932 	isl_union_map_free(umap);
2933 
2934 	data->set_sink = 0;
2935 	data->must = 0;
2936 	umap = isl_union_map_copy(data->access->access[isl_access_may_source]);
2937 	umap = isl_union_map_apply_range(isl_union_map_copy(prefix), umap);
2938 	if (isl_union_map_foreach_map(umap, &extract_sink_source, data) < 0)
2939 		r = isl_bool_error;
2940 	isl_union_map_free(umap);
2941 
2942 	isl_union_map_free(prefix);
2943 
2944 	return r;
2945 }
2946 
2947 /* isl_access_info_compute_flow callback for determining whether
2948  * the shared nesting level and the ordering within that level
2949  * for two scheduled accesses for use in compute_single_flow.
2950  *
2951  * The tokens passed to this function refer to the leaves
2952  * in the schedule tree where the accesses take place.
2953  *
2954  * If n is the shared number of loops, then we need to return
2955  * "2 * n + 1" if "first" precedes "second" inside the innermost
2956  * shared loop and "2 * n" otherwise.
2957  *
2958  * The innermost shared ancestor may be the leaves themselves
2959  * if the accesses take place in the same leaf.  Otherwise,
2960  * it is either a set node or a sequence node.  Only in the case
2961  * of a sequence node do we consider one access to precede the other.
2962  */
before_node(void * first,void * second)2963 static int before_node(void *first, void *second)
2964 {
2965 	isl_schedule_node *node1 = first;
2966 	isl_schedule_node *node2 = second;
2967 	isl_schedule_node *shared;
2968 	isl_size depth;
2969 	int before = 0;
2970 
2971 	shared = isl_schedule_node_get_shared_ancestor(node1, node2);
2972 	depth = isl_schedule_node_get_schedule_depth(shared);
2973 	if (depth < 0) {
2974 		isl_schedule_node_free(shared);
2975 		return -1;
2976 	}
2977 
2978 	if (isl_schedule_node_get_type(shared) == isl_schedule_node_sequence) {
2979 		isl_size pos1, pos2;
2980 
2981 		pos1 = isl_schedule_node_get_ancestor_child_position(node1,
2982 								    shared);
2983 		pos2 = isl_schedule_node_get_ancestor_child_position(node2,
2984 								    shared);
2985 		if (pos1 < 0 || pos2 < 0) {
2986 			isl_schedule_node_free(shared);
2987 			return -1;
2988 		}
2989 		before = pos1 < pos2;
2990 	}
2991 
2992 	isl_schedule_node_free(shared);
2993 
2994 	return 2 * depth + before;
2995 }
2996 
2997 /* Check if the given two accesses may be coscheduled.
2998  * If so, return isl_bool_true.  Otherwise return isl_bool_false.
2999  *
3000  * Two accesses may only be coscheduled if they appear in the same leaf.
3001  */
coscheduled_node(void * first,void * second)3002 static isl_bool coscheduled_node(void *first, void *second)
3003 {
3004 	isl_schedule_node *node1 = first;
3005 	isl_schedule_node *node2 = second;
3006 
3007 	return isl_bool_ok(node1 == node2);
3008 }
3009 
3010 /* Add the scheduled sources from "data" that access
3011  * the same data space as "sink" to "access".
3012  */
add_matching_sources(__isl_take isl_access_info * access,struct isl_scheduled_access * sink,struct isl_compute_flow_schedule_data * data)3013 static __isl_give isl_access_info *add_matching_sources(
3014 	__isl_take isl_access_info *access, struct isl_scheduled_access *sink,
3015 	struct isl_compute_flow_schedule_data *data)
3016 {
3017 	int i;
3018 	isl_space *space;
3019 
3020 	space = isl_space_range(isl_map_get_space(sink->access));
3021 	for (i = 0; i < data->n_source; ++i) {
3022 		struct isl_scheduled_access *source;
3023 		isl_space *source_space;
3024 		int eq;
3025 
3026 		source = &data->source[i];
3027 		source_space = isl_map_get_space(source->access);
3028 		source_space = isl_space_range(source_space);
3029 		eq = isl_space_is_equal(space, source_space);
3030 		isl_space_free(source_space);
3031 
3032 		if (!eq)
3033 			continue;
3034 		if (eq < 0)
3035 			goto error;
3036 
3037 		access = isl_access_info_add_source(access,
3038 		    isl_map_copy(source->access), source->must, source->node);
3039 	}
3040 
3041 	isl_space_free(space);
3042 	return access;
3043 error:
3044 	isl_space_free(space);
3045 	isl_access_info_free(access);
3046 	return NULL;
3047 }
3048 
3049 /* Given a scheduled sink access relation "sink", compute the corresponding
3050  * dependences on the sources in "data" and add the computed dependences
3051  * to "uf".
3052  *
3053  * The dependences computed by access_info_compute_flow_core are of the form
3054  *
3055  *	[S -> I] -> [[S' -> I'] -> A]
3056  *
3057  * The schedule dimensions are projected out by first currying the range,
3058  * resulting in
3059  *
3060  *	[S -> I] -> [S' -> [I' -> A]]
3061  *
3062  * and then computing the factor range
3063  *
3064  *	I -> [I' -> A]
3065  */
compute_single_flow(__isl_take isl_union_flow * uf,struct isl_scheduled_access * sink,struct isl_compute_flow_schedule_data * data)3066 static __isl_give isl_union_flow *compute_single_flow(
3067 	__isl_take isl_union_flow *uf, struct isl_scheduled_access *sink,
3068 	struct isl_compute_flow_schedule_data *data)
3069 {
3070 	int i;
3071 	isl_access_info *access;
3072 	isl_flow *flow;
3073 	isl_map *map;
3074 
3075 	if (!uf)
3076 		return NULL;
3077 
3078 	access = isl_access_info_alloc(isl_map_copy(sink->access), sink->node,
3079 					&before_node, data->n_source);
3080 	if (access)
3081 		access->coscheduled = &coscheduled_node;
3082 	access = add_matching_sources(access, sink, data);
3083 
3084 	flow = access_info_compute_flow_core(access);
3085 	if (!flow)
3086 		return isl_union_flow_free(uf);
3087 
3088 	map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 1));
3089 	uf->must_no_source = isl_union_map_union(uf->must_no_source,
3090 						isl_union_map_from_map(map));
3091 	map = isl_map_domain_factor_range(isl_flow_get_no_source(flow, 0));
3092 	uf->may_no_source = isl_union_map_union(uf->may_no_source,
3093 						isl_union_map_from_map(map));
3094 
3095 	for (i = 0; i < flow->n_source; ++i) {
3096 		isl_union_map *dep;
3097 
3098 		map = isl_map_range_curry(isl_map_copy(flow->dep[i].map));
3099 		map = isl_map_factor_range(map);
3100 		dep = isl_union_map_from_map(map);
3101 		if (flow->dep[i].must)
3102 			uf->must_dep = isl_union_map_union(uf->must_dep, dep);
3103 		else
3104 			uf->may_dep = isl_union_map_union(uf->may_dep, dep);
3105 	}
3106 
3107 	isl_flow_free(flow);
3108 
3109 	return uf;
3110 }
3111 
3112 /* Given a description of the "sink" accesses, the "source" accesses and
3113  * a schedule, compute for each instance of a sink access
3114  * and for each element accessed by that instance,
3115  * the possible or definite source accesses that last accessed the
3116  * element accessed by the sink access before this sink access
3117  * in the sense that there is no intermediate definite source access.
3118  * Only consider dependences between statement instances that belong
3119  * to the domain of the schedule.
3120  *
3121  * The must_no_source and may_no_source elements of the result
3122  * are subsets of access->sink.  The elements must_dep and may_dep
3123  * map domain elements of access->{may,must)_source to
3124  * domain elements of access->sink.
3125  *
3126  * This function is used when a schedule tree representation
3127  * is available.
3128  *
3129  * We extract the individual scheduled source and sink access relations
3130  * (taking into account the domain of the schedule) and
3131  * then compute dependences for each scheduled sink individually.
3132  */
compute_flow_schedule(__isl_take isl_union_access_info * access)3133 static __isl_give isl_union_flow *compute_flow_schedule(
3134 	__isl_take isl_union_access_info *access)
3135 {
3136 	struct isl_compute_flow_schedule_data data = { access };
3137 	int i, n;
3138 	isl_ctx *ctx;
3139 	isl_space *space;
3140 	isl_union_flow *flow;
3141 
3142 	ctx = isl_union_access_info_get_ctx(access);
3143 
3144 	data.n_sink = 0;
3145 	data.n_source = 0;
3146 	if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3147 						&count_sink_source, &data) < 0)
3148 		goto error;
3149 
3150 	n = data.n_sink + data.n_source;
3151 	data.sink = isl_calloc_array(ctx, struct isl_scheduled_access, n);
3152 	if (n && !data.sink)
3153 		goto error;
3154 	data.source = data.sink + data.n_sink;
3155 
3156 	data.n_sink = 0;
3157 	data.n_source = 0;
3158 	if (isl_schedule_foreach_schedule_node_top_down(access->schedule,
3159 					    &collect_sink_source, &data) < 0)
3160 		goto error;
3161 
3162 	space = isl_union_map_get_space(access->access[isl_access_sink]);
3163 	flow = isl_union_flow_alloc(space);
3164 
3165 	isl_compute_flow_schedule_data_align_params(&data);
3166 
3167 	for (i = 0; i < data.n_sink; ++i)
3168 		flow = compute_single_flow(flow, &data.sink[i], &data);
3169 
3170 	isl_compute_flow_schedule_data_clear(&data);
3171 
3172 	isl_union_access_info_free(access);
3173 	return flow;
3174 error:
3175 	isl_union_access_info_free(access);
3176 	isl_compute_flow_schedule_data_clear(&data);
3177 	return NULL;
3178 }
3179 
3180 /* Given a description of the "sink" accesses, the "source" accesses and
3181  * a schedule, compute for each instance of a sink access
3182  * and for each element accessed by that instance,
3183  * the possible or definite source accesses that last accessed the
3184  * element accessed by the sink access before this sink access
3185  * in the sense that there is no intermediate definite source access.
3186  *
3187  * The must_no_source and may_no_source elements of the result
3188  * are subsets of access->sink.  The elements must_dep and may_dep
3189  * map domain elements of access->{may,must)_source to
3190  * domain elements of access->sink.
3191  *
3192  * If any kills have been specified, then they are treated as
3193  * must-sources internally.  Any dependence that purely derives
3194  * from an original kill is removed from the output.
3195  *
3196  * We check whether the schedule is available as a schedule tree
3197  * or a schedule map and call the corresponding function to perform
3198  * the analysis.
3199  */
isl_union_access_info_compute_flow(__isl_take isl_union_access_info * access)3200 __isl_give isl_union_flow *isl_union_access_info_compute_flow(
3201 	__isl_take isl_union_access_info *access)
3202 {
3203 	isl_bool has_kill;
3204 	isl_union_map *must = NULL, *may = NULL;
3205 	isl_union_flow *flow;
3206 
3207 	has_kill = isl_union_access_has_kill(access);
3208 	if (has_kill < 0)
3209 		goto error;
3210 	if (has_kill) {
3211 		must = isl_union_access_info_get_must_source(access);
3212 		may = isl_union_access_info_get_may_source(access);
3213 	}
3214 	access = isl_union_access_info_add_kill_to_must_source(access);
3215 	access = isl_union_access_info_normalize(access);
3216 	if (!access)
3217 		goto error;
3218 	if (access->schedule)
3219 		flow = compute_flow_schedule(access);
3220 	else
3221 		flow = compute_flow_union_map(access);
3222 	if (has_kill)
3223 		flow = isl_union_flow_drop_kill_source(flow, must, may);
3224 	return flow;
3225 error:
3226 	isl_union_access_info_free(access);
3227 	isl_union_map_free(must);
3228 	isl_union_map_free(may);
3229 	return NULL;
3230 }
3231 
3232 /* Print the information contained in "flow" to "p".
3233  * The information is printed as a YAML document.
3234  */
isl_printer_print_union_flow(__isl_take isl_printer * p,__isl_keep isl_union_flow * flow)3235 __isl_give isl_printer *isl_printer_print_union_flow(
3236 	__isl_take isl_printer *p, __isl_keep isl_union_flow *flow)
3237 {
3238 	isl_union_map *umap;
3239 
3240 	if (!flow)
3241 		return isl_printer_free(p);
3242 
3243 	p = isl_printer_yaml_start_mapping(p);
3244 	umap = isl_union_flow_get_full_must_dependence(flow);
3245 	p = print_yaml_field_union_map(p, "must_dependence", umap);
3246 	isl_union_map_free(umap);
3247 	umap = isl_union_flow_get_full_may_dependence(flow);
3248 	p = print_yaml_field_union_map(p, "may_dependence", umap);
3249 	isl_union_map_free(umap);
3250 	p = print_yaml_field_union_map(p, "must_no_source",
3251 					flow->must_no_source);
3252 	umap = isl_union_flow_get_may_no_source(flow);
3253 	p = print_yaml_field_union_map(p, "may_no_source", umap);
3254 	isl_union_map_free(umap);
3255 	p = isl_printer_yaml_end_mapping(p);
3256 
3257 	return p;
3258 }
3259 
3260 /* Return a string representation of the information in "flow".
3261  * The information is printed in flow format.
3262  */
isl_union_flow_to_str(__isl_keep isl_union_flow * flow)3263 __isl_give char *isl_union_flow_to_str(__isl_keep isl_union_flow *flow)
3264 {
3265 	isl_printer *p;
3266 	char *s;
3267 
3268 	if (!flow)
3269 		return NULL;
3270 
3271 	p = isl_printer_to_str(isl_union_flow_get_ctx(flow));
3272 	p = isl_printer_set_yaml_style(p, ISL_YAML_STYLE_FLOW);
3273 	p = isl_printer_print_union_flow(p, flow);
3274 	s = isl_printer_get_str(p);
3275 	isl_printer_free(p);
3276 
3277 	return s;
3278 }
3279 
3280 /* Given a collection of "sink" and "source" accesses,
3281  * compute for each iteration of a sink access
3282  * and for each element accessed by that iteration,
3283  * the source access in the list that last accessed the
3284  * element accessed by the sink access before this sink access.
3285  * Each access is given as a map from the loop iterators
3286  * to the array indices.
3287  * The result is a relations between source and sink
3288  * iterations and a subset of the domain of the sink accesses,
3289  * corresponding to those iterations that access an element
3290  * not previously accessed.
3291  *
3292  * We collect the inputs in an isl_union_access_info object,
3293  * call isl_union_access_info_compute_flow and extract
3294  * the outputs from the result.
3295  */
isl_union_map_compute_flow(__isl_take isl_union_map * sink,__isl_take isl_union_map * must_source,__isl_take isl_union_map * may_source,__isl_take isl_union_map * schedule,__isl_give isl_union_map ** must_dep,__isl_give isl_union_map ** may_dep,__isl_give isl_union_map ** must_no_source,__isl_give isl_union_map ** may_no_source)3296 int isl_union_map_compute_flow(__isl_take isl_union_map *sink,
3297 	__isl_take isl_union_map *must_source,
3298 	__isl_take isl_union_map *may_source,
3299 	__isl_take isl_union_map *schedule,
3300 	__isl_give isl_union_map **must_dep, __isl_give isl_union_map **may_dep,
3301 	__isl_give isl_union_map **must_no_source,
3302 	__isl_give isl_union_map **may_no_source)
3303 {
3304 	isl_union_access_info *access;
3305 	isl_union_flow *flow;
3306 
3307 	access = isl_union_access_info_from_sink(sink);
3308 	access = isl_union_access_info_set_must_source(access, must_source);
3309 	access = isl_union_access_info_set_may_source(access, may_source);
3310 	access = isl_union_access_info_set_schedule_map(access, schedule);
3311 	flow = isl_union_access_info_compute_flow(access);
3312 
3313 	if (must_dep)
3314 		*must_dep = isl_union_flow_get_must_dependence(flow);
3315 	if (may_dep)
3316 		*may_dep = isl_union_flow_get_non_must_dependence(flow);
3317 	if (must_no_source)
3318 		*must_no_source = isl_union_flow_get_must_no_source(flow);
3319 	if (may_no_source)
3320 		*may_no_source = isl_union_flow_get_non_must_no_source(flow);
3321 
3322 	isl_union_flow_free(flow);
3323 
3324 	if ((must_dep && !*must_dep) || (may_dep && !*may_dep) ||
3325 	    (must_no_source && !*must_no_source) ||
3326 	    (may_no_source && !*may_no_source))
3327 		goto error;
3328 
3329 	return 0;
3330 error:
3331 	if (must_dep)
3332 		*must_dep = isl_union_map_free(*must_dep);
3333 	if (may_dep)
3334 		*may_dep = isl_union_map_free(*may_dep);
3335 	if (must_no_source)
3336 		*must_no_source = isl_union_map_free(*must_no_source);
3337 	if (may_no_source)
3338 		*may_no_source = isl_union_map_free(*may_no_source);
3339 	return -1;
3340 }
3341