OSDN Git Service

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