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