Fix bootstrap/PR63632
[official-gcc.git] / gcc / alias.c
blob30454c3210b2480316ad0e4b6090b5d7fd3343bf
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2014 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 "tree.h"
27 #include "varasm.h"
28 #include "expr.h"
29 #include "tm_p.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "vec.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "alias.h"
38 #include "emit-rtl.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "diagnostic-core.h"
42 #include "cselib.h"
43 #include "splay-tree.h"
44 #include "langhooks.h"
45 #include "timevar.h"
46 #include "dumpfile.h"
47 #include "target.h"
48 #include "df.h"
49 #include "tree-ssa-alias.h"
50 #include "internal-fn.h"
51 #include "gimple-expr.h"
52 #include "is-a.h"
53 #include "gimple.h"
54 #include "gimple-ssa.h"
55 #include "rtl-iter.h"
57 /* The aliasing API provided here solves related but different problems:
59 Say there exists (in c)
61 struct X {
62 struct Y y1;
63 struct Z z2;
64 } x1, *px1, *px2;
66 struct Y y2, *py;
67 struct Z z2, *pz;
70 py = &x1.y1;
71 px2 = &x1;
73 Consider the four questions:
75 Can a store to x1 interfere with px2->y1?
76 Can a store to x1 interfere with px2->z2?
77 Can a store to x1 change the value pointed to by with py?
78 Can a store to x1 change the value pointed to by with pz?
80 The answer to these questions can be yes, yes, yes, and maybe.
82 The first two questions can be answered with a simple examination
83 of the type system. If structure X contains a field of type Y then
84 a store through a pointer to an X can overwrite any field that is
85 contained (recursively) in an X (unless we know that px1 != px2).
87 The last two questions can be solved in the same way as the first
88 two questions but this is too conservative. The observation is
89 that in some cases we can know which (if any) fields are addressed
90 and if those addresses are used in bad ways. This analysis may be
91 language specific. In C, arbitrary operations may be applied to
92 pointers. However, there is some indication that this may be too
93 conservative for some C++ types.
95 The pass ipa-type-escape does this analysis for the types whose
96 instances do not escape across the compilation boundary.
98 Historically in GCC, these two problems were combined and a single
99 data structure that was used to represent the solution to these
100 problems. We now have two similar but different data structures,
101 The data structure to solve the last two questions is similar to
102 the first, but does not contain the fields whose address are never
103 taken. For types that do escape the compilation unit, the data
104 structures will have identical information.
107 /* The alias sets assigned to MEMs assist the back-end in determining
108 which MEMs can alias which other MEMs. In general, two MEMs in
109 different alias sets cannot alias each other, with one important
110 exception. Consider something like:
112 struct S { int i; double d; };
114 a store to an `S' can alias something of either type `int' or type
115 `double'. (However, a store to an `int' cannot alias a `double'
116 and vice versa.) We indicate this via a tree structure that looks
117 like:
118 struct S
121 |/_ _\|
122 int double
124 (The arrows are directed and point downwards.)
125 In this situation we say the alias set for `struct S' is the
126 `superset' and that those for `int' and `double' are `subsets'.
128 To see whether two alias sets can point to the same memory, we must
129 see if either alias set is a subset of the other. We need not trace
130 past immediate descendants, however, since we propagate all
131 grandchildren up one level.
133 Alias set zero is implicitly a superset of all other alias sets.
134 However, this is no actual entry for alias set zero. It is an
135 error to attempt to explicitly construct a subset of zero. */
137 struct GTY(()) alias_set_entry_d {
138 /* The alias set number, as stored in MEM_ALIAS_SET. */
139 alias_set_type alias_set;
141 /* Nonzero if would have a child of zero: this effectively makes this
142 alias set the same as alias set zero. */
143 int has_zero_child;
145 /* The children of the alias set. These are not just the immediate
146 children, but, in fact, all descendants. So, if we have:
148 struct T { struct S s; float f; }
150 continuing our example above, the children here will be all of
151 `int', `double', `float', and `struct S'. */
152 splay_tree GTY((param1_is (int), param2_is (int))) children;
154 typedef struct alias_set_entry_d *alias_set_entry;
156 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
157 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
158 static void record_set (rtx, const_rtx, void *);
159 static int base_alias_check (rtx, rtx, rtx, rtx, enum machine_mode,
160 enum machine_mode);
161 static rtx find_base_value (rtx);
162 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
163 static int insert_subset_children (splay_tree_node, void*);
164 static alias_set_entry get_alias_set_entry (alias_set_type);
165 static tree decl_for_component_ref (tree);
166 static int write_dependence_p (const_rtx,
167 const_rtx, enum machine_mode, rtx,
168 bool, bool, bool);
170 static void memory_modified_1 (rtx, const_rtx, void *);
172 /* Set up all info needed to perform alias analysis on memory references. */
174 /* Returns the size in bytes of the mode of X. */
175 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
177 /* Cap the number of passes we make over the insns propagating alias
178 information through set chains.
179 ??? 10 is a completely arbitrary choice. This should be based on the
180 maximum loop depth in the CFG, but we do not have this information
181 available (even if current_loops _is_ available). */
182 #define MAX_ALIAS_LOOP_PASSES 10
184 /* reg_base_value[N] gives an address to which register N is related.
185 If all sets after the first add or subtract to the current value
186 or otherwise modify it so it does not point to a different top level
187 object, reg_base_value[N] is equal to the address part of the source
188 of the first set.
190 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
191 expressions represent three types of base:
193 1. incoming arguments. There is just one ADDRESS to represent all
194 arguments, since we do not know at this level whether accesses
195 based on different arguments can alias. The ADDRESS has id 0.
197 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
198 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
199 Each of these rtxes has a separate ADDRESS associated with it,
200 each with a negative id.
202 GCC is (and is required to be) precise in which register it
203 chooses to access a particular region of stack. We can therefore
204 assume that accesses based on one of these rtxes do not alias
205 accesses based on another of these rtxes.
207 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
208 Each such piece of memory has a separate ADDRESS associated
209 with it, each with an id greater than 0.
211 Accesses based on one ADDRESS do not alias accesses based on other
212 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
213 alias globals either; the ADDRESSes have Pmode to indicate this.
214 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
215 indicate this. */
217 static GTY(()) vec<rtx, va_gc> *reg_base_value;
218 static rtx *new_reg_base_value;
220 /* The single VOIDmode ADDRESS that represents all argument bases.
221 It has id 0. */
222 static GTY(()) rtx arg_base_value;
224 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
225 static int unique_id;
227 /* We preserve the copy of old array around to avoid amount of garbage
228 produced. About 8% of garbage produced were attributed to this
229 array. */
230 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
232 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
233 registers. */
234 #define UNIQUE_BASE_VALUE_SP -1
235 #define UNIQUE_BASE_VALUE_ARGP -2
236 #define UNIQUE_BASE_VALUE_FP -3
237 #define UNIQUE_BASE_VALUE_HFP -4
239 #define static_reg_base_value \
240 (this_target_rtl->x_static_reg_base_value)
242 #define REG_BASE_VALUE(X) \
243 (REGNO (X) < vec_safe_length (reg_base_value) \
244 ? (*reg_base_value)[REGNO (X)] : 0)
246 /* Vector indexed by N giving the initial (unchanging) value known for
247 pseudo-register N. This vector is initialized in init_alias_analysis,
248 and does not change until end_alias_analysis is called. */
249 static GTY(()) vec<rtx, va_gc> *reg_known_value;
251 /* Vector recording for each reg_known_value whether it is due to a
252 REG_EQUIV note. Future passes (viz., reload) may replace the
253 pseudo with the equivalent expression and so we account for the
254 dependences that would be introduced if that happens.
256 The REG_EQUIV notes created in assign_parms may mention the arg
257 pointer, and there are explicit insns in the RTL that modify the
258 arg pointer. Thus we must ensure that such insns don't get
259 scheduled across each other because that would invalidate the
260 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
261 wrong, but solving the problem in the scheduler will likely give
262 better code, so we do it here. */
263 static sbitmap reg_known_equiv_p;
265 /* True when scanning insns from the start of the rtl to the
266 NOTE_INSN_FUNCTION_BEG note. */
267 static bool copying_arguments;
270 /* The splay-tree used to store the various alias set entries. */
271 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
273 /* Build a decomposed reference object for querying the alias-oracle
274 from the MEM rtx and store it in *REF.
275 Returns false if MEM is not suitable for the alias-oracle. */
277 static bool
278 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
280 tree expr = MEM_EXPR (mem);
281 tree base;
283 if (!expr)
284 return false;
286 ao_ref_init (ref, expr);
288 /* Get the base of the reference and see if we have to reject or
289 adjust it. */
290 base = ao_ref_base (ref);
291 if (base == NULL_TREE)
292 return false;
294 /* The tree oracle doesn't like bases that are neither decls
295 nor indirect references of SSA names. */
296 if (!(DECL_P (base)
297 || (TREE_CODE (base) == MEM_REF
298 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
299 || (TREE_CODE (base) == TARGET_MEM_REF
300 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
301 return false;
303 /* If this is a reference based on a partitioned decl replace the
304 base with a MEM_REF of the pointer representative we
305 created during stack slot partitioning. */
306 if (TREE_CODE (base) == VAR_DECL
307 && ! is_global_var (base)
308 && cfun->gimple_df->decls_to_pointers != NULL)
310 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
311 if (namep)
312 ref->base = build_simple_mem_ref (*namep);
315 ref->ref_alias_set = MEM_ALIAS_SET (mem);
317 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
318 is conservative, so trust it. */
319 if (!MEM_OFFSET_KNOWN_P (mem)
320 || !MEM_SIZE_KNOWN_P (mem))
321 return true;
323 /* If the base decl is a parameter we can have negative MEM_OFFSET in
324 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
325 here. */
326 if (MEM_OFFSET (mem) < 0
327 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
328 return true;
330 /* Otherwise continue and refine size and offset we got from analyzing
331 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
333 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
334 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
336 /* The MEM may extend into adjacent fields, so adjust max_size if
337 necessary. */
338 if (ref->max_size != -1
339 && ref->size > ref->max_size)
340 ref->max_size = ref->size;
342 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
343 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
344 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
345 && (ref->offset < 0
346 || (DECL_P (ref->base)
347 && (DECL_SIZE (ref->base) == NULL_TREE
348 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
349 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
350 ref->offset + ref->size)))))
351 return false;
353 return true;
356 /* Query the alias-oracle on whether the two memory rtx X and MEM may
357 alias. If TBAA_P is set also apply TBAA. Returns true if the
358 two rtxen may alias, false otherwise. */
360 static bool
361 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
363 ao_ref ref1, ref2;
365 if (!ao_ref_from_mem (&ref1, x)
366 || !ao_ref_from_mem (&ref2, mem))
367 return true;
369 return refs_may_alias_p_1 (&ref1, &ref2,
370 tbaa_p
371 && MEM_ALIAS_SET (x) != 0
372 && MEM_ALIAS_SET (mem) != 0);
375 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
376 such an entry, or NULL otherwise. */
378 static inline alias_set_entry
379 get_alias_set_entry (alias_set_type alias_set)
381 return (*alias_sets)[alias_set];
384 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
385 the two MEMs cannot alias each other. */
387 static inline int
388 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
390 /* Perform a basic sanity check. Namely, that there are no alias sets
391 if we're not using strict aliasing. This helps to catch bugs
392 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
393 where a MEM is allocated in some way other than by the use of
394 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
395 use alias sets to indicate that spilled registers cannot alias each
396 other, we might need to remove this check. */
397 gcc_assert (flag_strict_aliasing
398 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
400 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
403 /* Insert the NODE into the splay tree given by DATA. Used by
404 record_alias_subset via splay_tree_foreach. */
406 static int
407 insert_subset_children (splay_tree_node node, void *data)
409 splay_tree_insert ((splay_tree) data, node->key, node->value);
411 return 0;
414 /* Return true if the first alias set is a subset of the second. */
416 bool
417 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
419 alias_set_entry ase;
421 /* Everything is a subset of the "aliases everything" set. */
422 if (set2 == 0)
423 return true;
425 /* Otherwise, check if set1 is a subset of set2. */
426 ase = get_alias_set_entry (set2);
427 if (ase != 0
428 && (ase->has_zero_child
429 || splay_tree_lookup (ase->children,
430 (splay_tree_key) set1)))
431 return true;
432 return false;
435 /* Return 1 if the two specified alias sets may conflict. */
438 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
440 alias_set_entry ase;
442 /* The easy case. */
443 if (alias_sets_must_conflict_p (set1, set2))
444 return 1;
446 /* See if the first alias set is a subset of the second. */
447 ase = get_alias_set_entry (set1);
448 if (ase != 0
449 && (ase->has_zero_child
450 || splay_tree_lookup (ase->children,
451 (splay_tree_key) set2)))
452 return 1;
454 /* Now do the same, but with the alias sets reversed. */
455 ase = get_alias_set_entry (set2);
456 if (ase != 0
457 && (ase->has_zero_child
458 || splay_tree_lookup (ase->children,
459 (splay_tree_key) set1)))
460 return 1;
462 /* The two alias sets are distinct and neither one is the
463 child of the other. Therefore, they cannot conflict. */
464 return 0;
467 /* Return 1 if the two specified alias sets will always conflict. */
470 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
472 if (set1 == 0 || set2 == 0 || set1 == set2)
473 return 1;
475 return 0;
478 /* Return 1 if any MEM object of type T1 will always conflict (using the
479 dependency routines in this file) with any MEM object of type T2.
480 This is used when allocating temporary storage. If T1 and/or T2 are
481 NULL_TREE, it means we know nothing about the storage. */
484 objects_must_conflict_p (tree t1, tree t2)
486 alias_set_type set1, set2;
488 /* If neither has a type specified, we don't know if they'll conflict
489 because we may be using them to store objects of various types, for
490 example the argument and local variables areas of inlined functions. */
491 if (t1 == 0 && t2 == 0)
492 return 0;
494 /* If they are the same type, they must conflict. */
495 if (t1 == t2
496 /* Likewise if both are volatile. */
497 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
498 return 1;
500 set1 = t1 ? get_alias_set (t1) : 0;
501 set2 = t2 ? get_alias_set (t2) : 0;
503 /* We can't use alias_sets_conflict_p because we must make sure
504 that every subtype of t1 will conflict with every subtype of
505 t2 for which a pair of subobjects of these respective subtypes
506 overlaps on the stack. */
507 return alias_sets_must_conflict_p (set1, set2);
510 /* Return the outermost parent of component present in the chain of
511 component references handled by get_inner_reference in T with the
512 following property:
513 - the component is non-addressable, or
514 - the parent has alias set zero,
515 or NULL_TREE if no such parent exists. In the former cases, the alias
516 set of this parent is the alias set that must be used for T itself. */
518 tree
519 component_uses_parent_alias_set_from (const_tree t)
521 const_tree found = NULL_TREE;
523 while (handled_component_p (t))
525 switch (TREE_CODE (t))
527 case COMPONENT_REF:
528 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
529 found = t;
530 break;
532 case ARRAY_REF:
533 case ARRAY_RANGE_REF:
534 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
535 found = t;
536 break;
538 case REALPART_EXPR:
539 case IMAGPART_EXPR:
540 break;
542 case BIT_FIELD_REF:
543 case VIEW_CONVERT_EXPR:
544 /* Bitfields and casts are never addressable. */
545 found = t;
546 break;
548 default:
549 gcc_unreachable ();
552 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
553 found = t;
555 t = TREE_OPERAND (t, 0);
558 if (found)
559 return TREE_OPERAND (found, 0);
561 return NULL_TREE;
565 /* Return whether the pointer-type T effective for aliasing may
566 access everything and thus the reference has to be assigned
567 alias-set zero. */
569 static bool
570 ref_all_alias_ptr_type_p (const_tree t)
572 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
573 || TYPE_REF_CAN_ALIAS_ALL (t));
576 /* Return the alias set for the memory pointed to by T, which may be
577 either a type or an expression. Return -1 if there is nothing
578 special about dereferencing T. */
580 static alias_set_type
581 get_deref_alias_set_1 (tree t)
583 /* All we care about is the type. */
584 if (! TYPE_P (t))
585 t = TREE_TYPE (t);
587 /* If we have an INDIRECT_REF via a void pointer, we don't
588 know anything about what that might alias. Likewise if the
589 pointer is marked that way. */
590 if (ref_all_alias_ptr_type_p (t))
591 return 0;
593 return -1;
596 /* Return the alias set for the memory pointed to by T, which may be
597 either a type or an expression. */
599 alias_set_type
600 get_deref_alias_set (tree t)
602 /* If we're not doing any alias analysis, just assume everything
603 aliases everything else. */
604 if (!flag_strict_aliasing)
605 return 0;
607 alias_set_type set = get_deref_alias_set_1 (t);
609 /* Fall back to the alias-set of the pointed-to type. */
610 if (set == -1)
612 if (! TYPE_P (t))
613 t = TREE_TYPE (t);
614 set = get_alias_set (TREE_TYPE (t));
617 return set;
620 /* Return the pointer-type relevant for TBAA purposes from the
621 memory reference tree *T or NULL_TREE in which case *T is
622 adjusted to point to the outermost component reference that
623 can be used for assigning an alias set. */
625 static tree
626 reference_alias_ptr_type_1 (tree *t)
628 tree inner;
630 /* Get the base object of the reference. */
631 inner = *t;
632 while (handled_component_p (inner))
634 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
635 the type of any component references that wrap it to
636 determine the alias-set. */
637 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
638 *t = TREE_OPERAND (inner, 0);
639 inner = TREE_OPERAND (inner, 0);
642 /* Handle pointer dereferences here, they can override the
643 alias-set. */
644 if (INDIRECT_REF_P (inner)
645 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
646 return TREE_TYPE (TREE_OPERAND (inner, 0));
647 else if (TREE_CODE (inner) == TARGET_MEM_REF)
648 return TREE_TYPE (TMR_OFFSET (inner));
649 else if (TREE_CODE (inner) == MEM_REF
650 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
651 return TREE_TYPE (TREE_OPERAND (inner, 1));
653 /* If the innermost reference is a MEM_REF that has a
654 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
655 using the memory access type for determining the alias-set. */
656 if (TREE_CODE (inner) == MEM_REF
657 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
658 != TYPE_MAIN_VARIANT
659 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
660 return TREE_TYPE (TREE_OPERAND (inner, 1));
662 /* Otherwise, pick up the outermost object that we could have
663 a pointer to. */
664 tree tem = component_uses_parent_alias_set_from (*t);
665 if (tem)
666 *t = tem;
668 return NULL_TREE;
671 /* Return the pointer-type relevant for TBAA purposes from the
672 gimple memory reference tree T. This is the type to be used for
673 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
674 and guarantees that get_alias_set will return the same alias
675 set for T and the replacement. */
677 tree
678 reference_alias_ptr_type (tree t)
680 tree ptype = reference_alias_ptr_type_1 (&t);
681 /* If there is a given pointer type for aliasing purposes, return it. */
682 if (ptype != NULL_TREE)
683 return ptype;
685 /* Otherwise build one from the outermost component reference we
686 may use. */
687 if (TREE_CODE (t) == MEM_REF
688 || TREE_CODE (t) == TARGET_MEM_REF)
689 return TREE_TYPE (TREE_OPERAND (t, 1));
690 else
691 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
694 /* Return whether the pointer-types T1 and T2 used to determine
695 two alias sets of two references will yield the same answer
696 from get_deref_alias_set. */
698 bool
699 alias_ptr_types_compatible_p (tree t1, tree t2)
701 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
702 return true;
704 if (ref_all_alias_ptr_type_p (t1)
705 || ref_all_alias_ptr_type_p (t2))
706 return false;
708 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
709 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
712 /* Return the alias set for T, which may be either a type or an
713 expression. Call language-specific routine for help, if needed. */
715 alias_set_type
716 get_alias_set (tree t)
718 alias_set_type set;
720 /* If we're not doing any alias analysis, just assume everything
721 aliases everything else. Also return 0 if this or its type is
722 an error. */
723 if (! flag_strict_aliasing || t == error_mark_node
724 || (! TYPE_P (t)
725 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
726 return 0;
728 /* We can be passed either an expression or a type. This and the
729 language-specific routine may make mutually-recursive calls to each other
730 to figure out what to do. At each juncture, we see if this is a tree
731 that the language may need to handle specially. First handle things that
732 aren't types. */
733 if (! TYPE_P (t))
735 /* Give the language a chance to do something with this tree
736 before we look at it. */
737 STRIP_NOPS (t);
738 set = lang_hooks.get_alias_set (t);
739 if (set != -1)
740 return set;
742 /* Get the alias pointer-type to use or the outermost object
743 that we could have a pointer to. */
744 tree ptype = reference_alias_ptr_type_1 (&t);
745 if (ptype != NULL)
746 return get_deref_alias_set (ptype);
748 /* If we've already determined the alias set for a decl, just return
749 it. This is necessary for C++ anonymous unions, whose component
750 variables don't look like union members (boo!). */
751 if (TREE_CODE (t) == VAR_DECL
752 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
753 return MEM_ALIAS_SET (DECL_RTL (t));
755 /* Now all we care about is the type. */
756 t = TREE_TYPE (t);
759 /* Variant qualifiers don't affect the alias set, so get the main
760 variant. */
761 t = TYPE_MAIN_VARIANT (t);
763 /* Always use the canonical type as well. If this is a type that
764 requires structural comparisons to identify compatible types
765 use alias set zero. */
766 if (TYPE_STRUCTURAL_EQUALITY_P (t))
768 /* Allow the language to specify another alias set for this
769 type. */
770 set = lang_hooks.get_alias_set (t);
771 if (set != -1)
772 return set;
773 return 0;
776 t = TYPE_CANONICAL (t);
778 /* The canonical type should not require structural equality checks. */
779 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
781 /* If this is a type with a known alias set, return it. */
782 if (TYPE_ALIAS_SET_KNOWN_P (t))
783 return TYPE_ALIAS_SET (t);
785 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
786 if (!COMPLETE_TYPE_P (t))
788 /* For arrays with unknown size the conservative answer is the
789 alias set of the element type. */
790 if (TREE_CODE (t) == ARRAY_TYPE)
791 return get_alias_set (TREE_TYPE (t));
793 /* But return zero as a conservative answer for incomplete types. */
794 return 0;
797 /* See if the language has special handling for this type. */
798 set = lang_hooks.get_alias_set (t);
799 if (set != -1)
800 return set;
802 /* There are no objects of FUNCTION_TYPE, so there's no point in
803 using up an alias set for them. (There are, of course, pointers
804 and references to functions, but that's different.) */
805 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
806 set = 0;
808 /* Unless the language specifies otherwise, let vector types alias
809 their components. This avoids some nasty type punning issues in
810 normal usage. And indeed lets vectors be treated more like an
811 array slice. */
812 else if (TREE_CODE (t) == VECTOR_TYPE)
813 set = get_alias_set (TREE_TYPE (t));
815 /* Unless the language specifies otherwise, treat array types the
816 same as their components. This avoids the asymmetry we get
817 through recording the components. Consider accessing a
818 character(kind=1) through a reference to a character(kind=1)[1:1].
819 Or consider if we want to assign integer(kind=4)[0:D.1387] and
820 integer(kind=4)[4] the same alias set or not.
821 Just be pragmatic here and make sure the array and its element
822 type get the same alias set assigned. */
823 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
824 set = get_alias_set (TREE_TYPE (t));
826 /* From the former common C and C++ langhook implementation:
828 Unfortunately, there is no canonical form of a pointer type.
829 In particular, if we have `typedef int I', then `int *', and
830 `I *' are different types. So, we have to pick a canonical
831 representative. We do this below.
833 Technically, this approach is actually more conservative that
834 it needs to be. In particular, `const int *' and `int *'
835 should be in different alias sets, according to the C and C++
836 standard, since their types are not the same, and so,
837 technically, an `int **' and `const int **' cannot point at
838 the same thing.
840 But, the standard is wrong. In particular, this code is
841 legal C++:
843 int *ip;
844 int **ipp = &ip;
845 const int* const* cipp = ipp;
846 And, it doesn't make sense for that to be legal unless you
847 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
848 the pointed-to types. This issue has been reported to the
849 C++ committee.
851 In addition to the above canonicalization issue, with LTO
852 we should also canonicalize `T (*)[]' to `T *' avoiding
853 alias issues with pointer-to element types and pointer-to
854 array types.
856 Likewise we need to deal with the situation of incomplete
857 pointed-to types and make `*(struct X **)&a' and
858 `*(struct X {} **)&a' alias. Otherwise we will have to
859 guarantee that all pointer-to incomplete type variants
860 will be replaced by pointer-to complete type variants if
861 they are available.
863 With LTO the convenient situation of using `void *' to
864 access and store any pointer type will also become
865 more apparent (and `void *' is just another pointer-to
866 incomplete type). Assigning alias-set zero to `void *'
867 and all pointer-to incomplete types is a not appealing
868 solution. Assigning an effective alias-set zero only
869 affecting pointers might be - by recording proper subset
870 relationships of all pointer alias-sets.
872 Pointer-to function types are another grey area which
873 needs caution. Globbing them all into one alias-set
874 or the above effective zero set would work.
876 For now just assign the same alias-set to all pointers.
877 That's simple and avoids all the above problems. */
878 else if (POINTER_TYPE_P (t)
879 && t != ptr_type_node)
880 set = get_alias_set (ptr_type_node);
882 /* Otherwise make a new alias set for this type. */
883 else
885 /* Each canonical type gets its own alias set, so canonical types
886 shouldn't form a tree. It doesn't really matter for types
887 we handle specially above, so only check it where it possibly
888 would result in a bogus alias set. */
889 gcc_checking_assert (TYPE_CANONICAL (t) == t);
891 set = new_alias_set ();
894 TYPE_ALIAS_SET (t) = set;
896 /* If this is an aggregate type or a complex type, we must record any
897 component aliasing information. */
898 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
899 record_component_aliases (t);
901 return set;
904 /* Return a brand-new alias set. */
906 alias_set_type
907 new_alias_set (void)
909 if (flag_strict_aliasing)
911 if (alias_sets == 0)
912 vec_safe_push (alias_sets, (alias_set_entry) 0);
913 vec_safe_push (alias_sets, (alias_set_entry) 0);
914 return alias_sets->length () - 1;
916 else
917 return 0;
920 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
921 not everything that aliases SUPERSET also aliases SUBSET. For example,
922 in C, a store to an `int' can alias a load of a structure containing an
923 `int', and vice versa. But it can't alias a load of a 'double' member
924 of the same structure. Here, the structure would be the SUPERSET and
925 `int' the SUBSET. This relationship is also described in the comment at
926 the beginning of this file.
928 This function should be called only once per SUPERSET/SUBSET pair.
930 It is illegal for SUPERSET to be zero; everything is implicitly a
931 subset of alias set zero. */
933 void
934 record_alias_subset (alias_set_type superset, alias_set_type subset)
936 alias_set_entry superset_entry;
937 alias_set_entry subset_entry;
939 /* It is possible in complex type situations for both sets to be the same,
940 in which case we can ignore this operation. */
941 if (superset == subset)
942 return;
944 gcc_assert (superset);
946 superset_entry = get_alias_set_entry (superset);
947 if (superset_entry == 0)
949 /* Create an entry for the SUPERSET, so that we have a place to
950 attach the SUBSET. */
951 superset_entry = ggc_cleared_alloc<alias_set_entry_d> ();
952 superset_entry->alias_set = superset;
953 superset_entry->children
954 = splay_tree_new_ggc (splay_tree_compare_ints,
955 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s,
956 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s);
957 superset_entry->has_zero_child = 0;
958 (*alias_sets)[superset] = superset_entry;
961 if (subset == 0)
962 superset_entry->has_zero_child = 1;
963 else
965 subset_entry = get_alias_set_entry (subset);
966 /* If there is an entry for the subset, enter all of its children
967 (if they are not already present) as children of the SUPERSET. */
968 if (subset_entry)
970 if (subset_entry->has_zero_child)
971 superset_entry->has_zero_child = 1;
973 splay_tree_foreach (subset_entry->children, insert_subset_children,
974 superset_entry->children);
977 /* Enter the SUBSET itself as a child of the SUPERSET. */
978 splay_tree_insert (superset_entry->children,
979 (splay_tree_key) subset, 0);
983 /* Record that component types of TYPE, if any, are part of that type for
984 aliasing purposes. For record types, we only record component types
985 for fields that are not marked non-addressable. For array types, we
986 only record the component type if it is not marked non-aliased. */
988 void
989 record_component_aliases (tree type)
991 alias_set_type superset = get_alias_set (type);
992 tree field;
994 if (superset == 0)
995 return;
997 switch (TREE_CODE (type))
999 case RECORD_TYPE:
1000 case UNION_TYPE:
1001 case QUAL_UNION_TYPE:
1002 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1003 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1004 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1005 break;
1007 case COMPLEX_TYPE:
1008 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1009 break;
1011 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1012 element type. */
1014 default:
1015 break;
1019 /* Allocate an alias set for use in storing and reading from the varargs
1020 spill area. */
1022 static GTY(()) alias_set_type varargs_set = -1;
1024 alias_set_type
1025 get_varargs_alias_set (void)
1027 #if 1
1028 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1029 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1030 consistently use the varargs alias set for loads from the varargs
1031 area. So don't use it anywhere. */
1032 return 0;
1033 #else
1034 if (varargs_set == -1)
1035 varargs_set = new_alias_set ();
1037 return varargs_set;
1038 #endif
1041 /* Likewise, but used for the fixed portions of the frame, e.g., register
1042 save areas. */
1044 static GTY(()) alias_set_type frame_set = -1;
1046 alias_set_type
1047 get_frame_alias_set (void)
1049 if (frame_set == -1)
1050 frame_set = new_alias_set ();
1052 return frame_set;
1055 /* Create a new, unique base with id ID. */
1057 static rtx
1058 unique_base_value (HOST_WIDE_INT id)
1060 return gen_rtx_ADDRESS (Pmode, id);
1063 /* Return true if accesses based on any other base value cannot alias
1064 those based on X. */
1066 static bool
1067 unique_base_value_p (rtx x)
1069 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1072 /* Return true if X is known to be a base value. */
1074 static bool
1075 known_base_value_p (rtx x)
1077 switch (GET_CODE (x))
1079 case LABEL_REF:
1080 case SYMBOL_REF:
1081 return true;
1083 case ADDRESS:
1084 /* Arguments may or may not be bases; we don't know for sure. */
1085 return GET_MODE (x) != VOIDmode;
1087 default:
1088 return false;
1092 /* Inside SRC, the source of a SET, find a base address. */
1094 static rtx
1095 find_base_value (rtx src)
1097 unsigned int regno;
1099 #if defined (FIND_BASE_TERM)
1100 /* Try machine-dependent ways to find the base term. */
1101 src = FIND_BASE_TERM (src);
1102 #endif
1104 switch (GET_CODE (src))
1106 case SYMBOL_REF:
1107 case LABEL_REF:
1108 return src;
1110 case REG:
1111 regno = REGNO (src);
1112 /* At the start of a function, argument registers have known base
1113 values which may be lost later. Returning an ADDRESS
1114 expression here allows optimization based on argument values
1115 even when the argument registers are used for other purposes. */
1116 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1117 return new_reg_base_value[regno];
1119 /* If a pseudo has a known base value, return it. Do not do this
1120 for non-fixed hard regs since it can result in a circular
1121 dependency chain for registers which have values at function entry.
1123 The test above is not sufficient because the scheduler may move
1124 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1125 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1126 && regno < vec_safe_length (reg_base_value))
1128 /* If we're inside init_alias_analysis, use new_reg_base_value
1129 to reduce the number of relaxation iterations. */
1130 if (new_reg_base_value && new_reg_base_value[regno]
1131 && DF_REG_DEF_COUNT (regno) == 1)
1132 return new_reg_base_value[regno];
1134 if ((*reg_base_value)[regno])
1135 return (*reg_base_value)[regno];
1138 return 0;
1140 case MEM:
1141 /* Check for an argument passed in memory. Only record in the
1142 copying-arguments block; it is too hard to track changes
1143 otherwise. */
1144 if (copying_arguments
1145 && (XEXP (src, 0) == arg_pointer_rtx
1146 || (GET_CODE (XEXP (src, 0)) == PLUS
1147 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1148 return arg_base_value;
1149 return 0;
1151 case CONST:
1152 src = XEXP (src, 0);
1153 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1154 break;
1156 /* ... fall through ... */
1158 case PLUS:
1159 case MINUS:
1161 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1163 /* If either operand is a REG that is a known pointer, then it
1164 is the base. */
1165 if (REG_P (src_0) && REG_POINTER (src_0))
1166 return find_base_value (src_0);
1167 if (REG_P (src_1) && REG_POINTER (src_1))
1168 return find_base_value (src_1);
1170 /* If either operand is a REG, then see if we already have
1171 a known value for it. */
1172 if (REG_P (src_0))
1174 temp = find_base_value (src_0);
1175 if (temp != 0)
1176 src_0 = temp;
1179 if (REG_P (src_1))
1181 temp = find_base_value (src_1);
1182 if (temp!= 0)
1183 src_1 = temp;
1186 /* If either base is named object or a special address
1187 (like an argument or stack reference), then use it for the
1188 base term. */
1189 if (src_0 != 0 && known_base_value_p (src_0))
1190 return src_0;
1192 if (src_1 != 0 && known_base_value_p (src_1))
1193 return src_1;
1195 /* Guess which operand is the base address:
1196 If either operand is a symbol, then it is the base. If
1197 either operand is a CONST_INT, then the other is the base. */
1198 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1199 return find_base_value (src_0);
1200 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1201 return find_base_value (src_1);
1203 return 0;
1206 case LO_SUM:
1207 /* The standard form is (lo_sum reg sym) so look only at the
1208 second operand. */
1209 return find_base_value (XEXP (src, 1));
1211 case AND:
1212 /* If the second operand is constant set the base
1213 address to the first operand. */
1214 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1215 return find_base_value (XEXP (src, 0));
1216 return 0;
1218 case TRUNCATE:
1219 /* As we do not know which address space the pointer is referring to, we can
1220 handle this only if the target does not support different pointer or
1221 address modes depending on the address space. */
1222 if (!target_default_pointer_address_modes_p ())
1223 break;
1224 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1225 break;
1226 /* Fall through. */
1227 case HIGH:
1228 case PRE_INC:
1229 case PRE_DEC:
1230 case POST_INC:
1231 case POST_DEC:
1232 case PRE_MODIFY:
1233 case POST_MODIFY:
1234 return find_base_value (XEXP (src, 0));
1236 case ZERO_EXTEND:
1237 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1238 /* As we do not know which address space the pointer is referring to, we can
1239 handle this only if the target does not support different pointer or
1240 address modes depending on the address space. */
1241 if (!target_default_pointer_address_modes_p ())
1242 break;
1245 rtx temp = find_base_value (XEXP (src, 0));
1247 if (temp != 0 && CONSTANT_P (temp))
1248 temp = convert_memory_address (Pmode, temp);
1250 return temp;
1253 default:
1254 break;
1257 return 0;
1260 /* Called from init_alias_analysis indirectly through note_stores,
1261 or directly if DEST is a register with a REG_NOALIAS note attached.
1262 SET is null in the latter case. */
1264 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1265 register N has been set in this function. */
1266 static sbitmap reg_seen;
1268 static void
1269 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1271 unsigned regno;
1272 rtx src;
1273 int n;
1275 if (!REG_P (dest))
1276 return;
1278 regno = REGNO (dest);
1280 gcc_checking_assert (regno < reg_base_value->length ());
1282 /* If this spans multiple hard registers, then we must indicate that every
1283 register has an unusable value. */
1284 if (regno < FIRST_PSEUDO_REGISTER)
1285 n = hard_regno_nregs[regno][GET_MODE (dest)];
1286 else
1287 n = 1;
1288 if (n != 1)
1290 while (--n >= 0)
1292 bitmap_set_bit (reg_seen, regno + n);
1293 new_reg_base_value[regno + n] = 0;
1295 return;
1298 if (set)
1300 /* A CLOBBER wipes out any old value but does not prevent a previously
1301 unset register from acquiring a base address (i.e. reg_seen is not
1302 set). */
1303 if (GET_CODE (set) == CLOBBER)
1305 new_reg_base_value[regno] = 0;
1306 return;
1308 src = SET_SRC (set);
1310 else
1312 /* There's a REG_NOALIAS note against DEST. */
1313 if (bitmap_bit_p (reg_seen, regno))
1315 new_reg_base_value[regno] = 0;
1316 return;
1318 bitmap_set_bit (reg_seen, regno);
1319 new_reg_base_value[regno] = unique_base_value (unique_id++);
1320 return;
1323 /* If this is not the first set of REGNO, see whether the new value
1324 is related to the old one. There are two cases of interest:
1326 (1) The register might be assigned an entirely new value
1327 that has the same base term as the original set.
1329 (2) The set might be a simple self-modification that
1330 cannot change REGNO's base value.
1332 If neither case holds, reject the original base value as invalid.
1333 Note that the following situation is not detected:
1335 extern int x, y; int *p = &x; p += (&y-&x);
1337 ANSI C does not allow computing the difference of addresses
1338 of distinct top level objects. */
1339 if (new_reg_base_value[regno] != 0
1340 && find_base_value (src) != new_reg_base_value[regno])
1341 switch (GET_CODE (src))
1343 case LO_SUM:
1344 case MINUS:
1345 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1346 new_reg_base_value[regno] = 0;
1347 break;
1348 case PLUS:
1349 /* If the value we add in the PLUS is also a valid base value,
1350 this might be the actual base value, and the original value
1351 an index. */
1353 rtx other = NULL_RTX;
1355 if (XEXP (src, 0) == dest)
1356 other = XEXP (src, 1);
1357 else if (XEXP (src, 1) == dest)
1358 other = XEXP (src, 0);
1360 if (! other || find_base_value (other))
1361 new_reg_base_value[regno] = 0;
1362 break;
1364 case AND:
1365 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1366 new_reg_base_value[regno] = 0;
1367 break;
1368 default:
1369 new_reg_base_value[regno] = 0;
1370 break;
1372 /* If this is the first set of a register, record the value. */
1373 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1374 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1375 new_reg_base_value[regno] = find_base_value (src);
1377 bitmap_set_bit (reg_seen, regno);
1380 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1381 using hard registers with non-null REG_BASE_VALUE for renaming. */
1383 get_reg_base_value (unsigned int regno)
1385 return (*reg_base_value)[regno];
1388 /* If a value is known for REGNO, return it. */
1391 get_reg_known_value (unsigned int regno)
1393 if (regno >= FIRST_PSEUDO_REGISTER)
1395 regno -= FIRST_PSEUDO_REGISTER;
1396 if (regno < vec_safe_length (reg_known_value))
1397 return (*reg_known_value)[regno];
1399 return NULL;
1402 /* Set it. */
1404 static void
1405 set_reg_known_value (unsigned int regno, rtx val)
1407 if (regno >= FIRST_PSEUDO_REGISTER)
1409 regno -= FIRST_PSEUDO_REGISTER;
1410 if (regno < vec_safe_length (reg_known_value))
1411 (*reg_known_value)[regno] = val;
1415 /* Similarly for reg_known_equiv_p. */
1417 bool
1418 get_reg_known_equiv_p (unsigned int regno)
1420 if (regno >= FIRST_PSEUDO_REGISTER)
1422 regno -= FIRST_PSEUDO_REGISTER;
1423 if (regno < vec_safe_length (reg_known_value))
1424 return bitmap_bit_p (reg_known_equiv_p, regno);
1426 return false;
1429 static void
1430 set_reg_known_equiv_p (unsigned int regno, bool val)
1432 if (regno >= FIRST_PSEUDO_REGISTER)
1434 regno -= FIRST_PSEUDO_REGISTER;
1435 if (regno < vec_safe_length (reg_known_value))
1437 if (val)
1438 bitmap_set_bit (reg_known_equiv_p, regno);
1439 else
1440 bitmap_clear_bit (reg_known_equiv_p, regno);
1446 /* Returns a canonical version of X, from the point of view alias
1447 analysis. (For example, if X is a MEM whose address is a register,
1448 and the register has a known value (say a SYMBOL_REF), then a MEM
1449 whose address is the SYMBOL_REF is returned.) */
1452 canon_rtx (rtx x)
1454 /* Recursively look for equivalences. */
1455 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1457 rtx t = get_reg_known_value (REGNO (x));
1458 if (t == x)
1459 return x;
1460 if (t)
1461 return canon_rtx (t);
1464 if (GET_CODE (x) == PLUS)
1466 rtx x0 = canon_rtx (XEXP (x, 0));
1467 rtx x1 = canon_rtx (XEXP (x, 1));
1469 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1471 if (CONST_INT_P (x0))
1472 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1473 else if (CONST_INT_P (x1))
1474 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1475 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1479 /* This gives us much better alias analysis when called from
1480 the loop optimizer. Note we want to leave the original
1481 MEM alone, but need to return the canonicalized MEM with
1482 all the flags with their original values. */
1483 else if (MEM_P (x))
1484 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1486 return x;
1489 /* Return 1 if X and Y are identical-looking rtx's.
1490 Expect that X and Y has been already canonicalized.
1492 We use the data in reg_known_value above to see if two registers with
1493 different numbers are, in fact, equivalent. */
1495 static int
1496 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1498 int i;
1499 int j;
1500 enum rtx_code code;
1501 const char *fmt;
1503 if (x == 0 && y == 0)
1504 return 1;
1505 if (x == 0 || y == 0)
1506 return 0;
1508 if (x == y)
1509 return 1;
1511 code = GET_CODE (x);
1512 /* Rtx's of different codes cannot be equal. */
1513 if (code != GET_CODE (y))
1514 return 0;
1516 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1517 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1519 if (GET_MODE (x) != GET_MODE (y))
1520 return 0;
1522 /* Some RTL can be compared without a recursive examination. */
1523 switch (code)
1525 case REG:
1526 return REGNO (x) == REGNO (y);
1528 case LABEL_REF:
1529 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1531 case SYMBOL_REF:
1532 return XSTR (x, 0) == XSTR (y, 0);
1534 case ENTRY_VALUE:
1535 /* This is magic, don't go through canonicalization et al. */
1536 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1538 case VALUE:
1539 CASE_CONST_UNIQUE:
1540 /* Pointer equality guarantees equality for these nodes. */
1541 return 0;
1543 default:
1544 break;
1547 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1548 if (code == PLUS)
1549 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1550 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1551 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1552 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1553 /* For commutative operations, the RTX match if the operand match in any
1554 order. Also handle the simple binary and unary cases without a loop. */
1555 if (COMMUTATIVE_P (x))
1557 rtx xop0 = canon_rtx (XEXP (x, 0));
1558 rtx yop0 = canon_rtx (XEXP (y, 0));
1559 rtx yop1 = canon_rtx (XEXP (y, 1));
1561 return ((rtx_equal_for_memref_p (xop0, yop0)
1562 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1563 || (rtx_equal_for_memref_p (xop0, yop1)
1564 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1566 else if (NON_COMMUTATIVE_P (x))
1568 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1569 canon_rtx (XEXP (y, 0)))
1570 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1571 canon_rtx (XEXP (y, 1))));
1573 else if (UNARY_P (x))
1574 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1575 canon_rtx (XEXP (y, 0)));
1577 /* Compare the elements. If any pair of corresponding elements
1578 fail to match, return 0 for the whole things.
1580 Limit cases to types which actually appear in addresses. */
1582 fmt = GET_RTX_FORMAT (code);
1583 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1585 switch (fmt[i])
1587 case 'i':
1588 if (XINT (x, i) != XINT (y, i))
1589 return 0;
1590 break;
1592 case 'E':
1593 /* Two vectors must have the same length. */
1594 if (XVECLEN (x, i) != XVECLEN (y, i))
1595 return 0;
1597 /* And the corresponding elements must match. */
1598 for (j = 0; j < XVECLEN (x, i); j++)
1599 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1600 canon_rtx (XVECEXP (y, i, j))) == 0)
1601 return 0;
1602 break;
1604 case 'e':
1605 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1606 canon_rtx (XEXP (y, i))) == 0)
1607 return 0;
1608 break;
1610 /* This can happen for asm operands. */
1611 case 's':
1612 if (strcmp (XSTR (x, i), XSTR (y, i)))
1613 return 0;
1614 break;
1616 /* This can happen for an asm which clobbers memory. */
1617 case '0':
1618 break;
1620 /* It is believed that rtx's at this level will never
1621 contain anything but integers and other rtx's,
1622 except for within LABEL_REFs and SYMBOL_REFs. */
1623 default:
1624 gcc_unreachable ();
1627 return 1;
1630 static rtx
1631 find_base_term (rtx x)
1633 cselib_val *val;
1634 struct elt_loc_list *l, *f;
1635 rtx ret;
1637 #if defined (FIND_BASE_TERM)
1638 /* Try machine-dependent ways to find the base term. */
1639 x = FIND_BASE_TERM (x);
1640 #endif
1642 switch (GET_CODE (x))
1644 case REG:
1645 return REG_BASE_VALUE (x);
1647 case TRUNCATE:
1648 /* As we do not know which address space the pointer is referring to, we can
1649 handle this only if the target does not support different pointer or
1650 address modes depending on the address space. */
1651 if (!target_default_pointer_address_modes_p ())
1652 return 0;
1653 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1654 return 0;
1655 /* Fall through. */
1656 case HIGH:
1657 case PRE_INC:
1658 case PRE_DEC:
1659 case POST_INC:
1660 case POST_DEC:
1661 case PRE_MODIFY:
1662 case POST_MODIFY:
1663 return find_base_term (XEXP (x, 0));
1665 case ZERO_EXTEND:
1666 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1667 /* As we do not know which address space the pointer is referring to, we can
1668 handle this only if the target does not support different pointer or
1669 address modes depending on the address space. */
1670 if (!target_default_pointer_address_modes_p ())
1671 return 0;
1674 rtx temp = find_base_term (XEXP (x, 0));
1676 if (temp != 0 && CONSTANT_P (temp))
1677 temp = convert_memory_address (Pmode, temp);
1679 return temp;
1682 case VALUE:
1683 val = CSELIB_VAL_PTR (x);
1684 ret = NULL_RTX;
1686 if (!val)
1687 return ret;
1689 if (cselib_sp_based_value_p (val))
1690 return static_reg_base_value[STACK_POINTER_REGNUM];
1692 f = val->locs;
1693 /* Temporarily reset val->locs to avoid infinite recursion. */
1694 val->locs = NULL;
1696 for (l = f; l; l = l->next)
1697 if (GET_CODE (l->loc) == VALUE
1698 && CSELIB_VAL_PTR (l->loc)->locs
1699 && !CSELIB_VAL_PTR (l->loc)->locs->next
1700 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1701 continue;
1702 else if ((ret = find_base_term (l->loc)) != 0)
1703 break;
1705 val->locs = f;
1706 return ret;
1708 case LO_SUM:
1709 /* The standard form is (lo_sum reg sym) so look only at the
1710 second operand. */
1711 return find_base_term (XEXP (x, 1));
1713 case CONST:
1714 x = XEXP (x, 0);
1715 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1716 return 0;
1717 /* Fall through. */
1718 case PLUS:
1719 case MINUS:
1721 rtx tmp1 = XEXP (x, 0);
1722 rtx tmp2 = XEXP (x, 1);
1724 /* This is a little bit tricky since we have to determine which of
1725 the two operands represents the real base address. Otherwise this
1726 routine may return the index register instead of the base register.
1728 That may cause us to believe no aliasing was possible, when in
1729 fact aliasing is possible.
1731 We use a few simple tests to guess the base register. Additional
1732 tests can certainly be added. For example, if one of the operands
1733 is a shift or multiply, then it must be the index register and the
1734 other operand is the base register. */
1736 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1737 return find_base_term (tmp2);
1739 /* If either operand is known to be a pointer, then prefer it
1740 to determine the base term. */
1741 if (REG_P (tmp1) && REG_POINTER (tmp1))
1743 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1745 rtx tem = tmp1;
1746 tmp1 = tmp2;
1747 tmp2 = tem;
1750 /* Go ahead and find the base term for both operands. If either base
1751 term is from a pointer or is a named object or a special address
1752 (like an argument or stack reference), then use it for the
1753 base term. */
1754 rtx base = find_base_term (tmp1);
1755 if (base != NULL_RTX
1756 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1757 || known_base_value_p (base)))
1758 return base;
1759 base = find_base_term (tmp2);
1760 if (base != NULL_RTX
1761 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1762 || known_base_value_p (base)))
1763 return base;
1765 /* We could not determine which of the two operands was the
1766 base register and which was the index. So we can determine
1767 nothing from the base alias check. */
1768 return 0;
1771 case AND:
1772 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1773 return find_base_term (XEXP (x, 0));
1774 return 0;
1776 case SYMBOL_REF:
1777 case LABEL_REF:
1778 return x;
1780 default:
1781 return 0;
1785 /* Return true if accesses to address X may alias accesses based
1786 on the stack pointer. */
1788 bool
1789 may_be_sp_based_p (rtx x)
1791 rtx base = find_base_term (x);
1792 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1795 /* Return 0 if the addresses X and Y are known to point to different
1796 objects, 1 if they might be pointers to the same object. */
1798 static int
1799 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1800 enum machine_mode x_mode, enum machine_mode y_mode)
1802 /* If the address itself has no known base see if a known equivalent
1803 value has one. If either address still has no known base, nothing
1804 is known about aliasing. */
1805 if (x_base == 0)
1807 rtx x_c;
1809 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1810 return 1;
1812 x_base = find_base_term (x_c);
1813 if (x_base == 0)
1814 return 1;
1817 if (y_base == 0)
1819 rtx y_c;
1820 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1821 return 1;
1823 y_base = find_base_term (y_c);
1824 if (y_base == 0)
1825 return 1;
1828 /* If the base addresses are equal nothing is known about aliasing. */
1829 if (rtx_equal_p (x_base, y_base))
1830 return 1;
1832 /* The base addresses are different expressions. If they are not accessed
1833 via AND, there is no conflict. We can bring knowledge of object
1834 alignment into play here. For example, on alpha, "char a, b;" can
1835 alias one another, though "char a; long b;" cannot. AND addesses may
1836 implicitly alias surrounding objects; i.e. unaligned access in DImode
1837 via AND address can alias all surrounding object types except those
1838 with aligment 8 or higher. */
1839 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1840 return 1;
1841 if (GET_CODE (x) == AND
1842 && (!CONST_INT_P (XEXP (x, 1))
1843 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1844 return 1;
1845 if (GET_CODE (y) == AND
1846 && (!CONST_INT_P (XEXP (y, 1))
1847 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1848 return 1;
1850 /* Differing symbols not accessed via AND never alias. */
1851 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1852 return 0;
1854 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
1855 return 0;
1857 return 1;
1860 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1861 that of V. */
1863 static bool
1864 refs_newer_value_p (const_rtx expr, rtx v)
1866 int minuid = CSELIB_VAL_PTR (v)->uid;
1867 subrtx_iterator::array_type array;
1868 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
1869 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
1870 return true;
1871 return false;
1874 /* Convert the address X into something we can use. This is done by returning
1875 it unchanged unless it is a value; in the latter case we call cselib to get
1876 a more useful rtx. */
1879 get_addr (rtx x)
1881 cselib_val *v;
1882 struct elt_loc_list *l;
1884 if (GET_CODE (x) != VALUE)
1885 return x;
1886 v = CSELIB_VAL_PTR (x);
1887 if (v)
1889 bool have_equivs = cselib_have_permanent_equivalences ();
1890 if (have_equivs)
1891 v = canonical_cselib_val (v);
1892 for (l = v->locs; l; l = l->next)
1893 if (CONSTANT_P (l->loc))
1894 return l->loc;
1895 for (l = v->locs; l; l = l->next)
1896 if (!REG_P (l->loc) && !MEM_P (l->loc)
1897 /* Avoid infinite recursion when potentially dealing with
1898 var-tracking artificial equivalences, by skipping the
1899 equivalences themselves, and not choosing expressions
1900 that refer to newer VALUEs. */
1901 && (!have_equivs
1902 || (GET_CODE (l->loc) != VALUE
1903 && !refs_newer_value_p (l->loc, x))))
1904 return l->loc;
1905 if (have_equivs)
1907 for (l = v->locs; l; l = l->next)
1908 if (REG_P (l->loc)
1909 || (GET_CODE (l->loc) != VALUE
1910 && !refs_newer_value_p (l->loc, x)))
1911 return l->loc;
1912 /* Return the canonical value. */
1913 return v->val_rtx;
1915 if (v->locs)
1916 return v->locs->loc;
1918 return x;
1921 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1922 where SIZE is the size in bytes of the memory reference. If ADDR
1923 is not modified by the memory reference then ADDR is returned. */
1925 static rtx
1926 addr_side_effect_eval (rtx addr, int size, int n_refs)
1928 int offset = 0;
1930 switch (GET_CODE (addr))
1932 case PRE_INC:
1933 offset = (n_refs + 1) * size;
1934 break;
1935 case PRE_DEC:
1936 offset = -(n_refs + 1) * size;
1937 break;
1938 case POST_INC:
1939 offset = n_refs * size;
1940 break;
1941 case POST_DEC:
1942 offset = -n_refs * size;
1943 break;
1945 default:
1946 return addr;
1949 if (offset)
1950 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1951 gen_int_mode (offset, GET_MODE (addr)));
1952 else
1953 addr = XEXP (addr, 0);
1954 addr = canon_rtx (addr);
1956 return addr;
1959 /* Return TRUE if an object X sized at XSIZE bytes and another object
1960 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1961 any of the sizes is zero, assume an overlap, otherwise use the
1962 absolute value of the sizes as the actual sizes. */
1964 static inline bool
1965 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
1967 return (xsize == 0 || ysize == 0
1968 || (c >= 0
1969 ? (abs (xsize) > c)
1970 : (abs (ysize) > -c)));
1973 /* Return one if X and Y (memory addresses) reference the
1974 same location in memory or if the references overlap.
1975 Return zero if they do not overlap, else return
1976 minus one in which case they still might reference the same location.
1978 C is an offset accumulator. When
1979 C is nonzero, we are testing aliases between X and Y + C.
1980 XSIZE is the size in bytes of the X reference,
1981 similarly YSIZE is the size in bytes for Y.
1982 Expect that canon_rtx has been already called for X and Y.
1984 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1985 referenced (the reference was BLKmode), so make the most pessimistic
1986 assumptions.
1988 If XSIZE or YSIZE is negative, we may access memory outside the object
1989 being referenced as a side effect. This can happen when using AND to
1990 align memory references, as is done on the Alpha.
1992 Nice to notice that varying addresses cannot conflict with fp if no
1993 local variables had their addresses taken, but that's too hard now.
1995 ??? Contrary to the tree alias oracle this does not return
1996 one for X + non-constant and Y + non-constant when X and Y are equal.
1997 If that is fixed the TBAA hack for union type-punning can be removed. */
1999 static int
2000 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2002 if (GET_CODE (x) == VALUE)
2004 if (REG_P (y))
2006 struct elt_loc_list *l = NULL;
2007 if (CSELIB_VAL_PTR (x))
2008 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2009 l; l = l->next)
2010 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2011 break;
2012 if (l)
2013 x = y;
2014 else
2015 x = get_addr (x);
2017 /* Don't call get_addr if y is the same VALUE. */
2018 else if (x != y)
2019 x = get_addr (x);
2021 if (GET_CODE (y) == VALUE)
2023 if (REG_P (x))
2025 struct elt_loc_list *l = NULL;
2026 if (CSELIB_VAL_PTR (y))
2027 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2028 l; l = l->next)
2029 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2030 break;
2031 if (l)
2032 y = x;
2033 else
2034 y = get_addr (y);
2036 /* Don't call get_addr if x is the same VALUE. */
2037 else if (y != x)
2038 y = get_addr (y);
2040 if (GET_CODE (x) == HIGH)
2041 x = XEXP (x, 0);
2042 else if (GET_CODE (x) == LO_SUM)
2043 x = XEXP (x, 1);
2044 else
2045 x = addr_side_effect_eval (x, abs (xsize), 0);
2046 if (GET_CODE (y) == HIGH)
2047 y = XEXP (y, 0);
2048 else if (GET_CODE (y) == LO_SUM)
2049 y = XEXP (y, 1);
2050 else
2051 y = addr_side_effect_eval (y, abs (ysize), 0);
2053 if (rtx_equal_for_memref_p (x, y))
2055 return offset_overlap_p (c, xsize, ysize);
2058 /* This code used to check for conflicts involving stack references and
2059 globals but the base address alias code now handles these cases. */
2061 if (GET_CODE (x) == PLUS)
2063 /* The fact that X is canonicalized means that this
2064 PLUS rtx is canonicalized. */
2065 rtx x0 = XEXP (x, 0);
2066 rtx x1 = XEXP (x, 1);
2068 if (GET_CODE (y) == PLUS)
2070 /* The fact that Y is canonicalized means that this
2071 PLUS rtx is canonicalized. */
2072 rtx y0 = XEXP (y, 0);
2073 rtx y1 = XEXP (y, 1);
2075 if (rtx_equal_for_memref_p (x1, y1))
2076 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2077 if (rtx_equal_for_memref_p (x0, y0))
2078 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2079 if (CONST_INT_P (x1))
2081 if (CONST_INT_P (y1))
2082 return memrefs_conflict_p (xsize, x0, ysize, y0,
2083 c - INTVAL (x1) + INTVAL (y1));
2084 else
2085 return memrefs_conflict_p (xsize, x0, ysize, y,
2086 c - INTVAL (x1));
2088 else if (CONST_INT_P (y1))
2089 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2091 return -1;
2093 else if (CONST_INT_P (x1))
2094 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2096 else if (GET_CODE (y) == PLUS)
2098 /* The fact that Y is canonicalized means that this
2099 PLUS rtx is canonicalized. */
2100 rtx y0 = XEXP (y, 0);
2101 rtx y1 = XEXP (y, 1);
2103 if (CONST_INT_P (y1))
2104 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2105 else
2106 return -1;
2109 if (GET_CODE (x) == GET_CODE (y))
2110 switch (GET_CODE (x))
2112 case MULT:
2114 /* Handle cases where we expect the second operands to be the
2115 same, and check only whether the first operand would conflict
2116 or not. */
2117 rtx x0, y0;
2118 rtx x1 = canon_rtx (XEXP (x, 1));
2119 rtx y1 = canon_rtx (XEXP (y, 1));
2120 if (! rtx_equal_for_memref_p (x1, y1))
2121 return -1;
2122 x0 = canon_rtx (XEXP (x, 0));
2123 y0 = canon_rtx (XEXP (y, 0));
2124 if (rtx_equal_for_memref_p (x0, y0))
2125 return offset_overlap_p (c, xsize, ysize);
2127 /* Can't properly adjust our sizes. */
2128 if (!CONST_INT_P (x1))
2129 return -1;
2130 xsize /= INTVAL (x1);
2131 ysize /= INTVAL (x1);
2132 c /= INTVAL (x1);
2133 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2136 default:
2137 break;
2140 /* Deal with alignment ANDs by adjusting offset and size so as to
2141 cover the maximum range, without taking any previously known
2142 alignment into account. Make a size negative after such an
2143 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2144 assume a potential overlap, because they may end up in contiguous
2145 memory locations and the stricter-alignment access may span over
2146 part of both. */
2147 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2149 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2150 unsigned HOST_WIDE_INT uc = sc;
2151 if (sc < 0 && -uc == (uc & -uc))
2153 if (xsize > 0)
2154 xsize = -xsize;
2155 if (xsize)
2156 xsize += sc + 1;
2157 c -= sc + 1;
2158 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2159 ysize, y, c);
2162 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2164 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2165 unsigned HOST_WIDE_INT uc = sc;
2166 if (sc < 0 && -uc == (uc & -uc))
2168 if (ysize > 0)
2169 ysize = -ysize;
2170 if (ysize)
2171 ysize += sc + 1;
2172 c += sc + 1;
2173 return memrefs_conflict_p (xsize, x,
2174 ysize, canon_rtx (XEXP (y, 0)), c);
2178 if (CONSTANT_P (x))
2180 if (CONST_INT_P (x) && CONST_INT_P (y))
2182 c += (INTVAL (y) - INTVAL (x));
2183 return offset_overlap_p (c, xsize, ysize);
2186 if (GET_CODE (x) == CONST)
2188 if (GET_CODE (y) == CONST)
2189 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2190 ysize, canon_rtx (XEXP (y, 0)), c);
2191 else
2192 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2193 ysize, y, c);
2195 if (GET_CODE (y) == CONST)
2196 return memrefs_conflict_p (xsize, x, ysize,
2197 canon_rtx (XEXP (y, 0)), c);
2199 /* Assume a potential overlap for symbolic addresses that went
2200 through alignment adjustments (i.e., that have negative
2201 sizes), because we can't know how far they are from each
2202 other. */
2203 if (CONSTANT_P (y))
2204 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2206 return -1;
2209 return -1;
2212 /* Functions to compute memory dependencies.
2214 Since we process the insns in execution order, we can build tables
2215 to keep track of what registers are fixed (and not aliased), what registers
2216 are varying in known ways, and what registers are varying in unknown
2217 ways.
2219 If both memory references are volatile, then there must always be a
2220 dependence between the two references, since their order can not be
2221 changed. A volatile and non-volatile reference can be interchanged
2222 though.
2224 We also must allow AND addresses, because they may generate accesses
2225 outside the object being referenced. This is used to generate aligned
2226 addresses from unaligned addresses, for instance, the alpha
2227 storeqi_unaligned pattern. */
2229 /* Read dependence: X is read after read in MEM takes place. There can
2230 only be a dependence here if both reads are volatile, or if either is
2231 an explicit barrier. */
2234 read_dependence (const_rtx mem, const_rtx x)
2236 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2237 return true;
2238 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2239 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2240 return true;
2241 return false;
2244 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2246 static tree
2247 decl_for_component_ref (tree x)
2251 x = TREE_OPERAND (x, 0);
2253 while (x && TREE_CODE (x) == COMPONENT_REF);
2255 return x && DECL_P (x) ? x : NULL_TREE;
2258 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2259 for the offset of the field reference. *KNOWN_P says whether the
2260 offset is known. */
2262 static void
2263 adjust_offset_for_component_ref (tree x, bool *known_p,
2264 HOST_WIDE_INT *offset)
2266 if (!*known_p)
2267 return;
2270 tree xoffset = component_ref_field_offset (x);
2271 tree field = TREE_OPERAND (x, 1);
2272 if (TREE_CODE (xoffset) != INTEGER_CST)
2274 *known_p = false;
2275 return;
2278 offset_int woffset
2279 = (wi::to_offset (xoffset)
2280 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2281 LOG2_BITS_PER_UNIT));
2282 if (!wi::fits_uhwi_p (woffset))
2284 *known_p = false;
2285 return;
2287 *offset += woffset.to_uhwi ();
2289 x = TREE_OPERAND (x, 0);
2291 while (x && TREE_CODE (x) == COMPONENT_REF);
2294 /* Return nonzero if we can determine the exprs corresponding to memrefs
2295 X and Y and they do not overlap.
2296 If LOOP_VARIANT is set, skip offset-based disambiguation */
2299 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2301 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2302 rtx rtlx, rtly;
2303 rtx basex, basey;
2304 bool moffsetx_known_p, moffsety_known_p;
2305 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2306 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2308 /* Unless both have exprs, we can't tell anything. */
2309 if (exprx == 0 || expry == 0)
2310 return 0;
2312 /* For spill-slot accesses make sure we have valid offsets. */
2313 if ((exprx == get_spill_slot_decl (false)
2314 && ! MEM_OFFSET_KNOWN_P (x))
2315 || (expry == get_spill_slot_decl (false)
2316 && ! MEM_OFFSET_KNOWN_P (y)))
2317 return 0;
2319 /* If the field reference test failed, look at the DECLs involved. */
2320 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2321 if (moffsetx_known_p)
2322 moffsetx = MEM_OFFSET (x);
2323 if (TREE_CODE (exprx) == COMPONENT_REF)
2325 tree t = decl_for_component_ref (exprx);
2326 if (! t)
2327 return 0;
2328 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2329 exprx = t;
2332 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2333 if (moffsety_known_p)
2334 moffsety = MEM_OFFSET (y);
2335 if (TREE_CODE (expry) == COMPONENT_REF)
2337 tree t = decl_for_component_ref (expry);
2338 if (! t)
2339 return 0;
2340 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2341 expry = t;
2344 if (! DECL_P (exprx) || ! DECL_P (expry))
2345 return 0;
2347 /* With invalid code we can end up storing into the constant pool.
2348 Bail out to avoid ICEing when creating RTL for this.
2349 See gfortran.dg/lto/20091028-2_0.f90. */
2350 if (TREE_CODE (exprx) == CONST_DECL
2351 || TREE_CODE (expry) == CONST_DECL)
2352 return 1;
2354 rtlx = DECL_RTL (exprx);
2355 rtly = DECL_RTL (expry);
2357 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2358 can't overlap unless they are the same because we never reuse that part
2359 of the stack frame used for locals for spilled pseudos. */
2360 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2361 && ! rtx_equal_p (rtlx, rtly))
2362 return 1;
2364 /* If we have MEMs referring to different address spaces (which can
2365 potentially overlap), we cannot easily tell from the addresses
2366 whether the references overlap. */
2367 if (MEM_P (rtlx) && MEM_P (rtly)
2368 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2369 return 0;
2371 /* Get the base and offsets of both decls. If either is a register, we
2372 know both are and are the same, so use that as the base. The only
2373 we can avoid overlap is if we can deduce that they are nonoverlapping
2374 pieces of that decl, which is very rare. */
2375 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2376 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2377 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2379 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2380 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2381 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2383 /* If the bases are different, we know they do not overlap if both
2384 are constants or if one is a constant and the other a pointer into the
2385 stack frame. Otherwise a different base means we can't tell if they
2386 overlap or not. */
2387 if (! rtx_equal_p (basex, basey))
2388 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2389 || (CONSTANT_P (basex) && REG_P (basey)
2390 && REGNO_PTR_FRAME_P (REGNO (basey)))
2391 || (CONSTANT_P (basey) && REG_P (basex)
2392 && REGNO_PTR_FRAME_P (REGNO (basex))));
2394 /* Offset based disambiguation not appropriate for loop invariant */
2395 if (loop_invariant)
2396 return 0;
2398 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2399 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2400 : -1);
2401 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2402 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2403 : -1);
2405 /* If we have an offset for either memref, it can update the values computed
2406 above. */
2407 if (moffsetx_known_p)
2408 offsetx += moffsetx, sizex -= moffsetx;
2409 if (moffsety_known_p)
2410 offsety += moffsety, sizey -= moffsety;
2412 /* If a memref has both a size and an offset, we can use the smaller size.
2413 We can't do this if the offset isn't known because we must view this
2414 memref as being anywhere inside the DECL's MEM. */
2415 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2416 sizex = MEM_SIZE (x);
2417 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2418 sizey = MEM_SIZE (y);
2420 /* Put the values of the memref with the lower offset in X's values. */
2421 if (offsetx > offsety)
2423 tem = offsetx, offsetx = offsety, offsety = tem;
2424 tem = sizex, sizex = sizey, sizey = tem;
2427 /* If we don't know the size of the lower-offset value, we can't tell
2428 if they conflict. Otherwise, we do the test. */
2429 return sizex >= 0 && offsety >= offsetx + sizex;
2432 /* Helper for true_dependence and canon_true_dependence.
2433 Checks for true dependence: X is read after store in MEM takes place.
2435 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2436 NULL_RTX, and the canonical addresses of MEM and X are both computed
2437 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2439 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2441 Returns 1 if there is a true dependence, 0 otherwise. */
2443 static int
2444 true_dependence_1 (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2445 const_rtx x, rtx x_addr, bool mem_canonicalized)
2447 rtx true_mem_addr;
2448 rtx base;
2449 int ret;
2451 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2452 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2454 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2455 return 1;
2457 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2458 This is used in epilogue deallocation functions, and in cselib. */
2459 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2460 return 1;
2461 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2462 return 1;
2463 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2464 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2465 return 1;
2467 if (! x_addr)
2468 x_addr = XEXP (x, 0);
2469 x_addr = get_addr (x_addr);
2471 if (! mem_addr)
2473 mem_addr = XEXP (mem, 0);
2474 if (mem_mode == VOIDmode)
2475 mem_mode = GET_MODE (mem);
2477 true_mem_addr = get_addr (mem_addr);
2479 /* Read-only memory is by definition never modified, and therefore can't
2480 conflict with anything. However, don't assume anything when AND
2481 addresses are involved and leave to the code below to determine
2482 dependence. We don't expect to find read-only set on MEM, but
2483 stupid user tricks can produce them, so don't die. */
2484 if (MEM_READONLY_P (x)
2485 && GET_CODE (x_addr) != AND
2486 && GET_CODE (true_mem_addr) != AND)
2487 return 0;
2489 /* If we have MEMs referring to different address spaces (which can
2490 potentially overlap), we cannot easily tell from the addresses
2491 whether the references overlap. */
2492 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2493 return 1;
2495 base = find_base_term (x_addr);
2496 if (base && (GET_CODE (base) == LABEL_REF
2497 || (GET_CODE (base) == SYMBOL_REF
2498 && CONSTANT_POOL_ADDRESS_P (base))))
2499 return 0;
2501 rtx mem_base = find_base_term (true_mem_addr);
2502 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2503 GET_MODE (x), mem_mode))
2504 return 0;
2506 x_addr = canon_rtx (x_addr);
2507 if (!mem_canonicalized)
2508 mem_addr = canon_rtx (true_mem_addr);
2510 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2511 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2512 return ret;
2514 if (mems_in_disjoint_alias_sets_p (x, mem))
2515 return 0;
2517 if (nonoverlapping_memrefs_p (mem, x, false))
2518 return 0;
2520 return rtx_refs_may_alias_p (x, mem, true);
2523 /* True dependence: X is read after store in MEM takes place. */
2526 true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x)
2528 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2529 x, NULL_RTX, /*mem_canonicalized=*/false);
2532 /* Canonical true dependence: X is read after store in MEM takes place.
2533 Variant of true_dependence which assumes MEM has already been
2534 canonicalized (hence we no longer do that here).
2535 The mem_addr argument has been added, since true_dependence_1 computed
2536 this value prior to canonicalizing. */
2539 canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2540 const_rtx x, rtx x_addr)
2542 return true_dependence_1 (mem, mem_mode, mem_addr,
2543 x, x_addr, /*mem_canonicalized=*/true);
2546 /* Returns nonzero if a write to X might alias a previous read from
2547 (or, if WRITEP is true, a write to) MEM.
2548 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2549 and X_MODE the mode for that access.
2550 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2552 static int
2553 write_dependence_p (const_rtx mem,
2554 const_rtx x, enum machine_mode x_mode, rtx x_addr,
2555 bool mem_canonicalized, bool x_canonicalized, bool writep)
2557 rtx mem_addr;
2558 rtx true_mem_addr, true_x_addr;
2559 rtx base;
2560 int ret;
2562 gcc_checking_assert (x_canonicalized
2563 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2564 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2566 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2567 return 1;
2569 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2570 This is used in epilogue deallocation functions. */
2571 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2572 return 1;
2573 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2574 return 1;
2575 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2576 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2577 return 1;
2579 if (!x_addr)
2580 x_addr = XEXP (x, 0);
2581 true_x_addr = get_addr (x_addr);
2583 mem_addr = XEXP (mem, 0);
2584 true_mem_addr = get_addr (mem_addr);
2586 /* A read from read-only memory can't conflict with read-write memory.
2587 Don't assume anything when AND addresses are involved and leave to
2588 the code below to determine dependence. */
2589 if (!writep
2590 && MEM_READONLY_P (mem)
2591 && GET_CODE (true_x_addr) != AND
2592 && GET_CODE (true_mem_addr) != AND)
2593 return 0;
2595 /* If we have MEMs referring to different address spaces (which can
2596 potentially overlap), we cannot easily tell from the addresses
2597 whether the references overlap. */
2598 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2599 return 1;
2601 base = find_base_term (true_mem_addr);
2602 if (! writep
2603 && base
2604 && (GET_CODE (base) == LABEL_REF
2605 || (GET_CODE (base) == SYMBOL_REF
2606 && CONSTANT_POOL_ADDRESS_P (base))))
2607 return 0;
2609 rtx x_base = find_base_term (true_x_addr);
2610 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2611 GET_MODE (x), GET_MODE (mem)))
2612 return 0;
2614 if (!x_canonicalized)
2616 x_addr = canon_rtx (true_x_addr);
2617 x_mode = GET_MODE (x);
2619 if (!mem_canonicalized)
2620 mem_addr = canon_rtx (true_mem_addr);
2622 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2623 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2624 return ret;
2626 if (nonoverlapping_memrefs_p (x, mem, false))
2627 return 0;
2629 return rtx_refs_may_alias_p (x, mem, false);
2632 /* Anti dependence: X is written after read in MEM takes place. */
2635 anti_dependence (const_rtx mem, const_rtx x)
2637 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2638 /*mem_canonicalized=*/false,
2639 /*x_canonicalized*/false, /*writep=*/false);
2642 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2643 Also, consider X in X_MODE (which might be from an enclosing
2644 STRICT_LOW_PART / ZERO_EXTRACT).
2645 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2648 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2649 const_rtx x, enum machine_mode x_mode, rtx x_addr)
2651 return write_dependence_p (mem, x, x_mode, x_addr,
2652 mem_canonicalized, /*x_canonicalized=*/true,
2653 /*writep=*/false);
2656 /* Output dependence: X is written after store in MEM takes place. */
2659 output_dependence (const_rtx mem, const_rtx x)
2661 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2662 /*mem_canonicalized=*/false,
2663 /*x_canonicalized*/false, /*writep=*/true);
2668 /* Check whether X may be aliased with MEM. Don't do offset-based
2669 memory disambiguation & TBAA. */
2671 may_alias_p (const_rtx mem, const_rtx x)
2673 rtx x_addr, mem_addr;
2675 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2676 return 1;
2678 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2679 This is used in epilogue deallocation functions. */
2680 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2681 return 1;
2682 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2683 return 1;
2684 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2685 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2686 return 1;
2688 x_addr = XEXP (x, 0);
2689 x_addr = get_addr (x_addr);
2691 mem_addr = XEXP (mem, 0);
2692 mem_addr = get_addr (mem_addr);
2694 /* Read-only memory is by definition never modified, and therefore can't
2695 conflict with anything. However, don't assume anything when AND
2696 addresses are involved and leave to the code below to determine
2697 dependence. We don't expect to find read-only set on MEM, but
2698 stupid user tricks can produce them, so don't die. */
2699 if (MEM_READONLY_P (x)
2700 && GET_CODE (x_addr) != AND
2701 && GET_CODE (mem_addr) != AND)
2702 return 0;
2704 /* If we have MEMs referring to different address spaces (which can
2705 potentially overlap), we cannot easily tell from the addresses
2706 whether the references overlap. */
2707 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2708 return 1;
2710 rtx x_base = find_base_term (x_addr);
2711 rtx mem_base = find_base_term (mem_addr);
2712 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2713 GET_MODE (x), GET_MODE (mem_addr)))
2714 return 0;
2716 if (nonoverlapping_memrefs_p (mem, x, true))
2717 return 0;
2719 /* TBAA not valid for loop_invarint */
2720 return rtx_refs_may_alias_p (x, mem, false);
2723 void
2724 init_alias_target (void)
2726 int i;
2728 if (!arg_base_value)
2729 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2731 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2733 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2734 /* Check whether this register can hold an incoming pointer
2735 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2736 numbers, so translate if necessary due to register windows. */
2737 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2738 && HARD_REGNO_MODE_OK (i, Pmode))
2739 static_reg_base_value[i] = arg_base_value;
2741 static_reg_base_value[STACK_POINTER_REGNUM]
2742 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2743 static_reg_base_value[ARG_POINTER_REGNUM]
2744 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2745 static_reg_base_value[FRAME_POINTER_REGNUM]
2746 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2747 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2748 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2749 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2750 #endif
2753 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2754 to be memory reference. */
2755 static bool memory_modified;
2756 static void
2757 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2759 if (MEM_P (x))
2761 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2762 memory_modified = true;
2767 /* Return true when INSN possibly modify memory contents of MEM
2768 (i.e. address can be modified). */
2769 bool
2770 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2772 if (!INSN_P (insn))
2773 return false;
2774 memory_modified = false;
2775 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2776 return memory_modified;
2779 /* Return TRUE if the destination of a set is rtx identical to
2780 ITEM. */
2781 static inline bool
2782 set_dest_equal_p (const_rtx set, const_rtx item)
2784 rtx dest = SET_DEST (set);
2785 return rtx_equal_p (dest, item);
2788 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2789 *DEFINITELY* modify the memory contents of MEM. */
2790 bool
2791 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2793 if (!INSN_P (insn))
2794 return false;
2795 insn = PATTERN (insn);
2796 if (GET_CODE (insn) == SET)
2797 return set_dest_equal_p (insn, mem);
2798 else if (GET_CODE (insn) == PARALLEL)
2800 int i;
2801 for (i = 0; i < XVECLEN (insn, 0); i++)
2803 rtx sub = XVECEXP (insn, 0, i);
2804 if (GET_CODE (sub) == SET
2805 && set_dest_equal_p (sub, mem))
2806 return true;
2809 return false;
2812 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2813 array. */
2815 void
2816 init_alias_analysis (void)
2818 unsigned int maxreg = max_reg_num ();
2819 int changed, pass;
2820 int i;
2821 unsigned int ui;
2822 rtx_insn *insn;
2823 rtx val;
2824 int rpo_cnt;
2825 int *rpo;
2827 timevar_push (TV_ALIAS_ANALYSIS);
2829 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
2830 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
2831 bitmap_clear (reg_known_equiv_p);
2833 /* If we have memory allocated from the previous run, use it. */
2834 if (old_reg_base_value)
2835 reg_base_value = old_reg_base_value;
2837 if (reg_base_value)
2838 reg_base_value->truncate (0);
2840 vec_safe_grow_cleared (reg_base_value, maxreg);
2842 new_reg_base_value = XNEWVEC (rtx, maxreg);
2843 reg_seen = sbitmap_alloc (maxreg);
2845 /* The basic idea is that each pass through this loop will use the
2846 "constant" information from the previous pass to propagate alias
2847 information through another level of assignments.
2849 The propagation is done on the CFG in reverse post-order, to propagate
2850 things forward as far as possible in each iteration.
2852 This could get expensive if the assignment chains are long. Maybe
2853 we should throttle the number of iterations, possibly based on
2854 the optimization level or flag_expensive_optimizations.
2856 We could propagate more information in the first pass by making use
2857 of DF_REG_DEF_COUNT to determine immediately that the alias information
2858 for a pseudo is "constant".
2860 A program with an uninitialized variable can cause an infinite loop
2861 here. Instead of doing a full dataflow analysis to detect such problems
2862 we just cap the number of iterations for the loop.
2864 The state of the arrays for the set chain in question does not matter
2865 since the program has undefined behavior. */
2867 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2868 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
2870 pass = 0;
2873 /* Assume nothing will change this iteration of the loop. */
2874 changed = 0;
2876 /* We want to assign the same IDs each iteration of this loop, so
2877 start counting from one each iteration of the loop. */
2878 unique_id = 1;
2880 /* We're at the start of the function each iteration through the
2881 loop, so we're copying arguments. */
2882 copying_arguments = true;
2884 /* Wipe the potential alias information clean for this pass. */
2885 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2887 /* Wipe the reg_seen array clean. */
2888 bitmap_clear (reg_seen);
2890 /* Initialize the alias information for this pass. */
2891 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2892 if (static_reg_base_value[i])
2894 new_reg_base_value[i] = static_reg_base_value[i];
2895 bitmap_set_bit (reg_seen, i);
2898 /* Walk the insns adding values to the new_reg_base_value array. */
2899 for (i = 0; i < rpo_cnt; i++)
2901 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2902 FOR_BB_INSNS (bb, insn)
2904 if (NONDEBUG_INSN_P (insn))
2906 rtx note, set;
2908 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2909 /* The prologue/epilogue insns are not threaded onto the
2910 insn chain until after reload has completed. Thus,
2911 there is no sense wasting time checking if INSN is in
2912 the prologue/epilogue until after reload has completed. */
2913 if (reload_completed
2914 && prologue_epilogue_contains (insn))
2915 continue;
2916 #endif
2918 /* If this insn has a noalias note, process it, Otherwise,
2919 scan for sets. A simple set will have no side effects
2920 which could change the base value of any other register. */
2922 if (GET_CODE (PATTERN (insn)) == SET
2923 && REG_NOTES (insn) != 0
2924 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2925 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2926 else
2927 note_stores (PATTERN (insn), record_set, NULL);
2929 set = single_set (insn);
2931 if (set != 0
2932 && REG_P (SET_DEST (set))
2933 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2935 unsigned int regno = REGNO (SET_DEST (set));
2936 rtx src = SET_SRC (set);
2937 rtx t;
2939 note = find_reg_equal_equiv_note (insn);
2940 if (note && REG_NOTE_KIND (note) == REG_EQUAL
2941 && DF_REG_DEF_COUNT (regno) != 1)
2942 note = NULL_RTX;
2944 if (note != NULL_RTX
2945 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2946 && ! rtx_varies_p (XEXP (note, 0), 1)
2947 && ! reg_overlap_mentioned_p (SET_DEST (set),
2948 XEXP (note, 0)))
2950 set_reg_known_value (regno, XEXP (note, 0));
2951 set_reg_known_equiv_p (regno,
2952 REG_NOTE_KIND (note) == REG_EQUIV);
2954 else if (DF_REG_DEF_COUNT (regno) == 1
2955 && GET_CODE (src) == PLUS
2956 && REG_P (XEXP (src, 0))
2957 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2958 && CONST_INT_P (XEXP (src, 1)))
2960 t = plus_constant (GET_MODE (src), t,
2961 INTVAL (XEXP (src, 1)));
2962 set_reg_known_value (regno, t);
2963 set_reg_known_equiv_p (regno, false);
2965 else if (DF_REG_DEF_COUNT (regno) == 1
2966 && ! rtx_varies_p (src, 1))
2968 set_reg_known_value (regno, src);
2969 set_reg_known_equiv_p (regno, false);
2973 else if (NOTE_P (insn)
2974 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
2975 copying_arguments = false;
2979 /* Now propagate values from new_reg_base_value to reg_base_value. */
2980 gcc_assert (maxreg == (unsigned int) max_reg_num ());
2982 for (ui = 0; ui < maxreg; ui++)
2984 if (new_reg_base_value[ui]
2985 && new_reg_base_value[ui] != (*reg_base_value)[ui]
2986 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
2988 (*reg_base_value)[ui] = new_reg_base_value[ui];
2989 changed = 1;
2993 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
2994 XDELETEVEC (rpo);
2996 /* Fill in the remaining entries. */
2997 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
2999 int regno = i + FIRST_PSEUDO_REGISTER;
3000 if (! val)
3001 set_reg_known_value (regno, regno_reg_rtx[regno]);
3004 /* Clean up. */
3005 free (new_reg_base_value);
3006 new_reg_base_value = 0;
3007 sbitmap_free (reg_seen);
3008 reg_seen = 0;
3009 timevar_pop (TV_ALIAS_ANALYSIS);
3012 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3013 Special API for var-tracking pass purposes. */
3015 void
3016 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3018 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3021 void
3022 end_alias_analysis (void)
3024 old_reg_base_value = reg_base_value;
3025 vec_free (reg_known_value);
3026 sbitmap_free (reg_known_equiv_p);
3029 #include "gt-alias.h"