isl_local_space_divs_known: extract out isl_local_divs_known
[isl.git] / isl_ast_graft.c
blobde061c7871c7998659b2b322753cf2a557bb5815
1 /*
2 * Copyright 2012 Ecole Normale Superieure
3 * Copyright 2014 INRIA Rocquencourt
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
10 * B.P. 105 - 78153 Le Chesnay, France
13 #include <isl/space.h>
14 #include <isl_ast_private.h>
15 #include <isl_ast_build_expr.h>
16 #include <isl_ast_build_private.h>
17 #include <isl_ast_graft_private.h>
19 static __isl_give isl_ast_graft *isl_ast_graft_copy(
20 __isl_keep isl_ast_graft *graft);
22 #undef BASE
23 #define BASE ast_graft
25 #include <isl_list_templ.c>
27 #undef BASE
28 #define BASE ast_graft
29 #include <print_templ.c>
31 isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft)
33 if (!graft)
34 return NULL;
35 return isl_basic_set_get_ctx(graft->enforced);
38 __isl_give isl_ast_node *isl_ast_graft_get_node(
39 __isl_keep isl_ast_graft *graft)
41 return graft ? isl_ast_node_copy(graft->node) : NULL;
44 /* Create a graft for "node" with no guards and no enforced conditions.
46 __isl_give isl_ast_graft *isl_ast_graft_alloc(
47 __isl_take isl_ast_node *node, __isl_keep isl_ast_build *build)
49 isl_ctx *ctx;
50 isl_space *space;
51 isl_ast_graft *graft;
53 if (!node)
54 return NULL;
56 ctx = isl_ast_node_get_ctx(node);
57 graft = isl_calloc_type(ctx, isl_ast_graft);
58 if (!graft)
59 goto error;
61 space = isl_ast_build_get_space(build, 1);
63 graft->ref = 1;
64 graft->node = node;
65 graft->guard = isl_set_universe(isl_space_copy(space));
66 graft->enforced = isl_basic_set_universe(space);
68 if (!graft->guard || !graft->enforced)
69 return isl_ast_graft_free(graft);
71 return graft;
72 error:
73 isl_ast_node_free(node);
74 return NULL;
77 /* Create a graft with no guards and no enforced conditions
78 * encapsulating a call to the domain element specified by "executed".
79 * "executed" is assumed to be single-valued.
81 __isl_give isl_ast_graft *isl_ast_graft_alloc_domain(
82 __isl_take isl_map *executed, __isl_keep isl_ast_build *build)
84 isl_ast_node *node;
86 node = isl_ast_build_call_from_executed(build, executed);
88 return isl_ast_graft_alloc(node, build);
91 static __isl_give isl_ast_graft *isl_ast_graft_copy(
92 __isl_keep isl_ast_graft *graft)
94 if (!graft)
95 return NULL;
97 graft->ref++;
98 return graft;
101 /* Do all the grafts in "list" have the same guard and is this guard
102 * independent of the current depth?
104 static int equal_independent_guards(__isl_keep isl_ast_graft_list *list,
105 __isl_keep isl_ast_build *build)
107 int i, n;
108 int depth;
109 isl_ast_graft *graft_0;
110 int equal = 1;
111 int skip;
113 graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
114 if (!graft_0)
115 return -1;
117 depth = isl_ast_build_get_depth(build);
118 if (isl_set_dim(graft_0->guard, isl_dim_set) <= depth)
119 skip = 0;
120 else
121 skip = isl_set_involves_dims(graft_0->guard,
122 isl_dim_set, depth, 1);
123 if (skip < 0 || skip) {
124 isl_ast_graft_free(graft_0);
125 return skip < 0 ? -1 : 0;
128 n = isl_ast_graft_list_n_ast_graft(list);
129 for (i = 1; i < n; ++i) {
130 isl_ast_graft *graft;
131 graft = isl_ast_graft_list_get_ast_graft(list, i);
132 if (!graft)
133 equal = -1;
134 else
135 equal = isl_set_is_equal(graft_0->guard, graft->guard);
136 isl_ast_graft_free(graft);
137 if (equal < 0 || !equal)
138 break;
141 isl_ast_graft_free(graft_0);
143 return equal;
146 /* Hoist "guard" out of the current level (given by "build").
148 * In particular, eliminate the dimension corresponding to the current depth.
150 static __isl_give isl_set *hoist_guard(__isl_take isl_set *guard,
151 __isl_keep isl_ast_build *build)
153 int depth;
155 depth = isl_ast_build_get_depth(build);
156 if (depth < isl_set_dim(guard, isl_dim_set)) {
157 guard = isl_set_remove_divs_involving_dims(guard,
158 isl_dim_set, depth, 1);
159 guard = isl_set_eliminate(guard, isl_dim_set, depth, 1);
160 guard = isl_set_compute_divs(guard);
163 return guard;
166 /* Extract a common guard from the grafts in "list" that can be hoisted
167 * out of the current level. If no such guard can be found, then return
168 * a universal set.
170 * If all the grafts in the list have the same guard and if this guard
171 * is independent of the current level, then it can be hoisted out.
172 * If there is only one graft in the list and if its guard
173 * depends on the current level, then we eliminate this level and
174 * return the result.
176 * Otherwise, we return the unshifted simple hull of the guards.
177 * In order to be able to hoist as many constraints as possible,
178 * but at the same time avoid hoisting constraints that did not
179 * appear in the guards in the first place, we intersect the guards
180 * with all the information that is available (i.e., the domain
181 * from the build and the enforced constraints of the graft) and
182 * compute the unshifted hull of the result using only constraints
183 * from the original guards.
184 * In particular, intersecting the guards with other known information
185 * allows us to hoist guards that are only explicit is some of
186 * the grafts and implicit in the others.
188 * The special case for equal guards is needed in case those guards
189 * are non-convex. Taking the simple hull would remove information
190 * and would not allow for these guards to be hoisted completely.
192 __isl_give isl_set *isl_ast_graft_list_extract_hoistable_guard(
193 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
195 int i, n;
196 int equal;
197 isl_ctx *ctx;
198 isl_set *guard;
199 isl_set_list *set_list;
200 isl_basic_set *hull;
202 if (!list || !build)
203 return NULL;
205 n = isl_ast_graft_list_n_ast_graft(list);
206 if (n == 0)
207 return isl_set_universe(isl_ast_build_get_space(build, 1));
209 equal = equal_independent_guards(list, build);
210 if (equal < 0)
211 return NULL;
213 if (equal || n == 1) {
214 isl_ast_graft *graft_0;
216 graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
217 if (!graft_0)
218 return NULL;
219 guard = isl_set_copy(graft_0->guard);
220 if (!equal)
221 guard = hoist_guard(guard, build);
222 isl_ast_graft_free(graft_0);
223 return guard;
226 ctx = isl_ast_build_get_ctx(build);
227 set_list = isl_set_list_alloc(ctx, n);
228 guard = isl_set_empty(isl_ast_build_get_space(build, 1));
229 for (i = 0; i < n; ++i) {
230 isl_ast_graft *graft;
231 isl_basic_set *enforced;
232 isl_set *guard_i;
234 graft = isl_ast_graft_list_get_ast_graft(list, i);
235 enforced = isl_ast_graft_get_enforced(graft);
236 guard_i = isl_set_copy(graft->guard);
237 isl_ast_graft_free(graft);
238 set_list = isl_set_list_add(set_list, isl_set_copy(guard_i));
239 guard_i = isl_set_intersect(guard_i,
240 isl_set_from_basic_set(enforced));
241 guard_i = isl_set_intersect(guard_i,
242 isl_ast_build_get_domain(build));
243 guard = isl_set_union(guard, guard_i);
245 hull = isl_set_unshifted_simple_hull_from_set_list(guard, set_list);
246 guard = isl_set_from_basic_set(hull);
247 return hoist_guard(guard, build);
250 /* Internal data structure used inside insert_if.
252 * list is the list of guarded nodes created by each call to insert_if.
253 * node is the original node that is guarded by insert_if.
254 * build is the build in which the AST is constructed.
256 struct isl_insert_if_data {
257 isl_ast_node_list *list;
258 isl_ast_node *node;
259 isl_ast_build *build;
262 static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user);
264 /* Insert an if node around "node" testing the condition encoded
265 * in guard "guard".
267 * If the user does not want any disjunctions in the if conditions
268 * and if "guard" does involve a disjunction, then we make the different
269 * disjuncts disjoint and insert an if node corresponding to each disjunct
270 * around a copy of "node". The result is then a block node containing
271 * this sequence of guarded copies of "node".
273 static __isl_give isl_ast_node *ast_node_insert_if(
274 __isl_take isl_ast_node *node, __isl_take isl_set *guard,
275 __isl_keep isl_ast_build *build)
277 struct isl_insert_if_data data;
278 isl_ctx *ctx;
280 ctx = isl_ast_build_get_ctx(build);
281 if (isl_options_get_ast_build_allow_or(ctx) ||
282 isl_set_n_basic_set(guard) <= 1) {
283 isl_ast_node *if_node;
284 isl_ast_expr *expr;
286 expr = isl_ast_build_expr_from_set_internal(build, guard);
288 if_node = isl_ast_node_alloc_if(expr);
289 return isl_ast_node_if_set_then(if_node, node);
292 guard = isl_set_make_disjoint(guard);
294 data.list = isl_ast_node_list_alloc(ctx, 0);
295 data.node = node;
296 data.build = build;
297 if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0)
298 data.list = isl_ast_node_list_free(data.list);
300 isl_set_free(guard);
301 isl_ast_node_free(data.node);
302 return isl_ast_node_alloc_block(data.list);
305 /* Insert an if node around a copy of "data->node" testing the condition
306 * encoded in guard "bset" and add the result to data->list.
308 static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user)
310 struct isl_insert_if_data *data = user;
311 isl_ast_node *node;
312 isl_set *set;
314 set = isl_set_from_basic_set(bset);
315 node = isl_ast_node_copy(data->node);
316 node = ast_node_insert_if(node, set, data->build);
317 data->list = isl_ast_node_list_add(data->list, node);
319 return isl_stat_ok;
322 /* Insert an if node around graft->node testing the condition encoded
323 * in guard "guard", assuming guard involves any conditions.
325 static __isl_give isl_ast_graft *insert_if_node(
326 __isl_take isl_ast_graft *graft, __isl_take isl_set *guard,
327 __isl_keep isl_ast_build *build)
329 int univ;
331 if (!graft)
332 goto error;
334 univ = isl_set_plain_is_universe(guard);
335 if (univ < 0)
336 goto error;
337 if (univ) {
338 isl_set_free(guard);
339 return graft;
342 build = isl_ast_build_copy(build);
343 graft->node = ast_node_insert_if(graft->node, guard, build);
344 isl_ast_build_free(build);
346 if (!graft->node)
347 return isl_ast_graft_free(graft);
349 return graft;
350 error:
351 isl_set_free(guard);
352 return isl_ast_graft_free(graft);
355 /* Insert an if node around graft->node testing the condition encoded
356 * in graft->guard, assuming graft->guard involves any conditions.
358 static __isl_give isl_ast_graft *insert_pending_guard_node(
359 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
361 if (!graft)
362 return NULL;
364 return insert_if_node(graft, isl_set_copy(graft->guard), build);
367 /* Replace graft->enforced by "enforced".
369 __isl_give isl_ast_graft *isl_ast_graft_set_enforced(
370 __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
372 if (!graft || !enforced)
373 goto error;
375 isl_basic_set_free(graft->enforced);
376 graft->enforced = enforced;
378 return graft;
379 error:
380 isl_basic_set_free(enforced);
381 return isl_ast_graft_free(graft);
384 /* Update "enforced" such that it only involves constraints that are
385 * also enforced by "graft".
387 static __isl_give isl_basic_set *update_enforced(
388 __isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft,
389 int depth)
391 isl_basic_set *enforced_g;
393 enforced_g = isl_ast_graft_get_enforced(graft);
394 if (depth < isl_basic_set_dim(enforced_g, isl_dim_set))
395 enforced_g = isl_basic_set_eliminate(enforced_g,
396 isl_dim_set, depth, 1);
397 enforced_g = isl_basic_set_remove_unknown_divs(enforced_g);
398 enforced_g = isl_basic_set_align_params(enforced_g,
399 isl_basic_set_get_space(enforced));
400 enforced = isl_basic_set_align_params(enforced,
401 isl_basic_set_get_space(enforced_g));
402 enforced = isl_set_simple_hull(isl_basic_set_union(enforced,
403 enforced_g));
405 return enforced;
408 /* Extend the node at *body with node.
410 * If body points to the else branch, then *body may still be NULL.
411 * If so, we simply attach node to this else branch.
412 * Otherwise, we attach a list containing the statements already
413 * attached at *body followed by node.
415 static void extend_body(__isl_keep isl_ast_node **body,
416 __isl_take isl_ast_node *node)
418 isl_ast_node_list *list;
420 if (!*body) {
421 *body = node;
422 return;
425 if ((*body)->type == isl_ast_node_block) {
426 list = isl_ast_node_block_get_children(*body);
427 isl_ast_node_free(*body);
428 } else
429 list = isl_ast_node_list_from_ast_node(*body);
430 list = isl_ast_node_list_add(list, node);
431 *body = isl_ast_node_alloc_block(list);
434 /* Merge "graft" into the last graft of "list".
435 * body points to the then or else branch of an if node in that last graft.
437 * We attach graft->node to this branch and update the enforced
438 * set of the last graft of "list" to take into account the enforced
439 * set of "graft".
441 static __isl_give isl_ast_graft_list *graft_extend_body(
442 __isl_take isl_ast_graft_list *list,
443 __isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft,
444 __isl_keep isl_ast_build *build)
446 int n;
447 int depth;
448 isl_ast_graft *last;
449 isl_space *space;
450 isl_basic_set *enforced;
452 if (!list || !graft)
453 goto error;
454 extend_body(body, isl_ast_node_copy(graft->node));
455 if (!*body)
456 goto error;
458 n = isl_ast_graft_list_n_ast_graft(list);
459 last = isl_ast_graft_list_get_ast_graft(list, n - 1);
461 depth = isl_ast_build_get_depth(build);
462 space = isl_ast_build_get_space(build, 1);
463 enforced = isl_basic_set_empty(space);
464 enforced = update_enforced(enforced, last, depth);
465 enforced = update_enforced(enforced, graft, depth);
466 last = isl_ast_graft_set_enforced(last, enforced);
468 list = isl_ast_graft_list_set_ast_graft(list, n - 1, last);
469 isl_ast_graft_free(graft);
470 return list;
471 error:
472 isl_ast_graft_free(graft);
473 return isl_ast_graft_list_free(list);
476 /* Merge "graft" into the last graft of "list", attaching graft->node
477 * to the then branch of "last_if".
479 static __isl_give isl_ast_graft_list *extend_then(
480 __isl_take isl_ast_graft_list *list,
481 __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
482 __isl_keep isl_ast_build *build)
484 return graft_extend_body(list, &last_if->u.i.then, graft, build);
487 /* Merge "graft" into the last graft of "list", attaching graft->node
488 * to the else branch of "last_if".
490 static __isl_give isl_ast_graft_list *extend_else(
491 __isl_take isl_ast_graft_list *list,
492 __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
493 __isl_keep isl_ast_build *build)
495 return graft_extend_body(list, &last_if->u.i.else_node, graft, build);
498 /* This data structure keeps track of an if node.
500 * "node" is the actual if-node
501 * "guard" is the original, non-simplified guard of the node
502 * "complement" is the complement of "guard" in the context of outer if nodes
504 struct isl_if_node {
505 isl_ast_node *node;
506 isl_set *guard;
507 isl_set *complement;
510 /* Given a list of "n" if nodes, clear those starting at "first"
511 * and return "first" (i.e., the updated size of the array).
513 static int clear_if_nodes(struct isl_if_node *if_node, int first, int n)
515 int i;
517 for (i = first; i < n; ++i) {
518 isl_set_free(if_node[i].guard);
519 isl_set_free(if_node[i].complement);
522 return first;
525 /* For each graft in "list",
526 * insert an if node around graft->node testing the condition encoded
527 * in graft->guard, assuming graft->guard involves any conditions.
529 * We keep track of a list of generated if nodes that can be extended
530 * without changing the order of the elements in "list".
531 * If the guard of a graft is a subset of either the guard or its complement
532 * of one of those if nodes, then the node
533 * of the new graft is inserted into the then or else branch of the last graft
534 * and the current graft is discarded.
535 * The guard of the node is then simplified based on the conditions
536 * enforced at that then or else branch.
537 * Otherwise, the current graft is appended to the list.
539 * We only construct else branches if allowed by the user.
541 static __isl_give isl_ast_graft_list *insert_pending_guard_nodes(
542 __isl_take isl_ast_graft_list *list,
543 __isl_keep isl_ast_build *build)
545 int i, j, n, n_if;
546 int allow_else;
547 isl_ctx *ctx;
548 isl_ast_graft_list *res;
549 struct isl_if_node *if_node = NULL;
551 if (!build || !list)
552 return isl_ast_graft_list_free(list);
554 ctx = isl_ast_build_get_ctx(build);
555 n = isl_ast_graft_list_n_ast_graft(list);
557 allow_else = isl_options_get_ast_build_allow_else(ctx);
559 n_if = 0;
560 if (n > 1) {
561 if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1);
562 if (!if_node)
563 return isl_ast_graft_list_free(list);
566 res = isl_ast_graft_list_alloc(ctx, n);
568 for (i = 0; i < n; ++i) {
569 isl_set *guard;
570 isl_ast_graft *graft;
571 int subset, found_then, found_else;
572 isl_ast_node *node;
574 graft = isl_ast_graft_list_get_ast_graft(list, i);
575 if (!graft)
576 break;
577 subset = 0;
578 found_then = found_else = -1;
579 if (n_if > 0) {
580 isl_set *test;
581 test = isl_set_copy(graft->guard);
582 test = isl_set_intersect(test,
583 isl_set_copy(build->domain));
584 for (j = n_if - 1; j >= 0; --j) {
585 subset = isl_set_is_subset(test,
586 if_node[j].guard);
587 if (subset < 0 || subset) {
588 found_then = j;
589 break;
591 if (!allow_else)
592 continue;
593 subset = isl_set_is_subset(test,
594 if_node[j].complement);
595 if (subset < 0 || subset) {
596 found_else = j;
597 break;
600 n_if = clear_if_nodes(if_node, j + 1, n_if);
601 isl_set_free(test);
603 if (subset < 0) {
604 graft = isl_ast_graft_free(graft);
605 break;
608 guard = isl_set_copy(graft->guard);
609 if (found_then >= 0)
610 graft->guard = isl_set_gist(graft->guard,
611 isl_set_copy(if_node[found_then].guard));
612 else if (found_else >= 0)
613 graft->guard = isl_set_gist(graft->guard,
614 isl_set_copy(if_node[found_else].complement));
616 node = graft->node;
617 if (!graft->guard)
618 graft = isl_ast_graft_free(graft);
619 graft = insert_pending_guard_node(graft, build);
620 if (graft && graft->node != node && i != n - 1) {
621 isl_set *set;
622 if_node[n_if].node = graft->node;
623 if_node[n_if].guard = guard;
624 if (found_then >= 0)
625 set = if_node[found_then].guard;
626 else if (found_else >= 0)
627 set = if_node[found_else].complement;
628 else
629 set = build->domain;
630 set = isl_set_copy(set);
631 set = isl_set_subtract(set, isl_set_copy(guard));
632 if_node[n_if].complement = set;
633 n_if++;
634 } else
635 isl_set_free(guard);
636 if (!graft)
637 break;
639 if (found_then >= 0)
640 res = extend_then(res, if_node[found_then].node,
641 graft, build);
642 else if (found_else >= 0)
643 res = extend_else(res, if_node[found_else].node,
644 graft, build);
645 else
646 res = isl_ast_graft_list_add(res, graft);
648 if (i < n)
649 res = isl_ast_graft_list_free(res);
651 isl_ast_graft_list_free(list);
652 clear_if_nodes(if_node, 0, n_if);
653 free(if_node);
654 return res;
657 /* For each graft in "list",
658 * insert an if node around graft->node testing the condition encoded
659 * in graft->guard, assuming graft->guard involves any conditions.
660 * Subsequently remove the guards from the grafts.
662 __isl_give isl_ast_graft_list *isl_ast_graft_list_insert_pending_guard_nodes(
663 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
665 int i, n;
666 isl_set *universe;
668 list = insert_pending_guard_nodes(list, build);
669 if (!list)
670 return NULL;
672 universe = isl_set_universe(isl_ast_build_get_space(build, 1));
673 n = isl_ast_graft_list_n_ast_graft(list);
674 for (i = 0; i < n; ++i) {
675 isl_ast_graft *graft;
677 graft = isl_ast_graft_list_get_ast_graft(list, i);
678 if (!graft)
679 break;
680 isl_set_free(graft->guard);
681 graft->guard = isl_set_copy(universe);
682 if (!graft->guard)
683 graft = isl_ast_graft_free(graft);
684 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
686 isl_set_free(universe);
687 if (i < n)
688 return isl_ast_graft_list_free(list);
690 return list;
693 /* Collect the nodes contained in the grafts in "list" in a node list.
695 static __isl_give isl_ast_node_list *extract_node_list(
696 __isl_keep isl_ast_graft_list *list)
698 int i, n;
699 isl_ctx *ctx;
700 isl_ast_node_list *node_list;
702 if (!list)
703 return NULL;
704 ctx = isl_ast_graft_list_get_ctx(list);
705 n = isl_ast_graft_list_n_ast_graft(list);
706 node_list = isl_ast_node_list_alloc(ctx, n);
707 for (i = 0; i < n; ++i) {
708 isl_ast_node *node;
709 isl_ast_graft *graft;
711 graft = isl_ast_graft_list_get_ast_graft(list, i);
712 node = isl_ast_graft_get_node(graft);
713 node_list = isl_ast_node_list_add(node_list, node);
714 isl_ast_graft_free(graft);
717 return node_list;
720 /* Look for shared enforced constraints by all the elements in "list"
721 * on outer loops (with respect to the current depth) and return the result.
723 * If there are no elements in "list", then return the empty set.
725 __isl_give isl_basic_set *isl_ast_graft_list_extract_shared_enforced(
726 __isl_keep isl_ast_graft_list *list,
727 __isl_keep isl_ast_build *build)
729 int i, n;
730 int depth;
731 isl_space *space;
732 isl_basic_set *enforced;
734 if (!list)
735 return NULL;
737 space = isl_ast_build_get_space(build, 1);
738 enforced = isl_basic_set_empty(space);
740 depth = isl_ast_build_get_depth(build);
741 n = isl_ast_graft_list_n_ast_graft(list);
742 for (i = 0; i < n; ++i) {
743 isl_ast_graft *graft;
745 graft = isl_ast_graft_list_get_ast_graft(list, i);
746 enforced = update_enforced(enforced, graft, depth);
747 isl_ast_graft_free(graft);
750 return enforced;
753 /* Record "guard" in "graft" so that it will be enforced somewhere
754 * up the tree. If the graft already has a guard, then it may be partially
755 * redundant in combination with the new guard and in the context
756 * the generated constraints of "build". In fact, the new guard
757 * may in itself have some redundant constraints.
758 * We therefore (re)compute the gist of the intersection
759 * and coalesce the result.
761 static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft,
762 __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
764 int is_universe;
766 if (!graft)
767 goto error;
769 is_universe = isl_set_plain_is_universe(guard);
770 if (is_universe < 0)
771 goto error;
772 if (is_universe) {
773 isl_set_free(guard);
774 return graft;
777 graft->guard = isl_set_intersect(graft->guard, guard);
778 graft->guard = isl_set_gist(graft->guard,
779 isl_ast_build_get_generated(build));
780 graft->guard = isl_set_coalesce(graft->guard);
781 if (!graft->guard)
782 return isl_ast_graft_free(graft);
784 return graft;
785 error:
786 isl_set_free(guard);
787 return isl_ast_graft_free(graft);
790 /* For each graft in "list", replace its guard with the gist with
791 * respect to "context".
793 static __isl_give isl_ast_graft_list *gist_guards(
794 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *context)
796 int i, n;
798 if (!list)
799 return NULL;
801 n = isl_ast_graft_list_n_ast_graft(list);
802 for (i = 0; i < n; ++i) {
803 isl_ast_graft *graft;
805 graft = isl_ast_graft_list_get_ast_graft(list, i);
806 if (!graft)
807 break;
808 graft->guard = isl_set_gist(graft->guard,
809 isl_set_copy(context));
810 if (!graft->guard)
811 graft = isl_ast_graft_free(graft);
812 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
814 if (i < n)
815 return isl_ast_graft_list_free(list);
817 return list;
820 /* For each graft in "list", replace its guard with the gist with
821 * respect to "context".
823 __isl_give isl_ast_graft_list *isl_ast_graft_list_gist_guards(
824 __isl_take isl_ast_graft_list *list, __isl_take isl_set *context)
826 list = gist_guards(list, context);
827 isl_set_free(context);
829 return list;
832 /* Allocate a graft in "build" based on the list of grafts in "sub_build".
833 * "guard" and "enforced" are the guard and enforced constraints
834 * of the allocated graft. The guard is used to simplify the guards
835 * of the elements in "list".
837 * The node is initialized to either a block containing the nodes of "children"
838 * or, if there is only a single child, the node of that child.
839 * If the current level requires a for node, it should be inserted by
840 * a subsequent call to isl_ast_graft_insert_for.
842 __isl_give isl_ast_graft *isl_ast_graft_alloc_from_children(
843 __isl_take isl_ast_graft_list *list, __isl_take isl_set *guard,
844 __isl_take isl_basic_set *enforced, __isl_keep isl_ast_build *build,
845 __isl_keep isl_ast_build *sub_build)
847 isl_ast_build *guard_build;
848 isl_ast_node *node;
849 isl_ast_node_list *node_list;
850 isl_ast_graft *graft;
852 guard_build = isl_ast_build_copy(sub_build);
853 guard_build = isl_ast_build_replace_pending_by_guard(guard_build,
854 isl_set_copy(guard));
855 list = gist_guards(list, guard);
856 list = insert_pending_guard_nodes(list, guard_build);
857 isl_ast_build_free(guard_build);
859 node_list = extract_node_list(list);
860 node = isl_ast_node_from_ast_node_list(node_list);
861 isl_ast_graft_list_free(list);
863 graft = isl_ast_graft_alloc(node, build);
864 graft = store_guard(graft, guard, build);
865 graft = isl_ast_graft_enforce(graft, enforced);
867 return graft;
870 /* Combine the grafts in the list into a single graft.
872 * The guard is initialized to the shared guard of the list elements (if any),
873 * provided it does not depend on the current dimension.
874 * The guards in the elements are then simplified with respect to the
875 * hoisted guard and materialized as if nodes around the contained AST nodes
876 * in the context of "sub_build".
878 * The enforced set is initialized to the simple hull of the enforced sets
879 * of the elements, provided the ast_build_exploit_nested_bounds option is set
880 * or the new graft will be used at the same level.
882 * The node is initialized to either a block containing the nodes of "list"
883 * or, if there is only a single element, the node of that element.
885 static __isl_give isl_ast_graft *ast_graft_list_fuse(
886 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
888 isl_ast_graft *graft;
889 isl_basic_set *enforced;
890 isl_set *guard;
892 if (!list)
893 return NULL;
895 enforced = isl_ast_graft_list_extract_shared_enforced(list, build);
896 guard = isl_ast_graft_list_extract_hoistable_guard(list, build);
897 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
898 build, build);
900 return graft;
903 /* Combine the grafts in the list into a single graft.
904 * Return a list containing this single graft.
905 * If the original list is empty, then return an empty list.
907 __isl_give isl_ast_graft_list *isl_ast_graft_list_fuse(
908 __isl_take isl_ast_graft_list *list,
909 __isl_keep isl_ast_build *build)
911 isl_ast_graft *graft;
913 if (!list)
914 return NULL;
915 if (isl_ast_graft_list_n_ast_graft(list) <= 1)
916 return list;
917 graft = ast_graft_list_fuse(list, build);
918 return isl_ast_graft_list_from_ast_graft(graft);
921 /* Combine the two grafts into a single graft.
922 * Return a list containing this single graft.
924 static __isl_give isl_ast_graft *isl_ast_graft_fuse(
925 __isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2,
926 __isl_keep isl_ast_build *build)
928 isl_ctx *ctx;
929 isl_ast_graft_list *list;
931 ctx = isl_ast_build_get_ctx(build);
933 list = isl_ast_graft_list_alloc(ctx, 2);
934 list = isl_ast_graft_list_add(list, graft1);
935 list = isl_ast_graft_list_add(list, graft2);
937 return ast_graft_list_fuse(list, build);
940 /* Insert a for node enclosing the current graft->node.
942 __isl_give isl_ast_graft *isl_ast_graft_insert_for(
943 __isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node)
945 if (!graft)
946 goto error;
948 graft->node = isl_ast_node_for_set_body(node, graft->node);
949 if (!graft->node)
950 return isl_ast_graft_free(graft);
952 return graft;
953 error:
954 isl_ast_node_free(node);
955 isl_ast_graft_free(graft);
956 return NULL;
959 /* Insert a mark governing the current graft->node.
961 __isl_give isl_ast_graft *isl_ast_graft_insert_mark(
962 __isl_take isl_ast_graft *graft, __isl_take isl_id *mark)
964 if (!graft)
965 goto error;
967 graft->node = isl_ast_node_alloc_mark(mark, graft->node);
968 if (!graft->node)
969 return isl_ast_graft_free(graft);
971 return graft;
972 error:
973 isl_id_free(mark);
974 isl_ast_graft_free(graft);
975 return NULL;
978 /* Represent the graft list as an AST node.
979 * This operation drops the information about guards in the grafts, so
980 * if there are any pending guards, then they are materialized as if nodes.
982 __isl_give isl_ast_node *isl_ast_node_from_graft_list(
983 __isl_take isl_ast_graft_list *list,
984 __isl_keep isl_ast_build *build)
986 isl_ast_node_list *node_list;
988 list = insert_pending_guard_nodes(list, build);
989 node_list = extract_node_list(list);
990 isl_ast_graft_list_free(list);
992 return isl_ast_node_from_ast_node_list(node_list);
995 void *isl_ast_graft_free(__isl_take isl_ast_graft *graft)
997 if (!graft)
998 return NULL;
1000 if (--graft->ref > 0)
1001 return NULL;
1003 isl_ast_node_free(graft->node);
1004 isl_set_free(graft->guard);
1005 isl_basic_set_free(graft->enforced);
1006 free(graft);
1008 return NULL;
1011 /* Record that the grafted tree enforces
1012 * "enforced" by intersecting graft->enforced with "enforced".
1014 __isl_give isl_ast_graft *isl_ast_graft_enforce(
1015 __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
1017 if (!graft || !enforced)
1018 goto error;
1020 enforced = isl_basic_set_align_params(enforced,
1021 isl_basic_set_get_space(graft->enforced));
1022 graft->enforced = isl_basic_set_align_params(graft->enforced,
1023 isl_basic_set_get_space(enforced));
1024 graft->enforced = isl_basic_set_intersect(graft->enforced, enforced);
1025 if (!graft->enforced)
1026 return isl_ast_graft_free(graft);
1028 return graft;
1029 error:
1030 isl_basic_set_free(enforced);
1031 return isl_ast_graft_free(graft);
1034 __isl_give isl_basic_set *isl_ast_graft_get_enforced(
1035 __isl_keep isl_ast_graft *graft)
1037 return graft ? isl_basic_set_copy(graft->enforced) : NULL;
1040 __isl_give isl_set *isl_ast_graft_get_guard(__isl_keep isl_ast_graft *graft)
1042 return graft ? isl_set_copy(graft->guard) : NULL;
1045 /* Record that "guard" needs to be inserted in "graft".
1047 __isl_give isl_ast_graft *isl_ast_graft_add_guard(
1048 __isl_take isl_ast_graft *graft,
1049 __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
1051 return store_guard(graft, guard, build);
1054 /* Reformulate the "graft", which was generated in the context
1055 * of an inner code generation, in terms of the outer code generation
1056 * AST build.
1058 * If "product" is set, then the domain of the inner code generation build is
1060 * [O -> S]
1062 * with O the domain of the outer code generation build.
1063 * We essentially need to project out S.
1065 * If "product" is not set, then we need to project the domains onto
1066 * their parameter spaces.
1068 __isl_give isl_ast_graft *isl_ast_graft_unembed(__isl_take isl_ast_graft *graft,
1069 int product)
1071 isl_basic_set *enforced;
1073 if (!graft)
1074 return NULL;
1076 if (product) {
1077 enforced = graft->enforced;
1078 enforced = isl_basic_map_domain(isl_basic_set_unwrap(enforced));
1079 graft->enforced = enforced;
1080 graft->guard = isl_map_domain(isl_set_unwrap(graft->guard));
1081 } else {
1082 graft->enforced = isl_basic_set_params(graft->enforced);
1083 graft->guard = isl_set_params(graft->guard);
1085 graft->guard = isl_set_compute_divs(graft->guard);
1087 if (!graft->enforced || !graft->guard)
1088 return isl_ast_graft_free(graft);
1090 return graft;
1093 /* Reformulate the grafts in "list", which were generated in the context
1094 * of an inner code generation, in terms of the outer code generation
1095 * AST build.
1097 __isl_give isl_ast_graft_list *isl_ast_graft_list_unembed(
1098 __isl_take isl_ast_graft_list *list, int product)
1100 int i, n;
1102 n = isl_ast_graft_list_n_ast_graft(list);
1103 for (i = 0; i < n; ++i) {
1104 isl_ast_graft *graft;
1106 graft = isl_ast_graft_list_get_ast_graft(list, i);
1107 graft = isl_ast_graft_unembed(graft, product);
1108 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
1111 return list;
1114 /* Compute the preimage of "graft" under the function represented by "ma".
1115 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1117 __isl_give isl_ast_graft *isl_ast_graft_preimage_multi_aff(
1118 __isl_take isl_ast_graft *graft, __isl_take isl_multi_aff *ma)
1120 isl_basic_set *enforced;
1122 if (!graft)
1123 return NULL;
1125 enforced = graft->enforced;
1126 graft->enforced = isl_basic_set_preimage_multi_aff(enforced,
1127 isl_multi_aff_copy(ma));
1128 graft->guard = isl_set_preimage_multi_aff(graft->guard, ma);
1130 if (!graft->enforced || !graft->guard)
1131 return isl_ast_graft_free(graft);
1133 return graft;
1136 /* Compute the preimage of all the grafts in "list" under
1137 * the function represented by "ma".
1139 __isl_give isl_ast_graft_list *isl_ast_graft_list_preimage_multi_aff(
1140 __isl_take isl_ast_graft_list *list, __isl_take isl_multi_aff *ma)
1142 int i, n;
1144 n = isl_ast_graft_list_n_ast_graft(list);
1145 for (i = 0; i < n; ++i) {
1146 isl_ast_graft *graft;
1148 graft = isl_ast_graft_list_get_ast_graft(list, i);
1149 graft = isl_ast_graft_preimage_multi_aff(graft,
1150 isl_multi_aff_copy(ma));
1151 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
1154 isl_multi_aff_free(ma);
1155 return list;
1158 /* Compare two grafts based on their guards.
1160 static int cmp_graft(__isl_keep isl_ast_graft *a, __isl_keep isl_ast_graft *b,
1161 void *user)
1163 return isl_set_plain_cmp(a->guard, b->guard);
1166 /* Order the elements in "list" based on their guards.
1168 __isl_give isl_ast_graft_list *isl_ast_graft_list_sort_guard(
1169 __isl_take isl_ast_graft_list *list)
1171 return isl_ast_graft_list_sort(list, &cmp_graft, NULL);
1174 /* Merge the given two lists into a single list of grafts,
1175 * merging grafts with the same guard into a single graft.
1177 * "list2" has been sorted using isl_ast_graft_list_sort.
1178 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1179 * and may therefore not be completely sorted.
1181 * The elements in "list2" need to be executed after those in "list1",
1182 * but if the guard of a graft in "list2" is disjoint from the guards
1183 * of some final elements in "list1", then it can be moved up to before
1184 * those final elements.
1186 * In particular, we look at each element g of "list2" in turn
1187 * and move it up beyond elements of "list1" that would be sorted
1188 * after g as long as each of these elements has a guard that is disjoint
1189 * from that of g.
1191 * We do not allow the second or any later element of "list2" to be moved
1192 * before a previous elements of "list2" even if the reason that
1193 * that element didn't move up further was that its guard was not disjoint
1194 * from that of the previous element in "list1".
1196 __isl_give isl_ast_graft_list *isl_ast_graft_list_merge(
1197 __isl_take isl_ast_graft_list *list1,
1198 __isl_take isl_ast_graft_list *list2,
1199 __isl_keep isl_ast_build *build)
1201 int i, j, first;
1203 if (!list1 || !list2 || !build)
1204 goto error;
1205 if (list2->n == 0) {
1206 isl_ast_graft_list_free(list2);
1207 return list1;
1209 if (list1->n == 0) {
1210 isl_ast_graft_list_free(list1);
1211 return list2;
1214 first = 0;
1215 for (i = 0; i < list2->n; ++i) {
1216 isl_ast_graft *graft;
1217 graft = isl_ast_graft_list_get_ast_graft(list2, i);
1218 if (!graft)
1219 break;
1221 for (j = list1->n; j >= 0; --j) {
1222 int cmp, disjoint;
1223 isl_ast_graft *graft_j;
1225 if (j == first)
1226 cmp = -1;
1227 else
1228 cmp = isl_set_plain_cmp(list1->p[j - 1]->guard,
1229 graft->guard);
1230 if (cmp > 0) {
1231 disjoint = isl_set_is_disjoint(graft->guard,
1232 list1->p[j - 1]->guard);
1233 if (disjoint < 0) {
1234 isl_ast_graft_free(graft);
1235 list1 = isl_ast_graft_list_free(list1);
1236 break;
1238 if (!disjoint)
1239 cmp = -1;
1241 if (cmp > 0)
1242 continue;
1243 if (cmp < 0) {
1244 list1 = isl_ast_graft_list_insert(list1, j,
1245 graft);
1246 break;
1249 --j;
1251 graft_j = isl_ast_graft_list_get_ast_graft(list1, j);
1252 graft_j = isl_ast_graft_fuse(graft_j, graft, build);
1253 list1 = isl_ast_graft_list_set_ast_graft(list1, j,
1254 graft_j);
1255 break;
1258 if (j < 0) {
1259 isl_ast_graft_free(graft);
1260 isl_die(isl_ast_build_get_ctx(build),
1261 isl_error_internal,
1262 "element failed to get inserted", break);
1265 first = j + 1;
1266 if (!list1)
1267 break;
1269 if (i < list2->n)
1270 list1 = isl_ast_graft_list_free(list1);
1271 isl_ast_graft_list_free(list2);
1273 return list1;
1274 error:
1275 isl_ast_graft_list_free(list1);
1276 isl_ast_graft_list_free(list2);
1277 return NULL;
1280 __isl_give isl_printer *isl_printer_print_ast_graft(__isl_take isl_printer *p,
1281 __isl_keep isl_ast_graft *graft)
1283 if (!p)
1284 return NULL;
1285 if (!graft)
1286 return isl_printer_free(p);
1288 p = isl_printer_print_str(p, "(");
1289 p = isl_printer_print_str(p, "guard: ");
1290 p = isl_printer_print_set(p, graft->guard);
1291 p = isl_printer_print_str(p, ", ");
1292 p = isl_printer_print_str(p, "enforced: ");
1293 p = isl_printer_print_basic_set(p, graft->enforced);
1294 p = isl_printer_print_str(p, ", ");
1295 p = isl_printer_print_str(p, "node: ");
1296 p = isl_printer_print_ast_node(p, graft->node);
1297 p = isl_printer_print_str(p, ")");
1299 return p;