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