gcc/ada/
[official-gcc.git] / gcc / alias.c
blobf79cc4236a9df50798f0b09b46717afffb86201a
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 "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "varasm.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "expmed.h"
36 #include "dojump.h"
37 #include "explow.h"
38 #include "calls.h"
39 #include "emit-rtl.h"
40 #include "stmt.h"
41 #include "expr.h"
42 #include "tm_p.h"
43 #include "regs.h"
44 #include "diagnostic-core.h"
45 #include "alloc-pool.h"
46 #include "cselib.h"
47 #include "langhooks.h"
48 #include "timevar.h"
49 #include "dumpfile.h"
50 #include "target.h"
51 #include "dominance.h"
52 #include "cfg.h"
53 #include "cfganal.h"
54 #include "predict.h"
55 #include "basic-block.h"
56 #include "df.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "gimple-expr.h"
60 #include "gimple.h"
61 #include "gimple-ssa.h"
62 #include "rtl-iter.h"
64 /* The aliasing API provided here solves related but different problems:
66 Say there exists (in c)
68 struct X {
69 struct Y y1;
70 struct Z z2;
71 } x1, *px1, *px2;
73 struct Y y2, *py;
74 struct Z z2, *pz;
77 py = &x1.y1;
78 px2 = &x1;
80 Consider the four questions:
82 Can a store to x1 interfere with px2->y1?
83 Can a store to x1 interfere with px2->z2?
84 Can a store to x1 change the value pointed to by with py?
85 Can a store to x1 change the value pointed to by with pz?
87 The answer to these questions can be yes, yes, yes, and maybe.
89 The first two questions can be answered with a simple examination
90 of the type system. If structure X contains a field of type Y then
91 a store through a pointer to an X can overwrite any field that is
92 contained (recursively) in an X (unless we know that px1 != px2).
94 The last two questions can be solved in the same way as the first
95 two questions but this is too conservative. The observation is
96 that in some cases we can know which (if any) fields are addressed
97 and if those addresses are used in bad ways. This analysis may be
98 language specific. In C, arbitrary operations may be applied to
99 pointers. However, there is some indication that this may be too
100 conservative for some C++ types.
102 The pass ipa-type-escape does this analysis for the types whose
103 instances do not escape across the compilation boundary.
105 Historically in GCC, these two problems were combined and a single
106 data structure that was used to represent the solution to these
107 problems. We now have two similar but different data structures,
108 The data structure to solve the last two questions is similar to
109 the first, but does not contain the fields whose address are never
110 taken. For types that do escape the compilation unit, the data
111 structures will have identical information.
114 /* The alias sets assigned to MEMs assist the back-end in determining
115 which MEMs can alias which other MEMs. In general, two MEMs in
116 different alias sets cannot alias each other, with one important
117 exception. Consider something like:
119 struct S { int i; double d; };
121 a store to an `S' can alias something of either type `int' or type
122 `double'. (However, a store to an `int' cannot alias a `double'
123 and vice versa.) We indicate this via a tree structure that looks
124 like:
125 struct S
128 |/_ _\|
129 int double
131 (The arrows are directed and point downwards.)
132 In this situation we say the alias set for `struct S' is the
133 `superset' and that those for `int' and `double' are `subsets'.
135 To see whether two alias sets can point to the same memory, we must
136 see if either alias set is a subset of the other. We need not trace
137 past immediate descendants, however, since we propagate all
138 grandchildren up one level.
140 Alias set zero is implicitly a superset of all other alias sets.
141 However, this is no actual entry for alias set zero. It is an
142 error to attempt to explicitly construct a subset of zero. */
144 struct alias_set_traits : default_hashmap_traits
146 template<typename T>
147 static bool
148 is_empty (T &e)
150 return e.m_key == INT_MIN;
153 template<typename T>
154 static bool
155 is_deleted (T &e)
157 return e.m_key == (INT_MIN + 1);
160 template<typename T> static void mark_empty (T &e) { e.m_key = INT_MIN; }
162 template<typename T>
163 static void
164 mark_deleted (T &e)
166 e.m_key = INT_MIN + 1;
170 struct GTY(()) alias_set_entry_d {
171 /* The alias set number, as stored in MEM_ALIAS_SET. */
172 alias_set_type alias_set;
174 /* The children of the alias set. These are not just the immediate
175 children, but, in fact, all descendants. So, if we have:
177 struct T { struct S s; float f; }
179 continuing our example above, the children here will be all of
180 `int', `double', `float', and `struct S'. */
181 hash_map<int, int, alias_set_traits> *children;
183 /* Nonzero if would have a child of zero: this effectively makes this
184 alias set the same as alias set zero. */
185 bool has_zero_child;
186 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
187 aggregate contaiing pointer.
188 This is used for a special case where we need an universal pointer type
189 compatible with all other pointer types. */
190 bool is_pointer;
191 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
192 bool has_pointer;
194 typedef struct alias_set_entry_d *alias_set_entry;
196 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
197 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
198 static void record_set (rtx, const_rtx, void *);
199 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
200 machine_mode);
201 static rtx find_base_value (rtx);
202 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
203 static alias_set_entry get_alias_set_entry (alias_set_type);
204 static tree decl_for_component_ref (tree);
205 static int write_dependence_p (const_rtx,
206 const_rtx, machine_mode, rtx,
207 bool, bool, bool);
209 static void memory_modified_1 (rtx, const_rtx, void *);
211 /* Query statistics for the different low-level disambiguators.
212 A high-level query may trigger multiple of them. */
214 static struct {
215 unsigned long long num_alias_zero;
216 unsigned long long num_same_alias_set;
217 unsigned long long num_same_objects;
218 unsigned long long num_volatile;
219 unsigned long long num_dag;
220 unsigned long long num_universal;
221 unsigned long long num_disambiguated;
222 } alias_stats;
225 /* Set up all info needed to perform alias analysis on memory references. */
227 /* Returns the size in bytes of the mode of X. */
228 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
230 /* Cap the number of passes we make over the insns propagating alias
231 information through set chains.
232 ??? 10 is a completely arbitrary choice. This should be based on the
233 maximum loop depth in the CFG, but we do not have this information
234 available (even if current_loops _is_ available). */
235 #define MAX_ALIAS_LOOP_PASSES 10
237 /* reg_base_value[N] gives an address to which register N is related.
238 If all sets after the first add or subtract to the current value
239 or otherwise modify it so it does not point to a different top level
240 object, reg_base_value[N] is equal to the address part of the source
241 of the first set.
243 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
244 expressions represent three types of base:
246 1. incoming arguments. There is just one ADDRESS to represent all
247 arguments, since we do not know at this level whether accesses
248 based on different arguments can alias. The ADDRESS has id 0.
250 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
251 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
252 Each of these rtxes has a separate ADDRESS associated with it,
253 each with a negative id.
255 GCC is (and is required to be) precise in which register it
256 chooses to access a particular region of stack. We can therefore
257 assume that accesses based on one of these rtxes do not alias
258 accesses based on another of these rtxes.
260 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
261 Each such piece of memory has a separate ADDRESS associated
262 with it, each with an id greater than 0.
264 Accesses based on one ADDRESS do not alias accesses based on other
265 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
266 alias globals either; the ADDRESSes have Pmode to indicate this.
267 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
268 indicate this. */
270 static GTY(()) vec<rtx, va_gc> *reg_base_value;
271 static rtx *new_reg_base_value;
273 /* The single VOIDmode ADDRESS that represents all argument bases.
274 It has id 0. */
275 static GTY(()) rtx arg_base_value;
277 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
278 static int unique_id;
280 /* We preserve the copy of old array around to avoid amount of garbage
281 produced. About 8% of garbage produced were attributed to this
282 array. */
283 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
285 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
286 registers. */
287 #define UNIQUE_BASE_VALUE_SP -1
288 #define UNIQUE_BASE_VALUE_ARGP -2
289 #define UNIQUE_BASE_VALUE_FP -3
290 #define UNIQUE_BASE_VALUE_HFP -4
292 #define static_reg_base_value \
293 (this_target_rtl->x_static_reg_base_value)
295 #define REG_BASE_VALUE(X) \
296 (REGNO (X) < vec_safe_length (reg_base_value) \
297 ? (*reg_base_value)[REGNO (X)] : 0)
299 /* Vector indexed by N giving the initial (unchanging) value known for
300 pseudo-register N. This vector is initialized in init_alias_analysis,
301 and does not change until end_alias_analysis is called. */
302 static GTY(()) vec<rtx, va_gc> *reg_known_value;
304 /* Vector recording for each reg_known_value whether it is due to a
305 REG_EQUIV note. Future passes (viz., reload) may replace the
306 pseudo with the equivalent expression and so we account for the
307 dependences that would be introduced if that happens.
309 The REG_EQUIV notes created in assign_parms may mention the arg
310 pointer, and there are explicit insns in the RTL that modify the
311 arg pointer. Thus we must ensure that such insns don't get
312 scheduled across each other because that would invalidate the
313 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
314 wrong, but solving the problem in the scheduler will likely give
315 better code, so we do it here. */
316 static sbitmap reg_known_equiv_p;
318 /* True when scanning insns from the start of the rtl to the
319 NOTE_INSN_FUNCTION_BEG note. */
320 static bool copying_arguments;
323 /* The splay-tree used to store the various alias set entries. */
324 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
326 /* Build a decomposed reference object for querying the alias-oracle
327 from the MEM rtx and store it in *REF.
328 Returns false if MEM is not suitable for the alias-oracle. */
330 static bool
331 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
333 tree expr = MEM_EXPR (mem);
334 tree base;
336 if (!expr)
337 return false;
339 ao_ref_init (ref, expr);
341 /* Get the base of the reference and see if we have to reject or
342 adjust it. */
343 base = ao_ref_base (ref);
344 if (base == NULL_TREE)
345 return false;
347 /* The tree oracle doesn't like bases that are neither decls
348 nor indirect references of SSA names. */
349 if (!(DECL_P (base)
350 || (TREE_CODE (base) == MEM_REF
351 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
352 || (TREE_CODE (base) == TARGET_MEM_REF
353 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
354 return false;
356 /* If this is a reference based on a partitioned decl replace the
357 base with a MEM_REF of the pointer representative we
358 created during stack slot partitioning. */
359 if (TREE_CODE (base) == VAR_DECL
360 && ! is_global_var (base)
361 && cfun->gimple_df->decls_to_pointers != NULL)
363 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
364 if (namep)
365 ref->base = build_simple_mem_ref (*namep);
368 ref->ref_alias_set = MEM_ALIAS_SET (mem);
370 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
371 is conservative, so trust it. */
372 if (!MEM_OFFSET_KNOWN_P (mem)
373 || !MEM_SIZE_KNOWN_P (mem))
374 return true;
376 /* If the base decl is a parameter we can have negative MEM_OFFSET in
377 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
378 here. */
379 if (MEM_OFFSET (mem) < 0
380 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
381 return true;
383 /* Otherwise continue and refine size and offset we got from analyzing
384 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
386 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
387 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
389 /* The MEM may extend into adjacent fields, so adjust max_size if
390 necessary. */
391 if (ref->max_size != -1
392 && ref->size > ref->max_size)
393 ref->max_size = ref->size;
395 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
396 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
397 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
398 && (ref->offset < 0
399 || (DECL_P (ref->base)
400 && (DECL_SIZE (ref->base) == NULL_TREE
401 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
402 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
403 ref->offset + ref->size)))))
404 return false;
406 return true;
409 /* Query the alias-oracle on whether the two memory rtx X and MEM may
410 alias. If TBAA_P is set also apply TBAA. Returns true if the
411 two rtxen may alias, false otherwise. */
413 static bool
414 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
416 ao_ref ref1, ref2;
418 if (!ao_ref_from_mem (&ref1, x)
419 || !ao_ref_from_mem (&ref2, mem))
420 return true;
422 return refs_may_alias_p_1 (&ref1, &ref2,
423 tbaa_p
424 && MEM_ALIAS_SET (x) != 0
425 && MEM_ALIAS_SET (mem) != 0);
428 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
429 such an entry, or NULL otherwise. */
431 static inline alias_set_entry
432 get_alias_set_entry (alias_set_type alias_set)
434 return (*alias_sets)[alias_set];
437 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
438 the two MEMs cannot alias each other. */
440 static inline int
441 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
443 return (flag_strict_aliasing
444 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
445 MEM_ALIAS_SET (mem2)));
448 /* Return true if the first alias set is a subset of the second. */
450 bool
451 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
453 alias_set_entry ase2;
455 /* Everything is a subset of the "aliases everything" set. */
456 if (set2 == 0)
457 return true;
459 /* Check if set1 is a subset of set2. */
460 ase2 = get_alias_set_entry (set2);
461 if (ase2 != 0
462 && (ase2->has_zero_child
463 || (ase2->children && ase2->children->get (set1))))
464 return true;
466 /* As a special case we consider alias set of "void *" to be both subset
467 and superset of every alias set of a pointer. This extra symmetry does
468 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
469 to return true on the following testcase:
471 void *ptr;
472 char **ptr2=(char **)&ptr;
473 *ptr2 = ...
475 Additionally if a set contains universal pointer, we consider every pointer
476 to be a subset of it, but we do not represent this explicitely - doing so
477 would require us to update transitive closure each time we introduce new
478 pointer type. This makes aliasing_component_refs_p to return true
479 on the following testcase:
481 struct a {void *ptr;}
482 char **ptr = (char **)&a.ptr;
483 ptr = ...
485 This makes void * truly universal pointer type. See pointer handling in
486 get_alias_set for more details. */
487 if (ase2 && ase2->has_pointer)
489 alias_set_entry ase1 = get_alias_set_entry (set1);
491 if (ase1 && ase1->is_pointer)
493 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
494 /* If one is ptr_type_node and other is pointer, then we consider
495 them subset of each other. */
496 if (set1 == voidptr_set || set2 == voidptr_set)
497 return true;
498 /* If SET2 contains universal pointer's alias set, then we consdier
499 every (non-universal) pointer. */
500 if (ase2->children && set1 != voidptr_set
501 && ase2->children->get (voidptr_set))
502 return true;
505 return false;
508 /* Return 1 if the two specified alias sets may conflict. */
511 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
513 alias_set_entry ase1;
514 alias_set_entry ase2;
516 /* The easy case. */
517 if (alias_sets_must_conflict_p (set1, set2))
518 return 1;
520 /* See if the first alias set is a subset of the second. */
521 ase1 = get_alias_set_entry (set1);
522 if (ase1 != 0
523 && ase1->children && ase1->children->get (set2))
525 ++alias_stats.num_dag;
526 return 1;
529 /* Now do the same, but with the alias sets reversed. */
530 ase2 = get_alias_set_entry (set2);
531 if (ase2 != 0
532 && ase2->children && ase2->children->get (set1))
534 ++alias_stats.num_dag;
535 return 1;
538 /* We want void * to be compatible with any other pointer without
539 really dropping it to alias set 0. Doing so would make it
540 compatible with all non-pointer types too.
542 This is not strictly necessary by the C/C++ language
543 standards, but avoids common type punning mistakes. In
544 addition to that, we need the existence of such universal
545 pointer to implement Fortran's C_PTR type (which is defined as
546 type compatible with all C pointers). */
547 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
549 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
551 /* If one of the sets corresponds to universal pointer,
552 we consider it to conflict with anything that is
553 or contains pointer. */
554 if (set1 == voidptr_set || set2 == voidptr_set)
556 ++alias_stats.num_universal;
557 return true;
559 /* If one of sets is (non-universal) pointer and the other
560 contains universal pointer, we also get conflict. */
561 if (ase1->is_pointer && set2 != voidptr_set
562 && ase2->children && ase2->children->get (voidptr_set))
564 ++alias_stats.num_universal;
565 return true;
567 if (ase2->is_pointer && set1 != voidptr_set
568 && ase1->children && ase1->children->get (voidptr_set))
570 ++alias_stats.num_universal;
571 return true;
575 ++alias_stats.num_disambiguated;
577 /* The two alias sets are distinct and neither one is the
578 child of the other. Therefore, they cannot conflict. */
579 return 0;
582 /* Return 1 if the two specified alias sets will always conflict. */
585 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
587 if (set1 == 0 || set2 == 0)
589 ++alias_stats.num_alias_zero;
590 return 1;
592 if (set1 == set2)
594 ++alias_stats.num_same_alias_set;
595 return 1;
598 return 0;
601 /* Return 1 if any MEM object of type T1 will always conflict (using the
602 dependency routines in this file) with any MEM object of type T2.
603 This is used when allocating temporary storage. If T1 and/or T2 are
604 NULL_TREE, it means we know nothing about the storage. */
607 objects_must_conflict_p (tree t1, tree t2)
609 alias_set_type set1, set2;
611 /* If neither has a type specified, we don't know if they'll conflict
612 because we may be using them to store objects of various types, for
613 example the argument and local variables areas of inlined functions. */
614 if (t1 == 0 && t2 == 0)
615 return 0;
617 /* If they are the same type, they must conflict. */
618 if (t1 == t2)
620 ++alias_stats.num_same_objects;
621 return 1;
623 /* Likewise if both are volatile. */
624 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
626 ++alias_stats.num_volatile;
627 return 1;
630 set1 = t1 ? get_alias_set (t1) : 0;
631 set2 = t2 ? get_alias_set (t2) : 0;
633 /* We can't use alias_sets_conflict_p because we must make sure
634 that every subtype of t1 will conflict with every subtype of
635 t2 for which a pair of subobjects of these respective subtypes
636 overlaps on the stack. */
637 return alias_sets_must_conflict_p (set1, set2);
640 /* Return the outermost parent of component present in the chain of
641 component references handled by get_inner_reference in T with the
642 following property:
643 - the component is non-addressable, or
644 - the parent has alias set zero,
645 or NULL_TREE if no such parent exists. In the former cases, the alias
646 set of this parent is the alias set that must be used for T itself. */
648 tree
649 component_uses_parent_alias_set_from (const_tree t)
651 const_tree found = NULL_TREE;
653 while (handled_component_p (t))
655 switch (TREE_CODE (t))
657 case COMPONENT_REF:
658 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
659 found = t;
660 break;
662 case ARRAY_REF:
663 case ARRAY_RANGE_REF:
664 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
665 found = t;
666 break;
668 case REALPART_EXPR:
669 case IMAGPART_EXPR:
670 break;
672 case BIT_FIELD_REF:
673 case VIEW_CONVERT_EXPR:
674 /* Bitfields and casts are never addressable. */
675 found = t;
676 break;
678 default:
679 gcc_unreachable ();
682 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
683 found = t;
685 t = TREE_OPERAND (t, 0);
688 if (found)
689 return TREE_OPERAND (found, 0);
691 return NULL_TREE;
695 /* Return whether the pointer-type T effective for aliasing may
696 access everything and thus the reference has to be assigned
697 alias-set zero. */
699 static bool
700 ref_all_alias_ptr_type_p (const_tree t)
702 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
703 || TYPE_REF_CAN_ALIAS_ALL (t));
706 /* Return the alias set for the memory pointed to by T, which may be
707 either a type or an expression. Return -1 if there is nothing
708 special about dereferencing T. */
710 static alias_set_type
711 get_deref_alias_set_1 (tree t)
713 /* All we care about is the type. */
714 if (! TYPE_P (t))
715 t = TREE_TYPE (t);
717 /* If we have an INDIRECT_REF via a void pointer, we don't
718 know anything about what that might alias. Likewise if the
719 pointer is marked that way. */
720 if (ref_all_alias_ptr_type_p (t))
721 return 0;
723 return -1;
726 /* Return the alias set for the memory pointed to by T, which may be
727 either a type or an expression. */
729 alias_set_type
730 get_deref_alias_set (tree t)
732 /* If we're not doing any alias analysis, just assume everything
733 aliases everything else. */
734 if (!flag_strict_aliasing)
735 return 0;
737 alias_set_type set = get_deref_alias_set_1 (t);
739 /* Fall back to the alias-set of the pointed-to type. */
740 if (set == -1)
742 if (! TYPE_P (t))
743 t = TREE_TYPE (t);
744 set = get_alias_set (TREE_TYPE (t));
747 return set;
750 /* Return the pointer-type relevant for TBAA purposes from the
751 memory reference tree *T or NULL_TREE in which case *T is
752 adjusted to point to the outermost component reference that
753 can be used for assigning an alias set. */
755 static tree
756 reference_alias_ptr_type_1 (tree *t)
758 tree inner;
760 /* Get the base object of the reference. */
761 inner = *t;
762 while (handled_component_p (inner))
764 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
765 the type of any component references that wrap it to
766 determine the alias-set. */
767 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
768 *t = TREE_OPERAND (inner, 0);
769 inner = TREE_OPERAND (inner, 0);
772 /* Handle pointer dereferences here, they can override the
773 alias-set. */
774 if (INDIRECT_REF_P (inner)
775 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
776 return TREE_TYPE (TREE_OPERAND (inner, 0));
777 else if (TREE_CODE (inner) == TARGET_MEM_REF)
778 return TREE_TYPE (TMR_OFFSET (inner));
779 else if (TREE_CODE (inner) == MEM_REF
780 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
781 return TREE_TYPE (TREE_OPERAND (inner, 1));
783 /* If the innermost reference is a MEM_REF that has a
784 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
785 using the memory access type for determining the alias-set. */
786 if (TREE_CODE (inner) == MEM_REF
787 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
788 != TYPE_MAIN_VARIANT
789 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
790 return TREE_TYPE (TREE_OPERAND (inner, 1));
792 /* Otherwise, pick up the outermost object that we could have
793 a pointer to. */
794 tree tem = component_uses_parent_alias_set_from (*t);
795 if (tem)
796 *t = tem;
798 return NULL_TREE;
801 /* Return the pointer-type relevant for TBAA purposes from the
802 gimple memory reference tree T. This is the type to be used for
803 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
804 and guarantees that get_alias_set will return the same alias
805 set for T and the replacement. */
807 tree
808 reference_alias_ptr_type (tree t)
810 tree ptype = reference_alias_ptr_type_1 (&t);
811 /* If there is a given pointer type for aliasing purposes, return it. */
812 if (ptype != NULL_TREE)
813 return ptype;
815 /* Otherwise build one from the outermost component reference we
816 may use. */
817 if (TREE_CODE (t) == MEM_REF
818 || TREE_CODE (t) == TARGET_MEM_REF)
819 return TREE_TYPE (TREE_OPERAND (t, 1));
820 else
821 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
824 /* Return whether the pointer-types T1 and T2 used to determine
825 two alias sets of two references will yield the same answer
826 from get_deref_alias_set. */
828 bool
829 alias_ptr_types_compatible_p (tree t1, tree t2)
831 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
832 return true;
834 if (ref_all_alias_ptr_type_p (t1)
835 || ref_all_alias_ptr_type_p (t2))
836 return false;
838 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
839 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
842 /* Create emptry alias set entry. */
844 alias_set_entry
845 init_alias_set_entry (alias_set_type set)
847 alias_set_entry ase = ggc_alloc<alias_set_entry_d> ();
848 ase->alias_set = set;
849 ase->children = NULL;
850 ase->has_zero_child = false;
851 ase->is_pointer = false;
852 ase->has_pointer = false;
853 gcc_checking_assert (!get_alias_set_entry (set));
854 (*alias_sets)[set] = ase;
855 return ase;
858 /* Return the alias set for T, which may be either a type or an
859 expression. Call language-specific routine for help, if needed. */
861 alias_set_type
862 get_alias_set (tree t)
864 alias_set_type set;
866 /* If we're not doing any alias analysis, just assume everything
867 aliases everything else. Also return 0 if this or its type is
868 an error. */
869 if (! flag_strict_aliasing || t == error_mark_node
870 || (! TYPE_P (t)
871 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
872 return 0;
874 /* We can be passed either an expression or a type. This and the
875 language-specific routine may make mutually-recursive calls to each other
876 to figure out what to do. At each juncture, we see if this is a tree
877 that the language may need to handle specially. First handle things that
878 aren't types. */
879 if (! TYPE_P (t))
881 /* Give the language a chance to do something with this tree
882 before we look at it. */
883 STRIP_NOPS (t);
884 set = lang_hooks.get_alias_set (t);
885 if (set != -1)
886 return set;
888 /* Get the alias pointer-type to use or the outermost object
889 that we could have a pointer to. */
890 tree ptype = reference_alias_ptr_type_1 (&t);
891 if (ptype != NULL)
892 return get_deref_alias_set (ptype);
894 /* If we've already determined the alias set for a decl, just return
895 it. This is necessary for C++ anonymous unions, whose component
896 variables don't look like union members (boo!). */
897 if (TREE_CODE (t) == VAR_DECL
898 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
899 return MEM_ALIAS_SET (DECL_RTL (t));
901 /* Now all we care about is the type. */
902 t = TREE_TYPE (t);
905 /* Variant qualifiers don't affect the alias set, so get the main
906 variant. */
907 t = TYPE_MAIN_VARIANT (t);
909 /* Always use the canonical type as well. If this is a type that
910 requires structural comparisons to identify compatible types
911 use alias set zero. */
912 if (TYPE_STRUCTURAL_EQUALITY_P (t))
914 /* Allow the language to specify another alias set for this
915 type. */
916 set = lang_hooks.get_alias_set (t);
917 if (set != -1)
918 return set;
919 return 0;
922 t = TYPE_CANONICAL (t);
924 /* The canonical type should not require structural equality checks. */
925 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
927 /* If this is a type with a known alias set, return it. */
928 if (TYPE_ALIAS_SET_KNOWN_P (t))
929 return TYPE_ALIAS_SET (t);
931 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
932 if (!COMPLETE_TYPE_P (t))
934 /* For arrays with unknown size the conservative answer is the
935 alias set of the element type. */
936 if (TREE_CODE (t) == ARRAY_TYPE)
937 return get_alias_set (TREE_TYPE (t));
939 /* But return zero as a conservative answer for incomplete types. */
940 return 0;
943 /* See if the language has special handling for this type. */
944 set = lang_hooks.get_alias_set (t);
945 if (set != -1)
946 return set;
948 /* There are no objects of FUNCTION_TYPE, so there's no point in
949 using up an alias set for them. (There are, of course, pointers
950 and references to functions, but that's different.) */
951 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
952 set = 0;
954 /* Unless the language specifies otherwise, let vector types alias
955 their components. This avoids some nasty type punning issues in
956 normal usage. And indeed lets vectors be treated more like an
957 array slice. */
958 else if (TREE_CODE (t) == VECTOR_TYPE)
959 set = get_alias_set (TREE_TYPE (t));
961 /* Unless the language specifies otherwise, treat array types the
962 same as their components. This avoids the asymmetry we get
963 through recording the components. Consider accessing a
964 character(kind=1) through a reference to a character(kind=1)[1:1].
965 Or consider if we want to assign integer(kind=4)[0:D.1387] and
966 integer(kind=4)[4] the same alias set or not.
967 Just be pragmatic here and make sure the array and its element
968 type get the same alias set assigned. */
969 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
970 set = get_alias_set (TREE_TYPE (t));
972 /* From the former common C and C++ langhook implementation:
974 Unfortunately, there is no canonical form of a pointer type.
975 In particular, if we have `typedef int I', then `int *', and
976 `I *' are different types. So, we have to pick a canonical
977 representative. We do this below.
979 Technically, this approach is actually more conservative that
980 it needs to be. In particular, `const int *' and `int *'
981 should be in different alias sets, according to the C and C++
982 standard, since their types are not the same, and so,
983 technically, an `int **' and `const int **' cannot point at
984 the same thing.
986 But, the standard is wrong. In particular, this code is
987 legal C++:
989 int *ip;
990 int **ipp = &ip;
991 const int* const* cipp = ipp;
992 And, it doesn't make sense for that to be legal unless you
993 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
994 the pointed-to types. This issue has been reported to the
995 C++ committee.
997 For this reason go to canonical type of the unqalified pointer type.
998 Until GCC 6 this code set all pointers sets to have alias set of
999 ptr_type_node but that is a bad idea, because it prevents disabiguations
1000 in between pointers. For Firefox this accounts about 20% of all
1001 disambiguations in the program. */
1002 else if (POINTER_TYPE_P (t) && t != ptr_type_node && !in_lto_p)
1004 tree p;
1005 auto_vec <bool, 8> reference;
1007 /* Unnest all pointers and references.
1008 We also want to make pointer to array equivalent to pointer to its
1009 element. So skip all array types, too. */
1010 for (p = t; POINTER_TYPE_P (p)
1011 || (TREE_CODE (p) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (p));
1012 p = TREE_TYPE (p))
1014 if (TREE_CODE (p) == REFERENCE_TYPE)
1015 reference.safe_push (true);
1016 if (TREE_CODE (p) == POINTER_TYPE)
1017 reference.safe_push (false);
1019 p = TYPE_MAIN_VARIANT (p);
1021 /* Make void * compatible with char * and also void **.
1022 Programs are commonly violating TBAA by this.
1024 We also make void * to conflict with every pointer
1025 (see record_component_aliases) and thus it is safe it to use it for
1026 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
1027 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
1028 set = get_alias_set (ptr_type_node);
1029 else
1031 /* Rebuild pointer type from starting from canonical types using
1032 unqualified pointers and references only. This way all such
1033 pointers will have the same alias set and will conflict with
1034 each other.
1036 Most of time we already have pointers or references of a given type.
1037 If not we build new one just to be sure that if someone later
1038 (probably only middle-end can, as we should assign all alias
1039 classes only after finishing translation unit) builds the pointer
1040 type, the canonical type will match. */
1041 p = TYPE_CANONICAL (p);
1042 while (!reference.is_empty ())
1044 if (reference.pop ())
1045 p = build_reference_type (p);
1046 else
1047 p = build_pointer_type (p);
1048 p = TYPE_CANONICAL (TYPE_MAIN_VARIANT (p));
1050 gcc_checking_assert (TYPE_CANONICAL (p) == p);
1052 /* Assign the alias set to both p and t.
1053 We can not call get_alias_set (p) here as that would trigger
1054 infinite recursion when p == t. In other cases it would just
1055 trigger unnecesary legwork of rebuilding the pointer again. */
1056 if (TYPE_ALIAS_SET_KNOWN_P (p))
1057 set = TYPE_ALIAS_SET (p);
1058 else
1060 set = new_alias_set ();
1061 TYPE_ALIAS_SET (p) = set;
1065 /* In LTO the rules above needs to be part of canonical type machinery.
1066 For now just punt. */
1067 else if (POINTER_TYPE_P (t)
1068 && t != TYPE_CANONICAL (ptr_type_node) && in_lto_p)
1069 set = get_alias_set (TYPE_CANONICAL (ptr_type_node));
1071 /* Otherwise make a new alias set for this type. */
1072 else
1074 /* Each canonical type gets its own alias set, so canonical types
1075 shouldn't form a tree. It doesn't really matter for types
1076 we handle specially above, so only check it where it possibly
1077 would result in a bogus alias set. */
1078 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1080 set = new_alias_set ();
1083 TYPE_ALIAS_SET (t) = set;
1085 /* If this is an aggregate type or a complex type, we must record any
1086 component aliasing information. */
1087 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1088 record_component_aliases (t);
1090 /* We treat pointer types specially in alias_set_subset_of. */
1091 if (POINTER_TYPE_P (t) && set)
1093 alias_set_entry ase = get_alias_set_entry (set);
1094 if (!ase)
1095 ase = init_alias_set_entry (set);
1096 ase->is_pointer = true;
1097 ase->has_pointer = true;
1100 return set;
1103 /* Return a brand-new alias set. */
1105 alias_set_type
1106 new_alias_set (void)
1108 if (flag_strict_aliasing)
1110 if (alias_sets == 0)
1111 vec_safe_push (alias_sets, (alias_set_entry) 0);
1112 vec_safe_push (alias_sets, (alias_set_entry) 0);
1113 return alias_sets->length () - 1;
1115 else
1116 return 0;
1119 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1120 not everything that aliases SUPERSET also aliases SUBSET. For example,
1121 in C, a store to an `int' can alias a load of a structure containing an
1122 `int', and vice versa. But it can't alias a load of a 'double' member
1123 of the same structure. Here, the structure would be the SUPERSET and
1124 `int' the SUBSET. This relationship is also described in the comment at
1125 the beginning of this file.
1127 This function should be called only once per SUPERSET/SUBSET pair.
1129 It is illegal for SUPERSET to be zero; everything is implicitly a
1130 subset of alias set zero. */
1132 void
1133 record_alias_subset (alias_set_type superset, alias_set_type subset)
1135 alias_set_entry superset_entry;
1136 alias_set_entry subset_entry;
1138 /* It is possible in complex type situations for both sets to be the same,
1139 in which case we can ignore this operation. */
1140 if (superset == subset)
1141 return;
1143 gcc_assert (superset);
1145 superset_entry = get_alias_set_entry (superset);
1146 if (superset_entry == 0)
1148 /* Create an entry for the SUPERSET, so that we have a place to
1149 attach the SUBSET. */
1150 superset_entry = init_alias_set_entry (superset);
1153 if (subset == 0)
1154 superset_entry->has_zero_child = 1;
1155 else
1157 subset_entry = get_alias_set_entry (subset);
1158 if (!superset_entry->children)
1159 superset_entry->children
1160 = hash_map<int, int, alias_set_traits>::create_ggc (64);
1161 /* If there is an entry for the subset, enter all of its children
1162 (if they are not already present) as children of the SUPERSET. */
1163 if (subset_entry)
1165 if (subset_entry->has_zero_child)
1166 superset_entry->has_zero_child = true;
1167 if (subset_entry->has_pointer)
1168 superset_entry->has_pointer = true;
1170 if (subset_entry->children)
1172 hash_map<int, int, alias_set_traits>::iterator iter
1173 = subset_entry->children->begin ();
1174 for (; iter != subset_entry->children->end (); ++iter)
1175 superset_entry->children->put ((*iter).first, (*iter).second);
1179 /* Enter the SUBSET itself as a child of the SUPERSET. */
1180 superset_entry->children->put (subset, 0);
1184 /* Record that component types of TYPE, if any, are part of that type for
1185 aliasing purposes. For record types, we only record component types
1186 for fields that are not marked non-addressable. For array types, we
1187 only record the component type if it is not marked non-aliased. */
1189 void
1190 record_component_aliases (tree type)
1192 alias_set_type superset = get_alias_set (type);
1193 tree field;
1195 if (superset == 0)
1196 return;
1198 switch (TREE_CODE (type))
1200 case RECORD_TYPE:
1201 case UNION_TYPE:
1202 case QUAL_UNION_TYPE:
1203 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1204 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1205 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1206 break;
1208 case COMPLEX_TYPE:
1209 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1210 break;
1212 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1213 element type. */
1215 default:
1216 break;
1220 /* Allocate an alias set for use in storing and reading from the varargs
1221 spill area. */
1223 static GTY(()) alias_set_type varargs_set = -1;
1225 alias_set_type
1226 get_varargs_alias_set (void)
1228 #if 1
1229 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1230 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1231 consistently use the varargs alias set for loads from the varargs
1232 area. So don't use it anywhere. */
1233 return 0;
1234 #else
1235 if (varargs_set == -1)
1236 varargs_set = new_alias_set ();
1238 return varargs_set;
1239 #endif
1242 /* Likewise, but used for the fixed portions of the frame, e.g., register
1243 save areas. */
1245 static GTY(()) alias_set_type frame_set = -1;
1247 alias_set_type
1248 get_frame_alias_set (void)
1250 if (frame_set == -1)
1251 frame_set = new_alias_set ();
1253 return frame_set;
1256 /* Create a new, unique base with id ID. */
1258 static rtx
1259 unique_base_value (HOST_WIDE_INT id)
1261 return gen_rtx_ADDRESS (Pmode, id);
1264 /* Return true if accesses based on any other base value cannot alias
1265 those based on X. */
1267 static bool
1268 unique_base_value_p (rtx x)
1270 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1273 /* Return true if X is known to be a base value. */
1275 static bool
1276 known_base_value_p (rtx x)
1278 switch (GET_CODE (x))
1280 case LABEL_REF:
1281 case SYMBOL_REF:
1282 return true;
1284 case ADDRESS:
1285 /* Arguments may or may not be bases; we don't know for sure. */
1286 return GET_MODE (x) != VOIDmode;
1288 default:
1289 return false;
1293 /* Inside SRC, the source of a SET, find a base address. */
1295 static rtx
1296 find_base_value (rtx src)
1298 unsigned int regno;
1300 #if defined (FIND_BASE_TERM)
1301 /* Try machine-dependent ways to find the base term. */
1302 src = FIND_BASE_TERM (src);
1303 #endif
1305 switch (GET_CODE (src))
1307 case SYMBOL_REF:
1308 case LABEL_REF:
1309 return src;
1311 case REG:
1312 regno = REGNO (src);
1313 /* At the start of a function, argument registers have known base
1314 values which may be lost later. Returning an ADDRESS
1315 expression here allows optimization based on argument values
1316 even when the argument registers are used for other purposes. */
1317 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1318 return new_reg_base_value[regno];
1320 /* If a pseudo has a known base value, return it. Do not do this
1321 for non-fixed hard regs since it can result in a circular
1322 dependency chain for registers which have values at function entry.
1324 The test above is not sufficient because the scheduler may move
1325 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1326 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1327 && regno < vec_safe_length (reg_base_value))
1329 /* If we're inside init_alias_analysis, use new_reg_base_value
1330 to reduce the number of relaxation iterations. */
1331 if (new_reg_base_value && new_reg_base_value[regno]
1332 && DF_REG_DEF_COUNT (regno) == 1)
1333 return new_reg_base_value[regno];
1335 if ((*reg_base_value)[regno])
1336 return (*reg_base_value)[regno];
1339 return 0;
1341 case MEM:
1342 /* Check for an argument passed in memory. Only record in the
1343 copying-arguments block; it is too hard to track changes
1344 otherwise. */
1345 if (copying_arguments
1346 && (XEXP (src, 0) == arg_pointer_rtx
1347 || (GET_CODE (XEXP (src, 0)) == PLUS
1348 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1349 return arg_base_value;
1350 return 0;
1352 case CONST:
1353 src = XEXP (src, 0);
1354 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1355 break;
1357 /* ... fall through ... */
1359 case PLUS:
1360 case MINUS:
1362 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1364 /* If either operand is a REG that is a known pointer, then it
1365 is the base. */
1366 if (REG_P (src_0) && REG_POINTER (src_0))
1367 return find_base_value (src_0);
1368 if (REG_P (src_1) && REG_POINTER (src_1))
1369 return find_base_value (src_1);
1371 /* If either operand is a REG, then see if we already have
1372 a known value for it. */
1373 if (REG_P (src_0))
1375 temp = find_base_value (src_0);
1376 if (temp != 0)
1377 src_0 = temp;
1380 if (REG_P (src_1))
1382 temp = find_base_value (src_1);
1383 if (temp!= 0)
1384 src_1 = temp;
1387 /* If either base is named object or a special address
1388 (like an argument or stack reference), then use it for the
1389 base term. */
1390 if (src_0 != 0 && known_base_value_p (src_0))
1391 return src_0;
1393 if (src_1 != 0 && known_base_value_p (src_1))
1394 return src_1;
1396 /* Guess which operand is the base address:
1397 If either operand is a symbol, then it is the base. If
1398 either operand is a CONST_INT, then the other is the base. */
1399 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1400 return find_base_value (src_0);
1401 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1402 return find_base_value (src_1);
1404 return 0;
1407 case LO_SUM:
1408 /* The standard form is (lo_sum reg sym) so look only at the
1409 second operand. */
1410 return find_base_value (XEXP (src, 1));
1412 case AND:
1413 /* If the second operand is constant set the base
1414 address to the first operand. */
1415 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1416 return find_base_value (XEXP (src, 0));
1417 return 0;
1419 case TRUNCATE:
1420 /* As we do not know which address space the pointer is referring to, we can
1421 handle this only if the target does not support different pointer or
1422 address modes depending on the address space. */
1423 if (!target_default_pointer_address_modes_p ())
1424 break;
1425 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1426 break;
1427 /* Fall through. */
1428 case HIGH:
1429 case PRE_INC:
1430 case PRE_DEC:
1431 case POST_INC:
1432 case POST_DEC:
1433 case PRE_MODIFY:
1434 case POST_MODIFY:
1435 return find_base_value (XEXP (src, 0));
1437 case ZERO_EXTEND:
1438 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1439 /* As we do not know which address space the pointer is referring to, we can
1440 handle this only if the target does not support different pointer or
1441 address modes depending on the address space. */
1442 if (!target_default_pointer_address_modes_p ())
1443 break;
1446 rtx temp = find_base_value (XEXP (src, 0));
1448 if (temp != 0 && CONSTANT_P (temp))
1449 temp = convert_memory_address (Pmode, temp);
1451 return temp;
1454 default:
1455 break;
1458 return 0;
1461 /* Called from init_alias_analysis indirectly through note_stores,
1462 or directly if DEST is a register with a REG_NOALIAS note attached.
1463 SET is null in the latter case. */
1465 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1466 register N has been set in this function. */
1467 static sbitmap reg_seen;
1469 static void
1470 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1472 unsigned regno;
1473 rtx src;
1474 int n;
1476 if (!REG_P (dest))
1477 return;
1479 regno = REGNO (dest);
1481 gcc_checking_assert (regno < reg_base_value->length ());
1483 n = REG_NREGS (dest);
1484 if (n != 1)
1486 while (--n >= 0)
1488 bitmap_set_bit (reg_seen, regno + n);
1489 new_reg_base_value[regno + n] = 0;
1491 return;
1494 if (set)
1496 /* A CLOBBER wipes out any old value but does not prevent a previously
1497 unset register from acquiring a base address (i.e. reg_seen is not
1498 set). */
1499 if (GET_CODE (set) == CLOBBER)
1501 new_reg_base_value[regno] = 0;
1502 return;
1504 src = SET_SRC (set);
1506 else
1508 /* There's a REG_NOALIAS note against DEST. */
1509 if (bitmap_bit_p (reg_seen, regno))
1511 new_reg_base_value[regno] = 0;
1512 return;
1514 bitmap_set_bit (reg_seen, regno);
1515 new_reg_base_value[regno] = unique_base_value (unique_id++);
1516 return;
1519 /* If this is not the first set of REGNO, see whether the new value
1520 is related to the old one. There are two cases of interest:
1522 (1) The register might be assigned an entirely new value
1523 that has the same base term as the original set.
1525 (2) The set might be a simple self-modification that
1526 cannot change REGNO's base value.
1528 If neither case holds, reject the original base value as invalid.
1529 Note that the following situation is not detected:
1531 extern int x, y; int *p = &x; p += (&y-&x);
1533 ANSI C does not allow computing the difference of addresses
1534 of distinct top level objects. */
1535 if (new_reg_base_value[regno] != 0
1536 && find_base_value (src) != new_reg_base_value[regno])
1537 switch (GET_CODE (src))
1539 case LO_SUM:
1540 case MINUS:
1541 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1542 new_reg_base_value[regno] = 0;
1543 break;
1544 case PLUS:
1545 /* If the value we add in the PLUS is also a valid base value,
1546 this might be the actual base value, and the original value
1547 an index. */
1549 rtx other = NULL_RTX;
1551 if (XEXP (src, 0) == dest)
1552 other = XEXP (src, 1);
1553 else if (XEXP (src, 1) == dest)
1554 other = XEXP (src, 0);
1556 if (! other || find_base_value (other))
1557 new_reg_base_value[regno] = 0;
1558 break;
1560 case AND:
1561 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1562 new_reg_base_value[regno] = 0;
1563 break;
1564 default:
1565 new_reg_base_value[regno] = 0;
1566 break;
1568 /* If this is the first set of a register, record the value. */
1569 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1570 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1571 new_reg_base_value[regno] = find_base_value (src);
1573 bitmap_set_bit (reg_seen, regno);
1576 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1577 using hard registers with non-null REG_BASE_VALUE for renaming. */
1579 get_reg_base_value (unsigned int regno)
1581 return (*reg_base_value)[regno];
1584 /* If a value is known for REGNO, return it. */
1587 get_reg_known_value (unsigned int regno)
1589 if (regno >= FIRST_PSEUDO_REGISTER)
1591 regno -= FIRST_PSEUDO_REGISTER;
1592 if (regno < vec_safe_length (reg_known_value))
1593 return (*reg_known_value)[regno];
1595 return NULL;
1598 /* Set it. */
1600 static void
1601 set_reg_known_value (unsigned int regno, rtx val)
1603 if (regno >= FIRST_PSEUDO_REGISTER)
1605 regno -= FIRST_PSEUDO_REGISTER;
1606 if (regno < vec_safe_length (reg_known_value))
1607 (*reg_known_value)[regno] = val;
1611 /* Similarly for reg_known_equiv_p. */
1613 bool
1614 get_reg_known_equiv_p (unsigned int regno)
1616 if (regno >= FIRST_PSEUDO_REGISTER)
1618 regno -= FIRST_PSEUDO_REGISTER;
1619 if (regno < vec_safe_length (reg_known_value))
1620 return bitmap_bit_p (reg_known_equiv_p, regno);
1622 return false;
1625 static void
1626 set_reg_known_equiv_p (unsigned int regno, bool val)
1628 if (regno >= FIRST_PSEUDO_REGISTER)
1630 regno -= FIRST_PSEUDO_REGISTER;
1631 if (regno < vec_safe_length (reg_known_value))
1633 if (val)
1634 bitmap_set_bit (reg_known_equiv_p, regno);
1635 else
1636 bitmap_clear_bit (reg_known_equiv_p, regno);
1642 /* Returns a canonical version of X, from the point of view alias
1643 analysis. (For example, if X is a MEM whose address is a register,
1644 and the register has a known value (say a SYMBOL_REF), then a MEM
1645 whose address is the SYMBOL_REF is returned.) */
1648 canon_rtx (rtx x)
1650 /* Recursively look for equivalences. */
1651 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1653 rtx t = get_reg_known_value (REGNO (x));
1654 if (t == x)
1655 return x;
1656 if (t)
1657 return canon_rtx (t);
1660 if (GET_CODE (x) == PLUS)
1662 rtx x0 = canon_rtx (XEXP (x, 0));
1663 rtx x1 = canon_rtx (XEXP (x, 1));
1665 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1667 if (CONST_INT_P (x0))
1668 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1669 else if (CONST_INT_P (x1))
1670 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1671 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1675 /* This gives us much better alias analysis when called from
1676 the loop optimizer. Note we want to leave the original
1677 MEM alone, but need to return the canonicalized MEM with
1678 all the flags with their original values. */
1679 else if (MEM_P (x))
1680 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1682 return x;
1685 /* Return 1 if X and Y are identical-looking rtx's.
1686 Expect that X and Y has been already canonicalized.
1688 We use the data in reg_known_value above to see if two registers with
1689 different numbers are, in fact, equivalent. */
1691 static int
1692 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1694 int i;
1695 int j;
1696 enum rtx_code code;
1697 const char *fmt;
1699 if (x == 0 && y == 0)
1700 return 1;
1701 if (x == 0 || y == 0)
1702 return 0;
1704 if (x == y)
1705 return 1;
1707 code = GET_CODE (x);
1708 /* Rtx's of different codes cannot be equal. */
1709 if (code != GET_CODE (y))
1710 return 0;
1712 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1713 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1715 if (GET_MODE (x) != GET_MODE (y))
1716 return 0;
1718 /* Some RTL can be compared without a recursive examination. */
1719 switch (code)
1721 case REG:
1722 return REGNO (x) == REGNO (y);
1724 case LABEL_REF:
1725 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1727 case SYMBOL_REF:
1728 return XSTR (x, 0) == XSTR (y, 0);
1730 case ENTRY_VALUE:
1731 /* This is magic, don't go through canonicalization et al. */
1732 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1734 case VALUE:
1735 CASE_CONST_UNIQUE:
1736 /* Pointer equality guarantees equality for these nodes. */
1737 return 0;
1739 default:
1740 break;
1743 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1744 if (code == PLUS)
1745 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1746 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1747 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1748 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1749 /* For commutative operations, the RTX match if the operand match in any
1750 order. Also handle the simple binary and unary cases without a loop. */
1751 if (COMMUTATIVE_P (x))
1753 rtx xop0 = canon_rtx (XEXP (x, 0));
1754 rtx yop0 = canon_rtx (XEXP (y, 0));
1755 rtx yop1 = canon_rtx (XEXP (y, 1));
1757 return ((rtx_equal_for_memref_p (xop0, yop0)
1758 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1759 || (rtx_equal_for_memref_p (xop0, yop1)
1760 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1762 else if (NON_COMMUTATIVE_P (x))
1764 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1765 canon_rtx (XEXP (y, 0)))
1766 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1767 canon_rtx (XEXP (y, 1))));
1769 else if (UNARY_P (x))
1770 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1771 canon_rtx (XEXP (y, 0)));
1773 /* Compare the elements. If any pair of corresponding elements
1774 fail to match, return 0 for the whole things.
1776 Limit cases to types which actually appear in addresses. */
1778 fmt = GET_RTX_FORMAT (code);
1779 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1781 switch (fmt[i])
1783 case 'i':
1784 if (XINT (x, i) != XINT (y, i))
1785 return 0;
1786 break;
1788 case 'E':
1789 /* Two vectors must have the same length. */
1790 if (XVECLEN (x, i) != XVECLEN (y, i))
1791 return 0;
1793 /* And the corresponding elements must match. */
1794 for (j = 0; j < XVECLEN (x, i); j++)
1795 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1796 canon_rtx (XVECEXP (y, i, j))) == 0)
1797 return 0;
1798 break;
1800 case 'e':
1801 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1802 canon_rtx (XEXP (y, i))) == 0)
1803 return 0;
1804 break;
1806 /* This can happen for asm operands. */
1807 case 's':
1808 if (strcmp (XSTR (x, i), XSTR (y, i)))
1809 return 0;
1810 break;
1812 /* This can happen for an asm which clobbers memory. */
1813 case '0':
1814 break;
1816 /* It is believed that rtx's at this level will never
1817 contain anything but integers and other rtx's,
1818 except for within LABEL_REFs and SYMBOL_REFs. */
1819 default:
1820 gcc_unreachable ();
1823 return 1;
1826 static rtx
1827 find_base_term (rtx x)
1829 cselib_val *val;
1830 struct elt_loc_list *l, *f;
1831 rtx ret;
1833 #if defined (FIND_BASE_TERM)
1834 /* Try machine-dependent ways to find the base term. */
1835 x = FIND_BASE_TERM (x);
1836 #endif
1838 switch (GET_CODE (x))
1840 case REG:
1841 return REG_BASE_VALUE (x);
1843 case TRUNCATE:
1844 /* As we do not know which address space the pointer is referring to, we can
1845 handle this only if the target does not support different pointer or
1846 address modes depending on the address space. */
1847 if (!target_default_pointer_address_modes_p ())
1848 return 0;
1849 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1850 return 0;
1851 /* Fall through. */
1852 case HIGH:
1853 case PRE_INC:
1854 case PRE_DEC:
1855 case POST_INC:
1856 case POST_DEC:
1857 case PRE_MODIFY:
1858 case POST_MODIFY:
1859 return find_base_term (XEXP (x, 0));
1861 case ZERO_EXTEND:
1862 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1863 /* As we do not know which address space the pointer is referring to, we can
1864 handle this only if the target does not support different pointer or
1865 address modes depending on the address space. */
1866 if (!target_default_pointer_address_modes_p ())
1867 return 0;
1870 rtx temp = find_base_term (XEXP (x, 0));
1872 if (temp != 0 && CONSTANT_P (temp))
1873 temp = convert_memory_address (Pmode, temp);
1875 return temp;
1878 case VALUE:
1879 val = CSELIB_VAL_PTR (x);
1880 ret = NULL_RTX;
1882 if (!val)
1883 return ret;
1885 if (cselib_sp_based_value_p (val))
1886 return static_reg_base_value[STACK_POINTER_REGNUM];
1888 f = val->locs;
1889 /* Temporarily reset val->locs to avoid infinite recursion. */
1890 val->locs = NULL;
1892 for (l = f; l; l = l->next)
1893 if (GET_CODE (l->loc) == VALUE
1894 && CSELIB_VAL_PTR (l->loc)->locs
1895 && !CSELIB_VAL_PTR (l->loc)->locs->next
1896 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1897 continue;
1898 else if ((ret = find_base_term (l->loc)) != 0)
1899 break;
1901 val->locs = f;
1902 return ret;
1904 case LO_SUM:
1905 /* The standard form is (lo_sum reg sym) so look only at the
1906 second operand. */
1907 return find_base_term (XEXP (x, 1));
1909 case CONST:
1910 x = XEXP (x, 0);
1911 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1912 return 0;
1913 /* Fall through. */
1914 case PLUS:
1915 case MINUS:
1917 rtx tmp1 = XEXP (x, 0);
1918 rtx tmp2 = XEXP (x, 1);
1920 /* This is a little bit tricky since we have to determine which of
1921 the two operands represents the real base address. Otherwise this
1922 routine may return the index register instead of the base register.
1924 That may cause us to believe no aliasing was possible, when in
1925 fact aliasing is possible.
1927 We use a few simple tests to guess the base register. Additional
1928 tests can certainly be added. For example, if one of the operands
1929 is a shift or multiply, then it must be the index register and the
1930 other operand is the base register. */
1932 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1933 return find_base_term (tmp2);
1935 /* If either operand is known to be a pointer, then prefer it
1936 to determine the base term. */
1937 if (REG_P (tmp1) && REG_POINTER (tmp1))
1939 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1940 std::swap (tmp1, tmp2);
1941 /* If second argument is constant which has base term, prefer it
1942 over variable tmp1. See PR64025. */
1943 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
1944 std::swap (tmp1, tmp2);
1946 /* Go ahead and find the base term for both operands. If either base
1947 term is from a pointer or is a named object or a special address
1948 (like an argument or stack reference), then use it for the
1949 base term. */
1950 rtx base = find_base_term (tmp1);
1951 if (base != NULL_RTX
1952 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1953 || known_base_value_p (base)))
1954 return base;
1955 base = find_base_term (tmp2);
1956 if (base != NULL_RTX
1957 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1958 || known_base_value_p (base)))
1959 return base;
1961 /* We could not determine which of the two operands was the
1962 base register and which was the index. So we can determine
1963 nothing from the base alias check. */
1964 return 0;
1967 case AND:
1968 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1969 return find_base_term (XEXP (x, 0));
1970 return 0;
1972 case SYMBOL_REF:
1973 case LABEL_REF:
1974 return x;
1976 default:
1977 return 0;
1981 /* Return true if accesses to address X may alias accesses based
1982 on the stack pointer. */
1984 bool
1985 may_be_sp_based_p (rtx x)
1987 rtx base = find_base_term (x);
1988 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1991 /* Return 0 if the addresses X and Y are known to point to different
1992 objects, 1 if they might be pointers to the same object. */
1994 static int
1995 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1996 machine_mode x_mode, machine_mode y_mode)
1998 /* If the address itself has no known base see if a known equivalent
1999 value has one. If either address still has no known base, nothing
2000 is known about aliasing. */
2001 if (x_base == 0)
2003 rtx x_c;
2005 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
2006 return 1;
2008 x_base = find_base_term (x_c);
2009 if (x_base == 0)
2010 return 1;
2013 if (y_base == 0)
2015 rtx y_c;
2016 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
2017 return 1;
2019 y_base = find_base_term (y_c);
2020 if (y_base == 0)
2021 return 1;
2024 /* If the base addresses are equal nothing is known about aliasing. */
2025 if (rtx_equal_p (x_base, y_base))
2026 return 1;
2028 /* The base addresses are different expressions. If they are not accessed
2029 via AND, there is no conflict. We can bring knowledge of object
2030 alignment into play here. For example, on alpha, "char a, b;" can
2031 alias one another, though "char a; long b;" cannot. AND addesses may
2032 implicitly alias surrounding objects; i.e. unaligned access in DImode
2033 via AND address can alias all surrounding object types except those
2034 with aligment 8 or higher. */
2035 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2036 return 1;
2037 if (GET_CODE (x) == AND
2038 && (!CONST_INT_P (XEXP (x, 1))
2039 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2040 return 1;
2041 if (GET_CODE (y) == AND
2042 && (!CONST_INT_P (XEXP (y, 1))
2043 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2044 return 1;
2046 /* Differing symbols not accessed via AND never alias. */
2047 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2048 return 0;
2050 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2051 return 0;
2053 return 1;
2056 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2057 that of V. */
2059 static bool
2060 refs_newer_value_p (const_rtx expr, rtx v)
2062 int minuid = CSELIB_VAL_PTR (v)->uid;
2063 subrtx_iterator::array_type array;
2064 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2065 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
2066 return true;
2067 return false;
2070 /* Convert the address X into something we can use. This is done by returning
2071 it unchanged unless it is a value; in the latter case we call cselib to get
2072 a more useful rtx. */
2075 get_addr (rtx x)
2077 cselib_val *v;
2078 struct elt_loc_list *l;
2080 if (GET_CODE (x) != VALUE)
2081 return x;
2082 v = CSELIB_VAL_PTR (x);
2083 if (v)
2085 bool have_equivs = cselib_have_permanent_equivalences ();
2086 if (have_equivs)
2087 v = canonical_cselib_val (v);
2088 for (l = v->locs; l; l = l->next)
2089 if (CONSTANT_P (l->loc))
2090 return l->loc;
2091 for (l = v->locs; l; l = l->next)
2092 if (!REG_P (l->loc) && !MEM_P (l->loc)
2093 /* Avoid infinite recursion when potentially dealing with
2094 var-tracking artificial equivalences, by skipping the
2095 equivalences themselves, and not choosing expressions
2096 that refer to newer VALUEs. */
2097 && (!have_equivs
2098 || (GET_CODE (l->loc) != VALUE
2099 && !refs_newer_value_p (l->loc, x))))
2100 return l->loc;
2101 if (have_equivs)
2103 for (l = v->locs; l; l = l->next)
2104 if (REG_P (l->loc)
2105 || (GET_CODE (l->loc) != VALUE
2106 && !refs_newer_value_p (l->loc, x)))
2107 return l->loc;
2108 /* Return the canonical value. */
2109 return v->val_rtx;
2111 if (v->locs)
2112 return v->locs->loc;
2114 return x;
2117 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2118 where SIZE is the size in bytes of the memory reference. If ADDR
2119 is not modified by the memory reference then ADDR is returned. */
2121 static rtx
2122 addr_side_effect_eval (rtx addr, int size, int n_refs)
2124 int offset = 0;
2126 switch (GET_CODE (addr))
2128 case PRE_INC:
2129 offset = (n_refs + 1) * size;
2130 break;
2131 case PRE_DEC:
2132 offset = -(n_refs + 1) * size;
2133 break;
2134 case POST_INC:
2135 offset = n_refs * size;
2136 break;
2137 case POST_DEC:
2138 offset = -n_refs * size;
2139 break;
2141 default:
2142 return addr;
2145 if (offset)
2146 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
2147 gen_int_mode (offset, GET_MODE (addr)));
2148 else
2149 addr = XEXP (addr, 0);
2150 addr = canon_rtx (addr);
2152 return addr;
2155 /* Return TRUE if an object X sized at XSIZE bytes and another object
2156 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2157 any of the sizes is zero, assume an overlap, otherwise use the
2158 absolute value of the sizes as the actual sizes. */
2160 static inline bool
2161 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
2163 return (xsize == 0 || ysize == 0
2164 || (c >= 0
2165 ? (abs (xsize) > c)
2166 : (abs (ysize) > -c)));
2169 /* Return one if X and Y (memory addresses) reference the
2170 same location in memory or if the references overlap.
2171 Return zero if they do not overlap, else return
2172 minus one in which case they still might reference the same location.
2174 C is an offset accumulator. When
2175 C is nonzero, we are testing aliases between X and Y + C.
2176 XSIZE is the size in bytes of the X reference,
2177 similarly YSIZE is the size in bytes for Y.
2178 Expect that canon_rtx has been already called for X and Y.
2180 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2181 referenced (the reference was BLKmode), so make the most pessimistic
2182 assumptions.
2184 If XSIZE or YSIZE is negative, we may access memory outside the object
2185 being referenced as a side effect. This can happen when using AND to
2186 align memory references, as is done on the Alpha.
2188 Nice to notice that varying addresses cannot conflict with fp if no
2189 local variables had their addresses taken, but that's too hard now.
2191 ??? Contrary to the tree alias oracle this does not return
2192 one for X + non-constant and Y + non-constant when X and Y are equal.
2193 If that is fixed the TBAA hack for union type-punning can be removed. */
2195 static int
2196 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2198 if (GET_CODE (x) == VALUE)
2200 if (REG_P (y))
2202 struct elt_loc_list *l = NULL;
2203 if (CSELIB_VAL_PTR (x))
2204 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2205 l; l = l->next)
2206 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2207 break;
2208 if (l)
2209 x = y;
2210 else
2211 x = get_addr (x);
2213 /* Don't call get_addr if y is the same VALUE. */
2214 else if (x != y)
2215 x = get_addr (x);
2217 if (GET_CODE (y) == VALUE)
2219 if (REG_P (x))
2221 struct elt_loc_list *l = NULL;
2222 if (CSELIB_VAL_PTR (y))
2223 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2224 l; l = l->next)
2225 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2226 break;
2227 if (l)
2228 y = x;
2229 else
2230 y = get_addr (y);
2232 /* Don't call get_addr if x is the same VALUE. */
2233 else if (y != x)
2234 y = get_addr (y);
2236 if (GET_CODE (x) == HIGH)
2237 x = XEXP (x, 0);
2238 else if (GET_CODE (x) == LO_SUM)
2239 x = XEXP (x, 1);
2240 else
2241 x = addr_side_effect_eval (x, abs (xsize), 0);
2242 if (GET_CODE (y) == HIGH)
2243 y = XEXP (y, 0);
2244 else if (GET_CODE (y) == LO_SUM)
2245 y = XEXP (y, 1);
2246 else
2247 y = addr_side_effect_eval (y, abs (ysize), 0);
2249 if (rtx_equal_for_memref_p (x, y))
2251 return offset_overlap_p (c, xsize, ysize);
2254 /* This code used to check for conflicts involving stack references and
2255 globals but the base address alias code now handles these cases. */
2257 if (GET_CODE (x) == PLUS)
2259 /* The fact that X is canonicalized means that this
2260 PLUS rtx is canonicalized. */
2261 rtx x0 = XEXP (x, 0);
2262 rtx x1 = XEXP (x, 1);
2264 if (GET_CODE (y) == PLUS)
2266 /* The fact that Y is canonicalized means that this
2267 PLUS rtx is canonicalized. */
2268 rtx y0 = XEXP (y, 0);
2269 rtx y1 = XEXP (y, 1);
2271 if (rtx_equal_for_memref_p (x1, y1))
2272 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2273 if (rtx_equal_for_memref_p (x0, y0))
2274 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2275 if (CONST_INT_P (x1))
2277 if (CONST_INT_P (y1))
2278 return memrefs_conflict_p (xsize, x0, ysize, y0,
2279 c - INTVAL (x1) + INTVAL (y1));
2280 else
2281 return memrefs_conflict_p (xsize, x0, ysize, y,
2282 c - INTVAL (x1));
2284 else if (CONST_INT_P (y1))
2285 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2287 return -1;
2289 else if (CONST_INT_P (x1))
2290 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2292 else if (GET_CODE (y) == PLUS)
2294 /* The fact that Y is canonicalized means that this
2295 PLUS rtx is canonicalized. */
2296 rtx y0 = XEXP (y, 0);
2297 rtx y1 = XEXP (y, 1);
2299 if (CONST_INT_P (y1))
2300 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2301 else
2302 return -1;
2305 if (GET_CODE (x) == GET_CODE (y))
2306 switch (GET_CODE (x))
2308 case MULT:
2310 /* Handle cases where we expect the second operands to be the
2311 same, and check only whether the first operand would conflict
2312 or not. */
2313 rtx x0, y0;
2314 rtx x1 = canon_rtx (XEXP (x, 1));
2315 rtx y1 = canon_rtx (XEXP (y, 1));
2316 if (! rtx_equal_for_memref_p (x1, y1))
2317 return -1;
2318 x0 = canon_rtx (XEXP (x, 0));
2319 y0 = canon_rtx (XEXP (y, 0));
2320 if (rtx_equal_for_memref_p (x0, y0))
2321 return offset_overlap_p (c, xsize, ysize);
2323 /* Can't properly adjust our sizes. */
2324 if (!CONST_INT_P (x1))
2325 return -1;
2326 xsize /= INTVAL (x1);
2327 ysize /= INTVAL (x1);
2328 c /= INTVAL (x1);
2329 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2332 default:
2333 break;
2336 /* Deal with alignment ANDs by adjusting offset and size so as to
2337 cover the maximum range, without taking any previously known
2338 alignment into account. Make a size negative after such an
2339 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2340 assume a potential overlap, because they may end up in contiguous
2341 memory locations and the stricter-alignment access may span over
2342 part of both. */
2343 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2345 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2346 unsigned HOST_WIDE_INT uc = sc;
2347 if (sc < 0 && -uc == (uc & -uc))
2349 if (xsize > 0)
2350 xsize = -xsize;
2351 if (xsize)
2352 xsize += sc + 1;
2353 c -= sc + 1;
2354 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2355 ysize, y, c);
2358 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2360 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2361 unsigned HOST_WIDE_INT uc = sc;
2362 if (sc < 0 && -uc == (uc & -uc))
2364 if (ysize > 0)
2365 ysize = -ysize;
2366 if (ysize)
2367 ysize += sc + 1;
2368 c += sc + 1;
2369 return memrefs_conflict_p (xsize, x,
2370 ysize, canon_rtx (XEXP (y, 0)), c);
2374 if (CONSTANT_P (x))
2376 if (CONST_INT_P (x) && CONST_INT_P (y))
2378 c += (INTVAL (y) - INTVAL (x));
2379 return offset_overlap_p (c, xsize, ysize);
2382 if (GET_CODE (x) == CONST)
2384 if (GET_CODE (y) == CONST)
2385 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2386 ysize, canon_rtx (XEXP (y, 0)), c);
2387 else
2388 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2389 ysize, y, c);
2391 if (GET_CODE (y) == CONST)
2392 return memrefs_conflict_p (xsize, x, ysize,
2393 canon_rtx (XEXP (y, 0)), c);
2395 /* Assume a potential overlap for symbolic addresses that went
2396 through alignment adjustments (i.e., that have negative
2397 sizes), because we can't know how far they are from each
2398 other. */
2399 if (CONSTANT_P (y))
2400 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2402 return -1;
2405 return -1;
2408 /* Functions to compute memory dependencies.
2410 Since we process the insns in execution order, we can build tables
2411 to keep track of what registers are fixed (and not aliased), what registers
2412 are varying in known ways, and what registers are varying in unknown
2413 ways.
2415 If both memory references are volatile, then there must always be a
2416 dependence between the two references, since their order can not be
2417 changed. A volatile and non-volatile reference can be interchanged
2418 though.
2420 We also must allow AND addresses, because they may generate accesses
2421 outside the object being referenced. This is used to generate aligned
2422 addresses from unaligned addresses, for instance, the alpha
2423 storeqi_unaligned pattern. */
2425 /* Read dependence: X is read after read in MEM takes place. There can
2426 only be a dependence here if both reads are volatile, or if either is
2427 an explicit barrier. */
2430 read_dependence (const_rtx mem, const_rtx x)
2432 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2433 return true;
2434 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2435 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2436 return true;
2437 return false;
2440 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2442 static tree
2443 decl_for_component_ref (tree x)
2447 x = TREE_OPERAND (x, 0);
2449 while (x && TREE_CODE (x) == COMPONENT_REF);
2451 return x && DECL_P (x) ? x : NULL_TREE;
2454 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2455 for the offset of the field reference. *KNOWN_P says whether the
2456 offset is known. */
2458 static void
2459 adjust_offset_for_component_ref (tree x, bool *known_p,
2460 HOST_WIDE_INT *offset)
2462 if (!*known_p)
2463 return;
2466 tree xoffset = component_ref_field_offset (x);
2467 tree field = TREE_OPERAND (x, 1);
2468 if (TREE_CODE (xoffset) != INTEGER_CST)
2470 *known_p = false;
2471 return;
2474 offset_int woffset
2475 = (wi::to_offset (xoffset)
2476 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2477 LOG2_BITS_PER_UNIT));
2478 if (!wi::fits_uhwi_p (woffset))
2480 *known_p = false;
2481 return;
2483 *offset += woffset.to_uhwi ();
2485 x = TREE_OPERAND (x, 0);
2487 while (x && TREE_CODE (x) == COMPONENT_REF);
2490 /* Return nonzero if we can determine the exprs corresponding to memrefs
2491 X and Y and they do not overlap.
2492 If LOOP_VARIANT is set, skip offset-based disambiguation */
2495 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2497 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2498 rtx rtlx, rtly;
2499 rtx basex, basey;
2500 bool moffsetx_known_p, moffsety_known_p;
2501 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2502 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2504 /* Unless both have exprs, we can't tell anything. */
2505 if (exprx == 0 || expry == 0)
2506 return 0;
2508 /* For spill-slot accesses make sure we have valid offsets. */
2509 if ((exprx == get_spill_slot_decl (false)
2510 && ! MEM_OFFSET_KNOWN_P (x))
2511 || (expry == get_spill_slot_decl (false)
2512 && ! MEM_OFFSET_KNOWN_P (y)))
2513 return 0;
2515 /* If the field reference test failed, look at the DECLs involved. */
2516 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2517 if (moffsetx_known_p)
2518 moffsetx = MEM_OFFSET (x);
2519 if (TREE_CODE (exprx) == COMPONENT_REF)
2521 tree t = decl_for_component_ref (exprx);
2522 if (! t)
2523 return 0;
2524 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2525 exprx = t;
2528 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2529 if (moffsety_known_p)
2530 moffsety = MEM_OFFSET (y);
2531 if (TREE_CODE (expry) == COMPONENT_REF)
2533 tree t = decl_for_component_ref (expry);
2534 if (! t)
2535 return 0;
2536 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2537 expry = t;
2540 if (! DECL_P (exprx) || ! DECL_P (expry))
2541 return 0;
2543 /* With invalid code we can end up storing into the constant pool.
2544 Bail out to avoid ICEing when creating RTL for this.
2545 See gfortran.dg/lto/20091028-2_0.f90. */
2546 if (TREE_CODE (exprx) == CONST_DECL
2547 || TREE_CODE (expry) == CONST_DECL)
2548 return 1;
2550 rtlx = DECL_RTL (exprx);
2551 rtly = DECL_RTL (expry);
2553 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2554 can't overlap unless they are the same because we never reuse that part
2555 of the stack frame used for locals for spilled pseudos. */
2556 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2557 && ! rtx_equal_p (rtlx, rtly))
2558 return 1;
2560 /* If we have MEMs referring to different address spaces (which can
2561 potentially overlap), we cannot easily tell from the addresses
2562 whether the references overlap. */
2563 if (MEM_P (rtlx) && MEM_P (rtly)
2564 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2565 return 0;
2567 /* Get the base and offsets of both decls. If either is a register, we
2568 know both are and are the same, so use that as the base. The only
2569 we can avoid overlap is if we can deduce that they are nonoverlapping
2570 pieces of that decl, which is very rare. */
2571 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2572 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2573 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2575 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2576 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2577 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2579 /* If the bases are different, we know they do not overlap if both
2580 are constants or if one is a constant and the other a pointer into the
2581 stack frame. Otherwise a different base means we can't tell if they
2582 overlap or not. */
2583 if (! rtx_equal_p (basex, basey))
2584 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2585 || (CONSTANT_P (basex) && REG_P (basey)
2586 && REGNO_PTR_FRAME_P (REGNO (basey)))
2587 || (CONSTANT_P (basey) && REG_P (basex)
2588 && REGNO_PTR_FRAME_P (REGNO (basex))));
2590 /* Offset based disambiguation not appropriate for loop invariant */
2591 if (loop_invariant)
2592 return 0;
2594 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2595 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2596 : -1);
2597 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2598 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2599 : -1);
2601 /* If we have an offset for either memref, it can update the values computed
2602 above. */
2603 if (moffsetx_known_p)
2604 offsetx += moffsetx, sizex -= moffsetx;
2605 if (moffsety_known_p)
2606 offsety += moffsety, sizey -= moffsety;
2608 /* If a memref has both a size and an offset, we can use the smaller size.
2609 We can't do this if the offset isn't known because we must view this
2610 memref as being anywhere inside the DECL's MEM. */
2611 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2612 sizex = MEM_SIZE (x);
2613 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2614 sizey = MEM_SIZE (y);
2616 /* Put the values of the memref with the lower offset in X's values. */
2617 if (offsetx > offsety)
2619 tem = offsetx, offsetx = offsety, offsety = tem;
2620 tem = sizex, sizex = sizey, sizey = tem;
2623 /* If we don't know the size of the lower-offset value, we can't tell
2624 if they conflict. Otherwise, we do the test. */
2625 return sizex >= 0 && offsety >= offsetx + sizex;
2628 /* Helper for true_dependence and canon_true_dependence.
2629 Checks for true dependence: X is read after store in MEM takes place.
2631 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2632 NULL_RTX, and the canonical addresses of MEM and X are both computed
2633 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2635 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2637 Returns 1 if there is a true dependence, 0 otherwise. */
2639 static int
2640 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2641 const_rtx x, rtx x_addr, bool mem_canonicalized)
2643 rtx true_mem_addr;
2644 rtx base;
2645 int ret;
2647 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2648 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2650 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2651 return 1;
2653 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2654 This is used in epilogue deallocation functions, and in cselib. */
2655 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2656 return 1;
2657 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2658 return 1;
2659 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2660 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2661 return 1;
2663 if (! x_addr)
2664 x_addr = XEXP (x, 0);
2665 x_addr = get_addr (x_addr);
2667 if (! mem_addr)
2669 mem_addr = XEXP (mem, 0);
2670 if (mem_mode == VOIDmode)
2671 mem_mode = GET_MODE (mem);
2673 true_mem_addr = get_addr (mem_addr);
2675 /* Read-only memory is by definition never modified, and therefore can't
2676 conflict with anything. However, don't assume anything when AND
2677 addresses are involved and leave to the code below to determine
2678 dependence. We don't expect to find read-only set on MEM, but
2679 stupid user tricks can produce them, so don't die. */
2680 if (MEM_READONLY_P (x)
2681 && GET_CODE (x_addr) != AND
2682 && GET_CODE (true_mem_addr) != AND)
2683 return 0;
2685 /* If we have MEMs referring to different address spaces (which can
2686 potentially overlap), we cannot easily tell from the addresses
2687 whether the references overlap. */
2688 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2689 return 1;
2691 base = find_base_term (x_addr);
2692 if (base && (GET_CODE (base) == LABEL_REF
2693 || (GET_CODE (base) == SYMBOL_REF
2694 && CONSTANT_POOL_ADDRESS_P (base))))
2695 return 0;
2697 rtx mem_base = find_base_term (true_mem_addr);
2698 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2699 GET_MODE (x), mem_mode))
2700 return 0;
2702 x_addr = canon_rtx (x_addr);
2703 if (!mem_canonicalized)
2704 mem_addr = canon_rtx (true_mem_addr);
2706 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2707 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2708 return ret;
2710 if (mems_in_disjoint_alias_sets_p (x, mem))
2711 return 0;
2713 if (nonoverlapping_memrefs_p (mem, x, false))
2714 return 0;
2716 return rtx_refs_may_alias_p (x, mem, true);
2719 /* True dependence: X is read after store in MEM takes place. */
2722 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2724 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2725 x, NULL_RTX, /*mem_canonicalized=*/false);
2728 /* Canonical true dependence: X is read after store in MEM takes place.
2729 Variant of true_dependence which assumes MEM has already been
2730 canonicalized (hence we no longer do that here).
2731 The mem_addr argument has been added, since true_dependence_1 computed
2732 this value prior to canonicalizing. */
2735 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2736 const_rtx x, rtx x_addr)
2738 return true_dependence_1 (mem, mem_mode, mem_addr,
2739 x, x_addr, /*mem_canonicalized=*/true);
2742 /* Returns nonzero if a write to X might alias a previous read from
2743 (or, if WRITEP is true, a write to) MEM.
2744 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2745 and X_MODE the mode for that access.
2746 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2748 static int
2749 write_dependence_p (const_rtx mem,
2750 const_rtx x, machine_mode x_mode, rtx x_addr,
2751 bool mem_canonicalized, bool x_canonicalized, bool writep)
2753 rtx mem_addr;
2754 rtx true_mem_addr, true_x_addr;
2755 rtx base;
2756 int ret;
2758 gcc_checking_assert (x_canonicalized
2759 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2760 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2762 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2763 return 1;
2765 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2766 This is used in epilogue deallocation functions. */
2767 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2768 return 1;
2769 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2770 return 1;
2771 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2772 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2773 return 1;
2775 if (!x_addr)
2776 x_addr = XEXP (x, 0);
2777 true_x_addr = get_addr (x_addr);
2779 mem_addr = XEXP (mem, 0);
2780 true_mem_addr = get_addr (mem_addr);
2782 /* A read from read-only memory can't conflict with read-write memory.
2783 Don't assume anything when AND addresses are involved and leave to
2784 the code below to determine dependence. */
2785 if (!writep
2786 && MEM_READONLY_P (mem)
2787 && GET_CODE (true_x_addr) != AND
2788 && GET_CODE (true_mem_addr) != AND)
2789 return 0;
2791 /* If we have MEMs referring to different address spaces (which can
2792 potentially overlap), we cannot easily tell from the addresses
2793 whether the references overlap. */
2794 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2795 return 1;
2797 base = find_base_term (true_mem_addr);
2798 if (! writep
2799 && base
2800 && (GET_CODE (base) == LABEL_REF
2801 || (GET_CODE (base) == SYMBOL_REF
2802 && CONSTANT_POOL_ADDRESS_P (base))))
2803 return 0;
2805 rtx x_base = find_base_term (true_x_addr);
2806 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2807 GET_MODE (x), GET_MODE (mem)))
2808 return 0;
2810 if (!x_canonicalized)
2812 x_addr = canon_rtx (true_x_addr);
2813 x_mode = GET_MODE (x);
2815 if (!mem_canonicalized)
2816 mem_addr = canon_rtx (true_mem_addr);
2818 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2819 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2820 return ret;
2822 if (nonoverlapping_memrefs_p (x, mem, false))
2823 return 0;
2825 return rtx_refs_may_alias_p (x, mem, false);
2828 /* Anti dependence: X is written after read in MEM takes place. */
2831 anti_dependence (const_rtx mem, const_rtx x)
2833 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2834 /*mem_canonicalized=*/false,
2835 /*x_canonicalized*/false, /*writep=*/false);
2838 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2839 Also, consider X in X_MODE (which might be from an enclosing
2840 STRICT_LOW_PART / ZERO_EXTRACT).
2841 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2844 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2845 const_rtx x, machine_mode x_mode, rtx x_addr)
2847 return write_dependence_p (mem, x, x_mode, x_addr,
2848 mem_canonicalized, /*x_canonicalized=*/true,
2849 /*writep=*/false);
2852 /* Output dependence: X is written after store in MEM takes place. */
2855 output_dependence (const_rtx mem, const_rtx x)
2857 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2858 /*mem_canonicalized=*/false,
2859 /*x_canonicalized*/false, /*writep=*/true);
2864 /* Check whether X may be aliased with MEM. Don't do offset-based
2865 memory disambiguation & TBAA. */
2867 may_alias_p (const_rtx mem, const_rtx x)
2869 rtx x_addr, mem_addr;
2871 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2872 return 1;
2874 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2875 This is used in epilogue deallocation functions. */
2876 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2877 return 1;
2878 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2879 return 1;
2880 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2881 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2882 return 1;
2884 x_addr = XEXP (x, 0);
2885 x_addr = get_addr (x_addr);
2887 mem_addr = XEXP (mem, 0);
2888 mem_addr = get_addr (mem_addr);
2890 /* Read-only memory is by definition never modified, and therefore can't
2891 conflict with anything. However, don't assume anything when AND
2892 addresses are involved and leave to the code below to determine
2893 dependence. We don't expect to find read-only set on MEM, but
2894 stupid user tricks can produce them, so don't die. */
2895 if (MEM_READONLY_P (x)
2896 && GET_CODE (x_addr) != AND
2897 && GET_CODE (mem_addr) != AND)
2898 return 0;
2900 /* If we have MEMs referring to different address spaces (which can
2901 potentially overlap), we cannot easily tell from the addresses
2902 whether the references overlap. */
2903 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2904 return 1;
2906 rtx x_base = find_base_term (x_addr);
2907 rtx mem_base = find_base_term (mem_addr);
2908 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2909 GET_MODE (x), GET_MODE (mem_addr)))
2910 return 0;
2912 if (nonoverlapping_memrefs_p (mem, x, true))
2913 return 0;
2915 /* TBAA not valid for loop_invarint */
2916 return rtx_refs_may_alias_p (x, mem, false);
2919 void
2920 init_alias_target (void)
2922 int i;
2924 if (!arg_base_value)
2925 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2927 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2929 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2930 /* Check whether this register can hold an incoming pointer
2931 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2932 numbers, so translate if necessary due to register windows. */
2933 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2934 && HARD_REGNO_MODE_OK (i, Pmode))
2935 static_reg_base_value[i] = arg_base_value;
2937 static_reg_base_value[STACK_POINTER_REGNUM]
2938 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2939 static_reg_base_value[ARG_POINTER_REGNUM]
2940 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2941 static_reg_base_value[FRAME_POINTER_REGNUM]
2942 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2943 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2944 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2945 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2948 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2949 to be memory reference. */
2950 static bool memory_modified;
2951 static void
2952 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2954 if (MEM_P (x))
2956 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2957 memory_modified = true;
2962 /* Return true when INSN possibly modify memory contents of MEM
2963 (i.e. address can be modified). */
2964 bool
2965 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2967 if (!INSN_P (insn))
2968 return false;
2969 memory_modified = false;
2970 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2971 return memory_modified;
2974 /* Return TRUE if the destination of a set is rtx identical to
2975 ITEM. */
2976 static inline bool
2977 set_dest_equal_p (const_rtx set, const_rtx item)
2979 rtx dest = SET_DEST (set);
2980 return rtx_equal_p (dest, item);
2983 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2984 *DEFINITELY* modify the memory contents of MEM. */
2985 bool
2986 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2988 if (!INSN_P (insn))
2989 return false;
2990 insn = PATTERN (insn);
2991 if (GET_CODE (insn) == SET)
2992 return set_dest_equal_p (insn, mem);
2993 else if (GET_CODE (insn) == PARALLEL)
2995 int i;
2996 for (i = 0; i < XVECLEN (insn, 0); i++)
2998 rtx sub = XVECEXP (insn, 0, i);
2999 if (GET_CODE (sub) == SET
3000 && set_dest_equal_p (sub, mem))
3001 return true;
3004 return false;
3007 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3008 array. */
3010 void
3011 init_alias_analysis (void)
3013 unsigned int maxreg = max_reg_num ();
3014 int changed, pass;
3015 int i;
3016 unsigned int ui;
3017 rtx_insn *insn;
3018 rtx val;
3019 int rpo_cnt;
3020 int *rpo;
3022 timevar_push (TV_ALIAS_ANALYSIS);
3024 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
3025 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3026 bitmap_clear (reg_known_equiv_p);
3028 /* If we have memory allocated from the previous run, use it. */
3029 if (old_reg_base_value)
3030 reg_base_value = old_reg_base_value;
3032 if (reg_base_value)
3033 reg_base_value->truncate (0);
3035 vec_safe_grow_cleared (reg_base_value, maxreg);
3037 new_reg_base_value = XNEWVEC (rtx, maxreg);
3038 reg_seen = sbitmap_alloc (maxreg);
3040 /* The basic idea is that each pass through this loop will use the
3041 "constant" information from the previous pass to propagate alias
3042 information through another level of assignments.
3044 The propagation is done on the CFG in reverse post-order, to propagate
3045 things forward as far as possible in each iteration.
3047 This could get expensive if the assignment chains are long. Maybe
3048 we should throttle the number of iterations, possibly based on
3049 the optimization level or flag_expensive_optimizations.
3051 We could propagate more information in the first pass by making use
3052 of DF_REG_DEF_COUNT to determine immediately that the alias information
3053 for a pseudo is "constant".
3055 A program with an uninitialized variable can cause an infinite loop
3056 here. Instead of doing a full dataflow analysis to detect such problems
3057 we just cap the number of iterations for the loop.
3059 The state of the arrays for the set chain in question does not matter
3060 since the program has undefined behavior. */
3062 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3063 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3065 pass = 0;
3068 /* Assume nothing will change this iteration of the loop. */
3069 changed = 0;
3071 /* We want to assign the same IDs each iteration of this loop, so
3072 start counting from one each iteration of the loop. */
3073 unique_id = 1;
3075 /* We're at the start of the function each iteration through the
3076 loop, so we're copying arguments. */
3077 copying_arguments = true;
3079 /* Wipe the potential alias information clean for this pass. */
3080 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3082 /* Wipe the reg_seen array clean. */
3083 bitmap_clear (reg_seen);
3085 /* Initialize the alias information for this pass. */
3086 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3087 if (static_reg_base_value[i])
3089 new_reg_base_value[i] = static_reg_base_value[i];
3090 bitmap_set_bit (reg_seen, i);
3093 /* Walk the insns adding values to the new_reg_base_value array. */
3094 for (i = 0; i < rpo_cnt; i++)
3096 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3097 FOR_BB_INSNS (bb, insn)
3099 if (NONDEBUG_INSN_P (insn))
3101 rtx note, set;
3103 #if defined (HAVE_prologue)
3104 static const bool prologue = true;
3105 #else
3106 static const bool prologue = false;
3107 #endif
3109 /* The prologue/epilogue insns are not threaded onto the
3110 insn chain until after reload has completed. Thus,
3111 there is no sense wasting time checking if INSN is in
3112 the prologue/epilogue until after reload has completed. */
3113 if ((prologue || HAVE_epilogue) && reload_completed
3114 && prologue_epilogue_contains (insn))
3115 continue;
3117 /* If this insn has a noalias note, process it, Otherwise,
3118 scan for sets. A simple set will have no side effects
3119 which could change the base value of any other register. */
3121 if (GET_CODE (PATTERN (insn)) == SET
3122 && REG_NOTES (insn) != 0
3123 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3124 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3125 else
3126 note_stores (PATTERN (insn), record_set, NULL);
3128 set = single_set (insn);
3130 if (set != 0
3131 && REG_P (SET_DEST (set))
3132 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3134 unsigned int regno = REGNO (SET_DEST (set));
3135 rtx src = SET_SRC (set);
3136 rtx t;
3138 note = find_reg_equal_equiv_note (insn);
3139 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3140 && DF_REG_DEF_COUNT (regno) != 1)
3141 note = NULL_RTX;
3143 if (note != NULL_RTX
3144 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3145 && ! rtx_varies_p (XEXP (note, 0), 1)
3146 && ! reg_overlap_mentioned_p (SET_DEST (set),
3147 XEXP (note, 0)))
3149 set_reg_known_value (regno, XEXP (note, 0));
3150 set_reg_known_equiv_p (regno,
3151 REG_NOTE_KIND (note) == REG_EQUIV);
3153 else if (DF_REG_DEF_COUNT (regno) == 1
3154 && GET_CODE (src) == PLUS
3155 && REG_P (XEXP (src, 0))
3156 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3157 && CONST_INT_P (XEXP (src, 1)))
3159 t = plus_constant (GET_MODE (src), t,
3160 INTVAL (XEXP (src, 1)));
3161 set_reg_known_value (regno, t);
3162 set_reg_known_equiv_p (regno, false);
3164 else if (DF_REG_DEF_COUNT (regno) == 1
3165 && ! rtx_varies_p (src, 1))
3167 set_reg_known_value (regno, src);
3168 set_reg_known_equiv_p (regno, false);
3172 else if (NOTE_P (insn)
3173 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3174 copying_arguments = false;
3178 /* Now propagate values from new_reg_base_value to reg_base_value. */
3179 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3181 for (ui = 0; ui < maxreg; ui++)
3183 if (new_reg_base_value[ui]
3184 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3185 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3187 (*reg_base_value)[ui] = new_reg_base_value[ui];
3188 changed = 1;
3192 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3193 XDELETEVEC (rpo);
3195 /* Fill in the remaining entries. */
3196 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3198 int regno = i + FIRST_PSEUDO_REGISTER;
3199 if (! val)
3200 set_reg_known_value (regno, regno_reg_rtx[regno]);
3203 /* Clean up. */
3204 free (new_reg_base_value);
3205 new_reg_base_value = 0;
3206 sbitmap_free (reg_seen);
3207 reg_seen = 0;
3208 timevar_pop (TV_ALIAS_ANALYSIS);
3211 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3212 Special API for var-tracking pass purposes. */
3214 void
3215 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3217 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3220 void
3221 end_alias_analysis (void)
3223 old_reg_base_value = reg_base_value;
3224 vec_free (reg_known_value);
3225 sbitmap_free (reg_known_equiv_p);
3228 void
3229 dump_alias_stats_in_alias_c (FILE *s)
3231 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3232 " %llu are in alias set 0\n"
3233 " %llu queries asked about the same object\n"
3234 " %llu queries asked about the same alias set\n"
3235 " %llu access volatile\n"
3236 " %llu are dependent in the DAG\n"
3237 " %llu are aritificially in conflict with void *\n",
3238 alias_stats.num_disambiguated,
3239 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3240 + alias_stats.num_same_objects + alias_stats.num_volatile
3241 + alias_stats.num_dag + alias_stats.num_disambiguated
3242 + alias_stats.num_universal,
3243 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3244 alias_stats.num_same_objects, alias_stats.num_volatile,
3245 alias_stats.num_dag, alias_stats.num_universal);
3247 #include "gt-alias.h"