forked from illumos/gcc
/
tree-ssa-alias.c
1890 lines (1649 loc) · 56.7 KB
/
tree-ssa-alias.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
/* Alias analysis for trees.
Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
Free Software Foundation, Inc.
Contributed by Diego Novillo <dnovillo@redhat.com>
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "tm_p.h"
#include "target.h"
#include "basic-block.h"
#include "timevar.h"
#include "ggc.h"
#include "langhooks.h"
#include "flags.h"
#include "function.h"
#include "tree-pretty-print.h"
#include "tree-dump.h"
#include "gimple.h"
#include "tree-flow.h"
#include "tree-inline.h"
#include "tree-pass.h"
#include "convert.h"
#include "params.h"
#include "vec.h"
#include "bitmap.h"
#include "vecprim.h"
#include "pointer-set.h"
#include "alloc-pool.h"
#include "tree-ssa-alias.h"
/* Broad overview of how alias analysis on gimple works:
Statements clobbering or using memory are linked through the
virtual operand factored use-def chain. The virtual operand
is unique per function, its symbol is accessible via gimple_vop (cfun).
Virtual operands are used for efficiently walking memory statements
in the gimple IL and are useful for things like value-numbering as
a generation count for memory references.
SSA_NAME pointers may have associated points-to information
accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
points-to information is (re-)computed by the TODO_rebuild_alias
pass manager todo. Points-to information is also used for more
precise tracking of call-clobbered and call-used variables and
related disambiguations.
This file contains functions for disambiguating memory references,
the so called alias-oracle and tools for walking of the gimple IL.
The main alias-oracle entry-points are
bool stmt_may_clobber_ref_p (gimple, tree)
This function queries if a statement may invalidate (parts of)
the memory designated by the reference tree argument.
bool ref_maybe_used_by_stmt_p (gimple, tree)
This function queries if a statement may need (parts of) the
memory designated by the reference tree argument.
There are variants of these functions that only handle the call
part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
Note that these do not disambiguate against a possible call lhs.
bool refs_may_alias_p (tree, tree)
This function tries to disambiguate two reference trees.
bool ptr_deref_may_alias_global_p (tree)
This function queries if dereferencing a pointer variable may
alias global memory.
More low-level disambiguators are available and documented in
this file. Low-level disambiguators dealing with points-to
information are in tree-ssa-structalias.c. */
/* Query statistics for the different low-level disambiguators.
A high-level query may trigger multiple of them. */
static struct {
unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
} alias_stats;
void
dump_alias_stats (FILE *s)
{
fprintf (s, "\nAlias oracle query stats:\n");
fprintf (s, " refs_may_alias_p: "
HOST_WIDE_INT_PRINT_DEC" disambiguations, "
HOST_WIDE_INT_PRINT_DEC" queries\n",
alias_stats.refs_may_alias_p_no_alias,
alias_stats.refs_may_alias_p_no_alias
+ alias_stats.refs_may_alias_p_may_alias);
fprintf (s, " ref_maybe_used_by_call_p: "
HOST_WIDE_INT_PRINT_DEC" disambiguations, "
HOST_WIDE_INT_PRINT_DEC" queries\n",
alias_stats.ref_maybe_used_by_call_p_no_alias,
alias_stats.refs_may_alias_p_no_alias
+ alias_stats.ref_maybe_used_by_call_p_may_alias);
fprintf (s, " call_may_clobber_ref_p: "
HOST_WIDE_INT_PRINT_DEC" disambiguations, "
HOST_WIDE_INT_PRINT_DEC" queries\n",
alias_stats.call_may_clobber_ref_p_no_alias,
alias_stats.call_may_clobber_ref_p_no_alias
+ alias_stats.call_may_clobber_ref_p_may_alias);
}
/* Return true, if dereferencing PTR may alias with a global variable. */
bool
ptr_deref_may_alias_global_p (tree ptr)
{
struct ptr_info_def *pi;
/* If we end up with a pointer constant here that may point
to global memory. */
if (TREE_CODE (ptr) != SSA_NAME)
return true;
pi = SSA_NAME_PTR_INFO (ptr);
/* If we do not have points-to information for this variable,
we have to punt. */
if (!pi)
return true;
/* ??? This does not use TBAA to prune globals ptr may not access. */
return pt_solution_includes_global (&pi->pt);
}
/* Return true if dereferencing PTR may alias DECL.
The caller is responsible for applying TBAA to see if PTR
may access DECL at all. */
static bool
ptr_deref_may_alias_decl_p (tree ptr, tree decl)
{
struct ptr_info_def *pi;
/* Conversions are irrelevant for points-to information and
data-dependence analysis can feed us those. */
STRIP_NOPS (ptr);
/* Anything we do not explicilty handle aliases. */
if ((TREE_CODE (ptr) != SSA_NAME
&& TREE_CODE (ptr) != ADDR_EXPR
&& TREE_CODE (ptr) != POINTER_PLUS_EXPR)
|| !POINTER_TYPE_P (TREE_TYPE (ptr))
|| (TREE_CODE (decl) != VAR_DECL
&& TREE_CODE (decl) != PARM_DECL
&& TREE_CODE (decl) != RESULT_DECL))
return true;
/* Disregard pointer offsetting. */
if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
{
do
{
ptr = TREE_OPERAND (ptr, 0);
}
while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
return ptr_deref_may_alias_decl_p (ptr, decl);
}
/* ADDR_EXPR pointers either just offset another pointer or directly
specify the pointed-to set. */
if (TREE_CODE (ptr) == ADDR_EXPR)
{
tree base = get_base_address (TREE_OPERAND (ptr, 0));
if (base
&& (INDIRECT_REF_P (base)
|| TREE_CODE (base) == MEM_REF))
ptr = TREE_OPERAND (base, 0);
else if (base
&& SSA_VAR_P (base))
return base == decl;
else if (base
&& CONSTANT_CLASS_P (base))
return false;
else
return true;
}
/* Non-aliased variables can not be pointed to. */
if (!may_be_aliased (decl))
return false;
/* If we do not have useful points-to information for this pointer
we cannot disambiguate anything else. */
pi = SSA_NAME_PTR_INFO (ptr);
if (!pi)
return true;
/* If the decl can be used as a restrict tag and we have a restrict
pointer and that pointers points-to set doesn't contain this decl
then they can't alias. */
if (DECL_RESTRICTED_P (decl)
&& TYPE_RESTRICT (TREE_TYPE (ptr))
&& pi->pt.vars_contains_restrict)
return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
return pt_solution_includes (&pi->pt, decl);
}
/* Return true if dereferenced PTR1 and PTR2 may alias.
The caller is responsible for applying TBAA to see if accesses
through PTR1 and PTR2 may conflict at all. */
bool
ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
{
struct ptr_info_def *pi1, *pi2;
/* Conversions are irrelevant for points-to information and
data-dependence analysis can feed us those. */
STRIP_NOPS (ptr1);
STRIP_NOPS (ptr2);
/* Anything we do not explicilty handle aliases. */
if ((TREE_CODE (ptr1) != SSA_NAME
&& TREE_CODE (ptr1) != ADDR_EXPR
&& TREE_CODE (ptr1) != POINTER_PLUS_EXPR)
|| (TREE_CODE (ptr2) != SSA_NAME
&& TREE_CODE (ptr2) != ADDR_EXPR
&& TREE_CODE (ptr2) != POINTER_PLUS_EXPR)
|| !POINTER_TYPE_P (TREE_TYPE (ptr1))
|| !POINTER_TYPE_P (TREE_TYPE (ptr2)))
return true;
/* Disregard pointer offsetting. */
if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
{
do
{
ptr1 = TREE_OPERAND (ptr1, 0);
}
while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
return ptr_derefs_may_alias_p (ptr1, ptr2);
}
if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
{
do
{
ptr2 = TREE_OPERAND (ptr2, 0);
}
while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
return ptr_derefs_may_alias_p (ptr1, ptr2);
}
/* ADDR_EXPR pointers either just offset another pointer or directly
specify the pointed-to set. */
if (TREE_CODE (ptr1) == ADDR_EXPR)
{
tree base = get_base_address (TREE_OPERAND (ptr1, 0));
if (base
&& (INDIRECT_REF_P (base)
|| TREE_CODE (base) == MEM_REF))
ptr1 = TREE_OPERAND (base, 0);
else if (base
&& SSA_VAR_P (base))
return ptr_deref_may_alias_decl_p (ptr2, base);
else
return true;
}
if (TREE_CODE (ptr2) == ADDR_EXPR)
{
tree base = get_base_address (TREE_OPERAND (ptr2, 0));
if (base
&& (INDIRECT_REF_P (base)
|| TREE_CODE (base) == MEM_REF))
ptr2 = TREE_OPERAND (base, 0);
else if (base
&& SSA_VAR_P (base))
return ptr_deref_may_alias_decl_p (ptr1, base);
else
return true;
}
/* We may end up with two empty points-to solutions for two same pointers.
In this case we still want to say both pointers alias, so shortcut
that here. */
if (ptr1 == ptr2)
return true;
/* If we do not have useful points-to information for either pointer
we cannot disambiguate anything else. */
pi1 = SSA_NAME_PTR_INFO (ptr1);
pi2 = SSA_NAME_PTR_INFO (ptr2);
if (!pi1 || !pi2)
return true;
/* If both pointers are restrict-qualified try to disambiguate
with restrict information. */
if (TYPE_RESTRICT (TREE_TYPE (ptr1))
&& TYPE_RESTRICT (TREE_TYPE (ptr2))
&& !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
return false;
/* ??? This does not use TBAA to prune decls from the intersection
that not both pointers may access. */
return pt_solutions_intersect (&pi1->pt, &pi2->pt);
}
/* Return true if dereferencing PTR may alias *REF.
The caller is responsible for applying TBAA to see if PTR
may access *REF at all. */
static bool
ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
{
tree base = ao_ref_base (ref);
if (INDIRECT_REF_P (base)
|| TREE_CODE (base) == MEM_REF)
return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
else if (SSA_VAR_P (base))
return ptr_deref_may_alias_decl_p (ptr, base);
return true;
}
/* Dump alias information on FILE. */
void
dump_alias_info (FILE *file)
{
size_t i;
const char *funcname
= lang_hooks.decl_printable_name (current_function_decl, 2);
referenced_var_iterator rvi;
tree var;
fprintf (file, "\n\nAlias information for %s\n\n", funcname);
fprintf (file, "Aliased symbols\n\n");
FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
{
if (may_be_aliased (var))
dump_variable (file, var);
}
fprintf (file, "\nCall clobber information\n");
fprintf (file, "\nESCAPED");
dump_points_to_solution (file, &cfun->gimple_df->escaped);
fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
for (i = 1; i < num_ssa_names; i++)
{
tree ptr = ssa_name (i);
struct ptr_info_def *pi;
if (ptr == NULL_TREE
|| SSA_NAME_IN_FREE_LIST (ptr))
continue;
pi = SSA_NAME_PTR_INFO (ptr);
if (pi)
dump_points_to_info_for (file, ptr);
}
fprintf (file, "\n");
}
/* Dump alias information on stderr. */
DEBUG_FUNCTION void
debug_alias_info (void)
{
dump_alias_info (stderr);
}
/* Dump the points-to set *PT into FILE. */
void
dump_points_to_solution (FILE *file, struct pt_solution *pt)
{
if (pt->anything)
fprintf (file, ", points-to anything");
if (pt->nonlocal)
fprintf (file, ", points-to non-local");
if (pt->escaped)
fprintf (file, ", points-to escaped");
if (pt->ipa_escaped)
fprintf (file, ", points-to unit escaped");
if (pt->null)
fprintf (file, ", points-to NULL");
if (pt->vars)
{
fprintf (file, ", points-to vars: ");
dump_decl_set (file, pt->vars);
if (pt->vars_contains_global)
fprintf (file, " (includes global vars)");
if (pt->vars_contains_restrict)
fprintf (file, " (includes restrict tags)");
}
}
/* Dump points-to information for SSA_NAME PTR into FILE. */
void
dump_points_to_info_for (FILE *file, tree ptr)
{
struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
print_generic_expr (file, ptr, dump_flags);
if (pi)
dump_points_to_solution (file, &pi->pt);
else
fprintf (file, ", points-to anything");
fprintf (file, "\n");
}
/* Dump points-to information for VAR into stderr. */
DEBUG_FUNCTION void
debug_points_to_info_for (tree var)
{
dump_points_to_info_for (stderr, var);
}
/* Initializes the alias-oracle reference representation *R from REF. */
void
ao_ref_init (ao_ref *r, tree ref)
{
r->ref = ref;
r->base = NULL_TREE;
r->offset = 0;
r->size = -1;
r->max_size = -1;
r->ref_alias_set = -1;
r->base_alias_set = -1;
}
/* Returns the base object of the memory reference *REF. */
tree
ao_ref_base (ao_ref *ref)
{
if (ref->base)
return ref->base;
ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
&ref->max_size);
return ref->base;
}
/* Returns the base object alias set of the memory reference *REF. */
static alias_set_type
ao_ref_base_alias_set (ao_ref *ref)
{
tree base_ref;
if (ref->base_alias_set != -1)
return ref->base_alias_set;
if (!ref->ref)
return 0;
base_ref = ref->ref;
while (handled_component_p (base_ref))
base_ref = TREE_OPERAND (base_ref, 0);
ref->base_alias_set = get_alias_set (base_ref);
return ref->base_alias_set;
}
/* Returns the reference alias set of the memory reference *REF. */
alias_set_type
ao_ref_alias_set (ao_ref *ref)
{
if (ref->ref_alias_set != -1)
return ref->ref_alias_set;
ref->ref_alias_set = get_alias_set (ref->ref);
return ref->ref_alias_set;
}
/* Init an alias-oracle reference representation from a gimple pointer
PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
size is assumed to be unknown. The access is assumed to be only
to or after of the pointer target, not before it. */
void
ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
{
HOST_WIDE_INT t1, t2;
ref->ref = NULL_TREE;
if (TREE_CODE (ptr) == ADDR_EXPR)
ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
&ref->offset, &t1, &t2);
else
{
ref->base = build2 (MEM_REF, char_type_node,
ptr, null_pointer_node);
ref->offset = 0;
}
if (size
&& host_integerp (size, 0)
&& TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
else
ref->max_size = ref->size = -1;
ref->ref_alias_set = 0;
ref->base_alias_set = 0;
}
/* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
decide. */
static inline int
same_type_for_tbaa (tree type1, tree type2)
{
type1 = TYPE_MAIN_VARIANT (type1);
type2 = TYPE_MAIN_VARIANT (type2);
/* If we would have to do structural comparison bail out. */
if (TYPE_STRUCTURAL_EQUALITY_P (type1)
|| TYPE_STRUCTURAL_EQUALITY_P (type2))
return -1;
/* Compare the canonical types. */
if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
return 1;
/* ??? Array types are not properly unified in all cases as we have
spurious changes in the index types for example. Removing this
causes all sorts of problems with the Fortran frontend. */
if (TREE_CODE (type1) == ARRAY_TYPE
&& TREE_CODE (type2) == ARRAY_TYPE)
return -1;
/* ??? In Ada, an lvalue of an unconstrained type can be used to access an
object of one of its constrained subtypes, e.g. when a function with an
unconstrained parameter passed by reference is called on an object and
inlined. But, even in the case of a fixed size, type and subtypes are
not equivalent enough as to share the same TYPE_CANONICAL, since this
would mean that conversions between them are useless, whereas they are
not (e.g. type and subtypes can have different modes). So, in the end,
they are only guaranteed to have the same alias set. */
if (get_alias_set (type1) == get_alias_set (type2))
return -1;
/* The types are known to be not equal. */
return 0;
}
/* Determine if the two component references REF1 and REF2 which are
based on access types TYPE1 and TYPE2 and of which at least one is based
on an indirect reference may alias. REF2 is the only one that can
be a decl in which case REF2_IS_DECL is true.
REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
are the respective alias sets. */
static bool
aliasing_component_refs_p (tree ref1, tree type1,
alias_set_type ref1_alias_set,
alias_set_type base1_alias_set,
HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
tree ref2, tree type2,
alias_set_type ref2_alias_set,
alias_set_type base2_alias_set,
HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
bool ref2_is_decl)
{
/* If one reference is a component references through pointers try to find a
common base and apply offset based disambiguation. This handles
for example
struct A { int i; int j; } *q;
struct B { struct A a; int k; } *p;
disambiguating q->i and p->a.j. */
tree *refp;
int same_p;
/* Now search for the type1 in the access path of ref2. This
would be a common base for doing offset based disambiguation on. */
refp = &ref2;
while (handled_component_p (*refp)
&& same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
refp = &TREE_OPERAND (*refp, 0);
same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
/* If we couldn't compare types we have to bail out. */
if (same_p == -1)
return true;
else if (same_p == 1)
{
HOST_WIDE_INT offadj, sztmp, msztmp;
get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
offset2 -= offadj;
return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
}
/* If we didn't find a common base, try the other way around. */
refp = &ref1;
while (handled_component_p (*refp)
&& same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
refp = &TREE_OPERAND (*refp, 0);
same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
/* If we couldn't compare types we have to bail out. */
if (same_p == -1)
return true;
else if (same_p == 1)
{
HOST_WIDE_INT offadj, sztmp, msztmp;
get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
offset1 -= offadj;
return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
}
/* If we have two type access paths B1.path1 and B2.path2 they may
only alias if either B1 is in B2.path2 or B2 is in B1.path1.
But we can still have a path that goes B1.path1...B2.path2 with
a part that we do not see. So we can only disambiguate now
if there is no B2 in the tail of path1 and no B1 on the
tail of path2. */
if (base1_alias_set == ref2_alias_set
|| alias_set_subset_of (base1_alias_set, ref2_alias_set))
return true;
/* If this is ptr vs. decl then we know there is no ptr ... decl path. */
if (!ref2_is_decl)
return (base2_alias_set == ref1_alias_set
|| alias_set_subset_of (base2_alias_set, ref1_alias_set));
return false;
}
/* Return true if two memory references based on the variables BASE1
and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
[OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
static bool
decl_refs_may_alias_p (tree base1,
HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
tree base2,
HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
{
gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
/* If both references are based on different variables, they cannot alias. */
if (base1 != base2)
return false;
/* If both references are based on the same variable, they cannot alias if
the accesses do not overlap. */
return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
}
/* Return true if an indirect reference based on *PTR1 constrained
to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
in which case they are computed on-demand. REF1 and REF2
if non-NULL are the complete memory reference trees. */
static bool
indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
HOST_WIDE_INT offset1,
HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
alias_set_type ref1_alias_set,
alias_set_type base1_alias_set,
tree ref2 ATTRIBUTE_UNUSED, tree base2,
HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
alias_set_type ref2_alias_set,
alias_set_type base2_alias_set, bool tbaa_p)
{
tree ptr1;
tree ptrtype1;
HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
ptr1 = TREE_OPERAND (base1, 0);
/* The offset embedded in MEM_REFs can be negative. Bias them
so that the resulting offset adjustment is positive. */
if (TREE_CODE (base1) == MEM_REF
|| TREE_CODE (base1) == TARGET_MEM_REF)
{
double_int moff = mem_ref_offset (base1);
moff = double_int_lshift (moff,
BITS_PER_UNIT == 8
? 3 : exact_log2 (BITS_PER_UNIT),
HOST_BITS_PER_DOUBLE_INT, true);
if (double_int_negative_p (moff))
offset2p += double_int_neg (moff).low;
else
offset1p += moff.low;
}
/* If only one reference is based on a variable, they cannot alias if
the pointer access is beyond the extent of the variable access.
(the pointer base cannot validly point to an offset less than zero
of the variable).
They also cannot alias if the pointer may not point to the decl. */
if ((TREE_CODE (base1) != TARGET_MEM_REF
|| (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
&& !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
return false;
if (!ptr_deref_may_alias_decl_p (ptr1, base2))
return false;
/* Disambiguations that rely on strict aliasing rules follow. */
if (!flag_strict_aliasing || !tbaa_p)
return true;
if (TREE_CODE (base1) == MEM_REF)
ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
else if (TREE_CODE (base1) == TARGET_MEM_REF)
ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
else
ptrtype1 = TREE_TYPE (ptr1);
/* If the alias set for a pointer access is zero all bets are off. */
if (base1_alias_set == -1)
base1_alias_set = get_deref_alias_set (ptrtype1);
if (base1_alias_set == 0)
return true;
if (base2_alias_set == -1)
base2_alias_set = get_alias_set (base2);
/* If both references are through the same type, they do not alias
if the accesses do not overlap. This does extra disambiguation
for mixed/pointer accesses but requires strict aliasing.
For MEM_REFs we require that the component-ref offset we computed
is relative to the start of the type which we ensure by
comparing rvalue and access type and disregarding the constant
pointer offset. */
if ((TREE_CODE (base1) != TARGET_MEM_REF
|| (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
&& (TREE_CODE (base1) != MEM_REF
|| same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
&& same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
/* When we are trying to disambiguate an access with a pointer dereference
as base versus one with a decl as base we can use both the size
of the decl and its dynamic type for extra disambiguation.
??? We do not know anything about the dynamic type of the decl
other than that its alias-set contains base2_alias_set as a subset
which does not help us here. */
/* As we know nothing useful about the dynamic type of the decl just
use the usual conflict check rather than a subset test.
??? We could introduce -fvery-strict-aliasing when the language
does not allow decls to have a dynamic type that differs from their
static type. Then we can check
!alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
if (base1_alias_set != base2_alias_set
&& !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
return false;
/* If the size of the access relevant for TBAA through the pointer
is bigger than the size of the decl we can't possibly access the
decl via that pointer. */
if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
&& TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
&& TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
/* ??? This in turn may run afoul when a decl of type T which is
a member of union type U is accessed through a pointer to
type U and sizeof T is smaller than sizeof U. */
&& TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
&& TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
&& tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
return false;
/* Do access-path based disambiguation. */
if (ref1 && ref2
&& handled_component_p (ref1)
&& handled_component_p (ref2)
&& TREE_CODE (base1) != TARGET_MEM_REF
&& (TREE_CODE (base1) != MEM_REF
|| same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
ref1_alias_set, base1_alias_set,
offset1, max_size1,
ref2, TREE_TYPE
(reference_alias_ptr_type (ref2)),
ref2_alias_set, base2_alias_set,
offset2, max_size2, true);
return true;
}
/* Return true if two indirect references based on *PTR1
and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
[OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
in which case they are computed on-demand. REF1 and REF2
if non-NULL are the complete memory reference trees. */
static bool
indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
alias_set_type ref1_alias_set,
alias_set_type base1_alias_set,
tree ref2 ATTRIBUTE_UNUSED, tree base2,
HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
alias_set_type ref2_alias_set,
alias_set_type base2_alias_set, bool tbaa_p)
{
tree ptr1;
tree ptr2;
tree ptrtype1, ptrtype2;
ptr1 = TREE_OPERAND (base1, 0);
ptr2 = TREE_OPERAND (base2, 0);
/* If both bases are based on pointers they cannot alias if they may not
point to the same memory object or if they point to the same object
and the accesses do not overlap. */
if ((!cfun || gimple_in_ssa_p (cfun))
&& operand_equal_p (ptr1, ptr2, 0)
&& (((TREE_CODE (base1) != TARGET_MEM_REF
|| (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
&& (TREE_CODE (base2) != TARGET_MEM_REF
|| (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
|| (TREE_CODE (base1) == TARGET_MEM_REF
&& TREE_CODE (base2) == TARGET_MEM_REF
&& (TMR_STEP (base1) == TMR_STEP (base2)
|| (TMR_STEP (base1) && TMR_STEP (base2)
&& operand_equal_p (TMR_STEP (base1),
TMR_STEP (base2), 0)))
&& (TMR_INDEX (base1) == TMR_INDEX (base2)
|| (TMR_INDEX (base1) && TMR_INDEX (base2)
&& operand_equal_p (TMR_INDEX (base1),
TMR_INDEX (base2), 0)))
&& (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
|| (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
&& operand_equal_p (TMR_INDEX2 (base1),
TMR_INDEX2 (base2), 0))))))
{
/* The offset embedded in MEM_REFs can be negative. Bias them
so that the resulting offset adjustment is positive. */
if (TREE_CODE (base1) == MEM_REF
|| TREE_CODE (base1) == TARGET_MEM_REF)
{
double_int moff = mem_ref_offset (base1);
moff = double_int_lshift (moff,
BITS_PER_UNIT == 8
? 3 : exact_log2 (BITS_PER_UNIT),
HOST_BITS_PER_DOUBLE_INT, true);
if (double_int_negative_p (moff))
offset2 += double_int_neg (moff).low;
else
offset1 += moff.low;
}
if (TREE_CODE (base2) == MEM_REF
|| TREE_CODE (base2) == TARGET_MEM_REF)
{
double_int moff = mem_ref_offset (base2);
moff = double_int_lshift (moff,
BITS_PER_UNIT == 8
? 3 : exact_log2 (BITS_PER_UNIT),
HOST_BITS_PER_DOUBLE_INT, true);
if (double_int_negative_p (moff))
offset1 += double_int_neg (moff).low;
else
offset2 += moff.low;
}
return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
}
if (!ptr_derefs_may_alias_p (ptr1, ptr2))
return false;
/* Disambiguations that rely on strict aliasing rules follow. */
if (!flag_strict_aliasing || !tbaa_p)
return true;
if (TREE_CODE (base1) == MEM_REF)
ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
else if (TREE_CODE (base1) == TARGET_MEM_REF)
ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
else
ptrtype1 = TREE_TYPE (ptr1);
if (TREE_CODE (base2) == MEM_REF)
ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
else if (TREE_CODE (base2) == TARGET_MEM_REF)
ptrtype2 = TREE_TYPE (TMR_OFFSET (base2));
else
ptrtype2 = TREE_TYPE (ptr2);
/* If the alias set for a pointer access is zero all bets are off. */
if (base1_alias_set == -1)
base1_alias_set = get_deref_alias_set (ptrtype1);
if (base1_alias_set == 0)
return true;
if (base2_alias_set == -1)
base2_alias_set = get_deref_alias_set (ptrtype2);
if (base2_alias_set == 0)
return true;
/* If both references are through the same type, they do not alias
if the accesses do not overlap. This does extra disambiguation
for mixed/pointer accesses but requires strict aliasing. */
if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
&& (TREE_CODE (base2) != TARGET_MEM_REF || !TMR_INDEX (base2))
&& (TREE_CODE (base1) != MEM_REF
|| same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
&& (TREE_CODE (base2) != MEM_REF
|| same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
&& same_type_for_tbaa (TREE_TYPE (ptrtype1),
TREE_TYPE (ptrtype2)) == 1)
return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
/* Do type-based disambiguation. */
if (base1_alias_set != base2_alias_set
&& !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
return false;
/* Do access-path based disambiguation. */
if (ref1 && ref2
&& handled_component_p (ref1)
&& handled_component_p (ref2)
&& TREE_CODE (base1) != TARGET_MEM_REF
&& TREE_CODE (base2) != TARGET_MEM_REF
&& (TREE_CODE (base1) != MEM_REF
|| same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
&& (TREE_CODE (base2) != MEM_REF
|| same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
ref1_alias_set, base1_alias_set,
offset1, max_size1,
ref2, TREE_TYPE (ptrtype2),
ref2_alias_set, base2_alias_set,
offset2, max_size2, false);
return true;
}
/* Return true, if the two memory references REF1 and REF2 may alias. */
bool
refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
{
tree base1, base2;
HOST_WIDE_INT offset1 = 0, offset2 = 0;
HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
bool var1_p, var2_p, ind1_p, ind2_p;
gcc_checking_assert ((!ref1->ref
|| TREE_CODE (ref1->ref) == SSA_NAME
|| DECL_P (ref1->ref)
|| TREE_CODE (ref1->ref) == STRING_CST
|| handled_component_p (ref1->ref)
|| INDIRECT_REF_P (ref1->ref)
|| TREE_CODE (ref1->ref) == MEM_REF
|| TREE_CODE (ref1->ref) == TARGET_MEM_REF)
&& (!ref2->ref
|| TREE_CODE (ref2->ref) == SSA_NAME
|| DECL_P (ref2->ref)
|| TREE_CODE (ref2->ref) == STRING_CST
|| handled_component_p (ref2->ref)
|| INDIRECT_REF_P (ref2->ref)
|| TREE_CODE (ref2->ref) == MEM_REF
|| TREE_CODE (ref2->ref) == TARGET_MEM_REF));
/* Decompose the references into their base objects and the access. */
base1 = ao_ref_base (ref1);
offset1 = ref1->offset;
max_size1 = ref1->max_size;
base2 = ao_ref_base (ref2);
offset2 = ref2->offset;
max_size2 = ref2->max_size;
/* We can end up with registers or constants as bases for example from
*D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);