1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2009, 2010 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
37 #include "diagnostic-core.h"
39 #include "splay-tree.h"
41 #include "langhooks.h"
45 #include "tree-pass.h"
46 #include "ipa-type-escape.h"
48 #include "tree-ssa-alias.h"
49 #include "pointer-set.h"
50 #include "tree-flow.h"
52 /* The aliasing API provided here solves related but different problems:
54 Say there exists (in c)
68 Consider the four questions:
70 Can a store to x1 interfere with px2->y1?
71 Can a store to x1 interfere with px2->z2?
73 Can a store to x1 change the value pointed to by with py?
74 Can a store to x1 change the value pointed to by with pz?
76 The answer to these questions can be yes, yes, yes, and maybe.
78 The first two questions can be answered with a simple examination
79 of the type system. If structure X contains a field of type Y then
80 a store thru a pointer to an X can overwrite any field that is
81 contained (recursively) in an X (unless we know that px1 != px2).
83 The last two of the questions can be solved in the same way as the
84 first two questions but this is too conservative. The observation
85 is that in some cases analysis we can know if which (if any) fields
86 are addressed and if those addresses are used in bad ways. This
87 analysis may be language specific. In C, arbitrary operations may
88 be applied to pointers. However, there is some indication that
89 this may be too conservative for some C++ types.
91 The pass ipa-type-escape does this analysis for the types whose
92 instances do not escape across the compilation boundary.
94 Historically in GCC, these two problems were combined and a single
95 data structure was used to represent the solution to these
96 problems. We now have two similar but different data structures,
97 The data structure to solve the last two question is similar to the
98 first, but does not contain have the fields in it whose address are
99 never taken. For types that do escape the compilation unit, the
100 data structures will have identical information.
103 /* The alias sets assigned to MEMs assist the back-end in determining
104 which MEMs can alias which other MEMs. In general, two MEMs in
105 different alias sets cannot alias each other, with one important
106 exception. Consider something like:
108 struct S { int i; double d; };
110 a store to an `S' can alias something of either type `int' or type
111 `double'. (However, a store to an `int' cannot alias a `double'
112 and vice versa.) We indicate this via a tree structure that looks
120 (The arrows are directed and point downwards.)
121 In this situation we say the alias set for `struct S' is the
122 `superset' and that those for `int' and `double' are `subsets'.
124 To see whether two alias sets can point to the same memory, we must
125 see if either alias set is a subset of the other. We need not trace
126 past immediate descendants, however, since we propagate all
127 grandchildren up one level.
129 Alias set zero is implicitly a superset of all other alias sets.
130 However, this is no actual entry for alias set zero. It is an
131 error to attempt to explicitly construct a subset of zero. */
133 struct GTY(()) alias_set_entry_d
{
134 /* The alias set number, as stored in MEM_ALIAS_SET. */
135 alias_set_type alias_set
;
137 /* Nonzero if would have a child of zero: this effectively makes this
138 alias set the same as alias set zero. */
141 /* The children of the alias set. These are not just the immediate
142 children, but, in fact, all descendants. So, if we have:
144 struct T { struct S s; float f; }
146 continuing our example above, the children here will be all of
147 `int', `double', `float', and `struct S'. */
148 splay_tree
GTY((param1_is (int), param2_is (int))) children
;
150 typedef struct alias_set_entry_d
*alias_set_entry
;
152 static int rtx_equal_for_memref_p (const_rtx
, const_rtx
);
153 static int memrefs_conflict_p (int, rtx
, int, rtx
, HOST_WIDE_INT
);
154 static void record_set (rtx
, const_rtx
, void *);
155 static int base_alias_check (rtx
, rtx
, enum machine_mode
,
157 static rtx
find_base_value (rtx
);
158 static int mems_in_disjoint_alias_sets_p (const_rtx
, const_rtx
);
159 static int insert_subset_children (splay_tree_node
, void*);
160 static alias_set_entry
get_alias_set_entry (alias_set_type
);
161 static const_rtx
fixed_scalar_and_varying_struct_p (const_rtx
, const_rtx
, rtx
, rtx
,
162 bool (*) (const_rtx
, bool));
163 static int aliases_everything_p (const_rtx
);
164 static bool nonoverlapping_component_refs_p (const_tree
, const_tree
);
165 static tree
decl_for_component_ref (tree
);
166 static rtx
adjust_offset_for_component_ref (tree
, rtx
);
167 static int write_dependence_p (const_rtx
, const_rtx
, int);
169 static void memory_modified_1 (rtx
, const_rtx
, void *);
171 /* Set up all info needed to perform alias analysis on memory references. */
173 /* Returns the size in bytes of the mode of X. */
174 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
176 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
177 different alias sets. We ignore alias sets in functions making use
178 of variable arguments because the va_arg macros on some systems are
180 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
181 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
183 /* Cap the number of passes we make over the insns propagating alias
184 information through set chains. 10 is a completely arbitrary choice. */
185 #define MAX_ALIAS_LOOP_PASSES 10
187 /* reg_base_value[N] gives an address to which register N is related.
188 If all sets after the first add or subtract to the current value
189 or otherwise modify it so it does not point to a different top level
190 object, reg_base_value[N] is equal to the address part of the source
193 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
194 expressions represent certain special values: function arguments and
195 the stack, frame, and argument pointers.
197 The contents of an ADDRESS is not normally used, the mode of the
198 ADDRESS determines whether the ADDRESS is a function argument or some
199 other special value. Pointer equality, not rtx_equal_p, determines whether
200 two ADDRESS expressions refer to the same base address.
202 The only use of the contents of an ADDRESS is for determining if the
203 current function performs nonlocal memory memory references for the
204 purposes of marking the function as a constant function. */
206 static GTY(()) VEC(rtx
,gc
) *reg_base_value
;
207 static rtx
*new_reg_base_value
;
209 /* We preserve the copy of old array around to avoid amount of garbage
210 produced. About 8% of garbage produced were attributed to this
212 static GTY((deletable
)) VEC(rtx
,gc
) *old_reg_base_value
;
214 #define static_reg_base_value \
215 (this_target_rtl->x_static_reg_base_value)
217 #define REG_BASE_VALUE(X) \
218 (REGNO (X) < VEC_length (rtx, reg_base_value) \
219 ? VEC_index (rtx, reg_base_value, REGNO (X)) : 0)
221 /* Vector indexed by N giving the initial (unchanging) value known for
222 pseudo-register N. This array is initialized in init_alias_analysis,
223 and does not change until end_alias_analysis is called. */
224 static GTY((length("reg_known_value_size"))) rtx
*reg_known_value
;
226 /* Indicates number of valid entries in reg_known_value. */
227 static GTY(()) unsigned int reg_known_value_size
;
229 /* Vector recording for each reg_known_value whether it is due to a
230 REG_EQUIV note. Future passes (viz., reload) may replace the
231 pseudo with the equivalent expression and so we account for the
232 dependences that would be introduced if that happens.
234 The REG_EQUIV notes created in assign_parms may mention the arg
235 pointer, and there are explicit insns in the RTL that modify the
236 arg pointer. Thus we must ensure that such insns don't get
237 scheduled across each other because that would invalidate the
238 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
239 wrong, but solving the problem in the scheduler will likely give
240 better code, so we do it here. */
241 static bool *reg_known_equiv_p
;
243 /* True when scanning insns from the start of the rtl to the
244 NOTE_INSN_FUNCTION_BEG note. */
245 static bool copying_arguments
;
247 DEF_VEC_P(alias_set_entry
);
248 DEF_VEC_ALLOC_P(alias_set_entry
,gc
);
250 /* The splay-tree used to store the various alias set entries. */
251 static GTY (()) VEC(alias_set_entry
,gc
) *alias_sets
;
253 /* Build a decomposed reference object for querying the alias-oracle
254 from the MEM rtx and store it in *REF.
255 Returns false if MEM is not suitable for the alias-oracle. */
258 ao_ref_from_mem (ao_ref
*ref
, const_rtx mem
)
260 tree expr
= MEM_EXPR (mem
);
266 ao_ref_init (ref
, expr
);
268 /* Get the base of the reference and see if we have to reject or
270 base
= ao_ref_base (ref
);
271 if (base
== NULL_TREE
)
274 /* The tree oracle doesn't like to have these. */
275 if (TREE_CODE (base
) == FUNCTION_DECL
276 || TREE_CODE (base
) == LABEL_DECL
)
279 /* If this is a pointer dereference of a non-SSA_NAME punt.
280 ??? We could replace it with a pointer to anything. */
281 if ((INDIRECT_REF_P (base
)
282 || TREE_CODE (base
) == MEM_REF
)
283 && TREE_CODE (TREE_OPERAND (base
, 0)) != SSA_NAME
)
285 if (TREE_CODE (base
) == TARGET_MEM_REF
287 && TREE_CODE (TMR_BASE (base
)) != SSA_NAME
)
290 /* If this is a reference based on a partitioned decl replace the
291 base with an INDIRECT_REF of the pointer representative we
292 created during stack slot partitioning. */
293 if (TREE_CODE (base
) == VAR_DECL
294 && ! TREE_STATIC (base
)
295 && cfun
->gimple_df
->decls_to_pointers
!= NULL
)
298 namep
= pointer_map_contains (cfun
->gimple_df
->decls_to_pointers
, base
);
300 ref
->base
= build_simple_mem_ref (*(tree
*)namep
);
302 else if (TREE_CODE (base
) == TARGET_MEM_REF
303 && TREE_CODE (TMR_BASE (base
)) == ADDR_EXPR
304 && TREE_CODE (TREE_OPERAND (TMR_BASE (base
), 0)) == VAR_DECL
305 && ! TREE_STATIC (TREE_OPERAND (TMR_BASE (base
), 0))
306 && cfun
->gimple_df
->decls_to_pointers
!= NULL
)
309 namep
= pointer_map_contains (cfun
->gimple_df
->decls_to_pointers
,
310 TREE_OPERAND (TMR_BASE (base
), 0));
312 ref
->base
= build_simple_mem_ref (*(tree
*)namep
);
315 ref
->ref_alias_set
= MEM_ALIAS_SET (mem
);
317 /* If MEM_OFFSET or MEM_SIZE are NULL we have to punt.
318 Keep points-to related information though. */
319 if (!MEM_OFFSET (mem
)
322 ref
->ref
= NULL_TREE
;
329 /* If the base decl is a parameter we can have negative MEM_OFFSET in
330 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
332 if (INTVAL (MEM_OFFSET (mem
)) < 0
333 && ((INTVAL (MEM_SIZE (mem
)) + INTVAL (MEM_OFFSET (mem
)))
334 * BITS_PER_UNIT
) == ref
->size
)
337 ref
->offset
+= INTVAL (MEM_OFFSET (mem
)) * BITS_PER_UNIT
;
338 ref
->size
= INTVAL (MEM_SIZE (mem
)) * BITS_PER_UNIT
;
340 /* The MEM may extend into adjacent fields, so adjust max_size if
342 if (ref
->max_size
!= -1
343 && ref
->size
> ref
->max_size
)
344 ref
->max_size
= ref
->size
;
346 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
347 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
348 if (MEM_EXPR (mem
) != get_spill_slot_decl (false)
350 || (DECL_P (ref
->base
)
351 && (!host_integerp (DECL_SIZE (ref
->base
), 1)
352 || (TREE_INT_CST_LOW (DECL_SIZE ((ref
->base
)))
353 < (unsigned HOST_WIDE_INT
)(ref
->offset
+ ref
->size
))))))
359 /* Query the alias-oracle on whether the two memory rtx X and MEM may
360 alias. If TBAA_P is set also apply TBAA. Returns true if the
361 two rtxen may alias, false otherwise. */
364 rtx_refs_may_alias_p (const_rtx x
, const_rtx mem
, bool tbaa_p
)
368 if (!ao_ref_from_mem (&ref1
, x
)
369 || !ao_ref_from_mem (&ref2
, mem
))
372 return refs_may_alias_p_1 (&ref1
, &ref2
,
374 && MEM_ALIAS_SET (x
) != 0
375 && MEM_ALIAS_SET (mem
) != 0);
378 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
379 such an entry, or NULL otherwise. */
381 static inline alias_set_entry
382 get_alias_set_entry (alias_set_type alias_set
)
384 return VEC_index (alias_set_entry
, alias_sets
, alias_set
);
387 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
388 the two MEMs cannot alias each other. */
391 mems_in_disjoint_alias_sets_p (const_rtx mem1
, const_rtx mem2
)
393 /* Perform a basic sanity check. Namely, that there are no alias sets
394 if we're not using strict aliasing. This helps to catch bugs
395 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
396 where a MEM is allocated in some way other than by the use of
397 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
398 use alias sets to indicate that spilled registers cannot alias each
399 other, we might need to remove this check. */
400 gcc_assert (flag_strict_aliasing
401 || (!MEM_ALIAS_SET (mem1
) && !MEM_ALIAS_SET (mem2
)));
403 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1
), MEM_ALIAS_SET (mem2
));
406 /* Insert the NODE into the splay tree given by DATA. Used by
407 record_alias_subset via splay_tree_foreach. */
410 insert_subset_children (splay_tree_node node
, void *data
)
412 splay_tree_insert ((splay_tree
) data
, node
->key
, node
->value
);
417 /* Return true if the first alias set is a subset of the second. */
420 alias_set_subset_of (alias_set_type set1
, alias_set_type set2
)
424 /* Everything is a subset of the "aliases everything" set. */
428 /* Otherwise, check if set1 is a subset of set2. */
429 ase
= get_alias_set_entry (set2
);
431 && (ase
->has_zero_child
432 || splay_tree_lookup (ase
->children
,
433 (splay_tree_key
) set1
)))
438 /* Return 1 if the two specified alias sets may conflict. */
441 alias_sets_conflict_p (alias_set_type set1
, alias_set_type set2
)
446 if (alias_sets_must_conflict_p (set1
, set2
))
449 /* See if the first alias set is a subset of the second. */
450 ase
= get_alias_set_entry (set1
);
452 && (ase
->has_zero_child
453 || splay_tree_lookup (ase
->children
,
454 (splay_tree_key
) set2
)))
457 /* Now do the same, but with the alias sets reversed. */
458 ase
= get_alias_set_entry (set2
);
460 && (ase
->has_zero_child
461 || splay_tree_lookup (ase
->children
,
462 (splay_tree_key
) set1
)))
465 /* The two alias sets are distinct and neither one is the
466 child of the other. Therefore, they cannot conflict. */
470 /* Return 1 if the two specified alias sets will always conflict. */
473 alias_sets_must_conflict_p (alias_set_type set1
, alias_set_type set2
)
475 if (set1
== 0 || set2
== 0 || set1
== set2
)
481 /* Return 1 if any MEM object of type T1 will always conflict (using the
482 dependency routines in this file) with any MEM object of type T2.
483 This is used when allocating temporary storage. If T1 and/or T2 are
484 NULL_TREE, it means we know nothing about the storage. */
487 objects_must_conflict_p (tree t1
, tree t2
)
489 alias_set_type set1
, set2
;
491 /* If neither has a type specified, we don't know if they'll conflict
492 because we may be using them to store objects of various types, for
493 example the argument and local variables areas of inlined functions. */
494 if (t1
== 0 && t2
== 0)
497 /* If they are the same type, they must conflict. */
499 /* Likewise if both are volatile. */
500 || (t1
!= 0 && TYPE_VOLATILE (t1
) && t2
!= 0 && TYPE_VOLATILE (t2
)))
503 set1
= t1
? get_alias_set (t1
) : 0;
504 set2
= t2
? get_alias_set (t2
) : 0;
506 /* We can't use alias_sets_conflict_p because we must make sure
507 that every subtype of t1 will conflict with every subtype of
508 t2 for which a pair of subobjects of these respective subtypes
509 overlaps on the stack. */
510 return alias_sets_must_conflict_p (set1
, set2
);
513 /* Return true if all nested component references handled by
514 get_inner_reference in T are such that we should use the alias set
515 provided by the object at the heart of T.
517 This is true for non-addressable components (which don't have their
518 own alias set), as well as components of objects in alias set zero.
519 This later point is a special case wherein we wish to override the
520 alias set used by the component, but we don't have per-FIELD_DECL
521 assignable alias sets. */
524 component_uses_parent_alias_set (const_tree t
)
528 /* If we're at the end, it vacuously uses its own alias set. */
529 if (!handled_component_p (t
))
532 switch (TREE_CODE (t
))
535 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t
, 1)))
540 case ARRAY_RANGE_REF
:
541 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t
, 0))))
550 /* Bitfields and casts are never addressable. */
554 t
= TREE_OPERAND (t
, 0);
555 if (get_alias_set (TREE_TYPE (t
)) == 0)
560 /* Return the alias set for the memory pointed to by T, which may be
561 either a type or an expression. Return -1 if there is nothing
562 special about dereferencing T. */
564 static alias_set_type
565 get_deref_alias_set_1 (tree t
)
567 /* If we're not doing any alias analysis, just assume everything
568 aliases everything else. */
569 if (!flag_strict_aliasing
)
572 /* All we care about is the type. */
576 /* If we have an INDIRECT_REF via a void pointer, we don't
577 know anything about what that might alias. Likewise if the
578 pointer is marked that way. */
579 if (TREE_CODE (TREE_TYPE (t
)) == VOID_TYPE
580 || TYPE_REF_CAN_ALIAS_ALL (t
))
586 /* Return the alias set for the memory pointed to by T, which may be
587 either a type or an expression. */
590 get_deref_alias_set (tree t
)
592 alias_set_type set
= get_deref_alias_set_1 (t
);
594 /* Fall back to the alias-set of the pointed-to type. */
599 set
= get_alias_set (TREE_TYPE (t
));
605 /* Return the alias set for T, which may be either a type or an
606 expression. Call language-specific routine for help, if needed. */
609 get_alias_set (tree t
)
613 /* If we're not doing any alias analysis, just assume everything
614 aliases everything else. Also return 0 if this or its type is
616 if (! flag_strict_aliasing
|| t
== error_mark_node
618 && (TREE_TYPE (t
) == 0 || TREE_TYPE (t
) == error_mark_node
)))
621 /* We can be passed either an expression or a type. This and the
622 language-specific routine may make mutually-recursive calls to each other
623 to figure out what to do. At each juncture, we see if this is a tree
624 that the language may need to handle specially. First handle things that
630 /* Give the language a chance to do something with this tree
631 before we look at it. */
633 set
= lang_hooks
.get_alias_set (t
);
637 /* Get the base object of the reference. */
639 while (handled_component_p (inner
))
641 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
642 the type of any component references that wrap it to
643 determine the alias-set. */
644 if (TREE_CODE (inner
) == VIEW_CONVERT_EXPR
)
645 t
= TREE_OPERAND (inner
, 0);
646 inner
= TREE_OPERAND (inner
, 0);
649 /* Handle pointer dereferences here, they can override the
651 if (INDIRECT_REF_P (inner
))
653 set
= get_deref_alias_set_1 (TREE_OPERAND (inner
, 0));
657 else if (TREE_CODE (inner
) == TARGET_MEM_REF
)
658 return get_deref_alias_set (TMR_OFFSET (inner
));
659 else if (TREE_CODE (inner
) == MEM_REF
)
661 set
= get_deref_alias_set_1 (TREE_OPERAND (inner
, 1));
666 /* If the innermost reference is a MEM_REF that has a
667 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
668 using the memory access type for determining the alias-set. */
669 if (TREE_CODE (inner
) == MEM_REF
670 && TYPE_MAIN_VARIANT (TREE_TYPE (inner
))
672 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner
, 1)))))
673 return get_deref_alias_set (TREE_OPERAND (inner
, 1));
675 /* Otherwise, pick up the outermost object that we could have a pointer
676 to, processing conversions as above. */
677 while (component_uses_parent_alias_set (t
))
679 t
= TREE_OPERAND (t
, 0);
683 /* If we've already determined the alias set for a decl, just return
684 it. This is necessary for C++ anonymous unions, whose component
685 variables don't look like union members (boo!). */
686 if (TREE_CODE (t
) == VAR_DECL
687 && DECL_RTL_SET_P (t
) && MEM_P (DECL_RTL (t
)))
688 return MEM_ALIAS_SET (DECL_RTL (t
));
690 /* Now all we care about is the type. */
694 /* Variant qualifiers don't affect the alias set, so get the main
696 t
= TYPE_MAIN_VARIANT (t
);
698 /* Always use the canonical type as well. If this is a type that
699 requires structural comparisons to identify compatible types
700 use alias set zero. */
701 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
703 /* Allow the language to specify another alias set for this
705 set
= lang_hooks
.get_alias_set (t
);
711 t
= TYPE_CANONICAL (t
);
713 /* Canonical types shouldn't form a tree nor should the canonical
714 type require structural equality checks. */
715 gcc_checking_assert (TYPE_CANONICAL (t
) == t
716 && !TYPE_STRUCTURAL_EQUALITY_P (t
));
718 /* If this is a type with a known alias set, return it. */
719 if (TYPE_ALIAS_SET_KNOWN_P (t
))
720 return TYPE_ALIAS_SET (t
);
722 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
723 if (!COMPLETE_TYPE_P (t
))
725 /* For arrays with unknown size the conservative answer is the
726 alias set of the element type. */
727 if (TREE_CODE (t
) == ARRAY_TYPE
)
728 return get_alias_set (TREE_TYPE (t
));
730 /* But return zero as a conservative answer for incomplete types. */
734 /* See if the language has special handling for this type. */
735 set
= lang_hooks
.get_alias_set (t
);
739 /* There are no objects of FUNCTION_TYPE, so there's no point in
740 using up an alias set for them. (There are, of course, pointers
741 and references to functions, but that's different.) */
742 else if (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
)
745 /* Unless the language specifies otherwise, let vector types alias
746 their components. This avoids some nasty type punning issues in
747 normal usage. And indeed lets vectors be treated more like an
749 else if (TREE_CODE (t
) == VECTOR_TYPE
)
750 set
= get_alias_set (TREE_TYPE (t
));
752 /* Unless the language specifies otherwise, treat array types the
753 same as their components. This avoids the asymmetry we get
754 through recording the components. Consider accessing a
755 character(kind=1) through a reference to a character(kind=1)[1:1].
756 Or consider if we want to assign integer(kind=4)[0:D.1387] and
757 integer(kind=4)[4] the same alias set or not.
758 Just be pragmatic here and make sure the array and its element
759 type get the same alias set assigned. */
760 else if (TREE_CODE (t
) == ARRAY_TYPE
&& !TYPE_NONALIASED_COMPONENT (t
))
761 set
= get_alias_set (TREE_TYPE (t
));
763 /* From the former common C and C++ langhook implementation:
765 Unfortunately, there is no canonical form of a pointer type.
766 In particular, if we have `typedef int I', then `int *', and
767 `I *' are different types. So, we have to pick a canonical
768 representative. We do this below.
770 Technically, this approach is actually more conservative that
771 it needs to be. In particular, `const int *' and `int *'
772 should be in different alias sets, according to the C and C++
773 standard, since their types are not the same, and so,
774 technically, an `int **' and `const int **' cannot point at
777 But, the standard is wrong. In particular, this code is
782 const int* const* cipp = ipp;
783 And, it doesn't make sense for that to be legal unless you
784 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
785 the pointed-to types. This issue has been reported to the
788 In addition to the above canonicalization issue, with LTO
789 we should also canonicalize `T (*)[]' to `T *' avoiding
790 alias issues with pointer-to element types and pointer-to
793 Likewise we need to deal with the situation of incomplete
794 pointed-to types and make `*(struct X **)&a' and
795 `*(struct X {} **)&a' alias. Otherwise we will have to
796 guarantee that all pointer-to incomplete type variants
797 will be replaced by pointer-to complete type variants if
800 With LTO the convenient situation of using `void *' to
801 access and store any pointer type will also become
802 more apparent (and `void *' is just another pointer-to
803 incomplete type). Assigning alias-set zero to `void *'
804 and all pointer-to incomplete types is a not appealing
805 solution. Assigning an effective alias-set zero only
806 affecting pointers might be - by recording proper subset
807 relationships of all pointer alias-sets.
809 Pointer-to function types are another grey area which
810 needs caution. Globbing them all into one alias-set
811 or the above effective zero set would work.
813 For now just assign the same alias-set to all pointers.
814 That's simple and avoids all the above problems. */
815 else if (POINTER_TYPE_P (t
)
816 && t
!= ptr_type_node
)
817 return get_alias_set (ptr_type_node
);
819 /* Otherwise make a new alias set for this type. */
821 set
= new_alias_set ();
823 TYPE_ALIAS_SET (t
) = set
;
825 /* If this is an aggregate type or a complex type, we must record any
826 component aliasing information. */
827 if (AGGREGATE_TYPE_P (t
) || TREE_CODE (t
) == COMPLEX_TYPE
)
828 record_component_aliases (t
);
833 /* Return a brand-new alias set. */
838 if (flag_strict_aliasing
)
841 VEC_safe_push (alias_set_entry
, gc
, alias_sets
, 0);
842 VEC_safe_push (alias_set_entry
, gc
, alias_sets
, 0);
843 return VEC_length (alias_set_entry
, alias_sets
) - 1;
849 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
850 not everything that aliases SUPERSET also aliases SUBSET. For example,
851 in C, a store to an `int' can alias a load of a structure containing an
852 `int', and vice versa. But it can't alias a load of a 'double' member
853 of the same structure. Here, the structure would be the SUPERSET and
854 `int' the SUBSET. This relationship is also described in the comment at
855 the beginning of this file.
857 This function should be called only once per SUPERSET/SUBSET pair.
859 It is illegal for SUPERSET to be zero; everything is implicitly a
860 subset of alias set zero. */
863 record_alias_subset (alias_set_type superset
, alias_set_type subset
)
865 alias_set_entry superset_entry
;
866 alias_set_entry subset_entry
;
868 /* It is possible in complex type situations for both sets to be the same,
869 in which case we can ignore this operation. */
870 if (superset
== subset
)
873 gcc_assert (superset
);
875 superset_entry
= get_alias_set_entry (superset
);
876 if (superset_entry
== 0)
878 /* Create an entry for the SUPERSET, so that we have a place to
879 attach the SUBSET. */
880 superset_entry
= ggc_alloc_cleared_alias_set_entry_d ();
881 superset_entry
->alias_set
= superset
;
882 superset_entry
->children
883 = splay_tree_new_ggc (splay_tree_compare_ints
,
884 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s
,
885 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s
);
886 superset_entry
->has_zero_child
= 0;
887 VEC_replace (alias_set_entry
, alias_sets
, superset
, superset_entry
);
891 superset_entry
->has_zero_child
= 1;
894 subset_entry
= get_alias_set_entry (subset
);
895 /* If there is an entry for the subset, enter all of its children
896 (if they are not already present) as children of the SUPERSET. */
899 if (subset_entry
->has_zero_child
)
900 superset_entry
->has_zero_child
= 1;
902 splay_tree_foreach (subset_entry
->children
, insert_subset_children
,
903 superset_entry
->children
);
906 /* Enter the SUBSET itself as a child of the SUPERSET. */
907 splay_tree_insert (superset_entry
->children
,
908 (splay_tree_key
) subset
, 0);
912 /* Record that component types of TYPE, if any, are part of that type for
913 aliasing purposes. For record types, we only record component types
914 for fields that are not marked non-addressable. For array types, we
915 only record the component type if it is not marked non-aliased. */
918 record_component_aliases (tree type
)
920 alias_set_type superset
= get_alias_set (type
);
926 switch (TREE_CODE (type
))
930 case QUAL_UNION_TYPE
:
931 /* Recursively record aliases for the base classes, if there are any. */
932 if (TYPE_BINFO (type
))
935 tree binfo
, base_binfo
;
937 for (binfo
= TYPE_BINFO (type
), i
= 0;
938 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
939 record_alias_subset (superset
,
940 get_alias_set (BINFO_TYPE (base_binfo
)));
942 for (field
= TYPE_FIELDS (type
); field
!= 0; field
= DECL_CHAIN (field
))
943 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_NONADDRESSABLE_P (field
))
944 record_alias_subset (superset
, get_alias_set (TREE_TYPE (field
)));
948 record_alias_subset (superset
, get_alias_set (TREE_TYPE (type
)));
951 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
959 /* Allocate an alias set for use in storing and reading from the varargs
962 static GTY(()) alias_set_type varargs_set
= -1;
965 get_varargs_alias_set (void)
968 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
969 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
970 consistently use the varargs alias set for loads from the varargs
971 area. So don't use it anywhere. */
974 if (varargs_set
== -1)
975 varargs_set
= new_alias_set ();
981 /* Likewise, but used for the fixed portions of the frame, e.g., register
984 static GTY(()) alias_set_type frame_set
= -1;
987 get_frame_alias_set (void)
990 frame_set
= new_alias_set ();
995 /* Inside SRC, the source of a SET, find a base address. */
998 find_base_value (rtx src
)
1002 #if defined (FIND_BASE_TERM)
1003 /* Try machine-dependent ways to find the base term. */
1004 src
= FIND_BASE_TERM (src
);
1007 switch (GET_CODE (src
))
1014 regno
= REGNO (src
);
1015 /* At the start of a function, argument registers have known base
1016 values which may be lost later. Returning an ADDRESS
1017 expression here allows optimization based on argument values
1018 even when the argument registers are used for other purposes. */
1019 if (regno
< FIRST_PSEUDO_REGISTER
&& copying_arguments
)
1020 return new_reg_base_value
[regno
];
1022 /* If a pseudo has a known base value, return it. Do not do this
1023 for non-fixed hard regs since it can result in a circular
1024 dependency chain for registers which have values at function entry.
1026 The test above is not sufficient because the scheduler may move
1027 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1028 if ((regno
>= FIRST_PSEUDO_REGISTER
|| fixed_regs
[regno
])
1029 && regno
< VEC_length (rtx
, reg_base_value
))
1031 /* If we're inside init_alias_analysis, use new_reg_base_value
1032 to reduce the number of relaxation iterations. */
1033 if (new_reg_base_value
&& new_reg_base_value
[regno
]
1034 && DF_REG_DEF_COUNT (regno
) == 1)
1035 return new_reg_base_value
[regno
];
1037 if (VEC_index (rtx
, reg_base_value
, regno
))
1038 return VEC_index (rtx
, reg_base_value
, regno
);
1044 /* Check for an argument passed in memory. Only record in the
1045 copying-arguments block; it is too hard to track changes
1047 if (copying_arguments
1048 && (XEXP (src
, 0) == arg_pointer_rtx
1049 || (GET_CODE (XEXP (src
, 0)) == PLUS
1050 && XEXP (XEXP (src
, 0), 0) == arg_pointer_rtx
)))
1051 return gen_rtx_ADDRESS (VOIDmode
, src
);
1055 src
= XEXP (src
, 0);
1056 if (GET_CODE (src
) != PLUS
&& GET_CODE (src
) != MINUS
)
1059 /* ... fall through ... */
1064 rtx temp
, src_0
= XEXP (src
, 0), src_1
= XEXP (src
, 1);
1066 /* If either operand is a REG that is a known pointer, then it
1068 if (REG_P (src_0
) && REG_POINTER (src_0
))
1069 return find_base_value (src_0
);
1070 if (REG_P (src_1
) && REG_POINTER (src_1
))
1071 return find_base_value (src_1
);
1073 /* If either operand is a REG, then see if we already have
1074 a known value for it. */
1077 temp
= find_base_value (src_0
);
1084 temp
= find_base_value (src_1
);
1089 /* If either base is named object or a special address
1090 (like an argument or stack reference), then use it for the
1093 && (GET_CODE (src_0
) == SYMBOL_REF
1094 || GET_CODE (src_0
) == LABEL_REF
1095 || (GET_CODE (src_0
) == ADDRESS
1096 && GET_MODE (src_0
) != VOIDmode
)))
1100 && (GET_CODE (src_1
) == SYMBOL_REF
1101 || GET_CODE (src_1
) == LABEL_REF
1102 || (GET_CODE (src_1
) == ADDRESS
1103 && GET_MODE (src_1
) != VOIDmode
)))
1106 /* Guess which operand is the base address:
1107 If either operand is a symbol, then it is the base. If
1108 either operand is a CONST_INT, then the other is the base. */
1109 if (CONST_INT_P (src_1
) || CONSTANT_P (src_0
))
1110 return find_base_value (src_0
);
1111 else if (CONST_INT_P (src_0
) || CONSTANT_P (src_1
))
1112 return find_base_value (src_1
);
1118 /* The standard form is (lo_sum reg sym) so look only at the
1120 return find_base_value (XEXP (src
, 1));
1123 /* If the second operand is constant set the base
1124 address to the first operand. */
1125 if (CONST_INT_P (XEXP (src
, 1)) && INTVAL (XEXP (src
, 1)) != 0)
1126 return find_base_value (XEXP (src
, 0));
1130 /* As we do not know which address space the pointer is refering to, we can
1131 handle this only if the target does not support different pointer or
1132 address modes depending on the address space. */
1133 if (!target_default_pointer_address_modes_p ())
1135 if (GET_MODE_SIZE (GET_MODE (src
)) < GET_MODE_SIZE (Pmode
))
1145 return find_base_value (XEXP (src
, 0));
1148 case SIGN_EXTEND
: /* used for NT/Alpha pointers */
1149 /* As we do not know which address space the pointer is refering to, we can
1150 handle this only if the target does not support different pointer or
1151 address modes depending on the address space. */
1152 if (!target_default_pointer_address_modes_p ())
1156 rtx temp
= find_base_value (XEXP (src
, 0));
1158 if (temp
!= 0 && CONSTANT_P (temp
))
1159 temp
= convert_memory_address (Pmode
, temp
);
1171 /* Called from init_alias_analysis indirectly through note_stores. */
1173 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1174 register N has been set in this function. */
1175 static char *reg_seen
;
1177 /* Addresses which are known not to alias anything else are identified
1178 by a unique integer. */
1179 static int unique_id
;
1182 record_set (rtx dest
, const_rtx set
, void *data ATTRIBUTE_UNUSED
)
1191 regno
= REGNO (dest
);
1193 gcc_checking_assert (regno
< VEC_length (rtx
, reg_base_value
));
1195 /* If this spans multiple hard registers, then we must indicate that every
1196 register has an unusable value. */
1197 if (regno
< FIRST_PSEUDO_REGISTER
)
1198 n
= hard_regno_nregs
[regno
][GET_MODE (dest
)];
1205 reg_seen
[regno
+ n
] = 1;
1206 new_reg_base_value
[regno
+ n
] = 0;
1213 /* A CLOBBER wipes out any old value but does not prevent a previously
1214 unset register from acquiring a base address (i.e. reg_seen is not
1216 if (GET_CODE (set
) == CLOBBER
)
1218 new_reg_base_value
[regno
] = 0;
1221 src
= SET_SRC (set
);
1225 if (reg_seen
[regno
])
1227 new_reg_base_value
[regno
] = 0;
1230 reg_seen
[regno
] = 1;
1231 new_reg_base_value
[regno
] = gen_rtx_ADDRESS (Pmode
,
1232 GEN_INT (unique_id
++));
1236 /* If this is not the first set of REGNO, see whether the new value
1237 is related to the old one. There are two cases of interest:
1239 (1) The register might be assigned an entirely new value
1240 that has the same base term as the original set.
1242 (2) The set might be a simple self-modification that
1243 cannot change REGNO's base value.
1245 If neither case holds, reject the original base value as invalid.
1246 Note that the following situation is not detected:
1248 extern int x, y; int *p = &x; p += (&y-&x);
1250 ANSI C does not allow computing the difference of addresses
1251 of distinct top level objects. */
1252 if (new_reg_base_value
[regno
] != 0
1253 && find_base_value (src
) != new_reg_base_value
[regno
])
1254 switch (GET_CODE (src
))
1258 if (XEXP (src
, 0) != dest
&& XEXP (src
, 1) != dest
)
1259 new_reg_base_value
[regno
] = 0;
1262 /* If the value we add in the PLUS is also a valid base value,
1263 this might be the actual base value, and the original value
1266 rtx other
= NULL_RTX
;
1268 if (XEXP (src
, 0) == dest
)
1269 other
= XEXP (src
, 1);
1270 else if (XEXP (src
, 1) == dest
)
1271 other
= XEXP (src
, 0);
1273 if (! other
|| find_base_value (other
))
1274 new_reg_base_value
[regno
] = 0;
1278 if (XEXP (src
, 0) != dest
|| !CONST_INT_P (XEXP (src
, 1)))
1279 new_reg_base_value
[regno
] = 0;
1282 new_reg_base_value
[regno
] = 0;
1285 /* If this is the first set of a register, record the value. */
1286 else if ((regno
>= FIRST_PSEUDO_REGISTER
|| ! fixed_regs
[regno
])
1287 && ! reg_seen
[regno
] && new_reg_base_value
[regno
] == 0)
1288 new_reg_base_value
[regno
] = find_base_value (src
);
1290 reg_seen
[regno
] = 1;
1293 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1294 using hard registers with non-null REG_BASE_VALUE for renaming. */
1296 get_reg_base_value (unsigned int regno
)
1298 return VEC_index (rtx
, reg_base_value
, regno
);
1301 /* If a value is known for REGNO, return it. */
1304 get_reg_known_value (unsigned int regno
)
1306 if (regno
>= FIRST_PSEUDO_REGISTER
)
1308 regno
-= FIRST_PSEUDO_REGISTER
;
1309 if (regno
< reg_known_value_size
)
1310 return reg_known_value
[regno
];
1318 set_reg_known_value (unsigned int regno
, rtx val
)
1320 if (regno
>= FIRST_PSEUDO_REGISTER
)
1322 regno
-= FIRST_PSEUDO_REGISTER
;
1323 if (regno
< reg_known_value_size
)
1324 reg_known_value
[regno
] = val
;
1328 /* Similarly for reg_known_equiv_p. */
1331 get_reg_known_equiv_p (unsigned int regno
)
1333 if (regno
>= FIRST_PSEUDO_REGISTER
)
1335 regno
-= FIRST_PSEUDO_REGISTER
;
1336 if (regno
< reg_known_value_size
)
1337 return reg_known_equiv_p
[regno
];
1343 set_reg_known_equiv_p (unsigned int regno
, bool val
)
1345 if (regno
>= FIRST_PSEUDO_REGISTER
)
1347 regno
-= FIRST_PSEUDO_REGISTER
;
1348 if (regno
< reg_known_value_size
)
1349 reg_known_equiv_p
[regno
] = val
;
1354 /* Returns a canonical version of X, from the point of view alias
1355 analysis. (For example, if X is a MEM whose address is a register,
1356 and the register has a known value (say a SYMBOL_REF), then a MEM
1357 whose address is the SYMBOL_REF is returned.) */
1362 /* Recursively look for equivalences. */
1363 if (REG_P (x
) && REGNO (x
) >= FIRST_PSEUDO_REGISTER
)
1365 rtx t
= get_reg_known_value (REGNO (x
));
1369 return canon_rtx (t
);
1372 if (GET_CODE (x
) == PLUS
)
1374 rtx x0
= canon_rtx (XEXP (x
, 0));
1375 rtx x1
= canon_rtx (XEXP (x
, 1));
1377 if (x0
!= XEXP (x
, 0) || x1
!= XEXP (x
, 1))
1379 if (CONST_INT_P (x0
))
1380 return plus_constant (x1
, INTVAL (x0
));
1381 else if (CONST_INT_P (x1
))
1382 return plus_constant (x0
, INTVAL (x1
));
1383 return gen_rtx_PLUS (GET_MODE (x
), x0
, x1
);
1387 /* This gives us much better alias analysis when called from
1388 the loop optimizer. Note we want to leave the original
1389 MEM alone, but need to return the canonicalized MEM with
1390 all the flags with their original values. */
1392 x
= replace_equiv_address_nv (x
, canon_rtx (XEXP (x
, 0)));
1397 /* Return 1 if X and Y are identical-looking rtx's.
1398 Expect that X and Y has been already canonicalized.
1400 We use the data in reg_known_value above to see if two registers with
1401 different numbers are, in fact, equivalent. */
1404 rtx_equal_for_memref_p (const_rtx x
, const_rtx y
)
1411 if (x
== 0 && y
== 0)
1413 if (x
== 0 || y
== 0)
1419 code
= GET_CODE (x
);
1420 /* Rtx's of different codes cannot be equal. */
1421 if (code
!= GET_CODE (y
))
1424 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1425 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1427 if (GET_MODE (x
) != GET_MODE (y
))
1430 /* Some RTL can be compared without a recursive examination. */
1434 return REGNO (x
) == REGNO (y
);
1437 return XEXP (x
, 0) == XEXP (y
, 0);
1440 return XSTR (x
, 0) == XSTR (y
, 0);
1446 /* There's no need to compare the contents of CONST_DOUBLEs or
1447 CONST_INTs because pointer equality is a good enough
1448 comparison for these nodes. */
1455 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1457 return ((rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 0))
1458 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 1)))
1459 || (rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 1))
1460 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 0))));
1461 /* For commutative operations, the RTX match if the operand match in any
1462 order. Also handle the simple binary and unary cases without a loop. */
1463 if (COMMUTATIVE_P (x
))
1465 rtx xop0
= canon_rtx (XEXP (x
, 0));
1466 rtx yop0
= canon_rtx (XEXP (y
, 0));
1467 rtx yop1
= canon_rtx (XEXP (y
, 1));
1469 return ((rtx_equal_for_memref_p (xop0
, yop0
)
1470 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop1
))
1471 || (rtx_equal_for_memref_p (xop0
, yop1
)
1472 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop0
)));
1474 else if (NON_COMMUTATIVE_P (x
))
1476 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1477 canon_rtx (XEXP (y
, 0)))
1478 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)),
1479 canon_rtx (XEXP (y
, 1))));
1481 else if (UNARY_P (x
))
1482 return rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1483 canon_rtx (XEXP (y
, 0)));
1485 /* Compare the elements. If any pair of corresponding elements
1486 fail to match, return 0 for the whole things.
1488 Limit cases to types which actually appear in addresses. */
1490 fmt
= GET_RTX_FORMAT (code
);
1491 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1496 if (XINT (x
, i
) != XINT (y
, i
))
1501 /* Two vectors must have the same length. */
1502 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1505 /* And the corresponding elements must match. */
1506 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1507 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x
, i
, j
)),
1508 canon_rtx (XVECEXP (y
, i
, j
))) == 0)
1513 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, i
)),
1514 canon_rtx (XEXP (y
, i
))) == 0)
1518 /* This can happen for asm operands. */
1520 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1524 /* This can happen for an asm which clobbers memory. */
1528 /* It is believed that rtx's at this level will never
1529 contain anything but integers and other rtx's,
1530 except for within LABEL_REFs and SYMBOL_REFs. */
1539 find_base_term (rtx x
)
1542 struct elt_loc_list
*l
;
1544 #if defined (FIND_BASE_TERM)
1545 /* Try machine-dependent ways to find the base term. */
1546 x
= FIND_BASE_TERM (x
);
1549 switch (GET_CODE (x
))
1552 return REG_BASE_VALUE (x
);
1555 /* As we do not know which address space the pointer is refering to, we can
1556 handle this only if the target does not support different pointer or
1557 address modes depending on the address space. */
1558 if (!target_default_pointer_address_modes_p ())
1560 if (GET_MODE_SIZE (GET_MODE (x
)) < GET_MODE_SIZE (Pmode
))
1570 return find_base_term (XEXP (x
, 0));
1573 case SIGN_EXTEND
: /* Used for Alpha/NT pointers */
1574 /* As we do not know which address space the pointer is refering to, we can
1575 handle this only if the target does not support different pointer or
1576 address modes depending on the address space. */
1577 if (!target_default_pointer_address_modes_p ())
1581 rtx temp
= find_base_term (XEXP (x
, 0));
1583 if (temp
!= 0 && CONSTANT_P (temp
))
1584 temp
= convert_memory_address (Pmode
, temp
);
1590 val
= CSELIB_VAL_PTR (x
);
1593 for (l
= val
->locs
; l
; l
= l
->next
)
1594 if ((x
= find_base_term (l
->loc
)) != 0)
1599 /* The standard form is (lo_sum reg sym) so look only at the
1601 return find_base_term (XEXP (x
, 1));
1605 if (GET_CODE (x
) != PLUS
&& GET_CODE (x
) != MINUS
)
1611 rtx tmp1
= XEXP (x
, 0);
1612 rtx tmp2
= XEXP (x
, 1);
1614 /* This is a little bit tricky since we have to determine which of
1615 the two operands represents the real base address. Otherwise this
1616 routine may return the index register instead of the base register.
1618 That may cause us to believe no aliasing was possible, when in
1619 fact aliasing is possible.
1621 We use a few simple tests to guess the base register. Additional
1622 tests can certainly be added. For example, if one of the operands
1623 is a shift or multiply, then it must be the index register and the
1624 other operand is the base register. */
1626 if (tmp1
== pic_offset_table_rtx
&& CONSTANT_P (tmp2
))
1627 return find_base_term (tmp2
);
1629 /* If either operand is known to be a pointer, then use it
1630 to determine the base term. */
1631 if (REG_P (tmp1
) && REG_POINTER (tmp1
))
1633 rtx base
= find_base_term (tmp1
);
1638 if (REG_P (tmp2
) && REG_POINTER (tmp2
))
1640 rtx base
= find_base_term (tmp2
);
1645 /* Neither operand was known to be a pointer. Go ahead and find the
1646 base term for both operands. */
1647 tmp1
= find_base_term (tmp1
);
1648 tmp2
= find_base_term (tmp2
);
1650 /* If either base term is named object or a special address
1651 (like an argument or stack reference), then use it for the
1654 && (GET_CODE (tmp1
) == SYMBOL_REF
1655 || GET_CODE (tmp1
) == LABEL_REF
1656 || (GET_CODE (tmp1
) == ADDRESS
1657 && GET_MODE (tmp1
) != VOIDmode
)))
1661 && (GET_CODE (tmp2
) == SYMBOL_REF
1662 || GET_CODE (tmp2
) == LABEL_REF
1663 || (GET_CODE (tmp2
) == ADDRESS
1664 && GET_MODE (tmp2
) != VOIDmode
)))
1667 /* We could not determine which of the two operands was the
1668 base register and which was the index. So we can determine
1669 nothing from the base alias check. */
1674 if (CONST_INT_P (XEXP (x
, 1)) && INTVAL (XEXP (x
, 1)) != 0)
1675 return find_base_term (XEXP (x
, 0));
1687 /* Return 0 if the addresses X and Y are known to point to different
1688 objects, 1 if they might be pointers to the same object. */
1691 base_alias_check (rtx x
, rtx y
, enum machine_mode x_mode
,
1692 enum machine_mode y_mode
)
1694 rtx x_base
= find_base_term (x
);
1695 rtx y_base
= find_base_term (y
);
1697 /* If the address itself has no known base see if a known equivalent
1698 value has one. If either address still has no known base, nothing
1699 is known about aliasing. */
1704 if (! flag_expensive_optimizations
|| (x_c
= canon_rtx (x
)) == x
)
1707 x_base
= find_base_term (x_c
);
1715 if (! flag_expensive_optimizations
|| (y_c
= canon_rtx (y
)) == y
)
1718 y_base
= find_base_term (y_c
);
1723 /* If the base addresses are equal nothing is known about aliasing. */
1724 if (rtx_equal_p (x_base
, y_base
))
1727 /* The base addresses are different expressions. If they are not accessed
1728 via AND, there is no conflict. We can bring knowledge of object
1729 alignment into play here. For example, on alpha, "char a, b;" can
1730 alias one another, though "char a; long b;" cannot. AND addesses may
1731 implicitly alias surrounding objects; i.e. unaligned access in DImode
1732 via AND address can alias all surrounding object types except those
1733 with aligment 8 or higher. */
1734 if (GET_CODE (x
) == AND
&& GET_CODE (y
) == AND
)
1736 if (GET_CODE (x
) == AND
1737 && (!CONST_INT_P (XEXP (x
, 1))
1738 || (int) GET_MODE_UNIT_SIZE (y_mode
) < -INTVAL (XEXP (x
, 1))))
1740 if (GET_CODE (y
) == AND
1741 && (!CONST_INT_P (XEXP (y
, 1))
1742 || (int) GET_MODE_UNIT_SIZE (x_mode
) < -INTVAL (XEXP (y
, 1))))
1745 /* Differing symbols not accessed via AND never alias. */
1746 if (GET_CODE (x_base
) != ADDRESS
&& GET_CODE (y_base
) != ADDRESS
)
1749 /* If one address is a stack reference there can be no alias:
1750 stack references using different base registers do not alias,
1751 a stack reference can not alias a parameter, and a stack reference
1752 can not alias a global. */
1753 if ((GET_CODE (x_base
) == ADDRESS
&& GET_MODE (x_base
) == Pmode
)
1754 || (GET_CODE (y_base
) == ADDRESS
&& GET_MODE (y_base
) == Pmode
))
1760 /* Convert the address X into something we can use. This is done by returning
1761 it unchanged unless it is a value; in the latter case we call cselib to get
1762 a more useful rtx. */
1768 struct elt_loc_list
*l
;
1770 if (GET_CODE (x
) != VALUE
)
1772 v
= CSELIB_VAL_PTR (x
);
1775 for (l
= v
->locs
; l
; l
= l
->next
)
1776 if (CONSTANT_P (l
->loc
))
1778 for (l
= v
->locs
; l
; l
= l
->next
)
1779 if (!REG_P (l
->loc
) && !MEM_P (l
->loc
))
1782 return v
->locs
->loc
;
1787 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1788 where SIZE is the size in bytes of the memory reference. If ADDR
1789 is not modified by the memory reference then ADDR is returned. */
1792 addr_side_effect_eval (rtx addr
, int size
, int n_refs
)
1796 switch (GET_CODE (addr
))
1799 offset
= (n_refs
+ 1) * size
;
1802 offset
= -(n_refs
+ 1) * size
;
1805 offset
= n_refs
* size
;
1808 offset
= -n_refs
* size
;
1816 addr
= gen_rtx_PLUS (GET_MODE (addr
), XEXP (addr
, 0),
1819 addr
= XEXP (addr
, 0);
1820 addr
= canon_rtx (addr
);
1825 /* Return one if X and Y (memory addresses) reference the
1826 same location in memory or if the references overlap.
1827 Return zero if they do not overlap, else return
1828 minus one in which case they still might reference the same location.
1830 C is an offset accumulator. When
1831 C is nonzero, we are testing aliases between X and Y + C.
1832 XSIZE is the size in bytes of the X reference,
1833 similarly YSIZE is the size in bytes for Y.
1834 Expect that canon_rtx has been already called for X and Y.
1836 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1837 referenced (the reference was BLKmode), so make the most pessimistic
1840 If XSIZE or YSIZE is negative, we may access memory outside the object
1841 being referenced as a side effect. This can happen when using AND to
1842 align memory references, as is done on the Alpha.
1844 Nice to notice that varying addresses cannot conflict with fp if no
1845 local variables had their addresses taken, but that's too hard now.
1847 ??? Contrary to the tree alias oracle this does not return
1848 one for X + non-constant and Y + non-constant when X and Y are equal.
1849 If that is fixed the TBAA hack for union type-punning can be removed. */
1852 memrefs_conflict_p (int xsize
, rtx x
, int ysize
, rtx y
, HOST_WIDE_INT c
)
1854 if (GET_CODE (x
) == VALUE
)
1858 struct elt_loc_list
*l
= NULL
;
1859 if (CSELIB_VAL_PTR (x
))
1860 for (l
= CSELIB_VAL_PTR (x
)->locs
; l
; l
= l
->next
)
1861 if (REG_P (l
->loc
) && rtx_equal_for_memref_p (l
->loc
, y
))
1868 /* Don't call get_addr if y is the same VALUE. */
1872 if (GET_CODE (y
) == VALUE
)
1876 struct elt_loc_list
*l
= NULL
;
1877 if (CSELIB_VAL_PTR (y
))
1878 for (l
= CSELIB_VAL_PTR (y
)->locs
; l
; l
= l
->next
)
1879 if (REG_P (l
->loc
) && rtx_equal_for_memref_p (l
->loc
, x
))
1886 /* Don't call get_addr if x is the same VALUE. */
1890 if (GET_CODE (x
) == HIGH
)
1892 else if (GET_CODE (x
) == LO_SUM
)
1895 x
= addr_side_effect_eval (x
, xsize
, 0);
1896 if (GET_CODE (y
) == HIGH
)
1898 else if (GET_CODE (y
) == LO_SUM
)
1901 y
= addr_side_effect_eval (y
, ysize
, 0);
1903 if (rtx_equal_for_memref_p (x
, y
))
1905 if (xsize
<= 0 || ysize
<= 0)
1907 if (c
>= 0 && xsize
> c
)
1909 if (c
< 0 && ysize
+c
> 0)
1914 /* This code used to check for conflicts involving stack references and
1915 globals but the base address alias code now handles these cases. */
1917 if (GET_CODE (x
) == PLUS
)
1919 /* The fact that X is canonicalized means that this
1920 PLUS rtx is canonicalized. */
1921 rtx x0
= XEXP (x
, 0);
1922 rtx x1
= XEXP (x
, 1);
1924 if (GET_CODE (y
) == PLUS
)
1926 /* The fact that Y is canonicalized means that this
1927 PLUS rtx is canonicalized. */
1928 rtx y0
= XEXP (y
, 0);
1929 rtx y1
= XEXP (y
, 1);
1931 if (rtx_equal_for_memref_p (x1
, y1
))
1932 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
1933 if (rtx_equal_for_memref_p (x0
, y0
))
1934 return memrefs_conflict_p (xsize
, x1
, ysize
, y1
, c
);
1935 if (CONST_INT_P (x1
))
1937 if (CONST_INT_P (y1
))
1938 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
,
1939 c
- INTVAL (x1
) + INTVAL (y1
));
1941 return memrefs_conflict_p (xsize
, x0
, ysize
, y
,
1944 else if (CONST_INT_P (y1
))
1945 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
1949 else if (CONST_INT_P (x1
))
1950 return memrefs_conflict_p (xsize
, x0
, ysize
, y
, c
- INTVAL (x1
));
1952 else if (GET_CODE (y
) == PLUS
)
1954 /* The fact that Y is canonicalized means that this
1955 PLUS rtx is canonicalized. */
1956 rtx y0
= XEXP (y
, 0);
1957 rtx y1
= XEXP (y
, 1);
1959 if (CONST_INT_P (y1
))
1960 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
1965 if (GET_CODE (x
) == GET_CODE (y
))
1966 switch (GET_CODE (x
))
1970 /* Handle cases where we expect the second operands to be the
1971 same, and check only whether the first operand would conflict
1974 rtx x1
= canon_rtx (XEXP (x
, 1));
1975 rtx y1
= canon_rtx (XEXP (y
, 1));
1976 if (! rtx_equal_for_memref_p (x1
, y1
))
1978 x0
= canon_rtx (XEXP (x
, 0));
1979 y0
= canon_rtx (XEXP (y
, 0));
1980 if (rtx_equal_for_memref_p (x0
, y0
))
1981 return (xsize
== 0 || ysize
== 0
1982 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
1984 /* Can't properly adjust our sizes. */
1985 if (!CONST_INT_P (x1
))
1987 xsize
/= INTVAL (x1
);
1988 ysize
/= INTVAL (x1
);
1990 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
1997 /* Treat an access through an AND (e.g. a subword access on an Alpha)
1998 as an access with indeterminate size. Assume that references
1999 besides AND are aligned, so if the size of the other reference is
2000 at least as large as the alignment, assume no other overlap. */
2001 if (GET_CODE (x
) == AND
&& CONST_INT_P (XEXP (x
, 1)))
2003 if (GET_CODE (y
) == AND
|| ysize
< -INTVAL (XEXP (x
, 1)))
2005 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)), ysize
, y
, c
);
2007 if (GET_CODE (y
) == AND
&& CONST_INT_P (XEXP (y
, 1)))
2009 /* ??? If we are indexing far enough into the array/structure, we
2010 may yet be able to determine that we can not overlap. But we
2011 also need to that we are far enough from the end not to overlap
2012 a following reference, so we do nothing with that for now. */
2013 if (GET_CODE (x
) == AND
|| xsize
< -INTVAL (XEXP (y
, 1)))
2015 return memrefs_conflict_p (xsize
, x
, ysize
, canon_rtx (XEXP (y
, 0)), c
);
2020 if (CONST_INT_P (x
) && CONST_INT_P (y
))
2022 c
+= (INTVAL (y
) - INTVAL (x
));
2023 return (xsize
<= 0 || ysize
<= 0
2024 || (c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0));
2027 if (GET_CODE (x
) == CONST
)
2029 if (GET_CODE (y
) == CONST
)
2030 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2031 ysize
, canon_rtx (XEXP (y
, 0)), c
);
2033 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2036 if (GET_CODE (y
) == CONST
)
2037 return memrefs_conflict_p (xsize
, x
, ysize
,
2038 canon_rtx (XEXP (y
, 0)), c
);
2041 return (xsize
<= 0 || ysize
<= 0
2042 || (rtx_equal_for_memref_p (x
, y
)
2043 && ((c
>= 0 && xsize
> c
) || (c
< 0 && ysize
+c
> 0))));
2051 /* Functions to compute memory dependencies.
2053 Since we process the insns in execution order, we can build tables
2054 to keep track of what registers are fixed (and not aliased), what registers
2055 are varying in known ways, and what registers are varying in unknown
2058 If both memory references are volatile, then there must always be a
2059 dependence between the two references, since their order can not be
2060 changed. A volatile and non-volatile reference can be interchanged
2063 A MEM_IN_STRUCT reference at a non-AND varying address can never
2064 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
2065 also must allow AND addresses, because they may generate accesses
2066 outside the object being referenced. This is used to generate
2067 aligned addresses from unaligned addresses, for instance, the alpha
2068 storeqi_unaligned pattern. */
2070 /* Read dependence: X is read after read in MEM takes place. There can
2071 only be a dependence here if both reads are volatile. */
2074 read_dependence (const_rtx mem
, const_rtx x
)
2076 return MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
);
2079 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
2080 MEM2 is a reference to a structure at a varying address, or returns
2081 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
2082 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
2083 to decide whether or not an address may vary; it should return
2084 nonzero whenever variation is possible.
2085 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
2088 fixed_scalar_and_varying_struct_p (const_rtx mem1
, const_rtx mem2
, rtx mem1_addr
,
2090 bool (*varies_p
) (const_rtx
, bool))
2092 if (! flag_strict_aliasing
)
2095 if (MEM_ALIAS_SET (mem2
)
2096 && MEM_SCALAR_P (mem1
) && MEM_IN_STRUCT_P (mem2
)
2097 && !varies_p (mem1_addr
, 1) && varies_p (mem2_addr
, 1))
2098 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
2102 if (MEM_ALIAS_SET (mem1
)
2103 && MEM_IN_STRUCT_P (mem1
) && MEM_SCALAR_P (mem2
)
2104 && varies_p (mem1_addr
, 1) && !varies_p (mem2_addr
, 1))
2105 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
2112 /* Returns nonzero if something about the mode or address format MEM1
2113 indicates that it might well alias *anything*. */
2116 aliases_everything_p (const_rtx mem
)
2118 if (GET_CODE (XEXP (mem
, 0)) == AND
)
2119 /* If the address is an AND, it's very hard to know at what it is
2120 actually pointing. */
2126 /* Return true if we can determine that the fields referenced cannot
2127 overlap for any pair of objects. */
2130 nonoverlapping_component_refs_p (const_tree x
, const_tree y
)
2132 const_tree fieldx
, fieldy
, typex
, typey
, orig_y
;
2134 if (!flag_strict_aliasing
)
2139 /* The comparison has to be done at a common type, since we don't
2140 know how the inheritance hierarchy works. */
2144 fieldx
= TREE_OPERAND (x
, 1);
2145 typex
= TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx
));
2150 fieldy
= TREE_OPERAND (y
, 1);
2151 typey
= TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy
));
2156 y
= TREE_OPERAND (y
, 0);
2158 while (y
&& TREE_CODE (y
) == COMPONENT_REF
);
2160 x
= TREE_OPERAND (x
, 0);
2162 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2163 /* Never found a common type. */
2167 /* If we're left with accessing different fields of a structure,
2169 if (TREE_CODE (typex
) == RECORD_TYPE
2170 && fieldx
!= fieldy
)
2173 /* The comparison on the current field failed. If we're accessing
2174 a very nested structure, look at the next outer level. */
2175 x
= TREE_OPERAND (x
, 0);
2176 y
= TREE_OPERAND (y
, 0);
2179 && TREE_CODE (x
) == COMPONENT_REF
2180 && TREE_CODE (y
) == COMPONENT_REF
);
2185 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2188 decl_for_component_ref (tree x
)
2192 x
= TREE_OPERAND (x
, 0);
2194 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2196 return x
&& DECL_P (x
) ? x
: NULL_TREE
;
2199 /* Walk up the COMPONENT_REF list and adjust OFFSET to compensate for the
2200 offset of the field reference. */
2203 adjust_offset_for_component_ref (tree x
, rtx offset
)
2205 HOST_WIDE_INT ioffset
;
2210 ioffset
= INTVAL (offset
);
2213 tree offset
= component_ref_field_offset (x
);
2214 tree field
= TREE_OPERAND (x
, 1);
2216 if (! host_integerp (offset
, 1))
2218 ioffset
+= (tree_low_cst (offset
, 1)
2219 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
2222 x
= TREE_OPERAND (x
, 0);
2224 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2226 return GEN_INT (ioffset
);
2229 /* Return nonzero if we can determine the exprs corresponding to memrefs
2230 X and Y and they do not overlap.
2231 If LOOP_VARIANT is set, skip offset-based disambiguation */
2234 nonoverlapping_memrefs_p (const_rtx x
, const_rtx y
, bool loop_invariant
)
2236 tree exprx
= MEM_EXPR (x
), expry
= MEM_EXPR (y
);
2239 rtx moffsetx
, moffsety
;
2240 HOST_WIDE_INT offsetx
= 0, offsety
= 0, sizex
, sizey
, tem
;
2242 /* Unless both have exprs, we can't tell anything. */
2243 if (exprx
== 0 || expry
== 0)
2246 /* For spill-slot accesses make sure we have valid offsets. */
2247 if ((exprx
== get_spill_slot_decl (false)
2248 && ! MEM_OFFSET (x
))
2249 || (expry
== get_spill_slot_decl (false)
2250 && ! MEM_OFFSET (y
)))
2253 /* If both are field references, we may be able to determine something. */
2254 if (TREE_CODE (exprx
) == COMPONENT_REF
2255 && TREE_CODE (expry
) == COMPONENT_REF
2256 && nonoverlapping_component_refs_p (exprx
, expry
))
2260 /* If the field reference test failed, look at the DECLs involved. */
2261 moffsetx
= MEM_OFFSET (x
);
2262 if (TREE_CODE (exprx
) == COMPONENT_REF
)
2264 tree t
= decl_for_component_ref (exprx
);
2267 moffsetx
= adjust_offset_for_component_ref (exprx
, moffsetx
);
2271 moffsety
= MEM_OFFSET (y
);
2272 if (TREE_CODE (expry
) == COMPONENT_REF
)
2274 tree t
= decl_for_component_ref (expry
);
2277 moffsety
= adjust_offset_for_component_ref (expry
, moffsety
);
2281 if (! DECL_P (exprx
) || ! DECL_P (expry
))
2284 /* With invalid code we can end up storing into the constant pool.
2285 Bail out to avoid ICEing when creating RTL for this.
2286 See gfortran.dg/lto/20091028-2_0.f90. */
2287 if (TREE_CODE (exprx
) == CONST_DECL
2288 || TREE_CODE (expry
) == CONST_DECL
)
2291 rtlx
= DECL_RTL (exprx
);
2292 rtly
= DECL_RTL (expry
);
2294 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2295 can't overlap unless they are the same because we never reuse that part
2296 of the stack frame used for locals for spilled pseudos. */
2297 if ((!MEM_P (rtlx
) || !MEM_P (rtly
))
2298 && ! rtx_equal_p (rtlx
, rtly
))
2301 /* If we have MEMs refering to different address spaces (which can
2302 potentially overlap), we cannot easily tell from the addresses
2303 whether the references overlap. */
2304 if (MEM_P (rtlx
) && MEM_P (rtly
)
2305 && MEM_ADDR_SPACE (rtlx
) != MEM_ADDR_SPACE (rtly
))
2308 /* Get the base and offsets of both decls. If either is a register, we
2309 know both are and are the same, so use that as the base. The only
2310 we can avoid overlap is if we can deduce that they are nonoverlapping
2311 pieces of that decl, which is very rare. */
2312 basex
= MEM_P (rtlx
) ? XEXP (rtlx
, 0) : rtlx
;
2313 if (GET_CODE (basex
) == PLUS
&& CONST_INT_P (XEXP (basex
, 1)))
2314 offsetx
= INTVAL (XEXP (basex
, 1)), basex
= XEXP (basex
, 0);
2316 basey
= MEM_P (rtly
) ? XEXP (rtly
, 0) : rtly
;
2317 if (GET_CODE (basey
) == PLUS
&& CONST_INT_P (XEXP (basey
, 1)))
2318 offsety
= INTVAL (XEXP (basey
, 1)), basey
= XEXP (basey
, 0);
2320 /* If the bases are different, we know they do not overlap if both
2321 are constants or if one is a constant and the other a pointer into the
2322 stack frame. Otherwise a different base means we can't tell if they
2324 if (! rtx_equal_p (basex
, basey
))
2325 return ((CONSTANT_P (basex
) && CONSTANT_P (basey
))
2326 || (CONSTANT_P (basex
) && REG_P (basey
)
2327 && REGNO_PTR_FRAME_P (REGNO (basey
)))
2328 || (CONSTANT_P (basey
) && REG_P (basex
)
2329 && REGNO_PTR_FRAME_P (REGNO (basex
))));
2331 /* Offset based disambiguation not appropriate for loop invariant */
2335 sizex
= (!MEM_P (rtlx
) ? (int) GET_MODE_SIZE (GET_MODE (rtlx
))
2336 : MEM_SIZE (rtlx
) ? INTVAL (MEM_SIZE (rtlx
))
2338 sizey
= (!MEM_P (rtly
) ? (int) GET_MODE_SIZE (GET_MODE (rtly
))
2339 : MEM_SIZE (rtly
) ? INTVAL (MEM_SIZE (rtly
)) :
2342 /* If we have an offset for either memref, it can update the values computed
2345 offsetx
+= INTVAL (moffsetx
), sizex
-= INTVAL (moffsetx
);
2347 offsety
+= INTVAL (moffsety
), sizey
-= INTVAL (moffsety
);
2349 /* If a memref has both a size and an offset, we can use the smaller size.
2350 We can't do this if the offset isn't known because we must view this
2351 memref as being anywhere inside the DECL's MEM. */
2352 if (MEM_SIZE (x
) && moffsetx
)
2353 sizex
= INTVAL (MEM_SIZE (x
));
2354 if (MEM_SIZE (y
) && moffsety
)
2355 sizey
= INTVAL (MEM_SIZE (y
));
2357 /* Put the values of the memref with the lower offset in X's values. */
2358 if (offsetx
> offsety
)
2360 tem
= offsetx
, offsetx
= offsety
, offsety
= tem
;
2361 tem
= sizex
, sizex
= sizey
, sizey
= tem
;
2364 /* If we don't know the size of the lower-offset value, we can't tell
2365 if they conflict. Otherwise, we do the test. */
2366 return sizex
>= 0 && offsety
>= offsetx
+ sizex
;
2369 /* Helper for true_dependence and canon_true_dependence.
2370 Checks for true dependence: X is read after store in MEM takes place.
2372 VARIES is the function that should be used as rtx_varies function.
2374 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2375 NULL_RTX, and the canonical addresses of MEM and X are both computed
2376 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2378 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2380 Returns 1 if there is a true dependence, 0 otherwise. */
2383 true_dependence_1 (const_rtx mem
, enum machine_mode mem_mode
, rtx mem_addr
,
2384 const_rtx x
, rtx x_addr
, bool (*varies
) (const_rtx
, bool),
2385 bool mem_canonicalized
)
2390 gcc_checking_assert (mem_canonicalized
? (mem_addr
!= NULL_RTX
)
2391 : (mem_addr
== NULL_RTX
&& x_addr
== NULL_RTX
));
2393 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2396 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2397 This is used in epilogue deallocation functions, and in cselib. */
2398 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2400 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2402 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2403 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2406 /* Read-only memory is by definition never modified, and therefore can't
2407 conflict with anything. We don't expect to find read-only set on MEM,
2408 but stupid user tricks can produce them, so don't die. */
2409 if (MEM_READONLY_P (x
))
2412 /* If we have MEMs refering to different address spaces (which can
2413 potentially overlap), we cannot easily tell from the addresses
2414 whether the references overlap. */
2415 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2420 mem_addr
= XEXP (mem
, 0);
2421 if (mem_mode
== VOIDmode
)
2422 mem_mode
= GET_MODE (mem
);
2427 x_addr
= XEXP (x
, 0);
2428 if (!((GET_CODE (x_addr
) == VALUE
2429 && GET_CODE (mem_addr
) != VALUE
2430 && reg_mentioned_p (x_addr
, mem_addr
))
2431 || (GET_CODE (x_addr
) != VALUE
2432 && GET_CODE (mem_addr
) == VALUE
2433 && reg_mentioned_p (mem_addr
, x_addr
))))
2435 x_addr
= get_addr (x_addr
);
2436 if (! mem_canonicalized
)
2437 mem_addr
= get_addr (mem_addr
);
2441 base
= find_base_term (x_addr
);
2442 if (base
&& (GET_CODE (base
) == LABEL_REF
2443 || (GET_CODE (base
) == SYMBOL_REF
2444 && CONSTANT_POOL_ADDRESS_P (base
))))
2447 if (! base_alias_check (x_addr
, mem_addr
, GET_MODE (x
), mem_mode
))
2450 x_addr
= canon_rtx (x_addr
);
2451 if (!mem_canonicalized
)
2452 mem_addr
= canon_rtx (mem_addr
);
2454 if ((ret
= memrefs_conflict_p (GET_MODE_SIZE (mem_mode
), mem_addr
,
2455 SIZE_FOR_MODE (x
), x_addr
, 0)) != -1)
2458 if (DIFFERENT_ALIAS_SETS_P (x
, mem
))
2461 if (nonoverlapping_memrefs_p (mem
, x
, false))
2464 if (aliases_everything_p (x
))
2467 /* We cannot use aliases_everything_p to test MEM, since we must look
2468 at MEM_ADDR, rather than XEXP (mem, 0). */
2469 if (GET_CODE (mem_addr
) == AND
)
2472 /* ??? In true_dependence we also allow BLKmode to alias anything. Why
2473 don't we do this in anti_dependence and output_dependence? */
2474 if (mem_mode
== BLKmode
|| GET_MODE (x
) == BLKmode
)
2477 if (fixed_scalar_and_varying_struct_p (mem
, x
, mem_addr
, x_addr
, varies
))
2480 return rtx_refs_may_alias_p (x
, mem
, true);
2483 /* True dependence: X is read after store in MEM takes place. */
2486 true_dependence (const_rtx mem
, enum machine_mode mem_mode
, const_rtx x
,
2487 bool (*varies
) (const_rtx
, bool))
2489 return true_dependence_1 (mem
, mem_mode
, NULL_RTX
,
2490 x
, NULL_RTX
, varies
,
2491 /*mem_canonicalized=*/false);
2494 /* Canonical true dependence: X is read after store in MEM takes place.
2495 Variant of true_dependence which assumes MEM has already been
2496 canonicalized (hence we no longer do that here).
2497 The mem_addr argument has been added, since true_dependence_1 computed
2498 this value prior to canonicalizing. */
2501 canon_true_dependence (const_rtx mem
, enum machine_mode mem_mode
, rtx mem_addr
,
2502 const_rtx x
, rtx x_addr
, bool (*varies
) (const_rtx
, bool))
2504 return true_dependence_1 (mem
, mem_mode
, mem_addr
,
2506 /*mem_canonicalized=*/true);
2509 /* Returns nonzero if a write to X might alias a previous read from
2510 (or, if WRITEP is nonzero, a write to) MEM. */
2513 write_dependence_p (const_rtx mem
, const_rtx x
, int writep
)
2515 rtx x_addr
, mem_addr
;
2516 const_rtx fixed_scalar
;
2520 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2523 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2524 This is used in epilogue deallocation functions. */
2525 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2527 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2529 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2530 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2533 /* A read from read-only memory can't conflict with read-write memory. */
2534 if (!writep
&& MEM_READONLY_P (mem
))
2537 /* If we have MEMs refering to different address spaces (which can
2538 potentially overlap), we cannot easily tell from the addresses
2539 whether the references overlap. */
2540 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2543 x_addr
= XEXP (x
, 0);
2544 mem_addr
= XEXP (mem
, 0);
2545 if (!((GET_CODE (x_addr
) == VALUE
2546 && GET_CODE (mem_addr
) != VALUE
2547 && reg_mentioned_p (x_addr
, mem_addr
))
2548 || (GET_CODE (x_addr
) != VALUE
2549 && GET_CODE (mem_addr
) == VALUE
2550 && reg_mentioned_p (mem_addr
, x_addr
))))
2552 x_addr
= get_addr (x_addr
);
2553 mem_addr
= get_addr (mem_addr
);
2558 base
= find_base_term (mem_addr
);
2559 if (base
&& (GET_CODE (base
) == LABEL_REF
2560 || (GET_CODE (base
) == SYMBOL_REF
2561 && CONSTANT_POOL_ADDRESS_P (base
))))
2565 if (! base_alias_check (x_addr
, mem_addr
, GET_MODE (x
),
2569 x_addr
= canon_rtx (x_addr
);
2570 mem_addr
= canon_rtx (mem_addr
);
2572 if ((ret
= memrefs_conflict_p (SIZE_FOR_MODE (mem
), mem_addr
,
2573 SIZE_FOR_MODE (x
), x_addr
, 0)) != -1)
2576 if (nonoverlapping_memrefs_p (x
, mem
, false))
2580 = fixed_scalar_and_varying_struct_p (mem
, x
, mem_addr
, x_addr
,
2583 if ((fixed_scalar
== mem
&& !aliases_everything_p (x
))
2584 || (fixed_scalar
== x
&& !aliases_everything_p (mem
)))
2587 return rtx_refs_may_alias_p (x
, mem
, false);
2590 /* Anti dependence: X is written after read in MEM takes place. */
2593 anti_dependence (const_rtx mem
, const_rtx x
)
2595 return write_dependence_p (mem
, x
, /*writep=*/0);
2598 /* Output dependence: X is written after store in MEM takes place. */
2601 output_dependence (const_rtx mem
, const_rtx x
)
2603 return write_dependence_p (mem
, x
, /*writep=*/1);
2608 /* Check whether X may be aliased with MEM. Don't do offset-based
2609 memory disambiguation & TBAA. */
2611 may_alias_p (const_rtx mem
, const_rtx x
)
2613 rtx x_addr
, mem_addr
;
2615 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2618 /* ??? In true_dependence we also allow BLKmode to alias anything. */
2619 if (GET_MODE (mem
) == BLKmode
|| GET_MODE (x
) == BLKmode
)
2622 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2623 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2626 /* Read-only memory is by definition never modified, and therefore can't
2627 conflict with anything. We don't expect to find read-only set on MEM,
2628 but stupid user tricks can produce them, so don't die. */
2629 if (MEM_READONLY_P (x
))
2632 /* If we have MEMs refering to different address spaces (which can
2633 potentially overlap), we cannot easily tell from the addresses
2634 whether the references overlap. */
2635 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2638 x_addr
= XEXP (x
, 0);
2639 mem_addr
= XEXP (mem
, 0);
2640 if (!((GET_CODE (x_addr
) == VALUE
2641 && GET_CODE (mem_addr
) != VALUE
2642 && reg_mentioned_p (x_addr
, mem_addr
))
2643 || (GET_CODE (x_addr
) != VALUE
2644 && GET_CODE (mem_addr
) == VALUE
2645 && reg_mentioned_p (mem_addr
, x_addr
))))
2647 x_addr
= get_addr (x_addr
);
2648 mem_addr
= get_addr (mem_addr
);
2651 if (! base_alias_check (x_addr
, mem_addr
, GET_MODE (x
), GET_MODE (mem_addr
)))
2654 x_addr
= canon_rtx (x_addr
);
2655 mem_addr
= canon_rtx (mem_addr
);
2657 if (nonoverlapping_memrefs_p (mem
, x
, true))
2660 if (aliases_everything_p (x
))
2663 /* We cannot use aliases_everything_p to test MEM, since we must look
2664 at MEM_ADDR, rather than XEXP (mem, 0). */
2665 if (GET_CODE (mem_addr
) == AND
)
2668 if (fixed_scalar_and_varying_struct_p (mem
, x
, mem_addr
, x_addr
,
2672 /* TBAA not valid for loop_invarint */
2673 return rtx_refs_may_alias_p (x
, mem
, false);
2677 init_alias_target (void)
2681 memset (static_reg_base_value
, 0, sizeof static_reg_base_value
);
2683 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2684 /* Check whether this register can hold an incoming pointer
2685 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2686 numbers, so translate if necessary due to register windows. */
2687 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i
))
2688 && HARD_REGNO_MODE_OK (i
, Pmode
))
2689 static_reg_base_value
[i
]
2690 = gen_rtx_ADDRESS (VOIDmode
, gen_rtx_REG (Pmode
, i
));
2692 static_reg_base_value
[STACK_POINTER_REGNUM
]
2693 = gen_rtx_ADDRESS (Pmode
, stack_pointer_rtx
);
2694 static_reg_base_value
[ARG_POINTER_REGNUM
]
2695 = gen_rtx_ADDRESS (Pmode
, arg_pointer_rtx
);
2696 static_reg_base_value
[FRAME_POINTER_REGNUM
]
2697 = gen_rtx_ADDRESS (Pmode
, frame_pointer_rtx
);
2698 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2699 static_reg_base_value
[HARD_FRAME_POINTER_REGNUM
]
2700 = gen_rtx_ADDRESS (Pmode
, hard_frame_pointer_rtx
);
2704 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2705 to be memory reference. */
2706 static bool memory_modified
;
2708 memory_modified_1 (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
2712 if (anti_dependence (x
, (const_rtx
)data
) || output_dependence (x
, (const_rtx
)data
))
2713 memory_modified
= true;
2718 /* Return true when INSN possibly modify memory contents of MEM
2719 (i.e. address can be modified). */
2721 memory_modified_in_insn_p (const_rtx mem
, const_rtx insn
)
2725 memory_modified
= false;
2726 note_stores (PATTERN (insn
), memory_modified_1
, CONST_CAST_RTX(mem
));
2727 return memory_modified
;
2730 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2734 init_alias_analysis (void)
2736 unsigned int maxreg
= max_reg_num ();
2742 timevar_push (TV_ALIAS_ANALYSIS
);
2744 reg_known_value_size
= maxreg
- FIRST_PSEUDO_REGISTER
;
2745 reg_known_value
= ggc_alloc_cleared_vec_rtx (reg_known_value_size
);
2746 reg_known_equiv_p
= XCNEWVEC (bool, reg_known_value_size
);
2748 /* If we have memory allocated from the previous run, use it. */
2749 if (old_reg_base_value
)
2750 reg_base_value
= old_reg_base_value
;
2753 VEC_truncate (rtx
, reg_base_value
, 0);
2755 VEC_safe_grow_cleared (rtx
, gc
, reg_base_value
, maxreg
);
2757 new_reg_base_value
= XNEWVEC (rtx
, maxreg
);
2758 reg_seen
= XNEWVEC (char, maxreg
);
2760 /* The basic idea is that each pass through this loop will use the
2761 "constant" information from the previous pass to propagate alias
2762 information through another level of assignments.
2764 This could get expensive if the assignment chains are long. Maybe
2765 we should throttle the number of iterations, possibly based on
2766 the optimization level or flag_expensive_optimizations.
2768 We could propagate more information in the first pass by making use
2769 of DF_REG_DEF_COUNT to determine immediately that the alias information
2770 for a pseudo is "constant".
2772 A program with an uninitialized variable can cause an infinite loop
2773 here. Instead of doing a full dataflow analysis to detect such problems
2774 we just cap the number of iterations for the loop.
2776 The state of the arrays for the set chain in question does not matter
2777 since the program has undefined behavior. */
2782 /* Assume nothing will change this iteration of the loop. */
2785 /* We want to assign the same IDs each iteration of this loop, so
2786 start counting from zero each iteration of the loop. */
2789 /* We're at the start of the function each iteration through the
2790 loop, so we're copying arguments. */
2791 copying_arguments
= true;
2793 /* Wipe the potential alias information clean for this pass. */
2794 memset (new_reg_base_value
, 0, maxreg
* sizeof (rtx
));
2796 /* Wipe the reg_seen array clean. */
2797 memset (reg_seen
, 0, maxreg
);
2799 /* Mark all hard registers which may contain an address.
2800 The stack, frame and argument pointers may contain an address.
2801 An argument register which can hold a Pmode value may contain
2802 an address even if it is not in BASE_REGS.
2804 The address expression is VOIDmode for an argument and
2805 Pmode for other registers. */
2807 memcpy (new_reg_base_value
, static_reg_base_value
,
2808 FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
2810 /* Walk the insns adding values to the new_reg_base_value array. */
2811 for (insn
= get_insns (); insn
; insn
= NEXT_INSN (insn
))
2817 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2818 /* The prologue/epilogue insns are not threaded onto the
2819 insn chain until after reload has completed. Thus,
2820 there is no sense wasting time checking if INSN is in
2821 the prologue/epilogue until after reload has completed. */
2822 if (reload_completed
2823 && prologue_epilogue_contains (insn
))
2827 /* If this insn has a noalias note, process it, Otherwise,
2828 scan for sets. A simple set will have no side effects
2829 which could change the base value of any other register. */
2831 if (GET_CODE (PATTERN (insn
)) == SET
2832 && REG_NOTES (insn
) != 0
2833 && find_reg_note (insn
, REG_NOALIAS
, NULL_RTX
))
2834 record_set (SET_DEST (PATTERN (insn
)), NULL_RTX
, NULL
);
2836 note_stores (PATTERN (insn
), record_set
, NULL
);
2838 set
= single_set (insn
);
2841 && REG_P (SET_DEST (set
))
2842 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
2844 unsigned int regno
= REGNO (SET_DEST (set
));
2845 rtx src
= SET_SRC (set
);
2848 note
= find_reg_equal_equiv_note (insn
);
2849 if (note
&& REG_NOTE_KIND (note
) == REG_EQUAL
2850 && DF_REG_DEF_COUNT (regno
) != 1)
2853 if (note
!= NULL_RTX
2854 && GET_CODE (XEXP (note
, 0)) != EXPR_LIST
2855 && ! rtx_varies_p (XEXP (note
, 0), 1)
2856 && ! reg_overlap_mentioned_p (SET_DEST (set
),
2859 set_reg_known_value (regno
, XEXP (note
, 0));
2860 set_reg_known_equiv_p (regno
,
2861 REG_NOTE_KIND (note
) == REG_EQUIV
);
2863 else if (DF_REG_DEF_COUNT (regno
) == 1
2864 && GET_CODE (src
) == PLUS
2865 && REG_P (XEXP (src
, 0))
2866 && (t
= get_reg_known_value (REGNO (XEXP (src
, 0))))
2867 && CONST_INT_P (XEXP (src
, 1)))
2869 t
= plus_constant (t
, INTVAL (XEXP (src
, 1)));
2870 set_reg_known_value (regno
, t
);
2871 set_reg_known_equiv_p (regno
, 0);
2873 else if (DF_REG_DEF_COUNT (regno
) == 1
2874 && ! rtx_varies_p (src
, 1))
2876 set_reg_known_value (regno
, src
);
2877 set_reg_known_equiv_p (regno
, 0);
2881 else if (NOTE_P (insn
)
2882 && NOTE_KIND (insn
) == NOTE_INSN_FUNCTION_BEG
)
2883 copying_arguments
= false;
2886 /* Now propagate values from new_reg_base_value to reg_base_value. */
2887 gcc_assert (maxreg
== (unsigned int) max_reg_num ());
2889 for (ui
= 0; ui
< maxreg
; ui
++)
2891 if (new_reg_base_value
[ui
]
2892 && new_reg_base_value
[ui
] != VEC_index (rtx
, reg_base_value
, ui
)
2893 && ! rtx_equal_p (new_reg_base_value
[ui
],
2894 VEC_index (rtx
, reg_base_value
, ui
)))
2896 VEC_replace (rtx
, reg_base_value
, ui
, new_reg_base_value
[ui
]);
2901 while (changed
&& ++pass
< MAX_ALIAS_LOOP_PASSES
);
2903 /* Fill in the remaining entries. */
2904 for (i
= 0; i
< (int)reg_known_value_size
; i
++)
2905 if (reg_known_value
[i
] == 0)
2906 reg_known_value
[i
] = regno_reg_rtx
[i
+ FIRST_PSEUDO_REGISTER
];
2909 free (new_reg_base_value
);
2910 new_reg_base_value
= 0;
2913 timevar_pop (TV_ALIAS_ANALYSIS
);
2916 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
2917 Special API for var-tracking pass purposes. */
2920 vt_equate_reg_base_value (const_rtx reg1
, const_rtx reg2
)
2922 VEC_replace (rtx
, reg_base_value
, REGNO (reg1
), REG_BASE_VALUE (reg2
));
2926 end_alias_analysis (void)
2928 old_reg_base_value
= reg_base_value
;
2929 ggc_free (reg_known_value
);
2930 reg_known_value
= 0;
2931 reg_known_value_size
= 0;
2932 free (reg_known_equiv_p
);
2933 reg_known_equiv_p
= 0;
2936 #include "gt-alias.h"