OSDN Git Service

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