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