Mark ChangeLog
[official-gcc.git] / gcc / alias.c
blob381222c34872d3d2e4daa7e6015e6d07aa9d5a4e
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-flow.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, 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;
550 /* Return the alias set for the memory pointed to by T, which may be
551 either a type or an expression. Return -1 if there is nothing
552 special about dereferencing T. */
554 static alias_set_type
555 get_deref_alias_set_1 (tree t)
557 /* If we're not doing any alias analysis, just assume everything
558 aliases everything else. */
559 if (!flag_strict_aliasing)
560 return 0;
562 /* All we care about is the type. */
563 if (! TYPE_P (t))
564 t = TREE_TYPE (t);
566 /* If we have an INDIRECT_REF via a void pointer, we don't
567 know anything about what that might alias. Likewise if the
568 pointer is marked that way. */
569 if (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
570 || TYPE_REF_CAN_ALIAS_ALL (t))
571 return 0;
573 return -1;
576 /* Return the alias set for the memory pointed to by T, which may be
577 either a type or an expression. */
579 alias_set_type
580 get_deref_alias_set (tree t)
582 alias_set_type set = get_deref_alias_set_1 (t);
584 /* Fall back to the alias-set of the pointed-to type. */
585 if (set == -1)
587 if (! TYPE_P (t))
588 t = TREE_TYPE (t);
589 set = get_alias_set (TREE_TYPE (t));
592 return set;
595 /* Return the alias set for T, which may be either a type or an
596 expression. Call language-specific routine for help, if needed. */
598 alias_set_type
599 get_alias_set (tree t)
601 alias_set_type set;
603 /* If we're not doing any alias analysis, just assume everything
604 aliases everything else. Also return 0 if this or its type is
605 an error. */
606 if (! flag_strict_aliasing || t == error_mark_node
607 || (! TYPE_P (t)
608 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
609 return 0;
611 /* We can be passed either an expression or a type. This and the
612 language-specific routine may make mutually-recursive calls to each other
613 to figure out what to do. At each juncture, we see if this is a tree
614 that the language may need to handle specially. First handle things that
615 aren't types. */
616 if (! TYPE_P (t))
618 tree inner;
620 /* Give the language a chance to do something with this tree
621 before we look at it. */
622 STRIP_NOPS (t);
623 set = lang_hooks.get_alias_set (t);
624 if (set != -1)
625 return set;
627 /* Get the base object of the reference. */
628 inner = t;
629 while (handled_component_p (inner))
631 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
632 the type of any component references that wrap it to
633 determine the alias-set. */
634 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
635 t = TREE_OPERAND (inner, 0);
636 inner = TREE_OPERAND (inner, 0);
639 /* Handle pointer dereferences here, they can override the
640 alias-set. */
641 if (INDIRECT_REF_P (inner))
643 set = get_deref_alias_set_1 (TREE_OPERAND (inner, 0));
644 if (set != -1)
645 return set;
647 else if (TREE_CODE (inner) == TARGET_MEM_REF)
648 return get_deref_alias_set (TMR_OFFSET (inner));
649 else if (TREE_CODE (inner) == MEM_REF)
651 set = get_deref_alias_set_1 (TREE_OPERAND (inner, 1));
652 if (set != -1)
653 return set;
656 /* If the innermost reference is a MEM_REF that has a
657 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
658 using the memory access type for determining the alias-set. */
659 if (TREE_CODE (inner) == MEM_REF
660 && TYPE_MAIN_VARIANT (TREE_TYPE (inner))
661 != TYPE_MAIN_VARIANT
662 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1)))))
663 return get_deref_alias_set (TREE_OPERAND (inner, 1));
665 /* Otherwise, pick up the outermost object that we could have a pointer
666 to, processing conversions as above. */
667 while (component_uses_parent_alias_set (t))
669 t = TREE_OPERAND (t, 0);
670 STRIP_NOPS (t);
673 /* If we've already determined the alias set for a decl, just return
674 it. This is necessary for C++ anonymous unions, whose component
675 variables don't look like union members (boo!). */
676 if (TREE_CODE (t) == VAR_DECL
677 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
678 return MEM_ALIAS_SET (DECL_RTL (t));
680 /* Now all we care about is the type. */
681 t = TREE_TYPE (t);
684 /* Variant qualifiers don't affect the alias set, so get the main
685 variant. */
686 t = TYPE_MAIN_VARIANT (t);
688 /* Always use the canonical type as well. If this is a type that
689 requires structural comparisons to identify compatible types
690 use alias set zero. */
691 if (TYPE_STRUCTURAL_EQUALITY_P (t))
693 /* Allow the language to specify another alias set for this
694 type. */
695 set = lang_hooks.get_alias_set (t);
696 if (set != -1)
697 return set;
698 return 0;
701 t = TYPE_CANONICAL (t);
703 /* The canonical type should not require structural equality checks. */
704 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
706 /* If this is a type with a known alias set, return it. */
707 if (TYPE_ALIAS_SET_KNOWN_P (t))
708 return TYPE_ALIAS_SET (t);
710 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
711 if (!COMPLETE_TYPE_P (t))
713 /* For arrays with unknown size the conservative answer is the
714 alias set of the element type. */
715 if (TREE_CODE (t) == ARRAY_TYPE)
716 return get_alias_set (TREE_TYPE (t));
718 /* But return zero as a conservative answer for incomplete types. */
719 return 0;
722 /* See if the language has special handling for this type. */
723 set = lang_hooks.get_alias_set (t);
724 if (set != -1)
725 return set;
727 /* There are no objects of FUNCTION_TYPE, so there's no point in
728 using up an alias set for them. (There are, of course, pointers
729 and references to functions, but that's different.) */
730 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
731 set = 0;
733 /* Unless the language specifies otherwise, let vector types alias
734 their components. This avoids some nasty type punning issues in
735 normal usage. And indeed lets vectors be treated more like an
736 array slice. */
737 else if (TREE_CODE (t) == VECTOR_TYPE)
738 set = get_alias_set (TREE_TYPE (t));
740 /* Unless the language specifies otherwise, treat array types the
741 same as their components. This avoids the asymmetry we get
742 through recording the components. Consider accessing a
743 character(kind=1) through a reference to a character(kind=1)[1:1].
744 Or consider if we want to assign integer(kind=4)[0:D.1387] and
745 integer(kind=4)[4] the same alias set or not.
746 Just be pragmatic here and make sure the array and its element
747 type get the same alias set assigned. */
748 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
749 set = get_alias_set (TREE_TYPE (t));
751 /* From the former common C and C++ langhook implementation:
753 Unfortunately, there is no canonical form of a pointer type.
754 In particular, if we have `typedef int I', then `int *', and
755 `I *' are different types. So, we have to pick a canonical
756 representative. We do this below.
758 Technically, this approach is actually more conservative that
759 it needs to be. In particular, `const int *' and `int *'
760 should be in different alias sets, according to the C and C++
761 standard, since their types are not the same, and so,
762 technically, an `int **' and `const int **' cannot point at
763 the same thing.
765 But, the standard is wrong. In particular, this code is
766 legal C++:
768 int *ip;
769 int **ipp = &ip;
770 const int* const* cipp = ipp;
771 And, it doesn't make sense for that to be legal unless you
772 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
773 the pointed-to types. This issue has been reported to the
774 C++ committee.
776 In addition to the above canonicalization issue, with LTO
777 we should also canonicalize `T (*)[]' to `T *' avoiding
778 alias issues with pointer-to element types and pointer-to
779 array types.
781 Likewise we need to deal with the situation of incomplete
782 pointed-to types and make `*(struct X **)&a' and
783 `*(struct X {} **)&a' alias. Otherwise we will have to
784 guarantee that all pointer-to incomplete type variants
785 will be replaced by pointer-to complete type variants if
786 they are available.
788 With LTO the convenient situation of using `void *' to
789 access and store any pointer type will also become
790 more apparent (and `void *' is just another pointer-to
791 incomplete type). Assigning alias-set zero to `void *'
792 and all pointer-to incomplete types is a not appealing
793 solution. Assigning an effective alias-set zero only
794 affecting pointers might be - by recording proper subset
795 relationships of all pointer alias-sets.
797 Pointer-to function types are another grey area which
798 needs caution. Globbing them all into one alias-set
799 or the above effective zero set would work.
801 For now just assign the same alias-set to all pointers.
802 That's simple and avoids all the above problems. */
803 else if (POINTER_TYPE_P (t)
804 && t != ptr_type_node)
805 set = get_alias_set (ptr_type_node);
807 /* Otherwise make a new alias set for this type. */
808 else
810 /* Each canonical type gets its own alias set, so canonical types
811 shouldn't form a tree. It doesn't really matter for types
812 we handle specially above, so only check it where it possibly
813 would result in a bogus alias set. */
814 gcc_checking_assert (TYPE_CANONICAL (t) == t);
816 set = new_alias_set ();
819 TYPE_ALIAS_SET (t) = set;
821 /* If this is an aggregate type or a complex type, we must record any
822 component aliasing information. */
823 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
824 record_component_aliases (t);
826 return set;
829 /* Return a brand-new alias set. */
831 alias_set_type
832 new_alias_set (void)
834 if (flag_strict_aliasing)
836 if (alias_sets == 0)
837 vec_safe_push (alias_sets, (alias_set_entry) 0);
838 vec_safe_push (alias_sets, (alias_set_entry) 0);
839 return alias_sets->length () - 1;
841 else
842 return 0;
845 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
846 not everything that aliases SUPERSET also aliases SUBSET. For example,
847 in C, a store to an `int' can alias a load of a structure containing an
848 `int', and vice versa. But it can't alias a load of a 'double' member
849 of the same structure. Here, the structure would be the SUPERSET and
850 `int' the SUBSET. This relationship is also described in the comment at
851 the beginning of this file.
853 This function should be called only once per SUPERSET/SUBSET pair.
855 It is illegal for SUPERSET to be zero; everything is implicitly a
856 subset of alias set zero. */
858 void
859 record_alias_subset (alias_set_type superset, alias_set_type subset)
861 alias_set_entry superset_entry;
862 alias_set_entry subset_entry;
864 /* It is possible in complex type situations for both sets to be the same,
865 in which case we can ignore this operation. */
866 if (superset == subset)
867 return;
869 gcc_assert (superset);
871 superset_entry = get_alias_set_entry (superset);
872 if (superset_entry == 0)
874 /* Create an entry for the SUPERSET, so that we have a place to
875 attach the SUBSET. */
876 superset_entry = ggc_alloc_cleared_alias_set_entry_d ();
877 superset_entry->alias_set = superset;
878 superset_entry->children
879 = splay_tree_new_ggc (splay_tree_compare_ints,
880 ggc_alloc_splay_tree_scalar_scalar_splay_tree_s,
881 ggc_alloc_splay_tree_scalar_scalar_splay_tree_node_s);
882 superset_entry->has_zero_child = 0;
883 (*alias_sets)[superset] = superset_entry;
886 if (subset == 0)
887 superset_entry->has_zero_child = 1;
888 else
890 subset_entry = get_alias_set_entry (subset);
891 /* If there is an entry for the subset, enter all of its children
892 (if they are not already present) as children of the SUPERSET. */
893 if (subset_entry)
895 if (subset_entry->has_zero_child)
896 superset_entry->has_zero_child = 1;
898 splay_tree_foreach (subset_entry->children, insert_subset_children,
899 superset_entry->children);
902 /* Enter the SUBSET itself as a child of the SUPERSET. */
903 splay_tree_insert (superset_entry->children,
904 (splay_tree_key) subset, 0);
908 /* Record that component types of TYPE, if any, are part of that type for
909 aliasing purposes. For record types, we only record component types
910 for fields that are not marked non-addressable. For array types, we
911 only record the component type if it is not marked non-aliased. */
913 void
914 record_component_aliases (tree type)
916 alias_set_type superset = get_alias_set (type);
917 tree field;
919 if (superset == 0)
920 return;
922 switch (TREE_CODE (type))
924 case RECORD_TYPE:
925 case UNION_TYPE:
926 case QUAL_UNION_TYPE:
927 /* Recursively record aliases for the base classes, if there are any. */
928 if (TYPE_BINFO (type))
930 int i;
931 tree binfo, base_binfo;
933 for (binfo = TYPE_BINFO (type), i = 0;
934 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
935 record_alias_subset (superset,
936 get_alias_set (BINFO_TYPE (base_binfo)));
938 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
939 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
940 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
941 break;
943 case COMPLEX_TYPE:
944 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
945 break;
947 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
948 element type. */
950 default:
951 break;
955 /* Allocate an alias set for use in storing and reading from the varargs
956 spill area. */
958 static GTY(()) alias_set_type varargs_set = -1;
960 alias_set_type
961 get_varargs_alias_set (void)
963 #if 1
964 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
965 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
966 consistently use the varargs alias set for loads from the varargs
967 area. So don't use it anywhere. */
968 return 0;
969 #else
970 if (varargs_set == -1)
971 varargs_set = new_alias_set ();
973 return varargs_set;
974 #endif
977 /* Likewise, but used for the fixed portions of the frame, e.g., register
978 save areas. */
980 static GTY(()) alias_set_type frame_set = -1;
982 alias_set_type
983 get_frame_alias_set (void)
985 if (frame_set == -1)
986 frame_set = new_alias_set ();
988 return frame_set;
991 /* Create a new, unique base with id ID. */
993 static rtx
994 unique_base_value (HOST_WIDE_INT id)
996 return gen_rtx_ADDRESS (Pmode, id);
999 /* Return true if accesses based on any other base value cannot alias
1000 those based on X. */
1002 static bool
1003 unique_base_value_p (rtx x)
1005 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1008 /* Return true if X is known to be a base value. */
1010 static bool
1011 known_base_value_p (rtx x)
1013 switch (GET_CODE (x))
1015 case LABEL_REF:
1016 case SYMBOL_REF:
1017 return true;
1019 case ADDRESS:
1020 /* Arguments may or may not be bases; we don't know for sure. */
1021 return GET_MODE (x) != VOIDmode;
1023 default:
1024 return false;
1028 /* Inside SRC, the source of a SET, find a base address. */
1030 static rtx
1031 find_base_value (rtx src)
1033 unsigned int regno;
1035 #if defined (FIND_BASE_TERM)
1036 /* Try machine-dependent ways to find the base term. */
1037 src = FIND_BASE_TERM (src);
1038 #endif
1040 switch (GET_CODE (src))
1042 case SYMBOL_REF:
1043 case LABEL_REF:
1044 return src;
1046 case REG:
1047 regno = REGNO (src);
1048 /* At the start of a function, argument registers have known base
1049 values which may be lost later. Returning an ADDRESS
1050 expression here allows optimization based on argument values
1051 even when the argument registers are used for other purposes. */
1052 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1053 return new_reg_base_value[regno];
1055 /* If a pseudo has a known base value, return it. Do not do this
1056 for non-fixed hard regs since it can result in a circular
1057 dependency chain for registers which have values at function entry.
1059 The test above is not sufficient because the scheduler may move
1060 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1061 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1062 && regno < vec_safe_length (reg_base_value))
1064 /* If we're inside init_alias_analysis, use new_reg_base_value
1065 to reduce the number of relaxation iterations. */
1066 if (new_reg_base_value && new_reg_base_value[regno]
1067 && DF_REG_DEF_COUNT (regno) == 1)
1068 return new_reg_base_value[regno];
1070 if ((*reg_base_value)[regno])
1071 return (*reg_base_value)[regno];
1074 return 0;
1076 case MEM:
1077 /* Check for an argument passed in memory. Only record in the
1078 copying-arguments block; it is too hard to track changes
1079 otherwise. */
1080 if (copying_arguments
1081 && (XEXP (src, 0) == arg_pointer_rtx
1082 || (GET_CODE (XEXP (src, 0)) == PLUS
1083 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1084 return arg_base_value;
1085 return 0;
1087 case CONST:
1088 src = XEXP (src, 0);
1089 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1090 break;
1092 /* ... fall through ... */
1094 case PLUS:
1095 case MINUS:
1097 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1099 /* If either operand is a REG that is a known pointer, then it
1100 is the base. */
1101 if (REG_P (src_0) && REG_POINTER (src_0))
1102 return find_base_value (src_0);
1103 if (REG_P (src_1) && REG_POINTER (src_1))
1104 return find_base_value (src_1);
1106 /* If either operand is a REG, then see if we already have
1107 a known value for it. */
1108 if (REG_P (src_0))
1110 temp = find_base_value (src_0);
1111 if (temp != 0)
1112 src_0 = temp;
1115 if (REG_P (src_1))
1117 temp = find_base_value (src_1);
1118 if (temp!= 0)
1119 src_1 = temp;
1122 /* If either base is named object or a special address
1123 (like an argument or stack reference), then use it for the
1124 base term. */
1125 if (src_0 != 0 && known_base_value_p (src_0))
1126 return src_0;
1128 if (src_1 != 0 && known_base_value_p (src_1))
1129 return src_1;
1131 /* Guess which operand is the base address:
1132 If either operand is a symbol, then it is the base. If
1133 either operand is a CONST_INT, then the other is the base. */
1134 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1135 return find_base_value (src_0);
1136 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1137 return find_base_value (src_1);
1139 return 0;
1142 case LO_SUM:
1143 /* The standard form is (lo_sum reg sym) so look only at the
1144 second operand. */
1145 return find_base_value (XEXP (src, 1));
1147 case AND:
1148 /* If the second operand is constant set the base
1149 address to the first operand. */
1150 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1151 return find_base_value (XEXP (src, 0));
1152 return 0;
1154 case TRUNCATE:
1155 /* As we do not know which address space the pointer is referring to, we can
1156 handle this only if the target does not support different pointer or
1157 address modes depending on the address space. */
1158 if (!target_default_pointer_address_modes_p ())
1159 break;
1160 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1161 break;
1162 /* Fall through. */
1163 case HIGH:
1164 case PRE_INC:
1165 case PRE_DEC:
1166 case POST_INC:
1167 case POST_DEC:
1168 case PRE_MODIFY:
1169 case POST_MODIFY:
1170 return find_base_value (XEXP (src, 0));
1172 case ZERO_EXTEND:
1173 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1174 /* As we do not know which address space the pointer is referring to, we can
1175 handle this only if the target does not support different pointer or
1176 address modes depending on the address space. */
1177 if (!target_default_pointer_address_modes_p ())
1178 break;
1181 rtx temp = find_base_value (XEXP (src, 0));
1183 if (temp != 0 && CONSTANT_P (temp))
1184 temp = convert_memory_address (Pmode, temp);
1186 return temp;
1189 default:
1190 break;
1193 return 0;
1196 /* Called from init_alias_analysis indirectly through note_stores,
1197 or directly if DEST is a register with a REG_NOALIAS note attached.
1198 SET is null in the latter case. */
1200 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1201 register N has been set in this function. */
1202 static sbitmap reg_seen;
1204 static void
1205 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1207 unsigned regno;
1208 rtx src;
1209 int n;
1211 if (!REG_P (dest))
1212 return;
1214 regno = REGNO (dest);
1216 gcc_checking_assert (regno < reg_base_value->length ());
1218 /* If this spans multiple hard registers, then we must indicate that every
1219 register has an unusable value. */
1220 if (regno < FIRST_PSEUDO_REGISTER)
1221 n = hard_regno_nregs[regno][GET_MODE (dest)];
1222 else
1223 n = 1;
1224 if (n != 1)
1226 while (--n >= 0)
1228 bitmap_set_bit (reg_seen, regno + n);
1229 new_reg_base_value[regno + n] = 0;
1231 return;
1234 if (set)
1236 /* A CLOBBER wipes out any old value but does not prevent a previously
1237 unset register from acquiring a base address (i.e. reg_seen is not
1238 set). */
1239 if (GET_CODE (set) == CLOBBER)
1241 new_reg_base_value[regno] = 0;
1242 return;
1244 src = SET_SRC (set);
1246 else
1248 /* There's a REG_NOALIAS note against DEST. */
1249 if (bitmap_bit_p (reg_seen, regno))
1251 new_reg_base_value[regno] = 0;
1252 return;
1254 bitmap_set_bit (reg_seen, regno);
1255 new_reg_base_value[regno] = unique_base_value (unique_id++);
1256 return;
1259 /* If this is not the first set of REGNO, see whether the new value
1260 is related to the old one. There are two cases of interest:
1262 (1) The register might be assigned an entirely new value
1263 that has the same base term as the original set.
1265 (2) The set might be a simple self-modification that
1266 cannot change REGNO's base value.
1268 If neither case holds, reject the original base value as invalid.
1269 Note that the following situation is not detected:
1271 extern int x, y; int *p = &x; p += (&y-&x);
1273 ANSI C does not allow computing the difference of addresses
1274 of distinct top level objects. */
1275 if (new_reg_base_value[regno] != 0
1276 && find_base_value (src) != new_reg_base_value[regno])
1277 switch (GET_CODE (src))
1279 case LO_SUM:
1280 case MINUS:
1281 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1282 new_reg_base_value[regno] = 0;
1283 break;
1284 case PLUS:
1285 /* If the value we add in the PLUS is also a valid base value,
1286 this might be the actual base value, and the original value
1287 an index. */
1289 rtx other = NULL_RTX;
1291 if (XEXP (src, 0) == dest)
1292 other = XEXP (src, 1);
1293 else if (XEXP (src, 1) == dest)
1294 other = XEXP (src, 0);
1296 if (! other || find_base_value (other))
1297 new_reg_base_value[regno] = 0;
1298 break;
1300 case AND:
1301 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1302 new_reg_base_value[regno] = 0;
1303 break;
1304 default:
1305 new_reg_base_value[regno] = 0;
1306 break;
1308 /* If this is the first set of a register, record the value. */
1309 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1310 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1311 new_reg_base_value[regno] = find_base_value (src);
1313 bitmap_set_bit (reg_seen, regno);
1316 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1317 using hard registers with non-null REG_BASE_VALUE for renaming. */
1319 get_reg_base_value (unsigned int regno)
1321 return (*reg_base_value)[regno];
1324 /* If a value is known for REGNO, return it. */
1327 get_reg_known_value (unsigned int regno)
1329 if (regno >= FIRST_PSEUDO_REGISTER)
1331 regno -= FIRST_PSEUDO_REGISTER;
1332 if (regno < vec_safe_length (reg_known_value))
1333 return (*reg_known_value)[regno];
1335 return NULL;
1338 /* Set it. */
1340 static void
1341 set_reg_known_value (unsigned int regno, rtx val)
1343 if (regno >= FIRST_PSEUDO_REGISTER)
1345 regno -= FIRST_PSEUDO_REGISTER;
1346 if (regno < vec_safe_length (reg_known_value))
1347 (*reg_known_value)[regno] = val;
1351 /* Similarly for reg_known_equiv_p. */
1353 bool
1354 get_reg_known_equiv_p (unsigned int regno)
1356 if (regno >= FIRST_PSEUDO_REGISTER)
1358 regno -= FIRST_PSEUDO_REGISTER;
1359 if (regno < vec_safe_length (reg_known_value))
1360 return bitmap_bit_p (reg_known_equiv_p, regno);
1362 return false;
1365 static void
1366 set_reg_known_equiv_p (unsigned int regno, bool val)
1368 if (regno >= FIRST_PSEUDO_REGISTER)
1370 regno -= FIRST_PSEUDO_REGISTER;
1371 if (regno < vec_safe_length (reg_known_value))
1373 if (val)
1374 bitmap_set_bit (reg_known_equiv_p, regno);
1375 else
1376 bitmap_clear_bit (reg_known_equiv_p, regno);
1382 /* Returns a canonical version of X, from the point of view alias
1383 analysis. (For example, if X is a MEM whose address is a register,
1384 and the register has a known value (say a SYMBOL_REF), then a MEM
1385 whose address is the SYMBOL_REF is returned.) */
1388 canon_rtx (rtx x)
1390 /* Recursively look for equivalences. */
1391 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1393 rtx t = get_reg_known_value (REGNO (x));
1394 if (t == x)
1395 return x;
1396 if (t)
1397 return canon_rtx (t);
1400 if (GET_CODE (x) == PLUS)
1402 rtx x0 = canon_rtx (XEXP (x, 0));
1403 rtx x1 = canon_rtx (XEXP (x, 1));
1405 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1407 if (CONST_INT_P (x0))
1408 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1409 else if (CONST_INT_P (x1))
1410 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1411 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1415 /* This gives us much better alias analysis when called from
1416 the loop optimizer. Note we want to leave the original
1417 MEM alone, but need to return the canonicalized MEM with
1418 all the flags with their original values. */
1419 else if (MEM_P (x))
1420 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1422 return x;
1425 /* Return 1 if X and Y are identical-looking rtx's.
1426 Expect that X and Y has been already canonicalized.
1428 We use the data in reg_known_value above to see if two registers with
1429 different numbers are, in fact, equivalent. */
1431 static int
1432 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1434 int i;
1435 int j;
1436 enum rtx_code code;
1437 const char *fmt;
1439 if (x == 0 && y == 0)
1440 return 1;
1441 if (x == 0 || y == 0)
1442 return 0;
1444 if (x == y)
1445 return 1;
1447 code = GET_CODE (x);
1448 /* Rtx's of different codes cannot be equal. */
1449 if (code != GET_CODE (y))
1450 return 0;
1452 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1453 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1455 if (GET_MODE (x) != GET_MODE (y))
1456 return 0;
1458 /* Some RTL can be compared without a recursive examination. */
1459 switch (code)
1461 case REG:
1462 return REGNO (x) == REGNO (y);
1464 case LABEL_REF:
1465 return XEXP (x, 0) == XEXP (y, 0);
1467 case SYMBOL_REF:
1468 return XSTR (x, 0) == XSTR (y, 0);
1470 case ENTRY_VALUE:
1471 /* This is magic, don't go through canonicalization et al. */
1472 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1474 case VALUE:
1475 CASE_CONST_UNIQUE:
1476 /* There's no need to compare the contents of CONST_DOUBLEs or
1477 CONST_INTs because pointer equality is a good enough
1478 comparison for these nodes. */
1479 return 0;
1481 default:
1482 break;
1485 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1486 if (code == PLUS)
1487 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1488 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1489 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1490 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1491 /* For commutative operations, the RTX match if the operand match in any
1492 order. Also handle the simple binary and unary cases without a loop. */
1493 if (COMMUTATIVE_P (x))
1495 rtx xop0 = canon_rtx (XEXP (x, 0));
1496 rtx yop0 = canon_rtx (XEXP (y, 0));
1497 rtx yop1 = canon_rtx (XEXP (y, 1));
1499 return ((rtx_equal_for_memref_p (xop0, yop0)
1500 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1501 || (rtx_equal_for_memref_p (xop0, yop1)
1502 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1504 else if (NON_COMMUTATIVE_P (x))
1506 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1507 canon_rtx (XEXP (y, 0)))
1508 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1509 canon_rtx (XEXP (y, 1))));
1511 else if (UNARY_P (x))
1512 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1513 canon_rtx (XEXP (y, 0)));
1515 /* Compare the elements. If any pair of corresponding elements
1516 fail to match, return 0 for the whole things.
1518 Limit cases to types which actually appear in addresses. */
1520 fmt = GET_RTX_FORMAT (code);
1521 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1523 switch (fmt[i])
1525 case 'i':
1526 if (XINT (x, i) != XINT (y, i))
1527 return 0;
1528 break;
1530 case 'E':
1531 /* Two vectors must have the same length. */
1532 if (XVECLEN (x, i) != XVECLEN (y, i))
1533 return 0;
1535 /* And the corresponding elements must match. */
1536 for (j = 0; j < XVECLEN (x, i); j++)
1537 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1538 canon_rtx (XVECEXP (y, i, j))) == 0)
1539 return 0;
1540 break;
1542 case 'e':
1543 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1544 canon_rtx (XEXP (y, i))) == 0)
1545 return 0;
1546 break;
1548 /* This can happen for asm operands. */
1549 case 's':
1550 if (strcmp (XSTR (x, i), XSTR (y, i)))
1551 return 0;
1552 break;
1554 /* This can happen for an asm which clobbers memory. */
1555 case '0':
1556 break;
1558 /* It is believed that rtx's at this level will never
1559 contain anything but integers and other rtx's,
1560 except for within LABEL_REFs and SYMBOL_REFs. */
1561 default:
1562 gcc_unreachable ();
1565 return 1;
1568 static rtx
1569 find_base_term (rtx x)
1571 cselib_val *val;
1572 struct elt_loc_list *l, *f;
1573 rtx ret;
1575 #if defined (FIND_BASE_TERM)
1576 /* Try machine-dependent ways to find the base term. */
1577 x = FIND_BASE_TERM (x);
1578 #endif
1580 switch (GET_CODE (x))
1582 case REG:
1583 return REG_BASE_VALUE (x);
1585 case TRUNCATE:
1586 /* As we do not know which address space the pointer is referring to, we can
1587 handle this only if the target does not support different pointer or
1588 address modes depending on the address space. */
1589 if (!target_default_pointer_address_modes_p ())
1590 return 0;
1591 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1592 return 0;
1593 /* Fall through. */
1594 case HIGH:
1595 case PRE_INC:
1596 case PRE_DEC:
1597 case POST_INC:
1598 case POST_DEC:
1599 case PRE_MODIFY:
1600 case POST_MODIFY:
1601 return find_base_term (XEXP (x, 0));
1603 case ZERO_EXTEND:
1604 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1605 /* As we do not know which address space the pointer is referring to, we can
1606 handle this only if the target does not support different pointer or
1607 address modes depending on the address space. */
1608 if (!target_default_pointer_address_modes_p ())
1609 return 0;
1612 rtx temp = find_base_term (XEXP (x, 0));
1614 if (temp != 0 && CONSTANT_P (temp))
1615 temp = convert_memory_address (Pmode, temp);
1617 return temp;
1620 case VALUE:
1621 val = CSELIB_VAL_PTR (x);
1622 ret = NULL_RTX;
1624 if (!val)
1625 return ret;
1627 if (cselib_sp_based_value_p (val))
1628 return static_reg_base_value[STACK_POINTER_REGNUM];
1630 f = val->locs;
1631 /* Temporarily reset val->locs to avoid infinite recursion. */
1632 val->locs = NULL;
1634 for (l = f; l; l = l->next)
1635 if (GET_CODE (l->loc) == VALUE
1636 && CSELIB_VAL_PTR (l->loc)->locs
1637 && !CSELIB_VAL_PTR (l->loc)->locs->next
1638 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1639 continue;
1640 else if ((ret = find_base_term (l->loc)) != 0)
1641 break;
1643 val->locs = f;
1644 return ret;
1646 case LO_SUM:
1647 /* The standard form is (lo_sum reg sym) so look only at the
1648 second operand. */
1649 return find_base_term (XEXP (x, 1));
1651 case CONST:
1652 x = XEXP (x, 0);
1653 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1654 return 0;
1655 /* Fall through. */
1656 case PLUS:
1657 case MINUS:
1659 rtx tmp1 = XEXP (x, 0);
1660 rtx tmp2 = XEXP (x, 1);
1662 /* This is a little bit tricky since we have to determine which of
1663 the two operands represents the real base address. Otherwise this
1664 routine may return the index register instead of the base register.
1666 That may cause us to believe no aliasing was possible, when in
1667 fact aliasing is possible.
1669 We use a few simple tests to guess the base register. Additional
1670 tests can certainly be added. For example, if one of the operands
1671 is a shift or multiply, then it must be the index register and the
1672 other operand is the base register. */
1674 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1675 return find_base_term (tmp2);
1677 /* If either operand is known to be a pointer, then use it
1678 to determine the base term. */
1679 if (REG_P (tmp1) && REG_POINTER (tmp1))
1681 rtx base = find_base_term (tmp1);
1682 if (base)
1683 return base;
1686 if (REG_P (tmp2) && REG_POINTER (tmp2))
1688 rtx base = find_base_term (tmp2);
1689 if (base)
1690 return base;
1693 /* Neither operand was known to be a pointer. Go ahead and find the
1694 base term for both operands. */
1695 tmp1 = find_base_term (tmp1);
1696 tmp2 = find_base_term (tmp2);
1698 /* If either base term is named object or a special address
1699 (like an argument or stack reference), then use it for the
1700 base term. */
1701 if (tmp1 != 0 && known_base_value_p (tmp1))
1702 return tmp1;
1704 if (tmp2 != 0 && known_base_value_p (tmp2))
1705 return tmp2;
1707 /* We could not determine which of the two operands was the
1708 base register and which was the index. So we can determine
1709 nothing from the base alias check. */
1710 return 0;
1713 case AND:
1714 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1715 return find_base_term (XEXP (x, 0));
1716 return 0;
1718 case SYMBOL_REF:
1719 case LABEL_REF:
1720 return x;
1722 default:
1723 return 0;
1727 /* Return true if accesses to address X may alias accesses based
1728 on the stack pointer. */
1730 bool
1731 may_be_sp_based_p (rtx x)
1733 rtx base = find_base_term (x);
1734 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1737 /* Return 0 if the addresses X and Y are known to point to different
1738 objects, 1 if they might be pointers to the same object. */
1740 static int
1741 base_alias_check (rtx x, rtx y, enum machine_mode x_mode,
1742 enum machine_mode y_mode)
1744 rtx x_base = find_base_term (x);
1745 rtx y_base = find_base_term (y);
1747 /* If the address itself has no known base see if a known equivalent
1748 value has one. If either address still has no known base, nothing
1749 is known about aliasing. */
1750 if (x_base == 0)
1752 rtx x_c;
1754 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1755 return 1;
1757 x_base = find_base_term (x_c);
1758 if (x_base == 0)
1759 return 1;
1762 if (y_base == 0)
1764 rtx y_c;
1765 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1766 return 1;
1768 y_base = find_base_term (y_c);
1769 if (y_base == 0)
1770 return 1;
1773 /* If the base addresses are equal nothing is known about aliasing. */
1774 if (rtx_equal_p (x_base, y_base))
1775 return 1;
1777 /* The base addresses are different expressions. If they are not accessed
1778 via AND, there is no conflict. We can bring knowledge of object
1779 alignment into play here. For example, on alpha, "char a, b;" can
1780 alias one another, though "char a; long b;" cannot. AND addesses may
1781 implicitly alias surrounding objects; i.e. unaligned access in DImode
1782 via AND address can alias all surrounding object types except those
1783 with aligment 8 or higher. */
1784 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1785 return 1;
1786 if (GET_CODE (x) == AND
1787 && (!CONST_INT_P (XEXP (x, 1))
1788 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1789 return 1;
1790 if (GET_CODE (y) == AND
1791 && (!CONST_INT_P (XEXP (y, 1))
1792 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1793 return 1;
1795 /* Differing symbols not accessed via AND never alias. */
1796 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1797 return 0;
1799 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
1800 return 0;
1802 return 1;
1805 /* Callback for for_each_rtx, that returns 1 upon encountering a VALUE
1806 whose UID is greater than the int uid that D points to. */
1808 static int
1809 refs_newer_value_cb (rtx *x, void *d)
1811 if (GET_CODE (*x) == VALUE && CSELIB_VAL_PTR (*x)->uid > *(int *)d)
1812 return 1;
1814 return 0;
1817 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1818 that of V. */
1820 static bool
1821 refs_newer_value_p (rtx expr, rtx v)
1823 int minuid = CSELIB_VAL_PTR (v)->uid;
1825 return for_each_rtx (&expr, refs_newer_value_cb, &minuid);
1828 /* Convert the address X into something we can use. This is done by returning
1829 it unchanged unless it is a value; in the latter case we call cselib to get
1830 a more useful rtx. */
1833 get_addr (rtx x)
1835 cselib_val *v;
1836 struct elt_loc_list *l;
1838 if (GET_CODE (x) != VALUE)
1839 return x;
1840 v = CSELIB_VAL_PTR (x);
1841 if (v)
1843 bool have_equivs = cselib_have_permanent_equivalences ();
1844 if (have_equivs)
1845 v = canonical_cselib_val (v);
1846 for (l = v->locs; l; l = l->next)
1847 if (CONSTANT_P (l->loc))
1848 return l->loc;
1849 for (l = v->locs; l; l = l->next)
1850 if (!REG_P (l->loc) && !MEM_P (l->loc)
1851 /* Avoid infinite recursion when potentially dealing with
1852 var-tracking artificial equivalences, by skipping the
1853 equivalences themselves, and not choosing expressions
1854 that refer to newer VALUEs. */
1855 && (!have_equivs
1856 || (GET_CODE (l->loc) != VALUE
1857 && !refs_newer_value_p (l->loc, x))))
1858 return l->loc;
1859 if (have_equivs)
1861 for (l = v->locs; l; l = l->next)
1862 if (REG_P (l->loc)
1863 || (GET_CODE (l->loc) != VALUE
1864 && !refs_newer_value_p (l->loc, x)))
1865 return l->loc;
1866 /* Return the canonical value. */
1867 return v->val_rtx;
1869 if (v->locs)
1870 return v->locs->loc;
1872 return x;
1875 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1876 where SIZE is the size in bytes of the memory reference. If ADDR
1877 is not modified by the memory reference then ADDR is returned. */
1879 static rtx
1880 addr_side_effect_eval (rtx addr, int size, int n_refs)
1882 int offset = 0;
1884 switch (GET_CODE (addr))
1886 case PRE_INC:
1887 offset = (n_refs + 1) * size;
1888 break;
1889 case PRE_DEC:
1890 offset = -(n_refs + 1) * size;
1891 break;
1892 case POST_INC:
1893 offset = n_refs * size;
1894 break;
1895 case POST_DEC:
1896 offset = -n_refs * size;
1897 break;
1899 default:
1900 return addr;
1903 if (offset)
1904 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1905 GEN_INT (offset));
1906 else
1907 addr = XEXP (addr, 0);
1908 addr = canon_rtx (addr);
1910 return addr;
1913 /* Return TRUE if an object X sized at XSIZE bytes and another object
1914 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1915 any of the sizes is zero, assume an overlap, otherwise use the
1916 absolute value of the sizes as the actual sizes. */
1918 static inline bool
1919 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
1921 return (xsize == 0 || ysize == 0
1922 || (c >= 0
1923 ? (abs (xsize) > c)
1924 : (abs (ysize) > -c)));
1927 /* Return one if X and Y (memory addresses) reference the
1928 same location in memory or if the references overlap.
1929 Return zero if they do not overlap, else return
1930 minus one in which case they still might reference the same location.
1932 C is an offset accumulator. When
1933 C is nonzero, we are testing aliases between X and Y + C.
1934 XSIZE is the size in bytes of the X reference,
1935 similarly YSIZE is the size in bytes for Y.
1936 Expect that canon_rtx has been already called for X and Y.
1938 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1939 referenced (the reference was BLKmode), so make the most pessimistic
1940 assumptions.
1942 If XSIZE or YSIZE is negative, we may access memory outside the object
1943 being referenced as a side effect. This can happen when using AND to
1944 align memory references, as is done on the Alpha.
1946 Nice to notice that varying addresses cannot conflict with fp if no
1947 local variables had their addresses taken, but that's too hard now.
1949 ??? Contrary to the tree alias oracle this does not return
1950 one for X + non-constant and Y + non-constant when X and Y are equal.
1951 If that is fixed the TBAA hack for union type-punning can be removed. */
1953 static int
1954 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
1956 if (GET_CODE (x) == VALUE)
1958 if (REG_P (y))
1960 struct elt_loc_list *l = NULL;
1961 if (CSELIB_VAL_PTR (x))
1962 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
1963 l; l = l->next)
1964 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
1965 break;
1966 if (l)
1967 x = y;
1968 else
1969 x = get_addr (x);
1971 /* Don't call get_addr if y is the same VALUE. */
1972 else if (x != y)
1973 x = get_addr (x);
1975 if (GET_CODE (y) == VALUE)
1977 if (REG_P (x))
1979 struct elt_loc_list *l = NULL;
1980 if (CSELIB_VAL_PTR (y))
1981 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
1982 l; l = l->next)
1983 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
1984 break;
1985 if (l)
1986 y = x;
1987 else
1988 y = get_addr (y);
1990 /* Don't call get_addr if x is the same VALUE. */
1991 else if (y != x)
1992 y = get_addr (y);
1994 if (GET_CODE (x) == HIGH)
1995 x = XEXP (x, 0);
1996 else if (GET_CODE (x) == LO_SUM)
1997 x = XEXP (x, 1);
1998 else
1999 x = addr_side_effect_eval (x, abs (xsize), 0);
2000 if (GET_CODE (y) == HIGH)
2001 y = XEXP (y, 0);
2002 else if (GET_CODE (y) == LO_SUM)
2003 y = XEXP (y, 1);
2004 else
2005 y = addr_side_effect_eval (y, abs (ysize), 0);
2007 if (rtx_equal_for_memref_p (x, y))
2009 return offset_overlap_p (c, xsize, ysize);
2012 /* This code used to check for conflicts involving stack references and
2013 globals but the base address alias code now handles these cases. */
2015 if (GET_CODE (x) == PLUS)
2017 /* The fact that X is canonicalized means that this
2018 PLUS rtx is canonicalized. */
2019 rtx x0 = XEXP (x, 0);
2020 rtx x1 = XEXP (x, 1);
2022 if (GET_CODE (y) == PLUS)
2024 /* The fact that Y is canonicalized means that this
2025 PLUS rtx is canonicalized. */
2026 rtx y0 = XEXP (y, 0);
2027 rtx y1 = XEXP (y, 1);
2029 if (rtx_equal_for_memref_p (x1, y1))
2030 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2031 if (rtx_equal_for_memref_p (x0, y0))
2032 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2033 if (CONST_INT_P (x1))
2035 if (CONST_INT_P (y1))
2036 return memrefs_conflict_p (xsize, x0, ysize, y0,
2037 c - INTVAL (x1) + INTVAL (y1));
2038 else
2039 return memrefs_conflict_p (xsize, x0, ysize, y,
2040 c - INTVAL (x1));
2042 else if (CONST_INT_P (y1))
2043 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2045 return -1;
2047 else if (CONST_INT_P (x1))
2048 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2050 else if (GET_CODE (y) == PLUS)
2052 /* The fact that Y is canonicalized means that this
2053 PLUS rtx is canonicalized. */
2054 rtx y0 = XEXP (y, 0);
2055 rtx y1 = XEXP (y, 1);
2057 if (CONST_INT_P (y1))
2058 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2059 else
2060 return -1;
2063 if (GET_CODE (x) == GET_CODE (y))
2064 switch (GET_CODE (x))
2066 case MULT:
2068 /* Handle cases where we expect the second operands to be the
2069 same, and check only whether the first operand would conflict
2070 or not. */
2071 rtx x0, y0;
2072 rtx x1 = canon_rtx (XEXP (x, 1));
2073 rtx y1 = canon_rtx (XEXP (y, 1));
2074 if (! rtx_equal_for_memref_p (x1, y1))
2075 return -1;
2076 x0 = canon_rtx (XEXP (x, 0));
2077 y0 = canon_rtx (XEXP (y, 0));
2078 if (rtx_equal_for_memref_p (x0, y0))
2079 return offset_overlap_p (c, xsize, ysize);
2081 /* Can't properly adjust our sizes. */
2082 if (!CONST_INT_P (x1))
2083 return -1;
2084 xsize /= INTVAL (x1);
2085 ysize /= INTVAL (x1);
2086 c /= INTVAL (x1);
2087 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2090 default:
2091 break;
2094 /* Deal with alignment ANDs by adjusting offset and size so as to
2095 cover the maximum range, without taking any previously known
2096 alignment into account. Make a size negative after such an
2097 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2098 assume a potential overlap, because they may end up in contiguous
2099 memory locations and the stricter-alignment access may span over
2100 part of both. */
2101 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2103 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2104 unsigned HOST_WIDE_INT uc = sc;
2105 if (sc < 0 && -uc == (uc & -uc))
2107 if (xsize > 0)
2108 xsize = -xsize;
2109 if (xsize)
2110 xsize += sc + 1;
2111 c -= sc + 1;
2112 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2113 ysize, y, c);
2116 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2118 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2119 unsigned HOST_WIDE_INT uc = sc;
2120 if (sc < 0 && -uc == (uc & -uc))
2122 if (ysize > 0)
2123 ysize = -ysize;
2124 if (ysize)
2125 ysize += sc + 1;
2126 c += sc + 1;
2127 return memrefs_conflict_p (xsize, x,
2128 ysize, canon_rtx (XEXP (y, 0)), c);
2132 if (CONSTANT_P (x))
2134 if (CONST_INT_P (x) && CONST_INT_P (y))
2136 c += (INTVAL (y) - INTVAL (x));
2137 return offset_overlap_p (c, xsize, ysize);
2140 if (GET_CODE (x) == CONST)
2142 if (GET_CODE (y) == CONST)
2143 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2144 ysize, canon_rtx (XEXP (y, 0)), c);
2145 else
2146 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2147 ysize, y, c);
2149 if (GET_CODE (y) == CONST)
2150 return memrefs_conflict_p (xsize, x, ysize,
2151 canon_rtx (XEXP (y, 0)), c);
2153 /* Assume a potential overlap for symbolic addresses that went
2154 through alignment adjustments (i.e., that have negative
2155 sizes), because we can't know how far they are from each
2156 other. */
2157 if (CONSTANT_P (y))
2158 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2160 return -1;
2163 return -1;
2166 /* Functions to compute memory dependencies.
2168 Since we process the insns in execution order, we can build tables
2169 to keep track of what registers are fixed (and not aliased), what registers
2170 are varying in known ways, and what registers are varying in unknown
2171 ways.
2173 If both memory references are volatile, then there must always be a
2174 dependence between the two references, since their order can not be
2175 changed. A volatile and non-volatile reference can be interchanged
2176 though.
2178 We also must allow AND addresses, because they may generate accesses
2179 outside the object being referenced. This is used to generate aligned
2180 addresses from unaligned addresses, for instance, the alpha
2181 storeqi_unaligned pattern. */
2183 /* Read dependence: X is read after read in MEM takes place. There can
2184 only be a dependence here if both reads are volatile, or if either is
2185 an explicit barrier. */
2188 read_dependence (const_rtx mem, const_rtx x)
2190 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2191 return true;
2192 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2193 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2194 return true;
2195 return false;
2198 /* Return true if we can determine that the fields referenced cannot
2199 overlap for any pair of objects. */
2201 static bool
2202 nonoverlapping_component_refs_p (const_rtx rtlx, const_rtx rtly)
2204 const_tree x = MEM_EXPR (rtlx), y = MEM_EXPR (rtly);
2205 const_tree fieldx, fieldy, typex, typey, orig_y;
2207 if (!flag_strict_aliasing
2208 || !x || !y
2209 || TREE_CODE (x) != COMPONENT_REF
2210 || TREE_CODE (y) != COMPONENT_REF)
2211 return false;
2215 /* The comparison has to be done at a common type, since we don't
2216 know how the inheritance hierarchy works. */
2217 orig_y = y;
2220 fieldx = TREE_OPERAND (x, 1);
2221 typex = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx));
2223 y = orig_y;
2226 fieldy = TREE_OPERAND (y, 1);
2227 typey = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy));
2229 if (typex == typey)
2230 goto found;
2232 y = TREE_OPERAND (y, 0);
2234 while (y && TREE_CODE (y) == COMPONENT_REF);
2236 x = TREE_OPERAND (x, 0);
2238 while (x && TREE_CODE (x) == COMPONENT_REF);
2239 /* Never found a common type. */
2240 return false;
2242 found:
2243 /* If we're left with accessing different fields of a structure, then no
2244 possible overlap, unless they are both bitfields. */
2245 if (TREE_CODE (typex) == RECORD_TYPE && fieldx != fieldy)
2246 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
2248 /* The comparison on the current field failed. If we're accessing
2249 a very nested structure, look at the next outer level. */
2250 x = TREE_OPERAND (x, 0);
2251 y = TREE_OPERAND (y, 0);
2253 while (x && y
2254 && TREE_CODE (x) == COMPONENT_REF
2255 && TREE_CODE (y) == COMPONENT_REF);
2257 return false;
2260 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2262 static tree
2263 decl_for_component_ref (tree x)
2267 x = TREE_OPERAND (x, 0);
2269 while (x && TREE_CODE (x) == COMPONENT_REF);
2271 return x && DECL_P (x) ? x : NULL_TREE;
2274 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2275 for the offset of the field reference. *KNOWN_P says whether the
2276 offset is known. */
2278 static void
2279 adjust_offset_for_component_ref (tree x, bool *known_p,
2280 HOST_WIDE_INT *offset)
2282 if (!*known_p)
2283 return;
2286 tree xoffset = component_ref_field_offset (x);
2287 tree field = TREE_OPERAND (x, 1);
2289 if (! host_integerp (xoffset, 1))
2291 *known_p = false;
2292 return;
2294 *offset += (tree_low_cst (xoffset, 1)
2295 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
2296 / BITS_PER_UNIT));
2298 x = TREE_OPERAND (x, 0);
2300 while (x && TREE_CODE (x) == COMPONENT_REF);
2303 /* Return nonzero if we can determine the exprs corresponding to memrefs
2304 X and Y and they do not overlap.
2305 If LOOP_VARIANT is set, skip offset-based disambiguation */
2308 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2310 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2311 rtx rtlx, rtly;
2312 rtx basex, basey;
2313 bool moffsetx_known_p, moffsety_known_p;
2314 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2315 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2317 /* Unless both have exprs, we can't tell anything. */
2318 if (exprx == 0 || expry == 0)
2319 return 0;
2321 /* For spill-slot accesses make sure we have valid offsets. */
2322 if ((exprx == get_spill_slot_decl (false)
2323 && ! MEM_OFFSET_KNOWN_P (x))
2324 || (expry == get_spill_slot_decl (false)
2325 && ! MEM_OFFSET_KNOWN_P (y)))
2326 return 0;
2328 /* If the field reference test failed, look at the DECLs involved. */
2329 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2330 if (moffsetx_known_p)
2331 moffsetx = MEM_OFFSET (x);
2332 if (TREE_CODE (exprx) == COMPONENT_REF)
2334 tree t = decl_for_component_ref (exprx);
2335 if (! t)
2336 return 0;
2337 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2338 exprx = t;
2341 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2342 if (moffsety_known_p)
2343 moffsety = MEM_OFFSET (y);
2344 if (TREE_CODE (expry) == COMPONENT_REF)
2346 tree t = decl_for_component_ref (expry);
2347 if (! t)
2348 return 0;
2349 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2350 expry = t;
2353 if (! DECL_P (exprx) || ! DECL_P (expry))
2354 return 0;
2356 /* With invalid code we can end up storing into the constant pool.
2357 Bail out to avoid ICEing when creating RTL for this.
2358 See gfortran.dg/lto/20091028-2_0.f90. */
2359 if (TREE_CODE (exprx) == CONST_DECL
2360 || TREE_CODE (expry) == CONST_DECL)
2361 return 1;
2363 rtlx = DECL_RTL (exprx);
2364 rtly = DECL_RTL (expry);
2366 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2367 can't overlap unless they are the same because we never reuse that part
2368 of the stack frame used for locals for spilled pseudos. */
2369 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2370 && ! rtx_equal_p (rtlx, rtly))
2371 return 1;
2373 /* If we have MEMs referring to different address spaces (which can
2374 potentially overlap), we cannot easily tell from the addresses
2375 whether the references overlap. */
2376 if (MEM_P (rtlx) && MEM_P (rtly)
2377 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2378 return 0;
2380 /* Get the base and offsets of both decls. If either is a register, we
2381 know both are and are the same, so use that as the base. The only
2382 we can avoid overlap is if we can deduce that they are nonoverlapping
2383 pieces of that decl, which is very rare. */
2384 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2385 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2386 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2388 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2389 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2390 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2392 /* If the bases are different, we know they do not overlap if both
2393 are constants or if one is a constant and the other a pointer into the
2394 stack frame. Otherwise a different base means we can't tell if they
2395 overlap or not. */
2396 if (! rtx_equal_p (basex, basey))
2397 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2398 || (CONSTANT_P (basex) && REG_P (basey)
2399 && REGNO_PTR_FRAME_P (REGNO (basey)))
2400 || (CONSTANT_P (basey) && REG_P (basex)
2401 && REGNO_PTR_FRAME_P (REGNO (basex))));
2403 /* Offset based disambiguation not appropriate for loop invariant */
2404 if (loop_invariant)
2405 return 0;
2407 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2408 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2409 : -1);
2410 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2411 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2412 : -1);
2414 /* If we have an offset for either memref, it can update the values computed
2415 above. */
2416 if (moffsetx_known_p)
2417 offsetx += moffsetx, sizex -= moffsetx;
2418 if (moffsety_known_p)
2419 offsety += moffsety, sizey -= moffsety;
2421 /* If a memref has both a size and an offset, we can use the smaller size.
2422 We can't do this if the offset isn't known because we must view this
2423 memref as being anywhere inside the DECL's MEM. */
2424 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2425 sizex = MEM_SIZE (x);
2426 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2427 sizey = MEM_SIZE (y);
2429 /* Put the values of the memref with the lower offset in X's values. */
2430 if (offsetx > offsety)
2432 tem = offsetx, offsetx = offsety, offsety = tem;
2433 tem = sizex, sizex = sizey, sizey = tem;
2436 /* If we don't know the size of the lower-offset value, we can't tell
2437 if they conflict. Otherwise, we do the test. */
2438 return sizex >= 0 && offsety >= offsetx + sizex;
2441 /* Helper for true_dependence and canon_true_dependence.
2442 Checks for true dependence: X is read after store in MEM takes place.
2444 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2445 NULL_RTX, and the canonical addresses of MEM and X are both computed
2446 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2448 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2450 Returns 1 if there is a true dependence, 0 otherwise. */
2452 static int
2453 true_dependence_1 (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2454 const_rtx x, rtx x_addr, bool mem_canonicalized)
2456 rtx base;
2457 int ret;
2459 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2460 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2462 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2463 return 1;
2465 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2466 This is used in epilogue deallocation functions, and in cselib. */
2467 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2468 return 1;
2469 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2470 return 1;
2471 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2472 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2473 return 1;
2475 /* Read-only memory is by definition never modified, and therefore can't
2476 conflict with anything. We don't expect to find read-only set on MEM,
2477 but stupid user tricks can produce them, so don't die. */
2478 if (MEM_READONLY_P (x))
2479 return 0;
2481 /* If we have MEMs referring to different address spaces (which can
2482 potentially overlap), we cannot easily tell from the addresses
2483 whether the references overlap. */
2484 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2485 return 1;
2487 if (! mem_addr)
2489 mem_addr = XEXP (mem, 0);
2490 if (mem_mode == VOIDmode)
2491 mem_mode = GET_MODE (mem);
2494 if (! x_addr)
2496 x_addr = XEXP (x, 0);
2497 if (!((GET_CODE (x_addr) == VALUE
2498 && GET_CODE (mem_addr) != VALUE
2499 && reg_mentioned_p (x_addr, mem_addr))
2500 || (GET_CODE (x_addr) != VALUE
2501 && GET_CODE (mem_addr) == VALUE
2502 && reg_mentioned_p (mem_addr, x_addr))))
2504 x_addr = get_addr (x_addr);
2505 if (! mem_canonicalized)
2506 mem_addr = get_addr (mem_addr);
2510 base = find_base_term (x_addr);
2511 if (base && (GET_CODE (base) == LABEL_REF
2512 || (GET_CODE (base) == SYMBOL_REF
2513 && CONSTANT_POOL_ADDRESS_P (base))))
2514 return 0;
2516 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2517 return 0;
2519 x_addr = canon_rtx (x_addr);
2520 if (!mem_canonicalized)
2521 mem_addr = canon_rtx (mem_addr);
2523 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2524 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2525 return ret;
2527 if (mems_in_disjoint_alias_sets_p (x, mem))
2528 return 0;
2530 if (nonoverlapping_memrefs_p (mem, x, false))
2531 return 0;
2533 if (nonoverlapping_component_refs_p (mem, x))
2534 return 0;
2536 return rtx_refs_may_alias_p (x, mem, true);
2539 /* True dependence: X is read after store in MEM takes place. */
2542 true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x)
2544 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2545 x, NULL_RTX, /*mem_canonicalized=*/false);
2548 /* Canonical true dependence: X is read after store in MEM takes place.
2549 Variant of true_dependence which assumes MEM has already been
2550 canonicalized (hence we no longer do that here).
2551 The mem_addr argument has been added, since true_dependence_1 computed
2552 this value prior to canonicalizing. */
2555 canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2556 const_rtx x, rtx x_addr)
2558 return true_dependence_1 (mem, mem_mode, mem_addr,
2559 x, x_addr, /*mem_canonicalized=*/true);
2562 /* Returns nonzero if a write to X might alias a previous read from
2563 (or, if WRITEP is true, a write to) MEM.
2564 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2565 and X_MODE the mode for that access.
2566 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2568 static int
2569 write_dependence_p (const_rtx mem,
2570 const_rtx x, enum machine_mode x_mode, rtx x_addr,
2571 bool mem_canonicalized, bool x_canonicalized, bool writep)
2573 rtx mem_addr;
2574 rtx base;
2575 int ret;
2577 gcc_checking_assert (x_canonicalized
2578 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2579 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2581 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2582 return 1;
2584 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2585 This is used in epilogue deallocation functions. */
2586 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2587 return 1;
2588 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2589 return 1;
2590 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2591 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2592 return 1;
2594 /* A read from read-only memory can't conflict with read-write memory. */
2595 if (!writep && MEM_READONLY_P (mem))
2596 return 0;
2598 /* If we have MEMs referring to different address spaces (which can
2599 potentially overlap), we cannot easily tell from the addresses
2600 whether the references overlap. */
2601 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2602 return 1;
2604 mem_addr = XEXP (mem, 0);
2605 if (!x_addr)
2607 x_addr = XEXP (x, 0);
2608 if (!((GET_CODE (x_addr) == VALUE
2609 && GET_CODE (mem_addr) != VALUE
2610 && reg_mentioned_p (x_addr, mem_addr))
2611 || (GET_CODE (x_addr) != VALUE
2612 && GET_CODE (mem_addr) == VALUE
2613 && reg_mentioned_p (mem_addr, x_addr))))
2615 x_addr = get_addr (x_addr);
2616 if (!mem_canonicalized)
2617 mem_addr = get_addr (mem_addr);
2621 if (! writep)
2623 base = find_base_term (mem_addr);
2624 if (base && (GET_CODE (base) == LABEL_REF
2625 || (GET_CODE (base) == SYMBOL_REF
2626 && CONSTANT_POOL_ADDRESS_P (base))))
2627 return 0;
2630 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2631 GET_MODE (mem)))
2632 return 0;
2634 if (!x_canonicalized)
2636 x_addr = canon_rtx (x_addr);
2637 x_mode = GET_MODE (x);
2639 if (!mem_canonicalized)
2640 mem_addr = canon_rtx (mem_addr);
2642 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2643 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2644 return ret;
2646 if (nonoverlapping_memrefs_p (x, mem, false))
2647 return 0;
2649 return rtx_refs_may_alias_p (x, mem, false);
2652 /* Anti dependence: X is written after read in MEM takes place. */
2655 anti_dependence (const_rtx mem, const_rtx x)
2657 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2658 /*mem_canonicalized=*/false,
2659 /*x_canonicalized*/false, /*writep=*/false);
2662 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2663 Also, consider X in X_MODE (which might be from an enclosing
2664 STRICT_LOW_PART / ZERO_EXTRACT).
2665 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2668 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2669 const_rtx x, enum machine_mode x_mode, rtx x_addr)
2671 return write_dependence_p (mem, x, x_mode, x_addr,
2672 mem_canonicalized, /*x_canonicalized=*/true,
2673 /*writep=*/false);
2676 /* Output dependence: X is written after store in MEM takes place. */
2679 output_dependence (const_rtx mem, const_rtx x)
2681 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2682 /*mem_canonicalized=*/false,
2683 /*x_canonicalized*/false, /*writep=*/true);
2688 /* Check whether X may be aliased with MEM. Don't do offset-based
2689 memory disambiguation & TBAA. */
2691 may_alias_p (const_rtx mem, const_rtx x)
2693 rtx x_addr, mem_addr;
2695 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2696 return 1;
2698 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2699 This is used in epilogue deallocation functions. */
2700 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2701 return 1;
2702 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2703 return 1;
2704 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2705 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2706 return 1;
2708 /* Read-only memory is by definition never modified, and therefore can't
2709 conflict with anything. We don't expect to find read-only set on MEM,
2710 but stupid user tricks can produce them, so don't die. */
2711 if (MEM_READONLY_P (x))
2712 return 0;
2714 /* If we have MEMs referring to different address spaces (which can
2715 potentially overlap), we cannot easily tell from the addresses
2716 whether the references overlap. */
2717 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2718 return 1;
2720 x_addr = XEXP (x, 0);
2721 mem_addr = XEXP (mem, 0);
2722 if (!((GET_CODE (x_addr) == VALUE
2723 && GET_CODE (mem_addr) != VALUE
2724 && reg_mentioned_p (x_addr, mem_addr))
2725 || (GET_CODE (x_addr) != VALUE
2726 && GET_CODE (mem_addr) == VALUE
2727 && reg_mentioned_p (mem_addr, x_addr))))
2729 x_addr = get_addr (x_addr);
2730 mem_addr = get_addr (mem_addr);
2733 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), GET_MODE (mem_addr)))
2734 return 0;
2736 x_addr = canon_rtx (x_addr);
2737 mem_addr = canon_rtx (mem_addr);
2739 if (nonoverlapping_memrefs_p (mem, x, true))
2740 return 0;
2742 /* TBAA not valid for loop_invarint */
2743 return rtx_refs_may_alias_p (x, mem, false);
2746 void
2747 init_alias_target (void)
2749 int i;
2751 if (!arg_base_value)
2752 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2754 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2756 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2757 /* Check whether this register can hold an incoming pointer
2758 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2759 numbers, so translate if necessary due to register windows. */
2760 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2761 && HARD_REGNO_MODE_OK (i, Pmode))
2762 static_reg_base_value[i] = arg_base_value;
2764 static_reg_base_value[STACK_POINTER_REGNUM]
2765 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2766 static_reg_base_value[ARG_POINTER_REGNUM]
2767 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2768 static_reg_base_value[FRAME_POINTER_REGNUM]
2769 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2770 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2771 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2772 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2773 #endif
2776 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2777 to be memory reference. */
2778 static bool memory_modified;
2779 static void
2780 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2782 if (MEM_P (x))
2784 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2785 memory_modified = true;
2790 /* Return true when INSN possibly modify memory contents of MEM
2791 (i.e. address can be modified). */
2792 bool
2793 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2795 if (!INSN_P (insn))
2796 return false;
2797 memory_modified = false;
2798 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2799 return memory_modified;
2802 /* Return TRUE if the destination of a set is rtx identical to
2803 ITEM. */
2804 static inline bool
2805 set_dest_equal_p (const_rtx set, const_rtx item)
2807 rtx dest = SET_DEST (set);
2808 return rtx_equal_p (dest, item);
2811 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2812 *DEFINITELY* modify the memory contents of MEM. */
2813 bool
2814 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2816 if (!INSN_P (insn))
2817 return false;
2818 insn = PATTERN (insn);
2819 if (GET_CODE (insn) == SET)
2820 return set_dest_equal_p (insn, mem);
2821 else if (GET_CODE (insn) == PARALLEL)
2823 int i;
2824 for (i = 0; i < XVECLEN (insn, 0); i++)
2826 rtx sub = XVECEXP (insn, 0, i);
2827 if (GET_CODE (sub) == SET
2828 && set_dest_equal_p (sub, mem))
2829 return true;
2832 return false;
2835 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2836 array. */
2838 void
2839 init_alias_analysis (void)
2841 unsigned int maxreg = max_reg_num ();
2842 int changed, pass;
2843 int i;
2844 unsigned int ui;
2845 rtx insn, val;
2846 int rpo_cnt;
2847 int *rpo;
2849 timevar_push (TV_ALIAS_ANALYSIS);
2851 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
2852 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
2853 bitmap_clear (reg_known_equiv_p);
2855 /* If we have memory allocated from the previous run, use it. */
2856 if (old_reg_base_value)
2857 reg_base_value = old_reg_base_value;
2859 if (reg_base_value)
2860 reg_base_value->truncate (0);
2862 vec_safe_grow_cleared (reg_base_value, maxreg);
2864 new_reg_base_value = XNEWVEC (rtx, maxreg);
2865 reg_seen = sbitmap_alloc (maxreg);
2867 /* The basic idea is that each pass through this loop will use the
2868 "constant" information from the previous pass to propagate alias
2869 information through another level of assignments.
2871 The propagation is done on the CFG in reverse post-order, to propagate
2872 things forward as far as possible in each iteration.
2874 This could get expensive if the assignment chains are long. Maybe
2875 we should throttle the number of iterations, possibly based on
2876 the optimization level or flag_expensive_optimizations.
2878 We could propagate more information in the first pass by making use
2879 of DF_REG_DEF_COUNT to determine immediately that the alias information
2880 for a pseudo is "constant".
2882 A program with an uninitialized variable can cause an infinite loop
2883 here. Instead of doing a full dataflow analysis to detect such problems
2884 we just cap the number of iterations for the loop.
2886 The state of the arrays for the set chain in question does not matter
2887 since the program has undefined behavior. */
2889 rpo = XNEWVEC (int, n_basic_blocks);
2890 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
2892 pass = 0;
2895 /* Assume nothing will change this iteration of the loop. */
2896 changed = 0;
2898 /* We want to assign the same IDs each iteration of this loop, so
2899 start counting from one each iteration of the loop. */
2900 unique_id = 1;
2902 /* We're at the start of the function each iteration through the
2903 loop, so we're copying arguments. */
2904 copying_arguments = true;
2906 /* Wipe the potential alias information clean for this pass. */
2907 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2909 /* Wipe the reg_seen array clean. */
2910 bitmap_clear (reg_seen);
2912 /* Initialize the alias information for this pass. */
2913 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2914 if (static_reg_base_value[i])
2916 new_reg_base_value[i] = static_reg_base_value[i];
2917 bitmap_set_bit (reg_seen, i);
2920 /* Walk the insns adding values to the new_reg_base_value array. */
2921 for (i = 0; i < rpo_cnt; i++)
2923 basic_block bb = BASIC_BLOCK (rpo[i]);
2924 FOR_BB_INSNS (bb, insn)
2926 if (NONDEBUG_INSN_P (insn))
2928 rtx note, set;
2930 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2931 /* The prologue/epilogue insns are not threaded onto the
2932 insn chain until after reload has completed. Thus,
2933 there is no sense wasting time checking if INSN is in
2934 the prologue/epilogue until after reload has completed. */
2935 if (reload_completed
2936 && prologue_epilogue_contains (insn))
2937 continue;
2938 #endif
2940 /* If this insn has a noalias note, process it, Otherwise,
2941 scan for sets. A simple set will have no side effects
2942 which could change the base value of any other register. */
2944 if (GET_CODE (PATTERN (insn)) == SET
2945 && REG_NOTES (insn) != 0
2946 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2947 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2948 else
2949 note_stores (PATTERN (insn), record_set, NULL);
2951 set = single_set (insn);
2953 if (set != 0
2954 && REG_P (SET_DEST (set))
2955 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2957 unsigned int regno = REGNO (SET_DEST (set));
2958 rtx src = SET_SRC (set);
2959 rtx t;
2961 note = find_reg_equal_equiv_note (insn);
2962 if (note && REG_NOTE_KIND (note) == REG_EQUAL
2963 && DF_REG_DEF_COUNT (regno) != 1)
2964 note = NULL_RTX;
2966 if (note != NULL_RTX
2967 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2968 && ! rtx_varies_p (XEXP (note, 0), 1)
2969 && ! reg_overlap_mentioned_p (SET_DEST (set),
2970 XEXP (note, 0)))
2972 set_reg_known_value (regno, XEXP (note, 0));
2973 set_reg_known_equiv_p (regno,
2974 REG_NOTE_KIND (note) == REG_EQUIV);
2976 else if (DF_REG_DEF_COUNT (regno) == 1
2977 && GET_CODE (src) == PLUS
2978 && REG_P (XEXP (src, 0))
2979 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2980 && CONST_INT_P (XEXP (src, 1)))
2982 t = plus_constant (GET_MODE (src), t,
2983 INTVAL (XEXP (src, 1)));
2984 set_reg_known_value (regno, t);
2985 set_reg_known_equiv_p (regno, false);
2987 else if (DF_REG_DEF_COUNT (regno) == 1
2988 && ! rtx_varies_p (src, 1))
2990 set_reg_known_value (regno, src);
2991 set_reg_known_equiv_p (regno, false);
2995 else if (NOTE_P (insn)
2996 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
2997 copying_arguments = false;
3001 /* Now propagate values from new_reg_base_value to reg_base_value. */
3002 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3004 for (ui = 0; ui < maxreg; ui++)
3006 if (new_reg_base_value[ui]
3007 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3008 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3010 (*reg_base_value)[ui] = new_reg_base_value[ui];
3011 changed = 1;
3015 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3016 XDELETEVEC (rpo);
3018 /* Fill in the remaining entries. */
3019 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3021 int regno = i + FIRST_PSEUDO_REGISTER;
3022 if (! val)
3023 set_reg_known_value (regno, regno_reg_rtx[regno]);
3026 /* Clean up. */
3027 free (new_reg_base_value);
3028 new_reg_base_value = 0;
3029 sbitmap_free (reg_seen);
3030 reg_seen = 0;
3031 timevar_pop (TV_ALIAS_ANALYSIS);
3034 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3035 Special API for var-tracking pass purposes. */
3037 void
3038 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3040 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3043 void
3044 end_alias_analysis (void)
3046 old_reg_base_value = reg_base_value;
3047 vec_free (reg_known_value);
3048 sbitmap_free (reg_known_equiv_p);
3051 #include "gt-alias.h"