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