OSDN Git Service

* alias.c: Include tree.h.
[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 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   if (code == ADDRESSOF)
630     return REGNO (XEXP (x, 0)) == REGNO (XEXP (y, 0)) && XINT (x, 1) == XINT (y, 1);
631
632   /* For commutative operations, the RTX match if the operand match in any
633      order.  Also handle the simple binary and unary cases without a loop.  */
634   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
635     return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
636              && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
637             || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
638                 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
639   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
640     return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
641             && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
642   else if (GET_RTX_CLASS (code) == '1')
643     return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
644
645   /* Compare the elements.  If any pair of corresponding elements
646      fail to match, return 0 for the whole things.
647
648      Limit cases to types which actually appear in addresses.  */
649
650   fmt = GET_RTX_FORMAT (code);
651   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
652     {
653       switch (fmt[i])
654         {
655         case 'i':
656           if (XINT (x, i) != XINT (y, i))
657             return 0;
658           break;
659
660         case 'E':
661           /* Two vectors must have the same length.  */
662           if (XVECLEN (x, i) != XVECLEN (y, i))
663             return 0;
664
665           /* And the corresponding elements must match.  */
666           for (j = 0; j < XVECLEN (x, i); j++)
667             if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
668               return 0;
669           break;
670
671         case 'e':
672           if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
673             return 0;
674           break;
675
676         /* This can happen for an asm which clobbers memory.  */
677         case '0':
678           break;
679
680           /* It is believed that rtx's at this level will never
681              contain anything but integers and other rtx's,
682              except for within LABEL_REFs and SYMBOL_REFs.  */
683         default:
684           abort ();
685         }
686     }
687   return 1;
688 }
689
690 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
691    X and return it, or return 0 if none found.  */
692
693 static rtx
694 find_symbolic_term (x)
695      rtx x;
696 {
697   register int i;
698   register enum rtx_code code;
699   register char *fmt;
700
701   code = GET_CODE (x);
702   if (code == SYMBOL_REF || code == LABEL_REF)
703     return x;
704   if (GET_RTX_CLASS (code) == 'o')
705     return 0;
706
707   fmt = GET_RTX_FORMAT (code);
708   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
709     {
710       rtx t;
711
712       if (fmt[i] == 'e')
713         {
714           t = find_symbolic_term (XEXP (x, i));
715           if (t != 0)
716             return t;
717         }
718       else if (fmt[i] == 'E')
719         break;
720     }
721   return 0;
722 }
723
724 static rtx
725 find_base_term (x)
726      register rtx x;
727 {
728   switch (GET_CODE (x))
729     {
730     case REG:
731       return REG_BASE_VALUE (x);
732
733     case ZERO_EXTEND:
734     case SIGN_EXTEND:   /* Used for Alpha/NT pointers */
735     case HIGH:
736     case PRE_INC:
737     case PRE_DEC:
738     case POST_INC:
739     case POST_DEC:
740       return find_base_term (XEXP (x, 0));
741
742     case CONST:
743       x = XEXP (x, 0);
744       if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
745         return 0;
746       /* fall through */
747     case LO_SUM:
748     case PLUS:
749     case MINUS:
750       {
751         rtx tmp1 = XEXP (x, 0);
752         rtx tmp2 = XEXP (x, 1);
753
754         /* This is a litle bit tricky since we have to determine which of
755            the two operands represents the real base address.  Otherwise this
756            routine may return the index register instead of the base register.
757
758            That may cause us to believe no aliasing was possible, when in
759            fact aliasing is possible.
760
761            We use a few simple tests to guess the base register.  Additional
762            tests can certainly be added.  For example, if one of the operands
763            is a shift or multiply, then it must be the index register and the
764            other operand is the base register.  */
765         
766         /* If either operand is known to be a pointer, then use it
767            to determine the base term.  */
768         if (REG_P (tmp1) && REGNO_POINTER_FLAG (REGNO (tmp1)))
769           return find_base_term (tmp1);
770
771         if (REG_P (tmp2) && REGNO_POINTER_FLAG (REGNO (tmp2)))
772           return find_base_term (tmp2);
773
774         /* Neither operand was known to be a pointer.  Go ahead and find the
775            base term for both operands.  */
776         tmp1 = find_base_term (tmp1);
777         tmp2 = find_base_term (tmp2);
778
779         /* If either base term is named object or a special address
780            (like an argument or stack reference), then use it for the
781            base term.  */
782         if (tmp1
783             && (GET_CODE (tmp1) == SYMBOL_REF
784                 || GET_CODE (tmp1) == LABEL_REF
785                 || (GET_CODE (tmp1) == ADDRESS
786                     && GET_MODE (tmp1) != VOIDmode)))
787           return tmp1;
788
789         if (tmp2
790             && (GET_CODE (tmp2) == SYMBOL_REF
791                 || GET_CODE (tmp2) == LABEL_REF
792                 || (GET_CODE (tmp2) == ADDRESS
793                     && GET_MODE (tmp2) != VOIDmode)))
794           return tmp2;
795
796         /* We could not determine which of the two operands was the
797            base register and which was the index.  So we can determine
798            nothing from the base alias check.  */
799         return 0;
800       }
801
802     case AND:
803       if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
804         return REG_BASE_VALUE (XEXP (x, 0));
805       return 0;
806
807     case SYMBOL_REF:
808     case LABEL_REF:
809       return x;
810
811     default:
812       return 0;
813     }
814 }
815
816 /* Return 0 if the addresses X and Y are known to point to different
817    objects, 1 if they might be pointers to the same object.  */
818
819 static int
820 base_alias_check (x, y, x_mode, y_mode)
821      rtx x, y;
822      enum machine_mode x_mode, y_mode;
823 {
824   rtx x_base = find_base_term (x);
825   rtx y_base = find_base_term (y);
826
827   /* If the address itself has no known base see if a known equivalent
828      value has one.  If either address still has no known base, nothing
829      is known about aliasing.  */
830   if (x_base == 0)
831     {
832       rtx x_c;
833       if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
834         return 1;
835       x_base = find_base_term (x_c);
836       if (x_base == 0)
837         return 1;
838     }
839
840   if (y_base == 0)
841     {
842       rtx y_c;
843       if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
844         return 1;
845       y_base = find_base_term (y_c);
846       if (y_base == 0)
847         return 1;
848     }
849
850   /* If the base addresses are equal nothing is known about aliasing.  */
851   if (rtx_equal_p (x_base, y_base))
852     return 1;
853
854   /* The base addresses of the read and write are different expressions. 
855      If they are both symbols and they are not accessed via AND, there is
856      no conflict.  We can bring knowledge of object alignment into play
857      here.  For example, on alpha, "char a, b;" can alias one another,
858      though "char a; long b;" cannot.  */
859   if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
860     {
861       if (GET_CODE (x) == AND && GET_CODE (y) == AND)
862         return 1;
863       if (GET_CODE (x) == AND
864           && (GET_CODE (XEXP (x, 1)) != CONST_INT
865               || GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
866         return 1;
867       if (GET_CODE (y) == AND
868           && (GET_CODE (XEXP (y, 1)) != CONST_INT
869               || GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
870         return 1;
871       /* Differing symbols never alias.  */
872       return 0;
873     }
874
875   /* If one address is a stack reference there can be no alias:
876      stack references using different base registers do not alias,
877      a stack reference can not alias a parameter, and a stack reference
878      can not alias a global.  */
879   if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
880       || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
881     return 0;
882
883   if (! flag_argument_noalias)
884     return 1;
885
886   if (flag_argument_noalias > 1)
887     return 0;
888
889   /* Weak noalias assertion (arguments are distinct, but may match globals). */
890   return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
891 }
892
893 /*  Return the address of the (N_REFS + 1)th memory reference to ADDR
894     where SIZE is the size in bytes of the memory reference.  If ADDR
895     is not modified by the memory reference then ADDR is returned.  */
896
897 rtx
898 addr_side_effect_eval (addr, size, n_refs)
899      rtx addr;
900      int size;
901      int n_refs;
902 {
903   int offset = 0;
904   
905   switch (GET_CODE (addr))
906     {
907     case PRE_INC:
908       offset = (n_refs + 1) * size;
909       break;
910     case PRE_DEC:
911       offset = -(n_refs + 1) * size;
912       break;
913     case POST_INC:
914       offset = n_refs * size;
915       break;
916     case POST_DEC:
917       offset = -n_refs * size;
918       break;
919
920     default:
921       return addr;
922     }
923   
924   if (offset)
925     addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
926   else
927     addr = XEXP (addr, 0);
928
929   return addr;
930 }
931
932 /* Return nonzero if X and Y (memory addresses) could reference the
933    same location in memory.  C is an offset accumulator.  When
934    C is nonzero, we are testing aliases between X and Y + C.
935    XSIZE is the size in bytes of the X reference,
936    similarly YSIZE is the size in bytes for Y.
937
938    If XSIZE or YSIZE is zero, we do not know the amount of memory being
939    referenced (the reference was BLKmode), so make the most pessimistic
940    assumptions.
941
942    If XSIZE or YSIZE is negative, we may access memory outside the object
943    being referenced as a side effect.  This can happen when using AND to
944    align memory references, as is done on the Alpha.
945
946    Nice to notice that varying addresses cannot conflict with fp if no
947    local variables had their addresses taken, but that's too hard now.  */
948
949
950 static int
951 memrefs_conflict_p (xsize, x, ysize, y, c)
952      register rtx x, y;
953      int xsize, ysize;
954      HOST_WIDE_INT c;
955 {
956   if (GET_CODE (x) == HIGH)
957     x = XEXP (x, 0);
958   else if (GET_CODE (x) == LO_SUM)
959     x = XEXP (x, 1);
960   else
961     x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
962   if (GET_CODE (y) == HIGH)
963     y = XEXP (y, 0);
964   else if (GET_CODE (y) == LO_SUM)
965     y = XEXP (y, 1);
966   else
967     y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
968
969   if (rtx_equal_for_memref_p (x, y))
970     {
971       if (xsize <= 0 || ysize <= 0)
972         return 1;
973       if (c >= 0 && xsize > c)
974         return 1;
975       if (c < 0 && ysize+c > 0)
976         return 1;
977       return 0;
978     }
979
980   /* This code used to check for conflicts involving stack references and
981      globals but the base address alias code now handles these cases.  */
982
983   if (GET_CODE (x) == PLUS)
984     {
985       /* The fact that X is canonicalized means that this
986          PLUS rtx is canonicalized.  */
987       rtx x0 = XEXP (x, 0);
988       rtx x1 = XEXP (x, 1);
989
990       if (GET_CODE (y) == PLUS)
991         {
992           /* The fact that Y is canonicalized means that this
993              PLUS rtx is canonicalized.  */
994           rtx y0 = XEXP (y, 0);
995           rtx y1 = XEXP (y, 1);
996
997           if (rtx_equal_for_memref_p (x1, y1))
998             return memrefs_conflict_p (xsize, x0, ysize, y0, c);
999           if (rtx_equal_for_memref_p (x0, y0))
1000             return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1001           if (GET_CODE (x1) == CONST_INT)
1002             {
1003               if (GET_CODE (y1) == CONST_INT)
1004                 return memrefs_conflict_p (xsize, x0, ysize, y0,
1005                                            c - INTVAL (x1) + INTVAL (y1));
1006               else
1007                 return memrefs_conflict_p (xsize, x0, ysize, y,
1008                                            c - INTVAL (x1));
1009             }
1010           else if (GET_CODE (y1) == CONST_INT)
1011             return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1012
1013           return 1;
1014         }
1015       else if (GET_CODE (x1) == CONST_INT)
1016         return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1017     }
1018   else if (GET_CODE (y) == PLUS)
1019     {
1020       /* The fact that Y is canonicalized means that this
1021          PLUS rtx is canonicalized.  */
1022       rtx y0 = XEXP (y, 0);
1023       rtx y1 = XEXP (y, 1);
1024
1025       if (GET_CODE (y1) == CONST_INT)
1026         return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1027       else
1028         return 1;
1029     }
1030
1031   if (GET_CODE (x) == GET_CODE (y))
1032     switch (GET_CODE (x))
1033       {
1034       case MULT:
1035         {
1036           /* Handle cases where we expect the second operands to be the
1037              same, and check only whether the first operand would conflict
1038              or not.  */
1039           rtx x0, y0;
1040           rtx x1 = canon_rtx (XEXP (x, 1));
1041           rtx y1 = canon_rtx (XEXP (y, 1));
1042           if (! rtx_equal_for_memref_p (x1, y1))
1043             return 1;
1044           x0 = canon_rtx (XEXP (x, 0));
1045           y0 = canon_rtx (XEXP (y, 0));
1046           if (rtx_equal_for_memref_p (x0, y0))
1047             return (xsize == 0 || ysize == 0
1048                     || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1049
1050           /* Can't properly adjust our sizes.  */
1051           if (GET_CODE (x1) != CONST_INT)
1052             return 1;
1053           xsize /= INTVAL (x1);
1054           ysize /= INTVAL (x1);
1055           c /= INTVAL (x1);
1056           return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1057         }
1058
1059       case REG:
1060         /* Are these registers known not to be equal?  */
1061         if (alias_invariant)
1062           {
1063             unsigned int r_x = REGNO (x), r_y = REGNO (y);
1064             rtx i_x, i_y;       /* invariant relationships of X and Y */
1065
1066             i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1067             i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1068
1069             if (i_x == 0 && i_y == 0)
1070               break;
1071
1072             if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1073                                       ysize, i_y ? i_y : y, c))
1074               return 0;
1075           }
1076         break;
1077
1078       default:
1079         break;
1080       }
1081
1082   /* Treat an access through an AND (e.g. a subword access on an Alpha)
1083      as an access with indeterminate size.  Assume that references 
1084      besides AND are aligned, so if the size of the other reference is
1085      at least as large as the alignment, assume no other overlap.  */
1086   if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1087     {
1088       if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1089         xsize = -1;
1090       return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1091     }
1092   if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1093     {
1094       /* ??? If we are indexing far enough into the array/structure, we
1095          may yet be able to determine that we can not overlap.  But we 
1096          also need to that we are far enough from the end not to overlap
1097          a following reference, so we do nothing with that for now.  */
1098       if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1099         ysize = -1;
1100       return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1101     }
1102
1103   if (CONSTANT_P (x))
1104     {
1105       if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1106         {
1107           c += (INTVAL (y) - INTVAL (x));
1108           return (xsize <= 0 || ysize <= 0
1109                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1110         }
1111
1112       if (GET_CODE (x) == CONST)
1113         {
1114           if (GET_CODE (y) == CONST)
1115             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1116                                        ysize, canon_rtx (XEXP (y, 0)), c);
1117           else
1118             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1119                                        ysize, y, c);
1120         }
1121       if (GET_CODE (y) == CONST)
1122         return memrefs_conflict_p (xsize, x, ysize,
1123                                    canon_rtx (XEXP (y, 0)), c);
1124
1125       if (CONSTANT_P (y))
1126         return (xsize < 0 || ysize < 0
1127                 || (rtx_equal_for_memref_p (x, y)
1128                     && (xsize == 0 || ysize == 0
1129                         || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1130
1131       return 1;
1132     }
1133   return 1;
1134 }
1135
1136 /* Functions to compute memory dependencies.
1137
1138    Since we process the insns in execution order, we can build tables
1139    to keep track of what registers are fixed (and not aliased), what registers
1140    are varying in known ways, and what registers are varying in unknown
1141    ways.
1142
1143    If both memory references are volatile, then there must always be a
1144    dependence between the two references, since their order can not be
1145    changed.  A volatile and non-volatile reference can be interchanged
1146    though. 
1147
1148    A MEM_IN_STRUCT reference at a non-QImode non-AND varying address can never
1149    conflict with a non-MEM_IN_STRUCT reference at a fixed address.   We must
1150    allow QImode aliasing because the ANSI C standard allows character
1151    pointers to alias anything.  We are assuming that characters are
1152    always QImode here.  We also must allow AND addresses, because they may
1153    generate accesses outside the object being referenced.  This is used to
1154    generate aligned addresses from unaligned addresses, for instance, the
1155    alpha storeqi_unaligned pattern.  */
1156
1157 /* Read dependence: X is read after read in MEM takes place.  There can
1158    only be a dependence here if both reads are volatile.  */
1159
1160 int
1161 read_dependence (mem, x)
1162      rtx mem;
1163      rtx x;
1164 {
1165   return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1166 }
1167
1168 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1169    MEM2 is a reference to a structure at a varying address, or returns
1170    MEM2 if vice versa.  Otherwise, returns NULL_RTX.  If a non-NULL
1171    value is returned MEM1 and MEM2 can never alias.  VARIES_P is used
1172    to decide whether or not an address may vary; it should return
1173    nozero whenever variation is possible.  */
1174
1175 static rtx
1176 fixed_scalar_and_varying_struct_p (mem1, mem2, varies_p)
1177      rtx mem1;
1178      rtx mem2;
1179      int (*varies_p) PROTO((rtx));
1180 {
1181   rtx mem1_addr = XEXP (mem1, 0);
1182   rtx mem2_addr = XEXP (mem2, 0);
1183   
1184   if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2) 
1185       && !varies_p (mem1_addr) && varies_p (mem2_addr))
1186     /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1187        varying address.  */
1188     return mem1;
1189
1190   if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2) 
1191       && varies_p (mem1_addr) && !varies_p (mem2_addr))
1192     /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1193        varying address.  */
1194     return mem2;
1195
1196   return NULL_RTX;
1197 }
1198
1199 /* Returns nonzero if something about the mode or address format MEM1
1200    indicates that it might well alias *anything*.  */
1201
1202 static int
1203 aliases_everything_p (mem)
1204      rtx mem;
1205 {
1206   if (GET_MODE (mem) == QImode)
1207     /* ANSI C says that a `char*' can point to anything.  */
1208     return 1;
1209
1210   if (GET_CODE (XEXP (mem, 0)) == AND)
1211     /* If the address is an AND, its very hard to know at what it is
1212        actually pointing.  */
1213     return 1;
1214     
1215   return 0;
1216 }
1217
1218 /* True dependence: X is read after store in MEM takes place.  */
1219
1220 int
1221 true_dependence (mem, mem_mode, x, varies)
1222      rtx mem;
1223      enum machine_mode mem_mode;
1224      rtx x;
1225      int (*varies) PROTO((rtx));
1226 {
1227   register rtx x_addr, mem_addr;
1228
1229   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1230     return 1;
1231
1232   if (DIFFERENT_ALIAS_SETS_P (x, mem))
1233     return 0;
1234
1235   /* If X is an unchanging read, then it can't possibly conflict with any
1236      non-unchanging store.  It may conflict with an unchanging write though,
1237      because there may be a single store to this address to initialize it.
1238      Just fall through to the code below to resolve the case where we have
1239      both an unchanging read and an unchanging write.  This won't handle all
1240      cases optimally, but the possible performance loss should be
1241      negligible.  */
1242   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1243     return 0;
1244
1245   if (mem_mode == VOIDmode)
1246     mem_mode = GET_MODE (mem);
1247
1248   if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0), GET_MODE (x), mem_mode))
1249     return 0;
1250
1251   x_addr = canon_rtx (XEXP (x, 0));
1252   mem_addr = canon_rtx (XEXP (mem, 0));
1253
1254   if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1255                             SIZE_FOR_MODE (x), x_addr, 0))
1256     return 0;
1257
1258   if (aliases_everything_p (x))
1259     return 1;
1260
1261   /* We cannot use aliases_everyting_p to test MEM, since we must look
1262      at MEM_MODE, rather than GET_MODE (MEM).  */
1263   if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1264     return 1;
1265
1266   /* In true_dependence we also allow BLKmode to alias anything.  Why
1267      don't we do this in anti_dependence and output_dependence?  */
1268   if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1269     return 1;
1270
1271   return !fixed_scalar_and_varying_struct_p (mem, x, varies);
1272 }
1273
1274 /* Returns non-zero if a write to X might alias a previous read from
1275    (or, if WRITEP is non-zero, a write to) MEM.  */
1276
1277 static int
1278 write_dependence_p (mem, x, writep)
1279      rtx mem;
1280      rtx x;
1281      int writep;
1282 {
1283   rtx x_addr, mem_addr;
1284   rtx fixed_scalar;
1285
1286   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1287     return 1;
1288
1289   /* If MEM is an unchanging read, then it can't possibly conflict with
1290      the store to X, because there is at most one store to MEM, and it must
1291      have occurred somewhere before MEM.  */
1292   if (!writep && RTX_UNCHANGING_P (mem))
1293     return 0;
1294
1295   if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0), GET_MODE (x),
1296                           GET_MODE (mem)))
1297     return 0;
1298
1299   x = canon_rtx (x);
1300   mem = canon_rtx (mem);
1301
1302   if (DIFFERENT_ALIAS_SETS_P (x, mem))
1303     return 0;
1304
1305   x_addr = XEXP (x, 0);
1306   mem_addr = XEXP (mem, 0);
1307
1308   if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
1309                            SIZE_FOR_MODE (x), x_addr, 0))
1310     return 0;
1311
1312   fixed_scalar 
1313     = fixed_scalar_and_varying_struct_p (mem, x, rtx_addr_varies_p);
1314   
1315   return (!(fixed_scalar == mem && !aliases_everything_p (x))
1316           && !(fixed_scalar == x && !aliases_everything_p (mem)));
1317 }
1318
1319 /* Anti dependence: X is written after read in MEM takes place.  */
1320
1321 int
1322 anti_dependence (mem, x)
1323      rtx mem;
1324      rtx x;
1325 {
1326   return write_dependence_p (mem, x, /*writep=*/0);
1327 }
1328
1329 /* Output dependence: X is written after store in MEM takes place.  */
1330
1331 int
1332 output_dependence (mem, x)
1333      register rtx mem;
1334      register rtx x;
1335 {
1336   return write_dependence_p (mem, x, /*writep=*/1);
1337 }
1338
1339 /* Returns non-zero if X might refer to something which is not
1340    local to the function and is not constant.  */
1341
1342 static int
1343 nonlocal_reference_p (x)
1344      rtx x;
1345 {
1346   rtx base;
1347   register RTX_CODE code;
1348   int regno;
1349
1350   code = GET_CODE (x);
1351
1352   if (GET_RTX_CLASS (code) == 'i')
1353     {
1354       /* Constant functions are constant.  */
1355       if (code == CALL_INSN && CONST_CALL_P (x))
1356         return 0;
1357       x = PATTERN (x);
1358       code = GET_CODE (x);
1359     }
1360
1361   switch (code)
1362     {
1363     case SUBREG:
1364       if (GET_CODE (SUBREG_REG (x)) == REG)
1365         {
1366           /* Global registers are not local.  */
1367           if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
1368               && global_regs[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)])
1369             return 1;
1370           return 0;
1371         }
1372       break;
1373
1374     case REG:
1375       regno = REGNO (x);
1376       /* Global registers are not local.  */
1377       if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1378         return 1;
1379       return 0;
1380
1381     case SCRATCH:
1382     case PC:
1383     case CC0:
1384     case CONST_INT:
1385     case CONST_DOUBLE:
1386     case CONST:
1387     case LABEL_REF:
1388       return 0;
1389
1390     case SYMBOL_REF:
1391       /* Constants in the function's constants pool are constant.  */
1392       if (CONSTANT_POOL_ADDRESS_P (x))
1393         return 0;
1394       return 1;
1395
1396     case CALL:
1397       /* Recursion introduces no additional considerations.  */
1398       if (GET_CODE (XEXP (x, 0)) == MEM
1399           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1400           && strcmp(XSTR (XEXP (XEXP (x, 0), 0), 0),
1401                     IDENTIFIER_POINTER (
1402                           DECL_ASSEMBLER_NAME (current_function_decl))) == 0)
1403         return 0;
1404       return 1;
1405
1406     case MEM:
1407       /* Be overly conservative and consider any volatile memory
1408          reference as not local.  */
1409       if (MEM_VOLATILE_P (x))
1410         return 1;
1411       base = find_base_term (XEXP (x, 0));
1412       if (base)
1413         {
1414           /* Stack references are local.  */
1415           if (GET_CODE (base) == ADDRESS && GET_MODE (base) == Pmode)
1416             return 0;
1417           /* Constants in the function's constant pool are constant.  */
1418           if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
1419             return 0;
1420         }
1421       return 1;
1422
1423     case ASM_INPUT:
1424     case ASM_OPERANDS:
1425       return 1;
1426
1427     default:
1428       break;
1429     }
1430
1431   /* Recursively scan the operands of this expression.  */
1432
1433   {
1434     register char *fmt = GET_RTX_FORMAT (code);
1435     register int i;
1436     
1437     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1438       {
1439         if (fmt[i] == 'e')
1440           {
1441             if (nonlocal_reference_p (XEXP (x, i)))
1442               return 1;
1443           }
1444         if (fmt[i] == 'E')
1445           {
1446             register int j;
1447             for (j = 0; j < XVECLEN (x, i); j++)
1448               if (nonlocal_reference_p (XVECEXP (x, i, j)))
1449                 return 1;
1450           }
1451       }
1452   }
1453
1454   return 0;
1455 }
1456
1457 /* Mark the function if it is constant.  */
1458
1459 void
1460 mark_constant_function ()
1461 {
1462   rtx insn;
1463
1464   if (TREE_PUBLIC (current_function_decl)
1465       || TREE_READONLY (current_function_decl)
1466       || TREE_THIS_VOLATILE (current_function_decl)
1467       || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode)
1468     return;
1469
1470   /* Determine if this is a constant function.  */
1471
1472   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1473     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1474         && nonlocal_reference_p (insn))
1475       return;
1476
1477   /* Mark the function.  */
1478
1479   TREE_READONLY (current_function_decl) = 1;
1480 }
1481
1482
1483 static HARD_REG_SET argument_registers;
1484
1485 void
1486 init_alias_once ()
1487 {
1488   register int i;
1489
1490 #ifndef OUTGOING_REGNO
1491 #define OUTGOING_REGNO(N) N
1492 #endif
1493   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1494     /* Check whether this register can hold an incoming pointer
1495        argument.  FUNCTION_ARG_REGNO_P tests outgoing register
1496        numbers, so translate if necessary due to register windows. */
1497     if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
1498         && HARD_REGNO_MODE_OK (i, Pmode))
1499       SET_HARD_REG_BIT (argument_registers, i);
1500
1501   alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
1502 }
1503
1504 void
1505 init_alias_analysis ()
1506 {
1507   int maxreg = max_reg_num ();
1508   int changed, pass;
1509   register int i;
1510   register unsigned int ui;
1511   register rtx insn;
1512
1513   reg_known_value_size = maxreg;
1514
1515   reg_known_value
1516     = (rtx *) oballoc ((maxreg - FIRST_PSEUDO_REGISTER) * sizeof (rtx))
1517     - FIRST_PSEUDO_REGISTER;
1518   reg_known_equiv_p =
1519     oballoc (maxreg - FIRST_PSEUDO_REGISTER) - FIRST_PSEUDO_REGISTER;
1520   bzero ((char *) (reg_known_value + FIRST_PSEUDO_REGISTER),
1521          (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
1522   bzero (reg_known_equiv_p + FIRST_PSEUDO_REGISTER,
1523          (maxreg - FIRST_PSEUDO_REGISTER) * sizeof (char));
1524
1525   /* Overallocate reg_base_value to allow some growth during loop
1526      optimization.  Loop unrolling can create a large number of
1527      registers.  */
1528   reg_base_value_size = maxreg * 2;
1529   reg_base_value = (rtx *)oballoc (reg_base_value_size * sizeof (rtx));
1530   new_reg_base_value = (rtx *)alloca (reg_base_value_size * sizeof (rtx));
1531   reg_seen = (char *)alloca (reg_base_value_size);
1532   bzero ((char *) reg_base_value, reg_base_value_size * sizeof (rtx));
1533   if (! reload_completed && flag_unroll_loops)
1534     {
1535       alias_invariant = (rtx *)xrealloc (alias_invariant,
1536                                          reg_base_value_size * sizeof (rtx));
1537       bzero ((char *)alias_invariant, reg_base_value_size * sizeof (rtx));
1538     }
1539     
1540
1541   /* The basic idea is that each pass through this loop will use the
1542      "constant" information from the previous pass to propagate alias
1543      information through another level of assignments.
1544
1545      This could get expensive if the assignment chains are long.  Maybe
1546      we should throttle the number of iterations, possibly based on
1547      the optimization level or flag_expensive_optimizations.
1548
1549      We could propagate more information in the first pass by making use
1550      of REG_N_SETS to determine immediately that the alias information
1551      for a pseudo is "constant".
1552
1553      A program with an uninitialized variable can cause an infinite loop
1554      here.  Instead of doing a full dataflow analysis to detect such problems
1555      we just cap the number of iterations for the loop.
1556
1557      The state of the arrays for the set chain in question does not matter
1558      since the program has undefined behavior.  */
1559
1560   pass = 0;
1561   do
1562     {
1563       /* Assume nothing will change this iteration of the loop.  */
1564       changed = 0;
1565
1566       /* We want to assign the same IDs each iteration of this loop, so
1567          start counting from zero each iteration of the loop.  */
1568       unique_id = 0;
1569
1570       /* We're at the start of the funtion each iteration through the
1571          loop, so we're copying arguments.  */
1572       copying_arguments = 1;
1573
1574       /* Wipe the potential alias information clean for this pass.  */
1575       bzero ((char *) new_reg_base_value, reg_base_value_size * sizeof (rtx));
1576
1577       /* Wipe the reg_seen array clean.  */
1578       bzero ((char *) reg_seen, reg_base_value_size);
1579
1580       /* Mark all hard registers which may contain an address.
1581          The stack, frame and argument pointers may contain an address.
1582          An argument register which can hold a Pmode value may contain
1583          an address even if it is not in BASE_REGS.
1584
1585          The address expression is VOIDmode for an argument and
1586          Pmode for other registers.  */
1587
1588       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1589         if (TEST_HARD_REG_BIT (argument_registers, i))
1590           new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
1591                                                    gen_rtx_REG (Pmode, i));
1592
1593       new_reg_base_value[STACK_POINTER_REGNUM]
1594         = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
1595       new_reg_base_value[ARG_POINTER_REGNUM]
1596         = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
1597       new_reg_base_value[FRAME_POINTER_REGNUM]
1598         = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
1599 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1600       new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
1601         = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
1602 #endif
1603       if (struct_value_incoming_rtx
1604           && GET_CODE (struct_value_incoming_rtx) == REG)
1605         new_reg_base_value[REGNO (struct_value_incoming_rtx)]
1606           = gen_rtx_ADDRESS (Pmode, struct_value_incoming_rtx);
1607
1608       if (static_chain_rtx
1609           && GET_CODE (static_chain_rtx) == REG)
1610         new_reg_base_value[REGNO (static_chain_rtx)]
1611           = gen_rtx_ADDRESS (Pmode, static_chain_rtx);
1612
1613       /* Walk the insns adding values to the new_reg_base_value array.  */
1614       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1615         {
1616 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
1617           if (prologue_epilogue_contains (insn))
1618             continue;
1619 #endif
1620           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1621             {
1622               rtx note, set;
1623               /* If this insn has a noalias note, process it,  Otherwise,
1624                  scan for sets.  A simple set will have no side effects
1625                  which could change the base value of any other register. */
1626
1627               if (GET_CODE (PATTERN (insn)) == SET
1628                   && (find_reg_note (insn, REG_NOALIAS, NULL_RTX)))
1629                 record_set (SET_DEST (PATTERN (insn)), NULL_RTX);
1630               else
1631                 note_stores (PATTERN (insn), record_set);
1632
1633               set = single_set (insn);
1634
1635               if (set != 0
1636                   && GET_CODE (SET_DEST (set)) == REG
1637                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
1638                   && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1639                        && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
1640                       || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1641                   && GET_CODE (XEXP (note, 0)) != EXPR_LIST
1642                   && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
1643                 {
1644                   int regno = REGNO (SET_DEST (set));
1645                   reg_known_value[regno] = XEXP (note, 0);
1646                   reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
1647                 }
1648             }
1649           else if (GET_CODE (insn) == NOTE
1650                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
1651             copying_arguments = 0;
1652         }
1653
1654       /* Now propagate values from new_reg_base_value to reg_base_value.  */
1655       for (ui = 0; ui < reg_base_value_size; ui++)
1656         {
1657           if (new_reg_base_value[ui]
1658               && new_reg_base_value[ui] != reg_base_value[ui]
1659               && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
1660             {
1661               reg_base_value[ui] = new_reg_base_value[ui];
1662               changed = 1;
1663             }
1664         }
1665     }
1666   while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
1667
1668   /* Fill in the remaining entries.  */
1669   for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
1670     if (reg_known_value[i] == 0)
1671       reg_known_value[i] = regno_reg_rtx[i];
1672
1673   /* Simplify the reg_base_value array so that no register refers to
1674      another register, except to special registers indirectly through
1675      ADDRESS expressions.
1676
1677      In theory this loop can take as long as O(registers^2), but unless
1678      there are very long dependency chains it will run in close to linear
1679      time.
1680
1681      This loop may not be needed any longer now that the main loop does
1682      a better job at propagating alias information.  */
1683   pass = 0;
1684   do
1685     {
1686       changed = 0;
1687       pass++;
1688       for (ui = 0; ui < reg_base_value_size; ui++)
1689         {
1690           rtx base = reg_base_value[ui];
1691           if (base && GET_CODE (base) == REG)
1692             {
1693               unsigned int base_regno = REGNO (base);
1694               if (base_regno == ui)             /* register set from itself */
1695                 reg_base_value[ui] = 0;
1696               else
1697                 reg_base_value[ui] = reg_base_value[base_regno];
1698               changed = 1;
1699             }
1700         }
1701     }
1702   while (changed && pass < MAX_ALIAS_LOOP_PASSES);
1703
1704   new_reg_base_value = 0;
1705   reg_seen = 0;
1706 }
1707
1708 void
1709 end_alias_analysis ()
1710 {
1711   reg_known_value = 0;
1712   reg_base_value = 0;
1713   reg_base_value_size = 0;
1714   if (alias_invariant)
1715     {
1716       free ((char *)alias_invariant);
1717       alias_invariant = 0;
1718     }
1719 }