1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
3 Contributed by John Carr (jfc@mit.edu).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
35 #include "diagnostic-core.h"
37 #include "splay-tree.h"
39 #include "langhooks.h"
45 #include "tree-ssa-alias.h"
46 #include "pointer-set.h"
47 #include "tree-flow.h"
49 /* The aliasing API provided here solves related but different problems:
51 Say there exists (in c)
65 Consider the four questions:
67 Can a store to x1 interfere with px2->y1?
68 Can a store to x1 interfere with px2->z2?
69 Can a store to x1 change the value pointed to by with py?
70 Can a store to x1 change the value pointed to by with pz?
72 The answer to these questions can be yes, yes, yes, and maybe.
74 The first two questions can be answered with a simple examination
75 of the type system. If structure X contains a field of type Y then
76 a store through a pointer to an X can overwrite any field that is
77 contained (recursively) in an X (unless we know that px1 != px2).
79 The last two questions can be solved in the same way as the first
80 two questions but this is too conservative. The observation is
81 that in some cases we can know which (if any) fields are addressed
82 and if those addresses are used in bad ways. This analysis may be
83 language specific. In C, arbitrary operations may be applied to
84 pointers. However, there is some indication that this may be too
85 conservative for some C++ types.
87 The pass ipa-type-escape does this analysis for the types whose
88 instances do not escape across the compilation boundary.
90 Historically in GCC, these two problems were combined and a single
91 data structure that was used to represent the solution to these
92 problems. We now have two similar but different data structures,
93 The data structure to solve the last two questions is similar to
94 the first, but does not contain the fields whose address are never
95 taken. For types that do escape the compilation unit, the data
96 structures will have identical information.
99 /* The alias sets assigned to MEMs assist the back-end in determining
100 which MEMs can alias which other MEMs. In general, two MEMs in
101 different alias sets cannot alias each other, with one important
102 exception. Consider something like:
104 struct S { int i; double d; };
106 a store to an `S' can alias something of either type `int' or type
107 `double'. (However, a store to an `int' cannot alias a `double'
108 and vice versa.) We indicate this via a tree structure that looks
116 (The arrows are directed and point downwards.)
117 In this situation we say the alias set for `struct S' is the
118 `superset' and that those for `int' and `double' are `subsets'.
120 To see whether two alias sets can point to the same memory, we must
121 see if either alias set is a subset of the other. We need not trace
122 past immediate descendants, however, since we propagate all
123 grandchildren up one level.
125 Alias set zero is implicitly a superset of all other alias sets.
126 However, this is no actual entry for alias set zero. It is an
127 error to attempt to explicitly construct a subset of zero. */
129 struct GTY(()) alias_set_entry_d
{
130 /* The alias set number, as stored in MEM_ALIAS_SET. */
131 alias_set_type alias_set
;
133 /* Nonzero if would have a child of zero: this effectively makes this
134 alias set the same as alias set zero. */
137 /* The children of the alias set. These are not just the immediate
138 children, but, in fact, all descendants. So, if we have:
140 struct T { struct S s; float f; }
142 continuing our example above, the children here will be all of
143 `int', `double', `float', and `struct S'. */
144 splay_tree
GTY((param1_is (int), param2_is (int))) children
;
146 typedef struct alias_set_entry_d
*alias_set_entry
;
148 static int rtx_equal_for_memref_p (const_rtx
, const_rtx
);
149 static int memrefs_conflict_p (int, rtx
, int, rtx
, HOST_WIDE_INT
);
150 static void record_set (rtx
, const_rtx
, void *);
151 static int base_alias_check (rtx
, rtx
, rtx
, rtx
, enum machine_mode
,
153 static rtx
find_base_value (rtx
);
154 static int mems_in_disjoint_alias_sets_p (const_rtx
, const_rtx
);
155 static int insert_subset_children (splay_tree_node
, void*);
156 static alias_set_entry
get_alias_set_entry (alias_set_type
);
157 static bool nonoverlapping_component_refs_p (const_rtx
, const_rtx
);
158 static tree
decl_for_component_ref (tree
);
159 static int write_dependence_p (const_rtx
,
160 const_rtx
, enum machine_mode
, rtx
,
163 static void memory_modified_1 (rtx
, const_rtx
, void *);
165 /* Set up all info needed to perform alias analysis on memory references. */
167 /* Returns the size in bytes of the mode of X. */
168 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
170 /* Cap the number of passes we make over the insns propagating alias
171 information through set chains.
172 ??? 10 is a completely arbitrary choice. This should be based on the
173 maximum loop depth in the CFG, but we do not have this information
174 available (even if current_loops _is_ available). */
175 #define MAX_ALIAS_LOOP_PASSES 10
177 /* reg_base_value[N] gives an address to which register N is related.
178 If all sets after the first add or subtract to the current value
179 or otherwise modify it so it does not point to a different top level
180 object, reg_base_value[N] is equal to the address part of the source
183 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
184 expressions represent three types of base:
186 1. incoming arguments. There is just one ADDRESS to represent all
187 arguments, since we do not know at this level whether accesses
188 based on different arguments can alias. The ADDRESS has id 0.
190 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
191 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
192 Each of these rtxes has a separate ADDRESS associated with it,
193 each with a negative id.
195 GCC is (and is required to be) precise in which register it
196 chooses to access a particular region of stack. We can therefore
197 assume that accesses based on one of these rtxes do not alias
198 accesses based on another of these rtxes.
200 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
201 Each such piece of memory has a separate ADDRESS associated
202 with it, each with an id greater than 0.
204 Accesses based on one ADDRESS do not alias accesses based on other
205 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
206 alias globals either; the ADDRESSes have Pmode to indicate this.
207 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
210 static GTY(()) vec
<rtx
, va_gc
> *reg_base_value
;
211 static rtx
*new_reg_base_value
;
213 /* The single VOIDmode ADDRESS that represents all argument bases.
215 static GTY(()) rtx arg_base_value
;
217 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
218 static int unique_id
;
220 /* We preserve the copy of old array around to avoid amount of garbage
221 produced. About 8% of garbage produced were attributed to this
223 static GTY((deletable
)) vec
<rtx
, va_gc
> *old_reg_base_value
;
225 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
227 #define UNIQUE_BASE_VALUE_SP -1
228 #define UNIQUE_BASE_VALUE_ARGP -2
229 #define UNIQUE_BASE_VALUE_FP -3
230 #define UNIQUE_BASE_VALUE_HFP -4
232 #define static_reg_base_value \
233 (this_target_rtl->x_static_reg_base_value)
235 #define REG_BASE_VALUE(X) \
236 (REGNO (X) < vec_safe_length (reg_base_value) \
237 ? (*reg_base_value)[REGNO (X)] : 0)
239 /* Vector indexed by N giving the initial (unchanging) value known for
240 pseudo-register N. This vector is initialized in init_alias_analysis,
241 and does not change until end_alias_analysis is called. */
242 static GTY(()) vec
<rtx
, va_gc
> *reg_known_value
;
244 /* Vector recording for each reg_known_value whether it is due to a
245 REG_EQUIV note. Future passes (viz., reload) may replace the
246 pseudo with the equivalent expression and so we account for the
247 dependences that would be introduced if that happens.
249 The REG_EQUIV notes created in assign_parms may mention the arg
250 pointer, and there are explicit insns in the RTL that modify the
251 arg pointer. Thus we must ensure that such insns don't get
252 scheduled across each other because that would invalidate the
253 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
254 wrong, but solving the problem in the scheduler will likely give
255 better code, so we do it here. */
256 static sbitmap reg_known_equiv_p
;
258 /* True when scanning insns from the start of the rtl to the
259 NOTE_INSN_FUNCTION_BEG note. */
260 static bool copying_arguments
;
263 /* The splay-tree used to store the various alias set entries. */
264 static GTY (()) vec
<alias_set_entry
, va_gc
> *alias_sets
;
266 /* Build a decomposed reference object for querying the alias-oracle
267 from the MEM rtx and store it in *REF.
268 Returns false if MEM is not suitable for the alias-oracle. */
271 ao_ref_from_mem (ao_ref
*ref
, const_rtx mem
)
273 tree expr
= MEM_EXPR (mem
);
279 ao_ref_init (ref
, expr
);
281 /* Get the base of the reference and see if we have to reject or
283 base
= ao_ref_base (ref
);
284 if (base
== NULL_TREE
)
287 /* The tree oracle doesn't like bases that are neither decls
288 nor indirect references of SSA names. */
290 || (TREE_CODE (base
) == MEM_REF
291 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
292 || (TREE_CODE (base
) == TARGET_MEM_REF
293 && TREE_CODE (TMR_BASE (base
)) == SSA_NAME
)))
296 /* If this is a reference based on a partitioned decl replace the
297 base with a MEM_REF of the pointer representative we
298 created during stack slot partitioning. */
299 if (TREE_CODE (base
) == VAR_DECL
300 && ! is_global_var (base
)
301 && cfun
->gimple_df
->decls_to_pointers
!= NULL
)
304 namep
= pointer_map_contains (cfun
->gimple_df
->decls_to_pointers
, base
);
306 ref
->base
= build_simple_mem_ref (*(tree
*)namep
);
309 ref
->ref_alias_set
= MEM_ALIAS_SET (mem
);
311 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
312 is conservative, so trust it. */
313 if (!MEM_OFFSET_KNOWN_P (mem
)
314 || !MEM_SIZE_KNOWN_P (mem
))
317 /* If the base decl is a parameter we can have negative MEM_OFFSET in
318 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
320 if (MEM_OFFSET (mem
) < 0
321 && (MEM_SIZE (mem
) + MEM_OFFSET (mem
)) * BITS_PER_UNIT
== ref
->size
)
324 /* Otherwise continue and refine size and offset we got from analyzing
325 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
327 ref
->offset
+= MEM_OFFSET (mem
) * BITS_PER_UNIT
;
328 ref
->size
= MEM_SIZE (mem
) * BITS_PER_UNIT
;
330 /* The MEM may extend into adjacent fields, so adjust max_size if
332 if (ref
->max_size
!= -1
333 && ref
->size
> ref
->max_size
)
334 ref
->max_size
= ref
->size
;
336 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
337 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
338 if (MEM_EXPR (mem
) != get_spill_slot_decl (false)
340 || (DECL_P (ref
->base
)
341 && (!host_integerp (DECL_SIZE (ref
->base
), 1)
342 || (TREE_INT_CST_LOW (DECL_SIZE ((ref
->base
)))
343 < (unsigned HOST_WIDE_INT
)(ref
->offset
+ ref
->size
))))))
349 /* Query the alias-oracle on whether the two memory rtx X and MEM may
350 alias. If TBAA_P is set also apply TBAA. Returns true if the
351 two rtxen may alias, false otherwise. */
354 rtx_refs_may_alias_p (const_rtx x
, const_rtx mem
, bool tbaa_p
)
358 if (!ao_ref_from_mem (&ref1
, x
)
359 || !ao_ref_from_mem (&ref2
, mem
))
362 return refs_may_alias_p_1 (&ref1
, &ref2
,
364 && MEM_ALIAS_SET (x
) != 0
365 && MEM_ALIAS_SET (mem
) != 0);
368 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
369 such an entry, or NULL otherwise. */
371 static inline alias_set_entry
372 get_alias_set_entry (alias_set_type alias_set
)
374 return (*alias_sets
)[alias_set
];
377 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
378 the two MEMs cannot alias each other. */
381 mems_in_disjoint_alias_sets_p (const_rtx mem1
, const_rtx mem2
)
383 /* Perform a basic sanity check. Namely, that there are no alias sets
384 if we're not using strict aliasing. This helps to catch bugs
385 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
386 where a MEM is allocated in some way other than by the use of
387 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
388 use alias sets to indicate that spilled registers cannot alias each
389 other, we might need to remove this check. */
390 gcc_assert (flag_strict_aliasing
391 || (!MEM_ALIAS_SET (mem1
) && !MEM_ALIAS_SET (mem2
)));
393 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1
), MEM_ALIAS_SET (mem2
));
396 /* Insert the NODE into the splay tree given by DATA. Used by
397 record_alias_subset via splay_tree_foreach. */
400 insert_subset_children (splay_tree_node node
, void *data
)
402 splay_tree_insert ((splay_tree
) data
, node
->key
, node
->value
);
407 /* Return true if the first alias set is a subset of the second. */
410 alias_set_subset_of (alias_set_type set1
, alias_set_type set2
)
414 /* Everything is a subset of the "aliases everything" set. */
418 /* Otherwise, check if set1 is a subset of set2. */
419 ase
= get_alias_set_entry (set2
);
421 && (ase
->has_zero_child
422 || splay_tree_lookup (ase
->children
,
423 (splay_tree_key
) set1
)))
428 /* Return 1 if the two specified alias sets may conflict. */
431 alias_sets_conflict_p (alias_set_type set1
, alias_set_type set2
)
436 if (alias_sets_must_conflict_p (set1
, set2
))
439 /* See if the first alias set is a subset of the second. */
440 ase
= get_alias_set_entry (set1
);
442 && (ase
->has_zero_child
443 || splay_tree_lookup (ase
->children
,
444 (splay_tree_key
) set2
)))
447 /* Now do the same, but with the alias sets reversed. */
448 ase
= get_alias_set_entry (set2
);
450 && (ase
->has_zero_child
451 || splay_tree_lookup (ase
->children
,
452 (splay_tree_key
) set1
)))
455 /* The two alias sets are distinct and neither one is the
456 child of the other. Therefore, they cannot conflict. */
460 /* Return 1 if the two specified alias sets will always conflict. */
463 alias_sets_must_conflict_p (alias_set_type set1
, alias_set_type set2
)
465 if (set1
== 0 || set2
== 0 || set1
== set2
)
471 /* Return 1 if any MEM object of type T1 will always conflict (using the
472 dependency routines in this file) with any MEM object of type T2.
473 This is used when allocating temporary storage. If T1 and/or T2 are
474 NULL_TREE, it means we know nothing about the storage. */
477 objects_must_conflict_p (tree t1
, tree t2
)
479 alias_set_type set1
, set2
;
481 /* If neither has a type specified, we don't know if they'll conflict
482 because we may be using them to store objects of various types, for
483 example the argument and local variables areas of inlined functions. */
484 if (t1
== 0 && t2
== 0)
487 /* If they are the same type, they must conflict. */
489 /* Likewise if both are volatile. */
490 || (t1
!= 0 && TYPE_VOLATILE (t1
) && t2
!= 0 && TYPE_VOLATILE (t2
)))
493 set1
= t1
? get_alias_set (t1
) : 0;
494 set2
= t2
? get_alias_set (t2
) : 0;
496 /* We can't use alias_sets_conflict_p because we must make sure
497 that every subtype of t1 will conflict with every subtype of
498 t2 for which a pair of subobjects of these respective subtypes
499 overlaps on the stack. */
500 return alias_sets_must_conflict_p (set1
, set2
);
503 /* Return true if all nested component references handled by
504 get_inner_reference in T are such that we should use the alias set
505 provided by the object at the heart of T.
507 This is true for non-addressable components (which don't have their
508 own alias set), as well as components of objects in alias set zero.
509 This later point is a special case wherein we wish to override the
510 alias set used by the component, but we don't have per-FIELD_DECL
511 assignable alias sets. */
514 component_uses_parent_alias_set (const_tree t
)
518 /* If we're at the end, it vacuously uses its own alias set. */
519 if (!handled_component_p (t
))
522 switch (TREE_CODE (t
))
525 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t
, 1)))
530 case ARRAY_RANGE_REF
:
531 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t
, 0))))
540 /* Bitfields and casts are never addressable. */
544 t
= TREE_OPERAND (t
, 0);
545 if (get_alias_set (TREE_TYPE (t
)) == 0)
550 /* Return the alias set for the memory pointed to by T, which may be
551 either a type or an expression. Return -1 if there is nothing
552 special about dereferencing T. */
554 static alias_set_type
555 get_deref_alias_set_1 (tree t
)
557 /* If we're not doing any alias analysis, just assume everything
558 aliases everything else. */
559 if (!flag_strict_aliasing
)
562 /* All we care about is the type. */
566 /* If we have an INDIRECT_REF via a void pointer, we don't
567 know anything about what that might alias. Likewise if the
568 pointer is marked that way. */
569 if (TREE_CODE (TREE_TYPE (t
)) == VOID_TYPE
570 || TYPE_REF_CAN_ALIAS_ALL (t
))
576 /* Return the alias set for the memory pointed to by T, which may be
577 either a type or an expression. */
580 get_deref_alias_set (tree t
)
582 alias_set_type set
= get_deref_alias_set_1 (t
);
584 /* Fall back to the alias-set of the pointed-to type. */
589 set
= get_alias_set (TREE_TYPE (t
));
595 /* Return the alias set for T, which may be either a type or an
596 expression. Call language-specific routine for help, if needed. */
599 get_alias_set (tree t
)
603 /* If we're not doing any alias analysis, just assume everything
604 aliases everything else. Also return 0 if this or its type is
606 if (! flag_strict_aliasing
|| t
== error_mark_node
608 && (TREE_TYPE (t
) == 0 || TREE_TYPE (t
) == error_mark_node
)))
611 /* We can be passed either an expression or a type. This and the
612 language-specific routine may make mutually-recursive calls to each other
613 to figure out what to do. At each juncture, we see if this is a tree
614 that the language may need to handle specially. First handle things that
620 /* Give the language a chance to do something with this tree
621 before we look at it. */
623 set
= lang_hooks
.get_alias_set (t
);
627 /* Get the base object of the reference. */
629 while (handled_component_p (inner
))
631 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
632 the type of any component references that wrap it to
633 determine the alias-set. */
634 if (TREE_CODE (inner
) == VIEW_CONVERT_EXPR
)
635 t
= TREE_OPERAND (inner
, 0);
636 inner
= TREE_OPERAND (inner
, 0);
639 /* Handle pointer dereferences here, they can override the
641 if (INDIRECT_REF_P (inner
))
643 set
= get_deref_alias_set_1 (TREE_OPERAND (inner
, 0));
647 else if (TREE_CODE (inner
) == TARGET_MEM_REF
)
648 return get_deref_alias_set (TMR_OFFSET (inner
));
649 else if (TREE_CODE (inner
) == MEM_REF
)
651 set
= get_deref_alias_set_1 (TREE_OPERAND (inner
, 1));
656 /* If the innermost reference is a MEM_REF that has a
657 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
658 using the memory access type for determining the alias-set. */
659 if (TREE_CODE (inner
) == MEM_REF
660 && TYPE_MAIN_VARIANT (TREE_TYPE (inner
))
662 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner
, 1)))))
663 return get_deref_alias_set (TREE_OPERAND (inner
, 1));
665 /* Otherwise, pick up the outermost object that we could have a pointer
666 to, processing conversions as above. */
667 while (component_uses_parent_alias_set (t
))
669 t
= TREE_OPERAND (t
, 0);
673 /* If we've already determined the alias set for a decl, just return
674 it. This is necessary for C++ anonymous unions, whose component
675 variables don't look like union members (boo!). */
676 if (TREE_CODE (t
) == VAR_DECL
677 && DECL_RTL_SET_P (t
) && MEM_P (DECL_RTL (t
)))
678 return MEM_ALIAS_SET (DECL_RTL (t
));
680 /* Now all we care about is the type. */
684 /* Variant qualifiers don't affect the alias set, so get the main
686 t
= TYPE_MAIN_VARIANT (t
);
688 /* Always use the canonical type as well. If this is a type that
689 requires structural comparisons to identify compatible types
690 use alias set zero. */
691 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
693 /* Allow the language to specify another alias set for this
695 set
= lang_hooks
.get_alias_set (t
);
701 t
= TYPE_CANONICAL (t
);
703 /* The canonical type should not require structural equality checks. */
704 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t
));
706 /* If this is a type with a known alias set, return it. */
707 if (TYPE_ALIAS_SET_KNOWN_P (t
))
708 return TYPE_ALIAS_SET (t
);
710 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
711 if (!COMPLETE_TYPE_P (t
))
713 /* For arrays with unknown size the conservative answer is the
714 alias set of the element type. */
715 if (TREE_CODE (t
) == ARRAY_TYPE
)
716 return get_alias_set (TREE_TYPE (t
));
718 /* But return zero as a conservative answer for incomplete types. */
722 /* See if the language has special handling for this type. */
723 set
= lang_hooks
.get_alias_set (t
);
727 /* There are no objects of FUNCTION_TYPE, so there's no point in
728 using up an alias set for them. (There are, of course, pointers
729 and references to functions, but that's different.) */
730 else if (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
)
733 /* Unless the language specifies otherwise, let vector types alias
734 their components. This avoids some nasty type punning issues in
735 normal usage. And indeed lets vectors be treated more like an
737 else if (TREE_CODE (t
) == VECTOR_TYPE
)
738 set
= get_alias_set (TREE_TYPE (t
));
740 /* Unless the language specifies otherwise, treat array types the
741 same as their components. This avoids the asymmetry we get
742 through recording the components. Consider accessing a
743 character(kind=1) through a reference to a character(kind=1)[1:1].
744 Or consider if we want to assign integer(kind=4)[0:D.1387] and
745 integer(kind=4)[4] the same alias set or not.
746 Just be pragmatic here and make sure the array and its element
747 type get the same alias set assigned. */
748 else if (TREE_CODE (t
) == ARRAY_TYPE
&& !TYPE_NONALIASED_COMPONENT (t
))
749 set
= get_alias_set (TREE_TYPE (t
));
751 /* From the former common C and C++ langhook implementation:
753 Unfortunately, there is no canonical form of a pointer type.
754 In particular, if we have `typedef int I', then `int *', and
755 `I *' are different types. So, we have to pick a canonical
756 representative. We do this below.
758 Technically, this approach is actually more conservative that
759 it needs to be. In particular, `const int *' and `int *'
760 should be in different alias sets, according to the C and C++
761 standard, since their types are not the same, and so,
762 technically, an `int **' and `const int **' cannot point at
765 But, the standard is wrong. In particular, this code is
770 const int* const* cipp = ipp;
771 And, it doesn't make sense for that to be legal unless you
772 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
773 the pointed-to types. This issue has been reported to the
776 In addition to the above canonicalization issue, with LTO
777 we should also canonicalize `T (*)[]' to `T *' avoiding
778 alias issues with pointer-to element types and pointer-to
781 Likewise we need to deal with the situation of incomplete
782 pointed-to types and make `*(struct X **)&a' and
783 `*(struct X {} **)&a' alias. Otherwise we will have to
784 guarantee that all pointer-to incomplete type variants
785 will be replaced by pointer-to complete type variants if
788 With LTO the convenient situation of using `void *' to
789 access and store any pointer type will also become
790 more apparent (and `void *' is just another pointer-to
791 incomplete type). Assigning alias-set zero to `void *'
792 and all pointer-to incomplete types is a not appealing
793 solution. Assigning an effective alias-set zero only
794 affecting pointers might be - by recording proper subset
795 relationships of all pointer alias-sets.
797 Pointer-to function types are another grey area which
798 needs caution. Globbing them all into one alias-set
799 or the above effective zero set would work.
801 For now just assign the same alias-set to all pointers.
802 That's simple and avoids all the above problems. */
803 else if (POINTER_TYPE_P (t
)
804 && t
!= ptr_type_node
)
805 set
= get_alias_set (ptr_type_node
);
807 /* Otherwise make a new alias set for this type. */
810 /* Each canonical type gets its own alias set, so canonical types
811 shouldn't form a tree. It doesn't really matter for types
812 we handle specially above, so only check it where it possibly
813 would result in a bogus alias set. */
814 gcc_checking_assert (TYPE_CANONICAL (t
) == t
);
816 set
= new_alias_set ();
819 TYPE_ALIAS_SET (t
) = set
;
821 /* If this is an aggregate type or a complex type, we must record any
822 component aliasing information. */
823 if (AGGREGATE_TYPE_P (t
) || TREE_CODE (t
) == COMPLEX_TYPE
)
824 record_component_aliases (t
);
829 /* Return a brand-new alias set. */
834 if (flag_strict_aliasing
)
837 vec_safe_push (alias_sets
, (alias_set_entry
) 0);
838 vec_safe_push (alias_sets
, (alias_set_entry
) 0);
839 return alias_sets
->length () - 1;
845 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
846 not everything that aliases SUPERSET also aliases SUBSET. For example,
847 in C, a store to an `int' can alias a load of a structure containing an
848 `int', and vice versa. But it can't alias a load of a 'double' member
849 of the same structure. Here, the structure would be the SUPERSET and
850 `int' the SUBSET. This relationship is also described in the comment at
851 the beginning of this file.
853 This function should be called only once per SUPERSET/SUBSET pair.
855 It is illegal for SUPERSET to be zero; everything is implicitly a
856 subset of alias set zero. */
859 record_alias_subset (alias_set_type superset
, alias_set_type subset
)
861 alias_set_entry superset_entry
;
862 alias_set_entry subset_entry
;
864 /* It is possible in complex type situations for both sets to be the same,
865 in which case we can ignore this operation. */
866 if (superset
== subset
)
869 gcc_assert (superset
);
871 superset_entry
= get_alias_set_entry (superset
);
872 if (superset_entry
== 0)
874 /* Create an entry for the SUPERSET, so that we have a place to
875 attach the SUBSET. */
876 superset_entry
= ggc_alloc_cleared_alias_set_entry_d ();
877 superset_entry
->alias_set
= superset
;
878 superset_entry
->children
879 = splay_tree_new_ggc (splay_tree_compare_ints
,
880 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s
,
881 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s
);
882 superset_entry
->has_zero_child
= 0;
883 (*alias_sets
)[superset
] = superset_entry
;
887 superset_entry
->has_zero_child
= 1;
890 subset_entry
= get_alias_set_entry (subset
);
891 /* If there is an entry for the subset, enter all of its children
892 (if they are not already present) as children of the SUPERSET. */
895 if (subset_entry
->has_zero_child
)
896 superset_entry
->has_zero_child
= 1;
898 splay_tree_foreach (subset_entry
->children
, insert_subset_children
,
899 superset_entry
->children
);
902 /* Enter the SUBSET itself as a child of the SUPERSET. */
903 splay_tree_insert (superset_entry
->children
,
904 (splay_tree_key
) subset
, 0);
908 /* Record that component types of TYPE, if any, are part of that type for
909 aliasing purposes. For record types, we only record component types
910 for fields that are not marked non-addressable. For array types, we
911 only record the component type if it is not marked non-aliased. */
914 record_component_aliases (tree type
)
916 alias_set_type superset
= get_alias_set (type
);
922 switch (TREE_CODE (type
))
926 case QUAL_UNION_TYPE
:
927 /* Recursively record aliases for the base classes, if there are any. */
928 if (TYPE_BINFO (type
))
931 tree binfo
, base_binfo
;
933 for (binfo
= TYPE_BINFO (type
), i
= 0;
934 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
935 record_alias_subset (superset
,
936 get_alias_set (BINFO_TYPE (base_binfo
)));
938 for (field
= TYPE_FIELDS (type
); field
!= 0; field
= DECL_CHAIN (field
))
939 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_NONADDRESSABLE_P (field
))
940 record_alias_subset (superset
, get_alias_set (TREE_TYPE (field
)));
944 record_alias_subset (superset
, get_alias_set (TREE_TYPE (type
)));
947 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
955 /* Allocate an alias set for use in storing and reading from the varargs
958 static GTY(()) alias_set_type varargs_set
= -1;
961 get_varargs_alias_set (void)
964 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
965 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
966 consistently use the varargs alias set for loads from the varargs
967 area. So don't use it anywhere. */
970 if (varargs_set
== -1)
971 varargs_set
= new_alias_set ();
977 /* Likewise, but used for the fixed portions of the frame, e.g., register
980 static GTY(()) alias_set_type frame_set
= -1;
983 get_frame_alias_set (void)
986 frame_set
= new_alias_set ();
991 /* Create a new, unique base with id ID. */
994 unique_base_value (HOST_WIDE_INT id
)
996 return gen_rtx_ADDRESS (Pmode
, id
);
999 /* Return true if accesses based on any other base value cannot alias
1000 those based on X. */
1003 unique_base_value_p (rtx x
)
1005 return GET_CODE (x
) == ADDRESS
&& GET_MODE (x
) == Pmode
;
1008 /* Return true if X is known to be a base value. */
1011 known_base_value_p (rtx x
)
1013 switch (GET_CODE (x
))
1020 /* Arguments may or may not be bases; we don't know for sure. */
1021 return GET_MODE (x
) != VOIDmode
;
1028 /* Inside SRC, the source of a SET, find a base address. */
1031 find_base_value (rtx src
)
1035 #if defined (FIND_BASE_TERM)
1036 /* Try machine-dependent ways to find the base term. */
1037 src
= FIND_BASE_TERM (src
);
1040 switch (GET_CODE (src
))
1047 regno
= REGNO (src
);
1048 /* At the start of a function, argument registers have known base
1049 values which may be lost later. Returning an ADDRESS
1050 expression here allows optimization based on argument values
1051 even when the argument registers are used for other purposes. */
1052 if (regno
< FIRST_PSEUDO_REGISTER
&& copying_arguments
)
1053 return new_reg_base_value
[regno
];
1055 /* If a pseudo has a known base value, return it. Do not do this
1056 for non-fixed hard regs since it can result in a circular
1057 dependency chain for registers which have values at function entry.
1059 The test above is not sufficient because the scheduler may move
1060 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1061 if ((regno
>= FIRST_PSEUDO_REGISTER
|| fixed_regs
[regno
])
1062 && regno
< vec_safe_length (reg_base_value
))
1064 /* If we're inside init_alias_analysis, use new_reg_base_value
1065 to reduce the number of relaxation iterations. */
1066 if (new_reg_base_value
&& new_reg_base_value
[regno
]
1067 && DF_REG_DEF_COUNT (regno
) == 1)
1068 return new_reg_base_value
[regno
];
1070 if ((*reg_base_value
)[regno
])
1071 return (*reg_base_value
)[regno
];
1077 /* Check for an argument passed in memory. Only record in the
1078 copying-arguments block; it is too hard to track changes
1080 if (copying_arguments
1081 && (XEXP (src
, 0) == arg_pointer_rtx
1082 || (GET_CODE (XEXP (src
, 0)) == PLUS
1083 && XEXP (XEXP (src
, 0), 0) == arg_pointer_rtx
)))
1084 return arg_base_value
;
1088 src
= XEXP (src
, 0);
1089 if (GET_CODE (src
) != PLUS
&& GET_CODE (src
) != MINUS
)
1092 /* ... fall through ... */
1097 rtx temp
, src_0
= XEXP (src
, 0), src_1
= XEXP (src
, 1);
1099 /* If either operand is a REG that is a known pointer, then it
1101 if (REG_P (src_0
) && REG_POINTER (src_0
))
1102 return find_base_value (src_0
);
1103 if (REG_P (src_1
) && REG_POINTER (src_1
))
1104 return find_base_value (src_1
);
1106 /* If either operand is a REG, then see if we already have
1107 a known value for it. */
1110 temp
= find_base_value (src_0
);
1117 temp
= find_base_value (src_1
);
1122 /* If either base is named object or a special address
1123 (like an argument or stack reference), then use it for the
1125 if (src_0
!= 0 && known_base_value_p (src_0
))
1128 if (src_1
!= 0 && known_base_value_p (src_1
))
1131 /* Guess which operand is the base address:
1132 If either operand is a symbol, then it is the base. If
1133 either operand is a CONST_INT, then the other is the base. */
1134 if (CONST_INT_P (src_1
) || CONSTANT_P (src_0
))
1135 return find_base_value (src_0
);
1136 else if (CONST_INT_P (src_0
) || CONSTANT_P (src_1
))
1137 return find_base_value (src_1
);
1143 /* The standard form is (lo_sum reg sym) so look only at the
1145 return find_base_value (XEXP (src
, 1));
1148 /* If the second operand is constant set the base
1149 address to the first operand. */
1150 if (CONST_INT_P (XEXP (src
, 1)) && INTVAL (XEXP (src
, 1)) != 0)
1151 return find_base_value (XEXP (src
, 0));
1155 /* As we do not know which address space the pointer is referring to, we can
1156 handle this only if the target does not support different pointer or
1157 address modes depending on the address space. */
1158 if (!target_default_pointer_address_modes_p ())
1160 if (GET_MODE_SIZE (GET_MODE (src
)) < GET_MODE_SIZE (Pmode
))
1170 return find_base_value (XEXP (src
, 0));
1173 case SIGN_EXTEND
: /* used for NT/Alpha pointers */
1174 /* As we do not know which address space the pointer is referring to, we can
1175 handle this only if the target does not support different pointer or
1176 address modes depending on the address space. */
1177 if (!target_default_pointer_address_modes_p ())
1181 rtx temp
= find_base_value (XEXP (src
, 0));
1183 if (temp
!= 0 && CONSTANT_P (temp
))
1184 temp
= convert_memory_address (Pmode
, temp
);
1196 /* Called from init_alias_analysis indirectly through note_stores,
1197 or directly if DEST is a register with a REG_NOALIAS note attached.
1198 SET is null in the latter case. */
1200 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1201 register N has been set in this function. */
1202 static sbitmap reg_seen
;
1205 record_set (rtx dest
, const_rtx set
, void *data ATTRIBUTE_UNUSED
)
1214 regno
= REGNO (dest
);
1216 gcc_checking_assert (regno
< reg_base_value
->length ());
1218 /* If this spans multiple hard registers, then we must indicate that every
1219 register has an unusable value. */
1220 if (regno
< FIRST_PSEUDO_REGISTER
)
1221 n
= hard_regno_nregs
[regno
][GET_MODE (dest
)];
1228 bitmap_set_bit (reg_seen
, regno
+ n
);
1229 new_reg_base_value
[regno
+ n
] = 0;
1236 /* A CLOBBER wipes out any old value but does not prevent a previously
1237 unset register from acquiring a base address (i.e. reg_seen is not
1239 if (GET_CODE (set
) == CLOBBER
)
1241 new_reg_base_value
[regno
] = 0;
1244 src
= SET_SRC (set
);
1248 /* There's a REG_NOALIAS note against DEST. */
1249 if (bitmap_bit_p (reg_seen
, regno
))
1251 new_reg_base_value
[regno
] = 0;
1254 bitmap_set_bit (reg_seen
, regno
);
1255 new_reg_base_value
[regno
] = unique_base_value (unique_id
++);
1259 /* If this is not the first set of REGNO, see whether the new value
1260 is related to the old one. There are two cases of interest:
1262 (1) The register might be assigned an entirely new value
1263 that has the same base term as the original set.
1265 (2) The set might be a simple self-modification that
1266 cannot change REGNO's base value.
1268 If neither case holds, reject the original base value as invalid.
1269 Note that the following situation is not detected:
1271 extern int x, y; int *p = &x; p += (&y-&x);
1273 ANSI C does not allow computing the difference of addresses
1274 of distinct top level objects. */
1275 if (new_reg_base_value
[regno
] != 0
1276 && find_base_value (src
) != new_reg_base_value
[regno
])
1277 switch (GET_CODE (src
))
1281 if (XEXP (src
, 0) != dest
&& XEXP (src
, 1) != dest
)
1282 new_reg_base_value
[regno
] = 0;
1285 /* If the value we add in the PLUS is also a valid base value,
1286 this might be the actual base value, and the original value
1289 rtx other
= NULL_RTX
;
1291 if (XEXP (src
, 0) == dest
)
1292 other
= XEXP (src
, 1);
1293 else if (XEXP (src
, 1) == dest
)
1294 other
= XEXP (src
, 0);
1296 if (! other
|| find_base_value (other
))
1297 new_reg_base_value
[regno
] = 0;
1301 if (XEXP (src
, 0) != dest
|| !CONST_INT_P (XEXP (src
, 1)))
1302 new_reg_base_value
[regno
] = 0;
1305 new_reg_base_value
[regno
] = 0;
1308 /* If this is the first set of a register, record the value. */
1309 else if ((regno
>= FIRST_PSEUDO_REGISTER
|| ! fixed_regs
[regno
])
1310 && ! bitmap_bit_p (reg_seen
, regno
) && new_reg_base_value
[regno
] == 0)
1311 new_reg_base_value
[regno
] = find_base_value (src
);
1313 bitmap_set_bit (reg_seen
, regno
);
1316 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1317 using hard registers with non-null REG_BASE_VALUE for renaming. */
1319 get_reg_base_value (unsigned int regno
)
1321 return (*reg_base_value
)[regno
];
1324 /* If a value is known for REGNO, return it. */
1327 get_reg_known_value (unsigned int regno
)
1329 if (regno
>= FIRST_PSEUDO_REGISTER
)
1331 regno
-= FIRST_PSEUDO_REGISTER
;
1332 if (regno
< vec_safe_length (reg_known_value
))
1333 return (*reg_known_value
)[regno
];
1341 set_reg_known_value (unsigned int regno
, rtx val
)
1343 if (regno
>= FIRST_PSEUDO_REGISTER
)
1345 regno
-= FIRST_PSEUDO_REGISTER
;
1346 if (regno
< vec_safe_length (reg_known_value
))
1347 (*reg_known_value
)[regno
] = val
;
1351 /* Similarly for reg_known_equiv_p. */
1354 get_reg_known_equiv_p (unsigned int regno
)
1356 if (regno
>= FIRST_PSEUDO_REGISTER
)
1358 regno
-= FIRST_PSEUDO_REGISTER
;
1359 if (regno
< vec_safe_length (reg_known_value
))
1360 return bitmap_bit_p (reg_known_equiv_p
, regno
);
1366 set_reg_known_equiv_p (unsigned int regno
, bool val
)
1368 if (regno
>= FIRST_PSEUDO_REGISTER
)
1370 regno
-= FIRST_PSEUDO_REGISTER
;
1371 if (regno
< vec_safe_length (reg_known_value
))
1374 bitmap_set_bit (reg_known_equiv_p
, regno
);
1376 bitmap_clear_bit (reg_known_equiv_p
, regno
);
1382 /* Returns a canonical version of X, from the point of view alias
1383 analysis. (For example, if X is a MEM whose address is a register,
1384 and the register has a known value (say a SYMBOL_REF), then a MEM
1385 whose address is the SYMBOL_REF is returned.) */
1390 /* Recursively look for equivalences. */
1391 if (REG_P (x
) && REGNO (x
) >= FIRST_PSEUDO_REGISTER
)
1393 rtx t
= get_reg_known_value (REGNO (x
));
1397 return canon_rtx (t
);
1400 if (GET_CODE (x
) == PLUS
)
1402 rtx x0
= canon_rtx (XEXP (x
, 0));
1403 rtx x1
= canon_rtx (XEXP (x
, 1));
1405 if (x0
!= XEXP (x
, 0) || x1
!= XEXP (x
, 1))
1407 if (CONST_INT_P (x0
))
1408 return plus_constant (GET_MODE (x
), x1
, INTVAL (x0
));
1409 else if (CONST_INT_P (x1
))
1410 return plus_constant (GET_MODE (x
), x0
, INTVAL (x1
));
1411 return gen_rtx_PLUS (GET_MODE (x
), x0
, x1
);
1415 /* This gives us much better alias analysis when called from
1416 the loop optimizer. Note we want to leave the original
1417 MEM alone, but need to return the canonicalized MEM with
1418 all the flags with their original values. */
1420 x
= replace_equiv_address_nv (x
, canon_rtx (XEXP (x
, 0)));
1425 /* Return 1 if X and Y are identical-looking rtx's.
1426 Expect that X and Y has been already canonicalized.
1428 We use the data in reg_known_value above to see if two registers with
1429 different numbers are, in fact, equivalent. */
1432 rtx_equal_for_memref_p (const_rtx x
, const_rtx y
)
1439 if (x
== 0 && y
== 0)
1441 if (x
== 0 || y
== 0)
1447 code
= GET_CODE (x
);
1448 /* Rtx's of different codes cannot be equal. */
1449 if (code
!= GET_CODE (y
))
1452 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1453 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1455 if (GET_MODE (x
) != GET_MODE (y
))
1458 /* Some RTL can be compared without a recursive examination. */
1462 return REGNO (x
) == REGNO (y
);
1465 return XEXP (x
, 0) == XEXP (y
, 0);
1468 return XSTR (x
, 0) == XSTR (y
, 0);
1471 /* This is magic, don't go through canonicalization et al. */
1472 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
1476 /* There's no need to compare the contents of CONST_DOUBLEs or
1477 CONST_INTs because pointer equality is a good enough
1478 comparison for these nodes. */
1485 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1487 return ((rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 0))
1488 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 1)))
1489 || (rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 1))
1490 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 0))));
1491 /* For commutative operations, the RTX match if the operand match in any
1492 order. Also handle the simple binary and unary cases without a loop. */
1493 if (COMMUTATIVE_P (x
))
1495 rtx xop0
= canon_rtx (XEXP (x
, 0));
1496 rtx yop0
= canon_rtx (XEXP (y
, 0));
1497 rtx yop1
= canon_rtx (XEXP (y
, 1));
1499 return ((rtx_equal_for_memref_p (xop0
, yop0
)
1500 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop1
))
1501 || (rtx_equal_for_memref_p (xop0
, yop1
)
1502 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop0
)));
1504 else if (NON_COMMUTATIVE_P (x
))
1506 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1507 canon_rtx (XEXP (y
, 0)))
1508 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)),
1509 canon_rtx (XEXP (y
, 1))));
1511 else if (UNARY_P (x
))
1512 return rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1513 canon_rtx (XEXP (y
, 0)));
1515 /* Compare the elements. If any pair of corresponding elements
1516 fail to match, return 0 for the whole things.
1518 Limit cases to types which actually appear in addresses. */
1520 fmt
= GET_RTX_FORMAT (code
);
1521 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1526 if (XINT (x
, i
) != XINT (y
, i
))
1531 /* Two vectors must have the same length. */
1532 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1535 /* And the corresponding elements must match. */
1536 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1537 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x
, i
, j
)),
1538 canon_rtx (XVECEXP (y
, i
, j
))) == 0)
1543 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, i
)),
1544 canon_rtx (XEXP (y
, i
))) == 0)
1548 /* This can happen for asm operands. */
1550 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1554 /* This can happen for an asm which clobbers memory. */
1558 /* It is believed that rtx's at this level will never
1559 contain anything but integers and other rtx's,
1560 except for within LABEL_REFs and SYMBOL_REFs. */
1569 find_base_term (rtx x
)
1572 struct elt_loc_list
*l
, *f
;
1575 #if defined (FIND_BASE_TERM)
1576 /* Try machine-dependent ways to find the base term. */
1577 x
= FIND_BASE_TERM (x
);
1580 switch (GET_CODE (x
))
1583 return REG_BASE_VALUE (x
);
1586 /* As we do not know which address space the pointer is referring to, we can
1587 handle this only if the target does not support different pointer or
1588 address modes depending on the address space. */
1589 if (!target_default_pointer_address_modes_p ())
1591 if (GET_MODE_SIZE (GET_MODE (x
)) < GET_MODE_SIZE (Pmode
))
1601 return find_base_term (XEXP (x
, 0));
1604 case SIGN_EXTEND
: /* Used for Alpha/NT pointers */
1605 /* As we do not know which address space the pointer is referring to, we can
1606 handle this only if the target does not support different pointer or
1607 address modes depending on the address space. */
1608 if (!target_default_pointer_address_modes_p ())
1612 rtx temp
= find_base_term (XEXP (x
, 0));
1614 if (temp
!= 0 && CONSTANT_P (temp
))
1615 temp
= convert_memory_address (Pmode
, temp
);
1621 val
= CSELIB_VAL_PTR (x
);
1627 if (cselib_sp_based_value_p (val
))
1628 return static_reg_base_value
[STACK_POINTER_REGNUM
];
1631 /* Temporarily reset val->locs to avoid infinite recursion. */
1634 for (l
= f
; l
; l
= l
->next
)
1635 if (GET_CODE (l
->loc
) == VALUE
1636 && CSELIB_VAL_PTR (l
->loc
)->locs
1637 && !CSELIB_VAL_PTR (l
->loc
)->locs
->next
1638 && CSELIB_VAL_PTR (l
->loc
)->locs
->loc
== x
)
1640 else if ((ret
= find_base_term (l
->loc
)) != 0)
1647 /* The standard form is (lo_sum reg sym) so look only at the
1649 return find_base_term (XEXP (x
, 1));
1653 if (GET_CODE (x
) != PLUS
&& GET_CODE (x
) != MINUS
)
1659 rtx tmp1
= XEXP (x
, 0);
1660 rtx tmp2
= XEXP (x
, 1);
1662 /* This is a little bit tricky since we have to determine which of
1663 the two operands represents the real base address. Otherwise this
1664 routine may return the index register instead of the base register.
1666 That may cause us to believe no aliasing was possible, when in
1667 fact aliasing is possible.
1669 We use a few simple tests to guess the base register. Additional
1670 tests can certainly be added. For example, if one of the operands
1671 is a shift or multiply, then it must be the index register and the
1672 other operand is the base register. */
1674 if (tmp1
== pic_offset_table_rtx
&& CONSTANT_P (tmp2
))
1675 return find_base_term (tmp2
);
1677 /* If either operand is known to be a pointer, then prefer it
1678 to determine the base term. */
1679 if (REG_P (tmp1
) && REG_POINTER (tmp1
))
1681 else if (REG_P (tmp2
) && REG_POINTER (tmp2
))
1688 /* Go ahead and find the base term for both operands. If either base
1689 term is from a pointer or is a named object or a special address
1690 (like an argument or stack reference), then use it for the
1692 rtx base
= find_base_term (tmp1
);
1693 if (base
!= NULL_RTX
1694 && ((REG_P (tmp1
) && REG_POINTER (tmp1
))
1695 || known_base_value_p (base
)))
1697 base
= find_base_term (tmp2
);
1698 if (base
!= NULL_RTX
1699 && ((REG_P (tmp2
) && REG_POINTER (tmp2
))
1700 || known_base_value_p (base
)))
1703 /* We could not determine which of the two operands was the
1704 base register and which was the index. So we can determine
1705 nothing from the base alias check. */
1710 if (CONST_INT_P (XEXP (x
, 1)) && INTVAL (XEXP (x
, 1)) != 0)
1711 return find_base_term (XEXP (x
, 0));
1723 /* Return true if accesses to address X may alias accesses based
1724 on the stack pointer. */
1727 may_be_sp_based_p (rtx x
)
1729 rtx base
= find_base_term (x
);
1730 return !base
|| base
== static_reg_base_value
[STACK_POINTER_REGNUM
];
1733 /* Return 0 if the addresses X and Y are known to point to different
1734 objects, 1 if they might be pointers to the same object. */
1737 base_alias_check (rtx x
, rtx x_base
, rtx y
, rtx y_base
,
1738 enum machine_mode x_mode
, enum machine_mode y_mode
)
1740 /* If the address itself has no known base see if a known equivalent
1741 value has one. If either address still has no known base, nothing
1742 is known about aliasing. */
1747 if (! flag_expensive_optimizations
|| (x_c
= canon_rtx (x
)) == x
)
1750 x_base
= find_base_term (x_c
);
1758 if (! flag_expensive_optimizations
|| (y_c
= canon_rtx (y
)) == y
)
1761 y_base
= find_base_term (y_c
);
1766 /* If the base addresses are equal nothing is known about aliasing. */
1767 if (rtx_equal_p (x_base
, y_base
))
1770 /* The base addresses are different expressions. If they are not accessed
1771 via AND, there is no conflict. We can bring knowledge of object
1772 alignment into play here. For example, on alpha, "char a, b;" can
1773 alias one another, though "char a; long b;" cannot. AND addesses may
1774 implicitly alias surrounding objects; i.e. unaligned access in DImode
1775 via AND address can alias all surrounding object types except those
1776 with aligment 8 or higher. */
1777 if (GET_CODE (x
) == AND
&& GET_CODE (y
) == AND
)
1779 if (GET_CODE (x
) == AND
1780 && (!CONST_INT_P (XEXP (x
, 1))
1781 || (int) GET_MODE_UNIT_SIZE (y_mode
) < -INTVAL (XEXP (x
, 1))))
1783 if (GET_CODE (y
) == AND
1784 && (!CONST_INT_P (XEXP (y
, 1))
1785 || (int) GET_MODE_UNIT_SIZE (x_mode
) < -INTVAL (XEXP (y
, 1))))
1788 /* Differing symbols not accessed via AND never alias. */
1789 if (GET_CODE (x_base
) != ADDRESS
&& GET_CODE (y_base
) != ADDRESS
)
1792 if (unique_base_value_p (x_base
) || unique_base_value_p (y_base
))
1798 /* Callback for for_each_rtx, that returns 1 upon encountering a VALUE
1799 whose UID is greater than the int uid that D points to. */
1802 refs_newer_value_cb (rtx
*x
, void *d
)
1804 if (GET_CODE (*x
) == VALUE
&& CSELIB_VAL_PTR (*x
)->uid
> *(int *)d
)
1810 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1814 refs_newer_value_p (rtx expr
, rtx v
)
1816 int minuid
= CSELIB_VAL_PTR (v
)->uid
;
1818 return for_each_rtx (&expr
, refs_newer_value_cb
, &minuid
);
1821 /* Convert the address X into something we can use. This is done by returning
1822 it unchanged unless it is a value; in the latter case we call cselib to get
1823 a more useful rtx. */
1829 struct elt_loc_list
*l
;
1831 if (GET_CODE (x
) != VALUE
)
1833 v
= CSELIB_VAL_PTR (x
);
1836 bool have_equivs
= cselib_have_permanent_equivalences ();
1838 v
= canonical_cselib_val (v
);
1839 for (l
= v
->locs
; l
; l
= l
->next
)
1840 if (CONSTANT_P (l
->loc
))
1842 for (l
= v
->locs
; l
; l
= l
->next
)
1843 if (!REG_P (l
->loc
) && !MEM_P (l
->loc
)
1844 /* Avoid infinite recursion when potentially dealing with
1845 var-tracking artificial equivalences, by skipping the
1846 equivalences themselves, and not choosing expressions
1847 that refer to newer VALUEs. */
1849 || (GET_CODE (l
->loc
) != VALUE
1850 && !refs_newer_value_p (l
->loc
, x
))))
1854 for (l
= v
->locs
; l
; l
= l
->next
)
1856 || (GET_CODE (l
->loc
) != VALUE
1857 && !refs_newer_value_p (l
->loc
, x
)))
1859 /* Return the canonical value. */
1863 return v
->locs
->loc
;
1868 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1869 where SIZE is the size in bytes of the memory reference. If ADDR
1870 is not modified by the memory reference then ADDR is returned. */
1873 addr_side_effect_eval (rtx addr
, int size
, int n_refs
)
1877 switch (GET_CODE (addr
))
1880 offset
= (n_refs
+ 1) * size
;
1883 offset
= -(n_refs
+ 1) * size
;
1886 offset
= n_refs
* size
;
1889 offset
= -n_refs
* size
;
1897 addr
= gen_rtx_PLUS (GET_MODE (addr
), XEXP (addr
, 0),
1900 addr
= XEXP (addr
, 0);
1901 addr
= canon_rtx (addr
);
1906 /* Return TRUE if an object X sized at XSIZE bytes and another object
1907 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1908 any of the sizes is zero, assume an overlap, otherwise use the
1909 absolute value of the sizes as the actual sizes. */
1912 offset_overlap_p (HOST_WIDE_INT c
, int xsize
, int ysize
)
1914 return (xsize
== 0 || ysize
== 0
1917 : (abs (ysize
) > -c
)));
1920 /* Return one if X and Y (memory addresses) reference the
1921 same location in memory or if the references overlap.
1922 Return zero if they do not overlap, else return
1923 minus one in which case they still might reference the same location.
1925 C is an offset accumulator. When
1926 C is nonzero, we are testing aliases between X and Y + C.
1927 XSIZE is the size in bytes of the X reference,
1928 similarly YSIZE is the size in bytes for Y.
1929 Expect that canon_rtx has been already called for X and Y.
1931 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1932 referenced (the reference was BLKmode), so make the most pessimistic
1935 If XSIZE or YSIZE is negative, we may access memory outside the object
1936 being referenced as a side effect. This can happen when using AND to
1937 align memory references, as is done on the Alpha.
1939 Nice to notice that varying addresses cannot conflict with fp if no
1940 local variables had their addresses taken, but that's too hard now.
1942 ??? Contrary to the tree alias oracle this does not return
1943 one for X + non-constant and Y + non-constant when X and Y are equal.
1944 If that is fixed the TBAA hack for union type-punning can be removed. */
1947 memrefs_conflict_p (int xsize
, rtx x
, int ysize
, rtx y
, HOST_WIDE_INT c
)
1949 if (GET_CODE (x
) == VALUE
)
1953 struct elt_loc_list
*l
= NULL
;
1954 if (CSELIB_VAL_PTR (x
))
1955 for (l
= canonical_cselib_val (CSELIB_VAL_PTR (x
))->locs
;
1957 if (REG_P (l
->loc
) && rtx_equal_for_memref_p (l
->loc
, y
))
1964 /* Don't call get_addr if y is the same VALUE. */
1968 if (GET_CODE (y
) == VALUE
)
1972 struct elt_loc_list
*l
= NULL
;
1973 if (CSELIB_VAL_PTR (y
))
1974 for (l
= canonical_cselib_val (CSELIB_VAL_PTR (y
))->locs
;
1976 if (REG_P (l
->loc
) && rtx_equal_for_memref_p (l
->loc
, x
))
1983 /* Don't call get_addr if x is the same VALUE. */
1987 if (GET_CODE (x
) == HIGH
)
1989 else if (GET_CODE (x
) == LO_SUM
)
1992 x
= addr_side_effect_eval (x
, abs (xsize
), 0);
1993 if (GET_CODE (y
) == HIGH
)
1995 else if (GET_CODE (y
) == LO_SUM
)
1998 y
= addr_side_effect_eval (y
, abs (ysize
), 0);
2000 if (rtx_equal_for_memref_p (x
, y
))
2002 return offset_overlap_p (c
, xsize
, ysize
);
2005 /* This code used to check for conflicts involving stack references and
2006 globals but the base address alias code now handles these cases. */
2008 if (GET_CODE (x
) == PLUS
)
2010 /* The fact that X is canonicalized means that this
2011 PLUS rtx is canonicalized. */
2012 rtx x0
= XEXP (x
, 0);
2013 rtx x1
= XEXP (x
, 1);
2015 if (GET_CODE (y
) == PLUS
)
2017 /* The fact that Y is canonicalized means that this
2018 PLUS rtx is canonicalized. */
2019 rtx y0
= XEXP (y
, 0);
2020 rtx y1
= XEXP (y
, 1);
2022 if (rtx_equal_for_memref_p (x1
, y1
))
2023 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
2024 if (rtx_equal_for_memref_p (x0
, y0
))
2025 return memrefs_conflict_p (xsize
, x1
, ysize
, y1
, c
);
2026 if (CONST_INT_P (x1
))
2028 if (CONST_INT_P (y1
))
2029 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
,
2030 c
- INTVAL (x1
) + INTVAL (y1
));
2032 return memrefs_conflict_p (xsize
, x0
, ysize
, y
,
2035 else if (CONST_INT_P (y1
))
2036 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
2040 else if (CONST_INT_P (x1
))
2041 return memrefs_conflict_p (xsize
, x0
, ysize
, y
, c
- INTVAL (x1
));
2043 else if (GET_CODE (y
) == PLUS
)
2045 /* The fact that Y is canonicalized means that this
2046 PLUS rtx is canonicalized. */
2047 rtx y0
= XEXP (y
, 0);
2048 rtx y1
= XEXP (y
, 1);
2050 if (CONST_INT_P (y1
))
2051 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
2056 if (GET_CODE (x
) == GET_CODE (y
))
2057 switch (GET_CODE (x
))
2061 /* Handle cases where we expect the second operands to be the
2062 same, and check only whether the first operand would conflict
2065 rtx x1
= canon_rtx (XEXP (x
, 1));
2066 rtx y1
= canon_rtx (XEXP (y
, 1));
2067 if (! rtx_equal_for_memref_p (x1
, y1
))
2069 x0
= canon_rtx (XEXP (x
, 0));
2070 y0
= canon_rtx (XEXP (y
, 0));
2071 if (rtx_equal_for_memref_p (x0
, y0
))
2072 return offset_overlap_p (c
, xsize
, ysize
);
2074 /* Can't properly adjust our sizes. */
2075 if (!CONST_INT_P (x1
))
2077 xsize
/= INTVAL (x1
);
2078 ysize
/= INTVAL (x1
);
2080 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
2087 /* Deal with alignment ANDs by adjusting offset and size so as to
2088 cover the maximum range, without taking any previously known
2089 alignment into account. Make a size negative after such an
2090 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2091 assume a potential overlap, because they may end up in contiguous
2092 memory locations and the stricter-alignment access may span over
2094 if (GET_CODE (x
) == AND
&& CONST_INT_P (XEXP (x
, 1)))
2096 HOST_WIDE_INT sc
= INTVAL (XEXP (x
, 1));
2097 unsigned HOST_WIDE_INT uc
= sc
;
2098 if (sc
< 0 && -uc
== (uc
& -uc
))
2105 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2109 if (GET_CODE (y
) == AND
&& CONST_INT_P (XEXP (y
, 1)))
2111 HOST_WIDE_INT sc
= INTVAL (XEXP (y
, 1));
2112 unsigned HOST_WIDE_INT uc
= sc
;
2113 if (sc
< 0 && -uc
== (uc
& -uc
))
2120 return memrefs_conflict_p (xsize
, x
,
2121 ysize
, canon_rtx (XEXP (y
, 0)), c
);
2127 if (CONST_INT_P (x
) && CONST_INT_P (y
))
2129 c
+= (INTVAL (y
) - INTVAL (x
));
2130 return offset_overlap_p (c
, xsize
, ysize
);
2133 if (GET_CODE (x
) == CONST
)
2135 if (GET_CODE (y
) == CONST
)
2136 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2137 ysize
, canon_rtx (XEXP (y
, 0)), c
);
2139 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2142 if (GET_CODE (y
) == CONST
)
2143 return memrefs_conflict_p (xsize
, x
, ysize
,
2144 canon_rtx (XEXP (y
, 0)), c
);
2146 /* Assume a potential overlap for symbolic addresses that went
2147 through alignment adjustments (i.e., that have negative
2148 sizes), because we can't know how far they are from each
2151 return (xsize
< 0 || ysize
< 0 || offset_overlap_p (c
, xsize
, ysize
));
2159 /* Functions to compute memory dependencies.
2161 Since we process the insns in execution order, we can build tables
2162 to keep track of what registers are fixed (and not aliased), what registers
2163 are varying in known ways, and what registers are varying in unknown
2166 If both memory references are volatile, then there must always be a
2167 dependence between the two references, since their order can not be
2168 changed. A volatile and non-volatile reference can be interchanged
2171 We also must allow AND addresses, because they may generate accesses
2172 outside the object being referenced. This is used to generate aligned
2173 addresses from unaligned addresses, for instance, the alpha
2174 storeqi_unaligned pattern. */
2176 /* Read dependence: X is read after read in MEM takes place. There can
2177 only be a dependence here if both reads are volatile, or if either is
2178 an explicit barrier. */
2181 read_dependence (const_rtx mem
, const_rtx x
)
2183 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2185 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2186 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2191 /* Return true if we can determine that the fields referenced cannot
2192 overlap for any pair of objects. */
2195 nonoverlapping_component_refs_p (const_rtx rtlx
, const_rtx rtly
)
2197 const_tree x
= MEM_EXPR (rtlx
), y
= MEM_EXPR (rtly
);
2198 const_tree fieldx
, fieldy
, typex
, typey
, orig_y
;
2200 if (!flag_strict_aliasing
2202 || TREE_CODE (x
) != COMPONENT_REF
2203 || TREE_CODE (y
) != COMPONENT_REF
)
2208 /* The comparison has to be done at a common type, since we don't
2209 know how the inheritance hierarchy works. */
2213 fieldx
= TREE_OPERAND (x
, 1);
2214 typex
= TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx
));
2219 fieldy
= TREE_OPERAND (y
, 1);
2220 typey
= TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy
));
2225 y
= TREE_OPERAND (y
, 0);
2227 while (y
&& TREE_CODE (y
) == COMPONENT_REF
);
2229 x
= TREE_OPERAND (x
, 0);
2231 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2232 /* Never found a common type. */
2236 /* If we're left with accessing different fields of a structure, then no
2237 possible overlap, unless they are both bitfields. */
2238 if (TREE_CODE (typex
) == RECORD_TYPE
&& fieldx
!= fieldy
)
2239 return !(DECL_BIT_FIELD (fieldx
) && DECL_BIT_FIELD (fieldy
));
2241 /* The comparison on the current field failed. If we're accessing
2242 a very nested structure, look at the next outer level. */
2243 x
= TREE_OPERAND (x
, 0);
2244 y
= TREE_OPERAND (y
, 0);
2247 && TREE_CODE (x
) == COMPONENT_REF
2248 && TREE_CODE (y
) == COMPONENT_REF
);
2253 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2256 decl_for_component_ref (tree x
)
2260 x
= TREE_OPERAND (x
, 0);
2262 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2264 return x
&& DECL_P (x
) ? x
: NULL_TREE
;
2267 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2268 for the offset of the field reference. *KNOWN_P says whether the
2272 adjust_offset_for_component_ref (tree x
, bool *known_p
,
2273 HOST_WIDE_INT
*offset
)
2279 tree xoffset
= component_ref_field_offset (x
);
2280 tree field
= TREE_OPERAND (x
, 1);
2282 if (! host_integerp (xoffset
, 1))
2287 *offset
+= (tree_low_cst (xoffset
, 1)
2288 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
2291 x
= TREE_OPERAND (x
, 0);
2293 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2296 /* Return nonzero if we can determine the exprs corresponding to memrefs
2297 X and Y and they do not overlap.
2298 If LOOP_VARIANT is set, skip offset-based disambiguation */
2301 nonoverlapping_memrefs_p (const_rtx x
, const_rtx y
, bool loop_invariant
)
2303 tree exprx
= MEM_EXPR (x
), expry
= MEM_EXPR (y
);
2306 bool moffsetx_known_p
, moffsety_known_p
;
2307 HOST_WIDE_INT moffsetx
= 0, moffsety
= 0;
2308 HOST_WIDE_INT offsetx
= 0, offsety
= 0, sizex
, sizey
, tem
;
2310 /* Unless both have exprs, we can't tell anything. */
2311 if (exprx
== 0 || expry
== 0)
2314 /* For spill-slot accesses make sure we have valid offsets. */
2315 if ((exprx
== get_spill_slot_decl (false)
2316 && ! MEM_OFFSET_KNOWN_P (x
))
2317 || (expry
== get_spill_slot_decl (false)
2318 && ! MEM_OFFSET_KNOWN_P (y
)))
2321 /* If the field reference test failed, look at the DECLs involved. */
2322 moffsetx_known_p
= MEM_OFFSET_KNOWN_P (x
);
2323 if (moffsetx_known_p
)
2324 moffsetx
= MEM_OFFSET (x
);
2325 if (TREE_CODE (exprx
) == COMPONENT_REF
)
2327 tree t
= decl_for_component_ref (exprx
);
2330 adjust_offset_for_component_ref (exprx
, &moffsetx_known_p
, &moffsetx
);
2334 moffsety_known_p
= MEM_OFFSET_KNOWN_P (y
);
2335 if (moffsety_known_p
)
2336 moffsety
= MEM_OFFSET (y
);
2337 if (TREE_CODE (expry
) == COMPONENT_REF
)
2339 tree t
= decl_for_component_ref (expry
);
2342 adjust_offset_for_component_ref (expry
, &moffsety_known_p
, &moffsety
);
2346 if (! DECL_P (exprx
) || ! DECL_P (expry
))
2349 /* With invalid code we can end up storing into the constant pool.
2350 Bail out to avoid ICEing when creating RTL for this.
2351 See gfortran.dg/lto/20091028-2_0.f90. */
2352 if (TREE_CODE (exprx
) == CONST_DECL
2353 || TREE_CODE (expry
) == CONST_DECL
)
2356 rtlx
= DECL_RTL (exprx
);
2357 rtly
= DECL_RTL (expry
);
2359 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2360 can't overlap unless they are the same because we never reuse that part
2361 of the stack frame used for locals for spilled pseudos. */
2362 if ((!MEM_P (rtlx
) || !MEM_P (rtly
))
2363 && ! rtx_equal_p (rtlx
, rtly
))
2366 /* If we have MEMs referring to different address spaces (which can
2367 potentially overlap), we cannot easily tell from the addresses
2368 whether the references overlap. */
2369 if (MEM_P (rtlx
) && MEM_P (rtly
)
2370 && MEM_ADDR_SPACE (rtlx
) != MEM_ADDR_SPACE (rtly
))
2373 /* Get the base and offsets of both decls. If either is a register, we
2374 know both are and are the same, so use that as the base. The only
2375 we can avoid overlap is if we can deduce that they are nonoverlapping
2376 pieces of that decl, which is very rare. */
2377 basex
= MEM_P (rtlx
) ? XEXP (rtlx
, 0) : rtlx
;
2378 if (GET_CODE (basex
) == PLUS
&& CONST_INT_P (XEXP (basex
, 1)))
2379 offsetx
= INTVAL (XEXP (basex
, 1)), basex
= XEXP (basex
, 0);
2381 basey
= MEM_P (rtly
) ? XEXP (rtly
, 0) : rtly
;
2382 if (GET_CODE (basey
) == PLUS
&& CONST_INT_P (XEXP (basey
, 1)))
2383 offsety
= INTVAL (XEXP (basey
, 1)), basey
= XEXP (basey
, 0);
2385 /* If the bases are different, we know they do not overlap if both
2386 are constants or if one is a constant and the other a pointer into the
2387 stack frame. Otherwise a different base means we can't tell if they
2389 if (! rtx_equal_p (basex
, basey
))
2390 return ((CONSTANT_P (basex
) && CONSTANT_P (basey
))
2391 || (CONSTANT_P (basex
) && REG_P (basey
)
2392 && REGNO_PTR_FRAME_P (REGNO (basey
)))
2393 || (CONSTANT_P (basey
) && REG_P (basex
)
2394 && REGNO_PTR_FRAME_P (REGNO (basex
))));
2396 /* Offset based disambiguation not appropriate for loop invariant */
2400 sizex
= (!MEM_P (rtlx
) ? (int) GET_MODE_SIZE (GET_MODE (rtlx
))
2401 : MEM_SIZE_KNOWN_P (rtlx
) ? MEM_SIZE (rtlx
)
2403 sizey
= (!MEM_P (rtly
) ? (int) GET_MODE_SIZE (GET_MODE (rtly
))
2404 : MEM_SIZE_KNOWN_P (rtly
) ? MEM_SIZE (rtly
)
2407 /* If we have an offset for either memref, it can update the values computed
2409 if (moffsetx_known_p
)
2410 offsetx
+= moffsetx
, sizex
-= moffsetx
;
2411 if (moffsety_known_p
)
2412 offsety
+= moffsety
, sizey
-= moffsety
;
2414 /* If a memref has both a size and an offset, we can use the smaller size.
2415 We can't do this if the offset isn't known because we must view this
2416 memref as being anywhere inside the DECL's MEM. */
2417 if (MEM_SIZE_KNOWN_P (x
) && moffsetx_known_p
)
2418 sizex
= MEM_SIZE (x
);
2419 if (MEM_SIZE_KNOWN_P (y
) && moffsety_known_p
)
2420 sizey
= MEM_SIZE (y
);
2422 /* Put the values of the memref with the lower offset in X's values. */
2423 if (offsetx
> offsety
)
2425 tem
= offsetx
, offsetx
= offsety
, offsety
= tem
;
2426 tem
= sizex
, sizex
= sizey
, sizey
= tem
;
2429 /* If we don't know the size of the lower-offset value, we can't tell
2430 if they conflict. Otherwise, we do the test. */
2431 return sizex
>= 0 && offsety
>= offsetx
+ sizex
;
2434 /* Helper for true_dependence and canon_true_dependence.
2435 Checks for true dependence: X is read after store in MEM takes place.
2437 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2438 NULL_RTX, and the canonical addresses of MEM and X are both computed
2439 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2441 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2443 Returns 1 if there is a true dependence, 0 otherwise. */
2446 true_dependence_1 (const_rtx mem
, enum machine_mode mem_mode
, rtx mem_addr
,
2447 const_rtx x
, rtx x_addr
, bool mem_canonicalized
)
2452 gcc_checking_assert (mem_canonicalized
? (mem_addr
!= NULL_RTX
)
2453 : (mem_addr
== NULL_RTX
&& x_addr
== NULL_RTX
));
2455 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2458 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2459 This is used in epilogue deallocation functions, and in cselib. */
2460 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2462 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2464 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2465 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2468 /* Read-only memory is by definition never modified, and therefore can't
2469 conflict with anything. We don't expect to find read-only set on MEM,
2470 but stupid user tricks can produce them, so don't die. */
2471 if (MEM_READONLY_P (x
))
2474 /* If we have MEMs referring to different address spaces (which can
2475 potentially overlap), we cannot easily tell from the addresses
2476 whether the references overlap. */
2477 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2482 mem_addr
= XEXP (mem
, 0);
2483 if (mem_mode
== VOIDmode
)
2484 mem_mode
= GET_MODE (mem
);
2489 x_addr
= XEXP (x
, 0);
2490 if (!((GET_CODE (x_addr
) == VALUE
2491 && GET_CODE (mem_addr
) != VALUE
2492 && reg_mentioned_p (x_addr
, mem_addr
))
2493 || (GET_CODE (x_addr
) != VALUE
2494 && GET_CODE (mem_addr
) == VALUE
2495 && reg_mentioned_p (mem_addr
, x_addr
))))
2497 x_addr
= get_addr (x_addr
);
2498 if (! mem_canonicalized
)
2499 mem_addr
= get_addr (mem_addr
);
2503 base
= find_base_term (x_addr
);
2504 if (base
&& (GET_CODE (base
) == LABEL_REF
2505 || (GET_CODE (base
) == SYMBOL_REF
2506 && CONSTANT_POOL_ADDRESS_P (base
))))
2509 rtx mem_base
= find_base_term (mem_addr
);
2510 if (! base_alias_check (x_addr
, base
, mem_addr
, mem_base
,
2511 GET_MODE (x
), mem_mode
))
2514 x_addr
= canon_rtx (x_addr
);
2515 if (!mem_canonicalized
)
2516 mem_addr
= canon_rtx (mem_addr
);
2518 if ((ret
= memrefs_conflict_p (GET_MODE_SIZE (mem_mode
), mem_addr
,
2519 SIZE_FOR_MODE (x
), x_addr
, 0)) != -1)
2522 if (mems_in_disjoint_alias_sets_p (x
, mem
))
2525 if (nonoverlapping_memrefs_p (mem
, x
, false))
2528 if (nonoverlapping_component_refs_p (mem
, x
))
2531 return rtx_refs_may_alias_p (x
, mem
, true);
2534 /* True dependence: X is read after store in MEM takes place. */
2537 true_dependence (const_rtx mem
, enum machine_mode mem_mode
, const_rtx x
)
2539 return true_dependence_1 (mem
, mem_mode
, NULL_RTX
,
2540 x
, NULL_RTX
, /*mem_canonicalized=*/false);
2543 /* Canonical true dependence: X is read after store in MEM takes place.
2544 Variant of true_dependence which assumes MEM has already been
2545 canonicalized (hence we no longer do that here).
2546 The mem_addr argument has been added, since true_dependence_1 computed
2547 this value prior to canonicalizing. */
2550 canon_true_dependence (const_rtx mem
, enum machine_mode mem_mode
, rtx mem_addr
,
2551 const_rtx x
, rtx x_addr
)
2553 return true_dependence_1 (mem
, mem_mode
, mem_addr
,
2554 x
, x_addr
, /*mem_canonicalized=*/true);
2557 /* Returns nonzero if a write to X might alias a previous read from
2558 (or, if WRITEP is true, a write to) MEM.
2559 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2560 and X_MODE the mode for that access.
2561 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2564 write_dependence_p (const_rtx mem
,
2565 const_rtx x
, enum machine_mode x_mode
, rtx x_addr
,
2566 bool mem_canonicalized
, bool x_canonicalized
, bool writep
)
2572 gcc_checking_assert (x_canonicalized
2573 ? (x_addr
!= NULL_RTX
&& x_mode
!= VOIDmode
)
2574 : (x_addr
== NULL_RTX
&& x_mode
== VOIDmode
));
2576 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2579 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2580 This is used in epilogue deallocation functions. */
2581 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2583 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2585 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2586 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2589 /* A read from read-only memory can't conflict with read-write memory. */
2590 if (!writep
&& MEM_READONLY_P (mem
))
2593 /* If we have MEMs referring to different address spaces (which can
2594 potentially overlap), we cannot easily tell from the addresses
2595 whether the references overlap. */
2596 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2599 mem_addr
= XEXP (mem
, 0);
2602 x_addr
= XEXP (x
, 0);
2603 if (!((GET_CODE (x_addr
) == VALUE
2604 && GET_CODE (mem_addr
) != VALUE
2605 && reg_mentioned_p (x_addr
, mem_addr
))
2606 || (GET_CODE (x_addr
) != VALUE
2607 && GET_CODE (mem_addr
) == VALUE
2608 && reg_mentioned_p (mem_addr
, x_addr
))))
2610 x_addr
= get_addr (x_addr
);
2611 if (!mem_canonicalized
)
2612 mem_addr
= get_addr (mem_addr
);
2616 base
= find_base_term (mem_addr
);
2619 && (GET_CODE (base
) == LABEL_REF
2620 || (GET_CODE (base
) == SYMBOL_REF
2621 && CONSTANT_POOL_ADDRESS_P (base
))))
2624 rtx x_base
= find_base_term (x_addr
);
2625 if (! base_alias_check (x_addr
, x_base
, mem_addr
, base
, GET_MODE (x
),
2629 if (!x_canonicalized
)
2631 x_addr
= canon_rtx (x_addr
);
2632 x_mode
= GET_MODE (x
);
2634 if (!mem_canonicalized
)
2635 mem_addr
= canon_rtx (mem_addr
);
2637 if ((ret
= memrefs_conflict_p (SIZE_FOR_MODE (mem
), mem_addr
,
2638 GET_MODE_SIZE (x_mode
), x_addr
, 0)) != -1)
2641 if (nonoverlapping_memrefs_p (x
, mem
, false))
2644 return rtx_refs_may_alias_p (x
, mem
, false);
2647 /* Anti dependence: X is written after read in MEM takes place. */
2650 anti_dependence (const_rtx mem
, const_rtx x
)
2652 return write_dependence_p (mem
, x
, VOIDmode
, NULL_RTX
,
2653 /*mem_canonicalized=*/false,
2654 /*x_canonicalized*/false, /*writep=*/false);
2657 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2658 Also, consider X in X_MODE (which might be from an enclosing
2659 STRICT_LOW_PART / ZERO_EXTRACT).
2660 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2663 canon_anti_dependence (const_rtx mem
, bool mem_canonicalized
,
2664 const_rtx x
, enum machine_mode x_mode
, rtx x_addr
)
2666 return write_dependence_p (mem
, x
, x_mode
, x_addr
,
2667 mem_canonicalized
, /*x_canonicalized=*/true,
2671 /* Output dependence: X is written after store in MEM takes place. */
2674 output_dependence (const_rtx mem
, const_rtx x
)
2676 return write_dependence_p (mem
, x
, VOIDmode
, NULL_RTX
,
2677 /*mem_canonicalized=*/false,
2678 /*x_canonicalized*/false, /*writep=*/true);
2683 /* Check whether X may be aliased with MEM. Don't do offset-based
2684 memory disambiguation & TBAA. */
2686 may_alias_p (const_rtx mem
, const_rtx x
)
2688 rtx x_addr
, mem_addr
;
2690 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2693 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2694 This is used in epilogue deallocation functions. */
2695 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2697 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2699 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2700 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2703 /* Read-only memory is by definition never modified, and therefore can't
2704 conflict with anything. We don't expect to find read-only set on MEM,
2705 but stupid user tricks can produce them, so don't die. */
2706 if (MEM_READONLY_P (x
))
2709 /* If we have MEMs referring to different address spaces (which can
2710 potentially overlap), we cannot easily tell from the addresses
2711 whether the references overlap. */
2712 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2715 x_addr
= XEXP (x
, 0);
2716 mem_addr
= XEXP (mem
, 0);
2717 if (!((GET_CODE (x_addr
) == VALUE
2718 && GET_CODE (mem_addr
) != VALUE
2719 && reg_mentioned_p (x_addr
, mem_addr
))
2720 || (GET_CODE (x_addr
) != VALUE
2721 && GET_CODE (mem_addr
) == VALUE
2722 && reg_mentioned_p (mem_addr
, x_addr
))))
2724 x_addr
= get_addr (x_addr
);
2725 mem_addr
= get_addr (mem_addr
);
2728 rtx x_base
= find_base_term (x_addr
);
2729 rtx mem_base
= find_base_term (mem_addr
);
2730 if (! base_alias_check (x_addr
, x_base
, mem_addr
, mem_base
,
2731 GET_MODE (x
), GET_MODE (mem_addr
)))
2734 x_addr
= canon_rtx (x_addr
);
2735 mem_addr
= canon_rtx (mem_addr
);
2737 if (nonoverlapping_memrefs_p (mem
, x
, true))
2740 /* TBAA not valid for loop_invarint */
2741 return rtx_refs_may_alias_p (x
, mem
, false);
2745 init_alias_target (void)
2749 if (!arg_base_value
)
2750 arg_base_value
= gen_rtx_ADDRESS (VOIDmode
, 0);
2752 memset (static_reg_base_value
, 0, sizeof static_reg_base_value
);
2754 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2755 /* Check whether this register can hold an incoming pointer
2756 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2757 numbers, so translate if necessary due to register windows. */
2758 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i
))
2759 && HARD_REGNO_MODE_OK (i
, Pmode
))
2760 static_reg_base_value
[i
] = arg_base_value
;
2762 static_reg_base_value
[STACK_POINTER_REGNUM
]
2763 = unique_base_value (UNIQUE_BASE_VALUE_SP
);
2764 static_reg_base_value
[ARG_POINTER_REGNUM
]
2765 = unique_base_value (UNIQUE_BASE_VALUE_ARGP
);
2766 static_reg_base_value
[FRAME_POINTER_REGNUM
]
2767 = unique_base_value (UNIQUE_BASE_VALUE_FP
);
2768 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2769 static_reg_base_value
[HARD_FRAME_POINTER_REGNUM
]
2770 = unique_base_value (UNIQUE_BASE_VALUE_HFP
);
2774 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2775 to be memory reference. */
2776 static bool memory_modified
;
2778 memory_modified_1 (rtx x
, const_rtx pat ATTRIBUTE_UNUSED
, void *data
)
2782 if (anti_dependence (x
, (const_rtx
)data
) || output_dependence (x
, (const_rtx
)data
))
2783 memory_modified
= true;
2788 /* Return true when INSN possibly modify memory contents of MEM
2789 (i.e. address can be modified). */
2791 memory_modified_in_insn_p (const_rtx mem
, const_rtx insn
)
2795 memory_modified
= false;
2796 note_stores (PATTERN (insn
), memory_modified_1
, CONST_CAST_RTX(mem
));
2797 return memory_modified
;
2800 /* Return TRUE if the destination of a set is rtx identical to
2803 set_dest_equal_p (const_rtx set
, const_rtx item
)
2805 rtx dest
= SET_DEST (set
);
2806 return rtx_equal_p (dest
, item
);
2809 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2810 *DEFINITELY* modify the memory contents of MEM. */
2812 memory_must_be_modified_in_insn_p (const_rtx mem
, const_rtx insn
)
2816 insn
= PATTERN (insn
);
2817 if (GET_CODE (insn
) == SET
)
2818 return set_dest_equal_p (insn
, mem
);
2819 else if (GET_CODE (insn
) == PARALLEL
)
2822 for (i
= 0; i
< XVECLEN (insn
, 0); i
++)
2824 rtx sub
= XVECEXP (insn
, 0, i
);
2825 if (GET_CODE (sub
) == SET
2826 && set_dest_equal_p (sub
, mem
))
2833 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2837 init_alias_analysis (void)
2839 unsigned int maxreg
= max_reg_num ();
2847 timevar_push (TV_ALIAS_ANALYSIS
);
2849 vec_safe_grow_cleared (reg_known_value
, maxreg
- FIRST_PSEUDO_REGISTER
);
2850 reg_known_equiv_p
= sbitmap_alloc (maxreg
- FIRST_PSEUDO_REGISTER
);
2851 bitmap_clear (reg_known_equiv_p
);
2853 /* If we have memory allocated from the previous run, use it. */
2854 if (old_reg_base_value
)
2855 reg_base_value
= old_reg_base_value
;
2858 reg_base_value
->truncate (0);
2860 vec_safe_grow_cleared (reg_base_value
, maxreg
);
2862 new_reg_base_value
= XNEWVEC (rtx
, maxreg
);
2863 reg_seen
= sbitmap_alloc (maxreg
);
2865 /* The basic idea is that each pass through this loop will use the
2866 "constant" information from the previous pass to propagate alias
2867 information through another level of assignments.
2869 The propagation is done on the CFG in reverse post-order, to propagate
2870 things forward as far as possible in each iteration.
2872 This could get expensive if the assignment chains are long. Maybe
2873 we should throttle the number of iterations, possibly based on
2874 the optimization level or flag_expensive_optimizations.
2876 We could propagate more information in the first pass by making use
2877 of DF_REG_DEF_COUNT to determine immediately that the alias information
2878 for a pseudo is "constant".
2880 A program with an uninitialized variable can cause an infinite loop
2881 here. Instead of doing a full dataflow analysis to detect such problems
2882 we just cap the number of iterations for the loop.
2884 The state of the arrays for the set chain in question does not matter
2885 since the program has undefined behavior. */
2887 rpo
= XNEWVEC (int, n_basic_blocks
);
2888 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
2893 /* Assume nothing will change this iteration of the loop. */
2896 /* We want to assign the same IDs each iteration of this loop, so
2897 start counting from one each iteration of the loop. */
2900 /* We're at the start of the function each iteration through the
2901 loop, so we're copying arguments. */
2902 copying_arguments
= true;
2904 /* Wipe the potential alias information clean for this pass. */
2905 memset (new_reg_base_value
, 0, maxreg
* sizeof (rtx
));
2907 /* Wipe the reg_seen array clean. */
2908 bitmap_clear (reg_seen
);
2910 /* Mark all hard registers which may contain an address.
2911 The stack, frame and argument pointers may contain an address.
2912 An argument register which can hold a Pmode value may contain
2913 an address even if it is not in BASE_REGS.
2915 The address expression is VOIDmode for an argument and
2916 Pmode for other registers. */
2918 memcpy (new_reg_base_value
, static_reg_base_value
,
2919 FIRST_PSEUDO_REGISTER
* sizeof (rtx
));
2921 /* Walk the insns adding values to the new_reg_base_value array. */
2922 for (i
= 0; i
< rpo_cnt
; i
++)
2924 basic_block bb
= BASIC_BLOCK (rpo
[i
]);
2925 FOR_BB_INSNS (bb
, insn
)
2927 if (NONDEBUG_INSN_P (insn
))
2931 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2932 /* The prologue/epilogue insns are not threaded onto the
2933 insn chain until after reload has completed. Thus,
2934 there is no sense wasting time checking if INSN is in
2935 the prologue/epilogue until after reload has completed. */
2936 if (reload_completed
2937 && prologue_epilogue_contains (insn
))
2941 /* If this insn has a noalias note, process it, Otherwise,
2942 scan for sets. A simple set will have no side effects
2943 which could change the base value of any other register. */
2945 if (GET_CODE (PATTERN (insn
)) == SET
2946 && REG_NOTES (insn
) != 0
2947 && find_reg_note (insn
, REG_NOALIAS
, NULL_RTX
))
2948 record_set (SET_DEST (PATTERN (insn
)), NULL_RTX
, NULL
);
2950 note_stores (PATTERN (insn
), record_set
, NULL
);
2952 set
= single_set (insn
);
2955 && REG_P (SET_DEST (set
))
2956 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
2958 unsigned int regno
= REGNO (SET_DEST (set
));
2959 rtx src
= SET_SRC (set
);
2962 note
= find_reg_equal_equiv_note (insn
);
2963 if (note
&& REG_NOTE_KIND (note
) == REG_EQUAL
2964 && DF_REG_DEF_COUNT (regno
) != 1)
2967 if (note
!= NULL_RTX
2968 && GET_CODE (XEXP (note
, 0)) != EXPR_LIST
2969 && ! rtx_varies_p (XEXP (note
, 0), 1)
2970 && ! reg_overlap_mentioned_p (SET_DEST (set
),
2973 set_reg_known_value (regno
, XEXP (note
, 0));
2974 set_reg_known_equiv_p (regno
,
2975 REG_NOTE_KIND (note
) == REG_EQUIV
);
2977 else if (DF_REG_DEF_COUNT (regno
) == 1
2978 && GET_CODE (src
) == PLUS
2979 && REG_P (XEXP (src
, 0))
2980 && (t
= get_reg_known_value (REGNO (XEXP (src
, 0))))
2981 && CONST_INT_P (XEXP (src
, 1)))
2983 t
= plus_constant (GET_MODE (src
), t
,
2984 INTVAL (XEXP (src
, 1)));
2985 set_reg_known_value (regno
, t
);
2986 set_reg_known_equiv_p (regno
, false);
2988 else if (DF_REG_DEF_COUNT (regno
) == 1
2989 && ! rtx_varies_p (src
, 1))
2991 set_reg_known_value (regno
, src
);
2992 set_reg_known_equiv_p (regno
, false);
2996 else if (NOTE_P (insn
)
2997 && NOTE_KIND (insn
) == NOTE_INSN_FUNCTION_BEG
)
2998 copying_arguments
= false;
3002 /* Now propagate values from new_reg_base_value to reg_base_value. */
3003 gcc_assert (maxreg
== (unsigned int) max_reg_num ());
3005 for (ui
= 0; ui
< maxreg
; ui
++)
3007 if (new_reg_base_value
[ui
]
3008 && new_reg_base_value
[ui
] != (*reg_base_value
)[ui
]
3009 && ! rtx_equal_p (new_reg_base_value
[ui
], (*reg_base_value
)[ui
]))
3011 (*reg_base_value
)[ui
] = new_reg_base_value
[ui
];
3016 while (changed
&& ++pass
< MAX_ALIAS_LOOP_PASSES
);
3019 /* Fill in the remaining entries. */
3020 FOR_EACH_VEC_ELT (*reg_known_value
, i
, val
)
3022 int regno
= i
+ FIRST_PSEUDO_REGISTER
;
3024 set_reg_known_value (regno
, regno_reg_rtx
[regno
]);
3028 free (new_reg_base_value
);
3029 new_reg_base_value
= 0;
3030 sbitmap_free (reg_seen
);
3032 timevar_pop (TV_ALIAS_ANALYSIS
);
3035 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3036 Special API for var-tracking pass purposes. */
3039 vt_equate_reg_base_value (const_rtx reg1
, const_rtx reg2
)
3041 (*reg_base_value
)[REGNO (reg1
)] = REG_BASE_VALUE (reg2
);
3045 end_alias_analysis (void)
3047 old_reg_base_value
= reg_base_value
;
3048 vec_free (reg_known_value
);
3049 sbitmap_free (reg_known_equiv_p
);
3052 #include "gt-alias.h"