OSDN Git Service

* alias.c (find_base_value): When copying arguments, return the
[pf3gnuchains/gcc-fork.git] / gcc / alias.c
1 /* Alias analysis for GNU C
2    Copyright (C) 1997 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 "rtl.h"
24 #include "expr.h"
25 #include "regs.h"
26 #include "hard-reg-set.h"
27 #include "flags.h"
28
29 static rtx canon_rtx                    PROTO((rtx));
30 static int rtx_equal_for_memref_p       PROTO((rtx, rtx));
31 static rtx find_symbolic_term           PROTO((rtx));
32 static int memrefs_conflict_p           PROTO((int, rtx, int, rtx,
33                                                HOST_WIDE_INT));
34
35 /* Set up all info needed to perform alias analysis on memory references.  */
36
37 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
38
39 /* reg_base_value[N] gives an address to which register N is related.
40    If all sets after the first add or subtract to the current value
41    or otherwise modify it so it does not point to a different top level
42    object, reg_base_value[N] is equal to the address part of the source
43    of the first set.
44
45    A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF.  ADDRESS
46    expressions represent certain special values: function arguments and
47    the stack, frame, and argument pointers.  The contents of an address
48    expression are not used (but they are descriptive for debugging);
49    only the address and mode matter.  Pointer equality, not rtx_equal_p,
50    determines whether two ADDRESS expressions refer to the same base
51    address.  The mode determines whether it is a function argument or
52    other special value. */
53
54 rtx *reg_base_value;
55 rtx *new_reg_base_value;
56 unsigned int reg_base_value_size;       /* size of reg_base_value array */
57 #define REG_BASE_VALUE(X) \
58         (REGNO (X) < reg_base_value_size ? reg_base_value[REGNO (X)] : 0)
59
60 /* Vector indexed by N giving the initial (unchanging) value known
61    for pseudo-register N.  */
62 rtx *reg_known_value;
63
64 /* Indicates number of valid entries in reg_known_value.  */
65 static int reg_known_value_size;
66
67 /* Vector recording for each reg_known_value whether it is due to a
68    REG_EQUIV note.  Future passes (viz., reload) may replace the
69    pseudo with the equivalent expression and so we account for the
70    dependences that would be introduced if that happens. */
71 /* ??? This is a problem only on the Convex.  The REG_EQUIV notes created in
72    assign_parms mention the arg pointer, and there are explicit insns in the
73    RTL that modify the arg pointer.  Thus we must ensure that such insns don't
74    get scheduled across each other because that would invalidate the REG_EQUIV
75    notes.  One could argue that the REG_EQUIV notes are wrong, but solving
76    the problem in the scheduler will likely give better code, so we do it
77    here.  */
78 char *reg_known_equiv_p;
79
80 /* True when scanning insns from the start of the rtl to the
81    NOTE_INSN_FUNCTION_BEG note.  */
82
83 static int copying_arguments;
84
85 /* Inside SRC, the source of a SET, find a base address.  */
86
87 static rtx
88 find_base_value (src)
89      register rtx src;
90 {
91   switch (GET_CODE (src))
92     {
93     case SYMBOL_REF:
94     case LABEL_REF:
95       return src;
96
97     case REG:
98       /* At the start of a function argument registers have known base
99          values which may be lost later.  Returning an ADDRESS
100          expression here allows optimization based on argument values
101          even when the argument registers are used for other purposes.  */
102       if (REGNO (src) < FIRST_PSEUDO_REGISTER && copying_arguments)
103         return new_reg_base_value[REGNO (src)];
104
105       /* If this REG is related to a known base value, return it.
106          This must happen after the arg register check above to avoid
107          circular set chains.  */
108       if (reg_base_value[REGNO (src)])
109         return reg_base_value[REGNO (src)];
110
111       return src;
112
113     case MEM:
114       /* Check for an argument passed in memory.  Only record in the
115          copying-arguments block; it is too hard to track changes
116          otherwise.  */
117       if (copying_arguments
118           && (XEXP (src, 0) == arg_pointer_rtx
119               || (GET_CODE (XEXP (src, 0)) == PLUS
120                   && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
121         return gen_rtx (ADDRESS, VOIDmode, src);
122       return 0;
123
124     case CONST:
125       src = XEXP (src, 0);
126       if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
127         break;
128       /* fall through */
129
130     case PLUS:
131     case MINUS:
132       {
133         rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
134
135         /* If either operand is a REG, then see if we already have
136            a known value for it.  */
137         if (GET_CODE (src_0) == REG)
138           {
139             temp = find_base_value (src_0);
140             if (temp)
141               src_0 = temp;
142           }
143
144         if (GET_CODE (src_1) == REG)
145           {
146             temp = find_base_value (src_1);
147             if (temp)
148               src_1 = temp;
149           }
150
151         /* Guess which operand is the base address.
152
153            If either operand is a symbol, then it is the base.  If
154            either operand is a CONST_INT, then the other is the base.  */
155
156         if (GET_CODE (src_1) == CONST_INT
157             || GET_CODE (src_0) == SYMBOL_REF
158             || GET_CODE (src_0) == LABEL_REF
159             || GET_CODE (src_0) == CONST)
160           return find_base_value (src_0);
161
162         if (GET_CODE (src_0) == CONST_INT
163             || GET_CODE (src_1) == SYMBOL_REF
164             || GET_CODE (src_1) == LABEL_REF
165             || GET_CODE (src_1) == CONST)
166           return find_base_value (src_1);
167
168         /* This might not be necessary anymore. 
169
170            If either operand is a REG that is a known pointer, then it
171            is the base.  */
172         if (GET_CODE (src_0) == REG && REGNO_POINTER_FLAG (REGNO (src_0)))
173           return find_base_value (src_0);
174
175         if (GET_CODE (src_1) == REG && REGNO_POINTER_FLAG (REGNO (src_1)))
176           return find_base_value (src_1);
177
178         return 0;
179       }
180
181     case LO_SUM:
182       /* The standard form is (lo_sum reg sym) so look only at the
183          second operand.  */
184       return find_base_value (XEXP (src, 1));
185
186     case AND:
187       /* If the second operand is constant set the base
188          address to the first operand. */
189       if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
190         return find_base_value (XEXP (src, 0));
191       return 0;
192
193     case HIGH:
194       return find_base_value (XEXP (src, 0));
195     }
196
197   return 0;
198 }
199
200 /* Called from init_alias_analysis indirectly through note_stores.  */
201
202 /* while scanning insns to find base values, reg_seen[N] is nonzero if
203    register N has been set in this function.  */
204 static char *reg_seen;
205
206 /* */
207 static int unique_id;
208
209 static void
210 record_set (dest, set)
211      rtx dest, set;
212 {
213   register int regno;
214   rtx src;
215
216   if (GET_CODE (dest) != REG)
217     return;
218
219   regno = REGNO (dest);
220
221   if (set)
222     {
223       /* A CLOBBER wipes out any old value but does not prevent a previously
224          unset register from acquiring a base address (i.e. reg_seen is not
225          set).  */
226       if (GET_CODE (set) == CLOBBER)
227         {
228           new_reg_base_value[regno] = 0;
229           return;
230         }
231       src = SET_SRC (set);
232     }
233   else
234     {
235       if (reg_seen[regno])
236         {
237           new_reg_base_value[regno] = 0;
238           return;
239         }
240       reg_seen[regno] = 1;
241       new_reg_base_value[regno] = gen_rtx (ADDRESS, Pmode,
242                                        GEN_INT (unique_id++));
243       return;
244     }
245
246   /* This is not the first set.  If the new value is not related to the
247      old value, forget the base value. Note that the following code is
248      not detected:
249      extern int x, y;  int *p = &x; p += (&y-&x);
250      ANSI C does not allow computing the difference of addresses
251      of distinct top level objects.  */
252   if (new_reg_base_value[regno])
253     switch (GET_CODE (src))
254       {
255       case LO_SUM:
256       case PLUS:
257       case MINUS:
258         if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
259           new_reg_base_value[regno] = 0;
260         break;
261       case AND:
262         if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
263           new_reg_base_value[regno] = 0;
264         break;
265       default:
266         new_reg_base_value[regno] = 0;
267         break;
268       }
269   /* If this is the first set of a register, record the value.  */
270   else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
271            && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
272     new_reg_base_value[regno] = find_base_value (src);
273
274   reg_seen[regno] = 1;
275 }
276
277 /* Called from loop optimization when a new pseudo-register is created.  */
278 void
279 record_base_value (regno, val)
280      int regno;
281      rtx val;
282 {
283   if (!flag_alias_check || regno >= reg_base_value_size)
284     return;
285   if (GET_CODE (val) == REG)
286     {
287       if (REGNO (val) < reg_base_value_size)
288         reg_base_value[regno] = reg_base_value[REGNO (val)];
289       return;
290     }
291   reg_base_value[regno] = find_base_value (val);
292 }
293
294 static rtx
295 canon_rtx (x)
296      rtx x;
297 {
298   /* Recursively look for equivalences.  */
299   if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
300       && REGNO (x) < reg_known_value_size)
301     return reg_known_value[REGNO (x)] == x
302       ? x : canon_rtx (reg_known_value[REGNO (x)]);
303   else if (GET_CODE (x) == PLUS)
304     {
305       rtx x0 = canon_rtx (XEXP (x, 0));
306       rtx x1 = canon_rtx (XEXP (x, 1));
307
308       if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
309         {
310           /* We can tolerate LO_SUMs being offset here; these
311              rtl are used for nothing other than comparisons.  */
312           if (GET_CODE (x0) == CONST_INT)
313             return plus_constant_for_output (x1, INTVAL (x0));
314           else if (GET_CODE (x1) == CONST_INT)
315             return plus_constant_for_output (x0, INTVAL (x1));
316           return gen_rtx (PLUS, GET_MODE (x), x0, x1);
317         }
318     }
319   /* This gives us much better alias analysis when called from
320      the loop optimizer.   Note we want to leave the original
321      MEM alone, but need to return the canonicalized MEM with
322      all the flags with their original values.  */
323   else if (GET_CODE (x) == MEM)
324     {
325       rtx addr = canon_rtx (XEXP (x, 0));
326       if (addr != XEXP (x, 0))
327         {
328           rtx new = gen_rtx (MEM, GET_MODE (x), addr);
329           MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
330           RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
331           MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
332           x = new;
333         }
334     }
335   return x;
336 }
337
338 /* Return 1 if X and Y are identical-looking rtx's.
339
340    We use the data in reg_known_value above to see if two registers with
341    different numbers are, in fact, equivalent.  */
342
343 static int
344 rtx_equal_for_memref_p (x, y)
345      rtx x, y;
346 {
347   register int i;
348   register int j;
349   register enum rtx_code code;
350   register char *fmt;
351
352   if (x == 0 && y == 0)
353     return 1;
354   if (x == 0 || y == 0)
355     return 0;
356   x = canon_rtx (x);
357   y = canon_rtx (y);
358
359   if (x == y)
360     return 1;
361
362   code = GET_CODE (x);
363   /* Rtx's of different codes cannot be equal.  */
364   if (code != GET_CODE (y))
365     return 0;
366
367   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
368      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
369
370   if (GET_MODE (x) != GET_MODE (y))
371     return 0;
372
373   /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
374
375   if (code == REG)
376     return REGNO (x) == REGNO (y);
377   if (code == LABEL_REF)
378     return XEXP (x, 0) == XEXP (y, 0);
379   if (code == SYMBOL_REF)
380     return XSTR (x, 0) == XSTR (y, 0);
381
382   /* For commutative operations, the RTX match if the operand match in any
383      order.  Also handle the simple binary and unary cases without a loop.  */
384   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
385     return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
386              && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
387             || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
388                 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
389   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
390     return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
391             && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
392   else if (GET_RTX_CLASS (code) == '1')
393     return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
394
395   /* Compare the elements.  If any pair of corresponding elements
396      fail to match, return 0 for the whole things.  */
397
398   fmt = GET_RTX_FORMAT (code);
399   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
400     {
401       switch (fmt[i])
402         {
403         case 'w':
404           if (XWINT (x, i) != XWINT (y, i))
405             return 0;
406           break;
407
408         case 'n':
409         case 'i':
410           if (XINT (x, i) != XINT (y, i))
411             return 0;
412           break;
413
414         case 'V':
415         case 'E':
416           /* Two vectors must have the same length.  */
417           if (XVECLEN (x, i) != XVECLEN (y, i))
418             return 0;
419
420           /* And the corresponding elements must match.  */
421           for (j = 0; j < XVECLEN (x, i); j++)
422             if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
423               return 0;
424           break;
425
426         case 'e':
427           if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
428             return 0;
429           break;
430
431         case 'S':
432         case 's':
433           if (strcmp (XSTR (x, i), XSTR (y, i)))
434             return 0;
435           break;
436
437         case 'u':
438           /* These are just backpointers, so they don't matter.  */
439           break;
440
441         case '0':
442           break;
443
444           /* It is believed that rtx's at this level will never
445              contain anything but integers and other rtx's,
446              except for within LABEL_REFs and SYMBOL_REFs.  */
447         default:
448           abort ();
449         }
450     }
451   return 1;
452 }
453
454 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
455    X and return it, or return 0 if none found.  */
456
457 static rtx
458 find_symbolic_term (x)
459      rtx x;
460 {
461   register int i;
462   register enum rtx_code code;
463   register char *fmt;
464
465   code = GET_CODE (x);
466   if (code == SYMBOL_REF || code == LABEL_REF)
467     return x;
468   if (GET_RTX_CLASS (code) == 'o')
469     return 0;
470
471   fmt = GET_RTX_FORMAT (code);
472   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
473     {
474       rtx t;
475
476       if (fmt[i] == 'e')
477         {
478           t = find_symbolic_term (XEXP (x, i));
479           if (t != 0)
480             return t;
481         }
482       else if (fmt[i] == 'E')
483         break;
484     }
485   return 0;
486 }
487
488 static rtx
489 find_base_term (x)
490      register rtx x;
491 {
492   switch (GET_CODE (x))
493     {
494     case REG:
495       return REG_BASE_VALUE (x);
496
497     case HIGH:
498       return find_base_term (XEXP (x, 0));
499
500     case PRE_INC:
501     case PRE_DEC:
502     case POST_INC:
503     case POST_DEC:
504       return find_base_term (XEXP (x, 0));
505
506     case CONST:
507       x = XEXP (x, 0);
508       if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
509         return 0;
510       /* fall through */
511     case LO_SUM:
512     case PLUS:
513     case MINUS:
514       {
515         rtx tmp = find_base_term (XEXP (x, 0));
516         if (tmp)
517           return tmp;
518         return find_base_term (XEXP (x, 1));
519       }
520
521     case AND:
522       if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
523         return REG_BASE_VALUE (XEXP (x, 0));
524       return 0;
525
526     case SYMBOL_REF:
527     case LABEL_REF:
528       return x;
529
530     default:
531       return 0;
532     }
533 }
534
535 /* Return 0 if the addresses X and Y are known to point to different
536    objects, 1 if they might be pointers to the same object.  */
537
538 static int
539 base_alias_check (x, y)
540      rtx x, y;
541 {
542   rtx x_base = find_base_term (x);
543   rtx y_base = find_base_term (y);
544
545   /* If either base address is unknown or the base addresses are equal,
546      nothing is known about aliasing.  */
547
548   if (x_base == 0 || y_base == 0 || rtx_equal_p (x_base, y_base))
549     return 1;
550
551   /* The base addresses of the read and write are different
552      expressions.  If they are both symbols and they are not accessed
553      via AND, there is no conflict.  */
554   /* XXX: We can bring knowledge of object alignment and offset into 
555      play here.  For example, on alpha, "char a, b;" can alias one
556      another, though "char a; long b;" cannot.  Similarly, offsets
557      into strutures may be brought into play.  Given "char a, b[40];",
558      a and b[1] may overlap, but a and b[20] do not.  */
559   if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
560     {
561       return GET_CODE (x) == AND || GET_CODE (y) == AND;
562     }
563
564   /* If one address is a stack reference there can be no alias:
565      stack references using different base registers do not alias,
566      a stack reference can not alias a parameter, and a stack reference
567      can not alias a global.  */
568   if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
569       || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
570     return 0;
571
572   if (! flag_argument_noalias)
573     return 1;
574
575   if (flag_argument_noalias > 1)
576     return 0;
577
578   /* Weak noalias assertion (arguments are distinct, but may match globals). */
579   return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
580 }
581
582 /* Return nonzero if X and Y (memory addresses) could reference the
583    same location in memory.  C is an offset accumulator.  When
584    C is nonzero, we are testing aliases between X and Y + C.
585    XSIZE is the size in bytes of the X reference,
586    similarly YSIZE is the size in bytes for Y.
587
588    If XSIZE or YSIZE is zero, we do not know the amount of memory being
589    referenced (the reference was BLKmode), so make the most pessimistic
590    assumptions.
591
592    If XSIZE or YSIZE is negative, we may access memory outside the object
593    being referenced as a side effect.  This can happen when using AND to
594    align memory references, as is done on the Alpha.
595
596    We recognize the following cases of non-conflicting memory:
597
598         (1) addresses involving the frame pointer cannot conflict
599             with addresses involving static variables.
600         (2) static variables with different addresses cannot conflict.
601
602    Nice to notice that varying addresses cannot conflict with fp if no
603    local variables had their addresses taken, but that's too hard now.  */
604
605
606 static int
607 memrefs_conflict_p (xsize, x, ysize, y, c)
608      register rtx x, y;
609      int xsize, ysize;
610      HOST_WIDE_INT c;
611 {
612   if (GET_CODE (x) == HIGH)
613     x = XEXP (x, 0);
614   else if (GET_CODE (x) == LO_SUM)
615     x = XEXP (x, 1);
616   else
617     x = canon_rtx (x);
618   if (GET_CODE (y) == HIGH)
619     y = XEXP (y, 0);
620   else if (GET_CODE (y) == LO_SUM)
621     y = XEXP (y, 1);
622   else
623     y = canon_rtx (y);
624
625   if (rtx_equal_for_memref_p (x, y))
626     {
627       if (xsize <= 0 || ysize <= 0)
628         return 1;
629       if (c >= 0 && xsize > c)
630         return 1;
631       if (c < 0 && ysize+c > 0)
632         return 1;
633       return 0;
634     }
635
636   if (y == frame_pointer_rtx || y == hard_frame_pointer_rtx
637       || y == stack_pointer_rtx || y == arg_pointer_rtx)
638     {
639       rtx t = y;
640       int tsize = ysize;
641       y = x; ysize = xsize;
642       x = t; xsize = tsize;
643     }
644
645   if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
646       || x == stack_pointer_rtx || x == arg_pointer_rtx)
647     {
648       rtx y1;
649
650       if (CONSTANT_P (y))
651         return 0;
652
653       if (GET_CODE (y) == PLUS
654           && canon_rtx (XEXP (y, 0)) == x
655           && (y1 = canon_rtx (XEXP (y, 1)))
656           && GET_CODE (y1) == CONST_INT)
657         {
658           c += INTVAL (y1);
659           return (xsize <= 0 || ysize <= 0
660                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
661         }
662
663       if (GET_CODE (y) == PLUS
664           && (y1 = canon_rtx (XEXP (y, 0)))
665           && CONSTANT_P (y1))
666         return 0;
667
668       return 1;
669     }
670
671   if (GET_CODE (x) == PLUS)
672     {
673       /* The fact that X is canonicalized means that this
674          PLUS rtx is canonicalized.  */
675       rtx x0 = XEXP (x, 0);
676       rtx x1 = XEXP (x, 1);
677
678       if (GET_CODE (y) == PLUS)
679         {
680           /* The fact that Y is canonicalized means that this
681              PLUS rtx is canonicalized.  */
682           rtx y0 = XEXP (y, 0);
683           rtx y1 = XEXP (y, 1);
684
685           if (rtx_equal_for_memref_p (x1, y1))
686             return memrefs_conflict_p (xsize, x0, ysize, y0, c);
687           if (rtx_equal_for_memref_p (x0, y0))
688             return memrefs_conflict_p (xsize, x1, ysize, y1, c);
689           if (GET_CODE (x1) == CONST_INT)
690             if (GET_CODE (y1) == CONST_INT)
691               return memrefs_conflict_p (xsize, x0, ysize, y0,
692                                          c - INTVAL (x1) + INTVAL (y1));
693             else
694               return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
695           else if (GET_CODE (y1) == CONST_INT)
696             return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
697
698           /* Handle case where we cannot understand iteration operators,
699              but we notice that the base addresses are distinct objects.  */
700           /* ??? Is this still necessary? */
701           x = find_symbolic_term (x);
702           if (x == 0)
703             return 1;
704           y = find_symbolic_term (y);
705           if (y == 0)
706             return 1;
707           return rtx_equal_for_memref_p (x, y);
708         }
709       else if (GET_CODE (x1) == CONST_INT)
710         return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
711     }
712   else if (GET_CODE (y) == PLUS)
713     {
714       /* The fact that Y is canonicalized means that this
715          PLUS rtx is canonicalized.  */
716       rtx y0 = XEXP (y, 0);
717       rtx y1 = XEXP (y, 1);
718
719       if (GET_CODE (y1) == CONST_INT)
720         return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
721       else
722         return 1;
723     }
724
725   if (GET_CODE (x) == GET_CODE (y))
726     switch (GET_CODE (x))
727       {
728       case MULT:
729         {
730           /* Handle cases where we expect the second operands to be the
731              same, and check only whether the first operand would conflict
732              or not.  */
733           rtx x0, y0;
734           rtx x1 = canon_rtx (XEXP (x, 1));
735           rtx y1 = canon_rtx (XEXP (y, 1));
736           if (! rtx_equal_for_memref_p (x1, y1))
737             return 1;
738           x0 = canon_rtx (XEXP (x, 0));
739           y0 = canon_rtx (XEXP (y, 0));
740           if (rtx_equal_for_memref_p (x0, y0))
741             return (xsize == 0 || ysize == 0
742                     || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
743
744           /* Can't properly adjust our sizes.  */
745           if (GET_CODE (x1) != CONST_INT)
746             return 1;
747           xsize /= INTVAL (x1);
748           ysize /= INTVAL (x1);
749           c /= INTVAL (x1);
750           return memrefs_conflict_p (xsize, x0, ysize, y0, c);
751         }
752       }
753
754   /* Treat an access through an AND (e.g. a subword access on an Alpha)
755      as an access with indeterminate size.  */
756   if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
757     return memrefs_conflict_p (-1, XEXP (x, 0), ysize, y, c);
758   if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
759     {
760       /* XXX: If we are indexing far enough into the array/structure, we
761          may yet be able to determine that we can not overlap.  But we 
762          also need to that we are far enough from the end not to overlap
763          a following reference, so we do nothing for now.  */
764       return memrefs_conflict_p (xsize, x, -1, XEXP (y, 0), c);
765     }
766
767   if (CONSTANT_P (x))
768     {
769       if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
770         {
771           c += (INTVAL (y) - INTVAL (x));
772           return (xsize <= 0 || ysize <= 0
773                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
774         }
775
776       if (GET_CODE (x) == CONST)
777         {
778           if (GET_CODE (y) == CONST)
779             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
780                                        ysize, canon_rtx (XEXP (y, 0)), c);
781           else
782             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
783                                        ysize, y, c);
784         }
785       if (GET_CODE (y) == CONST)
786         return memrefs_conflict_p (xsize, x, ysize,
787                                    canon_rtx (XEXP (y, 0)), c);
788
789       if (CONSTANT_P (y))
790         return (xsize < 0 || ysize < 0
791                 || (rtx_equal_for_memref_p (x, y)
792                     && (xsize == 0 || ysize == 0
793                         || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
794
795       return 1;
796     }
797   return 1;
798 }
799
800 /* Functions to compute memory dependencies.
801
802    Since we process the insns in execution order, we can build tables
803    to keep track of what registers are fixed (and not aliased), what registers
804    are varying in known ways, and what registers are varying in unknown
805    ways.
806
807    If both memory references are volatile, then there must always be a
808    dependence between the two references, since their order can not be
809    changed.  A volatile and non-volatile reference can be interchanged
810    though. 
811
812    A MEM_IN_STRUCT reference at a non-QImode non-AND varying address can never
813    conflict with a non-MEM_IN_STRUCT reference at a fixed address.   We must
814    allow QImode aliasing because the ANSI C standard allows character
815    pointers to alias anything.  We are assuming that characters are
816    always QImode here.  We also must allow AND addresses, because they may
817    generate accesses outside the object being referenced.  This is used to
818    generate aligned addresses from unaligned addresses, for instance, the
819    alpha storeqi_unaligned pattern.  */
820
821 /* Read dependence: X is read after read in MEM takes place.  There can
822    only be a dependence here if both reads are volatile.  */
823
824 int
825 read_dependence (mem, x)
826      rtx mem;
827      rtx x;
828 {
829   return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
830 }
831
832 /* True dependence: X is read after store in MEM takes place.  */
833
834 int
835 true_dependence (mem, mem_mode, x, varies)
836      rtx mem;
837      enum machine_mode mem_mode;
838      rtx x;
839      int (*varies)();
840 {
841   rtx x_addr, mem_addr;
842
843   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
844     return 1;
845
846   x_addr = XEXP (x, 0);
847   mem_addr = XEXP (mem, 0);
848
849   if (flag_alias_check && ! base_alias_check (x_addr, mem_addr))
850     return 0;
851
852   /* If X is an unchanging read, then it can't possibly conflict with any
853      non-unchanging store.  It may conflict with an unchanging write though,
854      because there may be a single store to this address to initialize it.
855      Just fall through to the code below to resolve the case where we have
856      both an unchanging read and an unchanging write.  This won't handle all
857      cases optimally, but the possible performance loss should be
858      negligible.  */
859   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
860     return 0;
861
862   x_addr = canon_rtx (x_addr);
863   mem_addr = canon_rtx (mem_addr);
864   if (mem_mode == VOIDmode)
865     mem_mode = GET_MODE (mem);
866
867   if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
868                             SIZE_FOR_MODE (x), x_addr, 0))
869     return 0;
870
871   /* If both references are struct references, or both are not, nothing
872      is known about aliasing.
873
874      If either reference is QImode or BLKmode, ANSI C permits aliasing.
875
876      If both addresses are constant, or both are not, nothing is known
877      about aliasing.  */
878   if (MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (mem)
879       || mem_mode == QImode || mem_mode == BLKmode
880       || GET_MODE (x) == QImode || GET_MODE (x) == BLKmode
881       || GET_CODE (x_addr) == AND || GET_CODE (mem_addr) == AND
882       || varies (x_addr) == varies (mem_addr))
883     return 1;
884
885   /* One memory reference is to a constant address, one is not.
886      One is to a structure, the other is not.
887
888      If either memory reference is a variable structure the other is a
889      fixed scalar and there is no aliasing.  */
890   if ((MEM_IN_STRUCT_P (mem) && varies (mem_addr))
891       || (MEM_IN_STRUCT_P (x) && varies (x_addr)))
892     return 0;
893
894   return 1;
895 }
896
897 /* Anti dependence: X is written after read in MEM takes place.  */
898
899 int
900 anti_dependence (mem, x)
901      rtx mem;
902      rtx x;
903 {
904   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
905     return 1;
906
907   if (flag_alias_check && ! base_alias_check (XEXP (x, 0), XEXP (mem, 0)))
908     return 0;
909
910   /* If MEM is an unchanging read, then it can't possibly conflict with
911      the store to X, because there is at most one store to MEM, and it must
912      have occurred somewhere before MEM.  */
913   x = canon_rtx (x);
914   mem = canon_rtx (mem);
915   if (RTX_UNCHANGING_P (mem))
916     return 0;
917
918   return (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
919                               SIZE_FOR_MODE (x), XEXP (x, 0), 0)
920           && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
921                 && GET_MODE (mem) != QImode
922                 && GET_CODE (XEXP (mem, 0)) != AND
923                 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
924           && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
925                 && GET_MODE (x) != QImode
926                 && GET_CODE (XEXP (x, 0)) != AND
927                 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem)));
928 }
929
930 /* Output dependence: X is written after store in MEM takes place.  */
931
932 int
933 output_dependence (mem, x)
934      register rtx mem;
935      register rtx x;
936 {
937   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
938     return 1;
939
940   if (flag_alias_check && !base_alias_check (XEXP (x, 0), XEXP (mem, 0)))
941     return 0;
942
943   x = canon_rtx (x);
944   mem = canon_rtx (mem);
945   return (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
946                               SIZE_FOR_MODE (x), XEXP (x, 0), 0)
947           && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
948                 && GET_MODE (mem) != QImode
949                 && GET_CODE (XEXP (mem, 0)) != AND
950                 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
951           && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
952                 && GET_MODE (x) != QImode
953                 && GET_CODE (XEXP (x, 0)) != AND
954                 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem)));
955 }
956
957 void
958 init_alias_analysis ()
959 {
960   int maxreg = max_reg_num ();
961   int changed;
962   register int i;
963   register rtx insn;
964   rtx note;
965   rtx set;
966
967   reg_known_value_size = maxreg;
968
969   reg_known_value
970     = (rtx *) oballoc ((maxreg - FIRST_PSEUDO_REGISTER) * sizeof (rtx))
971     - FIRST_PSEUDO_REGISTER;
972   reg_known_equiv_p =
973     oballoc (maxreg - FIRST_PSEUDO_REGISTER) - FIRST_PSEUDO_REGISTER;
974   bzero ((char *) (reg_known_value + FIRST_PSEUDO_REGISTER),
975          (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
976   bzero (reg_known_equiv_p + FIRST_PSEUDO_REGISTER,
977          (maxreg - FIRST_PSEUDO_REGISTER) * sizeof (char));
978
979   if (flag_alias_check)
980     {
981       /* Overallocate reg_base_value to allow some growth during loop
982          optimization.  Loop unrolling can create a large number of
983          registers.  */
984       reg_base_value_size = maxreg * 2;
985       reg_base_value = (rtx *)oballoc (reg_base_value_size * sizeof (rtx));
986       new_reg_base_value = (rtx *)alloca (reg_base_value_size * sizeof (rtx));
987       reg_seen = (char *)alloca (reg_base_value_size);
988       bzero ((char *) reg_base_value, reg_base_value_size * sizeof (rtx));
989     }
990
991   /* The basic idea is that each pass through this loop will use the
992      "constant" information from the previous pass to propagate alias
993      information through another level of assignments.
994
995      This could get expensive if the assignment chains are long.  Maybe
996      we should throttle the number of iterations, possibly based on
997      the optimization level.
998
999      We could propagate more information in the first pass by making use
1000      of REG_N_SETS to determine immediately that the alias information
1001      for a pseudo is "constant".  */
1002   changed = 1;
1003   while (changed)
1004     {
1005       /* Assume nothing will change this iteration of the loop.  */
1006       changed = 0;
1007
1008       /* We want to assign the same IDs each iteration of this loop, so
1009          start counting from zero each iteration of the loop.  */
1010       unique_id = 0;
1011
1012       /* We're at the start of the funtion each iteration through the
1013          loop, so we're copying arguments.  */
1014       copying_arguments = 1;
1015
1016       /* Only perform initialization of the arrays if we're actually
1017          performing alias analysis. */
1018       if (flag_alias_check)
1019         {
1020           /* Wipe the potential alias information clean for this pass.  */
1021           bzero ((char *) new_reg_base_value,
1022                  reg_base_value_size * sizeof (rtx));
1023
1024           /* Wipe the reg_seen array clean.  */
1025           bzero ((char *) reg_seen, reg_base_value_size);
1026
1027           /* Mark all hard registers which may contain an address.
1028              The stack, frame and argument pointers may contain an address.
1029              An argument register which can hold a Pmode value may contain
1030              an address even if it is not in BASE_REGS.
1031
1032              The address expression is VOIDmode for an argument and
1033              Pmode for other registers.  */
1034 #ifndef OUTGOING_REGNO
1035 #define OUTGOING_REGNO(N) N
1036 #endif
1037           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1038             /* Check whether this register can hold an incoming pointer
1039                argument.  FUNCTION_ARG_REGNO_P tests outgoing register
1040                numbers, so translate if necessary due to register windows. */
1041             if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
1042                 && HARD_REGNO_MODE_OK (i, Pmode))
1043               new_reg_base_value[i] = gen_rtx (ADDRESS, VOIDmode,
1044                                                gen_rtx (REG, Pmode, i));
1045
1046           new_reg_base_value[STACK_POINTER_REGNUM]
1047             = gen_rtx (ADDRESS, Pmode, stack_pointer_rtx);
1048           new_reg_base_value[ARG_POINTER_REGNUM]
1049             = gen_rtx (ADDRESS, Pmode, arg_pointer_rtx);
1050           new_reg_base_value[FRAME_POINTER_REGNUM]
1051             = gen_rtx (ADDRESS, Pmode, frame_pointer_rtx);
1052 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1053           new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
1054             = gen_rtx (ADDRESS, Pmode, hard_frame_pointer_rtx);
1055 #endif
1056           if (struct_value_incoming_rtx
1057               && GET_CODE (struct_value_incoming_rtx) == REG)
1058             new_reg_base_value[REGNO (struct_value_incoming_rtx)]
1059               = gen_rtx (ADDRESS, Pmode, struct_value_incoming_rtx);
1060
1061           if (static_chain_rtx
1062               && GET_CODE (static_chain_rtx) == REG)
1063             new_reg_base_value[REGNO (static_chain_rtx)]
1064               = gen_rtx (ADDRESS, Pmode, static_chain_rtx);
1065         }
1066
1067       /* Walk the insns adding values to the new_reg_base_value array.  */
1068       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1069         {
1070           if (flag_alias_check && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1071             {
1072               /* If this insn has a noalias note, process it,  Otherwise,
1073                  scan for sets.  A simple set will have no side effects
1074                  which could change the base value of any other register. */
1075               rtx noalias_note;
1076               if (GET_CODE (PATTERN (insn)) == SET
1077                   && (noalias_note = find_reg_note (insn,
1078                                                     REG_NOALIAS, NULL_RTX)))
1079                 record_set (SET_DEST (PATTERN (insn)), 0);
1080               else
1081                 note_stores (PATTERN (insn), record_set);
1082             }
1083           else if (GET_CODE (insn) == NOTE
1084                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
1085             copying_arguments = 0;
1086
1087           if ((set = single_set (insn)) != 0
1088               && GET_CODE (SET_DEST (set)) == REG
1089               && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
1090               && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1091                    && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
1092                   || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1093               && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1094             {
1095               int regno = REGNO (SET_DEST (set));
1096               reg_known_value[regno] = XEXP (note, 0);
1097               reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
1098             }
1099         }
1100
1101       /* Now propagate values from new_reg_base_value to reg_base_value.  */
1102       if (flag_alias_check)
1103         for (i = 0; i < reg_base_value_size; i++)
1104           {
1105             if (new_reg_base_value[i]
1106                 && new_reg_base_value[i] != reg_base_value[i]
1107                 && !rtx_equal_p (new_reg_base_value[i], reg_base_value[i]))
1108               {
1109                 reg_base_value[i] = new_reg_base_value[i];
1110                 changed = 1;
1111               }
1112           }
1113     }
1114
1115   /* Fill in the remaining entries.  */
1116   for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
1117     if (reg_known_value[i] == 0)
1118       reg_known_value[i] = regno_reg_rtx[i];
1119
1120   if (! flag_alias_check)
1121     return;
1122
1123   /* Simplify the reg_base_value array so that no register refers to
1124      another register, except to special registers indirectly through
1125      ADDRESS expressions.
1126
1127      In theory this loop can take as long as O(registers^2), but unless
1128      there are very long dependency chains it will run in close to linear
1129      time.  */
1130   do
1131     {
1132       changed = 0;
1133       for (i = 0; i < reg_base_value_size; i++)
1134         {
1135           rtx base = reg_base_value[i];
1136           if (base && GET_CODE (base) == REG)
1137             {
1138               int base_regno = REGNO (base);
1139               if (base_regno == i)              /* register set from itself */
1140                 reg_base_value[i] = 0;
1141               else
1142                 reg_base_value[i] = reg_base_value[base_regno];
1143               changed = 1;
1144             }
1145         }
1146     }
1147   while (changed);
1148
1149   new_reg_base_value = 0;
1150   reg_seen = 0;
1151 }
1152
1153 void
1154 end_alias_analysis ()
1155 {
1156   reg_known_value = 0;
1157   reg_base_value = 0;
1158   reg_base_value_size = 0;
1159 }