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