forked from illumos/gcc
/
alias.c
2780 lines (2307 loc) · 82.1 KB
/
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 GNU C
Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009 Free Software Foundation, Inc.
Contributed by John Carr (jfc@mit.edu).
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 "rtl.h"
#include "tree.h"
#include "tm_p.h"
#include "function.h"
#include "alias.h"
#include "emit-rtl.h"
#include "regs.h"
#include "hard-reg-set.h"
#include "basic-block.h"
#include "flags.h"
#include "output.h"
#include "toplev.h"
#include "cselib.h"
#include "splay-tree.h"
#include "ggc.h"
#include "langhooks.h"
#include "timevar.h"
#include "target.h"
#include "cgraph.h"
#include "varray.h"
#include "tree-pass.h"
#include "ipa-type-escape.h"
#include "df.h"
#include "tree-ssa-alias.h"
#include "pointer-set.h"
#include "tree-flow.h"
/* The aliasing API provided here solves related but different problems:
Say there exists (in c)
struct X {
struct Y y1;
struct Z z2;
} x1, *px1, *px2;
struct Y y2, *py;
struct Z z2, *pz;
py = &px1.y1;
px2 = &x1;
Consider the four questions:
Can a store to x1 interfere with px2->y1?
Can a store to x1 interfere with px2->z2?
(*px2).z2
Can a store to x1 change the value pointed to by with py?
Can a store to x1 change the value pointed to by with pz?
The answer to these questions can be yes, yes, yes, and maybe.
The first two questions can be answered with a simple examination
of the type system. If structure X contains a field of type Y then
a store thru a pointer to an X can overwrite any field that is
contained (recursively) in an X (unless we know that px1 != px2).
The last two of the questions can be solved in the same way as the
first two questions but this is too conservative. The observation
is that in some cases analysis we can know if which (if any) fields
are addressed and if those addresses are used in bad ways. This
analysis may be language specific. In C, arbitrary operations may
be applied to pointers. However, there is some indication that
this may be too conservative for some C++ types.
The pass ipa-type-escape does this analysis for the types whose
instances do not escape across the compilation boundary.
Historically in GCC, these two problems were combined and a single
data structure was used to represent the solution to these
problems. We now have two similar but different data structures,
The data structure to solve the last two question is similar to the
first, but does not contain have the fields in it whose address are
never taken. For types that do escape the compilation unit, the
data structures will have identical information.
*/
/* The alias sets assigned to MEMs assist the back-end in determining
which MEMs can alias which other MEMs. In general, two MEMs in
different alias sets cannot alias each other, with one important
exception. Consider something like:
struct S { int i; double d; };
a store to an `S' can alias something of either type `int' or type
`double'. (However, a store to an `int' cannot alias a `double'
and vice versa.) We indicate this via a tree structure that looks
like:
struct S
/ \
/ \
|/_ _\|
int double
(The arrows are directed and point downwards.)
In this situation we say the alias set for `struct S' is the
`superset' and that those for `int' and `double' are `subsets'.
To see whether two alias sets can point to the same memory, we must
see if either alias set is a subset of the other. We need not trace
past immediate descendants, however, since we propagate all
grandchildren up one level.
Alias set zero is implicitly a superset of all other alias sets.
However, this is no actual entry for alias set zero. It is an
error to attempt to explicitly construct a subset of zero. */
struct GTY(()) alias_set_entry_d {
/* The alias set number, as stored in MEM_ALIAS_SET. */
alias_set_type alias_set;
/* Nonzero if would have a child of zero: this effectively makes this
alias set the same as alias set zero. */
int has_zero_child;
/* The children of the alias set. These are not just the immediate
children, but, in fact, all descendants. So, if we have:
struct T { struct S s; float f; }
continuing our example above, the children here will be all of
`int', `double', `float', and `struct S'. */
splay_tree GTY((param1_is (int), param2_is (int))) children;
};
typedef struct alias_set_entry_d *alias_set_entry;
static int rtx_equal_for_memref_p (const_rtx, const_rtx);
static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
static void record_set (rtx, const_rtx, void *);
static int base_alias_check (rtx, rtx, enum machine_mode,
enum machine_mode);
static rtx find_base_value (rtx);
static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
static int insert_subset_children (splay_tree_node, void*);
static alias_set_entry get_alias_set_entry (alias_set_type);
static const_rtx fixed_scalar_and_varying_struct_p (const_rtx, const_rtx, rtx, rtx,
bool (*) (const_rtx, bool));
static int aliases_everything_p (const_rtx);
static bool nonoverlapping_component_refs_p (const_tree, const_tree);
static tree decl_for_component_ref (tree);
static rtx adjust_offset_for_component_ref (tree, rtx);
static int write_dependence_p (const_rtx, const_rtx, int);
static void memory_modified_1 (rtx, const_rtx, void *);
/* Set up all info needed to perform alias analysis on memory references. */
/* Returns the size in bytes of the mode of X. */
#define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
/* Returns nonzero if MEM1 and MEM2 do not alias because they are in
different alias sets. We ignore alias sets in functions making use
of variable arguments because the va_arg macros on some systems are
not legal ANSI C. */
#define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
mems_in_disjoint_alias_sets_p (MEM1, MEM2)
/* Cap the number of passes we make over the insns propagating alias
information through set chains. 10 is a completely arbitrary choice. */
#define MAX_ALIAS_LOOP_PASSES 10
/* reg_base_value[N] gives an address to which register N is related.
If all sets after the first add or subtract to the current value
or otherwise modify it so it does not point to a different top level
object, reg_base_value[N] is equal to the address part of the source
of the first set.
A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
expressions represent certain special values: function arguments and
the stack, frame, and argument pointers.
The contents of an ADDRESS is not normally used, the mode of the
ADDRESS determines whether the ADDRESS is a function argument or some
other special value. Pointer equality, not rtx_equal_p, determines whether
two ADDRESS expressions refer to the same base address.
The only use of the contents of an ADDRESS is for determining if the
current function performs nonlocal memory memory references for the
purposes of marking the function as a constant function. */
static GTY(()) VEC(rtx,gc) *reg_base_value;
static rtx *new_reg_base_value;
/* We preserve the copy of old array around to avoid amount of garbage
produced. About 8% of garbage produced were attributed to this
array. */
static GTY((deletable)) VEC(rtx,gc) *old_reg_base_value;
/* Static hunks of RTL used by the aliasing code; these are initialized
once per function to avoid unnecessary RTL allocations. */
static GTY (()) rtx static_reg_base_value[FIRST_PSEUDO_REGISTER];
#define REG_BASE_VALUE(X) \
(REGNO (X) < VEC_length (rtx, reg_base_value) \
? VEC_index (rtx, reg_base_value, REGNO (X)) : 0)
/* Vector indexed by N giving the initial (unchanging) value known for
pseudo-register N. This array is initialized in init_alias_analysis,
and does not change until end_alias_analysis is called. */
static GTY((length("reg_known_value_size"))) rtx *reg_known_value;
/* Indicates number of valid entries in reg_known_value. */
static GTY(()) unsigned int reg_known_value_size;
/* Vector recording for each reg_known_value whether it is due to a
REG_EQUIV note. Future passes (viz., reload) may replace the
pseudo with the equivalent expression and so we account for the
dependences that would be introduced if that happens.
The REG_EQUIV notes created in assign_parms may mention the arg
pointer, and there are explicit insns in the RTL that modify the
arg pointer. Thus we must ensure that such insns don't get
scheduled across each other because that would invalidate the
REG_EQUIV notes. One could argue that the REG_EQUIV notes are
wrong, but solving the problem in the scheduler will likely give
better code, so we do it here. */
static bool *reg_known_equiv_p;
/* True when scanning insns from the start of the rtl to the
NOTE_INSN_FUNCTION_BEG note. */
static bool copying_arguments;
DEF_VEC_P(alias_set_entry);
DEF_VEC_ALLOC_P(alias_set_entry,gc);
/* The splay-tree used to store the various alias set entries. */
static GTY (()) VEC(alias_set_entry,gc) *alias_sets;
/* Build a decomposed reference object for querying the alias-oracle
from the MEM rtx and store it in *REF.
Returns false if MEM is not suitable for the alias-oracle. */
static bool
ao_ref_from_mem (ao_ref *ref, const_rtx mem)
{
tree expr = MEM_EXPR (mem);
tree base;
if (!expr)
return false;
/* If MEM_OFFSET or MEM_SIZE are NULL punt. */
if (!MEM_OFFSET (mem)
|| !MEM_SIZE (mem))
return false;
ao_ref_init (ref, expr);
/* Get the base of the reference and see if we have to reject or
adjust it. */
base = ao_ref_base (ref);
if (base == NULL_TREE)
return false;
/* If this is a pointer dereference of a non-SSA_NAME punt.
??? We could replace it with a pointer to anything. */
if (INDIRECT_REF_P (base)
&& TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME)
return false;
/* The tree oracle doesn't like to have these. */
if (TREE_CODE (base) == FUNCTION_DECL
|| TREE_CODE (base) == LABEL_DECL)
return false;
/* If this is a reference based on a partitioned decl replace the
base with an INDIRECT_REF of the pointer representative we
created during stack slot partitioning. */
if (TREE_CODE (base) == VAR_DECL
&& ! TREE_STATIC (base)
&& cfun->gimple_df->decls_to_pointers != NULL)
{
void *namep;
namep = pointer_map_contains (cfun->gimple_df->decls_to_pointers, base);
if (namep)
{
ref->base_alias_set = get_alias_set (base);
ref->base = build1 (INDIRECT_REF, TREE_TYPE (base), *(tree *)namep);
}
}
ref->ref_alias_set = MEM_ALIAS_SET (mem);
/* If the base decl is a parameter we can have negative MEM_OFFSET in
case of promoted subregs on bigendian targets. Trust the MEM_EXPR
here. */
if (INTVAL (MEM_OFFSET (mem)) < 0
&& ((INTVAL (MEM_SIZE (mem)) + INTVAL (MEM_OFFSET (mem)))
* BITS_PER_UNIT) == ref->size)
return true;
ref->offset += INTVAL (MEM_OFFSET (mem)) * BITS_PER_UNIT;
ref->size = INTVAL (MEM_SIZE (mem)) * BITS_PER_UNIT;
/* The MEM may extend into adjacent fields, so adjust max_size if
necessary. */
if (ref->max_size != -1
&& ref->size > ref->max_size)
ref->max_size = ref->size;
/* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
if (MEM_EXPR (mem) != get_spill_slot_decl (false)
&& (ref->offset < 0
|| (DECL_P (ref->base)
&& (!host_integerp (DECL_SIZE (ref->base), 1)
|| (TREE_INT_CST_LOW (DECL_SIZE ((ref->base)))
< (unsigned HOST_WIDE_INT)(ref->offset + ref->size))))))
return false;
return true;
}
/* Query the alias-oracle on whether the two memory rtx X and MEM may
alias. If TBAA_P is set also apply TBAA. Returns true if the
two rtxen may alias, false otherwise. */
static bool
rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
{
ao_ref ref1, ref2;
if (!ao_ref_from_mem (&ref1, x)
|| !ao_ref_from_mem (&ref2, mem))
return true;
return refs_may_alias_p_1 (&ref1, &ref2, tbaa_p);
}
/* Returns a pointer to the alias set entry for ALIAS_SET, if there is
such an entry, or NULL otherwise. */
static inline alias_set_entry
get_alias_set_entry (alias_set_type alias_set)
{
return VEC_index (alias_set_entry, alias_sets, alias_set);
}
/* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
the two MEMs cannot alias each other. */
static inline int
mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
{
/* Perform a basic sanity check. Namely, that there are no alias sets
if we're not using strict aliasing. This helps to catch bugs
whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
where a MEM is allocated in some way other than by the use of
gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
use alias sets to indicate that spilled registers cannot alias each
other, we might need to remove this check. */
gcc_assert (flag_strict_aliasing
|| (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
}
/* Insert the NODE into the splay tree given by DATA. Used by
record_alias_subset via splay_tree_foreach. */
static int
insert_subset_children (splay_tree_node node, void *data)
{
splay_tree_insert ((splay_tree) data, node->key, node->value);
return 0;
}
/* Return true if the first alias set is a subset of the second. */
bool
alias_set_subset_of (alias_set_type set1, alias_set_type set2)
{
alias_set_entry ase;
/* Everything is a subset of the "aliases everything" set. */
if (set2 == 0)
return true;
/* Otherwise, check if set1 is a subset of set2. */
ase = get_alias_set_entry (set2);
if (ase != 0
&& ((ase->has_zero_child && set1 == 0)
|| splay_tree_lookup (ase->children,
(splay_tree_key) set1)))
return true;
return false;
}
/* Return 1 if the two specified alias sets may conflict. */
int
alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
{
alias_set_entry ase;
/* The easy case. */
if (alias_sets_must_conflict_p (set1, set2))
return 1;
/* See if the first alias set is a subset of the second. */
ase = get_alias_set_entry (set1);
if (ase != 0
&& (ase->has_zero_child
|| splay_tree_lookup (ase->children,
(splay_tree_key) set2)))
return 1;
/* Now do the same, but with the alias sets reversed. */
ase = get_alias_set_entry (set2);
if (ase != 0
&& (ase->has_zero_child
|| splay_tree_lookup (ase->children,
(splay_tree_key) set1)))
return 1;
/* The two alias sets are distinct and neither one is the
child of the other. Therefore, they cannot conflict. */
return 0;
}
static int
walk_mems_2 (rtx *x, rtx mem)
{
if (MEM_P (*x))
{
if (alias_sets_conflict_p (MEM_ALIAS_SET(*x), MEM_ALIAS_SET(mem)))
return 1;
return -1;
}
return 0;
}
static int
walk_mems_1 (rtx *x, rtx *pat)
{
if (MEM_P (*x))
{
/* Visit all MEMs in *PAT and check indepedence. */
if (for_each_rtx (pat, (rtx_function) walk_mems_2, *x))
/* Indicate that dependence was determined and stop traversal. */
return 1;
return -1;
}
return 0;
}
/* Return 1 if two specified instructions have mem expr with conflict alias sets*/
bool
insn_alias_sets_conflict_p (rtx insn1, rtx insn2)
{
/* For each pair of MEMs in INSN1 and INSN2 check their independence. */
return for_each_rtx (&PATTERN (insn1), (rtx_function) walk_mems_1,
&PATTERN (insn2));
}
/* Return 1 if the two specified alias sets will always conflict. */
int
alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
{
if (set1 == 0 || set2 == 0 || set1 == set2)
return 1;
return 0;
}
/* Return 1 if any MEM object of type T1 will always conflict (using the
dependency routines in this file) with any MEM object of type T2.
This is used when allocating temporary storage. If T1 and/or T2 are
NULL_TREE, it means we know nothing about the storage. */
int
objects_must_conflict_p (tree t1, tree t2)
{
alias_set_type set1, set2;
/* If neither has a type specified, we don't know if they'll conflict
because we may be using them to store objects of various types, for
example the argument and local variables areas of inlined functions. */
if (t1 == 0 && t2 == 0)
return 0;
/* If they are the same type, they must conflict. */
if (t1 == t2
/* Likewise if both are volatile. */
|| (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
return 1;
set1 = t1 ? get_alias_set (t1) : 0;
set2 = t2 ? get_alias_set (t2) : 0;
/* We can't use alias_sets_conflict_p because we must make sure
that every subtype of t1 will conflict with every subtype of
t2 for which a pair of subobjects of these respective subtypes
overlaps on the stack. */
return alias_sets_must_conflict_p (set1, set2);
}
/* Return true if all nested component references handled by
get_inner_reference in T are such that we should use the alias set
provided by the object at the heart of T.
This is true for non-addressable components (which don't have their
own alias set), as well as components of objects in alias set zero.
This later point is a special case wherein we wish to override the
alias set used by the component, but we don't have per-FIELD_DECL
assignable alias sets. */
bool
component_uses_parent_alias_set (const_tree t)
{
while (1)
{
/* If we're at the end, it vacuously uses its own alias set. */
if (!handled_component_p (t))
return false;
switch (TREE_CODE (t))
{
case COMPONENT_REF:
if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
return true;
break;
case ARRAY_REF:
case ARRAY_RANGE_REF:
if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
return true;
break;
case REALPART_EXPR:
case IMAGPART_EXPR:
break;
default:
/* Bitfields and casts are never addressable. */
return true;
}
t = TREE_OPERAND (t, 0);
if (get_alias_set (TREE_TYPE (t)) == 0)
return true;
}
}
/* Return the alias set for the memory pointed to by T, which may be
either a type or an expression. Return -1 if there is nothing
special about dereferencing T. */
static alias_set_type
get_deref_alias_set_1 (tree t)
{
/* If we're not doing any alias analysis, just assume everything
aliases everything else. */
if (!flag_strict_aliasing)
return 0;
/* All we care about is the type. */
if (! TYPE_P (t))
t = TREE_TYPE (t);
/* If we have an INDIRECT_REF via a void pointer, we don't
know anything about what that might alias. Likewise if the
pointer is marked that way. */
if (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
|| TYPE_REF_CAN_ALIAS_ALL (t))
return 0;
return -1;
}
/* Return the alias set for the memory pointed to by T, which may be
either a type or an expression. */
alias_set_type
get_deref_alias_set (tree t)
{
alias_set_type set = get_deref_alias_set_1 (t);
/* Fall back to the alias-set of the pointed-to type. */
if (set == -1)
{
if (! TYPE_P (t))
t = TREE_TYPE (t);
set = get_alias_set (TREE_TYPE (t));
}
return set;
}
/* Return the alias set for T, which may be either a type or an
expression. Call language-specific routine for help, if needed. */
alias_set_type
get_alias_set (tree t)
{
alias_set_type set;
/* If we're not doing any alias analysis, just assume everything
aliases everything else. Also return 0 if this or its type is
an error. */
if (! flag_strict_aliasing || t == error_mark_node
|| (! TYPE_P (t)
&& (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
return 0;
/* We can be passed either an expression or a type. This and the
language-specific routine may make mutually-recursive calls to each other
to figure out what to do. At each juncture, we see if this is a tree
that the language may need to handle specially. First handle things that
aren't types. */
if (! TYPE_P (t))
{
tree inner;
/* Remove any nops, then give the language a chance to do
something with this tree before we look at it. */
STRIP_NOPS (t);
set = lang_hooks.get_alias_set (t);
if (set != -1)
return set;
/* Retrieve the original memory reference if needed. */
if (TREE_CODE (t) == TARGET_MEM_REF)
t = TMR_ORIGINAL (t);
/* First see if the actual object referenced is an INDIRECT_REF from a
restrict-qualified pointer or a "void *". */
inner = t;
while (handled_component_p (inner))
{
inner = TREE_OPERAND (inner, 0);
STRIP_NOPS (inner);
}
if (INDIRECT_REF_P (inner))
{
set = get_deref_alias_set_1 (TREE_OPERAND (inner, 0));
if (set != -1)
return set;
}
/* Otherwise, pick up the outermost object that we could have a pointer
to, processing conversions as above. */
while (component_uses_parent_alias_set (t))
{
t = TREE_OPERAND (t, 0);
STRIP_NOPS (t);
}
/* If we've already determined the alias set for a decl, just return
it. This is necessary for C++ anonymous unions, whose component
variables don't look like union members (boo!). */
if (TREE_CODE (t) == VAR_DECL
&& DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
return MEM_ALIAS_SET (DECL_RTL (t));
/* Now all we care about is the type. */
t = TREE_TYPE (t);
}
/* Variant qualifiers don't affect the alias set, so get the main
variant. */
t = TYPE_MAIN_VARIANT (t);
/* Always use the canonical type as well. If this is a type that
requires structural comparisons to identify compatible types
use alias set zero. */
if (TYPE_STRUCTURAL_EQUALITY_P (t))
{
/* Allow the language to specify another alias set for this
type. */
set = lang_hooks.get_alias_set (t);
if (set != -1)
return set;
return 0;
}
t = TYPE_CANONICAL (t);
/* Canonical types shouldn't form a tree nor should the canonical
type require structural equality checks. */
gcc_assert (!TYPE_STRUCTURAL_EQUALITY_P (t) && TYPE_CANONICAL (t) == t);
/* If this is a type with a known alias set, return it. */
if (TYPE_ALIAS_SET_KNOWN_P (t))
return TYPE_ALIAS_SET (t);
/* We don't want to set TYPE_ALIAS_SET for incomplete types. */
if (!COMPLETE_TYPE_P (t))
{
/* For arrays with unknown size the conservative answer is the
alias set of the element type. */
if (TREE_CODE (t) == ARRAY_TYPE)
return get_alias_set (TREE_TYPE (t));
/* But return zero as a conservative answer for incomplete types. */
return 0;
}
/* See if the language has special handling for this type. */
set = lang_hooks.get_alias_set (t);
if (set != -1)
return set;
/* There are no objects of FUNCTION_TYPE, so there's no point in
using up an alias set for them. (There are, of course, pointers
and references to functions, but that's different.) */
else if (TREE_CODE (t) == FUNCTION_TYPE
|| TREE_CODE (t) == METHOD_TYPE)
set = 0;
/* Unless the language specifies otherwise, let vector types alias
their components. This avoids some nasty type punning issues in
normal usage. And indeed lets vectors be treated more like an
array slice. */
else if (TREE_CODE (t) == VECTOR_TYPE)
set = get_alias_set (TREE_TYPE (t));
/* Unless the language specifies otherwise, treat array types the
same as their components. This avoids the asymmetry we get
through recording the components. Consider accessing a
character(kind=1) through a reference to a character(kind=1)[1:1].
Or consider if we want to assign integer(kind=4)[0:D.1387] and
integer(kind=4)[4] the same alias set or not.
Just be pragmatic here and make sure the array and its element
type get the same alias set assigned. */
else if (TREE_CODE (t) == ARRAY_TYPE
&& !TYPE_NONALIASED_COMPONENT (t))
set = get_alias_set (TREE_TYPE (t));
else
/* Otherwise make a new alias set for this type. */
set = new_alias_set ();
TYPE_ALIAS_SET (t) = set;
/* If this is an aggregate type, we must record any component aliasing
information. */
if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
record_component_aliases (t);
return set;
}
/* Return a brand-new alias set. */
alias_set_type
new_alias_set (void)
{
if (flag_strict_aliasing)
{
if (alias_sets == 0)
VEC_safe_push (alias_set_entry, gc, alias_sets, 0);
VEC_safe_push (alias_set_entry, gc, alias_sets, 0);
return VEC_length (alias_set_entry, alias_sets) - 1;
}
else
return 0;
}
/* Indicate that things in SUBSET can alias things in SUPERSET, but that
not everything that aliases SUPERSET also aliases SUBSET. For example,
in C, a store to an `int' can alias a load of a structure containing an
`int', and vice versa. But it can't alias a load of a 'double' member
of the same structure. Here, the structure would be the SUPERSET and
`int' the SUBSET. This relationship is also described in the comment at
the beginning of this file.
This function should be called only once per SUPERSET/SUBSET pair.
It is illegal for SUPERSET to be zero; everything is implicitly a
subset of alias set zero. */
void
record_alias_subset (alias_set_type superset, alias_set_type subset)
{
alias_set_entry superset_entry;
alias_set_entry subset_entry;
/* It is possible in complex type situations for both sets to be the same,
in which case we can ignore this operation. */
if (superset == subset)
return;
gcc_assert (superset);
superset_entry = get_alias_set_entry (superset);
if (superset_entry == 0)
{
/* Create an entry for the SUPERSET, so that we have a place to
attach the SUBSET. */
superset_entry = GGC_NEW (struct alias_set_entry_d);
superset_entry->alias_set = superset;
superset_entry->children
= splay_tree_new_ggc (splay_tree_compare_ints);
superset_entry->has_zero_child = 0;
VEC_replace (alias_set_entry, alias_sets, superset, superset_entry);
}
if (subset == 0)
superset_entry->has_zero_child = 1;
else
{
subset_entry = get_alias_set_entry (subset);
/* If there is an entry for the subset, enter all of its children
(if they are not already present) as children of the SUPERSET. */
if (subset_entry)
{
if (subset_entry->has_zero_child)
superset_entry->has_zero_child = 1;
splay_tree_foreach (subset_entry->children, insert_subset_children,
superset_entry->children);
}
/* Enter the SUBSET itself as a child of the SUPERSET. */
splay_tree_insert (superset_entry->children,
(splay_tree_key) subset, 0);
}
}
/* Record that component types of TYPE, if any, are part of that type for
aliasing purposes. For record types, we only record component types
for fields that are not marked non-addressable. For array types, we
only record the component type if it is not marked non-aliased. */
void
record_component_aliases (tree type)
{
alias_set_type superset = get_alias_set (type);
tree field;
if (superset == 0)
return;
switch (TREE_CODE (type))
{
case RECORD_TYPE:
case UNION_TYPE:
case QUAL_UNION_TYPE:
/* Recursively record aliases for the base classes, if there are any. */
if (TYPE_BINFO (type))
{
int i;
tree binfo, base_binfo;
for (binfo = TYPE_BINFO (type), i = 0;
BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
record_alias_subset (superset,
get_alias_set (BINFO_TYPE (base_binfo)));
}
for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
break;
case COMPLEX_TYPE:
record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
break;
/* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
element type. */
default:
break;
}
}
/* Allocate an alias set for use in storing and reading from the varargs
spill area. */
static GTY(()) alias_set_type varargs_set = -1;
alias_set_type
get_varargs_alias_set (void)
{
#if 1
/* We now lower VA_ARG_EXPR, and there's currently no way to attach the
varargs alias set to an INDIRECT_REF (FIXME!), so we can't
consistently use the varargs alias set for loads from the varargs
area. So don't use it anywhere. */
return 0;
#else
if (varargs_set == -1)
varargs_set = new_alias_set ();
return varargs_set;
#endif
}
/* Likewise, but used for the fixed portions of the frame, e.g., register
save areas. */
static GTY(()) alias_set_type frame_set = -1;
alias_set_type
get_frame_alias_set (void)
{
if (frame_set == -1)
frame_set = new_alias_set ();
return frame_set;
}
/* Inside SRC, the source of a SET, find a base address. */
static rtx
find_base_value (rtx src)
{
unsigned int regno;
#if defined (FIND_BASE_TERM)
/* Try machine-dependent ways to find the base term. */
src = FIND_BASE_TERM (src);
#endif
switch (GET_CODE (src))
{
case SYMBOL_REF:
case LABEL_REF:
return src;
case REG:
regno = REGNO (src);
/* At the start of a function, argument registers have known base
values which may be lost later. Returning an ADDRESS
expression here allows optimization based on argument values
even when the argument registers are used for other purposes. */
if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
return new_reg_base_value[regno];
/* If a pseudo has a known base value, return it. Do not do this
for non-fixed hard regs since it can result in a circular
dependency chain for registers which have values at function entry.
The test above is not sufficient because the scheduler may move
a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
&& regno < VEC_length (rtx, reg_base_value))
{
/* If we're inside init_alias_analysis, use new_reg_base_value
to reduce the number of relaxation iterations. */
if (new_reg_base_value && new_reg_base_value[regno]
&& DF_REG_DEF_COUNT (regno) == 1)
return new_reg_base_value[regno];
if (VEC_index (rtx, reg_base_value, regno))
return VEC_index (rtx, reg_base_value, regno);
}
return 0;
case MEM:
/* Check for an argument passed in memory. Only record in the
copying-arguments block; it is too hard to track changes
otherwise. */
if (copying_arguments
&& (XEXP (src, 0) == arg_pointer_rtx
|| (GET_CODE (XEXP (src, 0)) == PLUS
&& XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
return gen_rtx_ADDRESS (VOIDmode, src);
return 0;
case CONST:
src = XEXP (src, 0);
if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
break;
/* ... fall through ... */
case PLUS:
case MINUS: