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