1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998, 1999 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)
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. */
28 #include "hard-reg-set.h"
32 #include "splay-tree.h"
34 /* The alias sets assigned to MEMs assist the back-end in determining
35 which MEMs can alias which other MEMs. In general, two MEMs in
36 different alias sets to not alias each other. There is one
37 exception, however. Consider something like:
39 struct S {int i; double d; };
41 a store to an `S' can alias something of either type `int' or type
42 `double'. (However, a store to an `int' cannot alias a `double'
43 and vice versa.) We indicate this via a tree structure that looks
51 (The arrows are directed and point downwards.) If, when comparing
52 two alias sets, we can hold one set fixed, and trace the other set
53 downwards, and at some point find the first set, the two MEMs can
54 alias one another. In this situation we say the alias set for
55 `struct S' is the `superset' and that those for `int' and `double'
58 Alias set zero is implicitly a superset of all other alias sets.
59 However, this is no actual entry for alias set zero. It is an
60 error to attempt to explicitly construct a subset of zero. */
62 typedef struct alias_set_entry {
63 /* The alias set number, as stored in MEM_ALIAS_SET. */
66 /* The children of the alias set. These are not just the immediate
67 children, but, in fact, all children. So, if we have:
69 struct T { struct S s; float f; }
71 continuing our example above, the children here will be all of
72 `int', `double', `float', and `struct S'. */
76 static rtx canon_rtx PROTO((rtx));
77 static int rtx_equal_for_memref_p PROTO((rtx, rtx));
78 static rtx find_symbolic_term PROTO((rtx));
79 static int memrefs_conflict_p PROTO((int, rtx, int, rtx,
81 static void record_set PROTO((rtx, rtx));
82 static rtx find_base_term PROTO((rtx));
83 static int base_alias_check PROTO((rtx, rtx, enum machine_mode,
85 static rtx find_base_value PROTO((rtx));
86 static int mems_in_disjoint_alias_sets_p PROTO((rtx, rtx));
87 static int insert_subset_children PROTO((splay_tree_node,
89 static alias_set_entry get_alias_set_entry PROTO((int));
90 static rtx fixed_scalar_and_varying_struct_p PROTO((rtx, rtx, int (*)(rtx)));
91 static int aliases_everything_p PROTO((rtx));
92 static int write_dependence_p PROTO((rtx, rtx, int));
94 /* Set up all info needed to perform alias analysis on memory references. */
96 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
98 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
99 different alias sets. We ignore alias sets in functions making use
100 of variable arguments because the va_arg macros on some systems are
102 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
103 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
105 /* Cap the number of passes we make over the insns propagating alias
106 information through set chains.
108 10 is a completely arbitrary choice. */
109 #define MAX_ALIAS_LOOP_PASSES 10
111 /* reg_base_value[N] gives an address to which register N is related.
112 If all sets after the first add or subtract to the current value
113 or otherwise modify it so it does not point to a different top level
114 object, reg_base_value[N] is equal to the address part of the source
117 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
118 expressions represent certain special values: function arguments and
119 the stack, frame, and argument pointers. The contents of an address
120 expression are not used (but they are descriptive for debugging);
121 only the address and mode matter. Pointer equality, not rtx_equal_p,
122 determines whether two ADDRESS expressions refer to the same base
123 address. The mode determines whether it is a function argument or
124 other special value. */
127 rtx *new_reg_base_value;
128 unsigned int reg_base_value_size; /* size of reg_base_value array */
129 #define REG_BASE_VALUE(X) \
130 ((unsigned) REGNO (X) < reg_base_value_size ? reg_base_value[REGNO (X)] : 0)
132 /* Vector of known invariant relationships between registers. Set in
133 loop unrolling. Indexed by register number, if nonzero the value
134 is an expression describing this register in terms of another.
136 The length of this array is REG_BASE_VALUE_SIZE.
138 Because this array contains only pseudo registers it has no effect
140 static rtx *alias_invariant;
142 /* Vector indexed by N giving the initial (unchanging) value known
143 for pseudo-register N. */
144 rtx *reg_known_value;
146 /* Indicates number of valid entries in reg_known_value. */
147 static int reg_known_value_size;
149 /* Vector recording for each reg_known_value whether it is due to a
150 REG_EQUIV note. Future passes (viz., reload) may replace the
151 pseudo with the equivalent expression and so we account for the
152 dependences that would be introduced if that happens. */
153 /* ??? This is a problem only on the Convex. The REG_EQUIV notes created in
154 assign_parms mention the arg pointer, and there are explicit insns in the
155 RTL that modify the arg pointer. Thus we must ensure that such insns don't
156 get scheduled across each other because that would invalidate the REG_EQUIV
157 notes. One could argue that the REG_EQUIV notes are wrong, but solving
158 the problem in the scheduler will likely give better code, so we do it
160 char *reg_known_equiv_p;
162 /* True when scanning insns from the start of the rtl to the
163 NOTE_INSN_FUNCTION_BEG note. */
165 static int copying_arguments;
167 /* The splay-tree used to store the various alias set entries. */
169 static splay_tree alias_sets;
171 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
172 such an entry, or NULL otherwise. */
174 static alias_set_entry
175 get_alias_set_entry (alias_set)
179 splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
181 return sn ? ((alias_set_entry) sn->value) : ((alias_set_entry) 0);
184 /* Returns nonzero value if the alias sets for MEM1 and MEM2 are such
185 that the two MEMs cannot alias each other. */
188 mems_in_disjoint_alias_sets_p (mem1, mem2)
194 #ifdef ENABLE_CHECKING
195 /* Perform a basic sanity check. Namely, that there are no alias sets
196 if we're not using strict aliasing. This helps to catch bugs
197 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
198 where a MEM is allocated in some way other than by the use of
199 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
200 use alias sets to indicate that spilled registers cannot alias each
201 other, we might need to remove this check. */
202 if (!flag_strict_aliasing &&
203 (MEM_ALIAS_SET (mem1) || MEM_ALIAS_SET (mem2)))
207 /* The code used in varargs macros are often not conforming ANSI C,
208 which can trick the compiler into making incorrect aliasing
209 assumptions in these functions. So, we don't use alias sets in
210 such a function. FIXME: This should be moved into the front-end;
211 it is a language-dependent notion, and there's no reason not to
212 still use these checks to handle globals. */
213 if (current_function_stdarg || current_function_varargs)
216 if (!MEM_ALIAS_SET (mem1) || !MEM_ALIAS_SET (mem2))
217 /* We have no alias set information for one of the MEMs, so we
218 have to assume it can alias anything. */
221 if (MEM_ALIAS_SET (mem1) == MEM_ALIAS_SET (mem2))
222 /* The two alias sets are the same, so they may alias. */
225 /* Iterate through each of the children of the first alias set,
226 comparing it with the second alias set. */
227 ase = get_alias_set_entry (MEM_ALIAS_SET (mem1));
228 if (ase && splay_tree_lookup (ase->children,
229 (splay_tree_key) MEM_ALIAS_SET (mem2)))
232 /* Now do the same, but with the alias sets reversed. */
233 ase = get_alias_set_entry (MEM_ALIAS_SET (mem2));
234 if (ase && splay_tree_lookup (ase->children,
235 (splay_tree_key) MEM_ALIAS_SET (mem1)))
238 /* The two MEMs are in distinct alias sets, and neither one is the
239 child of the other. Therefore, they cannot alias. */
243 /* Insert the NODE into the splay tree given by DATA. Used by
244 record_alias_subset via splay_tree_foreach. */
247 insert_subset_children (node, data)
248 splay_tree_node node;
251 splay_tree_insert ((splay_tree) data,
258 /* Indicate that things in SUBSET can alias things in SUPERSET, but
259 not vice versa. For example, in C, a store to an `int' can alias a
260 structure containing an `int', but not vice versa. Here, the
261 structure would be the SUPERSET and `int' the SUBSET. This
262 function should be called only once per SUPERSET/SUBSET pair. At
263 present any given alias set may only be a subset of one superset.
265 It is illegal for SUPERSET to be zero; everything is implicitly a
266 subset of alias set zero. */
269 record_alias_subset (superset, subset)
273 alias_set_entry superset_entry;
274 alias_set_entry subset_entry;
279 superset_entry = get_alias_set_entry (superset);
282 /* Create an entry for the SUPERSET, so that we have a place to
283 attach the SUBSET. */
285 (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
286 superset_entry->alias_set = superset;
287 superset_entry->children
288 = splay_tree_new (splay_tree_compare_ints, 0, 0);
289 splay_tree_insert (alias_sets,
290 (splay_tree_key) superset,
291 (splay_tree_value) superset_entry);
295 subset_entry = get_alias_set_entry (subset);
297 /* There is an entry for the subset. Enter all of its children
298 (if they are not already present) as children of the SUPERSET. */
299 splay_tree_foreach (subset_entry->children,
300 insert_subset_children,
301 superset_entry->children);
303 /* Enter the SUBSET itself as a child of the SUPERSET. */
304 splay_tree_insert (superset_entry->children,
305 (splay_tree_key) subset,
309 /* Inside SRC, the source of a SET, find a base address. */
312 find_base_value (src)
315 switch (GET_CODE (src))
322 /* At the start of a function argument registers have known base
323 values which may be lost later. Returning an ADDRESS
324 expression here allows optimization based on argument values
325 even when the argument registers are used for other purposes. */
326 if (REGNO (src) < FIRST_PSEUDO_REGISTER && copying_arguments)
327 return new_reg_base_value[REGNO (src)];
329 /* If a pseudo has a known base value, return it. Do not do this
330 for hard regs since it can result in a circular dependency
331 chain for registers which have values at function entry.
333 The test above is not sufficient because the scheduler may move
334 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
335 if (REGNO (src) >= FIRST_PSEUDO_REGISTER
336 && (unsigned) REGNO (src) < reg_base_value_size
337 && reg_base_value[REGNO (src)])
338 return reg_base_value[REGNO (src)];
343 /* Check for an argument passed in memory. Only record in the
344 copying-arguments block; it is too hard to track changes
346 if (copying_arguments
347 && (XEXP (src, 0) == arg_pointer_rtx
348 || (GET_CODE (XEXP (src, 0)) == PLUS
349 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
350 return gen_rtx_ADDRESS (VOIDmode, src);
355 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
362 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
364 /* If either operand is a REG, then see if we already have
365 a known value for it. */
366 if (GET_CODE (src_0) == REG)
368 temp = find_base_value (src_0);
373 if (GET_CODE (src_1) == REG)
375 temp = find_base_value (src_1);
380 /* Guess which operand is the base address.
382 If either operand is a symbol, then it is the base. If
383 either operand is a CONST_INT, then the other is the base. */
385 if (GET_CODE (src_1) == CONST_INT
386 || GET_CODE (src_0) == SYMBOL_REF
387 || GET_CODE (src_0) == LABEL_REF
388 || GET_CODE (src_0) == CONST)
389 return find_base_value (src_0);
391 if (GET_CODE (src_0) == CONST_INT
392 || GET_CODE (src_1) == SYMBOL_REF
393 || GET_CODE (src_1) == LABEL_REF
394 || GET_CODE (src_1) == CONST)
395 return find_base_value (src_1);
397 /* This might not be necessary anymore.
399 If either operand is a REG that is a known pointer, then it
401 if (GET_CODE (src_0) == REG && REGNO_POINTER_FLAG (REGNO (src_0)))
402 return find_base_value (src_0);
404 if (GET_CODE (src_1) == REG && REGNO_POINTER_FLAG (REGNO (src_1)))
405 return find_base_value (src_1);
411 /* The standard form is (lo_sum reg sym) so look only at the
413 return find_base_value (XEXP (src, 1));
416 /* If the second operand is constant set the base
417 address to the first operand. */
418 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
419 return find_base_value (XEXP (src, 0));
423 case SIGN_EXTEND: /* used for NT/Alpha pointers */
425 return find_base_value (XEXP (src, 0));
434 /* Called from init_alias_analysis indirectly through note_stores. */
436 /* while scanning insns to find base values, reg_seen[N] is nonzero if
437 register N has been set in this function. */
438 static char *reg_seen;
440 /* Addresses which are known not to alias anything else are identified
441 by a unique integer. */
442 static int unique_id;
445 record_set (dest, set)
451 if (GET_CODE (dest) != REG)
454 regno = REGNO (dest);
458 /* A CLOBBER wipes out any old value but does not prevent a previously
459 unset register from acquiring a base address (i.e. reg_seen is not
461 if (GET_CODE (set) == CLOBBER)
463 new_reg_base_value[regno] = 0;
472 new_reg_base_value[regno] = 0;
476 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
477 GEN_INT (unique_id++));
481 /* This is not the first set. If the new value is not related to the
482 old value, forget the base value. Note that the following code is
484 extern int x, y; int *p = &x; p += (&y-&x);
485 ANSI C does not allow computing the difference of addresses
486 of distinct top level objects. */
487 if (new_reg_base_value[regno])
488 switch (GET_CODE (src))
493 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
494 new_reg_base_value[regno] = 0;
497 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
498 new_reg_base_value[regno] = 0;
501 new_reg_base_value[regno] = 0;
504 /* If this is the first set of a register, record the value. */
505 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
506 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
507 new_reg_base_value[regno] = find_base_value (src);
512 /* Called from loop optimization when a new pseudo-register is created. */
514 record_base_value (regno, val, invariant)
519 if ((unsigned) regno >= reg_base_value_size)
522 /* If INVARIANT is true then this value also describes an invariant
523 relationship which can be used to deduce that two registers with
524 unknown values are different. */
525 if (invariant && alias_invariant)
526 alias_invariant[regno] = val;
528 if (GET_CODE (val) == REG)
530 if ((unsigned) REGNO (val) < reg_base_value_size)
532 reg_base_value[regno] = reg_base_value[REGNO (val)];
536 reg_base_value[regno] = find_base_value (val);
543 /* Recursively look for equivalences. */
544 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
545 && REGNO (x) < reg_known_value_size)
546 return reg_known_value[REGNO (x)] == x
547 ? x : canon_rtx (reg_known_value[REGNO (x)]);
548 else if (GET_CODE (x) == PLUS)
550 rtx x0 = canon_rtx (XEXP (x, 0));
551 rtx x1 = canon_rtx (XEXP (x, 1));
553 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
555 /* We can tolerate LO_SUMs being offset here; these
556 rtl are used for nothing other than comparisons. */
557 if (GET_CODE (x0) == CONST_INT)
558 return plus_constant_for_output (x1, INTVAL (x0));
559 else if (GET_CODE (x1) == CONST_INT)
560 return plus_constant_for_output (x0, INTVAL (x1));
561 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
564 /* This gives us much better alias analysis when called from
565 the loop optimizer. Note we want to leave the original
566 MEM alone, but need to return the canonicalized MEM with
567 all the flags with their original values. */
568 else if (GET_CODE (x) == MEM)
570 rtx addr = canon_rtx (XEXP (x, 0));
571 if (addr != XEXP (x, 0))
573 rtx new = gen_rtx_MEM (GET_MODE (x), addr);
574 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
575 MEM_COPY_ATTRIBUTES (new, x);
576 MEM_ALIAS_SET (new) = MEM_ALIAS_SET (x);
583 /* Return 1 if X and Y are identical-looking rtx's.
585 We use the data in reg_known_value above to see if two registers with
586 different numbers are, in fact, equivalent. */
589 rtx_equal_for_memref_p (x, y)
594 register enum rtx_code code;
597 if (x == 0 && y == 0)
599 if (x == 0 || y == 0)
608 /* Rtx's of different codes cannot be equal. */
609 if (code != GET_CODE (y))
612 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
613 (REG:SI x) and (REG:HI x) are NOT equivalent. */
615 if (GET_MODE (x) != GET_MODE (y))
618 /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively. */
621 return REGNO (x) == REGNO (y);
622 if (code == LABEL_REF)
623 return XEXP (x, 0) == XEXP (y, 0);
624 if (code == SYMBOL_REF)
625 return XSTR (x, 0) == XSTR (y, 0);
626 if (code == CONST_INT)
627 return INTVAL (x) == INTVAL (y);
628 if (code == ADDRESSOF)
629 return REGNO (XEXP (x, 0)) == REGNO (XEXP (y, 0)) && XINT (x, 1) == XINT (y, 1);
631 /* For commutative operations, the RTX match if the operand match in any
632 order. Also handle the simple binary and unary cases without a loop. */
633 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
634 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
635 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
636 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
637 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
638 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
639 return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
640 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
641 else if (GET_RTX_CLASS (code) == '1')
642 return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
644 /* Compare the elements. If any pair of corresponding elements
645 fail to match, return 0 for the whole things.
647 Limit cases to types which actually appear in addresses. */
649 fmt = GET_RTX_FORMAT (code);
650 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
655 if (XINT (x, i) != XINT (y, i))
660 /* Two vectors must have the same length. */
661 if (XVECLEN (x, i) != XVECLEN (y, i))
664 /* And the corresponding elements must match. */
665 for (j = 0; j < XVECLEN (x, i); j++)
666 if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
671 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
675 /* This can happen for an asm which clobbers memory. */
679 /* It is believed that rtx's at this level will never
680 contain anything but integers and other rtx's,
681 except for within LABEL_REFs and SYMBOL_REFs. */
689 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
690 X and return it, or return 0 if none found. */
693 find_symbolic_term (x)
697 register enum rtx_code code;
701 if (code == SYMBOL_REF || code == LABEL_REF)
703 if (GET_RTX_CLASS (code) == 'o')
706 fmt = GET_RTX_FORMAT (code);
707 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
713 t = find_symbolic_term (XEXP (x, i));
717 else if (fmt[i] == 'E')
727 switch (GET_CODE (x))
730 return REG_BASE_VALUE (x);
733 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
739 return find_base_term (XEXP (x, 0));
743 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
750 rtx tmp1 = XEXP (x, 0);
751 rtx tmp2 = XEXP (x, 1);
753 /* This is a litle bit tricky since we have to determine which of
754 the two operands represents the real base address. Otherwise this
755 routine may return the index register instead of the base register.
757 That may cause us to believe no aliasing was possible, when in
758 fact aliasing is possible.
760 We use a few simple tests to guess the base register. Additional
761 tests can certainly be added. For example, if one of the operands
762 is a shift or multiply, then it must be the index register and the
763 other operand is the base register. */
765 /* If either operand is known to be a pointer, then use it
766 to determine the base term. */
767 if (REG_P (tmp1) && REGNO_POINTER_FLAG (REGNO (tmp1)))
768 return find_base_term (tmp1);
770 if (REG_P (tmp2) && REGNO_POINTER_FLAG (REGNO (tmp2)))
771 return find_base_term (tmp2);
773 /* Neither operand was known to be a pointer. Go ahead and find the
774 base term for both operands. */
775 tmp1 = find_base_term (tmp1);
776 tmp2 = find_base_term (tmp2);
778 /* If either base term is named object or a special address
779 (like an argument or stack reference), then use it for the
782 && (GET_CODE (tmp1) == SYMBOL_REF
783 || GET_CODE (tmp1) == LABEL_REF
784 || (GET_CODE (tmp1) == ADDRESS
785 && GET_MODE (tmp1) != VOIDmode)))
789 && (GET_CODE (tmp2) == SYMBOL_REF
790 || GET_CODE (tmp2) == LABEL_REF
791 || (GET_CODE (tmp2) == ADDRESS
792 && GET_MODE (tmp2) != VOIDmode)))
795 /* We could not determine which of the two operands was the
796 base register and which was the index. So we can determine
797 nothing from the base alias check. */
802 if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
803 return REG_BASE_VALUE (XEXP (x, 0));
815 /* Return 0 if the addresses X and Y are known to point to different
816 objects, 1 if they might be pointers to the same object. */
819 base_alias_check (x, y, x_mode, y_mode)
821 enum machine_mode x_mode, y_mode;
823 rtx x_base = find_base_term (x);
824 rtx y_base = find_base_term (y);
826 /* If the address itself has no known base see if a known equivalent
827 value has one. If either address still has no known base, nothing
828 is known about aliasing. */
832 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
834 x_base = find_base_term (x_c);
842 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
844 y_base = find_base_term (y_c);
849 /* If the base addresses are equal nothing is known about aliasing. */
850 if (rtx_equal_p (x_base, y_base))
853 /* The base addresses of the read and write are different expressions.
854 If they are both symbols and they are not accessed via AND, there is
855 no conflict. We can bring knowledge of object alignment into play
856 here. For example, on alpha, "char a, b;" can alias one another,
857 though "char a; long b;" cannot. */
858 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
860 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
862 if (GET_CODE (x) == AND
863 && (GET_CODE (XEXP (x, 1)) != CONST_INT
864 || GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
866 if (GET_CODE (y) == AND
867 && (GET_CODE (XEXP (y, 1)) != CONST_INT
868 || GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
870 /* Differing symbols never alias. */
874 /* If one address is a stack reference there can be no alias:
875 stack references using different base registers do not alias,
876 a stack reference can not alias a parameter, and a stack reference
877 can not alias a global. */
878 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
879 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
882 if (! flag_argument_noalias)
885 if (flag_argument_noalias > 1)
888 /* Weak noalias assertion (arguments are distinct, but may match globals). */
889 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
892 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
893 where SIZE is the size in bytes of the memory reference. If ADDR
894 is not modified by the memory reference then ADDR is returned. */
897 addr_side_effect_eval (addr, size, n_refs)
904 switch (GET_CODE (addr))
907 offset = (n_refs + 1) * size;
910 offset = -(n_refs + 1) * size;
913 offset = n_refs * size;
916 offset = -n_refs * size;
924 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
926 addr = XEXP (addr, 0);
931 /* Return nonzero if X and Y (memory addresses) could reference the
932 same location in memory. C is an offset accumulator. When
933 C is nonzero, we are testing aliases between X and Y + C.
934 XSIZE is the size in bytes of the X reference,
935 similarly YSIZE is the size in bytes for Y.
937 If XSIZE or YSIZE is zero, we do not know the amount of memory being
938 referenced (the reference was BLKmode), so make the most pessimistic
941 If XSIZE or YSIZE is negative, we may access memory outside the object
942 being referenced as a side effect. This can happen when using AND to
943 align memory references, as is done on the Alpha.
945 Nice to notice that varying addresses cannot conflict with fp if no
946 local variables had their addresses taken, but that's too hard now. */
950 memrefs_conflict_p (xsize, x, ysize, y, c)
955 if (GET_CODE (x) == HIGH)
957 else if (GET_CODE (x) == LO_SUM)
960 x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
961 if (GET_CODE (y) == HIGH)
963 else if (GET_CODE (y) == LO_SUM)
966 y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
968 if (rtx_equal_for_memref_p (x, y))
970 if (xsize <= 0 || ysize <= 0)
972 if (c >= 0 && xsize > c)
974 if (c < 0 && ysize+c > 0)
979 /* This code used to check for conflicts involving stack references and
980 globals but the base address alias code now handles these cases. */
982 if (GET_CODE (x) == PLUS)
984 /* The fact that X is canonicalized means that this
985 PLUS rtx is canonicalized. */
986 rtx x0 = XEXP (x, 0);
987 rtx x1 = XEXP (x, 1);
989 if (GET_CODE (y) == PLUS)
991 /* The fact that Y is canonicalized means that this
992 PLUS rtx is canonicalized. */
993 rtx y0 = XEXP (y, 0);
994 rtx y1 = XEXP (y, 1);
996 if (rtx_equal_for_memref_p (x1, y1))
997 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
998 if (rtx_equal_for_memref_p (x0, y0))
999 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1000 if (GET_CODE (x1) == CONST_INT)
1002 if (GET_CODE (y1) == CONST_INT)
1003 return memrefs_conflict_p (xsize, x0, ysize, y0,
1004 c - INTVAL (x1) + INTVAL (y1));
1006 return memrefs_conflict_p (xsize, x0, ysize, y,
1009 else if (GET_CODE (y1) == CONST_INT)
1010 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1014 else if (GET_CODE (x1) == CONST_INT)
1015 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1017 else if (GET_CODE (y) == PLUS)
1019 /* The fact that Y is canonicalized means that this
1020 PLUS rtx is canonicalized. */
1021 rtx y0 = XEXP (y, 0);
1022 rtx y1 = XEXP (y, 1);
1024 if (GET_CODE (y1) == CONST_INT)
1025 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1030 if (GET_CODE (x) == GET_CODE (y))
1031 switch (GET_CODE (x))
1035 /* Handle cases where we expect the second operands to be the
1036 same, and check only whether the first operand would conflict
1039 rtx x1 = canon_rtx (XEXP (x, 1));
1040 rtx y1 = canon_rtx (XEXP (y, 1));
1041 if (! rtx_equal_for_memref_p (x1, y1))
1043 x0 = canon_rtx (XEXP (x, 0));
1044 y0 = canon_rtx (XEXP (y, 0));
1045 if (rtx_equal_for_memref_p (x0, y0))
1046 return (xsize == 0 || ysize == 0
1047 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1049 /* Can't properly adjust our sizes. */
1050 if (GET_CODE (x1) != CONST_INT)
1052 xsize /= INTVAL (x1);
1053 ysize /= INTVAL (x1);
1055 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1059 /* Are these registers known not to be equal? */
1060 if (alias_invariant)
1062 unsigned int r_x = REGNO (x), r_y = REGNO (y);
1063 rtx i_x, i_y; /* invariant relationships of X and Y */
1065 i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1066 i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1068 if (i_x == 0 && i_y == 0)
1071 if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1072 ysize, i_y ? i_y : y, c))
1081 /* Treat an access through an AND (e.g. a subword access on an Alpha)
1082 as an access with indeterminate size. Assume that references
1083 besides AND are aligned, so if the size of the other reference is
1084 at least as large as the alignment, assume no other overlap. */
1085 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1087 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1089 return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1091 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1093 /* ??? If we are indexing far enough into the array/structure, we
1094 may yet be able to determine that we can not overlap. But we
1095 also need to that we are far enough from the end not to overlap
1096 a following reference, so we do nothing with that for now. */
1097 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1099 return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1104 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1106 c += (INTVAL (y) - INTVAL (x));
1107 return (xsize <= 0 || ysize <= 0
1108 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1111 if (GET_CODE (x) == CONST)
1113 if (GET_CODE (y) == CONST)
1114 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1115 ysize, canon_rtx (XEXP (y, 0)), c);
1117 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1120 if (GET_CODE (y) == CONST)
1121 return memrefs_conflict_p (xsize, x, ysize,
1122 canon_rtx (XEXP (y, 0)), c);
1125 return (xsize < 0 || ysize < 0
1126 || (rtx_equal_for_memref_p (x, y)
1127 && (xsize == 0 || ysize == 0
1128 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1135 /* Functions to compute memory dependencies.
1137 Since we process the insns in execution order, we can build tables
1138 to keep track of what registers are fixed (and not aliased), what registers
1139 are varying in known ways, and what registers are varying in unknown
1142 If both memory references are volatile, then there must always be a
1143 dependence between the two references, since their order can not be
1144 changed. A volatile and non-volatile reference can be interchanged
1147 A MEM_IN_STRUCT reference at a non-QImode non-AND varying address can never
1148 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We must
1149 allow QImode aliasing because the ANSI C standard allows character
1150 pointers to alias anything. We are assuming that characters are
1151 always QImode here. We also must allow AND addresses, because they may
1152 generate accesses outside the object being referenced. This is used to
1153 generate aligned addresses from unaligned addresses, for instance, the
1154 alpha storeqi_unaligned pattern. */
1156 /* Read dependence: X is read after read in MEM takes place. There can
1157 only be a dependence here if both reads are volatile. */
1160 read_dependence (mem, x)
1164 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1167 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1168 MEM2 is a reference to a structure at a varying address, or returns
1169 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1170 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1171 to decide whether or not an address may vary; it should return
1172 nozero whenever variation is possible. */
1175 fixed_scalar_and_varying_struct_p (mem1, mem2, varies_p)
1178 int (*varies_p) PROTO((rtx));
1180 rtx mem1_addr = XEXP (mem1, 0);
1181 rtx mem2_addr = XEXP (mem2, 0);
1183 if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
1184 && !varies_p (mem1_addr) && varies_p (mem2_addr))
1185 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1189 if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
1190 && varies_p (mem1_addr) && !varies_p (mem2_addr))
1191 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1198 /* Returns nonzero if something about the mode or address format MEM1
1199 indicates that it might well alias *anything*. */
1202 aliases_everything_p (mem)
1205 if (GET_MODE (mem) == QImode)
1206 /* ANSI C says that a `char*' can point to anything. */
1209 if (GET_CODE (XEXP (mem, 0)) == AND)
1210 /* If the address is an AND, its very hard to know at what it is
1211 actually pointing. */
1217 /* True dependence: X is read after store in MEM takes place. */
1220 true_dependence (mem, mem_mode, x, varies)
1222 enum machine_mode mem_mode;
1224 int (*varies) PROTO((rtx));
1226 register rtx x_addr, mem_addr;
1228 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1231 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1234 /* If X is an unchanging read, then it can't possibly conflict with any
1235 non-unchanging store. It may conflict with an unchanging write though,
1236 because there may be a single store to this address to initialize it.
1237 Just fall through to the code below to resolve the case where we have
1238 both an unchanging read and an unchanging write. This won't handle all
1239 cases optimally, but the possible performance loss should be
1241 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1244 if (mem_mode == VOIDmode)
1245 mem_mode = GET_MODE (mem);
1247 if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0), GET_MODE (x), mem_mode))
1250 x_addr = canon_rtx (XEXP (x, 0));
1251 mem_addr = canon_rtx (XEXP (mem, 0));
1253 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1254 SIZE_FOR_MODE (x), x_addr, 0))
1257 if (aliases_everything_p (x))
1260 /* We cannot use aliases_everyting_p to test MEM, since we must look
1261 at MEM_MODE, rather than GET_MODE (MEM). */
1262 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1265 /* In true_dependence we also allow BLKmode to alias anything. Why
1266 don't we do this in anti_dependence and output_dependence? */
1267 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1270 return !fixed_scalar_and_varying_struct_p (mem, x, varies);
1273 /* Returns non-zero if a write to X might alias a previous read from
1274 (or, if WRITEP is non-zero, a write to) MEM. */
1277 write_dependence_p (mem, x, writep)
1282 rtx x_addr, mem_addr;
1285 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1288 /* If MEM is an unchanging read, then it can't possibly conflict with
1289 the store to X, because there is at most one store to MEM, and it must
1290 have occurred somewhere before MEM. */
1291 if (!writep && RTX_UNCHANGING_P (mem))
1294 if (! base_alias_check (XEXP (x, 0), XEXP (mem, 0), GET_MODE (x),
1299 mem = canon_rtx (mem);
1301 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1304 x_addr = XEXP (x, 0);
1305 mem_addr = XEXP (mem, 0);
1307 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
1308 SIZE_FOR_MODE (x), x_addr, 0))
1312 = fixed_scalar_and_varying_struct_p (mem, x, rtx_addr_varies_p);
1314 return (!(fixed_scalar == mem && !aliases_everything_p (x))
1315 && !(fixed_scalar == x && !aliases_everything_p (mem)));
1318 /* Anti dependence: X is written after read in MEM takes place. */
1321 anti_dependence (mem, x)
1325 return write_dependence_p (mem, x, /*writep=*/0);
1328 /* Output dependence: X is written after store in MEM takes place. */
1331 output_dependence (mem, x)
1335 return write_dependence_p (mem, x, /*writep=*/1);
1339 static HARD_REG_SET argument_registers;
1346 #ifndef OUTGOING_REGNO
1347 #define OUTGOING_REGNO(N) N
1349 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1350 /* Check whether this register can hold an incoming pointer
1351 argument. FUNCTION_ARG_REGNO_P tests outgoing register
1352 numbers, so translate if necessary due to register windows. */
1353 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
1354 && HARD_REGNO_MODE_OK (i, Pmode))
1355 SET_HARD_REG_BIT (argument_registers, i);
1357 alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
1361 init_alias_analysis ()
1363 int maxreg = max_reg_num ();
1366 register unsigned int ui;
1369 reg_known_value_size = maxreg;
1372 = (rtx *) oballoc ((maxreg - FIRST_PSEUDO_REGISTER) * sizeof (rtx))
1373 - FIRST_PSEUDO_REGISTER;
1375 oballoc (maxreg - FIRST_PSEUDO_REGISTER) - FIRST_PSEUDO_REGISTER;
1376 bzero ((char *) (reg_known_value + FIRST_PSEUDO_REGISTER),
1377 (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
1378 bzero (reg_known_equiv_p + FIRST_PSEUDO_REGISTER,
1379 (maxreg - FIRST_PSEUDO_REGISTER) * sizeof (char));
1381 /* Overallocate reg_base_value to allow some growth during loop
1382 optimization. Loop unrolling can create a large number of
1384 reg_base_value_size = maxreg * 2;
1385 reg_base_value = (rtx *)oballoc (reg_base_value_size * sizeof (rtx));
1386 new_reg_base_value = (rtx *)alloca (reg_base_value_size * sizeof (rtx));
1387 reg_seen = (char *)alloca (reg_base_value_size);
1388 bzero ((char *) reg_base_value, reg_base_value_size * sizeof (rtx));
1389 if (! reload_completed && flag_unroll_loops)
1391 alias_invariant = (rtx *)xrealloc (alias_invariant,
1392 reg_base_value_size * sizeof (rtx));
1393 bzero ((char *)alias_invariant, reg_base_value_size * sizeof (rtx));
1397 /* The basic idea is that each pass through this loop will use the
1398 "constant" information from the previous pass to propagate alias
1399 information through another level of assignments.
1401 This could get expensive if the assignment chains are long. Maybe
1402 we should throttle the number of iterations, possibly based on
1403 the optimization level or flag_expensive_optimizations.
1405 We could propagate more information in the first pass by making use
1406 of REG_N_SETS to determine immediately that the alias information
1407 for a pseudo is "constant".
1409 A program with an uninitialized variable can cause an infinite loop
1410 here. Instead of doing a full dataflow analysis to detect such problems
1411 we just cap the number of iterations for the loop.
1413 The state of the arrays for the set chain in question does not matter
1414 since the program has undefined behavior. */
1419 /* Assume nothing will change this iteration of the loop. */
1422 /* We want to assign the same IDs each iteration of this loop, so
1423 start counting from zero each iteration of the loop. */
1426 /* We're at the start of the funtion each iteration through the
1427 loop, so we're copying arguments. */
1428 copying_arguments = 1;
1430 /* Wipe the potential alias information clean for this pass. */
1431 bzero ((char *) new_reg_base_value, reg_base_value_size * sizeof (rtx));
1433 /* Wipe the reg_seen array clean. */
1434 bzero ((char *) reg_seen, reg_base_value_size);
1436 /* Mark all hard registers which may contain an address.
1437 The stack, frame and argument pointers may contain an address.
1438 An argument register which can hold a Pmode value may contain
1439 an address even if it is not in BASE_REGS.
1441 The address expression is VOIDmode for an argument and
1442 Pmode for other registers. */
1444 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1445 if (TEST_HARD_REG_BIT (argument_registers, i))
1446 new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
1447 gen_rtx_REG (Pmode, i));
1449 new_reg_base_value[STACK_POINTER_REGNUM]
1450 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
1451 new_reg_base_value[ARG_POINTER_REGNUM]
1452 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
1453 new_reg_base_value[FRAME_POINTER_REGNUM]
1454 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
1455 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1456 new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
1457 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
1459 if (struct_value_incoming_rtx
1460 && GET_CODE (struct_value_incoming_rtx) == REG)
1461 new_reg_base_value[REGNO (struct_value_incoming_rtx)]
1462 = gen_rtx_ADDRESS (Pmode, struct_value_incoming_rtx);
1464 if (static_chain_rtx
1465 && GET_CODE (static_chain_rtx) == REG)
1466 new_reg_base_value[REGNO (static_chain_rtx)]
1467 = gen_rtx_ADDRESS (Pmode, static_chain_rtx);
1469 /* Walk the insns adding values to the new_reg_base_value array. */
1470 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1472 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
1473 if (prologue_epilogue_contains (insn))
1476 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1479 /* If this insn has a noalias note, process it, Otherwise,
1480 scan for sets. A simple set will have no side effects
1481 which could change the base value of any other register. */
1483 if (GET_CODE (PATTERN (insn)) == SET
1484 && (find_reg_note (insn, REG_NOALIAS, NULL_RTX)))
1485 record_set (SET_DEST (PATTERN (insn)), NULL_RTX);
1487 note_stores (PATTERN (insn), record_set);
1489 set = single_set (insn);
1492 && GET_CODE (SET_DEST (set)) == REG
1493 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
1494 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1495 && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
1496 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1497 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
1498 && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
1500 int regno = REGNO (SET_DEST (set));
1501 reg_known_value[regno] = XEXP (note, 0);
1502 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
1505 else if (GET_CODE (insn) == NOTE
1506 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
1507 copying_arguments = 0;
1510 /* Now propagate values from new_reg_base_value to reg_base_value. */
1511 for (ui = 0; ui < reg_base_value_size; ui++)
1513 if (new_reg_base_value[ui]
1514 && new_reg_base_value[ui] != reg_base_value[ui]
1515 && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
1517 reg_base_value[ui] = new_reg_base_value[ui];
1522 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
1524 /* Fill in the remaining entries. */
1525 for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
1526 if (reg_known_value[i] == 0)
1527 reg_known_value[i] = regno_reg_rtx[i];
1529 /* Simplify the reg_base_value array so that no register refers to
1530 another register, except to special registers indirectly through
1531 ADDRESS expressions.
1533 In theory this loop can take as long as O(registers^2), but unless
1534 there are very long dependency chains it will run in close to linear
1537 This loop may not be needed any longer now that the main loop does
1538 a better job at propagating alias information. */
1544 for (ui = 0; ui < reg_base_value_size; ui++)
1546 rtx base = reg_base_value[ui];
1547 if (base && GET_CODE (base) == REG)
1549 unsigned int base_regno = REGNO (base);
1550 if (base_regno == ui) /* register set from itself */
1551 reg_base_value[ui] = 0;
1553 reg_base_value[ui] = reg_base_value[base_regno];
1558 while (changed && pass < MAX_ALIAS_LOOP_PASSES);
1560 new_reg_base_value = 0;
1565 end_alias_analysis ()
1567 reg_known_value = 0;
1569 reg_base_value_size = 0;
1570 if (alias_invariant)
1572 free ((char *)alias_invariant);
1573 alias_invariant = 0;