-
Notifications
You must be signed in to change notification settings - Fork 26
/
isl_ast_graft.c
1464 lines (1262 loc) · 39.5 KB
/
isl_ast_graft.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2012 Ecole Normale Superieure
* Copyright 2014 INRIA Rocquencourt
* Copyright 2019 Cerebras Systems
*
* Use of this software is governed by the MIT license
*
* Written by Sven Verdoolaege,
* Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
* and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
* B.P. 105 - 78153 Le Chesnay, France
* and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
*/
#include <isl/id.h>
#include <isl/space.h>
#include <isl_ast_private.h>
#include <isl_ast_build_expr.h>
#include <isl_ast_build_private.h>
#include <isl_ast_graft_private.h>
#include "isl_set_to_ast_graft_list.h"
static __isl_give isl_ast_graft *isl_ast_graft_copy(
__isl_keep isl_ast_graft *graft);
#undef EL_BASE
#define EL_BASE ast_graft
#include <isl_list_templ.c>
#undef BASE
#define BASE ast_graft
#include <print_templ.c>
isl_ctx *isl_ast_graft_get_ctx(__isl_keep isl_ast_graft *graft)
{
if (!graft)
return NULL;
return isl_basic_set_get_ctx(graft->enforced);
}
__isl_give isl_ast_node *isl_ast_graft_get_node(
__isl_keep isl_ast_graft *graft)
{
return graft ? isl_ast_node_copy(graft->node) : NULL;
}
/* Create a graft for "node" with no guards and no enforced conditions.
*/
__isl_give isl_ast_graft *isl_ast_graft_alloc(
__isl_take isl_ast_node *node, __isl_keep isl_ast_build *build)
{
isl_ctx *ctx;
isl_space *space;
isl_ast_graft *graft;
if (!node)
return NULL;
ctx = isl_ast_node_get_ctx(node);
graft = isl_calloc_type(ctx, isl_ast_graft);
if (!graft)
goto error;
space = isl_ast_build_get_space(build, 1);
graft->ref = 1;
graft->node = node;
graft->guard = isl_set_universe(isl_space_copy(space));
graft->enforced = isl_basic_set_universe(space);
if (!graft->guard || !graft->enforced)
return isl_ast_graft_free(graft);
return graft;
error:
isl_ast_node_free(node);
return NULL;
}
/* Create a graft with no guards and no enforced conditions
* encapsulating a call to the domain element specified by "executed".
* "executed" is assumed to be single-valued.
*/
__isl_give isl_ast_graft *isl_ast_graft_alloc_domain(
__isl_take isl_map *executed, __isl_keep isl_ast_build *build)
{
isl_ast_node *node;
node = isl_ast_build_call_from_executed(build, executed);
return isl_ast_graft_alloc(node, build);
}
static __isl_give isl_ast_graft *isl_ast_graft_copy(
__isl_keep isl_ast_graft *graft)
{
if (!graft)
return NULL;
graft->ref++;
return graft;
}
/* Do all the grafts in "list" have the same guard and is this guard
* independent of the current depth?
*/
static isl_bool equal_independent_guards(__isl_keep isl_ast_graft_list *list,
__isl_keep isl_ast_build *build)
{
int i;
isl_size n;
isl_size depth;
isl_size dim;
isl_ast_graft *graft_0;
isl_bool equal = isl_bool_true;
isl_bool skip;
n = isl_ast_graft_list_n_ast_graft(list);
depth = isl_ast_build_get_depth(build);
if (n < 0 || depth < 0)
return isl_bool_error;
graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
if (!graft_0)
return isl_bool_error;
dim = isl_set_dim(graft_0->guard, isl_dim_set);
if (dim < 0)
skip = isl_bool_error;
else if (dim <= depth)
skip = isl_bool_false;
else
skip = isl_set_involves_dims(graft_0->guard,
isl_dim_set, depth, 1);
if (skip < 0 || skip) {
isl_ast_graft_free(graft_0);
return isl_bool_not(skip);
}
for (i = 1; i < n; ++i) {
isl_ast_graft *graft;
graft = isl_ast_graft_list_get_ast_graft(list, i);
if (!graft)
equal = isl_bool_error;
else
equal = isl_set_is_equal(graft_0->guard, graft->guard);
isl_ast_graft_free(graft);
if (equal < 0 || !equal)
break;
}
isl_ast_graft_free(graft_0);
return equal;
}
/* Hoist "guard" out of the current level (given by "build").
*
* In particular, eliminate the dimension corresponding to the current depth.
*/
static __isl_give isl_set *hoist_guard(__isl_take isl_set *guard,
__isl_keep isl_ast_build *build)
{
isl_size depth;
isl_size dim;
depth = isl_ast_build_get_depth(build);
dim = isl_set_dim(guard, isl_dim_set);
if (depth < 0 || dim < 0)
return isl_set_free(guard);
if (depth < dim) {
guard = isl_set_remove_divs_involving_dims(guard,
isl_dim_set, depth, 1);
guard = isl_set_eliminate(guard, isl_dim_set, depth, 1);
guard = isl_set_compute_divs(guard);
}
return guard;
}
/* Extract a common guard from the grafts in "list" that can be hoisted
* out of the current level. If no such guard can be found, then return
* a universal set.
*
* If all the grafts in the list have the same guard and if this guard
* is independent of the current level, then it can be hoisted out.
* If there is only one graft in the list and if its guard
* depends on the current level, then we eliminate this level and
* return the result.
*
* Otherwise, we return the unshifted simple hull of the guards.
* In order to be able to hoist as many constraints as possible,
* but at the same time avoid hoisting constraints that did not
* appear in the guards in the first place, we intersect the guards
* with all the information that is available (i.e., the domain
* from the build and the enforced constraints of the graft) and
* compute the unshifted hull of the result using only constraints
* from the original guards.
* In particular, intersecting the guards with other known information
* allows us to hoist guards that are only explicit is some of
* the grafts and implicit in the others.
*
* The special case for equal guards is needed in case those guards
* are non-convex. Taking the simple hull would remove information
* and would not allow for these guards to be hoisted completely.
*/
__isl_give isl_set *isl_ast_graft_list_extract_hoistable_guard(
__isl_keep isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
{
int i;
isl_size n;
isl_bool equal;
isl_ctx *ctx;
isl_set *guard;
isl_set_list *set_list;
isl_basic_set *hull;
if (!list || !build)
return NULL;
n = isl_ast_graft_list_n_ast_graft(list);
if (n < 0)
return NULL;
if (n == 0)
return isl_set_universe(isl_ast_build_get_space(build, 1));
equal = equal_independent_guards(list, build);
if (equal < 0)
return NULL;
if (equal || n == 1) {
isl_ast_graft *graft_0;
graft_0 = isl_ast_graft_list_get_ast_graft(list, 0);
if (!graft_0)
return NULL;
guard = isl_set_copy(graft_0->guard);
if (!equal)
guard = hoist_guard(guard, build);
isl_ast_graft_free(graft_0);
return guard;
}
ctx = isl_ast_build_get_ctx(build);
set_list = isl_set_list_alloc(ctx, n);
guard = isl_set_empty(isl_ast_build_get_space(build, 1));
for (i = 0; i < n; ++i) {
isl_ast_graft *graft;
isl_basic_set *enforced;
isl_set *guard_i;
graft = isl_ast_graft_list_get_ast_graft(list, i);
enforced = isl_ast_graft_get_enforced(graft);
guard_i = isl_set_copy(graft->guard);
isl_ast_graft_free(graft);
set_list = isl_set_list_add(set_list, isl_set_copy(guard_i));
guard_i = isl_set_intersect(guard_i,
isl_set_from_basic_set(enforced));
guard_i = isl_set_intersect(guard_i,
isl_ast_build_get_domain(build));
guard = isl_set_union(guard, guard_i);
}
hull = isl_set_unshifted_simple_hull_from_set_list(guard, set_list);
guard = isl_set_from_basic_set(hull);
return hoist_guard(guard, build);
}
/* Internal data structure used inside insert_if.
*
* list is the list of guarded nodes created by each call to insert_if.
* node is the original node that is guarded by insert_if.
* build is the build in which the AST is constructed.
*/
struct isl_insert_if_data {
isl_ast_node_list *list;
isl_ast_node *node;
isl_ast_build *build;
};
static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user);
/* Insert an if node around "node" testing the condition encoded
* in guard "guard".
*
* If the user does not want any disjunctions in the if conditions
* and if "guard" does involve a disjunction, then we make the different
* disjuncts disjoint and insert an if node corresponding to each disjunct
* around a copy of "node". The result is then a block node containing
* this sequence of guarded copies of "node".
*/
static __isl_give isl_ast_node *ast_node_insert_if(
__isl_take isl_ast_node *node, __isl_take isl_set *guard,
__isl_keep isl_ast_build *build)
{
struct isl_insert_if_data data;
isl_ctx *ctx;
isl_size n;
n = isl_set_n_basic_set(guard);
if (n < 0)
goto error;
ctx = isl_ast_build_get_ctx(build);
if (isl_options_get_ast_build_allow_or(ctx) || n <= 1) {
isl_ast_node *if_node;
isl_ast_expr *expr;
expr = isl_ast_build_expr_from_set_internal(build, guard);
if_node = isl_ast_node_alloc_if(expr);
return isl_ast_node_if_set_then(if_node, node);
}
guard = isl_set_make_disjoint(guard);
data.list = isl_ast_node_list_alloc(ctx, 0);
data.node = node;
data.build = build;
if (isl_set_foreach_basic_set(guard, &insert_if, &data) < 0)
data.list = isl_ast_node_list_free(data.list);
isl_set_free(guard);
isl_ast_node_free(data.node);
return isl_ast_node_alloc_block(data.list);
error:
isl_set_free(guard);
isl_ast_node_free(node);
return NULL;
}
/* Insert an if node around a copy of "data->node" testing the condition
* encoded in guard "bset" and add the result to data->list.
*/
static isl_stat insert_if(__isl_take isl_basic_set *bset, void *user)
{
struct isl_insert_if_data *data = user;
isl_ast_node *node;
isl_set *set;
set = isl_set_from_basic_set(bset);
node = isl_ast_node_copy(data->node);
node = ast_node_insert_if(node, set, data->build);
data->list = isl_ast_node_list_add(data->list, node);
return isl_stat_ok;
}
/* Insert an if node around graft->node testing the condition encoded
* in guard "guard", assuming guard involves any conditions.
*/
static __isl_give isl_ast_graft *insert_if_node(
__isl_take isl_ast_graft *graft, __isl_take isl_set *guard,
__isl_keep isl_ast_build *build)
{
int univ;
if (!graft)
goto error;
univ = isl_set_plain_is_universe(guard);
if (univ < 0)
goto error;
if (univ) {
isl_set_free(guard);
return graft;
}
graft->node = ast_node_insert_if(graft->node, guard, build);
if (!graft->node)
return isl_ast_graft_free(graft);
return graft;
error:
isl_set_free(guard);
return isl_ast_graft_free(graft);
}
/* Insert an if node around graft->node testing the condition encoded
* in graft->guard, assuming graft->guard involves any conditions.
*/
static __isl_give isl_ast_graft *insert_pending_guard_node(
__isl_take isl_ast_graft *graft, __isl_keep isl_ast_build *build)
{
if (!graft)
return NULL;
return insert_if_node(graft, isl_set_copy(graft->guard), build);
}
/* Replace graft->enforced by "enforced".
*/
__isl_give isl_ast_graft *isl_ast_graft_set_enforced(
__isl_take isl_ast_graft *graft, __isl_take isl_basic_set *enforced)
{
if (!graft || !enforced)
goto error;
isl_basic_set_free(graft->enforced);
graft->enforced = enforced;
return graft;
error:
isl_basic_set_free(enforced);
return isl_ast_graft_free(graft);
}
/* Update "enforced" such that it only involves constraints that are
* also enforced by "graft".
*/
static __isl_give isl_basic_set *update_enforced(
__isl_take isl_basic_set *enforced, __isl_keep isl_ast_graft *graft,
int depth)
{
isl_size dim;
isl_basic_set *enforced_g;
enforced_g = isl_ast_graft_get_enforced(graft);
dim = isl_basic_set_dim(enforced_g, isl_dim_set);
if (dim < 0)
enforced_g = isl_basic_set_free(enforced_g);
if (depth < dim)
enforced_g = isl_basic_set_eliminate(enforced_g,
isl_dim_set, depth, 1);
enforced_g = isl_basic_set_remove_unknown_divs(enforced_g);
enforced_g = isl_basic_set_align_params(enforced_g,
isl_basic_set_get_space(enforced));
enforced = isl_basic_set_align_params(enforced,
isl_basic_set_get_space(enforced_g));
enforced = isl_set_simple_hull(isl_basic_set_union(enforced,
enforced_g));
return enforced;
}
/* Extend the node at *body with node.
*
* If body points to the else branch, then *body may still be NULL.
* If so, we simply attach node to this else branch.
* Otherwise, we attach a list containing the statements already
* attached at *body followed by node.
*/
static void extend_body(__isl_keep isl_ast_node **body,
__isl_take isl_ast_node *node)
{
isl_ast_node_list *list;
if (!*body) {
*body = node;
return;
}
if ((*body)->type == isl_ast_node_block) {
list = isl_ast_node_block_get_children(*body);
isl_ast_node_free(*body);
} else
list = isl_ast_node_list_from_ast_node(*body);
list = isl_ast_node_list_add(list, node);
*body = isl_ast_node_alloc_block(list);
}
/* Merge "graft" into the last graft of "list".
* body points to the then or else branch of an if node in that last graft.
*
* We attach graft->node to this branch and update the enforced
* set of the last graft of "list" to take into account the enforced
* set of "graft".
*/
static __isl_give isl_ast_graft_list *graft_extend_body(
__isl_take isl_ast_graft_list *list,
__isl_keep isl_ast_node **body, __isl_take isl_ast_graft *graft,
__isl_keep isl_ast_build *build)
{
isl_size n;
isl_size depth;
isl_ast_graft *last;
isl_space *space;
isl_basic_set *enforced;
n = isl_ast_graft_list_n_ast_graft(list);
depth = isl_ast_build_get_depth(build);
if (n < 0 || depth < 0 || !graft)
goto error;
extend_body(body, isl_ast_node_copy(graft->node));
if (!*body)
goto error;
last = isl_ast_graft_list_get_ast_graft(list, n - 1);
space = isl_ast_build_get_space(build, 1);
enforced = isl_basic_set_empty(space);
enforced = update_enforced(enforced, last, depth);
enforced = update_enforced(enforced, graft, depth);
last = isl_ast_graft_set_enforced(last, enforced);
list = isl_ast_graft_list_set_ast_graft(list, n - 1, last);
isl_ast_graft_free(graft);
return list;
error:
isl_ast_graft_free(graft);
return isl_ast_graft_list_free(list);
}
/* Merge "graft" into the last graft of "list", attaching graft->node
* to the then branch of "last_if".
*/
static __isl_give isl_ast_graft_list *extend_then(
__isl_take isl_ast_graft_list *list,
__isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
__isl_keep isl_ast_build *build)
{
return graft_extend_body(list, &last_if->u.i.then, graft, build);
}
/* Merge "graft" into the last graft of "list", attaching graft->node
* to the else branch of "last_if".
*/
static __isl_give isl_ast_graft_list *extend_else(
__isl_take isl_ast_graft_list *list,
__isl_keep isl_ast_node *last_if, __isl_take isl_ast_graft *graft,
__isl_keep isl_ast_build *build)
{
return graft_extend_body(list, &last_if->u.i.else_node, graft, build);
}
/* This data structure keeps track of an if node.
*
* "node" is the actual if-node
* "guard" is the original, non-simplified guard of the node
* "complement" is the complement of "guard" in the context of outer if nodes
*/
struct isl_if_node {
isl_ast_node *node;
isl_set *guard;
isl_set *complement;
};
/* Given a list of "n" if nodes, clear those starting at "first"
* and return "first" (i.e., the updated size of the array).
*/
static int clear_if_nodes(struct isl_if_node *if_node, int first, int n)
{
int i;
for (i = first; i < n; ++i) {
isl_set_free(if_node[i].guard);
isl_set_free(if_node[i].complement);
}
return first;
}
/* For each graft in "list",
* insert an if node around graft->node testing the condition encoded
* in graft->guard, assuming graft->guard involves any conditions.
*
* We keep track of a list of generated if nodes that can be extended
* without changing the order of the elements in "list".
* If the guard of a graft is a subset of either the guard or its complement
* of one of those if nodes, then the node
* of the new graft is inserted into the then or else branch of the last graft
* and the current graft is discarded.
* The guard of the node is then simplified based on the conditions
* enforced at that then or else branch.
* Otherwise, the current graft is appended to the list.
*
* We only construct else branches if allowed by the user.
*/
static __isl_give isl_ast_graft_list *insert_pending_guard_nodes(
__isl_take isl_ast_graft_list *list,
__isl_keep isl_ast_build *build)
{
int i, j, n_if;
isl_size n;
int allow_else;
isl_ctx *ctx;
isl_ast_graft_list *res;
struct isl_if_node *if_node = NULL;
n = isl_ast_graft_list_n_ast_graft(list);
if (!build || n < 0)
return isl_ast_graft_list_free(list);
ctx = isl_ast_build_get_ctx(build);
allow_else = isl_options_get_ast_build_allow_else(ctx);
n_if = 0;
if (n > 1) {
if_node = isl_alloc_array(ctx, struct isl_if_node, n - 1);
if (!if_node)
return isl_ast_graft_list_free(list);
}
res = isl_ast_graft_list_alloc(ctx, n);
for (i = 0; i < n; ++i) {
isl_set *guard;
isl_ast_graft *graft;
int subset, found_then, found_else;
isl_ast_node *node;
graft = isl_ast_graft_list_get_ast_graft(list, i);
if (!graft)
break;
subset = 0;
found_then = found_else = -1;
if (n_if > 0) {
isl_set *test;
test = isl_set_copy(graft->guard);
test = isl_set_intersect(test,
isl_set_copy(build->domain));
for (j = n_if - 1; j >= 0; --j) {
subset = isl_set_is_subset(test,
if_node[j].guard);
if (subset < 0 || subset) {
found_then = j;
break;
}
if (!allow_else)
continue;
subset = isl_set_is_subset(test,
if_node[j].complement);
if (subset < 0 || subset) {
found_else = j;
break;
}
}
n_if = clear_if_nodes(if_node, j + 1, n_if);
isl_set_free(test);
}
if (subset < 0) {
graft = isl_ast_graft_free(graft);
break;
}
guard = isl_set_copy(graft->guard);
if (found_then >= 0)
graft->guard = isl_set_gist(graft->guard,
isl_set_copy(if_node[found_then].guard));
else if (found_else >= 0)
graft->guard = isl_set_gist(graft->guard,
isl_set_copy(if_node[found_else].complement));
node = graft->node;
if (!graft->guard)
graft = isl_ast_graft_free(graft);
graft = insert_pending_guard_node(graft, build);
if (graft && graft->node != node && i != n - 1) {
isl_set *set;
if_node[n_if].node = graft->node;
if_node[n_if].guard = guard;
if (found_then >= 0)
set = if_node[found_then].guard;
else if (found_else >= 0)
set = if_node[found_else].complement;
else
set = build->domain;
set = isl_set_copy(set);
set = isl_set_subtract(set, isl_set_copy(guard));
if_node[n_if].complement = set;
n_if++;
} else
isl_set_free(guard);
if (!graft)
break;
if (found_then >= 0)
res = extend_then(res, if_node[found_then].node,
graft, build);
else if (found_else >= 0)
res = extend_else(res, if_node[found_else].node,
graft, build);
else
res = isl_ast_graft_list_add(res, graft);
}
if (i < n)
res = isl_ast_graft_list_free(res);
isl_ast_graft_list_free(list);
clear_if_nodes(if_node, 0, n_if);
free(if_node);
return res;
}
/* For each graft in "list",
* insert an if node around graft->node testing the condition encoded
* in graft->guard, assuming graft->guard involves any conditions.
* Subsequently remove the guards from the grafts.
*/
__isl_give isl_ast_graft_list *isl_ast_graft_list_insert_pending_guard_nodes(
__isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
{
int i;
isl_size n;
isl_set *universe;
list = insert_pending_guard_nodes(list, build);
n = isl_ast_graft_list_n_ast_graft(list);
if (n < 0)
return isl_ast_graft_list_free(list);
universe = isl_set_universe(isl_ast_build_get_space(build, 1));
for (i = 0; i < n; ++i) {
isl_ast_graft *graft;
graft = isl_ast_graft_list_get_ast_graft(list, i);
if (!graft)
break;
isl_set_free(graft->guard);
graft->guard = isl_set_copy(universe);
if (!graft->guard)
graft = isl_ast_graft_free(graft);
list = isl_ast_graft_list_set_ast_graft(list, i, graft);
}
isl_set_free(universe);
if (i < n)
return isl_ast_graft_list_free(list);
return list;
}
/* Collect the nodes contained in the grafts in "list" in a node list.
*/
static __isl_give isl_ast_node_list *extract_node_list(
__isl_keep isl_ast_graft_list *list)
{
int i;
isl_size n;
isl_ctx *ctx;
isl_ast_node_list *node_list;
n = isl_ast_graft_list_n_ast_graft(list);
if (n < 0)
return NULL;
ctx = isl_ast_graft_list_get_ctx(list);
node_list = isl_ast_node_list_alloc(ctx, n);
for (i = 0; i < n; ++i) {
isl_ast_node *node;
isl_ast_graft *graft;
graft = isl_ast_graft_list_get_ast_graft(list, i);
node = isl_ast_graft_get_node(graft);
node_list = isl_ast_node_list_add(node_list, node);
isl_ast_graft_free(graft);
}
return node_list;
}
/* Look for shared enforced constraints by all the elements in "list"
* on outer loops (with respect to the current depth) and return the result.
*
* If there are no elements in "list", then return the empty set.
*/
__isl_give isl_basic_set *isl_ast_graft_list_extract_shared_enforced(
__isl_keep isl_ast_graft_list *list,
__isl_keep isl_ast_build *build)
{
int i;
isl_size n;
isl_size depth;
isl_space *space;
isl_basic_set *enforced;
n = isl_ast_graft_list_n_ast_graft(list);
depth = isl_ast_build_get_depth(build);
if (n < 0 || depth < 0)
return NULL;
space = isl_ast_build_get_space(build, 1);
enforced = isl_basic_set_empty(space);
for (i = 0; i < n; ++i) {
isl_ast_graft *graft;
graft = isl_ast_graft_list_get_ast_graft(list, i);
enforced = update_enforced(enforced, graft, depth);
isl_ast_graft_free(graft);
}
return enforced;
}
/* Record "guard" in "graft" so that it will be enforced somewhere
* up the tree. If the graft already has a guard, then it may be partially
* redundant in combination with the new guard and in the context
* the generated constraints of "build". In fact, the new guard
* may in itself have some redundant constraints.
* We therefore (re)compute the gist of the intersection
* and coalesce the result.
*/
static __isl_give isl_ast_graft *store_guard(__isl_take isl_ast_graft *graft,
__isl_take isl_set *guard, __isl_keep isl_ast_build *build)
{
int is_universe;
if (!graft)
goto error;
is_universe = isl_set_plain_is_universe(guard);
if (is_universe < 0)
goto error;
if (is_universe) {
isl_set_free(guard);
return graft;
}
graft->guard = isl_set_intersect(graft->guard, guard);
graft->guard = isl_set_gist(graft->guard,
isl_ast_build_get_generated(build));
graft->guard = isl_set_coalesce(graft->guard);
if (!graft->guard)
return isl_ast_graft_free(graft);
return graft;
error:
isl_set_free(guard);
return isl_ast_graft_free(graft);
}
/* For each graft in "list", replace its guard with the gist with
* respect to "context".
*/
static __isl_give isl_ast_graft_list *gist_guards(
__isl_take isl_ast_graft_list *list, __isl_keep isl_set *context)
{
int i;
isl_size n;
n = isl_ast_graft_list_n_ast_graft(list);
if (!list)
return isl_ast_graft_list_free(list);
for (i = 0; i < n; ++i) {
isl_ast_graft *graft;
graft = isl_ast_graft_list_get_ast_graft(list, i);
if (!graft)
break;
graft->guard = isl_set_gist(graft->guard,
isl_set_copy(context));
if (!graft->guard)
graft = isl_ast_graft_free(graft);
list = isl_ast_graft_list_set_ast_graft(list, i, graft);
}
if (i < n)
return isl_ast_graft_list_free(list);
return list;
}
/* For each graft in "list", replace its guard with the gist with
* respect to "context".
*/
__isl_give isl_ast_graft_list *isl_ast_graft_list_gist_guards(
__isl_take isl_ast_graft_list *list, __isl_take isl_set *context)
{
list = gist_guards(list, context);
isl_set_free(context);
return list;
}
/* Allocate a graft in "build" based on the list of grafts in "sub_build".
* "guard" and "enforced" are the guard and enforced constraints
* of the allocated graft. The guard is used to simplify the guards
* of the elements in "list".
*
* The node is initialized to either a block containing the nodes of "children"
* or, if there is only a single child, the node of that child.
* If the current level requires a for node, it should be inserted by
* a subsequent call to isl_ast_graft_insert_for.
*/
__isl_give isl_ast_graft *isl_ast_graft_alloc_from_children(
__isl_take isl_ast_graft_list *list, __isl_take isl_set *guard,
__isl_take isl_basic_set *enforced, __isl_keep isl_ast_build *build,
__isl_keep isl_ast_build *sub_build)
{
isl_ast_build *guard_build;
isl_ast_node *node;
isl_ast_node_list *node_list;
isl_ast_graft *graft;
guard_build = isl_ast_build_copy(sub_build);
guard_build = isl_ast_build_replace_pending_by_guard(guard_build,
isl_set_copy(guard));
list = gist_guards(list, guard);
list = insert_pending_guard_nodes(list, guard_build);
isl_ast_build_free(guard_build);
node_list = extract_node_list(list);
node = isl_ast_node_from_ast_node_list(node_list);
isl_ast_graft_list_free(list);
graft = isl_ast_graft_alloc(node, build);
graft = store_guard(graft, guard, build);
graft = isl_ast_graft_enforce(graft, enforced);
return graft;
}
/* Combine the grafts in the list into a single graft.
*
* The guard is initialized to the shared guard of the list elements (if any),
* provided it does not depend on the current dimension.
* The guards in the elements are then simplified with respect to the
* hoisted guard and materialized as if nodes around the contained AST nodes
* in the context of "sub_build".
*
* The enforced set is initialized to the simple hull of the enforced sets
* of the elements, provided the ast_build_exploit_nested_bounds option is set
* or the new graft will be used at the same level.
*
* The node is initialized to either a block containing the nodes of "list"
* or, if there is only a single element, the node of that element.
*/
static __isl_give isl_ast_graft *ast_graft_list_fuse(
__isl_take isl_ast_graft_list *list, __isl_keep isl_ast_build *build)
{
isl_ast_graft *graft;
isl_basic_set *enforced;
isl_set *guard;
if (!list)
return NULL;
enforced = isl_ast_graft_list_extract_shared_enforced(list, build);
guard = isl_ast_graft_list_extract_hoistable_guard(list, build);
graft = isl_ast_graft_alloc_from_children(list, guard, enforced,
build, build);
return graft;
}
/* Combine the grafts in the list into a single graft.
* Return a list containing this single graft.
* If the original list is empty, then return an empty list.
*/
__isl_give isl_ast_graft_list *isl_ast_graft_list_fuse(
__isl_take isl_ast_graft_list *list,
__isl_keep isl_ast_build *build)
{
isl_size n;
isl_ast_graft *graft;
n = isl_ast_graft_list_n_ast_graft(list);
if (n < 0)
return isl_ast_graft_list_free(list);
if (n <= 1)
return list;
graft = ast_graft_list_fuse(list, build);
return isl_ast_graft_list_from_ast_graft(graft);
}
/* Combine the two grafts into a single graft.
* Return a list containing this single graft.
*/
static __isl_give isl_ast_graft *isl_ast_graft_fuse(
__isl_take isl_ast_graft *graft1, __isl_take isl_ast_graft *graft2,
__isl_keep isl_ast_build *build)
{
isl_ctx *ctx;
isl_ast_graft_list *list;
ctx = isl_ast_build_get_ctx(build);
list = isl_ast_graft_list_alloc(ctx, 2);
list = isl_ast_graft_list_add(list, graft1);
list = isl_ast_graft_list_add(list, graft2);
return ast_graft_list_fuse(list, build);
}
/* Insert a for node enclosing the current graft->node.
*/
__isl_give isl_ast_graft *isl_ast_graft_insert_for(
__isl_take isl_ast_graft *graft, __isl_take isl_ast_node *node)
{
if (!graft)
goto error;
graft->node = isl_ast_node_for_set_body(node, graft->node);
if (!graft->node)
return isl_ast_graft_free(graft);
return graft;
error:
isl_ast_node_free(node);
isl_ast_graft_free(graft);
return NULL;
}
/* Insert a mark governing the current graft->node.
*/
__isl_give isl_ast_graft *isl_ast_graft_insert_mark(
__isl_take isl_ast_graft *graft, __isl_take isl_id *mark)
{
if (!graft)
goto error;