Make std::vector<bool> meet C++11 allocator requirements.
[official-gcc.git] / gcc / alias.c
blobaa6ae418522a5fe9bee19a8e8994eab22464f270
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 Contributed by John Carr (jfc@mit.edu).
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "varasm.h"
28 #include "expr.h"
29 #include "tm_p.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "vec.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "alias.h"
38 #include "emit-rtl.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "diagnostic-core.h"
42 #include "cselib.h"
43 #include "splay-tree.h"
44 #include "langhooks.h"
45 #include "timevar.h"
46 #include "dumpfile.h"
47 #include "target.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "cfganal.h"
51 #include "predict.h"
52 #include "basic-block.h"
53 #include "df.h"
54 #include "tree-ssa-alias.h"
55 #include "internal-fn.h"
56 #include "gimple-expr.h"
57 #include "is-a.h"
58 #include "gimple.h"
59 #include "gimple-ssa.h"
60 #include "rtl-iter.h"
62 /* The aliasing API provided here solves related but different problems:
64 Say there exists (in c)
66 struct X {
67 struct Y y1;
68 struct Z z2;
69 } x1, *px1, *px2;
71 struct Y y2, *py;
72 struct Z z2, *pz;
75 py = &x1.y1;
76 px2 = &x1;
78 Consider the four questions:
80 Can a store to x1 interfere with px2->y1?
81 Can a store to x1 interfere with px2->z2?
82 Can a store to x1 change the value pointed to by with py?
83 Can a store to x1 change the value pointed to by with pz?
85 The answer to these questions can be yes, yes, yes, and maybe.
87 The first two questions can be answered with a simple examination
88 of the type system. If structure X contains a field of type Y then
89 a store through a pointer to an X can overwrite any field that is
90 contained (recursively) in an X (unless we know that px1 != px2).
92 The last two questions can be solved in the same way as the first
93 two questions but this is too conservative. The observation is
94 that in some cases we can know which (if any) fields are addressed
95 and if those addresses are used in bad ways. This analysis may be
96 language specific. In C, arbitrary operations may be applied to
97 pointers. However, there is some indication that this may be too
98 conservative for some C++ types.
100 The pass ipa-type-escape does this analysis for the types whose
101 instances do not escape across the compilation boundary.
103 Historically in GCC, these two problems were combined and a single
104 data structure that was used to represent the solution to these
105 problems. We now have two similar but different data structures,
106 The data structure to solve the last two questions is similar to
107 the first, but does not contain the fields whose address are never
108 taken. For types that do escape the compilation unit, the data
109 structures will have identical information.
112 /* The alias sets assigned to MEMs assist the back-end in determining
113 which MEMs can alias which other MEMs. In general, two MEMs in
114 different alias sets cannot alias each other, with one important
115 exception. Consider something like:
117 struct S { int i; double d; };
119 a store to an `S' can alias something of either type `int' or type
120 `double'. (However, a store to an `int' cannot alias a `double'
121 and vice versa.) We indicate this via a tree structure that looks
122 like:
123 struct S
126 |/_ _\|
127 int double
129 (The arrows are directed and point downwards.)
130 In this situation we say the alias set for `struct S' is the
131 `superset' and that those for `int' and `double' are `subsets'.
133 To see whether two alias sets can point to the same memory, we must
134 see if either alias set is a subset of the other. We need not trace
135 past immediate descendants, however, since we propagate all
136 grandchildren up one level.
138 Alias set zero is implicitly a superset of all other alias sets.
139 However, this is no actual entry for alias set zero. It is an
140 error to attempt to explicitly construct a subset of zero. */
142 struct GTY(()) alias_set_entry_d {
143 /* The alias set number, as stored in MEM_ALIAS_SET. */
144 alias_set_type alias_set;
146 /* Nonzero if would have a child of zero: this effectively makes this
147 alias set the same as alias set zero. */
148 int has_zero_child;
150 /* The children of the alias set. These are not just the immediate
151 children, but, in fact, all descendants. So, if we have:
153 struct T { struct S s; float f; }
155 continuing our example above, the children here will be all of
156 `int', `double', `float', and `struct S'. */
157 splay_tree GTY((param1_is (int), param2_is (int))) children;
159 typedef struct alias_set_entry_d *alias_set_entry;
161 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
162 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
163 static void record_set (rtx, const_rtx, void *);
164 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
165 machine_mode);
166 static rtx find_base_value (rtx);
167 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
168 static int insert_subset_children (splay_tree_node, void*);
169 static alias_set_entry get_alias_set_entry (alias_set_type);
170 static tree decl_for_component_ref (tree);
171 static int write_dependence_p (const_rtx,
172 const_rtx, machine_mode, rtx,
173 bool, bool, bool);
175 static void memory_modified_1 (rtx, const_rtx, void *);
177 /* Set up all info needed to perform alias analysis on memory references. */
179 /* Returns the size in bytes of the mode of X. */
180 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
182 /* Cap the number of passes we make over the insns propagating alias
183 information through set chains.
184 ??? 10 is a completely arbitrary choice. This should be based on the
185 maximum loop depth in the CFG, but we do not have this information
186 available (even if current_loops _is_ available). */
187 #define MAX_ALIAS_LOOP_PASSES 10
189 /* reg_base_value[N] gives an address to which register N is related.
190 If all sets after the first add or subtract to the current value
191 or otherwise modify it so it does not point to a different top level
192 object, reg_base_value[N] is equal to the address part of the source
193 of the first set.
195 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
196 expressions represent three types of base:
198 1. incoming arguments. There is just one ADDRESS to represent all
199 arguments, since we do not know at this level whether accesses
200 based on different arguments can alias. The ADDRESS has id 0.
202 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
203 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
204 Each of these rtxes has a separate ADDRESS associated with it,
205 each with a negative id.
207 GCC is (and is required to be) precise in which register it
208 chooses to access a particular region of stack. We can therefore
209 assume that accesses based on one of these rtxes do not alias
210 accesses based on another of these rtxes.
212 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
213 Each such piece of memory has a separate ADDRESS associated
214 with it, each with an id greater than 0.
216 Accesses based on one ADDRESS do not alias accesses based on other
217 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
218 alias globals either; the ADDRESSes have Pmode to indicate this.
219 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
220 indicate this. */
222 static GTY(()) vec<rtx, va_gc> *reg_base_value;
223 static rtx *new_reg_base_value;
225 /* The single VOIDmode ADDRESS that represents all argument bases.
226 It has id 0. */
227 static GTY(()) rtx arg_base_value;
229 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
230 static int unique_id;
232 /* We preserve the copy of old array around to avoid amount of garbage
233 produced. About 8% of garbage produced were attributed to this
234 array. */
235 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
237 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
238 registers. */
239 #define UNIQUE_BASE_VALUE_SP -1
240 #define UNIQUE_BASE_VALUE_ARGP -2
241 #define UNIQUE_BASE_VALUE_FP -3
242 #define UNIQUE_BASE_VALUE_HFP -4
244 #define static_reg_base_value \
245 (this_target_rtl->x_static_reg_base_value)
247 #define REG_BASE_VALUE(X) \
248 (REGNO (X) < vec_safe_length (reg_base_value) \
249 ? (*reg_base_value)[REGNO (X)] : 0)
251 /* Vector indexed by N giving the initial (unchanging) value known for
252 pseudo-register N. This vector is initialized in init_alias_analysis,
253 and does not change until end_alias_analysis is called. */
254 static GTY(()) vec<rtx, va_gc> *reg_known_value;
256 /* Vector recording for each reg_known_value whether it is due to a
257 REG_EQUIV note. Future passes (viz., reload) may replace the
258 pseudo with the equivalent expression and so we account for the
259 dependences that would be introduced if that happens.
261 The REG_EQUIV notes created in assign_parms may mention the arg
262 pointer, and there are explicit insns in the RTL that modify the
263 arg pointer. Thus we must ensure that such insns don't get
264 scheduled across each other because that would invalidate the
265 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
266 wrong, but solving the problem in the scheduler will likely give
267 better code, so we do it here. */
268 static sbitmap reg_known_equiv_p;
270 /* True when scanning insns from the start of the rtl to the
271 NOTE_INSN_FUNCTION_BEG note. */
272 static bool copying_arguments;
275 /* The splay-tree used to store the various alias set entries. */
276 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
278 /* Build a decomposed reference object for querying the alias-oracle
279 from the MEM rtx and store it in *REF.
280 Returns false if MEM is not suitable for the alias-oracle. */
282 static bool
283 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
285 tree expr = MEM_EXPR (mem);
286 tree base;
288 if (!expr)
289 return false;
291 ao_ref_init (ref, expr);
293 /* Get the base of the reference and see if we have to reject or
294 adjust it. */
295 base = ao_ref_base (ref);
296 if (base == NULL_TREE)
297 return false;
299 /* The tree oracle doesn't like bases that are neither decls
300 nor indirect references of SSA names. */
301 if (!(DECL_P (base)
302 || (TREE_CODE (base) == MEM_REF
303 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
304 || (TREE_CODE (base) == TARGET_MEM_REF
305 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
306 return false;
308 /* If this is a reference based on a partitioned decl replace the
309 base with a MEM_REF of the pointer representative we
310 created during stack slot partitioning. */
311 if (TREE_CODE (base) == VAR_DECL
312 && ! is_global_var (base)
313 && cfun->gimple_df->decls_to_pointers != NULL)
315 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
316 if (namep)
317 ref->base = build_simple_mem_ref (*namep);
320 ref->ref_alias_set = MEM_ALIAS_SET (mem);
322 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
323 is conservative, so trust it. */
324 if (!MEM_OFFSET_KNOWN_P (mem)
325 || !MEM_SIZE_KNOWN_P (mem))
326 return true;
328 /* If the base decl is a parameter we can have negative MEM_OFFSET in
329 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
330 here. */
331 if (MEM_OFFSET (mem) < 0
332 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
333 return true;
335 /* Otherwise continue and refine size and offset we got from analyzing
336 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
338 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
339 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
341 /* The MEM may extend into adjacent fields, so adjust max_size if
342 necessary. */
343 if (ref->max_size != -1
344 && ref->size > ref->max_size)
345 ref->max_size = ref->size;
347 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
348 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
349 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
350 && (ref->offset < 0
351 || (DECL_P (ref->base)
352 && (DECL_SIZE (ref->base) == NULL_TREE
353 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
354 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
355 ref->offset + ref->size)))))
356 return false;
358 return true;
361 /* Query the alias-oracle on whether the two memory rtx X and MEM may
362 alias. If TBAA_P is set also apply TBAA. Returns true if the
363 two rtxen may alias, false otherwise. */
365 static bool
366 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
368 ao_ref ref1, ref2;
370 if (!ao_ref_from_mem (&ref1, x)
371 || !ao_ref_from_mem (&ref2, mem))
372 return true;
374 return refs_may_alias_p_1 (&ref1, &ref2,
375 tbaa_p
376 && MEM_ALIAS_SET (x) != 0
377 && MEM_ALIAS_SET (mem) != 0);
380 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
381 such an entry, or NULL otherwise. */
383 static inline alias_set_entry
384 get_alias_set_entry (alias_set_type alias_set)
386 return (*alias_sets)[alias_set];
389 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
390 the two MEMs cannot alias each other. */
392 static inline int
393 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
395 /* Perform a basic sanity check. Namely, that there are no alias sets
396 if we're not using strict aliasing. This helps to catch bugs
397 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
398 where a MEM is allocated in some way other than by the use of
399 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
400 use alias sets to indicate that spilled registers cannot alias each
401 other, we might need to remove this check. */
402 gcc_assert (flag_strict_aliasing
403 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
405 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
408 /* Insert the NODE into the splay tree given by DATA. Used by
409 record_alias_subset via splay_tree_foreach. */
411 static int
412 insert_subset_children (splay_tree_node node, void *data)
414 splay_tree_insert ((splay_tree) data, node->key, node->value);
416 return 0;
419 /* Return true if the first alias set is a subset of the second. */
421 bool
422 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
424 alias_set_entry ase;
426 /* Everything is a subset of the "aliases everything" set. */
427 if (set2 == 0)
428 return true;
430 /* Otherwise, check if set1 is a subset of set2. */
431 ase = get_alias_set_entry (set2);
432 if (ase != 0
433 && (ase->has_zero_child
434 || splay_tree_lookup (ase->children,
435 (splay_tree_key) set1)))
436 return true;
437 return false;
440 /* Return 1 if the two specified alias sets may conflict. */
443 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
445 alias_set_entry ase;
447 /* The easy case. */
448 if (alias_sets_must_conflict_p (set1, set2))
449 return 1;
451 /* See if the first alias set is a subset of the second. */
452 ase = get_alias_set_entry (set1);
453 if (ase != 0
454 && (ase->has_zero_child
455 || splay_tree_lookup (ase->children,
456 (splay_tree_key) set2)))
457 return 1;
459 /* Now do the same, but with the alias sets reversed. */
460 ase = get_alias_set_entry (set2);
461 if (ase != 0
462 && (ase->has_zero_child
463 || splay_tree_lookup (ase->children,
464 (splay_tree_key) set1)))
465 return 1;
467 /* The two alias sets are distinct and neither one is the
468 child of the other. Therefore, they cannot conflict. */
469 return 0;
472 /* Return 1 if the two specified alias sets will always conflict. */
475 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
477 if (set1 == 0 || set2 == 0 || set1 == set2)
478 return 1;
480 return 0;
483 /* Return 1 if any MEM object of type T1 will always conflict (using the
484 dependency routines in this file) with any MEM object of type T2.
485 This is used when allocating temporary storage. If T1 and/or T2 are
486 NULL_TREE, it means we know nothing about the storage. */
489 objects_must_conflict_p (tree t1, tree t2)
491 alias_set_type set1, set2;
493 /* If neither has a type specified, we don't know if they'll conflict
494 because we may be using them to store objects of various types, for
495 example the argument and local variables areas of inlined functions. */
496 if (t1 == 0 && t2 == 0)
497 return 0;
499 /* If they are the same type, they must conflict. */
500 if (t1 == t2
501 /* Likewise if both are volatile. */
502 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
503 return 1;
505 set1 = t1 ? get_alias_set (t1) : 0;
506 set2 = t2 ? get_alias_set (t2) : 0;
508 /* We can't use alias_sets_conflict_p because we must make sure
509 that every subtype of t1 will conflict with every subtype of
510 t2 for which a pair of subobjects of these respective subtypes
511 overlaps on the stack. */
512 return alias_sets_must_conflict_p (set1, set2);
515 /* Return the outermost parent of component present in the chain of
516 component references handled by get_inner_reference in T with the
517 following property:
518 - the component is non-addressable, or
519 - the parent has alias set zero,
520 or NULL_TREE if no such parent exists. In the former cases, the alias
521 set of this parent is the alias set that must be used for T itself. */
523 tree
524 component_uses_parent_alias_set_from (const_tree t)
526 const_tree found = NULL_TREE;
528 while (handled_component_p (t))
530 switch (TREE_CODE (t))
532 case COMPONENT_REF:
533 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
534 found = t;
535 break;
537 case ARRAY_REF:
538 case ARRAY_RANGE_REF:
539 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
540 found = t;
541 break;
543 case REALPART_EXPR:
544 case IMAGPART_EXPR:
545 break;
547 case BIT_FIELD_REF:
548 case VIEW_CONVERT_EXPR:
549 /* Bitfields and casts are never addressable. */
550 found = t;
551 break;
553 default:
554 gcc_unreachable ();
557 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
558 found = t;
560 t = TREE_OPERAND (t, 0);
563 if (found)
564 return TREE_OPERAND (found, 0);
566 return NULL_TREE;
570 /* Return whether the pointer-type T effective for aliasing may
571 access everything and thus the reference has to be assigned
572 alias-set zero. */
574 static bool
575 ref_all_alias_ptr_type_p (const_tree t)
577 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
578 || TYPE_REF_CAN_ALIAS_ALL (t));
581 /* Return the alias set for the memory pointed to by T, which may be
582 either a type or an expression. Return -1 if there is nothing
583 special about dereferencing T. */
585 static alias_set_type
586 get_deref_alias_set_1 (tree t)
588 /* All we care about is the type. */
589 if (! TYPE_P (t))
590 t = TREE_TYPE (t);
592 /* If we have an INDIRECT_REF via a void pointer, we don't
593 know anything about what that might alias. Likewise if the
594 pointer is marked that way. */
595 if (ref_all_alias_ptr_type_p (t))
596 return 0;
598 return -1;
601 /* Return the alias set for the memory pointed to by T, which may be
602 either a type or an expression. */
604 alias_set_type
605 get_deref_alias_set (tree t)
607 /* If we're not doing any alias analysis, just assume everything
608 aliases everything else. */
609 if (!flag_strict_aliasing)
610 return 0;
612 alias_set_type set = get_deref_alias_set_1 (t);
614 /* Fall back to the alias-set of the pointed-to type. */
615 if (set == -1)
617 if (! TYPE_P (t))
618 t = TREE_TYPE (t);
619 set = get_alias_set (TREE_TYPE (t));
622 return set;
625 /* Return the pointer-type relevant for TBAA purposes from the
626 memory reference tree *T or NULL_TREE in which case *T is
627 adjusted to point to the outermost component reference that
628 can be used for assigning an alias set. */
630 static tree
631 reference_alias_ptr_type_1 (tree *t)
633 tree inner;
635 /* Get the base object of the reference. */
636 inner = *t;
637 while (handled_component_p (inner))
639 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
640 the type of any component references that wrap it to
641 determine the alias-set. */
642 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
643 *t = TREE_OPERAND (inner, 0);
644 inner = TREE_OPERAND (inner, 0);
647 /* Handle pointer dereferences here, they can override the
648 alias-set. */
649 if (INDIRECT_REF_P (inner)
650 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
651 return TREE_TYPE (TREE_OPERAND (inner, 0));
652 else if (TREE_CODE (inner) == TARGET_MEM_REF)
653 return TREE_TYPE (TMR_OFFSET (inner));
654 else if (TREE_CODE (inner) == MEM_REF
655 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
656 return TREE_TYPE (TREE_OPERAND (inner, 1));
658 /* If the innermost reference is a MEM_REF that has a
659 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
660 using the memory access type for determining the alias-set. */
661 if (TREE_CODE (inner) == MEM_REF
662 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
663 != TYPE_MAIN_VARIANT
664 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
665 return TREE_TYPE (TREE_OPERAND (inner, 1));
667 /* Otherwise, pick up the outermost object that we could have
668 a pointer to. */
669 tree tem = component_uses_parent_alias_set_from (*t);
670 if (tem)
671 *t = tem;
673 return NULL_TREE;
676 /* Return the pointer-type relevant for TBAA purposes from the
677 gimple memory reference tree T. This is the type to be used for
678 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
679 and guarantees that get_alias_set will return the same alias
680 set for T and the replacement. */
682 tree
683 reference_alias_ptr_type (tree t)
685 tree ptype = reference_alias_ptr_type_1 (&t);
686 /* If there is a given pointer type for aliasing purposes, return it. */
687 if (ptype != NULL_TREE)
688 return ptype;
690 /* Otherwise build one from the outermost component reference we
691 may use. */
692 if (TREE_CODE (t) == MEM_REF
693 || TREE_CODE (t) == TARGET_MEM_REF)
694 return TREE_TYPE (TREE_OPERAND (t, 1));
695 else
696 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
699 /* Return whether the pointer-types T1 and T2 used to determine
700 two alias sets of two references will yield the same answer
701 from get_deref_alias_set. */
703 bool
704 alias_ptr_types_compatible_p (tree t1, tree t2)
706 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
707 return true;
709 if (ref_all_alias_ptr_type_p (t1)
710 || ref_all_alias_ptr_type_p (t2))
711 return false;
713 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
714 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
717 /* Return the alias set for T, which may be either a type or an
718 expression. Call language-specific routine for help, if needed. */
720 alias_set_type
721 get_alias_set (tree t)
723 alias_set_type set;
725 /* If we're not doing any alias analysis, just assume everything
726 aliases everything else. Also return 0 if this or its type is
727 an error. */
728 if (! flag_strict_aliasing || t == error_mark_node
729 || (! TYPE_P (t)
730 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
731 return 0;
733 /* We can be passed either an expression or a type. This and the
734 language-specific routine may make mutually-recursive calls to each other
735 to figure out what to do. At each juncture, we see if this is a tree
736 that the language may need to handle specially. First handle things that
737 aren't types. */
738 if (! TYPE_P (t))
740 /* Give the language a chance to do something with this tree
741 before we look at it. */
742 STRIP_NOPS (t);
743 set = lang_hooks.get_alias_set (t);
744 if (set != -1)
745 return set;
747 /* Get the alias pointer-type to use or the outermost object
748 that we could have a pointer to. */
749 tree ptype = reference_alias_ptr_type_1 (&t);
750 if (ptype != NULL)
751 return get_deref_alias_set (ptype);
753 /* If we've already determined the alias set for a decl, just return
754 it. This is necessary for C++ anonymous unions, whose component
755 variables don't look like union members (boo!). */
756 if (TREE_CODE (t) == VAR_DECL
757 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
758 return MEM_ALIAS_SET (DECL_RTL (t));
760 /* Now all we care about is the type. */
761 t = TREE_TYPE (t);
764 /* Variant qualifiers don't affect the alias set, so get the main
765 variant. */
766 t = TYPE_MAIN_VARIANT (t);
768 /* Always use the canonical type as well. If this is a type that
769 requires structural comparisons to identify compatible types
770 use alias set zero. */
771 if (TYPE_STRUCTURAL_EQUALITY_P (t))
773 /* Allow the language to specify another alias set for this
774 type. */
775 set = lang_hooks.get_alias_set (t);
776 if (set != -1)
777 return set;
778 return 0;
781 t = TYPE_CANONICAL (t);
783 /* The canonical type should not require structural equality checks. */
784 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
786 /* If this is a type with a known alias set, return it. */
787 if (TYPE_ALIAS_SET_KNOWN_P (t))
788 return TYPE_ALIAS_SET (t);
790 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
791 if (!COMPLETE_TYPE_P (t))
793 /* For arrays with unknown size the conservative answer is the
794 alias set of the element type. */
795 if (TREE_CODE (t) == ARRAY_TYPE)
796 return get_alias_set (TREE_TYPE (t));
798 /* But return zero as a conservative answer for incomplete types. */
799 return 0;
802 /* See if the language has special handling for this type. */
803 set = lang_hooks.get_alias_set (t);
804 if (set != -1)
805 return set;
807 /* There are no objects of FUNCTION_TYPE, so there's no point in
808 using up an alias set for them. (There are, of course, pointers
809 and references to functions, but that's different.) */
810 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
811 set = 0;
813 /* Unless the language specifies otherwise, let vector types alias
814 their components. This avoids some nasty type punning issues in
815 normal usage. And indeed lets vectors be treated more like an
816 array slice. */
817 else if (TREE_CODE (t) == VECTOR_TYPE)
818 set = get_alias_set (TREE_TYPE (t));
820 /* Unless the language specifies otherwise, treat array types the
821 same as their components. This avoids the asymmetry we get
822 through recording the components. Consider accessing a
823 character(kind=1) through a reference to a character(kind=1)[1:1].
824 Or consider if we want to assign integer(kind=4)[0:D.1387] and
825 integer(kind=4)[4] the same alias set or not.
826 Just be pragmatic here and make sure the array and its element
827 type get the same alias set assigned. */
828 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
829 set = get_alias_set (TREE_TYPE (t));
831 /* From the former common C and C++ langhook implementation:
833 Unfortunately, there is no canonical form of a pointer type.
834 In particular, if we have `typedef int I', then `int *', and
835 `I *' are different types. So, we have to pick a canonical
836 representative. We do this below.
838 Technically, this approach is actually more conservative that
839 it needs to be. In particular, `const int *' and `int *'
840 should be in different alias sets, according to the C and C++
841 standard, since their types are not the same, and so,
842 technically, an `int **' and `const int **' cannot point at
843 the same thing.
845 But, the standard is wrong. In particular, this code is
846 legal C++:
848 int *ip;
849 int **ipp = &ip;
850 const int* const* cipp = ipp;
851 And, it doesn't make sense for that to be legal unless you
852 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
853 the pointed-to types. This issue has been reported to the
854 C++ committee.
856 In addition to the above canonicalization issue, with LTO
857 we should also canonicalize `T (*)[]' to `T *' avoiding
858 alias issues with pointer-to element types and pointer-to
859 array types.
861 Likewise we need to deal with the situation of incomplete
862 pointed-to types and make `*(struct X **)&a' and
863 `*(struct X {} **)&a' alias. Otherwise we will have to
864 guarantee that all pointer-to incomplete type variants
865 will be replaced by pointer-to complete type variants if
866 they are available.
868 With LTO the convenient situation of using `void *' to
869 access and store any pointer type will also become
870 more apparent (and `void *' is just another pointer-to
871 incomplete type). Assigning alias-set zero to `void *'
872 and all pointer-to incomplete types is a not appealing
873 solution. Assigning an effective alias-set zero only
874 affecting pointers might be - by recording proper subset
875 relationships of all pointer alias-sets.
877 Pointer-to function types are another grey area which
878 needs caution. Globbing them all into one alias-set
879 or the above effective zero set would work.
881 For now just assign the same alias-set to all pointers.
882 That's simple and avoids all the above problems. */
883 else if (POINTER_TYPE_P (t)
884 && t != ptr_type_node)
885 set = get_alias_set (ptr_type_node);
887 /* Otherwise make a new alias set for this type. */
888 else
890 /* Each canonical type gets its own alias set, so canonical types
891 shouldn't form a tree. It doesn't really matter for types
892 we handle specially above, so only check it where it possibly
893 would result in a bogus alias set. */
894 gcc_checking_assert (TYPE_CANONICAL (t) == t);
896 set = new_alias_set ();
899 TYPE_ALIAS_SET (t) = set;
901 /* If this is an aggregate type or a complex type, we must record any
902 component aliasing information. */
903 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
904 record_component_aliases (t);
906 return set;
909 /* Return a brand-new alias set. */
911 alias_set_type
912 new_alias_set (void)
914 if (flag_strict_aliasing)
916 if (alias_sets == 0)
917 vec_safe_push (alias_sets, (alias_set_entry) 0);
918 vec_safe_push (alias_sets, (alias_set_entry) 0);
919 return alias_sets->length () - 1;
921 else
922 return 0;
925 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
926 not everything that aliases SUPERSET also aliases SUBSET. For example,
927 in C, a store to an `int' can alias a load of a structure containing an
928 `int', and vice versa. But it can't alias a load of a 'double' member
929 of the same structure. Here, the structure would be the SUPERSET and
930 `int' the SUBSET. This relationship is also described in the comment at
931 the beginning of this file.
933 This function should be called only once per SUPERSET/SUBSET pair.
935 It is illegal for SUPERSET to be zero; everything is implicitly a
936 subset of alias set zero. */
938 void
939 record_alias_subset (alias_set_type superset, alias_set_type subset)
941 alias_set_entry superset_entry;
942 alias_set_entry subset_entry;
944 /* It is possible in complex type situations for both sets to be the same,
945 in which case we can ignore this operation. */
946 if (superset == subset)
947 return;
949 gcc_assert (superset);
951 superset_entry = get_alias_set_entry (superset);
952 if (superset_entry == 0)
954 /* Create an entry for the SUPERSET, so that we have a place to
955 attach the SUBSET. */
956 superset_entry = ggc_cleared_alloc<alias_set_entry_d> ();
957 superset_entry->alias_set = superset;
958 superset_entry->children
959 = splay_tree_new_ggc (splay_tree_compare_ints,
960 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s,
961 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s);
962 superset_entry->has_zero_child = 0;
963 (*alias_sets)[superset] = superset_entry;
966 if (subset == 0)
967 superset_entry->has_zero_child = 1;
968 else
970 subset_entry = get_alias_set_entry (subset);
971 /* If there is an entry for the subset, enter all of its children
972 (if they are not already present) as children of the SUPERSET. */
973 if (subset_entry)
975 if (subset_entry->has_zero_child)
976 superset_entry->has_zero_child = 1;
978 splay_tree_foreach (subset_entry->children, insert_subset_children,
979 superset_entry->children);
982 /* Enter the SUBSET itself as a child of the SUPERSET. */
983 splay_tree_insert (superset_entry->children,
984 (splay_tree_key) subset, 0);
988 /* Record that component types of TYPE, if any, are part of that type for
989 aliasing purposes. For record types, we only record component types
990 for fields that are not marked non-addressable. For array types, we
991 only record the component type if it is not marked non-aliased. */
993 void
994 record_component_aliases (tree type)
996 alias_set_type superset = get_alias_set (type);
997 tree field;
999 if (superset == 0)
1000 return;
1002 switch (TREE_CODE (type))
1004 case RECORD_TYPE:
1005 case UNION_TYPE:
1006 case QUAL_UNION_TYPE:
1007 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1008 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1009 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1010 break;
1012 case COMPLEX_TYPE:
1013 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1014 break;
1016 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1017 element type. */
1019 default:
1020 break;
1024 /* Allocate an alias set for use in storing and reading from the varargs
1025 spill area. */
1027 static GTY(()) alias_set_type varargs_set = -1;
1029 alias_set_type
1030 get_varargs_alias_set (void)
1032 #if 1
1033 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1034 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1035 consistently use the varargs alias set for loads from the varargs
1036 area. So don't use it anywhere. */
1037 return 0;
1038 #else
1039 if (varargs_set == -1)
1040 varargs_set = new_alias_set ();
1042 return varargs_set;
1043 #endif
1046 /* Likewise, but used for the fixed portions of the frame, e.g., register
1047 save areas. */
1049 static GTY(()) alias_set_type frame_set = -1;
1051 alias_set_type
1052 get_frame_alias_set (void)
1054 if (frame_set == -1)
1055 frame_set = new_alias_set ();
1057 return frame_set;
1060 /* Create a new, unique base with id ID. */
1062 static rtx
1063 unique_base_value (HOST_WIDE_INT id)
1065 return gen_rtx_ADDRESS (Pmode, id);
1068 /* Return true if accesses based on any other base value cannot alias
1069 those based on X. */
1071 static bool
1072 unique_base_value_p (rtx x)
1074 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1077 /* Return true if X is known to be a base value. */
1079 static bool
1080 known_base_value_p (rtx x)
1082 switch (GET_CODE (x))
1084 case LABEL_REF:
1085 case SYMBOL_REF:
1086 return true;
1088 case ADDRESS:
1089 /* Arguments may or may not be bases; we don't know for sure. */
1090 return GET_MODE (x) != VOIDmode;
1092 default:
1093 return false;
1097 /* Inside SRC, the source of a SET, find a base address. */
1099 static rtx
1100 find_base_value (rtx src)
1102 unsigned int regno;
1104 #if defined (FIND_BASE_TERM)
1105 /* Try machine-dependent ways to find the base term. */
1106 src = FIND_BASE_TERM (src);
1107 #endif
1109 switch (GET_CODE (src))
1111 case SYMBOL_REF:
1112 case LABEL_REF:
1113 return src;
1115 case REG:
1116 regno = REGNO (src);
1117 /* At the start of a function, argument registers have known base
1118 values which may be lost later. Returning an ADDRESS
1119 expression here allows optimization based on argument values
1120 even when the argument registers are used for other purposes. */
1121 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1122 return new_reg_base_value[regno];
1124 /* If a pseudo has a known base value, return it. Do not do this
1125 for non-fixed hard regs since it can result in a circular
1126 dependency chain for registers which have values at function entry.
1128 The test above is not sufficient because the scheduler may move
1129 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1130 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1131 && regno < vec_safe_length (reg_base_value))
1133 /* If we're inside init_alias_analysis, use new_reg_base_value
1134 to reduce the number of relaxation iterations. */
1135 if (new_reg_base_value && new_reg_base_value[regno]
1136 && DF_REG_DEF_COUNT (regno) == 1)
1137 return new_reg_base_value[regno];
1139 if ((*reg_base_value)[regno])
1140 return (*reg_base_value)[regno];
1143 return 0;
1145 case MEM:
1146 /* Check for an argument passed in memory. Only record in the
1147 copying-arguments block; it is too hard to track changes
1148 otherwise. */
1149 if (copying_arguments
1150 && (XEXP (src, 0) == arg_pointer_rtx
1151 || (GET_CODE (XEXP (src, 0)) == PLUS
1152 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1153 return arg_base_value;
1154 return 0;
1156 case CONST:
1157 src = XEXP (src, 0);
1158 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1159 break;
1161 /* ... fall through ... */
1163 case PLUS:
1164 case MINUS:
1166 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1168 /* If either operand is a REG that is a known pointer, then it
1169 is the base. */
1170 if (REG_P (src_0) && REG_POINTER (src_0))
1171 return find_base_value (src_0);
1172 if (REG_P (src_1) && REG_POINTER (src_1))
1173 return find_base_value (src_1);
1175 /* If either operand is a REG, then see if we already have
1176 a known value for it. */
1177 if (REG_P (src_0))
1179 temp = find_base_value (src_0);
1180 if (temp != 0)
1181 src_0 = temp;
1184 if (REG_P (src_1))
1186 temp = find_base_value (src_1);
1187 if (temp!= 0)
1188 src_1 = temp;
1191 /* If either base is named object or a special address
1192 (like an argument or stack reference), then use it for the
1193 base term. */
1194 if (src_0 != 0 && known_base_value_p (src_0))
1195 return src_0;
1197 if (src_1 != 0 && known_base_value_p (src_1))
1198 return src_1;
1200 /* Guess which operand is the base address:
1201 If either operand is a symbol, then it is the base. If
1202 either operand is a CONST_INT, then the other is the base. */
1203 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1204 return find_base_value (src_0);
1205 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1206 return find_base_value (src_1);
1208 return 0;
1211 case LO_SUM:
1212 /* The standard form is (lo_sum reg sym) so look only at the
1213 second operand. */
1214 return find_base_value (XEXP (src, 1));
1216 case AND:
1217 /* If the second operand is constant set the base
1218 address to the first operand. */
1219 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1220 return find_base_value (XEXP (src, 0));
1221 return 0;
1223 case TRUNCATE:
1224 /* As we do not know which address space the pointer is referring to, we can
1225 handle this only if the target does not support different pointer or
1226 address modes depending on the address space. */
1227 if (!target_default_pointer_address_modes_p ())
1228 break;
1229 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1230 break;
1231 /* Fall through. */
1232 case HIGH:
1233 case PRE_INC:
1234 case PRE_DEC:
1235 case POST_INC:
1236 case POST_DEC:
1237 case PRE_MODIFY:
1238 case POST_MODIFY:
1239 return find_base_value (XEXP (src, 0));
1241 case ZERO_EXTEND:
1242 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1243 /* As we do not know which address space the pointer is referring to, we can
1244 handle this only if the target does not support different pointer or
1245 address modes depending on the address space. */
1246 if (!target_default_pointer_address_modes_p ())
1247 break;
1250 rtx temp = find_base_value (XEXP (src, 0));
1252 if (temp != 0 && CONSTANT_P (temp))
1253 temp = convert_memory_address (Pmode, temp);
1255 return temp;
1258 default:
1259 break;
1262 return 0;
1265 /* Called from init_alias_analysis indirectly through note_stores,
1266 or directly if DEST is a register with a REG_NOALIAS note attached.
1267 SET is null in the latter case. */
1269 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1270 register N has been set in this function. */
1271 static sbitmap reg_seen;
1273 static void
1274 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1276 unsigned regno;
1277 rtx src;
1278 int n;
1280 if (!REG_P (dest))
1281 return;
1283 regno = REGNO (dest);
1285 gcc_checking_assert (regno < reg_base_value->length ());
1287 /* If this spans multiple hard registers, then we must indicate that every
1288 register has an unusable value. */
1289 if (regno < FIRST_PSEUDO_REGISTER)
1290 n = hard_regno_nregs[regno][GET_MODE (dest)];
1291 else
1292 n = 1;
1293 if (n != 1)
1295 while (--n >= 0)
1297 bitmap_set_bit (reg_seen, regno + n);
1298 new_reg_base_value[regno + n] = 0;
1300 return;
1303 if (set)
1305 /* A CLOBBER wipes out any old value but does not prevent a previously
1306 unset register from acquiring a base address (i.e. reg_seen is not
1307 set). */
1308 if (GET_CODE (set) == CLOBBER)
1310 new_reg_base_value[regno] = 0;
1311 return;
1313 src = SET_SRC (set);
1315 else
1317 /* There's a REG_NOALIAS note against DEST. */
1318 if (bitmap_bit_p (reg_seen, regno))
1320 new_reg_base_value[regno] = 0;
1321 return;
1323 bitmap_set_bit (reg_seen, regno);
1324 new_reg_base_value[regno] = unique_base_value (unique_id++);
1325 return;
1328 /* If this is not the first set of REGNO, see whether the new value
1329 is related to the old one. There are two cases of interest:
1331 (1) The register might be assigned an entirely new value
1332 that has the same base term as the original set.
1334 (2) The set might be a simple self-modification that
1335 cannot change REGNO's base value.
1337 If neither case holds, reject the original base value as invalid.
1338 Note that the following situation is not detected:
1340 extern int x, y; int *p = &x; p += (&y-&x);
1342 ANSI C does not allow computing the difference of addresses
1343 of distinct top level objects. */
1344 if (new_reg_base_value[regno] != 0
1345 && find_base_value (src) != new_reg_base_value[regno])
1346 switch (GET_CODE (src))
1348 case LO_SUM:
1349 case MINUS:
1350 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1351 new_reg_base_value[regno] = 0;
1352 break;
1353 case PLUS:
1354 /* If the value we add in the PLUS is also a valid base value,
1355 this might be the actual base value, and the original value
1356 an index. */
1358 rtx other = NULL_RTX;
1360 if (XEXP (src, 0) == dest)
1361 other = XEXP (src, 1);
1362 else if (XEXP (src, 1) == dest)
1363 other = XEXP (src, 0);
1365 if (! other || find_base_value (other))
1366 new_reg_base_value[regno] = 0;
1367 break;
1369 case AND:
1370 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1371 new_reg_base_value[regno] = 0;
1372 break;
1373 default:
1374 new_reg_base_value[regno] = 0;
1375 break;
1377 /* If this is the first set of a register, record the value. */
1378 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1379 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1380 new_reg_base_value[regno] = find_base_value (src);
1382 bitmap_set_bit (reg_seen, regno);
1385 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1386 using hard registers with non-null REG_BASE_VALUE for renaming. */
1388 get_reg_base_value (unsigned int regno)
1390 return (*reg_base_value)[regno];
1393 /* If a value is known for REGNO, return it. */
1396 get_reg_known_value (unsigned int regno)
1398 if (regno >= FIRST_PSEUDO_REGISTER)
1400 regno -= FIRST_PSEUDO_REGISTER;
1401 if (regno < vec_safe_length (reg_known_value))
1402 return (*reg_known_value)[regno];
1404 return NULL;
1407 /* Set it. */
1409 static void
1410 set_reg_known_value (unsigned int regno, rtx val)
1412 if (regno >= FIRST_PSEUDO_REGISTER)
1414 regno -= FIRST_PSEUDO_REGISTER;
1415 if (regno < vec_safe_length (reg_known_value))
1416 (*reg_known_value)[regno] = val;
1420 /* Similarly for reg_known_equiv_p. */
1422 bool
1423 get_reg_known_equiv_p (unsigned int regno)
1425 if (regno >= FIRST_PSEUDO_REGISTER)
1427 regno -= FIRST_PSEUDO_REGISTER;
1428 if (regno < vec_safe_length (reg_known_value))
1429 return bitmap_bit_p (reg_known_equiv_p, regno);
1431 return false;
1434 static void
1435 set_reg_known_equiv_p (unsigned int regno, bool val)
1437 if (regno >= FIRST_PSEUDO_REGISTER)
1439 regno -= FIRST_PSEUDO_REGISTER;
1440 if (regno < vec_safe_length (reg_known_value))
1442 if (val)
1443 bitmap_set_bit (reg_known_equiv_p, regno);
1444 else
1445 bitmap_clear_bit (reg_known_equiv_p, regno);
1451 /* Returns a canonical version of X, from the point of view alias
1452 analysis. (For example, if X is a MEM whose address is a register,
1453 and the register has a known value (say a SYMBOL_REF), then a MEM
1454 whose address is the SYMBOL_REF is returned.) */
1457 canon_rtx (rtx x)
1459 /* Recursively look for equivalences. */
1460 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1462 rtx t = get_reg_known_value (REGNO (x));
1463 if (t == x)
1464 return x;
1465 if (t)
1466 return canon_rtx (t);
1469 if (GET_CODE (x) == PLUS)
1471 rtx x0 = canon_rtx (XEXP (x, 0));
1472 rtx x1 = canon_rtx (XEXP (x, 1));
1474 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1476 if (CONST_INT_P (x0))
1477 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1478 else if (CONST_INT_P (x1))
1479 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1480 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1484 /* This gives us much better alias analysis when called from
1485 the loop optimizer. Note we want to leave the original
1486 MEM alone, but need to return the canonicalized MEM with
1487 all the flags with their original values. */
1488 else if (MEM_P (x))
1489 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1491 return x;
1494 /* Return 1 if X and Y are identical-looking rtx's.
1495 Expect that X and Y has been already canonicalized.
1497 We use the data in reg_known_value above to see if two registers with
1498 different numbers are, in fact, equivalent. */
1500 static int
1501 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1503 int i;
1504 int j;
1505 enum rtx_code code;
1506 const char *fmt;
1508 if (x == 0 && y == 0)
1509 return 1;
1510 if (x == 0 || y == 0)
1511 return 0;
1513 if (x == y)
1514 return 1;
1516 code = GET_CODE (x);
1517 /* Rtx's of different codes cannot be equal. */
1518 if (code != GET_CODE (y))
1519 return 0;
1521 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1522 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1524 if (GET_MODE (x) != GET_MODE (y))
1525 return 0;
1527 /* Some RTL can be compared without a recursive examination. */
1528 switch (code)
1530 case REG:
1531 return REGNO (x) == REGNO (y);
1533 case LABEL_REF:
1534 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1536 case SYMBOL_REF:
1537 return XSTR (x, 0) == XSTR (y, 0);
1539 case ENTRY_VALUE:
1540 /* This is magic, don't go through canonicalization et al. */
1541 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1543 case VALUE:
1544 CASE_CONST_UNIQUE:
1545 /* Pointer equality guarantees equality for these nodes. */
1546 return 0;
1548 default:
1549 break;
1552 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1553 if (code == PLUS)
1554 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1555 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1556 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1557 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1558 /* For commutative operations, the RTX match if the operand match in any
1559 order. Also handle the simple binary and unary cases without a loop. */
1560 if (COMMUTATIVE_P (x))
1562 rtx xop0 = canon_rtx (XEXP (x, 0));
1563 rtx yop0 = canon_rtx (XEXP (y, 0));
1564 rtx yop1 = canon_rtx (XEXP (y, 1));
1566 return ((rtx_equal_for_memref_p (xop0, yop0)
1567 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1568 || (rtx_equal_for_memref_p (xop0, yop1)
1569 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1571 else if (NON_COMMUTATIVE_P (x))
1573 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1574 canon_rtx (XEXP (y, 0)))
1575 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1576 canon_rtx (XEXP (y, 1))));
1578 else if (UNARY_P (x))
1579 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1580 canon_rtx (XEXP (y, 0)));
1582 /* Compare the elements. If any pair of corresponding elements
1583 fail to match, return 0 for the whole things.
1585 Limit cases to types which actually appear in addresses. */
1587 fmt = GET_RTX_FORMAT (code);
1588 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1590 switch (fmt[i])
1592 case 'i':
1593 if (XINT (x, i) != XINT (y, i))
1594 return 0;
1595 break;
1597 case 'E':
1598 /* Two vectors must have the same length. */
1599 if (XVECLEN (x, i) != XVECLEN (y, i))
1600 return 0;
1602 /* And the corresponding elements must match. */
1603 for (j = 0; j < XVECLEN (x, i); j++)
1604 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1605 canon_rtx (XVECEXP (y, i, j))) == 0)
1606 return 0;
1607 break;
1609 case 'e':
1610 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1611 canon_rtx (XEXP (y, i))) == 0)
1612 return 0;
1613 break;
1615 /* This can happen for asm operands. */
1616 case 's':
1617 if (strcmp (XSTR (x, i), XSTR (y, i)))
1618 return 0;
1619 break;
1621 /* This can happen for an asm which clobbers memory. */
1622 case '0':
1623 break;
1625 /* It is believed that rtx's at this level will never
1626 contain anything but integers and other rtx's,
1627 except for within LABEL_REFs and SYMBOL_REFs. */
1628 default:
1629 gcc_unreachable ();
1632 return 1;
1635 static rtx
1636 find_base_term (rtx x)
1638 cselib_val *val;
1639 struct elt_loc_list *l, *f;
1640 rtx ret;
1642 #if defined (FIND_BASE_TERM)
1643 /* Try machine-dependent ways to find the base term. */
1644 x = FIND_BASE_TERM (x);
1645 #endif
1647 switch (GET_CODE (x))
1649 case REG:
1650 return REG_BASE_VALUE (x);
1652 case TRUNCATE:
1653 /* As we do not know which address space the pointer is referring to, we can
1654 handle this only if the target does not support different pointer or
1655 address modes depending on the address space. */
1656 if (!target_default_pointer_address_modes_p ())
1657 return 0;
1658 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1659 return 0;
1660 /* Fall through. */
1661 case HIGH:
1662 case PRE_INC:
1663 case PRE_DEC:
1664 case POST_INC:
1665 case POST_DEC:
1666 case PRE_MODIFY:
1667 case POST_MODIFY:
1668 return find_base_term (XEXP (x, 0));
1670 case ZERO_EXTEND:
1671 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1672 /* As we do not know which address space the pointer is referring to, we can
1673 handle this only if the target does not support different pointer or
1674 address modes depending on the address space. */
1675 if (!target_default_pointer_address_modes_p ())
1676 return 0;
1679 rtx temp = find_base_term (XEXP (x, 0));
1681 if (temp != 0 && CONSTANT_P (temp))
1682 temp = convert_memory_address (Pmode, temp);
1684 return temp;
1687 case VALUE:
1688 val = CSELIB_VAL_PTR (x);
1689 ret = NULL_RTX;
1691 if (!val)
1692 return ret;
1694 if (cselib_sp_based_value_p (val))
1695 return static_reg_base_value[STACK_POINTER_REGNUM];
1697 f = val->locs;
1698 /* Temporarily reset val->locs to avoid infinite recursion. */
1699 val->locs = NULL;
1701 for (l = f; l; l = l->next)
1702 if (GET_CODE (l->loc) == VALUE
1703 && CSELIB_VAL_PTR (l->loc)->locs
1704 && !CSELIB_VAL_PTR (l->loc)->locs->next
1705 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1706 continue;
1707 else if ((ret = find_base_term (l->loc)) != 0)
1708 break;
1710 val->locs = f;
1711 return ret;
1713 case LO_SUM:
1714 /* The standard form is (lo_sum reg sym) so look only at the
1715 second operand. */
1716 return find_base_term (XEXP (x, 1));
1718 case CONST:
1719 x = XEXP (x, 0);
1720 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1721 return 0;
1722 /* Fall through. */
1723 case PLUS:
1724 case MINUS:
1726 rtx tmp1 = XEXP (x, 0);
1727 rtx tmp2 = XEXP (x, 1);
1729 /* This is a little bit tricky since we have to determine which of
1730 the two operands represents the real base address. Otherwise this
1731 routine may return the index register instead of the base register.
1733 That may cause us to believe no aliasing was possible, when in
1734 fact aliasing is possible.
1736 We use a few simple tests to guess the base register. Additional
1737 tests can certainly be added. For example, if one of the operands
1738 is a shift or multiply, then it must be the index register and the
1739 other operand is the base register. */
1741 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1742 return find_base_term (tmp2);
1744 /* If either operand is known to be a pointer, then prefer it
1745 to determine the base term. */
1746 if (REG_P (tmp1) && REG_POINTER (tmp1))
1748 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1750 rtx tem = tmp1;
1751 tmp1 = tmp2;
1752 tmp2 = tem;
1755 /* Go ahead and find the base term for both operands. If either base
1756 term is from a pointer or is a named object or a special address
1757 (like an argument or stack reference), then use it for the
1758 base term. */
1759 rtx base = find_base_term (tmp1);
1760 if (base != NULL_RTX
1761 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1762 || known_base_value_p (base)))
1763 return base;
1764 base = find_base_term (tmp2);
1765 if (base != NULL_RTX
1766 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1767 || known_base_value_p (base)))
1768 return base;
1770 /* We could not determine which of the two operands was the
1771 base register and which was the index. So we can determine
1772 nothing from the base alias check. */
1773 return 0;
1776 case AND:
1777 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1778 return find_base_term (XEXP (x, 0));
1779 return 0;
1781 case SYMBOL_REF:
1782 case LABEL_REF:
1783 return x;
1785 default:
1786 return 0;
1790 /* Return true if accesses to address X may alias accesses based
1791 on the stack pointer. */
1793 bool
1794 may_be_sp_based_p (rtx x)
1796 rtx base = find_base_term (x);
1797 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1800 /* Return 0 if the addresses X and Y are known to point to different
1801 objects, 1 if they might be pointers to the same object. */
1803 static int
1804 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1805 machine_mode x_mode, machine_mode y_mode)
1807 /* If the address itself has no known base see if a known equivalent
1808 value has one. If either address still has no known base, nothing
1809 is known about aliasing. */
1810 if (x_base == 0)
1812 rtx x_c;
1814 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1815 return 1;
1817 x_base = find_base_term (x_c);
1818 if (x_base == 0)
1819 return 1;
1822 if (y_base == 0)
1824 rtx y_c;
1825 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1826 return 1;
1828 y_base = find_base_term (y_c);
1829 if (y_base == 0)
1830 return 1;
1833 /* If the base addresses are equal nothing is known about aliasing. */
1834 if (rtx_equal_p (x_base, y_base))
1835 return 1;
1837 /* The base addresses are different expressions. If they are not accessed
1838 via AND, there is no conflict. We can bring knowledge of object
1839 alignment into play here. For example, on alpha, "char a, b;" can
1840 alias one another, though "char a; long b;" cannot. AND addesses may
1841 implicitly alias surrounding objects; i.e. unaligned access in DImode
1842 via AND address can alias all surrounding object types except those
1843 with aligment 8 or higher. */
1844 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1845 return 1;
1846 if (GET_CODE (x) == AND
1847 && (!CONST_INT_P (XEXP (x, 1))
1848 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1849 return 1;
1850 if (GET_CODE (y) == AND
1851 && (!CONST_INT_P (XEXP (y, 1))
1852 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1853 return 1;
1855 /* Differing symbols not accessed via AND never alias. */
1856 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1857 return 0;
1859 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
1860 return 0;
1862 return 1;
1865 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1866 that of V. */
1868 static bool
1869 refs_newer_value_p (const_rtx expr, rtx v)
1871 int minuid = CSELIB_VAL_PTR (v)->uid;
1872 subrtx_iterator::array_type array;
1873 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
1874 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
1875 return true;
1876 return false;
1879 /* Convert the address X into something we can use. This is done by returning
1880 it unchanged unless it is a value; in the latter case we call cselib to get
1881 a more useful rtx. */
1884 get_addr (rtx x)
1886 cselib_val *v;
1887 struct elt_loc_list *l;
1889 if (GET_CODE (x) != VALUE)
1890 return x;
1891 v = CSELIB_VAL_PTR (x);
1892 if (v)
1894 bool have_equivs = cselib_have_permanent_equivalences ();
1895 if (have_equivs)
1896 v = canonical_cselib_val (v);
1897 for (l = v->locs; l; l = l->next)
1898 if (CONSTANT_P (l->loc))
1899 return l->loc;
1900 for (l = v->locs; l; l = l->next)
1901 if (!REG_P (l->loc) && !MEM_P (l->loc)
1902 /* Avoid infinite recursion when potentially dealing with
1903 var-tracking artificial equivalences, by skipping the
1904 equivalences themselves, and not choosing expressions
1905 that refer to newer VALUEs. */
1906 && (!have_equivs
1907 || (GET_CODE (l->loc) != VALUE
1908 && !refs_newer_value_p (l->loc, x))))
1909 return l->loc;
1910 if (have_equivs)
1912 for (l = v->locs; l; l = l->next)
1913 if (REG_P (l->loc)
1914 || (GET_CODE (l->loc) != VALUE
1915 && !refs_newer_value_p (l->loc, x)))
1916 return l->loc;
1917 /* Return the canonical value. */
1918 return v->val_rtx;
1920 if (v->locs)
1921 return v->locs->loc;
1923 return x;
1926 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1927 where SIZE is the size in bytes of the memory reference. If ADDR
1928 is not modified by the memory reference then ADDR is returned. */
1930 static rtx
1931 addr_side_effect_eval (rtx addr, int size, int n_refs)
1933 int offset = 0;
1935 switch (GET_CODE (addr))
1937 case PRE_INC:
1938 offset = (n_refs + 1) * size;
1939 break;
1940 case PRE_DEC:
1941 offset = -(n_refs + 1) * size;
1942 break;
1943 case POST_INC:
1944 offset = n_refs * size;
1945 break;
1946 case POST_DEC:
1947 offset = -n_refs * size;
1948 break;
1950 default:
1951 return addr;
1954 if (offset)
1955 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1956 gen_int_mode (offset, GET_MODE (addr)));
1957 else
1958 addr = XEXP (addr, 0);
1959 addr = canon_rtx (addr);
1961 return addr;
1964 /* Return TRUE if an object X sized at XSIZE bytes and another object
1965 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1966 any of the sizes is zero, assume an overlap, otherwise use the
1967 absolute value of the sizes as the actual sizes. */
1969 static inline bool
1970 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
1972 return (xsize == 0 || ysize == 0
1973 || (c >= 0
1974 ? (abs (xsize) > c)
1975 : (abs (ysize) > -c)));
1978 /* Return one if X and Y (memory addresses) reference the
1979 same location in memory or if the references overlap.
1980 Return zero if they do not overlap, else return
1981 minus one in which case they still might reference the same location.
1983 C is an offset accumulator. When
1984 C is nonzero, we are testing aliases between X and Y + C.
1985 XSIZE is the size in bytes of the X reference,
1986 similarly YSIZE is the size in bytes for Y.
1987 Expect that canon_rtx has been already called for X and Y.
1989 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1990 referenced (the reference was BLKmode), so make the most pessimistic
1991 assumptions.
1993 If XSIZE or YSIZE is negative, we may access memory outside the object
1994 being referenced as a side effect. This can happen when using AND to
1995 align memory references, as is done on the Alpha.
1997 Nice to notice that varying addresses cannot conflict with fp if no
1998 local variables had their addresses taken, but that's too hard now.
2000 ??? Contrary to the tree alias oracle this does not return
2001 one for X + non-constant and Y + non-constant when X and Y are equal.
2002 If that is fixed the TBAA hack for union type-punning can be removed. */
2004 static int
2005 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2007 if (GET_CODE (x) == VALUE)
2009 if (REG_P (y))
2011 struct elt_loc_list *l = NULL;
2012 if (CSELIB_VAL_PTR (x))
2013 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2014 l; l = l->next)
2015 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2016 break;
2017 if (l)
2018 x = y;
2019 else
2020 x = get_addr (x);
2022 /* Don't call get_addr if y is the same VALUE. */
2023 else if (x != y)
2024 x = get_addr (x);
2026 if (GET_CODE (y) == VALUE)
2028 if (REG_P (x))
2030 struct elt_loc_list *l = NULL;
2031 if (CSELIB_VAL_PTR (y))
2032 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2033 l; l = l->next)
2034 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2035 break;
2036 if (l)
2037 y = x;
2038 else
2039 y = get_addr (y);
2041 /* Don't call get_addr if x is the same VALUE. */
2042 else if (y != x)
2043 y = get_addr (y);
2045 if (GET_CODE (x) == HIGH)
2046 x = XEXP (x, 0);
2047 else if (GET_CODE (x) == LO_SUM)
2048 x = XEXP (x, 1);
2049 else
2050 x = addr_side_effect_eval (x, abs (xsize), 0);
2051 if (GET_CODE (y) == HIGH)
2052 y = XEXP (y, 0);
2053 else if (GET_CODE (y) == LO_SUM)
2054 y = XEXP (y, 1);
2055 else
2056 y = addr_side_effect_eval (y, abs (ysize), 0);
2058 if (rtx_equal_for_memref_p (x, y))
2060 return offset_overlap_p (c, xsize, ysize);
2063 /* This code used to check for conflicts involving stack references and
2064 globals but the base address alias code now handles these cases. */
2066 if (GET_CODE (x) == PLUS)
2068 /* The fact that X is canonicalized means that this
2069 PLUS rtx is canonicalized. */
2070 rtx x0 = XEXP (x, 0);
2071 rtx x1 = XEXP (x, 1);
2073 if (GET_CODE (y) == PLUS)
2075 /* The fact that Y is canonicalized means that this
2076 PLUS rtx is canonicalized. */
2077 rtx y0 = XEXP (y, 0);
2078 rtx y1 = XEXP (y, 1);
2080 if (rtx_equal_for_memref_p (x1, y1))
2081 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2082 if (rtx_equal_for_memref_p (x0, y0))
2083 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2084 if (CONST_INT_P (x1))
2086 if (CONST_INT_P (y1))
2087 return memrefs_conflict_p (xsize, x0, ysize, y0,
2088 c - INTVAL (x1) + INTVAL (y1));
2089 else
2090 return memrefs_conflict_p (xsize, x0, ysize, y,
2091 c - INTVAL (x1));
2093 else if (CONST_INT_P (y1))
2094 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2096 return -1;
2098 else if (CONST_INT_P (x1))
2099 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2101 else if (GET_CODE (y) == PLUS)
2103 /* The fact that Y is canonicalized means that this
2104 PLUS rtx is canonicalized. */
2105 rtx y0 = XEXP (y, 0);
2106 rtx y1 = XEXP (y, 1);
2108 if (CONST_INT_P (y1))
2109 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2110 else
2111 return -1;
2114 if (GET_CODE (x) == GET_CODE (y))
2115 switch (GET_CODE (x))
2117 case MULT:
2119 /* Handle cases where we expect the second operands to be the
2120 same, and check only whether the first operand would conflict
2121 or not. */
2122 rtx x0, y0;
2123 rtx x1 = canon_rtx (XEXP (x, 1));
2124 rtx y1 = canon_rtx (XEXP (y, 1));
2125 if (! rtx_equal_for_memref_p (x1, y1))
2126 return -1;
2127 x0 = canon_rtx (XEXP (x, 0));
2128 y0 = canon_rtx (XEXP (y, 0));
2129 if (rtx_equal_for_memref_p (x0, y0))
2130 return offset_overlap_p (c, xsize, ysize);
2132 /* Can't properly adjust our sizes. */
2133 if (!CONST_INT_P (x1))
2134 return -1;
2135 xsize /= INTVAL (x1);
2136 ysize /= INTVAL (x1);
2137 c /= INTVAL (x1);
2138 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2141 default:
2142 break;
2145 /* Deal with alignment ANDs by adjusting offset and size so as to
2146 cover the maximum range, without taking any previously known
2147 alignment into account. Make a size negative after such an
2148 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2149 assume a potential overlap, because they may end up in contiguous
2150 memory locations and the stricter-alignment access may span over
2151 part of both. */
2152 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2154 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2155 unsigned HOST_WIDE_INT uc = sc;
2156 if (sc < 0 && -uc == (uc & -uc))
2158 if (xsize > 0)
2159 xsize = -xsize;
2160 if (xsize)
2161 xsize += sc + 1;
2162 c -= sc + 1;
2163 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2164 ysize, y, c);
2167 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2169 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2170 unsigned HOST_WIDE_INT uc = sc;
2171 if (sc < 0 && -uc == (uc & -uc))
2173 if (ysize > 0)
2174 ysize = -ysize;
2175 if (ysize)
2176 ysize += sc + 1;
2177 c += sc + 1;
2178 return memrefs_conflict_p (xsize, x,
2179 ysize, canon_rtx (XEXP (y, 0)), c);
2183 if (CONSTANT_P (x))
2185 if (CONST_INT_P (x) && CONST_INT_P (y))
2187 c += (INTVAL (y) - INTVAL (x));
2188 return offset_overlap_p (c, xsize, ysize);
2191 if (GET_CODE (x) == CONST)
2193 if (GET_CODE (y) == CONST)
2194 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2195 ysize, canon_rtx (XEXP (y, 0)), c);
2196 else
2197 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2198 ysize, y, c);
2200 if (GET_CODE (y) == CONST)
2201 return memrefs_conflict_p (xsize, x, ysize,
2202 canon_rtx (XEXP (y, 0)), c);
2204 /* Assume a potential overlap for symbolic addresses that went
2205 through alignment adjustments (i.e., that have negative
2206 sizes), because we can't know how far they are from each
2207 other. */
2208 if (CONSTANT_P (y))
2209 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2211 return -1;
2214 return -1;
2217 /* Functions to compute memory dependencies.
2219 Since we process the insns in execution order, we can build tables
2220 to keep track of what registers are fixed (and not aliased), what registers
2221 are varying in known ways, and what registers are varying in unknown
2222 ways.
2224 If both memory references are volatile, then there must always be a
2225 dependence between the two references, since their order can not be
2226 changed. A volatile and non-volatile reference can be interchanged
2227 though.
2229 We also must allow AND addresses, because they may generate accesses
2230 outside the object being referenced. This is used to generate aligned
2231 addresses from unaligned addresses, for instance, the alpha
2232 storeqi_unaligned pattern. */
2234 /* Read dependence: X is read after read in MEM takes place. There can
2235 only be a dependence here if both reads are volatile, or if either is
2236 an explicit barrier. */
2239 read_dependence (const_rtx mem, const_rtx x)
2241 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2242 return true;
2243 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2244 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2245 return true;
2246 return false;
2249 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2251 static tree
2252 decl_for_component_ref (tree x)
2256 x = TREE_OPERAND (x, 0);
2258 while (x && TREE_CODE (x) == COMPONENT_REF);
2260 return x && DECL_P (x) ? x : NULL_TREE;
2263 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2264 for the offset of the field reference. *KNOWN_P says whether the
2265 offset is known. */
2267 static void
2268 adjust_offset_for_component_ref (tree x, bool *known_p,
2269 HOST_WIDE_INT *offset)
2271 if (!*known_p)
2272 return;
2275 tree xoffset = component_ref_field_offset (x);
2276 tree field = TREE_OPERAND (x, 1);
2277 if (TREE_CODE (xoffset) != INTEGER_CST)
2279 *known_p = false;
2280 return;
2283 offset_int woffset
2284 = (wi::to_offset (xoffset)
2285 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2286 LOG2_BITS_PER_UNIT));
2287 if (!wi::fits_uhwi_p (woffset))
2289 *known_p = false;
2290 return;
2292 *offset += woffset.to_uhwi ();
2294 x = TREE_OPERAND (x, 0);
2296 while (x && TREE_CODE (x) == COMPONENT_REF);
2299 /* Return nonzero if we can determine the exprs corresponding to memrefs
2300 X and Y and they do not overlap.
2301 If LOOP_VARIANT is set, skip offset-based disambiguation */
2304 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2306 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2307 rtx rtlx, rtly;
2308 rtx basex, basey;
2309 bool moffsetx_known_p, moffsety_known_p;
2310 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2311 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2313 /* Unless both have exprs, we can't tell anything. */
2314 if (exprx == 0 || expry == 0)
2315 return 0;
2317 /* For spill-slot accesses make sure we have valid offsets. */
2318 if ((exprx == get_spill_slot_decl (false)
2319 && ! MEM_OFFSET_KNOWN_P (x))
2320 || (expry == get_spill_slot_decl (false)
2321 && ! MEM_OFFSET_KNOWN_P (y)))
2322 return 0;
2324 /* If the field reference test failed, look at the DECLs involved. */
2325 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2326 if (moffsetx_known_p)
2327 moffsetx = MEM_OFFSET (x);
2328 if (TREE_CODE (exprx) == COMPONENT_REF)
2330 tree t = decl_for_component_ref (exprx);
2331 if (! t)
2332 return 0;
2333 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2334 exprx = t;
2337 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2338 if (moffsety_known_p)
2339 moffsety = MEM_OFFSET (y);
2340 if (TREE_CODE (expry) == COMPONENT_REF)
2342 tree t = decl_for_component_ref (expry);
2343 if (! t)
2344 return 0;
2345 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2346 expry = t;
2349 if (! DECL_P (exprx) || ! DECL_P (expry))
2350 return 0;
2352 /* With invalid code we can end up storing into the constant pool.
2353 Bail out to avoid ICEing when creating RTL for this.
2354 See gfortran.dg/lto/20091028-2_0.f90. */
2355 if (TREE_CODE (exprx) == CONST_DECL
2356 || TREE_CODE (expry) == CONST_DECL)
2357 return 1;
2359 rtlx = DECL_RTL (exprx);
2360 rtly = DECL_RTL (expry);
2362 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2363 can't overlap unless they are the same because we never reuse that part
2364 of the stack frame used for locals for spilled pseudos. */
2365 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2366 && ! rtx_equal_p (rtlx, rtly))
2367 return 1;
2369 /* If we have MEMs referring to different address spaces (which can
2370 potentially overlap), we cannot easily tell from the addresses
2371 whether the references overlap. */
2372 if (MEM_P (rtlx) && MEM_P (rtly)
2373 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2374 return 0;
2376 /* Get the base and offsets of both decls. If either is a register, we
2377 know both are and are the same, so use that as the base. The only
2378 we can avoid overlap is if we can deduce that they are nonoverlapping
2379 pieces of that decl, which is very rare. */
2380 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2381 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2382 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2384 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2385 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2386 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2388 /* If the bases are different, we know they do not overlap if both
2389 are constants or if one is a constant and the other a pointer into the
2390 stack frame. Otherwise a different base means we can't tell if they
2391 overlap or not. */
2392 if (! rtx_equal_p (basex, basey))
2393 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2394 || (CONSTANT_P (basex) && REG_P (basey)
2395 && REGNO_PTR_FRAME_P (REGNO (basey)))
2396 || (CONSTANT_P (basey) && REG_P (basex)
2397 && REGNO_PTR_FRAME_P (REGNO (basex))));
2399 /* Offset based disambiguation not appropriate for loop invariant */
2400 if (loop_invariant)
2401 return 0;
2403 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2404 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2405 : -1);
2406 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2407 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2408 : -1);
2410 /* If we have an offset for either memref, it can update the values computed
2411 above. */
2412 if (moffsetx_known_p)
2413 offsetx += moffsetx, sizex -= moffsetx;
2414 if (moffsety_known_p)
2415 offsety += moffsety, sizey -= moffsety;
2417 /* If a memref has both a size and an offset, we can use the smaller size.
2418 We can't do this if the offset isn't known because we must view this
2419 memref as being anywhere inside the DECL's MEM. */
2420 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2421 sizex = MEM_SIZE (x);
2422 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2423 sizey = MEM_SIZE (y);
2425 /* Put the values of the memref with the lower offset in X's values. */
2426 if (offsetx > offsety)
2428 tem = offsetx, offsetx = offsety, offsety = tem;
2429 tem = sizex, sizex = sizey, sizey = tem;
2432 /* If we don't know the size of the lower-offset value, we can't tell
2433 if they conflict. Otherwise, we do the test. */
2434 return sizex >= 0 && offsety >= offsetx + sizex;
2437 /* Helper for true_dependence and canon_true_dependence.
2438 Checks for true dependence: X is read after store in MEM takes place.
2440 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2441 NULL_RTX, and the canonical addresses of MEM and X are both computed
2442 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2444 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2446 Returns 1 if there is a true dependence, 0 otherwise. */
2448 static int
2449 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2450 const_rtx x, rtx x_addr, bool mem_canonicalized)
2452 rtx true_mem_addr;
2453 rtx base;
2454 int ret;
2456 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2457 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2459 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2460 return 1;
2462 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2463 This is used in epilogue deallocation functions, and in cselib. */
2464 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2465 return 1;
2466 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2467 return 1;
2468 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2469 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2470 return 1;
2472 if (! x_addr)
2473 x_addr = XEXP (x, 0);
2474 x_addr = get_addr (x_addr);
2476 if (! mem_addr)
2478 mem_addr = XEXP (mem, 0);
2479 if (mem_mode == VOIDmode)
2480 mem_mode = GET_MODE (mem);
2482 true_mem_addr = get_addr (mem_addr);
2484 /* Read-only memory is by definition never modified, and therefore can't
2485 conflict with anything. However, don't assume anything when AND
2486 addresses are involved and leave to the code below to determine
2487 dependence. We don't expect to find read-only set on MEM, but
2488 stupid user tricks can produce them, so don't die. */
2489 if (MEM_READONLY_P (x)
2490 && GET_CODE (x_addr) != AND
2491 && GET_CODE (true_mem_addr) != AND)
2492 return 0;
2494 /* If we have MEMs referring to different address spaces (which can
2495 potentially overlap), we cannot easily tell from the addresses
2496 whether the references overlap. */
2497 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2498 return 1;
2500 base = find_base_term (x_addr);
2501 if (base && (GET_CODE (base) == LABEL_REF
2502 || (GET_CODE (base) == SYMBOL_REF
2503 && CONSTANT_POOL_ADDRESS_P (base))))
2504 return 0;
2506 rtx mem_base = find_base_term (true_mem_addr);
2507 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2508 GET_MODE (x), mem_mode))
2509 return 0;
2511 x_addr = canon_rtx (x_addr);
2512 if (!mem_canonicalized)
2513 mem_addr = canon_rtx (true_mem_addr);
2515 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2516 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2517 return ret;
2519 if (mems_in_disjoint_alias_sets_p (x, mem))
2520 return 0;
2522 if (nonoverlapping_memrefs_p (mem, x, false))
2523 return 0;
2525 return rtx_refs_may_alias_p (x, mem, true);
2528 /* True dependence: X is read after store in MEM takes place. */
2531 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2533 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2534 x, NULL_RTX, /*mem_canonicalized=*/false);
2537 /* Canonical true dependence: X is read after store in MEM takes place.
2538 Variant of true_dependence which assumes MEM has already been
2539 canonicalized (hence we no longer do that here).
2540 The mem_addr argument has been added, since true_dependence_1 computed
2541 this value prior to canonicalizing. */
2544 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2545 const_rtx x, rtx x_addr)
2547 return true_dependence_1 (mem, mem_mode, mem_addr,
2548 x, x_addr, /*mem_canonicalized=*/true);
2551 /* Returns nonzero if a write to X might alias a previous read from
2552 (or, if WRITEP is true, a write to) MEM.
2553 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2554 and X_MODE the mode for that access.
2555 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2557 static int
2558 write_dependence_p (const_rtx mem,
2559 const_rtx x, machine_mode x_mode, rtx x_addr,
2560 bool mem_canonicalized, bool x_canonicalized, bool writep)
2562 rtx mem_addr;
2563 rtx true_mem_addr, true_x_addr;
2564 rtx base;
2565 int ret;
2567 gcc_checking_assert (x_canonicalized
2568 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2569 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2571 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2572 return 1;
2574 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2575 This is used in epilogue deallocation functions. */
2576 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2577 return 1;
2578 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2579 return 1;
2580 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2581 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2582 return 1;
2584 if (!x_addr)
2585 x_addr = XEXP (x, 0);
2586 true_x_addr = get_addr (x_addr);
2588 mem_addr = XEXP (mem, 0);
2589 true_mem_addr = get_addr (mem_addr);
2591 /* A read from read-only memory can't conflict with read-write memory.
2592 Don't assume anything when AND addresses are involved and leave to
2593 the code below to determine dependence. */
2594 if (!writep
2595 && MEM_READONLY_P (mem)
2596 && GET_CODE (true_x_addr) != AND
2597 && GET_CODE (true_mem_addr) != AND)
2598 return 0;
2600 /* If we have MEMs referring to different address spaces (which can
2601 potentially overlap), we cannot easily tell from the addresses
2602 whether the references overlap. */
2603 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2604 return 1;
2606 base = find_base_term (true_mem_addr);
2607 if (! writep
2608 && base
2609 && (GET_CODE (base) == LABEL_REF
2610 || (GET_CODE (base) == SYMBOL_REF
2611 && CONSTANT_POOL_ADDRESS_P (base))))
2612 return 0;
2614 rtx x_base = find_base_term (true_x_addr);
2615 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2616 GET_MODE (x), GET_MODE (mem)))
2617 return 0;
2619 if (!x_canonicalized)
2621 x_addr = canon_rtx (true_x_addr);
2622 x_mode = GET_MODE (x);
2624 if (!mem_canonicalized)
2625 mem_addr = canon_rtx (true_mem_addr);
2627 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2628 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2629 return ret;
2631 if (nonoverlapping_memrefs_p (x, mem, false))
2632 return 0;
2634 return rtx_refs_may_alias_p (x, mem, false);
2637 /* Anti dependence: X is written after read in MEM takes place. */
2640 anti_dependence (const_rtx mem, const_rtx x)
2642 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2643 /*mem_canonicalized=*/false,
2644 /*x_canonicalized*/false, /*writep=*/false);
2647 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2648 Also, consider X in X_MODE (which might be from an enclosing
2649 STRICT_LOW_PART / ZERO_EXTRACT).
2650 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2653 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2654 const_rtx x, machine_mode x_mode, rtx x_addr)
2656 return write_dependence_p (mem, x, x_mode, x_addr,
2657 mem_canonicalized, /*x_canonicalized=*/true,
2658 /*writep=*/false);
2661 /* Output dependence: X is written after store in MEM takes place. */
2664 output_dependence (const_rtx mem, const_rtx x)
2666 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2667 /*mem_canonicalized=*/false,
2668 /*x_canonicalized*/false, /*writep=*/true);
2673 /* Check whether X may be aliased with MEM. Don't do offset-based
2674 memory disambiguation & TBAA. */
2676 may_alias_p (const_rtx mem, const_rtx x)
2678 rtx x_addr, mem_addr;
2680 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2681 return 1;
2683 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2684 This is used in epilogue deallocation functions. */
2685 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2686 return 1;
2687 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2688 return 1;
2689 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2690 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2691 return 1;
2693 x_addr = XEXP (x, 0);
2694 x_addr = get_addr (x_addr);
2696 mem_addr = XEXP (mem, 0);
2697 mem_addr = get_addr (mem_addr);
2699 /* Read-only memory is by definition never modified, and therefore can't
2700 conflict with anything. However, don't assume anything when AND
2701 addresses are involved and leave to the code below to determine
2702 dependence. We don't expect to find read-only set on MEM, but
2703 stupid user tricks can produce them, so don't die. */
2704 if (MEM_READONLY_P (x)
2705 && GET_CODE (x_addr) != AND
2706 && GET_CODE (mem_addr) != AND)
2707 return 0;
2709 /* If we have MEMs referring to different address spaces (which can
2710 potentially overlap), we cannot easily tell from the addresses
2711 whether the references overlap. */
2712 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2713 return 1;
2715 rtx x_base = find_base_term (x_addr);
2716 rtx mem_base = find_base_term (mem_addr);
2717 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2718 GET_MODE (x), GET_MODE (mem_addr)))
2719 return 0;
2721 if (nonoverlapping_memrefs_p (mem, x, true))
2722 return 0;
2724 /* TBAA not valid for loop_invarint */
2725 return rtx_refs_may_alias_p (x, mem, false);
2728 void
2729 init_alias_target (void)
2731 int i;
2733 if (!arg_base_value)
2734 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2736 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2738 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2739 /* Check whether this register can hold an incoming pointer
2740 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2741 numbers, so translate if necessary due to register windows. */
2742 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2743 && HARD_REGNO_MODE_OK (i, Pmode))
2744 static_reg_base_value[i] = arg_base_value;
2746 static_reg_base_value[STACK_POINTER_REGNUM]
2747 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2748 static_reg_base_value[ARG_POINTER_REGNUM]
2749 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2750 static_reg_base_value[FRAME_POINTER_REGNUM]
2751 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2752 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2753 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2754 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2755 #endif
2758 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2759 to be memory reference. */
2760 static bool memory_modified;
2761 static void
2762 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2764 if (MEM_P (x))
2766 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2767 memory_modified = true;
2772 /* Return true when INSN possibly modify memory contents of MEM
2773 (i.e. address can be modified). */
2774 bool
2775 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2777 if (!INSN_P (insn))
2778 return false;
2779 memory_modified = false;
2780 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2781 return memory_modified;
2784 /* Return TRUE if the destination of a set is rtx identical to
2785 ITEM. */
2786 static inline bool
2787 set_dest_equal_p (const_rtx set, const_rtx item)
2789 rtx dest = SET_DEST (set);
2790 return rtx_equal_p (dest, item);
2793 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2794 *DEFINITELY* modify the memory contents of MEM. */
2795 bool
2796 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2798 if (!INSN_P (insn))
2799 return false;
2800 insn = PATTERN (insn);
2801 if (GET_CODE (insn) == SET)
2802 return set_dest_equal_p (insn, mem);
2803 else if (GET_CODE (insn) == PARALLEL)
2805 int i;
2806 for (i = 0; i < XVECLEN (insn, 0); i++)
2808 rtx sub = XVECEXP (insn, 0, i);
2809 if (GET_CODE (sub) == SET
2810 && set_dest_equal_p (sub, mem))
2811 return true;
2814 return false;
2817 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2818 array. */
2820 void
2821 init_alias_analysis (void)
2823 unsigned int maxreg = max_reg_num ();
2824 int changed, pass;
2825 int i;
2826 unsigned int ui;
2827 rtx_insn *insn;
2828 rtx val;
2829 int rpo_cnt;
2830 int *rpo;
2832 timevar_push (TV_ALIAS_ANALYSIS);
2834 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
2835 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
2836 bitmap_clear (reg_known_equiv_p);
2838 /* If we have memory allocated from the previous run, use it. */
2839 if (old_reg_base_value)
2840 reg_base_value = old_reg_base_value;
2842 if (reg_base_value)
2843 reg_base_value->truncate (0);
2845 vec_safe_grow_cleared (reg_base_value, maxreg);
2847 new_reg_base_value = XNEWVEC (rtx, maxreg);
2848 reg_seen = sbitmap_alloc (maxreg);
2850 /* The basic idea is that each pass through this loop will use the
2851 "constant" information from the previous pass to propagate alias
2852 information through another level of assignments.
2854 The propagation is done on the CFG in reverse post-order, to propagate
2855 things forward as far as possible in each iteration.
2857 This could get expensive if the assignment chains are long. Maybe
2858 we should throttle the number of iterations, possibly based on
2859 the optimization level or flag_expensive_optimizations.
2861 We could propagate more information in the first pass by making use
2862 of DF_REG_DEF_COUNT to determine immediately that the alias information
2863 for a pseudo is "constant".
2865 A program with an uninitialized variable can cause an infinite loop
2866 here. Instead of doing a full dataflow analysis to detect such problems
2867 we just cap the number of iterations for the loop.
2869 The state of the arrays for the set chain in question does not matter
2870 since the program has undefined behavior. */
2872 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2873 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
2875 pass = 0;
2878 /* Assume nothing will change this iteration of the loop. */
2879 changed = 0;
2881 /* We want to assign the same IDs each iteration of this loop, so
2882 start counting from one each iteration of the loop. */
2883 unique_id = 1;
2885 /* We're at the start of the function each iteration through the
2886 loop, so we're copying arguments. */
2887 copying_arguments = true;
2889 /* Wipe the potential alias information clean for this pass. */
2890 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2892 /* Wipe the reg_seen array clean. */
2893 bitmap_clear (reg_seen);
2895 /* Initialize the alias information for this pass. */
2896 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2897 if (static_reg_base_value[i])
2899 new_reg_base_value[i] = static_reg_base_value[i];
2900 bitmap_set_bit (reg_seen, i);
2903 /* Walk the insns adding values to the new_reg_base_value array. */
2904 for (i = 0; i < rpo_cnt; i++)
2906 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2907 FOR_BB_INSNS (bb, insn)
2909 if (NONDEBUG_INSN_P (insn))
2911 rtx note, set;
2913 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2914 /* The prologue/epilogue insns are not threaded onto the
2915 insn chain until after reload has completed. Thus,
2916 there is no sense wasting time checking if INSN is in
2917 the prologue/epilogue until after reload has completed. */
2918 if (reload_completed
2919 && prologue_epilogue_contains (insn))
2920 continue;
2921 #endif
2923 /* If this insn has a noalias note, process it, Otherwise,
2924 scan for sets. A simple set will have no side effects
2925 which could change the base value of any other register. */
2927 if (GET_CODE (PATTERN (insn)) == SET
2928 && REG_NOTES (insn) != 0
2929 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2930 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2931 else
2932 note_stores (PATTERN (insn), record_set, NULL);
2934 set = single_set (insn);
2936 if (set != 0
2937 && REG_P (SET_DEST (set))
2938 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2940 unsigned int regno = REGNO (SET_DEST (set));
2941 rtx src = SET_SRC (set);
2942 rtx t;
2944 note = find_reg_equal_equiv_note (insn);
2945 if (note && REG_NOTE_KIND (note) == REG_EQUAL
2946 && DF_REG_DEF_COUNT (regno) != 1)
2947 note = NULL_RTX;
2949 if (note != NULL_RTX
2950 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2951 && ! rtx_varies_p (XEXP (note, 0), 1)
2952 && ! reg_overlap_mentioned_p (SET_DEST (set),
2953 XEXP (note, 0)))
2955 set_reg_known_value (regno, XEXP (note, 0));
2956 set_reg_known_equiv_p (regno,
2957 REG_NOTE_KIND (note) == REG_EQUIV);
2959 else if (DF_REG_DEF_COUNT (regno) == 1
2960 && GET_CODE (src) == PLUS
2961 && REG_P (XEXP (src, 0))
2962 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2963 && CONST_INT_P (XEXP (src, 1)))
2965 t = plus_constant (GET_MODE (src), t,
2966 INTVAL (XEXP (src, 1)));
2967 set_reg_known_value (regno, t);
2968 set_reg_known_equiv_p (regno, false);
2970 else if (DF_REG_DEF_COUNT (regno) == 1
2971 && ! rtx_varies_p (src, 1))
2973 set_reg_known_value (regno, src);
2974 set_reg_known_equiv_p (regno, false);
2978 else if (NOTE_P (insn)
2979 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
2980 copying_arguments = false;
2984 /* Now propagate values from new_reg_base_value to reg_base_value. */
2985 gcc_assert (maxreg == (unsigned int) max_reg_num ());
2987 for (ui = 0; ui < maxreg; ui++)
2989 if (new_reg_base_value[ui]
2990 && new_reg_base_value[ui] != (*reg_base_value)[ui]
2991 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
2993 (*reg_base_value)[ui] = new_reg_base_value[ui];
2994 changed = 1;
2998 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
2999 XDELETEVEC (rpo);
3001 /* Fill in the remaining entries. */
3002 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3004 int regno = i + FIRST_PSEUDO_REGISTER;
3005 if (! val)
3006 set_reg_known_value (regno, regno_reg_rtx[regno]);
3009 /* Clean up. */
3010 free (new_reg_base_value);
3011 new_reg_base_value = 0;
3012 sbitmap_free (reg_seen);
3013 reg_seen = 0;
3014 timevar_pop (TV_ALIAS_ANALYSIS);
3017 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3018 Special API for var-tracking pass purposes. */
3020 void
3021 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3023 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3026 void
3027 end_alias_analysis (void)
3029 old_reg_base_value = reg_base_value;
3030 vec_free (reg_known_value);
3031 sbitmap_free (reg_known_equiv_p);
3034 #include "gt-alias.h"