Fix spawned function with lambda function
[official-gcc.git] / gcc / alias.c
blobf12d9d12a2bc16bf2ca65f60005a03c313cad971
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_d {
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;
161 typedef struct alias_set_entry_d *alias_set_entry;
163 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
164 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
165 static void record_set (rtx, const_rtx, void *);
166 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
167 machine_mode);
168 static rtx find_base_value (rtx);
169 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
170 static alias_set_entry get_alias_set_entry (alias_set_type);
171 static tree decl_for_component_ref (tree);
172 static int write_dependence_p (const_rtx,
173 const_rtx, machine_mode, rtx,
174 bool, bool, bool);
176 static void memory_modified_1 (rtx, const_rtx, void *);
178 /* Query statistics for the different low-level disambiguators.
179 A high-level query may trigger multiple of them. */
181 static struct {
182 unsigned long long num_alias_zero;
183 unsigned long long num_same_alias_set;
184 unsigned long long num_same_objects;
185 unsigned long long num_volatile;
186 unsigned long long num_dag;
187 unsigned long long num_universal;
188 unsigned long long num_disambiguated;
189 } alias_stats;
192 /* Set up all info needed to perform alias analysis on memory references. */
194 /* Returns the size in bytes of the mode of X. */
195 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
197 /* Cap the number of passes we make over the insns propagating alias
198 information through set chains.
199 ??? 10 is a completely arbitrary choice. This should be based on the
200 maximum loop depth in the CFG, but we do not have this information
201 available (even if current_loops _is_ available). */
202 #define MAX_ALIAS_LOOP_PASSES 10
204 /* reg_base_value[N] gives an address to which register N is related.
205 If all sets after the first add or subtract to the current value
206 or otherwise modify it so it does not point to a different top level
207 object, reg_base_value[N] is equal to the address part of the source
208 of the first set.
210 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
211 expressions represent three types of base:
213 1. incoming arguments. There is just one ADDRESS to represent all
214 arguments, since we do not know at this level whether accesses
215 based on different arguments can alias. The ADDRESS has id 0.
217 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
218 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
219 Each of these rtxes has a separate ADDRESS associated with it,
220 each with a negative id.
222 GCC is (and is required to be) precise in which register it
223 chooses to access a particular region of stack. We can therefore
224 assume that accesses based on one of these rtxes do not alias
225 accesses based on another of these rtxes.
227 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
228 Each such piece of memory has a separate ADDRESS associated
229 with it, each with an id greater than 0.
231 Accesses based on one ADDRESS do not alias accesses based on other
232 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
233 alias globals either; the ADDRESSes have Pmode to indicate this.
234 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
235 indicate this. */
237 static GTY(()) vec<rtx, va_gc> *reg_base_value;
238 static rtx *new_reg_base_value;
240 /* The single VOIDmode ADDRESS that represents all argument bases.
241 It has id 0. */
242 static GTY(()) rtx arg_base_value;
244 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
245 static int unique_id;
247 /* We preserve the copy of old array around to avoid amount of garbage
248 produced. About 8% of garbage produced were attributed to this
249 array. */
250 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
252 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
253 registers. */
254 #define UNIQUE_BASE_VALUE_SP -1
255 #define UNIQUE_BASE_VALUE_ARGP -2
256 #define UNIQUE_BASE_VALUE_FP -3
257 #define UNIQUE_BASE_VALUE_HFP -4
259 #define static_reg_base_value \
260 (this_target_rtl->x_static_reg_base_value)
262 #define REG_BASE_VALUE(X) \
263 (REGNO (X) < vec_safe_length (reg_base_value) \
264 ? (*reg_base_value)[REGNO (X)] : 0)
266 /* Vector indexed by N giving the initial (unchanging) value known for
267 pseudo-register N. This vector is initialized in init_alias_analysis,
268 and does not change until end_alias_analysis is called. */
269 static GTY(()) vec<rtx, va_gc> *reg_known_value;
271 /* Vector recording for each reg_known_value whether it is due to a
272 REG_EQUIV note. Future passes (viz., reload) may replace the
273 pseudo with the equivalent expression and so we account for the
274 dependences that would be introduced if that happens.
276 The REG_EQUIV notes created in assign_parms may mention the arg
277 pointer, and there are explicit insns in the RTL that modify the
278 arg pointer. Thus we must ensure that such insns don't get
279 scheduled across each other because that would invalidate the
280 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
281 wrong, but solving the problem in the scheduler will likely give
282 better code, so we do it here. */
283 static sbitmap reg_known_equiv_p;
285 /* True when scanning insns from the start of the rtl to the
286 NOTE_INSN_FUNCTION_BEG note. */
287 static bool copying_arguments;
290 /* The splay-tree used to store the various alias set entries. */
291 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
293 /* Build a decomposed reference object for querying the alias-oracle
294 from the MEM rtx and store it in *REF.
295 Returns false if MEM is not suitable for the alias-oracle. */
297 static bool
298 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
300 tree expr = MEM_EXPR (mem);
301 tree base;
303 if (!expr)
304 return false;
306 ao_ref_init (ref, expr);
308 /* Get the base of the reference and see if we have to reject or
309 adjust it. */
310 base = ao_ref_base (ref);
311 if (base == NULL_TREE)
312 return false;
314 /* The tree oracle doesn't like bases that are neither decls
315 nor indirect references of SSA names. */
316 if (!(DECL_P (base)
317 || (TREE_CODE (base) == MEM_REF
318 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
319 || (TREE_CODE (base) == TARGET_MEM_REF
320 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
321 return false;
323 /* If this is a reference based on a partitioned decl replace the
324 base with a MEM_REF of the pointer representative we
325 created during stack slot partitioning. */
326 if (TREE_CODE (base) == VAR_DECL
327 && ! is_global_var (base)
328 && cfun->gimple_df->decls_to_pointers != NULL)
330 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
331 if (namep)
332 ref->base = build_simple_mem_ref (*namep);
335 ref->ref_alias_set = MEM_ALIAS_SET (mem);
337 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
338 is conservative, so trust it. */
339 if (!MEM_OFFSET_KNOWN_P (mem)
340 || !MEM_SIZE_KNOWN_P (mem))
341 return true;
343 /* If the base decl is a parameter we can have negative MEM_OFFSET in
344 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
345 here. */
346 if (MEM_OFFSET (mem) < 0
347 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
348 return true;
350 /* Otherwise continue and refine size and offset we got from analyzing
351 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
353 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
354 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
356 /* The MEM may extend into adjacent fields, so adjust max_size if
357 necessary. */
358 if (ref->max_size != -1
359 && ref->size > ref->max_size)
360 ref->max_size = ref->size;
362 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
363 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
364 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
365 && (ref->offset < 0
366 || (DECL_P (ref->base)
367 && (DECL_SIZE (ref->base) == NULL_TREE
368 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
369 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
370 ref->offset + ref->size)))))
371 return false;
373 return true;
376 /* Query the alias-oracle on whether the two memory rtx X and MEM may
377 alias. If TBAA_P is set also apply TBAA. Returns true if the
378 two rtxen may alias, false otherwise. */
380 static bool
381 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
383 ao_ref ref1, ref2;
385 if (!ao_ref_from_mem (&ref1, x)
386 || !ao_ref_from_mem (&ref2, mem))
387 return true;
389 return refs_may_alias_p_1 (&ref1, &ref2,
390 tbaa_p
391 && MEM_ALIAS_SET (x) != 0
392 && MEM_ALIAS_SET (mem) != 0);
395 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
396 such an entry, or NULL otherwise. */
398 static inline alias_set_entry
399 get_alias_set_entry (alias_set_type alias_set)
401 return (*alias_sets)[alias_set];
404 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
405 the two MEMs cannot alias each other. */
407 static inline int
408 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
410 return (flag_strict_aliasing
411 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
412 MEM_ALIAS_SET (mem2)));
415 /* Return true if the first alias set is a subset of the second. */
417 bool
418 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
420 alias_set_entry ase2;
422 /* Everything is a subset of the "aliases everything" set. */
423 if (set2 == 0)
424 return true;
426 /* Check if set1 is a subset of set2. */
427 ase2 = get_alias_set_entry (set2);
428 if (ase2 != 0
429 && (ase2->has_zero_child
430 || (ase2->children && ase2->children->get (set1))))
431 return true;
433 /* As a special case we consider alias set of "void *" to be both subset
434 and superset of every alias set of a pointer. This extra symmetry does
435 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
436 to return true on the following testcase:
438 void *ptr;
439 char **ptr2=(char **)&ptr;
440 *ptr2 = ...
442 Additionally if a set contains universal pointer, we consider every pointer
443 to be a subset of it, but we do not represent this explicitely - doing so
444 would require us to update transitive closure each time we introduce new
445 pointer type. This makes aliasing_component_refs_p to return true
446 on the following testcase:
448 struct a {void *ptr;}
449 char **ptr = (char **)&a.ptr;
450 ptr = ...
452 This makes void * truly universal pointer type. See pointer handling in
453 get_alias_set for more details. */
454 if (ase2 && ase2->has_pointer)
456 alias_set_entry ase1 = get_alias_set_entry (set1);
458 if (ase1 && ase1->is_pointer)
460 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
461 /* If one is ptr_type_node and other is pointer, then we consider
462 them subset of each other. */
463 if (set1 == voidptr_set || set2 == voidptr_set)
464 return true;
465 /* If SET2 contains universal pointer's alias set, then we consdier
466 every (non-universal) pointer. */
467 if (ase2->children && set1 != voidptr_set
468 && ase2->children->get (voidptr_set))
469 return true;
472 return false;
475 /* Return 1 if the two specified alias sets may conflict. */
478 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
480 alias_set_entry ase1;
481 alias_set_entry ase2;
483 /* The easy case. */
484 if (alias_sets_must_conflict_p (set1, set2))
485 return 1;
487 /* See if the first alias set is a subset of the second. */
488 ase1 = get_alias_set_entry (set1);
489 if (ase1 != 0
490 && ase1->children && ase1->children->get (set2))
492 ++alias_stats.num_dag;
493 return 1;
496 /* Now do the same, but with the alias sets reversed. */
497 ase2 = get_alias_set_entry (set2);
498 if (ase2 != 0
499 && ase2->children && ase2->children->get (set1))
501 ++alias_stats.num_dag;
502 return 1;
505 /* We want void * to be compatible with any other pointer without
506 really dropping it to alias set 0. Doing so would make it
507 compatible with all non-pointer types too.
509 This is not strictly necessary by the C/C++ language
510 standards, but avoids common type punning mistakes. In
511 addition to that, we need the existence of such universal
512 pointer to implement Fortran's C_PTR type (which is defined as
513 type compatible with all C pointers). */
514 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
516 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
518 /* If one of the sets corresponds to universal pointer,
519 we consider it to conflict with anything that is
520 or contains pointer. */
521 if (set1 == voidptr_set || set2 == voidptr_set)
523 ++alias_stats.num_universal;
524 return true;
526 /* If one of sets is (non-universal) pointer and the other
527 contains universal pointer, we also get conflict. */
528 if (ase1->is_pointer && set2 != voidptr_set
529 && ase2->children && ase2->children->get (voidptr_set))
531 ++alias_stats.num_universal;
532 return true;
534 if (ase2->is_pointer && set1 != voidptr_set
535 && ase1->children && ase1->children->get (voidptr_set))
537 ++alias_stats.num_universal;
538 return true;
542 ++alias_stats.num_disambiguated;
544 /* The two alias sets are distinct and neither one is the
545 child of the other. Therefore, they cannot conflict. */
546 return 0;
549 /* Return 1 if the two specified alias sets will always conflict. */
552 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
554 if (set1 == 0 || set2 == 0)
556 ++alias_stats.num_alias_zero;
557 return 1;
559 if (set1 == set2)
561 ++alias_stats.num_same_alias_set;
562 return 1;
565 return 0;
568 /* Return 1 if any MEM object of type T1 will always conflict (using the
569 dependency routines in this file) with any MEM object of type T2.
570 This is used when allocating temporary storage. If T1 and/or T2 are
571 NULL_TREE, it means we know nothing about the storage. */
574 objects_must_conflict_p (tree t1, tree t2)
576 alias_set_type set1, set2;
578 /* If neither has a type specified, we don't know if they'll conflict
579 because we may be using them to store objects of various types, for
580 example the argument and local variables areas of inlined functions. */
581 if (t1 == 0 && t2 == 0)
582 return 0;
584 /* If they are the same type, they must conflict. */
585 if (t1 == t2)
587 ++alias_stats.num_same_objects;
588 return 1;
590 /* Likewise if both are volatile. */
591 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
593 ++alias_stats.num_volatile;
594 return 1;
597 set1 = t1 ? get_alias_set (t1) : 0;
598 set2 = t2 ? get_alias_set (t2) : 0;
600 /* We can't use alias_sets_conflict_p because we must make sure
601 that every subtype of t1 will conflict with every subtype of
602 t2 for which a pair of subobjects of these respective subtypes
603 overlaps on the stack. */
604 return alias_sets_must_conflict_p (set1, set2);
607 /* Return the outermost parent of component present in the chain of
608 component references handled by get_inner_reference in T with the
609 following property:
610 - the component is non-addressable, or
611 - the parent has alias set zero,
612 or NULL_TREE if no such parent exists. In the former cases, the alias
613 set of this parent is the alias set that must be used for T itself. */
615 tree
616 component_uses_parent_alias_set_from (const_tree t)
618 const_tree found = NULL_TREE;
620 while (handled_component_p (t))
622 switch (TREE_CODE (t))
624 case COMPONENT_REF:
625 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
626 found = t;
627 break;
629 case ARRAY_REF:
630 case ARRAY_RANGE_REF:
631 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
632 found = t;
633 break;
635 case REALPART_EXPR:
636 case IMAGPART_EXPR:
637 break;
639 case BIT_FIELD_REF:
640 case VIEW_CONVERT_EXPR:
641 /* Bitfields and casts are never addressable. */
642 found = t;
643 break;
645 default:
646 gcc_unreachable ();
649 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
650 found = t;
652 t = TREE_OPERAND (t, 0);
655 if (found)
656 return TREE_OPERAND (found, 0);
658 return NULL_TREE;
662 /* Return whether the pointer-type T effective for aliasing may
663 access everything and thus the reference has to be assigned
664 alias-set zero. */
666 static bool
667 ref_all_alias_ptr_type_p (const_tree t)
669 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
670 || TYPE_REF_CAN_ALIAS_ALL (t));
673 /* Return the alias set for the memory pointed to by T, which may be
674 either a type or an expression. Return -1 if there is nothing
675 special about dereferencing T. */
677 static alias_set_type
678 get_deref_alias_set_1 (tree t)
680 /* All we care about is the type. */
681 if (! TYPE_P (t))
682 t = TREE_TYPE (t);
684 /* If we have an INDIRECT_REF via a void pointer, we don't
685 know anything about what that might alias. Likewise if the
686 pointer is marked that way. */
687 if (ref_all_alias_ptr_type_p (t))
688 return 0;
690 return -1;
693 /* Return the alias set for the memory pointed to by T, which may be
694 either a type or an expression. */
696 alias_set_type
697 get_deref_alias_set (tree t)
699 /* If we're not doing any alias analysis, just assume everything
700 aliases everything else. */
701 if (!flag_strict_aliasing)
702 return 0;
704 alias_set_type set = get_deref_alias_set_1 (t);
706 /* Fall back to the alias-set of the pointed-to type. */
707 if (set == -1)
709 if (! TYPE_P (t))
710 t = TREE_TYPE (t);
711 set = get_alias_set (TREE_TYPE (t));
714 return set;
717 /* Return the pointer-type relevant for TBAA purposes from the
718 memory reference tree *T or NULL_TREE in which case *T is
719 adjusted to point to the outermost component reference that
720 can be used for assigning an alias set. */
722 static tree
723 reference_alias_ptr_type_1 (tree *t)
725 tree inner;
727 /* Get the base object of the reference. */
728 inner = *t;
729 while (handled_component_p (inner))
731 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
732 the type of any component references that wrap it to
733 determine the alias-set. */
734 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
735 *t = TREE_OPERAND (inner, 0);
736 inner = TREE_OPERAND (inner, 0);
739 /* Handle pointer dereferences here, they can override the
740 alias-set. */
741 if (INDIRECT_REF_P (inner)
742 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
743 return TREE_TYPE (TREE_OPERAND (inner, 0));
744 else if (TREE_CODE (inner) == TARGET_MEM_REF)
745 return TREE_TYPE (TMR_OFFSET (inner));
746 else if (TREE_CODE (inner) == MEM_REF
747 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
748 return TREE_TYPE (TREE_OPERAND (inner, 1));
750 /* If the innermost reference is a MEM_REF that has a
751 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
752 using the memory access type for determining the alias-set. */
753 if (TREE_CODE (inner) == MEM_REF
754 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
755 != TYPE_MAIN_VARIANT
756 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
757 return TREE_TYPE (TREE_OPERAND (inner, 1));
759 /* Otherwise, pick up the outermost object that we could have
760 a pointer to. */
761 tree tem = component_uses_parent_alias_set_from (*t);
762 if (tem)
763 *t = tem;
765 return NULL_TREE;
768 /* Return the pointer-type relevant for TBAA purposes from the
769 gimple memory reference tree T. This is the type to be used for
770 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
771 and guarantees that get_alias_set will return the same alias
772 set for T and the replacement. */
774 tree
775 reference_alias_ptr_type (tree t)
777 tree ptype = reference_alias_ptr_type_1 (&t);
778 /* If there is a given pointer type for aliasing purposes, return it. */
779 if (ptype != NULL_TREE)
780 return ptype;
782 /* Otherwise build one from the outermost component reference we
783 may use. */
784 if (TREE_CODE (t) == MEM_REF
785 || TREE_CODE (t) == TARGET_MEM_REF)
786 return TREE_TYPE (TREE_OPERAND (t, 1));
787 else
788 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
791 /* Return whether the pointer-types T1 and T2 used to determine
792 two alias sets of two references will yield the same answer
793 from get_deref_alias_set. */
795 bool
796 alias_ptr_types_compatible_p (tree t1, tree t2)
798 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
799 return true;
801 if (ref_all_alias_ptr_type_p (t1)
802 || ref_all_alias_ptr_type_p (t2))
803 return false;
805 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
806 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
809 /* Create emptry alias set entry. */
811 alias_set_entry
812 init_alias_set_entry (alias_set_type set)
814 alias_set_entry ase = ggc_alloc<alias_set_entry_d> ();
815 ase->alias_set = set;
816 ase->children = NULL;
817 ase->has_zero_child = false;
818 ase->is_pointer = false;
819 ase->has_pointer = false;
820 gcc_checking_assert (!get_alias_set_entry (set));
821 (*alias_sets)[set] = ase;
822 return ase;
825 /* Return the alias set for T, which may be either a type or an
826 expression. Call language-specific routine for help, if needed. */
828 alias_set_type
829 get_alias_set (tree t)
831 alias_set_type set;
833 /* If we're not doing any alias analysis, just assume everything
834 aliases everything else. Also return 0 if this or its type is
835 an error. */
836 if (! flag_strict_aliasing || t == error_mark_node
837 || (! TYPE_P (t)
838 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
839 return 0;
841 /* We can be passed either an expression or a type. This and the
842 language-specific routine may make mutually-recursive calls to each other
843 to figure out what to do. At each juncture, we see if this is a tree
844 that the language may need to handle specially. First handle things that
845 aren't types. */
846 if (! TYPE_P (t))
848 /* Give the language a chance to do something with this tree
849 before we look at it. */
850 STRIP_NOPS (t);
851 set = lang_hooks.get_alias_set (t);
852 if (set != -1)
853 return set;
855 /* Get the alias pointer-type to use or the outermost object
856 that we could have a pointer to. */
857 tree ptype = reference_alias_ptr_type_1 (&t);
858 if (ptype != NULL)
859 return get_deref_alias_set (ptype);
861 /* If we've already determined the alias set for a decl, just return
862 it. This is necessary for C++ anonymous unions, whose component
863 variables don't look like union members (boo!). */
864 if (TREE_CODE (t) == VAR_DECL
865 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
866 return MEM_ALIAS_SET (DECL_RTL (t));
868 /* Now all we care about is the type. */
869 t = TREE_TYPE (t);
872 /* Variant qualifiers don't affect the alias set, so get the main
873 variant. */
874 t = TYPE_MAIN_VARIANT (t);
876 /* Always use the canonical type as well. If this is a type that
877 requires structural comparisons to identify compatible types
878 use alias set zero. */
879 if (TYPE_STRUCTURAL_EQUALITY_P (t))
881 /* Allow the language to specify another alias set for this
882 type. */
883 set = lang_hooks.get_alias_set (t);
884 if (set != -1)
885 return set;
886 return 0;
889 t = TYPE_CANONICAL (t);
891 /* The canonical type should not require structural equality checks. */
892 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
894 /* If this is a type with a known alias set, return it. */
895 if (TYPE_ALIAS_SET_KNOWN_P (t))
896 return TYPE_ALIAS_SET (t);
898 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
899 if (!COMPLETE_TYPE_P (t))
901 /* For arrays with unknown size the conservative answer is the
902 alias set of the element type. */
903 if (TREE_CODE (t) == ARRAY_TYPE)
904 return get_alias_set (TREE_TYPE (t));
906 /* But return zero as a conservative answer for incomplete types. */
907 return 0;
910 /* See if the language has special handling for this type. */
911 set = lang_hooks.get_alias_set (t);
912 if (set != -1)
913 return set;
915 /* There are no objects of FUNCTION_TYPE, so there's no point in
916 using up an alias set for them. (There are, of course, pointers
917 and references to functions, but that's different.) */
918 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
919 set = 0;
921 /* Unless the language specifies otherwise, let vector types alias
922 their components. This avoids some nasty type punning issues in
923 normal usage. And indeed lets vectors be treated more like an
924 array slice. */
925 else if (TREE_CODE (t) == VECTOR_TYPE)
926 set = get_alias_set (TREE_TYPE (t));
928 /* Unless the language specifies otherwise, treat array types the
929 same as their components. This avoids the asymmetry we get
930 through recording the components. Consider accessing a
931 character(kind=1) through a reference to a character(kind=1)[1:1].
932 Or consider if we want to assign integer(kind=4)[0:D.1387] and
933 integer(kind=4)[4] the same alias set or not.
934 Just be pragmatic here and make sure the array and its element
935 type get the same alias set assigned. */
936 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
937 set = get_alias_set (TREE_TYPE (t));
939 /* From the former common C and C++ langhook implementation:
941 Unfortunately, there is no canonical form of a pointer type.
942 In particular, if we have `typedef int I', then `int *', and
943 `I *' are different types. So, we have to pick a canonical
944 representative. We do this below.
946 Technically, this approach is actually more conservative that
947 it needs to be. In particular, `const int *' and `int *'
948 should be in different alias sets, according to the C and C++
949 standard, since their types are not the same, and so,
950 technically, an `int **' and `const int **' cannot point at
951 the same thing.
953 But, the standard is wrong. In particular, this code is
954 legal C++:
956 int *ip;
957 int **ipp = &ip;
958 const int* const* cipp = ipp;
959 And, it doesn't make sense for that to be legal unless you
960 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
961 the pointed-to types. This issue has been reported to the
962 C++ committee.
964 For this reason go to canonical type of the unqalified pointer type.
965 Until GCC 6 this code set all pointers sets to have alias set of
966 ptr_type_node but that is a bad idea, because it prevents disabiguations
967 in between pointers. For Firefox this accounts about 20% of all
968 disambiguations in the program. */
969 else if (POINTER_TYPE_P (t) && t != ptr_type_node && !in_lto_p)
971 tree p;
972 auto_vec <bool, 8> reference;
974 /* Unnest all pointers and references.
975 We also want to make pointer to array equivalent to pointer to its
976 element. So skip all array types, too. */
977 for (p = t; POINTER_TYPE_P (p)
978 || (TREE_CODE (p) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (p));
979 p = TREE_TYPE (p))
981 if (TREE_CODE (p) == REFERENCE_TYPE)
982 reference.safe_push (true);
983 if (TREE_CODE (p) == POINTER_TYPE)
984 reference.safe_push (false);
986 p = TYPE_MAIN_VARIANT (p);
988 /* Make void * compatible with char * and also void **.
989 Programs are commonly violating TBAA by this.
991 We also make void * to conflict with every pointer
992 (see record_component_aliases) and thus it is safe it to use it for
993 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
994 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
995 set = get_alias_set (ptr_type_node);
996 else
998 /* Rebuild pointer type from starting from canonical types using
999 unqualified pointers and references only. This way all such
1000 pointers will have the same alias set and will conflict with
1001 each other.
1003 Most of time we already have pointers or references of a given type.
1004 If not we build new one just to be sure that if someone later
1005 (probably only middle-end can, as we should assign all alias
1006 classes only after finishing translation unit) builds the pointer
1007 type, the canonical type will match. */
1008 p = TYPE_CANONICAL (p);
1009 while (!reference.is_empty ())
1011 if (reference.pop ())
1012 p = build_reference_type (p);
1013 else
1014 p = build_pointer_type (p);
1015 p = TYPE_CANONICAL (TYPE_MAIN_VARIANT (p));
1017 gcc_checking_assert (TYPE_CANONICAL (p) == p);
1019 /* Assign the alias set to both p and t.
1020 We can not call get_alias_set (p) here as that would trigger
1021 infinite recursion when p == t. In other cases it would just
1022 trigger unnecesary legwork of rebuilding the pointer again. */
1023 if (TYPE_ALIAS_SET_KNOWN_P (p))
1024 set = TYPE_ALIAS_SET (p);
1025 else
1027 set = new_alias_set ();
1028 TYPE_ALIAS_SET (p) = set;
1032 /* In LTO the rules above needs to be part of canonical type machinery.
1033 For now just punt. */
1034 else if (POINTER_TYPE_P (t)
1035 && t != TYPE_CANONICAL (ptr_type_node) && in_lto_p)
1036 set = get_alias_set (TYPE_CANONICAL (ptr_type_node));
1038 /* Otherwise make a new alias set for this type. */
1039 else
1041 /* Each canonical type gets its own alias set, so canonical types
1042 shouldn't form a tree. It doesn't really matter for types
1043 we handle specially above, so only check it where it possibly
1044 would result in a bogus alias set. */
1045 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1047 set = new_alias_set ();
1050 TYPE_ALIAS_SET (t) = set;
1052 /* If this is an aggregate type or a complex type, we must record any
1053 component aliasing information. */
1054 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1055 record_component_aliases (t);
1057 /* We treat pointer types specially in alias_set_subset_of. */
1058 if (POINTER_TYPE_P (t) && set)
1060 alias_set_entry ase = get_alias_set_entry (set);
1061 if (!ase)
1062 ase = init_alias_set_entry (set);
1063 ase->is_pointer = true;
1064 ase->has_pointer = true;
1067 return set;
1070 /* Return a brand-new alias set. */
1072 alias_set_type
1073 new_alias_set (void)
1075 if (flag_strict_aliasing)
1077 if (alias_sets == 0)
1078 vec_safe_push (alias_sets, (alias_set_entry) 0);
1079 vec_safe_push (alias_sets, (alias_set_entry) 0);
1080 return alias_sets->length () - 1;
1082 else
1083 return 0;
1086 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1087 not everything that aliases SUPERSET also aliases SUBSET. For example,
1088 in C, a store to an `int' can alias a load of a structure containing an
1089 `int', and vice versa. But it can't alias a load of a 'double' member
1090 of the same structure. Here, the structure would be the SUPERSET and
1091 `int' the SUBSET. This relationship is also described in the comment at
1092 the beginning of this file.
1094 This function should be called only once per SUPERSET/SUBSET pair.
1096 It is illegal for SUPERSET to be zero; everything is implicitly a
1097 subset of alias set zero. */
1099 void
1100 record_alias_subset (alias_set_type superset, alias_set_type subset)
1102 alias_set_entry superset_entry;
1103 alias_set_entry subset_entry;
1105 /* It is possible in complex type situations for both sets to be the same,
1106 in which case we can ignore this operation. */
1107 if (superset == subset)
1108 return;
1110 gcc_assert (superset);
1112 superset_entry = get_alias_set_entry (superset);
1113 if (superset_entry == 0)
1115 /* Create an entry for the SUPERSET, so that we have a place to
1116 attach the SUBSET. */
1117 superset_entry = init_alias_set_entry (superset);
1120 if (subset == 0)
1121 superset_entry->has_zero_child = 1;
1122 else
1124 subset_entry = get_alias_set_entry (subset);
1125 if (!superset_entry->children)
1126 superset_entry->children
1127 = hash_map<alias_set_hash, int>::create_ggc (64);
1128 /* If there is an entry for the subset, enter all of its children
1129 (if they are not already present) as children of the SUPERSET. */
1130 if (subset_entry)
1132 if (subset_entry->has_zero_child)
1133 superset_entry->has_zero_child = true;
1134 if (subset_entry->has_pointer)
1135 superset_entry->has_pointer = true;
1137 if (subset_entry->children)
1139 hash_map<alias_set_hash, int>::iterator iter
1140 = subset_entry->children->begin ();
1141 for (; iter != subset_entry->children->end (); ++iter)
1142 superset_entry->children->put ((*iter).first, (*iter).second);
1146 /* Enter the SUBSET itself as a child of the SUPERSET. */
1147 superset_entry->children->put (subset, 0);
1151 /* Record that component types of TYPE, if any, are part of that type for
1152 aliasing purposes. For record types, we only record component types
1153 for fields that are not marked non-addressable. For array types, we
1154 only record the component type if it is not marked non-aliased. */
1156 void
1157 record_component_aliases (tree type)
1159 alias_set_type superset = get_alias_set (type);
1160 tree field;
1162 if (superset == 0)
1163 return;
1165 switch (TREE_CODE (type))
1167 case RECORD_TYPE:
1168 case UNION_TYPE:
1169 case QUAL_UNION_TYPE:
1170 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1171 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1172 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1173 break;
1175 case COMPLEX_TYPE:
1176 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1177 break;
1179 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1180 element type. */
1182 default:
1183 break;
1187 /* Allocate an alias set for use in storing and reading from the varargs
1188 spill area. */
1190 static GTY(()) alias_set_type varargs_set = -1;
1192 alias_set_type
1193 get_varargs_alias_set (void)
1195 #if 1
1196 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1197 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1198 consistently use the varargs alias set for loads from the varargs
1199 area. So don't use it anywhere. */
1200 return 0;
1201 #else
1202 if (varargs_set == -1)
1203 varargs_set = new_alias_set ();
1205 return varargs_set;
1206 #endif
1209 /* Likewise, but used for the fixed portions of the frame, e.g., register
1210 save areas. */
1212 static GTY(()) alias_set_type frame_set = -1;
1214 alias_set_type
1215 get_frame_alias_set (void)
1217 if (frame_set == -1)
1218 frame_set = new_alias_set ();
1220 return frame_set;
1223 /* Create a new, unique base with id ID. */
1225 static rtx
1226 unique_base_value (HOST_WIDE_INT id)
1228 return gen_rtx_ADDRESS (Pmode, id);
1231 /* Return true if accesses based on any other base value cannot alias
1232 those based on X. */
1234 static bool
1235 unique_base_value_p (rtx x)
1237 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1240 /* Return true if X is known to be a base value. */
1242 static bool
1243 known_base_value_p (rtx x)
1245 switch (GET_CODE (x))
1247 case LABEL_REF:
1248 case SYMBOL_REF:
1249 return true;
1251 case ADDRESS:
1252 /* Arguments may or may not be bases; we don't know for sure. */
1253 return GET_MODE (x) != VOIDmode;
1255 default:
1256 return false;
1260 /* Inside SRC, the source of a SET, find a base address. */
1262 static rtx
1263 find_base_value (rtx src)
1265 unsigned int regno;
1267 #if defined (FIND_BASE_TERM)
1268 /* Try machine-dependent ways to find the base term. */
1269 src = FIND_BASE_TERM (src);
1270 #endif
1272 switch (GET_CODE (src))
1274 case SYMBOL_REF:
1275 case LABEL_REF:
1276 return src;
1278 case REG:
1279 regno = REGNO (src);
1280 /* At the start of a function, argument registers have known base
1281 values which may be lost later. Returning an ADDRESS
1282 expression here allows optimization based on argument values
1283 even when the argument registers are used for other purposes. */
1284 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1285 return new_reg_base_value[regno];
1287 /* If a pseudo has a known base value, return it. Do not do this
1288 for non-fixed hard regs since it can result in a circular
1289 dependency chain for registers which have values at function entry.
1291 The test above is not sufficient because the scheduler may move
1292 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1293 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1294 && regno < vec_safe_length (reg_base_value))
1296 /* If we're inside init_alias_analysis, use new_reg_base_value
1297 to reduce the number of relaxation iterations. */
1298 if (new_reg_base_value && new_reg_base_value[regno]
1299 && DF_REG_DEF_COUNT (regno) == 1)
1300 return new_reg_base_value[regno];
1302 if ((*reg_base_value)[regno])
1303 return (*reg_base_value)[regno];
1306 return 0;
1308 case MEM:
1309 /* Check for an argument passed in memory. Only record in the
1310 copying-arguments block; it is too hard to track changes
1311 otherwise. */
1312 if (copying_arguments
1313 && (XEXP (src, 0) == arg_pointer_rtx
1314 || (GET_CODE (XEXP (src, 0)) == PLUS
1315 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1316 return arg_base_value;
1317 return 0;
1319 case CONST:
1320 src = XEXP (src, 0);
1321 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1322 break;
1324 /* ... fall through ... */
1326 case PLUS:
1327 case MINUS:
1329 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1331 /* If either operand is a REG that is a known pointer, then it
1332 is the base. */
1333 if (REG_P (src_0) && REG_POINTER (src_0))
1334 return find_base_value (src_0);
1335 if (REG_P (src_1) && REG_POINTER (src_1))
1336 return find_base_value (src_1);
1338 /* If either operand is a REG, then see if we already have
1339 a known value for it. */
1340 if (REG_P (src_0))
1342 temp = find_base_value (src_0);
1343 if (temp != 0)
1344 src_0 = temp;
1347 if (REG_P (src_1))
1349 temp = find_base_value (src_1);
1350 if (temp!= 0)
1351 src_1 = temp;
1354 /* If either base is named object or a special address
1355 (like an argument or stack reference), then use it for the
1356 base term. */
1357 if (src_0 != 0 && known_base_value_p (src_0))
1358 return src_0;
1360 if (src_1 != 0 && known_base_value_p (src_1))
1361 return src_1;
1363 /* Guess which operand is the base address:
1364 If either operand is a symbol, then it is the base. If
1365 either operand is a CONST_INT, then the other is the base. */
1366 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1367 return find_base_value (src_0);
1368 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1369 return find_base_value (src_1);
1371 return 0;
1374 case LO_SUM:
1375 /* The standard form is (lo_sum reg sym) so look only at the
1376 second operand. */
1377 return find_base_value (XEXP (src, 1));
1379 case AND:
1380 /* If the second operand is constant set the base
1381 address to the first operand. */
1382 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1383 return find_base_value (XEXP (src, 0));
1384 return 0;
1386 case TRUNCATE:
1387 /* As we do not know which address space the pointer is referring to, we can
1388 handle this only if the target does not support different pointer or
1389 address modes depending on the address space. */
1390 if (!target_default_pointer_address_modes_p ())
1391 break;
1392 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1393 break;
1394 /* Fall through. */
1395 case HIGH:
1396 case PRE_INC:
1397 case PRE_DEC:
1398 case POST_INC:
1399 case POST_DEC:
1400 case PRE_MODIFY:
1401 case POST_MODIFY:
1402 return find_base_value (XEXP (src, 0));
1404 case ZERO_EXTEND:
1405 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1406 /* As we do not know which address space the pointer is referring to, we can
1407 handle this only if the target does not support different pointer or
1408 address modes depending on the address space. */
1409 if (!target_default_pointer_address_modes_p ())
1410 break;
1413 rtx temp = find_base_value (XEXP (src, 0));
1415 if (temp != 0 && CONSTANT_P (temp))
1416 temp = convert_memory_address (Pmode, temp);
1418 return temp;
1421 default:
1422 break;
1425 return 0;
1428 /* Called from init_alias_analysis indirectly through note_stores,
1429 or directly if DEST is a register with a REG_NOALIAS note attached.
1430 SET is null in the latter case. */
1432 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1433 register N has been set in this function. */
1434 static sbitmap reg_seen;
1436 static void
1437 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1439 unsigned regno;
1440 rtx src;
1441 int n;
1443 if (!REG_P (dest))
1444 return;
1446 regno = REGNO (dest);
1448 gcc_checking_assert (regno < reg_base_value->length ());
1450 n = REG_NREGS (dest);
1451 if (n != 1)
1453 while (--n >= 0)
1455 bitmap_set_bit (reg_seen, regno + n);
1456 new_reg_base_value[regno + n] = 0;
1458 return;
1461 if (set)
1463 /* A CLOBBER wipes out any old value but does not prevent a previously
1464 unset register from acquiring a base address (i.e. reg_seen is not
1465 set). */
1466 if (GET_CODE (set) == CLOBBER)
1468 new_reg_base_value[regno] = 0;
1469 return;
1471 src = SET_SRC (set);
1473 else
1475 /* There's a REG_NOALIAS note against DEST. */
1476 if (bitmap_bit_p (reg_seen, regno))
1478 new_reg_base_value[regno] = 0;
1479 return;
1481 bitmap_set_bit (reg_seen, regno);
1482 new_reg_base_value[regno] = unique_base_value (unique_id++);
1483 return;
1486 /* If this is not the first set of REGNO, see whether the new value
1487 is related to the old one. There are two cases of interest:
1489 (1) The register might be assigned an entirely new value
1490 that has the same base term as the original set.
1492 (2) The set might be a simple self-modification that
1493 cannot change REGNO's base value.
1495 If neither case holds, reject the original base value as invalid.
1496 Note that the following situation is not detected:
1498 extern int x, y; int *p = &x; p += (&y-&x);
1500 ANSI C does not allow computing the difference of addresses
1501 of distinct top level objects. */
1502 if (new_reg_base_value[regno] != 0
1503 && find_base_value (src) != new_reg_base_value[regno])
1504 switch (GET_CODE (src))
1506 case LO_SUM:
1507 case MINUS:
1508 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1509 new_reg_base_value[regno] = 0;
1510 break;
1511 case PLUS:
1512 /* If the value we add in the PLUS is also a valid base value,
1513 this might be the actual base value, and the original value
1514 an index. */
1516 rtx other = NULL_RTX;
1518 if (XEXP (src, 0) == dest)
1519 other = XEXP (src, 1);
1520 else if (XEXP (src, 1) == dest)
1521 other = XEXP (src, 0);
1523 if (! other || find_base_value (other))
1524 new_reg_base_value[regno] = 0;
1525 break;
1527 case AND:
1528 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1529 new_reg_base_value[regno] = 0;
1530 break;
1531 default:
1532 new_reg_base_value[regno] = 0;
1533 break;
1535 /* If this is the first set of a register, record the value. */
1536 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1537 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1538 new_reg_base_value[regno] = find_base_value (src);
1540 bitmap_set_bit (reg_seen, regno);
1543 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1544 using hard registers with non-null REG_BASE_VALUE for renaming. */
1546 get_reg_base_value (unsigned int regno)
1548 return (*reg_base_value)[regno];
1551 /* If a value is known for REGNO, return it. */
1554 get_reg_known_value (unsigned int regno)
1556 if (regno >= FIRST_PSEUDO_REGISTER)
1558 regno -= FIRST_PSEUDO_REGISTER;
1559 if (regno < vec_safe_length (reg_known_value))
1560 return (*reg_known_value)[regno];
1562 return NULL;
1565 /* Set it. */
1567 static void
1568 set_reg_known_value (unsigned int regno, rtx val)
1570 if (regno >= FIRST_PSEUDO_REGISTER)
1572 regno -= FIRST_PSEUDO_REGISTER;
1573 if (regno < vec_safe_length (reg_known_value))
1574 (*reg_known_value)[regno] = val;
1578 /* Similarly for reg_known_equiv_p. */
1580 bool
1581 get_reg_known_equiv_p (unsigned int regno)
1583 if (regno >= FIRST_PSEUDO_REGISTER)
1585 regno -= FIRST_PSEUDO_REGISTER;
1586 if (regno < vec_safe_length (reg_known_value))
1587 return bitmap_bit_p (reg_known_equiv_p, regno);
1589 return false;
1592 static void
1593 set_reg_known_equiv_p (unsigned int regno, bool val)
1595 if (regno >= FIRST_PSEUDO_REGISTER)
1597 regno -= FIRST_PSEUDO_REGISTER;
1598 if (regno < vec_safe_length (reg_known_value))
1600 if (val)
1601 bitmap_set_bit (reg_known_equiv_p, regno);
1602 else
1603 bitmap_clear_bit (reg_known_equiv_p, regno);
1609 /* Returns a canonical version of X, from the point of view alias
1610 analysis. (For example, if X is a MEM whose address is a register,
1611 and the register has a known value (say a SYMBOL_REF), then a MEM
1612 whose address is the SYMBOL_REF is returned.) */
1615 canon_rtx (rtx x)
1617 /* Recursively look for equivalences. */
1618 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1620 rtx t = get_reg_known_value (REGNO (x));
1621 if (t == x)
1622 return x;
1623 if (t)
1624 return canon_rtx (t);
1627 if (GET_CODE (x) == PLUS)
1629 rtx x0 = canon_rtx (XEXP (x, 0));
1630 rtx x1 = canon_rtx (XEXP (x, 1));
1632 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1634 if (CONST_INT_P (x0))
1635 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1636 else if (CONST_INT_P (x1))
1637 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1638 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1642 /* This gives us much better alias analysis when called from
1643 the loop optimizer. Note we want to leave the original
1644 MEM alone, but need to return the canonicalized MEM with
1645 all the flags with their original values. */
1646 else if (MEM_P (x))
1647 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1649 return x;
1652 /* Return 1 if X and Y are identical-looking rtx's.
1653 Expect that X and Y has been already canonicalized.
1655 We use the data in reg_known_value above to see if two registers with
1656 different numbers are, in fact, equivalent. */
1658 static int
1659 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1661 int i;
1662 int j;
1663 enum rtx_code code;
1664 const char *fmt;
1666 if (x == 0 && y == 0)
1667 return 1;
1668 if (x == 0 || y == 0)
1669 return 0;
1671 if (x == y)
1672 return 1;
1674 code = GET_CODE (x);
1675 /* Rtx's of different codes cannot be equal. */
1676 if (code != GET_CODE (y))
1677 return 0;
1679 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1680 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1682 if (GET_MODE (x) != GET_MODE (y))
1683 return 0;
1685 /* Some RTL can be compared without a recursive examination. */
1686 switch (code)
1688 case REG:
1689 return REGNO (x) == REGNO (y);
1691 case LABEL_REF:
1692 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1694 case SYMBOL_REF:
1695 return XSTR (x, 0) == XSTR (y, 0);
1697 case ENTRY_VALUE:
1698 /* This is magic, don't go through canonicalization et al. */
1699 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1701 case VALUE:
1702 CASE_CONST_UNIQUE:
1703 /* Pointer equality guarantees equality for these nodes. */
1704 return 0;
1706 default:
1707 break;
1710 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1711 if (code == PLUS)
1712 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1713 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1714 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1715 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1716 /* For commutative operations, the RTX match if the operand match in any
1717 order. Also handle the simple binary and unary cases without a loop. */
1718 if (COMMUTATIVE_P (x))
1720 rtx xop0 = canon_rtx (XEXP (x, 0));
1721 rtx yop0 = canon_rtx (XEXP (y, 0));
1722 rtx yop1 = canon_rtx (XEXP (y, 1));
1724 return ((rtx_equal_for_memref_p (xop0, yop0)
1725 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1726 || (rtx_equal_for_memref_p (xop0, yop1)
1727 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1729 else if (NON_COMMUTATIVE_P (x))
1731 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1732 canon_rtx (XEXP (y, 0)))
1733 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1734 canon_rtx (XEXP (y, 1))));
1736 else if (UNARY_P (x))
1737 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1738 canon_rtx (XEXP (y, 0)));
1740 /* Compare the elements. If any pair of corresponding elements
1741 fail to match, return 0 for the whole things.
1743 Limit cases to types which actually appear in addresses. */
1745 fmt = GET_RTX_FORMAT (code);
1746 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1748 switch (fmt[i])
1750 case 'i':
1751 if (XINT (x, i) != XINT (y, i))
1752 return 0;
1753 break;
1755 case 'E':
1756 /* Two vectors must have the same length. */
1757 if (XVECLEN (x, i) != XVECLEN (y, i))
1758 return 0;
1760 /* And the corresponding elements must match. */
1761 for (j = 0; j < XVECLEN (x, i); j++)
1762 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1763 canon_rtx (XVECEXP (y, i, j))) == 0)
1764 return 0;
1765 break;
1767 case 'e':
1768 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1769 canon_rtx (XEXP (y, i))) == 0)
1770 return 0;
1771 break;
1773 /* This can happen for asm operands. */
1774 case 's':
1775 if (strcmp (XSTR (x, i), XSTR (y, i)))
1776 return 0;
1777 break;
1779 /* This can happen for an asm which clobbers memory. */
1780 case '0':
1781 break;
1783 /* It is believed that rtx's at this level will never
1784 contain anything but integers and other rtx's,
1785 except for within LABEL_REFs and SYMBOL_REFs. */
1786 default:
1787 gcc_unreachable ();
1790 return 1;
1793 static rtx
1794 find_base_term (rtx x)
1796 cselib_val *val;
1797 struct elt_loc_list *l, *f;
1798 rtx ret;
1800 #if defined (FIND_BASE_TERM)
1801 /* Try machine-dependent ways to find the base term. */
1802 x = FIND_BASE_TERM (x);
1803 #endif
1805 switch (GET_CODE (x))
1807 case REG:
1808 return REG_BASE_VALUE (x);
1810 case TRUNCATE:
1811 /* As we do not know which address space the pointer is referring to, we can
1812 handle this only if the target does not support different pointer or
1813 address modes depending on the address space. */
1814 if (!target_default_pointer_address_modes_p ())
1815 return 0;
1816 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1817 return 0;
1818 /* Fall through. */
1819 case HIGH:
1820 case PRE_INC:
1821 case PRE_DEC:
1822 case POST_INC:
1823 case POST_DEC:
1824 case PRE_MODIFY:
1825 case POST_MODIFY:
1826 return find_base_term (XEXP (x, 0));
1828 case ZERO_EXTEND:
1829 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1830 /* As we do not know which address space the pointer is referring to, we can
1831 handle this only if the target does not support different pointer or
1832 address modes depending on the address space. */
1833 if (!target_default_pointer_address_modes_p ())
1834 return 0;
1837 rtx temp = find_base_term (XEXP (x, 0));
1839 if (temp != 0 && CONSTANT_P (temp))
1840 temp = convert_memory_address (Pmode, temp);
1842 return temp;
1845 case VALUE:
1846 val = CSELIB_VAL_PTR (x);
1847 ret = NULL_RTX;
1849 if (!val)
1850 return ret;
1852 if (cselib_sp_based_value_p (val))
1853 return static_reg_base_value[STACK_POINTER_REGNUM];
1855 f = val->locs;
1856 /* Temporarily reset val->locs to avoid infinite recursion. */
1857 val->locs = NULL;
1859 for (l = f; l; l = l->next)
1860 if (GET_CODE (l->loc) == VALUE
1861 && CSELIB_VAL_PTR (l->loc)->locs
1862 && !CSELIB_VAL_PTR (l->loc)->locs->next
1863 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1864 continue;
1865 else if ((ret = find_base_term (l->loc)) != 0)
1866 break;
1868 val->locs = f;
1869 return ret;
1871 case LO_SUM:
1872 /* The standard form is (lo_sum reg sym) so look only at the
1873 second operand. */
1874 return find_base_term (XEXP (x, 1));
1876 case CONST:
1877 x = XEXP (x, 0);
1878 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1879 return 0;
1880 /* Fall through. */
1881 case PLUS:
1882 case MINUS:
1884 rtx tmp1 = XEXP (x, 0);
1885 rtx tmp2 = XEXP (x, 1);
1887 /* This is a little bit tricky since we have to determine which of
1888 the two operands represents the real base address. Otherwise this
1889 routine may return the index register instead of the base register.
1891 That may cause us to believe no aliasing was possible, when in
1892 fact aliasing is possible.
1894 We use a few simple tests to guess the base register. Additional
1895 tests can certainly be added. For example, if one of the operands
1896 is a shift or multiply, then it must be the index register and the
1897 other operand is the base register. */
1899 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1900 return find_base_term (tmp2);
1902 /* If either operand is known to be a pointer, then prefer it
1903 to determine the base term. */
1904 if (REG_P (tmp1) && REG_POINTER (tmp1))
1906 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1907 std::swap (tmp1, tmp2);
1908 /* If second argument is constant which has base term, prefer it
1909 over variable tmp1. See PR64025. */
1910 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
1911 std::swap (tmp1, tmp2);
1913 /* Go ahead and find the base term for both operands. If either base
1914 term is from a pointer or is a named object or a special address
1915 (like an argument or stack reference), then use it for the
1916 base term. */
1917 rtx base = find_base_term (tmp1);
1918 if (base != NULL_RTX
1919 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1920 || known_base_value_p (base)))
1921 return base;
1922 base = find_base_term (tmp2);
1923 if (base != NULL_RTX
1924 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1925 || known_base_value_p (base)))
1926 return base;
1928 /* We could not determine which of the two operands was the
1929 base register and which was the index. So we can determine
1930 nothing from the base alias check. */
1931 return 0;
1934 case AND:
1935 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1936 return find_base_term (XEXP (x, 0));
1937 return 0;
1939 case SYMBOL_REF:
1940 case LABEL_REF:
1941 return x;
1943 default:
1944 return 0;
1948 /* Return true if accesses to address X may alias accesses based
1949 on the stack pointer. */
1951 bool
1952 may_be_sp_based_p (rtx x)
1954 rtx base = find_base_term (x);
1955 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1958 /* Return 0 if the addresses X and Y are known to point to different
1959 objects, 1 if they might be pointers to the same object. */
1961 static int
1962 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1963 machine_mode x_mode, machine_mode y_mode)
1965 /* If the address itself has no known base see if a known equivalent
1966 value has one. If either address still has no known base, nothing
1967 is known about aliasing. */
1968 if (x_base == 0)
1970 rtx x_c;
1972 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1973 return 1;
1975 x_base = find_base_term (x_c);
1976 if (x_base == 0)
1977 return 1;
1980 if (y_base == 0)
1982 rtx y_c;
1983 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1984 return 1;
1986 y_base = find_base_term (y_c);
1987 if (y_base == 0)
1988 return 1;
1991 /* If the base addresses are equal nothing is known about aliasing. */
1992 if (rtx_equal_p (x_base, y_base))
1993 return 1;
1995 /* The base addresses are different expressions. If they are not accessed
1996 via AND, there is no conflict. We can bring knowledge of object
1997 alignment into play here. For example, on alpha, "char a, b;" can
1998 alias one another, though "char a; long b;" cannot. AND addesses may
1999 implicitly alias surrounding objects; i.e. unaligned access in DImode
2000 via AND address can alias all surrounding object types except those
2001 with aligment 8 or higher. */
2002 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2003 return 1;
2004 if (GET_CODE (x) == AND
2005 && (!CONST_INT_P (XEXP (x, 1))
2006 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2007 return 1;
2008 if (GET_CODE (y) == AND
2009 && (!CONST_INT_P (XEXP (y, 1))
2010 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2011 return 1;
2013 /* Differing symbols not accessed via AND never alias. */
2014 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2015 return 0;
2017 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2018 return 0;
2020 return 1;
2023 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2024 that of V. */
2026 static bool
2027 refs_newer_value_p (const_rtx expr, rtx v)
2029 int minuid = CSELIB_VAL_PTR (v)->uid;
2030 subrtx_iterator::array_type array;
2031 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2032 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
2033 return true;
2034 return false;
2037 /* Convert the address X into something we can use. This is done by returning
2038 it unchanged unless it is a value; in the latter case we call cselib to get
2039 a more useful rtx. */
2042 get_addr (rtx x)
2044 cselib_val *v;
2045 struct elt_loc_list *l;
2047 if (GET_CODE (x) != VALUE)
2048 return x;
2049 v = CSELIB_VAL_PTR (x);
2050 if (v)
2052 bool have_equivs = cselib_have_permanent_equivalences ();
2053 if (have_equivs)
2054 v = canonical_cselib_val (v);
2055 for (l = v->locs; l; l = l->next)
2056 if (CONSTANT_P (l->loc))
2057 return l->loc;
2058 for (l = v->locs; l; l = l->next)
2059 if (!REG_P (l->loc) && !MEM_P (l->loc)
2060 /* Avoid infinite recursion when potentially dealing with
2061 var-tracking artificial equivalences, by skipping the
2062 equivalences themselves, and not choosing expressions
2063 that refer to newer VALUEs. */
2064 && (!have_equivs
2065 || (GET_CODE (l->loc) != VALUE
2066 && !refs_newer_value_p (l->loc, x))))
2067 return l->loc;
2068 if (have_equivs)
2070 for (l = v->locs; l; l = l->next)
2071 if (REG_P (l->loc)
2072 || (GET_CODE (l->loc) != VALUE
2073 && !refs_newer_value_p (l->loc, x)))
2074 return l->loc;
2075 /* Return the canonical value. */
2076 return v->val_rtx;
2078 if (v->locs)
2079 return v->locs->loc;
2081 return x;
2084 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2085 where SIZE is the size in bytes of the memory reference. If ADDR
2086 is not modified by the memory reference then ADDR is returned. */
2088 static rtx
2089 addr_side_effect_eval (rtx addr, int size, int n_refs)
2091 int offset = 0;
2093 switch (GET_CODE (addr))
2095 case PRE_INC:
2096 offset = (n_refs + 1) * size;
2097 break;
2098 case PRE_DEC:
2099 offset = -(n_refs + 1) * size;
2100 break;
2101 case POST_INC:
2102 offset = n_refs * size;
2103 break;
2104 case POST_DEC:
2105 offset = -n_refs * size;
2106 break;
2108 default:
2109 return addr;
2112 if (offset)
2113 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
2114 gen_int_mode (offset, GET_MODE (addr)));
2115 else
2116 addr = XEXP (addr, 0);
2117 addr = canon_rtx (addr);
2119 return addr;
2122 /* Return TRUE if an object X sized at XSIZE bytes and another object
2123 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2124 any of the sizes is zero, assume an overlap, otherwise use the
2125 absolute value of the sizes as the actual sizes. */
2127 static inline bool
2128 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
2130 return (xsize == 0 || ysize == 0
2131 || (c >= 0
2132 ? (abs (xsize) > c)
2133 : (abs (ysize) > -c)));
2136 /* Return one if X and Y (memory addresses) reference the
2137 same location in memory or if the references overlap.
2138 Return zero if they do not overlap, else return
2139 minus one in which case they still might reference the same location.
2141 C is an offset accumulator. When
2142 C is nonzero, we are testing aliases between X and Y + C.
2143 XSIZE is the size in bytes of the X reference,
2144 similarly YSIZE is the size in bytes for Y.
2145 Expect that canon_rtx has been already called for X and Y.
2147 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2148 referenced (the reference was BLKmode), so make the most pessimistic
2149 assumptions.
2151 If XSIZE or YSIZE is negative, we may access memory outside the object
2152 being referenced as a side effect. This can happen when using AND to
2153 align memory references, as is done on the Alpha.
2155 Nice to notice that varying addresses cannot conflict with fp if no
2156 local variables had their addresses taken, but that's too hard now.
2158 ??? Contrary to the tree alias oracle this does not return
2159 one for X + non-constant and Y + non-constant when X and Y are equal.
2160 If that is fixed the TBAA hack for union type-punning can be removed. */
2162 static int
2163 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2165 if (GET_CODE (x) == VALUE)
2167 if (REG_P (y))
2169 struct elt_loc_list *l = NULL;
2170 if (CSELIB_VAL_PTR (x))
2171 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2172 l; l = l->next)
2173 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2174 break;
2175 if (l)
2176 x = y;
2177 else
2178 x = get_addr (x);
2180 /* Don't call get_addr if y is the same VALUE. */
2181 else if (x != y)
2182 x = get_addr (x);
2184 if (GET_CODE (y) == VALUE)
2186 if (REG_P (x))
2188 struct elt_loc_list *l = NULL;
2189 if (CSELIB_VAL_PTR (y))
2190 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2191 l; l = l->next)
2192 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2193 break;
2194 if (l)
2195 y = x;
2196 else
2197 y = get_addr (y);
2199 /* Don't call get_addr if x is the same VALUE. */
2200 else if (y != x)
2201 y = get_addr (y);
2203 if (GET_CODE (x) == HIGH)
2204 x = XEXP (x, 0);
2205 else if (GET_CODE (x) == LO_SUM)
2206 x = XEXP (x, 1);
2207 else
2208 x = addr_side_effect_eval (x, abs (xsize), 0);
2209 if (GET_CODE (y) == HIGH)
2210 y = XEXP (y, 0);
2211 else if (GET_CODE (y) == LO_SUM)
2212 y = XEXP (y, 1);
2213 else
2214 y = addr_side_effect_eval (y, abs (ysize), 0);
2216 if (rtx_equal_for_memref_p (x, y))
2218 return offset_overlap_p (c, xsize, ysize);
2221 /* This code used to check for conflicts involving stack references and
2222 globals but the base address alias code now handles these cases. */
2224 if (GET_CODE (x) == PLUS)
2226 /* The fact that X is canonicalized means that this
2227 PLUS rtx is canonicalized. */
2228 rtx x0 = XEXP (x, 0);
2229 rtx x1 = XEXP (x, 1);
2231 /* However, VALUEs might end up in different positions even in
2232 canonical PLUSes. Comparing their addresses is enough. */
2233 if (x0 == y)
2234 return memrefs_conflict_p (xsize, x1, ysize, const0_rtx, c);
2235 else if (x1 == y)
2236 return memrefs_conflict_p (xsize, x0, ysize, const0_rtx, c);
2238 if (GET_CODE (y) == PLUS)
2240 /* The fact that Y is canonicalized means that this
2241 PLUS rtx is canonicalized. */
2242 rtx y0 = XEXP (y, 0);
2243 rtx y1 = XEXP (y, 1);
2245 if (x0 == y1)
2246 return memrefs_conflict_p (xsize, x1, ysize, y0, c);
2247 if (x1 == y0)
2248 return memrefs_conflict_p (xsize, x0, ysize, y1, c);
2250 if (rtx_equal_for_memref_p (x1, y1))
2251 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2252 if (rtx_equal_for_memref_p (x0, y0))
2253 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2254 if (CONST_INT_P (x1))
2256 if (CONST_INT_P (y1))
2257 return memrefs_conflict_p (xsize, x0, ysize, y0,
2258 c - INTVAL (x1) + INTVAL (y1));
2259 else
2260 return memrefs_conflict_p (xsize, x0, ysize, y,
2261 c - INTVAL (x1));
2263 else if (CONST_INT_P (y1))
2264 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2266 return -1;
2268 else if (CONST_INT_P (x1))
2269 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2271 else if (GET_CODE (y) == PLUS)
2273 /* The fact that Y is canonicalized means that this
2274 PLUS rtx is canonicalized. */
2275 rtx y0 = XEXP (y, 0);
2276 rtx y1 = XEXP (y, 1);
2278 if (x == y0)
2279 return memrefs_conflict_p (xsize, const0_rtx, ysize, y1, c);
2280 if (x == y1)
2281 return memrefs_conflict_p (xsize, const0_rtx, ysize, y0, c);
2283 if (CONST_INT_P (y1))
2284 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2285 else
2286 return -1;
2289 if (GET_CODE (x) == GET_CODE (y))
2290 switch (GET_CODE (x))
2292 case MULT:
2294 /* Handle cases where we expect the second operands to be the
2295 same, and check only whether the first operand would conflict
2296 or not. */
2297 rtx x0, y0;
2298 rtx x1 = canon_rtx (XEXP (x, 1));
2299 rtx y1 = canon_rtx (XEXP (y, 1));
2300 if (! rtx_equal_for_memref_p (x1, y1))
2301 return -1;
2302 x0 = canon_rtx (XEXP (x, 0));
2303 y0 = canon_rtx (XEXP (y, 0));
2304 if (rtx_equal_for_memref_p (x0, y0))
2305 return offset_overlap_p (c, xsize, ysize);
2307 /* Can't properly adjust our sizes. */
2308 if (!CONST_INT_P (x1))
2309 return -1;
2310 xsize /= INTVAL (x1);
2311 ysize /= INTVAL (x1);
2312 c /= INTVAL (x1);
2313 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2316 default:
2317 break;
2320 /* Deal with alignment ANDs by adjusting offset and size so as to
2321 cover the maximum range, without taking any previously known
2322 alignment into account. Make a size negative after such an
2323 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2324 assume a potential overlap, because they may end up in contiguous
2325 memory locations and the stricter-alignment access may span over
2326 part of both. */
2327 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2329 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2330 unsigned HOST_WIDE_INT uc = sc;
2331 if (sc < 0 && -uc == (uc & -uc))
2333 if (xsize > 0)
2334 xsize = -xsize;
2335 if (xsize)
2336 xsize += sc + 1;
2337 c -= sc + 1;
2338 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2339 ysize, y, c);
2342 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2344 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2345 unsigned HOST_WIDE_INT uc = sc;
2346 if (sc < 0 && -uc == (uc & -uc))
2348 if (ysize > 0)
2349 ysize = -ysize;
2350 if (ysize)
2351 ysize += sc + 1;
2352 c += sc + 1;
2353 return memrefs_conflict_p (xsize, x,
2354 ysize, canon_rtx (XEXP (y, 0)), c);
2358 if (CONSTANT_P (x))
2360 if (CONST_INT_P (x) && CONST_INT_P (y))
2362 c += (INTVAL (y) - INTVAL (x));
2363 return offset_overlap_p (c, xsize, ysize);
2366 if (GET_CODE (x) == CONST)
2368 if (GET_CODE (y) == CONST)
2369 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2370 ysize, canon_rtx (XEXP (y, 0)), c);
2371 else
2372 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2373 ysize, y, c);
2375 if (GET_CODE (y) == CONST)
2376 return memrefs_conflict_p (xsize, x, ysize,
2377 canon_rtx (XEXP (y, 0)), c);
2379 /* Assume a potential overlap for symbolic addresses that went
2380 through alignment adjustments (i.e., that have negative
2381 sizes), because we can't know how far they are from each
2382 other. */
2383 if (CONSTANT_P (y))
2384 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2386 return -1;
2389 return -1;
2392 /* Functions to compute memory dependencies.
2394 Since we process the insns in execution order, we can build tables
2395 to keep track of what registers are fixed (and not aliased), what registers
2396 are varying in known ways, and what registers are varying in unknown
2397 ways.
2399 If both memory references are volatile, then there must always be a
2400 dependence between the two references, since their order can not be
2401 changed. A volatile and non-volatile reference can be interchanged
2402 though.
2404 We also must allow AND addresses, because they may generate accesses
2405 outside the object being referenced. This is used to generate aligned
2406 addresses from unaligned addresses, for instance, the alpha
2407 storeqi_unaligned pattern. */
2409 /* Read dependence: X is read after read in MEM takes place. There can
2410 only be a dependence here if both reads are volatile, or if either is
2411 an explicit barrier. */
2414 read_dependence (const_rtx mem, const_rtx x)
2416 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2417 return true;
2418 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2419 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2420 return true;
2421 return false;
2424 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2426 static tree
2427 decl_for_component_ref (tree x)
2431 x = TREE_OPERAND (x, 0);
2433 while (x && TREE_CODE (x) == COMPONENT_REF);
2435 return x && DECL_P (x) ? x : NULL_TREE;
2438 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2439 for the offset of the field reference. *KNOWN_P says whether the
2440 offset is known. */
2442 static void
2443 adjust_offset_for_component_ref (tree x, bool *known_p,
2444 HOST_WIDE_INT *offset)
2446 if (!*known_p)
2447 return;
2450 tree xoffset = component_ref_field_offset (x);
2451 tree field = TREE_OPERAND (x, 1);
2452 if (TREE_CODE (xoffset) != INTEGER_CST)
2454 *known_p = false;
2455 return;
2458 offset_int woffset
2459 = (wi::to_offset (xoffset)
2460 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2461 LOG2_BITS_PER_UNIT));
2462 if (!wi::fits_uhwi_p (woffset))
2464 *known_p = false;
2465 return;
2467 *offset += woffset.to_uhwi ();
2469 x = TREE_OPERAND (x, 0);
2471 while (x && TREE_CODE (x) == COMPONENT_REF);
2474 /* Return nonzero if we can determine the exprs corresponding to memrefs
2475 X and Y and they do not overlap.
2476 If LOOP_VARIANT is set, skip offset-based disambiguation */
2479 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2481 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2482 rtx rtlx, rtly;
2483 rtx basex, basey;
2484 bool moffsetx_known_p, moffsety_known_p;
2485 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2486 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey;
2488 /* Unless both have exprs, we can't tell anything. */
2489 if (exprx == 0 || expry == 0)
2490 return 0;
2492 /* For spill-slot accesses make sure we have valid offsets. */
2493 if ((exprx == get_spill_slot_decl (false)
2494 && ! MEM_OFFSET_KNOWN_P (x))
2495 || (expry == get_spill_slot_decl (false)
2496 && ! MEM_OFFSET_KNOWN_P (y)))
2497 return 0;
2499 /* If the field reference test failed, look at the DECLs involved. */
2500 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2501 if (moffsetx_known_p)
2502 moffsetx = MEM_OFFSET (x);
2503 if (TREE_CODE (exprx) == COMPONENT_REF)
2505 tree t = decl_for_component_ref (exprx);
2506 if (! t)
2507 return 0;
2508 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2509 exprx = t;
2512 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2513 if (moffsety_known_p)
2514 moffsety = MEM_OFFSET (y);
2515 if (TREE_CODE (expry) == COMPONENT_REF)
2517 tree t = decl_for_component_ref (expry);
2518 if (! t)
2519 return 0;
2520 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2521 expry = t;
2524 if (! DECL_P (exprx) || ! DECL_P (expry))
2525 return 0;
2527 /* If we refer to different gimple registers, or one gimple register
2528 and one non-gimple-register, we know they can't overlap. First,
2529 gimple registers don't have their addresses taken. Now, there
2530 could be more than one stack slot for (different versions of) the
2531 same gimple register, but we can presumably tell they don't
2532 overlap based on offsets from stack base addresses elsewhere.
2533 It's important that we don't proceed to DECL_RTL, because gimple
2534 registers may not pass DECL_RTL_SET_P, and make_decl_rtl won't be
2535 able to do anything about them since no SSA information will have
2536 remained to guide it. */
2537 if (is_gimple_reg (exprx) || is_gimple_reg (expry))
2538 return exprx != expry
2539 || (moffsetx_known_p && moffsety_known_p
2540 && MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y)
2541 && !offset_overlap_p (moffsety - moffsetx,
2542 MEM_SIZE (x), MEM_SIZE (y)));
2544 /* With invalid code we can end up storing into the constant pool.
2545 Bail out to avoid ICEing when creating RTL for this.
2546 See gfortran.dg/lto/20091028-2_0.f90. */
2547 if (TREE_CODE (exprx) == CONST_DECL
2548 || TREE_CODE (expry) == CONST_DECL)
2549 return 1;
2551 rtlx = DECL_RTL (exprx);
2552 rtly = DECL_RTL (expry);
2554 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2555 can't overlap unless they are the same because we never reuse that part
2556 of the stack frame used for locals for spilled pseudos. */
2557 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2558 && ! rtx_equal_p (rtlx, rtly))
2559 return 1;
2561 /* If we have MEMs referring to different address spaces (which can
2562 potentially overlap), we cannot easily tell from the addresses
2563 whether the references overlap. */
2564 if (MEM_P (rtlx) && MEM_P (rtly)
2565 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2566 return 0;
2568 /* Get the base and offsets of both decls. If either is a register, we
2569 know both are and are the same, so use that as the base. The only
2570 we can avoid overlap is if we can deduce that they are nonoverlapping
2571 pieces of that decl, which is very rare. */
2572 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2573 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2574 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2576 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2577 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2578 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2580 /* If the bases are different, we know they do not overlap if both
2581 are constants or if one is a constant and the other a pointer into the
2582 stack frame. Otherwise a different base means we can't tell if they
2583 overlap or not. */
2584 if (! rtx_equal_p (basex, basey))
2585 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2586 || (CONSTANT_P (basex) && REG_P (basey)
2587 && REGNO_PTR_FRAME_P (REGNO (basey)))
2588 || (CONSTANT_P (basey) && REG_P (basex)
2589 && REGNO_PTR_FRAME_P (REGNO (basex))));
2591 /* Offset based disambiguation not appropriate for loop invariant */
2592 if (loop_invariant)
2593 return 0;
2595 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2596 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2597 : -1);
2598 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2599 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2600 : -1);
2602 /* If we have an offset for either memref, it can update the values computed
2603 above. */
2604 if (moffsetx_known_p)
2605 offsetx += moffsetx, sizex -= moffsetx;
2606 if (moffsety_known_p)
2607 offsety += moffsety, sizey -= moffsety;
2609 /* If a memref has both a size and an offset, we can use the smaller size.
2610 We can't do this if the offset isn't known because we must view this
2611 memref as being anywhere inside the DECL's MEM. */
2612 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2613 sizex = MEM_SIZE (x);
2614 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2615 sizey = MEM_SIZE (y);
2617 /* Put the values of the memref with the lower offset in X's values. */
2618 if (offsetx > offsety)
2620 std::swap (offsetx, offsety);
2621 std::swap (sizex, sizey);
2624 /* If we don't know the size of the lower-offset value, we can't tell
2625 if they conflict. Otherwise, we do the test. */
2626 return sizex >= 0 && offsety >= offsetx + sizex;
2629 /* Helper for true_dependence and canon_true_dependence.
2630 Checks for true dependence: X is read after store in MEM takes place.
2632 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2633 NULL_RTX, and the canonical addresses of MEM and X are both computed
2634 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2636 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2638 Returns 1 if there is a true dependence, 0 otherwise. */
2640 static int
2641 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2642 const_rtx x, rtx x_addr, bool mem_canonicalized)
2644 rtx true_mem_addr;
2645 rtx base;
2646 int ret;
2648 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2649 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2651 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2652 return 1;
2654 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2655 This is used in epilogue deallocation functions, and in cselib. */
2656 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2657 return 1;
2658 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2659 return 1;
2660 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2661 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2662 return 1;
2664 if (! x_addr)
2665 x_addr = XEXP (x, 0);
2666 x_addr = get_addr (x_addr);
2668 if (! mem_addr)
2670 mem_addr = XEXP (mem, 0);
2671 if (mem_mode == VOIDmode)
2672 mem_mode = GET_MODE (mem);
2674 true_mem_addr = get_addr (mem_addr);
2676 /* Read-only memory is by definition never modified, and therefore can't
2677 conflict with anything. However, don't assume anything when AND
2678 addresses are involved and leave to the code below to determine
2679 dependence. We don't expect to find read-only set on MEM, but
2680 stupid user tricks can produce them, so don't die. */
2681 if (MEM_READONLY_P (x)
2682 && GET_CODE (x_addr) != AND
2683 && GET_CODE (true_mem_addr) != AND)
2684 return 0;
2686 /* If we have MEMs referring to different address spaces (which can
2687 potentially overlap), we cannot easily tell from the addresses
2688 whether the references overlap. */
2689 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2690 return 1;
2692 base = find_base_term (x_addr);
2693 if (base && (GET_CODE (base) == LABEL_REF
2694 || (GET_CODE (base) == SYMBOL_REF
2695 && CONSTANT_POOL_ADDRESS_P (base))))
2696 return 0;
2698 rtx mem_base = find_base_term (true_mem_addr);
2699 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2700 GET_MODE (x), mem_mode))
2701 return 0;
2703 x_addr = canon_rtx (x_addr);
2704 if (!mem_canonicalized)
2705 mem_addr = canon_rtx (true_mem_addr);
2707 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2708 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2709 return ret;
2711 if (mems_in_disjoint_alias_sets_p (x, mem))
2712 return 0;
2714 if (nonoverlapping_memrefs_p (mem, x, false))
2715 return 0;
2717 return rtx_refs_may_alias_p (x, mem, true);
2720 /* True dependence: X is read after store in MEM takes place. */
2723 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2725 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2726 x, NULL_RTX, /*mem_canonicalized=*/false);
2729 /* Canonical true dependence: X is read after store in MEM takes place.
2730 Variant of true_dependence which assumes MEM has already been
2731 canonicalized (hence we no longer do that here).
2732 The mem_addr argument has been added, since true_dependence_1 computed
2733 this value prior to canonicalizing. */
2736 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2737 const_rtx x, rtx x_addr)
2739 return true_dependence_1 (mem, mem_mode, mem_addr,
2740 x, x_addr, /*mem_canonicalized=*/true);
2743 /* Returns nonzero if a write to X might alias a previous read from
2744 (or, if WRITEP is true, a write to) MEM.
2745 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2746 and X_MODE the mode for that access.
2747 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2749 static int
2750 write_dependence_p (const_rtx mem,
2751 const_rtx x, machine_mode x_mode, rtx x_addr,
2752 bool mem_canonicalized, bool x_canonicalized, bool writep)
2754 rtx mem_addr;
2755 rtx true_mem_addr, true_x_addr;
2756 rtx base;
2757 int ret;
2759 gcc_checking_assert (x_canonicalized
2760 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2761 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2763 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2764 return 1;
2766 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2767 This is used in epilogue deallocation functions. */
2768 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2769 return 1;
2770 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2771 return 1;
2772 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2773 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2774 return 1;
2776 if (!x_addr)
2777 x_addr = XEXP (x, 0);
2778 true_x_addr = get_addr (x_addr);
2780 mem_addr = XEXP (mem, 0);
2781 true_mem_addr = get_addr (mem_addr);
2783 /* A read from read-only memory can't conflict with read-write memory.
2784 Don't assume anything when AND addresses are involved and leave to
2785 the code below to determine dependence. */
2786 if (!writep
2787 && MEM_READONLY_P (mem)
2788 && GET_CODE (true_x_addr) != AND
2789 && GET_CODE (true_mem_addr) != AND)
2790 return 0;
2792 /* If we have MEMs referring to different address spaces (which can
2793 potentially overlap), we cannot easily tell from the addresses
2794 whether the references overlap. */
2795 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2796 return 1;
2798 base = find_base_term (true_mem_addr);
2799 if (! writep
2800 && base
2801 && (GET_CODE (base) == LABEL_REF
2802 || (GET_CODE (base) == SYMBOL_REF
2803 && CONSTANT_POOL_ADDRESS_P (base))))
2804 return 0;
2806 rtx x_base = find_base_term (true_x_addr);
2807 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2808 GET_MODE (x), GET_MODE (mem)))
2809 return 0;
2811 if (!x_canonicalized)
2813 x_addr = canon_rtx (true_x_addr);
2814 x_mode = GET_MODE (x);
2816 if (!mem_canonicalized)
2817 mem_addr = canon_rtx (true_mem_addr);
2819 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2820 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2821 return ret;
2823 if (nonoverlapping_memrefs_p (x, mem, false))
2824 return 0;
2826 return rtx_refs_may_alias_p (x, mem, false);
2829 /* Anti dependence: X is written after read in MEM takes place. */
2832 anti_dependence (const_rtx mem, const_rtx x)
2834 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2835 /*mem_canonicalized=*/false,
2836 /*x_canonicalized*/false, /*writep=*/false);
2839 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2840 Also, consider X in X_MODE (which might be from an enclosing
2841 STRICT_LOW_PART / ZERO_EXTRACT).
2842 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2845 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2846 const_rtx x, machine_mode x_mode, rtx x_addr)
2848 return write_dependence_p (mem, x, x_mode, x_addr,
2849 mem_canonicalized, /*x_canonicalized=*/true,
2850 /*writep=*/false);
2853 /* Output dependence: X is written after store in MEM takes place. */
2856 output_dependence (const_rtx mem, const_rtx x)
2858 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2859 /*mem_canonicalized=*/false,
2860 /*x_canonicalized*/false, /*writep=*/true);
2865 /* Check whether X may be aliased with MEM. Don't do offset-based
2866 memory disambiguation & TBAA. */
2868 may_alias_p (const_rtx mem, const_rtx x)
2870 rtx x_addr, mem_addr;
2872 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2873 return 1;
2875 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2876 This is used in epilogue deallocation functions. */
2877 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2878 return 1;
2879 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2880 return 1;
2881 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2882 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2883 return 1;
2885 x_addr = XEXP (x, 0);
2886 x_addr = get_addr (x_addr);
2888 mem_addr = XEXP (mem, 0);
2889 mem_addr = get_addr (mem_addr);
2891 /* Read-only memory is by definition never modified, and therefore can't
2892 conflict with anything. However, don't assume anything when AND
2893 addresses are involved and leave to the code below to determine
2894 dependence. We don't expect to find read-only set on MEM, but
2895 stupid user tricks can produce them, so don't die. */
2896 if (MEM_READONLY_P (x)
2897 && GET_CODE (x_addr) != AND
2898 && GET_CODE (mem_addr) != AND)
2899 return 0;
2901 /* If we have MEMs referring to different address spaces (which can
2902 potentially overlap), we cannot easily tell from the addresses
2903 whether the references overlap. */
2904 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2905 return 1;
2907 rtx x_base = find_base_term (x_addr);
2908 rtx mem_base = find_base_term (mem_addr);
2909 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2910 GET_MODE (x), GET_MODE (mem_addr)))
2911 return 0;
2913 if (nonoverlapping_memrefs_p (mem, x, true))
2914 return 0;
2916 /* TBAA not valid for loop_invarint */
2917 return rtx_refs_may_alias_p (x, mem, false);
2920 void
2921 init_alias_target (void)
2923 int i;
2925 if (!arg_base_value)
2926 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2928 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2930 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2931 /* Check whether this register can hold an incoming pointer
2932 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2933 numbers, so translate if necessary due to register windows. */
2934 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2935 && HARD_REGNO_MODE_OK (i, Pmode))
2936 static_reg_base_value[i] = arg_base_value;
2938 static_reg_base_value[STACK_POINTER_REGNUM]
2939 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2940 static_reg_base_value[ARG_POINTER_REGNUM]
2941 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2942 static_reg_base_value[FRAME_POINTER_REGNUM]
2943 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2944 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2945 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2946 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2949 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2950 to be memory reference. */
2951 static bool memory_modified;
2952 static void
2953 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2955 if (MEM_P (x))
2957 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2958 memory_modified = true;
2963 /* Return true when INSN possibly modify memory contents of MEM
2964 (i.e. address can be modified). */
2965 bool
2966 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2968 if (!INSN_P (insn))
2969 return false;
2970 memory_modified = false;
2971 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2972 return memory_modified;
2975 /* Return TRUE if the destination of a set is rtx identical to
2976 ITEM. */
2977 static inline bool
2978 set_dest_equal_p (const_rtx set, const_rtx item)
2980 rtx dest = SET_DEST (set);
2981 return rtx_equal_p (dest, item);
2984 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2985 *DEFINITELY* modify the memory contents of MEM. */
2986 bool
2987 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2989 if (!INSN_P (insn))
2990 return false;
2991 insn = PATTERN (insn);
2992 if (GET_CODE (insn) == SET)
2993 return set_dest_equal_p (insn, mem);
2994 else if (GET_CODE (insn) == PARALLEL)
2996 int i;
2997 for (i = 0; i < XVECLEN (insn, 0); i++)
2999 rtx sub = XVECEXP (insn, 0, i);
3000 if (GET_CODE (sub) == SET
3001 && set_dest_equal_p (sub, mem))
3002 return true;
3005 return false;
3008 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3009 array. */
3011 void
3012 init_alias_analysis (void)
3014 unsigned int maxreg = max_reg_num ();
3015 int changed, pass;
3016 int i;
3017 unsigned int ui;
3018 rtx_insn *insn;
3019 rtx val;
3020 int rpo_cnt;
3021 int *rpo;
3023 timevar_push (TV_ALIAS_ANALYSIS);
3025 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
3026 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3027 bitmap_clear (reg_known_equiv_p);
3029 /* If we have memory allocated from the previous run, use it. */
3030 if (old_reg_base_value)
3031 reg_base_value = old_reg_base_value;
3033 if (reg_base_value)
3034 reg_base_value->truncate (0);
3036 vec_safe_grow_cleared (reg_base_value, maxreg);
3038 new_reg_base_value = XNEWVEC (rtx, maxreg);
3039 reg_seen = sbitmap_alloc (maxreg);
3041 /* The basic idea is that each pass through this loop will use the
3042 "constant" information from the previous pass to propagate alias
3043 information through another level of assignments.
3045 The propagation is done on the CFG in reverse post-order, to propagate
3046 things forward as far as possible in each iteration.
3048 This could get expensive if the assignment chains are long. Maybe
3049 we should throttle the number of iterations, possibly based on
3050 the optimization level or flag_expensive_optimizations.
3052 We could propagate more information in the first pass by making use
3053 of DF_REG_DEF_COUNT to determine immediately that the alias information
3054 for a pseudo is "constant".
3056 A program with an uninitialized variable can cause an infinite loop
3057 here. Instead of doing a full dataflow analysis to detect such problems
3058 we just cap the number of iterations for the loop.
3060 The state of the arrays for the set chain in question does not matter
3061 since the program has undefined behavior. */
3063 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3064 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3066 /* The prologue/epilogue insns are not threaded onto the
3067 insn chain until after reload has completed. Thus,
3068 there is no sense wasting time checking if INSN is in
3069 the prologue/epilogue until after reload has completed. */
3070 bool could_be_prologue_epilogue = ((targetm.have_prologue ()
3071 || targetm.have_epilogue ())
3072 && reload_completed);
3074 pass = 0;
3077 /* Assume nothing will change this iteration of the loop. */
3078 changed = 0;
3080 /* We want to assign the same IDs each iteration of this loop, so
3081 start counting from one each iteration of the loop. */
3082 unique_id = 1;
3084 /* We're at the start of the function each iteration through the
3085 loop, so we're copying arguments. */
3086 copying_arguments = true;
3088 /* Wipe the potential alias information clean for this pass. */
3089 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3091 /* Wipe the reg_seen array clean. */
3092 bitmap_clear (reg_seen);
3094 /* Initialize the alias information for this pass. */
3095 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3096 if (static_reg_base_value[i])
3098 new_reg_base_value[i] = static_reg_base_value[i];
3099 bitmap_set_bit (reg_seen, i);
3102 /* Walk the insns adding values to the new_reg_base_value array. */
3103 for (i = 0; i < rpo_cnt; i++)
3105 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3106 FOR_BB_INSNS (bb, insn)
3108 if (NONDEBUG_INSN_P (insn))
3110 rtx note, set;
3112 if (could_be_prologue_epilogue
3113 && prologue_epilogue_contains (insn))
3114 continue;
3116 /* If this insn has a noalias note, process it, Otherwise,
3117 scan for sets. A simple set will have no side effects
3118 which could change the base value of any other register. */
3120 if (GET_CODE (PATTERN (insn)) == SET
3121 && REG_NOTES (insn) != 0
3122 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3123 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3124 else
3125 note_stores (PATTERN (insn), record_set, NULL);
3127 set = single_set (insn);
3129 if (set != 0
3130 && REG_P (SET_DEST (set))
3131 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3133 unsigned int regno = REGNO (SET_DEST (set));
3134 rtx src = SET_SRC (set);
3135 rtx t;
3137 note = find_reg_equal_equiv_note (insn);
3138 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3139 && DF_REG_DEF_COUNT (regno) != 1)
3140 note = NULL_RTX;
3142 if (note != NULL_RTX
3143 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3144 && ! rtx_varies_p (XEXP (note, 0), 1)
3145 && ! reg_overlap_mentioned_p (SET_DEST (set),
3146 XEXP (note, 0)))
3148 set_reg_known_value (regno, XEXP (note, 0));
3149 set_reg_known_equiv_p (regno,
3150 REG_NOTE_KIND (note) == REG_EQUIV);
3152 else if (DF_REG_DEF_COUNT (regno) == 1
3153 && GET_CODE (src) == PLUS
3154 && REG_P (XEXP (src, 0))
3155 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3156 && CONST_INT_P (XEXP (src, 1)))
3158 t = plus_constant (GET_MODE (src), t,
3159 INTVAL (XEXP (src, 1)));
3160 set_reg_known_value (regno, t);
3161 set_reg_known_equiv_p (regno, false);
3163 else if (DF_REG_DEF_COUNT (regno) == 1
3164 && ! rtx_varies_p (src, 1))
3166 set_reg_known_value (regno, src);
3167 set_reg_known_equiv_p (regno, false);
3171 else if (NOTE_P (insn)
3172 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3173 copying_arguments = false;
3177 /* Now propagate values from new_reg_base_value to reg_base_value. */
3178 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3180 for (ui = 0; ui < maxreg; ui++)
3182 if (new_reg_base_value[ui]
3183 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3184 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3186 (*reg_base_value)[ui] = new_reg_base_value[ui];
3187 changed = 1;
3191 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3192 XDELETEVEC (rpo);
3194 /* Fill in the remaining entries. */
3195 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3197 int regno = i + FIRST_PSEUDO_REGISTER;
3198 if (! val)
3199 set_reg_known_value (regno, regno_reg_rtx[regno]);
3202 /* Clean up. */
3203 free (new_reg_base_value);
3204 new_reg_base_value = 0;
3205 sbitmap_free (reg_seen);
3206 reg_seen = 0;
3207 timevar_pop (TV_ALIAS_ANALYSIS);
3210 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3211 Special API for var-tracking pass purposes. */
3213 void
3214 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3216 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3219 void
3220 end_alias_analysis (void)
3222 old_reg_base_value = reg_base_value;
3223 vec_free (reg_known_value);
3224 sbitmap_free (reg_known_equiv_p);
3227 void
3228 dump_alias_stats_in_alias_c (FILE *s)
3230 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3231 " %llu are in alias set 0\n"
3232 " %llu queries asked about the same object\n"
3233 " %llu queries asked about the same alias set\n"
3234 " %llu access volatile\n"
3235 " %llu are dependent in the DAG\n"
3236 " %llu are aritificially in conflict with void *\n",
3237 alias_stats.num_disambiguated,
3238 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3239 + alias_stats.num_same_objects + alias_stats.num_volatile
3240 + alias_stats.num_dag + alias_stats.num_disambiguated
3241 + alias_stats.num_universal,
3242 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3243 alias_stats.num_same_objects, alias_stats.num_volatile,
3244 alias_stats.num_dag, alias_stats.num_universal);
3246 #include "gt-alias.h"