OSDN Git Service

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