* Make-lang.in (DEMANGLER_INSTALL_NAME, DEMANGLER_CROSS_NAME): New
[official-gcc.git] / gcc / alias.c
blob0b5989ca9491e58c90a3972ab2444412d6d821c3
1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
3 Contributed by John Carr (jfc@mit.edu).
5 This file is part of GNU CC.
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.
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.
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. */
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"
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));
35 /* Set up all info needed to perform alias analysis on memory references. */
37 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
39 /* Cap the number of passes we make over the insns propagating alias
40 information through set chains.
42 10 is a completely arbitrary choice. */
43 #define MAX_ALIAS_LOOP_PASSES 10
45 /* reg_base_value[N] gives an address to which register N is related.
46 If all sets after the first add or subtract to the current value
47 or otherwise modify it so it does not point to a different top level
48 object, reg_base_value[N] is equal to the address part of the source
49 of the first set.
51 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
52 expressions represent certain special values: function arguments and
53 the stack, frame, and argument pointers. The contents of an address
54 expression are not used (but they are descriptive for debugging);
55 only the address and mode matter. Pointer equality, not rtx_equal_p,
56 determines whether two ADDRESS expressions refer to the same base
57 address. The mode determines whether it is a function argument or
58 other special value. */
60 rtx *reg_base_value;
61 rtx *new_reg_base_value;
62 unsigned int reg_base_value_size; /* size of reg_base_value array */
63 #define REG_BASE_VALUE(X) \
64 (REGNO (X) < reg_base_value_size ? reg_base_value[REGNO (X)] : 0)
66 /* Vector indexed by N giving the initial (unchanging) value known
67 for pseudo-register N. */
68 rtx *reg_known_value;
70 /* Indicates number of valid entries in reg_known_value. */
71 static int reg_known_value_size;
73 /* Vector recording for each reg_known_value whether it is due to a
74 REG_EQUIV note. Future passes (viz., reload) may replace the
75 pseudo with the equivalent expression and so we account for the
76 dependences that would be introduced if that happens. */
77 /* ??? This is a problem only on the Convex. The REG_EQUIV notes created in
78 assign_parms mention the arg pointer, and there are explicit insns in the
79 RTL that modify the arg pointer. Thus we must ensure that such insns don't
80 get scheduled across each other because that would invalidate the REG_EQUIV
81 notes. One could argue that the REG_EQUIV notes are wrong, but solving
82 the problem in the scheduler will likely give better code, so we do it
83 here. */
84 char *reg_known_equiv_p;
86 /* True when scanning insns from the start of the rtl to the
87 NOTE_INSN_FUNCTION_BEG note. */
89 static int copying_arguments;
91 /* Inside SRC, the source of a SET, find a base address. */
93 static rtx
94 find_base_value (src)
95 register rtx src;
97 switch (GET_CODE (src))
99 case SYMBOL_REF:
100 case LABEL_REF:
101 return src;
103 case REG:
104 /* At the start of a function argument registers have known base
105 values which may be lost later. Returning an ADDRESS
106 expression here allows optimization based on argument values
107 even when the argument registers are used for other purposes. */
108 if (REGNO (src) < FIRST_PSEUDO_REGISTER && copying_arguments)
109 return new_reg_base_value[REGNO (src)];
111 /* If a pseudo has a known base value, return it. Do not do this
112 for hard regs since it can result in a circular dependency
113 chain for registers which have values at function entry.
115 The test above is not sufficient because the scheduler may move
116 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
117 if (REGNO (src) >= FIRST_PSEUDO_REGISTER
118 && reg_base_value[REGNO (src)])
119 return reg_base_value[REGNO (src)];
121 return src;
123 case MEM:
124 /* Check for an argument passed in memory. Only record in the
125 copying-arguments block; it is too hard to track changes
126 otherwise. */
127 if (copying_arguments
128 && (XEXP (src, 0) == arg_pointer_rtx
129 || (GET_CODE (XEXP (src, 0)) == PLUS
130 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
131 return gen_rtx_ADDRESS (VOIDmode, src);
132 return 0;
134 case CONST:
135 src = XEXP (src, 0);
136 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
137 break;
138 /* fall through */
140 case PLUS:
141 case MINUS:
143 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
145 /* If either operand is a REG, then see if we already have
146 a known value for it. */
147 if (GET_CODE (src_0) == REG)
149 temp = find_base_value (src_0);
150 if (temp)
151 src_0 = temp;
154 if (GET_CODE (src_1) == REG)
156 temp = find_base_value (src_1);
157 if (temp)
158 src_1 = temp;
161 /* Guess which operand is the base address.
163 If either operand is a symbol, then it is the base. If
164 either operand is a CONST_INT, then the other is the base. */
166 if (GET_CODE (src_1) == CONST_INT
167 || GET_CODE (src_0) == SYMBOL_REF
168 || GET_CODE (src_0) == LABEL_REF
169 || GET_CODE (src_0) == CONST)
170 return find_base_value (src_0);
172 if (GET_CODE (src_0) == CONST_INT
173 || GET_CODE (src_1) == SYMBOL_REF
174 || GET_CODE (src_1) == LABEL_REF
175 || GET_CODE (src_1) == CONST)
176 return find_base_value (src_1);
178 /* This might not be necessary anymore.
180 If either operand is a REG that is a known pointer, then it
181 is the base. */
182 if (GET_CODE (src_0) == REG && REGNO_POINTER_FLAG (REGNO (src_0)))
183 return find_base_value (src_0);
185 if (GET_CODE (src_1) == REG && REGNO_POINTER_FLAG (REGNO (src_1)))
186 return find_base_value (src_1);
188 return 0;
191 case LO_SUM:
192 /* The standard form is (lo_sum reg sym) so look only at the
193 second operand. */
194 return find_base_value (XEXP (src, 1));
196 case AND:
197 /* If the second operand is constant set the base
198 address to the first operand. */
199 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
200 return find_base_value (XEXP (src, 0));
201 return 0;
203 case HIGH:
204 return find_base_value (XEXP (src, 0));
206 default:
207 break;
210 return 0;
213 /* Called from init_alias_analysis indirectly through note_stores. */
215 /* while scanning insns to find base values, reg_seen[N] is nonzero if
216 register N has been set in this function. */
217 static char *reg_seen;
219 /* Addresses which are known not to alias anything else are identified
220 by a unique integer. */
221 static int unique_id;
223 static void
224 record_set (dest, set)
225 rtx dest, set;
227 register int regno;
228 rtx src;
230 if (GET_CODE (dest) != REG)
231 return;
233 regno = REGNO (dest);
235 if (set)
237 /* A CLOBBER wipes out any old value but does not prevent a previously
238 unset register from acquiring a base address (i.e. reg_seen is not
239 set). */
240 if (GET_CODE (set) == CLOBBER)
242 new_reg_base_value[regno] = 0;
243 return;
245 src = SET_SRC (set);
247 else
249 if (reg_seen[regno])
251 new_reg_base_value[regno] = 0;
252 return;
254 reg_seen[regno] = 1;
255 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
256 GEN_INT (unique_id++));
257 return;
260 /* This is not the first set. If the new value is not related to the
261 old value, forget the base value. Note that the following code is
262 not detected:
263 extern int x, y; int *p = &x; p += (&y-&x);
264 ANSI C does not allow computing the difference of addresses
265 of distinct top level objects. */
266 if (new_reg_base_value[regno])
267 switch (GET_CODE (src))
269 case LO_SUM:
270 case PLUS:
271 case MINUS:
272 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
273 new_reg_base_value[regno] = 0;
274 break;
275 case AND:
276 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
277 new_reg_base_value[regno] = 0;
278 break;
279 default:
280 new_reg_base_value[regno] = 0;
281 break;
283 /* If this is the first set of a register, record the value. */
284 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
285 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
286 new_reg_base_value[regno] = find_base_value (src);
288 reg_seen[regno] = 1;
291 /* Called from loop optimization when a new pseudo-register is created. */
292 void
293 record_base_value (regno, val)
294 int regno;
295 rtx val;
297 if (regno >= reg_base_value_size)
298 return;
299 if (GET_CODE (val) == REG)
301 if (REGNO (val) < reg_base_value_size)
302 reg_base_value[regno] = reg_base_value[REGNO (val)];
303 return;
305 reg_base_value[regno] = find_base_value (val);
308 static rtx
309 canon_rtx (x)
310 rtx x;
312 /* Recursively look for equivalences. */
313 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
314 && REGNO (x) < reg_known_value_size)
315 return reg_known_value[REGNO (x)] == x
316 ? x : canon_rtx (reg_known_value[REGNO (x)]);
317 else if (GET_CODE (x) == PLUS)
319 rtx x0 = canon_rtx (XEXP (x, 0));
320 rtx x1 = canon_rtx (XEXP (x, 1));
322 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
324 /* We can tolerate LO_SUMs being offset here; these
325 rtl are used for nothing other than comparisons. */
326 if (GET_CODE (x0) == CONST_INT)
327 return plus_constant_for_output (x1, INTVAL (x0));
328 else if (GET_CODE (x1) == CONST_INT)
329 return plus_constant_for_output (x0, INTVAL (x1));
330 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
333 /* This gives us much better alias analysis when called from
334 the loop optimizer. Note we want to leave the original
335 MEM alone, but need to return the canonicalized MEM with
336 all the flags with their original values. */
337 else if (GET_CODE (x) == MEM)
339 rtx addr = canon_rtx (XEXP (x, 0));
340 if (addr != XEXP (x, 0))
342 rtx new = gen_rtx_MEM (GET_MODE (x), addr);
343 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
344 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
345 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
346 x = new;
349 return x;
352 /* Return 1 if X and Y are identical-looking rtx's.
354 We use the data in reg_known_value above to see if two registers with
355 different numbers are, in fact, equivalent. */
357 static int
358 rtx_equal_for_memref_p (x, y)
359 rtx x, y;
361 register int i;
362 register int j;
363 register enum rtx_code code;
364 register char *fmt;
366 if (x == 0 && y == 0)
367 return 1;
368 if (x == 0 || y == 0)
369 return 0;
370 x = canon_rtx (x);
371 y = canon_rtx (y);
373 if (x == y)
374 return 1;
376 code = GET_CODE (x);
377 /* Rtx's of different codes cannot be equal. */
378 if (code != GET_CODE (y))
379 return 0;
381 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
382 (REG:SI x) and (REG:HI x) are NOT equivalent. */
384 if (GET_MODE (x) != GET_MODE (y))
385 return 0;
387 /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively. */
389 if (code == REG)
390 return REGNO (x) == REGNO (y);
391 if (code == LABEL_REF)
392 return XEXP (x, 0) == XEXP (y, 0);
393 if (code == SYMBOL_REF)
394 return XSTR (x, 0) == XSTR (y, 0);
396 /* For commutative operations, the RTX match if the operand match in any
397 order. Also handle the simple binary and unary cases without a loop. */
398 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
399 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
400 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
401 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
402 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
403 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
404 return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
405 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
406 else if (GET_RTX_CLASS (code) == '1')
407 return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
409 /* Compare the elements. If any pair of corresponding elements
410 fail to match, return 0 for the whole things. */
412 fmt = GET_RTX_FORMAT (code);
413 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
415 switch (fmt[i])
417 case 'w':
418 if (XWINT (x, i) != XWINT (y, i))
419 return 0;
420 break;
422 case 'n':
423 case 'i':
424 if (XINT (x, i) != XINT (y, i))
425 return 0;
426 break;
428 case 'V':
429 case 'E':
430 /* Two vectors must have the same length. */
431 if (XVECLEN (x, i) != XVECLEN (y, i))
432 return 0;
434 /* And the corresponding elements must match. */
435 for (j = 0; j < XVECLEN (x, i); j++)
436 if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
437 return 0;
438 break;
440 case 'e':
441 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
442 return 0;
443 break;
445 case 'S':
446 case 's':
447 if (strcmp (XSTR (x, i), XSTR (y, i)))
448 return 0;
449 break;
451 case 'u':
452 /* These are just backpointers, so they don't matter. */
453 break;
455 case '0':
456 break;
458 /* It is believed that rtx's at this level will never
459 contain anything but integers and other rtx's,
460 except for within LABEL_REFs and SYMBOL_REFs. */
461 default:
462 abort ();
465 return 1;
468 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
469 X and return it, or return 0 if none found. */
471 static rtx
472 find_symbolic_term (x)
473 rtx x;
475 register int i;
476 register enum rtx_code code;
477 register char *fmt;
479 code = GET_CODE (x);
480 if (code == SYMBOL_REF || code == LABEL_REF)
481 return x;
482 if (GET_RTX_CLASS (code) == 'o')
483 return 0;
485 fmt = GET_RTX_FORMAT (code);
486 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
488 rtx t;
490 if (fmt[i] == 'e')
492 t = find_symbolic_term (XEXP (x, i));
493 if (t != 0)
494 return t;
496 else if (fmt[i] == 'E')
497 break;
499 return 0;
502 static rtx
503 find_base_term (x)
504 register rtx x;
506 switch (GET_CODE (x))
508 case REG:
509 return REG_BASE_VALUE (x);
511 case HIGH:
512 return find_base_term (XEXP (x, 0));
514 case PRE_INC:
515 case PRE_DEC:
516 case POST_INC:
517 case POST_DEC:
518 return find_base_term (XEXP (x, 0));
520 case CONST:
521 x = XEXP (x, 0);
522 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
523 return 0;
524 /* fall through */
525 case LO_SUM:
526 case PLUS:
527 case MINUS:
529 rtx tmp = find_base_term (XEXP (x, 0));
530 if (tmp)
531 return tmp;
532 return find_base_term (XEXP (x, 1));
535 case AND:
536 if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
537 return REG_BASE_VALUE (XEXP (x, 0));
538 return 0;
540 case SYMBOL_REF:
541 case LABEL_REF:
542 return x;
544 default:
545 return 0;
549 /* Return 0 if the addresses X and Y are known to point to different
550 objects, 1 if they might be pointers to the same object. */
552 static int
553 base_alias_check (x, y)
554 rtx x, y;
556 rtx x_base = find_base_term (x);
557 rtx y_base = find_base_term (y);
559 /* If the address itself has no known base see if a known equivalent
560 value has one. If either address still has no known base, nothing
561 is known about aliasing. */
562 if (x_base == 0)
564 rtx x_c;
565 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
566 return 1;
567 x_base = find_base_term (x_c);
568 if (x_base == 0)
569 return 1;
572 if (y_base == 0)
574 rtx y_c;
575 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
576 return 1;
577 y_base = find_base_term (y_c);
578 if (y_base == 0)
579 return 1;
582 /* If the base addresses are equal nothing is known about aliasing. */
583 if (rtx_equal_p (x_base, y_base))
584 return 1;
586 /* The base addresses of the read and write are different
587 expressions. If they are both symbols and they are not accessed
588 via AND, there is no conflict. */
589 /* XXX: We can bring knowledge of object alignment and offset into
590 play here. For example, on alpha, "char a, b;" can alias one
591 another, though "char a; long b;" cannot. Similarly, offsets
592 into strutures may be brought into play. Given "char a, b[40];",
593 a and b[1] may overlap, but a and b[20] do not. */
594 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
596 return GET_CODE (x) == AND || GET_CODE (y) == AND;
599 /* If one address is a stack reference there can be no alias:
600 stack references using different base registers do not alias,
601 a stack reference can not alias a parameter, and a stack reference
602 can not alias a global. */
603 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
604 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
605 return 0;
607 if (! flag_argument_noalias)
608 return 1;
610 if (flag_argument_noalias > 1)
611 return 0;
613 /* Weak noalias assertion (arguments are distinct, but may match globals). */
614 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
617 /* Return nonzero if X and Y (memory addresses) could reference the
618 same location in memory. C is an offset accumulator. When
619 C is nonzero, we are testing aliases between X and Y + C.
620 XSIZE is the size in bytes of the X reference,
621 similarly YSIZE is the size in bytes for Y.
623 If XSIZE or YSIZE is zero, we do not know the amount of memory being
624 referenced (the reference was BLKmode), so make the most pessimistic
625 assumptions.
627 If XSIZE or YSIZE is negative, we may access memory outside the object
628 being referenced as a side effect. This can happen when using AND to
629 align memory references, as is done on the Alpha.
631 Nice to notice that varying addresses cannot conflict with fp if no
632 local variables had their addresses taken, but that's too hard now. */
635 static int
636 memrefs_conflict_p (xsize, x, ysize, y, c)
637 register rtx x, y;
638 int xsize, ysize;
639 HOST_WIDE_INT c;
641 if (GET_CODE (x) == HIGH)
642 x = XEXP (x, 0);
643 else if (GET_CODE (x) == LO_SUM)
644 x = XEXP (x, 1);
645 else
646 x = canon_rtx (x);
647 if (GET_CODE (y) == HIGH)
648 y = XEXP (y, 0);
649 else if (GET_CODE (y) == LO_SUM)
650 y = XEXP (y, 1);
651 else
652 y = canon_rtx (y);
654 if (rtx_equal_for_memref_p (x, y))
656 if (xsize <= 0 || ysize <= 0)
657 return 1;
658 if (c >= 0 && xsize > c)
659 return 1;
660 if (c < 0 && ysize+c > 0)
661 return 1;
662 return 0;
665 /* This code used to check for conflicts involving stack references and
666 globals but the base address alias code now handles these cases. */
668 if (GET_CODE (x) == PLUS)
670 /* The fact that X is canonicalized means that this
671 PLUS rtx is canonicalized. */
672 rtx x0 = XEXP (x, 0);
673 rtx x1 = XEXP (x, 1);
675 if (GET_CODE (y) == PLUS)
677 /* The fact that Y is canonicalized means that this
678 PLUS rtx is canonicalized. */
679 rtx y0 = XEXP (y, 0);
680 rtx y1 = XEXP (y, 1);
682 if (rtx_equal_for_memref_p (x1, y1))
683 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
684 if (rtx_equal_for_memref_p (x0, y0))
685 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
686 if (GET_CODE (x1) == CONST_INT)
687 if (GET_CODE (y1) == CONST_INT)
688 return memrefs_conflict_p (xsize, x0, ysize, y0,
689 c - INTVAL (x1) + INTVAL (y1));
690 else
691 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
692 else if (GET_CODE (y1) == CONST_INT)
693 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
695 return 1;
697 else if (GET_CODE (x1) == CONST_INT)
698 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
700 else if (GET_CODE (y) == PLUS)
702 /* The fact that Y is canonicalized means that this
703 PLUS rtx is canonicalized. */
704 rtx y0 = XEXP (y, 0);
705 rtx y1 = XEXP (y, 1);
707 if (GET_CODE (y1) == CONST_INT)
708 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
709 else
710 return 1;
713 if (GET_CODE (x) == GET_CODE (y))
714 switch (GET_CODE (x))
716 case MULT:
718 /* Handle cases where we expect the second operands to be the
719 same, and check only whether the first operand would conflict
720 or not. */
721 rtx x0, y0;
722 rtx x1 = canon_rtx (XEXP (x, 1));
723 rtx y1 = canon_rtx (XEXP (y, 1));
724 if (! rtx_equal_for_memref_p (x1, y1))
725 return 1;
726 x0 = canon_rtx (XEXP (x, 0));
727 y0 = canon_rtx (XEXP (y, 0));
728 if (rtx_equal_for_memref_p (x0, y0))
729 return (xsize == 0 || ysize == 0
730 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
732 /* Can't properly adjust our sizes. */
733 if (GET_CODE (x1) != CONST_INT)
734 return 1;
735 xsize /= INTVAL (x1);
736 ysize /= INTVAL (x1);
737 c /= INTVAL (x1);
738 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
741 default:
742 break;
745 /* Treat an access through an AND (e.g. a subword access on an Alpha)
746 as an access with indeterminate size.
747 ??? Could instead convert an n byte reference at (and x y) to an
748 n-y byte reference at (plus x y). */
749 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
750 return memrefs_conflict_p (-1, XEXP (x, 0), ysize, y, c);
751 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
753 /* XXX: If we are indexing far enough into the array/structure, we
754 may yet be able to determine that we can not overlap. But we
755 also need to that we are far enough from the end not to overlap
756 a following reference, so we do nothing for now. */
757 return memrefs_conflict_p (xsize, x, -1, XEXP (y, 0), c);
760 if (CONSTANT_P (x))
762 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
764 c += (INTVAL (y) - INTVAL (x));
765 return (xsize <= 0 || ysize <= 0
766 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
769 if (GET_CODE (x) == CONST)
771 if (GET_CODE (y) == CONST)
772 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
773 ysize, canon_rtx (XEXP (y, 0)), c);
774 else
775 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
776 ysize, y, c);
778 if (GET_CODE (y) == CONST)
779 return memrefs_conflict_p (xsize, x, ysize,
780 canon_rtx (XEXP (y, 0)), c);
782 if (CONSTANT_P (y))
783 return (xsize < 0 || ysize < 0
784 || (rtx_equal_for_memref_p (x, y)
785 && (xsize == 0 || ysize == 0
786 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
788 return 1;
790 return 1;
793 /* Functions to compute memory dependencies.
795 Since we process the insns in execution order, we can build tables
796 to keep track of what registers are fixed (and not aliased), what registers
797 are varying in known ways, and what registers are varying in unknown
798 ways.
800 If both memory references are volatile, then there must always be a
801 dependence between the two references, since their order can not be
802 changed. A volatile and non-volatile reference can be interchanged
803 though.
805 A MEM_IN_STRUCT reference at a non-QImode non-AND varying address can never
806 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We must
807 allow QImode aliasing because the ANSI C standard allows character
808 pointers to alias anything. We are assuming that characters are
809 always QImode here. We also must allow AND addresses, because they may
810 generate accesses outside the object being referenced. This is used to
811 generate aligned addresses from unaligned addresses, for instance, the
812 alpha storeqi_unaligned pattern. */
814 /* Read dependence: X is read after read in MEM takes place. There can
815 only be a dependence here if both reads are volatile. */
818 read_dependence (mem, x)
819 rtx mem;
820 rtx x;
822 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
825 /* True dependence: X is read after store in MEM takes place. */
828 true_dependence (mem, mem_mode, x, varies)
829 rtx mem;
830 enum machine_mode mem_mode;
831 rtx x;
832 int (*varies)();
834 register rtx x_addr, mem_addr;
836 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
837 return 1;
839 /* If X is an unchanging read, then it can't possibly conflict with any
840 non-unchanging store. It may conflict with an unchanging write though,
841 because there may be a single store to this address to initialize it.
842 Just fall through to the code below to resolve the case where we have
843 both an unchanging read and an unchanging write. This won't handle all
844 cases optimally, but the possible performance loss should be
845 negligible. */
846 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
847 return 0;
849 if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0)))
850 return 0;
852 x_addr = canon_rtx (XEXP (x, 0));
853 mem_addr = canon_rtx (XEXP (mem, 0));
855 if (mem_mode == VOIDmode)
856 mem_mode = GET_MODE (mem);
858 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
859 SIZE_FOR_MODE (x), x_addr, 0))
860 return 0;
862 /* If both references are struct references, or both are not, nothing
863 is known about aliasing.
865 If either reference is QImode or BLKmode, ANSI C permits aliasing.
867 If both addresses are constant, or both are not, nothing is known
868 about aliasing. */
869 if (MEM_IN_STRUCT_P (x) == MEM_IN_STRUCT_P (mem)
870 || mem_mode == QImode || mem_mode == BLKmode
871 || GET_MODE (x) == QImode || GET_MODE (x) == BLKmode
872 || GET_CODE (x_addr) == AND || GET_CODE (mem_addr) == AND
873 || varies (x_addr) == varies (mem_addr))
874 return 1;
876 /* One memory reference is to a constant address, one is not.
877 One is to a structure, the other is not.
879 If either memory reference is a variable structure the other is a
880 fixed scalar and there is no aliasing. */
881 if ((MEM_IN_STRUCT_P (mem) && varies (mem_addr))
882 || (MEM_IN_STRUCT_P (x) && varies (x_addr)))
883 return 0;
885 return 1;
888 /* Anti dependence: X is written after read in MEM takes place. */
891 anti_dependence (mem, x)
892 rtx mem;
893 rtx x;
895 rtx x_addr, mem_addr;
897 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
898 return 1;
900 /* If MEM is an unchanging read, then it can't possibly conflict with
901 the store to X, because there is at most one store to MEM, and it must
902 have occurred somewhere before MEM. */
903 if (RTX_UNCHANGING_P (mem))
904 return 0;
906 if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0)))
907 return 0;
909 x = canon_rtx (x);
910 mem = canon_rtx (mem);
912 x_addr = XEXP (x, 0);
913 mem_addr = XEXP (mem, 0);
915 return (memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
916 SIZE_FOR_MODE (x), x_addr, 0)
917 && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
918 && GET_MODE (mem) != QImode
919 && GET_CODE (mem_addr) != AND
920 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
921 && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
922 && GET_MODE (x) != QImode
923 && GET_CODE (x_addr) != AND
924 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem)));
927 /* Output dependence: X is written after store in MEM takes place. */
930 output_dependence (mem, x)
931 register rtx mem;
932 register rtx x;
934 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
935 return 1;
937 if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0)))
938 return 0;
940 x = canon_rtx (x);
941 mem = canon_rtx (mem);
943 return (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
944 SIZE_FOR_MODE (x), XEXP (x, 0), 0)
945 && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
946 && GET_MODE (mem) != QImode
947 && GET_CODE (XEXP (mem, 0)) != AND
948 && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
949 && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
950 && GET_MODE (x) != QImode
951 && GET_CODE (XEXP (x, 0)) != AND
952 && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem)));
956 static HARD_REG_SET argument_registers;
958 void
959 init_alias_once ()
961 register int i;
963 #ifndef OUTGOING_REGNO
964 #define OUTGOING_REGNO(N) N
965 #endif
966 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
967 /* Check whether this register can hold an incoming pointer
968 argument. FUNCTION_ARG_REGNO_P tests outgoing register
969 numbers, so translate if necessary due to register windows. */
970 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
971 && HARD_REGNO_MODE_OK (i, Pmode))
972 SET_HARD_REG_BIT (argument_registers, i);
975 void
976 init_alias_analysis ()
978 int maxreg = max_reg_num ();
979 int changed, pass;
980 register int i;
981 register rtx insn;
983 reg_known_value_size = maxreg;
985 reg_known_value
986 = (rtx *) oballoc ((maxreg - FIRST_PSEUDO_REGISTER) * sizeof (rtx))
987 - FIRST_PSEUDO_REGISTER;
988 reg_known_equiv_p =
989 oballoc (maxreg - FIRST_PSEUDO_REGISTER) - FIRST_PSEUDO_REGISTER;
990 bzero ((char *) (reg_known_value + FIRST_PSEUDO_REGISTER),
991 (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
992 bzero (reg_known_equiv_p + FIRST_PSEUDO_REGISTER,
993 (maxreg - FIRST_PSEUDO_REGISTER) * sizeof (char));
995 /* Overallocate reg_base_value to allow some growth during loop
996 optimization. Loop unrolling can create a large number of
997 registers. */
998 reg_base_value_size = maxreg * 2;
999 reg_base_value = (rtx *)oballoc (reg_base_value_size * sizeof (rtx));
1000 new_reg_base_value = (rtx *)alloca (reg_base_value_size * sizeof (rtx));
1001 reg_seen = (char *)alloca (reg_base_value_size);
1002 bzero ((char *) reg_base_value, reg_base_value_size * sizeof (rtx));
1004 /* The basic idea is that each pass through this loop will use the
1005 "constant" information from the previous pass to propagate alias
1006 information through another level of assignments.
1008 This could get expensive if the assignment chains are long. Maybe
1009 we should throttle the number of iterations, possibly based on
1010 the optimization level or flag_expensive_optimizations.
1012 We could propagate more information in the first pass by making use
1013 of REG_N_SETS to determine immediately that the alias information
1014 for a pseudo is "constant".
1016 A program with an uninitialized variable can cause an infinite loop
1017 here. Instead of doing a full dataflow analysis to detect such problems
1018 we just cap the number of iterations for the loop.
1020 The state of the arrays for the set chain in question does not matter
1021 since the program has undefined behavior. */
1023 pass = 0;
1026 /* Assume nothing will change this iteration of the loop. */
1027 changed = 0;
1029 /* We want to assign the same IDs each iteration of this loop, so
1030 start counting from zero each iteration of the loop. */
1031 unique_id = 0;
1033 /* We're at the start of the funtion each iteration through the
1034 loop, so we're copying arguments. */
1035 copying_arguments = 1;
1037 /* Wipe the potential alias information clean for this pass. */
1038 bzero ((char *) new_reg_base_value, reg_base_value_size * sizeof (rtx));
1040 /* Wipe the reg_seen array clean. */
1041 bzero ((char *) reg_seen, reg_base_value_size);
1043 /* Mark all hard registers which may contain an address.
1044 The stack, frame and argument pointers may contain an address.
1045 An argument register which can hold a Pmode value may contain
1046 an address even if it is not in BASE_REGS.
1048 The address expression is VOIDmode for an argument and
1049 Pmode for other registers. */
1051 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1052 if (TEST_HARD_REG_BIT (argument_registers, i))
1053 new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
1054 gen_rtx_REG (Pmode, i));
1056 new_reg_base_value[STACK_POINTER_REGNUM]
1057 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
1058 new_reg_base_value[ARG_POINTER_REGNUM]
1059 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
1060 new_reg_base_value[FRAME_POINTER_REGNUM]
1061 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
1062 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1063 new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
1064 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
1065 #endif
1066 if (struct_value_incoming_rtx
1067 && GET_CODE (struct_value_incoming_rtx) == REG)
1068 new_reg_base_value[REGNO (struct_value_incoming_rtx)]
1069 = gen_rtx_ADDRESS (Pmode, struct_value_incoming_rtx);
1071 if (static_chain_rtx
1072 && GET_CODE (static_chain_rtx) == REG)
1073 new_reg_base_value[REGNO (static_chain_rtx)]
1074 = gen_rtx_ADDRESS (Pmode, static_chain_rtx);
1076 /* Walk the insns adding values to the new_reg_base_value array. */
1077 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1079 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1081 rtx note, set;
1082 /* If this insn has a noalias note, process it, Otherwise,
1083 scan for sets. A simple set will have no side effects
1084 which could change the base value of any other register. */
1086 if (GET_CODE (PATTERN (insn)) == SET
1087 && (find_reg_note (insn, REG_NOALIAS, NULL_RTX)))
1088 record_set (SET_DEST (PATTERN (insn)), 0);
1089 else
1090 note_stores (PATTERN (insn), record_set);
1092 set = single_set (insn);
1094 if (set != 0
1095 && GET_CODE (SET_DEST (set)) == REG
1096 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
1097 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1098 && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
1099 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1100 && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1102 int regno = REGNO (SET_DEST (set));
1103 reg_known_value[regno] = XEXP (note, 0);
1104 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
1107 else if (GET_CODE (insn) == NOTE
1108 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
1109 copying_arguments = 0;
1112 /* Now propagate values from new_reg_base_value to reg_base_value. */
1113 for (i = 0; i < reg_base_value_size; i++)
1115 if (new_reg_base_value[i]
1116 && new_reg_base_value[i] != reg_base_value[i]
1117 && ! rtx_equal_p (new_reg_base_value[i], reg_base_value[i]))
1119 reg_base_value[i] = new_reg_base_value[i];
1120 changed = 1;
1124 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
1126 /* Fill in the remaining entries. */
1127 for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
1128 if (reg_known_value[i] == 0)
1129 reg_known_value[i] = regno_reg_rtx[i];
1131 /* Simplify the reg_base_value array so that no register refers to
1132 another register, except to special registers indirectly through
1133 ADDRESS expressions.
1135 In theory this loop can take as long as O(registers^2), but unless
1136 there are very long dependency chains it will run in close to linear
1137 time.
1139 This loop may not be needed any longer now that the main loop does
1140 a better job at propagating alias information. */
1141 pass = 0;
1144 changed = 0;
1145 pass++;
1146 for (i = 0; i < reg_base_value_size; i++)
1148 rtx base = reg_base_value[i];
1149 if (base && GET_CODE (base) == REG)
1151 int base_regno = REGNO (base);
1152 if (base_regno == i) /* register set from itself */
1153 reg_base_value[i] = 0;
1154 else
1155 reg_base_value[i] = reg_base_value[base_regno];
1156 changed = 1;
1160 while (changed && pass < MAX_ALIAS_LOOP_PASSES);
1162 new_reg_base_value = 0;
1163 reg_seen = 0;
1166 void
1167 end_alias_analysis ()
1169 reg_known_value = 0;
1170 reg_base_value = 0;
1171 reg_base_value_size = 0;