Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / alias.c
blobbc6a0eef0b8a5cd6b701b4b0f1926bf936647990
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 "tm.h"
25 #include "rtl.h"
26 #include "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "varasm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "flags.h"
35 #include "insn-config.h"
36 #include "expmed.h"
37 #include "dojump.h"
38 #include "explow.h"
39 #include "calls.h"
40 #include "emit-rtl.h"
41 #include "stmt.h"
42 #include "expr.h"
43 #include "tm_p.h"
44 #include "regs.h"
45 #include "diagnostic-core.h"
46 #include "alloc-pool.h"
47 #include "cselib.h"
48 #include "langhooks.h"
49 #include "timevar.h"
50 #include "dumpfile.h"
51 #include "target.h"
52 #include "dominance.h"
53 #include "cfg.h"
54 #include "cfganal.h"
55 #include "predict.h"
56 #include "basic-block.h"
57 #include "df.h"
58 #include "tree-ssa-alias.h"
59 #include "internal-fn.h"
60 #include "gimple-expr.h"
61 #include "is-a.h"
62 #include "gimple.h"
63 #include "gimple-ssa.h"
64 #include "rtl-iter.h"
66 /* The aliasing API provided here solves related but different problems:
68 Say there exists (in c)
70 struct X {
71 struct Y y1;
72 struct Z z2;
73 } x1, *px1, *px2;
75 struct Y y2, *py;
76 struct Z z2, *pz;
79 py = &x1.y1;
80 px2 = &x1;
82 Consider the four questions:
84 Can a store to x1 interfere with px2->y1?
85 Can a store to x1 interfere with px2->z2?
86 Can a store to x1 change the value pointed to by with py?
87 Can a store to x1 change the value pointed to by with pz?
89 The answer to these questions can be yes, yes, yes, and maybe.
91 The first two questions can be answered with a simple examination
92 of the type system. If structure X contains a field of type Y then
93 a store through a pointer to an X can overwrite any field that is
94 contained (recursively) in an X (unless we know that px1 != px2).
96 The last two questions can be solved in the same way as the first
97 two questions but this is too conservative. The observation is
98 that in some cases we can know which (if any) fields are addressed
99 and if those addresses are used in bad ways. This analysis may be
100 language specific. In C, arbitrary operations may be applied to
101 pointers. However, there is some indication that this may be too
102 conservative for some C++ types.
104 The pass ipa-type-escape does this analysis for the types whose
105 instances do not escape across the compilation boundary.
107 Historically in GCC, these two problems were combined and a single
108 data structure that was used to represent the solution to these
109 problems. We now have two similar but different data structures,
110 The data structure to solve the last two questions is similar to
111 the first, but does not contain the fields whose address are never
112 taken. For types that do escape the compilation unit, the data
113 structures will have identical information.
116 /* The alias sets assigned to MEMs assist the back-end in determining
117 which MEMs can alias which other MEMs. In general, two MEMs in
118 different alias sets cannot alias each other, with one important
119 exception. Consider something like:
121 struct S { int i; double d; };
123 a store to an `S' can alias something of either type `int' or type
124 `double'. (However, a store to an `int' cannot alias a `double'
125 and vice versa.) We indicate this via a tree structure that looks
126 like:
127 struct S
130 |/_ _\|
131 int double
133 (The arrows are directed and point downwards.)
134 In this situation we say the alias set for `struct S' is the
135 `superset' and that those for `int' and `double' are `subsets'.
137 To see whether two alias sets can point to the same memory, we must
138 see if either alias set is a subset of the other. We need not trace
139 past immediate descendants, however, since we propagate all
140 grandchildren up one level.
142 Alias set zero is implicitly a superset of all other alias sets.
143 However, this is no actual entry for alias set zero. It is an
144 error to attempt to explicitly construct a subset of zero. */
146 struct alias_set_traits : default_hashmap_traits
148 template<typename T>
149 static bool
150 is_empty (T &e)
152 return e.m_key == INT_MIN;
155 template<typename T>
156 static bool
157 is_deleted (T &e)
159 return e.m_key == (INT_MIN + 1);
162 template<typename T> static void mark_empty (T &e) { e.m_key = INT_MIN; }
164 template<typename T>
165 static void
166 mark_deleted (T &e)
168 e.m_key = INT_MIN + 1;
172 struct GTY(()) alias_set_entry_d {
173 /* The alias set number, as stored in MEM_ALIAS_SET. */
174 alias_set_type alias_set;
176 /* The children of the alias set. These are not just the immediate
177 children, but, in fact, all descendants. So, if we have:
179 struct T { struct S s; float f; }
181 continuing our example above, the children here will be all of
182 `int', `double', `float', and `struct S'. */
183 hash_map<int, int, alias_set_traits> *children;
185 /* Nonzero if would have a child of zero: this effectively makes this
186 alias set the same as alias set zero. */
187 bool has_zero_child;
188 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
189 aggregate contaiing pointer.
190 This is used for a special case where we need an universal pointer type
191 compatible with all other pointer types. */
192 bool is_pointer;
193 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
194 bool has_pointer;
196 typedef struct alias_set_entry_d *alias_set_entry;
198 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
199 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
200 static void record_set (rtx, const_rtx, void *);
201 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
202 machine_mode);
203 static rtx find_base_value (rtx);
204 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
205 static alias_set_entry get_alias_set_entry (alias_set_type);
206 static tree decl_for_component_ref (tree);
207 static int write_dependence_p (const_rtx,
208 const_rtx, machine_mode, rtx,
209 bool, bool, bool);
211 static void memory_modified_1 (rtx, const_rtx, void *);
213 /* Query statistics for the different low-level disambiguators.
214 A high-level query may trigger multiple of them. */
216 static struct {
217 unsigned long long num_alias_zero;
218 unsigned long long num_same_alias_set;
219 unsigned long long num_same_objects;
220 unsigned long long num_volatile;
221 unsigned long long num_dag;
222 unsigned long long num_universal;
223 unsigned long long num_disambiguated;
224 } alias_stats;
227 /* Set up all info needed to perform alias analysis on memory references. */
229 /* Returns the size in bytes of the mode of X. */
230 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
232 /* Cap the number of passes we make over the insns propagating alias
233 information through set chains.
234 ??? 10 is a completely arbitrary choice. This should be based on the
235 maximum loop depth in the CFG, but we do not have this information
236 available (even if current_loops _is_ available). */
237 #define MAX_ALIAS_LOOP_PASSES 10
239 /* reg_base_value[N] gives an address to which register N is related.
240 If all sets after the first add or subtract to the current value
241 or otherwise modify it so it does not point to a different top level
242 object, reg_base_value[N] is equal to the address part of the source
243 of the first set.
245 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
246 expressions represent three types of base:
248 1. incoming arguments. There is just one ADDRESS to represent all
249 arguments, since we do not know at this level whether accesses
250 based on different arguments can alias. The ADDRESS has id 0.
252 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
253 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
254 Each of these rtxes has a separate ADDRESS associated with it,
255 each with a negative id.
257 GCC is (and is required to be) precise in which register it
258 chooses to access a particular region of stack. We can therefore
259 assume that accesses based on one of these rtxes do not alias
260 accesses based on another of these rtxes.
262 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
263 Each such piece of memory has a separate ADDRESS associated
264 with it, each with an id greater than 0.
266 Accesses based on one ADDRESS do not alias accesses based on other
267 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
268 alias globals either; the ADDRESSes have Pmode to indicate this.
269 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
270 indicate this. */
272 static GTY(()) vec<rtx, va_gc> *reg_base_value;
273 static rtx *new_reg_base_value;
275 /* The single VOIDmode ADDRESS that represents all argument bases.
276 It has id 0. */
277 static GTY(()) rtx arg_base_value;
279 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
280 static int unique_id;
282 /* We preserve the copy of old array around to avoid amount of garbage
283 produced. About 8% of garbage produced were attributed to this
284 array. */
285 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
287 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
288 registers. */
289 #define UNIQUE_BASE_VALUE_SP -1
290 #define UNIQUE_BASE_VALUE_ARGP -2
291 #define UNIQUE_BASE_VALUE_FP -3
292 #define UNIQUE_BASE_VALUE_HFP -4
294 #define static_reg_base_value \
295 (this_target_rtl->x_static_reg_base_value)
297 #define REG_BASE_VALUE(X) \
298 (REGNO (X) < vec_safe_length (reg_base_value) \
299 ? (*reg_base_value)[REGNO (X)] : 0)
301 /* Vector indexed by N giving the initial (unchanging) value known for
302 pseudo-register N. This vector is initialized in init_alias_analysis,
303 and does not change until end_alias_analysis is called. */
304 static GTY(()) vec<rtx, va_gc> *reg_known_value;
306 /* Vector recording for each reg_known_value whether it is due to a
307 REG_EQUIV note. Future passes (viz., reload) may replace the
308 pseudo with the equivalent expression and so we account for the
309 dependences that would be introduced if that happens.
311 The REG_EQUIV notes created in assign_parms may mention the arg
312 pointer, and there are explicit insns in the RTL that modify the
313 arg pointer. Thus we must ensure that such insns don't get
314 scheduled across each other because that would invalidate the
315 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
316 wrong, but solving the problem in the scheduler will likely give
317 better code, so we do it here. */
318 static sbitmap reg_known_equiv_p;
320 /* True when scanning insns from the start of the rtl to the
321 NOTE_INSN_FUNCTION_BEG note. */
322 static bool copying_arguments;
325 /* The splay-tree used to store the various alias set entries. */
326 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
328 /* Build a decomposed reference object for querying the alias-oracle
329 from the MEM rtx and store it in *REF.
330 Returns false if MEM is not suitable for the alias-oracle. */
332 static bool
333 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
335 tree expr = MEM_EXPR (mem);
336 tree base;
338 if (!expr)
339 return false;
341 ao_ref_init (ref, expr);
343 /* Get the base of the reference and see if we have to reject or
344 adjust it. */
345 base = ao_ref_base (ref);
346 if (base == NULL_TREE)
347 return false;
349 /* The tree oracle doesn't like bases that are neither decls
350 nor indirect references of SSA names. */
351 if (!(DECL_P (base)
352 || (TREE_CODE (base) == MEM_REF
353 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
354 || (TREE_CODE (base) == TARGET_MEM_REF
355 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
356 return false;
358 /* If this is a reference based on a partitioned decl replace the
359 base with a MEM_REF of the pointer representative we
360 created during stack slot partitioning. */
361 if (TREE_CODE (base) == VAR_DECL
362 && ! is_global_var (base)
363 && cfun->gimple_df->decls_to_pointers != NULL)
365 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
366 if (namep)
367 ref->base = build_simple_mem_ref (*namep);
370 ref->ref_alias_set = MEM_ALIAS_SET (mem);
372 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
373 is conservative, so trust it. */
374 if (!MEM_OFFSET_KNOWN_P (mem)
375 || !MEM_SIZE_KNOWN_P (mem))
376 return true;
378 /* If the base decl is a parameter we can have negative MEM_OFFSET in
379 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
380 here. */
381 if (MEM_OFFSET (mem) < 0
382 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
383 return true;
385 /* Otherwise continue and refine size and offset we got from analyzing
386 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
388 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
389 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
391 /* The MEM may extend into adjacent fields, so adjust max_size if
392 necessary. */
393 if (ref->max_size != -1
394 && ref->size > ref->max_size)
395 ref->max_size = ref->size;
397 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
398 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
399 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
400 && (ref->offset < 0
401 || (DECL_P (ref->base)
402 && (DECL_SIZE (ref->base) == NULL_TREE
403 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
404 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
405 ref->offset + ref->size)))))
406 return false;
408 return true;
411 /* Query the alias-oracle on whether the two memory rtx X and MEM may
412 alias. If TBAA_P is set also apply TBAA. Returns true if the
413 two rtxen may alias, false otherwise. */
415 static bool
416 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
418 ao_ref ref1, ref2;
420 if (!ao_ref_from_mem (&ref1, x)
421 || !ao_ref_from_mem (&ref2, mem))
422 return true;
424 return refs_may_alias_p_1 (&ref1, &ref2,
425 tbaa_p
426 && MEM_ALIAS_SET (x) != 0
427 && MEM_ALIAS_SET (mem) != 0);
430 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
431 such an entry, or NULL otherwise. */
433 static inline alias_set_entry
434 get_alias_set_entry (alias_set_type alias_set)
436 return (*alias_sets)[alias_set];
439 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
440 the two MEMs cannot alias each other. */
442 static inline int
443 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
445 return (flag_strict_aliasing
446 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
447 MEM_ALIAS_SET (mem2)));
450 /* Return true if the first alias set is a subset of the second. */
452 bool
453 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
455 alias_set_entry ase2;
457 /* Everything is a subset of the "aliases everything" set. */
458 if (set2 == 0)
459 return true;
461 /* Check if set1 is a subset of set2. */
462 ase2 = get_alias_set_entry (set2);
463 if (ase2 != 0
464 && (ase2->has_zero_child
465 || (ase2->children && ase2->children->get (set1))))
466 return true;
468 /* As a special case we consider alias set of "void *" to be both subset
469 and superset of every alias set of a pointer. This extra symmetry does
470 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
471 to return true on the following testcase:
473 void *ptr;
474 char **ptr2=(char **)&ptr;
475 *ptr2 = ...
477 Additionally if a set contains universal pointer, we consider every pointer
478 to be a subset of it, but we do not represent this explicitely - doing so
479 would require us to update transitive closure each time we introduce new
480 pointer type. This makes aliasing_component_refs_p to return true
481 on the following testcase:
483 struct a {void *ptr;}
484 char **ptr = (char **)&a.ptr;
485 ptr = ...
487 This makes void * truly universal pointer type. See pointer handling in
488 get_alias_set for more details. */
489 if (ase2 && ase2->has_pointer)
491 alias_set_entry ase1 = get_alias_set_entry (set1);
493 if (ase1 && ase1->is_pointer)
495 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
496 /* If one is ptr_type_node and other is pointer, then we consider
497 them subset of each other. */
498 if (set1 == voidptr_set || set2 == voidptr_set)
499 return true;
500 /* If SET2 contains universal pointer's alias set, then we consdier
501 every (non-universal) pointer. */
502 if (ase2->children && set1 != voidptr_set
503 && ase2->children->get (voidptr_set))
504 return true;
507 return false;
510 /* Return 1 if the two specified alias sets may conflict. */
513 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
515 alias_set_entry ase1;
516 alias_set_entry ase2;
518 /* The easy case. */
519 if (alias_sets_must_conflict_p (set1, set2))
520 return 1;
522 /* See if the first alias set is a subset of the second. */
523 ase1 = get_alias_set_entry (set1);
524 if (ase1 != 0
525 && ase1->children && ase1->children->get (set2))
527 ++alias_stats.num_dag;
528 return 1;
531 /* Now do the same, but with the alias sets reversed. */
532 ase2 = get_alias_set_entry (set2);
533 if (ase2 != 0
534 && ase2->children && ase2->children->get (set1))
536 ++alias_stats.num_dag;
537 return 1;
540 /* We want void * to be compatible with any other pointer without
541 really dropping it to alias set 0. Doing so would make it
542 compatible with all non-pointer types too.
544 This is not strictly necessary by the C/C++ language
545 standards, but avoids common type punning mistakes. In
546 addition to that, we need the existence of such universal
547 pointer to implement Fortran's C_PTR type (which is defined as
548 type compatible with all C pointers). */
549 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
551 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
553 /* If one of the sets corresponds to universal pointer,
554 we consider it to conflict with anything that is
555 or contains pointer. */
556 if (set1 == voidptr_set || set2 == voidptr_set)
558 ++alias_stats.num_universal;
559 return true;
561 /* If one of sets is (non-universal) pointer and the other
562 contains universal pointer, we also get conflict. */
563 if (ase1->is_pointer && set2 != voidptr_set
564 && ase2->children && ase2->children->get (voidptr_set))
566 ++alias_stats.num_universal;
567 return true;
569 if (ase2->is_pointer && set1 != voidptr_set
570 && ase1->children && ase1->children->get (voidptr_set))
572 ++alias_stats.num_universal;
573 return true;
577 ++alias_stats.num_disambiguated;
579 /* The two alias sets are distinct and neither one is the
580 child of the other. Therefore, they cannot conflict. */
581 return 0;
584 /* Return 1 if the two specified alias sets will always conflict. */
587 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
589 if (set1 == 0 || set2 == 0)
591 ++alias_stats.num_alias_zero;
592 return 1;
594 if (set1 == set2)
596 ++alias_stats.num_same_alias_set;
597 return 1;
600 return 0;
603 /* Return 1 if any MEM object of type T1 will always conflict (using the
604 dependency routines in this file) with any MEM object of type T2.
605 This is used when allocating temporary storage. If T1 and/or T2 are
606 NULL_TREE, it means we know nothing about the storage. */
609 objects_must_conflict_p (tree t1, tree t2)
611 alias_set_type set1, set2;
613 /* If neither has a type specified, we don't know if they'll conflict
614 because we may be using them to store objects of various types, for
615 example the argument and local variables areas of inlined functions. */
616 if (t1 == 0 && t2 == 0)
617 return 0;
619 /* If they are the same type, they must conflict. */
620 if (t1 == t2)
622 ++alias_stats.num_same_objects;
623 return 1;
625 /* Likewise if both are volatile. */
626 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
628 ++alias_stats.num_volatile;
629 return 1;
632 set1 = t1 ? get_alias_set (t1) : 0;
633 set2 = t2 ? get_alias_set (t2) : 0;
635 /* We can't use alias_sets_conflict_p because we must make sure
636 that every subtype of t1 will conflict with every subtype of
637 t2 for which a pair of subobjects of these respective subtypes
638 overlaps on the stack. */
639 return alias_sets_must_conflict_p (set1, set2);
642 /* Return the outermost parent of component present in the chain of
643 component references handled by get_inner_reference in T with the
644 following property:
645 - the component is non-addressable, or
646 - the parent has alias set zero,
647 or NULL_TREE if no such parent exists. In the former cases, the alias
648 set of this parent is the alias set that must be used for T itself. */
650 tree
651 component_uses_parent_alias_set_from (const_tree t)
653 const_tree found = NULL_TREE;
655 while (handled_component_p (t))
657 switch (TREE_CODE (t))
659 case COMPONENT_REF:
660 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
661 found = t;
662 break;
664 case ARRAY_REF:
665 case ARRAY_RANGE_REF:
666 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
667 found = t;
668 break;
670 case REALPART_EXPR:
671 case IMAGPART_EXPR:
672 break;
674 case BIT_FIELD_REF:
675 case VIEW_CONVERT_EXPR:
676 /* Bitfields and casts are never addressable. */
677 found = t;
678 break;
680 default:
681 gcc_unreachable ();
684 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
685 found = t;
687 t = TREE_OPERAND (t, 0);
690 if (found)
691 return TREE_OPERAND (found, 0);
693 return NULL_TREE;
697 /* Return whether the pointer-type T effective for aliasing may
698 access everything and thus the reference has to be assigned
699 alias-set zero. */
701 static bool
702 ref_all_alias_ptr_type_p (const_tree t)
704 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
705 || TYPE_REF_CAN_ALIAS_ALL (t));
708 /* Return the alias set for the memory pointed to by T, which may be
709 either a type or an expression. Return -1 if there is nothing
710 special about dereferencing T. */
712 static alias_set_type
713 get_deref_alias_set_1 (tree t)
715 /* All we care about is the type. */
716 if (! TYPE_P (t))
717 t = TREE_TYPE (t);
719 /* If we have an INDIRECT_REF via a void pointer, we don't
720 know anything about what that might alias. Likewise if the
721 pointer is marked that way. */
722 if (ref_all_alias_ptr_type_p (t))
723 return 0;
725 return -1;
728 /* Return the alias set for the memory pointed to by T, which may be
729 either a type or an expression. */
731 alias_set_type
732 get_deref_alias_set (tree t)
734 /* If we're not doing any alias analysis, just assume everything
735 aliases everything else. */
736 if (!flag_strict_aliasing)
737 return 0;
739 alias_set_type set = get_deref_alias_set_1 (t);
741 /* Fall back to the alias-set of the pointed-to type. */
742 if (set == -1)
744 if (! TYPE_P (t))
745 t = TREE_TYPE (t);
746 set = get_alias_set (TREE_TYPE (t));
749 return set;
752 /* Return the pointer-type relevant for TBAA purposes from the
753 memory reference tree *T or NULL_TREE in which case *T is
754 adjusted to point to the outermost component reference that
755 can be used for assigning an alias set. */
757 static tree
758 reference_alias_ptr_type_1 (tree *t)
760 tree inner;
762 /* Get the base object of the reference. */
763 inner = *t;
764 while (handled_component_p (inner))
766 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
767 the type of any component references that wrap it to
768 determine the alias-set. */
769 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
770 *t = TREE_OPERAND (inner, 0);
771 inner = TREE_OPERAND (inner, 0);
774 /* Handle pointer dereferences here, they can override the
775 alias-set. */
776 if (INDIRECT_REF_P (inner)
777 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
778 return TREE_TYPE (TREE_OPERAND (inner, 0));
779 else if (TREE_CODE (inner) == TARGET_MEM_REF)
780 return TREE_TYPE (TMR_OFFSET (inner));
781 else if (TREE_CODE (inner) == MEM_REF
782 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
783 return TREE_TYPE (TREE_OPERAND (inner, 1));
785 /* If the innermost reference is a MEM_REF that has a
786 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
787 using the memory access type for determining the alias-set. */
788 if (TREE_CODE (inner) == MEM_REF
789 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
790 != TYPE_MAIN_VARIANT
791 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
792 return TREE_TYPE (TREE_OPERAND (inner, 1));
794 /* Otherwise, pick up the outermost object that we could have
795 a pointer to. */
796 tree tem = component_uses_parent_alias_set_from (*t);
797 if (tem)
798 *t = tem;
800 return NULL_TREE;
803 /* Return the pointer-type relevant for TBAA purposes from the
804 gimple memory reference tree T. This is the type to be used for
805 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
806 and guarantees that get_alias_set will return the same alias
807 set for T and the replacement. */
809 tree
810 reference_alias_ptr_type (tree t)
812 tree ptype = reference_alias_ptr_type_1 (&t);
813 /* If there is a given pointer type for aliasing purposes, return it. */
814 if (ptype != NULL_TREE)
815 return ptype;
817 /* Otherwise build one from the outermost component reference we
818 may use. */
819 if (TREE_CODE (t) == MEM_REF
820 || TREE_CODE (t) == TARGET_MEM_REF)
821 return TREE_TYPE (TREE_OPERAND (t, 1));
822 else
823 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
826 /* Return whether the pointer-types T1 and T2 used to determine
827 two alias sets of two references will yield the same answer
828 from get_deref_alias_set. */
830 bool
831 alias_ptr_types_compatible_p (tree t1, tree t2)
833 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
834 return true;
836 if (ref_all_alias_ptr_type_p (t1)
837 || ref_all_alias_ptr_type_p (t2))
838 return false;
840 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
841 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
844 /* Create emptry alias set entry. */
846 alias_set_entry
847 init_alias_set_entry (alias_set_type set)
849 alias_set_entry ase = ggc_alloc<alias_set_entry_d> ();
850 ase->alias_set = set;
851 ase->children = NULL;
852 ase->has_zero_child = false;
853 ase->is_pointer = false;
854 ase->has_pointer = false;
855 gcc_checking_assert (!get_alias_set_entry (set));
856 (*alias_sets)[set] = ase;
857 return ase;
860 /* Return the alias set for T, which may be either a type or an
861 expression. Call language-specific routine for help, if needed. */
863 alias_set_type
864 get_alias_set (tree t)
866 alias_set_type set;
868 /* If we're not doing any alias analysis, just assume everything
869 aliases everything else. Also return 0 if this or its type is
870 an error. */
871 if (! flag_strict_aliasing || t == error_mark_node
872 || (! TYPE_P (t)
873 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
874 return 0;
876 /* We can be passed either an expression or a type. This and the
877 language-specific routine may make mutually-recursive calls to each other
878 to figure out what to do. At each juncture, we see if this is a tree
879 that the language may need to handle specially. First handle things that
880 aren't types. */
881 if (! TYPE_P (t))
883 /* Give the language a chance to do something with this tree
884 before we look at it. */
885 STRIP_NOPS (t);
886 set = lang_hooks.get_alias_set (t);
887 if (set != -1)
888 return set;
890 /* Get the alias pointer-type to use or the outermost object
891 that we could have a pointer to. */
892 tree ptype = reference_alias_ptr_type_1 (&t);
893 if (ptype != NULL)
894 return get_deref_alias_set (ptype);
896 /* If we've already determined the alias set for a decl, just return
897 it. This is necessary for C++ anonymous unions, whose component
898 variables don't look like union members (boo!). */
899 if (TREE_CODE (t) == VAR_DECL
900 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
901 return MEM_ALIAS_SET (DECL_RTL (t));
903 /* Now all we care about is the type. */
904 t = TREE_TYPE (t);
907 /* Variant qualifiers don't affect the alias set, so get the main
908 variant. */
909 t = TYPE_MAIN_VARIANT (t);
911 /* Always use the canonical type as well. If this is a type that
912 requires structural comparisons to identify compatible types
913 use alias set zero. */
914 if (TYPE_STRUCTURAL_EQUALITY_P (t))
916 /* Allow the language to specify another alias set for this
917 type. */
918 set = lang_hooks.get_alias_set (t);
919 if (set != -1)
920 return set;
921 return 0;
924 t = TYPE_CANONICAL (t);
926 /* The canonical type should not require structural equality checks. */
927 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
929 /* If this is a type with a known alias set, return it. */
930 if (TYPE_ALIAS_SET_KNOWN_P (t))
931 return TYPE_ALIAS_SET (t);
933 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
934 if (!COMPLETE_TYPE_P (t))
936 /* For arrays with unknown size the conservative answer is the
937 alias set of the element type. */
938 if (TREE_CODE (t) == ARRAY_TYPE)
939 return get_alias_set (TREE_TYPE (t));
941 /* But return zero as a conservative answer for incomplete types. */
942 return 0;
945 /* See if the language has special handling for this type. */
946 set = lang_hooks.get_alias_set (t);
947 if (set != -1)
948 return set;
950 /* There are no objects of FUNCTION_TYPE, so there's no point in
951 using up an alias set for them. (There are, of course, pointers
952 and references to functions, but that's different.) */
953 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
954 set = 0;
956 /* Unless the language specifies otherwise, let vector types alias
957 their components. This avoids some nasty type punning issues in
958 normal usage. And indeed lets vectors be treated more like an
959 array slice. */
960 else if (TREE_CODE (t) == VECTOR_TYPE)
961 set = get_alias_set (TREE_TYPE (t));
963 /* Unless the language specifies otherwise, treat array types the
964 same as their components. This avoids the asymmetry we get
965 through recording the components. Consider accessing a
966 character(kind=1) through a reference to a character(kind=1)[1:1].
967 Or consider if we want to assign integer(kind=4)[0:D.1387] and
968 integer(kind=4)[4] the same alias set or not.
969 Just be pragmatic here and make sure the array and its element
970 type get the same alias set assigned. */
971 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
972 set = get_alias_set (TREE_TYPE (t));
974 /* From the former common C and C++ langhook implementation:
976 Unfortunately, there is no canonical form of a pointer type.
977 In particular, if we have `typedef int I', then `int *', and
978 `I *' are different types. So, we have to pick a canonical
979 representative. We do this below.
981 Technically, this approach is actually more conservative that
982 it needs to be. In particular, `const int *' and `int *'
983 should be in different alias sets, according to the C and C++
984 standard, since their types are not the same, and so,
985 technically, an `int **' and `const int **' cannot point at
986 the same thing.
988 But, the standard is wrong. In particular, this code is
989 legal C++:
991 int *ip;
992 int **ipp = &ip;
993 const int* const* cipp = ipp;
994 And, it doesn't make sense for that to be legal unless you
995 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
996 the pointed-to types. This issue has been reported to the
997 C++ committee.
999 For this reason go to canonical type of the unqalified pointer type.
1000 Until GCC 6 this code set all pointers sets to have alias set of
1001 ptr_type_node but that is a bad idea, because it prevents disabiguations
1002 in between pointers. For Firefox this accounts about 20% of all
1003 disambiguations in the program. */
1004 else if (POINTER_TYPE_P (t) && t != ptr_type_node && !in_lto_p)
1006 tree p;
1007 auto_vec <bool, 8> reference;
1009 /* Unnest all pointers and references.
1010 We also want to make pointer to array equivalent to pointer to its
1011 element. So skip all array types, too. */
1012 for (p = t; POINTER_TYPE_P (p)
1013 || (TREE_CODE (p) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (p));
1014 p = TREE_TYPE (p))
1016 if (TREE_CODE (p) == REFERENCE_TYPE)
1017 reference.safe_push (true);
1018 if (TREE_CODE (p) == POINTER_TYPE)
1019 reference.safe_push (false);
1021 p = TYPE_MAIN_VARIANT (p);
1023 /* Make void * compatible with char * and also void **.
1024 Programs are commonly violating TBAA by this.
1026 We also make void * to conflict with every pointer
1027 (see record_component_aliases) and thus it is safe it to use it for
1028 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
1029 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
1030 set = get_alias_set (ptr_type_node);
1031 else
1033 /* Rebuild pointer type from starting from canonical types using
1034 unqualified pointers and references only. This way all such
1035 pointers will have the same alias set and will conflict with
1036 each other.
1038 Most of time we already have pointers or references of a given type.
1039 If not we build new one just to be sure that if someone later
1040 (probably only middle-end can, as we should assign all alias
1041 classes only after finishing translation unit) builds the pointer
1042 type, the canonical type will match. */
1043 p = TYPE_CANONICAL (p);
1044 while (!reference.is_empty ())
1046 if (reference.pop ())
1047 p = build_reference_type (p);
1048 else
1049 p = build_pointer_type (p);
1050 p = TYPE_CANONICAL (TYPE_MAIN_VARIANT (p));
1052 gcc_checking_assert (TYPE_CANONICAL (p) == p);
1054 /* Assign the alias set to both p and t.
1055 We can not call get_alias_set (p) here as that would trigger
1056 infinite recursion when p == t. In other cases it would just
1057 trigger unnecesary legwork of rebuilding the pointer again. */
1058 if (TYPE_ALIAS_SET_KNOWN_P (p))
1059 set = TYPE_ALIAS_SET (p);
1060 else
1062 set = new_alias_set ();
1063 TYPE_ALIAS_SET (p) = set;
1067 /* In LTO the rules above needs to be part of canonical type machinery.
1068 For now just punt. */
1069 else if (POINTER_TYPE_P (t)
1070 && t != TYPE_CANONICAL (ptr_type_node) && in_lto_p)
1071 set = get_alias_set (TYPE_CANONICAL (ptr_type_node));
1073 /* Otherwise make a new alias set for this type. */
1074 else
1076 /* Each canonical type gets its own alias set, so canonical types
1077 shouldn't form a tree. It doesn't really matter for types
1078 we handle specially above, so only check it where it possibly
1079 would result in a bogus alias set. */
1080 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1082 set = new_alias_set ();
1085 TYPE_ALIAS_SET (t) = set;
1087 /* If this is an aggregate type or a complex type, we must record any
1088 component aliasing information. */
1089 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1090 record_component_aliases (t);
1092 /* We treat pointer types specially in alias_set_subset_of. */
1093 if (POINTER_TYPE_P (t) && set)
1095 alias_set_entry ase = get_alias_set_entry (set);
1096 if (!ase)
1097 ase = init_alias_set_entry (set);
1098 ase->is_pointer = true;
1099 ase->has_pointer = true;
1102 return set;
1105 /* Return a brand-new alias set. */
1107 alias_set_type
1108 new_alias_set (void)
1110 if (flag_strict_aliasing)
1112 if (alias_sets == 0)
1113 vec_safe_push (alias_sets, (alias_set_entry) 0);
1114 vec_safe_push (alias_sets, (alias_set_entry) 0);
1115 return alias_sets->length () - 1;
1117 else
1118 return 0;
1121 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1122 not everything that aliases SUPERSET also aliases SUBSET. For example,
1123 in C, a store to an `int' can alias a load of a structure containing an
1124 `int', and vice versa. But it can't alias a load of a 'double' member
1125 of the same structure. Here, the structure would be the SUPERSET and
1126 `int' the SUBSET. This relationship is also described in the comment at
1127 the beginning of this file.
1129 This function should be called only once per SUPERSET/SUBSET pair.
1131 It is illegal for SUPERSET to be zero; everything is implicitly a
1132 subset of alias set zero. */
1134 void
1135 record_alias_subset (alias_set_type superset, alias_set_type subset)
1137 alias_set_entry superset_entry;
1138 alias_set_entry subset_entry;
1140 /* It is possible in complex type situations for both sets to be the same,
1141 in which case we can ignore this operation. */
1142 if (superset == subset)
1143 return;
1145 gcc_assert (superset);
1147 superset_entry = get_alias_set_entry (superset);
1148 if (superset_entry == 0)
1150 /* Create an entry for the SUPERSET, so that we have a place to
1151 attach the SUBSET. */
1152 superset_entry = init_alias_set_entry (superset);
1155 if (subset == 0)
1156 superset_entry->has_zero_child = 1;
1157 else
1159 subset_entry = get_alias_set_entry (subset);
1160 if (!superset_entry->children)
1161 superset_entry->children
1162 = hash_map<int, int, alias_set_traits>::create_ggc (64);
1163 /* If there is an entry for the subset, enter all of its children
1164 (if they are not already present) as children of the SUPERSET. */
1165 if (subset_entry)
1167 if (subset_entry->has_zero_child)
1168 superset_entry->has_zero_child = true;
1169 if (subset_entry->has_pointer)
1170 superset_entry->has_pointer = true;
1172 if (subset_entry->children)
1174 hash_map<int, int, alias_set_traits>::iterator iter
1175 = subset_entry->children->begin ();
1176 for (; iter != subset_entry->children->end (); ++iter)
1177 superset_entry->children->put ((*iter).first, (*iter).second);
1181 /* Enter the SUBSET itself as a child of the SUPERSET. */
1182 superset_entry->children->put (subset, 0);
1186 /* Record that component types of TYPE, if any, are part of that type for
1187 aliasing purposes. For record types, we only record component types
1188 for fields that are not marked non-addressable. For array types, we
1189 only record the component type if it is not marked non-aliased. */
1191 void
1192 record_component_aliases (tree type)
1194 alias_set_type superset = get_alias_set (type);
1195 tree field;
1197 if (superset == 0)
1198 return;
1200 switch (TREE_CODE (type))
1202 case RECORD_TYPE:
1203 case UNION_TYPE:
1204 case QUAL_UNION_TYPE:
1205 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1206 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1207 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1208 break;
1210 case COMPLEX_TYPE:
1211 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1212 break;
1214 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1215 element type. */
1217 default:
1218 break;
1222 /* Allocate an alias set for use in storing and reading from the varargs
1223 spill area. */
1225 static GTY(()) alias_set_type varargs_set = -1;
1227 alias_set_type
1228 get_varargs_alias_set (void)
1230 #if 1
1231 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1232 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1233 consistently use the varargs alias set for loads from the varargs
1234 area. So don't use it anywhere. */
1235 return 0;
1236 #else
1237 if (varargs_set == -1)
1238 varargs_set = new_alias_set ();
1240 return varargs_set;
1241 #endif
1244 /* Likewise, but used for the fixed portions of the frame, e.g., register
1245 save areas. */
1247 static GTY(()) alias_set_type frame_set = -1;
1249 alias_set_type
1250 get_frame_alias_set (void)
1252 if (frame_set == -1)
1253 frame_set = new_alias_set ();
1255 return frame_set;
1258 /* Create a new, unique base with id ID. */
1260 static rtx
1261 unique_base_value (HOST_WIDE_INT id)
1263 return gen_rtx_ADDRESS (Pmode, id);
1266 /* Return true if accesses based on any other base value cannot alias
1267 those based on X. */
1269 static bool
1270 unique_base_value_p (rtx x)
1272 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1275 /* Return true if X is known to be a base value. */
1277 static bool
1278 known_base_value_p (rtx x)
1280 switch (GET_CODE (x))
1282 case LABEL_REF:
1283 case SYMBOL_REF:
1284 return true;
1286 case ADDRESS:
1287 /* Arguments may or may not be bases; we don't know for sure. */
1288 return GET_MODE (x) != VOIDmode;
1290 default:
1291 return false;
1295 /* Inside SRC, the source of a SET, find a base address. */
1297 static rtx
1298 find_base_value (rtx src)
1300 unsigned int regno;
1302 #if defined (FIND_BASE_TERM)
1303 /* Try machine-dependent ways to find the base term. */
1304 src = FIND_BASE_TERM (src);
1305 #endif
1307 switch (GET_CODE (src))
1309 case SYMBOL_REF:
1310 case LABEL_REF:
1311 return src;
1313 case REG:
1314 regno = REGNO (src);
1315 /* At the start of a function, argument registers have known base
1316 values which may be lost later. Returning an ADDRESS
1317 expression here allows optimization based on argument values
1318 even when the argument registers are used for other purposes. */
1319 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1320 return new_reg_base_value[regno];
1322 /* If a pseudo has a known base value, return it. Do not do this
1323 for non-fixed hard regs since it can result in a circular
1324 dependency chain for registers which have values at function entry.
1326 The test above is not sufficient because the scheduler may move
1327 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1328 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1329 && regno < vec_safe_length (reg_base_value))
1331 /* If we're inside init_alias_analysis, use new_reg_base_value
1332 to reduce the number of relaxation iterations. */
1333 if (new_reg_base_value && new_reg_base_value[regno]
1334 && DF_REG_DEF_COUNT (regno) == 1)
1335 return new_reg_base_value[regno];
1337 if ((*reg_base_value)[regno])
1338 return (*reg_base_value)[regno];
1341 return 0;
1343 case MEM:
1344 /* Check for an argument passed in memory. Only record in the
1345 copying-arguments block; it is too hard to track changes
1346 otherwise. */
1347 if (copying_arguments
1348 && (XEXP (src, 0) == arg_pointer_rtx
1349 || (GET_CODE (XEXP (src, 0)) == PLUS
1350 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1351 return arg_base_value;
1352 return 0;
1354 case CONST:
1355 src = XEXP (src, 0);
1356 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1357 break;
1359 /* ... fall through ... */
1361 case PLUS:
1362 case MINUS:
1364 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1366 /* If either operand is a REG that is a known pointer, then it
1367 is the base. */
1368 if (REG_P (src_0) && REG_POINTER (src_0))
1369 return find_base_value (src_0);
1370 if (REG_P (src_1) && REG_POINTER (src_1))
1371 return find_base_value (src_1);
1373 /* If either operand is a REG, then see if we already have
1374 a known value for it. */
1375 if (REG_P (src_0))
1377 temp = find_base_value (src_0);
1378 if (temp != 0)
1379 src_0 = temp;
1382 if (REG_P (src_1))
1384 temp = find_base_value (src_1);
1385 if (temp!= 0)
1386 src_1 = temp;
1389 /* If either base is named object or a special address
1390 (like an argument or stack reference), then use it for the
1391 base term. */
1392 if (src_0 != 0 && known_base_value_p (src_0))
1393 return src_0;
1395 if (src_1 != 0 && known_base_value_p (src_1))
1396 return src_1;
1398 /* Guess which operand is the base address:
1399 If either operand is a symbol, then it is the base. If
1400 either operand is a CONST_INT, then the other is the base. */
1401 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1402 return find_base_value (src_0);
1403 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1404 return find_base_value (src_1);
1406 return 0;
1409 case LO_SUM:
1410 /* The standard form is (lo_sum reg sym) so look only at the
1411 second operand. */
1412 return find_base_value (XEXP (src, 1));
1414 case AND:
1415 /* If the second operand is constant set the base
1416 address to the first operand. */
1417 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1418 return find_base_value (XEXP (src, 0));
1419 return 0;
1421 case TRUNCATE:
1422 /* As we do not know which address space the pointer is referring to, we can
1423 handle this only if the target does not support different pointer or
1424 address modes depending on the address space. */
1425 if (!target_default_pointer_address_modes_p ())
1426 break;
1427 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1428 break;
1429 /* Fall through. */
1430 case HIGH:
1431 case PRE_INC:
1432 case PRE_DEC:
1433 case POST_INC:
1434 case POST_DEC:
1435 case PRE_MODIFY:
1436 case POST_MODIFY:
1437 return find_base_value (XEXP (src, 0));
1439 case ZERO_EXTEND:
1440 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1441 /* As we do not know which address space the pointer is referring to, we can
1442 handle this only if the target does not support different pointer or
1443 address modes depending on the address space. */
1444 if (!target_default_pointer_address_modes_p ())
1445 break;
1448 rtx temp = find_base_value (XEXP (src, 0));
1450 if (temp != 0 && CONSTANT_P (temp))
1451 temp = convert_memory_address (Pmode, temp);
1453 return temp;
1456 default:
1457 break;
1460 return 0;
1463 /* Called from init_alias_analysis indirectly through note_stores,
1464 or directly if DEST is a register with a REG_NOALIAS note attached.
1465 SET is null in the latter case. */
1467 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1468 register N has been set in this function. */
1469 static sbitmap reg_seen;
1471 static void
1472 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1474 unsigned regno;
1475 rtx src;
1476 int n;
1478 if (!REG_P (dest))
1479 return;
1481 regno = REGNO (dest);
1483 gcc_checking_assert (regno < reg_base_value->length ());
1485 n = REG_NREGS (dest);
1486 if (n != 1)
1488 while (--n >= 0)
1490 bitmap_set_bit (reg_seen, regno + n);
1491 new_reg_base_value[regno + n] = 0;
1493 return;
1496 if (set)
1498 /* A CLOBBER wipes out any old value but does not prevent a previously
1499 unset register from acquiring a base address (i.e. reg_seen is not
1500 set). */
1501 if (GET_CODE (set) == CLOBBER)
1503 new_reg_base_value[regno] = 0;
1504 return;
1506 src = SET_SRC (set);
1508 else
1510 /* There's a REG_NOALIAS note against DEST. */
1511 if (bitmap_bit_p (reg_seen, regno))
1513 new_reg_base_value[regno] = 0;
1514 return;
1516 bitmap_set_bit (reg_seen, regno);
1517 new_reg_base_value[regno] = unique_base_value (unique_id++);
1518 return;
1521 /* If this is not the first set of REGNO, see whether the new value
1522 is related to the old one. There are two cases of interest:
1524 (1) The register might be assigned an entirely new value
1525 that has the same base term as the original set.
1527 (2) The set might be a simple self-modification that
1528 cannot change REGNO's base value.
1530 If neither case holds, reject the original base value as invalid.
1531 Note that the following situation is not detected:
1533 extern int x, y; int *p = &x; p += (&y-&x);
1535 ANSI C does not allow computing the difference of addresses
1536 of distinct top level objects. */
1537 if (new_reg_base_value[regno] != 0
1538 && find_base_value (src) != new_reg_base_value[regno])
1539 switch (GET_CODE (src))
1541 case LO_SUM:
1542 case MINUS:
1543 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1544 new_reg_base_value[regno] = 0;
1545 break;
1546 case PLUS:
1547 /* If the value we add in the PLUS is also a valid base value,
1548 this might be the actual base value, and the original value
1549 an index. */
1551 rtx other = NULL_RTX;
1553 if (XEXP (src, 0) == dest)
1554 other = XEXP (src, 1);
1555 else if (XEXP (src, 1) == dest)
1556 other = XEXP (src, 0);
1558 if (! other || find_base_value (other))
1559 new_reg_base_value[regno] = 0;
1560 break;
1562 case AND:
1563 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1564 new_reg_base_value[regno] = 0;
1565 break;
1566 default:
1567 new_reg_base_value[regno] = 0;
1568 break;
1570 /* If this is the first set of a register, record the value. */
1571 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1572 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1573 new_reg_base_value[regno] = find_base_value (src);
1575 bitmap_set_bit (reg_seen, regno);
1578 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1579 using hard registers with non-null REG_BASE_VALUE for renaming. */
1581 get_reg_base_value (unsigned int regno)
1583 return (*reg_base_value)[regno];
1586 /* If a value is known for REGNO, return it. */
1589 get_reg_known_value (unsigned int regno)
1591 if (regno >= FIRST_PSEUDO_REGISTER)
1593 regno -= FIRST_PSEUDO_REGISTER;
1594 if (regno < vec_safe_length (reg_known_value))
1595 return (*reg_known_value)[regno];
1597 return NULL;
1600 /* Set it. */
1602 static void
1603 set_reg_known_value (unsigned int regno, rtx val)
1605 if (regno >= FIRST_PSEUDO_REGISTER)
1607 regno -= FIRST_PSEUDO_REGISTER;
1608 if (regno < vec_safe_length (reg_known_value))
1609 (*reg_known_value)[regno] = val;
1613 /* Similarly for reg_known_equiv_p. */
1615 bool
1616 get_reg_known_equiv_p (unsigned int regno)
1618 if (regno >= FIRST_PSEUDO_REGISTER)
1620 regno -= FIRST_PSEUDO_REGISTER;
1621 if (regno < vec_safe_length (reg_known_value))
1622 return bitmap_bit_p (reg_known_equiv_p, regno);
1624 return false;
1627 static void
1628 set_reg_known_equiv_p (unsigned int regno, bool val)
1630 if (regno >= FIRST_PSEUDO_REGISTER)
1632 regno -= FIRST_PSEUDO_REGISTER;
1633 if (regno < vec_safe_length (reg_known_value))
1635 if (val)
1636 bitmap_set_bit (reg_known_equiv_p, regno);
1637 else
1638 bitmap_clear_bit (reg_known_equiv_p, regno);
1644 /* Returns a canonical version of X, from the point of view alias
1645 analysis. (For example, if X is a MEM whose address is a register,
1646 and the register has a known value (say a SYMBOL_REF), then a MEM
1647 whose address is the SYMBOL_REF is returned.) */
1650 canon_rtx (rtx x)
1652 /* Recursively look for equivalences. */
1653 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1655 rtx t = get_reg_known_value (REGNO (x));
1656 if (t == x)
1657 return x;
1658 if (t)
1659 return canon_rtx (t);
1662 if (GET_CODE (x) == PLUS)
1664 rtx x0 = canon_rtx (XEXP (x, 0));
1665 rtx x1 = canon_rtx (XEXP (x, 1));
1667 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1669 if (CONST_INT_P (x0))
1670 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1671 else if (CONST_INT_P (x1))
1672 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1673 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1677 /* This gives us much better alias analysis when called from
1678 the loop optimizer. Note we want to leave the original
1679 MEM alone, but need to return the canonicalized MEM with
1680 all the flags with their original values. */
1681 else if (MEM_P (x))
1682 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1684 return x;
1687 /* Return 1 if X and Y are identical-looking rtx's.
1688 Expect that X and Y has been already canonicalized.
1690 We use the data in reg_known_value above to see if two registers with
1691 different numbers are, in fact, equivalent. */
1693 static int
1694 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1696 int i;
1697 int j;
1698 enum rtx_code code;
1699 const char *fmt;
1701 if (x == 0 && y == 0)
1702 return 1;
1703 if (x == 0 || y == 0)
1704 return 0;
1706 if (x == y)
1707 return 1;
1709 code = GET_CODE (x);
1710 /* Rtx's of different codes cannot be equal. */
1711 if (code != GET_CODE (y))
1712 return 0;
1714 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1715 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1717 if (GET_MODE (x) != GET_MODE (y))
1718 return 0;
1720 /* Some RTL can be compared without a recursive examination. */
1721 switch (code)
1723 case REG:
1724 return REGNO (x) == REGNO (y);
1726 case LABEL_REF:
1727 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1729 case SYMBOL_REF:
1730 return XSTR (x, 0) == XSTR (y, 0);
1732 case ENTRY_VALUE:
1733 /* This is magic, don't go through canonicalization et al. */
1734 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1736 case VALUE:
1737 CASE_CONST_UNIQUE:
1738 /* Pointer equality guarantees equality for these nodes. */
1739 return 0;
1741 default:
1742 break;
1745 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1746 if (code == PLUS)
1747 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1748 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1749 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1750 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1751 /* For commutative operations, the RTX match if the operand match in any
1752 order. Also handle the simple binary and unary cases without a loop. */
1753 if (COMMUTATIVE_P (x))
1755 rtx xop0 = canon_rtx (XEXP (x, 0));
1756 rtx yop0 = canon_rtx (XEXP (y, 0));
1757 rtx yop1 = canon_rtx (XEXP (y, 1));
1759 return ((rtx_equal_for_memref_p (xop0, yop0)
1760 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1761 || (rtx_equal_for_memref_p (xop0, yop1)
1762 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1764 else if (NON_COMMUTATIVE_P (x))
1766 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1767 canon_rtx (XEXP (y, 0)))
1768 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1769 canon_rtx (XEXP (y, 1))));
1771 else if (UNARY_P (x))
1772 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1773 canon_rtx (XEXP (y, 0)));
1775 /* Compare the elements. If any pair of corresponding elements
1776 fail to match, return 0 for the whole things.
1778 Limit cases to types which actually appear in addresses. */
1780 fmt = GET_RTX_FORMAT (code);
1781 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1783 switch (fmt[i])
1785 case 'i':
1786 if (XINT (x, i) != XINT (y, i))
1787 return 0;
1788 break;
1790 case 'E':
1791 /* Two vectors must have the same length. */
1792 if (XVECLEN (x, i) != XVECLEN (y, i))
1793 return 0;
1795 /* And the corresponding elements must match. */
1796 for (j = 0; j < XVECLEN (x, i); j++)
1797 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1798 canon_rtx (XVECEXP (y, i, j))) == 0)
1799 return 0;
1800 break;
1802 case 'e':
1803 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1804 canon_rtx (XEXP (y, i))) == 0)
1805 return 0;
1806 break;
1808 /* This can happen for asm operands. */
1809 case 's':
1810 if (strcmp (XSTR (x, i), XSTR (y, i)))
1811 return 0;
1812 break;
1814 /* This can happen for an asm which clobbers memory. */
1815 case '0':
1816 break;
1818 /* It is believed that rtx's at this level will never
1819 contain anything but integers and other rtx's,
1820 except for within LABEL_REFs and SYMBOL_REFs. */
1821 default:
1822 gcc_unreachable ();
1825 return 1;
1828 static rtx
1829 find_base_term (rtx x)
1831 cselib_val *val;
1832 struct elt_loc_list *l, *f;
1833 rtx ret;
1835 #if defined (FIND_BASE_TERM)
1836 /* Try machine-dependent ways to find the base term. */
1837 x = FIND_BASE_TERM (x);
1838 #endif
1840 switch (GET_CODE (x))
1842 case REG:
1843 return REG_BASE_VALUE (x);
1845 case TRUNCATE:
1846 /* As we do not know which address space the pointer is referring to, we can
1847 handle this only if the target does not support different pointer or
1848 address modes depending on the address space. */
1849 if (!target_default_pointer_address_modes_p ())
1850 return 0;
1851 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1852 return 0;
1853 /* Fall through. */
1854 case HIGH:
1855 case PRE_INC:
1856 case PRE_DEC:
1857 case POST_INC:
1858 case POST_DEC:
1859 case PRE_MODIFY:
1860 case POST_MODIFY:
1861 return find_base_term (XEXP (x, 0));
1863 case ZERO_EXTEND:
1864 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1865 /* As we do not know which address space the pointer is referring to, we can
1866 handle this only if the target does not support different pointer or
1867 address modes depending on the address space. */
1868 if (!target_default_pointer_address_modes_p ())
1869 return 0;
1872 rtx temp = find_base_term (XEXP (x, 0));
1874 if (temp != 0 && CONSTANT_P (temp))
1875 temp = convert_memory_address (Pmode, temp);
1877 return temp;
1880 case VALUE:
1881 val = CSELIB_VAL_PTR (x);
1882 ret = NULL_RTX;
1884 if (!val)
1885 return ret;
1887 if (cselib_sp_based_value_p (val))
1888 return static_reg_base_value[STACK_POINTER_REGNUM];
1890 f = val->locs;
1891 /* Temporarily reset val->locs to avoid infinite recursion. */
1892 val->locs = NULL;
1894 for (l = f; l; l = l->next)
1895 if (GET_CODE (l->loc) == VALUE
1896 && CSELIB_VAL_PTR (l->loc)->locs
1897 && !CSELIB_VAL_PTR (l->loc)->locs->next
1898 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1899 continue;
1900 else if ((ret = find_base_term (l->loc)) != 0)
1901 break;
1903 val->locs = f;
1904 return ret;
1906 case LO_SUM:
1907 /* The standard form is (lo_sum reg sym) so look only at the
1908 second operand. */
1909 return find_base_term (XEXP (x, 1));
1911 case CONST:
1912 x = XEXP (x, 0);
1913 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1914 return 0;
1915 /* Fall through. */
1916 case PLUS:
1917 case MINUS:
1919 rtx tmp1 = XEXP (x, 0);
1920 rtx tmp2 = XEXP (x, 1);
1922 /* This is a little bit tricky since we have to determine which of
1923 the two operands represents the real base address. Otherwise this
1924 routine may return the index register instead of the base register.
1926 That may cause us to believe no aliasing was possible, when in
1927 fact aliasing is possible.
1929 We use a few simple tests to guess the base register. Additional
1930 tests can certainly be added. For example, if one of the operands
1931 is a shift or multiply, then it must be the index register and the
1932 other operand is the base register. */
1934 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1935 return find_base_term (tmp2);
1937 /* If either operand is known to be a pointer, then prefer it
1938 to determine the base term. */
1939 if (REG_P (tmp1) && REG_POINTER (tmp1))
1941 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1942 std::swap (tmp1, tmp2);
1943 /* If second argument is constant which has base term, prefer it
1944 over variable tmp1. See PR64025. */
1945 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
1946 std::swap (tmp1, tmp2);
1948 /* Go ahead and find the base term for both operands. If either base
1949 term is from a pointer or is a named object or a special address
1950 (like an argument or stack reference), then use it for the
1951 base term. */
1952 rtx base = find_base_term (tmp1);
1953 if (base != NULL_RTX
1954 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1955 || known_base_value_p (base)))
1956 return base;
1957 base = find_base_term (tmp2);
1958 if (base != NULL_RTX
1959 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1960 || known_base_value_p (base)))
1961 return base;
1963 /* We could not determine which of the two operands was the
1964 base register and which was the index. So we can determine
1965 nothing from the base alias check. */
1966 return 0;
1969 case AND:
1970 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1971 return find_base_term (XEXP (x, 0));
1972 return 0;
1974 case SYMBOL_REF:
1975 case LABEL_REF:
1976 return x;
1978 default:
1979 return 0;
1983 /* Return true if accesses to address X may alias accesses based
1984 on the stack pointer. */
1986 bool
1987 may_be_sp_based_p (rtx x)
1989 rtx base = find_base_term (x);
1990 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1993 /* Return 0 if the addresses X and Y are known to point to different
1994 objects, 1 if they might be pointers to the same object. */
1996 static int
1997 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1998 machine_mode x_mode, machine_mode y_mode)
2000 /* If the address itself has no known base see if a known equivalent
2001 value has one. If either address still has no known base, nothing
2002 is known about aliasing. */
2003 if (x_base == 0)
2005 rtx x_c;
2007 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
2008 return 1;
2010 x_base = find_base_term (x_c);
2011 if (x_base == 0)
2012 return 1;
2015 if (y_base == 0)
2017 rtx y_c;
2018 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
2019 return 1;
2021 y_base = find_base_term (y_c);
2022 if (y_base == 0)
2023 return 1;
2026 /* If the base addresses are equal nothing is known about aliasing. */
2027 if (rtx_equal_p (x_base, y_base))
2028 return 1;
2030 /* The base addresses are different expressions. If they are not accessed
2031 via AND, there is no conflict. We can bring knowledge of object
2032 alignment into play here. For example, on alpha, "char a, b;" can
2033 alias one another, though "char a; long b;" cannot. AND addesses may
2034 implicitly alias surrounding objects; i.e. unaligned access in DImode
2035 via AND address can alias all surrounding object types except those
2036 with aligment 8 or higher. */
2037 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2038 return 1;
2039 if (GET_CODE (x) == AND
2040 && (!CONST_INT_P (XEXP (x, 1))
2041 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2042 return 1;
2043 if (GET_CODE (y) == AND
2044 && (!CONST_INT_P (XEXP (y, 1))
2045 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2046 return 1;
2048 /* Differing symbols not accessed via AND never alias. */
2049 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2050 return 0;
2052 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2053 return 0;
2055 return 1;
2058 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2059 that of V. */
2061 static bool
2062 refs_newer_value_p (const_rtx expr, rtx v)
2064 int minuid = CSELIB_VAL_PTR (v)->uid;
2065 subrtx_iterator::array_type array;
2066 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2067 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
2068 return true;
2069 return false;
2072 /* Convert the address X into something we can use. This is done by returning
2073 it unchanged unless it is a value; in the latter case we call cselib to get
2074 a more useful rtx. */
2077 get_addr (rtx x)
2079 cselib_val *v;
2080 struct elt_loc_list *l;
2082 if (GET_CODE (x) != VALUE)
2083 return x;
2084 v = CSELIB_VAL_PTR (x);
2085 if (v)
2087 bool have_equivs = cselib_have_permanent_equivalences ();
2088 if (have_equivs)
2089 v = canonical_cselib_val (v);
2090 for (l = v->locs; l; l = l->next)
2091 if (CONSTANT_P (l->loc))
2092 return l->loc;
2093 for (l = v->locs; l; l = l->next)
2094 if (!REG_P (l->loc) && !MEM_P (l->loc)
2095 /* Avoid infinite recursion when potentially dealing with
2096 var-tracking artificial equivalences, by skipping the
2097 equivalences themselves, and not choosing expressions
2098 that refer to newer VALUEs. */
2099 && (!have_equivs
2100 || (GET_CODE (l->loc) != VALUE
2101 && !refs_newer_value_p (l->loc, x))))
2102 return l->loc;
2103 if (have_equivs)
2105 for (l = v->locs; l; l = l->next)
2106 if (REG_P (l->loc)
2107 || (GET_CODE (l->loc) != VALUE
2108 && !refs_newer_value_p (l->loc, x)))
2109 return l->loc;
2110 /* Return the canonical value. */
2111 return v->val_rtx;
2113 if (v->locs)
2114 return v->locs->loc;
2116 return x;
2119 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2120 where SIZE is the size in bytes of the memory reference. If ADDR
2121 is not modified by the memory reference then ADDR is returned. */
2123 static rtx
2124 addr_side_effect_eval (rtx addr, int size, int n_refs)
2126 int offset = 0;
2128 switch (GET_CODE (addr))
2130 case PRE_INC:
2131 offset = (n_refs + 1) * size;
2132 break;
2133 case PRE_DEC:
2134 offset = -(n_refs + 1) * size;
2135 break;
2136 case POST_INC:
2137 offset = n_refs * size;
2138 break;
2139 case POST_DEC:
2140 offset = -n_refs * size;
2141 break;
2143 default:
2144 return addr;
2147 if (offset)
2148 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
2149 gen_int_mode (offset, GET_MODE (addr)));
2150 else
2151 addr = XEXP (addr, 0);
2152 addr = canon_rtx (addr);
2154 return addr;
2157 /* Return TRUE if an object X sized at XSIZE bytes and another object
2158 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2159 any of the sizes is zero, assume an overlap, otherwise use the
2160 absolute value of the sizes as the actual sizes. */
2162 static inline bool
2163 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
2165 return (xsize == 0 || ysize == 0
2166 || (c >= 0
2167 ? (abs (xsize) > c)
2168 : (abs (ysize) > -c)));
2171 /* Return one if X and Y (memory addresses) reference the
2172 same location in memory or if the references overlap.
2173 Return zero if they do not overlap, else return
2174 minus one in which case they still might reference the same location.
2176 C is an offset accumulator. When
2177 C is nonzero, we are testing aliases between X and Y + C.
2178 XSIZE is the size in bytes of the X reference,
2179 similarly YSIZE is the size in bytes for Y.
2180 Expect that canon_rtx has been already called for X and Y.
2182 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2183 referenced (the reference was BLKmode), so make the most pessimistic
2184 assumptions.
2186 If XSIZE or YSIZE is negative, we may access memory outside the object
2187 being referenced as a side effect. This can happen when using AND to
2188 align memory references, as is done on the Alpha.
2190 Nice to notice that varying addresses cannot conflict with fp if no
2191 local variables had their addresses taken, but that's too hard now.
2193 ??? Contrary to the tree alias oracle this does not return
2194 one for X + non-constant and Y + non-constant when X and Y are equal.
2195 If that is fixed the TBAA hack for union type-punning can be removed. */
2197 static int
2198 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2200 if (GET_CODE (x) == VALUE)
2202 if (REG_P (y))
2204 struct elt_loc_list *l = NULL;
2205 if (CSELIB_VAL_PTR (x))
2206 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2207 l; l = l->next)
2208 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2209 break;
2210 if (l)
2211 x = y;
2212 else
2213 x = get_addr (x);
2215 /* Don't call get_addr if y is the same VALUE. */
2216 else if (x != y)
2217 x = get_addr (x);
2219 if (GET_CODE (y) == VALUE)
2221 if (REG_P (x))
2223 struct elt_loc_list *l = NULL;
2224 if (CSELIB_VAL_PTR (y))
2225 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2226 l; l = l->next)
2227 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2228 break;
2229 if (l)
2230 y = x;
2231 else
2232 y = get_addr (y);
2234 /* Don't call get_addr if x is the same VALUE. */
2235 else if (y != x)
2236 y = get_addr (y);
2238 if (GET_CODE (x) == HIGH)
2239 x = XEXP (x, 0);
2240 else if (GET_CODE (x) == LO_SUM)
2241 x = XEXP (x, 1);
2242 else
2243 x = addr_side_effect_eval (x, abs (xsize), 0);
2244 if (GET_CODE (y) == HIGH)
2245 y = XEXP (y, 0);
2246 else if (GET_CODE (y) == LO_SUM)
2247 y = XEXP (y, 1);
2248 else
2249 y = addr_side_effect_eval (y, abs (ysize), 0);
2251 if (rtx_equal_for_memref_p (x, y))
2253 return offset_overlap_p (c, xsize, ysize);
2256 /* This code used to check for conflicts involving stack references and
2257 globals but the base address alias code now handles these cases. */
2259 if (GET_CODE (x) == PLUS)
2261 /* The fact that X is canonicalized means that this
2262 PLUS rtx is canonicalized. */
2263 rtx x0 = XEXP (x, 0);
2264 rtx x1 = XEXP (x, 1);
2266 if (GET_CODE (y) == PLUS)
2268 /* The fact that Y is canonicalized means that this
2269 PLUS rtx is canonicalized. */
2270 rtx y0 = XEXP (y, 0);
2271 rtx y1 = XEXP (y, 1);
2273 if (rtx_equal_for_memref_p (x1, y1))
2274 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2275 if (rtx_equal_for_memref_p (x0, y0))
2276 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2277 if (CONST_INT_P (x1))
2279 if (CONST_INT_P (y1))
2280 return memrefs_conflict_p (xsize, x0, ysize, y0,
2281 c - INTVAL (x1) + INTVAL (y1));
2282 else
2283 return memrefs_conflict_p (xsize, x0, ysize, y,
2284 c - INTVAL (x1));
2286 else if (CONST_INT_P (y1))
2287 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2289 return -1;
2291 else if (CONST_INT_P (x1))
2292 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2294 else if (GET_CODE (y) == PLUS)
2296 /* The fact that Y is canonicalized means that this
2297 PLUS rtx is canonicalized. */
2298 rtx y0 = XEXP (y, 0);
2299 rtx y1 = XEXP (y, 1);
2301 if (CONST_INT_P (y1))
2302 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2303 else
2304 return -1;
2307 if (GET_CODE (x) == GET_CODE (y))
2308 switch (GET_CODE (x))
2310 case MULT:
2312 /* Handle cases where we expect the second operands to be the
2313 same, and check only whether the first operand would conflict
2314 or not. */
2315 rtx x0, y0;
2316 rtx x1 = canon_rtx (XEXP (x, 1));
2317 rtx y1 = canon_rtx (XEXP (y, 1));
2318 if (! rtx_equal_for_memref_p (x1, y1))
2319 return -1;
2320 x0 = canon_rtx (XEXP (x, 0));
2321 y0 = canon_rtx (XEXP (y, 0));
2322 if (rtx_equal_for_memref_p (x0, y0))
2323 return offset_overlap_p (c, xsize, ysize);
2325 /* Can't properly adjust our sizes. */
2326 if (!CONST_INT_P (x1))
2327 return -1;
2328 xsize /= INTVAL (x1);
2329 ysize /= INTVAL (x1);
2330 c /= INTVAL (x1);
2331 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2334 default:
2335 break;
2338 /* Deal with alignment ANDs by adjusting offset and size so as to
2339 cover the maximum range, without taking any previously known
2340 alignment into account. Make a size negative after such an
2341 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2342 assume a potential overlap, because they may end up in contiguous
2343 memory locations and the stricter-alignment access may span over
2344 part of both. */
2345 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2347 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2348 unsigned HOST_WIDE_INT uc = sc;
2349 if (sc < 0 && -uc == (uc & -uc))
2351 if (xsize > 0)
2352 xsize = -xsize;
2353 if (xsize)
2354 xsize += sc + 1;
2355 c -= sc + 1;
2356 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2357 ysize, y, c);
2360 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2362 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2363 unsigned HOST_WIDE_INT uc = sc;
2364 if (sc < 0 && -uc == (uc & -uc))
2366 if (ysize > 0)
2367 ysize = -ysize;
2368 if (ysize)
2369 ysize += sc + 1;
2370 c += sc + 1;
2371 return memrefs_conflict_p (xsize, x,
2372 ysize, canon_rtx (XEXP (y, 0)), c);
2376 if (CONSTANT_P (x))
2378 if (CONST_INT_P (x) && CONST_INT_P (y))
2380 c += (INTVAL (y) - INTVAL (x));
2381 return offset_overlap_p (c, xsize, ysize);
2384 if (GET_CODE (x) == CONST)
2386 if (GET_CODE (y) == CONST)
2387 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2388 ysize, canon_rtx (XEXP (y, 0)), c);
2389 else
2390 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2391 ysize, y, c);
2393 if (GET_CODE (y) == CONST)
2394 return memrefs_conflict_p (xsize, x, ysize,
2395 canon_rtx (XEXP (y, 0)), c);
2397 /* Assume a potential overlap for symbolic addresses that went
2398 through alignment adjustments (i.e., that have negative
2399 sizes), because we can't know how far they are from each
2400 other. */
2401 if (CONSTANT_P (y))
2402 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2404 return -1;
2407 return -1;
2410 /* Functions to compute memory dependencies.
2412 Since we process the insns in execution order, we can build tables
2413 to keep track of what registers are fixed (and not aliased), what registers
2414 are varying in known ways, and what registers are varying in unknown
2415 ways.
2417 If both memory references are volatile, then there must always be a
2418 dependence between the two references, since their order can not be
2419 changed. A volatile and non-volatile reference can be interchanged
2420 though.
2422 We also must allow AND addresses, because they may generate accesses
2423 outside the object being referenced. This is used to generate aligned
2424 addresses from unaligned addresses, for instance, the alpha
2425 storeqi_unaligned pattern. */
2427 /* Read dependence: X is read after read in MEM takes place. There can
2428 only be a dependence here if both reads are volatile, or if either is
2429 an explicit barrier. */
2432 read_dependence (const_rtx mem, const_rtx x)
2434 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2435 return true;
2436 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2437 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2438 return true;
2439 return false;
2442 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2444 static tree
2445 decl_for_component_ref (tree x)
2449 x = TREE_OPERAND (x, 0);
2451 while (x && TREE_CODE (x) == COMPONENT_REF);
2453 return x && DECL_P (x) ? x : NULL_TREE;
2456 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2457 for the offset of the field reference. *KNOWN_P says whether the
2458 offset is known. */
2460 static void
2461 adjust_offset_for_component_ref (tree x, bool *known_p,
2462 HOST_WIDE_INT *offset)
2464 if (!*known_p)
2465 return;
2468 tree xoffset = component_ref_field_offset (x);
2469 tree field = TREE_OPERAND (x, 1);
2470 if (TREE_CODE (xoffset) != INTEGER_CST)
2472 *known_p = false;
2473 return;
2476 offset_int woffset
2477 = (wi::to_offset (xoffset)
2478 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2479 LOG2_BITS_PER_UNIT));
2480 if (!wi::fits_uhwi_p (woffset))
2482 *known_p = false;
2483 return;
2485 *offset += woffset.to_uhwi ();
2487 x = TREE_OPERAND (x, 0);
2489 while (x && TREE_CODE (x) == COMPONENT_REF);
2492 /* Return nonzero if we can determine the exprs corresponding to memrefs
2493 X and Y and they do not overlap.
2494 If LOOP_VARIANT is set, skip offset-based disambiguation */
2497 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2499 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2500 rtx rtlx, rtly;
2501 rtx basex, basey;
2502 bool moffsetx_known_p, moffsety_known_p;
2503 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2504 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2506 /* Unless both have exprs, we can't tell anything. */
2507 if (exprx == 0 || expry == 0)
2508 return 0;
2510 /* For spill-slot accesses make sure we have valid offsets. */
2511 if ((exprx == get_spill_slot_decl (false)
2512 && ! MEM_OFFSET_KNOWN_P (x))
2513 || (expry == get_spill_slot_decl (false)
2514 && ! MEM_OFFSET_KNOWN_P (y)))
2515 return 0;
2517 /* If the field reference test failed, look at the DECLs involved. */
2518 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2519 if (moffsetx_known_p)
2520 moffsetx = MEM_OFFSET (x);
2521 if (TREE_CODE (exprx) == COMPONENT_REF)
2523 tree t = decl_for_component_ref (exprx);
2524 if (! t)
2525 return 0;
2526 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2527 exprx = t;
2530 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2531 if (moffsety_known_p)
2532 moffsety = MEM_OFFSET (y);
2533 if (TREE_CODE (expry) == COMPONENT_REF)
2535 tree t = decl_for_component_ref (expry);
2536 if (! t)
2537 return 0;
2538 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2539 expry = t;
2542 if (! DECL_P (exprx) || ! DECL_P (expry))
2543 return 0;
2545 /* With invalid code we can end up storing into the constant pool.
2546 Bail out to avoid ICEing when creating RTL for this.
2547 See gfortran.dg/lto/20091028-2_0.f90. */
2548 if (TREE_CODE (exprx) == CONST_DECL
2549 || TREE_CODE (expry) == CONST_DECL)
2550 return 1;
2552 rtlx = DECL_RTL (exprx);
2553 rtly = DECL_RTL (expry);
2555 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2556 can't overlap unless they are the same because we never reuse that part
2557 of the stack frame used for locals for spilled pseudos. */
2558 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2559 && ! rtx_equal_p (rtlx, rtly))
2560 return 1;
2562 /* If we have MEMs referring to different address spaces (which can
2563 potentially overlap), we cannot easily tell from the addresses
2564 whether the references overlap. */
2565 if (MEM_P (rtlx) && MEM_P (rtly)
2566 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2567 return 0;
2569 /* Get the base and offsets of both decls. If either is a register, we
2570 know both are and are the same, so use that as the base. The only
2571 we can avoid overlap is if we can deduce that they are nonoverlapping
2572 pieces of that decl, which is very rare. */
2573 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2574 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2575 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2577 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2578 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2579 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2581 /* If the bases are different, we know they do not overlap if both
2582 are constants or if one is a constant and the other a pointer into the
2583 stack frame. Otherwise a different base means we can't tell if they
2584 overlap or not. */
2585 if (! rtx_equal_p (basex, basey))
2586 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2587 || (CONSTANT_P (basex) && REG_P (basey)
2588 && REGNO_PTR_FRAME_P (REGNO (basey)))
2589 || (CONSTANT_P (basey) && REG_P (basex)
2590 && REGNO_PTR_FRAME_P (REGNO (basex))));
2592 /* Offset based disambiguation not appropriate for loop invariant */
2593 if (loop_invariant)
2594 return 0;
2596 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2597 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2598 : -1);
2599 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2600 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2601 : -1);
2603 /* If we have an offset for either memref, it can update the values computed
2604 above. */
2605 if (moffsetx_known_p)
2606 offsetx += moffsetx, sizex -= moffsetx;
2607 if (moffsety_known_p)
2608 offsety += moffsety, sizey -= moffsety;
2610 /* If a memref has both a size and an offset, we can use the smaller size.
2611 We can't do this if the offset isn't known because we must view this
2612 memref as being anywhere inside the DECL's MEM. */
2613 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2614 sizex = MEM_SIZE (x);
2615 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2616 sizey = MEM_SIZE (y);
2618 /* Put the values of the memref with the lower offset in X's values. */
2619 if (offsetx > offsety)
2621 tem = offsetx, offsetx = offsety, offsety = tem;
2622 tem = sizex, sizex = sizey, sizey = tem;
2625 /* If we don't know the size of the lower-offset value, we can't tell
2626 if they conflict. Otherwise, we do the test. */
2627 return sizex >= 0 && offsety >= offsetx + sizex;
2630 /* Helper for true_dependence and canon_true_dependence.
2631 Checks for true dependence: X is read after store in MEM takes place.
2633 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2634 NULL_RTX, and the canonical addresses of MEM and X are both computed
2635 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2637 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2639 Returns 1 if there is a true dependence, 0 otherwise. */
2641 static int
2642 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2643 const_rtx x, rtx x_addr, bool mem_canonicalized)
2645 rtx true_mem_addr;
2646 rtx base;
2647 int ret;
2649 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2650 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2652 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2653 return 1;
2655 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2656 This is used in epilogue deallocation functions, and in cselib. */
2657 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2658 return 1;
2659 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2660 return 1;
2661 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2662 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2663 return 1;
2665 if (! x_addr)
2666 x_addr = XEXP (x, 0);
2667 x_addr = get_addr (x_addr);
2669 if (! mem_addr)
2671 mem_addr = XEXP (mem, 0);
2672 if (mem_mode == VOIDmode)
2673 mem_mode = GET_MODE (mem);
2675 true_mem_addr = get_addr (mem_addr);
2677 /* Read-only memory is by definition never modified, and therefore can't
2678 conflict with anything. However, don't assume anything when AND
2679 addresses are involved and leave to the code below to determine
2680 dependence. We don't expect to find read-only set on MEM, but
2681 stupid user tricks can produce them, so don't die. */
2682 if (MEM_READONLY_P (x)
2683 && GET_CODE (x_addr) != AND
2684 && GET_CODE (true_mem_addr) != AND)
2685 return 0;
2687 /* If we have MEMs referring to different address spaces (which can
2688 potentially overlap), we cannot easily tell from the addresses
2689 whether the references overlap. */
2690 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2691 return 1;
2693 base = find_base_term (x_addr);
2694 if (base && (GET_CODE (base) == LABEL_REF
2695 || (GET_CODE (base) == SYMBOL_REF
2696 && CONSTANT_POOL_ADDRESS_P (base))))
2697 return 0;
2699 rtx mem_base = find_base_term (true_mem_addr);
2700 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2701 GET_MODE (x), mem_mode))
2702 return 0;
2704 x_addr = canon_rtx (x_addr);
2705 if (!mem_canonicalized)
2706 mem_addr = canon_rtx (true_mem_addr);
2708 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2709 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2710 return ret;
2712 if (mems_in_disjoint_alias_sets_p (x, mem))
2713 return 0;
2715 if (nonoverlapping_memrefs_p (mem, x, false))
2716 return 0;
2718 return rtx_refs_may_alias_p (x, mem, true);
2721 /* True dependence: X is read after store in MEM takes place. */
2724 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2726 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2727 x, NULL_RTX, /*mem_canonicalized=*/false);
2730 /* Canonical true dependence: X is read after store in MEM takes place.
2731 Variant of true_dependence which assumes MEM has already been
2732 canonicalized (hence we no longer do that here).
2733 The mem_addr argument has been added, since true_dependence_1 computed
2734 this value prior to canonicalizing. */
2737 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2738 const_rtx x, rtx x_addr)
2740 return true_dependence_1 (mem, mem_mode, mem_addr,
2741 x, x_addr, /*mem_canonicalized=*/true);
2744 /* Returns nonzero if a write to X might alias a previous read from
2745 (or, if WRITEP is true, a write to) MEM.
2746 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2747 and X_MODE the mode for that access.
2748 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2750 static int
2751 write_dependence_p (const_rtx mem,
2752 const_rtx x, machine_mode x_mode, rtx x_addr,
2753 bool mem_canonicalized, bool x_canonicalized, bool writep)
2755 rtx mem_addr;
2756 rtx true_mem_addr, true_x_addr;
2757 rtx base;
2758 int ret;
2760 gcc_checking_assert (x_canonicalized
2761 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2762 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2764 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2765 return 1;
2767 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2768 This is used in epilogue deallocation functions. */
2769 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2770 return 1;
2771 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2772 return 1;
2773 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2774 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2775 return 1;
2777 if (!x_addr)
2778 x_addr = XEXP (x, 0);
2779 true_x_addr = get_addr (x_addr);
2781 mem_addr = XEXP (mem, 0);
2782 true_mem_addr = get_addr (mem_addr);
2784 /* A read from read-only memory can't conflict with read-write memory.
2785 Don't assume anything when AND addresses are involved and leave to
2786 the code below to determine dependence. */
2787 if (!writep
2788 && MEM_READONLY_P (mem)
2789 && GET_CODE (true_x_addr) != AND
2790 && GET_CODE (true_mem_addr) != AND)
2791 return 0;
2793 /* If we have MEMs referring to different address spaces (which can
2794 potentially overlap), we cannot easily tell from the addresses
2795 whether the references overlap. */
2796 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2797 return 1;
2799 base = find_base_term (true_mem_addr);
2800 if (! writep
2801 && base
2802 && (GET_CODE (base) == LABEL_REF
2803 || (GET_CODE (base) == SYMBOL_REF
2804 && CONSTANT_POOL_ADDRESS_P (base))))
2805 return 0;
2807 rtx x_base = find_base_term (true_x_addr);
2808 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2809 GET_MODE (x), GET_MODE (mem)))
2810 return 0;
2812 if (!x_canonicalized)
2814 x_addr = canon_rtx (true_x_addr);
2815 x_mode = GET_MODE (x);
2817 if (!mem_canonicalized)
2818 mem_addr = canon_rtx (true_mem_addr);
2820 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2821 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2822 return ret;
2824 if (nonoverlapping_memrefs_p (x, mem, false))
2825 return 0;
2827 return rtx_refs_may_alias_p (x, mem, false);
2830 /* Anti dependence: X is written after read in MEM takes place. */
2833 anti_dependence (const_rtx mem, const_rtx x)
2835 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2836 /*mem_canonicalized=*/false,
2837 /*x_canonicalized*/false, /*writep=*/false);
2840 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2841 Also, consider X in X_MODE (which might be from an enclosing
2842 STRICT_LOW_PART / ZERO_EXTRACT).
2843 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2846 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2847 const_rtx x, machine_mode x_mode, rtx x_addr)
2849 return write_dependence_p (mem, x, x_mode, x_addr,
2850 mem_canonicalized, /*x_canonicalized=*/true,
2851 /*writep=*/false);
2854 /* Output dependence: X is written after store in MEM takes place. */
2857 output_dependence (const_rtx mem, const_rtx x)
2859 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2860 /*mem_canonicalized=*/false,
2861 /*x_canonicalized*/false, /*writep=*/true);
2866 /* Check whether X may be aliased with MEM. Don't do offset-based
2867 memory disambiguation & TBAA. */
2869 may_alias_p (const_rtx mem, const_rtx x)
2871 rtx x_addr, mem_addr;
2873 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2874 return 1;
2876 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2877 This is used in epilogue deallocation functions. */
2878 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2879 return 1;
2880 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2881 return 1;
2882 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2883 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2884 return 1;
2886 x_addr = XEXP (x, 0);
2887 x_addr = get_addr (x_addr);
2889 mem_addr = XEXP (mem, 0);
2890 mem_addr = get_addr (mem_addr);
2892 /* Read-only memory is by definition never modified, and therefore can't
2893 conflict with anything. However, don't assume anything when AND
2894 addresses are involved and leave to the code below to determine
2895 dependence. We don't expect to find read-only set on MEM, but
2896 stupid user tricks can produce them, so don't die. */
2897 if (MEM_READONLY_P (x)
2898 && GET_CODE (x_addr) != AND
2899 && GET_CODE (mem_addr) != AND)
2900 return 0;
2902 /* If we have MEMs referring to different address spaces (which can
2903 potentially overlap), we cannot easily tell from the addresses
2904 whether the references overlap. */
2905 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2906 return 1;
2908 rtx x_base = find_base_term (x_addr);
2909 rtx mem_base = find_base_term (mem_addr);
2910 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2911 GET_MODE (x), GET_MODE (mem_addr)))
2912 return 0;
2914 if (nonoverlapping_memrefs_p (mem, x, true))
2915 return 0;
2917 /* TBAA not valid for loop_invarint */
2918 return rtx_refs_may_alias_p (x, mem, false);
2921 void
2922 init_alias_target (void)
2924 int i;
2926 if (!arg_base_value)
2927 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2929 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2931 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2932 /* Check whether this register can hold an incoming pointer
2933 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2934 numbers, so translate if necessary due to register windows. */
2935 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2936 && HARD_REGNO_MODE_OK (i, Pmode))
2937 static_reg_base_value[i] = arg_base_value;
2939 static_reg_base_value[STACK_POINTER_REGNUM]
2940 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2941 static_reg_base_value[ARG_POINTER_REGNUM]
2942 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2943 static_reg_base_value[FRAME_POINTER_REGNUM]
2944 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2945 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2946 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2947 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2950 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2951 to be memory reference. */
2952 static bool memory_modified;
2953 static void
2954 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2956 if (MEM_P (x))
2958 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2959 memory_modified = true;
2964 /* Return true when INSN possibly modify memory contents of MEM
2965 (i.e. address can be modified). */
2966 bool
2967 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2969 if (!INSN_P (insn))
2970 return false;
2971 memory_modified = false;
2972 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2973 return memory_modified;
2976 /* Return TRUE if the destination of a set is rtx identical to
2977 ITEM. */
2978 static inline bool
2979 set_dest_equal_p (const_rtx set, const_rtx item)
2981 rtx dest = SET_DEST (set);
2982 return rtx_equal_p (dest, item);
2985 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2986 *DEFINITELY* modify the memory contents of MEM. */
2987 bool
2988 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2990 if (!INSN_P (insn))
2991 return false;
2992 insn = PATTERN (insn);
2993 if (GET_CODE (insn) == SET)
2994 return set_dest_equal_p (insn, mem);
2995 else if (GET_CODE (insn) == PARALLEL)
2997 int i;
2998 for (i = 0; i < XVECLEN (insn, 0); i++)
3000 rtx sub = XVECEXP (insn, 0, i);
3001 if (GET_CODE (sub) == SET
3002 && set_dest_equal_p (sub, mem))
3003 return true;
3006 return false;
3009 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3010 array. */
3012 void
3013 init_alias_analysis (void)
3015 unsigned int maxreg = max_reg_num ();
3016 int changed, pass;
3017 int i;
3018 unsigned int ui;
3019 rtx_insn *insn;
3020 rtx val;
3021 int rpo_cnt;
3022 int *rpo;
3024 timevar_push (TV_ALIAS_ANALYSIS);
3026 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
3027 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3028 bitmap_clear (reg_known_equiv_p);
3030 /* If we have memory allocated from the previous run, use it. */
3031 if (old_reg_base_value)
3032 reg_base_value = old_reg_base_value;
3034 if (reg_base_value)
3035 reg_base_value->truncate (0);
3037 vec_safe_grow_cleared (reg_base_value, maxreg);
3039 new_reg_base_value = XNEWVEC (rtx, maxreg);
3040 reg_seen = sbitmap_alloc (maxreg);
3042 /* The basic idea is that each pass through this loop will use the
3043 "constant" information from the previous pass to propagate alias
3044 information through another level of assignments.
3046 The propagation is done on the CFG in reverse post-order, to propagate
3047 things forward as far as possible in each iteration.
3049 This could get expensive if the assignment chains are long. Maybe
3050 we should throttle the number of iterations, possibly based on
3051 the optimization level or flag_expensive_optimizations.
3053 We could propagate more information in the first pass by making use
3054 of DF_REG_DEF_COUNT to determine immediately that the alias information
3055 for a pseudo is "constant".
3057 A program with an uninitialized variable can cause an infinite loop
3058 here. Instead of doing a full dataflow analysis to detect such problems
3059 we just cap the number of iterations for the loop.
3061 The state of the arrays for the set chain in question does not matter
3062 since the program has undefined behavior. */
3064 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3065 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3067 pass = 0;
3070 /* Assume nothing will change this iteration of the loop. */
3071 changed = 0;
3073 /* We want to assign the same IDs each iteration of this loop, so
3074 start counting from one each iteration of the loop. */
3075 unique_id = 1;
3077 /* We're at the start of the function each iteration through the
3078 loop, so we're copying arguments. */
3079 copying_arguments = true;
3081 /* Wipe the potential alias information clean for this pass. */
3082 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3084 /* Wipe the reg_seen array clean. */
3085 bitmap_clear (reg_seen);
3087 /* Initialize the alias information for this pass. */
3088 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3089 if (static_reg_base_value[i])
3091 new_reg_base_value[i] = static_reg_base_value[i];
3092 bitmap_set_bit (reg_seen, i);
3095 /* Walk the insns adding values to the new_reg_base_value array. */
3096 for (i = 0; i < rpo_cnt; i++)
3098 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3099 FOR_BB_INSNS (bb, insn)
3101 if (NONDEBUG_INSN_P (insn))
3103 rtx note, set;
3105 #if defined (HAVE_prologue)
3106 static const bool prologue = true;
3107 #else
3108 static const bool prologue = false;
3109 #endif
3111 /* The prologue/epilogue insns are not threaded onto the
3112 insn chain until after reload has completed. Thus,
3113 there is no sense wasting time checking if INSN is in
3114 the prologue/epilogue until after reload has completed. */
3115 if ((prologue || HAVE_epilogue) && reload_completed
3116 && prologue_epilogue_contains (insn))
3117 continue;
3119 /* If this insn has a noalias note, process it, Otherwise,
3120 scan for sets. A simple set will have no side effects
3121 which could change the base value of any other register. */
3123 if (GET_CODE (PATTERN (insn)) == SET
3124 && REG_NOTES (insn) != 0
3125 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3126 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3127 else
3128 note_stores (PATTERN (insn), record_set, NULL);
3130 set = single_set (insn);
3132 if (set != 0
3133 && REG_P (SET_DEST (set))
3134 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3136 unsigned int regno = REGNO (SET_DEST (set));
3137 rtx src = SET_SRC (set);
3138 rtx t;
3140 note = find_reg_equal_equiv_note (insn);
3141 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3142 && DF_REG_DEF_COUNT (regno) != 1)
3143 note = NULL_RTX;
3145 if (note != NULL_RTX
3146 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3147 && ! rtx_varies_p (XEXP (note, 0), 1)
3148 && ! reg_overlap_mentioned_p (SET_DEST (set),
3149 XEXP (note, 0)))
3151 set_reg_known_value (regno, XEXP (note, 0));
3152 set_reg_known_equiv_p (regno,
3153 REG_NOTE_KIND (note) == REG_EQUIV);
3155 else if (DF_REG_DEF_COUNT (regno) == 1
3156 && GET_CODE (src) == PLUS
3157 && REG_P (XEXP (src, 0))
3158 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3159 && CONST_INT_P (XEXP (src, 1)))
3161 t = plus_constant (GET_MODE (src), t,
3162 INTVAL (XEXP (src, 1)));
3163 set_reg_known_value (regno, t);
3164 set_reg_known_equiv_p (regno, false);
3166 else if (DF_REG_DEF_COUNT (regno) == 1
3167 && ! rtx_varies_p (src, 1))
3169 set_reg_known_value (regno, src);
3170 set_reg_known_equiv_p (regno, false);
3174 else if (NOTE_P (insn)
3175 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3176 copying_arguments = false;
3180 /* Now propagate values from new_reg_base_value to reg_base_value. */
3181 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3183 for (ui = 0; ui < maxreg; ui++)
3185 if (new_reg_base_value[ui]
3186 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3187 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3189 (*reg_base_value)[ui] = new_reg_base_value[ui];
3190 changed = 1;
3194 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3195 XDELETEVEC (rpo);
3197 /* Fill in the remaining entries. */
3198 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3200 int regno = i + FIRST_PSEUDO_REGISTER;
3201 if (! val)
3202 set_reg_known_value (regno, regno_reg_rtx[regno]);
3205 /* Clean up. */
3206 free (new_reg_base_value);
3207 new_reg_base_value = 0;
3208 sbitmap_free (reg_seen);
3209 reg_seen = 0;
3210 timevar_pop (TV_ALIAS_ANALYSIS);
3213 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3214 Special API for var-tracking pass purposes. */
3216 void
3217 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3219 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3222 void
3223 end_alias_analysis (void)
3225 old_reg_base_value = reg_base_value;
3226 vec_free (reg_known_value);
3227 sbitmap_free (reg_known_equiv_p);
3230 void
3231 dump_alias_stats_in_alias_c (FILE *s)
3233 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3234 " %llu are in alias set 0\n"
3235 " %llu queries asked about the same object\n"
3236 " %llu queries asked about the same alias set\n"
3237 " %llu access volatile\n"
3238 " %llu are dependent in the DAG\n"
3239 " %llu are aritificially in conflict with void *\n",
3240 alias_stats.num_disambiguated,
3241 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3242 + alias_stats.num_same_objects + alias_stats.num_volatile
3243 + alias_stats.num_dag + alias_stats.num_disambiguated
3244 + alias_stats.num_universal,
3245 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3246 alias_stats.num_same_objects, alias_stats.num_volatile,
3247 alias_stats.num_dag, alias_stats.num_universal);
3249 #include "gt-alias.h"