]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/alias.c
* configure.in: Add AC_PREREQ for consistency.
[thirdparty/gcc.git] / gcc / alias.c
CommitLineData
9ae8ffe7 1/* Alias analysis for GNU C
a3b88570 2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
9ae8ffe7
JL
3 Contributed by John Carr (jfc@mit.edu).
4
1322177d 5This file is part of GCC.
9ae8ffe7 6
1322177d
LB
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.
9ae8ffe7 11
1322177d
LB
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.
9ae8ffe7
JL
16
17You should have received a copy of the GNU General Public License
1322177d
LB
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. */
9ae8ffe7
JL
21
22#include "config.h"
670ee920 23#include "system.h"
4977bab6
ZW
24#include "coretypes.h"
25#include "tm.h"
9ae8ffe7 26#include "rtl.h"
7790df19 27#include "tree.h"
6baf1cc8 28#include "tm_p.h"
49ad7cfa 29#include "function.h"
9ae8ffe7
JL
30#include "expr.h"
31#include "regs.h"
32#include "hard-reg-set.h"
e004f2f7 33#include "basic-block.h"
9ae8ffe7 34#include "flags.h"
264fac34 35#include "output.h"
2e107e9e 36#include "toplev.h"
eab5c70a 37#include "cselib.h"
3932261a 38#include "splay-tree.h"
ac606739 39#include "ggc.h"
d23c55c2 40#include "langhooks.h"
ab780373 41#include "target.h"
3932261a
MM
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
ac3d9668
RK
45 different alias sets cannot alias each other, with one important
46 exception. Consider something like:
3932261a
MM
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
ac3d9668
RK
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
3bdf5ad1
RK
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
e3aafbad 66 past immediate descendents, however, since we propagate all
3bdf5ad1 67 grandchildren up one level.
3932261a
MM
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
d4b60170
RK
73typedef struct alias_set_entry
74{
3932261a 75 /* The alias set number, as stored in MEM_ALIAS_SET. */
3bdf5ad1 76 HOST_WIDE_INT alias_set;
3932261a
MM
77
78 /* The children of the alias set. These are not just the immediate
e3aafbad 79 children, but, in fact, all descendents. So, if we have:
3932261a 80
ca7fd9cd 81 struct T { struct S s; float f; }
3932261a
MM
82
83 continuing our example above, the children here will be all of
84 `int', `double', `float', and `struct S'. */
85 splay_tree children;
2bf105ab
RK
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;
d4b60170 90} *alias_set_entry;
9ae8ffe7 91
3d994c6b
KG
92static int rtx_equal_for_memref_p PARAMS ((rtx, rtx));
93static rtx find_symbolic_term PARAMS ((rtx));
a13d4ebf 94rtx get_addr PARAMS ((rtx));
3d994c6b
KG
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*));
738cc472 104static tree find_base_decl PARAMS ((tree));
3bdf5ad1 105static alias_set_entry get_alias_set_entry PARAMS ((HOST_WIDE_INT));
eab5c70a 106static rtx fixed_scalar_and_varying_struct_p PARAMS ((rtx, rtx, rtx, rtx,
e38fe8e0 107 int (*) (rtx, int)));
3d994c6b 108static int aliases_everything_p PARAMS ((rtx));
998d7deb
RH
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));
a4311dfe 112static int nonoverlapping_memrefs_p PARAMS ((rtx, rtx));
3d994c6b 113static int write_dependence_p PARAMS ((rtx, rtx, int));
c14b9960
JW
114
115static int nonlocal_mentioned_p_1 PARAMS ((rtx *, void *));
bf6d9fd7 116static int nonlocal_mentioned_p PARAMS ((rtx));
c14b9960
JW
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));
9ae8ffe7
JL
121
122/* Set up all info needed to perform alias analysis on memory references. */
123
d4b60170 124/* Returns the size in bytes of the mode of X. */
9ae8ffe7
JL
125#define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
126
41472af8 127/* Returns nonzero if MEM1 and MEM2 do not alias because they are in
264fac34
MM
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) \
3932261a 132 mems_in_disjoint_alias_sets_p (MEM1, MEM2)
41472af8 133
ea64ef27 134/* Cap the number of passes we make over the insns propagating alias
ac3d9668 135 information through set chains. 10 is a completely arbitrary choice. */
ea64ef27 136#define MAX_ALIAS_LOOP_PASSES 10
ca7fd9cd 137
9ae8ffe7
JL
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
2a2c8203
JC
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
ca7fd9cd 146 the stack, frame, and argument pointers.
b3b5ad95
JL
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. */
2a2c8203 156
e2500fed 157static GTY((length ("reg_base_value_size"))) rtx *reg_base_value;
ac606739 158static rtx *new_reg_base_value;
d4b60170
RK
159static unsigned int reg_base_value_size; /* size of reg_base_value array */
160
bf1660a6
JL
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
9ae8ffe7 165#define REG_BASE_VALUE(X) \
fb6754f0
BS
166 (REGNO (X) < reg_base_value_size \
167 ? reg_base_value[REGNO (X)] : 0)
9ae8ffe7 168
de12be17
JC
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
c13e8210
MM
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. */
9ae8ffe7
JL
183rtx *reg_known_value;
184
185/* Indicates number of valid entries in reg_known_value. */
770ae6cc 186static unsigned int reg_known_value_size;
9ae8ffe7
JL
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
ac3d9668
RK
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. */
9ae8ffe7
JL
200char *reg_known_equiv_p;
201
2a2c8203
JC
202/* True when scanning insns from the start of the rtl to the
203 NOTE_INSN_FUNCTION_BEG note. */
83bbd9b6 204static bool copying_arguments;
9ae8ffe7 205
3932261a 206/* The splay-tree used to store the various alias set entries. */
3932261a 207static splay_tree alias_sets;
ac3d9668 208\f
3932261a
MM
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)
3bdf5ad1 214 HOST_WIDE_INT alias_set;
3932261a 215{
d4b60170
RK
216 splay_tree_node sn
217 = splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
3932261a 218
d4b60170 219 return sn != 0 ? ((alias_set_entry) sn->value) : 0;
3932261a
MM
220}
221
ac3d9668
RK
222/* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
223 the two MEMs cannot alias each other. */
3932261a 224
ca7fd9cd 225static int
3932261a
MM
226mems_in_disjoint_alias_sets_p (mem1, mem2)
227 rtx mem1;
228 rtx mem2;
229{
ca7fd9cd 230#ifdef ENABLE_CHECKING
3932261a
MM
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. */
d4b60170
RK
238 if (! flag_strict_aliasing
239 && (MEM_ALIAS_SET (mem1) != 0 || MEM_ALIAS_SET (mem2) != 0))
3932261a
MM
240 abort ();
241#endif
242
1da68f56
RK
243 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
244}
3932261a 245
1da68f56
RK
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;
3932261a 273
3bdf5ad1 274 /* See if the first alias set is a subset of the second. */
1da68f56 275 ase = get_alias_set_entry (set1);
2bf105ab
RK
276 if (ase != 0
277 && (ase->has_zero_child
278 || splay_tree_lookup (ase->children,
1da68f56
RK
279 (splay_tree_key) set2)))
280 return 1;
3932261a
MM
281
282 /* Now do the same, but with the alias sets reversed. */
1da68f56 283 ase = get_alias_set_entry (set2);
2bf105ab
RK
284 if (ase != 0
285 && (ase->has_zero_child
286 || splay_tree_lookup (ase->children,
1da68f56
RK
287 (splay_tree_key) set1)))
288 return 1;
3932261a 289
1da68f56 290 /* The two alias sets are distinct and neither one is the
3932261a 291 child of the other. Therefore, they cannot alias. */
1da68f56 292 return 0;
3932261a 293}
1da68f56
RK
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. */
3932261a 298
1da68f56
RK
299int
300readonly_fields_p (type)
301 tree type;
3932261a 302{
1da68f56
RK
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;
3932261a
MM
314
315 return 0;
316}
3bdf5ad1 317\f
1da68f56
RK
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{
e8ea2809
RK
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. */
981a4c34 330 if (t1 == 0 && t2 == 0)
e8ea2809
RK
331 return 0;
332
66cce54d
RH
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))
a3b88570
MM
337 || (t1 != 0 && lang_hooks.honor_readonly && TYPE_READONLY (t1))
338 || (t2 != 0 && lang_hooks.honor_readonly && TYPE_READONLY (t2)))
66cce54d
RH
339 return 0;
340
1da68f56
RK
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
66cce54d
RH
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)))
1da68f56
RK
351 return 0;
352
ec5c56db 353 /* Otherwise they conflict only if the alias sets conflict. */
1da68f56
RK
354 return alias_sets_conflict_p (t1 ? get_alias_set (t1) : 0,
355 t2 ? get_alias_set (t2) : 0);
356}
357\f
3bdf5ad1
RK
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,
f5143c46 361 NULL_TREE is returned. */
3bdf5ad1
RK
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));
3bdf5ad1
RK
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
6e24b709
RK
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
10b76d73 420int
6e24b709
RK
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
8ac61af7
RK
432 /* Fields are addressable unless they are marked as nonaddressable or
433 the containing type has alias set 0. */
6e24b709
RK
434 else if (TREE_CODE (t) == COMPONENT_REF
435 && ! DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1))
8ac61af7 436 && get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) != 0
6e24b709
RK
437 && can_address_p (TREE_OPERAND (t, 0)))
438 return 1;
439
8ac61af7 440 /* Likewise for arrays. */
b4e3fabb 441 else if ((TREE_CODE (t) == ARRAY_REF || TREE_CODE (t) == ARRAY_RANGE_REF)
6e24b709 442 && ! TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0)))
8ac61af7 443 && get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) != 0
6e24b709
RK
444 && can_address_p (TREE_OPERAND (t, 0)))
445 return 1;
446
447 return 0;
448}
449
3bdf5ad1
RK
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;
3bdf5ad1
RK
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
f47e9b4e
RK
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
738cc472 471 aren't types. */
f824e5c3 472 if (! TYPE_P (t))
3bdf5ad1 473 {
738cc472
RK
474 tree inner = t;
475 tree placeholder_ptr = 0;
476
8ac61af7
RK
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
738cc472 484 /* First see if the actual object referenced is an INDIRECT_REF from a
8ac61af7 485 restrict-qualified pointer or a "void *". Replace
738cc472 486 PLACEHOLDER_EXPRs. */
8ac61af7 487 while (TREE_CODE (inner) == PLACEHOLDER_EXPR
738cc472
RK
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);
8ac61af7
RK
494
495 STRIP_NOPS (inner);
738cc472
RK
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 {
e5837c07 505 /* If we haven't computed the actual alias set, do it now. */
738cc472
RK
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 ();
ca7fd9cd
KH
524 record_alias_subset (pointed_to_alias_set,
525 DECL_POINTER_ALIAS_SET (decl));
738cc472
RK
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;
8ac61af7 542 while (TREE_CODE (t) == PLACEHOLDER_EXPR
f47e9b4e
RK
543 || (handled_component_p (t) && ! can_address_p (t)))
544 {
f47e9b4e 545 if (TREE_CODE (t) == PLACEHOLDER_EXPR)
738cc472 546 t = find_placeholder (t, &placeholder_ptr);
f47e9b4e
RK
547 else
548 t = TREE_OPERAND (t, 0);
f824e5c3 549
8ac61af7
RK
550 STRIP_NOPS (t);
551 }
f824e5c3 552
738cc472
RK
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!). */
5755cd38
JM
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
f824e5c3
RK
560 /* Now all we care about is the type. */
561 t = TREE_TYPE (t);
3bdf5ad1
RK
562 }
563
3bdf5ad1
RK
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);
738cc472 567 if (TYPE_ALIAS_SET_KNOWN_P (t))
3bdf5ad1
RK
568 return TYPE_ALIAS_SET (t);
569
570 /* See if the language has special handling for this type. */
8ac61af7
RK
571 set = (*lang_hooks.get_alias_set) (t);
572 if (set != -1)
738cc472 573 return set;
2bf105ab 574
3bdf5ad1
RK
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;
74d86f4f
RH
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
3bdf5ad1
RK
588 else
589 /* Otherwise make a new alias set for this type. */
590 set = new_alias_set ();
591
592 TYPE_ALIAS_SET (t) = set;
2bf105ab
RK
593
594 /* If this is an aggregate type, we must record any component aliasing
595 information. */
1d79fd2c 596 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
2bf105ab
RK
597 record_component_aliases (t);
598
3bdf5ad1
RK
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}
3932261a
MM
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
ca7fd9cd 619 function should be called only once per SUPERSET/SUBSET pair.
3932261a
MM
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)
3bdf5ad1
RK
626 HOST_WIDE_INT superset;
627 HOST_WIDE_INT subset;
3932261a
MM
628{
629 alias_set_entry superset_entry;
630 alias_set_entry subset_entry;
631
f47e9b4e
RK
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
3932261a
MM
637 if (superset == 0)
638 abort ();
639
640 superset_entry = get_alias_set_entry (superset);
ca7fd9cd 641 if (superset_entry == 0)
3932261a
MM
642 {
643 /* Create an entry for the SUPERSET, so that we have a place to
644 attach the SUBSET. */
d4b60170
RK
645 superset_entry
646 = (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
3932261a 647 superset_entry->alias_set = superset;
ca7fd9cd 648 superset_entry->children
30f72379 649 = splay_tree_new (splay_tree_compare_ints, 0, 0);
570eb5c8 650 superset_entry->has_zero_child = 0;
d4b60170 651 splay_tree_insert (alias_sets, (splay_tree_key) superset,
3932261a 652 (splay_tree_value) superset_entry);
3932261a
MM
653 }
654
2bf105ab
RK
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. */
ca7fd9cd 662 if (subset_entry)
2bf105ab
RK
663 {
664 if (subset_entry->has_zero_child)
665 superset_entry->has_zero_child = 1;
d4b60170 666
2bf105ab
RK
667 splay_tree_foreach (subset_entry->children, insert_subset_children,
668 superset_entry->children);
669 }
3932261a 670
2bf105ab 671 /* Enter the SUBSET itself as a child of the SUPERSET. */
ca7fd9cd 672 splay_tree_insert (superset_entry->children,
2bf105ab
RK
673 (splay_tree_key) subset, 0);
674 }
3932261a
MM
675}
676
a0c33338
RK
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{
3bdf5ad1 687 HOST_WIDE_INT superset = get_alias_set (type);
a0c33338
RK
688 tree field;
689
690 if (superset == 0)
691 return;
692
693 switch (TREE_CODE (type))
694 {
695 case ARRAY_TYPE:
2bf105ab
RK
696 if (! TYPE_NONALIASED_COMPONENT (type))
697 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
a0c33338
RK
698 break;
699
700 case RECORD_TYPE:
701 case UNION_TYPE:
702 case QUAL_UNION_TYPE:
61eece67
DN
703 /* Recursively record aliases for the base classes, if there are any */
704 if (TYPE_BINFO (type) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
ca7fd9cd
KH
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,
61eece67 711 get_alias_set (BINFO_TYPE (binfo)));
ca7fd9cd
KH
712 }
713 }
a0c33338 714 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
b16a49a1 715 if (TREE_CODE (field) == FIELD_DECL && ! DECL_NONADDRESSABLE_P (field))
2bf105ab 716 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
a0c33338
RK
717 break;
718
1d79fd2c
JW
719 case COMPLEX_TYPE:
720 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
721 break;
722
a0c33338
RK
723 default:
724 break;
725 }
726}
727
3bdf5ad1
RK
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
2a2c8203
JC
756/* Inside SRC, the source of a SET, find a base address. */
757
9ae8ffe7
JL
758static rtx
759find_base_value (src)
b3694847 760 rtx src;
9ae8ffe7 761{
713f41f9 762 unsigned int regno;
0aacc8ed 763
9ae8ffe7
JL
764 switch (GET_CODE (src))
765 {
766 case SYMBOL_REF:
767 case LABEL_REF:
768 return src;
769
770 case REG:
fb6754f0 771 regno = REGNO (src);
d4b60170 772 /* At the start of a function, argument registers have known base
2a2c8203
JC
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. */
713f41f9
BS
776 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
777 return new_reg_base_value[regno];
73774bc7 778
eaf407a5 779 /* If a pseudo has a known base value, return it. Do not do this
9b462c42
RH
780 for non-fixed hard regs since it can result in a circular
781 dependency chain for registers which have values at function entry.
eaf407a5
JL
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. */
9b462c42 785 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
83bbd9b6
RH
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 }
73774bc7 796
9ae8ffe7
JL
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)))
38a448ca 807 return gen_rtx_ADDRESS (VOIDmode, src);
9ae8ffe7
JL
808 return 0;
809
810 case CONST:
811 src = XEXP (src, 0);
812 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
813 break;
d4b60170 814
ec5c56db 815 /* ... fall through ... */
2a2c8203 816
9ae8ffe7
JL
817 case PLUS:
818 case MINUS:
2a2c8203 819 {
ec907dd8
JL
820 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
821
0134bf2d
DE
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
ec907dd8
JL
829 /* If either operand is a REG, then see if we already have
830 a known value for it. */
0134bf2d 831 if (REG_P (src_0))
ec907dd8
JL
832 {
833 temp = find_base_value (src_0);
d4b60170 834 if (temp != 0)
ec907dd8
JL
835 src_0 = temp;
836 }
837
0134bf2d 838 if (REG_P (src_1))
ec907dd8
JL
839 {
840 temp = find_base_value (src_1);
d4b60170 841 if (temp!= 0)
ec907dd8
JL
842 src_1 = temp;
843 }
2a2c8203 844
0134bf2d
DE
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
d4b60170 862 /* Guess which operand is the base address:
ec907dd8
JL
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. */
d4b60170 865 if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
2a2c8203 866 return find_base_value (src_0);
d4b60170 867 else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
ec907dd8
JL
868 return find_base_value (src_1);
869
9ae8ffe7 870 return 0;
2a2c8203
JC
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));
9ae8ffe7
JL
877
878 case AND:
879 /* If the second operand is constant set the base
ec5c56db 880 address to the first operand. */
2a2c8203
JC
881 if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
882 return find_base_value (XEXP (src, 0));
9ae8ffe7
JL
883 return 0;
884
61f0131c
R
885 case TRUNCATE:
886 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
887 break;
888 /* Fall through. */
9ae8ffe7 889 case HIGH:
d288e53d
DE
890 case PRE_INC:
891 case PRE_DEC:
892 case POST_INC:
893 case POST_DEC:
894 case PRE_MODIFY:
895 case POST_MODIFY:
2a2c8203 896 return find_base_value (XEXP (src, 0));
1d300e19 897
0aacc8ed
RK
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
1d300e19
KG
911 default:
912 break;
9ae8ffe7
JL
913 }
914
915 return 0;
916}
917
918/* Called from init_alias_analysis indirectly through note_stores. */
919
d4b60170 920/* While scanning insns to find base values, reg_seen[N] is nonzero if
9ae8ffe7
JL
921 register N has been set in this function. */
922static char *reg_seen;
923
13309a5f
JC
924/* Addresses which are known not to alias anything else are identified
925 by a unique integer. */
ec907dd8
JL
926static int unique_id;
927
2a2c8203 928static void
84832317 929record_set (dest, set, data)
9ae8ffe7 930 rtx dest, set;
84832317 931 void *data ATTRIBUTE_UNUSED;
9ae8ffe7 932{
b3694847 933 unsigned regno;
9ae8ffe7 934 rtx src;
c28b4e40 935 int n;
9ae8ffe7
JL
936
937 if (GET_CODE (dest) != REG)
938 return;
939
fb6754f0 940 regno = REGNO (dest);
9ae8ffe7 941
ac606739
GS
942 if (regno >= reg_base_value_size)
943 abort ();
944
c28b4e40
JW
945 /* If this spans multiple hard registers, then we must indicate that every
946 register has an unusable value. */
947 if (regno < FIRST_PSEUDO_REGISTER)
948 n = HARD_REGNO_NREGS (regno, GET_MODE (dest));
949 else
950 n = 1;
951 if (n != 1)
952 {
953 while (--n >= 0)
954 {
955 reg_seen[regno + n] = 1;
956 new_reg_base_value[regno + n] = 0;
957 }
958 return;
959 }
960
9ae8ffe7
JL
961 if (set)
962 {
963 /* A CLOBBER wipes out any old value but does not prevent a previously
964 unset register from acquiring a base address (i.e. reg_seen is not
965 set). */
966 if (GET_CODE (set) == CLOBBER)
967 {
ec907dd8 968 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
969 return;
970 }
971 src = SET_SRC (set);
972 }
973 else
974 {
9ae8ffe7
JL
975 if (reg_seen[regno])
976 {
ec907dd8 977 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
978 return;
979 }
980 reg_seen[regno] = 1;
38a448ca
RH
981 new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
982 GEN_INT (unique_id++));
9ae8ffe7
JL
983 return;
984 }
985
986 /* This is not the first set. If the new value is not related to the
987 old value, forget the base value. Note that the following code is
988 not detected:
989 extern int x, y; int *p = &x; p += (&y-&x);
990 ANSI C does not allow computing the difference of addresses
991 of distinct top level objects. */
ec907dd8 992 if (new_reg_base_value[regno])
9ae8ffe7
JL
993 switch (GET_CODE (src))
994 {
2a2c8203 995 case LO_SUM:
9ae8ffe7
JL
996 case MINUS:
997 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
ec907dd8 998 new_reg_base_value[regno] = 0;
9ae8ffe7 999 break;
61f0131c
R
1000 case PLUS:
1001 /* If the value we add in the PLUS is also a valid base value,
1002 this might be the actual base value, and the original value
1003 an index. */
1004 {
1005 rtx other = NULL_RTX;
1006
1007 if (XEXP (src, 0) == dest)
1008 other = XEXP (src, 1);
1009 else if (XEXP (src, 1) == dest)
1010 other = XEXP (src, 0);
1011
1012 if (! other || find_base_value (other))
1013 new_reg_base_value[regno] = 0;
1014 break;
1015 }
9ae8ffe7
JL
1016 case AND:
1017 if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
ec907dd8 1018 new_reg_base_value[regno] = 0;
9ae8ffe7 1019 break;
9ae8ffe7 1020 default:
ec907dd8 1021 new_reg_base_value[regno] = 0;
9ae8ffe7
JL
1022 break;
1023 }
1024 /* If this is the first set of a register, record the value. */
1025 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
ec907dd8
JL
1026 && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
1027 new_reg_base_value[regno] = find_base_value (src);
9ae8ffe7
JL
1028
1029 reg_seen[regno] = 1;
1030}
1031
ac3d9668
RK
1032/* Called from loop optimization when a new pseudo-register is
1033 created. It indicates that REGNO is being set to VAL. f INVARIANT
1034 is true then this value also describes an invariant relationship
1035 which can be used to deduce that two registers with unknown values
1036 are different. */
d4b60170 1037
9ae8ffe7 1038void
de12be17 1039record_base_value (regno, val, invariant)
ac3d9668 1040 unsigned int regno;
9ae8ffe7 1041 rtx val;
de12be17 1042 int invariant;
9ae8ffe7 1043{
ac3d9668 1044 if (regno >= reg_base_value_size)
9ae8ffe7 1045 return;
de12be17 1046
de12be17
JC
1047 if (invariant && alias_invariant)
1048 alias_invariant[regno] = val;
1049
9ae8ffe7
JL
1050 if (GET_CODE (val) == REG)
1051 {
fb6754f0
BS
1052 if (REGNO (val) < reg_base_value_size)
1053 reg_base_value[regno] = reg_base_value[REGNO (val)];
d4b60170 1054
9ae8ffe7
JL
1055 return;
1056 }
d4b60170 1057
9ae8ffe7
JL
1058 reg_base_value[regno] = find_base_value (val);
1059}
1060
43fe47ca
JW
1061/* Clear alias info for a register. This is used if an RTL transformation
1062 changes the value of a register. This is used in flow by AUTO_INC_DEC
1063 optimizations. We don't need to clear reg_base_value, since flow only
1064 changes the offset. */
1065
1066void
5197bd50
RK
1067clear_reg_alias_info (reg)
1068 rtx reg;
43fe47ca 1069{
4e1a4144
JW
1070 unsigned int regno = REGNO (reg);
1071
1072 if (regno < reg_known_value_size && regno >= FIRST_PSEUDO_REGISTER)
1073 reg_known_value[regno] = reg;
43fe47ca
JW
1074}
1075
db048faf
MM
1076/* Returns a canonical version of X, from the point of view alias
1077 analysis. (For example, if X is a MEM whose address is a register,
1078 and the register has a known value (say a SYMBOL_REF), then a MEM
1079 whose address is the SYMBOL_REF is returned.) */
1080
1081rtx
9ae8ffe7
JL
1082canon_rtx (x)
1083 rtx x;
1084{
1085 /* Recursively look for equivalences. */
fb6754f0
BS
1086 if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
1087 && REGNO (x) < reg_known_value_size)
1088 return reg_known_value[REGNO (x)] == x
1089 ? x : canon_rtx (reg_known_value[REGNO (x)]);
9ae8ffe7
JL
1090 else if (GET_CODE (x) == PLUS)
1091 {
1092 rtx x0 = canon_rtx (XEXP (x, 0));
1093 rtx x1 = canon_rtx (XEXP (x, 1));
1094
1095 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1096 {
9ae8ffe7 1097 if (GET_CODE (x0) == CONST_INT)
ed8908e7 1098 return plus_constant (x1, INTVAL (x0));
9ae8ffe7 1099 else if (GET_CODE (x1) == CONST_INT)
ed8908e7 1100 return plus_constant (x0, INTVAL (x1));
38a448ca 1101 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
9ae8ffe7
JL
1102 }
1103 }
d4b60170 1104
9ae8ffe7
JL
1105 /* This gives us much better alias analysis when called from
1106 the loop optimizer. Note we want to leave the original
1107 MEM alone, but need to return the canonicalized MEM with
1108 all the flags with their original values. */
1109 else if (GET_CODE (x) == MEM)
f1ec5147 1110 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
d4b60170 1111
9ae8ffe7
JL
1112 return x;
1113}
1114
1115/* Return 1 if X and Y are identical-looking rtx's.
1116
1117 We use the data in reg_known_value above to see if two registers with
1118 different numbers are, in fact, equivalent. */
1119
1120static int
1121rtx_equal_for_memref_p (x, y)
1122 rtx x, y;
1123{
b3694847
SS
1124 int i;
1125 int j;
1126 enum rtx_code code;
1127 const char *fmt;
9ae8ffe7
JL
1128
1129 if (x == 0 && y == 0)
1130 return 1;
1131 if (x == 0 || y == 0)
1132 return 0;
d4b60170 1133
9ae8ffe7
JL
1134 x = canon_rtx (x);
1135 y = canon_rtx (y);
1136
1137 if (x == y)
1138 return 1;
1139
1140 code = GET_CODE (x);
1141 /* Rtx's of different codes cannot be equal. */
1142 if (code != GET_CODE (y))
1143 return 0;
1144
1145 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1146 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1147
1148 if (GET_MODE (x) != GET_MODE (y))
1149 return 0;
1150
db048faf
MM
1151 /* Some RTL can be compared without a recursive examination. */
1152 switch (code)
1153 {
ab59db3c
BS
1154 case VALUE:
1155 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
1156
db048faf
MM
1157 case REG:
1158 return REGNO (x) == REGNO (y);
1159
1160 case LABEL_REF:
1161 return XEXP (x, 0) == XEXP (y, 0);
ca7fd9cd 1162
db048faf
MM
1163 case SYMBOL_REF:
1164 return XSTR (x, 0) == XSTR (y, 0);
1165
1166 case CONST_INT:
1167 case CONST_DOUBLE:
1168 /* There's no need to compare the contents of CONST_DOUBLEs or
1169 CONST_INTs because pointer equality is a good enough
1170 comparison for these nodes. */
1171 return 0;
1172
1173 case ADDRESSOF:
831ecbd4
RK
1174 return (XINT (x, 1) == XINT (y, 1)
1175 && rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0)));
db048faf
MM
1176
1177 default:
1178 break;
1179 }
9ae8ffe7
JL
1180
1181 /* For commutative operations, the RTX match if the operand match in any
1182 order. Also handle the simple binary and unary cases without a loop. */
1183 if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
1184 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1185 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1186 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1187 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1188 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
1189 return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1190 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
1191 else if (GET_RTX_CLASS (code) == '1')
1192 return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
1193
1194 /* Compare the elements. If any pair of corresponding elements
de12be17
JC
1195 fail to match, return 0 for the whole things.
1196
1197 Limit cases to types which actually appear in addresses. */
9ae8ffe7
JL
1198
1199 fmt = GET_RTX_FORMAT (code);
1200 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1201 {
1202 switch (fmt[i])
1203 {
9ae8ffe7
JL
1204 case 'i':
1205 if (XINT (x, i) != XINT (y, i))
1206 return 0;
1207 break;
1208
9ae8ffe7
JL
1209 case 'E':
1210 /* Two vectors must have the same length. */
1211 if (XVECLEN (x, i) != XVECLEN (y, i))
1212 return 0;
1213
1214 /* And the corresponding elements must match. */
1215 for (j = 0; j < XVECLEN (x, i); j++)
d4b60170
RK
1216 if (rtx_equal_for_memref_p (XVECEXP (x, i, j),
1217 XVECEXP (y, i, j)) == 0)
9ae8ffe7
JL
1218 return 0;
1219 break;
1220
1221 case 'e':
1222 if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
1223 return 0;
1224 break;
1225
3237ac18
AH
1226 /* This can happen for asm operands. */
1227 case 's':
1228 if (strcmp (XSTR (x, i), XSTR (y, i)))
1229 return 0;
1230 break;
1231
aee21ba9
JL
1232 /* This can happen for an asm which clobbers memory. */
1233 case '0':
1234 break;
1235
9ae8ffe7
JL
1236 /* It is believed that rtx's at this level will never
1237 contain anything but integers and other rtx's,
1238 except for within LABEL_REFs and SYMBOL_REFs. */
1239 default:
1240 abort ();
1241 }
1242 }
1243 return 1;
1244}
1245
1246/* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
1247 X and return it, or return 0 if none found. */
1248
1249static rtx
1250find_symbolic_term (x)
1251 rtx x;
1252{
b3694847
SS
1253 int i;
1254 enum rtx_code code;
1255 const char *fmt;
9ae8ffe7
JL
1256
1257 code = GET_CODE (x);
1258 if (code == SYMBOL_REF || code == LABEL_REF)
1259 return x;
1260 if (GET_RTX_CLASS (code) == 'o')
1261 return 0;
1262
1263 fmt = GET_RTX_FORMAT (code);
1264 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1265 {
1266 rtx t;
1267
1268 if (fmt[i] == 'e')
1269 {
1270 t = find_symbolic_term (XEXP (x, i));
1271 if (t != 0)
1272 return t;
1273 }
1274 else if (fmt[i] == 'E')
1275 break;
1276 }
1277 return 0;
1278}
1279
1280static rtx
1281find_base_term (x)
b3694847 1282 rtx x;
9ae8ffe7 1283{
eab5c70a
BS
1284 cselib_val *val;
1285 struct elt_loc_list *l;
1286
b949ea8b
JW
1287#if defined (FIND_BASE_TERM)
1288 /* Try machine-dependent ways to find the base term. */
1289 x = FIND_BASE_TERM (x);
1290#endif
1291
9ae8ffe7
JL
1292 switch (GET_CODE (x))
1293 {
1294 case REG:
1295 return REG_BASE_VALUE (x);
1296
d288e53d
DE
1297 case TRUNCATE:
1298 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
ca7fd9cd 1299 return 0;
d288e53d 1300 /* Fall through. */
9ae8ffe7 1301 case HIGH:
6d849a2a
JL
1302 case PRE_INC:
1303 case PRE_DEC:
1304 case POST_INC:
1305 case POST_DEC:
d288e53d
DE
1306 case PRE_MODIFY:
1307 case POST_MODIFY:
6d849a2a
JL
1308 return find_base_term (XEXP (x, 0));
1309
1abade85
RK
1310 case ZERO_EXTEND:
1311 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1312 {
1313 rtx temp = find_base_term (XEXP (x, 0));
1314
1315#ifdef POINTERS_EXTEND_UNSIGNED
1316 if (temp != 0 && CONSTANT_P (temp) && GET_MODE (temp) != Pmode)
1317 temp = convert_memory_address (Pmode, temp);
1318#endif
1319
1320 return temp;
1321 }
1322
eab5c70a
BS
1323 case VALUE:
1324 val = CSELIB_VAL_PTR (x);
1325 for (l = val->locs; l; l = l->next)
1326 if ((x = find_base_term (l->loc)) != 0)
1327 return x;
1328 return 0;
1329
9ae8ffe7
JL
1330 case CONST:
1331 x = XEXP (x, 0);
1332 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1333 return 0;
1334 /* fall through */
1335 case LO_SUM:
1336 case PLUS:
1337 case MINUS:
1338 {
3c567fae
JL
1339 rtx tmp1 = XEXP (x, 0);
1340 rtx tmp2 = XEXP (x, 1);
1341
f5143c46 1342 /* This is a little bit tricky since we have to determine which of
3c567fae
JL
1343 the two operands represents the real base address. Otherwise this
1344 routine may return the index register instead of the base register.
1345
1346 That may cause us to believe no aliasing was possible, when in
1347 fact aliasing is possible.
1348
1349 We use a few simple tests to guess the base register. Additional
1350 tests can certainly be added. For example, if one of the operands
1351 is a shift or multiply, then it must be the index register and the
1352 other operand is the base register. */
ca7fd9cd 1353
b949ea8b
JW
1354 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1355 return find_base_term (tmp2);
1356
3c567fae
JL
1357 /* If either operand is known to be a pointer, then use it
1358 to determine the base term. */
3502dc9c 1359 if (REG_P (tmp1) && REG_POINTER (tmp1))
3c567fae
JL
1360 return find_base_term (tmp1);
1361
3502dc9c 1362 if (REG_P (tmp2) && REG_POINTER (tmp2))
3c567fae
JL
1363 return find_base_term (tmp2);
1364
1365 /* Neither operand was known to be a pointer. Go ahead and find the
1366 base term for both operands. */
1367 tmp1 = find_base_term (tmp1);
1368 tmp2 = find_base_term (tmp2);
1369
1370 /* If either base term is named object or a special address
1371 (like an argument or stack reference), then use it for the
1372 base term. */
d4b60170 1373 if (tmp1 != 0
3c567fae
JL
1374 && (GET_CODE (tmp1) == SYMBOL_REF
1375 || GET_CODE (tmp1) == LABEL_REF
1376 || (GET_CODE (tmp1) == ADDRESS
1377 && GET_MODE (tmp1) != VOIDmode)))
1378 return tmp1;
1379
d4b60170 1380 if (tmp2 != 0
3c567fae
JL
1381 && (GET_CODE (tmp2) == SYMBOL_REF
1382 || GET_CODE (tmp2) == LABEL_REF
1383 || (GET_CODE (tmp2) == ADDRESS
1384 && GET_MODE (tmp2) != VOIDmode)))
1385 return tmp2;
1386
1387 /* We could not determine which of the two operands was the
1388 base register and which was the index. So we can determine
1389 nothing from the base alias check. */
1390 return 0;
9ae8ffe7
JL
1391 }
1392
1393 case AND:
d288e53d
DE
1394 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) != 0)
1395 return find_base_term (XEXP (x, 0));
9ae8ffe7
JL
1396 return 0;
1397
1398 case SYMBOL_REF:
1399 case LABEL_REF:
1400 return x;
1401
d982e46e 1402 case ADDRESSOF:
bb07060a 1403 return REG_BASE_VALUE (frame_pointer_rtx);
d982e46e 1404
9ae8ffe7
JL
1405 default:
1406 return 0;
1407 }
1408}
1409
1410/* Return 0 if the addresses X and Y are known to point to different
1411 objects, 1 if they might be pointers to the same object. */
1412
1413static int
56ee9281 1414base_alias_check (x, y, x_mode, y_mode)
9ae8ffe7 1415 rtx x, y;
56ee9281 1416 enum machine_mode x_mode, y_mode;
9ae8ffe7
JL
1417{
1418 rtx x_base = find_base_term (x);
1419 rtx y_base = find_base_term (y);
1420
1c72c7f6
JC
1421 /* If the address itself has no known base see if a known equivalent
1422 value has one. If either address still has no known base, nothing
1423 is known about aliasing. */
1424 if (x_base == 0)
1425 {
1426 rtx x_c;
d4b60170 1427
1c72c7f6
JC
1428 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1429 return 1;
d4b60170 1430
1c72c7f6
JC
1431 x_base = find_base_term (x_c);
1432 if (x_base == 0)
1433 return 1;
1434 }
9ae8ffe7 1435
1c72c7f6
JC
1436 if (y_base == 0)
1437 {
1438 rtx y_c;
1439 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1440 return 1;
d4b60170 1441
1c72c7f6
JC
1442 y_base = find_base_term (y_c);
1443 if (y_base == 0)
1444 return 1;
1445 }
1446
1447 /* If the base addresses are equal nothing is known about aliasing. */
1448 if (rtx_equal_p (x_base, y_base))
9ae8ffe7
JL
1449 return 1;
1450
ca7fd9cd 1451 /* The base addresses of the read and write are different expressions.
56ee9281
RH
1452 If they are both symbols and they are not accessed via AND, there is
1453 no conflict. We can bring knowledge of object alignment into play
1454 here. For example, on alpha, "char a, b;" can alias one another,
1455 though "char a; long b;" cannot. */
9ae8ffe7 1456 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
c02f035f 1457 {
56ee9281
RH
1458 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1459 return 1;
1460 if (GET_CODE (x) == AND
1461 && (GET_CODE (XEXP (x, 1)) != CONST_INT
8fa2140d 1462 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
56ee9281
RH
1463 return 1;
1464 if (GET_CODE (y) == AND
1465 && (GET_CODE (XEXP (y, 1)) != CONST_INT
8fa2140d 1466 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
56ee9281 1467 return 1;
b2972551
JL
1468 /* Differing symbols never alias. */
1469 return 0;
c02f035f 1470 }
9ae8ffe7
JL
1471
1472 /* If one address is a stack reference there can be no alias:
1473 stack references using different base registers do not alias,
1474 a stack reference can not alias a parameter, and a stack reference
1475 can not alias a global. */
1476 if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1477 || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1478 return 0;
1479
1480 if (! flag_argument_noalias)
1481 return 1;
1482
1483 if (flag_argument_noalias > 1)
1484 return 0;
1485
ec5c56db 1486 /* Weak noalias assertion (arguments are distinct, but may match globals). */
9ae8ffe7
JL
1487 return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1488}
1489
eab5c70a
BS
1490/* Convert the address X into something we can use. This is done by returning
1491 it unchanged unless it is a value; in the latter case we call cselib to get
1492 a more useful rtx. */
3bdf5ad1 1493
a13d4ebf 1494rtx
eab5c70a
BS
1495get_addr (x)
1496 rtx x;
1497{
1498 cselib_val *v;
1499 struct elt_loc_list *l;
1500
1501 if (GET_CODE (x) != VALUE)
1502 return x;
1503 v = CSELIB_VAL_PTR (x);
1504 for (l = v->locs; l; l = l->next)
1505 if (CONSTANT_P (l->loc))
1506 return l->loc;
1507 for (l = v->locs; l; l = l->next)
1508 if (GET_CODE (l->loc) != REG && GET_CODE (l->loc) != MEM)
1509 return l->loc;
1510 if (v->locs)
1511 return v->locs->loc;
1512 return x;
1513}
1514
39cec1ac
MH
1515/* Return the address of the (N_REFS + 1)th memory reference to ADDR
1516 where SIZE is the size in bytes of the memory reference. If ADDR
1517 is not modified by the memory reference then ADDR is returned. */
1518
1519rtx
1520addr_side_effect_eval (addr, size, n_refs)
1521 rtx addr;
1522 int size;
1523 int n_refs;
1524{
1525 int offset = 0;
ca7fd9cd 1526
39cec1ac
MH
1527 switch (GET_CODE (addr))
1528 {
1529 case PRE_INC:
1530 offset = (n_refs + 1) * size;
1531 break;
1532 case PRE_DEC:
1533 offset = -(n_refs + 1) * size;
1534 break;
1535 case POST_INC:
1536 offset = n_refs * size;
1537 break;
1538 case POST_DEC:
1539 offset = -n_refs * size;
1540 break;
1541
1542 default:
1543 return addr;
1544 }
ca7fd9cd 1545
39cec1ac
MH
1546 if (offset)
1547 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
1548 else
1549 addr = XEXP (addr, 0);
1550
1551 return addr;
1552}
1553
9ae8ffe7
JL
1554/* Return nonzero if X and Y (memory addresses) could reference the
1555 same location in memory. C is an offset accumulator. When
1556 C is nonzero, we are testing aliases between X and Y + C.
1557 XSIZE is the size in bytes of the X reference,
1558 similarly YSIZE is the size in bytes for Y.
1559
1560 If XSIZE or YSIZE is zero, we do not know the amount of memory being
1561 referenced (the reference was BLKmode), so make the most pessimistic
1562 assumptions.
1563
c02f035f
RH
1564 If XSIZE or YSIZE is negative, we may access memory outside the object
1565 being referenced as a side effect. This can happen when using AND to
1566 align memory references, as is done on the Alpha.
1567
9ae8ffe7 1568 Nice to notice that varying addresses cannot conflict with fp if no
0211b6ab 1569 local variables had their addresses taken, but that's too hard now. */
9ae8ffe7 1570
9ae8ffe7
JL
1571static int
1572memrefs_conflict_p (xsize, x, ysize, y, c)
b3694847 1573 rtx x, y;
9ae8ffe7
JL
1574 int xsize, ysize;
1575 HOST_WIDE_INT c;
1576{
eab5c70a
BS
1577 if (GET_CODE (x) == VALUE)
1578 x = get_addr (x);
1579 if (GET_CODE (y) == VALUE)
1580 y = get_addr (y);
9ae8ffe7
JL
1581 if (GET_CODE (x) == HIGH)
1582 x = XEXP (x, 0);
1583 else if (GET_CODE (x) == LO_SUM)
1584 x = XEXP (x, 1);
1585 else
39cec1ac 1586 x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
9ae8ffe7
JL
1587 if (GET_CODE (y) == HIGH)
1588 y = XEXP (y, 0);
1589 else if (GET_CODE (y) == LO_SUM)
1590 y = XEXP (y, 1);
1591 else
39cec1ac 1592 y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
9ae8ffe7
JL
1593
1594 if (rtx_equal_for_memref_p (x, y))
1595 {
c02f035f 1596 if (xsize <= 0 || ysize <= 0)
9ae8ffe7
JL
1597 return 1;
1598 if (c >= 0 && xsize > c)
1599 return 1;
1600 if (c < 0 && ysize+c > 0)
1601 return 1;
1602 return 0;
1603 }
1604
6e73e666
JC
1605 /* This code used to check for conflicts involving stack references and
1606 globals but the base address alias code now handles these cases. */
9ae8ffe7
JL
1607
1608 if (GET_CODE (x) == PLUS)
1609 {
1610 /* The fact that X is canonicalized means that this
1611 PLUS rtx is canonicalized. */
1612 rtx x0 = XEXP (x, 0);
1613 rtx x1 = XEXP (x, 1);
1614
1615 if (GET_CODE (y) == PLUS)
1616 {
1617 /* The fact that Y is canonicalized means that this
1618 PLUS rtx is canonicalized. */
1619 rtx y0 = XEXP (y, 0);
1620 rtx y1 = XEXP (y, 1);
1621
1622 if (rtx_equal_for_memref_p (x1, y1))
1623 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1624 if (rtx_equal_for_memref_p (x0, y0))
1625 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1626 if (GET_CODE (x1) == CONST_INT)
63be02db
JM
1627 {
1628 if (GET_CODE (y1) == CONST_INT)
1629 return memrefs_conflict_p (xsize, x0, ysize, y0,
1630 c - INTVAL (x1) + INTVAL (y1));
1631 else
1632 return memrefs_conflict_p (xsize, x0, ysize, y,
1633 c - INTVAL (x1));
1634 }
9ae8ffe7
JL
1635 else if (GET_CODE (y1) == CONST_INT)
1636 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1637
6e73e666 1638 return 1;
9ae8ffe7
JL
1639 }
1640 else if (GET_CODE (x1) == CONST_INT)
1641 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1642 }
1643 else if (GET_CODE (y) == PLUS)
1644 {
1645 /* The fact that Y is canonicalized means that this
1646 PLUS rtx is canonicalized. */
1647 rtx y0 = XEXP (y, 0);
1648 rtx y1 = XEXP (y, 1);
1649
1650 if (GET_CODE (y1) == CONST_INT)
1651 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1652 else
1653 return 1;
1654 }
1655
1656 if (GET_CODE (x) == GET_CODE (y))
1657 switch (GET_CODE (x))
1658 {
1659 case MULT:
1660 {
1661 /* Handle cases where we expect the second operands to be the
1662 same, and check only whether the first operand would conflict
1663 or not. */
1664 rtx x0, y0;
1665 rtx x1 = canon_rtx (XEXP (x, 1));
1666 rtx y1 = canon_rtx (XEXP (y, 1));
1667 if (! rtx_equal_for_memref_p (x1, y1))
1668 return 1;
1669 x0 = canon_rtx (XEXP (x, 0));
1670 y0 = canon_rtx (XEXP (y, 0));
1671 if (rtx_equal_for_memref_p (x0, y0))
1672 return (xsize == 0 || ysize == 0
1673 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1674
1675 /* Can't properly adjust our sizes. */
1676 if (GET_CODE (x1) != CONST_INT)
1677 return 1;
1678 xsize /= INTVAL (x1);
1679 ysize /= INTVAL (x1);
1680 c /= INTVAL (x1);
1681 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1682 }
1d300e19 1683
de12be17
JC
1684 case REG:
1685 /* Are these registers known not to be equal? */
1686 if (alias_invariant)
1687 {
e51712db 1688 unsigned int r_x = REGNO (x), r_y = REGNO (y);
de12be17
JC
1689 rtx i_x, i_y; /* invariant relationships of X and Y */
1690
1691 i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1692 i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1693
1694 if (i_x == 0 && i_y == 0)
1695 break;
1696
1697 if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1698 ysize, i_y ? i_y : y, c))
1699 return 0;
1700 }
1701 break;
1702
1d300e19
KG
1703 default:
1704 break;
9ae8ffe7
JL
1705 }
1706
1707 /* Treat an access through an AND (e.g. a subword access on an Alpha)
ca7fd9cd 1708 as an access with indeterminate size. Assume that references
56ee9281
RH
1709 besides AND are aligned, so if the size of the other reference is
1710 at least as large as the alignment, assume no other overlap. */
9ae8ffe7 1711 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
56ee9281 1712 {
02e3377d 1713 if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
56ee9281
RH
1714 xsize = -1;
1715 return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1716 }
9ae8ffe7 1717 if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
c02f035f 1718 {
56ee9281 1719 /* ??? If we are indexing far enough into the array/structure, we
ca7fd9cd 1720 may yet be able to determine that we can not overlap. But we
c02f035f 1721 also need to that we are far enough from the end not to overlap
56ee9281 1722 a following reference, so we do nothing with that for now. */
02e3377d 1723 if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
56ee9281
RH
1724 ysize = -1;
1725 return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
c02f035f 1726 }
9ae8ffe7 1727
b24ea077
JW
1728 if (GET_CODE (x) == ADDRESSOF)
1729 {
1730 if (y == frame_pointer_rtx
1731 || GET_CODE (y) == ADDRESSOF)
1732 return xsize <= 0 || ysize <= 0;
1733 }
1734 if (GET_CODE (y) == ADDRESSOF)
1735 {
1736 if (x == frame_pointer_rtx)
1737 return xsize <= 0 || ysize <= 0;
1738 }
d982e46e 1739
9ae8ffe7
JL
1740 if (CONSTANT_P (x))
1741 {
1742 if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1743 {
1744 c += (INTVAL (y) - INTVAL (x));
c02f035f 1745 return (xsize <= 0 || ysize <= 0
9ae8ffe7
JL
1746 || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1747 }
1748
1749 if (GET_CODE (x) == CONST)
1750 {
1751 if (GET_CODE (y) == CONST)
1752 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1753 ysize, canon_rtx (XEXP (y, 0)), c);
1754 else
1755 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1756 ysize, y, c);
1757 }
1758 if (GET_CODE (y) == CONST)
1759 return memrefs_conflict_p (xsize, x, ysize,
1760 canon_rtx (XEXP (y, 0)), c);
1761
1762 if (CONSTANT_P (y))
b949ea8b 1763 return (xsize <= 0 || ysize <= 0
c02f035f 1764 || (rtx_equal_for_memref_p (x, y)
b949ea8b 1765 && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
9ae8ffe7
JL
1766
1767 return 1;
1768 }
1769 return 1;
1770}
1771
1772/* Functions to compute memory dependencies.
1773
1774 Since we process the insns in execution order, we can build tables
1775 to keep track of what registers are fixed (and not aliased), what registers
1776 are varying in known ways, and what registers are varying in unknown
1777 ways.
1778
1779 If both memory references are volatile, then there must always be a
1780 dependence between the two references, since their order can not be
1781 changed. A volatile and non-volatile reference can be interchanged
ca7fd9cd 1782 though.
9ae8ffe7 1783
dc1618bc
RK
1784 A MEM_IN_STRUCT reference at a non-AND varying address can never
1785 conflict with a non-MEM_IN_STRUCT reference at a fixed address. We
1786 also must allow AND addresses, because they may generate accesses
1787 outside the object being referenced. This is used to generate
1788 aligned addresses from unaligned addresses, for instance, the alpha
1789 storeqi_unaligned pattern. */
9ae8ffe7
JL
1790
1791/* Read dependence: X is read after read in MEM takes place. There can
1792 only be a dependence here if both reads are volatile. */
1793
1794int
1795read_dependence (mem, x)
1796 rtx mem;
1797 rtx x;
1798{
1799 return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1800}
1801
c6df88cb
MM
1802/* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1803 MEM2 is a reference to a structure at a varying address, or returns
1804 MEM2 if vice versa. Otherwise, returns NULL_RTX. If a non-NULL
1805 value is returned MEM1 and MEM2 can never alias. VARIES_P is used
1806 to decide whether or not an address may vary; it should return
eab5c70a
BS
1807 nonzero whenever variation is possible.
1808 MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2. */
ca7fd9cd 1809
2c72b78f 1810static rtx
eab5c70a
BS
1811fixed_scalar_and_varying_struct_p (mem1, mem2, mem1_addr, mem2_addr, varies_p)
1812 rtx mem1, mem2;
1813 rtx mem1_addr, mem2_addr;
e38fe8e0 1814 int (*varies_p) PARAMS ((rtx, int));
ca7fd9cd 1815{
3e0abe15
GK
1816 if (! flag_strict_aliasing)
1817 return NULL_RTX;
1818
ca7fd9cd 1819 if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
e38fe8e0 1820 && !varies_p (mem1_addr, 1) && varies_p (mem2_addr, 1))
c6df88cb
MM
1821 /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1822 varying address. */
1823 return mem1;
1824
ca7fd9cd 1825 if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
e38fe8e0 1826 && varies_p (mem1_addr, 1) && !varies_p (mem2_addr, 1))
c6df88cb
MM
1827 /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1828 varying address. */
1829 return mem2;
1830
1831 return NULL_RTX;
1832}
1833
1834/* Returns nonzero if something about the mode or address format MEM1
1835 indicates that it might well alias *anything*. */
1836
2c72b78f 1837static int
c6df88cb
MM
1838aliases_everything_p (mem)
1839 rtx mem;
1840{
c6df88cb
MM
1841 if (GET_CODE (XEXP (mem, 0)) == AND)
1842 /* If the address is an AND, its very hard to know at what it is
1843 actually pointing. */
1844 return 1;
ca7fd9cd 1845
c6df88cb
MM
1846 return 0;
1847}
1848
998d7deb
RH
1849/* Return true if we can determine that the fields referenced cannot
1850 overlap for any pair of objects. */
1851
1852static bool
1853nonoverlapping_component_refs_p (x, y)
1854 tree x, y;
1855{
1856 tree fieldx, fieldy, typex, typey, orig_y;
1857
1858 do
1859 {
1860 /* The comparison has to be done at a common type, since we don't
d6a7951f 1861 know how the inheritance hierarchy works. */
998d7deb
RH
1862 orig_y = y;
1863 do
1864 {
1865 fieldx = TREE_OPERAND (x, 1);
1866 typex = DECL_FIELD_CONTEXT (fieldx);
1867
1868 y = orig_y;
1869 do
1870 {
1871 fieldy = TREE_OPERAND (y, 1);
1872 typey = DECL_FIELD_CONTEXT (fieldy);
1873
1874 if (typex == typey)
1875 goto found;
1876
1877 y = TREE_OPERAND (y, 0);
1878 }
1879 while (y && TREE_CODE (y) == COMPONENT_REF);
1880
1881 x = TREE_OPERAND (x, 0);
1882 }
1883 while (x && TREE_CODE (x) == COMPONENT_REF);
1884
1885 /* Never found a common type. */
1886 return false;
1887
1888 found:
1889 /* If we're left with accessing different fields of a structure,
1890 then no overlap. */
1891 if (TREE_CODE (typex) == RECORD_TYPE
1892 && fieldx != fieldy)
1893 return true;
1894
1895 /* The comparison on the current field failed. If we're accessing
1896 a very nested structure, look at the next outer level. */
1897 x = TREE_OPERAND (x, 0);
1898 y = TREE_OPERAND (y, 0);
1899 }
1900 while (x && y
1901 && TREE_CODE (x) == COMPONENT_REF
1902 && TREE_CODE (y) == COMPONENT_REF);
ca7fd9cd 1903
998d7deb
RH
1904 return false;
1905}
1906
1907/* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
1908
1909static tree
1910decl_for_component_ref (x)
1911 tree x;
1912{
1913 do
1914 {
1915 x = TREE_OPERAND (x, 0);
1916 }
1917 while (x && TREE_CODE (x) == COMPONENT_REF);
1918
1919 return x && DECL_P (x) ? x : NULL_TREE;
1920}
1921
1922/* Walk up the COMPONENT_REF list and adjust OFFSET to compensate for the
1923 offset of the field reference. */
1924
1925static rtx
1926adjust_offset_for_component_ref (x, offset)
1927 tree x;
1928 rtx offset;
1929{
1930 HOST_WIDE_INT ioffset;
1931
1932 if (! offset)
1933 return NULL_RTX;
1934
1935 ioffset = INTVAL (offset);
ca7fd9cd 1936 do
998d7deb
RH
1937 {
1938 tree field = TREE_OPERAND (x, 1);
1939
1940 if (! host_integerp (DECL_FIELD_OFFSET (field), 1))
1941 return NULL_RTX;
1942 ioffset += (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1943 + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1944 / BITS_PER_UNIT));
1945
1946 x = TREE_OPERAND (x, 0);
1947 }
1948 while (x && TREE_CODE (x) == COMPONENT_REF);
1949
1950 return GEN_INT (ioffset);
1951}
1952
1953/* Return nonzero if we can deterimine the exprs corresponding to memrefs
a4311dfe
RK
1954 X and Y and they do not overlap. */
1955
1956static int
1957nonoverlapping_memrefs_p (x, y)
1958 rtx x, y;
1959{
998d7deb 1960 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
a4311dfe
RK
1961 rtx rtlx, rtly;
1962 rtx basex, basey;
998d7deb 1963 rtx moffsetx, moffsety;
a4311dfe
RK
1964 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
1965
998d7deb
RH
1966 /* Unless both have exprs, we can't tell anything. */
1967 if (exprx == 0 || expry == 0)
1968 return 0;
1969
1970 /* If both are field references, we may be able to determine something. */
1971 if (TREE_CODE (exprx) == COMPONENT_REF
1972 && TREE_CODE (expry) == COMPONENT_REF
1973 && nonoverlapping_component_refs_p (exprx, expry))
1974 return 1;
1975
1976 /* If the field reference test failed, look at the DECLs involved. */
1977 moffsetx = MEM_OFFSET (x);
1978 if (TREE_CODE (exprx) == COMPONENT_REF)
1979 {
1980 tree t = decl_for_component_ref (exprx);
1981 if (! t)
1982 return 0;
1983 moffsetx = adjust_offset_for_component_ref (exprx, moffsetx);
1984 exprx = t;
1985 }
c67a1cf6
RH
1986 else if (TREE_CODE (exprx) == INDIRECT_REF)
1987 {
1988 exprx = TREE_OPERAND (exprx, 0);
1989 if (flag_argument_noalias < 2
1990 || TREE_CODE (exprx) != PARM_DECL)
1991 return 0;
1992 }
1993
998d7deb
RH
1994 moffsety = MEM_OFFSET (y);
1995 if (TREE_CODE (expry) == COMPONENT_REF)
1996 {
1997 tree t = decl_for_component_ref (expry);
1998 if (! t)
1999 return 0;
2000 moffsety = adjust_offset_for_component_ref (expry, moffsety);
2001 expry = t;
2002 }
c67a1cf6
RH
2003 else if (TREE_CODE (expry) == INDIRECT_REF)
2004 {
2005 expry = TREE_OPERAND (expry, 0);
2006 if (flag_argument_noalias < 2
2007 || TREE_CODE (expry) != PARM_DECL)
2008 return 0;
2009 }
998d7deb
RH
2010
2011 if (! DECL_P (exprx) || ! DECL_P (expry))
a4311dfe
RK
2012 return 0;
2013
998d7deb
RH
2014 rtlx = DECL_RTL (exprx);
2015 rtly = DECL_RTL (expry);
a4311dfe 2016
1edcd60b
RK
2017 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2018 can't overlap unless they are the same because we never reuse that part
2019 of the stack frame used for locals for spilled pseudos. */
2020 if ((GET_CODE (rtlx) != MEM || GET_CODE (rtly) != MEM)
2021 && ! rtx_equal_p (rtlx, rtly))
a4311dfe
RK
2022 return 1;
2023
2024 /* Get the base and offsets of both decls. If either is a register, we
2025 know both are and are the same, so use that as the base. The only
2026 we can avoid overlap is if we can deduce that they are nonoverlapping
2027 pieces of that decl, which is very rare. */
1edcd60b 2028 basex = GET_CODE (rtlx) == MEM ? XEXP (rtlx, 0) : rtlx;
a4311dfe
RK
2029 if (GET_CODE (basex) == PLUS && GET_CODE (XEXP (basex, 1)) == CONST_INT)
2030 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2031
1edcd60b 2032 basey = GET_CODE (rtly) == MEM ? XEXP (rtly, 0) : rtly;
a4311dfe
RK
2033 if (GET_CODE (basey) == PLUS && GET_CODE (XEXP (basey, 1)) == CONST_INT)
2034 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2035
d746694a 2036 /* If the bases are different, we know they do not overlap if both
ca7fd9cd 2037 are constants or if one is a constant and the other a pointer into the
d746694a
RK
2038 stack frame. Otherwise a different base means we can't tell if they
2039 overlap or not. */
2040 if (! rtx_equal_p (basex, basey))
ca7fd9cd
KH
2041 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2042 || (CONSTANT_P (basex) && REG_P (basey)
2043 && REGNO_PTR_FRAME_P (REGNO (basey)))
2044 || (CONSTANT_P (basey) && REG_P (basex)
2045 && REGNO_PTR_FRAME_P (REGNO (basex))));
a4311dfe 2046
998d7deb 2047 sizex = (GET_CODE (rtlx) != MEM ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
a4311dfe
RK
2048 : MEM_SIZE (rtlx) ? INTVAL (MEM_SIZE (rtlx))
2049 : -1);
998d7deb 2050 sizey = (GET_CODE (rtly) != MEM ? (int) GET_MODE_SIZE (GET_MODE (rtly))
a4311dfe
RK
2051 : MEM_SIZE (rtly) ? INTVAL (MEM_SIZE (rtly)) :
2052 -1);
2053
0af5bc3e
RK
2054 /* If we have an offset for either memref, it can update the values computed
2055 above. */
998d7deb
RH
2056 if (moffsetx)
2057 offsetx += INTVAL (moffsetx), sizex -= INTVAL (moffsetx);
2058 if (moffsety)
2059 offsety += INTVAL (moffsety), sizey -= INTVAL (moffsety);
a4311dfe 2060
0af5bc3e 2061 /* If a memref has both a size and an offset, we can use the smaller size.
efc981bb 2062 We can't do this if the offset isn't known because we must view this
0af5bc3e 2063 memref as being anywhere inside the DECL's MEM. */
998d7deb 2064 if (MEM_SIZE (x) && moffsetx)
a4311dfe 2065 sizex = INTVAL (MEM_SIZE (x));
998d7deb 2066 if (MEM_SIZE (y) && moffsety)
a4311dfe
RK
2067 sizey = INTVAL (MEM_SIZE (y));
2068
2069 /* Put the values of the memref with the lower offset in X's values. */
2070 if (offsetx > offsety)
2071 {
2072 tem = offsetx, offsetx = offsety, offsety = tem;
2073 tem = sizex, sizex = sizey, sizey = tem;
2074 }
2075
2076 /* If we don't know the size of the lower-offset value, we can't tell
2077 if they conflict. Otherwise, we do the test. */
a6f7c915 2078 return sizex >= 0 && offsety >= offsetx + sizex;
a4311dfe
RK
2079}
2080
9ae8ffe7
JL
2081/* True dependence: X is read after store in MEM takes place. */
2082
2083int
2084true_dependence (mem, mem_mode, x, varies)
2085 rtx mem;
2086 enum machine_mode mem_mode;
2087 rtx x;
e38fe8e0 2088 int (*varies) PARAMS ((rtx, int));
9ae8ffe7 2089{
b3694847 2090 rtx x_addr, mem_addr;
49982682 2091 rtx base;
9ae8ffe7
JL
2092
2093 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2094 return 1;
2095
c4484b8f
RH
2096 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2097 This is used in epilogue deallocation functions. */
2098 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2099 return 1;
2100 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2101 return 1;
2102
41472af8
MM
2103 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2104 return 0;
2105
b949ea8b
JW
2106 /* Unchanging memory can't conflict with non-unchanging memory.
2107 A non-unchanging read can conflict with a non-unchanging write.
2108 An unchanging read can conflict with an unchanging write since
2109 there may be a single store to this address to initialize it.
ec569656
RK
2110 Note that an unchanging store can conflict with a non-unchanging read
2111 since we have to make conservative assumptions when we have a
2112 record with readonly fields and we are copying the whole thing.
b949ea8b
JW
2113 Just fall through to the code below to resolve potential conflicts.
2114 This won't handle all cases optimally, but the possible performance
2115 loss should be negligible. */
ec569656 2116 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
9ae8ffe7
JL
2117 return 0;
2118
a4311dfe
RK
2119 if (nonoverlapping_memrefs_p (mem, x))
2120 return 0;
2121
56ee9281
RH
2122 if (mem_mode == VOIDmode)
2123 mem_mode = GET_MODE (mem);
2124
eab5c70a
BS
2125 x_addr = get_addr (XEXP (x, 0));
2126 mem_addr = get_addr (XEXP (mem, 0));
2127
55efb413
JW
2128 base = find_base_term (x_addr);
2129 if (base && (GET_CODE (base) == LABEL_REF
2130 || (GET_CODE (base) == SYMBOL_REF
2131 && CONSTANT_POOL_ADDRESS_P (base))))
2132 return 0;
2133
eab5c70a 2134 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1c72c7f6
JC
2135 return 0;
2136
eab5c70a
BS
2137 x_addr = canon_rtx (x_addr);
2138 mem_addr = canon_rtx (mem_addr);
6e73e666 2139
0211b6ab
JW
2140 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2141 SIZE_FOR_MODE (x), x_addr, 0))
2142 return 0;
2143
c6df88cb 2144 if (aliases_everything_p (x))
0211b6ab
JW
2145 return 1;
2146
f5143c46 2147 /* We cannot use aliases_everything_p to test MEM, since we must look
c6df88cb
MM
2148 at MEM_MODE, rather than GET_MODE (MEM). */
2149 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
a13d4ebf
AM
2150 return 1;
2151
2152 /* In true_dependence we also allow BLKmode to alias anything. Why
2153 don't we do this in anti_dependence and output_dependence? */
2154 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2155 return 1;
2156
2157 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2158 varies);
2159}
2160
2161/* Canonical true dependence: X is read after store in MEM takes place.
ca7fd9cd
KH
2162 Variant of true_dependence which assumes MEM has already been
2163 canonicalized (hence we no longer do that here).
2164 The mem_addr argument has been added, since true_dependence computed
a13d4ebf
AM
2165 this value prior to canonicalizing. */
2166
2167int
2168canon_true_dependence (mem, mem_mode, mem_addr, x, varies)
2169 rtx mem, mem_addr, x;
2170 enum machine_mode mem_mode;
2171 int (*varies) PARAMS ((rtx, int));
2172{
b3694847 2173 rtx x_addr;
a13d4ebf
AM
2174
2175 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2176 return 1;
2177
0fe854a7
RH
2178 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2179 This is used in epilogue deallocation functions. */
2180 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2181 return 1;
2182 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2183 return 1;
2184
a13d4ebf
AM
2185 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2186 return 0;
2187
2188 /* If X is an unchanging read, then it can't possibly conflict with any
2189 non-unchanging store. It may conflict with an unchanging write though,
2190 because there may be a single store to this address to initialize it.
2191 Just fall through to the code below to resolve the case where we have
2192 both an unchanging read and an unchanging write. This won't handle all
2193 cases optimally, but the possible performance loss should be
2194 negligible. */
2195 if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
2196 return 0;
2197
a4311dfe
RK
2198 if (nonoverlapping_memrefs_p (x, mem))
2199 return 0;
2200
a13d4ebf
AM
2201 x_addr = get_addr (XEXP (x, 0));
2202
2203 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2204 return 0;
2205
2206 x_addr = canon_rtx (x_addr);
2207 if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2208 SIZE_FOR_MODE (x), x_addr, 0))
2209 return 0;
2210
2211 if (aliases_everything_p (x))
2212 return 1;
2213
f5143c46 2214 /* We cannot use aliases_everything_p to test MEM, since we must look
a13d4ebf
AM
2215 at MEM_MODE, rather than GET_MODE (MEM). */
2216 if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
c6df88cb 2217 return 1;
0211b6ab 2218
c6df88cb
MM
2219 /* In true_dependence we also allow BLKmode to alias anything. Why
2220 don't we do this in anti_dependence and output_dependence? */
2221 if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2222 return 1;
0211b6ab 2223
eab5c70a
BS
2224 return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2225 varies);
9ae8ffe7
JL
2226}
2227
da7d8304
KH
2228/* Returns nonzero if a write to X might alias a previous read from
2229 (or, if WRITEP is nonzero, a write to) MEM. */
9ae8ffe7 2230
2c72b78f 2231static int
c6df88cb 2232write_dependence_p (mem, x, writep)
9ae8ffe7
JL
2233 rtx mem;
2234 rtx x;
c6df88cb 2235 int writep;
9ae8ffe7 2236{
6e73e666 2237 rtx x_addr, mem_addr;
c6df88cb 2238 rtx fixed_scalar;
49982682 2239 rtx base;
6e73e666 2240
9ae8ffe7
JL
2241 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2242 return 1;
2243
c4484b8f
RH
2244 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2245 This is used in epilogue deallocation functions. */
2246 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2247 return 1;
2248 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2249 return 1;
2250
eab5c70a
BS
2251 if (DIFFERENT_ALIAS_SETS_P (x, mem))
2252 return 0;
2253
b949ea8b
JW
2254 /* Unchanging memory can't conflict with non-unchanging memory. */
2255 if (RTX_UNCHANGING_P (x) != RTX_UNCHANGING_P (mem))
2256 return 0;
2257
9ae8ffe7
JL
2258 /* If MEM is an unchanging read, then it can't possibly conflict with
2259 the store to X, because there is at most one store to MEM, and it must
2260 have occurred somewhere before MEM. */
55efb413
JW
2261 if (! writep && RTX_UNCHANGING_P (mem))
2262 return 0;
2263
a4311dfe
RK
2264 if (nonoverlapping_memrefs_p (x, mem))
2265 return 0;
2266
55efb413
JW
2267 x_addr = get_addr (XEXP (x, 0));
2268 mem_addr = get_addr (XEXP (mem, 0));
2269
49982682
JW
2270 if (! writep)
2271 {
55efb413 2272 base = find_base_term (mem_addr);
49982682
JW
2273 if (base && (GET_CODE (base) == LABEL_REF
2274 || (GET_CODE (base) == SYMBOL_REF
2275 && CONSTANT_POOL_ADDRESS_P (base))))
2276 return 0;
2277 }
2278
eab5c70a
BS
2279 if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2280 GET_MODE (mem)))
41472af8
MM
2281 return 0;
2282
eab5c70a
BS
2283 x_addr = canon_rtx (x_addr);
2284 mem_addr = canon_rtx (mem_addr);
6e73e666 2285
c6df88cb
MM
2286 if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2287 SIZE_FOR_MODE (x), x_addr, 0))
2288 return 0;
2289
ca7fd9cd 2290 fixed_scalar
eab5c70a
BS
2291 = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2292 rtx_addr_varies_p);
2293
c6df88cb
MM
2294 return (!(fixed_scalar == mem && !aliases_everything_p (x))
2295 && !(fixed_scalar == x && !aliases_everything_p (mem)));
2296}
2297
2298/* Anti dependence: X is written after read in MEM takes place. */
2299
2300int
2301anti_dependence (mem, x)
2302 rtx mem;
2303 rtx x;
2304{
2305 return write_dependence_p (mem, x, /*writep=*/0);
9ae8ffe7
JL
2306}
2307
2308/* Output dependence: X is written after store in MEM takes place. */
2309
2310int
2311output_dependence (mem, x)
b3694847
SS
2312 rtx mem;
2313 rtx x;
9ae8ffe7 2314{
c6df88cb 2315 return write_dependence_p (mem, x, /*writep=*/1);
9ae8ffe7 2316}
c14b9960
JW
2317\f
2318/* A subroutine of nonlocal_mentioned_p, returns 1 if *LOC mentions
2319 something which is not local to the function and is not constant. */
7790df19
JW
2320
2321static int
c14b9960
JW
2322nonlocal_mentioned_p_1 (loc, data)
2323 rtx *loc;
2324 void *data ATTRIBUTE_UNUSED;
7790df19 2325{
c14b9960 2326 rtx x = *loc;
7790df19 2327 rtx base;
7790df19
JW
2328 int regno;
2329
c14b9960
JW
2330 if (! x)
2331 return 0;
7790df19 2332
c14b9960 2333 switch (GET_CODE (x))
7790df19
JW
2334 {
2335 case SUBREG:
2336 if (GET_CODE (SUBREG_REG (x)) == REG)
2337 {
2338 /* Global registers are not local. */
2339 if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
ddef6bc7 2340 && global_regs[subreg_regno (x)])
7790df19
JW
2341 return 1;
2342 return 0;
2343 }
2344 break;
2345
2346 case REG:
2347 regno = REGNO (x);
2348 /* Global registers are not local. */
2349 if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2350 return 1;
2351 return 0;
2352
2353 case SCRATCH:
2354 case PC:
2355 case CC0:
2356 case CONST_INT:
2357 case CONST_DOUBLE:
69ef87e2 2358 case CONST_VECTOR:
7790df19
JW
2359 case CONST:
2360 case LABEL_REF:
2361 return 0;
2362
2363 case SYMBOL_REF:
2364 /* Constants in the function's constants pool are constant. */
2365 if (CONSTANT_POOL_ADDRESS_P (x))
2366 return 0;
2367 return 1;
2368
2369 case CALL:
bf6d9fd7 2370 /* Non-constant calls and recursion are not local. */
7790df19
JW
2371 return 1;
2372
2373 case MEM:
2374 /* Be overly conservative and consider any volatile memory
2375 reference as not local. */
2376 if (MEM_VOLATILE_P (x))
2377 return 1;
2378 base = find_base_term (XEXP (x, 0));
2379 if (base)
2380 {
b3b5ad95
JL
2381 /* A Pmode ADDRESS could be a reference via the structure value
2382 address or static chain. Such memory references are nonlocal.
2383
2384 Thus, we have to examine the contents of the ADDRESS to find
2385 out if this is a local reference or not. */
2386 if (GET_CODE (base) == ADDRESS
2387 && GET_MODE (base) == Pmode
2388 && (XEXP (base, 0) == stack_pointer_rtx
2389 || XEXP (base, 0) == arg_pointer_rtx
2390#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2391 || XEXP (base, 0) == hard_frame_pointer_rtx
2392#endif
2393 || XEXP (base, 0) == frame_pointer_rtx))
7790df19
JW
2394 return 0;
2395 /* Constants in the function's constant pool are constant. */
2396 if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
2397 return 0;
2398 }
2399 return 1;
2400
bf6d9fd7 2401 case UNSPEC_VOLATILE:
7790df19 2402 case ASM_INPUT:
7790df19
JW
2403 return 1;
2404
bf6d9fd7
JW
2405 case ASM_OPERANDS:
2406 if (MEM_VOLATILE_P (x))
2407 return 1;
2408
2409 /* FALLTHROUGH */
2410
7790df19
JW
2411 default:
2412 break;
2413 }
2414
c14b9960
JW
2415 return 0;
2416}
2417
da7d8304 2418/* Returns nonzero if X might mention something which is not
c14b9960 2419 local to the function and is not constant. */
7790df19 2420
c14b9960
JW
2421static int
2422nonlocal_mentioned_p (x)
2423 rtx x;
2424{
2425
2426 if (INSN_P (x))
2427 {
2428 if (GET_CODE (x) == CALL_INSN)
2429 {
2430 if (! CONST_OR_PURE_CALL_P (x))
2431 return 1;
2432 x = CALL_INSN_FUNCTION_USAGE (x);
2433 if (x == 0)
2434 return 0;
ca7fd9cd 2435 }
c14b9960 2436 else
ca7fd9cd 2437 x = PATTERN (x);
c14b9960
JW
2438 }
2439
2440 return for_each_rtx (&x, nonlocal_mentioned_p_1, NULL);
2441}
2442
2443/* A subroutine of nonlocal_referenced_p, returns 1 if *LOC references
2444 something which is not local to the function and is not constant. */
2445
2446static int
2447nonlocal_referenced_p_1 (loc, data)
2448 rtx *loc;
2449 void *data ATTRIBUTE_UNUSED;
2450{
2451 rtx x = *loc;
2452
2453 if (! x)
2454 return 0;
2455
2456 switch (GET_CODE (x))
2457 {
2458 case MEM:
2459 case REG:
2460 case SYMBOL_REF:
2461 case SUBREG:
2462 return nonlocal_mentioned_p (x);
2463
2464 case CALL:
2465 /* Non-constant calls and recursion are not local. */
2466 return 1;
2467
2468 case SET:
2469 if (nonlocal_mentioned_p (SET_SRC (x)))
2470 return 1;
2471
2472 if (GET_CODE (SET_DEST (x)) == MEM)
2473 return nonlocal_mentioned_p (XEXP (SET_DEST (x), 0));
2474
2475 /* If the destination is anything other than a CC0, PC,
2476 MEM, REG, or a SUBREG of a REG that occupies all of
2477 the REG, then X references nonlocal memory if it is
2478 mentioned in the destination. */
2479 if (GET_CODE (SET_DEST (x)) != CC0
2480 && GET_CODE (SET_DEST (x)) != PC
2481 && GET_CODE (SET_DEST (x)) != REG
2482 && ! (GET_CODE (SET_DEST (x)) == SUBREG
2483 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
2484 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
2485 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
2486 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2487 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
2488 return nonlocal_mentioned_p (SET_DEST (x));
2489 return 0;
2490
2491 case CLOBBER:
2492 if (GET_CODE (XEXP (x, 0)) == MEM)
2493 return nonlocal_mentioned_p (XEXP (XEXP (x, 0), 0));
2494 return 0;
2495
2496 case USE:
2497 return nonlocal_mentioned_p (XEXP (x, 0));
2498
2499 case ASM_INPUT:
2500 case UNSPEC_VOLATILE:
2501 return 1;
2502
2503 case ASM_OPERANDS:
2504 if (MEM_VOLATILE_P (x))
2505 return 1;
2506
2507 /* FALLTHROUGH */
2508
2509 default:
2510 break;
2511 }
2512
2513 return 0;
2514}
2515
da7d8304 2516/* Returns nonzero if X might reference something which is not
c14b9960
JW
2517 local to the function and is not constant. */
2518
2519static int
2520nonlocal_referenced_p (x)
2521 rtx x;
2522{
2523
2524 if (INSN_P (x))
2525 {
2526 if (GET_CODE (x) == CALL_INSN)
2527 {
2528 if (! CONST_OR_PURE_CALL_P (x))
2529 return 1;
2530 x = CALL_INSN_FUNCTION_USAGE (x);
2531 if (x == 0)
2532 return 0;
ca7fd9cd 2533 }
c14b9960 2534 else
ca7fd9cd 2535 x = PATTERN (x);
c14b9960
JW
2536 }
2537
2538 return for_each_rtx (&x, nonlocal_referenced_p_1, NULL);
2539}
2540
2541/* A subroutine of nonlocal_set_p, returns 1 if *LOC sets
2542 something which is not local to the function and is not constant. */
2543
2544static int
2545nonlocal_set_p_1 (loc, data)
2546 rtx *loc;
2547 void *data ATTRIBUTE_UNUSED;
2548{
2549 rtx x = *loc;
2550
2551 if (! x)
2552 return 0;
2553
2554 switch (GET_CODE (x))
2555 {
2556 case CALL:
2557 /* Non-constant calls and recursion are not local. */
2558 return 1;
2559
2560 case PRE_INC:
2561 case PRE_DEC:
2562 case POST_INC:
2563 case POST_DEC:
2564 case PRE_MODIFY:
2565 case POST_MODIFY:
2566 return nonlocal_mentioned_p (XEXP (x, 0));
2567
2568 case SET:
2569 if (nonlocal_mentioned_p (SET_DEST (x)))
2570 return 1;
2571 return nonlocal_set_p (SET_SRC (x));
2572
2573 case CLOBBER:
2574 return nonlocal_mentioned_p (XEXP (x, 0));
2575
2576 case USE:
2577 return 0;
2578
2579 case ASM_INPUT:
2580 case UNSPEC_VOLATILE:
2581 return 1;
2582
2583 case ASM_OPERANDS:
2584 if (MEM_VOLATILE_P (x))
2585 return 1;
2586
2587 /* FALLTHROUGH */
2588
2589 default:
2590 break;
2591 }
7790df19
JW
2592
2593 return 0;
2594}
2595
da7d8304 2596/* Returns nonzero if X might set something which is not
c14b9960
JW
2597 local to the function and is not constant. */
2598
2599static int
2600nonlocal_set_p (x)
2601 rtx x;
2602{
2603
2604 if (INSN_P (x))
2605 {
2606 if (GET_CODE (x) == CALL_INSN)
2607 {
2608 if (! CONST_OR_PURE_CALL_P (x))
2609 return 1;
2610 x = CALL_INSN_FUNCTION_USAGE (x);
2611 if (x == 0)
2612 return 0;
ca7fd9cd 2613 }
c14b9960 2614 else
ca7fd9cd 2615 x = PATTERN (x);
c14b9960
JW
2616 }
2617
2618 return for_each_rtx (&x, nonlocal_set_p_1, NULL);
2619}
2620
7790df19
JW
2621/* Mark the function if it is constant. */
2622
2623void
2624mark_constant_function ()
2625{
2626 rtx insn;
c14b9960 2627 int nonlocal_memory_referenced;
7790df19 2628
ab780373 2629 if (TREE_READONLY (current_function_decl)
bf6d9fd7 2630 || DECL_IS_PURE (current_function_decl)
7790df19 2631 || TREE_THIS_VOLATILE (current_function_decl)
c14b9960 2632 || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode
ab780373
RH
2633 || current_function_has_nonlocal_goto
2634 || !(*targetm.binds_local_p) (current_function_decl))
7790df19
JW
2635 return;
2636
e004f2f7 2637 /* A loop might not return which counts as a side effect. */
0ecf09f9 2638 if (mark_dfs_back_edges ())
e004f2f7
JW
2639 return;
2640
c14b9960 2641 nonlocal_memory_referenced = 0;
bf6d9fd7
JW
2642
2643 init_alias_analysis ();
2644
c14b9960 2645 /* Determine if this is a constant or pure function. */
7790df19
JW
2646
2647 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
c14b9960
JW
2648 {
2649 if (! INSN_P (insn))
2650 continue;
bf6d9fd7 2651
c14b9960
JW
2652 if (nonlocal_set_p (insn) || global_reg_mentioned_p (insn)
2653 || volatile_refs_p (PATTERN (insn)))
ca7fd9cd 2654 break;
7790df19 2655
c14b9960
JW
2656 if (! nonlocal_memory_referenced)
2657 nonlocal_memory_referenced = nonlocal_referenced_p (insn);
2658 }
ca7fd9cd 2659
c14b9960 2660 end_alias_analysis ();
ca7fd9cd 2661
7790df19 2662 /* Mark the function. */
ca7fd9cd 2663
c14b9960
JW
2664 if (insn)
2665 ;
2666 else if (nonlocal_memory_referenced)
2667 DECL_IS_PURE (current_function_decl) = 1;
2668 else
bf6d9fd7 2669 TREE_READONLY (current_function_decl) = 1;
7790df19 2670}
c14b9960 2671\f
6e73e666 2672
6e73e666
JC
2673void
2674init_alias_once ()
2675{
b3694847 2676 int i;
6e73e666
JC
2677
2678#ifndef OUTGOING_REGNO
2679#define OUTGOING_REGNO(N) N
2680#endif
2681 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2682 /* Check whether this register can hold an incoming pointer
2683 argument. FUNCTION_ARG_REGNO_P tests outgoing register
ec5c56db 2684 numbers, so translate if necessary due to register windows. */
6e73e666
JC
2685 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2686 && HARD_REGNO_MODE_OK (i, Pmode))
bf1660a6
JL
2687 static_reg_base_value[i]
2688 = gen_rtx_ADDRESS (VOIDmode, gen_rtx_REG (Pmode, i));
2689
bf1660a6
JL
2690 static_reg_base_value[STACK_POINTER_REGNUM]
2691 = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
2692 static_reg_base_value[ARG_POINTER_REGNUM]
2693 = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
2694 static_reg_base_value[FRAME_POINTER_REGNUM]
2695 = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
2696#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2697 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2698 = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
2699#endif
7f243674
JL
2700
2701 alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
bf1660a6
JL
2702}
2703
c13e8210
MM
2704/* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2705 array. */
2706
9ae8ffe7
JL
2707void
2708init_alias_analysis ()
2709{
2710 int maxreg = max_reg_num ();
ea64ef27 2711 int changed, pass;
b3694847
SS
2712 int i;
2713 unsigned int ui;
2714 rtx insn;
9ae8ffe7
JL
2715
2716 reg_known_value_size = maxreg;
2717
ca7fd9cd 2718 reg_known_value
e05e2395
MM
2719 = (rtx *) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (rtx))
2720 - FIRST_PSEUDO_REGISTER;
ca7fd9cd 2721 reg_known_equiv_p
e05e2395 2722 = (char*) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (char))
9ae8ffe7 2723 - FIRST_PSEUDO_REGISTER;
9ae8ffe7 2724
6e73e666
JC
2725 /* Overallocate reg_base_value to allow some growth during loop
2726 optimization. Loop unrolling can create a large number of
2727 registers. */
2728 reg_base_value_size = maxreg * 2;
e2500fed
GK
2729 reg_base_value = (rtx *) ggc_alloc_cleared (reg_base_value_size
2730 * sizeof (rtx));
ac606739 2731
e05e2395
MM
2732 new_reg_base_value = (rtx *) xmalloc (reg_base_value_size * sizeof (rtx));
2733 reg_seen = (char *) xmalloc (reg_base_value_size);
de12be17
JC
2734 if (! reload_completed && flag_unroll_loops)
2735 {
ac606739 2736 /* ??? Why are we realloc'ing if we're just going to zero it? */
de12be17
JC
2737 alias_invariant = (rtx *)xrealloc (alias_invariant,
2738 reg_base_value_size * sizeof (rtx));
961192e1 2739 memset ((char *)alias_invariant, 0, reg_base_value_size * sizeof (rtx));
de12be17 2740 }
ec907dd8
JL
2741
2742 /* The basic idea is that each pass through this loop will use the
2743 "constant" information from the previous pass to propagate alias
2744 information through another level of assignments.
2745
2746 This could get expensive if the assignment chains are long. Maybe
2747 we should throttle the number of iterations, possibly based on
6e73e666 2748 the optimization level or flag_expensive_optimizations.
ec907dd8
JL
2749
2750 We could propagate more information in the first pass by making use
2751 of REG_N_SETS to determine immediately that the alias information
ea64ef27
JL
2752 for a pseudo is "constant".
2753
2754 A program with an uninitialized variable can cause an infinite loop
2755 here. Instead of doing a full dataflow analysis to detect such problems
2756 we just cap the number of iterations for the loop.
2757
2758 The state of the arrays for the set chain in question does not matter
2759 since the program has undefined behavior. */
6e73e666 2760
ea64ef27 2761 pass = 0;
6e73e666 2762 do
ec907dd8
JL
2763 {
2764 /* Assume nothing will change this iteration of the loop. */
2765 changed = 0;
2766
ec907dd8
JL
2767 /* We want to assign the same IDs each iteration of this loop, so
2768 start counting from zero each iteration of the loop. */
2769 unique_id = 0;
2770
f5143c46 2771 /* We're at the start of the function each iteration through the
ec907dd8 2772 loop, so we're copying arguments. */
83bbd9b6 2773 copying_arguments = true;
9ae8ffe7 2774
6e73e666 2775 /* Wipe the potential alias information clean for this pass. */
961192e1 2776 memset ((char *) new_reg_base_value, 0, reg_base_value_size * sizeof (rtx));
8072f69c 2777
6e73e666 2778 /* Wipe the reg_seen array clean. */
961192e1 2779 memset ((char *) reg_seen, 0, reg_base_value_size);
9ae8ffe7 2780
6e73e666
JC
2781 /* Mark all hard registers which may contain an address.
2782 The stack, frame and argument pointers may contain an address.
2783 An argument register which can hold a Pmode value may contain
2784 an address even if it is not in BASE_REGS.
8072f69c 2785
6e73e666
JC
2786 The address expression is VOIDmode for an argument and
2787 Pmode for other registers. */
2788
7f243674
JL
2789 memcpy (new_reg_base_value, static_reg_base_value,
2790 FIRST_PSEUDO_REGISTER * sizeof (rtx));
6e73e666 2791
ec907dd8
JL
2792 /* Walk the insns adding values to the new_reg_base_value array. */
2793 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
9ae8ffe7 2794 {
2c3c49de 2795 if (INSN_P (insn))
ec907dd8 2796 {
6e73e666 2797 rtx note, set;
efc9bd41
RK
2798
2799#if defined (HAVE_prologue) || defined (HAVE_epilogue)
f5143c46 2800 /* The prologue/epilogue insns are not threaded onto the
657959ca
JL
2801 insn chain until after reload has completed. Thus,
2802 there is no sense wasting time checking if INSN is in
2803 the prologue/epilogue until after reload has completed. */
2804 if (reload_completed
2805 && prologue_epilogue_contains (insn))
efc9bd41
RK
2806 continue;
2807#endif
2808
ec907dd8
JL
2809 /* If this insn has a noalias note, process it, Otherwise,
2810 scan for sets. A simple set will have no side effects
ec5c56db 2811 which could change the base value of any other register. */
6e73e666 2812
ec907dd8 2813 if (GET_CODE (PATTERN (insn)) == SET
efc9bd41
RK
2814 && REG_NOTES (insn) != 0
2815 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
84832317 2816 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
ec907dd8 2817 else
84832317 2818 note_stores (PATTERN (insn), record_set, NULL);
6e73e666
JC
2819
2820 set = single_set (insn);
2821
2822 if (set != 0
2823 && GET_CODE (SET_DEST (set)) == REG
fb6754f0 2824 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
6e73e666 2825 {
fb6754f0 2826 unsigned int regno = REGNO (SET_DEST (set));
713f41f9
BS
2827 rtx src = SET_SRC (set);
2828
2829 if (REG_NOTES (insn) != 0
2830 && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2831 && REG_N_SETS (regno) == 1)
2832 || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
2833 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
bb2cf916 2834 && ! rtx_varies_p (XEXP (note, 0), 1)
713f41f9
BS
2835 && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
2836 {
2837 reg_known_value[regno] = XEXP (note, 0);
2838 reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
2839 }
2840 else if (REG_N_SETS (regno) == 1
2841 && GET_CODE (src) == PLUS
2842 && GET_CODE (XEXP (src, 0)) == REG
fb6754f0
BS
2843 && REGNO (XEXP (src, 0)) >= FIRST_PSEUDO_REGISTER
2844 && (reg_known_value[REGNO (XEXP (src, 0))])
713f41f9
BS
2845 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2846 {
2847 rtx op0 = XEXP (src, 0);
bb2cf916 2848 op0 = reg_known_value[REGNO (op0)];
713f41f9 2849 reg_known_value[regno]
ed8908e7 2850 = plus_constant (op0, INTVAL (XEXP (src, 1)));
713f41f9
BS
2851 reg_known_equiv_p[regno] = 0;
2852 }
2853 else if (REG_N_SETS (regno) == 1
2854 && ! rtx_varies_p (src, 1))
2855 {
2856 reg_known_value[regno] = src;
2857 reg_known_equiv_p[regno] = 0;
2858 }
6e73e666 2859 }
ec907dd8
JL
2860 }
2861 else if (GET_CODE (insn) == NOTE
2862 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
83bbd9b6 2863 copying_arguments = false;
6e73e666 2864 }
ec907dd8 2865
6e73e666 2866 /* Now propagate values from new_reg_base_value to reg_base_value. */
e51712db 2867 for (ui = 0; ui < reg_base_value_size; ui++)
6e73e666 2868 {
e51712db
KG
2869 if (new_reg_base_value[ui]
2870 && new_reg_base_value[ui] != reg_base_value[ui]
2871 && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
ec907dd8 2872 {
e51712db 2873 reg_base_value[ui] = new_reg_base_value[ui];
6e73e666 2874 changed = 1;
ec907dd8 2875 }
9ae8ffe7 2876 }
9ae8ffe7 2877 }
6e73e666 2878 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
9ae8ffe7
JL
2879
2880 /* Fill in the remaining entries. */
2881 for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
2882 if (reg_known_value[i] == 0)
2883 reg_known_value[i] = regno_reg_rtx[i];
2884
9ae8ffe7
JL
2885 /* Simplify the reg_base_value array so that no register refers to
2886 another register, except to special registers indirectly through
2887 ADDRESS expressions.
2888
2889 In theory this loop can take as long as O(registers^2), but unless
2890 there are very long dependency chains it will run in close to linear
ea64ef27
JL
2891 time.
2892
2893 This loop may not be needed any longer now that the main loop does
2894 a better job at propagating alias information. */
2895 pass = 0;
9ae8ffe7
JL
2896 do
2897 {
2898 changed = 0;
ea64ef27 2899 pass++;
e51712db 2900 for (ui = 0; ui < reg_base_value_size; ui++)
9ae8ffe7 2901 {
e51712db 2902 rtx base = reg_base_value[ui];
9ae8ffe7
JL
2903 if (base && GET_CODE (base) == REG)
2904 {
fb6754f0 2905 unsigned int base_regno = REGNO (base);
e51712db
KG
2906 if (base_regno == ui) /* register set from itself */
2907 reg_base_value[ui] = 0;
9ae8ffe7 2908 else
e51712db 2909 reg_base_value[ui] = reg_base_value[base_regno];
9ae8ffe7
JL
2910 changed = 1;
2911 }
2912 }
2913 }
ea64ef27 2914 while (changed && pass < MAX_ALIAS_LOOP_PASSES);
9ae8ffe7 2915
e05e2395
MM
2916 /* Clean up. */
2917 free (new_reg_base_value);
ec907dd8 2918 new_reg_base_value = 0;
e05e2395 2919 free (reg_seen);
9ae8ffe7
JL
2920 reg_seen = 0;
2921}
2922
2923void
2924end_alias_analysis ()
2925{
e05e2395 2926 free (reg_known_value + FIRST_PSEUDO_REGISTER);
9ae8ffe7 2927 reg_known_value = 0;
ac606739 2928 reg_known_value_size = 0;
e05e2395
MM
2929 free (reg_known_equiv_p + FIRST_PSEUDO_REGISTER);
2930 reg_known_equiv_p = 0;
e2500fed 2931 reg_base_value = 0;
9ae8ffe7 2932 reg_base_value_size = 0;
de12be17
JC
2933 if (alias_invariant)
2934 {
ac606739 2935 free (alias_invariant);
de12be17
JC
2936 alias_invariant = 0;
2937 }
9ae8ffe7 2938}
e2500fed
GK
2939
2940#include "gt-alias.h"