-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtree2scop.c
2787 lines (2445 loc) · 85.8 KB
/
tree2scop.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 2011 Leiden University. All rights reserved.
* Copyright 2012-2014 Ecole Normale Superieure. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Leiden University.
*/
#include <stdlib.h>
#include <string.h>
#include <isl/id.h>
#include <isl/val.h>
#include <isl/space.h>
#include <isl/local_space.h>
#include <isl/aff.h>
#include <isl/id_to_pw_aff.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include "aff.h"
#include "expr.h"
#include "expr_arg.h"
#include "nest.h"
#include "scop.h"
#include "skip.h"
#include "state.h"
#include "tree2scop.h"
/* If "stmt" is an affine assumption, then record the assumption in "pc".
*/
static __isl_give pet_context *add_affine_assumption(struct pet_stmt *stmt,
__isl_take pet_context *pc)
{
isl_bool affine;
isl_set *cond;
affine = pet_stmt_is_affine_assume(stmt);
if (affine < 0)
return pet_context_free(pc);
if (!affine)
return pc;
cond = pet_stmt_assume_get_affine_condition(stmt);
cond = isl_set_reset_tuple_id(cond);
pc = pet_context_intersect_domain(pc, cond);
return pc;
}
/* Given a scop "scop" derived from an assumption statement,
* record the assumption in "pc", if it is affine.
* Note that "scop" should consist of exactly one statement.
*/
static __isl_give pet_context *scop_add_affine_assumption(
__isl_keep pet_scop *scop, __isl_take pet_context *pc)
{
int i;
if (!scop)
return pet_context_free(pc);
for (i = 0; i < scop->n_stmt; ++i)
pc = add_affine_assumption(scop->stmts[i], pc);
return pc;
}
/* Update "pc" by taking into account the writes in "stmt".
* That is, clear any previously assigned values to variables
* that are written by "stmt".
*/
static __isl_give pet_context *handle_writes(struct pet_stmt *stmt,
__isl_take pet_context *pc)
{
return pet_context_clear_writes_in_tree(pc, stmt->body);
}
/* Update "pc" based on the write accesses in "scop".
*/
static __isl_give pet_context *scop_handle_writes(struct pet_scop *scop,
__isl_take pet_context *pc)
{
int i;
if (!scop)
return pet_context_free(pc);
for (i = 0; i < scop->n_stmt; ++i)
pc = handle_writes(scop->stmts[i], pc);
return pc;
}
/* Wrapper around pet_expr_resolve_assume
* for use as a callback to pet_tree_map_expr.
*/
static __isl_give pet_expr *resolve_assume(__isl_take pet_expr *expr,
void *user)
{
pet_context *pc = user;
return pet_expr_resolve_assume(expr, pc);
}
/* Check if any expression inside "tree" is an assume expression and
* if its single argument can be converted to an affine expression
* in the context of "pc".
* If so, replace the argument by the affine expression.
*/
__isl_give pet_tree *pet_tree_resolve_assume(__isl_take pet_tree *tree,
__isl_keep pet_context *pc)
{
return pet_tree_map_expr(tree, &resolve_assume, pc);
}
/* Convert a pet_tree to a pet_scop with one statement within the context "pc".
* "tree" has already been evaluated in the context of "pc".
* This mainly involves resolving nested expression parameters
* and setting the name of the iteration space.
* The name is given by tree->label if it is non-NULL. Otherwise,
* it is of the form S_<stmt_nr>.
*/
static struct pet_scop *scop_from_evaluated_tree(__isl_take pet_tree *tree,
int stmt_nr, __isl_keep pet_context *pc)
{
isl_space *space;
isl_set *domain;
struct pet_stmt *ps;
space = pet_context_get_space(pc);
tree = pet_tree_resolve_nested(tree, space);
tree = pet_tree_resolve_assume(tree, pc);
domain = pet_context_get_domain(pc);
ps = pet_stmt_from_pet_tree(domain, stmt_nr, tree);
return pet_scop_from_pet_stmt(space, ps);
}
/* Convert a top-level pet_expr to a pet_scop with one statement
* within the context "pc".
* "expr" has already been evaluated in the context of "pc".
* We construct a pet_tree from "expr" and continue with
* scop_from_evaluated_tree.
* The name is of the form S_<stmt_nr>.
* The location of the statement is set to "loc".
*/
static struct pet_scop *scop_from_evaluated_expr(__isl_take pet_expr *expr,
int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
{
pet_tree *tree;
tree = pet_tree_new_expr(expr);
tree = pet_tree_set_loc(tree, loc);
return scop_from_evaluated_tree(tree, stmt_nr, pc);
}
/* Convert a pet_tree to a pet_scop with one statement within the context "pc".
* "tree" has not yet been evaluated in the context of "pc".
* We evaluate "tree" in the context of "pc" and continue with
* scop_from_evaluated_tree.
* The statement name is given by tree->label if it is non-NULL. Otherwise,
* it is of the form S_<stmt_nr>.
*/
static struct pet_scop *scop_from_unevaluated_tree(__isl_take pet_tree *tree,
int stmt_nr, __isl_keep pet_context *pc)
{
tree = pet_context_evaluate_tree(pc, tree);
return scop_from_evaluated_tree(tree, stmt_nr, pc);
}
/* Convert a top-level pet_expr to a pet_scop with one statement
* within the context "pc", where "expr" has not yet been evaluated
* in the context of "pc".
* We construct a pet_tree from "expr" and continue with
* scop_from_unevaluated_tree.
* The statement name is of the form S_<stmt_nr>.
* The location of the statement is set to "loc".
*/
static struct pet_scop *scop_from_expr(__isl_take pet_expr *expr,
int stmt_nr, __isl_take pet_loc *loc, __isl_keep pet_context *pc)
{
pet_tree *tree;
tree = pet_tree_new_expr(expr);
tree = pet_tree_set_loc(tree, loc);
return scop_from_unevaluated_tree(tree, stmt_nr, pc);
}
/* Construct a pet_scop with a single statement killing the entire
* array "array".
* The location of the statement is set to "loc".
*/
static struct pet_scop *kill(__isl_take pet_loc *loc, struct pet_array *array,
__isl_keep pet_context *pc, struct pet_state *state)
{
isl_ctx *ctx;
isl_id *id;
isl_space *space;
isl_multi_pw_aff *index;
isl_map *access;
pet_expr *expr;
if (!array)
goto error;
ctx = isl_set_get_ctx(array->extent);
access = isl_map_from_range(isl_set_copy(array->extent));
id = isl_set_get_tuple_id(array->extent);
space = isl_space_alloc(ctx, 0, 0, 0);
space = isl_space_set_tuple_id(space, isl_dim_out, id);
index = isl_multi_pw_aff_zero(space);
expr = pet_expr_kill_from_access_and_index(access, index);
return scop_from_expr(expr, state->n_stmt++, loc, pc);
error:
pet_loc_free(loc);
return NULL;
}
/* Construct and return a pet_array corresponding to the variable
* accessed by "access" by calling the extract_array callback.
*/
static struct pet_array *extract_array(__isl_keep pet_expr *access,
__isl_keep pet_context *pc, struct pet_state *state)
{
return state->extract_array(access, pc, state->user);
}
/* Construct a pet_scop for a (single) variable declaration
* within the context "pc".
*
* The scop contains the variable being declared (as an array)
* and a statement killing the array.
*
* If the declaration comes with an initialization, then the scop
* also contains an assignment to the variable.
*/
static struct pet_scop *scop_from_decl(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state)
{
int type_size;
isl_ctx *ctx;
struct pet_array *array;
struct pet_scop *scop_decl, *scop;
pet_expr *lhs, *rhs, *pe;
array = extract_array(tree->u.d.var, pc, state);
if (array)
array->declared = 1;
scop_decl = kill(pet_tree_get_loc(tree), array, pc, state);
scop_decl = pet_scop_add_array(scop_decl, array);
if (tree->type != pet_tree_decl_init)
return scop_decl;
lhs = pet_expr_copy(tree->u.d.var);
rhs = pet_expr_copy(tree->u.d.init);
type_size = pet_expr_get_type_size(lhs);
pe = pet_expr_new_binary(type_size, pet_op_assign, lhs, rhs);
scop = scop_from_expr(pe, state->n_stmt++, pet_tree_get_loc(tree), pc);
ctx = pet_tree_get_ctx(tree);
scop = pet_scop_add_seq(ctx, scop_decl, scop);
return scop;
}
/* Does "tree" represent a kill statement?
* That is, is it an expression statement that "calls" __pencil_kill?
*/
static int is_pencil_kill(__isl_keep pet_tree *tree)
{
pet_expr *expr;
const char *name;
if (!tree)
return -1;
if (tree->type != pet_tree_expr)
return 0;
expr = tree->u.e.expr;
if (pet_expr_get_type(expr) != pet_expr_call)
return 0;
name = pet_expr_call_get_name(expr);
if (!name)
return -1;
return !strcmp(name, "__pencil_kill");
}
/* Add a kill to "scop" that kills what is accessed by
* the access expression "expr".
*
* Mark the access as a write prior to evaluation to avoid
* the access being replaced by a possible known value
* during the evaluation.
*
* If the access expression has any arguments (after evaluation
* in the context of "pc"), then we ignore it, since we cannot
* tell which elements are definitely killed.
*
* Otherwise, we extend the index expression to the dimension
* of the accessed array and intersect with the extent of the array and
* add a kill expression that kills these array elements is added to "scop".
*/
static struct pet_scop *scop_add_kill(struct pet_scop *scop,
__isl_take pet_expr *expr, __isl_take pet_loc *loc,
__isl_keep pet_context *pc, struct pet_state *state)
{
int dim1, dim2;
isl_id *id;
isl_multi_pw_aff *index;
isl_map *map;
pet_expr *kill;
struct pet_array *array;
struct pet_scop *scop_i;
expr = pet_expr_access_set_write(expr, 1);
expr = pet_context_evaluate_expr(pc, expr);
if (!expr)
goto error;
if (expr->n_arg != 0) {
pet_loc_free(loc);
pet_expr_free(expr);
return scop;
}
array = extract_array(expr, pc, state);
if (!array)
goto error;
index = pet_expr_access_get_index(expr);
pet_expr_free(expr);
map = isl_map_from_multi_pw_aff(isl_multi_pw_aff_copy(index));
id = isl_map_get_tuple_id(map, isl_dim_out);
dim1 = isl_set_dim(array->extent, isl_dim_set);
dim2 = isl_map_dim(map, isl_dim_out);
map = isl_map_add_dims(map, isl_dim_out, dim1 - dim2);
map = isl_map_set_tuple_id(map, isl_dim_out, id);
map = isl_map_intersect_range(map, isl_set_copy(array->extent));
pet_array_free(array);
kill = pet_expr_kill_from_access_and_index(map, index);
scop_i = scop_from_evaluated_expr(kill, state->n_stmt++, loc, pc);
scop = pet_scop_add_par(state->ctx, scop, scop_i);
return scop;
error:
pet_expr_free(expr);
pet_loc_free(loc);
return pet_scop_free(scop);
}
/* For each argument of the __pencil_kill call in "tree" that
* represents an access, add a kill statement to "scop" killing the accessed
* elements.
*/
static struct pet_scop *scop_from_pencil_kill(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state)
{
pet_expr *call;
struct pet_scop *scop;
int i, n;
call = tree->u.e.expr;
scop = pet_scop_empty(pet_context_get_space(pc));
n = pet_expr_get_n_arg(call);
for (i = 0; i < n; ++i) {
pet_expr *arg;
pet_loc *loc;
arg = pet_expr_get_arg(call, i);
if (!arg)
return pet_scop_free(scop);
if (pet_expr_get_type(arg) != pet_expr_access) {
pet_expr_free(arg);
continue;
}
loc = pet_tree_get_loc(tree);
scop = scop_add_kill(scop, arg, loc, pc, state);
}
return scop;
}
/* Construct a pet_scop for an expression statement within the context "pc".
*
* If the expression calls __pencil_kill, then it needs to be converted
* into zero or more kill statements.
* Otherwise, a scop is extracted directly from the tree.
*/
static struct pet_scop *scop_from_tree_expr(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state)
{
int is_kill;
is_kill = is_pencil_kill(tree);
if (is_kill < 0)
return NULL;
if (is_kill)
return scop_from_pencil_kill(tree, pc, state);
return scop_from_unevaluated_tree(pet_tree_copy(tree),
state->n_stmt++, pc);
}
/* Construct a pet_scop for a return statement within the context "pc".
*/
static struct pet_scop *scop_from_return(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state)
{
return scop_from_unevaluated_tree(pet_tree_copy(tree),
state->n_stmt++, pc);
}
/* Return those elements in the space of "cond" that come after
* (based on "sign") an element in "cond" in the final dimension.
*/
static __isl_give isl_set *after(__isl_take isl_set *cond, int sign)
{
isl_space *space;
isl_map *previous_to_this;
int i, dim;
dim = isl_set_dim(cond, isl_dim_set);
space = isl_space_map_from_set(isl_set_get_space(cond));
previous_to_this = isl_map_universe(space);
for (i = 0; i + 1 < dim; ++i)
previous_to_this = isl_map_equate(previous_to_this,
isl_dim_in, i, isl_dim_out, i);
if (sign > 0)
previous_to_this = isl_map_order_lt(previous_to_this,
isl_dim_in, dim - 1, isl_dim_out, dim - 1);
else
previous_to_this = isl_map_order_gt(previous_to_this,
isl_dim_in, dim - 1, isl_dim_out, dim - 1);
cond = isl_set_apply(cond, previous_to_this);
return cond;
}
/* Remove those iterations of "domain" that have an earlier iteration
* (based on "sign") in the final dimension where "skip" is satisfied.
* If "apply_skip_map" is set, then "skip_map" is first applied
* to the embedded skip condition before removing it from the domain.
*/
static __isl_give isl_set *apply_affine_break(__isl_take isl_set *domain,
__isl_take isl_set *skip, int sign,
int apply_skip_map, __isl_keep isl_map *skip_map)
{
if (apply_skip_map)
skip = isl_set_apply(skip, isl_map_copy(skip_map));
skip = isl_set_intersect(skip , isl_set_copy(domain));
return isl_set_subtract(domain, after(skip, sign));
}
/* Create a single-dimensional multi-affine expression on the domain space
* of "pc" that is equal to the final dimension of this domain.
* "loop_nr" is the sequence number of the corresponding loop.
* If "id" is not NULL, then it is used as the output tuple name.
* Otherwise, the name is constructed as L_<loop_nr>.
*/
static __isl_give isl_multi_aff *map_to_last(__isl_keep pet_context *pc,
int loop_nr, __isl_keep isl_id *id)
{
int pos;
isl_space *space;
isl_local_space *ls;
isl_aff *aff;
isl_multi_aff *ma;
char name[50];
isl_id *label;
space = pet_context_get_space(pc);
pos = isl_space_dim(space, isl_dim_set) - 1;
ls = isl_local_space_from_space(space);
aff = isl_aff_var_on_domain(ls, isl_dim_set, pos);
ma = isl_multi_aff_from_aff(aff);
if (id) {
label = isl_id_copy(id);
} else {
snprintf(name, sizeof(name), "L_%d", loop_nr);
label = isl_id_alloc(pet_context_get_ctx(pc), name, NULL);
}
ma = isl_multi_aff_set_tuple_id(ma, isl_dim_out, label);
return ma;
}
/* Create an affine expression that maps elements
* of an array "id_test" to the previous element in the final dimension
* (according to "inc"), provided this element belongs to "domain".
* That is, create the affine expression
*
* { id[outer,x] -> id[outer,x - inc] : (outer,x - inc) in domain }
*/
static __isl_give isl_multi_pw_aff *map_to_previous(__isl_take isl_id *id_test,
__isl_take isl_set *domain, __isl_take isl_val *inc)
{
int pos;
isl_space *space;
isl_aff *aff;
isl_pw_aff *pa;
isl_multi_aff *ma;
isl_multi_pw_aff *prev;
pos = isl_set_dim(domain, isl_dim_set) - 1;
space = isl_set_get_space(domain);
space = isl_space_map_from_set(space);
ma = isl_multi_aff_identity(space);
aff = isl_multi_aff_get_aff(ma, pos);
aff = isl_aff_add_constant_val(aff, isl_val_neg(inc));
ma = isl_multi_aff_set_aff(ma, pos, aff);
domain = isl_set_preimage_multi_aff(domain, isl_multi_aff_copy(ma));
prev = isl_multi_pw_aff_from_multi_aff(ma);
pa = isl_multi_pw_aff_get_pw_aff(prev, pos);
pa = isl_pw_aff_intersect_domain(pa, domain);
prev = isl_multi_pw_aff_set_pw_aff(prev, pos, pa);
prev = isl_multi_pw_aff_set_tuple_id(prev, isl_dim_out, id_test);
return prev;
}
/* Add an implication to "scop" expressing that if an element of
* virtual array "id_test" has value "satisfied" then all previous elements
* of this array (in the final dimension) also have that value.
* The set of previous elements is bounded by "domain".
* If "sign" is negative then the iterator
* is decreasing and we express that all subsequent array elements
* (but still defined previously) have the same value.
*/
static struct pet_scop *add_implication(struct pet_scop *scop,
__isl_take isl_id *id_test, __isl_take isl_set *domain, int sign,
int satisfied)
{
int i, dim;
isl_space *space;
isl_map *map;
dim = isl_set_dim(domain, isl_dim_set);
domain = isl_set_set_tuple_id(domain, id_test);
space = isl_space_map_from_set(isl_set_get_space(domain));
map = isl_map_universe(space);
for (i = 0; i + 1 < dim; ++i)
map = isl_map_equate(map, isl_dim_in, i, isl_dim_out, i);
if (sign > 0)
map = isl_map_order_ge(map,
isl_dim_in, dim - 1, isl_dim_out, dim - 1);
else
map = isl_map_order_le(map,
isl_dim_in, dim - 1, isl_dim_out, dim - 1);
map = isl_map_intersect_range(map, domain);
scop = pet_scop_add_implication(scop, map, satisfied);
return scop;
}
/* Add a filter to "scop" that imposes that it is only executed
* when the variable identified by "id_test" has a zero value
* for all previous iterations of "domain".
*
* In particular, add a filter that imposes that the array
* has a zero value at the previous iteration of domain and
* add an implication that implies that it then has that
* value for all previous iterations.
*/
static struct pet_scop *scop_add_break(struct pet_scop *scop,
__isl_take isl_id *id_test, __isl_take isl_set *domain,
__isl_take isl_val *inc)
{
isl_multi_pw_aff *prev;
int sign = isl_val_sgn(inc);
prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
scop = add_implication(scop, id_test, domain, sign, 0);
scop = pet_scop_filter(scop, prev, 0);
return scop;
}
static struct pet_scop *scop_from_tree(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state);
/* Construct a pet_scop for an infinite loop around the given body
* within the context "pc".
* "loop_id" is the label on the loop or NULL if there is no such label.
*
* The domain of "pc" has already been extended with an infinite loop
*
* { [t] : t >= 0 }
*
* We extract a pet_scop for the body and then embed it in a loop with
* schedule
*
* { [outer,t] -> [t] }
*
* If the body contains any break, then it is taken into
* account in apply_affine_break (if the skip condition is affine)
* or in scop_add_break (if the skip condition is not affine).
*
* Note that in case of an affine skip condition,
* since we are dealing with a loop without loop iterator,
* the skip condition cannot refer to the current loop iterator and
* so effectively, the effect on the iteration domain is of the form
*
* { [outer,0]; [outer,t] : t >= 1 and not skip }
*/
static struct pet_scop *scop_from_infinite_loop(__isl_keep pet_tree *body,
__isl_keep isl_id *loop_id, __isl_keep pet_context *pc,
struct pet_state *state)
{
isl_ctx *ctx;
isl_id *id_test;
isl_set *domain;
isl_set *skip;
isl_multi_aff *sched;
struct pet_scop *scop;
int has_affine_break;
int has_var_break;
ctx = pet_tree_get_ctx(body);
domain = pet_context_get_domain(pc);
sched = map_to_last(pc, state->n_loop++, loop_id);
scop = scop_from_tree(body, pc, state);
has_affine_break = pet_scop_has_affine_skip(scop, pet_skip_later);
if (has_affine_break)
skip = pet_scop_get_affine_skip_domain(scop, pet_skip_later);
has_var_break = pet_scop_has_var_skip(scop, pet_skip_later);
if (has_var_break)
id_test = pet_scop_get_skip_id(scop, pet_skip_later);
scop = pet_scop_reset_skips(scop);
scop = pet_scop_embed(scop, isl_set_copy(domain), sched);
if (has_affine_break) {
domain = apply_affine_break(domain, skip, 1, 0, NULL);
scop = pet_scop_intersect_domain_prefix(scop,
isl_set_copy(domain));
}
if (has_var_break)
scop = scop_add_break(scop, id_test, domain, isl_val_one(ctx));
else
isl_set_free(domain);
return scop;
}
/* Construct a pet_scop for an infinite loop, i.e., a loop of the form
*
* for (;;)
* body
*
* within the context "pc".
*
* Extend the domain of "pc" with an extra inner loop
*
* { [t] : t >= 0 }
*
* and construct the scop in scop_from_infinite_loop.
*/
static struct pet_scop *scop_from_infinite_for(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state)
{
struct pet_scop *scop;
pc = pet_context_copy(pc);
pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
pc = pet_context_add_infinite_loop(pc);
scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
pet_context_free(pc);
return scop;
}
/* Construct a pet_scop for a while loop of the form
*
* while (pa)
* body
*
* within the context "pc".
*
* The domain of "pc" has already been extended with an infinite loop
*
* { [t] : t >= 0 }
*
* Here, we add the constraints on the outer loop iterators
* implied by "pa" and construct the scop in scop_from_infinite_loop.
* Note that the intersection with these constraints
* may result in an empty loop.
*/
static struct pet_scop *scop_from_affine_while(__isl_keep pet_tree *tree,
__isl_take isl_pw_aff *pa, __isl_take pet_context *pc,
struct pet_state *state)
{
struct pet_scop *scop;
isl_set *dom, *local;
isl_set *valid;
valid = isl_pw_aff_domain(isl_pw_aff_copy(pa));
dom = isl_pw_aff_non_zero_set(pa);
local = isl_set_add_dims(isl_set_copy(dom), isl_dim_set, 1);
pc = pet_context_intersect_domain(pc, local);
scop = scop_from_infinite_loop(tree->u.l.body, tree->label, pc, state);
scop = pet_scop_restrict(scop, dom);
scop = pet_scop_restrict_context(scop, valid);
pet_context_free(pc);
return scop;
}
/* Construct a scop for a while, given the scops for the condition
* and the body, the filter identifier and the iteration domain of
* the while loop.
*
* In particular, the scop for the condition is filtered to depend
* on "id_test" evaluating to true for all previous iterations
* of the loop, while the scop for the body is filtered to depend
* on "id_test" evaluating to true for all iterations up to the
* current iteration.
* The actual filter only imposes that this virtual array has
* value one on the previous or the current iteration.
* The fact that this condition also applies to the previous
* iterations is enforced by an implication.
*
* These filtered scops are then combined into a single scop,
* with the condition scop scheduled before the body scop.
*
* "sign" is positive if the iterator increases and negative
* if it decreases.
*/
static struct pet_scop *scop_add_while(struct pet_scop *scop_cond,
struct pet_scop *scop_body, __isl_take isl_id *id_test,
__isl_take isl_set *domain, __isl_take isl_val *inc)
{
isl_ctx *ctx = isl_set_get_ctx(domain);
isl_space *space;
isl_multi_pw_aff *test_index;
isl_multi_pw_aff *prev;
int sign = isl_val_sgn(inc);
struct pet_scop *scop;
prev = map_to_previous(isl_id_copy(id_test), isl_set_copy(domain), inc);
scop_cond = pet_scop_filter(scop_cond, prev, 1);
space = isl_space_map_from_set(isl_set_get_space(domain));
test_index = isl_multi_pw_aff_identity(space);
test_index = isl_multi_pw_aff_set_tuple_id(test_index, isl_dim_out,
isl_id_copy(id_test));
scop_body = pet_scop_filter(scop_body, test_index, 1);
scop = pet_scop_add_seq(ctx, scop_cond, scop_body);
scop = add_implication(scop, id_test, domain, sign, 1);
return scop;
}
/* Create a pet_scop with a single statement with name S_<stmt_nr>,
* evaluating "cond" and writing the result to a virtual scalar,
* as expressed by "index".
* The expression "cond" has not yet been evaluated in the context of "pc".
* Do so within the context "pc".
* The location of the statement is set to "loc".
*/
static struct pet_scop *scop_from_non_affine_condition(
__isl_take pet_expr *cond, int stmt_nr,
__isl_take isl_multi_pw_aff *index,
__isl_take pet_loc *loc, __isl_keep pet_context *pc)
{
pet_expr *expr, *write;
cond = pet_context_evaluate_expr(pc, cond);
write = pet_expr_from_index(index);
write = pet_expr_access_set_write(write, 1);
write = pet_expr_access_set_read(write, 0);
expr = pet_expr_new_binary(1, pet_op_assign, write, cond);
return scop_from_evaluated_expr(expr, stmt_nr, loc, pc);
}
/* Given that "scop" has an affine skip condition of type pet_skip_now,
* apply this skip condition to the domain of "pc".
* That is, remove the elements satisfying the skip condition from
* the domain of "pc".
*/
static __isl_give pet_context *apply_affine_continue(__isl_take pet_context *pc,
struct pet_scop *scop)
{
isl_set *domain, *skip;
skip = pet_scop_get_affine_skip_domain(scop, pet_skip_now);
domain = pet_context_get_domain(pc);
domain = isl_set_subtract(domain, skip);
pc = pet_context_intersect_domain(pc, domain);
return pc;
}
/* Add a scop for evaluating the loop increment "inc" at the end
* of a loop body "scop" within the context "pc".
*
* The skip conditions resulting from continue statements inside
* the body do not apply to "inc", but those resulting from break
* statements do need to get applied.
*/
static struct pet_scop *scop_add_inc(struct pet_scop *scop,
__isl_take pet_expr *inc, __isl_take pet_loc *loc,
__isl_keep pet_context *pc, struct pet_state *state)
{
struct pet_scop *scop_inc;
pc = pet_context_copy(pc);
if (pet_scop_has_skip(scop, pet_skip_later)) {
isl_multi_pw_aff *skip;
skip = pet_scop_get_skip(scop, pet_skip_later);
scop = pet_scop_set_skip(scop, pet_skip_now, skip);
if (pet_scop_has_affine_skip(scop, pet_skip_now))
pc = apply_affine_continue(pc, scop);
} else
pet_scop_reset_skip(scop, pet_skip_now);
scop_inc = scop_from_expr(inc, state->n_stmt++, loc, pc);
scop = pet_scop_add_seq(state->ctx, scop, scop_inc);
pet_context_free(pc);
return scop;
}
/* Construct a generic while scop, with iteration domain
* { [t] : t >= 0 } around the scop for "tree_body" within the context "pc".
* "loop_id" is the label on the loop or NULL if there is no such label.
* The domain of "pc" has already been extended with this infinite loop
*
* { [t] : t >= 0 }
*
* The scop consists of two parts,
* one for evaluating the condition "cond" and one for the body.
* If "expr_inc" is not NULL, then a scop for evaluating this expression
* is added at the end of the body,
* after replacing any skip conditions resulting from continue statements
* by the skip conditions resulting from break statements (if any).
*
* The schedules are combined as a sequence to reflect that the condition is
* evaluated before the body is executed and the body is filtered to depend
* on the result of the condition evaluating to true on all iterations
* up to the current iteration, while the evaluation of the condition itself
* is filtered to depend on the result of the condition evaluating to true
* on all previous iterations.
* The context of the scop representing the body is dropped
* because we don't know how many times the body will be executed,
* if at all.
*
* If the body contains any break, then it is taken into
* account in apply_affine_break (if the skip condition is affine)
* or in scop_add_break (if the skip condition is not affine).
*
* Note that in case of an affine skip condition,
* since we are dealing with a loop without loop iterator,
* the skip condition cannot refer to the current loop iterator and
* so effectively, the effect on the iteration domain is of the form
*
* { [outer,0]; [outer,t] : t >= 1 and not skip }
*/
static struct pet_scop *scop_from_non_affine_while(__isl_take pet_expr *cond,
__isl_take pet_loc *loc, __isl_keep pet_tree *tree_body,
__isl_keep isl_id *loop_id, __isl_take pet_expr *expr_inc,
__isl_take pet_context *pc, struct pet_state *state)
{
isl_ctx *ctx;
isl_id *id_test, *id_break_test;
isl_space *space;
isl_multi_pw_aff *test_index;
isl_set *domain;
isl_set *skip;
isl_multi_aff *sched;
struct pet_scop *scop, *scop_body;
int has_affine_break;
int has_var_break;
ctx = state->ctx;
space = pet_context_get_space(pc);
test_index = pet_create_test_index(space, state->n_test++);
scop = scop_from_non_affine_condition(cond, state->n_stmt++,
isl_multi_pw_aff_copy(test_index),
pet_loc_copy(loc), pc);
id_test = isl_multi_pw_aff_get_tuple_id(test_index, isl_dim_out);
domain = pet_context_get_domain(pc);
scop = pet_scop_add_boolean_array(scop, isl_set_copy(domain),
test_index, state->int_size);
sched = map_to_last(pc, state->n_loop++, loop_id);
scop_body = scop_from_tree(tree_body, pc, state);
has_affine_break = pet_scop_has_affine_skip(scop_body, pet_skip_later);
if (has_affine_break)
skip = pet_scop_get_affine_skip_domain(scop_body,
pet_skip_later);
has_var_break = pet_scop_has_var_skip(scop_body, pet_skip_later);
if (has_var_break)
id_break_test = pet_scop_get_skip_id(scop_body, pet_skip_later);
scop_body = pet_scop_reset_context(scop_body);
if (expr_inc)
scop_body = scop_add_inc(scop_body, expr_inc, loc, pc, state);
else
pet_loc_free(loc);
scop_body = pet_scop_reset_skips(scop_body);
if (has_affine_break) {
domain = apply_affine_break(domain, skip, 1, 0, NULL);
scop = pet_scop_intersect_domain_prefix(scop,
isl_set_copy(domain));
scop_body = pet_scop_intersect_domain_prefix(scop_body,
isl_set_copy(domain));
}
if (has_var_break) {
scop = scop_add_break(scop, isl_id_copy(id_break_test),
isl_set_copy(domain), isl_val_one(ctx));
scop_body = scop_add_break(scop_body, id_break_test,
isl_set_copy(domain), isl_val_one(ctx));
}
scop = scop_add_while(scop, scop_body, id_test, isl_set_copy(domain),
isl_val_one(ctx));
scop = pet_scop_embed(scop, domain, sched);
pet_context_free(pc);
return scop;
}
/* Check if the while loop is of the form
*
* while (affine expression)
* body
*
* If so, call scop_from_affine_while to construct a scop.
*
* Otherwise, pass control to scop_from_non_affine_while.
*
* "pc" is the context in which the affine expressions in the scop are created.
* The domain of "pc" is extended with an infinite loop
*
* { [t] : t >= 0 }
*
* before passing control to scop_from_affine_while or
* scop_from_non_affine_while.
*/
static struct pet_scop *scop_from_while(__isl_keep pet_tree *tree,
__isl_keep pet_context *pc, struct pet_state *state)
{
pet_expr *cond_expr;
isl_pw_aff *pa;
if (!tree)
return NULL;
pc = pet_context_copy(pc);
pc = pet_context_clear_writes_in_tree(pc, tree->u.l.body);
cond_expr = pet_expr_copy(tree->u.l.cond);
cond_expr = pet_context_evaluate_expr(pc, cond_expr);
pa = pet_expr_extract_affine_condition(cond_expr, pc);
pet_expr_free(cond_expr);
pc = pet_context_add_infinite_loop(pc);
if (!pa)
goto error;
if (!isl_pw_aff_involves_nan(pa))
return scop_from_affine_while(tree, pa, pc, state);
isl_pw_aff_free(pa);
return scop_from_non_affine_while(pet_expr_copy(tree->u.l.cond),