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