OSDN Git Service

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