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