]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Alias analysis for trees. |
36dc1a88 | 2 | Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
66647d44 | 3 | Free Software Foundation, Inc. |
6de9cd9a DN |
4 | Contributed by Diego Novillo <dnovillo@redhat.com> |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
9dcd6f09 | 10 | the Free Software Foundation; either version 3, or (at your option) |
6de9cd9a DN |
11 | any later version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "tm.h" | |
26 | #include "tree.h" | |
6de9cd9a | 27 | #include "tm_p.h" |
7352c013 | 28 | #include "target.h" |
6de9cd9a DN |
29 | #include "basic-block.h" |
30 | #include "timevar.h" | |
6de9cd9a DN |
31 | #include "ggc.h" |
32 | #include "langhooks.h" | |
33 | #include "flags.h" | |
34 | #include "function.h" | |
cf835838 | 35 | #include "tree-pretty-print.h" |
6de9cd9a | 36 | #include "tree-dump.h" |
726a989a | 37 | #include "gimple.h" |
6de9cd9a DN |
38 | #include "tree-flow.h" |
39 | #include "tree-inline.h" | |
6de9cd9a DN |
40 | #include "tree-pass.h" |
41 | #include "convert.h" | |
42 | #include "params.h" | |
c75ab022 | 43 | #include "vec.h" |
a3648cfc | 44 | #include "bitmap.h" |
e3df376d | 45 | #include "vecprim.h" |
38635499 | 46 | #include "pointer-set.h" |
603802e7 | 47 | #include "alloc-pool.h" |
5006671f | 48 | #include "tree-ssa-alias.h" |
a3648cfc | 49 | |
5006671f | 50 | /* Broad overview of how alias analysis on gimple works: |
38635499 | 51 | |
5006671f RG |
52 | Statements clobbering or using memory are linked through the |
53 | virtual operand factored use-def chain. The virtual operand | |
54 | is unique per function, its symbol is accessible via gimple_vop (cfun). | |
55 | Virtual operands are used for efficiently walking memory statements | |
56 | in the gimple IL and are useful for things like value-numbering as | |
57 | a generation count for memory references. | |
faf2ecc5 | 58 | |
5006671f RG |
59 | SSA_NAME pointers may have associated points-to information |
60 | accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive | |
61 | points-to information is (re-)computed by the TODO_rebuild_alias | |
62 | pass manager todo. Points-to information is also used for more | |
63 | precise tracking of call-clobbered and call-used variables and | |
64 | related disambiguations. | |
faf2ecc5 | 65 | |
5006671f RG |
66 | This file contains functions for disambiguating memory references, |
67 | the so called alias-oracle and tools for walking of the gimple IL. | |
faf2ecc5 | 68 | |
5006671f | 69 | The main alias-oracle entry-points are |
faf2ecc5 | 70 | |
5006671f | 71 | bool stmt_may_clobber_ref_p (gimple, tree) |
faf2ecc5 | 72 | |
5006671f RG |
73 | This function queries if a statement may invalidate (parts of) |
74 | the memory designated by the reference tree argument. | |
faf2ecc5 | 75 | |
5006671f | 76 | bool ref_maybe_used_by_stmt_p (gimple, tree) |
faf2ecc5 | 77 | |
5006671f RG |
78 | This function queries if a statement may need (parts of) the |
79 | memory designated by the reference tree argument. | |
faf2ecc5 | 80 | |
5006671f RG |
81 | There are variants of these functions that only handle the call |
82 | part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p. | |
83 | Note that these do not disambiguate against a possible call lhs. | |
faf2ecc5 | 84 | |
5006671f | 85 | bool refs_may_alias_p (tree, tree) |
faf2ecc5 | 86 | |
5006671f | 87 | This function tries to disambiguate two reference trees. |
63894637 | 88 | |
5006671f | 89 | bool ptr_deref_may_alias_global_p (tree) |
faf2ecc5 | 90 | |
5006671f RG |
91 | This function queries if dereferencing a pointer variable may |
92 | alias global memory. | |
faf2ecc5 | 93 | |
5006671f RG |
94 | More low-level disambiguators are available and documented in |
95 | this file. Low-level disambiguators dealing with points-to | |
96 | information are in tree-ssa-structalias.c. */ | |
faf2ecc5 | 97 | |
faf2ecc5 | 98 | |
5006671f RG |
99 | /* Query statistics for the different low-level disambiguators. |
100 | A high-level query may trigger multiple of them. */ | |
faf2ecc5 | 101 | |
5006671f RG |
102 | static struct { |
103 | unsigned HOST_WIDE_INT refs_may_alias_p_may_alias; | |
104 | unsigned HOST_WIDE_INT refs_may_alias_p_no_alias; | |
105 | unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias; | |
106 | unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias; | |
107 | unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias; | |
108 | unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias; | |
109 | } alias_stats; | |
faf2ecc5 | 110 | |
5006671f RG |
111 | void |
112 | dump_alias_stats (FILE *s) | |
113 | { | |
114 | fprintf (s, "\nAlias oracle query stats:\n"); | |
115 | fprintf (s, " refs_may_alias_p: " | |
116 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
117 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
118 | alias_stats.refs_may_alias_p_no_alias, | |
119 | alias_stats.refs_may_alias_p_no_alias | |
120 | + alias_stats.refs_may_alias_p_may_alias); | |
121 | fprintf (s, " ref_maybe_used_by_call_p: " | |
122 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
123 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
124 | alias_stats.ref_maybe_used_by_call_p_no_alias, | |
125 | alias_stats.refs_may_alias_p_no_alias | |
126 | + alias_stats.ref_maybe_used_by_call_p_may_alias); | |
127 | fprintf (s, " call_may_clobber_ref_p: " | |
128 | HOST_WIDE_INT_PRINT_DEC" disambiguations, " | |
129 | HOST_WIDE_INT_PRINT_DEC" queries\n", | |
130 | alias_stats.call_may_clobber_ref_p_no_alias, | |
131 | alias_stats.call_may_clobber_ref_p_no_alias | |
132 | + alias_stats.call_may_clobber_ref_p_may_alias); | |
133 | } | |
134 | ||
135 | ||
136 | /* Return true, if dereferencing PTR may alias with a global variable. */ | |
6de9cd9a | 137 | |
5006671f RG |
138 | bool |
139 | ptr_deref_may_alias_global_p (tree ptr) | |
6de9cd9a | 140 | { |
5006671f | 141 | struct ptr_info_def *pi; |
38635499 | 142 | |
5006671f RG |
143 | /* If we end up with a pointer constant here that may point |
144 | to global memory. */ | |
145 | if (TREE_CODE (ptr) != SSA_NAME) | |
146 | return true; | |
6de9cd9a | 147 | |
5006671f | 148 | pi = SSA_NAME_PTR_INFO (ptr); |
6de9cd9a | 149 | |
5006671f RG |
150 | /* If we do not have points-to information for this variable, |
151 | we have to punt. */ | |
152 | if (!pi) | |
153 | return true; | |
6de9cd9a | 154 | |
4d7a65ea | 155 | /* ??? This does not use TBAA to prune globals ptr may not access. */ |
5006671f | 156 | return pt_solution_includes_global (&pi->pt); |
6de9cd9a DN |
157 | } |
158 | ||
4d7a65ea RG |
159 | /* Return true if dereferencing PTR may alias DECL. |
160 | The caller is responsible for applying TBAA to see if PTR | |
161 | may access DECL at all. */ | |
6de9cd9a | 162 | |
5006671f RG |
163 | static bool |
164 | ptr_deref_may_alias_decl_p (tree ptr, tree decl) | |
6de9cd9a | 165 | { |
5006671f | 166 | struct ptr_info_def *pi; |
ea900239 | 167 | |
7d36e538 RG |
168 | /* Conversions are irrelevant for points-to information and |
169 | data-dependence analysis can feed us those. */ | |
170 | STRIP_NOPS (ptr); | |
171 | ||
172 | /* Anything we do not explicilty handle aliases. */ | |
173 | if ((TREE_CODE (ptr) != SSA_NAME | |
174 | && TREE_CODE (ptr) != ADDR_EXPR | |
175 | && TREE_CODE (ptr) != POINTER_PLUS_EXPR) | |
176 | || !POINTER_TYPE_P (TREE_TYPE (ptr)) | |
177 | || (TREE_CODE (decl) != VAR_DECL | |
178 | && TREE_CODE (decl) != PARM_DECL | |
179 | && TREE_CODE (decl) != RESULT_DECL)) | |
4b1a5c0d | 180 | return true; |
fd73537b | 181 | |
7d36e538 RG |
182 | /* Disregard pointer offsetting. */ |
183 | if (TREE_CODE (ptr) == POINTER_PLUS_EXPR) | |
184 | { | |
185 | do | |
186 | { | |
187 | ptr = TREE_OPERAND (ptr, 0); | |
188 | } | |
189 | while (TREE_CODE (ptr) == POINTER_PLUS_EXPR); | |
190 | return ptr_deref_may_alias_decl_p (ptr, decl); | |
191 | } | |
192 | ||
779704e7 RG |
193 | /* ADDR_EXPR pointers either just offset another pointer or directly |
194 | specify the pointed-to set. */ | |
195 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
196 | { | |
197 | tree base = get_base_address (TREE_OPERAND (ptr, 0)); | |
198 | if (base | |
e19f6650 RG |
199 | && (TREE_CODE (base) == MEM_REF |
200 | || TREE_CODE (base) == TARGET_MEM_REF)) | |
779704e7 RG |
201 | ptr = TREE_OPERAND (base, 0); |
202 | else if (base | |
e19f6650 | 203 | && DECL_P (base)) |
2ea9dc64 | 204 | return base == decl; |
779704e7 RG |
205 | else if (base |
206 | && CONSTANT_CLASS_P (base)) | |
207 | return false; | |
208 | else | |
209 | return true; | |
210 | } | |
211 | ||
7d36e538 RG |
212 | /* Non-aliased variables can not be pointed to. */ |
213 | if (!may_be_aliased (decl)) | |
214 | return false; | |
779704e7 | 215 | |
5006671f RG |
216 | /* If we do not have useful points-to information for this pointer |
217 | we cannot disambiguate anything else. */ | |
218 | pi = SSA_NAME_PTR_INFO (ptr); | |
219 | if (!pi) | |
fd73537b RG |
220 | return true; |
221 | ||
5006671f | 222 | return pt_solution_includes (&pi->pt, decl); |
fd73537b | 223 | } |
6de9cd9a | 224 | |
4d7a65ea RG |
225 | /* Return true if dereferenced PTR1 and PTR2 may alias. |
226 | The caller is responsible for applying TBAA to see if accesses | |
227 | through PTR1 and PTR2 may conflict at all. */ | |
6de9cd9a | 228 | |
7d36e538 | 229 | bool |
5006671f | 230 | ptr_derefs_may_alias_p (tree ptr1, tree ptr2) |
6de9cd9a | 231 | { |
5006671f | 232 | struct ptr_info_def *pi1, *pi2; |
6de9cd9a | 233 | |
7d36e538 RG |
234 | /* Conversions are irrelevant for points-to information and |
235 | data-dependence analysis can feed us those. */ | |
236 | STRIP_NOPS (ptr1); | |
237 | STRIP_NOPS (ptr2); | |
238 | ||
239 | /* Anything we do not explicilty handle aliases. */ | |
240 | if ((TREE_CODE (ptr1) != SSA_NAME | |
241 | && TREE_CODE (ptr1) != ADDR_EXPR | |
242 | && TREE_CODE (ptr1) != POINTER_PLUS_EXPR) | |
243 | || (TREE_CODE (ptr2) != SSA_NAME | |
244 | && TREE_CODE (ptr2) != ADDR_EXPR | |
245 | && TREE_CODE (ptr2) != POINTER_PLUS_EXPR) | |
246 | || !POINTER_TYPE_P (TREE_TYPE (ptr1)) | |
247 | || !POINTER_TYPE_P (TREE_TYPE (ptr2))) | |
248 | return true; | |
249 | ||
250 | /* Disregard pointer offsetting. */ | |
251 | if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR) | |
252 | { | |
253 | do | |
254 | { | |
255 | ptr1 = TREE_OPERAND (ptr1, 0); | |
256 | } | |
257 | while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR); | |
258 | return ptr_derefs_may_alias_p (ptr1, ptr2); | |
259 | } | |
260 | if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR) | |
261 | { | |
262 | do | |
263 | { | |
264 | ptr2 = TREE_OPERAND (ptr2, 0); | |
265 | } | |
266 | while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR); | |
267 | return ptr_derefs_may_alias_p (ptr1, ptr2); | |
268 | } | |
63838157 | 269 | |
779704e7 RG |
270 | /* ADDR_EXPR pointers either just offset another pointer or directly |
271 | specify the pointed-to set. */ | |
272 | if (TREE_CODE (ptr1) == ADDR_EXPR) | |
273 | { | |
274 | tree base = get_base_address (TREE_OPERAND (ptr1, 0)); | |
275 | if (base | |
e19f6650 RG |
276 | && (TREE_CODE (base) == MEM_REF |
277 | || TREE_CODE (base) == TARGET_MEM_REF)) | |
779704e7 RG |
278 | ptr1 = TREE_OPERAND (base, 0); |
279 | else if (base | |
e19f6650 | 280 | && DECL_P (base)) |
779704e7 RG |
281 | return ptr_deref_may_alias_decl_p (ptr2, base); |
282 | else | |
283 | return true; | |
284 | } | |
285 | if (TREE_CODE (ptr2) == ADDR_EXPR) | |
286 | { | |
287 | tree base = get_base_address (TREE_OPERAND (ptr2, 0)); | |
288 | if (base | |
e19f6650 RG |
289 | && (TREE_CODE (base) == MEM_REF |
290 | || TREE_CODE (base) == TARGET_MEM_REF)) | |
779704e7 RG |
291 | ptr2 = TREE_OPERAND (base, 0); |
292 | else if (base | |
e19f6650 | 293 | && DECL_P (base)) |
779704e7 RG |
294 | return ptr_deref_may_alias_decl_p (ptr1, base); |
295 | else | |
296 | return true; | |
297 | } | |
298 | ||
5006671f RG |
299 | /* We may end up with two empty points-to solutions for two same pointers. |
300 | In this case we still want to say both pointers alias, so shortcut | |
301 | that here. */ | |
302 | if (ptr1 == ptr2) | |
303 | return true; | |
53b4bf74 | 304 | |
5006671f RG |
305 | /* If we do not have useful points-to information for either pointer |
306 | we cannot disambiguate anything else. */ | |
307 | pi1 = SSA_NAME_PTR_INFO (ptr1); | |
308 | pi2 = SSA_NAME_PTR_INFO (ptr2); | |
309 | if (!pi1 || !pi2) | |
310 | return true; | |
53b4bf74 | 311 | |
4d7a65ea RG |
312 | /* ??? This does not use TBAA to prune decls from the intersection |
313 | that not both pointers may access. */ | |
5006671f | 314 | return pt_solutions_intersect (&pi1->pt, &pi2->pt); |
53b4bf74 DN |
315 | } |
316 | ||
779704e7 RG |
317 | /* Return true if dereferencing PTR may alias *REF. |
318 | The caller is responsible for applying TBAA to see if PTR | |
319 | may access *REF at all. */ | |
320 | ||
321 | static bool | |
322 | ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref) | |
323 | { | |
324 | tree base = ao_ref_base (ref); | |
325 | ||
e19f6650 RG |
326 | if (TREE_CODE (base) == MEM_REF |
327 | || TREE_CODE (base) == TARGET_MEM_REF) | |
779704e7 | 328 | return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0)); |
e19f6650 | 329 | else if (DECL_P (base)) |
779704e7 RG |
330 | return ptr_deref_may_alias_decl_p (ptr, base); |
331 | ||
332 | return true; | |
333 | } | |
334 | ||
6de9cd9a DN |
335 | |
336 | /* Dump alias information on FILE. */ | |
337 | ||
338 | void | |
339 | dump_alias_info (FILE *file) | |
340 | { | |
341 | size_t i; | |
342 | const char *funcname | |
673fda6b | 343 | = lang_hooks.decl_printable_name (current_function_decl, 2); |
a3648cfc DB |
344 | referenced_var_iterator rvi; |
345 | tree var; | |
6de9cd9a | 346 | |
5006671f | 347 | fprintf (file, "\n\nAlias information for %s\n\n", funcname); |
53b4bf74 DN |
348 | |
349 | fprintf (file, "Aliased symbols\n\n"); | |
b8698a0f | 350 | |
1b9a784a | 351 | FOR_EACH_REFERENCED_VAR (cfun, var, rvi) |
53b4bf74 | 352 | { |
53b4bf74 DN |
353 | if (may_be_aliased (var)) |
354 | dump_variable (file, var); | |
355 | } | |
356 | ||
6b8ed145 RG |
357 | fprintf (file, "\nCall clobber information\n"); |
358 | ||
359 | fprintf (file, "\nESCAPED"); | |
360 | dump_points_to_solution (file, &cfun->gimple_df->escaped); | |
6b8ed145 RG |
361 | |
362 | fprintf (file, "\n\nFlow-insensitive points-to information\n\n"); | |
53b4bf74 | 363 | |
53b4bf74 DN |
364 | for (i = 1; i < num_ssa_names; i++) |
365 | { | |
366 | tree ptr = ssa_name (i); | |
d804d490 | 367 | struct ptr_info_def *pi; |
b8698a0f | 368 | |
5006671f RG |
369 | if (ptr == NULL_TREE |
370 | || SSA_NAME_IN_FREE_LIST (ptr)) | |
d804d490 DN |
371 | continue; |
372 | ||
373 | pi = SSA_NAME_PTR_INFO (ptr); | |
5006671f | 374 | if (pi) |
53b4bf74 DN |
375 | dump_points_to_info_for (file, ptr); |
376 | } | |
6de9cd9a | 377 | |
6de9cd9a DN |
378 | fprintf (file, "\n"); |
379 | } | |
380 | ||
381 | ||
382 | /* Dump alias information on stderr. */ | |
383 | ||
24e47c76 | 384 | DEBUG_FUNCTION void |
6de9cd9a DN |
385 | debug_alias_info (void) |
386 | { | |
387 | dump_alias_info (stderr); | |
388 | } | |
389 | ||
390 | ||
6b8ed145 | 391 | /* Dump the points-to set *PT into FILE. */ |
6de9cd9a | 392 | |
d8903b30 | 393 | void |
6b8ed145 | 394 | dump_points_to_solution (FILE *file, struct pt_solution *pt) |
6de9cd9a | 395 | { |
6b8ed145 RG |
396 | if (pt->anything) |
397 | fprintf (file, ", points-to anything"); | |
6de9cd9a | 398 | |
6b8ed145 RG |
399 | if (pt->nonlocal) |
400 | fprintf (file, ", points-to non-local"); | |
6de9cd9a | 401 | |
6b8ed145 RG |
402 | if (pt->escaped) |
403 | fprintf (file, ", points-to escaped"); | |
404 | ||
25a6a873 RG |
405 | if (pt->ipa_escaped) |
406 | fprintf (file, ", points-to unit escaped"); | |
407 | ||
6b8ed145 RG |
408 | if (pt->null) |
409 | fprintf (file, ", points-to NULL"); | |
410 | ||
411 | if (pt->vars) | |
6de9cd9a | 412 | { |
6b8ed145 RG |
413 | fprintf (file, ", points-to vars: "); |
414 | dump_decl_set (file, pt->vars); | |
415 | if (pt->vars_contains_global) | |
416 | fprintf (file, " (includes global vars)"); | |
417 | } | |
418 | } | |
6de9cd9a | 419 | |
6b8ed145 | 420 | /* Dump points-to information for SSA_NAME PTR into FILE. */ |
6de9cd9a | 421 | |
6b8ed145 RG |
422 | void |
423 | dump_points_to_info_for (FILE *file, tree ptr) | |
424 | { | |
425 | struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr); | |
6de9cd9a | 426 | |
6b8ed145 | 427 | print_generic_expr (file, ptr, dump_flags); |
a1d13fa1 | 428 | |
6b8ed145 RG |
429 | if (pi) |
430 | dump_points_to_solution (file, &pi->pt); | |
431 | else | |
432 | fprintf (file, ", points-to anything"); | |
6de9cd9a DN |
433 | |
434 | fprintf (file, "\n"); | |
435 | } | |
436 | ||
437 | ||
d8903b30 DN |
438 | /* Dump points-to information for VAR into stderr. */ |
439 | ||
24e47c76 | 440 | DEBUG_FUNCTION void |
d8903b30 DN |
441 | debug_points_to_info_for (tree var) |
442 | { | |
443 | dump_points_to_info_for (stderr, var); | |
444 | } | |
445 | ||
b45d2719 RG |
446 | |
447 | /* Initializes the alias-oracle reference representation *R from REF. */ | |
448 | ||
449 | void | |
450 | ao_ref_init (ao_ref *r, tree ref) | |
451 | { | |
452 | r->ref = ref; | |
453 | r->base = NULL_TREE; | |
454 | r->offset = 0; | |
455 | r->size = -1; | |
456 | r->max_size = -1; | |
457 | r->ref_alias_set = -1; | |
458 | r->base_alias_set = -1; | |
459 | } | |
460 | ||
461 | /* Returns the base object of the memory reference *REF. */ | |
462 | ||
463 | tree | |
464 | ao_ref_base (ao_ref *ref) | |
465 | { | |
466 | if (ref->base) | |
467 | return ref->base; | |
468 | ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size, | |
469 | &ref->max_size); | |
470 | return ref->base; | |
471 | } | |
472 | ||
473 | /* Returns the base object alias set of the memory reference *REF. */ | |
474 | ||
70f34814 | 475 | static alias_set_type |
b45d2719 RG |
476 | ao_ref_base_alias_set (ao_ref *ref) |
477 | { | |
70f34814 | 478 | tree base_ref; |
b45d2719 RG |
479 | if (ref->base_alias_set != -1) |
480 | return ref->base_alias_set; | |
70f34814 RG |
481 | if (!ref->ref) |
482 | return 0; | |
483 | base_ref = ref->ref; | |
484 | while (handled_component_p (base_ref)) | |
485 | base_ref = TREE_OPERAND (base_ref, 0); | |
486 | ref->base_alias_set = get_alias_set (base_ref); | |
b45d2719 RG |
487 | return ref->base_alias_set; |
488 | } | |
489 | ||
490 | /* Returns the reference alias set of the memory reference *REF. */ | |
491 | ||
492 | alias_set_type | |
493 | ao_ref_alias_set (ao_ref *ref) | |
494 | { | |
495 | if (ref->ref_alias_set != -1) | |
496 | return ref->ref_alias_set; | |
497 | ref->ref_alias_set = get_alias_set (ref->ref); | |
498 | return ref->ref_alias_set; | |
499 | } | |
500 | ||
a778c4e7 RG |
501 | /* Init an alias-oracle reference representation from a gimple pointer |
502 | PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the | |
503 | size is assumed to be unknown. The access is assumed to be only | |
504 | to or after of the pointer target, not before it. */ | |
505 | ||
506 | void | |
507 | ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size) | |
508 | { | |
509 | HOST_WIDE_INT t1, t2; | |
510 | ref->ref = NULL_TREE; | |
511 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
512 | ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0), | |
513 | &ref->offset, &t1, &t2); | |
514 | else | |
515 | { | |
70f34814 | 516 | ref->base = build2 (MEM_REF, char_type_node, |
9a9d280e | 517 | ptr, null_pointer_node); |
a778c4e7 RG |
518 | ref->offset = 0; |
519 | } | |
520 | if (size | |
521 | && host_integerp (size, 0) | |
522 | && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size)) | |
523 | ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8; | |
524 | else | |
525 | ref->max_size = ref->size = -1; | |
526 | ref->ref_alias_set = 0; | |
527 | ref->base_alias_set = 0; | |
528 | } | |
529 | ||
5006671f RG |
530 | /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the |
531 | purpose of TBAA. Return 0 if they are distinct and -1 if we cannot | |
532 | decide. */ | |
d8903b30 | 533 | |
5006671f RG |
534 | static inline int |
535 | same_type_for_tbaa (tree type1, tree type2) | |
536 | { | |
537 | type1 = TYPE_MAIN_VARIANT (type1); | |
538 | type2 = TYPE_MAIN_VARIANT (type2); | |
6de9cd9a | 539 | |
5006671f RG |
540 | /* If we would have to do structural comparison bail out. */ |
541 | if (TYPE_STRUCTURAL_EQUALITY_P (type1) | |
542 | || TYPE_STRUCTURAL_EQUALITY_P (type2)) | |
543 | return -1; | |
544 | ||
545 | /* Compare the canonical types. */ | |
546 | if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2)) | |
547 | return 1; | |
548 | ||
55eb4dab | 549 | /* ??? Array types are not properly unified in all cases as we have |
5006671f RG |
550 | spurious changes in the index types for example. Removing this |
551 | causes all sorts of problems with the Fortran frontend. */ | |
552 | if (TREE_CODE (type1) == ARRAY_TYPE | |
553 | && TREE_CODE (type2) == ARRAY_TYPE) | |
554 | return -1; | |
555 | ||
2743db69 EB |
556 | /* ??? In Ada, an lvalue of an unconstrained type can be used to access an |
557 | object of one of its constrained subtypes, e.g. when a function with an | |
558 | unconstrained parameter passed by reference is called on an object and | |
559 | inlined. But, even in the case of a fixed size, type and subtypes are | |
560 | not equivalent enough as to share the same TYPE_CANONICAL, since this | |
561 | would mean that conversions between them are useless, whereas they are | |
562 | not (e.g. type and subtypes can have different modes). So, in the end, | |
563 | they are only guaranteed to have the same alias set. */ | |
564 | if (get_alias_set (type1) == get_alias_set (type2)) | |
55eb4dab EB |
565 | return -1; |
566 | ||
5006671f RG |
567 | /* The types are known to be not equal. */ |
568 | return 0; | |
569 | } | |
570 | ||
571 | /* Determine if the two component references REF1 and REF2 which are | |
572 | based on access types TYPE1 and TYPE2 and of which at least one is based | |
630d3fad RG |
573 | on an indirect reference may alias. REF2 is the only one that can |
574 | be a decl in which case REF2_IS_DECL is true. | |
575 | REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET | |
576 | are the respective alias sets. */ | |
5006671f RG |
577 | |
578 | static bool | |
dafc9511 | 579 | aliasing_component_refs_p (tree ref1, |
630d3fad RG |
580 | alias_set_type ref1_alias_set, |
581 | alias_set_type base1_alias_set, | |
72580319 | 582 | HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1, |
dafc9511 | 583 | tree ref2, |
630d3fad RG |
584 | alias_set_type ref2_alias_set, |
585 | alias_set_type base2_alias_set, | |
586 | HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2, | |
587 | bool ref2_is_decl) | |
5006671f RG |
588 | { |
589 | /* If one reference is a component references through pointers try to find a | |
590 | common base and apply offset based disambiguation. This handles | |
591 | for example | |
592 | struct A { int i; int j; } *q; | |
593 | struct B { struct A a; int k; } *p; | |
594 | disambiguating q->i and p->a.j. */ | |
dafc9511 RG |
595 | tree base1, base2; |
596 | tree type1, type2; | |
5006671f RG |
597 | tree *refp; |
598 | int same_p; | |
599 | ||
dafc9511 RG |
600 | /* Choose bases and base types to search for. */ |
601 | base1 = ref1; | |
602 | while (handled_component_p (base1)) | |
603 | base1 = TREE_OPERAND (base1, 0); | |
604 | type1 = TREE_TYPE (base1); | |
605 | base2 = ref2; | |
606 | while (handled_component_p (base2)) | |
607 | base2 = TREE_OPERAND (base2, 0); | |
608 | type2 = TREE_TYPE (base2); | |
609 | ||
5006671f RG |
610 | /* Now search for the type1 in the access path of ref2. This |
611 | would be a common base for doing offset based disambiguation on. */ | |
71dcd609 | 612 | refp = &ref2; |
5006671f RG |
613 | while (handled_component_p (*refp) |
614 | && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0) | |
615 | refp = &TREE_OPERAND (*refp, 0); | |
616 | same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1); | |
617 | /* If we couldn't compare types we have to bail out. */ | |
618 | if (same_p == -1) | |
619 | return true; | |
620 | else if (same_p == 1) | |
621 | { | |
622 | HOST_WIDE_INT offadj, sztmp, msztmp; | |
623 | get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp); | |
624 | offset2 -= offadj; | |
dafc9511 RG |
625 | get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp); |
626 | offset1 -= offadj; | |
5006671f RG |
627 | return ranges_overlap_p (offset1, max_size1, offset2, max_size2); |
628 | } | |
629 | /* If we didn't find a common base, try the other way around. */ | |
71dcd609 | 630 | refp = &ref1; |
5006671f RG |
631 | while (handled_component_p (*refp) |
632 | && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0) | |
633 | refp = &TREE_OPERAND (*refp, 0); | |
634 | same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2); | |
635 | /* If we couldn't compare types we have to bail out. */ | |
636 | if (same_p == -1) | |
637 | return true; | |
638 | else if (same_p == 1) | |
639 | { | |
640 | HOST_WIDE_INT offadj, sztmp, msztmp; | |
641 | get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp); | |
642 | offset1 -= offadj; | |
dafc9511 RG |
643 | get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp); |
644 | offset2 -= offadj; | |
5006671f RG |
645 | return ranges_overlap_p (offset1, max_size1, offset2, max_size2); |
646 | } | |
630d3fad | 647 | |
55eb4dab | 648 | /* If we have two type access paths B1.path1 and B2.path2 they may |
630d3fad RG |
649 | only alias if either B1 is in B2.path2 or B2 is in B1.path1. |
650 | But we can still have a path that goes B1.path1...B2.path2 with | |
651 | a part that we do not see. So we can only disambiguate now | |
652 | if there is no B2 in the tail of path1 and no B1 on the | |
653 | tail of path2. */ | |
654 | if (base1_alias_set == ref2_alias_set | |
655 | || alias_set_subset_of (base1_alias_set, ref2_alias_set)) | |
656 | return true; | |
657 | /* If this is ptr vs. decl then we know there is no ptr ... decl path. */ | |
658 | if (!ref2_is_decl) | |
659 | return (base2_alias_set == ref1_alias_set | |
660 | || alias_set_subset_of (base2_alias_set, ref1_alias_set)); | |
55eb4dab | 661 | return false; |
5006671f RG |
662 | } |
663 | ||
664 | /* Return true if two memory references based on the variables BASE1 | |
dcbd7063 AP |
665 | and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and |
666 | [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */ | |
5006671f RG |
667 | |
668 | static bool | |
669 | decl_refs_may_alias_p (tree base1, | |
670 | HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1, | |
671 | tree base2, | |
672 | HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2) | |
6de9cd9a | 673 | { |
e19f6650 | 674 | gcc_checking_assert (DECL_P (base1) && DECL_P (base2)); |
6de9cd9a | 675 | |
5006671f | 676 | /* If both references are based on different variables, they cannot alias. */ |
2ea9dc64 | 677 | if (base1 != base2) |
5006671f | 678 | return false; |
6de9cd9a | 679 | |
5006671f RG |
680 | /* If both references are based on the same variable, they cannot alias if |
681 | the accesses do not overlap. */ | |
682 | return ranges_overlap_p (offset1, max_size1, offset2, max_size2); | |
683 | } | |
684 | ||
685 | /* Return true if an indirect reference based on *PTR1 constrained | |
dcbd7063 AP |
686 | to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2 |
687 | constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have | |
5006671f RG |
688 | the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1 |
689 | in which case they are computed on-demand. REF1 and REF2 | |
690 | if non-NULL are the complete memory reference trees. */ | |
691 | ||
692 | static bool | |
70f34814 RG |
693 | indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1, |
694 | HOST_WIDE_INT offset1, | |
695 | HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED, | |
630d3fad | 696 | alias_set_type ref1_alias_set, |
5006671f | 697 | alias_set_type base1_alias_set, |
70f34814 | 698 | tree ref2 ATTRIBUTE_UNUSED, tree base2, |
5006671f | 699 | HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2, |
630d3fad | 700 | alias_set_type ref2_alias_set, |
70f34814 | 701 | alias_set_type base2_alias_set, bool tbaa_p) |
5006671f | 702 | { |
4b228e61 | 703 | tree ptr1; |
29f8b844 | 704 | tree ptrtype1, dbase2; |
dbfcc059 | 705 | HOST_WIDE_INT offset1p = offset1, offset2p = offset2; |
29f8b844 | 706 | HOST_WIDE_INT doffset1, doffset2; |
e19f6650 RG |
707 | double_int moff; |
708 | ||
709 | gcc_checking_assert ((TREE_CODE (base1) == MEM_REF | |
710 | || TREE_CODE (base1) == TARGET_MEM_REF) | |
711 | && DECL_P (base2)); | |
70f34814 | 712 | |
4d948885 | 713 | ptr1 = TREE_OPERAND (base1, 0); |
4b228e61 | 714 | |
dbfcc059 RG |
715 | /* The offset embedded in MEM_REFs can be negative. Bias them |
716 | so that the resulting offset adjustment is positive. */ | |
e19f6650 RG |
717 | moff = mem_ref_offset (base1); |
718 | moff = double_int_lshift (moff, | |
719 | BITS_PER_UNIT == 8 | |
720 | ? 3 : exact_log2 (BITS_PER_UNIT), | |
721 | HOST_BITS_PER_DOUBLE_INT, true); | |
722 | if (double_int_negative_p (moff)) | |
723 | offset2p += double_int_neg (moff).low; | |
724 | else | |
725 | offset1p += moff.low; | |
70f34814 | 726 | |
5006671f RG |
727 | /* If only one reference is based on a variable, they cannot alias if |
728 | the pointer access is beyond the extent of the variable access. | |
729 | (the pointer base cannot validly point to an offset less than zero | |
730 | of the variable). | |
ac8e1875 RG |
731 | ??? IVOPTs creates bases that do not honor this restriction, |
732 | so do not apply this optimization for TARGET_MEM_REFs. */ | |
733 | if (TREE_CODE (base1) != TARGET_MEM_REF | |
4b228e61 | 734 | && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2)) |
5006671f | 735 | return false; |
ac8e1875 | 736 | /* They also cannot alias if the pointer may not point to the decl. */ |
5006671f RG |
737 | if (!ptr_deref_may_alias_decl_p (ptr1, base2)) |
738 | return false; | |
739 | ||
740 | /* Disambiguations that rely on strict aliasing rules follow. */ | |
70f34814 | 741 | if (!flag_strict_aliasing || !tbaa_p) |
5006671f RG |
742 | return true; |
743 | ||
e19f6650 | 744 | ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1)); |
70f34814 | 745 | |
5006671f RG |
746 | /* If the alias set for a pointer access is zero all bets are off. */ |
747 | if (base1_alias_set == -1) | |
70f34814 | 748 | base1_alias_set = get_deref_alias_set (ptrtype1); |
5006671f RG |
749 | if (base1_alias_set == 0) |
750 | return true; | |
751 | if (base2_alias_set == -1) | |
752 | base2_alias_set = get_alias_set (base2); | |
753 | ||
70f34814 RG |
754 | /* When we are trying to disambiguate an access with a pointer dereference |
755 | as base versus one with a decl as base we can use both the size | |
756 | of the decl and its dynamic type for extra disambiguation. | |
757 | ??? We do not know anything about the dynamic type of the decl | |
758 | other than that its alias-set contains base2_alias_set as a subset | |
759 | which does not help us here. */ | |
760 | /* As we know nothing useful about the dynamic type of the decl just | |
761 | use the usual conflict check rather than a subset test. | |
762 | ??? We could introduce -fvery-strict-aliasing when the language | |
763 | does not allow decls to have a dynamic type that differs from their | |
764 | static type. Then we can check | |
765 | !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */ | |
5006671f | 766 | if (base1_alias_set != base2_alias_set |
70f34814 RG |
767 | && !alias_sets_conflict_p (base1_alias_set, base2_alias_set)) |
768 | return false; | |
769 | /* If the size of the access relevant for TBAA through the pointer | |
770 | is bigger than the size of the decl we can't possibly access the | |
771 | decl via that pointer. */ | |
772 | if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1)) | |
773 | && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST | |
774 | && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST | |
775 | /* ??? This in turn may run afoul when a decl of type T which is | |
776 | a member of union type U is accessed through a pointer to | |
777 | type U and sizeof T is smaller than sizeof U. */ | |
778 | && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE | |
779 | && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE | |
780 | && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1)))) | |
5006671f RG |
781 | return false; |
782 | ||
29f8b844 RG |
783 | if (!ref2) |
784 | return true; | |
785 | ||
ac8e1875 | 786 | /* If the decl is accessed via a MEM_REF, reconstruct the base |
29f8b844 RG |
787 | we can use for TBAA and an appropriately adjusted offset. */ |
788 | dbase2 = ref2; | |
789 | while (handled_component_p (dbase2)) | |
790 | dbase2 = TREE_OPERAND (dbase2, 0); | |
791 | doffset1 = offset1; | |
792 | doffset2 = offset2; | |
793 | if (TREE_CODE (dbase2) == MEM_REF | |
794 | || TREE_CODE (dbase2) == TARGET_MEM_REF) | |
795 | { | |
796 | double_int moff = mem_ref_offset (dbase2); | |
797 | moff = double_int_lshift (moff, | |
798 | BITS_PER_UNIT == 8 | |
799 | ? 3 : exact_log2 (BITS_PER_UNIT), | |
800 | HOST_BITS_PER_DOUBLE_INT, true); | |
801 | if (double_int_negative_p (moff)) | |
802 | doffset1 -= double_int_neg (moff).low; | |
803 | else | |
804 | doffset2 -= moff.low; | |
805 | } | |
806 | ||
807 | /* If either reference is view-converted, give up now. */ | |
808 | if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1 | |
809 | || same_type_for_tbaa (TREE_TYPE (dbase2), | |
810 | TREE_TYPE (reference_alias_ptr_type (dbase2))) != 1) | |
811 | return true; | |
812 | ||
813 | /* If both references are through the same type, they do not alias | |
814 | if the accesses do not overlap. This does extra disambiguation | |
815 | for mixed/pointer accesses but requires strict aliasing. | |
816 | For MEM_REFs we require that the component-ref offset we computed | |
817 | is relative to the start of the type which we ensure by | |
818 | comparing rvalue and access type and disregarding the constant | |
819 | pointer offset. */ | |
820 | if ((TREE_CODE (base1) != TARGET_MEM_REF | |
821 | || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1))) | |
822 | && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1) | |
823 | return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2); | |
824 | ||
5006671f RG |
825 | /* Do access-path based disambiguation. */ |
826 | if (ref1 && ref2 | |
29f8b844 | 827 | && (handled_component_p (ref1) || handled_component_p (ref2))) |
dafc9511 | 828 | return aliasing_component_refs_p (ref1, |
630d3fad | 829 | ref1_alias_set, base1_alias_set, |
72580319 | 830 | offset1, max_size1, |
dafc9511 | 831 | ref2, |
630d3fad RG |
832 | ref2_alias_set, base2_alias_set, |
833 | offset2, max_size2, true); | |
5006671f RG |
834 | |
835 | return true; | |
836 | } | |
837 | ||
838 | /* Return true if two indirect references based on *PTR1 | |
dcbd7063 AP |
839 | and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and |
840 | [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have | |
5006671f RG |
841 | the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1 |
842 | in which case they are computed on-demand. REF1 and REF2 | |
843 | if non-NULL are the complete memory reference trees. */ | |
844 | ||
845 | static bool | |
70f34814 | 846 | indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1, |
5006671f | 847 | HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1, |
630d3fad | 848 | alias_set_type ref1_alias_set, |
5006671f | 849 | alias_set_type base1_alias_set, |
70f34814 | 850 | tree ref2 ATTRIBUTE_UNUSED, tree base2, |
5006671f | 851 | HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2, |
630d3fad | 852 | alias_set_type ref2_alias_set, |
70f34814 | 853 | alias_set_type base2_alias_set, bool tbaa_p) |
5006671f | 854 | { |
4b228e61 RG |
855 | tree ptr1; |
856 | tree ptr2; | |
70f34814 RG |
857 | tree ptrtype1, ptrtype2; |
858 | ||
e19f6650 RG |
859 | gcc_checking_assert ((TREE_CODE (base1) == MEM_REF |
860 | || TREE_CODE (base1) == TARGET_MEM_REF) | |
861 | && (TREE_CODE (base2) == MEM_REF | |
862 | || TREE_CODE (base2) == TARGET_MEM_REF)); | |
863 | ||
4d948885 RG |
864 | ptr1 = TREE_OPERAND (base1, 0); |
865 | ptr2 = TREE_OPERAND (base2, 0); | |
4b228e61 | 866 | |
5006671f RG |
867 | /* If both bases are based on pointers they cannot alias if they may not |
868 | point to the same memory object or if they point to the same object | |
869 | and the accesses do not overlap. */ | |
e73cfe5d | 870 | if ((!cfun || gimple_in_ssa_p (cfun)) |
4b228e61 RG |
871 | && operand_equal_p (ptr1, ptr2, 0) |
872 | && (((TREE_CODE (base1) != TARGET_MEM_REF | |
f38fb2c4 | 873 | || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1))) |
4b228e61 | 874 | && (TREE_CODE (base2) != TARGET_MEM_REF |
f38fb2c4 | 875 | || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))) |
4b228e61 RG |
876 | || (TREE_CODE (base1) == TARGET_MEM_REF |
877 | && TREE_CODE (base2) == TARGET_MEM_REF | |
878 | && (TMR_STEP (base1) == TMR_STEP (base2) | |
879 | || (TMR_STEP (base1) && TMR_STEP (base2) | |
880 | && operand_equal_p (TMR_STEP (base1), | |
881 | TMR_STEP (base2), 0))) | |
f38fb2c4 RG |
882 | && (TMR_INDEX (base1) == TMR_INDEX (base2) |
883 | || (TMR_INDEX (base1) && TMR_INDEX (base2) | |
884 | && operand_equal_p (TMR_INDEX (base1), | |
885 | TMR_INDEX (base2), 0))) | |
886 | && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2) | |
887 | || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2) | |
888 | && operand_equal_p (TMR_INDEX2 (base1), | |
889 | TMR_INDEX2 (base2), 0)))))) | |
70f34814 | 890 | { |
e19f6650 | 891 | double_int moff; |
dbfcc059 RG |
892 | /* The offset embedded in MEM_REFs can be negative. Bias them |
893 | so that the resulting offset adjustment is positive. */ | |
e19f6650 RG |
894 | moff = mem_ref_offset (base1); |
895 | moff = double_int_lshift (moff, | |
896 | BITS_PER_UNIT == 8 | |
897 | ? 3 : exact_log2 (BITS_PER_UNIT), | |
898 | HOST_BITS_PER_DOUBLE_INT, true); | |
899 | if (double_int_negative_p (moff)) | |
900 | offset2 += double_int_neg (moff).low; | |
901 | else | |
902 | offset1 += moff.low; | |
903 | moff = mem_ref_offset (base2); | |
904 | moff = double_int_lshift (moff, | |
905 | BITS_PER_UNIT == 8 | |
906 | ? 3 : exact_log2 (BITS_PER_UNIT), | |
907 | HOST_BITS_PER_DOUBLE_INT, true); | |
908 | if (double_int_negative_p (moff)) | |
909 | offset1 += double_int_neg (moff).low; | |
910 | else | |
911 | offset2 += moff.low; | |
70f34814 RG |
912 | return ranges_overlap_p (offset1, max_size1, offset2, max_size2); |
913 | } | |
5006671f RG |
914 | if (!ptr_derefs_may_alias_p (ptr1, ptr2)) |
915 | return false; | |
916 | ||
917 | /* Disambiguations that rely on strict aliasing rules follow. */ | |
70f34814 | 918 | if (!flag_strict_aliasing || !tbaa_p) |
5006671f RG |
919 | return true; |
920 | ||
e19f6650 RG |
921 | ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1)); |
922 | ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1)); | |
70f34814 | 923 | |
5006671f RG |
924 | /* If the alias set for a pointer access is zero all bets are off. */ |
925 | if (base1_alias_set == -1) | |
70f34814 | 926 | base1_alias_set = get_deref_alias_set (ptrtype1); |
5006671f RG |
927 | if (base1_alias_set == 0) |
928 | return true; | |
929 | if (base2_alias_set == -1) | |
70f34814 | 930 | base2_alias_set = get_deref_alias_set (ptrtype2); |
5006671f RG |
931 | if (base2_alias_set == 0) |
932 | return true; | |
933 | ||
934 | /* If both references are through the same type, they do not alias | |
935 | if the accesses do not overlap. This does extra disambiguation | |
936 | for mixed/pointer accesses but requires strict aliasing. */ | |
f63d806d RG |
937 | if ((TREE_CODE (base1) != TARGET_MEM_REF |
938 | || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1))) | |
939 | && (TREE_CODE (base2) != TARGET_MEM_REF | |
940 | || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))) | |
941 | && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1 | |
942 | && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1 | |
70f34814 RG |
943 | && same_type_for_tbaa (TREE_TYPE (ptrtype1), |
944 | TREE_TYPE (ptrtype2)) == 1) | |
5006671f RG |
945 | return ranges_overlap_p (offset1, max_size1, offset2, max_size2); |
946 | ||
947 | /* Do type-based disambiguation. */ | |
948 | if (base1_alias_set != base2_alias_set | |
949 | && !alias_sets_conflict_p (base1_alias_set, base2_alias_set)) | |
950 | return false; | |
951 | ||
952 | /* Do access-path based disambiguation. */ | |
953 | if (ref1 && ref2 | |
f63d806d RG |
954 | && (handled_component_p (ref1) || handled_component_p (ref2)) |
955 | && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1 | |
956 | && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1) | |
dafc9511 | 957 | return aliasing_component_refs_p (ref1, |
630d3fad | 958 | ref1_alias_set, base1_alias_set, |
72580319 | 959 | offset1, max_size1, |
dafc9511 | 960 | ref2, |
630d3fad RG |
961 | ref2_alias_set, base2_alias_set, |
962 | offset2, max_size2, false); | |
5006671f RG |
963 | |
964 | return true; | |
965 | } | |
966 | ||
967 | /* Return true, if the two memory references REF1 and REF2 may alias. */ | |
968 | ||
55b34b5f | 969 | bool |
b45d2719 | 970 | refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p) |
5006671f RG |
971 | { |
972 | tree base1, base2; | |
973 | HOST_WIDE_INT offset1 = 0, offset2 = 0; | |
5006671f RG |
974 | HOST_WIDE_INT max_size1 = -1, max_size2 = -1; |
975 | bool var1_p, var2_p, ind1_p, ind2_p; | |
976 | ||
6bfd4302 | 977 | gcc_checking_assert ((!ref1->ref |
11af16ef | 978 | || TREE_CODE (ref1->ref) == SSA_NAME |
6bfd4302 | 979 | || DECL_P (ref1->ref) |
7d36e538 | 980 | || TREE_CODE (ref1->ref) == STRING_CST |
6bfd4302 | 981 | || handled_component_p (ref1->ref) |
70f34814 | 982 | || TREE_CODE (ref1->ref) == MEM_REF |
6bfd4302 RB |
983 | || TREE_CODE (ref1->ref) == TARGET_MEM_REF) |
984 | && (!ref2->ref | |
11af16ef | 985 | || TREE_CODE (ref2->ref) == SSA_NAME |
6bfd4302 | 986 | || DECL_P (ref2->ref) |
7d36e538 | 987 | || TREE_CODE (ref2->ref) == STRING_CST |
6bfd4302 | 988 | || handled_component_p (ref2->ref) |
70f34814 | 989 | || TREE_CODE (ref2->ref) == MEM_REF |
6bfd4302 | 990 | || TREE_CODE (ref2->ref) == TARGET_MEM_REF)); |
5006671f | 991 | |
5006671f | 992 | /* Decompose the references into their base objects and the access. */ |
b45d2719 RG |
993 | base1 = ao_ref_base (ref1); |
994 | offset1 = ref1->offset; | |
b45d2719 RG |
995 | max_size1 = ref1->max_size; |
996 | base2 = ao_ref_base (ref2); | |
997 | offset2 = ref2->offset; | |
b45d2719 | 998 | max_size2 = ref2->max_size; |
5006671f RG |
999 | |
1000 | /* We can end up with registers or constants as bases for example from | |
1001 | *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59); | |
1002 | which is seen as a struct copy. */ | |
1003 | if (TREE_CODE (base1) == SSA_NAME | |
1307c758 | 1004 | || TREE_CODE (base1) == CONST_DECL |
8ea34dab RG |
1005 | || TREE_CODE (base1) == CONSTRUCTOR |
1006 | || TREE_CODE (base1) == ADDR_EXPR | |
1007 | || CONSTANT_CLASS_P (base1) | |
1008 | || TREE_CODE (base2) == SSA_NAME | |
1307c758 | 1009 | || TREE_CODE (base2) == CONST_DECL |
8ea34dab RG |
1010 | || TREE_CODE (base2) == CONSTRUCTOR |
1011 | || TREE_CODE (base2) == ADDR_EXPR | |
1012 | || CONSTANT_CLASS_P (base2)) | |
5006671f RG |
1013 | return false; |
1014 | ||
6bfd4302 RB |
1015 | /* We can end up refering to code via function and label decls. |
1016 | As we likely do not properly track code aliases conservatively | |
1017 | bail out. */ | |
3c45b96b | 1018 | if (TREE_CODE (base1) == FUNCTION_DECL |
6bfd4302 | 1019 | || TREE_CODE (base1) == LABEL_DECL |
8ea34dab | 1020 | || TREE_CODE (base2) == FUNCTION_DECL |
6bfd4302 | 1021 | || TREE_CODE (base2) == LABEL_DECL) |
3c45b96b RG |
1022 | return true; |
1023 | ||
61e20b90 RG |
1024 | /* Defer to simple offset based disambiguation if we have |
1025 | references based on two decls. Do this before defering to | |
1026 | TBAA to handle must-alias cases in conformance with the | |
1027 | GCC extension of allowing type-punning through unions. */ | |
e19f6650 RG |
1028 | var1_p = DECL_P (base1); |
1029 | var2_p = DECL_P (base2); | |
5006671f RG |
1030 | if (var1_p && var2_p) |
1031 | return decl_refs_may_alias_p (base1, offset1, max_size1, | |
1032 | base2, offset2, max_size2); | |
61e20b90 | 1033 | |
e19f6650 RG |
1034 | ind1_p = (TREE_CODE (base1) == MEM_REF |
1035 | || TREE_CODE (base1) == TARGET_MEM_REF); | |
1036 | ind2_p = (TREE_CODE (base2) == MEM_REF | |
1037 | || TREE_CODE (base2) == TARGET_MEM_REF); | |
70f34814 | 1038 | |
91753e21 RG |
1039 | /* Canonicalize the pointer-vs-decl case. */ |
1040 | if (ind1_p && var2_p) | |
1041 | { | |
1042 | HOST_WIDE_INT tmp1; | |
1043 | tree tmp2; | |
1044 | ao_ref *tmp3; | |
1045 | tmp1 = offset1; offset1 = offset2; offset2 = tmp1; | |
1046 | tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1; | |
1047 | tmp2 = base1; base1 = base2; base2 = tmp2; | |
1048 | tmp3 = ref1; ref1 = ref2; ref2 = tmp3; | |
1049 | var1_p = true; | |
1050 | ind1_p = false; | |
1051 | var2_p = false; | |
1052 | ind2_p = true; | |
1053 | } | |
1054 | ||
61e20b90 | 1055 | /* First defer to TBAA if possible. */ |
4d7a65ea RG |
1056 | if (tbaa_p |
1057 | && flag_strict_aliasing | |
b45d2719 RG |
1058 | && !alias_sets_conflict_p (ao_ref_alias_set (ref1), |
1059 | ao_ref_alias_set (ref2))) | |
61e20b90 RG |
1060 | return false; |
1061 | ||
61e20b90 | 1062 | /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */ |
61e20b90 | 1063 | if (var1_p && ind2_p) |
70f34814 | 1064 | return indirect_ref_may_alias_decl_p (ref2->ref, base2, |
630d3fad | 1065 | offset2, max_size2, |
70f34814 | 1066 | ao_ref_alias_set (ref2), -1, |
b45d2719 | 1067 | ref1->ref, base1, |
630d3fad | 1068 | offset1, max_size1, |
70f34814 RG |
1069 | ao_ref_alias_set (ref1), |
1070 | ao_ref_base_alias_set (ref1), | |
1071 | tbaa_p); | |
5006671f | 1072 | else if (ind1_p && ind2_p) |
70f34814 | 1073 | return indirect_refs_may_alias_p (ref1->ref, base1, |
630d3fad | 1074 | offset1, max_size1, |
70f34814 RG |
1075 | ao_ref_alias_set (ref1), -1, |
1076 | ref2->ref, base2, | |
630d3fad | 1077 | offset2, max_size2, |
70f34814 RG |
1078 | ao_ref_alias_set (ref2), -1, |
1079 | tbaa_p); | |
5006671f | 1080 | |
4b1a5c0d RG |
1081 | /* We really do not want to end up here, but returning true is safe. */ |
1082 | #ifdef ENABLE_CHECKING | |
5006671f | 1083 | gcc_unreachable (); |
4b1a5c0d RG |
1084 | #else |
1085 | return true; | |
1086 | #endif | |
5006671f RG |
1087 | } |
1088 | ||
1089 | bool | |
1090 | refs_may_alias_p (tree ref1, tree ref2) | |
1091 | { | |
b45d2719 RG |
1092 | ao_ref r1, r2; |
1093 | bool res; | |
1094 | ao_ref_init (&r1, ref1); | |
1095 | ao_ref_init (&r2, ref2); | |
1096 | res = refs_may_alias_p_1 (&r1, &r2, true); | |
5006671f RG |
1097 | if (res) |
1098 | ++alias_stats.refs_may_alias_p_may_alias; | |
1099 | else | |
1100 | ++alias_stats.refs_may_alias_p_no_alias; | |
1101 | return res; | |
1102 | } | |
1103 | ||
4d7a65ea RG |
1104 | /* Returns true if there is a anti-dependence for the STORE that |
1105 | executes after the LOAD. */ | |
1106 | ||
1107 | bool | |
1108 | refs_anti_dependent_p (tree load, tree store) | |
1109 | { | |
b45d2719 RG |
1110 | ao_ref r1, r2; |
1111 | ao_ref_init (&r1, load); | |
1112 | ao_ref_init (&r2, store); | |
1113 | return refs_may_alias_p_1 (&r1, &r2, false); | |
4d7a65ea RG |
1114 | } |
1115 | ||
1116 | /* Returns true if there is a output dependence for the stores | |
1117 | STORE1 and STORE2. */ | |
1118 | ||
1119 | bool | |
1120 | refs_output_dependent_p (tree store1, tree store2) | |
1121 | { | |
b45d2719 RG |
1122 | ao_ref r1, r2; |
1123 | ao_ref_init (&r1, store1); | |
1124 | ao_ref_init (&r2, store2); | |
1125 | return refs_may_alias_p_1 (&r1, &r2, false); | |
4d7a65ea | 1126 | } |
5006671f RG |
1127 | |
1128 | /* If the call CALL may use the memory reference REF return true, | |
1129 | otherwise return false. */ | |
1130 | ||
1131 | static bool | |
a778c4e7 | 1132 | ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref) |
5006671f | 1133 | { |
779704e7 | 1134 | tree base, callee; |
5006671f RG |
1135 | unsigned i; |
1136 | int flags = gimple_call_flags (call); | |
1137 | ||
1138 | /* Const functions without a static chain do not implicitly use memory. */ | |
1139 | if (!gimple_call_chain (call) | |
1140 | && (flags & (ECF_CONST|ECF_NOVOPS))) | |
1141 | goto process_args; | |
1142 | ||
a778c4e7 | 1143 | base = ao_ref_base (ref); |
1cb367ae | 1144 | if (!base) |
5006671f RG |
1145 | return true; |
1146 | ||
0609b355 RG |
1147 | /* If the reference is based on a decl that is not aliased the call |
1148 | cannot possibly use it. */ | |
1149 | if (DECL_P (base) | |
1150 | && !may_be_aliased (base) | |
e3ac77ff RG |
1151 | /* But local statics can be used through recursion. */ |
1152 | && !is_global_var (base)) | |
0609b355 RG |
1153 | goto process_args; |
1154 | ||
779704e7 RG |
1155 | callee = gimple_call_fndecl (call); |
1156 | ||
1157 | /* Handle those builtin functions explicitly that do not act as | |
1158 | escape points. See tree-ssa-structalias.c:find_func_aliases | |
1159 | for the list of builtins we might need to handle here. */ | |
1160 | if (callee != NULL_TREE | |
1161 | && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) | |
1162 | switch (DECL_FUNCTION_CODE (callee)) | |
1163 | { | |
915afed6 JJ |
1164 | /* All the following functions read memory pointed to by |
1165 | their second argument. strcat/strncat additionally | |
1166 | reads memory pointed to by the first argument. */ | |
1167 | case BUILT_IN_STRCAT: | |
1168 | case BUILT_IN_STRNCAT: | |
1169 | { | |
1170 | ao_ref dref; | |
1171 | ao_ref_init_from_ptr_and_size (&dref, | |
1172 | gimple_call_arg (call, 0), | |
1173 | NULL_TREE); | |
1174 | if (refs_may_alias_p_1 (&dref, ref, false)) | |
1175 | return true; | |
1176 | } | |
1177 | /* FALLTHRU */ | |
779704e7 RG |
1178 | case BUILT_IN_STRCPY: |
1179 | case BUILT_IN_STRNCPY: | |
779704e7 RG |
1180 | case BUILT_IN_MEMCPY: |
1181 | case BUILT_IN_MEMMOVE: | |
1182 | case BUILT_IN_MEMPCPY: | |
1183 | case BUILT_IN_STPCPY: | |
1184 | case BUILT_IN_STPNCPY: | |
0a35513e AH |
1185 | case BUILT_IN_TM_MEMCPY: |
1186 | case BUILT_IN_TM_MEMMOVE: | |
779704e7 | 1187 | { |
a778c4e7 RG |
1188 | ao_ref dref; |
1189 | tree size = NULL_TREE; | |
1190 | if (gimple_call_num_args (call) == 3) | |
1191 | size = gimple_call_arg (call, 2); | |
1192 | ao_ref_init_from_ptr_and_size (&dref, | |
1193 | gimple_call_arg (call, 1), | |
1194 | size); | |
1195 | return refs_may_alias_p_1 (&dref, ref, false); | |
779704e7 | 1196 | } |
915afed6 JJ |
1197 | case BUILT_IN_STRCAT_CHK: |
1198 | case BUILT_IN_STRNCAT_CHK: | |
1199 | { | |
1200 | ao_ref dref; | |
1201 | ao_ref_init_from_ptr_and_size (&dref, | |
1202 | gimple_call_arg (call, 0), | |
1203 | NULL_TREE); | |
1204 | if (refs_may_alias_p_1 (&dref, ref, false)) | |
1205 | return true; | |
1206 | } | |
1207 | /* FALLTHRU */ | |
36dc1a88 JJ |
1208 | case BUILT_IN_STRCPY_CHK: |
1209 | case BUILT_IN_STRNCPY_CHK: | |
1210 | case BUILT_IN_MEMCPY_CHK: | |
1211 | case BUILT_IN_MEMMOVE_CHK: | |
1212 | case BUILT_IN_MEMPCPY_CHK: | |
1213 | case BUILT_IN_STPCPY_CHK: | |
36dc1a88 JJ |
1214 | { |
1215 | ao_ref dref; | |
1216 | tree size = NULL_TREE; | |
1217 | if (gimple_call_num_args (call) == 4) | |
1218 | size = gimple_call_arg (call, 2); | |
1219 | ao_ref_init_from_ptr_and_size (&dref, | |
1220 | gimple_call_arg (call, 1), | |
1221 | size); | |
1222 | return refs_may_alias_p_1 (&dref, ref, false); | |
1223 | } | |
1307c758 RG |
1224 | case BUILT_IN_BCOPY: |
1225 | { | |
1226 | ao_ref dref; | |
1227 | tree size = gimple_call_arg (call, 2); | |
1228 | ao_ref_init_from_ptr_and_size (&dref, | |
1229 | gimple_call_arg (call, 0), | |
1230 | size); | |
1231 | return refs_may_alias_p_1 (&dref, ref, false); | |
1232 | } | |
0a35513e AH |
1233 | |
1234 | /* The following functions read memory pointed to by their | |
1235 | first argument. */ | |
1236 | CASE_BUILT_IN_TM_LOAD (1): | |
1237 | CASE_BUILT_IN_TM_LOAD (2): | |
1238 | CASE_BUILT_IN_TM_LOAD (4): | |
1239 | CASE_BUILT_IN_TM_LOAD (8): | |
1240 | CASE_BUILT_IN_TM_LOAD (FLOAT): | |
1241 | CASE_BUILT_IN_TM_LOAD (DOUBLE): | |
1242 | CASE_BUILT_IN_TM_LOAD (LDOUBLE): | |
1243 | CASE_BUILT_IN_TM_LOAD (M64): | |
1244 | CASE_BUILT_IN_TM_LOAD (M128): | |
1245 | CASE_BUILT_IN_TM_LOAD (M256): | |
1246 | case BUILT_IN_TM_LOG: | |
1247 | case BUILT_IN_TM_LOG_1: | |
1248 | case BUILT_IN_TM_LOG_2: | |
1249 | case BUILT_IN_TM_LOG_4: | |
1250 | case BUILT_IN_TM_LOG_8: | |
1251 | case BUILT_IN_TM_LOG_FLOAT: | |
1252 | case BUILT_IN_TM_LOG_DOUBLE: | |
1253 | case BUILT_IN_TM_LOG_LDOUBLE: | |
1254 | case BUILT_IN_TM_LOG_M64: | |
1255 | case BUILT_IN_TM_LOG_M128: | |
1256 | case BUILT_IN_TM_LOG_M256: | |
1257 | return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref); | |
1258 | ||
915afed6 JJ |
1259 | /* These read memory pointed to by the first argument. */ |
1260 | case BUILT_IN_STRDUP: | |
1261 | case BUILT_IN_STRNDUP: | |
1262 | { | |
1263 | ao_ref dref; | |
1264 | tree size = NULL_TREE; | |
1265 | if (gimple_call_num_args (call) == 2) | |
1266 | size = gimple_call_arg (call, 1); | |
1267 | ao_ref_init_from_ptr_and_size (&dref, | |
1268 | gimple_call_arg (call, 0), | |
1269 | size); | |
1270 | return refs_may_alias_p_1 (&dref, ref, false); | |
1271 | } | |
825be69e RG |
1272 | /* The following builtins do not read from memory. */ |
1273 | case BUILT_IN_FREE: | |
c8b3e92f | 1274 | case BUILT_IN_MALLOC: |
e3c70387 | 1275 | case BUILT_IN_CALLOC: |
ab4472fa | 1276 | case BUILT_IN_ALLOCA: |
13e49da9 | 1277 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
ab4472fa RG |
1278 | case BUILT_IN_STACK_SAVE: |
1279 | case BUILT_IN_STACK_RESTORE: | |
825be69e | 1280 | case BUILT_IN_MEMSET: |
0a35513e | 1281 | case BUILT_IN_TM_MEMSET: |
36dc1a88 | 1282 | case BUILT_IN_MEMSET_CHK: |
825be69e RG |
1283 | case BUILT_IN_FREXP: |
1284 | case BUILT_IN_FREXPF: | |
1285 | case BUILT_IN_FREXPL: | |
1286 | case BUILT_IN_GAMMA_R: | |
1287 | case BUILT_IN_GAMMAF_R: | |
1288 | case BUILT_IN_GAMMAL_R: | |
1289 | case BUILT_IN_LGAMMA_R: | |
1290 | case BUILT_IN_LGAMMAF_R: | |
1291 | case BUILT_IN_LGAMMAL_R: | |
1292 | case BUILT_IN_MODF: | |
1293 | case BUILT_IN_MODFF: | |
1294 | case BUILT_IN_MODFL: | |
1295 | case BUILT_IN_REMQUO: | |
1296 | case BUILT_IN_REMQUOF: | |
1297 | case BUILT_IN_REMQUOL: | |
1298 | case BUILT_IN_SINCOS: | |
1299 | case BUILT_IN_SINCOSF: | |
1300 | case BUILT_IN_SINCOSL: | |
45d439ac | 1301 | case BUILT_IN_ASSUME_ALIGNED: |
df2f6100 | 1302 | case BUILT_IN_VA_END: |
825be69e | 1303 | return false; |
d0f305b1 JJ |
1304 | /* __sync_* builtins and some OpenMP builtins act as threading |
1305 | barriers. */ | |
1306 | #undef DEF_SYNC_BUILTIN | |
1307 | #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM: | |
1308 | #include "sync-builtins.def" | |
1309 | #undef DEF_SYNC_BUILTIN | |
1310 | case BUILT_IN_GOMP_ATOMIC_START: | |
1311 | case BUILT_IN_GOMP_ATOMIC_END: | |
1312 | case BUILT_IN_GOMP_BARRIER: | |
1313 | case BUILT_IN_GOMP_TASKWAIT: | |
1314 | case BUILT_IN_GOMP_CRITICAL_START: | |
1315 | case BUILT_IN_GOMP_CRITICAL_END: | |
1316 | case BUILT_IN_GOMP_CRITICAL_NAME_START: | |
1317 | case BUILT_IN_GOMP_CRITICAL_NAME_END: | |
1318 | case BUILT_IN_GOMP_LOOP_END: | |
1319 | case BUILT_IN_GOMP_ORDERED_START: | |
1320 | case BUILT_IN_GOMP_ORDERED_END: | |
1321 | case BUILT_IN_GOMP_PARALLEL_END: | |
1322 | case BUILT_IN_GOMP_SECTIONS_END: | |
1323 | case BUILT_IN_GOMP_SINGLE_COPY_START: | |
1324 | case BUILT_IN_GOMP_SINGLE_COPY_END: | |
1325 | return true; | |
825be69e | 1326 | |
779704e7 RG |
1327 | default: |
1328 | /* Fallthru to general call handling. */; | |
1329 | } | |
1330 | ||
5006671f RG |
1331 | /* Check if base is a global static variable that is not read |
1332 | by the function. */ | |
9f9ebcdf MJ |
1333 | if (callee != NULL_TREE |
1334 | && TREE_CODE (base) == VAR_DECL | |
f3380641 | 1335 | && TREE_STATIC (base)) |
6de9cd9a | 1336 | { |
9f9ebcdf | 1337 | struct cgraph_node *node = cgraph_get_node (callee); |
5006671f RG |
1338 | bitmap not_read; |
1339 | ||
9f9ebcdf MJ |
1340 | /* FIXME: Callee can be an OMP builtin that does not have a call graph |
1341 | node yet. We should enforce that there are nodes for all decls in the | |
1342 | IL and remove this check instead. */ | |
1343 | if (node | |
1344 | && (not_read = ipa_reference_get_not_read_global (node)) | |
5006671f RG |
1345 | && bitmap_bit_p (not_read, DECL_UID (base))) |
1346 | goto process_args; | |
6de9cd9a DN |
1347 | } |
1348 | ||
d086d311 RG |
1349 | /* Check if the base variable is call-used. */ |
1350 | if (DECL_P (base)) | |
6de9cd9a | 1351 | { |
d086d311 | 1352 | if (pt_solution_includes (gimple_call_use_set (call), base)) |
5006671f RG |
1353 | return true; |
1354 | } | |
e19f6650 RG |
1355 | else if ((TREE_CODE (base) == MEM_REF |
1356 | || TREE_CODE (base) == TARGET_MEM_REF) | |
d086d311 | 1357 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) |
5006671f | 1358 | { |
d086d311 RG |
1359 | struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0)); |
1360 | if (!pi) | |
1361 | return true; | |
1cb367ae | 1362 | |
d086d311 | 1363 | if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt)) |
5006671f RG |
1364 | return true; |
1365 | } | |
d086d311 RG |
1366 | else |
1367 | return true; | |
5006671f RG |
1368 | |
1369 | /* Inspect call arguments for passed-by-value aliases. */ | |
1370 | process_args: | |
1371 | for (i = 0; i < gimple_call_num_args (call); ++i) | |
1372 | { | |
1373 | tree op = gimple_call_arg (call, i); | |
0b7b376d RG |
1374 | int flags = gimple_call_arg_flags (call, i); |
1375 | ||
1376 | if (flags & EAF_UNUSED) | |
1377 | continue; | |
5006671f | 1378 | |
5006671f RG |
1379 | if (TREE_CODE (op) == WITH_SIZE_EXPR) |
1380 | op = TREE_OPERAND (op, 0); | |
6de9cd9a | 1381 | |
5006671f | 1382 | if (TREE_CODE (op) != SSA_NAME |
a778c4e7 RG |
1383 | && !is_gimple_min_invariant (op)) |
1384 | { | |
1385 | ao_ref r; | |
1386 | ao_ref_init (&r, op); | |
1387 | if (refs_may_alias_p_1 (&r, ref, true)) | |
1388 | return true; | |
1389 | } | |
6de9cd9a DN |
1390 | } |
1391 | ||
5006671f RG |
1392 | return false; |
1393 | } | |
1394 | ||
1395 | static bool | |
1396 | ref_maybe_used_by_call_p (gimple call, tree ref) | |
1397 | { | |
a778c4e7 RG |
1398 | ao_ref r; |
1399 | bool res; | |
1400 | ao_ref_init (&r, ref); | |
1401 | res = ref_maybe_used_by_call_p_1 (call, &r); | |
5006671f RG |
1402 | if (res) |
1403 | ++alias_stats.ref_maybe_used_by_call_p_may_alias; | |
1404 | else | |
1405 | ++alias_stats.ref_maybe_used_by_call_p_no_alias; | |
1406 | return res; | |
6de9cd9a DN |
1407 | } |
1408 | ||
1409 | ||
5006671f RG |
1410 | /* If the statement STMT may use the memory reference REF return |
1411 | true, otherwise return false. */ | |
6de9cd9a | 1412 | |
5006671f RG |
1413 | bool |
1414 | ref_maybe_used_by_stmt_p (gimple stmt, tree ref) | |
6de9cd9a | 1415 | { |
5006671f RG |
1416 | if (is_gimple_assign (stmt)) |
1417 | { | |
1418 | tree rhs; | |
1419 | ||
1420 | /* All memory assign statements are single. */ | |
1421 | if (!gimple_assign_single_p (stmt)) | |
1422 | return false; | |
1423 | ||
1424 | rhs = gimple_assign_rhs1 (stmt); | |
1425 | if (is_gimple_reg (rhs) | |
1426 | || is_gimple_min_invariant (rhs) | |
1427 | || gimple_assign_rhs_code (stmt) == CONSTRUCTOR) | |
1428 | return false; | |
1429 | ||
1430 | return refs_may_alias_p (rhs, ref); | |
1431 | } | |
1432 | else if (is_gimple_call (stmt)) | |
1433 | return ref_maybe_used_by_call_p (stmt, ref); | |
53f94a5c RG |
1434 | else if (gimple_code (stmt) == GIMPLE_RETURN) |
1435 | { | |
1436 | tree retval = gimple_return_retval (stmt); | |
1437 | tree base; | |
1438 | if (retval | |
1439 | && TREE_CODE (retval) != SSA_NAME | |
1440 | && !is_gimple_min_invariant (retval) | |
1441 | && refs_may_alias_p (retval, ref)) | |
1442 | return true; | |
1443 | /* If ref escapes the function then the return acts as a use. */ | |
1444 | base = get_base_address (ref); | |
1445 | if (!base) | |
1446 | ; | |
1447 | else if (DECL_P (base)) | |
1448 | return is_global_var (base); | |
1449 | else if (TREE_CODE (base) == MEM_REF | |
1450 | || TREE_CODE (base) == TARGET_MEM_REF) | |
1451 | return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0)); | |
1452 | return false; | |
1453 | } | |
5006671f RG |
1454 | |
1455 | return true; | |
6de9cd9a DN |
1456 | } |
1457 | ||
5006671f RG |
1458 | /* If the call in statement CALL may clobber the memory reference REF |
1459 | return true, otherwise return false. */ | |
6de9cd9a | 1460 | |
5006671f | 1461 | static bool |
b45d2719 | 1462 | call_may_clobber_ref_p_1 (gimple call, ao_ref *ref) |
6de9cd9a | 1463 | { |
e3ac77ff | 1464 | tree base; |
779704e7 | 1465 | tree callee; |
5006671f RG |
1466 | |
1467 | /* If the call is pure or const it cannot clobber anything. */ | |
1468 | if (gimple_call_flags (call) | |
1469 | & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS)) | |
1470 | return false; | |
1471 | ||
b45d2719 | 1472 | base = ao_ref_base (ref); |
5006671f RG |
1473 | if (!base) |
1474 | return true; | |
1475 | ||
1476 | if (TREE_CODE (base) == SSA_NAME | |
1477 | || CONSTANT_CLASS_P (base)) | |
1478 | return false; | |
1479 | ||
0609b355 RG |
1480 | /* If the reference is based on a decl that is not aliased the call |
1481 | cannot possibly clobber it. */ | |
1482 | if (DECL_P (base) | |
1483 | && !may_be_aliased (base) | |
e3ac77ff RG |
1484 | /* But local non-readonly statics can be modified through recursion |
1485 | or the call may implement a threading barrier which we must | |
1486 | treat as may-def. */ | |
0609b355 | 1487 | && (TREE_READONLY (base) |
e3ac77ff | 1488 | || !is_global_var (base))) |
0609b355 RG |
1489 | return false; |
1490 | ||
779704e7 RG |
1491 | callee = gimple_call_fndecl (call); |
1492 | ||
1493 | /* Handle those builtin functions explicitly that do not act as | |
1494 | escape points. See tree-ssa-structalias.c:find_func_aliases | |
1495 | for the list of builtins we might need to handle here. */ | |
1496 | if (callee != NULL_TREE | |
1497 | && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) | |
1498 | switch (DECL_FUNCTION_CODE (callee)) | |
1499 | { | |
1500 | /* All the following functions clobber memory pointed to by | |
1501 | their first argument. */ | |
1502 | case BUILT_IN_STRCPY: | |
1503 | case BUILT_IN_STRNCPY: | |
779704e7 RG |
1504 | case BUILT_IN_MEMCPY: |
1505 | case BUILT_IN_MEMMOVE: | |
1506 | case BUILT_IN_MEMPCPY: | |
1507 | case BUILT_IN_STPCPY: | |
1508 | case BUILT_IN_STPNCPY: | |
1509 | case BUILT_IN_STRCAT: | |
1510 | case BUILT_IN_STRNCAT: | |
a778c4e7 | 1511 | case BUILT_IN_MEMSET: |
0a35513e AH |
1512 | case BUILT_IN_TM_MEMSET: |
1513 | CASE_BUILT_IN_TM_STORE (1): | |
1514 | CASE_BUILT_IN_TM_STORE (2): | |
1515 | CASE_BUILT_IN_TM_STORE (4): | |
1516 | CASE_BUILT_IN_TM_STORE (8): | |
1517 | CASE_BUILT_IN_TM_STORE (FLOAT): | |
1518 | CASE_BUILT_IN_TM_STORE (DOUBLE): | |
1519 | CASE_BUILT_IN_TM_STORE (LDOUBLE): | |
1520 | CASE_BUILT_IN_TM_STORE (M64): | |
1521 | CASE_BUILT_IN_TM_STORE (M128): | |
1522 | CASE_BUILT_IN_TM_STORE (M256): | |
1523 | case BUILT_IN_TM_MEMCPY: | |
1524 | case BUILT_IN_TM_MEMMOVE: | |
779704e7 | 1525 | { |
a778c4e7 RG |
1526 | ao_ref dref; |
1527 | tree size = NULL_TREE; | |
915afed6 JJ |
1528 | /* Don't pass in size for strncat, as the maximum size |
1529 | is strlen (dest) + n + 1 instead of n, resp. | |
1530 | n + 1 at dest + strlen (dest), but strlen (dest) isn't | |
1531 | known. */ | |
1532 | if (gimple_call_num_args (call) == 3 | |
1533 | && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT) | |
a778c4e7 RG |
1534 | size = gimple_call_arg (call, 2); |
1535 | ao_ref_init_from_ptr_and_size (&dref, | |
1536 | gimple_call_arg (call, 0), | |
1537 | size); | |
1538 | return refs_may_alias_p_1 (&dref, ref, false); | |
779704e7 | 1539 | } |
36dc1a88 JJ |
1540 | case BUILT_IN_STRCPY_CHK: |
1541 | case BUILT_IN_STRNCPY_CHK: | |
1542 | case BUILT_IN_MEMCPY_CHK: | |
1543 | case BUILT_IN_MEMMOVE_CHK: | |
1544 | case BUILT_IN_MEMPCPY_CHK: | |
1545 | case BUILT_IN_STPCPY_CHK: | |
1546 | case BUILT_IN_STRCAT_CHK: | |
1547 | case BUILT_IN_STRNCAT_CHK: | |
1548 | case BUILT_IN_MEMSET_CHK: | |
1549 | { | |
1550 | ao_ref dref; | |
1551 | tree size = NULL_TREE; | |
915afed6 JJ |
1552 | /* Don't pass in size for __strncat_chk, as the maximum size |
1553 | is strlen (dest) + n + 1 instead of n, resp. | |
1554 | n + 1 at dest + strlen (dest), but strlen (dest) isn't | |
1555 | known. */ | |
1556 | if (gimple_call_num_args (call) == 4 | |
1557 | && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK) | |
36dc1a88 JJ |
1558 | size = gimple_call_arg (call, 2); |
1559 | ao_ref_init_from_ptr_and_size (&dref, | |
1560 | gimple_call_arg (call, 0), | |
1561 | size); | |
1562 | return refs_may_alias_p_1 (&dref, ref, false); | |
1563 | } | |
1307c758 RG |
1564 | case BUILT_IN_BCOPY: |
1565 | { | |
1566 | ao_ref dref; | |
1567 | tree size = gimple_call_arg (call, 2); | |
1568 | ao_ref_init_from_ptr_and_size (&dref, | |
1569 | gimple_call_arg (call, 1), | |
1570 | size); | |
1571 | return refs_may_alias_p_1 (&dref, ref, false); | |
1572 | } | |
c8b3e92f RG |
1573 | /* Allocating memory does not have any side-effects apart from |
1574 | being the definition point for the pointer. */ | |
1575 | case BUILT_IN_MALLOC: | |
e3c70387 | 1576 | case BUILT_IN_CALLOC: |
915afed6 JJ |
1577 | case BUILT_IN_STRDUP: |
1578 | case BUILT_IN_STRNDUP: | |
7352c013 | 1579 | /* Unix98 specifies that errno is set on allocation failure. */ |
604d0dbc | 1580 | if (flag_errno_math |
7352c013 RG |
1581 | && targetm.ref_may_alias_errno (ref)) |
1582 | return true; | |
c8b3e92f | 1583 | return false; |
ab4472fa RG |
1584 | case BUILT_IN_STACK_SAVE: |
1585 | case BUILT_IN_ALLOCA: | |
13e49da9 | 1586 | case BUILT_IN_ALLOCA_WITH_ALIGN: |
45d439ac | 1587 | case BUILT_IN_ASSUME_ALIGNED: |
ab4472fa | 1588 | return false; |
779704e7 RG |
1589 | /* Freeing memory kills the pointed-to memory. More importantly |
1590 | the call has to serve as a barrier for moving loads and stores | |
a778c4e7 | 1591 | across it. */ |
779704e7 | 1592 | case BUILT_IN_FREE: |
df2f6100 | 1593 | case BUILT_IN_VA_END: |
779704e7 RG |
1594 | { |
1595 | tree ptr = gimple_call_arg (call, 0); | |
1596 | return ptr_deref_may_alias_ref_p_1 (ptr, ref); | |
1597 | } | |
779704e7 RG |
1598 | case BUILT_IN_GAMMA_R: |
1599 | case BUILT_IN_GAMMAF_R: | |
1600 | case BUILT_IN_GAMMAL_R: | |
1601 | case BUILT_IN_LGAMMA_R: | |
1602 | case BUILT_IN_LGAMMAF_R: | |
1603 | case BUILT_IN_LGAMMAL_R: | |
825be69e RG |
1604 | { |
1605 | tree out = gimple_call_arg (call, 1); | |
1606 | if (ptr_deref_may_alias_ref_p_1 (out, ref)) | |
1607 | return true; | |
1608 | if (flag_errno_math) | |
1609 | break; | |
1610 | return false; | |
1611 | } | |
1612 | case BUILT_IN_FREXP: | |
1613 | case BUILT_IN_FREXPF: | |
1614 | case BUILT_IN_FREXPL: | |
779704e7 RG |
1615 | case BUILT_IN_MODF: |
1616 | case BUILT_IN_MODFF: | |
1617 | case BUILT_IN_MODFL: | |
1618 | { | |
1619 | tree out = gimple_call_arg (call, 1); | |
1620 | return ptr_deref_may_alias_ref_p_1 (out, ref); | |
1621 | } | |
1622 | case BUILT_IN_REMQUO: | |
1623 | case BUILT_IN_REMQUOF: | |
1624 | case BUILT_IN_REMQUOL: | |
1625 | { | |
1626 | tree out = gimple_call_arg (call, 2); | |
825be69e RG |
1627 | if (ptr_deref_may_alias_ref_p_1 (out, ref)) |
1628 | return true; | |
1629 | if (flag_errno_math) | |
1630 | break; | |
1631 | return false; | |
779704e7 RG |
1632 | } |
1633 | case BUILT_IN_SINCOS: | |
1634 | case BUILT_IN_SINCOSF: | |
1635 | case BUILT_IN_SINCOSL: | |
1636 | { | |
1637 | tree sin = gimple_call_arg (call, 1); | |
1638 | tree cos = gimple_call_arg (call, 2); | |
1639 | return (ptr_deref_may_alias_ref_p_1 (sin, ref) | |
1640 | || ptr_deref_may_alias_ref_p_1 (cos, ref)); | |
1641 | } | |
d0f305b1 JJ |
1642 | /* __sync_* builtins and some OpenMP builtins act as threading |
1643 | barriers. */ | |
1644 | #undef DEF_SYNC_BUILTIN | |
1645 | #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM: | |
1646 | #include "sync-builtins.def" | |
1647 | #undef DEF_SYNC_BUILTIN | |
1648 | case BUILT_IN_GOMP_ATOMIC_START: | |
1649 | case BUILT_IN_GOMP_ATOMIC_END: | |
1650 | case BUILT_IN_GOMP_BARRIER: | |
1651 | case BUILT_IN_GOMP_TASKWAIT: | |
1652 | case BUILT_IN_GOMP_CRITICAL_START: | |
1653 | case BUILT_IN_GOMP_CRITICAL_END: | |
1654 | case BUILT_IN_GOMP_CRITICAL_NAME_START: | |
1655 | case BUILT_IN_GOMP_CRITICAL_NAME_END: | |
1656 | case BUILT_IN_GOMP_LOOP_END: | |
1657 | case BUILT_IN_GOMP_ORDERED_START: | |
1658 | case BUILT_IN_GOMP_ORDERED_END: | |
1659 | case BUILT_IN_GOMP_PARALLEL_END: | |
1660 | case BUILT_IN_GOMP_SECTIONS_END: | |
1661 | case BUILT_IN_GOMP_SINGLE_COPY_START: | |
1662 | case BUILT_IN_GOMP_SINGLE_COPY_END: | |
1663 | return true; | |
779704e7 RG |
1664 | default: |
1665 | /* Fallthru to general call handling. */; | |
1666 | } | |
1667 | ||
5006671f RG |
1668 | /* Check if base is a global static variable that is not written |
1669 | by the function. */ | |
779704e7 RG |
1670 | if (callee != NULL_TREE |
1671 | && TREE_CODE (base) == VAR_DECL | |
f3380641 | 1672 | && TREE_STATIC (base)) |
6de9cd9a | 1673 | { |
9f9ebcdf | 1674 | struct cgraph_node *node = cgraph_get_node (callee); |
5006671f | 1675 | bitmap not_written; |
306219a2 | 1676 | |
9f9ebcdf MJ |
1677 | if (node |
1678 | && (not_written = ipa_reference_get_not_written_global (node)) | |
5006671f RG |
1679 | && bitmap_bit_p (not_written, DECL_UID (base))) |
1680 | return false; | |
6de9cd9a | 1681 | } |
6de9cd9a | 1682 | |
d086d311 | 1683 | /* Check if the base variable is call-clobbered. */ |
5006671f | 1684 | if (DECL_P (base)) |
d086d311 | 1685 | return pt_solution_includes (gimple_call_clobber_set (call), base); |
e19f6650 RG |
1686 | else if ((TREE_CODE (base) == MEM_REF |
1687 | || TREE_CODE (base) == TARGET_MEM_REF) | |
1cb367ae RG |
1688 | && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME) |
1689 | { | |
1690 | struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0)); | |
1691 | if (!pi) | |
1692 | return true; | |
1693 | ||
d086d311 | 1694 | return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt); |
1cb367ae | 1695 | } |
6de9cd9a | 1696 | |
5006671f RG |
1697 | return true; |
1698 | } | |
6de9cd9a | 1699 | |
12de6355 RG |
1700 | /* If the call in statement CALL may clobber the memory reference REF |
1701 | return true, otherwise return false. */ | |
1702 | ||
1703 | bool | |
5006671f | 1704 | call_may_clobber_ref_p (gimple call, tree ref) |
6de9cd9a | 1705 | { |
b45d2719 RG |
1706 | bool res; |
1707 | ao_ref r; | |
1708 | ao_ref_init (&r, ref); | |
1709 | res = call_may_clobber_ref_p_1 (call, &r); | |
5006671f RG |
1710 | if (res) |
1711 | ++alias_stats.call_may_clobber_ref_p_may_alias; | |
1712 | else | |
1713 | ++alias_stats.call_may_clobber_ref_p_no_alias; | |
1714 | return res; | |
6de9cd9a | 1715 | } |
ab8907ef | 1716 | |
5006671f RG |
1717 | |
1718 | /* If the statement STMT may clobber the memory reference REF return true, | |
1719 | otherwise return false. */ | |
ab8907ef RH |
1720 | |
1721 | bool | |
b45d2719 | 1722 | stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref) |
ab8907ef | 1723 | { |
5006671f RG |
1724 | if (is_gimple_call (stmt)) |
1725 | { | |
1726 | tree lhs = gimple_call_lhs (stmt); | |
1727 | if (lhs | |
8ea34dab | 1728 | && TREE_CODE (lhs) != SSA_NAME) |
b45d2719 RG |
1729 | { |
1730 | ao_ref r; | |
1731 | ao_ref_init (&r, lhs); | |
1732 | if (refs_may_alias_p_1 (ref, &r, true)) | |
1733 | return true; | |
1734 | } | |
ab8907ef | 1735 | |
b45d2719 | 1736 | return call_may_clobber_ref_p_1 (stmt, ref); |
5006671f | 1737 | } |
11af16ef | 1738 | else if (gimple_assign_single_p (stmt)) |
b45d2719 | 1739 | { |
11af16ef | 1740 | tree lhs = gimple_assign_lhs (stmt); |
8ea34dab | 1741 | if (TREE_CODE (lhs) != SSA_NAME) |
11af16ef RG |
1742 | { |
1743 | ao_ref r; | |
8ea34dab | 1744 | ao_ref_init (&r, lhs); |
11af16ef RG |
1745 | return refs_may_alias_p_1 (ref, &r, true); |
1746 | } | |
b45d2719 | 1747 | } |
5006671f | 1748 | else if (gimple_code (stmt) == GIMPLE_ASM) |
ab8907ef RH |
1749 | return true; |
1750 | ||
7e8b322a | 1751 | return false; |
ab8907ef | 1752 | } |
c75ab022 | 1753 | |
b45d2719 RG |
1754 | bool |
1755 | stmt_may_clobber_ref_p (gimple stmt, tree ref) | |
1756 | { | |
1757 | ao_ref r; | |
1758 | ao_ref_init (&r, ref); | |
1759 | return stmt_may_clobber_ref_p_1 (stmt, &r); | |
1760 | } | |
1761 | ||
71d61348 RG |
1762 | /* If STMT kills the memory reference REF return true, otherwise |
1763 | return false. */ | |
1764 | ||
1765 | static bool | |
1766 | stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref) | |
1767 | { | |
4a5ba3ed RG |
1768 | /* For a must-alias check we need to be able to constrain |
1769 | the access properly. */ | |
1770 | ao_ref_base (ref); | |
1771 | if (ref->max_size == -1) | |
1772 | return false; | |
1773 | ||
71d61348 | 1774 | if (gimple_has_lhs (stmt) |
094f6ab3 RG |
1775 | && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME |
1776 | /* The assignment is not necessarily carried out if it can throw | |
1777 | and we can catch it in the current function where we could inspect | |
1778 | the previous value. | |
1779 | ??? We only need to care about the RHS throwing. For aggregate | |
1780 | assignments or similar calls and non-call exceptions the LHS | |
1781 | might throw as well. */ | |
1782 | && !stmt_can_throw_internal (stmt)) | |
71d61348 RG |
1783 | { |
1784 | tree base, lhs = gimple_get_lhs (stmt); | |
1785 | HOST_WIDE_INT size, offset, max_size; | |
71d61348 RG |
1786 | base = get_ref_base_and_extent (lhs, &offset, &size, &max_size); |
1787 | /* We can get MEM[symbol: sZ, index: D.8862_1] here, | |
1788 | so base == ref->base does not always hold. */ | |
1789 | if (base == ref->base) | |
1790 | { | |
1791 | /* For a must-alias check we need to be able to constrain | |
4a5ba3ed RG |
1792 | the access properly. */ |
1793 | if (size != -1 && size == max_size) | |
71d61348 RG |
1794 | { |
1795 | if (offset <= ref->offset | |
1796 | && offset + size >= ref->offset + ref->max_size) | |
1797 | return true; | |
1798 | } | |
1799 | } | |
1800 | } | |
4a5ba3ed RG |
1801 | |
1802 | if (is_gimple_call (stmt)) | |
1803 | { | |
1804 | tree callee = gimple_call_fndecl (stmt); | |
1805 | if (callee != NULL_TREE | |
1806 | && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL) | |
1807 | switch (DECL_FUNCTION_CODE (callee)) | |
1808 | { | |
1809 | case BUILT_IN_MEMCPY: | |
1810 | case BUILT_IN_MEMPCPY: | |
1811 | case BUILT_IN_MEMMOVE: | |
1812 | case BUILT_IN_MEMSET: | |
36dc1a88 JJ |
1813 | case BUILT_IN_MEMCPY_CHK: |
1814 | case BUILT_IN_MEMPCPY_CHK: | |
1815 | case BUILT_IN_MEMMOVE_CHK: | |
1816 | case BUILT_IN_MEMSET_CHK: | |
4a5ba3ed RG |
1817 | { |
1818 | tree dest = gimple_call_arg (stmt, 0); | |
1819 | tree len = gimple_call_arg (stmt, 2); | |
1820 | tree base = NULL_TREE; | |
1821 | HOST_WIDE_INT offset = 0; | |
1822 | if (!host_integerp (len, 0)) | |
1823 | return false; | |
1824 | if (TREE_CODE (dest) == ADDR_EXPR) | |
1825 | base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0), | |
1826 | &offset); | |
1827 | else if (TREE_CODE (dest) == SSA_NAME) | |
1828 | base = dest; | |
1829 | if (base | |
1830 | && base == ao_ref_base (ref)) | |
1831 | { | |
1832 | HOST_WIDE_INT size = TREE_INT_CST_LOW (len); | |
1833 | if (offset <= ref->offset / BITS_PER_UNIT | |
1834 | && (offset + size | |
1835 | >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1) | |
1836 | / BITS_PER_UNIT))) | |
1837 | return true; | |
1838 | } | |
df2f6100 RG |
1839 | break; |
1840 | } | |
1841 | ||
1842 | case BUILT_IN_VA_END: | |
1843 | { | |
1844 | tree ptr = gimple_call_arg (stmt, 0); | |
1845 | if (TREE_CODE (ptr) == ADDR_EXPR) | |
1846 | { | |
1847 | tree base = ao_ref_base (ref); | |
1848 | if (TREE_OPERAND (ptr, 0) == base) | |
1849 | return true; | |
1850 | } | |
1851 | break; | |
4a5ba3ed | 1852 | } |
df2f6100 | 1853 | |
4a5ba3ed RG |
1854 | default:; |
1855 | } | |
4a5ba3ed | 1856 | } |
71d61348 RG |
1857 | return false; |
1858 | } | |
1859 | ||
1860 | bool | |
1861 | stmt_kills_ref_p (gimple stmt, tree ref) | |
1862 | { | |
1863 | ao_ref r; | |
1864 | ao_ref_init (&r, ref); | |
1865 | return stmt_kills_ref_p_1 (stmt, &r); | |
1866 | } | |
1867 | ||
b45d2719 | 1868 | |
5006671f RG |
1869 | /* Walk the virtual use-def chain of VUSE until hitting the virtual operand |
1870 | TARGET or a statement clobbering the memory reference REF in which | |
1871 | case false is returned. The walk starts with VUSE, one argument of PHI. */ | |
1872 | ||
1873 | static bool | |
b45d2719 RG |
1874 | maybe_skip_until (gimple phi, tree target, ao_ref *ref, |
1875 | tree vuse, bitmap *visited) | |
cc0968b0 | 1876 | { |
d660a3db RG |
1877 | basic_block bb = gimple_bb (phi); |
1878 | ||
5006671f RG |
1879 | if (!*visited) |
1880 | *visited = BITMAP_ALLOC (NULL); | |
cc0968b0 | 1881 | |
5006671f RG |
1882 | bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi))); |
1883 | ||
1884 | /* Walk until we hit the target. */ | |
1885 | while (vuse != target) | |
cc0968b0 | 1886 | { |
5006671f RG |
1887 | gimple def_stmt = SSA_NAME_DEF_STMT (vuse); |
1888 | /* Recurse for PHI nodes. */ | |
1889 | if (gimple_code (def_stmt) == GIMPLE_PHI) | |
1890 | { | |
1891 | /* An already visited PHI node ends the walk successfully. */ | |
1892 | if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt)))) | |
1893 | return true; | |
1894 | vuse = get_continuation_for_phi (def_stmt, ref, visited); | |
1895 | if (!vuse) | |
1896 | return false; | |
1897 | continue; | |
1898 | } | |
1899 | /* A clobbering statement or the end of the IL ends it failing. */ | |
1900 | else if (gimple_nop_p (def_stmt) | |
b45d2719 | 1901 | || stmt_may_clobber_ref_p_1 (def_stmt, ref)) |
5006671f | 1902 | return false; |
d660a3db RG |
1903 | /* If we reach a new basic-block see if we already skipped it |
1904 | in a previous walk that ended successfully. */ | |
1905 | if (gimple_bb (def_stmt) != bb) | |
1906 | { | |
1907 | if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse))) | |
1908 | return true; | |
1909 | bb = gimple_bb (def_stmt); | |
1910 | } | |
5006671f | 1911 | vuse = gimple_vuse (def_stmt); |
cc0968b0 | 1912 | } |
5006671f RG |
1913 | return true; |
1914 | } | |
cc0968b0 | 1915 | |
45ce6084 RG |
1916 | /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code |
1917 | until we hit the phi argument definition that dominates the other one. | |
1918 | Return that, or NULL_TREE if there is no such definition. */ | |
1919 | ||
1920 | static tree | |
1921 | get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1, | |
1922 | ao_ref *ref, bitmap *visited) | |
1923 | { | |
1924 | gimple def0 = SSA_NAME_DEF_STMT (arg0); | |
1925 | gimple def1 = SSA_NAME_DEF_STMT (arg1); | |
1926 | tree common_vuse; | |
1927 | ||
1928 | if (arg0 == arg1) | |
1929 | return arg0; | |
1930 | else if (gimple_nop_p (def0) | |
1931 | || (!gimple_nop_p (def1) | |
1932 | && dominated_by_p (CDI_DOMINATORS, | |
1933 | gimple_bb (def1), gimple_bb (def0)))) | |
1934 | { | |
1935 | if (maybe_skip_until (phi, arg0, ref, arg1, visited)) | |
1936 | return arg0; | |
1937 | } | |
1938 | else if (gimple_nop_p (def1) | |
1939 | || dominated_by_p (CDI_DOMINATORS, | |
1940 | gimple_bb (def0), gimple_bb (def1))) | |
1941 | { | |
1942 | if (maybe_skip_until (phi, arg1, ref, arg0, visited)) | |
1943 | return arg1; | |
1944 | } | |
1945 | /* Special case of a diamond: | |
1946 | MEM_1 = ... | |
1947 | goto (cond) ? L1 : L2 | |
1948 | L1: store1 = ... #MEM_2 = vuse(MEM_1) | |
1949 | goto L3 | |
1950 | L2: store2 = ... #MEM_3 = vuse(MEM_1) | |
1951 | L3: MEM_4 = PHI<MEM_2, MEM_3> | |
1952 | We were called with the PHI at L3, MEM_2 and MEM_3 don't | |
1953 | dominate each other, but still we can easily skip this PHI node | |
1954 | if we recognize that the vuse MEM operand is the same for both, | |
1955 | and that we can skip both statements (they don't clobber us). | |
1956 | This is still linear. Don't use maybe_skip_until, that might | |
1957 | potentially be slow. */ | |
1958 | else if ((common_vuse = gimple_vuse (def0)) | |
1959 | && common_vuse == gimple_vuse (def1)) | |
1960 | { | |
1961 | if (!stmt_may_clobber_ref_p_1 (def0, ref) | |
1962 | && !stmt_may_clobber_ref_p_1 (def1, ref)) | |
1963 | return common_vuse; | |
1964 | } | |
1965 | ||
1966 | return NULL_TREE; | |
1967 | } | |
1968 | ||
1969 | ||
5006671f RG |
1970 | /* Starting from a PHI node for the virtual operand of the memory reference |
1971 | REF find a continuation virtual operand that allows to continue walking | |
1972 | statements dominating PHI skipping only statements that cannot possibly | |
1973 | clobber REF. Returns NULL_TREE if no suitable virtual operand can | |
1974 | be found. */ | |
1975 | ||
84280917 | 1976 | tree |
b45d2719 | 1977 | get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited) |
5006671f RG |
1978 | { |
1979 | unsigned nargs = gimple_phi_num_args (phi); | |
1980 | ||
1981 | /* Through a single-argument PHI we can simply look through. */ | |
1982 | if (nargs == 1) | |
1983 | return PHI_ARG_DEF (phi, 0); | |
1984 | ||
45ce6084 RG |
1985 | /* For two or more arguments try to pairwise skip non-aliasing code |
1986 | until we hit the phi argument definition that dominates the other one. */ | |
1987 | else if (nargs >= 2) | |
cc0968b0 | 1988 | { |
d660a3db RG |
1989 | tree arg0, arg1; |
1990 | unsigned i; | |
1991 | ||
1992 | /* Find a candidate for the virtual operand which definition | |
1993 | dominates those of all others. */ | |
1994 | arg0 = PHI_ARG_DEF (phi, 0); | |
1995 | if (!SSA_NAME_IS_DEFAULT_DEF (arg0)) | |
1996 | for (i = 1; i < nargs; ++i) | |
1997 | { | |
1998 | arg1 = PHI_ARG_DEF (phi, i); | |
1999 | if (SSA_NAME_IS_DEFAULT_DEF (arg1)) | |
2000 | { | |
2001 | arg0 = arg1; | |
2002 | break; | |
2003 | } | |
2004 | if (dominated_by_p (CDI_DOMINATORS, | |
2005 | gimple_bb (SSA_NAME_DEF_STMT (arg0)), | |
2006 | gimple_bb (SSA_NAME_DEF_STMT (arg1)))) | |
2007 | arg0 = arg1; | |
2008 | } | |
2009 | ||
2010 | /* Then pairwise reduce against the found candidate. */ | |
2011 | for (i = 0; i < nargs; ++i) | |
84280917 | 2012 | { |
45ce6084 RG |
2013 | arg1 = PHI_ARG_DEF (phi, i); |
2014 | arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref, visited); | |
2015 | if (!arg0) | |
2016 | return NULL_TREE; | |
84280917 | 2017 | } |
45ce6084 RG |
2018 | |
2019 | return arg0; | |
cc0968b0 | 2020 | } |
5006671f RG |
2021 | |
2022 | return NULL_TREE; | |
cc0968b0 | 2023 | } |
86a07404 | 2024 | |
5006671f RG |
2025 | /* Based on the memory reference REF and its virtual use VUSE call |
2026 | WALKER for each virtual use that is equivalent to VUSE, including VUSE | |
2027 | itself. That is, for each virtual use for which its defining statement | |
2028 | does not clobber REF. | |
51540eba | 2029 | |
5006671f RG |
2030 | WALKER is called with REF, the current virtual use and DATA. If |
2031 | WALKER returns non-NULL the walk stops and its result is returned. | |
2032 | At the end of a non-successful walk NULL is returned. | |
9cf5a7e3 | 2033 | |
01df5c8a RG |
2034 | TRANSLATE if non-NULL is called with a pointer to REF, the virtual |
2035 | use which definition is a statement that may clobber REF and DATA. | |
2036 | If TRANSLATE returns (void *)-1 the walk stops and NULL is returned. | |
2037 | If TRANSLATE returns non-NULL the walk stops and its result is returned. | |
2038 | If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed | |
2039 | to adjust REF and *DATA to make that valid. | |
2040 | ||
5006671f RG |
2041 | TODO: Cache the vector of equivalent vuses per ref, vuse pair. */ |
2042 | ||
2043 | void * | |
b45d2719 RG |
2044 | walk_non_aliased_vuses (ao_ref *ref, tree vuse, |
2045 | void *(*walker)(ao_ref *, tree, void *), | |
2046 | void *(*translate)(ao_ref *, tree, void *), void *data) | |
9cf5a7e3 | 2047 | { |
5006671f RG |
2048 | bitmap visited = NULL; |
2049 | void *res; | |
2050 | ||
8122ccf1 PB |
2051 | timevar_push (TV_ALIAS_STMT_WALK); |
2052 | ||
5006671f RG |
2053 | do |
2054 | { | |
2055 | gimple def_stmt; | |
9cf5a7e3 | 2056 | |
8122ccf1 | 2057 | /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */ |
5006671f RG |
2058 | res = (*walker) (ref, vuse, data); |
2059 | if (res) | |
2060 | break; | |
9cf5a7e3 | 2061 | |
5006671f RG |
2062 | def_stmt = SSA_NAME_DEF_STMT (vuse); |
2063 | if (gimple_nop_p (def_stmt)) | |
2064 | break; | |
2065 | else if (gimple_code (def_stmt) == GIMPLE_PHI) | |
2066 | vuse = get_continuation_for_phi (def_stmt, ref, &visited); | |
2067 | else | |
2068 | { | |
b45d2719 | 2069 | if (stmt_may_clobber_ref_p_1 (def_stmt, ref)) |
01df5c8a RG |
2070 | { |
2071 | if (!translate) | |
2072 | break; | |
b45d2719 | 2073 | res = (*translate) (ref, vuse, data); |
01df5c8a RG |
2074 | /* Failed lookup and translation. */ |
2075 | if (res == (void *)-1) | |
2076 | { | |
2077 | res = NULL; | |
2078 | break; | |
2079 | } | |
2080 | /* Lookup succeeded. */ | |
2081 | else if (res != NULL) | |
2082 | break; | |
2083 | /* Translation succeeded, continue walking. */ | |
2084 | } | |
5006671f RG |
2085 | vuse = gimple_vuse (def_stmt); |
2086 | } | |
2087 | } | |
2088 | while (vuse); | |
cc0968b0 | 2089 | |
5006671f RG |
2090 | if (visited) |
2091 | BITMAP_FREE (visited); | |
cc0968b0 | 2092 | |
8122ccf1 PB |
2093 | timevar_pop (TV_ALIAS_STMT_WALK); |
2094 | ||
5006671f | 2095 | return res; |
9cf5a7e3 KB |
2096 | } |
2097 | ||
fe1f8f44 | 2098 | |
5006671f RG |
2099 | /* Based on the memory reference REF call WALKER for each vdef which |
2100 | defining statement may clobber REF, starting with VDEF. If REF | |
2101 | is NULL_TREE, each defining statement is visited. | |
2102 | ||
2103 | WALKER is called with REF, the current vdef and DATA. If WALKER | |
2104 | returns true the walk is stopped, otherwise it continues. | |
2105 | ||
2106 | At PHI nodes walk_aliased_vdefs forks into one walk for reach | |
2107 | PHI argument (but only one walk continues on merge points), the | |
2108 | return value is true if any of the walks was successful. | |
2109 | ||
2110 | The function returns the number of statements walked. */ | |
fe1f8f44 DB |
2111 | |
2112 | static unsigned int | |
42bc61e0 RG |
2113 | walk_aliased_vdefs_1 (ao_ref *ref, tree vdef, |
2114 | bool (*walker)(ao_ref *, tree, void *), void *data, | |
5006671f | 2115 | bitmap *visited, unsigned int cnt) |
fe1f8f44 | 2116 | { |
5006671f RG |
2117 | do |
2118 | { | |
2119 | gimple def_stmt = SSA_NAME_DEF_STMT (vdef); | |
fe1f8f44 | 2120 | |
5006671f RG |
2121 | if (*visited |
2122 | && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef))) | |
2123 | return cnt; | |
2124 | ||
2125 | if (gimple_nop_p (def_stmt)) | |
2126 | return cnt; | |
2127 | else if (gimple_code (def_stmt) == GIMPLE_PHI) | |
2128 | { | |
2129 | unsigned i; | |
2130 | if (!*visited) | |
2131 | *visited = BITMAP_ALLOC (NULL); | |
2132 | for (i = 0; i < gimple_phi_num_args (def_stmt); ++i) | |
2133 | cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i), | |
2134 | walker, data, visited, 0); | |
2135 | return cnt; | |
2136 | } | |
2137 | ||
8122ccf1 | 2138 | /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */ |
5006671f RG |
2139 | cnt++; |
2140 | if ((!ref | |
42bc61e0 | 2141 | || stmt_may_clobber_ref_p_1 (def_stmt, ref)) |
5006671f RG |
2142 | && (*walker) (ref, vdef, data)) |
2143 | return cnt; | |
2144 | ||
2145 | vdef = gimple_vuse (def_stmt); | |
2146 | } | |
2147 | while (1); | |
fe1f8f44 DB |
2148 | } |
2149 | ||
5006671f | 2150 | unsigned int |
42bc61e0 RG |
2151 | walk_aliased_vdefs (ao_ref *ref, tree vdef, |
2152 | bool (*walker)(ao_ref *, tree, void *), void *data, | |
5006671f | 2153 | bitmap *visited) |
f9d02384 | 2154 | { |
5006671f RG |
2155 | bitmap local_visited = NULL; |
2156 | unsigned int ret; | |
2157 | ||
8122ccf1 PB |
2158 | timevar_push (TV_ALIAS_STMT_WALK); |
2159 | ||
5006671f RG |
2160 | ret = walk_aliased_vdefs_1 (ref, vdef, walker, data, |
2161 | visited ? visited : &local_visited, 0); | |
2162 | if (local_visited) | |
2163 | BITMAP_FREE (local_visited); | |
2164 | ||
8122ccf1 PB |
2165 | timevar_pop (TV_ALIAS_STMT_WALK); |
2166 | ||
5006671f RG |
2167 | return ret; |
2168 | } | |
2169 |