* config/rs6000/rs6000.c (rs6000_xcoff_asm_named_section): Place
[official-gcc.git] / gcc / alias.c
blob1c58547df72372caa3f69b46f21367aff7a20c31
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
3 Contributed by John Carr (jfc@mit.edu).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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 "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "df.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "varasm.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expmed.h"
35 #include "dojump.h"
36 #include "explow.h"
37 #include "calls.h"
38 #include "emit-rtl.h"
39 #include "stmt.h"
40 #include "expr.h"
41 #include "tm_p.h"
42 #include "regs.h"
43 #include "diagnostic-core.h"
44 #include "alloc-pool.h"
45 #include "cselib.h"
46 #include "langhooks.h"
47 #include "timevar.h"
48 #include "dumpfile.h"
49 #include "target.h"
50 #include "cfganal.h"
51 #include "internal-fn.h"
52 #include "gimple-ssa.h"
53 #include "rtl-iter.h"
55 /* The aliasing API provided here solves related but different problems:
57 Say there exists (in c)
59 struct X {
60 struct Y y1;
61 struct Z z2;
62 } x1, *px1, *px2;
64 struct Y y2, *py;
65 struct Z z2, *pz;
68 py = &x1.y1;
69 px2 = &x1;
71 Consider the four questions:
73 Can a store to x1 interfere with px2->y1?
74 Can a store to x1 interfere with px2->z2?
75 Can a store to x1 change the value pointed to by with py?
76 Can a store to x1 change the value pointed to by with pz?
78 The answer to these questions can be yes, yes, yes, and maybe.
80 The first two questions can be answered with a simple examination
81 of the type system. If structure X contains a field of type Y then
82 a store through a pointer to an X can overwrite any field that is
83 contained (recursively) in an X (unless we know that px1 != px2).
85 The last two questions can be solved in the same way as the first
86 two questions but this is too conservative. The observation is
87 that in some cases we can know which (if any) fields are addressed
88 and if those addresses are used in bad ways. This analysis may be
89 language specific. In C, arbitrary operations may be applied to
90 pointers. However, there is some indication that this may be too
91 conservative for some C++ types.
93 The pass ipa-type-escape does this analysis for the types whose
94 instances do not escape across the compilation boundary.
96 Historically in GCC, these two problems were combined and a single
97 data structure that was used to represent the solution to these
98 problems. We now have two similar but different data structures,
99 The data structure to solve the last two questions is similar to
100 the first, but does not contain the fields whose address are never
101 taken. For types that do escape the compilation unit, the data
102 structures will have identical information.
105 /* The alias sets assigned to MEMs assist the back-end in determining
106 which MEMs can alias which other MEMs. In general, two MEMs in
107 different alias sets cannot alias each other, with one important
108 exception. Consider something like:
110 struct S { int i; double d; };
112 a store to an `S' can alias something of either type `int' or type
113 `double'. (However, a store to an `int' cannot alias a `double'
114 and vice versa.) We indicate this via a tree structure that looks
115 like:
116 struct S
119 |/_ _\|
120 int double
122 (The arrows are directed and point downwards.)
123 In this situation we say the alias set for `struct S' is the
124 `superset' and that those for `int' and `double' are `subsets'.
126 To see whether two alias sets can point to the same memory, we must
127 see if either alias set is a subset of the other. We need not trace
128 past immediate descendants, however, since we propagate all
129 grandchildren up one level.
131 Alias set zero is implicitly a superset of all other alias sets.
132 However, this is no actual entry for alias set zero. It is an
133 error to attempt to explicitly construct a subset of zero. */
135 struct alias_set_hash : int_hash <int, INT_MIN, INT_MIN + 1> {};
137 struct GTY(()) alias_set_entry {
138 /* The alias set number, as stored in MEM_ALIAS_SET. */
139 alias_set_type alias_set;
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;
150 /* Nonzero if would have a child of zero: this effectively makes this
151 alias set the same as alias set zero. */
152 bool has_zero_child;
153 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
154 aggregate contaiing pointer.
155 This is used for a special case where we need an universal pointer type
156 compatible with all other pointer types. */
157 bool is_pointer;
158 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
159 bool has_pointer;
162 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
163 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
164 static void record_set (rtx, const_rtx, void *);
165 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
166 machine_mode);
167 static rtx find_base_value (rtx);
168 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
169 static alias_set_entry *get_alias_set_entry (alias_set_type);
170 static tree decl_for_component_ref (tree);
171 static int write_dependence_p (const_rtx,
172 const_rtx, machine_mode, rtx,
173 bool, bool, bool);
175 static void memory_modified_1 (rtx, const_rtx, void *);
177 /* Query statistics for the different low-level disambiguators.
178 A high-level query may trigger multiple of them. */
180 static struct {
181 unsigned long long num_alias_zero;
182 unsigned long long num_same_alias_set;
183 unsigned long long num_same_objects;
184 unsigned long long num_volatile;
185 unsigned long long num_dag;
186 unsigned long long num_universal;
187 unsigned long long num_disambiguated;
188 } alias_stats;
191 /* Set up all info needed to perform alias analysis on memory references. */
193 /* Returns the size in bytes of the mode of X. */
194 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
196 /* Cap the number of passes we make over the insns propagating alias
197 information through set chains.
198 ??? 10 is a completely arbitrary choice. This should be based on the
199 maximum loop depth in the CFG, but we do not have this information
200 available (even if current_loops _is_ available). */
201 #define MAX_ALIAS_LOOP_PASSES 10
203 /* reg_base_value[N] gives an address to which register N is related.
204 If all sets after the first add or subtract to the current value
205 or otherwise modify it so it does not point to a different top level
206 object, reg_base_value[N] is equal to the address part of the source
207 of the first set.
209 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
210 expressions represent three types of base:
212 1. incoming arguments. There is just one ADDRESS to represent all
213 arguments, since we do not know at this level whether accesses
214 based on different arguments can alias. The ADDRESS has id 0.
216 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
217 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
218 Each of these rtxes has a separate ADDRESS associated with it,
219 each with a negative id.
221 GCC is (and is required to be) precise in which register it
222 chooses to access a particular region of stack. We can therefore
223 assume that accesses based on one of these rtxes do not alias
224 accesses based on another of these rtxes.
226 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
227 Each such piece of memory has a separate ADDRESS associated
228 with it, each with an id greater than 0.
230 Accesses based on one ADDRESS do not alias accesses based on other
231 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
232 alias globals either; the ADDRESSes have Pmode to indicate this.
233 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
234 indicate this. */
236 static GTY(()) vec<rtx, va_gc> *reg_base_value;
237 static rtx *new_reg_base_value;
239 /* The single VOIDmode ADDRESS that represents all argument bases.
240 It has id 0. */
241 static GTY(()) rtx arg_base_value;
243 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
244 static int unique_id;
246 /* We preserve the copy of old array around to avoid amount of garbage
247 produced. About 8% of garbage produced were attributed to this
248 array. */
249 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
251 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
252 registers. */
253 #define UNIQUE_BASE_VALUE_SP -1
254 #define UNIQUE_BASE_VALUE_ARGP -2
255 #define UNIQUE_BASE_VALUE_FP -3
256 #define UNIQUE_BASE_VALUE_HFP -4
258 #define static_reg_base_value \
259 (this_target_rtl->x_static_reg_base_value)
261 #define REG_BASE_VALUE(X) \
262 (REGNO (X) < vec_safe_length (reg_base_value) \
263 ? (*reg_base_value)[REGNO (X)] : 0)
265 /* Vector indexed by N giving the initial (unchanging) value known for
266 pseudo-register N. This vector is initialized in init_alias_analysis,
267 and does not change until end_alias_analysis is called. */
268 static GTY(()) vec<rtx, va_gc> *reg_known_value;
270 /* Vector recording for each reg_known_value whether it is due to a
271 REG_EQUIV note. Future passes (viz., reload) may replace the
272 pseudo with the equivalent expression and so we account for the
273 dependences that would be introduced if that happens.
275 The REG_EQUIV notes created in assign_parms may mention the arg
276 pointer, and there are explicit insns in the RTL that modify the
277 arg pointer. Thus we must ensure that such insns don't get
278 scheduled across each other because that would invalidate the
279 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
280 wrong, but solving the problem in the scheduler will likely give
281 better code, so we do it here. */
282 static sbitmap reg_known_equiv_p;
284 /* True when scanning insns from the start of the rtl to the
285 NOTE_INSN_FUNCTION_BEG note. */
286 static bool copying_arguments;
289 /* The splay-tree used to store the various alias set entries. */
290 static GTY (()) vec<alias_set_entry *, va_gc> *alias_sets;
292 /* Build a decomposed reference object for querying the alias-oracle
293 from the MEM rtx and store it in *REF.
294 Returns false if MEM is not suitable for the alias-oracle. */
296 static bool
297 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
299 tree expr = MEM_EXPR (mem);
300 tree base;
302 if (!expr)
303 return false;
305 ao_ref_init (ref, expr);
307 /* Get the base of the reference and see if we have to reject or
308 adjust it. */
309 base = ao_ref_base (ref);
310 if (base == NULL_TREE)
311 return false;
313 /* The tree oracle doesn't like bases that are neither decls
314 nor indirect references of SSA names. */
315 if (!(DECL_P (base)
316 || (TREE_CODE (base) == MEM_REF
317 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
318 || (TREE_CODE (base) == TARGET_MEM_REF
319 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
320 return false;
322 /* If this is a reference based on a partitioned decl replace the
323 base with a MEM_REF of the pointer representative we
324 created during stack slot partitioning. */
325 if (TREE_CODE (base) == VAR_DECL
326 && ! is_global_var (base)
327 && cfun->gimple_df->decls_to_pointers != NULL)
329 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
330 if (namep)
331 ref->base = build_simple_mem_ref (*namep);
334 ref->ref_alias_set = MEM_ALIAS_SET (mem);
336 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
337 is conservative, so trust it. */
338 if (!MEM_OFFSET_KNOWN_P (mem)
339 || !MEM_SIZE_KNOWN_P (mem))
340 return true;
342 /* If the base decl is a parameter we can have negative MEM_OFFSET in
343 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
344 here. */
345 if (MEM_OFFSET (mem) < 0
346 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
347 return true;
349 /* Otherwise continue and refine size and offset we got from analyzing
350 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
352 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
353 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
355 /* The MEM may extend into adjacent fields, so adjust max_size if
356 necessary. */
357 if (ref->max_size != -1
358 && ref->size > ref->max_size)
359 ref->max_size = ref->size;
361 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
362 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
363 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
364 && (ref->offset < 0
365 || (DECL_P (ref->base)
366 && (DECL_SIZE (ref->base) == NULL_TREE
367 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
368 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
369 ref->offset + ref->size)))))
370 return false;
372 return true;
375 /* Query the alias-oracle on whether the two memory rtx X and MEM may
376 alias. If TBAA_P is set also apply TBAA. Returns true if the
377 two rtxen may alias, false otherwise. */
379 static bool
380 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
382 ao_ref ref1, ref2;
384 if (!ao_ref_from_mem (&ref1, x)
385 || !ao_ref_from_mem (&ref2, mem))
386 return true;
388 return refs_may_alias_p_1 (&ref1, &ref2,
389 tbaa_p
390 && MEM_ALIAS_SET (x) != 0
391 && MEM_ALIAS_SET (mem) != 0);
394 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
395 such an entry, or NULL otherwise. */
397 static inline alias_set_entry *
398 get_alias_set_entry (alias_set_type alias_set)
400 return (*alias_sets)[alias_set];
403 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
404 the two MEMs cannot alias each other. */
406 static inline int
407 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
409 return (flag_strict_aliasing
410 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
411 MEM_ALIAS_SET (mem2)));
414 /* Return true if the first alias set is a subset of the second. */
416 bool
417 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
419 alias_set_entry *ase2;
421 /* Everything is a subset of the "aliases everything" set. */
422 if (set2 == 0)
423 return true;
425 /* Check if set1 is a subset of set2. */
426 ase2 = get_alias_set_entry (set2);
427 if (ase2 != 0
428 && (ase2->has_zero_child
429 || (ase2->children && ase2->children->get (set1))))
430 return true;
432 /* As a special case we consider alias set of "void *" to be both subset
433 and superset of every alias set of a pointer. This extra symmetry does
434 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
435 to return true on the following testcase:
437 void *ptr;
438 char **ptr2=(char **)&ptr;
439 *ptr2 = ...
441 Additionally if a set contains universal pointer, we consider every pointer
442 to be a subset of it, but we do not represent this explicitely - doing so
443 would require us to update transitive closure each time we introduce new
444 pointer type. This makes aliasing_component_refs_p to return true
445 on the following testcase:
447 struct a {void *ptr;}
448 char **ptr = (char **)&a.ptr;
449 ptr = ...
451 This makes void * truly universal pointer type. See pointer handling in
452 get_alias_set for more details. */
453 if (ase2 && ase2->has_pointer)
455 alias_set_entry *ase1 = get_alias_set_entry (set1);
457 if (ase1 && ase1->is_pointer)
459 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
460 /* If one is ptr_type_node and other is pointer, then we consider
461 them subset of each other. */
462 if (set1 == voidptr_set || set2 == voidptr_set)
463 return true;
464 /* If SET2 contains universal pointer's alias set, then we consdier
465 every (non-universal) pointer. */
466 if (ase2->children && set1 != voidptr_set
467 && ase2->children->get (voidptr_set))
468 return true;
471 return false;
474 /* Return 1 if the two specified alias sets may conflict. */
477 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
479 alias_set_entry *ase1;
480 alias_set_entry *ase2;
482 /* The easy case. */
483 if (alias_sets_must_conflict_p (set1, set2))
484 return 1;
486 /* See if the first alias set is a subset of the second. */
487 ase1 = get_alias_set_entry (set1);
488 if (ase1 != 0
489 && ase1->children && ase1->children->get (set2))
491 ++alias_stats.num_dag;
492 return 1;
495 /* Now do the same, but with the alias sets reversed. */
496 ase2 = get_alias_set_entry (set2);
497 if (ase2 != 0
498 && ase2->children && ase2->children->get (set1))
500 ++alias_stats.num_dag;
501 return 1;
504 /* We want void * to be compatible with any other pointer without
505 really dropping it to alias set 0. Doing so would make it
506 compatible with all non-pointer types too.
508 This is not strictly necessary by the C/C++ language
509 standards, but avoids common type punning mistakes. In
510 addition to that, we need the existence of such universal
511 pointer to implement Fortran's C_PTR type (which is defined as
512 type compatible with all C pointers). */
513 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
515 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
517 /* If one of the sets corresponds to universal pointer,
518 we consider it to conflict with anything that is
519 or contains pointer. */
520 if (set1 == voidptr_set || set2 == voidptr_set)
522 ++alias_stats.num_universal;
523 return true;
525 /* If one of sets is (non-universal) pointer and the other
526 contains universal pointer, we also get conflict. */
527 if (ase1->is_pointer && set2 != voidptr_set
528 && ase2->children && ase2->children->get (voidptr_set))
530 ++alias_stats.num_universal;
531 return true;
533 if (ase2->is_pointer && set1 != voidptr_set
534 && ase1->children && ase1->children->get (voidptr_set))
536 ++alias_stats.num_universal;
537 return true;
541 ++alias_stats.num_disambiguated;
543 /* The two alias sets are distinct and neither one is the
544 child of the other. Therefore, they cannot conflict. */
545 return 0;
548 /* Return 1 if the two specified alias sets will always conflict. */
551 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
553 if (set1 == 0 || set2 == 0)
555 ++alias_stats.num_alias_zero;
556 return 1;
558 if (set1 == set2)
560 ++alias_stats.num_same_alias_set;
561 return 1;
564 return 0;
567 /* Return 1 if any MEM object of type T1 will always conflict (using the
568 dependency routines in this file) with any MEM object of type T2.
569 This is used when allocating temporary storage. If T1 and/or T2 are
570 NULL_TREE, it means we know nothing about the storage. */
573 objects_must_conflict_p (tree t1, tree t2)
575 alias_set_type set1, set2;
577 /* If neither has a type specified, we don't know if they'll conflict
578 because we may be using them to store objects of various types, for
579 example the argument and local variables areas of inlined functions. */
580 if (t1 == 0 && t2 == 0)
581 return 0;
583 /* If they are the same type, they must conflict. */
584 if (t1 == t2)
586 ++alias_stats.num_same_objects;
587 return 1;
589 /* Likewise if both are volatile. */
590 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
592 ++alias_stats.num_volatile;
593 return 1;
596 set1 = t1 ? get_alias_set (t1) : 0;
597 set2 = t2 ? get_alias_set (t2) : 0;
599 /* We can't use alias_sets_conflict_p because we must make sure
600 that every subtype of t1 will conflict with every subtype of
601 t2 for which a pair of subobjects of these respective subtypes
602 overlaps on the stack. */
603 return alias_sets_must_conflict_p (set1, set2);
606 /* Return the outermost parent of component present in the chain of
607 component references handled by get_inner_reference in T with the
608 following property:
609 - the component is non-addressable, or
610 - the parent has alias set zero,
611 or NULL_TREE if no such parent exists. In the former cases, the alias
612 set of this parent is the alias set that must be used for T itself. */
614 tree
615 component_uses_parent_alias_set_from (const_tree t)
617 const_tree found = NULL_TREE;
619 while (handled_component_p (t))
621 switch (TREE_CODE (t))
623 case COMPONENT_REF:
624 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
625 found = t;
626 break;
628 case ARRAY_REF:
629 case ARRAY_RANGE_REF:
630 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
631 found = t;
632 break;
634 case REALPART_EXPR:
635 case IMAGPART_EXPR:
636 break;
638 case BIT_FIELD_REF:
639 case VIEW_CONVERT_EXPR:
640 /* Bitfields and casts are never addressable. */
641 found = t;
642 break;
644 default:
645 gcc_unreachable ();
648 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
649 found = t;
651 t = TREE_OPERAND (t, 0);
654 if (found)
655 return TREE_OPERAND (found, 0);
657 return NULL_TREE;
661 /* Return whether the pointer-type T effective for aliasing may
662 access everything and thus the reference has to be assigned
663 alias-set zero. */
665 static bool
666 ref_all_alias_ptr_type_p (const_tree t)
668 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
669 || TYPE_REF_CAN_ALIAS_ALL (t));
672 /* Return the alias set for the memory pointed to by T, which may be
673 either a type or an expression. Return -1 if there is nothing
674 special about dereferencing T. */
676 static alias_set_type
677 get_deref_alias_set_1 (tree t)
679 /* All we care about is the type. */
680 if (! TYPE_P (t))
681 t = TREE_TYPE (t);
683 /* If we have an INDIRECT_REF via a void pointer, we don't
684 know anything about what that might alias. Likewise if the
685 pointer is marked that way. */
686 if (ref_all_alias_ptr_type_p (t))
687 return 0;
689 return -1;
692 /* Return the alias set for the memory pointed to by T, which may be
693 either a type or an expression. */
695 alias_set_type
696 get_deref_alias_set (tree t)
698 /* If we're not doing any alias analysis, just assume everything
699 aliases everything else. */
700 if (!flag_strict_aliasing)
701 return 0;
703 alias_set_type set = get_deref_alias_set_1 (t);
705 /* Fall back to the alias-set of the pointed-to type. */
706 if (set == -1)
708 if (! TYPE_P (t))
709 t = TREE_TYPE (t);
710 set = get_alias_set (TREE_TYPE (t));
713 return set;
716 /* Return the pointer-type relevant for TBAA purposes from the
717 memory reference tree *T or NULL_TREE in which case *T is
718 adjusted to point to the outermost component reference that
719 can be used for assigning an alias set. */
721 static tree
722 reference_alias_ptr_type_1 (tree *t)
724 tree inner;
726 /* Get the base object of the reference. */
727 inner = *t;
728 while (handled_component_p (inner))
730 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
731 the type of any component references that wrap it to
732 determine the alias-set. */
733 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
734 *t = TREE_OPERAND (inner, 0);
735 inner = TREE_OPERAND (inner, 0);
738 /* Handle pointer dereferences here, they can override the
739 alias-set. */
740 if (INDIRECT_REF_P (inner)
741 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
742 return TREE_TYPE (TREE_OPERAND (inner, 0));
743 else if (TREE_CODE (inner) == TARGET_MEM_REF)
744 return TREE_TYPE (TMR_OFFSET (inner));
745 else if (TREE_CODE (inner) == MEM_REF
746 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
747 return TREE_TYPE (TREE_OPERAND (inner, 1));
749 /* If the innermost reference is a MEM_REF that has a
750 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
751 using the memory access type for determining the alias-set. */
752 if (TREE_CODE (inner) == MEM_REF
753 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
754 != TYPE_MAIN_VARIANT
755 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
756 return TREE_TYPE (TREE_OPERAND (inner, 1));
758 /* Otherwise, pick up the outermost object that we could have
759 a pointer to. */
760 tree tem = component_uses_parent_alias_set_from (*t);
761 if (tem)
762 *t = tem;
764 return NULL_TREE;
767 /* Return the pointer-type relevant for TBAA purposes from the
768 gimple memory reference tree T. This is the type to be used for
769 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
770 and guarantees that get_alias_set will return the same alias
771 set for T and the replacement. */
773 tree
774 reference_alias_ptr_type (tree t)
776 tree ptype = reference_alias_ptr_type_1 (&t);
777 /* If there is a given pointer type for aliasing purposes, return it. */
778 if (ptype != NULL_TREE)
779 return ptype;
781 /* Otherwise build one from the outermost component reference we
782 may use. */
783 if (TREE_CODE (t) == MEM_REF
784 || TREE_CODE (t) == TARGET_MEM_REF)
785 return TREE_TYPE (TREE_OPERAND (t, 1));
786 else
787 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
790 /* Return whether the pointer-types T1 and T2 used to determine
791 two alias sets of two references will yield the same answer
792 from get_deref_alias_set. */
794 bool
795 alias_ptr_types_compatible_p (tree t1, tree t2)
797 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
798 return true;
800 if (ref_all_alias_ptr_type_p (t1)
801 || ref_all_alias_ptr_type_p (t2))
802 return false;
804 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
805 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
808 /* Create emptry alias set entry. */
810 alias_set_entry *
811 init_alias_set_entry (alias_set_type set)
813 alias_set_entry *ase = ggc_alloc<alias_set_entry> ();
814 ase->alias_set = set;
815 ase->children = NULL;
816 ase->has_zero_child = false;
817 ase->is_pointer = false;
818 ase->has_pointer = false;
819 gcc_checking_assert (!get_alias_set_entry (set));
820 (*alias_sets)[set] = ase;
821 return ase;
824 /* Return the alias set for T, which may be either a type or an
825 expression. Call language-specific routine for help, if needed. */
827 alias_set_type
828 get_alias_set (tree t)
830 alias_set_type set;
832 /* If we're not doing any alias analysis, just assume everything
833 aliases everything else. Also return 0 if this or its type is
834 an error. */
835 if (! flag_strict_aliasing || t == error_mark_node
836 || (! TYPE_P (t)
837 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
838 return 0;
840 /* We can be passed either an expression or a type. This and the
841 language-specific routine may make mutually-recursive calls to each other
842 to figure out what to do. At each juncture, we see if this is a tree
843 that the language may need to handle specially. First handle things that
844 aren't types. */
845 if (! TYPE_P (t))
847 /* Give the language a chance to do something with this tree
848 before we look at it. */
849 STRIP_NOPS (t);
850 set = lang_hooks.get_alias_set (t);
851 if (set != -1)
852 return set;
854 /* Get the alias pointer-type to use or the outermost object
855 that we could have a pointer to. */
856 tree ptype = reference_alias_ptr_type_1 (&t);
857 if (ptype != NULL)
858 return get_deref_alias_set (ptype);
860 /* If we've already determined the alias set for a decl, just return
861 it. This is necessary for C++ anonymous unions, whose component
862 variables don't look like union members (boo!). */
863 if (TREE_CODE (t) == VAR_DECL
864 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
865 return MEM_ALIAS_SET (DECL_RTL (t));
867 /* Now all we care about is the type. */
868 t = TREE_TYPE (t);
871 /* Variant qualifiers don't affect the alias set, so get the main
872 variant. */
873 t = TYPE_MAIN_VARIANT (t);
875 /* Always use the canonical type as well. If this is a type that
876 requires structural comparisons to identify compatible types
877 use alias set zero. */
878 if (TYPE_STRUCTURAL_EQUALITY_P (t))
880 /* Allow the language to specify another alias set for this
881 type. */
882 set = lang_hooks.get_alias_set (t);
883 if (set != -1)
884 return set;
885 return 0;
888 t = TYPE_CANONICAL (t);
890 /* The canonical type should not require structural equality checks. */
891 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
893 /* If this is a type with a known alias set, return it. */
894 if (TYPE_ALIAS_SET_KNOWN_P (t))
895 return TYPE_ALIAS_SET (t);
897 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
898 if (!COMPLETE_TYPE_P (t))
900 /* For arrays with unknown size the conservative answer is the
901 alias set of the element type. */
902 if (TREE_CODE (t) == ARRAY_TYPE)
903 return get_alias_set (TREE_TYPE (t));
905 /* But return zero as a conservative answer for incomplete types. */
906 return 0;
909 /* See if the language has special handling for this type. */
910 set = lang_hooks.get_alias_set (t);
911 if (set != -1)
912 return set;
914 /* There are no objects of FUNCTION_TYPE, so there's no point in
915 using up an alias set for them. (There are, of course, pointers
916 and references to functions, but that's different.) */
917 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
918 set = 0;
920 /* Unless the language specifies otherwise, let vector types alias
921 their components. This avoids some nasty type punning issues in
922 normal usage. And indeed lets vectors be treated more like an
923 array slice. */
924 else if (TREE_CODE (t) == VECTOR_TYPE)
925 set = get_alias_set (TREE_TYPE (t));
927 /* Unless the language specifies otherwise, treat array types the
928 same as their components. This avoids the asymmetry we get
929 through recording the components. Consider accessing a
930 character(kind=1) through a reference to a character(kind=1)[1:1].
931 Or consider if we want to assign integer(kind=4)[0:D.1387] and
932 integer(kind=4)[4] the same alias set or not.
933 Just be pragmatic here and make sure the array and its element
934 type get the same alias set assigned. */
935 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
936 set = get_alias_set (TREE_TYPE (t));
938 /* From the former common C and C++ langhook implementation:
940 Unfortunately, there is no canonical form of a pointer type.
941 In particular, if we have `typedef int I', then `int *', and
942 `I *' are different types. So, we have to pick a canonical
943 representative. We do this below.
945 Technically, this approach is actually more conservative that
946 it needs to be. In particular, `const int *' and `int *'
947 should be in different alias sets, according to the C and C++
948 standard, since their types are not the same, and so,
949 technically, an `int **' and `const int **' cannot point at
950 the same thing.
952 But, the standard is wrong. In particular, this code is
953 legal C++:
955 int *ip;
956 int **ipp = &ip;
957 const int* const* cipp = ipp;
958 And, it doesn't make sense for that to be legal unless you
959 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
960 the pointed-to types. This issue has been reported to the
961 C++ committee.
963 For this reason go to canonical type of the unqalified pointer type.
964 Until GCC 6 this code set all pointers sets to have alias set of
965 ptr_type_node but that is a bad idea, because it prevents disabiguations
966 in between pointers. For Firefox this accounts about 20% of all
967 disambiguations in the program. */
968 else if (POINTER_TYPE_P (t) && t != ptr_type_node && !in_lto_p)
970 tree p;
971 auto_vec <bool, 8> reference;
973 /* Unnest all pointers and references.
974 We also want to make pointer to array equivalent to pointer to its
975 element. So skip all array types, too. */
976 for (p = t; POINTER_TYPE_P (p)
977 || (TREE_CODE (p) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (p));
978 p = TREE_TYPE (p))
980 if (TREE_CODE (p) == REFERENCE_TYPE)
981 reference.safe_push (true);
982 if (TREE_CODE (p) == POINTER_TYPE)
983 reference.safe_push (false);
985 p = TYPE_MAIN_VARIANT (p);
987 /* Make void * compatible with char * and also void **.
988 Programs are commonly violating TBAA by this.
990 We also make void * to conflict with every pointer
991 (see record_component_aliases) and thus it is safe it to use it for
992 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
993 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
994 set = get_alias_set (ptr_type_node);
995 else
997 /* Rebuild pointer type from starting from canonical types using
998 unqualified pointers and references only. This way all such
999 pointers will have the same alias set and will conflict with
1000 each other.
1002 Most of time we already have pointers or references of a given type.
1003 If not we build new one just to be sure that if someone later
1004 (probably only middle-end can, as we should assign all alias
1005 classes only after finishing translation unit) builds the pointer
1006 type, the canonical type will match. */
1007 p = TYPE_CANONICAL (p);
1008 while (!reference.is_empty ())
1010 if (reference.pop ())
1011 p = build_reference_type (p);
1012 else
1013 p = build_pointer_type (p);
1014 p = TYPE_CANONICAL (TYPE_MAIN_VARIANT (p));
1016 gcc_checking_assert (TYPE_CANONICAL (p) == p);
1018 /* Assign the alias set to both p and t.
1019 We can not call get_alias_set (p) here as that would trigger
1020 infinite recursion when p == t. In other cases it would just
1021 trigger unnecesary legwork of rebuilding the pointer again. */
1022 if (TYPE_ALIAS_SET_KNOWN_P (p))
1023 set = TYPE_ALIAS_SET (p);
1024 else
1026 set = new_alias_set ();
1027 TYPE_ALIAS_SET (p) = set;
1031 /* In LTO the rules above needs to be part of canonical type machinery.
1032 For now just punt. */
1033 else if (POINTER_TYPE_P (t)
1034 && t != TYPE_CANONICAL (ptr_type_node) && in_lto_p)
1035 set = get_alias_set (TYPE_CANONICAL (ptr_type_node));
1037 /* Otherwise make a new alias set for this type. */
1038 else
1040 /* Each canonical type gets its own alias set, so canonical types
1041 shouldn't form a tree. It doesn't really matter for types
1042 we handle specially above, so only check it where it possibly
1043 would result in a bogus alias set. */
1044 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1046 set = new_alias_set ();
1049 TYPE_ALIAS_SET (t) = set;
1051 /* If this is an aggregate type or a complex type, we must record any
1052 component aliasing information. */
1053 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1054 record_component_aliases (t);
1056 /* We treat pointer types specially in alias_set_subset_of. */
1057 if (POINTER_TYPE_P (t) && set)
1059 alias_set_entry *ase = get_alias_set_entry (set);
1060 if (!ase)
1061 ase = init_alias_set_entry (set);
1062 ase->is_pointer = true;
1063 ase->has_pointer = true;
1066 return set;
1069 /* Return a brand-new alias set. */
1071 alias_set_type
1072 new_alias_set (void)
1074 if (flag_strict_aliasing)
1076 if (alias_sets == 0)
1077 vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1078 vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1079 return alias_sets->length () - 1;
1081 else
1082 return 0;
1085 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1086 not everything that aliases SUPERSET also aliases SUBSET. For example,
1087 in C, a store to an `int' can alias a load of a structure containing an
1088 `int', and vice versa. But it can't alias a load of a 'double' member
1089 of the same structure. Here, the structure would be the SUPERSET and
1090 `int' the SUBSET. This relationship is also described in the comment at
1091 the beginning of this file.
1093 This function should be called only once per SUPERSET/SUBSET pair.
1095 It is illegal for SUPERSET to be zero; everything is implicitly a
1096 subset of alias set zero. */
1098 void
1099 record_alias_subset (alias_set_type superset, alias_set_type subset)
1101 alias_set_entry *superset_entry;
1102 alias_set_entry *subset_entry;
1104 /* It is possible in complex type situations for both sets to be the same,
1105 in which case we can ignore this operation. */
1106 if (superset == subset)
1107 return;
1109 gcc_assert (superset);
1111 superset_entry = get_alias_set_entry (superset);
1112 if (superset_entry == 0)
1114 /* Create an entry for the SUPERSET, so that we have a place to
1115 attach the SUBSET. */
1116 superset_entry = init_alias_set_entry (superset);
1119 if (subset == 0)
1120 superset_entry->has_zero_child = 1;
1121 else
1123 subset_entry = get_alias_set_entry (subset);
1124 if (!superset_entry->children)
1125 superset_entry->children
1126 = hash_map<alias_set_hash, int>::create_ggc (64);
1127 /* If there is an entry for the subset, enter all of its children
1128 (if they are not already present) as children of the SUPERSET. */
1129 if (subset_entry)
1131 if (subset_entry->has_zero_child)
1132 superset_entry->has_zero_child = true;
1133 if (subset_entry->has_pointer)
1134 superset_entry->has_pointer = true;
1136 if (subset_entry->children)
1138 hash_map<alias_set_hash, int>::iterator iter
1139 = subset_entry->children->begin ();
1140 for (; iter != subset_entry->children->end (); ++iter)
1141 superset_entry->children->put ((*iter).first, (*iter).second);
1145 /* Enter the SUBSET itself as a child of the SUPERSET. */
1146 superset_entry->children->put (subset, 0);
1150 /* Record that component types of TYPE, if any, are part of that type for
1151 aliasing purposes. For record types, we only record component types
1152 for fields that are not marked non-addressable. For array types, we
1153 only record the component type if it is not marked non-aliased. */
1155 void
1156 record_component_aliases (tree type)
1158 alias_set_type superset = get_alias_set (type);
1159 tree field;
1161 if (superset == 0)
1162 return;
1164 switch (TREE_CODE (type))
1166 case RECORD_TYPE:
1167 case UNION_TYPE:
1168 case QUAL_UNION_TYPE:
1169 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1170 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1171 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1172 break;
1174 case COMPLEX_TYPE:
1175 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1176 break;
1178 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1179 element type. */
1181 default:
1182 break;
1186 /* Allocate an alias set for use in storing and reading from the varargs
1187 spill area. */
1189 static GTY(()) alias_set_type varargs_set = -1;
1191 alias_set_type
1192 get_varargs_alias_set (void)
1194 #if 1
1195 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1196 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1197 consistently use the varargs alias set for loads from the varargs
1198 area. So don't use it anywhere. */
1199 return 0;
1200 #else
1201 if (varargs_set == -1)
1202 varargs_set = new_alias_set ();
1204 return varargs_set;
1205 #endif
1208 /* Likewise, but used for the fixed portions of the frame, e.g., register
1209 save areas. */
1211 static GTY(()) alias_set_type frame_set = -1;
1213 alias_set_type
1214 get_frame_alias_set (void)
1216 if (frame_set == -1)
1217 frame_set = new_alias_set ();
1219 return frame_set;
1222 /* Create a new, unique base with id ID. */
1224 static rtx
1225 unique_base_value (HOST_WIDE_INT id)
1227 return gen_rtx_ADDRESS (Pmode, id);
1230 /* Return true if accesses based on any other base value cannot alias
1231 those based on X. */
1233 static bool
1234 unique_base_value_p (rtx x)
1236 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1239 /* Return true if X is known to be a base value. */
1241 static bool
1242 known_base_value_p (rtx x)
1244 switch (GET_CODE (x))
1246 case LABEL_REF:
1247 case SYMBOL_REF:
1248 return true;
1250 case ADDRESS:
1251 /* Arguments may or may not be bases; we don't know for sure. */
1252 return GET_MODE (x) != VOIDmode;
1254 default:
1255 return false;
1259 /* Inside SRC, the source of a SET, find a base address. */
1261 static rtx
1262 find_base_value (rtx src)
1264 unsigned int regno;
1266 #if defined (FIND_BASE_TERM)
1267 /* Try machine-dependent ways to find the base term. */
1268 src = FIND_BASE_TERM (src);
1269 #endif
1271 switch (GET_CODE (src))
1273 case SYMBOL_REF:
1274 case LABEL_REF:
1275 return src;
1277 case REG:
1278 regno = REGNO (src);
1279 /* At the start of a function, argument registers have known base
1280 values which may be lost later. Returning an ADDRESS
1281 expression here allows optimization based on argument values
1282 even when the argument registers are used for other purposes. */
1283 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1284 return new_reg_base_value[regno];
1286 /* If a pseudo has a known base value, return it. Do not do this
1287 for non-fixed hard regs since it can result in a circular
1288 dependency chain for registers which have values at function entry.
1290 The test above is not sufficient because the scheduler may move
1291 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1292 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1293 && regno < vec_safe_length (reg_base_value))
1295 /* If we're inside init_alias_analysis, use new_reg_base_value
1296 to reduce the number of relaxation iterations. */
1297 if (new_reg_base_value && new_reg_base_value[regno]
1298 && DF_REG_DEF_COUNT (regno) == 1)
1299 return new_reg_base_value[regno];
1301 if ((*reg_base_value)[regno])
1302 return (*reg_base_value)[regno];
1305 return 0;
1307 case MEM:
1308 /* Check for an argument passed in memory. Only record in the
1309 copying-arguments block; it is too hard to track changes
1310 otherwise. */
1311 if (copying_arguments
1312 && (XEXP (src, 0) == arg_pointer_rtx
1313 || (GET_CODE (XEXP (src, 0)) == PLUS
1314 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1315 return arg_base_value;
1316 return 0;
1318 case CONST:
1319 src = XEXP (src, 0);
1320 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1321 break;
1323 /* ... fall through ... */
1325 case PLUS:
1326 case MINUS:
1328 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1330 /* If either operand is a REG that is a known pointer, then it
1331 is the base. */
1332 if (REG_P (src_0) && REG_POINTER (src_0))
1333 return find_base_value (src_0);
1334 if (REG_P (src_1) && REG_POINTER (src_1))
1335 return find_base_value (src_1);
1337 /* If either operand is a REG, then see if we already have
1338 a known value for it. */
1339 if (REG_P (src_0))
1341 temp = find_base_value (src_0);
1342 if (temp != 0)
1343 src_0 = temp;
1346 if (REG_P (src_1))
1348 temp = find_base_value (src_1);
1349 if (temp!= 0)
1350 src_1 = temp;
1353 /* If either base is named object or a special address
1354 (like an argument or stack reference), then use it for the
1355 base term. */
1356 if (src_0 != 0 && known_base_value_p (src_0))
1357 return src_0;
1359 if (src_1 != 0 && known_base_value_p (src_1))
1360 return src_1;
1362 /* Guess which operand is the base address:
1363 If either operand is a symbol, then it is the base. If
1364 either operand is a CONST_INT, then the other is the base. */
1365 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1366 return find_base_value (src_0);
1367 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1368 return find_base_value (src_1);
1370 return 0;
1373 case LO_SUM:
1374 /* The standard form is (lo_sum reg sym) so look only at the
1375 second operand. */
1376 return find_base_value (XEXP (src, 1));
1378 case AND:
1379 /* If the second operand is constant set the base
1380 address to the first operand. */
1381 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1382 return find_base_value (XEXP (src, 0));
1383 return 0;
1385 case TRUNCATE:
1386 /* As we do not know which address space the pointer is referring to, we can
1387 handle this only if the target does not support different pointer or
1388 address modes depending on the address space. */
1389 if (!target_default_pointer_address_modes_p ())
1390 break;
1391 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1392 break;
1393 /* Fall through. */
1394 case HIGH:
1395 case PRE_INC:
1396 case PRE_DEC:
1397 case POST_INC:
1398 case POST_DEC:
1399 case PRE_MODIFY:
1400 case POST_MODIFY:
1401 return find_base_value (XEXP (src, 0));
1403 case ZERO_EXTEND:
1404 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1405 /* As we do not know which address space the pointer is referring to, we can
1406 handle this only if the target does not support different pointer or
1407 address modes depending on the address space. */
1408 if (!target_default_pointer_address_modes_p ())
1409 break;
1412 rtx temp = find_base_value (XEXP (src, 0));
1414 if (temp != 0 && CONSTANT_P (temp))
1415 temp = convert_memory_address (Pmode, temp);
1417 return temp;
1420 default:
1421 break;
1424 return 0;
1427 /* Called from init_alias_analysis indirectly through note_stores,
1428 or directly if DEST is a register with a REG_NOALIAS note attached.
1429 SET is null in the latter case. */
1431 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1432 register N has been set in this function. */
1433 static sbitmap reg_seen;
1435 static void
1436 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1438 unsigned regno;
1439 rtx src;
1440 int n;
1442 if (!REG_P (dest))
1443 return;
1445 regno = REGNO (dest);
1447 gcc_checking_assert (regno < reg_base_value->length ());
1449 n = REG_NREGS (dest);
1450 if (n != 1)
1452 while (--n >= 0)
1454 bitmap_set_bit (reg_seen, regno + n);
1455 new_reg_base_value[regno + n] = 0;
1457 return;
1460 if (set)
1462 /* A CLOBBER wipes out any old value but does not prevent a previously
1463 unset register from acquiring a base address (i.e. reg_seen is not
1464 set). */
1465 if (GET_CODE (set) == CLOBBER)
1467 new_reg_base_value[regno] = 0;
1468 return;
1470 src = SET_SRC (set);
1472 else
1474 /* There's a REG_NOALIAS note against DEST. */
1475 if (bitmap_bit_p (reg_seen, regno))
1477 new_reg_base_value[regno] = 0;
1478 return;
1480 bitmap_set_bit (reg_seen, regno);
1481 new_reg_base_value[regno] = unique_base_value (unique_id++);
1482 return;
1485 /* If this is not the first set of REGNO, see whether the new value
1486 is related to the old one. There are two cases of interest:
1488 (1) The register might be assigned an entirely new value
1489 that has the same base term as the original set.
1491 (2) The set might be a simple self-modification that
1492 cannot change REGNO's base value.
1494 If neither case holds, reject the original base value as invalid.
1495 Note that the following situation is not detected:
1497 extern int x, y; int *p = &x; p += (&y-&x);
1499 ANSI C does not allow computing the difference of addresses
1500 of distinct top level objects. */
1501 if (new_reg_base_value[regno] != 0
1502 && find_base_value (src) != new_reg_base_value[regno])
1503 switch (GET_CODE (src))
1505 case LO_SUM:
1506 case MINUS:
1507 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1508 new_reg_base_value[regno] = 0;
1509 break;
1510 case PLUS:
1511 /* If the value we add in the PLUS is also a valid base value,
1512 this might be the actual base value, and the original value
1513 an index. */
1515 rtx other = NULL_RTX;
1517 if (XEXP (src, 0) == dest)
1518 other = XEXP (src, 1);
1519 else if (XEXP (src, 1) == dest)
1520 other = XEXP (src, 0);
1522 if (! other || find_base_value (other))
1523 new_reg_base_value[regno] = 0;
1524 break;
1526 case AND:
1527 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1528 new_reg_base_value[regno] = 0;
1529 break;
1530 default:
1531 new_reg_base_value[regno] = 0;
1532 break;
1534 /* If this is the first set of a register, record the value. */
1535 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1536 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1537 new_reg_base_value[regno] = find_base_value (src);
1539 bitmap_set_bit (reg_seen, regno);
1542 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1543 using hard registers with non-null REG_BASE_VALUE for renaming. */
1545 get_reg_base_value (unsigned int regno)
1547 return (*reg_base_value)[regno];
1550 /* If a value is known for REGNO, return it. */
1553 get_reg_known_value (unsigned int regno)
1555 if (regno >= FIRST_PSEUDO_REGISTER)
1557 regno -= FIRST_PSEUDO_REGISTER;
1558 if (regno < vec_safe_length (reg_known_value))
1559 return (*reg_known_value)[regno];
1561 return NULL;
1564 /* Set it. */
1566 static void
1567 set_reg_known_value (unsigned int regno, rtx val)
1569 if (regno >= FIRST_PSEUDO_REGISTER)
1571 regno -= FIRST_PSEUDO_REGISTER;
1572 if (regno < vec_safe_length (reg_known_value))
1573 (*reg_known_value)[regno] = val;
1577 /* Similarly for reg_known_equiv_p. */
1579 bool
1580 get_reg_known_equiv_p (unsigned int regno)
1582 if (regno >= FIRST_PSEUDO_REGISTER)
1584 regno -= FIRST_PSEUDO_REGISTER;
1585 if (regno < vec_safe_length (reg_known_value))
1586 return bitmap_bit_p (reg_known_equiv_p, regno);
1588 return false;
1591 static void
1592 set_reg_known_equiv_p (unsigned int regno, bool val)
1594 if (regno >= FIRST_PSEUDO_REGISTER)
1596 regno -= FIRST_PSEUDO_REGISTER;
1597 if (regno < vec_safe_length (reg_known_value))
1599 if (val)
1600 bitmap_set_bit (reg_known_equiv_p, regno);
1601 else
1602 bitmap_clear_bit (reg_known_equiv_p, regno);
1608 /* Returns a canonical version of X, from the point of view alias
1609 analysis. (For example, if X is a MEM whose address is a register,
1610 and the register has a known value (say a SYMBOL_REF), then a MEM
1611 whose address is the SYMBOL_REF is returned.) */
1614 canon_rtx (rtx x)
1616 /* Recursively look for equivalences. */
1617 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1619 rtx t = get_reg_known_value (REGNO (x));
1620 if (t == x)
1621 return x;
1622 if (t)
1623 return canon_rtx (t);
1626 if (GET_CODE (x) == PLUS)
1628 rtx x0 = canon_rtx (XEXP (x, 0));
1629 rtx x1 = canon_rtx (XEXP (x, 1));
1631 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1633 if (CONST_INT_P (x0))
1634 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1635 else if (CONST_INT_P (x1))
1636 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1637 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1641 /* This gives us much better alias analysis when called from
1642 the loop optimizer. Note we want to leave the original
1643 MEM alone, but need to return the canonicalized MEM with
1644 all the flags with their original values. */
1645 else if (MEM_P (x))
1646 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1648 return x;
1651 /* Return 1 if X and Y are identical-looking rtx's.
1652 Expect that X and Y has been already canonicalized.
1654 We use the data in reg_known_value above to see if two registers with
1655 different numbers are, in fact, equivalent. */
1657 static int
1658 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1660 int i;
1661 int j;
1662 enum rtx_code code;
1663 const char *fmt;
1665 if (x == 0 && y == 0)
1666 return 1;
1667 if (x == 0 || y == 0)
1668 return 0;
1670 if (x == y)
1671 return 1;
1673 code = GET_CODE (x);
1674 /* Rtx's of different codes cannot be equal. */
1675 if (code != GET_CODE (y))
1676 return 0;
1678 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1679 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1681 if (GET_MODE (x) != GET_MODE (y))
1682 return 0;
1684 /* Some RTL can be compared without a recursive examination. */
1685 switch (code)
1687 case REG:
1688 return REGNO (x) == REGNO (y);
1690 case LABEL_REF:
1691 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1693 case SYMBOL_REF:
1694 return XSTR (x, 0) == XSTR (y, 0);
1696 case ENTRY_VALUE:
1697 /* This is magic, don't go through canonicalization et al. */
1698 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1700 case VALUE:
1701 CASE_CONST_UNIQUE:
1702 /* Pointer equality guarantees equality for these nodes. */
1703 return 0;
1705 default:
1706 break;
1709 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1710 if (code == PLUS)
1711 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1712 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1713 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1714 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1715 /* For commutative operations, the RTX match if the operand match in any
1716 order. Also handle the simple binary and unary cases without a loop. */
1717 if (COMMUTATIVE_P (x))
1719 rtx xop0 = canon_rtx (XEXP (x, 0));
1720 rtx yop0 = canon_rtx (XEXP (y, 0));
1721 rtx yop1 = canon_rtx (XEXP (y, 1));
1723 return ((rtx_equal_for_memref_p (xop0, yop0)
1724 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1725 || (rtx_equal_for_memref_p (xop0, yop1)
1726 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1728 else if (NON_COMMUTATIVE_P (x))
1730 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1731 canon_rtx (XEXP (y, 0)))
1732 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1733 canon_rtx (XEXP (y, 1))));
1735 else if (UNARY_P (x))
1736 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1737 canon_rtx (XEXP (y, 0)));
1739 /* Compare the elements. If any pair of corresponding elements
1740 fail to match, return 0 for the whole things.
1742 Limit cases to types which actually appear in addresses. */
1744 fmt = GET_RTX_FORMAT (code);
1745 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1747 switch (fmt[i])
1749 case 'i':
1750 if (XINT (x, i) != XINT (y, i))
1751 return 0;
1752 break;
1754 case 'E':
1755 /* Two vectors must have the same length. */
1756 if (XVECLEN (x, i) != XVECLEN (y, i))
1757 return 0;
1759 /* And the corresponding elements must match. */
1760 for (j = 0; j < XVECLEN (x, i); j++)
1761 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1762 canon_rtx (XVECEXP (y, i, j))) == 0)
1763 return 0;
1764 break;
1766 case 'e':
1767 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1768 canon_rtx (XEXP (y, i))) == 0)
1769 return 0;
1770 break;
1772 /* This can happen for asm operands. */
1773 case 's':
1774 if (strcmp (XSTR (x, i), XSTR (y, i)))
1775 return 0;
1776 break;
1778 /* This can happen for an asm which clobbers memory. */
1779 case '0':
1780 break;
1782 /* It is believed that rtx's at this level will never
1783 contain anything but integers and other rtx's,
1784 except for within LABEL_REFs and SYMBOL_REFs. */
1785 default:
1786 gcc_unreachable ();
1789 return 1;
1792 static rtx
1793 find_base_term (rtx x)
1795 cselib_val *val;
1796 struct elt_loc_list *l, *f;
1797 rtx ret;
1799 #if defined (FIND_BASE_TERM)
1800 /* Try machine-dependent ways to find the base term. */
1801 x = FIND_BASE_TERM (x);
1802 #endif
1804 switch (GET_CODE (x))
1806 case REG:
1807 return REG_BASE_VALUE (x);
1809 case TRUNCATE:
1810 /* As we do not know which address space the pointer is referring to, we can
1811 handle this only if the target does not support different pointer or
1812 address modes depending on the address space. */
1813 if (!target_default_pointer_address_modes_p ())
1814 return 0;
1815 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1816 return 0;
1817 /* Fall through. */
1818 case HIGH:
1819 case PRE_INC:
1820 case PRE_DEC:
1821 case POST_INC:
1822 case POST_DEC:
1823 case PRE_MODIFY:
1824 case POST_MODIFY:
1825 return find_base_term (XEXP (x, 0));
1827 case ZERO_EXTEND:
1828 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1829 /* As we do not know which address space the pointer is referring to, we can
1830 handle this only if the target does not support different pointer or
1831 address modes depending on the address space. */
1832 if (!target_default_pointer_address_modes_p ())
1833 return 0;
1836 rtx temp = find_base_term (XEXP (x, 0));
1838 if (temp != 0 && CONSTANT_P (temp))
1839 temp = convert_memory_address (Pmode, temp);
1841 return temp;
1844 case VALUE:
1845 val = CSELIB_VAL_PTR (x);
1846 ret = NULL_RTX;
1848 if (!val)
1849 return ret;
1851 if (cselib_sp_based_value_p (val))
1852 return static_reg_base_value[STACK_POINTER_REGNUM];
1854 f = val->locs;
1855 /* Temporarily reset val->locs to avoid infinite recursion. */
1856 val->locs = NULL;
1858 for (l = f; l; l = l->next)
1859 if (GET_CODE (l->loc) == VALUE
1860 && CSELIB_VAL_PTR (l->loc)->locs
1861 && !CSELIB_VAL_PTR (l->loc)->locs->next
1862 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1863 continue;
1864 else if ((ret = find_base_term (l->loc)) != 0)
1865 break;
1867 val->locs = f;
1868 return ret;
1870 case LO_SUM:
1871 /* The standard form is (lo_sum reg sym) so look only at the
1872 second operand. */
1873 return find_base_term (XEXP (x, 1));
1875 case CONST:
1876 x = XEXP (x, 0);
1877 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1878 return 0;
1879 /* Fall through. */
1880 case PLUS:
1881 case MINUS:
1883 rtx tmp1 = XEXP (x, 0);
1884 rtx tmp2 = XEXP (x, 1);
1886 /* This is a little bit tricky since we have to determine which of
1887 the two operands represents the real base address. Otherwise this
1888 routine may return the index register instead of the base register.
1890 That may cause us to believe no aliasing was possible, when in
1891 fact aliasing is possible.
1893 We use a few simple tests to guess the base register. Additional
1894 tests can certainly be added. For example, if one of the operands
1895 is a shift or multiply, then it must be the index register and the
1896 other operand is the base register. */
1898 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1899 return find_base_term (tmp2);
1901 /* If either operand is known to be a pointer, then prefer it
1902 to determine the base term. */
1903 if (REG_P (tmp1) && REG_POINTER (tmp1))
1905 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1906 std::swap (tmp1, tmp2);
1907 /* If second argument is constant which has base term, prefer it
1908 over variable tmp1. See PR64025. */
1909 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
1910 std::swap (tmp1, tmp2);
1912 /* Go ahead and find the base term for both operands. If either base
1913 term is from a pointer or is a named object or a special address
1914 (like an argument or stack reference), then use it for the
1915 base term. */
1916 rtx base = find_base_term (tmp1);
1917 if (base != NULL_RTX
1918 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1919 || known_base_value_p (base)))
1920 return base;
1921 base = find_base_term (tmp2);
1922 if (base != NULL_RTX
1923 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1924 || known_base_value_p (base)))
1925 return base;
1927 /* We could not determine which of the two operands was the
1928 base register and which was the index. So we can determine
1929 nothing from the base alias check. */
1930 return 0;
1933 case AND:
1934 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1935 return find_base_term (XEXP (x, 0));
1936 return 0;
1938 case SYMBOL_REF:
1939 case LABEL_REF:
1940 return x;
1942 default:
1943 return 0;
1947 /* Return true if accesses to address X may alias accesses based
1948 on the stack pointer. */
1950 bool
1951 may_be_sp_based_p (rtx x)
1953 rtx base = find_base_term (x);
1954 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1957 /* Return 0 if the addresses X and Y are known to point to different
1958 objects, 1 if they might be pointers to the same object. */
1960 static int
1961 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1962 machine_mode x_mode, machine_mode y_mode)
1964 /* If the address itself has no known base see if a known equivalent
1965 value has one. If either address still has no known base, nothing
1966 is known about aliasing. */
1967 if (x_base == 0)
1969 rtx x_c;
1971 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1972 return 1;
1974 x_base = find_base_term (x_c);
1975 if (x_base == 0)
1976 return 1;
1979 if (y_base == 0)
1981 rtx y_c;
1982 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1983 return 1;
1985 y_base = find_base_term (y_c);
1986 if (y_base == 0)
1987 return 1;
1990 /* If the base addresses are equal nothing is known about aliasing. */
1991 if (rtx_equal_p (x_base, y_base))
1992 return 1;
1994 /* The base addresses are different expressions. If they are not accessed
1995 via AND, there is no conflict. We can bring knowledge of object
1996 alignment into play here. For example, on alpha, "char a, b;" can
1997 alias one another, though "char a; long b;" cannot. AND addesses may
1998 implicitly alias surrounding objects; i.e. unaligned access in DImode
1999 via AND address can alias all surrounding object types except those
2000 with aligment 8 or higher. */
2001 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2002 return 1;
2003 if (GET_CODE (x) == AND
2004 && (!CONST_INT_P (XEXP (x, 1))
2005 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2006 return 1;
2007 if (GET_CODE (y) == AND
2008 && (!CONST_INT_P (XEXP (y, 1))
2009 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2010 return 1;
2012 /* Differing symbols not accessed via AND never alias. */
2013 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2014 return 0;
2016 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2017 return 0;
2019 return 1;
2022 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2023 that of V. */
2025 static bool
2026 refs_newer_value_p (const_rtx expr, rtx v)
2028 int minuid = CSELIB_VAL_PTR (v)->uid;
2029 subrtx_iterator::array_type array;
2030 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2031 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
2032 return true;
2033 return false;
2036 /* Convert the address X into something we can use. This is done by returning
2037 it unchanged unless it is a value; in the latter case we call cselib to get
2038 a more useful rtx. */
2041 get_addr (rtx x)
2043 cselib_val *v;
2044 struct elt_loc_list *l;
2046 if (GET_CODE (x) != VALUE)
2047 return x;
2048 v = CSELIB_VAL_PTR (x);
2049 if (v)
2051 bool have_equivs = cselib_have_permanent_equivalences ();
2052 if (have_equivs)
2053 v = canonical_cselib_val (v);
2054 for (l = v->locs; l; l = l->next)
2055 if (CONSTANT_P (l->loc))
2056 return l->loc;
2057 for (l = v->locs; l; l = l->next)
2058 if (!REG_P (l->loc) && !MEM_P (l->loc)
2059 /* Avoid infinite recursion when potentially dealing with
2060 var-tracking artificial equivalences, by skipping the
2061 equivalences themselves, and not choosing expressions
2062 that refer to newer VALUEs. */
2063 && (!have_equivs
2064 || (GET_CODE (l->loc) != VALUE
2065 && !refs_newer_value_p (l->loc, x))))
2066 return l->loc;
2067 if (have_equivs)
2069 for (l = v->locs; l; l = l->next)
2070 if (REG_P (l->loc)
2071 || (GET_CODE (l->loc) != VALUE
2072 && !refs_newer_value_p (l->loc, x)))
2073 return l->loc;
2074 /* Return the canonical value. */
2075 return v->val_rtx;
2077 if (v->locs)
2078 return v->locs->loc;
2080 return x;
2083 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2084 where SIZE is the size in bytes of the memory reference. If ADDR
2085 is not modified by the memory reference then ADDR is returned. */
2087 static rtx
2088 addr_side_effect_eval (rtx addr, int size, int n_refs)
2090 int offset = 0;
2092 switch (GET_CODE (addr))
2094 case PRE_INC:
2095 offset = (n_refs + 1) * size;
2096 break;
2097 case PRE_DEC:
2098 offset = -(n_refs + 1) * size;
2099 break;
2100 case POST_INC:
2101 offset = n_refs * size;
2102 break;
2103 case POST_DEC:
2104 offset = -n_refs * size;
2105 break;
2107 default:
2108 return addr;
2111 if (offset)
2112 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
2113 gen_int_mode (offset, GET_MODE (addr)));
2114 else
2115 addr = XEXP (addr, 0);
2116 addr = canon_rtx (addr);
2118 return addr;
2121 /* Return TRUE if an object X sized at XSIZE bytes and another object
2122 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2123 any of the sizes is zero, assume an overlap, otherwise use the
2124 absolute value of the sizes as the actual sizes. */
2126 static inline bool
2127 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
2129 return (xsize == 0 || ysize == 0
2130 || (c >= 0
2131 ? (abs (xsize) > c)
2132 : (abs (ysize) > -c)));
2135 /* Return one if X and Y (memory addresses) reference the
2136 same location in memory or if the references overlap.
2137 Return zero if they do not overlap, else return
2138 minus one in which case they still might reference the same location.
2140 C is an offset accumulator. When
2141 C is nonzero, we are testing aliases between X and Y + C.
2142 XSIZE is the size in bytes of the X reference,
2143 similarly YSIZE is the size in bytes for Y.
2144 Expect that canon_rtx has been already called for X and Y.
2146 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2147 referenced (the reference was BLKmode), so make the most pessimistic
2148 assumptions.
2150 If XSIZE or YSIZE is negative, we may access memory outside the object
2151 being referenced as a side effect. This can happen when using AND to
2152 align memory references, as is done on the Alpha.
2154 Nice to notice that varying addresses cannot conflict with fp if no
2155 local variables had their addresses taken, but that's too hard now.
2157 ??? Contrary to the tree alias oracle this does not return
2158 one for X + non-constant and Y + non-constant when X and Y are equal.
2159 If that is fixed the TBAA hack for union type-punning can be removed. */
2161 static int
2162 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2164 if (GET_CODE (x) == VALUE)
2166 if (REG_P (y))
2168 struct elt_loc_list *l = NULL;
2169 if (CSELIB_VAL_PTR (x))
2170 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2171 l; l = l->next)
2172 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2173 break;
2174 if (l)
2175 x = y;
2176 else
2177 x = get_addr (x);
2179 /* Don't call get_addr if y is the same VALUE. */
2180 else if (x != y)
2181 x = get_addr (x);
2183 if (GET_CODE (y) == VALUE)
2185 if (REG_P (x))
2187 struct elt_loc_list *l = NULL;
2188 if (CSELIB_VAL_PTR (y))
2189 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2190 l; l = l->next)
2191 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2192 break;
2193 if (l)
2194 y = x;
2195 else
2196 y = get_addr (y);
2198 /* Don't call get_addr if x is the same VALUE. */
2199 else if (y != x)
2200 y = get_addr (y);
2202 if (GET_CODE (x) == HIGH)
2203 x = XEXP (x, 0);
2204 else if (GET_CODE (x) == LO_SUM)
2205 x = XEXP (x, 1);
2206 else
2207 x = addr_side_effect_eval (x, abs (xsize), 0);
2208 if (GET_CODE (y) == HIGH)
2209 y = XEXP (y, 0);
2210 else if (GET_CODE (y) == LO_SUM)
2211 y = XEXP (y, 1);
2212 else
2213 y = addr_side_effect_eval (y, abs (ysize), 0);
2215 if (rtx_equal_for_memref_p (x, y))
2217 return offset_overlap_p (c, xsize, ysize);
2220 /* This code used to check for conflicts involving stack references and
2221 globals but the base address alias code now handles these cases. */
2223 if (GET_CODE (x) == PLUS)
2225 /* The fact that X is canonicalized means that this
2226 PLUS rtx is canonicalized. */
2227 rtx x0 = XEXP (x, 0);
2228 rtx x1 = XEXP (x, 1);
2230 /* However, VALUEs might end up in different positions even in
2231 canonical PLUSes. Comparing their addresses is enough. */
2232 if (x0 == y)
2233 return memrefs_conflict_p (xsize, x1, ysize, const0_rtx, c);
2234 else if (x1 == y)
2235 return memrefs_conflict_p (xsize, x0, ysize, const0_rtx, c);
2237 if (GET_CODE (y) == PLUS)
2239 /* The fact that Y is canonicalized means that this
2240 PLUS rtx is canonicalized. */
2241 rtx y0 = XEXP (y, 0);
2242 rtx y1 = XEXP (y, 1);
2244 if (x0 == y1)
2245 return memrefs_conflict_p (xsize, x1, ysize, y0, c);
2246 if (x1 == y0)
2247 return memrefs_conflict_p (xsize, x0, ysize, y1, c);
2249 if (rtx_equal_for_memref_p (x1, y1))
2250 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2251 if (rtx_equal_for_memref_p (x0, y0))
2252 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2253 if (CONST_INT_P (x1))
2255 if (CONST_INT_P (y1))
2256 return memrefs_conflict_p (xsize, x0, ysize, y0,
2257 c - INTVAL (x1) + INTVAL (y1));
2258 else
2259 return memrefs_conflict_p (xsize, x0, ysize, y,
2260 c - INTVAL (x1));
2262 else if (CONST_INT_P (y1))
2263 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2265 return -1;
2267 else if (CONST_INT_P (x1))
2268 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2270 else if (GET_CODE (y) == PLUS)
2272 /* The fact that Y is canonicalized means that this
2273 PLUS rtx is canonicalized. */
2274 rtx y0 = XEXP (y, 0);
2275 rtx y1 = XEXP (y, 1);
2277 if (x == y0)
2278 return memrefs_conflict_p (xsize, const0_rtx, ysize, y1, c);
2279 if (x == y1)
2280 return memrefs_conflict_p (xsize, const0_rtx, ysize, y0, c);
2282 if (CONST_INT_P (y1))
2283 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2284 else
2285 return -1;
2288 if (GET_CODE (x) == GET_CODE (y))
2289 switch (GET_CODE (x))
2291 case MULT:
2293 /* Handle cases where we expect the second operands to be the
2294 same, and check only whether the first operand would conflict
2295 or not. */
2296 rtx x0, y0;
2297 rtx x1 = canon_rtx (XEXP (x, 1));
2298 rtx y1 = canon_rtx (XEXP (y, 1));
2299 if (! rtx_equal_for_memref_p (x1, y1))
2300 return -1;
2301 x0 = canon_rtx (XEXP (x, 0));
2302 y0 = canon_rtx (XEXP (y, 0));
2303 if (rtx_equal_for_memref_p (x0, y0))
2304 return offset_overlap_p (c, xsize, ysize);
2306 /* Can't properly adjust our sizes. */
2307 if (!CONST_INT_P (x1))
2308 return -1;
2309 xsize /= INTVAL (x1);
2310 ysize /= INTVAL (x1);
2311 c /= INTVAL (x1);
2312 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2315 default:
2316 break;
2319 /* Deal with alignment ANDs by adjusting offset and size so as to
2320 cover the maximum range, without taking any previously known
2321 alignment into account. Make a size negative after such an
2322 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2323 assume a potential overlap, because they may end up in contiguous
2324 memory locations and the stricter-alignment access may span over
2325 part of both. */
2326 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2328 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2329 unsigned HOST_WIDE_INT uc = sc;
2330 if (sc < 0 && -uc == (uc & -uc))
2332 if (xsize > 0)
2333 xsize = -xsize;
2334 if (xsize)
2335 xsize += sc + 1;
2336 c -= sc + 1;
2337 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2338 ysize, y, c);
2341 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2343 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2344 unsigned HOST_WIDE_INT uc = sc;
2345 if (sc < 0 && -uc == (uc & -uc))
2347 if (ysize > 0)
2348 ysize = -ysize;
2349 if (ysize)
2350 ysize += sc + 1;
2351 c += sc + 1;
2352 return memrefs_conflict_p (xsize, x,
2353 ysize, canon_rtx (XEXP (y, 0)), c);
2357 if (CONSTANT_P (x))
2359 if (CONST_INT_P (x) && CONST_INT_P (y))
2361 c += (INTVAL (y) - INTVAL (x));
2362 return offset_overlap_p (c, xsize, ysize);
2365 if (GET_CODE (x) == CONST)
2367 if (GET_CODE (y) == CONST)
2368 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2369 ysize, canon_rtx (XEXP (y, 0)), c);
2370 else
2371 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2372 ysize, y, c);
2374 if (GET_CODE (y) == CONST)
2375 return memrefs_conflict_p (xsize, x, ysize,
2376 canon_rtx (XEXP (y, 0)), c);
2378 /* Assume a potential overlap for symbolic addresses that went
2379 through alignment adjustments (i.e., that have negative
2380 sizes), because we can't know how far they are from each
2381 other. */
2382 if (CONSTANT_P (y))
2383 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2385 return -1;
2388 return -1;
2391 /* Functions to compute memory dependencies.
2393 Since we process the insns in execution order, we can build tables
2394 to keep track of what registers are fixed (and not aliased), what registers
2395 are varying in known ways, and what registers are varying in unknown
2396 ways.
2398 If both memory references are volatile, then there must always be a
2399 dependence between the two references, since their order can not be
2400 changed. A volatile and non-volatile reference can be interchanged
2401 though.
2403 We also must allow AND addresses, because they may generate accesses
2404 outside the object being referenced. This is used to generate aligned
2405 addresses from unaligned addresses, for instance, the alpha
2406 storeqi_unaligned pattern. */
2408 /* Read dependence: X is read after read in MEM takes place. There can
2409 only be a dependence here if both reads are volatile, or if either is
2410 an explicit barrier. */
2413 read_dependence (const_rtx mem, const_rtx x)
2415 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2416 return true;
2417 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2418 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2419 return true;
2420 return false;
2423 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2425 static tree
2426 decl_for_component_ref (tree x)
2430 x = TREE_OPERAND (x, 0);
2432 while (x && TREE_CODE (x) == COMPONENT_REF);
2434 return x && DECL_P (x) ? x : NULL_TREE;
2437 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2438 for the offset of the field reference. *KNOWN_P says whether the
2439 offset is known. */
2441 static void
2442 adjust_offset_for_component_ref (tree x, bool *known_p,
2443 HOST_WIDE_INT *offset)
2445 if (!*known_p)
2446 return;
2449 tree xoffset = component_ref_field_offset (x);
2450 tree field = TREE_OPERAND (x, 1);
2451 if (TREE_CODE (xoffset) != INTEGER_CST)
2453 *known_p = false;
2454 return;
2457 offset_int woffset
2458 = (wi::to_offset (xoffset)
2459 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2460 LOG2_BITS_PER_UNIT));
2461 if (!wi::fits_uhwi_p (woffset))
2463 *known_p = false;
2464 return;
2466 *offset += woffset.to_uhwi ();
2468 x = TREE_OPERAND (x, 0);
2470 while (x && TREE_CODE (x) == COMPONENT_REF);
2473 /* Return nonzero if we can determine the exprs corresponding to memrefs
2474 X and Y and they do not overlap.
2475 If LOOP_VARIANT is set, skip offset-based disambiguation */
2478 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2480 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2481 rtx rtlx, rtly;
2482 rtx basex, basey;
2483 bool moffsetx_known_p, moffsety_known_p;
2484 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2485 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey;
2487 /* Unless both have exprs, we can't tell anything. */
2488 if (exprx == 0 || expry == 0)
2489 return 0;
2491 /* For spill-slot accesses make sure we have valid offsets. */
2492 if ((exprx == get_spill_slot_decl (false)
2493 && ! MEM_OFFSET_KNOWN_P (x))
2494 || (expry == get_spill_slot_decl (false)
2495 && ! MEM_OFFSET_KNOWN_P (y)))
2496 return 0;
2498 /* If the field reference test failed, look at the DECLs involved. */
2499 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2500 if (moffsetx_known_p)
2501 moffsetx = MEM_OFFSET (x);
2502 if (TREE_CODE (exprx) == COMPONENT_REF)
2504 tree t = decl_for_component_ref (exprx);
2505 if (! t)
2506 return 0;
2507 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2508 exprx = t;
2511 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2512 if (moffsety_known_p)
2513 moffsety = MEM_OFFSET (y);
2514 if (TREE_CODE (expry) == COMPONENT_REF)
2516 tree t = decl_for_component_ref (expry);
2517 if (! t)
2518 return 0;
2519 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2520 expry = t;
2523 if (! DECL_P (exprx) || ! DECL_P (expry))
2524 return 0;
2526 /* If we refer to different gimple registers, or one gimple register
2527 and one non-gimple-register, we know they can't overlap. First,
2528 gimple registers don't have their addresses taken. Now, there
2529 could be more than one stack slot for (different versions of) the
2530 same gimple register, but we can presumably tell they don't
2531 overlap based on offsets from stack base addresses elsewhere.
2532 It's important that we don't proceed to DECL_RTL, because gimple
2533 registers may not pass DECL_RTL_SET_P, and make_decl_rtl won't be
2534 able to do anything about them since no SSA information will have
2535 remained to guide it. */
2536 if (is_gimple_reg (exprx) || is_gimple_reg (expry))
2537 return exprx != expry
2538 || (moffsetx_known_p && moffsety_known_p
2539 && MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y)
2540 && !offset_overlap_p (moffsety - moffsetx,
2541 MEM_SIZE (x), MEM_SIZE (y)));
2543 /* With invalid code we can end up storing into the constant pool.
2544 Bail out to avoid ICEing when creating RTL for this.
2545 See gfortran.dg/lto/20091028-2_0.f90. */
2546 if (TREE_CODE (exprx) == CONST_DECL
2547 || TREE_CODE (expry) == CONST_DECL)
2548 return 1;
2550 rtlx = DECL_RTL (exprx);
2551 rtly = DECL_RTL (expry);
2553 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2554 can't overlap unless they are the same because we never reuse that part
2555 of the stack frame used for locals for spilled pseudos. */
2556 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2557 && ! rtx_equal_p (rtlx, rtly))
2558 return 1;
2560 /* If we have MEMs referring to different address spaces (which can
2561 potentially overlap), we cannot easily tell from the addresses
2562 whether the references overlap. */
2563 if (MEM_P (rtlx) && MEM_P (rtly)
2564 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2565 return 0;
2567 /* Get the base and offsets of both decls. If either is a register, we
2568 know both are and are the same, so use that as the base. The only
2569 we can avoid overlap is if we can deduce that they are nonoverlapping
2570 pieces of that decl, which is very rare. */
2571 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2572 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2573 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2575 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2576 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2577 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2579 /* If the bases are different, we know they do not overlap if both
2580 are constants or if one is a constant and the other a pointer into the
2581 stack frame. Otherwise a different base means we can't tell if they
2582 overlap or not. */
2583 if (! rtx_equal_p (basex, basey))
2584 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2585 || (CONSTANT_P (basex) && REG_P (basey)
2586 && REGNO_PTR_FRAME_P (REGNO (basey)))
2587 || (CONSTANT_P (basey) && REG_P (basex)
2588 && REGNO_PTR_FRAME_P (REGNO (basex))));
2590 /* Offset based disambiguation not appropriate for loop invariant */
2591 if (loop_invariant)
2592 return 0;
2594 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2595 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2596 : -1);
2597 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2598 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2599 : -1);
2601 /* If we have an offset for either memref, it can update the values computed
2602 above. */
2603 if (moffsetx_known_p)
2604 offsetx += moffsetx, sizex -= moffsetx;
2605 if (moffsety_known_p)
2606 offsety += moffsety, sizey -= moffsety;
2608 /* If a memref has both a size and an offset, we can use the smaller size.
2609 We can't do this if the offset isn't known because we must view this
2610 memref as being anywhere inside the DECL's MEM. */
2611 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2612 sizex = MEM_SIZE (x);
2613 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2614 sizey = MEM_SIZE (y);
2616 /* Put the values of the memref with the lower offset in X's values. */
2617 if (offsetx > offsety)
2619 std::swap (offsetx, offsety);
2620 std::swap (sizex, sizey);
2623 /* If we don't know the size of the lower-offset value, we can't tell
2624 if they conflict. Otherwise, we do the test. */
2625 return sizex >= 0 && offsety >= offsetx + sizex;
2628 /* Helper for true_dependence and canon_true_dependence.
2629 Checks for true dependence: X is read after store in MEM takes place.
2631 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2632 NULL_RTX, and the canonical addresses of MEM and X are both computed
2633 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2635 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2637 Returns 1 if there is a true dependence, 0 otherwise. */
2639 static int
2640 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2641 const_rtx x, rtx x_addr, bool mem_canonicalized)
2643 rtx true_mem_addr;
2644 rtx base;
2645 int ret;
2647 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2648 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2650 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2651 return 1;
2653 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2654 This is used in epilogue deallocation functions, and in cselib. */
2655 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2656 return 1;
2657 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2658 return 1;
2659 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2660 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2661 return 1;
2663 if (! x_addr)
2664 x_addr = XEXP (x, 0);
2665 x_addr = get_addr (x_addr);
2667 if (! mem_addr)
2669 mem_addr = XEXP (mem, 0);
2670 if (mem_mode == VOIDmode)
2671 mem_mode = GET_MODE (mem);
2673 true_mem_addr = get_addr (mem_addr);
2675 /* Read-only memory is by definition never modified, and therefore can't
2676 conflict with anything. However, don't assume anything when AND
2677 addresses are involved and leave to the code below to determine
2678 dependence. We don't expect to find read-only set on MEM, but
2679 stupid user tricks can produce them, so don't die. */
2680 if (MEM_READONLY_P (x)
2681 && GET_CODE (x_addr) != AND
2682 && GET_CODE (true_mem_addr) != AND)
2683 return 0;
2685 /* If we have MEMs referring to different address spaces (which can
2686 potentially overlap), we cannot easily tell from the addresses
2687 whether the references overlap. */
2688 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2689 return 1;
2691 base = find_base_term (x_addr);
2692 if (base && (GET_CODE (base) == LABEL_REF
2693 || (GET_CODE (base) == SYMBOL_REF
2694 && CONSTANT_POOL_ADDRESS_P (base))))
2695 return 0;
2697 rtx mem_base = find_base_term (true_mem_addr);
2698 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2699 GET_MODE (x), mem_mode))
2700 return 0;
2702 x_addr = canon_rtx (x_addr);
2703 if (!mem_canonicalized)
2704 mem_addr = canon_rtx (true_mem_addr);
2706 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2707 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2708 return ret;
2710 if (mems_in_disjoint_alias_sets_p (x, mem))
2711 return 0;
2713 if (nonoverlapping_memrefs_p (mem, x, false))
2714 return 0;
2716 return rtx_refs_may_alias_p (x, mem, true);
2719 /* True dependence: X is read after store in MEM takes place. */
2722 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2724 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2725 x, NULL_RTX, /*mem_canonicalized=*/false);
2728 /* Canonical true dependence: X is read after store in MEM takes place.
2729 Variant of true_dependence which assumes MEM has already been
2730 canonicalized (hence we no longer do that here).
2731 The mem_addr argument has been added, since true_dependence_1 computed
2732 this value prior to canonicalizing. */
2735 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2736 const_rtx x, rtx x_addr)
2738 return true_dependence_1 (mem, mem_mode, mem_addr,
2739 x, x_addr, /*mem_canonicalized=*/true);
2742 /* Returns nonzero if a write to X might alias a previous read from
2743 (or, if WRITEP is true, a write to) MEM.
2744 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2745 and X_MODE the mode for that access.
2746 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2748 static int
2749 write_dependence_p (const_rtx mem,
2750 const_rtx x, machine_mode x_mode, rtx x_addr,
2751 bool mem_canonicalized, bool x_canonicalized, bool writep)
2753 rtx mem_addr;
2754 rtx true_mem_addr, true_x_addr;
2755 rtx base;
2756 int ret;
2758 gcc_checking_assert (x_canonicalized
2759 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2760 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2762 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2763 return 1;
2765 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2766 This is used in epilogue deallocation functions. */
2767 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2768 return 1;
2769 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2770 return 1;
2771 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2772 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2773 return 1;
2775 if (!x_addr)
2776 x_addr = XEXP (x, 0);
2777 true_x_addr = get_addr (x_addr);
2779 mem_addr = XEXP (mem, 0);
2780 true_mem_addr = get_addr (mem_addr);
2782 /* A read from read-only memory can't conflict with read-write memory.
2783 Don't assume anything when AND addresses are involved and leave to
2784 the code below to determine dependence. */
2785 if (!writep
2786 && MEM_READONLY_P (mem)
2787 && GET_CODE (true_x_addr) != AND
2788 && GET_CODE (true_mem_addr) != AND)
2789 return 0;
2791 /* If we have MEMs referring to different address spaces (which can
2792 potentially overlap), we cannot easily tell from the addresses
2793 whether the references overlap. */
2794 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2795 return 1;
2797 base = find_base_term (true_mem_addr);
2798 if (! writep
2799 && base
2800 && (GET_CODE (base) == LABEL_REF
2801 || (GET_CODE (base) == SYMBOL_REF
2802 && CONSTANT_POOL_ADDRESS_P (base))))
2803 return 0;
2805 rtx x_base = find_base_term (true_x_addr);
2806 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2807 GET_MODE (x), GET_MODE (mem)))
2808 return 0;
2810 if (!x_canonicalized)
2812 x_addr = canon_rtx (true_x_addr);
2813 x_mode = GET_MODE (x);
2815 if (!mem_canonicalized)
2816 mem_addr = canon_rtx (true_mem_addr);
2818 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2819 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2820 return ret;
2822 if (nonoverlapping_memrefs_p (x, mem, false))
2823 return 0;
2825 return rtx_refs_may_alias_p (x, mem, false);
2828 /* Anti dependence: X is written after read in MEM takes place. */
2831 anti_dependence (const_rtx mem, const_rtx x)
2833 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2834 /*mem_canonicalized=*/false,
2835 /*x_canonicalized*/false, /*writep=*/false);
2838 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2839 Also, consider X in X_MODE (which might be from an enclosing
2840 STRICT_LOW_PART / ZERO_EXTRACT).
2841 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2844 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2845 const_rtx x, machine_mode x_mode, rtx x_addr)
2847 return write_dependence_p (mem, x, x_mode, x_addr,
2848 mem_canonicalized, /*x_canonicalized=*/true,
2849 /*writep=*/false);
2852 /* Output dependence: X is written after store in MEM takes place. */
2855 output_dependence (const_rtx mem, const_rtx x)
2857 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2858 /*mem_canonicalized=*/false,
2859 /*x_canonicalized*/false, /*writep=*/true);
2864 /* Check whether X may be aliased with MEM. Don't do offset-based
2865 memory disambiguation & TBAA. */
2867 may_alias_p (const_rtx mem, const_rtx x)
2869 rtx x_addr, mem_addr;
2871 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2872 return 1;
2874 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2875 This is used in epilogue deallocation functions. */
2876 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2877 return 1;
2878 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2879 return 1;
2880 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2881 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2882 return 1;
2884 x_addr = XEXP (x, 0);
2885 x_addr = get_addr (x_addr);
2887 mem_addr = XEXP (mem, 0);
2888 mem_addr = get_addr (mem_addr);
2890 /* Read-only memory is by definition never modified, and therefore can't
2891 conflict with anything. However, don't assume anything when AND
2892 addresses are involved and leave to the code below to determine
2893 dependence. We don't expect to find read-only set on MEM, but
2894 stupid user tricks can produce them, so don't die. */
2895 if (MEM_READONLY_P (x)
2896 && GET_CODE (x_addr) != AND
2897 && GET_CODE (mem_addr) != AND)
2898 return 0;
2900 /* If we have MEMs referring to different address spaces (which can
2901 potentially overlap), we cannot easily tell from the addresses
2902 whether the references overlap. */
2903 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2904 return 1;
2906 rtx x_base = find_base_term (x_addr);
2907 rtx mem_base = find_base_term (mem_addr);
2908 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2909 GET_MODE (x), GET_MODE (mem_addr)))
2910 return 0;
2912 if (nonoverlapping_memrefs_p (mem, x, true))
2913 return 0;
2915 /* TBAA not valid for loop_invarint */
2916 return rtx_refs_may_alias_p (x, mem, false);
2919 void
2920 init_alias_target (void)
2922 int i;
2924 if (!arg_base_value)
2925 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2927 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2929 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2930 /* Check whether this register can hold an incoming pointer
2931 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2932 numbers, so translate if necessary due to register windows. */
2933 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2934 && HARD_REGNO_MODE_OK (i, Pmode))
2935 static_reg_base_value[i] = arg_base_value;
2937 static_reg_base_value[STACK_POINTER_REGNUM]
2938 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2939 static_reg_base_value[ARG_POINTER_REGNUM]
2940 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2941 static_reg_base_value[FRAME_POINTER_REGNUM]
2942 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2943 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2944 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2945 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2948 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2949 to be memory reference. */
2950 static bool memory_modified;
2951 static void
2952 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2954 if (MEM_P (x))
2956 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2957 memory_modified = true;
2962 /* Return true when INSN possibly modify memory contents of MEM
2963 (i.e. address can be modified). */
2964 bool
2965 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2967 if (!INSN_P (insn))
2968 return false;
2969 memory_modified = false;
2970 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2971 return memory_modified;
2974 /* Return TRUE if the destination of a set is rtx identical to
2975 ITEM. */
2976 static inline bool
2977 set_dest_equal_p (const_rtx set, const_rtx item)
2979 rtx dest = SET_DEST (set);
2980 return rtx_equal_p (dest, item);
2983 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2984 *DEFINITELY* modify the memory contents of MEM. */
2985 bool
2986 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2988 if (!INSN_P (insn))
2989 return false;
2990 insn = PATTERN (insn);
2991 if (GET_CODE (insn) == SET)
2992 return set_dest_equal_p (insn, mem);
2993 else if (GET_CODE (insn) == PARALLEL)
2995 int i;
2996 for (i = 0; i < XVECLEN (insn, 0); i++)
2998 rtx sub = XVECEXP (insn, 0, i);
2999 if (GET_CODE (sub) == SET
3000 && set_dest_equal_p (sub, mem))
3001 return true;
3004 return false;
3007 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3008 array. */
3010 void
3011 init_alias_analysis (void)
3013 unsigned int maxreg = max_reg_num ();
3014 int changed, pass;
3015 int i;
3016 unsigned int ui;
3017 rtx_insn *insn;
3018 rtx val;
3019 int rpo_cnt;
3020 int *rpo;
3022 timevar_push (TV_ALIAS_ANALYSIS);
3024 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
3025 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3026 bitmap_clear (reg_known_equiv_p);
3028 /* If we have memory allocated from the previous run, use it. */
3029 if (old_reg_base_value)
3030 reg_base_value = old_reg_base_value;
3032 if (reg_base_value)
3033 reg_base_value->truncate (0);
3035 vec_safe_grow_cleared (reg_base_value, maxreg);
3037 new_reg_base_value = XNEWVEC (rtx, maxreg);
3038 reg_seen = sbitmap_alloc (maxreg);
3040 /* The basic idea is that each pass through this loop will use the
3041 "constant" information from the previous pass to propagate alias
3042 information through another level of assignments.
3044 The propagation is done on the CFG in reverse post-order, to propagate
3045 things forward as far as possible in each iteration.
3047 This could get expensive if the assignment chains are long. Maybe
3048 we should throttle the number of iterations, possibly based on
3049 the optimization level or flag_expensive_optimizations.
3051 We could propagate more information in the first pass by making use
3052 of DF_REG_DEF_COUNT to determine immediately that the alias information
3053 for a pseudo is "constant".
3055 A program with an uninitialized variable can cause an infinite loop
3056 here. Instead of doing a full dataflow analysis to detect such problems
3057 we just cap the number of iterations for the loop.
3059 The state of the arrays for the set chain in question does not matter
3060 since the program has undefined behavior. */
3062 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3063 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3065 /* The prologue/epilogue insns are not threaded onto the
3066 insn chain until after reload has completed. Thus,
3067 there is no sense wasting time checking if INSN is in
3068 the prologue/epilogue until after reload has completed. */
3069 bool could_be_prologue_epilogue = ((targetm.have_prologue ()
3070 || targetm.have_epilogue ())
3071 && reload_completed);
3073 pass = 0;
3076 /* Assume nothing will change this iteration of the loop. */
3077 changed = 0;
3079 /* We want to assign the same IDs each iteration of this loop, so
3080 start counting from one each iteration of the loop. */
3081 unique_id = 1;
3083 /* We're at the start of the function each iteration through the
3084 loop, so we're copying arguments. */
3085 copying_arguments = true;
3087 /* Wipe the potential alias information clean for this pass. */
3088 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3090 /* Wipe the reg_seen array clean. */
3091 bitmap_clear (reg_seen);
3093 /* Initialize the alias information for this pass. */
3094 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3095 if (static_reg_base_value[i])
3097 new_reg_base_value[i] = static_reg_base_value[i];
3098 bitmap_set_bit (reg_seen, i);
3101 /* Walk the insns adding values to the new_reg_base_value array. */
3102 for (i = 0; i < rpo_cnt; i++)
3104 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3105 FOR_BB_INSNS (bb, insn)
3107 if (NONDEBUG_INSN_P (insn))
3109 rtx note, set;
3111 if (could_be_prologue_epilogue
3112 && prologue_epilogue_contains (insn))
3113 continue;
3115 /* If this insn has a noalias note, process it, Otherwise,
3116 scan for sets. A simple set will have no side effects
3117 which could change the base value of any other register. */
3119 if (GET_CODE (PATTERN (insn)) == SET
3120 && REG_NOTES (insn) != 0
3121 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3122 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3123 else
3124 note_stores (PATTERN (insn), record_set, NULL);
3126 set = single_set (insn);
3128 if (set != 0
3129 && REG_P (SET_DEST (set))
3130 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3132 unsigned int regno = REGNO (SET_DEST (set));
3133 rtx src = SET_SRC (set);
3134 rtx t;
3136 note = find_reg_equal_equiv_note (insn);
3137 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3138 && DF_REG_DEF_COUNT (regno) != 1)
3139 note = NULL_RTX;
3141 if (note != NULL_RTX
3142 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3143 && ! rtx_varies_p (XEXP (note, 0), 1)
3144 && ! reg_overlap_mentioned_p (SET_DEST (set),
3145 XEXP (note, 0)))
3147 set_reg_known_value (regno, XEXP (note, 0));
3148 set_reg_known_equiv_p (regno,
3149 REG_NOTE_KIND (note) == REG_EQUIV);
3151 else if (DF_REG_DEF_COUNT (regno) == 1
3152 && GET_CODE (src) == PLUS
3153 && REG_P (XEXP (src, 0))
3154 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3155 && CONST_INT_P (XEXP (src, 1)))
3157 t = plus_constant (GET_MODE (src), t,
3158 INTVAL (XEXP (src, 1)));
3159 set_reg_known_value (regno, t);
3160 set_reg_known_equiv_p (regno, false);
3162 else if (DF_REG_DEF_COUNT (regno) == 1
3163 && ! rtx_varies_p (src, 1))
3165 set_reg_known_value (regno, src);
3166 set_reg_known_equiv_p (regno, false);
3170 else if (NOTE_P (insn)
3171 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3172 copying_arguments = false;
3176 /* Now propagate values from new_reg_base_value to reg_base_value. */
3177 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3179 for (ui = 0; ui < maxreg; ui++)
3181 if (new_reg_base_value[ui]
3182 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3183 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3185 (*reg_base_value)[ui] = new_reg_base_value[ui];
3186 changed = 1;
3190 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3191 XDELETEVEC (rpo);
3193 /* Fill in the remaining entries. */
3194 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3196 int regno = i + FIRST_PSEUDO_REGISTER;
3197 if (! val)
3198 set_reg_known_value (regno, regno_reg_rtx[regno]);
3201 /* Clean up. */
3202 free (new_reg_base_value);
3203 new_reg_base_value = 0;
3204 sbitmap_free (reg_seen);
3205 reg_seen = 0;
3206 timevar_pop (TV_ALIAS_ANALYSIS);
3209 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3210 Special API for var-tracking pass purposes. */
3212 void
3213 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3215 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3218 void
3219 end_alias_analysis (void)
3221 old_reg_base_value = reg_base_value;
3222 vec_free (reg_known_value);
3223 sbitmap_free (reg_known_equiv_p);
3226 void
3227 dump_alias_stats_in_alias_c (FILE *s)
3229 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3230 " %llu are in alias set 0\n"
3231 " %llu queries asked about the same object\n"
3232 " %llu queries asked about the same alias set\n"
3233 " %llu access volatile\n"
3234 " %llu are dependent in the DAG\n"
3235 " %llu are aritificially in conflict with void *\n",
3236 alias_stats.num_disambiguated,
3237 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3238 + alias_stats.num_same_objects + alias_stats.num_volatile
3239 + alias_stats.num_dag + alias_stats.num_disambiguated
3240 + alias_stats.num_universal,
3241 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3242 alias_stats.num_same_objects, alias_stats.num_volatile,
3243 alias_stats.num_dag, alias_stats.num_universal);
3245 #include "gt-alias.h"