-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscop.c
3472 lines (2938 loc) · 91.1 KB
/
scop.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 <string.h>
#include <isl/ctx.h>
#include <isl/id.h>
#include <isl/space.h>
#include <isl/local_space.h>
#include <isl/constraint.h>
#include <isl/val.h>
#include <isl/aff.h>
#include <isl/set.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl/schedule_node.h>
#include "aff.h"
#include "expr.h"
#include "expr_access_type.h"
#include "filter.h"
#include "loc.h"
#include "nest.h"
#include "scop.h"
#include "tree.h"
#include "print.h"
#include "value_bounds.h"
/* pet_scop with extra information that is used during parsing and printing.
*
* In particular, we keep track of conditions under which we want
* to skip the rest of the current loop iteration (skip[pet_skip_now])
* and of conditions under which we want to skip subsequent
* loop iterations (skip[pet_skip_later]).
*
* The conditions are represented as index expressions defined
* over the outer loop iterators. The index expression is either
* a boolean affine expression or an access to a variable, which
* is assumed to attain values zero and one. The condition holds
* if the variable has value one or if the affine expression
* has value one (typically for only part of the domain).
*
* A missing condition (skip[type] == NULL) means that we don't want
* to skip anything.
*
* Additionally, we keep track of the original input file
* inside pet_transform_C_source.
*/
struct pet_scop_ext {
struct pet_scop scop;
isl_multi_pw_aff *skip[2];
FILE *input;
};
/* Construct a pet_stmt with given domain and statement number from a pet_tree.
* The input domain is anonymous and is the same as the domains
* of the access expressions inside "tree".
* These domains are modified to include the name of the statement.
* This name is given by tree->label if it is non-NULL.
* Otherwise, the name is constructed as S_<id>.
*/
struct pet_stmt *pet_stmt_from_pet_tree(__isl_take isl_set *domain,
int id, __isl_take pet_tree *tree)
{
struct pet_stmt *stmt;
isl_ctx *ctx;
isl_id *label;
isl_space *space;
isl_multi_aff *ma;
isl_multi_pw_aff *add_name;
char name[50];
if (!domain || !tree)
goto error;
ctx = pet_tree_get_ctx(tree);
stmt = isl_calloc_type(ctx, struct pet_stmt);
if (!stmt)
goto error;
if (tree->label) {
label = isl_id_copy(tree->label);
} else {
snprintf(name, sizeof(name), "S_%d", id);
label = isl_id_alloc(ctx, name, NULL);
}
domain = isl_set_set_tuple_id(domain, label);
space = isl_set_get_space(domain);
space = pet_nested_remove_from_space(space);
ma = pet_prefix_projection(space, isl_space_dim(space, isl_dim_set));
add_name = isl_multi_pw_aff_from_multi_aff(ma);
tree = pet_tree_update_domain(tree, add_name);
stmt->loc = pet_tree_get_loc(tree);
stmt->domain = domain;
stmt->body = tree;
if (!stmt->domain || !stmt->body)
return pet_stmt_free(stmt);
return stmt;
error:
isl_set_free(domain);
pet_tree_free(tree);
return NULL;
}
void *pet_stmt_free(struct pet_stmt *stmt)
{
int i;
if (!stmt)
return NULL;
pet_loc_free(stmt->loc);
isl_set_free(stmt->domain);
pet_tree_free(stmt->body);
for (i = 0; i < stmt->n_arg; ++i)
pet_expr_free(stmt->args[i]);
free(stmt->args);
free(stmt);
return NULL;
}
/* Return the iteration space of "stmt".
*
* If the statement has arguments, then stmt->domain is a wrapped map
* mapping the iteration domain to the values of the arguments
* for which this statement is executed.
* In this case, we need to extract the domain space of this wrapped map.
*/
__isl_give isl_space *pet_stmt_get_space(struct pet_stmt *stmt)
{
isl_space *space;
if (!stmt)
return NULL;
space = isl_set_get_space(stmt->domain);
if (isl_space_is_wrapping(space))
space = isl_space_domain(isl_space_unwrap(space));
return space;
}
static void stmt_dump(struct pet_stmt *stmt, int indent)
{
int i;
if (!stmt)
return;
fprintf(stderr, "%*s%d\n", indent, "", pet_loc_get_line(stmt->loc));
fprintf(stderr, "%*s", indent, "");
isl_set_dump(stmt->domain);
pet_tree_dump_with_indent(stmt->body, indent);
for (i = 0; i < stmt->n_arg; ++i)
pet_expr_dump_with_indent(stmt->args[i], indent + 2);
}
void pet_stmt_dump(struct pet_stmt *stmt)
{
stmt_dump(stmt, 0);
}
/* Allocate a new pet_type with the given "name" and "definition".
*/
struct pet_type *pet_type_alloc(isl_ctx *ctx, const char *name,
const char *definition)
{
struct pet_type *type;
type = isl_alloc_type(ctx, struct pet_type);
if (!type)
return NULL;
type->name = strdup(name);
type->definition = strdup(definition);
if (!type->name || !type->definition)
return pet_type_free(type);
return type;
}
/* Free "type" and return NULL.
*/
struct pet_type *pet_type_free(struct pet_type *type)
{
if (!type)
return NULL;
free(type->name);
free(type->definition);
free(type);
return NULL;
}
struct pet_array *pet_array_free(struct pet_array *array)
{
if (!array)
return NULL;
isl_set_free(array->context);
isl_set_free(array->extent);
isl_set_free(array->value_bounds);
free(array->element_type);
free(array);
return NULL;
}
void pet_array_dump(struct pet_array *array)
{
if (!array)
return;
isl_set_dump(array->context);
isl_set_dump(array->extent);
isl_set_dump(array->value_bounds);
fprintf(stderr, "%s%s%s\n", array->element_type,
array->element_is_record ? " element-is-record" : "",
array->live_out ? " live-out" : "");
}
/* Alloc a pet_scop structure, with extra room for information that
* is only used during parsing.
*/
struct pet_scop *pet_scop_alloc(isl_ctx *ctx)
{
return &isl_calloc_type(ctx, struct pet_scop_ext)->scop;
}
/* Construct a pet_scop in the given space, with the given schedule and
* room for n statements.
*
* The context is initialized as a universe set in "space".
*
* Since no information on the location is known at this point,
* scop->loc is initialized with pet_loc_dummy.
*/
static struct pet_scop *scop_alloc(__isl_take isl_space *space, int n,
__isl_take isl_schedule *schedule)
{
isl_ctx *ctx;
struct pet_scop *scop;
if (!space || !schedule)
goto error;
ctx = isl_space_get_ctx(space);
scop = pet_scop_alloc(ctx);
if (!scop)
goto error;
scop->context = isl_set_universe(isl_space_copy(space));
scop->context_value = isl_set_universe(isl_space_params(space));
scop->stmts = isl_calloc_array(ctx, struct pet_stmt *, n);
scop->schedule = schedule;
if (!scop->context || !scop->stmts)
return pet_scop_free(scop);
scop->loc = &pet_loc_dummy;
scop->n_stmt = n;
return scop;
error:
isl_space_free(space);
isl_schedule_free(schedule);
return NULL;
}
/* Construct a pet_scop in the given space containing 0 statements
* (and therefore an empty iteration domain).
*/
struct pet_scop *pet_scop_empty(__isl_take isl_space *space)
{
isl_schedule *schedule;
schedule = isl_schedule_empty(isl_space_copy(space));
return scop_alloc(space, 0, schedule);
}
/* Given either an iteration domain or a wrapped map with
* the iteration domain in the domain and some arguments
* in the range, return the iteration domain.
* That is, drop the arguments if there are any.
*/
static __isl_give isl_set *drop_arguments(__isl_take isl_set *domain)
{
if (isl_set_is_wrapping(domain))
domain = isl_map_domain(isl_set_unwrap(domain));
return domain;
}
/* Update "context" with the constraints imposed on the outer iteration
* domain by access expression "expr".
* "context" lives in an anonymous space, while the domain of the access
* relation of "expr" refers to a particular statement.
* This reference therefore needs to be stripped off.
*/
static __isl_give isl_set *access_extract_context(__isl_keep pet_expr *expr,
__isl_take isl_set *context)
{
isl_multi_pw_aff *mpa;
isl_set *domain;
mpa = pet_expr_access_get_index(expr);
domain = drop_arguments(isl_multi_pw_aff_domain(mpa));
domain = isl_set_reset_tuple_id(domain);
context = isl_set_intersect(context, domain);
return context;
}
/* Update "context" with the constraints imposed on the outer iteration
* domain by "expr".
*
* "context" lives in an anonymous space, while the domains of
* the access relations in "expr" refer to a particular statement.
* This reference therefore needs to be stripped off.
*
* If "expr" represents a conditional operator, then a parameter or outer
* iterator value needs to be valid for the condition and
* for at least one of the remaining two arguments.
* If the condition is an affine expression, then we can be a bit more specific.
* The value then has to be valid for the second argument for
* non-zero accesses and valid for the third argument for zero accesses.
*
* If "expr" represents a kill statement, then its argument is the entire
* extent of the array being killed. Do not update "context" based
* on this argument as that would impose constraints that ensure that
* the array is non-empty.
*/
static __isl_give isl_set *expr_extract_context(__isl_keep pet_expr *expr,
__isl_take isl_set *context)
{
int i;
if (expr->type == pet_expr_op && expr->op == pet_op_kill)
return context;
if (expr->type == pet_expr_op && expr->op == pet_op_cond) {
int is_aff;
isl_set *context1, *context2;
is_aff = pet_expr_is_affine(expr->args[0]);
if (is_aff < 0)
goto error;
context = expr_extract_context(expr->args[0], context);
context1 = expr_extract_context(expr->args[1],
isl_set_copy(context));
context2 = expr_extract_context(expr->args[2], context);
if (is_aff) {
isl_multi_pw_aff *mpa;
isl_pw_aff *pa;
isl_set *zero_set;
mpa = pet_expr_access_get_index(expr->args[0]);
pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
isl_multi_pw_aff_free(mpa);
zero_set = drop_arguments(isl_pw_aff_zero_set(pa));
zero_set = isl_set_reset_tuple_id(zero_set);
context1 = isl_set_subtract(context1,
isl_set_copy(zero_set));
context2 = isl_set_intersect(context2, zero_set);
}
context = isl_set_union(context1, context2);
context = isl_set_coalesce(context);
return context;
}
for (i = 0; i < expr->n_arg; ++i)
context = expr_extract_context(expr->args[i], context);
if (expr->type == pet_expr_access)
context = access_extract_context(expr, context);
return context;
error:
isl_set_free(context);
return NULL;
}
/* Is "stmt" an assume statement with an affine assumption?
*/
isl_bool pet_stmt_is_affine_assume(struct pet_stmt *stmt)
{
if (!stmt)
return isl_bool_error;
return pet_tree_is_affine_assume(stmt->body);
}
/* Given an assume statement "stmt" with an access argument,
* return the index expression of the argument.
*/
__isl_give isl_multi_pw_aff *pet_stmt_assume_get_index(struct pet_stmt *stmt)
{
if (!stmt)
return NULL;
return pet_tree_assume_get_index(stmt->body);
}
/* Assuming "stmt" is an assume statement with an affine assumption,
* return the assumption as a set.
*/
__isl_give isl_set *pet_stmt_assume_get_affine_condition(struct pet_stmt *stmt)
{
isl_multi_pw_aff *index;
isl_pw_aff *pa;
index = pet_stmt_assume_get_index(stmt);
pa = isl_multi_pw_aff_get_pw_aff(index, 0);
isl_multi_pw_aff_free(index);
return isl_pw_aff_non_zero_set(pa);
}
/* Update "context" with the constraints imposed on the outer iteration
* domain by "stmt".
*
* If the statement is an assume statement with an affine expression,
* then intersect "context" with that expression.
* Otherwise, if the statement body is an expression tree,
* then intersect "context" with the context of this expression.
* Note that we cannot safely extract a context from subtrees
* of the statement body since we cannot tell when those subtrees
* are executed, if at all.
*/
static __isl_give isl_set *stmt_extract_context(struct pet_stmt *stmt,
__isl_take isl_set *context)
{
int i;
isl_bool affine;
pet_expr *body;
affine = pet_stmt_is_affine_assume(stmt);
if (affine < 0)
return isl_set_free(context);
if (affine) {
isl_set *cond;
cond = pet_stmt_assume_get_affine_condition(stmt);
cond = isl_set_reset_tuple_id(cond);
return isl_set_intersect(context, cond);
}
for (i = 0; i < stmt->n_arg; ++i)
context = expr_extract_context(stmt->args[i], context);
if (pet_tree_get_type(stmt->body) != pet_tree_expr)
return context;
body = pet_tree_expr_get_expr(stmt->body);
context = expr_extract_context(body, context);
pet_expr_free(body);
return context;
}
/* Construct a pet_scop in the given space that contains the given pet_stmt.
* The initial schedule consists of only the iteration domain.
*/
struct pet_scop *pet_scop_from_pet_stmt(__isl_take isl_space *space,
struct pet_stmt *stmt)
{
struct pet_scop *scop;
isl_set *set;
isl_union_set *domain;
isl_schedule *schedule;
if (!stmt) {
isl_space_free(space);
return NULL;
}
set = pet_nested_remove_from_set(isl_set_copy(stmt->domain));
domain = isl_union_set_from_set(set);
schedule = isl_schedule_from_domain(domain);
scop = scop_alloc(space, 1, schedule);
if (!scop)
goto error;
scop->context = stmt_extract_context(stmt, scop->context);
if (!scop->context)
goto error;
scop->stmts[0] = stmt;
scop->loc = pet_loc_copy(stmt->loc);
if (!scop->loc)
return pet_scop_free(scop);
return scop;
error:
pet_stmt_free(stmt);
pet_scop_free(scop);
return NULL;
}
/* Does "mpa" represent an access to an element of an unnamed space, i.e.,
* does it represent an affine expression?
*/
static int multi_pw_aff_is_affine(__isl_keep isl_multi_pw_aff *mpa)
{
int has_id;
has_id = isl_multi_pw_aff_has_tuple_id(mpa, isl_dim_out);
if (has_id < 0)
return -1;
return !has_id;
}
/* Return the piecewise affine expression "set ? 1 : 0" defined on "dom".
*/
static __isl_give isl_pw_aff *indicator_function(__isl_take isl_set *set,
__isl_take isl_set *dom)
{
isl_pw_aff *pa;
pa = isl_set_indicator_function(set);
pa = isl_pw_aff_intersect_domain(pa, dom);
return pa;
}
/* Return "lhs || rhs", defined on the shared definition domain.
*/
static __isl_give isl_pw_aff *pw_aff_or(__isl_take isl_pw_aff *lhs,
__isl_take isl_pw_aff *rhs)
{
isl_set *cond;
isl_set *dom;
dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(lhs)),
isl_pw_aff_domain(isl_pw_aff_copy(rhs)));
cond = isl_set_union(isl_pw_aff_non_zero_set(lhs),
isl_pw_aff_non_zero_set(rhs));
cond = isl_set_coalesce(cond);
return indicator_function(cond, dom);
}
/* Combine ext1->skip[type] and ext2->skip[type] into ext->skip[type].
* ext may be equal to either ext1 or ext2.
*
* The two skips that need to be combined are assumed to be affine expressions.
*
* We need to skip in ext if we need to skip in either ext1 or ext2.
* We don't need to skip in ext if we don't need to skip in both ext1 and ext2.
*/
static struct pet_scop_ext *combine_skips(struct pet_scop_ext *ext,
struct pet_scop_ext *ext1, struct pet_scop_ext *ext2,
enum pet_skip type)
{
isl_pw_aff *skip, *skip1, *skip2;
if (!ext)
return NULL;
if (!ext1->skip[type] && !ext2->skip[type])
return ext;
if (!ext1->skip[type]) {
if (ext == ext2)
return ext;
ext->skip[type] = ext2->skip[type];
ext2->skip[type] = NULL;
return ext;
}
if (!ext2->skip[type]) {
if (ext == ext1)
return ext;
ext->skip[type] = ext1->skip[type];
ext1->skip[type] = NULL;
return ext;
}
if (!multi_pw_aff_is_affine(ext1->skip[type]) ||
!multi_pw_aff_is_affine(ext2->skip[type]))
isl_die(isl_multi_pw_aff_get_ctx(ext1->skip[type]),
isl_error_internal, "can only combine affine skips",
goto error);
skip1 = isl_multi_pw_aff_get_pw_aff(ext1->skip[type], 0);
skip2 = isl_multi_pw_aff_get_pw_aff(ext2->skip[type], 0);
skip = pw_aff_or(skip1, skip2);
isl_multi_pw_aff_free(ext1->skip[type]);
ext1->skip[type] = NULL;
isl_multi_pw_aff_free(ext2->skip[type]);
ext2->skip[type] = NULL;
ext->skip[type] = isl_multi_pw_aff_from_pw_aff(skip);
if (!ext->skip[type])
goto error;
return ext;
error:
pet_scop_free(&ext->scop);
return NULL;
}
/* Combine scop1->skip[type] and scop2->skip[type] into scop->skip[type],
* where type takes on the values pet_skip_now and pet_skip_later.
* scop may be equal to either scop1 or scop2.
*/
static struct pet_scop *scop_combine_skips(struct pet_scop *scop,
struct pet_scop *scop1, struct pet_scop *scop2)
{
struct pet_scop_ext *ext = (struct pet_scop_ext *) scop;
struct pet_scop_ext *ext1 = (struct pet_scop_ext *) scop1;
struct pet_scop_ext *ext2 = (struct pet_scop_ext *) scop2;
ext = combine_skips(ext, ext1, ext2, pet_skip_now);
ext = combine_skips(ext, ext1, ext2, pet_skip_later);
return &ext->scop;
}
/* Update start and end of scop->loc to include the region from "start"
* to "end". In particular, if scop->loc == &pet_loc_dummy, then "scop"
* does not have any offset information yet and we simply take the information
* from "start" and "end". Otherwise, we update loc using "start" and "end".
*/
struct pet_scop *pet_scop_update_start_end(struct pet_scop *scop,
unsigned start, unsigned end)
{
if (!scop)
return NULL;
if (scop->loc == &pet_loc_dummy)
scop->loc = pet_loc_alloc(isl_set_get_ctx(scop->context),
start, end, -1, strdup(""));
else
scop->loc = pet_loc_update_start_end(scop->loc, start, end);
if (!scop->loc)
return pet_scop_free(scop);
return scop;
}
/* Update start and end of scop->loc to include the region identified
* by "loc".
*/
struct pet_scop *pet_scop_update_start_end_from_loc(struct pet_scop *scop,
__isl_keep pet_loc *loc)
{
return pet_scop_update_start_end(scop, pet_loc_get_start(loc),
pet_loc_get_end(loc));
}
/* Replace the location of "scop" by "loc".
*/
struct pet_scop *pet_scop_set_loc(struct pet_scop *scop,
__isl_take pet_loc *loc)
{
if (!scop || !loc)
goto error;
pet_loc_free(scop->loc);
scop->loc = loc;
return scop;
error:
pet_loc_free(loc);
pet_scop_free(scop);
return NULL;
}
/* Does "implication" appear in the list of implications of "scop"?
*/
static int is_known_implication(struct pet_scop *scop,
struct pet_implication *implication)
{
int i;
for (i = 0; i < scop->n_implication; ++i) {
struct pet_implication *pi = scop->implications[i];
int equal;
if (pi->satisfied != implication->satisfied)
continue;
equal = isl_map_is_equal(pi->extension, implication->extension);
if (equal < 0)
return -1;
if (equal)
return 1;
}
return 0;
}
/* Store the concatenation of the implications of "scop1" and "scop2"
* in "scop", removing duplicates (i.e., implications in "scop2" that
* already appear in "scop1").
*/
static struct pet_scop *scop_collect_implications(isl_ctx *ctx,
struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
{
int i, j;
if (!scop)
return NULL;
if (scop2->n_implication == 0) {
scop->n_implication = scop1->n_implication;
scop->implications = scop1->implications;
scop1->n_implication = 0;
scop1->implications = NULL;
return scop;
}
if (scop1->n_implication == 0) {
scop->n_implication = scop2->n_implication;
scop->implications = scop2->implications;
scop2->n_implication = 0;
scop2->implications = NULL;
return scop;
}
scop->implications = isl_calloc_array(ctx, struct pet_implication *,
scop1->n_implication + scop2->n_implication);
if (!scop->implications)
return pet_scop_free(scop);
for (i = 0; i < scop1->n_implication; ++i) {
scop->implications[i] = scop1->implications[i];
scop1->implications[i] = NULL;
}
scop->n_implication = scop1->n_implication;
j = scop1->n_implication;
for (i = 0; i < scop2->n_implication; ++i) {
int known;
known = is_known_implication(scop, scop2->implications[i]);
if (known < 0)
return pet_scop_free(scop);
if (known)
continue;
scop->implications[j++] = scop2->implications[i];
scop2->implications[i] = NULL;
}
scop->n_implication = j;
return scop;
}
/* Combine the offset information of "scop1" and "scop2" into "scop".
*/
static struct pet_scop *scop_combine_start_end(struct pet_scop *scop,
struct pet_scop *scop1, struct pet_scop *scop2)
{
if (scop1->loc != &pet_loc_dummy)
scop = pet_scop_update_start_end_from_loc(scop, scop1->loc);
if (scop2->loc != &pet_loc_dummy)
scop = pet_scop_update_start_end_from_loc(scop, scop2->loc);
return scop;
}
/* Create and return an independence that filters out the dependences
* in "filter" with local variables "local".
*/
static struct pet_independence *new_independence(
__isl_take isl_union_map *filter, __isl_take isl_union_set *local)
{
isl_ctx *ctx;
struct pet_independence *independence;
if (!filter || !local)
goto error;
ctx = isl_union_map_get_ctx(filter);
independence = isl_alloc_type(ctx, struct pet_independence);
if (!independence)
goto error;
independence->filter = filter;
independence->local = local;
return independence;
error:
isl_union_map_free(filter);
isl_union_set_free(local);
return NULL;
}
/* Add an independence that filters out the dependences
* in "filter" with local variables "local" to "scop".
*/
struct pet_scop *pet_scop_add_independence(struct pet_scop *scop,
__isl_take isl_union_map *filter, __isl_take isl_union_set *local)
{
isl_ctx *ctx;
struct pet_independence *independence;
struct pet_independence **independences;
ctx = isl_union_map_get_ctx(filter);
independence = new_independence(filter, local);
if (!scop || !independence)
goto error;
independences = isl_realloc_array(ctx, scop->independences,
struct pet_independence *,
scop->n_independence + 1);
if (!independences)
goto error;
scop->independences = independences;
scop->independences[scop->n_independence] = independence;
scop->n_independence++;
return scop;
error:
pet_independence_free(independence);
pet_scop_free(scop);
return NULL;
}
/* Store the concatenation of the independences of "scop1" and "scop2"
* in "scop".
*/
static struct pet_scop *scop_collect_independences(isl_ctx *ctx,
struct pet_scop *scop, struct pet_scop *scop1, struct pet_scop *scop2)
{
int i, off;
if (!scop)
return NULL;
if (scop2->n_independence == 0) {
scop->n_independence = scop1->n_independence;
scop->independences = scop1->independences;
scop1->n_independence = 0;
scop1->independences = NULL;
return scop;
}
if (scop1->n_independence == 0) {
scop->n_independence = scop2->n_independence;
scop->independences = scop2->independences;
scop2->n_independence = 0;
scop2->independences = NULL;
return scop;
}
scop->independences = isl_calloc_array(ctx, struct pet_independence *,
scop1->n_independence + scop2->n_independence);
if (!scop->independences)
return pet_scop_free(scop);
for (i = 0; i < scop1->n_independence; ++i) {
scop->independences[i] = scop1->independences[i];
scop1->independences[i] = NULL;
}
off = scop1->n_independence;
for (i = 0; i < scop2->n_independence; ++i) {
scop->independences[off + i] = scop2->independences[i];
scop2->independences[i] = NULL;
}
scop->n_independence = scop1->n_independence + scop2->n_independence;
return scop;
}
/* Construct a pet_scop with the given schedule
* that contains the offset information,
* arrays, statements and skip information in "scop1" and "scop2".
*/
static struct pet_scop *pet_scop_add(isl_ctx *ctx,
__isl_take isl_schedule *schedule, struct pet_scop *scop1,
struct pet_scop *scop2)
{
int i;
isl_space *space;
struct pet_scop *scop = NULL;
if (!scop1 || !scop2)
goto error;
if (scop1->n_stmt == 0) {
scop2 = scop_combine_skips(scop2, scop1, scop2);
pet_scop_free(scop1);
isl_schedule_free(schedule);
return scop2;
}
if (scop2->n_stmt == 0) {
scop1 = scop_combine_skips(scop1, scop1, scop2);
pet_scop_free(scop2);
isl_schedule_free(schedule);
return scop1;
}
space = isl_set_get_space(scop1->context);
scop = scop_alloc(space, scop1->n_stmt + scop2->n_stmt,
isl_schedule_copy(schedule));
if (!scop)
goto error;
scop->arrays = isl_calloc_array(ctx, struct pet_array *,
scop1->n_array + scop2->n_array);
if (!scop->arrays)
goto error;
scop->n_array = scop1->n_array + scop2->n_array;
for (i = 0; i < scop1->n_stmt; ++i) {
scop->stmts[i] = scop1->stmts[i];
scop1->stmts[i] = NULL;
}
for (i = 0; i < scop2->n_stmt; ++i) {
scop->stmts[scop1->n_stmt + i] = scop2->stmts[i];
scop2->stmts[i] = NULL;
}
for (i = 0; i < scop1->n_array; ++i) {
scop->arrays[i] = scop1->arrays[i];
scop1->arrays[i] = NULL;
}
for (i = 0; i < scop2->n_array; ++i) {
scop->arrays[scop1->n_array + i] = scop2->arrays[i];
scop2->arrays[i] = NULL;
}
scop = scop_collect_implications(ctx, scop, scop1, scop2);
scop = pet_scop_restrict_context(scop, isl_set_copy(scop1->context));
scop = pet_scop_restrict_context(scop, isl_set_copy(scop2->context));
scop = scop_combine_skips(scop, scop1, scop2);
scop = scop_combine_start_end(scop, scop1, scop2);
scop = scop_collect_independences(ctx, scop, scop1, scop2);
pet_scop_free(scop1);
pet_scop_free(scop2);
isl_schedule_free(schedule);
return scop;
error:
pet_scop_free(scop1);
pet_scop_free(scop2);
pet_scop_free(scop);
isl_schedule_free(schedule);
return NULL;
}
/* Apply the skip condition "skip" to "scop".
* That is, make sure "scop" is not executed when the condition holds.
*
* If "skip" is an affine expression, we add the conditions under
* which the expression is zero to the context and the skip conditions
* of "scop".
* Otherwise, we add a filter on the variable attaining the value zero.
*/
static struct pet_scop *restrict_skip(struct pet_scop *scop,
__isl_take isl_multi_pw_aff *skip)
{
isl_set *zero;
isl_pw_aff *pa;
int is_aff;
if (!scop || !skip)
goto error;
is_aff = multi_pw_aff_is_affine(skip);
if (is_aff < 0)
goto error;