OSDN Git Service

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