]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/alias.c
Merge basic-improvements-branch to trunk
[thirdparty/gcc.git] / gcc / alias.c
CommitLineData
ea0cb7ae 1/* Alias analysis for GNU C
25763d80 2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
ea0cb7ae 3 Contributed by John Carr (jfc@mit.edu).
4
f12b58b3 5This file is part of GCC.
ea0cb7ae 6
f12b58b3 7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
ea0cb7ae 11
f12b58b3 12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
ea0cb7ae 16
17You should have received a copy of the GNU General Public License
f12b58b3 18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
ea0cb7ae 21
22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
25#include "tm.h"
ea0cb7ae 26#include "rtl.h"
142131ed 27#include "tree.h"
7953c610 28#include "tm_p.h"
0a893c29 29#include "function.h"
ea0cb7ae 30#include "expr.h"
31#include "regs.h"
32#include "hard-reg-set.h"
080f162d 33#include "basic-block.h"
ea0cb7ae 34#include "flags.h"
a068b2a9 35#include "output.h"
ce1fd7fc 36#include "toplev.h"
1617c276 37#include "cselib.h"
a5b1863e 38#include "splay-tree.h"
c2e94b25 39#include "ggc.h"
b0278d39 40#include "langhooks.h"
4f8e9d5c 41#include "target.h"
a5b1863e 42
43/* The alias sets assigned to MEMs assist the back-end in determining
44 which MEMs can alias which other MEMs. In general, two MEMs in
5badbbc0 45 different alias sets cannot alias each other, with one important
46 exception. Consider something like:
a5b1863e 47
48 struct S {int i; double d; };
49
50 a store to an `S' can alias something of either type `int' or type
51 `double'. (However, a store to an `int' cannot alias a `double'
52 and vice versa.) We indicate this via a tree structure that looks
53 like:
54 struct S
55 / \
56 / \
57 |/_ _\|
58 int double
59
5badbbc0 60 (The arrows are directed and point downwards.)
61 In this situation we say the alias set for `struct S' is the
62 `superset' and that those for `int' and `double' are `subsets'.
63
f7c44134 64 To see whether two alias sets can point to the same memory, we must
65 see if either alias set is a subset of the other. We need not trace
b9fe4edd 66 past immediate descendents, however, since we propagate all
f7c44134 67 grandchildren up one level.
a5b1863e 68
69 Alias set zero is implicitly a superset of all other alias sets.
70 However, this is no actual entry for alias set zero. It is an
71 error to attempt to explicitly construct a subset of zero. */
72
083a2b5e 73typedef struct alias_set_entry
74{
a5b1863e 75 /* The alias set number, as stored in MEM_ALIAS_SET. */
f7c44134 76 HOST_WIDE_INT alias_set;
a5b1863e 77
78 /* The children of the alias set. These are not just the immediate
b9fe4edd 79 children, but, in fact, all descendents. So, if we have:
a5b1863e 80
9342ee68 81 struct T { struct S s; float f; }
a5b1863e 82
83 continuing our example above, the children here will be all of
84 `int', `double', `float', and `struct S'. */
85 splay_tree children;
851dfbff 86
87 /* Nonzero if would have a child of zero: this effectively makes this
88 alias set the same as alias set zero. */
89 int has_zero_child;
083a2b5e 90} *alias_set_entry;
ea0cb7ae 91
6bcfea9e 92static int rtx_equal_for_memref_p PARAMS ((rtx, rtx));
93static rtx find_symbolic_term PARAMS ((rtx));
8e802be9 94rtx get_addr PARAMS ((rtx));
6bcfea9e 95static int memrefs_conflict_p PARAMS ((int, rtx, int, rtx,
96 HOST_WIDE_INT));
97static void record_set PARAMS ((rtx, rtx, void *));
98static rtx find_base_term PARAMS ((rtx));
99static int base_alias_check PARAMS ((rtx, rtx, enum machine_mode,
100 enum machine_mode));
101static rtx find_base_value PARAMS ((rtx));
102static int mems_in_disjoint_alias_sets_p PARAMS ((rtx, rtx));
103static int insert_subset_children PARAMS ((splay_tree_node, void*));
96216d37 104static tree find_base_decl PARAMS ((tree));
f7c44134 105static alias_set_entry get_alias_set_entry PARAMS ((HOST_WIDE_INT));
1617c276 106static rtx fixed_scalar_and_varying_struct_p PARAMS ((rtx, rtx, rtx, rtx,
ea087693 107 int (*) (rtx, int)));
6bcfea9e 108static int aliases_everything_p PARAMS ((rtx));
b10dbbca 109static bool nonoverlapping_component_refs_p PARAMS ((tree, tree));
110static tree decl_for_component_ref PARAMS ((tree));
111static rtx adjust_offset_for_component_ref PARAMS ((tree, rtx));
ddb3d16f 112static int nonoverlapping_memrefs_p PARAMS ((rtx, rtx));
6bcfea9e 113static int write_dependence_p PARAMS ((rtx, rtx, int));
c352cbc7 114
115static int nonlocal_mentioned_p_1 PARAMS ((rtx *, void *));
1addbe73 116static int nonlocal_mentioned_p PARAMS ((rtx));
c352cbc7 117static int nonlocal_referenced_p_1 PARAMS ((rtx *, void *));
118static int nonlocal_referenced_p PARAMS ((rtx));
119static int nonlocal_set_p_1 PARAMS ((rtx *, void *));
120static int nonlocal_set_p PARAMS ((rtx));
ea0cb7ae 121
122/* Set up all info needed to perform alias analysis on memory references. */
123
083a2b5e 124/* Returns the size in bytes of the mode of X. */
ea0cb7ae 125#define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
126
b5ba9f3a 127/* Returns nonzero if MEM1 and MEM2 do not alias because they are in
a068b2a9 128 different alias sets. We ignore alias sets in functions making use
129 of variable arguments because the va_arg macros on some systems are
130 not legal ANSI C. */
131#define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2) \
a5b1863e 132 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
b5ba9f3a 133
37b864db 134/* Cap the number of passes we make over the insns propagating alias
5badbbc0 135 information through set chains. 10 is a completely arbitrary choice. */
37b864db 136#define MAX_ALIAS_LOOP_PASSES 10
9342ee68 137
ea0cb7ae 138/* reg_base_value[N] gives an address to which register N is related.
139 If all sets after the first add or subtract to the current value
140 or otherwise modify it so it does not point to a different top level
141 object, reg_base_value[N] is equal to the address part of the source
e7c893ba 142 of the first set.
143
144 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
145 expressions represent certain special values: function arguments and
9342ee68 146 the stack, frame, and argument pointers.
3a935f06 147
148 The contents of an ADDRESS is not normally used, the mode of the
149 ADDRESS determines whether the ADDRESS is a function argument or some
150 other special value. Pointer equality, not rtx_equal_p, determines whether
151 two ADDRESS expressions refer to the same base address.
152
153 The only use of the contents of an ADDRESS is for determining if the
154 current function performs nonlocal memory memory references for the
155 purposes of marking the function as a constant function. */
e7c893ba 156
1f3233d1 157static GTY((length ("reg_base_value_size"))) rtx *reg_base_value;
c2e94b25 158static rtx *new_reg_base_value;
083a2b5e 159static unsigned int reg_base_value_size; /* size of reg_base_value array */
160
0c7f5242 161/* Static hunks of RTL used by the aliasing code; these are initialized
162 once per function to avoid unnecessary RTL allocations. */
163static GTY (()) rtx static_reg_base_value[FIRST_PSEUDO_REGISTER];
164
ea0cb7ae 165#define REG_BASE_VALUE(X) \
9db10305 166 (REGNO (X) < reg_base_value_size \
167 ? reg_base_value[REGNO (X)] : 0)
ea0cb7ae 168
1a6c3add 169/* Vector of known invariant relationships between registers. Set in
170 loop unrolling. Indexed by register number, if nonzero the value
171 is an expression describing this register in terms of another.
172
173 The length of this array is REG_BASE_VALUE_SIZE.
174
175 Because this array contains only pseudo registers it has no effect
176 after reload. */
177static rtx *alias_invariant;
178
73f5c1e3 179/* Vector indexed by N giving the initial (unchanging) value known for
180 pseudo-register N. This array is initialized in
181 init_alias_analysis, and does not change until end_alias_analysis
182 is called. */
ea0cb7ae 183rtx *reg_known_value;
184
185/* Indicates number of valid entries in reg_known_value. */
02e7a332 186static unsigned int reg_known_value_size;
ea0cb7ae 187
188/* Vector recording for each reg_known_value whether it is due to a
189 REG_EQUIV note. Future passes (viz., reload) may replace the
190 pseudo with the equivalent expression and so we account for the
5badbbc0 191 dependences that would be introduced if that happens.
192
193 The REG_EQUIV notes created in assign_parms may mention the arg
194 pointer, and there are explicit insns in the RTL that modify the
195 arg pointer. Thus we must ensure that such insns don't get
196 scheduled across each other because that would invalidate the
197 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
198 wrong, but solving the problem in the scheduler will likely give
199 better code, so we do it here. */
ea0cb7ae 200char *reg_known_equiv_p;
201
e7c893ba 202/* True when scanning insns from the start of the rtl to the
203 NOTE_INSN_FUNCTION_BEG note. */
e5be0cc6 204static bool copying_arguments;
ea0cb7ae 205
a5b1863e 206/* The splay-tree used to store the various alias set entries. */
a5b1863e 207static splay_tree alias_sets;
5badbbc0 208\f
a5b1863e 209/* Returns a pointer to the alias set entry for ALIAS_SET, if there is
210 such an entry, or NULL otherwise. */
211
212static alias_set_entry
213get_alias_set_entry (alias_set)
f7c44134 214 HOST_WIDE_INT alias_set;
a5b1863e 215{
083a2b5e 216 splay_tree_node sn
217 = splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
a5b1863e 218
083a2b5e 219 return sn != 0 ? ((alias_set_entry) sn->value) : 0;
a5b1863e 220}
221
5badbbc0 222/* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
223 the two MEMs cannot alias each other. */
a5b1863e 224
9342ee68 225static int
a5b1863e 226mems_in_disjoint_alias_sets_p (mem1, mem2)
227 rtx mem1;
228 rtx mem2;
229{
9342ee68 230#ifdef ENABLE_CHECKING
a5b1863e 231/* Perform a basic sanity check. Namely, that there are no alias sets
232 if we're not using strict aliasing. This helps to catch bugs
233 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
234 where a MEM is allocated in some way other than by the use of
235 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
236 use alias sets to indicate that spilled registers cannot alias each
237 other, we might need to remove this check. */
083a2b5e 238 if (! flag_strict_aliasing
239 && (MEM_ALIAS_SET (mem1) != 0 || MEM_ALIAS_SET (mem2) != 0))
a5b1863e 240 abort ();
241#endif
242
387bc205 243 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
244}
a5b1863e 245
387bc205 246/* Insert the NODE into the splay tree given by DATA. Used by
247 record_alias_subset via splay_tree_foreach. */
248
249static int
250insert_subset_children (node, data)
251 splay_tree_node node;
252 void *data;
253{
254 splay_tree_insert ((splay_tree) data, node->key, node->value);
255
256 return 0;
257}
258
259/* Return 1 if the two specified alias sets may conflict. */
260
261int
262alias_sets_conflict_p (set1, set2)
263 HOST_WIDE_INT set1, set2;
264{
265 alias_set_entry ase;
266
267 /* If have no alias set information for one of the operands, we have
268 to assume it can alias anything. */
269 if (set1 == 0 || set2 == 0
270 /* If the two alias sets are the same, they may alias. */
271 || set1 == set2)
272 return 1;
a5b1863e 273
f7c44134 274 /* See if the first alias set is a subset of the second. */
387bc205 275 ase = get_alias_set_entry (set1);
851dfbff 276 if (ase != 0
277 && (ase->has_zero_child
278 || splay_tree_lookup (ase->children,
387bc205 279 (splay_tree_key) set2)))
280 return 1;
a5b1863e 281
282 /* Now do the same, but with the alias sets reversed. */
387bc205 283 ase = get_alias_set_entry (set2);
851dfbff 284 if (ase != 0
285 && (ase->has_zero_child
286 || splay_tree_lookup (ase->children,
387bc205 287 (splay_tree_key) set1)))
288 return 1;
a5b1863e 289
387bc205 290 /* The two alias sets are distinct and neither one is the
a5b1863e 291 child of the other. Therefore, they cannot alias. */
387bc205 292 return 0;
a5b1863e 293}
387bc205 294\f
295/* Return 1 if TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE and has
296 has any readonly fields. If any of the fields have types that
297 contain readonly fields, return true as well. */
a5b1863e 298
387bc205 299int
300readonly_fields_p (type)
301 tree type;
a5b1863e 302{
387bc205 303 tree field;
304
305 if (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
306 && TREE_CODE (type) != QUAL_UNION_TYPE)
307 return 0;
308
309 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
310 if (TREE_CODE (field) == FIELD_DECL
311 && (TREE_READONLY (field)
312 || readonly_fields_p (TREE_TYPE (field))))
313 return 1;
a5b1863e 314
315 return 0;
316}
f7c44134 317\f
387bc205 318/* Return 1 if any MEM object of type T1 will always conflict (using the
319 dependency routines in this file) with any MEM object of type T2.
320 This is used when allocating temporary storage. If T1 and/or T2 are
321 NULL_TREE, it means we know nothing about the storage. */
322
323int
324objects_must_conflict_p (t1, t2)
325 tree t1, t2;
326{
218eb8d0 327 /* If neither has a type specified, we don't know if they'll conflict
328 because we may be using them to store objects of various types, for
329 example the argument and local variables areas of inlined functions. */
8f2ba9c9 330 if (t1 == 0 && t2 == 0)
218eb8d0 331 return 0;
332
bd881992 333 /* If one or the other has readonly fields or is readonly,
334 then they may not conflict. */
335 if ((t1 != 0 && readonly_fields_p (t1))
336 || (t2 != 0 && readonly_fields_p (t2))
25763d80 337 || (t1 != 0 && lang_hooks.honor_readonly && TYPE_READONLY (t1))
338 || (t2 != 0 && lang_hooks.honor_readonly && TYPE_READONLY (t2)))
bd881992 339 return 0;
340
387bc205 341 /* If they are the same type, they must conflict. */
342 if (t1 == t2
343 /* Likewise if both are volatile. */
344 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
345 return 1;
346
bd881992 347 /* If one is aggregate and the other is scalar then they may not
348 conflict. */
349 if ((t1 != 0 && AGGREGATE_TYPE_P (t1))
350 != (t2 != 0 && AGGREGATE_TYPE_P (t2)))
387bc205 351 return 0;
352
2c0e001b 353 /* Otherwise they conflict only if the alias sets conflict. */
387bc205 354 return alias_sets_conflict_p (t1 ? get_alias_set (t1) : 0,
355 t2 ? get_alias_set (t2) : 0);
356}
357\f
f7c44134 358/* T is an expression with pointer type. Find the DECL on which this
359 expression is based. (For example, in `a[i]' this would be `a'.)
360 If there is no such DECL, or a unique decl cannot be determined,
35a3065a 361 NULL_TREE is returned. */
f7c44134 362
363static tree
364find_base_decl (t)
365 tree t;
366{
367 tree d0, d1, d2;
368
369 if (t == 0 || t == error_mark_node || ! POINTER_TYPE_P (TREE_TYPE (t)))
370 return 0;
371
372 /* If this is a declaration, return it. */
373 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
374 return t;
375
376 /* Handle general expressions. It would be nice to deal with
377 COMPONENT_REFs here. If we could tell that `a' and `b' were the
378 same, then `a->f' and `b->f' are also the same. */
379 switch (TREE_CODE_CLASS (TREE_CODE (t)))
380 {
381 case '1':
382 return find_base_decl (TREE_OPERAND (t, 0));
383
384 case '2':
385 /* Return 0 if found in neither or both are the same. */
386 d0 = find_base_decl (TREE_OPERAND (t, 0));
387 d1 = find_base_decl (TREE_OPERAND (t, 1));
388 if (d0 == d1)
389 return d0;
390 else if (d0 == 0)
391 return d1;
392 else if (d1 == 0)
393 return d0;
394 else
395 return 0;
396
397 case '3':
398 d0 = find_base_decl (TREE_OPERAND (t, 0));
399 d1 = find_base_decl (TREE_OPERAND (t, 1));
f7c44134 400 d2 = find_base_decl (TREE_OPERAND (t, 2));
401
402 /* Set any nonzero values from the last, then from the first. */
403 if (d1 == 0) d1 = d2;
404 if (d0 == 0) d0 = d1;
405 if (d1 == 0) d1 = d0;
406 if (d2 == 0) d2 = d1;
407
408 /* At this point all are nonzero or all are zero. If all three are the
409 same, return it. Otherwise, return zero. */
410 return (d0 == d1 && d1 == d2) ? d0 : 0;
411
412 default:
413 return 0;
414 }
415}
416
bd32979f 417/* Return 1 if all the nested component references handled by
418 get_inner_reference in T are such that we can address the object in T. */
419
5cc193e7 420int
bd32979f 421can_address_p (t)
422 tree t;
423{
424 /* If we're at the end, it is vacuously addressable. */
425 if (! handled_component_p (t))
426 return 1;
427
428 /* Bitfields are never addressable. */
429 else if (TREE_CODE (t) == BIT_FIELD_REF)
430 return 0;
431
2a631e19 432 /* Fields are addressable unless they are marked as nonaddressable or
433 the containing type has alias set 0. */
bd32979f 434 else if (TREE_CODE (t) == COMPONENT_REF
435 && ! DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1))
2a631e19 436 && get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) != 0
bd32979f 437 && can_address_p (TREE_OPERAND (t, 0)))
438 return 1;
439
2a631e19 440 /* Likewise for arrays. */
ba04d9d5 441 else if ((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
bd32979f 442 && ! TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0)))
2a631e19 443 && get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) != 0
bd32979f 444 && can_address_p (TREE_OPERAND (t, 0)))
445 return 1;
446
447 return 0;
448}
449
f7c44134 450/* Return the alias set for T, which may be either a type or an
451 expression. Call language-specific routine for help, if needed. */
452
453HOST_WIDE_INT
454get_alias_set (t)
455 tree t;
456{
457 HOST_WIDE_INT set;
f7c44134 458
459 /* If we're not doing any alias analysis, just assume everything
460 aliases everything else. Also return 0 if this or its type is
461 an error. */
462 if (! flag_strict_aliasing || t == error_mark_node
463 || (! TYPE_P (t)
464 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
465 return 0;
466
467 /* We can be passed either an expression or a type. This and the
c3a9c149 468 language-specific routine may make mutually-recursive calls to each other
469 to figure out what to do. At each juncture, we see if this is a tree
470 that the language may need to handle specially. First handle things that
96216d37 471 aren't types. */
1607663f 472 if (! TYPE_P (t))
f7c44134 473 {
96216d37 474 tree inner = t;
475 tree placeholder_ptr = 0;
476
2a631e19 477 /* Remove any nops, then give the language a chance to do
478 something with this tree before we look at it. */
479 STRIP_NOPS (t);
480 set = (*lang_hooks.get_alias_set) (t);
481 if (set != -1)
482 return set;
483
96216d37 484 /* First see if the actual object referenced is an INDIRECT_REF from a
2a631e19 485 restrict-qualified pointer or a "void *". Replace
96216d37 486 PLACEHOLDER_EXPRs. */
2a631e19 487 while (TREE_CODE (inner) == PLACEHOLDER_EXPR
96216d37 488 || handled_component_p (inner))
489 {
490 if (TREE_CODE (inner) == PLACEHOLDER_EXPR)
491 inner = find_placeholder (inner, &placeholder_ptr);
492 else
493 inner = TREE_OPERAND (inner, 0);
2a631e19 494
495 STRIP_NOPS (inner);
96216d37 496 }
497
498 /* Check for accesses through restrict-qualified pointers. */
499 if (TREE_CODE (inner) == INDIRECT_REF)
500 {
501 tree decl = find_base_decl (TREE_OPERAND (inner, 0));
502
503 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
504 {
4e69477b 505 /* If we haven't computed the actual alias set, do it now. */
96216d37 506 if (DECL_POINTER_ALIAS_SET (decl) == -2)
507 {
508 /* No two restricted pointers can point at the same thing.
509 However, a restricted pointer can point at the same thing
510 as an unrestricted pointer, if that unrestricted pointer
511 is based on the restricted pointer. So, we make the
512 alias set for the restricted pointer a subset of the
513 alias set for the type pointed to by the type of the
514 decl. */
515 HOST_WIDE_INT pointed_to_alias_set
516 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
517
518 if (pointed_to_alias_set == 0)
519 /* It's not legal to make a subset of alias set zero. */
520 ;
521 else
522 {
523 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
9342ee68 524 record_alias_subset (pointed_to_alias_set,
525 DECL_POINTER_ALIAS_SET (decl));
96216d37 526 }
527 }
528
529 /* We use the alias set indicated in the declaration. */
530 return DECL_POINTER_ALIAS_SET (decl);
531 }
532
533 /* If we have an INDIRECT_REF via a void pointer, we don't
534 know anything about what that might alias. */
535 else if (TREE_CODE (TREE_TYPE (inner)) == VOID_TYPE)
536 return 0;
537 }
538
539 /* Otherwise, pick up the outermost object that we could have a pointer
540 to, processing conversion and PLACEHOLDER_EXPR as above. */
541 placeholder_ptr = 0;
2a631e19 542 while (TREE_CODE (t) == PLACEHOLDER_EXPR
c3a9c149 543 || (handled_component_p (t) && ! can_address_p (t)))
544 {
c3a9c149 545 if (TREE_CODE (t) == PLACEHOLDER_EXPR)
96216d37 546 t = find_placeholder (t, &placeholder_ptr);
c3a9c149 547 else
548 t = TREE_OPERAND (t, 0);
1607663f 549
2a631e19 550 STRIP_NOPS (t);
551 }
1607663f 552
96216d37 553 /* If we've already determined the alias set for a decl, just return
554 it. This is necessary for C++ anonymous unions, whose component
555 variables don't look like union members (boo!). */
afce1f57 556 if (TREE_CODE (t) == VAR_DECL
557 && DECL_RTL_SET_P (t) && GET_CODE (DECL_RTL (t)) == MEM)
558 return MEM_ALIAS_SET (DECL_RTL (t));
559
1607663f 560 /* Now all we care about is the type. */
561 t = TREE_TYPE (t);
f7c44134 562 }
563
f7c44134 564 /* Variant qualifiers don't affect the alias set, so get the main
565 variant. If this is a type with a known alias set, return it. */
566 t = TYPE_MAIN_VARIANT (t);
96216d37 567 if (TYPE_ALIAS_SET_KNOWN_P (t))
f7c44134 568 return TYPE_ALIAS_SET (t);
569
570 /* See if the language has special handling for this type. */
2a631e19 571 set = (*lang_hooks.get_alias_set) (t);
572 if (set != -1)
96216d37 573 return set;
851dfbff 574
f7c44134 575 /* There are no objects of FUNCTION_TYPE, so there's no point in
576 using up an alias set for them. (There are, of course, pointers
577 and references to functions, but that's different.) */
578 else if (TREE_CODE (t) == FUNCTION_TYPE)
579 set = 0;
9fcc3e54 580
581 /* Unless the language specifies otherwise, let vector types alias
582 their components. This avoids some nasty type punning issues in
583 normal usage. And indeed lets vectors be treated more like an
584 array slice. */
585 else if (TREE_CODE (t) == VECTOR_TYPE)
586 set = get_alias_set (TREE_TYPE (t));
587
f7c44134 588 else
589 /* Otherwise make a new alias set for this type. */
590 set = new_alias_set ();
591
592 TYPE_ALIAS_SET (t) = set;
851dfbff 593
594 /* If this is an aggregate type, we must record any component aliasing
595 information. */
680210f2 596 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
851dfbff 597 record_component_aliases (t);
598
f7c44134 599 return set;
600}
601
602/* Return a brand-new alias set. */
603
604HOST_WIDE_INT
605new_alias_set ()
606{
607 static HOST_WIDE_INT last_alias_set;
608
609 if (flag_strict_aliasing)
610 return ++last_alias_set;
611 else
612 return 0;
613}
a5b1863e 614
615/* Indicate that things in SUBSET can alias things in SUPERSET, but
616 not vice versa. For example, in C, a store to an `int' can alias a
617 structure containing an `int', but not vice versa. Here, the
618 structure would be the SUPERSET and `int' the SUBSET. This
9342ee68 619 function should be called only once per SUPERSET/SUBSET pair.
a5b1863e 620
621 It is illegal for SUPERSET to be zero; everything is implicitly a
622 subset of alias set zero. */
623
624void
625record_alias_subset (superset, subset)
f7c44134 626 HOST_WIDE_INT superset;
627 HOST_WIDE_INT subset;
a5b1863e 628{
629 alias_set_entry superset_entry;
630 alias_set_entry subset_entry;
631
c3a9c149 632 /* It is possible in complex type situations for both sets to be the same,
633 in which case we can ignore this operation. */
634 if (superset == subset)
635 return;
636
a5b1863e 637 if (superset == 0)
638 abort ();
639
640 superset_entry = get_alias_set_entry (superset);
9342ee68 641 if (superset_entry == 0)
a5b1863e 642 {
643 /* Create an entry for the SUPERSET, so that we have a place to
644 attach the SUBSET. */
083a2b5e 645 superset_entry
646 = (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
a5b1863e 647 superset_entry->alias_set = superset;
9342ee68 648 superset_entry->children
d1264606 649 = splay_tree_new (splay_tree_compare_ints, 0, 0);
f2e5de80 650 superset_entry->has_zero_child = 0;
083a2b5e 651 splay_tree_insert (alias_sets, (splay_tree_key) superset,
a5b1863e 652 (splay_tree_value) superset_entry);
a5b1863e 653 }
654
851dfbff 655 if (subset == 0)
656 superset_entry->has_zero_child = 1;
657 else
658 {
659 subset_entry = get_alias_set_entry (subset);
660 /* If there is an entry for the subset, enter all of its children
661 (if they are not already present) as children of the SUPERSET. */
9342ee68 662 if (subset_entry)
851dfbff 663 {
664 if (subset_entry->has_zero_child)
665 superset_entry->has_zero_child = 1;
083a2b5e 666
851dfbff 667 splay_tree_foreach (subset_entry->children, insert_subset_children,
668 superset_entry->children);
669 }
a5b1863e 670
851dfbff 671 /* Enter the SUBSET itself as a child of the SUPERSET. */
9342ee68 672 splay_tree_insert (superset_entry->children,
851dfbff 673 (splay_tree_key) subset, 0);
674 }
a5b1863e 675}
676
9501a338 677/* Record that component types of TYPE, if any, are part of that type for
678 aliasing purposes. For record types, we only record component types
679 for fields that are marked addressable. For array types, we always
680 record the component types, so the front end should not call this
681 function if the individual component aren't addressable. */
682
683void
684record_component_aliases (type)
685 tree type;
686{
f7c44134 687 HOST_WIDE_INT superset = get_alias_set (type);
9501a338 688 tree field;
689
690 if (superset == 0)
691 return;
692
693 switch (TREE_CODE (type))
694 {
695 case ARRAY_TYPE:
851dfbff 696 if (! TYPE_NONALIASED_COMPONENT (type))
697 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
9501a338 698 break;
699
700 case RECORD_TYPE:
701 case UNION_TYPE:
702 case QUAL_UNION_TYPE:
c5d44e2f 703 /* Recursively record aliases for the base classes, if there are any */
704 if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
9342ee68 705 {
706 int i;
707 for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)); i++)
708 {
709 tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
710 record_alias_subset (superset,
c5d44e2f 711 get_alias_set (BINFO_TYPE (binfo)));
9342ee68 712 }
713 }
9501a338 714 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
e7bfabe2 715 if (TREE_CODE (field) == FIELD_DECL && ! DECL_NONADDRESSABLE_P (field))
851dfbff 716 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
9501a338 717 break;
718
680210f2 719 case COMPLEX_TYPE:
720 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
721 break;
722
9501a338 723 default:
724 break;
725 }
726}
727
f7c44134 728/* Allocate an alias set for use in storing and reading from the varargs
729 spill area. */
730
731HOST_WIDE_INT
732get_varargs_alias_set ()
733{
734 static HOST_WIDE_INT set = -1;
735
736 if (set == -1)
737 set = new_alias_set ();
738
739 return set;
740}
741
742/* Likewise, but used for the fixed portions of the frame, e.g., register
743 save areas. */
744
745HOST_WIDE_INT
746get_frame_alias_set ()
747{
748 static HOST_WIDE_INT set = -1;
749
750 if (set == -1)
751 set = new_alias_set ();
752
753 return set;
754}
755
e7c893ba 756/* Inside SRC, the source of a SET, find a base address. */
757
ea0cb7ae 758static rtx
759find_base_value (src)
19cb6b50 760 rtx src;
ea0cb7ae 761{
970c0c06 762 unsigned int regno;
d01410d3 763
ea0cb7ae 764 switch (GET_CODE (src))
765 {
766 case SYMBOL_REF:
767 case LABEL_REF:
768 return src;
769
770 case REG:
9db10305 771 regno = REGNO (src);
083a2b5e 772 /* At the start of a function, argument registers have known base
e7c893ba 773 values which may be lost later. Returning an ADDRESS
774 expression here allows optimization based on argument values
775 even when the argument registers are used for other purposes. */
970c0c06 776 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
777 return new_reg_base_value[regno];
1a538f67 778
ebd13eec 779 /* If a pseudo has a known base value, return it. Do not do this
9464d436 780 for non-fixed hard regs since it can result in a circular
781 dependency chain for registers which have values at function entry.
ebd13eec 782
783 The test above is not sufficient because the scheduler may move
784 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
9464d436 785 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
e5be0cc6 786 && regno < reg_base_value_size)
787 {
788 /* If we're inside init_alias_analysis, use new_reg_base_value
789 to reduce the number of relaxation iterations. */
790 if (new_reg_base_value && new_reg_base_value[regno])
791 return new_reg_base_value[regno];
792
793 if (reg_base_value[regno])
794 return reg_base_value[regno];
795 }
1a538f67 796
ea0cb7ae 797 return src;
798
799 case MEM:
800 /* Check for an argument passed in memory. Only record in the
801 copying-arguments block; it is too hard to track changes
802 otherwise. */
803 if (copying_arguments
804 && (XEXP (src, 0) == arg_pointer_rtx
805 || (GET_CODE (XEXP (src, 0)) == PLUS
806 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
941522d6 807 return gen_rtx_ADDRESS (VOIDmode, src);
ea0cb7ae 808 return 0;
809
810 case CONST:
811 src = XEXP (src, 0);
812 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
813 break;
083a2b5e 814
2c0e001b 815 /* ... fall through ... */
e7c893ba 816
ea0cb7ae 817 case PLUS:
818 case MINUS:
e7c893ba 819 {
06c5c535 820 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
821
560de2f8 822 /* If either operand is a REG that is a known pointer, then it
823 is the base. */
824 if (REG_P (src_0) && REG_POINTER (src_0))
825 return find_base_value (src_0);
826 if (REG_P (src_1) && REG_POINTER (src_1))
827 return find_base_value (src_1);
828
06c5c535 829 /* If either operand is a REG, then see if we already have
830 a known value for it. */
560de2f8 831 if (REG_P (src_0))
06c5c535 832 {
833 temp = find_base_value (src_0);
083a2b5e 834 if (temp != 0)
06c5c535 835 src_0 = temp;
836 }
837
560de2f8 838 if (REG_P (src_1))
06c5c535 839 {
840 temp = find_base_value (src_1);
083a2b5e 841 if (temp!= 0)
06c5c535 842 src_1 = temp;
843 }
e7c893ba 844
560de2f8 845 /* If either base is named object or a special address
846 (like an argument or stack reference), then use it for the
847 base term. */
848 if (src_0 != 0
849 && (GET_CODE (src_0) == SYMBOL_REF
850 || GET_CODE (src_0) == LABEL_REF
851 || (GET_CODE (src_0) == ADDRESS
852 && GET_MODE (src_0) != VOIDmode)))
853 return src_0;
854
855 if (src_1 != 0
856 && (GET_CODE (src_1) == SYMBOL_REF
857 || GET_CODE (src_1) == LABEL_REF
858 || (GET_CODE (src_1) == ADDRESS
859 && GET_MODE (src_1) != VOIDmode)))
860 return src_1;
861
083a2b5e 862 /* Guess which operand is the base address:
06c5c535 863 If either operand is a symbol, then it is the base. If
864 either operand is a CONST_INT, then the other is the base. */
083a2b5e 865 if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
e7c893ba 866 return find_base_value (src_0);
083a2b5e 867 else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
06c5c535 868 return find_base_value (src_1);
869
ea0cb7ae 870 return 0;
e7c893ba 871 }
872
873 case LO_SUM:
874 /* The standard form is (lo_sum reg sym) so look only at the
875 second operand. */
876 return find_base_value (XEXP (src, 1));
ea0cb7ae 877
878 case AND:
879 /* If the second operand is constant set the base
2c0e001b 880 address to the first operand. */
e7c893ba 881 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
882 return find_base_value (XEXP (src, 0));
ea0cb7ae 883 return 0;
884
43d45282 885 case TRUNCATE:
886 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
887 break;
888 /* Fall through. */
ea0cb7ae 889 case HIGH:
2be64ef0 890 case PRE_INC:
891 case PRE_DEC:
892 case POST_INC:
893 case POST_DEC:
894 case PRE_MODIFY:
895 case POST_MODIFY:
e7c893ba 896 return find_base_value (XEXP (src, 0));
99c14947 897
d01410d3 898 case ZERO_EXTEND:
899 case SIGN_EXTEND: /* used for NT/Alpha pointers */
900 {
901 rtx temp = find_base_value (XEXP (src, 0));
902
903#ifdef POINTERS_EXTEND_UNSIGNED
904 if (temp != 0 && CONSTANT_P (temp) && GET_MODE (temp) != Pmode)
905 temp = convert_memory_address (Pmode, temp);
906#endif
907
908 return temp;
909 }
910
99c14947 911 default:
912 break;
ea0cb7ae 913 }
914
915 return 0;
916}
917
918/* Called from init_alias_analysis indirectly through note_stores. */
919
083a2b5e 920/* While scanning insns to find base values, reg_seen[N] is nonzero if
ea0cb7ae 921 register N has been set in this function. */
922static char *reg_seen;
923
10882800 924/* Addresses which are known not to alias anything else are identified
925 by a unique integer. */
06c5c535 926static int unique_id;
927
e7c893ba 928static void
ec8895d7 929record_set (dest, set, data)
ea0cb7ae 930 rtx dest, set;
ec8895d7 931 void *data ATTRIBUTE_UNUSED;
ea0cb7ae 932{
19cb6b50 933 unsigned regno;
ea0cb7ae 934 rtx src;
935
936 if (GET_CODE (dest) != REG)
937 return;
938
9db10305 939 regno = REGNO (dest);
ea0cb7ae 940
c2e94b25 941 if (regno >= reg_base_value_size)
942 abort ();
943
ea0cb7ae 944 if (set)
945 {
946 /* A CLOBBER wipes out any old value but does not prevent a previously
947 unset register from acquiring a base address (i.e. reg_seen is not
948 set). */
949 if (GET_CODE (set) == CLOBBER)
950 {
06c5c535 951 new_reg_base_value[regno] = 0;
ea0cb7ae 952 return;
953 }
954 src = SET_SRC (set);
955 }
956 else
957 {
ea0cb7ae 958 if (reg_seen[regno])
959 {
06c5c535 960 new_reg_base_value[regno] = 0;
ea0cb7ae 961 return;
962 }
963 reg_seen[regno] = 1;
941522d6 964 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
965 GEN_INT (unique_id++));
ea0cb7ae 966 return;
967 }
968
969 /* This is not the first set. If the new value is not related to the
970 old value, forget the base value. Note that the following code is
971 not detected:
972 extern int x, y; int *p = &x; p += (&y-&x);
973 ANSI C does not allow computing the difference of addresses
974 of distinct top level objects. */
06c5c535 975 if (new_reg_base_value[regno])
ea0cb7ae 976 switch (GET_CODE (src))
977 {
e7c893ba 978 case LO_SUM:
ea0cb7ae 979 case MINUS:
980 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
06c5c535 981 new_reg_base_value[regno] = 0;
ea0cb7ae 982 break;
43d45282 983 case PLUS:
984 /* If the value we add in the PLUS is also a valid base value,
985 this might be the actual base value, and the original value
986 an index. */
987 {
988 rtx other = NULL_RTX;
989
990 if (XEXP (src, 0) == dest)
991 other = XEXP (src, 1);
992 else if (XEXP (src, 1) == dest)
993 other = XEXP (src, 0);
994
995 if (! other || find_base_value (other))
996 new_reg_base_value[regno] = 0;
997 break;
998 }
ea0cb7ae 999 case AND:
1000 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
06c5c535 1001 new_reg_base_value[regno] = 0;
ea0cb7ae 1002 break;
ea0cb7ae 1003 default:
06c5c535 1004 new_reg_base_value[regno] = 0;
ea0cb7ae 1005 break;
1006 }
1007 /* If this is the first set of a register, record the value. */
1008 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
06c5c535 1009 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
1010 new_reg_base_value[regno] = find_base_value (src);
ea0cb7ae 1011
1012 reg_seen[regno] = 1;
1013}
1014
5badbbc0 1015/* Called from loop optimization when a new pseudo-register is
1016 created. It indicates that REGNO is being set to VAL. f INVARIANT
1017 is true then this value also describes an invariant relationship
1018 which can be used to deduce that two registers with unknown values
1019 are different. */
083a2b5e 1020
ea0cb7ae 1021void
1a6c3add 1022record_base_value (regno, val, invariant)
5badbbc0 1023 unsigned int regno;
ea0cb7ae 1024 rtx val;
1a6c3add 1025 int invariant;
ea0cb7ae 1026{
5badbbc0 1027 if (regno >= reg_base_value_size)
ea0cb7ae 1028 return;
1a6c3add 1029
1a6c3add 1030 if (invariant && alias_invariant)
1031 alias_invariant[regno] = val;
1032
ea0cb7ae 1033 if (GET_CODE (val) == REG)
1034 {
9db10305 1035 if (REGNO (val) < reg_base_value_size)
1036 reg_base_value[regno] = reg_base_value[REGNO (val)];
083a2b5e 1037
ea0cb7ae 1038 return;
1039 }
083a2b5e 1040
ea0cb7ae 1041 reg_base_value[regno] = find_base_value (val);
1042}
1043
9f5e1a5a 1044/* Clear alias info for a register. This is used if an RTL transformation
1045 changes the value of a register. This is used in flow by AUTO_INC_DEC
1046 optimizations. We don't need to clear reg_base_value, since flow only
1047 changes the offset. */
1048
1049void
27d0c333 1050clear_reg_alias_info (reg)
1051 rtx reg;
9f5e1a5a 1052{
26c8b134 1053 unsigned int regno = REGNO (reg);
1054
1055 if (regno < reg_known_value_size && regno >= FIRST_PSEUDO_REGISTER)
1056 reg_known_value[regno] = reg;
9f5e1a5a 1057}
1058
7cfb9bcf 1059/* Returns a canonical version of X, from the point of view alias
1060 analysis. (For example, if X is a MEM whose address is a register,
1061 and the register has a known value (say a SYMBOL_REF), then a MEM
1062 whose address is the SYMBOL_REF is returned.) */
1063
1064rtx
ea0cb7ae 1065canon_rtx (x)
1066 rtx x;
1067{
1068 /* Recursively look for equivalences. */
9db10305 1069 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
1070 && REGNO (x) < reg_known_value_size)
1071 return reg_known_value[REGNO (x)] == x
1072 ? x : canon_rtx (reg_known_value[REGNO (x)]);
ea0cb7ae 1073 else if (GET_CODE (x) == PLUS)
1074 {
1075 rtx x0 = canon_rtx (XEXP (x, 0));
1076 rtx x1 = canon_rtx (XEXP (x, 1));
1077
1078 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1079 {
ea0cb7ae 1080 if (GET_CODE (x0) == CONST_INT)
b244d4c7 1081 return plus_constant (x1, INTVAL (x0));
ea0cb7ae 1082 else if (GET_CODE (x1) == CONST_INT)
b244d4c7 1083 return plus_constant (x0, INTVAL (x1));
941522d6 1084 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
ea0cb7ae 1085 }
1086 }
083a2b5e 1087
ea0cb7ae 1088 /* This gives us much better alias analysis when called from
1089 the loop optimizer. Note we want to leave the original
1090 MEM alone, but need to return the canonicalized MEM with
1091 all the flags with their original values. */
1092 else if (GET_CODE (x) == MEM)
e4e86ec5 1093 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
083a2b5e 1094
ea0cb7ae 1095 return x;
1096}
1097
1098/* Return 1 if X and Y are identical-looking rtx's.
1099
1100 We use the data in reg_known_value above to see if two registers with
1101 different numbers are, in fact, equivalent. */
1102
1103static int
1104rtx_equal_for_memref_p (x, y)
1105 rtx x, y;
1106{
19cb6b50 1107 int i;
1108 int j;
1109 enum rtx_code code;
1110 const char *fmt;
ea0cb7ae 1111
1112 if (x == 0 && y == 0)
1113 return 1;
1114 if (x == 0 || y == 0)
1115 return 0;
083a2b5e 1116
ea0cb7ae 1117 x = canon_rtx (x);
1118 y = canon_rtx (y);
1119
1120 if (x == y)
1121 return 1;
1122
1123 code = GET_CODE (x);
1124 /* Rtx's of different codes cannot be equal. */
1125 if (code != GET_CODE (y))
1126 return 0;
1127
1128 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1129 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1130
1131 if (GET_MODE (x) != GET_MODE (y))
1132 return 0;
1133
7cfb9bcf 1134 /* Some RTL can be compared without a recursive examination. */
1135 switch (code)
1136 {
3d0f529a 1137 case VALUE:
1138 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
1139
7cfb9bcf 1140 case REG:
1141 return REGNO (x) == REGNO (y);
1142
1143 case LABEL_REF:
1144 return XEXP (x, 0) == XEXP (y, 0);
9342ee68 1145
7cfb9bcf 1146 case SYMBOL_REF:
1147 return XSTR (x, 0) == XSTR (y, 0);
1148
1149 case CONST_INT:
1150 case CONST_DOUBLE:
1151 /* There's no need to compare the contents of CONST_DOUBLEs or
1152 CONST_INTs because pointer equality is a good enough
1153 comparison for these nodes. */
1154 return 0;
1155
1156 case ADDRESSOF:
a1a6acb4 1157 return (XINT (x, 1) == XINT (y, 1)
1158 && rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0)));
7cfb9bcf 1159
1160 default:
1161 break;
1162 }
ea0cb7ae 1163
1164 /* For commutative operations, the RTX match if the operand match in any
1165 order. Also handle the simple binary and unary cases without a loop. */
1166 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
1167 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1168 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1169 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1170 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1171 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
1172 return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1173 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
1174 else if (GET_RTX_CLASS (code) == '1')
1175 return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
1176
1177 /* Compare the elements. If any pair of corresponding elements
1a6c3add 1178 fail to match, return 0 for the whole things.
1179
1180 Limit cases to types which actually appear in addresses. */
ea0cb7ae 1181
1182 fmt = GET_RTX_FORMAT (code);
1183 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1184 {
1185 switch (fmt[i])
1186 {
ea0cb7ae 1187 case 'i':
1188 if (XINT (x, i) != XINT (y, i))
1189 return 0;
1190 break;
1191
ea0cb7ae 1192 case 'E':
1193 /* Two vectors must have the same length. */
1194 if (XVECLEN (x, i) != XVECLEN (y, i))
1195 return 0;
1196
1197 /* And the corresponding elements must match. */
1198 for (j = 0; j < XVECLEN (x, i); j++)
083a2b5e 1199 if (rtx_equal_for_memref_p (XVECEXP (x, i, j),
1200 XVECEXP (y, i, j)) == 0)
ea0cb7ae 1201 return 0;
1202 break;
1203
1204 case 'e':
1205 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
1206 return 0;
1207 break;
1208
83ec925c 1209 /* This can happen for asm operands. */
1210 case 's':
1211 if (strcmp (XSTR (x, i), XSTR (y, i)))
1212 return 0;
1213 break;
1214
23c04f6b 1215 /* This can happen for an asm which clobbers memory. */
1216 case '0':
1217 break;
1218
ea0cb7ae 1219 /* It is believed that rtx's at this level will never
1220 contain anything but integers and other rtx's,
1221 except for within LABEL_REFs and SYMBOL_REFs. */
1222 default:
1223 abort ();
1224 }
1225 }
1226 return 1;
1227}
1228
1229/* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
1230 X and return it, or return 0 if none found. */
1231
1232static rtx
1233find_symbolic_term (x)
1234 rtx x;
1235{
19cb6b50 1236 int i;
1237 enum rtx_code code;
1238 const char *fmt;
ea0cb7ae 1239
1240 code = GET_CODE (x);
1241 if (code == SYMBOL_REF || code == LABEL_REF)
1242 return x;
1243 if (GET_RTX_CLASS (code) == 'o')
1244 return 0;
1245
1246 fmt = GET_RTX_FORMAT (code);
1247 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1248 {
1249 rtx t;
1250
1251 if (fmt[i] == 'e')
1252 {
1253 t = find_symbolic_term (XEXP (x, i));
1254 if (t != 0)
1255 return t;
1256 }
1257 else if (fmt[i] == 'E')
1258 break;
1259 }
1260 return 0;
1261}
1262
1263static rtx
1264find_base_term (x)
19cb6b50 1265 rtx x;
ea0cb7ae 1266{
1617c276 1267 cselib_val *val;
1268 struct elt_loc_list *l;
1269
15750b1e 1270#if defined (FIND_BASE_TERM)
1271 /* Try machine-dependent ways to find the base term. */
1272 x = FIND_BASE_TERM (x);
1273#endif
1274
ea0cb7ae 1275 switch (GET_CODE (x))
1276 {
1277 case REG:
1278 return REG_BASE_VALUE (x);
1279
2be64ef0 1280 case TRUNCATE:
1281 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
9342ee68 1282 return 0;
2be64ef0 1283 /* Fall through. */
ea0cb7ae 1284 case HIGH:
22dfec7d 1285 case PRE_INC:
1286 case PRE_DEC:
1287 case POST_INC:
1288 case POST_DEC:
2be64ef0 1289 case PRE_MODIFY:
1290 case POST_MODIFY:
22dfec7d 1291 return find_base_term (XEXP (x, 0));
1292
5104ef69 1293 case ZERO_EXTEND:
1294 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1295 {
1296 rtx temp = find_base_term (XEXP (x, 0));
1297
1298#ifdef POINTERS_EXTEND_UNSIGNED
1299 if (temp != 0 && CONSTANT_P (temp) && GET_MODE (temp) != Pmode)
1300 temp = convert_memory_address (Pmode, temp);
1301#endif
1302
1303 return temp;
1304 }
1305
1617c276 1306 case VALUE:
1307 val = CSELIB_VAL_PTR (x);
1308 for (l = val->locs; l; l = l->next)
1309 if ((x = find_base_term (l->loc)) != 0)
1310 return x;
1311 return 0;
1312
ea0cb7ae 1313 case CONST:
1314 x = XEXP (x, 0);
1315 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1316 return 0;
1317 /* fall through */
1318 case LO_SUM:
1319 case PLUS:
1320 case MINUS:
1321 {
1b5bbc4c 1322 rtx tmp1 = XEXP (x, 0);
1323 rtx tmp2 = XEXP (x, 1);
1324
35a3065a 1325 /* This is a little bit tricky since we have to determine which of
1b5bbc4c 1326 the two operands represents the real base address. Otherwise this
1327 routine may return the index register instead of the base register.
1328
1329 That may cause us to believe no aliasing was possible, when in
1330 fact aliasing is possible.
1331
1332 We use a few simple tests to guess the base register. Additional
1333 tests can certainly be added. For example, if one of the operands
1334 is a shift or multiply, then it must be the index register and the
1335 other operand is the base register. */
9342ee68 1336
15750b1e 1337 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1338 return find_base_term (tmp2);
1339
1b5bbc4c 1340 /* If either operand is known to be a pointer, then use it
1341 to determine the base term. */
e61a0a7f 1342 if (REG_P (tmp1) && REG_POINTER (tmp1))
1b5bbc4c 1343 return find_base_term (tmp1);
1344
e61a0a7f 1345 if (REG_P (tmp2) && REG_POINTER (tmp2))
1b5bbc4c 1346 return find_base_term (tmp2);
1347
1348 /* Neither operand was known to be a pointer. Go ahead and find the
1349 base term for both operands. */
1350 tmp1 = find_base_term (tmp1);
1351 tmp2 = find_base_term (tmp2);
1352
1353 /* If either base term is named object or a special address
1354 (like an argument or stack reference), then use it for the
1355 base term. */
083a2b5e 1356 if (tmp1 != 0
1b5bbc4c 1357 && (GET_CODE (tmp1) == SYMBOL_REF
1358 || GET_CODE (tmp1) == LABEL_REF
1359 || (GET_CODE (tmp1) == ADDRESS
1360 && GET_MODE (tmp1) != VOIDmode)))
1361 return tmp1;
1362
083a2b5e 1363 if (tmp2 != 0
1b5bbc4c 1364 && (GET_CODE (tmp2) == SYMBOL_REF
1365 || GET_CODE (tmp2) == LABEL_REF
1366 || (GET_CODE (tmp2) == ADDRESS
1367 && GET_MODE (tmp2) != VOIDmode)))
1368 return tmp2;
1369
1370 /* We could not determine which of the two operands was the
1371 base register and which was the index. So we can determine
1372 nothing from the base alias check. */
1373 return 0;
ea0cb7ae 1374 }
1375
1376 case AND:
2be64ef0 1377 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) != 0)
1378 return find_base_term (XEXP (x, 0));
ea0cb7ae 1379 return 0;
1380
1381 case SYMBOL_REF:
1382 case LABEL_REF:
1383 return x;
1384
7a4fb8f4 1385 case ADDRESSOF:
7002e559 1386 return REG_BASE_VALUE (frame_pointer_rtx);
7a4fb8f4 1387
ea0cb7ae 1388 default:
1389 return 0;
1390 }
1391}
1392
1393/* Return 0 if the addresses X and Y are known to point to different
1394 objects, 1 if they might be pointers to the same object. */
1395
1396static int
9fb1c31b 1397base_alias_check (x, y, x_mode, y_mode)
ea0cb7ae 1398 rtx x, y;
9fb1c31b 1399 enum machine_mode x_mode, y_mode;
ea0cb7ae 1400{
1401 rtx x_base = find_base_term (x);
1402 rtx y_base = find_base_term (y);
1403
3ad3a0ed 1404 /* If the address itself has no known base see if a known equivalent
1405 value has one. If either address still has no known base, nothing
1406 is known about aliasing. */
1407 if (x_base == 0)
1408 {
1409 rtx x_c;
083a2b5e 1410
3ad3a0ed 1411 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1412 return 1;
083a2b5e 1413
3ad3a0ed 1414 x_base = find_base_term (x_c);
1415 if (x_base == 0)
1416 return 1;
1417 }
ea0cb7ae 1418
3ad3a0ed 1419 if (y_base == 0)
1420 {
1421 rtx y_c;
1422 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1423 return 1;
083a2b5e 1424
3ad3a0ed 1425 y_base = find_base_term (y_c);
1426 if (y_base == 0)
1427 return 1;
1428 }
1429
1430 /* If the base addresses are equal nothing is known about aliasing. */
1431 if (rtx_equal_p (x_base, y_base))
ea0cb7ae 1432 return 1;
1433
9342ee68 1434 /* The base addresses of the read and write are different expressions.
9fb1c31b 1435 If they are both symbols and they are not accessed via AND, there is
1436 no conflict. We can bring knowledge of object alignment into play
1437 here. For example, on alpha, "char a, b;" can alias one another,
1438 though "char a; long b;" cannot. */
ea0cb7ae 1439 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1e38bf49 1440 {
9fb1c31b 1441 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1442 return 1;
1443 if (GET_CODE (x) == AND
1444 && (GET_CODE (XEXP (x, 1)) != CONST_INT
8e6c5eac 1445 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
9fb1c31b 1446 return 1;
1447 if (GET_CODE (y) == AND
1448 && (GET_CODE (XEXP (y, 1)) != CONST_INT
8e6c5eac 1449 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
9fb1c31b 1450 return 1;
2b4e2cb9 1451 /* Differing symbols never alias. */
1452 return 0;
1e38bf49 1453 }
ea0cb7ae 1454
1455 /* If one address is a stack reference there can be no alias:
1456 stack references using different base registers do not alias,
1457 a stack reference can not alias a parameter, and a stack reference
1458 can not alias a global. */
1459 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1460 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1461 return 0;
1462
1463 if (! flag_argument_noalias)
1464 return 1;
1465
1466 if (flag_argument_noalias > 1)
1467 return 0;
1468
2c0e001b 1469 /* Weak noalias assertion (arguments are distinct, but may match globals). */
ea0cb7ae 1470 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1471}
1472
1617c276 1473/* Convert the address X into something we can use. This is done by returning
1474 it unchanged unless it is a value; in the latter case we call cselib to get
1475 a more useful rtx. */
f7c44134 1476
8e802be9 1477rtx
1617c276 1478get_addr (x)
1479 rtx x;
1480{
1481 cselib_val *v;
1482 struct elt_loc_list *l;
1483
1484 if (GET_CODE (x) != VALUE)
1485 return x;
1486 v = CSELIB_VAL_PTR (x);
1487 for (l = v->locs; l; l = l->next)
1488 if (CONSTANT_P (l->loc))
1489 return l->loc;
1490 for (l = v->locs; l; l = l->next)
1491 if (GET_CODE (l->loc) != REG && GET_CODE (l->loc) != MEM)
1492 return l->loc;
1493 if (v->locs)
1494 return v->locs->loc;
1495 return x;
1496}
1497
b9f92976 1498/* Return the address of the (N_REFS + 1)th memory reference to ADDR
1499 where SIZE is the size in bytes of the memory reference. If ADDR
1500 is not modified by the memory reference then ADDR is returned. */
1501
1502rtx
1503addr_side_effect_eval (addr, size, n_refs)
1504 rtx addr;
1505 int size;
1506 int n_refs;
1507{
1508 int offset = 0;
9342ee68 1509
b9f92976 1510 switch (GET_CODE (addr))
1511 {
1512 case PRE_INC:
1513 offset = (n_refs + 1) * size;
1514 break;
1515 case PRE_DEC:
1516 offset = -(n_refs + 1) * size;
1517 break;
1518 case POST_INC:
1519 offset = n_refs * size;
1520 break;
1521 case POST_DEC:
1522 offset = -n_refs * size;
1523 break;
1524
1525 default:
1526 return addr;
1527 }
9342ee68 1528
b9f92976 1529 if (offset)
1530 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
1531 else
1532 addr = XEXP (addr, 0);
1533
1534 return addr;
1535}
1536
ea0cb7ae 1537/* Return nonzero if X and Y (memory addresses) could reference the
1538 same location in memory. C is an offset accumulator. When
1539 C is nonzero, we are testing aliases between X and Y + C.
1540 XSIZE is the size in bytes of the X reference,
1541 similarly YSIZE is the size in bytes for Y.
1542
1543 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1544 referenced (the reference was BLKmode), so make the most pessimistic
1545 assumptions.
1546
1e38bf49 1547 If XSIZE or YSIZE is negative, we may access memory outside the object
1548 being referenced as a side effect. This can happen when using AND to
1549 align memory references, as is done on the Alpha.
1550
ea0cb7ae 1551 Nice to notice that varying addresses cannot conflict with fp if no
4958fe6e 1552 local variables had their addresses taken, but that's too hard now. */
ea0cb7ae 1553
ea0cb7ae 1554static int
1555memrefs_conflict_p (xsize, x, ysize, y, c)
19cb6b50 1556 rtx x, y;
ea0cb7ae 1557 int xsize, ysize;
1558 HOST_WIDE_INT c;
1559{
1617c276 1560 if (GET_CODE (x) == VALUE)
1561 x = get_addr (x);
1562 if (GET_CODE (y) == VALUE)
1563 y = get_addr (y);
ea0cb7ae 1564 if (GET_CODE (x) == HIGH)
1565 x = XEXP (x, 0);
1566 else if (GET_CODE (x) == LO_SUM)
1567 x = XEXP (x, 1);
1568 else
b9f92976 1569 x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
ea0cb7ae 1570 if (GET_CODE (y) == HIGH)
1571 y = XEXP (y, 0);
1572 else if (GET_CODE (y) == LO_SUM)
1573 y = XEXP (y, 1);
1574 else
b9f92976 1575 y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
ea0cb7ae 1576
1577 if (rtx_equal_for_memref_p (x, y))
1578 {
1e38bf49 1579 if (xsize <= 0 || ysize <= 0)
ea0cb7ae 1580 return 1;
1581 if (c >= 0 && xsize > c)
1582 return 1;
1583 if (c < 0 && ysize+c > 0)
1584 return 1;
1585 return 0;
1586 }
1587
35d3eedd 1588 /* This code used to check for conflicts involving stack references and
1589 globals but the base address alias code now handles these cases. */
ea0cb7ae 1590
1591 if (GET_CODE (x) == PLUS)
1592 {
1593 /* The fact that X is canonicalized means that this
1594 PLUS rtx is canonicalized. */
1595 rtx x0 = XEXP (x, 0);
1596 rtx x1 = XEXP (x, 1);
1597
1598 if (GET_CODE (y) == PLUS)
1599 {
1600 /* The fact that Y is canonicalized means that this
1601 PLUS rtx is canonicalized. */
1602 rtx y0 = XEXP (y, 0);
1603 rtx y1 = XEXP (y, 1);
1604
1605 if (rtx_equal_for_memref_p (x1, y1))
1606 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1607 if (rtx_equal_for_memref_p (x0, y0))
1608 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1609 if (GET_CODE (x1) == CONST_INT)
5f59c49b 1610 {
1611 if (GET_CODE (y1) == CONST_INT)
1612 return memrefs_conflict_p (xsize, x0, ysize, y0,
1613 c - INTVAL (x1) + INTVAL (y1));
1614 else
1615 return memrefs_conflict_p (xsize, x0, ysize, y,
1616 c - INTVAL (x1));
1617 }
ea0cb7ae 1618 else if (GET_CODE (y1) == CONST_INT)
1619 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1620
35d3eedd 1621 return 1;
ea0cb7ae 1622 }
1623 else if (GET_CODE (x1) == CONST_INT)
1624 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1625 }
1626 else if (GET_CODE (y) == PLUS)
1627 {
1628 /* The fact that Y is canonicalized means that this
1629 PLUS rtx is canonicalized. */
1630 rtx y0 = XEXP (y, 0);
1631 rtx y1 = XEXP (y, 1);
1632
1633 if (GET_CODE (y1) == CONST_INT)
1634 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1635 else
1636 return 1;
1637 }
1638
1639 if (GET_CODE (x) == GET_CODE (y))
1640 switch (GET_CODE (x))
1641 {
1642 case MULT:
1643 {
1644 /* Handle cases where we expect the second operands to be the
1645 same, and check only whether the first operand would conflict
1646 or not. */
1647 rtx x0, y0;
1648 rtx x1 = canon_rtx (XEXP (x, 1));
1649 rtx y1 = canon_rtx (XEXP (y, 1));
1650 if (! rtx_equal_for_memref_p (x1, y1))
1651 return 1;
1652 x0 = canon_rtx (XEXP (x, 0));
1653 y0 = canon_rtx (XEXP (y, 0));
1654 if (rtx_equal_for_memref_p (x0, y0))
1655 return (xsize == 0 || ysize == 0
1656 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1657
1658 /* Can't properly adjust our sizes. */
1659 if (GET_CODE (x1) != CONST_INT)
1660 return 1;
1661 xsize /= INTVAL (x1);
1662 ysize /= INTVAL (x1);
1663 c /= INTVAL (x1);
1664 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1665 }
99c14947 1666
1a6c3add 1667 case REG:
1668 /* Are these registers known not to be equal? */
1669 if (alias_invariant)
1670 {
274c11d8 1671 unsigned int r_x = REGNO (x), r_y = REGNO (y);
1a6c3add 1672 rtx i_x, i_y; /* invariant relationships of X and Y */
1673
1674 i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1675 i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1676
1677 if (i_x == 0 && i_y == 0)
1678 break;
1679
1680 if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1681 ysize, i_y ? i_y : y, c))
1682 return 0;
1683 }
1684 break;
1685
99c14947 1686 default:
1687 break;
ea0cb7ae 1688 }
1689
1690 /* Treat an access through an AND (e.g. a subword access on an Alpha)
9342ee68 1691 as an access with indeterminate size. Assume that references
9fb1c31b 1692 besides AND are aligned, so if the size of the other reference is
1693 at least as large as the alignment, assume no other overlap. */
ea0cb7ae 1694 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
9fb1c31b 1695 {
bcdb334e 1696 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
9fb1c31b 1697 xsize = -1;
1698 return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1699 }
ea0cb7ae 1700 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1e38bf49 1701 {
9fb1c31b 1702 /* ??? If we are indexing far enough into the array/structure, we
9342ee68 1703 may yet be able to determine that we can not overlap. But we
1e38bf49 1704 also need to that we are far enough from the end not to overlap
9fb1c31b 1705 a following reference, so we do nothing with that for now. */
bcdb334e 1706 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
9fb1c31b 1707 ysize = -1;
1708 return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1e38bf49 1709 }
ea0cb7ae 1710
85e64f58 1711 if (GET_CODE (x) == ADDRESSOF)
1712 {
1713 if (y == frame_pointer_rtx
1714 || GET_CODE (y) == ADDRESSOF)
1715 return xsize <= 0 || ysize <= 0;
1716 }
1717 if (GET_CODE (y) == ADDRESSOF)
1718 {
1719 if (x == frame_pointer_rtx)
1720 return xsize <= 0 || ysize <= 0;
1721 }
7a4fb8f4 1722
ea0cb7ae 1723 if (CONSTANT_P (x))
1724 {
1725 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1726 {
1727 c += (INTVAL (y) - INTVAL (x));
1e38bf49 1728 return (xsize <= 0 || ysize <= 0
ea0cb7ae 1729 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1730 }
1731
1732 if (GET_CODE (x) == CONST)
1733 {
1734 if (GET_CODE (y) == CONST)
1735 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1736 ysize, canon_rtx (XEXP (y, 0)), c);
1737 else
1738 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1739 ysize, y, c);
1740 }
1741 if (GET_CODE (y) == CONST)
1742 return memrefs_conflict_p (xsize, x, ysize,
1743 canon_rtx (XEXP (y, 0)), c);
1744
1745 if (CONSTANT_P (y))
15750b1e 1746 return (xsize <= 0 || ysize <= 0
1e38bf49 1747 || (rtx_equal_for_memref_p (x, y)
15750b1e 1748 && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
ea0cb7ae 1749
1750 return 1;
1751 }
1752 return 1;
1753}
1754
1755/* Functions to compute memory dependencies.
1756
1757 Since we process the insns in execution order, we can build tables
1758 to keep track of what registers are fixed (and not aliased), what registers
1759 are varying in known ways, and what registers are varying in unknown
1760 ways.
1761
1762 If both memory references are volatile, then there must always be a
1763 dependence between the two references, since their order can not be
1764 changed. A volatile and non-volatile reference can be interchanged
9342ee68 1765 though.
ea0cb7ae 1766
71d9e995 1767 A MEM_IN_STRUCT reference at a non-AND varying address can never
1768 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
1769 also must allow AND addresses, because they may generate accesses
1770 outside the object being referenced. This is used to generate
1771 aligned addresses from unaligned addresses, for instance, the alpha
1772 storeqi_unaligned pattern. */
ea0cb7ae 1773
1774/* Read dependence: X is read after read in MEM takes place. There can
1775 only be a dependence here if both reads are volatile. */
1776
1777int
1778read_dependence (mem, x)
1779 rtx mem;
1780 rtx x;
1781{
1782 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1783}
1784
6a0934dd 1785/* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1786 MEM2 is a reference to a structure at a varying address, or returns
1787 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1788 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1789 to decide whether or not an address may vary; it should return
1617c276 1790 nonzero whenever variation is possible.
1791 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
9342ee68 1792
fe69e4fe 1793static rtx
1617c276 1794fixed_scalar_and_varying_struct_p (mem1, mem2, mem1_addr, mem2_addr, varies_p)
1795 rtx mem1, mem2;
1796 rtx mem1_addr, mem2_addr;
ea087693 1797 int (*varies_p) PARAMS ((rtx, int));
9342ee68 1798{
c7f6877a 1799 if (! flag_strict_aliasing)
1800 return NULL_RTX;
1801
9342ee68 1802 if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
ea087693 1803 && !varies_p (mem1_addr, 1) && varies_p (mem2_addr, 1))
6a0934dd 1804 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1805 varying address. */
1806 return mem1;
1807
9342ee68 1808 if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
ea087693 1809 && varies_p (mem1_addr, 1) && !varies_p (mem2_addr, 1))
6a0934dd 1810 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1811 varying address. */
1812 return mem2;
1813
1814 return NULL_RTX;
1815}
1816
1817/* Returns nonzero if something about the mode or address format MEM1
1818 indicates that it might well alias *anything*. */
1819
fe69e4fe 1820static int
6a0934dd 1821aliases_everything_p (mem)
1822 rtx mem;
1823{
6a0934dd 1824 if (GET_CODE (XEXP (mem, 0)) == AND)
1825 /* If the address is an AND, its very hard to know at what it is
1826 actually pointing. */
1827 return 1;
9342ee68 1828
6a0934dd 1829 return 0;
1830}
1831
b10dbbca 1832/* Return true if we can determine that the fields referenced cannot
1833 overlap for any pair of objects. */
1834
1835static bool
1836nonoverlapping_component_refs_p (x, y)
1837 tree x, y;
1838{
1839 tree fieldx, fieldy, typex, typey, orig_y;
1840
1841 do
1842 {
1843 /* The comparison has to be done at a common type, since we don't
cb0ccc1e 1844 know how the inheritance hierarchy works. */
b10dbbca 1845 orig_y = y;
1846 do
1847 {
1848 fieldx = TREE_OPERAND (x, 1);
1849 typex = DECL_FIELD_CONTEXT (fieldx);
1850
1851 y = orig_y;
1852 do
1853 {
1854 fieldy = TREE_OPERAND (y, 1);
1855 typey = DECL_FIELD_CONTEXT (fieldy);
1856
1857 if (typex == typey)
1858 goto found;
1859
1860 y = TREE_OPERAND (y, 0);
1861 }
1862 while (y && TREE_CODE (y) == COMPONENT_REF);
1863
1864 x = TREE_OPERAND (x, 0);
1865 }
1866 while (x && TREE_CODE (x) == COMPONENT_REF);
1867
1868 /* Never found a common type. */
1869 return false;
1870
1871 found:
1872 /* If we're left with accessing different fields of a structure,
1873 then no overlap. */
1874 if (TREE_CODE (typex) == RECORD_TYPE
1875 && fieldx != fieldy)
1876 return true;
1877
1878 /* The comparison on the current field failed. If we're accessing
1879 a very nested structure, look at the next outer level. */
1880 x = TREE_OPERAND (x, 0);
1881 y = TREE_OPERAND (y, 0);
1882 }
1883 while (x && y
1884 && TREE_CODE (x) == COMPONENT_REF
1885 && TREE_CODE (y) == COMPONENT_REF);
9342ee68 1886
b10dbbca 1887 return false;
1888}
1889
1890/* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
1891
1892static tree
1893decl_for_component_ref (x)
1894 tree x;
1895{
1896 do
1897 {
1898 x = TREE_OPERAND (x, 0);
1899 }
1900 while (x && TREE_CODE (x) == COMPONENT_REF);
1901
1902 return x && DECL_P (x) ? x : NULL_TREE;
1903}
1904
1905/* Walk up the COMPONENT_REF list and adjust OFFSET to compensate for the
1906 offset of the field reference. */
1907
1908static rtx
1909adjust_offset_for_component_ref (x, offset)
1910 tree x;
1911 rtx offset;
1912{
1913 HOST_WIDE_INT ioffset;
1914
1915 if (! offset)
1916 return NULL_RTX;
1917
1918 ioffset = INTVAL (offset);
9342ee68 1919 do
b10dbbca 1920 {
1921 tree field = TREE_OPERAND (x, 1);
1922
1923 if (! host_integerp (DECL_FIELD_OFFSET (field), 1))
1924 return NULL_RTX;
1925 ioffset += (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1926 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1927 / BITS_PER_UNIT));
1928
1929 x = TREE_OPERAND (x, 0);
1930 }
1931 while (x && TREE_CODE (x) == COMPONENT_REF);
1932
1933 return GEN_INT (ioffset);
1934}
1935
1936/* Return nonzero if we can deterimine the exprs corresponding to memrefs
ddb3d16f 1937 X and Y and they do not overlap. */
1938
1939static int
1940nonoverlapping_memrefs_p (x, y)
1941 rtx x, y;
1942{
b10dbbca 1943 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
ddb3d16f 1944 rtx rtlx, rtly;
1945 rtx basex, basey;
b10dbbca 1946 rtx moffsetx, moffsety;
ddb3d16f 1947 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
1948
b10dbbca 1949 /* Unless both have exprs, we can't tell anything. */
1950 if (exprx == 0 || expry == 0)
1951 return 0;
1952
1953 /* If both are field references, we may be able to determine something. */
1954 if (TREE_CODE (exprx) == COMPONENT_REF
1955 && TREE_CODE (expry) == COMPONENT_REF
1956 && nonoverlapping_component_refs_p (exprx, expry))
1957 return 1;
1958
1959 /* If the field reference test failed, look at the DECLs involved. */
1960 moffsetx = MEM_OFFSET (x);
1961 if (TREE_CODE (exprx) == COMPONENT_REF)
1962 {
1963 tree t = decl_for_component_ref (exprx);
1964 if (! t)
1965 return 0;
1966 moffsetx = adjust_offset_for_component_ref (exprx, moffsetx);
1967 exprx = t;
1968 }
2d8fe5d0 1969 else if (TREE_CODE (exprx) == INDIRECT_REF)
1970 {
1971 exprx = TREE_OPERAND (exprx, 0);
1972 if (flag_argument_noalias < 2
1973 || TREE_CODE (exprx) != PARM_DECL)
1974 return 0;
1975 }
1976
b10dbbca 1977 moffsety = MEM_OFFSET (y);
1978 if (TREE_CODE (expry) == COMPONENT_REF)
1979 {
1980 tree t = decl_for_component_ref (expry);
1981 if (! t)
1982 return 0;
1983 moffsety = adjust_offset_for_component_ref (expry, moffsety);
1984 expry = t;
1985 }
2d8fe5d0 1986 else if (TREE_CODE (expry) == INDIRECT_REF)
1987 {
1988 expry = TREE_OPERAND (expry, 0);
1989 if (flag_argument_noalias < 2
1990 || TREE_CODE (expry) != PARM_DECL)
1991 return 0;
1992 }
b10dbbca 1993
1994 if (! DECL_P (exprx) || ! DECL_P (expry))
ddb3d16f 1995 return 0;
1996
b10dbbca 1997 rtlx = DECL_RTL (exprx);
1998 rtly = DECL_RTL (expry);
ddb3d16f 1999
10e949c1 2000 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2001 can't overlap unless they are the same because we never reuse that part
2002 of the stack frame used for locals for spilled pseudos. */
2003 if ((GET_CODE (rtlx) != MEM || GET_CODE (rtly) != MEM)
2004 && ! rtx_equal_p (rtlx, rtly))
ddb3d16f 2005 return 1;
2006
2007 /* Get the base and offsets of both decls. If either is a register, we
2008 know both are and are the same, so use that as the base. The only
2009 we can avoid overlap is if we can deduce that they are nonoverlapping
2010 pieces of that decl, which is very rare. */
10e949c1 2011 basex = GET_CODE (rtlx) == MEM ? XEXP (rtlx, 0) : rtlx;
ddb3d16f 2012 if (GET_CODE (basex) == PLUS && GET_CODE (XEXP (basex, 1)) == CONST_INT)
2013 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2014
10e949c1 2015 basey = GET_CODE (rtly) == MEM ? XEXP (rtly, 0) : rtly;
ddb3d16f 2016 if (GET_CODE (basey) == PLUS && GET_CODE (XEXP (basey, 1)) == CONST_INT)
2017 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2018
c9c48480 2019 /* If the bases are different, we know they do not overlap if both
9342ee68 2020 are constants or if one is a constant and the other a pointer into the
c9c48480 2021 stack frame. Otherwise a different base means we can't tell if they
2022 overlap or not. */
2023 if (! rtx_equal_p (basex, basey))
9342ee68 2024 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2025 || (CONSTANT_P (basex) && REG_P (basey)
2026 && REGNO_PTR_FRAME_P (REGNO (basey)))
2027 || (CONSTANT_P (basey) && REG_P (basex)
2028 && REGNO_PTR_FRAME_P (REGNO (basex))));
ddb3d16f 2029
b10dbbca 2030 sizex = (GET_CODE (rtlx) != MEM ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
ddb3d16f 2031 : MEM_SIZE (rtlx) ? INTVAL (MEM_SIZE (rtlx))
2032 : -1);
b10dbbca 2033 sizey = (GET_CODE (rtly) != MEM ? (int) GET_MODE_SIZE (GET_MODE (rtly))
ddb3d16f 2034 : MEM_SIZE (rtly) ? INTVAL (MEM_SIZE (rtly)) :
2035 -1);
2036
dc2dfdb6 2037 /* If we have an offset for either memref, it can update the values computed
2038 above. */
b10dbbca 2039 if (moffsetx)
2040 offsetx += INTVAL (moffsetx), sizex -= INTVAL (moffsetx);
2041 if (moffsety)
2042 offsety += INTVAL (moffsety), sizey -= INTVAL (moffsety);
ddb3d16f 2043
dc2dfdb6 2044 /* If a memref has both a size and an offset, we can use the smaller size.
203c9fee 2045 We can't do this if the offset isn't known because we must view this
dc2dfdb6 2046 memref as being anywhere inside the DECL's MEM. */
b10dbbca 2047 if (MEM_SIZE (x) && moffsetx)
ddb3d16f 2048 sizex = INTVAL (MEM_SIZE (x));
b10dbbca 2049 if (MEM_SIZE (y) && moffsety)
ddb3d16f 2050 sizey = INTVAL (MEM_SIZE (y));
2051
2052 /* Put the values of the memref with the lower offset in X's values. */
2053 if (offsetx > offsety)
2054 {
2055 tem = offsetx, offsetx = offsety, offsety = tem;
2056 tem = sizex, sizex = sizey, sizey = tem;
2057 }
2058
2059 /* If we don't know the size of the lower-offset value, we can't tell
2060 if they conflict. Otherwise, we do the test. */
906923ba 2061 return sizex >= 0 && offsety >= offsetx + sizex;
ddb3d16f 2062}
2063
ea0cb7ae 2064/* True dependence: X is read after store in MEM takes place. */
2065
2066int
2067true_dependence (mem, mem_mode, x, varies)
2068 rtx mem;
2069 enum machine_mode mem_mode;
2070 rtx x;
ea087693 2071 int (*varies) PARAMS ((rtx, int));
ea0cb7ae 2072{
19cb6b50 2073 rtx x_addr, mem_addr;
9ee7cac5 2074 rtx base;
ea0cb7ae 2075
2076 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2077 return 1;
2078
a57f4c4d 2079 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2080 This is used in epilogue deallocation functions. */
2081 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2082 return 1;
2083 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2084 return 1;
2085
b5ba9f3a 2086 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2087 return 0;
2088
15750b1e 2089 /* Unchanging memory can't conflict with non-unchanging memory.
2090 A non-unchanging read can conflict with a non-unchanging write.
2091 An unchanging read can conflict with an unchanging write since
2092 there may be a single store to this address to initialize it.
805acfa0 2093 Note that an unchanging store can conflict with a non-unchanging read
2094 since we have to make conservative assumptions when we have a
2095 record with readonly fields and we are copying the whole thing.
15750b1e 2096 Just fall through to the code below to resolve potential conflicts.
2097 This won't handle all cases optimally, but the possible performance
2098 loss should be negligible. */
805acfa0 2099 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
ea0cb7ae 2100 return 0;
2101
ddb3d16f 2102 if (nonoverlapping_memrefs_p (mem, x))
2103 return 0;
2104
9fb1c31b 2105 if (mem_mode == VOIDmode)
2106 mem_mode = GET_MODE (mem);
2107
1617c276 2108 x_addr = get_addr (XEXP (x, 0));
2109 mem_addr = get_addr (XEXP (mem, 0));
2110
002fe3cb 2111 base = find_base_term (x_addr);
2112 if (base && (GET_CODE (base) == LABEL_REF
2113 || (GET_CODE (base) == SYMBOL_REF
2114 && CONSTANT_POOL_ADDRESS_P (base))))
2115 return 0;
2116
1617c276 2117 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
3ad3a0ed 2118 return 0;
2119
1617c276 2120 x_addr = canon_rtx (x_addr);
2121 mem_addr = canon_rtx (mem_addr);
35d3eedd 2122
4958fe6e 2123 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2124 SIZE_FOR_MODE (x), x_addr, 0))
2125 return 0;
2126
6a0934dd 2127 if (aliases_everything_p (x))
4958fe6e 2128 return 1;
2129
35a3065a 2130 /* We cannot use aliases_everything_p to test MEM, since we must look
6a0934dd 2131 at MEM_MODE, rather than GET_MODE (MEM). */
2132 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
8e802be9 2133 return 1;
2134
2135 /* In true_dependence we also allow BLKmode to alias anything. Why
2136 don't we do this in anti_dependence and output_dependence? */
2137 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2138 return 1;
2139
2140 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2141 varies);
2142}
2143
2144/* Canonical true dependence: X is read after store in MEM takes place.
9342ee68 2145 Variant of true_dependence which assumes MEM has already been
2146 canonicalized (hence we no longer do that here).
2147 The mem_addr argument has been added, since true_dependence computed
8e802be9 2148 this value prior to canonicalizing. */
2149
2150int
2151canon_true_dependence (mem, mem_mode, mem_addr, x, varies)
2152 rtx mem, mem_addr, x;
2153 enum machine_mode mem_mode;
2154 int (*varies) PARAMS ((rtx, int));
2155{
19cb6b50 2156 rtx x_addr;
8e802be9 2157
2158 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2159 return 1;
2160
d7b592b0 2161 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2162 This is used in epilogue deallocation functions. */
2163 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2164 return 1;
2165 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2166 return 1;
2167
8e802be9 2168 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2169 return 0;
2170
2171 /* If X is an unchanging read, then it can't possibly conflict with any
2172 non-unchanging store. It may conflict with an unchanging write though,
2173 because there may be a single store to this address to initialize it.
2174 Just fall through to the code below to resolve the case where we have
2175 both an unchanging read and an unchanging write. This won't handle all
2176 cases optimally, but the possible performance loss should be
2177 negligible. */
2178 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
2179 return 0;
2180
ddb3d16f 2181 if (nonoverlapping_memrefs_p (x, mem))
2182 return 0;
2183
8e802be9 2184 x_addr = get_addr (XEXP (x, 0));
2185
2186 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2187 return 0;
2188
2189 x_addr = canon_rtx (x_addr);
2190 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2191 SIZE_FOR_MODE (x), x_addr, 0))
2192 return 0;
2193
2194 if (aliases_everything_p (x))
2195 return 1;
2196
35a3065a 2197 /* We cannot use aliases_everything_p to test MEM, since we must look
8e802be9 2198 at MEM_MODE, rather than GET_MODE (MEM). */
2199 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
6a0934dd 2200 return 1;
4958fe6e 2201
6a0934dd 2202 /* In true_dependence we also allow BLKmode to alias anything. Why
2203 don't we do this in anti_dependence and output_dependence? */
2204 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2205 return 1;
4958fe6e 2206
1617c276 2207 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2208 varies);
ea0cb7ae 2209}
2210
d10cfa8d 2211/* Returns nonzero if a write to X might alias a previous read from
2212 (or, if WRITEP is nonzero, a write to) MEM. */
ea0cb7ae 2213
fe69e4fe 2214static int
6a0934dd 2215write_dependence_p (mem, x, writep)
ea0cb7ae 2216 rtx mem;
2217 rtx x;
6a0934dd 2218 int writep;
ea0cb7ae 2219{
35d3eedd 2220 rtx x_addr, mem_addr;
6a0934dd 2221 rtx fixed_scalar;
9ee7cac5 2222 rtx base;
35d3eedd 2223
ea0cb7ae 2224 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2225 return 1;
2226
a57f4c4d 2227 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2228 This is used in epilogue deallocation functions. */
2229 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2230 return 1;
2231 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2232 return 1;
2233
1617c276 2234 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2235 return 0;
2236
15750b1e 2237 /* Unchanging memory can't conflict with non-unchanging memory. */
2238 if (RTX_UNCHANGING_P (x) != RTX_UNCHANGING_P (mem))
2239 return 0;
2240
ea0cb7ae 2241 /* If MEM is an unchanging read, then it can't possibly conflict with
2242 the store to X, because there is at most one store to MEM, and it must
2243 have occurred somewhere before MEM. */
002fe3cb 2244 if (! writep && RTX_UNCHANGING_P (mem))
2245 return 0;
2246
ddb3d16f 2247 if (nonoverlapping_memrefs_p (x, mem))
2248 return 0;
2249
002fe3cb 2250 x_addr = get_addr (XEXP (x, 0));
2251 mem_addr = get_addr (XEXP (mem, 0));
2252
9ee7cac5 2253 if (! writep)
2254 {
002fe3cb 2255 base = find_base_term (mem_addr);
9ee7cac5 2256 if (base && (GET_CODE (base) == LABEL_REF
2257 || (GET_CODE (base) == SYMBOL_REF
2258 && CONSTANT_POOL_ADDRESS_P (base))))
2259 return 0;
2260 }
2261
1617c276 2262 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2263 GET_MODE (mem)))
b5ba9f3a 2264 return 0;
2265
1617c276 2266 x_addr = canon_rtx (x_addr);
2267 mem_addr = canon_rtx (mem_addr);
35d3eedd 2268
6a0934dd 2269 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2270 SIZE_FOR_MODE (x), x_addr, 0))
2271 return 0;
2272
9342ee68 2273 fixed_scalar
1617c276 2274 = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2275 rtx_addr_varies_p);
2276
6a0934dd 2277 return (!(fixed_scalar == mem && !aliases_everything_p (x))
2278 && !(fixed_scalar == x && !aliases_everything_p (mem)));
2279}
2280
2281/* Anti dependence: X is written after read in MEM takes place. */
2282
2283int
2284anti_dependence (mem, x)
2285 rtx mem;
2286 rtx x;
2287{
2288 return write_dependence_p (mem, x, /*writep=*/0);
ea0cb7ae 2289}
2290
2291/* Output dependence: X is written after store in MEM takes place. */
2292
2293int
2294output_dependence (mem, x)
19cb6b50 2295 rtx mem;
2296 rtx x;
ea0cb7ae 2297{
6a0934dd 2298 return write_dependence_p (mem, x, /*writep=*/1);
ea0cb7ae 2299}
c352cbc7 2300\f
2301/* A subroutine of nonlocal_mentioned_p, returns 1 if *LOC mentions
2302 something which is not local to the function and is not constant. */
142131ed 2303
2304static int
c352cbc7 2305nonlocal_mentioned_p_1 (loc, data)
2306 rtx *loc;
2307 void *data ATTRIBUTE_UNUSED;
142131ed 2308{
c352cbc7 2309 rtx x = *loc;
142131ed 2310 rtx base;
142131ed 2311 int regno;
2312
c352cbc7 2313 if (! x)
2314 return 0;
142131ed 2315
c352cbc7 2316 switch (GET_CODE (x))
142131ed 2317 {
2318 case SUBREG:
2319 if (GET_CODE (SUBREG_REG (x)) == REG)
2320 {
2321 /* Global registers are not local. */
2322 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
701e46d0 2323 && global_regs[subreg_regno (x)])
142131ed 2324 return 1;
2325 return 0;
2326 }
2327 break;
2328
2329 case REG:
2330 regno = REGNO (x);
2331 /* Global registers are not local. */
2332 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2333 return 1;
2334 return 0;
2335
2336 case SCRATCH:
2337 case PC:
2338 case CC0:
2339 case CONST_INT:
2340 case CONST_DOUBLE:
886cfd4f 2341 case CONST_VECTOR:
142131ed 2342 case CONST:
2343 case LABEL_REF:
2344 return 0;
2345
2346 case SYMBOL_REF:
2347 /* Constants in the function's constants pool are constant. */
2348 if (CONSTANT_POOL_ADDRESS_P (x))
2349 return 0;
2350 return 1;
2351
2352 case CALL:
1addbe73 2353 /* Non-constant calls and recursion are not local. */
142131ed 2354 return 1;
2355
2356 case MEM:
2357 /* Be overly conservative and consider any volatile memory
2358 reference as not local. */
2359 if (MEM_VOLATILE_P (x))
2360 return 1;
2361 base = find_base_term (XEXP (x, 0));
2362 if (base)
2363 {
3a935f06 2364 /* A Pmode ADDRESS could be a reference via the structure value
2365 address or static chain. Such memory references are nonlocal.
2366
2367 Thus, we have to examine the contents of the ADDRESS to find
2368 out if this is a local reference or not. */
2369 if (GET_CODE (base) == ADDRESS
2370 && GET_MODE (base) == Pmode
2371 && (XEXP (base, 0) == stack_pointer_rtx
2372 || XEXP (base, 0) == arg_pointer_rtx
2373#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2374 || XEXP (base, 0) == hard_frame_pointer_rtx
2375#endif
2376 || XEXP (base, 0) == frame_pointer_rtx))
142131ed 2377 return 0;
2378 /* Constants in the function's constant pool are constant. */
2379 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
2380 return 0;
2381 }
2382 return 1;
2383
1addbe73 2384 case UNSPEC_VOLATILE:
142131ed 2385 case ASM_INPUT:
142131ed 2386 return 1;
2387
1addbe73 2388 case ASM_OPERANDS:
2389 if (MEM_VOLATILE_P (x))
2390 return 1;
2391
2392 /* FALLTHROUGH */
2393
142131ed 2394 default:
2395 break;
2396 }
2397
c352cbc7 2398 return 0;
2399}
2400
d10cfa8d 2401/* Returns nonzero if X might mention something which is not
c352cbc7 2402 local to the function and is not constant. */
142131ed 2403
c352cbc7 2404static int
2405nonlocal_mentioned_p (x)
2406 rtx x;
2407{
2408
2409 if (INSN_P (x))
2410 {
2411 if (GET_CODE (x) == CALL_INSN)
2412 {
2413 if (! CONST_OR_PURE_CALL_P (x))
2414 return 1;
2415 x = CALL_INSN_FUNCTION_USAGE (x);
2416 if (x == 0)
2417 return 0;
9342ee68 2418 }
c352cbc7 2419 else
9342ee68 2420 x = PATTERN (x);
c352cbc7 2421 }
2422
2423 return for_each_rtx (&x, nonlocal_mentioned_p_1, NULL);
2424}
2425
2426/* A subroutine of nonlocal_referenced_p, returns 1 if *LOC references
2427 something which is not local to the function and is not constant. */
2428
2429static int
2430nonlocal_referenced_p_1 (loc, data)
2431 rtx *loc;
2432 void *data ATTRIBUTE_UNUSED;
2433{
2434 rtx x = *loc;
2435
2436 if (! x)
2437 return 0;
2438
2439 switch (GET_CODE (x))
2440 {
2441 case MEM:
2442 case REG:
2443 case SYMBOL_REF:
2444 case SUBREG:
2445 return nonlocal_mentioned_p (x);
2446
2447 case CALL:
2448 /* Non-constant calls and recursion are not local. */
2449 return 1;
2450
2451 case SET:
2452 if (nonlocal_mentioned_p (SET_SRC (x)))
2453 return 1;
2454
2455 if (GET_CODE (SET_DEST (x)) == MEM)
2456 return nonlocal_mentioned_p (XEXP (SET_DEST (x), 0));
2457
2458 /* If the destination is anything other than a CC0, PC,
2459 MEM, REG, or a SUBREG of a REG that occupies all of
2460 the REG, then X references nonlocal memory if it is
2461 mentioned in the destination. */
2462 if (GET_CODE (SET_DEST (x)) != CC0
2463 && GET_CODE (SET_DEST (x)) != PC
2464 && GET_CODE (SET_DEST (x)) != REG
2465 && ! (GET_CODE (SET_DEST (x)) == SUBREG
2466 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
2467 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
2468 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
2469 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2470 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
2471 return nonlocal_mentioned_p (SET_DEST (x));
2472 return 0;
2473
2474 case CLOBBER:
2475 if (GET_CODE (XEXP (x, 0)) == MEM)
2476 return nonlocal_mentioned_p (XEXP (XEXP (x, 0), 0));
2477 return 0;
2478
2479 case USE:
2480 return nonlocal_mentioned_p (XEXP (x, 0));
2481
2482 case ASM_INPUT:
2483 case UNSPEC_VOLATILE:
2484 return 1;
2485
2486 case ASM_OPERANDS:
2487 if (MEM_VOLATILE_P (x))
2488 return 1;
2489
2490 /* FALLTHROUGH */
2491
2492 default:
2493 break;
2494 }
2495
2496 return 0;
2497}
2498
d10cfa8d 2499/* Returns nonzero if X might reference something which is not
c352cbc7 2500 local to the function and is not constant. */
2501
2502static int
2503nonlocal_referenced_p (x)
2504 rtx x;
2505{
2506
2507 if (INSN_P (x))
2508 {
2509 if (GET_CODE (x) == CALL_INSN)
2510 {
2511 if (! CONST_OR_PURE_CALL_P (x))
2512 return 1;
2513 x = CALL_INSN_FUNCTION_USAGE (x);
2514 if (x == 0)
2515 return 0;
9342ee68 2516 }
c352cbc7 2517 else
9342ee68 2518 x = PATTERN (x);
c352cbc7 2519 }
2520
2521 return for_each_rtx (&x, nonlocal_referenced_p_1, NULL);
2522}
2523
2524/* A subroutine of nonlocal_set_p, returns 1 if *LOC sets
2525 something which is not local to the function and is not constant. */
2526
2527static int
2528nonlocal_set_p_1 (loc, data)
2529 rtx *loc;
2530 void *data ATTRIBUTE_UNUSED;
2531{
2532 rtx x = *loc;
2533
2534 if (! x)
2535 return 0;
2536
2537 switch (GET_CODE (x))
2538 {
2539 case CALL:
2540 /* Non-constant calls and recursion are not local. */
2541 return 1;
2542
2543 case PRE_INC:
2544 case PRE_DEC:
2545 case POST_INC:
2546 case POST_DEC:
2547 case PRE_MODIFY:
2548 case POST_MODIFY:
2549 return nonlocal_mentioned_p (XEXP (x, 0));
2550
2551 case SET:
2552 if (nonlocal_mentioned_p (SET_DEST (x)))
2553 return 1;
2554 return nonlocal_set_p (SET_SRC (x));
2555
2556 case CLOBBER:
2557 return nonlocal_mentioned_p (XEXP (x, 0));
2558
2559 case USE:
2560 return 0;
2561
2562 case ASM_INPUT:
2563 case UNSPEC_VOLATILE:
2564 return 1;
2565
2566 case ASM_OPERANDS:
2567 if (MEM_VOLATILE_P (x))
2568 return 1;
2569
2570 /* FALLTHROUGH */
2571
2572 default:
2573 break;
2574 }
142131ed 2575
2576 return 0;
2577}
2578
d10cfa8d 2579/* Returns nonzero if X might set something which is not
c352cbc7 2580 local to the function and is not constant. */
2581
2582static int
2583nonlocal_set_p (x)
2584 rtx x;
2585{
2586
2587 if (INSN_P (x))
2588 {
2589 if (GET_CODE (x) == CALL_INSN)
2590 {
2591 if (! CONST_OR_PURE_CALL_P (x))
2592 return 1;
2593 x = CALL_INSN_FUNCTION_USAGE (x);
2594 if (x == 0)
2595 return 0;
9342ee68 2596 }
c352cbc7 2597 else
9342ee68 2598 x = PATTERN (x);
c352cbc7 2599 }
2600
2601 return for_each_rtx (&x, nonlocal_set_p_1, NULL);
2602}
2603
142131ed 2604/* Mark the function if it is constant. */
2605
2606void
2607mark_constant_function ()
2608{
2609 rtx insn;
c352cbc7 2610 int nonlocal_memory_referenced;
142131ed 2611
4f8e9d5c 2612 if (TREE_READONLY (current_function_decl)
1addbe73 2613 || DECL_IS_PURE (current_function_decl)
142131ed 2614 || TREE_THIS_VOLATILE (current_function_decl)
c352cbc7 2615 || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode
4f8e9d5c 2616 || current_function_has_nonlocal_goto
2617 || !(*targetm.binds_local_p) (current_function_decl))
142131ed 2618 return;
2619
080f162d 2620 /* A loop might not return which counts as a side effect. */
ecb7b891 2621 if (mark_dfs_back_edges ())
080f162d 2622 return;
2623
c352cbc7 2624 nonlocal_memory_referenced = 0;
1addbe73 2625
2626 init_alias_analysis ();
2627
c352cbc7 2628 /* Determine if this is a constant or pure function. */
142131ed 2629
2630 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
c352cbc7 2631 {
2632 if (! INSN_P (insn))
2633 continue;
1addbe73 2634
c352cbc7 2635 if (nonlocal_set_p (insn) || global_reg_mentioned_p (insn)
2636 || volatile_refs_p (PATTERN (insn)))
9342ee68 2637 break;
142131ed 2638
c352cbc7 2639 if (! nonlocal_memory_referenced)
2640 nonlocal_memory_referenced = nonlocal_referenced_p (insn);
2641 }
9342ee68 2642
c352cbc7 2643 end_alias_analysis ();
9342ee68 2644
142131ed 2645 /* Mark the function. */
9342ee68 2646
c352cbc7 2647 if (insn)
2648 ;
2649 else if (nonlocal_memory_referenced)
2650 DECL_IS_PURE (current_function_decl) = 1;
2651 else
1addbe73 2652 TREE_READONLY (current_function_decl) = 1;
142131ed 2653}
c352cbc7 2654\f
35d3eedd 2655
35d3eedd 2656void
2657init_alias_once ()
2658{
19cb6b50 2659 int i;
35d3eedd 2660
2661#ifndef OUTGOING_REGNO
2662#define OUTGOING_REGNO(N) N
2663#endif
2664 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2665 /* Check whether this register can hold an incoming pointer
2666 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2c0e001b 2667 numbers, so translate if necessary due to register windows. */
35d3eedd 2668 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2669 && HARD_REGNO_MODE_OK (i, Pmode))
0c7f5242 2670 static_reg_base_value[i]
2671 = gen_rtx_ADDRESS (VOIDmode, gen_rtx_REG (Pmode, i));
2672
0c7f5242 2673 static_reg_base_value[STACK_POINTER_REGNUM]
2674 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
2675 static_reg_base_value[ARG_POINTER_REGNUM]
2676 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
2677 static_reg_base_value[FRAME_POINTER_REGNUM]
2678 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
2679#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2680 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2681 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
2682#endif
e26fd9bc 2683
2684 alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
0c7f5242 2685}
2686
73f5c1e3 2687/* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2688 array. */
2689
ea0cb7ae 2690void
2691init_alias_analysis ()
2692{
2693 int maxreg = max_reg_num ();
37b864db 2694 int changed, pass;
19cb6b50 2695 int i;
2696 unsigned int ui;
2697 rtx insn;
ea0cb7ae 2698
2699 reg_known_value_size = maxreg;
2700
9342ee68 2701 reg_known_value
7098ef4a 2702 = (rtx *) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (rtx))
2703 - FIRST_PSEUDO_REGISTER;
9342ee68 2704 reg_known_equiv_p
7098ef4a 2705 = (char*) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (char))
ea0cb7ae 2706 - FIRST_PSEUDO_REGISTER;
ea0cb7ae 2707
35d3eedd 2708 /* Overallocate reg_base_value to allow some growth during loop
2709 optimization. Loop unrolling can create a large number of
2710 registers. */
2711 reg_base_value_size = maxreg * 2;
1f3233d1 2712 reg_base_value = (rtx *) ggc_alloc_cleared (reg_base_value_size
2713 * sizeof (rtx));
c2e94b25 2714
7098ef4a 2715 new_reg_base_value = (rtx *) xmalloc (reg_base_value_size * sizeof (rtx));
2716 reg_seen = (char *) xmalloc (reg_base_value_size);
1a6c3add 2717 if (! reload_completed && flag_unroll_loops)
2718 {
c2e94b25 2719 /* ??? Why are we realloc'ing if we're just going to zero it? */
1a6c3add 2720 alias_invariant = (rtx *)xrealloc (alias_invariant,
2721 reg_base_value_size * sizeof (rtx));
93d3b7de 2722 memset ((char *)alias_invariant, 0, reg_base_value_size * sizeof (rtx));
1a6c3add 2723 }
06c5c535 2724
2725 /* The basic idea is that each pass through this loop will use the
2726 "constant" information from the previous pass to propagate alias
2727 information through another level of assignments.
2728
2729 This could get expensive if the assignment chains are long. Maybe
2730 we should throttle the number of iterations, possibly based on
35d3eedd 2731 the optimization level or flag_expensive_optimizations.
06c5c535 2732
2733 We could propagate more information in the first pass by making use
2734 of REG_N_SETS to determine immediately that the alias information
37b864db 2735 for a pseudo is "constant".
2736
2737 A program with an uninitialized variable can cause an infinite loop
2738 here. Instead of doing a full dataflow analysis to detect such problems
2739 we just cap the number of iterations for the loop.
2740
2741 The state of the arrays for the set chain in question does not matter
2742 since the program has undefined behavior. */
35d3eedd 2743
37b864db 2744 pass = 0;
35d3eedd 2745 do
06c5c535 2746 {
2747 /* Assume nothing will change this iteration of the loop. */
2748 changed = 0;
2749
06c5c535 2750 /* We want to assign the same IDs each iteration of this loop, so
2751 start counting from zero each iteration of the loop. */
2752 unique_id = 0;
2753
35a3065a 2754 /* We're at the start of the function each iteration through the
06c5c535 2755 loop, so we're copying arguments. */
e5be0cc6 2756 copying_arguments = true;
ea0cb7ae 2757
35d3eedd 2758 /* Wipe the potential alias information clean for this pass. */
93d3b7de 2759 memset ((char *) new_reg_base_value, 0, reg_base_value_size * sizeof (rtx));
f0ad0992 2760
35d3eedd 2761 /* Wipe the reg_seen array clean. */
93d3b7de 2762 memset ((char *) reg_seen, 0, reg_base_value_size);
ea0cb7ae 2763
35d3eedd 2764 /* Mark all hard registers which may contain an address.
2765 The stack, frame and argument pointers may contain an address.
2766 An argument register which can hold a Pmode value may contain
2767 an address even if it is not in BASE_REGS.
f0ad0992 2768
35d3eedd 2769 The address expression is VOIDmode for an argument and
2770 Pmode for other registers. */
2771
e26fd9bc 2772 memcpy (new_reg_base_value, static_reg_base_value,
2773 FIRST_PSEUDO_REGISTER * sizeof (rtx));
35d3eedd 2774
06c5c535 2775 /* Walk the insns adding values to the new_reg_base_value array. */
2776 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
ea0cb7ae 2777 {
9204e736 2778 if (INSN_P (insn))
06c5c535 2779 {
35d3eedd 2780 rtx note, set;
d6cb6164 2781
2782#if defined (HAVE_prologue) || defined (HAVE_epilogue)
35a3065a 2783 /* The prologue/epilogue insns are not threaded onto the
fd037eda 2784 insn chain until after reload has completed. Thus,
2785 there is no sense wasting time checking if INSN is in
2786 the prologue/epilogue until after reload has completed. */
2787 if (reload_completed
2788 && prologue_epilogue_contains (insn))
d6cb6164 2789 continue;
2790#endif
2791
06c5c535 2792 /* If this insn has a noalias note, process it, Otherwise,
2793 scan for sets. A simple set will have no side effects
2c0e001b 2794 which could change the base value of any other register. */
35d3eedd 2795
06c5c535 2796 if (GET_CODE (PATTERN (insn)) == SET
d6cb6164 2797 && REG_NOTES (insn) != 0
2798 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
ec8895d7 2799 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
06c5c535 2800 else
ec8895d7 2801 note_stores (PATTERN (insn), record_set, NULL);
35d3eedd 2802
2803 set = single_set (insn);
2804
2805 if (set != 0
2806 && GET_CODE (SET_DEST (set)) == REG
9db10305 2807 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
35d3eedd 2808 {
9db10305 2809 unsigned int regno = REGNO (SET_DEST (set));
970c0c06 2810 rtx src = SET_SRC (set);
2811
2812 if (REG_NOTES (insn) != 0
2813 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2814 && REG_N_SETS (regno) == 1)
2815 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
2816 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
97fb4325 2817 && ! rtx_varies_p (XEXP (note, 0), 1)
970c0c06 2818 && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
2819 {
2820 reg_known_value[regno] = XEXP (note, 0);
2821 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
2822 }
2823 else if (REG_N_SETS (regno) == 1
2824 && GET_CODE (src) == PLUS
2825 && GET_CODE (XEXP (src, 0)) == REG
9db10305 2826 && REGNO (XEXP (src, 0)) >= FIRST_PSEUDO_REGISTER
2827 && (reg_known_value[REGNO (XEXP (src, 0))])
970c0c06 2828 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2829 {
2830 rtx op0 = XEXP (src, 0);
97fb4325 2831 op0 = reg_known_value[REGNO (op0)];
970c0c06 2832 reg_known_value[regno]
b244d4c7 2833 = plus_constant (op0, INTVAL (XEXP (src, 1)));
970c0c06 2834 reg_known_equiv_p[regno] = 0;
2835 }
2836 else if (REG_N_SETS (regno) == 1
2837 && ! rtx_varies_p (src, 1))
2838 {
2839 reg_known_value[regno] = src;
2840 reg_known_equiv_p[regno] = 0;
2841 }
35d3eedd 2842 }
06c5c535 2843 }
2844 else if (GET_CODE (insn) == NOTE
2845 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
e5be0cc6 2846 copying_arguments = false;
35d3eedd 2847 }
06c5c535 2848
35d3eedd 2849 /* Now propagate values from new_reg_base_value to reg_base_value. */
274c11d8 2850 for (ui = 0; ui < reg_base_value_size; ui++)
35d3eedd 2851 {
274c11d8 2852 if (new_reg_base_value[ui]
2853 && new_reg_base_value[ui] != reg_base_value[ui]
2854 && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
06c5c535 2855 {
274c11d8 2856 reg_base_value[ui] = new_reg_base_value[ui];
35d3eedd 2857 changed = 1;
06c5c535 2858 }
ea0cb7ae 2859 }
ea0cb7ae 2860 }
35d3eedd 2861 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
ea0cb7ae 2862
2863 /* Fill in the remaining entries. */
2864 for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
2865 if (reg_known_value[i] == 0)
2866 reg_known_value[i] = regno_reg_rtx[i];
2867
ea0cb7ae 2868 /* Simplify the reg_base_value array so that no register refers to
2869 another register, except to special registers indirectly through
2870 ADDRESS expressions.
2871
2872 In theory this loop can take as long as O(registers^2), but unless
2873 there are very long dependency chains it will run in close to linear
37b864db 2874 time.
2875
2876 This loop may not be needed any longer now that the main loop does
2877 a better job at propagating alias information. */
2878 pass = 0;
ea0cb7ae 2879 do
2880 {
2881 changed = 0;
37b864db 2882 pass++;
274c11d8 2883 for (ui = 0; ui < reg_base_value_size; ui++)
ea0cb7ae 2884 {
274c11d8 2885 rtx base = reg_base_value[ui];
ea0cb7ae 2886 if (base && GET_CODE (base) == REG)
2887 {
9db10305 2888 unsigned int base_regno = REGNO (base);
274c11d8 2889 if (base_regno == ui) /* register set from itself */
2890 reg_base_value[ui] = 0;
ea0cb7ae 2891 else
274c11d8 2892 reg_base_value[ui] = reg_base_value[base_regno];
ea0cb7ae 2893 changed = 1;
2894 }
2895 }
2896 }
37b864db 2897 while (changed && pass < MAX_ALIAS_LOOP_PASSES);
ea0cb7ae 2898
7098ef4a 2899 /* Clean up. */
2900 free (new_reg_base_value);
06c5c535 2901 new_reg_base_value = 0;
7098ef4a 2902 free (reg_seen);
ea0cb7ae 2903 reg_seen = 0;
2904}
2905
2906void
2907end_alias_analysis ()
2908{
7098ef4a 2909 free (reg_known_value + FIRST_PSEUDO_REGISTER);
ea0cb7ae 2910 reg_known_value = 0;
c2e94b25 2911 reg_known_value_size = 0;
7098ef4a 2912 free (reg_known_equiv_p + FIRST_PSEUDO_REGISTER);
2913 reg_known_equiv_p = 0;
1f3233d1 2914 reg_base_value = 0;
ea0cb7ae 2915 reg_base_value_size = 0;
1a6c3add 2916 if (alias_invariant)
2917 {
c2e94b25 2918 free (alias_invariant);
1a6c3add 2919 alias_invariant = 0;
2920 }
ea0cb7ae 2921}
1f3233d1 2922
2923#include "gt-alias.h"