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