isl_*_dim: return isl_size
[isl.git] / isl_ast_graft.c
blob874e10349c68e7e8bd38e6f2b3141914e7f00dcf
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/id.h>
14 #include <isl/space.h>
15 #include <isl_ast_private.h>
16 #include <isl_ast_build_expr.h>
17 #include <isl_ast_build_private.h>
18 #include <isl_ast_graft_private.h>
20 static __isl_give isl_ast_graft *isl_ast_graft_copy(
21 __isl_keep isl_ast_graft *graft);
23 #undef BASE
24 #define BASE ast_graft
26 #include <isl_list_templ.c>
28 #undef BASE
29 #define BASE ast_graft
30 #include <print_templ.c>
32 isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft)
34 if (!graft)
35 return NULL;
36 return isl_basic_set_get_ctx(graft->enforced);
39 __isl_give isl_ast_node *isl_ast_graft_get_node(
40 __isl_keep isl_ast_graft *graft)
42 return graft ? isl_ast_node_copy(graft->node) : NULL;
45 /* Create a graft for "node" with no guards and no enforced conditions.
47 __isl_give isl_ast_graft *isl_ast_graft_alloc(
48 __isl_take isl_ast_node *node, __isl_keep isl_ast_build *build)
50 isl_ctx *ctx;
51 isl_space *space;
52 isl_ast_graft *graft;
54 if (!node)
55 return NULL;
57 ctx = isl_ast_node_get_ctx(node);
58 graft = isl_calloc_type(ctx, isl_ast_graft);
59 if (!graft)
60 goto error;
62 space = isl_ast_build_get_space(build, 1);
64 graft->ref = 1;
65 graft->node = node;
66 graft->guard = isl_set_universe(isl_space_copy(space));
67 graft->enforced = isl_basic_set_universe(space);
69 if (!graft->guard || !graft->enforced)
70 return isl_ast_graft_free(graft);
72 return graft;
73 error:
74 isl_ast_node_free(node);
75 return NULL;
78 /* Create a graft with no guards and no enforced conditions
79 * encapsulating a call to the domain element specified by "executed".
80 * "executed" is assumed to be single-valued.
82 __isl_give isl_ast_graft *isl_ast_graft_alloc_domain(
83 __isl_take isl_map *executed, __isl_keep isl_ast_build *build)
85 isl_ast_node *node;
87 node = isl_ast_build_call_from_executed(build, executed);
89 return isl_ast_graft_alloc(node, build);
92 static __isl_give isl_ast_graft *isl_ast_graft_copy(
93 __isl_keep isl_ast_graft *graft)
95 if (!graft)
96 return NULL;
98 graft->ref++;
99 return graft;
102 /* Do all the grafts in "list" have the same guard and is this guard
103 * independent of the current depth?
105 static isl_bool equal_independent_guards(__isl_keep isl_ast_graft_list *list,
106 __isl_keep isl_ast_build *build)
108 int i, n;
109 int depth;
110 isl_size dim;
111 isl_ast_graft *graft_0;
112 isl_bool equal = isl_bool_true;
113 isl_bool skip;
115 graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
116 if (!graft_0)
117 return isl_bool_error;
119 depth = isl_ast_build_get_depth(build);
120 dim = isl_set_dim(graft_0->guard, isl_dim_set);
121 if (dim < 0)
122 return isl_bool_error;
123 if (dim <= depth)
124 skip = isl_bool_false;
125 else
126 skip = isl_set_involves_dims(graft_0->guard,
127 isl_dim_set, depth, 1);
128 if (skip < 0 || skip) {
129 isl_ast_graft_free(graft_0);
130 return isl_bool_not(skip);
133 n = isl_ast_graft_list_n_ast_graft(list);
134 for (i = 1; i < n; ++i) {
135 isl_ast_graft *graft;
136 graft = isl_ast_graft_list_get_ast_graft(list, i);
137 if (!graft)
138 equal = isl_bool_error;
139 else
140 equal = isl_set_is_equal(graft_0->guard, graft->guard);
141 isl_ast_graft_free(graft);
142 if (equal < 0 || !equal)
143 break;
146 isl_ast_graft_free(graft_0);
148 return equal;
151 /* Hoist "guard" out of the current level (given by "build").
153 * In particular, eliminate the dimension corresponding to the current depth.
155 static __isl_give isl_set *hoist_guard(__isl_take isl_set *guard,
156 __isl_keep isl_ast_build *build)
158 int depth;
159 isl_size dim;
161 depth = isl_ast_build_get_depth(build);
162 dim = isl_set_dim(guard, isl_dim_set);
163 if (dim < 0)
164 return isl_set_free(guard);
165 if (depth < dim) {
166 guard = isl_set_remove_divs_involving_dims(guard,
167 isl_dim_set, depth, 1);
168 guard = isl_set_eliminate(guard, isl_dim_set, depth, 1);
169 guard = isl_set_compute_divs(guard);
172 return guard;
175 /* Extract a common guard from the grafts in "list" that can be hoisted
176 * out of the current level. If no such guard can be found, then return
177 * a universal set.
179 * If all the grafts in the list have the same guard and if this guard
180 * is independent of the current level, then it can be hoisted out.
181 * If there is only one graft in the list and if its guard
182 * depends on the current level, then we eliminate this level and
183 * return the result.
185 * Otherwise, we return the unshifted simple hull of the guards.
186 * In order to be able to hoist as many constraints as possible,
187 * but at the same time avoid hoisting constraints that did not
188 * appear in the guards in the first place, we intersect the guards
189 * with all the information that is available (i.e., the domain
190 * from the build and the enforced constraints of the graft) and
191 * compute the unshifted hull of the result using only constraints
192 * from the original guards.
193 * In particular, intersecting the guards with other known information
194 * allows us to hoist guards that are only explicit is some of
195 * the grafts and implicit in the others.
197 * The special case for equal guards is needed in case those guards
198 * are non-convex. Taking the simple hull would remove information
199 * and would not allow for these guards to be hoisted completely.
201 __isl_give isl_set *isl_ast_graft_list_extract_hoistable_guard(
202 __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
204 int i, n;
205 isl_bool equal;
206 isl_ctx *ctx;
207 isl_set *guard;
208 isl_set_list *set_list;
209 isl_basic_set *hull;
211 if (!list || !build)
212 return NULL;
214 n = isl_ast_graft_list_n_ast_graft(list);
215 if (n == 0)
216 return isl_set_universe(isl_ast_build_get_space(build, 1));
218 equal = equal_independent_guards(list, build);
219 if (equal < 0)
220 return NULL;
222 if (equal || n == 1) {
223 isl_ast_graft *graft_0;
225 graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
226 if (!graft_0)
227 return NULL;
228 guard = isl_set_copy(graft_0->guard);
229 if (!equal)
230 guard = hoist_guard(guard, build);
231 isl_ast_graft_free(graft_0);
232 return guard;
235 ctx = isl_ast_build_get_ctx(build);
236 set_list = isl_set_list_alloc(ctx, n);
237 guard = isl_set_empty(isl_ast_build_get_space(build, 1));
238 for (i = 0; i < n; ++i) {
239 isl_ast_graft *graft;
240 isl_basic_set *enforced;
241 isl_set *guard_i;
243 graft = isl_ast_graft_list_get_ast_graft(list, i);
244 enforced = isl_ast_graft_get_enforced(graft);
245 guard_i = isl_set_copy(graft->guard);
246 isl_ast_graft_free(graft);
247 set_list = isl_set_list_add(set_list, isl_set_copy(guard_i));
248 guard_i = isl_set_intersect(guard_i,
249 isl_set_from_basic_set(enforced));
250 guard_i = isl_set_intersect(guard_i,
251 isl_ast_build_get_domain(build));
252 guard = isl_set_union(guard, guard_i);
254 hull = isl_set_unshifted_simple_hull_from_set_list(guard, set_list);
255 guard = isl_set_from_basic_set(hull);
256 return hoist_guard(guard, build);
259 /* Internal data structure used inside insert_if.
261 * list is the list of guarded nodes created by each call to insert_if.
262 * node is the original node that is guarded by insert_if.
263 * build is the build in which the AST is constructed.
265 struct isl_insert_if_data {
266 isl_ast_node_list *list;
267 isl_ast_node *node;
268 isl_ast_build *build;
271 static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user);
273 /* Insert an if node around "node" testing the condition encoded
274 * in guard "guard".
276 * If the user does not want any disjunctions in the if conditions
277 * and if "guard" does involve a disjunction, then we make the different
278 * disjuncts disjoint and insert an if node corresponding to each disjunct
279 * around a copy of "node". The result is then a block node containing
280 * this sequence of guarded copies of "node".
282 static __isl_give isl_ast_node *ast_node_insert_if(
283 __isl_take isl_ast_node *node, __isl_take isl_set *guard,
284 __isl_keep isl_ast_build *build)
286 struct isl_insert_if_data data;
287 isl_ctx *ctx;
289 ctx = isl_ast_build_get_ctx(build);
290 if (isl_options_get_ast_build_allow_or(ctx) ||
291 isl_set_n_basic_set(guard) <= 1) {
292 isl_ast_node *if_node;
293 isl_ast_expr *expr;
295 expr = isl_ast_build_expr_from_set_internal(build, guard);
297 if_node = isl_ast_node_alloc_if(expr);
298 return isl_ast_node_if_set_then(if_node, node);
301 guard = isl_set_make_disjoint(guard);
303 data.list = isl_ast_node_list_alloc(ctx, 0);
304 data.node = node;
305 data.build = build;
306 if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0)
307 data.list = isl_ast_node_list_free(data.list);
309 isl_set_free(guard);
310 isl_ast_node_free(data.node);
311 return isl_ast_node_alloc_block(data.list);
314 /* Insert an if node around a copy of "data->node" testing the condition
315 * encoded in guard "bset" and add the result to data->list.
317 static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user)
319 struct isl_insert_if_data *data = user;
320 isl_ast_node *node;
321 isl_set *set;
323 set = isl_set_from_basic_set(bset);
324 node = isl_ast_node_copy(data->node);
325 node = ast_node_insert_if(node, set, data->build);
326 data->list = isl_ast_node_list_add(data->list, node);
328 return isl_stat_ok;
331 /* Insert an if node around graft->node testing the condition encoded
332 * in guard "guard", assuming guard involves any conditions.
334 static __isl_give isl_ast_graft *insert_if_node(
335 __isl_take isl_ast_graft *graft, __isl_take isl_set *guard,
336 __isl_keep isl_ast_build *build)
338 int univ;
340 if (!graft)
341 goto error;
343 univ = isl_set_plain_is_universe(guard);
344 if (univ < 0)
345 goto error;
346 if (univ) {
347 isl_set_free(guard);
348 return graft;
351 build = isl_ast_build_copy(build);
352 graft->node = ast_node_insert_if(graft->node, guard, build);
353 isl_ast_build_free(build);
355 if (!graft->node)
356 return isl_ast_graft_free(graft);
358 return graft;
359 error:
360 isl_set_free(guard);
361 return isl_ast_graft_free(graft);
364 /* Insert an if node around graft->node testing the condition encoded
365 * in graft->guard, assuming graft->guard involves any conditions.
367 static __isl_give isl_ast_graft *insert_pending_guard_node(
368 __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
370 if (!graft)
371 return NULL;
373 return insert_if_node(graft, isl_set_copy(graft->guard), build);
376 /* Replace graft->enforced by "enforced".
378 __isl_give isl_ast_graft *isl_ast_graft_set_enforced(
379 __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
381 if (!graft || !enforced)
382 goto error;
384 isl_basic_set_free(graft->enforced);
385 graft->enforced = enforced;
387 return graft;
388 error:
389 isl_basic_set_free(enforced);
390 return isl_ast_graft_free(graft);
393 /* Update "enforced" such that it only involves constraints that are
394 * also enforced by "graft".
396 static __isl_give isl_basic_set *update_enforced(
397 __isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft,
398 int depth)
400 isl_size dim;
401 isl_basic_set *enforced_g;
403 enforced_g = isl_ast_graft_get_enforced(graft);
404 dim = isl_basic_set_dim(enforced_g, isl_dim_set);
405 if (dim < 0)
406 enforced_g = isl_basic_set_free(enforced_g);
407 if (depth < dim)
408 enforced_g = isl_basic_set_eliminate(enforced_g,
409 isl_dim_set, depth, 1);
410 enforced_g = isl_basic_set_remove_unknown_divs(enforced_g);
411 enforced_g = isl_basic_set_align_params(enforced_g,
412 isl_basic_set_get_space(enforced));
413 enforced = isl_basic_set_align_params(enforced,
414 isl_basic_set_get_space(enforced_g));
415 enforced = isl_set_simple_hull(isl_basic_set_union(enforced,
416 enforced_g));
418 return enforced;
421 /* Extend the node at *body with node.
423 * If body points to the else branch, then *body may still be NULL.
424 * If so, we simply attach node to this else branch.
425 * Otherwise, we attach a list containing the statements already
426 * attached at *body followed by node.
428 static void extend_body(__isl_keep isl_ast_node **body,
429 __isl_take isl_ast_node *node)
431 isl_ast_node_list *list;
433 if (!*body) {
434 *body = node;
435 return;
438 if ((*body)->type == isl_ast_node_block) {
439 list = isl_ast_node_block_get_children(*body);
440 isl_ast_node_free(*body);
441 } else
442 list = isl_ast_node_list_from_ast_node(*body);
443 list = isl_ast_node_list_add(list, node);
444 *body = isl_ast_node_alloc_block(list);
447 /* Merge "graft" into the last graft of "list".
448 * body points to the then or else branch of an if node in that last graft.
450 * We attach graft->node to this branch and update the enforced
451 * set of the last graft of "list" to take into account the enforced
452 * set of "graft".
454 static __isl_give isl_ast_graft_list *graft_extend_body(
455 __isl_take isl_ast_graft_list *list,
456 __isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft,
457 __isl_keep isl_ast_build *build)
459 int n;
460 int depth;
461 isl_ast_graft *last;
462 isl_space *space;
463 isl_basic_set *enforced;
465 if (!list || !graft)
466 goto error;
467 extend_body(body, isl_ast_node_copy(graft->node));
468 if (!*body)
469 goto error;
471 n = isl_ast_graft_list_n_ast_graft(list);
472 last = isl_ast_graft_list_get_ast_graft(list, n - 1);
474 depth = isl_ast_build_get_depth(build);
475 space = isl_ast_build_get_space(build, 1);
476 enforced = isl_basic_set_empty(space);
477 enforced = update_enforced(enforced, last, depth);
478 enforced = update_enforced(enforced, graft, depth);
479 last = isl_ast_graft_set_enforced(last, enforced);
481 list = isl_ast_graft_list_set_ast_graft(list, n - 1, last);
482 isl_ast_graft_free(graft);
483 return list;
484 error:
485 isl_ast_graft_free(graft);
486 return isl_ast_graft_list_free(list);
489 /* Merge "graft" into the last graft of "list", attaching graft->node
490 * to the then branch of "last_if".
492 static __isl_give isl_ast_graft_list *extend_then(
493 __isl_take isl_ast_graft_list *list,
494 __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
495 __isl_keep isl_ast_build *build)
497 return graft_extend_body(list, &last_if->u.i.then, graft, build);
500 /* Merge "graft" into the last graft of "list", attaching graft->node
501 * to the else branch of "last_if".
503 static __isl_give isl_ast_graft_list *extend_else(
504 __isl_take isl_ast_graft_list *list,
505 __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
506 __isl_keep isl_ast_build *build)
508 return graft_extend_body(list, &last_if->u.i.else_node, graft, build);
511 /* This data structure keeps track of an if node.
513 * "node" is the actual if-node
514 * "guard" is the original, non-simplified guard of the node
515 * "complement" is the complement of "guard" in the context of outer if nodes
517 struct isl_if_node {
518 isl_ast_node *node;
519 isl_set *guard;
520 isl_set *complement;
523 /* Given a list of "n" if nodes, clear those starting at "first"
524 * and return "first" (i.e., the updated size of the array).
526 static int clear_if_nodes(struct isl_if_node *if_node, int first, int n)
528 int i;
530 for (i = first; i < n; ++i) {
531 isl_set_free(if_node[i].guard);
532 isl_set_free(if_node[i].complement);
535 return first;
538 /* For each graft in "list",
539 * insert an if node around graft->node testing the condition encoded
540 * in graft->guard, assuming graft->guard involves any conditions.
542 * We keep track of a list of generated if nodes that can be extended
543 * without changing the order of the elements in "list".
544 * If the guard of a graft is a subset of either the guard or its complement
545 * of one of those if nodes, then the node
546 * of the new graft is inserted into the then or else branch of the last graft
547 * and the current graft is discarded.
548 * The guard of the node is then simplified based on the conditions
549 * enforced at that then or else branch.
550 * Otherwise, the current graft is appended to the list.
552 * We only construct else branches if allowed by the user.
554 static __isl_give isl_ast_graft_list *insert_pending_guard_nodes(
555 __isl_take isl_ast_graft_list *list,
556 __isl_keep isl_ast_build *build)
558 int i, j, n, n_if;
559 int allow_else;
560 isl_ctx *ctx;
561 isl_ast_graft_list *res;
562 struct isl_if_node *if_node = NULL;
564 if (!build || !list)
565 return isl_ast_graft_list_free(list);
567 ctx = isl_ast_build_get_ctx(build);
568 n = isl_ast_graft_list_n_ast_graft(list);
570 allow_else = isl_options_get_ast_build_allow_else(ctx);
572 n_if = 0;
573 if (n > 1) {
574 if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1);
575 if (!if_node)
576 return isl_ast_graft_list_free(list);
579 res = isl_ast_graft_list_alloc(ctx, n);
581 for (i = 0; i < n; ++i) {
582 isl_set *guard;
583 isl_ast_graft *graft;
584 int subset, found_then, found_else;
585 isl_ast_node *node;
587 graft = isl_ast_graft_list_get_ast_graft(list, i);
588 if (!graft)
589 break;
590 subset = 0;
591 found_then = found_else = -1;
592 if (n_if > 0) {
593 isl_set *test;
594 test = isl_set_copy(graft->guard);
595 test = isl_set_intersect(test,
596 isl_set_copy(build->domain));
597 for (j = n_if - 1; j >= 0; --j) {
598 subset = isl_set_is_subset(test,
599 if_node[j].guard);
600 if (subset < 0 || subset) {
601 found_then = j;
602 break;
604 if (!allow_else)
605 continue;
606 subset = isl_set_is_subset(test,
607 if_node[j].complement);
608 if (subset < 0 || subset) {
609 found_else = j;
610 break;
613 n_if = clear_if_nodes(if_node, j + 1, n_if);
614 isl_set_free(test);
616 if (subset < 0) {
617 graft = isl_ast_graft_free(graft);
618 break;
621 guard = isl_set_copy(graft->guard);
622 if (found_then >= 0)
623 graft->guard = isl_set_gist(graft->guard,
624 isl_set_copy(if_node[found_then].guard));
625 else if (found_else >= 0)
626 graft->guard = isl_set_gist(graft->guard,
627 isl_set_copy(if_node[found_else].complement));
629 node = graft->node;
630 if (!graft->guard)
631 graft = isl_ast_graft_free(graft);
632 graft = insert_pending_guard_node(graft, build);
633 if (graft && graft->node != node && i != n - 1) {
634 isl_set *set;
635 if_node[n_if].node = graft->node;
636 if_node[n_if].guard = guard;
637 if (found_then >= 0)
638 set = if_node[found_then].guard;
639 else if (found_else >= 0)
640 set = if_node[found_else].complement;
641 else
642 set = build->domain;
643 set = isl_set_copy(set);
644 set = isl_set_subtract(set, isl_set_copy(guard));
645 if_node[n_if].complement = set;
646 n_if++;
647 } else
648 isl_set_free(guard);
649 if (!graft)
650 break;
652 if (found_then >= 0)
653 res = extend_then(res, if_node[found_then].node,
654 graft, build);
655 else if (found_else >= 0)
656 res = extend_else(res, if_node[found_else].node,
657 graft, build);
658 else
659 res = isl_ast_graft_list_add(res, graft);
661 if (i < n)
662 res = isl_ast_graft_list_free(res);
664 isl_ast_graft_list_free(list);
665 clear_if_nodes(if_node, 0, n_if);
666 free(if_node);
667 return res;
670 /* For each graft in "list",
671 * insert an if node around graft->node testing the condition encoded
672 * in graft->guard, assuming graft->guard involves any conditions.
673 * Subsequently remove the guards from the grafts.
675 __isl_give isl_ast_graft_list *isl_ast_graft_list_insert_pending_guard_nodes(
676 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
678 int i, n;
679 isl_set *universe;
681 list = insert_pending_guard_nodes(list, build);
682 if (!list)
683 return NULL;
685 universe = isl_set_universe(isl_ast_build_get_space(build, 1));
686 n = isl_ast_graft_list_n_ast_graft(list);
687 for (i = 0; i < n; ++i) {
688 isl_ast_graft *graft;
690 graft = isl_ast_graft_list_get_ast_graft(list, i);
691 if (!graft)
692 break;
693 isl_set_free(graft->guard);
694 graft->guard = isl_set_copy(universe);
695 if (!graft->guard)
696 graft = isl_ast_graft_free(graft);
697 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
699 isl_set_free(universe);
700 if (i < n)
701 return isl_ast_graft_list_free(list);
703 return list;
706 /* Collect the nodes contained in the grafts in "list" in a node list.
708 static __isl_give isl_ast_node_list *extract_node_list(
709 __isl_keep isl_ast_graft_list *list)
711 int i, n;
712 isl_ctx *ctx;
713 isl_ast_node_list *node_list;
715 if (!list)
716 return NULL;
717 ctx = isl_ast_graft_list_get_ctx(list);
718 n = isl_ast_graft_list_n_ast_graft(list);
719 node_list = isl_ast_node_list_alloc(ctx, n);
720 for (i = 0; i < n; ++i) {
721 isl_ast_node *node;
722 isl_ast_graft *graft;
724 graft = isl_ast_graft_list_get_ast_graft(list, i);
725 node = isl_ast_graft_get_node(graft);
726 node_list = isl_ast_node_list_add(node_list, node);
727 isl_ast_graft_free(graft);
730 return node_list;
733 /* Look for shared enforced constraints by all the elements in "list"
734 * on outer loops (with respect to the current depth) and return the result.
736 * If there are no elements in "list", then return the empty set.
738 __isl_give isl_basic_set *isl_ast_graft_list_extract_shared_enforced(
739 __isl_keep isl_ast_graft_list *list,
740 __isl_keep isl_ast_build *build)
742 int i, n;
743 int depth;
744 isl_space *space;
745 isl_basic_set *enforced;
747 if (!list)
748 return NULL;
750 space = isl_ast_build_get_space(build, 1);
751 enforced = isl_basic_set_empty(space);
753 depth = isl_ast_build_get_depth(build);
754 n = isl_ast_graft_list_n_ast_graft(list);
755 for (i = 0; i < n; ++i) {
756 isl_ast_graft *graft;
758 graft = isl_ast_graft_list_get_ast_graft(list, i);
759 enforced = update_enforced(enforced, graft, depth);
760 isl_ast_graft_free(graft);
763 return enforced;
766 /* Record "guard" in "graft" so that it will be enforced somewhere
767 * up the tree. If the graft already has a guard, then it may be partially
768 * redundant in combination with the new guard and in the context
769 * the generated constraints of "build". In fact, the new guard
770 * may in itself have some redundant constraints.
771 * We therefore (re)compute the gist of the intersection
772 * and coalesce the result.
774 static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft,
775 __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
777 int is_universe;
779 if (!graft)
780 goto error;
782 is_universe = isl_set_plain_is_universe(guard);
783 if (is_universe < 0)
784 goto error;
785 if (is_universe) {
786 isl_set_free(guard);
787 return graft;
790 graft->guard = isl_set_intersect(graft->guard, guard);
791 graft->guard = isl_set_gist(graft->guard,
792 isl_ast_build_get_generated(build));
793 graft->guard = isl_set_coalesce(graft->guard);
794 if (!graft->guard)
795 return isl_ast_graft_free(graft);
797 return graft;
798 error:
799 isl_set_free(guard);
800 return isl_ast_graft_free(graft);
803 /* For each graft in "list", replace its guard with the gist with
804 * respect to "context".
806 static __isl_give isl_ast_graft_list *gist_guards(
807 __isl_take isl_ast_graft_list *list, __isl_keep isl_set *context)
809 int i, n;
811 if (!list)
812 return NULL;
814 n = isl_ast_graft_list_n_ast_graft(list);
815 for (i = 0; i < n; ++i) {
816 isl_ast_graft *graft;
818 graft = isl_ast_graft_list_get_ast_graft(list, i);
819 if (!graft)
820 break;
821 graft->guard = isl_set_gist(graft->guard,
822 isl_set_copy(context));
823 if (!graft->guard)
824 graft = isl_ast_graft_free(graft);
825 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
827 if (i < n)
828 return isl_ast_graft_list_free(list);
830 return list;
833 /* For each graft in "list", replace its guard with the gist with
834 * respect to "context".
836 __isl_give isl_ast_graft_list *isl_ast_graft_list_gist_guards(
837 __isl_take isl_ast_graft_list *list, __isl_take isl_set *context)
839 list = gist_guards(list, context);
840 isl_set_free(context);
842 return list;
845 /* Allocate a graft in "build" based on the list of grafts in "sub_build".
846 * "guard" and "enforced" are the guard and enforced constraints
847 * of the allocated graft. The guard is used to simplify the guards
848 * of the elements in "list".
850 * The node is initialized to either a block containing the nodes of "children"
851 * or, if there is only a single child, the node of that child.
852 * If the current level requires a for node, it should be inserted by
853 * a subsequent call to isl_ast_graft_insert_for.
855 __isl_give isl_ast_graft *isl_ast_graft_alloc_from_children(
856 __isl_take isl_ast_graft_list *list, __isl_take isl_set *guard,
857 __isl_take isl_basic_set *enforced, __isl_keep isl_ast_build *build,
858 __isl_keep isl_ast_build *sub_build)
860 isl_ast_build *guard_build;
861 isl_ast_node *node;
862 isl_ast_node_list *node_list;
863 isl_ast_graft *graft;
865 guard_build = isl_ast_build_copy(sub_build);
866 guard_build = isl_ast_build_replace_pending_by_guard(guard_build,
867 isl_set_copy(guard));
868 list = gist_guards(list, guard);
869 list = insert_pending_guard_nodes(list, guard_build);
870 isl_ast_build_free(guard_build);
872 node_list = extract_node_list(list);
873 node = isl_ast_node_from_ast_node_list(node_list);
874 isl_ast_graft_list_free(list);
876 graft = isl_ast_graft_alloc(node, build);
877 graft = store_guard(graft, guard, build);
878 graft = isl_ast_graft_enforce(graft, enforced);
880 return graft;
883 /* Combine the grafts in the list into a single graft.
885 * The guard is initialized to the shared guard of the list elements (if any),
886 * provided it does not depend on the current dimension.
887 * The guards in the elements are then simplified with respect to the
888 * hoisted guard and materialized as if nodes around the contained AST nodes
889 * in the context of "sub_build".
891 * The enforced set is initialized to the simple hull of the enforced sets
892 * of the elements, provided the ast_build_exploit_nested_bounds option is set
893 * or the new graft will be used at the same level.
895 * The node is initialized to either a block containing the nodes of "list"
896 * or, if there is only a single element, the node of that element.
898 static __isl_give isl_ast_graft *ast_graft_list_fuse(
899 __isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
901 isl_ast_graft *graft;
902 isl_basic_set *enforced;
903 isl_set *guard;
905 if (!list)
906 return NULL;
908 enforced = isl_ast_graft_list_extract_shared_enforced(list, build);
909 guard = isl_ast_graft_list_extract_hoistable_guard(list, build);
910 graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
911 build, build);
913 return graft;
916 /* Combine the grafts in the list into a single graft.
917 * Return a list containing this single graft.
918 * If the original list is empty, then return an empty list.
920 __isl_give isl_ast_graft_list *isl_ast_graft_list_fuse(
921 __isl_take isl_ast_graft_list *list,
922 __isl_keep isl_ast_build *build)
924 isl_ast_graft *graft;
926 if (!list)
927 return NULL;
928 if (isl_ast_graft_list_n_ast_graft(list) <= 1)
929 return list;
930 graft = ast_graft_list_fuse(list, build);
931 return isl_ast_graft_list_from_ast_graft(graft);
934 /* Combine the two grafts into a single graft.
935 * Return a list containing this single graft.
937 static __isl_give isl_ast_graft *isl_ast_graft_fuse(
938 __isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2,
939 __isl_keep isl_ast_build *build)
941 isl_ctx *ctx;
942 isl_ast_graft_list *list;
944 ctx = isl_ast_build_get_ctx(build);
946 list = isl_ast_graft_list_alloc(ctx, 2);
947 list = isl_ast_graft_list_add(list, graft1);
948 list = isl_ast_graft_list_add(list, graft2);
950 return ast_graft_list_fuse(list, build);
953 /* Insert a for node enclosing the current graft->node.
955 __isl_give isl_ast_graft *isl_ast_graft_insert_for(
956 __isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node)
958 if (!graft)
959 goto error;
961 graft->node = isl_ast_node_for_set_body(node, graft->node);
962 if (!graft->node)
963 return isl_ast_graft_free(graft);
965 return graft;
966 error:
967 isl_ast_node_free(node);
968 isl_ast_graft_free(graft);
969 return NULL;
972 /* Insert a mark governing the current graft->node.
974 __isl_give isl_ast_graft *isl_ast_graft_insert_mark(
975 __isl_take isl_ast_graft *graft, __isl_take isl_id *mark)
977 if (!graft)
978 goto error;
980 graft->node = isl_ast_node_alloc_mark(mark, graft->node);
981 if (!graft->node)
982 return isl_ast_graft_free(graft);
984 return graft;
985 error:
986 isl_id_free(mark);
987 isl_ast_graft_free(graft);
988 return NULL;
991 /* Represent the graft list as an AST node.
992 * This operation drops the information about guards in the grafts, so
993 * if there are any pending guards, then they are materialized as if nodes.
995 __isl_give isl_ast_node *isl_ast_node_from_graft_list(
996 __isl_take isl_ast_graft_list *list,
997 __isl_keep isl_ast_build *build)
999 isl_ast_node_list *node_list;
1001 list = insert_pending_guard_nodes(list, build);
1002 node_list = extract_node_list(list);
1003 isl_ast_graft_list_free(list);
1005 return isl_ast_node_from_ast_node_list(node_list);
1008 __isl_null isl_ast_graft *isl_ast_graft_free(__isl_take isl_ast_graft *graft)
1010 if (!graft)
1011 return NULL;
1013 if (--graft->ref > 0)
1014 return NULL;
1016 isl_ast_node_free(graft->node);
1017 isl_set_free(graft->guard);
1018 isl_basic_set_free(graft->enforced);
1019 free(graft);
1021 return NULL;
1024 /* Record that the grafted tree enforces
1025 * "enforced" by intersecting graft->enforced with "enforced".
1027 __isl_give isl_ast_graft *isl_ast_graft_enforce(
1028 __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
1030 if (!graft || !enforced)
1031 goto error;
1033 enforced = isl_basic_set_align_params(enforced,
1034 isl_basic_set_get_space(graft->enforced));
1035 graft->enforced = isl_basic_set_align_params(graft->enforced,
1036 isl_basic_set_get_space(enforced));
1037 graft->enforced = isl_basic_set_intersect(graft->enforced, enforced);
1038 if (!graft->enforced)
1039 return isl_ast_graft_free(graft);
1041 return graft;
1042 error:
1043 isl_basic_set_free(enforced);
1044 return isl_ast_graft_free(graft);
1047 __isl_give isl_basic_set *isl_ast_graft_get_enforced(
1048 __isl_keep isl_ast_graft *graft)
1050 return graft ? isl_basic_set_copy(graft->enforced) : NULL;
1053 __isl_give isl_set *isl_ast_graft_get_guard(__isl_keep isl_ast_graft *graft)
1055 return graft ? isl_set_copy(graft->guard) : NULL;
1058 /* Record that "guard" needs to be inserted in "graft".
1060 __isl_give isl_ast_graft *isl_ast_graft_add_guard(
1061 __isl_take isl_ast_graft *graft,
1062 __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
1064 return store_guard(graft, guard, build);
1067 /* Reformulate the "graft", which was generated in the context
1068 * of an inner code generation, in terms of the outer code generation
1069 * AST build.
1071 * If "product" is set, then the domain of the inner code generation build is
1073 * [O -> S]
1075 * with O the domain of the outer code generation build.
1076 * We essentially need to project out S.
1078 * If "product" is not set, then we need to project the domains onto
1079 * their parameter spaces.
1081 __isl_give isl_ast_graft *isl_ast_graft_unembed(__isl_take isl_ast_graft *graft,
1082 int product)
1084 isl_basic_set *enforced;
1086 if (!graft)
1087 return NULL;
1089 if (product) {
1090 enforced = graft->enforced;
1091 enforced = isl_basic_map_domain(isl_basic_set_unwrap(enforced));
1092 graft->enforced = enforced;
1093 graft->guard = isl_map_domain(isl_set_unwrap(graft->guard));
1094 } else {
1095 graft->enforced = isl_basic_set_params(graft->enforced);
1096 graft->guard = isl_set_params(graft->guard);
1098 graft->guard = isl_set_compute_divs(graft->guard);
1100 if (!graft->enforced || !graft->guard)
1101 return isl_ast_graft_free(graft);
1103 return graft;
1106 /* Reformulate the grafts in "list", which were generated in the context
1107 * of an inner code generation, in terms of the outer code generation
1108 * AST build.
1110 __isl_give isl_ast_graft_list *isl_ast_graft_list_unembed(
1111 __isl_take isl_ast_graft_list *list, int product)
1113 int i, n;
1115 n = isl_ast_graft_list_n_ast_graft(list);
1116 for (i = 0; i < n; ++i) {
1117 isl_ast_graft *graft;
1119 graft = isl_ast_graft_list_get_ast_graft(list, i);
1120 graft = isl_ast_graft_unembed(graft, product);
1121 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
1124 return list;
1127 /* Compute the preimage of "graft" under the function represented by "ma".
1128 * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
1130 __isl_give isl_ast_graft *isl_ast_graft_preimage_multi_aff(
1131 __isl_take isl_ast_graft *graft, __isl_take isl_multi_aff *ma)
1133 isl_basic_set *enforced;
1135 if (!graft)
1136 return NULL;
1138 enforced = graft->enforced;
1139 graft->enforced = isl_basic_set_preimage_multi_aff(enforced,
1140 isl_multi_aff_copy(ma));
1141 graft->guard = isl_set_preimage_multi_aff(graft->guard, ma);
1143 if (!graft->enforced || !graft->guard)
1144 return isl_ast_graft_free(graft);
1146 return graft;
1149 /* Compute the preimage of all the grafts in "list" under
1150 * the function represented by "ma".
1152 __isl_give isl_ast_graft_list *isl_ast_graft_list_preimage_multi_aff(
1153 __isl_take isl_ast_graft_list *list, __isl_take isl_multi_aff *ma)
1155 int i, n;
1157 n = isl_ast_graft_list_n_ast_graft(list);
1158 for (i = 0; i < n; ++i) {
1159 isl_ast_graft *graft;
1161 graft = isl_ast_graft_list_get_ast_graft(list, i);
1162 graft = isl_ast_graft_preimage_multi_aff(graft,
1163 isl_multi_aff_copy(ma));
1164 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
1167 isl_multi_aff_free(ma);
1168 return list;
1171 /* Compare two grafts based on their guards.
1173 static int cmp_graft(__isl_keep isl_ast_graft *a, __isl_keep isl_ast_graft *b,
1174 void *user)
1176 return isl_set_plain_cmp(a->guard, b->guard);
1179 /* Order the elements in "list" based on their guards.
1181 __isl_give isl_ast_graft_list *isl_ast_graft_list_sort_guard(
1182 __isl_take isl_ast_graft_list *list)
1184 return isl_ast_graft_list_sort(list, &cmp_graft, NULL);
1187 /* Merge the given two lists into a single list of grafts,
1188 * merging grafts with the same guard into a single graft.
1190 * "list2" has been sorted using isl_ast_graft_list_sort.
1191 * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1192 * and may therefore not be completely sorted.
1194 * The elements in "list2" need to be executed after those in "list1",
1195 * but if the guard of a graft in "list2" is disjoint from the guards
1196 * of some final elements in "list1", then it can be moved up to before
1197 * those final elements.
1199 * In particular, we look at each element g of "list2" in turn
1200 * and move it up beyond elements of "list1" that would be sorted
1201 * after g as long as each of these elements has a guard that is disjoint
1202 * from that of g.
1204 * We do not allow the second or any later element of "list2" to be moved
1205 * before a previous elements of "list2" even if the reason that
1206 * that element didn't move up further was that its guard was not disjoint
1207 * from that of the previous element in "list1".
1209 __isl_give isl_ast_graft_list *isl_ast_graft_list_merge(
1210 __isl_take isl_ast_graft_list *list1,
1211 __isl_take isl_ast_graft_list *list2,
1212 __isl_keep isl_ast_build *build)
1214 int i, j, first;
1216 if (!list1 || !list2 || !build)
1217 goto error;
1218 if (list2->n == 0) {
1219 isl_ast_graft_list_free(list2);
1220 return list1;
1222 if (list1->n == 0) {
1223 isl_ast_graft_list_free(list1);
1224 return list2;
1227 first = 0;
1228 for (i = 0; i < list2->n; ++i) {
1229 isl_ast_graft *graft;
1230 graft = isl_ast_graft_list_get_ast_graft(list2, i);
1231 if (!graft)
1232 break;
1234 for (j = list1->n; j >= 0; --j) {
1235 int cmp, disjoint;
1236 isl_ast_graft *graft_j;
1238 if (j == first)
1239 cmp = -1;
1240 else
1241 cmp = isl_set_plain_cmp(list1->p[j - 1]->guard,
1242 graft->guard);
1243 if (cmp > 0) {
1244 disjoint = isl_set_is_disjoint(graft->guard,
1245 list1->p[j - 1]->guard);
1246 if (disjoint < 0) {
1247 isl_ast_graft_free(graft);
1248 list1 = isl_ast_graft_list_free(list1);
1249 break;
1251 if (!disjoint)
1252 cmp = -1;
1254 if (cmp > 0)
1255 continue;
1256 if (cmp < 0) {
1257 list1 = isl_ast_graft_list_insert(list1, j,
1258 graft);
1259 break;
1262 --j;
1264 graft_j = isl_ast_graft_list_get_ast_graft(list1, j);
1265 graft_j = isl_ast_graft_fuse(graft_j, graft, build);
1266 list1 = isl_ast_graft_list_set_ast_graft(list1, j,
1267 graft_j);
1268 break;
1271 if (j < 0) {
1272 isl_ast_graft_free(graft);
1273 isl_die(isl_ast_build_get_ctx(build),
1274 isl_error_internal,
1275 "element failed to get inserted", break);
1278 first = j + 1;
1279 if (!list1)
1280 break;
1282 if (i < list2->n)
1283 list1 = isl_ast_graft_list_free(list1);
1284 isl_ast_graft_list_free(list2);
1286 return list1;
1287 error:
1288 isl_ast_graft_list_free(list1);
1289 isl_ast_graft_list_free(list2);
1290 return NULL;
1293 __isl_give isl_printer *isl_printer_print_ast_graft(__isl_take isl_printer *p,
1294 __isl_keep isl_ast_graft *graft)
1296 if (!p)
1297 return NULL;
1298 if (!graft)
1299 return isl_printer_free(p);
1301 p = isl_printer_print_str(p, "(");
1302 p = isl_printer_print_str(p, "guard: ");
1303 p = isl_printer_print_set(p, graft->guard);
1304 p = isl_printer_print_str(p, ", ");
1305 p = isl_printer_print_str(p, "enforced: ");
1306 p = isl_printer_print_basic_set(p, graft->enforced);
1307 p = isl_printer_print_str(p, ", ");
1308 p = isl_printer_print_str(p, "node: ");
1309 p = isl_printer_print_ast_node(p, graft->node);
1310 p = isl_printer_print_str(p, ")");
1312 return p;