OSDN Git Service

* alias.c (get_alias_set): Don't call language-specific routine more
[pf3gnuchains/gcc-fork.git] / gcc / alias.c
1 /* Alias analysis for GNU C
2    Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3    Contributed by John Carr (jfc@mit.edu).
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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 "insn-flags.h"
29 #include "expr.h"
30 #include "regs.h"
31 #include "hard-reg-set.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
39 /* The alias sets assigned to MEMs assist the back-end in determining
40    which MEMs can alias which other MEMs.  In general, two MEMs in
41    different alias sets cannot alias each other, with one important
42    exception.  Consider something like:
43
44      struct S {int i; double d; };
45
46    a store to an `S' can alias something of either type `int' or type
47    `double'.  (However, a store to an `int' cannot alias a `double'
48    and vice versa.)  We indicate this via a tree structure that looks
49    like:
50            struct S
51             /   \
52            /     \
53          |/_     _\|
54          int    double
55
56    (The arrows are directed and point downwards.)
57     In this situation we say the alias set for `struct S' is the
58    `superset' and that those for `int' and `double' are `subsets'.
59
60    To see whether two alias sets can point to the same memory, we must
61    see if either alias set is a subset of the other. We need not trace
62    past immediate decendents, however, since we propagate all
63    grandchildren up one level.
64
65    Alias set zero is implicitly a superset of all other alias sets.
66    However, this is no actual entry for alias set zero.  It is an
67    error to attempt to explicitly construct a subset of zero.  */
68
69 typedef struct alias_set_entry
70 {
71   /* The alias set number, as stored in MEM_ALIAS_SET.  */
72   HOST_WIDE_INT alias_set;
73
74   /* The children of the alias set.  These are not just the immediate
75      children, but, in fact, all decendents.  So, if we have:
76
77        struct T { struct S s; float f; } 
78
79      continuing our example above, the children here will be all of
80      `int', `double', `float', and `struct S'.  */
81   splay_tree children;
82 } *alias_set_entry;
83
84 /* The language-specific function for alias analysis.  If NULL, the
85    language does not do any special alias analysis.  */
86 HOST_WIDE_INT (*lang_get_alias_set) PARAMS ((tree));
87
88 static int rtx_equal_for_memref_p       PARAMS ((rtx, rtx));
89 static rtx find_symbolic_term           PARAMS ((rtx));
90 static rtx get_addr                     PARAMS ((rtx));
91 static int memrefs_conflict_p           PARAMS ((int, rtx, int, rtx,
92                                                  HOST_WIDE_INT));
93 static void record_set                  PARAMS ((rtx, rtx, void *));
94 static rtx find_base_term               PARAMS ((rtx));
95 static int base_alias_check             PARAMS ((rtx, rtx, enum machine_mode,
96                                                  enum machine_mode));
97 static rtx find_base_value              PARAMS ((rtx));
98 static int mems_in_disjoint_alias_sets_p PARAMS ((rtx, rtx));
99 static int insert_subset_children       PARAMS ((splay_tree_node, void*));
100 static tree find_base_decl            PARAMS ((tree));
101 static alias_set_entry get_alias_set_entry PARAMS ((HOST_WIDE_INT));
102 static rtx fixed_scalar_and_varying_struct_p PARAMS ((rtx, rtx, rtx, rtx,
103                                                       int (*) (rtx)));
104 static int aliases_everything_p         PARAMS ((rtx));
105 static int write_dependence_p           PARAMS ((rtx, rtx, int));
106 static int nonlocal_reference_p         PARAMS ((rtx));
107
108 /* Set up all info needed to perform alias analysis on memory references.  */
109
110 /* Returns the size in bytes of the mode of X.  */
111 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
112
113 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
114    different alias sets.  We ignore alias sets in functions making use
115    of variable arguments because the va_arg macros on some systems are
116    not legal ANSI C.  */
117 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2)                      \
118   mems_in_disjoint_alias_sets_p (MEM1, MEM2)
119
120 /* Cap the number of passes we make over the insns propagating alias
121    information through set chains.   10 is a completely arbitrary choice.  */
122 #define MAX_ALIAS_LOOP_PASSES 10
123    
124 /* reg_base_value[N] gives an address to which register N is related.
125    If all sets after the first add or subtract to the current value
126    or otherwise modify it so it does not point to a different top level
127    object, reg_base_value[N] is equal to the address part of the source
128    of the first set.
129
130    A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF.  ADDRESS
131    expressions represent certain special values: function arguments and
132    the stack, frame, and argument pointers.  
133
134    The contents of an ADDRESS is not normally used, the mode of the
135    ADDRESS determines whether the ADDRESS is a function argument or some
136    other special value.  Pointer equality, not rtx_equal_p, determines whether
137    two ADDRESS expressions refer to the same base address.
138
139    The only use of the contents of an ADDRESS is for determining if the
140    current function performs nonlocal memory memory references for the
141    purposes of marking the function as a constant function.  */
142
143 static rtx *reg_base_value;
144 static rtx *new_reg_base_value;
145 static unsigned int reg_base_value_size; /* size of reg_base_value array */
146
147 #define REG_BASE_VALUE(X) \
148   (REGNO (X) < reg_base_value_size ? reg_base_value[REGNO (X)] : 0)
149
150 /* Vector of known invariant relationships between registers.  Set in
151    loop unrolling.  Indexed by register number, if nonzero the value
152    is an expression describing this register in terms of another.
153
154    The length of this array is REG_BASE_VALUE_SIZE.
155
156    Because this array contains only pseudo registers it has no effect
157    after reload.  */
158 static rtx *alias_invariant;
159
160 /* Vector indexed by N giving the initial (unchanging) value known for
161    pseudo-register N.  This array is initialized in
162    init_alias_analysis, and does not change until end_alias_analysis
163    is called.  */
164 rtx *reg_known_value;
165
166 /* Indicates number of valid entries in reg_known_value.  */
167 static unsigned int reg_known_value_size;
168
169 /* Vector recording for each reg_known_value whether it is due to a
170    REG_EQUIV note.  Future passes (viz., reload) may replace the
171    pseudo with the equivalent expression and so we account for the
172    dependences that would be introduced if that happens.
173
174    The REG_EQUIV notes created in assign_parms may mention the arg
175    pointer, and there are explicit insns in the RTL that modify the
176    arg pointer.  Thus we must ensure that such insns don't get
177    scheduled across each other because that would invalidate the
178    REG_EQUIV notes.  One could argue that the REG_EQUIV notes are
179    wrong, but solving the problem in the scheduler will likely give
180    better code, so we do it here.  */
181 char *reg_known_equiv_p;
182
183 /* True when scanning insns from the start of the rtl to the
184    NOTE_INSN_FUNCTION_BEG note.  */
185 static int copying_arguments;
186
187 /* The splay-tree used to store the various alias set entries.  */
188 static splay_tree alias_sets;
189 \f
190 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
191    such an entry, or NULL otherwise.  */
192
193 static alias_set_entry
194 get_alias_set_entry (alias_set)
195      HOST_WIDE_INT alias_set;
196 {
197   splay_tree_node sn
198     = splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
199
200   return sn != 0 ? ((alias_set_entry) sn->value) : 0;
201 }
202
203 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
204    the two MEMs cannot alias each other.  */
205
206 static int 
207 mems_in_disjoint_alias_sets_p (mem1, mem2)
208      rtx mem1;
209      rtx mem2;
210 {
211   alias_set_entry ase;
212
213 #ifdef ENABLE_CHECKING  
214 /* Perform a basic sanity check.  Namely, that there are no alias sets
215    if we're not using strict aliasing.  This helps to catch bugs
216    whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
217    where a MEM is allocated in some way other than by the use of
218    gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared.  If we begin to
219    use alias sets to indicate that spilled registers cannot alias each
220    other, we might need to remove this check.  */
221   if (! flag_strict_aliasing
222       && (MEM_ALIAS_SET (mem1) != 0 || MEM_ALIAS_SET (mem2) != 0))
223     abort ();
224 #endif
225
226   /* The code used in varargs macros are often not conforming ANSI C,
227      which can trick the compiler into making incorrect aliasing
228      assumptions in these functions.  So, we don't use alias sets in
229      such a function.  FIXME: This should be moved into the front-end;
230      it is a language-dependent notion, and there's no reason not to
231      still use these checks to handle globals.  */
232   if (current_function_stdarg || current_function_varargs)
233     return 0;
234
235   /* If have no alias set information for one of the MEMs, we have to assume
236      it can alias anything.  */
237   if (MEM_ALIAS_SET (mem1) == 0 || MEM_ALIAS_SET (mem2) == 0)
238     return 0;
239
240   /* If the two alias sets are the same, they may alias.  */
241   if (MEM_ALIAS_SET (mem1) == MEM_ALIAS_SET (mem2))
242     return 0;
243
244   /* See if the first alias set is a subset of the second.  */
245   ase = get_alias_set_entry (MEM_ALIAS_SET (mem1));
246   if (ase != 0 && splay_tree_lookup (ase->children,
247                                      (splay_tree_key) MEM_ALIAS_SET (mem2)))
248     return  0;
249
250   /* Now do the same, but with the alias sets reversed.  */
251   ase = get_alias_set_entry (MEM_ALIAS_SET (mem2));
252   if (ase != 0 && splay_tree_lookup (ase->children,
253                                      (splay_tree_key) MEM_ALIAS_SET (mem1)))
254     return  0;
255
256   /* The two MEMs are in distinct alias sets, and neither one is the
257      child of the other.  Therefore, they cannot alias.  */
258   return 1;
259 }
260
261 /* Insert the NODE into the splay tree given by DATA.  Used by
262    record_alias_subset via splay_tree_foreach.  */
263
264 static int
265 insert_subset_children (node, data)
266      splay_tree_node node;
267      void *data;
268 {
269   splay_tree_insert ((splay_tree) data, node->key, node->value);
270
271   return 0;
272 }
273 \f
274 /* T is an expression with pointer type.  Find the DECL on which this
275    expression is based.  (For example, in `a[i]' this would be `a'.)
276    If there is no such DECL, or a unique decl cannot be determined,
277    NULL_TREE is retured.  */
278
279 static tree
280 find_base_decl (t)
281      tree t;
282 {
283   tree d0, d1, d2;
284
285   if (t == 0 || t == error_mark_node || ! POINTER_TYPE_P (TREE_TYPE (t)))
286     return 0;
287
288   /* If this is a declaration, return it.  */
289   if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
290     return t;
291
292   /* Handle general expressions.  It would be nice to deal with
293      COMPONENT_REFs here.  If we could tell that `a' and `b' were the
294      same, then `a->f' and `b->f' are also the same.  */
295   switch (TREE_CODE_CLASS (TREE_CODE (t)))
296     {
297     case '1':
298       return find_base_decl (TREE_OPERAND (t, 0));
299
300     case '2':
301       /* Return 0 if found in neither or both are the same.  */
302       d0 = find_base_decl (TREE_OPERAND (t, 0));
303       d1 = find_base_decl (TREE_OPERAND (t, 1));
304       if (d0 == d1)
305         return d0;
306       else if (d0 == 0)
307         return d1;
308       else if (d1 == 0)
309         return d0;
310       else
311         return 0;
312
313     case '3':
314       d0 = find_base_decl (TREE_OPERAND (t, 0));
315       d1 = find_base_decl (TREE_OPERAND (t, 1));
316       d0 = find_base_decl (TREE_OPERAND (t, 0));
317       d2 = find_base_decl (TREE_OPERAND (t, 2));
318
319       /* Set any nonzero values from the last, then from the first.  */
320       if (d1 == 0) d1 = d2;
321       if (d0 == 0) d0 = d1;
322       if (d1 == 0) d1 = d0;
323       if (d2 == 0) d2 = d1;
324
325       /* At this point all are nonzero or all are zero.  If all three are the
326          same, return it.  Otherwise, return zero.  */
327       return (d0 == d1 && d1 == d2) ? d0 : 0;
328
329     default:
330       return 0;
331     }
332 }
333
334 /* Return the alias set for T, which may be either a type or an
335    expression.  Call language-specific routine for help, if needed.  */
336
337 HOST_WIDE_INT
338 get_alias_set (t)
339      tree t;
340 {
341   tree orig_t;
342   HOST_WIDE_INT set;
343   HOST_WIDE_INT bitsize, bitpos;
344   tree offset;
345   enum machine_mode mode;
346   int volatilep, unsignedp;
347   unsigned int alignment;
348
349   /* If we're not doing any alias analysis, just assume everything
350      aliases everything else.  Also return 0 if this or its type is
351      an error.  */
352   if (! flag_strict_aliasing || t == error_mark_node
353       || (! TYPE_P (t)
354           && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
355     return 0;
356
357   /* We can be passed either an expression or a type.  This and the
358      language-specific routine may make mutually-recursive calls to
359      each other to figure out what to do.  At each juncture, we see if
360      this is a tree that the language may need to handle specially.
361      First handle things that aren't types and start by removing nops
362      since we care only about the actual object.  */
363   if (! TYPE_P (t))
364     {
365       while (TREE_CODE (t) == NOP_EXPR || TREE_CODE (t) == CONVERT_EXPR
366              || TREE_CODE (t) == NON_LVALUE_EXPR)
367         t = TREE_OPERAND (t, 0);
368
369       /* Now give the language a chance to do something but record what we
370          gave it this time.  */
371       orig_t = t;
372       if (lang_get_alias_set != 0
373           && (set = (*lang_get_alias_set) (t)) != -1)
374         return set;
375
376       /* If this is a reference, go inside it and use the underlying
377          object.  */
378       if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r')
379         t = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
380                                  &unsignedp, &volatilep, &alignment);
381
382       if (TREE_CODE (t) == INDIRECT_REF)
383         {
384           /* Check for accesses through restrict-qualified pointers.  */
385           tree decl = find_base_decl (TREE_OPERAND (t, 0));
386
387           if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
388             /* We use the alias set indicated in the declaration.  */
389             return DECL_POINTER_ALIAS_SET (decl);
390
391           /* If we have an INDIRECT_REF via a void pointer, we don't
392              know anything about what that might alias.  */
393           if (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE)
394             return 0;
395         }
396
397       /* Give the language another chance to do something special.  */
398       if (orig_t != t && lang_get_alias_set != 0
399           && (set = (*lang_get_alias_set) (t)) != -1)
400         return set;
401
402       /* Now all we care about is the type.  */
403       t = TREE_TYPE (t);
404     }
405
406   /* Variant qualifiers don't affect the alias set, so get the main
407      variant. If this is a type with a known alias set, return it.  */
408   t = TYPE_MAIN_VARIANT (t);
409   if (TYPE_P (t) && TYPE_ALIAS_SET_KNOWN_P (t))
410     return TYPE_ALIAS_SET (t);
411
412   /* See if the language has special handling for this type.  */
413   if (lang_get_alias_set != 0
414       && (set = (*lang_get_alias_set) (t)) != -1)
415     ;
416   /* There are no objects of FUNCTION_TYPE, so there's no point in
417      using up an alias set for them.  (There are, of course, pointers
418      and references to functions, but that's different.)  */
419   else if (TREE_CODE (t) == FUNCTION_TYPE)
420     set = 0;
421   else
422     /* Otherwise make a new alias set for this type.  */
423     set = new_alias_set ();
424
425   TYPE_ALIAS_SET (t) = set;
426   return set;
427 }
428
429 /* Return a brand-new alias set.  */
430
431 HOST_WIDE_INT
432 new_alias_set ()
433 {
434   static HOST_WIDE_INT last_alias_set;
435
436   if (flag_strict_aliasing)
437     return ++last_alias_set;
438   else
439     return 0;
440 }
441
442 /* Indicate that things in SUBSET can alias things in SUPERSET, but
443    not vice versa.  For example, in C, a store to an `int' can alias a
444    structure containing an `int', but not vice versa.  Here, the
445    structure would be the SUPERSET and `int' the SUBSET.  This
446    function should be called only once per SUPERSET/SUBSET pair. 
447
448    It is illegal for SUPERSET to be zero; everything is implicitly a
449    subset of alias set zero.  */
450
451 void
452 record_alias_subset (superset, subset)
453      HOST_WIDE_INT superset;
454      HOST_WIDE_INT subset;
455 {
456   alias_set_entry superset_entry;
457   alias_set_entry subset_entry;
458
459   if (superset == 0)
460     abort ();
461
462   superset_entry = get_alias_set_entry (superset);
463   if (superset_entry == 0) 
464     {
465       /* Create an entry for the SUPERSET, so that we have a place to
466          attach the SUBSET.  */
467       superset_entry
468         = (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
469       superset_entry->alias_set = superset;
470       superset_entry->children 
471         = splay_tree_new (splay_tree_compare_ints, 0, 0);
472       splay_tree_insert (alias_sets, (splay_tree_key) superset,
473                          (splay_tree_value) superset_entry);
474
475     }
476
477   subset_entry = get_alias_set_entry (subset);
478
479   /* If there is an entry for the subset, enter all of its children
480      (if they are not already present) as children of the SUPERSET.  */
481   if (subset_entry) 
482     splay_tree_foreach (subset_entry->children,
483                         insert_subset_children,
484                         superset_entry->children);
485
486   /* Enter the SUBSET itself as a child of the SUPERSET.  */
487   splay_tree_insert (superset_entry->children, 
488                      (splay_tree_key) subset, 0);
489 }
490
491 /* Record that component types of TYPE, if any, are part of that type for
492    aliasing purposes.  For record types, we only record component types
493    for fields that are marked addressable.  For array types, we always
494    record the component types, so the front end should not call this
495    function if the individual component aren't addressable.  */
496
497 void
498 record_component_aliases (type)
499      tree type;
500 {
501   HOST_WIDE_INT superset = get_alias_set (type);
502   HOST_WIDE_INT subset;
503   tree field;
504
505   if (superset == 0)
506     return;
507
508   switch (TREE_CODE (type))
509     {
510     case ARRAY_TYPE:
511       subset = get_alias_set (TREE_TYPE (type));
512       if (subset != 0)
513         record_alias_subset (superset, subset);
514       break;
515
516     case RECORD_TYPE:
517     case UNION_TYPE:
518     case QUAL_UNION_TYPE:
519       for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
520         {
521           subset = get_alias_set (TREE_TYPE (field));
522           if (TREE_ADDRESSABLE (field) && subset != 0 && subset != superset)
523             record_alias_subset (superset, subset);
524         }
525       break;
526
527     default:
528       break;
529     }
530 }
531
532 /* Allocate an alias set for use in storing and reading from the varargs
533    spill area.  */
534
535 HOST_WIDE_INT
536 get_varargs_alias_set ()
537 {
538   static HOST_WIDE_INT set = -1;
539
540   if (set == -1)
541     set = new_alias_set ();
542
543   return set;
544 }
545
546 /* Likewise, but used for the fixed portions of the frame, e.g., register
547    save areas.  */
548
549 HOST_WIDE_INT
550 get_frame_alias_set ()
551 {
552   static HOST_WIDE_INT set = -1;
553
554   if (set == -1)
555     set = new_alias_set ();
556
557   return set;
558 }
559
560 /* Inside SRC, the source of a SET, find a base address.  */
561
562 static rtx
563 find_base_value (src)
564      register rtx src;
565 {
566   switch (GET_CODE (src))
567     {
568     case SYMBOL_REF:
569     case LABEL_REF:
570       return src;
571
572     case REG:
573       /* At the start of a function, argument registers have known base
574          values which may be lost later.  Returning an ADDRESS
575          expression here allows optimization based on argument values
576          even when the argument registers are used for other purposes.  */
577       if (REGNO (src) < FIRST_PSEUDO_REGISTER && copying_arguments)
578         return new_reg_base_value[REGNO (src)];
579
580       /* If a pseudo has a known base value, return it.  Do not do this
581          for hard regs since it can result in a circular dependency
582          chain for registers which have values at function entry.
583
584          The test above is not sufficient because the scheduler may move
585          a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN.  */
586       if (REGNO (src) >= FIRST_PSEUDO_REGISTER
587           && (unsigned) REGNO (src) < reg_base_value_size
588           && reg_base_value[REGNO (src)])
589         return reg_base_value[REGNO (src)];
590
591       return src;
592
593     case MEM:
594       /* Check for an argument passed in memory.  Only record in the
595          copying-arguments block; it is too hard to track changes
596          otherwise.  */
597       if (copying_arguments
598           && (XEXP (src, 0) == arg_pointer_rtx
599               || (GET_CODE (XEXP (src, 0)) == PLUS
600                   && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
601         return gen_rtx_ADDRESS (VOIDmode, src);
602       return 0;
603
604     case CONST:
605       src = XEXP (src, 0);
606       if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
607         break;
608
609       /* ... fall through ... */
610
611     case PLUS:
612     case MINUS:
613       {
614         rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
615
616         /* If either operand is a REG, then see if we already have
617            a known value for it.  */
618         if (GET_CODE (src_0) == REG)
619           {
620             temp = find_base_value (src_0);
621             if (temp != 0)
622               src_0 = temp;
623           }
624
625         if (GET_CODE (src_1) == REG)
626           {
627             temp = find_base_value (src_1);
628             if (temp!= 0)
629               src_1 = temp;
630           }
631
632         /* Guess which operand is the base address:
633            If either operand is a symbol, then it is the base.  If
634            either operand is a CONST_INT, then the other is the base.  */
635         if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
636           return find_base_value (src_0);
637         else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
638           return find_base_value (src_1);
639
640         /* This might not be necessary anymore:
641            If either operand is a REG that is a known pointer, then it
642            is the base.  */
643         else if (GET_CODE (src_0) == REG && REGNO_POINTER_FLAG (REGNO (src_0)))
644           return find_base_value (src_0);
645         else if (GET_CODE (src_1) == REG && REGNO_POINTER_FLAG (REGNO (src_1)))
646           return find_base_value (src_1);
647
648         return 0;
649       }
650
651     case LO_SUM:
652       /* The standard form is (lo_sum reg sym) so look only at the
653          second operand.  */
654       return find_base_value (XEXP (src, 1));
655
656     case AND:
657       /* If the second operand is constant set the base
658          address to the first operand. */
659       if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
660         return find_base_value (XEXP (src, 0));
661       return 0;
662
663     case ZERO_EXTEND:
664     case SIGN_EXTEND:   /* used for NT/Alpha pointers */
665     case HIGH:
666       return find_base_value (XEXP (src, 0));
667
668     default:
669       break;
670     }
671
672   return 0;
673 }
674
675 /* Called from init_alias_analysis indirectly through note_stores.  */
676
677 /* While scanning insns to find base values, reg_seen[N] is nonzero if
678    register N has been set in this function.  */
679 static char *reg_seen;
680
681 /* Addresses which are known not to alias anything else are identified
682    by a unique integer.  */
683 static int unique_id;
684
685 static void
686 record_set (dest, set, data)
687      rtx dest, set;
688      void *data ATTRIBUTE_UNUSED;
689 {
690   register unsigned regno;
691   rtx src;
692
693   if (GET_CODE (dest) != REG)
694     return;
695
696   regno = REGNO (dest);
697
698   if (regno >= reg_base_value_size)
699     abort ();
700
701   if (set)
702     {
703       /* A CLOBBER wipes out any old value but does not prevent a previously
704          unset register from acquiring a base address (i.e. reg_seen is not
705          set).  */
706       if (GET_CODE (set) == CLOBBER)
707         {
708           new_reg_base_value[regno] = 0;
709           return;
710         }
711       src = SET_SRC (set);
712     }
713   else
714     {
715       if (reg_seen[regno])
716         {
717           new_reg_base_value[regno] = 0;
718           return;
719         }
720       reg_seen[regno] = 1;
721       new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
722                                                    GEN_INT (unique_id++));
723       return;
724     }
725
726   /* This is not the first set.  If the new value is not related to the
727      old value, forget the base value. Note that the following code is
728      not detected:
729      extern int x, y;  int *p = &x; p += (&y-&x);
730      ANSI C does not allow computing the difference of addresses
731      of distinct top level objects.  */
732   if (new_reg_base_value[regno])
733     switch (GET_CODE (src))
734       {
735       case LO_SUM:
736       case PLUS:
737       case MINUS:
738         if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
739           new_reg_base_value[regno] = 0;
740         break;
741       case AND:
742         if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
743           new_reg_base_value[regno] = 0;
744         break;
745       default:
746         new_reg_base_value[regno] = 0;
747         break;
748       }
749   /* If this is the first set of a register, record the value.  */
750   else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
751            && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
752     new_reg_base_value[regno] = find_base_value (src);
753
754   reg_seen[regno] = 1;
755 }
756
757 /* Called from loop optimization when a new pseudo-register is
758    created.  It indicates that REGNO is being set to VAL.  f INVARIANT
759    is true then this value also describes an invariant relationship
760    which can be used to deduce that two registers with unknown values
761    are different.  */
762
763 void
764 record_base_value (regno, val, invariant)
765      unsigned int regno;
766      rtx val;
767      int invariant;
768 {
769   if (regno >= reg_base_value_size)
770     return;
771
772   if (invariant && alias_invariant)
773     alias_invariant[regno] = val;
774
775   if (GET_CODE (val) == REG)
776     {
777       if (REGNO (val) < reg_base_value_size)
778         reg_base_value[regno] = reg_base_value[REGNO (val)];
779
780       return;
781     }
782
783   reg_base_value[regno] = find_base_value (val);
784 }
785
786 /* Returns a canonical version of X, from the point of view alias
787    analysis.  (For example, if X is a MEM whose address is a register,
788    and the register has a known value (say a SYMBOL_REF), then a MEM
789    whose address is the SYMBOL_REF is returned.)  */
790
791 rtx
792 canon_rtx (x)
793      rtx x;
794 {
795   /* Recursively look for equivalences.  */
796   if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
797       && REGNO (x) < reg_known_value_size)
798     return reg_known_value[REGNO (x)] == x
799       ? x : canon_rtx (reg_known_value[REGNO (x)]);
800   else if (GET_CODE (x) == PLUS)
801     {
802       rtx x0 = canon_rtx (XEXP (x, 0));
803       rtx x1 = canon_rtx (XEXP (x, 1));
804
805       if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
806         {
807           /* We can tolerate LO_SUMs being offset here; these
808              rtl are used for nothing other than comparisons.  */
809           if (GET_CODE (x0) == CONST_INT)
810             return plus_constant_for_output (x1, INTVAL (x0));
811           else if (GET_CODE (x1) == CONST_INT)
812             return plus_constant_for_output (x0, INTVAL (x1));
813           return gen_rtx_PLUS (GET_MODE (x), x0, x1);
814         }
815     }
816
817   /* This gives us much better alias analysis when called from
818      the loop optimizer.   Note we want to leave the original
819      MEM alone, but need to return the canonicalized MEM with
820      all the flags with their original values.  */
821   else if (GET_CODE (x) == MEM)
822     {
823       rtx addr = canon_rtx (XEXP (x, 0));
824
825       if (addr != XEXP (x, 0))
826         {
827           rtx new = gen_rtx_MEM (GET_MODE (x), addr);
828
829           MEM_COPY_ATTRIBUTES (new, x);
830           x = new;
831         }
832     }
833   return x;
834 }
835
836 /* Return 1 if X and Y are identical-looking rtx's.
837
838    We use the data in reg_known_value above to see if two registers with
839    different numbers are, in fact, equivalent.  */
840
841 static int
842 rtx_equal_for_memref_p (x, y)
843      rtx x, y;
844 {
845   register int i;
846   register int j;
847   register enum rtx_code code;
848   register const char *fmt;
849
850   if (x == 0 && y == 0)
851     return 1;
852   if (x == 0 || y == 0)
853     return 0;
854
855   x = canon_rtx (x);
856   y = canon_rtx (y);
857
858   if (x == y)
859     return 1;
860
861   code = GET_CODE (x);
862   /* Rtx's of different codes cannot be equal.  */
863   if (code != GET_CODE (y))
864     return 0;
865
866   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
867      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
868
869   if (GET_MODE (x) != GET_MODE (y))
870     return 0;
871
872   /* Some RTL can be compared without a recursive examination.  */
873   switch (code)
874     {
875     case REG:
876       return REGNO (x) == REGNO (y);
877
878     case LABEL_REF:
879       return XEXP (x, 0) == XEXP (y, 0);
880       
881     case SYMBOL_REF:
882       return XSTR (x, 0) == XSTR (y, 0);
883
884     case CONST_INT:
885     case CONST_DOUBLE:
886       /* There's no need to compare the contents of CONST_DOUBLEs or
887          CONST_INTs because pointer equality is a good enough
888          comparison for these nodes.  */
889       return 0;
890
891     case ADDRESSOF:
892       return (REGNO (XEXP (x, 0)) == REGNO (XEXP (y, 0))
893               && XINT (x, 1) == XINT (y, 1));
894
895     default:
896       break;
897     }
898
899   /* For commutative operations, the RTX match if the operand match in any
900      order.  Also handle the simple binary and unary cases without a loop.  */
901   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
902     return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
903              && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
904             || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
905                 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
906   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
907     return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
908             && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
909   else if (GET_RTX_CLASS (code) == '1')
910     return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
911
912   /* Compare the elements.  If any pair of corresponding elements
913      fail to match, return 0 for the whole things.
914
915      Limit cases to types which actually appear in addresses.  */
916
917   fmt = GET_RTX_FORMAT (code);
918   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
919     {
920       switch (fmt[i])
921         {
922         case 'i':
923           if (XINT (x, i) != XINT (y, i))
924             return 0;
925           break;
926
927         case 'E':
928           /* Two vectors must have the same length.  */
929           if (XVECLEN (x, i) != XVECLEN (y, i))
930             return 0;
931
932           /* And the corresponding elements must match.  */
933           for (j = 0; j < XVECLEN (x, i); j++)
934             if (rtx_equal_for_memref_p (XVECEXP (x, i, j),
935                                         XVECEXP (y, i, j)) == 0)
936               return 0;
937           break;
938
939         case 'e':
940           if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
941             return 0;
942           break;
943
944         /* This can happen for an asm which clobbers memory.  */
945         case '0':
946           break;
947
948           /* It is believed that rtx's at this level will never
949              contain anything but integers and other rtx's,
950              except for within LABEL_REFs and SYMBOL_REFs.  */
951         default:
952           abort ();
953         }
954     }
955   return 1;
956 }
957
958 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
959    X and return it, or return 0 if none found.  */
960
961 static rtx
962 find_symbolic_term (x)
963      rtx x;
964 {
965   register int i;
966   register enum rtx_code code;
967   register const char *fmt;
968
969   code = GET_CODE (x);
970   if (code == SYMBOL_REF || code == LABEL_REF)
971     return x;
972   if (GET_RTX_CLASS (code) == 'o')
973     return 0;
974
975   fmt = GET_RTX_FORMAT (code);
976   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
977     {
978       rtx t;
979
980       if (fmt[i] == 'e')
981         {
982           t = find_symbolic_term (XEXP (x, i));
983           if (t != 0)
984             return t;
985         }
986       else if (fmt[i] == 'E')
987         break;
988     }
989   return 0;
990 }
991
992 static rtx
993 find_base_term (x)
994      register rtx x;
995 {
996   cselib_val *val;
997   struct elt_loc_list *l;
998
999   switch (GET_CODE (x))
1000     {
1001     case REG:
1002       return REG_BASE_VALUE (x);
1003
1004     case ZERO_EXTEND:
1005     case SIGN_EXTEND:   /* Used for Alpha/NT pointers */
1006     case HIGH:
1007     case PRE_INC:
1008     case PRE_DEC:
1009     case POST_INC:
1010     case POST_DEC:
1011       return find_base_term (XEXP (x, 0));
1012
1013     case VALUE:
1014       val = CSELIB_VAL_PTR (x);
1015       for (l = val->locs; l; l = l->next)
1016         if ((x = find_base_term (l->loc)) != 0)
1017           return x;
1018       return 0;
1019
1020     case CONST:
1021       x = XEXP (x, 0);
1022       if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1023         return 0;
1024       /* fall through */
1025     case LO_SUM:
1026     case PLUS:
1027     case MINUS:
1028       {
1029         rtx tmp1 = XEXP (x, 0);
1030         rtx tmp2 = XEXP (x, 1);
1031
1032         /* This is a litle bit tricky since we have to determine which of
1033            the two operands represents the real base address.  Otherwise this
1034            routine may return the index register instead of the base register.
1035
1036            That may cause us to believe no aliasing was possible, when in
1037            fact aliasing is possible.
1038
1039            We use a few simple tests to guess the base register.  Additional
1040            tests can certainly be added.  For example, if one of the operands
1041            is a shift or multiply, then it must be the index register and the
1042            other operand is the base register.  */
1043         
1044         /* If either operand is known to be a pointer, then use it
1045            to determine the base term.  */
1046         if (REG_P (tmp1) && REGNO_POINTER_FLAG (REGNO (tmp1)))
1047           return find_base_term (tmp1);
1048
1049         if (REG_P (tmp2) && REGNO_POINTER_FLAG (REGNO (tmp2)))
1050           return find_base_term (tmp2);
1051
1052         /* Neither operand was known to be a pointer.  Go ahead and find the
1053            base term for both operands.  */
1054         tmp1 = find_base_term (tmp1);
1055         tmp2 = find_base_term (tmp2);
1056
1057         /* If either base term is named object or a special address
1058            (like an argument or stack reference), then use it for the
1059            base term.  */
1060         if (tmp1 != 0
1061             && (GET_CODE (tmp1) == SYMBOL_REF
1062                 || GET_CODE (tmp1) == LABEL_REF
1063                 || (GET_CODE (tmp1) == ADDRESS
1064                     && GET_MODE (tmp1) != VOIDmode)))
1065           return tmp1;
1066
1067         if (tmp2 != 0
1068             && (GET_CODE (tmp2) == SYMBOL_REF
1069                 || GET_CODE (tmp2) == LABEL_REF
1070                 || (GET_CODE (tmp2) == ADDRESS
1071                     && GET_MODE (tmp2) != VOIDmode)))
1072           return tmp2;
1073
1074         /* We could not determine which of the two operands was the
1075            base register and which was the index.  So we can determine
1076            nothing from the base alias check.  */
1077         return 0;
1078       }
1079
1080     case AND:
1081       if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
1082         return REG_BASE_VALUE (XEXP (x, 0));
1083       return 0;
1084
1085     case SYMBOL_REF:
1086     case LABEL_REF:
1087       return x;
1088
1089     default:
1090       return 0;
1091     }
1092 }
1093
1094 /* Return 0 if the addresses X and Y are known to point to different
1095    objects, 1 if they might be pointers to the same object.  */
1096
1097 static int
1098 base_alias_check (x, y, x_mode, y_mode)
1099      rtx x, y;
1100      enum machine_mode x_mode, y_mode;
1101 {
1102   rtx x_base = find_base_term (x);
1103   rtx y_base = find_base_term (y);
1104
1105   /* If the address itself has no known base see if a known equivalent
1106      value has one.  If either address still has no known base, nothing
1107      is known about aliasing.  */
1108   if (x_base == 0)
1109     {
1110       rtx x_c;
1111
1112       if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1113         return 1;
1114
1115       x_base = find_base_term (x_c);
1116       if (x_base == 0)
1117         return 1;
1118     }
1119
1120   if (y_base == 0)
1121     {
1122       rtx y_c;
1123       if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1124         return 1;
1125
1126       y_base = find_base_term (y_c);
1127       if (y_base == 0)
1128         return 1;
1129     }
1130
1131   /* If the base addresses are equal nothing is known about aliasing.  */
1132   if (rtx_equal_p (x_base, y_base))
1133     return 1;
1134
1135   /* The base addresses of the read and write are different expressions. 
1136      If they are both symbols and they are not accessed via AND, there is
1137      no conflict.  We can bring knowledge of object alignment into play
1138      here.  For example, on alpha, "char a, b;" can alias one another,
1139      though "char a; long b;" cannot.  */
1140   if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1141     {
1142       if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1143         return 1;
1144       if (GET_CODE (x) == AND
1145           && (GET_CODE (XEXP (x, 1)) != CONST_INT
1146               || GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1147         return 1;
1148       if (GET_CODE (y) == AND
1149           && (GET_CODE (XEXP (y, 1)) != CONST_INT
1150               || GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1151         return 1;
1152       /* Differing symbols never alias.  */
1153       return 0;
1154     }
1155
1156   /* If one address is a stack reference there can be no alias:
1157      stack references using different base registers do not alias,
1158      a stack reference can not alias a parameter, and a stack reference
1159      can not alias a global.  */
1160   if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1161       || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1162     return 0;
1163
1164   if (! flag_argument_noalias)
1165     return 1;
1166
1167   if (flag_argument_noalias > 1)
1168     return 0;
1169
1170   /* Weak noalias assertion (arguments are distinct, but may match globals). */
1171   return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1172 }
1173
1174 /* Convert the address X into something we can use.  This is done by returning
1175    it unchanged unless it is a value; in the latter case we call cselib to get
1176    a more useful rtx.  */
1177
1178 static rtx
1179 get_addr (x)
1180      rtx x;
1181 {
1182   cselib_val *v;
1183   struct elt_loc_list *l;
1184
1185   if (GET_CODE (x) != VALUE)
1186     return x;
1187   v = CSELIB_VAL_PTR (x);
1188   for (l = v->locs; l; l = l->next)
1189     if (CONSTANT_P (l->loc))
1190       return l->loc;
1191   for (l = v->locs; l; l = l->next)
1192     if (GET_CODE (l->loc) != REG && GET_CODE (l->loc) != MEM)
1193       return l->loc;
1194   if (v->locs)
1195     return v->locs->loc;
1196   return x;
1197 }
1198
1199 /*  Return the address of the (N_REFS + 1)th memory reference to ADDR
1200     where SIZE is the size in bytes of the memory reference.  If ADDR
1201     is not modified by the memory reference then ADDR is returned.  */
1202
1203 rtx
1204 addr_side_effect_eval (addr, size, n_refs)
1205      rtx addr;
1206      int size;
1207      int n_refs;
1208 {
1209   int offset = 0;
1210   
1211   switch (GET_CODE (addr))
1212     {
1213     case PRE_INC:
1214       offset = (n_refs + 1) * size;
1215       break;
1216     case PRE_DEC:
1217       offset = -(n_refs + 1) * size;
1218       break;
1219     case POST_INC:
1220       offset = n_refs * size;
1221       break;
1222     case POST_DEC:
1223       offset = -n_refs * size;
1224       break;
1225
1226     default:
1227       return addr;
1228     }
1229   
1230   if (offset)
1231     addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
1232   else
1233     addr = XEXP (addr, 0);
1234
1235   return addr;
1236 }
1237
1238 /* Return nonzero if X and Y (memory addresses) could reference the
1239    same location in memory.  C is an offset accumulator.  When
1240    C is nonzero, we are testing aliases between X and Y + C.
1241    XSIZE is the size in bytes of the X reference,
1242    similarly YSIZE is the size in bytes for Y.
1243
1244    If XSIZE or YSIZE is zero, we do not know the amount of memory being
1245    referenced (the reference was BLKmode), so make the most pessimistic
1246    assumptions.
1247
1248    If XSIZE or YSIZE is negative, we may access memory outside the object
1249    being referenced as a side effect.  This can happen when using AND to
1250    align memory references, as is done on the Alpha.
1251
1252    Nice to notice that varying addresses cannot conflict with fp if no
1253    local variables had their addresses taken, but that's too hard now.  */
1254
1255 static int
1256 memrefs_conflict_p (xsize, x, ysize, y, c)
1257      register rtx x, y;
1258      int xsize, ysize;
1259      HOST_WIDE_INT c;
1260 {
1261   if (GET_CODE (x) == VALUE)
1262     x = get_addr (x);
1263   if (GET_CODE (y) == VALUE)
1264     y = get_addr (y);
1265   if (GET_CODE (x) == HIGH)
1266     x = XEXP (x, 0);
1267   else if (GET_CODE (x) == LO_SUM)
1268     x = XEXP (x, 1);
1269   else
1270     x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
1271   if (GET_CODE (y) == HIGH)
1272     y = XEXP (y, 0);
1273   else if (GET_CODE (y) == LO_SUM)
1274     y = XEXP (y, 1);
1275   else
1276     y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
1277
1278   if (rtx_equal_for_memref_p (x, y))
1279     {
1280       if (xsize <= 0 || ysize <= 0)
1281         return 1;
1282       if (c >= 0 && xsize > c)
1283         return 1;
1284       if (c < 0 && ysize+c > 0)
1285         return 1;
1286       return 0;
1287     }
1288
1289   /* This code used to check for conflicts involving stack references and
1290      globals but the base address alias code now handles these cases.  */
1291
1292   if (GET_CODE (x) == PLUS)
1293     {
1294       /* The fact that X is canonicalized means that this
1295          PLUS rtx is canonicalized.  */
1296       rtx x0 = XEXP (x, 0);
1297       rtx x1 = XEXP (x, 1);
1298
1299       if (GET_CODE (y) == PLUS)
1300         {
1301           /* The fact that Y is canonicalized means that this
1302              PLUS rtx is canonicalized.  */
1303           rtx y0 = XEXP (y, 0);
1304           rtx y1 = XEXP (y, 1);
1305
1306           if (rtx_equal_for_memref_p (x1, y1))
1307             return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1308           if (rtx_equal_for_memref_p (x0, y0))
1309             return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1310           if (GET_CODE (x1) == CONST_INT)
1311             {
1312               if (GET_CODE (y1) == CONST_INT)
1313                 return memrefs_conflict_p (xsize, x0, ysize, y0,
1314                                            c - INTVAL (x1) + INTVAL (y1));
1315               else
1316                 return memrefs_conflict_p (xsize, x0, ysize, y,
1317                                            c - INTVAL (x1));
1318             }
1319           else if (GET_CODE (y1) == CONST_INT)
1320             return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1321
1322           return 1;
1323         }
1324       else if (GET_CODE (x1) == CONST_INT)
1325         return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1326     }
1327   else if (GET_CODE (y) == PLUS)
1328     {
1329       /* The fact that Y is canonicalized means that this
1330          PLUS rtx is canonicalized.  */
1331       rtx y0 = XEXP (y, 0);
1332       rtx y1 = XEXP (y, 1);
1333
1334       if (GET_CODE (y1) == CONST_INT)
1335         return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1336       else
1337         return 1;
1338     }
1339
1340   if (GET_CODE (x) == GET_CODE (y))
1341     switch (GET_CODE (x))
1342       {
1343       case MULT:
1344         {
1345           /* Handle cases where we expect the second operands to be the
1346              same, and check only whether the first operand would conflict
1347              or not.  */
1348           rtx x0, y0;
1349           rtx x1 = canon_rtx (XEXP (x, 1));
1350           rtx y1 = canon_rtx (XEXP (y, 1));
1351           if (! rtx_equal_for_memref_p (x1, y1))
1352             return 1;
1353           x0 = canon_rtx (XEXP (x, 0));
1354           y0 = canon_rtx (XEXP (y, 0));
1355           if (rtx_equal_for_memref_p (x0, y0))
1356             return (xsize == 0 || ysize == 0
1357                     || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1358
1359           /* Can't properly adjust our sizes.  */
1360           if (GET_CODE (x1) != CONST_INT)
1361             return 1;
1362           xsize /= INTVAL (x1);
1363           ysize /= INTVAL (x1);
1364           c /= INTVAL (x1);
1365           return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1366         }
1367
1368       case REG:
1369         /* Are these registers known not to be equal?  */
1370         if (alias_invariant)
1371           {
1372             unsigned int r_x = REGNO (x), r_y = REGNO (y);
1373             rtx i_x, i_y;       /* invariant relationships of X and Y */
1374
1375             i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1376             i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1377
1378             if (i_x == 0 && i_y == 0)
1379               break;
1380
1381             if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1382                                       ysize, i_y ? i_y : y, c))
1383               return 0;
1384           }
1385         break;
1386
1387       default:
1388         break;
1389       }
1390
1391   /* Treat an access through an AND (e.g. a subword access on an Alpha)
1392      as an access with indeterminate size.  Assume that references 
1393      besides AND are aligned, so if the size of the other reference is
1394      at least as large as the alignment, assume no other overlap.  */
1395   if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1396     {
1397       if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1398         xsize = -1;
1399       return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1400     }
1401   if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1402     {
1403       /* ??? If we are indexing far enough into the array/structure, we
1404          may yet be able to determine that we can not overlap.  But we 
1405          also need to that we are far enough from the end not to overlap
1406          a following reference, so we do nothing with that for now.  */
1407       if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1408         ysize = -1;
1409       return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1410     }
1411
1412   if (CONSTANT_P (x))
1413     {
1414       if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1415         {
1416           c += (INTVAL (y) - INTVAL (x));
1417           return (xsize <= 0 || ysize <= 0
1418                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1419         }
1420
1421       if (GET_CODE (x) == CONST)
1422         {
1423           if (GET_CODE (y) == CONST)
1424             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1425                                        ysize, canon_rtx (XEXP (y, 0)), c);
1426           else
1427             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1428                                        ysize, y, c);
1429         }
1430       if (GET_CODE (y) == CONST)
1431         return memrefs_conflict_p (xsize, x, ysize,
1432                                    canon_rtx (XEXP (y, 0)), c);
1433
1434       if (CONSTANT_P (y))
1435         return (xsize < 0 || ysize < 0
1436                 || (rtx_equal_for_memref_p (x, y)
1437                     && (xsize == 0 || ysize == 0
1438                         || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1439
1440       return 1;
1441     }
1442   return 1;
1443 }
1444
1445 /* Functions to compute memory dependencies.
1446
1447    Since we process the insns in execution order, we can build tables
1448    to keep track of what registers are fixed (and not aliased), what registers
1449    are varying in known ways, and what registers are varying in unknown
1450    ways.
1451
1452    If both memory references are volatile, then there must always be a
1453    dependence between the two references, since their order can not be
1454    changed.  A volatile and non-volatile reference can be interchanged
1455    though. 
1456
1457    A MEM_IN_STRUCT reference at a non-AND varying address can never
1458    conflict with a non-MEM_IN_STRUCT reference at a fixed address.  We
1459    also must allow AND addresses, because they may generate accesses
1460    outside the object being referenced.  This is used to generate
1461    aligned addresses from unaligned addresses, for instance, the alpha
1462    storeqi_unaligned pattern.  */
1463
1464 /* Read dependence: X is read after read in MEM takes place.  There can
1465    only be a dependence here if both reads are volatile.  */
1466
1467 int
1468 read_dependence (mem, x)
1469      rtx mem;
1470      rtx x;
1471 {
1472   return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1473 }
1474
1475 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1476    MEM2 is a reference to a structure at a varying address, or returns
1477    MEM2 if vice versa.  Otherwise, returns NULL_RTX.  If a non-NULL
1478    value is returned MEM1 and MEM2 can never alias.  VARIES_P is used
1479    to decide whether or not an address may vary; it should return
1480    nonzero whenever variation is possible.
1481    MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2.  */
1482   
1483 static rtx
1484 fixed_scalar_and_varying_struct_p (mem1, mem2, mem1_addr, mem2_addr, varies_p)
1485      rtx mem1, mem2;
1486      rtx mem1_addr, mem2_addr;
1487      int (*varies_p) PARAMS ((rtx));
1488 {  
1489   if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2) 
1490       && !varies_p (mem1_addr) && varies_p (mem2_addr))
1491     /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1492        varying address.  */
1493     return mem1;
1494
1495   if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2) 
1496       && varies_p (mem1_addr) && !varies_p (mem2_addr))
1497     /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1498        varying address.  */
1499     return mem2;
1500
1501   return NULL_RTX;
1502 }
1503
1504 /* Returns nonzero if something about the mode or address format MEM1
1505    indicates that it might well alias *anything*.  */
1506
1507 static int
1508 aliases_everything_p (mem)
1509      rtx mem;
1510 {
1511   if (GET_CODE (XEXP (mem, 0)) == AND)
1512     /* If the address is an AND, its very hard to know at what it is
1513        actually pointing.  */
1514     return 1;
1515     
1516   return 0;
1517 }
1518
1519 /* True dependence: X is read after store in MEM takes place.  */
1520
1521 int
1522 true_dependence (mem, mem_mode, x, varies)
1523      rtx mem;
1524      enum machine_mode mem_mode;
1525      rtx x;
1526      int (*varies) PARAMS ((rtx));
1527 {
1528   register rtx x_addr, mem_addr;
1529
1530   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1531     return 1;
1532
1533   if (DIFFERENT_ALIAS_SETS_P (x, mem))
1534     return 0;
1535
1536   /* If X is an unchanging read, then it can't possibly conflict with any
1537      non-unchanging store.  It may conflict with an unchanging write though,
1538      because there may be a single store to this address to initialize it.
1539      Just fall through to the code below to resolve the case where we have
1540      both an unchanging read and an unchanging write.  This won't handle all
1541      cases optimally, but the possible performance loss should be
1542      negligible.  */
1543   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1544     return 0;
1545
1546   if (mem_mode == VOIDmode)
1547     mem_mode = GET_MODE (mem);
1548
1549   x_addr = get_addr (XEXP (x, 0));
1550   mem_addr = get_addr (XEXP (mem, 0));
1551
1552   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1553     return 0;
1554
1555   x_addr = canon_rtx (x_addr);
1556   mem_addr = canon_rtx (mem_addr);
1557
1558   if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1559                             SIZE_FOR_MODE (x), x_addr, 0))
1560     return 0;
1561
1562   if (aliases_everything_p (x))
1563     return 1;
1564
1565   /* We cannot use aliases_everyting_p to test MEM, since we must look
1566      at MEM_MODE, rather than GET_MODE (MEM).  */
1567   if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1568     return 1;
1569
1570   /* In true_dependence we also allow BLKmode to alias anything.  Why
1571      don't we do this in anti_dependence and output_dependence?  */
1572   if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1573     return 1;
1574
1575   return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1576                                               varies);
1577 }
1578
1579 /* Returns non-zero if a write to X might alias a previous read from
1580    (or, if WRITEP is non-zero, a write to) MEM.  */
1581
1582 static int
1583 write_dependence_p (mem, x, writep)
1584      rtx mem;
1585      rtx x;
1586      int writep;
1587 {
1588   rtx x_addr, mem_addr;
1589   rtx fixed_scalar;
1590
1591   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1592     return 1;
1593
1594   if (DIFFERENT_ALIAS_SETS_P (x, mem))
1595     return 0;
1596
1597   /* If MEM is an unchanging read, then it can't possibly conflict with
1598      the store to X, because there is at most one store to MEM, and it must
1599      have occurred somewhere before MEM.  */
1600   if (!writep && RTX_UNCHANGING_P (mem))
1601     return 0;
1602
1603   x_addr = get_addr (XEXP (x, 0));
1604   mem_addr = get_addr (XEXP (mem, 0));
1605
1606   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
1607                           GET_MODE (mem)))
1608     return 0;
1609
1610   x_addr = canon_rtx (x_addr);
1611   mem_addr = canon_rtx (mem_addr);
1612
1613   if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
1614                            SIZE_FOR_MODE (x), x_addr, 0))
1615     return 0;
1616
1617   fixed_scalar 
1618     = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1619                                          rtx_addr_varies_p);
1620
1621   return (!(fixed_scalar == mem && !aliases_everything_p (x))
1622           && !(fixed_scalar == x && !aliases_everything_p (mem)));
1623 }
1624
1625 /* Anti dependence: X is written after read in MEM takes place.  */
1626
1627 int
1628 anti_dependence (mem, x)
1629      rtx mem;
1630      rtx x;
1631 {
1632   return write_dependence_p (mem, x, /*writep=*/0);
1633 }
1634
1635 /* Output dependence: X is written after store in MEM takes place.  */
1636
1637 int
1638 output_dependence (mem, x)
1639      register rtx mem;
1640      register rtx x;
1641 {
1642   return write_dependence_p (mem, x, /*writep=*/1);
1643 }
1644
1645 /* Returns non-zero if X might refer to something which is not
1646    local to the function and is not constant.  */
1647
1648 static int
1649 nonlocal_reference_p (x)
1650      rtx x;
1651 {
1652   rtx base;
1653   register RTX_CODE code;
1654   int regno;
1655
1656   code = GET_CODE (x);
1657
1658   if (GET_RTX_CLASS (code) == 'i')
1659     {
1660       /* Constant functions can be constant if they don't use
1661          scratch memory used to mark function w/o side effects.  */
1662       if (code == CALL_INSN && CONST_CALL_P (x))
1663         {
1664           x = CALL_INSN_FUNCTION_USAGE (x);
1665           if (x == 0)
1666             return 0;
1667         }
1668       else
1669         x = PATTERN (x);
1670       code = GET_CODE (x);
1671     }
1672
1673   switch (code)
1674     {
1675     case SUBREG:
1676       if (GET_CODE (SUBREG_REG (x)) == REG)
1677         {
1678           /* Global registers are not local.  */
1679           if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
1680               && global_regs[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)])
1681             return 1;
1682           return 0;
1683         }
1684       break;
1685
1686     case REG:
1687       regno = REGNO (x);
1688       /* Global registers are not local.  */
1689       if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1690         return 1;
1691       return 0;
1692
1693     case SCRATCH:
1694     case PC:
1695     case CC0:
1696     case CONST_INT:
1697     case CONST_DOUBLE:
1698     case CONST:
1699     case LABEL_REF:
1700       return 0;
1701
1702     case SYMBOL_REF:
1703       /* Constants in the function's constants pool are constant.  */
1704       if (CONSTANT_POOL_ADDRESS_P (x))
1705         return 0;
1706       return 1;
1707
1708     case CALL:
1709       /* Recursion introduces no additional considerations.  */
1710       if (GET_CODE (XEXP (x, 0)) == MEM
1711           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1712           && strcmp(XSTR (XEXP (XEXP (x, 0), 0), 0),
1713                     IDENTIFIER_POINTER (
1714                           DECL_ASSEMBLER_NAME (current_function_decl))) == 0)
1715         return 0;
1716       return 1;
1717
1718     case MEM:
1719       /* Be overly conservative and consider any volatile memory
1720          reference as not local.  */
1721       if (MEM_VOLATILE_P (x))
1722         return 1;
1723       base = find_base_term (XEXP (x, 0));
1724       if (base)
1725         {
1726           /* A Pmode ADDRESS could be a reference via the structure value
1727              address or static chain.  Such memory references are nonlocal.
1728
1729              Thus, we have to examine the contents of the ADDRESS to find
1730              out if this is a local reference or not.  */
1731           if (GET_CODE (base) == ADDRESS
1732               && GET_MODE (base) == Pmode
1733               && (XEXP (base, 0) == stack_pointer_rtx
1734                   || XEXP (base, 0) == arg_pointer_rtx
1735 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1736                   || XEXP (base, 0) == hard_frame_pointer_rtx
1737 #endif
1738                   || XEXP (base, 0) == frame_pointer_rtx))
1739             return 0;
1740           /* Constants in the function's constant pool are constant.  */
1741           if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
1742             return 0;
1743         }
1744       return 1;
1745
1746     case ASM_INPUT:
1747     case ASM_OPERANDS:
1748       return 1;
1749
1750     default:
1751       break;
1752     }
1753
1754   /* Recursively scan the operands of this expression.  */
1755
1756   {
1757     register const char *fmt = GET_RTX_FORMAT (code);
1758     register int i;
1759     
1760     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1761       {
1762         if (fmt[i] == 'e' && XEXP (x, i))
1763           {
1764             if (nonlocal_reference_p (XEXP (x, i)))
1765               return 1;
1766           }
1767         else if (fmt[i] == 'E')
1768           {
1769             register int j;
1770             for (j = 0; j < XVECLEN (x, i); j++)
1771               if (nonlocal_reference_p (XVECEXP (x, i, j)))
1772                 return 1;
1773           }
1774       }
1775   }
1776
1777   return 0;
1778 }
1779
1780 /* Mark the function if it is constant.  */
1781
1782 void
1783 mark_constant_function ()
1784 {
1785   rtx insn;
1786
1787   if (TREE_PUBLIC (current_function_decl)
1788       || TREE_READONLY (current_function_decl)
1789       || TREE_THIS_VOLATILE (current_function_decl)
1790       || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode)
1791     return;
1792
1793   /* Determine if this is a constant function.  */
1794
1795   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1796     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1797         && nonlocal_reference_p (insn))
1798       return;
1799
1800   /* Mark the function.  */
1801
1802   TREE_READONLY (current_function_decl) = 1;
1803 }
1804
1805
1806 static HARD_REG_SET argument_registers;
1807
1808 void
1809 init_alias_once ()
1810 {
1811   register int i;
1812
1813 #ifndef OUTGOING_REGNO
1814 #define OUTGOING_REGNO(N) N
1815 #endif
1816   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1817     /* Check whether this register can hold an incoming pointer
1818        argument.  FUNCTION_ARG_REGNO_P tests outgoing register
1819        numbers, so translate if necessary due to register windows. */
1820     if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
1821         && HARD_REGNO_MODE_OK (i, Pmode))
1822       SET_HARD_REG_BIT (argument_registers, i);
1823
1824   alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
1825 }
1826
1827 /* Initialize the aliasing machinery.  Initialize the REG_KNOWN_VALUE
1828    array.  */
1829
1830 void
1831 init_alias_analysis ()
1832 {
1833   int maxreg = max_reg_num ();
1834   int changed, pass;
1835   register int i;
1836   register unsigned int ui;
1837   register rtx insn;
1838
1839   reg_known_value_size = maxreg;
1840
1841   reg_known_value 
1842     = (rtx *) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (rtx))
1843     - FIRST_PSEUDO_REGISTER;
1844   reg_known_equiv_p 
1845     = (char*) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (char))
1846     - FIRST_PSEUDO_REGISTER;
1847
1848   /* Overallocate reg_base_value to allow some growth during loop
1849      optimization.  Loop unrolling can create a large number of
1850      registers.  */
1851   reg_base_value_size = maxreg * 2;
1852   reg_base_value = (rtx *) xcalloc (reg_base_value_size, sizeof (rtx));
1853   if (ggc_p)
1854     ggc_add_rtx_root (reg_base_value, reg_base_value_size);
1855
1856   new_reg_base_value = (rtx *) xmalloc (reg_base_value_size * sizeof (rtx));
1857   reg_seen = (char *) xmalloc (reg_base_value_size);
1858   if (! reload_completed && flag_unroll_loops)
1859     {
1860       /* ??? Why are we realloc'ing if we're just going to zero it?  */
1861       alias_invariant = (rtx *)xrealloc (alias_invariant,
1862                                          reg_base_value_size * sizeof (rtx));
1863       bzero ((char *)alias_invariant, reg_base_value_size * sizeof (rtx));
1864     }
1865     
1866
1867   /* The basic idea is that each pass through this loop will use the
1868      "constant" information from the previous pass to propagate alias
1869      information through another level of assignments.
1870
1871      This could get expensive if the assignment chains are long.  Maybe
1872      we should throttle the number of iterations, possibly based on
1873      the optimization level or flag_expensive_optimizations.
1874
1875      We could propagate more information in the first pass by making use
1876      of REG_N_SETS to determine immediately that the alias information
1877      for a pseudo is "constant".
1878
1879      A program with an uninitialized variable can cause an infinite loop
1880      here.  Instead of doing a full dataflow analysis to detect such problems
1881      we just cap the number of iterations for the loop.
1882
1883      The state of the arrays for the set chain in question does not matter
1884      since the program has undefined behavior.  */
1885
1886   pass = 0;
1887   do
1888     {
1889       /* Assume nothing will change this iteration of the loop.  */
1890       changed = 0;
1891
1892       /* We want to assign the same IDs each iteration of this loop, so
1893          start counting from zero each iteration of the loop.  */
1894       unique_id = 0;
1895
1896       /* We're at the start of the funtion each iteration through the
1897          loop, so we're copying arguments.  */
1898       copying_arguments = 1;
1899
1900       /* Wipe the potential alias information clean for this pass.  */
1901       bzero ((char *) new_reg_base_value, reg_base_value_size * sizeof (rtx));
1902
1903       /* Wipe the reg_seen array clean.  */
1904       bzero ((char *) reg_seen, reg_base_value_size);
1905
1906       /* Mark all hard registers which may contain an address.
1907          The stack, frame and argument pointers may contain an address.
1908          An argument register which can hold a Pmode value may contain
1909          an address even if it is not in BASE_REGS.
1910
1911          The address expression is VOIDmode for an argument and
1912          Pmode for other registers.  */
1913
1914       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1915         if (TEST_HARD_REG_BIT (argument_registers, i))
1916           new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
1917                                                    gen_rtx_REG (Pmode, i));
1918
1919       new_reg_base_value[STACK_POINTER_REGNUM]
1920         = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
1921       new_reg_base_value[ARG_POINTER_REGNUM]
1922         = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
1923       new_reg_base_value[FRAME_POINTER_REGNUM]
1924         = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
1925 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1926       new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
1927         = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
1928 #endif
1929       if (struct_value_incoming_rtx
1930           && GET_CODE (struct_value_incoming_rtx) == REG)
1931         new_reg_base_value[REGNO (struct_value_incoming_rtx)]
1932           = gen_rtx_ADDRESS (Pmode, struct_value_incoming_rtx);
1933
1934       if (static_chain_rtx
1935           && GET_CODE (static_chain_rtx) == REG)
1936         new_reg_base_value[REGNO (static_chain_rtx)]
1937           = gen_rtx_ADDRESS (Pmode, static_chain_rtx);
1938
1939       /* Walk the insns adding values to the new_reg_base_value array.  */
1940       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1941         {
1942           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1943             {
1944               rtx note, set;
1945
1946 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
1947               if (prologue_epilogue_contains (insn))
1948                 continue;
1949 #endif
1950
1951               /* If this insn has a noalias note, process it,  Otherwise,
1952                  scan for sets.  A simple set will have no side effects
1953                  which could change the base value of any other register. */
1954
1955               if (GET_CODE (PATTERN (insn)) == SET
1956                   && REG_NOTES (insn) != 0
1957                   && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
1958                 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
1959               else
1960                 note_stores (PATTERN (insn), record_set, NULL);
1961
1962               set = single_set (insn);
1963
1964               if (set != 0
1965                   && GET_CODE (SET_DEST (set)) == REG
1966                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
1967                   && REG_NOTES (insn) != 0
1968                   && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1969                        && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
1970                       || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1971                   && GET_CODE (XEXP (note, 0)) != EXPR_LIST
1972                   && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
1973                 {
1974                   int regno = REGNO (SET_DEST (set));
1975                   reg_known_value[regno] = XEXP (note, 0);
1976                   reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
1977                 }
1978             }
1979           else if (GET_CODE (insn) == NOTE
1980                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
1981             copying_arguments = 0;
1982         }
1983
1984       /* Now propagate values from new_reg_base_value to reg_base_value.  */
1985       for (ui = 0; ui < reg_base_value_size; ui++)
1986         {
1987           if (new_reg_base_value[ui]
1988               && new_reg_base_value[ui] != reg_base_value[ui]
1989               && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
1990             {
1991               reg_base_value[ui] = new_reg_base_value[ui];
1992               changed = 1;
1993             }
1994         }
1995     }
1996   while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
1997
1998   /* Fill in the remaining entries.  */
1999   for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
2000     if (reg_known_value[i] == 0)
2001       reg_known_value[i] = regno_reg_rtx[i];
2002
2003   /* Simplify the reg_base_value array so that no register refers to
2004      another register, except to special registers indirectly through
2005      ADDRESS expressions.
2006
2007      In theory this loop can take as long as O(registers^2), but unless
2008      there are very long dependency chains it will run in close to linear
2009      time.
2010
2011      This loop may not be needed any longer now that the main loop does
2012      a better job at propagating alias information.  */
2013   pass = 0;
2014   do
2015     {
2016       changed = 0;
2017       pass++;
2018       for (ui = 0; ui < reg_base_value_size; ui++)
2019         {
2020           rtx base = reg_base_value[ui];
2021           if (base && GET_CODE (base) == REG)
2022             {
2023               unsigned int base_regno = REGNO (base);
2024               if (base_regno == ui)             /* register set from itself */
2025                 reg_base_value[ui] = 0;
2026               else
2027                 reg_base_value[ui] = reg_base_value[base_regno];
2028               changed = 1;
2029             }
2030         }
2031     }
2032   while (changed && pass < MAX_ALIAS_LOOP_PASSES);
2033
2034   /* Clean up.  */
2035   free (new_reg_base_value);
2036   new_reg_base_value = 0;
2037   free (reg_seen);
2038   reg_seen = 0;
2039 }
2040
2041 void
2042 end_alias_analysis ()
2043 {
2044   free (reg_known_value + FIRST_PSEUDO_REGISTER);
2045   reg_known_value = 0;
2046   reg_known_value_size = 0;
2047   free (reg_known_equiv_p + FIRST_PSEUDO_REGISTER);
2048   reg_known_equiv_p = 0;
2049   if (reg_base_value)
2050     {
2051       if (ggc_p)
2052         ggc_del_root (reg_base_value);
2053       free (reg_base_value);
2054       reg_base_value = 0;
2055     }
2056   reg_base_value_size = 0;
2057   if (alias_invariant)
2058     {
2059       free (alias_invariant);
2060       alias_invariant = 0;
2061     }
2062 }