update barvinok to version 0.41.5
[ppn.git] / da.cc
blob0fd5d4aebe22e8b79fa8746ae5700280a5ff63a4
1 #include <set>
2 #include <vector>
3 #include <iostream>
5 #include <isa/yaml.h>
6 #include <isa/pdg.h>
7 #include "da.h"
9 extern "C" {
10 #include "isl_util.h"
12 #include <isl/id.h>
13 #include <isl/space.h>
14 #include <isl/aff.h>
15 #include <isl/set.h>
16 #include <isl/map.h>
17 #include <isl/union_set.h>
18 #include <isl/union_map.h>
19 #include <isl/flow.h>
21 using pdg::PDG;
22 using namespace std;
24 namespace da {
26 /* A pair of a node and an access from that node.
27 * "map" is the converted access relation.
28 * "projected_map" represents the converted access relation without
29 * any embedded access relation or access filters
31 struct na_pair {
32 pdg::node *node;
33 pdg::access *access;
34 isl_map *map;
35 isl_map *projected_map;
36 void project_out_access_filters(void);
37 na_pair(pdg::node *n, pdg::access *a) : node(n), access(a), map(NULL),
38 projected_map(NULL) {}
39 ~na_pair() {
40 isl_map_free(map);
41 isl_map_free(projected_map);
45 /* If the access has any embedded filters, then project them out
46 * from "projected_map", initializing "projected_map" from "map"
47 * if there is no "projected_map" yet.
49 void na_pair::project_out_access_filters(void)
51 isl_space *space;
52 isl_map *proj;
54 if (access->nested.size() == 0)
55 return;
57 if (!projected_map)
58 projected_map = isl_map_copy(map);
60 space = isl_space_domain(isl_map_get_space(projected_map));
61 space = isl_space_unwrap(space);
62 proj = isl_map_domain_map(isl_map_universe(space));
64 projected_map = isl_map_apply_domain(projected_map, proj);
67 static int precedes_level_nodes(na_pair *first, na_pair *second);
68 static int precedes_level_accesses(na_pair *first, na_pair *second);
70 /* Given a map from a domain to an orthogonal projection of an array
71 * (say, the rows of an array), mapping i to m(i), this function
72 * extends the range of the mapping to the original array and extends
73 * the domain of the mapping correspondingly such that (i,j) maps
74 * to (m(i),j), with (m(i),j) identifying an element of the array.
75 * The bounds on j are taken from the size of the array.
77 * The mapping from i to (i,j) is stored in the "extension" field
78 * of the access.
80 * The dependences computed using these extended access mappings,
81 * will map a (possibly) extended source domain to a (possibly)
82 * extended sink domain. One or both of these domains need to
83 * be transformed back to the original domains using the inverse
84 * of the corresponding extensions.
86 static isl_map *extend_access(isl_map *map, na_pair *na)
88 pdg::array *array = na->access->array;
89 unsigned n_out = isl_map_dim(map, isl_dim_out);
90 assert(n_out < array->dims.size());
91 unsigned s_dim = array->dims.size() - n_out;
92 isl_id *array_id = NULL;
93 if (isl_map_has_tuple_id(map, isl_dim_out))
94 array_id = isl_map_get_tuple_id(map, isl_dim_out);
95 isl_space *space = isl_map_get_space(map);
96 space = isl_space_drop_dims(space, isl_dim_in,
97 0, isl_space_dim(space, isl_dim_in));
98 space = isl_space_drop_dims(space, isl_dim_out,
99 0, isl_space_dim(space, isl_dim_out));
100 space = isl_space_add_dims(space, isl_dim_in, s_dim);
101 space = isl_space_add_dims(space, isl_dim_out, s_dim);
102 isl_basic_map *id = isl_basic_map_identity(space);
103 for (int i = 0; i < s_dim; ++i) {
104 int v;
106 id = isl_basic_map_lower_bound_si(id, isl_dim_out, i, 0);
107 v = array->dims[n_out + i] - 1;
108 id = isl_basic_map_upper_bound_si(id, isl_dim_out, i, v);
110 map = isl_map_product(map, isl_map_from_basic_map(id));
111 map = isl_map_flatten_range(map);
112 if (array_id)
113 map = isl_map_set_tuple_id(map, isl_dim_out, array_id);
114 if (!na->access->extension) {
115 isl_map *ext = isl_map_copy(map);
116 ext = isl_set_unwrap(isl_map_domain(ext));
117 ext = isl_map_reverse(isl_map_domain_map(ext));
118 na->access->extension = new pdg::IslMap(ext);
120 if (!na->access->extended_map)
121 na->access->extended_map = new pdg::IslMap(isl_map_copy(map));
122 return map;
125 /* If access "access" contains any nested accesses, then the domain
126 * of the access relation contains extra dimensions corresponding to
127 * the values of the nested accesses.
128 * Add these extra dimensions, with ranges given by the value_bounds
129 * of the corresponding array to domain "dom".
130 * If a nested access array does not have value_bounds, then we assume
131 * an infinite interval.
133 static isl_set *append_nested_value_domains(isl_set *dom, pdg::access *access)
135 isl_ctx *ctx;
137 ctx = isl_set_get_ctx(dom);
138 for (int i = 0; i < access->nested.size(); ++i) {
139 pdg::call_or_access *coa = access->nested[i];
140 assert(coa->type == pdg::call_or_access::t_access);
141 pdg::access *nested = coa->access;
142 isl_set *bounds;
143 if (nested->array->value_bounds)
144 bounds = nested->array->value_bounds->get_isl_set(ctx);
145 else {
146 isl_space *dim = isl_set_get_space(dom);
147 dim = isl_space_drop_dims(dim, isl_dim_set,
148 0, isl_space_dim(dim, isl_dim_set));
149 dim = isl_space_add_dims(dim, isl_dim_set, 1);
150 bounds = isl_set_universe(dim);
152 dom = isl_set_product(dom, bounds);
154 return dom;
157 /* Combine constraints of the "pure" mapping with the constraints
158 * on the domain. If the range of the mapping is of a dimension
159 * that is lower than the dimension of the accessed array,
160 * we extend the dimension of both domain and range of the mapping
161 * with the missing dimension. The size of domain and range
162 * in these dimensions is set to the extent of the array in the
163 * corresponding missing dimension. Each point in the original
164 * domain is therefore expanded to a hyperrectangle and each point
165 * in this hyperrectangle is mapped onto a single point in the array.
167 * If node->source is a wrapped map, then the iteration domain
168 * is the domain of this map.
170 static isl_map *convert_access(na_pair *na)
172 isl_map *map = na->access->map->get_isl_map();
173 isl_set *dom = na->node->source->get_isl_set();
175 if (isl_set_is_wrapping(dom)) {
176 dom = isl_map_domain(isl_set_unwrap(dom));
177 dom = isl_set_coalesce(dom);
180 dom = append_nested_value_domains(dom, na->access);
181 if (isl_map_dim(map, isl_dim_in) != isl_set_dim(dom, isl_dim_set))
182 assert(0);
183 map = isl_map_intersect_domain(map, dom);
184 if (isl_map_dim(map, isl_dim_out) != na->access->array->dims.size())
185 map = extend_access(map, na);
186 return map;
189 typedef std::map<na_pair *, isl_map *> na_pair2map;
191 struct add_dep_info {
192 PDG *pdg;
193 pdg::array *a;
194 type t;
195 enum pdg::dependence::type dtype;
196 /* The sink. */
197 na_pair *read_na_pair;
198 /* The comparison routine that was used during
199 * the dependence analysis.
201 isl_access_level_before precedes_level;
202 /* Cache of memory based dependence relations.
203 * The key of the map refers to the write.
205 na_pair2map mem_dep;
206 /* How many loops are shared by the current sink and source?
207 * Initialized to -1.
209 int n_shared;
210 /* For each potential source, what's (up to now) the minimal
211 * and maximal number of shared loops?
212 * If not set, then we don't know yet.
214 std::map<na_pair *, int> min_n_shared;
215 std::map<na_pair *, int> max_n_shared;
217 /* Potential sources that are actually used. */
218 std::set<na_pair *> used;
220 __isl_give isl_map *get_mem_dep(na_pair *write_na);
221 void clear_mem_dep();
222 void set_read_na(na_pair *read_na);
223 void update_min_n_shared(na_pair *source_na);
225 ~add_dep_info();
228 /* Update min_n_shared of "source_na" to the current number of shared loops.
229 * The new value is always smaller than or equal to the old value (if any).
230 * If max_n_shared hasn't been set yet, then set it as well.
232 void add_dep_info::update_min_n_shared(na_pair *source_na)
234 if (max_n_shared.find(source_na) == max_n_shared.end())
235 max_n_shared[source_na] = n_shared;
236 min_n_shared[source_na] = n_shared;
239 /* Create an id
241 * __last_<stmt>_<access_nr>_valid
243 * corresponding to "na", with "na" attached as user pointer.
245 static __isl_give isl_id *valid_bit_id(isl_ctx *ctx, na_pair *na)
247 char name[60];
249 snprintf(name, sizeof(name), "__last_%s_%d_valid",
250 na->node->name->s.c_str(), na->access->nr);
251 return isl_id_alloc(ctx, name, na);
254 /* Create an id
256 * __last_<stmt>_<access_nr>_shared
258 * corresponding to "na", with "na" attached as user pointer.
260 static __isl_give isl_id *create_shared_id(isl_ctx *ctx, na_pair *na)
262 char name[60];
264 snprintf(name, sizeof(name), "__last_%s_%d_shared",
265 na->node->name->s.c_str(), na->access->nr);
266 return isl_id_alloc(ctx, name, na);
269 /* Project out all the dimensions of the given type from "map" except "pos".
271 static __isl_give isl_map *project_on(__isl_take isl_map *map,
272 enum isl_dim_type type, unsigned pos)
274 unsigned n = isl_map_dim(map, type);
276 map = isl_map_project_out(map, type, pos + 1, n - (pos + 1));
277 map = isl_map_project_out(map, type, 0, pos);
279 return map;
282 /* Does output dimension "pos" have a fixed value in terms of the
283 * input dimensions (and parameters)?
285 static int has_fixed_value(__isl_keep isl_map *map, int pos)
287 int sv;
289 map = isl_map_copy(map);
290 map = project_on(map, isl_dim_out, pos);
291 sv = isl_map_is_single_valued(map);
292 isl_map_free(map);
294 return sv;
297 /* Return the position of the parameter with the given "id" in "set",
298 * adding it if it wasn't there already.
300 static int find_or_add_param(__isl_keep isl_set **set, __isl_take isl_id *id)
302 int pos;
304 pos = isl_set_find_dim_by_id(*set, isl_dim_param, id);
305 if (pos >= 0) {
306 isl_id_free(id);
307 return pos;
310 pos = isl_set_dim(*set, isl_dim_param);
311 *set = isl_set_add_dims(*set, isl_dim_param, 1);
312 *set = isl_set_set_dim_id(*set, isl_dim_param, pos, id);
314 return pos;
317 /* Add parameters to "set" identifying the last iteration of the access
318 * identified by "na".
320 * In particular, we add a parameter
322 * __last_<stmt>_<access_nr>_shared >= info->n_shared
324 * and parameters
326 * __last_<stmt>_<access_nr>_<i> = it_<i>
328 * with i ranging over the iterators, starting at info->n_shared,
329 * that are affected by the filters,
330 * except those that have a fixed value according to the memory based
331 * dependence.
332 * "na" is attached to the first two parameters, so that it can be recovered
333 * in refine_controls(). If the set already references some of these
334 * parameters, then we don't add the parameter again, but instead
335 * simply add the corresponding constraint.
337 static __isl_give isl_set *add_parametrization(__isl_take isl_set *set,
338 na_pair *na, add_dep_info *info)
340 int pos;
341 isl_ctx *ctx;
342 isl_id *id;
343 char name[60];
344 int depth;
345 isl_map *mem;
347 depth = na->node->get_filter_depth();
348 mem = info->get_mem_dep(na);
349 mem = isl_map_reverse(mem);
351 ctx = isl_set_get_ctx(set);
352 id = create_shared_id(ctx, na);
353 pos = find_or_add_param(&set, id);
354 set = isl_set_lower_bound_si(set, isl_dim_param, pos, info->n_shared);
356 for (int i = info->n_shared; i < depth; ++i) {
357 if (has_fixed_value(mem, i))
358 continue;
360 snprintf(name, sizeof(name), "__last_%s_%d_%d",
361 na->node->name->s.c_str(), na->access->nr, i);
363 id = isl_id_alloc(ctx, name, NULL);
364 pos = find_or_add_param(&set, id);
366 set = isl_set_equate(set, isl_dim_param, pos, isl_dim_set, i);
369 isl_map_free(mem);
370 return set;
373 /* Is the i-th parameter of "map" a control, i.e., a parameter
374 * introduced by add_parametrization()?
375 * In particular, is the parameter of the form __last_*?
377 static bool is_control(__isl_keep isl_map *map, int i)
379 const char *name;
380 const char *prefix = "__last_";
381 size_t prefix_len = strlen(prefix);
383 if (!isl_map_has_dim_id(map, isl_dim_param, i))
384 return false;
385 name = isl_map_get_dim_name(map, isl_dim_param, i);
386 return strncmp(name, prefix, prefix_len) == 0;
389 /* Is the i-th parameter of "set" a control, i.e., a parameter
390 * introduced by add_parametrization()?
391 * In particular, is the parameter of the form __last_*?
393 static bool is_control(__isl_keep isl_set *set, int i)
395 const char *name;
396 const char *prefix = "__last_";
397 size_t prefix_len = strlen(prefix);
399 if (!isl_set_has_dim_id(set, isl_dim_param, i))
400 return false;
401 name = isl_set_get_dim_name(set, isl_dim_param, i);
402 return strncmp(name, prefix, prefix_len) == 0;
405 /* Remove all controls that are redundant, i.e., that do not appear
406 * in any of the constraints.
407 * Set *has_controls to true if there are any controls that are not redundant.
409 static __isl_give isl_map *remove_redundant_controls(__isl_take isl_map *dep,
410 bool *has_controls)
412 int i;
413 int n_param;
415 *has_controls = false;
417 n_param = isl_map_dim(dep, isl_dim_param);
418 for (i = n_param - 1; i >= 0; --i) {
419 if (!is_control(dep, i))
420 continue;
421 if (isl_map_involves_dims(dep, isl_dim_param, i, 1))
422 *has_controls = true;
423 else
424 dep = isl_map_project_out(dep, isl_dim_param, i, 1);
427 return dep;
430 /* Rename controls of "dep" from
432 * __last_<source_stmt>_<source_acc_nr>_*
434 * to
436 * __last_<source_stmt>_<source_acc_nr>_<sink_stmt>_<sink_acc_nr>_*
438 * "na" represents the sink.
440 static __isl_give isl_map *rename_controls(__isl_take isl_map *dep, na_pair *na)
442 char buf[100];
443 int n_param;
444 const char *name;
445 const char *underscore;
447 n_param = isl_map_dim(dep, isl_dim_param);
448 for (int i = 0; i < n_param; ++i) {
449 int len;
450 if (!is_control(dep, i))
451 continue;
452 name = isl_map_get_dim_name(dep, isl_dim_param, i);
453 underscore = strrchr(name, '_');
454 assert(underscore);
455 len = underscore + 1 - name;
456 memcpy(buf, name, len);
457 snprintf(buf + len, sizeof(buf) - len, "%s_%d_%s",
458 na->node->name->s.c_str(), na->access->nr, name + len);
459 dep = isl_map_set_dim_name(dep, isl_dim_param, i, buf);
462 return dep;
465 extern "C" {
466 static isl_stat extract_dep(__isl_take isl_map *dep, int must,
467 void *dep_user, void *user);
470 /* Extract the single dependence relation from the result of
471 * dataflow analyis and assign it to *user.
473 static isl_stat extract_dep(__isl_take isl_map *dep, int must, void *dep_user,
474 void *user)
476 isl_map **dep_p = (isl_map **) user;
477 assert(!*dep_p);
478 *dep_p = dep;
479 return isl_stat_ok;
482 /* Return the memory based dependence relation from write_na
483 * to read_na_pair. If the "projected_map"
484 * fields are not NULL, then use the "projected_map"
485 * instead of the "map" of write_na and this->read_na_pair.
487 __isl_give isl_map *add_dep_info::get_mem_dep(na_pair *write_na)
489 isl_access_info *acc;
490 isl_flow *deps;
491 isl_map *read_map, *write_map;
492 isl_map *dep = NULL;
494 if (mem_dep.find(write_na) != mem_dep.end())
495 return isl_map_copy(mem_dep[write_na]);
497 if (read_na_pair->projected_map)
498 read_map = isl_map_copy(read_na_pair->projected_map);
499 else
500 read_map = isl_map_copy(read_na_pair->map);
501 acc = isl_access_info_alloc(read_map, read_na_pair, precedes_level, 1);
502 if (write_na->projected_map)
503 write_map = isl_map_copy(write_na->projected_map);
504 else
505 write_map = isl_map_copy(write_na->map);
506 acc = isl_access_info_add_source(acc, write_map, 0, write_na);
507 deps = isl_access_info_compute_flow(acc);
508 isl_flow_foreach(deps, &extract_dep, &dep);
509 isl_flow_free(deps);
511 mem_dep[write_na] = isl_map_copy(dep);
513 return dep;
516 /* Clear the cache of memory based dependence relations.
518 void add_dep_info::clear_mem_dep()
520 na_pair2map::iterator it;
522 for (it = mem_dep.begin(); it != mem_dep.end(); ++it)
523 isl_map_free(it->second);
524 mem_dep.clear();
527 /* Set read_na_pair to read_na.
529 * If the cache of memory based dependence relations contains any
530 * elements then they refer to a different read, so we need to clear
531 * the cache.
533 * We also clear the set of used potential sources and reset
534 * the data that keeps track of the number of shared loops between
535 * the sink (read_na_pair) and the sources.
537 void add_dep_info::set_read_na(na_pair *read_na)
539 used.clear();
540 clear_mem_dep();
541 n_shared = -1;
542 min_n_shared.clear();
543 max_n_shared.clear();
544 read_na_pair = read_na;
547 add_dep_info::~add_dep_info()
549 clear_mem_dep();
552 /* Is the name of parameter "i" of "space" of the form __last_*_suffix?
554 static bool is_last_with_suffix(__isl_keep isl_space *space, int i,
555 const char *suffix, size_t suffix_len)
557 const char *prefix = "__last_";
558 size_t prefix_len = strlen(prefix);
559 const char *name;
560 size_t len;
562 if (!isl_space_has_dim_id(space, isl_dim_param, i))
563 return false;
564 name = isl_space_get_dim_name(space, isl_dim_param, i);
565 if (strncmp(name, prefix, prefix_len))
566 return false;
567 len = strlen(name);
568 return len > suffix_len && !strcmp(name + len - suffix_len, suffix);
571 /* Is the name of parameter "i" of "space" of the form __last_*_valid?
572 * In practice, those are the parameters __last_*_valid, created
573 * in add_parametrization().
575 static bool is_valid_bit(__isl_keep isl_space *space, int i)
577 const char *suffix = "_valid";
579 return is_last_with_suffix(space, i, suffix, strlen(suffix));
582 /* Is the name of parameter "i" of "space" of the form __last_*_shared?
583 * In practice, those are the parameters __last_*_shared, created
584 * in add_parametrization().
586 static bool is_shared(__isl_keep isl_space *space, int i)
588 const char *suffix = "_shared";
589 size_t suffix_len = strlen(suffix);
591 return is_last_with_suffix(space, i, suffix, strlen(suffix));
594 /* Assuming "coa" is a (read) access, return the array being
595 * accessed.
597 static pdg::array *get_filter_array(pdg::call_or_access *coa)
599 assert(coa->type == pdg::call_or_access::t_access);
600 return coa->access->array;
603 /* Compute a map between domain elements (i) of "map1" and range elements
604 * of "map2" (j) such that all the images of i in "map1" map to j through
605 * "map2" and such that there is at least one such image element.
607 * In other words, the result contains those pairs of elements such that
608 * map1(i) \cap map2^-1(j) is non-empty and map1(i) \subseteq map2^-1(j).
610 * Equivalently, compute
612 * (map1 . map2) \setminus
613 * (map1 . ((\range map1 \to \range map2) \setminus map2))
615 * If map1 is single valued, then we can do a simple join.
617 static __isl_give isl_union_map *join_non_empty_subset(
618 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
620 isl_union_set *dom, *ran;
621 isl_union_map *univ;
622 isl_union_map *res;
624 if (isl_union_map_is_single_valued(umap1))
625 return isl_union_map_apply_range(umap1, umap2);
627 res = isl_union_map_apply_range(isl_union_map_copy(umap1),
628 isl_union_map_copy(umap2));
629 dom = isl_union_map_range(isl_union_map_copy(umap1));
630 ran = isl_union_map_range(isl_union_map_copy(umap2));
631 univ = isl_union_map_from_domain_and_range(dom, ran);
632 umap2 = isl_union_map_subtract(univ, umap2);
633 umap1 = isl_union_map_apply_range(umap1, umap2);
634 res = isl_union_map_subtract(res, umap1);
636 return res;
639 /* Compute a map between domain elements (i) of "map1" and range elements
640 * of "map2" (j) such that the images of i in "map1" include all those
641 * elements that map to j through "map2" and such that there is
642 * at least one such image element.
644 * In other words, the result contains those pairs of elements such that
645 * map1(i) \cap map2^-1(j) is non-empty and map1(i) \supseteq map2^-1(j).
647 * Equivalently, compute
649 * (map1 . map2) \setminus
650 * (((\domain map1 \to \domain map2) \setminus map1) . map2)
652 * If map1 is single valued, then we can do a simple join.
654 static __isl_give isl_union_map *join_non_empty_superset(
655 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
657 isl_union_set *dom, *ran;
658 isl_union_map *univ;
659 isl_union_map *res;
661 if (isl_union_map_is_single_valued(umap2))
662 return isl_union_map_apply_range(umap1, umap2);
664 res = isl_union_map_apply_range(isl_union_map_copy(umap1),
665 isl_union_map_copy(umap2));
666 dom = isl_union_map_domain(isl_union_map_copy(umap1));
667 ran = isl_union_map_domain(isl_union_map_copy(umap2));
668 univ = isl_union_map_from_domain_and_range(dom, ran);
669 umap1 = isl_union_map_subtract(univ, umap1);
670 umap1 = isl_union_map_apply_range(umap1, umap2);
671 res = isl_union_map_subtract(res, umap1);
673 return res;
676 /* Return those elements in the domain of "umap" where "umap" is multi-valued.
678 * In particular, construct a mapping between domain elements of "umap"
679 * and pairs of corresponding image elements.
680 * Remove pairs of identical image elements from the range of this mapping.
681 * The result is a mapping between domain elements and pairs of different
682 * corresponding image elements. The domain of this mapping contains those
683 * domain elements of "umap" with at least two images.
685 static __isl_give isl_union_set *multi_valued(__isl_keep isl_union_map *umap)
687 isl_union_map *multi, *id;
689 multi = isl_union_map_range_product(isl_union_map_copy(umap),
690 isl_union_map_copy(umap));
691 id = isl_union_map_universe(isl_union_map_copy(multi));
692 id = isl_union_set_unwrap(isl_union_map_range(id));
693 id = isl_union_set_identity(isl_union_map_domain(id));
694 multi = isl_union_map_subtract_range(multi, isl_union_map_wrap(id));
695 return isl_union_map_domain(multi);
698 /* Given two filter access relations, return a mapping between the domain
699 * elements of these access relations such that they access "the same filter".
700 * In particular, any pair of elements in the returned relation
701 * accesses at least one element in common, but if subset1 is set,
702 * then the set of elements accessed by the first is a subset of the
703 * set of elements accessed by the second. Similarly, if subset2 is set,
704 * then the set of elements accessed by the second is a subset of the
705 * set of elements accessed by the first. If both are set, then we further
706 * impose that both should access exactly one element.
707 * "space" is the space in which the result should live.
708 * Although "map1" and "map2" are allowed to have ranges in multiple spaces,
709 * their domains should live in a single space. "space" is the space
710 * of the relation between those two domains.
712 * Call the given maps A and B.
714 * A relation between domains elements of A and B that access at least
715 * one element in common can be obtained as
717 * A . B^-1
719 * To ensure that all elements accessed through A form a subset of
720 * the elements accessed through B, we compute join_non_empty_subset(A, B^-1).
722 * Ensuring that all elements accessed through B form a subset of
723 * the elements accessed through A is handled in a similar way.
725 * To remove those iterations that access more that one element,
726 * we compute those parts of the domains where A and B are multi-valued
727 * and subtract them from domain and range of the result.
729 static __isl_give isl_map *compute_common_filter(__isl_keep isl_union_map *map1,
730 bool subset1, __isl_keep isl_union_map *map2, bool subset2,
731 __isl_keep isl_space *space)
733 isl_union_map *reverse, *common;
734 isl_union_set *bad;
735 isl_map *res;
737 reverse = isl_union_map_reverse(isl_union_map_copy(map2));
739 if (subset1 && !subset2) {
740 common = join_non_empty_subset(isl_union_map_copy(map1),
741 reverse);
742 } else if (!subset1 && subset2) {
743 common = join_non_empty_superset(isl_union_map_copy(map1),
744 reverse);
745 } else {
746 common = isl_union_map_apply_range(isl_union_map_copy(map1),
747 reverse);
748 if (subset1 && subset2) {
749 bad = multi_valued(map1);
750 common = isl_union_map_subtract_domain(common, bad);
751 bad = multi_valued(map2);
752 common = isl_union_map_subtract_range(common, bad);
756 res = isl_union_map_extract_map(common, isl_space_copy(space));
757 isl_union_map_free(common);
758 return res;
761 /* Assuming "coa" is a (read) access, construct a union map from the domain
762 * of the access relation to the access relations of the corresponding
763 * writes. If we are unable to determine the corresponding writes, then
764 * return a map to the read access relation.
766 static __isl_give isl_union_map *extract_access_map(pdg::call_or_access *coa)
768 assert(coa->type == pdg::call_or_access::t_access);
769 return coa->access->extract_access_map();
772 /* Return a set that contains all possible filter values,
773 * where the possible values for a given filter is either as specified
774 * by the value_bounds property of the corresponding array or the universe.
776 static __isl_give isl_set *compute_filter_bounds(pdg::node *node)
778 isl_ctx *ctx;
779 isl_set *bounds;
781 ctx = isl_set_get_ctx(node->source->set);
783 bounds = isl_set_universe(isl_space_set_alloc(ctx, 0, 0));
784 for (int i = 0; i < node->filters.size(); ++i) {
785 isl_set *bnd;
786 pdg::array *array;
787 pdg::call_or_access *coa = node->filters[i];
788 assert(coa->type == pdg::call_or_access::t_access);
789 array = coa->access->array;
790 if (array->value_bounds)
791 bnd = array->value_bounds->get_isl_set();
792 else
793 bnd = isl_set_universe(isl_space_set_alloc(ctx, 0, 1));
794 bounds = isl_set_flat_product(bounds, bnd);
797 return bounds;
800 /* Return either the filter values themselves or their complement,
801 * taken with respect to the bounds on the filter values.
803 static __isl_give isl_map *compute_filter_values(pdg::node *node,
804 bool complement)
806 isl_set *bounds;
807 isl_map *value;
808 isl_map *res;
810 value = isl_set_unwrap(node->source->get_isl_set());
811 if (!complement)
812 return value;
814 bounds = compute_filter_bounds(node);
815 res = isl_map_from_domain_and_range(
816 isl_map_domain(isl_map_copy(value)), bounds);
817 res = isl_map_subtract(res, value);
819 return res;
822 /* Equate the first "n" input and output dimensions of "map"
823 * and return the result.
825 static __isl_give isl_map *share(__isl_take isl_map *map, int n)
827 for (int i = 0; i < n; ++i)
828 map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
830 return map;
833 /* Return the set of source iterations of "na" either at the last
834 * iteration (if valid is set) or after the last iteration
835 * (if valid is not set). "id" represents the control variable
836 * corresponding to the number of shared loops (__last_<na>_shared).
838 * In particlar, if valid is set, we return the set
840 * { S[i] : i = __last_<na> and
841 * __last_<na>_shared >= info->n_shared }
843 * If valid is not set, we return the set
845 * { S[i] : (i >> __last_<na> and __last_<na>_shared >= info->n_shared) or
846 * __last_<na>_shared < info->n_shared }
848 * That is, the iterations after the last if there is a last iteration
849 * with at least info->n_shared shared loops
850 * or just any iteration if there is no such last iteration.
852 * The lexicographic order i >> __last_<na> is imposed on the loop iterators
853 * that are affected by any filters.
855 static __isl_give isl_set *source_iterations(na_pair *na,
856 __isl_keep isl_id *id, bool valid, add_dep_info *info)
858 isl_space *space;
859 isl_set *set, *invalid;
860 isl_map *map_after;
861 int pos;
862 int depth;
864 space = isl_set_get_space(na->node->source->set);
865 space = isl_space_domain(isl_space_unwrap(space));
867 set = isl_set_universe(space);
868 set = add_parametrization(set, na, info);
870 if (valid)
871 return set;
873 depth = na->node->get_filter_depth();
875 space = isl_space_map_from_set(isl_set_get_space(set));
876 map_after = isl_map_lex_lt_first(space, depth);
877 map_after = share(map_after, info->n_shared);
878 map_after = isl_map_intersect_domain(map_after, set);
879 set = isl_map_range(map_after);
881 invalid = isl_set_universe(isl_set_get_space(set));
882 pos = isl_set_find_dim_by_id(invalid, isl_dim_param, id);
883 assert(pos >= 0);
884 invalid = isl_set_upper_bound_si(invalid,
885 isl_dim_param, pos, info->n_shared - 1);
886 set = isl_set_union(set, invalid);
888 return set;
891 /* Look for a matching between the filters of node1 and those of node2.
892 * That is look for pairs of filters of the two nodes that are "the same".
893 * Return true if any such matching can be found. The correspondence between
894 * the filters is returned in *same_value_p, while the pairs of iterations
895 * where the filters are the same is returned in *same_filter_p.
896 * The first "n_shared" dimensions of these iterations are guaranteed
897 * to be equal to each other.
899 * Two filter accesses are considered "the same" if they access at least
900 * one element in common. Moreover, if valid1 is false then the set
901 * of elements accessed by an element from node1 should be a subset
902 * of the set of elements accessed by the corresponding element from node2.
903 * Similarly for valid2.
905 * We perform a greedy search, checking if two filters could possibly
906 * match given the matchings we have performed before and updating
907 * the matching if it is indeed possible.
909 * Note that this function only computes one of the possibly many matchings.
911 static bool compute_matching(pdg::node *node1, bool valid1,
912 pdg::node *node2, bool valid2, __isl_give isl_map **same_filter_p,
913 __isl_give isl_map **same_value_p, int n_shared)
915 bool any = false;
916 isl_space *space1, *space2;
917 isl_map *same_filter;
918 isl_map *same_value;
920 space1 = isl_space_unwrap(isl_set_get_space(node1->source->set));
921 space2 = isl_space_unwrap(isl_set_get_space(node2->source->set));
922 space1 = isl_space_product(space1, space2);
923 space2 = isl_space_unwrap(isl_space_range(isl_space_copy(space1)));
924 space1 = isl_space_unwrap(isl_space_domain(space1));
926 same_filter = isl_map_universe(isl_space_copy(space1));
927 same_filter = share(same_filter, n_shared);
928 same_value = isl_map_universe(space2);
930 for (int i = 0; i < node1->filters.size(); ++i) {
931 isl_union_map *map_i;
932 pdg::call_or_access *filter_i = node1->filters[i];
933 pdg::array *array_i = get_filter_array(filter_i);
935 map_i = extract_access_map(node1->filters[i]);
937 for (int j = 0; j < node2->filters.size(); ++j) {
938 pdg::call_or_access *filter_j;
939 filter_j = node2->filters[j];
940 pdg::array *array_j = get_filter_array(filter_j);
941 isl_union_map *map_j;
942 isl_map *same_filter_ij;
944 if (array_i != array_j)
945 continue;
947 map_j = extract_access_map(node2->filters[j]);
948 same_filter_ij = compute_common_filter(map_i, !valid1,
949 map_j, !valid2,
950 space1);
951 same_filter_ij = isl_map_intersect(same_filter_ij,
952 isl_map_copy(same_filter));
953 if (isl_map_is_empty(same_filter_ij))
954 isl_map_free(same_filter_ij);
955 else {
956 any = true;
957 isl_map_free(same_filter);
958 same_filter = same_filter_ij;
959 same_value = isl_map_equate(same_value,
960 isl_dim_in, i,
961 isl_dim_out, j);
963 isl_union_map_free(map_j);
966 isl_union_map_free(map_i);
968 isl_space_free(space1);
970 if (any) {
971 *same_value_p = same_value;
972 *same_filter_p = same_filter;
973 } else {
974 isl_map_free(same_value);
975 isl_map_free(same_filter);
976 *same_value_p = NULL;
977 *same_filter_p = NULL;
980 return any;
983 /* Given a set of sink iterations "sink", mappings "map1" and "map2"
984 * from two potential sources to this sink,
985 * the possible filter values "value1" and "value2" at those
986 * potential sources, a relation "same_filter" between the two
987 * potential sources expressing when some filters of the two
988 * potential sources are the same and the correponding matching
989 * "same_value" between the filter values,
990 * remove those elements from the sink that have
991 * corresponding pairs of potential source iterations that should
992 * have the same filter values but do not.
994 * Let us call the sink S, the potential sources A and B and the
995 * corresponding filters F and G.
997 * We start from the mappings A[..] -> S[..] and B[..] -> S[..],
998 * combine them into
1000 * S[..] -> [A[..] -> B[..]]
1002 * and intersect the range with the condition "same_filter" on A and B,
1003 * resulting in a mapping from sink iterations to pairs of potential
1004 * source iterations that should have the same filter values
1005 * (as specified by "same_value").
1007 * We subtract from the range of this mapping those pairs of
1008 * potential source iterations that actually have the same filter values.
1009 * The result is a mapping from sink iterations to pairs of potential
1010 * source iterations that should have the same filter values but do not.
1012 * The mapping between potential source iterations that have the
1013 * same filter values is obtained by combining the mappings
1014 * A[..] -> F[..] and B[..] -> G[..] into
1016 * [A[..] -> B[..]] -> [F[..] -> G[..]]
1018 * intersecting the range with "same_value" and then computing the domain.
1020 static __isl_give isl_set *remove_conflict(__isl_take isl_set *sink,
1021 __isl_take isl_map *map1, __isl_take isl_map *value1,
1022 __isl_take isl_map *map2, __isl_take isl_map *value2,
1023 __isl_take isl_map *same_filter, __isl_take isl_map *same_value)
1025 isl_map *value, *conflict;
1026 isl_set *conflict_set;
1028 conflict = isl_map_domain_product(map1, map2);
1029 conflict = isl_map_reverse(conflict);
1031 conflict = isl_map_intersect_range(conflict, isl_map_wrap(same_filter));
1033 value = isl_map_product(value1, value2);
1034 value = isl_map_intersect_range(value, isl_map_wrap(same_value));
1035 conflict = isl_map_subtract_range(conflict, isl_map_domain(value));
1037 conflict_set = isl_map_domain(conflict);
1039 sink = isl_set_subtract(sink, conflict_set);
1041 return sink;
1044 /* Remove inconsistencies from the set of sink iterations "sink"
1045 * based on two potential sources identified by "id1" and "id2"
1046 * (representing the number of shared loops),
1047 * in particular, on either the last iteration where the filters hold
1048 * (if valid? is set) or on later iterations (if valid? is not set).
1050 * Let us first consider the case where both "valid1" and "valid2" are set.
1051 * If the last iterations of the corresponding sources access the same
1052 * filters, then these filters should have the same value.
1053 * If a filter access accesses more than one element, then these elements
1054 * should all have the same value. It is therefore sufficient for the
1055 * two last iterations to access at least one element in common for there
1056 * to be a requirement that the corresponding values should be the same.
1057 * We therefore obtain the filter values, the mappings from the sink
1058 * to the last iterations, a matching between the
1059 * the filters of the corresponding sources and remove conflicts from "dep".
1061 * If one or both of the valid bits are not set, then we need to make
1062 * some changes. First the inconsistencies now do not arise from
1063 * the filter values at the last iteration, but from the filter values
1064 * lying _outside_ of the possible values for all iterations _after_
1065 * the "last" (i.e., the last iteration satisfying the filter constraints).
1066 * In case there is no last iteration with at least info->n_shared shared loops,
1067 * then the filter values should lie outside of the possible values
1068 * for any potential source iteration with info->n_shared shared loops.
1069 * Note however, that if the filter access relation accesses several
1070 * elements, then it is sufficient for one of those to have a value
1071 * outside of the possible values. We can therefore only consider
1072 * any inconsistencies for those cases where the set of accessed elements
1073 * forms a subset of the set of accessed elements through the other potential
1074 * source. If valid1 is not set, but valid2 is set, then we consider
1075 * those pairs of potential source iterations where the first accesses
1076 * a subset of the second and we impose that at least one of those
1077 * accessed elements has a valid outside the possible values.
1078 * Since those accessed elements form a subset of the elements accessed
1079 * by the other potential source, there is at least one element that
1080 * has a value outside of the posssible values on the first potential source
1081 * and a value belonging to the posssible values on the second potential source.
1082 * We can therefore impose that this value should exist.
1084 * If both valid1 and valid2 are not set, then we can only
1085 * impose a constraint on those pairs of iterations that access the same
1086 * single element. We then know that the value of this single element
1087 * accessed by both potential sources should lie outside of the possible
1088 * values on both sides.
1090 static __isl_give isl_set *remove_inconsistencies(__isl_take isl_set *sink,
1091 add_dep_info *info, __isl_keep isl_id *id1, bool valid1,
1092 __isl_keep isl_id *id2, bool valid2)
1094 na_pair *write1_na, *write2_na;
1095 isl_set *source;
1096 isl_map *value1, *value2;
1097 isl_map *same_filter;
1098 isl_map *same_value;
1099 isl_map *mem1, *mem2;
1101 write1_na = (na_pair *) isl_id_get_user(id1);
1102 write2_na = (na_pair *) isl_id_get_user(id2);
1104 if (!compute_matching(write1_na->node, valid1, write2_na->node, valid2,
1105 &same_filter, &same_value, info->n_shared))
1106 return sink;
1108 value1 = compute_filter_values(write1_na->node, !valid1);
1109 value2 = compute_filter_values(write2_na->node, !valid2);
1111 mem1 = info->get_mem_dep(write1_na);
1112 source = source_iterations(write1_na, id1, valid1, info);
1113 mem1 = share(mem1, info->n_shared);
1114 mem1 = isl_map_intersect_domain(mem1, source);
1116 mem2 = info->get_mem_dep(write2_na);
1117 source = source_iterations(write2_na, id2, valid2, info);
1118 mem2 = share(mem2, info->n_shared);
1119 mem2 = isl_map_intersect_domain(mem2, source);
1121 sink = remove_conflict(sink, mem1, value1, mem2, value2,
1122 same_filter, same_value);
1124 return sink;
1127 /* Remove inconsistencies from the set of sink iterations "sink"
1128 * based on two potential sources identified by "id1" and "id2",
1129 * (representing the number of shared loops).
1131 static __isl_give isl_set *remove_inconsistencies(__isl_take isl_set *sink,
1132 add_dep_info *info, __isl_keep isl_id *id1, __isl_keep isl_id *id2)
1134 sink = remove_inconsistencies(sink, info, id1, false, id2, false);
1135 sink = remove_inconsistencies(sink, info, id1, false, id2, true);
1136 sink = remove_inconsistencies(sink, info, id1, true, id2, false);
1137 sink = remove_inconsistencies(sink, info, id1, true, id2, true);
1138 return sink;
1141 /* Remove inconsistencies from the set of sink iterations "sink"
1142 * based on the potential source identified by "id"
1143 * (representing the number of shared loops),
1144 * in particular, on either the last iteration where the filters hold
1145 * (if valid is set) or on later iterations (if valid is not set).
1147 * This function is very similar to the remove_inconsistencies
1148 * function above that considers two potential sources instead
1149 * of the sink and one potential source. The main differences
1150 * are that for the sink, the filters always hold and that the mapping
1151 * from sink iterations to sink iterations is computed in a different
1152 * (and fairly trivial) way.
1154 static __isl_give isl_set *remove_inconsistencies(__isl_take isl_set *sink,
1155 add_dep_info *info, __isl_keep isl_id *id, bool valid)
1157 na_pair *read_na, *write_na;
1158 isl_set *after;
1159 isl_map *value1, *value2;
1160 isl_map *same_filter;
1161 isl_map *same_value;
1162 isl_map *id_map, *mem;
1164 read_na = info->read_na_pair;
1165 write_na = (na_pair *) isl_id_get_user(id);
1167 if (!compute_matching(read_na->node, true, write_na->node, valid,
1168 &same_filter, &same_value, info->n_shared))
1169 return sink;
1171 value1 = compute_filter_values(read_na->node, false);
1172 value2 = compute_filter_values(write_na->node, !valid);
1174 id_map = isl_set_identity(isl_set_copy(sink));
1176 mem = info->get_mem_dep(write_na);
1177 after = source_iterations(write_na, id, valid, info);
1178 mem = share(mem, info->n_shared);
1179 mem = isl_map_intersect_domain(mem, after);
1181 sink = remove_conflict(sink, id_map, value1, mem, value2,
1182 same_filter, same_value);
1184 return sink;
1187 /* Remove inconsistencies from the set of sink iterations "sink"
1188 * based on the potential source identified by "id"
1189 * (representing the number of shared loops).
1191 static __isl_give isl_set *remove_inconsistencies(__isl_take isl_set *sink,
1192 add_dep_info *info, __isl_keep isl_id *id)
1194 sink = remove_inconsistencies(sink, info, id, false);
1195 sink = remove_inconsistencies(sink, info, id, true);
1196 return sink;
1199 /* Remove all parameters that were introduced by add_parametrization().
1201 static __isl_give isl_map *remove_all_controls(__isl_take isl_map *dep)
1203 int i;
1204 int n_param;
1205 const char *name;
1207 n_param = isl_map_dim(dep, isl_dim_param);
1208 for (i = n_param - 1; i >= 0; --i) {
1209 if (!is_control(dep, i))
1210 continue;
1211 dep = isl_map_project_out(dep, isl_dim_param, i, 1);
1213 dep = isl_map_coalesce(dep);
1215 return dep;
1218 /* Remove all parameters that were introduced by add_parametrization().
1220 static __isl_give isl_set *remove_all_controls(__isl_take isl_set *set)
1222 int i;
1223 int n_param;
1224 const char *name;
1226 n_param = isl_set_dim(set, isl_dim_param);
1227 for (i = n_param - 1; i >= 0; --i) {
1228 if (!is_control(set, i))
1229 continue;
1230 set = isl_set_project_out(set, isl_dim_param, i, 1);
1232 set = isl_set_coalesce(set);
1234 return set;
1237 /* Create the map
1239 * { a -> b : (shared >= max and
1240 * the first max iterators are equal) or
1241 * (shared = max - 1 and
1242 * the first max - 1 iterators are equal and
1243 * dimension max - 1 of a is smaller than that of b) or
1244 * ...
1245 * (shared = min and
1246 * the first min iterators are equal and
1247 * dimension min of a is smaller than that of b) }
1249 static __isl_give isl_map *compute_shared_map(__isl_take isl_space *space,
1250 __isl_keep isl_id *shared_id, int min, int max)
1252 isl_map *shared_map;
1253 int shared_pos;
1255 shared_pos = isl_space_find_dim_by_id(space, isl_dim_param, shared_id);
1256 shared_map = isl_map_universe(isl_space_copy(space));
1257 shared_map = isl_map_lower_bound_si(shared_map, isl_dim_param,
1258 shared_pos, max);
1259 shared_map = share(shared_map, max);
1261 for (int i = min; i < max; ++i) {
1262 isl_map *shared_map_i;
1264 shared_map_i = isl_map_universe(isl_space_copy(space));
1265 shared_map_i = isl_map_fix_si(shared_map_i, isl_dim_param,
1266 shared_pos, i);
1267 shared_map_i = share(shared_map_i, i);
1268 shared_map_i = isl_map_order_lt(shared_map_i, isl_dim_in, i,
1269 isl_dim_out, i);
1271 shared_map = isl_map_union(shared_map, shared_map_i);
1274 isl_space_free(space);
1276 return shared_map;
1279 /* Different parts of the final dependence relations may have been
1280 * created at different depths and may therefore have a different
1281 * number of dimensions of the last iterator. The __last_<na>_shared
1282 * value determines how many of the dimensions are implicitly equal
1283 * to those of the sink iteration. This function creates a set that
1284 * makes these equalities explicit, so that we can later remove
1285 * the __last_<na>_shared parameter. It also marks those parts
1286 * that have a number of shared iterators that is smaller than the minimum
1287 * as not having any last iteration.
1289 * In particular, we create a set in the space of the sink of the form
1291 * { s : __last_<na>_valid = 0 or
1292 * (__last_<na>_valid = 1 and
1293 * __last_<na>_<i> is a potential source iteration and
1294 * ((__last_<na>_shared >= max_shared and
1295 * the first max_shared iterators are equal) or
1296 * (__last_<na>_shared = max_shared - 1 and
1297 * the first max_shared - 1 iterators are equal and
1298 * iterator max_shared - 1 of the source is smaller) or
1299 * ...
1300 * (__last_<na>_shared = min_shared and
1301 * the first min_shared iterators are equal and
1302 * iterator min_shared of the source is smaller))) }
1304 * That is, for those parts with __last_<na>_shared smaller than
1305 * max_n_shared[source_na], intersection with the set will introduce
1306 * __last_<na>_<i> parameters (assuming they don't have a known fixed value)
1307 * up until __last_<na>_shared and equate them to the corresponding iterators
1308 * of the sink.
1310 static __isl_give isl_set *shared_refinement(__isl_keep isl_id *shared_id,
1311 add_dep_info *info)
1313 isl_ctx *ctx;
1314 isl_space *space;
1315 isl_map *mem;
1316 na_pair *source_na;
1317 isl_map *shared_map;
1318 int valid_pos, shared_pos;
1319 isl_set *valid;
1320 isl_set *invalid;
1321 isl_set *domain;
1322 isl_id *valid_id;
1324 ctx = isl_id_get_ctx(shared_id);
1326 source_na = (na_pair *) isl_id_get_user(shared_id);
1327 valid_id = valid_bit_id(ctx, source_na);
1329 mem = info->get_mem_dep(source_na);
1330 space = isl_map_get_space(mem);
1331 isl_map_free(mem);
1333 shared_pos = isl_space_dim(space, isl_dim_param);
1334 space = isl_space_add_dims(space, isl_dim_param, 1);
1335 space = isl_space_set_dim_id(space, isl_dim_param,
1336 shared_pos, isl_id_copy(shared_id));
1338 shared_map = compute_shared_map(isl_space_copy(space), shared_id,
1339 info->min_n_shared[source_na], info->max_n_shared[source_na]);
1341 domain = isl_set_universe(isl_space_domain(space));
1342 valid_pos = find_or_add_param(&domain, isl_id_copy(valid_id));
1343 domain = isl_set_fix_si(domain, isl_dim_param, valid_pos, 1);
1344 domain = add_parametrization(domain, source_na, info);
1345 shared_map = isl_map_intersect_domain(shared_map, domain);
1347 valid = isl_map_range(shared_map);
1348 invalid = isl_set_universe(isl_set_get_space(valid));
1349 shared_pos = isl_set_find_dim_by_id(invalid, isl_dim_param, shared_id);
1350 invalid = isl_set_upper_bound_si(invalid, isl_dim_param, shared_pos,
1351 info->n_shared - 1);
1353 valid_pos = find_or_add_param(&invalid, valid_id);
1354 invalid = isl_set_fix_si(invalid, isl_dim_param, valid_pos, 0);
1356 return isl_set_union(valid, invalid);
1359 /* Different parts of the final dependence relations may have been
1360 * created at different depths and may therefore have a different
1361 * number of dimensions of the last iterator. The __last_*_shared
1362 * value determines how many of the dimensions are implicitly equal
1363 * to those of the sink iteration.
1365 * For each of the __last_*_shared parameters, explicitly add
1366 * the implicitly equal __last_*_i iterators by intersecting
1367 * the sink with the set computed by shared_refinement.
1368 * Finally, remove the __last_*_shared parameters.
1370 static __isl_give isl_map *refine_shared(__isl_take isl_map *dep,
1371 add_dep_info *info)
1373 isl_space *space;
1374 isl_set *refinement;
1375 int n_param;
1377 space = isl_map_get_space(dep);
1378 n_param = isl_space_dim(space, isl_dim_param);
1380 refinement = isl_set_universe(isl_space_range(isl_space_copy(space)));
1382 for (int i = 0; i < n_param; ++i) {
1383 isl_set *ref_i;
1384 isl_id *id;
1386 if (!is_shared(space, i))
1387 continue;
1388 if (!isl_map_involves_dims(dep, isl_dim_param, i, 1))
1389 continue;
1391 id = isl_space_get_dim_id(space, isl_dim_param, i);
1392 ref_i = shared_refinement(id, info);
1393 isl_id_free(id);
1395 if (isl_set_is_wrapping(refinement)) {
1396 isl_map *map = isl_set_unwrap(refinement);
1397 map = isl_map_intersect_domain(map, ref_i);
1398 refinement = isl_map_wrap(map);
1399 } else
1400 refinement = isl_set_intersect(refinement, ref_i);
1402 dep = isl_map_intersect_range(dep, refinement);
1404 isl_space_free(space);
1406 space = isl_map_get_space(dep);
1407 n_param = isl_space_dim(space, isl_dim_param);
1408 for (int i = n_param - 1; i >= 0; --i) {
1409 if (!is_shared(space, i))
1410 continue;
1411 dep = isl_map_project_out(dep, isl_dim_param, i, 1);
1413 isl_space_free(space);
1415 dep = isl_map_coalesce(dep);
1417 return dep;
1420 /* Compute the gist of "dep" with respect to the fact that
1422 * 0 <= __last_*_valid <= 1
1424 static __isl_give isl_map *gist_valid(__isl_take isl_map *dep)
1426 isl_space *space;
1427 isl_set *valid;
1428 int n_param;
1430 space = isl_space_params(isl_map_get_space(dep));
1431 n_param = isl_space_dim(space, isl_dim_param);
1433 valid = isl_set_universe(isl_space_copy(space));
1434 for (int i = 0; i < n_param; ++i) {
1435 if (!is_valid_bit(space, i))
1436 continue;
1438 valid = isl_set_lower_bound_si(valid, isl_dim_param, i, 0);
1439 valid = isl_set_upper_bound_si(valid, isl_dim_param, i, 1);
1442 isl_space_free(space);
1444 dep = isl_map_gist_params(dep, valid);
1446 return dep;
1449 /* Simplify the constraints on the parameters introduced
1450 * in add_parametrization().
1451 * We first add __last_*_i iterators that are only implicitly referred
1452 * to through the __last_*_shared parameters.
1453 * Then, we remove all those parameters that turn out not be needed.
1454 * If there are any of those parameters left, then we compute the gist
1455 * with respect to the valid bit being either 0 or 1 and rename
1456 * the parameters to also include a reference to the sink.
1457 * The resulting relation is assigned to controlled_relation,
1458 * while the relation field is assigned the result of projecting out
1459 * all those parameters.
1461 static __isl_give isl_map *simplify_controls(__isl_take isl_map *dep,
1462 add_dep_info *info, bool *has_controls)
1464 bool hs;
1466 if (!has_controls)
1467 has_controls = &hs;
1468 dep = refine_shared(dep, info);
1469 dep = remove_redundant_controls(dep, has_controls);
1470 if (*has_controls) {
1471 dep = gist_valid(dep);
1472 dep = rename_controls(dep, info->read_na_pair);
1475 return dep;
1478 /* Extract a single dependence from the result of dataflow analysis.
1480 * We first simplify the constraints on the parameters introduced
1481 * in add_parametrization().
1483 * If the dependence relation turns out to be empty, we simply return.
1484 * Otherwise, we create a corresponding pdg::dependence and keep track
1485 * of the fact that the potential source is actually used
1486 * so that we can remove any reference to potential sources that are
1487 * never used from the dependence relations.
1489 static isl_stat add_dep(__isl_take isl_map *dep, int must, void *dep_user,
1490 void *user)
1492 bool has_controls;
1493 na_pair *write_na = (na_pair *) dep_user;
1494 add_dep_info *info = (struct add_dep_info *)user;
1495 isl_ctx *ctx = info->pdg->get_isl_ctx();
1496 pdg::dependence *d;
1498 dep = isl_map_coalesce(dep);
1499 dep = simplify_controls(dep, info, &has_controls);
1500 if (isl_map_is_empty(dep)) {
1501 isl_map_free(dep);
1502 return isl_stat_ok;
1505 info->used.insert(write_na);
1507 d = new pdg::dependence;
1508 d->array = info->a;
1509 d->type = info->dtype;
1510 d->from = write_na->node;
1511 d->to = info->read_na_pair->node;
1512 d->from_access = write_na->access;
1513 d->to_access = info->read_na_pair->access;
1515 if (d->from_access->extension || d->to_access->extension)
1516 d->extended_relation =
1517 new pdg::IslMap(remove_all_controls(isl_map_copy(dep)));
1518 if (d->from_access->extension)
1519 dep = isl_map_apply_domain(dep,
1520 isl_map_reverse(
1521 d->from_access->extension->get_isl_map(ctx)));
1522 if (d->to_access->extension)
1523 dep = isl_map_apply_range(dep,
1524 isl_map_reverse(
1525 d->to_access->extension->get_isl_map(ctx)));
1526 if (has_controls)
1527 d->controlled_relation = new pdg::IslMap(isl_map_copy(dep));
1528 d->relation = new pdg::IslMap(remove_all_controls(isl_map_copy(dep)));
1529 info->pdg->dependences.push_back(d);
1530 isl_map_free(dep);
1531 return isl_stat_ok;
1534 /* This structure represents a set of filter index expressions
1535 * along with bounds on the correponding filter values.
1536 * The number of output dimensions in "value" is the same as
1537 * the number of elements in the "index" vector.
1539 struct da_filter {
1540 std::vector<isl_union_map *> index;
1541 isl_map *value;
1544 /* Construct a da_filter object representing the filters in
1545 * na->node and na->access.
1547 static struct da_filter *extract_filter(na_pair *na)
1549 da_filter *filter = new da_filter;
1550 isl_set *domain;
1551 pdg::node *node = na->node;
1552 pdg::access *access = na->access;
1554 domain = node->source->get_isl_set();
1555 if (isl_set_is_wrapping(domain))
1556 filter->value = isl_set_unwrap(domain);
1557 else
1558 filter->value = isl_map_from_domain(domain);
1560 for (int i = 0; i < node->filters.size(); ++i)
1561 filter->index.push_back(extract_access_map(node->filters[i]));
1563 if (access->nested.size() == 0)
1564 return filter;
1566 domain = isl_map_domain(access->map->get_isl_map());
1567 filter->value = isl_map_flat_range_product(filter->value,
1568 isl_set_unwrap(domain));
1570 for (int i = 0; i < access->nested.size(); ++i)
1571 filter->index.push_back(extract_access_map(access->nested[i]));
1573 return filter;
1576 static struct da_filter *da_filter_free(struct da_filter *filter)
1578 if (!filter)
1579 return NULL;
1580 for (int i = 0; i < filter->index.size(); ++i)
1581 isl_union_map_free(filter->index[i]);
1582 isl_map_free(filter->value);
1583 delete filter;
1584 return NULL;
1587 static void da_filter_dump(struct da_filter *filter)
1589 isl_printer *p;
1591 if (!filter)
1592 return;
1594 p = isl_printer_to_file(isl_map_get_ctx(filter->value), stderr);
1595 p = isl_printer_start_line(p);
1596 p = isl_printer_print_str(p, "value(");
1597 for (int i = 0; i < filter->index.size(); ++i) {
1598 if (i)
1599 p = isl_printer_print_str(p, ", ");
1600 p = isl_printer_print_union_map(p, filter->index[i]);
1602 p = isl_printer_print_str(p, ") in ");
1603 p = isl_printer_print_map(p, filter->value);
1604 p = isl_printer_end_line(p);
1606 isl_printer_free(p);
1609 /* Look for a filter index expression in "filter" that is identical
1610 * to "index". Return the index of this index expression if it is
1611 * found and the number of elements in filter->index otherwise.
1613 static int da_filter_find_exact_match(struct da_filter *filter,
1614 __isl_keep isl_union_map *index)
1616 for (int i = 0; i < filter->index.size(); ++i) {
1617 int equal;
1619 equal = isl_union_map_is_equal(filter->index[i], index);
1620 if (equal < 0)
1621 return -1;
1622 if (equal)
1623 return i;
1626 return filter->index.size();
1629 /* Add the index expression "index" to "filter" with an unconstrained
1630 * filter value. To ease debugging we set the name of the new filter
1631 * value dimension to that of the array being accessed by "index".
1632 * Although the range of "index" is allowed to live in more than
1633 * one space, we assume that they are all wrapped maps to the same
1634 * array space.
1636 static struct da_filter *da_filter_add(struct da_filter *filter,
1637 __isl_take isl_union_map *index)
1639 isl_id *id;
1640 isl_union_map *univ;
1641 isl_set *set;
1643 if (!filter || !index)
1644 goto error;
1646 filter->value = isl_map_add_dims(filter->value, isl_dim_out, 1);
1647 univ = isl_union_map_universe(isl_union_map_copy(index));
1648 univ = isl_union_set_unwrap(isl_union_map_range(univ));
1649 set = isl_set_from_union_set(isl_union_map_range(univ));
1650 id = isl_set_get_tuple_id(set);
1651 isl_set_free(set);
1652 filter->value = isl_map_set_dim_id(filter->value, isl_dim_out,
1653 filter->index.size(), id);
1654 if (!filter->value)
1655 goto error;
1656 filter->index.push_back(index);
1658 return filter;
1659 error:
1660 isl_union_map_free(index);
1661 da_filter_free(filter);
1662 return NULL;
1665 /* Intersect the set of possible filter values in "filter" with "value".
1667 static struct da_filter *da_filter_restrict(struct da_filter *filter,
1668 __isl_take isl_map *value)
1670 if (!filter || !value)
1671 goto error;
1673 filter->value = isl_map_intersect(filter->value, value);
1674 if (!filter->value)
1675 return da_filter_free(filter);
1677 return filter;
1678 error:
1679 isl_map_free(value);
1680 da_filter_free(filter);
1681 return NULL;
1684 /* Does "map" represent a total filter on "domain", i.e., one that is defined
1685 * on every element of "domain"?
1687 * Although the range of "map" may live in different spaces, we assume
1688 * that the domain of "map" lives in a single space.
1690 static int is_total_filter(__isl_keep isl_union_map *map,
1691 __isl_keep isl_set *domain)
1693 isl_union_set *map_domain;
1694 isl_set *set;
1695 int total;
1697 map_domain = isl_union_map_domain(isl_union_map_copy(map));
1698 set = isl_set_from_union_set(map_domain);
1699 total = isl_set_is_subset(domain, set);
1700 isl_set_free(set);
1702 return total;
1705 /* Does "node" have only total filters on "domain"?
1707 static int all_total_filters(pdg::node *node, __isl_take isl_set *domain)
1709 int total = 1;
1711 for (int i = 0; i < node->filters.size(); ++i) {
1712 isl_union_map *map = extract_access_map(node->filters[i]);
1714 total = is_total_filter(map, domain);
1715 isl_union_map_free(map);
1717 if (!total)
1718 break;
1721 isl_set_free(domain);
1722 return total;
1725 /* Does "node" have only total filters on the domain of "map"?
1727 static bool filters_total_on_domain(pdg::node *node, __isl_keep isl_map *map)
1729 return all_total_filters(node, isl_map_domain(isl_map_copy(map)));
1732 /* Does "node" have only total filters on the range of "map"?
1734 static bool filters_total_on_range(pdg::node *node, __isl_keep isl_map *map)
1736 return all_total_filters(node, isl_map_range(isl_map_copy(map)));
1739 /* Are the filters of "source_node" total on the range of "source_map"
1740 * and those of "sink_node" (if any) total on the domain of "soruce_map"?
1742 static bool total_filters(pdg::node *source_node, pdg::node *sink_node,
1743 __isl_keep isl_map *source_map)
1745 int total;
1747 total = filters_total_on_range(source_node, source_map);
1748 if (!total)
1749 return false;
1751 if (!isl_set_is_wrapping(sink_node->source->set))
1752 return true;
1754 total = filters_total_on_domain(sink_node, source_map);
1755 if (!total)
1756 return false;
1758 return true;
1761 /* Does "node" have any single valued filters?
1763 static int any_single_valued_filter(pdg::node *node)
1765 for (int i = 0; i < node->filters.size(); ++i) {
1766 isl_union_map *map = extract_access_map(node->filters[i]);
1767 int sv;
1769 sv = isl_union_map_is_single_valued(map);
1770 isl_union_map_free(map);
1772 if (sv)
1773 return sv;
1776 return 0;
1779 /* Construct a mapping from the source iterations to all source filter values
1780 * that allow some corresponding sink iteration(s) (according to "source_map")
1781 * to be executed. In other words, if any sink iteration is executed,
1782 * then we know that the filters of the corresponding source iterations
1783 * satisfy the returned relation.
1785 * The "filter" input represents what is known about the filter
1786 * values at the sink.
1788 * The "source" domain of the source is a wrapped map,
1789 * mapping iteration vectors to filter values.
1790 * We first construct a relation between the sink filter values (available
1791 * in "filter") and the source filter values. The purpose of this relation
1792 * is to find as much information about the source filter values
1793 * as possible. We can start with the value bounds on the arrays
1794 * accessed in the filters as they always hold.
1795 * Next, we loop over the source filters and check whether there
1796 * is any sink filter that covers the source filter.
1797 * In particular, for each source filter, we construct a map
1798 * from the source iteration domain to a wrapped access relation,
1799 * representing the write access relation that corresponds to
1800 * the filter read access. Note that if we were unable to determine this
1801 * write access, then the mapping returned by extract_access_map
1802 * maps to the original read access, which will not match with
1803 * any filter access relations of the sink.
1804 * We combine the constructed map with the proto-dependence
1805 * (source_map) to obtain a mapping from sink iterations to
1806 * access relations such that there is some source iteration that
1807 * may be the source of the given sink iteration (based on source_map)
1808 * and that the filter value at this source iteration was written by
1809 * that access. If the result is a subset of the mapping from the
1810 * sink iterations to the corresponding write access relation for some filter,
1811 * then we know that any constraint on this filter value also applies
1812 * to the source filter value. We therefore introduce an equality
1813 * in our mapping from sink filter values to source filter values.
1815 * When we apply the mapping from sink filter values to source filter values
1816 * to the mapping from source iterations to sink filter values, we
1817 * obtain a mapping from sink iterations to source filter values
1818 * that represents what we know about the source filter values.
1819 * That is, for each sink iteration in the domain of this map, if this
1820 * sink iteration is executed, then the actual source filter values
1821 * are an element of the image of the sink iteration.
1822 * In other words, the sink iteration is only executed if the source
1823 * filter values are an element of the image.
1824 * Mapping this relation back to the source through the proto-dependence
1825 * source_map, we obtain a relation from source iterations to all source
1826 * filter values for which any sink iteration is executed.
1827 * In particular, for values of the filters outside this relation,
1828 * no corresponding (according to source_map) sink is executed.
1830 static __isl_give isl_map *known_filter_values(struct da_filter *filter,
1831 na_pair *source_na, __isl_keep isl_map *source_map)
1833 isl_space *space, *space2;
1834 isl_map *source_value, *sink_value;
1835 isl_map *filter_map;
1836 isl_set *bounds;
1838 sink_value = isl_map_copy(filter->value);
1840 space = isl_set_get_space(source_na->node->source->set);
1841 space = isl_space_range(isl_space_unwrap(space));
1842 space2 = isl_space_range(isl_map_get_space(sink_value));
1843 space = isl_space_align_params(space, isl_space_copy(space2));
1844 space2 = isl_space_align_params(space2, isl_space_copy(space));
1845 space = isl_space_map_from_domain_and_range(space2, space);
1846 filter_map = isl_map_universe(space);
1848 bounds = compute_filter_bounds(source_na->node);
1849 filter_map = isl_map_intersect_range(filter_map, bounds);
1851 for (int i = 0; i < source_na->node->filters.size(); ++i) {
1852 isl_union_map *map_i;
1853 map_i = extract_access_map(source_na->node->filters[i]);
1854 map_i = isl_union_map_apply_range(
1855 isl_union_map_from_map(isl_map_copy(source_map)), map_i);
1856 for (int j = 0; j < filter->index.size(); ++j) {
1857 if (isl_union_map_is_subset(map_i, filter->index[j]))
1858 filter_map = isl_map_equate(filter_map,
1859 isl_dim_in, j, isl_dim_out, i);
1861 isl_union_map_free(map_i);
1864 source_value = isl_map_apply_range(sink_value, filter_map);
1865 source_value = isl_map_apply_domain(source_value,
1866 isl_map_copy(source_map));
1867 source_value = isl_map_coalesce(source_value);
1869 return source_value;
1872 /* Add a new output dimension to "map" with constraints that are the
1873 * same as those on output dimension "pos".
1875 * Given map { [i] -> [j] }, we first and an extra dimension,
1877 * { [i] -> [j,*] }
1879 * extract out j_pos,
1881 * { [[i] -> [j_0,...,j_{pos-1},*,j_{pos+1},...,*]] -> [j_pos] }
1883 * create a copy
1885 * { [[i] -> [j_0,...,j_{pos-1},*,j_{pos+1},...,*]] -> [j_pos,j_pos'] }
1887 * and then move the dimensions back
1889 * { [i] -> [j,j_pos'] }
1891 static __isl_give isl_map *copy_dim(__isl_take isl_map *map, int pos)
1893 int pos_new;
1895 pos_new = isl_map_dim(map, isl_dim_out);
1896 pos += isl_map_dim(map, isl_dim_in);
1897 pos_new += isl_map_dim(map, isl_dim_in);
1898 map = isl_map_add_dims(map, isl_dim_out, 1);
1899 map = isl_map_from_domain(isl_map_wrap(map));
1900 map = isl_map_add_dims(map, isl_dim_out, 1);
1901 map = isl_map_equate(map, isl_dim_in, pos, isl_dim_out, 0);
1902 map = isl_map_eliminate(map, isl_dim_in, pos, 1);
1903 map = isl_map_range_product(map, isl_map_copy(map));
1904 map = isl_map_equate(map, isl_dim_in, pos, isl_dim_out, 0);
1905 map = isl_map_equate(map, isl_dim_in, pos_new, isl_dim_out, 1);
1906 map = isl_set_unwrap(isl_map_domain(map));
1908 return map;
1911 /* Given constraints on the filter values "filter" at the sink iterations
1912 * "sink", derive additional constraints from the filter values of source
1913 * node "source_na". In particular, consider the iterations of "source_na"
1914 * that have _not_ been executed based on the constraints of the corresponding
1915 * last iteration parameters in "sink" and what this implies about the
1916 * filter values at those iterations.
1918 * Essentially, we consider all pairs of sink iterations and filter
1919 * elements, together with the corresponding non-executed source iterations
1920 * and the possible values of those filters. We universally quantify
1921 * the non-executed source iterations so that we obtain the intersection
1922 * of the constraints on the filter values over all those source iterations
1923 * and then existentially quantify the filter elements to obtain constraints
1924 * that are valid for all filter elements.
1926 * In more details, the computation is performed as follows.
1928 * Compute a mapping from potential last iterations of the other source
1929 * to sink iterations, taking into account the contraints on
1930 * the last executed iterations encoded in the parameters of "sink",
1931 * but projecting out all parameters encoding last iterations from the result.
1932 * Include all earlier iterations of the other source, resulting in
1933 * a mapping with a domain that includes all potential iterations of the
1934 * other source.
1936 * Subtract these iterations from all possible iterations of the other
1937 * source for a given sink iteration, resulting in a mapping from
1938 * potential source iterations that are definitely not executed
1939 * to the corresponding sink iteration.
1941 * If this map is empty, then this means we can't find any iterations
1942 * of the other source that are certainly not executed and then we
1943 * can't derive any further information.
1944 * Similarly, if the filters of source_na->node are not total
1945 * on the set of non-executed iterations, then we cannot draw any conclusions.
1946 * (Note that we already tested that the filters on sink->node are total
1947 * on the domain of "source_map". By intersecting the set of corresponding
1948 * sink iterations with this domain, we ensure that this property also holds
1949 * on those sink iterations.)
1950 * Otherwise, keep track of those sink iterations without any corresponding
1951 * non-executed other source iterations. We will lose these sink iterations
1952 * in subsequent computations, so we need to add them back in at the end.
1954 * Compute bounds on the filter values at the non executed iterations
1955 * based on what we know about filters at the sink and the fact that
1956 * the iterations are not executed, meaning that the filter values
1957 * do not satisfy the constraints that allow the iteration to be executed.
1958 * The result is a mapping T -> V.
1960 * Note that we only know that there is some accessed filter element
1961 * that does not satisfy the constraints that allow the iteration to be
1962 * executed. We therefore project out those dimensions that correspond
1963 * to filters with an access relation that is not single-valued, i.e.,
1964 * one that may access more than one element for some iterations.
1965 * If there are no single-valued filters, then we can skip the rest of
1966 * the computation.
1968 * Construct a mapping [K -> F] -> T, with K the sink iterations,
1969 * T the corresponding non-executed iterations of the other source and
1970 * F the filters accessed at those iterations.
1972 * Combine the above two mappings into a mapping [K -> F] -> V
1973 * such that the set of possible filter values (V) is the intersection
1974 * over all iterations of the other source that access the filter,
1975 * and such that there is at least one such iteration.
1976 * In other words, ensure that the range of [K -> F] -> T
1977 * is a non-empty subset of the range of V -> T.
1978 * We require a non-empty subset to ensure that the domain of
1979 * [K -> F] -> V is equal to the result of composing K -> T with T -> F.
1980 * Projecting out F from [K -> F] -> V, we obtain a map K -> V that is
1981 * the union of all possible values of the filters K -> F, i.e.,
1982 * the constraints that the values of K -> F satisfy.
1983 * If we didn't impose non-emptiness above, then the domain of [K -> F] -> V
1984 * would also include pairs that we are not interested in, related to
1985 * arbitrary values. Projecting out F would then also lead to arbitrary
1986 * values.
1988 * Compose the mapping K -> T with the index expressions, pulling them
1989 * back to the sink.
1990 * For each of these pulled back index expressions, we check if it
1991 * is equal to one of the sink filter index expressions. If not, we
1992 * add it to the sink filter index expressions.
1993 * In both cases, we keep track of the fact that this sink filter
1994 * should have a value that satisfies the constraints in K -> V.
1995 * We further check if there is any sink filter index expression
1996 * that is a (strict) subset of the pulled back index expression.
1997 * The value of any such sink filter should also satisfy those
1998 * constraints, so we duplicate the filter value in K -> V.
2000 * Finally, we intersect the possible filter values with the constraints
2001 * obtained above on the affected sink iterations and a universe range
2002 * on the unaffected sink iterations.
2004 static struct da_filter *include_other_source(struct da_filter *filter,
2005 __isl_keep isl_map *source_map, __isl_keep isl_set *sink,
2006 na_pair *source_na, add_dep_info *info)
2008 isl_space *space, *space2;
2009 isl_set *source_domain;
2010 isl_map *mem;
2011 isl_map *may_run, *not_run;
2012 isl_map *source_value;
2013 isl_union_map *usource, *umap;
2014 std::vector<isl_union_map *> index;
2015 isl_union_map *index_product;
2016 isl_map *map;
2017 isl_map *filter_map;
2018 isl_set *unaffected_sink;
2019 isl_map *unaffected_value;
2021 if (source_na->node->filters.size() == 0)
2022 return filter;
2024 space = isl_set_get_space(source_na->node->source->set);
2025 space = isl_space_domain(isl_space_unwrap(space));
2026 source_domain = isl_set_universe(space);
2027 source_domain = add_parametrization(source_domain, source_na, info);
2028 may_run = isl_map_from_domain_and_range(source_domain,
2029 isl_set_copy(sink));
2030 may_run = share(may_run, info->n_shared);
2032 may_run = remove_all_controls(may_run);
2033 mem = info->get_mem_dep(source_na);
2034 mem = share(mem, info->n_shared);
2035 mem = isl_map_intersect_range(mem,
2036 isl_map_domain(remove_all_controls(isl_map_copy(source_map))));
2037 space = isl_space_domain(isl_map_get_space(may_run));
2038 may_run = isl_map_apply_domain(may_run, isl_map_lex_ge(space));
2039 not_run = isl_map_subtract(mem, may_run);
2041 if (isl_map_is_empty(not_run) ||
2042 !any_single_valued_filter(source_na->node) ||
2043 !filters_total_on_domain(source_na->node, not_run)) {
2044 isl_map_free(not_run);
2045 return filter;
2048 not_run = isl_map_reverse(not_run);
2049 source_value = known_filter_values(filter, source_na, not_run);
2050 source_value = isl_map_subtract(source_value,
2051 isl_set_unwrap(source_na->node->source->get_isl_set()));
2052 unaffected_sink = isl_set_copy(sink);
2053 unaffected_sink = remove_all_controls(unaffected_sink);
2054 unaffected_sink = isl_set_subtract(unaffected_sink,
2055 isl_map_domain(isl_map_copy(not_run)));
2057 for (int i = 0; i < source_na->node->filters.size(); ++i) {
2058 isl_union_map *map;
2059 int sv;
2061 map = extract_access_map(source_na->node->filters[i]);
2062 sv = isl_union_map_is_single_valued(map);
2063 if (sv) {
2064 index.push_back(map);
2065 } else {
2066 isl_union_map_free(map);
2067 source_value = isl_map_project_out(source_value,
2068 isl_dim_out, index.size(), 1);
2072 index_product = isl_union_map_copy(index[0]);
2073 for (int i = 1; i < index.size(); ++i)
2074 index_product = isl_union_map_range_product(index_product,
2075 isl_union_map_copy(index[1]));
2076 index_product = isl_union_map_reverse(index_product);
2077 index_product = isl_union_map_domain_product(
2078 isl_union_map_from_map(isl_map_copy(not_run)), index_product);
2080 usource = isl_union_map_from_map(source_value);
2081 usource = join_non_empty_subset(index_product, usource);
2082 umap = isl_union_map_universe(isl_union_map_copy(usource));
2083 umap = isl_union_set_unwrap(isl_union_map_domain(umap));
2084 umap = isl_union_map_domain_map(umap);
2085 usource = isl_union_map_apply_domain(usource, umap);
2086 source_value = isl_map_from_union_map(usource);
2088 for (int i = 0; i < index.size(); ++i)
2089 index[i] = isl_union_map_apply_range(
2090 isl_union_map_from_map(isl_map_copy(not_run)), index[i]);
2092 space = isl_space_range(isl_map_get_space(source_value));
2093 space2 = isl_space_range(isl_map_get_space(filter->value));
2094 space = isl_space_align_params(space, isl_space_copy(space2));
2095 space2 = isl_space_align_params(space2, isl_space_copy(space));
2096 space = isl_space_map_from_domain_and_range(space, space2);
2097 filter_map = isl_map_universe(space);
2099 for (int i = 0; i < index.size(); ++i) {
2100 int exact = da_filter_find_exact_match(filter, index[i]);
2101 assert(exact >= 0);
2102 if (exact == filter->index.size()) {
2103 filter = da_filter_add(filter,
2104 isl_union_map_copy(index[i]));
2105 filter_map = isl_map_add_dims(filter_map,
2106 isl_dim_out, 1);
2108 filter_map = isl_map_equate(filter_map, isl_dim_in, i,
2109 isl_dim_out, exact);
2110 for (int j = 0; j < filter->index.size(); ++j) {
2111 int pos;
2113 if (j == exact)
2114 continue;
2115 if (!isl_union_map_is_subset(filter->index[j],
2116 index[i]))
2117 continue;
2118 pos = isl_map_dim(source_value, isl_dim_out);
2119 source_value = copy_dim(source_value, i);
2120 filter_map = isl_map_add_dims(filter_map,
2121 isl_dim_in, 1);
2122 filter_map = isl_map_equate(filter_map, isl_dim_in, pos,
2123 isl_dim_out, j);
2127 source_value = isl_map_apply_range(source_value, filter_map);
2128 source_value = isl_map_coalesce(source_value);
2129 unaffected_value = isl_map_from_domain(unaffected_sink);
2130 unaffected_value = isl_map_add_dims(unaffected_value, isl_dim_out,
2131 filter->index.size());
2132 source_value = isl_map_union(source_value, unaffected_value);
2133 filter = da_filter_restrict(filter, source_value);
2135 for (int i = 0; i < index.size(); ++i)
2136 isl_union_map_free(index[i]);
2137 isl_map_free(not_run);
2139 return filter;
2142 /* Given constraints on the filter values "filter" at the sink iterations
2143 * "sink", derive additional constraints from the filter values of those
2144 * source nodes for which "sink" contains a reference to its last iteration,
2145 * for use in determining whether parametrization is needed on "source_map".
2146 * In particular, we try and derive extra information from the fact that
2147 * some iterations of those source nodes have _not_ been executed.
2149 static struct da_filter *include_other_sources(struct da_filter *filter,
2150 __isl_keep isl_map *source_map, __isl_keep isl_set *sink,
2151 add_dep_info *info)
2153 isl_space *space;
2154 int n_param;
2156 space = isl_set_get_space(sink);
2157 n_param = isl_space_dim(space, isl_dim_param);
2158 for (int i = 0; i < n_param; ++i) {
2159 isl_id *id;
2160 na_pair *na;
2162 if (!is_shared(space, i))
2163 continue;
2165 id = isl_space_get_dim_id(space, isl_dim_param, i);
2166 na = (na_pair *) isl_id_get_user(id);
2167 isl_id_free(id);
2169 filter = include_other_source(filter, source_map, sink,
2170 na, info);
2172 isl_space_free(space);
2174 return filter;
2177 #define RESTRICT_ERROR -1
2178 #define RESTRICT_NO 0
2179 #define RESTRICT_EMPTY 1
2180 #define RESTRICT_INPUT 2
2181 #define RESTRICT_OUTPUT 3
2183 /* Given a map from sinks to potential sources (source_map)
2184 * and the set of sink iterations (sink),
2185 * check if any parametrization is needed on the sources.
2186 * That is, check whether the possible filter values at the sink
2187 * imply that the filter values at the source are always valid.
2188 * If so, the source is executed whenever the sink is executed
2189 * and no parametrization is required.
2191 * Return RESTRICT_NO if no parametrization is required.
2192 * Return RESTRICT_INPUT if parametrization is required on the input
2193 * of the computation of the last iteration.
2194 * Return RESTRICT_OUTPUT if parametrization is required on the output
2195 * of the computation of the last iteration. This means that we know
2196 * that the source will be executed, but we want to introduce a parameter
2197 * to represent the last iteration anyway, because the knowledge depends
2198 * on the parameters representing last iterations of other nodes.
2199 * Return RESTRICT_EMPTY if the potential sources cannot possibly
2200 * be executed, assuming that the sink is executed.
2202 * If there are no filters on the source, then obviously the source
2203 * is always executed.
2205 * If the filters of the sink and the source are not all total
2206 * on domain and range of "source_map", then we cannot draw any conclusion.
2207 * In principle, we could split up "source_map" according to whether
2208 * the filters would be total on the domain and range.
2210 * We first construct a mapping from source iterations to source filter
2211 * values that allow some corresponding sink iteration(s) (according to
2212 * "source_map") to be executed.
2213 * If this relation is a subset of the actual mapping from iteration
2214 * vectors to filter values at the source, then we know that a corresponding
2215 * sink is only executed when the source is executed and no parametrization
2216 * is required. However, we postpone the decision until we have considered
2217 * the other potential sources below.
2218 * If, on the other hand, the constructed relation is disjoint
2219 * from the source filter relation, then the sources cannot have
2220 * executed if the sink is executed. If so, we return
2221 * RESTRICT_EMPTY immediately.
2223 * Otherwise, we check if we can find out more information by considering
2224 * information derived from knowledge about the last iterations of other
2225 * nodes. If, by considering this extract information, we can find
2226 * that the potential source is never executed (given that the sink
2227 * is executed), then we return RESTRICT_EMPTY.
2228 * Otherwise, if we had already determined that the relation based
2229 * on only the sink is a subset of the filter values, then we return
2230 * RESTRICT_NO. If we can only draw this conclusion when taking into
2231 * account the other potential sources, then we return RESTRICT_OUTPUT.
2232 * Otherwise, we return RESTRICT_INPUT.
2234 static int need_parametrization(__isl_keep isl_map *source_map,
2235 __isl_keep isl_set *sink, na_pair *source_na, add_dep_info *info)
2237 bool filtered_source;
2238 isl_space *space;
2239 isl_map *source_value, *sink_value;
2240 da_filter *filter;
2241 na_pair *sink_na = info->read_na_pair;
2242 int res;
2244 if (isl_map_plain_is_empty(source_map) ||
2245 isl_set_plain_is_empty(sink))
2246 return RESTRICT_NO;
2248 filtered_source = isl_set_is_wrapping(source_na->node->source->set);
2250 if (!filtered_source)
2251 return RESTRICT_NO;
2253 if (!total_filters(source_na->node, sink_na->node, source_map))
2254 return RESTRICT_INPUT;
2256 filter = extract_filter(sink_na);
2257 res = -1;
2258 if (filter->index.size() != 0) {
2259 sink_value = known_filter_values(filter, source_na, source_map);
2260 source_value = isl_set_unwrap(
2261 source_na->node->source->get_isl_set());
2263 if (isl_map_is_disjoint(sink_value, source_value))
2264 res = RESTRICT_EMPTY;
2265 else if (isl_map_is_subset(sink_value, source_value))
2266 res = RESTRICT_NO;
2267 isl_map_free(source_value);
2268 isl_map_free(sink_value);
2271 if (res == RESTRICT_EMPTY) {
2272 da_filter_free(filter);
2273 return res;
2276 filter = include_other_sources(filter, source_map, sink, info);
2278 if (!filter)
2279 return RESTRICT_ERROR;
2280 if (filter->index.size() == 0) {
2281 da_filter_free(filter);
2282 return RESTRICT_INPUT;
2285 sink_value = known_filter_values(filter, source_na, source_map);
2286 source_value = isl_set_unwrap(source_na->node->source->get_isl_set());
2288 if (isl_map_is_disjoint(sink_value, source_value))
2289 res = RESTRICT_EMPTY;
2290 else if (res == RESTRICT_NO)
2292 else if (isl_map_is_subset(sink_value, source_value))
2293 res = RESTRICT_OUTPUT;
2294 else
2295 res = RESTRICT_INPUT;
2297 isl_map_free(source_value);
2298 isl_map_free(sink_value);
2299 da_filter_free(filter);
2301 return res;
2304 extern "C" {
2305 static __isl_give isl_restriction *do_restrict(
2306 __isl_keep isl_map *source_map, __isl_keep isl_set *sink,
2307 void *source_user, void *user);
2310 /* Add parameters corresponding to the last iteration of "na" to "set"
2311 * (assuming they don't already appear in "set")
2312 * and add constraints to them to express that there either is no
2313 * last iteration with info->n_shared shared loops
2314 * (__last_<na>_shared < info->n_shared) or that there is a last
2315 * iteration with at least info->n_shared shared loops
2316 * (__last_<na>_shared >= info->n_shared) and that the iteration is a possible
2317 * source of the current sink (based on the memory dependence between
2318 * the source and the sink).
2320 static __isl_give isl_set *set_parameter_bounds(__isl_take isl_set *set,
2321 na_pair *na, add_dep_info *info)
2323 isl_map *mem;
2324 isl_id *id;
2325 isl_set *valid;
2326 isl_set *invalid;
2327 isl_set *domain;
2328 int shared_pos;
2330 id = create_shared_id(isl_set_get_ctx(set), na);
2331 shared_pos = find_or_add_param(&set, id);
2333 valid = isl_set_copy(set);
2334 invalid = set;
2336 valid = isl_set_lower_bound_si(valid, isl_dim_param, shared_pos,
2337 info->n_shared);
2339 mem = info->get_mem_dep(na);
2340 domain = isl_set_universe(isl_space_domain(isl_map_get_space(mem)));
2341 domain = add_parametrization(domain, na, info);
2342 mem = isl_map_intersect_domain(mem, domain);
2343 valid = isl_set_intersect(valid, isl_map_range(mem));
2345 invalid = isl_set_upper_bound_si(invalid, isl_dim_param, shared_pos,
2346 info->n_shared - 1);
2348 return isl_set_union(valid, invalid);
2351 /* Check if there are any iterations of "source_na" in "source_map"
2352 * that are definitely executed, based solely on the possible filter values.
2353 * If so, add constraints to "sink" to indicate that the last execution
2354 * cannot be earlier than those definitely executed iterations.
2356 * We first compute the set of source iterations that are definitely
2357 * executed because there are no filter values that would prohibit
2358 * their execution. If there are no such source iterations then we are done.
2360 * Then we construct a map from sink iterations to associated (through
2361 * "source_map") definitely executed source iterations.
2363 * For those sink iterations that have a corresponding definitely
2364 * executed source iteration, we add constraints that express that
2365 * this last definitely executed source iteration is lexicographically
2366 * smaller than or equal to the last executed source iteration
2367 * (and that there definitely is a last executed source iteration).
2369 static __isl_give isl_set *mark_definite_source(__isl_take isl_set *sink,
2370 add_dep_info *info, na_pair *source_na, __isl_keep isl_map *source_map)
2372 isl_space *space;
2373 isl_set *dom;
2374 isl_map *invalid;
2375 isl_map *definite_source_map;
2376 isl_set *with_source;
2377 isl_map *map_after;
2378 int depth;
2380 dom = source_na->node->source->get_isl_set();
2381 dom = isl_map_domain(isl_set_unwrap(dom));
2382 invalid = compute_filter_values(source_na->node, true);
2383 dom = isl_set_subtract(dom, isl_map_domain(invalid));
2385 if (isl_set_is_empty(dom)) {
2386 isl_set_free(dom);
2387 return sink;
2390 space = isl_set_get_space(dom);
2392 definite_source_map = isl_map_copy(source_map);
2393 definite_source_map = isl_map_intersect_range(source_map, dom);
2395 dom = isl_map_domain(isl_map_copy(definite_source_map));
2397 with_source = isl_set_copy(sink);
2398 with_source = isl_set_intersect(with_source, isl_set_copy(dom));
2399 sink = isl_set_subtract(sink, dom);
2401 dom = isl_set_universe(isl_space_copy(space));
2402 dom = add_parametrization(dom, source_na, info);
2404 depth = source_na->node->get_filter_depth();
2406 space = isl_space_map_from_set(space);
2407 map_after = isl_map_lex_ge_first(space, depth);
2408 dom = isl_set_apply(dom, map_after);
2410 definite_source_map = isl_map_intersect_range(definite_source_map, dom);
2412 dom = isl_map_domain(definite_source_map);
2413 with_source = isl_set_intersect(with_source, dom);
2415 sink = isl_set_union(sink, with_source);
2417 return sink;
2420 /* Remove inconsistencies from the set of sink iterations "sink"
2421 * based on the current potential source "source_na" and other
2422 * potential sources referenced by "sink".
2424 * We first identify those iterations of "source_na" that are
2425 * definitely executed based solely on the possible filter values.
2427 * If the sink has filters, then we remove inconsistencies based
2428 * on the sink and the current potential source.
2430 * Finally, we go through the references to other potential sources
2431 * in "sink" and remove inconsistencies based on this other potential
2432 * source and the current potential source.
2434 static __isl_give isl_set *remove_inconsistencies(__isl_take isl_set *sink,
2435 add_dep_info *info, na_pair *source_na, __isl_keep isl_map *source_map)
2437 isl_space *space;
2438 isl_id *source_id;
2439 int n_param;
2441 source_id = create_shared_id(isl_map_get_ctx(source_map), source_na);
2443 sink = mark_definite_source(sink, info, source_na, source_map);
2445 if (isl_set_is_wrapping(info->read_na_pair->node->source->set))
2446 sink = remove_inconsistencies(sink, info, source_id);
2448 space = isl_set_get_space(sink);
2449 n_param = isl_space_dim(space, isl_dim_param);
2450 for (int i = 0; i < n_param; ++i) {
2451 isl_id *other_id;
2453 if (!is_shared(space, i))
2454 continue;
2455 other_id = isl_space_get_dim_id(space, isl_dim_param, i);
2457 if (other_id != source_id) {
2458 na_pair *other_na;
2460 other_na = (na_pair *) isl_id_get_user(other_id);
2461 sink = set_parameter_bounds(sink, other_na, info);
2462 sink = remove_inconsistencies(sink, info, other_id,
2463 source_id);
2466 isl_id_free(other_id);
2468 isl_space_free(space);
2470 isl_id_free(source_id);
2471 return sink;
2474 /* Compute a restriction for the given sink.
2475 * That is, add constraints to the parameters expressing
2476 * that the source is either not executed with info->n_shared shared
2477 * iterators (*_shared < info->n_shared)
2478 * or it is executed (*_shared >= info->n_shared) and then the last iteration
2479 * satisfies the corresponding memory based dependence.
2481 * Only do this for that part of "sink" that has any corresponding
2482 * sources in "source_map". The remaining part of "sink" is not affected.
2484 * Note that the "sink" set may have undergone a refinement based
2485 * on the _shared parameters and we want this refinement to also
2486 * be present in the sink restriction. We therefore need
2487 * to intersect the affected part with "sink".
2489 static __isl_give isl_set *compute_sink_restriction(
2490 __isl_keep isl_map *source_map, __isl_keep isl_set *sink,
2491 na_pair *source_na, add_dep_info *info)
2493 isl_set *with_source, *without_source;
2495 with_source = isl_map_domain(isl_map_copy(source_map));
2496 without_source = isl_set_subtract(isl_set_copy(sink),
2497 isl_set_copy(with_source));
2498 with_source = isl_set_intersect(with_source, isl_set_copy(sink));
2500 with_source = set_parameter_bounds(with_source, source_na, info);
2501 with_source = remove_inconsistencies(with_source, info, source_na,
2502 source_map);
2504 return isl_set_union(with_source, without_source);
2507 /* How many loops do "node1" and "node2" share?
2509 static int max_shared(pdg::node *node1, pdg::node *node2)
2511 int shared = 0;
2512 int size = node1->prefix.size();
2514 if (node2->prefix.size() < size)
2515 size = node2->prefix.size();
2517 for (int i = 0; i < size && node1->prefix[i] == node2->prefix[i]; ++i)
2518 if (node1->prefix[i] == -1)
2519 ++shared;
2521 return shared;
2524 /* Determine the number of shared loop iterators between sink
2525 * and source domains in "sink2source". That is, find out how
2526 * many of the initial input and output dimensions are equal
2527 * to each other. Return the result.
2529 * The first time this function is called for a given sink access
2530 * (info->n_shared is set to -1 in add_dep_info::set_read_na),
2531 * we check for equal dimensions up to the shared nesting depth.
2532 * Later call check dimensions up to the result of the previous call.
2534 static int extract_shared_levels(__isl_keep isl_map *sink2source,
2535 na_pair *source_na, add_dep_info *info)
2537 int i;
2538 int max;
2539 isl_space *space;
2541 max = info->n_shared;
2542 if (max < 0)
2543 max = max_shared(source_na->node, info->read_na_pair->node);
2545 if (max == 0)
2546 return info->n_shared = 0;
2548 space = isl_map_get_space(sink2source);
2549 for (i = 0; i < max; ++i) {
2550 isl_map *test;
2551 int subset;
2553 test = isl_map_universe(isl_space_copy(space));
2554 test = isl_map_equate(test, isl_dim_in, i, isl_dim_out, i);
2555 subset = isl_map_is_subset(sink2source, test);
2556 isl_map_free(test);
2558 if (!subset)
2559 break;
2561 isl_space_free(space);
2563 return i;
2566 /* The last iteration referred to by the sink may have been added
2567 * at a different nesting level. This means that __last_<na>_shared
2568 * is greater than or equal to a value greater than info->n_shared
2569 * and that therefore the iterators between info->n_shared and
2570 * __last_<na>_shared are not represented as they are implicitly
2571 * considered to be equal to the corresponding sink iterator.
2572 * For consistency, we need to explicitly add those iterators
2573 * and set them to be equal to the corresponding sink iterator.
2575 * In particular, we create a set in the space of the sink of the form
2577 * { s : __last_<na>_shared < info->n_shared or
2578 * (__last_<na>_<i> is a potential source iteration for s and
2579 * (__last_<na>_shared >= max_shared or
2580 * (__last_<na>_shared = max_shared - 1 and
2581 * the first max_shared - 1 iterators are equal and
2582 * iterator max_shared - 1 of the source is smaller) or
2583 * ...
2584 * (__last_<na>_shared = info->n_shared and
2585 * the first n_shared iterators are equal and
2586 * iterator n_shared of the source is smaller))) }
2588 * That is, for those parts with __last_<na>_shared smaller than
2589 * max_n_shared[source_na], intersection with the set will introduce
2590 * __last_<na>_<i> parameters (assuming they don't have a known fixed value)
2591 * up until __last_<na>_shared and equate them to the corresponding iterators
2592 * of the sink.
2594 static __isl_give isl_set *internal_shared_refinement(
2595 __isl_keep isl_id *shared_id, add_dep_info *info)
2597 isl_map *mem;
2598 na_pair *source_na;
2599 isl_set *domain;
2600 isl_map *shared_map;
2601 int shared_pos;
2602 isl_set *valid;
2603 isl_set *invalid;
2605 source_na = (na_pair *) isl_id_get_user(shared_id);
2607 mem = info->get_mem_dep(source_na);
2608 domain = isl_set_universe(isl_space_domain(isl_map_get_space(mem)));
2609 domain = add_parametrization(domain, source_na, info);
2610 mem = isl_map_intersect_domain(mem, domain);
2612 shared_map = compute_shared_map(isl_map_get_space(mem), shared_id,
2613 info->n_shared, info->max_n_shared[source_na]);
2615 mem = isl_map_intersect(mem, shared_map);
2617 valid = isl_map_range(mem);
2618 invalid = isl_set_universe(isl_set_get_space(valid));
2619 shared_pos = isl_set_find_dim_by_id(invalid, isl_dim_param, shared_id);
2620 invalid = isl_set_upper_bound_si(invalid, isl_dim_param, shared_pos,
2621 info->n_shared - 1);
2623 return isl_set_union(valid, invalid);
2626 /* The last iteration of some source referred to by the sink may have been
2627 * added at a different nesting level. This means that __last_*_shared
2628 * is greater than or equal to a value greater than info->n_shared
2629 * and that therefore the iterators between info->n_shared and
2630 * __last_*_shared are not represented as they are implicitly
2631 * considered to be equal to the corresponding sink iterator.
2632 * For consistency, we need to explicitly add those iterators
2633 * and set them to be equal to the corresponding sink iterator.
2635 * For each of the __last_*_shared parameters, explicitly add
2636 * the implicitly equal __last_*_i iterators by intersecting
2637 * the sink with the set computed by internal_shared_refinement.
2639 static __isl_give isl_set *refine_shared_internal(__isl_take isl_set *sink,
2640 add_dep_info *info)
2642 isl_space *space;
2643 isl_set *refinement;
2644 int n_param;
2646 space = isl_set_get_space(sink);
2647 refinement = isl_set_universe(isl_space_copy(space));
2649 n_param = isl_space_dim(space, isl_dim_param);
2650 for (int i = 0; i < n_param; ++i) {
2651 isl_set *ref_i;
2652 isl_id *id;
2654 if (!is_shared(space, i))
2655 continue;
2657 id = isl_space_get_dim_id(space, isl_dim_param, i);
2658 ref_i = internal_shared_refinement(id, info);
2659 isl_id_free(id);
2660 refinement = isl_set_intersect(refinement, ref_i);
2662 isl_space_free(space);
2664 sink = isl_set_intersect(sink, refinement);
2666 return sink;
2669 /* Given a map from sinks to potential sources (sink2source),
2670 * check if any parametrization is needed.
2671 * Depending on the result, return either a universe restriction,
2672 * an empty restriction (if the sources cannot have executed),
2673 * a restriction that parametrizes the source and the sink
2674 * of the input of the computation of the last source
2675 * or a restriction that parametrizes the source of the output.
2677 * sink_map maps the domain of sink2source to the sink iteration domain.
2678 * source_map maps the range of sink2source to the source iteration domain.
2680 * Before we check if we need any parametrization, we update the number of
2681 * shared loop levels and add possibly missing __last_*_i iterators
2682 * (in refine_shared_internal). If parametrization turns out to be required,
2683 * we also update the minimal number of shared loop levels for the given
2684 * source.
2686 static __isl_give isl_restriction *compute_restriction_core(
2687 __isl_keep isl_map *sink2source,
2688 __isl_take isl_map *sink_map, __isl_take isl_map *source_map,
2689 __isl_keep isl_set *sink, na_pair *source_na, add_dep_info *info)
2691 isl_space *space;
2692 isl_set *source_restr;
2693 isl_set *sink_restr;
2694 int need;
2696 sink2source = isl_map_copy(sink2source);
2697 sink = isl_set_copy(sink);
2699 sink2source = isl_map_apply_range(sink2source,
2700 isl_map_copy(source_map));
2701 sink2source = isl_map_apply_domain(sink2source,
2702 isl_map_copy(sink_map));
2703 sink = isl_set_apply(sink, isl_map_copy(sink_map));
2705 info->n_shared = extract_shared_levels(sink2source, source_na, info);
2706 sink = refine_shared_internal(sink, info);
2708 need = need_parametrization(sink2source, sink, source_na, info);
2709 if (need == RESTRICT_ERROR ||
2710 need == RESTRICT_NO || need == RESTRICT_EMPTY) {
2711 isl_map_free(source_map);
2712 isl_map_free(sink_map);
2713 isl_set_free(sink);
2714 if (need == RESTRICT_ERROR) {
2715 isl_map_free(sink2source);
2716 return NULL;
2717 } else if (need == RESTRICT_NO)
2718 return isl_restriction_none(sink2source);
2719 else
2720 return isl_restriction_empty(sink2source);
2723 info->update_min_n_shared(source_na);
2725 space = isl_map_get_space(source_map);
2726 source_restr = isl_set_universe(isl_space_range(space));
2728 source_restr = add_parametrization(source_restr, source_na, info);
2729 source_restr = isl_set_apply(source_restr, isl_map_reverse(source_map));
2731 if (need == RESTRICT_OUTPUT) {
2732 isl_map_free(sink_map);
2733 isl_set_free(sink);
2734 isl_map_free(sink2source);
2735 return isl_restriction_output(source_restr);
2738 sink_restr = compute_sink_restriction(sink2source, sink,
2739 source_na, info);
2740 sink_restr = isl_set_apply(sink_restr, isl_map_reverse(sink_map));
2742 isl_set_free(sink);
2743 isl_map_free(sink2source);
2745 return isl_restriction_input(source_restr, sink_restr);
2748 /* Compute a restriction for the given map from sinks to potential sources
2749 * (sink2source).
2751 * First check if the sink access has any filters. If so, compose the original
2752 * sink_map with a mapping that projects out these access filters.
2753 * Handle the source access similarly.
2754 * Then call compute_restriction_core to perform the main computation.
2756 static __isl_give isl_restriction *compute_restriction(
2757 __isl_keep isl_map *sink2source,
2758 __isl_take isl_map *sink_map, __isl_take isl_map *source_map,
2759 __isl_keep isl_set *sink, na_pair *source_na, add_dep_info *info)
2761 na_pair *sink_na = info->read_na_pair;
2763 if (sink_na->access->nested.size() > 0) {
2764 isl_space *space;
2765 isl_map *map;
2767 space = isl_space_range(isl_map_get_space(sink_map));
2768 space = isl_space_unwrap(space);
2769 map = isl_map_domain_map(isl_map_universe(space));
2771 sink_map = isl_map_apply_range(sink_map, map);
2774 if (source_na->access->nested.size() > 0) {
2775 isl_space *space;
2776 isl_map *map;
2778 space = isl_space_range(isl_map_get_space(source_map));
2779 space = isl_space_unwrap(space);
2780 map = isl_map_domain_map(isl_map_universe(space));
2782 source_map = isl_map_apply_range(source_map, map);
2785 return compute_restriction_core(sink2source, sink_map, source_map,
2786 sink, source_na, info);
2789 /* Compute a restriction for the given map from sinks to potential sources
2790 * (sink2source). We simply call compute_restriction to compute the
2791 * restriction. Since, unlike the case of do_restrict_domain_map bewloe,
2792 * we didn't encode the entire access relation in the domains of the input
2793 * to isl_access_info_compute_flow, we pass identity mappings on the source
2794 * and sink to compute_restriction.
2796 static __isl_give isl_restriction *do_restrict(__isl_keep isl_map *sink2source,
2797 __isl_keep isl_set *sink, void *source_user, void *user)
2799 na_pair *source_na = (na_pair *) source_user;
2800 add_dep_info *info = (struct add_dep_info *) user;
2801 isl_space *space;
2802 isl_map *source_map;
2803 isl_map *sink_map;
2805 space = isl_space_domain(isl_map_get_space(sink2source));
2806 sink_map = isl_map_identity(isl_space_map_from_set(space));
2807 space = isl_space_range(isl_map_get_space(sink2source));
2808 source_map = isl_map_identity(isl_space_map_from_set(space));
2810 return compute_restriction(sink2source, sink_map, source_map,
2811 sink, source_na, info);
2814 /* Does the iteration domain of any of the writes involve any filters?
2816 static bool any_filters(vector<na_pair> &writers)
2818 for (int i = 0; i < writers.size(); ++i)
2819 if (isl_set_is_wrapping(writers[i].node->source->set))
2820 return true;
2821 return false;
2824 /* Does any of the dependences starting at "first" have
2825 * controlled dependence relation?
2827 static bool any_controlled_dependences(PDG *pdg, int first)
2829 for (int i = first; i < pdg->dependences.size(); ++i)
2830 if (pdg->dependences[i]->controlled_relation)
2831 return true;
2833 return false;
2836 /* Remove parameters from "map" that start with "prefix".
2838 static __isl_give isl_map *remove_source(__isl_take isl_map *map,
2839 const char *prefix)
2841 int n = isl_map_dim(map, isl_dim_param);
2842 size_t len = strlen(prefix);
2844 for (int i = n - 1; i >= 0; --i) {
2845 const char *name;
2847 name = isl_map_get_dim_name(map, isl_dim_param, i);
2848 if (strncmp(name, prefix, len))
2849 continue;
2851 map = isl_map_project_out(map, isl_dim_param, i, 1);
2854 map = isl_map_coalesce(map);
2856 return map;
2859 /* Remove parameters from the dependences starting at "first"
2860 * that refer to any of the unused potential sources, i.e.,
2861 * those potential sources that are in writers, but not in info->used.
2863 * Since the current sink does not depend on those unused potential sources,
2864 * the corresponding dependence relations cannot depend on them and
2865 * any reference to them can simply be projected out.
2867 static void remove_unused_sources(PDG *pdg, int first,
2868 vector<na_pair> &writers, add_dep_info *info)
2870 char name[60];
2871 std::vector<na_pair *> unused;
2873 for (int i = 0; i < writers.size(); ++i) {
2874 if (info->used.find(&writers[i]) == info->used.end())
2875 unused.push_back(&writers[i]);
2878 if (unused.size() == 0)
2879 return;
2880 if (!any_controlled_dependences(pdg, first))
2881 return;
2883 for (int i = 0; i < unused.size(); ++i) {
2884 na_pair *na = unused[i];
2886 snprintf(name, sizeof(name), "__last_%s_%d_",
2887 na->node->name->s.c_str(), na->access->nr);
2889 for (int j = first; j < pdg->dependences.size(); ++j) {
2890 pdg::dependence *dep = pdg->dependences[j];
2891 isl_map *map;
2893 if (!dep->controlled_relation)
2894 continue;
2896 map = dep->controlled_relation->map;
2897 map = remove_source(map, name);
2898 dep->controlled_relation->map = map;
2903 /* Look for the unique write access that writes to the array accessed
2904 * by "a" and return an na_pair consisting of the node in which the
2905 * access is performed and the access itself.
2907 static na_pair find_unique_source(pdg::PDG* pdg, pdg::array *a)
2909 for (int i = 0; i < pdg->nodes.size(); ++i) {
2910 pdg::node *node = pdg->nodes[i];
2911 pdg::statement *s = pdg->nodes[i]->statement;
2912 for (int j = 0; j < s->accesses.size(); ++j) {
2913 pdg::access *access = s->accesses[j];
2914 if (access->array != a)
2915 continue;
2916 if (access->type != pdg::access::write)
2917 continue;
2918 return na_pair(node, access);
2922 assert(0);
2925 /* Add a dependence from (source_node, source_access) to
2926 * (sink_node, sink_access) to pdg->dependences, for the
2927 * case where the array being accessed is marked uniquely_defined.
2929 * Since the array is marked uniquely_defined, the value based
2930 * dependence is equal to the memory based dependence, so we
2931 * simply need to compose the access relations to obtain
2932 * the dependence relation.
2933 * This dependence relation is then specialized with respect to
2934 * the context and the iteration domain of the sink.
2936 static void add_unique_dep(PDG *pdg, pdg::node *source_node,
2937 pdg::access *source_access, pdg::node *sink_node,
2938 pdg::access *sink_access)
2940 pdg::dependence *d;
2941 isl_set *dom;
2942 isl_map *dep;
2943 isl_map *read;
2945 d = new pdg::dependence;
2946 d->array = source_access->array;
2947 d->type = pdg::dependence::flow;
2948 d->from = source_node;
2949 d->to = sink_node;
2950 d->from_access = source_access;
2951 d->to_access = sink_access;
2953 dep = source_access->map->get_isl_map();
2954 read = sink_access->map->get_isl_map();
2955 dep = isl_map_apply_range(dep, isl_map_reverse(read));
2956 dom = sink_node->source->get_isl_set();
2957 if (isl_set_is_wrapping(dom))
2958 dom = isl_map_domain(isl_set_unwrap(dom));
2959 dep = isl_map_intersect_range(dep, dom);
2960 dep = isl_map_intersect_params(dep, pdg->get_context_isl_set());
2961 d->relation = new pdg::IslMap(dep);
2963 pdg->dependences.push_back(d);
2966 /* Find the flow dependences associated to the array "a", which is marked
2967 * uniquely_defined, and add them to pdg->dependences.
2969 * First determine the unique source and then iterate through all the reads,
2970 * adding dependences from the unique source to each of the reads.
2972 static void find_unique_deps(PDG *pdg, pdg::array *a)
2974 na_pair na = find_unique_source(pdg, a);
2976 for (int i = 0; i < pdg->nodes.size(); ++i) {
2977 pdg::node *node = pdg->nodes[i];
2978 pdg::statement *s = pdg->nodes[i]->statement;
2979 for (int j = 0; j < s->accesses.size(); ++j) {
2980 pdg::access *access = s->accesses[j];
2981 if (access->array != a)
2982 continue;
2983 if (access->type != pdg::access::read)
2984 continue;
2985 add_unique_dep(pdg, na.node, na.access, node, access);
2990 /* Find the dependence of type "t" associated to array "a" and add them
2991 * to pdg->dependences.
2993 * If we are looking for flow dependences for an array that is marked
2994 * uniquely_defined, then we do not need to compute anything, but instead
2995 * can simply read off the dependences in find_unique_deps.
2997 void find_deps(PDG* pdg, pdg::array *a, type t)
2999 isl_ctx *ctx = pdg->get_isl_ctx();
3000 int nparam = pdg->params.size();
3001 int selfinput = 0;
3002 int reuse = 0;
3003 int firstuse = 0;
3004 add_dep_info info = { pdg, a, t };
3005 isl_set *context;
3006 bool need_parametrization;
3008 switch (t) {
3009 case flow:
3010 info.dtype = pdg::dependence::flow;
3011 firstuse = 1;
3012 break;
3013 case anti:
3014 info.dtype = pdg::dependence::anti;
3015 break;
3016 case data_reuse:
3017 info.dtype = pdg::dependence::reuse;
3018 reuse = 1;
3019 firstuse = 1;
3020 break;
3021 case reuse_pair:
3022 info.dtype = pdg::dependence::reuse_pair;
3023 break;
3024 case output:
3025 info.dtype = pdg::dependence::output;
3026 break;
3029 a->analysis_performed.push_back(new pdg::dependence_type(info.dtype));
3031 if (t == flow && a->uniquely_defined) {
3032 find_unique_deps(pdg, a);
3033 return;
3036 vector<na_pair> readers;
3037 vector<na_pair> writers;
3038 for (int i = 0; i < pdg->nodes.size(); ++i) {
3039 pdg::node *node = pdg->nodes[i];
3040 pdg::statement *s = pdg->nodes[i]->statement;
3041 for (int j = 0; j < s->accesses.size(); ++j) {
3042 pdg::access *access = s->accesses[j];
3043 if (access->array != a)
3044 continue;
3045 switch (t) {
3046 case flow:
3047 case data_reuse:
3048 case anti:
3049 if ((access->type == pdg::access::read) ^ (t == anti))
3050 readers.push_back(na_pair(node, access));
3051 else
3052 writers.push_back(na_pair(node, access));
3053 break;
3054 case output:
3055 if (access->type == pdg::access::read)
3056 continue;
3057 case reuse_pair:
3058 readers.push_back(na_pair(node, access));
3059 writers.push_back(na_pair(node, access));
3060 break;
3065 int maxsize = (selfinput || reuse) ? writers.size() + readers.size()
3066 : writers.size();
3067 context = pdg->get_context_isl_set();
3068 info.precedes_level = (isl_access_level_before)
3069 ((t == reuse_pair) ? precedes_level_accesses
3070 : precedes_level_nodes);
3071 need_parametrization = any_filters(writers);
3072 for (int i = 0; i < writers.size(); ++i) {
3073 writers[i].map = convert_access(&writers[i]);
3074 writers[i].project_out_access_filters();
3076 for (int i = 0; i < readers.size(); ++i) {
3077 readers[i].map = convert_access(&readers[i]);
3078 readers[i].map = isl_map_intersect_params(readers[i].map,
3079 isl_set_copy(context));
3080 readers[i].project_out_access_filters();
3082 for (int i = 0; i < readers.size(); ++i) {
3083 isl_access_info *acc;
3084 int n_dep = pdg->dependences.size();
3086 acc = isl_access_info_alloc(isl_map_copy(readers[i].map),
3087 &readers[i], info.precedes_level, maxsize);
3088 if (need_parametrization)
3089 acc = isl_access_info_set_restrict(acc, &do_restrict, &info);
3090 for (int j = 0; j < writers.size(); ++j)
3091 acc = isl_access_info_add_source(acc,
3092 isl_map_copy(writers[j].map), 1, &writers[j]);
3093 if (selfinput && writers.size()) {
3094 pdg::node *readnode = readers[i].node;
3095 for (int j = 0; j < readers.size(); ++j) {
3096 if (readers[j].node == readnode)
3097 acc = isl_access_info_add_source(acc,
3098 isl_map_copy(readers[j].map), 1, &readers[j]);
3101 if (reuse) {
3102 for (int j = 0; j < readers.size(); ++j)
3103 acc = isl_access_info_add_source(acc,
3104 isl_map_copy(readers[j].map), 1, &readers[j]);
3106 info.set_read_na(&readers[i]);
3107 isl_flow *deps = isl_access_info_compute_flow(acc);
3108 isl_flow_foreach(deps, add_dep, &info);
3109 isl_map *no_source;
3110 no_source = isl_flow_get_no_source(deps, 1);
3111 no_source = isl_map_from_range(isl_map_domain(no_source));
3112 no_source = simplify_controls(no_source, &info, NULL);
3113 if (!isl_map_plain_is_empty(no_source) && firstuse) {
3114 pdg::dependence *d = new pdg::dependence;
3115 d->array = a;
3116 d->type = pdg::dependence::uninitialized;
3117 d->to = readers[i].node;
3118 d->to_access = readers[i].access;
3119 if (d->to_access->extension) {
3120 d->extended_relation = new pdg::IslMap(isl_map_copy(no_source));
3121 no_source = isl_map_apply_range(no_source,
3122 isl_map_reverse(
3123 d->to_access->extension->get_isl_map(ctx)));
3125 d->relation = new pdg::IslMap(isl_map_copy(no_source));
3126 pdg->dependences.push_back(d);
3128 isl_map_free(no_source);
3129 isl_flow_free(deps);
3130 remove_unused_sources(pdg, n_dep, writers, &info);
3132 isl_set_free(context);
3135 /* Add a dependence from "write" to "a" to a->sources.
3137 static void add_unique_source(pdg::access *a, pdg::access *write)
3139 isl_map *dep;
3140 isl_map *read;
3142 dep = isl_map_range_map(write->map->get_isl_map());
3143 read = isl_map_range_map(a->map->get_isl_map());
3144 dep = isl_map_apply_range(dep, isl_map_reverse(read));
3146 a->sources.push_back(new pdg::IslMap(dep));
3149 /* Look for the unique write access that writes to the array accessed
3150 * by "a" and then add a dependence from that write to a->sources.
3152 static void add_unique_source(pdg::PDG* pdg, pdg::access *a)
3154 na_pair na = find_unique_source(pdg, a->array);
3155 add_unique_source(a, na.access);
3158 extern "C" {
3159 static isl_stat add_source(__isl_take isl_map *dep, int must,
3160 void *dep_user, void *user);
3163 /* Add "dep" to a->sources, provided it is exact, and return isl_stat_ok.
3164 * Otherwise, return isl_stat_error.
3166 static isl_stat add_source(__isl_take isl_map *dep, int must, void *dep_user,
3167 void *user)
3169 bool has_controls;
3170 pdg::access *a = (pdg::access *) user;
3172 dep = remove_redundant_controls(dep, &has_controls);
3173 if (has_controls) {
3174 isl_map_free(dep);
3175 return isl_stat_error;
3178 a->sources.push_back(new pdg::IslMap(dep));
3180 return isl_stat_ok;
3183 extern "C" {
3184 static __isl_give isl_restriction *do_restrict_domain_map(
3185 __isl_keep isl_map *source_map, __isl_keep isl_set *sink,
3186 void *source_user, void *user);
3189 /* Compute a restriction for the given map from sinks to potential sources
3190 * (sink2source). We simply call compute_restriction to compute the
3191 * restriction. This function is used from within find_sources,
3192 * which encodes the entire access relation into the domains of
3193 * the access relations passed to isl_access_info_compute_flow.
3194 * That is, the access relations passed to isl_access_info_compute_flow
3195 * are the result of applying isl_map_range_map to the original access
3196 * relations. We therefore pass mappings that undo this encoding
3197 * to compute_restriction.
3199 static __isl_give isl_restriction *do_restrict_domain_map(
3200 __isl_keep isl_map *source_map, __isl_keep isl_set *sink,
3201 void *source_user, void *user)
3203 na_pair *source_na = (na_pair *) source_user;
3204 add_dep_info *info = (struct add_dep_info *) user;
3205 isl_space *space;
3206 isl_map *source_domain_map, *sink_domain_map;
3208 space = isl_space_range(isl_map_get_space(source_map));
3209 space = isl_space_unwrap(space);
3210 source_domain_map = isl_map_domain_map(isl_map_universe(space));
3211 space = isl_space_domain(isl_map_get_space(source_map));
3212 space = isl_space_unwrap(space);
3213 sink_domain_map = isl_map_domain_map(isl_map_universe(space));
3215 return compute_restriction(source_map, sink_domain_map,
3216 source_domain_map, sink, source_na, info);
3219 /* Find the sources of (read) access "a" in node "node".
3220 * If they are complete (no uninitialized accesses) and exact,
3221 * then put them in a->sources. Otherwise, discard them.
3223 * If the array is marked uniquely_defined, then we simply look
3224 * for the defining write in find_unique_source.
3226 * Otherwise, we look for all writes that write to the same array,
3227 * perform dependence analysis and then check whether
3228 * the result is complete and exact.
3230 * The sources record not only the node iteration, but also
3231 * the index of the array element. We therefore apply
3232 * isl_map_range_map to the access relations, to obtain
3233 * a relation from the access (iteration -> element)
3234 * to the array element and feed that to the dependence analysis engine.
3236 void find_sources(pdg::PDG* pdg, pdg::node *node, pdg::access *a)
3238 isl_set *context;
3239 isl_map *no_source;
3240 isl_access_info *acc;
3241 isl_flow *deps;
3242 vector<na_pair> writers;
3243 na_pair reader(node, a);
3244 add_dep_info info = { pdg, a->array, flow, pdg::dependence::flow };
3246 if (a->array->uniquely_defined) {
3247 add_unique_source(pdg, a);
3248 return;
3251 info.precedes_level = (isl_access_level_before) precedes_level_nodes;
3253 for (int i = 0; i < pdg->nodes.size(); ++i) {
3254 pdg::node *node = pdg->nodes[i];
3255 pdg::statement *s = pdg->nodes[i]->statement;
3256 for (int j = 0; j < s->accesses.size(); ++j) {
3257 pdg::access *access = s->accesses[j];
3258 if (access->array != a->array)
3259 continue;
3260 if (access->type != pdg::access::write)
3261 continue;
3262 writers.push_back(na_pair(node, access));
3266 context = pdg->get_context_isl_set();
3267 for (int i = 0; i < writers.size(); ++i) {
3268 writers[i].projected_map = convert_access(&writers[i]);
3269 writers[i].map = isl_map_copy(writers[i].projected_map);
3270 writers[i].map = isl_map_range_map(writers[i].map);
3271 writers[i].project_out_access_filters();
3273 reader.projected_map = convert_access(&reader);
3274 reader.projected_map = isl_map_intersect_params(reader.projected_map,
3275 context);
3276 reader.map = isl_map_range_map(isl_map_copy(reader.projected_map));
3277 reader.project_out_access_filters();
3279 acc = isl_access_info_alloc(isl_map_copy(reader.map),
3280 &reader, info.precedes_level, writers.size());
3281 for (int j = 0; j < writers.size(); ++j)
3282 acc = isl_access_info_add_source(acc,
3283 isl_map_copy(writers[j].map), 1, &writers[j]);
3285 if (any_filters(writers))
3286 acc = isl_access_info_set_restrict(acc,
3287 &do_restrict_domain_map, &info);
3289 info.set_read_na(&reader);
3290 deps = isl_access_info_compute_flow(acc);
3291 no_source = isl_flow_get_no_source(deps, 1);
3292 if (isl_map_plain_is_empty(no_source)) {
3293 if (isl_flow_foreach(deps, add_source, a) < 0) {
3294 for (int i = 0; i < a->sources.size(); ++i)
3295 delete a->sources[i];
3296 a->sources.resize(0);
3299 isl_map_free(no_source);
3300 isl_flow_free(deps);
3303 /* Find the source (if possible) of the filter "coa" in node "node".
3304 * We assume that the filter is an access rather than a function call.
3306 static void find_sources(pdg::PDG *pdg, pdg::node *node,
3307 pdg::call_or_access *coa)
3309 pdg::access *access;
3311 assert(coa->type == pdg::call_or_access::t_access);
3312 access = coa->access;
3314 find_sources(pdg, node, access);
3317 /* Compute the sources (if possible) for all the filters in all the
3318 * nodes and accesses in "pdg".
3320 void compute_filter_sources(pdg::PDG *pdg)
3322 for (int i = 0; i < pdg->nodes.size(); ++i) {
3323 pdg::node *node = pdg->nodes[i];
3324 pdg::statement *s = pdg->nodes[i]->statement;
3325 int n_filter = node->filters.size();
3327 for (int j = 0; j < n_filter; ++j)
3328 find_sources(pdg, node, node->filters[j]);
3330 for (int j = 0; j < s->accesses.size(); ++j) {
3331 pdg::access *access = s->accesses[j];
3333 for (int k = 0; k < access->nested.size(); ++k)
3334 find_sources(pdg, node, access->nested[k]);
3339 static int precedes_level_nodes(na_pair *first, na_pair *second)
3341 int d = 0;
3342 int cmp = 0;
3343 for (int i = 0; i < first->node->prefix.size(); ++i) {
3344 if (i >= second->node->prefix.size())
3345 break;
3346 cmp = first->node->prefix[i] - second->node->prefix[i];
3347 if (cmp)
3348 break;
3349 if (first->node->prefix[i] == -1)
3350 ++d;
3352 return 2*d + (cmp<0);
3355 static int precedes_level_accesses(na_pair *first, na_pair *second)
3357 int cmp = 0;
3358 int d = 0;
3359 for (int i = 0; i < first->node->prefix.size(); ++i) {
3360 if (i >= second->node->prefix.size())
3361 break;
3362 cmp = first->node->prefix[i] - second->node->prefix[i];
3363 if (cmp)
3364 break;
3365 if (first->node->prefix[i] == -1)
3366 ++d;
3368 /* same node; now compare accesses */
3369 if (!cmp)
3370 cmp = first->access->nr - second->access->nr;
3371 return 2*d + (cmp<0);