isl_ast_codegen.c: refine_eliminated: do not add stride guard
[isl.git] / isl_ast_graft.c
blob450bef585b0bc56f2013f7420c29db3a4876bbef
1 /*
2 * Copyright 2012 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8 */
10 #include <isl_ast_private.h>
11 #include <isl_ast_build_expr.h>
12 #include <isl_ast_build_private.h>
13 #include <isl_ast_graft_private.h>
15 static __isl_give isl_ast_graft *isl_ast_graft_copy(
16 __isl_keep isl_ast_graft *graft);
18 #undef BASE
19 #define BASE ast_graft
21 #include <isl_list_templ.c>
23 #undef BASE
24 #define BASE ast_graft
25 #include <print_templ.c>
27 isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft)
29 if (!graft)
30 return NULL;
31 return isl_basic_set_get_ctx(graft->enforced);
34 __isl_give isl_ast_node *isl_ast_graft_get_node(
35 __isl_keep isl_ast_graft *graft)
37 return graft ? isl_ast_node_copy(graft->node) : NULL;
40 /* Create a graft for "node" with no guards and no enforced conditions.
42 __isl_give isl_ast_graft *isl_ast_graft_alloc(
43 __isl_take isl_ast_node *node, __isl_keep isl_ast_build *build)
45 isl_ctx *ctx;
46 isl_space *space;
47 isl_ast_graft *graft;
49 if (!node)
50 return NULL;
52 ctx = isl_ast_node_get_ctx(node);
53 graft = isl_calloc_type(ctx, isl_ast_graft);
54 if (!graft)
55 goto error;
57 space = isl_ast_build_get_space(build, 1);
59 graft->ref = 1;
60 graft->node = node;
61 graft->guard = isl_set_universe(isl_space_copy(space));
62 graft->enforced = isl_basic_set_universe(space);
64 if (!graft->guard || !graft->enforced)
65 return isl_ast_graft_free(graft);
67 return graft;
68 error:
69 isl_ast_node_free(node);
70 return NULL;
73 /* Create a graft with no guards and no enforced conditions
74 * encapsulating a call to the domain element specified by "executed".
75 * "executed" is assumed to be single-valued.
77 __isl_give isl_ast_graft *isl_ast_graft_alloc_domain(
78 __isl_take isl_map *executed, __isl_keep isl_ast_build *build)
80 isl_ast_node *node;
82 node = isl_ast_build_call_from_executed(build, executed);
84 return isl_ast_graft_alloc(node, build);
87 static __isl_give isl_ast_graft *isl_ast_graft_copy(
88 __isl_keep isl_ast_graft *graft)
90 if (!graft)
91 return NULL;
93 graft->ref++;
94 return graft;
97 /* Do all the grafts in "list" have the same guard and is this guard
98 * independent of the current depth?
100 static int equal_independent_guards(__isl_keep isl_ast_graft_list *list,
101 __isl_keep isl_ast_build *build)
103 int i, n;
104 int depth;
105 isl_ast_graft *graft_0;
106 int equal = 1;
107 int skip;
109 graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
110 if (!graft_0)
111 return -1;
113 depth = isl_ast_build_get_depth(build);
114 skip = isl_set_involves_dims(graft_0->guard, isl_dim_set, depth, 1);
115 if (skip < 0 || skip) {
116 isl_ast_graft_free(graft_0);
117 return skip < 0 ? -1 : 0;
120 n = isl_ast_graft_list_n_ast_graft(list);
121 for (i = 1; i < n; ++i) {
122 isl_ast_graft *graft;
123 graft = isl_ast_graft_list_get_ast_graft(list, i);
124 if (!graft)
125 equal = -1;
126 else
127 equal = isl_set_is_equal(graft_0->guard, graft->guard);
128 isl_ast_graft_free(graft);
129 if (equal < 0 || !equal)
130 break;
133 isl_ast_graft_free(graft_0);
135 return equal;
138 /* Hoist "guard" out of the current level (given by "build").
140 * In particular, eliminate the dimension corresponding to the current depth.
142 static __isl_give isl_set *hoist_guard(__isl_take isl_set *guard,
143 __isl_keep isl_ast_build *build)
145 int depth;
147 depth = isl_ast_build_get_depth(build);
148 if (depth < isl_set_dim(guard, isl_dim_set)) {
149 guard = isl_set_remove_divs_involving_dims(guard,
150 isl_dim_set, depth, 1);
151 guard = isl_set_eliminate(guard, isl_dim_set, depth, 1);
152 guard = isl_set_compute_divs(guard);
155 return guard;
158 /* Extract a common guard from the grafts in "list" that can be hoisted
159 * out of the current level. If no such guard can be found, then return
160 * a universal set.
162 * If all the grafts in the list have the same guard and if this guard
163 * is independent of the current level, then it can be hoisted out.
164 * If there is only one graft in the list and if its guard
165 * depends on the current level, then we eliminate this level and
166 * return the result.
168 * Otherwise, we return the unshifted simple hull of the guards.
169 * In order to be able to hoist as many constraints as possible,
170 * but at the same time avoid hoisting constraints that did not
171 * appear in the guards in the first place, we intersect the guards
172 * with all the information that is available (i.e., the domain
173 * from the build and the enforced constraints of the graft) and
174 * compute the unshifted hull of the result using only constraints
175 * from the original guards.
176 * In particular, intersecting the guards with other known information
177 * allows us to hoist guards that are only explicit is some of
178 * the grafts and implicit in the others.
180 * The special case for equal guards is needed in case those guards
181 * are non-convex. Taking the simple hull would remove information
182 * and would not allow for these guards to be hoisted completely.
184 static __isl_give isl_set *extract_hoistable_guard(
185 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
187 int i, n;
188 int equal;
189 isl_ctx *ctx;
190 isl_set *guard;
191 isl_set_list *set_list;
192 isl_basic_set *hull;
194 if (!list || !build)
195 return NULL;
197 n = isl_ast_graft_list_n_ast_graft(list);
198 if (n == 0)
199 return isl_set_universe(isl_ast_build_get_space(build, 1));
201 equal = equal_independent_guards(list, build);
202 if (equal < 0)
203 return NULL;
205 if (equal || n == 1) {
206 isl_ast_graft *graft_0;
208 graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
209 if (!graft_0)
210 return NULL;
211 guard = isl_set_copy(graft_0->guard);
212 if (!equal)
213 guard = hoist_guard(guard, build);
214 isl_ast_graft_free(graft_0);
215 return guard;
218 ctx = isl_ast_build_get_ctx(build);
219 set_list = isl_set_list_alloc(ctx, n);
220 guard = isl_set_empty(isl_ast_build_get_space(build, 1));
221 for (i = 0; i < n; ++i) {
222 isl_ast_graft *graft;
223 isl_basic_set *enforced;
224 isl_set *guard_i;
226 graft = isl_ast_graft_list_get_ast_graft(list, i);
227 enforced = isl_ast_graft_get_enforced(graft);
228 guard_i = isl_set_copy(graft->guard);
229 isl_ast_graft_free(graft);
230 set_list = isl_set_list_add(set_list, isl_set_copy(guard_i));
231 guard_i = isl_set_intersect(guard_i,
232 isl_set_from_basic_set(enforced));
233 guard_i = isl_set_intersect(guard_i,
234 isl_ast_build_get_domain(build));
235 guard = isl_set_union(guard, guard_i);
237 hull = isl_set_unshifted_simple_hull_from_set_list(guard, set_list);
238 guard = isl_set_from_basic_set(hull);
239 return hoist_guard(guard, build);
242 /* Internal data structure used inside insert_if.
244 * list is the list of guarded nodes created by each call to insert_if.
245 * node is the original node that is guarded by insert_if.
246 * build is the build in which the AST is constructed.
248 struct isl_insert_if_data {
249 isl_ast_node_list *list;
250 isl_ast_node *node;
251 isl_ast_build *build;
254 static int insert_if(__isl_take isl_basic_set *bset, void *user);
256 /* Insert an if node around "node" testing the condition encoded
257 * in guard "guard".
259 * If the user does not want any disjunctions in the if conditions
260 * and if "guard" does involve a disjunction, then we make the different
261 * disjuncts disjoint and insert an if node corresponding to each disjunct
262 * around a copy of "node". The result is then a block node containing
263 * this sequence of guarded copies of "node".
265 static __isl_give isl_ast_node *ast_node_insert_if(
266 __isl_take isl_ast_node *node, __isl_take isl_set *guard,
267 __isl_keep isl_ast_build *build)
269 struct isl_insert_if_data data;
270 isl_ctx *ctx;
272 ctx = isl_ast_build_get_ctx(build);
273 if (isl_options_get_ast_build_allow_or(ctx) ||
274 isl_set_n_basic_set(guard) <= 1) {
275 isl_ast_node *if_node;
276 isl_ast_expr *expr;
278 expr = isl_ast_build_expr_from_set(build, guard);
280 if_node = isl_ast_node_alloc_if(expr);
281 return isl_ast_node_if_set_then(if_node, node);
284 guard = isl_set_make_disjoint(guard);
286 data.list = isl_ast_node_list_alloc(ctx, 0);
287 data.node = node;
288 data.build = build;
289 if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0)
290 data.list = isl_ast_node_list_free(data.list);
292 isl_set_free(guard);
293 isl_ast_node_free(data.node);
294 return isl_ast_node_alloc_block(data.list);
297 /* Insert an if node around a copy of "data->node" testing the condition
298 * encoded in guard "bset" and add the result to data->list.
300 static int insert_if(__isl_take isl_basic_set *bset, void *user)
302 struct isl_insert_if_data *data = user;
303 isl_ast_node *node;
304 isl_set *set;
306 set = isl_set_from_basic_set(bset);
307 node = isl_ast_node_copy(data->node);
308 node = ast_node_insert_if(node, set, data->build);
309 data->list = isl_ast_node_list_add(data->list, node);
311 return 0;
314 /* Insert an if node around graft->node testing the condition encoded
315 * in guard "guard", assuming guard involves any conditions.
317 static __isl_give isl_ast_graft *insert_if_node(
318 __isl_take isl_ast_graft *graft, __isl_take isl_set *guard,
319 __isl_keep isl_ast_build *build)
321 int univ;
323 if (!graft)
324 goto error;
326 univ = isl_set_plain_is_universe(guard);
327 if (univ < 0)
328 goto error;
329 if (univ) {
330 isl_set_free(guard);
331 return graft;
334 build = isl_ast_build_copy(build);
335 build = isl_ast_build_set_enforced(build,
336 isl_ast_graft_get_enforced(graft));
337 graft->node = ast_node_insert_if(graft->node, guard, build);
338 isl_ast_build_free(build);
340 if (!graft->node)
341 return isl_ast_graft_free(graft);
343 return graft;
344 error:
345 isl_set_free(guard);
346 return isl_ast_graft_free(graft);
349 /* Insert an if node around graft->node testing the condition encoded
350 * in graft->guard, assuming graft->guard involves any conditions.
352 static __isl_give isl_ast_graft *insert_pending_guard_node(
353 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
355 if (!graft)
356 return NULL;
358 return insert_if_node(graft, isl_set_copy(graft->guard), build);
361 /* Replace graft->enforced by "enforced".
363 __isl_give isl_ast_graft *isl_ast_graft_set_enforced(
364 __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
366 if (!graft || !enforced)
367 goto error;
369 isl_basic_set_free(graft->enforced);
370 graft->enforced = enforced;
372 return graft;
373 error:
374 isl_basic_set_free(enforced);
375 return isl_ast_graft_free(graft);
378 /* Update "enforced" such that it only involves constraints that are
379 * also enforced by "graft".
381 static __isl_give isl_basic_set *update_enforced(
382 __isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft,
383 int depth)
385 isl_basic_set *enforced_g;
387 enforced_g = isl_ast_graft_get_enforced(graft);
388 if (depth < isl_basic_set_dim(enforced_g, isl_dim_set))
389 enforced_g = isl_basic_set_eliminate(enforced_g,
390 isl_dim_set, depth, 1);
391 enforced_g = isl_basic_set_remove_unknown_divs(enforced_g);
392 enforced_g = isl_basic_set_align_params(enforced_g,
393 isl_basic_set_get_space(enforced));
394 enforced = isl_basic_set_align_params(enforced,
395 isl_basic_set_get_space(enforced_g));
396 enforced = isl_set_simple_hull(isl_basic_set_union(enforced,
397 enforced_g));
399 return enforced;
402 /* Extend the node at *body with node.
404 * If body points to the else branch, then *body may still be NULL.
405 * If so, we simply attach node to this else branch.
406 * Otherwise, we attach a list containing the statements already
407 * attached at *body followed by node.
409 static void extend_body(__isl_keep isl_ast_node **body,
410 __isl_take isl_ast_node *node)
412 isl_ast_node_list *list;
414 if (!*body) {
415 *body = node;
416 return;
419 if ((*body)->type == isl_ast_node_block) {
420 list = isl_ast_node_block_get_children(*body);
421 isl_ast_node_free(*body);
422 } else
423 list = isl_ast_node_list_from_ast_node(*body);
424 list = isl_ast_node_list_add(list, node);
425 *body = isl_ast_node_alloc_block(list);
428 /* Merge "graft" into the last graft of "list".
429 * body points to the then or else branch of an if node in that last graft.
431 * We attach graft->node to this branch and update the enforced
432 * set of the last graft of "list" to take into account the enforced
433 * set of "graft".
435 static __isl_give isl_ast_graft_list *graft_extend_body(
436 __isl_take isl_ast_graft_list *list,
437 __isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft,
438 __isl_keep isl_ast_build *build)
440 int n;
441 int depth;
442 isl_ast_graft *last;
443 isl_space *space;
444 isl_basic_set *enforced;
446 if (!list || !graft)
447 goto error;
448 extend_body(body, isl_ast_node_copy(graft->node));
449 if (!*body)
450 goto error;
452 n = isl_ast_graft_list_n_ast_graft(list);
453 last = isl_ast_graft_list_get_ast_graft(list, n - 1);
455 depth = isl_ast_build_get_depth(build);
456 space = isl_ast_build_get_space(build, 1);
457 enforced = isl_basic_set_empty(space);
458 enforced = update_enforced(enforced, last, depth);
459 enforced = update_enforced(enforced, graft, depth);
460 last = isl_ast_graft_set_enforced(last, enforced);
462 list = isl_ast_graft_list_set_ast_graft(list, n - 1, last);
463 isl_ast_graft_free(graft);
464 return list;
465 error:
466 isl_ast_graft_free(graft);
467 return isl_ast_graft_list_free(list);
470 /* Merge "graft" into the last graft of "list", attaching graft->node
471 * to the then branch of "last_if".
473 static __isl_give isl_ast_graft_list *extend_then(
474 __isl_take isl_ast_graft_list *list,
475 __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
476 __isl_keep isl_ast_build *build)
478 return graft_extend_body(list, &last_if->u.i.then, graft, build);
481 /* Merge "graft" into the last graft of "list", attaching graft->node
482 * to the else branch of "last_if".
484 static __isl_give isl_ast_graft_list *extend_else(
485 __isl_take isl_ast_graft_list *list,
486 __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
487 __isl_keep isl_ast_build *build)
489 return graft_extend_body(list, &last_if->u.i.else_node, graft, build);
492 /* This data structure keeps track of an if node.
494 * "node" is the actual if-node
495 * "guard" is the original, non-simplified guard of the node
496 * "complement" is the complement of "guard" in the context of outer if nodes
498 struct isl_if_node {
499 isl_ast_node *node;
500 isl_set *guard;
501 isl_set *complement;
504 /* Given a list of "n" if nodes, clear those starting at "first"
505 * and return "first" (i.e., the updated size of the array).
507 static int clear_if_nodes(struct isl_if_node *if_node, int first, int n)
509 int i;
511 for (i = first; i < n; ++i) {
512 isl_set_free(if_node[i].guard);
513 isl_set_free(if_node[i].complement);
516 return first;
519 /* For each graft in "list",
520 * insert an if node around graft->node testing the condition encoded
521 * in graft->guard, assuming graft->guard involves any conditions.
523 * We keep track of a list of generated if nodes that can be extended
524 * without changing the order of the elements in "list".
525 * If the guard of a graft is a subset of either the guard or its complement
526 * of one of those if nodes, then the node
527 * of the new graft is inserted into the then or else branch of the last graft
528 * and the current graft is discarded.
529 * The guard of the node is then simplified based on the conditions
530 * enforced at that then or else branch.
531 * Otherwise, the current graft is appended to the list.
533 * We only construct else branches if allowed by the user.
535 static __isl_give isl_ast_graft_list *insert_pending_guard_nodes(
536 __isl_take isl_ast_graft_list *list,
537 __isl_keep isl_ast_build *build)
539 int i, j, n, n_if;
540 int allow_else;
541 isl_ctx *ctx;
542 isl_ast_graft_list *res;
543 struct isl_if_node *if_node = NULL;
545 if (!build || !list)
546 return isl_ast_graft_list_free(list);
548 ctx = isl_ast_build_get_ctx(build);
549 n = isl_ast_graft_list_n_ast_graft(list);
551 allow_else = isl_options_get_ast_build_allow_else(ctx);
553 n_if = 0;
554 if (n > 1) {
555 if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1);
556 if (!if_node)
557 return isl_ast_graft_list_free(list);
560 res = isl_ast_graft_list_alloc(ctx, n);
562 for (i = 0; i < n; ++i) {
563 isl_set *guard;
564 isl_ast_graft *graft;
565 int subset, found_then, found_else;
566 isl_ast_node *node;
568 graft = isl_ast_graft_list_get_ast_graft(list, i);
569 if (!graft)
570 break;
571 subset = 0;
572 found_then = found_else = -1;
573 if (n_if > 0) {
574 isl_set *test;
575 test = isl_set_copy(graft->guard);
576 test = isl_set_intersect(test,
577 isl_set_copy(build->domain));
578 for (j = n_if - 1; j >= 0; --j) {
579 subset = isl_set_is_subset(test,
580 if_node[j].guard);
581 if (subset < 0 || subset) {
582 found_then = j;
583 break;
585 if (!allow_else)
586 continue;
587 subset = isl_set_is_subset(test,
588 if_node[j].complement);
589 if (subset < 0 || subset) {
590 found_else = j;
591 break;
594 n_if = clear_if_nodes(if_node, j + 1, n_if);
595 isl_set_free(test);
597 if (subset < 0) {
598 graft = isl_ast_graft_free(graft);
599 break;
602 guard = isl_set_copy(graft->guard);
603 if (found_then >= 0)
604 graft->guard = isl_set_gist(graft->guard,
605 isl_set_copy(if_node[found_then].guard));
606 else if (found_else >= 0)
607 graft->guard = isl_set_gist(graft->guard,
608 isl_set_copy(if_node[found_else].complement));
610 node = graft->node;
611 if (!graft->guard)
612 graft = isl_ast_graft_free(graft);
613 graft = insert_pending_guard_node(graft, build);
614 if (graft && graft->node != node && i != n - 1) {
615 isl_set *set;
616 if_node[n_if].node = graft->node;
617 if_node[n_if].guard = guard;
618 if (found_then >= 0)
619 set = if_node[found_then].guard;
620 else if (found_else >= 0)
621 set = if_node[found_else].complement;
622 else
623 set = build->domain;
624 set = isl_set_copy(set);
625 set = isl_set_subtract(set, isl_set_copy(guard));
626 if_node[n_if].complement = set;
627 n_if++;
628 } else
629 isl_set_free(guard);
630 if (!graft)
631 break;
633 if (found_then >= 0)
634 res = extend_then(res, if_node[found_then].node,
635 graft, build);
636 else if (found_else >= 0)
637 res = extend_else(res, if_node[found_else].node,
638 graft, build);
639 else
640 res = isl_ast_graft_list_add(res, graft);
642 if (i < n)
643 res = isl_ast_graft_list_free(res);
645 isl_ast_graft_list_free(list);
646 clear_if_nodes(if_node, 0, n_if);
647 free(if_node);
648 return res;
651 /* Collect the nodes contained in the grafts in "list" in a node list.
653 static __isl_give isl_ast_node_list *extract_node_list(
654 __isl_keep isl_ast_graft_list *list)
656 int i, n;
657 isl_ctx *ctx;
658 isl_ast_node_list *node_list;
660 if (!list)
661 return NULL;
662 ctx = isl_ast_graft_list_get_ctx(list);
663 n = isl_ast_graft_list_n_ast_graft(list);
664 node_list = isl_ast_node_list_alloc(ctx, n);
665 for (i = 0; i < n; ++i) {
666 isl_ast_node *node;
667 isl_ast_graft *graft;
669 graft = isl_ast_graft_list_get_ast_graft(list, i);
670 node = isl_ast_graft_get_node(graft);
671 node_list = isl_ast_node_list_add(node_list, node);
672 isl_ast_graft_free(graft);
675 return node_list;
678 /* Look for shared enforced constraints by all the elements in "list"
679 * on outer loops (with respect to the current depth) and return the result.
681 * We assume that the number of children is at least one.
683 static __isl_give isl_basic_set *extract_shared_enforced(
684 __isl_keep isl_ast_graft_list *list,
685 __isl_keep isl_ast_build *build)
687 int i, n;
688 int depth;
689 isl_space *space;
690 isl_basic_set *enforced;
692 if (!list)
693 return NULL;
695 n = isl_ast_graft_list_n_ast_graft(list);
696 if (n == 0)
697 isl_die(isl_ast_graft_list_get_ctx(list), isl_error_invalid,
698 "for node should have at least one child",
699 return NULL);
701 space = isl_ast_build_get_space(build, 1);
702 enforced = isl_basic_set_empty(space);
704 depth = isl_ast_build_get_depth(build);
705 for (i = 0; i < n; ++i) {
706 isl_ast_graft *graft;
708 graft = isl_ast_graft_list_get_ast_graft(list, i);
709 enforced = update_enforced(enforced, graft, depth);
710 isl_ast_graft_free(graft);
713 return enforced;
716 /* Record "guard" in "graft" so that it will be enforced somewhere
717 * up the tree. If the graft already has a guard, then it may be partially
718 * redundant in combination with the new guard and in the context
719 * of build->domain. We therefore (re)compute the gist of the intersection
720 * and coalesce the result.
722 static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft,
723 __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
725 int is_universe;
727 if (!graft)
728 goto error;
730 is_universe = isl_set_plain_is_universe(guard);
731 if (is_universe < 0)
732 goto error;
733 if (is_universe) {
734 isl_set_free(guard);
735 return graft;
738 graft->guard = isl_set_intersect(graft->guard, guard);
739 graft->guard = isl_ast_build_compute_gist(build, graft->guard);
740 graft->guard = isl_set_coalesce(graft->guard);
741 if (!graft->guard)
742 return isl_ast_graft_free(graft);
744 return graft;
745 error:
746 isl_set_free(guard);
747 return isl_ast_graft_free(graft);
750 /* For each graft in "list", replace its guard with the gist with
751 * respect to "context".
753 static __isl_give isl_ast_graft_list *gist_guards(
754 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *context)
756 int i, n;
758 if (!list)
759 return NULL;
761 n = isl_ast_graft_list_n_ast_graft(list);
762 for (i = 0; i < n; ++i) {
763 isl_ast_graft *graft;
765 graft = isl_ast_graft_list_get_ast_graft(list, i);
766 if (!graft)
767 break;
768 graft->guard = isl_set_gist(graft->guard,
769 isl_set_copy(context));
770 if (!graft->guard)
771 graft = isl_ast_graft_free(graft);
772 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
774 if (i < n)
775 return isl_ast_graft_list_free(list);
777 return list;
780 /* Combine the grafts in the list into a single graft.
782 * If "up" is set then the resulting graft will be used at an outer level.
783 * In this case, "build" refers to the outer level, while "sub_build"
784 * refers to the inner level. If "up" is not set, then the same build
785 * should be passed to both arguments.
787 * The guard is initialized to the shared guard of the list elements (if any),
788 * provided it does not depend on the current dimension.
789 * The guards in the elements are then simplified with respect to the
790 * hoisted guard and materialized as if nodes around the contained AST nodes
791 * in the context of "sub_build".
793 * The enforced set is initialized to the simple hull of the enforced sets
794 * of the elements, provided the ast_build_exploit_nested_bounds option is set
795 * or the new graft will be used at the same level.
797 * The node is initialized to either a block containing the nodes of "list"
798 * or, if there is only a single element, the node of that element.
800 static __isl_give isl_ast_graft *ast_graft_list_fuse(
801 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build,
802 __isl_keep isl_ast_build *sub_build, int up)
804 isl_ctx *ctx;
805 isl_ast_node *node;
806 isl_ast_graft *graft;
807 isl_ast_node_list *node_list;
808 isl_set *guard;
810 if (!list)
811 return NULL;
813 ctx = isl_ast_build_get_ctx(build);
814 guard = extract_hoistable_guard(list, build);
815 list = gist_guards(list, guard);
816 list = insert_pending_guard_nodes(list, sub_build);
818 node_list = extract_node_list(list);
819 node = isl_ast_node_from_ast_node_list(node_list);
821 graft = isl_ast_graft_alloc(node, build);
823 if (!up || isl_options_get_ast_build_exploit_nested_bounds(ctx)) {
824 isl_basic_set *enforced;
825 enforced = extract_shared_enforced(list, build);
826 graft = isl_ast_graft_enforce(graft, enforced);
829 graft = store_guard(graft, guard, build);
831 isl_ast_graft_list_free(list);
832 return graft;
835 /* Combine the grafts in the list into a single graft.
836 * Return a list containing this single graft.
837 * If the original list is empty, then return an empty list.
839 __isl_give isl_ast_graft_list *isl_ast_graft_list_fuse(
840 __isl_take isl_ast_graft_list *list,
841 __isl_keep isl_ast_build *build)
843 isl_ast_graft *graft;
845 if (!list)
846 return NULL;
847 if (isl_ast_graft_list_n_ast_graft(list) <= 1)
848 return list;
849 graft = ast_graft_list_fuse(list, build, build, 0);
850 return isl_ast_graft_list_from_ast_graft(graft);
853 /* Combine the two grafts into a single graft.
854 * Return a list containing this single graft.
856 static __isl_give isl_ast_graft *isl_ast_graft_fuse(
857 __isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2,
858 __isl_keep isl_ast_build *build)
860 isl_ctx *ctx;
861 isl_ast_graft_list *list;
863 ctx = isl_ast_build_get_ctx(build);
865 list = isl_ast_graft_list_alloc(ctx, 2);
866 list = isl_ast_graft_list_add(list, graft1);
867 list = isl_ast_graft_list_add(list, graft2);
869 return ast_graft_list_fuse(list, build, build, 0);
872 /* Allocate a graft for the current level based on the list of grafts
873 * of the inner level.
874 * "build" represents the context of the current level.
875 * "sub_build" represents the context of the inner level.
877 * The node is initialized to either a block containing the nodes of "children"
878 * or, if there is only a single child, the node of that child.
879 * If the current level requires a for node, it should be inserted by
880 * a subsequent call to isl_ast_graft_insert_for.
882 __isl_give isl_ast_graft *isl_ast_graft_alloc_level(
883 __isl_take isl_ast_graft_list *children,
884 __isl_keep isl_ast_build *build, __isl_keep isl_ast_build *sub_build)
886 return ast_graft_list_fuse(children, build, sub_build, 1);
889 /* Insert a for node enclosing the current graft->node.
891 __isl_give isl_ast_graft *isl_ast_graft_insert_for(
892 __isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node)
894 if (!graft)
895 goto error;
897 graft->node = isl_ast_node_for_set_body(node, graft->node);
898 if (!graft->node)
899 return isl_ast_graft_free(graft);
901 return graft;
902 error:
903 isl_ast_node_free(node);
904 isl_ast_graft_free(graft);
905 return NULL;
908 /* Represent the graft list as an AST node.
909 * This operation drops the information about guards in the grafts, so
910 * if there are any pending guards, then they are materialized as if nodes.
912 __isl_give isl_ast_node *isl_ast_node_from_graft_list(
913 __isl_take isl_ast_graft_list *list,
914 __isl_keep isl_ast_build *build)
916 isl_ast_node_list *node_list;
918 list = insert_pending_guard_nodes(list, build);
919 node_list = extract_node_list(list);
920 isl_ast_graft_list_free(list);
922 return isl_ast_node_from_ast_node_list(node_list);
925 void *isl_ast_graft_free(__isl_take isl_ast_graft *graft)
927 if (!graft)
928 return NULL;
930 if (--graft->ref > 0)
931 return NULL;
933 isl_ast_node_free(graft->node);
934 isl_set_free(graft->guard);
935 isl_basic_set_free(graft->enforced);
936 free(graft);
938 return NULL;
941 /* Record that the grafted tree enforces
942 * "enforced" by intersecting graft->enforced with "enforced".
944 __isl_give isl_ast_graft *isl_ast_graft_enforce(
945 __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
947 if (!graft || !enforced)
948 goto error;
950 enforced = isl_basic_set_align_params(enforced,
951 isl_basic_set_get_space(graft->enforced));
952 graft->enforced = isl_basic_set_align_params(graft->enforced,
953 isl_basic_set_get_space(enforced));
954 graft->enforced = isl_basic_set_intersect(graft->enforced, enforced);
955 if (!graft->enforced)
956 return isl_ast_graft_free(graft);
958 return graft;
959 error:
960 isl_basic_set_free(enforced);
961 return isl_ast_graft_free(graft);
964 __isl_give isl_basic_set *isl_ast_graft_get_enforced(
965 __isl_keep isl_ast_graft *graft)
967 return graft ? isl_basic_set_copy(graft->enforced) : NULL;
970 __isl_give isl_set *isl_ast_graft_get_guard(__isl_keep isl_ast_graft *graft)
972 return graft ? isl_set_copy(graft->guard) : NULL;
975 /* Record that "guard" needs to be inserted in "graft".
977 * We first simplify the guard in the context of the enforced set and
978 * then we store the guard in case we may be able
979 * to hoist it to higher levels and/or combine it with those of other grafts.
981 __isl_give isl_ast_graft *isl_ast_graft_add_guard(
982 __isl_take isl_ast_graft *graft,
983 __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
985 isl_basic_set *enforced;
987 if (!graft || !build)
988 goto error;
990 enforced = isl_basic_set_copy(graft->enforced);
991 guard = isl_set_gist(guard, isl_set_from_basic_set(enforced));
993 graft = store_guard(graft, guard, build);
995 return graft;
996 error:
997 isl_set_free(guard);
998 isl_ast_graft_free(graft);
999 return NULL;
1002 /* Reformulate the "graft", which was generated in the context
1003 * of an inner code generation, in terms of the outer code generation
1004 * AST build.
1006 * If "product" is set, then the domain of the inner code generation build is
1008 * [O -> S]
1010 * with O the domain of the outer code generation build.
1011 * We essentially need to project out S.
1013 * If "product" is not set, then we need to project the domains onto
1014 * their parameter spaces.
1016 __isl_give isl_ast_graft *isl_ast_graft_unembed(__isl_take isl_ast_graft *graft,
1017 int product)
1019 isl_basic_set *enforced;
1021 if (!graft)
1022 return NULL;
1024 if (product) {
1025 enforced = graft->enforced;
1026 enforced = isl_basic_map_domain(isl_basic_set_unwrap(enforced));
1027 graft->enforced = enforced;
1028 graft->guard = isl_map_domain(isl_set_unwrap(graft->guard));
1029 } else {
1030 graft->enforced = isl_basic_set_params(graft->enforced);
1031 graft->guard = isl_set_params(graft->guard);
1033 graft->guard = isl_set_compute_divs(graft->guard);
1035 if (!graft->enforced || !graft->guard)
1036 return isl_ast_graft_free(graft);
1038 return graft;
1041 /* Reformulate the grafts in "list", which were generated in the context
1042 * of an inner code generation, in terms of the outer code generation
1043 * AST build.
1045 __isl_give isl_ast_graft_list *isl_ast_graft_list_unembed(
1046 __isl_take isl_ast_graft_list *list, int product)
1048 int i, n;
1050 n = isl_ast_graft_list_n_ast_graft(list);
1051 for (i = 0; i < n; ++i) {
1052 isl_ast_graft *graft;
1054 graft = isl_ast_graft_list_get_ast_graft(list, i);
1055 graft = isl_ast_graft_unembed(graft, product);
1056 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
1059 return list;
1062 /* Compute the preimage of "graft" under the function represented by "ma".
1063 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1065 __isl_give isl_ast_graft *isl_ast_graft_preimage_multi_aff(
1066 __isl_take isl_ast_graft *graft, __isl_take isl_multi_aff *ma)
1068 isl_basic_set *enforced;
1070 if (!graft)
1071 return NULL;
1073 enforced = graft->enforced;
1074 graft->enforced = isl_basic_set_preimage_multi_aff(enforced,
1075 isl_multi_aff_copy(ma));
1076 graft->guard = isl_set_preimage_multi_aff(graft->guard, ma);
1078 if (!graft->enforced || !graft->guard)
1079 return isl_ast_graft_free(graft);
1081 return graft;
1084 /* Compute the preimage of all the grafts in "list" under
1085 * the function represented by "ma".
1087 __isl_give isl_ast_graft_list *isl_ast_graft_list_preimage_multi_aff(
1088 __isl_take isl_ast_graft_list *list, __isl_take isl_multi_aff *ma)
1090 int i, n;
1092 n = isl_ast_graft_list_n_ast_graft(list);
1093 for (i = 0; i < n; ++i) {
1094 isl_ast_graft *graft;
1096 graft = isl_ast_graft_list_get_ast_graft(list, i);
1097 graft = isl_ast_graft_preimage_multi_aff(graft,
1098 isl_multi_aff_copy(ma));
1099 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
1102 isl_multi_aff_free(ma);
1103 return list;
1106 /* Compare two grafts based on their guards.
1108 static int cmp_graft(__isl_keep isl_ast_graft *a, __isl_keep isl_ast_graft *b,
1109 void *user)
1111 return isl_set_plain_cmp(a->guard, b->guard);
1114 /* Order the elements in "list" based on their guards.
1116 __isl_give isl_ast_graft_list *isl_ast_graft_list_sort_guard(
1117 __isl_take isl_ast_graft_list *list)
1119 return isl_ast_graft_list_sort(list, &cmp_graft, NULL);
1122 /* Merge the given two lists into a single list of grafts,
1123 * merging grafts with the same guard into a single graft.
1125 * "list2" has been sorted using isl_ast_graft_list_sort.
1126 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1127 * and may therefore not be completely sorted.
1129 * The elements in "list2" need to be executed after those in "list1",
1130 * but if the guard of a graft in "list2" is disjoint from the guards
1131 * of some final elements in "list1", then it can be moved up to before
1132 * those final elements.
1134 * In particular, we look at each element g of "list2" in turn
1135 * and move it up beyond elements of "list1" that would be sorted
1136 * after g as long as each of these elements has a guard that is disjoint
1137 * from that of g.
1139 * We do not allow the second or any later element of "list2" to be moved
1140 * before a previous elements of "list2" even if the reason that
1141 * that element didn't move up further was that its guard was not disjoint
1142 * from that of the previous element in "list1".
1144 __isl_give isl_ast_graft_list *isl_ast_graft_list_merge(
1145 __isl_take isl_ast_graft_list *list1,
1146 __isl_take isl_ast_graft_list *list2,
1147 __isl_keep isl_ast_build *build)
1149 int i, j, first;
1151 if (!list1 || !list2 || !build)
1152 goto error;
1153 if (list2->n == 0) {
1154 isl_ast_graft_list_free(list2);
1155 return list1;
1157 if (list1->n == 0) {
1158 isl_ast_graft_list_free(list1);
1159 return list2;
1162 first = 0;
1163 for (i = 0; i < list2->n; ++i) {
1164 isl_ast_graft *graft;
1165 graft = isl_ast_graft_list_get_ast_graft(list2, i);
1166 if (!graft)
1167 break;
1169 for (j = list1->n; j >= 0; --j) {
1170 int cmp, disjoint;
1171 isl_ast_graft *graft_j;
1173 if (j == first)
1174 cmp = -1;
1175 else
1176 cmp = isl_set_plain_cmp(list1->p[j - 1]->guard,
1177 graft->guard);
1178 if (cmp > 0) {
1179 disjoint = isl_set_is_disjoint(graft->guard,
1180 list1->p[j - 1]->guard);
1181 if (disjoint < 0) {
1182 list1 = isl_ast_graft_list_free(list1);
1183 break;
1185 if (!disjoint)
1186 cmp = -1;
1188 if (cmp > 0)
1189 continue;
1190 if (cmp < 0) {
1191 list1 = isl_ast_graft_list_insert(list1, j,
1192 graft);
1193 break;
1196 --j;
1198 graft_j = isl_ast_graft_list_get_ast_graft(list1, j);
1199 graft_j = isl_ast_graft_fuse(graft_j, graft, build);
1200 list1 = isl_ast_graft_list_set_ast_graft(list1, j,
1201 graft_j);
1202 break;
1205 if (j < 0)
1206 isl_die(isl_ast_build_get_ctx(build),
1207 isl_error_internal,
1208 "element failed to get inserted", break);
1210 first = j + 1;
1211 if (!list1)
1212 break;
1214 if (i < list2->n)
1215 list1 = isl_ast_graft_list_free(list1);
1216 isl_ast_graft_list_free(list2);
1218 return list1;
1219 error:
1220 isl_ast_graft_list_free(list1);
1221 isl_ast_graft_list_free(list2);
1222 return NULL;
1225 __isl_give isl_printer *isl_printer_print_ast_graft(__isl_take isl_printer *p,
1226 __isl_keep isl_ast_graft *graft)
1228 if (!p)
1229 return NULL;
1230 if (!graft)
1231 return isl_printer_free(p);
1233 p = isl_printer_print_str(p, "(");
1234 p = isl_printer_print_str(p, "guard: ");
1235 p = isl_printer_print_set(p, graft->guard);
1236 p = isl_printer_print_str(p, ", ");
1237 p = isl_printer_print_str(p, "enforced: ");
1238 p = isl_printer_print_basic_set(p, graft->enforced);
1239 p = isl_printer_print_str(p, ", ");
1240 p = isl_printer_print_str(p, "node: ");
1241 p = isl_printer_print_ast_node(p, graft->node);
1242 p = isl_printer_print_str(p, ")");
1244 return p;