re PR c++/58516 (ICE with __transaction_atomic)
[official-gcc.git] / gcc / alias.c
blob7d28267657bdb818e5cbb64db9cabc26db0722b3
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2013 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 "tm_p.h"
28 #include "function.h"
29 #include "alias.h"
30 #include "emit-rtl.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "flags.h"
35 #include "diagnostic-core.h"
36 #include "cselib.h"
37 #include "splay-tree.h"
38 #include "ggc.h"
39 #include "langhooks.h"
40 #include "timevar.h"
41 #include "dumpfile.h"
42 #include "target.h"
43 #include "cgraph.h"
44 #include "df.h"
45 #include "tree-ssa-alias.h"
46 #include "pointer-set.h"
47 #include "tree-ssa.h"
49 /* The aliasing API provided here solves related but different problems:
51 Say there exists (in c)
53 struct X {
54 struct Y y1;
55 struct Z z2;
56 } x1, *px1, *px2;
58 struct Y y2, *py;
59 struct Z z2, *pz;
62 py = &x1.y1;
63 px2 = &x1;
65 Consider the four questions:
67 Can a store to x1 interfere with px2->y1?
68 Can a store to x1 interfere with px2->z2?
69 Can a store to x1 change the value pointed to by with py?
70 Can a store to x1 change the value pointed to by with pz?
72 The answer to these questions can be yes, yes, yes, and maybe.
74 The first two questions can be answered with a simple examination
75 of the type system. If structure X contains a field of type Y then
76 a store through a pointer to an X can overwrite any field that is
77 contained (recursively) in an X (unless we know that px1 != px2).
79 The last two questions can be solved in the same way as the first
80 two questions but this is too conservative. The observation is
81 that in some cases we can know which (if any) fields are addressed
82 and if those addresses are used in bad ways. This analysis may be
83 language specific. In C, arbitrary operations may be applied to
84 pointers. However, there is some indication that this may be too
85 conservative for some C++ types.
87 The pass ipa-type-escape does this analysis for the types whose
88 instances do not escape across the compilation boundary.
90 Historically in GCC, these two problems were combined and a single
91 data structure that was used to represent the solution to these
92 problems. We now have two similar but different data structures,
93 The data structure to solve the last two questions is similar to
94 the first, but does not contain the fields whose address are never
95 taken. For types that do escape the compilation unit, the data
96 structures will have identical information.
99 /* The alias sets assigned to MEMs assist the back-end in determining
100 which MEMs can alias which other MEMs. In general, two MEMs in
101 different alias sets cannot alias each other, with one important
102 exception. Consider something like:
104 struct S { int i; double d; };
106 a store to an `S' can alias something of either type `int' or type
107 `double'. (However, a store to an `int' cannot alias a `double'
108 and vice versa.) We indicate this via a tree structure that looks
109 like:
110 struct S
113 |/_ _\|
114 int double
116 (The arrows are directed and point downwards.)
117 In this situation we say the alias set for `struct S' is the
118 `superset' and that those for `int' and `double' are `subsets'.
120 To see whether two alias sets can point to the same memory, we must
121 see if either alias set is a subset of the other. We need not trace
122 past immediate descendants, however, since we propagate all
123 grandchildren up one level.
125 Alias set zero is implicitly a superset of all other alias sets.
126 However, this is no actual entry for alias set zero. It is an
127 error to attempt to explicitly construct a subset of zero. */
129 struct GTY(()) alias_set_entry_d {
130 /* The alias set number, as stored in MEM_ALIAS_SET. */
131 alias_set_type alias_set;
133 /* Nonzero if would have a child of zero: this effectively makes this
134 alias set the same as alias set zero. */
135 int has_zero_child;
137 /* The children of the alias set. These are not just the immediate
138 children, but, in fact, all descendants. So, if we have:
140 struct T { struct S s; float f; }
142 continuing our example above, the children here will be all of
143 `int', `double', `float', and `struct S'. */
144 splay_tree GTY((param1_is (int), param2_is (int))) children;
146 typedef struct alias_set_entry_d *alias_set_entry;
148 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
149 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
150 static void record_set (rtx, const_rtx, void *);
151 static int base_alias_check (rtx, rtx, rtx, rtx, enum machine_mode,
152 enum machine_mode);
153 static rtx find_base_value (rtx);
154 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
155 static int insert_subset_children (splay_tree_node, void*);
156 static alias_set_entry get_alias_set_entry (alias_set_type);
157 static bool nonoverlapping_component_refs_p (const_rtx, const_rtx);
158 static tree decl_for_component_ref (tree);
159 static int write_dependence_p (const_rtx,
160 const_rtx, enum machine_mode, rtx,
161 bool, bool, bool);
163 static void memory_modified_1 (rtx, const_rtx, void *);
165 /* Set up all info needed to perform alias analysis on memory references. */
167 /* Returns the size in bytes of the mode of X. */
168 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
170 /* Cap the number of passes we make over the insns propagating alias
171 information through set chains.
172 ??? 10 is a completely arbitrary choice. This should be based on the
173 maximum loop depth in the CFG, but we do not have this information
174 available (even if current_loops _is_ available). */
175 #define MAX_ALIAS_LOOP_PASSES 10
177 /* reg_base_value[N] gives an address to which register N is related.
178 If all sets after the first add or subtract to the current value
179 or otherwise modify it so it does not point to a different top level
180 object, reg_base_value[N] is equal to the address part of the source
181 of the first set.
183 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
184 expressions represent three types of base:
186 1. incoming arguments. There is just one ADDRESS to represent all
187 arguments, since we do not know at this level whether accesses
188 based on different arguments can alias. The ADDRESS has id 0.
190 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
191 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
192 Each of these rtxes has a separate ADDRESS associated with it,
193 each with a negative id.
195 GCC is (and is required to be) precise in which register it
196 chooses to access a particular region of stack. We can therefore
197 assume that accesses based on one of these rtxes do not alias
198 accesses based on another of these rtxes.
200 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
201 Each such piece of memory has a separate ADDRESS associated
202 with it, each with an id greater than 0.
204 Accesses based on one ADDRESS do not alias accesses based on other
205 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
206 alias globals either; the ADDRESSes have Pmode to indicate this.
207 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
208 indicate this. */
210 static GTY(()) vec<rtx, va_gc> *reg_base_value;
211 static rtx *new_reg_base_value;
213 /* The single VOIDmode ADDRESS that represents all argument bases.
214 It has id 0. */
215 static GTY(()) rtx arg_base_value;
217 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
218 static int unique_id;
220 /* We preserve the copy of old array around to avoid amount of garbage
221 produced. About 8% of garbage produced were attributed to this
222 array. */
223 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
225 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
226 registers. */
227 #define UNIQUE_BASE_VALUE_SP -1
228 #define UNIQUE_BASE_VALUE_ARGP -2
229 #define UNIQUE_BASE_VALUE_FP -3
230 #define UNIQUE_BASE_VALUE_HFP -4
232 #define static_reg_base_value \
233 (this_target_rtl->x_static_reg_base_value)
235 #define REG_BASE_VALUE(X) \
236 (REGNO (X) < vec_safe_length (reg_base_value) \
237 ? (*reg_base_value)[REGNO (X)] : 0)
239 /* Vector indexed by N giving the initial (unchanging) value known for
240 pseudo-register N. This vector is initialized in init_alias_analysis,
241 and does not change until end_alias_analysis is called. */
242 static GTY(()) vec<rtx, va_gc> *reg_known_value;
244 /* Vector recording for each reg_known_value whether it is due to a
245 REG_EQUIV note. Future passes (viz., reload) may replace the
246 pseudo with the equivalent expression and so we account for the
247 dependences that would be introduced if that happens.
249 The REG_EQUIV notes created in assign_parms may mention the arg
250 pointer, and there are explicit insns in the RTL that modify the
251 arg pointer. Thus we must ensure that such insns don't get
252 scheduled across each other because that would invalidate the
253 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
254 wrong, but solving the problem in the scheduler will likely give
255 better code, so we do it here. */
256 static sbitmap reg_known_equiv_p;
258 /* True when scanning insns from the start of the rtl to the
259 NOTE_INSN_FUNCTION_BEG note. */
260 static bool copying_arguments;
263 /* The splay-tree used to store the various alias set entries. */
264 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
266 /* Build a decomposed reference object for querying the alias-oracle
267 from the MEM rtx and store it in *REF.
268 Returns false if MEM is not suitable for the alias-oracle. */
270 static bool
271 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
273 tree expr = MEM_EXPR (mem);
274 tree base;
276 if (!expr)
277 return false;
279 ao_ref_init (ref, expr);
281 /* Get the base of the reference and see if we have to reject or
282 adjust it. */
283 base = ao_ref_base (ref);
284 if (base == NULL_TREE)
285 return false;
287 /* The tree oracle doesn't like bases that are neither decls
288 nor indirect references of SSA names. */
289 if (!(DECL_P (base)
290 || (TREE_CODE (base) == MEM_REF
291 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
292 || (TREE_CODE (base) == TARGET_MEM_REF
293 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
294 return false;
296 /* If this is a reference based on a partitioned decl replace the
297 base with a MEM_REF of the pointer representative we
298 created during stack slot partitioning. */
299 if (TREE_CODE (base) == VAR_DECL
300 && ! is_global_var (base)
301 && cfun->gimple_df->decls_to_pointers != NULL)
303 void *namep;
304 namep = pointer_map_contains (cfun->gimple_df->decls_to_pointers, base);
305 if (namep)
306 ref->base = build_simple_mem_ref (*(tree *)namep);
309 ref->ref_alias_set = MEM_ALIAS_SET (mem);
311 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
312 is conservative, so trust it. */
313 if (!MEM_OFFSET_KNOWN_P (mem)
314 || !MEM_SIZE_KNOWN_P (mem))
315 return true;
317 /* If the base decl is a parameter we can have negative MEM_OFFSET in
318 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
319 here. */
320 if (MEM_OFFSET (mem) < 0
321 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
322 return true;
324 /* Otherwise continue and refine size and offset we got from analyzing
325 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
327 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
328 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
330 /* The MEM may extend into adjacent fields, so adjust max_size if
331 necessary. */
332 if (ref->max_size != -1
333 && ref->size > ref->max_size)
334 ref->max_size = ref->size;
336 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
337 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
338 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
339 && (ref->offset < 0
340 || (DECL_P (ref->base)
341 && (!host_integerp (DECL_SIZE (ref->base), 1)
342 || (TREE_INT_CST_LOW (DECL_SIZE ((ref->base)))
343 < (unsigned HOST_WIDE_INT)(ref->offset + ref->size))))))
344 return false;
346 return true;
349 /* Query the alias-oracle on whether the two memory rtx X and MEM may
350 alias. If TBAA_P is set also apply TBAA. Returns true if the
351 two rtxen may alias, false otherwise. */
353 static bool
354 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
356 ao_ref ref1, ref2;
358 if (!ao_ref_from_mem (&ref1, x)
359 || !ao_ref_from_mem (&ref2, mem))
360 return true;
362 return refs_may_alias_p_1 (&ref1, &ref2,
363 tbaa_p
364 && MEM_ALIAS_SET (x) != 0
365 && MEM_ALIAS_SET (mem) != 0);
368 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
369 such an entry, or NULL otherwise. */
371 static inline alias_set_entry
372 get_alias_set_entry (alias_set_type alias_set)
374 return (*alias_sets)[alias_set];
377 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
378 the two MEMs cannot alias each other. */
380 static inline int
381 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
383 /* Perform a basic sanity check. Namely, that there are no alias sets
384 if we're not using strict aliasing. This helps to catch bugs
385 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
386 where a MEM is allocated in some way other than by the use of
387 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
388 use alias sets to indicate that spilled registers cannot alias each
389 other, we might need to remove this check. */
390 gcc_assert (flag_strict_aliasing
391 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
393 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
396 /* Insert the NODE into the splay tree given by DATA. Used by
397 record_alias_subset via splay_tree_foreach. */
399 static int
400 insert_subset_children (splay_tree_node node, void *data)
402 splay_tree_insert ((splay_tree) data, node->key, node->value);
404 return 0;
407 /* Return true if the first alias set is a subset of the second. */
409 bool
410 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
412 alias_set_entry ase;
414 /* Everything is a subset of the "aliases everything" set. */
415 if (set2 == 0)
416 return true;
418 /* Otherwise, check if set1 is a subset of set2. */
419 ase = get_alias_set_entry (set2);
420 if (ase != 0
421 && (ase->has_zero_child
422 || splay_tree_lookup (ase->children,
423 (splay_tree_key) set1)))
424 return true;
425 return false;
428 /* Return 1 if the two specified alias sets may conflict. */
431 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
433 alias_set_entry ase;
435 /* The easy case. */
436 if (alias_sets_must_conflict_p (set1, set2))
437 return 1;
439 /* See if the first alias set is a subset of the second. */
440 ase = get_alias_set_entry (set1);
441 if (ase != 0
442 && (ase->has_zero_child
443 || splay_tree_lookup (ase->children,
444 (splay_tree_key) set2)))
445 return 1;
447 /* Now do the same, but with the alias sets reversed. */
448 ase = get_alias_set_entry (set2);
449 if (ase != 0
450 && (ase->has_zero_child
451 || splay_tree_lookup (ase->children,
452 (splay_tree_key) set1)))
453 return 1;
455 /* The two alias sets are distinct and neither one is the
456 child of the other. Therefore, they cannot conflict. */
457 return 0;
460 /* Return 1 if the two specified alias sets will always conflict. */
463 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
465 if (set1 == 0 || set2 == 0 || set1 == set2)
466 return 1;
468 return 0;
471 /* Return 1 if any MEM object of type T1 will always conflict (using the
472 dependency routines in this file) with any MEM object of type T2.
473 This is used when allocating temporary storage. If T1 and/or T2 are
474 NULL_TREE, it means we know nothing about the storage. */
477 objects_must_conflict_p (tree t1, tree t2)
479 alias_set_type set1, set2;
481 /* If neither has a type specified, we don't know if they'll conflict
482 because we may be using them to store objects of various types, for
483 example the argument and local variables areas of inlined functions. */
484 if (t1 == 0 && t2 == 0)
485 return 0;
487 /* If they are the same type, they must conflict. */
488 if (t1 == t2
489 /* Likewise if both are volatile. */
490 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
491 return 1;
493 set1 = t1 ? get_alias_set (t1) : 0;
494 set2 = t2 ? get_alias_set (t2) : 0;
496 /* We can't use alias_sets_conflict_p because we must make sure
497 that every subtype of t1 will conflict with every subtype of
498 t2 for which a pair of subobjects of these respective subtypes
499 overlaps on the stack. */
500 return alias_sets_must_conflict_p (set1, set2);
503 /* Return true if all nested component references handled by
504 get_inner_reference in T are such that we should use the alias set
505 provided by the object at the heart of T.
507 This is true for non-addressable components (which don't have their
508 own alias set), as well as components of objects in alias set zero.
509 This later point is a special case wherein we wish to override the
510 alias set used by the component, but we don't have per-FIELD_DECL
511 assignable alias sets. */
513 bool
514 component_uses_parent_alias_set (const_tree t)
516 while (1)
518 /* If we're at the end, it vacuously uses its own alias set. */
519 if (!handled_component_p (t))
520 return false;
522 switch (TREE_CODE (t))
524 case COMPONENT_REF:
525 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
526 return true;
527 break;
529 case ARRAY_REF:
530 case ARRAY_RANGE_REF:
531 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
532 return true;
533 break;
535 case REALPART_EXPR:
536 case IMAGPART_EXPR:
537 break;
539 default:
540 /* Bitfields and casts are never addressable. */
541 return true;
544 t = TREE_OPERAND (t, 0);
545 if (get_alias_set (TREE_TYPE (t)) == 0)
546 return true;
551 /* Return whether the pointer-type T effective for aliasing may
552 access everything and thus the reference has to be assigned
553 alias-set zero. */
555 static bool
556 ref_all_alias_ptr_type_p (const_tree t)
558 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
559 || TYPE_REF_CAN_ALIAS_ALL (t));
562 /* Return the alias set for the memory pointed to by T, which may be
563 either a type or an expression. Return -1 if there is nothing
564 special about dereferencing T. */
566 static alias_set_type
567 get_deref_alias_set_1 (tree t)
569 /* All we care about is the type. */
570 if (! TYPE_P (t))
571 t = TREE_TYPE (t);
573 /* If we have an INDIRECT_REF via a void pointer, we don't
574 know anything about what that might alias. Likewise if the
575 pointer is marked that way. */
576 if (ref_all_alias_ptr_type_p (t))
577 return 0;
579 return -1;
582 /* Return the alias set for the memory pointed to by T, which may be
583 either a type or an expression. */
585 alias_set_type
586 get_deref_alias_set (tree t)
588 /* If we're not doing any alias analysis, just assume everything
589 aliases everything else. */
590 if (!flag_strict_aliasing)
591 return 0;
593 alias_set_type set = get_deref_alias_set_1 (t);
595 /* Fall back to the alias-set of the pointed-to type. */
596 if (set == -1)
598 if (! TYPE_P (t))
599 t = TREE_TYPE (t);
600 set = get_alias_set (TREE_TYPE (t));
603 return set;
606 /* Return the pointer-type relevant for TBAA purposes from the
607 memory reference tree *T or NULL_TREE in which case *T is
608 adjusted to point to the outermost component reference that
609 can be used for assigning an alias set. */
611 static tree
612 reference_alias_ptr_type_1 (tree *t)
614 tree inner;
616 /* Get the base object of the reference. */
617 inner = *t;
618 while (handled_component_p (inner))
620 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
621 the type of any component references that wrap it to
622 determine the alias-set. */
623 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
624 *t = TREE_OPERAND (inner, 0);
625 inner = TREE_OPERAND (inner, 0);
628 /* Handle pointer dereferences here, they can override the
629 alias-set. */
630 if (INDIRECT_REF_P (inner)
631 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
632 return TREE_TYPE (TREE_OPERAND (inner, 0));
633 else if (TREE_CODE (inner) == TARGET_MEM_REF)
634 return TREE_TYPE (TMR_OFFSET (inner));
635 else if (TREE_CODE (inner) == MEM_REF
636 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
637 return TREE_TYPE (TREE_OPERAND (inner, 1));
639 /* If the innermost reference is a MEM_REF that has a
640 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
641 using the memory access type for determining the alias-set. */
642 if (TREE_CODE (inner) == MEM_REF
643 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
644 != TYPE_MAIN_VARIANT
645 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
646 return TREE_TYPE (TREE_OPERAND (inner, 1));
648 /* Otherwise, pick up the outermost object that we could have a pointer
649 to, processing conversions as above. */
650 /* ??? Ick, this is worse than quadratic! */
651 while (component_uses_parent_alias_set (*t))
653 *t = TREE_OPERAND (*t, 0);
654 STRIP_NOPS (*t);
657 return NULL_TREE;
660 /* Return the pointer-type relevant for TBAA purposes from the
661 gimple memory reference tree T. This is the type to be used for
662 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
663 and guarantees that get_alias_set will return the same alias
664 set for T and the replacement. */
666 tree
667 reference_alias_ptr_type (tree t)
669 tree ptype = reference_alias_ptr_type_1 (&t);
670 /* If there is a given pointer type for aliasing purposes, return it. */
671 if (ptype != NULL_TREE)
672 return ptype;
674 /* Otherwise build one from the outermost component reference we
675 may use. */
676 if (TREE_CODE (t) == MEM_REF
677 || TREE_CODE (t) == TARGET_MEM_REF)
678 return TREE_TYPE (TREE_OPERAND (t, 1));
679 else
680 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
683 /* Return whether the pointer-types T1 and T2 used to determine
684 two alias sets of two references will yield the same answer
685 from get_deref_alias_set. */
687 bool
688 alias_ptr_types_compatible_p (tree t1, tree t2)
690 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
691 return true;
693 if (ref_all_alias_ptr_type_p (t1)
694 || ref_all_alias_ptr_type_p (t2))
695 return false;
697 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
698 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
701 /* Return the alias set for T, which may be either a type or an
702 expression. Call language-specific routine for help, if needed. */
704 alias_set_type
705 get_alias_set (tree t)
707 alias_set_type set;
709 /* If we're not doing any alias analysis, just assume everything
710 aliases everything else. Also return 0 if this or its type is
711 an error. */
712 if (! flag_strict_aliasing || t == error_mark_node
713 || (! TYPE_P (t)
714 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
715 return 0;
717 /* We can be passed either an expression or a type. This and the
718 language-specific routine may make mutually-recursive calls to each other
719 to figure out what to do. At each juncture, we see if this is a tree
720 that the language may need to handle specially. First handle things that
721 aren't types. */
722 if (! TYPE_P (t))
724 /* Give the language a chance to do something with this tree
725 before we look at it. */
726 STRIP_NOPS (t);
727 set = lang_hooks.get_alias_set (t);
728 if (set != -1)
729 return set;
731 /* Get the alias pointer-type to use or the outermost object
732 that we could have a pointer to. */
733 tree ptype = reference_alias_ptr_type_1 (&t);
734 if (ptype != NULL)
735 return get_deref_alias_set (ptype);
737 /* If we've already determined the alias set for a decl, just return
738 it. This is necessary for C++ anonymous unions, whose component
739 variables don't look like union members (boo!). */
740 if (TREE_CODE (t) == VAR_DECL
741 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
742 return MEM_ALIAS_SET (DECL_RTL (t));
744 /* Now all we care about is the type. */
745 t = TREE_TYPE (t);
748 /* Variant qualifiers don't affect the alias set, so get the main
749 variant. */
750 t = TYPE_MAIN_VARIANT (t);
752 /* Always use the canonical type as well. If this is a type that
753 requires structural comparisons to identify compatible types
754 use alias set zero. */
755 if (TYPE_STRUCTURAL_EQUALITY_P (t))
757 /* Allow the language to specify another alias set for this
758 type. */
759 set = lang_hooks.get_alias_set (t);
760 if (set != -1)
761 return set;
762 return 0;
765 t = TYPE_CANONICAL (t);
767 /* The canonical type should not require structural equality checks. */
768 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
770 /* If this is a type with a known alias set, return it. */
771 if (TYPE_ALIAS_SET_KNOWN_P (t))
772 return TYPE_ALIAS_SET (t);
774 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
775 if (!COMPLETE_TYPE_P (t))
777 /* For arrays with unknown size the conservative answer is the
778 alias set of the element type. */
779 if (TREE_CODE (t) == ARRAY_TYPE)
780 return get_alias_set (TREE_TYPE (t));
782 /* But return zero as a conservative answer for incomplete types. */
783 return 0;
786 /* See if the language has special handling for this type. */
787 set = lang_hooks.get_alias_set (t);
788 if (set != -1)
789 return set;
791 /* There are no objects of FUNCTION_TYPE, so there's no point in
792 using up an alias set for them. (There are, of course, pointers
793 and references to functions, but that's different.) */
794 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
795 set = 0;
797 /* Unless the language specifies otherwise, let vector types alias
798 their components. This avoids some nasty type punning issues in
799 normal usage. And indeed lets vectors be treated more like an
800 array slice. */
801 else if (TREE_CODE (t) == VECTOR_TYPE)
802 set = get_alias_set (TREE_TYPE (t));
804 /* Unless the language specifies otherwise, treat array types the
805 same as their components. This avoids the asymmetry we get
806 through recording the components. Consider accessing a
807 character(kind=1) through a reference to a character(kind=1)[1:1].
808 Or consider if we want to assign integer(kind=4)[0:D.1387] and
809 integer(kind=4)[4] the same alias set or not.
810 Just be pragmatic here and make sure the array and its element
811 type get the same alias set assigned. */
812 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
813 set = get_alias_set (TREE_TYPE (t));
815 /* From the former common C and C++ langhook implementation:
817 Unfortunately, there is no canonical form of a pointer type.
818 In particular, if we have `typedef int I', then `int *', and
819 `I *' are different types. So, we have to pick a canonical
820 representative. We do this below.
822 Technically, this approach is actually more conservative that
823 it needs to be. In particular, `const int *' and `int *'
824 should be in different alias sets, according to the C and C++
825 standard, since their types are not the same, and so,
826 technically, an `int **' and `const int **' cannot point at
827 the same thing.
829 But, the standard is wrong. In particular, this code is
830 legal C++:
832 int *ip;
833 int **ipp = &ip;
834 const int* const* cipp = ipp;
835 And, it doesn't make sense for that to be legal unless you
836 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
837 the pointed-to types. This issue has been reported to the
838 C++ committee.
840 In addition to the above canonicalization issue, with LTO
841 we should also canonicalize `T (*)[]' to `T *' avoiding
842 alias issues with pointer-to element types and pointer-to
843 array types.
845 Likewise we need to deal with the situation of incomplete
846 pointed-to types and make `*(struct X **)&a' and
847 `*(struct X {} **)&a' alias. Otherwise we will have to
848 guarantee that all pointer-to incomplete type variants
849 will be replaced by pointer-to complete type variants if
850 they are available.
852 With LTO the convenient situation of using `void *' to
853 access and store any pointer type will also become
854 more apparent (and `void *' is just another pointer-to
855 incomplete type). Assigning alias-set zero to `void *'
856 and all pointer-to incomplete types is a not appealing
857 solution. Assigning an effective alias-set zero only
858 affecting pointers might be - by recording proper subset
859 relationships of all pointer alias-sets.
861 Pointer-to function types are another grey area which
862 needs caution. Globbing them all into one alias-set
863 or the above effective zero set would work.
865 For now just assign the same alias-set to all pointers.
866 That's simple and avoids all the above problems. */
867 else if (POINTER_TYPE_P (t)
868 && t != ptr_type_node)
869 set = get_alias_set (ptr_type_node);
871 /* Otherwise make a new alias set for this type. */
872 else
874 /* Each canonical type gets its own alias set, so canonical types
875 shouldn't form a tree. It doesn't really matter for types
876 we handle specially above, so only check it where it possibly
877 would result in a bogus alias set. */
878 gcc_checking_assert (TYPE_CANONICAL (t) == t);
880 set = new_alias_set ();
883 TYPE_ALIAS_SET (t) = set;
885 /* If this is an aggregate type or a complex type, we must record any
886 component aliasing information. */
887 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
888 record_component_aliases (t);
890 return set;
893 /* Return a brand-new alias set. */
895 alias_set_type
896 new_alias_set (void)
898 if (flag_strict_aliasing)
900 if (alias_sets == 0)
901 vec_safe_push (alias_sets, (alias_set_entry) 0);
902 vec_safe_push (alias_sets, (alias_set_entry) 0);
903 return alias_sets->length () - 1;
905 else
906 return 0;
909 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
910 not everything that aliases SUPERSET also aliases SUBSET. For example,
911 in C, a store to an `int' can alias a load of a structure containing an
912 `int', and vice versa. But it can't alias a load of a 'double' member
913 of the same structure. Here, the structure would be the SUPERSET and
914 `int' the SUBSET. This relationship is also described in the comment at
915 the beginning of this file.
917 This function should be called only once per SUPERSET/SUBSET pair.
919 It is illegal for SUPERSET to be zero; everything is implicitly a
920 subset of alias set zero. */
922 void
923 record_alias_subset (alias_set_type superset, alias_set_type subset)
925 alias_set_entry superset_entry;
926 alias_set_entry subset_entry;
928 /* It is possible in complex type situations for both sets to be the same,
929 in which case we can ignore this operation. */
930 if (superset == subset)
931 return;
933 gcc_assert (superset);
935 superset_entry = get_alias_set_entry (superset);
936 if (superset_entry == 0)
938 /* Create an entry for the SUPERSET, so that we have a place to
939 attach the SUBSET. */
940 superset_entry = ggc_alloc_cleared_alias_set_entry_d ();
941 superset_entry->alias_set = superset;
942 superset_entry->children
943 = splay_tree_new_ggc (splay_tree_compare_ints,
944 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s,
945 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s);
946 superset_entry->has_zero_child = 0;
947 (*alias_sets)[superset] = superset_entry;
950 if (subset == 0)
951 superset_entry->has_zero_child = 1;
952 else
954 subset_entry = get_alias_set_entry (subset);
955 /* If there is an entry for the subset, enter all of its children
956 (if they are not already present) as children of the SUPERSET. */
957 if (subset_entry)
959 if (subset_entry->has_zero_child)
960 superset_entry->has_zero_child = 1;
962 splay_tree_foreach (subset_entry->children, insert_subset_children,
963 superset_entry->children);
966 /* Enter the SUBSET itself as a child of the SUPERSET. */
967 splay_tree_insert (superset_entry->children,
968 (splay_tree_key) subset, 0);
972 /* Record that component types of TYPE, if any, are part of that type for
973 aliasing purposes. For record types, we only record component types
974 for fields that are not marked non-addressable. For array types, we
975 only record the component type if it is not marked non-aliased. */
977 void
978 record_component_aliases (tree type)
980 alias_set_type superset = get_alias_set (type);
981 tree field;
983 if (superset == 0)
984 return;
986 switch (TREE_CODE (type))
988 case RECORD_TYPE:
989 case UNION_TYPE:
990 case QUAL_UNION_TYPE:
991 /* Recursively record aliases for the base classes, if there are any. */
992 if (TYPE_BINFO (type))
994 int i;
995 tree binfo, base_binfo;
997 for (binfo = TYPE_BINFO (type), i = 0;
998 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
999 record_alias_subset (superset,
1000 get_alias_set (BINFO_TYPE (base_binfo)));
1002 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1003 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1004 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1005 break;
1007 case COMPLEX_TYPE:
1008 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1009 break;
1011 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1012 element type. */
1014 default:
1015 break;
1019 /* Allocate an alias set for use in storing and reading from the varargs
1020 spill area. */
1022 static GTY(()) alias_set_type varargs_set = -1;
1024 alias_set_type
1025 get_varargs_alias_set (void)
1027 #if 1
1028 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1029 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1030 consistently use the varargs alias set for loads from the varargs
1031 area. So don't use it anywhere. */
1032 return 0;
1033 #else
1034 if (varargs_set == -1)
1035 varargs_set = new_alias_set ();
1037 return varargs_set;
1038 #endif
1041 /* Likewise, but used for the fixed portions of the frame, e.g., register
1042 save areas. */
1044 static GTY(()) alias_set_type frame_set = -1;
1046 alias_set_type
1047 get_frame_alias_set (void)
1049 if (frame_set == -1)
1050 frame_set = new_alias_set ();
1052 return frame_set;
1055 /* Create a new, unique base with id ID. */
1057 static rtx
1058 unique_base_value (HOST_WIDE_INT id)
1060 return gen_rtx_ADDRESS (Pmode, id);
1063 /* Return true if accesses based on any other base value cannot alias
1064 those based on X. */
1066 static bool
1067 unique_base_value_p (rtx x)
1069 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1072 /* Return true if X is known to be a base value. */
1074 static bool
1075 known_base_value_p (rtx x)
1077 switch (GET_CODE (x))
1079 case LABEL_REF:
1080 case SYMBOL_REF:
1081 return true;
1083 case ADDRESS:
1084 /* Arguments may or may not be bases; we don't know for sure. */
1085 return GET_MODE (x) != VOIDmode;
1087 default:
1088 return false;
1092 /* Inside SRC, the source of a SET, find a base address. */
1094 static rtx
1095 find_base_value (rtx src)
1097 unsigned int regno;
1099 #if defined (FIND_BASE_TERM)
1100 /* Try machine-dependent ways to find the base term. */
1101 src = FIND_BASE_TERM (src);
1102 #endif
1104 switch (GET_CODE (src))
1106 case SYMBOL_REF:
1107 case LABEL_REF:
1108 return src;
1110 case REG:
1111 regno = REGNO (src);
1112 /* At the start of a function, argument registers have known base
1113 values which may be lost later. Returning an ADDRESS
1114 expression here allows optimization based on argument values
1115 even when the argument registers are used for other purposes. */
1116 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1117 return new_reg_base_value[regno];
1119 /* If a pseudo has a known base value, return it. Do not do this
1120 for non-fixed hard regs since it can result in a circular
1121 dependency chain for registers which have values at function entry.
1123 The test above is not sufficient because the scheduler may move
1124 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1125 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1126 && regno < vec_safe_length (reg_base_value))
1128 /* If we're inside init_alias_analysis, use new_reg_base_value
1129 to reduce the number of relaxation iterations. */
1130 if (new_reg_base_value && new_reg_base_value[regno]
1131 && DF_REG_DEF_COUNT (regno) == 1)
1132 return new_reg_base_value[regno];
1134 if ((*reg_base_value)[regno])
1135 return (*reg_base_value)[regno];
1138 return 0;
1140 case MEM:
1141 /* Check for an argument passed in memory. Only record in the
1142 copying-arguments block; it is too hard to track changes
1143 otherwise. */
1144 if (copying_arguments
1145 && (XEXP (src, 0) == arg_pointer_rtx
1146 || (GET_CODE (XEXP (src, 0)) == PLUS
1147 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1148 return arg_base_value;
1149 return 0;
1151 case CONST:
1152 src = XEXP (src, 0);
1153 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1154 break;
1156 /* ... fall through ... */
1158 case PLUS:
1159 case MINUS:
1161 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1163 /* If either operand is a REG that is a known pointer, then it
1164 is the base. */
1165 if (REG_P (src_0) && REG_POINTER (src_0))
1166 return find_base_value (src_0);
1167 if (REG_P (src_1) && REG_POINTER (src_1))
1168 return find_base_value (src_1);
1170 /* If either operand is a REG, then see if we already have
1171 a known value for it. */
1172 if (REG_P (src_0))
1174 temp = find_base_value (src_0);
1175 if (temp != 0)
1176 src_0 = temp;
1179 if (REG_P (src_1))
1181 temp = find_base_value (src_1);
1182 if (temp!= 0)
1183 src_1 = temp;
1186 /* If either base is named object or a special address
1187 (like an argument or stack reference), then use it for the
1188 base term. */
1189 if (src_0 != 0 && known_base_value_p (src_0))
1190 return src_0;
1192 if (src_1 != 0 && known_base_value_p (src_1))
1193 return src_1;
1195 /* Guess which operand is the base address:
1196 If either operand is a symbol, then it is the base. If
1197 either operand is a CONST_INT, then the other is the base. */
1198 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1199 return find_base_value (src_0);
1200 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1201 return find_base_value (src_1);
1203 return 0;
1206 case LO_SUM:
1207 /* The standard form is (lo_sum reg sym) so look only at the
1208 second operand. */
1209 return find_base_value (XEXP (src, 1));
1211 case AND:
1212 /* If the second operand is constant set the base
1213 address to the first operand. */
1214 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1215 return find_base_value (XEXP (src, 0));
1216 return 0;
1218 case TRUNCATE:
1219 /* As we do not know which address space the pointer is referring to, we can
1220 handle this only if the target does not support different pointer or
1221 address modes depending on the address space. */
1222 if (!target_default_pointer_address_modes_p ())
1223 break;
1224 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1225 break;
1226 /* Fall through. */
1227 case HIGH:
1228 case PRE_INC:
1229 case PRE_DEC:
1230 case POST_INC:
1231 case POST_DEC:
1232 case PRE_MODIFY:
1233 case POST_MODIFY:
1234 return find_base_value (XEXP (src, 0));
1236 case ZERO_EXTEND:
1237 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1238 /* As we do not know which address space the pointer is referring to, we can
1239 handle this only if the target does not support different pointer or
1240 address modes depending on the address space. */
1241 if (!target_default_pointer_address_modes_p ())
1242 break;
1245 rtx temp = find_base_value (XEXP (src, 0));
1247 if (temp != 0 && CONSTANT_P (temp))
1248 temp = convert_memory_address (Pmode, temp);
1250 return temp;
1253 default:
1254 break;
1257 return 0;
1260 /* Called from init_alias_analysis indirectly through note_stores,
1261 or directly if DEST is a register with a REG_NOALIAS note attached.
1262 SET is null in the latter case. */
1264 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1265 register N has been set in this function. */
1266 static sbitmap reg_seen;
1268 static void
1269 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1271 unsigned regno;
1272 rtx src;
1273 int n;
1275 if (!REG_P (dest))
1276 return;
1278 regno = REGNO (dest);
1280 gcc_checking_assert (regno < reg_base_value->length ());
1282 /* If this spans multiple hard registers, then we must indicate that every
1283 register has an unusable value. */
1284 if (regno < FIRST_PSEUDO_REGISTER)
1285 n = hard_regno_nregs[regno][GET_MODE (dest)];
1286 else
1287 n = 1;
1288 if (n != 1)
1290 while (--n >= 0)
1292 bitmap_set_bit (reg_seen, regno + n);
1293 new_reg_base_value[regno + n] = 0;
1295 return;
1298 if (set)
1300 /* A CLOBBER wipes out any old value but does not prevent a previously
1301 unset register from acquiring a base address (i.e. reg_seen is not
1302 set). */
1303 if (GET_CODE (set) == CLOBBER)
1305 new_reg_base_value[regno] = 0;
1306 return;
1308 src = SET_SRC (set);
1310 else
1312 /* There's a REG_NOALIAS note against DEST. */
1313 if (bitmap_bit_p (reg_seen, regno))
1315 new_reg_base_value[regno] = 0;
1316 return;
1318 bitmap_set_bit (reg_seen, regno);
1319 new_reg_base_value[regno] = unique_base_value (unique_id++);
1320 return;
1323 /* If this is not the first set of REGNO, see whether the new value
1324 is related to the old one. There are two cases of interest:
1326 (1) The register might be assigned an entirely new value
1327 that has the same base term as the original set.
1329 (2) The set might be a simple self-modification that
1330 cannot change REGNO's base value.
1332 If neither case holds, reject the original base value as invalid.
1333 Note that the following situation is not detected:
1335 extern int x, y; int *p = &x; p += (&y-&x);
1337 ANSI C does not allow computing the difference of addresses
1338 of distinct top level objects. */
1339 if (new_reg_base_value[regno] != 0
1340 && find_base_value (src) != new_reg_base_value[regno])
1341 switch (GET_CODE (src))
1343 case LO_SUM:
1344 case MINUS:
1345 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1346 new_reg_base_value[regno] = 0;
1347 break;
1348 case PLUS:
1349 /* If the value we add in the PLUS is also a valid base value,
1350 this might be the actual base value, and the original value
1351 an index. */
1353 rtx other = NULL_RTX;
1355 if (XEXP (src, 0) == dest)
1356 other = XEXP (src, 1);
1357 else if (XEXP (src, 1) == dest)
1358 other = XEXP (src, 0);
1360 if (! other || find_base_value (other))
1361 new_reg_base_value[regno] = 0;
1362 break;
1364 case AND:
1365 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1366 new_reg_base_value[regno] = 0;
1367 break;
1368 default:
1369 new_reg_base_value[regno] = 0;
1370 break;
1372 /* If this is the first set of a register, record the value. */
1373 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1374 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1375 new_reg_base_value[regno] = find_base_value (src);
1377 bitmap_set_bit (reg_seen, regno);
1380 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1381 using hard registers with non-null REG_BASE_VALUE for renaming. */
1383 get_reg_base_value (unsigned int regno)
1385 return (*reg_base_value)[regno];
1388 /* If a value is known for REGNO, return it. */
1391 get_reg_known_value (unsigned int regno)
1393 if (regno >= FIRST_PSEUDO_REGISTER)
1395 regno -= FIRST_PSEUDO_REGISTER;
1396 if (regno < vec_safe_length (reg_known_value))
1397 return (*reg_known_value)[regno];
1399 return NULL;
1402 /* Set it. */
1404 static void
1405 set_reg_known_value (unsigned int regno, rtx val)
1407 if (regno >= FIRST_PSEUDO_REGISTER)
1409 regno -= FIRST_PSEUDO_REGISTER;
1410 if (regno < vec_safe_length (reg_known_value))
1411 (*reg_known_value)[regno] = val;
1415 /* Similarly for reg_known_equiv_p. */
1417 bool
1418 get_reg_known_equiv_p (unsigned int regno)
1420 if (regno >= FIRST_PSEUDO_REGISTER)
1422 regno -= FIRST_PSEUDO_REGISTER;
1423 if (regno < vec_safe_length (reg_known_value))
1424 return bitmap_bit_p (reg_known_equiv_p, regno);
1426 return false;
1429 static void
1430 set_reg_known_equiv_p (unsigned int regno, bool val)
1432 if (regno >= FIRST_PSEUDO_REGISTER)
1434 regno -= FIRST_PSEUDO_REGISTER;
1435 if (regno < vec_safe_length (reg_known_value))
1437 if (val)
1438 bitmap_set_bit (reg_known_equiv_p, regno);
1439 else
1440 bitmap_clear_bit (reg_known_equiv_p, regno);
1446 /* Returns a canonical version of X, from the point of view alias
1447 analysis. (For example, if X is a MEM whose address is a register,
1448 and the register has a known value (say a SYMBOL_REF), then a MEM
1449 whose address is the SYMBOL_REF is returned.) */
1452 canon_rtx (rtx x)
1454 /* Recursively look for equivalences. */
1455 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1457 rtx t = get_reg_known_value (REGNO (x));
1458 if (t == x)
1459 return x;
1460 if (t)
1461 return canon_rtx (t);
1464 if (GET_CODE (x) == PLUS)
1466 rtx x0 = canon_rtx (XEXP (x, 0));
1467 rtx x1 = canon_rtx (XEXP (x, 1));
1469 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1471 if (CONST_INT_P (x0))
1472 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1473 else if (CONST_INT_P (x1))
1474 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1475 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1479 /* This gives us much better alias analysis when called from
1480 the loop optimizer. Note we want to leave the original
1481 MEM alone, but need to return the canonicalized MEM with
1482 all the flags with their original values. */
1483 else if (MEM_P (x))
1484 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1486 return x;
1489 /* Return 1 if X and Y are identical-looking rtx's.
1490 Expect that X and Y has been already canonicalized.
1492 We use the data in reg_known_value above to see if two registers with
1493 different numbers are, in fact, equivalent. */
1495 static int
1496 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1498 int i;
1499 int j;
1500 enum rtx_code code;
1501 const char *fmt;
1503 if (x == 0 && y == 0)
1504 return 1;
1505 if (x == 0 || y == 0)
1506 return 0;
1508 if (x == y)
1509 return 1;
1511 code = GET_CODE (x);
1512 /* Rtx's of different codes cannot be equal. */
1513 if (code != GET_CODE (y))
1514 return 0;
1516 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1517 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1519 if (GET_MODE (x) != GET_MODE (y))
1520 return 0;
1522 /* Some RTL can be compared without a recursive examination. */
1523 switch (code)
1525 case REG:
1526 return REGNO (x) == REGNO (y);
1528 case LABEL_REF:
1529 return XEXP (x, 0) == XEXP (y, 0);
1531 case SYMBOL_REF:
1532 return XSTR (x, 0) == XSTR (y, 0);
1534 case ENTRY_VALUE:
1535 /* This is magic, don't go through canonicalization et al. */
1536 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1538 case VALUE:
1539 CASE_CONST_UNIQUE:
1540 /* There's no need to compare the contents of CONST_DOUBLEs or
1541 CONST_INTs because pointer equality is a good enough
1542 comparison for these nodes. */
1543 return 0;
1545 default:
1546 break;
1549 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1550 if (code == PLUS)
1551 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1552 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1553 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1554 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1555 /* For commutative operations, the RTX match if the operand match in any
1556 order. Also handle the simple binary and unary cases without a loop. */
1557 if (COMMUTATIVE_P (x))
1559 rtx xop0 = canon_rtx (XEXP (x, 0));
1560 rtx yop0 = canon_rtx (XEXP (y, 0));
1561 rtx yop1 = canon_rtx (XEXP (y, 1));
1563 return ((rtx_equal_for_memref_p (xop0, yop0)
1564 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1565 || (rtx_equal_for_memref_p (xop0, yop1)
1566 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1568 else if (NON_COMMUTATIVE_P (x))
1570 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1571 canon_rtx (XEXP (y, 0)))
1572 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1573 canon_rtx (XEXP (y, 1))));
1575 else if (UNARY_P (x))
1576 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1577 canon_rtx (XEXP (y, 0)));
1579 /* Compare the elements. If any pair of corresponding elements
1580 fail to match, return 0 for the whole things.
1582 Limit cases to types which actually appear in addresses. */
1584 fmt = GET_RTX_FORMAT (code);
1585 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1587 switch (fmt[i])
1589 case 'i':
1590 if (XINT (x, i) != XINT (y, i))
1591 return 0;
1592 break;
1594 case 'E':
1595 /* Two vectors must have the same length. */
1596 if (XVECLEN (x, i) != XVECLEN (y, i))
1597 return 0;
1599 /* And the corresponding elements must match. */
1600 for (j = 0; j < XVECLEN (x, i); j++)
1601 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1602 canon_rtx (XVECEXP (y, i, j))) == 0)
1603 return 0;
1604 break;
1606 case 'e':
1607 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1608 canon_rtx (XEXP (y, i))) == 0)
1609 return 0;
1610 break;
1612 /* This can happen for asm operands. */
1613 case 's':
1614 if (strcmp (XSTR (x, i), XSTR (y, i)))
1615 return 0;
1616 break;
1618 /* This can happen for an asm which clobbers memory. */
1619 case '0':
1620 break;
1622 /* It is believed that rtx's at this level will never
1623 contain anything but integers and other rtx's,
1624 except for within LABEL_REFs and SYMBOL_REFs. */
1625 default:
1626 gcc_unreachable ();
1629 return 1;
1632 static rtx
1633 find_base_term (rtx x)
1635 cselib_val *val;
1636 struct elt_loc_list *l, *f;
1637 rtx ret;
1639 #if defined (FIND_BASE_TERM)
1640 /* Try machine-dependent ways to find the base term. */
1641 x = FIND_BASE_TERM (x);
1642 #endif
1644 switch (GET_CODE (x))
1646 case REG:
1647 return REG_BASE_VALUE (x);
1649 case TRUNCATE:
1650 /* As we do not know which address space the pointer is referring to, we can
1651 handle this only if the target does not support different pointer or
1652 address modes depending on the address space. */
1653 if (!target_default_pointer_address_modes_p ())
1654 return 0;
1655 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1656 return 0;
1657 /* Fall through. */
1658 case HIGH:
1659 case PRE_INC:
1660 case PRE_DEC:
1661 case POST_INC:
1662 case POST_DEC:
1663 case PRE_MODIFY:
1664 case POST_MODIFY:
1665 return find_base_term (XEXP (x, 0));
1667 case ZERO_EXTEND:
1668 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1669 /* As we do not know which address space the pointer is referring to, we can
1670 handle this only if the target does not support different pointer or
1671 address modes depending on the address space. */
1672 if (!target_default_pointer_address_modes_p ())
1673 return 0;
1676 rtx temp = find_base_term (XEXP (x, 0));
1678 if (temp != 0 && CONSTANT_P (temp))
1679 temp = convert_memory_address (Pmode, temp);
1681 return temp;
1684 case VALUE:
1685 val = CSELIB_VAL_PTR (x);
1686 ret = NULL_RTX;
1688 if (!val)
1689 return ret;
1691 if (cselib_sp_based_value_p (val))
1692 return static_reg_base_value[STACK_POINTER_REGNUM];
1694 f = val->locs;
1695 /* Temporarily reset val->locs to avoid infinite recursion. */
1696 val->locs = NULL;
1698 for (l = f; l; l = l->next)
1699 if (GET_CODE (l->loc) == VALUE
1700 && CSELIB_VAL_PTR (l->loc)->locs
1701 && !CSELIB_VAL_PTR (l->loc)->locs->next
1702 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1703 continue;
1704 else if ((ret = find_base_term (l->loc)) != 0)
1705 break;
1707 val->locs = f;
1708 return ret;
1710 case LO_SUM:
1711 /* The standard form is (lo_sum reg sym) so look only at the
1712 second operand. */
1713 return find_base_term (XEXP (x, 1));
1715 case CONST:
1716 x = XEXP (x, 0);
1717 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1718 return 0;
1719 /* Fall through. */
1720 case PLUS:
1721 case MINUS:
1723 rtx tmp1 = XEXP (x, 0);
1724 rtx tmp2 = XEXP (x, 1);
1726 /* This is a little bit tricky since we have to determine which of
1727 the two operands represents the real base address. Otherwise this
1728 routine may return the index register instead of the base register.
1730 That may cause us to believe no aliasing was possible, when in
1731 fact aliasing is possible.
1733 We use a few simple tests to guess the base register. Additional
1734 tests can certainly be added. For example, if one of the operands
1735 is a shift or multiply, then it must be the index register and the
1736 other operand is the base register. */
1738 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1739 return find_base_term (tmp2);
1741 /* If either operand is known to be a pointer, then prefer it
1742 to determine the base term. */
1743 if (REG_P (tmp1) && REG_POINTER (tmp1))
1745 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1747 rtx tem = tmp1;
1748 tmp1 = tmp2;
1749 tmp2 = tem;
1752 /* Go ahead and find the base term for both operands. If either base
1753 term is from a pointer or is a named object or a special address
1754 (like an argument or stack reference), then use it for the
1755 base term. */
1756 rtx base = find_base_term (tmp1);
1757 if (base != NULL_RTX
1758 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1759 || known_base_value_p (base)))
1760 return base;
1761 base = find_base_term (tmp2);
1762 if (base != NULL_RTX
1763 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1764 || known_base_value_p (base)))
1765 return base;
1767 /* We could not determine which of the two operands was the
1768 base register and which was the index. So we can determine
1769 nothing from the base alias check. */
1770 return 0;
1773 case AND:
1774 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1775 return find_base_term (XEXP (x, 0));
1776 return 0;
1778 case SYMBOL_REF:
1779 case LABEL_REF:
1780 return x;
1782 default:
1783 return 0;
1787 /* Return true if accesses to address X may alias accesses based
1788 on the stack pointer. */
1790 bool
1791 may_be_sp_based_p (rtx x)
1793 rtx base = find_base_term (x);
1794 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1797 /* Return 0 if the addresses X and Y are known to point to different
1798 objects, 1 if they might be pointers to the same object. */
1800 static int
1801 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1802 enum machine_mode x_mode, enum machine_mode y_mode)
1804 /* If the address itself has no known base see if a known equivalent
1805 value has one. If either address still has no known base, nothing
1806 is known about aliasing. */
1807 if (x_base == 0)
1809 rtx x_c;
1811 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1812 return 1;
1814 x_base = find_base_term (x_c);
1815 if (x_base == 0)
1816 return 1;
1819 if (y_base == 0)
1821 rtx y_c;
1822 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1823 return 1;
1825 y_base = find_base_term (y_c);
1826 if (y_base == 0)
1827 return 1;
1830 /* If the base addresses are equal nothing is known about aliasing. */
1831 if (rtx_equal_p (x_base, y_base))
1832 return 1;
1834 /* The base addresses are different expressions. If they are not accessed
1835 via AND, there is no conflict. We can bring knowledge of object
1836 alignment into play here. For example, on alpha, "char a, b;" can
1837 alias one another, though "char a; long b;" cannot. AND addesses may
1838 implicitly alias surrounding objects; i.e. unaligned access in DImode
1839 via AND address can alias all surrounding object types except those
1840 with aligment 8 or higher. */
1841 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1842 return 1;
1843 if (GET_CODE (x) == AND
1844 && (!CONST_INT_P (XEXP (x, 1))
1845 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1846 return 1;
1847 if (GET_CODE (y) == AND
1848 && (!CONST_INT_P (XEXP (y, 1))
1849 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1850 return 1;
1852 /* Differing symbols not accessed via AND never alias. */
1853 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1854 return 0;
1856 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
1857 return 0;
1859 return 1;
1862 /* Callback for for_each_rtx, that returns 1 upon encountering a VALUE
1863 whose UID is greater than the int uid that D points to. */
1865 static int
1866 refs_newer_value_cb (rtx *x, void *d)
1868 if (GET_CODE (*x) == VALUE && CSELIB_VAL_PTR (*x)->uid > *(int *)d)
1869 return 1;
1871 return 0;
1874 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1875 that of V. */
1877 static bool
1878 refs_newer_value_p (rtx expr, rtx v)
1880 int minuid = CSELIB_VAL_PTR (v)->uid;
1882 return for_each_rtx (&expr, refs_newer_value_cb, &minuid);
1885 /* Convert the address X into something we can use. This is done by returning
1886 it unchanged unless it is a value; in the latter case we call cselib to get
1887 a more useful rtx. */
1890 get_addr (rtx x)
1892 cselib_val *v;
1893 struct elt_loc_list *l;
1895 if (GET_CODE (x) != VALUE)
1896 return x;
1897 v = CSELIB_VAL_PTR (x);
1898 if (v)
1900 bool have_equivs = cselib_have_permanent_equivalences ();
1901 if (have_equivs)
1902 v = canonical_cselib_val (v);
1903 for (l = v->locs; l; l = l->next)
1904 if (CONSTANT_P (l->loc))
1905 return l->loc;
1906 for (l = v->locs; l; l = l->next)
1907 if (!REG_P (l->loc) && !MEM_P (l->loc)
1908 /* Avoid infinite recursion when potentially dealing with
1909 var-tracking artificial equivalences, by skipping the
1910 equivalences themselves, and not choosing expressions
1911 that refer to newer VALUEs. */
1912 && (!have_equivs
1913 || (GET_CODE (l->loc) != VALUE
1914 && !refs_newer_value_p (l->loc, x))))
1915 return l->loc;
1916 if (have_equivs)
1918 for (l = v->locs; l; l = l->next)
1919 if (REG_P (l->loc)
1920 || (GET_CODE (l->loc) != VALUE
1921 && !refs_newer_value_p (l->loc, x)))
1922 return l->loc;
1923 /* Return the canonical value. */
1924 return v->val_rtx;
1926 if (v->locs)
1927 return v->locs->loc;
1929 return x;
1932 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1933 where SIZE is the size in bytes of the memory reference. If ADDR
1934 is not modified by the memory reference then ADDR is returned. */
1936 static rtx
1937 addr_side_effect_eval (rtx addr, int size, int n_refs)
1939 int offset = 0;
1941 switch (GET_CODE (addr))
1943 case PRE_INC:
1944 offset = (n_refs + 1) * size;
1945 break;
1946 case PRE_DEC:
1947 offset = -(n_refs + 1) * size;
1948 break;
1949 case POST_INC:
1950 offset = n_refs * size;
1951 break;
1952 case POST_DEC:
1953 offset = -n_refs * size;
1954 break;
1956 default:
1957 return addr;
1960 if (offset)
1961 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1962 gen_int_mode (offset, GET_MODE (addr)));
1963 else
1964 addr = XEXP (addr, 0);
1965 addr = canon_rtx (addr);
1967 return addr;
1970 /* Return TRUE if an object X sized at XSIZE bytes and another object
1971 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1972 any of the sizes is zero, assume an overlap, otherwise use the
1973 absolute value of the sizes as the actual sizes. */
1975 static inline bool
1976 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
1978 return (xsize == 0 || ysize == 0
1979 || (c >= 0
1980 ? (abs (xsize) > c)
1981 : (abs (ysize) > -c)));
1984 /* Return one if X and Y (memory addresses) reference the
1985 same location in memory or if the references overlap.
1986 Return zero if they do not overlap, else return
1987 minus one in which case they still might reference the same location.
1989 C is an offset accumulator. When
1990 C is nonzero, we are testing aliases between X and Y + C.
1991 XSIZE is the size in bytes of the X reference,
1992 similarly YSIZE is the size in bytes for Y.
1993 Expect that canon_rtx has been already called for X and Y.
1995 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1996 referenced (the reference was BLKmode), so make the most pessimistic
1997 assumptions.
1999 If XSIZE or YSIZE is negative, we may access memory outside the object
2000 being referenced as a side effect. This can happen when using AND to
2001 align memory references, as is done on the Alpha.
2003 Nice to notice that varying addresses cannot conflict with fp if no
2004 local variables had their addresses taken, but that's too hard now.
2006 ??? Contrary to the tree alias oracle this does not return
2007 one for X + non-constant and Y + non-constant when X and Y are equal.
2008 If that is fixed the TBAA hack for union type-punning can be removed. */
2010 static int
2011 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2013 if (GET_CODE (x) == VALUE)
2015 if (REG_P (y))
2017 struct elt_loc_list *l = NULL;
2018 if (CSELIB_VAL_PTR (x))
2019 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2020 l; l = l->next)
2021 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2022 break;
2023 if (l)
2024 x = y;
2025 else
2026 x = get_addr (x);
2028 /* Don't call get_addr if y is the same VALUE. */
2029 else if (x != y)
2030 x = get_addr (x);
2032 if (GET_CODE (y) == VALUE)
2034 if (REG_P (x))
2036 struct elt_loc_list *l = NULL;
2037 if (CSELIB_VAL_PTR (y))
2038 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2039 l; l = l->next)
2040 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2041 break;
2042 if (l)
2043 y = x;
2044 else
2045 y = get_addr (y);
2047 /* Don't call get_addr if x is the same VALUE. */
2048 else if (y != x)
2049 y = get_addr (y);
2051 if (GET_CODE (x) == HIGH)
2052 x = XEXP (x, 0);
2053 else if (GET_CODE (x) == LO_SUM)
2054 x = XEXP (x, 1);
2055 else
2056 x = addr_side_effect_eval (x, abs (xsize), 0);
2057 if (GET_CODE (y) == HIGH)
2058 y = XEXP (y, 0);
2059 else if (GET_CODE (y) == LO_SUM)
2060 y = XEXP (y, 1);
2061 else
2062 y = addr_side_effect_eval (y, abs (ysize), 0);
2064 if (rtx_equal_for_memref_p (x, y))
2066 return offset_overlap_p (c, xsize, ysize);
2069 /* This code used to check for conflicts involving stack references and
2070 globals but the base address alias code now handles these cases. */
2072 if (GET_CODE (x) == PLUS)
2074 /* The fact that X is canonicalized means that this
2075 PLUS rtx is canonicalized. */
2076 rtx x0 = XEXP (x, 0);
2077 rtx x1 = XEXP (x, 1);
2079 if (GET_CODE (y) == PLUS)
2081 /* The fact that Y is canonicalized means that this
2082 PLUS rtx is canonicalized. */
2083 rtx y0 = XEXP (y, 0);
2084 rtx y1 = XEXP (y, 1);
2086 if (rtx_equal_for_memref_p (x1, y1))
2087 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2088 if (rtx_equal_for_memref_p (x0, y0))
2089 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2090 if (CONST_INT_P (x1))
2092 if (CONST_INT_P (y1))
2093 return memrefs_conflict_p (xsize, x0, ysize, y0,
2094 c - INTVAL (x1) + INTVAL (y1));
2095 else
2096 return memrefs_conflict_p (xsize, x0, ysize, y,
2097 c - INTVAL (x1));
2099 else if (CONST_INT_P (y1))
2100 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2102 return -1;
2104 else if (CONST_INT_P (x1))
2105 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2107 else if (GET_CODE (y) == PLUS)
2109 /* The fact that Y is canonicalized means that this
2110 PLUS rtx is canonicalized. */
2111 rtx y0 = XEXP (y, 0);
2112 rtx y1 = XEXP (y, 1);
2114 if (CONST_INT_P (y1))
2115 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2116 else
2117 return -1;
2120 if (GET_CODE (x) == GET_CODE (y))
2121 switch (GET_CODE (x))
2123 case MULT:
2125 /* Handle cases where we expect the second operands to be the
2126 same, and check only whether the first operand would conflict
2127 or not. */
2128 rtx x0, y0;
2129 rtx x1 = canon_rtx (XEXP (x, 1));
2130 rtx y1 = canon_rtx (XEXP (y, 1));
2131 if (! rtx_equal_for_memref_p (x1, y1))
2132 return -1;
2133 x0 = canon_rtx (XEXP (x, 0));
2134 y0 = canon_rtx (XEXP (y, 0));
2135 if (rtx_equal_for_memref_p (x0, y0))
2136 return offset_overlap_p (c, xsize, ysize);
2138 /* Can't properly adjust our sizes. */
2139 if (!CONST_INT_P (x1))
2140 return -1;
2141 xsize /= INTVAL (x1);
2142 ysize /= INTVAL (x1);
2143 c /= INTVAL (x1);
2144 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2147 default:
2148 break;
2151 /* Deal with alignment ANDs by adjusting offset and size so as to
2152 cover the maximum range, without taking any previously known
2153 alignment into account. Make a size negative after such an
2154 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2155 assume a potential overlap, because they may end up in contiguous
2156 memory locations and the stricter-alignment access may span over
2157 part of both. */
2158 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2160 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2161 unsigned HOST_WIDE_INT uc = sc;
2162 if (sc < 0 && -uc == (uc & -uc))
2164 if (xsize > 0)
2165 xsize = -xsize;
2166 if (xsize)
2167 xsize += sc + 1;
2168 c -= sc + 1;
2169 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2170 ysize, y, c);
2173 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2175 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2176 unsigned HOST_WIDE_INT uc = sc;
2177 if (sc < 0 && -uc == (uc & -uc))
2179 if (ysize > 0)
2180 ysize = -ysize;
2181 if (ysize)
2182 ysize += sc + 1;
2183 c += sc + 1;
2184 return memrefs_conflict_p (xsize, x,
2185 ysize, canon_rtx (XEXP (y, 0)), c);
2189 if (CONSTANT_P (x))
2191 if (CONST_INT_P (x) && CONST_INT_P (y))
2193 c += (INTVAL (y) - INTVAL (x));
2194 return offset_overlap_p (c, xsize, ysize);
2197 if (GET_CODE (x) == CONST)
2199 if (GET_CODE (y) == CONST)
2200 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2201 ysize, canon_rtx (XEXP (y, 0)), c);
2202 else
2203 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2204 ysize, y, c);
2206 if (GET_CODE (y) == CONST)
2207 return memrefs_conflict_p (xsize, x, ysize,
2208 canon_rtx (XEXP (y, 0)), c);
2210 /* Assume a potential overlap for symbolic addresses that went
2211 through alignment adjustments (i.e., that have negative
2212 sizes), because we can't know how far they are from each
2213 other. */
2214 if (CONSTANT_P (y))
2215 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2217 return -1;
2220 return -1;
2223 /* Functions to compute memory dependencies.
2225 Since we process the insns in execution order, we can build tables
2226 to keep track of what registers are fixed (and not aliased), what registers
2227 are varying in known ways, and what registers are varying in unknown
2228 ways.
2230 If both memory references are volatile, then there must always be a
2231 dependence between the two references, since their order can not be
2232 changed. A volatile and non-volatile reference can be interchanged
2233 though.
2235 We also must allow AND addresses, because they may generate accesses
2236 outside the object being referenced. This is used to generate aligned
2237 addresses from unaligned addresses, for instance, the alpha
2238 storeqi_unaligned pattern. */
2240 /* Read dependence: X is read after read in MEM takes place. There can
2241 only be a dependence here if both reads are volatile, or if either is
2242 an explicit barrier. */
2245 read_dependence (const_rtx mem, const_rtx x)
2247 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2248 return true;
2249 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2250 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2251 return true;
2252 return false;
2255 /* Return true if we can determine that the fields referenced cannot
2256 overlap for any pair of objects. */
2258 static bool
2259 nonoverlapping_component_refs_p (const_rtx rtlx, const_rtx rtly)
2261 const_tree x = MEM_EXPR (rtlx), y = MEM_EXPR (rtly);
2262 const_tree fieldx, fieldy, typex, typey, orig_y;
2264 if (!flag_strict_aliasing
2265 || !x || !y
2266 || TREE_CODE (x) != COMPONENT_REF
2267 || TREE_CODE (y) != COMPONENT_REF)
2268 return false;
2272 /* The comparison has to be done at a common type, since we don't
2273 know how the inheritance hierarchy works. */
2274 orig_y = y;
2277 fieldx = TREE_OPERAND (x, 1);
2278 typex = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx));
2280 y = orig_y;
2283 fieldy = TREE_OPERAND (y, 1);
2284 typey = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy));
2286 if (typex == typey)
2287 goto found;
2289 y = TREE_OPERAND (y, 0);
2291 while (y && TREE_CODE (y) == COMPONENT_REF);
2293 x = TREE_OPERAND (x, 0);
2295 while (x && TREE_CODE (x) == COMPONENT_REF);
2296 /* Never found a common type. */
2297 return false;
2299 found:
2300 /* If we're left with accessing different fields of a structure, then no
2301 possible overlap, unless they are both bitfields. */
2302 if (TREE_CODE (typex) == RECORD_TYPE && fieldx != fieldy)
2303 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
2305 /* The comparison on the current field failed. If we're accessing
2306 a very nested structure, look at the next outer level. */
2307 x = TREE_OPERAND (x, 0);
2308 y = TREE_OPERAND (y, 0);
2310 while (x && y
2311 && TREE_CODE (x) == COMPONENT_REF
2312 && TREE_CODE (y) == COMPONENT_REF);
2314 return false;
2317 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2319 static tree
2320 decl_for_component_ref (tree x)
2324 x = TREE_OPERAND (x, 0);
2326 while (x && TREE_CODE (x) == COMPONENT_REF);
2328 return x && DECL_P (x) ? x : NULL_TREE;
2331 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2332 for the offset of the field reference. *KNOWN_P says whether the
2333 offset is known. */
2335 static void
2336 adjust_offset_for_component_ref (tree x, bool *known_p,
2337 HOST_WIDE_INT *offset)
2339 if (!*known_p)
2340 return;
2343 tree xoffset = component_ref_field_offset (x);
2344 tree field = TREE_OPERAND (x, 1);
2346 if (! host_integerp (xoffset, 1))
2348 *known_p = false;
2349 return;
2351 *offset += (tree_low_cst (xoffset, 1)
2352 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
2353 / BITS_PER_UNIT));
2355 x = TREE_OPERAND (x, 0);
2357 while (x && TREE_CODE (x) == COMPONENT_REF);
2360 /* Return nonzero if we can determine the exprs corresponding to memrefs
2361 X and Y and they do not overlap.
2362 If LOOP_VARIANT is set, skip offset-based disambiguation */
2365 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2367 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2368 rtx rtlx, rtly;
2369 rtx basex, basey;
2370 bool moffsetx_known_p, moffsety_known_p;
2371 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2372 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2374 /* Unless both have exprs, we can't tell anything. */
2375 if (exprx == 0 || expry == 0)
2376 return 0;
2378 /* For spill-slot accesses make sure we have valid offsets. */
2379 if ((exprx == get_spill_slot_decl (false)
2380 && ! MEM_OFFSET_KNOWN_P (x))
2381 || (expry == get_spill_slot_decl (false)
2382 && ! MEM_OFFSET_KNOWN_P (y)))
2383 return 0;
2385 /* If the field reference test failed, look at the DECLs involved. */
2386 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2387 if (moffsetx_known_p)
2388 moffsetx = MEM_OFFSET (x);
2389 if (TREE_CODE (exprx) == COMPONENT_REF)
2391 tree t = decl_for_component_ref (exprx);
2392 if (! t)
2393 return 0;
2394 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2395 exprx = t;
2398 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2399 if (moffsety_known_p)
2400 moffsety = MEM_OFFSET (y);
2401 if (TREE_CODE (expry) == COMPONENT_REF)
2403 tree t = decl_for_component_ref (expry);
2404 if (! t)
2405 return 0;
2406 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2407 expry = t;
2410 if (! DECL_P (exprx) || ! DECL_P (expry))
2411 return 0;
2413 /* With invalid code we can end up storing into the constant pool.
2414 Bail out to avoid ICEing when creating RTL for this.
2415 See gfortran.dg/lto/20091028-2_0.f90. */
2416 if (TREE_CODE (exprx) == CONST_DECL
2417 || TREE_CODE (expry) == CONST_DECL)
2418 return 1;
2420 rtlx = DECL_RTL (exprx);
2421 rtly = DECL_RTL (expry);
2423 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2424 can't overlap unless they are the same because we never reuse that part
2425 of the stack frame used for locals for spilled pseudos. */
2426 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2427 && ! rtx_equal_p (rtlx, rtly))
2428 return 1;
2430 /* If we have MEMs referring to different address spaces (which can
2431 potentially overlap), we cannot easily tell from the addresses
2432 whether the references overlap. */
2433 if (MEM_P (rtlx) && MEM_P (rtly)
2434 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2435 return 0;
2437 /* Get the base and offsets of both decls. If either is a register, we
2438 know both are and are the same, so use that as the base. The only
2439 we can avoid overlap is if we can deduce that they are nonoverlapping
2440 pieces of that decl, which is very rare. */
2441 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2442 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2443 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2445 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2446 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2447 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2449 /* If the bases are different, we know they do not overlap if both
2450 are constants or if one is a constant and the other a pointer into the
2451 stack frame. Otherwise a different base means we can't tell if they
2452 overlap or not. */
2453 if (! rtx_equal_p (basex, basey))
2454 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2455 || (CONSTANT_P (basex) && REG_P (basey)
2456 && REGNO_PTR_FRAME_P (REGNO (basey)))
2457 || (CONSTANT_P (basey) && REG_P (basex)
2458 && REGNO_PTR_FRAME_P (REGNO (basex))));
2460 /* Offset based disambiguation not appropriate for loop invariant */
2461 if (loop_invariant)
2462 return 0;
2464 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2465 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2466 : -1);
2467 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2468 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2469 : -1);
2471 /* If we have an offset for either memref, it can update the values computed
2472 above. */
2473 if (moffsetx_known_p)
2474 offsetx += moffsetx, sizex -= moffsetx;
2475 if (moffsety_known_p)
2476 offsety += moffsety, sizey -= moffsety;
2478 /* If a memref has both a size and an offset, we can use the smaller size.
2479 We can't do this if the offset isn't known because we must view this
2480 memref as being anywhere inside the DECL's MEM. */
2481 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2482 sizex = MEM_SIZE (x);
2483 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2484 sizey = MEM_SIZE (y);
2486 /* Put the values of the memref with the lower offset in X's values. */
2487 if (offsetx > offsety)
2489 tem = offsetx, offsetx = offsety, offsety = tem;
2490 tem = sizex, sizex = sizey, sizey = tem;
2493 /* If we don't know the size of the lower-offset value, we can't tell
2494 if they conflict. Otherwise, we do the test. */
2495 return sizex >= 0 && offsety >= offsetx + sizex;
2498 /* Helper for true_dependence and canon_true_dependence.
2499 Checks for true dependence: X is read after store in MEM takes place.
2501 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2502 NULL_RTX, and the canonical addresses of MEM and X are both computed
2503 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2505 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2507 Returns 1 if there is a true dependence, 0 otherwise. */
2509 static int
2510 true_dependence_1 (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2511 const_rtx x, rtx x_addr, bool mem_canonicalized)
2513 rtx base;
2514 int ret;
2516 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2517 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2519 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2520 return 1;
2522 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2523 This is used in epilogue deallocation functions, and in cselib. */
2524 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2525 return 1;
2526 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2527 return 1;
2528 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2529 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2530 return 1;
2532 /* Read-only memory is by definition never modified, and therefore can't
2533 conflict with anything. We don't expect to find read-only set on MEM,
2534 but stupid user tricks can produce them, so don't die. */
2535 if (MEM_READONLY_P (x))
2536 return 0;
2538 /* If we have MEMs referring to different address spaces (which can
2539 potentially overlap), we cannot easily tell from the addresses
2540 whether the references overlap. */
2541 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2542 return 1;
2544 if (! mem_addr)
2546 mem_addr = XEXP (mem, 0);
2547 if (mem_mode == VOIDmode)
2548 mem_mode = GET_MODE (mem);
2551 if (! x_addr)
2553 x_addr = XEXP (x, 0);
2554 if (!((GET_CODE (x_addr) == VALUE
2555 && GET_CODE (mem_addr) != VALUE
2556 && reg_mentioned_p (x_addr, mem_addr))
2557 || (GET_CODE (x_addr) != VALUE
2558 && GET_CODE (mem_addr) == VALUE
2559 && reg_mentioned_p (mem_addr, x_addr))))
2561 x_addr = get_addr (x_addr);
2562 if (! mem_canonicalized)
2563 mem_addr = get_addr (mem_addr);
2567 base = find_base_term (x_addr);
2568 if (base && (GET_CODE (base) == LABEL_REF
2569 || (GET_CODE (base) == SYMBOL_REF
2570 && CONSTANT_POOL_ADDRESS_P (base))))
2571 return 0;
2573 rtx mem_base = find_base_term (mem_addr);
2574 if (! base_alias_check (x_addr, base, mem_addr, mem_base,
2575 GET_MODE (x), mem_mode))
2576 return 0;
2578 x_addr = canon_rtx (x_addr);
2579 if (!mem_canonicalized)
2580 mem_addr = canon_rtx (mem_addr);
2582 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2583 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2584 return ret;
2586 if (mems_in_disjoint_alias_sets_p (x, mem))
2587 return 0;
2589 if (nonoverlapping_memrefs_p (mem, x, false))
2590 return 0;
2592 if (nonoverlapping_component_refs_p (mem, x))
2593 return 0;
2595 return rtx_refs_may_alias_p (x, mem, true);
2598 /* True dependence: X is read after store in MEM takes place. */
2601 true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x)
2603 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2604 x, NULL_RTX, /*mem_canonicalized=*/false);
2607 /* Canonical true dependence: X is read after store in MEM takes place.
2608 Variant of true_dependence which assumes MEM has already been
2609 canonicalized (hence we no longer do that here).
2610 The mem_addr argument has been added, since true_dependence_1 computed
2611 this value prior to canonicalizing. */
2614 canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2615 const_rtx x, rtx x_addr)
2617 return true_dependence_1 (mem, mem_mode, mem_addr,
2618 x, x_addr, /*mem_canonicalized=*/true);
2621 /* Returns nonzero if a write to X might alias a previous read from
2622 (or, if WRITEP is true, a write to) MEM.
2623 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2624 and X_MODE the mode for that access.
2625 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2627 static int
2628 write_dependence_p (const_rtx mem,
2629 const_rtx x, enum machine_mode x_mode, rtx x_addr,
2630 bool mem_canonicalized, bool x_canonicalized, bool writep)
2632 rtx mem_addr;
2633 rtx base;
2634 int ret;
2636 gcc_checking_assert (x_canonicalized
2637 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2638 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2640 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2641 return 1;
2643 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2644 This is used in epilogue deallocation functions. */
2645 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2646 return 1;
2647 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2648 return 1;
2649 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2650 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2651 return 1;
2653 /* A read from read-only memory can't conflict with read-write memory. */
2654 if (!writep && MEM_READONLY_P (mem))
2655 return 0;
2657 /* If we have MEMs referring to different address spaces (which can
2658 potentially overlap), we cannot easily tell from the addresses
2659 whether the references overlap. */
2660 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2661 return 1;
2663 mem_addr = XEXP (mem, 0);
2664 if (!x_addr)
2666 x_addr = XEXP (x, 0);
2667 if (!((GET_CODE (x_addr) == VALUE
2668 && GET_CODE (mem_addr) != VALUE
2669 && reg_mentioned_p (x_addr, mem_addr))
2670 || (GET_CODE (x_addr) != VALUE
2671 && GET_CODE (mem_addr) == VALUE
2672 && reg_mentioned_p (mem_addr, x_addr))))
2674 x_addr = get_addr (x_addr);
2675 if (!mem_canonicalized)
2676 mem_addr = get_addr (mem_addr);
2680 base = find_base_term (mem_addr);
2681 if (! writep
2682 && base
2683 && (GET_CODE (base) == LABEL_REF
2684 || (GET_CODE (base) == SYMBOL_REF
2685 && CONSTANT_POOL_ADDRESS_P (base))))
2686 return 0;
2688 rtx x_base = find_base_term (x_addr);
2689 if (! base_alias_check (x_addr, x_base, mem_addr, base, GET_MODE (x),
2690 GET_MODE (mem)))
2691 return 0;
2693 if (!x_canonicalized)
2695 x_addr = canon_rtx (x_addr);
2696 x_mode = GET_MODE (x);
2698 if (!mem_canonicalized)
2699 mem_addr = canon_rtx (mem_addr);
2701 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2702 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2703 return ret;
2705 if (nonoverlapping_memrefs_p (x, mem, false))
2706 return 0;
2708 return rtx_refs_may_alias_p (x, mem, false);
2711 /* Anti dependence: X is written after read in MEM takes place. */
2714 anti_dependence (const_rtx mem, const_rtx x)
2716 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2717 /*mem_canonicalized=*/false,
2718 /*x_canonicalized*/false, /*writep=*/false);
2721 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2722 Also, consider X in X_MODE (which might be from an enclosing
2723 STRICT_LOW_PART / ZERO_EXTRACT).
2724 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2727 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2728 const_rtx x, enum machine_mode x_mode, rtx x_addr)
2730 return write_dependence_p (mem, x, x_mode, x_addr,
2731 mem_canonicalized, /*x_canonicalized=*/true,
2732 /*writep=*/false);
2735 /* Output dependence: X is written after store in MEM takes place. */
2738 output_dependence (const_rtx mem, const_rtx x)
2740 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2741 /*mem_canonicalized=*/false,
2742 /*x_canonicalized*/false, /*writep=*/true);
2747 /* Check whether X may be aliased with MEM. Don't do offset-based
2748 memory disambiguation & TBAA. */
2750 may_alias_p (const_rtx mem, const_rtx x)
2752 rtx x_addr, mem_addr;
2754 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2755 return 1;
2757 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2758 This is used in epilogue deallocation functions. */
2759 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2760 return 1;
2761 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2762 return 1;
2763 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2764 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2765 return 1;
2767 /* Read-only memory is by definition never modified, and therefore can't
2768 conflict with anything. We don't expect to find read-only set on MEM,
2769 but stupid user tricks can produce them, so don't die. */
2770 if (MEM_READONLY_P (x))
2771 return 0;
2773 /* If we have MEMs referring to different address spaces (which can
2774 potentially overlap), we cannot easily tell from the addresses
2775 whether the references overlap. */
2776 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2777 return 1;
2779 x_addr = XEXP (x, 0);
2780 mem_addr = XEXP (mem, 0);
2781 if (!((GET_CODE (x_addr) == VALUE
2782 && GET_CODE (mem_addr) != VALUE
2783 && reg_mentioned_p (x_addr, mem_addr))
2784 || (GET_CODE (x_addr) != VALUE
2785 && GET_CODE (mem_addr) == VALUE
2786 && reg_mentioned_p (mem_addr, x_addr))))
2788 x_addr = get_addr (x_addr);
2789 mem_addr = get_addr (mem_addr);
2792 rtx x_base = find_base_term (x_addr);
2793 rtx mem_base = find_base_term (mem_addr);
2794 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2795 GET_MODE (x), GET_MODE (mem_addr)))
2796 return 0;
2798 x_addr = canon_rtx (x_addr);
2799 mem_addr = canon_rtx (mem_addr);
2801 if (nonoverlapping_memrefs_p (mem, x, true))
2802 return 0;
2804 /* TBAA not valid for loop_invarint */
2805 return rtx_refs_may_alias_p (x, mem, false);
2808 void
2809 init_alias_target (void)
2811 int i;
2813 if (!arg_base_value)
2814 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2816 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2818 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2819 /* Check whether this register can hold an incoming pointer
2820 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2821 numbers, so translate if necessary due to register windows. */
2822 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2823 && HARD_REGNO_MODE_OK (i, Pmode))
2824 static_reg_base_value[i] = arg_base_value;
2826 static_reg_base_value[STACK_POINTER_REGNUM]
2827 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2828 static_reg_base_value[ARG_POINTER_REGNUM]
2829 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2830 static_reg_base_value[FRAME_POINTER_REGNUM]
2831 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2832 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2833 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2834 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2835 #endif
2838 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2839 to be memory reference. */
2840 static bool memory_modified;
2841 static void
2842 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2844 if (MEM_P (x))
2846 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2847 memory_modified = true;
2852 /* Return true when INSN possibly modify memory contents of MEM
2853 (i.e. address can be modified). */
2854 bool
2855 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2857 if (!INSN_P (insn))
2858 return false;
2859 memory_modified = false;
2860 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2861 return memory_modified;
2864 /* Return TRUE if the destination of a set is rtx identical to
2865 ITEM. */
2866 static inline bool
2867 set_dest_equal_p (const_rtx set, const_rtx item)
2869 rtx dest = SET_DEST (set);
2870 return rtx_equal_p (dest, item);
2873 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2874 *DEFINITELY* modify the memory contents of MEM. */
2875 bool
2876 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2878 if (!INSN_P (insn))
2879 return false;
2880 insn = PATTERN (insn);
2881 if (GET_CODE (insn) == SET)
2882 return set_dest_equal_p (insn, mem);
2883 else if (GET_CODE (insn) == PARALLEL)
2885 int i;
2886 for (i = 0; i < XVECLEN (insn, 0); i++)
2888 rtx sub = XVECEXP (insn, 0, i);
2889 if (GET_CODE (sub) == SET
2890 && set_dest_equal_p (sub, mem))
2891 return true;
2894 return false;
2897 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2898 array. */
2900 void
2901 init_alias_analysis (void)
2903 unsigned int maxreg = max_reg_num ();
2904 int changed, pass;
2905 int i;
2906 unsigned int ui;
2907 rtx insn, val;
2908 int rpo_cnt;
2909 int *rpo;
2911 timevar_push (TV_ALIAS_ANALYSIS);
2913 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
2914 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
2915 bitmap_clear (reg_known_equiv_p);
2917 /* If we have memory allocated from the previous run, use it. */
2918 if (old_reg_base_value)
2919 reg_base_value = old_reg_base_value;
2921 if (reg_base_value)
2922 reg_base_value->truncate (0);
2924 vec_safe_grow_cleared (reg_base_value, maxreg);
2926 new_reg_base_value = XNEWVEC (rtx, maxreg);
2927 reg_seen = sbitmap_alloc (maxreg);
2929 /* The basic idea is that each pass through this loop will use the
2930 "constant" information from the previous pass to propagate alias
2931 information through another level of assignments.
2933 The propagation is done on the CFG in reverse post-order, to propagate
2934 things forward as far as possible in each iteration.
2936 This could get expensive if the assignment chains are long. Maybe
2937 we should throttle the number of iterations, possibly based on
2938 the optimization level or flag_expensive_optimizations.
2940 We could propagate more information in the first pass by making use
2941 of DF_REG_DEF_COUNT to determine immediately that the alias information
2942 for a pseudo is "constant".
2944 A program with an uninitialized variable can cause an infinite loop
2945 here. Instead of doing a full dataflow analysis to detect such problems
2946 we just cap the number of iterations for the loop.
2948 The state of the arrays for the set chain in question does not matter
2949 since the program has undefined behavior. */
2951 rpo = XNEWVEC (int, n_basic_blocks);
2952 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
2954 pass = 0;
2957 /* Assume nothing will change this iteration of the loop. */
2958 changed = 0;
2960 /* We want to assign the same IDs each iteration of this loop, so
2961 start counting from one each iteration of the loop. */
2962 unique_id = 1;
2964 /* We're at the start of the function each iteration through the
2965 loop, so we're copying arguments. */
2966 copying_arguments = true;
2968 /* Wipe the potential alias information clean for this pass. */
2969 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2971 /* Wipe the reg_seen array clean. */
2972 bitmap_clear (reg_seen);
2974 /* Mark all hard registers which may contain an address.
2975 The stack, frame and argument pointers may contain an address.
2976 An argument register which can hold a Pmode value may contain
2977 an address even if it is not in BASE_REGS.
2979 The address expression is VOIDmode for an argument and
2980 Pmode for other registers. */
2982 memcpy (new_reg_base_value, static_reg_base_value,
2983 FIRST_PSEUDO_REGISTER * sizeof (rtx));
2985 /* Walk the insns adding values to the new_reg_base_value array. */
2986 for (i = 0; i < rpo_cnt; i++)
2988 basic_block bb = BASIC_BLOCK (rpo[i]);
2989 FOR_BB_INSNS (bb, insn)
2991 if (NONDEBUG_INSN_P (insn))
2993 rtx note, set;
2995 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2996 /* The prologue/epilogue insns are not threaded onto the
2997 insn chain until after reload has completed. Thus,
2998 there is no sense wasting time checking if INSN is in
2999 the prologue/epilogue until after reload has completed. */
3000 if (reload_completed
3001 && prologue_epilogue_contains (insn))
3002 continue;
3003 #endif
3005 /* If this insn has a noalias note, process it, Otherwise,
3006 scan for sets. A simple set will have no side effects
3007 which could change the base value of any other register. */
3009 if (GET_CODE (PATTERN (insn)) == SET
3010 && REG_NOTES (insn) != 0
3011 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3012 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3013 else
3014 note_stores (PATTERN (insn), record_set, NULL);
3016 set = single_set (insn);
3018 if (set != 0
3019 && REG_P (SET_DEST (set))
3020 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3022 unsigned int regno = REGNO (SET_DEST (set));
3023 rtx src = SET_SRC (set);
3024 rtx t;
3026 note = find_reg_equal_equiv_note (insn);
3027 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3028 && DF_REG_DEF_COUNT (regno) != 1)
3029 note = NULL_RTX;
3031 if (note != NULL_RTX
3032 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3033 && ! rtx_varies_p (XEXP (note, 0), 1)
3034 && ! reg_overlap_mentioned_p (SET_DEST (set),
3035 XEXP (note, 0)))
3037 set_reg_known_value (regno, XEXP (note, 0));
3038 set_reg_known_equiv_p (regno,
3039 REG_NOTE_KIND (note) == REG_EQUIV);
3041 else if (DF_REG_DEF_COUNT (regno) == 1
3042 && GET_CODE (src) == PLUS
3043 && REG_P (XEXP (src, 0))
3044 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3045 && CONST_INT_P (XEXP (src, 1)))
3047 t = plus_constant (GET_MODE (src), t,
3048 INTVAL (XEXP (src, 1)));
3049 set_reg_known_value (regno, t);
3050 set_reg_known_equiv_p (regno, false);
3052 else if (DF_REG_DEF_COUNT (regno) == 1
3053 && ! rtx_varies_p (src, 1))
3055 set_reg_known_value (regno, src);
3056 set_reg_known_equiv_p (regno, false);
3060 else if (NOTE_P (insn)
3061 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3062 copying_arguments = false;
3066 /* Now propagate values from new_reg_base_value to reg_base_value. */
3067 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3069 for (ui = 0; ui < maxreg; ui++)
3071 if (new_reg_base_value[ui]
3072 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3073 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3075 (*reg_base_value)[ui] = new_reg_base_value[ui];
3076 changed = 1;
3080 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3081 XDELETEVEC (rpo);
3083 /* Fill in the remaining entries. */
3084 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3086 int regno = i + FIRST_PSEUDO_REGISTER;
3087 if (! val)
3088 set_reg_known_value (regno, regno_reg_rtx[regno]);
3091 /* Clean up. */
3092 free (new_reg_base_value);
3093 new_reg_base_value = 0;
3094 sbitmap_free (reg_seen);
3095 reg_seen = 0;
3096 timevar_pop (TV_ALIAS_ANALYSIS);
3099 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3100 Special API for var-tracking pass purposes. */
3102 void
3103 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3105 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3108 void
3109 end_alias_analysis (void)
3111 old_reg_base_value = reg_base_value;
3112 vec_free (reg_known_value);
3113 sbitmap_free (reg_known_equiv_p);
3116 #include "gt-alias.h"