Branch data Line data Source code
1 : : /* Alias analysis for trees.
2 : : Copyright (C) 2004-2025 Free Software Foundation, Inc.
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
9 : : the Free Software Foundation; either version 3, or (at your option)
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
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "target.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 : : #include "ssa.h"
31 : : #include "cgraph.h"
32 : : #include "tree-pretty-print.h"
33 : : #include "alias.h"
34 : : #include "fold-const.h"
35 : : #include "langhooks.h"
36 : : #include "dumpfile.h"
37 : : #include "tree-eh.h"
38 : : #include "tree-dfa.h"
39 : : #include "ipa-reference.h"
40 : : #include "varasm.h"
41 : : #include "ipa-modref-tree.h"
42 : : #include "ipa-modref.h"
43 : : #include "attr-fnspec.h"
44 : : #include "errors.h"
45 : : #include "dbgcnt.h"
46 : : #include "gimple-pretty-print.h"
47 : : #include "print-tree.h"
48 : : #include "tree-ssa-alias-compare.h"
49 : : #include "builtins.h"
50 : : #include "internal-fn.h"
51 : : #include "ipa-utils.h"
52 : :
53 : : /* Broad overview of how alias analysis on gimple works:
54 : :
55 : : Statements clobbering or using memory are linked through the
56 : : virtual operand factored use-def chain. The virtual operand
57 : : is unique per function, its symbol is accessible via gimple_vop (cfun).
58 : : Virtual operands are used for efficiently walking memory statements
59 : : in the gimple IL and are useful for things like value-numbering as
60 : : a generation count for memory references.
61 : :
62 : : SSA_NAME pointers may have associated points-to information
63 : : accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
64 : : points-to information is (re-)computed by the TODO_rebuild_alias
65 : : pass manager todo. Points-to information is also used for more
66 : : precise tracking of call-clobbered and call-used variables and
67 : : related disambiguations.
68 : :
69 : : This file contains functions for disambiguating memory references,
70 : : the so called alias-oracle and tools for walking of the gimple IL.
71 : :
72 : : The main alias-oracle entry-points are
73 : :
74 : : bool stmt_may_clobber_ref_p (gimple *, tree)
75 : :
76 : : This function queries if a statement may invalidate (parts of)
77 : : the memory designated by the reference tree argument.
78 : :
79 : : bool ref_maybe_used_by_stmt_p (gimple *, tree)
80 : :
81 : : This function queries if a statement may need (parts of) the
82 : : memory designated by the reference tree argument.
83 : :
84 : : There are variants of these functions that only handle the call
85 : : part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
86 : : Note that these do not disambiguate against a possible call lhs.
87 : :
88 : : bool refs_may_alias_p (tree, tree)
89 : :
90 : : This function tries to disambiguate two reference trees.
91 : :
92 : : bool ptr_deref_may_alias_global_p (tree, bool)
93 : :
94 : : This function queries if dereferencing a pointer variable may
95 : : alias global memory. If bool argument is true, global memory
96 : : is considered to also include function local memory that escaped.
97 : :
98 : : More low-level disambiguators are available and documented in
99 : : this file. Low-level disambiguators dealing with points-to
100 : : information are in tree-ssa-structalias.cc. */
101 : :
102 : : static int nonoverlapping_refs_since_match_p (tree, tree, tree, tree, bool);
103 : : static bool nonoverlapping_component_refs_p (const_tree, const_tree);
104 : :
105 : : /* Query statistics for the different low-level disambiguators.
106 : : A high-level query may trigger multiple of them. */
107 : :
108 : : static struct {
109 : : unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
110 : : unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
111 : : unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
112 : : unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
113 : : unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
114 : : unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
115 : : unsigned HOST_WIDE_INT aliasing_component_refs_p_may_alias;
116 : : unsigned HOST_WIDE_INT aliasing_component_refs_p_no_alias;
117 : : unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_may_alias;
118 : : unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_no_alias;
119 : : unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_may_alias;
120 : : unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_must_overlap;
121 : : unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_no_alias;
122 : : unsigned HOST_WIDE_INT stmt_kills_ref_p_no;
123 : : unsigned HOST_WIDE_INT stmt_kills_ref_p_yes;
124 : : unsigned HOST_WIDE_INT modref_use_may_alias;
125 : : unsigned HOST_WIDE_INT modref_use_no_alias;
126 : : unsigned HOST_WIDE_INT modref_clobber_may_alias;
127 : : unsigned HOST_WIDE_INT modref_clobber_no_alias;
128 : : unsigned HOST_WIDE_INT modref_kill_no;
129 : : unsigned HOST_WIDE_INT modref_kill_yes;
130 : : unsigned HOST_WIDE_INT modref_tests;
131 : : unsigned HOST_WIDE_INT modref_baseptr_tests;
132 : : } alias_stats;
133 : :
134 : : void
135 : 0 : dump_alias_stats (FILE *s)
136 : : {
137 : 0 : fprintf (s, "\nAlias oracle query stats:\n");
138 : 0 : fprintf (s, " refs_may_alias_p: "
139 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
140 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
141 : : alias_stats.refs_may_alias_p_no_alias,
142 : 0 : alias_stats.refs_may_alias_p_no_alias
143 : 0 : + alias_stats.refs_may_alias_p_may_alias);
144 : 0 : fprintf (s, " ref_maybe_used_by_call_p: "
145 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
146 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
147 : : alias_stats.ref_maybe_used_by_call_p_no_alias,
148 : 0 : alias_stats.refs_may_alias_p_no_alias
149 : 0 : + alias_stats.ref_maybe_used_by_call_p_may_alias);
150 : 0 : fprintf (s, " call_may_clobber_ref_p: "
151 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
152 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
153 : : alias_stats.call_may_clobber_ref_p_no_alias,
154 : 0 : alias_stats.call_may_clobber_ref_p_no_alias
155 : 0 : + alias_stats.call_may_clobber_ref_p_may_alias);
156 : 0 : fprintf (s, " stmt_kills_ref_p: "
157 : : HOST_WIDE_INT_PRINT_DEC" kills, "
158 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
159 : : alias_stats.stmt_kills_ref_p_yes + alias_stats.modref_kill_yes,
160 : 0 : alias_stats.stmt_kills_ref_p_yes + alias_stats.modref_kill_yes
161 : 0 : + alias_stats.stmt_kills_ref_p_no + alias_stats.modref_kill_no);
162 : 0 : fprintf (s, " nonoverlapping_component_refs_p: "
163 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
164 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
165 : : alias_stats.nonoverlapping_component_refs_p_no_alias,
166 : 0 : alias_stats.nonoverlapping_component_refs_p_no_alias
167 : 0 : + alias_stats.nonoverlapping_component_refs_p_may_alias);
168 : 0 : fprintf (s, " nonoverlapping_refs_since_match_p: "
169 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
170 : : HOST_WIDE_INT_PRINT_DEC" must overlaps, "
171 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
172 : : alias_stats.nonoverlapping_refs_since_match_p_no_alias,
173 : : alias_stats.nonoverlapping_refs_since_match_p_must_overlap,
174 : 0 : alias_stats.nonoverlapping_refs_since_match_p_no_alias
175 : 0 : + alias_stats.nonoverlapping_refs_since_match_p_may_alias
176 : 0 : + alias_stats.nonoverlapping_refs_since_match_p_must_overlap);
177 : 0 : fprintf (s, " aliasing_component_refs_p: "
178 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
179 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
180 : : alias_stats.aliasing_component_refs_p_no_alias,
181 : 0 : alias_stats.aliasing_component_refs_p_no_alias
182 : 0 : + alias_stats.aliasing_component_refs_p_may_alias);
183 : 0 : dump_alias_stats_in_alias_c (s);
184 : 0 : fprintf (s, "\nModref stats:\n");
185 : 0 : fprintf (s, " modref kill: "
186 : : HOST_WIDE_INT_PRINT_DEC" kills, "
187 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
188 : : alias_stats.modref_kill_yes,
189 : 0 : alias_stats.modref_kill_yes
190 : 0 : + alias_stats.modref_kill_no);
191 : 0 : fprintf (s, " modref use: "
192 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
193 : : HOST_WIDE_INT_PRINT_DEC" queries\n",
194 : : alias_stats.modref_use_no_alias,
195 : 0 : alias_stats.modref_use_no_alias
196 : 0 : + alias_stats.modref_use_may_alias);
197 : 0 : fprintf (s, " modref clobber: "
198 : : HOST_WIDE_INT_PRINT_DEC" disambiguations, "
199 : : HOST_WIDE_INT_PRINT_DEC" queries\n"
200 : : " " HOST_WIDE_INT_PRINT_DEC" tbaa queries (%f per modref query)\n"
201 : : " " HOST_WIDE_INT_PRINT_DEC" base compares (%f per modref query)\n",
202 : : alias_stats.modref_clobber_no_alias,
203 : : alias_stats.modref_clobber_no_alias
204 : : + alias_stats.modref_clobber_may_alias,
205 : : alias_stats.modref_tests,
206 : 0 : ((double)alias_stats.modref_tests)
207 : : / (alias_stats.modref_clobber_no_alias
208 : : + alias_stats.modref_clobber_may_alias),
209 : : alias_stats.modref_baseptr_tests,
210 : 0 : ((double)alias_stats.modref_baseptr_tests)
211 : 0 : / (alias_stats.modref_clobber_no_alias
212 : 0 : + alias_stats.modref_clobber_may_alias));
213 : 0 : }
214 : :
215 : :
216 : : /* Return true, if dereferencing PTR may alias with a global variable.
217 : : When ESCAPED_LOCAL_P is true escaped local memory is also considered
218 : : global. */
219 : :
220 : : bool
221 : 59440499 : ptr_deref_may_alias_global_p (tree ptr, bool escaped_local_p)
222 : : {
223 : 59440499 : struct ptr_info_def *pi;
224 : :
225 : : /* If we end up with a pointer constant here that may point
226 : : to global memory. */
227 : 59440499 : if (TREE_CODE (ptr) != SSA_NAME)
228 : : return true;
229 : :
230 : 59434371 : pi = SSA_NAME_PTR_INFO (ptr);
231 : :
232 : : /* If we do not have points-to information for this variable,
233 : : we have to punt. */
234 : 59434371 : if (!pi)
235 : : return true;
236 : :
237 : : /* ??? This does not use TBAA to prune globals ptr may not access. */
238 : 47621086 : return pt_solution_includes_global (&pi->pt, escaped_local_p);
239 : : }
240 : :
241 : : /* Return true if dereferencing PTR may alias DECL.
242 : : The caller is responsible for applying TBAA to see if PTR
243 : : may access DECL at all. */
244 : :
245 : : static bool
246 : 211496181 : ptr_deref_may_alias_decl_p (tree ptr, tree decl)
247 : : {
248 : 211496181 : struct ptr_info_def *pi;
249 : :
250 : : /* Conversions are irrelevant for points-to information and
251 : : data-dependence analysis can feed us those. */
252 : 211496181 : STRIP_NOPS (ptr);
253 : :
254 : : /* Anything we do not explicilty handle aliases. */
255 : 211496181 : if ((TREE_CODE (ptr) != SSA_NAME
256 : 2138350 : && TREE_CODE (ptr) != ADDR_EXPR
257 : 972977 : && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
258 : 210523204 : || !POINTER_TYPE_P (TREE_TYPE (ptr))
259 : 422018887 : || (!VAR_P (decl)
260 : : && TREE_CODE (decl) != PARM_DECL
261 : : && TREE_CODE (decl) != RESULT_DECL))
262 : : return true;
263 : :
264 : : /* Disregard pointer offsetting. */
265 : 210521658 : if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
266 : : {
267 : 0 : do
268 : : {
269 : 0 : ptr = TREE_OPERAND (ptr, 0);
270 : : }
271 : 0 : while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
272 : : return ptr_deref_may_alias_decl_p (ptr, decl);
273 : : }
274 : :
275 : : /* ADDR_EXPR pointers either just offset another pointer or directly
276 : : specify the pointed-to set. */
277 : 210521658 : if (TREE_CODE (ptr) == ADDR_EXPR)
278 : : {
279 : 1164354 : tree base = get_base_address (TREE_OPERAND (ptr, 0));
280 : 1164354 : if (base
281 : 1164354 : && (TREE_CODE (base) == MEM_REF
282 : 1164354 : || TREE_CODE (base) == TARGET_MEM_REF))
283 : 21075 : ptr = TREE_OPERAND (base, 0);
284 : 1143279 : else if (base
285 : 1143279 : && DECL_P (base))
286 : 1126473 : return compare_base_decls (base, decl) != 0;
287 : 16806 : else if (base
288 : 16806 : && CONSTANT_CLASS_P (base))
289 : : return false;
290 : : else
291 : : return true;
292 : : }
293 : :
294 : : /* Non-aliased variables cannot be pointed to. */
295 : 209378379 : if (!may_be_aliased (decl))
296 : : return false;
297 : :
298 : : /* From here we require a SSA name pointer. Anything else aliases. */
299 : 77474665 : if (TREE_CODE (ptr) != SSA_NAME
300 : 77474665 : || !POINTER_TYPE_P (TREE_TYPE (ptr)))
301 : : return true;
302 : :
303 : : /* If we do not have useful points-to information for this pointer
304 : : we cannot disambiguate anything else. */
305 : 77474665 : pi = SSA_NAME_PTR_INFO (ptr);
306 : 77474665 : if (!pi)
307 : : return true;
308 : :
309 : 76469121 : return pt_solution_includes (&pi->pt, decl);
310 : : }
311 : :
312 : : /* Return true if dereferenced PTR1 and PTR2 may alias.
313 : : The caller is responsible for applying TBAA to see if accesses
314 : : through PTR1 and PTR2 may conflict at all. */
315 : :
316 : : bool
317 : 65690184 : ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
318 : : {
319 : 67147272 : struct ptr_info_def *pi1, *pi2;
320 : :
321 : : /* Conversions are irrelevant for points-to information and
322 : : data-dependence analysis can feed us those. */
323 : 67147272 : STRIP_NOPS (ptr1);
324 : 67147272 : STRIP_NOPS (ptr2);
325 : :
326 : : /* Disregard pointer offsetting. */
327 : 67147272 : if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
328 : : {
329 : 628368 : do
330 : : {
331 : 628368 : ptr1 = TREE_OPERAND (ptr1, 0);
332 : : }
333 : 628368 : while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
334 : : return ptr_derefs_may_alias_p (ptr1, ptr2);
335 : : }
336 : 66518904 : if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
337 : : {
338 : 644057 : do
339 : : {
340 : 644057 : ptr2 = TREE_OPERAND (ptr2, 0);
341 : : }
342 : 644057 : while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
343 : : return ptr_derefs_may_alias_p (ptr1, ptr2);
344 : : }
345 : :
346 : : /* ADDR_EXPR pointers either just offset another pointer or directly
347 : : specify the pointed-to set. */
348 : 65874847 : if (TREE_CODE (ptr1) == ADDR_EXPR)
349 : : {
350 : 888969 : tree base = get_base_address (TREE_OPERAND (ptr1, 0));
351 : 888969 : if (base
352 : 888969 : && (TREE_CODE (base) == MEM_REF
353 : 888969 : || TREE_CODE (base) == TARGET_MEM_REF))
354 : 90863 : return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
355 : 798106 : else if (base
356 : 798106 : && DECL_P (base))
357 : 795513 : return ptr_deref_may_alias_decl_p (ptr2, base);
358 : : /* Try ptr2 when ptr1 points to a constant. */
359 : : else if (base
360 : 2593 : && !CONSTANT_CLASS_P (base))
361 : : return true;
362 : : }
363 : 64988471 : if (TREE_CODE (ptr2) == ADDR_EXPR)
364 : : {
365 : 326658 : tree base = get_base_address (TREE_OPERAND (ptr2, 0));
366 : 326658 : if (base
367 : 326658 : && (TREE_CODE (base) == MEM_REF
368 : 326658 : || TREE_CODE (base) == TARGET_MEM_REF))
369 : 93800 : return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
370 : 232858 : else if (base
371 : 232858 : && DECL_P (base))
372 : 230771 : return ptr_deref_may_alias_decl_p (ptr1, base);
373 : : else
374 : : return true;
375 : : }
376 : :
377 : : /* From here we require SSA name pointers. Anything else aliases. */
378 : 64661813 : if (TREE_CODE (ptr1) != SSA_NAME
379 : 64527489 : || TREE_CODE (ptr2) != SSA_NAME
380 : 64509489 : || !POINTER_TYPE_P (TREE_TYPE (ptr1))
381 : 129161520 : || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
382 : : return true;
383 : :
384 : : /* We may end up with two empty points-to solutions for two same pointers.
385 : : In this case we still want to say both pointers alias, so shortcut
386 : : that here. */
387 : 64499592 : if (ptr1 == ptr2)
388 : : return true;
389 : :
390 : : /* If we do not have useful points-to information for either pointer
391 : : we cannot disambiguate anything else. */
392 : 60770917 : pi1 = SSA_NAME_PTR_INFO (ptr1);
393 : 60770917 : pi2 = SSA_NAME_PTR_INFO (ptr2);
394 : 60770917 : if (!pi1 || !pi2)
395 : : return true;
396 : :
397 : : /* ??? This does not use TBAA to prune decls from the intersection
398 : : that not both pointers may access. */
399 : 58234574 : return pt_solutions_intersect (&pi1->pt, &pi2->pt);
400 : : }
401 : :
402 : : /* Return true if dereferencing PTR may alias *REF.
403 : : The caller is responsible for applying TBAA to see if PTR
404 : : may access *REF at all. */
405 : :
406 : : static bool
407 : 1742560 : ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
408 : : {
409 : 1742560 : tree base = ao_ref_base (ref);
410 : :
411 : 1742560 : if (TREE_CODE (base) == MEM_REF
412 : 1742560 : || TREE_CODE (base) == TARGET_MEM_REF)
413 : 211107 : return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
414 : 1531453 : else if (DECL_P (base))
415 : 1529136 : return ptr_deref_may_alias_decl_p (ptr, base);
416 : :
417 : : return true;
418 : : }
419 : :
420 : : /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
421 : :
422 : : bool
423 : 56350888 : ptrs_compare_unequal (tree ptr1, tree ptr2)
424 : : {
425 : : /* First resolve the pointers down to a SSA name pointer base or
426 : : a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
427 : : not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
428 : : or STRING_CSTs which needs points-to adjustments to track them
429 : : in the points-to sets. */
430 : 56350888 : tree obj1 = NULL_TREE;
431 : 56350888 : tree obj2 = NULL_TREE;
432 : 56350888 : if (TREE_CODE (ptr1) == ADDR_EXPR)
433 : : {
434 : 52323 : tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
435 : 52323 : if (! tem)
436 : : return false;
437 : 52323 : if (VAR_P (tem)
438 : : || TREE_CODE (tem) == PARM_DECL
439 : : || TREE_CODE (tem) == RESULT_DECL)
440 : : obj1 = tem;
441 : : else if (TREE_CODE (tem) == MEM_REF)
442 : 6632 : ptr1 = TREE_OPERAND (tem, 0);
443 : : }
444 : 56350888 : if (TREE_CODE (ptr2) == ADDR_EXPR)
445 : : {
446 : 9262136 : tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
447 : 9262136 : if (! tem)
448 : : return false;
449 : 9262136 : if (VAR_P (tem)
450 : : || TREE_CODE (tem) == PARM_DECL
451 : : || TREE_CODE (tem) == RESULT_DECL)
452 : : obj2 = tem;
453 : : else if (TREE_CODE (tem) == MEM_REF)
454 : 136897 : ptr2 = TREE_OPERAND (tem, 0);
455 : : }
456 : :
457 : : /* Canonicalize ptr vs. object. */
458 : 56350888 : if (TREE_CODE (ptr1) == SSA_NAME && obj2)
459 : : {
460 : : std::swap (ptr1, ptr2);
461 : : std::swap (obj1, obj2);
462 : : }
463 : :
464 : 56350888 : if (obj1 && obj2)
465 : : /* Other code handles this correctly, no need to duplicate it here. */;
466 : 6475138 : else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
467 : : {
468 : 6463994 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
469 : : /* We may not use restrict to optimize pointer comparisons.
470 : : See PR71062. So we have to assume that restrict-pointed-to
471 : : may be in fact obj1. */
472 : 6463994 : if (!pi
473 : 5346407 : || pi->pt.vars_contains_restrict
474 : 5336486 : || pi->pt.vars_contains_interposable)
475 : : return false;
476 : 4720473 : if (VAR_P (obj1)
477 : 4720473 : && (TREE_STATIC (obj1) || DECL_EXTERNAL (obj1)))
478 : : {
479 : 1125341 : varpool_node *node = varpool_node::get (obj1);
480 : : /* If obj1 may bind to NULL give up (see below). */
481 : 1125341 : if (! node
482 : 1125341 : || ! node->nonzero_address ()
483 : 2250682 : || ! decl_binds_to_current_def_p (obj1))
484 : 851396 : return false;
485 : : }
486 : 3869077 : return !pt_solution_includes (&pi->pt, obj1);
487 : : }
488 : 49874641 : else if (TREE_CODE (ptr1) == SSA_NAME)
489 : : {
490 : 44085556 : struct ptr_info_def *pi1 = SSA_NAME_PTR_INFO (ptr1);
491 : 44085556 : if (!pi1
492 : 33611217 : || pi1->pt.vars_contains_restrict
493 : 32574082 : || pi1->pt.vars_contains_interposable)
494 : : return false;
495 : 30677797 : if (integer_zerop (ptr2) && !pi1->pt.null)
496 : : return true;
497 : 30655079 : if (TREE_CODE (ptr2) == SSA_NAME)
498 : : {
499 : 10594394 : struct ptr_info_def *pi2 = SSA_NAME_PTR_INFO (ptr2);
500 : 10594394 : if (!pi2
501 : 9885216 : || pi2->pt.vars_contains_restrict
502 : 9878770 : || pi2->pt.vars_contains_interposable)
503 : : return false;
504 : 8461804 : if ((!pi1->pt.null || !pi2->pt.null)
505 : : /* ??? We do not represent FUNCTION_DECL and LABEL_DECL
506 : : in pt.vars but only set pt.vars_contains_nonlocal. This
507 : : makes compares involving those and other nonlocals
508 : : imprecise. */
509 : 3845521 : && (!pi1->pt.vars_contains_nonlocal
510 : 64172 : || !pi2->pt.vars_contains_nonlocal)
511 : 13637798 : && (!pt_solution_includes_const_pool (&pi1->pt)
512 : 3446121 : || !pt_solution_includes_const_pool (&pi2->pt)))
513 : 460664 : return !pt_solutions_intersect (&pi1->pt, &pi2->pt);
514 : : }
515 : : }
516 : :
517 : : return false;
518 : : }
519 : :
520 : : /* Returns whether reference REF to BASE may refer to global memory.
521 : : When ESCAPED_LOCAL_P is true escaped local memory is also considered
522 : : global. */
523 : :
524 : : static bool
525 : 55854589 : ref_may_alias_global_p_1 (tree base, bool escaped_local_p)
526 : : {
527 : 55854589 : if (DECL_P (base))
528 : 43639506 : return (is_global_var (base)
529 : 43639506 : || (escaped_local_p
530 : 1334485 : && pt_solution_includes (&cfun->gimple_df->escaped_return,
531 : : base)));
532 : 12215083 : else if (TREE_CODE (base) == MEM_REF
533 : 12215083 : || TREE_CODE (base) == TARGET_MEM_REF)
534 : 12204190 : return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0),
535 : 12204190 : escaped_local_p);
536 : : return true;
537 : : }
538 : :
539 : : bool
540 : 11380577 : ref_may_alias_global_p (ao_ref *ref, bool escaped_local_p)
541 : : {
542 : 11380577 : tree base = ao_ref_base (ref);
543 : 11380577 : return ref_may_alias_global_p_1 (base, escaped_local_p);
544 : : }
545 : :
546 : : bool
547 : 44474012 : ref_may_alias_global_p (tree ref, bool escaped_local_p)
548 : : {
549 : 44474012 : tree base = get_base_address (ref);
550 : 44474012 : return ref_may_alias_global_p_1 (base, escaped_local_p);
551 : : }
552 : :
553 : : /* Return true whether STMT may clobber global memory.
554 : : When ESCAPED_LOCAL_P is true escaped local memory is also considered
555 : : global. */
556 : :
557 : : bool
558 : 180765591 : stmt_may_clobber_global_p (gimple *stmt, bool escaped_local_p)
559 : : {
560 : 180765591 : tree lhs;
561 : :
562 : 360991974 : if (!gimple_vdef (stmt))
563 : : return false;
564 : :
565 : : /* ??? We can ask the oracle whether an artificial pointer
566 : : dereference with a pointer with points-to information covering
567 : : all global memory (what about non-address taken memory?) maybe
568 : : clobbered by this call. As there is at the moment no convenient
569 : : way of doing that without generating garbage do some manual
570 : : checking instead.
571 : : ??? We could make a NULL ao_ref argument to the various
572 : : predicates special, meaning any global memory. */
573 : :
574 : 44550520 : switch (gimple_code (stmt))
575 : : {
576 : 44474012 : case GIMPLE_ASSIGN:
577 : 44474012 : lhs = gimple_assign_lhs (stmt);
578 : 44474012 : return (TREE_CODE (lhs) != SSA_NAME
579 : 44474012 : && ref_may_alias_global_p (lhs, escaped_local_p));
580 : : case GIMPLE_CALL:
581 : : return true;
582 : : default:
583 : : return true;
584 : : }
585 : : }
586 : :
587 : :
588 : : /* Dump alias information on FILE. */
589 : :
590 : : void
591 : 282 : dump_alias_info (FILE *file)
592 : : {
593 : 282 : unsigned i;
594 : 282 : tree ptr;
595 : 282 : const char *funcname
596 : 282 : = lang_hooks.decl_printable_name (current_function_decl, 2);
597 : 282 : tree var;
598 : :
599 : 282 : fprintf (file, "\n\nAlias information for %s\n\n", funcname);
600 : :
601 : 282 : fprintf (file, "Aliased symbols\n\n");
602 : :
603 : 1114 : FOR_EACH_LOCAL_DECL (cfun, i, var)
604 : : {
605 : 579 : if (may_be_aliased (var))
606 : 347 : dump_variable (file, var);
607 : : }
608 : :
609 : 282 : fprintf (file, "\nCall clobber information\n");
610 : :
611 : 282 : fprintf (file, "\nESCAPED");
612 : 282 : dump_points_to_solution (file, &cfun->gimple_df->escaped);
613 : :
614 : 282 : fprintf (file, "\nESCAPED_RETURN");
615 : 282 : dump_points_to_solution (file, &cfun->gimple_df->escaped_return);
616 : :
617 : 282 : fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
618 : :
619 : 3015 : FOR_EACH_SSA_NAME (i, ptr, cfun)
620 : : {
621 : 2513 : struct ptr_info_def *pi;
622 : :
623 : 4587 : if (!POINTER_TYPE_P (TREE_TYPE (ptr))
624 : 2560 : || SSA_NAME_IN_FREE_LIST (ptr))
625 : 2027 : continue;
626 : :
627 : 486 : pi = SSA_NAME_PTR_INFO (ptr);
628 : 486 : if (pi)
629 : 480 : dump_points_to_info_for (file, ptr);
630 : : }
631 : :
632 : 282 : fprintf (file, "\n");
633 : 282 : }
634 : :
635 : :
636 : : /* Dump alias information on stderr. */
637 : :
638 : : DEBUG_FUNCTION void
639 : 0 : debug_alias_info (void)
640 : : {
641 : 0 : dump_alias_info (stderr);
642 : 0 : }
643 : :
644 : :
645 : : /* Dump the points-to set *PT into FILE. */
646 : :
647 : : void
648 : 1044 : dump_points_to_solution (FILE *file, struct pt_solution *pt)
649 : : {
650 : 1044 : if (pt->anything)
651 : 3 : fprintf (file, ", points-to anything");
652 : :
653 : 1044 : if (pt->nonlocal)
654 : 632 : fprintf (file, ", points-to non-local");
655 : :
656 : 1044 : if (pt->escaped)
657 : 419 : fprintf (file, ", points-to escaped");
658 : :
659 : 1044 : if (pt->ipa_escaped)
660 : 0 : fprintf (file, ", points-to unit escaped");
661 : :
662 : 1044 : if (pt->null)
663 : 606 : fprintf (file, ", points-to NULL");
664 : :
665 : 1044 : if (pt->const_pool)
666 : 0 : fprintf (file, ", points-to const-pool");
667 : :
668 : 1044 : if (pt->vars)
669 : : {
670 : 1041 : fprintf (file, ", points-to vars: ");
671 : 1041 : dump_decl_set (file, pt->vars);
672 : 1041 : if (pt->vars_contains_nonlocal
673 : 907 : || pt->vars_contains_escaped
674 : 830 : || pt->vars_contains_escaped_heap
675 : 830 : || pt->vars_contains_restrict
676 : 830 : || pt->vars_contains_interposable)
677 : : {
678 : 211 : const char *comma = "";
679 : 211 : fprintf (file, " (");
680 : 211 : if (pt->vars_contains_nonlocal)
681 : : {
682 : 134 : fprintf (file, "nonlocal");
683 : 134 : comma = ", ";
684 : : }
685 : 211 : if (pt->vars_contains_escaped)
686 : : {
687 : 139 : fprintf (file, "%sescaped", comma);
688 : 139 : comma = ", ";
689 : : }
690 : 211 : if (pt->vars_contains_escaped_heap)
691 : : {
692 : 0 : fprintf (file, "%sescaped heap", comma);
693 : 0 : comma = ", ";
694 : : }
695 : 211 : if (pt->vars_contains_restrict)
696 : : {
697 : 58 : fprintf (file, "%srestrict", comma);
698 : 58 : comma = ", ";
699 : : }
700 : 211 : if (pt->vars_contains_interposable)
701 : 0 : fprintf (file, "%sinterposable", comma);
702 : 211 : fprintf (file, ")");
703 : : }
704 : : }
705 : 1044 : }
706 : :
707 : :
708 : : /* Unified dump function for pt_solution. */
709 : :
710 : : DEBUG_FUNCTION void
711 : 0 : debug (pt_solution &ref)
712 : : {
713 : 0 : dump_points_to_solution (stderr, &ref);
714 : 0 : }
715 : :
716 : : DEBUG_FUNCTION void
717 : 0 : debug (pt_solution *ptr)
718 : : {
719 : 0 : if (ptr)
720 : 0 : debug (*ptr);
721 : : else
722 : 0 : fprintf (stderr, "<nil>\n");
723 : 0 : }
724 : :
725 : :
726 : : /* Dump points-to information for SSA_NAME PTR into FILE. */
727 : :
728 : : void
729 : 480 : dump_points_to_info_for (FILE *file, tree ptr)
730 : : {
731 : 480 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
732 : :
733 : 480 : print_generic_expr (file, ptr, dump_flags);
734 : :
735 : 480 : if (pi)
736 : 480 : dump_points_to_solution (file, &pi->pt);
737 : : else
738 : 0 : fprintf (file, ", points-to anything");
739 : :
740 : 480 : fprintf (file, "\n");
741 : 480 : }
742 : :
743 : :
744 : : /* Dump points-to information for VAR into stderr. */
745 : :
746 : : DEBUG_FUNCTION void
747 : 0 : debug_points_to_info_for (tree var)
748 : : {
749 : 0 : dump_points_to_info_for (stderr, var);
750 : 0 : }
751 : :
752 : :
753 : : /* Initializes the alias-oracle reference representation *R from REF. */
754 : :
755 : : void
756 : 2772512955 : ao_ref_init (ao_ref *r, tree ref)
757 : : {
758 : 2772512955 : r->ref = ref;
759 : 2772512955 : r->base = NULL_TREE;
760 : 2772512955 : r->offset = 0;
761 : 2772512955 : r->size = -1;
762 : 2772512955 : r->max_size = -1;
763 : 2772512955 : r->ref_alias_set = -1;
764 : 2772512955 : r->base_alias_set = -1;
765 : 2772512955 : r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
766 : 2772512955 : }
767 : :
768 : : /* Returns the base object of the memory reference *REF. */
769 : :
770 : : tree
771 : 5400244952 : ao_ref_base (ao_ref *ref)
772 : : {
773 : 5400244952 : bool reverse;
774 : :
775 : 5400244952 : if (ref->base)
776 : : return ref->base;
777 : 2502328927 : ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
778 : : &ref->max_size, &reverse);
779 : 2502328927 : return ref->base;
780 : : }
781 : :
782 : : /* Returns the base object alias set of the memory reference *REF. */
783 : :
784 : : alias_set_type
785 : 1007893677 : ao_ref_base_alias_set (ao_ref *ref)
786 : : {
787 : 1007893677 : tree base_ref;
788 : 1007893677 : if (ref->base_alias_set != -1)
789 : : return ref->base_alias_set;
790 : 784352767 : if (!ref->ref)
791 : : return 0;
792 : 746316318 : base_ref = ref->ref;
793 : 746316318 : if (TREE_CODE (base_ref) == WITH_SIZE_EXPR)
794 : 4 : base_ref = TREE_OPERAND (base_ref, 0);
795 : 1190410934 : while (handled_component_p (base_ref))
796 : 444094616 : base_ref = TREE_OPERAND (base_ref, 0);
797 : 746316318 : ref->base_alias_set = get_alias_set (base_ref);
798 : 746316318 : return ref->base_alias_set;
799 : : }
800 : :
801 : : /* Returns the reference alias set of the memory reference *REF. */
802 : :
803 : : alias_set_type
804 : 1282184632 : ao_ref_alias_set (ao_ref *ref)
805 : : {
806 : 1282184632 : if (ref->ref_alias_set != -1)
807 : : return ref->ref_alias_set;
808 : 520192410 : if (!ref->ref)
809 : : return 0;
810 : 520192408 : ref->ref_alias_set = get_alias_set (ref->ref);
811 : 520192408 : return ref->ref_alias_set;
812 : : }
813 : :
814 : : /* Returns a type satisfying
815 : : get_deref_alias_set (type) == ao_ref_base_alias_set (REF). */
816 : :
817 : : tree
818 : 319514 : ao_ref_base_alias_ptr_type (ao_ref *ref)
819 : : {
820 : 319514 : tree base_ref;
821 : :
822 : 319514 : if (!ref->ref)
823 : : return NULL_TREE;
824 : 319514 : base_ref = ref->ref;
825 : 319514 : if (TREE_CODE (base_ref) == WITH_SIZE_EXPR)
826 : 0 : base_ref = TREE_OPERAND (base_ref, 0);
827 : 425306 : while (handled_component_p (base_ref))
828 : 105792 : base_ref = TREE_OPERAND (base_ref, 0);
829 : 319514 : tree ret = reference_alias_ptr_type (base_ref);
830 : 319514 : return ret;
831 : : }
832 : :
833 : : /* Returns a type satisfying
834 : : get_deref_alias_set (type) == ao_ref_alias_set (REF). */
835 : :
836 : : tree
837 : 319514 : ao_ref_alias_ptr_type (ao_ref *ref)
838 : : {
839 : 319514 : if (!ref->ref)
840 : : return NULL_TREE;
841 : 319514 : tree ret = reference_alias_ptr_type (ref->ref);
842 : 319514 : return ret;
843 : : }
844 : :
845 : : /* Return the alignment of the access *REF and store it in the *ALIGN
846 : : and *BITPOS pairs. Returns false if no alignment could be determined.
847 : : See get_object_alignment_2 for details. */
848 : :
849 : : bool
850 : 95424 : ao_ref_alignment (ao_ref *ref, unsigned int *align,
851 : : unsigned HOST_WIDE_INT *bitpos)
852 : : {
853 : 95424 : if (ref->ref)
854 : 93904 : return get_object_alignment_1 (ref->ref, align, bitpos);
855 : :
856 : : /* When we just have ref->base we cannot use get_object_alignment since
857 : : that will eventually use the type of the appearant access while for
858 : : example ao_ref_init_from_ptr_and_range is not careful to adjust that. */
859 : 1520 : *align = BITS_PER_UNIT;
860 : 1520 : HOST_WIDE_INT offset;
861 : 1520 : if (!ref->offset.is_constant (&offset)
862 : 1520 : || !get_object_alignment_2 (ref->base, align, bitpos, true))
863 : : return false;
864 : 1325 : *bitpos += (unsigned HOST_WIDE_INT)offset * BITS_PER_UNIT;
865 : 1325 : *bitpos = *bitpos & (*align - 1);
866 : 1325 : return true;
867 : : }
868 : :
869 : : /* Init an alias-oracle reference representation from a gimple pointer
870 : : PTR a range specified by OFFSET, SIZE and MAX_SIZE under the assumption
871 : : that RANGE_KNOWN is set.
872 : :
873 : : The access is assumed to be only to or after of the pointer target adjusted
874 : : by the offset, not before it (even in the case RANGE_KNOWN is false). */
875 : :
876 : : void
877 : 39389066 : ao_ref_init_from_ptr_and_range (ao_ref *ref, tree ptr,
878 : : bool range_known,
879 : : poly_int64 offset,
880 : : poly_int64 size,
881 : : poly_int64 max_size)
882 : : {
883 : 39389066 : poly_int64 t, extra_offset = 0;
884 : :
885 : 39389066 : ref->ref = NULL_TREE;
886 : 39389066 : if (TREE_CODE (ptr) == SSA_NAME)
887 : : {
888 : 24985573 : gimple *stmt = SSA_NAME_DEF_STMT (ptr);
889 : 24985573 : if (gimple_assign_single_p (stmt)
890 : 24985573 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
891 : 3926214 : ptr = gimple_assign_rhs1 (stmt);
892 : 21059359 : else if (is_gimple_assign (stmt)
893 : 14011479 : && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
894 : 26579210 : && ptrdiff_tree_p (gimple_assign_rhs2 (stmt), &extra_offset))
895 : : {
896 : 255781 : ptr = gimple_assign_rhs1 (stmt);
897 : 255781 : extra_offset *= BITS_PER_UNIT;
898 : : }
899 : : }
900 : :
901 : 39389066 : if (TREE_CODE (ptr) == ADDR_EXPR)
902 : : {
903 : 18273262 : ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
904 : 18273262 : if (ref->base
905 : 33788634 : && coeffs_in_range_p (t, -HOST_WIDE_INT_MAX / BITS_PER_UNIT,
906 : : HOST_WIDE_INT_MAX / BITS_PER_UNIT))
907 : 15515194 : ref->offset = BITS_PER_UNIT * t;
908 : : else
909 : : {
910 : 2758068 : range_known = false;
911 : 2758068 : ref->offset = 0;
912 : 2758068 : ref->base = get_base_address (TREE_OPERAND (ptr, 0));
913 : : }
914 : : }
915 : : else
916 : : {
917 : 21115804 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
918 : 21115804 : ref->base = build2 (MEM_REF, char_type_node,
919 : : ptr, null_pointer_node);
920 : 21115804 : ref->offset = 0;
921 : : }
922 : 39389066 : ref->offset += extra_offset + offset;
923 : 39389066 : if (range_known)
924 : : {
925 : 21496426 : ref->max_size = max_size;
926 : 21496426 : ref->size = size;
927 : : }
928 : : else
929 : 17892640 : ref->max_size = ref->size = -1;
930 : 39389066 : ref->ref_alias_set = 0;
931 : 39389066 : ref->base_alias_set = 0;
932 : 39389066 : ref->volatile_p = false;
933 : 39389066 : }
934 : :
935 : : /* Init an alias-oracle reference representation from a gimple pointer
936 : : PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
937 : : size is assumed to be unknown. The access is assumed to be only
938 : : to or after of the pointer target, not before it. */
939 : :
940 : : void
941 : 10975355 : ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
942 : : {
943 : 10975355 : poly_int64 size_hwi;
944 : 10975355 : if (size
945 : 5746122 : && poly_int_tree_p (size, &size_hwi)
946 : 16095457 : && coeffs_in_range_p (size_hwi, 0, HOST_WIDE_INT_MAX / BITS_PER_UNIT))
947 : : {
948 : 5119400 : size_hwi = size_hwi * BITS_PER_UNIT;
949 : 5119400 : ao_ref_init_from_ptr_and_range (ref, ptr, true, 0, size_hwi, size_hwi);
950 : : }
951 : : else
952 : 5855955 : ao_ref_init_from_ptr_and_range (ref, ptr, false, 0, -1, -1);
953 : 10975355 : }
954 : :
955 : : /* S1 and S2 are TYPE_SIZE or DECL_SIZE. Compare them:
956 : : Return -1 if S1 < S2
957 : : Return 1 if S1 > S2
958 : : Return 0 if equal or incomparable. */
959 : :
960 : : static int
961 : 8973661 : compare_sizes (tree s1, tree s2)
962 : : {
963 : 8973661 : if (!s1 || !s2)
964 : : return 0;
965 : :
966 : 8964275 : poly_uint64 size1;
967 : 8964275 : poly_uint64 size2;
968 : :
969 : 8964275 : if (!poly_int_tree_p (s1, &size1) || !poly_int_tree_p (s2, &size2))
970 : 494 : return 0;
971 : 8963781 : if (known_lt (size1, size2))
972 : : return -1;
973 : 6471485 : if (known_lt (size2, size1))
974 : : return 1;
975 : : return 0;
976 : : }
977 : :
978 : : /* Compare TYPE1 and TYPE2 by its size.
979 : : Return -1 if size of TYPE1 < size of TYPE2
980 : : Return 1 if size of TYPE1 > size of TYPE2
981 : : Return 0 if types are of equal sizes or we can not compare them. */
982 : :
983 : : static int
984 : 7686594 : compare_type_sizes (tree type1, tree type2)
985 : : {
986 : : /* Be conservative for arrays and vectors. We want to support partial
987 : : overlap on int[3] and int[3] as tested in gcc.dg/torture/alias-2.c. */
988 : 7686594 : while (TREE_CODE (type1) == ARRAY_TYPE
989 : 8616998 : || VECTOR_TYPE_P (type1))
990 : 930404 : type1 = TREE_TYPE (type1);
991 : 7963738 : while (TREE_CODE (type2) == ARRAY_TYPE
992 : 7963738 : || VECTOR_TYPE_P (type2))
993 : 277144 : type2 = TREE_TYPE (type2);
994 : 7686594 : return compare_sizes (TYPE_SIZE (type1), TYPE_SIZE (type2));
995 : : }
996 : :
997 : : /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
998 : : purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
999 : : decide. */
1000 : :
1001 : : static inline int
1002 : 863122600 : same_type_for_tbaa (tree type1, tree type2)
1003 : : {
1004 : 863122600 : type1 = TYPE_MAIN_VARIANT (type1);
1005 : 863122600 : type2 = TYPE_MAIN_VARIANT (type2);
1006 : :
1007 : : /* Handle the most common case first. */
1008 : 863122600 : if (type1 == type2)
1009 : : return 1;
1010 : :
1011 : : /* If we would have to do structural comparison bail out. */
1012 : 135317778 : if (TYPE_STRUCTURAL_EQUALITY_P (type1)
1013 : 135317778 : || TYPE_STRUCTURAL_EQUALITY_P (type2))
1014 : : return -1;
1015 : :
1016 : : /* Compare the canonical types. */
1017 : 82333022 : if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
1018 : : return 1;
1019 : :
1020 : : /* ??? Array types are not properly unified in all cases as we have
1021 : : spurious changes in the index types for example. Removing this
1022 : : causes all sorts of problems with the Fortran frontend. */
1023 : 81622084 : if (TREE_CODE (type1) == ARRAY_TYPE
1024 : 4864395 : && TREE_CODE (type2) == ARRAY_TYPE)
1025 : : return -1;
1026 : :
1027 : : /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
1028 : : object of one of its constrained subtypes, e.g. when a function with an
1029 : : unconstrained parameter passed by reference is called on an object and
1030 : : inlined. But, even in the case of a fixed size, type and subtypes are
1031 : : not equivalent enough as to share the same TYPE_CANONICAL, since this
1032 : : would mean that conversions between them are useless, whereas they are
1033 : : not (e.g. type and subtypes can have different modes). So, in the end,
1034 : : they are only guaranteed to have the same alias set. */
1035 : 81232176 : alias_set_type set1 = get_alias_set (type1);
1036 : 81232176 : alias_set_type set2 = get_alias_set (type2);
1037 : 81232176 : if (set1 == set2)
1038 : : return -1;
1039 : :
1040 : : /* Pointers to void are considered compatible with all other pointers,
1041 : : so for two pointers see what the alias set resolution thinks. */
1042 : 46354923 : if (POINTER_TYPE_P (type1)
1043 : 13440683 : && POINTER_TYPE_P (type2)
1044 : 46531742 : && alias_sets_conflict_p (set1, set2))
1045 : : return -1;
1046 : :
1047 : : /* The types are known to be not equal. */
1048 : : return 0;
1049 : : }
1050 : :
1051 : : /* Return true if TYPE is a composite type (i.e. we may apply one of handled
1052 : : components on it). */
1053 : :
1054 : : static bool
1055 : 1970317 : type_has_components_p (tree type)
1056 : : {
1057 : 1970317 : return AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)
1058 : 1970317 : || TREE_CODE (type) == COMPLEX_TYPE;
1059 : : }
1060 : :
1061 : : /* MATCH1 and MATCH2 which are part of access path of REF1 and REF2
1062 : : respectively are either pointing to same address or are completely
1063 : : disjoint. If PARTIAL_OVERLAP is true, assume that outermost arrays may
1064 : : just partly overlap.
1065 : :
1066 : : Try to disambiguate using the access path starting from the match
1067 : : and return false if there is no conflict.
1068 : :
1069 : : Helper for aliasing_component_refs_p. */
1070 : :
1071 : : static bool
1072 : 905663 : aliasing_matching_component_refs_p (tree match1, tree ref1,
1073 : : poly_int64 offset1, poly_int64 max_size1,
1074 : : tree match2, tree ref2,
1075 : : poly_int64 offset2, poly_int64 max_size2,
1076 : : bool partial_overlap)
1077 : : {
1078 : 905663 : poly_int64 offadj, sztmp, msztmp;
1079 : 905663 : bool reverse;
1080 : :
1081 : 905663 : if (!partial_overlap)
1082 : : {
1083 : 905659 : get_ref_base_and_extent (match2, &offadj, &sztmp, &msztmp, &reverse);
1084 : 905659 : offset2 -= offadj;
1085 : 905659 : get_ref_base_and_extent (match1, &offadj, &sztmp, &msztmp, &reverse);
1086 : 905659 : offset1 -= offadj;
1087 : 905659 : if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1088 : : {
1089 : 80059 : ++alias_stats.aliasing_component_refs_p_no_alias;
1090 : 80059 : return false;
1091 : : }
1092 : : }
1093 : :
1094 : 825604 : int cmp = nonoverlapping_refs_since_match_p (match1, ref1, match2, ref2,
1095 : : partial_overlap);
1096 : 825604 : if (cmp == 1
1097 : 825604 : || (cmp == -1 && nonoverlapping_component_refs_p (ref1, ref2)))
1098 : : {
1099 : 890 : ++alias_stats.aliasing_component_refs_p_no_alias;
1100 : 890 : return false;
1101 : : }
1102 : 824714 : ++alias_stats.aliasing_component_refs_p_may_alias;
1103 : 824714 : return true;
1104 : : }
1105 : :
1106 : : /* Return true if REF is reference to zero sized trailing array. I.e.
1107 : : struct foo {int bar; int array[0];} *fooptr;
1108 : : fooptr->array. */
1109 : :
1110 : : static bool
1111 : 6324368 : component_ref_to_zero_sized_trailing_array_p (tree ref)
1112 : : {
1113 : 6324368 : return (TREE_CODE (ref) == COMPONENT_REF
1114 : 5676069 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE
1115 : 118824 : && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))
1116 : 37591 : || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))))
1117 : 6405678 : && array_ref_flexible_size_p (ref));
1118 : : }
1119 : :
1120 : : /* Worker for aliasing_component_refs_p. Most parameters match parameters of
1121 : : aliasing_component_refs_p.
1122 : :
1123 : : Walk access path REF2 and try to find type matching TYPE1
1124 : : (which is a start of possibly aliasing access path REF1).
1125 : : If match is found, try to disambiguate.
1126 : :
1127 : : Return 0 for sucessful disambiguation.
1128 : : Return 1 if match was found but disambiguation failed
1129 : : Return -1 if there is no match.
1130 : : In this case MAYBE_MATCH is set to 0 if there is no type matching TYPE1
1131 : : in access patch REF2 and -1 if we are not sure. */
1132 : :
1133 : : static int
1134 : 2629087 : aliasing_component_refs_walk (tree ref1, tree type1, tree base1,
1135 : : poly_int64 offset1, poly_int64 max_size1,
1136 : : tree end_struct_ref1,
1137 : : tree ref2, tree base2,
1138 : : poly_int64 offset2, poly_int64 max_size2,
1139 : : bool *maybe_match)
1140 : : {
1141 : 2629087 : tree ref = ref2;
1142 : 2629087 : int same_p = 0;
1143 : :
1144 : 7636555 : while (true)
1145 : : {
1146 : : /* We walk from inner type to the outer types. If type we see is
1147 : : already too large to be part of type1, terminate the search. */
1148 : 5132821 : int cmp = compare_type_sizes (type1, TREE_TYPE (ref));
1149 : :
1150 : 5132821 : if (cmp < 0
1151 : 5132821 : && (!end_struct_ref1
1152 : 60 : || compare_type_sizes (TREE_TYPE (end_struct_ref1),
1153 : 60 : TREE_TYPE (ref)) < 0))
1154 : : break;
1155 : : /* If types may be of same size, see if we can decide about their
1156 : : equality. */
1157 : 3804525 : if (cmp == 0)
1158 : : {
1159 : 2726171 : same_p = same_type_for_tbaa (TREE_TYPE (ref), type1);
1160 : 2726171 : if (same_p == 1)
1161 : : break;
1162 : : /* In case we can't decide whether types are same try to
1163 : : continue looking for the exact match.
1164 : : Remember however that we possibly saw a match
1165 : : to bypass the access path continuations tests we do later. */
1166 : 1820508 : if (same_p == -1)
1167 : 699087 : *maybe_match = true;
1168 : : }
1169 : 2898862 : if (!handled_component_p (ref))
1170 : : break;
1171 : 2503734 : ref = TREE_OPERAND (ref, 0);
1172 : 2503734 : }
1173 : 2629087 : if (same_p == 1)
1174 : : {
1175 : 905663 : bool partial_overlap = false;
1176 : :
1177 : : /* We assume that arrays can overlap by multiple of their elements
1178 : : size as tested in gcc.dg/torture/alias-2.c.
1179 : : This partial overlap happen only when both arrays are bases of
1180 : : the access and not contained within another component ref.
1181 : : To be safe we also assume partial overlap for VLAs. */
1182 : 905663 : if (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
1183 : 905663 : && (!TYPE_SIZE (TREE_TYPE (base1))
1184 : 4416 : || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) != INTEGER_CST
1185 : 4416 : || ref == base2))
1186 : : {
1187 : : /* Setting maybe_match to true triggers
1188 : : nonoverlapping_component_refs_p test later that still may do
1189 : : useful disambiguation. */
1190 : 4 : *maybe_match = true;
1191 : 4 : partial_overlap = true;
1192 : : }
1193 : 905663 : return aliasing_matching_component_refs_p (base1, ref1,
1194 : : offset1, max_size1,
1195 : : ref, ref2,
1196 : : offset2, max_size2,
1197 : 905663 : partial_overlap);
1198 : : }
1199 : : return -1;
1200 : : }
1201 : :
1202 : : /* Consider access path1 base1....ref1 and access path2 base2...ref2.
1203 : : Return true if they can be composed to single access path
1204 : : base1...ref1...base2...ref2.
1205 : :
1206 : : REF_TYPE1 if type of REF1. END_STRUCT_PAST_END1 is true if there is
1207 : : a trailing array access after REF1 in the non-TBAA part of the access.
1208 : : REF1_ALIAS_SET is the alias set of REF1.
1209 : :
1210 : : BASE_TYPE2 is type of base2. END_STRUCT_REF2 is non-NULL if there is
1211 : : a trailing array access in the TBAA part of access path2.
1212 : : BASE2_ALIAS_SET is the alias set of base2. */
1213 : :
1214 : : bool
1215 : 1970317 : access_path_may_continue_p (tree ref_type1, bool end_struct_past_end1,
1216 : : alias_set_type ref1_alias_set,
1217 : : tree base_type2, tree end_struct_ref2,
1218 : : alias_set_type base2_alias_set)
1219 : : {
1220 : : /* Access path can not continue past types with no components. */
1221 : 1970317 : if (!type_has_components_p (ref_type1))
1222 : : return false;
1223 : :
1224 : : /* If first access path ends by too small type to hold base of
1225 : : the second access path, typically paths can not continue.
1226 : :
1227 : : Punt if end_struct_past_end1 is true. We want to support arbitrary
1228 : : type puning past first COMPONENT_REF to union because redundant store
1229 : : elimination depends on this, see PR92152. For this reason we can not
1230 : : check size of the reference because types may partially overlap. */
1231 : 182465 : if (!end_struct_past_end1)
1232 : : {
1233 : 182416 : if (compare_type_sizes (ref_type1, base_type2) < 0)
1234 : : return false;
1235 : : /* If the path2 contains trailing array access we can strenghten the check
1236 : : to verify that also the size of element of the trailing array fits.
1237 : : In fact we could check for offset + type_size, but we do not track
1238 : : offsets and this is quite side case. */
1239 : 164397 : if (end_struct_ref2
1240 : 164397 : && compare_type_sizes (ref_type1, TREE_TYPE (end_struct_ref2)) < 0)
1241 : : return false;
1242 : : }
1243 : 164446 : return (base2_alias_set == ref1_alias_set
1244 : 164446 : || alias_set_subset_of (base2_alias_set, ref1_alias_set));
1245 : : }
1246 : :
1247 : : /* Determine if the two component references REF1 and REF2 which are
1248 : : based on access types TYPE1 and TYPE2 and of which at least one is based
1249 : : on an indirect reference may alias.
1250 : : REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
1251 : : are the respective alias sets. */
1252 : :
1253 : : static bool
1254 : 2371236 : aliasing_component_refs_p (tree ref1,
1255 : : alias_set_type ref1_alias_set,
1256 : : alias_set_type base1_alias_set,
1257 : : poly_int64 offset1, poly_int64 max_size1,
1258 : : tree ref2,
1259 : : alias_set_type ref2_alias_set,
1260 : : alias_set_type base2_alias_set,
1261 : : poly_int64 offset2, poly_int64 max_size2)
1262 : : {
1263 : : /* If one reference is a component references through pointers try to find a
1264 : : common base and apply offset based disambiguation. This handles
1265 : : for example
1266 : : struct A { int i; int j; } *q;
1267 : : struct B { struct A a; int k; } *p;
1268 : : disambiguating q->i and p->a.j. */
1269 : 2371236 : tree base1, base2;
1270 : 2371236 : tree type1, type2;
1271 : 2371236 : bool maybe_match = false;
1272 : 2371236 : tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
1273 : 2371236 : bool end_struct_past_end1 = false;
1274 : 2371236 : bool end_struct_past_end2 = false;
1275 : :
1276 : : /* Choose bases and base types to search for.
1277 : : The access path is as follows:
1278 : : base....end_of_tbaa_ref...actual_ref
1279 : : At one place in the access path may be a reference to zero sized or
1280 : : trailing array.
1281 : :
1282 : : We generally discard the segment after end_of_tbaa_ref however
1283 : : we need to be careful in case it contains zero sized or trailing array.
1284 : : These may happen after reference to union and in this case we need to
1285 : : not disambiguate type puning scenarios.
1286 : :
1287 : : We set:
1288 : : base1 to point to base
1289 : :
1290 : : ref1 to point to end_of_tbaa_ref
1291 : :
1292 : : end_struct_ref1 to point the trailing reference (if it exists
1293 : : in range base....end_of_tbaa_ref
1294 : :
1295 : : end_struct_past_end1 is true if this trailing reference occurs in
1296 : : end_of_tbaa_ref...actual_ref. */
1297 : 2371236 : base1 = ref1;
1298 : 5150794 : while (handled_component_p (base1))
1299 : : {
1300 : : /* Generally access paths are monotous in the size of object. The
1301 : : exception are trailing arrays of structures. I.e.
1302 : : struct a {int array[0];};
1303 : : or
1304 : : struct a {int array1[0]; int array[];};
1305 : : Such struct has size 0 but accesses to a.array may have non-zero size.
1306 : : In this case the size of TREE_TYPE (base1) is smaller than
1307 : : size of TREE_TYPE (TREE_OPERAND (base1, 0)).
1308 : :
1309 : : Because we compare sizes of arrays just by sizes of their elements,
1310 : : we only need to care about zero sized array fields here. */
1311 : 2779558 : if (component_ref_to_zero_sized_trailing_array_p (base1))
1312 : : {
1313 : 75337 : gcc_checking_assert (!end_struct_ref1);
1314 : : end_struct_ref1 = base1;
1315 : : }
1316 : 2779558 : if (ends_tbaa_access_path_p (base1))
1317 : : {
1318 : 27520 : ref1 = TREE_OPERAND (base1, 0);
1319 : 27520 : if (end_struct_ref1)
1320 : : {
1321 : 1 : end_struct_past_end1 = true;
1322 : 1 : end_struct_ref1 = NULL;
1323 : : }
1324 : : }
1325 : 2779558 : base1 = TREE_OPERAND (base1, 0);
1326 : : }
1327 : 2371236 : type1 = TREE_TYPE (base1);
1328 : 2371236 : base2 = ref2;
1329 : 5521034 : while (handled_component_p (base2))
1330 : : {
1331 : 3149798 : if (component_ref_to_zero_sized_trailing_array_p (base2))
1332 : : {
1333 : 5456 : gcc_checking_assert (!end_struct_ref2);
1334 : : end_struct_ref2 = base2;
1335 : : }
1336 : 3149798 : if (ends_tbaa_access_path_p (base2))
1337 : : {
1338 : 71151 : ref2 = TREE_OPERAND (base2, 0);
1339 : 71151 : if (end_struct_ref2)
1340 : : {
1341 : 48 : end_struct_past_end2 = true;
1342 : 48 : end_struct_ref2 = NULL;
1343 : : }
1344 : : }
1345 : 3149798 : base2 = TREE_OPERAND (base2, 0);
1346 : : }
1347 : 2371236 : type2 = TREE_TYPE (base2);
1348 : :
1349 : : /* Now search for the type1 in the access path of ref2. This
1350 : : would be a common base for doing offset based disambiguation on.
1351 : : This however only makes sense if type2 is big enough to hold type1. */
1352 : 2371236 : int cmp_outer = compare_type_sizes (type2, type1);
1353 : :
1354 : : /* If type2 is big enough to contain type1 walk its access path.
1355 : : We also need to care of arrays at the end of structs that may extend
1356 : : beyond the end of structure. If this occurs in the TBAA part of the
1357 : : access path, we need to consider the increased type as well. */
1358 : 2371236 : if (cmp_outer >= 0
1359 : 2371236 : || (end_struct_ref2
1360 : 1 : && compare_type_sizes (TREE_TYPE (end_struct_ref2), type1) >= 0))
1361 : : {
1362 : 1261048 : int res = aliasing_component_refs_walk (ref1, type1, base1,
1363 : : offset1, max_size1,
1364 : : end_struct_ref1,
1365 : : ref2, base2, offset2, max_size2,
1366 : : &maybe_match);
1367 : 1261048 : if (res != -1)
1368 : 579866 : return res;
1369 : : }
1370 : :
1371 : : /* If we didn't find a common base, try the other way around. */
1372 : 1791370 : if (cmp_outer <= 0
1373 : 1791370 : || (end_struct_ref1
1374 : 60 : && compare_type_sizes (TREE_TYPE (end_struct_ref1), type2) <= 0))
1375 : : {
1376 : 1368039 : int res = aliasing_component_refs_walk (ref2, type2, base2,
1377 : : offset2, max_size2,
1378 : : end_struct_ref2,
1379 : : ref1, base1, offset1, max_size1,
1380 : : &maybe_match);
1381 : 1368039 : if (res != -1)
1382 : 325797 : return res;
1383 : : }
1384 : :
1385 : : /* In the following code we make an assumption that the types in access
1386 : : paths do not overlap and thus accesses alias only if one path can be
1387 : : continuation of another. If we was not able to decide about equivalence,
1388 : : we need to give up. */
1389 : 1465573 : if (maybe_match)
1390 : : {
1391 : 465621 : if (!nonoverlapping_component_refs_p (ref1, ref2))
1392 : : {
1393 : 448460 : ++alias_stats.aliasing_component_refs_p_may_alias;
1394 : 448460 : return true;
1395 : : }
1396 : 17161 : ++alias_stats.aliasing_component_refs_p_no_alias;
1397 : 17161 : return false;
1398 : : }
1399 : :
1400 : 999952 : if (access_path_may_continue_p (TREE_TYPE (ref1), end_struct_past_end1,
1401 : : ref1_alias_set,
1402 : : type2, end_struct_ref2,
1403 : : base2_alias_set)
1404 : 999952 : || access_path_may_continue_p (TREE_TYPE (ref2), end_struct_past_end2,
1405 : : ref2_alias_set,
1406 : : type1, end_struct_ref1,
1407 : : base1_alias_set))
1408 : : {
1409 : 163774 : ++alias_stats.aliasing_component_refs_p_may_alias;
1410 : 163774 : return true;
1411 : : }
1412 : 836178 : ++alias_stats.aliasing_component_refs_p_no_alias;
1413 : 836178 : return false;
1414 : : }
1415 : :
1416 : : /* FIELD1 and FIELD2 are two fields of component refs. We assume
1417 : : that bases of both component refs are either equivalent or nonoverlapping.
1418 : : We do not assume that the containers of FIELD1 and FIELD2 are of the
1419 : : same type or size.
1420 : :
1421 : : Return 0 in case the base address of component_refs are same then
1422 : : FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1423 : : may not be of same type or size.
1424 : :
1425 : : Return 1 if FIELD1 and FIELD2 are non-overlapping.
1426 : :
1427 : : Return -1 otherwise.
1428 : :
1429 : : Main difference between 0 and -1 is to let
1430 : : nonoverlapping_component_refs_since_match_p discover the semantically
1431 : : equivalent part of the access path.
1432 : :
1433 : : Note that this function is used even with -fno-strict-aliasing
1434 : : and makes use of no TBAA assumptions. */
1435 : :
1436 : : static int
1437 : 3207592 : nonoverlapping_component_refs_p_1 (const_tree field1, const_tree field2)
1438 : : {
1439 : : /* If both fields are of the same type, we could save hard work of
1440 : : comparing offsets. */
1441 : 3207592 : tree type1 = DECL_CONTEXT (field1);
1442 : 3207592 : tree type2 = DECL_CONTEXT (field2);
1443 : :
1444 : 3207592 : if (TREE_CODE (type1) == RECORD_TYPE
1445 : 6289447 : && DECL_BIT_FIELD_REPRESENTATIVE (field1))
1446 : : field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
1447 : 3207592 : if (TREE_CODE (type2) == RECORD_TYPE
1448 : 6289447 : && DECL_BIT_FIELD_REPRESENTATIVE (field2))
1449 : : field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
1450 : :
1451 : : /* ??? Bitfields can overlap at RTL level so punt on them.
1452 : : FIXME: RTL expansion should be fixed by adjusting the access path
1453 : : when producing MEM_ATTRs for MEMs which are wider than
1454 : : the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1455 : 3207592 : if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1456 : : return -1;
1457 : :
1458 : : /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1459 : 3207592 : if (type1 == type2 && TREE_CODE (type1) == RECORD_TYPE)
1460 : 3060403 : return field1 != field2;
1461 : :
1462 : : /* In common case the offsets and bit offsets will be the same.
1463 : : However if frontends do not agree on the alignment, they may be
1464 : : different even if they actually represent same address.
1465 : : Try the common case first and if that fails calcualte the
1466 : : actual bit offset. */
1467 : 147189 : if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1),
1468 : 147189 : DECL_FIELD_OFFSET (field2))
1469 : 269719 : && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1),
1470 : 122530 : DECL_FIELD_BIT_OFFSET (field2)))
1471 : : return 0;
1472 : :
1473 : : /* Note that it may be possible to use component_ref_field_offset
1474 : : which would provide offsets as trees. However constructing and folding
1475 : : trees is expensive and does not seem to be worth the compile time
1476 : : cost. */
1477 : :
1478 : 25404 : poly_uint64 offset1, offset2;
1479 : 25404 : poly_uint64 bit_offset1, bit_offset2;
1480 : :
1481 : 25404 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field1), &offset1)
1482 : 25404 : && poly_int_tree_p (DECL_FIELD_OFFSET (field2), &offset2)
1483 : 25404 : && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1), &bit_offset1)
1484 : 50808 : && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2), &bit_offset2))
1485 : : {
1486 : 25404 : offset1 = (offset1 << LOG2_BITS_PER_UNIT) + bit_offset1;
1487 : 25404 : offset2 = (offset2 << LOG2_BITS_PER_UNIT) + bit_offset2;
1488 : :
1489 : 25404 : if (known_eq (offset1, offset2))
1490 : 21933 : return 0;
1491 : :
1492 : 25404 : poly_uint64 size1, size2;
1493 : :
1494 : 25404 : if (poly_int_tree_p (DECL_SIZE (field1), &size1)
1495 : 25404 : && poly_int_tree_p (DECL_SIZE (field2), &size2)
1496 : 50808 : && !ranges_maybe_overlap_p (offset1, size1, offset2, size2))
1497 : : return 1;
1498 : : }
1499 : : /* Resort to slower overlap checking by looking for matching types in
1500 : : the middle of access path. */
1501 : : return -1;
1502 : : }
1503 : :
1504 : : /* Return low bound of array. Do not produce new trees
1505 : : and thus do not care about particular type of integer constant
1506 : : and placeholder exprs. */
1507 : :
1508 : : static tree
1509 : 17158320 : cheap_array_ref_low_bound (tree ref)
1510 : : {
1511 : 17158320 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
1512 : :
1513 : : /* Avoid expensive array_ref_low_bound.
1514 : : low bound is either stored in operand2, or it is TYPE_MIN_VALUE of domain
1515 : : type or it is zero. */
1516 : 17158320 : if (TREE_OPERAND (ref, 2))
1517 : 119534 : return TREE_OPERAND (ref, 2);
1518 : 17038786 : else if (domain_type && TYPE_MIN_VALUE (domain_type))
1519 : 17031722 : return TYPE_MIN_VALUE (domain_type);
1520 : : else
1521 : 7064 : return integer_zero_node;
1522 : : }
1523 : :
1524 : : /* REF1 and REF2 are ARRAY_REFs with either same base address or which are
1525 : : completely disjoint.
1526 : :
1527 : : Return 1 if the refs are non-overlapping.
1528 : : Return 0 if they are possibly overlapping but if so the overlap again
1529 : : starts on the same address.
1530 : : Return -1 otherwise. */
1531 : :
1532 : : int
1533 : 8561714 : nonoverlapping_array_refs_p (tree ref1, tree ref2)
1534 : : {
1535 : 8561714 : tree index1 = TREE_OPERAND (ref1, 1);
1536 : 8561714 : tree index2 = TREE_OPERAND (ref2, 1);
1537 : 8561714 : tree low_bound1 = cheap_array_ref_low_bound (ref1);
1538 : 8561714 : tree low_bound2 = cheap_array_ref_low_bound (ref2);
1539 : :
1540 : : /* Handle zero offsets first: we do not need to match type size in this
1541 : : case. */
1542 : 8561714 : if (operand_equal_p (index1, low_bound1, 0)
1543 : 8561714 : && operand_equal_p (index2, low_bound2, 0))
1544 : : return 0;
1545 : :
1546 : : /* If type sizes are different, give up.
1547 : :
1548 : : Avoid expensive array_ref_element_size.
1549 : : If operand 3 is present it denotes size in the alignmnet units.
1550 : : Otherwise size is TYPE_SIZE of the element type.
1551 : : Handle only common cases where types are of the same "kind". */
1552 : 8491968 : if ((TREE_OPERAND (ref1, 3) == NULL) != (TREE_OPERAND (ref2, 3) == NULL))
1553 : : return -1;
1554 : :
1555 : 8491968 : tree elmt_type1 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref1, 0)));
1556 : 8491968 : tree elmt_type2 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref2, 0)));
1557 : :
1558 : 8491968 : if (TREE_OPERAND (ref1, 3))
1559 : : {
1560 : 4218 : if (TYPE_ALIGN (elmt_type1) != TYPE_ALIGN (elmt_type2)
1561 : 8436 : || !operand_equal_p (TREE_OPERAND (ref1, 3),
1562 : 4218 : TREE_OPERAND (ref2, 3), 0))
1563 : 646 : return -1;
1564 : : }
1565 : : else
1566 : : {
1567 : 8487750 : if (!operand_equal_p (TYPE_SIZE_UNIT (elmt_type1),
1568 : 8487750 : TYPE_SIZE_UNIT (elmt_type2), 0))
1569 : : return -1;
1570 : : }
1571 : :
1572 : : /* Since we know that type sizes are the same, there is no need to return
1573 : : -1 after this point. Partial overlap can not be introduced. */
1574 : :
1575 : : /* We may need to fold trees in this case.
1576 : : TODO: Handle integer constant case at least. */
1577 : 8483114 : if (!operand_equal_p (low_bound1, low_bound2, 0))
1578 : : return 0;
1579 : :
1580 : 8483114 : if (TREE_CODE (index1) == INTEGER_CST && TREE_CODE (index2) == INTEGER_CST)
1581 : : {
1582 : 301052 : if (tree_int_cst_equal (index1, index2))
1583 : : return 0;
1584 : : return 1;
1585 : : }
1586 : : /* TODO: We can use VRP to further disambiguate here. */
1587 : : return 0;
1588 : : }
1589 : :
1590 : : /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1591 : : MATCH2 either point to the same address or are disjoint.
1592 : : MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1593 : : respectively or NULL in the case we established equivalence of bases.
1594 : : If PARTIAL_OVERLAP is true assume that the toplevel arrays may actually
1595 : : overlap by exact multiply of their element size.
1596 : :
1597 : : This test works by matching the initial segment of the access path
1598 : : and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1599 : : match was determined without use of TBAA oracle.
1600 : :
1601 : : Return 1 if we can determine that component references REF1 and REF2,
1602 : : that are within a common DECL, cannot overlap.
1603 : :
1604 : : Return 0 if paths are same and thus there is nothing to disambiguate more
1605 : : (i.e. there is must alias assuming there is must alias between MATCH1 and
1606 : : MATCH2)
1607 : :
1608 : : Return -1 if we can not determine 0 or 1 - this happens when we met
1609 : : non-matching types was met in the path.
1610 : : In this case it may make sense to continue by other disambiguation
1611 : : oracles. */
1612 : :
1613 : : static int
1614 : 9036449 : nonoverlapping_refs_since_match_p (tree match1, tree ref1,
1615 : : tree match2, tree ref2,
1616 : : bool partial_overlap)
1617 : : {
1618 : 9036449 : int ntbaa1 = 0, ntbaa2 = 0;
1619 : : /* Early return if there are no references to match, we do not need
1620 : : to walk the access paths.
1621 : :
1622 : : Do not consider this as may-alias for stats - it is more useful
1623 : : to have information how many disambiguations happened provided that
1624 : : the query was meaningful. */
1625 : :
1626 : 8258385 : if (match1 == ref1 || !handled_component_p (ref1)
1627 : 17293377 : || match2 == ref2 || !handled_component_p (ref2))
1628 : : return -1;
1629 : :
1630 : 8238848 : auto_vec<tree, 16> component_refs1;
1631 : 8238848 : auto_vec<tree, 16> component_refs2;
1632 : :
1633 : : /* Create the stack of handled components for REF1. */
1634 : 20882851 : while (handled_component_p (ref1) && ref1 != match1)
1635 : : {
1636 : : /* We use TBAA only to re-synchronize after mismatched refs. So we
1637 : : do not need to truncate access path after TBAA part ends. */
1638 : 12644003 : if (ends_tbaa_access_path_p (ref1))
1639 : : ntbaa1 = 0;
1640 : : else
1641 : 12435566 : ntbaa1++;
1642 : 12644003 : component_refs1.safe_push (ref1);
1643 : 12644003 : ref1 = TREE_OPERAND (ref1, 0);
1644 : : }
1645 : :
1646 : : /* Create the stack of handled components for REF2. */
1647 : 20817888 : while (handled_component_p (ref2) && ref2 != match2)
1648 : : {
1649 : 12579040 : if (ends_tbaa_access_path_p (ref2))
1650 : : ntbaa2 = 0;
1651 : : else
1652 : 12348551 : ntbaa2++;
1653 : 12579040 : component_refs2.safe_push (ref2);
1654 : 12579040 : ref2 = TREE_OPERAND (ref2, 0);
1655 : : }
1656 : :
1657 : 8238848 : if (!flag_strict_aliasing)
1658 : : {
1659 : 507115 : ntbaa1 = 0;
1660 : 507115 : ntbaa2 = 0;
1661 : : }
1662 : :
1663 : 8238848 : bool mem_ref1 = TREE_CODE (ref1) == MEM_REF && ref1 != match1;
1664 : 8238848 : bool mem_ref2 = TREE_CODE (ref2) == MEM_REF && ref2 != match2;
1665 : :
1666 : : /* If only one of access path starts with MEM_REF check that offset is 0
1667 : : so the addresses stays the same after stripping it.
1668 : : TODO: In this case we may walk the other access path until we get same
1669 : : offset.
1670 : :
1671 : : If both starts with MEM_REF, offset has to be same. */
1672 : 77720 : if ((mem_ref1 && !mem_ref2 && !integer_zerop (TREE_OPERAND (ref1, 1)))
1673 : 8205872 : || (mem_ref2 && !mem_ref1 && !integer_zerop (TREE_OPERAND (ref2, 1)))
1674 : 16435677 : || (mem_ref1 && mem_ref2
1675 : 1053848 : && !tree_int_cst_equal (TREE_OPERAND (ref1, 1),
1676 : 1053848 : TREE_OPERAND (ref2, 1))))
1677 : : {
1678 : 73129 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1679 : 73129 : return -1;
1680 : : }
1681 : :
1682 : : /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1683 : : to handle them here at all. */
1684 : 8165719 : gcc_checking_assert (TREE_CODE (ref1) != TARGET_MEM_REF
1685 : : && TREE_CODE (ref2) != TARGET_MEM_REF);
1686 : :
1687 : : /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1688 : : rank. This is sufficient because we start from the same DECL and you
1689 : : cannot reference several fields at a time with COMPONENT_REFs (unlike
1690 : : with ARRAY_RANGE_REFs for arrays) so you always need the same number
1691 : : of them to access a sub-component, unless you're in a union, in which
1692 : : case the return value will precisely be false. */
1693 : 10320458 : while (true)
1694 : : {
1695 : : /* Track if we seen unmatched ref with non-zero offset. In this case
1696 : : we must look for partial overlaps. */
1697 : 10320458 : bool seen_unmatched_ref_p = false;
1698 : :
1699 : : /* First match ARRAY_REFs an try to disambiguate. */
1700 : 19959972 : if (!component_refs1.is_empty ()
1701 : 10055779 : && !component_refs2.is_empty ())
1702 : : {
1703 : 18558873 : unsigned int narray_refs1=0, narray_refs2=0;
1704 : :
1705 : : /* We generally assume that both access paths starts by same sequence
1706 : : of refs. However if number of array refs is not in sync, try
1707 : : to recover and pop elts until number match. This helps the case
1708 : : where one access path starts by array and other by element. */
1709 : 18558873 : for (narray_refs1 = 0; narray_refs1 < component_refs1.length ();
1710 : : narray_refs1++)
1711 : 12283036 : if (TREE_CODE (component_refs1 [component_refs1.length()
1712 : : - 1 - narray_refs1]) != ARRAY_REF)
1713 : : break;
1714 : :
1715 : 18561385 : for (narray_refs2 = 0; narray_refs2 < component_refs2.length ();
1716 : : narray_refs2++)
1717 : 12292374 : if (TREE_CODE (component_refs2 [component_refs2.length()
1718 : : - 1 - narray_refs2]) != ARRAY_REF)
1719 : : break;
1720 : 9614022 : for (; narray_refs1 > narray_refs2; narray_refs1--)
1721 : : {
1722 : 16190 : ref1 = component_refs1.pop ();
1723 : 16190 : ntbaa1--;
1724 : :
1725 : : /* If index is non-zero we need to check whether the reference
1726 : : does not break the main invariant that bases are either
1727 : : disjoint or equal. Consider the example:
1728 : :
1729 : : unsigned char out[][1];
1730 : : out[1]="a";
1731 : : out[i][0];
1732 : :
1733 : : Here bases out and out are same, but after removing the
1734 : : [i] index, this invariant no longer holds, because
1735 : : out[i] points to the middle of array out.
1736 : :
1737 : : TODO: If size of type of the skipped reference is an integer
1738 : : multiply of the size of type of the other reference this
1739 : : invariant can be verified, but even then it is not completely
1740 : : safe with !flag_strict_aliasing if the other reference contains
1741 : : unbounded array accesses.
1742 : : See */
1743 : :
1744 : 16190 : if (!operand_equal_p (TREE_OPERAND (ref1, 1),
1745 : 16190 : cheap_array_ref_low_bound (ref1), 0))
1746 : : return 0;
1747 : : }
1748 : 9597863 : for (; narray_refs2 > narray_refs1; narray_refs2--)
1749 : : {
1750 : 18702 : ref2 = component_refs2.pop ();
1751 : 18702 : ntbaa2--;
1752 : 18702 : if (!operand_equal_p (TREE_OPERAND (ref2, 1),
1753 : 18702 : cheap_array_ref_low_bound (ref2), 0))
1754 : : return 0;
1755 : : }
1756 : : /* Try to disambiguate matched arrays. */
1757 : 17909863 : for (unsigned int i = 0; i < narray_refs1; i++)
1758 : : {
1759 : 17123428 : int cmp = nonoverlapping_array_refs_p (component_refs1.pop (),
1760 : 8561714 : component_refs2.pop ());
1761 : 8561714 : ntbaa1--;
1762 : 8561714 : ntbaa2--;
1763 : 8561714 : if (cmp == 1 && !partial_overlap)
1764 : : {
1765 : 222158 : ++alias_stats
1766 : 222158 : .nonoverlapping_refs_since_match_p_no_alias;
1767 : 222158 : return 1;
1768 : : }
1769 : 8339556 : if (cmp == -1)
1770 : : {
1771 : 8854 : seen_unmatched_ref_p = true;
1772 : : /* We can not maintain the invariant that bases are either
1773 : : same or completely disjoint. However we can still recover
1774 : : from type based alias analysis if we reach references to
1775 : : same sizes. We do not attempt to match array sizes, so
1776 : : just finish array walking and look for component refs. */
1777 : 8854 : if (ntbaa1 < 0 || ntbaa2 < 0)
1778 : : {
1779 : 7989 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1780 : 7989 : return -1;
1781 : : }
1782 : 1804 : for (i++; i < narray_refs1; i++)
1783 : : {
1784 : 939 : component_refs1.pop ();
1785 : 939 : component_refs2.pop ();
1786 : 939 : ntbaa1--;
1787 : 939 : ntbaa2--;
1788 : : }
1789 : : break;
1790 : : }
1791 : 8330702 : partial_overlap = false;
1792 : : }
1793 : : }
1794 : :
1795 : : /* Next look for component_refs. */
1796 : 10126843 : do
1797 : : {
1798 : 10126843 : if (component_refs1.is_empty ())
1799 : : {
1800 : 6809102 : ++alias_stats
1801 : 6809102 : .nonoverlapping_refs_since_match_p_must_overlap;
1802 : 6809102 : return 0;
1803 : : }
1804 : 3317741 : ref1 = component_refs1.pop ();
1805 : 3317741 : ntbaa1--;
1806 : 3317741 : if (TREE_CODE (ref1) != COMPONENT_REF)
1807 : : {
1808 : 112523 : seen_unmatched_ref_p = true;
1809 : 112523 : if (ntbaa1 < 0 || ntbaa2 < 0)
1810 : : {
1811 : 41459 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1812 : 41459 : return -1;
1813 : : }
1814 : : }
1815 : : }
1816 : 6481500 : while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1817 : :
1818 : 3205218 : do
1819 : : {
1820 : 3205218 : if (component_refs2.is_empty ())
1821 : : {
1822 : 18407 : ++alias_stats
1823 : 18407 : .nonoverlapping_refs_since_match_p_must_overlap;
1824 : 18407 : return 0;
1825 : : }
1826 : 3186811 : ref2 = component_refs2.pop ();
1827 : 3186811 : ntbaa2--;
1828 : 3186811 : if (TREE_CODE (ref2) != COMPONENT_REF)
1829 : : {
1830 : 48 : if (ntbaa1 < 0 || ntbaa2 < 0)
1831 : : {
1832 : 48 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1833 : 48 : return -1;
1834 : : }
1835 : : seen_unmatched_ref_p = true;
1836 : : }
1837 : : }
1838 : 6373526 : while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1839 : :
1840 : : /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1841 : : earlier. */
1842 : 3186763 : gcc_checking_assert (TREE_CODE (ref1) == COMPONENT_REF
1843 : : && TREE_CODE (ref2) == COMPONENT_REF);
1844 : :
1845 : 3186763 : tree field1 = TREE_OPERAND (ref1, 1);
1846 : 3186763 : tree field2 = TREE_OPERAND (ref2, 1);
1847 : :
1848 : : /* ??? We cannot simply use the type of operand #0 of the refs here
1849 : : as the Fortran compiler smuggles type punning into COMPONENT_REFs
1850 : : for common blocks instead of using unions like everyone else. */
1851 : 3186763 : tree type1 = DECL_CONTEXT (field1);
1852 : 3186763 : tree type2 = DECL_CONTEXT (field2);
1853 : :
1854 : 3186763 : partial_overlap = false;
1855 : :
1856 : : /* If we skipped array refs on type of different sizes, we can
1857 : : no longer be sure that there are not partial overlaps. */
1858 : 432 : if (seen_unmatched_ref_p && ntbaa1 >= 0 && ntbaa2 >= 0
1859 : 3187195 : && !operand_equal_p (TYPE_SIZE (type1), TYPE_SIZE (type2), 0))
1860 : : {
1861 : 0 : ++alias_stats
1862 : 0 : .nonoverlapping_refs_since_match_p_may_alias;
1863 : 0 : return -1;
1864 : : }
1865 : :
1866 : 3186763 : int cmp = nonoverlapping_component_refs_p_1 (field1, field2);
1867 : 3186763 : if (cmp == -1)
1868 : : {
1869 : 3471 : ++alias_stats
1870 : 3471 : .nonoverlapping_refs_since_match_p_may_alias;
1871 : 3471 : return -1;
1872 : : }
1873 : 3183292 : else if (cmp == 1)
1874 : : {
1875 : 1028553 : ++alias_stats
1876 : 1028553 : .nonoverlapping_refs_since_match_p_no_alias;
1877 : 1028553 : return 1;
1878 : : }
1879 : : }
1880 : 8238848 : }
1881 : :
1882 : : /* Return TYPE_UID which can be used to match record types we consider
1883 : : same for TBAA purposes. */
1884 : :
1885 : : static inline int
1886 : 138502 : ncr_type_uid (const_tree field)
1887 : : {
1888 : : /* ??? We cannot simply use the type of operand #0 of the refs here
1889 : : as the Fortran compiler smuggles type punning into COMPONENT_REFs
1890 : : for common blocks instead of using unions like everyone else. */
1891 : 138502 : tree type = DECL_FIELD_CONTEXT (field);
1892 : : /* With LTO types considered same_type_for_tbaa_p
1893 : : from different translation unit may not have same
1894 : : main variant. They however have same TYPE_CANONICAL. */
1895 : 138502 : if (TYPE_CANONICAL (type))
1896 : 138502 : return TYPE_UID (TYPE_CANONICAL (type));
1897 : 0 : return TYPE_UID (type);
1898 : : }
1899 : :
1900 : : /* qsort compare function to sort FIELD_DECLs after their
1901 : : DECL_FIELD_CONTEXT TYPE_UID. */
1902 : :
1903 : : static inline int
1904 : 60377 : ncr_compar (const void *field1_, const void *field2_)
1905 : : {
1906 : 60377 : const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1907 : 60377 : const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1908 : 60377 : unsigned int uid1 = ncr_type_uid (field1);
1909 : 60377 : unsigned int uid2 = ncr_type_uid (field2);
1910 : :
1911 : 60377 : if (uid1 < uid2)
1912 : : return -1;
1913 : 23727 : else if (uid1 > uid2)
1914 : 23727 : return 1;
1915 : : return 0;
1916 : : }
1917 : :
1918 : : /* Return true if we can determine that the fields referenced cannot
1919 : : overlap for any pair of objects. This relies on TBAA. */
1920 : :
1921 : : static bool
1922 : 1260892 : nonoverlapping_component_refs_p (const_tree x, const_tree y)
1923 : : {
1924 : : /* Early return if we have nothing to do.
1925 : :
1926 : : Do not consider this as may-alias for stats - it is more useful
1927 : : to have information how many disambiguations happened provided that
1928 : : the query was meaningful. */
1929 : 1260892 : if (!flag_strict_aliasing
1930 : 1260892 : || !x || !y
1931 : 179650 : || !handled_component_p (x)
1932 : 1260892 : || !handled_component_p (y))
1933 : : return false;
1934 : :
1935 : 120984 : auto_vec<const_tree, 16> fieldsx;
1936 : 379302 : while (handled_component_p (x))
1937 : : {
1938 : 258318 : if (TREE_CODE (x) == COMPONENT_REF)
1939 : : {
1940 : 145718 : tree field = TREE_OPERAND (x, 1);
1941 : 145718 : tree type = DECL_FIELD_CONTEXT (field);
1942 : 145718 : if (TREE_CODE (type) == RECORD_TYPE)
1943 : 145430 : fieldsx.safe_push (field);
1944 : : }
1945 : 112600 : else if (ends_tbaa_access_path_p (x))
1946 : 2363 : fieldsx.truncate (0);
1947 : 258318 : x = TREE_OPERAND (x, 0);
1948 : : }
1949 : 208115 : if (fieldsx.length () == 0)
1950 : : return false;
1951 : 87131 : auto_vec<const_tree, 16> fieldsy;
1952 : 206737 : while (handled_component_p (y))
1953 : : {
1954 : 119606 : if (TREE_CODE (y) == COMPONENT_REF)
1955 : : {
1956 : 34135 : tree field = TREE_OPERAND (y, 1);
1957 : 34135 : tree type = DECL_FIELD_CONTEXT (field);
1958 : 34135 : if (TREE_CODE (type) == RECORD_TYPE)
1959 : 33847 : fieldsy.safe_push (TREE_OPERAND (y, 1));
1960 : : }
1961 : 85471 : else if (ends_tbaa_access_path_p (y))
1962 : 159 : fieldsy.truncate (0);
1963 : 119606 : y = TREE_OPERAND (y, 0);
1964 : : }
1965 : 87131 : if (fieldsy.length () == 0)
1966 : : {
1967 : 61481 : ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1968 : 61481 : return false;
1969 : : }
1970 : :
1971 : : /* Most common case first. */
1972 : 25650 : if (fieldsx.length () == 1
1973 : 25650 : && fieldsy.length () == 1)
1974 : : {
1975 : 44064 : if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx[0]),
1976 : 22032 : DECL_FIELD_CONTEXT (fieldsy[0])) == 1
1977 : 42667 : && nonoverlapping_component_refs_p_1 (fieldsx[0], fieldsy[0]) == 1)
1978 : : {
1979 : 17161 : ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1980 : 17161 : return true;
1981 : : }
1982 : : else
1983 : : {
1984 : 4871 : ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1985 : 4871 : return false;
1986 : : }
1987 : : }
1988 : :
1989 : 3618 : if (fieldsx.length () == 2)
1990 : : {
1991 : 352 : if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1992 : 138 : std::swap (fieldsx[0], fieldsx[1]);
1993 : : }
1994 : : else
1995 : 3266 : fieldsx.qsort (ncr_compar);
1996 : :
1997 : 3618 : if (fieldsy.length () == 2)
1998 : : {
1999 : 168 : if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
2000 : 108 : std::swap (fieldsy[0], fieldsy[1]);
2001 : : }
2002 : : else
2003 : 3450 : fieldsy.qsort (ncr_compar);
2004 : :
2005 : : unsigned i = 0, j = 0;
2006 : 8874 : do
2007 : : {
2008 : 8874 : const_tree fieldx = fieldsx[i];
2009 : 8874 : const_tree fieldy = fieldsy[j];
2010 : :
2011 : : /* We're left with accessing different fields of a structure,
2012 : : no possible overlap. */
2013 : 17748 : if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx),
2014 : 8874 : DECL_FIELD_CONTEXT (fieldy)) == 1
2015 : 8874 : && nonoverlapping_component_refs_p_1 (fieldx, fieldy) == 1)
2016 : : {
2017 : 0 : ++alias_stats.nonoverlapping_component_refs_p_no_alias;
2018 : 0 : return true;
2019 : : }
2020 : :
2021 : 8874 : if (ncr_type_uid (fieldx) < ncr_type_uid (fieldy))
2022 : : {
2023 : 3129 : i++;
2024 : 6258 : if (i == fieldsx.length ())
2025 : : break;
2026 : : }
2027 : : else
2028 : : {
2029 : 5745 : j++;
2030 : 11490 : if (j == fieldsy.length ())
2031 : : break;
2032 : : }
2033 : : }
2034 : : while (1);
2035 : :
2036 : 3618 : ++alias_stats.nonoverlapping_component_refs_p_may_alias;
2037 : 3618 : return false;
2038 : 208115 : }
2039 : :
2040 : :
2041 : : /* Return true if two memory references based on the variables BASE1
2042 : : and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2043 : : [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
2044 : : if non-NULL are the complete memory reference trees. */
2045 : :
2046 : : static bool
2047 : 1481862852 : decl_refs_may_alias_p (tree ref1, tree base1,
2048 : : poly_int64 offset1, poly_int64 max_size1,
2049 : : poly_int64 size1,
2050 : : tree ref2, tree base2,
2051 : : poly_int64 offset2, poly_int64 max_size2,
2052 : : poly_int64 size2)
2053 : : {
2054 : 1481862852 : gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
2055 : :
2056 : : /* If both references are based on different variables, they cannot alias. */
2057 : 1481862852 : if (compare_base_decls (base1, base2) == 0)
2058 : : return false;
2059 : :
2060 : : /* If both references are based on the same variable, they cannot alias if
2061 : : the accesses do not overlap. */
2062 : 207305674 : if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
2063 : : return false;
2064 : :
2065 : : /* If there is must alias, there is no use disambiguating further. */
2066 : 62523514 : if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
2067 : : return true;
2068 : :
2069 : : /* For components with variable position, the above test isn't sufficient,
2070 : : so we disambiguate component references manually. */
2071 : 10371222 : if (ref1 && ref2
2072 : 8108809 : && handled_component_p (ref1) && handled_component_p (ref2)
2073 : 17564162 : && nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2, false) == 1)
2074 : : return false;
2075 : :
2076 : : return true;
2077 : : }
2078 : :
2079 : : /* Return true if access with BASE is view converted.
2080 : : Base must not be stripped from inner MEM_REF (&decl)
2081 : : which is done by ao_ref_base and thus one extra walk
2082 : : of handled components is needed. */
2083 : :
2084 : : bool
2085 : 1246253359 : view_converted_memref_p (tree base)
2086 : : {
2087 : 1246253359 : if (TREE_CODE (base) != MEM_REF && TREE_CODE (base) != TARGET_MEM_REF)
2088 : : return false;
2089 : 836091262 : return (same_type_for_tbaa (TREE_TYPE (base),
2090 : 836091262 : TREE_TYPE (TREE_TYPE (TREE_OPERAND (base, 1))))
2091 : 836091262 : != 1);
2092 : : }
2093 : :
2094 : : /* Return true if an indirect reference based on *PTR1 constrained
2095 : : to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
2096 : : constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
2097 : : the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2098 : : in which case they are computed on-demand. REF1 and REF2
2099 : : if non-NULL are the complete memory reference trees. */
2100 : :
2101 : : static bool
2102 : 301859267 : indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
2103 : : poly_int64 offset1, poly_int64 max_size1,
2104 : : poly_int64 size1,
2105 : : alias_set_type ref1_alias_set,
2106 : : alias_set_type base1_alias_set,
2107 : : tree ref2 ATTRIBUTE_UNUSED, tree base2,
2108 : : poly_int64 offset2, poly_int64 max_size2,
2109 : : poly_int64 size2,
2110 : : alias_set_type ref2_alias_set,
2111 : : alias_set_type base2_alias_set, bool tbaa_p)
2112 : : {
2113 : 301859267 : tree ptr1;
2114 : 301859267 : tree ptrtype1, dbase2;
2115 : :
2116 : 301859267 : gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
2117 : : || TREE_CODE (base1) == TARGET_MEM_REF)
2118 : : && DECL_P (base2));
2119 : :
2120 : 301859267 : ptr1 = TREE_OPERAND (base1, 0);
2121 : 301859267 : poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
2122 : :
2123 : : /* If only one reference is based on a variable, they cannot alias if
2124 : : the pointer access is beyond the extent of the variable access.
2125 : : (the pointer base cannot validly point to an offset less than zero
2126 : : of the variable).
2127 : : ??? IVOPTs creates bases that do not honor this restriction,
2128 : : so do not apply this optimization for TARGET_MEM_REFs. */
2129 : 301859267 : if (TREE_CODE (base1) != TARGET_MEM_REF
2130 : 301859267 : && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
2131 : 73700030 : return false;
2132 : :
2133 : : /* If the pointer based access is bigger than the variable they cannot
2134 : : alias. This is similar to the check below where we use TBAA to
2135 : : increase the size of the pointer based access based on the dynamic
2136 : : type of a containing object we can infer from it. */
2137 : 228159237 : poly_int64 dsize2;
2138 : 228159237 : if (known_size_p (size1)
2139 : 213723039 : && poly_int_tree_p (DECL_SIZE (base2), &dsize2)
2140 : 404015642 : && known_lt (dsize2, size1))
2141 : : return false;
2142 : :
2143 : : /* They also cannot alias if the pointer may not point to the decl. */
2144 : 208940761 : if (!ptr_deref_may_alias_decl_p (ptr1, base2))
2145 : : return false;
2146 : :
2147 : : /* Disambiguations that rely on strict aliasing rules follow. */
2148 : 32239959 : if (!flag_strict_aliasing || !tbaa_p)
2149 : : return true;
2150 : :
2151 : : /* If the alias set for a pointer access is zero all bets are off. */
2152 : 7005470 : if (base1_alias_set == 0 || base2_alias_set == 0)
2153 : : return true;
2154 : :
2155 : : /* When we are trying to disambiguate an access with a pointer dereference
2156 : : as base versus one with a decl as base we can use both the size
2157 : : of the decl and its dynamic type for extra disambiguation.
2158 : : ??? We do not know anything about the dynamic type of the decl
2159 : : other than that its alias-set contains base2_alias_set as a subset
2160 : : which does not help us here. */
2161 : : /* As we know nothing useful about the dynamic type of the decl just
2162 : : use the usual conflict check rather than a subset test.
2163 : : ??? We could introduce -fvery-strict-aliasing when the language
2164 : : does not allow decls to have a dynamic type that differs from their
2165 : : static type. Then we can check
2166 : : !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
2167 : 1616074 : if (base1_alias_set != base2_alias_set
2168 : 1616074 : && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2169 : : return false;
2170 : :
2171 : 1316795 : ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2172 : :
2173 : : /* If the size of the access relevant for TBAA through the pointer
2174 : : is bigger than the size of the decl we can't possibly access the
2175 : : decl via that pointer. */
2176 : 1316795 : if (/* ??? This in turn may run afoul when a decl of type T which is
2177 : : a member of union type U is accessed through a pointer to
2178 : : type U and sizeof T is smaller than sizeof U. */
2179 : 1316795 : TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
2180 : 1287067 : && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
2181 : 2603862 : && compare_sizes (DECL_SIZE (base2),
2182 : 1287067 : TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
2183 : : return false;
2184 : :
2185 : 1281063 : if (!ref2)
2186 : : return true;
2187 : :
2188 : : /* If the decl is accessed via a MEM_REF, reconstruct the base
2189 : : we can use for TBAA and an appropriately adjusted offset. */
2190 : : dbase2 = ref2;
2191 : 2061088 : while (handled_component_p (dbase2))
2192 : 851904 : dbase2 = TREE_OPERAND (dbase2, 0);
2193 : 1209184 : poly_int64 doffset1 = offset1;
2194 : 1209184 : poly_offset_int doffset2 = offset2;
2195 : 1209184 : if (TREE_CODE (dbase2) == MEM_REF
2196 : 1209184 : || TREE_CODE (dbase2) == TARGET_MEM_REF)
2197 : : {
2198 : 866846 : doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
2199 : 433423 : tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
2200 : : /* If second reference is view-converted, give up now. */
2201 : 433423 : if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
2202 : : return true;
2203 : : }
2204 : :
2205 : : /* If first reference is view-converted, give up now. */
2206 : 1108777 : if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
2207 : : return true;
2208 : :
2209 : : /* If both references are through the same type, they do not alias
2210 : : if the accesses do not overlap. This does extra disambiguation
2211 : : for mixed/pointer accesses but requires strict aliasing.
2212 : : For MEM_REFs we require that the component-ref offset we computed
2213 : : is relative to the start of the type which we ensure by
2214 : : comparing rvalue and access type and disregarding the constant
2215 : : pointer offset.
2216 : :
2217 : : But avoid treating variable length arrays as "objects", instead assume they
2218 : : can overlap by an exact multiple of their element size.
2219 : : See gcc.dg/torture/alias-2.c. */
2220 : 999637 : if (((TREE_CODE (base1) != TARGET_MEM_REF
2221 : 166524 : || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2222 : 966857 : && (TREE_CODE (dbase2) != TARGET_MEM_REF
2223 : 23391 : || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
2224 : 1943123 : && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
2225 : : {
2226 : 332037 : bool partial_overlap = (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
2227 : 332037 : && (TYPE_SIZE (TREE_TYPE (base1))
2228 : 2394 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1)))
2229 : 643620 : != INTEGER_CST));
2230 : 332037 : if (!partial_overlap
2231 : 332037 : && !ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
2232 : : return false;
2233 : 311583 : if (!ref1 || !ref2
2234 : : /* If there is must alias, there is no use disambiguating further. */
2235 : 311583 : || (!partial_overlap
2236 : 299454 : && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2237 : : return true;
2238 : 4387 : int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2239 : : partial_overlap);
2240 : 4387 : if (res == -1)
2241 : 4287 : return !nonoverlapping_component_refs_p (ref1, ref2);
2242 : 100 : return !res;
2243 : : }
2244 : :
2245 : : /* Do access-path based disambiguation. */
2246 : 667600 : if (ref1 && ref2
2247 : 1115954 : && (handled_component_p (ref1) || handled_component_p (ref2)))
2248 : 480929 : return aliasing_component_refs_p (ref1,
2249 : : ref1_alias_set, base1_alias_set,
2250 : : offset1, max_size1,
2251 : : ref2,
2252 : : ref2_alias_set, base2_alias_set,
2253 : 480929 : offset2, max_size2);
2254 : :
2255 : : return true;
2256 : : }
2257 : :
2258 : : /* Return true if two indirect references based on *PTR1
2259 : : and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2260 : : [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
2261 : : the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2262 : : in which case they are computed on-demand. REF1 and REF2
2263 : : if non-NULL are the complete memory reference trees. */
2264 : :
2265 : : static bool
2266 : 91964223 : indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
2267 : : poly_int64 offset1, poly_int64 max_size1,
2268 : : poly_int64 size1,
2269 : : alias_set_type ref1_alias_set,
2270 : : alias_set_type base1_alias_set,
2271 : : tree ref2 ATTRIBUTE_UNUSED, tree base2,
2272 : : poly_int64 offset2, poly_int64 max_size2,
2273 : : poly_int64 size2,
2274 : : alias_set_type ref2_alias_set,
2275 : : alias_set_type base2_alias_set, bool tbaa_p)
2276 : : {
2277 : 91964223 : tree ptr1;
2278 : 91964223 : tree ptr2;
2279 : 91964223 : tree ptrtype1, ptrtype2;
2280 : :
2281 : 91964223 : gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
2282 : : || TREE_CODE (base1) == TARGET_MEM_REF)
2283 : : && (TREE_CODE (base2) == MEM_REF
2284 : : || TREE_CODE (base2) == TARGET_MEM_REF));
2285 : :
2286 : 91964223 : ptr1 = TREE_OPERAND (base1, 0);
2287 : 91964223 : ptr2 = TREE_OPERAND (base2, 0);
2288 : :
2289 : : /* If both bases are based on pointers they cannot alias if they may not
2290 : : point to the same memory object or if they point to the same object
2291 : : and the accesses do not overlap. */
2292 : 91964223 : if ((!cfun || gimple_in_ssa_p (cfun))
2293 : 58416039 : && operand_equal_p (ptr1, ptr2, 0)
2294 : 123285265 : && (((TREE_CODE (base1) != TARGET_MEM_REF
2295 : 1138892 : || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2296 : 31232518 : && (TREE_CODE (base2) != TARGET_MEM_REF
2297 : 1031879 : || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
2298 : 106446 : || (TREE_CODE (base1) == TARGET_MEM_REF
2299 : 92185 : && TREE_CODE (base2) == TARGET_MEM_REF
2300 : 84466 : && (TMR_STEP (base1) == TMR_STEP (base2)
2301 : 13303 : || (TMR_STEP (base1) && TMR_STEP (base2)
2302 : 2810 : && operand_equal_p (TMR_STEP (base1),
2303 : 2810 : TMR_STEP (base2), 0)))
2304 : 71163 : && (TMR_INDEX (base1) == TMR_INDEX (base2)
2305 : 11695 : || (TMR_INDEX (base1) && TMR_INDEX (base2)
2306 : 10223 : && operand_equal_p (TMR_INDEX (base1),
2307 : 10223 : TMR_INDEX (base2), 0)))
2308 : 59468 : && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
2309 : 0 : || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
2310 : 0 : && operand_equal_p (TMR_INDEX2 (base1),
2311 : 0 : TMR_INDEX2 (base2), 0))))))
2312 : : {
2313 : 31274064 : poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
2314 : 31274064 : poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
2315 : 31274064 : if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
2316 : 31274064 : offset2 + moff2, max_size2))
2317 : 31008508 : return false;
2318 : : /* If there is must alias, there is no use disambiguating further. */
2319 : 4601471 : if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
2320 : : return true;
2321 : 1075206 : if (ref1 && ref2)
2322 : : {
2323 : 820260 : int res = nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2,
2324 : : false);
2325 : 820260 : if (res != -1)
2326 : 809650 : return !res;
2327 : : }
2328 : : }
2329 : 60955715 : if (!ptr_derefs_may_alias_p (ptr1, ptr2))
2330 : : return false;
2331 : :
2332 : : /* Disambiguations that rely on strict aliasing rules follow. */
2333 : 37880635 : if (!flag_strict_aliasing || !tbaa_p)
2334 : : return true;
2335 : :
2336 : 14479033 : ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2337 : 14479033 : ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
2338 : :
2339 : : /* If the alias set for a pointer access is zero all bets are off. */
2340 : 14479033 : if (base1_alias_set == 0
2341 : 14479033 : || base2_alias_set == 0)
2342 : : return true;
2343 : :
2344 : : /* Do type-based disambiguation. */
2345 : 9656866 : if (base1_alias_set != base2_alias_set
2346 : 9656866 : && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2347 : : return false;
2348 : :
2349 : : /* If either reference is view-converted, give up now. */
2350 : 8952809 : if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
2351 : 8952809 : || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
2352 : 2675899 : return true;
2353 : :
2354 : : /* If both references are through the same type, they do not alias
2355 : : if the accesses do not overlap. This does extra disambiguation
2356 : : for mixed/pointer accesses but requires strict aliasing. */
2357 : 6276910 : if ((TREE_CODE (base1) != TARGET_MEM_REF
2358 : 1140475 : || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2359 : 5962973 : && (TREE_CODE (base2) != TARGET_MEM_REF
2360 : 971854 : || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
2361 : 12111948 : && same_type_for_tbaa (TREE_TYPE (ptrtype1),
2362 : 5835038 : TREE_TYPE (ptrtype2)) == 1)
2363 : : {
2364 : : /* But avoid treating arrays as "objects", instead assume they
2365 : : can overlap by an exact multiple of their element size.
2366 : : See gcc.dg/torture/alias-2.c. */
2367 : 3724760 : bool partial_overlap = TREE_CODE (TREE_TYPE (ptrtype1)) == ARRAY_TYPE;
2368 : :
2369 : 3724760 : if (!partial_overlap
2370 : 3724760 : && !ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
2371 : : return false;
2372 : 3384490 : if (!ref1 || !ref2
2373 : 3384490 : || (!partial_overlap
2374 : 2829924 : && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2375 : : return true;
2376 : 193258 : int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2377 : : partial_overlap);
2378 : 193258 : if (res == -1)
2379 : 70744 : return !nonoverlapping_component_refs_p (ref1, ref2);
2380 : 122514 : return !res;
2381 : : }
2382 : :
2383 : : /* Do access-path based disambiguation. */
2384 : 2552150 : if (ref1 && ref2
2385 : 3653156 : && (handled_component_p (ref1) || handled_component_p (ref2)))
2386 : 1890307 : return aliasing_component_refs_p (ref1,
2387 : : ref1_alias_set, base1_alias_set,
2388 : : offset1, max_size1,
2389 : : ref2,
2390 : : ref2_alias_set, base2_alias_set,
2391 : 1890307 : offset2, max_size2);
2392 : :
2393 : : return true;
2394 : : }
2395 : :
2396 : : /* Return true, if the two memory references REF1 and REF2 may alias. */
2397 : :
2398 : : static bool
2399 : 1957014456 : refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2400 : : {
2401 : 1957014456 : tree base1, base2;
2402 : 1957014456 : poly_int64 offset1 = 0, offset2 = 0;
2403 : 1957014456 : poly_int64 max_size1 = -1, max_size2 = -1;
2404 : 1957014456 : bool var1_p, var2_p, ind1_p, ind2_p;
2405 : :
2406 : 1957014456 : gcc_checking_assert ((!ref1->ref
2407 : : || TREE_CODE (ref1->ref) == SSA_NAME
2408 : : || DECL_P (ref1->ref)
2409 : : || TREE_CODE (ref1->ref) == STRING_CST
2410 : : || handled_component_p (ref1->ref)
2411 : : || TREE_CODE (ref1->ref) == MEM_REF
2412 : : || TREE_CODE (ref1->ref) == TARGET_MEM_REF
2413 : : || TREE_CODE (ref1->ref) == WITH_SIZE_EXPR)
2414 : : && (!ref2->ref
2415 : : || TREE_CODE (ref2->ref) == SSA_NAME
2416 : : || DECL_P (ref2->ref)
2417 : : || TREE_CODE (ref2->ref) == STRING_CST
2418 : : || handled_component_p (ref2->ref)
2419 : : || TREE_CODE (ref2->ref) == MEM_REF
2420 : : || TREE_CODE (ref2->ref) == TARGET_MEM_REF
2421 : : || TREE_CODE (ref2->ref) == WITH_SIZE_EXPR));
2422 : :
2423 : : /* Decompose the references into their base objects and the access. */
2424 : 1957014456 : base1 = ao_ref_base (ref1);
2425 : 1957014456 : offset1 = ref1->offset;
2426 : 1957014456 : max_size1 = ref1->max_size;
2427 : 1957014456 : base2 = ao_ref_base (ref2);
2428 : 1957014456 : offset2 = ref2->offset;
2429 : 1957014456 : max_size2 = ref2->max_size;
2430 : :
2431 : : /* We can end up with registers or constants as bases for example from
2432 : : *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
2433 : : which is seen as a struct copy. */
2434 : 1957014456 : if (TREE_CODE (base1) == SSA_NAME
2435 : 1957013380 : || TREE_CODE (base1) == CONST_DECL
2436 : 1955088525 : || TREE_CODE (base1) == CONSTRUCTOR
2437 : 1955088525 : || TREE_CODE (base1) == ADDR_EXPR
2438 : 1955088525 : || CONSTANT_CLASS_P (base1)
2439 : 1948557695 : || TREE_CODE (base2) == SSA_NAME
2440 : 1948557695 : || TREE_CODE (base2) == CONST_DECL
2441 : 1948464663 : || TREE_CODE (base2) == CONSTRUCTOR
2442 : 1948464663 : || TREE_CODE (base2) == ADDR_EXPR
2443 : 1948464663 : || CONSTANT_CLASS_P (base2))
2444 : : return false;
2445 : :
2446 : : /* Two volatile accesses always conflict. */
2447 : 1948416874 : if (ref1->volatile_p
2448 : 5878853 : && ref2->volatile_p)
2449 : : return true;
2450 : :
2451 : : /* refN->ref may convey size information, do not confuse our workers
2452 : : with that but strip it - ao_ref_base took it into account already. */
2453 : 1944795755 : tree ref1ref = ref1->ref;
2454 : 1944795755 : if (ref1ref && TREE_CODE (ref1ref) == WITH_SIZE_EXPR)
2455 : 162 : ref1ref = TREE_OPERAND (ref1ref, 0);
2456 : 1944795755 : tree ref2ref = ref2->ref;
2457 : 1944795755 : if (ref2ref && TREE_CODE (ref2ref) == WITH_SIZE_EXPR)
2458 : 0 : ref2ref = TREE_OPERAND (ref2ref, 0);
2459 : :
2460 : : /* Defer to simple offset based disambiguation if we have
2461 : : references based on two decls. Do this before defering to
2462 : : TBAA to handle must-alias cases in conformance with the
2463 : : GCC extension of allowing type-punning through unions. */
2464 : 1944795755 : var1_p = DECL_P (base1);
2465 : 1944795755 : var2_p = DECL_P (base2);
2466 : 1944795755 : if (var1_p && var2_p)
2467 : 1481862852 : return decl_refs_may_alias_p (ref1ref, base1, offset1, max_size1,
2468 : : ref1->size,
2469 : : ref2ref, base2, offset2, max_size2,
2470 : 1481862852 : ref2->size);
2471 : :
2472 : : /* We can end up referring to code via function and label decls.
2473 : : As we likely do not properly track code aliases conservatively
2474 : : bail out. */
2475 : 462932903 : if (TREE_CODE (base1) == FUNCTION_DECL
2476 : 462932903 : || TREE_CODE (base1) == LABEL_DECL
2477 : 461978677 : || TREE_CODE (base2) == FUNCTION_DECL
2478 : 461965390 : || TREE_CODE (base2) == LABEL_DECL)
2479 : : return true;
2480 : :
2481 : : /* Handle restrict based accesses.
2482 : : ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
2483 : : here. */
2484 : 461965390 : tree rbase1 = base1;
2485 : 461965390 : tree rbase2 = base2;
2486 : 461965390 : if (var1_p)
2487 : : {
2488 : 226395992 : rbase1 = ref1ref;
2489 : 226395992 : if (rbase1)
2490 : 297430224 : while (handled_component_p (rbase1))
2491 : 109551121 : rbase1 = TREE_OPERAND (rbase1, 0);
2492 : : }
2493 : 461965390 : if (var2_p)
2494 : : {
2495 : 119100804 : rbase2 = ref2ref;
2496 : 119100804 : if (rbase2)
2497 : 193605175 : while (handled_component_p (rbase2))
2498 : 82077153 : rbase2 = TREE_OPERAND (rbase2, 0);
2499 : : }
2500 : 461965390 : if (rbase1 && rbase2
2501 : 415875719 : && (TREE_CODE (rbase1) == MEM_REF || TREE_CODE (rbase1) == TARGET_MEM_REF)
2502 : 261063671 : && (TREE_CODE (rbase2) == MEM_REF || TREE_CODE (rbase2) == TARGET_MEM_REF)
2503 : : /* If the accesses are in the same restrict clique... */
2504 : 184135280 : && MR_DEPENDENCE_CLIQUE (rbase1) == MR_DEPENDENCE_CLIQUE (rbase2)
2505 : : /* But based on different pointers they do not alias. */
2506 : 612916744 : && MR_DEPENDENCE_BASE (rbase1) != MR_DEPENDENCE_BASE (rbase2))
2507 : : return false;
2508 : :
2509 : 445485183 : ind1_p = (TREE_CODE (base1) == MEM_REF
2510 : 445485183 : || TREE_CODE (base1) == TARGET_MEM_REF);
2511 : 445485183 : ind2_p = (TREE_CODE (base2) == MEM_REF
2512 : 445485183 : || TREE_CODE (base2) == TARGET_MEM_REF);
2513 : :
2514 : : /* Canonicalize the pointer-vs-decl case. */
2515 : 445485183 : if (ind1_p && var2_p)
2516 : : {
2517 : 117398905 : std::swap (offset1, offset2);
2518 : 117398905 : std::swap (max_size1, max_size2);
2519 : 117398905 : std::swap (base1, base2);
2520 : 117398905 : std::swap (ref1, ref2);
2521 : 117398905 : std::swap (ref1ref, ref2ref);
2522 : 117398905 : var1_p = true;
2523 : 117398905 : ind1_p = false;
2524 : 117398905 : var2_p = false;
2525 : 117398905 : ind2_p = true;
2526 : : }
2527 : :
2528 : : /* First defer to TBAA if possible. */
2529 : 445485183 : if (tbaa_p
2530 : 204119198 : && flag_strict_aliasing
2531 : 579969717 : && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
2532 : : ao_ref_alias_set (ref2)))
2533 : : return false;
2534 : :
2535 : : /* If the reference is based on a pointer that points to memory
2536 : : that may not be written to then the other reference cannot possibly
2537 : : clobber it. */
2538 : 394331089 : if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
2539 : 393151013 : && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
2540 : 787099902 : || (ind1_p
2541 : 92089622 : && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
2542 : 91919900 : && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
2543 : : return false;
2544 : :
2545 : : /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2546 : 393823490 : if (var1_p && ind2_p)
2547 : 301859267 : return indirect_ref_may_alias_decl_p (ref2ref, base2,
2548 : : offset2, max_size2, ref2->size,
2549 : : ao_ref_alias_set (ref2),
2550 : : ao_ref_base_alias_set (ref2),
2551 : : ref1ref, base1,
2552 : : offset1, max_size1, ref1->size,
2553 : : ao_ref_alias_set (ref1),
2554 : : ao_ref_base_alias_set (ref1),
2555 : 301859267 : tbaa_p);
2556 : 91964223 : else if (ind1_p && ind2_p)
2557 : 91964223 : return indirect_refs_may_alias_p (ref1ref, base1,
2558 : : offset1, max_size1, ref1->size,
2559 : : ao_ref_alias_set (ref1),
2560 : : ao_ref_base_alias_set (ref1),
2561 : : ref2ref, base2,
2562 : : offset2, max_size2, ref2->size,
2563 : : ao_ref_alias_set (ref2),
2564 : : ao_ref_base_alias_set (ref2),
2565 : 91964223 : tbaa_p);
2566 : :
2567 : 0 : gcc_unreachable ();
2568 : : }
2569 : :
2570 : : /* Return true, if the two memory references REF1 and REF2 may alias
2571 : : and update statistics. */
2572 : :
2573 : : bool
2574 : 1957014456 : refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2575 : : {
2576 : 1957014456 : bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
2577 : 1957014456 : if (res)
2578 : 137984754 : ++alias_stats.refs_may_alias_p_may_alias;
2579 : : else
2580 : 1819029702 : ++alias_stats.refs_may_alias_p_no_alias;
2581 : 1957014456 : return res;
2582 : : }
2583 : :
2584 : : static bool
2585 : 60381460 : refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
2586 : : {
2587 : 60381460 : ao_ref r1;
2588 : 60381460 : ao_ref_init (&r1, ref1);
2589 : 60381460 : return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
2590 : : }
2591 : :
2592 : : bool
2593 : 1098035 : refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
2594 : : {
2595 : 1098035 : ao_ref r1, r2;
2596 : 1098035 : ao_ref_init (&r1, ref1);
2597 : 1098035 : ao_ref_init (&r2, ref2);
2598 : 1098035 : return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
2599 : : }
2600 : :
2601 : : /* Returns true if there is a anti-dependence for the STORE that
2602 : : executes after the LOAD. */
2603 : :
2604 : : bool
2605 : 483144 : refs_anti_dependent_p (tree load, tree store)
2606 : : {
2607 : 483144 : ao_ref r1, r2;
2608 : 483144 : ao_ref_init (&r1, load);
2609 : 483144 : ao_ref_init (&r2, store);
2610 : 483144 : return refs_may_alias_p_1 (&r1, &r2, false);
2611 : : }
2612 : :
2613 : : /* Returns true if there is a output dependence for the stores
2614 : : STORE1 and STORE2. */
2615 : :
2616 : : bool
2617 : 2996569 : refs_output_dependent_p (tree store1, tree store2)
2618 : : {
2619 : 2996569 : ao_ref r1, r2;
2620 : 2996569 : ao_ref_init (&r1, store1);
2621 : 2996569 : ao_ref_init (&r2, store2);
2622 : 2996569 : return refs_may_alias_p_1 (&r1, &r2, false);
2623 : : }
2624 : :
2625 : : /* Returns true if and only if REF may alias any access stored in TT.
2626 : : IF TBAA_P is true, use TBAA oracle. */
2627 : :
2628 : : static bool
2629 : 43191894 : modref_may_conflict (const gcall *stmt,
2630 : : modref_tree <alias_set_type> *tt, ao_ref *ref, bool tbaa_p)
2631 : : {
2632 : 43191894 : alias_set_type base_set, ref_set;
2633 : 43191894 : bool global_memory_ok = false;
2634 : :
2635 : 43191894 : if (tt->every_base)
2636 : : return true;
2637 : :
2638 : 8635580 : if (!dbg_cnt (ipa_mod_ref))
2639 : : return true;
2640 : :
2641 : 8635580 : base_set = ao_ref_base_alias_set (ref);
2642 : :
2643 : 8635580 : ref_set = ao_ref_alias_set (ref);
2644 : :
2645 : 8635580 : int num_tests = 0, max_tests = param_modref_max_tests;
2646 : 35875483 : for (auto base_node : tt->bases)
2647 : : {
2648 : 13965015 : if (tbaa_p && flag_strict_aliasing)
2649 : : {
2650 : 10912041 : if (num_tests >= max_tests)
2651 : : return true;
2652 : 10912041 : alias_stats.modref_tests++;
2653 : 10912041 : if (!alias_sets_conflict_p (base_set, base_node->base))
2654 : 3790290 : continue;
2655 : 7121751 : num_tests++;
2656 : : }
2657 : :
2658 : 10174725 : if (base_node->every_ref)
2659 : : return true;
2660 : :
2661 : 38699388 : for (auto ref_node : base_node->refs)
2662 : : {
2663 : : /* Do not repeat same test as before. */
2664 : 11791891 : if ((ref_set != base_set || base_node->base != ref_node->ref)
2665 : 6858806 : && tbaa_p && flag_strict_aliasing)
2666 : : {
2667 : 4519821 : if (num_tests >= max_tests)
2668 : : return true;
2669 : 4488549 : alias_stats.modref_tests++;
2670 : 4488549 : if (!alias_sets_conflict_p (ref_set, ref_node->ref))
2671 : 1088241 : continue;
2672 : 3400308 : num_tests++;
2673 : : }
2674 : :
2675 : 10672378 : if (ref_node->every_access)
2676 : : return true;
2677 : :
2678 : : /* TBAA checks did not disambiguate, try individual accesses. */
2679 : 34929149 : for (auto access_node : ref_node->accesses)
2680 : : {
2681 : 9902848 : if (num_tests >= max_tests)
2682 : 1870394 : return true;
2683 : :
2684 : 9902830 : if (access_node.parm_index == MODREF_GLOBAL_MEMORY_PARM)
2685 : : {
2686 : 1968196 : if (global_memory_ok)
2687 : 1107596 : continue;
2688 : 1968196 : if (ref_may_alias_global_p (ref, true))
2689 : : return true;
2690 : 1084198 : global_memory_ok = true;
2691 : 1084198 : num_tests++;
2692 : 1084198 : continue;
2693 : : }
2694 : :
2695 : 7934634 : tree arg = access_node.get_call_arg (stmt);
2696 : 7934634 : if (!arg)
2697 : : return true;
2698 : :
2699 : 7933592 : alias_stats.modref_baseptr_tests++;
2700 : :
2701 : 7933592 : if (integer_zerop (arg) && flag_delete_null_pointer_checks)
2702 : 23398 : continue;
2703 : :
2704 : : /* PTA oracle will be unhapy of arg is not an pointer. */
2705 : 7910194 : if (!POINTER_TYPE_P (TREE_TYPE (arg)))
2706 : : return true;
2707 : :
2708 : : /* If we don't have base pointer, give up. */
2709 : 7910194 : if (!ref->ref && !ref->base)
2710 : 0 : continue;
2711 : :
2712 : 7910194 : ao_ref ref2;
2713 : 7910194 : if (access_node.get_ao_ref (stmt, &ref2))
2714 : : {
2715 : 6167634 : ref2.ref_alias_set = ref_node->ref;
2716 : 6167634 : ref2.base_alias_set = base_node->base;
2717 : 6167634 : if (refs_may_alias_p_1 (&ref2, ref, tbaa_p))
2718 : : return true;
2719 : : }
2720 : 1742560 : else if (ptr_deref_may_alias_ref_p_1 (arg, ref))
2721 : : return true;
2722 : :
2723 : 6924858 : num_tests++;
2724 : : }
2725 : : }
2726 : : }
2727 : : return false;
2728 : : }
2729 : :
2730 : : /* Check if REF conflicts with call using "fn spec" attribute.
2731 : : If CLOBBER is true we are checking for writes, otherwise check loads.
2732 : :
2733 : : Return 0 if there are no conflicts (except for possible function call
2734 : : argument reads), 1 if there are conflicts and -1 if we can not decide by
2735 : : fn spec. */
2736 : :
2737 : : static int
2738 : 147635547 : check_fnspec (gcall *call, ao_ref *ref, bool clobber)
2739 : : {
2740 : 147635547 : attr_fnspec fnspec = gimple_call_fnspec (call);
2741 : 147635547 : if (fnspec.known_p ())
2742 : : {
2743 : 72146456 : if (clobber
2744 : 72146456 : ? !fnspec.global_memory_written_p ()
2745 : 8430089 : : !fnspec.global_memory_read_p ())
2746 : : {
2747 : 85564496 : for (unsigned int i = 0; i < gimple_call_num_args (call); i++)
2748 : 83272744 : if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, i)))
2749 : 59342307 : && (!fnspec.arg_specified_p (i)
2750 : 35354740 : || (clobber ? fnspec.arg_maybe_written_p (i)
2751 : 3731926 : : fnspec.arg_maybe_read_p (i))))
2752 : : {
2753 : 21886189 : ao_ref dref;
2754 : 21886189 : tree size = NULL_TREE;
2755 : 21886189 : unsigned int size_arg;
2756 : :
2757 : 21886189 : if (!fnspec.arg_specified_p (i))
2758 : : ;
2759 : 21883114 : else if (fnspec.arg_max_access_size_given_by_arg_p
2760 : 21883114 : (i, &size_arg))
2761 : 14848153 : size = gimple_call_arg (call, size_arg);
2762 : 7034961 : else if (fnspec.arg_access_size_given_by_type_p (i))
2763 : : {
2764 : 42793 : tree callee = gimple_call_fndecl (call);
2765 : 42793 : tree t = TYPE_ARG_TYPES (TREE_TYPE (callee));
2766 : :
2767 : 86596 : for (unsigned int p = 0; p < i; p++)
2768 : 43803 : t = TREE_CHAIN (t);
2769 : 42793 : size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_VALUE (t)));
2770 : : }
2771 : 14890946 : poly_int64 size_hwi;
2772 : 14890946 : if (size
2773 : 14890946 : && poly_int_tree_p (size, &size_hwi)
2774 : 27425042 : && coeffs_in_range_p (size_hwi, 0,
2775 : : HOST_WIDE_INT_MAX / BITS_PER_UNIT))
2776 : : {
2777 : 12532560 : size_hwi = size_hwi * BITS_PER_UNIT;
2778 : 12532560 : ao_ref_init_from_ptr_and_range (&dref,
2779 : : gimple_call_arg (call, i),
2780 : : true, 0, -1, size_hwi);
2781 : : }
2782 : : else
2783 : 9353629 : ao_ref_init_from_ptr_and_range (&dref,
2784 : : gimple_call_arg (call, i),
2785 : : false, 0, -1, -1);
2786 : 21886189 : if (refs_may_alias_p_1 (&dref, ref, false))
2787 : 4618802 : return 1;
2788 : : }
2789 : 26276244 : if (clobber
2790 : 22789050 : && fnspec.errno_maybe_written_p ()
2791 : 6449530 : && flag_errno_math
2792 : 27345929 : && targetm.ref_may_alias_errno (ref))
2793 : : return 1;
2794 : 26260723 : return 0;
2795 : : }
2796 : : }
2797 : :
2798 : : /* FIXME: we should handle barriers more consistently, but for now leave the
2799 : : check here. */
2800 : 116740501 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2801 : 6232807 : switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
2802 : : {
2803 : : /* __sync_* builtins and some OpenMP builtins act as threading
2804 : : barriers. */
2805 : : #undef DEF_SYNC_BUILTIN
2806 : : #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2807 : : #include "sync-builtins.def"
2808 : : #undef DEF_SYNC_BUILTIN
2809 : : case BUILT_IN_GOMP_ATOMIC_START:
2810 : : case BUILT_IN_GOMP_ATOMIC_END:
2811 : : case BUILT_IN_GOMP_BARRIER:
2812 : : case BUILT_IN_GOMP_BARRIER_CANCEL:
2813 : : case BUILT_IN_GOMP_TASKWAIT:
2814 : : case BUILT_IN_GOMP_TASKGROUP_END:
2815 : : case BUILT_IN_GOMP_CRITICAL_START:
2816 : : case BUILT_IN_GOMP_CRITICAL_END:
2817 : : case BUILT_IN_GOMP_CRITICAL_NAME_START:
2818 : : case BUILT_IN_GOMP_CRITICAL_NAME_END:
2819 : : case BUILT_IN_GOMP_LOOP_END:
2820 : : case BUILT_IN_GOMP_LOOP_END_CANCEL:
2821 : : case BUILT_IN_GOMP_ORDERED_START:
2822 : : case BUILT_IN_GOMP_ORDERED_END:
2823 : : case BUILT_IN_GOMP_SECTIONS_END:
2824 : : case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2825 : : case BUILT_IN_GOMP_SINGLE_COPY_START:
2826 : : case BUILT_IN_GOMP_SINGLE_COPY_END:
2827 : : return 1;
2828 : :
2829 : : default:
2830 : : return -1;
2831 : : }
2832 : : return -1;
2833 : : }
2834 : :
2835 : : /* If the call CALL may use the memory reference REF return true,
2836 : : otherwise return false. */
2837 : :
2838 : : static bool
2839 : 44328752 : ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
2840 : : {
2841 : 44328752 : tree base, callee;
2842 : 44328752 : unsigned i;
2843 : 44328752 : int flags = gimple_call_flags (call);
2844 : :
2845 : 44328752 : if (flags & (ECF_CONST|ECF_NOVOPS))
2846 : 31987 : goto process_args;
2847 : :
2848 : : /* A call that is not without side-effects might involve volatile
2849 : : accesses and thus conflicts with all other volatile accesses. */
2850 : 44296765 : if (ref->volatile_p)
2851 : : return true;
2852 : :
2853 : 44296563 : if (gimple_call_internal_p (call))
2854 : 58085 : switch (gimple_call_internal_fn (call))
2855 : : {
2856 : : case IFN_MASK_STORE:
2857 : : case IFN_SCATTER_STORE:
2858 : : case IFN_MASK_SCATTER_STORE:
2859 : : case IFN_LEN_STORE:
2860 : : case IFN_MASK_LEN_STORE:
2861 : : return false;
2862 : 0 : case IFN_MASK_STORE_LANES:
2863 : 0 : case IFN_MASK_LEN_STORE_LANES:
2864 : 0 : goto process_args;
2865 : 766 : case IFN_MASK_LOAD:
2866 : 766 : case IFN_LEN_LOAD:
2867 : 766 : case IFN_MASK_LEN_LOAD:
2868 : 766 : case IFN_MASK_LOAD_LANES:
2869 : 766 : case IFN_MASK_LEN_LOAD_LANES:
2870 : 766 : {
2871 : 766 : ao_ref rhs_ref;
2872 : 766 : tree lhs = gimple_call_lhs (call);
2873 : 766 : if (lhs)
2874 : : {
2875 : 766 : ao_ref_init_from_ptr_and_size (&rhs_ref,
2876 : : gimple_call_arg (call, 0),
2877 : 766 : TYPE_SIZE_UNIT (TREE_TYPE (lhs)));
2878 : : /* We cannot make this a known-size access since otherwise
2879 : : we disambiguate against refs to decls that are smaller. */
2880 : 766 : rhs_ref.size = -1;
2881 : 1532 : rhs_ref.ref_alias_set = rhs_ref.base_alias_set
2882 : 1532 : = tbaa_p ? get_deref_alias_set (TREE_TYPE
2883 : : (gimple_call_arg (call, 1))) : 0;
2884 : 766 : return refs_may_alias_p_1 (ref, &rhs_ref, tbaa_p);
2885 : : }
2886 : 0 : break;
2887 : : }
2888 : : default:;
2889 : : }
2890 : :
2891 : 44290364 : callee = gimple_call_fndecl (call);
2892 : 44290364 : if (callee != NULL_TREE)
2893 : : {
2894 : 42795304 : struct cgraph_node *node = cgraph_node::get (callee);
2895 : : /* We can not safely optimize based on summary of calle if it does
2896 : : not always bind to current def: it is possible that memory load
2897 : : was optimized out earlier and the interposed variant may not be
2898 : : optimized this way. */
2899 : 42795304 : if (node && node->binds_to_current_def_p ())
2900 : : {
2901 : 5203799 : modref_summary *summary = get_modref_function_summary (node);
2902 : 5203799 : if (summary && !summary->calls_interposable)
2903 : : {
2904 : 2701220 : if (!modref_may_conflict (call, summary->loads, ref, tbaa_p))
2905 : : {
2906 : 563914 : alias_stats.modref_use_no_alias++;
2907 : 563914 : if (dump_file && (dump_flags & TDF_DETAILS))
2908 : : {
2909 : 24 : fprintf (dump_file,
2910 : : "ipa-modref: call stmt ");
2911 : 24 : print_gimple_stmt (dump_file, call, 0);
2912 : 24 : fprintf (dump_file,
2913 : : "ipa-modref: call to %s does not use ",
2914 : : node->dump_name ());
2915 : 24 : if (!ref->ref && ref->base)
2916 : : {
2917 : 3 : fprintf (dump_file, "base: ");
2918 : 3 : print_generic_expr (dump_file, ref->base);
2919 : : }
2920 : 21 : else if (ref->ref)
2921 : : {
2922 : 21 : fprintf (dump_file, "ref: ");
2923 : 21 : print_generic_expr (dump_file, ref->ref);
2924 : : }
2925 : 24 : fprintf (dump_file, " alias sets: %i->%i\n",
2926 : : ao_ref_base_alias_set (ref),
2927 : : ao_ref_alias_set (ref));
2928 : : }
2929 : 563914 : goto process_args;
2930 : : }
2931 : 2137306 : alias_stats.modref_use_may_alias++;
2932 : : }
2933 : : }
2934 : : }
2935 : :
2936 : 43726450 : base = ao_ref_base (ref);
2937 : 43726450 : if (!base)
2938 : : return true;
2939 : :
2940 : : /* If the reference is based on a decl that is not aliased the call
2941 : : cannot possibly use it. */
2942 : 43726450 : if (DECL_P (base)
2943 : 38984248 : && !may_be_aliased (base)
2944 : : /* But local statics can be used through recursion. */
2945 : 64842898 : && !is_global_var (base))
2946 : 20851408 : goto process_args;
2947 : :
2948 : 22875042 : if (int res = check_fnspec (call, ref, false))
2949 : : {
2950 : 19387848 : if (res == 1)
2951 : : return true;
2952 : : }
2953 : : else
2954 : 3487194 : goto process_args;
2955 : :
2956 : : /* Check if base is a global static variable that is not read
2957 : : by the function. */
2958 : 18906828 : if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2959 : : {
2960 : 991374 : struct cgraph_node *node = cgraph_node::get (callee);
2961 : 991374 : bitmap read;
2962 : 991374 : int id;
2963 : :
2964 : : /* FIXME: Callee can be an OMP builtin that does not have a call graph
2965 : : node yet. We should enforce that there are nodes for all decls in the
2966 : : IL and remove this check instead. */
2967 : 991374 : if (node
2968 : 990744 : && (id = ipa_reference_var_uid (base)) != -1
2969 : 131919 : && (read = ipa_reference_get_read_global (node))
2970 : 1006421 : && !bitmap_bit_p (read, id))
2971 : 11294 : goto process_args;
2972 : : }
2973 : :
2974 : : /* Check if the base variable is call-used. */
2975 : 18895534 : if (DECL_P (base))
2976 : : {
2977 : 15327716 : if (pt_solution_includes (gimple_call_use_set (call), base))
2978 : : return true;
2979 : : }
2980 : 3567818 : else if ((TREE_CODE (base) == MEM_REF
2981 : 3567818 : || TREE_CODE (base) == TARGET_MEM_REF)
2982 : 3567818 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2983 : : {
2984 : 3567585 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2985 : 3567585 : if (!pi)
2986 : : return true;
2987 : :
2988 : 3557177 : if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
2989 : : return true;
2990 : : }
2991 : : else
2992 : : return true;
2993 : :
2994 : : /* Inspect call arguments for passed-by-value aliases. */
2995 : : process_args:
2996 : 81281330 : for (i = 0; i < gimple_call_num_args (call); ++i)
2997 : : {
2998 : 57202402 : tree op = gimple_call_arg (call, i);
2999 : 57202402 : int flags = gimple_call_arg_flags (call, i);
3000 : :
3001 : 57202402 : if (flags & (EAF_UNUSED | EAF_NO_DIRECT_READ))
3002 : 632708 : continue;
3003 : :
3004 : 56569694 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
3005 : 466 : op = TREE_OPERAND (op, 0);
3006 : :
3007 : 56569694 : if (TREE_CODE (op) != SSA_NAME
3008 : 56569694 : && !is_gimple_min_invariant (op))
3009 : : {
3010 : 10036973 : ao_ref r;
3011 : 10036973 : ao_ref_init (&r, op);
3012 : 10036973 : if (refs_may_alias_p_1 (&r, ref, tbaa_p))
3013 : 4120929 : return true;
3014 : : }
3015 : : }
3016 : :
3017 : : return false;
3018 : : }
3019 : :
3020 : : static bool
3021 : 44319554 : ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
3022 : : {
3023 : 44319554 : bool res;
3024 : 44319554 : res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
3025 : 44319554 : if (res)
3026 : 20238140 : ++alias_stats.ref_maybe_used_by_call_p_may_alias;
3027 : : else
3028 : 24081414 : ++alias_stats.ref_maybe_used_by_call_p_no_alias;
3029 : 44319554 : return res;
3030 : : }
3031 : :
3032 : :
3033 : : /* If the statement STMT may use the memory reference REF return
3034 : : true, otherwise return false. */
3035 : :
3036 : : bool
3037 : 348224830 : ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
3038 : : {
3039 : 348224830 : if (is_gimple_assign (stmt))
3040 : : {
3041 : 297770973 : tree rhs;
3042 : :
3043 : : /* All memory assign statements are single. */
3044 : 297770973 : if (!gimple_assign_single_p (stmt))
3045 : : return false;
3046 : :
3047 : 297770973 : rhs = gimple_assign_rhs1 (stmt);
3048 : 297770973 : if (is_gimple_reg (rhs)
3049 : 242938479 : || is_gimple_min_invariant (rhs)
3050 : 386716366 : || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
3051 : 239500872 : return false;
3052 : :
3053 : 58270101 : return refs_may_alias_p (rhs, ref, tbaa_p);
3054 : : }
3055 : 50453857 : else if (is_gimple_call (stmt))
3056 : 44319554 : return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
3057 : 6134303 : else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
3058 : : {
3059 : 5396946 : tree retval = gimple_return_retval (return_stmt);
3060 : 5396946 : if (retval
3061 : 2883311 : && TREE_CODE (retval) != SSA_NAME
3062 : 2272730 : && !is_gimple_min_invariant (retval)
3063 : 7508305 : && refs_may_alias_p (retval, ref, tbaa_p))
3064 : : return true;
3065 : : /* If ref escapes the function then the return acts as a use. */
3066 : 3795727 : tree base = ao_ref_base (ref);
3067 : 3795727 : if (!base)
3068 : : ;
3069 : 3795727 : else if (DECL_P (base))
3070 : 1346042 : return is_global_var (base);
3071 : 2449685 : else if (TREE_CODE (base) == MEM_REF
3072 : 2449685 : || TREE_CODE (base) == TARGET_MEM_REF)
3073 : 2449588 : return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0), false);
3074 : : return false;
3075 : : }
3076 : :
3077 : : return true;
3078 : : }
3079 : :
3080 : : bool
3081 : 783198 : ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
3082 : : {
3083 : 783198 : ao_ref r;
3084 : 783198 : ao_ref_init (&r, ref);
3085 : 783198 : return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
3086 : : }
3087 : :
3088 : : /* If the call in statement CALL may clobber the memory reference REF
3089 : : return true, otherwise return false. */
3090 : :
3091 : : bool
3092 : 329025701 : call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
3093 : : {
3094 : 329025701 : tree base;
3095 : 329025701 : tree callee;
3096 : :
3097 : : /* If the call is pure or const it cannot clobber anything. */
3098 : 329025701 : if (gimple_call_flags (call)
3099 : 329025701 : & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
3100 : : return false;
3101 : 326183709 : if (gimple_call_internal_p (call))
3102 : 581050 : switch (auto fn = gimple_call_internal_fn (call))
3103 : : {
3104 : : /* Treat these internal calls like ECF_PURE for aliasing,
3105 : : they don't write to any memory the program should care about.
3106 : : They have important other side-effects, and read memory,
3107 : : so can't be ECF_NOVOPS. */
3108 : : case IFN_UBSAN_NULL:
3109 : : case IFN_UBSAN_BOUNDS:
3110 : : case IFN_UBSAN_VPTR:
3111 : : case IFN_UBSAN_OBJECT_SIZE:
3112 : : case IFN_UBSAN_PTR:
3113 : : case IFN_ASAN_CHECK:
3114 : : return false;
3115 : 7258 : case IFN_MASK_STORE:
3116 : 7258 : case IFN_LEN_STORE:
3117 : 7258 : case IFN_MASK_LEN_STORE:
3118 : 7258 : case IFN_MASK_STORE_LANES:
3119 : 7258 : case IFN_MASK_LEN_STORE_LANES:
3120 : 7258 : {
3121 : 7258 : tree rhs = gimple_call_arg (call,
3122 : 7258 : internal_fn_stored_value_index (fn));
3123 : 7258 : ao_ref lhs_ref;
3124 : 7258 : ao_ref_init_from_ptr_and_size (&lhs_ref, gimple_call_arg (call, 0),
3125 : 7258 : TYPE_SIZE_UNIT (TREE_TYPE (rhs)));
3126 : : /* We cannot make this a known-size access since otherwise
3127 : : we disambiguate against refs to decls that are smaller. */
3128 : 7258 : lhs_ref.size = -1;
3129 : 14516 : lhs_ref.ref_alias_set = lhs_ref.base_alias_set
3130 : 7258 : = tbaa_p ? get_deref_alias_set
3131 : 6916 : (TREE_TYPE (gimple_call_arg (call, 1))) : 0;
3132 : 7258 : return refs_may_alias_p_1 (ref, &lhs_ref, tbaa_p);
3133 : : }
3134 : : default:
3135 : : break;
3136 : : }
3137 : :
3138 : 325932808 : callee = gimple_call_fndecl (call);
3139 : :
3140 : 325932808 : if (callee != NULL_TREE && !ref->volatile_p)
3141 : : {
3142 : 304803209 : struct cgraph_node *node = cgraph_node::get (callee);
3143 : 304803209 : if (node)
3144 : : {
3145 : 304517767 : modref_summary *summary = get_modref_function_summary (node);
3146 : 304517767 : if (summary)
3147 : : {
3148 : 40490674 : if (!modref_may_conflict (call, summary->stores, ref, tbaa_p)
3149 : 40490674 : && (!summary->writes_errno
3150 : 86052 : || !targetm.ref_may_alias_errno (ref)))
3151 : : {
3152 : 4460104 : alias_stats.modref_clobber_no_alias++;
3153 : 4460104 : if (dump_file && (dump_flags & TDF_DETAILS))
3154 : : {
3155 : 52 : fprintf (dump_file,
3156 : : "ipa-modref: call stmt ");
3157 : 52 : print_gimple_stmt (dump_file, call, 0);
3158 : 52 : fprintf (dump_file,
3159 : : "ipa-modref: call to %s does not clobber ",
3160 : : node->dump_name ());
3161 : 52 : if (!ref->ref && ref->base)
3162 : : {
3163 : 32 : fprintf (dump_file, "base: ");
3164 : 32 : print_generic_expr (dump_file, ref->base);
3165 : : }
3166 : 20 : else if (ref->ref)
3167 : : {
3168 : 20 : fprintf (dump_file, "ref: ");
3169 : 20 : print_generic_expr (dump_file, ref->ref);
3170 : : }
3171 : 52 : fprintf (dump_file, " alias sets: %i->%i\n",
3172 : : ao_ref_base_alias_set (ref),
3173 : : ao_ref_alias_set (ref));
3174 : : }
3175 : 4460104 : return false;
3176 : : }
3177 : 36030570 : alias_stats.modref_clobber_may_alias++;
3178 : : }
3179 : : }
3180 : : }
3181 : :
3182 : 321472704 : base = ao_ref_base (ref);
3183 : 321472704 : if (!base)
3184 : : return true;
3185 : :
3186 : 321472704 : if (TREE_CODE (base) == SSA_NAME
3187 : 321472314 : || CONSTANT_CLASS_P (base))
3188 : : return false;
3189 : :
3190 : : /* A call that is not without side-effects might involve volatile
3191 : : accesses and thus conflicts with all other volatile accesses. */
3192 : 313418727 : if (ref->volatile_p)
3193 : : return true;
3194 : :
3195 : : /* If the reference is based on a decl that is not aliased the call
3196 : : cannot possibly clobber it. */
3197 : 311952917 : if (DECL_P (base)
3198 : 282900972 : && !may_be_aliased (base)
3199 : : /* But local non-readonly statics can be modified through recursion
3200 : : or the call may implement a threading barrier which we must
3201 : : treat as may-def. */
3202 : 507249447 : && (TREE_READONLY (base)
3203 : 187428899 : || !is_global_var (base)))
3204 : : return false;
3205 : :
3206 : : /* If the reference is based on a pointer that points to memory
3207 : : that may not be written to then the call cannot possibly clobber it. */
3208 : 125104269 : if ((TREE_CODE (base) == MEM_REF
3209 : 125104269 : || TREE_CODE (base) == TARGET_MEM_REF)
3210 : 29051945 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3211 : 153874932 : && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
3212 : : return false;
3213 : :
3214 : 124760505 : if (int res = check_fnspec (call, ref, true))
3215 : : {
3216 : 101986976 : if (res == 1)
3217 : : return true;
3218 : : }
3219 : : else
3220 : : return false;
3221 : :
3222 : : /* Check if base is a global static variable that is not written
3223 : : by the function. */
3224 : 96432344 : if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
3225 : : {
3226 : 9656751 : struct cgraph_node *node = cgraph_node::get (callee);
3227 : 9656751 : bitmap written;
3228 : 9656751 : int id;
3229 : :
3230 : 9656751 : if (node
3231 : 9656295 : && (id = ipa_reference_var_uid (base)) != -1
3232 : 3909631 : && (written = ipa_reference_get_written_global (node))
3233 : 9729689 : && !bitmap_bit_p (written, id))
3234 : : return false;
3235 : : }
3236 : :
3237 : : /* Check if the base variable is call-clobbered. */
3238 : 96376015 : if (DECL_P (base))
3239 : 74318304 : return pt_solution_includes (gimple_call_clobber_set (call), base);
3240 : 22057711 : else if ((TREE_CODE (base) == MEM_REF
3241 : 22057711 : || TREE_CODE (base) == TARGET_MEM_REF)
3242 : 22057711 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
3243 : : {
3244 : 21826223 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
3245 : 21826223 : if (!pi)
3246 : : return true;
3247 : :
3248 : 20965209 : return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
3249 : : }
3250 : :
3251 : : return true;
3252 : : }
3253 : :
3254 : : /* If the call in statement CALL may clobber the memory reference REF
3255 : : return true, otherwise return false. */
3256 : :
3257 : : bool
3258 : 346227 : call_may_clobber_ref_p (gcall *call, tree ref, bool tbaa_p)
3259 : : {
3260 : 346227 : bool res;
3261 : 346227 : ao_ref r;
3262 : 346227 : ao_ref_init (&r, ref);
3263 : 346227 : res = call_may_clobber_ref_p_1 (call, &r, tbaa_p);
3264 : 346227 : if (res)
3265 : 59491 : ++alias_stats.call_may_clobber_ref_p_may_alias;
3266 : : else
3267 : 286736 : ++alias_stats.call_may_clobber_ref_p_no_alias;
3268 : 346227 : return res;
3269 : : }
3270 : :
3271 : :
3272 : : /* If the statement STMT may clobber the memory reference REF return true,
3273 : : otherwise return false. */
3274 : :
3275 : : bool
3276 : 1933041732 : stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
3277 : : {
3278 : 1933041732 : if (is_gimple_call (stmt))
3279 : : {
3280 : 334546608 : tree lhs = gimple_call_lhs (stmt);
3281 : 334546608 : if (lhs
3282 : 155976939 : && TREE_CODE (lhs) != SSA_NAME)
3283 : : {
3284 : 58976057 : ao_ref r;
3285 : 58976057 : ao_ref_init (&r, lhs);
3286 : 58976057 : if (refs_may_alias_p_1 (ref, &r, tbaa_p))
3287 : 5967156 : return true;
3288 : : }
3289 : :
3290 : 328579452 : return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref, tbaa_p);
3291 : : }
3292 : 1598495124 : else if (gimple_assign_single_p (stmt))
3293 : : {
3294 : 1590477570 : tree lhs = gimple_assign_lhs (stmt);
3295 : 1590477570 : if (TREE_CODE (lhs) != SSA_NAME)
3296 : : {
3297 : 1589577288 : ao_ref r;
3298 : 1589577288 : ao_ref_init (&r, lhs);
3299 : 1589577288 : return refs_may_alias_p_1 (ref, &r, tbaa_p);
3300 : : }
3301 : : }
3302 : 8017554 : else if (gimple_code (stmt) == GIMPLE_ASM)
3303 : : return true;
3304 : :
3305 : : return false;
3306 : : }
3307 : :
3308 : : bool
3309 : 4468743 : stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
3310 : : {
3311 : 4468743 : ao_ref r;
3312 : 4468743 : ao_ref_init (&r, ref);
3313 : 4468743 : return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
3314 : : }
3315 : :
3316 : : /* Return true if store1 and store2 described by corresponding tuples
3317 : : <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
3318 : : address. */
3319 : :
3320 : : static bool
3321 : 136996161 : same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
3322 : : poly_int64 max_size1,
3323 : : tree base2, poly_int64 offset2, poly_int64 size2,
3324 : : poly_int64 max_size2)
3325 : : {
3326 : : /* Offsets need to be 0. */
3327 : 136996161 : if (maybe_ne (offset1, 0)
3328 : 136996161 : || maybe_ne (offset2, 0))
3329 : : return false;
3330 : :
3331 : 39593096 : bool base1_obj_p = SSA_VAR_P (base1);
3332 : 39593096 : bool base2_obj_p = SSA_VAR_P (base2);
3333 : :
3334 : : /* We need one object. */
3335 : 39593096 : if (base1_obj_p == base2_obj_p)
3336 : : return false;
3337 : 4983739 : tree obj = base1_obj_p ? base1 : base2;
3338 : :
3339 : : /* And we need one MEM_REF. */
3340 : 4983739 : bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
3341 : 4983739 : bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
3342 : 4983739 : if (base1_memref_p == base2_memref_p)
3343 : : return false;
3344 : 4882374 : tree memref = base1_memref_p ? base1 : base2;
3345 : :
3346 : : /* Sizes need to be valid. */
3347 : 4882374 : if (!known_size_p (max_size1)
3348 : 4858008 : || !known_size_p (max_size2)
3349 : 4857661 : || !known_size_p (size1)
3350 : 9740035 : || !known_size_p (size2))
3351 : : return false;
3352 : :
3353 : : /* Max_size needs to match size. */
3354 : 4857661 : if (maybe_ne (max_size1, size1)
3355 : 4857661 : || maybe_ne (max_size2, size2))
3356 : : return false;
3357 : :
3358 : : /* Sizes need to match. */
3359 : 4813724 : if (maybe_ne (size1, size2))
3360 : : return false;
3361 : :
3362 : :
3363 : : /* Check that memref is a store to pointer with singleton points-to info. */
3364 : 1281936 : if (!integer_zerop (TREE_OPERAND (memref, 1)))
3365 : : return false;
3366 : 936559 : tree ptr = TREE_OPERAND (memref, 0);
3367 : 936559 : if (TREE_CODE (ptr) != SSA_NAME)
3368 : : return false;
3369 : 936243 : struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3370 : 936243 : unsigned int pt_uid;
3371 : 936243 : if (pi == NULL
3372 : 936243 : || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid))
3373 : 739738 : return false;
3374 : :
3375 : : /* Be conservative with non-call exceptions when the address might
3376 : : be NULL. */
3377 : 196505 : if (cfun->can_throw_non_call_exceptions && pi->pt.null)
3378 : : return false;
3379 : :
3380 : : /* Check that ptr points relative to obj. */
3381 : 196482 : unsigned int obj_uid = DECL_PT_UID (obj);
3382 : 196482 : if (obj_uid != pt_uid)
3383 : : return false;
3384 : :
3385 : : /* Check that the object size is the same as the store size. That ensures us
3386 : : that ptr points to the start of obj. */
3387 : 41723 : return (DECL_SIZE (obj)
3388 : 41723 : && poly_int_tree_p (DECL_SIZE (obj))
3389 : 125081 : && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
3390 : : }
3391 : :
3392 : : /* Return true if REF is killed by an store described by
3393 : : BASE, OFFSET, SIZE and MAX_SIZE. */
3394 : :
3395 : : static bool
3396 : 238341328 : store_kills_ref_p (tree base, poly_int64 offset, poly_int64 size,
3397 : : poly_int64 max_size, ao_ref *ref)
3398 : : {
3399 : 238341328 : poly_int64 ref_offset = ref->offset;
3400 : : /* We can get MEM[symbol: sZ, index: D.8862_1] here,
3401 : : so base == ref->base does not always hold. */
3402 : 238341328 : if (base != ref->base)
3403 : : {
3404 : : /* Try using points-to info. */
3405 : 136996161 : if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
3406 : : ref->offset, ref->size, ref->max_size))
3407 : : return true;
3408 : :
3409 : : /* If both base and ref->base are MEM_REFs, only compare the
3410 : : first operand, and if the second operand isn't equal constant,
3411 : : try to add the offsets into offset and ref_offset. */
3412 : 36267371 : if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
3413 : 164147655 : && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
3414 : : {
3415 : 20838658 : if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
3416 : 20838658 : TREE_OPERAND (ref->base, 1)))
3417 : : {
3418 : 7616162 : poly_offset_int off1 = mem_ref_offset (base);
3419 : 7616162 : off1 <<= LOG2_BITS_PER_UNIT;
3420 : 7616162 : off1 += offset;
3421 : 7616162 : poly_offset_int off2 = mem_ref_offset (ref->base);
3422 : 7616162 : off2 <<= LOG2_BITS_PER_UNIT;
3423 : 7616162 : off2 += ref_offset;
3424 : 7616162 : if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
3425 : 0 : size = -1;
3426 : : }
3427 : : }
3428 : : else
3429 : 116157415 : size = -1;
3430 : : }
3431 : : /* For a must-alias check we need to be able to constrain
3432 : : the access properly. */
3433 : 238341240 : return (known_eq (size, max_size)
3434 : 238341240 : && known_subrange_p (ref_offset, ref->max_size, offset, size));
3435 : : }
3436 : :
3437 : : /* If STMT kills the memory reference REF return true, otherwise
3438 : : return false. */
3439 : :
3440 : : bool
3441 : 288489716 : stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
3442 : : {
3443 : 288489716 : if (!ao_ref_base (ref))
3444 : : return false;
3445 : :
3446 : 288489716 : if (gimple_has_lhs (stmt)
3447 : 251824180 : && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
3448 : : /* The assignment is not necessarily carried out if it can throw
3449 : : and we can catch it in the current function where we could inspect
3450 : : the previous value. Similarly if the function can throw externally
3451 : : and the ref does not die on the function return.
3452 : : ??? We only need to care about the RHS throwing. For aggregate
3453 : : assignments or similar calls and non-call exceptions the LHS
3454 : : might throw as well.
3455 : : ??? We also should care about possible longjmp, but since we
3456 : : do not understand that longjmp is not using global memory we will
3457 : : not consider a kill here since the function call will be considered
3458 : : as possibly using REF. */
3459 : 246162200 : && !stmt_can_throw_internal (cfun, stmt)
3460 : 264879975 : && (!stmt_can_throw_external (cfun, stmt)
3461 : 3995692 : || !ref_may_alias_global_p (ref, false)))
3462 : : {
3463 : 241358962 : tree lhs = gimple_get_lhs (stmt);
3464 : : /* If LHS is literally a base of the access we are done. */
3465 : 241358962 : if (ref->ref)
3466 : : {
3467 : 239849970 : tree base = ref->ref;
3468 : 239849970 : tree innermost_dropped_array_ref = NULL_TREE;
3469 : 239849970 : if (handled_component_p (base))
3470 : : {
3471 : 150807927 : tree saved_lhs0 = NULL_TREE;
3472 : 267451162 : if (handled_component_p (lhs))
3473 : : {
3474 : 116643235 : saved_lhs0 = TREE_OPERAND (lhs, 0);
3475 : 116643235 : TREE_OPERAND (lhs, 0) = integer_zero_node;
3476 : : }
3477 : 252854235 : do
3478 : : {
3479 : : /* Just compare the outermost handled component, if
3480 : : they are equal we have found a possible common
3481 : : base. */
3482 : 252854235 : tree saved_base0 = TREE_OPERAND (base, 0);
3483 : 252854235 : TREE_OPERAND (base, 0) = integer_zero_node;
3484 : 252854235 : bool res = operand_equal_p (lhs, base, 0);
3485 : 252854235 : TREE_OPERAND (base, 0) = saved_base0;
3486 : 252854235 : if (res)
3487 : : break;
3488 : : /* Remember if we drop an array-ref that we need to
3489 : : double-check not being at struct end. */
3490 : 237938650 : if (TREE_CODE (base) == ARRAY_REF
3491 : 237938650 : || TREE_CODE (base) == ARRAY_RANGE_REF)
3492 : 68627995 : innermost_dropped_array_ref = base;
3493 : : /* Otherwise drop handled components of the access. */
3494 : 237938650 : base = saved_base0;
3495 : : }
3496 : 373830992 : while (handled_component_p (base));
3497 : 150807927 : if (saved_lhs0)
3498 : 116643235 : TREE_OPERAND (lhs, 0) = saved_lhs0;
3499 : : }
3500 : : /* Finally check if the lhs has the same address and size as the
3501 : : base candidate of the access. Watch out if we have dropped
3502 : : an array-ref that might have flexible size, this means ref->ref
3503 : : may be outside of the TYPE_SIZE of its base. */
3504 : 150807927 : if ((! innermost_dropped_array_ref
3505 : 67860050 : || ! array_ref_flexible_size_p (innermost_dropped_array_ref))
3506 : 380086690 : && (lhs == base
3507 : 227006086 : || (((TYPE_SIZE (TREE_TYPE (lhs))
3508 : 227006086 : == TYPE_SIZE (TREE_TYPE (base)))
3509 : 157476674 : || (TYPE_SIZE (TREE_TYPE (lhs))
3510 : 157476611 : && TYPE_SIZE (TREE_TYPE (base))
3511 : 157476512 : && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
3512 : 157476512 : TYPE_SIZE (TREE_TYPE (base)),
3513 : : 0)))
3514 : 69529412 : && operand_equal_p (lhs, base,
3515 : : OEP_ADDRESS_OF
3516 : : | OEP_MATCH_SIDE_EFFECTS))))
3517 : : {
3518 : 3199118 : ++alias_stats.stmt_kills_ref_p_yes;
3519 : 9499391 : return true;
3520 : : }
3521 : : }
3522 : :
3523 : : /* Now look for non-literal equal bases with the restriction of
3524 : : handling constant offset and size. */
3525 : : /* For a must-alias check we need to be able to constrain
3526 : : the access properly. */
3527 : 238159844 : if (!ref->max_size_known_p ())
3528 : : {
3529 : 1101929 : ++alias_stats.stmt_kills_ref_p_no;
3530 : 1101929 : return false;
3531 : : }
3532 : 237057915 : poly_int64 size, offset, max_size;
3533 : 237057915 : bool reverse;
3534 : 237057915 : tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
3535 : : &reverse);
3536 : 237057915 : if (store_kills_ref_p (base, offset, size, max_size, ref))
3537 : : {
3538 : 1999226 : ++alias_stats.stmt_kills_ref_p_yes;
3539 : 1999226 : return true;
3540 : : }
3541 : : }
3542 : :
3543 : 282189443 : if (is_gimple_call (stmt))
3544 : : {
3545 : 22619864 : tree callee = gimple_call_fndecl (stmt);
3546 : 22619864 : struct cgraph_node *node;
3547 : 22619864 : modref_summary *summary;
3548 : :
3549 : : /* Try to disambiguate using modref summary. Modref records a vector
3550 : : of stores with known offsets relative to function parameters that must
3551 : : happen every execution of function. Find if we have a matching
3552 : : store and verify that function can not use the value. */
3553 : 22619864 : if (callee != NULL_TREE
3554 : 21760756 : && (node = cgraph_node::get (callee)) != NULL
3555 : 21718494 : && node->binds_to_current_def_p ()
3556 : 2100557 : && (summary = get_modref_function_summary (node)) != NULL
3557 : 1067983 : && summary->kills.length ()
3558 : : /* Check that we can not trap while evaulating function
3559 : : parameters. This check is overly conservative. */
3560 : 22726357 : && (!cfun->can_throw_non_call_exceptions
3561 : 0 : || (!stmt_can_throw_internal (cfun, stmt)
3562 : 0 : && (!stmt_can_throw_external (cfun, stmt)
3563 : 0 : || !ref_may_alias_global_p (ref, false)))))
3564 : : {
3565 : 457026 : for (auto kill : summary->kills)
3566 : : {
3567 : 146745 : ao_ref dref;
3568 : :
3569 : : /* We only can do useful compares if we know the access range
3570 : : precisely. */
3571 : 146745 : if (!kill.get_ao_ref (as_a <gcall *> (stmt), &dref))
3572 : 24 : continue;
3573 : 146721 : if (store_kills_ref_p (ao_ref_base (&dref), dref.offset,
3574 : : dref.size, dref.max_size, ref))
3575 : : {
3576 : : /* For store to be killed it needs to not be used
3577 : : earlier. */
3578 : 9198 : if (ref_maybe_used_by_call_p_1 (as_a <gcall *> (stmt), ref,
3579 : : true)
3580 : 9198 : || !dbg_cnt (ipa_mod_ref))
3581 : : break;
3582 : 3622 : if (dump_file && (dump_flags & TDF_DETAILS))
3583 : : {
3584 : 2 : fprintf (dump_file,
3585 : : "ipa-modref: call stmt ");
3586 : 2 : print_gimple_stmt (dump_file, stmt, 0);
3587 : 2 : fprintf (dump_file,
3588 : : "ipa-modref: call to %s kills ",
3589 : : node->dump_name ());
3590 : 2 : print_generic_expr (dump_file, ref->base);
3591 : 2 : fprintf (dump_file, "\n");
3592 : : }
3593 : 3622 : ++alias_stats.modref_kill_yes;
3594 : 3622 : return true;
3595 : : }
3596 : : }
3597 : 102871 : ++alias_stats.modref_kill_no;
3598 : : }
3599 : 22616242 : if (callee != NULL_TREE
3600 : 22616242 : && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3601 : 4600883 : switch (DECL_FUNCTION_CODE (callee))
3602 : : {
3603 : 536355 : case BUILT_IN_FREE:
3604 : 536355 : {
3605 : 536355 : tree ptr = gimple_call_arg (stmt, 0);
3606 : 536355 : tree base = ao_ref_base (ref);
3607 : 536355 : if (base && TREE_CODE (base) == MEM_REF
3608 : 629557 : && TREE_OPERAND (base, 0) == ptr)
3609 : : {
3610 : 13533 : ++alias_stats.stmt_kills_ref_p_yes;
3611 : 13533 : return true;
3612 : : }
3613 : : break;
3614 : : }
3615 : :
3616 : 1521854 : case BUILT_IN_MEMCPY:
3617 : 1521854 : case BUILT_IN_MEMPCPY:
3618 : 1521854 : case BUILT_IN_MEMMOVE:
3619 : 1521854 : case BUILT_IN_MEMSET:
3620 : 1521854 : case BUILT_IN_MEMCPY_CHK:
3621 : 1521854 : case BUILT_IN_MEMPCPY_CHK:
3622 : 1521854 : case BUILT_IN_MEMMOVE_CHK:
3623 : 1521854 : case BUILT_IN_MEMSET_CHK:
3624 : 1521854 : case BUILT_IN_STRNCPY:
3625 : 1521854 : case BUILT_IN_STPNCPY:
3626 : 1521854 : case BUILT_IN_CALLOC:
3627 : 1521854 : {
3628 : : /* For a must-alias check we need to be able to constrain
3629 : : the access properly. */
3630 : 1521854 : if (!ref->max_size_known_p ())
3631 : : {
3632 : 57805 : ++alias_stats.stmt_kills_ref_p_no;
3633 : 448173 : return false;
3634 : : }
3635 : 1464049 : tree dest;
3636 : 1464049 : tree len;
3637 : :
3638 : : /* In execution order a calloc call will never kill
3639 : : anything. However, DSE will (ab)use this interface
3640 : : to ask if a calloc call writes the same memory locations
3641 : : as a later assignment, memset, etc. So handle calloc
3642 : : in the expected way. */
3643 : 1464049 : if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
3644 : : {
3645 : 1445 : tree arg0 = gimple_call_arg (stmt, 0);
3646 : 1445 : tree arg1 = gimple_call_arg (stmt, 1);
3647 : 1445 : if (TREE_CODE (arg0) != INTEGER_CST
3648 : 1307 : || TREE_CODE (arg1) != INTEGER_CST)
3649 : : {
3650 : 170 : ++alias_stats.stmt_kills_ref_p_no;
3651 : 170 : return false;
3652 : : }
3653 : :
3654 : 1275 : dest = gimple_call_lhs (stmt);
3655 : 1275 : if (!dest)
3656 : : {
3657 : 1 : ++alias_stats.stmt_kills_ref_p_no;
3658 : 1 : return false;
3659 : : }
3660 : 1274 : len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
3661 : : }
3662 : : else
3663 : : {
3664 : 1462604 : dest = gimple_call_arg (stmt, 0);
3665 : 1462604 : len = gimple_call_arg (stmt, 2);
3666 : : }
3667 : 1463878 : if (!poly_int_tree_p (len))
3668 : : return false;
3669 : 1136692 : ao_ref dref;
3670 : 1136692 : ao_ref_init_from_ptr_and_size (&dref, dest, len);
3671 : 1136692 : if (store_kills_ref_p (ao_ref_base (&dref), dref.offset,
3672 : : dref.size, dref.max_size, ref))
3673 : : {
3674 : 5206 : ++alias_stats.stmt_kills_ref_p_yes;
3675 : 5206 : return true;
3676 : : }
3677 : 1131486 : break;
3678 : : }
3679 : :
3680 : 11842 : case BUILT_IN_VA_END:
3681 : 11842 : {
3682 : 11842 : tree ptr = gimple_call_arg (stmt, 0);
3683 : 11842 : if (TREE_CODE (ptr) == ADDR_EXPR)
3684 : : {
3685 : 11791 : tree base = ao_ref_base (ref);
3686 : 11791 : if (TREE_OPERAND (ptr, 0) == base)
3687 : : {
3688 : 6979 : ++alias_stats.stmt_kills_ref_p_yes;
3689 : 6979 : return true;
3690 : : }
3691 : : }
3692 : : break;
3693 : : }
3694 : :
3695 : : default:;
3696 : : }
3697 : : }
3698 : 281774941 : ++alias_stats.stmt_kills_ref_p_no;
3699 : 281774941 : return false;
3700 : : }
3701 : :
3702 : : bool
3703 : 0 : stmt_kills_ref_p (gimple *stmt, tree ref)
3704 : : {
3705 : 0 : ao_ref r;
3706 : 0 : ao_ref_init (&r, ref);
3707 : 0 : return stmt_kills_ref_p (stmt, &r);
3708 : : }
3709 : :
3710 : : /* Return whether REF can be subject to store data races. */
3711 : :
3712 : : bool
3713 : 24647 : ref_can_have_store_data_races (tree ref)
3714 : : {
3715 : : /* With -fallow-store-data-races do not care about them. */
3716 : 24647 : if (flag_store_data_races)
3717 : : return false;
3718 : :
3719 : 24543 : tree base = get_base_address (ref);
3720 : 24543 : if (auto_var_p (base)
3721 : 24543 : && ! may_be_aliased (base))
3722 : : /* Automatic variables not aliased are not subject to
3723 : : data races. */
3724 : : return false;
3725 : :
3726 : : return true;
3727 : : }
3728 : :
3729 : :
3730 : : /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3731 : : TARGET or a statement clobbering the memory reference REF in which
3732 : : case false is returned. The walk starts with VUSE, one argument of PHI. */
3733 : :
3734 : : static bool
3735 : 127518609 : maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3736 : : ao_ref *ref, tree vuse, bool tbaa_p, unsigned int &limit,
3737 : : bitmap *visited, bool abort_on_visited,
3738 : : void *(*translate)(ao_ref *, tree, void *, translate_flags *),
3739 : : translate_flags disambiguate_only,
3740 : : void *data)
3741 : : {
3742 : 127518609 : basic_block bb = gimple_bb (phi);
3743 : :
3744 : 127518609 : if (!*visited)
3745 : : {
3746 : 23682645 : *visited = BITMAP_ALLOC (NULL);
3747 : 23682645 : bitmap_tree_view (*visited);
3748 : : }
3749 : :
3750 : 127518609 : bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3751 : :
3752 : : /* Walk until we hit the target. */
3753 : 127518609 : while (vuse != target)
3754 : : {
3755 : 389406794 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3756 : : /* If we are searching for the target VUSE by walking up to
3757 : : TARGET_BB dominating the original PHI we are finished once
3758 : : we reach a default def or a definition in a block dominating
3759 : : that block. Update TARGET and return. */
3760 : 389406794 : if (!target
3761 : 389406794 : && (gimple_nop_p (def_stmt)
3762 : 73414930 : || dominated_by_p (CDI_DOMINATORS,
3763 : 73414930 : target_bb, gimple_bb (def_stmt))))
3764 : : {
3765 : 19829440 : target = vuse;
3766 : 19829440 : return true;
3767 : : }
3768 : :
3769 : : /* Recurse for PHI nodes. */
3770 : 369577354 : if (gimple_code (def_stmt) == GIMPLE_PHI)
3771 : : {
3772 : : /* An already visited PHI node ends the walk successfully. */
3773 : 73648210 : if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
3774 : 33216338 : return !abort_on_visited;
3775 : 40431872 : vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3776 : : visited, abort_on_visited,
3777 : : translate, data, disambiguate_only);
3778 : 40431872 : if (!vuse)
3779 : : return false;
3780 : 35544715 : continue;
3781 : : }
3782 : 295929144 : else if (gimple_nop_p (def_stmt))
3783 : : return false;
3784 : : else
3785 : : {
3786 : : /* A clobbering statement or the end of the IL ends it failing. */
3787 : 295929144 : if ((int)limit <= 0)
3788 : : return false;
3789 : 295896863 : --limit;
3790 : 295896863 : if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3791 : : {
3792 : 18474846 : translate_flags tf = disambiguate_only;
3793 : 18474846 : if (translate
3794 : 18474846 : && (*translate) (ref, vuse, data, &tf) == NULL)
3795 : : ;
3796 : : else
3797 : 15240584 : return false;
3798 : : }
3799 : : }
3800 : : /* If we reach a new basic-block see if we already skipped it
3801 : : in a previous walk that ended successfully. */
3802 : 280656279 : if (gimple_bb (def_stmt) != bb)
3803 : : {
3804 : 126290233 : if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3805 : 9353881 : return !abort_on_visited;
3806 : 116936352 : bb = gimple_bb (def_stmt);
3807 : : }
3808 : 705668120 : vuse = gimple_vuse (def_stmt);
3809 : : }
3810 : : return true;
3811 : : }
3812 : :
3813 : :
3814 : : /* Starting from a PHI node for the virtual operand of the memory reference
3815 : : REF find a continuation virtual operand that allows to continue walking
3816 : : statements dominating PHI skipping only statements that cannot possibly
3817 : : clobber REF. Decrements LIMIT for each alias disambiguation done
3818 : : and aborts the walk, returning NULL_TREE if it reaches zero.
3819 : : Returns NULL_TREE if no suitable virtual operand can be found. */
3820 : :
3821 : : tree
3822 : 100518020 : get_continuation_for_phi (gimple *phi, ao_ref *ref, bool tbaa_p,
3823 : : unsigned int &limit, bitmap *visited,
3824 : : bool abort_on_visited,
3825 : : void *(*translate)(ao_ref *, tree, void *,
3826 : : translate_flags *),
3827 : : void *data,
3828 : : translate_flags disambiguate_only)
3829 : : {
3830 : 100518020 : unsigned nargs = gimple_phi_num_args (phi);
3831 : :
3832 : : /* Through a single-argument PHI we can simply look through. */
3833 : 100518020 : if (nargs == 1)
3834 : 3445568 : return PHI_ARG_DEF (phi, 0);
3835 : :
3836 : : /* For two or more arguments try to pairwise skip non-aliasing code
3837 : : until we hit the phi argument definition that dominates the other one. */
3838 : 97072452 : basic_block phi_bb = gimple_bb (phi);
3839 : 97072452 : tree arg0, arg1;
3840 : 97072452 : unsigned i;
3841 : :
3842 : : /* Find a candidate for the virtual operand which definition
3843 : : dominates those of all others. */
3844 : : /* First look if any of the args themselves satisfy this. */
3845 : 178835912 : for (i = 0; i < nargs; ++i)
3846 : : {
3847 : 154270530 : arg0 = PHI_ARG_DEF (phi, i);
3848 : 154270530 : if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3849 : : break;
3850 : 151044684 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3851 : 151044684 : if (def_bb != phi_bb
3852 : 151044684 : && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3853 : : break;
3854 : 81763460 : arg0 = NULL_TREE;
3855 : : }
3856 : : /* If not, look if we can reach such candidate by walking defs
3857 : : until we hit the immediate dominator. maybe_skip_until will
3858 : : do that for us. */
3859 : 97072452 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3860 : :
3861 : : /* Then check against the (to be) found candidate. */
3862 : 373603463 : for (i = 0; i < nargs; ++i)
3863 : : {
3864 : 199740479 : arg1 = PHI_ARG_DEF (phi, i);
3865 : 199740479 : if (arg1 == arg0)
3866 : : ;
3867 : 343220791 : else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, tbaa_p,
3868 : : limit, visited,
3869 : : abort_on_visited,
3870 : : translate,
3871 : : /* Do not valueize when walking over
3872 : : backedges. */
3873 : : dominated_by_p
3874 : 127518609 : (CDI_DOMINATORS,
3875 : 127518609 : gimple_bb (SSA_NAME_DEF_STMT (arg1)),
3876 : : phi_bb)
3877 : : ? TR_DISAMBIGUATE
3878 : : : disambiguate_only, data))
3879 : : return NULL_TREE;
3880 : : }
3881 : :
3882 : 76790532 : return arg0;
3883 : : }
3884 : :
3885 : : /* Based on the memory reference REF and its virtual use VUSE call
3886 : : WALKER for each virtual use that is equivalent to VUSE, including VUSE
3887 : : itself. That is, for each virtual use for which its defining statement
3888 : : does not clobber REF.
3889 : :
3890 : : WALKER is called with REF, the current virtual use and DATA. If
3891 : : WALKER returns non-NULL the walk stops and its result is returned.
3892 : : At the end of a non-successful walk NULL is returned.
3893 : :
3894 : : TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3895 : : use which definition is a statement that may clobber REF and DATA.
3896 : : If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3897 : : If TRANSLATE returns non-NULL the walk stops and its result is returned.
3898 : : If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3899 : : to adjust REF and *DATA to make that valid.
3900 : :
3901 : : VALUEIZE if non-NULL is called with the next VUSE that is considered
3902 : : and return value is substituted for that. This can be used to
3903 : : implement optimistic value-numbering for example. Note that the
3904 : : VUSE argument is assumed to be valueized already.
3905 : :
3906 : : LIMIT specifies the number of alias queries we are allowed to do,
3907 : : the walk stops when it reaches zero and NULL is returned. LIMIT
3908 : : is decremented by the number of alias queries (plus adjustments
3909 : : done by the callbacks) upon return.
3910 : :
3911 : : TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3912 : :
3913 : : void *
3914 : 69155259 : walk_non_aliased_vuses (ao_ref *ref, tree vuse, bool tbaa_p,
3915 : : void *(*walker)(ao_ref *, tree, void *),
3916 : : void *(*translate)(ao_ref *, tree, void *,
3917 : : translate_flags *),
3918 : : tree (*valueize)(tree),
3919 : : unsigned &limit, void *data)
3920 : : {
3921 : 69155259 : bitmap visited = NULL;
3922 : 69155259 : void *res;
3923 : 69155259 : bool translated = false;
3924 : :
3925 : 69155259 : timevar_push (TV_ALIAS_STMT_WALK);
3926 : :
3927 : 1064863659 : do
3928 : : {
3929 : 1064863659 : gimple *def_stmt;
3930 : :
3931 : : /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3932 : 1064863659 : res = (*walker) (ref, vuse, data);
3933 : : /* Abort walk. */
3934 : 1064863659 : if (res == (void *)-1)
3935 : : {
3936 : : res = NULL;
3937 : : break;
3938 : : }
3939 : : /* Lookup succeeded. */
3940 : 1064863541 : else if (res != NULL)
3941 : : break;
3942 : :
3943 : 1056610702 : if (valueize)
3944 : : {
3945 : 1038056349 : vuse = valueize (vuse);
3946 : 1038056349 : if (!vuse)
3947 : : {
3948 : : res = NULL;
3949 : : break;
3950 : : }
3951 : : }
3952 : 1041296917 : def_stmt = SSA_NAME_DEF_STMT (vuse);
3953 : 1041296917 : if (gimple_nop_p (def_stmt))
3954 : : break;
3955 : 1038808116 : else if (gimple_code (def_stmt) == GIMPLE_PHI)
3956 : 57097996 : vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3957 : : &visited, translated, translate, data);
3958 : : else
3959 : : {
3960 : 981710120 : if ((int)limit <= 0)
3961 : : {
3962 : : res = NULL;
3963 : : break;
3964 : : }
3965 : 981470676 : --limit;
3966 : 981470676 : if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3967 : : {
3968 : 34056821 : if (!translate)
3969 : : break;
3970 : 28711605 : translate_flags disambiguate_only = TR_TRANSLATE;
3971 : 28711605 : res = (*translate) (ref, vuse, data, &disambiguate_only);
3972 : : /* Failed lookup and translation. */
3973 : 28711605 : if (res == (void *)-1)
3974 : : {
3975 : : res = NULL;
3976 : : break;
3977 : : }
3978 : : /* Lookup succeeded. */
3979 : 5983061 : else if (res != NULL)
3980 : : break;
3981 : : /* Translation succeeded, continue walking. */
3982 : 7856803 : translated = translated || disambiguate_only == TR_TRANSLATE;
3983 : : }
3984 : 952237302 : vuse = gimple_vuse (def_stmt);
3985 : : }
3986 : : }
3987 : 1009335298 : while (vuse);
3988 : :
3989 : 69155259 : if (visited)
3990 : 21230764 : BITMAP_FREE (visited);
3991 : :
3992 : 69155259 : timevar_pop (TV_ALIAS_STMT_WALK);
3993 : :
3994 : 69155259 : return res;
3995 : : }
3996 : :
3997 : :
3998 : : /* Based on the memory reference REF call WALKER for each vdef whose
3999 : : defining statement may clobber REF, starting with VDEF. If REF
4000 : : is NULL_TREE, each defining statement is visited.
4001 : :
4002 : : WALKER is called with REF, the current vdef and DATA. If WALKER
4003 : : returns true the walk is stopped, otherwise it continues.
4004 : :
4005 : : If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
4006 : : The pointer may be NULL and then we do not track this information.
4007 : :
4008 : : At PHI nodes walk_aliased_vdefs forks into one walk for each
4009 : : PHI argument (but only one walk continues at merge points), the
4010 : : return value is true if any of the walks was successful.
4011 : :
4012 : : The function returns the number of statements walked or -1 if
4013 : : LIMIT stmts were walked and the walk was aborted at this point.
4014 : : If LIMIT is zero the walk is not aborted. */
4015 : :
4016 : : static int
4017 : 308556699 : walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
4018 : : bool (*walker)(ao_ref *, tree, void *), void *data,
4019 : : bitmap *visited, unsigned int cnt,
4020 : : bool *function_entry_reached, unsigned limit)
4021 : : {
4022 : 959244265 : do
4023 : : {
4024 : 1918488530 : gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
4025 : :
4026 : 959244265 : if (*visited
4027 : 959244265 : && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
4028 : 170612136 : return cnt;
4029 : :
4030 : 788632129 : if (gimple_nop_p (def_stmt))
4031 : : {
4032 : 24859178 : if (function_entry_reached)
4033 : 3889697 : *function_entry_reached = true;
4034 : 24859178 : return cnt;
4035 : : }
4036 : 763772951 : else if (gimple_code (def_stmt) == GIMPLE_PHI)
4037 : : {
4038 : 97151566 : unsigned i;
4039 : 97151566 : if (!*visited)
4040 : : {
4041 : 8396460 : *visited = BITMAP_ALLOC (NULL);
4042 : 8396460 : bitmap_tree_view (*visited);
4043 : : }
4044 : 301725633 : for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
4045 : : {
4046 : 209889903 : int res = walk_aliased_vdefs_1 (ref,
4047 : : gimple_phi_arg_def (def_stmt, i),
4048 : : walker, data, visited, cnt,
4049 : : function_entry_reached, limit);
4050 : 209889903 : if (res == -1)
4051 : : return -1;
4052 : 204574067 : cnt = res;
4053 : : }
4054 : 91835730 : return cnt;
4055 : : }
4056 : :
4057 : : /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
4058 : 666621385 : cnt++;
4059 : 666621385 : if (cnt == limit)
4060 : : return -1;
4061 : 666481708 : if ((!ref
4062 : 589205726 : || stmt_may_clobber_ref_p_1 (def_stmt, ref))
4063 : 741860025 : && (*walker) (ref, vdef, data))
4064 : 15794142 : return cnt;
4065 : :
4066 : 1609931831 : vdef = gimple_vuse (def_stmt);
4067 : : }
4068 : : while (1);
4069 : : }
4070 : :
4071 : : int
4072 : 98666796 : walk_aliased_vdefs (ao_ref *ref, tree vdef,
4073 : : bool (*walker)(ao_ref *, tree, void *), void *data,
4074 : : bitmap *visited,
4075 : : bool *function_entry_reached, unsigned int limit)
4076 : : {
4077 : 98666796 : bitmap local_visited = NULL;
4078 : 98666796 : int ret;
4079 : :
4080 : 98666796 : timevar_push (TV_ALIAS_STMT_WALK);
4081 : :
4082 : 98666796 : if (function_entry_reached)
4083 : 4859158 : *function_entry_reached = false;
4084 : :
4085 : 170351168 : ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
4086 : : visited ? visited : &local_visited, 0,
4087 : : function_entry_reached, limit);
4088 : 98666796 : if (local_visited)
4089 : 8396460 : BITMAP_FREE (local_visited);
4090 : :
4091 : 98666796 : timevar_pop (TV_ALIAS_STMT_WALK);
4092 : :
4093 : 98666796 : return ret;
4094 : : }
4095 : :
4096 : : /* Verify validity of the fnspec string.
4097 : : See attr-fnspec.h for details. */
4098 : :
4099 : : void
4100 : 443682638 : attr_fnspec::verify ()
4101 : : {
4102 : 443682638 : bool err = false;
4103 : 443682638 : if (!len)
4104 : : return;
4105 : :
4106 : : /* Check return value specifier. */
4107 : 137909069 : if (len < return_desc_size)
4108 : : err = true;
4109 : 137909069 : else if ((len - return_desc_size) % arg_desc_size)
4110 : : err = true;
4111 : 137909069 : else if ((str[0] < '1' || str[0] > '4')
4112 : 137909069 : && str[0] != '.' && str[0] != 'm')
4113 : 0 : err = true;
4114 : :
4115 : 137909069 : switch (str[1])
4116 : : {
4117 : : case ' ':
4118 : : case 'p':
4119 : : case 'P':
4120 : : case 'c':
4121 : : case 'C':
4122 : : break;
4123 : : default:
4124 : : err = true;
4125 : : }
4126 : 137909069 : if (err)
4127 : 0 : internal_error ("invalid fn spec attribute \"%s\"", str);
4128 : :
4129 : : /* Now check all parameters. */
4130 : 412271050 : for (unsigned int i = 0; arg_specified_p (i); i++)
4131 : : {
4132 : 274361981 : unsigned int idx = arg_idx (i);
4133 : 274361981 : switch (str[idx])
4134 : : {
4135 : 254813696 : case 'x':
4136 : 254813696 : case 'X':
4137 : 254813696 : case 'r':
4138 : 254813696 : case 'R':
4139 : 254813696 : case 'o':
4140 : 254813696 : case 'O':
4141 : 254813696 : case 'w':
4142 : 254813696 : case 'W':
4143 : 254813696 : case '.':
4144 : 254813696 : if ((str[idx + 1] >= '1' && str[idx + 1] <= '9')
4145 : 254813696 : || str[idx + 1] == 't')
4146 : : {
4147 : 41358974 : if (str[idx] != 'r' && str[idx] != 'R'
4148 : : && str[idx] != 'w' && str[idx] != 'W'
4149 : : && str[idx] != 'o' && str[idx] != 'O')
4150 : 41358974 : err = true;
4151 : 41358974 : if (str[idx + 1] != 't'
4152 : : /* Size specified is scalar, so it should be described
4153 : : by ". " if specified at all. */
4154 : 41358974 : && (arg_specified_p (str[idx + 1] - '1')
4155 : 0 : && str[arg_idx (str[idx + 1] - '1')] != '.'))
4156 : : err = true;
4157 : : }
4158 : 213454722 : else if (str[idx + 1] != ' ')
4159 : : err = true;
4160 : : break;
4161 : 19548285 : default:
4162 : 19548285 : if (str[idx] < '1' || str[idx] > '9')
4163 : : err = true;
4164 : : }
4165 : 274361981 : if (err)
4166 : 0 : internal_error ("invalid fn spec attribute \"%s\" arg %i", str, i);
4167 : : }
4168 : : }
4169 : :
4170 : : /* Return ture if TYPE1 and TYPE2 will always give the same answer
4171 : : when compared with other types using same_type_for_tbaa. */
4172 : :
4173 : : static bool
4174 : 22399720 : types_equal_for_same_type_for_tbaa_p (tree type1, tree type2,
4175 : : bool lto_streaming_safe)
4176 : : {
4177 : : /* We use same_type_for_tbaa_p to match types in the access path.
4178 : : This check is overly conservative. */
4179 : 22399720 : type1 = TYPE_MAIN_VARIANT (type1);
4180 : 22399720 : type2 = TYPE_MAIN_VARIANT (type2);
4181 : :
4182 : 22399720 : if (TYPE_STRUCTURAL_EQUALITY_P (type1)
4183 : 22399720 : != TYPE_STRUCTURAL_EQUALITY_P (type2))
4184 : : return false;
4185 : 22121112 : if (TYPE_STRUCTURAL_EQUALITY_P (type1))
4186 : : return true;
4187 : :
4188 : 18352614 : if (lto_streaming_safe)
4189 : 83084 : return type1 == type2;
4190 : : else
4191 : 18269530 : return TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2);
4192 : : }
4193 : :
4194 : : /* Return ture if TYPE1 and TYPE2 will always give the same answer
4195 : : when compared with other types using same_type_for_tbaa. */
4196 : :
4197 : : bool
4198 : 22146656 : types_equal_for_same_type_for_tbaa_p (tree type1, tree type2)
4199 : : {
4200 : 22146656 : return types_equal_for_same_type_for_tbaa_p (type1, type2,
4201 : 22146656 : lto_streaming_expected_p ());
4202 : : }
4203 : :
4204 : : /* Compare REF1 and REF2 and return flags specifying their differences.
4205 : : If LTO_STREAMING_SAFE is true do not use alias sets and canonical
4206 : : types that are going to be recomputed.
4207 : : If TBAA is true also compare TBAA metadata. */
4208 : :
4209 : : int
4210 : 160315 : ao_compare::compare_ao_refs (ao_ref *ref1, ao_ref *ref2,
4211 : : bool lto_streaming_safe,
4212 : : bool tbaa)
4213 : : {
4214 : 160315 : if (TREE_THIS_VOLATILE (ref1->ref) != TREE_THIS_VOLATILE (ref2->ref))
4215 : : return SEMANTICS;
4216 : 160315 : tree base1 = ao_ref_base (ref1);
4217 : 160315 : tree base2 = ao_ref_base (ref2);
4218 : :
4219 : 160315 : if (!known_eq (ref1->offset, ref2->offset)
4220 : 160315 : || !known_eq (ref1->size, ref2->size)
4221 : 320630 : || !known_eq (ref1->max_size, ref2->max_size))
4222 : : return SEMANTICS;
4223 : :
4224 : : /* For variable accesses we need to compare actual paths
4225 : : to check that both refs are accessing same address and the access size. */
4226 : 160312 : if (!known_eq (ref1->size, ref1->max_size))
4227 : : {
4228 : 4130 : if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (ref1->ref)),
4229 : 4130 : TYPE_SIZE (TREE_TYPE (ref2->ref)), 0))
4230 : : return SEMANTICS;
4231 : 4130 : tree r1 = ref1->ref;
4232 : 4130 : tree r2 = ref2->ref;
4233 : :
4234 : : /* Handle toplevel COMPONENT_REFs of bitfields.
4235 : : Those are special since they are not allowed in
4236 : : ADDR_EXPR. */
4237 : 4130 : if (TREE_CODE (r1) == COMPONENT_REF
4238 : 4130 : && DECL_BIT_FIELD (TREE_OPERAND (r1, 1)))
4239 : : {
4240 : 0 : if (TREE_CODE (r2) != COMPONENT_REF
4241 : 0 : || !DECL_BIT_FIELD (TREE_OPERAND (r2, 1)))
4242 : : return SEMANTICS;
4243 : 0 : tree field1 = TREE_OPERAND (r1, 1);
4244 : 0 : tree field2 = TREE_OPERAND (r2, 1);
4245 : 0 : if (!operand_equal_p (DECL_FIELD_OFFSET (field1),
4246 : 0 : DECL_FIELD_OFFSET (field2), 0)
4247 : 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field1),
4248 : 0 : DECL_FIELD_BIT_OFFSET (field2), 0)
4249 : 0 : || !operand_equal_p (DECL_SIZE (field1), DECL_SIZE (field2), 0)
4250 : 0 : || !types_compatible_p (TREE_TYPE (r1),
4251 : 0 : TREE_TYPE (r2)))
4252 : 0 : return SEMANTICS;
4253 : 0 : r1 = TREE_OPERAND (r1, 0);
4254 : 0 : r2 = TREE_OPERAND (r2, 0);
4255 : : }
4256 : 4130 : else if (TREE_CODE (r2) == COMPONENT_REF
4257 : 4130 : && DECL_BIT_FIELD (TREE_OPERAND (r2, 1)))
4258 : : return SEMANTICS;
4259 : :
4260 : : /* Similarly for bit field refs. */
4261 : 4130 : if (TREE_CODE (r1) == BIT_FIELD_REF)
4262 : : {
4263 : 0 : if (TREE_CODE (r2) != BIT_FIELD_REF
4264 : 0 : || !operand_equal_p (TREE_OPERAND (r1, 1),
4265 : 0 : TREE_OPERAND (r2, 1), 0)
4266 : 0 : || !operand_equal_p (TREE_OPERAND (r1, 2),
4267 : 0 : TREE_OPERAND (r2, 2), 0)
4268 : 0 : || !types_compatible_p (TREE_TYPE (r1),
4269 : 0 : TREE_TYPE (r2)))
4270 : 0 : return SEMANTICS;
4271 : 0 : r1 = TREE_OPERAND (r1, 0);
4272 : 0 : r2 = TREE_OPERAND (r2, 0);
4273 : : }
4274 : 4130 : else if (TREE_CODE (r2) == BIT_FIELD_REF)
4275 : : return SEMANTICS;
4276 : :
4277 : : /* Now we can compare the address of actual memory access. */
4278 : 4130 : if (!operand_equal_p (r1, r2, OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS))
4279 : : return SEMANTICS;
4280 : : }
4281 : : /* For constant accesses we get more matches by comparing offset only. */
4282 : 156182 : else if (!operand_equal_p (base1, base2,
4283 : : OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS))
4284 : : return SEMANTICS;
4285 : :
4286 : : /* We can't simply use get_object_alignment_1 on the full
4287 : : reference as for accesses with variable indexes this reports
4288 : : too conservative alignment. */
4289 : 160243 : unsigned int align1, align2;
4290 : 160243 : unsigned HOST_WIDE_INT bitpos1, bitpos2;
4291 : 160243 : bool known1 = get_object_alignment_1 (base1, &align1, &bitpos1);
4292 : 160243 : bool known2 = get_object_alignment_1 (base2, &align2, &bitpos2);
4293 : : /* ??? For MEMREF get_object_alignment_1 determines aligned from
4294 : : TYPE_ALIGN but still returns false. This seem to contradict
4295 : : its description. So compare even if alignment is unknown. */
4296 : 160243 : if (known1 != known2
4297 : 160243 : || (bitpos1 != bitpos2 || align1 != align2))
4298 : : return SEMANTICS;
4299 : :
4300 : : /* Now we know that accesses are semantically same. */
4301 : 160028 : int flags = 0;
4302 : :
4303 : : /* ao_ref_base strips inner MEM_REF [&decl], recover from that here. */
4304 : 160028 : tree rbase1 = ref1->ref;
4305 : 160028 : if (rbase1)
4306 : 259940 : while (handled_component_p (rbase1))
4307 : 99912 : rbase1 = TREE_OPERAND (rbase1, 0);
4308 : 160028 : tree rbase2 = ref2->ref;
4309 : 259916 : while (handled_component_p (rbase2))
4310 : 99888 : rbase2 = TREE_OPERAND (rbase2, 0);
4311 : :
4312 : : /* MEM_REFs and TARGET_MEM_REFs record dependence cliques which are used to
4313 : : implement restrict pointers. MR_DEPENDENCE_CLIQUE 0 means no information.
4314 : : Otherwise we need to match bases and cliques. */
4315 : 160028 : if ((((TREE_CODE (rbase1) == MEM_REF || TREE_CODE (rbase1) == TARGET_MEM_REF)
4316 : 90973 : && MR_DEPENDENCE_CLIQUE (rbase1))
4317 : 132038 : || ((TREE_CODE (rbase2) == MEM_REF || TREE_CODE (rbase2) == TARGET_MEM_REF)
4318 : 62983 : && MR_DEPENDENCE_CLIQUE (rbase2)))
4319 : 188018 : && (TREE_CODE (rbase1) != TREE_CODE (rbase2)
4320 : 27990 : || MR_DEPENDENCE_CLIQUE (rbase1) != MR_DEPENDENCE_CLIQUE (rbase2)
4321 : 27910 : || (MR_DEPENDENCE_BASE (rbase1) != MR_DEPENDENCE_BASE (rbase2))))
4322 : : flags |= DEPENDENCE_CLIQUE;
4323 : :
4324 : 160028 : if (!tbaa)
4325 : : return flags;
4326 : :
4327 : : /* Alias sets are not stable across LTO sreaming; be conservative here
4328 : : and compare types the alias sets are ultimately based on. */
4329 : 159999 : if (lto_streaming_safe)
4330 : : {
4331 : 2837 : tree t1 = ao_ref_alias_ptr_type (ref1);
4332 : 2837 : tree t2 = ao_ref_alias_ptr_type (ref2);
4333 : 2837 : if (!alias_ptr_types_compatible_p (t1, t2))
4334 : 15 : flags |= REF_ALIAS_SET;
4335 : :
4336 : 2837 : t1 = ao_ref_base_alias_ptr_type (ref1);
4337 : 2837 : t2 = ao_ref_base_alias_ptr_type (ref2);
4338 : 2837 : if (!alias_ptr_types_compatible_p (t1, t2))
4339 : 23 : flags |= BASE_ALIAS_SET;
4340 : : }
4341 : : else
4342 : : {
4343 : 157162 : if (ao_ref_alias_set (ref1) != ao_ref_alias_set (ref2))
4344 : 0 : flags |= REF_ALIAS_SET;
4345 : 157162 : if (ao_ref_base_alias_set (ref1) != ao_ref_base_alias_set (ref2))
4346 : 0 : flags |= BASE_ALIAS_SET;
4347 : : }
4348 : :
4349 : : /* Access path is used only on non-view-converted references. */
4350 : 159999 : bool view_converted = view_converted_memref_p (rbase1);
4351 : 159999 : if (view_converted_memref_p (rbase2) != view_converted)
4352 : 0 : return flags | ACCESS_PATH;
4353 : 159999 : else if (view_converted)
4354 : : return flags;
4355 : :
4356 : :
4357 : : /* Find start of access paths and look for trailing arrays. */
4358 : 155430 : tree c1 = ref1->ref, c2 = ref2->ref;
4359 : 155430 : tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
4360 : 155430 : int nskipped1 = 0, nskipped2 = 0;
4361 : 155430 : int i = 0;
4362 : :
4363 : 255272 : for (tree p1 = ref1->ref; handled_component_p (p1); p1 = TREE_OPERAND (p1, 0))
4364 : : {
4365 : 99842 : if (component_ref_to_zero_sized_trailing_array_p (p1))
4366 : 133 : end_struct_ref1 = p1;
4367 : 99842 : if (ends_tbaa_access_path_p (p1))
4368 : 4383 : c1 = p1, nskipped1 = i;
4369 : 99842 : i++;
4370 : : }
4371 : 155430 : i = 0;
4372 : 255248 : for (tree p2 = ref2->ref; handled_component_p (p2); p2 = TREE_OPERAND (p2, 0))
4373 : : {
4374 : 99818 : if (component_ref_to_zero_sized_trailing_array_p (p2))
4375 : 140 : end_struct_ref2 = p2;
4376 : 99818 : if (ends_tbaa_access_path_p (p2))
4377 : 4383 : c2 = p2, nskipped2 = i;
4378 : 99818 : i++;
4379 : : }
4380 : :
4381 : : /* For variable accesses we can not rely on offset match below.
4382 : : We know that paths are struturally same, so only check that
4383 : : starts of TBAA paths did not diverge. */
4384 : 155430 : if (!known_eq (ref1->size, ref1->max_size)
4385 : 155430 : && nskipped1 != nskipped2)
4386 : 0 : return flags | ACCESS_PATH;
4387 : :
4388 : : /* Information about trailing refs is used by
4389 : : aliasing_component_refs_p that is applied only if paths
4390 : : has handled components.. */
4391 : 155430 : if (!handled_component_p (c1) && !handled_component_p (c2))
4392 : : ;
4393 : 62937 : else if ((end_struct_ref1 != NULL) != (end_struct_ref2 != NULL))
4394 : 27 : return flags | ACCESS_PATH;
4395 : 155403 : if (end_struct_ref1
4396 : 155526 : && same_type_for_tbaa (TREE_TYPE (end_struct_ref1),
4397 : 123 : TREE_TYPE (end_struct_ref2)) != 1)
4398 : 3 : return flags | ACCESS_PATH;
4399 : :
4400 : : /* Now compare all handled components of the access path.
4401 : : We have three oracles that cares about access paths:
4402 : : - aliasing_component_refs_p
4403 : : - nonoverlapping_refs_since_match_p
4404 : : - nonoverlapping_component_refs_p
4405 : : We need to match things these oracles compare.
4406 : :
4407 : : It is only necessary to check types for compatibility
4408 : : and offsets. Rest of what oracles compares are actual
4409 : : addresses. Those are already known to be same:
4410 : : - for constant accesses we check offsets
4411 : : - for variable accesses we already matched
4412 : : the path lexically with operand_equal_p. */
4413 : 350752 : while (true)
4414 : : {
4415 : 253076 : bool comp1 = handled_component_p (c1);
4416 : 253076 : bool comp2 = handled_component_p (c2);
4417 : :
4418 : 253076 : if (comp1 != comp2)
4419 : 12 : return flags | ACCESS_PATH;
4420 : 253064 : if (!comp1)
4421 : : break;
4422 : :
4423 : 97888 : if (TREE_CODE (c1) != TREE_CODE (c2))
4424 : 0 : return flags | ACCESS_PATH;
4425 : :
4426 : : /* aliasing_component_refs_p attempts to find type match within
4427 : : the paths. For that reason both types needs to be equal
4428 : : with respect to same_type_for_tbaa_p. */
4429 : 97888 : if (!types_equal_for_same_type_for_tbaa_p (TREE_TYPE (c1),
4430 : 97888 : TREE_TYPE (c2),
4431 : : lto_streaming_safe))
4432 : 212 : return flags | ACCESS_PATH;
4433 : 195352 : if (component_ref_to_zero_sized_trailing_array_p (c1)
4434 : 97676 : != component_ref_to_zero_sized_trailing_array_p (c2))
4435 : 0 : return flags | ACCESS_PATH;
4436 : :
4437 : : /* aliasing_matching_component_refs_p compares
4438 : : offsets within the path. Other properties are ignored.
4439 : : Do not bother to verify offsets in variable accesses. Here we
4440 : : already compared them by operand_equal_p so they are
4441 : : structurally same. */
4442 : 97676 : if (!known_eq (ref1->size, ref1->max_size))
4443 : : {
4444 : 4922 : poly_int64 offadj1, sztmc1, msztmc1;
4445 : 4922 : bool reverse1;
4446 : 4922 : get_ref_base_and_extent (c1, &offadj1, &sztmc1, &msztmc1, &reverse1);
4447 : 4922 : poly_int64 offadj2, sztmc2, msztmc2;
4448 : 4922 : bool reverse2;
4449 : 4922 : get_ref_base_and_extent (c2, &offadj2, &sztmc2, &msztmc2, &reverse2);
4450 : 4922 : if (!known_eq (offadj1, offadj2))
4451 : 0 : return flags | ACCESS_PATH;
4452 : : }
4453 : 97676 : c1 = TREE_OPERAND (c1, 0);
4454 : 97676 : c2 = TREE_OPERAND (c2, 0);
4455 : 97676 : }
4456 : : /* Finally test the access type. */
4457 : 155176 : if (!types_equal_for_same_type_for_tbaa_p (TREE_TYPE (c1),
4458 : 155176 : TREE_TYPE (c2),
4459 : : lto_streaming_safe))
4460 : 1537 : return flags | ACCESS_PATH;
4461 : : return flags;
4462 : : }
4463 : :
4464 : : /* Hash REF to HSTATE. If LTO_STREAMING_SAFE do not use alias sets
4465 : : and canonical types. */
4466 : : void
4467 : 8339018 : ao_compare::hash_ao_ref (ao_ref *ref, bool lto_streaming_safe, bool tbaa,
4468 : : inchash::hash &hstate)
4469 : : {
4470 : 8339018 : tree base = ao_ref_base (ref);
4471 : 8339018 : tree tbase = base;
4472 : :
4473 : 8339018 : if (!known_eq (ref->size, ref->max_size))
4474 : : {
4475 : 410252 : tree r = ref->ref;
4476 : 410252 : if (TREE_CODE (r) == COMPONENT_REF
4477 : 410252 : && DECL_BIT_FIELD (TREE_OPERAND (r, 1)))
4478 : : {
4479 : 1729 : tree field = TREE_OPERAND (r, 1);
4480 : 1729 : hash_operand (DECL_FIELD_OFFSET (field), hstate, 0);
4481 : 1729 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, 0);
4482 : 1729 : hash_operand (DECL_SIZE (field), hstate, 0);
4483 : 1729 : r = TREE_OPERAND (r, 0);
4484 : : }
4485 : 410252 : if (TREE_CODE (r) == BIT_FIELD_REF)
4486 : : {
4487 : 1704 : hash_operand (TREE_OPERAND (r, 1), hstate, 0);
4488 : 1704 : hash_operand (TREE_OPERAND (r, 2), hstate, 0);
4489 : 1704 : r = TREE_OPERAND (r, 0);
4490 : : }
4491 : 410252 : hash_operand (TYPE_SIZE (TREE_TYPE (ref->ref)), hstate, 0);
4492 : 410252 : hash_operand (r, hstate, OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS);
4493 : : }
4494 : : else
4495 : : {
4496 : 7928766 : hash_operand (tbase, hstate, OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS);
4497 : 7928766 : hstate.add_poly_int (ref->offset);
4498 : 7928766 : hstate.add_poly_int (ref->size);
4499 : 7928766 : hstate.add_poly_int (ref->max_size);
4500 : : }
4501 : 8339018 : if (!lto_streaming_safe && tbaa)
4502 : : {
4503 : 8024710 : hstate.add_int (ao_ref_alias_set (ref));
4504 : 8024710 : hstate.add_int (ao_ref_base_alias_set (ref));
4505 : : }
4506 : 8339018 : }
|