libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / alias.cc
blob853e84d7439a2a27ad07682f209e8e42d11be813
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2024 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
10 version.
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
15 for more details.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "df.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple-ssa.h"
33 #include "emit-rtl.h"
34 #include "alias.h"
35 #include "fold-const.h"
36 #include "varasm.h"
37 #include "cselib.h"
38 #include "langhooks.h"
39 #include "cfganal.h"
40 #include "rtl-iter.h"
41 #include "cgraph.h"
42 #include "ipa-utils.h"
44 /* The aliasing API provided here solves related but different problems:
46 Say there exists (in c)
48 struct X {
49 struct Y y1;
50 struct Z z2;
51 } x1, *px1, *px2;
53 struct Y y2, *py;
54 struct Z z2, *pz;
57 py = &x1.y1;
58 px2 = &x1;
60 Consider the four questions:
62 Can a store to x1 interfere with px2->y1?
63 Can a store to x1 interfere with px2->z2?
64 Can a store to x1 change the value pointed to by with py?
65 Can a store to x1 change the value pointed to by with pz?
67 The answer to these questions can be yes, yes, yes, and maybe.
69 The first two questions can be answered with a simple examination
70 of the type system. If structure X contains a field of type Y then
71 a store through a pointer to an X can overwrite any field that is
72 contained (recursively) in an X (unless we know that px1 != px2).
74 The last two questions can be solved in the same way as the first
75 two questions but this is too conservative. The observation is
76 that in some cases we can know which (if any) fields are addressed
77 and if those addresses are used in bad ways. This analysis may be
78 language specific. In C, arbitrary operations may be applied to
79 pointers. However, there is some indication that this may be too
80 conservative for some C++ types.
82 The pass ipa-type-escape does this analysis for the types whose
83 instances do not escape across the compilation boundary.
85 Historically in GCC, these two problems were combined and a single
86 data structure that was used to represent the solution to these
87 problems. We now have two similar but different data structures,
88 The data structure to solve the last two questions is similar to
89 the first, but does not contain the fields whose address are never
90 taken. For types that do escape the compilation unit, the data
91 structures will have identical information.
94 /* The alias sets assigned to MEMs assist the back-end in determining
95 which MEMs can alias which other MEMs. In general, two MEMs in
96 different alias sets cannot alias each other, with one important
97 exception. Consider something like:
99 struct S { int i; double d; };
101 a store to an `S' can alias something of either type `int' or type
102 `double'. (However, a store to an `int' cannot alias a `double'
103 and vice versa.) We indicate this via a tree structure that looks
104 like:
105 struct S
108 |/_ _\|
109 int double
111 (The arrows are directed and point downwards.)
112 In this situation we say the alias set for `struct S' is the
113 `superset' and that those for `int' and `double' are `subsets'.
115 To see whether two alias sets can point to the same memory, we must
116 see if either alias set is a subset of the other. We need not trace
117 past immediate descendants, however, since we propagate all
118 grandchildren up one level.
120 Alias set zero is implicitly a superset of all other alias sets.
121 However, this is no actual entry for alias set zero. It is an
122 error to attempt to explicitly construct a subset of zero. */
124 struct alias_set_hash : int_hash <int, INT_MIN, INT_MIN + 1> {};
126 struct GTY(()) alias_set_entry {
127 /* The alias set number, as stored in MEM_ALIAS_SET. */
128 alias_set_type alias_set;
130 /* Nonzero if would have a child of zero: this effectively makes this
131 alias set the same as alias set zero. */
132 bool has_zero_child;
133 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
134 aggregate contaiing pointer.
135 This is used for a special case where we need an universal pointer type
136 compatible with all other pointer types. */
137 bool is_pointer;
138 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
139 bool has_pointer;
141 /* The children of the alias set. These are not just the immediate
142 children, but, in fact, all descendants. So, if we have:
144 struct T { struct S s; float f; }
146 continuing our example above, the children here will be all of
147 `int', `double', `float', and `struct S'. */
148 hash_map<alias_set_hash, int> *children;
151 static int compare_base_symbol_refs (const_rtx, const_rtx,
152 HOST_WIDE_INT * = NULL);
154 /* Query statistics for the different low-level disambiguators.
155 A high-level query may trigger multiple of them. */
157 static struct {
158 unsigned long long num_alias_zero;
159 unsigned long long num_same_alias_set;
160 unsigned long long num_same_objects;
161 unsigned long long num_volatile;
162 unsigned long long num_dag;
163 unsigned long long num_universal;
164 unsigned long long num_disambiguated;
165 } alias_stats;
168 /* Set up all info needed to perform alias analysis on memory references. */
170 /* Returns the size in bytes of the mode of X. */
171 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
173 /* Cap the number of passes we make over the insns propagating alias
174 information through set chains.
175 ??? 10 is a completely arbitrary choice. This should be based on the
176 maximum loop depth in the CFG, but we do not have this information
177 available (even if current_loops _is_ available). */
178 #define MAX_ALIAS_LOOP_PASSES 10
180 /* reg_base_value[N] gives an address to which register N is related.
181 If all sets after the first add or subtract to the current value
182 or otherwise modify it so it does not point to a different top level
183 object, reg_base_value[N] is equal to the address part of the source
184 of the first set.
186 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
187 expressions represent three types of base:
189 1. incoming arguments. There is just one ADDRESS to represent all
190 arguments, since we do not know at this level whether accesses
191 based on different arguments can alias. The ADDRESS has id 0.
193 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
194 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
195 Each of these rtxes has a separate ADDRESS associated with it,
196 each with a negative id.
198 GCC is (and is required to be) precise in which register it
199 chooses to access a particular region of stack. We can therefore
200 assume that accesses based on one of these rtxes do not alias
201 accesses based on another of these rtxes.
203 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
204 Each such piece of memory has a separate ADDRESS associated
205 with it, each with an id greater than 0.
207 Accesses based on one ADDRESS do not alias accesses based on other
208 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
209 alias globals either; the ADDRESSes have Pmode to indicate this.
210 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
211 indicate this. */
213 static GTY(()) vec<rtx, va_gc> *reg_base_value;
214 static rtx *new_reg_base_value;
216 /* The single VOIDmode ADDRESS that represents all argument bases.
217 It has id 0. */
218 static GTY(()) rtx arg_base_value;
220 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
221 static int unique_id;
223 /* We preserve the copy of old array around to avoid amount of garbage
224 produced. About 8% of garbage produced were attributed to this
225 array. */
226 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
228 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
229 registers. */
230 #define UNIQUE_BASE_VALUE_SP -1
231 #define UNIQUE_BASE_VALUE_ARGP -2
232 #define UNIQUE_BASE_VALUE_FP -3
233 #define UNIQUE_BASE_VALUE_HFP -4
235 #define static_reg_base_value \
236 (this_target_rtl->x_static_reg_base_value)
238 #define REG_BASE_VALUE(X) \
239 (REGNO (X) < vec_safe_length (reg_base_value) \
240 ? (*reg_base_value)[REGNO (X)] : 0)
242 /* Vector indexed by N giving the initial (unchanging) value known for
243 pseudo-register N. This vector is initialized in init_alias_analysis,
244 and does not change until end_alias_analysis is called. */
245 static GTY(()) vec<rtx, va_gc> *reg_known_value;
247 /* Vector recording for each reg_known_value whether it is due to a
248 REG_EQUIV note. Future passes (viz., reload) may replace the
249 pseudo with the equivalent expression and so we account for the
250 dependences that would be introduced if that happens.
252 The REG_EQUIV notes created in assign_parms may mention the arg
253 pointer, and there are explicit insns in the RTL that modify the
254 arg pointer. Thus we must ensure that such insns don't get
255 scheduled across each other because that would invalidate the
256 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
257 wrong, but solving the problem in the scheduler will likely give
258 better code, so we do it here. */
259 static sbitmap reg_known_equiv_p;
261 /* True when scanning insns from the start of the rtl to the
262 NOTE_INSN_FUNCTION_BEG note. */
263 static bool copying_arguments;
266 /* The splay-tree used to store the various alias set entries. */
267 static GTY (()) vec<alias_set_entry *, va_gc> *alias_sets;
269 /* Build a decomposed reference object for querying the alias-oracle
270 from the MEM rtx and store it in *REF.
271 Returns false if MEM is not suitable for the alias-oracle. */
273 static bool
274 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
276 tree expr = MEM_EXPR (mem);
277 tree base;
279 if (!expr)
280 return false;
282 ao_ref_init (ref, expr);
284 /* Get the base of the reference and see if we have to reject or
285 adjust it. */
286 base = ao_ref_base (ref);
287 if (base == NULL_TREE)
288 return false;
290 /* The tree oracle doesn't like bases that are neither decls
291 nor indirect references of SSA names. */
292 if (!(DECL_P (base)
293 || (TREE_CODE (base) == MEM_REF
294 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
295 || (TREE_CODE (base) == TARGET_MEM_REF
296 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
297 return false;
299 ref->ref_alias_set = MEM_ALIAS_SET (mem);
301 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
302 is conservative, so trust it. */
303 if (!MEM_OFFSET_KNOWN_P (mem)
304 || !MEM_SIZE_KNOWN_P (mem))
305 return true;
307 /* If MEM_OFFSET/MEM_SIZE get us outside of ref->offset/ref->max_size
308 drop ref->ref. */
309 if (maybe_lt (MEM_OFFSET (mem), 0)
310 || (ref->max_size_known_p ()
311 && maybe_gt ((MEM_OFFSET (mem) + MEM_SIZE (mem)) * BITS_PER_UNIT,
312 ref->max_size)))
313 ref->ref = NULL_TREE;
315 /* Refine size and offset we got from analyzing MEM_EXPR by using
316 MEM_SIZE and MEM_OFFSET. */
318 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
319 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
321 /* The MEM may extend into adjacent fields, so adjust max_size if
322 necessary. */
323 if (ref->max_size_known_p ())
324 ref->max_size = upper_bound (ref->max_size, ref->size);
326 /* If MEM_OFFSET and MEM_SIZE might get us outside of the base object of
327 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
328 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
329 && (maybe_lt (ref->offset, 0)
330 || (DECL_P (ref->base)
331 && (DECL_SIZE (ref->base) == NULL_TREE
332 || !poly_int_tree_p (DECL_SIZE (ref->base))
333 || maybe_lt (wi::to_poly_offset (DECL_SIZE (ref->base)),
334 ref->offset + ref->size)))))
335 return false;
337 return true;
340 /* Query the alias-oracle on whether the two memory rtx X and MEM may
341 alias. If TBAA_P is set also apply TBAA. Returns true if the
342 two rtxen may alias, false otherwise. */
344 static bool
345 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
347 ao_ref ref1, ref2;
349 if (!ao_ref_from_mem (&ref1, x)
350 || !ao_ref_from_mem (&ref2, mem))
351 return true;
353 return refs_may_alias_p_1 (&ref1, &ref2,
354 tbaa_p
355 && MEM_ALIAS_SET (x) != 0
356 && MEM_ALIAS_SET (mem) != 0);
359 /* Return true if the ref EARLIER behaves the same as LATER with respect
360 to TBAA for every memory reference that might follow LATER. */
362 bool
363 refs_same_for_tbaa_p (tree earlier, tree later)
365 ao_ref earlier_ref, later_ref;
366 ao_ref_init (&earlier_ref, earlier);
367 ao_ref_init (&later_ref, later);
368 alias_set_type earlier_set = ao_ref_alias_set (&earlier_ref);
369 alias_set_type later_set = ao_ref_alias_set (&later_ref);
370 if (!(earlier_set == later_set
371 || alias_set_subset_of (later_set, earlier_set)))
372 return false;
373 alias_set_type later_base_set = ao_ref_base_alias_set (&later_ref);
374 alias_set_type earlier_base_set = ao_ref_base_alias_set (&earlier_ref);
375 return (earlier_base_set == later_base_set
376 || alias_set_subset_of (later_base_set, earlier_base_set));
379 /* Similar to refs_same_for_tbaa_p() but for use on MEM rtxs. */
380 bool
381 mems_same_for_tbaa_p (rtx earlier, rtx later)
383 gcc_assert (MEM_P (earlier));
384 gcc_assert (MEM_P (later));
386 return ((MEM_ALIAS_SET (earlier) == MEM_ALIAS_SET (later)
387 || alias_set_subset_of (MEM_ALIAS_SET (later),
388 MEM_ALIAS_SET (earlier)))
389 && (!MEM_EXPR (earlier)
390 || refs_same_for_tbaa_p (MEM_EXPR (earlier), MEM_EXPR (later))));
393 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
394 such an entry, or NULL otherwise. */
396 static inline alias_set_entry *
397 get_alias_set_entry (alias_set_type alias_set)
399 return (*alias_sets)[alias_set];
402 /* Returns true if the alias sets for MEM1 and MEM2 are such that
403 the two MEMs cannot alias each other. */
405 static inline bool
406 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
408 return (flag_strict_aliasing
409 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
410 MEM_ALIAS_SET (mem2)));
413 /* Return true if the first alias set is a subset of the second. */
415 bool
416 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
418 alias_set_entry *ase2;
420 /* Disable TBAA oracle with !flag_strict_aliasing. */
421 if (!flag_strict_aliasing)
422 return true;
424 /* Everything is a subset of the "aliases everything" set. */
425 if (set2 == 0)
426 return true;
428 /* Check if set1 is a subset of set2. */
429 ase2 = get_alias_set_entry (set2);
430 if (ase2 != 0
431 && (ase2->has_zero_child
432 || (ase2->children && ase2->children->get (set1))))
433 return true;
435 /* As a special case we consider alias set of "void *" to be both subset
436 and superset of every alias set of a pointer. This extra symmetry does
437 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
438 to return true on the following testcase:
440 void *ptr;
441 char **ptr2=(char **)&ptr;
442 *ptr2 = ...
444 Additionally if a set contains universal pointer, we consider every pointer
445 to be a subset of it, but we do not represent this explicitely - doing so
446 would require us to update transitive closure each time we introduce new
447 pointer type. This makes aliasing_component_refs_p to return true
448 on the following testcase:
450 struct a {void *ptr;}
451 char **ptr = (char **)&a.ptr;
452 ptr = ...
454 This makes void * truly universal pointer type. See pointer handling in
455 get_alias_set for more details. */
456 if (ase2 && ase2->has_pointer)
458 alias_set_entry *ase1 = get_alias_set_entry (set1);
460 if (ase1 && ase1->is_pointer)
462 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
463 /* If one is ptr_type_node and other is pointer, then we consider
464 them subset of each other. */
465 if (set1 == voidptr_set || set2 == voidptr_set)
466 return true;
467 /* If SET2 contains universal pointer's alias set, then we consdier
468 every (non-universal) pointer. */
469 if (ase2->children && set1 != voidptr_set
470 && ase2->children->get (voidptr_set))
471 return true;
474 return false;
477 /* Return true if the two specified alias sets may conflict. */
479 bool
480 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
482 alias_set_entry *ase1;
483 alias_set_entry *ase2;
485 /* The easy case. */
486 if (alias_sets_must_conflict_p (set1, set2))
487 return true;
489 /* See if the first alias set is a subset of the second. */
490 ase1 = get_alias_set_entry (set1);
491 if (ase1 != 0
492 && ase1->children && ase1->children->get (set2))
494 ++alias_stats.num_dag;
495 return true;
498 /* Now do the same, but with the alias sets reversed. */
499 ase2 = get_alias_set_entry (set2);
500 if (ase2 != 0
501 && ase2->children && ase2->children->get (set1))
503 ++alias_stats.num_dag;
504 return true;
507 /* We want void * to be compatible with any other pointer without
508 really dropping it to alias set 0. Doing so would make it
509 compatible with all non-pointer types too.
511 This is not strictly necessary by the C/C++ language
512 standards, but avoids common type punning mistakes. In
513 addition to that, we need the existence of such universal
514 pointer to implement Fortran's C_PTR type (which is defined as
515 type compatible with all C pointers). */
516 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
518 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
520 /* If one of the sets corresponds to universal pointer,
521 we consider it to conflict with anything that is
522 or contains pointer. */
523 if (set1 == voidptr_set || set2 == voidptr_set)
525 ++alias_stats.num_universal;
526 return true;
528 /* If one of sets is (non-universal) pointer and the other
529 contains universal pointer, we also get conflict. */
530 if (ase1->is_pointer && set2 != voidptr_set
531 && ase2->children && ase2->children->get (voidptr_set))
533 ++alias_stats.num_universal;
534 return true;
536 if (ase2->is_pointer && set1 != voidptr_set
537 && ase1->children && ase1->children->get (voidptr_set))
539 ++alias_stats.num_universal;
540 return true;
544 ++alias_stats.num_disambiguated;
546 /* The two alias sets are distinct and neither one is the
547 child of the other. Therefore, they cannot conflict. */
548 return false;
551 /* Return true if the two specified alias sets will always conflict. */
553 bool
554 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
556 /* Disable TBAA oracle with !flag_strict_aliasing. */
557 if (!flag_strict_aliasing)
558 return true;
559 if (set1 == 0 || set2 == 0)
561 ++alias_stats.num_alias_zero;
562 return true;
564 if (set1 == set2)
566 ++alias_stats.num_same_alias_set;
567 return true;
570 return false;
573 /* Return true if any MEM object of type T1 will always conflict (using the
574 dependency routines in this file) with any MEM object of type T2.
575 This is used when allocating temporary storage. If T1 and/or T2 are
576 NULL_TREE, it means we know nothing about the storage. */
578 bool
579 objects_must_conflict_p (tree t1, tree t2)
581 alias_set_type set1, set2;
583 /* If neither has a type specified, we don't know if they'll conflict
584 because we may be using them to store objects of various types, for
585 example the argument and local variables areas of inlined functions. */
586 if (t1 == 0 && t2 == 0)
587 return false;
589 /* If they are the same type, they must conflict. */
590 if (t1 == t2)
592 ++alias_stats.num_same_objects;
593 return true;
595 /* Likewise if both are volatile. */
596 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
598 ++alias_stats.num_volatile;
599 return true;
602 set1 = t1 ? get_alias_set (t1) : 0;
603 set2 = t2 ? get_alias_set (t2) : 0;
605 /* We can't use alias_sets_conflict_p because we must make sure
606 that every subtype of t1 will conflict with every subtype of
607 t2 for which a pair of subobjects of these respective subtypes
608 overlaps on the stack. */
609 return alias_sets_must_conflict_p (set1, set2);
612 /* Return true if T is an end of the access path which can be used
613 by type based alias oracle. */
615 bool
616 ends_tbaa_access_path_p (const_tree t)
618 switch (TREE_CODE (t))
620 case COMPONENT_REF:
621 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
622 return true;
623 /* Permit type-punning when accessing a union, provided the access
624 is directly through the union. For example, this code does not
625 permit taking the address of a union member and then storing
626 through it. Even the type-punning allowed here is a GCC
627 extension, albeit a common and useful one; the C standard says
628 that such accesses have implementation-defined behavior. */
629 else if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
630 return true;
631 break;
633 case ARRAY_REF:
634 case ARRAY_RANGE_REF:
635 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
636 return true;
637 break;
639 case REALPART_EXPR:
640 case IMAGPART_EXPR:
641 break;
643 case BIT_FIELD_REF:
644 case VIEW_CONVERT_EXPR:
645 /* Bitfields and casts are never addressable. */
646 return true;
647 break;
649 default:
650 gcc_unreachable ();
652 return false;
655 /* Return the outermost parent of component present in the chain of
656 component references handled by get_inner_reference in T with the
657 following property:
658 - the component is non-addressable
659 or NULL_TREE if no such parent exists. In the former cases, the alias
660 set of this parent is the alias set that must be used for T itself. */
662 tree
663 component_uses_parent_alias_set_from (const_tree t)
665 const_tree found = NULL_TREE;
667 while (handled_component_p (t))
669 if (ends_tbaa_access_path_p (t))
670 found = t;
672 t = TREE_OPERAND (t, 0);
675 if (found)
676 return TREE_OPERAND (found, 0);
678 return NULL_TREE;
682 /* Return whether the pointer-type T effective for aliasing may
683 access everything and thus the reference has to be assigned
684 alias-set zero. */
686 static bool
687 ref_all_alias_ptr_type_p (const_tree t)
689 return (VOID_TYPE_P (TREE_TYPE (t))
690 || TYPE_REF_CAN_ALIAS_ALL (t));
693 /* Return the alias set for the memory pointed to by T, which may be
694 either a type or an expression. Return -1 if there is nothing
695 special about dereferencing T. */
697 static alias_set_type
698 get_deref_alias_set_1 (tree t)
700 /* All we care about is the type. */
701 if (! TYPE_P (t))
702 t = TREE_TYPE (t);
704 /* If we have an INDIRECT_REF via a void pointer, we don't
705 know anything about what that might alias. Likewise if the
706 pointer is marked that way. */
707 if (ref_all_alias_ptr_type_p (t))
708 return 0;
710 return -1;
713 /* Return the alias set for the memory pointed to by T, which may be
714 either a type or an expression. */
716 alias_set_type
717 get_deref_alias_set (tree t)
719 /* If we're not doing any alias analysis, just assume everything
720 aliases everything else. */
721 if (!flag_strict_aliasing)
722 return 0;
724 alias_set_type set = get_deref_alias_set_1 (t);
726 /* Fall back to the alias-set of the pointed-to type. */
727 if (set == -1)
729 if (! TYPE_P (t))
730 t = TREE_TYPE (t);
731 set = get_alias_set (TREE_TYPE (t));
734 return set;
737 /* Return the pointer-type relevant for TBAA purposes from the
738 memory reference tree *T or NULL_TREE in which case *T is
739 adjusted to point to the outermost component reference that
740 can be used for assigning an alias set. */
742 tree
743 reference_alias_ptr_type_1 (tree *t)
745 tree inner;
747 /* Get the base object of the reference. */
748 inner = *t;
749 while (handled_component_p (inner))
751 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
752 the type of any component references that wrap it to
753 determine the alias-set. */
754 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
755 *t = TREE_OPERAND (inner, 0);
756 inner = TREE_OPERAND (inner, 0);
759 /* Handle pointer dereferences here, they can override the
760 alias-set. */
761 if (INDIRECT_REF_P (inner)
762 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
763 return TREE_TYPE (TREE_OPERAND (inner, 0));
764 else if (TREE_CODE (inner) == TARGET_MEM_REF)
765 return TREE_TYPE (TMR_OFFSET (inner));
766 else if (TREE_CODE (inner) == MEM_REF
767 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
768 return TREE_TYPE (TREE_OPERAND (inner, 1));
770 /* If the innermost reference is a MEM_REF that has a
771 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
772 using the memory access type for determining the alias-set. */
773 if (view_converted_memref_p (inner))
775 tree alias_ptrtype = TREE_TYPE (TREE_OPERAND (inner, 1));
776 /* Unless we have the (aggregate) effective type of the access
777 somewhere on the access path. If we have for example
778 (&a->elts[i])->l.len exposed by abstraction we'd see
779 MEM <A> [(B *)a].elts[i].l.len and we can use the alias set
780 of 'len' when typeof (MEM <A> [(B *)a].elts[i]) == B for
781 example. See PR111715. */
782 tree inner = *t;
783 while (handled_component_p (inner)
784 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
785 != TYPE_MAIN_VARIANT (TREE_TYPE (alias_ptrtype))))
786 inner = TREE_OPERAND (inner, 0);
787 if (TREE_CODE (inner) == MEM_REF)
788 return alias_ptrtype;
791 /* Otherwise, pick up the outermost object that we could have
792 a pointer to. */
793 tree tem = component_uses_parent_alias_set_from (*t);
794 if (tem)
795 *t = tem;
797 return NULL_TREE;
800 /* Return the pointer-type relevant for TBAA purposes from the
801 gimple memory reference tree T. This is the type to be used for
802 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
803 and guarantees that get_alias_set will return the same alias
804 set for T and the replacement. */
806 tree
807 reference_alias_ptr_type (tree t)
809 /* If the frontend assigns this alias-set zero, preserve that. */
810 if (lang_hooks.get_alias_set (t) == 0)
811 return ptr_type_node;
813 tree ptype = reference_alias_ptr_type_1 (&t);
814 /* If there is a given pointer type for aliasing purposes, return it. */
815 if (ptype != NULL_TREE)
816 return ptype;
818 /* Otherwise build one from the outermost component reference we
819 may use. */
820 if (TREE_CODE (t) == MEM_REF
821 || TREE_CODE (t) == TARGET_MEM_REF)
822 return TREE_TYPE (TREE_OPERAND (t, 1));
823 else
824 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
827 /* Return whether the pointer-types T1 and T2 used to determine
828 two alias sets of two references will yield the same answer
829 from get_deref_alias_set. */
831 bool
832 alias_ptr_types_compatible_p (tree t1, tree t2)
834 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
835 return true;
837 if (ref_all_alias_ptr_type_p (t1)
838 || ref_all_alias_ptr_type_p (t2))
839 return false;
841 /* This function originally abstracts from simply comparing
842 get_deref_alias_set so that we are sure this still computes
843 the same result after LTO type merging is applied.
844 When in LTO type merging is done we can actually do this compare.
846 if (in_lto_p)
847 return get_deref_alias_set (t1) == get_deref_alias_set (t2);
848 else
849 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
850 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
853 /* Create emptry alias set entry. */
855 alias_set_entry *
856 init_alias_set_entry (alias_set_type set)
858 alias_set_entry *ase = ggc_alloc<alias_set_entry> ();
859 ase->alias_set = set;
860 ase->children = NULL;
861 ase->has_zero_child = false;
862 ase->is_pointer = false;
863 ase->has_pointer = false;
864 gcc_checking_assert (!get_alias_set_entry (set));
865 (*alias_sets)[set] = ase;
866 return ase;
869 /* Return the alias set for T, which may be either a type or an
870 expression. Call language-specific routine for help, if needed. */
872 alias_set_type
873 get_alias_set (tree t)
875 alias_set_type set;
877 /* We cannot give up with -fno-strict-aliasing because we need to build
878 proper type representations for possible functions which are built with
879 -fstrict-aliasing. */
881 /* return 0 if this or its type is an error. */
882 if (t == error_mark_node
883 || (! TYPE_P (t)
884 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
885 return 0;
887 /* We can be passed either an expression or a type. This and the
888 language-specific routine may make mutually-recursive calls to each other
889 to figure out what to do. At each juncture, we see if this is a tree
890 that the language may need to handle specially. First handle things that
891 aren't types. */
892 if (! TYPE_P (t))
894 /* Give the language a chance to do something with this tree
895 before we look at it. */
896 STRIP_NOPS (t);
897 set = lang_hooks.get_alias_set (t);
898 if (set != -1)
899 return set;
901 /* Get the alias pointer-type to use or the outermost object
902 that we could have a pointer to. */
903 tree ptype = reference_alias_ptr_type_1 (&t);
904 if (ptype != NULL)
905 return get_deref_alias_set (ptype);
907 /* If we've already determined the alias set for a decl, just return
908 it. This is necessary for C++ anonymous unions, whose component
909 variables don't look like union members (boo!). */
910 if (VAR_P (t)
911 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
912 return MEM_ALIAS_SET (DECL_RTL (t));
914 /* Now all we care about is the type. */
915 t = TREE_TYPE (t);
918 /* Variant qualifiers don't affect the alias set, so get the main
919 variant. */
920 t = TYPE_MAIN_VARIANT (t);
922 if (AGGREGATE_TYPE_P (t)
923 && TYPE_TYPELESS_STORAGE (t))
924 return 0;
926 /* Always use the canonical type as well. If this is a type that
927 requires structural comparisons to identify compatible types
928 use alias set zero. */
929 if (TYPE_STRUCTURAL_EQUALITY_P (t))
931 /* Allow the language to specify another alias set for this
932 type. */
933 set = lang_hooks.get_alias_set (t);
934 if (set != -1)
935 return set;
936 /* Handle structure type equality for pointer types, arrays and vectors.
937 This is easy to do, because the code below ignores canonical types on
938 these anyway. This is important for LTO, where TYPE_CANONICAL for
939 pointers cannot be meaningfully computed by the frontend. */
940 if (canonical_type_used_p (t))
942 /* In LTO we set canonical types for all types where it makes
943 sense to do so. Double check we did not miss some type. */
944 gcc_checking_assert (!in_lto_p || !type_with_alias_set_p (t));
945 return 0;
948 else
950 t = TYPE_CANONICAL (t);
951 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
954 /* If this is a type with a known alias set, return it. */
955 gcc_checking_assert (t == TYPE_MAIN_VARIANT (t));
956 if (TYPE_ALIAS_SET_KNOWN_P (t))
957 return TYPE_ALIAS_SET (t);
959 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
960 if (!COMPLETE_TYPE_P (t))
962 /* For arrays with unknown size the conservative answer is the
963 alias set of the element type. */
964 if (TREE_CODE (t) == ARRAY_TYPE)
965 return get_alias_set (TREE_TYPE (t));
967 /* But return zero as a conservative answer for incomplete types. */
968 return 0;
971 /* See if the language has special handling for this type. */
972 set = lang_hooks.get_alias_set (t);
973 if (set != -1)
974 return set;
976 /* There are no objects of FUNCTION_TYPE, so there's no point in
977 using up an alias set for them. (There are, of course, pointers
978 and references to functions, but that's different.) */
979 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
980 set = 0;
982 /* Unless the language specifies otherwise, let vector types alias
983 their components. This avoids some nasty type punning issues in
984 normal usage. And indeed lets vectors be treated more like an
985 array slice. */
986 else if (TREE_CODE (t) == VECTOR_TYPE)
987 set = get_alias_set (TREE_TYPE (t));
989 /* Unless the language specifies otherwise, treat array types the
990 same as their components. This avoids the asymmetry we get
991 through recording the components. Consider accessing a
992 character(kind=1) through a reference to a character(kind=1)[1:1].
993 Or consider if we want to assign integer(kind=4)[0:D.1387] and
994 integer(kind=4)[4] the same alias set or not.
995 Just be pragmatic here and make sure the array and its element
996 type get the same alias set assigned. */
997 else if (TREE_CODE (t) == ARRAY_TYPE
998 && (!TYPE_NONALIASED_COMPONENT (t)
999 || TYPE_STRUCTURAL_EQUALITY_P (t)))
1000 set = get_alias_set (TREE_TYPE (t));
1002 /* From the former common C and C++ langhook implementation:
1004 Unfortunately, there is no canonical form of a pointer type.
1005 In particular, if we have `typedef int I', then `int *', and
1006 `I *' are different types. So, we have to pick a canonical
1007 representative. We do this below.
1009 Technically, this approach is actually more conservative that
1010 it needs to be. In particular, `const int *' and `int *'
1011 should be in different alias sets, according to the C and C++
1012 standard, since their types are not the same, and so,
1013 technically, an `int **' and `const int **' cannot point at
1014 the same thing.
1016 But, the standard is wrong. In particular, this code is
1017 legal C++:
1019 int *ip;
1020 int **ipp = &ip;
1021 const int* const* cipp = ipp;
1022 And, it doesn't make sense for that to be legal unless you
1023 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
1024 the pointed-to types. This issue has been reported to the
1025 C++ committee.
1027 For this reason go to canonical type of the unqalified pointer type.
1028 Until GCC 6 this code set all pointers sets to have alias set of
1029 ptr_type_node but that is a bad idea, because it prevents disabiguations
1030 in between pointers. For Firefox this accounts about 20% of all
1031 disambiguations in the program. */
1032 else if (POINTER_TYPE_P (t) && t != ptr_type_node)
1034 tree p;
1035 auto_vec <bool, 8> reference;
1037 /* Unnest all pointers and references.
1038 We also want to make pointer to array/vector equivalent to pointer to
1039 its element (see the reasoning above). Skip all those types, too. */
1040 for (p = t; POINTER_TYPE_P (p)
1041 || (TREE_CODE (p) == ARRAY_TYPE
1042 && (!TYPE_NONALIASED_COMPONENT (p)
1043 || !COMPLETE_TYPE_P (p)
1044 || TYPE_STRUCTURAL_EQUALITY_P (p)))
1045 || TREE_CODE (p) == VECTOR_TYPE;
1046 p = TREE_TYPE (p))
1048 /* Ada supports recursive pointers. Instead of doing recursion
1049 check, just give up once the preallocated space of 8 elements
1050 is up. In this case just punt to void * alias set. */
1051 if (reference.length () == 8)
1053 p = ptr_type_node;
1054 break;
1056 if (TREE_CODE (p) == REFERENCE_TYPE)
1057 /* In LTO we want languages that use references to be compatible
1058 with languages that use pointers. */
1059 reference.safe_push (true && !in_lto_p);
1060 if (TREE_CODE (p) == POINTER_TYPE)
1061 reference.safe_push (false);
1063 p = TYPE_MAIN_VARIANT (p);
1065 /* In LTO for C++ programs we can turn incomplete types to complete
1066 using ODR name lookup. */
1067 if (in_lto_p && TYPE_STRUCTURAL_EQUALITY_P (p) && odr_type_p (p))
1069 p = prevailing_odr_type (p);
1070 gcc_checking_assert (TYPE_MAIN_VARIANT (p) == p);
1073 /* Make void * compatible with char * and also void **.
1074 Programs are commonly violating TBAA by this.
1076 We also make void * to conflict with every pointer
1077 (see record_component_aliases) and thus it is safe it to use it for
1078 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
1079 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
1080 set = get_alias_set (ptr_type_node);
1081 else
1083 /* Rebuild pointer type starting from canonical types using
1084 unqualified pointers and references only. This way all such
1085 pointers will have the same alias set and will conflict with
1086 each other.
1088 Most of time we already have pointers or references of a given type.
1089 If not we build new one just to be sure that if someone later
1090 (probably only middle-end can, as we should assign all alias
1091 classes only after finishing translation unit) builds the pointer
1092 type, the canonical type will match. */
1093 p = TYPE_CANONICAL (p);
1094 while (!reference.is_empty ())
1096 if (reference.pop ())
1097 p = build_reference_type (p);
1098 else
1099 p = build_pointer_type (p);
1100 gcc_checking_assert (p == TYPE_MAIN_VARIANT (p));
1101 /* build_pointer_type should always return the canonical type.
1102 For LTO TYPE_CANOINCAL may be NULL, because we do not compute
1103 them. Be sure that frontends do not glob canonical types of
1104 pointers in unexpected way and that p == TYPE_CANONICAL (p)
1105 in all other cases. */
1106 gcc_checking_assert (!TYPE_CANONICAL (p)
1107 || p == TYPE_CANONICAL (p));
1110 /* Assign the alias set to both p and t.
1111 We cannot call get_alias_set (p) here as that would trigger
1112 infinite recursion when p == t. In other cases it would just
1113 trigger unnecesary legwork of rebuilding the pointer again. */
1114 gcc_checking_assert (p == TYPE_MAIN_VARIANT (p));
1115 if (TYPE_ALIAS_SET_KNOWN_P (p))
1116 set = TYPE_ALIAS_SET (p);
1117 else
1119 set = new_alias_set ();
1120 TYPE_ALIAS_SET (p) = set;
1124 /* Alias set of ptr_type_node is special and serve as universal pointer which
1125 is TBAA compatible with every other pointer type. Be sure we have the
1126 alias set built even for LTO which otherwise keeps all TYPE_CANONICAL
1127 of pointer types NULL. */
1128 else if (t == ptr_type_node)
1129 set = new_alias_set ();
1131 /* Otherwise make a new alias set for this type. */
1132 else
1134 /* Each canonical type gets its own alias set, so canonical types
1135 shouldn't form a tree. It doesn't really matter for types
1136 we handle specially above, so only check it where it possibly
1137 would result in a bogus alias set. */
1138 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1140 set = new_alias_set ();
1143 TYPE_ALIAS_SET (t) = set;
1145 /* If this is an aggregate type or a complex type, we must record any
1146 component aliasing information. */
1147 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1148 record_component_aliases (t);
1150 /* We treat pointer types specially in alias_set_subset_of. */
1151 if (POINTER_TYPE_P (t) && set)
1153 alias_set_entry *ase = get_alias_set_entry (set);
1154 if (!ase)
1155 ase = init_alias_set_entry (set);
1156 ase->is_pointer = true;
1157 ase->has_pointer = true;
1160 return set;
1163 /* Return a brand-new alias set. */
1165 alias_set_type
1166 new_alias_set (void)
1168 if (alias_sets == 0)
1169 vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1170 vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1171 return alias_sets->length () - 1;
1174 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1175 not everything that aliases SUPERSET also aliases SUBSET. For example,
1176 in C, a store to an `int' can alias a load of a structure containing an
1177 `int', and vice versa. But it can't alias a load of a 'double' member
1178 of the same structure. Here, the structure would be the SUPERSET and
1179 `int' the SUBSET. This relationship is also described in the comment at
1180 the beginning of this file.
1182 This function should be called only once per SUPERSET/SUBSET pair.
1184 It is illegal for SUPERSET to be zero; everything is implicitly a
1185 subset of alias set zero. */
1187 void
1188 record_alias_subset (alias_set_type superset, alias_set_type subset)
1190 alias_set_entry *superset_entry;
1191 alias_set_entry *subset_entry;
1193 /* It is possible in complex type situations for both sets to be the same,
1194 in which case we can ignore this operation. */
1195 if (superset == subset)
1196 return;
1198 gcc_assert (superset);
1200 superset_entry = get_alias_set_entry (superset);
1201 if (superset_entry == 0)
1203 /* Create an entry for the SUPERSET, so that we have a place to
1204 attach the SUBSET. */
1205 superset_entry = init_alias_set_entry (superset);
1208 if (subset == 0)
1209 superset_entry->has_zero_child = 1;
1210 else
1212 if (!superset_entry->children)
1213 superset_entry->children
1214 = hash_map<alias_set_hash, int>::create_ggc (64);
1216 /* Enter the SUBSET itself as a child of the SUPERSET. If it was
1217 already there we're done. */
1218 if (superset_entry->children->put (subset, 0))
1219 return;
1221 subset_entry = get_alias_set_entry (subset);
1222 /* If there is an entry for the subset, enter all of its children
1223 (if they are not already present) as children of the SUPERSET. */
1224 if (subset_entry)
1226 if (subset_entry->has_zero_child)
1227 superset_entry->has_zero_child = true;
1228 if (subset_entry->has_pointer)
1229 superset_entry->has_pointer = true;
1231 if (subset_entry->children)
1233 hash_map<alias_set_hash, int>::iterator iter
1234 = subset_entry->children->begin ();
1235 for (; iter != subset_entry->children->end (); ++iter)
1236 superset_entry->children->put ((*iter).first, (*iter).second);
1242 /* Record that component types of TYPE, if any, are part of SUPERSET for
1243 aliasing purposes. For record types, we only record component types
1244 for fields that are not marked non-addressable. For array types, we
1245 only record the component type if it is not marked non-aliased. */
1247 void
1248 record_component_aliases (tree type, alias_set_type superset)
1250 tree field;
1252 if (superset == 0)
1253 return;
1255 switch (TREE_CODE (type))
1257 case RECORD_TYPE:
1258 case UNION_TYPE:
1259 case QUAL_UNION_TYPE:
1261 /* LTO non-ODR type merging does not make any difference between
1262 component pointer types. We may have
1264 struct foo {int *a;};
1266 as TYPE_CANONICAL of
1268 struct bar {float *a;};
1270 Because accesses to int * and float * do not alias, we would get
1271 false negative when accessing the same memory location by
1272 float ** and bar *. We thus record the canonical type as:
1274 struct {void *a;};
1276 void * is special cased and works as a universal pointer type.
1277 Accesses to it conflicts with accesses to any other pointer
1278 type. */
1279 bool void_pointers = in_lto_p
1280 && (!odr_type_p (type)
1281 || !odr_based_tbaa_p (type));
1282 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1283 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1285 tree t = TREE_TYPE (field);
1286 if (void_pointers)
1288 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1289 element type and that type has to be normalized to void *,
1290 too, in the case it is a pointer. */
1291 while (!canonical_type_used_p (t) && !POINTER_TYPE_P (t))
1293 gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
1294 t = TREE_TYPE (t);
1296 if (POINTER_TYPE_P (t))
1297 t = ptr_type_node;
1298 else if (flag_checking)
1299 gcc_checking_assert (get_alias_set (t)
1300 == get_alias_set (TREE_TYPE (field)));
1303 alias_set_type set = get_alias_set (t);
1304 record_alias_subset (superset, set);
1305 /* If the field has alias-set zero make sure to still record
1306 any componets of it. This makes sure that for
1307 struct A {
1308 struct B {
1309 int i;
1310 char c[4];
1311 } b;
1313 in C++ even though 'B' has alias-set zero because
1314 TYPE_TYPELESS_STORAGE is set, 'A' has the alias-set of
1315 'int' as subset. */
1316 if (set == 0)
1317 record_component_aliases (t, superset);
1320 break;
1322 case COMPLEX_TYPE:
1323 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1324 break;
1326 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1327 element type. */
1329 default:
1330 break;
1334 /* Record that component types of TYPE, if any, are part of that type for
1335 aliasing purposes. For record types, we only record component types
1336 for fields that are not marked non-addressable. For array types, we
1337 only record the component type if it is not marked non-aliased. */
1339 void
1340 record_component_aliases (tree type)
1342 alias_set_type superset = get_alias_set (type);
1343 record_component_aliases (type, superset);
1347 /* Allocate an alias set for use in storing and reading from the varargs
1348 spill area. */
1350 static GTY(()) alias_set_type varargs_set = -1;
1352 alias_set_type
1353 get_varargs_alias_set (void)
1355 #if 1
1356 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1357 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1358 consistently use the varargs alias set for loads from the varargs
1359 area. So don't use it anywhere. */
1360 return 0;
1361 #else
1362 if (varargs_set == -1)
1363 varargs_set = new_alias_set ();
1365 return varargs_set;
1366 #endif
1369 /* Likewise, but used for the fixed portions of the frame, e.g., register
1370 save areas. */
1372 static GTY(()) alias_set_type frame_set = -1;
1374 alias_set_type
1375 get_frame_alias_set (void)
1377 if (frame_set == -1)
1378 frame_set = new_alias_set ();
1380 return frame_set;
1383 /* Create a new, unique base with id ID. */
1385 static rtx
1386 unique_base_value (HOST_WIDE_INT id)
1388 return gen_rtx_ADDRESS (Pmode, id);
1391 /* Return true if accesses based on any other base value cannot alias
1392 those based on X. */
1394 static bool
1395 unique_base_value_p (rtx x)
1397 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1400 /* Inside SRC, the source of a SET, find a base address. */
1402 static rtx
1403 find_base_value (rtx src)
1405 unsigned int regno;
1406 scalar_int_mode int_mode;
1408 #if defined (FIND_BASE_TERM)
1409 /* Try machine-dependent ways to find the base term. */
1410 src = FIND_BASE_TERM (src);
1411 #endif
1413 switch (GET_CODE (src))
1415 case SYMBOL_REF:
1416 case LABEL_REF:
1417 return src;
1419 case REG:
1420 regno = REGNO (src);
1421 /* At the start of a function, argument registers have known base
1422 values which may be lost later. Returning an ADDRESS
1423 expression here allows optimization based on argument values
1424 even when the argument registers are used for other purposes. */
1425 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1426 return new_reg_base_value[regno];
1428 /* If a pseudo has a known base value, return it. Do not do this
1429 for non-fixed hard regs since it can result in a circular
1430 dependency chain for registers which have values at function entry.
1432 The test above is not sufficient because the scheduler may move
1433 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1434 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1435 && regno < vec_safe_length (reg_base_value))
1437 /* If we're inside init_alias_analysis, use new_reg_base_value
1438 to reduce the number of relaxation iterations. */
1439 if (new_reg_base_value && new_reg_base_value[regno]
1440 && DF_REG_DEF_COUNT (regno) == 1)
1441 return new_reg_base_value[regno];
1443 if ((*reg_base_value)[regno])
1444 return (*reg_base_value)[regno];
1447 return 0;
1449 case MEM:
1450 /* Check for an argument passed in memory. Only record in the
1451 copying-arguments block; it is too hard to track changes
1452 otherwise. */
1453 if (copying_arguments
1454 && (XEXP (src, 0) == arg_pointer_rtx
1455 || (GET_CODE (XEXP (src, 0)) == PLUS
1456 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1457 return arg_base_value;
1458 return 0;
1460 case CONST:
1461 src = XEXP (src, 0);
1462 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1463 break;
1465 /* fall through */
1467 case PLUS:
1468 case MINUS:
1470 rtx src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1472 /* If either operand is a CONST_INT, then the other is the base. */
1473 if (CONST_INT_P (src_1))
1474 return find_base_value (src_0);
1475 else if (CONST_INT_P (src_0))
1476 return find_base_value (src_1);
1478 return 0;
1481 case LO_SUM:
1482 /* The standard form is (lo_sum reg sym) so look only at the
1483 second operand. */
1484 return find_base_value (XEXP (src, 1));
1486 case AND:
1487 /* Look through aligning ANDs. And AND with zero or one with
1488 the LSB set isn't one (see for example PR92462). */
1489 if (CONST_INT_P (XEXP (src, 1))
1490 && INTVAL (XEXP (src, 1)) != 0
1491 && (INTVAL (XEXP (src, 1)) & 1) == 0)
1492 return find_base_value (XEXP (src, 0));
1493 return 0;
1495 case TRUNCATE:
1496 /* As we do not know which address space the pointer is referring to, we can
1497 handle this only if the target does not support different pointer or
1498 address modes depending on the address space. */
1499 if (!target_default_pointer_address_modes_p ())
1500 break;
1501 if (!is_a <scalar_int_mode> (GET_MODE (src), &int_mode)
1502 || GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (Pmode))
1503 break;
1504 /* Fall through. */
1505 case HIGH:
1506 case PRE_INC:
1507 case PRE_DEC:
1508 case POST_INC:
1509 case POST_DEC:
1510 case PRE_MODIFY:
1511 case POST_MODIFY:
1512 return find_base_value (XEXP (src, 0));
1514 case ZERO_EXTEND:
1515 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1516 /* As we do not know which address space the pointer is referring to, we can
1517 handle this only if the target does not support different pointer or
1518 address modes depending on the address space. */
1519 if (!target_default_pointer_address_modes_p ())
1520 break;
1523 rtx temp = find_base_value (XEXP (src, 0));
1525 if (temp != 0 && CONSTANT_P (temp))
1526 temp = convert_memory_address (Pmode, temp);
1528 return temp;
1531 default:
1532 break;
1535 return 0;
1538 /* Called from init_alias_analysis indirectly through note_stores,
1539 or directly if DEST is a register with a REG_NOALIAS note attached.
1540 SET is null in the latter case. */
1542 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1543 register N has been set in this function. */
1544 static sbitmap reg_seen;
1546 static void
1547 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1549 unsigned regno;
1550 rtx src;
1551 int n;
1553 if (!REG_P (dest))
1554 return;
1556 regno = REGNO (dest);
1558 gcc_checking_assert (regno < reg_base_value->length ());
1560 n = REG_NREGS (dest);
1561 if (n != 1)
1563 while (--n >= 0)
1565 bitmap_set_bit (reg_seen, regno + n);
1566 new_reg_base_value[regno + n] = 0;
1568 return;
1571 if (set)
1573 /* A CLOBBER wipes out any old value but does not prevent a previously
1574 unset register from acquiring a base address (i.e. reg_seen is not
1575 set). */
1576 if (GET_CODE (set) == CLOBBER)
1578 new_reg_base_value[regno] = 0;
1579 return;
1582 src = SET_SRC (set);
1584 else
1586 /* There's a REG_NOALIAS note against DEST. */
1587 if (bitmap_bit_p (reg_seen, regno))
1589 new_reg_base_value[regno] = 0;
1590 return;
1592 bitmap_set_bit (reg_seen, regno);
1593 new_reg_base_value[regno] = unique_base_value (unique_id++);
1594 return;
1597 /* If this is not the first set of REGNO, see whether the new value
1598 is related to the old one. There are two cases of interest:
1600 (1) The register might be assigned an entirely new value
1601 that has the same base term as the original set.
1603 (2) The set might be a simple self-modification that
1604 cannot change REGNO's base value.
1606 If neither case holds, reject the original base value as invalid.
1607 Note that the following situation is not detected:
1609 extern int x, y; int *p = &x; p += (&y-&x);
1611 ANSI C does not allow computing the difference of addresses
1612 of distinct top level objects. */
1613 if (new_reg_base_value[regno] != 0
1614 && find_base_value (src) != new_reg_base_value[regno])
1615 switch (GET_CODE (src))
1617 case LO_SUM:
1618 case MINUS:
1619 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1620 new_reg_base_value[regno] = 0;
1621 break;
1622 case PLUS:
1623 /* If the value we add in the PLUS is also a valid base value,
1624 this might be the actual base value, and the original value
1625 an index. */
1627 rtx other = NULL_RTX;
1629 if (XEXP (src, 0) == dest)
1630 other = XEXP (src, 1);
1631 else if (XEXP (src, 1) == dest)
1632 other = XEXP (src, 0);
1634 if (! other || find_base_value (other))
1635 new_reg_base_value[regno] = 0;
1636 break;
1638 case AND:
1639 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1640 new_reg_base_value[regno] = 0;
1641 break;
1642 default:
1643 new_reg_base_value[regno] = 0;
1644 break;
1646 /* If this is the first set of a register, record the value. */
1647 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1648 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1649 new_reg_base_value[regno] = find_base_value (src);
1651 bitmap_set_bit (reg_seen, regno);
1654 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1655 using hard registers with non-null REG_BASE_VALUE for renaming. */
1657 get_reg_base_value (unsigned int regno)
1659 return (*reg_base_value)[regno];
1662 /* If a value is known for REGNO, return it. */
1665 get_reg_known_value (unsigned int regno)
1667 if (regno >= FIRST_PSEUDO_REGISTER)
1669 regno -= FIRST_PSEUDO_REGISTER;
1670 if (regno < vec_safe_length (reg_known_value))
1671 return (*reg_known_value)[regno];
1673 return NULL;
1676 /* Set it. */
1678 static void
1679 set_reg_known_value (unsigned int regno, rtx val)
1681 if (regno >= FIRST_PSEUDO_REGISTER)
1683 regno -= FIRST_PSEUDO_REGISTER;
1684 if (regno < vec_safe_length (reg_known_value))
1685 (*reg_known_value)[regno] = val;
1689 /* Similarly for reg_known_equiv_p. */
1691 bool
1692 get_reg_known_equiv_p (unsigned int regno)
1694 if (regno >= FIRST_PSEUDO_REGISTER)
1696 regno -= FIRST_PSEUDO_REGISTER;
1697 if (regno < vec_safe_length (reg_known_value))
1698 return bitmap_bit_p (reg_known_equiv_p, regno);
1700 return false;
1703 static void
1704 set_reg_known_equiv_p (unsigned int regno, bool val)
1706 if (regno >= FIRST_PSEUDO_REGISTER)
1708 regno -= FIRST_PSEUDO_REGISTER;
1709 if (regno < vec_safe_length (reg_known_value))
1711 if (val)
1712 bitmap_set_bit (reg_known_equiv_p, regno);
1713 else
1714 bitmap_clear_bit (reg_known_equiv_p, regno);
1720 /* Returns a canonical version of X, from the point of view alias
1721 analysis. (For example, if X is a MEM whose address is a register,
1722 and the register has a known value (say a SYMBOL_REF), then a MEM
1723 whose address is the SYMBOL_REF is returned.) */
1726 canon_rtx (rtx x)
1728 /* Recursively look for equivalences. */
1729 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1731 rtx t = get_reg_known_value (REGNO (x));
1732 if (t == x)
1733 return x;
1734 if (t)
1735 return canon_rtx (t);
1738 if (GET_CODE (x) == PLUS)
1740 rtx x0 = canon_rtx (XEXP (x, 0));
1741 rtx x1 = canon_rtx (XEXP (x, 1));
1743 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1744 return simplify_gen_binary (PLUS, GET_MODE (x), x0, x1);
1747 /* This gives us much better alias analysis when called from
1748 the loop optimizer. Note we want to leave the original
1749 MEM alone, but need to return the canonicalized MEM with
1750 all the flags with their original values. */
1751 else if (MEM_P (x))
1752 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1754 return x;
1757 /* Return true if X and Y are identical-looking rtx's.
1758 Expect that X and Y has been already canonicalized.
1760 We use the data in reg_known_value above to see if two registers with
1761 different numbers are, in fact, equivalent. */
1763 static bool
1764 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1766 int i;
1767 int j;
1768 enum rtx_code code;
1769 const char *fmt;
1771 if (x == 0 && y == 0)
1772 return true;
1773 if (x == 0 || y == 0)
1774 return false;
1776 if (x == y)
1777 return true;
1779 code = GET_CODE (x);
1780 /* Rtx's of different codes cannot be equal. */
1781 if (code != GET_CODE (y))
1782 return false;
1784 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1785 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1787 if (GET_MODE (x) != GET_MODE (y))
1788 return false;
1790 /* Some RTL can be compared without a recursive examination. */
1791 switch (code)
1793 case REG:
1794 return REGNO (x) == REGNO (y);
1796 case LABEL_REF:
1797 return label_ref_label (x) == label_ref_label (y);
1799 case SYMBOL_REF:
1801 HOST_WIDE_INT distance = 0;
1802 return (compare_base_symbol_refs (x, y, &distance) == 1
1803 && distance == 0);
1806 case ENTRY_VALUE:
1807 /* This is magic, don't go through canonicalization et al. */
1808 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1810 case VALUE:
1811 CASE_CONST_UNIQUE:
1812 /* Pointer equality guarantees equality for these nodes. */
1813 return false;
1815 default:
1816 break;
1819 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1820 if (code == PLUS)
1821 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1822 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1823 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1824 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1825 /* For commutative operations, the RTX match if the operand match in any
1826 order. Also handle the simple binary and unary cases without a loop. */
1827 if (COMMUTATIVE_P (x))
1829 rtx xop0 = canon_rtx (XEXP (x, 0));
1830 rtx yop0 = canon_rtx (XEXP (y, 0));
1831 rtx yop1 = canon_rtx (XEXP (y, 1));
1833 return ((rtx_equal_for_memref_p (xop0, yop0)
1834 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1835 || (rtx_equal_for_memref_p (xop0, yop1)
1836 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1838 else if (NON_COMMUTATIVE_P (x))
1840 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1841 canon_rtx (XEXP (y, 0)))
1842 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1843 canon_rtx (XEXP (y, 1))));
1845 else if (UNARY_P (x))
1846 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1847 canon_rtx (XEXP (y, 0)));
1849 /* Compare the elements. If any pair of corresponding elements
1850 fail to match, return false for the whole things.
1852 Limit cases to types which actually appear in addresses. */
1854 fmt = GET_RTX_FORMAT (code);
1855 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1857 switch (fmt[i])
1859 case 'i':
1860 if (XINT (x, i) != XINT (y, i))
1861 return false;
1862 break;
1864 case 'p':
1865 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1866 return false;
1867 break;
1869 case 'E':
1870 /* Two vectors must have the same length. */
1871 if (XVECLEN (x, i) != XVECLEN (y, i))
1872 return false;
1874 /* And the corresponding elements must match. */
1875 for (j = 0; j < XVECLEN (x, i); j++)
1876 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1877 canon_rtx (XVECEXP (y, i, j))) == 0)
1878 return false;
1879 break;
1881 case 'e':
1882 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1883 canon_rtx (XEXP (y, i))) == 0)
1884 return false;
1885 break;
1887 /* This can happen for asm operands. */
1888 case 's':
1889 if (strcmp (XSTR (x, i), XSTR (y, i)))
1890 return false;
1891 break;
1893 /* This can happen for an asm which clobbers memory. */
1894 case '0':
1895 break;
1897 /* It is believed that rtx's at this level will never
1898 contain anything but integers and other rtx's,
1899 except for within LABEL_REFs and SYMBOL_REFs. */
1900 default:
1901 gcc_unreachable ();
1904 return true;
1907 static rtx
1908 find_base_term (rtx x, vec<std::pair<cselib_val *,
1909 struct elt_loc_list *> > &visited_vals)
1911 cselib_val *val;
1912 struct elt_loc_list *l, *f;
1913 rtx ret;
1914 scalar_int_mode int_mode;
1916 #if defined (FIND_BASE_TERM)
1917 /* Try machine-dependent ways to find the base term. */
1918 x = FIND_BASE_TERM (x);
1919 #endif
1921 switch (GET_CODE (x))
1923 case REG:
1924 return REG_BASE_VALUE (x);
1926 case TRUNCATE:
1927 /* As we do not know which address space the pointer is referring to, we can
1928 handle this only if the target does not support different pointer or
1929 address modes depending on the address space. */
1930 if (!target_default_pointer_address_modes_p ())
1931 return 0;
1932 if (!is_a <scalar_int_mode> (GET_MODE (x), &int_mode)
1933 || GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (Pmode))
1934 return 0;
1935 /* Fall through. */
1936 case HIGH:
1937 case PRE_INC:
1938 case PRE_DEC:
1939 case POST_INC:
1940 case POST_DEC:
1941 case PRE_MODIFY:
1942 case POST_MODIFY:
1943 return find_base_term (XEXP (x, 0), visited_vals);
1945 case ZERO_EXTEND:
1946 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1947 /* As we do not know which address space the pointer is referring to, we can
1948 handle this only if the target does not support different pointer or
1949 address modes depending on the address space. */
1950 if (!target_default_pointer_address_modes_p ())
1951 return 0;
1954 rtx temp = find_base_term (XEXP (x, 0), visited_vals);
1956 if (temp != 0 && CONSTANT_P (temp))
1957 temp = convert_memory_address (Pmode, temp);
1959 return temp;
1962 case VALUE:
1963 val = CSELIB_VAL_PTR (x);
1964 ret = NULL_RTX;
1966 if (!val)
1967 return ret;
1969 if (cselib_sp_based_value_p (val))
1970 return static_reg_base_value[STACK_POINTER_REGNUM];
1972 if (visited_vals.length () > (unsigned) param_max_find_base_term_values)
1973 return ret;
1975 f = val->locs;
1976 /* Reset val->locs to avoid infinite recursion. */
1977 if (f)
1978 visited_vals.safe_push (std::make_pair (val, f));
1979 val->locs = NULL;
1981 for (l = f; l; l = l->next)
1982 if (GET_CODE (l->loc) == VALUE
1983 && CSELIB_VAL_PTR (l->loc)->locs
1984 && !CSELIB_VAL_PTR (l->loc)->locs->next
1985 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1986 continue;
1987 else if ((ret = find_base_term (l->loc, visited_vals)) != 0)
1988 break;
1990 return ret;
1992 case LO_SUM:
1993 /* The standard form is (lo_sum reg sym) so look only at the
1994 second operand. */
1995 return find_base_term (XEXP (x, 1), visited_vals);
1997 case CONST:
1998 x = XEXP (x, 0);
1999 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
2000 return 0;
2001 /* Fall through. */
2002 case PLUS:
2003 case MINUS:
2005 rtx tmp1 = XEXP (x, 0);
2006 rtx tmp2 = XEXP (x, 1);
2008 /* This is a little bit tricky since we have to determine which of
2009 the two operands represents the real base address. Otherwise this
2010 routine may return the index register instead of the base register.
2012 That may cause us to believe no aliasing was possible, when in
2013 fact aliasing is possible.
2015 We use a few simple tests to guess the base register. Additional
2016 tests can certainly be added. For example, if one of the operands
2017 is a shift or multiply, then it must be the index register and the
2018 other operand is the base register. */
2020 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
2021 return find_base_term (tmp2, visited_vals);
2023 if (CONST_INT_P (tmp1))
2024 std::swap (tmp1, tmp2);
2026 /* We can only handle binary operators when one of the operands
2027 never leads to a base value. */
2028 if (CONST_INT_P (tmp2))
2029 return find_base_term (tmp1, visited_vals);
2031 /* We could not determine which of the two operands was the
2032 base register and which was the index. So we can determine
2033 nothing from the base alias check. */
2034 return 0;
2037 case AND:
2038 /* Look through aligning ANDs. And AND with zero or one with
2039 the LSB set isn't one (see for example PR92462). */
2040 if (CONST_INT_P (XEXP (x, 1))
2041 && INTVAL (XEXP (x, 1)) != 0
2042 && (INTVAL (XEXP (x, 1)) & 1) == 0)
2043 return find_base_term (XEXP (x, 0), visited_vals);
2044 return 0;
2046 case SYMBOL_REF:
2047 case LABEL_REF:
2048 return x;
2050 default:
2051 return 0;
2055 /* Wrapper around the worker above which removes locs from visited VALUEs
2056 to avoid visiting them multiple times. We unwind that changes here. */
2058 static rtx
2059 find_base_term (rtx x)
2061 auto_vec<std::pair<cselib_val *, struct elt_loc_list *>, 32> visited_vals;
2062 rtx res = find_base_term (x, visited_vals);
2063 for (unsigned i = 0; i < visited_vals.length (); ++i)
2064 visited_vals[i].first->locs = visited_vals[i].second;
2065 return res;
2068 /* Return true if accesses to address X may alias accesses based
2069 on the stack pointer. */
2071 bool
2072 may_be_sp_based_p (rtx x)
2074 rtx base = find_base_term (x);
2075 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
2078 /* BASE1 and BASE2 are decls. Return 1 if they refer to same object, 0
2079 if they refer to different objects and -1 if we cannot decide. */
2082 compare_base_decls (tree base1, tree base2)
2084 int ret;
2085 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
2086 if (base1 == base2)
2087 return 1;
2089 /* If we have two register decls with register specification we
2090 cannot decide unless their assembler names are the same. */
2091 if (VAR_P (base1)
2092 && VAR_P (base2)
2093 && DECL_HARD_REGISTER (base1)
2094 && DECL_HARD_REGISTER (base2)
2095 && DECL_ASSEMBLER_NAME_SET_P (base1)
2096 && DECL_ASSEMBLER_NAME_SET_P (base2))
2098 if (DECL_ASSEMBLER_NAME_RAW (base1) == DECL_ASSEMBLER_NAME_RAW (base2))
2099 return 1;
2100 return -1;
2103 /* Declarations of non-automatic variables may have aliases. All other
2104 decls are unique. */
2105 if (!decl_in_symtab_p (base1)
2106 || !decl_in_symtab_p (base2))
2107 return 0;
2109 /* Don't cause symbols to be inserted by the act of checking. */
2110 symtab_node *node1 = symtab_node::get (base1);
2111 if (!node1)
2112 return 0;
2113 symtab_node *node2 = symtab_node::get (base2);
2114 if (!node2)
2115 return 0;
2117 ret = node1->equal_address_to (node2, true);
2118 return ret;
2121 /* Compare SYMBOL_REFs X_BASE and Y_BASE.
2123 - Return 1 if Y_BASE - X_BASE is constant, adding that constant
2124 to *DISTANCE if DISTANCE is nonnull.
2126 - Return 0 if no accesses based on X_BASE can alias Y_BASE.
2128 - Return -1 if one of the two results applies, but we can't tell
2129 which at compile time. Update DISTANCE in the same way as
2130 for a return value of 1, for the case in which that holds. */
2132 static int
2133 compare_base_symbol_refs (const_rtx x_base, const_rtx y_base,
2134 HOST_WIDE_INT *distance)
2136 tree x_decl = SYMBOL_REF_DECL (x_base);
2137 tree y_decl = SYMBOL_REF_DECL (y_base);
2138 bool binds_def = true;
2139 bool swap = false;
2141 if (XSTR (x_base, 0) == XSTR (y_base, 0))
2142 return 1;
2143 if (x_decl && y_decl)
2144 return compare_base_decls (x_decl, y_decl);
2145 if (x_decl || y_decl)
2147 if (!x_decl)
2149 swap = true;
2150 std::swap (x_decl, y_decl);
2151 std::swap (x_base, y_base);
2153 /* We handle specially only section anchors. Other symbols are
2154 either equal (via aliasing) or refer to different objects. */
2155 if (!SYMBOL_REF_HAS_BLOCK_INFO_P (y_base))
2156 return -1;
2157 /* Anchors contains static VAR_DECLs and CONST_DECLs. We are safe
2158 to ignore CONST_DECLs because they are readonly. */
2159 if (!VAR_P (x_decl)
2160 || (!TREE_STATIC (x_decl) && !TREE_PUBLIC (x_decl)))
2161 return 0;
2163 symtab_node *x_node = symtab_node::get_create (x_decl)
2164 ->ultimate_alias_target ();
2165 /* External variable cannot be in section anchor. */
2166 if (!x_node->definition)
2167 return 0;
2168 x_base = XEXP (DECL_RTL (x_node->decl), 0);
2169 /* If not in anchor, we can disambiguate. */
2170 if (!SYMBOL_REF_HAS_BLOCK_INFO_P (x_base))
2171 return 0;
2173 /* We have an alias of anchored variable. If it can be interposed;
2174 we must assume it may or may not alias its anchor. */
2175 binds_def = decl_binds_to_current_def_p (x_decl);
2177 /* If we have variable in section anchor, we can compare by offset. */
2178 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x_base)
2179 && SYMBOL_REF_HAS_BLOCK_INFO_P (y_base))
2181 if (SYMBOL_REF_BLOCK (x_base) != SYMBOL_REF_BLOCK (y_base))
2182 return 0;
2183 if (distance)
2184 *distance += (swap ? -1 : 1) * (SYMBOL_REF_BLOCK_OFFSET (y_base)
2185 - SYMBOL_REF_BLOCK_OFFSET (x_base));
2186 return binds_def ? 1 : -1;
2188 /* Either the symbols are equal (via aliasing) or they refer to
2189 different objects. */
2190 return -1;
2193 /* Return false if the addresses X and Y are known to point to different
2194 objects, true if they might be pointers to the same object. */
2196 static bool
2197 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
2198 machine_mode x_mode, machine_mode y_mode)
2200 /* If the address itself has no known base see if a known equivalent
2201 value has one. If either address still has no known base, nothing
2202 is known about aliasing. */
2203 if (x_base == 0)
2205 rtx x_c;
2207 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
2208 return true;
2210 x_base = find_base_term (x_c);
2211 if (x_base == 0)
2212 return true;
2215 if (y_base == 0)
2217 rtx y_c;
2218 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
2219 return true;
2221 y_base = find_base_term (y_c);
2222 if (y_base == 0)
2223 return true;
2226 /* If the base addresses are equal nothing is known about aliasing. */
2227 if (rtx_equal_p (x_base, y_base))
2228 return true;
2230 /* The base addresses are different expressions. If they are not accessed
2231 via AND, there is no conflict. We can bring knowledge of object
2232 alignment into play here. For example, on alpha, "char a, b;" can
2233 alias one another, though "char a; long b;" cannot. AND addresses may
2234 implicitly alias surrounding objects; i.e. unaligned access in DImode
2235 via AND address can alias all surrounding object types except those
2236 with aligment 8 or higher. */
2237 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2238 return true;
2239 if (GET_CODE (x) == AND
2240 && (!CONST_INT_P (XEXP (x, 1))
2241 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2242 return true;
2243 if (GET_CODE (y) == AND
2244 && (!CONST_INT_P (XEXP (y, 1))
2245 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2246 return true;
2248 /* Differing symbols not accessed via AND never alias. */
2249 if (GET_CODE (x_base) == SYMBOL_REF && GET_CODE (y_base) == SYMBOL_REF)
2250 return compare_base_symbol_refs (x_base, y_base) != 0;
2252 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2253 return false;
2255 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2256 return false;
2258 return true;
2261 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2262 (or equal to) that of V. */
2264 static bool
2265 refs_newer_value_p (const_rtx expr, rtx v)
2267 int minuid = CSELIB_VAL_PTR (v)->uid;
2268 subrtx_iterator::array_type array;
2269 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2270 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid >= minuid)
2271 return true;
2272 return false;
2275 /* Convert the address X into something we can use. This is done by returning
2276 it unchanged unless it is a VALUE or VALUE +/- constant; for VALUE
2277 we call cselib to get a more useful rtx. */
2280 get_addr (rtx x)
2282 cselib_val *v;
2283 struct elt_loc_list *l;
2285 if (GET_CODE (x) != VALUE)
2287 if ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
2288 && GET_CODE (XEXP (x, 0)) == VALUE
2289 && CONST_SCALAR_INT_P (XEXP (x, 1)))
2291 rtx op0 = get_addr (XEXP (x, 0));
2292 if (op0 != XEXP (x, 0))
2294 poly_int64 c;
2295 if (GET_CODE (x) == PLUS
2296 && poly_int_rtx_p (XEXP (x, 1), &c))
2297 return plus_constant (GET_MODE (x), op0, c);
2298 return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
2299 op0, XEXP (x, 1));
2302 return x;
2304 v = CSELIB_VAL_PTR (x);
2305 if (v)
2307 bool have_equivs = cselib_have_permanent_equivalences ();
2308 if (have_equivs)
2309 v = canonical_cselib_val (v);
2310 for (l = v->locs; l; l = l->next)
2311 if (CONSTANT_P (l->loc))
2312 return l->loc;
2313 for (l = v->locs; l; l = l->next)
2314 if (!REG_P (l->loc) && !MEM_P (l->loc)
2315 /* Avoid infinite recursion when potentially dealing with
2316 var-tracking artificial equivalences, by skipping the
2317 equivalences themselves, and not choosing expressions
2318 that refer to newer VALUEs. */
2319 && (!have_equivs
2320 || (GET_CODE (l->loc) != VALUE
2321 && !refs_newer_value_p (l->loc, x))))
2322 return l->loc;
2323 if (have_equivs)
2325 for (l = v->locs; l; l = l->next)
2326 if (REG_P (l->loc)
2327 || (GET_CODE (l->loc) != VALUE
2328 && !refs_newer_value_p (l->loc, x)))
2329 return l->loc;
2330 /* Return the canonical value. */
2331 return v->val_rtx;
2333 if (v->locs)
2334 return v->locs->loc;
2336 return x;
2339 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2340 where SIZE is the size in bytes of the memory reference. If ADDR
2341 is not modified by the memory reference then ADDR is returned. */
2343 static rtx
2344 addr_side_effect_eval (rtx addr, poly_int64 size, int n_refs)
2346 poly_int64 offset = 0;
2348 switch (GET_CODE (addr))
2350 case PRE_INC:
2351 offset = (n_refs + 1) * size;
2352 break;
2353 case PRE_DEC:
2354 offset = -(n_refs + 1) * size;
2355 break;
2356 case POST_INC:
2357 offset = n_refs * size;
2358 break;
2359 case POST_DEC:
2360 offset = -n_refs * size;
2361 break;
2363 default:
2364 return addr;
2367 addr = plus_constant (GET_MODE (addr), XEXP (addr, 0), offset);
2368 addr = canon_rtx (addr);
2370 return addr;
2373 /* Return TRUE if an object X sized at XSIZE bytes and another object
2374 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2375 any of the sizes is zero, assume an overlap, otherwise use the
2376 absolute value of the sizes as the actual sizes. */
2378 static inline bool
2379 offset_overlap_p (poly_int64 c, poly_int64 xsize, poly_int64 ysize)
2381 if (known_eq (xsize, 0) || known_eq (ysize, 0))
2382 return true;
2384 if (maybe_ge (c, 0))
2385 return maybe_gt (maybe_lt (xsize, 0) ? -xsize : xsize, c);
2386 else
2387 return maybe_gt (maybe_lt (ysize, 0) ? -ysize : ysize, -c);
2390 /* Return one if X and Y (memory addresses) reference the
2391 same location in memory or if the references overlap.
2392 Return zero if they do not overlap, else return
2393 minus one in which case they still might reference the same location.
2395 C is an offset accumulator. When
2396 C is nonzero, we are testing aliases between X and Y + C.
2397 XSIZE is the size in bytes of the X reference,
2398 similarly YSIZE is the size in bytes for Y.
2399 Expect that canon_rtx has been already called for X and Y.
2401 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2402 referenced (the reference was BLKmode), so make the most pessimistic
2403 assumptions.
2405 If XSIZE or YSIZE is negative, we may access memory outside the object
2406 being referenced as a side effect. This can happen when using AND to
2407 align memory references, as is done on the Alpha.
2409 Nice to notice that varying addresses cannot conflict with fp if no
2410 local variables had their addresses taken, but that's too hard now.
2412 ??? Contrary to the tree alias oracle this does not return
2413 one for X + non-constant and Y + non-constant when X and Y are equal.
2414 If that is fixed the TBAA hack for union type-punning can be removed. */
2416 static int
2417 memrefs_conflict_p (poly_int64 xsize, rtx x, poly_int64 ysize, rtx y,
2418 poly_int64 c)
2420 if (GET_CODE (x) == VALUE)
2422 if (REG_P (y))
2424 struct elt_loc_list *l = NULL;
2425 if (CSELIB_VAL_PTR (x))
2426 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2427 l; l = l->next)
2428 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2429 break;
2430 if (l)
2431 x = y;
2432 else
2433 x = get_addr (x);
2435 /* Don't call get_addr if y is the same VALUE. */
2436 else if (x != y)
2437 x = get_addr (x);
2439 if (GET_CODE (y) == VALUE)
2441 if (REG_P (x))
2443 struct elt_loc_list *l = NULL;
2444 if (CSELIB_VAL_PTR (y))
2445 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2446 l; l = l->next)
2447 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2448 break;
2449 if (l)
2450 y = x;
2451 else
2452 y = get_addr (y);
2454 /* Don't call get_addr if x is the same VALUE. */
2455 else if (y != x)
2456 y = get_addr (y);
2458 if (GET_CODE (x) == HIGH)
2459 x = XEXP (x, 0);
2460 else if (GET_CODE (x) == LO_SUM)
2461 x = XEXP (x, 1);
2462 else
2463 x = addr_side_effect_eval (x, maybe_lt (xsize, 0) ? -xsize : xsize, 0);
2464 if (GET_CODE (y) == HIGH)
2465 y = XEXP (y, 0);
2466 else if (GET_CODE (y) == LO_SUM)
2467 y = XEXP (y, 1);
2468 else
2469 y = addr_side_effect_eval (y, maybe_lt (ysize, 0) ? -ysize : ysize, 0);
2471 if (GET_CODE (x) == SYMBOL_REF && GET_CODE (y) == SYMBOL_REF)
2473 HOST_WIDE_INT distance = 0;
2474 int cmp = compare_base_symbol_refs (x, y, &distance);
2476 /* If both decls are the same, decide by offsets. */
2477 if (cmp == 1)
2478 return offset_overlap_p (c + distance, xsize, ysize);
2479 /* Assume a potential overlap for symbolic addresses that went
2480 through alignment adjustments (i.e., that have negative
2481 sizes), because we can't know how far they are from each
2482 other. */
2483 if (maybe_lt (xsize, 0) || maybe_lt (ysize, 0))
2484 return -1;
2485 /* If decls are different or we know by offsets that there is no overlap,
2486 we win. */
2487 if (!cmp || !offset_overlap_p (c + distance, xsize, ysize))
2488 return 0;
2489 /* Decls may or may not be different and offsets overlap....*/
2490 return -1;
2492 else if (rtx_equal_for_memref_p (x, y))
2494 return offset_overlap_p (c, xsize, ysize);
2497 /* This code used to check for conflicts involving stack references and
2498 globals but the base address alias code now handles these cases. */
2500 if (GET_CODE (x) == PLUS)
2502 /* The fact that X is canonicalized means that this
2503 PLUS rtx is canonicalized. */
2504 rtx x0 = XEXP (x, 0);
2505 rtx x1 = XEXP (x, 1);
2507 /* However, VALUEs might end up in different positions even in
2508 canonical PLUSes. Comparing their addresses is enough. */
2509 if (x0 == y)
2510 return memrefs_conflict_p (xsize, x1, ysize, const0_rtx, c);
2511 else if (x1 == y)
2512 return memrefs_conflict_p (xsize, x0, ysize, const0_rtx, c);
2514 poly_int64 cx1, cy1;
2515 if (GET_CODE (y) == PLUS)
2517 /* The fact that Y is canonicalized means that this
2518 PLUS rtx is canonicalized. */
2519 rtx y0 = XEXP (y, 0);
2520 rtx y1 = XEXP (y, 1);
2522 if (x0 == y1)
2523 return memrefs_conflict_p (xsize, x1, ysize, y0, c);
2524 if (x1 == y0)
2525 return memrefs_conflict_p (xsize, x0, ysize, y1, c);
2527 if (rtx_equal_for_memref_p (x1, y1))
2528 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2529 if (rtx_equal_for_memref_p (x0, y0))
2530 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2531 if (poly_int_rtx_p (x1, &cx1))
2533 if (poly_int_rtx_p (y1, &cy1))
2534 return memrefs_conflict_p (xsize, x0, ysize, y0,
2535 c - cx1 + cy1);
2536 else
2537 return memrefs_conflict_p (xsize, x0, ysize, y, c - cx1);
2539 else if (poly_int_rtx_p (y1, &cy1))
2540 return memrefs_conflict_p (xsize, x, ysize, y0, c + cy1);
2542 return -1;
2544 else if (poly_int_rtx_p (x1, &cx1))
2545 return memrefs_conflict_p (xsize, x0, ysize, y, c - cx1);
2547 else if (GET_CODE (y) == PLUS)
2549 /* The fact that Y is canonicalized means that this
2550 PLUS rtx is canonicalized. */
2551 rtx y0 = XEXP (y, 0);
2552 rtx y1 = XEXP (y, 1);
2554 if (x == y0)
2555 return memrefs_conflict_p (xsize, const0_rtx, ysize, y1, c);
2556 if (x == y1)
2557 return memrefs_conflict_p (xsize, const0_rtx, ysize, y0, c);
2559 poly_int64 cy1;
2560 if (poly_int_rtx_p (y1, &cy1))
2561 return memrefs_conflict_p (xsize, x, ysize, y0, c + cy1);
2562 else
2563 return -1;
2566 if (GET_CODE (x) == GET_CODE (y))
2567 switch (GET_CODE (x))
2569 case MULT:
2571 /* Handle cases where we expect the second operands to be the
2572 same, and check only whether the first operand would conflict
2573 or not. */
2574 rtx x0, y0;
2575 rtx x1 = canon_rtx (XEXP (x, 1));
2576 rtx y1 = canon_rtx (XEXP (y, 1));
2577 if (! rtx_equal_for_memref_p (x1, y1))
2578 return -1;
2579 x0 = canon_rtx (XEXP (x, 0));
2580 y0 = canon_rtx (XEXP (y, 0));
2581 if (rtx_equal_for_memref_p (x0, y0))
2582 return offset_overlap_p (c, xsize, ysize);
2584 /* Can't properly adjust our sizes. */
2585 poly_int64 c1;
2586 if (!poly_int_rtx_p (x1, &c1)
2587 || !can_div_trunc_p (xsize, c1, &xsize)
2588 || !can_div_trunc_p (ysize, c1, &ysize)
2589 || !can_div_trunc_p (c, c1, &c))
2590 return -1;
2591 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2594 default:
2595 break;
2598 /* Deal with alignment ANDs by adjusting offset and size so as to
2599 cover the maximum range, without taking any previously known
2600 alignment into account. Make a size negative after such an
2601 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2602 assume a potential overlap, because they may end up in contiguous
2603 memory locations and the stricter-alignment access may span over
2604 part of both. */
2605 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2607 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2608 unsigned HOST_WIDE_INT uc = sc;
2609 if (sc < 0 && pow2_or_zerop (-uc))
2611 if (maybe_gt (xsize, 0))
2612 xsize = -xsize;
2613 if (maybe_ne (xsize, 0))
2614 xsize += sc + 1;
2615 c -= sc + 1;
2616 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2617 ysize, y, c);
2620 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2622 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2623 unsigned HOST_WIDE_INT uc = sc;
2624 if (sc < 0 && pow2_or_zerop (-uc))
2626 if (maybe_gt (ysize, 0))
2627 ysize = -ysize;
2628 if (maybe_ne (ysize, 0))
2629 ysize += sc + 1;
2630 c += sc + 1;
2631 return memrefs_conflict_p (xsize, x,
2632 ysize, canon_rtx (XEXP (y, 0)), c);
2636 if (CONSTANT_P (x))
2638 poly_int64 cx, cy;
2639 if (poly_int_rtx_p (x, &cx) && poly_int_rtx_p (y, &cy))
2641 c += cy - cx;
2642 return offset_overlap_p (c, xsize, ysize);
2645 if (GET_CODE (x) == CONST)
2647 if (GET_CODE (y) == CONST)
2648 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2649 ysize, canon_rtx (XEXP (y, 0)), c);
2650 else
2651 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2652 ysize, y, c);
2654 if (GET_CODE (y) == CONST)
2655 return memrefs_conflict_p (xsize, x, ysize,
2656 canon_rtx (XEXP (y, 0)), c);
2658 /* Assume a potential overlap for symbolic addresses that went
2659 through alignment adjustments (i.e., that have negative
2660 sizes), because we can't know how far they are from each
2661 other. */
2662 if (CONSTANT_P (y))
2663 return (maybe_lt (xsize, 0)
2664 || maybe_lt (ysize, 0)
2665 || offset_overlap_p (c, xsize, ysize));
2667 return -1;
2670 return -1;
2673 /* Functions to compute memory dependencies.
2675 Since we process the insns in execution order, we can build tables
2676 to keep track of what registers are fixed (and not aliased), what registers
2677 are varying in known ways, and what registers are varying in unknown
2678 ways.
2680 If both memory references are volatile, then there must always be a
2681 dependence between the two references, since their order cannot be
2682 changed. A volatile and non-volatile reference can be interchanged
2683 though.
2685 We also must allow AND addresses, because they may generate accesses
2686 outside the object being referenced. This is used to generate aligned
2687 addresses from unaligned addresses, for instance, the alpha
2688 storeqi_unaligned pattern. */
2690 /* Read dependence: X is read after read in MEM takes place. There can
2691 only be a dependence here if both reads are volatile, or if either is
2692 an explicit barrier. */
2694 bool
2695 read_dependence (const_rtx mem, const_rtx x)
2697 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2698 return true;
2699 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2700 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2701 return true;
2702 return false;
2705 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2707 static tree
2708 decl_for_component_ref (tree x)
2712 x = TREE_OPERAND (x, 0);
2714 while (x && TREE_CODE (x) == COMPONENT_REF);
2716 return x && DECL_P (x) ? x : NULL_TREE;
2719 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2720 for the offset of the field reference. *KNOWN_P says whether the
2721 offset is known. */
2723 static void
2724 adjust_offset_for_component_ref (tree x, bool *known_p,
2725 poly_int64 *offset)
2727 if (!*known_p)
2728 return;
2731 tree xoffset = component_ref_field_offset (x);
2732 tree field = TREE_OPERAND (x, 1);
2733 if (!poly_int_tree_p (xoffset))
2735 *known_p = false;
2736 return;
2739 poly_offset_int woffset
2740 = (wi::to_poly_offset (xoffset)
2741 + (wi::to_offset (DECL_FIELD_BIT_OFFSET (field))
2742 >> LOG2_BITS_PER_UNIT)
2743 + *offset);
2744 if (!woffset.to_shwi (offset))
2746 *known_p = false;
2747 return;
2750 x = TREE_OPERAND (x, 0);
2752 while (x && TREE_CODE (x) == COMPONENT_REF);
2755 /* Return true if we can determine the exprs corresponding to memrefs
2756 X and Y and they do not overlap.
2757 If LOOP_VARIANT is set, skip offset-based disambiguation */
2759 bool
2760 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2762 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2763 rtx rtlx, rtly;
2764 rtx basex, basey;
2765 bool moffsetx_known_p, moffsety_known_p;
2766 poly_int64 moffsetx = 0, moffsety = 0;
2767 poly_int64 offsetx = 0, offsety = 0, sizex, sizey;
2769 /* Unless both have exprs, we can't tell anything. */
2770 if (exprx == 0 || expry == 0)
2771 return false;
2773 /* For spill-slot accesses make sure we have valid offsets. */
2774 if ((exprx == get_spill_slot_decl (false)
2775 && ! MEM_OFFSET_KNOWN_P (x))
2776 || (expry == get_spill_slot_decl (false)
2777 && ! MEM_OFFSET_KNOWN_P (y)))
2778 return false;
2780 /* If the field reference test failed, look at the DECLs involved. */
2781 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2782 if (moffsetx_known_p)
2783 moffsetx = MEM_OFFSET (x);
2784 if (TREE_CODE (exprx) == COMPONENT_REF)
2786 tree t = decl_for_component_ref (exprx);
2787 if (! t)
2788 return false;
2789 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2790 exprx = t;
2793 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2794 if (moffsety_known_p)
2795 moffsety = MEM_OFFSET (y);
2796 if (TREE_CODE (expry) == COMPONENT_REF)
2798 tree t = decl_for_component_ref (expry);
2799 if (! t)
2800 return false;
2801 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2802 expry = t;
2805 if (! DECL_P (exprx) || ! DECL_P (expry))
2806 return false;
2808 /* If we refer to different gimple registers, or one gimple register
2809 and one non-gimple-register, we know they can't overlap. First,
2810 gimple registers don't have their addresses taken. Now, there
2811 could be more than one stack slot for (different versions of) the
2812 same gimple register, but we can presumably tell they don't
2813 overlap based on offsets from stack base addresses elsewhere.
2814 It's important that we don't proceed to DECL_RTL, because gimple
2815 registers may not pass DECL_RTL_SET_P, and make_decl_rtl won't be
2816 able to do anything about them since no SSA information will have
2817 remained to guide it. */
2818 if (is_gimple_reg (exprx) || is_gimple_reg (expry))
2819 return exprx != expry
2820 || (moffsetx_known_p && moffsety_known_p
2821 && MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y)
2822 && !offset_overlap_p (moffsety - moffsetx,
2823 MEM_SIZE (x), MEM_SIZE (y)));
2825 /* With invalid code we can end up storing into the constant pool.
2826 Bail out to avoid ICEing when creating RTL for this.
2827 See gfortran.dg/lto/20091028-2_0.f90. */
2828 if (TREE_CODE (exprx) == CONST_DECL
2829 || TREE_CODE (expry) == CONST_DECL)
2830 return true;
2832 /* If one decl is known to be a function or label in a function and
2833 the other is some kind of data, they can't overlap. */
2834 if ((TREE_CODE (exprx) == FUNCTION_DECL
2835 || TREE_CODE (exprx) == LABEL_DECL)
2836 != (TREE_CODE (expry) == FUNCTION_DECL
2837 || TREE_CODE (expry) == LABEL_DECL))
2838 return true;
2840 /* If either of the decls doesn't have DECL_RTL set (e.g. marked as
2841 living in multiple places), we can't tell anything. Exception
2842 are FUNCTION_DECLs for which we can create DECL_RTL on demand. */
2843 if ((!DECL_RTL_SET_P (exprx) && TREE_CODE (exprx) != FUNCTION_DECL)
2844 || (!DECL_RTL_SET_P (expry) && TREE_CODE (expry) != FUNCTION_DECL))
2845 return false;
2847 rtlx = DECL_RTL (exprx);
2848 rtly = DECL_RTL (expry);
2850 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2851 can't overlap unless they are the same because we never reuse that part
2852 of the stack frame used for locals for spilled pseudos. */
2853 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2854 && ! rtx_equal_p (rtlx, rtly))
2855 return true;
2857 /* If we have MEMs referring to different address spaces (which can
2858 potentially overlap), we cannot easily tell from the addresses
2859 whether the references overlap. */
2860 if (MEM_P (rtlx) && MEM_P (rtly)
2861 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2862 return false;
2864 /* Get the base and offsets of both decls. If either is a register, we
2865 know both are and are the same, so use that as the base. The only
2866 we can avoid overlap is if we can deduce that they are nonoverlapping
2867 pieces of that decl, which is very rare. */
2868 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2869 basex = strip_offset_and_add (basex, &offsetx);
2871 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2872 basey = strip_offset_and_add (basey, &offsety);
2874 /* If the bases are different, we know they do not overlap if both
2875 are constants or if one is a constant and the other a pointer into the
2876 stack frame. Otherwise a different base means we can't tell if they
2877 overlap or not. */
2878 if (compare_base_decls (exprx, expry) == 0)
2879 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2880 || (CONSTANT_P (basex) && REG_P (basey)
2881 && REGNO_PTR_FRAME_P (REGNO (basey)))
2882 || (CONSTANT_P (basey) && REG_P (basex)
2883 && REGNO_PTR_FRAME_P (REGNO (basex))));
2885 /* Offset based disambiguation not appropriate for loop invariant */
2886 if (loop_invariant)
2887 return false;
2889 /* Offset based disambiguation is OK even if we do not know that the
2890 declarations are necessarily different
2891 (i.e. compare_base_decls (exprx, expry) == -1) */
2893 sizex = (!MEM_P (rtlx) ? poly_int64 (GET_MODE_SIZE (GET_MODE (rtlx)))
2894 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2895 : -1);
2896 sizey = (!MEM_P (rtly) ? poly_int64 (GET_MODE_SIZE (GET_MODE (rtly)))
2897 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2898 : -1);
2900 /* If we have an offset for either memref, it can update the values computed
2901 above. */
2902 if (moffsetx_known_p)
2903 offsetx += moffsetx, sizex -= moffsetx;
2904 if (moffsety_known_p)
2905 offsety += moffsety, sizey -= moffsety;
2907 /* If a memref has both a size and an offset, we can use the smaller size.
2908 We can't do this if the offset isn't known because we must view this
2909 memref as being anywhere inside the DECL's MEM. */
2910 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2911 sizex = MEM_SIZE (x);
2912 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2913 sizey = MEM_SIZE (y);
2915 return !ranges_maybe_overlap_p (offsetx, sizex, offsety, sizey);
2918 /* Helper for true_dependence and canon_true_dependence.
2919 Checks for true dependence: X is read after store in MEM takes place.
2921 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2922 NULL_RTX, and the canonical addresses of MEM and X are both computed
2923 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2925 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2927 Returns true if there is a true dependence, false otherwise. */
2929 static bool
2930 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2931 const_rtx x, rtx x_addr, bool mem_canonicalized)
2933 rtx true_mem_addr;
2934 rtx base;
2935 int ret;
2937 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2938 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2940 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2941 return true;
2943 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2944 This is used in epilogue deallocation functions, and in cselib. */
2945 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2946 return true;
2947 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2948 return true;
2949 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2950 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2951 return true;
2953 if (! x_addr)
2954 x_addr = XEXP (x, 0);
2955 x_addr = get_addr (x_addr);
2957 if (! mem_addr)
2959 mem_addr = XEXP (mem, 0);
2960 if (mem_mode == VOIDmode)
2961 mem_mode = GET_MODE (mem);
2963 true_mem_addr = get_addr (mem_addr);
2965 /* Read-only memory is by definition never modified, and therefore can't
2966 conflict with anything. However, don't assume anything when AND
2967 addresses are involved and leave to the code below to determine
2968 dependence. We don't expect to find read-only set on MEM, but
2969 stupid user tricks can produce them, so don't die. */
2970 if (MEM_READONLY_P (x)
2971 && GET_CODE (x_addr) != AND
2972 && GET_CODE (true_mem_addr) != AND)
2973 return false;
2975 /* If we have MEMs referring to different address spaces (which can
2976 potentially overlap), we cannot easily tell from the addresses
2977 whether the references overlap. */
2978 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2979 return true;
2981 base = find_base_term (x_addr);
2982 if (base && (GET_CODE (base) == LABEL_REF
2983 || (GET_CODE (base) == SYMBOL_REF
2984 && CONSTANT_POOL_ADDRESS_P (base))))
2985 return false;
2987 rtx mem_base = find_base_term (true_mem_addr);
2988 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2989 GET_MODE (x), mem_mode))
2990 return false;
2992 x_addr = canon_rtx (x_addr);
2993 if (!mem_canonicalized)
2994 mem_addr = canon_rtx (true_mem_addr);
2996 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2997 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2998 return !!ret;
3000 if (mems_in_disjoint_alias_sets_p (x, mem))
3001 return false;
3003 if (nonoverlapping_memrefs_p (mem, x, false))
3004 return false;
3006 return rtx_refs_may_alias_p (x, mem, true);
3009 /* True dependence: X is read after store in MEM takes place. */
3011 bool
3012 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
3014 return true_dependence_1 (mem, mem_mode, NULL_RTX,
3015 x, NULL_RTX, /*mem_canonicalized=*/false);
3018 /* Canonical true dependence: X is read after store in MEM takes place.
3019 Variant of true_dependence which assumes MEM has already been
3020 canonicalized (hence we no longer do that here).
3021 The mem_addr argument has been added, since true_dependence_1 computed
3022 this value prior to canonicalizing. */
3024 bool
3025 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
3026 const_rtx x, rtx x_addr)
3028 return true_dependence_1 (mem, mem_mode, mem_addr,
3029 x, x_addr, /*mem_canonicalized=*/true);
3032 /* Returns true if a write to X might alias a previous read from
3033 (or, if WRITEP is true, a write to) MEM.
3034 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
3035 and X_MODE the mode for that access.
3036 If MEM_CANONICALIZED is true, MEM is canonicalized. */
3038 static bool
3039 write_dependence_p (const_rtx mem,
3040 const_rtx x, machine_mode x_mode, rtx x_addr,
3041 bool mem_canonicalized, bool x_canonicalized, bool writep)
3043 rtx mem_addr;
3044 rtx true_mem_addr, true_x_addr;
3045 rtx base;
3046 int ret;
3048 gcc_checking_assert (x_canonicalized
3049 ? (x_addr != NULL_RTX
3050 && (x_mode != VOIDmode || GET_MODE (x) == VOIDmode))
3051 : (x_addr == NULL_RTX && x_mode == VOIDmode));
3053 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
3054 return true;
3056 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
3057 This is used in epilogue deallocation functions. */
3058 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3059 return true;
3060 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3061 return true;
3062 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3063 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3064 return true;
3066 if (!x_addr)
3067 x_addr = XEXP (x, 0);
3068 true_x_addr = get_addr (x_addr);
3070 mem_addr = XEXP (mem, 0);
3071 true_mem_addr = get_addr (mem_addr);
3073 /* A read from read-only memory can't conflict with read-write memory.
3074 Don't assume anything when AND addresses are involved and leave to
3075 the code below to determine dependence. */
3076 if (!writep
3077 && MEM_READONLY_P (mem)
3078 && GET_CODE (true_x_addr) != AND
3079 && GET_CODE (true_mem_addr) != AND)
3080 return false;
3082 /* If we have MEMs referring to different address spaces (which can
3083 potentially overlap), we cannot easily tell from the addresses
3084 whether the references overlap. */
3085 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3086 return true;
3088 base = find_base_term (true_mem_addr);
3089 if (! writep
3090 && base
3091 && (GET_CODE (base) == LABEL_REF
3092 || (GET_CODE (base) == SYMBOL_REF
3093 && CONSTANT_POOL_ADDRESS_P (base))))
3094 return false;
3096 rtx x_base = find_base_term (true_x_addr);
3097 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
3098 GET_MODE (x), GET_MODE (mem)))
3099 return false;
3101 if (!x_canonicalized)
3103 x_addr = canon_rtx (true_x_addr);
3104 x_mode = GET_MODE (x);
3106 if (!mem_canonicalized)
3107 mem_addr = canon_rtx (true_mem_addr);
3109 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
3110 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
3111 return !!ret;
3113 if (nonoverlapping_memrefs_p (x, mem, false))
3114 return false;
3116 return rtx_refs_may_alias_p (x, mem, false);
3119 /* Anti dependence: X is written after read in MEM takes place. */
3121 bool
3122 anti_dependence (const_rtx mem, const_rtx x)
3124 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
3125 /*mem_canonicalized=*/false,
3126 /*x_canonicalized*/false, /*writep=*/false);
3129 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
3130 Also, consider X in X_MODE (which might be from an enclosing
3131 STRICT_LOW_PART / ZERO_EXTRACT).
3132 If MEM_CANONICALIZED is true, MEM is canonicalized. */
3134 bool
3135 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
3136 const_rtx x, machine_mode x_mode, rtx x_addr)
3138 return write_dependence_p (mem, x, x_mode, x_addr,
3139 mem_canonicalized, /*x_canonicalized=*/true,
3140 /*writep=*/false);
3143 /* Output dependence: X is written after store in MEM takes place. */
3145 bool
3146 output_dependence (const_rtx mem, const_rtx x)
3148 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
3149 /*mem_canonicalized=*/false,
3150 /*x_canonicalized*/false, /*writep=*/true);
3153 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
3154 Also, consider X in X_MODE (which might be from an enclosing
3155 STRICT_LOW_PART / ZERO_EXTRACT).
3156 If MEM_CANONICALIZED is true, MEM is canonicalized. */
3158 bool
3159 canon_output_dependence (const_rtx mem, bool mem_canonicalized,
3160 const_rtx x, machine_mode x_mode, rtx x_addr)
3162 return write_dependence_p (mem, x, x_mode, x_addr,
3163 mem_canonicalized, /*x_canonicalized=*/true,
3164 /*writep=*/true);
3169 /* Check whether X may be aliased with MEM. Don't do offset-based
3170 memory disambiguation & TBAA. */
3171 bool
3172 may_alias_p (const_rtx mem, const_rtx x)
3174 rtx x_addr, mem_addr;
3176 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
3177 return true;
3179 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
3180 This is used in epilogue deallocation functions. */
3181 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3182 return true;
3183 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3184 return true;
3185 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3186 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3187 return true;
3189 x_addr = XEXP (x, 0);
3190 x_addr = get_addr (x_addr);
3192 mem_addr = XEXP (mem, 0);
3193 mem_addr = get_addr (mem_addr);
3195 /* Read-only memory is by definition never modified, and therefore can't
3196 conflict with anything. However, don't assume anything when AND
3197 addresses are involved and leave to the code below to determine
3198 dependence. We don't expect to find read-only set on MEM, but
3199 stupid user tricks can produce them, so don't die. */
3200 if (MEM_READONLY_P (x)
3201 && GET_CODE (x_addr) != AND
3202 && GET_CODE (mem_addr) != AND)
3203 return false;
3205 /* If we have MEMs referring to different address spaces (which can
3206 potentially overlap), we cannot easily tell from the addresses
3207 whether the references overlap. */
3208 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3209 return true;
3211 rtx x_base = find_base_term (x_addr);
3212 rtx mem_base = find_base_term (mem_addr);
3213 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
3214 GET_MODE (x), GET_MODE (mem_addr)))
3215 return false;
3217 if (nonoverlapping_memrefs_p (mem, x, true))
3218 return false;
3220 /* TBAA not valid for loop_invarint */
3221 return rtx_refs_may_alias_p (x, mem, false);
3224 void
3225 init_alias_target (void)
3227 int i;
3229 if (!arg_base_value)
3230 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
3232 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
3234 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3235 /* Check whether this register can hold an incoming pointer
3236 argument. FUNCTION_ARG_REGNO_P tests outgoing register
3237 numbers, so translate if necessary due to register windows. */
3238 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
3239 && targetm.hard_regno_mode_ok (i, Pmode))
3240 static_reg_base_value[i] = arg_base_value;
3242 /* RTL code is required to be consistent about whether it uses the
3243 stack pointer, the frame pointer or the argument pointer to
3244 access a given area of the frame. We can therefore use the
3245 base address to distinguish between the different areas. */
3246 static_reg_base_value[STACK_POINTER_REGNUM]
3247 = unique_base_value (UNIQUE_BASE_VALUE_SP);
3248 static_reg_base_value[ARG_POINTER_REGNUM]
3249 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
3250 static_reg_base_value[FRAME_POINTER_REGNUM]
3251 = unique_base_value (UNIQUE_BASE_VALUE_FP);
3253 /* The above rules extend post-reload, with eliminations applying
3254 consistently to each of the three pointers. Cope with cases in
3255 which the frame pointer is eliminated to the hard frame pointer
3256 rather than the stack pointer. */
3257 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3258 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
3259 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
3262 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
3263 to be memory reference. */
3264 static bool memory_modified;
3265 static void
3266 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
3268 if (MEM_P (x))
3270 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
3271 memory_modified = true;
3276 /* Return true when INSN possibly modify memory contents of MEM
3277 (i.e. address can be modified). */
3278 bool
3279 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
3281 if (!INSN_P (insn))
3282 return false;
3283 /* Conservatively assume all non-readonly MEMs might be modified in
3284 calls. */
3285 if (CALL_P (insn))
3286 return true;
3287 memory_modified = false;
3288 note_stores (as_a<const rtx_insn *> (insn), memory_modified_1,
3289 CONST_CAST_RTX(mem));
3290 return memory_modified;
3293 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3294 array. */
3296 void
3297 init_alias_analysis (void)
3299 const bool frame_pointer_eliminated
3300 = reload_completed
3301 && !frame_pointer_needed
3302 && targetm.can_eliminate (FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM);
3303 unsigned int maxreg = max_reg_num ();
3304 bool changed;
3305 int pass, i;
3306 unsigned int ui;
3307 rtx_insn *insn;
3308 rtx val;
3309 int rpo_cnt;
3310 int *rpo;
3312 timevar_push (TV_ALIAS_ANALYSIS);
3314 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER,
3315 true);
3316 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3317 bitmap_clear (reg_known_equiv_p);
3319 /* If we have memory allocated from the previous run, use it. */
3320 if (old_reg_base_value)
3321 reg_base_value = old_reg_base_value;
3323 if (reg_base_value)
3324 reg_base_value->truncate (0);
3326 vec_safe_grow_cleared (reg_base_value, maxreg, true);
3328 new_reg_base_value = XNEWVEC (rtx, maxreg);
3329 reg_seen = sbitmap_alloc (maxreg);
3331 /* The basic idea is that each pass through this loop will use the
3332 "constant" information from the previous pass to propagate alias
3333 information through another level of assignments.
3335 The propagation is done on the CFG in reverse post-order, to propagate
3336 things forward as far as possible in each iteration.
3338 This could get expensive if the assignment chains are long. Maybe
3339 we should throttle the number of iterations, possibly based on
3340 the optimization level or flag_expensive_optimizations.
3342 We could propagate more information in the first pass by making use
3343 of DF_REG_DEF_COUNT to determine immediately that the alias information
3344 for a pseudo is "constant".
3346 A program with an uninitialized variable can cause an infinite loop
3347 here. Instead of doing a full dataflow analysis to detect such problems
3348 we just cap the number of iterations for the loop.
3350 The state of the arrays for the set chain in question does not matter
3351 since the program has undefined behavior. */
3353 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3354 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3356 pass = 0;
3359 /* Assume nothing will change this iteration of the loop. */
3360 changed = false;
3362 /* We want to assign the same IDs each iteration of this loop, so
3363 start counting from one each iteration of the loop. */
3364 unique_id = 1;
3366 /* We're at the start of the function each iteration through the
3367 loop, so we're copying arguments. */
3368 copying_arguments = true;
3370 /* Wipe the potential alias information clean for this pass. */
3371 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3373 /* Wipe the reg_seen array clean. */
3374 bitmap_clear (reg_seen);
3376 /* Initialize the alias information for this pass. */
3377 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3378 if (static_reg_base_value[i]
3379 /* Don't treat the hard frame pointer as special if we
3380 eliminated the frame pointer to the stack pointer. */
3381 && !(i == HARD_FRAME_POINTER_REGNUM && frame_pointer_eliminated))
3383 new_reg_base_value[i] = static_reg_base_value[i];
3384 bitmap_set_bit (reg_seen, i);
3387 /* Walk the insns adding values to the new_reg_base_value array. */
3388 for (i = 0; i < rpo_cnt; i++)
3390 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3391 FOR_BB_INSNS (bb, insn)
3393 if (NONDEBUG_INSN_P (insn))
3395 rtx note, set;
3397 /* Treat the hard frame pointer as special unless we
3398 eliminated the frame pointer to the stack pointer. */
3399 if (!frame_pointer_eliminated
3400 && modified_in_p (hard_frame_pointer_rtx, insn))
3401 continue;
3403 /* If this insn has a noalias note, process it, Otherwise,
3404 scan for sets. A simple set will have no side effects
3405 which could change the base value of any other register. */
3406 if (GET_CODE (PATTERN (insn)) == SET
3407 && REG_NOTES (insn) != 0
3408 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3409 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3410 else
3411 note_stores (insn, record_set, NULL);
3413 set = single_set (insn);
3415 if (set != 0
3416 && REG_P (SET_DEST (set))
3417 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3419 unsigned int regno = REGNO (SET_DEST (set));
3420 rtx src = SET_SRC (set);
3421 rtx t;
3423 note = find_reg_equal_equiv_note (insn);
3424 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3425 && DF_REG_DEF_COUNT (regno) != 1)
3426 note = NULL_RTX;
3428 poly_int64 offset;
3429 if (note != NULL_RTX
3430 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3431 && ! rtx_varies_p (XEXP (note, 0), 1)
3432 && ! reg_overlap_mentioned_p (SET_DEST (set),
3433 XEXP (note, 0)))
3435 set_reg_known_value (regno, XEXP (note, 0));
3436 set_reg_known_equiv_p (regno,
3437 REG_NOTE_KIND (note) == REG_EQUIV);
3439 else if (DF_REG_DEF_COUNT (regno) == 1
3440 && GET_CODE (src) == PLUS
3441 && REG_P (XEXP (src, 0))
3442 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3443 && poly_int_rtx_p (XEXP (src, 1), &offset))
3445 t = plus_constant (GET_MODE (src), t, offset);
3446 set_reg_known_value (regno, t);
3447 set_reg_known_equiv_p (regno, false);
3449 else if (DF_REG_DEF_COUNT (regno) == 1
3450 && ! rtx_varies_p (src, 1))
3452 set_reg_known_value (regno, src);
3453 set_reg_known_equiv_p (regno, false);
3457 else if (NOTE_P (insn)
3458 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3459 copying_arguments = false;
3463 /* Now propagate values from new_reg_base_value to reg_base_value. */
3464 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3466 for (ui = 0; ui < maxreg; ui++)
3468 if (new_reg_base_value[ui]
3469 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3470 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3472 (*reg_base_value)[ui] = new_reg_base_value[ui];
3473 changed = true;
3477 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3478 XDELETEVEC (rpo);
3480 /* Fill in the remaining entries. */
3481 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3483 int regno = i + FIRST_PSEUDO_REGISTER;
3484 if (! val)
3485 set_reg_known_value (regno, regno_reg_rtx[regno]);
3488 /* Clean up. */
3489 free (new_reg_base_value);
3490 new_reg_base_value = 0;
3491 sbitmap_free (reg_seen);
3492 reg_seen = 0;
3493 timevar_pop (TV_ALIAS_ANALYSIS);
3496 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3497 Special API for var-tracking pass purposes. */
3499 void
3500 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3502 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3505 void
3506 end_alias_analysis (void)
3508 old_reg_base_value = reg_base_value;
3509 vec_free (reg_known_value);
3510 sbitmap_free (reg_known_equiv_p);
3513 void
3514 dump_alias_stats_in_alias_c (FILE *s)
3516 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3517 " %llu are in alias set 0\n"
3518 " %llu queries asked about the same object\n"
3519 " %llu queries asked about the same alias set\n"
3520 " %llu access volatile\n"
3521 " %llu are dependent in the DAG\n"
3522 " %llu are aritificially in conflict with void *\n",
3523 alias_stats.num_disambiguated,
3524 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3525 + alias_stats.num_same_objects + alias_stats.num_volatile
3526 + alias_stats.num_dag + alias_stats.num_disambiguated
3527 + alias_stats.num_universal,
3528 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3529 alias_stats.num_same_objects, alias_stats.num_volatile,
3530 alias_stats.num_dag, alias_stats.num_universal);
3532 #include "gt-alias.h"