1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by John Carr (jfc@mit.edu).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
34 #include "hard-reg-set.h"
35 #include "basic-block.h"
40 #include "splay-tree.h"
42 #include "langhooks.h"
48 /* The alias sets assigned to MEMs assist the back-end in determining
49 which MEMs can alias which other MEMs. In general, two MEMs in
50 different alias sets cannot alias each other, with one important
51 exception. Consider something like:
53 struct S { int i; double d; };
55 a store to an `S' can alias something of either type `int' or type
56 `double'. (However, a store to an `int' cannot alias a `double'
57 and vice versa.) We indicate this via a tree structure that looks
65 (The arrows are directed and point downwards.)
66 In this situation we say the alias set for `struct S' is the
67 `superset' and that those for `int' and `double' are `subsets'.
69 To see whether two alias sets can point to the same memory, we must
70 see if either alias set is a subset of the other. We need not trace
71 past immediate descendants, however, since we propagate all
72 grandchildren up one level.
74 Alias set zero is implicitly a superset of all other alias sets.
75 However, this is no actual entry for alias set zero. It is an
76 error to attempt to explicitly construct a subset of zero. */
78 struct alias_set_entry
GTY(())
80 /* The alias set number, as stored in MEM_ALIAS_SET. */
81 HOST_WIDE_INT alias_set
;
83 /* The children of the alias set. These are not just the immediate
84 children, but, in fact, all descendants. So, if we have:
86 struct T { struct S s; float f; }
88 continuing our example above, the children here will be all of
89 `int', `double', `float', and `struct S'. */
90 splay_tree
GTY((param1_is (int), param2_is (int))) children
;
92 /* Nonzero if would have a child of zero: this effectively makes this
93 alias set the same as alias set zero. */
96 typedef struct alias_set_entry
*alias_set_entry
;
98 static int rtx_equal_for_memref_p (rtx
, rtx
);
99 static rtx
find_symbolic_term (rtx
);
100 static int memrefs_conflict_p (int, rtx
, int, rtx
, HOST_WIDE_INT
);
101 static void record_set (rtx
, rtx
, void *);
102 static int base_alias_check (rtx
, rtx
, enum machine_mode
,
104 static rtx
find_base_value (rtx
);
105 static int mems_in_disjoint_alias_sets_p (rtx
, rtx
);
106 static int insert_subset_children (splay_tree_node
, void*);
107 static tree
find_base_decl (tree
);
108 static alias_set_entry
get_alias_set_entry (HOST_WIDE_INT
);
109 static rtx
fixed_scalar_and_varying_struct_p (rtx
, rtx
, rtx
, rtx
,
111 static int aliases_everything_p (rtx
);
112 static bool nonoverlapping_component_refs_p (tree
, tree
);
113 static tree
decl_for_component_ref (tree
);
114 static rtx
adjust_offset_for_component_ref (tree
, rtx
);
115 static int nonoverlapping_memrefs_p (rtx
, rtx
);
116 static int write_dependence_p (rtx
, rtx
, int);
118 static int nonlocal_mentioned_p_1 (rtx
*, void *);
119 static int nonlocal_mentioned_p (rtx
);
120 static int nonlocal_referenced_p_1 (rtx
*, void *);
121 static int nonlocal_referenced_p (rtx
);
122 static int nonlocal_set_p_1 (rtx
*, void *);
123 static int nonlocal_set_p (rtx
);
124 static void memory_modified_1 (rtx
, rtx
, void *);
126 /* Set up all info needed to perform alias analysis on memory references. */
128 /* Returns the size in bytes of the mode of X. */
129 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
131 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
132 different alias sets. We ignore alias sets in functions making use
133 of variable arguments because the va_arg macros on some systems are
135 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
136 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
138 /* Cap the number of passes we make over the insns propagating alias
139 information through set chains. 10 is a completely arbitrary choice. */
140 #define MAX_ALIAS_LOOP_PASSES 10
142 /* reg_base_value[N] gives an address to which register N is related.
143 If all sets after the first add or subtract to the current value
144 or otherwise modify it so it does not point to a different top level
145 object, reg_base_value[N] is equal to the address part of the source
148 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
149 expressions represent certain special values: function arguments and
150 the stack, frame, and argument pointers.
152 The contents of an ADDRESS is not normally used, the mode of the
153 ADDRESS determines whether the ADDRESS is a function argument or some
154 other special value. Pointer equality, not rtx_equal_p, determines whether
155 two ADDRESS expressions refer to the same base address.
157 The only use of the contents of an ADDRESS is for determining if the
158 current function performs nonlocal memory memory references for the
159 purposes of marking the function as a constant function. */
161 static GTY(()) varray_type reg_base_value
;
162 static rtx
*new_reg_base_value
;
164 /* We preserve the copy of old array around to avoid amount of garbage
165 produced. About 8% of garbage produced were attributed to this
167 static GTY((deletable
)) varray_type old_reg_base_value
;
169 /* Static hunks of RTL used by the aliasing code; these are initialized
170 once per function to avoid unnecessary RTL allocations. */
171 static GTY (()) rtx static_reg_base_value
[FIRST_PSEUDO_REGISTER
];
173 #define REG_BASE_VALUE(X) \
174 (reg_base_value && REGNO (X) < VARRAY_SIZE (reg_base_value) \
175 ? VARRAY_RTX (reg_base_value, REGNO (X)) : 0)
177 /* Vector of known invariant relationships between registers. Set in
178 loop unrolling. Indexed by register number, if nonzero the value
179 is an expression describing this register in terms of another.
181 The length of this array is REG_BASE_VALUE_SIZE.
183 Because this array contains only pseudo registers it has no effect
185 static GTY((length("alias_invariant_size"))) rtx
*alias_invariant
;
186 static GTY(()) unsigned int alias_invariant_size
;
188 /* Vector indexed by N giving the initial (unchanging) value known for
189 pseudo-register N. This array is initialized in init_alias_analysis,
190 and does not change until end_alias_analysis is called. */
191 static GTY((length("reg_known_value_size"))) rtx
*reg_known_value
;
193 /* Indicates number of valid entries in reg_known_value. */
194 static GTY(()) unsigned int reg_known_value_size
;
196 /* Vector recording for each reg_known_value whether it is due to a
197 REG_EQUIV note. Future passes (viz., reload) may replace the
198 pseudo with the equivalent expression and so we account for the
199 dependences that would be introduced if that happens.
201 The REG_EQUIV notes created in assign_parms may mention the arg
202 pointer, and there are explicit insns in the RTL that modify the
203 arg pointer. Thus we must ensure that such insns don't get
204 scheduled across each other because that would invalidate the
205 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
206 wrong, but solving the problem in the scheduler will likely give
207 better code, so we do it here. */
208 static bool *reg_known_equiv_p
;
210 /* True when scanning insns from the start of the rtl to the
211 NOTE_INSN_FUNCTION_BEG note. */
212 static bool copying_arguments
;
214 /* The splay-tree used to store the various alias set entries. */
215 static GTY ((param_is (struct alias_set_entry
))) varray_type alias_sets
;
217 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
218 such an entry, or NULL otherwise. */
220 static inline alias_set_entry
221 get_alias_set_entry (HOST_WIDE_INT alias_set
)
223 return (alias_set_entry
)VARRAY_GENERIC_PTR (alias_sets
, alias_set
);
226 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
227 the two MEMs cannot alias each other. */
230 mems_in_disjoint_alias_sets_p (rtx mem1
, rtx mem2
)
232 #ifdef ENABLE_CHECKING
233 /* Perform a basic sanity check. Namely, that there are no alias sets
234 if we're not using strict aliasing. This helps to catch bugs
235 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
236 where a MEM is allocated in some way other than by the use of
237 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
238 use alias sets to indicate that spilled registers cannot alias each
239 other, we might need to remove this check. */
240 if (! flag_strict_aliasing
241 && (MEM_ALIAS_SET (mem1
) != 0 || MEM_ALIAS_SET (mem2
) != 0))
245 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1
), MEM_ALIAS_SET (mem2
));
248 /* Insert the NODE into the splay tree given by DATA. Used by
249 record_alias_subset via splay_tree_foreach. */
252 insert_subset_children (splay_tree_node node
, void *data
)
254 splay_tree_insert ((splay_tree
) data
, node
->key
, node
->value
);
259 /* Return 1 if the two specified alias sets may conflict. */
262 alias_sets_conflict_p (HOST_WIDE_INT set1
, HOST_WIDE_INT set2
)
266 /* If have no alias set information for one of the operands, we have
267 to assume it can alias anything. */
268 if (set1
== 0 || set2
== 0
269 /* If the two alias sets are the same, they may alias. */
273 /* See if the first alias set is a subset of the second. */
274 ase
= get_alias_set_entry (set1
);
276 && (ase
->has_zero_child
277 || splay_tree_lookup (ase
->children
,
278 (splay_tree_key
) set2
)))
281 /* Now do the same, but with the alias sets reversed. */
282 ase
= get_alias_set_entry (set2
);
284 && (ase
->has_zero_child
285 || splay_tree_lookup (ase
->children
,
286 (splay_tree_key
) set1
)))
289 /* The two alias sets are distinct and neither one is the
290 child of the other. Therefore, they cannot alias. */
294 /* Return 1 if the two specified alias sets might conflict, or if any subtype
295 of these alias sets might conflict. */
298 alias_sets_might_conflict_p (HOST_WIDE_INT set1
, HOST_WIDE_INT set2
)
300 if (set1
== 0 || set2
== 0 || set1
== set2
)
307 /* Return 1 if any MEM object of type T1 will always conflict (using the
308 dependency routines in this file) with any MEM object of type T2.
309 This is used when allocating temporary storage. If T1 and/or T2 are
310 NULL_TREE, it means we know nothing about the storage. */
313 objects_must_conflict_p (tree t1
, tree t2
)
315 HOST_WIDE_INT set1
, set2
;
317 /* If neither has a type specified, we don't know if they'll conflict
318 because we may be using them to store objects of various types, for
319 example the argument and local variables areas of inlined functions. */
320 if (t1
== 0 && t2
== 0)
323 /* If they are the same type, they must conflict. */
325 /* Likewise if both are volatile. */
326 || (t1
!= 0 && TYPE_VOLATILE (t1
) && t2
!= 0 && TYPE_VOLATILE (t2
)))
329 set1
= t1
? get_alias_set (t1
) : 0;
330 set2
= t2
? get_alias_set (t2
) : 0;
332 /* Otherwise they conflict if they have no alias set or the same. We
333 can't simply use alias_sets_conflict_p here, because we must make
334 sure that every subtype of t1 will conflict with every subtype of
335 t2 for which a pair of subobjects of these respective subtypes
336 overlaps on the stack. */
337 return set1
== 0 || set2
== 0 || set1
== set2
;
340 /* T is an expression with pointer type. Find the DECL on which this
341 expression is based. (For example, in `a[i]' this would be `a'.)
342 If there is no such DECL, or a unique decl cannot be determined,
343 NULL_TREE is returned. */
346 find_base_decl (tree t
)
350 if (t
== 0 || t
== error_mark_node
|| ! POINTER_TYPE_P (TREE_TYPE (t
)))
353 /* If this is a declaration, return it. */
354 if (TREE_CODE_CLASS (TREE_CODE (t
)) == 'd')
357 /* Handle general expressions. It would be nice to deal with
358 COMPONENT_REFs here. If we could tell that `a' and `b' were the
359 same, then `a->f' and `b->f' are also the same. */
360 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
363 return find_base_decl (TREE_OPERAND (t
, 0));
366 /* Return 0 if found in neither or both are the same. */
367 d0
= find_base_decl (TREE_OPERAND (t
, 0));
368 d1
= find_base_decl (TREE_OPERAND (t
, 1));
379 d0
= find_base_decl (TREE_OPERAND (t
, 0));
380 d1
= find_base_decl (TREE_OPERAND (t
, 1));
381 d2
= find_base_decl (TREE_OPERAND (t
, 2));
383 /* Set any nonzero values from the last, then from the first. */
384 if (d1
== 0) d1
= d2
;
385 if (d0
== 0) d0
= d1
;
386 if (d1
== 0) d1
= d0
;
387 if (d2
== 0) d2
= d1
;
389 /* At this point all are nonzero or all are zero. If all three are the
390 same, return it. Otherwise, return zero. */
391 return (d0
== d1
&& d1
== d2
) ? d0
: 0;
398 /* Return 1 if all the nested component references handled by
399 get_inner_reference in T are such that we can address the object in T. */
402 can_address_p (tree t
)
404 /* If we're at the end, it is vacuously addressable. */
405 if (! handled_component_p (t
))
408 /* Bitfields are never addressable. */
409 else if (TREE_CODE (t
) == BIT_FIELD_REF
)
412 /* Fields are addressable unless they are marked as nonaddressable or
413 the containing type has alias set 0. */
414 else if (TREE_CODE (t
) == COMPONENT_REF
415 && ! DECL_NONADDRESSABLE_P (TREE_OPERAND (t
, 1))
416 && get_alias_set (TREE_TYPE (TREE_OPERAND (t
, 0))) != 0
417 && can_address_p (TREE_OPERAND (t
, 0)))
420 /* Likewise for arrays. */
421 else if ((TREE_CODE (t
) == ARRAY_REF
|| TREE_CODE (t
) == ARRAY_RANGE_REF
)
422 && ! TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t
, 0)))
423 && get_alias_set (TREE_TYPE (TREE_OPERAND (t
, 0))) != 0
424 && can_address_p (TREE_OPERAND (t
, 0)))
430 /* Return the alias set for T, which may be either a type or an
431 expression. Call language-specific routine for help, if needed. */
434 get_alias_set (tree t
)
438 /* If we're not doing any alias analysis, just assume everything
439 aliases everything else. Also return 0 if this or its type is
441 if (! flag_strict_aliasing
|| t
== error_mark_node
443 && (TREE_TYPE (t
) == 0 || TREE_TYPE (t
) == error_mark_node
)))
446 /* We can be passed either an expression or a type. This and the
447 language-specific routine may make mutually-recursive calls to each other
448 to figure out what to do. At each juncture, we see if this is a tree
449 that the language may need to handle specially. First handle things that
455 /* Remove any nops, then give the language a chance to do
456 something with this tree before we look at it. */
458 set
= lang_hooks
.get_alias_set (t
);
462 /* First see if the actual object referenced is an INDIRECT_REF from a
463 restrict-qualified pointer or a "void *". */
464 while (handled_component_p (inner
))
466 inner
= TREE_OPERAND (inner
, 0);
470 /* Check for accesses through restrict-qualified pointers. */
471 if (TREE_CODE (inner
) == INDIRECT_REF
)
473 tree decl
= find_base_decl (TREE_OPERAND (inner
, 0));
475 if (decl
&& DECL_POINTER_ALIAS_SET_KNOWN_P (decl
))
477 /* If we haven't computed the actual alias set, do it now. */
478 if (DECL_POINTER_ALIAS_SET (decl
) == -2)
480 tree pointed_to_type
= TREE_TYPE (TREE_TYPE (decl
));
482 /* No two restricted pointers can point at the same thing.
483 However, a restricted pointer can point at the same thing
484 as an unrestricted pointer, if that unrestricted pointer
485 is based on the restricted pointer. So, we make the
486 alias set for the restricted pointer a subset of the
487 alias set for the type pointed to by the type of the
489 HOST_WIDE_INT pointed_to_alias_set
490 = get_alias_set (pointed_to_type
);
492 if (pointed_to_alias_set
== 0)
493 /* It's not legal to make a subset of alias set zero. */
494 DECL_POINTER_ALIAS_SET (decl
) = 0;
495 else if (AGGREGATE_TYPE_P (pointed_to_type
))
496 /* For an aggregate, we must treat the restricted
497 pointer the same as an ordinary pointer. If we
498 were to make the type pointed to by the
499 restricted pointer a subset of the pointed-to
500 type, then we would believe that other subsets
501 of the pointed-to type (such as fields of that
502 type) do not conflict with the type pointed to
503 by the restricted pointer. */
504 DECL_POINTER_ALIAS_SET (decl
)
505 = pointed_to_alias_set
;
508 DECL_POINTER_ALIAS_SET (decl
) = new_alias_set ();
509 record_alias_subset (pointed_to_alias_set
,
510 DECL_POINTER_ALIAS_SET (decl
));
514 /* We use the alias set indicated in the declaration. */
515 return DECL_POINTER_ALIAS_SET (decl
);
518 /* If we have an INDIRECT_REF via a void pointer, we don't
519 know anything about what that might alias. Likewise if the
520 pointer is marked that way. */
521 else if (TREE_CODE (TREE_TYPE (inner
)) == VOID_TYPE
522 || (TYPE_REF_CAN_ALIAS_ALL
523 (TREE_TYPE (TREE_OPERAND (inner
, 0)))))
527 /* Otherwise, pick up the outermost object that we could have a pointer
528 to, processing conversions as above. */
529 while (handled_component_p (t
) && ! can_address_p (t
))
531 t
= TREE_OPERAND (t
, 0);
535 /* If we've already determined the alias set for a decl, just return
536 it. This is necessary for C++ anonymous unions, whose component
537 variables don't look like union members (boo!). */
538 if (TREE_CODE (t
) == VAR_DECL
539 && DECL_RTL_SET_P (t
) && MEM_P (DECL_RTL (t
)))
540 return MEM_ALIAS_SET (DECL_RTL (t
));
542 /* Now all we care about is the type. */
546 /* Variant qualifiers don't affect the alias set, so get the main
547 variant. If this is a type with a known alias set, return it. */
548 t
= TYPE_MAIN_VARIANT (t
);
549 if (TYPE_ALIAS_SET_KNOWN_P (t
))
550 return TYPE_ALIAS_SET (t
);
552 /* See if the language has special handling for this type. */
553 set
= lang_hooks
.get_alias_set (t
);
557 /* There are no objects of FUNCTION_TYPE, so there's no point in
558 using up an alias set for them. (There are, of course, pointers
559 and references to functions, but that's different.) */
560 else if (TREE_CODE (t
) == FUNCTION_TYPE
)
563 /* Unless the language specifies otherwise, let vector types alias
564 their components. This avoids some nasty type punning issues in
565 normal usage. And indeed lets vectors be treated more like an
567 else if (TREE_CODE (t
) == VECTOR_TYPE
)
568 set
= get_alias_set (TREE_TYPE (t
));
571 /* Otherwise make a new alias set for this type. */
572 set
= new_alias_set ();
574 TYPE_ALIAS_SET (t
) = set
;
576 /* If this is an aggregate type, we must record any component aliasing
578 if (AGGREGATE_TYPE_P (t
) || TREE_CODE (t
) == COMPLEX_TYPE
)
579 record_component_aliases (t
);
584 /* Return a brand-new alias set. */
586 static GTY(()) HOST_WIDE_INT last_alias_set
;
591 if (flag_strict_aliasing
)
594 VARRAY_GENERIC_PTR_INIT (alias_sets
, 10, "alias sets");
596 VARRAY_GROW (alias_sets
, last_alias_set
+ 2);
597 return ++last_alias_set
;
603 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
604 not everything that aliases SUPERSET also aliases SUBSET. For example,
605 in C, a store to an `int' can alias a load of a structure containing an
606 `int', and vice versa. But it can't alias a load of a 'double' member
607 of the same structure. Here, the structure would be the SUPERSET and
608 `int' the SUBSET. This relationship is also described in the comment at
609 the beginning of this file.
611 This function should be called only once per SUPERSET/SUBSET pair.
613 It is illegal for SUPERSET to be zero; everything is implicitly a
614 subset of alias set zero. */
617 record_alias_subset (HOST_WIDE_INT superset
, HOST_WIDE_INT subset
)
619 alias_set_entry superset_entry
;
620 alias_set_entry subset_entry
;
622 /* It is possible in complex type situations for both sets to be the same,
623 in which case we can ignore this operation. */
624 if (superset
== subset
)
630 superset_entry
= get_alias_set_entry (superset
);
631 if (superset_entry
== 0)
633 /* Create an entry for the SUPERSET, so that we have a place to
634 attach the SUBSET. */
635 superset_entry
= ggc_alloc (sizeof (struct alias_set_entry
));
636 superset_entry
->alias_set
= superset
;
637 superset_entry
->children
638 = splay_tree_new_ggc (splay_tree_compare_ints
);
639 superset_entry
->has_zero_child
= 0;
640 VARRAY_GENERIC_PTR (alias_sets
, superset
) = superset_entry
;
644 superset_entry
->has_zero_child
= 1;
647 subset_entry
= get_alias_set_entry (subset
);
648 /* If there is an entry for the subset, enter all of its children
649 (if they are not already present) as children of the SUPERSET. */
652 if (subset_entry
->has_zero_child
)
653 superset_entry
->has_zero_child
= 1;
655 splay_tree_foreach (subset_entry
->children
, insert_subset_children
,
656 superset_entry
->children
);
659 /* Enter the SUBSET itself as a child of the SUPERSET. */
660 splay_tree_insert (superset_entry
->children
,
661 (splay_tree_key
) subset
, 0);
665 /* Record that component types of TYPE, if any, are part of that type for
666 aliasing purposes. For record types, we only record component types
667 for fields that are marked addressable. For array types, we always
668 record the component types, so the front end should not call this
669 function if the individual component aren't addressable. */
672 record_component_aliases (tree type
)
674 HOST_WIDE_INT superset
= get_alias_set (type
);
680 switch (TREE_CODE (type
))
683 if (! TYPE_NONALIASED_COMPONENT (type
))
684 record_alias_subset (superset
, get_alias_set (TREE_TYPE (type
)));
689 case QUAL_UNION_TYPE
:
690 /* Recursively record aliases for the base classes, if there are any. */
691 if (TYPE_BINFO (type
))
694 tree binfo
, base_binfo
;
696 for (binfo
= TYPE_BINFO (type
), i
= 0;
697 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
698 record_alias_subset (superset
,
699 get_alias_set (BINFO_TYPE (base_binfo
)));
701 for (field
= TYPE_FIELDS (type
); field
!= 0; field
= TREE_CHAIN (field
))
702 if (TREE_CODE (field
) == FIELD_DECL
&& ! DECL_NONADDRESSABLE_P (field
))
703 record_alias_subset (superset
, get_alias_set (TREE_TYPE (field
)));
707 record_alias_subset (superset
, get_alias_set (TREE_TYPE (type
)));
715 /* Allocate an alias set for use in storing and reading from the varargs
718 static GTY(()) HOST_WIDE_INT varargs_set
= -1;
721 get_varargs_alias_set (void)
724 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
725 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
726 consistently use the varargs alias set for loads from the varargs
727 area. So don't use it anywhere. */
730 if (varargs_set
== -1)
731 varargs_set
= new_alias_set ();
737 /* Likewise, but used for the fixed portions of the frame, e.g., register
740 static GTY(()) HOST_WIDE_INT frame_set
= -1;
743 get_frame_alias_set (void)
746 frame_set
= new_alias_set ();
751 /* Inside SRC, the source of a SET, find a base address. */
754 find_base_value (rtx src
)
758 switch (GET_CODE (src
))
766 /* At the start of a function, argument registers have known base
767 values which may be lost later. Returning an ADDRESS
768 expression here allows optimization based on argument values
769 even when the argument registers are used for other purposes. */
770 if (regno
< FIRST_PSEUDO_REGISTER
&& copying_arguments
)
771 return new_reg_base_value
[regno
];
773 /* If a pseudo has a known base value, return it. Do not do this
774 for non-fixed hard regs since it can result in a circular
775 dependency chain for registers which have values at function entry.
777 The test above is not sufficient because the scheduler may move
778 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
779 if ((regno
>= FIRST_PSEUDO_REGISTER
|| fixed_regs
[regno
])
780 && regno
< VARRAY_SIZE (reg_base_value
))
782 /* If we're inside init_alias_analysis, use new_reg_base_value
783 to reduce the number of relaxation iterations. */
784 if (new_reg_base_value
&& new_reg_base_value
[regno
]
785 && REG_N_SETS (regno
) == 1)
786 return new_reg_base_value
[regno
];
788 if (VARRAY_RTX (reg_base_value
, regno
))
789 return VARRAY_RTX (reg_base_value
, regno
);
795 /* Check for an argument passed in memory. Only record in the
796 copying-arguments block; it is too hard to track changes
798 if (copying_arguments
799 && (XEXP (src
, 0) == arg_pointer_rtx
800 || (GET_CODE (XEXP (src
, 0)) == PLUS
801 && XEXP (XEXP (src
, 0), 0) == arg_pointer_rtx
)))
802 return gen_rtx_ADDRESS (VOIDmode
, src
);
807 if (GET_CODE (src
) != PLUS
&& GET_CODE (src
) != MINUS
)
810 /* ... fall through ... */
815 rtx temp
, src_0
= XEXP (src
, 0), src_1
= XEXP (src
, 1);
817 /* If either operand is a REG that is a known pointer, then it
819 if (REG_P (src_0
) && REG_POINTER (src_0
))
820 return find_base_value (src_0
);
821 if (REG_P (src_1
) && REG_POINTER (src_1
))
822 return find_base_value (src_1
);
824 /* If either operand is a REG, then see if we already have
825 a known value for it. */
828 temp
= find_base_value (src_0
);
835 temp
= find_base_value (src_1
);
840 /* If either base is named object or a special address
841 (like an argument or stack reference), then use it for the
844 && (GET_CODE (src_0
) == SYMBOL_REF
845 || GET_CODE (src_0
) == LABEL_REF
846 || (GET_CODE (src_0
) == ADDRESS
847 && GET_MODE (src_0
) != VOIDmode
)))
851 && (GET_CODE (src_1
) == SYMBOL_REF
852 || GET_CODE (src_1
) == LABEL_REF
853 || (GET_CODE (src_1
) == ADDRESS
854 && GET_MODE (src_1
) != VOIDmode
)))
857 /* Guess which operand is the base address:
858 If either operand is a symbol, then it is the base. If
859 either operand is a CONST_INT, then the other is the base. */
860 if (GET_CODE (src_1
) == CONST_INT
|| CONSTANT_P (src_0
))
861 return find_base_value (src_0
);
862 else if (GET_CODE (src_0
) == CONST_INT
|| CONSTANT_P (src_1
))
863 return find_base_value (src_1
);
869 /* The standard form is (lo_sum reg sym) so look only at the
871 return find_base_value (XEXP (src
, 1));
874 /* If the second operand is constant set the base
875 address to the first operand. */
876 if (GET_CODE (XEXP (src
, 1)) == CONST_INT
&& INTVAL (XEXP (src
, 1)) != 0)
877 return find_base_value (XEXP (src
, 0));
881 if (GET_MODE_SIZE (GET_MODE (src
)) < GET_MODE_SIZE (Pmode
))
891 return find_base_value (XEXP (src
, 0));
894 case SIGN_EXTEND
: /* used for NT/Alpha pointers */
896 rtx temp
= find_base_value (XEXP (src
, 0));
898 if (temp
!= 0 && CONSTANT_P (temp
))
899 temp
= convert_memory_address (Pmode
, temp
);
911 /* Called from init_alias_analysis indirectly through note_stores. */
913 /* While scanning insns to find base values, reg_seen[N] is nonzero if
914 register N has been set in this function. */
915 static char *reg_seen
;
917 /* Addresses which are known not to alias anything else are identified
918 by a unique integer. */
919 static int unique_id
;
922 record_set (rtx dest
, rtx set
, void *data ATTRIBUTE_UNUSED
)
931 regno
= REGNO (dest
);
933 if (regno
>= VARRAY_SIZE (reg_base_value
))
936 /* If this spans multiple hard registers, then we must indicate that every
937 register has an unusable value. */
938 if (regno
< FIRST_PSEUDO_REGISTER
)
939 n
= hard_regno_nregs
[regno
][GET_MODE (dest
)];
946 reg_seen
[regno
+ n
] = 1;
947 new_reg_base_value
[regno
+ n
] = 0;
954 /* A CLOBBER wipes out any old value but does not prevent a previously
955 unset register from acquiring a base address (i.e. reg_seen is not
957 if (GET_CODE (set
) == CLOBBER
)
959 new_reg_base_value
[regno
] = 0;
968 new_reg_base_value
[regno
] = 0;
972 new_reg_base_value
[regno
] = gen_rtx_ADDRESS (Pmode
,
973 GEN_INT (unique_id
++));
977 /* If this is not the first set of REGNO, see whether the new value
978 is related to the old one. There are two cases of interest:
980 (1) The register might be assigned an entirely new value
981 that has the same base term as the original set.
983 (2) The set might be a simple self-modification that
984 cannot change REGNO's base value.
986 If neither case holds, reject the original base value as invalid.
987 Note that the following situation is not detected:
989 extern int x, y; int *p = &x; p += (&y-&x);
991 ANSI C does not allow computing the difference of addresses
992 of distinct top level objects. */
993 if (new_reg_base_value
[regno
] != 0
994 && find_base_value (src
) != new_reg_base_value
[regno
])
995 switch (GET_CODE (src
))
999 if (XEXP (src
, 0) != dest
&& XEXP (src
, 1) != dest
)
1000 new_reg_base_value
[regno
] = 0;
1003 /* If the value we add in the PLUS is also a valid base value,
1004 this might be the actual base value, and the original value
1007 rtx other
= NULL_RTX
;
1009 if (XEXP (src
, 0) == dest
)
1010 other
= XEXP (src
, 1);
1011 else if (XEXP (src
, 1) == dest
)
1012 other
= XEXP (src
, 0);
1014 if (! other
|| find_base_value (other
))
1015 new_reg_base_value
[regno
] = 0;
1019 if (XEXP (src
, 0) != dest
|| GET_CODE (XEXP (src
, 1)) != CONST_INT
)
1020 new_reg_base_value
[regno
] = 0;
1023 new_reg_base_value
[regno
] = 0;
1026 /* If this is the first set of a register, record the value. */
1027 else if ((regno
>= FIRST_PSEUDO_REGISTER
|| ! fixed_regs
[regno
])
1028 && ! reg_seen
[regno
] && new_reg_base_value
[regno
] == 0)
1029 new_reg_base_value
[regno
] = find_base_value (src
);
1031 reg_seen
[regno
] = 1;
1034 /* Called from loop optimization when a new pseudo-register is
1035 created. It indicates that REGNO is being set to VAL. f INVARIANT
1036 is true then this value also describes an invariant relationship
1037 which can be used to deduce that two registers with unknown values
1041 record_base_value (unsigned int regno
, rtx val
, int invariant
)
1043 if (invariant
&& alias_invariant
&& regno
< alias_invariant_size
)
1044 alias_invariant
[regno
] = val
;
1046 if (regno
>= VARRAY_SIZE (reg_base_value
))
1047 VARRAY_GROW (reg_base_value
, max_reg_num ());
1051 VARRAY_RTX (reg_base_value
, regno
)
1052 = REG_BASE_VALUE (val
);
1055 VARRAY_RTX (reg_base_value
, regno
)
1056 = find_base_value (val
);
1059 /* Clear alias info for a register. This is used if an RTL transformation
1060 changes the value of a register. This is used in flow by AUTO_INC_DEC
1061 optimizations. We don't need to clear reg_base_value, since flow only
1062 changes the offset. */
1065 clear_reg_alias_info (rtx reg
)
1067 unsigned int regno
= REGNO (reg
);
1069 if (regno
>= FIRST_PSEUDO_REGISTER
)
1071 regno
-= FIRST_PSEUDO_REGISTER
;
1072 if (regno
< reg_known_value_size
)
1074 reg_known_value
[regno
] = reg
;
1075 reg_known_equiv_p
[regno
] = false;
1080 /* If a value is known for REGNO, return it. */
1083 get_reg_known_value (unsigned int regno
)
1085 if (regno
>= FIRST_PSEUDO_REGISTER
)
1087 regno
-= FIRST_PSEUDO_REGISTER
;
1088 if (regno
< reg_known_value_size
)
1089 return reg_known_value
[regno
];
1097 set_reg_known_value (unsigned int regno
, rtx val
)
1099 if (regno
>= FIRST_PSEUDO_REGISTER
)
1101 regno
-= FIRST_PSEUDO_REGISTER
;
1102 if (regno
< reg_known_value_size
)
1103 reg_known_value
[regno
] = val
;
1107 /* Similarly for reg_known_equiv_p. */
1110 get_reg_known_equiv_p (unsigned int regno
)
1112 if (regno
>= FIRST_PSEUDO_REGISTER
)
1114 regno
-= FIRST_PSEUDO_REGISTER
;
1115 if (regno
< reg_known_value_size
)
1116 return reg_known_equiv_p
[regno
];
1122 set_reg_known_equiv_p (unsigned int regno
, bool val
)
1124 if (regno
>= FIRST_PSEUDO_REGISTER
)
1126 regno
-= FIRST_PSEUDO_REGISTER
;
1127 if (regno
< reg_known_value_size
)
1128 reg_known_equiv_p
[regno
] = val
;
1133 /* Returns a canonical version of X, from the point of view alias
1134 analysis. (For example, if X is a MEM whose address is a register,
1135 and the register has a known value (say a SYMBOL_REF), then a MEM
1136 whose address is the SYMBOL_REF is returned.) */
1141 /* Recursively look for equivalences. */
1142 if (REG_P (x
) && REGNO (x
) >= FIRST_PSEUDO_REGISTER
)
1144 rtx t
= get_reg_known_value (REGNO (x
));
1148 return canon_rtx (t
);
1151 if (GET_CODE (x
) == PLUS
)
1153 rtx x0
= canon_rtx (XEXP (x
, 0));
1154 rtx x1
= canon_rtx (XEXP (x
, 1));
1156 if (x0
!= XEXP (x
, 0) || x1
!= XEXP (x
, 1))
1158 if (GET_CODE (x0
) == CONST_INT
)
1159 return plus_constant (x1
, INTVAL (x0
));
1160 else if (GET_CODE (x1
) == CONST_INT
)
1161 return plus_constant (x0
, INTVAL (x1
));
1162 return gen_rtx_PLUS (GET_MODE (x
), x0
, x1
);
1166 /* This gives us much better alias analysis when called from
1167 the loop optimizer. Note we want to leave the original
1168 MEM alone, but need to return the canonicalized MEM with
1169 all the flags with their original values. */
1171 x
= replace_equiv_address_nv (x
, canon_rtx (XEXP (x
, 0)));
1176 /* Return 1 if X and Y are identical-looking rtx's.
1177 Expect that X and Y has been already canonicalized.
1179 We use the data in reg_known_value above to see if two registers with
1180 different numbers are, in fact, equivalent. */
1183 rtx_equal_for_memref_p (rtx x
, rtx y
)
1190 if (x
== 0 && y
== 0)
1192 if (x
== 0 || y
== 0)
1198 code
= GET_CODE (x
);
1199 /* Rtx's of different codes cannot be equal. */
1200 if (code
!= GET_CODE (y
))
1203 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1204 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1206 if (GET_MODE (x
) != GET_MODE (y
))
1209 /* Some RTL can be compared without a recursive examination. */
1213 return REGNO (x
) == REGNO (y
);
1216 return XEXP (x
, 0) == XEXP (y
, 0);
1219 return XSTR (x
, 0) == XSTR (y
, 0);
1224 /* There's no need to compare the contents of CONST_DOUBLEs or
1225 CONST_INTs because pointer equality is a good enough
1226 comparison for these nodes. */
1233 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1235 return ((rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 0))
1236 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 1)))
1237 || (rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 1))
1238 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 0))));
1239 /* For commutative operations, the RTX match if the operand match in any
1240 order. Also handle the simple binary and unary cases without a loop. */
1241 if (COMMUTATIVE_P (x
))
1243 rtx xop0
= canon_rtx (XEXP (x
, 0));
1244 rtx yop0
= canon_rtx (XEXP (y
, 0));
1245 rtx yop1
= canon_rtx (XEXP (y
, 1));
1247 return ((rtx_equal_for_memref_p (xop0
, yop0
)
1248 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop1
))
1249 || (rtx_equal_for_memref_p (xop0
, yop1
)
1250 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop0
)));
1252 else if (NON_COMMUTATIVE_P (x
))
1254 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1255 canon_rtx (XEXP (y
, 0)))
1256 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)),
1257 canon_rtx (XEXP (y
, 1))));
1259 else if (UNARY_P (x
))
1260 return rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1261 canon_rtx (XEXP (y
, 0)));
1263 /* Compare the elements. If any pair of corresponding elements
1264 fail to match, return 0 for the whole things.
1266 Limit cases to types which actually appear in addresses. */
1268 fmt
= GET_RTX_FORMAT (code
);
1269 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1274 if (XINT (x
, i
) != XINT (y
, i
))
1279 /* Two vectors must have the same length. */
1280 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1283 /* And the corresponding elements must match. */
1284 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1285 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x
, i
, j
)),
1286 canon_rtx (XVECEXP (y
, i
, j
))) == 0)
1291 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, i
)),
1292 canon_rtx (XEXP (y
, i
))) == 0)
1296 /* This can happen for asm operands. */
1298 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1302 /* This can happen for an asm which clobbers memory. */
1306 /* It is believed that rtx's at this level will never
1307 contain anything but integers and other rtx's,
1308 except for within LABEL_REFs and SYMBOL_REFs. */
1316 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
1317 X and return it, or return 0 if none found. */
1320 find_symbolic_term (rtx x
)
1326 code
= GET_CODE (x
);
1327 if (code
== SYMBOL_REF
|| code
== LABEL_REF
)
1332 fmt
= GET_RTX_FORMAT (code
);
1333 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1339 t
= find_symbolic_term (XEXP (x
, i
));
1343 else if (fmt
[i
] == 'E')
1350 find_base_term (rtx x
)
1353 struct elt_loc_list
*l
;
1355 #if defined (FIND_BASE_TERM)
1356 /* Try machine-dependent ways to find the base term. */
1357 x
= FIND_BASE_TERM (x
);
1360 switch (GET_CODE (x
))
1363 return REG_BASE_VALUE (x
);
1366 if (GET_MODE_SIZE (GET_MODE (x
)) < GET_MODE_SIZE (Pmode
))
1376 return find_base_term (XEXP (x
, 0));
1379 case SIGN_EXTEND
: /* Used for Alpha/NT pointers */
1381 rtx temp
= find_base_term (XEXP (x
, 0));
1383 if (temp
!= 0 && CONSTANT_P (temp
))
1384 temp
= convert_memory_address (Pmode
, temp
);
1390 val
= CSELIB_VAL_PTR (x
);
1393 for (l
= val
->locs
; l
; l
= l
->next
)
1394 if ((x
= find_base_term (l
->loc
)) != 0)
1400 if (GET_CODE (x
) != PLUS
&& GET_CODE (x
) != MINUS
)
1407 rtx tmp1
= XEXP (x
, 0);
1408 rtx tmp2
= XEXP (x
, 1);
1410 /* This is a little bit tricky since we have to determine which of
1411 the two operands represents the real base address. Otherwise this
1412 routine may return the index register instead of the base register.
1414 That may cause us to believe no aliasing was possible, when in
1415 fact aliasing is possible.
1417 We use a few simple tests to guess the base register. Additional
1418 tests can certainly be added. For example, if one of the operands
1419 is a shift or multiply, then it must be the index register and the
1420 other operand is the base register. */
1422 if (tmp1
== pic_offset_table_rtx
&& CONSTANT_P (tmp2
))
1423 return find_base_term (tmp2
);
1425 /* If either operand is known to be a pointer, then use it
1426 to determine the base term. */
1427 if (REG_P (tmp1
) && REG_POINTER (tmp1
))
1428 return find_base_term (tmp1
);
1430 if (REG_P (tmp2
) && REG_POINTER (tmp2
))
1431 return find_base_term (tmp2
);
1433 /* Neither operand was known to be a pointer. Go ahead and find the
1434 base term for both operands. */
1435 tmp1
= find_base_term (tmp1
);
1436 tmp2
= find_base_term (tmp2
);
1438 /* If either base term is named object or a special address
1439 (like an argument or stack reference), then use it for the
1442 && (GET_CODE (tmp1
) == SYMBOL_REF
1443 || GET_CODE (tmp1
) == LABEL_REF
1444 || (GET_CODE (tmp1
) == ADDRESS
1445 && GET_MODE (tmp1
) != VOIDmode
)))
1449 && (GET_CODE (tmp2
) == SYMBOL_REF
1450 || GET_CODE (tmp2
) == LABEL_REF
1451 || (GET_CODE (tmp2
) == ADDRESS
1452 && GET_MODE (tmp2
) != VOIDmode
)))
1455 /* We could not determine which of the two operands was the
1456 base register and which was the index. So we can determine
1457 nothing from the base alias check. */
1462 if (GET_CODE (XEXP (x
, 1)) == CONST_INT
&& INTVAL (XEXP (x
, 1)) != 0)
1463 return find_base_term (XEXP (x
, 0));
1475 /* Return 0 if the addresses X and Y are known to point to different
1476 objects, 1 if they might be pointers to the same object. */
1479 base_alias_check (rtx x
, rtx y
, enum machine_mode x_mode
,
1480 enum machine_mode y_mode
)
1482 rtx x_base
= find_base_term (x
);
1483 rtx y_base
= find_base_term (y
);
1485 /* If the address itself has no known base see if a known equivalent
1486 value has one. If either address still has no known base, nothing
1487 is known about aliasing. */
1492 if (! flag_expensive_optimizations
|| (x_c
= canon_rtx (x
)) == x
)
1495 x_base
= find_base_term (x_c
);
1503 if (! flag_expensive_optimizations
|| (y_c
= canon_rtx (y
)) == y
)
1506 y_base
= find_base_term (y_c
);
1511 /* If the base addresses are equal nothing is known about aliasing. */
1512 if (rtx_equal_p (x_base
, y_base
))
1515 /* The base addresses of the read and write are different expressions.
1516 If they are both symbols and they are not accessed via AND, there is
1517 no conflict. We can bring knowledge of object alignment into play
1518 here. For example, on alpha, "char a, b;" can alias one another,
1519 though "char a; long b;" cannot. */
1520 if (GET_CODE (x_base
) != ADDRESS
&& GET_CODE (y_base
) != ADDRESS
)
1522 if (GET_CODE (x
) == AND
&& GET_CODE (y
) == AND
)
1524 if (GET_CODE (x
) == AND
1525 && (GET_CODE (XEXP (x
, 1)) != CONST_INT
1526 || (int) GET_MODE_UNIT_SIZE (y_mode
) < -INTVAL (XEXP (x
, 1))))
1528 if (GET_CODE (y
) == AND
1529 && (GET_CODE (XEXP (y
, 1)) != CONST_INT
1530 || (int) GET_MODE_UNIT_SIZE (x_mode
) < -INTVAL (XEXP (y
, 1))))
1532 /* Differing symbols never alias. */
1536 /* If one address is a stack reference there can be no alias:
1537 stack references using different base registers do not alias,
1538 a stack reference can not alias a parameter, and a stack reference
1539 can not alias a global. */
1540 if ((GET_CODE (x_base
) == ADDRESS
&& GET_MODE (x_base
) == Pmode
)
1541 || (GET_CODE (y_base
) == ADDRESS
&& GET_MODE (y_base
) == Pmode
))
1544 if (! flag_argument_noalias
)
1547 if (flag_argument_noalias
> 1)
1550 /* Weak noalias assertion (arguments are distinct, but may match globals). */
1551 return ! (GET_MODE (x_base
) == VOIDmode
&& GET_MODE (y_base
) == VOIDmode
);
1554 /* Convert the address X into something we can use. This is done by returning
1555 it unchanged unless it is a value; in the latter case we call cselib to get
1556 a more useful rtx. */
1562 struct elt_loc_list
*l
;
1564 if (GET_CODE (x
) != VALUE
)
1566 v
= CSELIB_VAL_PTR (x
);
1569 for (l
= v
->locs
; l
; l
= l
->next
)
1570 if (CONSTANT_P (l
->loc
))
1572 for (l
= v
->locs
; l
; l
= l
->next
)
1573 if (!REG_P (l
->loc
) && !MEM_P (l
->loc
))
1576 return v
->locs
->loc
;
1581 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1582 where SIZE is the size in bytes of the memory reference. If ADDR
1583 is not modified by the memory reference then ADDR is returned. */
1586 addr_side_effect_eval (rtx addr
, int size
, int n_refs
)
1590 switch (GET_CODE (addr
))
1593 offset
= (n_refs
+ 1) * size
;
1596 offset
= -(n_refs
+ 1) * size
;
1599 offset
= n_refs
* size
;
1602 offset
= -n_refs
* size
;
1610 addr
= gen_rtx_PLUS (GET_MODE (addr
), XEXP (addr
, 0),
1613 addr
= XEXP (addr
, 0);
1614 addr
= canon_rtx (addr
);
1619 /* Return nonzero if X and Y (memory addresses) could reference the
1620 same location in memory. C is an offset accumulator. When
1621 C is nonzero, we are testing aliases between X and Y + C.
1622 XSIZE is the size in bytes of the X reference,
1623 similarly YSIZE is the size in bytes for Y.
1624 Expect that canon_rtx has been already called for X and Y.
1626 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1627 referenced (the reference was BLKmode), so make the most pessimistic
1630 If XSIZE or YSIZE is negative, we may access memory outside the object
1631 being referenced as a side effect. This can happen when using AND to
1632 align memory references, as is done on the Alpha.
1634 Nice to notice that varying addresses cannot conflict with fp if no
1635 local variables had their addresses taken, but that's too hard now. */
1638 memrefs_conflict_p (int xsize
, rtx x
, int ysize
, rtx y
, HOST_WIDE_INT c
)
1640 if (GET_CODE (x
) == VALUE
)
1642 if (GET_CODE (y
) == VALUE
)
1644 if (GET_CODE (x
) == HIGH
)
1646 else if (GET_CODE (x
) == LO_SUM
)
1649 x
= addr_side_effect_eval (x
, xsize
, 0);
1650 if (GET_CODE (y
) == HIGH
)
1652 else if (GET_CODE (y
) == LO_SUM
)
1655 y
= addr_side_effect_eval (y
, ysize
, 0);
1657 if (rtx_equal_for_memref_p (x
, y
))
1659 if (xsize
<= 0 || ysize
<= 0)
1661 if (c
>= 0 && xsize
> c
)
1663 if (c
< 0 && ysize
+c
> 0)
1668 /* This code used to check for conflicts involving stack references and
1669 globals but the base address alias code now handles these cases. */
1671 if (GET_CODE (x
) == PLUS
)
1673 /* The fact that X is canonicalized means that this
1674 PLUS rtx is canonicalized. */
1675 rtx x0
= XEXP (x
, 0);
1676 rtx x1
= XEXP (x
, 1);
1678 if (GET_CODE (y
) == PLUS
)
1680 /* The fact that Y is canonicalized means that this
1681 PLUS rtx is canonicalized. */
1682 rtx y0
= XEXP (y
, 0);
1683 rtx y1
= XEXP (y
, 1);
1685 if (rtx_equal_for_memref_p (x1
, y1
))
1686 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
1687 if (rtx_equal_for_memref_p (x0
, y0
))
1688 return memrefs_conflict_p (xsize
, x1
, ysize
, y1
, c
);
1689 if (GET_CODE (x1
) == CONST_INT
)
1691 if (GET_CODE (y1
) == CONST_INT
)
1692 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
,
1693 c
- INTVAL (x1
) + INTVAL (y1
));
1695 return memrefs_conflict_p (xsize
, x0
, ysize
, y
,
1698 else if (GET_CODE (y1
) == CONST_INT
)
1699 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
1703 else if (GET_CODE (x1
) == CONST_INT
)
1704 return memrefs_conflict_p (xsize
, x0
, ysize
, y
, c
- INTVAL (x1
));
1706 else if (GET_CODE (y
) == PLUS
)
1708 /* The fact that Y is canonicalized means that this
1709 PLUS rtx is canonicalized. */
1710 rtx y0
= XEXP (y
, 0);
1711 rtx y1
= XEXP (y
, 1);
1713 if (GET_CODE (y1
) == CONST_INT
)
1714 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
1719 if (GET_CODE (x
) == GET_CODE (y
))
1720 switch (GET_CODE (x
))
1724 /* Handle cases where we expect the second operands to be the
1725 same, and check only whether the first operand would conflict
1728 rtx x1
= canon_rtx (XEXP (x
, 1));
1729 rtx y1
= canon_rtx (XEXP (y
, 1));
1730 if (! rtx_equal_for_memref_p (x1
, y1
))
1732 x0
= canon_rtx (XEXP (x
, 0));
1733 y0
= canon_rtx (XEXP (y
, 0));
1734 if (rtx_equal_for_memref_p (x0
, y0
))
1735 return (xsize
== 0 || ysize
== 0
1736 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
1738 /* Can't properly adjust our sizes. */
1739 if (GET_CODE (x1
) != CONST_INT
)
1741 xsize
/= INTVAL (x1
);
1742 ysize
/= INTVAL (x1
);
1744 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
1748 /* Are these registers known not to be equal? */
1749 if (alias_invariant
)
1751 unsigned int r_x
= REGNO (x
), r_y
= REGNO (y
);
1752 rtx i_x
, i_y
; /* invariant relationships of X and Y */
1754 i_x
= r_x
>= alias_invariant_size
? 0 : alias_invariant
[r_x
];
1755 i_y
= r_y
>= alias_invariant_size
? 0 : alias_invariant
[r_y
];
1757 if (i_x
== 0 && i_y
== 0)
1760 if (! memrefs_conflict_p (xsize
, i_x
? i_x
: x
,
1761 ysize
, i_y
? i_y
: y
, c
))
1770 /* Treat an access through an AND (e.g. a subword access on an Alpha)
1771 as an access with indeterminate size. Assume that references
1772 besides AND are aligned, so if the size of the other reference is
1773 at least as large as the alignment, assume no other overlap. */
1774 if (GET_CODE (x
) == AND
&& GET_CODE (XEXP (x
, 1)) == CONST_INT
)
1776 if (GET_CODE (y
) == AND
|| ysize
< -INTVAL (XEXP (x
, 1)))
1778 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)), ysize
, y
, c
);
1780 if (GET_CODE (y
) == AND
&& GET_CODE (XEXP (y
, 1)) == CONST_INT
)
1782 /* ??? If we are indexing far enough into the array/structure, we
1783 may yet be able to determine that we can not overlap. But we
1784 also need to that we are far enough from the end not to overlap
1785 a following reference, so we do nothing with that for now. */
1786 if (GET_CODE (x
) == AND
|| xsize
< -INTVAL (XEXP (y
, 1)))
1788 return memrefs_conflict_p (xsize
, x
, ysize
, canon_rtx (XEXP (y
, 0)), c
);
1793 if (GET_CODE (x
) == CONST_INT
&& GET_CODE (y
) == CONST_INT
)
1795 c
+= (INTVAL (y
) - INTVAL (x
));
1796 return (xsize
<= 0 || ysize
<= 0
1797 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
1800 if (GET_CODE (x
) == CONST
)
1802 if (GET_CODE (y
) == CONST
)
1803 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
1804 ysize
, canon_rtx (XEXP (y
, 0)), c
);
1806 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
1809 if (GET_CODE (y
) == CONST
)
1810 return memrefs_conflict_p (xsize
, x
, ysize
,
1811 canon_rtx (XEXP (y
, 0)), c
);
1814 return (xsize
<= 0 || ysize
<= 0
1815 || (rtx_equal_for_memref_p (x
, y
)
1816 && ((c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0))));
1823 /* Functions to compute memory dependencies.
1825 Since we process the insns in execution order, we can build tables
1826 to keep track of what registers are fixed (and not aliased), what registers
1827 are varying in known ways, and what registers are varying in unknown
1830 If both memory references are volatile, then there must always be a
1831 dependence between the two references, since their order can not be
1832 changed. A volatile and non-volatile reference can be interchanged
1835 A MEM_IN_STRUCT reference at a non-AND varying address can never
1836 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
1837 also must allow AND addresses, because they may generate accesses
1838 outside the object being referenced. This is used to generate
1839 aligned addresses from unaligned addresses, for instance, the alpha
1840 storeqi_unaligned pattern. */
1842 /* Read dependence: X is read after read in MEM takes place. There can
1843 only be a dependence here if both reads are volatile. */
1846 read_dependence (rtx mem
, rtx x
)
1848 return MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
);
1851 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1852 MEM2 is a reference to a structure at a varying address, or returns
1853 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1854 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1855 to decide whether or not an address may vary; it should return
1856 nonzero whenever variation is possible.
1857 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
1860 fixed_scalar_and_varying_struct_p (rtx mem1
, rtx mem2
, rtx mem1_addr
,
1862 int (*varies_p
) (rtx
, int))
1864 if (! flag_strict_aliasing
)
1867 if (MEM_SCALAR_P (mem1
) && MEM_IN_STRUCT_P (mem2
)
1868 && !varies_p (mem1_addr
, 1) && varies_p (mem2_addr
, 1))
1869 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1873 if (MEM_IN_STRUCT_P (mem1
) && MEM_SCALAR_P (mem2
)
1874 && varies_p (mem1_addr
, 1) && !varies_p (mem2_addr
, 1))
1875 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1882 /* Returns nonzero if something about the mode or address format MEM1
1883 indicates that it might well alias *anything*. */
1886 aliases_everything_p (rtx mem
)
1888 if (GET_CODE (XEXP (mem
, 0)) == AND
)
1889 /* If the address is an AND, its very hard to know at what it is
1890 actually pointing. */
1896 /* Return true if we can determine that the fields referenced cannot
1897 overlap for any pair of objects. */
1900 nonoverlapping_component_refs_p (tree x
, tree y
)
1902 tree fieldx
, fieldy
, typex
, typey
, orig_y
;
1906 /* The comparison has to be done at a common type, since we don't
1907 know how the inheritance hierarchy works. */
1911 fieldx
= TREE_OPERAND (x
, 1);
1912 typex
= DECL_FIELD_CONTEXT (fieldx
);
1917 fieldy
= TREE_OPERAND (y
, 1);
1918 typey
= DECL_FIELD_CONTEXT (fieldy
);
1923 y
= TREE_OPERAND (y
, 0);
1925 while (y
&& TREE_CODE (y
) == COMPONENT_REF
);
1927 x
= TREE_OPERAND (x
, 0);
1929 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
1931 /* Never found a common type. */
1935 /* If we're left with accessing different fields of a structure,
1937 if (TREE_CODE (typex
) == RECORD_TYPE
1938 && fieldx
!= fieldy
)
1941 /* The comparison on the current field failed. If we're accessing
1942 a very nested structure, look at the next outer level. */
1943 x
= TREE_OPERAND (x
, 0);
1944 y
= TREE_OPERAND (y
, 0);
1947 && TREE_CODE (x
) == COMPONENT_REF
1948 && TREE_CODE (y
) == COMPONENT_REF
);
1953 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
1956 decl_for_component_ref (tree x
)
1960 x
= TREE_OPERAND (x
, 0);
1962 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
1964 return x
&& DECL_P (x
) ? x
: NULL_TREE
;
1967 /* Walk up the COMPONENT_REF list and adjust OFFSET to compensate for the
1968 offset of the field reference. */
1971 adjust_offset_for_component_ref (tree x
, rtx offset
)
1973 HOST_WIDE_INT ioffset
;
1978 ioffset
= INTVAL (offset
);
1981 tree offset
= component_ref_field_offset (x
);
1982 tree field
= TREE_OPERAND (x
, 1);
1984 if (! host_integerp (offset
, 1))
1986 ioffset
+= (tree_low_cst (offset
, 1)
1987 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
1990 x
= TREE_OPERAND (x
, 0);
1992 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
1994 return GEN_INT (ioffset
);
1997 /* Return nonzero if we can determine the exprs corresponding to memrefs
1998 X and Y and they do not overlap. */
2001 nonoverlapping_memrefs_p (rtx x
, rtx y
)
2003 tree exprx
= MEM_EXPR (x
), expry
= MEM_EXPR (y
);
2006 rtx moffsetx
, moffsety
;
2007 HOST_WIDE_INT offsetx
= 0, offsety
= 0, sizex
, sizey
, tem
;
2009 /* Unless both have exprs, we can't tell anything. */
2010 if (exprx
== 0 || expry
== 0)
2013 /* If both are field references, we may be able to determine something. */
2014 if (TREE_CODE (exprx
) == COMPONENT_REF
2015 && TREE_CODE (expry
) == COMPONENT_REF
2016 && nonoverlapping_component_refs_p (exprx
, expry
))
2019 /* If the field reference test failed, look at the DECLs involved. */
2020 moffsetx
= MEM_OFFSET (x
);
2021 if (TREE_CODE (exprx
) == COMPONENT_REF
)
2023 tree t
= decl_for_component_ref (exprx
);
2026 moffsetx
= adjust_offset_for_component_ref (exprx
, moffsetx
);
2029 else if (TREE_CODE (exprx
) == INDIRECT_REF
)
2031 exprx
= TREE_OPERAND (exprx
, 0);
2032 if (flag_argument_noalias
< 2
2033 || TREE_CODE (exprx
) != PARM_DECL
)
2037 moffsety
= MEM_OFFSET (y
);
2038 if (TREE_CODE (expry
) == COMPONENT_REF
)
2040 tree t
= decl_for_component_ref (expry
);
2043 moffsety
= adjust_offset_for_component_ref (expry
, moffsety
);
2046 else if (TREE_CODE (expry
) == INDIRECT_REF
)
2048 expry
= TREE_OPERAND (expry
, 0);
2049 if (flag_argument_noalias
< 2
2050 || TREE_CODE (expry
) != PARM_DECL
)
2054 if (! DECL_P (exprx
) || ! DECL_P (expry
))
2057 rtlx
= DECL_RTL (exprx
);
2058 rtly
= DECL_RTL (expry
);
2060 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2061 can't overlap unless they are the same because we never reuse that part
2062 of the stack frame used for locals for spilled pseudos. */
2063 if ((!MEM_P (rtlx
) || !MEM_P (rtly
))
2064 && ! rtx_equal_p (rtlx
, rtly
))
2067 /* Get the base and offsets of both decls. If either is a register, we
2068 know both are and are the same, so use that as the base. The only
2069 we can avoid overlap is if we can deduce that they are nonoverlapping
2070 pieces of that decl, which is very rare. */
2071 basex
= MEM_P (rtlx
) ? XEXP (rtlx
, 0) : rtlx
;
2072 if (GET_CODE (basex
) == PLUS
&& GET_CODE (XEXP (basex
, 1)) == CONST_INT
)
2073 offsetx
= INTVAL (XEXP (basex
, 1)), basex
= XEXP (basex
, 0);
2075 basey
= MEM_P (rtly
) ? XEXP (rtly
, 0) : rtly
;
2076 if (GET_CODE (basey
) == PLUS
&& GET_CODE (XEXP (basey
, 1)) == CONST_INT
)
2077 offsety
= INTVAL (XEXP (basey
, 1)), basey
= XEXP (basey
, 0);
2079 /* If the bases are different, we know they do not overlap if both
2080 are constants or if one is a constant and the other a pointer into the
2081 stack frame. Otherwise a different base means we can't tell if they
2083 if (! rtx_equal_p (basex
, basey
))
2084 return ((CONSTANT_P (basex
) && CONSTANT_P (basey
))
2085 || (CONSTANT_P (basex
) && REG_P (basey
)
2086 && REGNO_PTR_FRAME_P (REGNO (basey
)))
2087 || (CONSTANT_P (basey
) && REG_P (basex
)
2088 && REGNO_PTR_FRAME_P (REGNO (basex
))));
2090 sizex
= (!MEM_P (rtlx
) ? (int) GET_MODE_SIZE (GET_MODE (rtlx
))
2091 : MEM_SIZE (rtlx
) ? INTVAL (MEM_SIZE (rtlx
))
2093 sizey
= (!MEM_P (rtly
) ? (int) GET_MODE_SIZE (GET_MODE (rtly
))
2094 : MEM_SIZE (rtly
) ? INTVAL (MEM_SIZE (rtly
)) :
2097 /* If we have an offset for either memref, it can update the values computed
2100 offsetx
+= INTVAL (moffsetx
), sizex
-= INTVAL (moffsetx
);
2102 offsety
+= INTVAL (moffsety
), sizey
-= INTVAL (moffsety
);
2104 /* If a memref has both a size and an offset, we can use the smaller size.
2105 We can't do this if the offset isn't known because we must view this
2106 memref as being anywhere inside the DECL's MEM. */
2107 if (MEM_SIZE (x
) && moffsetx
)
2108 sizex
= INTVAL (MEM_SIZE (x
));
2109 if (MEM_SIZE (y
) && moffsety
)
2110 sizey
= INTVAL (MEM_SIZE (y
));
2112 /* Put the values of the memref with the lower offset in X's values. */
2113 if (offsetx
> offsety
)
2115 tem
= offsetx
, offsetx
= offsety
, offsety
= tem
;
2116 tem
= sizex
, sizex
= sizey
, sizey
= tem
;
2119 /* If we don't know the size of the lower-offset value, we can't tell
2120 if they conflict. Otherwise, we do the test. */
2121 return sizex
>= 0 && offsety
>= offsetx
+ sizex
;
2124 /* True dependence: X is read after store in MEM takes place. */
2127 true_dependence (rtx mem
, enum machine_mode mem_mode
, rtx x
,
2128 int (*varies
) (rtx
, int))
2130 rtx x_addr
, mem_addr
;
2133 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2136 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2137 This is used in epilogue deallocation functions. */
2138 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2140 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2143 if (DIFFERENT_ALIAS_SETS_P (x
, mem
))
2146 /* Read-only memory is by definition never modified, and therefore can't
2147 conflict with anything. We don't expect to find read-only set on MEM,
2148 but stupid user tricks can produce them, so don't abort. */
2149 if (MEM_READONLY_P (x
))
2152 if (nonoverlapping_memrefs_p (mem
, x
))
2155 if (mem_mode
== VOIDmode
)
2156 mem_mode
= GET_MODE (mem
);
2158 x_addr
= get_addr (XEXP (x
, 0));
2159 mem_addr
= get_addr (XEXP (mem
, 0));
2161 base
= find_base_term (x_addr
);
2162 if (base
&& (GET_CODE (base
) == LABEL_REF
2163 || (GET_CODE (base
) == SYMBOL_REF
2164 && CONSTANT_POOL_ADDRESS_P (base
))))
2167 if (! base_alias_check (x_addr
, mem_addr
, GET_MODE (x
), mem_mode
))
2170 x_addr
= canon_rtx (x_addr
);
2171 mem_addr
= canon_rtx (mem_addr
);
2173 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode
), mem_addr
,
2174 SIZE_FOR_MODE (x
), x_addr
, 0))
2177 if (aliases_everything_p (x
))
2180 /* We cannot use aliases_everything_p to test MEM, since we must look
2181 at MEM_MODE, rather than GET_MODE (MEM). */
2182 if (mem_mode
== QImode
|| GET_CODE (mem_addr
) == AND
)
2185 /* In true_dependence we also allow BLKmode to alias anything. Why
2186 don't we do this in anti_dependence and output_dependence? */
2187 if (mem_mode
== BLKmode
|| GET_MODE (x
) == BLKmode
)
2190 return ! fixed_scalar_and_varying_struct_p (mem
, x
, mem_addr
, x_addr
,
2194 /* Canonical true dependence: X is read after store in MEM takes place.
2195 Variant of true_dependence which assumes MEM has already been
2196 canonicalized (hence we no longer do that here).
2197 The mem_addr argument has been added, since true_dependence computed
2198 this value prior to canonicalizing. */
2201 canon_true_dependence (rtx mem
, enum machine_mode mem_mode
, rtx mem_addr
,
2202 rtx x
, int (*varies
) (rtx
, int))
2206 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2209 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2210 This is used in epilogue deallocation functions. */
2211 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2213 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2216 if (DIFFERENT_ALIAS_SETS_P (x
, mem
))
2219 /* Read-only memory is by definition never modified, and therefore can't
2220 conflict with anything. We don't expect to find read-only set on MEM,
2221 but stupid user tricks can produce them, so don't abort. */
2222 if (MEM_READONLY_P (x
))
2225 if (nonoverlapping_memrefs_p (x
, mem
))
2228 x_addr
= get_addr (XEXP (x
, 0));
2230 if (! base_alias_check (x_addr
, mem_addr
, GET_MODE (x
), mem_mode
))
2233 x_addr
= canon_rtx (x_addr
);
2234 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode
), mem_addr
,
2235 SIZE_FOR_MODE (x
), x_addr
, 0))
2238 if (aliases_everything_p (x
))
2241 /* We cannot use aliases_everything_p to test MEM, since we must look
2242 at MEM_MODE, rather than GET_MODE (MEM). */
2243 if (mem_mode
== QImode
|| GET_CODE (mem_addr
) == AND
)
2246 /* In true_dependence we also allow BLKmode to alias anything. Why
2247 don't we do this in anti_dependence and output_dependence? */
2248 if (mem_mode
== BLKmode
|| GET_MODE (x
) == BLKmode
)
2251 return ! fixed_scalar_and_varying_struct_p (mem
, x
, mem_addr
, x_addr
,
2255 /* Returns nonzero if a write to X might alias a previous read from
2256 (or, if WRITEP is nonzero, a write to) MEM. */
2259 write_dependence_p (rtx mem
, rtx x
, int writep
)
2261 rtx x_addr
, mem_addr
;
2265 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2268 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2269 This is used in epilogue deallocation functions. */
2270 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2272 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2275 if (DIFFERENT_ALIAS_SETS_P (x
, mem
))
2278 /* A read from read-only memory can't conflict with read-write memory. */
2279 if (!writep
&& MEM_READONLY_P (mem
))
2282 if (nonoverlapping_memrefs_p (x
, mem
))
2285 x_addr
= get_addr (XEXP (x
, 0));
2286 mem_addr
= get_addr (XEXP (mem
, 0));
2290 base
= find_base_term (mem_addr
);
2291 if (base
&& (GET_CODE (base
) == LABEL_REF
2292 || (GET_CODE (base
) == SYMBOL_REF
2293 && CONSTANT_POOL_ADDRESS_P (base
))))
2297 if (! base_alias_check (x_addr
, mem_addr
, GET_MODE (x
),
2301 x_addr
= canon_rtx (x_addr
);
2302 mem_addr
= canon_rtx (mem_addr
);
2304 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem
), mem_addr
,
2305 SIZE_FOR_MODE (x
), x_addr
, 0))
2309 = fixed_scalar_and_varying_struct_p (mem
, x
, mem_addr
, x_addr
,
2312 return (!(fixed_scalar
== mem
&& !aliases_everything_p (x
))
2313 && !(fixed_scalar
== x
&& !aliases_everything_p (mem
)));
2316 /* Anti dependence: X is written after read in MEM takes place. */
2319 anti_dependence (rtx mem
, rtx x
)
2321 return write_dependence_p (mem
, x
, /*writep=*/0);
2324 /* Output dependence: X is written after store in MEM takes place. */
2327 output_dependence (rtx mem
, rtx x
)
2329 return write_dependence_p (mem
, x
, /*writep=*/1);
2332 /* A subroutine of nonlocal_mentioned_p, returns 1 if *LOC mentions
2333 something which is not local to the function and is not constant. */
2336 nonlocal_mentioned_p_1 (rtx
*loc
, void *data ATTRIBUTE_UNUSED
)
2345 switch (GET_CODE (x
))
2348 if (REG_P (SUBREG_REG (x
)))
2350 /* Global registers are not local. */
2351 if (REGNO (SUBREG_REG (x
)) < FIRST_PSEUDO_REGISTER
2352 && global_regs
[subreg_regno (x
)])
2360 /* Global registers are not local. */
2361 if (regno
< FIRST_PSEUDO_REGISTER
&& global_regs
[regno
])
2376 /* Constants in the function's constants pool are constant. */
2377 if (CONSTANT_POOL_ADDRESS_P (x
))
2382 /* Non-constant calls and recursion are not local. */
2386 /* Be overly conservative and consider any volatile memory
2387 reference as not local. */
2388 if (MEM_VOLATILE_P (x
))
2390 base
= find_base_term (XEXP (x
, 0));
2393 /* A Pmode ADDRESS could be a reference via the structure value
2394 address or static chain. Such memory references are nonlocal.
2396 Thus, we have to examine the contents of the ADDRESS to find
2397 out if this is a local reference or not. */
2398 if (GET_CODE (base
) == ADDRESS
2399 && GET_MODE (base
) == Pmode
2400 && (XEXP (base
, 0) == stack_pointer_rtx
2401 || XEXP (base
, 0) == arg_pointer_rtx
2402 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2403 || XEXP (base
, 0) == hard_frame_pointer_rtx
2405 || XEXP (base
, 0) == frame_pointer_rtx
))
2407 /* Constants in the function's constant pool are constant. */
2408 if (GET_CODE (base
) == SYMBOL_REF
&& CONSTANT_POOL_ADDRESS_P (base
))
2413 case UNSPEC_VOLATILE
:
2418 if (MEM_VOLATILE_P (x
))
2430 /* Returns nonzero if X might mention something which is not
2431 local to the function and is not constant. */
2434 nonlocal_mentioned_p (rtx x
)
2440 if (! CONST_OR_PURE_CALL_P (x
))
2442 x
= CALL_INSN_FUNCTION_USAGE (x
);
2450 return for_each_rtx (&x
, nonlocal_mentioned_p_1
, NULL
);
2453 /* A subroutine of nonlocal_referenced_p, returns 1 if *LOC references
2454 something which is not local to the function and is not constant. */
2457 nonlocal_referenced_p_1 (rtx
*loc
, void *data ATTRIBUTE_UNUSED
)
2464 switch (GET_CODE (x
))
2470 return nonlocal_mentioned_p (x
);
2473 /* Non-constant calls and recursion are not local. */
2477 if (nonlocal_mentioned_p (SET_SRC (x
)))
2480 if (MEM_P (SET_DEST (x
)))
2481 return nonlocal_mentioned_p (XEXP (SET_DEST (x
), 0));
2483 /* If the destination is anything other than a CC0, PC,
2484 MEM, REG, or a SUBREG of a REG that occupies all of
2485 the REG, then X references nonlocal memory if it is
2486 mentioned in the destination. */
2487 if (GET_CODE (SET_DEST (x
)) != CC0
2488 && GET_CODE (SET_DEST (x
)) != PC
2489 && !REG_P (SET_DEST (x
))
2490 && ! (GET_CODE (SET_DEST (x
)) == SUBREG
2491 && REG_P (SUBREG_REG (SET_DEST (x
)))
2492 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x
))))
2493 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
)
2494 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x
)))
2495 + (UNITS_PER_WORD
- 1)) / UNITS_PER_WORD
))))
2496 return nonlocal_mentioned_p (SET_DEST (x
));
2500 if (MEM_P (XEXP (x
, 0)))
2501 return nonlocal_mentioned_p (XEXP (XEXP (x
, 0), 0));
2505 return nonlocal_mentioned_p (XEXP (x
, 0));
2508 case UNSPEC_VOLATILE
:
2512 if (MEM_VOLATILE_P (x
))
2524 /* Returns nonzero if X might reference something which is not
2525 local to the function and is not constant. */
2528 nonlocal_referenced_p (rtx x
)
2534 if (! CONST_OR_PURE_CALL_P (x
))
2536 x
= CALL_INSN_FUNCTION_USAGE (x
);
2544 return for_each_rtx (&x
, nonlocal_referenced_p_1
, NULL
);
2547 /* A subroutine of nonlocal_set_p, returns 1 if *LOC sets
2548 something which is not local to the function and is not constant. */
2551 nonlocal_set_p_1 (rtx
*loc
, void *data ATTRIBUTE_UNUSED
)
2558 switch (GET_CODE (x
))
2561 /* Non-constant calls and recursion are not local. */
2570 return nonlocal_mentioned_p (XEXP (x
, 0));
2573 if (nonlocal_mentioned_p (SET_DEST (x
)))
2575 return nonlocal_set_p (SET_SRC (x
));
2578 return nonlocal_mentioned_p (XEXP (x
, 0));
2584 case UNSPEC_VOLATILE
:
2588 if (MEM_VOLATILE_P (x
))
2600 /* Returns nonzero if X might set something which is not
2601 local to the function and is not constant. */
2604 nonlocal_set_p (rtx x
)
2610 if (! CONST_OR_PURE_CALL_P (x
))
2612 x
= CALL_INSN_FUNCTION_USAGE (x
);
2620 return for_each_rtx (&x
, nonlocal_set_p_1
, NULL
);
2623 /* Mark the function if it is pure or constant. */
2626 mark_constant_function (void)
2629 int nonlocal_memory_referenced
;
2631 if (TREE_READONLY (current_function_decl
)
2632 || DECL_IS_PURE (current_function_decl
)
2633 || TREE_THIS_VOLATILE (current_function_decl
)
2634 || current_function_has_nonlocal_goto
2635 || !targetm
.binds_local_p (current_function_decl
))
2638 /* A loop might not return which counts as a side effect. */
2639 if (mark_dfs_back_edges ())
2642 nonlocal_memory_referenced
= 0;
2644 init_alias_analysis ();
2646 /* Determine if this is a constant or pure function. */
2648 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2650 if (! INSN_P (insn
))
2653 if (nonlocal_set_p (insn
) || global_reg_mentioned_p (insn
)
2654 || volatile_refs_p (PATTERN (insn
)))
2657 if (! nonlocal_memory_referenced
)
2658 nonlocal_memory_referenced
= nonlocal_referenced_p (insn
);
2661 end_alias_analysis ();
2663 /* Mark the function. */
2667 else if (nonlocal_memory_referenced
)
2669 cgraph_rtl_info (current_function_decl
)->pure_function
= 1;
2670 DECL_IS_PURE (current_function_decl
) = 1;
2674 cgraph_rtl_info (current_function_decl
)->const_function
= 1;
2675 TREE_READONLY (current_function_decl
) = 1;
2681 init_alias_once (void)
2685 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2686 /* Check whether this register can hold an incoming pointer
2687 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2688 numbers, so translate if necessary due to register windows. */
2689 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i
))
2690 && HARD_REGNO_MODE_OK (i
, Pmode
))
2691 static_reg_base_value
[i
]
2692 = gen_rtx_ADDRESS (VOIDmode
, gen_rtx_REG (Pmode
, i
));
2694 static_reg_base_value
[STACK_POINTER_REGNUM
]
2695 = gen_rtx_ADDRESS (Pmode
, stack_pointer_rtx
);
2696 static_reg_base_value
[ARG_POINTER_REGNUM
]
2697 = gen_rtx_ADDRESS (Pmode
, arg_pointer_rtx
);
2698 static_reg_base_value
[FRAME_POINTER_REGNUM
]
2699 = gen_rtx_ADDRESS (Pmode
, frame_pointer_rtx
);
2700 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2701 static_reg_base_value
[HARD_FRAME_POINTER_REGNUM
]
2702 = gen_rtx_ADDRESS (Pmode
, hard_frame_pointer_rtx
);
2706 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2707 to be memory reference. */
2708 static bool memory_modified
;
2710 memory_modified_1 (rtx x
, rtx pat ATTRIBUTE_UNUSED
, void *data
)
2714 if (anti_dependence (x
, (rtx
)data
) || output_dependence (x
, (rtx
)data
))
2715 memory_modified
= true;
2720 /* Return true when INSN possibly modify memory contents of MEM
2721 (ie address can be modified). */
2723 memory_modified_in_insn_p (rtx mem
, rtx insn
)
2727 memory_modified
= false;
2728 note_stores (PATTERN (insn
), memory_modified_1
, mem
);
2729 return memory_modified
;
2732 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2736 init_alias_analysis (void)
2738 unsigned int maxreg
= max_reg_num ();
2744 timevar_push (TV_ALIAS_ANALYSIS
);
2746 reg_known_value_size
= maxreg
- FIRST_PSEUDO_REGISTER
;
2747 reg_known_value
= ggc_calloc (reg_known_value_size
, sizeof (rtx
));
2748 reg_known_equiv_p
= xcalloc (reg_known_value_size
, sizeof (bool));
2750 /* Overallocate reg_base_value to allow some growth during loop
2751 optimization. Loop unrolling can create a large number of
2753 if (old_reg_base_value
)
2755 reg_base_value
= old_reg_base_value
;
2756 /* If varray gets large zeroing cost may get important. */
2757 if (VARRAY_SIZE (reg_base_value
) > 256
2758 && VARRAY_SIZE (reg_base_value
) > 4 * maxreg
)
2759 VARRAY_GROW (reg_base_value
, maxreg
);
2760 VARRAY_CLEAR (reg_base_value
);
2761 if (VARRAY_SIZE (reg_base_value
) < maxreg
)
2762 VARRAY_GROW (reg_base_value
, maxreg
);
2766 VARRAY_RTX_INIT (reg_base_value
, maxreg
, "reg_base_value");
2769 new_reg_base_value
= xmalloc (maxreg
* sizeof (rtx
));
2770 reg_seen
= xmalloc (maxreg
);
2771 if (! reload_completed
&& flag_old_unroll_loops
)
2773 alias_invariant
= ggc_calloc (maxreg
, sizeof (rtx
));
2774 alias_invariant_size
= maxreg
;
2777 /* The basic idea is that each pass through this loop will use the
2778 "constant" information from the previous pass to propagate alias
2779 information through another level of assignments.
2781 This could get expensive if the assignment chains are long. Maybe
2782 we should throttle the number of iterations, possibly based on
2783 the optimization level or flag_expensive_optimizations.
2785 We could propagate more information in the first pass by making use
2786 of REG_N_SETS to determine immediately that the alias information
2787 for a pseudo is "constant".
2789 A program with an uninitialized variable can cause an infinite loop
2790 here. Instead of doing a full dataflow analysis to detect such problems
2791 we just cap the number of iterations for the loop.
2793 The state of the arrays for the set chain in question does not matter
2794 since the program has undefined behavior. */
2799 /* Assume nothing will change this iteration of the loop. */
2802 /* We want to assign the same IDs each iteration of this loop, so
2803 start counting from zero each iteration of the loop. */
2806 /* We're at the start of the function each iteration through the
2807 loop, so we're copying arguments. */
2808 copying_arguments
= true;
2810 /* Wipe the potential alias information clean for this pass. */
2811 memset (new_reg_base_value
, 0, maxreg
* sizeof (rtx
));
2813 /* Wipe the reg_seen array clean. */
2814 memset (reg_seen
, 0, maxreg
);
2816 /* Mark all hard registers which may contain an address.
2817 The stack, frame and argument pointers may contain an address.
2818 An argument register which can hold a Pmode value may contain
2819 an address even if it is not in BASE_REGS.
2821 The address expression is VOIDmode for an argument and
2822 Pmode for other registers. */
2824 memcpy (new_reg_base_value
, static_reg_base_value
,
2825 FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
2827 /* Walk the insns adding values to the new_reg_base_value array. */
2828 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2834 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2835 /* The prologue/epilogue insns are not threaded onto the
2836 insn chain until after reload has completed. Thus,
2837 there is no sense wasting time checking if INSN is in
2838 the prologue/epilogue until after reload has completed. */
2839 if (reload_completed
2840 && prologue_epilogue_contains (insn
))
2844 /* If this insn has a noalias note, process it, Otherwise,
2845 scan for sets. A simple set will have no side effects
2846 which could change the base value of any other register. */
2848 if (GET_CODE (PATTERN (insn
)) == SET
2849 && REG_NOTES (insn
) != 0
2850 && find_reg_note (insn
, REG_NOALIAS
, NULL_RTX
))
2851 record_set (SET_DEST (PATTERN (insn
)), NULL_RTX
, NULL
);
2853 note_stores (PATTERN (insn
), record_set
, NULL
);
2855 set
= single_set (insn
);
2858 && REG_P (SET_DEST (set
))
2859 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
2861 unsigned int regno
= REGNO (SET_DEST (set
));
2862 rtx src
= SET_SRC (set
);
2865 if (REG_NOTES (insn
) != 0
2866 && (((note
= find_reg_note (insn
, REG_EQUAL
, 0)) != 0
2867 && REG_N_SETS (regno
) == 1)
2868 || (note
= find_reg_note (insn
, REG_EQUIV
, NULL_RTX
)) != 0)
2869 && GET_CODE (XEXP (note
, 0)) != EXPR_LIST
2870 && ! rtx_varies_p (XEXP (note
, 0), 1)
2871 && ! reg_overlap_mentioned_p (SET_DEST (set
),
2874 set_reg_known_value (regno
, XEXP (note
, 0));
2875 set_reg_known_equiv_p (regno
,
2876 REG_NOTE_KIND (note
) == REG_EQUIV
);
2878 else if (REG_N_SETS (regno
) == 1
2879 && GET_CODE (src
) == PLUS
2880 && REG_P (XEXP (src
, 0))
2881 && (t
= get_reg_known_value (REGNO (XEXP (src
, 0))))
2882 && GET_CODE (XEXP (src
, 1)) == CONST_INT
)
2884 t
= plus_constant (t
, INTVAL (XEXP (src
, 1)));
2885 set_reg_known_value (regno
, t
);
2886 set_reg_known_equiv_p (regno
, 0);
2888 else if (REG_N_SETS (regno
) == 1
2889 && ! rtx_varies_p (src
, 1))
2891 set_reg_known_value (regno
, src
);
2892 set_reg_known_equiv_p (regno
, 0);
2896 else if (NOTE_P (insn
)
2897 && NOTE_LINE_NUMBER (insn
) == NOTE_INSN_FUNCTION_BEG
)
2898 copying_arguments
= false;
2901 /* Now propagate values from new_reg_base_value to reg_base_value. */
2902 if (maxreg
!= (unsigned int) max_reg_num())
2904 for (ui
= 0; ui
< maxreg
; ui
++)
2906 if (new_reg_base_value
[ui
]
2907 && new_reg_base_value
[ui
] != VARRAY_RTX (reg_base_value
, ui
)
2908 && ! rtx_equal_p (new_reg_base_value
[ui
],
2909 VARRAY_RTX (reg_base_value
, ui
)))
2911 VARRAY_RTX (reg_base_value
, ui
) = new_reg_base_value
[ui
];
2916 while (changed
&& ++pass
< MAX_ALIAS_LOOP_PASSES
);
2918 /* Fill in the remaining entries. */
2919 for (i
= 0; i
< (int)reg_known_value_size
; i
++)
2920 if (reg_known_value
[i
] == 0)
2921 reg_known_value
[i
] = regno_reg_rtx
[i
+ FIRST_PSEUDO_REGISTER
];
2923 /* Simplify the reg_base_value array so that no register refers to
2924 another register, except to special registers indirectly through
2925 ADDRESS expressions.
2927 In theory this loop can take as long as O(registers^2), but unless
2928 there are very long dependency chains it will run in close to linear
2931 This loop may not be needed any longer now that the main loop does
2932 a better job at propagating alias information. */
2938 for (ui
= 0; ui
< maxreg
; ui
++)
2940 rtx base
= VARRAY_RTX (reg_base_value
, ui
);
2941 if (base
&& REG_P (base
))
2943 unsigned int base_regno
= REGNO (base
);
2944 if (base_regno
== ui
) /* register set from itself */
2945 VARRAY_RTX (reg_base_value
, ui
) = 0;
2947 VARRAY_RTX (reg_base_value
, ui
)
2948 = VARRAY_RTX (reg_base_value
, base_regno
);
2953 while (changed
&& pass
< MAX_ALIAS_LOOP_PASSES
);
2956 free (new_reg_base_value
);
2957 new_reg_base_value
= 0;
2960 timevar_pop (TV_ALIAS_ANALYSIS
);
2964 end_alias_analysis (void)
2966 old_reg_base_value
= reg_base_value
;
2967 ggc_free (reg_known_value
);
2968 reg_known_value
= 0;
2969 reg_known_value_size
= 0;
2970 free (reg_known_equiv_p
);
2971 reg_known_equiv_p
= 0;
2972 if (alias_invariant
)
2974 ggc_free (alias_invariant
);
2975 alias_invariant
= 0;
2976 alias_invariant_size
= 0;
2980 #include "gt-alias.h"