1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998, 1999, 2000 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 "insn-flags.h"
31 #include "hard-reg-set.h"
36 #include "splay-tree.h"
39 /* The alias sets assigned to MEMs assist the back-end in determining
40 which MEMs can alias which other MEMs. In general, two MEMs in
41 different alias sets cannot alias each other, with one important
42 exception. Consider something like:
44 struct S {int i; double d; };
46 a store to an `S' can alias something of either type `int' or type
47 `double'. (However, a store to an `int' cannot alias a `double'
48 and vice versa.) We indicate this via a tree structure that looks
56 (The arrows are directed and point downwards.)
57 In this situation we say the alias set for `struct S' is the
58 `superset' and that those for `int' and `double' are `subsets'.
60 To see whether two alias sets can point to the same memory, we must
61 see if either alias set is a subset of the other. We need not trace
62 past immediate decendents, however, since we propagate all
63 grandchildren up one level.
65 Alias set zero is implicitly a superset of all other alias sets.
66 However, this is no actual entry for alias set zero. It is an
67 error to attempt to explicitly construct a subset of zero. */
69 typedef struct alias_set_entry
71 /* The alias set number, as stored in MEM_ALIAS_SET. */
72 HOST_WIDE_INT alias_set;
74 /* The children of the alias set. These are not just the immediate
75 children, but, in fact, all decendents. So, if we have:
77 struct T { struct S s; float f; }
79 continuing our example above, the children here will be all of
80 `int', `double', `float', and `struct S'. */
84 /* The language-specific function for alias analysis. If NULL, the
85 language does not do any special alias analysis. */
86 HOST_WIDE_INT (*lang_get_alias_set) PARAMS ((tree));
88 static int rtx_equal_for_memref_p PARAMS ((rtx, rtx));
89 static rtx find_symbolic_term PARAMS ((rtx));
90 static rtx get_addr PARAMS ((rtx));
91 static int memrefs_conflict_p PARAMS ((int, rtx, int, rtx,
93 static void record_set PARAMS ((rtx, rtx, void *));
94 static rtx find_base_term PARAMS ((rtx));
95 static int base_alias_check PARAMS ((rtx, rtx, enum machine_mode,
97 static rtx find_base_value PARAMS ((rtx));
98 static int mems_in_disjoint_alias_sets_p PARAMS ((rtx, rtx));
99 static int insert_subset_children PARAMS ((splay_tree_node, void*));
100 static tree find_base_decl PARAMS ((tree));
101 static alias_set_entry get_alias_set_entry PARAMS ((HOST_WIDE_INT));
102 static rtx fixed_scalar_and_varying_struct_p PARAMS ((rtx, rtx, rtx, rtx,
104 static int aliases_everything_p PARAMS ((rtx));
105 static int write_dependence_p PARAMS ((rtx, rtx, int));
106 static int nonlocal_reference_p PARAMS ((rtx));
108 /* Set up all info needed to perform alias analysis on memory references. */
110 /* Returns the size in bytes of the mode of X. */
111 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
113 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
114 different alias sets. We ignore alias sets in functions making use
115 of variable arguments because the va_arg macros on some systems are
117 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
118 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
120 /* Cap the number of passes we make over the insns propagating alias
121 information through set chains. 10 is a completely arbitrary choice. */
122 #define MAX_ALIAS_LOOP_PASSES 10
124 /* reg_base_value[N] gives an address to which register N is related.
125 If all sets after the first add or subtract to the current value
126 or otherwise modify it so it does not point to a different top level
127 object, reg_base_value[N] is equal to the address part of the source
130 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
131 expressions represent certain special values: function arguments and
132 the stack, frame, and argument pointers.
134 The contents of an ADDRESS is not normally used, the mode of the
135 ADDRESS determines whether the ADDRESS is a function argument or some
136 other special value. Pointer equality, not rtx_equal_p, determines whether
137 two ADDRESS expressions refer to the same base address.
139 The only use of the contents of an ADDRESS is for determining if the
140 current function performs nonlocal memory memory references for the
141 purposes of marking the function as a constant function. */
143 static rtx *reg_base_value;
144 static rtx *new_reg_base_value;
145 static unsigned int reg_base_value_size; /* size of reg_base_value array */
147 #define REG_BASE_VALUE(X) \
148 (REGNO (X) < reg_base_value_size ? reg_base_value[REGNO (X)] : 0)
150 /* Vector of known invariant relationships between registers. Set in
151 loop unrolling. Indexed by register number, if nonzero the value
152 is an expression describing this register in terms of another.
154 The length of this array is REG_BASE_VALUE_SIZE.
156 Because this array contains only pseudo registers it has no effect
158 static rtx *alias_invariant;
160 /* Vector indexed by N giving the initial (unchanging) value known for
161 pseudo-register N. This array is initialized in
162 init_alias_analysis, and does not change until end_alias_analysis
164 rtx *reg_known_value;
166 /* Indicates number of valid entries in reg_known_value. */
167 static unsigned int reg_known_value_size;
169 /* Vector recording for each reg_known_value whether it is due to a
170 REG_EQUIV note. Future passes (viz., reload) may replace the
171 pseudo with the equivalent expression and so we account for the
172 dependences that would be introduced if that happens.
174 The REG_EQUIV notes created in assign_parms may mention the arg
175 pointer, and there are explicit insns in the RTL that modify the
176 arg pointer. Thus we must ensure that such insns don't get
177 scheduled across each other because that would invalidate the
178 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
179 wrong, but solving the problem in the scheduler will likely give
180 better code, so we do it here. */
181 char *reg_known_equiv_p;
183 /* True when scanning insns from the start of the rtl to the
184 NOTE_INSN_FUNCTION_BEG note. */
185 static int copying_arguments;
187 /* The splay-tree used to store the various alias set entries. */
188 static splay_tree alias_sets;
190 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
191 such an entry, or NULL otherwise. */
193 static alias_set_entry
194 get_alias_set_entry (alias_set)
195 HOST_WIDE_INT alias_set;
198 = splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
200 return sn != 0 ? ((alias_set_entry) sn->value) : 0;
203 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
204 the two MEMs cannot alias each other. */
207 mems_in_disjoint_alias_sets_p (mem1, mem2)
213 #ifdef ENABLE_CHECKING
214 /* Perform a basic sanity check. Namely, that there are no alias sets
215 if we're not using strict aliasing. This helps to catch bugs
216 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
217 where a MEM is allocated in some way other than by the use of
218 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
219 use alias sets to indicate that spilled registers cannot alias each
220 other, we might need to remove this check. */
221 if (! flag_strict_aliasing
222 && (MEM_ALIAS_SET (mem1) != 0 || MEM_ALIAS_SET (mem2) != 0))
226 /* The code used in varargs macros are often not conforming ANSI C,
227 which can trick the compiler into making incorrect aliasing
228 assumptions in these functions. So, we don't use alias sets in
229 such a function. FIXME: This should be moved into the front-end;
230 it is a language-dependent notion, and there's no reason not to
231 still use these checks to handle globals. */
232 if (current_function_stdarg || current_function_varargs)
235 /* If have no alias set information for one of the MEMs, we have to assume
236 it can alias anything. */
237 if (MEM_ALIAS_SET (mem1) == 0 || MEM_ALIAS_SET (mem2) == 0)
240 /* If the two alias sets are the same, they may alias. */
241 if (MEM_ALIAS_SET (mem1) == MEM_ALIAS_SET (mem2))
244 /* See if the first alias set is a subset of the second. */
245 ase = get_alias_set_entry (MEM_ALIAS_SET (mem1));
246 if (ase != 0 && splay_tree_lookup (ase->children,
247 (splay_tree_key) MEM_ALIAS_SET (mem2)))
250 /* Now do the same, but with the alias sets reversed. */
251 ase = get_alias_set_entry (MEM_ALIAS_SET (mem2));
252 if (ase != 0 && splay_tree_lookup (ase->children,
253 (splay_tree_key) MEM_ALIAS_SET (mem1)))
256 /* The two MEMs are in distinct alias sets, and neither one is the
257 child of the other. Therefore, they cannot alias. */
261 /* Insert the NODE into the splay tree given by DATA. Used by
262 record_alias_subset via splay_tree_foreach. */
265 insert_subset_children (node, data)
266 splay_tree_node node;
269 splay_tree_insert ((splay_tree) data, node->key, node->value);
274 /* T is an expression with pointer type. Find the DECL on which this
275 expression is based. (For example, in `a[i]' this would be `a'.)
276 If there is no such DECL, or a unique decl cannot be determined,
277 NULL_TREE is retured. */
285 if (t == 0 || t == error_mark_node || ! POINTER_TYPE_P (TREE_TYPE (t)))
288 /* If this is a declaration, return it. */
289 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
292 /* Handle general expressions. It would be nice to deal with
293 COMPONENT_REFs here. If we could tell that `a' and `b' were the
294 same, then `a->f' and `b->f' are also the same. */
295 switch (TREE_CODE_CLASS (TREE_CODE (t)))
298 return find_base_decl (TREE_OPERAND (t, 0));
301 /* Return 0 if found in neither or both are the same. */
302 d0 = find_base_decl (TREE_OPERAND (t, 0));
303 d1 = find_base_decl (TREE_OPERAND (t, 1));
314 d0 = find_base_decl (TREE_OPERAND (t, 0));
315 d1 = find_base_decl (TREE_OPERAND (t, 1));
316 d0 = find_base_decl (TREE_OPERAND (t, 0));
317 d2 = find_base_decl (TREE_OPERAND (t, 2));
319 /* Set any nonzero values from the last, then from the first. */
320 if (d1 == 0) d1 = d2;
321 if (d0 == 0) d0 = d1;
322 if (d1 == 0) d1 = d0;
323 if (d2 == 0) d2 = d1;
325 /* At this point all are nonzero or all are zero. If all three are the
326 same, return it. Otherwise, return zero. */
327 return (d0 == d1 && d1 == d2) ? d0 : 0;
334 /* Return the alias set for T, which may be either a type or an
335 expression. Call language-specific routine for help, if needed. */
343 HOST_WIDE_INT bitsize, bitpos;
345 enum machine_mode mode;
346 int volatilep, unsignedp;
347 unsigned int alignment;
349 /* If we're not doing any alias analysis, just assume everything
350 aliases everything else. Also return 0 if this or its type is
352 if (! flag_strict_aliasing || t == error_mark_node
354 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
357 /* We can be passed either an expression or a type. This and the
358 language-specific routine may make mutually-recursive calls to
359 each other to figure out what to do. At each juncture, we see if
360 this is a tree that the language may need to handle specially.
361 First handle things that aren't types and start by removing nops
362 since we care only about the actual object. */
365 while (TREE_CODE (t) == NOP_EXPR || TREE_CODE (t) == CONVERT_EXPR
366 || TREE_CODE (t) == NON_LVALUE_EXPR)
367 t = TREE_OPERAND (t, 0);
369 /* Now give the language a chance to do something but record what we
370 gave it this time. */
372 if (lang_get_alias_set != 0
373 && (set = (*lang_get_alias_set) (t)) != -1)
376 /* If this is a reference, go inside it and use the underlying
378 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r')
379 t = get_inner_reference (t, &bitsize, &bitpos, &offset, &mode,
380 &unsignedp, &volatilep, &alignment);
382 if (TREE_CODE (t) == INDIRECT_REF)
384 /* Check for accesses through restrict-qualified pointers. */
385 tree decl = find_base_decl (TREE_OPERAND (t, 0));
387 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
388 /* We use the alias set indicated in the declaration. */
389 return DECL_POINTER_ALIAS_SET (decl);
391 /* If we have an INDIRECT_REF via a void pointer, we don't
392 know anything about what that might alias. */
393 if (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE)
397 /* Give the language another chance to do something special. */
398 if (orig_t != t && lang_get_alias_set != 0
399 && (set = (*lang_get_alias_set) (t)) != -1)
402 /* Now all we care about is the type. */
406 /* Variant qualifiers don't affect the alias set, so get the main
407 variant. If this is a type with a known alias set, return it. */
408 t = TYPE_MAIN_VARIANT (t);
409 if (TYPE_P (t) && TYPE_ALIAS_SET_KNOWN_P (t))
410 return TYPE_ALIAS_SET (t);
412 /* See if the language has special handling for this type. */
413 if (lang_get_alias_set != 0
414 && (set = (*lang_get_alias_set) (t)) != -1)
416 /* There are no objects of FUNCTION_TYPE, so there's no point in
417 using up an alias set for them. (There are, of course, pointers
418 and references to functions, but that's different.) */
419 else if (TREE_CODE (t) == FUNCTION_TYPE)
422 /* Otherwise make a new alias set for this type. */
423 set = new_alias_set ();
425 TYPE_ALIAS_SET (t) = set;
429 /* Return a brand-new alias set. */
434 static HOST_WIDE_INT last_alias_set;
436 if (flag_strict_aliasing)
437 return ++last_alias_set;
442 /* Indicate that things in SUBSET can alias things in SUPERSET, but
443 not vice versa. For example, in C, a store to an `int' can alias a
444 structure containing an `int', but not vice versa. Here, the
445 structure would be the SUPERSET and `int' the SUBSET. This
446 function should be called only once per SUPERSET/SUBSET pair.
448 It is illegal for SUPERSET to be zero; everything is implicitly a
449 subset of alias set zero. */
452 record_alias_subset (superset, subset)
453 HOST_WIDE_INT superset;
454 HOST_WIDE_INT subset;
456 alias_set_entry superset_entry;
457 alias_set_entry subset_entry;
462 superset_entry = get_alias_set_entry (superset);
463 if (superset_entry == 0)
465 /* Create an entry for the SUPERSET, so that we have a place to
466 attach the SUBSET. */
468 = (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
469 superset_entry->alias_set = superset;
470 superset_entry->children
471 = splay_tree_new (splay_tree_compare_ints, 0, 0);
472 splay_tree_insert (alias_sets, (splay_tree_key) superset,
473 (splay_tree_value) superset_entry);
477 subset_entry = get_alias_set_entry (subset);
479 /* If there is an entry for the subset, enter all of its children
480 (if they are not already present) as children of the SUPERSET. */
482 splay_tree_foreach (subset_entry->children,
483 insert_subset_children,
484 superset_entry->children);
486 /* Enter the SUBSET itself as a child of the SUPERSET. */
487 splay_tree_insert (superset_entry->children,
488 (splay_tree_key) subset, 0);
491 /* Record that component types of TYPE, if any, are part of that type for
492 aliasing purposes. For record types, we only record component types
493 for fields that are marked addressable. For array types, we always
494 record the component types, so the front end should not call this
495 function if the individual component aren't addressable. */
498 record_component_aliases (type)
501 HOST_WIDE_INT superset = get_alias_set (type);
502 HOST_WIDE_INT subset;
508 switch (TREE_CODE (type))
511 subset = get_alias_set (TREE_TYPE (type));
513 record_alias_subset (superset, subset);
518 case QUAL_UNION_TYPE:
519 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
521 subset = get_alias_set (TREE_TYPE (field));
522 if (TREE_ADDRESSABLE (field) && subset != 0 && subset != superset)
523 record_alias_subset (superset, subset);
532 /* Allocate an alias set for use in storing and reading from the varargs
536 get_varargs_alias_set ()
538 static HOST_WIDE_INT set = -1;
541 set = new_alias_set ();
546 /* Likewise, but used for the fixed portions of the frame, e.g., register
550 get_frame_alias_set ()
552 static HOST_WIDE_INT set = -1;
555 set = new_alias_set ();
560 /* Inside SRC, the source of a SET, find a base address. */
563 find_base_value (src)
566 switch (GET_CODE (src))
573 /* At the start of a function, argument registers have known base
574 values which may be lost later. Returning an ADDRESS
575 expression here allows optimization based on argument values
576 even when the argument registers are used for other purposes. */
577 if (REGNO (src) < FIRST_PSEUDO_REGISTER && copying_arguments)
578 return new_reg_base_value[REGNO (src)];
580 /* If a pseudo has a known base value, return it. Do not do this
581 for hard regs since it can result in a circular dependency
582 chain for registers which have values at function entry.
584 The test above is not sufficient because the scheduler may move
585 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
586 if (REGNO (src) >= FIRST_PSEUDO_REGISTER
587 && (unsigned) REGNO (src) < reg_base_value_size
588 && reg_base_value[REGNO (src)])
589 return reg_base_value[REGNO (src)];
594 /* Check for an argument passed in memory. Only record in the
595 copying-arguments block; it is too hard to track changes
597 if (copying_arguments
598 && (XEXP (src, 0) == arg_pointer_rtx
599 || (GET_CODE (XEXP (src, 0)) == PLUS
600 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
601 return gen_rtx_ADDRESS (VOIDmode, src);
606 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
609 /* ... fall through ... */
614 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
616 /* If either operand is a REG, then see if we already have
617 a known value for it. */
618 if (GET_CODE (src_0) == REG)
620 temp = find_base_value (src_0);
625 if (GET_CODE (src_1) == REG)
627 temp = find_base_value (src_1);
632 /* Guess which operand is the base address:
633 If either operand is a symbol, then it is the base. If
634 either operand is a CONST_INT, then the other is the base. */
635 if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
636 return find_base_value (src_0);
637 else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
638 return find_base_value (src_1);
640 /* This might not be necessary anymore:
641 If either operand is a REG that is a known pointer, then it
643 else if (GET_CODE (src_0) == REG && REGNO_POINTER_FLAG (REGNO (src_0)))
644 return find_base_value (src_0);
645 else if (GET_CODE (src_1) == REG && REGNO_POINTER_FLAG (REGNO (src_1)))
646 return find_base_value (src_1);
652 /* The standard form is (lo_sum reg sym) so look only at the
654 return find_base_value (XEXP (src, 1));
657 /* If the second operand is constant set the base
658 address to the first operand. */
659 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
660 return find_base_value (XEXP (src, 0));
664 case SIGN_EXTEND: /* used for NT/Alpha pointers */
666 return find_base_value (XEXP (src, 0));
675 /* Called from init_alias_analysis indirectly through note_stores. */
677 /* While scanning insns to find base values, reg_seen[N] is nonzero if
678 register N has been set in this function. */
679 static char *reg_seen;
681 /* Addresses which are known not to alias anything else are identified
682 by a unique integer. */
683 static int unique_id;
686 record_set (dest, set, data)
688 void *data ATTRIBUTE_UNUSED;
690 register unsigned regno;
693 if (GET_CODE (dest) != REG)
696 regno = REGNO (dest);
698 if (regno >= reg_base_value_size)
703 /* A CLOBBER wipes out any old value but does not prevent a previously
704 unset register from acquiring a base address (i.e. reg_seen is not
706 if (GET_CODE (set) == CLOBBER)
708 new_reg_base_value[regno] = 0;
717 new_reg_base_value[regno] = 0;
721 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
722 GEN_INT (unique_id++));
726 /* This is not the first set. If the new value is not related to the
727 old value, forget the base value. Note that the following code is
729 extern int x, y; int *p = &x; p += (&y-&x);
730 ANSI C does not allow computing the difference of addresses
731 of distinct top level objects. */
732 if (new_reg_base_value[regno])
733 switch (GET_CODE (src))
738 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
739 new_reg_base_value[regno] = 0;
742 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
743 new_reg_base_value[regno] = 0;
746 new_reg_base_value[regno] = 0;
749 /* If this is the first set of a register, record the value. */
750 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
751 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
752 new_reg_base_value[regno] = find_base_value (src);
757 /* Called from loop optimization when a new pseudo-register is
758 created. It indicates that REGNO is being set to VAL. f INVARIANT
759 is true then this value also describes an invariant relationship
760 which can be used to deduce that two registers with unknown values
764 record_base_value (regno, val, invariant)
769 if (regno >= reg_base_value_size)
772 if (invariant && alias_invariant)
773 alias_invariant[regno] = val;
775 if (GET_CODE (val) == REG)
777 if (REGNO (val) < reg_base_value_size)
778 reg_base_value[regno] = reg_base_value[REGNO (val)];
783 reg_base_value[regno] = find_base_value (val);
786 /* Returns a canonical version of X, from the point of view alias
787 analysis. (For example, if X is a MEM whose address is a register,
788 and the register has a known value (say a SYMBOL_REF), then a MEM
789 whose address is the SYMBOL_REF is returned.) */
795 /* Recursively look for equivalences. */
796 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
797 && REGNO (x) < reg_known_value_size)
798 return reg_known_value[REGNO (x)] == x
799 ? x : canon_rtx (reg_known_value[REGNO (x)]);
800 else if (GET_CODE (x) == PLUS)
802 rtx x0 = canon_rtx (XEXP (x, 0));
803 rtx x1 = canon_rtx (XEXP (x, 1));
805 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
807 /* We can tolerate LO_SUMs being offset here; these
808 rtl are used for nothing other than comparisons. */
809 if (GET_CODE (x0) == CONST_INT)
810 return plus_constant_for_output (x1, INTVAL (x0));
811 else if (GET_CODE (x1) == CONST_INT)
812 return plus_constant_for_output (x0, INTVAL (x1));
813 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
817 /* This gives us much better alias analysis when called from
818 the loop optimizer. Note we want to leave the original
819 MEM alone, but need to return the canonicalized MEM with
820 all the flags with their original values. */
821 else if (GET_CODE (x) == MEM)
823 rtx addr = canon_rtx (XEXP (x, 0));
825 if (addr != XEXP (x, 0))
827 rtx new = gen_rtx_MEM (GET_MODE (x), addr);
829 MEM_COPY_ATTRIBUTES (new, x);
836 /* Return 1 if X and Y are identical-looking rtx's.
838 We use the data in reg_known_value above to see if two registers with
839 different numbers are, in fact, equivalent. */
842 rtx_equal_for_memref_p (x, y)
847 register enum rtx_code code;
848 register const char *fmt;
850 if (x == 0 && y == 0)
852 if (x == 0 || y == 0)
862 /* Rtx's of different codes cannot be equal. */
863 if (code != GET_CODE (y))
866 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
867 (REG:SI x) and (REG:HI x) are NOT equivalent. */
869 if (GET_MODE (x) != GET_MODE (y))
872 /* Some RTL can be compared without a recursive examination. */
876 return REGNO (x) == REGNO (y);
879 return XEXP (x, 0) == XEXP (y, 0);
882 return XSTR (x, 0) == XSTR (y, 0);
886 /* There's no need to compare the contents of CONST_DOUBLEs or
887 CONST_INTs because pointer equality is a good enough
888 comparison for these nodes. */
892 return (REGNO (XEXP (x, 0)) == REGNO (XEXP (y, 0))
893 && XINT (x, 1) == XINT (y, 1));
899 /* For commutative operations, the RTX match if the operand match in any
900 order. Also handle the simple binary and unary cases without a loop. */
901 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
902 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
903 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
904 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
905 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
906 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
907 return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
908 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
909 else if (GET_RTX_CLASS (code) == '1')
910 return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
912 /* Compare the elements. If any pair of corresponding elements
913 fail to match, return 0 for the whole things.
915 Limit cases to types which actually appear in addresses. */
917 fmt = GET_RTX_FORMAT (code);
918 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
923 if (XINT (x, i) != XINT (y, i))
928 /* Two vectors must have the same length. */
929 if (XVECLEN (x, i) != XVECLEN (y, i))
932 /* And the corresponding elements must match. */
933 for (j = 0; j < XVECLEN (x, i); j++)
934 if (rtx_equal_for_memref_p (XVECEXP (x, i, j),
935 XVECEXP (y, i, j)) == 0)
940 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
944 /* This can happen for an asm which clobbers memory. */
948 /* It is believed that rtx's at this level will never
949 contain anything but integers and other rtx's,
950 except for within LABEL_REFs and SYMBOL_REFs. */
958 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
959 X and return it, or return 0 if none found. */
962 find_symbolic_term (x)
966 register enum rtx_code code;
967 register const char *fmt;
970 if (code == SYMBOL_REF || code == LABEL_REF)
972 if (GET_RTX_CLASS (code) == 'o')
975 fmt = GET_RTX_FORMAT (code);
976 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
982 t = find_symbolic_term (XEXP (x, i));
986 else if (fmt[i] == 'E')
997 struct elt_loc_list *l;
999 switch (GET_CODE (x))
1002 return REG_BASE_VALUE (x);
1005 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1011 return find_base_term (XEXP (x, 0));
1014 val = CSELIB_VAL_PTR (x);
1015 for (l = val->locs; l; l = l->next)
1016 if ((x = find_base_term (l->loc)) != 0)
1022 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1029 rtx tmp1 = XEXP (x, 0);
1030 rtx tmp2 = XEXP (x, 1);
1032 /* This is a litle bit tricky since we have to determine which of
1033 the two operands represents the real base address. Otherwise this
1034 routine may return the index register instead of the base register.
1036 That may cause us to believe no aliasing was possible, when in
1037 fact aliasing is possible.
1039 We use a few simple tests to guess the base register. Additional
1040 tests can certainly be added. For example, if one of the operands
1041 is a shift or multiply, then it must be the index register and the
1042 other operand is the base register. */
1044 /* If either operand is known to be a pointer, then use it
1045 to determine the base term. */
1046 if (REG_P (tmp1) && REGNO_POINTER_FLAG (REGNO (tmp1)))
1047 return find_base_term (tmp1);
1049 if (REG_P (tmp2) && REGNO_POINTER_FLAG (REGNO (tmp2)))
1050 return find_base_term (tmp2);
1052 /* Neither operand was known to be a pointer. Go ahead and find the
1053 base term for both operands. */
1054 tmp1 = find_base_term (tmp1);
1055 tmp2 = find_base_term (tmp2);
1057 /* If either base term is named object or a special address
1058 (like an argument or stack reference), then use it for the
1061 && (GET_CODE (tmp1) == SYMBOL_REF
1062 || GET_CODE (tmp1) == LABEL_REF
1063 || (GET_CODE (tmp1) == ADDRESS
1064 && GET_MODE (tmp1) != VOIDmode)))
1068 && (GET_CODE (tmp2) == SYMBOL_REF
1069 || GET_CODE (tmp2) == LABEL_REF
1070 || (GET_CODE (tmp2) == ADDRESS
1071 && GET_MODE (tmp2) != VOIDmode)))
1074 /* We could not determine which of the two operands was the
1075 base register and which was the index. So we can determine
1076 nothing from the base alias check. */
1081 if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
1082 return REG_BASE_VALUE (XEXP (x, 0));
1094 /* Return 0 if the addresses X and Y are known to point to different
1095 objects, 1 if they might be pointers to the same object. */
1098 base_alias_check (x, y, x_mode, y_mode)
1100 enum machine_mode x_mode, y_mode;
1102 rtx x_base = find_base_term (x);
1103 rtx y_base = find_base_term (y);
1105 /* If the address itself has no known base see if a known equivalent
1106 value has one. If either address still has no known base, nothing
1107 is known about aliasing. */
1112 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1115 x_base = find_base_term (x_c);
1123 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1126 y_base = find_base_term (y_c);
1131 /* If the base addresses are equal nothing is known about aliasing. */
1132 if (rtx_equal_p (x_base, y_base))
1135 /* The base addresses of the read and write are different expressions.
1136 If they are both symbols and they are not accessed via AND, there is
1137 no conflict. We can bring knowledge of object alignment into play
1138 here. For example, on alpha, "char a, b;" can alias one another,
1139 though "char a; long b;" cannot. */
1140 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1142 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1144 if (GET_CODE (x) == AND
1145 && (GET_CODE (XEXP (x, 1)) != CONST_INT
1146 || GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1148 if (GET_CODE (y) == AND
1149 && (GET_CODE (XEXP (y, 1)) != CONST_INT
1150 || GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1152 /* Differing symbols never alias. */
1156 /* If one address is a stack reference there can be no alias:
1157 stack references using different base registers do not alias,
1158 a stack reference can not alias a parameter, and a stack reference
1159 can not alias a global. */
1160 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1161 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1164 if (! flag_argument_noalias)
1167 if (flag_argument_noalias > 1)
1170 /* Weak noalias assertion (arguments are distinct, but may match globals). */
1171 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1174 /* Convert the address X into something we can use. This is done by returning
1175 it unchanged unless it is a value; in the latter case we call cselib to get
1176 a more useful rtx. */
1183 struct elt_loc_list *l;
1185 if (GET_CODE (x) != VALUE)
1187 v = CSELIB_VAL_PTR (x);
1188 for (l = v->locs; l; l = l->next)
1189 if (CONSTANT_P (l->loc))
1191 for (l = v->locs; l; l = l->next)
1192 if (GET_CODE (l->loc) != REG && GET_CODE (l->loc) != MEM)
1195 return v->locs->loc;
1199 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1200 where SIZE is the size in bytes of the memory reference. If ADDR
1201 is not modified by the memory reference then ADDR is returned. */
1204 addr_side_effect_eval (addr, size, n_refs)
1211 switch (GET_CODE (addr))
1214 offset = (n_refs + 1) * size;
1217 offset = -(n_refs + 1) * size;
1220 offset = n_refs * size;
1223 offset = -n_refs * size;
1231 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
1233 addr = XEXP (addr, 0);
1238 /* Return nonzero if X and Y (memory addresses) could reference the
1239 same location in memory. C is an offset accumulator. When
1240 C is nonzero, we are testing aliases between X and Y + C.
1241 XSIZE is the size in bytes of the X reference,
1242 similarly YSIZE is the size in bytes for Y.
1244 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1245 referenced (the reference was BLKmode), so make the most pessimistic
1248 If XSIZE or YSIZE is negative, we may access memory outside the object
1249 being referenced as a side effect. This can happen when using AND to
1250 align memory references, as is done on the Alpha.
1252 Nice to notice that varying addresses cannot conflict with fp if no
1253 local variables had their addresses taken, but that's too hard now. */
1256 memrefs_conflict_p (xsize, x, ysize, y, c)
1261 if (GET_CODE (x) == VALUE)
1263 if (GET_CODE (y) == VALUE)
1265 if (GET_CODE (x) == HIGH)
1267 else if (GET_CODE (x) == LO_SUM)
1270 x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
1271 if (GET_CODE (y) == HIGH)
1273 else if (GET_CODE (y) == LO_SUM)
1276 y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
1278 if (rtx_equal_for_memref_p (x, y))
1280 if (xsize <= 0 || ysize <= 0)
1282 if (c >= 0 && xsize > c)
1284 if (c < 0 && ysize+c > 0)
1289 /* This code used to check for conflicts involving stack references and
1290 globals but the base address alias code now handles these cases. */
1292 if (GET_CODE (x) == PLUS)
1294 /* The fact that X is canonicalized means that this
1295 PLUS rtx is canonicalized. */
1296 rtx x0 = XEXP (x, 0);
1297 rtx x1 = XEXP (x, 1);
1299 if (GET_CODE (y) == PLUS)
1301 /* The fact that Y is canonicalized means that this
1302 PLUS rtx is canonicalized. */
1303 rtx y0 = XEXP (y, 0);
1304 rtx y1 = XEXP (y, 1);
1306 if (rtx_equal_for_memref_p (x1, y1))
1307 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1308 if (rtx_equal_for_memref_p (x0, y0))
1309 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1310 if (GET_CODE (x1) == CONST_INT)
1312 if (GET_CODE (y1) == CONST_INT)
1313 return memrefs_conflict_p (xsize, x0, ysize, y0,
1314 c - INTVAL (x1) + INTVAL (y1));
1316 return memrefs_conflict_p (xsize, x0, ysize, y,
1319 else if (GET_CODE (y1) == CONST_INT)
1320 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1324 else if (GET_CODE (x1) == CONST_INT)
1325 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1327 else if (GET_CODE (y) == PLUS)
1329 /* The fact that Y is canonicalized means that this
1330 PLUS rtx is canonicalized. */
1331 rtx y0 = XEXP (y, 0);
1332 rtx y1 = XEXP (y, 1);
1334 if (GET_CODE (y1) == CONST_INT)
1335 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1340 if (GET_CODE (x) == GET_CODE (y))
1341 switch (GET_CODE (x))
1345 /* Handle cases where we expect the second operands to be the
1346 same, and check only whether the first operand would conflict
1349 rtx x1 = canon_rtx (XEXP (x, 1));
1350 rtx y1 = canon_rtx (XEXP (y, 1));
1351 if (! rtx_equal_for_memref_p (x1, y1))
1353 x0 = canon_rtx (XEXP (x, 0));
1354 y0 = canon_rtx (XEXP (y, 0));
1355 if (rtx_equal_for_memref_p (x0, y0))
1356 return (xsize == 0 || ysize == 0
1357 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1359 /* Can't properly adjust our sizes. */
1360 if (GET_CODE (x1) != CONST_INT)
1362 xsize /= INTVAL (x1);
1363 ysize /= INTVAL (x1);
1365 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1369 /* Are these registers known not to be equal? */
1370 if (alias_invariant)
1372 unsigned int r_x = REGNO (x), r_y = REGNO (y);
1373 rtx i_x, i_y; /* invariant relationships of X and Y */
1375 i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1376 i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1378 if (i_x == 0 && i_y == 0)
1381 if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1382 ysize, i_y ? i_y : y, c))
1391 /* Treat an access through an AND (e.g. a subword access on an Alpha)
1392 as an access with indeterminate size. Assume that references
1393 besides AND are aligned, so if the size of the other reference is
1394 at least as large as the alignment, assume no other overlap. */
1395 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1397 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1399 return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1401 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1403 /* ??? If we are indexing far enough into the array/structure, we
1404 may yet be able to determine that we can not overlap. But we
1405 also need to that we are far enough from the end not to overlap
1406 a following reference, so we do nothing with that for now. */
1407 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1409 return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1414 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1416 c += (INTVAL (y) - INTVAL (x));
1417 return (xsize <= 0 || ysize <= 0
1418 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1421 if (GET_CODE (x) == CONST)
1423 if (GET_CODE (y) == CONST)
1424 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1425 ysize, canon_rtx (XEXP (y, 0)), c);
1427 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1430 if (GET_CODE (y) == CONST)
1431 return memrefs_conflict_p (xsize, x, ysize,
1432 canon_rtx (XEXP (y, 0)), c);
1435 return (xsize < 0 || ysize < 0
1436 || (rtx_equal_for_memref_p (x, y)
1437 && (xsize == 0 || ysize == 0
1438 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1445 /* Functions to compute memory dependencies.
1447 Since we process the insns in execution order, we can build tables
1448 to keep track of what registers are fixed (and not aliased), what registers
1449 are varying in known ways, and what registers are varying in unknown
1452 If both memory references are volatile, then there must always be a
1453 dependence between the two references, since their order can not be
1454 changed. A volatile and non-volatile reference can be interchanged
1457 A MEM_IN_STRUCT reference at a non-AND varying address can never
1458 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
1459 also must allow AND addresses, because they may generate accesses
1460 outside the object being referenced. This is used to generate
1461 aligned addresses from unaligned addresses, for instance, the alpha
1462 storeqi_unaligned pattern. */
1464 /* Read dependence: X is read after read in MEM takes place. There can
1465 only be a dependence here if both reads are volatile. */
1468 read_dependence (mem, x)
1472 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1475 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1476 MEM2 is a reference to a structure at a varying address, or returns
1477 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1478 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1479 to decide whether or not an address may vary; it should return
1480 nonzero whenever variation is possible.
1481 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
1484 fixed_scalar_and_varying_struct_p (mem1, mem2, mem1_addr, mem2_addr, varies_p)
1486 rtx mem1_addr, mem2_addr;
1487 int (*varies_p) PARAMS ((rtx));
1489 if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
1490 && !varies_p (mem1_addr) && varies_p (mem2_addr))
1491 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1495 if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
1496 && varies_p (mem1_addr) && !varies_p (mem2_addr))
1497 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1504 /* Returns nonzero if something about the mode or address format MEM1
1505 indicates that it might well alias *anything*. */
1508 aliases_everything_p (mem)
1511 if (GET_CODE (XEXP (mem, 0)) == AND)
1512 /* If the address is an AND, its very hard to know at what it is
1513 actually pointing. */
1519 /* True dependence: X is read after store in MEM takes place. */
1522 true_dependence (mem, mem_mode, x, varies)
1524 enum machine_mode mem_mode;
1526 int (*varies) PARAMS ((rtx));
1528 register rtx x_addr, mem_addr;
1530 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1533 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1536 /* If X is an unchanging read, then it can't possibly conflict with any
1537 non-unchanging store. It may conflict with an unchanging write though,
1538 because there may be a single store to this address to initialize it.
1539 Just fall through to the code below to resolve the case where we have
1540 both an unchanging read and an unchanging write. This won't handle all
1541 cases optimally, but the possible performance loss should be
1543 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1546 if (mem_mode == VOIDmode)
1547 mem_mode = GET_MODE (mem);
1549 x_addr = get_addr (XEXP (x, 0));
1550 mem_addr = get_addr (XEXP (mem, 0));
1552 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1555 x_addr = canon_rtx (x_addr);
1556 mem_addr = canon_rtx (mem_addr);
1558 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1559 SIZE_FOR_MODE (x), x_addr, 0))
1562 if (aliases_everything_p (x))
1565 /* We cannot use aliases_everyting_p to test MEM, since we must look
1566 at MEM_MODE, rather than GET_MODE (MEM). */
1567 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1570 /* In true_dependence we also allow BLKmode to alias anything. Why
1571 don't we do this in anti_dependence and output_dependence? */
1572 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1575 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1579 /* Returns non-zero if a write to X might alias a previous read from
1580 (or, if WRITEP is non-zero, a write to) MEM. */
1583 write_dependence_p (mem, x, writep)
1588 rtx x_addr, mem_addr;
1591 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1594 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1597 /* If MEM is an unchanging read, then it can't possibly conflict with
1598 the store to X, because there is at most one store to MEM, and it must
1599 have occurred somewhere before MEM. */
1600 if (!writep && RTX_UNCHANGING_P (mem))
1603 x_addr = get_addr (XEXP (x, 0));
1604 mem_addr = get_addr (XEXP (mem, 0));
1606 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
1610 x_addr = canon_rtx (x_addr);
1611 mem_addr = canon_rtx (mem_addr);
1613 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
1614 SIZE_FOR_MODE (x), x_addr, 0))
1618 = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1621 return (!(fixed_scalar == mem && !aliases_everything_p (x))
1622 && !(fixed_scalar == x && !aliases_everything_p (mem)));
1625 /* Anti dependence: X is written after read in MEM takes place. */
1628 anti_dependence (mem, x)
1632 return write_dependence_p (mem, x, /*writep=*/0);
1635 /* Output dependence: X is written after store in MEM takes place. */
1638 output_dependence (mem, x)
1642 return write_dependence_p (mem, x, /*writep=*/1);
1645 /* Returns non-zero if X might refer to something which is not
1646 local to the function and is not constant. */
1649 nonlocal_reference_p (x)
1653 register RTX_CODE code;
1656 code = GET_CODE (x);
1658 if (GET_RTX_CLASS (code) == 'i')
1660 /* Constant functions can be constant if they don't use
1661 scratch memory used to mark function w/o side effects. */
1662 if (code == CALL_INSN && CONST_CALL_P (x))
1664 x = CALL_INSN_FUNCTION_USAGE (x);
1670 code = GET_CODE (x);
1676 if (GET_CODE (SUBREG_REG (x)) == REG)
1678 /* Global registers are not local. */
1679 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
1680 && global_regs[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)])
1688 /* Global registers are not local. */
1689 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1703 /* Constants in the function's constants pool are constant. */
1704 if (CONSTANT_POOL_ADDRESS_P (x))
1709 /* Recursion introduces no additional considerations. */
1710 if (GET_CODE (XEXP (x, 0)) == MEM
1711 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1712 && strcmp(XSTR (XEXP (XEXP (x, 0), 0), 0),
1713 IDENTIFIER_POINTER (
1714 DECL_ASSEMBLER_NAME (current_function_decl))) == 0)
1719 /* Be overly conservative and consider any volatile memory
1720 reference as not local. */
1721 if (MEM_VOLATILE_P (x))
1723 base = find_base_term (XEXP (x, 0));
1726 /* A Pmode ADDRESS could be a reference via the structure value
1727 address or static chain. Such memory references are nonlocal.
1729 Thus, we have to examine the contents of the ADDRESS to find
1730 out if this is a local reference or not. */
1731 if (GET_CODE (base) == ADDRESS
1732 && GET_MODE (base) == Pmode
1733 && (XEXP (base, 0) == stack_pointer_rtx
1734 || XEXP (base, 0) == arg_pointer_rtx
1735 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1736 || XEXP (base, 0) == hard_frame_pointer_rtx
1738 || XEXP (base, 0) == frame_pointer_rtx))
1740 /* Constants in the function's constant pool are constant. */
1741 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
1754 /* Recursively scan the operands of this expression. */
1757 register const char *fmt = GET_RTX_FORMAT (code);
1760 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1762 if (fmt[i] == 'e' && XEXP (x, i))
1764 if (nonlocal_reference_p (XEXP (x, i)))
1767 else if (fmt[i] == 'E')
1770 for (j = 0; j < XVECLEN (x, i); j++)
1771 if (nonlocal_reference_p (XVECEXP (x, i, j)))
1780 /* Mark the function if it is constant. */
1783 mark_constant_function ()
1787 if (TREE_PUBLIC (current_function_decl)
1788 || TREE_READONLY (current_function_decl)
1789 || TREE_THIS_VOLATILE (current_function_decl)
1790 || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode)
1793 /* Determine if this is a constant function. */
1795 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1796 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1797 && nonlocal_reference_p (insn))
1800 /* Mark the function. */
1802 TREE_READONLY (current_function_decl) = 1;
1806 static HARD_REG_SET argument_registers;
1813 #ifndef OUTGOING_REGNO
1814 #define OUTGOING_REGNO(N) N
1816 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1817 /* Check whether this register can hold an incoming pointer
1818 argument. FUNCTION_ARG_REGNO_P tests outgoing register
1819 numbers, so translate if necessary due to register windows. */
1820 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
1821 && HARD_REGNO_MODE_OK (i, Pmode))
1822 SET_HARD_REG_BIT (argument_registers, i);
1824 alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
1827 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
1831 init_alias_analysis ()
1833 int maxreg = max_reg_num ();
1836 register unsigned int ui;
1839 reg_known_value_size = maxreg;
1842 = (rtx *) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (rtx))
1843 - FIRST_PSEUDO_REGISTER;
1845 = (char*) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (char))
1846 - FIRST_PSEUDO_REGISTER;
1848 /* Overallocate reg_base_value to allow some growth during loop
1849 optimization. Loop unrolling can create a large number of
1851 reg_base_value_size = maxreg * 2;
1852 reg_base_value = (rtx *) xcalloc (reg_base_value_size, sizeof (rtx));
1854 ggc_add_rtx_root (reg_base_value, reg_base_value_size);
1856 new_reg_base_value = (rtx *) xmalloc (reg_base_value_size * sizeof (rtx));
1857 reg_seen = (char *) xmalloc (reg_base_value_size);
1858 if (! reload_completed && flag_unroll_loops)
1860 /* ??? Why are we realloc'ing if we're just going to zero it? */
1861 alias_invariant = (rtx *)xrealloc (alias_invariant,
1862 reg_base_value_size * sizeof (rtx));
1863 bzero ((char *)alias_invariant, reg_base_value_size * sizeof (rtx));
1867 /* The basic idea is that each pass through this loop will use the
1868 "constant" information from the previous pass to propagate alias
1869 information through another level of assignments.
1871 This could get expensive if the assignment chains are long. Maybe
1872 we should throttle the number of iterations, possibly based on
1873 the optimization level or flag_expensive_optimizations.
1875 We could propagate more information in the first pass by making use
1876 of REG_N_SETS to determine immediately that the alias information
1877 for a pseudo is "constant".
1879 A program with an uninitialized variable can cause an infinite loop
1880 here. Instead of doing a full dataflow analysis to detect such problems
1881 we just cap the number of iterations for the loop.
1883 The state of the arrays for the set chain in question does not matter
1884 since the program has undefined behavior. */
1889 /* Assume nothing will change this iteration of the loop. */
1892 /* We want to assign the same IDs each iteration of this loop, so
1893 start counting from zero each iteration of the loop. */
1896 /* We're at the start of the funtion each iteration through the
1897 loop, so we're copying arguments. */
1898 copying_arguments = 1;
1900 /* Wipe the potential alias information clean for this pass. */
1901 bzero ((char *) new_reg_base_value, reg_base_value_size * sizeof (rtx));
1903 /* Wipe the reg_seen array clean. */
1904 bzero ((char *) reg_seen, reg_base_value_size);
1906 /* Mark all hard registers which may contain an address.
1907 The stack, frame and argument pointers may contain an address.
1908 An argument register which can hold a Pmode value may contain
1909 an address even if it is not in BASE_REGS.
1911 The address expression is VOIDmode for an argument and
1912 Pmode for other registers. */
1914 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1915 if (TEST_HARD_REG_BIT (argument_registers, i))
1916 new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
1917 gen_rtx_REG (Pmode, i));
1919 new_reg_base_value[STACK_POINTER_REGNUM]
1920 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
1921 new_reg_base_value[ARG_POINTER_REGNUM]
1922 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
1923 new_reg_base_value[FRAME_POINTER_REGNUM]
1924 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
1925 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1926 new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
1927 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
1929 if (struct_value_incoming_rtx
1930 && GET_CODE (struct_value_incoming_rtx) == REG)
1931 new_reg_base_value[REGNO (struct_value_incoming_rtx)]
1932 = gen_rtx_ADDRESS (Pmode, struct_value_incoming_rtx);
1934 if (static_chain_rtx
1935 && GET_CODE (static_chain_rtx) == REG)
1936 new_reg_base_value[REGNO (static_chain_rtx)]
1937 = gen_rtx_ADDRESS (Pmode, static_chain_rtx);
1939 /* Walk the insns adding values to the new_reg_base_value array. */
1940 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1942 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1946 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
1947 if (prologue_epilogue_contains (insn))
1951 /* If this insn has a noalias note, process it, Otherwise,
1952 scan for sets. A simple set will have no side effects
1953 which could change the base value of any other register. */
1955 if (GET_CODE (PATTERN (insn)) == SET
1956 && REG_NOTES (insn) != 0
1957 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
1958 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
1960 note_stores (PATTERN (insn), record_set, NULL);
1962 set = single_set (insn);
1965 && GET_CODE (SET_DEST (set)) == REG
1966 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
1967 && REG_NOTES (insn) != 0
1968 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
1969 && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
1970 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1971 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
1972 && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
1974 int regno = REGNO (SET_DEST (set));
1975 reg_known_value[regno] = XEXP (note, 0);
1976 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
1979 else if (GET_CODE (insn) == NOTE
1980 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
1981 copying_arguments = 0;
1984 /* Now propagate values from new_reg_base_value to reg_base_value. */
1985 for (ui = 0; ui < reg_base_value_size; ui++)
1987 if (new_reg_base_value[ui]
1988 && new_reg_base_value[ui] != reg_base_value[ui]
1989 && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
1991 reg_base_value[ui] = new_reg_base_value[ui];
1996 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
1998 /* Fill in the remaining entries. */
1999 for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
2000 if (reg_known_value[i] == 0)
2001 reg_known_value[i] = regno_reg_rtx[i];
2003 /* Simplify the reg_base_value array so that no register refers to
2004 another register, except to special registers indirectly through
2005 ADDRESS expressions.
2007 In theory this loop can take as long as O(registers^2), but unless
2008 there are very long dependency chains it will run in close to linear
2011 This loop may not be needed any longer now that the main loop does
2012 a better job at propagating alias information. */
2018 for (ui = 0; ui < reg_base_value_size; ui++)
2020 rtx base = reg_base_value[ui];
2021 if (base && GET_CODE (base) == REG)
2023 unsigned int base_regno = REGNO (base);
2024 if (base_regno == ui) /* register set from itself */
2025 reg_base_value[ui] = 0;
2027 reg_base_value[ui] = reg_base_value[base_regno];
2032 while (changed && pass < MAX_ALIAS_LOOP_PASSES);
2035 free (new_reg_base_value);
2036 new_reg_base_value = 0;
2042 end_alias_analysis ()
2044 free (reg_known_value + FIRST_PSEUDO_REGISTER);
2045 reg_known_value = 0;
2046 reg_known_value_size = 0;
2047 free (reg_known_equiv_p + FIRST_PSEUDO_REGISTER);
2048 reg_known_equiv_p = 0;
2052 ggc_del_root (reg_base_value);
2053 free (reg_base_value);
2056 reg_base_value_size = 0;
2057 if (alias_invariant)
2059 free (alias_invariant);
2060 alias_invariant = 0;