PR c++/3637
[official-gcc.git] / gcc / alias.c
bloba92cf857844f63de38cd86ad0ae65296eef51423
1 /* Alias analysis for GNU C
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "function.h"
28 #include "expr.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "flags.h"
33 #include "output.h"
34 #include "toplev.h"
35 #include "cselib.h"
36 #include "splay-tree.h"
37 #include "ggc.h"
38 #include "langhooks.h"
40 /* The alias sets assigned to MEMs assist the back-end in determining
41 which MEMs can alias which other MEMs. In general, two MEMs in
42 different alias sets cannot alias each other, with one important
43 exception. Consider something like:
45 struct S {int i; double d; };
47 a store to an `S' can alias something of either type `int' or type
48 `double'. (However, a store to an `int' cannot alias a `double'
49 and vice versa.) We indicate this via a tree structure that looks
50 like:
51 struct S
52 / \
53 / \
54 |/_ _\|
55 int double
57 (The arrows are directed and point downwards.)
58 In this situation we say the alias set for `struct S' is the
59 `superset' and that those for `int' and `double' are `subsets'.
61 To see whether two alias sets can point to the same memory, we must
62 see if either alias set is a subset of the other. We need not trace
63 past immediate descendents, however, since we propagate all
64 grandchildren up one level.
66 Alias set zero is implicitly a superset of all other alias sets.
67 However, this is no actual entry for alias set zero. It is an
68 error to attempt to explicitly construct a subset of zero. */
70 typedef struct alias_set_entry
72 /* The alias set number, as stored in MEM_ALIAS_SET. */
73 HOST_WIDE_INT alias_set;
75 /* The children of the alias set. These are not just the immediate
76 children, but, in fact, all descendents. So, if we have:
78 struct T { struct S s; float f; }
80 continuing our example above, the children here will be all of
81 `int', `double', `float', and `struct S'. */
82 splay_tree children;
84 /* Nonzero if would have a child of zero: this effectively makes this
85 alias set the same as alias set zero. */
86 int has_zero_child;
87 } *alias_set_entry;
89 static int rtx_equal_for_memref_p PARAMS ((rtx, rtx));
90 static rtx find_symbolic_term PARAMS ((rtx));
91 rtx get_addr PARAMS ((rtx));
92 static int memrefs_conflict_p PARAMS ((int, rtx, int, rtx,
93 HOST_WIDE_INT));
94 static void record_set PARAMS ((rtx, rtx, void *));
95 static rtx find_base_term PARAMS ((rtx));
96 static int base_alias_check PARAMS ((rtx, rtx, enum machine_mode,
97 enum machine_mode));
98 static rtx find_base_value PARAMS ((rtx));
99 static int mems_in_disjoint_alias_sets_p PARAMS ((rtx, rtx));
100 static int insert_subset_children PARAMS ((splay_tree_node, void*));
101 static tree find_base_decl PARAMS ((tree));
102 static alias_set_entry get_alias_set_entry PARAMS ((HOST_WIDE_INT));
103 static rtx fixed_scalar_and_varying_struct_p PARAMS ((rtx, rtx, rtx, rtx,
104 int (*) (rtx, int)));
105 static int aliases_everything_p PARAMS ((rtx));
106 static int nonoverlapping_memrefs_p PARAMS ((rtx, rtx));
107 static int write_dependence_p PARAMS ((rtx, rtx, int));
108 static int nonlocal_mentioned_p PARAMS ((rtx));
110 /* Set up all info needed to perform alias analysis on memory references. */
112 /* Returns the size in bytes of the mode of X. */
113 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
115 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
116 different alias sets. We ignore alias sets in functions making use
117 of variable arguments because the va_arg macros on some systems are
118 not legal ANSI C. */
119 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
120 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
122 /* Cap the number of passes we make over the insns propagating alias
123 information through set chains. 10 is a completely arbitrary choice. */
124 #define MAX_ALIAS_LOOP_PASSES 10
126 /* reg_base_value[N] gives an address to which register N is related.
127 If all sets after the first add or subtract to the current value
128 or otherwise modify it so it does not point to a different top level
129 object, reg_base_value[N] is equal to the address part of the source
130 of the first set.
132 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
133 expressions represent certain special values: function arguments and
134 the stack, frame, and argument pointers.
136 The contents of an ADDRESS is not normally used, the mode of the
137 ADDRESS determines whether the ADDRESS is a function argument or some
138 other special value. Pointer equality, not rtx_equal_p, determines whether
139 two ADDRESS expressions refer to the same base address.
141 The only use of the contents of an ADDRESS is for determining if the
142 current function performs nonlocal memory memory references for the
143 purposes of marking the function as a constant function. */
145 static rtx *reg_base_value;
146 static rtx *new_reg_base_value;
147 static unsigned int reg_base_value_size; /* size of reg_base_value array */
149 #define REG_BASE_VALUE(X) \
150 (REGNO (X) < reg_base_value_size \
151 ? reg_base_value[REGNO (X)] : 0)
153 /* Vector of known invariant relationships between registers. Set in
154 loop unrolling. Indexed by register number, if nonzero the value
155 is an expression describing this register in terms of another.
157 The length of this array is REG_BASE_VALUE_SIZE.
159 Because this array contains only pseudo registers it has no effect
160 after reload. */
161 static rtx *alias_invariant;
163 /* Vector indexed by N giving the initial (unchanging) value known for
164 pseudo-register N. This array is initialized in
165 init_alias_analysis, and does not change until end_alias_analysis
166 is called. */
167 rtx *reg_known_value;
169 /* Indicates number of valid entries in reg_known_value. */
170 static unsigned int reg_known_value_size;
172 /* Vector recording for each reg_known_value whether it is due to a
173 REG_EQUIV note. Future passes (viz., reload) may replace the
174 pseudo with the equivalent expression and so we account for the
175 dependences that would be introduced if that happens.
177 The REG_EQUIV notes created in assign_parms may mention the arg
178 pointer, and there are explicit insns in the RTL that modify the
179 arg pointer. Thus we must ensure that such insns don't get
180 scheduled across each other because that would invalidate the
181 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
182 wrong, but solving the problem in the scheduler will likely give
183 better code, so we do it here. */
184 char *reg_known_equiv_p;
186 /* True when scanning insns from the start of the rtl to the
187 NOTE_INSN_FUNCTION_BEG note. */
188 static int copying_arguments;
190 /* The splay-tree used to store the various alias set entries. */
191 static splay_tree alias_sets;
193 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
194 such an entry, or NULL otherwise. */
196 static alias_set_entry
197 get_alias_set_entry (alias_set)
198 HOST_WIDE_INT alias_set;
200 splay_tree_node sn
201 = splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
203 return sn != 0 ? ((alias_set_entry) sn->value) : 0;
206 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
207 the two MEMs cannot alias each other. */
209 static int
210 mems_in_disjoint_alias_sets_p (mem1, mem2)
211 rtx mem1;
212 rtx mem2;
214 #ifdef ENABLE_CHECKING
215 /* Perform a basic sanity check. Namely, that there are no alias sets
216 if we're not using strict aliasing. This helps to catch bugs
217 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
218 where a MEM is allocated in some way other than by the use of
219 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
220 use alias sets to indicate that spilled registers cannot alias each
221 other, we might need to remove this check. */
222 if (! flag_strict_aliasing
223 && (MEM_ALIAS_SET (mem1) != 0 || MEM_ALIAS_SET (mem2) != 0))
224 abort ();
225 #endif
227 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
230 /* Insert the NODE into the splay tree given by DATA. Used by
231 record_alias_subset via splay_tree_foreach. */
233 static int
234 insert_subset_children (node, data)
235 splay_tree_node node;
236 void *data;
238 splay_tree_insert ((splay_tree) data, node->key, node->value);
240 return 0;
243 /* Return 1 if the two specified alias sets may conflict. */
246 alias_sets_conflict_p (set1, set2)
247 HOST_WIDE_INT set1, set2;
249 alias_set_entry ase;
251 /* If have no alias set information for one of the operands, we have
252 to assume it can alias anything. */
253 if (set1 == 0 || set2 == 0
254 /* If the two alias sets are the same, they may alias. */
255 || set1 == set2)
256 return 1;
258 /* See if the first alias set is a subset of the second. */
259 ase = get_alias_set_entry (set1);
260 if (ase != 0
261 && (ase->has_zero_child
262 || splay_tree_lookup (ase->children,
263 (splay_tree_key) set2)))
264 return 1;
266 /* Now do the same, but with the alias sets reversed. */
267 ase = get_alias_set_entry (set2);
268 if (ase != 0
269 && (ase->has_zero_child
270 || splay_tree_lookup (ase->children,
271 (splay_tree_key) set1)))
272 return 1;
274 /* The two alias sets are distinct and neither one is the
275 child of the other. Therefore, they cannot alias. */
276 return 0;
279 /* Return 1 if TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE and has
280 has any readonly fields. If any of the fields have types that
281 contain readonly fields, return true as well. */
284 readonly_fields_p (type)
285 tree type;
287 tree field;
289 if (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
290 && TREE_CODE (type) != QUAL_UNION_TYPE)
291 return 0;
293 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
294 if (TREE_CODE (field) == FIELD_DECL
295 && (TREE_READONLY (field)
296 || readonly_fields_p (TREE_TYPE (field))))
297 return 1;
299 return 0;
302 /* Return 1 if any MEM object of type T1 will always conflict (using the
303 dependency routines in this file) with any MEM object of type T2.
304 This is used when allocating temporary storage. If T1 and/or T2 are
305 NULL_TREE, it means we know nothing about the storage. */
308 objects_must_conflict_p (t1, t2)
309 tree t1, t2;
311 /* If neither has a type specified, we don't know if they'll conflict
312 because we may be using them to store objects of various types, for
313 example the argument and local variables areas of inlined functions. */
314 if (t1 == 0 && t2 == 0)
315 return 0;
317 /* If one or the other has readonly fields or is readonly,
318 then they may not conflict. */
319 if ((t1 != 0 && readonly_fields_p (t1))
320 || (t2 != 0 && readonly_fields_p (t2))
321 || (t1 != 0 && TYPE_READONLY (t1))
322 || (t2 != 0 && TYPE_READONLY (t2)))
323 return 0;
325 /* If they are the same type, they must conflict. */
326 if (t1 == t2
327 /* Likewise if both are volatile. */
328 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
329 return 1;
331 /* If one is aggregate and the other is scalar then they may not
332 conflict. */
333 if ((t1 != 0 && AGGREGATE_TYPE_P (t1))
334 != (t2 != 0 && AGGREGATE_TYPE_P (t2)))
335 return 0;
337 /* Otherwise they conflict only if the alias sets conflict. */
338 return alias_sets_conflict_p (t1 ? get_alias_set (t1) : 0,
339 t2 ? get_alias_set (t2) : 0);
342 /* T is an expression with pointer type. Find the DECL on which this
343 expression is based. (For example, in `a[i]' this would be `a'.)
344 If there is no such DECL, or a unique decl cannot be determined,
345 NULL_TREE is returned. */
347 static tree
348 find_base_decl (t)
349 tree t;
351 tree d0, d1, d2;
353 if (t == 0 || t == error_mark_node || ! POINTER_TYPE_P (TREE_TYPE (t)))
354 return 0;
356 /* If this is a declaration, return it. */
357 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
358 return t;
360 /* Handle general expressions. It would be nice to deal with
361 COMPONENT_REFs here. If we could tell that `a' and `b' were the
362 same, then `a->f' and `b->f' are also the same. */
363 switch (TREE_CODE_CLASS (TREE_CODE (t)))
365 case '1':
366 return find_base_decl (TREE_OPERAND (t, 0));
368 case '2':
369 /* Return 0 if found in neither or both are the same. */
370 d0 = find_base_decl (TREE_OPERAND (t, 0));
371 d1 = find_base_decl (TREE_OPERAND (t, 1));
372 if (d0 == d1)
373 return d0;
374 else if (d0 == 0)
375 return d1;
376 else if (d1 == 0)
377 return d0;
378 else
379 return 0;
381 case '3':
382 d0 = find_base_decl (TREE_OPERAND (t, 0));
383 d1 = find_base_decl (TREE_OPERAND (t, 1));
384 d2 = find_base_decl (TREE_OPERAND (t, 2));
386 /* Set any nonzero values from the last, then from the first. */
387 if (d1 == 0) d1 = d2;
388 if (d0 == 0) d0 = d1;
389 if (d1 == 0) d1 = d0;
390 if (d2 == 0) d2 = d1;
392 /* At this point all are nonzero or all are zero. If all three are the
393 same, return it. Otherwise, return zero. */
394 return (d0 == d1 && d1 == d2) ? d0 : 0;
396 default:
397 return 0;
401 /* Return 1 if all the nested component references handled by
402 get_inner_reference in T are such that we can address the object in T. */
405 can_address_p (t)
406 tree t;
408 /* If we're at the end, it is vacuously addressable. */
409 if (! handled_component_p (t))
410 return 1;
412 /* Bitfields are never addressable. */
413 else if (TREE_CODE (t) == BIT_FIELD_REF)
414 return 0;
416 /* Fields are addressable unless they are marked as nonaddressable or
417 the containing type has alias set 0. */
418 else if (TREE_CODE (t) == COMPONENT_REF
419 && ! DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1))
420 && get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) != 0
421 && can_address_p (TREE_OPERAND (t, 0)))
422 return 1;
424 /* Likewise for arrays. */
425 else if ((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
426 && ! TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0)))
427 && get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) != 0
428 && can_address_p (TREE_OPERAND (t, 0)))
429 return 1;
431 return 0;
434 /* Return the alias set for T, which may be either a type or an
435 expression. Call language-specific routine for help, if needed. */
437 HOST_WIDE_INT
438 get_alias_set (t)
439 tree t;
441 HOST_WIDE_INT set;
443 /* If we're not doing any alias analysis, just assume everything
444 aliases everything else. Also return 0 if this or its type is
445 an error. */
446 if (! flag_strict_aliasing || t == error_mark_node
447 || (! TYPE_P (t)
448 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
449 return 0;
451 /* We can be passed either an expression or a type. This and the
452 language-specific routine may make mutually-recursive calls to each other
453 to figure out what to do. At each juncture, we see if this is a tree
454 that the language may need to handle specially. First handle things that
455 aren't types. */
456 if (! TYPE_P (t))
458 tree inner = t;
459 tree placeholder_ptr = 0;
461 /* Remove any nops, then give the language a chance to do
462 something with this tree before we look at it. */
463 STRIP_NOPS (t);
464 set = (*lang_hooks.get_alias_set) (t);
465 if (set != -1)
466 return set;
468 /* First see if the actual object referenced is an INDIRECT_REF from a
469 restrict-qualified pointer or a "void *". Replace
470 PLACEHOLDER_EXPRs. */
471 while (TREE_CODE (inner) == PLACEHOLDER_EXPR
472 || handled_component_p (inner))
474 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
475 inner = find_placeholder (inner, &placeholder_ptr);
476 else
477 inner = TREE_OPERAND (inner, 0);
479 STRIP_NOPS (inner);
482 /* Check for accesses through restrict-qualified pointers. */
483 if (TREE_CODE (inner) == INDIRECT_REF)
485 tree decl = find_base_decl (TREE_OPERAND (inner, 0));
487 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
489 /* If we haven't computed the actual alias set, do it now. */
490 if (DECL_POINTER_ALIAS_SET (decl) == -2)
492 /* No two restricted pointers can point at the same thing.
493 However, a restricted pointer can point at the same thing
494 as an unrestricted pointer, if that unrestricted pointer
495 is based on the restricted pointer. So, we make the
496 alias set for the restricted pointer a subset of the
497 alias set for the type pointed to by the type of the
498 decl. */
499 HOST_WIDE_INT pointed_to_alias_set
500 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
502 if (pointed_to_alias_set == 0)
503 /* It's not legal to make a subset of alias set zero. */
505 else
507 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
508 record_alias_subset (pointed_to_alias_set,
509 DECL_POINTER_ALIAS_SET (decl));
513 /* We use the alias set indicated in the declaration. */
514 return DECL_POINTER_ALIAS_SET (decl);
517 /* If we have an INDIRECT_REF via a void pointer, we don't
518 know anything about what that might alias. */
519 else if (TREE_CODE (TREE_TYPE (inner)) == VOID_TYPE)
520 return 0;
523 /* Otherwise, pick up the outermost object that we could have a pointer
524 to, processing conversion and PLACEHOLDER_EXPR as above. */
525 placeholder_ptr = 0;
526 while (TREE_CODE (t) == PLACEHOLDER_EXPR
527 || (handled_component_p (t) && ! can_address_p (t)))
529 if (TREE_CODE (t) == PLACEHOLDER_EXPR)
530 t = find_placeholder (t, &placeholder_ptr);
531 else
532 t = TREE_OPERAND (t, 0);
534 STRIP_NOPS (t);
537 /* If we've already determined the alias set for a decl, just return
538 it. This is necessary for C++ anonymous unions, whose component
539 variables don't look like union members (boo!). */
540 if (TREE_CODE (t) == VAR_DECL
541 && DECL_RTL_SET_P (t) && GET_CODE (DECL_RTL (t)) == MEM)
542 return MEM_ALIAS_SET (DECL_RTL (t));
544 /* Now all we care about is the type. */
545 t = TREE_TYPE (t);
548 /* Variant qualifiers don't affect the alias set, so get the main
549 variant. If this is a type with a known alias set, return it. */
550 t = TYPE_MAIN_VARIANT (t);
551 if (TYPE_ALIAS_SET_KNOWN_P (t))
552 return TYPE_ALIAS_SET (t);
554 /* See if the language has special handling for this type. */
555 set = (*lang_hooks.get_alias_set) (t);
556 if (set != -1)
557 return set;
559 /* There are no objects of FUNCTION_TYPE, so there's no point in
560 using up an alias set for them. (There are, of course, pointers
561 and references to functions, but that's different.) */
562 else if (TREE_CODE (t) == FUNCTION_TYPE)
563 set = 0;
564 else
565 /* Otherwise make a new alias set for this type. */
566 set = new_alias_set ();
568 TYPE_ALIAS_SET (t) = set;
570 /* If this is an aggregate type, we must record any component aliasing
571 information. */
572 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
573 record_component_aliases (t);
575 return set;
578 /* Return a brand-new alias set. */
580 HOST_WIDE_INT
581 new_alias_set ()
583 static HOST_WIDE_INT last_alias_set;
585 if (flag_strict_aliasing)
586 return ++last_alias_set;
587 else
588 return 0;
591 /* Indicate that things in SUBSET can alias things in SUPERSET, but
592 not vice versa. For example, in C, a store to an `int' can alias a
593 structure containing an `int', but not vice versa. Here, the
594 structure would be the SUPERSET and `int' the SUBSET. This
595 function should be called only once per SUPERSET/SUBSET pair.
597 It is illegal for SUPERSET to be zero; everything is implicitly a
598 subset of alias set zero. */
600 void
601 record_alias_subset (superset, subset)
602 HOST_WIDE_INT superset;
603 HOST_WIDE_INT subset;
605 alias_set_entry superset_entry;
606 alias_set_entry subset_entry;
608 /* It is possible in complex type situations for both sets to be the same,
609 in which case we can ignore this operation. */
610 if (superset == subset)
611 return;
613 if (superset == 0)
614 abort ();
616 superset_entry = get_alias_set_entry (superset);
617 if (superset_entry == 0)
619 /* Create an entry for the SUPERSET, so that we have a place to
620 attach the SUBSET. */
621 superset_entry
622 = (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
623 superset_entry->alias_set = superset;
624 superset_entry->children
625 = splay_tree_new (splay_tree_compare_ints, 0, 0);
626 superset_entry->has_zero_child = 0;
627 splay_tree_insert (alias_sets, (splay_tree_key) superset,
628 (splay_tree_value) superset_entry);
631 if (subset == 0)
632 superset_entry->has_zero_child = 1;
633 else
635 subset_entry = get_alias_set_entry (subset);
636 /* If there is an entry for the subset, enter all of its children
637 (if they are not already present) as children of the SUPERSET. */
638 if (subset_entry)
640 if (subset_entry->has_zero_child)
641 superset_entry->has_zero_child = 1;
643 splay_tree_foreach (subset_entry->children, insert_subset_children,
644 superset_entry->children);
647 /* Enter the SUBSET itself as a child of the SUPERSET. */
648 splay_tree_insert (superset_entry->children,
649 (splay_tree_key) subset, 0);
653 /* Record that component types of TYPE, if any, are part of that type for
654 aliasing purposes. For record types, we only record component types
655 for fields that are marked addressable. For array types, we always
656 record the component types, so the front end should not call this
657 function if the individual component aren't addressable. */
659 void
660 record_component_aliases (type)
661 tree type;
663 HOST_WIDE_INT superset = get_alias_set (type);
664 tree field;
666 if (superset == 0)
667 return;
669 switch (TREE_CODE (type))
671 case ARRAY_TYPE:
672 if (! TYPE_NONALIASED_COMPONENT (type))
673 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
674 break;
676 case RECORD_TYPE:
677 case UNION_TYPE:
678 case QUAL_UNION_TYPE:
679 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
680 if (TREE_CODE (field) == FIELD_DECL && ! DECL_NONADDRESSABLE_P (field))
681 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
682 break;
684 case COMPLEX_TYPE:
685 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
686 break;
688 default:
689 break;
693 /* Allocate an alias set for use in storing and reading from the varargs
694 spill area. */
696 HOST_WIDE_INT
697 get_varargs_alias_set ()
699 static HOST_WIDE_INT set = -1;
701 if (set == -1)
702 set = new_alias_set ();
704 return set;
707 /* Likewise, but used for the fixed portions of the frame, e.g., register
708 save areas. */
710 HOST_WIDE_INT
711 get_frame_alias_set ()
713 static HOST_WIDE_INT set = -1;
715 if (set == -1)
716 set = new_alias_set ();
718 return set;
721 /* Inside SRC, the source of a SET, find a base address. */
723 static rtx
724 find_base_value (src)
725 rtx src;
727 unsigned int regno;
728 switch (GET_CODE (src))
730 case SYMBOL_REF:
731 case LABEL_REF:
732 return src;
734 case REG:
735 regno = REGNO (src);
736 /* At the start of a function, argument registers have known base
737 values which may be lost later. Returning an ADDRESS
738 expression here allows optimization based on argument values
739 even when the argument registers are used for other purposes. */
740 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
741 return new_reg_base_value[regno];
743 /* If a pseudo has a known base value, return it. Do not do this
744 for hard regs since it can result in a circular dependency
745 chain for registers which have values at function entry.
747 The test above is not sufficient because the scheduler may move
748 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
749 if (regno >= FIRST_PSEUDO_REGISTER
750 && regno < reg_base_value_size
751 && reg_base_value[regno])
752 return reg_base_value[regno];
754 return src;
756 case MEM:
757 /* Check for an argument passed in memory. Only record in the
758 copying-arguments block; it is too hard to track changes
759 otherwise. */
760 if (copying_arguments
761 && (XEXP (src, 0) == arg_pointer_rtx
762 || (GET_CODE (XEXP (src, 0)) == PLUS
763 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
764 return gen_rtx_ADDRESS (VOIDmode, src);
765 return 0;
767 case CONST:
768 src = XEXP (src, 0);
769 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
770 break;
772 /* ... fall through ... */
774 case PLUS:
775 case MINUS:
777 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
779 /* If either operand is a REG, then see if we already have
780 a known value for it. */
781 if (GET_CODE (src_0) == REG)
783 temp = find_base_value (src_0);
784 if (temp != 0)
785 src_0 = temp;
788 if (GET_CODE (src_1) == REG)
790 temp = find_base_value (src_1);
791 if (temp!= 0)
792 src_1 = temp;
795 /* Guess which operand is the base address:
796 If either operand is a symbol, then it is the base. If
797 either operand is a CONST_INT, then the other is the base. */
798 if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
799 return find_base_value (src_0);
800 else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
801 return find_base_value (src_1);
803 /* This might not be necessary anymore:
804 If either operand is a REG that is a known pointer, then it
805 is the base. */
806 else if (GET_CODE (src_0) == REG && REG_POINTER (src_0))
807 return find_base_value (src_0);
808 else if (GET_CODE (src_1) == REG && REG_POINTER (src_1))
809 return find_base_value (src_1);
811 return 0;
814 case LO_SUM:
815 /* The standard form is (lo_sum reg sym) so look only at the
816 second operand. */
817 return find_base_value (XEXP (src, 1));
819 case AND:
820 /* If the second operand is constant set the base
821 address to the first operand. */
822 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
823 return find_base_value (XEXP (src, 0));
824 return 0;
826 case TRUNCATE:
827 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
828 break;
829 /* Fall through. */
830 case ZERO_EXTEND:
831 case SIGN_EXTEND: /* used for NT/Alpha pointers */
832 case HIGH:
833 return find_base_value (XEXP (src, 0));
835 default:
836 break;
839 return 0;
842 /* Called from init_alias_analysis indirectly through note_stores. */
844 /* While scanning insns to find base values, reg_seen[N] is nonzero if
845 register N has been set in this function. */
846 static char *reg_seen;
848 /* Addresses which are known not to alias anything else are identified
849 by a unique integer. */
850 static int unique_id;
852 static void
853 record_set (dest, set, data)
854 rtx dest, set;
855 void *data ATTRIBUTE_UNUSED;
857 unsigned regno;
858 rtx src;
860 if (GET_CODE (dest) != REG)
861 return;
863 regno = REGNO (dest);
865 if (regno >= reg_base_value_size)
866 abort ();
868 if (set)
870 /* A CLOBBER wipes out any old value but does not prevent a previously
871 unset register from acquiring a base address (i.e. reg_seen is not
872 set). */
873 if (GET_CODE (set) == CLOBBER)
875 new_reg_base_value[regno] = 0;
876 return;
878 src = SET_SRC (set);
880 else
882 if (reg_seen[regno])
884 new_reg_base_value[regno] = 0;
885 return;
887 reg_seen[regno] = 1;
888 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
889 GEN_INT (unique_id++));
890 return;
893 /* This is not the first set. If the new value is not related to the
894 old value, forget the base value. Note that the following code is
895 not detected:
896 extern int x, y; int *p = &x; p += (&y-&x);
897 ANSI C does not allow computing the difference of addresses
898 of distinct top level objects. */
899 if (new_reg_base_value[regno])
900 switch (GET_CODE (src))
902 case LO_SUM:
903 case MINUS:
904 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
905 new_reg_base_value[regno] = 0;
906 break;
907 case PLUS:
908 /* If the value we add in the PLUS is also a valid base value,
909 this might be the actual base value, and the original value
910 an index. */
912 rtx other = NULL_RTX;
914 if (XEXP (src, 0) == dest)
915 other = XEXP (src, 1);
916 else if (XEXP (src, 1) == dest)
917 other = XEXP (src, 0);
919 if (! other || find_base_value (other))
920 new_reg_base_value[regno] = 0;
921 break;
923 case AND:
924 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
925 new_reg_base_value[regno] = 0;
926 break;
927 default:
928 new_reg_base_value[regno] = 0;
929 break;
931 /* If this is the first set of a register, record the value. */
932 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
933 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
934 new_reg_base_value[regno] = find_base_value (src);
936 reg_seen[regno] = 1;
939 /* Called from loop optimization when a new pseudo-register is
940 created. It indicates that REGNO is being set to VAL. f INVARIANT
941 is true then this value also describes an invariant relationship
942 which can be used to deduce that two registers with unknown values
943 are different. */
945 void
946 record_base_value (regno, val, invariant)
947 unsigned int regno;
948 rtx val;
949 int invariant;
951 if (regno >= reg_base_value_size)
952 return;
954 if (invariant && alias_invariant)
955 alias_invariant[regno] = val;
957 if (GET_CODE (val) == REG)
959 if (REGNO (val) < reg_base_value_size)
960 reg_base_value[regno] = reg_base_value[REGNO (val)];
962 return;
965 reg_base_value[regno] = find_base_value (val);
968 /* Clear alias info for a register. This is used if an RTL transformation
969 changes the value of a register. This is used in flow by AUTO_INC_DEC
970 optimizations. We don't need to clear reg_base_value, since flow only
971 changes the offset. */
973 void
974 clear_reg_alias_info (reg)
975 rtx reg;
977 unsigned int regno = REGNO (reg);
979 if (regno < reg_known_value_size && regno >= FIRST_PSEUDO_REGISTER)
980 reg_known_value[regno] = reg;
983 /* Returns a canonical version of X, from the point of view alias
984 analysis. (For example, if X is a MEM whose address is a register,
985 and the register has a known value (say a SYMBOL_REF), then a MEM
986 whose address is the SYMBOL_REF is returned.) */
989 canon_rtx (x)
990 rtx x;
992 /* Recursively look for equivalences. */
993 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
994 && REGNO (x) < reg_known_value_size)
995 return reg_known_value[REGNO (x)] == x
996 ? x : canon_rtx (reg_known_value[REGNO (x)]);
997 else if (GET_CODE (x) == PLUS)
999 rtx x0 = canon_rtx (XEXP (x, 0));
1000 rtx x1 = canon_rtx (XEXP (x, 1));
1002 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1004 if (GET_CODE (x0) == CONST_INT)
1005 return plus_constant (x1, INTVAL (x0));
1006 else if (GET_CODE (x1) == CONST_INT)
1007 return plus_constant (x0, INTVAL (x1));
1008 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1012 /* This gives us much better alias analysis when called from
1013 the loop optimizer. Note we want to leave the original
1014 MEM alone, but need to return the canonicalized MEM with
1015 all the flags with their original values. */
1016 else if (GET_CODE (x) == MEM)
1017 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1019 return x;
1022 /* Return 1 if X and Y are identical-looking rtx's.
1024 We use the data in reg_known_value above to see if two registers with
1025 different numbers are, in fact, equivalent. */
1027 static int
1028 rtx_equal_for_memref_p (x, y)
1029 rtx x, y;
1031 int i;
1032 int j;
1033 enum rtx_code code;
1034 const char *fmt;
1036 if (x == 0 && y == 0)
1037 return 1;
1038 if (x == 0 || y == 0)
1039 return 0;
1041 x = canon_rtx (x);
1042 y = canon_rtx (y);
1044 if (x == y)
1045 return 1;
1047 code = GET_CODE (x);
1048 /* Rtx's of different codes cannot be equal. */
1049 if (code != GET_CODE (y))
1050 return 0;
1052 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1053 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1055 if (GET_MODE (x) != GET_MODE (y))
1056 return 0;
1058 /* Some RTL can be compared without a recursive examination. */
1059 switch (code)
1061 case VALUE:
1062 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
1064 case REG:
1065 return REGNO (x) == REGNO (y);
1067 case LABEL_REF:
1068 return XEXP (x, 0) == XEXP (y, 0);
1070 case SYMBOL_REF:
1071 return XSTR (x, 0) == XSTR (y, 0);
1073 case CONST_INT:
1074 case CONST_DOUBLE:
1075 /* There's no need to compare the contents of CONST_DOUBLEs or
1076 CONST_INTs because pointer equality is a good enough
1077 comparison for these nodes. */
1078 return 0;
1080 case ADDRESSOF:
1081 return (XINT (x, 1) == XINT (y, 1)
1082 && rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0)));
1084 default:
1085 break;
1088 /* For commutative operations, the RTX match if the operand match in any
1089 order. Also handle the simple binary and unary cases without a loop. */
1090 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
1091 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1092 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1093 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1094 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1095 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
1096 return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1097 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
1098 else if (GET_RTX_CLASS (code) == '1')
1099 return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
1101 /* Compare the elements. If any pair of corresponding elements
1102 fail to match, return 0 for the whole things.
1104 Limit cases to types which actually appear in addresses. */
1106 fmt = GET_RTX_FORMAT (code);
1107 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1109 switch (fmt[i])
1111 case 'i':
1112 if (XINT (x, i) != XINT (y, i))
1113 return 0;
1114 break;
1116 case 'E':
1117 /* Two vectors must have the same length. */
1118 if (XVECLEN (x, i) != XVECLEN (y, i))
1119 return 0;
1121 /* And the corresponding elements must match. */
1122 for (j = 0; j < XVECLEN (x, i); j++)
1123 if (rtx_equal_for_memref_p (XVECEXP (x, i, j),
1124 XVECEXP (y, i, j)) == 0)
1125 return 0;
1126 break;
1128 case 'e':
1129 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
1130 return 0;
1131 break;
1133 /* This can happen for asm operands. */
1134 case 's':
1135 if (strcmp (XSTR (x, i), XSTR (y, i)))
1136 return 0;
1137 break;
1139 /* This can happen for an asm which clobbers memory. */
1140 case '0':
1141 break;
1143 /* It is believed that rtx's at this level will never
1144 contain anything but integers and other rtx's,
1145 except for within LABEL_REFs and SYMBOL_REFs. */
1146 default:
1147 abort ();
1150 return 1;
1153 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
1154 X and return it, or return 0 if none found. */
1156 static rtx
1157 find_symbolic_term (x)
1158 rtx x;
1160 int i;
1161 enum rtx_code code;
1162 const char *fmt;
1164 code = GET_CODE (x);
1165 if (code == SYMBOL_REF || code == LABEL_REF)
1166 return x;
1167 if (GET_RTX_CLASS (code) == 'o')
1168 return 0;
1170 fmt = GET_RTX_FORMAT (code);
1171 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1173 rtx t;
1175 if (fmt[i] == 'e')
1177 t = find_symbolic_term (XEXP (x, i));
1178 if (t != 0)
1179 return t;
1181 else if (fmt[i] == 'E')
1182 break;
1184 return 0;
1187 static rtx
1188 find_base_term (x)
1189 rtx x;
1191 cselib_val *val;
1192 struct elt_loc_list *l;
1194 #if defined (FIND_BASE_TERM)
1195 /* Try machine-dependent ways to find the base term. */
1196 x = FIND_BASE_TERM (x);
1197 #endif
1199 switch (GET_CODE (x))
1201 case REG:
1202 return REG_BASE_VALUE (x);
1204 case ZERO_EXTEND:
1205 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1206 case HIGH:
1207 case PRE_INC:
1208 case PRE_DEC:
1209 case POST_INC:
1210 case POST_DEC:
1211 return find_base_term (XEXP (x, 0));
1213 case VALUE:
1214 val = CSELIB_VAL_PTR (x);
1215 for (l = val->locs; l; l = l->next)
1216 if ((x = find_base_term (l->loc)) != 0)
1217 return x;
1218 return 0;
1220 case CONST:
1221 x = XEXP (x, 0);
1222 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1223 return 0;
1224 /* fall through */
1225 case LO_SUM:
1226 case PLUS:
1227 case MINUS:
1229 rtx tmp1 = XEXP (x, 0);
1230 rtx tmp2 = XEXP (x, 1);
1232 /* This is a little bit tricky since we have to determine which of
1233 the two operands represents the real base address. Otherwise this
1234 routine may return the index register instead of the base register.
1236 That may cause us to believe no aliasing was possible, when in
1237 fact aliasing is possible.
1239 We use a few simple tests to guess the base register. Additional
1240 tests can certainly be added. For example, if one of the operands
1241 is a shift or multiply, then it must be the index register and the
1242 other operand is the base register. */
1244 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1245 return find_base_term (tmp2);
1247 /* If either operand is known to be a pointer, then use it
1248 to determine the base term. */
1249 if (REG_P (tmp1) && REG_POINTER (tmp1))
1250 return find_base_term (tmp1);
1252 if (REG_P (tmp2) && REG_POINTER (tmp2))
1253 return find_base_term (tmp2);
1255 /* Neither operand was known to be a pointer. Go ahead and find the
1256 base term for both operands. */
1257 tmp1 = find_base_term (tmp1);
1258 tmp2 = find_base_term (tmp2);
1260 /* If either base term is named object or a special address
1261 (like an argument or stack reference), then use it for the
1262 base term. */
1263 if (tmp1 != 0
1264 && (GET_CODE (tmp1) == SYMBOL_REF
1265 || GET_CODE (tmp1) == LABEL_REF
1266 || (GET_CODE (tmp1) == ADDRESS
1267 && GET_MODE (tmp1) != VOIDmode)))
1268 return tmp1;
1270 if (tmp2 != 0
1271 && (GET_CODE (tmp2) == SYMBOL_REF
1272 || GET_CODE (tmp2) == LABEL_REF
1273 || (GET_CODE (tmp2) == ADDRESS
1274 && GET_MODE (tmp2) != VOIDmode)))
1275 return tmp2;
1277 /* We could not determine which of the two operands was the
1278 base register and which was the index. So we can determine
1279 nothing from the base alias check. */
1280 return 0;
1283 case AND:
1284 if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
1285 return REG_BASE_VALUE (XEXP (x, 0));
1286 return 0;
1288 case SYMBOL_REF:
1289 case LABEL_REF:
1290 return x;
1292 case ADDRESSOF:
1293 return REG_BASE_VALUE (frame_pointer_rtx);
1295 default:
1296 return 0;
1300 /* Return 0 if the addresses X and Y are known to point to different
1301 objects, 1 if they might be pointers to the same object. */
1303 static int
1304 base_alias_check (x, y, x_mode, y_mode)
1305 rtx x, y;
1306 enum machine_mode x_mode, y_mode;
1308 rtx x_base = find_base_term (x);
1309 rtx y_base = find_base_term (y);
1311 /* If the address itself has no known base see if a known equivalent
1312 value has one. If either address still has no known base, nothing
1313 is known about aliasing. */
1314 if (x_base == 0)
1316 rtx x_c;
1318 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1319 return 1;
1321 x_base = find_base_term (x_c);
1322 if (x_base == 0)
1323 return 1;
1326 if (y_base == 0)
1328 rtx y_c;
1329 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1330 return 1;
1332 y_base = find_base_term (y_c);
1333 if (y_base == 0)
1334 return 1;
1337 /* If the base addresses are equal nothing is known about aliasing. */
1338 if (rtx_equal_p (x_base, y_base))
1339 return 1;
1341 /* The base addresses of the read and write are different expressions.
1342 If they are both symbols and they are not accessed via AND, there is
1343 no conflict. We can bring knowledge of object alignment into play
1344 here. For example, on alpha, "char a, b;" can alias one another,
1345 though "char a; long b;" cannot. */
1346 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1348 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1349 return 1;
1350 if (GET_CODE (x) == AND
1351 && (GET_CODE (XEXP (x, 1)) != CONST_INT
1352 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1353 return 1;
1354 if (GET_CODE (y) == AND
1355 && (GET_CODE (XEXP (y, 1)) != CONST_INT
1356 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1357 return 1;
1358 /* Differing symbols never alias. */
1359 return 0;
1362 /* If one address is a stack reference there can be no alias:
1363 stack references using different base registers do not alias,
1364 a stack reference can not alias a parameter, and a stack reference
1365 can not alias a global. */
1366 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1367 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1368 return 0;
1370 if (! flag_argument_noalias)
1371 return 1;
1373 if (flag_argument_noalias > 1)
1374 return 0;
1376 /* Weak noalias assertion (arguments are distinct, but may match globals). */
1377 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1380 /* Convert the address X into something we can use. This is done by returning
1381 it unchanged unless it is a value; in the latter case we call cselib to get
1382 a more useful rtx. */
1385 get_addr (x)
1386 rtx x;
1388 cselib_val *v;
1389 struct elt_loc_list *l;
1391 if (GET_CODE (x) != VALUE)
1392 return x;
1393 v = CSELIB_VAL_PTR (x);
1394 for (l = v->locs; l; l = l->next)
1395 if (CONSTANT_P (l->loc))
1396 return l->loc;
1397 for (l = v->locs; l; l = l->next)
1398 if (GET_CODE (l->loc) != REG && GET_CODE (l->loc) != MEM)
1399 return l->loc;
1400 if (v->locs)
1401 return v->locs->loc;
1402 return x;
1405 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1406 where SIZE is the size in bytes of the memory reference. If ADDR
1407 is not modified by the memory reference then ADDR is returned. */
1410 addr_side_effect_eval (addr, size, n_refs)
1411 rtx addr;
1412 int size;
1413 int n_refs;
1415 int offset = 0;
1417 switch (GET_CODE (addr))
1419 case PRE_INC:
1420 offset = (n_refs + 1) * size;
1421 break;
1422 case PRE_DEC:
1423 offset = -(n_refs + 1) * size;
1424 break;
1425 case POST_INC:
1426 offset = n_refs * size;
1427 break;
1428 case POST_DEC:
1429 offset = -n_refs * size;
1430 break;
1432 default:
1433 return addr;
1436 if (offset)
1437 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
1438 else
1439 addr = XEXP (addr, 0);
1441 return addr;
1444 /* Return nonzero if X and Y (memory addresses) could reference the
1445 same location in memory. C is an offset accumulator. When
1446 C is nonzero, we are testing aliases between X and Y + C.
1447 XSIZE is the size in bytes of the X reference,
1448 similarly YSIZE is the size in bytes for Y.
1450 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1451 referenced (the reference was BLKmode), so make the most pessimistic
1452 assumptions.
1454 If XSIZE or YSIZE is negative, we may access memory outside the object
1455 being referenced as a side effect. This can happen when using AND to
1456 align memory references, as is done on the Alpha.
1458 Nice to notice that varying addresses cannot conflict with fp if no
1459 local variables had their addresses taken, but that's too hard now. */
1461 static int
1462 memrefs_conflict_p (xsize, x, ysize, y, c)
1463 rtx x, y;
1464 int xsize, ysize;
1465 HOST_WIDE_INT c;
1467 if (GET_CODE (x) == VALUE)
1468 x = get_addr (x);
1469 if (GET_CODE (y) == VALUE)
1470 y = get_addr (y);
1471 if (GET_CODE (x) == HIGH)
1472 x = XEXP (x, 0);
1473 else if (GET_CODE (x) == LO_SUM)
1474 x = XEXP (x, 1);
1475 else
1476 x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
1477 if (GET_CODE (y) == HIGH)
1478 y = XEXP (y, 0);
1479 else if (GET_CODE (y) == LO_SUM)
1480 y = XEXP (y, 1);
1481 else
1482 y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
1484 if (rtx_equal_for_memref_p (x, y))
1486 if (xsize <= 0 || ysize <= 0)
1487 return 1;
1488 if (c >= 0 && xsize > c)
1489 return 1;
1490 if (c < 0 && ysize+c > 0)
1491 return 1;
1492 return 0;
1495 /* This code used to check for conflicts involving stack references and
1496 globals but the base address alias code now handles these cases. */
1498 if (GET_CODE (x) == PLUS)
1500 /* The fact that X is canonicalized means that this
1501 PLUS rtx is canonicalized. */
1502 rtx x0 = XEXP (x, 0);
1503 rtx x1 = XEXP (x, 1);
1505 if (GET_CODE (y) == PLUS)
1507 /* The fact that Y is canonicalized means that this
1508 PLUS rtx is canonicalized. */
1509 rtx y0 = XEXP (y, 0);
1510 rtx y1 = XEXP (y, 1);
1512 if (rtx_equal_for_memref_p (x1, y1))
1513 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1514 if (rtx_equal_for_memref_p (x0, y0))
1515 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1516 if (GET_CODE (x1) == CONST_INT)
1518 if (GET_CODE (y1) == CONST_INT)
1519 return memrefs_conflict_p (xsize, x0, ysize, y0,
1520 c - INTVAL (x1) + INTVAL (y1));
1521 else
1522 return memrefs_conflict_p (xsize, x0, ysize, y,
1523 c - INTVAL (x1));
1525 else if (GET_CODE (y1) == CONST_INT)
1526 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1528 return 1;
1530 else if (GET_CODE (x1) == CONST_INT)
1531 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1533 else if (GET_CODE (y) == PLUS)
1535 /* The fact that Y is canonicalized means that this
1536 PLUS rtx is canonicalized. */
1537 rtx y0 = XEXP (y, 0);
1538 rtx y1 = XEXP (y, 1);
1540 if (GET_CODE (y1) == CONST_INT)
1541 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1542 else
1543 return 1;
1546 if (GET_CODE (x) == GET_CODE (y))
1547 switch (GET_CODE (x))
1549 case MULT:
1551 /* Handle cases where we expect the second operands to be the
1552 same, and check only whether the first operand would conflict
1553 or not. */
1554 rtx x0, y0;
1555 rtx x1 = canon_rtx (XEXP (x, 1));
1556 rtx y1 = canon_rtx (XEXP (y, 1));
1557 if (! rtx_equal_for_memref_p (x1, y1))
1558 return 1;
1559 x0 = canon_rtx (XEXP (x, 0));
1560 y0 = canon_rtx (XEXP (y, 0));
1561 if (rtx_equal_for_memref_p (x0, y0))
1562 return (xsize == 0 || ysize == 0
1563 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1565 /* Can't properly adjust our sizes. */
1566 if (GET_CODE (x1) != CONST_INT)
1567 return 1;
1568 xsize /= INTVAL (x1);
1569 ysize /= INTVAL (x1);
1570 c /= INTVAL (x1);
1571 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1574 case REG:
1575 /* Are these registers known not to be equal? */
1576 if (alias_invariant)
1578 unsigned int r_x = REGNO (x), r_y = REGNO (y);
1579 rtx i_x, i_y; /* invariant relationships of X and Y */
1581 i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1582 i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1584 if (i_x == 0 && i_y == 0)
1585 break;
1587 if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1588 ysize, i_y ? i_y : y, c))
1589 return 0;
1591 break;
1593 default:
1594 break;
1597 /* Treat an access through an AND (e.g. a subword access on an Alpha)
1598 as an access with indeterminate size. Assume that references
1599 besides AND are aligned, so if the size of the other reference is
1600 at least as large as the alignment, assume no other overlap. */
1601 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1603 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1604 xsize = -1;
1605 return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1607 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1609 /* ??? If we are indexing far enough into the array/structure, we
1610 may yet be able to determine that we can not overlap. But we
1611 also need to that we are far enough from the end not to overlap
1612 a following reference, so we do nothing with that for now. */
1613 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1614 ysize = -1;
1615 return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1618 if (GET_CODE (x) == ADDRESSOF)
1620 if (y == frame_pointer_rtx
1621 || GET_CODE (y) == ADDRESSOF)
1622 return xsize <= 0 || ysize <= 0;
1624 if (GET_CODE (y) == ADDRESSOF)
1626 if (x == frame_pointer_rtx)
1627 return xsize <= 0 || ysize <= 0;
1630 if (CONSTANT_P (x))
1632 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1634 c += (INTVAL (y) - INTVAL (x));
1635 return (xsize <= 0 || ysize <= 0
1636 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1639 if (GET_CODE (x) == CONST)
1641 if (GET_CODE (y) == CONST)
1642 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1643 ysize, canon_rtx (XEXP (y, 0)), c);
1644 else
1645 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1646 ysize, y, c);
1648 if (GET_CODE (y) == CONST)
1649 return memrefs_conflict_p (xsize, x, ysize,
1650 canon_rtx (XEXP (y, 0)), c);
1652 if (CONSTANT_P (y))
1653 return (xsize <= 0 || ysize <= 0
1654 || (rtx_equal_for_memref_p (x, y)
1655 && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1657 return 1;
1659 return 1;
1662 /* Functions to compute memory dependencies.
1664 Since we process the insns in execution order, we can build tables
1665 to keep track of what registers are fixed (and not aliased), what registers
1666 are varying in known ways, and what registers are varying in unknown
1667 ways.
1669 If both memory references are volatile, then there must always be a
1670 dependence between the two references, since their order can not be
1671 changed. A volatile and non-volatile reference can be interchanged
1672 though.
1674 A MEM_IN_STRUCT reference at a non-AND varying address can never
1675 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
1676 also must allow AND addresses, because they may generate accesses
1677 outside the object being referenced. This is used to generate
1678 aligned addresses from unaligned addresses, for instance, the alpha
1679 storeqi_unaligned pattern. */
1681 /* Read dependence: X is read after read in MEM takes place. There can
1682 only be a dependence here if both reads are volatile. */
1685 read_dependence (mem, x)
1686 rtx mem;
1687 rtx x;
1689 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1692 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1693 MEM2 is a reference to a structure at a varying address, or returns
1694 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1695 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1696 to decide whether or not an address may vary; it should return
1697 nonzero whenever variation is possible.
1698 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
1700 static rtx
1701 fixed_scalar_and_varying_struct_p (mem1, mem2, mem1_addr, mem2_addr, varies_p)
1702 rtx mem1, mem2;
1703 rtx mem1_addr, mem2_addr;
1704 int (*varies_p) PARAMS ((rtx, int));
1706 if (! flag_strict_aliasing)
1707 return NULL_RTX;
1709 if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
1710 && !varies_p (mem1_addr, 1) && varies_p (mem2_addr, 1))
1711 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1712 varying address. */
1713 return mem1;
1715 if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
1716 && varies_p (mem1_addr, 1) && !varies_p (mem2_addr, 1))
1717 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1718 varying address. */
1719 return mem2;
1721 return NULL_RTX;
1724 /* Returns nonzero if something about the mode or address format MEM1
1725 indicates that it might well alias *anything*. */
1727 static int
1728 aliases_everything_p (mem)
1729 rtx mem;
1731 if (GET_CODE (XEXP (mem, 0)) == AND)
1732 /* If the address is an AND, its very hard to know at what it is
1733 actually pointing. */
1734 return 1;
1736 return 0;
1739 /* Return nonzero if we can deterimine the decls corresponding to memrefs
1740 X and Y and they do not overlap. */
1742 static int
1743 nonoverlapping_memrefs_p (x, y)
1744 rtx x, y;
1746 rtx rtlx, rtly;
1747 rtx basex, basey;
1748 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
1750 /* Unless both have decls, we can't tell anything. */
1751 if (MEM_DECL (x) == 0 || MEM_DECL (y) == 0)
1752 return 0;
1754 rtlx = DECL_RTL (MEM_DECL (x));
1755 rtly = DECL_RTL (MEM_DECL (y));
1757 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
1758 can't overlap unless they are the same because we never reuse that part
1759 of the stack frame used for locals for spilled pseudos. */
1760 if ((GET_CODE (rtlx) != MEM || GET_CODE (rtly) != MEM)
1761 && ! rtx_equal_p (rtlx, rtly))
1762 return 1;
1764 /* Get the base and offsets of both decls. If either is a register, we
1765 know both are and are the same, so use that as the base. The only
1766 we can avoid overlap is if we can deduce that they are nonoverlapping
1767 pieces of that decl, which is very rare. */
1768 basex = GET_CODE (rtlx) == MEM ? XEXP (rtlx, 0) : rtlx;
1769 if (GET_CODE (basex) == PLUS && GET_CODE (XEXP (basex, 1)) == CONST_INT)
1770 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
1772 basey = GET_CODE (rtly) == MEM ? XEXP (rtly, 0) : rtly;
1773 if (GET_CODE (basey) == PLUS && GET_CODE (XEXP (basey, 1)) == CONST_INT)
1774 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
1776 /* If the bases are different, we know they do not overlap if both
1777 are constants or if one is a constant and the other a pointer into the
1778 stack frame. Otherwise a different base means we can't tell if they
1779 overlap or not. */
1780 if (! rtx_equal_p (basex, basey))
1781 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
1782 || (CONSTANT_P (basex) && REG_P (basey)
1783 && REGNO_PTR_FRAME_P (REGNO (basey)))
1784 || (CONSTANT_P (basey) && REG_P (basex)
1785 && REGNO_PTR_FRAME_P (REGNO (basex))));
1787 sizex = (GET_CODE (rtlx) != MEM ? GET_MODE_SIZE (GET_MODE (rtlx))
1788 : MEM_SIZE (rtlx) ? INTVAL (MEM_SIZE (rtlx))
1789 : -1);
1790 sizey = (GET_CODE (rtly) != MEM ? GET_MODE_SIZE (GET_MODE (rtly))
1791 : MEM_SIZE (rtly) ? INTVAL (MEM_SIZE (rtly)) :
1792 -1);
1794 /* If we have an offset for either memref, it can update the values computed
1795 above. */
1796 if (MEM_OFFSET (x))
1797 offsetx += INTVAL (MEM_OFFSET (x)), sizex -= INTVAL (MEM_OFFSET (x));
1798 if (MEM_OFFSET (y))
1799 offsety += INTVAL (MEM_OFFSET (y)), sizey -= INTVAL (MEM_OFFSET (y));
1801 /* If a memref has both a size and an offset, we can use the smaller size.
1802 We can't do this is the offset isn't know because we must view this
1803 memref as being anywhere inside the DECL's MEM. */
1804 if (MEM_SIZE (x) && MEM_OFFSET (x))
1805 sizex = INTVAL (MEM_SIZE (x));
1806 if (MEM_SIZE (y) && MEM_OFFSET (y))
1807 sizey = INTVAL (MEM_SIZE (y));
1809 /* Put the values of the memref with the lower offset in X's values. */
1810 if (offsetx > offsety)
1812 tem = offsetx, offsetx = offsety, offsety = tem;
1813 tem = sizex, sizex = sizey, sizey = tem;
1816 /* If we don't know the size of the lower-offset value, we can't tell
1817 if they conflict. Otherwise, we do the test. */
1818 return sizex >= 0 && offsety > offsetx + sizex;
1821 /* True dependence: X is read after store in MEM takes place. */
1824 true_dependence (mem, mem_mode, x, varies)
1825 rtx mem;
1826 enum machine_mode mem_mode;
1827 rtx x;
1828 int (*varies) PARAMS ((rtx, int));
1830 rtx x_addr, mem_addr;
1831 rtx base;
1833 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1834 return 1;
1836 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1837 return 0;
1839 /* Unchanging memory can't conflict with non-unchanging memory.
1840 A non-unchanging read can conflict with a non-unchanging write.
1841 An unchanging read can conflict with an unchanging write since
1842 there may be a single store to this address to initialize it.
1843 Note that an unchanging store can conflict with a non-unchanging read
1844 since we have to make conservative assumptions when we have a
1845 record with readonly fields and we are copying the whole thing.
1846 Just fall through to the code below to resolve potential conflicts.
1847 This won't handle all cases optimally, but the possible performance
1848 loss should be negligible. */
1849 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1850 return 0;
1852 if (nonoverlapping_memrefs_p (mem, x))
1853 return 0;
1855 if (mem_mode == VOIDmode)
1856 mem_mode = GET_MODE (mem);
1858 x_addr = get_addr (XEXP (x, 0));
1859 mem_addr = get_addr (XEXP (mem, 0));
1861 base = find_base_term (x_addr);
1862 if (base && (GET_CODE (base) == LABEL_REF
1863 || (GET_CODE (base) == SYMBOL_REF
1864 && CONSTANT_POOL_ADDRESS_P (base))))
1865 return 0;
1867 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1868 return 0;
1870 x_addr = canon_rtx (x_addr);
1871 mem_addr = canon_rtx (mem_addr);
1873 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1874 SIZE_FOR_MODE (x), x_addr, 0))
1875 return 0;
1877 if (aliases_everything_p (x))
1878 return 1;
1880 /* We cannot use aliases_everything_p to test MEM, since we must look
1881 at MEM_MODE, rather than GET_MODE (MEM). */
1882 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1883 return 1;
1885 /* In true_dependence we also allow BLKmode to alias anything. Why
1886 don't we do this in anti_dependence and output_dependence? */
1887 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1888 return 1;
1890 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1891 varies);
1894 /* Canonical true dependence: X is read after store in MEM takes place.
1895 Variant of true_dependence which assumes MEM has already been
1896 canonicalized (hence we no longer do that here).
1897 The mem_addr argument has been added, since true_dependence computed
1898 this value prior to canonicalizing. */
1901 canon_true_dependence (mem, mem_mode, mem_addr, x, varies)
1902 rtx mem, mem_addr, x;
1903 enum machine_mode mem_mode;
1904 int (*varies) PARAMS ((rtx, int));
1906 rtx x_addr;
1908 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1909 return 1;
1911 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1912 return 0;
1914 /* If X is an unchanging read, then it can't possibly conflict with any
1915 non-unchanging store. It may conflict with an unchanging write though,
1916 because there may be a single store to this address to initialize it.
1917 Just fall through to the code below to resolve the case where we have
1918 both an unchanging read and an unchanging write. This won't handle all
1919 cases optimally, but the possible performance loss should be
1920 negligible. */
1921 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1922 return 0;
1924 if (nonoverlapping_memrefs_p (x, mem))
1925 return 0;
1927 x_addr = get_addr (XEXP (x, 0));
1929 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1930 return 0;
1932 x_addr = canon_rtx (x_addr);
1933 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1934 SIZE_FOR_MODE (x), x_addr, 0))
1935 return 0;
1937 if (aliases_everything_p (x))
1938 return 1;
1940 /* We cannot use aliases_everything_p to test MEM, since we must look
1941 at MEM_MODE, rather than GET_MODE (MEM). */
1942 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1943 return 1;
1945 /* In true_dependence we also allow BLKmode to alias anything. Why
1946 don't we do this in anti_dependence and output_dependence? */
1947 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1948 return 1;
1950 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1951 varies);
1954 /* Returns non-zero if a write to X might alias a previous read from
1955 (or, if WRITEP is non-zero, a write to) MEM. */
1957 static int
1958 write_dependence_p (mem, x, writep)
1959 rtx mem;
1960 rtx x;
1961 int writep;
1963 rtx x_addr, mem_addr;
1964 rtx fixed_scalar;
1965 rtx base;
1967 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1968 return 1;
1970 if (DIFFERENT_ALIAS_SETS_P (x, mem))
1971 return 0;
1973 /* Unchanging memory can't conflict with non-unchanging memory. */
1974 if (RTX_UNCHANGING_P (x) != RTX_UNCHANGING_P (mem))
1975 return 0;
1977 /* If MEM is an unchanging read, then it can't possibly conflict with
1978 the store to X, because there is at most one store to MEM, and it must
1979 have occurred somewhere before MEM. */
1980 if (! writep && RTX_UNCHANGING_P (mem))
1981 return 0;
1983 if (nonoverlapping_memrefs_p (x, mem))
1984 return 0;
1986 x_addr = get_addr (XEXP (x, 0));
1987 mem_addr = get_addr (XEXP (mem, 0));
1989 if (! writep)
1991 base = find_base_term (mem_addr);
1992 if (base && (GET_CODE (base) == LABEL_REF
1993 || (GET_CODE (base) == SYMBOL_REF
1994 && CONSTANT_POOL_ADDRESS_P (base))))
1995 return 0;
1998 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
1999 GET_MODE (mem)))
2000 return 0;
2002 x_addr = canon_rtx (x_addr);
2003 mem_addr = canon_rtx (mem_addr);
2005 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2006 SIZE_FOR_MODE (x), x_addr, 0))
2007 return 0;
2009 fixed_scalar
2010 = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2011 rtx_addr_varies_p);
2013 return (!(fixed_scalar == mem && !aliases_everything_p (x))
2014 && !(fixed_scalar == x && !aliases_everything_p (mem)));
2017 /* Anti dependence: X is written after read in MEM takes place. */
2020 anti_dependence (mem, x)
2021 rtx mem;
2022 rtx x;
2024 return write_dependence_p (mem, x, /*writep=*/0);
2027 /* Output dependence: X is written after store in MEM takes place. */
2030 output_dependence (mem, x)
2031 rtx mem;
2032 rtx x;
2034 return write_dependence_p (mem, x, /*writep=*/1);
2037 /* Returns non-zero if X mentions something which is not
2038 local to the function and is not constant. */
2040 static int
2041 nonlocal_mentioned_p (x)
2042 rtx x;
2044 rtx base;
2045 RTX_CODE code;
2046 int regno;
2048 code = GET_CODE (x);
2050 if (GET_RTX_CLASS (code) == 'i')
2052 /* Constant functions can be constant if they don't use
2053 scratch memory used to mark function w/o side effects. */
2054 if (code == CALL_INSN && CONST_OR_PURE_CALL_P (x))
2056 x = CALL_INSN_FUNCTION_USAGE (x);
2057 if (x == 0)
2058 return 0;
2060 else
2061 x = PATTERN (x);
2062 code = GET_CODE (x);
2065 switch (code)
2067 case SUBREG:
2068 if (GET_CODE (SUBREG_REG (x)) == REG)
2070 /* Global registers are not local. */
2071 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
2072 && global_regs[subreg_regno (x)])
2073 return 1;
2074 return 0;
2076 break;
2078 case REG:
2079 regno = REGNO (x);
2080 /* Global registers are not local. */
2081 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2082 return 1;
2083 return 0;
2085 case SCRATCH:
2086 case PC:
2087 case CC0:
2088 case CONST_INT:
2089 case CONST_DOUBLE:
2090 case CONST:
2091 case LABEL_REF:
2092 return 0;
2094 case SYMBOL_REF:
2095 /* Constants in the function's constants pool are constant. */
2096 if (CONSTANT_POOL_ADDRESS_P (x))
2097 return 0;
2098 return 1;
2100 case CALL:
2101 /* Non-constant calls and recursion are not local. */
2102 return 1;
2104 case MEM:
2105 /* Be overly conservative and consider any volatile memory
2106 reference as not local. */
2107 if (MEM_VOLATILE_P (x))
2108 return 1;
2109 base = find_base_term (XEXP (x, 0));
2110 if (base)
2112 /* A Pmode ADDRESS could be a reference via the structure value
2113 address or static chain. Such memory references are nonlocal.
2115 Thus, we have to examine the contents of the ADDRESS to find
2116 out if this is a local reference or not. */
2117 if (GET_CODE (base) == ADDRESS
2118 && GET_MODE (base) == Pmode
2119 && (XEXP (base, 0) == stack_pointer_rtx
2120 || XEXP (base, 0) == arg_pointer_rtx
2121 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2122 || XEXP (base, 0) == hard_frame_pointer_rtx
2123 #endif
2124 || XEXP (base, 0) == frame_pointer_rtx))
2125 return 0;
2126 /* Constants in the function's constant pool are constant. */
2127 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
2128 return 0;
2130 return 1;
2132 case UNSPEC_VOLATILE:
2133 case ASM_INPUT:
2134 return 1;
2136 case ASM_OPERANDS:
2137 if (MEM_VOLATILE_P (x))
2138 return 1;
2140 /* FALLTHROUGH */
2142 default:
2143 break;
2146 /* Recursively scan the operands of this expression. */
2149 const char *fmt = GET_RTX_FORMAT (code);
2150 int i;
2152 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2154 if (fmt[i] == 'e' && XEXP (x, i))
2156 if (nonlocal_mentioned_p (XEXP (x, i)))
2157 return 1;
2159 else if (fmt[i] == 'E')
2161 int j;
2162 for (j = 0; j < XVECLEN (x, i); j++)
2163 if (nonlocal_mentioned_p (XVECEXP (x, i, j)))
2164 return 1;
2169 return 0;
2172 /* Mark the function if it is constant. */
2174 void
2175 mark_constant_function ()
2177 rtx insn;
2178 int nonlocal_mentioned;
2180 if (TREE_PUBLIC (current_function_decl)
2181 || TREE_READONLY (current_function_decl)
2182 || DECL_IS_PURE (current_function_decl)
2183 || TREE_THIS_VOLATILE (current_function_decl)
2184 || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode)
2185 return;
2187 /* A loop might not return which counts as a side effect. */
2188 if (mark_dfs_back_edges ())
2189 return;
2191 nonlocal_mentioned = 0;
2193 init_alias_analysis ();
2195 /* Determine if this is a constant function. */
2197 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2198 if (INSN_P (insn) && nonlocal_mentioned_p (insn))
2200 nonlocal_mentioned = 1;
2201 break;
2204 end_alias_analysis ();
2206 /* Mark the function. */
2208 if (! nonlocal_mentioned)
2209 TREE_READONLY (current_function_decl) = 1;
2213 static HARD_REG_SET argument_registers;
2215 void
2216 init_alias_once ()
2218 int i;
2220 #ifndef OUTGOING_REGNO
2221 #define OUTGOING_REGNO(N) N
2222 #endif
2223 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2224 /* Check whether this register can hold an incoming pointer
2225 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2226 numbers, so translate if necessary due to register windows. */
2227 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2228 && HARD_REGNO_MODE_OK (i, Pmode))
2229 SET_HARD_REG_BIT (argument_registers, i);
2231 alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
2234 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2235 array. */
2237 void
2238 init_alias_analysis ()
2240 int maxreg = max_reg_num ();
2241 int changed, pass;
2242 int i;
2243 unsigned int ui;
2244 rtx insn;
2246 reg_known_value_size = maxreg;
2248 reg_known_value
2249 = (rtx *) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (rtx))
2250 - FIRST_PSEUDO_REGISTER;
2251 reg_known_equiv_p
2252 = (char*) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (char))
2253 - FIRST_PSEUDO_REGISTER;
2255 /* Overallocate reg_base_value to allow some growth during loop
2256 optimization. Loop unrolling can create a large number of
2257 registers. */
2258 reg_base_value_size = maxreg * 2;
2259 reg_base_value = (rtx *) xcalloc (reg_base_value_size, sizeof (rtx));
2260 ggc_add_rtx_root (reg_base_value, reg_base_value_size);
2262 new_reg_base_value = (rtx *) xmalloc (reg_base_value_size * sizeof (rtx));
2263 reg_seen = (char *) xmalloc (reg_base_value_size);
2264 if (! reload_completed && flag_unroll_loops)
2266 /* ??? Why are we realloc'ing if we're just going to zero it? */
2267 alias_invariant = (rtx *)xrealloc (alias_invariant,
2268 reg_base_value_size * sizeof (rtx));
2269 memset ((char *)alias_invariant, 0, reg_base_value_size * sizeof (rtx));
2272 /* The basic idea is that each pass through this loop will use the
2273 "constant" information from the previous pass to propagate alias
2274 information through another level of assignments.
2276 This could get expensive if the assignment chains are long. Maybe
2277 we should throttle the number of iterations, possibly based on
2278 the optimization level or flag_expensive_optimizations.
2280 We could propagate more information in the first pass by making use
2281 of REG_N_SETS to determine immediately that the alias information
2282 for a pseudo is "constant".
2284 A program with an uninitialized variable can cause an infinite loop
2285 here. Instead of doing a full dataflow analysis to detect such problems
2286 we just cap the number of iterations for the loop.
2288 The state of the arrays for the set chain in question does not matter
2289 since the program has undefined behavior. */
2291 pass = 0;
2294 /* Assume nothing will change this iteration of the loop. */
2295 changed = 0;
2297 /* We want to assign the same IDs each iteration of this loop, so
2298 start counting from zero each iteration of the loop. */
2299 unique_id = 0;
2301 /* We're at the start of the function each iteration through the
2302 loop, so we're copying arguments. */
2303 copying_arguments = 1;
2305 /* Wipe the potential alias information clean for this pass. */
2306 memset ((char *) new_reg_base_value, 0, reg_base_value_size * sizeof (rtx));
2308 /* Wipe the reg_seen array clean. */
2309 memset ((char *) reg_seen, 0, reg_base_value_size);
2311 /* Mark all hard registers which may contain an address.
2312 The stack, frame and argument pointers may contain an address.
2313 An argument register which can hold a Pmode value may contain
2314 an address even if it is not in BASE_REGS.
2316 The address expression is VOIDmode for an argument and
2317 Pmode for other registers. */
2319 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2320 if (TEST_HARD_REG_BIT (argument_registers, i))
2321 new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
2322 gen_rtx_REG (Pmode, i));
2324 new_reg_base_value[STACK_POINTER_REGNUM]
2325 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
2326 new_reg_base_value[ARG_POINTER_REGNUM]
2327 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
2328 new_reg_base_value[FRAME_POINTER_REGNUM]
2329 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
2330 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2331 new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2332 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
2333 #endif
2335 /* Walk the insns adding values to the new_reg_base_value array. */
2336 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2338 if (INSN_P (insn))
2340 rtx note, set;
2342 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2343 /* The prologue/epilogue insns are not threaded onto the
2344 insn chain until after reload has completed. Thus,
2345 there is no sense wasting time checking if INSN is in
2346 the prologue/epilogue until after reload has completed. */
2347 if (reload_completed
2348 && prologue_epilogue_contains (insn))
2349 continue;
2350 #endif
2352 /* If this insn has a noalias note, process it, Otherwise,
2353 scan for sets. A simple set will have no side effects
2354 which could change the base value of any other register. */
2356 if (GET_CODE (PATTERN (insn)) == SET
2357 && REG_NOTES (insn) != 0
2358 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2359 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2360 else
2361 note_stores (PATTERN (insn), record_set, NULL);
2363 set = single_set (insn);
2365 if (set != 0
2366 && GET_CODE (SET_DEST (set)) == REG
2367 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2369 unsigned int regno = REGNO (SET_DEST (set));
2370 rtx src = SET_SRC (set);
2372 if (REG_NOTES (insn) != 0
2373 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2374 && REG_N_SETS (regno) == 1)
2375 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
2376 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2377 && ! rtx_varies_p (XEXP (note, 0), 1)
2378 && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
2380 reg_known_value[regno] = XEXP (note, 0);
2381 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
2383 else if (REG_N_SETS (regno) == 1
2384 && GET_CODE (src) == PLUS
2385 && GET_CODE (XEXP (src, 0)) == REG
2386 && REGNO (XEXP (src, 0)) >= FIRST_PSEUDO_REGISTER
2387 && (reg_known_value[REGNO (XEXP (src, 0))])
2388 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2390 rtx op0 = XEXP (src, 0);
2391 op0 = reg_known_value[REGNO (op0)];
2392 reg_known_value[regno]
2393 = plus_constant (op0, INTVAL (XEXP (src, 1)));
2394 reg_known_equiv_p[regno] = 0;
2396 else if (REG_N_SETS (regno) == 1
2397 && ! rtx_varies_p (src, 1))
2399 reg_known_value[regno] = src;
2400 reg_known_equiv_p[regno] = 0;
2404 else if (GET_CODE (insn) == NOTE
2405 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
2406 copying_arguments = 0;
2409 /* Now propagate values from new_reg_base_value to reg_base_value. */
2410 for (ui = 0; ui < reg_base_value_size; ui++)
2412 if (new_reg_base_value[ui]
2413 && new_reg_base_value[ui] != reg_base_value[ui]
2414 && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
2416 reg_base_value[ui] = new_reg_base_value[ui];
2417 changed = 1;
2421 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
2423 /* Fill in the remaining entries. */
2424 for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
2425 if (reg_known_value[i] == 0)
2426 reg_known_value[i] = regno_reg_rtx[i];
2428 /* Simplify the reg_base_value array so that no register refers to
2429 another register, except to special registers indirectly through
2430 ADDRESS expressions.
2432 In theory this loop can take as long as O(registers^2), but unless
2433 there are very long dependency chains it will run in close to linear
2434 time.
2436 This loop may not be needed any longer now that the main loop does
2437 a better job at propagating alias information. */
2438 pass = 0;
2441 changed = 0;
2442 pass++;
2443 for (ui = 0; ui < reg_base_value_size; ui++)
2445 rtx base = reg_base_value[ui];
2446 if (base && GET_CODE (base) == REG)
2448 unsigned int base_regno = REGNO (base);
2449 if (base_regno == ui) /* register set from itself */
2450 reg_base_value[ui] = 0;
2451 else
2452 reg_base_value[ui] = reg_base_value[base_regno];
2453 changed = 1;
2457 while (changed && pass < MAX_ALIAS_LOOP_PASSES);
2459 /* Clean up. */
2460 free (new_reg_base_value);
2461 new_reg_base_value = 0;
2462 free (reg_seen);
2463 reg_seen = 0;
2466 void
2467 end_alias_analysis ()
2469 free (reg_known_value + FIRST_PSEUDO_REGISTER);
2470 reg_known_value = 0;
2471 reg_known_value_size = 0;
2472 free (reg_known_equiv_p + FIRST_PSEUDO_REGISTER);
2473 reg_known_equiv_p = 0;
2474 if (reg_base_value)
2476 ggc_del_root (reg_base_value);
2477 free (reg_base_value);
2478 reg_base_value = 0;
2480 reg_base_value_size = 0;
2481 if (alias_invariant)
2483 free (alias_invariant);
2484 alias_invariant = 0;