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