Line data Source code
1 : /* Alias analysis for trees.
2 : Copyright (C) 2004-2026 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 ssa_name_pt_solution. 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 60347375 : ptr_deref_may_alias_global_p (tree ptr, bool escaped_local_p)
222 : {
223 60347375 : const pt_solution *pt;
224 :
225 : /* If we end up with a pointer constant here that may point
226 : to global memory. */
227 60347375 : if (TREE_CODE (ptr) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (ptr)))
228 : return true;
229 :
230 53172151 : pt = ssa_name_pt_solution (ptr);
231 :
232 : /* If we do not have points-to information for this variable,
233 : we have to punt. */
234 47733435 : if (!pt)
235 : return true;
236 :
237 : /* ??? This does not use TBAA to prune globals ptr may not access. */
238 47093901 : return pt_solution_includes_global (pt, escaped_local_p);
239 : }
240 :
241 : /* Return true, if dereferencing PTR may alias with a local automatic
242 : variable. */
243 :
244 : bool
245 0 : ptr_deref_may_alias_auto_p (tree ptr)
246 : {
247 0 : const pt_solution *pt;
248 :
249 : /* If we end up with a pointer constant here that may point
250 : to local stack memory. */
251 0 : if (TREE_CODE (ptr) != SSA_NAME || !POINTER_TYPE_P (TREE_TYPE (ptr)))
252 : return true;
253 :
254 0 : pt = ssa_name_pt_solution (ptr);
255 :
256 : /* If we do not have points-to information for this variable,
257 : we have to punt. */
258 0 : if (!pt)
259 : return true;
260 :
261 0 : return pt_solution_includes_auto (pt);
262 : }
263 :
264 :
265 : /* Return true if dereferencing PTR may alias DECL.
266 : The caller is responsible for applying TBAA to see if PTR
267 : may access DECL at all. */
268 :
269 : static bool
270 223322223 : ptr_deref_may_alias_decl_p (tree ptr, tree decl)
271 : {
272 223322223 : const pt_solution *pt;
273 :
274 : /* Conversions are irrelevant for points-to information and
275 : data-dependence analysis can feed us those. */
276 223322223 : STRIP_NOPS (ptr);
277 :
278 : /* Anything we do not explicitly handle aliases. */
279 223322223 : if ((TREE_CODE (ptr) != SSA_NAME
280 2399990 : && TREE_CODE (ptr) != ADDR_EXPR
281 1165837 : && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
282 222156386 : || !POINTER_TYPE_P (TREE_TYPE (ptr))
283 445478201 : || (!VAR_P (decl)
284 : && TREE_CODE (decl) != PARM_DECL
285 : && TREE_CODE (decl) != RESULT_DECL))
286 : return true;
287 :
288 : /* Disregard pointer offsetting. */
289 222154791 : if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
290 : {
291 0 : do
292 : {
293 0 : ptr = TREE_OPERAND (ptr, 0);
294 : }
295 0 : while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
296 : return ptr_deref_may_alias_decl_p (ptr, decl);
297 : }
298 :
299 : /* ADDR_EXPR pointers either just offset another pointer or directly
300 : specify the pointed-to set. */
301 222154791 : if (TREE_CODE (ptr) == ADDR_EXPR)
302 : {
303 1233028 : tree base = get_base_address (TREE_OPERAND (ptr, 0));
304 1233028 : if (base
305 1233028 : && (TREE_CODE (base) == MEM_REF
306 1233028 : || TREE_CODE (base) == TARGET_MEM_REF))
307 19926 : ptr = TREE_OPERAND (base, 0);
308 1213102 : else if (base
309 1213102 : && DECL_P (base))
310 1212751 : return compare_base_decls (base, decl) != 0;
311 351 : else if (base
312 351 : && CONSTANT_CLASS_P (base))
313 : return false;
314 : else
315 0 : return true;
316 : }
317 :
318 : /* Non-aliased variables cannot be pointed to. */
319 220941689 : if (!may_be_aliased (decl))
320 : return false;
321 :
322 : /* From here we require a SSA name pointer. Anything else aliases. */
323 83346020 : if (TREE_CODE (ptr) != SSA_NAME
324 83346020 : || !POINTER_TYPE_P (TREE_TYPE (ptr)))
325 : return true;
326 :
327 : /* If we do not have useful points-to information for this pointer
328 : we cannot disambiguate anything else. */
329 83346020 : pt = ssa_name_pt_solution (ptr);
330 82384884 : if (!pt)
331 : return true;
332 :
333 82110189 : return pt_solution_includes (pt, decl);
334 : }
335 :
336 : /* Return true if dereferenced PTR1 and PTR2 may alias.
337 : The caller is responsible for applying TBAA to see if accesses
338 : through PTR1 and PTR2 may conflict at all. */
339 :
340 : bool
341 71512683 : ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
342 : {
343 72725475 : const pt_solution *pt1, *pt2;
344 :
345 : /* Conversions are irrelevant for points-to information and
346 : data-dependence analysis can feed us those. */
347 72725475 : STRIP_NOPS (ptr1);
348 72725475 : STRIP_NOPS (ptr2);
349 :
350 : /* Disregard pointer offsetting. */
351 72725475 : if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
352 : {
353 485683 : do
354 : {
355 485683 : ptr1 = TREE_OPERAND (ptr1, 0);
356 : }
357 485683 : while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
358 : return ptr_derefs_may_alias_p (ptr1, ptr2);
359 : }
360 72239792 : if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
361 : {
362 498062 : do
363 : {
364 498062 : ptr2 = TREE_OPERAND (ptr2, 0);
365 : }
366 498062 : while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
367 : return ptr_derefs_may_alias_p (ptr1, ptr2);
368 : }
369 :
370 : /* ADDR_EXPR pointers either just offset another pointer or directly
371 : specify the pointed-to set. */
372 71741730 : if (TREE_CODE (ptr1) == ADDR_EXPR)
373 : {
374 908213 : tree base = get_base_address (TREE_OPERAND (ptr1, 0));
375 908213 : if (base
376 908213 : && (TREE_CODE (base) == MEM_REF
377 908213 : || TREE_CODE (base) == TARGET_MEM_REF))
378 116152 : return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
379 792061 : else if (base
380 792061 : && DECL_P (base))
381 790864 : return ptr_deref_may_alias_decl_p (ptr2, base);
382 : /* Try ptr2 when ptr1 points to a constant. */
383 : else if (base
384 1197 : && !CONSTANT_CLASS_P (base))
385 : return true;
386 : }
387 70834714 : if (TREE_CODE (ptr2) == ADDR_EXPR)
388 : {
389 359958 : tree base = get_base_address (TREE_OPERAND (ptr2, 0));
390 359958 : if (base
391 359958 : && (TREE_CODE (base) == MEM_REF
392 359958 : || TREE_CODE (base) == TARGET_MEM_REF))
393 112895 : return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
394 247063 : else if (base
395 247063 : && DECL_P (base))
396 246794 : return ptr_deref_may_alias_decl_p (ptr1, base);
397 : else
398 : return true;
399 : }
400 :
401 : /* From here we require SSA name pointers. Anything else aliases. */
402 70474756 : if (TREE_CODE (ptr1) != SSA_NAME
403 70313677 : || TREE_CODE (ptr2) != SSA_NAME
404 70289410 : || !POINTER_TYPE_P (TREE_TYPE (ptr1))
405 140754365 : || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
406 : return true;
407 :
408 : /* We may end up with two empty points-to solutions for two same pointers.
409 : In this case we still want to say both pointers alias, so shortcut
410 : that here. */
411 70279430 : if (ptr1 == ptr2)
412 : return true;
413 :
414 : /* If we do not have useful points-to information for either pointer
415 : we cannot disambiguate anything else. */
416 66339440 : pt1 = ssa_name_pt_solution (ptr1);
417 66339440 : pt2 = ssa_name_pt_solution (ptr2);
418 66339440 : if (!pt1 || !pt2)
419 : return true;
420 :
421 : /* ??? This does not use TBAA to prune decls from the intersection
422 : that not both pointers may access. */
423 62797541 : return pt_solutions_intersect (pt1, pt2);
424 : }
425 :
426 : /* Return true if dereferencing PTR may alias *REF.
427 : The caller is responsible for applying TBAA to see if PTR
428 : may access *REF at all. */
429 :
430 : static bool
431 1735326 : ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
432 : {
433 1735326 : tree base = ao_ref_base (ref);
434 :
435 1735326 : if (TREE_CODE (base) == MEM_REF
436 1735326 : || TREE_CODE (base) == TARGET_MEM_REF)
437 143613 : return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
438 1591713 : else if (DECL_P (base))
439 1589086 : return ptr_deref_may_alias_decl_p (ptr, base);
440 :
441 : return true;
442 : }
443 :
444 : /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
445 :
446 : bool
447 59248173 : ptrs_compare_unequal (tree ptr1, tree ptr2)
448 : {
449 : /* First resolve the pointers down to a SSA name pointer base or
450 : a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitly does
451 : not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
452 : or STRING_CSTs which needs points-to adjustments to track them
453 : in the points-to sets. */
454 59248173 : tree obj1 = NULL_TREE;
455 59248173 : tree obj2 = NULL_TREE;
456 59248173 : if (TREE_CODE (ptr1) == ADDR_EXPR)
457 : {
458 58236 : tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
459 58236 : if (! tem)
460 : return false;
461 58236 : if (VAR_P (tem)
462 : || TREE_CODE (tem) == PARM_DECL
463 : || TREE_CODE (tem) == RESULT_DECL)
464 : obj1 = tem;
465 : else if (TREE_CODE (tem) == MEM_REF)
466 9420 : ptr1 = TREE_OPERAND (tem, 0);
467 : }
468 59248173 : if (TREE_CODE (ptr2) == ADDR_EXPR)
469 : {
470 9821826 : tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
471 9821826 : if (! tem)
472 : return false;
473 9821826 : if (VAR_P (tem)
474 : || TREE_CODE (tem) == PARM_DECL
475 : || TREE_CODE (tem) == RESULT_DECL)
476 : obj2 = tem;
477 : else if (TREE_CODE (tem) == MEM_REF)
478 157333 : ptr2 = TREE_OPERAND (tem, 0);
479 : }
480 :
481 : /* Canonicalize ptr vs. object. */
482 59248173 : if (TREE_CODE (ptr1) == SSA_NAME && obj2)
483 : {
484 : std::swap (ptr1, ptr2);
485 : std::swap (obj1, obj2);
486 : }
487 :
488 59248173 : if (obj1 && obj2)
489 : /* Other code handles this correctly, no need to duplicate it here. */;
490 6226005 : else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
491 : {
492 6214670 : const pt_solution *pt = ssa_name_pt_solution (ptr2);
493 : /* We may not use restrict to optimize pointer comparisons.
494 : See PR71062. So we have to assume that restrict-pointed-to
495 : may be in fact obj1. */
496 5440743 : if (!pt
497 4981748 : || pt->vars_contains_restrict
498 4974130 : || pt->vars_contains_interposable)
499 : return false;
500 4313293 : if (VAR_P (obj1)
501 4313293 : && (TREE_STATIC (obj1) || DECL_EXTERNAL (obj1)))
502 : {
503 1221099 : varpool_node *node = varpool_node::get (obj1);
504 : /* If obj1 may bind to NULL give up (see below). */
505 1221099 : if (! node
506 1221099 : || ! node->nonzero_address ()
507 2442161 : || ! decl_binds_to_current_def_p (obj1))
508 : return false;
509 : }
510 3393199 : return !pt_solution_includes (pt, obj1);
511 : }
512 53020551 : else if (TREE_CODE (ptr1) == SSA_NAME)
513 : {
514 46569894 : const pt_solution *pt1 = ssa_name_pt_solution (ptr1);
515 40674458 : if (!pt1
516 35646021 : || pt1->vars_contains_restrict
517 34622014 : || pt1->vars_contains_interposable)
518 : return false;
519 32808377 : if (integer_zerop (ptr2) && ptr_nonnull_p (ptr1))
520 : return true;
521 32766387 : if (TREE_CODE (ptr2) == SSA_NAME)
522 : {
523 10687937 : const pt_solution *pt2 = ssa_name_pt_solution (ptr2);
524 10127369 : if (!pt2
525 10004782 : || pt2->vars_contains_restrict
526 9998680 : || pt2->vars_contains_interposable)
527 : return false;
528 18235186 : if ((ptr_nonnull_p (ptr1) || ptr_nonnull_p (ptr2))
529 : /* ??? We do not represent FUNCTION_DECL and LABEL_DECL
530 : in pt.vars but only set pt.vars_contains_nonlocal. This
531 : makes compares involving those and other nonlocals
532 : imprecise. */
533 4288245 : && (!pt1->vars_contains_nonlocal
534 61314 : || !pt2->vars_contains_nonlocal)
535 14169800 : && (!pt_solution_includes_const_pool (pt1)
536 3943073 : || !pt_solution_includes_const_pool (pt2)))
537 339527 : return !pt_solutions_intersect (pt1, pt2);
538 : }
539 : }
540 :
541 : return false;
542 : }
543 :
544 : /* Returns whether reference REF to BASE may refer to global memory.
545 : When ESCAPED_LOCAL_P is true escaped local memory is also considered
546 : global. */
547 :
548 : static bool
549 57191228 : ref_may_alias_global_p_1 (tree base, bool escaped_local_p)
550 : {
551 57191228 : if (DECL_P (base))
552 44675650 : return (is_global_var (base)
553 44675650 : || (escaped_local_p
554 1682556 : && pt_solution_includes (&cfun->gimple_df->escaped_return,
555 : base)));
556 12515578 : else if (TREE_CODE (base) == MEM_REF
557 12515578 : || TREE_CODE (base) == TARGET_MEM_REF)
558 12503380 : return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0),
559 12503380 : escaped_local_p);
560 : return true;
561 : }
562 :
563 : bool
564 11609809 : ref_may_alias_global_p (ao_ref *ref, bool escaped_local_p)
565 : {
566 11609809 : tree base = ao_ref_base (ref);
567 11609809 : return ref_may_alias_global_p_1 (base, escaped_local_p);
568 : }
569 :
570 : bool
571 45581419 : ref_may_alias_global_p (tree ref, bool escaped_local_p)
572 : {
573 45581419 : tree base = get_base_address (ref);
574 45581419 : return ref_may_alias_global_p_1 (base, escaped_local_p);
575 : }
576 :
577 : /* Return true whether STMT may clobber global memory.
578 : When ESCAPED_LOCAL_P is true escaped local memory is also considered
579 : global. */
580 :
581 : bool
582 185687247 : stmt_may_clobber_global_p (gimple *stmt, bool escaped_local_p)
583 : {
584 185687247 : tree lhs;
585 :
586 339119376 : if (!gimple_vdef (stmt))
587 : return false;
588 :
589 : /* ??? We can ask the oracle whether an artificial pointer
590 : dereference with a pointer with points-to information covering
591 : all global memory (what about non-address taken memory?) maybe
592 : clobbered by this call. As there is at the moment no convenient
593 : way of doing that without generating garbage do some manual
594 : checking instead.
595 : ??? We could make a NULL ao_ref argument to the various
596 : predicates special, meaning any global memory. */
597 :
598 45818043 : switch (gimple_code (stmt))
599 : {
600 45581419 : case GIMPLE_ASSIGN:
601 45581419 : lhs = gimple_assign_lhs (stmt);
602 45581419 : return (TREE_CODE (lhs) != SSA_NAME
603 45581419 : && ref_may_alias_global_p (lhs, escaped_local_p));
604 : case GIMPLE_CALL:
605 : return true;
606 : default:
607 : return true;
608 : }
609 : }
610 :
611 :
612 : /* Dump alias information on FILE. */
613 :
614 : void
615 283 : dump_alias_info (FILE *file)
616 : {
617 283 : unsigned i;
618 283 : tree ptr;
619 283 : const char *funcname
620 283 : = lang_hooks.decl_printable_name (current_function_decl, 2);
621 283 : tree var;
622 :
623 283 : fprintf (file, "\n\nAlias information for %s\n\n", funcname);
624 :
625 283 : fprintf (file, "Aliased symbols\n\n");
626 :
627 1122 : FOR_EACH_LOCAL_DECL (cfun, i, var)
628 : {
629 585 : if (may_be_aliased (var))
630 351 : dump_variable (file, var);
631 : }
632 :
633 283 : fprintf (file, "\nCall clobber information\n");
634 :
635 283 : fprintf (file, "\nESCAPED");
636 283 : dump_points_to_solution (file, &cfun->gimple_df->escaped);
637 :
638 283 : fprintf (file, "\nESCAPED_RETURN");
639 283 : dump_points_to_solution (file, &cfun->gimple_df->escaped_return);
640 :
641 283 : fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
642 :
643 3336 : FOR_EACH_SSA_NAME (i, ptr, cfun)
644 : {
645 2550 : const pt_solution *pt;
646 :
647 4656 : if (!POINTER_TYPE_P (TREE_TYPE (ptr))
648 2597 : || SSA_NAME_IN_FREE_LIST (ptr))
649 2059 : continue;
650 :
651 3261 : pt = ssa_name_pt_solution (ptr);
652 485 : if (pt)
653 485 : dump_points_to_info_for (file, ptr);
654 : }
655 :
656 283 : fprintf (file, "\n");
657 283 : }
658 :
659 :
660 : /* Dump alias information on stderr. */
661 :
662 : DEBUG_FUNCTION void
663 0 : debug_alias_info (void)
664 : {
665 0 : dump_alias_info (stderr);
666 0 : }
667 :
668 :
669 : /* Dump the points-to set *PT into FILE. */
670 :
671 : void
672 1051 : dump_points_to_solution (FILE *file, const pt_solution *pt)
673 : {
674 1051 : if (pt->anything)
675 3 : fprintf (file, ", points-to anything");
676 :
677 1051 : if (pt->nonlocal)
678 633 : fprintf (file, ", points-to non-local");
679 :
680 1051 : if (pt->escaped)
681 420 : fprintf (file, ", points-to escaped");
682 :
683 1051 : if (pt->ipa_escaped)
684 0 : fprintf (file, ", points-to unit escaped");
685 :
686 1051 : if (pt->const_pool)
687 0 : fprintf (file, ", points-to const-pool");
688 :
689 1051 : if (pt->vars)
690 : {
691 1048 : fprintf (file, ", points-to vars: ");
692 1048 : dump_decl_set (file, pt->vars);
693 1048 : if (pt->vars_contains_nonlocal
694 914 : || pt->vars_contains_escaped
695 837 : || pt->vars_contains_escaped_heap
696 837 : || pt->vars_contains_restrict
697 837 : || pt->vars_contains_interposable)
698 : {
699 211 : const char *comma = "";
700 211 : fprintf (file, " (");
701 211 : if (pt->vars_contains_nonlocal)
702 : {
703 134 : fprintf (file, "nonlocal");
704 134 : comma = ", ";
705 : }
706 211 : if (pt->vars_contains_escaped)
707 : {
708 139 : fprintf (file, "%sescaped", comma);
709 139 : comma = ", ";
710 : }
711 211 : if (pt->vars_contains_escaped_heap)
712 : {
713 0 : fprintf (file, "%sescaped heap", comma);
714 0 : comma = ", ";
715 : }
716 211 : if (pt->vars_contains_restrict)
717 : {
718 58 : fprintf (file, "%srestrict", comma);
719 58 : comma = ", ";
720 : }
721 211 : if (pt->vars_contains_interposable)
722 0 : fprintf (file, "%sinterposable", comma);
723 211 : fprintf (file, ")");
724 : }
725 : }
726 1051 : }
727 :
728 :
729 : /* Unified dump function for pt_solution. */
730 :
731 : DEBUG_FUNCTION void
732 0 : debug (pt_solution &ref)
733 : {
734 0 : dump_points_to_solution (stderr, &ref);
735 0 : }
736 :
737 : DEBUG_FUNCTION void
738 0 : debug (pt_solution *ptr)
739 : {
740 0 : if (ptr)
741 0 : debug (*ptr);
742 : else
743 0 : fprintf (stderr, "<nil>\n");
744 0 : }
745 :
746 :
747 : /* Dump points-to information for SSA_NAME PTR into FILE. */
748 :
749 : void
750 485 : dump_points_to_info_for (FILE *file, tree ptr)
751 : {
752 485 : const pt_solution *pt = ssa_name_pt_solution (ptr);
753 :
754 485 : print_generic_expr (file, ptr, dump_flags);
755 :
756 485 : if (pt)
757 485 : dump_points_to_solution (file, pt);
758 : else
759 0 : fprintf (file, ", points-to anything");
760 :
761 485 : fprintf (file, "\n");
762 485 : }
763 :
764 :
765 : /* Dump points-to information for VAR into stderr. */
766 :
767 : DEBUG_FUNCTION void
768 0 : debug_points_to_info_for (tree var)
769 : {
770 0 : dump_points_to_info_for (stderr, var);
771 0 : }
772 :
773 :
774 : /* Initializes the alias-oracle reference representation *R from REF. */
775 :
776 : void
777 2955194619 : ao_ref_init (ao_ref *r, tree ref)
778 : {
779 2955194619 : r->ref = ref;
780 2955194619 : r->base = NULL_TREE;
781 2955194619 : r->offset = 0;
782 2955194619 : r->size = -1;
783 2955194619 : r->max_size = -1;
784 2955194619 : r->ref_alias_set = -1;
785 2955194619 : r->base_alias_set = -1;
786 2955194619 : r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
787 2955194619 : }
788 :
789 : /* Returns the base object of the memory reference *REF. */
790 :
791 : tree
792 5784236802 : ao_ref_base (ao_ref *ref)
793 : {
794 5784236802 : bool reverse;
795 :
796 5784236802 : if (ref->base)
797 : return ref->base;
798 2682485605 : ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
799 : &ref->max_size, &reverse);
800 2682485605 : return ref->base;
801 : }
802 :
803 : /* Returns the base object alias set of the memory reference *REF. */
804 :
805 : alias_set_type
806 1059265177 : ao_ref_base_alias_set (ao_ref *ref)
807 : {
808 1059265177 : tree base_ref;
809 1059265177 : if (ref->base_alias_set != -1)
810 : return ref->base_alias_set;
811 814411776 : if (!ref->ref)
812 : return 0;
813 775943462 : base_ref = ref->ref;
814 775943462 : if (TREE_CODE (base_ref) == WITH_SIZE_EXPR)
815 4 : base_ref = TREE_OPERAND (base_ref, 0);
816 1250871524 : while (handled_component_p (base_ref))
817 474928062 : base_ref = TREE_OPERAND (base_ref, 0);
818 775943462 : ref->base_alias_set = get_alias_set (base_ref);
819 775943462 : return ref->base_alias_set;
820 : }
821 :
822 : /* Returns the reference alias set of the memory reference *REF. */
823 :
824 : alias_set_type
825 1346435012 : ao_ref_alias_set (ao_ref *ref)
826 : {
827 1346435012 : if (ref->ref_alias_set != -1)
828 : return ref->ref_alias_set;
829 542724186 : if (!ref->ref)
830 : return 0;
831 542724184 : ref->ref_alias_set = get_alias_set (ref->ref);
832 542724184 : return ref->ref_alias_set;
833 : }
834 :
835 : /* Returns a type satisfying
836 : get_deref_alias_set (type) == ao_ref_base_alias_set (REF). */
837 :
838 : tree
839 351202 : ao_ref_base_alias_ptr_type (ao_ref *ref)
840 : {
841 351202 : tree base_ref;
842 :
843 351202 : if (!ref->ref)
844 : return NULL_TREE;
845 351202 : base_ref = ref->ref;
846 351202 : if (TREE_CODE (base_ref) == WITH_SIZE_EXPR)
847 0 : base_ref = TREE_OPERAND (base_ref, 0);
848 480815 : while (handled_component_p (base_ref))
849 129613 : base_ref = TREE_OPERAND (base_ref, 0);
850 351202 : tree ret = reference_alias_ptr_type (base_ref);
851 351202 : return ret;
852 : }
853 :
854 : /* Returns a type satisfying
855 : get_deref_alias_set (type) == ao_ref_alias_set (REF). */
856 :
857 : tree
858 351202 : ao_ref_alias_ptr_type (ao_ref *ref)
859 : {
860 351202 : if (!ref->ref)
861 : return NULL_TREE;
862 351202 : tree ret = reference_alias_ptr_type (ref->ref);
863 351202 : return ret;
864 : }
865 :
866 : /* Return the alignment of the access *REF and store it in the *ALIGN
867 : and *BITPOS pairs. Returns false if no alignment could be determined.
868 : See get_object_alignment_2 for details. */
869 :
870 : bool
871 100340 : ao_ref_alignment (ao_ref *ref, unsigned int *align,
872 : unsigned HOST_WIDE_INT *bitpos)
873 : {
874 100340 : if (ref->ref)
875 98683 : return get_object_alignment_1 (ref->ref, align, bitpos);
876 :
877 : /* When we just have ref->base we cannot use get_object_alignment since
878 : that will eventually use the type of the apparent access while for
879 : example ao_ref_init_from_ptr_and_range is not careful to adjust that. */
880 1657 : *align = BITS_PER_UNIT;
881 1657 : HOST_WIDE_INT offset;
882 1657 : if (!ref->offset.is_constant (&offset)
883 1657 : || !get_object_alignment_2 (ref->base, align, bitpos, true))
884 : return false;
885 1404 : *bitpos += (unsigned HOST_WIDE_INT)offset * BITS_PER_UNIT;
886 1404 : *bitpos = *bitpos & (*align - 1);
887 1404 : return true;
888 : }
889 :
890 : /* Init an alias-oracle reference representation from a gimple pointer
891 : PTR a range specified by OFFSET, SIZE and MAX_SIZE under the assumption
892 : that RANGE_KNOWN is set.
893 :
894 : The access is assumed to be only to or after of the pointer target adjusted
895 : by the offset, not before it (even in the case RANGE_KNOWN is false). */
896 :
897 : void
898 42948728 : ao_ref_init_from_ptr_and_range (ao_ref *ref, tree ptr,
899 : bool range_known,
900 : poly_int64 offset,
901 : poly_int64 size,
902 : poly_int64 max_size)
903 : {
904 42948728 : poly_int64 t, extra_offset = 0;
905 :
906 42948728 : ref->ref = NULL_TREE;
907 42948728 : if (TREE_CODE (ptr) == SSA_NAME)
908 : {
909 28166030 : auto *stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (ptr));
910 20193956 : if (stmt
911 20193956 : && gimple_assign_single_p (stmt)
912 11434697 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
913 4419762 : ptr = gimple_assign_rhs1 (stmt);
914 23746268 : else if (stmt
915 15774194 : && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
916 6075905 : && ptrdiff_tree_p (gimple_assign_rhs2 (stmt), &t)
917 24173410 : && coeffs_in_range_p (t,
918 : -HOST_WIDE_INT_MAX / BITS_PER_UNIT,
919 : HOST_WIDE_INT_MAX / BITS_PER_UNIT))
920 : {
921 426894 : ptr = gimple_assign_rhs1 (stmt);
922 426894 : extra_offset = t * BITS_PER_UNIT;
923 : }
924 : }
925 :
926 42948728 : if (TREE_CODE (ptr) == ADDR_EXPR)
927 : {
928 19132411 : ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
929 19132411 : if (ref->base
930 35127208 : && coeffs_in_range_p (t, -HOST_WIDE_INT_MAX / BITS_PER_UNIT,
931 : HOST_WIDE_INT_MAX / BITS_PER_UNIT))
932 15994619 : ref->offset = BITS_PER_UNIT * t;
933 : else
934 : {
935 3137792 : range_known = false;
936 3137792 : ref->offset = 0;
937 3137792 : ref->base = get_base_address (TREE_OPERAND (ptr, 0));
938 : }
939 : }
940 : else
941 : {
942 23816317 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
943 23816317 : ref->base = build2 (MEM_REF, char_type_node,
944 : ptr, null_pointer_node);
945 23816317 : ref->offset = 0;
946 : }
947 42948728 : ref->offset += extra_offset + offset;
948 42948728 : if (range_known)
949 : {
950 22662545 : ref->max_size = max_size;
951 22662545 : ref->size = size;
952 : }
953 : else
954 20286183 : ref->max_size = ref->size = -1;
955 42948728 : ref->ref_alias_set = 0;
956 42948728 : ref->base_alias_set = 0;
957 42948728 : ref->volatile_p = false;
958 42948728 : }
959 :
960 : /* Init an alias-oracle reference representation from a gimple pointer
961 : PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
962 : size is assumed to be unknown. The access is assumed to be only
963 : to or after of the pointer target, not before it. */
964 :
965 : void
966 11926877 : ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
967 : {
968 11926877 : poly_int64 size_hwi;
969 11926877 : if (size
970 6441388 : && poly_int_tree_p (size, &size_hwi)
971 17655157 : && coeffs_in_range_p (size_hwi, 0, HOST_WIDE_INT_MAX / BITS_PER_UNIT))
972 : {
973 5727588 : size_hwi = size_hwi * BITS_PER_UNIT;
974 5727588 : ao_ref_init_from_ptr_and_range (ref, ptr, true, 0, size_hwi, size_hwi);
975 : }
976 : else
977 6199289 : ao_ref_init_from_ptr_and_range (ref, ptr, false, 0, -1, -1);
978 11926877 : }
979 :
980 : /* S1 and S2 are TYPE_SIZE or DECL_SIZE. Compare them:
981 : Return -1 if S1 < S2
982 : Return 1 if S1 > S2
983 : Return 0 if equal or incomparable. */
984 :
985 : static int
986 9036641 : compare_sizes (tree s1, tree s2)
987 : {
988 9036641 : if (!s1 || !s2)
989 : return 0;
990 :
991 9027592 : poly_uint64 size1;
992 9027592 : poly_uint64 size2;
993 :
994 9027592 : if (!poly_int_tree_p (s1, &size1) || !poly_int_tree_p (s2, &size2))
995 : return 0;
996 9027110 : if (known_lt (size1, size2))
997 : return -1;
998 6491362 : if (known_lt (size2, size1))
999 2901102 : return 1;
1000 : return 0;
1001 : }
1002 :
1003 : /* Compare TYPE1 and TYPE2 by its size.
1004 : Return -1 if size of TYPE1 < size of TYPE2
1005 : Return 1 if size of TYPE1 > size of TYPE2
1006 : Return 0 if types are of equal sizes or we can not compare them. */
1007 :
1008 : static int
1009 7645479 : compare_type_sizes (tree type1, tree type2)
1010 : {
1011 : /* Be conservative for arrays and vectors. We want to support partial
1012 : overlap on int[3] and int[3] as tested in gcc.dg/torture/alias-2.c. */
1013 7645479 : while (TREE_CODE (type1) == ARRAY_TYPE
1014 8427127 : || VECTOR_TYPE_P (type1))
1015 781648 : type1 = TREE_TYPE (type1);
1016 7835399 : while (TREE_CODE (type2) == ARRAY_TYPE
1017 7835399 : || VECTOR_TYPE_P (type2))
1018 189920 : type2 = TREE_TYPE (type2);
1019 7645479 : return compare_sizes (TYPE_SIZE (type1), TYPE_SIZE (type2));
1020 : }
1021 :
1022 : /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
1023 : purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
1024 : decide. */
1025 :
1026 : static inline int
1027 886455257 : same_type_for_tbaa (tree type1, tree type2)
1028 : {
1029 886455257 : type1 = TYPE_MAIN_VARIANT (type1);
1030 886455257 : type2 = TYPE_MAIN_VARIANT (type2);
1031 :
1032 : /* Handle the most common case first. */
1033 886455257 : if (type1 == type2)
1034 : return 1;
1035 :
1036 : /* If we would have to do structural comparison bail out. */
1037 144312580 : if (TYPE_STRUCTURAL_EQUALITY_P (type1)
1038 144312580 : || TYPE_STRUCTURAL_EQUALITY_P (type2))
1039 : return -1;
1040 :
1041 : /* Compare the canonical types. */
1042 91359219 : if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
1043 : return 1;
1044 :
1045 : /* ??? Array types are not properly unified in all cases as we have
1046 : spurious changes in the index types for example. Removing this
1047 : causes all sorts of problems with the Fortran frontend. */
1048 90383028 : if (TREE_CODE (type1) == ARRAY_TYPE
1049 5197556 : && TREE_CODE (type2) == ARRAY_TYPE)
1050 : return -1;
1051 :
1052 : /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
1053 : object of one of its constrained subtypes, e.g. when a function with an
1054 : unconstrained parameter passed by reference is called on an object and
1055 : inlined. But, even in the case of a fixed size, type and subtypes are
1056 : not equivalent enough as to share the same TYPE_CANONICAL, since this
1057 : would mean that conversions between them are useless, whereas they are
1058 : not (e.g. type and subtypes can have different modes). So, in the end,
1059 : they are only guaranteed to have the same alias set. */
1060 90076685 : alias_set_type set1 = get_alias_set (type1);
1061 90076685 : alias_set_type set2 = get_alias_set (type2);
1062 90076685 : if (set1 == set2)
1063 : return -1;
1064 :
1065 : /* Pointers to void are considered compatible with all other pointers,
1066 : so for two pointers see what the alias set resolution thinks. */
1067 50760286 : if (POINTER_TYPE_P (type1)
1068 11465731 : && POINTER_TYPE_P (type2)
1069 50937142 : && alias_sets_conflict_p (set1, set2))
1070 176856 : return -1;
1071 :
1072 : /* The types are known to be not equal. */
1073 : return 0;
1074 : }
1075 :
1076 : /* Return true if TYPE is a composite type (i.e. we may apply one of handled
1077 : components on it). */
1078 :
1079 : static bool
1080 1960356 : type_has_components_p (tree type)
1081 : {
1082 1960356 : return AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)
1083 1960356 : || TREE_CODE (type) == COMPLEX_TYPE;
1084 : }
1085 :
1086 : /* MATCH1 and MATCH2 which are part of access path of REF1 and REF2
1087 : respectively are either pointing to same address or are completely
1088 : disjoint. If PARTIAL_OVERLAP is true, assume that outermost arrays may
1089 : just partly overlap.
1090 :
1091 : Try to disambiguate using the access path starting from the match
1092 : and return false if there is no conflict.
1093 :
1094 : Helper for aliasing_component_refs_p. */
1095 :
1096 : static bool
1097 963552 : aliasing_matching_component_refs_p (tree match1, tree ref1,
1098 : poly_int64 offset1, poly_int64 max_size1,
1099 : tree match2, tree ref2,
1100 : poly_int64 offset2, poly_int64 max_size2,
1101 : bool partial_overlap)
1102 : {
1103 963552 : poly_int64 offadj, sztmp, msztmp;
1104 963552 : bool reverse;
1105 :
1106 963552 : if (!partial_overlap)
1107 : {
1108 963548 : get_ref_base_and_extent (match2, &offadj, &sztmp, &msztmp, &reverse);
1109 963548 : offset2 -= offadj;
1110 963548 : get_ref_base_and_extent (match1, &offadj, &sztmp, &msztmp, &reverse);
1111 963548 : offset1 -= offadj;
1112 963548 : if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1113 : {
1114 62290 : ++alias_stats.aliasing_component_refs_p_no_alias;
1115 62290 : return false;
1116 : }
1117 : }
1118 :
1119 901262 : int cmp = nonoverlapping_refs_since_match_p (match1, ref1, match2, ref2,
1120 : partial_overlap);
1121 901262 : if (cmp == 1
1122 901262 : || (cmp == -1 && nonoverlapping_component_refs_p (ref1, ref2)))
1123 : {
1124 450 : ++alias_stats.aliasing_component_refs_p_no_alias;
1125 450 : return false;
1126 : }
1127 900812 : ++alias_stats.aliasing_component_refs_p_may_alias;
1128 900812 : return true;
1129 : }
1130 :
1131 : /* Return true if REF is reference to zero sized trailing array. I.e.
1132 : struct foo {int bar; int array[0];} *fooptr;
1133 : fooptr->array. */
1134 :
1135 : static bool
1136 6248724 : component_ref_to_zero_sized_trailing_array_p (tree ref)
1137 : {
1138 6248724 : return (TREE_CODE (ref) == COMPONENT_REF
1139 5601518 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE
1140 131840 : && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))
1141 47040 : || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))))
1142 6333597 : && array_ref_flexible_size_p (ref));
1143 : }
1144 :
1145 : /* Worker for aliasing_component_refs_p. Most parameters match parameters of
1146 : aliasing_component_refs_p.
1147 :
1148 : Walk access path REF2 and try to find type matching TYPE1
1149 : (which is a start of possibly aliasing access path REF1).
1150 : If match is found, try to disambiguate.
1151 :
1152 : Return 0 for successful disambiguation.
1153 : Return 1 if match was found but disambiguation failed
1154 : Return -1 if there is no match.
1155 : In this case MAYBE_MATCH is set to 0 if there is no type matching TYPE1
1156 : in access patch REF2 and -1 if we are not sure. */
1157 :
1158 : static int
1159 2650017 : aliasing_component_refs_walk (tree ref1, tree type1, tree base1,
1160 : poly_int64 offset1, poly_int64 max_size1,
1161 : tree end_struct_ref1,
1162 : tree ref2, tree base2,
1163 : poly_int64 offset2, poly_int64 max_size2,
1164 : bool *maybe_match)
1165 : {
1166 2650017 : tree ref = ref2;
1167 2650017 : int same_p = 0;
1168 :
1169 7397507 : while (true)
1170 : {
1171 : /* We walk from inner type to the outer types. If type we see is
1172 : already too large to be part of type1, terminate the search. */
1173 5023762 : int cmp = compare_type_sizes (type1, TREE_TYPE (ref));
1174 :
1175 5023762 : if (cmp < 0
1176 5023762 : && (!end_struct_ref1
1177 60 : || compare_type_sizes (TREE_TYPE (end_struct_ref1),
1178 60 : TREE_TYPE (ref)) < 0))
1179 : break;
1180 : /* If types may be of same size, see if we can decide about their
1181 : equality. */
1182 3662551 : if (cmp == 0)
1183 : {
1184 2672190 : same_p = same_type_for_tbaa (TREE_TYPE (ref), type1);
1185 2672190 : if (same_p == 1)
1186 : break;
1187 : /* In case we can't decide whether types are same try to
1188 : continue looking for the exact match.
1189 : Remember however that we possibly saw a match
1190 : to bypass the access path continuations tests we do later. */
1191 1708638 : if (same_p == -1)
1192 629291 : *maybe_match = true;
1193 : }
1194 2698999 : if (!handled_component_p (ref))
1195 : break;
1196 2373745 : ref = TREE_OPERAND (ref, 0);
1197 2373745 : }
1198 2650017 : if (same_p == 1)
1199 : {
1200 963552 : bool partial_overlap = false;
1201 :
1202 : /* We assume that arrays can overlap by multiple of their elements
1203 : size as tested in gcc.dg/torture/alias-2.c.
1204 : This partial overlap happen only when both arrays are bases of
1205 : the access and not contained within another component ref.
1206 : To be safe we also assume partial overlap for VLAs. */
1207 963552 : if (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
1208 963552 : && (!TYPE_SIZE (TREE_TYPE (base1))
1209 1856 : || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) != INTEGER_CST
1210 1856 : || ref == base2))
1211 : {
1212 : /* Setting maybe_match to true triggers
1213 : nonoverlapping_component_refs_p test later that still may do
1214 : useful disambiguation. */
1215 4 : *maybe_match = true;
1216 4 : partial_overlap = true;
1217 : }
1218 963552 : return aliasing_matching_component_refs_p (base1, ref1,
1219 : offset1, max_size1,
1220 : ref, ref2,
1221 : offset2, max_size2,
1222 963552 : partial_overlap);
1223 : }
1224 : return -1;
1225 : }
1226 :
1227 : /* Consider access path1 base1....ref1 and access path2 base2...ref2.
1228 : Return true if they can be composed to single access path
1229 : base1...ref1...base2...ref2.
1230 :
1231 : REF_TYPE1 if type of REF1. END_STRUCT_PAST_END1 is true if there is
1232 : a trailing array access after REF1 in the non-TBAA part of the access.
1233 : REF1_ALIAS_SET is the alias set of REF1.
1234 :
1235 : BASE_TYPE2 is type of base2. END_STRUCT_REF2 is non-NULL if there is
1236 : a trailing array access in the TBAA part of access path2.
1237 : BASE2_ALIAS_SET is the alias set of base2. */
1238 :
1239 : bool
1240 1960356 : access_path_may_continue_p (tree ref_type1, bool end_struct_past_end1,
1241 : alias_set_type ref1_alias_set,
1242 : tree base_type2, tree end_struct_ref2,
1243 : alias_set_type base2_alias_set)
1244 : {
1245 : /* Access path can not continue past types with no components. */
1246 1960356 : if (!type_has_components_p (ref_type1))
1247 : return false;
1248 :
1249 : /* If first access path ends by too small type to hold base of
1250 : the second access path, typically paths can not continue.
1251 :
1252 : Punt if end_struct_past_end1 is true. We want to support arbitrary
1253 : type puning past first COMPONENT_REF to union because redundant store
1254 : elimination depends on this, see PR92152. For this reason we can not
1255 : check size of the reference because types may partially overlap. */
1256 188823 : if (!end_struct_past_end1)
1257 : {
1258 188778 : if (compare_type_sizes (ref_type1, base_type2) < 0)
1259 : return false;
1260 : /* If the path2 contains trailing array access we can strengthen the check
1261 : to verify that also the size of element of the trailing array fits.
1262 : In fact we could check for offset + type_size, but we do not track
1263 : offsets and this is quite side case. */
1264 171102 : if (end_struct_ref2
1265 171102 : && compare_type_sizes (ref_type1, TREE_TYPE (end_struct_ref2)) < 0)
1266 : return false;
1267 : }
1268 171147 : return (base2_alias_set == ref1_alias_set
1269 171147 : || alias_set_subset_of (base2_alias_set, ref1_alias_set));
1270 : }
1271 :
1272 : /* Determine if the two component references REF1 and REF2 which are
1273 : based on access types TYPE1 and TYPE2 and of which at least one is based
1274 : on an indirect reference may alias.
1275 : REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
1276 : are the respective alias sets. */
1277 :
1278 : static bool
1279 2432818 : aliasing_component_refs_p (tree ref1,
1280 : alias_set_type ref1_alias_set,
1281 : alias_set_type base1_alias_set,
1282 : poly_int64 offset1, poly_int64 max_size1,
1283 : tree ref2,
1284 : alias_set_type ref2_alias_set,
1285 : alias_set_type base2_alias_set,
1286 : poly_int64 offset2, poly_int64 max_size2)
1287 : {
1288 : /* If one reference is a component references through pointers try to find a
1289 : common base and apply offset based disambiguation. This handles
1290 : for example
1291 : struct A { int i; int j; } *q;
1292 : struct B { struct A a; int k; } *p;
1293 : disambiguating q->i and p->a.j. */
1294 2432818 : tree base1, base2;
1295 2432818 : tree type1, type2;
1296 2432818 : bool maybe_match = false;
1297 2432818 : tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
1298 2432818 : bool end_struct_past_end1 = false;
1299 2432818 : bool end_struct_past_end2 = false;
1300 :
1301 : /* Choose bases and base types to search for.
1302 : The access path is as follows:
1303 : base....end_of_tbaa_ref...actual_ref
1304 : At one place in the access path may be a reference to zero sized or
1305 : trailing array.
1306 :
1307 : We generally discard the segment after end_of_tbaa_ref however
1308 : we need to be careful in case it contains zero sized or trailing array.
1309 : These may happen after reference to union and in this case we need to
1310 : not disambiguate type puning scenarios.
1311 :
1312 : We set:
1313 : base1 to point to base
1314 :
1315 : ref1 to point to end_of_tbaa_ref
1316 :
1317 : end_struct_ref1 to point the trailing reference (if it exists
1318 : in range base....end_of_tbaa_ref
1319 :
1320 : end_struct_past_end1 is true if this trailing reference occurs in
1321 : end_of_tbaa_ref...actual_ref. */
1322 2432818 : base1 = ref1;
1323 5084188 : while (handled_component_p (base1))
1324 : {
1325 : /* Generally access paths are monotous in the size of object. The
1326 : exception are trailing arrays of structures. I.e.
1327 : struct a {int array[0];};
1328 : or
1329 : struct a {int array1[0]; int array[];};
1330 : Such struct has size 0 but accesses to a.array may have non-zero size.
1331 : In this case the size of TREE_TYPE (base1) is smaller than
1332 : size of TREE_TYPE (TREE_OPERAND (base1, 0)).
1333 :
1334 : Because we compare sizes of arrays just by sizes of their elements,
1335 : we only need to care about zero sized array fields here. */
1336 2651370 : if (component_ref_to_zero_sized_trailing_array_p (base1))
1337 : {
1338 75205 : gcc_checking_assert (!end_struct_ref1);
1339 : end_struct_ref1 = base1;
1340 : }
1341 2651370 : if (ends_tbaa_access_path_p (base1))
1342 : {
1343 28466 : ref1 = TREE_OPERAND (base1, 0);
1344 28466 : if (end_struct_ref1)
1345 : {
1346 1 : end_struct_past_end1 = true;
1347 1 : end_struct_ref1 = NULL;
1348 : }
1349 : }
1350 2651370 : base1 = TREE_OPERAND (base1, 0);
1351 : }
1352 2432818 : type1 = TREE_TYPE (base1);
1353 2432818 : base2 = ref2;
1354 5616618 : while (handled_component_p (base2))
1355 : {
1356 3183800 : if (component_ref_to_zero_sized_trailing_array_p (base2))
1357 : {
1358 9151 : gcc_checking_assert (!end_struct_ref2);
1359 : end_struct_ref2 = base2;
1360 : }
1361 3183800 : if (ends_tbaa_access_path_p (base2))
1362 : {
1363 106624 : ref2 = TREE_OPERAND (base2, 0);
1364 106624 : if (end_struct_ref2)
1365 : {
1366 44 : end_struct_past_end2 = true;
1367 44 : end_struct_ref2 = NULL;
1368 : }
1369 : }
1370 3183800 : base2 = TREE_OPERAND (base2, 0);
1371 : }
1372 2432818 : type2 = TREE_TYPE (base2);
1373 :
1374 : /* Now search for the type1 in the access path of ref2. This
1375 : would be a common base for doing offset based disambiguation on.
1376 : This however only makes sense if type2 is big enough to hold type1. */
1377 2432818 : int cmp_outer = compare_type_sizes (type2, type1);
1378 :
1379 : /* If type2 is big enough to contain type1 walk its access path.
1380 : We also need to care of arrays at the end of structs that may extend
1381 : beyond the end of structure. If this occurs in the TBAA part of the
1382 : access path, we need to consider the increased type as well. */
1383 2432818 : if (cmp_outer >= 0
1384 2432818 : || (end_struct_ref2
1385 1 : && compare_type_sizes (TREE_TYPE (end_struct_ref2), type1) >= 0))
1386 : {
1387 1310698 : int res = aliasing_component_refs_walk (ref1, type1, base1,
1388 : offset1, max_size1,
1389 : end_struct_ref1,
1390 : ref2, base2, offset2, max_size2,
1391 : &maybe_match);
1392 1310698 : if (res != -1)
1393 647554 : return res;
1394 : }
1395 :
1396 : /* If we didn't find a common base, try the other way around. */
1397 1785264 : if (cmp_outer <= 0
1398 1785264 : || (end_struct_ref1
1399 60 : && compare_type_sizes (TREE_TYPE (end_struct_ref1), type2) <= 0))
1400 : {
1401 1339319 : int res = aliasing_component_refs_walk (ref2, type2, base2,
1402 : offset2, max_size2,
1403 : end_struct_ref2,
1404 : ref1, base1, offset1, max_size1,
1405 : &maybe_match);
1406 1339319 : if (res != -1)
1407 315998 : return res;
1408 : }
1409 :
1410 : /* In the following code we make an assumption that the types in access
1411 : paths do not overlap and thus accesses alias only if one path can be
1412 : continuation of another. If we was not able to decide about equivalence,
1413 : we need to give up. */
1414 1469266 : if (maybe_match)
1415 : {
1416 473084 : if (!nonoverlapping_component_refs_p (ref1, ref2))
1417 : {
1418 471276 : ++alias_stats.aliasing_component_refs_p_may_alias;
1419 471276 : return true;
1420 : }
1421 1808 : ++alias_stats.aliasing_component_refs_p_no_alias;
1422 1808 : return false;
1423 : }
1424 :
1425 996182 : if (access_path_may_continue_p (TREE_TYPE (ref1), end_struct_past_end1,
1426 : ref1_alias_set,
1427 : type2, end_struct_ref2,
1428 : base2_alias_set)
1429 996182 : || access_path_may_continue_p (TREE_TYPE (ref2), end_struct_past_end2,
1430 : ref2_alias_set,
1431 : type1, end_struct_ref1,
1432 : base1_alias_set))
1433 : {
1434 170480 : ++alias_stats.aliasing_component_refs_p_may_alias;
1435 170480 : return true;
1436 : }
1437 825702 : ++alias_stats.aliasing_component_refs_p_no_alias;
1438 825702 : return false;
1439 : }
1440 :
1441 : /* FIELD1 and FIELD2 are two fields of component refs. We assume
1442 : that bases of both component refs are either equivalent or nonoverlapping.
1443 : We do not assume that the containers of FIELD1 and FIELD2 are of the
1444 : same type or size.
1445 :
1446 : Return 0 in case the base address of component_refs are same then
1447 : FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1448 : may not be of same type or size.
1449 :
1450 : Return 1 if FIELD1 and FIELD2 are non-overlapping.
1451 :
1452 : Return -1 otherwise.
1453 :
1454 : Main difference between 0 and -1 is to let
1455 : nonoverlapping_component_refs_since_match_p discover the semantically
1456 : equivalent part of the access path.
1457 :
1458 : Note that this function is used even with -fno-strict-aliasing
1459 : and makes use of no TBAA assumptions. */
1460 :
1461 : static int
1462 3593533 : nonoverlapping_component_refs_p_1 (const_tree field1, const_tree field2)
1463 : {
1464 : /* If both fields are of the same type, we could save hard work of
1465 : comparing offsets. */
1466 3593533 : tree type1 = DECL_CONTEXT (field1);
1467 3593533 : tree type2 = DECL_CONTEXT (field2);
1468 :
1469 3593533 : if (TREE_CODE (type1) == RECORD_TYPE
1470 7044398 : && DECL_BIT_FIELD_REPRESENTATIVE (field1))
1471 : field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
1472 3593533 : if (TREE_CODE (type2) == RECORD_TYPE
1473 7044398 : && DECL_BIT_FIELD_REPRESENTATIVE (field2))
1474 : field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
1475 :
1476 : /* ??? Bitfields can overlap at RTL level so punt on them.
1477 : FIXME: RTL expansion should be fixed by adjusting the access path
1478 : when producing MEM_ATTRs for MEMs which are wider than
1479 : the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1480 3593543 : if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1481 : return -1;
1482 :
1483 : /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1484 3593523 : if (type1 == type2 && TREE_CODE (type1) == RECORD_TYPE)
1485 3419544 : return field1 != field2;
1486 :
1487 : /* In common case the offsets and bit offsets will be the same.
1488 : However if frontends do not agree on the alignment, they may be
1489 : different even if they actually represent same address.
1490 : Try the common case first and if that fails calculate the
1491 : actual bit offset. */
1492 173979 : if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1),
1493 173979 : DECL_FIELD_OFFSET (field2))
1494 316971 : && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1),
1495 142992 : DECL_FIELD_BIT_OFFSET (field2)))
1496 : return 0;
1497 :
1498 : /* Note that it may be possible to use component_ref_field_offset
1499 : which would provide offsets as trees. However constructing and folding
1500 : trees is expensive and does not seem to be worth the compile time
1501 : cost. */
1502 :
1503 31852 : poly_uint64 offset1, offset2;
1504 31852 : poly_uint64 bit_offset1, bit_offset2;
1505 :
1506 31852 : if (poly_int_tree_p (DECL_FIELD_OFFSET (field1), &offset1)
1507 31852 : && poly_int_tree_p (DECL_FIELD_OFFSET (field2), &offset2)
1508 31852 : && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1), &bit_offset1)
1509 63704 : && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2), &bit_offset2))
1510 : {
1511 31852 : offset1 = (offset1 << LOG2_BITS_PER_UNIT) + bit_offset1;
1512 31852 : offset2 = (offset2 << LOG2_BITS_PER_UNIT) + bit_offset2;
1513 :
1514 31852 : if (known_eq (offset1, offset2))
1515 28350 : return 0;
1516 :
1517 31852 : poly_uint64 size1, size2;
1518 :
1519 31852 : if (poly_int_tree_p (DECL_SIZE (field1), &size1)
1520 31852 : && poly_int_tree_p (DECL_SIZE (field2), &size2)
1521 63704 : && !ranges_maybe_overlap_p (offset1, size1, offset2, size2))
1522 : return 1;
1523 : }
1524 : /* Resort to slower overlap checking by looking for matching types in
1525 : the middle of access path. */
1526 : return -1;
1527 : }
1528 :
1529 : /* Return low bound of array. Do not produce new trees
1530 : and thus do not care about particular type of integer constant
1531 : and placeholder exprs. */
1532 :
1533 : static tree
1534 16020941 : cheap_array_ref_low_bound (tree ref)
1535 : {
1536 16020941 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
1537 :
1538 : /* Avoid expensive array_ref_low_bound.
1539 : low bound is either stored in operand2, or it is TYPE_MIN_VALUE of domain
1540 : type or it is zero. */
1541 16020941 : if (TREE_OPERAND (ref, 2))
1542 90063 : return TREE_OPERAND (ref, 2);
1543 15930878 : else if (domain_type && TYPE_MIN_VALUE (domain_type))
1544 15925338 : return TYPE_MIN_VALUE (domain_type);
1545 : else
1546 5540 : return integer_zero_node;
1547 : }
1548 :
1549 : /* REF1 and REF2 are ARRAY_REFs with either same base address or which are
1550 : completely disjoint.
1551 :
1552 : Return 1 if the refs are non-overlapping.
1553 : Return 0 if they are possibly overlapping but if so the overlap again
1554 : starts on the same address.
1555 : Return -1 otherwise. */
1556 :
1557 : int
1558 7989583 : nonoverlapping_array_refs_p (tree ref1, tree ref2)
1559 : {
1560 7989583 : tree index1 = TREE_OPERAND (ref1, 1);
1561 7989583 : tree index2 = TREE_OPERAND (ref2, 1);
1562 7989583 : tree low_bound1 = cheap_array_ref_low_bound (ref1);
1563 7989583 : tree low_bound2 = cheap_array_ref_low_bound (ref2);
1564 :
1565 : /* Handle zero offsets first: we do not need to match type size in this
1566 : case. */
1567 7989583 : if (operand_equal_p (index1, low_bound1, 0)
1568 7989583 : && operand_equal_p (index2, low_bound2, 0))
1569 : return 0;
1570 :
1571 : /* If type sizes are different, give up.
1572 :
1573 : Avoid expensive array_ref_element_size.
1574 : If operand 3 is present it denotes size in the alignment units.
1575 : Otherwise size is TYPE_SIZE of the element type.
1576 : Handle only common cases where types are of the same "kind". */
1577 7895289 : if ((TREE_OPERAND (ref1, 3) == NULL) != (TREE_OPERAND (ref2, 3) == NULL))
1578 : return -1;
1579 :
1580 7895289 : tree elmt_type1 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref1, 0)));
1581 7895289 : tree elmt_type2 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref2, 0)));
1582 :
1583 7895289 : if (TREE_OPERAND (ref1, 3))
1584 : {
1585 3873 : if (TYPE_ALIGN (elmt_type1) != TYPE_ALIGN (elmt_type2)
1586 7746 : || !operand_equal_p (TREE_OPERAND (ref1, 3),
1587 3873 : TREE_OPERAND (ref2, 3), 0))
1588 : return -1;
1589 : }
1590 : else
1591 : {
1592 7891416 : if (!operand_equal_p (TYPE_SIZE_UNIT (elmt_type1),
1593 7891416 : TYPE_SIZE_UNIT (elmt_type2), 0))
1594 : return -1;
1595 : }
1596 :
1597 : /* Since we know that type sizes are the same, there is no need to return
1598 : -1 after this point. Partial overlap can not be introduced. */
1599 :
1600 : /* We may need to fold trees in this case.
1601 : TODO: Handle integer constant case at least. */
1602 7886597 : if (!operand_equal_p (low_bound1, low_bound2, 0))
1603 : return 0;
1604 :
1605 7886581 : if (TREE_CODE (index1) == INTEGER_CST && TREE_CODE (index2) == INTEGER_CST)
1606 : {
1607 361357 : if (tree_int_cst_equal (index1, index2))
1608 : return 0;
1609 262631 : return 1;
1610 : }
1611 : /* TODO: We can use VRP to further disambiguate here. */
1612 : return 0;
1613 : }
1614 :
1615 : /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1616 : MATCH2 either point to the same address or are disjoint.
1617 : MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1618 : respectively or NULL in the case we established equivalence of bases.
1619 : If PARTIAL_OVERLAP is true assume that the toplevel arrays may actually
1620 : overlap by exact multiply of their element size.
1621 :
1622 : This test works by matching the initial segment of the access path
1623 : and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1624 : match was determined without use of TBAA oracle.
1625 :
1626 : Return 1 if we can determine that component references REF1 and REF2,
1627 : that are within a common DECL, cannot overlap.
1628 :
1629 : Return 0 if paths are same and thus there is nothing to disambiguate more
1630 : (i.e. there is must alias assuming there is must alias between MATCH1 and
1631 : MATCH2)
1632 :
1633 : Return -1 if we can not determine 0 or 1 - this happens when we met
1634 : non-matching types was met in the path.
1635 : In this case it may make sense to continue by other disambiguation
1636 : oracles. */
1637 :
1638 : static int
1639 8453813 : nonoverlapping_refs_since_match_p (tree match1, tree ref1,
1640 : tree match2, tree ref2,
1641 : bool partial_overlap)
1642 : {
1643 8453813 : int ntbaa1 = 0, ntbaa2 = 0;
1644 : /* Early return if there are no references to match, we do not need
1645 : to walk the access paths.
1646 :
1647 : Do not consider this as may-alias for stats - it is more useful
1648 : to have information how many disambiguations happened provided that
1649 : the query was meaningful. */
1650 :
1651 7607035 : if (match1 == ref1 || !handled_component_p (ref1)
1652 16059396 : || match2 == ref2 || !handled_component_p (ref2))
1653 : return -1;
1654 :
1655 7585532 : auto_vec<tree, 16> component_refs1;
1656 7585532 : auto_vec<tree, 16> component_refs2;
1657 :
1658 : /* Create the stack of handled components for REF1. */
1659 20222276 : while (handled_component_p (ref1) && ref1 != match1)
1660 : {
1661 : /* We use TBAA only to re-synchronize after mismatched refs. So we
1662 : do not need to truncate access path after TBAA part ends. */
1663 12636744 : if (ends_tbaa_access_path_p (ref1))
1664 : ntbaa1 = 0;
1665 : else
1666 12401894 : ntbaa1++;
1667 12636744 : component_refs1.safe_push (ref1);
1668 12636744 : ref1 = TREE_OPERAND (ref1, 0);
1669 : }
1670 :
1671 : /* Create the stack of handled components for REF2. */
1672 20175069 : while (handled_component_p (ref2) && ref2 != match2)
1673 : {
1674 12589537 : if (ends_tbaa_access_path_p (ref2))
1675 : ntbaa2 = 0;
1676 : else
1677 12334635 : ntbaa2++;
1678 12589537 : component_refs2.safe_push (ref2);
1679 12589537 : ref2 = TREE_OPERAND (ref2, 0);
1680 : }
1681 :
1682 7585532 : if (!flag_strict_aliasing)
1683 : {
1684 509333 : ntbaa1 = 0;
1685 509333 : ntbaa2 = 0;
1686 : }
1687 :
1688 7585532 : bool mem_ref1 = TREE_CODE (ref1) == MEM_REF && ref1 != match1;
1689 7585532 : bool mem_ref2 = TREE_CODE (ref2) == MEM_REF && ref2 != match2;
1690 :
1691 : /* If only one of access path starts with MEM_REF check that offset is 0
1692 : so the addresses stays the same after stripping it.
1693 : TODO: In this case we may walk the other access path until we get same
1694 : offset.
1695 :
1696 : If both starts with MEM_REF, offset has to be same. */
1697 87417 : if ((mem_ref1 && !mem_ref2 && !integer_zerop (TREE_OPERAND (ref1, 1)))
1698 7555419 : || (mem_ref2 && !mem_ref1 && !integer_zerop (TREE_OPERAND (ref2, 1)))
1699 15130106 : || (mem_ref1 && mem_ref2
1700 1001194 : && !tree_int_cst_equal (TREE_OPERAND (ref1, 1),
1701 1001194 : TREE_OPERAND (ref2, 1))))
1702 : {
1703 73368 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1704 73368 : return -1;
1705 : }
1706 :
1707 : /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1708 : to handle them here at all. */
1709 7512164 : gcc_checking_assert (TREE_CODE (ref1) != TARGET_MEM_REF
1710 : && TREE_CODE (ref2) != TARGET_MEM_REF);
1711 :
1712 : /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1713 : rank. This is sufficient because we start from the same DECL and you
1714 : cannot reference several fields at a time with COMPONENT_REFs (unlike
1715 : with ARRAY_RANGE_REFs for arrays) so you always need the same number
1716 : of them to access a sub-component, unless you're in a union, in which
1717 : case the return value will precisely be false. */
1718 9939787 : while (true)
1719 : {
1720 : /* Track if we seen unmatched ref with non-zero offset. In this case
1721 : we must look for partial overlaps. */
1722 9939787 : bool seen_unmatched_ref_p = false;
1723 :
1724 : /* First match ARRAY_REFs an try to disambiguate. */
1725 19152018 : if (!component_refs1.is_empty ()
1726 9634148 : && !component_refs2.is_empty ())
1727 : {
1728 17697030 : unsigned int narray_refs1=0, narray_refs2=0;
1729 :
1730 : /* We generally assume that both access paths starts by same sequence
1731 : of refs. However if number of array refs is not in sync, try
1732 : to recover and pop elts until number match. This helps the case
1733 : where one access path starts by array and other by element. */
1734 17697030 : for (narray_refs1 = 0; narray_refs1 < component_refs1.length ();
1735 : narray_refs1++)
1736 12251617 : if (TREE_CODE (component_refs1 [component_refs1.length()
1737 : - 1 - narray_refs1]) != ARRAY_REF)
1738 : break;
1739 :
1740 17696455 : for (narray_refs2 = 0; narray_refs2 < component_refs2.length ();
1741 : narray_refs2++)
1742 12261011 : if (TREE_CODE (component_refs2 [component_refs2.length()
1743 : - 1 - narray_refs2]) != ARRAY_REF)
1744 : break;
1745 9183069 : for (; narray_refs1 > narray_refs2; narray_refs1--)
1746 : {
1747 21175 : ref1 = component_refs1.pop ();
1748 21175 : ntbaa1--;
1749 :
1750 : /* If index is non-zero we need to check whether the reference
1751 : does not break the main invariant that bases are either
1752 : disjoint or equal. Consider the example:
1753 :
1754 : unsigned char out[][1];
1755 : out[1]="a";
1756 : out[i][0];
1757 :
1758 : Here bases out and out are same, but after removing the
1759 : [i] index, this invariant no longer holds, because
1760 : out[i] points to the middle of array out.
1761 :
1762 : TODO: If size of type of the skipped reference is an integer
1763 : multiply of the size of type of the other reference this
1764 : invariant can be verified, but even then it is not completely
1765 : safe with !flag_strict_aliasing if the other reference contains
1766 : unbounded array accesses.
1767 : See */
1768 :
1769 21175 : if (!operand_equal_p (TREE_OPERAND (ref1, 1),
1770 21175 : cheap_array_ref_low_bound (ref1), 0))
1771 : return 0;
1772 : }
1773 9161935 : for (; narray_refs2 > narray_refs1; narray_refs2--)
1774 : {
1775 20600 : ref2 = component_refs2.pop ();
1776 20600 : ntbaa2--;
1777 20600 : if (!operand_equal_p (TREE_OPERAND (ref2, 1),
1778 20600 : cheap_array_ref_low_bound (ref2), 0))
1779 : return 0;
1780 : }
1781 : /* Try to disambiguate matched arrays. */
1782 16865816 : for (unsigned int i = 0; i < narray_refs1; i++)
1783 : {
1784 15979166 : int cmp = nonoverlapping_array_refs_p (component_refs1.pop (),
1785 7989583 : component_refs2.pop ());
1786 7989583 : ntbaa1--;
1787 7989583 : ntbaa2--;
1788 7989583 : if (cmp == 1 && !partial_overlap)
1789 : {
1790 256410 : ++alias_stats
1791 256410 : .nonoverlapping_refs_since_match_p_no_alias;
1792 256410 : return 1;
1793 : }
1794 7733173 : if (cmp == -1)
1795 : {
1796 8692 : seen_unmatched_ref_p = true;
1797 : /* We can not maintain the invariant that bases are either
1798 : same or completely disjoint. However we can still recover
1799 : from type based alias analysis if we reach references to
1800 : same sizes. We do not attempt to match array sizes, so
1801 : just finish array walking and look for component refs. */
1802 8692 : if (ntbaa1 < 0 || ntbaa2 < 0)
1803 : {
1804 7831 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1805 7831 : return -1;
1806 : }
1807 1796 : for (i++; i < narray_refs1; i++)
1808 : {
1809 935 : component_refs1.pop ();
1810 935 : component_refs2.pop ();
1811 935 : ntbaa1--;
1812 935 : ntbaa2--;
1813 : }
1814 : break;
1815 : }
1816 7724481 : partial_overlap = false;
1817 : }
1818 : }
1819 :
1820 : /* Next look for component_refs. */
1821 9698017 : do
1822 : {
1823 9698017 : if (component_refs1.is_empty ())
1824 : {
1825 5972081 : ++alias_stats
1826 5972081 : .nonoverlapping_refs_since_match_p_must_overlap;
1827 5972081 : return 0;
1828 : }
1829 3725936 : ref1 = component_refs1.pop ();
1830 3725936 : ntbaa1--;
1831 3725936 : if (TREE_CODE (ref1) != COMPONENT_REF)
1832 : {
1833 115026 : seen_unmatched_ref_p = true;
1834 115026 : if (ntbaa1 < 0 || ntbaa2 < 0)
1835 : {
1836 51157 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1837 51157 : return -1;
1838 : }
1839 : }
1840 : }
1841 7285689 : while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1842 :
1843 3610910 : do
1844 : {
1845 3610910 : if (component_refs2.is_empty ())
1846 : {
1847 20719 : ++alias_stats
1848 20719 : .nonoverlapping_refs_since_match_p_must_overlap;
1849 20719 : return 0;
1850 : }
1851 3590191 : ref2 = component_refs2.pop ();
1852 3590191 : ntbaa2--;
1853 3590191 : if (TREE_CODE (ref2) != COMPONENT_REF)
1854 : {
1855 48 : if (ntbaa1 < 0 || ntbaa2 < 0)
1856 : {
1857 48 : ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1858 48 : return -1;
1859 : }
1860 : seen_unmatched_ref_p = true;
1861 : }
1862 : }
1863 7180286 : while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1864 :
1865 : /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1866 : earlier. */
1867 3590143 : gcc_checking_assert (TREE_CODE (ref1) == COMPONENT_REF
1868 : && TREE_CODE (ref2) == COMPONENT_REF);
1869 :
1870 3590143 : tree field1 = TREE_OPERAND (ref1, 1);
1871 3590143 : tree field2 = TREE_OPERAND (ref2, 1);
1872 :
1873 : /* ??? We cannot simply use the type of operand #0 of the refs here
1874 : as the Fortran compiler smuggles type punning into COMPONENT_REFs
1875 : for common blocks instead of using unions like everyone else. */
1876 3590143 : tree type1 = DECL_CONTEXT (field1);
1877 3590143 : tree type2 = DECL_CONTEXT (field2);
1878 :
1879 3590143 : partial_overlap = false;
1880 :
1881 : /* If we skipped array refs on type of different sizes, we can
1882 : no longer be sure that there are not partial overlaps. */
1883 432 : if (seen_unmatched_ref_p && ntbaa1 >= 0 && ntbaa2 >= 0
1884 3590575 : && !operand_equal_p (TYPE_SIZE (type1), TYPE_SIZE (type2), 0))
1885 : {
1886 0 : ++alias_stats
1887 0 : .nonoverlapping_refs_since_match_p_may_alias;
1888 0 : return -1;
1889 : }
1890 :
1891 3590143 : int cmp = nonoverlapping_component_refs_p_1 (field1, field2);
1892 3590143 : if (cmp == -1)
1893 : {
1894 3512 : ++alias_stats
1895 3512 : .nonoverlapping_refs_since_match_p_may_alias;
1896 3512 : return -1;
1897 : }
1898 3586631 : else if (cmp == 1)
1899 : {
1900 1159008 : ++alias_stats
1901 1159008 : .nonoverlapping_refs_since_match_p_no_alias;
1902 1159008 : return 1;
1903 : }
1904 : }
1905 7585532 : }
1906 :
1907 : /* Return TYPE_UID which can be used to match record types we consider
1908 : same for TBAA purposes. */
1909 :
1910 : static inline int
1911 163592 : ncr_type_uid (const_tree field)
1912 : {
1913 : /* ??? We cannot simply use the type of operand #0 of the refs here
1914 : as the Fortran compiler smuggles type punning into COMPONENT_REFs
1915 : for common blocks instead of using unions like everyone else. */
1916 163592 : tree type = DECL_FIELD_CONTEXT (field);
1917 : /* With LTO types considered same_type_for_tbaa_p
1918 : from different translation unit may not have same
1919 : main variant. They however have same TYPE_CANONICAL. */
1920 163592 : if (TYPE_CANONICAL (type))
1921 163592 : return TYPE_UID (TYPE_CANONICAL (type));
1922 0 : return TYPE_UID (type);
1923 : }
1924 :
1925 : /* qsort compare function to sort FIELD_DECLs after their
1926 : DECL_FIELD_CONTEXT TYPE_UID. */
1927 :
1928 : static inline int
1929 71915 : ncr_compar (const void *field1_, const void *field2_)
1930 : {
1931 71915 : const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1932 71915 : const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1933 71915 : unsigned int uid1 = ncr_type_uid (field1);
1934 71915 : unsigned int uid2 = ncr_type_uid (field2);
1935 :
1936 71915 : if (uid1 < uid2)
1937 : return -1;
1938 28425 : else if (uid1 > uid2)
1939 28425 : return 1;
1940 : return 0;
1941 : }
1942 :
1943 : /* Return true if we can determine that the fields referenced cannot
1944 : overlap for any pair of objects. This relies on TBAA. */
1945 :
1946 : static bool
1947 1337451 : nonoverlapping_component_refs_p (const_tree x, const_tree y)
1948 : {
1949 : /* Early return if we have nothing to do.
1950 :
1951 : Do not consider this as may-alias for stats - it is more useful
1952 : to have information how many disambiguations happened provided that
1953 : the query was meaningful. */
1954 1337451 : if (!flag_strict_aliasing
1955 1337451 : || !x || !y
1956 169726 : || !handled_component_p (x)
1957 1337451 : || !handled_component_p (y))
1958 : return false;
1959 :
1960 100313 : auto_vec<const_tree, 16> fieldsx;
1961 317221 : while (handled_component_p (x))
1962 : {
1963 216908 : if (TREE_CODE (x) == COMPONENT_REF)
1964 : {
1965 129551 : tree field = TREE_OPERAND (x, 1);
1966 129551 : tree type = DECL_FIELD_CONTEXT (field);
1967 129551 : if (TREE_CODE (type) == RECORD_TYPE)
1968 129259 : fieldsx.safe_push (field);
1969 : }
1970 87357 : else if (ends_tbaa_access_path_p (x))
1971 2273 : fieldsx.truncate (0);
1972 216908 : x = TREE_OPERAND (x, 0);
1973 : }
1974 171206 : if (fieldsx.length () == 0)
1975 : return false;
1976 70893 : auto_vec<const_tree, 16> fieldsy;
1977 156323 : while (handled_component_p (y))
1978 : {
1979 85430 : if (TREE_CODE (y) == COMPONENT_REF)
1980 : {
1981 19257 : tree field = TREE_OPERAND (y, 1);
1982 19257 : tree type = DECL_FIELD_CONTEXT (field);
1983 19257 : if (TREE_CODE (type) == RECORD_TYPE)
1984 18965 : fieldsy.safe_push (TREE_OPERAND (y, 1));
1985 : }
1986 66173 : else if (ends_tbaa_access_path_p (y))
1987 140 : fieldsy.truncate (0);
1988 85430 : y = TREE_OPERAND (y, 0);
1989 : }
1990 70893 : if (fieldsy.length () == 0)
1991 : {
1992 61916 : ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1993 61916 : return false;
1994 : }
1995 :
1996 : /* Most common case first. */
1997 8977 : if (fieldsx.length () == 1
1998 8977 : && fieldsy.length () == 1)
1999 : {
2000 9656 : if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx[0]),
2001 4828 : DECL_FIELD_CONTEXT (fieldsy[0])) == 1
2002 8022 : && nonoverlapping_component_refs_p_1 (fieldsx[0], fieldsy[0]) == 1)
2003 : {
2004 1808 : ++alias_stats.nonoverlapping_component_refs_p_no_alias;
2005 1808 : return true;
2006 : }
2007 : else
2008 : {
2009 3020 : ++alias_stats.nonoverlapping_component_refs_p_may_alias;
2010 3020 : return false;
2011 : }
2012 : }
2013 :
2014 4149 : if (fieldsx.length () == 2)
2015 : {
2016 314 : if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
2017 172 : std::swap (fieldsx[0], fieldsx[1]);
2018 : }
2019 : else
2020 3835 : fieldsx.qsort (ncr_compar);
2021 :
2022 4149 : if (fieldsy.length () == 2)
2023 : {
2024 166 : if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
2025 106 : std::swap (fieldsy[0], fieldsy[1]);
2026 : }
2027 : else
2028 3983 : fieldsy.qsort (ncr_compar);
2029 :
2030 4149 : unsigned i = 0, j = 0;
2031 9881 : do
2032 : {
2033 9881 : const_tree fieldx = fieldsx[i];
2034 9881 : const_tree fieldy = fieldsy[j];
2035 :
2036 : /* We're left with accessing different fields of a structure,
2037 : no possible overlap. */
2038 19762 : if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx),
2039 9881 : DECL_FIELD_CONTEXT (fieldy)) == 1
2040 9881 : && nonoverlapping_component_refs_p_1 (fieldx, fieldy) == 1)
2041 : {
2042 0 : ++alias_stats.nonoverlapping_component_refs_p_no_alias;
2043 0 : return true;
2044 : }
2045 :
2046 9881 : if (ncr_type_uid (fieldx) < ncr_type_uid (fieldy))
2047 : {
2048 3578 : i++;
2049 7156 : if (i == fieldsx.length ())
2050 : break;
2051 : }
2052 : else
2053 : {
2054 6303 : j++;
2055 12606 : if (j == fieldsy.length ())
2056 : break;
2057 : }
2058 : }
2059 : while (1);
2060 :
2061 4149 : ++alias_stats.nonoverlapping_component_refs_p_may_alias;
2062 4149 : return false;
2063 171206 : }
2064 :
2065 :
2066 : /* Return true if two memory references based on the variables BASE1
2067 : and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2068 : [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
2069 : if non-NULL are the complete memory reference trees. */
2070 :
2071 : static bool
2072 1615755416 : decl_refs_may_alias_p (tree ref1, tree base1,
2073 : poly_int64 offset1, poly_int64 max_size1,
2074 : poly_int64 size1,
2075 : tree ref2, tree base2,
2076 : poly_int64 offset2, poly_int64 max_size2,
2077 : poly_int64 size2)
2078 : {
2079 1615755416 : gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
2080 :
2081 : /* If both references are based on different variables, they cannot alias. */
2082 1615755416 : if (compare_base_decls (base1, base2) == 0)
2083 : return false;
2084 :
2085 : /* If both references are based on the same variable, they cannot alias if
2086 : the accesses do not overlap. */
2087 210064055 : if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
2088 : return false;
2089 :
2090 : /* If there is must alias, there is no use disambiguating further. */
2091 62037477 : if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
2092 : return true;
2093 :
2094 : /* For components with variable position, the above test isn't sufficient,
2095 : so we disambiguate component references manually. */
2096 9986409 : if (ref1 && ref2
2097 7378286 : && handled_component_p (ref1) && handled_component_p (ref2)
2098 16520674 : && nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2, false) == 1)
2099 1170508 : return false;
2100 :
2101 : return true;
2102 : }
2103 :
2104 : /* Return true if access with BASE is view converted.
2105 : Base must not be stripped from inner MEM_REF (&decl)
2106 : which is done by ao_ref_base and thus one extra walk
2107 : of handled components is needed. */
2108 :
2109 : bool
2110 1284005778 : view_converted_memref_p (tree base)
2111 : {
2112 1284005778 : if (TREE_CODE (base) != MEM_REF && TREE_CODE (base) != TARGET_MEM_REF)
2113 : return false;
2114 857987191 : return (same_type_for_tbaa (TREE_TYPE (base),
2115 857987191 : TREE_TYPE (TREE_TYPE (TREE_OPERAND (base, 1))))
2116 857987191 : != 1);
2117 : }
2118 :
2119 : /* Return true if an indirect reference based on *PTR1 constrained
2120 : to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
2121 : constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
2122 : the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2123 : in which case they are computed on-demand. REF1 and REF2
2124 : if non-NULL are the complete memory reference trees. */
2125 :
2126 : static bool
2127 316412796 : indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
2128 : poly_int64 offset1, poly_int64 max_size1,
2129 : poly_int64 size1,
2130 : alias_set_type ref1_alias_set,
2131 : alias_set_type base1_alias_set,
2132 : tree ref2 ATTRIBUTE_UNUSED, tree base2,
2133 : poly_int64 offset2, poly_int64 max_size2,
2134 : poly_int64 size2,
2135 : alias_set_type ref2_alias_set,
2136 : alias_set_type base2_alias_set, bool tbaa_p)
2137 : {
2138 316412796 : tree ptr1;
2139 316412796 : tree ptrtype1, dbase2;
2140 :
2141 316412796 : gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
2142 : || TREE_CODE (base1) == TARGET_MEM_REF)
2143 : && DECL_P (base2));
2144 :
2145 316412796 : ptr1 = TREE_OPERAND (base1, 0);
2146 316412796 : poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
2147 :
2148 : /* If only one reference is based on a variable, they cannot alias if
2149 : the pointer access is beyond the extent of the variable access.
2150 : (the pointer base cannot validly point to an offset less than zero
2151 : of the variable).
2152 : ??? IVOPTs creates bases that do not honor this restriction,
2153 : so do not apply this optimization for TARGET_MEM_REFs. */
2154 316412796 : if (TREE_CODE (base1) != TARGET_MEM_REF
2155 316412796 : && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
2156 76442533 : return false;
2157 :
2158 : /* If the pointer based access is bigger than the variable they cannot
2159 : alias. This is similar to the check below where we use TBAA to
2160 : increase the size of the pointer based access based on the dynamic
2161 : type of a containing object we can infer from it. */
2162 239970263 : poly_int64 dsize2;
2163 239970263 : if (known_size_p (size1)
2164 223586014 : && poly_int_tree_p (DECL_SIZE (base2), &dsize2)
2165 425193502 : && known_lt (dsize2, size1))
2166 : return false;
2167 :
2168 : /* They also cannot alias if the pointer may not point to the decl. */
2169 220695479 : if (!ptr_deref_may_alias_decl_p (ptr1, base2))
2170 : return false;
2171 :
2172 : /* Disambiguations that rely on strict aliasing rules follow. */
2173 31665860 : if (!flag_strict_aliasing || !tbaa_p)
2174 : return true;
2175 :
2176 : /* If the alias set for a pointer access is zero all bets are off. */
2177 6864858 : if (base1_alias_set == 0 || base2_alias_set == 0)
2178 : return true;
2179 :
2180 : /* When we are trying to disambiguate an access with a pointer dereference
2181 : as base versus one with a decl as base we can use both the size
2182 : of the decl and its dynamic type for extra disambiguation.
2183 : ??? We do not know anything about the dynamic type of the decl
2184 : other than that its alias-set contains base2_alias_set as a subset
2185 : which does not help us here. */
2186 : /* As we know nothing useful about the dynamic type of the decl just
2187 : use the usual conflict check rather than a subset test.
2188 : ??? We could introduce -fvery-strict-aliasing when the language
2189 : does not allow decls to have a dynamic type that differs from their
2190 : static type. Then we can check
2191 : !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
2192 1654104 : if (base1_alias_set != base2_alias_set
2193 1654104 : && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2194 : return false;
2195 :
2196 1426212 : ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2197 :
2198 : /* If the size of the access relevant for TBAA through the pointer
2199 : is bigger than the size of the decl we can't possibly access the
2200 : decl via that pointer. */
2201 1426212 : if (/* ??? This in turn may run afoul when a decl of type T which is
2202 : a member of union type U is accessed through a pointer to
2203 : type U and sizeof T is smaller than sizeof U. */
2204 1426212 : TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
2205 1391162 : && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
2206 2817374 : && compare_sizes (DECL_SIZE (base2),
2207 1391162 : TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
2208 : return false;
2209 :
2210 1391532 : if (!ref2)
2211 : return true;
2212 :
2213 : /* If the decl is accessed via a MEM_REF, reconstruct the base
2214 : we can use for TBAA and an appropriately adjusted offset. */
2215 : dbase2 = ref2;
2216 2280292 : while (handled_component_p (dbase2))
2217 959905 : dbase2 = TREE_OPERAND (dbase2, 0);
2218 1320387 : poly_int64 doffset1 = offset1;
2219 1320387 : poly_offset_int doffset2 = offset2;
2220 1320387 : if (TREE_CODE (dbase2) == MEM_REF
2221 1320387 : || TREE_CODE (dbase2) == TARGET_MEM_REF)
2222 : {
2223 932538 : doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
2224 466269 : tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
2225 : /* If second reference is view-converted, give up now. */
2226 466269 : if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
2227 : return true;
2228 : }
2229 :
2230 : /* If first reference is view-converted, give up now. */
2231 1212649 : if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
2232 : return true;
2233 :
2234 : /* If both references are through the same type, they do not alias
2235 : if the accesses do not overlap. This does extra disambiguation
2236 : for mixed/pointer accesses but requires strict aliasing.
2237 : For MEM_REFs we require that the component-ref offset we computed
2238 : is relative to the start of the type which we ensure by
2239 : comparing rvalue and access type and disregarding the constant
2240 : pointer offset.
2241 :
2242 : But avoid treating variable length arrays as "objects", instead assume they
2243 : can overlap by an exact multiple of their element size.
2244 : See gcc.dg/torture/alias-2.c. */
2245 1104502 : if (((TREE_CODE (base1) != TARGET_MEM_REF
2246 213680 : || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2247 1068920 : && (TREE_CODE (dbase2) != TARGET_MEM_REF
2248 27995 : || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
2249 2145714 : && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
2250 : {
2251 339942 : bool partial_overlap = (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
2252 339942 : && (TYPE_SIZE (TREE_TYPE (base1))
2253 2467 : && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1)))
2254 660211 : != INTEGER_CST));
2255 339942 : if (!partial_overlap
2256 339942 : && !ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
2257 : return false;
2258 320269 : if (!ref1 || !ref2
2259 : /* If there is must alias, there is no use disambiguating further. */
2260 320269 : || (!partial_overlap
2261 307621 : && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2262 : return true;
2263 3520 : int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2264 : partial_overlap);
2265 3520 : if (res == -1)
2266 3280 : return !nonoverlapping_component_refs_p (ref1, ref2);
2267 240 : return !res;
2268 : }
2269 :
2270 : /* Do access-path based disambiguation. */
2271 764560 : if (ref1 && ref2
2272 1303010 : && (handled_component_p (ref1) || handled_component_p (ref2)))
2273 557988 : return aliasing_component_refs_p (ref1,
2274 : ref1_alias_set, base1_alias_set,
2275 : offset1, max_size1,
2276 : ref2,
2277 : ref2_alias_set, base2_alias_set,
2278 557988 : offset2, max_size2);
2279 :
2280 : return true;
2281 : }
2282 :
2283 : /* Return true if two indirect references based on *PTR1
2284 : and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2285 : [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
2286 : the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2287 : in which case they are computed on-demand. REF1 and REF2
2288 : if non-NULL are the complete memory reference trees. */
2289 :
2290 : static bool
2291 99639864 : indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
2292 : poly_int64 offset1, poly_int64 max_size1,
2293 : poly_int64 size1,
2294 : alias_set_type ref1_alias_set,
2295 : alias_set_type base1_alias_set,
2296 : tree ref2 ATTRIBUTE_UNUSED, tree base2,
2297 : poly_int64 offset2, poly_int64 max_size2,
2298 : poly_int64 size2,
2299 : alias_set_type ref2_alias_set,
2300 : alias_set_type base2_alias_set, bool tbaa_p)
2301 : {
2302 99639864 : tree ptr1;
2303 99639864 : tree ptr2;
2304 99639864 : tree ptrtype1, ptrtype2;
2305 :
2306 99639864 : gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
2307 : || TREE_CODE (base1) == TARGET_MEM_REF)
2308 : && (TREE_CODE (base2) == MEM_REF
2309 : || TREE_CODE (base2) == TARGET_MEM_REF));
2310 :
2311 99639864 : ptr1 = TREE_OPERAND (base1, 0);
2312 99639864 : ptr2 = TREE_OPERAND (base2, 0);
2313 :
2314 : /* If both bases are based on pointers they cannot alias if they may not
2315 : point to the same memory object or if they point to the same object
2316 : and the accesses do not overlap. */
2317 99639864 : if ((!cfun || gimple_in_ssa_p (cfun))
2318 63861221 : && operand_equal_p (ptr1, ptr2, 0)
2319 132841979 : && (((TREE_CODE (base1) != TARGET_MEM_REF
2320 1157481 : || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2321 33108770 : && (TREE_CODE (base2) != TARGET_MEM_REF
2322 1055431 : || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
2323 109286 : || (TREE_CODE (base1) == TARGET_MEM_REF
2324 97135 : && TREE_CODE (base2) == TARGET_MEM_REF
2325 88410 : && (TMR_STEP (base1) == TMR_STEP (base2)
2326 13836 : || (TMR_STEP (base1) && TMR_STEP (base2)
2327 3024 : && operand_equal_p (TMR_STEP (base1),
2328 3024 : TMR_STEP (base2), 0)))
2329 74574 : && (TMR_INDEX (base1) == TMR_INDEX (base2)
2330 12793 : || (TMR_INDEX (base1) && TMR_INDEX (base2)
2331 11382 : && operand_equal_p (TMR_INDEX (base1),
2332 11382 : TMR_INDEX (base2), 0)))
2333 61781 : && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
2334 0 : || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
2335 0 : && operand_equal_p (TMR_INDEX2 (base1),
2336 0 : TMR_INDEX2 (base2), 0))))))
2337 : {
2338 33154610 : poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
2339 33154610 : poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
2340 33154610 : if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
2341 33154610 : offset2 + moff2, max_size2))
2342 32829059 : return false;
2343 : /* If there is must alias, there is no use disambiguating further. */
2344 4105962 : if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
2345 : return true;
2346 1076706 : if (ref1 && ref2)
2347 : {
2348 763766 : int res = nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2,
2349 : false);
2350 763766 : if (res != -1)
2351 751155 : return !res;
2352 : }
2353 : }
2354 66810805 : if (!ptr_derefs_may_alias_p (ptr1, ptr2))
2355 : return false;
2356 :
2357 : /* Disambiguations that rely on strict aliasing rules follow. */
2358 37840888 : if (!flag_strict_aliasing || !tbaa_p)
2359 : return true;
2360 :
2361 15244115 : ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2362 15244115 : ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
2363 :
2364 : /* If the alias set for a pointer access is zero all bets are off. */
2365 15244115 : if (base1_alias_set == 0
2366 15244115 : || base2_alias_set == 0)
2367 : return true;
2368 :
2369 : /* Do type-based disambiguation. */
2370 10107152 : if (base1_alias_set != base2_alias_set
2371 10107152 : && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2372 : return false;
2373 :
2374 : /* If either reference is view-converted, give up now. */
2375 9279505 : if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
2376 9279505 : || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
2377 : return true;
2378 :
2379 : /* If both references are through the same type, they do not alias
2380 : if the accesses do not overlap. This does extra disambiguation
2381 : for mixed/pointer accesses but requires strict aliasing. */
2382 6707411 : if ((TREE_CODE (base1) != TARGET_MEM_REF
2383 1176147 : || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2384 6355736 : && (TREE_CODE (base2) != TARGET_MEM_REF
2385 997422 : || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
2386 12928487 : && same_type_for_tbaa (TREE_TYPE (ptrtype1),
2387 6221076 : TREE_TYPE (ptrtype2)) == 1)
2388 : {
2389 : /* But avoid treating arrays as "objects", instead assume they
2390 : can overlap by an exact multiple of their element size.
2391 : See gcc.dg/torture/alias-2.c. */
2392 4141345 : bool partial_overlap = TREE_CODE (TREE_TYPE (ptrtype1)) == ARRAY_TYPE;
2393 :
2394 4141345 : if (!partial_overlap
2395 4141345 : && !ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
2396 : return false;
2397 3782415 : if (!ref1 || !ref2
2398 3782415 : || (!partial_overlap
2399 3110567 : && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2400 : return true;
2401 251000 : int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2402 : partial_overlap);
2403 251000 : if (res == -1)
2404 63709 : return !nonoverlapping_component_refs_p (ref1, ref2);
2405 187291 : return !res;
2406 : }
2407 :
2408 : /* Do access-path based disambiguation. */
2409 2566066 : if (ref1 && ref2
2410 3744552 : && (handled_component_p (ref1) || handled_component_p (ref2)))
2411 1874830 : return aliasing_component_refs_p (ref1,
2412 : ref1_alias_set, base1_alias_set,
2413 : offset1, max_size1,
2414 : ref2,
2415 : ref2_alias_set, base2_alias_set,
2416 1874830 : offset2, max_size2);
2417 :
2418 : return true;
2419 : }
2420 :
2421 : /* Return true, if the two memory references REF1 and REF2 may alias. */
2422 :
2423 : static bool
2424 2116132112 : refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2425 : {
2426 2116132112 : tree base1, base2;
2427 2116132112 : poly_int64 offset1 = 0, offset2 = 0;
2428 2116132112 : poly_int64 max_size1 = -1, max_size2 = -1;
2429 2116132112 : bool var1_p, var2_p, ind1_p, ind2_p;
2430 :
2431 2116132112 : gcc_checking_assert ((!ref1->ref
2432 : || TREE_CODE (ref1->ref) == SSA_NAME
2433 : || DECL_P (ref1->ref)
2434 : || TREE_CODE (ref1->ref) == STRING_CST
2435 : || handled_component_p (ref1->ref)
2436 : || TREE_CODE (ref1->ref) == MEM_REF
2437 : || TREE_CODE (ref1->ref) == TARGET_MEM_REF
2438 : || TREE_CODE (ref1->ref) == WITH_SIZE_EXPR)
2439 : && (!ref2->ref
2440 : || TREE_CODE (ref2->ref) == SSA_NAME
2441 : || DECL_P (ref2->ref)
2442 : || TREE_CODE (ref2->ref) == STRING_CST
2443 : || handled_component_p (ref2->ref)
2444 : || TREE_CODE (ref2->ref) == MEM_REF
2445 : || TREE_CODE (ref2->ref) == TARGET_MEM_REF
2446 : || TREE_CODE (ref2->ref) == WITH_SIZE_EXPR));
2447 :
2448 : /* Decompose the references into their base objects and the access. */
2449 2116132112 : base1 = ao_ref_base (ref1);
2450 2116132112 : offset1 = ref1->offset;
2451 2116132112 : max_size1 = ref1->max_size;
2452 2116132112 : base2 = ao_ref_base (ref2);
2453 2116132112 : offset2 = ref2->offset;
2454 2116132112 : max_size2 = ref2->max_size;
2455 :
2456 : /* We can end up with registers or constants as bases for example from
2457 : *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
2458 : which is seen as a struct copy. */
2459 2116132112 : if (TREE_CODE (base1) == SSA_NAME
2460 2116126599 : || TREE_CODE (base1) == CONST_DECL
2461 2114152080 : || TREE_CODE (base1) == CONSTRUCTOR
2462 2114152080 : || TREE_CODE (base1) == ADDR_EXPR
2463 2114152072 : || CONSTANT_CLASS_P (base1)
2464 2107598218 : || TREE_CODE (base2) == SSA_NAME
2465 2107598218 : || TREE_CODE (base2) == CONST_DECL
2466 2107505731 : || TREE_CODE (base2) == CONSTRUCTOR
2467 2107505731 : || TREE_CODE (base2) == ADDR_EXPR
2468 2107505731 : || CONSTANT_CLASS_P (base2))
2469 : return false;
2470 :
2471 : /* Two volatile accesses always conflict. */
2472 2107437136 : if (ref1->volatile_p
2473 5902890 : && ref2->volatile_p)
2474 : return true;
2475 :
2476 : /* refN->ref may convey size information, do not confuse our workers
2477 : with that but strip it - ao_ref_base took it into account already. */
2478 2103778750 : tree ref1ref = ref1->ref;
2479 2103778750 : if (ref1ref && TREE_CODE (ref1ref) == WITH_SIZE_EXPR)
2480 162 : ref1ref = TREE_OPERAND (ref1ref, 0);
2481 2103778750 : tree ref2ref = ref2->ref;
2482 2103778750 : if (ref2ref && TREE_CODE (ref2ref) == WITH_SIZE_EXPR)
2483 0 : ref2ref = TREE_OPERAND (ref2ref, 0);
2484 :
2485 : /* Defer to simple offset based disambiguation if we have
2486 : references based on two decls. Do this before deferring to
2487 : TBAA to handle must-alias cases in conformance with the
2488 : GCC extension of allowing type-punning through unions. */
2489 2103778750 : var1_p = DECL_P (base1);
2490 2103778750 : var2_p = DECL_P (base2);
2491 2103778750 : if (var1_p && var2_p)
2492 1615755416 : return decl_refs_may_alias_p (ref1ref, base1, offset1, max_size1,
2493 : ref1->size,
2494 : ref2ref, base2, offset2, max_size2,
2495 1615755416 : ref2->size);
2496 :
2497 : /* We can end up referring to code via function and label decls.
2498 : As we likely do not properly track code aliases conservatively
2499 : bail out. */
2500 488023334 : if (TREE_CODE (base1) == FUNCTION_DECL
2501 488023334 : || TREE_CODE (base1) == LABEL_DECL
2502 487021736 : || TREE_CODE (base2) == FUNCTION_DECL
2503 487009482 : || TREE_CODE (base2) == LABEL_DECL)
2504 : return true;
2505 :
2506 : /* Handle restrict based accesses.
2507 : ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
2508 : here. */
2509 487009482 : tree rbase1 = base1;
2510 487009482 : tree rbase2 = base2;
2511 487009482 : if (var1_p)
2512 : {
2513 231597459 : rbase1 = ref1ref;
2514 231597459 : if (rbase1)
2515 303724707 : while (handled_component_p (rbase1))
2516 110743308 : rbase1 = TREE_OPERAND (rbase1, 0);
2517 : }
2518 487009482 : if (var2_p)
2519 : {
2520 127209255 : rbase2 = ref2ref;
2521 127209255 : if (rbase2)
2522 209728013 : while (handled_component_p (rbase2))
2523 91092671 : rbase2 = TREE_OPERAND (rbase2, 0);
2524 : }
2525 487009482 : if (rbase1 && rbase2
2526 439819509 : && (TREE_CODE (rbase1) == MEM_REF || TREE_CODE (rbase1) == TARGET_MEM_REF)
2527 283237698 : && (TREE_CODE (rbase2) == MEM_REF || TREE_CODE (rbase2) == TARGET_MEM_REF)
2528 : /* If the accesses are in the same restrict clique... */
2529 197560277 : && MR_DEPENDENCE_CLIQUE (rbase1) == MR_DEPENDENCE_CLIQUE (rbase2)
2530 : /* But based on different pointers they do not alias. */
2531 653533584 : && MR_DEPENDENCE_BASE (rbase1) != MR_DEPENDENCE_BASE (rbase2))
2532 : return false;
2533 :
2534 467920199 : ind1_p = (TREE_CODE (base1) == MEM_REF
2535 467920199 : || TREE_CODE (base1) == TARGET_MEM_REF);
2536 467920199 : ind2_p = (TREE_CODE (base2) == MEM_REF
2537 467920199 : || TREE_CODE (base2) == TARGET_MEM_REF);
2538 :
2539 : /* Canonicalize the pointer-vs-decl case. */
2540 467920199 : if (ind1_p && var2_p)
2541 : {
2542 126251206 : std::swap (offset1, offset2);
2543 126251206 : std::swap (max_size1, max_size2);
2544 126251206 : std::swap (base1, base2);
2545 126251206 : std::swap (ref1, ref2);
2546 126251206 : std::swap (ref1ref, ref2ref);
2547 126251206 : var1_p = true;
2548 126251206 : ind1_p = false;
2549 126251206 : var2_p = false;
2550 126251206 : ind2_p = true;
2551 : }
2552 :
2553 : /* First defer to TBAA if possible. */
2554 467920199 : if (tbaa_p
2555 208206137 : && flag_strict_aliasing
2556 608699712 : && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
2557 : ao_ref_alias_set (ref2)))
2558 : return false;
2559 :
2560 : /* If the reference is based on a pointer that points to memory
2561 : that may not be written to then the other reference cannot possibly
2562 : clobber it. */
2563 416735181 : if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
2564 415336582 : && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
2565 831499722 : || (ind1_p
2566 99750344 : && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
2567 99517507 : && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
2568 : return false;
2569 :
2570 : /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2571 416052660 : if (var1_p && ind2_p)
2572 316412796 : return indirect_ref_may_alias_decl_p (ref2ref, base2,
2573 : offset2, max_size2, ref2->size,
2574 : ao_ref_alias_set (ref2),
2575 : ao_ref_base_alias_set (ref2),
2576 : ref1ref, base1,
2577 : offset1, max_size1, ref1->size,
2578 : ao_ref_alias_set (ref1),
2579 : ao_ref_base_alias_set (ref1),
2580 316412796 : tbaa_p);
2581 99639864 : else if (ind1_p && ind2_p)
2582 99639864 : return indirect_refs_may_alias_p (ref1ref, base1,
2583 : offset1, max_size1, ref1->size,
2584 : ao_ref_alias_set (ref1),
2585 : ao_ref_base_alias_set (ref1),
2586 : ref2ref, base2,
2587 : offset2, max_size2, ref2->size,
2588 : ao_ref_alias_set (ref2),
2589 : ao_ref_base_alias_set (ref2),
2590 99639864 : tbaa_p);
2591 :
2592 0 : gcc_unreachable ();
2593 : }
2594 :
2595 : /* Return true, if the two memory references REF1 and REF2 may alias
2596 : and update statistics. */
2597 :
2598 : bool
2599 2116132112 : refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2600 : {
2601 2116132112 : bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
2602 2116132112 : if (res)
2603 136222834 : ++alias_stats.refs_may_alias_p_may_alias;
2604 : else
2605 1979909278 : ++alias_stats.refs_may_alias_p_no_alias;
2606 2116132112 : return res;
2607 : }
2608 :
2609 : static bool
2610 65634041 : refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
2611 : {
2612 65634041 : ao_ref r1;
2613 65634041 : ao_ref_init (&r1, ref1);
2614 65634041 : return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
2615 : }
2616 :
2617 : bool
2618 4537224 : refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
2619 : {
2620 4537224 : ao_ref r1, r2;
2621 4537224 : ao_ref_init (&r1, ref1);
2622 4537224 : ao_ref_init (&r2, ref2);
2623 4537224 : return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
2624 : }
2625 :
2626 : /* Returns true if there is a anti-dependence for the STORE that
2627 : executes after the LOAD. */
2628 :
2629 : bool
2630 0 : refs_anti_dependent_p (tree load, tree store)
2631 : {
2632 0 : ao_ref r1, r2;
2633 0 : ao_ref_init (&r1, load);
2634 0 : ao_ref_init (&r2, store);
2635 0 : return refs_may_alias_p_1 (&r1, &r2, false);
2636 : }
2637 :
2638 : /* Returns true if there is a output dependence for the stores
2639 : STORE1 and STORE2. */
2640 :
2641 : bool
2642 0 : refs_output_dependent_p (tree store1, tree store2)
2643 : {
2644 0 : ao_ref r1, r2;
2645 0 : ao_ref_init (&r1, store1);
2646 0 : ao_ref_init (&r2, store2);
2647 0 : return refs_may_alias_p_1 (&r1, &r2, false);
2648 : }
2649 :
2650 : /* Returns true if and only if REF may alias any access stored in TT.
2651 : IF TBAA_P is true, use TBAA oracle. */
2652 :
2653 : static bool
2654 50147103 : modref_may_conflict (const gcall *stmt,
2655 : modref_tree <alias_set_type> *tt, ao_ref *ref, bool tbaa_p)
2656 : {
2657 50147103 : alias_set_type base_set, ref_set;
2658 50147103 : bool global_memory_ok = false;
2659 :
2660 50147103 : if (tt->every_base)
2661 : return true;
2662 :
2663 9472397 : if (!dbg_cnt (ipa_mod_ref))
2664 : return true;
2665 :
2666 9472397 : base_set = ao_ref_base_alias_set (ref);
2667 :
2668 9472397 : ref_set = ao_ref_alias_set (ref);
2669 :
2670 9472397 : int num_tests = 0, max_tests = param_modref_max_tests;
2671 38618983 : for (auto base_node : tt->bases)
2672 : {
2673 14524973 : if (tbaa_p && flag_strict_aliasing)
2674 : {
2675 11132078 : if (num_tests >= max_tests)
2676 : return true;
2677 11132078 : alias_stats.modref_tests++;
2678 11132078 : if (!alias_sets_conflict_p (base_set, base_node->base))
2679 3684739 : continue;
2680 7447339 : num_tests++;
2681 : }
2682 :
2683 10840234 : if (base_node->every_ref)
2684 : return true;
2685 :
2686 41537659 : for (auto ref_node : base_node->refs)
2687 : {
2688 : /* Do not repeat same test as before. */
2689 12900676 : if ((ref_set != base_set || base_node->base != ref_node->ref)
2690 8276260 : && tbaa_p && flag_strict_aliasing)
2691 : {
2692 5627056 : if (num_tests >= max_tests)
2693 : return true;
2694 5595784 : alias_stats.modref_tests++;
2695 5595784 : if (!alias_sets_conflict_p (ref_set, ref_node->ref))
2696 1591627 : continue;
2697 4004157 : num_tests++;
2698 : }
2699 :
2700 11277777 : if (ref_node->every_access)
2701 : return true;
2702 :
2703 : /* TBAA checks did not disambiguate, try individual accesses. */
2704 36172516 : for (auto access_node : ref_node->accesses)
2705 : {
2706 10301170 : if (num_tests >= max_tests)
2707 1777023 : return true;
2708 :
2709 10301170 : if (access_node.parm_index == MODREF_GLOBAL_MEMORY_PARM)
2710 : {
2711 2159572 : if (global_memory_ok)
2712 1516807 : continue;
2713 2159572 : if (ref_may_alias_global_p (ref, true))
2714 : return true;
2715 1486503 : global_memory_ok = true;
2716 1486503 : num_tests++;
2717 1486503 : continue;
2718 : }
2719 :
2720 8141598 : tree arg = access_node.get_call_arg (stmt);
2721 8141598 : if (!arg)
2722 : return true;
2723 :
2724 8140523 : alias_stats.modref_baseptr_tests++;
2725 :
2726 8140523 : if (integer_zerop (arg) && flag_delete_null_pointer_checks)
2727 30304 : continue;
2728 :
2729 : /* PTA oracle will be unhapy of arg is not an pointer. */
2730 8110219 : if (!POINTER_TYPE_P (TREE_TYPE (arg)))
2731 : return true;
2732 :
2733 : /* If we don't have base pointer, give up. */
2734 8110219 : if (!ref->ref && !ref->base)
2735 0 : continue;
2736 :
2737 8110219 : ao_ref ref2;
2738 8110219 : if (access_node.get_ao_ref (stmt, &ref2))
2739 : {
2740 6374893 : ref2.ref_alias_set = ref_node->ref;
2741 6374893 : ref2.base_alias_set = base_node->base;
2742 6374893 : if (refs_may_alias_p_1 (&ref2, ref, tbaa_p))
2743 : return true;
2744 : }
2745 1735326 : else if (ptr_deref_may_alias_ref_p_1 (arg, ref))
2746 : return true;
2747 :
2748 7007340 : num_tests++;
2749 : }
2750 : }
2751 : }
2752 : return false;
2753 : }
2754 :
2755 : /* Check if REF conflicts with call using "fn spec" attribute.
2756 : If CLOBBER is true we are checking for writes, otherwise check loads.
2757 :
2758 : Return 0 if there are no conflicts (except for possible function call
2759 : argument reads), 1 if there are conflicts and -1 if we can not decide by
2760 : fn spec. */
2761 :
2762 : static int
2763 160901271 : check_fnspec (gcall *call, ao_ref *ref, bool clobber)
2764 : {
2765 160901271 : attr_fnspec fnspec = gimple_call_fnspec (call);
2766 160901271 : if (fnspec.known_p ())
2767 : {
2768 81047925 : if (clobber
2769 81047925 : ? !fnspec.global_memory_written_p ()
2770 9314374 : : !fnspec.global_memory_read_p ())
2771 : {
2772 97854131 : for (unsigned int i = 0; i < gimple_call_num_args (call); i++)
2773 94647430 : if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, i)))
2774 66826105 : && (!fnspec.arg_specified_p (i)
2775 38941093 : || (clobber ? fnspec.arg_maybe_written_p (i)
2776 3533556 : : fnspec.arg_maybe_read_p (i))))
2777 : {
2778 24261701 : ao_ref dref;
2779 24261701 : tree size = NULL_TREE;
2780 24261701 : unsigned int size_arg;
2781 :
2782 24261701 : if (!fnspec.arg_specified_p (i))
2783 : ;
2784 24258072 : else if (fnspec.arg_max_access_size_given_by_arg_p
2785 24258072 : (i, &size_arg))
2786 15956792 : size = gimple_call_arg (call, size_arg);
2787 8301280 : else if (fnspec.arg_access_size_given_by_type_p (i))
2788 : {
2789 28234 : tree callee = gimple_call_fndecl (call);
2790 28234 : tree t = TYPE_ARG_TYPES (TREE_TYPE (callee));
2791 :
2792 57663 : for (unsigned int p = 0; p < i; p++)
2793 29429 : t = TREE_CHAIN (t);
2794 28234 : size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_VALUE (t)));
2795 : }
2796 15985026 : poly_int64 size_hwi;
2797 15985026 : if (size
2798 15985026 : && poly_int_tree_p (size, &size_hwi)
2799 29215679 : && coeffs_in_range_p (size_hwi, 0,
2800 : HOST_WIDE_INT_MAX / BITS_PER_UNIT))
2801 : {
2802 13229115 : size_hwi = size_hwi * BITS_PER_UNIT;
2803 13229115 : ao_ref_init_from_ptr_and_range (&dref,
2804 : gimple_call_arg (call, i),
2805 : true, 0, -1, size_hwi);
2806 : }
2807 : else
2808 11032586 : ao_ref_init_from_ptr_and_range (&dref,
2809 : gimple_call_arg (call, i),
2810 : false, 0, -1, -1);
2811 24261701 : if (refs_may_alias_p_1 (&dref, ref, false))
2812 4711526 : return 1;
2813 : }
2814 31088084 : if (clobber
2815 27252971 : && fnspec.errno_maybe_written_p ()
2816 39498059 : && targetm.ref_may_alias_errno (ref))
2817 27497 : return 1;
2818 : return 0;
2819 : }
2820 : }
2821 :
2822 : /* FIXME: we should handle barriers more consistently, but for now leave the
2823 : check here. */
2824 125101661 : if (gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2825 7020780 : switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
2826 : {
2827 : /* __sync_* builtins and some OpenMP builtins act as threading
2828 : barriers. */
2829 : #undef DEF_SYNC_BUILTIN
2830 : #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2831 : #include "sync-builtins.def"
2832 : #undef DEF_SYNC_BUILTIN
2833 : case BUILT_IN_GOMP_ATOMIC_START:
2834 : case BUILT_IN_GOMP_ATOMIC_END:
2835 : case BUILT_IN_GOMP_REDUCTION_START:
2836 : case BUILT_IN_GOMP_REDUCTION_END:
2837 : case BUILT_IN_GOMP_BARRIER:
2838 : case BUILT_IN_GOMP_BARRIER_CANCEL:
2839 : case BUILT_IN_GOMP_TASKWAIT:
2840 : case BUILT_IN_GOMP_TASKGROUP_END:
2841 : case BUILT_IN_GOMP_CRITICAL_START:
2842 : case BUILT_IN_GOMP_CRITICAL_END:
2843 : case BUILT_IN_GOMP_CRITICAL_NAME_START:
2844 : case BUILT_IN_GOMP_CRITICAL_NAME_END:
2845 : case BUILT_IN_GOMP_LOOP_END:
2846 : case BUILT_IN_GOMP_LOOP_END_CANCEL:
2847 : case BUILT_IN_GOMP_ORDERED_START:
2848 : case BUILT_IN_GOMP_ORDERED_END:
2849 : case BUILT_IN_GOMP_SECTIONS_END:
2850 : case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2851 : case BUILT_IN_GOMP_SINGLE_COPY_START:
2852 : case BUILT_IN_GOMP_SINGLE_COPY_END:
2853 : return 1;
2854 :
2855 5611708 : default:
2856 5611708 : return -1;
2857 : }
2858 : return -1;
2859 : }
2860 :
2861 : /* If the call CALL may use the memory reference REF return true,
2862 : otherwise return false. */
2863 :
2864 : static bool
2865 48113163 : ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
2866 : {
2867 48113163 : tree base, callee;
2868 48113163 : unsigned i;
2869 48113163 : int flags = gimple_call_flags (call);
2870 :
2871 48113163 : if (flags & (ECF_CONST|ECF_NOVOPS))
2872 378579 : goto process_args;
2873 :
2874 : /* A call that is not without side-effects might involve volatile
2875 : accesses and thus conflicts with all other volatile accesses. */
2876 47734584 : if (ref->volatile_p)
2877 : return true;
2878 :
2879 47734336 : if (gimple_call_internal_p (call))
2880 82949 : switch (gimple_call_internal_fn (call))
2881 : {
2882 : case IFN_MASK_STORE:
2883 : case IFN_SCATTER_STORE:
2884 : case IFN_MASK_SCATTER_STORE:
2885 : case IFN_LEN_STORE:
2886 : case IFN_MASK_LEN_STORE:
2887 : return false;
2888 0 : case IFN_MASK_STORE_LANES:
2889 0 : case IFN_MASK_LEN_STORE_LANES:
2890 0 : goto process_args;
2891 1104 : case IFN_MASK_LOAD:
2892 1104 : case IFN_LEN_LOAD:
2893 1104 : case IFN_MASK_LEN_LOAD:
2894 1104 : case IFN_MASK_LOAD_LANES:
2895 1104 : case IFN_MASK_LEN_LOAD_LANES:
2896 1104 : {
2897 1104 : ao_ref rhs_ref;
2898 1104 : tree lhs = gimple_call_lhs (call);
2899 1104 : if (lhs)
2900 : {
2901 1104 : ao_ref_init_from_ptr_and_size (&rhs_ref,
2902 : gimple_call_arg (call, 0),
2903 1104 : TYPE_SIZE_UNIT (TREE_TYPE (lhs)));
2904 : /* We cannot make this a known-size access since otherwise
2905 : we disambiguate against refs to decls that are smaller. */
2906 1104 : rhs_ref.size = -1;
2907 2208 : rhs_ref.ref_alias_set = rhs_ref.base_alias_set
2908 2204 : = tbaa_p ? get_deref_alias_set (TREE_TYPE
2909 : (gimple_call_arg (call, 1))) : 0;
2910 1104 : return refs_may_alias_p_1 (ref, &rhs_ref, tbaa_p);
2911 : }
2912 0 : break;
2913 : }
2914 : default:;
2915 : }
2916 :
2917 47727412 : callee = gimple_call_fndecl (call);
2918 47727412 : if (callee != NULL_TREE)
2919 : {
2920 46158379 : struct cgraph_node *node = cgraph_node::get (callee);
2921 : /* We can not safely optimize based on summary of callee if it does
2922 : not always bind to current def: it is possible that memory load
2923 : was optimized out earlier and the interposed variant may not be
2924 : optimized this way. */
2925 46158379 : if (node && node->binds_to_current_def_p ())
2926 : {
2927 6518374 : modref_summary *summary = get_modref_function_summary (node);
2928 6518374 : if (summary && !summary->calls_interposable)
2929 : {
2930 3977595 : if (!modref_may_conflict (call, summary->loads, ref, tbaa_p))
2931 : {
2932 804907 : alias_stats.modref_use_no_alias++;
2933 804907 : if (dump_file && (dump_flags & TDF_DETAILS))
2934 : {
2935 24 : fprintf (dump_file,
2936 : "ipa-modref: call stmt ");
2937 24 : print_gimple_stmt (dump_file, call, 0);
2938 24 : fprintf (dump_file,
2939 : "ipa-modref: call to %s does not use ",
2940 : node->dump_name ());
2941 24 : if (!ref->ref && ref->base)
2942 : {
2943 3 : fprintf (dump_file, "base: ");
2944 3 : print_generic_expr (dump_file, ref->base);
2945 : }
2946 21 : else if (ref->ref)
2947 : {
2948 21 : fprintf (dump_file, "ref: ");
2949 21 : print_generic_expr (dump_file, ref->ref);
2950 : }
2951 24 : fprintf (dump_file, " alias sets: %i->%i\n",
2952 : ao_ref_base_alias_set (ref),
2953 : ao_ref_alias_set (ref));
2954 : }
2955 804907 : goto process_args;
2956 : }
2957 3172688 : alias_stats.modref_use_may_alias++;
2958 : }
2959 : }
2960 : }
2961 :
2962 46922505 : base = ao_ref_base (ref);
2963 46922505 : if (!base)
2964 : return true;
2965 :
2966 : /* If the reference is based on a decl that is not aliased the call
2967 : cannot possibly use it. */
2968 46922505 : if (DECL_P (base)
2969 41876189 : && !may_be_aliased (base)
2970 : /* But local statics can be used through recursion. */
2971 70367418 : && !is_global_var (base))
2972 23124005 : goto process_args;
2973 :
2974 23798500 : if (int res = check_fnspec (call, ref, false))
2975 : {
2976 19963387 : if (res == 1)
2977 : return true;
2978 : }
2979 : else
2980 3835113 : goto process_args;
2981 :
2982 : /* Check if base is a global static variable that is not read
2983 : by the function. */
2984 19422653 : if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2985 : {
2986 1019027 : struct cgraph_node *node = cgraph_node::get (callee);
2987 1019027 : bitmap read;
2988 1019027 : int id;
2989 :
2990 : /* FIXME: Callee can be an OMP builtin that does not have a call graph
2991 : node yet. We should enforce that there are nodes for all decls in the
2992 : IL and remove this check instead. */
2993 1019027 : if (node
2994 1018328 : && (id = ipa_reference_var_uid (base)) != -1
2995 156496 : && (read = ipa_reference_get_read_global (node))
2996 1036373 : && !bitmap_bit_p (read, id))
2997 12104 : goto process_args;
2998 : }
2999 :
3000 : /* Check if the base variable is call-used. */
3001 19410549 : if (DECL_P (base))
3002 : {
3003 15948581 : if (pt_solution_includes (gimple_call_use_set (call), base))
3004 : return true;
3005 : }
3006 3461968 : else if ((TREE_CODE (base) == MEM_REF
3007 3461968 : || TREE_CODE (base) == TARGET_MEM_REF)
3008 3461968 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
3009 : {
3010 3461729 : const pt_solution *pt = ssa_name_pt_solution (TREE_OPERAND (base, 0));
3011 3451378 : if (!pt)
3012 : return true;
3013 :
3014 3449604 : if (pt_solutions_intersect (gimple_call_use_set (call), pt))
3015 : return true;
3016 : }
3017 : else
3018 : return true;
3019 :
3020 : /* Inspect call arguments for passed-by-value aliases. */
3021 31909419 : process_args:
3022 92155129 : for (i = 0; i < gimple_call_num_args (call); ++i)
3023 : {
3024 64513696 : tree op = gimple_call_arg (call, i);
3025 64513696 : int flags = gimple_call_arg_flags (call, i);
3026 :
3027 64513696 : if (flags & (EAF_UNUSED | EAF_NO_DIRECT_READ))
3028 826736 : continue;
3029 :
3030 63686960 : if (TREE_CODE (op) == WITH_SIZE_EXPR)
3031 504 : op = TREE_OPERAND (op, 0);
3032 :
3033 63686960 : if (TREE_CODE (op) != SSA_NAME
3034 63686960 : && !is_gimple_min_invariant (op))
3035 : {
3036 11236188 : ao_ref r;
3037 11236188 : ao_ref_init (&r, op);
3038 11236188 : if (refs_may_alias_p_1 (&r, ref, tbaa_p))
3039 4267986 : return true;
3040 : }
3041 : }
3042 :
3043 : return false;
3044 : }
3045 :
3046 : static bool
3047 48103991 : ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
3048 : {
3049 48103991 : bool res;
3050 48103991 : res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
3051 48103991 : if (res)
3052 20459970 : ++alias_stats.ref_maybe_used_by_call_p_may_alias;
3053 : else
3054 27644021 : ++alias_stats.ref_maybe_used_by_call_p_no_alias;
3055 48103991 : return res;
3056 : }
3057 :
3058 :
3059 : /* If the statement STMT may use the memory reference REF return
3060 : true, otherwise return false. */
3061 :
3062 : bool
3063 387396664 : ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
3064 : {
3065 387396664 : if (auto assign = dyn_cast<gassign *> (stmt))
3066 : {
3067 332950802 : tree rhs;
3068 :
3069 : /* All memory assign statements are single. */
3070 332950802 : if (!gimple_assign_single_p (assign))
3071 : return false;
3072 :
3073 332950802 : rhs = gimple_assign_rhs1 (assign);
3074 332950802 : if (is_gimple_reg (rhs)
3075 271280468 : || is_gimple_min_invariant (rhs)
3076 440077243 : || gimple_assign_rhs_code (assign) == CONSTRUCTOR)
3077 : return false;
3078 :
3079 63492022 : return refs_may_alias_p (rhs, ref, tbaa_p);
3080 : }
3081 54445862 : else if (is_gimple_call (stmt))
3082 48103991 : return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
3083 6341871 : else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
3084 : {
3085 5583071 : tree retval = gimple_return_retval (return_stmt);
3086 5583071 : if (retval
3087 2943272 : && TREE_CODE (retval) != SSA_NAME
3088 2317343 : && !is_gimple_min_invariant (retval)
3089 7725090 : && refs_may_alias_p (retval, ref, tbaa_p))
3090 : return true;
3091 : /* If ref escapes the function then the return acts as a use. */
3092 3982302 : tree base = ao_ref_base (ref);
3093 3982302 : if (!base)
3094 : ;
3095 3982302 : else if (DECL_P (base))
3096 1521555 : return is_global_var (base);
3097 2460747 : else if (TREE_CODE (base) == MEM_REF
3098 2460747 : || TREE_CODE (base) == TARGET_MEM_REF)
3099 2460649 : return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0), false);
3100 : return false;
3101 : }
3102 :
3103 : return true;
3104 : }
3105 :
3106 : bool
3107 833228 : ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
3108 : {
3109 833228 : ao_ref r;
3110 833228 : ao_ref_init (&r, ref);
3111 833228 : return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
3112 : }
3113 :
3114 : /* If the call in statement CALL may clobber the memory reference REF
3115 : return true, otherwise return false. */
3116 :
3117 : bool
3118 351234975 : call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
3119 : {
3120 351234975 : tree base;
3121 351234975 : tree callee;
3122 :
3123 : /* If the call is pure or const it cannot clobber anything. */
3124 351234975 : if (gimple_call_flags (call)
3125 351234975 : & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
3126 : return false;
3127 345780650 : if (gimple_call_internal_p (call))
3128 1040111 : switch (auto fn = gimple_call_internal_fn (call))
3129 : {
3130 : /* Treat these internal calls like ECF_PURE for aliasing,
3131 : they don't write to any memory the program should care about.
3132 : They have important other side-effects, and read memory,
3133 : so can't be ECF_NOVOPS. */
3134 : case IFN_UBSAN_NULL:
3135 : case IFN_UBSAN_BOUNDS:
3136 : case IFN_UBSAN_VPTR:
3137 : case IFN_UBSAN_OBJECT_SIZE:
3138 : case IFN_UBSAN_PTR:
3139 : case IFN_ASAN_CHECK:
3140 : return false;
3141 11903 : case IFN_MASK_STORE:
3142 11903 : case IFN_LEN_STORE:
3143 11903 : case IFN_MASK_LEN_STORE:
3144 11903 : case IFN_MASK_STORE_LANES:
3145 11903 : case IFN_MASK_LEN_STORE_LANES:
3146 11903 : {
3147 11903 : tree rhs = gimple_call_arg (call,
3148 11903 : internal_fn_stored_value_index (fn));
3149 11903 : ao_ref lhs_ref;
3150 11903 : ao_ref_init_from_ptr_and_size (&lhs_ref, gimple_call_arg (call, 0),
3151 11903 : TYPE_SIZE_UNIT (TREE_TYPE (rhs)));
3152 : /* We cannot make this a known-size access since otherwise
3153 : we disambiguate against refs to decls that are smaller. */
3154 11903 : lhs_ref.size = -1;
3155 23806 : lhs_ref.ref_alias_set = lhs_ref.base_alias_set
3156 11903 : = tbaa_p ? get_deref_alias_set
3157 11183 : (TREE_TYPE (gimple_call_arg (call, 1))) : 0;
3158 11903 : return refs_may_alias_p_1 (ref, &lhs_ref, tbaa_p);
3159 : }
3160 : default:
3161 : break;
3162 : }
3163 :
3164 345091282 : callee = gimple_call_fndecl (call);
3165 :
3166 345091282 : if (callee != NULL_TREE && !ref->volatile_p)
3167 : {
3168 324285863 : struct cgraph_node *node = cgraph_node::get (callee);
3169 324285863 : if (node)
3170 : {
3171 323992827 : modref_summary *summary = get_modref_function_summary (node);
3172 323992827 : if (summary)
3173 : {
3174 46169508 : if (!modref_may_conflict (call, summary->stores, ref, tbaa_p)
3175 46169508 : && (!summary->writes_errno
3176 292402 : || !targetm.ref_may_alias_errno (ref)))
3177 : {
3178 4791913 : alias_stats.modref_clobber_no_alias++;
3179 4791913 : if (dump_file && (dump_flags & TDF_DETAILS))
3180 : {
3181 52 : fprintf (dump_file,
3182 : "ipa-modref: call stmt ");
3183 52 : print_gimple_stmt (dump_file, call, 0);
3184 52 : fprintf (dump_file,
3185 : "ipa-modref: call to %s does not clobber ",
3186 : node->dump_name ());
3187 52 : if (!ref->ref && ref->base)
3188 : {
3189 32 : fprintf (dump_file, "base: ");
3190 32 : print_generic_expr (dump_file, ref->base);
3191 : }
3192 20 : else if (ref->ref)
3193 : {
3194 20 : fprintf (dump_file, "ref: ");
3195 20 : print_generic_expr (dump_file, ref->ref);
3196 : }
3197 52 : fprintf (dump_file, " alias sets: %i->%i\n",
3198 : ao_ref_base_alias_set (ref),
3199 : ao_ref_alias_set (ref));
3200 : }
3201 : return false;
3202 : }
3203 41377595 : alias_stats.modref_clobber_may_alias++;
3204 : }
3205 : }
3206 : }
3207 :
3208 340299369 : base = ao_ref_base (ref);
3209 340299369 : if (!base)
3210 : return true;
3211 :
3212 340299369 : if (TREE_CODE (base) == SSA_NAME
3213 340297290 : || CONSTANT_CLASS_P (base))
3214 : return false;
3215 :
3216 : /* A call that is not without side-effects might involve volatile
3217 : accesses and thus conflicts with all other volatile accesses. */
3218 332137400 : if (ref->volatile_p)
3219 : return true;
3220 :
3221 : /* If the reference is based on a decl that is not aliased the call
3222 : cannot possibly clobber it. */
3223 330634678 : if (DECL_P (base)
3224 299913938 : && !may_be_aliased (base)
3225 : /* But local non-readonly statics can be modified through recursion
3226 : or the call may implement a threading barrier which we must
3227 : treat as may-def. */
3228 534220438 : && (TREE_READONLY (base)
3229 196013324 : || !is_global_var (base)))
3230 : return false;
3231 :
3232 : /* If the reference is based on a pointer that points to memory
3233 : that may not be written to then the call cannot possibly clobber it. */
3234 137455116 : if ((TREE_CODE (base) == MEM_REF
3235 137455116 : || TREE_CODE (base) == TARGET_MEM_REF)
3236 30720740 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3237 167772674 : && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
3238 : return false;
3239 :
3240 137102771 : if (int res = check_fnspec (call, ref, true))
3241 : {
3242 109877297 : if (res == 1)
3243 : return true;
3244 : }
3245 : else
3246 : return false;
3247 :
3248 : /* Check if base is a global static variable that is not written
3249 : by the function. */
3250 104269936 : if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
3251 : {
3252 11099599 : struct cgraph_node *node = cgraph_node::get (callee);
3253 11099599 : bitmap written;
3254 11099599 : int id;
3255 :
3256 11099599 : if (node
3257 11099097 : && (id = ipa_reference_var_uid (base)) != -1
3258 4966913 : && (written = ipa_reference_get_written_global (node))
3259 11194802 : && !bitmap_bit_p (written, id))
3260 : return false;
3261 : }
3262 :
3263 : /* Check if the base variable is call-clobbered. */
3264 104189785 : if (DECL_P (base))
3265 81723944 : return pt_solution_includes (gimple_call_clobber_set (call), base);
3266 22465841 : else if ((TREE_CODE (base) == MEM_REF
3267 22465841 : || TREE_CODE (base) == TARGET_MEM_REF)
3268 22465841 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
3269 : {
3270 22129483 : const pt_solution *pt = ssa_name_pt_solution (TREE_OPERAND (base, 0));
3271 21401329 : if (!pt)
3272 : return true;
3273 :
3274 21302755 : return pt_solutions_intersect (gimple_call_clobber_set (call), pt);
3275 : }
3276 :
3277 : return true;
3278 : }
3279 :
3280 : /* If the call in statement CALL may clobber the memory reference REF
3281 : return true, otherwise return false. */
3282 :
3283 : bool
3284 385944 : call_may_clobber_ref_p (gcall *call, tree ref, bool tbaa_p)
3285 : {
3286 385944 : bool res;
3287 385944 : ao_ref r;
3288 385944 : ao_ref_init (&r, ref);
3289 385944 : res = call_may_clobber_ref_p_1 (call, &r, tbaa_p);
3290 385944 : if (res)
3291 43556 : ++alias_stats.call_may_clobber_ref_p_may_alias;
3292 : else
3293 342388 : ++alias_stats.call_may_clobber_ref_p_no_alias;
3294 385944 : return res;
3295 : }
3296 :
3297 :
3298 : /* If the statement STMT may clobber the memory reference REF return true,
3299 : otherwise return false. */
3300 :
3301 : bool
3302 2100504041 : stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
3303 : {
3304 2100504041 : if (is_gimple_call (stmt))
3305 : {
3306 356772311 : tree lhs = gimple_call_lhs (stmt);
3307 356772311 : if (lhs
3308 164116028 : && TREE_CODE (lhs) != SSA_NAME)
3309 : {
3310 62746139 : ao_ref r;
3311 62746139 : ao_ref_init (&r, lhs);
3312 62746139 : if (refs_may_alias_p_1 (ref, &r, tbaa_p))
3313 6034201 : return true;
3314 : }
3315 :
3316 350738110 : return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref, tbaa_p);
3317 : }
3318 1743731730 : else if (gimple_assign_single_p (stmt))
3319 : {
3320 1735284908 : tree lhs = gimple_assign_lhs (stmt);
3321 1735284908 : if (TREE_CODE (lhs) != SSA_NAME)
3322 : {
3323 1734345758 : ao_ref r;
3324 1734345758 : ao_ref_init (&r, lhs);
3325 1734345758 : return refs_may_alias_p_1 (ref, &r, tbaa_p);
3326 : }
3327 : }
3328 8446822 : else if (gimple_code (stmt) == GIMPLE_ASM)
3329 7258203 : return true;
3330 :
3331 : return false;
3332 : }
3333 :
3334 : bool
3335 4459732 : stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
3336 : {
3337 4459732 : ao_ref r;
3338 4459732 : ao_ref_init (&r, ref);
3339 4459732 : return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
3340 : }
3341 :
3342 : /* Return true if store1 and store2 described by corresponding tuples
3343 : <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
3344 : address. */
3345 :
3346 : static bool
3347 149777195 : same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
3348 : poly_int64 max_size1,
3349 : tree base2, poly_int64 offset2, poly_int64 size2,
3350 : poly_int64 max_size2)
3351 : {
3352 : /* Offsets need to be 0. */
3353 149777195 : if (maybe_ne (offset1, 0)
3354 149777195 : || maybe_ne (offset2, 0))
3355 : return false;
3356 :
3357 41630075 : bool base1_obj_p = SSA_VAR_P (base1);
3358 41630075 : bool base2_obj_p = SSA_VAR_P (base2);
3359 :
3360 : /* We need one object. */
3361 41630075 : if (base1_obj_p == base2_obj_p)
3362 : return false;
3363 5232879 : tree obj = base1_obj_p ? base1 : base2;
3364 :
3365 : /* And we need one MEM_REF. */
3366 5232879 : bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
3367 5232879 : bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
3368 5232879 : if (base1_memref_p == base2_memref_p)
3369 : return false;
3370 5123504 : tree memref = base1_memref_p ? base1 : base2;
3371 :
3372 : /* Sizes need to be valid. */
3373 5123504 : if (!known_size_p (max_size1)
3374 5097874 : || !known_size_p (max_size2)
3375 5097523 : || !known_size_p (size1)
3376 10221027 : || !known_size_p (size2))
3377 : return false;
3378 :
3379 : /* Max_size needs to match size. */
3380 5097523 : if (maybe_ne (max_size1, size1)
3381 5097523 : || maybe_ne (max_size2, size2))
3382 : return false;
3383 :
3384 : /* Sizes need to match. */
3385 5047797 : if (maybe_ne (size1, size2))
3386 : return false;
3387 :
3388 :
3389 : /* Check that memref is a store to pointer with singleton points-to info. */
3390 1293792 : if (!integer_zerop (TREE_OPERAND (memref, 1)))
3391 : return false;
3392 959455 : tree ptr = TREE_OPERAND (memref, 0);
3393 959455 : if (TREE_CODE (ptr) != SSA_NAME)
3394 : return false;
3395 959158 : const pt_solution *pt = ssa_name_pt_solution (ptr);
3396 890201 : unsigned int pt_uid;
3397 890201 : if (pt == NULL
3398 890201 : || !pt_solution_singleton_or_null_p (pt, &pt_uid))
3399 : return false;
3400 :
3401 : /* Be conservative with non-call exceptions when the address might
3402 : be NULL. */
3403 225815 : if (cfun->can_throw_non_call_exceptions && !ptr_nonnull_p (ptr))
3404 : return false;
3405 :
3406 : /* Check that ptr points relative to obj. */
3407 225809 : unsigned int obj_uid = DECL_PT_UID (obj);
3408 225809 : if (obj_uid != pt_uid)
3409 : return false;
3410 :
3411 : /* Check that the object size is the same as the store size. That ensures us
3412 : that ptr points to the start of obj. */
3413 34058 : return (DECL_SIZE (obj)
3414 34058 : && poly_int_tree_p (DECL_SIZE (obj))
3415 102107 : && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
3416 : }
3417 :
3418 : /* Return true if REF is killed by an store described by
3419 : BASE, OFFSET, SIZE and MAX_SIZE. */
3420 :
3421 : static bool
3422 248872535 : store_kills_ref_p (tree base, poly_int64 offset, poly_int64 size,
3423 : poly_int64 max_size, ao_ref *ref)
3424 : {
3425 248872535 : poly_int64 ref_offset = ref->offset;
3426 : /* We can get MEM[symbol: sZ, index: D.8862_1] here,
3427 : so base == ref->base does not always hold. */
3428 248872535 : if (base != ref->base)
3429 : {
3430 : /* Try using points-to info. */
3431 149777195 : if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
3432 : ref->offset, ref->size, ref->max_size))
3433 : return true;
3434 :
3435 : /* If both base and ref->base are MEM_REFs, only compare the
3436 : first operand, and if the second operand isn't equal constant,
3437 : try to add the offsets into offset and ref_offset. */
3438 37343510 : if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
3439 176931951 : && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
3440 : {
3441 20260130 : if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
3442 20260130 : TREE_OPERAND (ref->base, 1)))
3443 : {
3444 7423729 : poly_offset_int off1 = mem_ref_offset (base);
3445 7423729 : off1 <<= LOG2_BITS_PER_UNIT;
3446 7423729 : off1 += offset;
3447 7423729 : poly_offset_int off2 = mem_ref_offset (ref->base);
3448 7423729 : off2 <<= LOG2_BITS_PER_UNIT;
3449 7423729 : off2 += ref_offset;
3450 7423729 : if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
3451 0 : size = -1;
3452 : }
3453 : }
3454 : else
3455 129516998 : size = -1;
3456 : }
3457 : /* For a must-alias check we need to be able to constrain
3458 : the access properly. */
3459 248872468 : return (known_eq (size, max_size)
3460 248872468 : && known_subrange_p (ref_offset, ref->max_size, offset, size));
3461 : }
3462 :
3463 : /* If STMT kills the memory reference REF return true, otherwise
3464 : return false. */
3465 :
3466 : bool
3467 303169174 : stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
3468 : {
3469 303169174 : if (!ao_ref_base (ref))
3470 : return false;
3471 :
3472 303169174 : if (gimple_has_lhs (stmt)
3473 262762574 : && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
3474 : /* The assignment is not necessarily carried out if it can throw
3475 : and we can catch it in the current function where we could inspect
3476 : the previous value. Similarly if the function can throw externally
3477 : and the ref does not die on the function return.
3478 : ??? We only need to care about the RHS throwing. For aggregate
3479 : assignments or similar calls and non-call exceptions the LHS
3480 : might throw as well.
3481 : ??? We also should care about possible longjmp, but since we
3482 : do not understand that longjmp is not using global memory we will
3483 : not consider a kill here since the function call will be considered
3484 : as possibly using REF. */
3485 256476084 : && !stmt_can_throw_internal (cfun, stmt)
3486 277602905 : && (!stmt_can_throw_external (cfun, stmt)
3487 3554247 : || !ref_may_alias_global_p (ref, false)))
3488 : {
3489 252315442 : tree lhs = gimple_get_lhs (stmt);
3490 : /* If LHS is literally a base of the access we are done. */
3491 252315442 : if (ref->ref)
3492 : {
3493 250836044 : tree base = ref->ref;
3494 250836044 : tree innermost_dropped_array_ref = NULL_TREE;
3495 250836044 : if (handled_component_p (base))
3496 : {
3497 158869589 : tree saved_lhs0 = NULL_TREE;
3498 281630116 : if (handled_component_p (lhs))
3499 : {
3500 122760527 : saved_lhs0 = TREE_OPERAND (lhs, 0);
3501 122760527 : TREE_OPERAND (lhs, 0) = integer_zero_node;
3502 : }
3503 271832782 : do
3504 : {
3505 : /* Just compare the outermost handled component, if
3506 : they are equal we have found a possible common
3507 : base. */
3508 271832782 : tree saved_base0 = TREE_OPERAND (base, 0);
3509 271832782 : TREE_OPERAND (base, 0) = integer_zero_node;
3510 271832782 : bool res = operand_equal_p (lhs, base, 0);
3511 271832782 : TREE_OPERAND (base, 0) = saved_base0;
3512 271832782 : if (res)
3513 : break;
3514 : /* Remember if we drop an array-ref that we need to
3515 : double-check not being at struct end. */
3516 256116737 : if (TREE_CODE (base) == ARRAY_REF
3517 256116737 : || TREE_CODE (base) == ARRAY_RANGE_REF)
3518 70596592 : innermost_dropped_array_ref = base;
3519 : /* Otherwise drop handled components of the access. */
3520 256116737 : base = saved_base0;
3521 : }
3522 399270281 : while (handled_component_p (base));
3523 158869589 : if (saved_lhs0)
3524 122760527 : TREE_OPERAND (lhs, 0) = saved_lhs0;
3525 : }
3526 : /* Finally check if the lhs has the same address and size as the
3527 : base candidate of the access. Watch out if we have dropped
3528 : an array-ref that might have flexible size, this means ref->ref
3529 : may be outside of the TYPE_SIZE of its base. */
3530 158869589 : if ((! innermost_dropped_array_ref
3531 69788252 : || ! array_ref_flexible_size_p (innermost_dropped_array_ref))
3532 309696628 : && (lhs == base
3533 237474622 : || (((TYPE_SIZE (TREE_TYPE (lhs))
3534 237474622 : == TYPE_SIZE (TREE_TYPE (base)))
3535 162099896 : || (TYPE_SIZE (TREE_TYPE (lhs))
3536 162099833 : && TYPE_SIZE (TREE_TYPE (base))
3537 162099736 : && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
3538 162099736 : TYPE_SIZE (TREE_TYPE (base)),
3539 : 0)))
3540 75374726 : && operand_equal_p (lhs, base,
3541 : OEP_ADDRESS_OF
3542 : | OEP_MATCH_SIDE_EFFECTS))))
3543 : {
3544 3144277 : ++alias_stats.stmt_kills_ref_p_yes;
3545 6029627 : return true;
3546 : }
3547 : }
3548 :
3549 : /* Now look for non-literal equal bases with the restriction of
3550 : handling constant offset and size. */
3551 : /* For a must-alias check we need to be able to constrain
3552 : the access properly. */
3553 249171165 : if (!ref->max_size_known_p ())
3554 : {
3555 1319970 : ++alias_stats.stmt_kills_ref_p_no;
3556 1319970 : return false;
3557 : }
3558 247851195 : poly_int64 size, offset, max_size;
3559 247851195 : bool reverse;
3560 247851195 : tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
3561 : &reverse);
3562 247851195 : if (store_kills_ref_p (base, offset, size, max_size, ref))
3563 : {
3564 1565380 : ++alias_stats.stmt_kills_ref_p_yes;
3565 1565380 : return true;
3566 : }
3567 : }
3568 :
3569 297139547 : if (is_gimple_call (stmt))
3570 : {
3571 24469842 : tree callee = gimple_call_fndecl (stmt);
3572 24469842 : struct cgraph_node *node;
3573 24469842 : modref_summary *summary;
3574 :
3575 : /* Try to disambiguate using modref summary. Modref records a vector
3576 : of stores with known offsets relative to function parameters that must
3577 : happen every execution of function. Find if we have a matching
3578 : store and verify that function can not use the value. */
3579 24469842 : if (callee != NULL_TREE
3580 23267580 : && (node = cgraph_node::get (callee)) != NULL
3581 23223069 : && node->binds_to_current_def_p ()
3582 2576554 : && (summary = get_modref_function_summary (node)) != NULL
3583 1534480 : && summary->kills.length ()
3584 : /* Check that we can not trap while evaluating function
3585 : parameters. This check is overly conservative. */
3586 24588048 : && (!cfun->can_throw_non_call_exceptions
3587 0 : || (!stmt_can_throw_internal (cfun, stmt)
3588 0 : && (!stmt_can_throw_external (cfun, stmt)
3589 0 : || !ref_may_alias_global_p (ref, false)))))
3590 : {
3591 492843 : for (auto kill : summary->kills)
3592 : {
3593 147397 : ao_ref dref;
3594 :
3595 : /* We only can do useful compares if we know the access range
3596 : precisely. */
3597 147397 : if (!kill.get_ao_ref (as_a <gcall *> (stmt), &dref))
3598 24 : continue;
3599 147373 : if (store_kills_ref_p (ao_ref_base (&dref), dref.offset,
3600 : dref.size, dref.max_size, ref))
3601 : {
3602 : /* For store to be killed it needs to not be used
3603 : earlier. */
3604 9172 : if (ref_maybe_used_by_call_p_1 (as_a <gcall *> (stmt), ref,
3605 : true)
3606 9172 : || !dbg_cnt (ipa_mod_ref))
3607 : break;
3608 4105 : if (dump_file && (dump_flags & TDF_DETAILS))
3609 : {
3610 2 : fprintf (dump_file,
3611 : "ipa-modref: call stmt ");
3612 2 : print_gimple_stmt (dump_file, stmt, 0);
3613 2 : fprintf (dump_file,
3614 : "ipa-modref: call to %s kills ",
3615 : node->dump_name ());
3616 2 : print_generic_expr (dump_file, ref->base);
3617 2 : fprintf (dump_file, "\n");
3618 : }
3619 4105 : ++alias_stats.modref_kill_yes;
3620 4105 : return true;
3621 : }
3622 : }
3623 114101 : ++alias_stats.modref_kill_no;
3624 : }
3625 24465737 : if (callee != NULL_TREE
3626 24465737 : && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3627 5114203 : switch (DECL_FUNCTION_CODE (callee))
3628 : {
3629 734329 : case BUILT_IN_FREE:
3630 734329 : {
3631 734329 : tree ptr = gimple_call_arg (stmt, 0);
3632 734329 : tree base = ao_ref_base (ref);
3633 734329 : if (base && TREE_CODE (base) == MEM_REF
3634 893741 : && TREE_OPERAND (base, 0) == ptr)
3635 : {
3636 28890 : ++alias_stats.stmt_kills_ref_p_yes;
3637 28890 : return true;
3638 : }
3639 : break;
3640 : }
3641 :
3642 1425349 : case BUILT_IN_MEMCPY:
3643 1425349 : case BUILT_IN_MEMPCPY:
3644 1425349 : case BUILT_IN_MEMMOVE:
3645 1425349 : case BUILT_IN_MEMSET:
3646 1425349 : case BUILT_IN_MEMCPY_CHK:
3647 1425349 : case BUILT_IN_MEMPCPY_CHK:
3648 1425349 : case BUILT_IN_MEMMOVE_CHK:
3649 1425349 : case BUILT_IN_MEMSET_CHK:
3650 1425349 : case BUILT_IN_STRNCPY:
3651 1425349 : case BUILT_IN_STPNCPY:
3652 1425349 : case BUILT_IN_CALLOC:
3653 1425349 : {
3654 : /* For a must-alias check we need to be able to constrain
3655 : the access properly. */
3656 1425349 : if (!ref->max_size_known_p ())
3657 : {
3658 73367 : ++alias_stats.stmt_kills_ref_p_no;
3659 557000 : return false;
3660 : }
3661 1351982 : tree dest;
3662 1351982 : tree len;
3663 :
3664 : /* In execution order a calloc call will never kill
3665 : anything. However, DSE will (ab)use this interface
3666 : to ask if a calloc call writes the same memory locations
3667 : as a later assignment, memset, etc. So handle calloc
3668 : in the expected way. */
3669 1351982 : if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
3670 : {
3671 1839 : tree arg0 = gimple_call_arg (stmt, 0);
3672 1839 : tree arg1 = gimple_call_arg (stmt, 1);
3673 1839 : if (TREE_CODE (arg0) != INTEGER_CST
3674 1697 : || TREE_CODE (arg1) != INTEGER_CST)
3675 : {
3676 174 : ++alias_stats.stmt_kills_ref_p_no;
3677 174 : return false;
3678 : }
3679 :
3680 1665 : dest = gimple_call_lhs (stmt);
3681 1665 : if (!dest)
3682 : {
3683 1 : ++alias_stats.stmt_kills_ref_p_no;
3684 1 : return false;
3685 : }
3686 1664 : len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
3687 : }
3688 : else
3689 : {
3690 1350143 : dest = gimple_call_arg (stmt, 0);
3691 1350143 : len = gimple_call_arg (stmt, 2);
3692 : }
3693 1351807 : if (!poly_int_tree_p (len))
3694 : return false;
3695 873967 : ao_ref dref;
3696 873967 : ao_ref_init_from_ptr_and_size (&dref, dest, len);
3697 873967 : if (store_kills_ref_p (ao_ref_base (&dref), dref.offset,
3698 : dref.size, dref.max_size, ref))
3699 : {
3700 5618 : ++alias_stats.stmt_kills_ref_p_yes;
3701 5618 : return true;
3702 : }
3703 868349 : break;
3704 : }
3705 :
3706 5917 : case BUILT_IN_VA_END:
3707 5917 : {
3708 5917 : tree ptr = gimple_call_arg (stmt, 0);
3709 5917 : if (TREE_CODE (ptr) == ADDR_EXPR)
3710 : {
3711 5866 : tree base = ao_ref_base (ref);
3712 5866 : if (TREE_OPERAND (ptr, 0) == base)
3713 : {
3714 1417 : ++alias_stats.stmt_kills_ref_p_yes;
3715 1417 : return true;
3716 : }
3717 : }
3718 : break;
3719 : }
3720 :
3721 : default:;
3722 : }
3723 : }
3724 296548135 : ++alias_stats.stmt_kills_ref_p_no;
3725 296548135 : return false;
3726 : }
3727 :
3728 : bool
3729 0 : stmt_kills_ref_p (gimple *stmt, tree ref)
3730 : {
3731 0 : ao_ref r;
3732 0 : ao_ref_init (&r, ref);
3733 0 : return stmt_kills_ref_p (stmt, &r);
3734 : }
3735 :
3736 : /* Return whether REF can be subject to store data races. */
3737 :
3738 : bool
3739 168422 : ref_can_have_store_data_races (tree ref)
3740 : {
3741 : /* With -fallow-store-data-races do not care about them. */
3742 168422 : if (flag_store_data_races)
3743 : return false;
3744 :
3745 167202 : tree base = get_base_address (ref);
3746 167202 : if (auto_var_p (base)
3747 167202 : && ! may_be_aliased (base))
3748 : /* Automatic variables not aliased are not subject to
3749 : data races. */
3750 : return false;
3751 :
3752 : return true;
3753 : }
3754 :
3755 :
3756 : /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3757 : TARGET or a statement clobbering the memory reference REF in which
3758 : case false is returned. The walk starts with VUSE, one argument of PHI. */
3759 :
3760 : static bool
3761 139886095 : maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3762 : ao_ref *ref, tree vuse, bool tbaa_p, unsigned int &limit,
3763 : bitmap *visited,
3764 : void *(*translate)(ao_ref *, tree, void *, translate_flags *),
3765 : bool (*is_backedge)(edge, void *),
3766 : translate_flags disambiguate_only,
3767 : void *data)
3768 : {
3769 139886095 : basic_block bb = gimple_bb (phi);
3770 :
3771 139886095 : if (!*visited)
3772 : {
3773 25049174 : *visited = BITMAP_ALLOC (NULL);
3774 25049174 : bitmap_tree_view (*visited);
3775 : }
3776 :
3777 139886095 : bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3778 :
3779 : /* Walk until we hit the target. */
3780 641382303 : while (vuse != target)
3781 : {
3782 450838222 : gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3783 : /* If we are searching for the target VUSE by walking up to
3784 : TARGET_BB dominating the original PHI we are finished once
3785 : we reach a default def or a definition in a block dominating
3786 : that block. Update TARGET and return. */
3787 450838222 : if (!target
3788 450838222 : && (gimple_nop_p (def_stmt)
3789 75532821 : || dominated_by_p (CDI_DOMINATORS,
3790 75532821 : target_bb, gimple_bb (def_stmt))))
3791 : {
3792 20957226 : target = vuse;
3793 20957226 : return true;
3794 : }
3795 :
3796 : /* When we reach the original PHI we are done. TARGET must be
3797 : reached via other edges in the caller. */
3798 429880996 : if (def_stmt == phi)
3799 : return true;
3800 : /* Recurse for other PHI nodes. */
3801 397051129 : else if (gphi *phi = dyn_cast <gphi *> (def_stmt))
3802 : {
3803 : /* An already visited PHI node ends the walk successfully. */
3804 48748391 : if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (phi))))
3805 : return true;
3806 43299159 : vuse = get_continuation_for_phi (phi, ref, tbaa_p, limit,
3807 : visited, translate, data, is_backedge,
3808 : disambiguate_only);
3809 43299159 : if (!vuse)
3810 : return false;
3811 38840987 : continue;
3812 : }
3813 348302738 : else if (gimple_nop_p (def_stmt))
3814 : return false;
3815 : else
3816 : {
3817 : /* A clobbering statement or the end of the IL ends it failing. */
3818 348302738 : if ((int)limit <= 0)
3819 : return false;
3820 348261632 : --limit;
3821 348261632 : if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3822 : {
3823 18278111 : translate_flags tf = disambiguate_only;
3824 18278111 : if (translate
3825 18278111 : && (*translate) (ref, vuse, data, &tf) == NULL)
3826 : ;
3827 : else
3828 14872126 : return false;
3829 : }
3830 : }
3831 : /* If we reach a new basic-block see if we already skipped it
3832 : in a previous walk that ended successfully. */
3833 333389506 : if (gimple_bb (def_stmt) != bb)
3834 : {
3835 143618322 : if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3836 : return true;
3837 132997942 : bb = gimple_bb (def_stmt);
3838 : }
3839 684379239 : vuse = gimple_vuse (def_stmt);
3840 : }
3841 : return true;
3842 : }
3843 :
3844 : /* qsort comparator for a pair of int and bool. For same int sort
3845 : true before false. */
3846 :
3847 : static int
3848 959476706 : cmp_intpair (const void *a_, const void *b_)
3849 : {
3850 959476706 : auto a = (const std::pair<int, bool> *)a_;
3851 959476706 : auto b = (const std::pair<int, bool> *)b_;
3852 959476706 : if (a->first == b->first)
3853 49489193 : return (int)b->second - (int)a->second;
3854 909987513 : return a->first - b->first;
3855 : }
3856 :
3857 : /* Starting from a PHI node for the virtual operand of the memory reference
3858 : REF find a continuation virtual operand that allows to continue walking
3859 : statements dominating PHI skipping only statements that cannot possibly
3860 : clobber REF. Decrements LIMIT for each alias disambiguation done
3861 : and aborts the walk, returning NULL_TREE if it reaches zero.
3862 : Returns NULL_TREE if no suitable virtual operand can be found. */
3863 :
3864 : tree
3865 109929694 : get_continuation_for_phi (gphi *phi, ao_ref *ref, bool tbaa_p,
3866 : unsigned int &limit, bitmap *visited,
3867 : void *(*translate)(ao_ref *, tree, void *,
3868 : translate_flags *),
3869 : void *data,
3870 : bool (*is_backedge)(edge, void *),
3871 : translate_flags disambiguate_only)
3872 : {
3873 109929694 : unsigned nargs = gimple_phi_num_args (phi);
3874 :
3875 : /* Through a single-argument PHI we can simply look through. */
3876 109929694 : if (nargs == 1)
3877 4654667 : return PHI_ARG_DEF (phi, 0);
3878 :
3879 : /* For two or more arguments try to pairwise skip non-aliasing code
3880 : until we hit the phi argument definition that dominates the other one. */
3881 105275027 : basic_block phi_bb = gimple_bb (phi);
3882 105275027 : tree arg0, arg1;
3883 105275027 : unsigned i;
3884 :
3885 : /* Find a candidate for the virtual operand which definition
3886 : dominates those of all others. */
3887 : /* First look if any of the args themselves satisfy this. */
3888 194849916 : for (i = 0; i < nargs; ++i)
3889 : {
3890 169275029 : arg0 = PHI_ARG_DEF (phi, i);
3891 169275029 : if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3892 : break;
3893 165749980 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3894 165749980 : if (def_bb != phi_bb
3895 165749980 : && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3896 : break;
3897 89574889 : arg0 = NULL_TREE;
3898 : }
3899 : /* If not, look if we can reach such candidate by walking defs
3900 : until we hit the immediate dominator. maybe_skip_until will
3901 : do that for us. */
3902 105275027 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3903 :
3904 : /* Sort the PHI arguments so we can quickly avoid walking duplicates.
3905 : Keep pairs of PHI arg and edge backedge state, sorting the backedge
3906 : case first which is the conservative walk kind in case we have
3907 : a duplicate PHI arg reachable both via a forward and a backward
3908 : edge. */
3909 105275027 : auto_vec<std::pair<int, bool>, 10> args;
3910 349936668 : for (i = 0; i < nargs; ++i)
3911 244661641 : args.safe_push (std::make_pair (SSA_NAME_VERSION (PHI_ARG_DEF (phi, i)),
3912 : (!is_backedge
3913 469490178 : || is_backedge
3914 224828537 : (gimple_phi_arg_edge (phi, i),
3915 : data))));
3916 105275027 : args.qsort (cmp_intpair);
3917 :
3918 : /* Then check against the (to be) found candidate. */
3919 310780893 : for (i = 0; i < nargs; ++i)
3920 : {
3921 224877270 : arg1 = ssa_name (args[i].first);
3922 224877270 : if (arg1 == arg0)
3923 : ;
3924 141854480 : else if (i > 0 && args[i].first == args[i-1].first)
3925 : /* Already walked paths can be skipped. */
3926 : ;
3927 139886095 : else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, tbaa_p,
3928 : limit, visited, translate, is_backedge,
3929 : /* Do not valueize when walking over
3930 : backedges. */
3931 139886095 : !args[i].second
3932 : ? disambiguate_only : TR_DISAMBIGUATE,
3933 : data))
3934 : return NULL_TREE;
3935 : }
3936 :
3937 85903623 : return arg0;
3938 105275027 : }
3939 :
3940 : /* Based on the memory reference REF and its virtual use VUSE call
3941 : WALKER for each virtual use that is equivalent to VUSE, including VUSE
3942 : itself. That is, for each virtual use for which its defining statement
3943 : does not clobber REF.
3944 :
3945 : WALKER is called with REF, the current virtual use and DATA. If
3946 : WALKER returns non-NULL the walk stops and its result is returned.
3947 : At the end of a non-successful walk NULL is returned.
3948 :
3949 : TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3950 : use which definition is a statement that may clobber REF and DATA.
3951 : If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3952 : If TRANSLATE returns non-NULL the walk stops and its result is returned.
3953 : If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3954 : to adjust REF and *DATA to make that valid.
3955 :
3956 : VALUEIZE if non-NULL is called with the next VUSE that is considered
3957 : and return value is substituted for that. This can be used to
3958 : implement optimistic value-numbering for example. Note that the
3959 : VUSE argument is assumed to be valueized already.
3960 :
3961 : LIMIT specifies the number of alias queries we are allowed to do,
3962 : the walk stops when it reaches zero and NULL is returned. LIMIT
3963 : is decremented by the number of alias queries (plus adjustments
3964 : done by the callbacks) upon return.
3965 :
3966 : TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3967 :
3968 : void *
3969 70557284 : walk_non_aliased_vuses (ao_ref *ref, tree vuse, bool tbaa_p,
3970 : void *(*walker)(ao_ref *, tree, void *),
3971 : void *(*translate)(ao_ref *, tree, void *,
3972 : translate_flags *),
3973 : bool (*is_backedge)(edge, void *),
3974 : tree (*valueize)(tree),
3975 : unsigned &limit, void *data)
3976 : {
3977 70557284 : bitmap visited = NULL;
3978 70557284 : void *res;
3979 :
3980 70557284 : timevar_push (TV_ALIAS_STMT_WALK);
3981 :
3982 1173336715 : do
3983 : {
3984 1173336715 : gimple *def_stmt;
3985 :
3986 : /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3987 1173336715 : res = (*walker) (ref, vuse, data);
3988 : /* Abort walk. */
3989 1173336715 : if (res == (void *)-1)
3990 : {
3991 : res = NULL;
3992 : break;
3993 : }
3994 : /* Lookup succeeded. */
3995 1173336620 : else if (res != NULL)
3996 : break;
3997 :
3998 1164585926 : if (valueize)
3999 : {
4000 1145894989 : vuse = valueize (vuse);
4001 1145894989 : if (!vuse)
4002 : {
4003 : res = NULL;
4004 : break;
4005 : }
4006 : }
4007 1147278214 : def_stmt = SSA_NAME_DEF_STMT (vuse);
4008 1147278214 : if (gimple_nop_p (def_stmt))
4009 : break;
4010 1144404861 : else if (gphi *phi = dyn_cast <gphi *> (def_stmt))
4011 63408033 : vuse = get_continuation_for_phi (phi, ref, tbaa_p, limit,
4012 : &visited, translate, data,
4013 : is_backedge);
4014 : else
4015 : {
4016 1080996828 : if ((int)limit <= 0)
4017 : {
4018 : res = NULL;
4019 : break;
4020 : }
4021 1080726744 : --limit;
4022 1080726744 : if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
4023 : {
4024 33031531 : if (!translate)
4025 : break;
4026 27700959 : translate_flags disambiguate_only = TR_TRANSLATE;
4027 27700959 : res = (*translate) (ref, vuse, data, &disambiguate_only);
4028 : /* Failed lookup and translation. */
4029 27700959 : if (res == (void *)-1)
4030 : {
4031 : res = NULL;
4032 : break;
4033 : }
4034 : /* Lookup succeeded. */
4035 6054924 : else if (res != NULL)
4036 : break;
4037 : /* Translation succeeded, continue walking. Regions skipped
4038 : so far were only verified to not clobber the original ref,
4039 : so forget about them. */
4040 4807679 : if (disambiguate_only == TR_TRANSLATE && visited)
4041 522755 : bitmap_clear (visited);
4042 : }
4043 1052502892 : vuse = gimple_vuse (def_stmt);
4044 : }
4045 : }
4046 1115910925 : while (vuse);
4047 :
4048 70557284 : if (visited)
4049 22518984 : BITMAP_FREE (visited);
4050 :
4051 70557284 : timevar_pop (TV_ALIAS_STMT_WALK);
4052 :
4053 70557284 : return res;
4054 : }
4055 :
4056 :
4057 : /* Based on the memory reference REF call WALKER for each vdef whose
4058 : defining statement may clobber REF, starting with VDEF. If REF
4059 : is NULL_TREE, each defining statement is visited.
4060 :
4061 : WALKER is called with REF, the current vdef and DATA. If WALKER
4062 : returns true the walk is stopped, otherwise it continues.
4063 :
4064 : If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
4065 : The pointer may be NULL and then we do not track this information.
4066 :
4067 : At PHI nodes walk_aliased_vdefs forks into one walk for each
4068 : PHI argument (but only one walk continues at merge points), the
4069 : return value is true if any of the walks was successful.
4070 :
4071 : The function returns the number of statements walked or -1 if
4072 : LIMIT stmts were walked and the walk was aborted at this point.
4073 : If LIMIT is zero the walk is not aborted. */
4074 :
4075 : static int
4076 299999746 : walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
4077 : bool (*walker)(ao_ref *, tree, void *), void *data,
4078 : bitmap *visited, unsigned int cnt,
4079 : bool *function_entry_reached, unsigned limit)
4080 : {
4081 1599678306 : do
4082 : {
4083 949839026 : gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
4084 :
4085 949839026 : if (*visited
4086 949839026 : && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
4087 168875003 : return cnt;
4088 :
4089 780964023 : if (gimple_nop_p (def_stmt))
4090 : {
4091 25168901 : if (function_entry_reached)
4092 3977917 : *function_entry_reached = true;
4093 25168901 : return cnt;
4094 : }
4095 755795122 : else if (gimple_code (def_stmt) == GIMPLE_PHI)
4096 : {
4097 90130123 : unsigned i;
4098 90130123 : if (!*visited)
4099 : {
4100 8359954 : *visited = BITMAP_ALLOC (NULL);
4101 8359954 : bitmap_tree_view (*visited);
4102 : }
4103 285167698 : for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
4104 : {
4105 200100846 : int res = walk_aliased_vdefs_1 (ref,
4106 : gimple_phi_arg_def (def_stmt, i),
4107 : walker, data, visited, cnt,
4108 : function_entry_reached, limit);
4109 200100846 : if (res == -1)
4110 : return -1;
4111 195037575 : cnt = res;
4112 : }
4113 85066852 : return cnt;
4114 : }
4115 :
4116 : /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
4117 665664999 : cnt++;
4118 665664999 : if (cnt == limit)
4119 : return -1;
4120 665520744 : if ((!ref
4121 587212288 : || stmt_may_clobber_ref_p_1 (def_stmt, ref))
4122 741813266 : && (*walker) (ref, vdef, data))
4123 15681464 : return cnt;
4124 :
4125 649839280 : vdef = gimple_vuse (def_stmt);
4126 649839280 : }
4127 : while (1);
4128 : }
4129 :
4130 : int
4131 99898900 : walk_aliased_vdefs (ao_ref *ref, tree vdef,
4132 : bool (*walker)(ao_ref *, tree, void *), void *data,
4133 : bitmap *visited,
4134 : bool *function_entry_reached, unsigned int limit)
4135 : {
4136 99898900 : bitmap local_visited = NULL;
4137 99898900 : int ret;
4138 :
4139 99898900 : timevar_push (TV_ALIAS_STMT_WALK);
4140 :
4141 99898900 : if (function_entry_reached)
4142 4993517 : *function_entry_reached = false;
4143 :
4144 127227688 : ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
4145 : visited ? visited : &local_visited, 0,
4146 : function_entry_reached, limit);
4147 99898900 : if (local_visited)
4148 8359954 : BITMAP_FREE (local_visited);
4149 :
4150 99898900 : timevar_pop (TV_ALIAS_STMT_WALK);
4151 :
4152 99898900 : return ret;
4153 : }
4154 :
4155 : /* Verify validity of the fnspec string.
4156 : See attr-fnspec.h for details. */
4157 :
4158 : void
4159 476092455 : attr_fnspec::verify ()
4160 : {
4161 476092455 : bool err = false;
4162 476092455 : if (!len)
4163 : return;
4164 :
4165 : /* Check return value specifier. */
4166 156536959 : if (len < return_desc_size)
4167 : err = true;
4168 156536959 : else if ((len - return_desc_size) % arg_desc_size)
4169 : err = true;
4170 156536959 : else if ((str[0] < '1' || str[0] > '4')
4171 156536959 : && str[0] != '.' && str[0] != 'm')
4172 0 : err = true;
4173 :
4174 156536959 : switch (str[1])
4175 : {
4176 : case ' ':
4177 : case 'p':
4178 : case 'P':
4179 : case 'c':
4180 : case 'C':
4181 : break;
4182 : default:
4183 : err = true;
4184 : }
4185 156536959 : if (err)
4186 0 : internal_error ("invalid fn spec attribute \"%s\"", str);
4187 :
4188 : /* Now check all parameters. */
4189 461166880 : for (unsigned int i = 0; arg_specified_p (i); i++)
4190 : {
4191 304629921 : unsigned int idx = arg_idx (i);
4192 304629921 : switch (str[idx])
4193 : {
4194 284303512 : case 'x':
4195 284303512 : case 'X':
4196 284303512 : case 'r':
4197 284303512 : case 'R':
4198 284303512 : case 'o':
4199 284303512 : case 'O':
4200 284303512 : case 'w':
4201 284303512 : case 'W':
4202 284303512 : case '.':
4203 284303512 : if ((str[idx + 1] >= '1' && str[idx + 1] <= '9')
4204 284303512 : || str[idx + 1] == 't')
4205 : {
4206 45702002 : if (str[idx] != 'r' && str[idx] != 'R'
4207 : && str[idx] != 'w' && str[idx] != 'W'
4208 : && str[idx] != 'o' && str[idx] != 'O')
4209 45702002 : err = true;
4210 45702002 : if (str[idx + 1] != 't'
4211 : /* Size specified is scalar, so it should be described
4212 : by ". " if specified at all. */
4213 45702002 : && (arg_specified_p (str[idx + 1] - '1')
4214 0 : && str[arg_idx (str[idx + 1] - '1')] != '.'))
4215 : err = true;
4216 : }
4217 238601510 : else if (str[idx + 1] != ' ')
4218 : err = true;
4219 : break;
4220 20326409 : default:
4221 20326409 : if (str[idx] < '1' || str[idx] > '9')
4222 : err = true;
4223 : }
4224 304629921 : if (err)
4225 0 : internal_error ("invalid fn spec attribute \"%s\" arg %i", str, i);
4226 : }
4227 : }
4228 :
4229 : /* Return true if TYPE1 and TYPE2 will always give the same answer
4230 : when compared with other types using same_type_for_tbaa. */
4231 :
4232 : static bool
4233 22968997 : types_equal_for_same_type_for_tbaa_p (tree type1, tree type2,
4234 : bool lto_streaming_safe)
4235 : {
4236 : /* We use same_type_for_tbaa_p to match types in the access path.
4237 : This check is overly conservative. */
4238 22968997 : type1 = TYPE_MAIN_VARIANT (type1);
4239 22968997 : type2 = TYPE_MAIN_VARIANT (type2);
4240 :
4241 22968997 : if (TYPE_STRUCTURAL_EQUALITY_P (type1)
4242 22968997 : != TYPE_STRUCTURAL_EQUALITY_P (type2))
4243 : return false;
4244 22690289 : if (TYPE_STRUCTURAL_EQUALITY_P (type1))
4245 : return true;
4246 :
4247 18945441 : if (lto_streaming_safe)
4248 90967 : return type1 == type2;
4249 : else
4250 18854474 : return TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2);
4251 : }
4252 :
4253 : /* Return true if TYPE1 and TYPE2 will always give the same answer
4254 : when compared with other types using same_type_for_tbaa. */
4255 :
4256 : bool
4257 22739744 : types_equal_for_same_type_for_tbaa_p (tree type1, tree type2)
4258 : {
4259 22739744 : return types_equal_for_same_type_for_tbaa_p (type1, type2,
4260 22739744 : lto_streaming_expected_p ());
4261 : }
4262 :
4263 : /* Compare REF1 and REF2 and return flags specifying their differences.
4264 : If LTO_STREAMING_SAFE is true do not use alias sets and canonical
4265 : types that are going to be recomputed.
4266 : If TBAA is true also compare TBAA metadata. */
4267 :
4268 : int
4269 132526 : ao_compare::compare_ao_refs (ao_ref *ref1, ao_ref *ref2,
4270 : bool lto_streaming_safe,
4271 : bool tbaa)
4272 : {
4273 132526 : if (TREE_THIS_VOLATILE (ref1->ref) != TREE_THIS_VOLATILE (ref2->ref))
4274 : return SEMANTICS;
4275 132508 : tree base1 = ao_ref_base (ref1);
4276 132508 : tree base2 = ao_ref_base (ref2);
4277 :
4278 132508 : if (!known_eq (ref1->offset, ref2->offset)
4279 132508 : || !known_eq (ref1->size, ref2->size)
4280 265016 : || !known_eq (ref1->max_size, ref2->max_size))
4281 : return SEMANTICS;
4282 :
4283 : /* For variable accesses we need to compare actual paths
4284 : to check that both refs are accessing same address and the access size. */
4285 132500 : if (!known_eq (ref1->size, ref1->max_size))
4286 : {
4287 3496 : if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (ref1->ref)),
4288 3496 : TYPE_SIZE (TREE_TYPE (ref2->ref)), 0))
4289 : return SEMANTICS;
4290 3496 : tree r1 = ref1->ref;
4291 3496 : tree r2 = ref2->ref;
4292 :
4293 : /* Handle toplevel COMPONENT_REFs of bitfields.
4294 : Those are special since they are not allowed in
4295 : ADDR_EXPR. */
4296 3496 : if (TREE_CODE (r1) == COMPONENT_REF
4297 3496 : && DECL_BIT_FIELD (TREE_OPERAND (r1, 1)))
4298 : {
4299 0 : if (TREE_CODE (r2) != COMPONENT_REF
4300 0 : || !DECL_BIT_FIELD (TREE_OPERAND (r2, 1)))
4301 : return SEMANTICS;
4302 0 : tree field1 = TREE_OPERAND (r1, 1);
4303 0 : tree field2 = TREE_OPERAND (r2, 1);
4304 0 : if (!operand_equal_p (DECL_FIELD_OFFSET (field1),
4305 0 : DECL_FIELD_OFFSET (field2), 0)
4306 0 : || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field1),
4307 0 : DECL_FIELD_BIT_OFFSET (field2), 0)
4308 0 : || !operand_equal_p (DECL_SIZE (field1), DECL_SIZE (field2), 0)
4309 0 : || !types_compatible_p (TREE_TYPE (r1),
4310 0 : TREE_TYPE (r2)))
4311 : return SEMANTICS;
4312 0 : r1 = TREE_OPERAND (r1, 0);
4313 0 : r2 = TREE_OPERAND (r2, 0);
4314 : }
4315 3496 : else if (TREE_CODE (r2) == COMPONENT_REF
4316 3496 : && DECL_BIT_FIELD (TREE_OPERAND (r2, 1)))
4317 : return SEMANTICS;
4318 :
4319 : /* Similarly for bit field refs. */
4320 3496 : if (TREE_CODE (r1) == BIT_FIELD_REF)
4321 : {
4322 0 : if (TREE_CODE (r2) != BIT_FIELD_REF
4323 0 : || !operand_equal_p (TREE_OPERAND (r1, 1),
4324 0 : TREE_OPERAND (r2, 1), 0)
4325 0 : || !operand_equal_p (TREE_OPERAND (r1, 2),
4326 0 : TREE_OPERAND (r2, 2), 0)
4327 0 : || !types_compatible_p (TREE_TYPE (r1),
4328 0 : TREE_TYPE (r2)))
4329 : return SEMANTICS;
4330 0 : r1 = TREE_OPERAND (r1, 0);
4331 0 : r2 = TREE_OPERAND (r2, 0);
4332 : }
4333 3496 : else if (TREE_CODE (r2) == BIT_FIELD_REF)
4334 : return SEMANTICS;
4335 :
4336 : /* Now we can compare the address of actual memory access. */
4337 3496 : if (!operand_equal_p (r1, r2, OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS))
4338 : return SEMANTICS;
4339 : }
4340 : /* For constant accesses we get more matches by comparing offset only. */
4341 129004 : else if (!operand_equal_p (base1, base2,
4342 : OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS))
4343 : return SEMANTICS;
4344 :
4345 : /* We can't simply use get_object_alignment_1 on the full
4346 : reference as for accesses with variable indexes this reports
4347 : too conservative alignment. */
4348 132297 : unsigned int align1, align2;
4349 132297 : unsigned HOST_WIDE_INT bitpos1, bitpos2;
4350 132297 : bool known1 = get_object_alignment_1 (base1, &align1, &bitpos1);
4351 132297 : bool known2 = get_object_alignment_1 (base2, &align2, &bitpos2);
4352 : /* ??? For MEMREF get_object_alignment_1 determines aligned from
4353 : TYPE_ALIGN but still returns false. This seem to contradict
4354 : its description. So compare even if alignment is unknown. */
4355 132297 : if (known1 != known2
4356 132297 : || (bitpos1 != bitpos2 || align1 != align2))
4357 : return SEMANTICS;
4358 :
4359 : /* Now we know that accesses are semantically same. */
4360 132082 : int flags = 0;
4361 :
4362 : /* ao_ref_base strips inner MEM_REF [&decl], recover from that here. */
4363 132082 : tree rbase1 = ref1->ref;
4364 132082 : if (rbase1)
4365 236726 : while (handled_component_p (rbase1))
4366 104644 : rbase1 = TREE_OPERAND (rbase1, 0);
4367 132082 : tree rbase2 = ref2->ref;
4368 236702 : while (handled_component_p (rbase2))
4369 104620 : rbase2 = TREE_OPERAND (rbase2, 0);
4370 :
4371 : /* MEM_REFs and TARGET_MEM_REFs record dependence cliques which are used to
4372 : implement restrict pointers. MR_DEPENDENCE_CLIQUE 0 means no information.
4373 : Otherwise we need to match bases and cliques. */
4374 132082 : if ((((TREE_CODE (rbase1) == MEM_REF || TREE_CODE (rbase1) == TARGET_MEM_REF)
4375 85398 : && MR_DEPENDENCE_CLIQUE (rbase1))
4376 108561 : || ((TREE_CODE (rbase2) == MEM_REF || TREE_CODE (rbase2) == TARGET_MEM_REF)
4377 61877 : && MR_DEPENDENCE_CLIQUE (rbase2)))
4378 155603 : && (TREE_CODE (rbase1) != TREE_CODE (rbase2)
4379 23521 : || MR_DEPENDENCE_CLIQUE (rbase1) != MR_DEPENDENCE_CLIQUE (rbase2)
4380 23441 : || (MR_DEPENDENCE_BASE (rbase1) != MR_DEPENDENCE_BASE (rbase2))))
4381 : flags |= DEPENDENCE_CLIQUE;
4382 :
4383 132082 : if (!tbaa)
4384 : return flags;
4385 :
4386 : /* Alias sets are not stable across LTO sreaming; be conservative here
4387 : and compare types the alias sets are ultimately based on. */
4388 132025 : if (lto_streaming_safe)
4389 : {
4390 2937 : tree t1 = ao_ref_alias_ptr_type (ref1);
4391 2937 : tree t2 = ao_ref_alias_ptr_type (ref2);
4392 2937 : if (!alias_ptr_types_compatible_p (t1, t2))
4393 13 : flags |= REF_ALIAS_SET;
4394 :
4395 2937 : t1 = ao_ref_base_alias_ptr_type (ref1);
4396 2937 : t2 = ao_ref_base_alias_ptr_type (ref2);
4397 2937 : if (!alias_ptr_types_compatible_p (t1, t2))
4398 22 : flags |= BASE_ALIAS_SET;
4399 : }
4400 : else
4401 : {
4402 129088 : if (ao_ref_alias_set (ref1) != ao_ref_alias_set (ref2))
4403 0 : flags |= REF_ALIAS_SET;
4404 129088 : if (ao_ref_base_alias_set (ref1) != ao_ref_base_alias_set (ref2))
4405 0 : flags |= BASE_ALIAS_SET;
4406 : }
4407 :
4408 : /* Access path is used only on non-view-converted references. */
4409 132025 : bool view_converted = view_converted_memref_p (rbase1);
4410 132025 : if (view_converted_memref_p (rbase2) != view_converted)
4411 0 : return flags | ACCESS_PATH;
4412 132025 : else if (view_converted)
4413 : return flags;
4414 :
4415 :
4416 : /* Find start of access paths and look for trailing arrays. */
4417 127024 : tree c1 = ref1->ref, c2 = ref2->ref;
4418 127024 : tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
4419 127024 : int nskipped1 = 0, nskipped2 = 0;
4420 127024 : int i = 0;
4421 :
4422 231542 : for (tree p1 = ref1->ref; handled_component_p (p1); p1 = TREE_OPERAND (p1, 0))
4423 : {
4424 104518 : if (component_ref_to_zero_sized_trailing_array_p (p1))
4425 133 : end_struct_ref1 = p1;
4426 104518 : if (ends_tbaa_access_path_p (p1))
4427 5190 : c1 = p1, nskipped1 = i;
4428 104518 : i++;
4429 : }
4430 127024 : i = 0;
4431 231518 : for (tree p2 = ref2->ref; handled_component_p (p2); p2 = TREE_OPERAND (p2, 0))
4432 : {
4433 104494 : if (component_ref_to_zero_sized_trailing_array_p (p2))
4434 140 : end_struct_ref2 = p2;
4435 104494 : if (ends_tbaa_access_path_p (p2))
4436 5190 : c2 = p2, nskipped2 = i;
4437 104494 : i++;
4438 : }
4439 :
4440 : /* For variable accesses we can not rely on offset match below.
4441 : We know that paths are struturally same, so only check that
4442 : starts of TBAA paths did not diverge. */
4443 127024 : if (!known_eq (ref1->size, ref1->max_size)
4444 127024 : && nskipped1 != nskipped2)
4445 0 : return flags | ACCESS_PATH;
4446 :
4447 : /* Information about trailing refs is used by
4448 : aliasing_component_refs_p that is applied only if paths
4449 : has handled components.. */
4450 127024 : if (!handled_component_p (c1) && !handled_component_p (c2))
4451 : ;
4452 62247 : else if ((end_struct_ref1 != NULL) != (end_struct_ref2 != NULL))
4453 27 : return flags | ACCESS_PATH;
4454 126997 : if (end_struct_ref1
4455 127120 : && same_type_for_tbaa (TREE_TYPE (end_struct_ref1),
4456 123 : TREE_TYPE (end_struct_ref2)) != 1)
4457 3 : return flags | ACCESS_PATH;
4458 :
4459 : /* Now compare all handled components of the access path.
4460 : We have three oracles that cares about access paths:
4461 : - aliasing_component_refs_p
4462 : - nonoverlapping_refs_since_match_p
4463 : - nonoverlapping_component_refs_p
4464 : We need to match things these oracles compare.
4465 :
4466 : It is only necessary to check types for compatibility
4467 : and offsets. Rest of what oracles compares are actual
4468 : addresses. Those are already known to be same:
4469 : - for constant accesses we check offsets
4470 : - for variable accesses we already matched
4471 : the path lexically with operand_equal_p. */
4472 331536 : while (true)
4473 : {
4474 229265 : bool comp1 = handled_component_p (c1);
4475 229265 : bool comp2 = handled_component_p (c2);
4476 :
4477 229265 : if (comp1 != comp2)
4478 12 : return flags | ACCESS_PATH;
4479 229253 : if (!comp1)
4480 : break;
4481 :
4482 102383 : if (TREE_CODE (c1) != TREE_CODE (c2))
4483 0 : return flags | ACCESS_PATH;
4484 :
4485 : /* aliasing_component_refs_p attempts to find type match within
4486 : the paths. For that reason both types needs to be equal
4487 : with respect to same_type_for_tbaa_p. */
4488 102383 : if (!types_equal_for_same_type_for_tbaa_p (TREE_TYPE (c1),
4489 102383 : TREE_TYPE (c2),
4490 : lto_streaming_safe))
4491 112 : return flags | ACCESS_PATH;
4492 204542 : if (component_ref_to_zero_sized_trailing_array_p (c1)
4493 102271 : != component_ref_to_zero_sized_trailing_array_p (c2))
4494 0 : return flags | ACCESS_PATH;
4495 :
4496 : /* aliasing_matching_component_refs_p compares
4497 : offsets within the path. Other properties are ignored.
4498 : Do not bother to verify offsets in variable accesses. Here we
4499 : already compared them by operand_equal_p so they are
4500 : structurally same. */
4501 102271 : if (!known_eq (ref1->size, ref1->max_size))
4502 : {
4503 4216 : poly_int64 offadj1, sztmc1, msztmc1;
4504 4216 : bool reverse1;
4505 4216 : get_ref_base_and_extent (c1, &offadj1, &sztmc1, &msztmc1, &reverse1);
4506 4216 : poly_int64 offadj2, sztmc2, msztmc2;
4507 4216 : bool reverse2;
4508 4216 : get_ref_base_and_extent (c2, &offadj2, &sztmc2, &msztmc2, &reverse2);
4509 4216 : if (!known_eq (offadj1, offadj2))
4510 0 : return flags | ACCESS_PATH;
4511 : }
4512 102271 : c1 = TREE_OPERAND (c1, 0);
4513 102271 : c2 = TREE_OPERAND (c2, 0);
4514 102271 : }
4515 : /* Finally test the access type. */
4516 126870 : if (!types_equal_for_same_type_for_tbaa_p (TREE_TYPE (c1),
4517 126870 : TREE_TYPE (c2),
4518 : lto_streaming_safe))
4519 1796 : return flags | ACCESS_PATH;
4520 : return flags;
4521 : }
4522 :
4523 : /* Hash REF to HSTATE. If LTO_STREAMING_SAFE do not use alias sets
4524 : and canonical types. */
4525 : void
4526 8585143 : ao_compare::hash_ao_ref (ao_ref *ref, bool lto_streaming_safe, bool tbaa,
4527 : inchash::hash &hstate)
4528 : {
4529 8585143 : tree base = ao_ref_base (ref);
4530 8585143 : tree tbase = base;
4531 :
4532 8585143 : if (!known_eq (ref->size, ref->max_size))
4533 : {
4534 434545 : tree r = ref->ref;
4535 434545 : if (TREE_CODE (r) == COMPONENT_REF
4536 434545 : && DECL_BIT_FIELD (TREE_OPERAND (r, 1)))
4537 : {
4538 1721 : tree field = TREE_OPERAND (r, 1);
4539 1721 : hash_operand (DECL_FIELD_OFFSET (field), hstate, 0);
4540 1721 : hash_operand (DECL_FIELD_BIT_OFFSET (field), hstate, 0);
4541 1721 : hash_operand (DECL_SIZE (field), hstate, 0);
4542 1721 : r = TREE_OPERAND (r, 0);
4543 : }
4544 434545 : if (TREE_CODE (r) == BIT_FIELD_REF)
4545 : {
4546 1704 : hash_operand (TREE_OPERAND (r, 1), hstate, 0);
4547 1704 : hash_operand (TREE_OPERAND (r, 2), hstate, 0);
4548 1704 : r = TREE_OPERAND (r, 0);
4549 : }
4550 434545 : hash_operand (TYPE_SIZE (TREE_TYPE (ref->ref)), hstate, 0);
4551 434545 : hash_operand (r, hstate, OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS);
4552 : }
4553 : else
4554 : {
4555 8150598 : hash_operand (tbase, hstate, OEP_ADDRESS_OF | OEP_MATCH_SIDE_EFFECTS);
4556 8150598 : hstate.add_poly_int (ref->offset);
4557 8150598 : hstate.add_poly_int (ref->size);
4558 8150598 : hstate.add_poly_int (ref->max_size);
4559 : }
4560 8585143 : if (!lto_streaming_safe && tbaa)
4561 : {
4562 8239297 : hstate.add_int (ao_ref_alias_set (ref));
4563 8239297 : hstate.add_int (ao_ref_base_alias_set (ref));
4564 : }
4565 8585143 : }
|