]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/alias.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / alias.c
CommitLineData
9ae8ffe7 1/* Alias analysis for GNU C
5624e564 2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
9ae8ffe7
JL
3 Contributed by John Carr (jfc@mit.edu).
4
1322177d 5This file is part of GCC.
9ae8ffe7 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9dcd6f09 9Software Foundation; either version 3, or (at your option) any later
1322177d 10version.
9ae8ffe7 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
9ae8ffe7
JL
16
17You should have received a copy of the GNU General Public License
9dcd6f09
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
9ae8ffe7
JL
20
21#include "config.h"
670ee920 22#include "system.h"
4977bab6 23#include "coretypes.h"
c7131fb2
AM
24#include "backend.h"
25#include "tree.h"
26#include "gimple.h"
9ae8ffe7 27#include "rtl.h"
c7131fb2 28#include "df.h"
40e23961 29#include "alias.h"
40e23961 30#include "fold-const.h"
d8a2d370 31#include "varasm.h"
36566b39 32#include "flags.h"
36566b39
PK
33#include "insn-config.h"
34#include "expmed.h"
35#include "dojump.h"
36#include "explow.h"
37#include "calls.h"
78528714 38#include "emit-rtl.h"
36566b39
PK
39#include "stmt.h"
40#include "expr.h"
41#include "tm_p.h"
9ae8ffe7 42#include "regs.h"
718f9c0f 43#include "diagnostic-core.h"
a78a26f1 44#include "alloc-pool.h"
eab5c70a 45#include "cselib.h"
d23c55c2 46#include "langhooks.h"
0d446150 47#include "timevar.h"
7ee2468b 48#include "dumpfile.h"
ab780373 49#include "target.h"
60393bbc 50#include "cfganal.h"
2fb9a547 51#include "internal-fn.h"
442b4905 52#include "gimple-ssa.h"
403837b4 53#include "rtl-iter.h"
ea900239
DB
54
55/* The aliasing API provided here solves related but different problems:
56
c22cacf3 57 Say there exists (in c)
ea900239
DB
58
59 struct X {
60 struct Y y1;
61 struct Z z2;
62 } x1, *px1, *px2;
63
64 struct Y y2, *py;
65 struct Z z2, *pz;
66
67
308a3fe2 68 py = &x1.y1;
ea900239
DB
69 px2 = &x1;
70
71 Consider the four questions:
72
73 Can a store to x1 interfere with px2->y1?
74 Can a store to x1 interfere with px2->z2?
ea900239
DB
75 Can a store to x1 change the value pointed to by with py?
76 Can a store to x1 change the value pointed to by with pz?
77
78 The answer to these questions can be yes, yes, yes, and maybe.
79
80 The first two questions can be answered with a simple examination
81 of the type system. If structure X contains a field of type Y then
073a8998 82 a store through a pointer to an X can overwrite any field that is
ea900239
DB
83 contained (recursively) in an X (unless we know that px1 != px2).
84
308a3fe2
DS
85 The last two questions can be solved in the same way as the first
86 two questions but this is too conservative. The observation is
87 that in some cases we can know which (if any) fields are addressed
88 and if those addresses are used in bad ways. This analysis may be
89 language specific. In C, arbitrary operations may be applied to
90 pointers. However, there is some indication that this may be too
91 conservative for some C++ types.
ea900239
DB
92
93 The pass ipa-type-escape does this analysis for the types whose
c22cacf3 94 instances do not escape across the compilation boundary.
ea900239
DB
95
96 Historically in GCC, these two problems were combined and a single
308a3fe2 97 data structure that was used to represent the solution to these
ea900239 98 problems. We now have two similar but different data structures,
308a3fe2
DS
99 The data structure to solve the last two questions is similar to
100 the first, but does not contain the fields whose address are never
101 taken. For types that do escape the compilation unit, the data
102 structures will have identical information.
ea900239 103*/
3932261a
MM
104
105/* The alias sets assigned to MEMs assist the back-end in determining
106 which MEMs can alias which other MEMs. In general, two MEMs in
ac3d9668
RK
107 different alias sets cannot alias each other, with one important
108 exception. Consider something like:
3932261a 109
01d28c3f 110 struct S { int i; double d; };
3932261a
MM
111
112 a store to an `S' can alias something of either type `int' or type
113 `double'. (However, a store to an `int' cannot alias a `double'
114 and vice versa.) We indicate this via a tree structure that looks
115 like:
c22cacf3
MS
116 struct S
117 / \
3932261a 118 / \
c22cacf3
MS
119 |/_ _\|
120 int double
3932261a 121
ac3d9668
RK
122 (The arrows are directed and point downwards.)
123 In this situation we say the alias set for `struct S' is the
124 `superset' and that those for `int' and `double' are `subsets'.
125
3bdf5ad1
RK
126 To see whether two alias sets can point to the same memory, we must
127 see if either alias set is a subset of the other. We need not trace
95bd1dd7 128 past immediate descendants, however, since we propagate all
3bdf5ad1 129 grandchildren up one level.
3932261a
MM
130
131 Alias set zero is implicitly a superset of all other alias sets.
132 However, this is no actual entry for alias set zero. It is an
133 error to attempt to explicitly construct a subset of zero. */
134
e0702244 135struct alias_set_hash : int_hash <int, INT_MIN, INT_MIN + 1> {};
de144fb2 136
7e5487a2 137struct GTY(()) alias_set_entry_d {
3932261a 138 /* The alias set number, as stored in MEM_ALIAS_SET. */
4862826d 139 alias_set_type alias_set;
3932261a
MM
140
141 /* The children of the alias set. These are not just the immediate
95bd1dd7 142 children, but, in fact, all descendants. So, if we have:
3932261a 143
ca7fd9cd 144 struct T { struct S s; float f; }
3932261a
MM
145
146 continuing our example above, the children here will be all of
147 `int', `double', `float', and `struct S'. */
fb5c464a 148 hash_map<alias_set_hash, int> *children;
6e042ef4
JH
149
150 /* Nonzero if would have a child of zero: this effectively makes this
151 alias set the same as alias set zero. */
152 bool has_zero_child;
153 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
154 aggregate contaiing pointer.
155 This is used for a special case where we need an universal pointer type
156 compatible with all other pointer types. */
157 bool is_pointer;
158 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
159 bool has_pointer;
b604074c 160};
7e5487a2 161typedef struct alias_set_entry_d *alias_set_entry;
9ae8ffe7 162
ed7a4b4b 163static int rtx_equal_for_memref_p (const_rtx, const_rtx);
4682ae04 164static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
7bc980e1 165static void record_set (rtx, const_rtx, void *);
ef4bddc2
RS
166static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
167 machine_mode);
4682ae04 168static rtx find_base_value (rtx);
4f588890 169static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
4862826d 170static alias_set_entry get_alias_set_entry (alias_set_type);
4682ae04 171static tree decl_for_component_ref (tree);
bd280792 172static int write_dependence_p (const_rtx,
ef4bddc2 173 const_rtx, machine_mode, rtx,
bd280792 174 bool, bool, bool);
4682ae04 175
aa317c97 176static void memory_modified_1 (rtx, const_rtx, void *);
9ae8ffe7 177
3ecf9d13
JH
178/* Query statistics for the different low-level disambiguators.
179 A high-level query may trigger multiple of them. */
180
181static struct {
182 unsigned long long num_alias_zero;
183 unsigned long long num_same_alias_set;
184 unsigned long long num_same_objects;
185 unsigned long long num_volatile;
186 unsigned long long num_dag;
6e042ef4 187 unsigned long long num_universal;
3ecf9d13
JH
188 unsigned long long num_disambiguated;
189} alias_stats;
190
191
9ae8ffe7
JL
192/* Set up all info needed to perform alias analysis on memory references. */
193
d4b60170 194/* Returns the size in bytes of the mode of X. */
9ae8ffe7
JL
195#define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
196
ea64ef27 197/* Cap the number of passes we make over the insns propagating alias
131db6b8
SB
198 information through set chains.
199 ??? 10 is a completely arbitrary choice. This should be based on the
200 maximum loop depth in the CFG, but we do not have this information
201 available (even if current_loops _is_ available). */
ea64ef27 202#define MAX_ALIAS_LOOP_PASSES 10
ca7fd9cd 203
9ae8ffe7
JL
204/* reg_base_value[N] gives an address to which register N is related.
205 If all sets after the first add or subtract to the current value
206 or otherwise modify it so it does not point to a different top level
207 object, reg_base_value[N] is equal to the address part of the source
2a2c8203
JC
208 of the first set.
209
210 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
9fc37b2b 211 expressions represent three types of base:
b3b5ad95 212
9fc37b2b
RS
213 1. incoming arguments. There is just one ADDRESS to represent all
214 arguments, since we do not know at this level whether accesses
215 based on different arguments can alias. The ADDRESS has id 0.
b3b5ad95 216
9fc37b2b
RS
217 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
218 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
219 Each of these rtxes has a separate ADDRESS associated with it,
220 each with a negative id.
221
222 GCC is (and is required to be) precise in which register it
223 chooses to access a particular region of stack. We can therefore
224 assume that accesses based on one of these rtxes do not alias
225 accesses based on another of these rtxes.
226
227 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
228 Each such piece of memory has a separate ADDRESS associated
229 with it, each with an id greater than 0.
230
231 Accesses based on one ADDRESS do not alias accesses based on other
232 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
233 alias globals either; the ADDRESSes have Pmode to indicate this.
234 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
235 indicate this. */
2a2c8203 236
9771b263 237static GTY(()) vec<rtx, va_gc> *reg_base_value;
ac606739 238static rtx *new_reg_base_value;
c582d54a 239
9fc37b2b
RS
240/* The single VOIDmode ADDRESS that represents all argument bases.
241 It has id 0. */
242static GTY(()) rtx arg_base_value;
243
244/* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
245static int unique_id;
246
c582d54a
JH
247/* We preserve the copy of old array around to avoid amount of garbage
248 produced. About 8% of garbage produced were attributed to this
249 array. */
9771b263 250static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
d4b60170 251
9e412ca3
RS
252/* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
253 registers. */
254#define UNIQUE_BASE_VALUE_SP -1
255#define UNIQUE_BASE_VALUE_ARGP -2
256#define UNIQUE_BASE_VALUE_FP -3
257#define UNIQUE_BASE_VALUE_HFP -4
258
7bf84454
RS
259#define static_reg_base_value \
260 (this_target_rtl->x_static_reg_base_value)
bf1660a6 261
9771b263
DN
262#define REG_BASE_VALUE(X) \
263 (REGNO (X) < vec_safe_length (reg_base_value) \
264 ? (*reg_base_value)[REGNO (X)] : 0)
9ae8ffe7 265
c13e8210 266/* Vector indexed by N giving the initial (unchanging) value known for
9ff3c7ca 267 pseudo-register N. This vector is initialized in init_alias_analysis,
bb1acb3e 268 and does not change until end_alias_analysis is called. */
9771b263 269static GTY(()) vec<rtx, va_gc> *reg_known_value;
9ae8ffe7
JL
270
271/* Vector recording for each reg_known_value whether it is due to a
272 REG_EQUIV note. Future passes (viz., reload) may replace the
273 pseudo with the equivalent expression and so we account for the
ac3d9668
RK
274 dependences that would be introduced if that happens.
275
276 The REG_EQUIV notes created in assign_parms may mention the arg
277 pointer, and there are explicit insns in the RTL that modify the
278 arg pointer. Thus we must ensure that such insns don't get
279 scheduled across each other because that would invalidate the
280 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
281 wrong, but solving the problem in the scheduler will likely give
282 better code, so we do it here. */
9ff3c7ca 283static sbitmap reg_known_equiv_p;
9ae8ffe7 284
2a2c8203
JC
285/* True when scanning insns from the start of the rtl to the
286 NOTE_INSN_FUNCTION_BEG note. */
83bbd9b6 287static bool copying_arguments;
9ae8ffe7 288
1a5640b4 289
3932261a 290/* The splay-tree used to store the various alias set entries. */
9771b263 291static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
ac3d9668 292\f
55b34b5f
RG
293/* Build a decomposed reference object for querying the alias-oracle
294 from the MEM rtx and store it in *REF.
295 Returns false if MEM is not suitable for the alias-oracle. */
296
297static bool
298ao_ref_from_mem (ao_ref *ref, const_rtx mem)
299{
300 tree expr = MEM_EXPR (mem);
301 tree base;
302
303 if (!expr)
304 return false;
305
306 ao_ref_init (ref, expr);
307
308 /* Get the base of the reference and see if we have to reject or
309 adjust it. */
310 base = ao_ref_base (ref);
311 if (base == NULL_TREE)
312 return false;
313
ef7a9fb8
RB
314 /* The tree oracle doesn't like bases that are neither decls
315 nor indirect references of SSA names. */
316 if (!(DECL_P (base)
317 || (TREE_CODE (base) == MEM_REF
318 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
319 || (TREE_CODE (base) == TARGET_MEM_REF
320 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
d15adbeb 321 return false;
55b34b5f
RG
322
323 /* If this is a reference based on a partitioned decl replace the
ef7a9fb8 324 base with a MEM_REF of the pointer representative we
55b34b5f
RG
325 created during stack slot partitioning. */
326 if (TREE_CODE (base) == VAR_DECL
ef7a9fb8 327 && ! is_global_var (base)
55b34b5f
RG
328 && cfun->gimple_df->decls_to_pointers != NULL)
329 {
39c8aaa4 330 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
55b34b5f 331 if (namep)
39c8aaa4 332 ref->base = build_simple_mem_ref (*namep);
d15adbeb 333 }
55b34b5f
RG
334
335 ref->ref_alias_set = MEM_ALIAS_SET (mem);
336
f68396a1
RG
337 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
338 is conservative, so trust it. */
527210c4 339 if (!MEM_OFFSET_KNOWN_P (mem)
f5541398 340 || !MEM_SIZE_KNOWN_P (mem))
f68396a1 341 return true;
366f945f 342
b0e96404
RG
343 /* If the base decl is a parameter we can have negative MEM_OFFSET in
344 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
345 here. */
527210c4
RS
346 if (MEM_OFFSET (mem) < 0
347 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
b0e96404
RG
348 return true;
349
f68396a1
RG
350 /* Otherwise continue and refine size and offset we got from analyzing
351 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
352
527210c4 353 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
f5541398 354 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
b0e96404
RG
355
356 /* The MEM may extend into adjacent fields, so adjust max_size if
357 necessary. */
358 if (ref->max_size != -1
359 && ref->size > ref->max_size)
360 ref->max_size = ref->size;
361
362 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
363 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
364 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
365 && (ref->offset < 0
366 || (DECL_P (ref->base)
807e902e
KZ
367 && (DECL_SIZE (ref->base) == NULL_TREE
368 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
369 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
370 ref->offset + ref->size)))))
b0e96404 371 return false;
55b34b5f
RG
372
373 return true;
374}
375
376/* Query the alias-oracle on whether the two memory rtx X and MEM may
377 alias. If TBAA_P is set also apply TBAA. Returns true if the
378 two rtxen may alias, false otherwise. */
379
380static bool
381rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
382{
383 ao_ref ref1, ref2;
384
385 if (!ao_ref_from_mem (&ref1, x)
386 || !ao_ref_from_mem (&ref2, mem))
387 return true;
388
55e3bc4c
RG
389 return refs_may_alias_p_1 (&ref1, &ref2,
390 tbaa_p
391 && MEM_ALIAS_SET (x) != 0
392 && MEM_ALIAS_SET (mem) != 0);
55b34b5f
RG
393}
394
3932261a
MM
395/* Returns a pointer to the alias set entry for ALIAS_SET, if there is
396 such an entry, or NULL otherwise. */
397
9ddb66ca 398static inline alias_set_entry
4862826d 399get_alias_set_entry (alias_set_type alias_set)
3932261a 400{
9771b263 401 return (*alias_sets)[alias_set];
3932261a
MM
402}
403
ac3d9668
RK
404/* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
405 the two MEMs cannot alias each other. */
3932261a 406
9ddb66ca 407static inline int
4f588890 408mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
3932261a 409{
598f8eca
RB
410 return (flag_strict_aliasing
411 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
412 MEM_ALIAS_SET (mem2)));
1da68f56 413}
3932261a 414
c58936b6
DB
415/* Return true if the first alias set is a subset of the second. */
416
417bool
4862826d 418alias_set_subset_of (alias_set_type set1, alias_set_type set2)
c58936b6 419{
6e042ef4 420 alias_set_entry ase2;
c58936b6
DB
421
422 /* Everything is a subset of the "aliases everything" set. */
423 if (set2 == 0)
424 return true;
425
6e042ef4
JH
426 /* Check if set1 is a subset of set2. */
427 ase2 = get_alias_set_entry (set2);
428 if (ase2 != 0
429 && (ase2->has_zero_child
430 || (ase2->children && ase2->children->get (set1))))
c58936b6 431 return true;
6e042ef4
JH
432
433 /* As a special case we consider alias set of "void *" to be both subset
434 and superset of every alias set of a pointer. This extra symmetry does
435 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
436 to return true on the following testcase:
437
438 void *ptr;
439 char **ptr2=(char **)&ptr;
440 *ptr2 = ...
441
442 Additionally if a set contains universal pointer, we consider every pointer
443 to be a subset of it, but we do not represent this explicitely - doing so
444 would require us to update transitive closure each time we introduce new
445 pointer type. This makes aliasing_component_refs_p to return true
446 on the following testcase:
447
448 struct a {void *ptr;}
449 char **ptr = (char **)&a.ptr;
450 ptr = ...
451
452 This makes void * truly universal pointer type. See pointer handling in
453 get_alias_set for more details. */
454 if (ase2 && ase2->has_pointer)
455 {
456 alias_set_entry ase1 = get_alias_set_entry (set1);
457
458 if (ase1 && ase1->is_pointer)
459 {
460 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
461 /* If one is ptr_type_node and other is pointer, then we consider
462 them subset of each other. */
463 if (set1 == voidptr_set || set2 == voidptr_set)
464 return true;
465 /* If SET2 contains universal pointer's alias set, then we consdier
466 every (non-universal) pointer. */
467 if (ase2->children && set1 != voidptr_set
468 && ase2->children->get (voidptr_set))
469 return true;
470 }
471 }
c58936b6
DB
472 return false;
473}
474
1da68f56
RK
475/* Return 1 if the two specified alias sets may conflict. */
476
477int
4862826d 478alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
1da68f56 479{
6e042ef4
JH
480 alias_set_entry ase1;
481 alias_set_entry ase2;
1da68f56 482
836f7794
EB
483 /* The easy case. */
484 if (alias_sets_must_conflict_p (set1, set2))
1da68f56 485 return 1;
3932261a 486
3bdf5ad1 487 /* See if the first alias set is a subset of the second. */
6e042ef4
JH
488 ase1 = get_alias_set_entry (set1);
489 if (ase1 != 0
490 && ase1->children && ase1->children->get (set2))
3ecf9d13
JH
491 {
492 ++alias_stats.num_dag;
493 return 1;
494 }
3932261a
MM
495
496 /* Now do the same, but with the alias sets reversed. */
6e042ef4
JH
497 ase2 = get_alias_set_entry (set2);
498 if (ase2 != 0
499 && ase2->children && ase2->children->get (set1))
3ecf9d13
JH
500 {
501 ++alias_stats.num_dag;
502 return 1;
503 }
6e042ef4
JH
504
505 /* We want void * to be compatible with any other pointer without
506 really dropping it to alias set 0. Doing so would make it
507 compatible with all non-pointer types too.
508
509 This is not strictly necessary by the C/C++ language
510 standards, but avoids common type punning mistakes. In
511 addition to that, we need the existence of such universal
512 pointer to implement Fortran's C_PTR type (which is defined as
513 type compatible with all C pointers). */
514 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
515 {
516 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
517
518 /* If one of the sets corresponds to universal pointer,
519 we consider it to conflict with anything that is
520 or contains pointer. */
521 if (set1 == voidptr_set || set2 == voidptr_set)
522 {
523 ++alias_stats.num_universal;
524 return true;
525 }
526 /* If one of sets is (non-universal) pointer and the other
527 contains universal pointer, we also get conflict. */
528 if (ase1->is_pointer && set2 != voidptr_set
529 && ase2->children && ase2->children->get (voidptr_set))
530 {
531 ++alias_stats.num_universal;
532 return true;
533 }
534 if (ase2->is_pointer && set1 != voidptr_set
535 && ase1->children && ase1->children->get (voidptr_set))
536 {
537 ++alias_stats.num_universal;
538 return true;
539 }
540 }
541
3ecf9d13 542 ++alias_stats.num_disambiguated;
3932261a 543
1da68f56 544 /* The two alias sets are distinct and neither one is the
836f7794 545 child of the other. Therefore, they cannot conflict. */
1da68f56 546 return 0;
3932261a 547}
5399d643 548
836f7794 549/* Return 1 if the two specified alias sets will always conflict. */
5399d643
JW
550
551int
4862826d 552alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
5399d643 553{
3ecf9d13
JH
554 if (set1 == 0 || set2 == 0)
555 {
556 ++alias_stats.num_alias_zero;
557 return 1;
558 }
559 if (set1 == set2)
560 {
561 ++alias_stats.num_same_alias_set;
562 return 1;
563 }
5399d643
JW
564
565 return 0;
566}
567
1da68f56
RK
568/* Return 1 if any MEM object of type T1 will always conflict (using the
569 dependency routines in this file) with any MEM object of type T2.
570 This is used when allocating temporary storage. If T1 and/or T2 are
571 NULL_TREE, it means we know nothing about the storage. */
572
573int
4682ae04 574objects_must_conflict_p (tree t1, tree t2)
1da68f56 575{
4862826d 576 alias_set_type set1, set2;
82d610ec 577
e8ea2809
RK
578 /* If neither has a type specified, we don't know if they'll conflict
579 because we may be using them to store objects of various types, for
580 example the argument and local variables areas of inlined functions. */
981a4c34 581 if (t1 == 0 && t2 == 0)
e8ea2809
RK
582 return 0;
583
1da68f56 584 /* If they are the same type, they must conflict. */
3ecf9d13
JH
585 if (t1 == t2)
586 {
587 ++alias_stats.num_same_objects;
588 return 1;
589 }
590 /* Likewise if both are volatile. */
591 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
592 {
593 ++alias_stats.num_volatile;
594 return 1;
595 }
1da68f56 596
82d610ec
RK
597 set1 = t1 ? get_alias_set (t1) : 0;
598 set2 = t2 ? get_alias_set (t2) : 0;
1da68f56 599
836f7794
EB
600 /* We can't use alias_sets_conflict_p because we must make sure
601 that every subtype of t1 will conflict with every subtype of
82d610ec
RK
602 t2 for which a pair of subobjects of these respective subtypes
603 overlaps on the stack. */
836f7794 604 return alias_sets_must_conflict_p (set1, set2);
1da68f56
RK
605}
606\f
b4ada065
RB
607/* Return the outermost parent of component present in the chain of
608 component references handled by get_inner_reference in T with the
609 following property:
610 - the component is non-addressable, or
611 - the parent has alias set zero,
612 or NULL_TREE if no such parent exists. In the former cases, the alias
613 set of this parent is the alias set that must be used for T itself. */
614
615tree
616component_uses_parent_alias_set_from (const_tree t)
6e24b709 617{
b4ada065 618 const_tree found = NULL_TREE;
afe84921 619
b4ada065
RB
620 while (handled_component_p (t))
621 {
afe84921
RH
622 switch (TREE_CODE (t))
623 {
624 case COMPONENT_REF:
625 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
b4ada065 626 found = t;
afe84921
RH
627 break;
628
629 case ARRAY_REF:
630 case ARRAY_RANGE_REF:
631 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
b4ada065 632 found = t;
afe84921
RH
633 break;
634
635 case REALPART_EXPR:
636 case IMAGPART_EXPR:
637 break;
638
b4ada065
RB
639 case BIT_FIELD_REF:
640 case VIEW_CONVERT_EXPR:
afe84921 641 /* Bitfields and casts are never addressable. */
b4ada065
RB
642 found = t;
643 break;
644
645 default:
646 gcc_unreachable ();
afe84921
RH
647 }
648
b4ada065
RB
649 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
650 found = t;
651
afe84921
RH
652 t = TREE_OPERAND (t, 0);
653 }
b4ada065
RB
654
655 if (found)
656 return TREE_OPERAND (found, 0);
657
658 return NULL_TREE;
6e24b709
RK
659}
660
f40333af
RB
661
662/* Return whether the pointer-type T effective for aliasing may
663 access everything and thus the reference has to be assigned
664 alias-set zero. */
665
666static bool
667ref_all_alias_ptr_type_p (const_tree t)
668{
669 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
670 || TYPE_REF_CAN_ALIAS_ALL (t));
671}
672
5006671f
RG
673/* Return the alias set for the memory pointed to by T, which may be
674 either a type or an expression. Return -1 if there is nothing
675 special about dereferencing T. */
676
677static alias_set_type
678get_deref_alias_set_1 (tree t)
679{
5b21f0f3 680 /* All we care about is the type. */
5006671f 681 if (! TYPE_P (t))
5b21f0f3 682 t = TREE_TYPE (t);
5006671f
RG
683
684 /* If we have an INDIRECT_REF via a void pointer, we don't
685 know anything about what that might alias. Likewise if the
686 pointer is marked that way. */
f40333af 687 if (ref_all_alias_ptr_type_p (t))
5006671f
RG
688 return 0;
689
690 return -1;
691}
692
693/* Return the alias set for the memory pointed to by T, which may be
694 either a type or an expression. */
695
696alias_set_type
697get_deref_alias_set (tree t)
698{
f40333af
RB
699 /* If we're not doing any alias analysis, just assume everything
700 aliases everything else. */
701 if (!flag_strict_aliasing)
702 return 0;
703
5006671f
RG
704 alias_set_type set = get_deref_alias_set_1 (t);
705
706 /* Fall back to the alias-set of the pointed-to type. */
707 if (set == -1)
708 {
709 if (! TYPE_P (t))
710 t = TREE_TYPE (t);
711 set = get_alias_set (TREE_TYPE (t));
712 }
713
714 return set;
715}
716
f40333af
RB
717/* Return the pointer-type relevant for TBAA purposes from the
718 memory reference tree *T or NULL_TREE in which case *T is
719 adjusted to point to the outermost component reference that
720 can be used for assigning an alias set. */
721
722static tree
723reference_alias_ptr_type_1 (tree *t)
724{
725 tree inner;
726
727 /* Get the base object of the reference. */
728 inner = *t;
729 while (handled_component_p (inner))
730 {
731 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
732 the type of any component references that wrap it to
733 determine the alias-set. */
734 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
735 *t = TREE_OPERAND (inner, 0);
736 inner = TREE_OPERAND (inner, 0);
737 }
738
739 /* Handle pointer dereferences here, they can override the
740 alias-set. */
741 if (INDIRECT_REF_P (inner)
742 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
743 return TREE_TYPE (TREE_OPERAND (inner, 0));
744 else if (TREE_CODE (inner) == TARGET_MEM_REF)
745 return TREE_TYPE (TMR_OFFSET (inner));
746 else if (TREE_CODE (inner) == MEM_REF
747 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
748 return TREE_TYPE (TREE_OPERAND (inner, 1));
749
750 /* If the innermost reference is a MEM_REF that has a
751 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
752 using the memory access type for determining the alias-set. */
753 if (TREE_CODE (inner) == MEM_REF
754 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
755 != TYPE_MAIN_VARIANT
756 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
757 return TREE_TYPE (TREE_OPERAND (inner, 1));
758
b4ada065
RB
759 /* Otherwise, pick up the outermost object that we could have
760 a pointer to. */
761 tree tem = component_uses_parent_alias_set_from (*t);
762 if (tem)
763 *t = tem;
f40333af
RB
764
765 return NULL_TREE;
766}
767
768/* Return the pointer-type relevant for TBAA purposes from the
769 gimple memory reference tree T. This is the type to be used for
770 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
771 and guarantees that get_alias_set will return the same alias
772 set for T and the replacement. */
773
774tree
775reference_alias_ptr_type (tree t)
776{
777 tree ptype = reference_alias_ptr_type_1 (&t);
778 /* If there is a given pointer type for aliasing purposes, return it. */
779 if (ptype != NULL_TREE)
780 return ptype;
781
782 /* Otherwise build one from the outermost component reference we
783 may use. */
784 if (TREE_CODE (t) == MEM_REF
785 || TREE_CODE (t) == TARGET_MEM_REF)
786 return TREE_TYPE (TREE_OPERAND (t, 1));
787 else
788 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
789}
790
791/* Return whether the pointer-types T1 and T2 used to determine
792 two alias sets of two references will yield the same answer
793 from get_deref_alias_set. */
794
795bool
796alias_ptr_types_compatible_p (tree t1, tree t2)
797{
798 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
799 return true;
800
801 if (ref_all_alias_ptr_type_p (t1)
802 || ref_all_alias_ptr_type_p (t2))
803 return false;
804
805 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
806 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
807}
808
6e042ef4
JH
809/* Create emptry alias set entry. */
810
811alias_set_entry
812init_alias_set_entry (alias_set_type set)
813{
814 alias_set_entry ase = ggc_alloc<alias_set_entry_d> ();
815 ase->alias_set = set;
816 ase->children = NULL;
817 ase->has_zero_child = false;
818 ase->is_pointer = false;
819 ase->has_pointer = false;
820 gcc_checking_assert (!get_alias_set_entry (set));
821 (*alias_sets)[set] = ase;
822 return ase;
823}
824
3bdf5ad1
RK
825/* Return the alias set for T, which may be either a type or an
826 expression. Call language-specific routine for help, if needed. */
827
4862826d 828alias_set_type
4682ae04 829get_alias_set (tree t)
3bdf5ad1 830{
4862826d 831 alias_set_type set;
3bdf5ad1
RK
832
833 /* If we're not doing any alias analysis, just assume everything
834 aliases everything else. Also return 0 if this or its type is
835 an error. */
836 if (! flag_strict_aliasing || t == error_mark_node
837 || (! TYPE_P (t)
838 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
839 return 0;
840
841 /* We can be passed either an expression or a type. This and the
f47e9b4e
RK
842 language-specific routine may make mutually-recursive calls to each other
843 to figure out what to do. At each juncture, we see if this is a tree
844 that the language may need to handle specially. First handle things that
738cc472 845 aren't types. */
f824e5c3 846 if (! TYPE_P (t))
3bdf5ad1 847 {
70f34814
RG
848 /* Give the language a chance to do something with this tree
849 before we look at it. */
8ac61af7 850 STRIP_NOPS (t);
ae2bcd98 851 set = lang_hooks.get_alias_set (t);
8ac61af7
RK
852 if (set != -1)
853 return set;
854
f40333af
RB
855 /* Get the alias pointer-type to use or the outermost object
856 that we could have a pointer to. */
857 tree ptype = reference_alias_ptr_type_1 (&t);
858 if (ptype != NULL)
859 return get_deref_alias_set (ptype);
f824e5c3 860
738cc472
RK
861 /* If we've already determined the alias set for a decl, just return
862 it. This is necessary for C++ anonymous unions, whose component
863 variables don't look like union members (boo!). */
5755cd38 864 if (TREE_CODE (t) == VAR_DECL
3c0cb5de 865 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
5755cd38
JM
866 return MEM_ALIAS_SET (DECL_RTL (t));
867
f824e5c3
RK
868 /* Now all we care about is the type. */
869 t = TREE_TYPE (t);
3bdf5ad1
RK
870 }
871
3bdf5ad1 872 /* Variant qualifiers don't affect the alias set, so get the main
daad0278 873 variant. */
3bdf5ad1 874 t = TYPE_MAIN_VARIANT (t);
daad0278
RG
875
876 /* Always use the canonical type as well. If this is a type that
877 requires structural comparisons to identify compatible types
878 use alias set zero. */
879 if (TYPE_STRUCTURAL_EQUALITY_P (t))
cb9c2485
JM
880 {
881 /* Allow the language to specify another alias set for this
882 type. */
883 set = lang_hooks.get_alias_set (t);
884 if (set != -1)
885 return set;
886 return 0;
887 }
7be7d292 888
daad0278 889 t = TYPE_CANONICAL (t);
7be7d292 890
96d91dcf
RG
891 /* The canonical type should not require structural equality checks. */
892 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
daad0278
RG
893
894 /* If this is a type with a known alias set, return it. */
738cc472 895 if (TYPE_ALIAS_SET_KNOWN_P (t))
3bdf5ad1
RK
896 return TYPE_ALIAS_SET (t);
897
36784d0e
RG
898 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
899 if (!COMPLETE_TYPE_P (t))
900 {
901 /* For arrays with unknown size the conservative answer is the
902 alias set of the element type. */
903 if (TREE_CODE (t) == ARRAY_TYPE)
904 return get_alias_set (TREE_TYPE (t));
905
906 /* But return zero as a conservative answer for incomplete types. */
907 return 0;
908 }
909
3bdf5ad1 910 /* See if the language has special handling for this type. */
ae2bcd98 911 set = lang_hooks.get_alias_set (t);
8ac61af7 912 if (set != -1)
738cc472 913 return set;
2bf105ab 914
3bdf5ad1
RK
915 /* There are no objects of FUNCTION_TYPE, so there's no point in
916 using up an alias set for them. (There are, of course, pointers
917 and references to functions, but that's different.) */
7be7d292 918 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
3bdf5ad1 919 set = 0;
74d86f4f
RH
920
921 /* Unless the language specifies otherwise, let vector types alias
922 their components. This avoids some nasty type punning issues in
923 normal usage. And indeed lets vectors be treated more like an
924 array slice. */
925 else if (TREE_CODE (t) == VECTOR_TYPE)
926 set = get_alias_set (TREE_TYPE (t));
927
4653cae5
RG
928 /* Unless the language specifies otherwise, treat array types the
929 same as their components. This avoids the asymmetry we get
930 through recording the components. Consider accessing a
931 character(kind=1) through a reference to a character(kind=1)[1:1].
932 Or consider if we want to assign integer(kind=4)[0:D.1387] and
933 integer(kind=4)[4] the same alias set or not.
934 Just be pragmatic here and make sure the array and its element
935 type get the same alias set assigned. */
7be7d292 936 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
4653cae5
RG
937 set = get_alias_set (TREE_TYPE (t));
938
0ceb0201
RG
939 /* From the former common C and C++ langhook implementation:
940
941 Unfortunately, there is no canonical form of a pointer type.
942 In particular, if we have `typedef int I', then `int *', and
943 `I *' are different types. So, we have to pick a canonical
944 representative. We do this below.
945
946 Technically, this approach is actually more conservative that
947 it needs to be. In particular, `const int *' and `int *'
948 should be in different alias sets, according to the C and C++
949 standard, since their types are not the same, and so,
950 technically, an `int **' and `const int **' cannot point at
951 the same thing.
952
953 But, the standard is wrong. In particular, this code is
954 legal C++:
955
956 int *ip;
957 int **ipp = &ip;
958 const int* const* cipp = ipp;
959 And, it doesn't make sense for that to be legal unless you
960 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
961 the pointed-to types. This issue has been reported to the
962 C++ committee.
963
6e042ef4
JH
964 For this reason go to canonical type of the unqalified pointer type.
965 Until GCC 6 this code set all pointers sets to have alias set of
966 ptr_type_node but that is a bad idea, because it prevents disabiguations
967 in between pointers. For Firefox this accounts about 20% of all
968 disambiguations in the program. */
969 else if (POINTER_TYPE_P (t) && t != ptr_type_node && !in_lto_p)
970 {
971 tree p;
972 auto_vec <bool, 8> reference;
973
974 /* Unnest all pointers and references.
975 We also want to make pointer to array equivalent to pointer to its
976 element. So skip all array types, too. */
977 for (p = t; POINTER_TYPE_P (p)
978 || (TREE_CODE (p) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (p));
979 p = TREE_TYPE (p))
980 {
981 if (TREE_CODE (p) == REFERENCE_TYPE)
982 reference.safe_push (true);
983 if (TREE_CODE (p) == POINTER_TYPE)
984 reference.safe_push (false);
985 }
986 p = TYPE_MAIN_VARIANT (p);
987
988 /* Make void * compatible with char * and also void **.
989 Programs are commonly violating TBAA by this.
990
991 We also make void * to conflict with every pointer
992 (see record_component_aliases) and thus it is safe it to use it for
993 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
994 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
995 set = get_alias_set (ptr_type_node);
996 else
997 {
998 /* Rebuild pointer type from starting from canonical types using
999 unqualified pointers and references only. This way all such
1000 pointers will have the same alias set and will conflict with
1001 each other.
1002
1003 Most of time we already have pointers or references of a given type.
1004 If not we build new one just to be sure that if someone later
1005 (probably only middle-end can, as we should assign all alias
1006 classes only after finishing translation unit) builds the pointer
1007 type, the canonical type will match. */
1008 p = TYPE_CANONICAL (p);
1009 while (!reference.is_empty ())
1010 {
1011 if (reference.pop ())
1012 p = build_reference_type (p);
1013 else
1014 p = build_pointer_type (p);
1015 p = TYPE_CANONICAL (TYPE_MAIN_VARIANT (p));
1016 }
1017 gcc_checking_assert (TYPE_CANONICAL (p) == p);
1018
1019 /* Assign the alias set to both p and t.
1020 We can not call get_alias_set (p) here as that would trigger
1021 infinite recursion when p == t. In other cases it would just
1022 trigger unnecesary legwork of rebuilding the pointer again. */
1023 if (TYPE_ALIAS_SET_KNOWN_P (p))
1024 set = TYPE_ALIAS_SET (p);
1025 else
1026 {
1027 set = new_alias_set ();
1028 TYPE_ALIAS_SET (p) = set;
1029 }
1030 }
1031 }
1032 /* In LTO the rules above needs to be part of canonical type machinery.
1033 For now just punt. */
ccb5ad37
JH
1034 else if (POINTER_TYPE_P (t)
1035 && t != TYPE_CANONICAL (ptr_type_node) && in_lto_p)
1036 set = get_alias_set (TYPE_CANONICAL (ptr_type_node));
0ceb0201 1037
7be7d292 1038 /* Otherwise make a new alias set for this type. */
3bdf5ad1 1039 else
96d91dcf
RG
1040 {
1041 /* Each canonical type gets its own alias set, so canonical types
1042 shouldn't form a tree. It doesn't really matter for types
1043 we handle specially above, so only check it where it possibly
1044 would result in a bogus alias set. */
1045 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1046
1047 set = new_alias_set ();
1048 }
3bdf5ad1
RK
1049
1050 TYPE_ALIAS_SET (t) = set;
2bf105ab 1051
7be7d292
EB
1052 /* If this is an aggregate type or a complex type, we must record any
1053 component aliasing information. */
1d79fd2c 1054 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
2bf105ab
RK
1055 record_component_aliases (t);
1056
6e042ef4
JH
1057 /* We treat pointer types specially in alias_set_subset_of. */
1058 if (POINTER_TYPE_P (t) && set)
1059 {
1060 alias_set_entry ase = get_alias_set_entry (set);
1061 if (!ase)
1062 ase = init_alias_set_entry (set);
1063 ase->is_pointer = true;
1064 ase->has_pointer = true;
1065 }
1066
3bdf5ad1
RK
1067 return set;
1068}
1069
1070/* Return a brand-new alias set. */
1071
4862826d 1072alias_set_type
4682ae04 1073new_alias_set (void)
3bdf5ad1 1074{
3bdf5ad1 1075 if (flag_strict_aliasing)
9ddb66ca 1076 {
1a5640b4 1077 if (alias_sets == 0)
9771b263
DN
1078 vec_safe_push (alias_sets, (alias_set_entry) 0);
1079 vec_safe_push (alias_sets, (alias_set_entry) 0);
1080 return alias_sets->length () - 1;
9ddb66ca 1081 }
3bdf5ad1
RK
1082 else
1083 return 0;
1084}
3932261a 1085
01d28c3f
JM
1086/* Indicate that things in SUBSET can alias things in SUPERSET, but that
1087 not everything that aliases SUPERSET also aliases SUBSET. For example,
1088 in C, a store to an `int' can alias a load of a structure containing an
1089 `int', and vice versa. But it can't alias a load of a 'double' member
1090 of the same structure. Here, the structure would be the SUPERSET and
1091 `int' the SUBSET. This relationship is also described in the comment at
1092 the beginning of this file.
1093
1094 This function should be called only once per SUPERSET/SUBSET pair.
3932261a
MM
1095
1096 It is illegal for SUPERSET to be zero; everything is implicitly a
1097 subset of alias set zero. */
1098
794511d2 1099void
4862826d 1100record_alias_subset (alias_set_type superset, alias_set_type subset)
3932261a
MM
1101{
1102 alias_set_entry superset_entry;
1103 alias_set_entry subset_entry;
1104
f47e9b4e
RK
1105 /* It is possible in complex type situations for both sets to be the same,
1106 in which case we can ignore this operation. */
1107 if (superset == subset)
1108 return;
1109
298e6adc 1110 gcc_assert (superset);
3932261a
MM
1111
1112 superset_entry = get_alias_set_entry (superset);
ca7fd9cd 1113 if (superset_entry == 0)
3932261a
MM
1114 {
1115 /* Create an entry for the SUPERSET, so that we have a place to
1116 attach the SUBSET. */
6e042ef4 1117 superset_entry = init_alias_set_entry (superset);
3932261a
MM
1118 }
1119
2bf105ab
RK
1120 if (subset == 0)
1121 superset_entry->has_zero_child = 1;
1122 else
1123 {
1124 subset_entry = get_alias_set_entry (subset);
6e042ef4
JH
1125 if (!superset_entry->children)
1126 superset_entry->children
fb5c464a 1127 = hash_map<alias_set_hash, int>::create_ggc (64);
2bf105ab
RK
1128 /* If there is an entry for the subset, enter all of its children
1129 (if they are not already present) as children of the SUPERSET. */
ca7fd9cd 1130 if (subset_entry)
2bf105ab
RK
1131 {
1132 if (subset_entry->has_zero_child)
6e042ef4
JH
1133 superset_entry->has_zero_child = true;
1134 if (subset_entry->has_pointer)
1135 superset_entry->has_pointer = true;
d4b60170 1136
6e042ef4
JH
1137 if (subset_entry->children)
1138 {
fb5c464a 1139 hash_map<alias_set_hash, int>::iterator iter
6e042ef4
JH
1140 = subset_entry->children->begin ();
1141 for (; iter != subset_entry->children->end (); ++iter)
1142 superset_entry->children->put ((*iter).first, (*iter).second);
1143 }
2bf105ab 1144 }
3932261a 1145
2bf105ab 1146 /* Enter the SUBSET itself as a child of the SUPERSET. */
de144fb2 1147 superset_entry->children->put (subset, 0);
2bf105ab 1148 }
3932261a
MM
1149}
1150
a0c33338
RK
1151/* Record that component types of TYPE, if any, are part of that type for
1152 aliasing purposes. For record types, we only record component types
b5487346
EB
1153 for fields that are not marked non-addressable. For array types, we
1154 only record the component type if it is not marked non-aliased. */
a0c33338
RK
1155
1156void
4682ae04 1157record_component_aliases (tree type)
a0c33338 1158{
4862826d 1159 alias_set_type superset = get_alias_set (type);
a0c33338
RK
1160 tree field;
1161
1162 if (superset == 0)
1163 return;
1164
1165 switch (TREE_CODE (type))
1166 {
a0c33338
RK
1167 case RECORD_TYPE:
1168 case UNION_TYPE:
1169 case QUAL_UNION_TYPE:
910ad8de 1170 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
b5487346 1171 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
2bf105ab 1172 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
a0c33338
RK
1173 break;
1174
1d79fd2c
JW
1175 case COMPLEX_TYPE:
1176 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1177 break;
1178
4653cae5
RG
1179 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1180 element type. */
1181
a0c33338
RK
1182 default:
1183 break;
1184 }
1185}
1186
3bdf5ad1
RK
1187/* Allocate an alias set for use in storing and reading from the varargs
1188 spill area. */
1189
4862826d 1190static GTY(()) alias_set_type varargs_set = -1;
f103e34d 1191
4862826d 1192alias_set_type
4682ae04 1193get_varargs_alias_set (void)
3bdf5ad1 1194{
cd3ce9b4
JM
1195#if 1
1196 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1197 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1198 consistently use the varargs alias set for loads from the varargs
1199 area. So don't use it anywhere. */
1200 return 0;
1201#else
f103e34d
GK
1202 if (varargs_set == -1)
1203 varargs_set = new_alias_set ();
3bdf5ad1 1204
f103e34d 1205 return varargs_set;
cd3ce9b4 1206#endif
3bdf5ad1
RK
1207}
1208
1209/* Likewise, but used for the fixed portions of the frame, e.g., register
1210 save areas. */
1211
4862826d 1212static GTY(()) alias_set_type frame_set = -1;
f103e34d 1213
4862826d 1214alias_set_type
4682ae04 1215get_frame_alias_set (void)
3bdf5ad1 1216{
f103e34d
GK
1217 if (frame_set == -1)
1218 frame_set = new_alias_set ();
3bdf5ad1 1219
f103e34d 1220 return frame_set;
3bdf5ad1
RK
1221}
1222
9fc37b2b
RS
1223/* Create a new, unique base with id ID. */
1224
1225static rtx
1226unique_base_value (HOST_WIDE_INT id)
1227{
1228 return gen_rtx_ADDRESS (Pmode, id);
1229}
1230
1231/* Return true if accesses based on any other base value cannot alias
1232 those based on X. */
1233
1234static bool
1235unique_base_value_p (rtx x)
1236{
1237 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1238}
1239
1240/* Return true if X is known to be a base value. */
1241
1242static bool
1243known_base_value_p (rtx x)
1244{
1245 switch (GET_CODE (x))
1246 {
1247 case LABEL_REF:
1248 case SYMBOL_REF:
1249 return true;
1250
1251 case ADDRESS:
1252 /* Arguments may or may not be bases; we don't know for sure. */
1253 return GET_MODE (x) != VOIDmode;
1254
1255 default:
1256 return false;
1257 }
1258}
1259
2a2c8203
JC
1260/* Inside SRC, the source of a SET, find a base address. */
1261
9ae8ffe7 1262static rtx
4682ae04 1263find_base_value (rtx src)
9ae8ffe7 1264{
713f41f9 1265 unsigned int regno;
0aacc8ed 1266
53451050
RS
1267#if defined (FIND_BASE_TERM)
1268 /* Try machine-dependent ways to find the base term. */
1269 src = FIND_BASE_TERM (src);
1270#endif
1271
9ae8ffe7
JL
1272 switch (GET_CODE (src))
1273 {
1274 case SYMBOL_REF:
1275 case LABEL_REF:
1276 return src;
1277
1278 case REG:
fb6754f0 1279 regno = REGNO (src);
d4b60170 1280 /* At the start of a function, argument registers have known base
2a2c8203
JC
1281 values which may be lost later. Returning an ADDRESS
1282 expression here allows optimization based on argument values
1283 even when the argument registers are used for other purposes. */
713f41f9
BS
1284 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1285 return new_reg_base_value[regno];
73774bc7 1286
eaf407a5 1287 /* If a pseudo has a known base value, return it. Do not do this
9b462c42
RH
1288 for non-fixed hard regs since it can result in a circular
1289 dependency chain for registers which have values at function entry.
eaf407a5
JL
1290
1291 The test above is not sufficient because the scheduler may move
1292 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
9b462c42 1293 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
9771b263 1294 && regno < vec_safe_length (reg_base_value))
83bbd9b6
RH
1295 {
1296 /* If we're inside init_alias_analysis, use new_reg_base_value
1297 to reduce the number of relaxation iterations. */
1afdf91c 1298 if (new_reg_base_value && new_reg_base_value[regno]
6fb5fa3c 1299 && DF_REG_DEF_COUNT (regno) == 1)
83bbd9b6
RH
1300 return new_reg_base_value[regno];
1301
9771b263
DN
1302 if ((*reg_base_value)[regno])
1303 return (*reg_base_value)[regno];
83bbd9b6 1304 }
73774bc7 1305
e3f049a8 1306 return 0;
9ae8ffe7
JL
1307
1308 case MEM:
1309 /* Check for an argument passed in memory. Only record in the
1310 copying-arguments block; it is too hard to track changes
1311 otherwise. */
1312 if (copying_arguments
1313 && (XEXP (src, 0) == arg_pointer_rtx
1314 || (GET_CODE (XEXP (src, 0)) == PLUS
1315 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
9fc37b2b 1316 return arg_base_value;
9ae8ffe7
JL
1317 return 0;
1318
1319 case CONST:
1320 src = XEXP (src, 0);
1321 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1322 break;
d4b60170 1323
ec5c56db 1324 /* ... fall through ... */
2a2c8203 1325
9ae8ffe7
JL
1326 case PLUS:
1327 case MINUS:
2a2c8203 1328 {
ec907dd8
JL
1329 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1330
0134bf2d
DE
1331 /* If either operand is a REG that is a known pointer, then it
1332 is the base. */
1333 if (REG_P (src_0) && REG_POINTER (src_0))
1334 return find_base_value (src_0);
1335 if (REG_P (src_1) && REG_POINTER (src_1))
1336 return find_base_value (src_1);
1337
ec907dd8
JL
1338 /* If either operand is a REG, then see if we already have
1339 a known value for it. */
0134bf2d 1340 if (REG_P (src_0))
ec907dd8
JL
1341 {
1342 temp = find_base_value (src_0);
d4b60170 1343 if (temp != 0)
ec907dd8
JL
1344 src_0 = temp;
1345 }
1346
0134bf2d 1347 if (REG_P (src_1))
ec907dd8
JL
1348 {
1349 temp = find_base_value (src_1);
d4b60170 1350 if (temp!= 0)
ec907dd8
JL
1351 src_1 = temp;
1352 }
2a2c8203 1353
0134bf2d
DE
1354 /* If either base is named object or a special address
1355 (like an argument or stack reference), then use it for the
1356 base term. */
9fc37b2b 1357 if (src_0 != 0 && known_base_value_p (src_0))
0134bf2d
DE
1358 return src_0;
1359
9fc37b2b 1360 if (src_1 != 0 && known_base_value_p (src_1))
0134bf2d
DE
1361 return src_1;
1362
d4b60170 1363 /* Guess which operand is the base address:
ec907dd8
JL
1364 If either operand is a symbol, then it is the base. If
1365 either operand is a CONST_INT, then the other is the base. */
481683e1 1366 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
2a2c8203 1367 return find_base_value (src_0);
481683e1 1368 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
ec907dd8
JL
1369 return find_base_value (src_1);
1370
9ae8ffe7 1371 return 0;
2a2c8203
JC
1372 }
1373
1374 case LO_SUM:
1375 /* The standard form is (lo_sum reg sym) so look only at the
1376 second operand. */
1377 return find_base_value (XEXP (src, 1));
9ae8ffe7
JL
1378
1379 case AND:
1380 /* If the second operand is constant set the base
ec5c56db 1381 address to the first operand. */
481683e1 1382 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
2a2c8203 1383 return find_base_value (XEXP (src, 0));
9ae8ffe7
JL
1384 return 0;
1385
61f0131c 1386 case TRUNCATE:
5932a4d4 1387 /* As we do not know which address space the pointer is referring to, we can
d4ebfa65
BE
1388 handle this only if the target does not support different pointer or
1389 address modes depending on the address space. */
1390 if (!target_default_pointer_address_modes_p ())
1391 break;
61f0131c
R
1392 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1393 break;
1394 /* Fall through. */
9ae8ffe7 1395 case HIGH:
d288e53d
DE
1396 case PRE_INC:
1397 case PRE_DEC:
1398 case POST_INC:
1399 case POST_DEC:
1400 case PRE_MODIFY:
1401 case POST_MODIFY:
2a2c8203 1402 return find_base_value (XEXP (src, 0));
1d300e19 1403
0aacc8ed
RK
1404 case ZERO_EXTEND:
1405 case SIGN_EXTEND: /* used for NT/Alpha pointers */
5932a4d4 1406 /* As we do not know which address space the pointer is referring to, we can
d4ebfa65
BE
1407 handle this only if the target does not support different pointer or
1408 address modes depending on the address space. */
1409 if (!target_default_pointer_address_modes_p ())
1410 break;
1411
0aacc8ed
RK
1412 {
1413 rtx temp = find_base_value (XEXP (src, 0));
1414
5ae6cd0d 1415 if (temp != 0 && CONSTANT_P (temp))
0aacc8ed 1416 temp = convert_memory_address (Pmode, temp);
0aacc8ed
RK
1417
1418 return temp;
1419 }
1420
1d300e19
KG
1421 default:
1422 break;
9ae8ffe7
JL
1423 }
1424
1425 return 0;
1426}
1427
9fc37b2b
RS
1428/* Called from init_alias_analysis indirectly through note_stores,
1429 or directly if DEST is a register with a REG_NOALIAS note attached.
1430 SET is null in the latter case. */
9ae8ffe7 1431
d4b60170 1432/* While scanning insns to find base values, reg_seen[N] is nonzero if
9ae8ffe7 1433 register N has been set in this function. */
d630245f 1434static sbitmap reg_seen;
9ae8ffe7 1435
2a2c8203 1436static void
7bc980e1 1437record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
9ae8ffe7 1438{
b3694847 1439 unsigned regno;
9ae8ffe7 1440 rtx src;
c28b4e40 1441 int n;
9ae8ffe7 1442
f8cfc6aa 1443 if (!REG_P (dest))
9ae8ffe7
JL
1444 return;
1445
fb6754f0 1446 regno = REGNO (dest);
9ae8ffe7 1447
9771b263 1448 gcc_checking_assert (regno < reg_base_value->length ());
ac606739 1449
dc8afb70 1450 n = REG_NREGS (dest);
c28b4e40
JW
1451 if (n != 1)
1452 {
1453 while (--n >= 0)
1454 {
d7c028c0 1455 bitmap_set_bit (reg_seen, regno + n);
c28b4e40
JW
1456 new_reg_base_value[regno + n] = 0;
1457 }
1458 return;
1459 }
1460
9ae8ffe7
JL
1461 if (set)
1462 {
1463 /* A CLOBBER wipes out any old value but does not prevent a previously
1464 unset register from acquiring a base address (i.e. reg_seen is not
1465 set). */
1466 if (GET_CODE (set) == CLOBBER)
1467 {
ec907dd8 1468 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
1469 return;
1470 }
1471 src = SET_SRC (set);
1472 }
1473 else
1474 {
9fc37b2b 1475 /* There's a REG_NOALIAS note against DEST. */
d7c028c0 1476 if (bitmap_bit_p (reg_seen, regno))
9ae8ffe7 1477 {
ec907dd8 1478 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
1479 return;
1480 }
d7c028c0 1481 bitmap_set_bit (reg_seen, regno);
9fc37b2b 1482 new_reg_base_value[regno] = unique_base_value (unique_id++);
9ae8ffe7
JL
1483 return;
1484 }
1485
5da6f168
RS
1486 /* If this is not the first set of REGNO, see whether the new value
1487 is related to the old one. There are two cases of interest:
1488
1489 (1) The register might be assigned an entirely new value
1490 that has the same base term as the original set.
1491
1492 (2) The set might be a simple self-modification that
1493 cannot change REGNO's base value.
1494
1495 If neither case holds, reject the original base value as invalid.
1496 Note that the following situation is not detected:
1497
c22cacf3 1498 extern int x, y; int *p = &x; p += (&y-&x);
5da6f168 1499
9ae8ffe7
JL
1500 ANSI C does not allow computing the difference of addresses
1501 of distinct top level objects. */
5da6f168
RS
1502 if (new_reg_base_value[regno] != 0
1503 && find_base_value (src) != new_reg_base_value[regno])
9ae8ffe7
JL
1504 switch (GET_CODE (src))
1505 {
2a2c8203 1506 case LO_SUM:
9ae8ffe7
JL
1507 case MINUS:
1508 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
ec907dd8 1509 new_reg_base_value[regno] = 0;
9ae8ffe7 1510 break;
61f0131c
R
1511 case PLUS:
1512 /* If the value we add in the PLUS is also a valid base value,
1513 this might be the actual base value, and the original value
1514 an index. */
1515 {
1516 rtx other = NULL_RTX;
1517
1518 if (XEXP (src, 0) == dest)
1519 other = XEXP (src, 1);
1520 else if (XEXP (src, 1) == dest)
1521 other = XEXP (src, 0);
1522
1523 if (! other || find_base_value (other))
1524 new_reg_base_value[regno] = 0;
1525 break;
1526 }
9ae8ffe7 1527 case AND:
481683e1 1528 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
ec907dd8 1529 new_reg_base_value[regno] = 0;
9ae8ffe7 1530 break;
9ae8ffe7 1531 default:
ec907dd8 1532 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
1533 break;
1534 }
1535 /* If this is the first set of a register, record the value. */
1536 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
d7c028c0 1537 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
ec907dd8 1538 new_reg_base_value[regno] = find_base_value (src);
9ae8ffe7 1539
d7c028c0 1540 bitmap_set_bit (reg_seen, regno);
9ae8ffe7
JL
1541}
1542
8fd0a474
AM
1543/* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1544 using hard registers with non-null REG_BASE_VALUE for renaming. */
1545rtx
1546get_reg_base_value (unsigned int regno)
1547{
9771b263 1548 return (*reg_base_value)[regno];
8fd0a474
AM
1549}
1550
bb1acb3e
RH
1551/* If a value is known for REGNO, return it. */
1552
c22cacf3 1553rtx
bb1acb3e
RH
1554get_reg_known_value (unsigned int regno)
1555{
1556 if (regno >= FIRST_PSEUDO_REGISTER)
1557 {
1558 regno -= FIRST_PSEUDO_REGISTER;
9771b263
DN
1559 if (regno < vec_safe_length (reg_known_value))
1560 return (*reg_known_value)[regno];
bb1acb3e
RH
1561 }
1562 return NULL;
43fe47ca
JW
1563}
1564
bb1acb3e
RH
1565/* Set it. */
1566
1567static void
1568set_reg_known_value (unsigned int regno, rtx val)
1569{
1570 if (regno >= FIRST_PSEUDO_REGISTER)
1571 {
1572 regno -= FIRST_PSEUDO_REGISTER;
9771b263
DN
1573 if (regno < vec_safe_length (reg_known_value))
1574 (*reg_known_value)[regno] = val;
bb1acb3e
RH
1575 }
1576}
1577
1578/* Similarly for reg_known_equiv_p. */
1579
1580bool
1581get_reg_known_equiv_p (unsigned int regno)
1582{
1583 if (regno >= FIRST_PSEUDO_REGISTER)
1584 {
1585 regno -= FIRST_PSEUDO_REGISTER;
9771b263 1586 if (regno < vec_safe_length (reg_known_value))
d7c028c0 1587 return bitmap_bit_p (reg_known_equiv_p, regno);
bb1acb3e
RH
1588 }
1589 return false;
1590}
1591
1592static void
1593set_reg_known_equiv_p (unsigned int regno, bool val)
1594{
1595 if (regno >= FIRST_PSEUDO_REGISTER)
1596 {
1597 regno -= FIRST_PSEUDO_REGISTER;
9771b263 1598 if (regno < vec_safe_length (reg_known_value))
9ff3c7ca
SB
1599 {
1600 if (val)
d7c028c0 1601 bitmap_set_bit (reg_known_equiv_p, regno);
9ff3c7ca 1602 else
d7c028c0 1603 bitmap_clear_bit (reg_known_equiv_p, regno);
9ff3c7ca 1604 }
bb1acb3e
RH
1605 }
1606}
1607
1608
db048faf
MM
1609/* Returns a canonical version of X, from the point of view alias
1610 analysis. (For example, if X is a MEM whose address is a register,
1611 and the register has a known value (say a SYMBOL_REF), then a MEM
1612 whose address is the SYMBOL_REF is returned.) */
1613
1614rtx
4682ae04 1615canon_rtx (rtx x)
9ae8ffe7
JL
1616{
1617 /* Recursively look for equivalences. */
f8cfc6aa 1618 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
bb1acb3e
RH
1619 {
1620 rtx t = get_reg_known_value (REGNO (x));
1621 if (t == x)
1622 return x;
1623 if (t)
1624 return canon_rtx (t);
1625 }
1626
1627 if (GET_CODE (x) == PLUS)
9ae8ffe7
JL
1628 {
1629 rtx x0 = canon_rtx (XEXP (x, 0));
1630 rtx x1 = canon_rtx (XEXP (x, 1));
1631
1632 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1633 {
481683e1 1634 if (CONST_INT_P (x0))
0a81f074 1635 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
481683e1 1636 else if (CONST_INT_P (x1))
0a81f074 1637 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
38a448ca 1638 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
9ae8ffe7
JL
1639 }
1640 }
d4b60170 1641
9ae8ffe7
JL
1642 /* This gives us much better alias analysis when called from
1643 the loop optimizer. Note we want to leave the original
1644 MEM alone, but need to return the canonicalized MEM with
1645 all the flags with their original values. */
3c0cb5de 1646 else if (MEM_P (x))
f1ec5147 1647 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
d4b60170 1648
9ae8ffe7
JL
1649 return x;
1650}
1651
1652/* Return 1 if X and Y are identical-looking rtx's.
45183e03 1653 Expect that X and Y has been already canonicalized.
9ae8ffe7
JL
1654
1655 We use the data in reg_known_value above to see if two registers with
1656 different numbers are, in fact, equivalent. */
1657
1658static int
ed7a4b4b 1659rtx_equal_for_memref_p (const_rtx x, const_rtx y)
9ae8ffe7 1660{
b3694847
SS
1661 int i;
1662 int j;
1663 enum rtx_code code;
1664 const char *fmt;
9ae8ffe7
JL
1665
1666 if (x == 0 && y == 0)
1667 return 1;
1668 if (x == 0 || y == 0)
1669 return 0;
d4b60170 1670
9ae8ffe7
JL
1671 if (x == y)
1672 return 1;
1673
1674 code = GET_CODE (x);
1675 /* Rtx's of different codes cannot be equal. */
1676 if (code != GET_CODE (y))
1677 return 0;
1678
1679 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1680 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1681
1682 if (GET_MODE (x) != GET_MODE (y))
1683 return 0;
1684
db048faf
MM
1685 /* Some RTL can be compared without a recursive examination. */
1686 switch (code)
1687 {
1688 case REG:
1689 return REGNO (x) == REGNO (y);
1690
1691 case LABEL_REF:
a827d9b1 1692 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
ca7fd9cd 1693
db048faf
MM
1694 case SYMBOL_REF:
1695 return XSTR (x, 0) == XSTR (y, 0);
1696
af6236c1
AO
1697 case ENTRY_VALUE:
1698 /* This is magic, don't go through canonicalization et al. */
1699 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1700
40e02b4a 1701 case VALUE:
d8116890 1702 CASE_CONST_UNIQUE:
807e902e 1703 /* Pointer equality guarantees equality for these nodes. */
db048faf
MM
1704 return 0;
1705
db048faf
MM
1706 default:
1707 break;
1708 }
9ae8ffe7 1709
45183e03
JH
1710 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1711 if (code == PLUS)
9ae8ffe7
JL
1712 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1713 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1714 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1715 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
45183e03
JH
1716 /* For commutative operations, the RTX match if the operand match in any
1717 order. Also handle the simple binary and unary cases without a loop. */
ec8e098d 1718 if (COMMUTATIVE_P (x))
45183e03
JH
1719 {
1720 rtx xop0 = canon_rtx (XEXP (x, 0));
1721 rtx yop0 = canon_rtx (XEXP (y, 0));
1722 rtx yop1 = canon_rtx (XEXP (y, 1));
1723
1724 return ((rtx_equal_for_memref_p (xop0, yop0)
1725 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1726 || (rtx_equal_for_memref_p (xop0, yop1)
1727 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1728 }
ec8e098d 1729 else if (NON_COMMUTATIVE_P (x))
45183e03
JH
1730 {
1731 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
4682ae04 1732 canon_rtx (XEXP (y, 0)))
45183e03
JH
1733 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1734 canon_rtx (XEXP (y, 1))));
1735 }
ec8e098d 1736 else if (UNARY_P (x))
45183e03 1737 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
4682ae04 1738 canon_rtx (XEXP (y, 0)));
9ae8ffe7
JL
1739
1740 /* Compare the elements. If any pair of corresponding elements
de12be17
JC
1741 fail to match, return 0 for the whole things.
1742
1743 Limit cases to types which actually appear in addresses. */
9ae8ffe7
JL
1744
1745 fmt = GET_RTX_FORMAT (code);
1746 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1747 {
1748 switch (fmt[i])
1749 {
9ae8ffe7
JL
1750 case 'i':
1751 if (XINT (x, i) != XINT (y, i))
1752 return 0;
1753 break;
1754
9ae8ffe7
JL
1755 case 'E':
1756 /* Two vectors must have the same length. */
1757 if (XVECLEN (x, i) != XVECLEN (y, i))
1758 return 0;
1759
1760 /* And the corresponding elements must match. */
1761 for (j = 0; j < XVECLEN (x, i); j++)
45183e03
JH
1762 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1763 canon_rtx (XVECEXP (y, i, j))) == 0)
9ae8ffe7
JL
1764 return 0;
1765 break;
1766
1767 case 'e':
45183e03
JH
1768 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1769 canon_rtx (XEXP (y, i))) == 0)
9ae8ffe7
JL
1770 return 0;
1771 break;
1772
3237ac18
AH
1773 /* This can happen for asm operands. */
1774 case 's':
1775 if (strcmp (XSTR (x, i), XSTR (y, i)))
1776 return 0;
1777 break;
1778
aee21ba9
JL
1779 /* This can happen for an asm which clobbers memory. */
1780 case '0':
1781 break;
1782
9ae8ffe7
JL
1783 /* It is believed that rtx's at this level will never
1784 contain anything but integers and other rtx's,
1785 except for within LABEL_REFs and SYMBOL_REFs. */
1786 default:
298e6adc 1787 gcc_unreachable ();
9ae8ffe7
JL
1788 }
1789 }
1790 return 1;
1791}
1792
9e412ca3 1793static rtx
4682ae04 1794find_base_term (rtx x)
9ae8ffe7 1795{
eab5c70a 1796 cselib_val *val;
6f2ffb4b
AO
1797 struct elt_loc_list *l, *f;
1798 rtx ret;
eab5c70a 1799
b949ea8b
JW
1800#if defined (FIND_BASE_TERM)
1801 /* Try machine-dependent ways to find the base term. */
1802 x = FIND_BASE_TERM (x);
1803#endif
1804
9ae8ffe7
JL
1805 switch (GET_CODE (x))
1806 {
1807 case REG:
1808 return REG_BASE_VALUE (x);
1809
d288e53d 1810 case TRUNCATE:
5932a4d4 1811 /* As we do not know which address space the pointer is referring to, we can
d4ebfa65
BE
1812 handle this only if the target does not support different pointer or
1813 address modes depending on the address space. */
1814 if (!target_default_pointer_address_modes_p ())
1815 return 0;
d288e53d 1816 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
ca7fd9cd 1817 return 0;
d288e53d 1818 /* Fall through. */
9ae8ffe7 1819 case HIGH:
6d849a2a
JL
1820 case PRE_INC:
1821 case PRE_DEC:
1822 case POST_INC:
1823 case POST_DEC:
d288e53d
DE
1824 case PRE_MODIFY:
1825 case POST_MODIFY:
6d849a2a
JL
1826 return find_base_term (XEXP (x, 0));
1827
1abade85
RK
1828 case ZERO_EXTEND:
1829 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
5932a4d4 1830 /* As we do not know which address space the pointer is referring to, we can
d4ebfa65
BE
1831 handle this only if the target does not support different pointer or
1832 address modes depending on the address space. */
1833 if (!target_default_pointer_address_modes_p ())
1834 return 0;
1835
1abade85
RK
1836 {
1837 rtx temp = find_base_term (XEXP (x, 0));
1838
5ae6cd0d 1839 if (temp != 0 && CONSTANT_P (temp))
1abade85 1840 temp = convert_memory_address (Pmode, temp);
1abade85
RK
1841
1842 return temp;
1843 }
1844
eab5c70a
BS
1845 case VALUE:
1846 val = CSELIB_VAL_PTR (x);
6f2ffb4b
AO
1847 ret = NULL_RTX;
1848
40e02b4a 1849 if (!val)
6f2ffb4b
AO
1850 return ret;
1851
0fe03ac3
JJ
1852 if (cselib_sp_based_value_p (val))
1853 return static_reg_base_value[STACK_POINTER_REGNUM];
1854
6f2ffb4b
AO
1855 f = val->locs;
1856 /* Temporarily reset val->locs to avoid infinite recursion. */
1857 val->locs = NULL;
1858
1859 for (l = f; l; l = l->next)
1860 if (GET_CODE (l->loc) == VALUE
1861 && CSELIB_VAL_PTR (l->loc)->locs
1862 && !CSELIB_VAL_PTR (l->loc)->locs->next
1863 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1864 continue;
1865 else if ((ret = find_base_term (l->loc)) != 0)
1866 break;
1867
1868 val->locs = f;
1869 return ret;
eab5c70a 1870
023f059b
JJ
1871 case LO_SUM:
1872 /* The standard form is (lo_sum reg sym) so look only at the
1873 second operand. */
1874 return find_base_term (XEXP (x, 1));
1875
9ae8ffe7
JL
1876 case CONST:
1877 x = XEXP (x, 0);
1878 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1879 return 0;
938d968e 1880 /* Fall through. */
9ae8ffe7
JL
1881 case PLUS:
1882 case MINUS:
1883 {
3c567fae
JL
1884 rtx tmp1 = XEXP (x, 0);
1885 rtx tmp2 = XEXP (x, 1);
1886
f5143c46 1887 /* This is a little bit tricky since we have to determine which of
3c567fae
JL
1888 the two operands represents the real base address. Otherwise this
1889 routine may return the index register instead of the base register.
1890
1891 That may cause us to believe no aliasing was possible, when in
1892 fact aliasing is possible.
1893
1894 We use a few simple tests to guess the base register. Additional
1895 tests can certainly be added. For example, if one of the operands
1896 is a shift or multiply, then it must be the index register and the
1897 other operand is the base register. */
ca7fd9cd 1898
b949ea8b
JW
1899 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1900 return find_base_term (tmp2);
1901
31b0a960 1902 /* If either operand is known to be a pointer, then prefer it
3c567fae 1903 to determine the base term. */
3502dc9c 1904 if (REG_P (tmp1) && REG_POINTER (tmp1))
31b0a960
RB
1905 ;
1906 else if (REG_P (tmp2) && REG_POINTER (tmp2))
a7c75343
JJ
1907 std::swap (tmp1, tmp2);
1908 /* If second argument is constant which has base term, prefer it
1909 over variable tmp1. See PR64025. */
1910 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
1911 std::swap (tmp1, tmp2);
3c567fae 1912
31b0a960
RB
1913 /* Go ahead and find the base term for both operands. If either base
1914 term is from a pointer or is a named object or a special address
3c567fae
JL
1915 (like an argument or stack reference), then use it for the
1916 base term. */
481be1c4
RB
1917 rtx base = find_base_term (tmp1);
1918 if (base != NULL_RTX
31b0a960 1919 && ((REG_P (tmp1) && REG_POINTER (tmp1))
481be1c4
RB
1920 || known_base_value_p (base)))
1921 return base;
1922 base = find_base_term (tmp2);
1923 if (base != NULL_RTX
31b0a960 1924 && ((REG_P (tmp2) && REG_POINTER (tmp2))
481be1c4
RB
1925 || known_base_value_p (base)))
1926 return base;
3c567fae
JL
1927
1928 /* We could not determine which of the two operands was the
1929 base register and which was the index. So we can determine
1930 nothing from the base alias check. */
1931 return 0;
9ae8ffe7
JL
1932 }
1933
1934 case AND:
481683e1 1935 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
d288e53d 1936 return find_base_term (XEXP (x, 0));
9ae8ffe7
JL
1937 return 0;
1938
1939 case SYMBOL_REF:
1940 case LABEL_REF:
1941 return x;
1942
1943 default:
1944 return 0;
1945 }
1946}
1947
9e412ca3
RS
1948/* Return true if accesses to address X may alias accesses based
1949 on the stack pointer. */
1950
1951bool
1952may_be_sp_based_p (rtx x)
1953{
1954 rtx base = find_base_term (x);
1955 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1956}
1957
9ae8ffe7
JL
1958/* Return 0 if the addresses X and Y are known to point to different
1959 objects, 1 if they might be pointers to the same object. */
1960
1961static int
31b0a960 1962base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
ef4bddc2 1963 machine_mode x_mode, machine_mode y_mode)
9ae8ffe7 1964{
1c72c7f6
JC
1965 /* If the address itself has no known base see if a known equivalent
1966 value has one. If either address still has no known base, nothing
1967 is known about aliasing. */
1968 if (x_base == 0)
1969 {
1970 rtx x_c;
d4b60170 1971
1c72c7f6
JC
1972 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1973 return 1;
d4b60170 1974
1c72c7f6
JC
1975 x_base = find_base_term (x_c);
1976 if (x_base == 0)
1977 return 1;
1978 }
9ae8ffe7 1979
1c72c7f6
JC
1980 if (y_base == 0)
1981 {
1982 rtx y_c;
1983 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1984 return 1;
d4b60170 1985
1c72c7f6
JC
1986 y_base = find_base_term (y_c);
1987 if (y_base == 0)
1988 return 1;
1989 }
1990
1991 /* If the base addresses are equal nothing is known about aliasing. */
1992 if (rtx_equal_p (x_base, y_base))
9ae8ffe7
JL
1993 return 1;
1994
435da628
UB
1995 /* The base addresses are different expressions. If they are not accessed
1996 via AND, there is no conflict. We can bring knowledge of object
1997 alignment into play here. For example, on alpha, "char a, b;" can
1998 alias one another, though "char a; long b;" cannot. AND addesses may
1999 implicitly alias surrounding objects; i.e. unaligned access in DImode
2000 via AND address can alias all surrounding object types except those
2001 with aligment 8 or higher. */
2002 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2003 return 1;
2004 if (GET_CODE (x) == AND
481683e1 2005 && (!CONST_INT_P (XEXP (x, 1))
435da628
UB
2006 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2007 return 1;
2008 if (GET_CODE (y) == AND
481683e1 2009 && (!CONST_INT_P (XEXP (y, 1))
435da628
UB
2010 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2011 return 1;
2012
2013 /* Differing symbols not accessed via AND never alias. */
9ae8ffe7 2014 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
435da628 2015 return 0;
9ae8ffe7 2016
9fc37b2b 2017 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
9ae8ffe7
JL
2018 return 0;
2019
0d3c82d6 2020 return 1;
9ae8ffe7
JL
2021}
2022
a5628378
AO
2023/* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2024 that of V. */
2025
2026static bool
403837b4 2027refs_newer_value_p (const_rtx expr, rtx v)
a5628378
AO
2028{
2029 int minuid = CSELIB_VAL_PTR (v)->uid;
403837b4
RS
2030 subrtx_iterator::array_type array;
2031 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2032 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
2033 return true;
2034 return false;
a5628378
AO
2035}
2036
eab5c70a
BS
2037/* Convert the address X into something we can use. This is done by returning
2038 it unchanged unless it is a value; in the latter case we call cselib to get
2039 a more useful rtx. */
3bdf5ad1 2040
a13d4ebf 2041rtx
4682ae04 2042get_addr (rtx x)
eab5c70a
BS
2043{
2044 cselib_val *v;
2045 struct elt_loc_list *l;
2046
2047 if (GET_CODE (x) != VALUE)
2048 return x;
2049 v = CSELIB_VAL_PTR (x);
40e02b4a
JH
2050 if (v)
2051 {
0f68ba3e
AO
2052 bool have_equivs = cselib_have_permanent_equivalences ();
2053 if (have_equivs)
2054 v = canonical_cselib_val (v);
40e02b4a
JH
2055 for (l = v->locs; l; l = l->next)
2056 if (CONSTANT_P (l->loc))
2057 return l->loc;
2058 for (l = v->locs; l; l = l->next)
0f68ba3e
AO
2059 if (!REG_P (l->loc) && !MEM_P (l->loc)
2060 /* Avoid infinite recursion when potentially dealing with
2061 var-tracking artificial equivalences, by skipping the
2062 equivalences themselves, and not choosing expressions
2063 that refer to newer VALUEs. */
2064 && (!have_equivs
2065 || (GET_CODE (l->loc) != VALUE
2066 && !refs_newer_value_p (l->loc, x))))
a5628378 2067 return l->loc;
0f68ba3e
AO
2068 if (have_equivs)
2069 {
2070 for (l = v->locs; l; l = l->next)
2071 if (REG_P (l->loc)
2072 || (GET_CODE (l->loc) != VALUE
2073 && !refs_newer_value_p (l->loc, x)))
2074 return l->loc;
2075 /* Return the canonical value. */
2076 return v->val_rtx;
2077 }
2078 if (v->locs)
2079 return v->locs->loc;
40e02b4a 2080 }
eab5c70a
BS
2081 return x;
2082}
2083
39cec1ac
MH
2084/* Return the address of the (N_REFS + 1)th memory reference to ADDR
2085 where SIZE is the size in bytes of the memory reference. If ADDR
2086 is not modified by the memory reference then ADDR is returned. */
2087
04e2b4d3 2088static rtx
4682ae04 2089addr_side_effect_eval (rtx addr, int size, int n_refs)
39cec1ac
MH
2090{
2091 int offset = 0;
ca7fd9cd 2092
39cec1ac
MH
2093 switch (GET_CODE (addr))
2094 {
2095 case PRE_INC:
2096 offset = (n_refs + 1) * size;
2097 break;
2098 case PRE_DEC:
2099 offset = -(n_refs + 1) * size;
2100 break;
2101 case POST_INC:
2102 offset = n_refs * size;
2103 break;
2104 case POST_DEC:
2105 offset = -n_refs * size;
2106 break;
2107
2108 default:
2109 return addr;
2110 }
ca7fd9cd 2111
39cec1ac 2112 if (offset)
45183e03 2113 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
4789c0ce 2114 gen_int_mode (offset, GET_MODE (addr)));
39cec1ac
MH
2115 else
2116 addr = XEXP (addr, 0);
45183e03 2117 addr = canon_rtx (addr);
39cec1ac
MH
2118
2119 return addr;
2120}
2121
3aa03517
AO
2122/* Return TRUE if an object X sized at XSIZE bytes and another object
2123 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2124 any of the sizes is zero, assume an overlap, otherwise use the
2125 absolute value of the sizes as the actual sizes. */
2126
2127static inline bool
2128offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
2129{
2130 return (xsize == 0 || ysize == 0
2131 || (c >= 0
2132 ? (abs (xsize) > c)
2133 : (abs (ysize) > -c)));
2134}
2135
f47e08d9
RG
2136/* Return one if X and Y (memory addresses) reference the
2137 same location in memory or if the references overlap.
2138 Return zero if they do not overlap, else return
2139 minus one in which case they still might reference the same location.
2140
2141 C is an offset accumulator. When
9ae8ffe7
JL
2142 C is nonzero, we are testing aliases between X and Y + C.
2143 XSIZE is the size in bytes of the X reference,
2144 similarly YSIZE is the size in bytes for Y.
45183e03 2145 Expect that canon_rtx has been already called for X and Y.
9ae8ffe7
JL
2146
2147 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2148 referenced (the reference was BLKmode), so make the most pessimistic
2149 assumptions.
2150
c02f035f
RH
2151 If XSIZE or YSIZE is negative, we may access memory outside the object
2152 being referenced as a side effect. This can happen when using AND to
2153 align memory references, as is done on the Alpha.
2154
9ae8ffe7 2155 Nice to notice that varying addresses cannot conflict with fp if no
f47e08d9
RG
2156 local variables had their addresses taken, but that's too hard now.
2157
2158 ??? Contrary to the tree alias oracle this does not return
2159 one for X + non-constant and Y + non-constant when X and Y are equal.
2160 If that is fixed the TBAA hack for union type-punning can be removed. */
9ae8ffe7 2161
9ae8ffe7 2162static int
4682ae04 2163memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
9ae8ffe7 2164{
eab5c70a 2165 if (GET_CODE (x) == VALUE)
5312b066
JJ
2166 {
2167 if (REG_P (y))
2168 {
24f8d71e
JJ
2169 struct elt_loc_list *l = NULL;
2170 if (CSELIB_VAL_PTR (x))
a5628378
AO
2171 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2172 l; l = l->next)
24f8d71e
JJ
2173 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2174 break;
5312b066
JJ
2175 if (l)
2176 x = y;
2177 else
2178 x = get_addr (x);
2179 }
2180 /* Don't call get_addr if y is the same VALUE. */
2181 else if (x != y)
2182 x = get_addr (x);
2183 }
eab5c70a 2184 if (GET_CODE (y) == VALUE)
5312b066
JJ
2185 {
2186 if (REG_P (x))
2187 {
24f8d71e
JJ
2188 struct elt_loc_list *l = NULL;
2189 if (CSELIB_VAL_PTR (y))
a5628378
AO
2190 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2191 l; l = l->next)
24f8d71e
JJ
2192 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2193 break;
5312b066
JJ
2194 if (l)
2195 y = x;
2196 else
2197 y = get_addr (y);
2198 }
2199 /* Don't call get_addr if x is the same VALUE. */
2200 else if (y != x)
2201 y = get_addr (y);
2202 }
9ae8ffe7
JL
2203 if (GET_CODE (x) == HIGH)
2204 x = XEXP (x, 0);
2205 else if (GET_CODE (x) == LO_SUM)
2206 x = XEXP (x, 1);
2207 else
3aa03517 2208 x = addr_side_effect_eval (x, abs (xsize), 0);
9ae8ffe7
JL
2209 if (GET_CODE (y) == HIGH)
2210 y = XEXP (y, 0);
2211 else if (GET_CODE (y) == LO_SUM)
2212 y = XEXP (y, 1);
2213 else
3aa03517 2214 y = addr_side_effect_eval (y, abs (ysize), 0);
9ae8ffe7
JL
2215
2216 if (rtx_equal_for_memref_p (x, y))
2217 {
3aa03517 2218 return offset_overlap_p (c, xsize, ysize);
9ae8ffe7
JL
2219 }
2220
6e73e666
JC
2221 /* This code used to check for conflicts involving stack references and
2222 globals but the base address alias code now handles these cases. */
9ae8ffe7
JL
2223
2224 if (GET_CODE (x) == PLUS)
2225 {
2226 /* The fact that X is canonicalized means that this
2227 PLUS rtx is canonicalized. */
2228 rtx x0 = XEXP (x, 0);
2229 rtx x1 = XEXP (x, 1);
2230
2231 if (GET_CODE (y) == PLUS)
2232 {
2233 /* The fact that Y is canonicalized means that this
2234 PLUS rtx is canonicalized. */
2235 rtx y0 = XEXP (y, 0);
2236 rtx y1 = XEXP (y, 1);
2237
2238 if (rtx_equal_for_memref_p (x1, y1))
2239 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2240 if (rtx_equal_for_memref_p (x0, y0))
2241 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
481683e1 2242 if (CONST_INT_P (x1))
63be02db 2243 {
481683e1 2244 if (CONST_INT_P (y1))
63be02db
JM
2245 return memrefs_conflict_p (xsize, x0, ysize, y0,
2246 c - INTVAL (x1) + INTVAL (y1));
2247 else
2248 return memrefs_conflict_p (xsize, x0, ysize, y,
2249 c - INTVAL (x1));
2250 }
481683e1 2251 else if (CONST_INT_P (y1))
9ae8ffe7
JL
2252 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2253
f47e08d9 2254 return -1;
9ae8ffe7 2255 }
481683e1 2256 else if (CONST_INT_P (x1))
9ae8ffe7
JL
2257 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2258 }
2259 else if (GET_CODE (y) == PLUS)
2260 {
2261 /* The fact that Y is canonicalized means that this
2262 PLUS rtx is canonicalized. */
2263 rtx y0 = XEXP (y, 0);
2264 rtx y1 = XEXP (y, 1);
2265
481683e1 2266 if (CONST_INT_P (y1))
9ae8ffe7
JL
2267 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2268 else
f47e08d9 2269 return -1;
9ae8ffe7
JL
2270 }
2271
2272 if (GET_CODE (x) == GET_CODE (y))
2273 switch (GET_CODE (x))
2274 {
2275 case MULT:
2276 {
2277 /* Handle cases where we expect the second operands to be the
2278 same, and check only whether the first operand would conflict
2279 or not. */
2280 rtx x0, y0;
2281 rtx x1 = canon_rtx (XEXP (x, 1));
2282 rtx y1 = canon_rtx (XEXP (y, 1));
2283 if (! rtx_equal_for_memref_p (x1, y1))
f47e08d9 2284 return -1;
9ae8ffe7
JL
2285 x0 = canon_rtx (XEXP (x, 0));
2286 y0 = canon_rtx (XEXP (y, 0));
2287 if (rtx_equal_for_memref_p (x0, y0))
3aa03517 2288 return offset_overlap_p (c, xsize, ysize);
9ae8ffe7
JL
2289
2290 /* Can't properly adjust our sizes. */
481683e1 2291 if (!CONST_INT_P (x1))
f47e08d9 2292 return -1;
9ae8ffe7
JL
2293 xsize /= INTVAL (x1);
2294 ysize /= INTVAL (x1);
2295 c /= INTVAL (x1);
2296 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2297 }
1d300e19
KG
2298
2299 default:
2300 break;
9ae8ffe7
JL
2301 }
2302
a9bf4fe2
AO
2303 /* Deal with alignment ANDs by adjusting offset and size so as to
2304 cover the maximum range, without taking any previously known
5147bf6a
AO
2305 alignment into account. Make a size negative after such an
2306 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2307 assume a potential overlap, because they may end up in contiguous
2308 memory locations and the stricter-alignment access may span over
2309 part of both. */
481683e1 2310 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
56ee9281 2311 {
a9bf4fe2
AO
2312 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2313 unsigned HOST_WIDE_INT uc = sc;
5147bf6a 2314 if (sc < 0 && -uc == (uc & -uc))
a9bf4fe2 2315 {
5147bf6a
AO
2316 if (xsize > 0)
2317 xsize = -xsize;
3aa03517
AO
2318 if (xsize)
2319 xsize += sc + 1;
fe8fb1c4 2320 c -= sc + 1;
a9bf4fe2
AO
2321 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2322 ysize, y, c);
2323 }
56ee9281 2324 }
481683e1 2325 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
c02f035f 2326 {
a9bf4fe2
AO
2327 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2328 unsigned HOST_WIDE_INT uc = sc;
5147bf6a 2329 if (sc < 0 && -uc == (uc & -uc))
a9bf4fe2 2330 {
5147bf6a
AO
2331 if (ysize > 0)
2332 ysize = -ysize;
3aa03517
AO
2333 if (ysize)
2334 ysize += sc + 1;
fe8fb1c4 2335 c += sc + 1;
a9bf4fe2
AO
2336 return memrefs_conflict_p (xsize, x,
2337 ysize, canon_rtx (XEXP (y, 0)), c);
2338 }
c02f035f 2339 }
9ae8ffe7
JL
2340
2341 if (CONSTANT_P (x))
2342 {
481683e1 2343 if (CONST_INT_P (x) && CONST_INT_P (y))
9ae8ffe7
JL
2344 {
2345 c += (INTVAL (y) - INTVAL (x));
3aa03517 2346 return offset_overlap_p (c, xsize, ysize);
9ae8ffe7
JL
2347 }
2348
2349 if (GET_CODE (x) == CONST)
2350 {
2351 if (GET_CODE (y) == CONST)
2352 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2353 ysize, canon_rtx (XEXP (y, 0)), c);
2354 else
2355 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2356 ysize, y, c);
2357 }
2358 if (GET_CODE (y) == CONST)
2359 return memrefs_conflict_p (xsize, x, ysize,
2360 canon_rtx (XEXP (y, 0)), c);
2361
3aa03517
AO
2362 /* Assume a potential overlap for symbolic addresses that went
2363 through alignment adjustments (i.e., that have negative
2364 sizes), because we can't know how far they are from each
2365 other. */
9ae8ffe7 2366 if (CONSTANT_P (y))
3aa03517 2367 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
9ae8ffe7 2368
f47e08d9 2369 return -1;
9ae8ffe7 2370 }
f47e08d9
RG
2371
2372 return -1;
9ae8ffe7
JL
2373}
2374
2375/* Functions to compute memory dependencies.
2376
2377 Since we process the insns in execution order, we can build tables
2378 to keep track of what registers are fixed (and not aliased), what registers
2379 are varying in known ways, and what registers are varying in unknown
2380 ways.
2381
2382 If both memory references are volatile, then there must always be a
2383 dependence between the two references, since their order can not be
2384 changed. A volatile and non-volatile reference can be interchanged
ca7fd9cd 2385 though.
9ae8ffe7 2386
53d9622b
RS
2387 We also must allow AND addresses, because they may generate accesses
2388 outside the object being referenced. This is used to generate aligned
2389 addresses from unaligned addresses, for instance, the alpha
dc1618bc 2390 storeqi_unaligned pattern. */
9ae8ffe7
JL
2391
2392/* Read dependence: X is read after read in MEM takes place. There can
96672a3e
RH
2393 only be a dependence here if both reads are volatile, or if either is
2394 an explicit barrier. */
9ae8ffe7
JL
2395
2396int
4f588890 2397read_dependence (const_rtx mem, const_rtx x)
9ae8ffe7 2398{
96672a3e
RH
2399 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2400 return true;
2401 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2402 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2403 return true;
2404 return false;
9ae8ffe7
JL
2405}
2406
998d7deb
RH
2407/* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2408
2409static tree
4682ae04 2410decl_for_component_ref (tree x)
998d7deb
RH
2411{
2412 do
2413 {
2414 x = TREE_OPERAND (x, 0);
2415 }
2416 while (x && TREE_CODE (x) == COMPONENT_REF);
2417
2418 return x && DECL_P (x) ? x : NULL_TREE;
2419}
2420
527210c4
RS
2421/* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2422 for the offset of the field reference. *KNOWN_P says whether the
2423 offset is known. */
998d7deb 2424
527210c4
RS
2425static void
2426adjust_offset_for_component_ref (tree x, bool *known_p,
2427 HOST_WIDE_INT *offset)
998d7deb 2428{
527210c4
RS
2429 if (!*known_p)
2430 return;
ca7fd9cd 2431 do
998d7deb 2432 {
527210c4 2433 tree xoffset = component_ref_field_offset (x);
998d7deb 2434 tree field = TREE_OPERAND (x, 1);
807e902e
KZ
2435 if (TREE_CODE (xoffset) != INTEGER_CST)
2436 {
2437 *known_p = false;
2438 return;
2439 }
998d7deb 2440
807e902e
KZ
2441 offset_int woffset
2442 = (wi::to_offset (xoffset)
2443 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2444 LOG2_BITS_PER_UNIT));
2445 if (!wi::fits_uhwi_p (woffset))
527210c4
RS
2446 {
2447 *known_p = false;
2448 return;
2449 }
807e902e 2450 *offset += woffset.to_uhwi ();
998d7deb
RH
2451
2452 x = TREE_OPERAND (x, 0);
2453 }
2454 while (x && TREE_CODE (x) == COMPONENT_REF);
998d7deb
RH
2455}
2456
95bd1dd7 2457/* Return nonzero if we can determine the exprs corresponding to memrefs
c6ea834c
BM
2458 X and Y and they do not overlap.
2459 If LOOP_VARIANT is set, skip offset-based disambiguation */
a4311dfe 2460
2e4e39f6 2461int
c6ea834c 2462nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
a4311dfe 2463{
998d7deb 2464 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
a4311dfe
RK
2465 rtx rtlx, rtly;
2466 rtx basex, basey;
527210c4
RS
2467 bool moffsetx_known_p, moffsety_known_p;
2468 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
a4311dfe
RK
2469 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2470
998d7deb
RH
2471 /* Unless both have exprs, we can't tell anything. */
2472 if (exprx == 0 || expry == 0)
2473 return 0;
2b22e382
RG
2474
2475 /* For spill-slot accesses make sure we have valid offsets. */
2476 if ((exprx == get_spill_slot_decl (false)
527210c4 2477 && ! MEM_OFFSET_KNOWN_P (x))
2b22e382 2478 || (expry == get_spill_slot_decl (false)
527210c4 2479 && ! MEM_OFFSET_KNOWN_P (y)))
2b22e382 2480 return 0;
c22cacf3 2481
998d7deb 2482 /* If the field reference test failed, look at the DECLs involved. */
527210c4
RS
2483 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2484 if (moffsetx_known_p)
2485 moffsetx = MEM_OFFSET (x);
998d7deb
RH
2486 if (TREE_CODE (exprx) == COMPONENT_REF)
2487 {
2e0c984c
RG
2488 tree t = decl_for_component_ref (exprx);
2489 if (! t)
2490 return 0;
527210c4 2491 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2e0c984c 2492 exprx = t;
998d7deb 2493 }
c67a1cf6 2494
527210c4
RS
2495 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2496 if (moffsety_known_p)
2497 moffsety = MEM_OFFSET (y);
998d7deb
RH
2498 if (TREE_CODE (expry) == COMPONENT_REF)
2499 {
2e0c984c
RG
2500 tree t = decl_for_component_ref (expry);
2501 if (! t)
2502 return 0;
527210c4 2503 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2e0c984c 2504 expry = t;
998d7deb
RH
2505 }
2506
2507 if (! DECL_P (exprx) || ! DECL_P (expry))
a4311dfe
RK
2508 return 0;
2509
1307c758
RG
2510 /* With invalid code we can end up storing into the constant pool.
2511 Bail out to avoid ICEing when creating RTL for this.
2512 See gfortran.dg/lto/20091028-2_0.f90. */
2513 if (TREE_CODE (exprx) == CONST_DECL
2514 || TREE_CODE (expry) == CONST_DECL)
2515 return 1;
2516
998d7deb
RH
2517 rtlx = DECL_RTL (exprx);
2518 rtly = DECL_RTL (expry);
a4311dfe 2519
1edcd60b
RK
2520 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2521 can't overlap unless they are the same because we never reuse that part
2522 of the stack frame used for locals for spilled pseudos. */
3c0cb5de 2523 if ((!MEM_P (rtlx) || !MEM_P (rtly))
1edcd60b 2524 && ! rtx_equal_p (rtlx, rtly))
a4311dfe
RK
2525 return 1;
2526
5932a4d4 2527 /* If we have MEMs referring to different address spaces (which can
09e881c9
BE
2528 potentially overlap), we cannot easily tell from the addresses
2529 whether the references overlap. */
2530 if (MEM_P (rtlx) && MEM_P (rtly)
2531 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2532 return 0;
2533
a4311dfe
RK
2534 /* Get the base and offsets of both decls. If either is a register, we
2535 know both are and are the same, so use that as the base. The only
2536 we can avoid overlap is if we can deduce that they are nonoverlapping
2537 pieces of that decl, which is very rare. */
3c0cb5de 2538 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
481683e1 2539 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
a4311dfe
RK
2540 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2541
3c0cb5de 2542 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
481683e1 2543 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
a4311dfe
RK
2544 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2545
d746694a 2546 /* If the bases are different, we know they do not overlap if both
ca7fd9cd 2547 are constants or if one is a constant and the other a pointer into the
d746694a
RK
2548 stack frame. Otherwise a different base means we can't tell if they
2549 overlap or not. */
2550 if (! rtx_equal_p (basex, basey))
ca7fd9cd
KH
2551 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2552 || (CONSTANT_P (basex) && REG_P (basey)
2553 && REGNO_PTR_FRAME_P (REGNO (basey)))
2554 || (CONSTANT_P (basey) && REG_P (basex)
2555 && REGNO_PTR_FRAME_P (REGNO (basex))));
a4311dfe 2556
c6ea834c
BM
2557 /* Offset based disambiguation not appropriate for loop invariant */
2558 if (loop_invariant)
2559 return 0;
2560
3c0cb5de 2561 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
f5541398 2562 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
a4311dfe 2563 : -1);
3c0cb5de 2564 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
f5541398
RS
2565 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2566 : -1);
a4311dfe 2567
0af5bc3e
RK
2568 /* If we have an offset for either memref, it can update the values computed
2569 above. */
527210c4
RS
2570 if (moffsetx_known_p)
2571 offsetx += moffsetx, sizex -= moffsetx;
2572 if (moffsety_known_p)
2573 offsety += moffsety, sizey -= moffsety;
a4311dfe 2574
0af5bc3e 2575 /* If a memref has both a size and an offset, we can use the smaller size.
efc981bb 2576 We can't do this if the offset isn't known because we must view this
0af5bc3e 2577 memref as being anywhere inside the DECL's MEM. */
527210c4 2578 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
f5541398 2579 sizex = MEM_SIZE (x);
527210c4 2580 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
f5541398 2581 sizey = MEM_SIZE (y);
a4311dfe
RK
2582
2583 /* Put the values of the memref with the lower offset in X's values. */
2584 if (offsetx > offsety)
2585 {
2586 tem = offsetx, offsetx = offsety, offsety = tem;
2587 tem = sizex, sizex = sizey, sizey = tem;
2588 }
2589
2590 /* If we don't know the size of the lower-offset value, we can't tell
2591 if they conflict. Otherwise, we do the test. */
a6f7c915 2592 return sizex >= 0 && offsety >= offsetx + sizex;
a4311dfe
RK
2593}
2594
9362286d
SB
2595/* Helper for true_dependence and canon_true_dependence.
2596 Checks for true dependence: X is read after store in MEM takes place.
9ae8ffe7 2597
9362286d
SB
2598 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2599 NULL_RTX, and the canonical addresses of MEM and X are both computed
2600 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2601
2602 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2603
2604 Returns 1 if there is a true dependence, 0 otherwise. */
2605
2606static int
ef4bddc2 2607true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
53d9622b 2608 const_rtx x, rtx x_addr, bool mem_canonicalized)
9ae8ffe7 2609{
0777fc02 2610 rtx true_mem_addr;
49982682 2611 rtx base;
f47e08d9 2612 int ret;
9ae8ffe7 2613
9362286d
SB
2614 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2615 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2616
9ae8ffe7
JL
2617 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2618 return 1;
2619
c4484b8f 2620 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
ac3768f6 2621 This is used in epilogue deallocation functions, and in cselib. */
c4484b8f
RH
2622 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2623 return 1;
2624 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2625 return 1;
9cd9e512
RH
2626 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2627 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2628 return 1;
c4484b8f 2629
0777fc02
UB
2630 if (! x_addr)
2631 x_addr = XEXP (x, 0);
2632 x_addr = get_addr (x_addr);
2633
9362286d
SB
2634 if (! mem_addr)
2635 {
2636 mem_addr = XEXP (mem, 0);
2637 if (mem_mode == VOIDmode)
2638 mem_mode = GET_MODE (mem);
2639 }
0777fc02 2640 true_mem_addr = get_addr (mem_addr);
eab5c70a 2641
878f5596
UB
2642 /* Read-only memory is by definition never modified, and therefore can't
2643 conflict with anything. However, don't assume anything when AND
2644 addresses are involved and leave to the code below to determine
2645 dependence. We don't expect to find read-only set on MEM, but
2646 stupid user tricks can produce them, so don't die. */
2647 if (MEM_READONLY_P (x)
2648 && GET_CODE (x_addr) != AND
0777fc02 2649 && GET_CODE (true_mem_addr) != AND)
878f5596
UB
2650 return 0;
2651
2652 /* If we have MEMs referring to different address spaces (which can
2653 potentially overlap), we cannot easily tell from the addresses
2654 whether the references overlap. */
2655 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2656 return 1;
2657
55efb413
JW
2658 base = find_base_term (x_addr);
2659 if (base && (GET_CODE (base) == LABEL_REF
2660 || (GET_CODE (base) == SYMBOL_REF
2661 && CONSTANT_POOL_ADDRESS_P (base))))
2662 return 0;
2663
0777fc02
UB
2664 rtx mem_base = find_base_term (true_mem_addr);
2665 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
31b0a960 2666 GET_MODE (x), mem_mode))
1c72c7f6
JC
2667 return 0;
2668
eab5c70a 2669 x_addr = canon_rtx (x_addr);
9362286d 2670 if (!mem_canonicalized)
0777fc02 2671 mem_addr = canon_rtx (true_mem_addr);
6e73e666 2672
f47e08d9
RG
2673 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2674 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2675 return ret;
2676
a95b3cc7 2677 if (mems_in_disjoint_alias_sets_p (x, mem))
f47e08d9
RG
2678 return 0;
2679
c6ea834c 2680 if (nonoverlapping_memrefs_p (mem, x, false))
0211b6ab 2681 return 0;
175a7536 2682
55b34b5f 2683 return rtx_refs_may_alias_p (x, mem, true);
a13d4ebf
AM
2684}
2685
9362286d
SB
2686/* True dependence: X is read after store in MEM takes place. */
2687
2688int
ef4bddc2 2689true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
9362286d
SB
2690{
2691 return true_dependence_1 (mem, mem_mode, NULL_RTX,
53d9622b 2692 x, NULL_RTX, /*mem_canonicalized=*/false);
9362286d
SB
2693}
2694
a13d4ebf 2695/* Canonical true dependence: X is read after store in MEM takes place.
ca7fd9cd
KH
2696 Variant of true_dependence which assumes MEM has already been
2697 canonicalized (hence we no longer do that here).
9362286d
SB
2698 The mem_addr argument has been added, since true_dependence_1 computed
2699 this value prior to canonicalizing. */
a13d4ebf
AM
2700
2701int
ef4bddc2 2702canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
53d9622b 2703 const_rtx x, rtx x_addr)
a13d4ebf 2704{
9362286d 2705 return true_dependence_1 (mem, mem_mode, mem_addr,
53d9622b 2706 x, x_addr, /*mem_canonicalized=*/true);
9ae8ffe7
JL
2707}
2708
da7d8304 2709/* Returns nonzero if a write to X might alias a previous read from
393f9fed 2710 (or, if WRITEP is true, a write to) MEM.
bd280792
JR
2711 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2712 and X_MODE the mode for that access.
2713 If MEM_CANONICALIZED is true, MEM is canonicalized. */
9ae8ffe7 2714
2c72b78f 2715static int
bd280792 2716write_dependence_p (const_rtx mem,
ef4bddc2 2717 const_rtx x, machine_mode x_mode, rtx x_addr,
bd280792 2718 bool mem_canonicalized, bool x_canonicalized, bool writep)
9ae8ffe7 2719{
bd280792 2720 rtx mem_addr;
0777fc02 2721 rtx true_mem_addr, true_x_addr;
49982682 2722 rtx base;
f47e08d9 2723 int ret;
6e73e666 2724
bd280792
JR
2725 gcc_checking_assert (x_canonicalized
2726 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2727 : (x_addr == NULL_RTX && x_mode == VOIDmode));
393f9fed 2728
9ae8ffe7
JL
2729 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2730 return 1;
2731
c4484b8f
RH
2732 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2733 This is used in epilogue deallocation functions. */
2734 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2735 return 1;
2736 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2737 return 1;
9cd9e512
RH
2738 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2739 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2740 return 1;
c4484b8f 2741
bd280792 2742 if (!x_addr)
0777fc02
UB
2743 x_addr = XEXP (x, 0);
2744 true_x_addr = get_addr (x_addr);
2745
2746 mem_addr = XEXP (mem, 0);
2747 true_mem_addr = get_addr (mem_addr);
55efb413 2748
878f5596
UB
2749 /* A read from read-only memory can't conflict with read-write memory.
2750 Don't assume anything when AND addresses are involved and leave to
2751 the code below to determine dependence. */
2752 if (!writep
2753 && MEM_READONLY_P (mem)
0777fc02
UB
2754 && GET_CODE (true_x_addr) != AND
2755 && GET_CODE (true_mem_addr) != AND)
878f5596
UB
2756 return 0;
2757
2758 /* If we have MEMs referring to different address spaces (which can
2759 potentially overlap), we cannot easily tell from the addresses
2760 whether the references overlap. */
2761 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2762 return 1;
2763
0777fc02 2764 base = find_base_term (true_mem_addr);
31b0a960
RB
2765 if (! writep
2766 && base
2767 && (GET_CODE (base) == LABEL_REF
2768 || (GET_CODE (base) == SYMBOL_REF
2769 && CONSTANT_POOL_ADDRESS_P (base))))
2770 return 0;
49982682 2771
0777fc02
UB
2772 rtx x_base = find_base_term (true_x_addr);
2773 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2774 GET_MODE (x), GET_MODE (mem)))
41472af8
MM
2775 return 0;
2776
bd280792 2777 if (!x_canonicalized)
393f9fed 2778 {
0777fc02 2779 x_addr = canon_rtx (true_x_addr);
bd280792 2780 x_mode = GET_MODE (x);
393f9fed 2781 }
bd280792 2782 if (!mem_canonicalized)
0777fc02 2783 mem_addr = canon_rtx (true_mem_addr);
6e73e666 2784
bd280792
JR
2785 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2786 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
f47e08d9
RG
2787 return ret;
2788
c6ea834c 2789 if (nonoverlapping_memrefs_p (x, mem, false))
c6df88cb
MM
2790 return 0;
2791
55b34b5f 2792 return rtx_refs_may_alias_p (x, mem, false);
c6df88cb
MM
2793}
2794
2795/* Anti dependence: X is written after read in MEM takes place. */
2796
2797int
4f588890 2798anti_dependence (const_rtx mem, const_rtx x)
c6df88cb 2799{
bd280792
JR
2800 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2801 /*mem_canonicalized=*/false,
2802 /*x_canonicalized*/false, /*writep=*/false);
393f9fed
JR
2803}
2804
bd280792
JR
2805/* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2806 Also, consider X in X_MODE (which might be from an enclosing
2807 STRICT_LOW_PART / ZERO_EXTRACT).
2808 If MEM_CANONICALIZED is true, MEM is canonicalized. */
393f9fed
JR
2809
2810int
bd280792 2811canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
ef4bddc2 2812 const_rtx x, machine_mode x_mode, rtx x_addr)
393f9fed 2813{
bd280792
JR
2814 return write_dependence_p (mem, x, x_mode, x_addr,
2815 mem_canonicalized, /*x_canonicalized=*/true,
2816 /*writep=*/false);
9ae8ffe7
JL
2817}
2818
2819/* Output dependence: X is written after store in MEM takes place. */
2820
2821int
4f588890 2822output_dependence (const_rtx mem, const_rtx x)
9ae8ffe7 2823{
bd280792
JR
2824 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2825 /*mem_canonicalized=*/false,
2826 /*x_canonicalized*/false, /*writep=*/true);
9ae8ffe7 2827}
c14b9960 2828\f
6e73e666 2829
c6ea834c
BM
2830
2831/* Check whether X may be aliased with MEM. Don't do offset-based
2832 memory disambiguation & TBAA. */
2833int
2834may_alias_p (const_rtx mem, const_rtx x)
2835{
2836 rtx x_addr, mem_addr;
c6ea834c
BM
2837
2838 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2839 return 1;
2840
a95b3cc7
RG
2841 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2842 This is used in epilogue deallocation functions. */
2843 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2844 return 1;
2845 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
c6ea834c 2846 return 1;
c6ea834c
BM
2847 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2848 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2849 return 1;
2850
c6ea834c 2851 x_addr = XEXP (x, 0);
0777fc02
UB
2852 x_addr = get_addr (x_addr);
2853
c6ea834c 2854 mem_addr = XEXP (mem, 0);
0777fc02 2855 mem_addr = get_addr (mem_addr);
c6ea834c 2856
878f5596
UB
2857 /* Read-only memory is by definition never modified, and therefore can't
2858 conflict with anything. However, don't assume anything when AND
2859 addresses are involved and leave to the code below to determine
2860 dependence. We don't expect to find read-only set on MEM, but
2861 stupid user tricks can produce them, so don't die. */
2862 if (MEM_READONLY_P (x)
2863 && GET_CODE (x_addr) != AND
2864 && GET_CODE (mem_addr) != AND)
2865 return 0;
2866
2867 /* If we have MEMs referring to different address spaces (which can
2868 potentially overlap), we cannot easily tell from the addresses
2869 whether the references overlap. */
2870 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2871 return 1;
2872
31b0a960
RB
2873 rtx x_base = find_base_term (x_addr);
2874 rtx mem_base = find_base_term (mem_addr);
2875 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2876 GET_MODE (x), GET_MODE (mem_addr)))
c6ea834c
BM
2877 return 0;
2878
c6ea834c
BM
2879 if (nonoverlapping_memrefs_p (mem, x, true))
2880 return 0;
2881
c6ea834c
BM
2882 /* TBAA not valid for loop_invarint */
2883 return rtx_refs_may_alias_p (x, mem, false);
2884}
2885
6e73e666 2886void
b5deb7b6 2887init_alias_target (void)
6e73e666 2888{
b3694847 2889 int i;
6e73e666 2890
9fc37b2b
RS
2891 if (!arg_base_value)
2892 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2893
b5deb7b6
SL
2894 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2895
6e73e666
JC
2896 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2897 /* Check whether this register can hold an incoming pointer
2898 argument. FUNCTION_ARG_REGNO_P tests outgoing register
ec5c56db 2899 numbers, so translate if necessary due to register windows. */
6e73e666
JC
2900 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2901 && HARD_REGNO_MODE_OK (i, Pmode))
9fc37b2b
RS
2902 static_reg_base_value[i] = arg_base_value;
2903
757e8ba2
JJ
2904 static_reg_base_value[STACK_POINTER_REGNUM]
2905 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2906 static_reg_base_value[ARG_POINTER_REGNUM]
2907 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2908 static_reg_base_value[FRAME_POINTER_REGNUM]
2909 = unique_base_value (UNIQUE_BASE_VALUE_FP);
c3e08036
TS
2910 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2911 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2912 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
bf1660a6
JL
2913}
2914
7b52eede
JH
2915/* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2916 to be memory reference. */
2917static bool memory_modified;
2918static void
aa317c97 2919memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
7b52eede 2920{
3c0cb5de 2921 if (MEM_P (x))
7b52eede 2922 {
9678086d 2923 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
7b52eede
JH
2924 memory_modified = true;
2925 }
2926}
2927
2928
2929/* Return true when INSN possibly modify memory contents of MEM
454ff5cb 2930 (i.e. address can be modified). */
7b52eede 2931bool
9678086d 2932memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
7b52eede
JH
2933{
2934 if (!INSN_P (insn))
2935 return false;
2936 memory_modified = false;
aa317c97 2937 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
7b52eede
JH
2938 return memory_modified;
2939}
2940
a7b159a4
AH
2941/* Return TRUE if the destination of a set is rtx identical to
2942 ITEM. */
2943static inline bool
2944set_dest_equal_p (const_rtx set, const_rtx item)
2945{
2946 rtx dest = SET_DEST (set);
2947 return rtx_equal_p (dest, item);
2948}
2949
2950/* Like memory_modified_in_insn_p, but return TRUE if INSN will
2951 *DEFINITELY* modify the memory contents of MEM. */
2952bool
2953memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2954{
2955 if (!INSN_P (insn))
2956 return false;
2957 insn = PATTERN (insn);
2958 if (GET_CODE (insn) == SET)
2959 return set_dest_equal_p (insn, mem);
2960 else if (GET_CODE (insn) == PARALLEL)
2961 {
2962 int i;
2963 for (i = 0; i < XVECLEN (insn, 0); i++)
2964 {
2965 rtx sub = XVECEXP (insn, 0, i);
2966 if (GET_CODE (sub) == SET
2967 && set_dest_equal_p (sub, mem))
2968 return true;
2969 }
2970 }
2971 return false;
2972}
2973
c13e8210
MM
2974/* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2975 array. */
2976
9ae8ffe7 2977void
4682ae04 2978init_alias_analysis (void)
9ae8ffe7 2979{
c582d54a 2980 unsigned int maxreg = max_reg_num ();
ea64ef27 2981 int changed, pass;
b3694847
SS
2982 int i;
2983 unsigned int ui;
d36a28b8
DM
2984 rtx_insn *insn;
2985 rtx val;
131db6b8
SB
2986 int rpo_cnt;
2987 int *rpo;
9ae8ffe7 2988
0d446150
JH
2989 timevar_push (TV_ALIAS_ANALYSIS);
2990
92390dd1 2991 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
9ff3c7ca 2992 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
dd3d1ec0 2993 bitmap_clear (reg_known_equiv_p);
9ae8ffe7 2994
08c79682 2995 /* If we have memory allocated from the previous run, use it. */
c582d54a 2996 if (old_reg_base_value)
08c79682
KH
2997 reg_base_value = old_reg_base_value;
2998
2999 if (reg_base_value)
9771b263 3000 reg_base_value->truncate (0);
08c79682 3001
9771b263 3002 vec_safe_grow_cleared (reg_base_value, maxreg);
ac606739 3003
5ed6ace5 3004 new_reg_base_value = XNEWVEC (rtx, maxreg);
d630245f 3005 reg_seen = sbitmap_alloc (maxreg);
ec907dd8
JL
3006
3007 /* The basic idea is that each pass through this loop will use the
3008 "constant" information from the previous pass to propagate alias
3009 information through another level of assignments.
3010
131db6b8
SB
3011 The propagation is done on the CFG in reverse post-order, to propagate
3012 things forward as far as possible in each iteration.
3013
ec907dd8
JL
3014 This could get expensive if the assignment chains are long. Maybe
3015 we should throttle the number of iterations, possibly based on
6e73e666 3016 the optimization level or flag_expensive_optimizations.
ec907dd8
JL
3017
3018 We could propagate more information in the first pass by making use
6fb5fa3c 3019 of DF_REG_DEF_COUNT to determine immediately that the alias information
ea64ef27
JL
3020 for a pseudo is "constant".
3021
3022 A program with an uninitialized variable can cause an infinite loop
3023 here. Instead of doing a full dataflow analysis to detect such problems
3024 we just cap the number of iterations for the loop.
3025
3026 The state of the arrays for the set chain in question does not matter
3027 since the program has undefined behavior. */
6e73e666 3028
0cae8d31 3029 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
131db6b8
SB
3030 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3031
e86a9946
RS
3032 /* The prologue/epilogue insns are not threaded onto the
3033 insn chain until after reload has completed. Thus,
3034 there is no sense wasting time checking if INSN is in
3035 the prologue/epilogue until after reload has completed. */
3036 bool could_be_prologue_epilogue = ((targetm.have_prologue ()
3037 || targetm.have_epilogue ())
3038 && reload_completed);
3039
ea64ef27 3040 pass = 0;
6e73e666 3041 do
ec907dd8
JL
3042 {
3043 /* Assume nothing will change this iteration of the loop. */
3044 changed = 0;
3045
ec907dd8 3046 /* We want to assign the same IDs each iteration of this loop, so
9fc37b2b
RS
3047 start counting from one each iteration of the loop. */
3048 unique_id = 1;
ec907dd8 3049
f5143c46 3050 /* We're at the start of the function each iteration through the
ec907dd8 3051 loop, so we're copying arguments. */
83bbd9b6 3052 copying_arguments = true;
9ae8ffe7 3053
6e73e666 3054 /* Wipe the potential alias information clean for this pass. */
c582d54a 3055 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
8072f69c 3056
6e73e666 3057 /* Wipe the reg_seen array clean. */
f61e445a 3058 bitmap_clear (reg_seen);
9ae8ffe7 3059
356610cb
EB
3060 /* Initialize the alias information for this pass. */
3061 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3062 if (static_reg_base_value[i])
3063 {
3064 new_reg_base_value[i] = static_reg_base_value[i];
3065 bitmap_set_bit (reg_seen, i);
3066 }
6e73e666 3067
ec907dd8 3068 /* Walk the insns adding values to the new_reg_base_value array. */
131db6b8 3069 for (i = 0; i < rpo_cnt; i++)
9ae8ffe7 3070 {
06e28de2 3071 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
131db6b8 3072 FOR_BB_INSNS (bb, insn)
ec907dd8 3073 {
131db6b8
SB
3074 if (NONDEBUG_INSN_P (insn))
3075 {
3076 rtx note, set;
efc9bd41 3077
e86a9946 3078 if (could_be_prologue_epilogue
131db6b8
SB
3079 && prologue_epilogue_contains (insn))
3080 continue;
efc9bd41 3081
131db6b8
SB
3082 /* If this insn has a noalias note, process it, Otherwise,
3083 scan for sets. A simple set will have no side effects
3084 which could change the base value of any other register. */
6e73e666 3085
131db6b8
SB
3086 if (GET_CODE (PATTERN (insn)) == SET
3087 && REG_NOTES (insn) != 0
3088 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3089 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3090 else
3091 note_stores (PATTERN (insn), record_set, NULL);
6e73e666 3092
131db6b8 3093 set = single_set (insn);
6e73e666 3094
131db6b8
SB
3095 if (set != 0
3096 && REG_P (SET_DEST (set))
3097 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
713f41f9 3098 {
131db6b8
SB
3099 unsigned int regno = REGNO (SET_DEST (set));
3100 rtx src = SET_SRC (set);
3101 rtx t;
3102
3103 note = find_reg_equal_equiv_note (insn);
3104 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3105 && DF_REG_DEF_COUNT (regno) != 1)
3106 note = NULL_RTX;
3107
3108 if (note != NULL_RTX
3109 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3110 && ! rtx_varies_p (XEXP (note, 0), 1)
3111 && ! reg_overlap_mentioned_p (SET_DEST (set),
3112 XEXP (note, 0)))
3113 {
3114 set_reg_known_value (regno, XEXP (note, 0));
3115 set_reg_known_equiv_p (regno,
3116 REG_NOTE_KIND (note) == REG_EQUIV);
3117 }
3118 else if (DF_REG_DEF_COUNT (regno) == 1
3119 && GET_CODE (src) == PLUS
3120 && REG_P (XEXP (src, 0))
3121 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3122 && CONST_INT_P (XEXP (src, 1)))
3123 {
3124 t = plus_constant (GET_MODE (src), t,
3125 INTVAL (XEXP (src, 1)));
3126 set_reg_known_value (regno, t);
3127 set_reg_known_equiv_p (regno, false);
3128 }
3129 else if (DF_REG_DEF_COUNT (regno) == 1
3130 && ! rtx_varies_p (src, 1))
3131 {
3132 set_reg_known_value (regno, src);
3133 set_reg_known_equiv_p (regno, false);
3134 }
713f41f9 3135 }
6e73e666 3136 }
131db6b8
SB
3137 else if (NOTE_P (insn)
3138 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3139 copying_arguments = false;
ec907dd8 3140 }
6e73e666 3141 }
ec907dd8 3142
6e73e666 3143 /* Now propagate values from new_reg_base_value to reg_base_value. */
62e5bf5d 3144 gcc_assert (maxreg == (unsigned int) max_reg_num ());
c22cacf3 3145
c582d54a 3146 for (ui = 0; ui < maxreg; ui++)
6e73e666 3147 {
e51712db 3148 if (new_reg_base_value[ui]
9771b263
DN
3149 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3150 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
ec907dd8 3151 {
9771b263 3152 (*reg_base_value)[ui] = new_reg_base_value[ui];
6e73e666 3153 changed = 1;
ec907dd8 3154 }
9ae8ffe7 3155 }
9ae8ffe7 3156 }
6e73e666 3157 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
131db6b8 3158 XDELETEVEC (rpo);
9ae8ffe7
JL
3159
3160 /* Fill in the remaining entries. */
9771b263 3161 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
9ff3c7ca
SB
3162 {
3163 int regno = i + FIRST_PSEUDO_REGISTER;
3164 if (! val)
3165 set_reg_known_value (regno, regno_reg_rtx[regno]);
3166 }
9ae8ffe7 3167
e05e2395
MM
3168 /* Clean up. */
3169 free (new_reg_base_value);
ec907dd8 3170 new_reg_base_value = 0;
d630245f 3171 sbitmap_free (reg_seen);
9ae8ffe7 3172 reg_seen = 0;
0d446150 3173 timevar_pop (TV_ALIAS_ANALYSIS);
9ae8ffe7
JL
3174}
3175
61630b27
JJ
3176/* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3177 Special API for var-tracking pass purposes. */
3178
3179void
3180vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3181{
9771b263 3182 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
61630b27
JJ
3183}
3184
9ae8ffe7 3185void
4682ae04 3186end_alias_analysis (void)
9ae8ffe7 3187{
c582d54a 3188 old_reg_base_value = reg_base_value;
9771b263 3189 vec_free (reg_known_value);
9ff3c7ca 3190 sbitmap_free (reg_known_equiv_p);
9ae8ffe7 3191}
e2500fed 3192
3ecf9d13
JH
3193void
3194dump_alias_stats_in_alias_c (FILE *s)
3195{
3196 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3197 " %llu are in alias set 0\n"
3198 " %llu queries asked about the same object\n"
3199 " %llu queries asked about the same alias set\n"
3200 " %llu access volatile\n"
6e042ef4
JH
3201 " %llu are dependent in the DAG\n"
3202 " %llu are aritificially in conflict with void *\n",
3ecf9d13
JH
3203 alias_stats.num_disambiguated,
3204 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3205 + alias_stats.num_same_objects + alias_stats.num_volatile
6e042ef4
JH
3206 + alias_stats.num_dag + alias_stats.num_disambiguated
3207 + alias_stats.num_universal,
3ecf9d13 3208 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
6e042ef4
JH
3209 alias_stats.num_same_objects, alias_stats.num_volatile,
3210 alias_stats.num_dag, alias_stats.num_universal);
3ecf9d13 3211}
e2500fed 3212#include "gt-alias.h"