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