add support for generating ASTs from schedule relations
[platform/upstream/isl.git] / isl_ast_graft.c
1 /*
2  * Copyright 2012      Ecole Normale Superieure
3  *
4  * Use of this software is governed by the MIT license
5  *
6  * Written by Sven Verdoolaege,
7  * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
8  */
9
10 #include <isl_list_private.h>
11 #include <isl_ast_private.h>
12 #include <isl_ast_build_expr.h>
13 #include <isl_ast_build_private.h>
14 #include <isl_ast_graft_private.h>
15
16 static __isl_give isl_ast_graft *isl_ast_graft_copy(
17         __isl_keep isl_ast_graft *graft);
18
19 #undef BASE
20 #define BASE ast_graft
21
22 #include <isl_list_templ.c>
23
24 #undef BASE
25 #define BASE ast_graft
26 #include <print_templ.c>
27
28 isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft)
29 {
30         if (!graft)
31                 return NULL;
32         return isl_basic_set_get_ctx(graft->enforced);
33 }
34
35 __isl_give isl_ast_node *isl_ast_graft_get_node(
36         __isl_keep isl_ast_graft *graft)
37 {
38         return graft ? isl_ast_node_copy(graft->node) : NULL;
39 }
40
41 /* Create a graft for "node" with no guards and no enforced conditions.
42  */
43 __isl_give isl_ast_graft *isl_ast_graft_alloc(
44         __isl_take isl_ast_node *node, __isl_keep isl_ast_build *build)
45 {
46         isl_ctx *ctx;
47         isl_space *space;
48         isl_ast_graft *graft;
49
50         if (!node)
51                 return NULL;
52
53         ctx = isl_ast_node_get_ctx(node);
54         graft = isl_calloc_type(ctx, isl_ast_graft);
55         if (!graft)
56                 goto error;
57
58         space = isl_ast_build_get_space(build, 1);
59
60         graft->ref = 1;
61         graft->node = node;
62         graft->guard = isl_set_universe(isl_space_copy(space));
63         graft->enforced = isl_basic_set_universe(space);
64
65         if (!graft->guard || !graft->enforced)
66                 return isl_ast_graft_free(graft);
67
68         return graft;
69 error:
70         isl_ast_node_free(node);
71         return NULL;
72 }
73
74 /* Create a graft with no guards and no enforced conditions
75  * encapsulating a call to the domain element specified by "executed".
76  * "executed" is assumed to be single-valued.
77  */
78 __isl_give isl_ast_graft *isl_ast_graft_alloc_domain(
79         __isl_take isl_map *executed, __isl_keep isl_ast_build *build)
80 {
81         isl_ast_node *node;
82
83         node = isl_ast_build_call_from_executed(build, executed);
84
85         return isl_ast_graft_alloc(node, build);
86 }
87
88 static __isl_give isl_ast_graft *isl_ast_graft_copy(
89         __isl_keep isl_ast_graft *graft)
90 {
91         if (!graft)
92                 return NULL;
93
94         graft->ref++;
95         return graft;
96 }
97
98 /* Do all the grafts in "list" have the same guard and is this guard
99  * independent of the current depth?
100  */
101 static int equal_independent_guards(__isl_keep isl_ast_graft_list *list,
102         __isl_keep isl_ast_build *build)
103 {
104         int i, n;
105         int depth;
106         isl_ast_graft *graft_0;
107         int equal = 1;
108         int skip;
109
110         graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
111         if (!graft_0)
112                 return -1;
113
114         depth = isl_ast_build_get_depth(build);
115         skip = isl_set_involves_dims(graft_0->guard, isl_dim_set, depth, 1);
116         if (skip < 0 || skip) {
117                 isl_ast_graft_free(graft_0);
118                 return skip < 0 ? -1 : 0;
119         }
120
121         n = isl_ast_graft_list_n_ast_graft(list);
122         for (i = 1; i < n; ++i) {
123                 isl_ast_graft *graft;
124                 graft = isl_ast_graft_list_get_ast_graft(list, i);
125                 if (!graft)
126                         equal = -1;
127                 else
128                         equal = isl_set_is_equal(graft_0->guard, graft->guard);
129                 isl_ast_graft_free(graft);
130                 if (equal < 0 || !equal)
131                         break;
132         }
133
134         isl_ast_graft_free(graft_0);
135
136         return equal;
137 }
138
139 /* Extract a common guard from the grafts in "list" that can be hoisted
140  * out of the current level.  If no such guard can be found, then return
141  * a universal set.
142  *
143  * If all the grafts in the list have the same guard and if this guard
144  * is independent of the current level, then it can be hoisted out.
145  * Otherwise, we return the unshifted simple hull of the guards.
146  *
147  * The special case for equal guards is needed in case those guards
148  * are non-convex.  Taking the simple hull would remove information
149  * and would not allow for these guards to be hoisted completely.
150  */
151 static __isl_give isl_set *extract_hoistable_guard(
152         __isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
153 {
154         int i, n;
155         int depth;
156         isl_ast_graft *graft_0;
157         int equal;
158         isl_set *guard;
159
160         if (!list || !build)
161                 return NULL;
162
163         n = isl_ast_graft_list_n_ast_graft(list);
164         if (n == 0)
165                 return isl_set_universe(isl_ast_build_get_space(build, 1));
166
167         equal = equal_independent_guards(list, build);
168         if (equal < 0)
169                 return NULL;
170
171         graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
172         if (!graft_0)
173                 return NULL;
174         guard = isl_set_copy(graft_0->guard);
175         isl_ast_graft_free(graft_0);
176         if (equal)
177                 return guard;
178
179         depth = isl_ast_build_get_depth(build);
180         if (depth < isl_set_dim(guard, isl_dim_set)) {
181                 guard = isl_set_eliminate(guard, isl_dim_set, depth, 1);
182                 guard = isl_set_compute_divs(guard);
183         }
184
185         for (i = 1; i < n; ++i) {
186                 isl_ast_graft *graft;
187                 isl_basic_set *hull;
188                 int is_universe;
189
190                 is_universe = isl_set_plain_is_universe(guard);
191                 if (is_universe < 0)
192                         guard = isl_set_free(guard);
193                 if (is_universe)
194                         break;
195
196                 graft = isl_ast_graft_list_get_ast_graft(list, i);
197                 if (!graft) {
198                         guard = isl_set_free(guard);
199                         break;
200                 }
201                 guard = isl_set_union(guard, isl_set_copy(graft->guard));
202                 hull = isl_set_unshifted_simple_hull(guard);
203                 guard = isl_set_from_basic_set(hull);
204                 isl_ast_graft_free(graft);
205         }
206
207         return guard;
208 }
209
210 /* Insert an if node around graft->node testing the condition encoded
211  * in guard "guard", assuming guard involves any conditions.
212  */
213 static __isl_give isl_ast_graft *insert_if_node(
214         __isl_take isl_ast_graft *graft, __isl_take isl_set *guard,
215         __isl_keep isl_ast_build *build)
216 {
217         int univ;
218         isl_ast_node *node;
219         isl_ast_expr *expr;
220
221         if (!graft)
222                 goto error;
223
224         univ = isl_set_plain_is_universe(guard);
225         if (univ < 0)
226                 goto error;
227         if (univ) {
228                 isl_set_free(guard);
229                 return graft;
230         }
231
232         build = isl_ast_build_copy(build);
233         build = isl_ast_build_set_enforced(build,
234                                         isl_ast_graft_get_enforced(graft));
235         expr = isl_ast_build_expr_from_set(build, guard);
236         isl_ast_build_free(build);
237
238         node = isl_ast_node_alloc_if(expr);
239         graft->node = isl_ast_node_if_set_then(node, graft->node);
240
241         if (!graft->node)
242                 return isl_ast_graft_free(graft);
243
244         return graft;
245 error:
246         isl_set_free(guard);
247         return isl_ast_graft_free(graft);
248 }
249
250 /* Insert an if node around graft->node testing the condition encoded
251  * in graft->guard, assuming graft->guard involves any conditions.
252  */
253 static __isl_give isl_ast_graft *insert_pending_guard_node(
254         __isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
255 {
256         if (!graft)
257                 return NULL;
258
259         return insert_if_node(graft, isl_set_copy(graft->guard), build);
260 }
261
262 /* Replace graft->enforced by "enforced".
263  */
264 __isl_give isl_ast_graft *isl_ast_graft_set_enforced(
265         __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
266 {
267         if (!graft || !enforced)
268                 goto error;
269
270         isl_basic_set_free(graft->enforced);
271         graft->enforced = enforced;
272
273         return graft;
274 error:
275         isl_basic_set_free(enforced);
276         return isl_ast_graft_free(graft);
277 }
278
279 /* Update "enforced" such that it only involves constraints that are
280  * also enforced by "graft".
281  */
282 static __isl_give isl_basic_set *update_enforced(
283         __isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft,
284         int depth)
285 {
286         isl_basic_set *enforced_g;
287
288         enforced_g = isl_ast_graft_get_enforced(graft);
289         if (depth < isl_basic_set_dim(enforced_g, isl_dim_set))
290                 enforced_g = isl_basic_set_eliminate(enforced_g,
291                                                         isl_dim_set, depth, 1);
292         enforced_g = isl_basic_set_remove_unknown_divs(enforced_g);
293         enforced_g = isl_basic_set_align_params(enforced_g,
294                                 isl_basic_set_get_space(enforced));
295         enforced = isl_basic_set_align_params(enforced,
296                                 isl_basic_set_get_space(enforced_g));
297         enforced = isl_set_simple_hull(isl_basic_set_union(enforced,
298                                                 enforced_g));
299
300         return enforced;
301 }
302
303 /* Extend the node at *body with node.
304  *
305  * If body points to the else branch, then *body may still be NULL.
306  * If so, we simply attach node to this else branch.
307  * Otherwise, we attach a list containing the statements already
308  * attached at *body followed by node.
309  */
310 static void extend_body(__isl_keep isl_ast_node **body,
311         __isl_take isl_ast_node *node)
312 {
313         isl_ast_node_list *list;
314
315         if  (!*body) {
316                 *body = node;
317                 return;
318         }
319
320         if ((*body)->type == isl_ast_node_block) {
321                 list = isl_ast_node_block_get_children(*body);
322                 isl_ast_node_free(*body);
323         } else
324                 list = isl_ast_node_list_from_ast_node(*body);
325         list = isl_ast_node_list_add(list, node);
326         *body = isl_ast_node_alloc_block(list);
327 }
328
329 /* Merge "graft" into the last graft of "list".
330  * body points to the then or else branch of an if node in that last graft.
331  *
332  * We attach graft->node to this branch and update the enforced
333  * set of the last graft of "list" to take into account the enforced
334  * set of "graft".
335  */
336 static __isl_give isl_ast_graft_list *graft_extend_body(
337         __isl_take isl_ast_graft_list *list,
338         __isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft,
339         __isl_keep isl_ast_build *build)
340 {
341         int n;
342         int depth;
343         isl_ast_graft *last;
344         isl_space *space;
345         isl_basic_set *enforced;
346
347         if (!list || !graft)
348                 goto error;
349         extend_body(body, isl_ast_node_copy(graft->node));
350         if (!*body)
351                 goto error;
352
353         n = isl_ast_graft_list_n_ast_graft(list);
354         last = isl_ast_graft_list_get_ast_graft(list, n - 1);
355
356         depth = isl_ast_build_get_depth(build);
357         space = isl_ast_build_get_space(build, 1);
358         enforced = isl_basic_set_empty(space);
359         enforced = update_enforced(enforced, last, depth);
360         enforced = update_enforced(enforced, graft, depth);
361         last = isl_ast_graft_set_enforced(last, enforced);
362
363         list = isl_ast_graft_list_set_ast_graft(list, n - 1, last);
364         isl_ast_graft_free(graft);
365         return list;
366 error:
367         isl_ast_graft_free(graft);
368         return isl_ast_graft_list_free(list);
369 }
370
371 /* Merge "graft" into the last graft of "list", attaching graft->node
372  * to the then branch of "last_if".
373  */
374 static __isl_give isl_ast_graft_list *extend_then(
375         __isl_take isl_ast_graft_list *list,
376         __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
377         __isl_keep isl_ast_build *build)
378 {
379         return graft_extend_body(list, &last_if->u.i.then, graft, build);
380 }
381
382 /* Merge "graft" into the last graft of "list", attaching graft->node
383  * to the else branch of "last_if".
384  */
385 static __isl_give isl_ast_graft_list *extend_else(
386         __isl_take isl_ast_graft_list *list,
387         __isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
388         __isl_keep isl_ast_build *build)
389 {
390         return graft_extend_body(list, &last_if->u.i.else_node, graft, build);
391 }
392
393 /* This data structure keeps track of an if node.
394  *
395  * "node" is the actual if-node
396  * "guard" is the original, non-simplified guard of the node
397  * "complement" is the complement of "guard" in the context of outer if nodes
398  */
399 struct isl_if_node {
400         isl_ast_node *node;
401         isl_set *guard;
402         isl_set *complement;
403 };
404
405 /* Given a list of "n" if nodes, clear those starting at "first"
406  * and return "first" (i.e., the updated size of the array).
407  */
408 static int clear_if_nodes(struct isl_if_node *if_node, int first, int n)
409 {
410         int i;
411
412         for (i = first; i < n; ++i) {
413                 isl_set_free(if_node[i].guard);
414                 isl_set_free(if_node[i].complement);
415         }
416
417         return first;
418 }
419
420 /* For each graft in "list",
421  * insert an if node around graft->node testing the condition encoded
422  * in graft->guard, assuming graft->guard involves any conditions.
423  *
424  * We keep track of a list of generated if nodes that can be extended
425  * without changing the order of the elements in "list".
426  * If the guard of a graft is a subset of either the guard or its complement
427  * of one of those if nodes, then the node
428  * of the new graft is inserted into the then or else branch of the last graft
429  * and the current graft is discarded.
430  * The guard of the node is then simplified based on the conditions
431  * enforced at that then or else branch.
432  * Otherwise, the current graft is appended to the list.
433  */
434 static __isl_give isl_ast_graft_list *insert_pending_guard_nodes(
435         __isl_take isl_ast_graft_list *list,
436         __isl_keep isl_ast_build *build)
437 {
438         int i, j, n, n_if;
439         isl_ctx *ctx;
440         isl_ast_graft_list *res;
441         struct isl_if_node *if_node = NULL;
442
443         if (!build || !list)
444                 return isl_ast_graft_list_free(list);
445
446         ctx = isl_ast_build_get_ctx(build);
447         n = isl_ast_graft_list_n_ast_graft(list);
448
449         n_if = 0;
450         if (n > 0) {
451                 if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1);
452                 if (!if_node)
453                         return isl_ast_graft_list_free(list);
454         }
455
456         res = isl_ast_graft_list_alloc(ctx, n);
457
458         for (i = 0; i < n; ++i) {
459                 isl_set *guard;
460                 isl_ast_graft *graft;
461                 int subset, found_then, found_else;
462                 isl_ast_node *node;
463
464                 graft = isl_ast_graft_list_get_ast_graft(list, i);
465                 if (!graft)
466                         break;
467                 subset = 0;
468                 found_then = found_else = -1;
469                 if (n_if > 0) {
470                         isl_set *test;
471                         test = isl_set_copy(graft->guard);
472                         test = isl_set_intersect(test,
473                                                 isl_set_copy(build->domain));
474                         for (j = n_if - 1; j >= 0; --j) {
475                                 subset = isl_set_is_subset(test,
476                                                         if_node[j].guard);
477                                 if (subset < 0 || subset) {
478                                         found_then = j;
479                                         break;
480                                 }
481                                 subset = isl_set_is_subset(test,
482                                                         if_node[j].complement);
483                                 if (subset < 0 || subset) {
484                                         found_else = j;
485                                         break;
486                                 }
487                         }
488                         n_if = clear_if_nodes(if_node, j + 1, n_if);
489                         isl_set_free(test);
490                 }
491                 if (subset < 0) {
492                         graft = isl_ast_graft_free(graft);
493                         break;
494                 }
495
496                 guard = isl_set_copy(graft->guard);
497                 if (found_then >= 0)
498                         graft->guard = isl_set_gist(graft->guard,
499                                 isl_set_copy(if_node[found_then].guard));
500                 else if (found_else >= 0)
501                         graft->guard = isl_set_gist(graft->guard,
502                                 isl_set_copy(if_node[found_else].complement));
503
504                 node = graft->node;
505                 if (!graft->guard)
506                         graft = isl_ast_graft_free(graft);
507                 graft = insert_pending_guard_node(graft, build);
508                 if (graft && graft->node != node && i != n - 1) {
509                         isl_set *set;
510                         if_node[n_if].node = graft->node;
511                         if_node[n_if].guard = guard;
512                         if (found_then >= 0)
513                                 set = if_node[found_then].guard;
514                         else if (found_else >= 0)
515                                 set = if_node[found_else].complement;
516                         else
517                                 set = build->domain;
518                         set = isl_set_copy(set);
519                         set = isl_set_subtract(set, isl_set_copy(guard));
520                         if_node[n_if].complement = set;
521                         n_if++;
522                 } else
523                         isl_set_free(guard);
524                 if (!graft)
525                         break;
526
527                 if (found_then >= 0)
528                         res = extend_then(res, if_node[found_then].node,
529                                                 graft, build);
530                 else if (found_else >= 0)
531                         res = extend_else(res, if_node[found_else].node,
532                                                 graft, build);
533                 else
534                         res = isl_ast_graft_list_add(res, graft);
535         }
536         if (i < n)
537                 res = isl_ast_graft_list_free(res);
538
539         isl_ast_graft_list_free(list);
540         clear_if_nodes(if_node, 0, n_if);
541         free(if_node);
542         return res;
543 }
544
545 /* Collect the nodes contained in the grafts in "list" in a node list.
546  */
547 static __isl_give isl_ast_node_list *extract_node_list(
548         __isl_keep isl_ast_graft_list *list)
549 {
550         int i, n;
551         isl_ctx *ctx;
552         isl_ast_node_list *node_list;
553
554         if (!list)
555                 return NULL;
556         ctx = isl_ast_graft_list_get_ctx(list);
557         n = isl_ast_graft_list_n_ast_graft(list);
558         node_list = isl_ast_node_list_alloc(ctx, n);
559         for (i = 0; i < n; ++i) {
560                 isl_ast_node *node;
561                 isl_ast_graft *graft;
562
563                 graft = isl_ast_graft_list_get_ast_graft(list, i);
564                 node = isl_ast_graft_get_node(graft);
565                 node_list = isl_ast_node_list_add(node_list, node);
566                 isl_ast_graft_free(graft);
567         }
568
569         return node_list;
570 }
571
572 /* Look for shared enforced constraints by all the elements in "list"
573  * on outer loops (with respect to the current depth) and return the result.
574  *
575  * We assume that the number of children is at least one.
576  */
577 static __isl_give isl_basic_set *extract_shared_enforced(
578         __isl_keep isl_ast_graft_list *list,
579         __isl_keep isl_ast_build *build)
580 {
581         int i, n;
582         int depth;
583         isl_space *space;
584         isl_basic_set *enforced;
585
586         if (!list)
587                 return NULL;
588
589         n = isl_ast_graft_list_n_ast_graft(list);
590         if (n == 0)
591                 isl_die(isl_ast_graft_list_get_ctx(list), isl_error_invalid,
592                         "for node should have at least one child",
593                         return NULL);
594
595         space = isl_ast_build_get_space(build, 1);
596         enforced = isl_basic_set_empty(space);
597
598         depth = isl_ast_build_get_depth(build);
599         for (i = 0; i < n; ++i) {
600                 isl_ast_graft *graft;
601
602                 graft = isl_ast_graft_list_get_ast_graft(list, i);
603                 enforced = update_enforced(enforced, graft, depth);
604                 isl_ast_graft_free(graft);
605         }
606
607         return enforced;
608 }
609
610 /* Record "guard" in "graft" so that it will be enforced somewhere
611  * up the tree.  If the graft already has a guard, then it may be partially
612  * redundant in combination with the new guard and in the context
613  * of build->domain.  We therefore (re)compute the gist of the intersection.
614  */
615 static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft,
616         __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
617 {
618         int is_universe;
619
620         if (!graft)
621                 goto error;
622
623         is_universe = isl_set_plain_is_universe(guard);
624         if (is_universe < 0)
625                 goto error;
626         if (is_universe) {
627                 isl_set_free(guard);
628                 return graft;
629         }
630
631         graft->guard = isl_set_intersect(graft->guard, guard);
632         graft->guard = isl_ast_build_compute_gist(build, graft->guard);
633         if (!graft->guard)
634                 return isl_ast_graft_free(graft);
635
636         return graft;
637 error:
638         isl_set_free(guard);
639         return isl_ast_graft_free(graft);
640 }
641
642 /* For each graft in "list", replace its guard with the gist with
643  * respect to "context".
644  */
645 static __isl_give isl_ast_graft_list *gist_guards(
646         __isl_take isl_ast_graft_list *list, __isl_keep isl_set *context)
647 {
648         int i, n;
649
650         if (!list)
651                 return NULL;
652
653         n = isl_ast_graft_list_n_ast_graft(list);
654         for (i = 0; i < n; ++i) {
655                 isl_ast_graft *graft;
656
657                 graft = isl_ast_graft_list_get_ast_graft(list, i);
658                 if (!graft)
659                         break;
660                 graft->guard = isl_set_gist(graft->guard,
661                                                 isl_set_copy(context));
662                 if (!graft->guard)
663                         graft = isl_ast_graft_free(graft);
664                 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
665         }
666         if (i < n)
667                 return isl_ast_graft_list_free(list);
668
669         return list;
670 }
671
672 /* Combine the grafts in the list into a single graft.
673  *
674  * If "up" is set then the resulting graft will be used at an outer level.
675  *
676  * The guard is initialized to the shared guard of the list elements (if any),
677  * provided it does not depend on the current dimension.
678  * The guards in the elements are then simplified with respect to the
679  * hoisted guard and materialized as if nodes around the contained AST nodes.
680  *
681  * The enforced set is initialized to the simple hull of the enforced sets
682  * of the elements, provided the ast_build_exploit_nested_bounds option is set
683  * or the new graft will be used at the same level.
684  *
685  * The node is initialized to either a block containing the nodes of "list"
686  * or, if there is only a single element, the node of that element.
687  */
688 static __isl_give isl_ast_graft *ast_graft_list_fuse(
689         __isl_take isl_ast_graft_list *list,
690         __isl_keep isl_ast_build *build, int up)
691 {
692         isl_ctx *ctx;
693         isl_ast_node *node;
694         isl_ast_graft *graft;
695         isl_ast_node_list *node_list;
696         isl_set *guard;
697
698         if (!list)
699                 return NULL;
700
701         ctx = isl_ast_build_get_ctx(build);
702         guard = extract_hoistable_guard(list, build);
703         list = gist_guards(list, guard);
704         list = insert_pending_guard_nodes(list, build);
705
706         node_list = extract_node_list(list);
707         node = isl_ast_node_from_ast_node_list(node_list);
708
709         graft = isl_ast_graft_alloc(node, build);
710
711         if (!up || isl_options_get_ast_build_exploit_nested_bounds(ctx)) {
712                 isl_basic_set *enforced;
713                 enforced = extract_shared_enforced(list, build);
714                 graft = isl_ast_graft_enforce(graft, enforced);
715         }
716
717         graft = store_guard(graft, guard, build);
718
719         isl_ast_graft_list_free(list);
720         return graft;
721 }
722
723 /* Combine the grafts in the list into a single graft.
724  * Return a list containing this single graft.
725  * If the original list is empty, then return an empty list.
726  */
727 __isl_give isl_ast_graft_list *isl_ast_graft_list_fuse(
728         __isl_take isl_ast_graft_list *list,
729         __isl_keep isl_ast_build *build)
730 {
731         isl_ast_graft *graft;
732
733         if (!list)
734                 return NULL;
735         if (isl_ast_graft_list_n_ast_graft(list) <= 1)
736                 return list;
737         graft = ast_graft_list_fuse(list, build, 0);
738         return isl_ast_graft_list_from_ast_graft(graft);
739 }
740
741 /* Combine the two grafts into a single graft.
742  * Return a list containing this single graft.
743  */
744 static __isl_give isl_ast_graft *isl_ast_graft_fuse(
745         __isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2,
746         __isl_keep isl_ast_build *build)
747 {
748         isl_ctx *ctx;
749         isl_ast_graft_list *list;
750
751         ctx = isl_ast_build_get_ctx(build);
752
753         list = isl_ast_graft_list_alloc(ctx, 2);
754         list = isl_ast_graft_list_add(list, graft1);
755         list = isl_ast_graft_list_add(list, graft2);
756
757         return ast_graft_list_fuse(list, build, 0);
758 }
759
760 /* Allocate a graft for the current level based on the list of grafts
761  * of the inner level.
762  *
763  * The node is initialized to either a block containing the nodes of "children"
764  * or, if there is only a single child, the node of that child.
765  * If the current level requires a for node, it should be inserted by
766  * a subsequent call to isl_ast_graft_insert_for.
767  */
768 __isl_give isl_ast_graft *isl_ast_graft_alloc_level(
769         __isl_take isl_ast_graft_list *children,
770         __isl_keep isl_ast_build *build)
771 {
772         return ast_graft_list_fuse(children, build, 1);
773 }
774
775 /* Insert a for node enclosing the current graft->node.
776  */
777 __isl_give isl_ast_graft *isl_ast_graft_insert_for(
778         __isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node)
779 {
780         if (!graft)
781                 goto error;
782
783         graft->node = isl_ast_node_for_set_body(node, graft->node);
784         if (!graft->node)
785                 return isl_ast_graft_free(graft);
786
787         return graft;
788 error:
789         isl_ast_node_free(node);
790         isl_ast_graft_free(graft);
791         return NULL;
792 }
793
794 /* Represent the graft list as an AST node.
795  * This operation drops the information about guards in the grafts, so
796  * if there are any pending guards, then they are materialized as if nodes.
797  */
798 __isl_give isl_ast_node *isl_ast_node_from_graft_list(
799         __isl_take isl_ast_graft_list *list,
800         __isl_keep isl_ast_build *build)
801 {
802         isl_ast_node_list *node_list;
803
804         list = insert_pending_guard_nodes(list, build);
805         node_list = extract_node_list(list);
806         isl_ast_graft_list_free(list);
807
808         return isl_ast_node_from_ast_node_list(node_list);
809 }
810
811 void *isl_ast_graft_free(__isl_take isl_ast_graft *graft)
812 {
813         if (!graft)
814                 return NULL;
815
816         if (--graft->ref > 0)
817                 return NULL;
818
819         isl_ast_node_free(graft->node);
820         isl_set_free(graft->guard);
821         isl_basic_set_free(graft->enforced);
822         free(graft);
823
824         return NULL;
825 }
826
827 /* Record that the grafted tree enforces
828  * "enforced" by intersecting graft->enforced with "enforced".
829  */
830 __isl_give isl_ast_graft *isl_ast_graft_enforce(
831         __isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
832 {
833         if (!graft || !enforced)
834                 goto error;
835
836         enforced = isl_basic_set_align_params(enforced,
837                                 isl_basic_set_get_space(graft->enforced));
838         graft->enforced = isl_basic_set_align_params(graft->enforced,
839                                 isl_basic_set_get_space(enforced));
840         graft->enforced = isl_basic_set_intersect(graft->enforced, enforced);
841         if (!graft->enforced)
842                 return isl_ast_graft_free(graft);
843
844         return graft;
845 error:
846         isl_basic_set_free(enforced);
847         return isl_ast_graft_free(graft);
848 }
849
850 __isl_give isl_basic_set *isl_ast_graft_get_enforced(
851         __isl_keep isl_ast_graft *graft)
852 {
853         return graft ? isl_basic_set_copy(graft->enforced) : NULL;
854 }
855
856 __isl_give isl_set *isl_ast_graft_get_guard(__isl_keep isl_ast_graft *graft)
857 {
858         return graft ? isl_set_copy(graft->guard) : NULL;
859 }
860
861 /* Record that "guard" needs to be inserted in "graft".
862  *
863  * We first simplify the guard in the context of the enforced set and
864  * then we store the guard in case we may be able
865  * to hoist it to higher levels and/or combine it with those of other grafts.
866  */
867 __isl_give isl_ast_graft *isl_ast_graft_add_guard(
868         __isl_take isl_ast_graft *graft,
869         __isl_take isl_set *guard, __isl_keep isl_ast_build *build)
870 {
871         isl_basic_set *enforced;
872
873         if (!graft || !build)
874                 goto error;
875
876         enforced = isl_basic_set_copy(graft->enforced);
877         guard = isl_set_gist(guard, isl_set_from_basic_set(enforced));
878
879         graft = store_guard(graft, guard, build);
880
881         return graft;
882 error:
883         isl_set_free(guard);
884         isl_ast_graft_free(graft);
885         return NULL;
886 }
887
888 /* Reformulate the "graft", which was generated in the context
889  * of an inner code generation, in terms of the outer code generation
890  * AST build.
891  *
892  * If "product" is set, then the domain of the inner code generation build is
893  *
894  *      [O -> S]
895  *
896  * with O the domain of the outer code generation build.
897  * We essentially need to project out S.
898  *
899  * If "product" is not set, then we need to project the domains onto
900  * their parameter spaces.
901  */
902 __isl_give isl_ast_graft *isl_ast_graft_unembed(__isl_take isl_ast_graft *graft,
903         int product)
904 {
905         isl_basic_set *enforced;
906
907         if (!graft)
908                 return NULL;
909
910         if (product) {
911                 enforced = graft->enforced;
912                 enforced = isl_basic_map_domain(isl_basic_set_unwrap(enforced));
913                 graft->enforced = enforced;
914                 graft->guard = isl_map_domain(isl_set_unwrap(graft->guard));
915         } else {
916                 graft->enforced = isl_basic_set_params(graft->enforced);
917                 graft->guard = isl_set_params(graft->guard);
918         }
919         graft->guard = isl_set_compute_divs(graft->guard);
920
921         if (!graft->enforced || !graft->guard)
922                 return isl_ast_graft_free(graft);
923
924         return graft;
925 }
926
927 /* Reformulate the grafts in "list", which were generated in the context
928  * of an inner code generation, in terms of the outer code generation
929  * AST build.
930  */
931 __isl_give isl_ast_graft_list *isl_ast_graft_list_unembed(
932         __isl_take isl_ast_graft_list *list, int product)
933 {
934         int i, n;
935
936         n = isl_ast_graft_list_n_ast_graft(list);
937         for (i = 0; i < n; ++i) {
938                 isl_ast_graft *graft;
939
940                 graft = isl_ast_graft_list_get_ast_graft(list, i);
941                 graft = isl_ast_graft_unembed(graft, product);
942                 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
943         }
944
945         return list;
946 }
947
948 /* Compute the preimage of "graft" under the function represented by "ma".
949  * In other words, plug in "ma" in "enforced" and "guard" fields of "graft".
950  */
951 __isl_give isl_ast_graft *isl_ast_graft_preimage_multi_aff(
952         __isl_take isl_ast_graft *graft, __isl_take isl_multi_aff *ma)
953 {
954         isl_basic_set *enforced;
955
956         if (!graft)
957                 return NULL;
958
959         enforced = graft->enforced;
960         graft->enforced = isl_basic_set_preimage_multi_aff(enforced,
961                                                 isl_multi_aff_copy(ma));
962         graft->guard = isl_set_preimage_multi_aff(graft->guard, ma);
963
964         if (!graft->enforced || !graft->guard)
965                 return isl_ast_graft_free(graft);
966
967         return graft;
968 }
969
970 /* Compute the preimage of all the grafts in "list" under
971  * the function represented by "ma".
972  */
973 __isl_give isl_ast_graft_list *isl_ast_graft_list_preimage_multi_aff(
974         __isl_take isl_ast_graft_list *list, __isl_take isl_multi_aff *ma)
975 {
976         int i, n;
977
978         n = isl_ast_graft_list_n_ast_graft(list);
979         for (i = 0; i < n; ++i) {
980                 isl_ast_graft *graft;
981
982                 graft = isl_ast_graft_list_get_ast_graft(list, i);
983                 graft = isl_ast_graft_preimage_multi_aff(graft,
984                                                     isl_multi_aff_copy(ma));
985                 list = isl_ast_graft_list_set_ast_graft(list, i, graft);
986         }
987
988         isl_multi_aff_free(ma);
989         return list;
990 }
991
992 /* Compare two grafts based on their guards.
993  */
994 static int cmp_graft(const void *a, const void *b)
995 {
996         isl_ast_graft * const *g1 = a;
997         isl_ast_graft * const *g2 = b;
998
999         return isl_set_plain_cmp((*g1)->guard, (*g2)->guard);
1000 }
1001
1002 /* Order the elements in "list" based on their guards.
1003  */
1004 __isl_give isl_ast_graft_list *isl_ast_graft_list_sort(
1005         __isl_take isl_ast_graft_list *list)
1006 {
1007         if (!list)
1008                 return NULL;
1009         if (list->n <= 1)
1010                 return list;
1011
1012         qsort(list->p, list->n, sizeof(list->p[0]), &cmp_graft);
1013
1014         return list;
1015 }
1016
1017 /* Merge the given two lists into a single list of grafts,
1018  * merging grafts with the same guard into a single graft.
1019  *
1020  * "list2" has been sorted using isl_ast_graft_list_sort.
1021  * "list1" may be the result of a previous call to isl_ast_graft_list_merge
1022  * and may therefore not be completely sorted.
1023  *
1024  * The elements in "list2" need to be executed after those in "list1",
1025  * but if the guard of a graft in "list2" is disjoint from the guards
1026  * of some final elements in "list1", then it can be moved up to before
1027  * those final elements.
1028  *
1029  * In particular, we look at each element g of "list2" in turn
1030  * and move it up beyond elements of "list1" that would be sorted
1031  * after g as long as each of these elements has a guard that is disjoint
1032  * from that of g.
1033  *
1034  * We do not allow the second or any later element of "list2" to be moved
1035  * before a previous elements of "list2" even if the reason that
1036  * that element didn't move up further was that its guard was not disjoint
1037  * from that of the previous element in "list1".
1038  */
1039 __isl_give isl_ast_graft_list *isl_ast_graft_list_merge(
1040         __isl_take isl_ast_graft_list *list1,
1041         __isl_take isl_ast_graft_list *list2,
1042         __isl_keep isl_ast_build *build)
1043 {
1044         int i, j, first;
1045
1046         if (!list1 || !list2 || !build)
1047                 goto error;
1048         if (list2->n == 0) {
1049                 isl_ast_graft_list_free(list2);
1050                 return list1;
1051         }
1052         if (list1->n == 0) {
1053                 isl_ast_graft_list_free(list1);
1054                 return list2;
1055         }
1056
1057         first = 0;
1058         for (i = 0; i < list2->n; ++i) {
1059                 isl_ast_graft *graft;
1060                 graft = isl_ast_graft_list_get_ast_graft(list2, i);
1061                 if (!graft)
1062                         break;
1063
1064                 for (j = list1->n; j >= 0; --j) {
1065                         int cmp, disjoint;
1066                         isl_ast_graft *graft_j;
1067
1068                         if (j == first)
1069                                 cmp = -1;
1070                         else
1071                                 cmp = isl_set_plain_cmp(list1->p[j - 1]->guard,
1072                                                         graft->guard);
1073                         if (cmp > 0) {
1074                                 disjoint = isl_set_is_disjoint(graft->guard,
1075                                                         list1->p[j - 1]->guard);
1076                                 if (disjoint < 0) {
1077                                         list1 = isl_ast_graft_list_free(list1);
1078                                         break;
1079                                 }
1080                                 if (!disjoint)
1081                                         cmp = -1;
1082                         }
1083                         if (cmp > 0)
1084                                 continue;
1085                         if (cmp < 0) {
1086                                 list1 = isl_ast_graft_list_insert(list1, j,
1087                                                                 graft);
1088                                 break;
1089                         }
1090
1091                         --j;
1092
1093                         graft_j = isl_ast_graft_list_get_ast_graft(list1, j);
1094                         graft_j = isl_ast_graft_fuse(graft_j, graft, build);
1095                         list1 = isl_ast_graft_list_set_ast_graft(list1, j,
1096                                                                 graft_j);
1097                         break;
1098                 }
1099
1100                 if (j < 0)
1101                         isl_die(isl_ast_build_get_ctx(build),
1102                                 isl_error_internal,
1103                                 "element failed to get inserted", break);
1104
1105                 first = j + 1;
1106                 if (!list1)
1107                         break;
1108         }
1109         if (i < list2->n)
1110                 list1 = isl_ast_graft_list_free(list1);
1111         isl_ast_graft_list_free(list2);
1112
1113         return list1;
1114 error:
1115         isl_ast_graft_list_free(list1);
1116         isl_ast_graft_list_free(list2);
1117         return NULL;
1118 }
1119
1120 __isl_give isl_printer *isl_printer_print_ast_graft(__isl_take isl_printer *p,
1121         __isl_keep isl_ast_graft *graft)
1122 {
1123         if (!p)
1124                 return NULL;
1125         if (!graft)
1126                 return isl_printer_free(p);
1127
1128         p = isl_printer_print_str(p, "(");
1129         p = isl_printer_print_str(p, "guard: ");
1130         p = isl_printer_print_set(p, graft->guard);
1131         p = isl_printer_print_str(p, ", ");
1132         p = isl_printer_print_str(p, "enforced: ");
1133         p = isl_printer_print_basic_set(p, graft->enforced);
1134         p = isl_printer_print_str(p, ", ");
1135         p = isl_printer_print_str(p, "node: ");
1136         p = isl_printer_print_ast_node(p, graft->node);
1137         p = isl_printer_print_str(p, ")");
1138
1139         return p;
1140 }