1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2015 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"
29 #include "double-int.h"
36 #include "fold-const.h"
39 #include "hard-reg-set.h"
42 #include "statistics.h"
44 #include "fixed-value.h"
45 #include "insn-config.h"
55 #include "diagnostic-core.h"
58 #include "langhooks.h"
62 #include "dominance.h"
66 #include "basic-block.h"
68 #include "tree-ssa-alias.h"
69 #include "internal-fn.h"
70 #include "gimple-expr.h"
73 #include "gimple-ssa.h"
76 /* The aliasing API provided here solves related but different problems:
78 Say there exists (in c)
92 Consider the four questions:
94 Can a store to x1 interfere with px2->y1?
95 Can a store to x1 interfere with px2->z2?
96 Can a store to x1 change the value pointed to by with py?
97 Can a store to x1 change the value pointed to by with pz?
99 The answer to these questions can be yes, yes, yes, and maybe.
101 The first two questions can be answered with a simple examination
102 of the type system. If structure X contains a field of type Y then
103 a store through a pointer to an X can overwrite any field that is
104 contained (recursively) in an X (unless we know that px1 != px2).
106 The last two questions can be solved in the same way as the first
107 two questions but this is too conservative. The observation is
108 that in some cases we can know which (if any) fields are addressed
109 and if those addresses are used in bad ways. This analysis may be
110 language specific. In C, arbitrary operations may be applied to
111 pointers. However, there is some indication that this may be too
112 conservative for some C++ types.
114 The pass ipa-type-escape does this analysis for the types whose
115 instances do not escape across the compilation boundary.
117 Historically in GCC, these two problems were combined and a single
118 data structure that was used to represent the solution to these
119 problems. We now have two similar but different data structures,
120 The data structure to solve the last two questions is similar to
121 the first, but does not contain the fields whose address are never
122 taken. For types that do escape the compilation unit, the data
123 structures will have identical information.
126 /* The alias sets assigned to MEMs assist the back-end in determining
127 which MEMs can alias which other MEMs. In general, two MEMs in
128 different alias sets cannot alias each other, with one important
129 exception. Consider something like:
131 struct S { int i; double d; };
133 a store to an `S' can alias something of either type `int' or type
134 `double'. (However, a store to an `int' cannot alias a `double'
135 and vice versa.) We indicate this via a tree structure that looks
143 (The arrows are directed and point downwards.)
144 In this situation we say the alias set for `struct S' is the
145 `superset' and that those for `int' and `double' are `subsets'.
147 To see whether two alias sets can point to the same memory, we must
148 see if either alias set is a subset of the other. We need not trace
149 past immediate descendants, however, since we propagate all
150 grandchildren up one level.
152 Alias set zero is implicitly a superset of all other alias sets.
153 However, this is no actual entry for alias set zero. It is an
154 error to attempt to explicitly construct a subset of zero. */
156 struct alias_set_traits
: default_hashmap_traits
162 return e
.m_key
== INT_MIN
;
169 return e
.m_key
== (INT_MIN
+ 1);
172 template<typename T
> static void mark_empty (T
&e
) { e
.m_key
= INT_MIN
; }
178 e
.m_key
= INT_MIN
+ 1;
182 struct GTY(()) alias_set_entry_d
{
183 /* The alias set number, as stored in MEM_ALIAS_SET. */
184 alias_set_type alias_set
;
186 /* Nonzero if would have a child of zero: this effectively makes this
187 alias set the same as alias set zero. */
190 /* The children of the alias set. These are not just the immediate
191 children, but, in fact, all descendants. So, if we have:
193 struct T { struct S s; float f; }
195 continuing our example above, the children here will be all of
196 `int', `double', `float', and `struct S'. */
197 hash_map
<int, int, alias_set_traits
> *children
;
199 typedef struct alias_set_entry_d
*alias_set_entry
;
201 static int rtx_equal_for_memref_p (const_rtx
, const_rtx
);
202 static int memrefs_conflict_p (int, rtx
, int, rtx
, HOST_WIDE_INT
);
203 static void record_set (rtx
, const_rtx
, void *);
204 static int base_alias_check (rtx
, rtx
, rtx
, rtx
, machine_mode
,
206 static rtx
find_base_value (rtx
);
207 static int mems_in_disjoint_alias_sets_p (const_rtx
, const_rtx
);
208 static alias_set_entry
get_alias_set_entry (alias_set_type
);
209 static tree
decl_for_component_ref (tree
);
210 static int write_dependence_p (const_rtx
,
211 const_rtx
, machine_mode
, rtx
,
214 static void memory_modified_1 (rtx
, const_rtx
, void *);
216 /* Set up all info needed to perform alias analysis on memory references. */
218 /* Returns the size in bytes of the mode of X. */
219 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
221 /* Cap the number of passes we make over the insns propagating alias
222 information through set chains.
223 ??? 10 is a completely arbitrary choice. This should be based on the
224 maximum loop depth in the CFG, but we do not have this information
225 available (even if current_loops _is_ available). */
226 #define MAX_ALIAS_LOOP_PASSES 10
228 /* reg_base_value[N] gives an address to which register N is related.
229 If all sets after the first add or subtract to the current value
230 or otherwise modify it so it does not point to a different top level
231 object, reg_base_value[N] is equal to the address part of the source
234 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
235 expressions represent three types of base:
237 1. incoming arguments. There is just one ADDRESS to represent all
238 arguments, since we do not know at this level whether accesses
239 based on different arguments can alias. The ADDRESS has id 0.
241 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
242 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
243 Each of these rtxes has a separate ADDRESS associated with it,
244 each with a negative id.
246 GCC is (and is required to be) precise in which register it
247 chooses to access a particular region of stack. We can therefore
248 assume that accesses based on one of these rtxes do not alias
249 accesses based on another of these rtxes.
251 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
252 Each such piece of memory has a separate ADDRESS associated
253 with it, each with an id greater than 0.
255 Accesses based on one ADDRESS do not alias accesses based on other
256 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
257 alias globals either; the ADDRESSes have Pmode to indicate this.
258 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
261 static GTY(()) vec
<rtx
, va_gc
> *reg_base_value
;
262 static rtx
*new_reg_base_value
;
264 /* The single VOIDmode ADDRESS that represents all argument bases.
266 static GTY(()) rtx arg_base_value
;
268 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
269 static int unique_id
;
271 /* We preserve the copy of old array around to avoid amount of garbage
272 produced. About 8% of garbage produced were attributed to this
274 static GTY((deletable
)) vec
<rtx
, va_gc
> *old_reg_base_value
;
276 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
278 #define UNIQUE_BASE_VALUE_SP -1
279 #define UNIQUE_BASE_VALUE_ARGP -2
280 #define UNIQUE_BASE_VALUE_FP -3
281 #define UNIQUE_BASE_VALUE_HFP -4
283 #define static_reg_base_value \
284 (this_target_rtl->x_static_reg_base_value)
286 #define REG_BASE_VALUE(X) \
287 (REGNO (X) < vec_safe_length (reg_base_value) \
288 ? (*reg_base_value)[REGNO (X)] : 0)
290 /* Vector indexed by N giving the initial (unchanging) value known for
291 pseudo-register N. This vector is initialized in init_alias_analysis,
292 and does not change until end_alias_analysis is called. */
293 static GTY(()) vec
<rtx
, va_gc
> *reg_known_value
;
295 /* Vector recording for each reg_known_value whether it is due to a
296 REG_EQUIV note. Future passes (viz., reload) may replace the
297 pseudo with the equivalent expression and so we account for the
298 dependences that would be introduced if that happens.
300 The REG_EQUIV notes created in assign_parms may mention the arg
301 pointer, and there are explicit insns in the RTL that modify the
302 arg pointer. Thus we must ensure that such insns don't get
303 scheduled across each other because that would invalidate the
304 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
305 wrong, but solving the problem in the scheduler will likely give
306 better code, so we do it here. */
307 static sbitmap reg_known_equiv_p
;
309 /* True when scanning insns from the start of the rtl to the
310 NOTE_INSN_FUNCTION_BEG note. */
311 static bool copying_arguments
;
314 /* The splay-tree used to store the various alias set entries. */
315 static GTY (()) vec
<alias_set_entry
, va_gc
> *alias_sets
;
317 /* Build a decomposed reference object for querying the alias-oracle
318 from the MEM rtx and store it in *REF.
319 Returns false if MEM is not suitable for the alias-oracle. */
322 ao_ref_from_mem (ao_ref
*ref
, const_rtx mem
)
324 tree expr
= MEM_EXPR (mem
);
330 ao_ref_init (ref
, expr
);
332 /* Get the base of the reference and see if we have to reject or
334 base
= ao_ref_base (ref
);
335 if (base
== NULL_TREE
)
338 /* The tree oracle doesn't like bases that are neither decls
339 nor indirect references of SSA names. */
341 || (TREE_CODE (base
) == MEM_REF
342 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
343 || (TREE_CODE (base
) == TARGET_MEM_REF
344 && TREE_CODE (TMR_BASE (base
)) == SSA_NAME
)))
347 /* If this is a reference based on a partitioned decl replace the
348 base with a MEM_REF of the pointer representative we
349 created during stack slot partitioning. */
350 if (TREE_CODE (base
) == VAR_DECL
351 && ! is_global_var (base
)
352 && cfun
->gimple_df
->decls_to_pointers
!= NULL
)
354 tree
*namep
= cfun
->gimple_df
->decls_to_pointers
->get (base
);
356 ref
->base
= build_simple_mem_ref (*namep
);
359 ref
->ref_alias_set
= MEM_ALIAS_SET (mem
);
361 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
362 is conservative, so trust it. */
363 if (!MEM_OFFSET_KNOWN_P (mem
)
364 || !MEM_SIZE_KNOWN_P (mem
))
367 /* If the base decl is a parameter we can have negative MEM_OFFSET in
368 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
370 if (MEM_OFFSET (mem
) < 0
371 && (MEM_SIZE (mem
) + MEM_OFFSET (mem
)) * BITS_PER_UNIT
== ref
->size
)
374 /* Otherwise continue and refine size and offset we got from analyzing
375 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
377 ref
->offset
+= MEM_OFFSET (mem
) * BITS_PER_UNIT
;
378 ref
->size
= MEM_SIZE (mem
) * BITS_PER_UNIT
;
380 /* The MEM may extend into adjacent fields, so adjust max_size if
382 if (ref
->max_size
!= -1
383 && ref
->size
> ref
->max_size
)
384 ref
->max_size
= ref
->size
;
386 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
387 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
388 if (MEM_EXPR (mem
) != get_spill_slot_decl (false)
390 || (DECL_P (ref
->base
)
391 && (DECL_SIZE (ref
->base
) == NULL_TREE
392 || TREE_CODE (DECL_SIZE (ref
->base
)) != INTEGER_CST
393 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref
->base
)),
394 ref
->offset
+ ref
->size
)))))
400 /* Query the alias-oracle on whether the two memory rtx X and MEM may
401 alias. If TBAA_P is set also apply TBAA. Returns true if the
402 two rtxen may alias, false otherwise. */
405 rtx_refs_may_alias_p (const_rtx x
, const_rtx mem
, bool tbaa_p
)
409 if (!ao_ref_from_mem (&ref1
, x
)
410 || !ao_ref_from_mem (&ref2
, mem
))
413 return refs_may_alias_p_1 (&ref1
, &ref2
,
415 && MEM_ALIAS_SET (x
) != 0
416 && MEM_ALIAS_SET (mem
) != 0);
419 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
420 such an entry, or NULL otherwise. */
422 static inline alias_set_entry
423 get_alias_set_entry (alias_set_type alias_set
)
425 return (*alias_sets
)[alias_set
];
428 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
429 the two MEMs cannot alias each other. */
432 mems_in_disjoint_alias_sets_p (const_rtx mem1
, const_rtx mem2
)
434 return (flag_strict_aliasing
435 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1
),
436 MEM_ALIAS_SET (mem2
)));
439 /* Return true if the first alias set is a subset of the second. */
442 alias_set_subset_of (alias_set_type set1
, alias_set_type set2
)
446 /* Everything is a subset of the "aliases everything" set. */
450 /* Otherwise, check if set1 is a subset of set2. */
451 ase
= get_alias_set_entry (set2
);
453 && (ase
->has_zero_child
454 || ase
->children
->get (set1
)))
459 /* Return 1 if the two specified alias sets may conflict. */
462 alias_sets_conflict_p (alias_set_type set1
, alias_set_type set2
)
467 if (alias_sets_must_conflict_p (set1
, set2
))
470 /* See if the first alias set is a subset of the second. */
471 ase
= get_alias_set_entry (set1
);
473 && (ase
->has_zero_child
474 || ase
->children
->get (set2
)))
477 /* Now do the same, but with the alias sets reversed. */
478 ase
= get_alias_set_entry (set2
);
480 && (ase
->has_zero_child
481 || ase
->children
->get (set1
)))
484 /* The two alias sets are distinct and neither one is the
485 child of the other. Therefore, they cannot conflict. */
489 /* Return 1 if the two specified alias sets will always conflict. */
492 alias_sets_must_conflict_p (alias_set_type set1
, alias_set_type set2
)
494 if (set1
== 0 || set2
== 0 || set1
== set2
)
500 /* Return 1 if any MEM object of type T1 will always conflict (using the
501 dependency routines in this file) with any MEM object of type T2.
502 This is used when allocating temporary storage. If T1 and/or T2 are
503 NULL_TREE, it means we know nothing about the storage. */
506 objects_must_conflict_p (tree t1
, tree t2
)
508 alias_set_type set1
, set2
;
510 /* If neither has a type specified, we don't know if they'll conflict
511 because we may be using them to store objects of various types, for
512 example the argument and local variables areas of inlined functions. */
513 if (t1
== 0 && t2
== 0)
516 /* If they are the same type, they must conflict. */
518 /* Likewise if both are volatile. */
519 || (t1
!= 0 && TYPE_VOLATILE (t1
) && t2
!= 0 && TYPE_VOLATILE (t2
)))
522 set1
= t1
? get_alias_set (t1
) : 0;
523 set2
= t2
? get_alias_set (t2
) : 0;
525 /* We can't use alias_sets_conflict_p because we must make sure
526 that every subtype of t1 will conflict with every subtype of
527 t2 for which a pair of subobjects of these respective subtypes
528 overlaps on the stack. */
529 return alias_sets_must_conflict_p (set1
, set2
);
532 /* Return the outermost parent of component present in the chain of
533 component references handled by get_inner_reference in T with the
535 - the component is non-addressable, or
536 - the parent has alias set zero,
537 or NULL_TREE if no such parent exists. In the former cases, the alias
538 set of this parent is the alias set that must be used for T itself. */
541 component_uses_parent_alias_set_from (const_tree t
)
543 const_tree found
= NULL_TREE
;
545 while (handled_component_p (t
))
547 switch (TREE_CODE (t
))
550 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t
, 1)))
555 case ARRAY_RANGE_REF
:
556 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t
, 0))))
565 case VIEW_CONVERT_EXPR
:
566 /* Bitfields and casts are never addressable. */
574 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t
, 0))) == 0)
577 t
= TREE_OPERAND (t
, 0);
581 return TREE_OPERAND (found
, 0);
587 /* Return whether the pointer-type T effective for aliasing may
588 access everything and thus the reference has to be assigned
592 ref_all_alias_ptr_type_p (const_tree t
)
594 return (TREE_CODE (TREE_TYPE (t
)) == VOID_TYPE
595 || TYPE_REF_CAN_ALIAS_ALL (t
));
598 /* Return the alias set for the memory pointed to by T, which may be
599 either a type or an expression. Return -1 if there is nothing
600 special about dereferencing T. */
602 static alias_set_type
603 get_deref_alias_set_1 (tree t
)
605 /* All we care about is the type. */
609 /* If we have an INDIRECT_REF via a void pointer, we don't
610 know anything about what that might alias. Likewise if the
611 pointer is marked that way. */
612 if (ref_all_alias_ptr_type_p (t
))
618 /* Return the alias set for the memory pointed to by T, which may be
619 either a type or an expression. */
622 get_deref_alias_set (tree t
)
624 /* If we're not doing any alias analysis, just assume everything
625 aliases everything else. */
626 if (!flag_strict_aliasing
)
629 alias_set_type set
= get_deref_alias_set_1 (t
);
631 /* Fall back to the alias-set of the pointed-to type. */
636 set
= get_alias_set (TREE_TYPE (t
));
642 /* Return the pointer-type relevant for TBAA purposes from the
643 memory reference tree *T or NULL_TREE in which case *T is
644 adjusted to point to the outermost component reference that
645 can be used for assigning an alias set. */
648 reference_alias_ptr_type_1 (tree
*t
)
652 /* Get the base object of the reference. */
654 while (handled_component_p (inner
))
656 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
657 the type of any component references that wrap it to
658 determine the alias-set. */
659 if (TREE_CODE (inner
) == VIEW_CONVERT_EXPR
)
660 *t
= TREE_OPERAND (inner
, 0);
661 inner
= TREE_OPERAND (inner
, 0);
664 /* Handle pointer dereferences here, they can override the
666 if (INDIRECT_REF_P (inner
)
667 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner
, 0))))
668 return TREE_TYPE (TREE_OPERAND (inner
, 0));
669 else if (TREE_CODE (inner
) == TARGET_MEM_REF
)
670 return TREE_TYPE (TMR_OFFSET (inner
));
671 else if (TREE_CODE (inner
) == MEM_REF
672 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner
, 1))))
673 return TREE_TYPE (TREE_OPERAND (inner
, 1));
675 /* If the innermost reference is a MEM_REF that has a
676 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
677 using the memory access type for determining the alias-set. */
678 if (TREE_CODE (inner
) == MEM_REF
679 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner
))
681 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner
, 1))))))
682 return TREE_TYPE (TREE_OPERAND (inner
, 1));
684 /* Otherwise, pick up the outermost object that we could have
686 tree tem
= component_uses_parent_alias_set_from (*t
);
693 /* Return the pointer-type relevant for TBAA purposes from the
694 gimple memory reference tree T. This is the type to be used for
695 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
696 and guarantees that get_alias_set will return the same alias
697 set for T and the replacement. */
700 reference_alias_ptr_type (tree t
)
702 tree ptype
= reference_alias_ptr_type_1 (&t
);
703 /* If there is a given pointer type for aliasing purposes, return it. */
704 if (ptype
!= NULL_TREE
)
707 /* Otherwise build one from the outermost component reference we
709 if (TREE_CODE (t
) == MEM_REF
710 || TREE_CODE (t
) == TARGET_MEM_REF
)
711 return TREE_TYPE (TREE_OPERAND (t
, 1));
713 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t
)));
716 /* Return whether the pointer-types T1 and T2 used to determine
717 two alias sets of two references will yield the same answer
718 from get_deref_alias_set. */
721 alias_ptr_types_compatible_p (tree t1
, tree t2
)
723 if (TYPE_MAIN_VARIANT (t1
) == TYPE_MAIN_VARIANT (t2
))
726 if (ref_all_alias_ptr_type_p (t1
)
727 || ref_all_alias_ptr_type_p (t2
))
730 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1
))
731 == TYPE_MAIN_VARIANT (TREE_TYPE (t2
)));
734 /* Return the alias set for T, which may be either a type or an
735 expression. Call language-specific routine for help, if needed. */
738 get_alias_set (tree t
)
742 /* If we're not doing any alias analysis, just assume everything
743 aliases everything else. Also return 0 if this or its type is
745 if (! flag_strict_aliasing
|| t
== error_mark_node
747 && (TREE_TYPE (t
) == 0 || TREE_TYPE (t
) == error_mark_node
)))
750 /* We can be passed either an expression or a type. This and the
751 language-specific routine may make mutually-recursive calls to each other
752 to figure out what to do. At each juncture, we see if this is a tree
753 that the language may need to handle specially. First handle things that
757 /* Give the language a chance to do something with this tree
758 before we look at it. */
760 set
= lang_hooks
.get_alias_set (t
);
764 /* Get the alias pointer-type to use or the outermost object
765 that we could have a pointer to. */
766 tree ptype
= reference_alias_ptr_type_1 (&t
);
768 return get_deref_alias_set (ptype
);
770 /* If we've already determined the alias set for a decl, just return
771 it. This is necessary for C++ anonymous unions, whose component
772 variables don't look like union members (boo!). */
773 if (TREE_CODE (t
) == VAR_DECL
774 && DECL_RTL_SET_P (t
) && MEM_P (DECL_RTL (t
)))
775 return MEM_ALIAS_SET (DECL_RTL (t
));
777 /* Now all we care about is the type. */
781 /* Variant qualifiers don't affect the alias set, so get the main
783 t
= TYPE_MAIN_VARIANT (t
);
785 /* Always use the canonical type as well. If this is a type that
786 requires structural comparisons to identify compatible types
787 use alias set zero. */
788 if (TYPE_STRUCTURAL_EQUALITY_P (t
))
790 /* Allow the language to specify another alias set for this
792 set
= lang_hooks
.get_alias_set (t
);
798 t
= TYPE_CANONICAL (t
);
800 /* The canonical type should not require structural equality checks. */
801 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t
));
803 /* If this is a type with a known alias set, return it. */
804 if (TYPE_ALIAS_SET_KNOWN_P (t
))
805 return TYPE_ALIAS_SET (t
);
807 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
808 if (!COMPLETE_TYPE_P (t
))
810 /* For arrays with unknown size the conservative answer is the
811 alias set of the element type. */
812 if (TREE_CODE (t
) == ARRAY_TYPE
)
813 return get_alias_set (TREE_TYPE (t
));
815 /* But return zero as a conservative answer for incomplete types. */
819 /* See if the language has special handling for this type. */
820 set
= lang_hooks
.get_alias_set (t
);
824 /* There are no objects of FUNCTION_TYPE, so there's no point in
825 using up an alias set for them. (There are, of course, pointers
826 and references to functions, but that's different.) */
827 else if (TREE_CODE (t
) == FUNCTION_TYPE
|| TREE_CODE (t
) == METHOD_TYPE
)
830 /* Unless the language specifies otherwise, let vector types alias
831 their components. This avoids some nasty type punning issues in
832 normal usage. And indeed lets vectors be treated more like an
834 else if (TREE_CODE (t
) == VECTOR_TYPE
)
835 set
= get_alias_set (TREE_TYPE (t
));
837 /* Unless the language specifies otherwise, treat array types the
838 same as their components. This avoids the asymmetry we get
839 through recording the components. Consider accessing a
840 character(kind=1) through a reference to a character(kind=1)[1:1].
841 Or consider if we want to assign integer(kind=4)[0:D.1387] and
842 integer(kind=4)[4] the same alias set or not.
843 Just be pragmatic here and make sure the array and its element
844 type get the same alias set assigned. */
845 else if (TREE_CODE (t
) == ARRAY_TYPE
&& !TYPE_NONALIASED_COMPONENT (t
))
846 set
= get_alias_set (TREE_TYPE (t
));
848 /* From the former common C and C++ langhook implementation:
850 Unfortunately, there is no canonical form of a pointer type.
851 In particular, if we have `typedef int I', then `int *', and
852 `I *' are different types. So, we have to pick a canonical
853 representative. We do this below.
855 Technically, this approach is actually more conservative that
856 it needs to be. In particular, `const int *' and `int *'
857 should be in different alias sets, according to the C and C++
858 standard, since their types are not the same, and so,
859 technically, an `int **' and `const int **' cannot point at
862 But, the standard is wrong. In particular, this code is
867 const int* const* cipp = ipp;
868 And, it doesn't make sense for that to be legal unless you
869 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
870 the pointed-to types. This issue has been reported to the
873 In addition to the above canonicalization issue, with LTO
874 we should also canonicalize `T (*)[]' to `T *' avoiding
875 alias issues with pointer-to element types and pointer-to
878 Likewise we need to deal with the situation of incomplete
879 pointed-to types and make `*(struct X **)&a' and
880 `*(struct X {} **)&a' alias. Otherwise we will have to
881 guarantee that all pointer-to incomplete type variants
882 will be replaced by pointer-to complete type variants if
885 With LTO the convenient situation of using `void *' to
886 access and store any pointer type will also become
887 more apparent (and `void *' is just another pointer-to
888 incomplete type). Assigning alias-set zero to `void *'
889 and all pointer-to incomplete types is a not appealing
890 solution. Assigning an effective alias-set zero only
891 affecting pointers might be - by recording proper subset
892 relationships of all pointer alias-sets.
894 Pointer-to function types are another grey area which
895 needs caution. Globbing them all into one alias-set
896 or the above effective zero set would work.
898 For now just assign the same alias-set to all pointers.
899 That's simple and avoids all the above problems. */
900 else if (POINTER_TYPE_P (t
)
901 && t
!= ptr_type_node
)
902 set
= get_alias_set (ptr_type_node
);
904 /* Otherwise make a new alias set for this type. */
907 /* Each canonical type gets its own alias set, so canonical types
908 shouldn't form a tree. It doesn't really matter for types
909 we handle specially above, so only check it where it possibly
910 would result in a bogus alias set. */
911 gcc_checking_assert (TYPE_CANONICAL (t
) == t
);
913 set
= new_alias_set ();
916 TYPE_ALIAS_SET (t
) = set
;
918 /* If this is an aggregate type or a complex type, we must record any
919 component aliasing information. */
920 if (AGGREGATE_TYPE_P (t
) || TREE_CODE (t
) == COMPLEX_TYPE
)
921 record_component_aliases (t
);
926 /* Return a brand-new alias set. */
931 if (flag_strict_aliasing
)
934 vec_safe_push (alias_sets
, (alias_set_entry
) 0);
935 vec_safe_push (alias_sets
, (alias_set_entry
) 0);
936 return alias_sets
->length () - 1;
942 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
943 not everything that aliases SUPERSET also aliases SUBSET. For example,
944 in C, a store to an `int' can alias a load of a structure containing an
945 `int', and vice versa. But it can't alias a load of a 'double' member
946 of the same structure. Here, the structure would be the SUPERSET and
947 `int' the SUBSET. This relationship is also described in the comment at
948 the beginning of this file.
950 This function should be called only once per SUPERSET/SUBSET pair.
952 It is illegal for SUPERSET to be zero; everything is implicitly a
953 subset of alias set zero. */
956 record_alias_subset (alias_set_type superset
, alias_set_type subset
)
958 alias_set_entry superset_entry
;
959 alias_set_entry subset_entry
;
961 /* It is possible in complex type situations for both sets to be the same,
962 in which case we can ignore this operation. */
963 if (superset
== subset
)
966 gcc_assert (superset
);
968 superset_entry
= get_alias_set_entry (superset
);
969 if (superset_entry
== 0)
971 /* Create an entry for the SUPERSET, so that we have a place to
972 attach the SUBSET. */
973 superset_entry
= ggc_cleared_alloc
<alias_set_entry_d
> ();
974 superset_entry
->alias_set
= superset
;
975 superset_entry
->children
976 = hash_map
<int, int, alias_set_traits
>::create_ggc (64);
977 superset_entry
->has_zero_child
= 0;
978 (*alias_sets
)[superset
] = superset_entry
;
982 superset_entry
->has_zero_child
= 1;
985 subset_entry
= get_alias_set_entry (subset
);
986 /* If there is an entry for the subset, enter all of its children
987 (if they are not already present) as children of the SUPERSET. */
990 if (subset_entry
->has_zero_child
)
991 superset_entry
->has_zero_child
= 1;
993 hash_map
<int, int, alias_set_traits
>::iterator iter
994 = subset_entry
->children
->begin ();
995 for (; iter
!= subset_entry
->children
->end (); ++iter
)
996 superset_entry
->children
->put ((*iter
).first
, (*iter
).second
);
999 /* Enter the SUBSET itself as a child of the SUPERSET. */
1000 superset_entry
->children
->put (subset
, 0);
1004 /* Record that component types of TYPE, if any, are part of that type for
1005 aliasing purposes. For record types, we only record component types
1006 for fields that are not marked non-addressable. For array types, we
1007 only record the component type if it is not marked non-aliased. */
1010 record_component_aliases (tree type
)
1012 alias_set_type superset
= get_alias_set (type
);
1018 switch (TREE_CODE (type
))
1022 case QUAL_UNION_TYPE
:
1023 for (field
= TYPE_FIELDS (type
); field
!= 0; field
= DECL_CHAIN (field
))
1024 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_NONADDRESSABLE_P (field
))
1025 record_alias_subset (superset
, get_alias_set (TREE_TYPE (field
)));
1029 record_alias_subset (superset
, get_alias_set (TREE_TYPE (type
)));
1032 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1040 /* Allocate an alias set for use in storing and reading from the varargs
1043 static GTY(()) alias_set_type varargs_set
= -1;
1046 get_varargs_alias_set (void)
1049 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1050 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1051 consistently use the varargs alias set for loads from the varargs
1052 area. So don't use it anywhere. */
1055 if (varargs_set
== -1)
1056 varargs_set
= new_alias_set ();
1062 /* Likewise, but used for the fixed portions of the frame, e.g., register
1065 static GTY(()) alias_set_type frame_set
= -1;
1068 get_frame_alias_set (void)
1070 if (frame_set
== -1)
1071 frame_set
= new_alias_set ();
1076 /* Create a new, unique base with id ID. */
1079 unique_base_value (HOST_WIDE_INT id
)
1081 return gen_rtx_ADDRESS (Pmode
, id
);
1084 /* Return true if accesses based on any other base value cannot alias
1085 those based on X. */
1088 unique_base_value_p (rtx x
)
1090 return GET_CODE (x
) == ADDRESS
&& GET_MODE (x
) == Pmode
;
1093 /* Return true if X is known to be a base value. */
1096 known_base_value_p (rtx x
)
1098 switch (GET_CODE (x
))
1105 /* Arguments may or may not be bases; we don't know for sure. */
1106 return GET_MODE (x
) != VOIDmode
;
1113 /* Inside SRC, the source of a SET, find a base address. */
1116 find_base_value (rtx src
)
1120 #if defined (FIND_BASE_TERM)
1121 /* Try machine-dependent ways to find the base term. */
1122 src
= FIND_BASE_TERM (src
);
1125 switch (GET_CODE (src
))
1132 regno
= REGNO (src
);
1133 /* At the start of a function, argument registers have known base
1134 values which may be lost later. Returning an ADDRESS
1135 expression here allows optimization based on argument values
1136 even when the argument registers are used for other purposes. */
1137 if (regno
< FIRST_PSEUDO_REGISTER
&& copying_arguments
)
1138 return new_reg_base_value
[regno
];
1140 /* If a pseudo has a known base value, return it. Do not do this
1141 for non-fixed hard regs since it can result in a circular
1142 dependency chain for registers which have values at function entry.
1144 The test above is not sufficient because the scheduler may move
1145 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1146 if ((regno
>= FIRST_PSEUDO_REGISTER
|| fixed_regs
[regno
])
1147 && regno
< vec_safe_length (reg_base_value
))
1149 /* If we're inside init_alias_analysis, use new_reg_base_value
1150 to reduce the number of relaxation iterations. */
1151 if (new_reg_base_value
&& new_reg_base_value
[regno
]
1152 && DF_REG_DEF_COUNT (regno
) == 1)
1153 return new_reg_base_value
[regno
];
1155 if ((*reg_base_value
)[regno
])
1156 return (*reg_base_value
)[regno
];
1162 /* Check for an argument passed in memory. Only record in the
1163 copying-arguments block; it is too hard to track changes
1165 if (copying_arguments
1166 && (XEXP (src
, 0) == arg_pointer_rtx
1167 || (GET_CODE (XEXP (src
, 0)) == PLUS
1168 && XEXP (XEXP (src
, 0), 0) == arg_pointer_rtx
)))
1169 return arg_base_value
;
1173 src
= XEXP (src
, 0);
1174 if (GET_CODE (src
) != PLUS
&& GET_CODE (src
) != MINUS
)
1177 /* ... fall through ... */
1182 rtx temp
, src_0
= XEXP (src
, 0), src_1
= XEXP (src
, 1);
1184 /* If either operand is a REG that is a known pointer, then it
1186 if (REG_P (src_0
) && REG_POINTER (src_0
))
1187 return find_base_value (src_0
);
1188 if (REG_P (src_1
) && REG_POINTER (src_1
))
1189 return find_base_value (src_1
);
1191 /* If either operand is a REG, then see if we already have
1192 a known value for it. */
1195 temp
= find_base_value (src_0
);
1202 temp
= find_base_value (src_1
);
1207 /* If either base is named object or a special address
1208 (like an argument or stack reference), then use it for the
1210 if (src_0
!= 0 && known_base_value_p (src_0
))
1213 if (src_1
!= 0 && known_base_value_p (src_1
))
1216 /* Guess which operand is the base address:
1217 If either operand is a symbol, then it is the base. If
1218 either operand is a CONST_INT, then the other is the base. */
1219 if (CONST_INT_P (src_1
) || CONSTANT_P (src_0
))
1220 return find_base_value (src_0
);
1221 else if (CONST_INT_P (src_0
) || CONSTANT_P (src_1
))
1222 return find_base_value (src_1
);
1228 /* The standard form is (lo_sum reg sym) so look only at the
1230 return find_base_value (XEXP (src
, 1));
1233 /* If the second operand is constant set the base
1234 address to the first operand. */
1235 if (CONST_INT_P (XEXP (src
, 1)) && INTVAL (XEXP (src
, 1)) != 0)
1236 return find_base_value (XEXP (src
, 0));
1240 /* As we do not know which address space the pointer is referring to, we can
1241 handle this only if the target does not support different pointer or
1242 address modes depending on the address space. */
1243 if (!target_default_pointer_address_modes_p ())
1245 if (GET_MODE_SIZE (GET_MODE (src
)) < GET_MODE_SIZE (Pmode
))
1255 return find_base_value (XEXP (src
, 0));
1258 case SIGN_EXTEND
: /* used for NT/Alpha pointers */
1259 /* As we do not know which address space the pointer is referring to, we can
1260 handle this only if the target does not support different pointer or
1261 address modes depending on the address space. */
1262 if (!target_default_pointer_address_modes_p ())
1266 rtx temp
= find_base_value (XEXP (src
, 0));
1268 if (temp
!= 0 && CONSTANT_P (temp
))
1269 temp
= convert_memory_address (Pmode
, temp
);
1281 /* Called from init_alias_analysis indirectly through note_stores,
1282 or directly if DEST is a register with a REG_NOALIAS note attached.
1283 SET is null in the latter case. */
1285 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1286 register N has been set in this function. */
1287 static sbitmap reg_seen
;
1290 record_set (rtx dest
, const_rtx set
, void *data ATTRIBUTE_UNUSED
)
1299 regno
= REGNO (dest
);
1301 gcc_checking_assert (regno
< reg_base_value
->length ());
1303 /* If this spans multiple hard registers, then we must indicate that every
1304 register has an unusable value. */
1305 if (regno
< FIRST_PSEUDO_REGISTER
)
1306 n
= hard_regno_nregs
[regno
][GET_MODE (dest
)];
1313 bitmap_set_bit (reg_seen
, regno
+ n
);
1314 new_reg_base_value
[regno
+ n
] = 0;
1321 /* A CLOBBER wipes out any old value but does not prevent a previously
1322 unset register from acquiring a base address (i.e. reg_seen is not
1324 if (GET_CODE (set
) == CLOBBER
)
1326 new_reg_base_value
[regno
] = 0;
1329 src
= SET_SRC (set
);
1333 /* There's a REG_NOALIAS note against DEST. */
1334 if (bitmap_bit_p (reg_seen
, regno
))
1336 new_reg_base_value
[regno
] = 0;
1339 bitmap_set_bit (reg_seen
, regno
);
1340 new_reg_base_value
[regno
] = unique_base_value (unique_id
++);
1344 /* If this is not the first set of REGNO, see whether the new value
1345 is related to the old one. There are two cases of interest:
1347 (1) The register might be assigned an entirely new value
1348 that has the same base term as the original set.
1350 (2) The set might be a simple self-modification that
1351 cannot change REGNO's base value.
1353 If neither case holds, reject the original base value as invalid.
1354 Note that the following situation is not detected:
1356 extern int x, y; int *p = &x; p += (&y-&x);
1358 ANSI C does not allow computing the difference of addresses
1359 of distinct top level objects. */
1360 if (new_reg_base_value
[regno
] != 0
1361 && find_base_value (src
) != new_reg_base_value
[regno
])
1362 switch (GET_CODE (src
))
1366 if (XEXP (src
, 0) != dest
&& XEXP (src
, 1) != dest
)
1367 new_reg_base_value
[regno
] = 0;
1370 /* If the value we add in the PLUS is also a valid base value,
1371 this might be the actual base value, and the original value
1374 rtx other
= NULL_RTX
;
1376 if (XEXP (src
, 0) == dest
)
1377 other
= XEXP (src
, 1);
1378 else if (XEXP (src
, 1) == dest
)
1379 other
= XEXP (src
, 0);
1381 if (! other
|| find_base_value (other
))
1382 new_reg_base_value
[regno
] = 0;
1386 if (XEXP (src
, 0) != dest
|| !CONST_INT_P (XEXP (src
, 1)))
1387 new_reg_base_value
[regno
] = 0;
1390 new_reg_base_value
[regno
] = 0;
1393 /* If this is the first set of a register, record the value. */
1394 else if ((regno
>= FIRST_PSEUDO_REGISTER
|| ! fixed_regs
[regno
])
1395 && ! bitmap_bit_p (reg_seen
, regno
) && new_reg_base_value
[regno
] == 0)
1396 new_reg_base_value
[regno
] = find_base_value (src
);
1398 bitmap_set_bit (reg_seen
, regno
);
1401 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1402 using hard registers with non-null REG_BASE_VALUE for renaming. */
1404 get_reg_base_value (unsigned int regno
)
1406 return (*reg_base_value
)[regno
];
1409 /* If a value is known for REGNO, return it. */
1412 get_reg_known_value (unsigned int regno
)
1414 if (regno
>= FIRST_PSEUDO_REGISTER
)
1416 regno
-= FIRST_PSEUDO_REGISTER
;
1417 if (regno
< vec_safe_length (reg_known_value
))
1418 return (*reg_known_value
)[regno
];
1426 set_reg_known_value (unsigned int regno
, rtx val
)
1428 if (regno
>= FIRST_PSEUDO_REGISTER
)
1430 regno
-= FIRST_PSEUDO_REGISTER
;
1431 if (regno
< vec_safe_length (reg_known_value
))
1432 (*reg_known_value
)[regno
] = val
;
1436 /* Similarly for reg_known_equiv_p. */
1439 get_reg_known_equiv_p (unsigned int regno
)
1441 if (regno
>= FIRST_PSEUDO_REGISTER
)
1443 regno
-= FIRST_PSEUDO_REGISTER
;
1444 if (regno
< vec_safe_length (reg_known_value
))
1445 return bitmap_bit_p (reg_known_equiv_p
, regno
);
1451 set_reg_known_equiv_p (unsigned int regno
, bool val
)
1453 if (regno
>= FIRST_PSEUDO_REGISTER
)
1455 regno
-= FIRST_PSEUDO_REGISTER
;
1456 if (regno
< vec_safe_length (reg_known_value
))
1459 bitmap_set_bit (reg_known_equiv_p
, regno
);
1461 bitmap_clear_bit (reg_known_equiv_p
, regno
);
1467 /* Returns a canonical version of X, from the point of view alias
1468 analysis. (For example, if X is a MEM whose address is a register,
1469 and the register has a known value (say a SYMBOL_REF), then a MEM
1470 whose address is the SYMBOL_REF is returned.) */
1475 /* Recursively look for equivalences. */
1476 if (REG_P (x
) && REGNO (x
) >= FIRST_PSEUDO_REGISTER
)
1478 rtx t
= get_reg_known_value (REGNO (x
));
1482 return canon_rtx (t
);
1485 if (GET_CODE (x
) == PLUS
)
1487 rtx x0
= canon_rtx (XEXP (x
, 0));
1488 rtx x1
= canon_rtx (XEXP (x
, 1));
1490 if (x0
!= XEXP (x
, 0) || x1
!= XEXP (x
, 1))
1492 if (CONST_INT_P (x0
))
1493 return plus_constant (GET_MODE (x
), x1
, INTVAL (x0
));
1494 else if (CONST_INT_P (x1
))
1495 return plus_constant (GET_MODE (x
), x0
, INTVAL (x1
));
1496 return gen_rtx_PLUS (GET_MODE (x
), x0
, x1
);
1500 /* This gives us much better alias analysis when called from
1501 the loop optimizer. Note we want to leave the original
1502 MEM alone, but need to return the canonicalized MEM with
1503 all the flags with their original values. */
1505 x
= replace_equiv_address_nv (x
, canon_rtx (XEXP (x
, 0)));
1510 /* Return 1 if X and Y are identical-looking rtx's.
1511 Expect that X and Y has been already canonicalized.
1513 We use the data in reg_known_value above to see if two registers with
1514 different numbers are, in fact, equivalent. */
1517 rtx_equal_for_memref_p (const_rtx x
, const_rtx y
)
1524 if (x
== 0 && y
== 0)
1526 if (x
== 0 || y
== 0)
1532 code
= GET_CODE (x
);
1533 /* Rtx's of different codes cannot be equal. */
1534 if (code
!= GET_CODE (y
))
1537 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1538 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1540 if (GET_MODE (x
) != GET_MODE (y
))
1543 /* Some RTL can be compared without a recursive examination. */
1547 return REGNO (x
) == REGNO (y
);
1550 return LABEL_REF_LABEL (x
) == LABEL_REF_LABEL (y
);
1553 return XSTR (x
, 0) == XSTR (y
, 0);
1556 /* This is magic, don't go through canonicalization et al. */
1557 return rtx_equal_p (ENTRY_VALUE_EXP (x
), ENTRY_VALUE_EXP (y
));
1561 /* Pointer equality guarantees equality for these nodes. */
1568 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1570 return ((rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 0))
1571 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 1)))
1572 || (rtx_equal_for_memref_p (XEXP (x
, 0), XEXP (y
, 1))
1573 && rtx_equal_for_memref_p (XEXP (x
, 1), XEXP (y
, 0))));
1574 /* For commutative operations, the RTX match if the operand match in any
1575 order. Also handle the simple binary and unary cases without a loop. */
1576 if (COMMUTATIVE_P (x
))
1578 rtx xop0
= canon_rtx (XEXP (x
, 0));
1579 rtx yop0
= canon_rtx (XEXP (y
, 0));
1580 rtx yop1
= canon_rtx (XEXP (y
, 1));
1582 return ((rtx_equal_for_memref_p (xop0
, yop0
)
1583 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop1
))
1584 || (rtx_equal_for_memref_p (xop0
, yop1
)
1585 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)), yop0
)));
1587 else if (NON_COMMUTATIVE_P (x
))
1589 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1590 canon_rtx (XEXP (y
, 0)))
1591 && rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 1)),
1592 canon_rtx (XEXP (y
, 1))));
1594 else if (UNARY_P (x
))
1595 return rtx_equal_for_memref_p (canon_rtx (XEXP (x
, 0)),
1596 canon_rtx (XEXP (y
, 0)));
1598 /* Compare the elements. If any pair of corresponding elements
1599 fail to match, return 0 for the whole things.
1601 Limit cases to types which actually appear in addresses. */
1603 fmt
= GET_RTX_FORMAT (code
);
1604 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1609 if (XINT (x
, i
) != XINT (y
, i
))
1614 /* Two vectors must have the same length. */
1615 if (XVECLEN (x
, i
) != XVECLEN (y
, i
))
1618 /* And the corresponding elements must match. */
1619 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1620 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x
, i
, j
)),
1621 canon_rtx (XVECEXP (y
, i
, j
))) == 0)
1626 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x
, i
)),
1627 canon_rtx (XEXP (y
, i
))) == 0)
1631 /* This can happen for asm operands. */
1633 if (strcmp (XSTR (x
, i
), XSTR (y
, i
)))
1637 /* This can happen for an asm which clobbers memory. */
1641 /* It is believed that rtx's at this level will never
1642 contain anything but integers and other rtx's,
1643 except for within LABEL_REFs and SYMBOL_REFs. */
1652 find_base_term (rtx x
)
1655 struct elt_loc_list
*l
, *f
;
1658 #if defined (FIND_BASE_TERM)
1659 /* Try machine-dependent ways to find the base term. */
1660 x
= FIND_BASE_TERM (x
);
1663 switch (GET_CODE (x
))
1666 return REG_BASE_VALUE (x
);
1669 /* As we do not know which address space the pointer is referring to, we can
1670 handle this only if the target does not support different pointer or
1671 address modes depending on the address space. */
1672 if (!target_default_pointer_address_modes_p ())
1674 if (GET_MODE_SIZE (GET_MODE (x
)) < GET_MODE_SIZE (Pmode
))
1684 return find_base_term (XEXP (x
, 0));
1687 case SIGN_EXTEND
: /* Used for Alpha/NT pointers */
1688 /* As we do not know which address space the pointer is referring to, we can
1689 handle this only if the target does not support different pointer or
1690 address modes depending on the address space. */
1691 if (!target_default_pointer_address_modes_p ())
1695 rtx temp
= find_base_term (XEXP (x
, 0));
1697 if (temp
!= 0 && CONSTANT_P (temp
))
1698 temp
= convert_memory_address (Pmode
, temp
);
1704 val
= CSELIB_VAL_PTR (x
);
1710 if (cselib_sp_based_value_p (val
))
1711 return static_reg_base_value
[STACK_POINTER_REGNUM
];
1714 /* Temporarily reset val->locs to avoid infinite recursion. */
1717 for (l
= f
; l
; l
= l
->next
)
1718 if (GET_CODE (l
->loc
) == VALUE
1719 && CSELIB_VAL_PTR (l
->loc
)->locs
1720 && !CSELIB_VAL_PTR (l
->loc
)->locs
->next
1721 && CSELIB_VAL_PTR (l
->loc
)->locs
->loc
== x
)
1723 else if ((ret
= find_base_term (l
->loc
)) != 0)
1730 /* The standard form is (lo_sum reg sym) so look only at the
1732 return find_base_term (XEXP (x
, 1));
1736 if (GET_CODE (x
) != PLUS
&& GET_CODE (x
) != MINUS
)
1742 rtx tmp1
= XEXP (x
, 0);
1743 rtx tmp2
= XEXP (x
, 1);
1745 /* This is a little bit tricky since we have to determine which of
1746 the two operands represents the real base address. Otherwise this
1747 routine may return the index register instead of the base register.
1749 That may cause us to believe no aliasing was possible, when in
1750 fact aliasing is possible.
1752 We use a few simple tests to guess the base register. Additional
1753 tests can certainly be added. For example, if one of the operands
1754 is a shift or multiply, then it must be the index register and the
1755 other operand is the base register. */
1757 if (tmp1
== pic_offset_table_rtx
&& CONSTANT_P (tmp2
))
1758 return find_base_term (tmp2
);
1760 /* If either operand is known to be a pointer, then prefer it
1761 to determine the base term. */
1762 if (REG_P (tmp1
) && REG_POINTER (tmp1
))
1764 else if (REG_P (tmp2
) && REG_POINTER (tmp2
))
1765 std::swap (tmp1
, tmp2
);
1766 /* If second argument is constant which has base term, prefer it
1767 over variable tmp1. See PR64025. */
1768 else if (CONSTANT_P (tmp2
) && !CONST_INT_P (tmp2
))
1769 std::swap (tmp1
, tmp2
);
1771 /* Go ahead and find the base term for both operands. If either base
1772 term is from a pointer or is a named object or a special address
1773 (like an argument or stack reference), then use it for the
1775 rtx base
= find_base_term (tmp1
);
1776 if (base
!= NULL_RTX
1777 && ((REG_P (tmp1
) && REG_POINTER (tmp1
))
1778 || known_base_value_p (base
)))
1780 base
= find_base_term (tmp2
);
1781 if (base
!= NULL_RTX
1782 && ((REG_P (tmp2
) && REG_POINTER (tmp2
))
1783 || known_base_value_p (base
)))
1786 /* We could not determine which of the two operands was the
1787 base register and which was the index. So we can determine
1788 nothing from the base alias check. */
1793 if (CONST_INT_P (XEXP (x
, 1)) && INTVAL (XEXP (x
, 1)) != 0)
1794 return find_base_term (XEXP (x
, 0));
1806 /* Return true if accesses to address X may alias accesses based
1807 on the stack pointer. */
1810 may_be_sp_based_p (rtx x
)
1812 rtx base
= find_base_term (x
);
1813 return !base
|| base
== static_reg_base_value
[STACK_POINTER_REGNUM
];
1816 /* Return 0 if the addresses X and Y are known to point to different
1817 objects, 1 if they might be pointers to the same object. */
1820 base_alias_check (rtx x
, rtx x_base
, rtx y
, rtx y_base
,
1821 machine_mode x_mode
, machine_mode y_mode
)
1823 /* If the address itself has no known base see if a known equivalent
1824 value has one. If either address still has no known base, nothing
1825 is known about aliasing. */
1830 if (! flag_expensive_optimizations
|| (x_c
= canon_rtx (x
)) == x
)
1833 x_base
= find_base_term (x_c
);
1841 if (! flag_expensive_optimizations
|| (y_c
= canon_rtx (y
)) == y
)
1844 y_base
= find_base_term (y_c
);
1849 /* If the base addresses are equal nothing is known about aliasing. */
1850 if (rtx_equal_p (x_base
, y_base
))
1853 /* The base addresses are different expressions. If they are not accessed
1854 via AND, there is no conflict. We can bring knowledge of object
1855 alignment into play here. For example, on alpha, "char a, b;" can
1856 alias one another, though "char a; long b;" cannot. AND addesses may
1857 implicitly alias surrounding objects; i.e. unaligned access in DImode
1858 via AND address can alias all surrounding object types except those
1859 with aligment 8 or higher. */
1860 if (GET_CODE (x
) == AND
&& GET_CODE (y
) == AND
)
1862 if (GET_CODE (x
) == AND
1863 && (!CONST_INT_P (XEXP (x
, 1))
1864 || (int) GET_MODE_UNIT_SIZE (y_mode
) < -INTVAL (XEXP (x
, 1))))
1866 if (GET_CODE (y
) == AND
1867 && (!CONST_INT_P (XEXP (y
, 1))
1868 || (int) GET_MODE_UNIT_SIZE (x_mode
) < -INTVAL (XEXP (y
, 1))))
1871 /* Differing symbols not accessed via AND never alias. */
1872 if (GET_CODE (x_base
) != ADDRESS
&& GET_CODE (y_base
) != ADDRESS
)
1875 if (unique_base_value_p (x_base
) || unique_base_value_p (y_base
))
1881 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1885 refs_newer_value_p (const_rtx expr
, rtx v
)
1887 int minuid
= CSELIB_VAL_PTR (v
)->uid
;
1888 subrtx_iterator::array_type array
;
1889 FOR_EACH_SUBRTX (iter
, array
, expr
, NONCONST
)
1890 if (GET_CODE (*iter
) == VALUE
&& CSELIB_VAL_PTR (*iter
)->uid
> minuid
)
1895 /* Convert the address X into something we can use. This is done by returning
1896 it unchanged unless it is a value; in the latter case we call cselib to get
1897 a more useful rtx. */
1903 struct elt_loc_list
*l
;
1905 if (GET_CODE (x
) != VALUE
)
1907 v
= CSELIB_VAL_PTR (x
);
1910 bool have_equivs
= cselib_have_permanent_equivalences ();
1912 v
= canonical_cselib_val (v
);
1913 for (l
= v
->locs
; l
; l
= l
->next
)
1914 if (CONSTANT_P (l
->loc
))
1916 for (l
= v
->locs
; l
; l
= l
->next
)
1917 if (!REG_P (l
->loc
) && !MEM_P (l
->loc
)
1918 /* Avoid infinite recursion when potentially dealing with
1919 var-tracking artificial equivalences, by skipping the
1920 equivalences themselves, and not choosing expressions
1921 that refer to newer VALUEs. */
1923 || (GET_CODE (l
->loc
) != VALUE
1924 && !refs_newer_value_p (l
->loc
, x
))))
1928 for (l
= v
->locs
; l
; l
= l
->next
)
1930 || (GET_CODE (l
->loc
) != VALUE
1931 && !refs_newer_value_p (l
->loc
, x
)))
1933 /* Return the canonical value. */
1937 return v
->locs
->loc
;
1942 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1943 where SIZE is the size in bytes of the memory reference. If ADDR
1944 is not modified by the memory reference then ADDR is returned. */
1947 addr_side_effect_eval (rtx addr
, int size
, int n_refs
)
1951 switch (GET_CODE (addr
))
1954 offset
= (n_refs
+ 1) * size
;
1957 offset
= -(n_refs
+ 1) * size
;
1960 offset
= n_refs
* size
;
1963 offset
= -n_refs
* size
;
1971 addr
= gen_rtx_PLUS (GET_MODE (addr
), XEXP (addr
, 0),
1972 gen_int_mode (offset
, GET_MODE (addr
)));
1974 addr
= XEXP (addr
, 0);
1975 addr
= canon_rtx (addr
);
1980 /* Return TRUE if an object X sized at XSIZE bytes and another object
1981 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1982 any of the sizes is zero, assume an overlap, otherwise use the
1983 absolute value of the sizes as the actual sizes. */
1986 offset_overlap_p (HOST_WIDE_INT c
, int xsize
, int ysize
)
1988 return (xsize
== 0 || ysize
== 0
1991 : (abs (ysize
) > -c
)));
1994 /* Return one if X and Y (memory addresses) reference the
1995 same location in memory or if the references overlap.
1996 Return zero if they do not overlap, else return
1997 minus one in which case they still might reference the same location.
1999 C is an offset accumulator. When
2000 C is nonzero, we are testing aliases between X and Y + C.
2001 XSIZE is the size in bytes of the X reference,
2002 similarly YSIZE is the size in bytes for Y.
2003 Expect that canon_rtx has been already called for X and Y.
2005 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2006 referenced (the reference was BLKmode), so make the most pessimistic
2009 If XSIZE or YSIZE is negative, we may access memory outside the object
2010 being referenced as a side effect. This can happen when using AND to
2011 align memory references, as is done on the Alpha.
2013 Nice to notice that varying addresses cannot conflict with fp if no
2014 local variables had their addresses taken, but that's too hard now.
2016 ??? Contrary to the tree alias oracle this does not return
2017 one for X + non-constant and Y + non-constant when X and Y are equal.
2018 If that is fixed the TBAA hack for union type-punning can be removed. */
2021 memrefs_conflict_p (int xsize
, rtx x
, int ysize
, rtx y
, HOST_WIDE_INT c
)
2023 if (GET_CODE (x
) == VALUE
)
2027 struct elt_loc_list
*l
= NULL
;
2028 if (CSELIB_VAL_PTR (x
))
2029 for (l
= canonical_cselib_val (CSELIB_VAL_PTR (x
))->locs
;
2031 if (REG_P (l
->loc
) && rtx_equal_for_memref_p (l
->loc
, y
))
2038 /* Don't call get_addr if y is the same VALUE. */
2042 if (GET_CODE (y
) == VALUE
)
2046 struct elt_loc_list
*l
= NULL
;
2047 if (CSELIB_VAL_PTR (y
))
2048 for (l
= canonical_cselib_val (CSELIB_VAL_PTR (y
))->locs
;
2050 if (REG_P (l
->loc
) && rtx_equal_for_memref_p (l
->loc
, x
))
2057 /* Don't call get_addr if x is the same VALUE. */
2061 if (GET_CODE (x
) == HIGH
)
2063 else if (GET_CODE (x
) == LO_SUM
)
2066 x
= addr_side_effect_eval (x
, abs (xsize
), 0);
2067 if (GET_CODE (y
) == HIGH
)
2069 else if (GET_CODE (y
) == LO_SUM
)
2072 y
= addr_side_effect_eval (y
, abs (ysize
), 0);
2074 if (rtx_equal_for_memref_p (x
, y
))
2076 return offset_overlap_p (c
, xsize
, ysize
);
2079 /* This code used to check for conflicts involving stack references and
2080 globals but the base address alias code now handles these cases. */
2082 if (GET_CODE (x
) == PLUS
)
2084 /* The fact that X is canonicalized means that this
2085 PLUS rtx is canonicalized. */
2086 rtx x0
= XEXP (x
, 0);
2087 rtx x1
= XEXP (x
, 1);
2089 if (GET_CODE (y
) == PLUS
)
2091 /* The fact that Y is canonicalized means that this
2092 PLUS rtx is canonicalized. */
2093 rtx y0
= XEXP (y
, 0);
2094 rtx y1
= XEXP (y
, 1);
2096 if (rtx_equal_for_memref_p (x1
, y1
))
2097 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
2098 if (rtx_equal_for_memref_p (x0
, y0
))
2099 return memrefs_conflict_p (xsize
, x1
, ysize
, y1
, c
);
2100 if (CONST_INT_P (x1
))
2102 if (CONST_INT_P (y1
))
2103 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
,
2104 c
- INTVAL (x1
) + INTVAL (y1
));
2106 return memrefs_conflict_p (xsize
, x0
, ysize
, y
,
2109 else if (CONST_INT_P (y1
))
2110 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
2114 else if (CONST_INT_P (x1
))
2115 return memrefs_conflict_p (xsize
, x0
, ysize
, y
, c
- INTVAL (x1
));
2117 else if (GET_CODE (y
) == PLUS
)
2119 /* The fact that Y is canonicalized means that this
2120 PLUS rtx is canonicalized. */
2121 rtx y0
= XEXP (y
, 0);
2122 rtx y1
= XEXP (y
, 1);
2124 if (CONST_INT_P (y1
))
2125 return memrefs_conflict_p (xsize
, x
, ysize
, y0
, c
+ INTVAL (y1
));
2130 if (GET_CODE (x
) == GET_CODE (y
))
2131 switch (GET_CODE (x
))
2135 /* Handle cases where we expect the second operands to be the
2136 same, and check only whether the first operand would conflict
2139 rtx x1
= canon_rtx (XEXP (x
, 1));
2140 rtx y1
= canon_rtx (XEXP (y
, 1));
2141 if (! rtx_equal_for_memref_p (x1
, y1
))
2143 x0
= canon_rtx (XEXP (x
, 0));
2144 y0
= canon_rtx (XEXP (y
, 0));
2145 if (rtx_equal_for_memref_p (x0
, y0
))
2146 return offset_overlap_p (c
, xsize
, ysize
);
2148 /* Can't properly adjust our sizes. */
2149 if (!CONST_INT_P (x1
))
2151 xsize
/= INTVAL (x1
);
2152 ysize
/= INTVAL (x1
);
2154 return memrefs_conflict_p (xsize
, x0
, ysize
, y0
, c
);
2161 /* Deal with alignment ANDs by adjusting offset and size so as to
2162 cover the maximum range, without taking any previously known
2163 alignment into account. Make a size negative after such an
2164 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2165 assume a potential overlap, because they may end up in contiguous
2166 memory locations and the stricter-alignment access may span over
2168 if (GET_CODE (x
) == AND
&& CONST_INT_P (XEXP (x
, 1)))
2170 HOST_WIDE_INT sc
= INTVAL (XEXP (x
, 1));
2171 unsigned HOST_WIDE_INT uc
= sc
;
2172 if (sc
< 0 && -uc
== (uc
& -uc
))
2179 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2183 if (GET_CODE (y
) == AND
&& CONST_INT_P (XEXP (y
, 1)))
2185 HOST_WIDE_INT sc
= INTVAL (XEXP (y
, 1));
2186 unsigned HOST_WIDE_INT uc
= sc
;
2187 if (sc
< 0 && -uc
== (uc
& -uc
))
2194 return memrefs_conflict_p (xsize
, x
,
2195 ysize
, canon_rtx (XEXP (y
, 0)), c
);
2201 if (CONST_INT_P (x
) && CONST_INT_P (y
))
2203 c
+= (INTVAL (y
) - INTVAL (x
));
2204 return offset_overlap_p (c
, xsize
, ysize
);
2207 if (GET_CODE (x
) == CONST
)
2209 if (GET_CODE (y
) == CONST
)
2210 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2211 ysize
, canon_rtx (XEXP (y
, 0)), c
);
2213 return memrefs_conflict_p (xsize
, canon_rtx (XEXP (x
, 0)),
2216 if (GET_CODE (y
) == CONST
)
2217 return memrefs_conflict_p (xsize
, x
, ysize
,
2218 canon_rtx (XEXP (y
, 0)), c
);
2220 /* Assume a potential overlap for symbolic addresses that went
2221 through alignment adjustments (i.e., that have negative
2222 sizes), because we can't know how far they are from each
2225 return (xsize
< 0 || ysize
< 0 || offset_overlap_p (c
, xsize
, ysize
));
2233 /* Functions to compute memory dependencies.
2235 Since we process the insns in execution order, we can build tables
2236 to keep track of what registers are fixed (and not aliased), what registers
2237 are varying in known ways, and what registers are varying in unknown
2240 If both memory references are volatile, then there must always be a
2241 dependence between the two references, since their order can not be
2242 changed. A volatile and non-volatile reference can be interchanged
2245 We also must allow AND addresses, because they may generate accesses
2246 outside the object being referenced. This is used to generate aligned
2247 addresses from unaligned addresses, for instance, the alpha
2248 storeqi_unaligned pattern. */
2250 /* Read dependence: X is read after read in MEM takes place. There can
2251 only be a dependence here if both reads are volatile, or if either is
2252 an explicit barrier. */
2255 read_dependence (const_rtx mem
, const_rtx x
)
2257 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2259 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2260 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2265 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2268 decl_for_component_ref (tree x
)
2272 x
= TREE_OPERAND (x
, 0);
2274 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2276 return x
&& DECL_P (x
) ? x
: NULL_TREE
;
2279 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2280 for the offset of the field reference. *KNOWN_P says whether the
2284 adjust_offset_for_component_ref (tree x
, bool *known_p
,
2285 HOST_WIDE_INT
*offset
)
2291 tree xoffset
= component_ref_field_offset (x
);
2292 tree field
= TREE_OPERAND (x
, 1);
2293 if (TREE_CODE (xoffset
) != INTEGER_CST
)
2300 = (wi::to_offset (xoffset
)
2301 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field
)),
2302 LOG2_BITS_PER_UNIT
));
2303 if (!wi::fits_uhwi_p (woffset
))
2308 *offset
+= woffset
.to_uhwi ();
2310 x
= TREE_OPERAND (x
, 0);
2312 while (x
&& TREE_CODE (x
) == COMPONENT_REF
);
2315 /* Return nonzero if we can determine the exprs corresponding to memrefs
2316 X and Y and they do not overlap.
2317 If LOOP_VARIANT is set, skip offset-based disambiguation */
2320 nonoverlapping_memrefs_p (const_rtx x
, const_rtx y
, bool loop_invariant
)
2322 tree exprx
= MEM_EXPR (x
), expry
= MEM_EXPR (y
);
2325 bool moffsetx_known_p
, moffsety_known_p
;
2326 HOST_WIDE_INT moffsetx
= 0, moffsety
= 0;
2327 HOST_WIDE_INT offsetx
= 0, offsety
= 0, sizex
, sizey
, tem
;
2329 /* Unless both have exprs, we can't tell anything. */
2330 if (exprx
== 0 || expry
== 0)
2333 /* For spill-slot accesses make sure we have valid offsets. */
2334 if ((exprx
== get_spill_slot_decl (false)
2335 && ! MEM_OFFSET_KNOWN_P (x
))
2336 || (expry
== get_spill_slot_decl (false)
2337 && ! MEM_OFFSET_KNOWN_P (y
)))
2340 /* If the field reference test failed, look at the DECLs involved. */
2341 moffsetx_known_p
= MEM_OFFSET_KNOWN_P (x
);
2342 if (moffsetx_known_p
)
2343 moffsetx
= MEM_OFFSET (x
);
2344 if (TREE_CODE (exprx
) == COMPONENT_REF
)
2346 tree t
= decl_for_component_ref (exprx
);
2349 adjust_offset_for_component_ref (exprx
, &moffsetx_known_p
, &moffsetx
);
2353 moffsety_known_p
= MEM_OFFSET_KNOWN_P (y
);
2354 if (moffsety_known_p
)
2355 moffsety
= MEM_OFFSET (y
);
2356 if (TREE_CODE (expry
) == COMPONENT_REF
)
2358 tree t
= decl_for_component_ref (expry
);
2361 adjust_offset_for_component_ref (expry
, &moffsety_known_p
, &moffsety
);
2365 if (! DECL_P (exprx
) || ! DECL_P (expry
))
2368 /* With invalid code we can end up storing into the constant pool.
2369 Bail out to avoid ICEing when creating RTL for this.
2370 See gfortran.dg/lto/20091028-2_0.f90. */
2371 if (TREE_CODE (exprx
) == CONST_DECL
2372 || TREE_CODE (expry
) == CONST_DECL
)
2375 rtlx
= DECL_RTL (exprx
);
2376 rtly
= DECL_RTL (expry
);
2378 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2379 can't overlap unless they are the same because we never reuse that part
2380 of the stack frame used for locals for spilled pseudos. */
2381 if ((!MEM_P (rtlx
) || !MEM_P (rtly
))
2382 && ! rtx_equal_p (rtlx
, rtly
))
2385 /* If we have MEMs referring to different address spaces (which can
2386 potentially overlap), we cannot easily tell from the addresses
2387 whether the references overlap. */
2388 if (MEM_P (rtlx
) && MEM_P (rtly
)
2389 && MEM_ADDR_SPACE (rtlx
) != MEM_ADDR_SPACE (rtly
))
2392 /* Get the base and offsets of both decls. If either is a register, we
2393 know both are and are the same, so use that as the base. The only
2394 we can avoid overlap is if we can deduce that they are nonoverlapping
2395 pieces of that decl, which is very rare. */
2396 basex
= MEM_P (rtlx
) ? XEXP (rtlx
, 0) : rtlx
;
2397 if (GET_CODE (basex
) == PLUS
&& CONST_INT_P (XEXP (basex
, 1)))
2398 offsetx
= INTVAL (XEXP (basex
, 1)), basex
= XEXP (basex
, 0);
2400 basey
= MEM_P (rtly
) ? XEXP (rtly
, 0) : rtly
;
2401 if (GET_CODE (basey
) == PLUS
&& CONST_INT_P (XEXP (basey
, 1)))
2402 offsety
= INTVAL (XEXP (basey
, 1)), basey
= XEXP (basey
, 0);
2404 /* If the bases are different, we know they do not overlap if both
2405 are constants or if one is a constant and the other a pointer into the
2406 stack frame. Otherwise a different base means we can't tell if they
2408 if (! rtx_equal_p (basex
, basey
))
2409 return ((CONSTANT_P (basex
) && CONSTANT_P (basey
))
2410 || (CONSTANT_P (basex
) && REG_P (basey
)
2411 && REGNO_PTR_FRAME_P (REGNO (basey
)))
2412 || (CONSTANT_P (basey
) && REG_P (basex
)
2413 && REGNO_PTR_FRAME_P (REGNO (basex
))));
2415 /* Offset based disambiguation not appropriate for loop invariant */
2419 sizex
= (!MEM_P (rtlx
) ? (int) GET_MODE_SIZE (GET_MODE (rtlx
))
2420 : MEM_SIZE_KNOWN_P (rtlx
) ? MEM_SIZE (rtlx
)
2422 sizey
= (!MEM_P (rtly
) ? (int) GET_MODE_SIZE (GET_MODE (rtly
))
2423 : MEM_SIZE_KNOWN_P (rtly
) ? MEM_SIZE (rtly
)
2426 /* If we have an offset for either memref, it can update the values computed
2428 if (moffsetx_known_p
)
2429 offsetx
+= moffsetx
, sizex
-= moffsetx
;
2430 if (moffsety_known_p
)
2431 offsety
+= moffsety
, sizey
-= moffsety
;
2433 /* If a memref has both a size and an offset, we can use the smaller size.
2434 We can't do this if the offset isn't known because we must view this
2435 memref as being anywhere inside the DECL's MEM. */
2436 if (MEM_SIZE_KNOWN_P (x
) && moffsetx_known_p
)
2437 sizex
= MEM_SIZE (x
);
2438 if (MEM_SIZE_KNOWN_P (y
) && moffsety_known_p
)
2439 sizey
= MEM_SIZE (y
);
2441 /* Put the values of the memref with the lower offset in X's values. */
2442 if (offsetx
> offsety
)
2444 tem
= offsetx
, offsetx
= offsety
, offsety
= tem
;
2445 tem
= sizex
, sizex
= sizey
, sizey
= tem
;
2448 /* If we don't know the size of the lower-offset value, we can't tell
2449 if they conflict. Otherwise, we do the test. */
2450 return sizex
>= 0 && offsety
>= offsetx
+ sizex
;
2453 /* Helper for true_dependence and canon_true_dependence.
2454 Checks for true dependence: X is read after store in MEM takes place.
2456 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2457 NULL_RTX, and the canonical addresses of MEM and X are both computed
2458 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2460 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2462 Returns 1 if there is a true dependence, 0 otherwise. */
2465 true_dependence_1 (const_rtx mem
, machine_mode mem_mode
, rtx mem_addr
,
2466 const_rtx x
, rtx x_addr
, bool mem_canonicalized
)
2472 gcc_checking_assert (mem_canonicalized
? (mem_addr
!= NULL_RTX
)
2473 : (mem_addr
== NULL_RTX
&& x_addr
== NULL_RTX
));
2475 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2478 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2479 This is used in epilogue deallocation functions, and in cselib. */
2480 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2482 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2484 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2485 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2489 x_addr
= XEXP (x
, 0);
2490 x_addr
= get_addr (x_addr
);
2494 mem_addr
= XEXP (mem
, 0);
2495 if (mem_mode
== VOIDmode
)
2496 mem_mode
= GET_MODE (mem
);
2498 true_mem_addr
= get_addr (mem_addr
);
2500 /* Read-only memory is by definition never modified, and therefore can't
2501 conflict with anything. However, don't assume anything when AND
2502 addresses are involved and leave to the code below to determine
2503 dependence. We don't expect to find read-only set on MEM, but
2504 stupid user tricks can produce them, so don't die. */
2505 if (MEM_READONLY_P (x
)
2506 && GET_CODE (x_addr
) != AND
2507 && GET_CODE (true_mem_addr
) != AND
)
2510 /* If we have MEMs referring to different address spaces (which can
2511 potentially overlap), we cannot easily tell from the addresses
2512 whether the references overlap. */
2513 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2516 base
= find_base_term (x_addr
);
2517 if (base
&& (GET_CODE (base
) == LABEL_REF
2518 || (GET_CODE (base
) == SYMBOL_REF
2519 && CONSTANT_POOL_ADDRESS_P (base
))))
2522 rtx mem_base
= find_base_term (true_mem_addr
);
2523 if (! base_alias_check (x_addr
, base
, true_mem_addr
, mem_base
,
2524 GET_MODE (x
), mem_mode
))
2527 x_addr
= canon_rtx (x_addr
);
2528 if (!mem_canonicalized
)
2529 mem_addr
= canon_rtx (true_mem_addr
);
2531 if ((ret
= memrefs_conflict_p (GET_MODE_SIZE (mem_mode
), mem_addr
,
2532 SIZE_FOR_MODE (x
), x_addr
, 0)) != -1)
2535 if (mems_in_disjoint_alias_sets_p (x
, mem
))
2538 if (nonoverlapping_memrefs_p (mem
, x
, false))
2541 return rtx_refs_may_alias_p (x
, mem
, true);
2544 /* True dependence: X is read after store in MEM takes place. */
2547 true_dependence (const_rtx mem
, machine_mode mem_mode
, const_rtx x
)
2549 return true_dependence_1 (mem
, mem_mode
, NULL_RTX
,
2550 x
, NULL_RTX
, /*mem_canonicalized=*/false);
2553 /* Canonical true dependence: X is read after store in MEM takes place.
2554 Variant of true_dependence which assumes MEM has already been
2555 canonicalized (hence we no longer do that here).
2556 The mem_addr argument has been added, since true_dependence_1 computed
2557 this value prior to canonicalizing. */
2560 canon_true_dependence (const_rtx mem
, machine_mode mem_mode
, rtx mem_addr
,
2561 const_rtx x
, rtx x_addr
)
2563 return true_dependence_1 (mem
, mem_mode
, mem_addr
,
2564 x
, x_addr
, /*mem_canonicalized=*/true);
2567 /* Returns nonzero if a write to X might alias a previous read from
2568 (or, if WRITEP is true, a write to) MEM.
2569 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2570 and X_MODE the mode for that access.
2571 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2574 write_dependence_p (const_rtx mem
,
2575 const_rtx x
, machine_mode x_mode
, rtx x_addr
,
2576 bool mem_canonicalized
, bool x_canonicalized
, bool writep
)
2579 rtx true_mem_addr
, true_x_addr
;
2583 gcc_checking_assert (x_canonicalized
2584 ? (x_addr
!= NULL_RTX
&& x_mode
!= VOIDmode
)
2585 : (x_addr
== NULL_RTX
&& x_mode
== VOIDmode
));
2587 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2590 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2591 This is used in epilogue deallocation functions. */
2592 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2594 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2596 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2597 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2601 x_addr
= XEXP (x
, 0);
2602 true_x_addr
= get_addr (x_addr
);
2604 mem_addr
= XEXP (mem
, 0);
2605 true_mem_addr
= get_addr (mem_addr
);
2607 /* A read from read-only memory can't conflict with read-write memory.
2608 Don't assume anything when AND addresses are involved and leave to
2609 the code below to determine dependence. */
2611 && MEM_READONLY_P (mem
)
2612 && GET_CODE (true_x_addr
) != AND
2613 && GET_CODE (true_mem_addr
) != AND
)
2616 /* If we have MEMs referring to different address spaces (which can
2617 potentially overlap), we cannot easily tell from the addresses
2618 whether the references overlap. */
2619 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2622 base
= find_base_term (true_mem_addr
);
2625 && (GET_CODE (base
) == LABEL_REF
2626 || (GET_CODE (base
) == SYMBOL_REF
2627 && CONSTANT_POOL_ADDRESS_P (base
))))
2630 rtx x_base
= find_base_term (true_x_addr
);
2631 if (! base_alias_check (true_x_addr
, x_base
, true_mem_addr
, base
,
2632 GET_MODE (x
), GET_MODE (mem
)))
2635 if (!x_canonicalized
)
2637 x_addr
= canon_rtx (true_x_addr
);
2638 x_mode
= GET_MODE (x
);
2640 if (!mem_canonicalized
)
2641 mem_addr
= canon_rtx (true_mem_addr
);
2643 if ((ret
= memrefs_conflict_p (SIZE_FOR_MODE (mem
), mem_addr
,
2644 GET_MODE_SIZE (x_mode
), x_addr
, 0)) != -1)
2647 if (nonoverlapping_memrefs_p (x
, mem
, false))
2650 return rtx_refs_may_alias_p (x
, mem
, false);
2653 /* Anti dependence: X is written after read in MEM takes place. */
2656 anti_dependence (const_rtx mem
, const_rtx x
)
2658 return write_dependence_p (mem
, x
, VOIDmode
, NULL_RTX
,
2659 /*mem_canonicalized=*/false,
2660 /*x_canonicalized*/false, /*writep=*/false);
2663 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2664 Also, consider X in X_MODE (which might be from an enclosing
2665 STRICT_LOW_PART / ZERO_EXTRACT).
2666 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2669 canon_anti_dependence (const_rtx mem
, bool mem_canonicalized
,
2670 const_rtx x
, machine_mode x_mode
, rtx x_addr
)
2672 return write_dependence_p (mem
, x
, x_mode
, x_addr
,
2673 mem_canonicalized
, /*x_canonicalized=*/true,
2677 /* Output dependence: X is written after store in MEM takes place. */
2680 output_dependence (const_rtx mem
, const_rtx x
)
2682 return write_dependence_p (mem
, x
, VOIDmode
, NULL_RTX
,
2683 /*mem_canonicalized=*/false,
2684 /*x_canonicalized*/false, /*writep=*/true);
2689 /* Check whether X may be aliased with MEM. Don't do offset-based
2690 memory disambiguation & TBAA. */
2692 may_alias_p (const_rtx mem
, const_rtx x
)
2694 rtx x_addr
, mem_addr
;
2696 if (MEM_VOLATILE_P (x
) && MEM_VOLATILE_P (mem
))
2699 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2700 This is used in epilogue deallocation functions. */
2701 if (GET_MODE (x
) == BLKmode
&& GET_CODE (XEXP (x
, 0)) == SCRATCH
)
2703 if (GET_MODE (mem
) == BLKmode
&& GET_CODE (XEXP (mem
, 0)) == SCRATCH
)
2705 if (MEM_ALIAS_SET (x
) == ALIAS_SET_MEMORY_BARRIER
2706 || MEM_ALIAS_SET (mem
) == ALIAS_SET_MEMORY_BARRIER
)
2709 x_addr
= XEXP (x
, 0);
2710 x_addr
= get_addr (x_addr
);
2712 mem_addr
= XEXP (mem
, 0);
2713 mem_addr
= get_addr (mem_addr
);
2715 /* Read-only memory is by definition never modified, and therefore can't
2716 conflict with anything. However, don't assume anything when AND
2717 addresses are involved and leave to the code below to determine
2718 dependence. We don't expect to find read-only set on MEM, but
2719 stupid user tricks can produce them, so don't die. */
2720 if (MEM_READONLY_P (x
)
2721 && GET_CODE (x_addr
) != AND
2722 && GET_CODE (mem_addr
) != AND
)
2725 /* If we have MEMs referring to different address spaces (which can
2726 potentially overlap), we cannot easily tell from the addresses
2727 whether the references overlap. */
2728 if (MEM_ADDR_SPACE (mem
) != MEM_ADDR_SPACE (x
))
2731 rtx x_base
= find_base_term (x_addr
);
2732 rtx mem_base
= find_base_term (mem_addr
);
2733 if (! base_alias_check (x_addr
, x_base
, mem_addr
, mem_base
,
2734 GET_MODE (x
), GET_MODE (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 ();
2848 timevar_push (TV_ALIAS_ANALYSIS
);
2850 vec_safe_grow_cleared (reg_known_value
, maxreg
- FIRST_PSEUDO_REGISTER
);
2851 reg_known_equiv_p
= sbitmap_alloc (maxreg
- FIRST_PSEUDO_REGISTER
);
2852 bitmap_clear (reg_known_equiv_p
);
2854 /* If we have memory allocated from the previous run, use it. */
2855 if (old_reg_base_value
)
2856 reg_base_value
= old_reg_base_value
;
2859 reg_base_value
->truncate (0);
2861 vec_safe_grow_cleared (reg_base_value
, maxreg
);
2863 new_reg_base_value
= XNEWVEC (rtx
, maxreg
);
2864 reg_seen
= sbitmap_alloc (maxreg
);
2866 /* The basic idea is that each pass through this loop will use the
2867 "constant" information from the previous pass to propagate alias
2868 information through another level of assignments.
2870 The propagation is done on the CFG in reverse post-order, to propagate
2871 things forward as far as possible in each iteration.
2873 This could get expensive if the assignment chains are long. Maybe
2874 we should throttle the number of iterations, possibly based on
2875 the optimization level or flag_expensive_optimizations.
2877 We could propagate more information in the first pass by making use
2878 of DF_REG_DEF_COUNT to determine immediately that the alias information
2879 for a pseudo is "constant".
2881 A program with an uninitialized variable can cause an infinite loop
2882 here. Instead of doing a full dataflow analysis to detect such problems
2883 we just cap the number of iterations for the loop.
2885 The state of the arrays for the set chain in question does not matter
2886 since the program has undefined behavior. */
2888 rpo
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
2889 rpo_cnt
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
2894 /* Assume nothing will change this iteration of the loop. */
2897 /* We want to assign the same IDs each iteration of this loop, so
2898 start counting from one each iteration of the loop. */
2901 /* We're at the start of the function each iteration through the
2902 loop, so we're copying arguments. */
2903 copying_arguments
= true;
2905 /* Wipe the potential alias information clean for this pass. */
2906 memset (new_reg_base_value
, 0, maxreg
* sizeof (rtx
));
2908 /* Wipe the reg_seen array clean. */
2909 bitmap_clear (reg_seen
);
2911 /* Initialize the alias information for this pass. */
2912 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
2913 if (static_reg_base_value
[i
])
2915 new_reg_base_value
[i
] = static_reg_base_value
[i
];
2916 bitmap_set_bit (reg_seen
, i
);
2919 /* Walk the insns adding values to the new_reg_base_value array. */
2920 for (i
= 0; i
< rpo_cnt
; i
++)
2922 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[i
]);
2923 FOR_BB_INSNS (bb
, insn
)
2925 if (NONDEBUG_INSN_P (insn
))
2929 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2930 /* The prologue/epilogue insns are not threaded onto the
2931 insn chain until after reload has completed. Thus,
2932 there is no sense wasting time checking if INSN is in
2933 the prologue/epilogue until after reload has completed. */
2934 if (reload_completed
2935 && prologue_epilogue_contains (insn
))
2939 /* If this insn has a noalias note, process it, Otherwise,
2940 scan for sets. A simple set will have no side effects
2941 which could change the base value of any other register. */
2943 if (GET_CODE (PATTERN (insn
)) == SET
2944 && REG_NOTES (insn
) != 0
2945 && find_reg_note (insn
, REG_NOALIAS
, NULL_RTX
))
2946 record_set (SET_DEST (PATTERN (insn
)), NULL_RTX
, NULL
);
2948 note_stores (PATTERN (insn
), record_set
, NULL
);
2950 set
= single_set (insn
);
2953 && REG_P (SET_DEST (set
))
2954 && REGNO (SET_DEST (set
)) >= FIRST_PSEUDO_REGISTER
)
2956 unsigned int regno
= REGNO (SET_DEST (set
));
2957 rtx src
= SET_SRC (set
);
2960 note
= find_reg_equal_equiv_note (insn
);
2961 if (note
&& REG_NOTE_KIND (note
) == REG_EQUAL
2962 && DF_REG_DEF_COUNT (regno
) != 1)
2965 if (note
!= NULL_RTX
2966 && GET_CODE (XEXP (note
, 0)) != EXPR_LIST
2967 && ! rtx_varies_p (XEXP (note
, 0), 1)
2968 && ! reg_overlap_mentioned_p (SET_DEST (set
),
2971 set_reg_known_value (regno
, XEXP (note
, 0));
2972 set_reg_known_equiv_p (regno
,
2973 REG_NOTE_KIND (note
) == REG_EQUIV
);
2975 else if (DF_REG_DEF_COUNT (regno
) == 1
2976 && GET_CODE (src
) == PLUS
2977 && REG_P (XEXP (src
, 0))
2978 && (t
= get_reg_known_value (REGNO (XEXP (src
, 0))))
2979 && CONST_INT_P (XEXP (src
, 1)))
2981 t
= plus_constant (GET_MODE (src
), t
,
2982 INTVAL (XEXP (src
, 1)));
2983 set_reg_known_value (regno
, t
);
2984 set_reg_known_equiv_p (regno
, false);
2986 else if (DF_REG_DEF_COUNT (regno
) == 1
2987 && ! rtx_varies_p (src
, 1))
2989 set_reg_known_value (regno
, src
);
2990 set_reg_known_equiv_p (regno
, false);
2994 else if (NOTE_P (insn
)
2995 && NOTE_KIND (insn
) == NOTE_INSN_FUNCTION_BEG
)
2996 copying_arguments
= false;
3000 /* Now propagate values from new_reg_base_value to reg_base_value. */
3001 gcc_assert (maxreg
== (unsigned int) max_reg_num ());
3003 for (ui
= 0; ui
< maxreg
; ui
++)
3005 if (new_reg_base_value
[ui
]
3006 && new_reg_base_value
[ui
] != (*reg_base_value
)[ui
]
3007 && ! rtx_equal_p (new_reg_base_value
[ui
], (*reg_base_value
)[ui
]))
3009 (*reg_base_value
)[ui
] = new_reg_base_value
[ui
];
3014 while (changed
&& ++pass
< MAX_ALIAS_LOOP_PASSES
);
3017 /* Fill in the remaining entries. */
3018 FOR_EACH_VEC_ELT (*reg_known_value
, i
, val
)
3020 int regno
= i
+ FIRST_PSEUDO_REGISTER
;
3022 set_reg_known_value (regno
, regno_reg_rtx
[regno
]);
3026 free (new_reg_base_value
);
3027 new_reg_base_value
= 0;
3028 sbitmap_free (reg_seen
);
3030 timevar_pop (TV_ALIAS_ANALYSIS
);
3033 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3034 Special API for var-tracking pass purposes. */
3037 vt_equate_reg_base_value (const_rtx reg1
, const_rtx reg2
)
3039 (*reg_base_value
)[REGNO (reg1
)] = REG_BASE_VALUE (reg2
);
3043 end_alias_analysis (void)
3045 old_reg_base_value
= reg_base_value
;
3046 vec_free (reg_known_value
);
3047 sbitmap_free (reg_known_equiv_p
);
3050 #include "gt-alias.h"