OSDN Git Service

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