]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/alias.c
* lto-symtab.c (lto_cgraph_replace_node): Update.
[thirdparty/gcc.git] / gcc / alias.c
CommitLineData
ea0cb7ae 1/* Alias analysis for GNU C
d4473c84 2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
7cf0dbf3 3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
ea0cb7ae 4 Contributed by John Carr (jfc@mit.edu).
5
f12b58b3 6This file is part of GCC.
ea0cb7ae 7
f12b58b3 8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
8c4c00c1 10Software Foundation; either version 3, or (at your option) any later
f12b58b3 11version.
ea0cb7ae 12
f12b58b3 13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
ea0cb7ae 17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
ea0cb7ae 21
22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
ea0cb7ae 26#include "rtl.h"
142131ed 27#include "tree.h"
7953c610 28#include "tm_p.h"
0a893c29 29#include "function.h"
4c74e6d9 30#include "alias.h"
31#include "emit-rtl.h"
ea0cb7ae 32#include "regs.h"
33#include "hard-reg-set.h"
080f162d 34#include "basic-block.h"
ea0cb7ae 35#include "flags.h"
a068b2a9 36#include "output.h"
0b205f4c 37#include "diagnostic-core.h"
1617c276 38#include "cselib.h"
a5b1863e 39#include "splay-tree.h"
c2e94b25 40#include "ggc.h"
b0278d39 41#include "langhooks.h"
376c21d1 42#include "timevar.h"
4f8e9d5c 43#include "target.h"
28992b23 44#include "cgraph.h"
77fce4cd 45#include "tree-pass.h"
3072d30e 46#include "df.h"
3a443843 47#include "tree-ssa-alias.h"
48#include "pointer-set.h"
49#include "tree-flow.h"
f7d118a9 50
51/* The aliasing API provided here solves related but different problems:
52
a0c938f0 53 Say there exists (in c)
f7d118a9 54
55 struct X {
56 struct Y y1;
57 struct Z z2;
58 } x1, *px1, *px2;
59
60 struct Y y2, *py;
61 struct Z z2, *pz;
62
63
64 py = &px1.y1;
65 px2 = &x1;
66
67 Consider the four questions:
68
69 Can a store to x1 interfere with px2->y1?
70 Can a store to x1 interfere with px2->z2?
71 (*px2).z2
72 Can a store to x1 change the value pointed to by with py?
73 Can a store to x1 change the value pointed to by with pz?
74
75 The answer to these questions can be yes, yes, yes, and maybe.
76
77 The first two questions can be answered with a simple examination
78 of the type system. If structure X contains a field of type Y then
79 a store thru a pointer to an X can overwrite any field that is
80 contained (recursively) in an X (unless we know that px1 != px2).
81
82 The last two of the questions can be solved in the same way as the
83 first two questions but this is too conservative. The observation
84 is that in some cases analysis we can know if which (if any) fields
85 are addressed and if those addresses are used in bad ways. This
86 analysis may be language specific. In C, arbitrary operations may
87 be applied to pointers. However, there is some indication that
88 this may be too conservative for some C++ types.
89
90 The pass ipa-type-escape does this analysis for the types whose
a0c938f0 91 instances do not escape across the compilation boundary.
f7d118a9 92
93 Historically in GCC, these two problems were combined and a single
94 data structure was used to represent the solution to these
95 problems. We now have two similar but different data structures,
96 The data structure to solve the last two question is similar to the
97 first, but does not contain have the fields in it whose address are
98 never taken. For types that do escape the compilation unit, the
99 data structures will have identical information.
100*/
a5b1863e 101
102/* The alias sets assigned to MEMs assist the back-end in determining
103 which MEMs can alias which other MEMs. In general, two MEMs in
5badbbc0 104 different alias sets cannot alias each other, with one important
105 exception. Consider something like:
a5b1863e 106
8e2ef041 107 struct S { int i; double d; };
a5b1863e 108
109 a store to an `S' can alias something of either type `int' or type
110 `double'. (However, a store to an `int' cannot alias a `double'
111 and vice versa.) We indicate this via a tree structure that looks
112 like:
a0c938f0 113 struct S
114 / \
a5b1863e 115 / \
a0c938f0 116 |/_ _\|
117 int double
a5b1863e 118
5badbbc0 119 (The arrows are directed and point downwards.)
120 In this situation we say the alias set for `struct S' is the
121 `superset' and that those for `int' and `double' are `subsets'.
122
f7c44134 123 To see whether two alias sets can point to the same memory, we must
124 see if either alias set is a subset of the other. We need not trace
d716ce75 125 past immediate descendants, however, since we propagate all
f7c44134 126 grandchildren up one level.
a5b1863e 127
128 Alias set zero is implicitly a superset of all other alias sets.
129 However, this is no actual entry for alias set zero. It is an
130 error to attempt to explicitly construct a subset of zero. */
131
26dbec0a 132struct GTY(()) alias_set_entry_d {
a5b1863e 133 /* The alias set number, as stored in MEM_ALIAS_SET. */
32c2fdea 134 alias_set_type alias_set;
a5b1863e 135
1e020227 136 /* Nonzero if would have a child of zero: this effectively makes this
137 alias set the same as alias set zero. */
138 int has_zero_child;
139
a5b1863e 140 /* The children of the alias set. These are not just the immediate
d716ce75 141 children, but, in fact, all descendants. So, if we have:
a5b1863e 142
9342ee68 143 struct T { struct S s; float f; }
a5b1863e 144
145 continuing our example above, the children here will be all of
146 `int', `double', `float', and `struct S'. */
318dcdd8 147 splay_tree GTY((param1_is (int), param2_is (int))) children;
318dcdd8 148};
26dbec0a 149typedef struct alias_set_entry_d *alias_set_entry;
ea0cb7ae 150
7ecb5bb2 151static int rtx_equal_for_memref_p (const_rtx, const_rtx);
aecda0d6 152static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
81a410b1 153static void record_set (rtx, const_rtx, void *);
aecda0d6 154static int base_alias_check (rtx, rtx, enum machine_mode,
155 enum machine_mode);
156static rtx find_base_value (rtx);
52d07779 157static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
aecda0d6 158static int insert_subset_children (splay_tree_node, void*);
32c2fdea 159static alias_set_entry get_alias_set_entry (alias_set_type);
52d07779 160static int aliases_everything_p (const_rtx);
161static bool nonoverlapping_component_refs_p (const_tree, const_tree);
aecda0d6 162static tree decl_for_component_ref (tree);
52d07779 163static int write_dependence_p (const_rtx, const_rtx, int);
aecda0d6 164
a80f1c6c 165static void memory_modified_1 (rtx, const_rtx, void *);
ea0cb7ae 166
167/* Set up all info needed to perform alias analysis on memory references. */
168
083a2b5e 169/* Returns the size in bytes of the mode of X. */
ea0cb7ae 170#define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
171
b5ba9f3a 172/* Returns nonzero if MEM1 and MEM2 do not alias because they are in
a068b2a9 173 different alias sets. We ignore alias sets in functions making use
174 of variable arguments because the va_arg macros on some systems are
175 not legal ANSI C. */
176#define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
a5b1863e 177 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
b5ba9f3a 178
37b864db 179/* Cap the number of passes we make over the insns propagating alias
5badbbc0 180 information through set chains. 10 is a completely arbitrary choice. */
37b864db 181#define MAX_ALIAS_LOOP_PASSES 10
9342ee68 182
ea0cb7ae 183/* reg_base_value[N] gives an address to which register N is related.
184 If all sets after the first add or subtract to the current value
185 or otherwise modify it so it does not point to a different top level
186 object, reg_base_value[N] is equal to the address part of the source
e7c893ba 187 of the first set.
188
189 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
4e56ceb1 190 expressions represent three types of base:
3a935f06 191
4e56ceb1 192 1. incoming arguments. There is just one ADDRESS to represent all
193 arguments, since we do not know at this level whether accesses
194 based on different arguments can alias. The ADDRESS has id 0.
3a935f06 195
4e56ceb1 196 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
197 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
198 Each of these rtxes has a separate ADDRESS associated with it,
199 each with a negative id.
200
201 GCC is (and is required to be) precise in which register it
202 chooses to access a particular region of stack. We can therefore
203 assume that accesses based on one of these rtxes do not alias
204 accesses based on another of these rtxes.
205
206 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
207 Each such piece of memory has a separate ADDRESS associated
208 with it, each with an id greater than 0.
209
210 Accesses based on one ADDRESS do not alias accesses based on other
211 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
212 alias globals either; the ADDRESSes have Pmode to indicate this.
213 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
214 indicate this. */
e7c893ba 215
980f3285 216static GTY(()) VEC(rtx,gc) *reg_base_value;
c2e94b25 217static rtx *new_reg_base_value;
e5b19a8c 218
4e56ceb1 219/* The single VOIDmode ADDRESS that represents all argument bases.
220 It has id 0. */
221static GTY(()) rtx arg_base_value;
222
223/* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
224static int unique_id;
225
e5b19a8c 226/* We preserve the copy of old array around to avoid amount of garbage
227 produced. About 8% of garbage produced were attributed to this
228 array. */
980f3285 229static GTY((deletable)) VEC(rtx,gc) *old_reg_base_value;
083a2b5e 230
eb2c25b5 231#define static_reg_base_value \
232 (this_target_rtl->x_static_reg_base_value)
0c7f5242 233
980f3285 234#define REG_BASE_VALUE(X) \
235 (REGNO (X) < VEC_length (rtx, reg_base_value) \
236 ? VEC_index (rtx, reg_base_value, REGNO (X)) : 0)
ea0cb7ae 237
73f5c1e3 238/* Vector indexed by N giving the initial (unchanging) value known for
12cb06ad 239 pseudo-register N. This array is initialized in init_alias_analysis,
240 and does not change until end_alias_analysis is called. */
241static GTY((length("reg_known_value_size"))) rtx *reg_known_value;
ea0cb7ae 242
243/* Indicates number of valid entries in reg_known_value. */
12cb06ad 244static GTY(()) unsigned int reg_known_value_size;
ea0cb7ae 245
246/* Vector recording for each reg_known_value whether it is due to a
247 REG_EQUIV note. Future passes (viz., reload) may replace the
248 pseudo with the equivalent expression and so we account for the
5badbbc0 249 dependences that would be introduced if that happens.
250
251 The REG_EQUIV notes created in assign_parms may mention the arg
252 pointer, and there are explicit insns in the RTL that modify the
253 arg pointer. Thus we must ensure that such insns don't get
254 scheduled across each other because that would invalidate the
255 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
256 wrong, but solving the problem in the scheduler will likely give
257 better code, so we do it here. */
12cb06ad 258static bool *reg_known_equiv_p;
ea0cb7ae 259
e7c893ba 260/* True when scanning insns from the start of the rtl to the
261 NOTE_INSN_FUNCTION_BEG note. */
e5be0cc6 262static bool copying_arguments;
ea0cb7ae 263
8b435339 264DEF_VEC_P(alias_set_entry);
265DEF_VEC_ALLOC_P(alias_set_entry,gc);
266
a5b1863e 267/* The splay-tree used to store the various alias set entries. */
8b435339 268static GTY (()) VEC(alias_set_entry,gc) *alias_sets;
5badbbc0 269\f
3a443843 270/* Build a decomposed reference object for querying the alias-oracle
271 from the MEM rtx and store it in *REF.
272 Returns false if MEM is not suitable for the alias-oracle. */
273
274static bool
275ao_ref_from_mem (ao_ref *ref, const_rtx mem)
276{
277 tree expr = MEM_EXPR (mem);
278 tree base;
279
280 if (!expr)
281 return false;
282
283 ao_ref_init (ref, expr);
284
285 /* Get the base of the reference and see if we have to reject or
286 adjust it. */
287 base = ao_ref_base (ref);
288 if (base == NULL_TREE)
289 return false;
290
1f1e1860 291 /* The tree oracle doesn't like to have these. */
292 if (TREE_CODE (base) == FUNCTION_DECL
293 || TREE_CODE (base) == LABEL_DECL)
294 return false;
295
3a443843 296 /* If this is a pointer dereference of a non-SSA_NAME punt.
297 ??? We could replace it with a pointer to anything. */
182cf5a9 298 if ((INDIRECT_REF_P (base)
299 || TREE_CODE (base) == MEM_REF)
3a443843 300 && TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME)
301 return false;
7e9c660e 302 if (TREE_CODE (base) == TARGET_MEM_REF
303 && TMR_BASE (base)
304 && TREE_CODE (TMR_BASE (base)) != SSA_NAME)
305 return false;
3a443843 306
307 /* If this is a reference based on a partitioned decl replace the
308 base with an INDIRECT_REF of the pointer representative we
309 created during stack slot partitioning. */
310 if (TREE_CODE (base) == VAR_DECL
311 && ! TREE_STATIC (base)
312 && cfun->gimple_df->decls_to_pointers != NULL)
313 {
314 void *namep;
315 namep = pointer_map_contains (cfun->gimple_df->decls_to_pointers, base);
316 if (namep)
182cf5a9 317 ref->base = build_simple_mem_ref (*(tree *)namep);
3a443843 318 }
7e9c660e 319 else if (TREE_CODE (base) == TARGET_MEM_REF
28daba6f 320 && TREE_CODE (TMR_BASE (base)) == ADDR_EXPR
321 && TREE_CODE (TREE_OPERAND (TMR_BASE (base), 0)) == VAR_DECL
322 && ! TREE_STATIC (TREE_OPERAND (TMR_BASE (base), 0))
7e9c660e 323 && cfun->gimple_df->decls_to_pointers != NULL)
324 {
325 void *namep;
326 namep = pointer_map_contains (cfun->gimple_df->decls_to_pointers,
28daba6f 327 TREE_OPERAND (TMR_BASE (base), 0));
7e9c660e 328 if (namep)
329 ref->base = build_simple_mem_ref (*(tree *)namep);
330 }
3a443843 331
332 ref->ref_alias_set = MEM_ALIAS_SET (mem);
333
5b2a69fa 334 /* If MEM_OFFSET or MEM_SIZE are unknown we have to punt.
1f1e1860 335 Keep points-to related information though. */
da443c27 336 if (!MEM_OFFSET_KNOWN_P (mem)
5b2a69fa 337 || !MEM_SIZE_KNOWN_P (mem))
1f1e1860 338 {
339 ref->ref = NULL_TREE;
340 ref->offset = 0;
341 ref->size = -1;
342 ref->max_size = -1;
343 return true;
344 }
345
3804203d 346 /* If the base decl is a parameter we can have negative MEM_OFFSET in
347 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
348 here. */
da443c27 349 if (MEM_OFFSET (mem) < 0
350 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
3804203d 351 return true;
352
da443c27 353 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
5b2a69fa 354 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
3804203d 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)
367 && (!host_integerp (DECL_SIZE (ref->base), 1)
368 || (TREE_INT_CST_LOW (DECL_SIZE ((ref->base)))
369 < (unsigned HOST_WIDE_INT)(ref->offset + ref->size))))))
370 return false;
3a443843 371
372 return true;
373}
374
375/* Query the alias-oracle on whether the two memory rtx X and MEM may
376 alias. If TBAA_P is set also apply TBAA. Returns true if the
377 two rtxen may alias, false otherwise. */
378
379static bool
380rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
381{
382 ao_ref ref1, ref2;
383
384 if (!ao_ref_from_mem (&ref1, x)
385 || !ao_ref_from_mem (&ref2, mem))
386 return true;
387
33dda198 388 return refs_may_alias_p_1 (&ref1, &ref2,
389 tbaa_p
390 && MEM_ALIAS_SET (x) != 0
391 && MEM_ALIAS_SET (mem) != 0);
3a443843 392}
393
a5b1863e 394/* Returns a pointer to the alias set entry for ALIAS_SET, if there is
395 such an entry, or NULL otherwise. */
396
56bbdce4 397static inline alias_set_entry
32c2fdea 398get_alias_set_entry (alias_set_type alias_set)
a5b1863e 399{
8b435339 400 return VEC_index (alias_set_entry, alias_sets, alias_set);
a5b1863e 401}
402
5badbbc0 403/* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
404 the two MEMs cannot alias each other. */
a5b1863e 405
56bbdce4 406static inline int
52d07779 407mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
a5b1863e 408{
a5b1863e 409/* Perform a basic sanity check. Namely, that there are no alias sets
410 if we're not using strict aliasing. This helps to catch bugs
411 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
412 where a MEM is allocated in some way other than by the use of
413 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
414 use alias sets to indicate that spilled registers cannot alias each
415 other, we might need to remove this check. */
64db345d 416 gcc_assert (flag_strict_aliasing
417 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
a5b1863e 418
387bc205 419 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
420}
a5b1863e 421
387bc205 422/* Insert the NODE into the splay tree given by DATA. Used by
423 record_alias_subset via splay_tree_foreach. */
424
425static int
aecda0d6 426insert_subset_children (splay_tree_node node, void *data)
387bc205 427{
428 splay_tree_insert ((splay_tree) data, node->key, node->value);
429
430 return 0;
431}
432
7d1f52b2 433/* Return true if the first alias set is a subset of the second. */
434
435bool
32c2fdea 436alias_set_subset_of (alias_set_type set1, alias_set_type set2)
7d1f52b2 437{
438 alias_set_entry ase;
439
440 /* Everything is a subset of the "aliases everything" set. */
441 if (set2 == 0)
442 return true;
443
444 /* Otherwise, check if set1 is a subset of set2. */
445 ase = get_alias_set_entry (set2);
446 if (ase != 0
a5b373b8 447 && (ase->has_zero_child
6c711ccf 448 || splay_tree_lookup (ase->children,
449 (splay_tree_key) set1)))
7d1f52b2 450 return true;
451 return false;
452}
453
387bc205 454/* Return 1 if the two specified alias sets may conflict. */
455
456int
32c2fdea 457alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
387bc205 458{
459 alias_set_entry ase;
460
879f881c 461 /* The easy case. */
462 if (alias_sets_must_conflict_p (set1, set2))
387bc205 463 return 1;
a5b1863e 464
f7c44134 465 /* See if the first alias set is a subset of the second. */
387bc205 466 ase = get_alias_set_entry (set1);
851dfbff 467 if (ase != 0
468 && (ase->has_zero_child
469 || splay_tree_lookup (ase->children,
387bc205 470 (splay_tree_key) set2)))
471 return 1;
a5b1863e 472
473 /* Now do the same, but with the alias sets reversed. */
387bc205 474 ase = get_alias_set_entry (set2);
851dfbff 475 if (ase != 0
476 && (ase->has_zero_child
477 || splay_tree_lookup (ase->children,
387bc205 478 (splay_tree_key) set1)))
479 return 1;
a5b1863e 480
387bc205 481 /* The two alias sets are distinct and neither one is the
879f881c 482 child of the other. Therefore, they cannot conflict. */
387bc205 483 return 0;
a5b1863e 484}
c1628b55 485
879f881c 486/* Return 1 if the two specified alias sets will always conflict. */
c1628b55 487
488int
32c2fdea 489alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
c1628b55 490{
491 if (set1 == 0 || set2 == 0 || set1 == set2)
492 return 1;
493
494 return 0;
495}
496
387bc205 497/* Return 1 if any MEM object of type T1 will always conflict (using the
498 dependency routines in this file) with any MEM object of type T2.
499 This is used when allocating temporary storage. If T1 and/or T2 are
500 NULL_TREE, it means we know nothing about the storage. */
501
502int
aecda0d6 503objects_must_conflict_p (tree t1, tree t2)
387bc205 504{
32c2fdea 505 alias_set_type set1, set2;
2bedf233 506
218eb8d0 507 /* If neither has a type specified, we don't know if they'll conflict
508 because we may be using them to store objects of various types, for
509 example the argument and local variables areas of inlined functions. */
8f2ba9c9 510 if (t1 == 0 && t2 == 0)
218eb8d0 511 return 0;
512
387bc205 513 /* If they are the same type, they must conflict. */
514 if (t1 == t2
515 /* Likewise if both are volatile. */
516 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
517 return 1;
518
2bedf233 519 set1 = t1 ? get_alias_set (t1) : 0;
520 set2 = t2 ? get_alias_set (t2) : 0;
387bc205 521
879f881c 522 /* We can't use alias_sets_conflict_p because we must make sure
523 that every subtype of t1 will conflict with every subtype of
2bedf233 524 t2 for which a pair of subobjects of these respective subtypes
525 overlaps on the stack. */
879f881c 526 return alias_sets_must_conflict_p (set1, set2);
387bc205 527}
528\f
2b02580f 529/* Return true if all nested component references handled by
530 get_inner_reference in T are such that we should use the alias set
531 provided by the object at the heart of T.
532
533 This is true for non-addressable components (which don't have their
534 own alias set), as well as components of objects in alias set zero.
535 This later point is a special case wherein we wish to override the
536 alias set used by the component, but we don't have per-FIELD_DECL
537 assignable alias sets. */
538
539bool
1f1872fd 540component_uses_parent_alias_set (const_tree t)
bd32979f 541{
1f9b622b 542 while (1)
543 {
2b02580f 544 /* If we're at the end, it vacuously uses its own alias set. */
1f9b622b 545 if (!handled_component_p (t))
2b02580f 546 return false;
1f9b622b 547
548 switch (TREE_CODE (t))
549 {
550 case COMPONENT_REF:
551 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
2b02580f 552 return true;
1f9b622b 553 break;
554
555 case ARRAY_REF:
556 case ARRAY_RANGE_REF:
557 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
2b02580f 558 return true;
1f9b622b 559 break;
560
561 case REALPART_EXPR:
562 case IMAGPART_EXPR:
563 break;
564
565 default:
566 /* Bitfields and casts are never addressable. */
2b02580f 567 return true;
1f9b622b 568 }
569
570 t = TREE_OPERAND (t, 0);
2b02580f 571 if (get_alias_set (TREE_TYPE (t)) == 0)
572 return true;
1f9b622b 573 }
bd32979f 574}
575
dd277d48 576/* Return the alias set for the memory pointed to by T, which may be
577 either a type or an expression. Return -1 if there is nothing
578 special about dereferencing T. */
579
580static alias_set_type
581get_deref_alias_set_1 (tree t)
582{
583 /* If we're not doing any alias analysis, just assume everything
584 aliases everything else. */
585 if (!flag_strict_aliasing)
586 return 0;
587
8115f0af 588 /* All we care about is the type. */
dd277d48 589 if (! TYPE_P (t))
8115f0af 590 t = TREE_TYPE (t);
dd277d48 591
592 /* If we have an INDIRECT_REF via a void pointer, we don't
593 know anything about what that might alias. Likewise if the
594 pointer is marked that way. */
595 if (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
596 || TYPE_REF_CAN_ALIAS_ALL (t))
597 return 0;
598
599 return -1;
600}
601
602/* Return the alias set for the memory pointed to by T, which may be
603 either a type or an expression. */
604
605alias_set_type
606get_deref_alias_set (tree t)
607{
608 alias_set_type set = get_deref_alias_set_1 (t);
609
610 /* Fall back to the alias-set of the pointed-to type. */
611 if (set == -1)
612 {
613 if (! TYPE_P (t))
614 t = TREE_TYPE (t);
615 set = get_alias_set (TREE_TYPE (t));
616 }
617
618 return set;
619}
620
f7c44134 621/* Return the alias set for T, which may be either a type or an
622 expression. Call language-specific routine for help, if needed. */
623
32c2fdea 624alias_set_type
aecda0d6 625get_alias_set (tree t)
f7c44134 626{
32c2fdea 627 alias_set_type set;
f7c44134 628
629 /* If we're not doing any alias analysis, just assume everything
630 aliases everything else. Also return 0 if this or its type is
631 an error. */
632 if (! flag_strict_aliasing || t == error_mark_node
633 || (! TYPE_P (t)
634 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
635 return 0;
636
637 /* We can be passed either an expression or a type. This and the
c3a9c149 638 language-specific routine may make mutually-recursive calls to each other
639 to figure out what to do. At each juncture, we see if this is a tree
640 that the language may need to handle specially. First handle things that
96216d37 641 aren't types. */
1607663f 642 if (! TYPE_P (t))
f7c44134 643 {
38e63987 644 tree inner;
96216d37 645
182cf5a9 646 /* Give the language a chance to do something with this tree
647 before we look at it. */
2a631e19 648 STRIP_NOPS (t);
dc24ddbd 649 set = lang_hooks.get_alias_set (t);
2a631e19 650 if (set != -1)
651 return set;
652
182cf5a9 653 /* Get the base object of the reference. */
38e63987 654 inner = t;
55f9d7dc 655 while (handled_component_p (inner))
96216d37 656 {
182cf5a9 657 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
658 the type of any component references that wrap it to
659 determine the alias-set. */
660 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
661 t = TREE_OPERAND (inner, 0);
55f9d7dc 662 inner = TREE_OPERAND (inner, 0);
96216d37 663 }
664
182cf5a9 665 /* Handle pointer dereferences here, they can override the
666 alias-set. */
2a448a75 667 if (INDIRECT_REF_P (inner))
96216d37 668 {
dd277d48 669 set = get_deref_alias_set_1 (TREE_OPERAND (inner, 0));
670 if (set != -1)
671 return set;
96216d37 672 }
9a14ba4f 673 else if (TREE_CODE (inner) == TARGET_MEM_REF)
674 return get_deref_alias_set (TMR_OFFSET (inner));
182cf5a9 675 else if (TREE_CODE (inner) == MEM_REF)
676 {
677 set = get_deref_alias_set_1 (TREE_OPERAND (inner, 1));
678 if (set != -1)
679 return set;
680 }
681
682 /* If the innermost reference is a MEM_REF that has a
683 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
684 using the memory access type for determining the alias-set. */
685 if (TREE_CODE (inner) == MEM_REF
ec7b61e0 686 && TYPE_MAIN_VARIANT (TREE_TYPE (inner))
687 != TYPE_MAIN_VARIANT
688 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1)))))
182cf5a9 689 return get_deref_alias_set (TREE_OPERAND (inner, 1));
96216d37 690
691 /* Otherwise, pick up the outermost object that we could have a pointer
55f9d7dc 692 to, processing conversions as above. */
2b02580f 693 while (component_uses_parent_alias_set (t))
c3a9c149 694 {
55f9d7dc 695 t = TREE_OPERAND (t, 0);
2a631e19 696 STRIP_NOPS (t);
697 }
1607663f 698
96216d37 699 /* If we've already determined the alias set for a decl, just return
700 it. This is necessary for C++ anonymous unions, whose component
701 variables don't look like union members (boo!). */
afce1f57 702 if (TREE_CODE (t) == VAR_DECL
e16ceb8e 703 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
afce1f57 704 return MEM_ALIAS_SET (DECL_RTL (t));
705
1607663f 706 /* Now all we care about is the type. */
707 t = TREE_TYPE (t);
f7c44134 708 }
709
f7c44134 710 /* Variant qualifiers don't affect the alias set, so get the main
dfe8e806 711 variant. */
f7c44134 712 t = TYPE_MAIN_VARIANT (t);
dfe8e806 713
714 /* Always use the canonical type as well. If this is a type that
715 requires structural comparisons to identify compatible types
716 use alias set zero. */
717 if (TYPE_STRUCTURAL_EQUALITY_P (t))
e58c17e7 718 {
719 /* Allow the language to specify another alias set for this
720 type. */
721 set = lang_hooks.get_alias_set (t);
722 if (set != -1)
723 return set;
724 return 0;
725 }
ec7b61e0 726
dfe8e806 727 t = TYPE_CANONICAL (t);
ec7b61e0 728
45014c84 729 /* The canonical type should not require structural equality checks. */
730 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
dfe8e806 731
732 /* If this is a type with a known alias set, return it. */
96216d37 733 if (TYPE_ALIAS_SET_KNOWN_P (t))
f7c44134 734 return TYPE_ALIAS_SET (t);
735
b35a8f48 736 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
737 if (!COMPLETE_TYPE_P (t))
738 {
739 /* For arrays with unknown size the conservative answer is the
740 alias set of the element type. */
741 if (TREE_CODE (t) == ARRAY_TYPE)
742 return get_alias_set (TREE_TYPE (t));
743
744 /* But return zero as a conservative answer for incomplete types. */
745 return 0;
746 }
747
f7c44134 748 /* See if the language has special handling for this type. */
dc24ddbd 749 set = lang_hooks.get_alias_set (t);
2a631e19 750 if (set != -1)
96216d37 751 return set;
851dfbff 752
f7c44134 753 /* There are no objects of FUNCTION_TYPE, so there's no point in
754 using up an alias set for them. (There are, of course, pointers
755 and references to functions, but that's different.) */
ec7b61e0 756 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
f7c44134 757 set = 0;
9fcc3e54 758
759 /* Unless the language specifies otherwise, let vector types alias
760 their components. This avoids some nasty type punning issues in
761 normal usage. And indeed lets vectors be treated more like an
762 array slice. */
763 else if (TREE_CODE (t) == VECTOR_TYPE)
764 set = get_alias_set (TREE_TYPE (t));
765
62d823d0 766 /* Unless the language specifies otherwise, treat array types the
767 same as their components. This avoids the asymmetry we get
768 through recording the components. Consider accessing a
769 character(kind=1) through a reference to a character(kind=1)[1:1].
770 Or consider if we want to assign integer(kind=4)[0:D.1387] and
771 integer(kind=4)[4] the same alias set or not.
772 Just be pragmatic here and make sure the array and its element
773 type get the same alias set assigned. */
ec7b61e0 774 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
62d823d0 775 set = get_alias_set (TREE_TYPE (t));
776
2af087f2 777 /* From the former common C and C++ langhook implementation:
778
779 Unfortunately, there is no canonical form of a pointer type.
780 In particular, if we have `typedef int I', then `int *', and
781 `I *' are different types. So, we have to pick a canonical
782 representative. We do this below.
783
784 Technically, this approach is actually more conservative that
785 it needs to be. In particular, `const int *' and `int *'
786 should be in different alias sets, according to the C and C++
787 standard, since their types are not the same, and so,
788 technically, an `int **' and `const int **' cannot point at
789 the same thing.
790
791 But, the standard is wrong. In particular, this code is
792 legal C++:
793
794 int *ip;
795 int **ipp = &ip;
796 const int* const* cipp = ipp;
797 And, it doesn't make sense for that to be legal unless you
798 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
799 the pointed-to types. This issue has been reported to the
800 C++ committee.
801
802 In addition to the above canonicalization issue, with LTO
803 we should also canonicalize `T (*)[]' to `T *' avoiding
804 alias issues with pointer-to element types and pointer-to
805 array types.
806
807 Likewise we need to deal with the situation of incomplete
808 pointed-to types and make `*(struct X **)&a' and
809 `*(struct X {} **)&a' alias. Otherwise we will have to
810 guarantee that all pointer-to incomplete type variants
811 will be replaced by pointer-to complete type variants if
812 they are available.
813
814 With LTO the convenient situation of using `void *' to
815 access and store any pointer type will also become
816 more apparent (and `void *' is just another pointer-to
817 incomplete type). Assigning alias-set zero to `void *'
818 and all pointer-to incomplete types is a not appealing
819 solution. Assigning an effective alias-set zero only
820 affecting pointers might be - by recording proper subset
821 relationships of all pointer alias-sets.
822
823 Pointer-to function types are another grey area which
824 needs caution. Globbing them all into one alias-set
825 or the above effective zero set would work.
826
827 For now just assign the same alias-set to all pointers.
828 That's simple and avoids all the above problems. */
829 else if (POINTER_TYPE_P (t)
830 && t != ptr_type_node)
45014c84 831 set = get_alias_set (ptr_type_node);
2af087f2 832
ec7b61e0 833 /* Otherwise make a new alias set for this type. */
f7c44134 834 else
45014c84 835 {
836 /* Each canonical type gets its own alias set, so canonical types
837 shouldn't form a tree. It doesn't really matter for types
838 we handle specially above, so only check it where it possibly
839 would result in a bogus alias set. */
840 gcc_checking_assert (TYPE_CANONICAL (t) == t);
841
842 set = new_alias_set ();
843 }
f7c44134 844
845 TYPE_ALIAS_SET (t) = set;
851dfbff 846
ec7b61e0 847 /* If this is an aggregate type or a complex type, we must record any
848 component aliasing information. */
680210f2 849 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
851dfbff 850 record_component_aliases (t);
851
f7c44134 852 return set;
853}
854
855/* Return a brand-new alias set. */
856
32c2fdea 857alias_set_type
aecda0d6 858new_alias_set (void)
f7c44134 859{
f7c44134 860 if (flag_strict_aliasing)
56bbdce4 861 {
8b435339 862 if (alias_sets == 0)
863 VEC_safe_push (alias_set_entry, gc, alias_sets, 0);
864 VEC_safe_push (alias_set_entry, gc, alias_sets, 0);
865 return VEC_length (alias_set_entry, alias_sets) - 1;
56bbdce4 866 }
f7c44134 867 else
868 return 0;
869}
a5b1863e 870
8e2ef041 871/* Indicate that things in SUBSET can alias things in SUPERSET, but that
872 not everything that aliases SUPERSET also aliases SUBSET. For example,
873 in C, a store to an `int' can alias a load of a structure containing an
874 `int', and vice versa. But it can't alias a load of a 'double' member
875 of the same structure. Here, the structure would be the SUPERSET and
876 `int' the SUBSET. This relationship is also described in the comment at
877 the beginning of this file.
878
879 This function should be called only once per SUPERSET/SUBSET pair.
a5b1863e 880
881 It is illegal for SUPERSET to be zero; everything is implicitly a
882 subset of alias set zero. */
883
892fdb03 884void
32c2fdea 885record_alias_subset (alias_set_type superset, alias_set_type subset)
a5b1863e 886{
887 alias_set_entry superset_entry;
888 alias_set_entry subset_entry;
889
c3a9c149 890 /* It is possible in complex type situations for both sets to be the same,
891 in which case we can ignore this operation. */
892 if (superset == subset)
893 return;
894
64db345d 895 gcc_assert (superset);
a5b1863e 896
897 superset_entry = get_alias_set_entry (superset);
9342ee68 898 if (superset_entry == 0)
a5b1863e 899 {
900 /* Create an entry for the SUPERSET, so that we have a place to
901 attach the SUBSET. */
ba72912a 902 superset_entry = ggc_alloc_cleared_alias_set_entry_d ();
a5b1863e 903 superset_entry->alias_set = superset;
9342ee68 904 superset_entry->children
ba72912a 905 = splay_tree_new_ggc (splay_tree_compare_ints,
906 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s,
907 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s);
f2e5de80 908 superset_entry->has_zero_child = 0;
8b435339 909 VEC_replace (alias_set_entry, alias_sets, superset, superset_entry);
a5b1863e 910 }
911
851dfbff 912 if (subset == 0)
913 superset_entry->has_zero_child = 1;
914 else
915 {
916 subset_entry = get_alias_set_entry (subset);
917 /* If there is an entry for the subset, enter all of its children
918 (if they are not already present) as children of the SUPERSET. */
9342ee68 919 if (subset_entry)
851dfbff 920 {
921 if (subset_entry->has_zero_child)
922 superset_entry->has_zero_child = 1;
083a2b5e 923
851dfbff 924 splay_tree_foreach (subset_entry->children, insert_subset_children,
925 superset_entry->children);
926 }
a5b1863e 927
851dfbff 928 /* Enter the SUBSET itself as a child of the SUPERSET. */
9342ee68 929 splay_tree_insert (superset_entry->children,
851dfbff 930 (splay_tree_key) subset, 0);
931 }
a5b1863e 932}
933
9501a338 934/* Record that component types of TYPE, if any, are part of that type for
935 aliasing purposes. For record types, we only record component types
c67bf066 936 for fields that are not marked non-addressable. For array types, we
937 only record the component type if it is not marked non-aliased. */
9501a338 938
939void
aecda0d6 940record_component_aliases (tree type)
9501a338 941{
32c2fdea 942 alias_set_type superset = get_alias_set (type);
9501a338 943 tree field;
944
945 if (superset == 0)
946 return;
947
948 switch (TREE_CODE (type))
949 {
9501a338 950 case RECORD_TYPE:
951 case UNION_TYPE:
952 case QUAL_UNION_TYPE:
8b332087 953 /* Recursively record aliases for the base classes, if there are any. */
f6cc6a08 954 if (TYPE_BINFO (type))
9342ee68 955 {
956 int i;
f6cc6a08 957 tree binfo, base_binfo;
a0c938f0 958
f6cc6a08 959 for (binfo = TYPE_BINFO (type), i = 0;
960 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
961 record_alias_subset (superset,
962 get_alias_set (BINFO_TYPE (base_binfo)));
9342ee68 963 }
1767a056 964 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
c67bf066 965 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
851dfbff 966 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
9501a338 967 break;
968
680210f2 969 case COMPLEX_TYPE:
970 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
971 break;
972
62d823d0 973 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
974 element type. */
975
9501a338 976 default:
977 break;
978 }
979}
980
f7c44134 981/* Allocate an alias set for use in storing and reading from the varargs
982 spill area. */
983
32c2fdea 984static GTY(()) alias_set_type varargs_set = -1;
4ec9406e 985
32c2fdea 986alias_set_type
aecda0d6 987get_varargs_alias_set (void)
f7c44134 988{
fcdd3ab3 989#if 1
990 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
991 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
992 consistently use the varargs alias set for loads from the varargs
993 area. So don't use it anywhere. */
994 return 0;
995#else
4ec9406e 996 if (varargs_set == -1)
997 varargs_set = new_alias_set ();
f7c44134 998
4ec9406e 999 return varargs_set;
fcdd3ab3 1000#endif
f7c44134 1001}
1002
1003/* Likewise, but used for the fixed portions of the frame, e.g., register
1004 save areas. */
1005
32c2fdea 1006static GTY(()) alias_set_type frame_set = -1;
4ec9406e 1007
32c2fdea 1008alias_set_type
aecda0d6 1009get_frame_alias_set (void)
f7c44134 1010{
4ec9406e 1011 if (frame_set == -1)
1012 frame_set = new_alias_set ();
f7c44134 1013
4ec9406e 1014 return frame_set;
f7c44134 1015}
1016
4e56ceb1 1017/* Create a new, unique base with id ID. */
1018
1019static rtx
1020unique_base_value (HOST_WIDE_INT id)
1021{
1022 return gen_rtx_ADDRESS (Pmode, id);
1023}
1024
1025/* Return true if accesses based on any other base value cannot alias
1026 those based on X. */
1027
1028static bool
1029unique_base_value_p (rtx x)
1030{
1031 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1032}
1033
1034/* Return true if X is known to be a base value. */
1035
1036static bool
1037known_base_value_p (rtx x)
1038{
1039 switch (GET_CODE (x))
1040 {
1041 case LABEL_REF:
1042 case SYMBOL_REF:
1043 return true;
1044
1045 case ADDRESS:
1046 /* Arguments may or may not be bases; we don't know for sure. */
1047 return GET_MODE (x) != VOIDmode;
1048
1049 default:
1050 return false;
1051 }
1052}
1053
e7c893ba 1054/* Inside SRC, the source of a SET, find a base address. */
1055
ea0cb7ae 1056static rtx
aecda0d6 1057find_base_value (rtx src)
ea0cb7ae 1058{
970c0c06 1059 unsigned int regno;
d01410d3 1060
2e984a7a 1061#if defined (FIND_BASE_TERM)
1062 /* Try machine-dependent ways to find the base term. */
1063 src = FIND_BASE_TERM (src);
1064#endif
1065
ea0cb7ae 1066 switch (GET_CODE (src))
1067 {
1068 case SYMBOL_REF:
1069 case LABEL_REF:
1070 return src;
1071
1072 case REG:
9db10305 1073 regno = REGNO (src);
083a2b5e 1074 /* At the start of a function, argument registers have known base
e7c893ba 1075 values which may be lost later. Returning an ADDRESS
1076 expression here allows optimization based on argument values
1077 even when the argument registers are used for other purposes. */
970c0c06 1078 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1079 return new_reg_base_value[regno];
1a538f67 1080
ebd13eec 1081 /* If a pseudo has a known base value, return it. Do not do this
9464d436 1082 for non-fixed hard regs since it can result in a circular
1083 dependency chain for registers which have values at function entry.
ebd13eec 1084
1085 The test above is not sufficient because the scheduler may move
1086 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
9464d436 1087 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
980f3285 1088 && regno < VEC_length (rtx, reg_base_value))
e5be0cc6 1089 {
1090 /* If we're inside init_alias_analysis, use new_reg_base_value
1091 to reduce the number of relaxation iterations. */
a303ed8c 1092 if (new_reg_base_value && new_reg_base_value[regno]
3072d30e 1093 && DF_REG_DEF_COUNT (regno) == 1)
e5be0cc6 1094 return new_reg_base_value[regno];
1095
980f3285 1096 if (VEC_index (rtx, reg_base_value, regno))
1097 return VEC_index (rtx, reg_base_value, regno);
e5be0cc6 1098 }
1a538f67 1099
1c5cb56c 1100 return 0;
ea0cb7ae 1101
1102 case MEM:
1103 /* Check for an argument passed in memory. Only record in the
1104 copying-arguments block; it is too hard to track changes
1105 otherwise. */
1106 if (copying_arguments
1107 && (XEXP (src, 0) == arg_pointer_rtx
1108 || (GET_CODE (XEXP (src, 0)) == PLUS
1109 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
4e56ceb1 1110 return arg_base_value;
ea0cb7ae 1111 return 0;
1112
1113 case CONST:
1114 src = XEXP (src, 0);
1115 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1116 break;
083a2b5e 1117
2c0e001b 1118 /* ... fall through ... */
e7c893ba 1119
ea0cb7ae 1120 case PLUS:
1121 case MINUS:
e7c893ba 1122 {
06c5c535 1123 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1124
560de2f8 1125 /* If either operand is a REG that is a known pointer, then it
1126 is the base. */
1127 if (REG_P (src_0) && REG_POINTER (src_0))
1128 return find_base_value (src_0);
1129 if (REG_P (src_1) && REG_POINTER (src_1))
1130 return find_base_value (src_1);
1131
06c5c535 1132 /* If either operand is a REG, then see if we already have
1133 a known value for it. */
560de2f8 1134 if (REG_P (src_0))
06c5c535 1135 {
1136 temp = find_base_value (src_0);
083a2b5e 1137 if (temp != 0)
06c5c535 1138 src_0 = temp;
1139 }
1140
560de2f8 1141 if (REG_P (src_1))
06c5c535 1142 {
1143 temp = find_base_value (src_1);
083a2b5e 1144 if (temp!= 0)
06c5c535 1145 src_1 = temp;
1146 }
e7c893ba 1147
560de2f8 1148 /* If either base is named object or a special address
1149 (like an argument or stack reference), then use it for the
1150 base term. */
4e56ceb1 1151 if (src_0 != 0 && known_base_value_p (src_0))
560de2f8 1152 return src_0;
1153
4e56ceb1 1154 if (src_1 != 0 && known_base_value_p (src_1))
560de2f8 1155 return src_1;
1156
083a2b5e 1157 /* Guess which operand is the base address:
06c5c535 1158 If either operand is a symbol, then it is the base. If
1159 either operand is a CONST_INT, then the other is the base. */
971ba038 1160 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
e7c893ba 1161 return find_base_value (src_0);
971ba038 1162 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
06c5c535 1163 return find_base_value (src_1);
1164
ea0cb7ae 1165 return 0;
e7c893ba 1166 }
1167
1168 case LO_SUM:
1169 /* The standard form is (lo_sum reg sym) so look only at the
1170 second operand. */
1171 return find_base_value (XEXP (src, 1));
ea0cb7ae 1172
1173 case AND:
1174 /* If the second operand is constant set the base
2c0e001b 1175 address to the first operand. */
971ba038 1176 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
e7c893ba 1177 return find_base_value (XEXP (src, 0));
ea0cb7ae 1178 return 0;
1179
43d45282 1180 case TRUNCATE:
04ec15fa 1181 /* As we do not know which address space the pointer is referring to, we can
98155838 1182 handle this only if the target does not support different pointer or
1183 address modes depending on the address space. */
1184 if (!target_default_pointer_address_modes_p ())
1185 break;
43d45282 1186 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1187 break;
1188 /* Fall through. */
ea0cb7ae 1189 case HIGH:
2be64ef0 1190 case PRE_INC:
1191 case PRE_DEC:
1192 case POST_INC:
1193 case POST_DEC:
1194 case PRE_MODIFY:
1195 case POST_MODIFY:
e7c893ba 1196 return find_base_value (XEXP (src, 0));
99c14947 1197
d01410d3 1198 case ZERO_EXTEND:
1199 case SIGN_EXTEND: /* used for NT/Alpha pointers */
04ec15fa 1200 /* As we do not know which address space the pointer is referring to, we can
98155838 1201 handle this only if the target does not support different pointer or
1202 address modes depending on the address space. */
1203 if (!target_default_pointer_address_modes_p ())
1204 break;
1205
d01410d3 1206 {
1207 rtx temp = find_base_value (XEXP (src, 0));
1208
85d654dd 1209 if (temp != 0 && CONSTANT_P (temp))
d01410d3 1210 temp = convert_memory_address (Pmode, temp);
d01410d3 1211
1212 return temp;
1213 }
1214
99c14947 1215 default:
1216 break;
ea0cb7ae 1217 }
1218
1219 return 0;
1220}
1221
4e56ceb1 1222/* Called from init_alias_analysis indirectly through note_stores,
1223 or directly if DEST is a register with a REG_NOALIAS note attached.
1224 SET is null in the latter case. */
ea0cb7ae 1225
083a2b5e 1226/* While scanning insns to find base values, reg_seen[N] is nonzero if
ea0cb7ae 1227 register N has been set in this function. */
1228static char *reg_seen;
1229
e7c893ba 1230static void
81a410b1 1231record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
ea0cb7ae 1232{
19cb6b50 1233 unsigned regno;
ea0cb7ae 1234 rtx src;
3721f260 1235 int n;
ea0cb7ae 1236
8ad4c111 1237 if (!REG_P (dest))
ea0cb7ae 1238 return;
1239
9db10305 1240 regno = REGNO (dest);
ea0cb7ae 1241
0ea2d350 1242 gcc_checking_assert (regno < VEC_length (rtx, reg_base_value));
c2e94b25 1243
3721f260 1244 /* If this spans multiple hard registers, then we must indicate that every
1245 register has an unusable value. */
1246 if (regno < FIRST_PSEUDO_REGISTER)
67d6c12b 1247 n = hard_regno_nregs[regno][GET_MODE (dest)];
3721f260 1248 else
1249 n = 1;
1250 if (n != 1)
1251 {
1252 while (--n >= 0)
1253 {
1254 reg_seen[regno + n] = 1;
1255 new_reg_base_value[regno + n] = 0;
1256 }
1257 return;
1258 }
1259
ea0cb7ae 1260 if (set)
1261 {
1262 /* A CLOBBER wipes out any old value but does not prevent a previously
1263 unset register from acquiring a base address (i.e. reg_seen is not
1264 set). */
1265 if (GET_CODE (set) == CLOBBER)
1266 {
06c5c535 1267 new_reg_base_value[regno] = 0;
ea0cb7ae 1268 return;
1269 }
1270 src = SET_SRC (set);
1271 }
1272 else
1273 {
4e56ceb1 1274 /* There's a REG_NOALIAS note against DEST. */
ea0cb7ae 1275 if (reg_seen[regno])
1276 {
06c5c535 1277 new_reg_base_value[regno] = 0;
ea0cb7ae 1278 return;
1279 }
1280 reg_seen[regno] = 1;
4e56ceb1 1281 new_reg_base_value[regno] = unique_base_value (unique_id++);
ea0cb7ae 1282 return;
1283 }
1284
ad6b1fe1 1285 /* If this is not the first set of REGNO, see whether the new value
1286 is related to the old one. There are two cases of interest:
1287
1288 (1) The register might be assigned an entirely new value
1289 that has the same base term as the original set.
1290
1291 (2) The set might be a simple self-modification that
1292 cannot change REGNO's base value.
1293
1294 If neither case holds, reject the original base value as invalid.
1295 Note that the following situation is not detected:
1296
a0c938f0 1297 extern int x, y; int *p = &x; p += (&y-&x);
ad6b1fe1 1298
ea0cb7ae 1299 ANSI C does not allow computing the difference of addresses
1300 of distinct top level objects. */
ad6b1fe1 1301 if (new_reg_base_value[regno] != 0
1302 && find_base_value (src) != new_reg_base_value[regno])
ea0cb7ae 1303 switch (GET_CODE (src))
1304 {
e7c893ba 1305 case LO_SUM:
ea0cb7ae 1306 case MINUS:
1307 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
06c5c535 1308 new_reg_base_value[regno] = 0;
ea0cb7ae 1309 break;
43d45282 1310 case PLUS:
1311 /* If the value we add in the PLUS is also a valid base value,
1312 this might be the actual base value, and the original value
1313 an index. */
1314 {
1315 rtx other = NULL_RTX;
1316
1317 if (XEXP (src, 0) == dest)
1318 other = XEXP (src, 1);
1319 else if (XEXP (src, 1) == dest)
1320 other = XEXP (src, 0);
1321
1322 if (! other || find_base_value (other))
1323 new_reg_base_value[regno] = 0;
1324 break;
1325 }
ea0cb7ae 1326 case AND:
971ba038 1327 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
06c5c535 1328 new_reg_base_value[regno] = 0;
ea0cb7ae 1329 break;
ea0cb7ae 1330 default:
06c5c535 1331 new_reg_base_value[regno] = 0;
ea0cb7ae 1332 break;
1333 }
1334 /* If this is the first set of a register, record the value. */
1335 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
06c5c535 1336 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
1337 new_reg_base_value[regno] = find_base_value (src);
ea0cb7ae 1338
1339 reg_seen[regno] = 1;
1340}
1341
0cfef6c5 1342/* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1343 using hard registers with non-null REG_BASE_VALUE for renaming. */
1344rtx
1345get_reg_base_value (unsigned int regno)
1346{
1347 return VEC_index (rtx, reg_base_value, regno);
1348}
1349
12cb06ad 1350/* If a value is known for REGNO, return it. */
1351
a0c938f0 1352rtx
12cb06ad 1353get_reg_known_value (unsigned int regno)
1354{
1355 if (regno >= FIRST_PSEUDO_REGISTER)
1356 {
1357 regno -= FIRST_PSEUDO_REGISTER;
1358 if (regno < reg_known_value_size)
1359 return reg_known_value[regno];
1360 }
1361 return NULL;
9f5e1a5a 1362}
1363
12cb06ad 1364/* Set it. */
1365
1366static void
1367set_reg_known_value (unsigned int regno, rtx val)
1368{
1369 if (regno >= FIRST_PSEUDO_REGISTER)
1370 {
1371 regno -= FIRST_PSEUDO_REGISTER;
1372 if (regno < reg_known_value_size)
1373 reg_known_value[regno] = val;
1374 }
1375}
1376
1377/* Similarly for reg_known_equiv_p. */
1378
1379bool
1380get_reg_known_equiv_p (unsigned int regno)
1381{
1382 if (regno >= FIRST_PSEUDO_REGISTER)
1383 {
1384 regno -= FIRST_PSEUDO_REGISTER;
1385 if (regno < reg_known_value_size)
1386 return reg_known_equiv_p[regno];
1387 }
1388 return false;
1389}
1390
1391static void
1392set_reg_known_equiv_p (unsigned int regno, bool val)
1393{
1394 if (regno >= FIRST_PSEUDO_REGISTER)
1395 {
1396 regno -= FIRST_PSEUDO_REGISTER;
1397 if (regno < reg_known_value_size)
1398 reg_known_equiv_p[regno] = val;
1399 }
1400}
1401
1402
7cfb9bcf 1403/* Returns a canonical version of X, from the point of view alias
1404 analysis. (For example, if X is a MEM whose address is a register,
1405 and the register has a known value (say a SYMBOL_REF), then a MEM
1406 whose address is the SYMBOL_REF is returned.) */
1407
1408rtx
aecda0d6 1409canon_rtx (rtx x)
ea0cb7ae 1410{
1411 /* Recursively look for equivalences. */
8ad4c111 1412 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
12cb06ad 1413 {
1414 rtx t = get_reg_known_value (REGNO (x));
1415 if (t == x)
1416 return x;
1417 if (t)
1418 return canon_rtx (t);
1419 }
1420
1421 if (GET_CODE (x) == PLUS)
ea0cb7ae 1422 {
1423 rtx x0 = canon_rtx (XEXP (x, 0));
1424 rtx x1 = canon_rtx (XEXP (x, 1));
1425
1426 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1427 {
971ba038 1428 if (CONST_INT_P (x0))
b244d4c7 1429 return plus_constant (x1, INTVAL (x0));
971ba038 1430 else if (CONST_INT_P (x1))
b244d4c7 1431 return plus_constant (x0, INTVAL (x1));
941522d6 1432 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
ea0cb7ae 1433 }
1434 }
083a2b5e 1435
ea0cb7ae 1436 /* This gives us much better alias analysis when called from
1437 the loop optimizer. Note we want to leave the original
1438 MEM alone, but need to return the canonicalized MEM with
1439 all the flags with their original values. */
e16ceb8e 1440 else if (MEM_P (x))
e4e86ec5 1441 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
083a2b5e 1442
ea0cb7ae 1443 return x;
1444}
1445
1446/* Return 1 if X and Y are identical-looking rtx's.
6285298a 1447 Expect that X and Y has been already canonicalized.
ea0cb7ae 1448
1449 We use the data in reg_known_value above to see if two registers with
1450 different numbers are, in fact, equivalent. */
1451
1452static int
7ecb5bb2 1453rtx_equal_for_memref_p (const_rtx x, const_rtx y)
ea0cb7ae 1454{
19cb6b50 1455 int i;
1456 int j;
1457 enum rtx_code code;
1458 const char *fmt;
ea0cb7ae 1459
1460 if (x == 0 && y == 0)
1461 return 1;
1462 if (x == 0 || y == 0)
1463 return 0;
083a2b5e 1464
ea0cb7ae 1465 if (x == y)
1466 return 1;
1467
1468 code = GET_CODE (x);
1469 /* Rtx's of different codes cannot be equal. */
1470 if (code != GET_CODE (y))
1471 return 0;
1472
1473 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1474 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1475
1476 if (GET_MODE (x) != GET_MODE (y))
1477 return 0;
1478
7cfb9bcf 1479 /* Some RTL can be compared without a recursive examination. */
1480 switch (code)
1481 {
1482 case REG:
1483 return REGNO (x) == REGNO (y);
1484
1485 case LABEL_REF:
1486 return XEXP (x, 0) == XEXP (y, 0);
9342ee68 1487
7cfb9bcf 1488 case SYMBOL_REF:
1489 return XSTR (x, 0) == XSTR (y, 0);
1490
a2e238f7 1491 case VALUE:
7cfb9bcf 1492 case CONST_INT:
1493 case CONST_DOUBLE:
e397ad8e 1494 case CONST_FIXED:
7cfb9bcf 1495 /* There's no need to compare the contents of CONST_DOUBLEs or
1496 CONST_INTs because pointer equality is a good enough
1497 comparison for these nodes. */
1498 return 0;
1499
7cfb9bcf 1500 default:
1501 break;
1502 }
ea0cb7ae 1503
6285298a 1504 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1505 if (code == PLUS)
ea0cb7ae 1506 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1507 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1508 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1509 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
6285298a 1510 /* For commutative operations, the RTX match if the operand match in any
1511 order. Also handle the simple binary and unary cases without a loop. */
6720e96c 1512 if (COMMUTATIVE_P (x))
6285298a 1513 {
1514 rtx xop0 = canon_rtx (XEXP (x, 0));
1515 rtx yop0 = canon_rtx (XEXP (y, 0));
1516 rtx yop1 = canon_rtx (XEXP (y, 1));
1517
1518 return ((rtx_equal_for_memref_p (xop0, yop0)
1519 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1520 || (rtx_equal_for_memref_p (xop0, yop1)
1521 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1522 }
6720e96c 1523 else if (NON_COMMUTATIVE_P (x))
6285298a 1524 {
1525 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
aecda0d6 1526 canon_rtx (XEXP (y, 0)))
6285298a 1527 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1528 canon_rtx (XEXP (y, 1))));
1529 }
6720e96c 1530 else if (UNARY_P (x))
6285298a 1531 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
aecda0d6 1532 canon_rtx (XEXP (y, 0)));
ea0cb7ae 1533
1534 /* Compare the elements. If any pair of corresponding elements
1a6c3add 1535 fail to match, return 0 for the whole things.
1536
1537 Limit cases to types which actually appear in addresses. */
ea0cb7ae 1538
1539 fmt = GET_RTX_FORMAT (code);
1540 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1541 {
1542 switch (fmt[i])
1543 {
ea0cb7ae 1544 case 'i':
1545 if (XINT (x, i) != XINT (y, i))
1546 return 0;
1547 break;
1548
ea0cb7ae 1549 case 'E':
1550 /* Two vectors must have the same length. */
1551 if (XVECLEN (x, i) != XVECLEN (y, i))
1552 return 0;
1553
1554 /* And the corresponding elements must match. */
1555 for (j = 0; j < XVECLEN (x, i); j++)
6285298a 1556 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1557 canon_rtx (XVECEXP (y, i, j))) == 0)
ea0cb7ae 1558 return 0;
1559 break;
1560
1561 case 'e':
6285298a 1562 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1563 canon_rtx (XEXP (y, i))) == 0)
ea0cb7ae 1564 return 0;
1565 break;
1566
83ec925c 1567 /* This can happen for asm operands. */
1568 case 's':
1569 if (strcmp (XSTR (x, i), XSTR (y, i)))
1570 return 0;
1571 break;
1572
23c04f6b 1573 /* This can happen for an asm which clobbers memory. */
1574 case '0':
1575 break;
1576
ea0cb7ae 1577 /* It is believed that rtx's at this level will never
1578 contain anything but integers and other rtx's,
1579 except for within LABEL_REFs and SYMBOL_REFs. */
1580 default:
64db345d 1581 gcc_unreachable ();
ea0cb7ae 1582 }
1583 }
1584 return 1;
1585}
1586
5a37abfd 1587rtx
aecda0d6 1588find_base_term (rtx x)
ea0cb7ae 1589{
1617c276 1590 cselib_val *val;
8081d3a6 1591 struct elt_loc_list *l, *f;
1592 rtx ret;
1617c276 1593
15750b1e 1594#if defined (FIND_BASE_TERM)
1595 /* Try machine-dependent ways to find the base term. */
1596 x = FIND_BASE_TERM (x);
1597#endif
1598
ea0cb7ae 1599 switch (GET_CODE (x))
1600 {
1601 case REG:
1602 return REG_BASE_VALUE (x);
1603
2be64ef0 1604 case TRUNCATE:
04ec15fa 1605 /* As we do not know which address space the pointer is referring to, we can
98155838 1606 handle this only if the target does not support different pointer or
1607 address modes depending on the address space. */
1608 if (!target_default_pointer_address_modes_p ())
1609 return 0;
2be64ef0 1610 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
9342ee68 1611 return 0;
2be64ef0 1612 /* Fall through. */
ea0cb7ae 1613 case HIGH:
22dfec7d 1614 case PRE_INC:
1615 case PRE_DEC:
1616 case POST_INC:
1617 case POST_DEC:
2be64ef0 1618 case PRE_MODIFY:
1619 case POST_MODIFY:
22dfec7d 1620 return find_base_term (XEXP (x, 0));
1621
5104ef69 1622 case ZERO_EXTEND:
1623 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
04ec15fa 1624 /* As we do not know which address space the pointer is referring to, we can
98155838 1625 handle this only if the target does not support different pointer or
1626 address modes depending on the address space. */
1627 if (!target_default_pointer_address_modes_p ())
1628 return 0;
1629
5104ef69 1630 {
1631 rtx temp = find_base_term (XEXP (x, 0));
1632
85d654dd 1633 if (temp != 0 && CONSTANT_P (temp))
5104ef69 1634 temp = convert_memory_address (Pmode, temp);
5104ef69 1635
1636 return temp;
1637 }
1638
1617c276 1639 case VALUE:
1640 val = CSELIB_VAL_PTR (x);
8081d3a6 1641 ret = NULL_RTX;
1642
a2e238f7 1643 if (!val)
8081d3a6 1644 return ret;
1645
1646 f = val->locs;
1647 /* Temporarily reset val->locs to avoid infinite recursion. */
1648 val->locs = NULL;
1649
1650 for (l = f; l; l = l->next)
1651 if (GET_CODE (l->loc) == VALUE
1652 && CSELIB_VAL_PTR (l->loc)->locs
1653 && !CSELIB_VAL_PTR (l->loc)->locs->next
1654 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1655 continue;
1656 else if ((ret = find_base_term (l->loc)) != 0)
1657 break;
1658
1659 val->locs = f;
1660 return ret;
1617c276 1661
a7cf5bbf 1662 case LO_SUM:
1663 /* The standard form is (lo_sum reg sym) so look only at the
1664 second operand. */
1665 return find_base_term (XEXP (x, 1));
1666
ea0cb7ae 1667 case CONST:
1668 x = XEXP (x, 0);
1669 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1670 return 0;
b4b174c3 1671 /* Fall through. */
ea0cb7ae 1672 case PLUS:
1673 case MINUS:
1674 {
1b5bbc4c 1675 rtx tmp1 = XEXP (x, 0);
1676 rtx tmp2 = XEXP (x, 1);
1677
35a3065a 1678 /* This is a little bit tricky since we have to determine which of
1b5bbc4c 1679 the two operands represents the real base address. Otherwise this
1680 routine may return the index register instead of the base register.
1681
1682 That may cause us to believe no aliasing was possible, when in
1683 fact aliasing is possible.
1684
1685 We use a few simple tests to guess the base register. Additional
1686 tests can certainly be added. For example, if one of the operands
1687 is a shift or multiply, then it must be the index register and the
1688 other operand is the base register. */
9342ee68 1689
15750b1e 1690 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1691 return find_base_term (tmp2);
1692
1b5bbc4c 1693 /* If either operand is known to be a pointer, then use it
1694 to determine the base term. */
e61a0a7f 1695 if (REG_P (tmp1) && REG_POINTER (tmp1))
02b0d167 1696 {
1697 rtx base = find_base_term (tmp1);
1698 if (base)
1699 return base;
1700 }
1b5bbc4c 1701
e61a0a7f 1702 if (REG_P (tmp2) && REG_POINTER (tmp2))
02b0d167 1703 {
1704 rtx base = find_base_term (tmp2);
1705 if (base)
1706 return base;
1707 }
1b5bbc4c 1708
1709 /* Neither operand was known to be a pointer. Go ahead and find the
1710 base term for both operands. */
1711 tmp1 = find_base_term (tmp1);
1712 tmp2 = find_base_term (tmp2);
1713
1714 /* If either base term is named object or a special address
1715 (like an argument or stack reference), then use it for the
1716 base term. */
4e56ceb1 1717 if (tmp1 != 0 && known_base_value_p (tmp1))
1b5bbc4c 1718 return tmp1;
1719
4e56ceb1 1720 if (tmp2 != 0 && known_base_value_p (tmp2))
1b5bbc4c 1721 return tmp2;
1722
1723 /* We could not determine which of the two operands was the
1724 base register and which was the index. So we can determine
1725 nothing from the base alias check. */
1726 return 0;
ea0cb7ae 1727 }
1728
1729 case AND:
971ba038 1730 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
2be64ef0 1731 return find_base_term (XEXP (x, 0));
ea0cb7ae 1732 return 0;
1733
1734 case SYMBOL_REF:
1735 case LABEL_REF:
1736 return x;
1737
1738 default:
1739 return 0;
1740 }
1741}
1742
1743/* Return 0 if the addresses X and Y are known to point to different
1744 objects, 1 if they might be pointers to the same object. */
1745
1746static int
aecda0d6 1747base_alias_check (rtx x, rtx y, enum machine_mode x_mode,
1748 enum machine_mode y_mode)
ea0cb7ae 1749{
1750 rtx x_base = find_base_term (x);
1751 rtx y_base = find_base_term (y);
1752
3ad3a0ed 1753 /* If the address itself has no known base see if a known equivalent
1754 value has one. If either address still has no known base, nothing
1755 is known about aliasing. */
1756 if (x_base == 0)
1757 {
1758 rtx x_c;
083a2b5e 1759
3ad3a0ed 1760 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1761 return 1;
083a2b5e 1762
3ad3a0ed 1763 x_base = find_base_term (x_c);
1764 if (x_base == 0)
1765 return 1;
1766 }
ea0cb7ae 1767
3ad3a0ed 1768 if (y_base == 0)
1769 {
1770 rtx y_c;
1771 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1772 return 1;
083a2b5e 1773
3ad3a0ed 1774 y_base = find_base_term (y_c);
1775 if (y_base == 0)
1776 return 1;
1777 }
1778
1779 /* If the base addresses are equal nothing is known about aliasing. */
1780 if (rtx_equal_p (x_base, y_base))
ea0cb7ae 1781 return 1;
1782
f92492f7 1783 /* The base addresses are different expressions. If they are not accessed
1784 via AND, there is no conflict. We can bring knowledge of object
1785 alignment into play here. For example, on alpha, "char a, b;" can
1786 alias one another, though "char a; long b;" cannot. AND addesses may
1787 implicitly alias surrounding objects; i.e. unaligned access in DImode
1788 via AND address can alias all surrounding object types except those
1789 with aligment 8 or higher. */
1790 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1791 return 1;
1792 if (GET_CODE (x) == AND
971ba038 1793 && (!CONST_INT_P (XEXP (x, 1))
f92492f7 1794 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1795 return 1;
1796 if (GET_CODE (y) == AND
971ba038 1797 && (!CONST_INT_P (XEXP (y, 1))
f92492f7 1798 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1799 return 1;
1800
1801 /* Differing symbols not accessed via AND never alias. */
ea0cb7ae 1802 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
f92492f7 1803 return 0;
ea0cb7ae 1804
4e56ceb1 1805 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
ea0cb7ae 1806 return 0;
1807
e190b18a 1808 return 1;
ea0cb7ae 1809}
1810
fa5e407e 1811/* Callback for for_each_rtx, that returns 1 upon encountering a VALUE
1812 whose UID is greater than the int uid that D points to. */
1813
1814static int
1815refs_newer_value_cb (rtx *x, void *d)
1816{
1817 if (GET_CODE (*x) == VALUE && CSELIB_VAL_PTR (*x)->uid > *(int *)d)
1818 return 1;
1819
1820 return 0;
1821}
1822
1823/* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1824 that of V. */
1825
1826static bool
1827refs_newer_value_p (rtx expr, rtx v)
1828{
1829 int minuid = CSELIB_VAL_PTR (v)->uid;
1830
1831 return for_each_rtx (&expr, refs_newer_value_cb, &minuid);
1832}
1833
1617c276 1834/* Convert the address X into something we can use. This is done by returning
1835 it unchanged unless it is a value; in the latter case we call cselib to get
1836 a more useful rtx. */
f7c44134 1837
8e802be9 1838rtx
aecda0d6 1839get_addr (rtx x)
1617c276 1840{
1841 cselib_val *v;
1842 struct elt_loc_list *l;
1843
1844 if (GET_CODE (x) != VALUE)
1845 return x;
1846 v = CSELIB_VAL_PTR (x);
a2e238f7 1847 if (v)
1848 {
85b6e75b 1849 bool have_equivs = cselib_have_permanent_equivalences ();
1850 if (have_equivs)
1851 v = canonical_cselib_val (v);
a2e238f7 1852 for (l = v->locs; l; l = l->next)
1853 if (CONSTANT_P (l->loc))
1854 return l->loc;
1855 for (l = v->locs; l; l = l->next)
85b6e75b 1856 if (!REG_P (l->loc) && !MEM_P (l->loc)
1857 /* Avoid infinite recursion when potentially dealing with
1858 var-tracking artificial equivalences, by skipping the
1859 equivalences themselves, and not choosing expressions
1860 that refer to newer VALUEs. */
1861 && (!have_equivs
1862 || (GET_CODE (l->loc) != VALUE
1863 && !refs_newer_value_p (l->loc, x))))
fa5e407e 1864 return l->loc;
85b6e75b 1865 if (have_equivs)
1866 {
1867 for (l = v->locs; l; l = l->next)
1868 if (REG_P (l->loc)
1869 || (GET_CODE (l->loc) != VALUE
1870 && !refs_newer_value_p (l->loc, x)))
1871 return l->loc;
1872 /* Return the canonical value. */
1873 return v->val_rtx;
1874 }
1875 if (v->locs)
1876 return v->locs->loc;
a2e238f7 1877 }
1617c276 1878 return x;
1879}
1880
b9f92976 1881/* Return the address of the (N_REFS + 1)th memory reference to ADDR
1882 where SIZE is the size in bytes of the memory reference. If ADDR
1883 is not modified by the memory reference then ADDR is returned. */
1884
0aa9ffd4 1885static rtx
aecda0d6 1886addr_side_effect_eval (rtx addr, int size, int n_refs)
b9f92976 1887{
1888 int offset = 0;
9342ee68 1889
b9f92976 1890 switch (GET_CODE (addr))
1891 {
1892 case PRE_INC:
1893 offset = (n_refs + 1) * size;
1894 break;
1895 case PRE_DEC:
1896 offset = -(n_refs + 1) * size;
1897 break;
1898 case POST_INC:
1899 offset = n_refs * size;
1900 break;
1901 case POST_DEC:
1902 offset = -n_refs * size;
1903 break;
1904
1905 default:
1906 return addr;
1907 }
9342ee68 1908
b9f92976 1909 if (offset)
6285298a 1910 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
a0c938f0 1911 GEN_INT (offset));
b9f92976 1912 else
1913 addr = XEXP (addr, 0);
6285298a 1914 addr = canon_rtx (addr);
b9f92976 1915
1916 return addr;
1917}
1918
40f9e245 1919/* Return one if X and Y (memory addresses) reference the
1920 same location in memory or if the references overlap.
1921 Return zero if they do not overlap, else return
1922 minus one in which case they still might reference the same location.
1923
1924 C is an offset accumulator. When
ea0cb7ae 1925 C is nonzero, we are testing aliases between X and Y + C.
1926 XSIZE is the size in bytes of the X reference,
1927 similarly YSIZE is the size in bytes for Y.
6285298a 1928 Expect that canon_rtx has been already called for X and Y.
ea0cb7ae 1929
1930 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1931 referenced (the reference was BLKmode), so make the most pessimistic
1932 assumptions.
1933
1e38bf49 1934 If XSIZE or YSIZE is negative, we may access memory outside the object
1935 being referenced as a side effect. This can happen when using AND to
1936 align memory references, as is done on the Alpha.
1937
ea0cb7ae 1938 Nice to notice that varying addresses cannot conflict with fp if no
40f9e245 1939 local variables had their addresses taken, but that's too hard now.
1940
1941 ??? Contrary to the tree alias oracle this does not return
1942 one for X + non-constant and Y + non-constant when X and Y are equal.
1943 If that is fixed the TBAA hack for union type-punning can be removed. */
ea0cb7ae 1944
ea0cb7ae 1945static int
aecda0d6 1946memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
ea0cb7ae 1947{
1617c276 1948 if (GET_CODE (x) == VALUE)
6f9688ce 1949 {
1950 if (REG_P (y))
1951 {
4e15f806 1952 struct elt_loc_list *l = NULL;
1953 if (CSELIB_VAL_PTR (x))
fa5e407e 1954 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
1955 l; l = l->next)
4e15f806 1956 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
1957 break;
6f9688ce 1958 if (l)
1959 x = y;
1960 else
1961 x = get_addr (x);
1962 }
1963 /* Don't call get_addr if y is the same VALUE. */
1964 else if (x != y)
1965 x = get_addr (x);
1966 }
1617c276 1967 if (GET_CODE (y) == VALUE)
6f9688ce 1968 {
1969 if (REG_P (x))
1970 {
4e15f806 1971 struct elt_loc_list *l = NULL;
1972 if (CSELIB_VAL_PTR (y))
fa5e407e 1973 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
1974 l; l = l->next)
4e15f806 1975 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
1976 break;
6f9688ce 1977 if (l)
1978 y = x;
1979 else
1980 y = get_addr (y);
1981 }
1982 /* Don't call get_addr if x is the same VALUE. */
1983 else if (y != x)
1984 y = get_addr (y);
1985 }
ea0cb7ae 1986 if (GET_CODE (x) == HIGH)
1987 x = XEXP (x, 0);
1988 else if (GET_CODE (x) == LO_SUM)
1989 x = XEXP (x, 1);
1990 else
6285298a 1991 x = addr_side_effect_eval (x, xsize, 0);
ea0cb7ae 1992 if (GET_CODE (y) == HIGH)
1993 y = XEXP (y, 0);
1994 else if (GET_CODE (y) == LO_SUM)
1995 y = XEXP (y, 1);
1996 else
6285298a 1997 y = addr_side_effect_eval (y, ysize, 0);
ea0cb7ae 1998
1999 if (rtx_equal_for_memref_p (x, y))
2000 {
1e38bf49 2001 if (xsize <= 0 || ysize <= 0)
ea0cb7ae 2002 return 1;
2003 if (c >= 0 && xsize > c)
2004 return 1;
2005 if (c < 0 && ysize+c > 0)
2006 return 1;
2007 return 0;
2008 }
2009
35d3eedd 2010 /* This code used to check for conflicts involving stack references and
2011 globals but the base address alias code now handles these cases. */
ea0cb7ae 2012
2013 if (GET_CODE (x) == PLUS)
2014 {
2015 /* The fact that X is canonicalized means that this
2016 PLUS rtx is canonicalized. */
2017 rtx x0 = XEXP (x, 0);
2018 rtx x1 = XEXP (x, 1);
2019
2020 if (GET_CODE (y) == PLUS)
2021 {
2022 /* The fact that Y is canonicalized means that this
2023 PLUS rtx is canonicalized. */
2024 rtx y0 = XEXP (y, 0);
2025 rtx y1 = XEXP (y, 1);
2026
2027 if (rtx_equal_for_memref_p (x1, y1))
2028 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2029 if (rtx_equal_for_memref_p (x0, y0))
2030 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
971ba038 2031 if (CONST_INT_P (x1))
5f59c49b 2032 {
971ba038 2033 if (CONST_INT_P (y1))
5f59c49b 2034 return memrefs_conflict_p (xsize, x0, ysize, y0,
2035 c - INTVAL (x1) + INTVAL (y1));
2036 else
2037 return memrefs_conflict_p (xsize, x0, ysize, y,
2038 c - INTVAL (x1));
2039 }
971ba038 2040 else if (CONST_INT_P (y1))
ea0cb7ae 2041 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2042
40f9e245 2043 return -1;
ea0cb7ae 2044 }
971ba038 2045 else if (CONST_INT_P (x1))
ea0cb7ae 2046 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2047 }
2048 else if (GET_CODE (y) == PLUS)
2049 {
2050 /* The fact that Y is canonicalized means that this
2051 PLUS rtx is canonicalized. */
2052 rtx y0 = XEXP (y, 0);
2053 rtx y1 = XEXP (y, 1);
2054
971ba038 2055 if (CONST_INT_P (y1))
ea0cb7ae 2056 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2057 else
40f9e245 2058 return -1;
ea0cb7ae 2059 }
2060
2061 if (GET_CODE (x) == GET_CODE (y))
2062 switch (GET_CODE (x))
2063 {
2064 case MULT:
2065 {
2066 /* Handle cases where we expect the second operands to be the
2067 same, and check only whether the first operand would conflict
2068 or not. */
2069 rtx x0, y0;
2070 rtx x1 = canon_rtx (XEXP (x, 1));
2071 rtx y1 = canon_rtx (XEXP (y, 1));
2072 if (! rtx_equal_for_memref_p (x1, y1))
40f9e245 2073 return -1;
ea0cb7ae 2074 x0 = canon_rtx (XEXP (x, 0));
2075 y0 = canon_rtx (XEXP (y, 0));
2076 if (rtx_equal_for_memref_p (x0, y0))
2077 return (xsize == 0 || ysize == 0
2078 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
2079
2080 /* Can't properly adjust our sizes. */
971ba038 2081 if (!CONST_INT_P (x1))
40f9e245 2082 return -1;
ea0cb7ae 2083 xsize /= INTVAL (x1);
2084 ysize /= INTVAL (x1);
2085 c /= INTVAL (x1);
2086 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2087 }
99c14947 2088
2089 default:
2090 break;
ea0cb7ae 2091 }
2092
2093 /* Treat an access through an AND (e.g. a subword access on an Alpha)
9342ee68 2094 as an access with indeterminate size. Assume that references
9fb1c31b 2095 besides AND are aligned, so if the size of the other reference is
2096 at least as large as the alignment, assume no other overlap. */
971ba038 2097 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
9fb1c31b 2098 {
bcdb334e 2099 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
9fb1c31b 2100 xsize = -1;
6285298a 2101 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)), ysize, y, c);
9fb1c31b 2102 }
971ba038 2103 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
1e38bf49 2104 {
9fb1c31b 2105 /* ??? If we are indexing far enough into the array/structure, we
9342ee68 2106 may yet be able to determine that we can not overlap. But we
1e38bf49 2107 also need to that we are far enough from the end not to overlap
9fb1c31b 2108 a following reference, so we do nothing with that for now. */
bcdb334e 2109 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
9fb1c31b 2110 ysize = -1;
6285298a 2111 return memrefs_conflict_p (xsize, x, ysize, canon_rtx (XEXP (y, 0)), c);
1e38bf49 2112 }
ea0cb7ae 2113
2114 if (CONSTANT_P (x))
2115 {
971ba038 2116 if (CONST_INT_P (x) && CONST_INT_P (y))
ea0cb7ae 2117 {
2118 c += (INTVAL (y) - INTVAL (x));
1e38bf49 2119 return (xsize <= 0 || ysize <= 0
ea0cb7ae 2120 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
2121 }
2122
2123 if (GET_CODE (x) == CONST)
2124 {
2125 if (GET_CODE (y) == CONST)
2126 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2127 ysize, canon_rtx (XEXP (y, 0)), c);
2128 else
2129 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2130 ysize, y, c);
2131 }
2132 if (GET_CODE (y) == CONST)
2133 return memrefs_conflict_p (xsize, x, ysize,
2134 canon_rtx (XEXP (y, 0)), c);
2135
2136 if (CONSTANT_P (y))
15750b1e 2137 return (xsize <= 0 || ysize <= 0
1e38bf49 2138 || (rtx_equal_for_memref_p (x, y)
15750b1e 2139 && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
ea0cb7ae 2140
40f9e245 2141 return -1;
ea0cb7ae 2142 }
40f9e245 2143
2144 return -1;
ea0cb7ae 2145}
2146
2147/* Functions to compute memory dependencies.
2148
2149 Since we process the insns in execution order, we can build tables
2150 to keep track of what registers are fixed (and not aliased), what registers
2151 are varying in known ways, and what registers are varying in unknown
2152 ways.
2153
2154 If both memory references are volatile, then there must always be a
2155 dependence between the two references, since their order can not be
2156 changed. A volatile and non-volatile reference can be interchanged
9342ee68 2157 though.
ea0cb7ae 2158
376a287d 2159 We also must allow AND addresses, because they may generate accesses
2160 outside the object being referenced. This is used to generate aligned
2161 addresses from unaligned addresses, for instance, the alpha
71d9e995 2162 storeqi_unaligned pattern. */
ea0cb7ae 2163
2164/* Read dependence: X is read after read in MEM takes place. There can
2165 only be a dependence here if both reads are volatile. */
2166
2167int
52d07779 2168read_dependence (const_rtx mem, const_rtx x)
ea0cb7ae 2169{
2170 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
2171}
2172
6a0934dd 2173/* Returns nonzero if something about the mode or address format MEM1
2174 indicates that it might well alias *anything*. */
2175
fe69e4fe 2176static int
52d07779 2177aliases_everything_p (const_rtx mem)
6a0934dd 2178{
6a0934dd 2179 if (GET_CODE (XEXP (mem, 0)) == AND)
0975351b 2180 /* If the address is an AND, it's very hard to know at what it is
6a0934dd 2181 actually pointing. */
2182 return 1;
9342ee68 2183
6a0934dd 2184 return 0;
2185}
2186
b10dbbca 2187/* Return true if we can determine that the fields referenced cannot
2188 overlap for any pair of objects. */
2189
2190static bool
52d07779 2191nonoverlapping_component_refs_p (const_tree x, const_tree y)
b10dbbca 2192{
52d07779 2193 const_tree fieldx, fieldy, typex, typey, orig_y;
b10dbbca 2194
b95fdaae 2195 if (!flag_strict_aliasing)
2196 return false;
2197
b10dbbca 2198 do
2199 {
2200 /* The comparison has to be done at a common type, since we don't
cb0ccc1e 2201 know how the inheritance hierarchy works. */
b10dbbca 2202 orig_y = y;
2203 do
2204 {
2205 fieldx = TREE_OPERAND (x, 1);
a769a0a7 2206 typex = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx));
b10dbbca 2207
2208 y = orig_y;
2209 do
2210 {
2211 fieldy = TREE_OPERAND (y, 1);
a769a0a7 2212 typey = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy));
b10dbbca 2213
2214 if (typex == typey)
2215 goto found;
2216
2217 y = TREE_OPERAND (y, 0);
2218 }
2219 while (y && TREE_CODE (y) == COMPONENT_REF);
2220
2221 x = TREE_OPERAND (x, 0);
2222 }
2223 while (x && TREE_CODE (x) == COMPONENT_REF);
b10dbbca 2224 /* Never found a common type. */
a769a0a7 2225 return false;
b10dbbca 2226
2227 found:
2228 /* If we're left with accessing different fields of a structure,
2229 then no overlap. */
2230 if (TREE_CODE (typex) == RECORD_TYPE
2231 && fieldx != fieldy)
2232 return true;
2233
2234 /* The comparison on the current field failed. If we're accessing
2235 a very nested structure, look at the next outer level. */
2236 x = TREE_OPERAND (x, 0);
2237 y = TREE_OPERAND (y, 0);
2238 }
2239 while (x && y
2240 && TREE_CODE (x) == COMPONENT_REF
2241 && TREE_CODE (y) == COMPONENT_REF);
9342ee68 2242
b10dbbca 2243 return false;
2244}
2245
2246/* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2247
2248static tree
aecda0d6 2249decl_for_component_ref (tree x)
b10dbbca 2250{
2251 do
2252 {
2253 x = TREE_OPERAND (x, 0);
2254 }
2255 while (x && TREE_CODE (x) == COMPONENT_REF);
2256
2257 return x && DECL_P (x) ? x : NULL_TREE;
2258}
2259
da443c27 2260/* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2261 for the offset of the field reference. *KNOWN_P says whether the
2262 offset is known. */
b10dbbca 2263
da443c27 2264static void
2265adjust_offset_for_component_ref (tree x, bool *known_p,
2266 HOST_WIDE_INT *offset)
b10dbbca 2267{
da443c27 2268 if (!*known_p)
2269 return;
9342ee68 2270 do
b10dbbca 2271 {
da443c27 2272 tree xoffset = component_ref_field_offset (x);
b10dbbca 2273 tree field = TREE_OPERAND (x, 1);
2274
da443c27 2275 if (! host_integerp (xoffset, 1))
2276 {
2277 *known_p = false;
2278 return;
2279 }
2280 *offset += (tree_low_cst (xoffset, 1)
b10dbbca 2281 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
2282 / BITS_PER_UNIT));
2283
2284 x = TREE_OPERAND (x, 0);
2285 }
2286 while (x && TREE_CODE (x) == COMPONENT_REF);
b10dbbca 2287}
2288
d716ce75 2289/* Return nonzero if we can determine the exprs corresponding to memrefs
a84256aa 2290 X and Y and they do not overlap.
2291 If LOOP_VARIANT is set, skip offset-based disambiguation */
ddb3d16f 2292
5fe61d21 2293int
a84256aa 2294nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
ddb3d16f 2295{
b10dbbca 2296 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
ddb3d16f 2297 rtx rtlx, rtly;
2298 rtx basex, basey;
da443c27 2299 bool moffsetx_known_p, moffsety_known_p;
2300 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
ddb3d16f 2301 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2302
b10dbbca 2303 /* Unless both have exprs, we can't tell anything. */
2304 if (exprx == 0 || expry == 0)
2305 return 0;
c6b80e5d 2306
2307 /* For spill-slot accesses make sure we have valid offsets. */
2308 if ((exprx == get_spill_slot_decl (false)
da443c27 2309 && ! MEM_OFFSET_KNOWN_P (x))
c6b80e5d 2310 || (expry == get_spill_slot_decl (false)
da443c27 2311 && ! MEM_OFFSET_KNOWN_P (y)))
c6b80e5d 2312 return 0;
a0c938f0 2313
b10dbbca 2314 /* If both are field references, we may be able to determine something. */
2315 if (TREE_CODE (exprx) == COMPONENT_REF
2316 && TREE_CODE (expry) == COMPONENT_REF
2317 && nonoverlapping_component_refs_p (exprx, expry))
2318 return 1;
2319
a0c938f0 2320
b10dbbca 2321 /* If the field reference test failed, look at the DECLs involved. */
da443c27 2322 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2323 if (moffsetx_known_p)
2324 moffsetx = MEM_OFFSET (x);
b10dbbca 2325 if (TREE_CODE (exprx) == COMPONENT_REF)
2326 {
b50c6e49 2327 tree t = decl_for_component_ref (exprx);
2328 if (! t)
2329 return 0;
da443c27 2330 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
b50c6e49 2331 exprx = t;
b10dbbca 2332 }
2d8fe5d0 2333
da443c27 2334 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2335 if (moffsety_known_p)
2336 moffsety = MEM_OFFSET (y);
b10dbbca 2337 if (TREE_CODE (expry) == COMPONENT_REF)
2338 {
b50c6e49 2339 tree t = decl_for_component_ref (expry);
2340 if (! t)
2341 return 0;
da443c27 2342 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
b50c6e49 2343 expry = t;
b10dbbca 2344 }
2345
2346 if (! DECL_P (exprx) || ! DECL_P (expry))
ddb3d16f 2347 return 0;
2348
119147d1 2349 /* With invalid code we can end up storing into the constant pool.
2350 Bail out to avoid ICEing when creating RTL for this.
2351 See gfortran.dg/lto/20091028-2_0.f90. */
2352 if (TREE_CODE (exprx) == CONST_DECL
2353 || TREE_CODE (expry) == CONST_DECL)
2354 return 1;
2355
b10dbbca 2356 rtlx = DECL_RTL (exprx);
2357 rtly = DECL_RTL (expry);
ddb3d16f 2358
10e949c1 2359 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2360 can't overlap unless they are the same because we never reuse that part
2361 of the stack frame used for locals for spilled pseudos. */
e16ceb8e 2362 if ((!MEM_P (rtlx) || !MEM_P (rtly))
10e949c1 2363 && ! rtx_equal_p (rtlx, rtly))
ddb3d16f 2364 return 1;
2365
04ec15fa 2366 /* If we have MEMs referring to different address spaces (which can
bd1a81f7 2367 potentially overlap), we cannot easily tell from the addresses
2368 whether the references overlap. */
2369 if (MEM_P (rtlx) && MEM_P (rtly)
2370 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2371 return 0;
2372
ddb3d16f 2373 /* Get the base and offsets of both decls. If either is a register, we
2374 know both are and are the same, so use that as the base. The only
2375 we can avoid overlap is if we can deduce that they are nonoverlapping
2376 pieces of that decl, which is very rare. */
e16ceb8e 2377 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
971ba038 2378 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
ddb3d16f 2379 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2380
e16ceb8e 2381 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
971ba038 2382 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
ddb3d16f 2383 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2384
c9c48480 2385 /* If the bases are different, we know they do not overlap if both
9342ee68 2386 are constants or if one is a constant and the other a pointer into the
c9c48480 2387 stack frame. Otherwise a different base means we can't tell if they
2388 overlap or not. */
2389 if (! rtx_equal_p (basex, basey))
9342ee68 2390 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2391 || (CONSTANT_P (basex) && REG_P (basey)
2392 && REGNO_PTR_FRAME_P (REGNO (basey)))
2393 || (CONSTANT_P (basey) && REG_P (basex)
2394 && REGNO_PTR_FRAME_P (REGNO (basex))));
ddb3d16f 2395
a84256aa 2396 /* Offset based disambiguation not appropriate for loop invariant */
2397 if (loop_invariant)
2398 return 0;
2399
e16ceb8e 2400 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
5b2a69fa 2401 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
ddb3d16f 2402 : -1);
e16ceb8e 2403 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
5b2a69fa 2404 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2405 : -1);
ddb3d16f 2406
dc2dfdb6 2407 /* If we have an offset for either memref, it can update the values computed
2408 above. */
da443c27 2409 if (moffsetx_known_p)
2410 offsetx += moffsetx, sizex -= moffsetx;
2411 if (moffsety_known_p)
2412 offsety += moffsety, sizey -= moffsety;
ddb3d16f 2413
dc2dfdb6 2414 /* If a memref has both a size and an offset, we can use the smaller size.
203c9fee 2415 We can't do this if the offset isn't known because we must view this
dc2dfdb6 2416 memref as being anywhere inside the DECL's MEM. */
da443c27 2417 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
5b2a69fa 2418 sizex = MEM_SIZE (x);
da443c27 2419 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
5b2a69fa 2420 sizey = MEM_SIZE (y);
ddb3d16f 2421
2422 /* Put the values of the memref with the lower offset in X's values. */
2423 if (offsetx > offsety)
2424 {
2425 tem = offsetx, offsetx = offsety, offsety = tem;
2426 tem = sizex, sizex = sizey, sizey = tem;
2427 }
2428
2429 /* If we don't know the size of the lower-offset value, we can't tell
2430 if they conflict. Otherwise, we do the test. */
906923ba 2431 return sizex >= 0 && offsety >= offsetx + sizex;
ddb3d16f 2432}
2433
02f77cc4 2434/* Helper for true_dependence and canon_true_dependence.
2435 Checks for true dependence: X is read after store in MEM takes place.
ea0cb7ae 2436
02f77cc4 2437 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2438 NULL_RTX, and the canonical addresses of MEM and X are both computed
2439 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2440
2441 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2442
2443 Returns 1 if there is a true dependence, 0 otherwise. */
2444
2445static int
2446true_dependence_1 (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
376a287d 2447 const_rtx x, rtx x_addr, bool mem_canonicalized)
ea0cb7ae 2448{
9ee7cac5 2449 rtx base;
40f9e245 2450 int ret;
ea0cb7ae 2451
02f77cc4 2452 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2453 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2454
ea0cb7ae 2455 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2456 return 1;
2457
a57f4c4d 2458 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
67837170 2459 This is used in epilogue deallocation functions, and in cselib. */
a57f4c4d 2460 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2461 return 1;
2462 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2463 return 1;
c94cfd1c 2464 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2465 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2466 return 1;
a57f4c4d 2467
b04fab2a 2468 /* Read-only memory is by definition never modified, and therefore can't
2469 conflict with anything. We don't expect to find read-only set on MEM,
a53ff4c1 2470 but stupid user tricks can produce them, so don't die. */
b04fab2a 2471 if (MEM_READONLY_P (x))
ea0cb7ae 2472 return 0;
2473
04ec15fa 2474 /* If we have MEMs referring to different address spaces (which can
bd1a81f7 2475 potentially overlap), we cannot easily tell from the addresses
2476 whether the references overlap. */
2477 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2478 return 1;
2479
02f77cc4 2480 if (! mem_addr)
2481 {
2482 mem_addr = XEXP (mem, 0);
2483 if (mem_mode == VOIDmode)
2484 mem_mode = GET_MODE (mem);
2485 }
2486
2487 if (! x_addr)
288debed 2488 {
62696140 2489 x_addr = XEXP (x, 0);
2490 if (!((GET_CODE (x_addr) == VALUE
2491 && GET_CODE (mem_addr) != VALUE
2492 && reg_mentioned_p (x_addr, mem_addr))
2493 || (GET_CODE (x_addr) != VALUE
2494 && GET_CODE (mem_addr) == VALUE
2495 && reg_mentioned_p (mem_addr, x_addr))))
2496 {
2497 x_addr = get_addr (x_addr);
2498 if (! mem_canonicalized)
2499 mem_addr = get_addr (mem_addr);
2500 }
288debed 2501 }
1617c276 2502
002fe3cb 2503 base = find_base_term (x_addr);
2504 if (base && (GET_CODE (base) == LABEL_REF
2505 || (GET_CODE (base) == SYMBOL_REF
2506 && CONSTANT_POOL_ADDRESS_P (base))))
2507 return 0;
2508
1617c276 2509 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
3ad3a0ed 2510 return 0;
2511
1617c276 2512 x_addr = canon_rtx (x_addr);
02f77cc4 2513 if (!mem_canonicalized)
2514 mem_addr = canon_rtx (mem_addr);
35d3eedd 2515
40f9e245 2516 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2517 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2518 return ret;
2519
2520 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2521 return 0;
2522
a84256aa 2523 if (nonoverlapping_memrefs_p (mem, x, false))
4958fe6e 2524 return 0;
2525
6a0934dd 2526 if (aliases_everything_p (x))
4958fe6e 2527 return 1;
2528
35a3065a 2529 /* We cannot use aliases_everything_p to test MEM, since we must look
02f77cc4 2530 at MEM_ADDR, rather than XEXP (mem, 0). */
3bddda63 2531 if (GET_CODE (mem_addr) == AND)
8e802be9 2532 return 1;
2533
02f77cc4 2534 /* ??? In true_dependence we also allow BLKmode to alias anything. Why
8e802be9 2535 don't we do this in anti_dependence and output_dependence? */
2536 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2537 return 1;
2538
3a443843 2539 return rtx_refs_may_alias_p (x, mem, true);
8e802be9 2540}
2541
02f77cc4 2542/* True dependence: X is read after store in MEM takes place. */
2543
2544int
376a287d 2545true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x)
02f77cc4 2546{
2547 return true_dependence_1 (mem, mem_mode, NULL_RTX,
376a287d 2548 x, NULL_RTX, /*mem_canonicalized=*/false);
02f77cc4 2549}
2550
8e802be9 2551/* Canonical true dependence: X is read after store in MEM takes place.
9342ee68 2552 Variant of true_dependence which assumes MEM has already been
2553 canonicalized (hence we no longer do that here).
02f77cc4 2554 The mem_addr argument has been added, since true_dependence_1 computed
2555 this value prior to canonicalizing. */
8e802be9 2556
2557int
52d07779 2558canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
376a287d 2559 const_rtx x, rtx x_addr)
8e802be9 2560{
02f77cc4 2561 return true_dependence_1 (mem, mem_mode, mem_addr,
376a287d 2562 x, x_addr, /*mem_canonicalized=*/true);
ea0cb7ae 2563}
2564
d10cfa8d 2565/* Returns nonzero if a write to X might alias a previous read from
b04fab2a 2566 (or, if WRITEP is nonzero, a write to) MEM. */
ea0cb7ae 2567
fe69e4fe 2568static int
52d07779 2569write_dependence_p (const_rtx mem, const_rtx x, int writep)
ea0cb7ae 2570{
35d3eedd 2571 rtx x_addr, mem_addr;
9ee7cac5 2572 rtx base;
40f9e245 2573 int ret;
35d3eedd 2574
ea0cb7ae 2575 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2576 return 1;
2577
a57f4c4d 2578 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2579 This is used in epilogue deallocation functions. */
2580 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2581 return 1;
2582 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2583 return 1;
c94cfd1c 2584 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2585 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2586 return 1;
a57f4c4d 2587
b04fab2a 2588 /* A read from read-only memory can't conflict with read-write memory. */
2589 if (!writep && MEM_READONLY_P (mem))
2590 return 0;
002fe3cb 2591
04ec15fa 2592 /* If we have MEMs referring to different address spaces (which can
bd1a81f7 2593 potentially overlap), we cannot easily tell from the addresses
2594 whether the references overlap. */
2595 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2596 return 1;
2597
288debed 2598 x_addr = XEXP (x, 0);
2599 mem_addr = XEXP (mem, 0);
2600 if (!((GET_CODE (x_addr) == VALUE
2601 && GET_CODE (mem_addr) != VALUE
2602 && reg_mentioned_p (x_addr, mem_addr))
2603 || (GET_CODE (x_addr) != VALUE
2604 && GET_CODE (mem_addr) == VALUE
2605 && reg_mentioned_p (mem_addr, x_addr))))
2606 {
2607 x_addr = get_addr (x_addr);
2608 mem_addr = get_addr (mem_addr);
2609 }
002fe3cb 2610
9ee7cac5 2611 if (! writep)
2612 {
002fe3cb 2613 base = find_base_term (mem_addr);
9ee7cac5 2614 if (base && (GET_CODE (base) == LABEL_REF
2615 || (GET_CODE (base) == SYMBOL_REF
2616 && CONSTANT_POOL_ADDRESS_P (base))))
2617 return 0;
2618 }
2619
1617c276 2620 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2621 GET_MODE (mem)))
b5ba9f3a 2622 return 0;
2623
1617c276 2624 x_addr = canon_rtx (x_addr);
2625 mem_addr = canon_rtx (mem_addr);
35d3eedd 2626
40f9e245 2627 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2628 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2629 return ret;
2630
a84256aa 2631 if (nonoverlapping_memrefs_p (x, mem, false))
6a0934dd 2632 return 0;
2633
3a443843 2634 return rtx_refs_may_alias_p (x, mem, false);
6a0934dd 2635}
2636
2637/* Anti dependence: X is written after read in MEM takes place. */
2638
2639int
52d07779 2640anti_dependence (const_rtx mem, const_rtx x)
6a0934dd 2641{
b04fab2a 2642 return write_dependence_p (mem, x, /*writep=*/0);
ea0cb7ae 2643}
2644
2645/* Output dependence: X is written after store in MEM takes place. */
2646
2647int
52d07779 2648output_dependence (const_rtx mem, const_rtx x)
ea0cb7ae 2649{
b04fab2a 2650 return write_dependence_p (mem, x, /*writep=*/1);
ea0cb7ae 2651}
c352cbc7 2652\f
35d3eedd 2653
a84256aa 2654
2655/* Check whether X may be aliased with MEM. Don't do offset-based
2656 memory disambiguation & TBAA. */
2657int
2658may_alias_p (const_rtx mem, const_rtx x)
2659{
2660 rtx x_addr, mem_addr;
a84256aa 2661
2662 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2663 return 1;
2664
2665 /* ??? In true_dependence we also allow BLKmode to alias anything. */
2666 if (GET_MODE (mem) == BLKmode || GET_MODE (x) == BLKmode)
2667 return 1;
2668
2669 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2670 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2671 return 1;
2672
2673 /* Read-only memory is by definition never modified, and therefore can't
2674 conflict with anything. We don't expect to find read-only set on MEM,
2675 but stupid user tricks can produce them, so don't die. */
2676 if (MEM_READONLY_P (x))
2677 return 0;
2678
04ec15fa 2679 /* If we have MEMs referring to different address spaces (which can
a84256aa 2680 potentially overlap), we cannot easily tell from the addresses
2681 whether the references overlap. */
2682 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2683 return 1;
2684
2685 x_addr = XEXP (x, 0);
2686 mem_addr = XEXP (mem, 0);
2687 if (!((GET_CODE (x_addr) == VALUE
2688 && GET_CODE (mem_addr) != VALUE
2689 && reg_mentioned_p (x_addr, mem_addr))
2690 || (GET_CODE (x_addr) != VALUE
2691 && GET_CODE (mem_addr) == VALUE
2692 && reg_mentioned_p (mem_addr, x_addr))))
2693 {
2694 x_addr = get_addr (x_addr);
2695 mem_addr = get_addr (mem_addr);
2696 }
2697
2698 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), GET_MODE (mem_addr)))
2699 return 0;
2700
2701 x_addr = canon_rtx (x_addr);
2702 mem_addr = canon_rtx (mem_addr);
2703
2704 if (nonoverlapping_memrefs_p (mem, x, true))
2705 return 0;
2706
2707 if (aliases_everything_p (x))
2708 return 1;
2709
2710 /* We cannot use aliases_everything_p to test MEM, since we must look
2711 at MEM_ADDR, rather than XEXP (mem, 0). */
3bddda63 2712 if (GET_CODE (mem_addr) == AND)
a84256aa 2713 return 1;
2714
a84256aa 2715 /* TBAA not valid for loop_invarint */
2716 return rtx_refs_may_alias_p (x, mem, false);
2717}
2718
35d3eedd 2719void
6d8b68a3 2720init_alias_target (void)
35d3eedd 2721{
19cb6b50 2722 int i;
35d3eedd 2723
4e56ceb1 2724 if (!arg_base_value)
2725 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2726
6d8b68a3 2727 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2728
35d3eedd 2729 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2730 /* Check whether this register can hold an incoming pointer
2731 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2c0e001b 2732 numbers, so translate if necessary due to register windows. */
35d3eedd 2733 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2734 && HARD_REGNO_MODE_OK (i, Pmode))
4e56ceb1 2735 static_reg_base_value[i] = arg_base_value;
2736
2737 static_reg_base_value[STACK_POINTER_REGNUM] = unique_base_value (-1);
2738 static_reg_base_value[ARG_POINTER_REGNUM] = unique_base_value (-2);
2739 static_reg_base_value[FRAME_POINTER_REGNUM] = unique_base_value (-3);
5ae82d58 2740#if !HARD_FRAME_POINTER_IS_FRAME_POINTER
4e56ceb1 2741 static_reg_base_value[HARD_FRAME_POINTER_REGNUM] = unique_base_value (-4);
0c7f5242 2742#endif
2743}
2744
c7bf7428 2745/* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2746 to be memory reference. */
2747static bool memory_modified;
2748static void
a80f1c6c 2749memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
c7bf7428 2750{
e16ceb8e 2751 if (MEM_P (x))
c7bf7428 2752 {
5493cb9a 2753 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
c7bf7428 2754 memory_modified = true;
2755 }
2756}
2757
2758
2759/* Return true when INSN possibly modify memory contents of MEM
0c6d8c36 2760 (i.e. address can be modified). */
c7bf7428 2761bool
5493cb9a 2762memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
c7bf7428 2763{
2764 if (!INSN_P (insn))
2765 return false;
2766 memory_modified = false;
a80f1c6c 2767 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
c7bf7428 2768 return memory_modified;
2769}
2770
73f5c1e3 2771/* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2772 array. */
2773
ea0cb7ae 2774void
aecda0d6 2775init_alias_analysis (void)
ea0cb7ae 2776{
e5b19a8c 2777 unsigned int maxreg = max_reg_num ();
37b864db 2778 int changed, pass;
19cb6b50 2779 int i;
2780 unsigned int ui;
2781 rtx insn;
ea0cb7ae 2782
376c21d1 2783 timevar_push (TV_ALIAS_ANALYSIS);
2784
12cb06ad 2785 reg_known_value_size = maxreg - FIRST_PSEUDO_REGISTER;
ba72912a 2786 reg_known_value = ggc_alloc_cleared_vec_rtx (reg_known_value_size);
364c0c59 2787 reg_known_equiv_p = XCNEWVEC (bool, reg_known_value_size);
ea0cb7ae 2788
980f3285 2789 /* If we have memory allocated from the previous run, use it. */
e5b19a8c 2790 if (old_reg_base_value)
980f3285 2791 reg_base_value = old_reg_base_value;
2792
2793 if (reg_base_value)
2794 VEC_truncate (rtx, reg_base_value, 0);
2795
e85c2c2d 2796 VEC_safe_grow_cleared (rtx, gc, reg_base_value, maxreg);
c2e94b25 2797
4c36ffe6 2798 new_reg_base_value = XNEWVEC (rtx, maxreg);
2799 reg_seen = XNEWVEC (char, maxreg);
06c5c535 2800
2801 /* The basic idea is that each pass through this loop will use the
2802 "constant" information from the previous pass to propagate alias
2803 information through another level of assignments.
2804
2805 This could get expensive if the assignment chains are long. Maybe
2806 we should throttle the number of iterations, possibly based on
35d3eedd 2807 the optimization level or flag_expensive_optimizations.
06c5c535 2808
2809 We could propagate more information in the first pass by making use
3072d30e 2810 of DF_REG_DEF_COUNT to determine immediately that the alias information
37b864db 2811 for a pseudo is "constant".
2812
2813 A program with an uninitialized variable can cause an infinite loop
2814 here. Instead of doing a full dataflow analysis to detect such problems
2815 we just cap the number of iterations for the loop.
2816
2817 The state of the arrays for the set chain in question does not matter
2818 since the program has undefined behavior. */
35d3eedd 2819
37b864db 2820 pass = 0;
35d3eedd 2821 do
06c5c535 2822 {
2823 /* Assume nothing will change this iteration of the loop. */
2824 changed = 0;
2825
06c5c535 2826 /* We want to assign the same IDs each iteration of this loop, so
4e56ceb1 2827 start counting from one each iteration of the loop. */
2828 unique_id = 1;
06c5c535 2829
35a3065a 2830 /* We're at the start of the function each iteration through the
06c5c535 2831 loop, so we're copying arguments. */
e5be0cc6 2832 copying_arguments = true;
ea0cb7ae 2833
35d3eedd 2834 /* Wipe the potential alias information clean for this pass. */
e5b19a8c 2835 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
f0ad0992 2836
35d3eedd 2837 /* Wipe the reg_seen array clean. */
e5b19a8c 2838 memset (reg_seen, 0, maxreg);
ea0cb7ae 2839
35d3eedd 2840 /* Mark all hard registers which may contain an address.
2841 The stack, frame and argument pointers may contain an address.
2842 An argument register which can hold a Pmode value may contain
2843 an address even if it is not in BASE_REGS.
f0ad0992 2844
35d3eedd 2845 The address expression is VOIDmode for an argument and
2846 Pmode for other registers. */
2847
e26fd9bc 2848 memcpy (new_reg_base_value, static_reg_base_value,
2849 FIRST_PSEUDO_REGISTER * sizeof (rtx));
35d3eedd 2850
06c5c535 2851 /* Walk the insns adding values to the new_reg_base_value array. */
2852 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
ea0cb7ae 2853 {
9204e736 2854 if (INSN_P (insn))
06c5c535 2855 {
35d3eedd 2856 rtx note, set;
d6cb6164 2857
2858#if defined (HAVE_prologue) || defined (HAVE_epilogue)
35a3065a 2859 /* The prologue/epilogue insns are not threaded onto the
fd037eda 2860 insn chain until after reload has completed. Thus,
2861 there is no sense wasting time checking if INSN is in
2862 the prologue/epilogue until after reload has completed. */
2863 if (reload_completed
2864 && prologue_epilogue_contains (insn))
d6cb6164 2865 continue;
2866#endif
2867
06c5c535 2868 /* If this insn has a noalias note, process it, Otherwise,
a0c938f0 2869 scan for sets. A simple set will have no side effects
2870 which could change the base value of any other register. */
35d3eedd 2871
06c5c535 2872 if (GET_CODE (PATTERN (insn)) == SET
d6cb6164 2873 && REG_NOTES (insn) != 0
2874 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
ec8895d7 2875 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
06c5c535 2876 else
ec8895d7 2877 note_stores (PATTERN (insn), record_set, NULL);
35d3eedd 2878
2879 set = single_set (insn);
2880
2881 if (set != 0
8ad4c111 2882 && REG_P (SET_DEST (set))
9db10305 2883 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
35d3eedd 2884 {
9db10305 2885 unsigned int regno = REGNO (SET_DEST (set));
970c0c06 2886 rtx src = SET_SRC (set);
12cb06ad 2887 rtx t;
970c0c06 2888
750a330e 2889 note = find_reg_equal_equiv_note (insn);
2890 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3072d30e 2891 && DF_REG_DEF_COUNT (regno) != 1)
750a330e 2892 note = NULL_RTX;
2893
2894 if (note != NULL_RTX
970c0c06 2895 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
97fb4325 2896 && ! rtx_varies_p (XEXP (note, 0), 1)
12cb06ad 2897 && ! reg_overlap_mentioned_p (SET_DEST (set),
2898 XEXP (note, 0)))
970c0c06 2899 {
12cb06ad 2900 set_reg_known_value (regno, XEXP (note, 0));
2901 set_reg_known_equiv_p (regno,
2902 REG_NOTE_KIND (note) == REG_EQUIV);
970c0c06 2903 }
3072d30e 2904 else if (DF_REG_DEF_COUNT (regno) == 1
970c0c06 2905 && GET_CODE (src) == PLUS
8ad4c111 2906 && REG_P (XEXP (src, 0))
12cb06ad 2907 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
971ba038 2908 && CONST_INT_P (XEXP (src, 1)))
970c0c06 2909 {
12cb06ad 2910 t = plus_constant (t, INTVAL (XEXP (src, 1)));
2911 set_reg_known_value (regno, t);
2912 set_reg_known_equiv_p (regno, 0);
970c0c06 2913 }
3072d30e 2914 else if (DF_REG_DEF_COUNT (regno) == 1
970c0c06 2915 && ! rtx_varies_p (src, 1))
2916 {
12cb06ad 2917 set_reg_known_value (regno, src);
2918 set_reg_known_equiv_p (regno, 0);
970c0c06 2919 }
35d3eedd 2920 }
06c5c535 2921 }
6d7dc5b9 2922 else if (NOTE_P (insn)
ad4583d9 2923 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
e5be0cc6 2924 copying_arguments = false;
35d3eedd 2925 }
06c5c535 2926
35d3eedd 2927 /* Now propagate values from new_reg_base_value to reg_base_value. */
d4473c84 2928 gcc_assert (maxreg == (unsigned int) max_reg_num ());
a0c938f0 2929
e5b19a8c 2930 for (ui = 0; ui < maxreg; ui++)
35d3eedd 2931 {
274c11d8 2932 if (new_reg_base_value[ui]
980f3285 2933 && new_reg_base_value[ui] != VEC_index (rtx, reg_base_value, ui)
e5b19a8c 2934 && ! rtx_equal_p (new_reg_base_value[ui],
980f3285 2935 VEC_index (rtx, reg_base_value, ui)))
06c5c535 2936 {
980f3285 2937 VEC_replace (rtx, reg_base_value, ui, new_reg_base_value[ui]);
35d3eedd 2938 changed = 1;
06c5c535 2939 }
ea0cb7ae 2940 }
ea0cb7ae 2941 }
35d3eedd 2942 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
ea0cb7ae 2943
2944 /* Fill in the remaining entries. */
12cb06ad 2945 for (i = 0; i < (int)reg_known_value_size; i++)
ea0cb7ae 2946 if (reg_known_value[i] == 0)
12cb06ad 2947 reg_known_value[i] = regno_reg_rtx[i + FIRST_PSEUDO_REGISTER];
ea0cb7ae 2948
7098ef4a 2949 /* Clean up. */
2950 free (new_reg_base_value);
06c5c535 2951 new_reg_base_value = 0;
7098ef4a 2952 free (reg_seen);
ea0cb7ae 2953 reg_seen = 0;
376c21d1 2954 timevar_pop (TV_ALIAS_ANALYSIS);
ea0cb7ae 2955}
2956
1837e966 2957/* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
2958 Special API for var-tracking pass purposes. */
2959
2960void
2961vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
2962{
2963 VEC_replace (rtx, reg_base_value, REGNO (reg1), REG_BASE_VALUE (reg2));
2964}
2965
ea0cb7ae 2966void
aecda0d6 2967end_alias_analysis (void)
ea0cb7ae 2968{
e5b19a8c 2969 old_reg_base_value = reg_base_value;
12cb06ad 2970 ggc_free (reg_known_value);
ea0cb7ae 2971 reg_known_value = 0;
c2e94b25 2972 reg_known_value_size = 0;
12cb06ad 2973 free (reg_known_equiv_p);
7098ef4a 2974 reg_known_equiv_p = 0;
ea0cb7ae 2975}
1f3233d1 2976
2977#include "gt-alias.h"