[Ada] Wrong accessibility level under -gnat12
[official-gcc.git] / gcc / tree-ssa-alias.c
blob5c5cbe41fec05d0b157e2af2e36a1a4e06d48616
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2019 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
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.
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.
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/>. */
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"
42 /* Broad overview of how alias analysis on gimple works:
44 Statements clobbering or using memory are linked through the
45 virtual operand factored use-def chain. The virtual operand
46 is unique per function, its symbol is accessible via gimple_vop (cfun).
47 Virtual operands are used for efficiently walking memory statements
48 in the gimple IL and are useful for things like value-numbering as
49 a generation count for memory references.
51 SSA_NAME pointers may have associated points-to information
52 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
53 points-to information is (re-)computed by the TODO_rebuild_alias
54 pass manager todo. Points-to information is also used for more
55 precise tracking of call-clobbered and call-used variables and
56 related disambiguations.
58 This file contains functions for disambiguating memory references,
59 the so called alias-oracle and tools for walking of the gimple IL.
61 The main alias-oracle entry-points are
63 bool stmt_may_clobber_ref_p (gimple *, tree)
65 This function queries if a statement may invalidate (parts of)
66 the memory designated by the reference tree argument.
68 bool ref_maybe_used_by_stmt_p (gimple *, tree)
70 This function queries if a statement may need (parts of) the
71 memory designated by the reference tree argument.
73 There are variants of these functions that only handle the call
74 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
75 Note that these do not disambiguate against a possible call lhs.
77 bool refs_may_alias_p (tree, tree)
79 This function tries to disambiguate two reference trees.
81 bool ptr_deref_may_alias_global_p (tree)
83 This function queries if dereferencing a pointer variable may
84 alias global memory.
86 More low-level disambiguators are available and documented in
87 this file. Low-level disambiguators dealing with points-to
88 information are in tree-ssa-structalias.c. */
90 static int nonoverlapping_component_refs_since_match_p (tree, tree, tree, tree);
91 static bool nonoverlapping_component_refs_p (const_tree, const_tree);
93 /* Query statistics for the different low-level disambiguators.
94 A high-level query may trigger multiple of them. */
96 static struct {
97 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
98 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
99 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
100 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
101 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
102 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
103 unsigned HOST_WIDE_INT aliasing_component_refs_p_may_alias;
104 unsigned HOST_WIDE_INT aliasing_component_refs_p_no_alias;
105 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_may_alias;
106 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_no_alias;
107 unsigned HOST_WIDE_INT nonoverlapping_component_refs_since_match_p_may_alias;
108 unsigned HOST_WIDE_INT nonoverlapping_component_refs_since_match_p_no_alias;
109 } alias_stats;
111 void
112 dump_alias_stats (FILE *s)
114 fprintf (s, "\nAlias oracle query stats:\n");
115 fprintf (s, " refs_may_alias_p: "
116 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
117 HOST_WIDE_INT_PRINT_DEC" queries\n",
118 alias_stats.refs_may_alias_p_no_alias,
119 alias_stats.refs_may_alias_p_no_alias
120 + alias_stats.refs_may_alias_p_may_alias);
121 fprintf (s, " ref_maybe_used_by_call_p: "
122 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
123 HOST_WIDE_INT_PRINT_DEC" queries\n",
124 alias_stats.ref_maybe_used_by_call_p_no_alias,
125 alias_stats.refs_may_alias_p_no_alias
126 + alias_stats.ref_maybe_used_by_call_p_may_alias);
127 fprintf (s, " call_may_clobber_ref_p: "
128 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
129 HOST_WIDE_INT_PRINT_DEC" queries\n",
130 alias_stats.call_may_clobber_ref_p_no_alias,
131 alias_stats.call_may_clobber_ref_p_no_alias
132 + alias_stats.call_may_clobber_ref_p_may_alias);
133 fprintf (s, " nonoverlapping_component_refs_p: "
134 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
135 HOST_WIDE_INT_PRINT_DEC" queries\n",
136 alias_stats.nonoverlapping_component_refs_p_no_alias,
137 alias_stats.nonoverlapping_component_refs_p_no_alias
138 + alias_stats.nonoverlapping_component_refs_p_may_alias);
139 fprintf (s, " nonoverlapping_component_refs_since_match_p: "
140 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
141 HOST_WIDE_INT_PRINT_DEC" queries\n",
142 alias_stats.nonoverlapping_component_refs_since_match_p_no_alias,
143 alias_stats.nonoverlapping_component_refs_since_match_p_no_alias
144 + alias_stats.nonoverlapping_component_refs_since_match_p_may_alias);
145 fprintf (s, " aliasing_component_refs_p: "
146 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
147 HOST_WIDE_INT_PRINT_DEC" queries\n",
148 alias_stats.aliasing_component_refs_p_no_alias,
149 alias_stats.aliasing_component_refs_p_no_alias
150 + alias_stats.aliasing_component_refs_p_may_alias);
151 dump_alias_stats_in_alias_c (s);
155 /* Return true, if dereferencing PTR may alias with a global variable. */
157 bool
158 ptr_deref_may_alias_global_p (tree ptr)
160 struct ptr_info_def *pi;
162 /* If we end up with a pointer constant here that may point
163 to global memory. */
164 if (TREE_CODE (ptr) != SSA_NAME)
165 return true;
167 pi = SSA_NAME_PTR_INFO (ptr);
169 /* If we do not have points-to information for this variable,
170 we have to punt. */
171 if (!pi)
172 return true;
174 /* ??? This does not use TBAA to prune globals ptr may not access. */
175 return pt_solution_includes_global (&pi->pt);
178 /* Return true if dereferencing PTR may alias DECL.
179 The caller is responsible for applying TBAA to see if PTR
180 may access DECL at all. */
182 static bool
183 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
185 struct ptr_info_def *pi;
187 /* Conversions are irrelevant for points-to information and
188 data-dependence analysis can feed us those. */
189 STRIP_NOPS (ptr);
191 /* Anything we do not explicilty handle aliases. */
192 if ((TREE_CODE (ptr) != SSA_NAME
193 && TREE_CODE (ptr) != ADDR_EXPR
194 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
195 || !POINTER_TYPE_P (TREE_TYPE (ptr))
196 || (!VAR_P (decl)
197 && TREE_CODE (decl) != PARM_DECL
198 && TREE_CODE (decl) != RESULT_DECL))
199 return true;
201 /* Disregard pointer offsetting. */
202 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
206 ptr = TREE_OPERAND (ptr, 0);
208 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
209 return ptr_deref_may_alias_decl_p (ptr, decl);
212 /* ADDR_EXPR pointers either just offset another pointer or directly
213 specify the pointed-to set. */
214 if (TREE_CODE (ptr) == ADDR_EXPR)
216 tree base = get_base_address (TREE_OPERAND (ptr, 0));
217 if (base
218 && (TREE_CODE (base) == MEM_REF
219 || TREE_CODE (base) == TARGET_MEM_REF))
220 ptr = TREE_OPERAND (base, 0);
221 else if (base
222 && DECL_P (base))
223 return compare_base_decls (base, decl) != 0;
224 else if (base
225 && CONSTANT_CLASS_P (base))
226 return false;
227 else
228 return true;
231 /* Non-aliased variables cannot be pointed to. */
232 if (!may_be_aliased (decl))
233 return false;
235 /* If we do not have useful points-to information for this pointer
236 we cannot disambiguate anything else. */
237 pi = SSA_NAME_PTR_INFO (ptr);
238 if (!pi)
239 return true;
241 return pt_solution_includes (&pi->pt, decl);
244 /* Return true if dereferenced PTR1 and PTR2 may alias.
245 The caller is responsible for applying TBAA to see if accesses
246 through PTR1 and PTR2 may conflict at all. */
248 bool
249 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
251 struct ptr_info_def *pi1, *pi2;
253 /* Conversions are irrelevant for points-to information and
254 data-dependence analysis can feed us those. */
255 STRIP_NOPS (ptr1);
256 STRIP_NOPS (ptr2);
258 /* Disregard pointer offsetting. */
259 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
263 ptr1 = TREE_OPERAND (ptr1, 0);
265 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
266 return ptr_derefs_may_alias_p (ptr1, ptr2);
268 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
272 ptr2 = TREE_OPERAND (ptr2, 0);
274 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
275 return ptr_derefs_may_alias_p (ptr1, ptr2);
278 /* ADDR_EXPR pointers either just offset another pointer or directly
279 specify the pointed-to set. */
280 if (TREE_CODE (ptr1) == ADDR_EXPR)
282 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
283 if (base
284 && (TREE_CODE (base) == MEM_REF
285 || TREE_CODE (base) == TARGET_MEM_REF))
286 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
287 else if (base
288 && DECL_P (base))
289 return ptr_deref_may_alias_decl_p (ptr2, base);
290 else
291 return true;
293 if (TREE_CODE (ptr2) == ADDR_EXPR)
295 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
296 if (base
297 && (TREE_CODE (base) == MEM_REF
298 || TREE_CODE (base) == TARGET_MEM_REF))
299 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
300 else if (base
301 && DECL_P (base))
302 return ptr_deref_may_alias_decl_p (ptr1, base);
303 else
304 return true;
307 /* From here we require SSA name pointers. Anything else aliases. */
308 if (TREE_CODE (ptr1) != SSA_NAME
309 || TREE_CODE (ptr2) != SSA_NAME
310 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
311 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
312 return true;
314 /* We may end up with two empty points-to solutions for two same pointers.
315 In this case we still want to say both pointers alias, so shortcut
316 that here. */
317 if (ptr1 == ptr2)
318 return true;
320 /* If we do not have useful points-to information for either pointer
321 we cannot disambiguate anything else. */
322 pi1 = SSA_NAME_PTR_INFO (ptr1);
323 pi2 = SSA_NAME_PTR_INFO (ptr2);
324 if (!pi1 || !pi2)
325 return true;
327 /* ??? This does not use TBAA to prune decls from the intersection
328 that not both pointers may access. */
329 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
332 /* Return true if dereferencing PTR may alias *REF.
333 The caller is responsible for applying TBAA to see if PTR
334 may access *REF at all. */
336 static bool
337 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
339 tree base = ao_ref_base (ref);
341 if (TREE_CODE (base) == MEM_REF
342 || TREE_CODE (base) == TARGET_MEM_REF)
343 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
344 else if (DECL_P (base))
345 return ptr_deref_may_alias_decl_p (ptr, base);
347 return true;
350 /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
352 bool
353 ptrs_compare_unequal (tree ptr1, tree ptr2)
355 /* First resolve the pointers down to a SSA name pointer base or
356 a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
357 not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
358 or STRING_CSTs which needs points-to adjustments to track them
359 in the points-to sets. */
360 tree obj1 = NULL_TREE;
361 tree obj2 = NULL_TREE;
362 if (TREE_CODE (ptr1) == ADDR_EXPR)
364 tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
365 if (! tem)
366 return false;
367 if (VAR_P (tem)
368 || TREE_CODE (tem) == PARM_DECL
369 || TREE_CODE (tem) == RESULT_DECL)
370 obj1 = tem;
371 else if (TREE_CODE (tem) == MEM_REF)
372 ptr1 = TREE_OPERAND (tem, 0);
374 if (TREE_CODE (ptr2) == ADDR_EXPR)
376 tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
377 if (! tem)
378 return false;
379 if (VAR_P (tem)
380 || TREE_CODE (tem) == PARM_DECL
381 || TREE_CODE (tem) == RESULT_DECL)
382 obj2 = tem;
383 else if (TREE_CODE (tem) == MEM_REF)
384 ptr2 = TREE_OPERAND (tem, 0);
387 /* Canonicalize ptr vs. object. */
388 if (TREE_CODE (ptr1) == SSA_NAME && obj2)
390 std::swap (ptr1, ptr2);
391 std::swap (obj1, obj2);
394 if (obj1 && obj2)
395 /* Other code handles this correctly, no need to duplicate it here. */;
396 else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
398 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
399 /* We may not use restrict to optimize pointer comparisons.
400 See PR71062. So we have to assume that restrict-pointed-to
401 may be in fact obj1. */
402 if (!pi
403 || pi->pt.vars_contains_restrict
404 || pi->pt.vars_contains_interposable)
405 return false;
406 if (VAR_P (obj1)
407 && (TREE_STATIC (obj1) || DECL_EXTERNAL (obj1)))
409 varpool_node *node = varpool_node::get (obj1);
410 /* If obj1 may bind to NULL give up (see below). */
411 if (! node
412 || ! node->nonzero_address ()
413 || ! decl_binds_to_current_def_p (obj1))
414 return false;
416 return !pt_solution_includes (&pi->pt, obj1);
419 /* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
420 but those require pt.null to be conservatively correct. */
422 return false;
425 /* Returns whether reference REF to BASE may refer to global memory. */
427 static bool
428 ref_may_alias_global_p_1 (tree base)
430 if (DECL_P (base))
431 return is_global_var (base);
432 else if (TREE_CODE (base) == MEM_REF
433 || TREE_CODE (base) == TARGET_MEM_REF)
434 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
435 return true;
438 bool
439 ref_may_alias_global_p (ao_ref *ref)
441 tree base = ao_ref_base (ref);
442 return ref_may_alias_global_p_1 (base);
445 bool
446 ref_may_alias_global_p (tree ref)
448 tree base = get_base_address (ref);
449 return ref_may_alias_global_p_1 (base);
452 /* Return true whether STMT may clobber global memory. */
454 bool
455 stmt_may_clobber_global_p (gimple *stmt)
457 tree lhs;
459 if (!gimple_vdef (stmt))
460 return false;
462 /* ??? We can ask the oracle whether an artificial pointer
463 dereference with a pointer with points-to information covering
464 all global memory (what about non-address taken memory?) maybe
465 clobbered by this call. As there is at the moment no convenient
466 way of doing that without generating garbage do some manual
467 checking instead.
468 ??? We could make a NULL ao_ref argument to the various
469 predicates special, meaning any global memory. */
471 switch (gimple_code (stmt))
473 case GIMPLE_ASSIGN:
474 lhs = gimple_assign_lhs (stmt);
475 return (TREE_CODE (lhs) != SSA_NAME
476 && ref_may_alias_global_p (lhs));
477 case GIMPLE_CALL:
478 return true;
479 default:
480 return true;
485 /* Dump alias information on FILE. */
487 void
488 dump_alias_info (FILE *file)
490 unsigned i;
491 tree ptr;
492 const char *funcname
493 = lang_hooks.decl_printable_name (current_function_decl, 2);
494 tree var;
496 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
498 fprintf (file, "Aliased symbols\n\n");
500 FOR_EACH_LOCAL_DECL (cfun, i, var)
502 if (may_be_aliased (var))
503 dump_variable (file, var);
506 fprintf (file, "\nCall clobber information\n");
508 fprintf (file, "\nESCAPED");
509 dump_points_to_solution (file, &cfun->gimple_df->escaped);
511 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
513 FOR_EACH_SSA_NAME (i, ptr, cfun)
515 struct ptr_info_def *pi;
517 if (!POINTER_TYPE_P (TREE_TYPE (ptr))
518 || SSA_NAME_IN_FREE_LIST (ptr))
519 continue;
521 pi = SSA_NAME_PTR_INFO (ptr);
522 if (pi)
523 dump_points_to_info_for (file, ptr);
526 fprintf (file, "\n");
530 /* Dump alias information on stderr. */
532 DEBUG_FUNCTION void
533 debug_alias_info (void)
535 dump_alias_info (stderr);
539 /* Dump the points-to set *PT into FILE. */
541 void
542 dump_points_to_solution (FILE *file, struct pt_solution *pt)
544 if (pt->anything)
545 fprintf (file, ", points-to anything");
547 if (pt->nonlocal)
548 fprintf (file, ", points-to non-local");
550 if (pt->escaped)
551 fprintf (file, ", points-to escaped");
553 if (pt->ipa_escaped)
554 fprintf (file, ", points-to unit escaped");
556 if (pt->null)
557 fprintf (file, ", points-to NULL");
559 if (pt->vars)
561 fprintf (file, ", points-to vars: ");
562 dump_decl_set (file, pt->vars);
563 if (pt->vars_contains_nonlocal
564 || pt->vars_contains_escaped
565 || pt->vars_contains_escaped_heap
566 || pt->vars_contains_restrict)
568 const char *comma = "";
569 fprintf (file, " (");
570 if (pt->vars_contains_nonlocal)
572 fprintf (file, "nonlocal");
573 comma = ", ";
575 if (pt->vars_contains_escaped)
577 fprintf (file, "%sescaped", comma);
578 comma = ", ";
580 if (pt->vars_contains_escaped_heap)
582 fprintf (file, "%sescaped heap", comma);
583 comma = ", ";
585 if (pt->vars_contains_restrict)
587 fprintf (file, "%srestrict", comma);
588 comma = ", ";
590 if (pt->vars_contains_interposable)
591 fprintf (file, "%sinterposable", comma);
592 fprintf (file, ")");
598 /* Unified dump function for pt_solution. */
600 DEBUG_FUNCTION void
601 debug (pt_solution &ref)
603 dump_points_to_solution (stderr, &ref);
606 DEBUG_FUNCTION void
607 debug (pt_solution *ptr)
609 if (ptr)
610 debug (*ptr);
611 else
612 fprintf (stderr, "<nil>\n");
616 /* Dump points-to information for SSA_NAME PTR into FILE. */
618 void
619 dump_points_to_info_for (FILE *file, tree ptr)
621 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
623 print_generic_expr (file, ptr, dump_flags);
625 if (pi)
626 dump_points_to_solution (file, &pi->pt);
627 else
628 fprintf (file, ", points-to anything");
630 fprintf (file, "\n");
634 /* Dump points-to information for VAR into stderr. */
636 DEBUG_FUNCTION void
637 debug_points_to_info_for (tree var)
639 dump_points_to_info_for (stderr, var);
643 /* Initializes the alias-oracle reference representation *R from REF. */
645 void
646 ao_ref_init (ao_ref *r, tree ref)
648 r->ref = ref;
649 r->base = NULL_TREE;
650 r->offset = 0;
651 r->size = -1;
652 r->max_size = -1;
653 r->ref_alias_set = -1;
654 r->base_alias_set = -1;
655 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
658 /* Returns the base object of the memory reference *REF. */
660 tree
661 ao_ref_base (ao_ref *ref)
663 bool reverse;
665 if (ref->base)
666 return ref->base;
667 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
668 &ref->max_size, &reverse);
669 return ref->base;
672 /* Returns the base object alias set of the memory reference *REF. */
674 alias_set_type
675 ao_ref_base_alias_set (ao_ref *ref)
677 tree base_ref;
678 if (ref->base_alias_set != -1)
679 return ref->base_alias_set;
680 if (!ref->ref)
681 return 0;
682 base_ref = ref->ref;
683 while (handled_component_p (base_ref))
684 base_ref = TREE_OPERAND (base_ref, 0);
685 ref->base_alias_set = get_alias_set (base_ref);
686 return ref->base_alias_set;
689 /* Returns the reference alias set of the memory reference *REF. */
691 alias_set_type
692 ao_ref_alias_set (ao_ref *ref)
694 if (ref->ref_alias_set != -1)
695 return ref->ref_alias_set;
696 ref->ref_alias_set = get_alias_set (ref->ref);
697 return ref->ref_alias_set;
700 /* Init an alias-oracle reference representation from a gimple pointer
701 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
702 size is assumed to be unknown. The access is assumed to be only
703 to or after of the pointer target, not before it. */
705 void
706 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
708 poly_int64 t, size_hwi, extra_offset = 0;
709 ref->ref = NULL_TREE;
710 if (TREE_CODE (ptr) == SSA_NAME)
712 gimple *stmt = SSA_NAME_DEF_STMT (ptr);
713 if (gimple_assign_single_p (stmt)
714 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
715 ptr = gimple_assign_rhs1 (stmt);
716 else if (is_gimple_assign (stmt)
717 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
718 && ptrdiff_tree_p (gimple_assign_rhs2 (stmt), &extra_offset))
720 ptr = gimple_assign_rhs1 (stmt);
721 extra_offset *= BITS_PER_UNIT;
725 if (TREE_CODE (ptr) == ADDR_EXPR)
727 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
728 if (ref->base)
729 ref->offset = BITS_PER_UNIT * t;
730 else
732 size = NULL_TREE;
733 ref->offset = 0;
734 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
737 else
739 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
740 ref->base = build2 (MEM_REF, char_type_node,
741 ptr, null_pointer_node);
742 ref->offset = 0;
744 ref->offset += extra_offset;
745 if (size
746 && poly_int_tree_p (size, &size_hwi)
747 && coeffs_in_range_p (size_hwi, 0, HOST_WIDE_INT_MAX / BITS_PER_UNIT))
748 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
749 else
750 ref->max_size = ref->size = -1;
751 ref->ref_alias_set = 0;
752 ref->base_alias_set = 0;
753 ref->volatile_p = false;
756 /* S1 and S2 are TYPE_SIZE or DECL_SIZE. Compare them:
757 Return -1 if S1 < S2
758 Return 1 if S1 > S2
759 Return 0 if equal or incomparable. */
761 static int
762 compare_sizes (tree s1, tree s2)
764 if (!s1 || !s2)
765 return 0;
767 poly_uint64 size1;
768 poly_uint64 size2;
770 if (!poly_int_tree_p (s1, &size1) || !poly_int_tree_p (s2, &size2))
771 return 0;
772 if (known_lt (size1, size2))
773 return -1;
774 if (known_lt (size2, size1))
775 return 1;
776 return 0;
779 /* Compare TYPE1 and TYPE2 by its size.
780 Return -1 if size of TYPE1 < size of TYPE2
781 Return 1 if size of TYPE1 > size of TYPE2
782 Return 0 if types are of equal sizes or we can not compare them. */
784 static int
785 compare_type_sizes (tree type1, tree type2)
787 /* Be conservative for arrays and vectors. We want to support partial
788 overlap on int[3] and int[3] as tested in gcc.dg/torture/alias-2.c. */
789 while (TREE_CODE (type1) == ARRAY_TYPE
790 || TREE_CODE (type1) == VECTOR_TYPE)
791 type1 = TREE_TYPE (type1);
792 while (TREE_CODE (type2) == ARRAY_TYPE
793 || TREE_CODE (type2) == VECTOR_TYPE)
794 type2 = TREE_TYPE (type2);
795 return compare_sizes (TYPE_SIZE (type1), TYPE_SIZE (type2));
798 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
799 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
800 decide. */
802 static inline int
803 same_type_for_tbaa (tree type1, tree type2)
805 type1 = TYPE_MAIN_VARIANT (type1);
806 type2 = TYPE_MAIN_VARIANT (type2);
808 /* Handle the most common case first. */
809 if (type1 == type2)
810 return 1;
812 /* If we would have to do structural comparison bail out. */
813 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
814 || TYPE_STRUCTURAL_EQUALITY_P (type2))
815 return -1;
817 /* Compare the canonical types. */
818 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
819 return 1;
821 /* ??? Array types are not properly unified in all cases as we have
822 spurious changes in the index types for example. Removing this
823 causes all sorts of problems with the Fortran frontend. */
824 if (TREE_CODE (type1) == ARRAY_TYPE
825 && TREE_CODE (type2) == ARRAY_TYPE)
826 return -1;
828 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
829 object of one of its constrained subtypes, e.g. when a function with an
830 unconstrained parameter passed by reference is called on an object and
831 inlined. But, even in the case of a fixed size, type and subtypes are
832 not equivalent enough as to share the same TYPE_CANONICAL, since this
833 would mean that conversions between them are useless, whereas they are
834 not (e.g. type and subtypes can have different modes). So, in the end,
835 they are only guaranteed to have the same alias set. */
836 if (get_alias_set (type1) == get_alias_set (type2))
837 return -1;
839 /* The types are known to be not equal. */
840 return 0;
843 /* Return true if TYPE is a composite type (i.e. we may apply one of handled
844 components on it). */
846 static bool
847 type_has_components_p (tree type)
849 return AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)
850 || TREE_CODE (type) == COMPLEX_TYPE;
853 /* MATCH1 and MATCH2 which are part of access path of REF1 and REF2
854 respectively are either pointing to same address or are completely
855 disjoint.
857 Try to disambiguate using the access path starting from the match
858 and return false if there is no conflict.
860 Helper for aliasing_component_refs_p. */
862 static bool
863 aliasing_matching_component_refs_p (tree match1, tree ref1,
864 poly_int64 offset1, poly_int64 max_size1,
865 tree match2, tree ref2,
866 poly_int64 offset2, poly_int64 max_size2)
868 poly_int64 offadj, sztmp, msztmp;
869 bool reverse;
872 get_ref_base_and_extent (match2, &offadj, &sztmp, &msztmp, &reverse);
873 offset2 -= offadj;
874 get_ref_base_and_extent (match1, &offadj, &sztmp, &msztmp, &reverse);
875 offset1 -= offadj;
876 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
878 ++alias_stats.aliasing_component_refs_p_no_alias;
879 return false;
882 int cmp = nonoverlapping_component_refs_since_match_p (match1, ref1,
883 match2, ref2);
884 if (cmp == 1
885 || (cmp == -1 && nonoverlapping_component_refs_p (ref1, ref2)))
887 ++alias_stats.aliasing_component_refs_p_no_alias;
888 return false;
890 ++alias_stats.aliasing_component_refs_p_may_alias;
891 return true;
894 /* Determine if the two component references REF1 and REF2 which are
895 based on access types TYPE1 and TYPE2 and of which at least one is based
896 on an indirect reference may alias.
897 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
898 are the respective alias sets. */
900 static bool
901 aliasing_component_refs_p (tree ref1,
902 alias_set_type ref1_alias_set,
903 alias_set_type base1_alias_set,
904 poly_int64 offset1, poly_int64 max_size1,
905 tree ref2,
906 alias_set_type ref2_alias_set,
907 alias_set_type base2_alias_set,
908 poly_int64 offset2, poly_int64 max_size2)
910 /* If one reference is a component references through pointers try to find a
911 common base and apply offset based disambiguation. This handles
912 for example
913 struct A { int i; int j; } *q;
914 struct B { struct A a; int k; } *p;
915 disambiguating q->i and p->a.j. */
916 tree base1, base2;
917 tree type1, type2;
918 int same_p1 = 0, same_p2 = 0;
919 bool maybe_match = false;
920 tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
922 /* Choose bases and base types to search for. */
923 base1 = ref1;
924 while (handled_component_p (base1))
926 /* Generally access paths are monotous in the size of object. The
927 exception are trailing arrays of structures. I.e.
928 struct a {int array[0];};
930 struct a {int array1[0]; int array[];};
931 Such struct has size 0 but accesses to a.array may have non-zero size.
932 In this case the size of TREE_TYPE (base1) is smaller than
933 size of TREE_TYPE (TREE_OPERNAD (base1, 0)).
935 Because we compare sizes of arrays just by sizes of their elements,
936 we only need to care about zero sized array fields here. */
937 if (TREE_CODE (base1) == COMPONENT_REF
938 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base1, 1))) == ARRAY_TYPE
939 && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (base1, 1)))
940 || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (base1, 1)))))
941 && array_at_struct_end_p (base1))
943 gcc_checking_assert (!end_struct_ref1);
944 end_struct_ref1 = base1;
946 if (TREE_CODE (base1) == VIEW_CONVERT_EXPR
947 || TREE_CODE (base1) == BIT_FIELD_REF)
948 ref1 = TREE_OPERAND (base1, 0);
949 base1 = TREE_OPERAND (base1, 0);
951 type1 = TREE_TYPE (base1);
952 base2 = ref2;
953 while (handled_component_p (base2))
955 if (TREE_CODE (base2) == COMPONENT_REF
956 && TREE_CODE (TREE_TYPE (TREE_OPERAND (base2, 1))) == ARRAY_TYPE
957 && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (base2, 1)))
958 || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (base2, 1)))))
959 && array_at_struct_end_p (base2))
961 gcc_checking_assert (!end_struct_ref2);
962 end_struct_ref2 = base2;
964 if (TREE_CODE (base2) == VIEW_CONVERT_EXPR
965 || TREE_CODE (base2) == BIT_FIELD_REF)
966 ref2 = TREE_OPERAND (base2, 0);
967 base2 = TREE_OPERAND (base2, 0);
969 type2 = TREE_TYPE (base2);
971 /* Now search for the type1 in the access path of ref2. This
972 would be a common base for doing offset based disambiguation on.
973 This however only makes sense if type2 is big enough to hold type1. */
974 int cmp_outer = compare_type_sizes (type2, type1);
976 /* If type2 is big enough to contain type1 walk its access path.
977 We also need to care of arrays at the end of structs that may extend
978 beyond the end of structure. */
979 if (cmp_outer >= 0
980 || (end_struct_ref2
981 && compare_type_sizes (TREE_TYPE (end_struct_ref2), type1) >= 0))
983 tree ref = ref2;
984 while (true)
986 /* We walk from inner type to the outer types. If type we see is
987 already too large to be part of type1, terminate the search. */
988 int cmp = compare_type_sizes (type1, TREE_TYPE (ref));
990 if (cmp < 0
991 && (!end_struct_ref1
992 || compare_type_sizes (TREE_TYPE (end_struct_ref1),
993 TREE_TYPE (ref)) < 0))
994 break;
995 /* If types may be of same size, see if we can decide about their
996 equality. */
997 if (cmp == 0)
999 same_p2 = same_type_for_tbaa (TREE_TYPE (ref), type1);
1000 if (same_p2 == 1)
1001 break;
1002 /* In case we can't decide whether types are same try to
1003 continue looking for the exact match.
1004 Remember however that we possibly saw a match
1005 to bypass the access path continuations tests we do later. */
1006 if (same_p2 == -1)
1007 maybe_match = true;
1009 if (!handled_component_p (ref))
1010 break;
1011 ref = TREE_OPERAND (ref, 0);
1013 if (same_p2 == 1)
1015 /* We assume that arrays can overlap by multiple of their elements
1016 size as tested in gcc.dg/torture/alias-2.c.
1017 This partial overlap happen only when both arrays are bases of
1018 the access and not contained within another component ref.
1019 To be safe we also assume partial overlap for VLAs. */
1020 if (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
1021 && (!TYPE_SIZE (TREE_TYPE (base1))
1022 || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) != INTEGER_CST
1023 || ref == base2))
1024 /* Setting maybe_match to true triggers
1025 nonoverlapping_component_refs_p test later that still may do
1026 useful disambiguation. */
1027 maybe_match = true;
1028 else
1029 return aliasing_matching_component_refs_p (base1, ref1,
1030 offset1, max_size1,
1031 ref, ref2,
1032 offset2, max_size2);
1036 /* If we didn't find a common base, try the other way around. */
1037 if (cmp_outer <= 0
1038 || (end_struct_ref1
1039 && compare_type_sizes (TREE_TYPE (end_struct_ref1), type1) <= 0))
1041 tree ref = ref1;
1042 while (true)
1044 int cmp = compare_type_sizes (type2, TREE_TYPE (ref));
1045 if (cmp < 0
1046 && (!end_struct_ref2
1047 || compare_type_sizes (TREE_TYPE (end_struct_ref2),
1048 TREE_TYPE (ref)) < 0))
1049 break;
1050 /* If types may be of same size, see if we can decide about their
1051 equality. */
1052 if (cmp == 0)
1054 same_p1 = same_type_for_tbaa (TREE_TYPE (ref), type2);
1055 if (same_p1 == 1)
1056 break;
1057 if (same_p1 == -1)
1058 maybe_match = true;
1060 if (!handled_component_p (ref))
1061 break;
1062 ref = TREE_OPERAND (ref, 0);
1064 if (same_p1 == 1)
1066 if (TREE_CODE (TREE_TYPE (base2)) == ARRAY_TYPE
1067 && (!TYPE_SIZE (TREE_TYPE (base2))
1068 || TREE_CODE (TYPE_SIZE (TREE_TYPE (base2))) != INTEGER_CST
1069 || ref == base1))
1070 maybe_match = true;
1071 else
1072 return aliasing_matching_component_refs_p (ref, ref1,
1073 offset1, max_size1,
1074 base2, ref2,
1075 offset2, max_size2);
1079 /* In the following code we make an assumption that the types in access
1080 paths do not overlap and thus accesses alias only if one path can be
1081 continuation of another. If we was not able to decide about equivalence,
1082 we need to give up. */
1083 if (maybe_match)
1085 if (!nonoverlapping_component_refs_p (ref1, ref2))
1087 ++alias_stats.aliasing_component_refs_p_may_alias;
1088 return true;
1090 ++alias_stats.aliasing_component_refs_p_no_alias;
1091 return false;
1094 /* If we have two type access paths B1.path1 and B2.path2 they may
1095 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
1096 But we can still have a path that goes B1.path1...B2.path2 with
1097 a part that we do not see. So we can only disambiguate now
1098 if there is no B2 in the tail of path1 and no B1 on the
1099 tail of path2. */
1100 if (compare_type_sizes (TREE_TYPE (ref2), type1) >= 0
1101 && (!end_struct_ref1
1102 || compare_type_sizes (TREE_TYPE (ref2),
1103 TREE_TYPE (end_struct_ref1)) >= 0)
1104 && type_has_components_p (TREE_TYPE (ref2))
1105 && (base1_alias_set == ref2_alias_set
1106 || alias_set_subset_of (base1_alias_set, ref2_alias_set)))
1108 ++alias_stats.aliasing_component_refs_p_may_alias;
1109 return true;
1111 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
1112 if (compare_type_sizes (TREE_TYPE (ref1), type2) >= 0
1113 && (!end_struct_ref2
1114 || compare_type_sizes (TREE_TYPE (ref1),
1115 TREE_TYPE (end_struct_ref2)) >= 0)
1116 && type_has_components_p (TREE_TYPE (ref1))
1117 && (base2_alias_set == ref1_alias_set
1118 || alias_set_subset_of (base2_alias_set, ref1_alias_set)))
1120 ++alias_stats.aliasing_component_refs_p_may_alias;
1121 return true;
1123 ++alias_stats.aliasing_component_refs_p_no_alias;
1124 return false;
1127 /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1128 MATCH2 either point to the same address or are disjoint.
1129 MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1130 respectively or NULL in the case we established equivalence of bases.
1132 This test works by matching the initial segment of the access path
1133 and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1134 match was determined without use of TBAA oracle.
1136 Return 1 if we can determine that component references REF1 and REF2,
1137 that are within a common DECL, cannot overlap.
1139 Return 0 if paths are same and thus there is nothing to disambiguate more
1140 (i.e. there is must alias assuming there is must alias between MATCH1 and
1141 MATCH2)
1143 Return -1 if we can not determine 0 or 1 - this happens when we met
1144 non-matching types was met in the path.
1145 In this case it may make sense to continue by other disambiguation
1146 oracles. */
1148 static int
1149 nonoverlapping_component_refs_since_match_p (tree match1, tree ref1,
1150 tree match2, tree ref2)
1152 auto_vec<tree, 16> component_refs1;
1153 auto_vec<tree, 16> component_refs2;
1155 /* Create the stack of handled components for REF1. */
1156 while (handled_component_p (ref1) && ref1 != match1)
1158 if (TREE_CODE (ref1) == VIEW_CONVERT_EXPR
1159 || TREE_CODE (ref1) == BIT_FIELD_REF)
1160 component_refs1.truncate (0);
1161 else
1162 component_refs1.safe_push (ref1);
1163 ref1 = TREE_OPERAND (ref1, 0);
1165 if (TREE_CODE (ref1) == MEM_REF && ref1 != match1)
1167 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
1169 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1170 return -1;
1173 /* TODO: Handle TARGET_MEM_REF later. */
1174 if (TREE_CODE (ref1) == TARGET_MEM_REF && ref1 != match1)
1176 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1177 return -1;
1180 /* Create the stack of handled components for REF2. */
1181 while (handled_component_p (ref2) && ref2 != match2)
1183 if (TREE_CODE (ref2) == VIEW_CONVERT_EXPR
1184 || TREE_CODE (ref2) == BIT_FIELD_REF)
1185 component_refs2.truncate (0);
1186 else
1187 component_refs2.safe_push (ref2);
1188 ref2 = TREE_OPERAND (ref2, 0);
1190 if (TREE_CODE (ref2) == MEM_REF && ref2 != match2)
1192 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
1194 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1195 return -1;
1198 if (TREE_CODE (ref2) == TARGET_MEM_REF && ref2 != match2)
1200 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1201 return -1;
1204 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1205 rank. This is sufficient because we start from the same DECL and you
1206 cannot reference several fields at a time with COMPONENT_REFs (unlike
1207 with ARRAY_RANGE_REFs for arrays) so you always need the same number
1208 of them to access a sub-component, unless you're in a union, in which
1209 case the return value will precisely be false. */
1210 while (true)
1214 if (component_refs1.is_empty ())
1216 ++alias_stats
1217 .nonoverlapping_component_refs_since_match_p_may_alias;
1218 return 0;
1220 ref1 = component_refs1.pop ();
1222 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1226 if (component_refs2.is_empty ())
1228 ++alias_stats
1229 .nonoverlapping_component_refs_since_match_p_may_alias;
1230 return 0;
1232 ref2 = component_refs2.pop ();
1234 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1236 /* Beware of BIT_FIELD_REF. */
1237 if (TREE_CODE (ref1) != COMPONENT_REF
1238 || TREE_CODE (ref2) != COMPONENT_REF)
1240 ++alias_stats
1241 .nonoverlapping_component_refs_since_match_p_may_alias;
1242 return -1;
1245 tree field1 = TREE_OPERAND (ref1, 1);
1246 tree field2 = TREE_OPERAND (ref2, 1);
1248 /* ??? We cannot simply use the type of operand #0 of the refs here
1249 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1250 for common blocks instead of using unions like everyone else. */
1251 tree type1 = DECL_CONTEXT (field1);
1252 tree type2 = DECL_CONTEXT (field2);
1254 /* We cannot disambiguate fields in a union or qualified union. */
1255 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
1257 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1258 return -1;
1261 if (field1 != field2)
1263 /* A field and its representative need to be considered the
1264 same. */
1265 if (DECL_BIT_FIELD_REPRESENTATIVE (field1) == field2
1266 || DECL_BIT_FIELD_REPRESENTATIVE (field2) == field1)
1268 ++alias_stats
1269 .nonoverlapping_component_refs_since_match_p_may_alias;
1270 return 0;
1272 /* Different fields of the same record type cannot overlap.
1273 ??? Bitfields can overlap at RTL level so punt on them. */
1274 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1276 ++alias_stats
1277 .nonoverlapping_component_refs_since_match_p_may_alias;
1278 return 0;
1280 ++alias_stats.nonoverlapping_component_refs_since_match_p_no_alias;
1281 return 1;
1285 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1286 return 0;
1289 /* qsort compare function to sort FIELD_DECLs after their
1290 DECL_FIELD_CONTEXT TYPE_UID. */
1292 static inline int
1293 ncr_compar (const void *field1_, const void *field2_)
1295 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1296 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1297 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
1298 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
1299 if (uid1 < uid2)
1300 return -1;
1301 else if (uid1 > uid2)
1302 return 1;
1303 return 0;
1306 /* Return true if we can determine that the fields referenced cannot
1307 overlap for any pair of objects. This relies on TBAA. */
1309 static bool
1310 nonoverlapping_component_refs_p (const_tree x, const_tree y)
1312 if (!flag_strict_aliasing
1313 || !x || !y
1314 || !handled_component_p (x)
1315 || !handled_component_p (y))
1317 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1318 return false;
1321 auto_vec<const_tree, 16> fieldsx;
1322 while (handled_component_p (x))
1324 if (TREE_CODE (x) == COMPONENT_REF)
1326 tree field = TREE_OPERAND (x, 1);
1327 tree type = DECL_FIELD_CONTEXT (field);
1328 if (TREE_CODE (type) == RECORD_TYPE)
1329 fieldsx.safe_push (field);
1331 else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
1332 || TREE_CODE (x) == BIT_FIELD_REF)
1333 fieldsx.truncate (0);
1334 x = TREE_OPERAND (x, 0);
1336 if (fieldsx.length () == 0)
1337 return false;
1338 auto_vec<const_tree, 16> fieldsy;
1339 while (handled_component_p (y))
1341 if (TREE_CODE (y) == COMPONENT_REF)
1343 tree field = TREE_OPERAND (y, 1);
1344 tree type = DECL_FIELD_CONTEXT (field);
1345 if (TREE_CODE (type) == RECORD_TYPE)
1346 fieldsy.safe_push (TREE_OPERAND (y, 1));
1348 else if (TREE_CODE (y) == VIEW_CONVERT_EXPR
1349 || TREE_CODE (y) == BIT_FIELD_REF)
1350 fieldsy.truncate (0);
1351 y = TREE_OPERAND (y, 0);
1353 if (fieldsy.length () == 0)
1355 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1356 return false;
1359 /* Most common case first. */
1360 if (fieldsx.length () == 1
1361 && fieldsy.length () == 1)
1363 if ((DECL_FIELD_CONTEXT (fieldsx[0])
1364 == DECL_FIELD_CONTEXT (fieldsy[0]))
1365 && fieldsx[0] != fieldsy[0]
1366 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])))
1368 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1369 return true;
1371 else
1373 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1374 return false;
1378 if (fieldsx.length () == 2)
1380 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1381 std::swap (fieldsx[0], fieldsx[1]);
1383 else
1384 fieldsx.qsort (ncr_compar);
1386 if (fieldsy.length () == 2)
1388 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1389 std::swap (fieldsy[0], fieldsy[1]);
1391 else
1392 fieldsy.qsort (ncr_compar);
1394 unsigned i = 0, j = 0;
1397 const_tree fieldx = fieldsx[i];
1398 const_tree fieldy = fieldsy[j];
1399 tree typex = DECL_FIELD_CONTEXT (fieldx);
1400 tree typey = DECL_FIELD_CONTEXT (fieldy);
1401 if (typex == typey)
1403 /* We're left with accessing different fields of a structure,
1404 no possible overlap. */
1405 if (fieldx != fieldy)
1407 /* A field and its representative need to be considered the
1408 same. */
1409 if (DECL_BIT_FIELD_REPRESENTATIVE (fieldx) == fieldy
1410 || DECL_BIT_FIELD_REPRESENTATIVE (fieldy) == fieldx)
1412 /* Different fields of the same record type cannot overlap.
1413 ??? Bitfields can overlap at RTL level so punt on them. */
1414 else if (DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy))
1416 else
1418 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1419 return true;
1423 if (TYPE_UID (typex) < TYPE_UID (typey))
1425 i++;
1426 if (i == fieldsx.length ())
1427 break;
1429 else
1431 j++;
1432 if (j == fieldsy.length ())
1433 break;
1436 while (1);
1438 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1439 return false;
1443 /* Return true if two memory references based on the variables BASE1
1444 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1445 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1446 if non-NULL are the complete memory reference trees. */
1448 static bool
1449 decl_refs_may_alias_p (tree ref1, tree base1,
1450 poly_int64 offset1, poly_int64 max_size1,
1451 poly_int64 size1,
1452 tree ref2, tree base2,
1453 poly_int64 offset2, poly_int64 max_size2,
1454 poly_int64 size2)
1456 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1458 /* If both references are based on different variables, they cannot alias. */
1459 if (compare_base_decls (base1, base2) == 0)
1460 return false;
1462 /* If both references are based on the same variable, they cannot alias if
1463 the accesses do not overlap. */
1464 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1465 return false;
1467 /* If there is must alias, there is no use disambiguating further. */
1468 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1469 return true;
1471 /* For components with variable position, the above test isn't sufficient,
1472 so we disambiguate component references manually. */
1473 if (ref1 && ref2
1474 && handled_component_p (ref1) && handled_component_p (ref2)
1475 && nonoverlapping_component_refs_since_match_p (NULL, ref1,
1476 NULL, ref2) == 1)
1477 return false;
1479 return true;
1482 /* Return true if an indirect reference based on *PTR1 constrained
1483 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1484 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1485 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1486 in which case they are computed on-demand. REF1 and REF2
1487 if non-NULL are the complete memory reference trees. */
1489 static bool
1490 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1491 poly_int64 offset1, poly_int64 max_size1,
1492 poly_int64 size1,
1493 alias_set_type ref1_alias_set,
1494 alias_set_type base1_alias_set,
1495 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1496 poly_int64 offset2, poly_int64 max_size2,
1497 poly_int64 size2,
1498 alias_set_type ref2_alias_set,
1499 alias_set_type base2_alias_set, bool tbaa_p)
1501 tree ptr1;
1502 tree ptrtype1, dbase2;
1504 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1505 || TREE_CODE (base1) == TARGET_MEM_REF)
1506 && DECL_P (base2));
1508 ptr1 = TREE_OPERAND (base1, 0);
1509 poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1511 /* If only one reference is based on a variable, they cannot alias if
1512 the pointer access is beyond the extent of the variable access.
1513 (the pointer base cannot validly point to an offset less than zero
1514 of the variable).
1515 ??? IVOPTs creates bases that do not honor this restriction,
1516 so do not apply this optimization for TARGET_MEM_REFs. */
1517 if (TREE_CODE (base1) != TARGET_MEM_REF
1518 && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
1519 return false;
1520 /* They also cannot alias if the pointer may not point to the decl. */
1521 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1522 return false;
1524 /* Disambiguations that rely on strict aliasing rules follow. */
1525 if (!flag_strict_aliasing || !tbaa_p)
1526 return true;
1528 /* If the alias set for a pointer access is zero all bets are off. */
1529 if (base1_alias_set == 0 || base2_alias_set == 0)
1530 return true;
1532 /* When we are trying to disambiguate an access with a pointer dereference
1533 as base versus one with a decl as base we can use both the size
1534 of the decl and its dynamic type for extra disambiguation.
1535 ??? We do not know anything about the dynamic type of the decl
1536 other than that its alias-set contains base2_alias_set as a subset
1537 which does not help us here. */
1538 /* As we know nothing useful about the dynamic type of the decl just
1539 use the usual conflict check rather than a subset test.
1540 ??? We could introduce -fvery-strict-aliasing when the language
1541 does not allow decls to have a dynamic type that differs from their
1542 static type. Then we can check
1543 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1544 if (base1_alias_set != base2_alias_set
1545 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1546 return false;
1548 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1550 /* If the size of the access relevant for TBAA through the pointer
1551 is bigger than the size of the decl we can't possibly access the
1552 decl via that pointer. */
1553 if (/* ??? This in turn may run afoul when a decl of type T which is
1554 a member of union type U is accessed through a pointer to
1555 type U and sizeof T is smaller than sizeof U. */
1556 TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1557 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1558 && compare_sizes (DECL_SIZE (base2),
1559 TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
1560 return false;
1562 if (!ref2)
1563 return true;
1565 /* If the decl is accessed via a MEM_REF, reconstruct the base
1566 we can use for TBAA and an appropriately adjusted offset. */
1567 dbase2 = ref2;
1568 while (handled_component_p (dbase2))
1569 dbase2 = TREE_OPERAND (dbase2, 0);
1570 poly_int64 doffset1 = offset1;
1571 poly_offset_int doffset2 = offset2;
1572 if (TREE_CODE (dbase2) == MEM_REF
1573 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1575 doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
1576 tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
1577 /* If second reference is view-converted, give up now. */
1578 if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
1579 return true;
1582 /* If first reference is view-converted, give up now. */
1583 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
1584 return true;
1586 /* If both references are through the same type, they do not alias
1587 if the accesses do not overlap. This does extra disambiguation
1588 for mixed/pointer accesses but requires strict aliasing.
1589 For MEM_REFs we require that the component-ref offset we computed
1590 is relative to the start of the type which we ensure by
1591 comparing rvalue and access type and disregarding the constant
1592 pointer offset.
1594 But avoid treating variable length arrays as "objects", instead assume they
1595 can overlap by an exact multiple of their element size.
1596 See gcc.dg/torture/alias-2.c. */
1597 if (((TREE_CODE (base1) != TARGET_MEM_REF
1598 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1599 && (TREE_CODE (dbase2) != TARGET_MEM_REF
1600 || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
1601 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1
1602 && (TREE_CODE (TREE_TYPE (base1)) != ARRAY_TYPE
1603 || (TYPE_SIZE (TREE_TYPE (base1))
1604 && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) == INTEGER_CST)))
1606 if (!ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
1607 return false;
1608 if (!ref1 || !ref2
1609 /* If there is must alias, there is no use disambiguating further. */
1610 || (known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1611 return true;
1612 int res = nonoverlapping_component_refs_since_match_p (base1, ref1,
1613 base2, ref2);
1614 if (res == -1)
1615 return !nonoverlapping_component_refs_p (ref1, ref2);
1616 return !res;
1619 /* Do access-path based disambiguation. */
1620 if (ref1 && ref2
1621 && (handled_component_p (ref1) || handled_component_p (ref2)))
1622 return aliasing_component_refs_p (ref1,
1623 ref1_alias_set, base1_alias_set,
1624 offset1, max_size1,
1625 ref2,
1626 ref2_alias_set, base2_alias_set,
1627 offset2, max_size2);
1629 return true;
1632 /* Return true if two indirect references based on *PTR1
1633 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1634 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1635 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1636 in which case they are computed on-demand. REF1 and REF2
1637 if non-NULL are the complete memory reference trees. */
1639 static bool
1640 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1641 poly_int64 offset1, poly_int64 max_size1,
1642 poly_int64 size1,
1643 alias_set_type ref1_alias_set,
1644 alias_set_type base1_alias_set,
1645 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1646 poly_int64 offset2, poly_int64 max_size2,
1647 poly_int64 size2,
1648 alias_set_type ref2_alias_set,
1649 alias_set_type base2_alias_set, bool tbaa_p)
1651 tree ptr1;
1652 tree ptr2;
1653 tree ptrtype1, ptrtype2;
1655 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1656 || TREE_CODE (base1) == TARGET_MEM_REF)
1657 && (TREE_CODE (base2) == MEM_REF
1658 || TREE_CODE (base2) == TARGET_MEM_REF));
1660 ptr1 = TREE_OPERAND (base1, 0);
1661 ptr2 = TREE_OPERAND (base2, 0);
1663 /* If both bases are based on pointers they cannot alias if they may not
1664 point to the same memory object or if they point to the same object
1665 and the accesses do not overlap. */
1666 if ((!cfun || gimple_in_ssa_p (cfun))
1667 && operand_equal_p (ptr1, ptr2, 0)
1668 && (((TREE_CODE (base1) != TARGET_MEM_REF
1669 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1670 && (TREE_CODE (base2) != TARGET_MEM_REF
1671 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1672 || (TREE_CODE (base1) == TARGET_MEM_REF
1673 && TREE_CODE (base2) == TARGET_MEM_REF
1674 && (TMR_STEP (base1) == TMR_STEP (base2)
1675 || (TMR_STEP (base1) && TMR_STEP (base2)
1676 && operand_equal_p (TMR_STEP (base1),
1677 TMR_STEP (base2), 0)))
1678 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1679 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1680 && operand_equal_p (TMR_INDEX (base1),
1681 TMR_INDEX (base2), 0)))
1682 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1683 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1684 && operand_equal_p (TMR_INDEX2 (base1),
1685 TMR_INDEX2 (base2), 0))))))
1687 poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1688 poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
1689 if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
1690 offset2 + moff2, max_size2))
1691 return false;
1692 /* If there is must alias, there is no use disambiguating further. */
1693 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1694 return true;
1695 if (ref1 && ref2)
1697 int res = nonoverlapping_component_refs_since_match_p (NULL, ref1,
1698 NULL, ref2);
1699 if (res != -1)
1700 return !res;
1703 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1704 return false;
1706 /* Disambiguations that rely on strict aliasing rules follow. */
1707 if (!flag_strict_aliasing || !tbaa_p)
1708 return true;
1710 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1711 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1713 /* If the alias set for a pointer access is zero all bets are off. */
1714 if (base1_alias_set == 0
1715 || base2_alias_set == 0)
1716 return true;
1718 /* Do type-based disambiguation. */
1719 if (base1_alias_set != base2_alias_set
1720 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1721 return false;
1723 /* If either reference is view-converted, give up now. */
1724 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1725 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1726 return true;
1728 /* If both references are through the same type, they do not alias
1729 if the accesses do not overlap. This does extra disambiguation
1730 for mixed/pointer accesses but requires strict aliasing. */
1731 if ((TREE_CODE (base1) != TARGET_MEM_REF
1732 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1733 && (TREE_CODE (base2) != TARGET_MEM_REF
1734 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1735 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1736 TREE_TYPE (ptrtype2)) == 1
1737 /* But avoid treating arrays as "objects", instead assume they
1738 can overlap by an exact multiple of their element size.
1739 See gcc.dg/torture/alias-2.c. */
1740 && TREE_CODE (TREE_TYPE (ptrtype1)) != ARRAY_TYPE)
1742 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1743 return false;
1744 if (!ref1 || !ref2
1745 || (known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1746 return true;
1747 int res = nonoverlapping_component_refs_since_match_p (base1, ref1,
1748 base2, ref2);
1749 if (res == -1)
1750 return !nonoverlapping_component_refs_p (ref1, ref2);
1751 return !res;
1754 /* Do access-path based disambiguation. */
1755 if (ref1 && ref2
1756 && (handled_component_p (ref1) || handled_component_p (ref2)))
1757 return aliasing_component_refs_p (ref1,
1758 ref1_alias_set, base1_alias_set,
1759 offset1, max_size1,
1760 ref2,
1761 ref2_alias_set, base2_alias_set,
1762 offset2, max_size2);
1764 return true;
1767 /* Return true, if the two memory references REF1 and REF2 may alias. */
1769 static bool
1770 refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1772 tree base1, base2;
1773 poly_int64 offset1 = 0, offset2 = 0;
1774 poly_int64 max_size1 = -1, max_size2 = -1;
1775 bool var1_p, var2_p, ind1_p, ind2_p;
1777 gcc_checking_assert ((!ref1->ref
1778 || TREE_CODE (ref1->ref) == SSA_NAME
1779 || DECL_P (ref1->ref)
1780 || TREE_CODE (ref1->ref) == STRING_CST
1781 || handled_component_p (ref1->ref)
1782 || TREE_CODE (ref1->ref) == MEM_REF
1783 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1784 && (!ref2->ref
1785 || TREE_CODE (ref2->ref) == SSA_NAME
1786 || DECL_P (ref2->ref)
1787 || TREE_CODE (ref2->ref) == STRING_CST
1788 || handled_component_p (ref2->ref)
1789 || TREE_CODE (ref2->ref) == MEM_REF
1790 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1792 /* Decompose the references into their base objects and the access. */
1793 base1 = ao_ref_base (ref1);
1794 offset1 = ref1->offset;
1795 max_size1 = ref1->max_size;
1796 base2 = ao_ref_base (ref2);
1797 offset2 = ref2->offset;
1798 max_size2 = ref2->max_size;
1800 /* We can end up with registers or constants as bases for example from
1801 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1802 which is seen as a struct copy. */
1803 if (TREE_CODE (base1) == SSA_NAME
1804 || TREE_CODE (base1) == CONST_DECL
1805 || TREE_CODE (base1) == CONSTRUCTOR
1806 || TREE_CODE (base1) == ADDR_EXPR
1807 || CONSTANT_CLASS_P (base1)
1808 || TREE_CODE (base2) == SSA_NAME
1809 || TREE_CODE (base2) == CONST_DECL
1810 || TREE_CODE (base2) == CONSTRUCTOR
1811 || TREE_CODE (base2) == ADDR_EXPR
1812 || CONSTANT_CLASS_P (base2))
1813 return false;
1815 /* We can end up referring to code via function and label decls.
1816 As we likely do not properly track code aliases conservatively
1817 bail out. */
1818 if (TREE_CODE (base1) == FUNCTION_DECL
1819 || TREE_CODE (base1) == LABEL_DECL
1820 || TREE_CODE (base2) == FUNCTION_DECL
1821 || TREE_CODE (base2) == LABEL_DECL)
1822 return true;
1824 /* Two volatile accesses always conflict. */
1825 if (ref1->volatile_p
1826 && ref2->volatile_p)
1827 return true;
1829 /* Defer to simple offset based disambiguation if we have
1830 references based on two decls. Do this before defering to
1831 TBAA to handle must-alias cases in conformance with the
1832 GCC extension of allowing type-punning through unions. */
1833 var1_p = DECL_P (base1);
1834 var2_p = DECL_P (base2);
1835 if (var1_p && var2_p)
1836 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1837 ref1->size,
1838 ref2->ref, base2, offset2, max_size2,
1839 ref2->size);
1841 /* Handle restrict based accesses.
1842 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1843 here. */
1844 tree rbase1 = base1;
1845 tree rbase2 = base2;
1846 if (var1_p)
1848 rbase1 = ref1->ref;
1849 if (rbase1)
1850 while (handled_component_p (rbase1))
1851 rbase1 = TREE_OPERAND (rbase1, 0);
1853 if (var2_p)
1855 rbase2 = ref2->ref;
1856 if (rbase2)
1857 while (handled_component_p (rbase2))
1858 rbase2 = TREE_OPERAND (rbase2, 0);
1860 if (rbase1 && rbase2
1861 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1862 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1863 /* If the accesses are in the same restrict clique... */
1864 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1865 /* But based on different pointers they do not alias. */
1866 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1867 return false;
1869 ind1_p = (TREE_CODE (base1) == MEM_REF
1870 || TREE_CODE (base1) == TARGET_MEM_REF);
1871 ind2_p = (TREE_CODE (base2) == MEM_REF
1872 || TREE_CODE (base2) == TARGET_MEM_REF);
1874 /* Canonicalize the pointer-vs-decl case. */
1875 if (ind1_p && var2_p)
1877 std::swap (offset1, offset2);
1878 std::swap (max_size1, max_size2);
1879 std::swap (base1, base2);
1880 std::swap (ref1, ref2);
1881 var1_p = true;
1882 ind1_p = false;
1883 var2_p = false;
1884 ind2_p = true;
1887 /* First defer to TBAA if possible. */
1888 if (tbaa_p
1889 && flag_strict_aliasing
1890 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1891 ao_ref_alias_set (ref2)))
1892 return false;
1894 /* If the reference is based on a pointer that points to memory
1895 that may not be written to then the other reference cannot possibly
1896 clobber it. */
1897 if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
1898 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
1899 || (ind1_p
1900 && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
1901 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
1902 return false;
1904 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1905 if (var1_p && ind2_p)
1906 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1907 offset2, max_size2, ref2->size,
1908 ao_ref_alias_set (ref2),
1909 ao_ref_base_alias_set (ref2),
1910 ref1->ref, base1,
1911 offset1, max_size1, ref1->size,
1912 ao_ref_alias_set (ref1),
1913 ao_ref_base_alias_set (ref1),
1914 tbaa_p);
1915 else if (ind1_p && ind2_p)
1916 return indirect_refs_may_alias_p (ref1->ref, base1,
1917 offset1, max_size1, ref1->size,
1918 ao_ref_alias_set (ref1),
1919 ao_ref_base_alias_set (ref1),
1920 ref2->ref, base2,
1921 offset2, max_size2, ref2->size,
1922 ao_ref_alias_set (ref2),
1923 ao_ref_base_alias_set (ref2),
1924 tbaa_p);
1926 gcc_unreachable ();
1929 /* Return true, if the two memory references REF1 and REF2 may alias
1930 and update statistics. */
1932 bool
1933 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1935 bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
1936 if (res)
1937 ++alias_stats.refs_may_alias_p_may_alias;
1938 else
1939 ++alias_stats.refs_may_alias_p_no_alias;
1940 return res;
1943 static bool
1944 refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
1946 ao_ref r1;
1947 ao_ref_init (&r1, ref1);
1948 return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
1951 bool
1952 refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
1954 ao_ref r1, r2;
1955 ao_ref_init (&r1, ref1);
1956 ao_ref_init (&r2, ref2);
1957 return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
1960 /* Returns true if there is a anti-dependence for the STORE that
1961 executes after the LOAD. */
1963 bool
1964 refs_anti_dependent_p (tree load, tree store)
1966 ao_ref r1, r2;
1967 ao_ref_init (&r1, load);
1968 ao_ref_init (&r2, store);
1969 return refs_may_alias_p_1 (&r1, &r2, false);
1972 /* Returns true if there is a output dependence for the stores
1973 STORE1 and STORE2. */
1975 bool
1976 refs_output_dependent_p (tree store1, tree store2)
1978 ao_ref r1, r2;
1979 ao_ref_init (&r1, store1);
1980 ao_ref_init (&r2, store2);
1981 return refs_may_alias_p_1 (&r1, &r2, false);
1984 /* If the call CALL may use the memory reference REF return true,
1985 otherwise return false. */
1987 static bool
1988 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
1990 tree base, callee;
1991 unsigned i;
1992 int flags = gimple_call_flags (call);
1994 /* Const functions without a static chain do not implicitly use memory. */
1995 if (!gimple_call_chain (call)
1996 && (flags & (ECF_CONST|ECF_NOVOPS)))
1997 goto process_args;
1999 base = ao_ref_base (ref);
2000 if (!base)
2001 return true;
2003 /* A call that is not without side-effects might involve volatile
2004 accesses and thus conflicts with all other volatile accesses. */
2005 if (ref->volatile_p)
2006 return true;
2008 /* If the reference is based on a decl that is not aliased the call
2009 cannot possibly use it. */
2010 if (DECL_P (base)
2011 && !may_be_aliased (base)
2012 /* But local statics can be used through recursion. */
2013 && !is_global_var (base))
2014 goto process_args;
2016 callee = gimple_call_fndecl (call);
2018 /* Handle those builtin functions explicitly that do not act as
2019 escape points. See tree-ssa-structalias.c:find_func_aliases
2020 for the list of builtins we might need to handle here. */
2021 if (callee != NULL_TREE
2022 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2023 switch (DECL_FUNCTION_CODE (callee))
2025 /* All the following functions read memory pointed to by
2026 their second argument. strcat/strncat additionally
2027 reads memory pointed to by the first argument. */
2028 case BUILT_IN_STRCAT:
2029 case BUILT_IN_STRNCAT:
2031 ao_ref dref;
2032 ao_ref_init_from_ptr_and_size (&dref,
2033 gimple_call_arg (call, 0),
2034 NULL_TREE);
2035 if (refs_may_alias_p_1 (&dref, ref, false))
2036 return true;
2038 /* FALLTHRU */
2039 case BUILT_IN_STRCPY:
2040 case BUILT_IN_STRNCPY:
2041 case BUILT_IN_MEMCPY:
2042 case BUILT_IN_MEMMOVE:
2043 case BUILT_IN_MEMPCPY:
2044 case BUILT_IN_STPCPY:
2045 case BUILT_IN_STPNCPY:
2046 case BUILT_IN_TM_MEMCPY:
2047 case BUILT_IN_TM_MEMMOVE:
2049 ao_ref dref;
2050 tree size = NULL_TREE;
2051 if (gimple_call_num_args (call) == 3)
2052 size = gimple_call_arg (call, 2);
2053 ao_ref_init_from_ptr_and_size (&dref,
2054 gimple_call_arg (call, 1),
2055 size);
2056 return refs_may_alias_p_1 (&dref, ref, false);
2058 case BUILT_IN_STRCAT_CHK:
2059 case BUILT_IN_STRNCAT_CHK:
2061 ao_ref dref;
2062 ao_ref_init_from_ptr_and_size (&dref,
2063 gimple_call_arg (call, 0),
2064 NULL_TREE);
2065 if (refs_may_alias_p_1 (&dref, ref, false))
2066 return true;
2068 /* FALLTHRU */
2069 case BUILT_IN_STRCPY_CHK:
2070 case BUILT_IN_STRNCPY_CHK:
2071 case BUILT_IN_MEMCPY_CHK:
2072 case BUILT_IN_MEMMOVE_CHK:
2073 case BUILT_IN_MEMPCPY_CHK:
2074 case BUILT_IN_STPCPY_CHK:
2075 case BUILT_IN_STPNCPY_CHK:
2077 ao_ref dref;
2078 tree size = NULL_TREE;
2079 if (gimple_call_num_args (call) == 4)
2080 size = gimple_call_arg (call, 2);
2081 ao_ref_init_from_ptr_and_size (&dref,
2082 gimple_call_arg (call, 1),
2083 size);
2084 return refs_may_alias_p_1 (&dref, ref, false);
2086 case BUILT_IN_BCOPY:
2088 ao_ref dref;
2089 tree size = gimple_call_arg (call, 2);
2090 ao_ref_init_from_ptr_and_size (&dref,
2091 gimple_call_arg (call, 0),
2092 size);
2093 return refs_may_alias_p_1 (&dref, ref, false);
2096 /* The following functions read memory pointed to by their
2097 first argument. */
2098 CASE_BUILT_IN_TM_LOAD (1):
2099 CASE_BUILT_IN_TM_LOAD (2):
2100 CASE_BUILT_IN_TM_LOAD (4):
2101 CASE_BUILT_IN_TM_LOAD (8):
2102 CASE_BUILT_IN_TM_LOAD (FLOAT):
2103 CASE_BUILT_IN_TM_LOAD (DOUBLE):
2104 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
2105 CASE_BUILT_IN_TM_LOAD (M64):
2106 CASE_BUILT_IN_TM_LOAD (M128):
2107 CASE_BUILT_IN_TM_LOAD (M256):
2108 case BUILT_IN_TM_LOG:
2109 case BUILT_IN_TM_LOG_1:
2110 case BUILT_IN_TM_LOG_2:
2111 case BUILT_IN_TM_LOG_4:
2112 case BUILT_IN_TM_LOG_8:
2113 case BUILT_IN_TM_LOG_FLOAT:
2114 case BUILT_IN_TM_LOG_DOUBLE:
2115 case BUILT_IN_TM_LOG_LDOUBLE:
2116 case BUILT_IN_TM_LOG_M64:
2117 case BUILT_IN_TM_LOG_M128:
2118 case BUILT_IN_TM_LOG_M256:
2119 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
2121 /* These read memory pointed to by the first argument. */
2122 case BUILT_IN_STRDUP:
2123 case BUILT_IN_STRNDUP:
2124 case BUILT_IN_REALLOC:
2126 ao_ref dref;
2127 tree size = NULL_TREE;
2128 if (gimple_call_num_args (call) == 2)
2129 size = gimple_call_arg (call, 1);
2130 ao_ref_init_from_ptr_and_size (&dref,
2131 gimple_call_arg (call, 0),
2132 size);
2133 return refs_may_alias_p_1 (&dref, ref, false);
2135 /* These read memory pointed to by the first argument. */
2136 case BUILT_IN_INDEX:
2137 case BUILT_IN_STRCHR:
2138 case BUILT_IN_STRRCHR:
2140 ao_ref dref;
2141 ao_ref_init_from_ptr_and_size (&dref,
2142 gimple_call_arg (call, 0),
2143 NULL_TREE);
2144 return refs_may_alias_p_1 (&dref, ref, false);
2146 /* These read memory pointed to by the first argument with size
2147 in the third argument. */
2148 case BUILT_IN_MEMCHR:
2150 ao_ref dref;
2151 ao_ref_init_from_ptr_and_size (&dref,
2152 gimple_call_arg (call, 0),
2153 gimple_call_arg (call, 2));
2154 return refs_may_alias_p_1 (&dref, ref, false);
2156 /* These read memory pointed to by the first and second arguments. */
2157 case BUILT_IN_STRSTR:
2158 case BUILT_IN_STRPBRK:
2160 ao_ref dref;
2161 ao_ref_init_from_ptr_and_size (&dref,
2162 gimple_call_arg (call, 0),
2163 NULL_TREE);
2164 if (refs_may_alias_p_1 (&dref, ref, false))
2165 return true;
2166 ao_ref_init_from_ptr_and_size (&dref,
2167 gimple_call_arg (call, 1),
2168 NULL_TREE);
2169 return refs_may_alias_p_1 (&dref, ref, false);
2172 /* The following builtins do not read from memory. */
2173 case BUILT_IN_FREE:
2174 case BUILT_IN_MALLOC:
2175 case BUILT_IN_POSIX_MEMALIGN:
2176 case BUILT_IN_ALIGNED_ALLOC:
2177 case BUILT_IN_CALLOC:
2178 CASE_BUILT_IN_ALLOCA:
2179 case BUILT_IN_STACK_SAVE:
2180 case BUILT_IN_STACK_RESTORE:
2181 case BUILT_IN_MEMSET:
2182 case BUILT_IN_TM_MEMSET:
2183 case BUILT_IN_MEMSET_CHK:
2184 case BUILT_IN_FREXP:
2185 case BUILT_IN_FREXPF:
2186 case BUILT_IN_FREXPL:
2187 case BUILT_IN_GAMMA_R:
2188 case BUILT_IN_GAMMAF_R:
2189 case BUILT_IN_GAMMAL_R:
2190 case BUILT_IN_LGAMMA_R:
2191 case BUILT_IN_LGAMMAF_R:
2192 case BUILT_IN_LGAMMAL_R:
2193 case BUILT_IN_MODF:
2194 case BUILT_IN_MODFF:
2195 case BUILT_IN_MODFL:
2196 case BUILT_IN_REMQUO:
2197 case BUILT_IN_REMQUOF:
2198 case BUILT_IN_REMQUOL:
2199 case BUILT_IN_SINCOS:
2200 case BUILT_IN_SINCOSF:
2201 case BUILT_IN_SINCOSL:
2202 case BUILT_IN_ASSUME_ALIGNED:
2203 case BUILT_IN_VA_END:
2204 return false;
2205 /* __sync_* builtins and some OpenMP builtins act as threading
2206 barriers. */
2207 #undef DEF_SYNC_BUILTIN
2208 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2209 #include "sync-builtins.def"
2210 #undef DEF_SYNC_BUILTIN
2211 case BUILT_IN_GOMP_ATOMIC_START:
2212 case BUILT_IN_GOMP_ATOMIC_END:
2213 case BUILT_IN_GOMP_BARRIER:
2214 case BUILT_IN_GOMP_BARRIER_CANCEL:
2215 case BUILT_IN_GOMP_TASKWAIT:
2216 case BUILT_IN_GOMP_TASKGROUP_END:
2217 case BUILT_IN_GOMP_CRITICAL_START:
2218 case BUILT_IN_GOMP_CRITICAL_END:
2219 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2220 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2221 case BUILT_IN_GOMP_LOOP_END:
2222 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2223 case BUILT_IN_GOMP_ORDERED_START:
2224 case BUILT_IN_GOMP_ORDERED_END:
2225 case BUILT_IN_GOMP_SECTIONS_END:
2226 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2227 case BUILT_IN_GOMP_SINGLE_COPY_START:
2228 case BUILT_IN_GOMP_SINGLE_COPY_END:
2229 return true;
2231 default:
2232 /* Fallthru to general call handling. */;
2235 /* Check if base is a global static variable that is not read
2236 by the function. */
2237 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2239 struct cgraph_node *node = cgraph_node::get (callee);
2240 bitmap not_read;
2242 /* FIXME: Callee can be an OMP builtin that does not have a call graph
2243 node yet. We should enforce that there are nodes for all decls in the
2244 IL and remove this check instead. */
2245 if (node
2246 && (not_read = ipa_reference_get_not_read_global (node))
2247 && bitmap_bit_p (not_read, ipa_reference_var_uid (base)))
2248 goto process_args;
2251 /* Check if the base variable is call-used. */
2252 if (DECL_P (base))
2254 if (pt_solution_includes (gimple_call_use_set (call), base))
2255 return true;
2257 else if ((TREE_CODE (base) == MEM_REF
2258 || TREE_CODE (base) == TARGET_MEM_REF)
2259 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2261 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2262 if (!pi)
2263 return true;
2265 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
2266 return true;
2268 else
2269 return true;
2271 /* Inspect call arguments for passed-by-value aliases. */
2272 process_args:
2273 for (i = 0; i < gimple_call_num_args (call); ++i)
2275 tree op = gimple_call_arg (call, i);
2276 int flags = gimple_call_arg_flags (call, i);
2278 if (flags & EAF_UNUSED)
2279 continue;
2281 if (TREE_CODE (op) == WITH_SIZE_EXPR)
2282 op = TREE_OPERAND (op, 0);
2284 if (TREE_CODE (op) != SSA_NAME
2285 && !is_gimple_min_invariant (op))
2287 ao_ref r;
2288 ao_ref_init (&r, op);
2289 if (refs_may_alias_p_1 (&r, ref, tbaa_p))
2290 return true;
2294 return false;
2297 static bool
2298 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
2300 bool res;
2301 res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
2302 if (res)
2303 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
2304 else
2305 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
2306 return res;
2310 /* If the statement STMT may use the memory reference REF return
2311 true, otherwise return false. */
2313 bool
2314 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
2316 if (is_gimple_assign (stmt))
2318 tree rhs;
2320 /* All memory assign statements are single. */
2321 if (!gimple_assign_single_p (stmt))
2322 return false;
2324 rhs = gimple_assign_rhs1 (stmt);
2325 if (is_gimple_reg (rhs)
2326 || is_gimple_min_invariant (rhs)
2327 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
2328 return false;
2330 return refs_may_alias_p (rhs, ref, tbaa_p);
2332 else if (is_gimple_call (stmt))
2333 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
2334 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2336 tree retval = gimple_return_retval (return_stmt);
2337 if (retval
2338 && TREE_CODE (retval) != SSA_NAME
2339 && !is_gimple_min_invariant (retval)
2340 && refs_may_alias_p (retval, ref, tbaa_p))
2341 return true;
2342 /* If ref escapes the function then the return acts as a use. */
2343 tree base = ao_ref_base (ref);
2344 if (!base)
2346 else if (DECL_P (base))
2347 return is_global_var (base);
2348 else if (TREE_CODE (base) == MEM_REF
2349 || TREE_CODE (base) == TARGET_MEM_REF)
2350 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
2351 return false;
2354 return true;
2357 bool
2358 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
2360 ao_ref r;
2361 ao_ref_init (&r, ref);
2362 return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
2365 /* If the call in statement CALL may clobber the memory reference REF
2366 return true, otherwise return false. */
2368 bool
2369 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
2371 tree base;
2372 tree callee;
2374 /* If the call is pure or const it cannot clobber anything. */
2375 if (gimple_call_flags (call)
2376 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
2377 return false;
2378 if (gimple_call_internal_p (call))
2379 switch (gimple_call_internal_fn (call))
2381 /* Treat these internal calls like ECF_PURE for aliasing,
2382 they don't write to any memory the program should care about.
2383 They have important other side-effects, and read memory,
2384 so can't be ECF_NOVOPS. */
2385 case IFN_UBSAN_NULL:
2386 case IFN_UBSAN_BOUNDS:
2387 case IFN_UBSAN_VPTR:
2388 case IFN_UBSAN_OBJECT_SIZE:
2389 case IFN_UBSAN_PTR:
2390 case IFN_ASAN_CHECK:
2391 return false;
2392 default:
2393 break;
2396 base = ao_ref_base (ref);
2397 if (!base)
2398 return true;
2400 if (TREE_CODE (base) == SSA_NAME
2401 || CONSTANT_CLASS_P (base))
2402 return false;
2404 /* A call that is not without side-effects might involve volatile
2405 accesses and thus conflicts with all other volatile accesses. */
2406 if (ref->volatile_p)
2407 return true;
2409 /* If the reference is based on a decl that is not aliased the call
2410 cannot possibly clobber it. */
2411 if (DECL_P (base)
2412 && !may_be_aliased (base)
2413 /* But local non-readonly statics can be modified through recursion
2414 or the call may implement a threading barrier which we must
2415 treat as may-def. */
2416 && (TREE_READONLY (base)
2417 || !is_global_var (base)))
2418 return false;
2420 /* If the reference is based on a pointer that points to memory
2421 that may not be written to then the call cannot possibly clobber it. */
2422 if ((TREE_CODE (base) == MEM_REF
2423 || TREE_CODE (base) == TARGET_MEM_REF)
2424 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2425 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
2426 return false;
2428 callee = gimple_call_fndecl (call);
2430 /* Handle those builtin functions explicitly that do not act as
2431 escape points. See tree-ssa-structalias.c:find_func_aliases
2432 for the list of builtins we might need to handle here. */
2433 if (callee != NULL_TREE
2434 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2435 switch (DECL_FUNCTION_CODE (callee))
2437 /* All the following functions clobber memory pointed to by
2438 their first argument. */
2439 case BUILT_IN_STRCPY:
2440 case BUILT_IN_STRNCPY:
2441 case BUILT_IN_MEMCPY:
2442 case BUILT_IN_MEMMOVE:
2443 case BUILT_IN_MEMPCPY:
2444 case BUILT_IN_STPCPY:
2445 case BUILT_IN_STPNCPY:
2446 case BUILT_IN_STRCAT:
2447 case BUILT_IN_STRNCAT:
2448 case BUILT_IN_MEMSET:
2449 case BUILT_IN_TM_MEMSET:
2450 CASE_BUILT_IN_TM_STORE (1):
2451 CASE_BUILT_IN_TM_STORE (2):
2452 CASE_BUILT_IN_TM_STORE (4):
2453 CASE_BUILT_IN_TM_STORE (8):
2454 CASE_BUILT_IN_TM_STORE (FLOAT):
2455 CASE_BUILT_IN_TM_STORE (DOUBLE):
2456 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2457 CASE_BUILT_IN_TM_STORE (M64):
2458 CASE_BUILT_IN_TM_STORE (M128):
2459 CASE_BUILT_IN_TM_STORE (M256):
2460 case BUILT_IN_TM_MEMCPY:
2461 case BUILT_IN_TM_MEMMOVE:
2463 ao_ref dref;
2464 tree size = NULL_TREE;
2465 /* Don't pass in size for strncat, as the maximum size
2466 is strlen (dest) + n + 1 instead of n, resp.
2467 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2468 known. */
2469 if (gimple_call_num_args (call) == 3
2470 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2471 size = gimple_call_arg (call, 2);
2472 ao_ref_init_from_ptr_and_size (&dref,
2473 gimple_call_arg (call, 0),
2474 size);
2475 return refs_may_alias_p_1 (&dref, ref, false);
2477 case BUILT_IN_STRCPY_CHK:
2478 case BUILT_IN_STRNCPY_CHK:
2479 case BUILT_IN_MEMCPY_CHK:
2480 case BUILT_IN_MEMMOVE_CHK:
2481 case BUILT_IN_MEMPCPY_CHK:
2482 case BUILT_IN_STPCPY_CHK:
2483 case BUILT_IN_STPNCPY_CHK:
2484 case BUILT_IN_STRCAT_CHK:
2485 case BUILT_IN_STRNCAT_CHK:
2486 case BUILT_IN_MEMSET_CHK:
2488 ao_ref dref;
2489 tree size = NULL_TREE;
2490 /* Don't pass in size for __strncat_chk, as the maximum size
2491 is strlen (dest) + n + 1 instead of n, resp.
2492 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2493 known. */
2494 if (gimple_call_num_args (call) == 4
2495 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2496 size = gimple_call_arg (call, 2);
2497 ao_ref_init_from_ptr_and_size (&dref,
2498 gimple_call_arg (call, 0),
2499 size);
2500 return refs_may_alias_p_1 (&dref, ref, false);
2502 case BUILT_IN_BCOPY:
2504 ao_ref dref;
2505 tree size = gimple_call_arg (call, 2);
2506 ao_ref_init_from_ptr_and_size (&dref,
2507 gimple_call_arg (call, 1),
2508 size);
2509 return refs_may_alias_p_1 (&dref, ref, false);
2511 /* Allocating memory does not have any side-effects apart from
2512 being the definition point for the pointer. */
2513 case BUILT_IN_MALLOC:
2514 case BUILT_IN_ALIGNED_ALLOC:
2515 case BUILT_IN_CALLOC:
2516 case BUILT_IN_STRDUP:
2517 case BUILT_IN_STRNDUP:
2518 /* Unix98 specifies that errno is set on allocation failure. */
2519 if (flag_errno_math
2520 && targetm.ref_may_alias_errno (ref))
2521 return true;
2522 return false;
2523 case BUILT_IN_STACK_SAVE:
2524 CASE_BUILT_IN_ALLOCA:
2525 case BUILT_IN_ASSUME_ALIGNED:
2526 return false;
2527 /* But posix_memalign stores a pointer into the memory pointed to
2528 by its first argument. */
2529 case BUILT_IN_POSIX_MEMALIGN:
2531 tree ptrptr = gimple_call_arg (call, 0);
2532 ao_ref dref;
2533 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2534 TYPE_SIZE_UNIT (ptr_type_node));
2535 return (refs_may_alias_p_1 (&dref, ref, false)
2536 || (flag_errno_math
2537 && targetm.ref_may_alias_errno (ref)));
2539 /* Freeing memory kills the pointed-to memory. More importantly
2540 the call has to serve as a barrier for moving loads and stores
2541 across it. */
2542 case BUILT_IN_FREE:
2543 case BUILT_IN_VA_END:
2545 tree ptr = gimple_call_arg (call, 0);
2546 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2548 /* Realloc serves both as allocation point and deallocation point. */
2549 case BUILT_IN_REALLOC:
2551 tree ptr = gimple_call_arg (call, 0);
2552 /* Unix98 specifies that errno is set on allocation failure. */
2553 return ((flag_errno_math
2554 && targetm.ref_may_alias_errno (ref))
2555 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2557 case BUILT_IN_GAMMA_R:
2558 case BUILT_IN_GAMMAF_R:
2559 case BUILT_IN_GAMMAL_R:
2560 case BUILT_IN_LGAMMA_R:
2561 case BUILT_IN_LGAMMAF_R:
2562 case BUILT_IN_LGAMMAL_R:
2564 tree out = gimple_call_arg (call, 1);
2565 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2566 return true;
2567 if (flag_errno_math)
2568 break;
2569 return false;
2571 case BUILT_IN_FREXP:
2572 case BUILT_IN_FREXPF:
2573 case BUILT_IN_FREXPL:
2574 case BUILT_IN_MODF:
2575 case BUILT_IN_MODFF:
2576 case BUILT_IN_MODFL:
2578 tree out = gimple_call_arg (call, 1);
2579 return ptr_deref_may_alias_ref_p_1 (out, ref);
2581 case BUILT_IN_REMQUO:
2582 case BUILT_IN_REMQUOF:
2583 case BUILT_IN_REMQUOL:
2585 tree out = gimple_call_arg (call, 2);
2586 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2587 return true;
2588 if (flag_errno_math)
2589 break;
2590 return false;
2592 case BUILT_IN_SINCOS:
2593 case BUILT_IN_SINCOSF:
2594 case BUILT_IN_SINCOSL:
2596 tree sin = gimple_call_arg (call, 1);
2597 tree cos = gimple_call_arg (call, 2);
2598 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2599 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2601 /* __sync_* builtins and some OpenMP builtins act as threading
2602 barriers. */
2603 #undef DEF_SYNC_BUILTIN
2604 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2605 #include "sync-builtins.def"
2606 #undef DEF_SYNC_BUILTIN
2607 case BUILT_IN_GOMP_ATOMIC_START:
2608 case BUILT_IN_GOMP_ATOMIC_END:
2609 case BUILT_IN_GOMP_BARRIER:
2610 case BUILT_IN_GOMP_BARRIER_CANCEL:
2611 case BUILT_IN_GOMP_TASKWAIT:
2612 case BUILT_IN_GOMP_TASKGROUP_END:
2613 case BUILT_IN_GOMP_CRITICAL_START:
2614 case BUILT_IN_GOMP_CRITICAL_END:
2615 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2616 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2617 case BUILT_IN_GOMP_LOOP_END:
2618 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2619 case BUILT_IN_GOMP_ORDERED_START:
2620 case BUILT_IN_GOMP_ORDERED_END:
2621 case BUILT_IN_GOMP_SECTIONS_END:
2622 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2623 case BUILT_IN_GOMP_SINGLE_COPY_START:
2624 case BUILT_IN_GOMP_SINGLE_COPY_END:
2625 return true;
2626 default:
2627 /* Fallthru to general call handling. */;
2630 /* Check if base is a global static variable that is not written
2631 by the function. */
2632 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2634 struct cgraph_node *node = cgraph_node::get (callee);
2635 bitmap not_written;
2637 if (node
2638 && (not_written = ipa_reference_get_not_written_global (node))
2639 && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
2640 return false;
2643 /* Check if the base variable is call-clobbered. */
2644 if (DECL_P (base))
2645 return pt_solution_includes (gimple_call_clobber_set (call), base);
2646 else if ((TREE_CODE (base) == MEM_REF
2647 || TREE_CODE (base) == TARGET_MEM_REF)
2648 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2650 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2651 if (!pi)
2652 return true;
2654 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2657 return true;
2660 /* If the call in statement CALL may clobber the memory reference REF
2661 return true, otherwise return false. */
2663 bool
2664 call_may_clobber_ref_p (gcall *call, tree ref)
2666 bool res;
2667 ao_ref r;
2668 ao_ref_init (&r, ref);
2669 res = call_may_clobber_ref_p_1 (call, &r);
2670 if (res)
2671 ++alias_stats.call_may_clobber_ref_p_may_alias;
2672 else
2673 ++alias_stats.call_may_clobber_ref_p_no_alias;
2674 return res;
2678 /* If the statement STMT may clobber the memory reference REF return true,
2679 otherwise return false. */
2681 bool
2682 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
2684 if (is_gimple_call (stmt))
2686 tree lhs = gimple_call_lhs (stmt);
2687 if (lhs
2688 && TREE_CODE (lhs) != SSA_NAME)
2690 ao_ref r;
2691 ao_ref_init (&r, lhs);
2692 if (refs_may_alias_p_1 (ref, &r, tbaa_p))
2693 return true;
2696 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2698 else if (gimple_assign_single_p (stmt))
2700 tree lhs = gimple_assign_lhs (stmt);
2701 if (TREE_CODE (lhs) != SSA_NAME)
2703 ao_ref r;
2704 ao_ref_init (&r, lhs);
2705 return refs_may_alias_p_1 (ref, &r, tbaa_p);
2708 else if (gimple_code (stmt) == GIMPLE_ASM)
2709 return true;
2711 return false;
2714 bool
2715 stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
2717 ao_ref r;
2718 ao_ref_init (&r, ref);
2719 return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
2722 /* Return true if store1 and store2 described by corresponding tuples
2723 <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
2724 address. */
2726 static bool
2727 same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
2728 poly_int64 max_size1,
2729 tree base2, poly_int64 offset2, poly_int64 size2,
2730 poly_int64 max_size2)
2732 /* Offsets need to be 0. */
2733 if (maybe_ne (offset1, 0)
2734 || maybe_ne (offset2, 0))
2735 return false;
2737 bool base1_obj_p = SSA_VAR_P (base1);
2738 bool base2_obj_p = SSA_VAR_P (base2);
2740 /* We need one object. */
2741 if (base1_obj_p == base2_obj_p)
2742 return false;
2743 tree obj = base1_obj_p ? base1 : base2;
2745 /* And we need one MEM_REF. */
2746 bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
2747 bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
2748 if (base1_memref_p == base2_memref_p)
2749 return false;
2750 tree memref = base1_memref_p ? base1 : base2;
2752 /* Sizes need to be valid. */
2753 if (!known_size_p (max_size1)
2754 || !known_size_p (max_size2)
2755 || !known_size_p (size1)
2756 || !known_size_p (size2))
2757 return false;
2759 /* Max_size needs to match size. */
2760 if (maybe_ne (max_size1, size1)
2761 || maybe_ne (max_size2, size2))
2762 return false;
2764 /* Sizes need to match. */
2765 if (maybe_ne (size1, size2))
2766 return false;
2769 /* Check that memref is a store to pointer with singleton points-to info. */
2770 if (!integer_zerop (TREE_OPERAND (memref, 1)))
2771 return false;
2772 tree ptr = TREE_OPERAND (memref, 0);
2773 if (TREE_CODE (ptr) != SSA_NAME)
2774 return false;
2775 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2776 unsigned int pt_uid;
2777 if (pi == NULL
2778 || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid))
2779 return false;
2781 /* Be conservative with non-call exceptions when the address might
2782 be NULL. */
2783 if (cfun->can_throw_non_call_exceptions && pi->pt.null)
2784 return false;
2786 /* Check that ptr points relative to obj. */
2787 unsigned int obj_uid = DECL_PT_UID (obj);
2788 if (obj_uid != pt_uid)
2789 return false;
2791 /* Check that the object size is the same as the store size. That ensures us
2792 that ptr points to the start of obj. */
2793 return (DECL_SIZE (obj)
2794 && poly_int_tree_p (DECL_SIZE (obj))
2795 && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
2798 /* If STMT kills the memory reference REF return true, otherwise
2799 return false. */
2801 bool
2802 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2804 if (!ao_ref_base (ref))
2805 return false;
2807 if (gimple_has_lhs (stmt)
2808 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2809 /* The assignment is not necessarily carried out if it can throw
2810 and we can catch it in the current function where we could inspect
2811 the previous value.
2812 ??? We only need to care about the RHS throwing. For aggregate
2813 assignments or similar calls and non-call exceptions the LHS
2814 might throw as well. */
2815 && !stmt_can_throw_internal (cfun, stmt))
2817 tree lhs = gimple_get_lhs (stmt);
2818 /* If LHS is literally a base of the access we are done. */
2819 if (ref->ref)
2821 tree base = ref->ref;
2822 tree innermost_dropped_array_ref = NULL_TREE;
2823 if (handled_component_p (base))
2825 tree saved_lhs0 = NULL_TREE;
2826 if (handled_component_p (lhs))
2828 saved_lhs0 = TREE_OPERAND (lhs, 0);
2829 TREE_OPERAND (lhs, 0) = integer_zero_node;
2833 /* Just compare the outermost handled component, if
2834 they are equal we have found a possible common
2835 base. */
2836 tree saved_base0 = TREE_OPERAND (base, 0);
2837 TREE_OPERAND (base, 0) = integer_zero_node;
2838 bool res = operand_equal_p (lhs, base, 0);
2839 TREE_OPERAND (base, 0) = saved_base0;
2840 if (res)
2841 break;
2842 /* Remember if we drop an array-ref that we need to
2843 double-check not being at struct end. */
2844 if (TREE_CODE (base) == ARRAY_REF
2845 || TREE_CODE (base) == ARRAY_RANGE_REF)
2846 innermost_dropped_array_ref = base;
2847 /* Otherwise drop handled components of the access. */
2848 base = saved_base0;
2850 while (handled_component_p (base));
2851 if (saved_lhs0)
2852 TREE_OPERAND (lhs, 0) = saved_lhs0;
2854 /* Finally check if the lhs has the same address and size as the
2855 base candidate of the access. Watch out if we have dropped
2856 an array-ref that was at struct end, this means ref->ref may
2857 be outside of the TYPE_SIZE of its base. */
2858 if ((! innermost_dropped_array_ref
2859 || ! array_at_struct_end_p (innermost_dropped_array_ref))
2860 && (lhs == base
2861 || (((TYPE_SIZE (TREE_TYPE (lhs))
2862 == TYPE_SIZE (TREE_TYPE (base)))
2863 || (TYPE_SIZE (TREE_TYPE (lhs))
2864 && TYPE_SIZE (TREE_TYPE (base))
2865 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2866 TYPE_SIZE (TREE_TYPE (base)),
2867 0)))
2868 && operand_equal_p (lhs, base,
2869 OEP_ADDRESS_OF
2870 | OEP_MATCH_SIDE_EFFECTS))))
2871 return true;
2874 /* Now look for non-literal equal bases with the restriction of
2875 handling constant offset and size. */
2876 /* For a must-alias check we need to be able to constrain
2877 the access properly. */
2878 if (!ref->max_size_known_p ())
2879 return false;
2880 poly_int64 size, offset, max_size, ref_offset = ref->offset;
2881 bool reverse;
2882 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
2883 &reverse);
2884 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2885 so base == ref->base does not always hold. */
2886 if (base != ref->base)
2888 /* Try using points-to info. */
2889 if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
2890 ref->offset, ref->size, ref->max_size))
2891 return true;
2893 /* If both base and ref->base are MEM_REFs, only compare the
2894 first operand, and if the second operand isn't equal constant,
2895 try to add the offsets into offset and ref_offset. */
2896 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2897 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2899 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2900 TREE_OPERAND (ref->base, 1)))
2902 poly_offset_int off1 = mem_ref_offset (base);
2903 off1 <<= LOG2_BITS_PER_UNIT;
2904 off1 += offset;
2905 poly_offset_int off2 = mem_ref_offset (ref->base);
2906 off2 <<= LOG2_BITS_PER_UNIT;
2907 off2 += ref_offset;
2908 if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
2909 size = -1;
2912 else
2913 size = -1;
2915 /* For a must-alias check we need to be able to constrain
2916 the access properly. */
2917 if (known_eq (size, max_size)
2918 && known_subrange_p (ref_offset, ref->max_size, offset, size))
2919 return true;
2922 if (is_gimple_call (stmt))
2924 tree callee = gimple_call_fndecl (stmt);
2925 if (callee != NULL_TREE
2926 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2927 switch (DECL_FUNCTION_CODE (callee))
2929 case BUILT_IN_FREE:
2931 tree ptr = gimple_call_arg (stmt, 0);
2932 tree base = ao_ref_base (ref);
2933 if (base && TREE_CODE (base) == MEM_REF
2934 && TREE_OPERAND (base, 0) == ptr)
2935 return true;
2936 break;
2939 case BUILT_IN_MEMCPY:
2940 case BUILT_IN_MEMPCPY:
2941 case BUILT_IN_MEMMOVE:
2942 case BUILT_IN_MEMSET:
2943 case BUILT_IN_MEMCPY_CHK:
2944 case BUILT_IN_MEMPCPY_CHK:
2945 case BUILT_IN_MEMMOVE_CHK:
2946 case BUILT_IN_MEMSET_CHK:
2947 case BUILT_IN_STRNCPY:
2948 case BUILT_IN_STPNCPY:
2949 case BUILT_IN_CALLOC:
2951 /* For a must-alias check we need to be able to constrain
2952 the access properly. */
2953 if (!ref->max_size_known_p ())
2954 return false;
2955 tree dest;
2956 tree len;
2958 /* In execution order a calloc call will never kill
2959 anything. However, DSE will (ab)use this interface
2960 to ask if a calloc call writes the same memory locations
2961 as a later assignment, memset, etc. So handle calloc
2962 in the expected way. */
2963 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
2965 tree arg0 = gimple_call_arg (stmt, 0);
2966 tree arg1 = gimple_call_arg (stmt, 1);
2967 if (TREE_CODE (arg0) != INTEGER_CST
2968 || TREE_CODE (arg1) != INTEGER_CST)
2969 return false;
2971 dest = gimple_call_lhs (stmt);
2972 len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
2974 else
2976 dest = gimple_call_arg (stmt, 0);
2977 len = gimple_call_arg (stmt, 2);
2979 if (!poly_int_tree_p (len))
2980 return false;
2981 tree rbase = ref->base;
2982 poly_offset_int roffset = ref->offset;
2983 ao_ref dref;
2984 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2985 tree base = ao_ref_base (&dref);
2986 poly_offset_int offset = dref.offset;
2987 if (!base || !known_size_p (dref.size))
2988 return false;
2989 if (TREE_CODE (base) == MEM_REF)
2991 if (TREE_CODE (rbase) != MEM_REF)
2992 return false;
2993 // Compare pointers.
2994 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
2995 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
2996 base = TREE_OPERAND (base, 0);
2997 rbase = TREE_OPERAND (rbase, 0);
2999 if (base == rbase
3000 && known_subrange_p (roffset, ref->max_size, offset,
3001 wi::to_poly_offset (len)
3002 << LOG2_BITS_PER_UNIT))
3003 return true;
3004 break;
3007 case BUILT_IN_VA_END:
3009 tree ptr = gimple_call_arg (stmt, 0);
3010 if (TREE_CODE (ptr) == ADDR_EXPR)
3012 tree base = ao_ref_base (ref);
3013 if (TREE_OPERAND (ptr, 0) == base)
3014 return true;
3016 break;
3019 default:;
3022 return false;
3025 bool
3026 stmt_kills_ref_p (gimple *stmt, tree ref)
3028 ao_ref r;
3029 ao_ref_init (&r, ref);
3030 return stmt_kills_ref_p (stmt, &r);
3034 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3035 TARGET or a statement clobbering the memory reference REF in which
3036 case false is returned. The walk starts with VUSE, one argument of PHI. */
3038 static bool
3039 maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3040 ao_ref *ref, tree vuse, unsigned int &limit, bitmap *visited,
3041 bool abort_on_visited,
3042 void *(*translate)(ao_ref *, tree, void *, bool *),
3043 void *data)
3045 basic_block bb = gimple_bb (phi);
3047 if (!*visited)
3048 *visited = BITMAP_ALLOC (NULL);
3050 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3052 /* Walk until we hit the target. */
3053 while (vuse != target)
3055 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3056 /* If we are searching for the target VUSE by walking up to
3057 TARGET_BB dominating the original PHI we are finished once
3058 we reach a default def or a definition in a block dominating
3059 that block. Update TARGET and return. */
3060 if (!target
3061 && (gimple_nop_p (def_stmt)
3062 || dominated_by_p (CDI_DOMINATORS,
3063 target_bb, gimple_bb (def_stmt))))
3065 target = vuse;
3066 return true;
3069 /* Recurse for PHI nodes. */
3070 if (gimple_code (def_stmt) == GIMPLE_PHI)
3072 /* An already visited PHI node ends the walk successfully. */
3073 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
3074 return !abort_on_visited;
3075 vuse = get_continuation_for_phi (def_stmt, ref, limit,
3076 visited, abort_on_visited,
3077 translate, data);
3078 if (!vuse)
3079 return false;
3080 continue;
3082 else if (gimple_nop_p (def_stmt))
3083 return false;
3084 else
3086 /* A clobbering statement or the end of the IL ends it failing. */
3087 if ((int)limit <= 0)
3088 return false;
3089 --limit;
3090 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
3092 bool disambiguate_only = true;
3093 if (translate
3094 && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
3096 else
3097 return false;
3100 /* If we reach a new basic-block see if we already skipped it
3101 in a previous walk that ended successfully. */
3102 if (gimple_bb (def_stmt) != bb)
3104 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3105 return !abort_on_visited;
3106 bb = gimple_bb (def_stmt);
3108 vuse = gimple_vuse (def_stmt);
3110 return true;
3114 /* Starting from a PHI node for the virtual operand of the memory reference
3115 REF find a continuation virtual operand that allows to continue walking
3116 statements dominating PHI skipping only statements that cannot possibly
3117 clobber REF. Decrements LIMIT for each alias disambiguation done
3118 and aborts the walk, returning NULL_TREE if it reaches zero.
3119 Returns NULL_TREE if no suitable virtual operand can be found. */
3121 tree
3122 get_continuation_for_phi (gimple *phi, ao_ref *ref,
3123 unsigned int &limit, bitmap *visited,
3124 bool abort_on_visited,
3125 void *(*translate)(ao_ref *, tree, void *, bool *),
3126 void *data)
3128 unsigned nargs = gimple_phi_num_args (phi);
3130 /* Through a single-argument PHI we can simply look through. */
3131 if (nargs == 1)
3132 return PHI_ARG_DEF (phi, 0);
3134 /* For two or more arguments try to pairwise skip non-aliasing code
3135 until we hit the phi argument definition that dominates the other one. */
3136 basic_block phi_bb = gimple_bb (phi);
3137 tree arg0, arg1;
3138 unsigned i;
3140 /* Find a candidate for the virtual operand which definition
3141 dominates those of all others. */
3142 /* First look if any of the args themselves satisfy this. */
3143 for (i = 0; i < nargs; ++i)
3145 arg0 = PHI_ARG_DEF (phi, i);
3146 if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3147 break;
3148 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3149 if (def_bb != phi_bb
3150 && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3151 break;
3152 arg0 = NULL_TREE;
3154 /* If not, look if we can reach such candidate by walking defs
3155 until we hit the immediate dominator. maybe_skip_until will
3156 do that for us. */
3157 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3159 /* Then check against the (to be) found candidate. */
3160 for (i = 0; i < nargs; ++i)
3162 arg1 = PHI_ARG_DEF (phi, i);
3163 if (arg1 == arg0)
3165 else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, limit, visited,
3166 abort_on_visited,
3167 /* Do not translate when walking over
3168 backedges. */
3169 dominated_by_p
3170 (CDI_DOMINATORS,
3171 gimple_bb (SSA_NAME_DEF_STMT (arg1)),
3172 phi_bb)
3173 ? NULL : translate, data))
3174 return NULL_TREE;
3177 return arg0;
3180 /* Based on the memory reference REF and its virtual use VUSE call
3181 WALKER for each virtual use that is equivalent to VUSE, including VUSE
3182 itself. That is, for each virtual use for which its defining statement
3183 does not clobber REF.
3185 WALKER is called with REF, the current virtual use and DATA. If
3186 WALKER returns non-NULL the walk stops and its result is returned.
3187 At the end of a non-successful walk NULL is returned.
3189 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3190 use which definition is a statement that may clobber REF and DATA.
3191 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3192 If TRANSLATE returns non-NULL the walk stops and its result is returned.
3193 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3194 to adjust REF and *DATA to make that valid.
3196 VALUEIZE if non-NULL is called with the next VUSE that is considered
3197 and return value is substituted for that. This can be used to
3198 implement optimistic value-numbering for example. Note that the
3199 VUSE argument is assumed to be valueized already.
3201 LIMIT specifies the number of alias queries we are allowed to do,
3202 the walk stops when it reaches zero and NULL is returned. LIMIT
3203 is decremented by the number of alias queries (plus adjustments
3204 done by the callbacks) upon return.
3206 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3208 void *
3209 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
3210 void *(*walker)(ao_ref *, tree, void *),
3211 void *(*translate)(ao_ref *, tree, void *, bool *),
3212 tree (*valueize)(tree),
3213 unsigned &limit, void *data)
3215 bitmap visited = NULL;
3216 void *res;
3217 bool translated = false;
3219 timevar_push (TV_ALIAS_STMT_WALK);
3223 gimple *def_stmt;
3225 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3226 res = (*walker) (ref, vuse, data);
3227 /* Abort walk. */
3228 if (res == (void *)-1)
3230 res = NULL;
3231 break;
3233 /* Lookup succeeded. */
3234 else if (res != NULL)
3235 break;
3237 if (valueize)
3239 vuse = valueize (vuse);
3240 if (!vuse)
3242 res = NULL;
3243 break;
3246 def_stmt = SSA_NAME_DEF_STMT (vuse);
3247 if (gimple_nop_p (def_stmt))
3248 break;
3249 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3250 vuse = get_continuation_for_phi (def_stmt, ref, limit,
3251 &visited, translated, translate, data);
3252 else
3254 if ((int)limit <= 0)
3256 res = NULL;
3257 break;
3259 --limit;
3260 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
3262 if (!translate)
3263 break;
3264 bool disambiguate_only = false;
3265 res = (*translate) (ref, vuse, data, &disambiguate_only);
3266 /* Failed lookup and translation. */
3267 if (res == (void *)-1)
3269 res = NULL;
3270 break;
3272 /* Lookup succeeded. */
3273 else if (res != NULL)
3274 break;
3275 /* Translation succeeded, continue walking. */
3276 translated = translated || !disambiguate_only;
3278 vuse = gimple_vuse (def_stmt);
3281 while (vuse);
3283 if (visited)
3284 BITMAP_FREE (visited);
3286 timevar_pop (TV_ALIAS_STMT_WALK);
3288 return res;
3292 /* Based on the memory reference REF call WALKER for each vdef which
3293 defining statement may clobber REF, starting with VDEF. If REF
3294 is NULL_TREE, each defining statement is visited.
3296 WALKER is called with REF, the current vdef and DATA. If WALKER
3297 returns true the walk is stopped, otherwise it continues.
3299 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
3300 The pointer may be NULL and then we do not track this information.
3302 At PHI nodes walk_aliased_vdefs forks into one walk for reach
3303 PHI argument (but only one walk continues on merge points), the
3304 return value is true if any of the walks was successful.
3306 The function returns the number of statements walked or -1 if
3307 LIMIT stmts were walked and the walk was aborted at this point.
3308 If LIMIT is zero the walk is not aborted. */
3310 static int
3311 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
3312 bool (*walker)(ao_ref *, tree, void *), void *data,
3313 bitmap *visited, unsigned int cnt,
3314 bool *function_entry_reached, unsigned limit)
3318 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
3320 if (*visited
3321 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
3322 return cnt;
3324 if (gimple_nop_p (def_stmt))
3326 if (function_entry_reached)
3327 *function_entry_reached = true;
3328 return cnt;
3330 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3332 unsigned i;
3333 if (!*visited)
3334 *visited = BITMAP_ALLOC (NULL);
3335 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
3337 int res = walk_aliased_vdefs_1 (ref,
3338 gimple_phi_arg_def (def_stmt, i),
3339 walker, data, visited, cnt,
3340 function_entry_reached, limit);
3341 if (res == -1)
3342 return -1;
3343 cnt = res;
3345 return cnt;
3348 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3349 cnt++;
3350 if (cnt == limit)
3351 return -1;
3352 if ((!ref
3353 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
3354 && (*walker) (ref, vdef, data))
3355 return cnt;
3357 vdef = gimple_vuse (def_stmt);
3359 while (1);
3363 walk_aliased_vdefs (ao_ref *ref, tree vdef,
3364 bool (*walker)(ao_ref *, tree, void *), void *data,
3365 bitmap *visited,
3366 bool *function_entry_reached, unsigned int limit)
3368 bitmap local_visited = NULL;
3369 int ret;
3371 timevar_push (TV_ALIAS_STMT_WALK);
3373 if (function_entry_reached)
3374 *function_entry_reached = false;
3376 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
3377 visited ? visited : &local_visited, 0,
3378 function_entry_reached, limit);
3379 if (local_visited)
3380 BITMAP_FREE (local_visited);
3382 timevar_pop (TV_ALIAS_STMT_WALK);
3384 return ret;