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