libfuncs.h (LTI_synchronize): New libfunc_index.
[official-gcc.git] / gcc / alias.c
blob684205cd89e35ce1bb80135cfd0e78de80a814cb
1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007 Free Software Foundation, Inc.
4 Contributed by John Carr (jfc@mit.edu).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "function.h"
30 #include "alias.h"
31 #include "emit-rtl.h"
32 #include "regs.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "flags.h"
36 #include "output.h"
37 #include "toplev.h"
38 #include "cselib.h"
39 #include "splay-tree.h"
40 #include "ggc.h"
41 #include "langhooks.h"
42 #include "timevar.h"
43 #include "target.h"
44 #include "cgraph.h"
45 #include "varray.h"
46 #include "tree-pass.h"
47 #include "ipa-type-escape.h"
48 #include "df.h"
50 /* The aliasing API provided here solves related but different problems:
52 Say there exists (in c)
54 struct X {
55 struct Y y1;
56 struct Z z2;
57 } x1, *px1, *px2;
59 struct Y y2, *py;
60 struct Z z2, *pz;
63 py = &px1.y1;
64 px2 = &x1;
66 Consider the four questions:
68 Can a store to x1 interfere with px2->y1?
69 Can a store to x1 interfere with px2->z2?
70 (*px2).z2
71 Can a store to x1 change the value pointed to by with py?
72 Can a store to x1 change the value pointed to by with pz?
74 The answer to these questions can be yes, yes, yes, and maybe.
76 The first two questions can be answered with a simple examination
77 of the type system. If structure X contains a field of type Y then
78 a store thru a pointer to an X can overwrite any field that is
79 contained (recursively) in an X (unless we know that px1 != px2).
81 The last two of the questions can be solved in the same way as the
82 first two questions but this is too conservative. The observation
83 is that in some cases analysis we can know if which (if any) fields
84 are addressed and if those addresses are used in bad ways. This
85 analysis may be language specific. In C, arbitrary operations may
86 be applied to pointers. However, there is some indication that
87 this may be too conservative for some C++ types.
89 The pass ipa-type-escape does this analysis for the types whose
90 instances do not escape across the compilation boundary.
92 Historically in GCC, these two problems were combined and a single
93 data structure was used to represent the solution to these
94 problems. We now have two similar but different data structures,
95 The data structure to solve the last two question is similar to the
96 first, but does not contain have the fields in it whose address are
97 never taken. For types that do escape the compilation unit, the
98 data structures will have identical information.
101 /* The alias sets assigned to MEMs assist the back-end in determining
102 which MEMs can alias which other MEMs. In general, two MEMs in
103 different alias sets cannot alias each other, with one important
104 exception. Consider something like:
106 struct S { int i; double d; };
108 a store to an `S' can alias something of either type `int' or type
109 `double'. (However, a store to an `int' cannot alias a `double'
110 and vice versa.) We indicate this via a tree structure that looks
111 like:
112 struct S
115 |/_ _\|
116 int double
118 (The arrows are directed and point downwards.)
119 In this situation we say the alias set for `struct S' is the
120 `superset' and that those for `int' and `double' are `subsets'.
122 To see whether two alias sets can point to the same memory, we must
123 see if either alias set is a subset of the other. We need not trace
124 past immediate descendants, however, since we propagate all
125 grandchildren up one level.
127 Alias set zero is implicitly a superset of all other alias sets.
128 However, this is no actual entry for alias set zero. It is an
129 error to attempt to explicitly construct a subset of zero. */
131 struct alias_set_entry GTY(())
133 /* The alias set number, as stored in MEM_ALIAS_SET. */
134 alias_set_type alias_set;
136 /* Nonzero if would have a child of zero: this effectively makes this
137 alias set the same as alias set zero. */
138 int has_zero_child;
140 /* The children of the alias set. These are not just the immediate
141 children, but, in fact, all descendants. So, if we have:
143 struct T { struct S s; float f; }
145 continuing our example above, the children here will be all of
146 `int', `double', `float', and `struct S'. */
147 splay_tree GTY((param1_is (int), param2_is (int))) children;
149 typedef struct alias_set_entry *alias_set_entry;
151 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
152 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
153 static void record_set (rtx, const_rtx, void *);
154 static int base_alias_check (rtx, rtx, enum machine_mode,
155 enum machine_mode);
156 static rtx find_base_value (rtx);
157 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
158 static int insert_subset_children (splay_tree_node, void*);
159 static tree find_base_decl (tree);
160 static alias_set_entry get_alias_set_entry (alias_set_type);
161 static const_rtx fixed_scalar_and_varying_struct_p (const_rtx, const_rtx, rtx, rtx,
162 bool (*) (const_rtx, bool));
163 static int aliases_everything_p (const_rtx);
164 static bool nonoverlapping_component_refs_p (const_tree, const_tree);
165 static tree decl_for_component_ref (tree);
166 static rtx adjust_offset_for_component_ref (tree, rtx);
167 static int write_dependence_p (const_rtx, const_rtx, int);
169 static void memory_modified_1 (rtx, const_rtx, void *);
170 static void record_alias_subset (alias_set_type, alias_set_type);
172 /* Set up all info needed to perform alias analysis on memory references. */
174 /* Returns the size in bytes of the mode of X. */
175 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
177 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
178 different alias sets. We ignore alias sets in functions making use
179 of variable arguments because the va_arg macros on some systems are
180 not legal ANSI C. */
181 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
182 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
184 /* Cap the number of passes we make over the insns propagating alias
185 information through set chains. 10 is a completely arbitrary choice. */
186 #define MAX_ALIAS_LOOP_PASSES 10
188 /* reg_base_value[N] gives an address to which register N is related.
189 If all sets after the first add or subtract to the current value
190 or otherwise modify it so it does not point to a different top level
191 object, reg_base_value[N] is equal to the address part of the source
192 of the first set.
194 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
195 expressions represent certain special values: function arguments and
196 the stack, frame, and argument pointers.
198 The contents of an ADDRESS is not normally used, the mode of the
199 ADDRESS determines whether the ADDRESS is a function argument or some
200 other special value. Pointer equality, not rtx_equal_p, determines whether
201 two ADDRESS expressions refer to the same base address.
203 The only use of the contents of an ADDRESS is for determining if the
204 current function performs nonlocal memory memory references for the
205 purposes of marking the function as a constant function. */
207 static GTY(()) VEC(rtx,gc) *reg_base_value;
208 static rtx *new_reg_base_value;
210 /* We preserve the copy of old array around to avoid amount of garbage
211 produced. About 8% of garbage produced were attributed to this
212 array. */
213 static GTY((deletable)) VEC(rtx,gc) *old_reg_base_value;
215 /* Static hunks of RTL used by the aliasing code; these are initialized
216 once per function to avoid unnecessary RTL allocations. */
217 static GTY (()) rtx static_reg_base_value[FIRST_PSEUDO_REGISTER];
219 #define REG_BASE_VALUE(X) \
220 (REGNO (X) < VEC_length (rtx, reg_base_value) \
221 ? VEC_index (rtx, reg_base_value, REGNO (X)) : 0)
223 /* Vector indexed by N giving the initial (unchanging) value known for
224 pseudo-register N. This array is initialized in init_alias_analysis,
225 and does not change until end_alias_analysis is called. */
226 static GTY((length("reg_known_value_size"))) rtx *reg_known_value;
228 /* Indicates number of valid entries in reg_known_value. */
229 static GTY(()) unsigned int reg_known_value_size;
231 /* Vector recording for each reg_known_value whether it is due to a
232 REG_EQUIV note. Future passes (viz., reload) may replace the
233 pseudo with the equivalent expression and so we account for the
234 dependences that would be introduced if that happens.
236 The REG_EQUIV notes created in assign_parms may mention the arg
237 pointer, and there are explicit insns in the RTL that modify the
238 arg pointer. Thus we must ensure that such insns don't get
239 scheduled across each other because that would invalidate the
240 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
241 wrong, but solving the problem in the scheduler will likely give
242 better code, so we do it here. */
243 static bool *reg_known_equiv_p;
245 /* True when scanning insns from the start of the rtl to the
246 NOTE_INSN_FUNCTION_BEG note. */
247 static bool copying_arguments;
249 DEF_VEC_P(alias_set_entry);
250 DEF_VEC_ALLOC_P(alias_set_entry,gc);
252 /* The splay-tree used to store the various alias set entries. */
253 static GTY (()) VEC(alias_set_entry,gc) *alias_sets;
255 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
256 such an entry, or NULL otherwise. */
258 static inline alias_set_entry
259 get_alias_set_entry (alias_set_type alias_set)
261 return VEC_index (alias_set_entry, alias_sets, alias_set);
264 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
265 the two MEMs cannot alias each other. */
267 static inline int
268 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
270 /* Perform a basic sanity check. Namely, that there are no alias sets
271 if we're not using strict aliasing. This helps to catch bugs
272 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
273 where a MEM is allocated in some way other than by the use of
274 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
275 use alias sets to indicate that spilled registers cannot alias each
276 other, we might need to remove this check. */
277 gcc_assert (flag_strict_aliasing
278 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
280 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
283 /* Insert the NODE into the splay tree given by DATA. Used by
284 record_alias_subset via splay_tree_foreach. */
286 static int
287 insert_subset_children (splay_tree_node node, void *data)
289 splay_tree_insert ((splay_tree) data, node->key, node->value);
291 return 0;
294 /* Return true if the first alias set is a subset of the second. */
296 bool
297 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
299 alias_set_entry ase;
301 /* Everything is a subset of the "aliases everything" set. */
302 if (set2 == 0)
303 return true;
305 /* Otherwise, check if set1 is a subset of set2. */
306 ase = get_alias_set_entry (set2);
307 if (ase != 0
308 && ((ase->has_zero_child && set1 == 0)
309 || splay_tree_lookup (ase->children,
310 (splay_tree_key) set1)))
311 return true;
312 return false;
315 /* Return 1 if the two specified alias sets may conflict. */
318 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
320 alias_set_entry ase;
322 /* The easy case. */
323 if (alias_sets_must_conflict_p (set1, set2))
324 return 1;
326 /* See if the first alias set is a subset of the second. */
327 ase = get_alias_set_entry (set1);
328 if (ase != 0
329 && (ase->has_zero_child
330 || splay_tree_lookup (ase->children,
331 (splay_tree_key) set2)))
332 return 1;
334 /* Now do the same, but with the alias sets reversed. */
335 ase = get_alias_set_entry (set2);
336 if (ase != 0
337 && (ase->has_zero_child
338 || splay_tree_lookup (ase->children,
339 (splay_tree_key) set1)))
340 return 1;
342 /* The two alias sets are distinct and neither one is the
343 child of the other. Therefore, they cannot conflict. */
344 return 0;
347 /* Return 1 if the two specified alias sets will always conflict. */
350 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
352 if (set1 == 0 || set2 == 0 || set1 == set2)
353 return 1;
355 return 0;
358 /* Return 1 if any MEM object of type T1 will always conflict (using the
359 dependency routines in this file) with any MEM object of type T2.
360 This is used when allocating temporary storage. If T1 and/or T2 are
361 NULL_TREE, it means we know nothing about the storage. */
364 objects_must_conflict_p (tree t1, tree t2)
366 alias_set_type set1, set2;
368 /* If neither has a type specified, we don't know if they'll conflict
369 because we may be using them to store objects of various types, for
370 example the argument and local variables areas of inlined functions. */
371 if (t1 == 0 && t2 == 0)
372 return 0;
374 /* If they are the same type, they must conflict. */
375 if (t1 == t2
376 /* Likewise if both are volatile. */
377 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
378 return 1;
380 set1 = t1 ? get_alias_set (t1) : 0;
381 set2 = t2 ? get_alias_set (t2) : 0;
383 /* We can't use alias_sets_conflict_p because we must make sure
384 that every subtype of t1 will conflict with every subtype of
385 t2 for which a pair of subobjects of these respective subtypes
386 overlaps on the stack. */
387 return alias_sets_must_conflict_p (set1, set2);
390 /* T is an expression with pointer type. Find the DECL on which this
391 expression is based. (For example, in `a[i]' this would be `a'.)
392 If there is no such DECL, or a unique decl cannot be determined,
393 NULL_TREE is returned. */
395 static tree
396 find_base_decl (tree t)
398 tree d0, d1;
400 if (t == 0 || t == error_mark_node || ! POINTER_TYPE_P (TREE_TYPE (t)))
401 return 0;
403 /* If this is a declaration, return it. If T is based on a restrict
404 qualified decl, return that decl. */
405 if (DECL_P (t))
407 if (TREE_CODE (t) == VAR_DECL && DECL_BASED_ON_RESTRICT_P (t))
408 t = DECL_GET_RESTRICT_BASE (t);
409 return t;
412 /* Handle general expressions. It would be nice to deal with
413 COMPONENT_REFs here. If we could tell that `a' and `b' were the
414 same, then `a->f' and `b->f' are also the same. */
415 switch (TREE_CODE_CLASS (TREE_CODE (t)))
417 case tcc_unary:
418 return find_base_decl (TREE_OPERAND (t, 0));
420 case tcc_binary:
421 /* Return 0 if found in neither or both are the same. */
422 d0 = find_base_decl (TREE_OPERAND (t, 0));
423 d1 = find_base_decl (TREE_OPERAND (t, 1));
424 if (d0 == d1)
425 return d0;
426 else if (d0 == 0)
427 return d1;
428 else if (d1 == 0)
429 return d0;
430 else
431 return 0;
433 default:
434 return 0;
438 /* Return true if all nested component references handled by
439 get_inner_reference in T are such that we should use the alias set
440 provided by the object at the heart of T.
442 This is true for non-addressable components (which don't have their
443 own alias set), as well as components of objects in alias set zero.
444 This later point is a special case wherein we wish to override the
445 alias set used by the component, but we don't have per-FIELD_DECL
446 assignable alias sets. */
448 bool
449 component_uses_parent_alias_set (const_tree t)
451 while (1)
453 /* If we're at the end, it vacuously uses its own alias set. */
454 if (!handled_component_p (t))
455 return false;
457 switch (TREE_CODE (t))
459 case COMPONENT_REF:
460 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
461 return true;
462 break;
464 case ARRAY_REF:
465 case ARRAY_RANGE_REF:
466 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
467 return true;
468 break;
470 case REALPART_EXPR:
471 case IMAGPART_EXPR:
472 break;
474 default:
475 /* Bitfields and casts are never addressable. */
476 return true;
479 t = TREE_OPERAND (t, 0);
480 if (get_alias_set (TREE_TYPE (t)) == 0)
481 return true;
485 /* Return the alias set for T, which may be either a type or an
486 expression. Call language-specific routine for help, if needed. */
488 alias_set_type
489 get_alias_set (tree t)
491 alias_set_type set;
493 /* If we're not doing any alias analysis, just assume everything
494 aliases everything else. Also return 0 if this or its type is
495 an error. */
496 if (! flag_strict_aliasing || t == error_mark_node
497 || (! TYPE_P (t)
498 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
499 return 0;
501 /* We can be passed either an expression or a type. This and the
502 language-specific routine may make mutually-recursive calls to each other
503 to figure out what to do. At each juncture, we see if this is a tree
504 that the language may need to handle specially. First handle things that
505 aren't types. */
506 if (! TYPE_P (t))
508 tree inner = t;
510 /* Remove any nops, then give the language a chance to do
511 something with this tree before we look at it. */
512 STRIP_NOPS (t);
513 set = lang_hooks.get_alias_set (t);
514 if (set != -1)
515 return set;
517 /* First see if the actual object referenced is an INDIRECT_REF from a
518 restrict-qualified pointer or a "void *". */
519 while (handled_component_p (inner))
521 inner = TREE_OPERAND (inner, 0);
522 STRIP_NOPS (inner);
525 /* Check for accesses through restrict-qualified pointers. */
526 if (INDIRECT_REF_P (inner))
528 tree decl;
530 if (TREE_CODE (TREE_OPERAND (inner, 0)) == SSA_NAME)
531 decl = SSA_NAME_VAR (TREE_OPERAND (inner, 0));
532 else
533 decl = find_base_decl (TREE_OPERAND (inner, 0));
535 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
537 /* If we haven't computed the actual alias set, do it now. */
538 if (DECL_POINTER_ALIAS_SET (decl) == -2)
540 tree pointed_to_type = TREE_TYPE (TREE_TYPE (decl));
542 /* No two restricted pointers can point at the same thing.
543 However, a restricted pointer can point at the same thing
544 as an unrestricted pointer, if that unrestricted pointer
545 is based on the restricted pointer. So, we make the
546 alias set for the restricted pointer a subset of the
547 alias set for the type pointed to by the type of the
548 decl. */
549 alias_set_type pointed_to_alias_set
550 = get_alias_set (pointed_to_type);
552 if (pointed_to_alias_set == 0)
553 /* It's not legal to make a subset of alias set zero. */
554 DECL_POINTER_ALIAS_SET (decl) = 0;
555 else if (AGGREGATE_TYPE_P (pointed_to_type))
556 /* For an aggregate, we must treat the restricted
557 pointer the same as an ordinary pointer. If we
558 were to make the type pointed to by the
559 restricted pointer a subset of the pointed-to
560 type, then we would believe that other subsets
561 of the pointed-to type (such as fields of that
562 type) do not conflict with the type pointed to
563 by the restricted pointer. */
564 DECL_POINTER_ALIAS_SET (decl)
565 = pointed_to_alias_set;
566 else
568 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
569 record_alias_subset (pointed_to_alias_set,
570 DECL_POINTER_ALIAS_SET (decl));
574 /* We use the alias set indicated in the declaration. */
575 return DECL_POINTER_ALIAS_SET (decl);
578 /* If we have an INDIRECT_REF via a void pointer, we don't
579 know anything about what that might alias. Likewise if the
580 pointer is marked that way. */
581 else if (TREE_CODE (TREE_TYPE (inner)) == VOID_TYPE
582 || (TYPE_REF_CAN_ALIAS_ALL
583 (TREE_TYPE (TREE_OPERAND (inner, 0)))))
584 return 0;
587 /* Otherwise, pick up the outermost object that we could have a pointer
588 to, processing conversions as above. */
589 while (component_uses_parent_alias_set (t))
591 t = TREE_OPERAND (t, 0);
592 STRIP_NOPS (t);
595 /* If we've already determined the alias set for a decl, just return
596 it. This is necessary for C++ anonymous unions, whose component
597 variables don't look like union members (boo!). */
598 if (TREE_CODE (t) == VAR_DECL
599 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
600 return MEM_ALIAS_SET (DECL_RTL (t));
602 /* Now all we care about is the type. */
603 t = TREE_TYPE (t);
606 /* Variant qualifiers don't affect the alias set, so get the main
607 variant. If this is a type with a known alias set, return it. */
608 t = TYPE_MAIN_VARIANT (t);
609 if (TYPE_ALIAS_SET_KNOWN_P (t))
610 return TYPE_ALIAS_SET (t);
612 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
613 if (!COMPLETE_TYPE_P (t))
615 /* For arrays with unknown size the conservative answer is the
616 alias set of the element type. */
617 if (TREE_CODE (t) == ARRAY_TYPE)
618 return get_alias_set (TREE_TYPE (t));
620 /* But return zero as a conservative answer for incomplete types. */
621 return 0;
624 /* See if the language has special handling for this type. */
625 set = lang_hooks.get_alias_set (t);
626 if (set != -1)
627 return set;
629 /* There are no objects of FUNCTION_TYPE, so there's no point in
630 using up an alias set for them. (There are, of course, pointers
631 and references to functions, but that's different.) */
632 else if (TREE_CODE (t) == FUNCTION_TYPE
633 || TREE_CODE (t) == METHOD_TYPE)
634 set = 0;
636 /* Unless the language specifies otherwise, let vector types alias
637 their components. This avoids some nasty type punning issues in
638 normal usage. And indeed lets vectors be treated more like an
639 array slice. */
640 else if (TREE_CODE (t) == VECTOR_TYPE)
641 set = get_alias_set (TREE_TYPE (t));
643 /* Unless the language specifies otherwise, treat array types the
644 same as their components. This avoids the asymmetry we get
645 through recording the components. Consider accessing a
646 character(kind=1) through a reference to a character(kind=1)[1:1].
647 Or consider if we want to assign integer(kind=4)[0:D.1387] and
648 integer(kind=4)[4] the same alias set or not.
649 Just be pragmatic here and make sure the array and its element
650 type get the same alias set assigned. */
651 else if (TREE_CODE (t) == ARRAY_TYPE
652 && !TYPE_NONALIASED_COMPONENT (t))
653 set = get_alias_set (TREE_TYPE (t));
655 else
656 /* Otherwise make a new alias set for this type. */
657 set = new_alias_set ();
659 TYPE_ALIAS_SET (t) = set;
661 /* If this is an aggregate type, we must record any component aliasing
662 information. */
663 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
664 record_component_aliases (t);
666 return set;
669 /* Return a brand-new alias set. */
671 alias_set_type
672 new_alias_set (void)
674 if (flag_strict_aliasing)
676 if (alias_sets == 0)
677 VEC_safe_push (alias_set_entry, gc, alias_sets, 0);
678 VEC_safe_push (alias_set_entry, gc, alias_sets, 0);
679 return VEC_length (alias_set_entry, alias_sets) - 1;
681 else
682 return 0;
685 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
686 not everything that aliases SUPERSET also aliases SUBSET. For example,
687 in C, a store to an `int' can alias a load of a structure containing an
688 `int', and vice versa. But it can't alias a load of a 'double' member
689 of the same structure. Here, the structure would be the SUPERSET and
690 `int' the SUBSET. This relationship is also described in the comment at
691 the beginning of this file.
693 This function should be called only once per SUPERSET/SUBSET pair.
695 It is illegal for SUPERSET to be zero; everything is implicitly a
696 subset of alias set zero. */
698 static void
699 record_alias_subset (alias_set_type superset, alias_set_type subset)
701 alias_set_entry superset_entry;
702 alias_set_entry subset_entry;
704 /* It is possible in complex type situations for both sets to be the same,
705 in which case we can ignore this operation. */
706 if (superset == subset)
707 return;
709 gcc_assert (superset);
711 superset_entry = get_alias_set_entry (superset);
712 if (superset_entry == 0)
714 /* Create an entry for the SUPERSET, so that we have a place to
715 attach the SUBSET. */
716 superset_entry = GGC_NEW (struct alias_set_entry);
717 superset_entry->alias_set = superset;
718 superset_entry->children
719 = splay_tree_new_ggc (splay_tree_compare_ints);
720 superset_entry->has_zero_child = 0;
721 VEC_replace (alias_set_entry, alias_sets, superset, superset_entry);
724 if (subset == 0)
725 superset_entry->has_zero_child = 1;
726 else
728 subset_entry = get_alias_set_entry (subset);
729 /* If there is an entry for the subset, enter all of its children
730 (if they are not already present) as children of the SUPERSET. */
731 if (subset_entry)
733 if (subset_entry->has_zero_child)
734 superset_entry->has_zero_child = 1;
736 splay_tree_foreach (subset_entry->children, insert_subset_children,
737 superset_entry->children);
740 /* Enter the SUBSET itself as a child of the SUPERSET. */
741 splay_tree_insert (superset_entry->children,
742 (splay_tree_key) subset, 0);
746 /* Record that component types of TYPE, if any, are part of that type for
747 aliasing purposes. For record types, we only record component types
748 for fields that are not marked non-addressable. For array types, we
749 only record the component type if it is not marked non-aliased. */
751 void
752 record_component_aliases (tree type)
754 alias_set_type superset = get_alias_set (type);
755 tree field;
757 if (superset == 0)
758 return;
760 switch (TREE_CODE (type))
762 case RECORD_TYPE:
763 case UNION_TYPE:
764 case QUAL_UNION_TYPE:
765 /* Recursively record aliases for the base classes, if there are any. */
766 if (TYPE_BINFO (type))
768 int i;
769 tree binfo, base_binfo;
771 for (binfo = TYPE_BINFO (type), i = 0;
772 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
773 record_alias_subset (superset,
774 get_alias_set (BINFO_TYPE (base_binfo)));
776 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
777 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
778 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
779 break;
781 case COMPLEX_TYPE:
782 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
783 break;
785 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
786 element type. */
788 default:
789 break;
793 /* Allocate an alias set for use in storing and reading from the varargs
794 spill area. */
796 static GTY(()) alias_set_type varargs_set = -1;
798 alias_set_type
799 get_varargs_alias_set (void)
801 #if 1
802 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
803 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
804 consistently use the varargs alias set for loads from the varargs
805 area. So don't use it anywhere. */
806 return 0;
807 #else
808 if (varargs_set == -1)
809 varargs_set = new_alias_set ();
811 return varargs_set;
812 #endif
815 /* Likewise, but used for the fixed portions of the frame, e.g., register
816 save areas. */
818 static GTY(()) alias_set_type frame_set = -1;
820 alias_set_type
821 get_frame_alias_set (void)
823 if (frame_set == -1)
824 frame_set = new_alias_set ();
826 return frame_set;
829 /* Inside SRC, the source of a SET, find a base address. */
831 static rtx
832 find_base_value (rtx src)
834 unsigned int regno;
836 switch (GET_CODE (src))
838 case SYMBOL_REF:
839 case LABEL_REF:
840 return src;
842 case REG:
843 regno = REGNO (src);
844 /* At the start of a function, argument registers have known base
845 values which may be lost later. Returning an ADDRESS
846 expression here allows optimization based on argument values
847 even when the argument registers are used for other purposes. */
848 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
849 return new_reg_base_value[regno];
851 /* If a pseudo has a known base value, return it. Do not do this
852 for non-fixed hard regs since it can result in a circular
853 dependency chain for registers which have values at function entry.
855 The test above is not sufficient because the scheduler may move
856 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
857 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
858 && regno < VEC_length (rtx, reg_base_value))
860 /* If we're inside init_alias_analysis, use new_reg_base_value
861 to reduce the number of relaxation iterations. */
862 if (new_reg_base_value && new_reg_base_value[regno]
863 && DF_REG_DEF_COUNT (regno) == 1)
864 return new_reg_base_value[regno];
866 if (VEC_index (rtx, reg_base_value, regno))
867 return VEC_index (rtx, reg_base_value, regno);
870 return 0;
872 case MEM:
873 /* Check for an argument passed in memory. Only record in the
874 copying-arguments block; it is too hard to track changes
875 otherwise. */
876 if (copying_arguments
877 && (XEXP (src, 0) == arg_pointer_rtx
878 || (GET_CODE (XEXP (src, 0)) == PLUS
879 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
880 return gen_rtx_ADDRESS (VOIDmode, src);
881 return 0;
883 case CONST:
884 src = XEXP (src, 0);
885 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
886 break;
888 /* ... fall through ... */
890 case PLUS:
891 case MINUS:
893 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
895 /* If either operand is a REG that is a known pointer, then it
896 is the base. */
897 if (REG_P (src_0) && REG_POINTER (src_0))
898 return find_base_value (src_0);
899 if (REG_P (src_1) && REG_POINTER (src_1))
900 return find_base_value (src_1);
902 /* If either operand is a REG, then see if we already have
903 a known value for it. */
904 if (REG_P (src_0))
906 temp = find_base_value (src_0);
907 if (temp != 0)
908 src_0 = temp;
911 if (REG_P (src_1))
913 temp = find_base_value (src_1);
914 if (temp!= 0)
915 src_1 = temp;
918 /* If either base is named object or a special address
919 (like an argument or stack reference), then use it for the
920 base term. */
921 if (src_0 != 0
922 && (GET_CODE (src_0) == SYMBOL_REF
923 || GET_CODE (src_0) == LABEL_REF
924 || (GET_CODE (src_0) == ADDRESS
925 && GET_MODE (src_0) != VOIDmode)))
926 return src_0;
928 if (src_1 != 0
929 && (GET_CODE (src_1) == SYMBOL_REF
930 || GET_CODE (src_1) == LABEL_REF
931 || (GET_CODE (src_1) == ADDRESS
932 && GET_MODE (src_1) != VOIDmode)))
933 return src_1;
935 /* Guess which operand is the base address:
936 If either operand is a symbol, then it is the base. If
937 either operand is a CONST_INT, then the other is the base. */
938 if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
939 return find_base_value (src_0);
940 else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
941 return find_base_value (src_1);
943 return 0;
946 case LO_SUM:
947 /* The standard form is (lo_sum reg sym) so look only at the
948 second operand. */
949 return find_base_value (XEXP (src, 1));
951 case AND:
952 /* If the second operand is constant set the base
953 address to the first operand. */
954 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
955 return find_base_value (XEXP (src, 0));
956 return 0;
958 case TRUNCATE:
959 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
960 break;
961 /* Fall through. */
962 case HIGH:
963 case PRE_INC:
964 case PRE_DEC:
965 case POST_INC:
966 case POST_DEC:
967 case PRE_MODIFY:
968 case POST_MODIFY:
969 return find_base_value (XEXP (src, 0));
971 case ZERO_EXTEND:
972 case SIGN_EXTEND: /* used for NT/Alpha pointers */
974 rtx temp = find_base_value (XEXP (src, 0));
976 if (temp != 0 && CONSTANT_P (temp))
977 temp = convert_memory_address (Pmode, temp);
979 return temp;
982 default:
983 break;
986 return 0;
989 /* Called from init_alias_analysis indirectly through note_stores. */
991 /* While scanning insns to find base values, reg_seen[N] is nonzero if
992 register N has been set in this function. */
993 static char *reg_seen;
995 /* Addresses which are known not to alias anything else are identified
996 by a unique integer. */
997 static int unique_id;
999 static void
1000 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1002 unsigned regno;
1003 rtx src;
1004 int n;
1006 if (!REG_P (dest))
1007 return;
1009 regno = REGNO (dest);
1011 gcc_assert (regno < VEC_length (rtx, reg_base_value));
1013 /* If this spans multiple hard registers, then we must indicate that every
1014 register has an unusable value. */
1015 if (regno < FIRST_PSEUDO_REGISTER)
1016 n = hard_regno_nregs[regno][GET_MODE (dest)];
1017 else
1018 n = 1;
1019 if (n != 1)
1021 while (--n >= 0)
1023 reg_seen[regno + n] = 1;
1024 new_reg_base_value[regno + n] = 0;
1026 return;
1029 if (set)
1031 /* A CLOBBER wipes out any old value but does not prevent a previously
1032 unset register from acquiring a base address (i.e. reg_seen is not
1033 set). */
1034 if (GET_CODE (set) == CLOBBER)
1036 new_reg_base_value[regno] = 0;
1037 return;
1039 src = SET_SRC (set);
1041 else
1043 if (reg_seen[regno])
1045 new_reg_base_value[regno] = 0;
1046 return;
1048 reg_seen[regno] = 1;
1049 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
1050 GEN_INT (unique_id++));
1051 return;
1054 /* If this is not the first set of REGNO, see whether the new value
1055 is related to the old one. There are two cases of interest:
1057 (1) The register might be assigned an entirely new value
1058 that has the same base term as the original set.
1060 (2) The set might be a simple self-modification that
1061 cannot change REGNO's base value.
1063 If neither case holds, reject the original base value as invalid.
1064 Note that the following situation is not detected:
1066 extern int x, y; int *p = &x; p += (&y-&x);
1068 ANSI C does not allow computing the difference of addresses
1069 of distinct top level objects. */
1070 if (new_reg_base_value[regno] != 0
1071 && find_base_value (src) != new_reg_base_value[regno])
1072 switch (GET_CODE (src))
1074 case LO_SUM:
1075 case MINUS:
1076 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1077 new_reg_base_value[regno] = 0;
1078 break;
1079 case PLUS:
1080 /* If the value we add in the PLUS is also a valid base value,
1081 this might be the actual base value, and the original value
1082 an index. */
1084 rtx other = NULL_RTX;
1086 if (XEXP (src, 0) == dest)
1087 other = XEXP (src, 1);
1088 else if (XEXP (src, 1) == dest)
1089 other = XEXP (src, 0);
1091 if (! other || find_base_value (other))
1092 new_reg_base_value[regno] = 0;
1093 break;
1095 case AND:
1096 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
1097 new_reg_base_value[regno] = 0;
1098 break;
1099 default:
1100 new_reg_base_value[regno] = 0;
1101 break;
1103 /* If this is the first set of a register, record the value. */
1104 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1105 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
1106 new_reg_base_value[regno] = find_base_value (src);
1108 reg_seen[regno] = 1;
1111 /* If a value is known for REGNO, return it. */
1114 get_reg_known_value (unsigned int regno)
1116 if (regno >= FIRST_PSEUDO_REGISTER)
1118 regno -= FIRST_PSEUDO_REGISTER;
1119 if (regno < reg_known_value_size)
1120 return reg_known_value[regno];
1122 return NULL;
1125 /* Set it. */
1127 static void
1128 set_reg_known_value (unsigned int regno, rtx val)
1130 if (regno >= FIRST_PSEUDO_REGISTER)
1132 regno -= FIRST_PSEUDO_REGISTER;
1133 if (regno < reg_known_value_size)
1134 reg_known_value[regno] = val;
1138 /* Similarly for reg_known_equiv_p. */
1140 bool
1141 get_reg_known_equiv_p (unsigned int regno)
1143 if (regno >= FIRST_PSEUDO_REGISTER)
1145 regno -= FIRST_PSEUDO_REGISTER;
1146 if (regno < reg_known_value_size)
1147 return reg_known_equiv_p[regno];
1149 return false;
1152 static void
1153 set_reg_known_equiv_p (unsigned int regno, bool val)
1155 if (regno >= FIRST_PSEUDO_REGISTER)
1157 regno -= FIRST_PSEUDO_REGISTER;
1158 if (regno < reg_known_value_size)
1159 reg_known_equiv_p[regno] = val;
1164 /* Returns a canonical version of X, from the point of view alias
1165 analysis. (For example, if X is a MEM whose address is a register,
1166 and the register has a known value (say a SYMBOL_REF), then a MEM
1167 whose address is the SYMBOL_REF is returned.) */
1170 canon_rtx (rtx x)
1172 /* Recursively look for equivalences. */
1173 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1175 rtx t = get_reg_known_value (REGNO (x));
1176 if (t == x)
1177 return x;
1178 if (t)
1179 return canon_rtx (t);
1182 if (GET_CODE (x) == PLUS)
1184 rtx x0 = canon_rtx (XEXP (x, 0));
1185 rtx x1 = canon_rtx (XEXP (x, 1));
1187 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1189 if (GET_CODE (x0) == CONST_INT)
1190 return plus_constant (x1, INTVAL (x0));
1191 else if (GET_CODE (x1) == CONST_INT)
1192 return plus_constant (x0, INTVAL (x1));
1193 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1197 /* This gives us much better alias analysis when called from
1198 the loop optimizer. Note we want to leave the original
1199 MEM alone, but need to return the canonicalized MEM with
1200 all the flags with their original values. */
1201 else if (MEM_P (x))
1202 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1204 return x;
1207 /* Return 1 if X and Y are identical-looking rtx's.
1208 Expect that X and Y has been already canonicalized.
1210 We use the data in reg_known_value above to see if two registers with
1211 different numbers are, in fact, equivalent. */
1213 static int
1214 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1216 int i;
1217 int j;
1218 enum rtx_code code;
1219 const char *fmt;
1221 if (x == 0 && y == 0)
1222 return 1;
1223 if (x == 0 || y == 0)
1224 return 0;
1226 if (x == y)
1227 return 1;
1229 code = GET_CODE (x);
1230 /* Rtx's of different codes cannot be equal. */
1231 if (code != GET_CODE (y))
1232 return 0;
1234 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1235 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1237 if (GET_MODE (x) != GET_MODE (y))
1238 return 0;
1240 /* Some RTL can be compared without a recursive examination. */
1241 switch (code)
1243 case REG:
1244 return REGNO (x) == REGNO (y);
1246 case LABEL_REF:
1247 return XEXP (x, 0) == XEXP (y, 0);
1249 case SYMBOL_REF:
1250 return XSTR (x, 0) == XSTR (y, 0);
1252 case VALUE:
1253 case CONST_INT:
1254 case CONST_DOUBLE:
1255 case CONST_FIXED:
1256 /* There's no need to compare the contents of CONST_DOUBLEs or
1257 CONST_INTs because pointer equality is a good enough
1258 comparison for these nodes. */
1259 return 0;
1261 default:
1262 break;
1265 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1266 if (code == PLUS)
1267 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1268 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1269 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1270 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1271 /* For commutative operations, the RTX match if the operand match in any
1272 order. Also handle the simple binary and unary cases without a loop. */
1273 if (COMMUTATIVE_P (x))
1275 rtx xop0 = canon_rtx (XEXP (x, 0));
1276 rtx yop0 = canon_rtx (XEXP (y, 0));
1277 rtx yop1 = canon_rtx (XEXP (y, 1));
1279 return ((rtx_equal_for_memref_p (xop0, yop0)
1280 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1281 || (rtx_equal_for_memref_p (xop0, yop1)
1282 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1284 else if (NON_COMMUTATIVE_P (x))
1286 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1287 canon_rtx (XEXP (y, 0)))
1288 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1289 canon_rtx (XEXP (y, 1))));
1291 else if (UNARY_P (x))
1292 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1293 canon_rtx (XEXP (y, 0)));
1295 /* Compare the elements. If any pair of corresponding elements
1296 fail to match, return 0 for the whole things.
1298 Limit cases to types which actually appear in addresses. */
1300 fmt = GET_RTX_FORMAT (code);
1301 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1303 switch (fmt[i])
1305 case 'i':
1306 if (XINT (x, i) != XINT (y, i))
1307 return 0;
1308 break;
1310 case 'E':
1311 /* Two vectors must have the same length. */
1312 if (XVECLEN (x, i) != XVECLEN (y, i))
1313 return 0;
1315 /* And the corresponding elements must match. */
1316 for (j = 0; j < XVECLEN (x, i); j++)
1317 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1318 canon_rtx (XVECEXP (y, i, j))) == 0)
1319 return 0;
1320 break;
1322 case 'e':
1323 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1324 canon_rtx (XEXP (y, i))) == 0)
1325 return 0;
1326 break;
1328 /* This can happen for asm operands. */
1329 case 's':
1330 if (strcmp (XSTR (x, i), XSTR (y, i)))
1331 return 0;
1332 break;
1334 /* This can happen for an asm which clobbers memory. */
1335 case '0':
1336 break;
1338 /* It is believed that rtx's at this level will never
1339 contain anything but integers and other rtx's,
1340 except for within LABEL_REFs and SYMBOL_REFs. */
1341 default:
1342 gcc_unreachable ();
1345 return 1;
1349 find_base_term (rtx x)
1351 cselib_val *val;
1352 struct elt_loc_list *l;
1354 #if defined (FIND_BASE_TERM)
1355 /* Try machine-dependent ways to find the base term. */
1356 x = FIND_BASE_TERM (x);
1357 #endif
1359 switch (GET_CODE (x))
1361 case REG:
1362 return REG_BASE_VALUE (x);
1364 case TRUNCATE:
1365 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1366 return 0;
1367 /* Fall through. */
1368 case HIGH:
1369 case PRE_INC:
1370 case PRE_DEC:
1371 case POST_INC:
1372 case POST_DEC:
1373 case PRE_MODIFY:
1374 case POST_MODIFY:
1375 return find_base_term (XEXP (x, 0));
1377 case ZERO_EXTEND:
1378 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1380 rtx temp = find_base_term (XEXP (x, 0));
1382 if (temp != 0 && CONSTANT_P (temp))
1383 temp = convert_memory_address (Pmode, temp);
1385 return temp;
1388 case VALUE:
1389 val = CSELIB_VAL_PTR (x);
1390 if (!val)
1391 return 0;
1392 for (l = val->locs; l; l = l->next)
1393 if ((x = find_base_term (l->loc)) != 0)
1394 return x;
1395 return 0;
1397 case CONST:
1398 x = XEXP (x, 0);
1399 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1400 return 0;
1401 /* Fall through. */
1402 case LO_SUM:
1403 case PLUS:
1404 case MINUS:
1406 rtx tmp1 = XEXP (x, 0);
1407 rtx tmp2 = XEXP (x, 1);
1409 /* This is a little bit tricky since we have to determine which of
1410 the two operands represents the real base address. Otherwise this
1411 routine may return the index register instead of the base register.
1413 That may cause us to believe no aliasing was possible, when in
1414 fact aliasing is possible.
1416 We use a few simple tests to guess the base register. Additional
1417 tests can certainly be added. For example, if one of the operands
1418 is a shift or multiply, then it must be the index register and the
1419 other operand is the base register. */
1421 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1422 return find_base_term (tmp2);
1424 /* If either operand is known to be a pointer, then use it
1425 to determine the base term. */
1426 if (REG_P (tmp1) && REG_POINTER (tmp1))
1427 return find_base_term (tmp1);
1429 if (REG_P (tmp2) && REG_POINTER (tmp2))
1430 return find_base_term (tmp2);
1432 /* Neither operand was known to be a pointer. Go ahead and find the
1433 base term for both operands. */
1434 tmp1 = find_base_term (tmp1);
1435 tmp2 = find_base_term (tmp2);
1437 /* If either base term is named object or a special address
1438 (like an argument or stack reference), then use it for the
1439 base term. */
1440 if (tmp1 != 0
1441 && (GET_CODE (tmp1) == SYMBOL_REF
1442 || GET_CODE (tmp1) == LABEL_REF
1443 || (GET_CODE (tmp1) == ADDRESS
1444 && GET_MODE (tmp1) != VOIDmode)))
1445 return tmp1;
1447 if (tmp2 != 0
1448 && (GET_CODE (tmp2) == SYMBOL_REF
1449 || GET_CODE (tmp2) == LABEL_REF
1450 || (GET_CODE (tmp2) == ADDRESS
1451 && GET_MODE (tmp2) != VOIDmode)))
1452 return tmp2;
1454 /* We could not determine which of the two operands was the
1455 base register and which was the index. So we can determine
1456 nothing from the base alias check. */
1457 return 0;
1460 case AND:
1461 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) != 0)
1462 return find_base_term (XEXP (x, 0));
1463 return 0;
1465 case SYMBOL_REF:
1466 case LABEL_REF:
1467 return x;
1469 default:
1470 return 0;
1474 /* Return 0 if the addresses X and Y are known to point to different
1475 objects, 1 if they might be pointers to the same object. */
1477 static int
1478 base_alias_check (rtx x, rtx y, enum machine_mode x_mode,
1479 enum machine_mode y_mode)
1481 rtx x_base = find_base_term (x);
1482 rtx y_base = find_base_term (y);
1484 /* If the address itself has no known base see if a known equivalent
1485 value has one. If either address still has no known base, nothing
1486 is known about aliasing. */
1487 if (x_base == 0)
1489 rtx x_c;
1491 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1492 return 1;
1494 x_base = find_base_term (x_c);
1495 if (x_base == 0)
1496 return 1;
1499 if (y_base == 0)
1501 rtx y_c;
1502 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1503 return 1;
1505 y_base = find_base_term (y_c);
1506 if (y_base == 0)
1507 return 1;
1510 /* If the base addresses are equal nothing is known about aliasing. */
1511 if (rtx_equal_p (x_base, y_base))
1512 return 1;
1514 /* The base addresses of the read and write are different expressions.
1515 If they are both symbols and they are not accessed via AND, there is
1516 no conflict. We can bring knowledge of object alignment into play
1517 here. For example, on alpha, "char a, b;" can alias one another,
1518 though "char a; long b;" cannot. */
1519 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1521 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1522 return 1;
1523 if (GET_CODE (x) == AND
1524 && (GET_CODE (XEXP (x, 1)) != CONST_INT
1525 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1526 return 1;
1527 if (GET_CODE (y) == AND
1528 && (GET_CODE (XEXP (y, 1)) != CONST_INT
1529 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1530 return 1;
1531 /* Differing symbols never alias. */
1532 return 0;
1535 /* If one address is a stack reference there can be no alias:
1536 stack references using different base registers do not alias,
1537 a stack reference can not alias a parameter, and a stack reference
1538 can not alias a global. */
1539 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1540 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1541 return 0;
1543 if (! flag_argument_noalias)
1544 return 1;
1546 if (flag_argument_noalias > 1)
1547 return 0;
1549 /* Weak noalias assertion (arguments are distinct, but may match globals). */
1550 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1553 /* Convert the address X into something we can use. This is done by returning
1554 it unchanged unless it is a value; in the latter case we call cselib to get
1555 a more useful rtx. */
1558 get_addr (rtx x)
1560 cselib_val *v;
1561 struct elt_loc_list *l;
1563 if (GET_CODE (x) != VALUE)
1564 return x;
1565 v = CSELIB_VAL_PTR (x);
1566 if (v)
1568 for (l = v->locs; l; l = l->next)
1569 if (CONSTANT_P (l->loc))
1570 return l->loc;
1571 for (l = v->locs; l; l = l->next)
1572 if (!REG_P (l->loc) && !MEM_P (l->loc))
1573 return l->loc;
1574 if (v->locs)
1575 return v->locs->loc;
1577 return x;
1580 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1581 where SIZE is the size in bytes of the memory reference. If ADDR
1582 is not modified by the memory reference then ADDR is returned. */
1584 static rtx
1585 addr_side_effect_eval (rtx addr, int size, int n_refs)
1587 int offset = 0;
1589 switch (GET_CODE (addr))
1591 case PRE_INC:
1592 offset = (n_refs + 1) * size;
1593 break;
1594 case PRE_DEC:
1595 offset = -(n_refs + 1) * size;
1596 break;
1597 case POST_INC:
1598 offset = n_refs * size;
1599 break;
1600 case POST_DEC:
1601 offset = -n_refs * size;
1602 break;
1604 default:
1605 return addr;
1608 if (offset)
1609 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1610 GEN_INT (offset));
1611 else
1612 addr = XEXP (addr, 0);
1613 addr = canon_rtx (addr);
1615 return addr;
1618 /* Return nonzero if X and Y (memory addresses) could reference the
1619 same location in memory. C is an offset accumulator. When
1620 C is nonzero, we are testing aliases between X and Y + C.
1621 XSIZE is the size in bytes of the X reference,
1622 similarly YSIZE is the size in bytes for Y.
1623 Expect that canon_rtx has been already called for X and Y.
1625 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1626 referenced (the reference was BLKmode), so make the most pessimistic
1627 assumptions.
1629 If XSIZE or YSIZE is negative, we may access memory outside the object
1630 being referenced as a side effect. This can happen when using AND to
1631 align memory references, as is done on the Alpha.
1633 Nice to notice that varying addresses cannot conflict with fp if no
1634 local variables had their addresses taken, but that's too hard now. */
1636 static int
1637 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
1639 if (GET_CODE (x) == VALUE)
1640 x = get_addr (x);
1641 if (GET_CODE (y) == VALUE)
1642 y = get_addr (y);
1643 if (GET_CODE (x) == HIGH)
1644 x = XEXP (x, 0);
1645 else if (GET_CODE (x) == LO_SUM)
1646 x = XEXP (x, 1);
1647 else
1648 x = addr_side_effect_eval (x, xsize, 0);
1649 if (GET_CODE (y) == HIGH)
1650 y = XEXP (y, 0);
1651 else if (GET_CODE (y) == LO_SUM)
1652 y = XEXP (y, 1);
1653 else
1654 y = addr_side_effect_eval (y, ysize, 0);
1656 if (rtx_equal_for_memref_p (x, y))
1658 if (xsize <= 0 || ysize <= 0)
1659 return 1;
1660 if (c >= 0 && xsize > c)
1661 return 1;
1662 if (c < 0 && ysize+c > 0)
1663 return 1;
1664 return 0;
1667 /* This code used to check for conflicts involving stack references and
1668 globals but the base address alias code now handles these cases. */
1670 if (GET_CODE (x) == PLUS)
1672 /* The fact that X is canonicalized means that this
1673 PLUS rtx is canonicalized. */
1674 rtx x0 = XEXP (x, 0);
1675 rtx x1 = XEXP (x, 1);
1677 if (GET_CODE (y) == PLUS)
1679 /* The fact that Y is canonicalized means that this
1680 PLUS rtx is canonicalized. */
1681 rtx y0 = XEXP (y, 0);
1682 rtx y1 = XEXP (y, 1);
1684 if (rtx_equal_for_memref_p (x1, y1))
1685 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1686 if (rtx_equal_for_memref_p (x0, y0))
1687 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1688 if (GET_CODE (x1) == CONST_INT)
1690 if (GET_CODE (y1) == CONST_INT)
1691 return memrefs_conflict_p (xsize, x0, ysize, y0,
1692 c - INTVAL (x1) + INTVAL (y1));
1693 else
1694 return memrefs_conflict_p (xsize, x0, ysize, y,
1695 c - INTVAL (x1));
1697 else if (GET_CODE (y1) == CONST_INT)
1698 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1700 return 1;
1702 else if (GET_CODE (x1) == CONST_INT)
1703 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1705 else if (GET_CODE (y) == PLUS)
1707 /* The fact that Y is canonicalized means that this
1708 PLUS rtx is canonicalized. */
1709 rtx y0 = XEXP (y, 0);
1710 rtx y1 = XEXP (y, 1);
1712 if (GET_CODE (y1) == CONST_INT)
1713 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1714 else
1715 return 1;
1718 if (GET_CODE (x) == GET_CODE (y))
1719 switch (GET_CODE (x))
1721 case MULT:
1723 /* Handle cases where we expect the second operands to be the
1724 same, and check only whether the first operand would conflict
1725 or not. */
1726 rtx x0, y0;
1727 rtx x1 = canon_rtx (XEXP (x, 1));
1728 rtx y1 = canon_rtx (XEXP (y, 1));
1729 if (! rtx_equal_for_memref_p (x1, y1))
1730 return 1;
1731 x0 = canon_rtx (XEXP (x, 0));
1732 y0 = canon_rtx (XEXP (y, 0));
1733 if (rtx_equal_for_memref_p (x0, y0))
1734 return (xsize == 0 || ysize == 0
1735 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1737 /* Can't properly adjust our sizes. */
1738 if (GET_CODE (x1) != CONST_INT)
1739 return 1;
1740 xsize /= INTVAL (x1);
1741 ysize /= INTVAL (x1);
1742 c /= INTVAL (x1);
1743 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1746 default:
1747 break;
1750 /* Treat an access through an AND (e.g. a subword access on an Alpha)
1751 as an access with indeterminate size. Assume that references
1752 besides AND are aligned, so if the size of the other reference is
1753 at least as large as the alignment, assume no other overlap. */
1754 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1756 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1757 xsize = -1;
1758 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)), ysize, y, c);
1760 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1762 /* ??? If we are indexing far enough into the array/structure, we
1763 may yet be able to determine that we can not overlap. But we
1764 also need to that we are far enough from the end not to overlap
1765 a following reference, so we do nothing with that for now. */
1766 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1767 ysize = -1;
1768 return memrefs_conflict_p (xsize, x, ysize, canon_rtx (XEXP (y, 0)), c);
1771 if (CONSTANT_P (x))
1773 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1775 c += (INTVAL (y) - INTVAL (x));
1776 return (xsize <= 0 || ysize <= 0
1777 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1780 if (GET_CODE (x) == CONST)
1782 if (GET_CODE (y) == CONST)
1783 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1784 ysize, canon_rtx (XEXP (y, 0)), c);
1785 else
1786 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1787 ysize, y, c);
1789 if (GET_CODE (y) == CONST)
1790 return memrefs_conflict_p (xsize, x, ysize,
1791 canon_rtx (XEXP (y, 0)), c);
1793 if (CONSTANT_P (y))
1794 return (xsize <= 0 || ysize <= 0
1795 || (rtx_equal_for_memref_p (x, y)
1796 && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1798 return 1;
1800 return 1;
1803 /* Functions to compute memory dependencies.
1805 Since we process the insns in execution order, we can build tables
1806 to keep track of what registers are fixed (and not aliased), what registers
1807 are varying in known ways, and what registers are varying in unknown
1808 ways.
1810 If both memory references are volatile, then there must always be a
1811 dependence between the two references, since their order can not be
1812 changed. A volatile and non-volatile reference can be interchanged
1813 though.
1815 A MEM_IN_STRUCT reference at a non-AND varying address can never
1816 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
1817 also must allow AND addresses, because they may generate accesses
1818 outside the object being referenced. This is used to generate
1819 aligned addresses from unaligned addresses, for instance, the alpha
1820 storeqi_unaligned pattern. */
1822 /* Read dependence: X is read after read in MEM takes place. There can
1823 only be a dependence here if both reads are volatile. */
1826 read_dependence (const_rtx mem, const_rtx x)
1828 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1831 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1832 MEM2 is a reference to a structure at a varying address, or returns
1833 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1834 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1835 to decide whether or not an address may vary; it should return
1836 nonzero whenever variation is possible.
1837 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
1839 static const_rtx
1840 fixed_scalar_and_varying_struct_p (const_rtx mem1, const_rtx mem2, rtx mem1_addr,
1841 rtx mem2_addr,
1842 bool (*varies_p) (const_rtx, bool))
1844 if (! flag_strict_aliasing)
1845 return NULL_RTX;
1847 if (MEM_ALIAS_SET (mem2)
1848 && MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
1849 && !varies_p (mem1_addr, 1) && varies_p (mem2_addr, 1))
1850 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1851 varying address. */
1852 return mem1;
1854 if (MEM_ALIAS_SET (mem1)
1855 && MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
1856 && varies_p (mem1_addr, 1) && !varies_p (mem2_addr, 1))
1857 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1858 varying address. */
1859 return mem2;
1861 return NULL_RTX;
1864 /* Returns nonzero if something about the mode or address format MEM1
1865 indicates that it might well alias *anything*. */
1867 static int
1868 aliases_everything_p (const_rtx mem)
1870 if (GET_CODE (XEXP (mem, 0)) == AND)
1871 /* If the address is an AND, it's very hard to know at what it is
1872 actually pointing. */
1873 return 1;
1875 return 0;
1878 /* Return true if we can determine that the fields referenced cannot
1879 overlap for any pair of objects. */
1881 static bool
1882 nonoverlapping_component_refs_p (const_tree x, const_tree y)
1884 const_tree fieldx, fieldy, typex, typey, orig_y;
1888 /* The comparison has to be done at a common type, since we don't
1889 know how the inheritance hierarchy works. */
1890 orig_y = y;
1893 fieldx = TREE_OPERAND (x, 1);
1894 typex = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldx));
1896 y = orig_y;
1899 fieldy = TREE_OPERAND (y, 1);
1900 typey = TYPE_MAIN_VARIANT (DECL_FIELD_CONTEXT (fieldy));
1902 if (typex == typey)
1903 goto found;
1905 y = TREE_OPERAND (y, 0);
1907 while (y && TREE_CODE (y) == COMPONENT_REF);
1909 x = TREE_OPERAND (x, 0);
1911 while (x && TREE_CODE (x) == COMPONENT_REF);
1912 /* Never found a common type. */
1913 return false;
1915 found:
1916 /* If we're left with accessing different fields of a structure,
1917 then no overlap. */
1918 if (TREE_CODE (typex) == RECORD_TYPE
1919 && fieldx != fieldy)
1920 return true;
1922 /* The comparison on the current field failed. If we're accessing
1923 a very nested structure, look at the next outer level. */
1924 x = TREE_OPERAND (x, 0);
1925 y = TREE_OPERAND (y, 0);
1927 while (x && y
1928 && TREE_CODE (x) == COMPONENT_REF
1929 && TREE_CODE (y) == COMPONENT_REF);
1931 return false;
1934 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
1936 static tree
1937 decl_for_component_ref (tree x)
1941 x = TREE_OPERAND (x, 0);
1943 while (x && TREE_CODE (x) == COMPONENT_REF);
1945 return x && DECL_P (x) ? x : NULL_TREE;
1948 /* Walk up the COMPONENT_REF list and adjust OFFSET to compensate for the
1949 offset of the field reference. */
1951 static rtx
1952 adjust_offset_for_component_ref (tree x, rtx offset)
1954 HOST_WIDE_INT ioffset;
1956 if (! offset)
1957 return NULL_RTX;
1959 ioffset = INTVAL (offset);
1962 tree offset = component_ref_field_offset (x);
1963 tree field = TREE_OPERAND (x, 1);
1965 if (! host_integerp (offset, 1))
1966 return NULL_RTX;
1967 ioffset += (tree_low_cst (offset, 1)
1968 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1969 / BITS_PER_UNIT));
1971 x = TREE_OPERAND (x, 0);
1973 while (x && TREE_CODE (x) == COMPONENT_REF);
1975 return GEN_INT (ioffset);
1978 /* Return nonzero if we can determine the exprs corresponding to memrefs
1979 X and Y and they do not overlap. */
1982 nonoverlapping_memrefs_p (const_rtx x, const_rtx y)
1984 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
1985 rtx rtlx, rtly;
1986 rtx basex, basey;
1987 rtx moffsetx, moffsety;
1988 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
1990 /* Unless both have exprs, we can't tell anything. */
1991 if (exprx == 0 || expry == 0)
1992 return 0;
1994 /* If both are field references, we may be able to determine something. */
1995 if (TREE_CODE (exprx) == COMPONENT_REF
1996 && TREE_CODE (expry) == COMPONENT_REF
1997 && nonoverlapping_component_refs_p (exprx, expry))
1998 return 1;
2001 /* If the field reference test failed, look at the DECLs involved. */
2002 moffsetx = MEM_OFFSET (x);
2003 if (TREE_CODE (exprx) == COMPONENT_REF)
2005 if (TREE_CODE (expry) == VAR_DECL
2006 && POINTER_TYPE_P (TREE_TYPE (expry)))
2008 tree field = TREE_OPERAND (exprx, 1);
2009 tree fieldcontext = DECL_FIELD_CONTEXT (field);
2010 if (ipa_type_escape_field_does_not_clobber_p (fieldcontext,
2011 TREE_TYPE (field)))
2012 return 1;
2015 tree t = decl_for_component_ref (exprx);
2016 if (! t)
2017 return 0;
2018 moffsetx = adjust_offset_for_component_ref (exprx, moffsetx);
2019 exprx = t;
2022 else if (INDIRECT_REF_P (exprx))
2024 exprx = TREE_OPERAND (exprx, 0);
2025 if (flag_argument_noalias < 2
2026 || TREE_CODE (exprx) != PARM_DECL)
2027 return 0;
2030 moffsety = MEM_OFFSET (y);
2031 if (TREE_CODE (expry) == COMPONENT_REF)
2033 if (TREE_CODE (exprx) == VAR_DECL
2034 && POINTER_TYPE_P (TREE_TYPE (exprx)))
2036 tree field = TREE_OPERAND (expry, 1);
2037 tree fieldcontext = DECL_FIELD_CONTEXT (field);
2038 if (ipa_type_escape_field_does_not_clobber_p (fieldcontext,
2039 TREE_TYPE (field)))
2040 return 1;
2043 tree t = decl_for_component_ref (expry);
2044 if (! t)
2045 return 0;
2046 moffsety = adjust_offset_for_component_ref (expry, moffsety);
2047 expry = t;
2050 else if (INDIRECT_REF_P (expry))
2052 expry = TREE_OPERAND (expry, 0);
2053 if (flag_argument_noalias < 2
2054 || TREE_CODE (expry) != PARM_DECL)
2055 return 0;
2058 if (! DECL_P (exprx) || ! DECL_P (expry))
2059 return 0;
2061 rtlx = DECL_RTL (exprx);
2062 rtly = DECL_RTL (expry);
2064 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2065 can't overlap unless they are the same because we never reuse that part
2066 of the stack frame used for locals for spilled pseudos. */
2067 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2068 && ! rtx_equal_p (rtlx, rtly))
2069 return 1;
2071 /* Get the base and offsets of both decls. If either is a register, we
2072 know both are and are the same, so use that as the base. The only
2073 we can avoid overlap is if we can deduce that they are nonoverlapping
2074 pieces of that decl, which is very rare. */
2075 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2076 if (GET_CODE (basex) == PLUS && GET_CODE (XEXP (basex, 1)) == CONST_INT)
2077 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2079 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2080 if (GET_CODE (basey) == PLUS && GET_CODE (XEXP (basey, 1)) == CONST_INT)
2081 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2083 /* If the bases are different, we know they do not overlap if both
2084 are constants or if one is a constant and the other a pointer into the
2085 stack frame. Otherwise a different base means we can't tell if they
2086 overlap or not. */
2087 if (! rtx_equal_p (basex, basey))
2088 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2089 || (CONSTANT_P (basex) && REG_P (basey)
2090 && REGNO_PTR_FRAME_P (REGNO (basey)))
2091 || (CONSTANT_P (basey) && REG_P (basex)
2092 && REGNO_PTR_FRAME_P (REGNO (basex))));
2094 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2095 : MEM_SIZE (rtlx) ? INTVAL (MEM_SIZE (rtlx))
2096 : -1);
2097 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2098 : MEM_SIZE (rtly) ? INTVAL (MEM_SIZE (rtly)) :
2099 -1);
2101 /* If we have an offset for either memref, it can update the values computed
2102 above. */
2103 if (moffsetx)
2104 offsetx += INTVAL (moffsetx), sizex -= INTVAL (moffsetx);
2105 if (moffsety)
2106 offsety += INTVAL (moffsety), sizey -= INTVAL (moffsety);
2108 /* If a memref has both a size and an offset, we can use the smaller size.
2109 We can't do this if the offset isn't known because we must view this
2110 memref as being anywhere inside the DECL's MEM. */
2111 if (MEM_SIZE (x) && moffsetx)
2112 sizex = INTVAL (MEM_SIZE (x));
2113 if (MEM_SIZE (y) && moffsety)
2114 sizey = INTVAL (MEM_SIZE (y));
2116 /* Put the values of the memref with the lower offset in X's values. */
2117 if (offsetx > offsety)
2119 tem = offsetx, offsetx = offsety, offsety = tem;
2120 tem = sizex, sizex = sizey, sizey = tem;
2123 /* If we don't know the size of the lower-offset value, we can't tell
2124 if they conflict. Otherwise, we do the test. */
2125 return sizex >= 0 && offsety >= offsetx + sizex;
2128 /* True dependence: X is read after store in MEM takes place. */
2131 true_dependence (const_rtx mem, enum machine_mode mem_mode, const_rtx x,
2132 bool (*varies) (const_rtx, bool))
2134 rtx x_addr, mem_addr;
2135 rtx base;
2137 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2138 return 1;
2140 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2141 This is used in epilogue deallocation functions, and in cselib. */
2142 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2143 return 1;
2144 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2145 return 1;
2146 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2147 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2148 return 1;
2150 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2151 return 0;
2153 /* Read-only memory is by definition never modified, and therefore can't
2154 conflict with anything. We don't expect to find read-only set on MEM,
2155 but stupid user tricks can produce them, so don't die. */
2156 if (MEM_READONLY_P (x))
2157 return 0;
2159 if (nonoverlapping_memrefs_p (mem, x))
2160 return 0;
2162 if (mem_mode == VOIDmode)
2163 mem_mode = GET_MODE (mem);
2165 x_addr = get_addr (XEXP (x, 0));
2166 mem_addr = get_addr (XEXP (mem, 0));
2168 base = find_base_term (x_addr);
2169 if (base && (GET_CODE (base) == LABEL_REF
2170 || (GET_CODE (base) == SYMBOL_REF
2171 && CONSTANT_POOL_ADDRESS_P (base))))
2172 return 0;
2174 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2175 return 0;
2177 x_addr = canon_rtx (x_addr);
2178 mem_addr = canon_rtx (mem_addr);
2180 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2181 SIZE_FOR_MODE (x), x_addr, 0))
2182 return 0;
2184 if (aliases_everything_p (x))
2185 return 1;
2187 /* We cannot use aliases_everything_p to test MEM, since we must look
2188 at MEM_MODE, rather than GET_MODE (MEM). */
2189 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
2190 return 1;
2192 /* In true_dependence we also allow BLKmode to alias anything. Why
2193 don't we do this in anti_dependence and output_dependence? */
2194 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2195 return 1;
2197 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2198 varies);
2201 /* Canonical true dependence: X is read after store in MEM takes place.
2202 Variant of true_dependence which assumes MEM has already been
2203 canonicalized (hence we no longer do that here).
2204 The mem_addr argument has been added, since true_dependence computed
2205 this value prior to canonicalizing. */
2208 canon_true_dependence (const_rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2209 const_rtx x, bool (*varies) (const_rtx, bool))
2211 rtx x_addr;
2213 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2214 return 1;
2216 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2217 This is used in epilogue deallocation functions. */
2218 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2219 return 1;
2220 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2221 return 1;
2222 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2223 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2224 return 1;
2226 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2227 return 0;
2229 /* Read-only memory is by definition never modified, and therefore can't
2230 conflict with anything. We don't expect to find read-only set on MEM,
2231 but stupid user tricks can produce them, so don't die. */
2232 if (MEM_READONLY_P (x))
2233 return 0;
2235 if (nonoverlapping_memrefs_p (x, mem))
2236 return 0;
2238 x_addr = get_addr (XEXP (x, 0));
2240 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2241 return 0;
2243 x_addr = canon_rtx (x_addr);
2244 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2245 SIZE_FOR_MODE (x), x_addr, 0))
2246 return 0;
2248 if (aliases_everything_p (x))
2249 return 1;
2251 /* We cannot use aliases_everything_p to test MEM, since we must look
2252 at MEM_MODE, rather than GET_MODE (MEM). */
2253 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
2254 return 1;
2256 /* In true_dependence we also allow BLKmode to alias anything. Why
2257 don't we do this in anti_dependence and output_dependence? */
2258 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2259 return 1;
2261 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2262 varies);
2265 /* Returns nonzero if a write to X might alias a previous read from
2266 (or, if WRITEP is nonzero, a write to) MEM. */
2268 static int
2269 write_dependence_p (const_rtx mem, const_rtx x, int writep)
2271 rtx x_addr, mem_addr;
2272 const_rtx fixed_scalar;
2273 rtx base;
2275 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2276 return 1;
2278 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2279 This is used in epilogue deallocation functions. */
2280 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2281 return 1;
2282 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2283 return 1;
2284 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2285 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2286 return 1;
2288 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2289 return 0;
2291 /* A read from read-only memory can't conflict with read-write memory. */
2292 if (!writep && MEM_READONLY_P (mem))
2293 return 0;
2295 if (nonoverlapping_memrefs_p (x, mem))
2296 return 0;
2298 x_addr = get_addr (XEXP (x, 0));
2299 mem_addr = get_addr (XEXP (mem, 0));
2301 if (! writep)
2303 base = find_base_term (mem_addr);
2304 if (base && (GET_CODE (base) == LABEL_REF
2305 || (GET_CODE (base) == SYMBOL_REF
2306 && CONSTANT_POOL_ADDRESS_P (base))))
2307 return 0;
2310 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2311 GET_MODE (mem)))
2312 return 0;
2314 x_addr = canon_rtx (x_addr);
2315 mem_addr = canon_rtx (mem_addr);
2317 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2318 SIZE_FOR_MODE (x), x_addr, 0))
2319 return 0;
2321 fixed_scalar
2322 = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2323 rtx_addr_varies_p);
2325 return (!(fixed_scalar == mem && !aliases_everything_p (x))
2326 && !(fixed_scalar == x && !aliases_everything_p (mem)));
2329 /* Anti dependence: X is written after read in MEM takes place. */
2332 anti_dependence (const_rtx mem, const_rtx x)
2334 return write_dependence_p (mem, x, /*writep=*/0);
2337 /* Output dependence: X is written after store in MEM takes place. */
2340 output_dependence (const_rtx mem, const_rtx x)
2342 return write_dependence_p (mem, x, /*writep=*/1);
2346 void
2347 init_alias_target (void)
2349 int i;
2351 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2353 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2354 /* Check whether this register can hold an incoming pointer
2355 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2356 numbers, so translate if necessary due to register windows. */
2357 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2358 && HARD_REGNO_MODE_OK (i, Pmode))
2359 static_reg_base_value[i]
2360 = gen_rtx_ADDRESS (VOIDmode, gen_rtx_REG (Pmode, i));
2362 static_reg_base_value[STACK_POINTER_REGNUM]
2363 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
2364 static_reg_base_value[ARG_POINTER_REGNUM]
2365 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
2366 static_reg_base_value[FRAME_POINTER_REGNUM]
2367 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
2368 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2369 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2370 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
2371 #endif
2374 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2375 to be memory reference. */
2376 static bool memory_modified;
2377 static void
2378 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2380 if (MEM_P (x))
2382 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2383 memory_modified = true;
2388 /* Return true when INSN possibly modify memory contents of MEM
2389 (i.e. address can be modified). */
2390 bool
2391 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2393 if (!INSN_P (insn))
2394 return false;
2395 memory_modified = false;
2396 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2397 return memory_modified;
2400 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2401 array. */
2403 void
2404 init_alias_analysis (void)
2406 unsigned int maxreg = max_reg_num ();
2407 int changed, pass;
2408 int i;
2409 unsigned int ui;
2410 rtx insn;
2412 timevar_push (TV_ALIAS_ANALYSIS);
2414 reg_known_value_size = maxreg - FIRST_PSEUDO_REGISTER;
2415 reg_known_value = GGC_CNEWVEC (rtx, reg_known_value_size);
2416 reg_known_equiv_p = XCNEWVEC (bool, reg_known_value_size);
2418 /* If we have memory allocated from the previous run, use it. */
2419 if (old_reg_base_value)
2420 reg_base_value = old_reg_base_value;
2422 if (reg_base_value)
2423 VEC_truncate (rtx, reg_base_value, 0);
2425 VEC_safe_grow_cleared (rtx, gc, reg_base_value, maxreg);
2427 new_reg_base_value = XNEWVEC (rtx, maxreg);
2428 reg_seen = XNEWVEC (char, maxreg);
2430 /* The basic idea is that each pass through this loop will use the
2431 "constant" information from the previous pass to propagate alias
2432 information through another level of assignments.
2434 This could get expensive if the assignment chains are long. Maybe
2435 we should throttle the number of iterations, possibly based on
2436 the optimization level or flag_expensive_optimizations.
2438 We could propagate more information in the first pass by making use
2439 of DF_REG_DEF_COUNT to determine immediately that the alias information
2440 for a pseudo is "constant".
2442 A program with an uninitialized variable can cause an infinite loop
2443 here. Instead of doing a full dataflow analysis to detect such problems
2444 we just cap the number of iterations for the loop.
2446 The state of the arrays for the set chain in question does not matter
2447 since the program has undefined behavior. */
2449 pass = 0;
2452 /* Assume nothing will change this iteration of the loop. */
2453 changed = 0;
2455 /* We want to assign the same IDs each iteration of this loop, so
2456 start counting from zero each iteration of the loop. */
2457 unique_id = 0;
2459 /* We're at the start of the function each iteration through the
2460 loop, so we're copying arguments. */
2461 copying_arguments = true;
2463 /* Wipe the potential alias information clean for this pass. */
2464 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2466 /* Wipe the reg_seen array clean. */
2467 memset (reg_seen, 0, maxreg);
2469 /* Mark all hard registers which may contain an address.
2470 The stack, frame and argument pointers may contain an address.
2471 An argument register which can hold a Pmode value may contain
2472 an address even if it is not in BASE_REGS.
2474 The address expression is VOIDmode for an argument and
2475 Pmode for other registers. */
2477 memcpy (new_reg_base_value, static_reg_base_value,
2478 FIRST_PSEUDO_REGISTER * sizeof (rtx));
2480 /* Walk the insns adding values to the new_reg_base_value array. */
2481 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2483 if (INSN_P (insn))
2485 rtx note, set;
2487 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2488 /* The prologue/epilogue insns are not threaded onto the
2489 insn chain until after reload has completed. Thus,
2490 there is no sense wasting time checking if INSN is in
2491 the prologue/epilogue until after reload has completed. */
2492 if (reload_completed
2493 && prologue_epilogue_contains (insn))
2494 continue;
2495 #endif
2497 /* If this insn has a noalias note, process it, Otherwise,
2498 scan for sets. A simple set will have no side effects
2499 which could change the base value of any other register. */
2501 if (GET_CODE (PATTERN (insn)) == SET
2502 && REG_NOTES (insn) != 0
2503 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2504 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2505 else
2506 note_stores (PATTERN (insn), record_set, NULL);
2508 set = single_set (insn);
2510 if (set != 0
2511 && REG_P (SET_DEST (set))
2512 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2514 unsigned int regno = REGNO (SET_DEST (set));
2515 rtx src = SET_SRC (set);
2516 rtx t;
2518 note = find_reg_equal_equiv_note (insn);
2519 if (note && REG_NOTE_KIND (note) == REG_EQUAL
2520 && DF_REG_DEF_COUNT (regno) != 1)
2521 note = NULL_RTX;
2523 if (note != NULL_RTX
2524 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2525 && ! rtx_varies_p (XEXP (note, 0), 1)
2526 && ! reg_overlap_mentioned_p (SET_DEST (set),
2527 XEXP (note, 0)))
2529 set_reg_known_value (regno, XEXP (note, 0));
2530 set_reg_known_equiv_p (regno,
2531 REG_NOTE_KIND (note) == REG_EQUIV);
2533 else if (DF_REG_DEF_COUNT (regno) == 1
2534 && GET_CODE (src) == PLUS
2535 && REG_P (XEXP (src, 0))
2536 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2537 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2539 t = plus_constant (t, INTVAL (XEXP (src, 1)));
2540 set_reg_known_value (regno, t);
2541 set_reg_known_equiv_p (regno, 0);
2543 else if (DF_REG_DEF_COUNT (regno) == 1
2544 && ! rtx_varies_p (src, 1))
2546 set_reg_known_value (regno, src);
2547 set_reg_known_equiv_p (regno, 0);
2551 else if (NOTE_P (insn)
2552 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
2553 copying_arguments = false;
2556 /* Now propagate values from new_reg_base_value to reg_base_value. */
2557 gcc_assert (maxreg == (unsigned int) max_reg_num ());
2559 for (ui = 0; ui < maxreg; ui++)
2561 if (new_reg_base_value[ui]
2562 && new_reg_base_value[ui] != VEC_index (rtx, reg_base_value, ui)
2563 && ! rtx_equal_p (new_reg_base_value[ui],
2564 VEC_index (rtx, reg_base_value, ui)))
2566 VEC_replace (rtx, reg_base_value, ui, new_reg_base_value[ui]);
2567 changed = 1;
2571 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
2573 /* Fill in the remaining entries. */
2574 for (i = 0; i < (int)reg_known_value_size; i++)
2575 if (reg_known_value[i] == 0)
2576 reg_known_value[i] = regno_reg_rtx[i + FIRST_PSEUDO_REGISTER];
2578 /* Clean up. */
2579 free (new_reg_base_value);
2580 new_reg_base_value = 0;
2581 free (reg_seen);
2582 reg_seen = 0;
2583 timevar_pop (TV_ALIAS_ANALYSIS);
2586 void
2587 end_alias_analysis (void)
2589 old_reg_base_value = reg_base_value;
2590 ggc_free (reg_known_value);
2591 reg_known_value = 0;
2592 reg_known_value_size = 0;
2593 free (reg_known_equiv_p);
2594 reg_known_equiv_p = 0;
2597 #include "gt-alias.h"