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