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