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