* config/rl78/rl78-expand.md (umulqihi3): Disable for G10.
[official-gcc.git] / gcc / alias.c
blob39df09b4e88853aa9da88262e58cbabdd3e9b9dc
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 "function.h"
31 #include "alias.h"
32 #include "emit-rtl.h"
33 #include "regs.h"
34 #include "hard-reg-set.h"
35 #include "flags.h"
36 #include "diagnostic-core.h"
37 #include "cselib.h"
38 #include "splay-tree.h"
39 #include "langhooks.h"
40 #include "timevar.h"
41 #include "dumpfile.h"
42 #include "target.h"
43 #include "df.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "gimple-expr.h"
47 #include "is-a.h"
48 #include "gimple.h"
49 #include "gimple-ssa.h"
51 /* The aliasing API provided here solves related but different problems:
53 Say there exists (in c)
55 struct X {
56 struct Y y1;
57 struct Z z2;
58 } x1, *px1, *px2;
60 struct Y y2, *py;
61 struct Z z2, *pz;
64 py = &x1.y1;
65 px2 = &x1;
67 Consider the four questions:
69 Can a store to x1 interfere with px2->y1?
70 Can a store to x1 interfere with px2->z2?
71 Can a store to x1 change the value pointed to by with py?
72 Can a store to x1 change the value pointed to by with pz?
74 The answer to these questions can be yes, yes, yes, and maybe.
76 The first two questions can be answered with a simple examination
77 of the type system. If structure X contains a field of type Y then
78 a store through a pointer to an X can overwrite any field that is
79 contained (recursively) in an X (unless we know that px1 != px2).
81 The last two questions can be solved in the same way as the first
82 two questions but this is too conservative. The observation is
83 that in some cases we can know which (if any) fields are addressed
84 and if those addresses are used in bad ways. This analysis may be
85 language specific. In C, arbitrary operations may be applied to
86 pointers. However, there is some indication that this may be too
87 conservative for some C++ types.
89 The pass ipa-type-escape does this analysis for the types whose
90 instances do not escape across the compilation boundary.
92 Historically in GCC, these two problems were combined and a single
93 data structure that was used to represent the solution to these
94 problems. We now have two similar but different data structures,
95 The data structure to solve the last two questions is similar to
96 the first, but does not contain the fields whose address are never
97 taken. For types that do escape the compilation unit, the data
98 structures will have identical information.
101 /* The alias sets assigned to MEMs assist the back-end in determining
102 which MEMs can alias which other MEMs. In general, two MEMs in
103 different alias sets cannot alias each other, with one important
104 exception. Consider something like:
106 struct S { int i; double d; };
108 a store to an `S' can alias something of either type `int' or type
109 `double'. (However, a store to an `int' cannot alias a `double'
110 and vice versa.) We indicate this via a tree structure that looks
111 like:
112 struct S
115 |/_ _\|
116 int double
118 (The arrows are directed and point downwards.)
119 In this situation we say the alias set for `struct S' is the
120 `superset' and that those for `int' and `double' are `subsets'.
122 To see whether two alias sets can point to the same memory, we must
123 see if either alias set is a subset of the other. We need not trace
124 past immediate descendants, however, since we propagate all
125 grandchildren up one level.
127 Alias set zero is implicitly a superset of all other alias sets.
128 However, this is no actual entry for alias set zero. It is an
129 error to attempt to explicitly construct a subset of zero. */
131 struct GTY(()) alias_set_entry_d {
132 /* The alias set number, as stored in MEM_ALIAS_SET. */
133 alias_set_type alias_set;
135 /* Nonzero if would have a child of zero: this effectively makes this
136 alias set the same as alias set zero. */
137 int has_zero_child;
139 /* The children of the alias set. These are not just the immediate
140 children, but, in fact, all descendants. So, if we have:
142 struct T { struct S s; float f; }
144 continuing our example above, the children here will be all of
145 `int', `double', `float', and `struct S'. */
146 splay_tree GTY((param1_is (int), param2_is (int))) children;
148 typedef struct alias_set_entry_d *alias_set_entry;
150 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
151 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
152 static void record_set (rtx, const_rtx, void *);
153 static int base_alias_check (rtx, rtx, rtx, rtx, enum machine_mode,
154 enum machine_mode);
155 static rtx find_base_value (rtx);
156 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
157 static int insert_subset_children (splay_tree_node, void*);
158 static alias_set_entry get_alias_set_entry (alias_set_type);
159 static tree decl_for_component_ref (tree);
160 static int write_dependence_p (const_rtx,
161 const_rtx, enum machine_mode, rtx,
162 bool, bool, bool);
164 static void memory_modified_1 (rtx, const_rtx, void *);
166 /* Set up all info needed to perform alias analysis on memory references. */
168 /* Returns the size in bytes of the mode of X. */
169 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
171 /* Cap the number of passes we make over the insns propagating alias
172 information through set chains.
173 ??? 10 is a completely arbitrary choice. This should be based on the
174 maximum loop depth in the CFG, but we do not have this information
175 available (even if current_loops _is_ available). */
176 #define MAX_ALIAS_LOOP_PASSES 10
178 /* reg_base_value[N] gives an address to which register N is related.
179 If all sets after the first add or subtract to the current value
180 or otherwise modify it so it does not point to a different top level
181 object, reg_base_value[N] is equal to the address part of the source
182 of the first set.
184 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
185 expressions represent three types of base:
187 1. incoming arguments. There is just one ADDRESS to represent all
188 arguments, since we do not know at this level whether accesses
189 based on different arguments can alias. The ADDRESS has id 0.
191 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
192 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
193 Each of these rtxes has a separate ADDRESS associated with it,
194 each with a negative id.
196 GCC is (and is required to be) precise in which register it
197 chooses to access a particular region of stack. We can therefore
198 assume that accesses based on one of these rtxes do not alias
199 accesses based on another of these rtxes.
201 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
202 Each such piece of memory has a separate ADDRESS associated
203 with it, each with an id greater than 0.
205 Accesses based on one ADDRESS do not alias accesses based on other
206 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
207 alias globals either; the ADDRESSes have Pmode to indicate this.
208 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
209 indicate this. */
211 static GTY(()) vec<rtx, va_gc> *reg_base_value;
212 static rtx *new_reg_base_value;
214 /* The single VOIDmode ADDRESS that represents all argument bases.
215 It has id 0. */
216 static GTY(()) rtx arg_base_value;
218 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
219 static int unique_id;
221 /* We preserve the copy of old array around to avoid amount of garbage
222 produced. About 8% of garbage produced were attributed to this
223 array. */
224 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
226 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
227 registers. */
228 #define UNIQUE_BASE_VALUE_SP -1
229 #define UNIQUE_BASE_VALUE_ARGP -2
230 #define UNIQUE_BASE_VALUE_FP -3
231 #define UNIQUE_BASE_VALUE_HFP -4
233 #define static_reg_base_value \
234 (this_target_rtl->x_static_reg_base_value)
236 #define REG_BASE_VALUE(X) \
237 (REGNO (X) < vec_safe_length (reg_base_value) \
238 ? (*reg_base_value)[REGNO (X)] : 0)
240 /* Vector indexed by N giving the initial (unchanging) value known for
241 pseudo-register N. This vector is initialized in init_alias_analysis,
242 and does not change until end_alias_analysis is called. */
243 static GTY(()) vec<rtx, va_gc> *reg_known_value;
245 /* Vector recording for each reg_known_value whether it is due to a
246 REG_EQUIV note. Future passes (viz., reload) may replace the
247 pseudo with the equivalent expression and so we account for the
248 dependences that would be introduced if that happens.
250 The REG_EQUIV notes created in assign_parms may mention the arg
251 pointer, and there are explicit insns in the RTL that modify the
252 arg pointer. Thus we must ensure that such insns don't get
253 scheduled across each other because that would invalidate the
254 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
255 wrong, but solving the problem in the scheduler will likely give
256 better code, so we do it here. */
257 static sbitmap reg_known_equiv_p;
259 /* True when scanning insns from the start of the rtl to the
260 NOTE_INSN_FUNCTION_BEG note. */
261 static bool copying_arguments;
264 /* The splay-tree used to store the various alias set entries. */
265 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
267 /* Build a decomposed reference object for querying the alias-oracle
268 from the MEM rtx and store it in *REF.
269 Returns false if MEM is not suitable for the alias-oracle. */
271 static bool
272 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
274 tree expr = MEM_EXPR (mem);
275 tree base;
277 if (!expr)
278 return false;
280 ao_ref_init (ref, expr);
282 /* Get the base of the reference and see if we have to reject or
283 adjust it. */
284 base = ao_ref_base (ref);
285 if (base == NULL_TREE)
286 return false;
288 /* The tree oracle doesn't like bases that are neither decls
289 nor indirect references of SSA names. */
290 if (!(DECL_P (base)
291 || (TREE_CODE (base) == MEM_REF
292 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
293 || (TREE_CODE (base) == TARGET_MEM_REF
294 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
295 return false;
297 /* If this is a reference based on a partitioned decl replace the
298 base with a MEM_REF of the pointer representative we
299 created during stack slot partitioning. */
300 if (TREE_CODE (base) == VAR_DECL
301 && ! is_global_var (base)
302 && cfun->gimple_df->decls_to_pointers != NULL)
304 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
305 if (namep)
306 ref->base = build_simple_mem_ref (*namep);
309 ref->ref_alias_set = MEM_ALIAS_SET (mem);
311 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
312 is conservative, so trust it. */
313 if (!MEM_OFFSET_KNOWN_P (mem)
314 || !MEM_SIZE_KNOWN_P (mem))
315 return true;
317 /* If the base decl is a parameter we can have negative MEM_OFFSET in
318 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
319 here. */
320 if (MEM_OFFSET (mem) < 0
321 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
322 return true;
324 /* Otherwise continue and refine size and offset we got from analyzing
325 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
327 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
328 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
330 /* The MEM may extend into adjacent fields, so adjust max_size if
331 necessary. */
332 if (ref->max_size != -1
333 && ref->size > ref->max_size)
334 ref->max_size = ref->size;
336 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
337 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
338 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
339 && (ref->offset < 0
340 || (DECL_P (ref->base)
341 && (DECL_SIZE (ref->base) == NULL_TREE
342 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
343 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
344 ref->offset + ref->size)))))
345 return false;
347 return true;
350 /* Query the alias-oracle on whether the two memory rtx X and MEM may
351 alias. If TBAA_P is set also apply TBAA. Returns true if the
352 two rtxen may alias, false otherwise. */
354 static bool
355 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
357 ao_ref ref1, ref2;
359 if (!ao_ref_from_mem (&ref1, x)
360 || !ao_ref_from_mem (&ref2, mem))
361 return true;
363 return refs_may_alias_p_1 (&ref1, &ref2,
364 tbaa_p
365 && MEM_ALIAS_SET (x) != 0
366 && MEM_ALIAS_SET (mem) != 0);
369 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
370 such an entry, or NULL otherwise. */
372 static inline alias_set_entry
373 get_alias_set_entry (alias_set_type alias_set)
375 return (*alias_sets)[alias_set];
378 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
379 the two MEMs cannot alias each other. */
381 static inline int
382 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
384 /* Perform a basic sanity check. Namely, that there are no alias sets
385 if we're not using strict aliasing. This helps to catch bugs
386 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
387 where a MEM is allocated in some way other than by the use of
388 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
389 use alias sets to indicate that spilled registers cannot alias each
390 other, we might need to remove this check. */
391 gcc_assert (flag_strict_aliasing
392 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
394 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
397 /* Insert the NODE into the splay tree given by DATA. Used by
398 record_alias_subset via splay_tree_foreach. */
400 static int
401 insert_subset_children (splay_tree_node node, void *data)
403 splay_tree_insert ((splay_tree) data, node->key, node->value);
405 return 0;
408 /* Return true if the first alias set is a subset of the second. */
410 bool
411 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
413 alias_set_entry ase;
415 /* Everything is a subset of the "aliases everything" set. */
416 if (set2 == 0)
417 return true;
419 /* Otherwise, check if set1 is a subset of set2. */
420 ase = get_alias_set_entry (set2);
421 if (ase != 0
422 && (ase->has_zero_child
423 || splay_tree_lookup (ase->children,
424 (splay_tree_key) set1)))
425 return true;
426 return false;
429 /* Return 1 if the two specified alias sets may conflict. */
432 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
434 alias_set_entry ase;
436 /* The easy case. */
437 if (alias_sets_must_conflict_p (set1, set2))
438 return 1;
440 /* See if the first alias set is a subset of the second. */
441 ase = get_alias_set_entry (set1);
442 if (ase != 0
443 && (ase->has_zero_child
444 || splay_tree_lookup (ase->children,
445 (splay_tree_key) set2)))
446 return 1;
448 /* Now do the same, but with the alias sets reversed. */
449 ase = get_alias_set_entry (set2);
450 if (ase != 0
451 && (ase->has_zero_child
452 || splay_tree_lookup (ase->children,
453 (splay_tree_key) set1)))
454 return 1;
456 /* The two alias sets are distinct and neither one is the
457 child of the other. Therefore, they cannot conflict. */
458 return 0;
461 /* Return 1 if the two specified alias sets will always conflict. */
464 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
466 if (set1 == 0 || set2 == 0 || set1 == set2)
467 return 1;
469 return 0;
472 /* Return 1 if any MEM object of type T1 will always conflict (using the
473 dependency routines in this file) with any MEM object of type T2.
474 This is used when allocating temporary storage. If T1 and/or T2 are
475 NULL_TREE, it means we know nothing about the storage. */
478 objects_must_conflict_p (tree t1, tree t2)
480 alias_set_type set1, set2;
482 /* If neither has a type specified, we don't know if they'll conflict
483 because we may be using them to store objects of various types, for
484 example the argument and local variables areas of inlined functions. */
485 if (t1 == 0 && t2 == 0)
486 return 0;
488 /* If they are the same type, they must conflict. */
489 if (t1 == t2
490 /* Likewise if both are volatile. */
491 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
492 return 1;
494 set1 = t1 ? get_alias_set (t1) : 0;
495 set2 = t2 ? get_alias_set (t2) : 0;
497 /* We can't use alias_sets_conflict_p because we must make sure
498 that every subtype of t1 will conflict with every subtype of
499 t2 for which a pair of subobjects of these respective subtypes
500 overlaps on the stack. */
501 return alias_sets_must_conflict_p (set1, set2);
504 /* Return the outermost parent of component present in the chain of
505 component references handled by get_inner_reference in T with the
506 following property:
507 - the component is non-addressable, or
508 - the parent has alias set zero,
509 or NULL_TREE if no such parent exists. In the former cases, the alias
510 set of this parent is the alias set that must be used for T itself. */
512 tree
513 component_uses_parent_alias_set_from (const_tree t)
515 const_tree found = NULL_TREE;
517 while (handled_component_p (t))
519 switch (TREE_CODE (t))
521 case COMPONENT_REF:
522 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
523 found = t;
524 break;
526 case ARRAY_REF:
527 case ARRAY_RANGE_REF:
528 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
529 found = t;
530 break;
532 case REALPART_EXPR:
533 case IMAGPART_EXPR:
534 break;
536 case BIT_FIELD_REF:
537 case VIEW_CONVERT_EXPR:
538 /* Bitfields and casts are never addressable. */
539 found = t;
540 break;
542 default:
543 gcc_unreachable ();
546 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
547 found = t;
549 t = TREE_OPERAND (t, 0);
552 if (found)
553 return TREE_OPERAND (found, 0);
555 return NULL_TREE;
559 /* Return whether the pointer-type T effective for aliasing may
560 access everything and thus the reference has to be assigned
561 alias-set zero. */
563 static bool
564 ref_all_alias_ptr_type_p (const_tree t)
566 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
567 || TYPE_REF_CAN_ALIAS_ALL (t));
570 /* Return the alias set for the memory pointed to by T, which may be
571 either a type or an expression. Return -1 if there is nothing
572 special about dereferencing T. */
574 static alias_set_type
575 get_deref_alias_set_1 (tree t)
577 /* All we care about is the type. */
578 if (! TYPE_P (t))
579 t = TREE_TYPE (t);
581 /* If we have an INDIRECT_REF via a void pointer, we don't
582 know anything about what that might alias. Likewise if the
583 pointer is marked that way. */
584 if (ref_all_alias_ptr_type_p (t))
585 return 0;
587 return -1;
590 /* Return the alias set for the memory pointed to by T, which may be
591 either a type or an expression. */
593 alias_set_type
594 get_deref_alias_set (tree t)
596 /* If we're not doing any alias analysis, just assume everything
597 aliases everything else. */
598 if (!flag_strict_aliasing)
599 return 0;
601 alias_set_type set = get_deref_alias_set_1 (t);
603 /* Fall back to the alias-set of the pointed-to type. */
604 if (set == -1)
606 if (! TYPE_P (t))
607 t = TREE_TYPE (t);
608 set = get_alias_set (TREE_TYPE (t));
611 return set;
614 /* Return the pointer-type relevant for TBAA purposes from the
615 memory reference tree *T or NULL_TREE in which case *T is
616 adjusted to point to the outermost component reference that
617 can be used for assigning an alias set. */
619 static tree
620 reference_alias_ptr_type_1 (tree *t)
622 tree inner;
624 /* Get the base object of the reference. */
625 inner = *t;
626 while (handled_component_p (inner))
628 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
629 the type of any component references that wrap it to
630 determine the alias-set. */
631 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
632 *t = TREE_OPERAND (inner, 0);
633 inner = TREE_OPERAND (inner, 0);
636 /* Handle pointer dereferences here, they can override the
637 alias-set. */
638 if (INDIRECT_REF_P (inner)
639 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
640 return TREE_TYPE (TREE_OPERAND (inner, 0));
641 else if (TREE_CODE (inner) == TARGET_MEM_REF)
642 return TREE_TYPE (TMR_OFFSET (inner));
643 else if (TREE_CODE (inner) == MEM_REF
644 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
645 return TREE_TYPE (TREE_OPERAND (inner, 1));
647 /* If the innermost reference is a MEM_REF that has a
648 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
649 using the memory access type for determining the alias-set. */
650 if (TREE_CODE (inner) == MEM_REF
651 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
652 != TYPE_MAIN_VARIANT
653 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
654 return TREE_TYPE (TREE_OPERAND (inner, 1));
656 /* Otherwise, pick up the outermost object that we could have
657 a pointer to. */
658 tree tem = component_uses_parent_alias_set_from (*t);
659 if (tem)
660 *t = tem;
662 return NULL_TREE;
665 /* Return the pointer-type relevant for TBAA purposes from the
666 gimple memory reference tree T. This is the type to be used for
667 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
668 and guarantees that get_alias_set will return the same alias
669 set for T and the replacement. */
671 tree
672 reference_alias_ptr_type (tree t)
674 tree ptype = reference_alias_ptr_type_1 (&t);
675 /* If there is a given pointer type for aliasing purposes, return it. */
676 if (ptype != NULL_TREE)
677 return ptype;
679 /* Otherwise build one from the outermost component reference we
680 may use. */
681 if (TREE_CODE (t) == MEM_REF
682 || TREE_CODE (t) == TARGET_MEM_REF)
683 return TREE_TYPE (TREE_OPERAND (t, 1));
684 else
685 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
688 /* Return whether the pointer-types T1 and T2 used to determine
689 two alias sets of two references will yield the same answer
690 from get_deref_alias_set. */
692 bool
693 alias_ptr_types_compatible_p (tree t1, tree t2)
695 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
696 return true;
698 if (ref_all_alias_ptr_type_p (t1)
699 || ref_all_alias_ptr_type_p (t2))
700 return false;
702 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
703 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
706 /* Return the alias set for T, which may be either a type or an
707 expression. Call language-specific routine for help, if needed. */
709 alias_set_type
710 get_alias_set (tree t)
712 alias_set_type set;
714 /* If we're not doing any alias analysis, just assume everything
715 aliases everything else. Also return 0 if this or its type is
716 an error. */
717 if (! flag_strict_aliasing || t == error_mark_node
718 || (! TYPE_P (t)
719 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
720 return 0;
722 /* We can be passed either an expression or a type. This and the
723 language-specific routine may make mutually-recursive calls to each other
724 to figure out what to do. At each juncture, we see if this is a tree
725 that the language may need to handle specially. First handle things that
726 aren't types. */
727 if (! TYPE_P (t))
729 /* Give the language a chance to do something with this tree
730 before we look at it. */
731 STRIP_NOPS (t);
732 set = lang_hooks.get_alias_set (t);
733 if (set != -1)
734 return set;
736 /* Get the alias pointer-type to use or the outermost object
737 that we could have a pointer to. */
738 tree ptype = reference_alias_ptr_type_1 (&t);
739 if (ptype != NULL)
740 return get_deref_alias_set (ptype);
742 /* If we've already determined the alias set for a decl, just return
743 it. This is necessary for C++ anonymous unions, whose component
744 variables don't look like union members (boo!). */
745 if (TREE_CODE (t) == VAR_DECL
746 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
747 return MEM_ALIAS_SET (DECL_RTL (t));
749 /* Now all we care about is the type. */
750 t = TREE_TYPE (t);
753 /* Variant qualifiers don't affect the alias set, so get the main
754 variant. */
755 t = TYPE_MAIN_VARIANT (t);
757 /* Always use the canonical type as well. If this is a type that
758 requires structural comparisons to identify compatible types
759 use alias set zero. */
760 if (TYPE_STRUCTURAL_EQUALITY_P (t))
762 /* Allow the language to specify another alias set for this
763 type. */
764 set = lang_hooks.get_alias_set (t);
765 if (set != -1)
766 return set;
767 return 0;
770 t = TYPE_CANONICAL (t);
772 /* The canonical type should not require structural equality checks. */
773 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
775 /* If this is a type with a known alias set, return it. */
776 if (TYPE_ALIAS_SET_KNOWN_P (t))
777 return TYPE_ALIAS_SET (t);
779 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
780 if (!COMPLETE_TYPE_P (t))
782 /* For arrays with unknown size the conservative answer is the
783 alias set of the element type. */
784 if (TREE_CODE (t) == ARRAY_TYPE)
785 return get_alias_set (TREE_TYPE (t));
787 /* But return zero as a conservative answer for incomplete types. */
788 return 0;
791 /* See if the language has special handling for this type. */
792 set = lang_hooks.get_alias_set (t);
793 if (set != -1)
794 return set;
796 /* There are no objects of FUNCTION_TYPE, so there's no point in
797 using up an alias set for them. (There are, of course, pointers
798 and references to functions, but that's different.) */
799 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
800 set = 0;
802 /* Unless the language specifies otherwise, let vector types alias
803 their components. This avoids some nasty type punning issues in
804 normal usage. And indeed lets vectors be treated more like an
805 array slice. */
806 else if (TREE_CODE (t) == VECTOR_TYPE)
807 set = get_alias_set (TREE_TYPE (t));
809 /* Unless the language specifies otherwise, treat array types the
810 same as their components. This avoids the asymmetry we get
811 through recording the components. Consider accessing a
812 character(kind=1) through a reference to a character(kind=1)[1:1].
813 Or consider if we want to assign integer(kind=4)[0:D.1387] and
814 integer(kind=4)[4] the same alias set or not.
815 Just be pragmatic here and make sure the array and its element
816 type get the same alias set assigned. */
817 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
818 set = get_alias_set (TREE_TYPE (t));
820 /* From the former common C and C++ langhook implementation:
822 Unfortunately, there is no canonical form of a pointer type.
823 In particular, if we have `typedef int I', then `int *', and
824 `I *' are different types. So, we have to pick a canonical
825 representative. We do this below.
827 Technically, this approach is actually more conservative that
828 it needs to be. In particular, `const int *' and `int *'
829 should be in different alias sets, according to the C and C++
830 standard, since their types are not the same, and so,
831 technically, an `int **' and `const int **' cannot point at
832 the same thing.
834 But, the standard is wrong. In particular, this code is
835 legal C++:
837 int *ip;
838 int **ipp = &ip;
839 const int* const* cipp = ipp;
840 And, it doesn't make sense for that to be legal unless you
841 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
842 the pointed-to types. This issue has been reported to the
843 C++ committee.
845 In addition to the above canonicalization issue, with LTO
846 we should also canonicalize `T (*)[]' to `T *' avoiding
847 alias issues with pointer-to element types and pointer-to
848 array types.
850 Likewise we need to deal with the situation of incomplete
851 pointed-to types and make `*(struct X **)&a' and
852 `*(struct X {} **)&a' alias. Otherwise we will have to
853 guarantee that all pointer-to incomplete type variants
854 will be replaced by pointer-to complete type variants if
855 they are available.
857 With LTO the convenient situation of using `void *' to
858 access and store any pointer type will also become
859 more apparent (and `void *' is just another pointer-to
860 incomplete type). Assigning alias-set zero to `void *'
861 and all pointer-to incomplete types is a not appealing
862 solution. Assigning an effective alias-set zero only
863 affecting pointers might be - by recording proper subset
864 relationships of all pointer alias-sets.
866 Pointer-to function types are another grey area which
867 needs caution. Globbing them all into one alias-set
868 or the above effective zero set would work.
870 For now just assign the same alias-set to all pointers.
871 That's simple and avoids all the above problems. */
872 else if (POINTER_TYPE_P (t)
873 && t != ptr_type_node)
874 set = get_alias_set (ptr_type_node);
876 /* Otherwise make a new alias set for this type. */
877 else
879 /* Each canonical type gets its own alias set, so canonical types
880 shouldn't form a tree. It doesn't really matter for types
881 we handle specially above, so only check it where it possibly
882 would result in a bogus alias set. */
883 gcc_checking_assert (TYPE_CANONICAL (t) == t);
885 set = new_alias_set ();
888 TYPE_ALIAS_SET (t) = set;
890 /* If this is an aggregate type or a complex type, we must record any
891 component aliasing information. */
892 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
893 record_component_aliases (t);
895 return set;
898 /* Return a brand-new alias set. */
900 alias_set_type
901 new_alias_set (void)
903 if (flag_strict_aliasing)
905 if (alias_sets == 0)
906 vec_safe_push (alias_sets, (alias_set_entry) 0);
907 vec_safe_push (alias_sets, (alias_set_entry) 0);
908 return alias_sets->length () - 1;
910 else
911 return 0;
914 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
915 not everything that aliases SUPERSET also aliases SUBSET. For example,
916 in C, a store to an `int' can alias a load of a structure containing an
917 `int', and vice versa. But it can't alias a load of a 'double' member
918 of the same structure. Here, the structure would be the SUPERSET and
919 `int' the SUBSET. This relationship is also described in the comment at
920 the beginning of this file.
922 This function should be called only once per SUPERSET/SUBSET pair.
924 It is illegal for SUPERSET to be zero; everything is implicitly a
925 subset of alias set zero. */
927 void
928 record_alias_subset (alias_set_type superset, alias_set_type subset)
930 alias_set_entry superset_entry;
931 alias_set_entry subset_entry;
933 /* It is possible in complex type situations for both sets to be the same,
934 in which case we can ignore this operation. */
935 if (superset == subset)
936 return;
938 gcc_assert (superset);
940 superset_entry = get_alias_set_entry (superset);
941 if (superset_entry == 0)
943 /* Create an entry for the SUPERSET, so that we have a place to
944 attach the SUBSET. */
945 superset_entry = ggc_cleared_alloc<alias_set_entry_d> ();
946 superset_entry->alias_set = superset;
947 superset_entry->children
948 = splay_tree_new_ggc (splay_tree_compare_ints,
949 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s,
950 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s);
951 superset_entry->has_zero_child = 0;
952 (*alias_sets)[superset] = superset_entry;
955 if (subset == 0)
956 superset_entry->has_zero_child = 1;
957 else
959 subset_entry = get_alias_set_entry (subset);
960 /* If there is an entry for the subset, enter all of its children
961 (if they are not already present) as children of the SUPERSET. */
962 if (subset_entry)
964 if (subset_entry->has_zero_child)
965 superset_entry->has_zero_child = 1;
967 splay_tree_foreach (subset_entry->children, insert_subset_children,
968 superset_entry->children);
971 /* Enter the SUBSET itself as a child of the SUPERSET. */
972 splay_tree_insert (superset_entry->children,
973 (splay_tree_key) subset, 0);
977 /* Record that component types of TYPE, if any, are part of that type for
978 aliasing purposes. For record types, we only record component types
979 for fields that are not marked non-addressable. For array types, we
980 only record the component type if it is not marked non-aliased. */
982 void
983 record_component_aliases (tree type)
985 alias_set_type superset = get_alias_set (type);
986 tree field;
988 if (superset == 0)
989 return;
991 switch (TREE_CODE (type))
993 case RECORD_TYPE:
994 case UNION_TYPE:
995 case QUAL_UNION_TYPE:
996 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
997 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
998 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
999 break;
1001 case COMPLEX_TYPE:
1002 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1003 break;
1005 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1006 element type. */
1008 default:
1009 break;
1013 /* Allocate an alias set for use in storing and reading from the varargs
1014 spill area. */
1016 static GTY(()) alias_set_type varargs_set = -1;
1018 alias_set_type
1019 get_varargs_alias_set (void)
1021 #if 1
1022 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1023 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1024 consistently use the varargs alias set for loads from the varargs
1025 area. So don't use it anywhere. */
1026 return 0;
1027 #else
1028 if (varargs_set == -1)
1029 varargs_set = new_alias_set ();
1031 return varargs_set;
1032 #endif
1035 /* Likewise, but used for the fixed portions of the frame, e.g., register
1036 save areas. */
1038 static GTY(()) alias_set_type frame_set = -1;
1040 alias_set_type
1041 get_frame_alias_set (void)
1043 if (frame_set == -1)
1044 frame_set = new_alias_set ();
1046 return frame_set;
1049 /* Create a new, unique base with id ID. */
1051 static rtx
1052 unique_base_value (HOST_WIDE_INT id)
1054 return gen_rtx_ADDRESS (Pmode, id);
1057 /* Return true if accesses based on any other base value cannot alias
1058 those based on X. */
1060 static bool
1061 unique_base_value_p (rtx x)
1063 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1066 /* Return true if X is known to be a base value. */
1068 static bool
1069 known_base_value_p (rtx x)
1071 switch (GET_CODE (x))
1073 case LABEL_REF:
1074 case SYMBOL_REF:
1075 return true;
1077 case ADDRESS:
1078 /* Arguments may or may not be bases; we don't know for sure. */
1079 return GET_MODE (x) != VOIDmode;
1081 default:
1082 return false;
1086 /* Inside SRC, the source of a SET, find a base address. */
1088 static rtx
1089 find_base_value (rtx src)
1091 unsigned int regno;
1093 #if defined (FIND_BASE_TERM)
1094 /* Try machine-dependent ways to find the base term. */
1095 src = FIND_BASE_TERM (src);
1096 #endif
1098 switch (GET_CODE (src))
1100 case SYMBOL_REF:
1101 case LABEL_REF:
1102 return src;
1104 case REG:
1105 regno = REGNO (src);
1106 /* At the start of a function, argument registers have known base
1107 values which may be lost later. Returning an ADDRESS
1108 expression here allows optimization based on argument values
1109 even when the argument registers are used for other purposes. */
1110 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1111 return new_reg_base_value[regno];
1113 /* If a pseudo has a known base value, return it. Do not do this
1114 for non-fixed hard regs since it can result in a circular
1115 dependency chain for registers which have values at function entry.
1117 The test above is not sufficient because the scheduler may move
1118 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1119 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1120 && regno < vec_safe_length (reg_base_value))
1122 /* If we're inside init_alias_analysis, use new_reg_base_value
1123 to reduce the number of relaxation iterations. */
1124 if (new_reg_base_value && new_reg_base_value[regno]
1125 && DF_REG_DEF_COUNT (regno) == 1)
1126 return new_reg_base_value[regno];
1128 if ((*reg_base_value)[regno])
1129 return (*reg_base_value)[regno];
1132 return 0;
1134 case MEM:
1135 /* Check for an argument passed in memory. Only record in the
1136 copying-arguments block; it is too hard to track changes
1137 otherwise. */
1138 if (copying_arguments
1139 && (XEXP (src, 0) == arg_pointer_rtx
1140 || (GET_CODE (XEXP (src, 0)) == PLUS
1141 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1142 return arg_base_value;
1143 return 0;
1145 case CONST:
1146 src = XEXP (src, 0);
1147 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1148 break;
1150 /* ... fall through ... */
1152 case PLUS:
1153 case MINUS:
1155 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1157 /* If either operand is a REG that is a known pointer, then it
1158 is the base. */
1159 if (REG_P (src_0) && REG_POINTER (src_0))
1160 return find_base_value (src_0);
1161 if (REG_P (src_1) && REG_POINTER (src_1))
1162 return find_base_value (src_1);
1164 /* If either operand is a REG, then see if we already have
1165 a known value for it. */
1166 if (REG_P (src_0))
1168 temp = find_base_value (src_0);
1169 if (temp != 0)
1170 src_0 = temp;
1173 if (REG_P (src_1))
1175 temp = find_base_value (src_1);
1176 if (temp!= 0)
1177 src_1 = temp;
1180 /* If either base is named object or a special address
1181 (like an argument or stack reference), then use it for the
1182 base term. */
1183 if (src_0 != 0 && known_base_value_p (src_0))
1184 return src_0;
1186 if (src_1 != 0 && known_base_value_p (src_1))
1187 return src_1;
1189 /* Guess which operand is the base address:
1190 If either operand is a symbol, then it is the base. If
1191 either operand is a CONST_INT, then the other is the base. */
1192 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1193 return find_base_value (src_0);
1194 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1195 return find_base_value (src_1);
1197 return 0;
1200 case LO_SUM:
1201 /* The standard form is (lo_sum reg sym) so look only at the
1202 second operand. */
1203 return find_base_value (XEXP (src, 1));
1205 case AND:
1206 /* If the second operand is constant set the base
1207 address to the first operand. */
1208 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1209 return find_base_value (XEXP (src, 0));
1210 return 0;
1212 case TRUNCATE:
1213 /* As we do not know which address space the pointer is referring to, we can
1214 handle this only if the target does not support different pointer or
1215 address modes depending on the address space. */
1216 if (!target_default_pointer_address_modes_p ())
1217 break;
1218 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1219 break;
1220 /* Fall through. */
1221 case HIGH:
1222 case PRE_INC:
1223 case PRE_DEC:
1224 case POST_INC:
1225 case POST_DEC:
1226 case PRE_MODIFY:
1227 case POST_MODIFY:
1228 return find_base_value (XEXP (src, 0));
1230 case ZERO_EXTEND:
1231 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1232 /* As we do not know which address space the pointer is referring to, we can
1233 handle this only if the target does not support different pointer or
1234 address modes depending on the address space. */
1235 if (!target_default_pointer_address_modes_p ())
1236 break;
1239 rtx temp = find_base_value (XEXP (src, 0));
1241 if (temp != 0 && CONSTANT_P (temp))
1242 temp = convert_memory_address (Pmode, temp);
1244 return temp;
1247 default:
1248 break;
1251 return 0;
1254 /* Called from init_alias_analysis indirectly through note_stores,
1255 or directly if DEST is a register with a REG_NOALIAS note attached.
1256 SET is null in the latter case. */
1258 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1259 register N has been set in this function. */
1260 static sbitmap reg_seen;
1262 static void
1263 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1265 unsigned regno;
1266 rtx src;
1267 int n;
1269 if (!REG_P (dest))
1270 return;
1272 regno = REGNO (dest);
1274 gcc_checking_assert (regno < reg_base_value->length ());
1276 /* If this spans multiple hard registers, then we must indicate that every
1277 register has an unusable value. */
1278 if (regno < FIRST_PSEUDO_REGISTER)
1279 n = hard_regno_nregs[regno][GET_MODE (dest)];
1280 else
1281 n = 1;
1282 if (n != 1)
1284 while (--n >= 0)
1286 bitmap_set_bit (reg_seen, regno + n);
1287 new_reg_base_value[regno + n] = 0;
1289 return;
1292 if (set)
1294 /* A CLOBBER wipes out any old value but does not prevent a previously
1295 unset register from acquiring a base address (i.e. reg_seen is not
1296 set). */
1297 if (GET_CODE (set) == CLOBBER)
1299 new_reg_base_value[regno] = 0;
1300 return;
1302 src = SET_SRC (set);
1304 else
1306 /* There's a REG_NOALIAS note against DEST. */
1307 if (bitmap_bit_p (reg_seen, regno))
1309 new_reg_base_value[regno] = 0;
1310 return;
1312 bitmap_set_bit (reg_seen, regno);
1313 new_reg_base_value[regno] = unique_base_value (unique_id++);
1314 return;
1317 /* If this is not the first set of REGNO, see whether the new value
1318 is related to the old one. There are two cases of interest:
1320 (1) The register might be assigned an entirely new value
1321 that has the same base term as the original set.
1323 (2) The set might be a simple self-modification that
1324 cannot change REGNO's base value.
1326 If neither case holds, reject the original base value as invalid.
1327 Note that the following situation is not detected:
1329 extern int x, y; int *p = &x; p += (&y-&x);
1331 ANSI C does not allow computing the difference of addresses
1332 of distinct top level objects. */
1333 if (new_reg_base_value[regno] != 0
1334 && find_base_value (src) != new_reg_base_value[regno])
1335 switch (GET_CODE (src))
1337 case LO_SUM:
1338 case MINUS:
1339 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1340 new_reg_base_value[regno] = 0;
1341 break;
1342 case PLUS:
1343 /* If the value we add in the PLUS is also a valid base value,
1344 this might be the actual base value, and the original value
1345 an index. */
1347 rtx other = NULL_RTX;
1349 if (XEXP (src, 0) == dest)
1350 other = XEXP (src, 1);
1351 else if (XEXP (src, 1) == dest)
1352 other = XEXP (src, 0);
1354 if (! other || find_base_value (other))
1355 new_reg_base_value[regno] = 0;
1356 break;
1358 case AND:
1359 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1360 new_reg_base_value[regno] = 0;
1361 break;
1362 default:
1363 new_reg_base_value[regno] = 0;
1364 break;
1366 /* If this is the first set of a register, record the value. */
1367 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1368 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1369 new_reg_base_value[regno] = find_base_value (src);
1371 bitmap_set_bit (reg_seen, regno);
1374 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1375 using hard registers with non-null REG_BASE_VALUE for renaming. */
1377 get_reg_base_value (unsigned int regno)
1379 return (*reg_base_value)[regno];
1382 /* If a value is known for REGNO, return it. */
1385 get_reg_known_value (unsigned int regno)
1387 if (regno >= FIRST_PSEUDO_REGISTER)
1389 regno -= FIRST_PSEUDO_REGISTER;
1390 if (regno < vec_safe_length (reg_known_value))
1391 return (*reg_known_value)[regno];
1393 return NULL;
1396 /* Set it. */
1398 static void
1399 set_reg_known_value (unsigned int regno, rtx val)
1401 if (regno >= FIRST_PSEUDO_REGISTER)
1403 regno -= FIRST_PSEUDO_REGISTER;
1404 if (regno < vec_safe_length (reg_known_value))
1405 (*reg_known_value)[regno] = val;
1409 /* Similarly for reg_known_equiv_p. */
1411 bool
1412 get_reg_known_equiv_p (unsigned int regno)
1414 if (regno >= FIRST_PSEUDO_REGISTER)
1416 regno -= FIRST_PSEUDO_REGISTER;
1417 if (regno < vec_safe_length (reg_known_value))
1418 return bitmap_bit_p (reg_known_equiv_p, regno);
1420 return false;
1423 static void
1424 set_reg_known_equiv_p (unsigned int regno, bool val)
1426 if (regno >= FIRST_PSEUDO_REGISTER)
1428 regno -= FIRST_PSEUDO_REGISTER;
1429 if (regno < vec_safe_length (reg_known_value))
1431 if (val)
1432 bitmap_set_bit (reg_known_equiv_p, regno);
1433 else
1434 bitmap_clear_bit (reg_known_equiv_p, regno);
1440 /* Returns a canonical version of X, from the point of view alias
1441 analysis. (For example, if X is a MEM whose address is a register,
1442 and the register has a known value (say a SYMBOL_REF), then a MEM
1443 whose address is the SYMBOL_REF is returned.) */
1446 canon_rtx (rtx x)
1448 /* Recursively look for equivalences. */
1449 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1451 rtx t = get_reg_known_value (REGNO (x));
1452 if (t == x)
1453 return x;
1454 if (t)
1455 return canon_rtx (t);
1458 if (GET_CODE (x) == PLUS)
1460 rtx x0 = canon_rtx (XEXP (x, 0));
1461 rtx x1 = canon_rtx (XEXP (x, 1));
1463 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1465 if (CONST_INT_P (x0))
1466 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1467 else if (CONST_INT_P (x1))
1468 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1469 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1473 /* This gives us much better alias analysis when called from
1474 the loop optimizer. Note we want to leave the original
1475 MEM alone, but need to return the canonicalized MEM with
1476 all the flags with their original values. */
1477 else if (MEM_P (x))
1478 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1480 return x;
1483 /* Return 1 if X and Y are identical-looking rtx's.
1484 Expect that X and Y has been already canonicalized.
1486 We use the data in reg_known_value above to see if two registers with
1487 different numbers are, in fact, equivalent. */
1489 static int
1490 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1492 int i;
1493 int j;
1494 enum rtx_code code;
1495 const char *fmt;
1497 if (x == 0 && y == 0)
1498 return 1;
1499 if (x == 0 || y == 0)
1500 return 0;
1502 if (x == y)
1503 return 1;
1505 code = GET_CODE (x);
1506 /* Rtx's of different codes cannot be equal. */
1507 if (code != GET_CODE (y))
1508 return 0;
1510 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1511 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1513 if (GET_MODE (x) != GET_MODE (y))
1514 return 0;
1516 /* Some RTL can be compared without a recursive examination. */
1517 switch (code)
1519 case REG:
1520 return REGNO (x) == REGNO (y);
1522 case LABEL_REF:
1523 return XEXP (x, 0) == XEXP (y, 0);
1525 case SYMBOL_REF:
1526 return XSTR (x, 0) == XSTR (y, 0);
1528 case ENTRY_VALUE:
1529 /* This is magic, don't go through canonicalization et al. */
1530 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1532 case VALUE:
1533 CASE_CONST_UNIQUE:
1534 /* Pointer equality guarantees equality for these nodes. */
1535 return 0;
1537 default:
1538 break;
1541 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1542 if (code == PLUS)
1543 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1544 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1545 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1546 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1547 /* For commutative operations, the RTX match if the operand match in any
1548 order. Also handle the simple binary and unary cases without a loop. */
1549 if (COMMUTATIVE_P (x))
1551 rtx xop0 = canon_rtx (XEXP (x, 0));
1552 rtx yop0 = canon_rtx (XEXP (y, 0));
1553 rtx yop1 = canon_rtx (XEXP (y, 1));
1555 return ((rtx_equal_for_memref_p (xop0, yop0)
1556 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1557 || (rtx_equal_for_memref_p (xop0, yop1)
1558 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1560 else if (NON_COMMUTATIVE_P (x))
1562 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1563 canon_rtx (XEXP (y, 0)))
1564 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1565 canon_rtx (XEXP (y, 1))));
1567 else if (UNARY_P (x))
1568 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1569 canon_rtx (XEXP (y, 0)));
1571 /* Compare the elements. If any pair of corresponding elements
1572 fail to match, return 0 for the whole things.
1574 Limit cases to types which actually appear in addresses. */
1576 fmt = GET_RTX_FORMAT (code);
1577 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1579 switch (fmt[i])
1581 case 'i':
1582 if (XINT (x, i) != XINT (y, i))
1583 return 0;
1584 break;
1586 case 'E':
1587 /* Two vectors must have the same length. */
1588 if (XVECLEN (x, i) != XVECLEN (y, i))
1589 return 0;
1591 /* And the corresponding elements must match. */
1592 for (j = 0; j < XVECLEN (x, i); j++)
1593 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1594 canon_rtx (XVECEXP (y, i, j))) == 0)
1595 return 0;
1596 break;
1598 case 'e':
1599 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1600 canon_rtx (XEXP (y, i))) == 0)
1601 return 0;
1602 break;
1604 /* This can happen for asm operands. */
1605 case 's':
1606 if (strcmp (XSTR (x, i), XSTR (y, i)))
1607 return 0;
1608 break;
1610 /* This can happen for an asm which clobbers memory. */
1611 case '0':
1612 break;
1614 /* It is believed that rtx's at this level will never
1615 contain anything but integers and other rtx's,
1616 except for within LABEL_REFs and SYMBOL_REFs. */
1617 default:
1618 gcc_unreachable ();
1621 return 1;
1624 static rtx
1625 find_base_term (rtx x)
1627 cselib_val *val;
1628 struct elt_loc_list *l, *f;
1629 rtx ret;
1631 #if defined (FIND_BASE_TERM)
1632 /* Try machine-dependent ways to find the base term. */
1633 x = FIND_BASE_TERM (x);
1634 #endif
1636 switch (GET_CODE (x))
1638 case REG:
1639 return REG_BASE_VALUE (x);
1641 case TRUNCATE:
1642 /* As we do not know which address space the pointer is referring to, we can
1643 handle this only if the target does not support different pointer or
1644 address modes depending on the address space. */
1645 if (!target_default_pointer_address_modes_p ())
1646 return 0;
1647 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1648 return 0;
1649 /* Fall through. */
1650 case HIGH:
1651 case PRE_INC:
1652 case PRE_DEC:
1653 case POST_INC:
1654 case POST_DEC:
1655 case PRE_MODIFY:
1656 case POST_MODIFY:
1657 return find_base_term (XEXP (x, 0));
1659 case ZERO_EXTEND:
1660 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1661 /* As we do not know which address space the pointer is referring to, we can
1662 handle this only if the target does not support different pointer or
1663 address modes depending on the address space. */
1664 if (!target_default_pointer_address_modes_p ())
1665 return 0;
1668 rtx temp = find_base_term (XEXP (x, 0));
1670 if (temp != 0 && CONSTANT_P (temp))
1671 temp = convert_memory_address (Pmode, temp);
1673 return temp;
1676 case VALUE:
1677 val = CSELIB_VAL_PTR (x);
1678 ret = NULL_RTX;
1680 if (!val)
1681 return ret;
1683 if (cselib_sp_based_value_p (val))
1684 return static_reg_base_value[STACK_POINTER_REGNUM];
1686 f = val->locs;
1687 /* Temporarily reset val->locs to avoid infinite recursion. */
1688 val->locs = NULL;
1690 for (l = f; l; l = l->next)
1691 if (GET_CODE (l->loc) == VALUE
1692 && CSELIB_VAL_PTR (l->loc)->locs
1693 && !CSELIB_VAL_PTR (l->loc)->locs->next
1694 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1695 continue;
1696 else if ((ret = find_base_term (l->loc)) != 0)
1697 break;
1699 val->locs = f;
1700 return ret;
1702 case LO_SUM:
1703 /* The standard form is (lo_sum reg sym) so look only at the
1704 second operand. */
1705 return find_base_term (XEXP (x, 1));
1707 case CONST:
1708 x = XEXP (x, 0);
1709 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1710 return 0;
1711 /* Fall through. */
1712 case PLUS:
1713 case MINUS:
1715 rtx tmp1 = XEXP (x, 0);
1716 rtx tmp2 = XEXP (x, 1);
1718 /* This is a little bit tricky since we have to determine which of
1719 the two operands represents the real base address. Otherwise this
1720 routine may return the index register instead of the base register.
1722 That may cause us to believe no aliasing was possible, when in
1723 fact aliasing is possible.
1725 We use a few simple tests to guess the base register. Additional
1726 tests can certainly be added. For example, if one of the operands
1727 is a shift or multiply, then it must be the index register and the
1728 other operand is the base register. */
1730 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1731 return find_base_term (tmp2);
1733 /* If either operand is known to be a pointer, then prefer it
1734 to determine the base term. */
1735 if (REG_P (tmp1) && REG_POINTER (tmp1))
1737 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1739 rtx tem = tmp1;
1740 tmp1 = tmp2;
1741 tmp2 = tem;
1744 /* Go ahead and find the base term for both operands. If either base
1745 term is from a pointer or is a named object or a special address
1746 (like an argument or stack reference), then use it for the
1747 base term. */
1748 rtx base = find_base_term (tmp1);
1749 if (base != NULL_RTX
1750 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1751 || known_base_value_p (base)))
1752 return base;
1753 base = find_base_term (tmp2);
1754 if (base != NULL_RTX
1755 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1756 || known_base_value_p (base)))
1757 return base;
1759 /* We could not determine which of the two operands was the
1760 base register and which was the index. So we can determine
1761 nothing from the base alias check. */
1762 return 0;
1765 case AND:
1766 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1767 return find_base_term (XEXP (x, 0));
1768 return 0;
1770 case SYMBOL_REF:
1771 case LABEL_REF:
1772 return x;
1774 default:
1775 return 0;
1779 /* Return true if accesses to address X may alias accesses based
1780 on the stack pointer. */
1782 bool
1783 may_be_sp_based_p (rtx x)
1785 rtx base = find_base_term (x);
1786 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1789 /* Return 0 if the addresses X and Y are known to point to different
1790 objects, 1 if they might be pointers to the same object. */
1792 static int
1793 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1794 enum machine_mode x_mode, enum machine_mode y_mode)
1796 /* If the address itself has no known base see if a known equivalent
1797 value has one. If either address still has no known base, nothing
1798 is known about aliasing. */
1799 if (x_base == 0)
1801 rtx x_c;
1803 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1804 return 1;
1806 x_base = find_base_term (x_c);
1807 if (x_base == 0)
1808 return 1;
1811 if (y_base == 0)
1813 rtx y_c;
1814 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1815 return 1;
1817 y_base = find_base_term (y_c);
1818 if (y_base == 0)
1819 return 1;
1822 /* If the base addresses are equal nothing is known about aliasing. */
1823 if (rtx_equal_p (x_base, y_base))
1824 return 1;
1826 /* The base addresses are different expressions. If they are not accessed
1827 via AND, there is no conflict. We can bring knowledge of object
1828 alignment into play here. For example, on alpha, "char a, b;" can
1829 alias one another, though "char a; long b;" cannot. AND addesses may
1830 implicitly alias surrounding objects; i.e. unaligned access in DImode
1831 via AND address can alias all surrounding object types except those
1832 with aligment 8 or higher. */
1833 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1834 return 1;
1835 if (GET_CODE (x) == AND
1836 && (!CONST_INT_P (XEXP (x, 1))
1837 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1838 return 1;
1839 if (GET_CODE (y) == AND
1840 && (!CONST_INT_P (XEXP (y, 1))
1841 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1842 return 1;
1844 /* Differing symbols not accessed via AND never alias. */
1845 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1846 return 0;
1848 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
1849 return 0;
1851 return 1;
1854 /* Callback for for_each_rtx, that returns 1 upon encountering a VALUE
1855 whose UID is greater than the int uid that D points to. */
1857 static int
1858 refs_newer_value_cb (rtx *x, void *d)
1860 if (GET_CODE (*x) == VALUE && CSELIB_VAL_PTR (*x)->uid > *(int *)d)
1861 return 1;
1863 return 0;
1866 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1867 that of V. */
1869 static bool
1870 refs_newer_value_p (rtx expr, rtx v)
1872 int minuid = CSELIB_VAL_PTR (v)->uid;
1874 return for_each_rtx (&expr, refs_newer_value_cb, &minuid);
1877 /* Convert the address X into something we can use. This is done by returning
1878 it unchanged unless it is a value; in the latter case we call cselib to get
1879 a more useful rtx. */
1882 get_addr (rtx x)
1884 cselib_val *v;
1885 struct elt_loc_list *l;
1887 if (GET_CODE (x) != VALUE)
1888 return x;
1889 v = CSELIB_VAL_PTR (x);
1890 if (v)
1892 bool have_equivs = cselib_have_permanent_equivalences ();
1893 if (have_equivs)
1894 v = canonical_cselib_val (v);
1895 for (l = v->locs; l; l = l->next)
1896 if (CONSTANT_P (l->loc))
1897 return l->loc;
1898 for (l = v->locs; l; l = l->next)
1899 if (!REG_P (l->loc) && !MEM_P (l->loc)
1900 /* Avoid infinite recursion when potentially dealing with
1901 var-tracking artificial equivalences, by skipping the
1902 equivalences themselves, and not choosing expressions
1903 that refer to newer VALUEs. */
1904 && (!have_equivs
1905 || (GET_CODE (l->loc) != VALUE
1906 && !refs_newer_value_p (l->loc, x))))
1907 return l->loc;
1908 if (have_equivs)
1910 for (l = v->locs; l; l = l->next)
1911 if (REG_P (l->loc)
1912 || (GET_CODE (l->loc) != VALUE
1913 && !refs_newer_value_p (l->loc, x)))
1914 return l->loc;
1915 /* Return the canonical value. */
1916 return v->val_rtx;
1918 if (v->locs)
1919 return v->locs->loc;
1921 return x;
1924 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1925 where SIZE is the size in bytes of the memory reference. If ADDR
1926 is not modified by the memory reference then ADDR is returned. */
1928 static rtx
1929 addr_side_effect_eval (rtx addr, int size, int n_refs)
1931 int offset = 0;
1933 switch (GET_CODE (addr))
1935 case PRE_INC:
1936 offset = (n_refs + 1) * size;
1937 break;
1938 case PRE_DEC:
1939 offset = -(n_refs + 1) * size;
1940 break;
1941 case POST_INC:
1942 offset = n_refs * size;
1943 break;
1944 case POST_DEC:
1945 offset = -n_refs * size;
1946 break;
1948 default:
1949 return addr;
1952 if (offset)
1953 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1954 gen_int_mode (offset, GET_MODE (addr)));
1955 else
1956 addr = XEXP (addr, 0);
1957 addr = canon_rtx (addr);
1959 return addr;
1962 /* Return TRUE if an object X sized at XSIZE bytes and another object
1963 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1964 any of the sizes is zero, assume an overlap, otherwise use the
1965 absolute value of the sizes as the actual sizes. */
1967 static inline bool
1968 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
1970 return (xsize == 0 || ysize == 0
1971 || (c >= 0
1972 ? (abs (xsize) > c)
1973 : (abs (ysize) > -c)));
1976 /* Return one if X and Y (memory addresses) reference the
1977 same location in memory or if the references overlap.
1978 Return zero if they do not overlap, else return
1979 minus one in which case they still might reference the same location.
1981 C is an offset accumulator. When
1982 C is nonzero, we are testing aliases between X and Y + C.
1983 XSIZE is the size in bytes of the X reference,
1984 similarly YSIZE is the size in bytes for Y.
1985 Expect that canon_rtx has been already called for X and Y.
1987 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1988 referenced (the reference was BLKmode), so make the most pessimistic
1989 assumptions.
1991 If XSIZE or YSIZE is negative, we may access memory outside the object
1992 being referenced as a side effect. This can happen when using AND to
1993 align memory references, as is done on the Alpha.
1995 Nice to notice that varying addresses cannot conflict with fp if no
1996 local variables had their addresses taken, but that's too hard now.
1998 ??? Contrary to the tree alias oracle this does not return
1999 one for X + non-constant and Y + non-constant when X and Y are equal.
2000 If that is fixed the TBAA hack for union type-punning can be removed. */
2002 static int
2003 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2005 if (GET_CODE (x) == VALUE)
2007 if (REG_P (y))
2009 struct elt_loc_list *l = NULL;
2010 if (CSELIB_VAL_PTR (x))
2011 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2012 l; l = l->next)
2013 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2014 break;
2015 if (l)
2016 x = y;
2017 else
2018 x = get_addr (x);
2020 /* Don't call get_addr if y is the same VALUE. */
2021 else if (x != y)
2022 x = get_addr (x);
2024 if (GET_CODE (y) == VALUE)
2026 if (REG_P (x))
2028 struct elt_loc_list *l = NULL;
2029 if (CSELIB_VAL_PTR (y))
2030 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2031 l; l = l->next)
2032 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2033 break;
2034 if (l)
2035 y = x;
2036 else
2037 y = get_addr (y);
2039 /* Don't call get_addr if x is the same VALUE. */
2040 else if (y != x)
2041 y = get_addr (y);
2043 if (GET_CODE (x) == HIGH)
2044 x = XEXP (x, 0);
2045 else if (GET_CODE (x) == LO_SUM)
2046 x = XEXP (x, 1);
2047 else
2048 x = addr_side_effect_eval (x, abs (xsize), 0);
2049 if (GET_CODE (y) == HIGH)
2050 y = XEXP (y, 0);
2051 else if (GET_CODE (y) == LO_SUM)
2052 y = XEXP (y, 1);
2053 else
2054 y = addr_side_effect_eval (y, abs (ysize), 0);
2056 if (rtx_equal_for_memref_p (x, y))
2058 return offset_overlap_p (c, xsize, ysize);
2061 /* This code used to check for conflicts involving stack references and
2062 globals but the base address alias code now handles these cases. */
2064 if (GET_CODE (x) == PLUS)
2066 /* The fact that X is canonicalized means that this
2067 PLUS rtx is canonicalized. */
2068 rtx x0 = XEXP (x, 0);
2069 rtx x1 = XEXP (x, 1);
2071 if (GET_CODE (y) == PLUS)
2073 /* The fact that Y is canonicalized means that this
2074 PLUS rtx is canonicalized. */
2075 rtx y0 = XEXP (y, 0);
2076 rtx y1 = XEXP (y, 1);
2078 if (rtx_equal_for_memref_p (x1, y1))
2079 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2080 if (rtx_equal_for_memref_p (x0, y0))
2081 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2082 if (CONST_INT_P (x1))
2084 if (CONST_INT_P (y1))
2085 return memrefs_conflict_p (xsize, x0, ysize, y0,
2086 c - INTVAL (x1) + INTVAL (y1));
2087 else
2088 return memrefs_conflict_p (xsize, x0, ysize, y,
2089 c - INTVAL (x1));
2091 else if (CONST_INT_P (y1))
2092 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2094 return -1;
2096 else if (CONST_INT_P (x1))
2097 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2099 else if (GET_CODE (y) == PLUS)
2101 /* The fact that Y is canonicalized means that this
2102 PLUS rtx is canonicalized. */
2103 rtx y0 = XEXP (y, 0);
2104 rtx y1 = XEXP (y, 1);
2106 if (CONST_INT_P (y1))
2107 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2108 else
2109 return -1;
2112 if (GET_CODE (x) == GET_CODE (y))
2113 switch (GET_CODE (x))
2115 case MULT:
2117 /* Handle cases where we expect the second operands to be the
2118 same, and check only whether the first operand would conflict
2119 or not. */
2120 rtx x0, y0;
2121 rtx x1 = canon_rtx (XEXP (x, 1));
2122 rtx y1 = canon_rtx (XEXP (y, 1));
2123 if (! rtx_equal_for_memref_p (x1, y1))
2124 return -1;
2125 x0 = canon_rtx (XEXP (x, 0));
2126 y0 = canon_rtx (XEXP (y, 0));
2127 if (rtx_equal_for_memref_p (x0, y0))
2128 return offset_overlap_p (c, xsize, ysize);
2130 /* Can't properly adjust our sizes. */
2131 if (!CONST_INT_P (x1))
2132 return -1;
2133 xsize /= INTVAL (x1);
2134 ysize /= INTVAL (x1);
2135 c /= INTVAL (x1);
2136 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2139 default:
2140 break;
2143 /* Deal with alignment ANDs by adjusting offset and size so as to
2144 cover the maximum range, without taking any previously known
2145 alignment into account. Make a size negative after such an
2146 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2147 assume a potential overlap, because they may end up in contiguous
2148 memory locations and the stricter-alignment access may span over
2149 part of both. */
2150 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2152 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2153 unsigned HOST_WIDE_INT uc = sc;
2154 if (sc < 0 && -uc == (uc & -uc))
2156 if (xsize > 0)
2157 xsize = -xsize;
2158 if (xsize)
2159 xsize += sc + 1;
2160 c -= sc + 1;
2161 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2162 ysize, y, c);
2165 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2167 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2168 unsigned HOST_WIDE_INT uc = sc;
2169 if (sc < 0 && -uc == (uc & -uc))
2171 if (ysize > 0)
2172 ysize = -ysize;
2173 if (ysize)
2174 ysize += sc + 1;
2175 c += sc + 1;
2176 return memrefs_conflict_p (xsize, x,
2177 ysize, canon_rtx (XEXP (y, 0)), c);
2181 if (CONSTANT_P (x))
2183 if (CONST_INT_P (x) && CONST_INT_P (y))
2185 c += (INTVAL (y) - INTVAL (x));
2186 return offset_overlap_p (c, xsize, ysize);
2189 if (GET_CODE (x) == CONST)
2191 if (GET_CODE (y) == CONST)
2192 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2193 ysize, canon_rtx (XEXP (y, 0)), c);
2194 else
2195 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2196 ysize, y, c);
2198 if (GET_CODE (y) == CONST)
2199 return memrefs_conflict_p (xsize, x, ysize,
2200 canon_rtx (XEXP (y, 0)), c);
2202 /* Assume a potential overlap for symbolic addresses that went
2203 through alignment adjustments (i.e., that have negative
2204 sizes), because we can't know how far they are from each
2205 other. */
2206 if (CONSTANT_P (y))
2207 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2209 return -1;
2212 return -1;
2215 /* Functions to compute memory dependencies.
2217 Since we process the insns in execution order, we can build tables
2218 to keep track of what registers are fixed (and not aliased), what registers
2219 are varying in known ways, and what registers are varying in unknown
2220 ways.
2222 If both memory references are volatile, then there must always be a
2223 dependence between the two references, since their order can not be
2224 changed. A volatile and non-volatile reference can be interchanged
2225 though.
2227 We also must allow AND addresses, because they may generate accesses
2228 outside the object being referenced. This is used to generate aligned
2229 addresses from unaligned addresses, for instance, the alpha
2230 storeqi_unaligned pattern. */
2232 /* Read dependence: X is read after read in MEM takes place. There can
2233 only be a dependence here if both reads are volatile, or if either is
2234 an explicit barrier. */
2237 read_dependence (const_rtx mem, const_rtx x)
2239 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2240 return true;
2241 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2242 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2243 return true;
2244 return false;
2247 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2249 static tree
2250 decl_for_component_ref (tree x)
2254 x = TREE_OPERAND (x, 0);
2256 while (x && TREE_CODE (x) == COMPONENT_REF);
2258 return x && DECL_P (x) ? x : NULL_TREE;
2261 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2262 for the offset of the field reference. *KNOWN_P says whether the
2263 offset is known. */
2265 static void
2266 adjust_offset_for_component_ref (tree x, bool *known_p,
2267 HOST_WIDE_INT *offset)
2269 if (!*known_p)
2270 return;
2273 tree xoffset = component_ref_field_offset (x);
2274 tree field = TREE_OPERAND (x, 1);
2275 if (TREE_CODE (xoffset) != INTEGER_CST)
2277 *known_p = false;
2278 return;
2281 offset_int woffset
2282 = (wi::to_offset (xoffset)
2283 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2284 LOG2_BITS_PER_UNIT));
2285 if (!wi::fits_uhwi_p (woffset))
2287 *known_p = false;
2288 return;
2290 *offset += woffset.to_uhwi ();
2292 x = TREE_OPERAND (x, 0);
2294 while (x && TREE_CODE (x) == COMPONENT_REF);
2297 /* Return nonzero if we can determine the exprs corresponding to memrefs
2298 X and Y and they do not overlap.
2299 If LOOP_VARIANT is set, skip offset-based disambiguation */
2302 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2304 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2305 rtx rtlx, rtly;
2306 rtx basex, basey;
2307 bool moffsetx_known_p, moffsety_known_p;
2308 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2309 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2311 /* Unless both have exprs, we can't tell anything. */
2312 if (exprx == 0 || expry == 0)
2313 return 0;
2315 /* For spill-slot accesses make sure we have valid offsets. */
2316 if ((exprx == get_spill_slot_decl (false)
2317 && ! MEM_OFFSET_KNOWN_P (x))
2318 || (expry == get_spill_slot_decl (false)
2319 && ! MEM_OFFSET_KNOWN_P (y)))
2320 return 0;
2322 /* If the field reference test failed, look at the DECLs involved. */
2323 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2324 if (moffsetx_known_p)
2325 moffsetx = MEM_OFFSET (x);
2326 if (TREE_CODE (exprx) == COMPONENT_REF)
2328 tree t = decl_for_component_ref (exprx);
2329 if (! t)
2330 return 0;
2331 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2332 exprx = t;
2335 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2336 if (moffsety_known_p)
2337 moffsety = MEM_OFFSET (y);
2338 if (TREE_CODE (expry) == COMPONENT_REF)
2340 tree t = decl_for_component_ref (expry);
2341 if (! t)
2342 return 0;
2343 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2344 expry = t;
2347 if (! DECL_P (exprx) || ! DECL_P (expry))
2348 return 0;
2350 /* With invalid code we can end up storing into the constant pool.
2351 Bail out to avoid ICEing when creating RTL for this.
2352 See gfortran.dg/lto/20091028-2_0.f90. */
2353 if (TREE_CODE (exprx) == CONST_DECL
2354 || TREE_CODE (expry) == CONST_DECL)
2355 return 1;
2357 rtlx = DECL_RTL (exprx);
2358 rtly = DECL_RTL (expry);
2360 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2361 can't overlap unless they are the same because we never reuse that part
2362 of the stack frame used for locals for spilled pseudos. */
2363 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2364 && ! rtx_equal_p (rtlx, rtly))
2365 return 1;
2367 /* If we have MEMs referring to different address spaces (which can
2368 potentially overlap), we cannot easily tell from the addresses
2369 whether the references overlap. */
2370 if (MEM_P (rtlx) && MEM_P (rtly)
2371 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2372 return 0;
2374 /* Get the base and offsets of both decls. If either is a register, we
2375 know both are and are the same, so use that as the base. The only
2376 we can avoid overlap is if we can deduce that they are nonoverlapping
2377 pieces of that decl, which is very rare. */
2378 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2379 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2380 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2382 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2383 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2384 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2386 /* If the bases are different, we know they do not overlap if both
2387 are constants or if one is a constant and the other a pointer into the
2388 stack frame. Otherwise a different base means we can't tell if they
2389 overlap or not. */
2390 if (! rtx_equal_p (basex, basey))
2391 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2392 || (CONSTANT_P (basex) && REG_P (basey)
2393 && REGNO_PTR_FRAME_P (REGNO (basey)))
2394 || (CONSTANT_P (basey) && REG_P (basex)
2395 && REGNO_PTR_FRAME_P (REGNO (basex))));
2397 /* Offset based disambiguation not appropriate for loop invariant */
2398 if (loop_invariant)
2399 return 0;
2401 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2402 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2403 : -1);
2404 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2405 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2406 : -1);
2408 /* If we have an offset for either memref, it can update the values computed
2409 above. */
2410 if (moffsetx_known_p)
2411 offsetx += moffsetx, sizex -= moffsetx;
2412 if (moffsety_known_p)
2413 offsety += moffsety, sizey -= moffsety;
2415 /* If a memref has both a size and an offset, we can use the smaller size.
2416 We can't do this if the offset isn't known because we must view this
2417 memref as being anywhere inside the DECL's MEM. */
2418 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2419 sizex = MEM_SIZE (x);
2420 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2421 sizey = MEM_SIZE (y);
2423 /* Put the values of the memref with the lower offset in X's values. */
2424 if (offsetx > offsety)
2426 tem = offsetx, offsetx = offsety, offsety = tem;
2427 tem = sizex, sizex = sizey, sizey = tem;
2430 /* If we don't know the size of the lower-offset value, we can't tell
2431 if they conflict. Otherwise, we do the test. */
2432 return sizex >= 0 && offsety >= offsetx + sizex;
2435 /* Helper for true_dependence and canon_true_dependence.
2436 Checks for true dependence: X is read after store in MEM takes place.
2438 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2439 NULL_RTX, and the canonical addresses of MEM and X are both computed
2440 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2442 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2444 Returns 1 if there is a true dependence, 0 otherwise. */
2446 static int
2447 true_dependence_1 (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2448 const_rtx x, rtx x_addr, bool mem_canonicalized)
2450 rtx base;
2451 int ret;
2453 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2454 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2456 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2457 return 1;
2459 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2460 This is used in epilogue deallocation functions, and in cselib. */
2461 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2462 return 1;
2463 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2464 return 1;
2465 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2466 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2467 return 1;
2469 /* Read-only memory is by definition never modified, and therefore can't
2470 conflict with anything. We don't expect to find read-only set on MEM,
2471 but stupid user tricks can produce them, so don't die. */
2472 if (MEM_READONLY_P (x))
2473 return 0;
2475 /* If we have MEMs referring to different address spaces (which can
2476 potentially overlap), we cannot easily tell from the addresses
2477 whether the references overlap. */
2478 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2479 return 1;
2481 if (! mem_addr)
2483 mem_addr = XEXP (mem, 0);
2484 if (mem_mode == VOIDmode)
2485 mem_mode = GET_MODE (mem);
2488 if (! x_addr)
2490 x_addr = XEXP (x, 0);
2491 if (!((GET_CODE (x_addr) == VALUE
2492 && GET_CODE (mem_addr) != VALUE
2493 && reg_mentioned_p (x_addr, mem_addr))
2494 || (GET_CODE (x_addr) != VALUE
2495 && GET_CODE (mem_addr) == VALUE
2496 && reg_mentioned_p (mem_addr, x_addr))))
2498 x_addr = get_addr (x_addr);
2499 if (! mem_canonicalized)
2500 mem_addr = get_addr (mem_addr);
2504 base = find_base_term (x_addr);
2505 if (base && (GET_CODE (base) == LABEL_REF
2506 || (GET_CODE (base) == SYMBOL_REF
2507 && CONSTANT_POOL_ADDRESS_P (base))))
2508 return 0;
2510 rtx mem_base = find_base_term (mem_addr);
2511 if (! base_alias_check (x_addr, base, mem_addr, mem_base,
2512 GET_MODE (x), mem_mode))
2513 return 0;
2515 x_addr = canon_rtx (x_addr);
2516 if (!mem_canonicalized)
2517 mem_addr = canon_rtx (mem_addr);
2519 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2520 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2521 return ret;
2523 if (mems_in_disjoint_alias_sets_p (x, mem))
2524 return 0;
2526 if (nonoverlapping_memrefs_p (mem, x, false))
2527 return 0;
2529 return rtx_refs_may_alias_p (x, mem, true);
2532 /* True dependence: X is read after store in MEM takes place. */
2535 true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x)
2537 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2538 x, NULL_RTX, /*mem_canonicalized=*/false);
2541 /* Canonical true dependence: X is read after store in MEM takes place.
2542 Variant of true_dependence which assumes MEM has already been
2543 canonicalized (hence we no longer do that here).
2544 The mem_addr argument has been added, since true_dependence_1 computed
2545 this value prior to canonicalizing. */
2548 canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2549 const_rtx x, rtx x_addr)
2551 return true_dependence_1 (mem, mem_mode, mem_addr,
2552 x, x_addr, /*mem_canonicalized=*/true);
2555 /* Returns nonzero if a write to X might alias a previous read from
2556 (or, if WRITEP is true, a write to) MEM.
2557 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2558 and X_MODE the mode for that access.
2559 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2561 static int
2562 write_dependence_p (const_rtx mem,
2563 const_rtx x, enum machine_mode x_mode, rtx x_addr,
2564 bool mem_canonicalized, bool x_canonicalized, bool writep)
2566 rtx mem_addr;
2567 rtx base;
2568 int ret;
2570 gcc_checking_assert (x_canonicalized
2571 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2572 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2574 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2575 return 1;
2577 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2578 This is used in epilogue deallocation functions. */
2579 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2580 return 1;
2581 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2582 return 1;
2583 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2584 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2585 return 1;
2587 /* A read from read-only memory can't conflict with read-write memory. */
2588 if (!writep && MEM_READONLY_P (mem))
2589 return 0;
2591 /* If we have MEMs referring to different address spaces (which can
2592 potentially overlap), we cannot easily tell from the addresses
2593 whether the references overlap. */
2594 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2595 return 1;
2597 mem_addr = XEXP (mem, 0);
2598 if (!x_addr)
2600 x_addr = XEXP (x, 0);
2601 if (!((GET_CODE (x_addr) == VALUE
2602 && GET_CODE (mem_addr) != VALUE
2603 && reg_mentioned_p (x_addr, mem_addr))
2604 || (GET_CODE (x_addr) != VALUE
2605 && GET_CODE (mem_addr) == VALUE
2606 && reg_mentioned_p (mem_addr, x_addr))))
2608 x_addr = get_addr (x_addr);
2609 if (!mem_canonicalized)
2610 mem_addr = get_addr (mem_addr);
2614 base = find_base_term (mem_addr);
2615 if (! writep
2616 && base
2617 && (GET_CODE (base) == LABEL_REF
2618 || (GET_CODE (base) == SYMBOL_REF
2619 && CONSTANT_POOL_ADDRESS_P (base))))
2620 return 0;
2622 rtx x_base = find_base_term (x_addr);
2623 if (! base_alias_check (x_addr, x_base, mem_addr, base, GET_MODE (x),
2624 GET_MODE (mem)))
2625 return 0;
2627 if (!x_canonicalized)
2629 x_addr = canon_rtx (x_addr);
2630 x_mode = GET_MODE (x);
2632 if (!mem_canonicalized)
2633 mem_addr = canon_rtx (mem_addr);
2635 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2636 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2637 return ret;
2639 if (nonoverlapping_memrefs_p (x, mem, false))
2640 return 0;
2642 return rtx_refs_may_alias_p (x, mem, false);
2645 /* Anti dependence: X is written after read in MEM takes place. */
2648 anti_dependence (const_rtx mem, const_rtx x)
2650 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2651 /*mem_canonicalized=*/false,
2652 /*x_canonicalized*/false, /*writep=*/false);
2655 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2656 Also, consider X in X_MODE (which might be from an enclosing
2657 STRICT_LOW_PART / ZERO_EXTRACT).
2658 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2661 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2662 const_rtx x, enum machine_mode x_mode, rtx x_addr)
2664 return write_dependence_p (mem, x, x_mode, x_addr,
2665 mem_canonicalized, /*x_canonicalized=*/true,
2666 /*writep=*/false);
2669 /* Output dependence: X is written after store in MEM takes place. */
2672 output_dependence (const_rtx mem, const_rtx x)
2674 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2675 /*mem_canonicalized=*/false,
2676 /*x_canonicalized*/false, /*writep=*/true);
2681 /* Check whether X may be aliased with MEM. Don't do offset-based
2682 memory disambiguation & TBAA. */
2684 may_alias_p (const_rtx mem, const_rtx x)
2686 rtx x_addr, mem_addr;
2688 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2689 return 1;
2691 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2692 This is used in epilogue deallocation functions. */
2693 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2694 return 1;
2695 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2696 return 1;
2697 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2698 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2699 return 1;
2701 /* Read-only memory is by definition never modified, and therefore can't
2702 conflict with anything. We don't expect to find read-only set on MEM,
2703 but stupid user tricks can produce them, so don't die. */
2704 if (MEM_READONLY_P (x))
2705 return 0;
2707 /* If we have MEMs referring to different address spaces (which can
2708 potentially overlap), we cannot easily tell from the addresses
2709 whether the references overlap. */
2710 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2711 return 1;
2713 x_addr = XEXP (x, 0);
2714 mem_addr = XEXP (mem, 0);
2715 if (!((GET_CODE (x_addr) == VALUE
2716 && GET_CODE (mem_addr) != VALUE
2717 && reg_mentioned_p (x_addr, mem_addr))
2718 || (GET_CODE (x_addr) != VALUE
2719 && GET_CODE (mem_addr) == VALUE
2720 && reg_mentioned_p (mem_addr, x_addr))))
2722 x_addr = get_addr (x_addr);
2723 mem_addr = get_addr (mem_addr);
2726 rtx x_base = find_base_term (x_addr);
2727 rtx mem_base = find_base_term (mem_addr);
2728 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2729 GET_MODE (x), GET_MODE (mem_addr)))
2730 return 0;
2732 x_addr = canon_rtx (x_addr);
2733 mem_addr = canon_rtx (mem_addr);
2735 if (nonoverlapping_memrefs_p (mem, x, true))
2736 return 0;
2738 /* TBAA not valid for loop_invarint */
2739 return rtx_refs_may_alias_p (x, mem, false);
2742 void
2743 init_alias_target (void)
2745 int i;
2747 if (!arg_base_value)
2748 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2750 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2752 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2753 /* Check whether this register can hold an incoming pointer
2754 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2755 numbers, so translate if necessary due to register windows. */
2756 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2757 && HARD_REGNO_MODE_OK (i, Pmode))
2758 static_reg_base_value[i] = arg_base_value;
2760 static_reg_base_value[STACK_POINTER_REGNUM]
2761 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2762 static_reg_base_value[ARG_POINTER_REGNUM]
2763 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2764 static_reg_base_value[FRAME_POINTER_REGNUM]
2765 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2766 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2767 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2768 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2769 #endif
2772 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2773 to be memory reference. */
2774 static bool memory_modified;
2775 static void
2776 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2778 if (MEM_P (x))
2780 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2781 memory_modified = true;
2786 /* Return true when INSN possibly modify memory contents of MEM
2787 (i.e. address can be modified). */
2788 bool
2789 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2791 if (!INSN_P (insn))
2792 return false;
2793 memory_modified = false;
2794 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2795 return memory_modified;
2798 /* Return TRUE if the destination of a set is rtx identical to
2799 ITEM. */
2800 static inline bool
2801 set_dest_equal_p (const_rtx set, const_rtx item)
2803 rtx dest = SET_DEST (set);
2804 return rtx_equal_p (dest, item);
2807 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2808 *DEFINITELY* modify the memory contents of MEM. */
2809 bool
2810 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2812 if (!INSN_P (insn))
2813 return false;
2814 insn = PATTERN (insn);
2815 if (GET_CODE (insn) == SET)
2816 return set_dest_equal_p (insn, mem);
2817 else if (GET_CODE (insn) == PARALLEL)
2819 int i;
2820 for (i = 0; i < XVECLEN (insn, 0); i++)
2822 rtx sub = XVECEXP (insn, 0, i);
2823 if (GET_CODE (sub) == SET
2824 && set_dest_equal_p (sub, mem))
2825 return true;
2828 return false;
2831 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2832 array. */
2834 void
2835 init_alias_analysis (void)
2837 unsigned int maxreg = max_reg_num ();
2838 int changed, pass;
2839 int i;
2840 unsigned int ui;
2841 rtx insn, val;
2842 int rpo_cnt;
2843 int *rpo;
2845 timevar_push (TV_ALIAS_ANALYSIS);
2847 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
2848 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
2849 bitmap_clear (reg_known_equiv_p);
2851 /* If we have memory allocated from the previous run, use it. */
2852 if (old_reg_base_value)
2853 reg_base_value = old_reg_base_value;
2855 if (reg_base_value)
2856 reg_base_value->truncate (0);
2858 vec_safe_grow_cleared (reg_base_value, maxreg);
2860 new_reg_base_value = XNEWVEC (rtx, maxreg);
2861 reg_seen = sbitmap_alloc (maxreg);
2863 /* The basic idea is that each pass through this loop will use the
2864 "constant" information from the previous pass to propagate alias
2865 information through another level of assignments.
2867 The propagation is done on the CFG in reverse post-order, to propagate
2868 things forward as far as possible in each iteration.
2870 This could get expensive if the assignment chains are long. Maybe
2871 we should throttle the number of iterations, possibly based on
2872 the optimization level or flag_expensive_optimizations.
2874 We could propagate more information in the first pass by making use
2875 of DF_REG_DEF_COUNT to determine immediately that the alias information
2876 for a pseudo is "constant".
2878 A program with an uninitialized variable can cause an infinite loop
2879 here. Instead of doing a full dataflow analysis to detect such problems
2880 we just cap the number of iterations for the loop.
2882 The state of the arrays for the set chain in question does not matter
2883 since the program has undefined behavior. */
2885 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2886 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
2888 pass = 0;
2891 /* Assume nothing will change this iteration of the loop. */
2892 changed = 0;
2894 /* We want to assign the same IDs each iteration of this loop, so
2895 start counting from one each iteration of the loop. */
2896 unique_id = 1;
2898 /* We're at the start of the function each iteration through the
2899 loop, so we're copying arguments. */
2900 copying_arguments = true;
2902 /* Wipe the potential alias information clean for this pass. */
2903 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2905 /* Wipe the reg_seen array clean. */
2906 bitmap_clear (reg_seen);
2908 /* Initialize the alias information for this pass. */
2909 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2910 if (static_reg_base_value[i])
2912 new_reg_base_value[i] = static_reg_base_value[i];
2913 bitmap_set_bit (reg_seen, i);
2916 /* Walk the insns adding values to the new_reg_base_value array. */
2917 for (i = 0; i < rpo_cnt; i++)
2919 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2920 FOR_BB_INSNS (bb, insn)
2922 if (NONDEBUG_INSN_P (insn))
2924 rtx note, set;
2926 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2927 /* The prologue/epilogue insns are not threaded onto the
2928 insn chain until after reload has completed. Thus,
2929 there is no sense wasting time checking if INSN is in
2930 the prologue/epilogue until after reload has completed. */
2931 if (reload_completed
2932 && prologue_epilogue_contains (insn))
2933 continue;
2934 #endif
2936 /* If this insn has a noalias note, process it, Otherwise,
2937 scan for sets. A simple set will have no side effects
2938 which could change the base value of any other register. */
2940 if (GET_CODE (PATTERN (insn)) == SET
2941 && REG_NOTES (insn) != 0
2942 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2943 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2944 else
2945 note_stores (PATTERN (insn), record_set, NULL);
2947 set = single_set (insn);
2949 if (set != 0
2950 && REG_P (SET_DEST (set))
2951 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2953 unsigned int regno = REGNO (SET_DEST (set));
2954 rtx src = SET_SRC (set);
2955 rtx t;
2957 note = find_reg_equal_equiv_note (insn);
2958 if (note && REG_NOTE_KIND (note) == REG_EQUAL
2959 && DF_REG_DEF_COUNT (regno) != 1)
2960 note = NULL_RTX;
2962 if (note != NULL_RTX
2963 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2964 && ! rtx_varies_p (XEXP (note, 0), 1)
2965 && ! reg_overlap_mentioned_p (SET_DEST (set),
2966 XEXP (note, 0)))
2968 set_reg_known_value (regno, XEXP (note, 0));
2969 set_reg_known_equiv_p (regno,
2970 REG_NOTE_KIND (note) == REG_EQUIV);
2972 else if (DF_REG_DEF_COUNT (regno) == 1
2973 && GET_CODE (src) == PLUS
2974 && REG_P (XEXP (src, 0))
2975 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2976 && CONST_INT_P (XEXP (src, 1)))
2978 t = plus_constant (GET_MODE (src), t,
2979 INTVAL (XEXP (src, 1)));
2980 set_reg_known_value (regno, t);
2981 set_reg_known_equiv_p (regno, false);
2983 else if (DF_REG_DEF_COUNT (regno) == 1
2984 && ! rtx_varies_p (src, 1))
2986 set_reg_known_value (regno, src);
2987 set_reg_known_equiv_p (regno, false);
2991 else if (NOTE_P (insn)
2992 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
2993 copying_arguments = false;
2997 /* Now propagate values from new_reg_base_value to reg_base_value. */
2998 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3000 for (ui = 0; ui < maxreg; ui++)
3002 if (new_reg_base_value[ui]
3003 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3004 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3006 (*reg_base_value)[ui] = new_reg_base_value[ui];
3007 changed = 1;
3011 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3012 XDELETEVEC (rpo);
3014 /* Fill in the remaining entries. */
3015 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3017 int regno = i + FIRST_PSEUDO_REGISTER;
3018 if (! val)
3019 set_reg_known_value (regno, regno_reg_rtx[regno]);
3022 /* Clean up. */
3023 free (new_reg_base_value);
3024 new_reg_base_value = 0;
3025 sbitmap_free (reg_seen);
3026 reg_seen = 0;
3027 timevar_pop (TV_ALIAS_ANALYSIS);
3030 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3031 Special API for var-tracking pass purposes. */
3033 void
3034 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3036 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3039 void
3040 end_alias_analysis (void)
3042 old_reg_base_value = reg_base_value;
3043 vec_free (reg_known_value);
3044 sbitmap_free (reg_known_equiv_p);
3047 #include "gt-alias.h"