-
Notifications
You must be signed in to change notification settings - Fork 9
/
expr.c
3831 lines (3341 loc) · 99.3 KB
/
expr.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/hash.h>
#include <isl/id.h>
#include <isl/val.h>
#include <isl/space.h>
#include <isl/local_space.h>
#include <isl/aff.h>
#include <isl/map.h>
#include <isl/union_set.h>
#include <isl/union_map.h>
#include <isl/printer.h>
#include "aff.h"
#include "array.h"
#include "expr.h"
#include "expr_arg.h"
#include "filter.h"
#include "nest.h"
#include "options.h"
#include "value_bounds.h"
#include "patch.h"
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(*array))
static char *type_str[] = {
[pet_expr_access] = "access",
[pet_expr_call] = "call",
[pet_expr_cast] = "cast",
[pet_expr_double] = "double",
[pet_expr_int] = "int",
[pet_expr_op] = "op",
};
static char *op_str[] = {
[pet_op_add_assign] = "+=",
[pet_op_sub_assign] = "-=",
[pet_op_mul_assign] = "*=",
[pet_op_div_assign] = "/=",
[pet_op_and_assign] = "&=",
[pet_op_xor_assign] = "^=",
[pet_op_or_assign] = "|=",
[pet_op_assign] = "=",
[pet_op_add] = "+",
[pet_op_sub] = "-",
[pet_op_mul] = "*",
[pet_op_div] = "/",
[pet_op_mod] = "%",
[pet_op_shl] = "<<",
[pet_op_shr] = ">>",
[pet_op_eq] = "==",
[pet_op_ne] = "!=",
[pet_op_le] = "<=",
[pet_op_ge] = ">=",
[pet_op_lt] = "<",
[pet_op_gt] = ">",
[pet_op_minus] = "-",
[pet_op_post_inc] = "++",
[pet_op_post_dec] = "--",
[pet_op_pre_inc] = "++",
[pet_op_pre_dec] = "--",
[pet_op_address_of] = "&",
[pet_op_and] = "&",
[pet_op_xor] = "^",
[pet_op_or] = "|",
[pet_op_not] = "~",
[pet_op_land] = "&&",
[pet_op_lor] = "||",
[pet_op_lnot] = "!",
[pet_op_cond] = "?:",
[pet_op_assume] = "assume",
[pet_op_kill] = "kill"
};
const char *pet_op_str(enum pet_op_type op)
{
return op_str[op];
}
int pet_op_is_inc_dec(enum pet_op_type op)
{
return op == pet_op_post_inc || op == pet_op_post_dec ||
op == pet_op_pre_inc || op == pet_op_pre_dec;
}
const char *pet_type_str(enum pet_expr_type type)
{
return type_str[type];
}
enum pet_op_type pet_str_op(const char *str)
{
int i;
for (i = 0; i < ARRAY_SIZE(op_str); ++i)
if (!strcmp(op_str[i], str))
return i;
return -1;
}
enum pet_expr_type pet_str_type(const char *str)
{
int i;
for (i = 0; i < ARRAY_SIZE(type_str); ++i)
if (!strcmp(type_str[i], str))
return i;
return -1;
}
/* Construct a pet_expr of the given type.
*/
__isl_give pet_expr *pet_expr_alloc(isl_ctx *ctx, enum pet_expr_type type)
{
pet_expr *expr;
expr = isl_calloc_type(ctx, struct pet_expr);
if (!expr)
return NULL;
expr->ctx = ctx;
isl_ctx_ref(ctx);
expr->type = type;
expr->ref = 1;
return expr;
}
/* Construct an access pet_expr from an index expression.
* By default, the access is considered to be a read access.
* The initial depth is set from the index expression and
* may still be updated by the caller before the access relation
* is created.
*/
__isl_give pet_expr *pet_expr_from_index(__isl_take isl_multi_pw_aff *index)
{
isl_ctx *ctx;
pet_expr *expr;
if (!index)
return NULL;
ctx = isl_multi_pw_aff_get_ctx(index);
expr = pet_expr_alloc(ctx, pet_expr_access);
if (!expr)
goto error;
expr->acc.read = 1;
expr->acc.write = 0;
expr = pet_expr_access_set_index(expr, index);
return expr;
error:
isl_multi_pw_aff_free(index);
return NULL;
}
/* Extend the range of "access" with "n" dimensions, retaining
* the tuple identifier on this range.
*
* If "access" represents a member access, then extend the range
* of the member.
*/
static __isl_give isl_map *extend_range(__isl_take isl_map *access, int n)
{
isl_id *id;
id = isl_map_get_tuple_id(access, isl_dim_out);
if (!isl_map_range_is_wrapping(access)) {
access = isl_map_add_dims(access, isl_dim_out, n);
} else {
isl_map *domain;
domain = isl_map_copy(access);
domain = isl_map_range_factor_domain(domain);
access = isl_map_range_factor_range(access);
access = extend_range(access, n);
access = isl_map_range_product(domain, access);
}
access = isl_map_set_tuple_id(access, isl_dim_out, id);
return access;
}
/* Does the access expression "expr" have any explicit access relation?
*/
isl_bool pet_expr_access_has_any_access_relation(__isl_keep pet_expr *expr)
{
enum pet_expr_access_type type;
if (!expr)
return isl_bool_error;
for (type = pet_expr_access_begin; type < pet_expr_access_end; ++type)
if (expr->acc.access[type])
return isl_bool_true;
return isl_bool_false;
}
/* Are all relevant access relations explicitly available in "expr"?
*/
static int has_relevant_access_relations(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->acc.kill && !expr->acc.access[pet_expr_access_fake_killed])
return 0;
if (expr->acc.read && !expr->acc.access[pet_expr_access_may_read])
return 0;
if (expr->acc.write &&
(!expr->acc.access[pet_expr_access_may_write] ||
!expr->acc.access[pet_expr_access_must_write]))
return 0;
return 1;
}
/* Replace the depth of the access expr "expr" by "depth".
*
* To avoid inconsistencies between the depth and the access relation,
* we currently do not allow the depth to change once the access relation
* has been set or computed.
*/
__isl_give pet_expr *pet_expr_access_set_depth(__isl_take pet_expr *expr,
int depth)
{
if (!expr)
return NULL;
if (expr->acc.depth == depth)
return expr;
if (pet_expr_access_has_any_access_relation(expr))
isl_die(pet_expr_get_ctx(expr), isl_error_unsupported,
"depth cannot be changed after access relation "
"has been set or computed", return pet_expr_free(expr));
expr = pet_expr_cow(expr);
if (!expr)
return NULL;
expr->acc.depth = depth;
return expr;
}
/* Construct a pet_expr that kills the elements specified by
* the index expression "index" and the access relation "access".
*/
__isl_give pet_expr *pet_expr_kill_from_access_and_index(
__isl_take isl_map *access, __isl_take isl_multi_pw_aff *index)
{
int depth;
pet_expr *expr;
if (!access || !index)
goto error;
expr = pet_expr_from_index(index);
expr = pet_expr_access_set_read(expr, 0);
expr = pet_expr_access_set_kill(expr, 1);
depth = isl_map_dim(access, isl_dim_out);
expr = pet_expr_access_set_depth(expr, depth);
expr = pet_expr_access_set_access(expr, pet_expr_access_killed,
isl_union_map_from_map(access));
return pet_expr_new_unary(0, pet_op_kill, expr);
error:
isl_map_free(access);
isl_multi_pw_aff_free(index);
return NULL;
}
/* Construct a unary pet_expr that performs "op" on "arg",
* where the result is represented using a type of "type_size" bits
* (may be zero if unknown or if the type is not an integer).
*/
__isl_give pet_expr *pet_expr_new_unary(int type_size, enum pet_op_type op,
__isl_take pet_expr *arg)
{
isl_ctx *ctx;
pet_expr *expr;
if (!arg)
return NULL;
ctx = pet_expr_get_ctx(arg);
expr = pet_expr_alloc(ctx, pet_expr_op);
expr = pet_expr_set_n_arg(expr, 1);
if (!expr)
goto error;
expr->op = op;
expr->type_size = type_size;
expr->args[pet_un_arg] = arg;
return expr;
error:
pet_expr_free(arg);
return NULL;
}
/* Construct a binary pet_expr that performs "op" on "lhs" and "rhs",
* where the result is represented using a type of "type_size" bits
* (may be zero if unknown or if the type is not an integer).
*/
__isl_give pet_expr *pet_expr_new_binary(int type_size, enum pet_op_type op,
__isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
{
isl_ctx *ctx;
pet_expr *expr;
if (!lhs || !rhs)
goto error;
ctx = pet_expr_get_ctx(lhs);
expr = pet_expr_alloc(ctx, pet_expr_op);
expr = pet_expr_set_n_arg(expr, 2);
if (!expr)
goto error;
expr->op = op;
expr->type_size = type_size;
expr->args[pet_bin_lhs] = lhs;
expr->args[pet_bin_rhs] = rhs;
return expr;
error:
pet_expr_free(lhs);
pet_expr_free(rhs);
return NULL;
}
/* Construct a ternary pet_expr that performs "cond" ? "lhs" : "rhs".
*/
__isl_give pet_expr *pet_expr_new_ternary(__isl_take pet_expr *cond,
__isl_take pet_expr *lhs, __isl_take pet_expr *rhs)
{
isl_ctx *ctx;
pet_expr *expr;
if (!cond || !lhs || !rhs)
goto error;
ctx = pet_expr_get_ctx(cond);
expr = pet_expr_alloc(ctx, pet_expr_op);
expr = pet_expr_set_n_arg(expr, 3);
if (!expr)
goto error;
expr->op = pet_op_cond;
expr->args[pet_ter_cond] = cond;
expr->args[pet_ter_true] = lhs;
expr->args[pet_ter_false] = rhs;
return expr;
error:
pet_expr_free(cond);
pet_expr_free(lhs);
pet_expr_free(rhs);
return NULL;
}
/* Construct a call pet_expr that calls function "name" with "n_arg"
* arguments. The caller is responsible for filling in the arguments.
*/
__isl_give pet_expr *pet_expr_new_call(isl_ctx *ctx, const char *name,
unsigned n_arg)
{
pet_expr *expr;
expr = pet_expr_alloc(ctx, pet_expr_call);
expr = pet_expr_set_n_arg(expr, n_arg);
if (!expr)
return NULL;
expr->c.name = strdup(name);
if (!expr->c.name)
return pet_expr_free(expr);
return expr;
}
/* Construct a pet_expr that represents the cast of "arg" to "type_name".
*/
__isl_give pet_expr *pet_expr_new_cast(const char *type_name,
__isl_take pet_expr *arg)
{
isl_ctx *ctx;
pet_expr *expr;
if (!arg)
return NULL;
ctx = pet_expr_get_ctx(arg);
expr = pet_expr_alloc(ctx, pet_expr_cast);
expr = pet_expr_set_n_arg(expr, 1);
if (!expr)
goto error;
expr->type_name = strdup(type_name);
if (!expr->type_name)
goto error;
expr->args[0] = arg;
return expr;
error:
pet_expr_free(arg);
pet_expr_free(expr);
return NULL;
}
/* Construct a pet_expr that represents the double "d".
*/
__isl_give pet_expr *pet_expr_new_double(isl_ctx *ctx,
double val, const char *s)
{
pet_expr *expr;
expr = pet_expr_alloc(ctx, pet_expr_double);
if (!expr)
return NULL;
expr->d.val = val;
expr->d.s = strdup(s);
if (!expr->d.s)
return pet_expr_free(expr);
return expr;
}
/* Construct a pet_expr that represents the integer value "v".
*/
__isl_give pet_expr *pet_expr_new_int(__isl_take isl_val *v)
{
isl_ctx *ctx;
pet_expr *expr;
if (!v)
return NULL;
ctx = isl_val_get_ctx(v);
expr = pet_expr_alloc(ctx, pet_expr_int);
if (!expr)
goto error;
expr->i = v;
return expr;
error:
isl_val_free(v);
return NULL;
}
/* Return an independent duplicate of "expr".
*
* In case of an access expression, make sure the depth of the duplicate is set
* before the access relation (if any) is set and after the index expression
* is set.
*/
static __isl_give pet_expr *pet_expr_dup(__isl_keep pet_expr *expr)
{
int i;
pet_expr *dup;
enum pet_expr_access_type type;
if (!expr)
return NULL;
dup = pet_expr_alloc(expr->ctx, expr->type);
dup = pet_expr_set_type_size(dup, expr->type_size);
dup = pet_expr_set_n_arg(dup, expr->n_arg);
for (i = 0; i < expr->n_arg; ++i)
dup = pet_expr_set_arg(dup, i, pet_expr_copy(expr->args[i]));
switch (expr->type) {
case pet_expr_access:
if (expr->acc.ref_id)
dup = pet_expr_access_set_ref_id(dup,
isl_id_copy(expr->acc.ref_id));
dup = pet_expr_access_set_index(dup,
isl_multi_pw_aff_copy(expr->acc.index));
dup = pet_expr_access_set_depth(dup, expr->acc.depth);
for (type = pet_expr_access_begin;
type < pet_expr_access_end; ++type) {
if (!expr->acc.access[type])
continue;
dup = pet_expr_access_set_access(dup, type,
isl_union_map_copy(expr->acc.access[type]));
}
dup = pet_expr_access_set_read(dup, expr->acc.read);
dup = pet_expr_access_set_write(dup, expr->acc.write);
dup = pet_expr_access_set_kill(dup, expr->acc.kill);
break;
case pet_expr_call:
dup = pet_expr_call_set_name(dup, expr->c.name);
if (expr->c.summary)
dup = pet_expr_call_set_summary(dup,
pet_function_summary_copy(expr->c.summary));
break;
case pet_expr_cast:
dup = pet_expr_cast_set_type_name(dup, expr->type_name);
break;
case pet_expr_double:
dup = pet_expr_double_set(dup, expr->d.val, expr->d.s);
break;
case pet_expr_int:
dup = pet_expr_int_set_val(dup, isl_val_copy(expr->i));
break;
case pet_expr_op:
dup = pet_expr_op_set_type(dup, expr->op);
break;
case pet_expr_error:
dup = pet_expr_free(dup);
break;
}
return dup;
}
/* Return a pet_expr that is equal to "expr" and that has only
* a single reference.
*
* If "expr" itself only has one reference, then clear its hash value
* since the returned pet_expr will be modified.
*/
__isl_give pet_expr *pet_expr_cow(__isl_take pet_expr *expr)
{
if (!expr)
return NULL;
if (expr->ref == 1) {
expr->hash = 0;
return expr;
}
expr->ref--;
return pet_expr_dup(expr);
}
__isl_null pet_expr *pet_expr_free(__isl_take pet_expr *expr)
{
enum pet_expr_access_type type;
int i;
if (!expr)
return NULL;
if (--expr->ref > 0)
return NULL;
for (i = 0; i < expr->n_arg; ++i)
pet_expr_free(expr->args[i]);
free(expr->args);
switch (expr->type) {
case pet_expr_access:
isl_id_free(expr->acc.ref_id);
for (type = pet_expr_access_begin;
type < pet_expr_access_end; ++type)
isl_union_map_free(expr->acc.access[type]);
isl_multi_pw_aff_free(expr->acc.index);
break;
case pet_expr_call:
free(expr->c.name);
pet_function_summary_free(expr->c.summary);
break;
case pet_expr_cast:
free(expr->type_name);
break;
case pet_expr_double:
free(expr->d.s);
break;
case pet_expr_int:
isl_val_free(expr->i);
break;
case pet_expr_op:
case pet_expr_error:
break;
}
isl_ctx_deref(expr->ctx);
free(expr);
return NULL;
}
/* Return an additional reference to "expr".
*/
__isl_give pet_expr *pet_expr_copy(__isl_keep pet_expr *expr)
{
if (!expr)
return NULL;
expr->ref++;
return expr;
}
/* Return the isl_ctx in which "expr" was created.
*/
isl_ctx *pet_expr_get_ctx(__isl_keep pet_expr *expr)
{
return expr ? expr->ctx : NULL;
}
/* Return the type of "expr".
*/
enum pet_expr_type pet_expr_get_type(__isl_keep pet_expr *expr)
{
if (!expr)
return pet_expr_error;
return expr->type;
}
/* Return the number of arguments of "expr".
*/
int pet_expr_get_n_arg(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
return expr->n_arg;
}
/* Set the number of arguments of "expr" to "n".
*
* If "expr" originally had more arguments, then remove the extra arguments.
* If "expr" originally had fewer arguments, then create space for
* the extra arguments ans initialize them to NULL.
*/
__isl_give pet_expr *pet_expr_set_n_arg(__isl_take pet_expr *expr, int n)
{
int i;
pet_expr **args;
if (!expr)
return NULL;
if (expr->n_arg == n)
return expr;
expr = pet_expr_cow(expr);
if (!expr)
return NULL;
if (n < expr->n_arg) {
for (i = n; i < expr->n_arg; ++i)
pet_expr_free(expr->args[i]);
expr->n_arg = n;
return expr;
}
args = isl_realloc_array(expr->ctx, expr->args, pet_expr *, n);
if (!args)
return pet_expr_free(expr);
expr->args = args;
for (i = expr->n_arg; i < n; ++i)
expr->args[i] = NULL;
expr->n_arg = n;
return expr;
}
/* Return the argument of "expr" at position "pos".
*/
__isl_give pet_expr *pet_expr_get_arg(__isl_keep pet_expr *expr, int pos)
{
if (!expr)
return NULL;
if (pos < 0 || pos >= expr->n_arg)
isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
"position out of bounds", return NULL);
return pet_expr_copy(expr->args[pos]);
}
/* Replace "expr" by its argument at position "pos".
*/
__isl_give pet_expr *pet_expr_arg(__isl_take pet_expr *expr, int pos)
{
pet_expr *arg;
arg = pet_expr_get_arg(expr, pos);
pet_expr_free(expr);
return arg;
}
/* Replace the argument of "expr" at position "pos" by "arg".
*/
__isl_give pet_expr *pet_expr_set_arg(__isl_take pet_expr *expr, int pos,
__isl_take pet_expr *arg)
{
if (!expr || !arg)
goto error;
if (pos < 0 || pos >= expr->n_arg)
isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
"position out of bounds", goto error);
if (expr->args[pos] == arg) {
pet_expr_free(arg);
return expr;
}
expr = pet_expr_cow(expr);
if (!expr)
goto error;
pet_expr_free(expr->args[pos]);
expr->args[pos] = arg;
return expr;
error:
pet_expr_free(expr);
pet_expr_free(arg);
return NULL;
}
/* Does "expr" perform a comparison operation?
*/
int pet_expr_is_comparison(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_op)
return 0;
switch (expr->op) {
case pet_op_eq:
case pet_op_ne:
case pet_op_le:
case pet_op_ge:
case pet_op_lt:
case pet_op_gt:
return 1;
default:
return 0;
}
}
/* Does "expr" perform a boolean operation?
*/
int pet_expr_is_boolean(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_op)
return 0;
switch (expr->op) {
case pet_op_land:
case pet_op_lor:
case pet_op_lnot:
return 1;
default:
return 0;
}
}
/* Is "expr" an address-of operation?
*/
int pet_expr_is_address_of(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_op)
return 0;
return expr->op == pet_op_address_of;
}
/* Is "expr" an assume statement?
*/
int pet_expr_is_assume(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_op)
return 0;
return expr->op == pet_op_assume;
}
/* Does "expr" perform a min operation?
*/
int pet_expr_is_min(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_call)
return 0;
if (expr->n_arg != 2)
return 0;
if (strcmp(expr->c.name, "min") != 0)
return 0;
return 1;
}
/* Does "expr" perform a max operation?
*/
int pet_expr_is_max(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_call)
return 0;
if (expr->n_arg != 2)
return 0;
if (strcmp(expr->c.name, "max") != 0)
return 0;
return 1;
}
/* Does "expr" represent an access to an unnamed space, i.e.,
* does it represent an affine expression?
*/
isl_bool pet_expr_is_affine(__isl_keep pet_expr *expr)
{
int has_id;
if (!expr)
return isl_bool_error;
if (expr->type != pet_expr_access)
return isl_bool_false;
has_id = isl_multi_pw_aff_has_tuple_id(expr->acc.index, isl_dim_out);
if (has_id < 0)
return isl_bool_error;
return !has_id;
}
/* Given that "expr" represents an affine expression, i.e., that
* it is an access to an unnamed (1D) space, return this affine expression.
*/
__isl_give isl_pw_aff *pet_expr_get_affine(__isl_keep pet_expr *expr)
{
isl_bool is_affine;
isl_pw_aff *pa;
isl_multi_pw_aff *mpa;
is_affine = pet_expr_is_affine(expr);
if (is_affine < 0)
return NULL;
if (!is_affine)
isl_die(pet_expr_get_ctx(expr), isl_error_invalid,
"not an affine expression", return NULL);
mpa = pet_expr_access_get_index(expr);
pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
isl_multi_pw_aff_free(mpa);
return pa;
}
/* Does "expr" represent an access to a scalar, i.e., a zero-dimensional array,
* not part of any struct?
*/
int pet_expr_is_scalar_access(__isl_keep pet_expr *expr)
{
if (!expr)
return -1;
if (expr->type != pet_expr_access)
return 0;
if (isl_multi_pw_aff_range_is_wrapping(expr->acc.index))
return 0;
return expr->acc.depth == 0;
}
/* Construct an access relation from the index expression and
* the array depth of the access expression "expr".
*
* If the number of indices is smaller than the depth of the array,
* then we assume that all elements of the remaining dimensions
* are accessed.
*/
static __isl_give isl_union_map *construct_access_relation(
__isl_keep pet_expr *expr)
{
isl_map *access;
int dim;
if (!expr)
return NULL;
access = isl_map_from_multi_pw_aff(pet_expr_access_get_index(expr));
if (!access)
return NULL;
dim = isl_map_dim(access, isl_dim_out);
if (dim > expr->acc.depth)
isl_die(isl_map_get_ctx(access), isl_error_internal,
"number of indices greater than depth",
access = isl_map_free(access));
if (dim != expr->acc.depth)
access = extend_range(access, expr->acc.depth - dim);
return isl_union_map_from_map(access);
}
/* Ensure that all relevant access relations are explicitly
* available in "expr".
*
* If "expr" does not already have the relevant access relations, then create
* them based on the index expression and the array depth.
*
* We do not cow since adding an explicit access relation
* does not change the meaning of the expression.
* However, the explicit access relations may modify the hash value,
* so the cached value is reset.
*/
static __isl_give pet_expr *introduce_access_relations(
__isl_take pet_expr *expr)
{
isl_union_map *access;
int kill, read, write;
if (!expr)
return NULL;
if (has_relevant_access_relations(expr))
return expr;
access = construct_access_relation(expr);
if (!access)
return pet_expr_free(expr);
expr->hash = 0;
kill = expr->acc.kill;
read = expr->acc.read;
write = expr->acc.write;
if (kill && !expr->acc.access[pet_expr_access_fake_killed])
expr->acc.access[pet_expr_access_fake_killed] =
isl_union_map_copy(access);
if (read && !expr->acc.access[pet_expr_access_may_read])
expr->acc.access[pet_expr_access_may_read] =
isl_union_map_copy(access);
if (write && !expr->acc.access[pet_expr_access_may_write])
expr->acc.access[pet_expr_access_may_write] =
isl_union_map_copy(access);
if (write && !expr->acc.access[pet_expr_access_must_write])
expr->acc.access[pet_expr_access_must_write] =
isl_union_map_copy(access);
isl_union_map_free(access);
if (!has_relevant_access_relations(expr))
return pet_expr_free(expr);
return expr;
}
/* Return a hash value that digests "expr".
* If a hash value was computed already, then return that value.
* Otherwise, compute the hash value and store a copy in expr->hash.
*/
uint32_t pet_expr_get_hash(__isl_keep pet_expr *expr)
{
int i;
enum pet_expr_access_type type;
uint32_t hash, hash_f;
if (!expr)
return 0;
if (expr->hash)
return expr->hash;
hash = isl_hash_init();
isl_hash_byte(hash, expr->type & 0xFF);
isl_hash_byte(hash, expr->n_arg & 0xFF);
for (i = 0; i < expr->n_arg; ++i) {
uint32_t hash_i;
hash_i = pet_expr_get_hash(expr->args[i]);
isl_hash_hash(hash, hash_i);
}