]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/alias.c
thumb1-imm.c: Skip it in non-thumb1 target.
[thirdparty/gcc.git] / gcc / alias.c
CommitLineData
9ae8ffe7 1/* Alias analysis for GNU C
62e5bf5d 2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
c75c517d 3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
9ae8ffe7
JL
4 Contributed by John Carr (jfc@mit.edu).
5
1322177d 6This file is part of GCC.
9ae8ffe7 7
1322177d
LB
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
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
9ae8ffe7 12
1322177d
LB
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.
9ae8ffe7
JL
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
9ae8ffe7
JL
21
22#include "config.h"
670ee920 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
9ae8ffe7 26#include "rtl.h"
7790df19 27#include "tree.h"
6baf1cc8 28#include "tm_p.h"
49ad7cfa 29#include "function.h"
78528714
JQ
30#include "alias.h"
31#include "emit-rtl.h"
9ae8ffe7
JL
32#include "regs.h"
33#include "hard-reg-set.h"
e004f2f7 34#include "basic-block.h"
9ae8ffe7 35#include "flags.h"
264fac34 36#include "output.h"
718f9c0f 37#include "diagnostic-core.h"
eab5c70a 38#include "cselib.h"
3932261a 39#include "splay-tree.h"
ac606739 40#include "ggc.h"
d23c55c2 41#include "langhooks.h"
0d446150 42#include "timevar.h"
ab780373 43#include "target.h"
b255a036 44#include "cgraph.h"
ef330312 45#include "tree-pass.h"
6fb5fa3c 46#include "df.h"
55b34b5f
RG
47#include "tree-ssa-alias.h"
48#include "pointer-set.h"
49#include "tree-flow.h"
ea900239
DB
50
51/* The aliasing API provided here solves related but different problems:
52
c22cacf3 53 Say there exists (in c)
ea900239
DB
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
c22cacf3 91 instances do not escape across the compilation boundary.
ea900239
DB
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*/
3932261a
MM
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
ac3d9668
RK
104 different alias sets cannot alias each other, with one important
105 exception. Consider something like:
3932261a 106
01d28c3f 107 struct S { int i; double d; };
3932261a
MM
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:
c22cacf3
MS
113 struct S
114 / \
3932261a 115 / \
c22cacf3
MS
116 |/_ _\|
117 int double
3932261a 118
ac3d9668
RK
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
3bdf5ad1
RK
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
95bd1dd7 125 past immediate descendants, however, since we propagate all
3bdf5ad1 126 grandchildren up one level.
3932261a
MM
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
7e5487a2 132struct GTY(()) alias_set_entry_d {
3932261a 133 /* The alias set number, as stored in MEM_ALIAS_SET. */
4862826d 134 alias_set_type alias_set;
3932261a 135
4c067742
RG
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
3932261a 140 /* The children of the alias set. These are not just the immediate
95bd1dd7 141 children, but, in fact, all descendants. So, if we have:
3932261a 142
ca7fd9cd 143 struct T { struct S s; float f; }
3932261a
MM
144
145 continuing our example above, the children here will be all of
146 `int', `double', `float', and `struct S'. */
b604074c 147 splay_tree GTY((param1_is (int), param2_is (int))) children;
b604074c 148};
7e5487a2 149typedef struct alias_set_entry_d *alias_set_entry;
9ae8ffe7 150
ed7a4b4b 151static int rtx_equal_for_memref_p (const_rtx, const_rtx);
4682ae04 152static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
7bc980e1 153static void record_set (rtx, const_rtx, void *);
4682ae04
AJ
154static int base_alias_check (rtx, rtx, enum machine_mode,
155 enum machine_mode);
156static rtx find_base_value (rtx);
4f588890 157static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
4682ae04 158static int insert_subset_children (splay_tree_node, void*);
4862826d 159static alias_set_entry get_alias_set_entry (alias_set_type);
4f588890
KG
160static int aliases_everything_p (const_rtx);
161static bool nonoverlapping_component_refs_p (const_tree, const_tree);
4682ae04 162static tree decl_for_component_ref (tree);
4f588890 163static int write_dependence_p (const_rtx, const_rtx, int);
4682ae04 164
aa317c97 165static void memory_modified_1 (rtx, const_rtx, void *);
9ae8ffe7
JL
166
167/* Set up all info needed to perform alias analysis on memory references. */
168
d4b60170 169/* Returns the size in bytes of the mode of X. */
9ae8ffe7
JL
170#define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
171
41472af8 172/* Returns nonzero if MEM1 and MEM2 do not alias because they are in
264fac34
MM
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) \
3932261a 177 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
41472af8 178
ea64ef27 179/* Cap the number of passes we make over the insns propagating alias
ac3d9668 180 information through set chains. 10 is a completely arbitrary choice. */
ea64ef27 181#define MAX_ALIAS_LOOP_PASSES 10
ca7fd9cd 182
9ae8ffe7
JL
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
2a2c8203
JC
187 of the first set.
188
189 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
9fc37b2b 190 expressions represent three types of base:
b3b5ad95 191
9fc37b2b
RS
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.
b3b5ad95 195
9fc37b2b
RS
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. */
2a2c8203 215
08c79682 216static GTY(()) VEC(rtx,gc) *reg_base_value;
ac606739 217static rtx *new_reg_base_value;
c582d54a 218
9fc37b2b
RS
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
c582d54a
JH
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. */
08c79682 229static GTY((deletable)) VEC(rtx,gc) *old_reg_base_value;
d4b60170 230
7bf84454
RS
231#define static_reg_base_value \
232 (this_target_rtl->x_static_reg_base_value)
bf1660a6 233
08c79682
KH
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)
9ae8ffe7 237
c13e8210 238/* Vector indexed by N giving the initial (unchanging) value known for
bb1acb3e
RH
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;
9ae8ffe7
JL
242
243/* Indicates number of valid entries in reg_known_value. */
bb1acb3e 244static GTY(()) unsigned int reg_known_value_size;
9ae8ffe7
JL
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
ac3d9668
RK
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. */
bb1acb3e 258static bool *reg_known_equiv_p;
9ae8ffe7 259
2a2c8203
JC
260/* True when scanning insns from the start of the rtl to the
261 NOTE_INSN_FUNCTION_BEG note. */
83bbd9b6 262static bool copying_arguments;
9ae8ffe7 263
1a5640b4
KH
264DEF_VEC_P(alias_set_entry);
265DEF_VEC_ALLOC_P(alias_set_entry,gc);
266
3932261a 267/* The splay-tree used to store the various alias set entries. */
1a5640b4 268static GTY (()) VEC(alias_set_entry,gc) *alias_sets;
ac3d9668 269\f
55b34b5f
RG
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
366f945f
RG
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
55b34b5f
RG
296 /* If this is a pointer dereference of a non-SSA_NAME punt.
297 ??? We could replace it with a pointer to anything. */
70f34814
RG
298 if ((INDIRECT_REF_P (base)
299 || TREE_CODE (base) == MEM_REF)
55b34b5f
RG
300 && TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME)
301 return false;
d15adbeb
RG
302 if (TREE_CODE (base) == TARGET_MEM_REF
303 && TMR_BASE (base)
304 && TREE_CODE (TMR_BASE (base)) != SSA_NAME)
305 return false;
55b34b5f
RG
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)
70f34814 317 ref->base = build_simple_mem_ref (*(tree *)namep);
55b34b5f 318 }
d15adbeb 319 else if (TREE_CODE (base) == TARGET_MEM_REF
4d948885
RG
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))
d15adbeb
RG
323 && cfun->gimple_df->decls_to_pointers != NULL)
324 {
325 void *namep;
326 namep = pointer_map_contains (cfun->gimple_df->decls_to_pointers,
4d948885 327 TREE_OPERAND (TMR_BASE (base), 0));
d15adbeb
RG
328 if (namep)
329 ref->base = build_simple_mem_ref (*(tree *)namep);
330 }
55b34b5f
RG
331
332 ref->ref_alias_set = MEM_ALIAS_SET (mem);
333
f5541398 334 /* If MEM_OFFSET or MEM_SIZE are unknown we have to punt.
366f945f 335 Keep points-to related information though. */
527210c4 336 if (!MEM_OFFSET_KNOWN_P (mem)
f5541398 337 || !MEM_SIZE_KNOWN_P (mem))
366f945f
RG
338 {
339 ref->ref = NULL_TREE;
340 ref->offset = 0;
341 ref->size = -1;
342 ref->max_size = -1;
343 return true;
344 }
345
b0e96404
RG
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. */
527210c4
RS
349 if (MEM_OFFSET (mem) < 0
350 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
b0e96404
RG
351 return true;
352
527210c4 353 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
f5541398 354 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
b0e96404
RG
355
356 /* The MEM may extend into adjacent fields, so adjust max_size if
357 necessary. */
358 if (ref->max_size != -1
359 && ref->size > ref->max_size)
360 ref->max_size = ref->size;
361
362 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
363 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
364 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
365 && (ref->offset < 0
366 || (DECL_P (ref->base)
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;
55b34b5f
RG
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
55e3bc4c
RG
388 return refs_may_alias_p_1 (&ref1, &ref2,
389 tbaa_p
390 && MEM_ALIAS_SET (x) != 0
391 && MEM_ALIAS_SET (mem) != 0);
55b34b5f
RG
392}
393
3932261a
MM
394/* Returns a pointer to the alias set entry for ALIAS_SET, if there is
395 such an entry, or NULL otherwise. */
396
9ddb66ca 397static inline alias_set_entry
4862826d 398get_alias_set_entry (alias_set_type alias_set)
3932261a 399{
1a5640b4 400 return VEC_index (alias_set_entry, alias_sets, alias_set);
3932261a
MM
401}
402
ac3d9668
RK
403/* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
404 the two MEMs cannot alias each other. */
3932261a 405
9ddb66ca 406static inline int
4f588890 407mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
3932261a 408{
3932261a
MM
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. */
298e6adc
NS
416 gcc_assert (flag_strict_aliasing
417 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
3932261a 418
1da68f56
RK
419 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
420}
3932261a 421
1da68f56
RK
422/* Insert the NODE into the splay tree given by DATA. Used by
423 record_alias_subset via splay_tree_foreach. */
424
425static int
4682ae04 426insert_subset_children (splay_tree_node node, void *data)
1da68f56
RK
427{
428 splay_tree_insert ((splay_tree) data, node->key, node->value);
429
430 return 0;
431}
432
c58936b6
DB
433/* Return true if the first alias set is a subset of the second. */
434
435bool
4862826d 436alias_set_subset_of (alias_set_type set1, alias_set_type set2)
c58936b6
DB
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
038a39d1 447 && (ase->has_zero_child
a7a512be
RG
448 || splay_tree_lookup (ase->children,
449 (splay_tree_key) set1)))
c58936b6
DB
450 return true;
451 return false;
452}
453
1da68f56
RK
454/* Return 1 if the two specified alias sets may conflict. */
455
456int
4862826d 457alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
1da68f56
RK
458{
459 alias_set_entry ase;
460
836f7794
EB
461 /* The easy case. */
462 if (alias_sets_must_conflict_p (set1, set2))
1da68f56 463 return 1;
3932261a 464
3bdf5ad1 465 /* See if the first alias set is a subset of the second. */
1da68f56 466 ase = get_alias_set_entry (set1);
2bf105ab
RK
467 if (ase != 0
468 && (ase->has_zero_child
469 || splay_tree_lookup (ase->children,
1da68f56
RK
470 (splay_tree_key) set2)))
471 return 1;
3932261a
MM
472
473 /* Now do the same, but with the alias sets reversed. */
1da68f56 474 ase = get_alias_set_entry (set2);
2bf105ab
RK
475 if (ase != 0
476 && (ase->has_zero_child
477 || splay_tree_lookup (ase->children,
1da68f56
RK
478 (splay_tree_key) set1)))
479 return 1;
3932261a 480
1da68f56 481 /* The two alias sets are distinct and neither one is the
836f7794 482 child of the other. Therefore, they cannot conflict. */
1da68f56 483 return 0;
3932261a 484}
5399d643 485
836f7794 486/* Return 1 if the two specified alias sets will always conflict. */
5399d643
JW
487
488int
4862826d 489alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
5399d643
JW
490{
491 if (set1 == 0 || set2 == 0 || set1 == set2)
492 return 1;
493
494 return 0;
495}
496
1da68f56
RK
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
4682ae04 503objects_must_conflict_p (tree t1, tree t2)
1da68f56 504{
4862826d 505 alias_set_type set1, set2;
82d610ec 506
e8ea2809
RK
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. */
981a4c34 510 if (t1 == 0 && t2 == 0)
e8ea2809
RK
511 return 0;
512
1da68f56
RK
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
82d610ec
RK
519 set1 = t1 ? get_alias_set (t1) : 0;
520 set2 = t2 ? get_alias_set (t2) : 0;
1da68f56 521
836f7794
EB
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
82d610ec
RK
524 t2 for which a pair of subobjects of these respective subtypes
525 overlaps on the stack. */
836f7794 526 return alias_sets_must_conflict_p (set1, set2);
1da68f56
RK
527}
528\f
2039d7aa
RH
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
22ea9ec0 540component_uses_parent_alias_set (const_tree t)
6e24b709 541{
afe84921
RH
542 while (1)
543 {
2039d7aa 544 /* If we're at the end, it vacuously uses its own alias set. */
afe84921 545 if (!handled_component_p (t))
2039d7aa 546 return false;
afe84921
RH
547
548 switch (TREE_CODE (t))
549 {
550 case COMPONENT_REF:
551 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
2039d7aa 552 return true;
afe84921
RH
553 break;
554
555 case ARRAY_REF:
556 case ARRAY_RANGE_REF:
557 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
2039d7aa 558 return true;
afe84921
RH
559 break;
560
561 case REALPART_EXPR:
562 case IMAGPART_EXPR:
563 break;
564
565 default:
566 /* Bitfields and casts are never addressable. */
2039d7aa 567 return true;
afe84921
RH
568 }
569
570 t = TREE_OPERAND (t, 0);
2039d7aa
RH
571 if (get_alias_set (TREE_TYPE (t)) == 0)
572 return true;
afe84921 573 }
6e24b709
RK
574}
575
5006671f
RG
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
5b21f0f3 588 /* All we care about is the type. */
5006671f 589 if (! TYPE_P (t))
5b21f0f3 590 t = TREE_TYPE (t);
5006671f
RG
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
3bdf5ad1
RK
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
4862826d 624alias_set_type
4682ae04 625get_alias_set (tree t)
3bdf5ad1 626{
4862826d 627 alias_set_type set;
3bdf5ad1
RK
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
f47e9b4e
RK
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
738cc472 641 aren't types. */
f824e5c3 642 if (! TYPE_P (t))
3bdf5ad1 643 {
ebcc3d93 644 tree inner;
738cc472 645
70f34814
RG
646 /* Give the language a chance to do something with this tree
647 before we look at it. */
8ac61af7 648 STRIP_NOPS (t);
ae2bcd98 649 set = lang_hooks.get_alias_set (t);
8ac61af7
RK
650 if (set != -1)
651 return set;
652
70f34814 653 /* Get the base object of the reference. */
ebcc3d93 654 inner = t;
6fce44af 655 while (handled_component_p (inner))
738cc472 656 {
70f34814
RG
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);
6fce44af 662 inner = TREE_OPERAND (inner, 0);
738cc472
RK
663 }
664
70f34814
RG
665 /* Handle pointer dereferences here, they can override the
666 alias-set. */
1b096a0a 667 if (INDIRECT_REF_P (inner))
738cc472 668 {
5006671f
RG
669 set = get_deref_alias_set_1 (TREE_OPERAND (inner, 0));
670 if (set != -1)
671 return set;
738cc472 672 }
4b228e61
RG
673 else if (TREE_CODE (inner) == TARGET_MEM_REF)
674 return get_deref_alias_set (TMR_OFFSET (inner));
70f34814
RG
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
7be7d292
EB
686 && TYPE_MAIN_VARIANT (TREE_TYPE (inner))
687 != TYPE_MAIN_VARIANT
688 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1)))))
70f34814 689 return get_deref_alias_set (TREE_OPERAND (inner, 1));
738cc472
RK
690
691 /* Otherwise, pick up the outermost object that we could have a pointer
6fce44af 692 to, processing conversions as above. */
2039d7aa 693 while (component_uses_parent_alias_set (t))
f47e9b4e 694 {
6fce44af 695 t = TREE_OPERAND (t, 0);
8ac61af7
RK
696 STRIP_NOPS (t);
697 }
f824e5c3 698
738cc472
RK
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!). */
5755cd38 702 if (TREE_CODE (t) == VAR_DECL
3c0cb5de 703 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
5755cd38
JM
704 return MEM_ALIAS_SET (DECL_RTL (t));
705
f824e5c3
RK
706 /* Now all we care about is the type. */
707 t = TREE_TYPE (t);
3bdf5ad1
RK
708 }
709
3bdf5ad1 710 /* Variant qualifiers don't affect the alias set, so get the main
daad0278 711 variant. */
3bdf5ad1 712 t = TYPE_MAIN_VARIANT (t);
daad0278
RG
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))
cb9c2485
JM
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 }
7be7d292 726
daad0278 727 t = TYPE_CANONICAL (t);
7be7d292 728
96d91dcf
RG
729 /* The canonical type should not require structural equality checks. */
730 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
daad0278
RG
731
732 /* If this is a type with a known alias set, return it. */
738cc472 733 if (TYPE_ALIAS_SET_KNOWN_P (t))
3bdf5ad1
RK
734 return TYPE_ALIAS_SET (t);
735
36784d0e
RG
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
3bdf5ad1 748 /* See if the language has special handling for this type. */
ae2bcd98 749 set = lang_hooks.get_alias_set (t);
8ac61af7 750 if (set != -1)
738cc472 751 return set;
2bf105ab 752
3bdf5ad1
RK
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.) */
7be7d292 756 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
3bdf5ad1 757 set = 0;
74d86f4f
RH
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
4653cae5
RG
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. */
7be7d292 774 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
4653cae5
RG
775 set = get_alias_set (TREE_TYPE (t));
776
0ceb0201
RG
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)
96d91dcf 831 set = get_alias_set (ptr_type_node);
0ceb0201 832
7be7d292 833 /* Otherwise make a new alias set for this type. */
3bdf5ad1 834 else
96d91dcf
RG
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 }
3bdf5ad1
RK
844
845 TYPE_ALIAS_SET (t) = set;
2bf105ab 846
7be7d292
EB
847 /* If this is an aggregate type or a complex type, we must record any
848 component aliasing information. */
1d79fd2c 849 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
2bf105ab
RK
850 record_component_aliases (t);
851
3bdf5ad1
RK
852 return set;
853}
854
855/* Return a brand-new alias set. */
856
4862826d 857alias_set_type
4682ae04 858new_alias_set (void)
3bdf5ad1 859{
3bdf5ad1 860 if (flag_strict_aliasing)
9ddb66ca 861 {
1a5640b4
KH
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;
9ddb66ca 866 }
3bdf5ad1
RK
867 else
868 return 0;
869}
3932261a 870
01d28c3f
JM
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.
3932261a
MM
880
881 It is illegal for SUPERSET to be zero; everything is implicitly a
882 subset of alias set zero. */
883
794511d2 884void
4862826d 885record_alias_subset (alias_set_type superset, alias_set_type subset)
3932261a
MM
886{
887 alias_set_entry superset_entry;
888 alias_set_entry subset_entry;
889
f47e9b4e
RK
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
298e6adc 895 gcc_assert (superset);
3932261a
MM
896
897 superset_entry = get_alias_set_entry (superset);
ca7fd9cd 898 if (superset_entry == 0)
3932261a
MM
899 {
900 /* Create an entry for the SUPERSET, so that we have a place to
901 attach the SUBSET. */
a9429e29 902 superset_entry = ggc_alloc_cleared_alias_set_entry_d ();
3932261a 903 superset_entry->alias_set = superset;
ca7fd9cd 904 superset_entry->children
a9429e29
LB
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);
570eb5c8 908 superset_entry->has_zero_child = 0;
1a5640b4 909 VEC_replace (alias_set_entry, alias_sets, superset, superset_entry);
3932261a
MM
910 }
911
2bf105ab
RK
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. */
ca7fd9cd 919 if (subset_entry)
2bf105ab
RK
920 {
921 if (subset_entry->has_zero_child)
922 superset_entry->has_zero_child = 1;
d4b60170 923
2bf105ab
RK
924 splay_tree_foreach (subset_entry->children, insert_subset_children,
925 superset_entry->children);
926 }
3932261a 927
2bf105ab 928 /* Enter the SUBSET itself as a child of the SUPERSET. */
ca7fd9cd 929 splay_tree_insert (superset_entry->children,
2bf105ab
RK
930 (splay_tree_key) subset, 0);
931 }
3932261a
MM
932}
933
a0c33338
RK
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
b5487346
EB
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. */
a0c33338
RK
938
939void
4682ae04 940record_component_aliases (tree type)
a0c33338 941{
4862826d 942 alias_set_type superset = get_alias_set (type);
a0c33338
RK
943 tree field;
944
945 if (superset == 0)
946 return;
947
948 switch (TREE_CODE (type))
949 {
a0c33338
RK
950 case RECORD_TYPE:
951 case UNION_TYPE:
952 case QUAL_UNION_TYPE:
6614fd40 953 /* Recursively record aliases for the base classes, if there are any. */
fa743e8c 954 if (TYPE_BINFO (type))
ca7fd9cd
KH
955 {
956 int i;
fa743e8c 957 tree binfo, base_binfo;
c22cacf3 958
fa743e8c
NS
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)));
ca7fd9cd 963 }
910ad8de 964 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
b5487346 965 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
2bf105ab 966 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
a0c33338
RK
967 break;
968
1d79fd2c
JW
969 case COMPLEX_TYPE:
970 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
971 break;
972
4653cae5
RG
973 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
974 element type. */
975
a0c33338
RK
976 default:
977 break;
978 }
979}
980
3bdf5ad1
RK
981/* Allocate an alias set for use in storing and reading from the varargs
982 spill area. */
983
4862826d 984static GTY(()) alias_set_type varargs_set = -1;
f103e34d 985
4862826d 986alias_set_type
4682ae04 987get_varargs_alias_set (void)
3bdf5ad1 988{
cd3ce9b4
JM
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
f103e34d
GK
996 if (varargs_set == -1)
997 varargs_set = new_alias_set ();
3bdf5ad1 998
f103e34d 999 return varargs_set;
cd3ce9b4 1000#endif
3bdf5ad1
RK
1001}
1002
1003/* Likewise, but used for the fixed portions of the frame, e.g., register
1004 save areas. */
1005
4862826d 1006static GTY(()) alias_set_type frame_set = -1;
f103e34d 1007
4862826d 1008alias_set_type
4682ae04 1009get_frame_alias_set (void)
3bdf5ad1 1010{
f103e34d
GK
1011 if (frame_set == -1)
1012 frame_set = new_alias_set ();
3bdf5ad1 1013
f103e34d 1014 return frame_set;
3bdf5ad1
RK
1015}
1016
9fc37b2b
RS
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
2a2c8203
JC
1054/* Inside SRC, the source of a SET, find a base address. */
1055
9ae8ffe7 1056static rtx
4682ae04 1057find_base_value (rtx src)
9ae8ffe7 1058{
713f41f9 1059 unsigned int regno;
0aacc8ed 1060
53451050
RS
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
9ae8ffe7
JL
1066 switch (GET_CODE (src))
1067 {
1068 case SYMBOL_REF:
1069 case LABEL_REF:
1070 return src;
1071
1072 case REG:
fb6754f0 1073 regno = REGNO (src);
d4b60170 1074 /* At the start of a function, argument registers have known base
2a2c8203
JC
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. */
713f41f9
BS
1078 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1079 return new_reg_base_value[regno];
73774bc7 1080
eaf407a5 1081 /* If a pseudo has a known base value, return it. Do not do this
9b462c42
RH
1082 for non-fixed hard regs since it can result in a circular
1083 dependency chain for registers which have values at function entry.
eaf407a5
JL
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. */
9b462c42 1087 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
08c79682 1088 && regno < VEC_length (rtx, reg_base_value))
83bbd9b6
RH
1089 {
1090 /* If we're inside init_alias_analysis, use new_reg_base_value
1091 to reduce the number of relaxation iterations. */
1afdf91c 1092 if (new_reg_base_value && new_reg_base_value[regno]
6fb5fa3c 1093 && DF_REG_DEF_COUNT (regno) == 1)
83bbd9b6
RH
1094 return new_reg_base_value[regno];
1095
08c79682
KH
1096 if (VEC_index (rtx, reg_base_value, regno))
1097 return VEC_index (rtx, reg_base_value, regno);
83bbd9b6 1098 }
73774bc7 1099
e3f049a8 1100 return 0;
9ae8ffe7
JL
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)))
9fc37b2b 1110 return arg_base_value;
9ae8ffe7
JL
1111 return 0;
1112
1113 case CONST:
1114 src = XEXP (src, 0);
1115 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1116 break;
d4b60170 1117
ec5c56db 1118 /* ... fall through ... */
2a2c8203 1119
9ae8ffe7
JL
1120 case PLUS:
1121 case MINUS:
2a2c8203 1122 {
ec907dd8
JL
1123 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1124
0134bf2d
DE
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
ec907dd8
JL
1132 /* If either operand is a REG, then see if we already have
1133 a known value for it. */
0134bf2d 1134 if (REG_P (src_0))
ec907dd8
JL
1135 {
1136 temp = find_base_value (src_0);
d4b60170 1137 if (temp != 0)
ec907dd8
JL
1138 src_0 = temp;
1139 }
1140
0134bf2d 1141 if (REG_P (src_1))
ec907dd8
JL
1142 {
1143 temp = find_base_value (src_1);
d4b60170 1144 if (temp!= 0)
ec907dd8
JL
1145 src_1 = temp;
1146 }
2a2c8203 1147
0134bf2d
DE
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. */
9fc37b2b 1151 if (src_0 != 0 && known_base_value_p (src_0))
0134bf2d
DE
1152 return src_0;
1153
9fc37b2b 1154 if (src_1 != 0 && known_base_value_p (src_1))
0134bf2d
DE
1155 return src_1;
1156
d4b60170 1157 /* Guess which operand is the base address:
ec907dd8
JL
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. */
481683e1 1160 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
2a2c8203 1161 return find_base_value (src_0);
481683e1 1162 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
ec907dd8
JL
1163 return find_base_value (src_1);
1164
9ae8ffe7 1165 return 0;
2a2c8203
JC
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));
9ae8ffe7
JL
1172
1173 case AND:
1174 /* If the second operand is constant set the base
ec5c56db 1175 address to the first operand. */
481683e1 1176 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
2a2c8203 1177 return find_base_value (XEXP (src, 0));
9ae8ffe7
JL
1178 return 0;
1179
61f0131c 1180 case TRUNCATE:
d4ebfa65
BE
1181 /* As we do not know which address space the pointer is refering to, we can
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;
61f0131c
R
1186 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1187 break;
1188 /* Fall through. */
9ae8ffe7 1189 case HIGH:
d288e53d
DE
1190 case PRE_INC:
1191 case PRE_DEC:
1192 case POST_INC:
1193 case POST_DEC:
1194 case PRE_MODIFY:
1195 case POST_MODIFY:
2a2c8203 1196 return find_base_value (XEXP (src, 0));
1d300e19 1197
0aacc8ed
RK
1198 case ZERO_EXTEND:
1199 case SIGN_EXTEND: /* used for NT/Alpha pointers */
d4ebfa65
BE
1200 /* As we do not know which address space the pointer is refering to, we can
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
0aacc8ed
RK
1206 {
1207 rtx temp = find_base_value (XEXP (src, 0));
1208
5ae6cd0d 1209 if (temp != 0 && CONSTANT_P (temp))
0aacc8ed 1210 temp = convert_memory_address (Pmode, temp);
0aacc8ed
RK
1211
1212 return temp;
1213 }
1214
1d300e19
KG
1215 default:
1216 break;
9ae8ffe7
JL
1217 }
1218
1219 return 0;
1220}
1221
9fc37b2b
RS
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. */
9ae8ffe7 1225
d4b60170 1226/* While scanning insns to find base values, reg_seen[N] is nonzero if
9ae8ffe7
JL
1227 register N has been set in this function. */
1228static char *reg_seen;
1229
2a2c8203 1230static void
7bc980e1 1231record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
9ae8ffe7 1232{
b3694847 1233 unsigned regno;
9ae8ffe7 1234 rtx src;
c28b4e40 1235 int n;
9ae8ffe7 1236
f8cfc6aa 1237 if (!REG_P (dest))
9ae8ffe7
JL
1238 return;
1239
fb6754f0 1240 regno = REGNO (dest);
9ae8ffe7 1241
7a40b8b1 1242 gcc_checking_assert (regno < VEC_length (rtx, reg_base_value));
ac606739 1243
c28b4e40
JW
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)
66fd46b6 1247 n = hard_regno_nregs[regno][GET_MODE (dest)];
c28b4e40
JW
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
9ae8ffe7
JL
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 {
ec907dd8 1267 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
1268 return;
1269 }
1270 src = SET_SRC (set);
1271 }
1272 else
1273 {
9fc37b2b 1274 /* There's a REG_NOALIAS note against DEST. */
9ae8ffe7
JL
1275 if (reg_seen[regno])
1276 {
ec907dd8 1277 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
1278 return;
1279 }
1280 reg_seen[regno] = 1;
9fc37b2b 1281 new_reg_base_value[regno] = unique_base_value (unique_id++);
9ae8ffe7
JL
1282 return;
1283 }
1284
5da6f168
RS
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
c22cacf3 1297 extern int x, y; int *p = &x; p += (&y-&x);
5da6f168 1298
9ae8ffe7
JL
1299 ANSI C does not allow computing the difference of addresses
1300 of distinct top level objects. */
5da6f168
RS
1301 if (new_reg_base_value[regno] != 0
1302 && find_base_value (src) != new_reg_base_value[regno])
9ae8ffe7
JL
1303 switch (GET_CODE (src))
1304 {
2a2c8203 1305 case LO_SUM:
9ae8ffe7
JL
1306 case MINUS:
1307 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
ec907dd8 1308 new_reg_base_value[regno] = 0;
9ae8ffe7 1309 break;
61f0131c
R
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 }
9ae8ffe7 1326 case AND:
481683e1 1327 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
ec907dd8 1328 new_reg_base_value[regno] = 0;
9ae8ffe7 1329 break;
9ae8ffe7 1330 default:
ec907dd8 1331 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
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])
ec907dd8
JL
1336 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
1337 new_reg_base_value[regno] = find_base_value (src);
9ae8ffe7
JL
1338
1339 reg_seen[regno] = 1;
1340}
1341
8fd0a474
AM
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
bb1acb3e
RH
1350/* If a value is known for REGNO, return it. */
1351
c22cacf3 1352rtx
bb1acb3e
RH
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;
43fe47ca
JW
1362}
1363
bb1acb3e
RH
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
db048faf
MM
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
4682ae04 1409canon_rtx (rtx x)
9ae8ffe7
JL
1410{
1411 /* Recursively look for equivalences. */
f8cfc6aa 1412 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
bb1acb3e
RH
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)
9ae8ffe7
JL
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 {
481683e1 1428 if (CONST_INT_P (x0))
ed8908e7 1429 return plus_constant (x1, INTVAL (x0));
481683e1 1430 else if (CONST_INT_P (x1))
ed8908e7 1431 return plus_constant (x0, INTVAL (x1));
38a448ca 1432 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
9ae8ffe7
JL
1433 }
1434 }
d4b60170 1435
9ae8ffe7
JL
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. */
3c0cb5de 1440 else if (MEM_P (x))
f1ec5147 1441 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
d4b60170 1442
9ae8ffe7
JL
1443 return x;
1444}
1445
1446/* Return 1 if X and Y are identical-looking rtx's.
45183e03 1447 Expect that X and Y has been already canonicalized.
9ae8ffe7
JL
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
ed7a4b4b 1453rtx_equal_for_memref_p (const_rtx x, const_rtx y)
9ae8ffe7 1454{
b3694847
SS
1455 int i;
1456 int j;
1457 enum rtx_code code;
1458 const char *fmt;
9ae8ffe7
JL
1459
1460 if (x == 0 && y == 0)
1461 return 1;
1462 if (x == 0 || y == 0)
1463 return 0;
d4b60170 1464
9ae8ffe7
JL
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
db048faf
MM
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);
ca7fd9cd 1487
db048faf
MM
1488 case SYMBOL_REF:
1489 return XSTR (x, 0) == XSTR (y, 0);
1490
40e02b4a 1491 case VALUE:
db048faf
MM
1492 case CONST_INT:
1493 case CONST_DOUBLE:
091a3ac7 1494 case CONST_FIXED:
db048faf
MM
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
db048faf
MM
1500 default:
1501 break;
1502 }
9ae8ffe7 1503
45183e03
JH
1504 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1505 if (code == PLUS)
9ae8ffe7
JL
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))));
45183e03
JH
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. */
ec8e098d 1512 if (COMMUTATIVE_P (x))
45183e03
JH
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 }
ec8e098d 1523 else if (NON_COMMUTATIVE_P (x))
45183e03
JH
1524 {
1525 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
4682ae04 1526 canon_rtx (XEXP (y, 0)))
45183e03
JH
1527 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1528 canon_rtx (XEXP (y, 1))));
1529 }
ec8e098d 1530 else if (UNARY_P (x))
45183e03 1531 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
4682ae04 1532 canon_rtx (XEXP (y, 0)));
9ae8ffe7
JL
1533
1534 /* Compare the elements. If any pair of corresponding elements
de12be17
JC
1535 fail to match, return 0 for the whole things.
1536
1537 Limit cases to types which actually appear in addresses. */
9ae8ffe7
JL
1538
1539 fmt = GET_RTX_FORMAT (code);
1540 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1541 {
1542 switch (fmt[i])
1543 {
9ae8ffe7
JL
1544 case 'i':
1545 if (XINT (x, i) != XINT (y, i))
1546 return 0;
1547 break;
1548
9ae8ffe7
JL
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++)
45183e03
JH
1556 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1557 canon_rtx (XVECEXP (y, i, j))) == 0)
9ae8ffe7
JL
1558 return 0;
1559 break;
1560
1561 case 'e':
45183e03
JH
1562 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1563 canon_rtx (XEXP (y, i))) == 0)
9ae8ffe7
JL
1564 return 0;
1565 break;
1566
3237ac18
AH
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
aee21ba9
JL
1573 /* This can happen for an asm which clobbers memory. */
1574 case '0':
1575 break;
1576
9ae8ffe7
JL
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:
298e6adc 1581 gcc_unreachable ();
9ae8ffe7
JL
1582 }
1583 }
1584 return 1;
1585}
1586
94f24ddc 1587rtx
4682ae04 1588find_base_term (rtx x)
9ae8ffe7 1589{
eab5c70a 1590 cselib_val *val;
6f2ffb4b
AO
1591 struct elt_loc_list *l, *f;
1592 rtx ret;
eab5c70a 1593
b949ea8b
JW
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
9ae8ffe7
JL
1599 switch (GET_CODE (x))
1600 {
1601 case REG:
1602 return REG_BASE_VALUE (x);
1603
d288e53d 1604 case TRUNCATE:
d4ebfa65
BE
1605 /* As we do not know which address space the pointer is refering to, we can
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;
d288e53d 1610 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
ca7fd9cd 1611 return 0;
d288e53d 1612 /* Fall through. */
9ae8ffe7 1613 case HIGH:
6d849a2a
JL
1614 case PRE_INC:
1615 case PRE_DEC:
1616 case POST_INC:
1617 case POST_DEC:
d288e53d
DE
1618 case PRE_MODIFY:
1619 case POST_MODIFY:
6d849a2a
JL
1620 return find_base_term (XEXP (x, 0));
1621
1abade85
RK
1622 case ZERO_EXTEND:
1623 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
d4ebfa65
BE
1624 /* As we do not know which address space the pointer is refering to, we can
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
1abade85
RK
1630 {
1631 rtx temp = find_base_term (XEXP (x, 0));
1632
5ae6cd0d 1633 if (temp != 0 && CONSTANT_P (temp))
1abade85 1634 temp = convert_memory_address (Pmode, temp);
1abade85
RK
1635
1636 return temp;
1637 }
1638
eab5c70a
BS
1639 case VALUE:
1640 val = CSELIB_VAL_PTR (x);
6f2ffb4b
AO
1641 ret = NULL_RTX;
1642
40e02b4a 1643 if (!val)
6f2ffb4b
AO
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;
eab5c70a 1661
023f059b
JJ
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
9ae8ffe7
JL
1667 case CONST:
1668 x = XEXP (x, 0);
1669 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1670 return 0;
938d968e 1671 /* Fall through. */
9ae8ffe7
JL
1672 case PLUS:
1673 case MINUS:
1674 {
3c567fae
JL
1675 rtx tmp1 = XEXP (x, 0);
1676 rtx tmp2 = XEXP (x, 1);
1677
f5143c46 1678 /* This is a little bit tricky since we have to determine which of
3c567fae
JL
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. */
ca7fd9cd 1689
b949ea8b
JW
1690 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1691 return find_base_term (tmp2);
1692
3c567fae
JL
1693 /* If either operand is known to be a pointer, then use it
1694 to determine the base term. */
3502dc9c 1695 if (REG_P (tmp1) && REG_POINTER (tmp1))
7eba2d1f
LM
1696 {
1697 rtx base = find_base_term (tmp1);
1698 if (base)
1699 return base;
1700 }
3c567fae 1701
3502dc9c 1702 if (REG_P (tmp2) && REG_POINTER (tmp2))
7eba2d1f
LM
1703 {
1704 rtx base = find_base_term (tmp2);
1705 if (base)
1706 return base;
1707 }
3c567fae
JL
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. */
9fc37b2b 1717 if (tmp1 != 0 && known_base_value_p (tmp1))
3c567fae
JL
1718 return tmp1;
1719
9fc37b2b 1720 if (tmp2 != 0 && known_base_value_p (tmp2))
3c567fae
JL
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;
9ae8ffe7
JL
1727 }
1728
1729 case AND:
481683e1 1730 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
d288e53d 1731 return find_base_term (XEXP (x, 0));
9ae8ffe7
JL
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
4682ae04
AJ
1747base_alias_check (rtx x, rtx y, enum machine_mode x_mode,
1748 enum machine_mode y_mode)
9ae8ffe7
JL
1749{
1750 rtx x_base = find_base_term (x);
1751 rtx y_base = find_base_term (y);
1752
1c72c7f6
JC
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;
d4b60170 1759
1c72c7f6
JC
1760 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1761 return 1;
d4b60170 1762
1c72c7f6
JC
1763 x_base = find_base_term (x_c);
1764 if (x_base == 0)
1765 return 1;
1766 }
9ae8ffe7 1767
1c72c7f6
JC
1768 if (y_base == 0)
1769 {
1770 rtx y_c;
1771 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1772 return 1;
d4b60170 1773
1c72c7f6
JC
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))
9ae8ffe7
JL
1781 return 1;
1782
435da628
UB
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
481683e1 1793 && (!CONST_INT_P (XEXP (x, 1))
435da628
UB
1794 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1795 return 1;
1796 if (GET_CODE (y) == AND
481683e1 1797 && (!CONST_INT_P (XEXP (y, 1))
435da628
UB
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. */
9ae8ffe7 1802 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
435da628 1803 return 0;
9ae8ffe7 1804
9fc37b2b 1805 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
9ae8ffe7
JL
1806 return 0;
1807
0d3c82d6 1808 return 1;
9ae8ffe7
JL
1809}
1810
a5628378
AO
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
eab5c70a
BS
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. */
3bdf5ad1 1837
a13d4ebf 1838rtx
4682ae04 1839get_addr (rtx x)
eab5c70a
BS
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);
40e02b4a
JH
1847 if (v)
1848 {
0f68ba3e
AO
1849 bool have_equivs = cselib_have_permanent_equivalences ();
1850 if (have_equivs)
1851 v = canonical_cselib_val (v);
40e02b4a
JH
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)
0f68ba3e
AO
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))))
a5628378 1864 return l->loc;
0f68ba3e
AO
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;
40e02b4a 1877 }
eab5c70a
BS
1878 return x;
1879}
1880
39cec1ac
MH
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
04e2b4d3 1885static rtx
4682ae04 1886addr_side_effect_eval (rtx addr, int size, int n_refs)
39cec1ac
MH
1887{
1888 int offset = 0;
ca7fd9cd 1889
39cec1ac
MH
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 }
ca7fd9cd 1908
39cec1ac 1909 if (offset)
45183e03 1910 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
c22cacf3 1911 GEN_INT (offset));
39cec1ac
MH
1912 else
1913 addr = XEXP (addr, 0);
45183e03 1914 addr = canon_rtx (addr);
39cec1ac
MH
1915
1916 return addr;
1917}
1918
f47e08d9
RG
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
9ae8ffe7
JL
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.
45183e03 1928 Expect that canon_rtx has been already called for X and Y.
9ae8ffe7
JL
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
c02f035f
RH
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
9ae8ffe7 1938 Nice to notice that varying addresses cannot conflict with fp if no
f47e08d9
RG
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. */
9ae8ffe7 1944
9ae8ffe7 1945static int
4682ae04 1946memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
9ae8ffe7 1947{
eab5c70a 1948 if (GET_CODE (x) == VALUE)
5312b066
JJ
1949 {
1950 if (REG_P (y))
1951 {
24f8d71e
JJ
1952 struct elt_loc_list *l = NULL;
1953 if (CSELIB_VAL_PTR (x))
a5628378
AO
1954 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
1955 l; l = l->next)
24f8d71e
JJ
1956 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
1957 break;
5312b066
JJ
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 }
eab5c70a 1967 if (GET_CODE (y) == VALUE)
5312b066
JJ
1968 {
1969 if (REG_P (x))
1970 {
24f8d71e
JJ
1971 struct elt_loc_list *l = NULL;
1972 if (CSELIB_VAL_PTR (y))
a5628378
AO
1973 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
1974 l; l = l->next)
24f8d71e
JJ
1975 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
1976 break;
5312b066
JJ
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 }
9ae8ffe7
JL
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
45183e03 1991 x = addr_side_effect_eval (x, xsize, 0);
9ae8ffe7
JL
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
45183e03 1997 y = addr_side_effect_eval (y, ysize, 0);
9ae8ffe7
JL
1998
1999 if (rtx_equal_for_memref_p (x, y))
2000 {
c02f035f 2001 if (xsize <= 0 || ysize <= 0)
9ae8ffe7
JL
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
6e73e666
JC
2010 /* This code used to check for conflicts involving stack references and
2011 globals but the base address alias code now handles these cases. */
9ae8ffe7
JL
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);
481683e1 2031 if (CONST_INT_P (x1))
63be02db 2032 {
481683e1 2033 if (CONST_INT_P (y1))
63be02db
JM
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 }
481683e1 2040 else if (CONST_INT_P (y1))
9ae8ffe7
JL
2041 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2042
f47e08d9 2043 return -1;
9ae8ffe7 2044 }
481683e1 2045 else if (CONST_INT_P (x1))
9ae8ffe7
JL
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
481683e1 2055 if (CONST_INT_P (y1))
9ae8ffe7
JL
2056 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2057 else
f47e08d9 2058 return -1;
9ae8ffe7
JL
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))
f47e08d9 2073 return -1;
9ae8ffe7
JL
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. */
481683e1 2081 if (!CONST_INT_P (x1))
f47e08d9 2082 return -1;
9ae8ffe7
JL
2083 xsize /= INTVAL (x1);
2084 ysize /= INTVAL (x1);
2085 c /= INTVAL (x1);
2086 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2087 }
1d300e19
KG
2088
2089 default:
2090 break;
9ae8ffe7
JL
2091 }
2092
2093 /* Treat an access through an AND (e.g. a subword access on an Alpha)
ca7fd9cd 2094 as an access with indeterminate size. Assume that references
56ee9281
RH
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. */
481683e1 2097 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
56ee9281 2098 {
02e3377d 2099 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
56ee9281 2100 xsize = -1;
45183e03 2101 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)), ysize, y, c);
56ee9281 2102 }
481683e1 2103 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
c02f035f 2104 {
56ee9281 2105 /* ??? If we are indexing far enough into the array/structure, we
ca7fd9cd 2106 may yet be able to determine that we can not overlap. But we
c02f035f 2107 also need to that we are far enough from the end not to overlap
56ee9281 2108 a following reference, so we do nothing with that for now. */
02e3377d 2109 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
56ee9281 2110 ysize = -1;
45183e03 2111 return memrefs_conflict_p (xsize, x, ysize, canon_rtx (XEXP (y, 0)), c);
c02f035f 2112 }
9ae8ffe7
JL
2113
2114 if (CONSTANT_P (x))
2115 {
481683e1 2116 if (CONST_INT_P (x) && CONST_INT_P (y))
9ae8ffe7
JL
2117 {
2118 c += (INTVAL (y) - INTVAL (x));
c02f035f 2119 return (xsize <= 0 || ysize <= 0
9ae8ffe7
JL
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))
b949ea8b 2137 return (xsize <= 0 || ysize <= 0
c02f035f 2138 || (rtx_equal_for_memref_p (x, y)
b949ea8b 2139 && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
9ae8ffe7 2140
f47e08d9 2141 return -1;
9ae8ffe7 2142 }
f47e08d9
RG
2143
2144 return -1;
9ae8ffe7
JL
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
ca7fd9cd 2157 though.
9ae8ffe7 2158
53d9622b
RS
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
dc1618bc 2162 storeqi_unaligned pattern. */
9ae8ffe7
JL
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
4f588890 2168read_dependence (const_rtx mem, const_rtx x)
9ae8ffe7
JL
2169{
2170 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
2171}
2172
c6df88cb
MM
2173/* Returns nonzero if something about the mode or address format MEM1
2174 indicates that it might well alias *anything*. */
2175
2c72b78f 2176static int
4f588890 2177aliases_everything_p (const_rtx mem)
c6df88cb 2178{
c6df88cb 2179 if (GET_CODE (XEXP (mem, 0)) == AND)
35fd3193 2180 /* If the address is an AND, it's very hard to know at what it is
c6df88cb
MM
2181 actually pointing. */
2182 return 1;
ca7fd9cd 2183
c6df88cb
MM
2184 return 0;
2185}
2186
998d7deb
RH
2187/* Return true if we can determine that the fields referenced cannot
2188 overlap for any pair of objects. */
2189
2190static bool
4f588890 2191nonoverlapping_component_refs_p (const_tree x, const_tree y)
998d7deb 2192{
4f588890 2193 const_tree fieldx, fieldy, typex, typey, orig_y;
998d7deb 2194
4c7af939
AB
2195 if (!flag_strict_aliasing)
2196 return false;
2197
998d7deb
RH
2198 do
2199 {
2200 /* The comparison has to be done at a common type, since we don't
d6a7951f 2201 know how the inheritance hierarchy works. */
998d7deb
RH
2202 orig_y = y;
2203 do
2204 {
2205 fieldx = TREE_OPERAND (x, 1);
c05a0766 2206 typex = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx));
998d7deb
RH
2207
2208 y = orig_y;
2209 do
2210 {
2211 fieldy = TREE_OPERAND (y, 1);
c05a0766 2212 typey = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy));
998d7deb
RH
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);
998d7deb 2224 /* Never found a common type. */
c05a0766 2225 return false;
998d7deb
RH
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);
ca7fd9cd 2242
998d7deb
RH
2243 return false;
2244}
2245
2246/* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2247
2248static tree
4682ae04 2249decl_for_component_ref (tree x)
998d7deb
RH
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
527210c4
RS
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. */
998d7deb 2263
527210c4
RS
2264static void
2265adjust_offset_for_component_ref (tree x, bool *known_p,
2266 HOST_WIDE_INT *offset)
998d7deb 2267{
527210c4
RS
2268 if (!*known_p)
2269 return;
ca7fd9cd 2270 do
998d7deb 2271 {
527210c4 2272 tree xoffset = component_ref_field_offset (x);
998d7deb
RH
2273 tree field = TREE_OPERAND (x, 1);
2274
527210c4
RS
2275 if (! host_integerp (xoffset, 1))
2276 {
2277 *known_p = false;
2278 return;
2279 }
2280 *offset += (tree_low_cst (xoffset, 1)
998d7deb
RH
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);
998d7deb
RH
2287}
2288
95bd1dd7 2289/* Return nonzero if we can determine the exprs corresponding to memrefs
c6ea834c
BM
2290 X and Y and they do not overlap.
2291 If LOOP_VARIANT is set, skip offset-based disambiguation */
a4311dfe 2292
2e4e39f6 2293int
c6ea834c 2294nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
a4311dfe 2295{
998d7deb 2296 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
a4311dfe
RK
2297 rtx rtlx, rtly;
2298 rtx basex, basey;
527210c4
RS
2299 bool moffsetx_known_p, moffsety_known_p;
2300 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
a4311dfe
RK
2301 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2302
998d7deb
RH
2303 /* Unless both have exprs, we can't tell anything. */
2304 if (exprx == 0 || expry == 0)
2305 return 0;
2b22e382
RG
2306
2307 /* For spill-slot accesses make sure we have valid offsets. */
2308 if ((exprx == get_spill_slot_decl (false)
527210c4 2309 && ! MEM_OFFSET_KNOWN_P (x))
2b22e382 2310 || (expry == get_spill_slot_decl (false)
527210c4 2311 && ! MEM_OFFSET_KNOWN_P (y)))
2b22e382 2312 return 0;
c22cacf3 2313
998d7deb
RH
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
c22cacf3 2320
998d7deb 2321 /* If the field reference test failed, look at the DECLs involved. */
527210c4
RS
2322 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2323 if (moffsetx_known_p)
2324 moffsetx = MEM_OFFSET (x);
998d7deb
RH
2325 if (TREE_CODE (exprx) == COMPONENT_REF)
2326 {
2e0c984c
RG
2327 tree t = decl_for_component_ref (exprx);
2328 if (! t)
2329 return 0;
527210c4 2330 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2e0c984c 2331 exprx = t;
998d7deb 2332 }
c67a1cf6 2333
527210c4
RS
2334 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2335 if (moffsety_known_p)
2336 moffsety = MEM_OFFSET (y);
998d7deb
RH
2337 if (TREE_CODE (expry) == COMPONENT_REF)
2338 {
2e0c984c
RG
2339 tree t = decl_for_component_ref (expry);
2340 if (! t)
2341 return 0;
527210c4 2342 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2e0c984c 2343 expry = t;
998d7deb
RH
2344 }
2345
2346 if (! DECL_P (exprx) || ! DECL_P (expry))
a4311dfe
RK
2347 return 0;
2348
1307c758
RG
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
998d7deb
RH
2356 rtlx = DECL_RTL (exprx);
2357 rtly = DECL_RTL (expry);
a4311dfe 2358
1edcd60b
RK
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. */
3c0cb5de 2362 if ((!MEM_P (rtlx) || !MEM_P (rtly))
1edcd60b 2363 && ! rtx_equal_p (rtlx, rtly))
a4311dfe
RK
2364 return 1;
2365
09e881c9
BE
2366 /* If we have MEMs refering to different address spaces (which can
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
a4311dfe
RK
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. */
3c0cb5de 2377 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
481683e1 2378 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
a4311dfe
RK
2379 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2380
3c0cb5de 2381 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
481683e1 2382 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
a4311dfe
RK
2383 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2384
d746694a 2385 /* If the bases are different, we know they do not overlap if both
ca7fd9cd 2386 are constants or if one is a constant and the other a pointer into the
d746694a
RK
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))
ca7fd9cd
KH
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))));
a4311dfe 2395
c6ea834c
BM
2396 /* Offset based disambiguation not appropriate for loop invariant */
2397 if (loop_invariant)
2398 return 0;
2399
3c0cb5de 2400 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
f5541398 2401 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
a4311dfe 2402 : -1);
3c0cb5de 2403 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
f5541398
RS
2404 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2405 : -1);
a4311dfe 2406
0af5bc3e
RK
2407 /* If we have an offset for either memref, it can update the values computed
2408 above. */
527210c4
RS
2409 if (moffsetx_known_p)
2410 offsetx += moffsetx, sizex -= moffsetx;
2411 if (moffsety_known_p)
2412 offsety += moffsety, sizey -= moffsety;
a4311dfe 2413
0af5bc3e 2414 /* If a memref has both a size and an offset, we can use the smaller size.
efc981bb 2415 We can't do this if the offset isn't known because we must view this
0af5bc3e 2416 memref as being anywhere inside the DECL's MEM. */
527210c4 2417 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
f5541398 2418 sizex = MEM_SIZE (x);
527210c4 2419 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
f5541398 2420 sizey = MEM_SIZE (y);
a4311dfe
RK
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. */
a6f7c915 2431 return sizex >= 0 && offsety >= offsetx + sizex;
a4311dfe
RK
2432}
2433
9362286d
SB
2434/* Helper for true_dependence and canon_true_dependence.
2435 Checks for true dependence: X is read after store in MEM takes place.
9ae8ffe7 2436
9362286d
SB
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,
53d9622b 2447 const_rtx x, rtx x_addr, bool mem_canonicalized)
9ae8ffe7 2448{
49982682 2449 rtx base;
f47e08d9 2450 int ret;
9ae8ffe7 2451
9362286d
SB
2452 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2453 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2454
9ae8ffe7
JL
2455 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2456 return 1;
2457
c4484b8f 2458 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
ac3768f6 2459 This is used in epilogue deallocation functions, and in cselib. */
c4484b8f
RH
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;
9cd9e512
RH
2464 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2465 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2466 return 1;
c4484b8f 2467
389fdba0
RH
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,
41806d92 2470 but stupid user tricks can produce them, so don't die. */
389fdba0 2471 if (MEM_READONLY_P (x))
9ae8ffe7
JL
2472 return 0;
2473
09e881c9
BE
2474 /* If we have MEMs refering to different address spaces (which can
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
9362286d
SB
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)
2147c71c 2488 {
a522de15
SB
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 }
2147c71c 2501 }
eab5c70a 2502
55efb413
JW
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
eab5c70a 2509 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1c72c7f6
JC
2510 return 0;
2511
eab5c70a 2512 x_addr = canon_rtx (x_addr);
9362286d
SB
2513 if (!mem_canonicalized)
2514 mem_addr = canon_rtx (mem_addr);
6e73e666 2515
f47e08d9
RG
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
c6ea834c 2523 if (nonoverlapping_memrefs_p (mem, x, false))
0211b6ab
JW
2524 return 0;
2525
c6df88cb 2526 if (aliases_everything_p (x))
0211b6ab
JW
2527 return 1;
2528
f5143c46 2529 /* We cannot use aliases_everything_p to test MEM, since we must look
9362286d 2530 at MEM_ADDR, rather than XEXP (mem, 0). */
4fcf718a 2531 if (GET_CODE (mem_addr) == AND)
a13d4ebf
AM
2532 return 1;
2533
9362286d 2534 /* ??? In true_dependence we also allow BLKmode to alias anything. Why
a13d4ebf
AM
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
55b34b5f 2539 return rtx_refs_may_alias_p (x, mem, true);
a13d4ebf
AM
2540}
2541
9362286d
SB
2542/* True dependence: X is read after store in MEM takes place. */
2543
2544int
53d9622b 2545true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x)
9362286d
SB
2546{
2547 return true_dependence_1 (mem, mem_mode, NULL_RTX,
53d9622b 2548 x, NULL_RTX, /*mem_canonicalized=*/false);
9362286d
SB
2549}
2550
a13d4ebf 2551/* Canonical true dependence: X is read after store in MEM takes place.
ca7fd9cd
KH
2552 Variant of true_dependence which assumes MEM has already been
2553 canonicalized (hence we no longer do that here).
9362286d
SB
2554 The mem_addr argument has been added, since true_dependence_1 computed
2555 this value prior to canonicalizing. */
a13d4ebf
AM
2556
2557int
4f588890 2558canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
53d9622b 2559 const_rtx x, rtx x_addr)
a13d4ebf 2560{
9362286d 2561 return true_dependence_1 (mem, mem_mode, mem_addr,
53d9622b 2562 x, x_addr, /*mem_canonicalized=*/true);
9ae8ffe7
JL
2563}
2564
da7d8304 2565/* Returns nonzero if a write to X might alias a previous read from
389fdba0 2566 (or, if WRITEP is nonzero, a write to) MEM. */
9ae8ffe7 2567
2c72b78f 2568static int
4f588890 2569write_dependence_p (const_rtx mem, const_rtx x, int writep)
9ae8ffe7 2570{
6e73e666 2571 rtx x_addr, mem_addr;
49982682 2572 rtx base;
f47e08d9 2573 int ret;
6e73e666 2574
9ae8ffe7
JL
2575 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2576 return 1;
2577
c4484b8f
RH
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;
9cd9e512
RH
2584 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2585 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2586 return 1;
c4484b8f 2587
389fdba0
RH
2588 /* A read from read-only memory can't conflict with read-write memory. */
2589 if (!writep && MEM_READONLY_P (mem))
2590 return 0;
55efb413 2591
09e881c9
BE
2592 /* If we have MEMs refering to different address spaces (which can
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
2147c71c
L
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 }
55efb413 2610
49982682
JW
2611 if (! writep)
2612 {
55efb413 2613 base = find_base_term (mem_addr);
49982682
JW
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
eab5c70a
BS
2620 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2621 GET_MODE (mem)))
41472af8
MM
2622 return 0;
2623
eab5c70a
BS
2624 x_addr = canon_rtx (x_addr);
2625 mem_addr = canon_rtx (mem_addr);
6e73e666 2626
f47e08d9
RG
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
c6ea834c 2631 if (nonoverlapping_memrefs_p (x, mem, false))
c6df88cb
MM
2632 return 0;
2633
55b34b5f 2634 return rtx_refs_may_alias_p (x, mem, false);
c6df88cb
MM
2635}
2636
2637/* Anti dependence: X is written after read in MEM takes place. */
2638
2639int
4f588890 2640anti_dependence (const_rtx mem, const_rtx x)
c6df88cb 2641{
389fdba0 2642 return write_dependence_p (mem, x, /*writep=*/0);
9ae8ffe7
JL
2643}
2644
2645/* Output dependence: X is written after store in MEM takes place. */
2646
2647int
4f588890 2648output_dependence (const_rtx mem, const_rtx x)
9ae8ffe7 2649{
389fdba0 2650 return write_dependence_p (mem, x, /*writep=*/1);
9ae8ffe7 2651}
c14b9960 2652\f
6e73e666 2653
c6ea834c
BM
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;
c6ea834c
BM
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
2679 /* If we have MEMs refering to different address spaces (which can
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). */
4fcf718a 2712 if (GET_CODE (mem_addr) == AND)
c6ea834c
BM
2713 return 1;
2714
c6ea834c
BM
2715 /* TBAA not valid for loop_invarint */
2716 return rtx_refs_may_alias_p (x, mem, false);
2717}
2718
6e73e666 2719void
b5deb7b6 2720init_alias_target (void)
6e73e666 2721{
b3694847 2722 int i;
6e73e666 2723
9fc37b2b
RS
2724 if (!arg_base_value)
2725 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2726
b5deb7b6
SL
2727 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2728
6e73e666
JC
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
ec5c56db 2732 numbers, so translate if necessary due to register windows. */
6e73e666
JC
2733 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2734 && HARD_REGNO_MODE_OK (i, Pmode))
9fc37b2b
RS
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);
e3339d0f 2740#if !HARD_FRAME_POINTER_IS_FRAME_POINTER
9fc37b2b 2741 static_reg_base_value[HARD_FRAME_POINTER_REGNUM] = unique_base_value (-4);
bf1660a6
JL
2742#endif
2743}
2744
7b52eede
JH
2745/* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2746 to be memory reference. */
2747static bool memory_modified;
2748static void
aa317c97 2749memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
7b52eede 2750{
3c0cb5de 2751 if (MEM_P (x))
7b52eede 2752 {
9678086d 2753 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
7b52eede
JH
2754 memory_modified = true;
2755 }
2756}
2757
2758
2759/* Return true when INSN possibly modify memory contents of MEM
454ff5cb 2760 (i.e. address can be modified). */
7b52eede 2761bool
9678086d 2762memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
7b52eede
JH
2763{
2764 if (!INSN_P (insn))
2765 return false;
2766 memory_modified = false;
aa317c97 2767 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
7b52eede
JH
2768 return memory_modified;
2769}
2770
c13e8210
MM
2771/* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2772 array. */
2773
9ae8ffe7 2774void
4682ae04 2775init_alias_analysis (void)
9ae8ffe7 2776{
c582d54a 2777 unsigned int maxreg = max_reg_num ();
ea64ef27 2778 int changed, pass;
b3694847
SS
2779 int i;
2780 unsigned int ui;
2781 rtx insn;
9ae8ffe7 2782
0d446150
JH
2783 timevar_push (TV_ALIAS_ANALYSIS);
2784
bb1acb3e 2785 reg_known_value_size = maxreg - FIRST_PSEUDO_REGISTER;
a9429e29 2786 reg_known_value = ggc_alloc_cleared_vec_rtx (reg_known_value_size);
f883e0a7 2787 reg_known_equiv_p = XCNEWVEC (bool, reg_known_value_size);
9ae8ffe7 2788
08c79682 2789 /* If we have memory allocated from the previous run, use it. */
c582d54a 2790 if (old_reg_base_value)
08c79682
KH
2791 reg_base_value = old_reg_base_value;
2792
2793 if (reg_base_value)
2794 VEC_truncate (rtx, reg_base_value, 0);
2795
a590ac65 2796 VEC_safe_grow_cleared (rtx, gc, reg_base_value, maxreg);
ac606739 2797
5ed6ace5
MD
2798 new_reg_base_value = XNEWVEC (rtx, maxreg);
2799 reg_seen = XNEWVEC (char, maxreg);
ec907dd8
JL
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
6e73e666 2807 the optimization level or flag_expensive_optimizations.
ec907dd8
JL
2808
2809 We could propagate more information in the first pass by making use
6fb5fa3c 2810 of DF_REG_DEF_COUNT to determine immediately that the alias information
ea64ef27
JL
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. */
6e73e666 2819
ea64ef27 2820 pass = 0;
6e73e666 2821 do
ec907dd8
JL
2822 {
2823 /* Assume nothing will change this iteration of the loop. */
2824 changed = 0;
2825
ec907dd8 2826 /* We want to assign the same IDs each iteration of this loop, so
9fc37b2b
RS
2827 start counting from one each iteration of the loop. */
2828 unique_id = 1;
ec907dd8 2829
f5143c46 2830 /* We're at the start of the function each iteration through the
ec907dd8 2831 loop, so we're copying arguments. */
83bbd9b6 2832 copying_arguments = true;
9ae8ffe7 2833
6e73e666 2834 /* Wipe the potential alias information clean for this pass. */
c582d54a 2835 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
8072f69c 2836
6e73e666 2837 /* Wipe the reg_seen array clean. */
c582d54a 2838 memset (reg_seen, 0, maxreg);
9ae8ffe7 2839
6e73e666
JC
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.
8072f69c 2844
6e73e666
JC
2845 The address expression is VOIDmode for an argument and
2846 Pmode for other registers. */
2847
7f243674
JL
2848 memcpy (new_reg_base_value, static_reg_base_value,
2849 FIRST_PSEUDO_REGISTER * sizeof (rtx));
6e73e666 2850
ec907dd8
JL
2851 /* Walk the insns adding values to the new_reg_base_value array. */
2852 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9ae8ffe7 2853 {
2c3c49de 2854 if (INSN_P (insn))
ec907dd8 2855 {
6e73e666 2856 rtx note, set;
efc9bd41
RK
2857
2858#if defined (HAVE_prologue) || defined (HAVE_epilogue)
f5143c46 2859 /* The prologue/epilogue insns are not threaded onto the
657959ca
JL
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))
efc9bd41
RK
2865 continue;
2866#endif
2867
ec907dd8 2868 /* If this insn has a noalias note, process it, Otherwise,
c22cacf3
MS
2869 scan for sets. A simple set will have no side effects
2870 which could change the base value of any other register. */
6e73e666 2871
ec907dd8 2872 if (GET_CODE (PATTERN (insn)) == SET
efc9bd41
RK
2873 && REG_NOTES (insn) != 0
2874 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
84832317 2875 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
ec907dd8 2876 else
84832317 2877 note_stores (PATTERN (insn), record_set, NULL);
6e73e666
JC
2878
2879 set = single_set (insn);
2880
2881 if (set != 0
f8cfc6aa 2882 && REG_P (SET_DEST (set))
fb6754f0 2883 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
6e73e666 2884 {
fb6754f0 2885 unsigned int regno = REGNO (SET_DEST (set));
713f41f9 2886 rtx src = SET_SRC (set);
bb1acb3e 2887 rtx t;
713f41f9 2888
a31830a7
SB
2889 note = find_reg_equal_equiv_note (insn);
2890 if (note && REG_NOTE_KIND (note) == REG_EQUAL
6fb5fa3c 2891 && DF_REG_DEF_COUNT (regno) != 1)
a31830a7
SB
2892 note = NULL_RTX;
2893
2894 if (note != NULL_RTX
713f41f9 2895 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
bb2cf916 2896 && ! rtx_varies_p (XEXP (note, 0), 1)
bb1acb3e
RH
2897 && ! reg_overlap_mentioned_p (SET_DEST (set),
2898 XEXP (note, 0)))
713f41f9 2899 {
bb1acb3e
RH
2900 set_reg_known_value (regno, XEXP (note, 0));
2901 set_reg_known_equiv_p (regno,
2902 REG_NOTE_KIND (note) == REG_EQUIV);
713f41f9 2903 }
6fb5fa3c 2904 else if (DF_REG_DEF_COUNT (regno) == 1
713f41f9 2905 && GET_CODE (src) == PLUS
f8cfc6aa 2906 && REG_P (XEXP (src, 0))
bb1acb3e 2907 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
481683e1 2908 && CONST_INT_P (XEXP (src, 1)))
713f41f9 2909 {
bb1acb3e
RH
2910 t = plus_constant (t, INTVAL (XEXP (src, 1)));
2911 set_reg_known_value (regno, t);
2912 set_reg_known_equiv_p (regno, 0);
713f41f9 2913 }
6fb5fa3c 2914 else if (DF_REG_DEF_COUNT (regno) == 1
713f41f9
BS
2915 && ! rtx_varies_p (src, 1))
2916 {
bb1acb3e
RH
2917 set_reg_known_value (regno, src);
2918 set_reg_known_equiv_p (regno, 0);
713f41f9 2919 }
6e73e666 2920 }
ec907dd8 2921 }
4b4bf941 2922 else if (NOTE_P (insn)
a38e7aa5 2923 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
83bbd9b6 2924 copying_arguments = false;
6e73e666 2925 }
ec907dd8 2926
6e73e666 2927 /* Now propagate values from new_reg_base_value to reg_base_value. */
62e5bf5d 2928 gcc_assert (maxreg == (unsigned int) max_reg_num ());
c22cacf3 2929
c582d54a 2930 for (ui = 0; ui < maxreg; ui++)
6e73e666 2931 {
e51712db 2932 if (new_reg_base_value[ui]
08c79682 2933 && new_reg_base_value[ui] != VEC_index (rtx, reg_base_value, ui)
c582d54a 2934 && ! rtx_equal_p (new_reg_base_value[ui],
08c79682 2935 VEC_index (rtx, reg_base_value, ui)))
ec907dd8 2936 {
08c79682 2937 VEC_replace (rtx, reg_base_value, ui, new_reg_base_value[ui]);
6e73e666 2938 changed = 1;
ec907dd8 2939 }
9ae8ffe7 2940 }
9ae8ffe7 2941 }
6e73e666 2942 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
9ae8ffe7
JL
2943
2944 /* Fill in the remaining entries. */
bb1acb3e 2945 for (i = 0; i < (int)reg_known_value_size; i++)
9ae8ffe7 2946 if (reg_known_value[i] == 0)
bb1acb3e 2947 reg_known_value[i] = regno_reg_rtx[i + FIRST_PSEUDO_REGISTER];
9ae8ffe7 2948
e05e2395
MM
2949 /* Clean up. */
2950 free (new_reg_base_value);
ec907dd8 2951 new_reg_base_value = 0;
e05e2395 2952 free (reg_seen);
9ae8ffe7 2953 reg_seen = 0;
0d446150 2954 timevar_pop (TV_ALIAS_ANALYSIS);
9ae8ffe7
JL
2955}
2956
61630b27
JJ
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
9ae8ffe7 2966void
4682ae04 2967end_alias_analysis (void)
9ae8ffe7 2968{
c582d54a 2969 old_reg_base_value = reg_base_value;
bb1acb3e 2970 ggc_free (reg_known_value);
9ae8ffe7 2971 reg_known_value = 0;
ac606739 2972 reg_known_value_size = 0;
bb1acb3e 2973 free (reg_known_equiv_p);
e05e2395 2974 reg_known_equiv_p = 0;
9ae8ffe7 2975}
e2500fed
GK
2976
2977#include "gt-alias.h"