1 /* Alias analysis for trees.
2 Copyright (C) 2004-2023 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)
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/>. */
23 #include "coretypes.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
32 #include "tree-pretty-print.h"
34 #include "fold-const.h"
35 #include "langhooks.h"
39 #include "ipa-reference.h"
41 #include "ipa-modref-tree.h"
42 #include "ipa-modref.h"
43 #include "attr-fnspec.h"
46 #include "gimple-pretty-print.h"
47 #include "print-tree.h"
48 #include "tree-ssa-alias-compare.h"
50 #include "internal-fn.h"
52 /* Broad overview of how alias analysis on gimple works:
54 Statements clobbering or using memory are linked through the
55 virtual operand factored use-def chain. The virtual operand
56 is unique per function, its symbol is accessible via gimple_vop (cfun).
57 Virtual operands are used for efficiently walking memory statements
58 in the gimple IL and are useful for things like value-numbering as
59 a generation count for memory references.
61 SSA_NAME pointers may have associated points-to information
62 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
63 points-to information is (re-)computed by the TODO_rebuild_alias
64 pass manager todo. Points-to information is also used for more
65 precise tracking of call-clobbered and call-used variables and
66 related disambiguations.
68 This file contains functions for disambiguating memory references,
69 the so called alias-oracle and tools for walking of the gimple IL.
71 The main alias-oracle entry-points are
73 bool stmt_may_clobber_ref_p (gimple *, tree)
75 This function queries if a statement may invalidate (parts of)
76 the memory designated by the reference tree argument.
78 bool ref_maybe_used_by_stmt_p (gimple *, tree)
80 This function queries if a statement may need (parts of) the
81 memory designated by the reference tree argument.
83 There are variants of these functions that only handle the call
84 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
85 Note that these do not disambiguate against a possible call lhs.
87 bool refs_may_alias_p (tree, tree)
89 This function tries to disambiguate two reference trees.
91 bool ptr_deref_may_alias_global_p (tree, bool)
93 This function queries if dereferencing a pointer variable may
94 alias global memory. If bool argument is true, global memory
95 is considered to also include function local memory that escaped.
97 More low-level disambiguators are available and documented in
98 this file. Low-level disambiguators dealing with points-to
99 information are in tree-ssa-structalias.cc. */
101 static int nonoverlapping_refs_since_match_p (tree
, tree
, tree
, tree
, bool);
102 static bool nonoverlapping_component_refs_p (const_tree
, const_tree
);
104 /* Query statistics for the different low-level disambiguators.
105 A high-level query may trigger multiple of them. */
108 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias
;
109 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias
;
110 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias
;
111 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias
;
112 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias
;
113 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias
;
114 unsigned HOST_WIDE_INT aliasing_component_refs_p_may_alias
;
115 unsigned HOST_WIDE_INT aliasing_component_refs_p_no_alias
;
116 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_may_alias
;
117 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_no_alias
;
118 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_may_alias
;
119 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_must_overlap
;
120 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_no_alias
;
121 unsigned HOST_WIDE_INT stmt_kills_ref_p_no
;
122 unsigned HOST_WIDE_INT stmt_kills_ref_p_yes
;
123 unsigned HOST_WIDE_INT modref_use_may_alias
;
124 unsigned HOST_WIDE_INT modref_use_no_alias
;
125 unsigned HOST_WIDE_INT modref_clobber_may_alias
;
126 unsigned HOST_WIDE_INT modref_clobber_no_alias
;
127 unsigned HOST_WIDE_INT modref_kill_no
;
128 unsigned HOST_WIDE_INT modref_kill_yes
;
129 unsigned HOST_WIDE_INT modref_tests
;
130 unsigned HOST_WIDE_INT modref_baseptr_tests
;
134 dump_alias_stats (FILE *s
)
136 fprintf (s
, "\nAlias oracle query stats:\n");
137 fprintf (s
, " refs_may_alias_p: "
138 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
139 HOST_WIDE_INT_PRINT_DEC
" queries\n",
140 alias_stats
.refs_may_alias_p_no_alias
,
141 alias_stats
.refs_may_alias_p_no_alias
142 + alias_stats
.refs_may_alias_p_may_alias
);
143 fprintf (s
, " ref_maybe_used_by_call_p: "
144 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
145 HOST_WIDE_INT_PRINT_DEC
" queries\n",
146 alias_stats
.ref_maybe_used_by_call_p_no_alias
,
147 alias_stats
.refs_may_alias_p_no_alias
148 + alias_stats
.ref_maybe_used_by_call_p_may_alias
);
149 fprintf (s
, " call_may_clobber_ref_p: "
150 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
151 HOST_WIDE_INT_PRINT_DEC
" queries\n",
152 alias_stats
.call_may_clobber_ref_p_no_alias
,
153 alias_stats
.call_may_clobber_ref_p_no_alias
154 + alias_stats
.call_may_clobber_ref_p_may_alias
);
155 fprintf (s
, " stmt_kills_ref_p: "
156 HOST_WIDE_INT_PRINT_DEC
" kills, "
157 HOST_WIDE_INT_PRINT_DEC
" queries\n",
158 alias_stats
.stmt_kills_ref_p_yes
+ alias_stats
.modref_kill_yes
,
159 alias_stats
.stmt_kills_ref_p_yes
+ alias_stats
.modref_kill_yes
160 + alias_stats
.stmt_kills_ref_p_no
+ alias_stats
.modref_kill_no
);
161 fprintf (s
, " nonoverlapping_component_refs_p: "
162 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
163 HOST_WIDE_INT_PRINT_DEC
" queries\n",
164 alias_stats
.nonoverlapping_component_refs_p_no_alias
,
165 alias_stats
.nonoverlapping_component_refs_p_no_alias
166 + alias_stats
.nonoverlapping_component_refs_p_may_alias
);
167 fprintf (s
, " nonoverlapping_refs_since_match_p: "
168 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
169 HOST_WIDE_INT_PRINT_DEC
" must overlaps, "
170 HOST_WIDE_INT_PRINT_DEC
" queries\n",
171 alias_stats
.nonoverlapping_refs_since_match_p_no_alias
,
172 alias_stats
.nonoverlapping_refs_since_match_p_must_overlap
,
173 alias_stats
.nonoverlapping_refs_since_match_p_no_alias
174 + alias_stats
.nonoverlapping_refs_since_match_p_may_alias
175 + alias_stats
.nonoverlapping_refs_since_match_p_must_overlap
);
176 fprintf (s
, " aliasing_component_refs_p: "
177 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
178 HOST_WIDE_INT_PRINT_DEC
" queries\n",
179 alias_stats
.aliasing_component_refs_p_no_alias
,
180 alias_stats
.aliasing_component_refs_p_no_alias
181 + alias_stats
.aliasing_component_refs_p_may_alias
);
182 dump_alias_stats_in_alias_c (s
);
183 fprintf (s
, "\nModref stats:\n");
184 fprintf (s
, " modref kill: "
185 HOST_WIDE_INT_PRINT_DEC
" kills, "
186 HOST_WIDE_INT_PRINT_DEC
" queries\n",
187 alias_stats
.modref_kill_yes
,
188 alias_stats
.modref_kill_yes
189 + alias_stats
.modref_kill_no
);
190 fprintf (s
, " modref use: "
191 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
192 HOST_WIDE_INT_PRINT_DEC
" queries\n",
193 alias_stats
.modref_use_no_alias
,
194 alias_stats
.modref_use_no_alias
195 + alias_stats
.modref_use_may_alias
);
196 fprintf (s
, " modref clobber: "
197 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
198 HOST_WIDE_INT_PRINT_DEC
" queries\n"
199 " " HOST_WIDE_INT_PRINT_DEC
" tbaa queries (%f per modref query)\n"
200 " " HOST_WIDE_INT_PRINT_DEC
" base compares (%f per modref query)\n",
201 alias_stats
.modref_clobber_no_alias
,
202 alias_stats
.modref_clobber_no_alias
203 + alias_stats
.modref_clobber_may_alias
,
204 alias_stats
.modref_tests
,
205 ((double)alias_stats
.modref_tests
)
206 / (alias_stats
.modref_clobber_no_alias
207 + alias_stats
.modref_clobber_may_alias
),
208 alias_stats
.modref_baseptr_tests
,
209 ((double)alias_stats
.modref_baseptr_tests
)
210 / (alias_stats
.modref_clobber_no_alias
211 + alias_stats
.modref_clobber_may_alias
));
215 /* Return true, if dereferencing PTR may alias with a global variable.
216 When ESCAPED_LOCAL_P is true escaped local memory is also considered
220 ptr_deref_may_alias_global_p (tree ptr
, bool escaped_local_p
)
222 struct ptr_info_def
*pi
;
224 /* If we end up with a pointer constant here that may point
226 if (TREE_CODE (ptr
) != SSA_NAME
)
229 pi
= SSA_NAME_PTR_INFO (ptr
);
231 /* If we do not have points-to information for this variable,
236 /* ??? This does not use TBAA to prune globals ptr may not access. */
237 return pt_solution_includes_global (&pi
->pt
, escaped_local_p
);
240 /* Return true if dereferencing PTR may alias DECL.
241 The caller is responsible for applying TBAA to see if PTR
242 may access DECL at all. */
245 ptr_deref_may_alias_decl_p (tree ptr
, tree decl
)
247 struct ptr_info_def
*pi
;
249 /* Conversions are irrelevant for points-to information and
250 data-dependence analysis can feed us those. */
253 /* Anything we do not explicilty handle aliases. */
254 if ((TREE_CODE (ptr
) != SSA_NAME
255 && TREE_CODE (ptr
) != ADDR_EXPR
256 && TREE_CODE (ptr
) != POINTER_PLUS_EXPR
)
257 || !POINTER_TYPE_P (TREE_TYPE (ptr
))
259 && TREE_CODE (decl
) != PARM_DECL
260 && TREE_CODE (decl
) != RESULT_DECL
))
263 /* Disregard pointer offsetting. */
264 if (TREE_CODE (ptr
) == POINTER_PLUS_EXPR
)
268 ptr
= TREE_OPERAND (ptr
, 0);
270 while (TREE_CODE (ptr
) == POINTER_PLUS_EXPR
);
271 return ptr_deref_may_alias_decl_p (ptr
, decl
);
274 /* ADDR_EXPR pointers either just offset another pointer or directly
275 specify the pointed-to set. */
276 if (TREE_CODE (ptr
) == ADDR_EXPR
)
278 tree base
= get_base_address (TREE_OPERAND (ptr
, 0));
280 && (TREE_CODE (base
) == MEM_REF
281 || TREE_CODE (base
) == TARGET_MEM_REF
))
282 ptr
= TREE_OPERAND (base
, 0);
285 return compare_base_decls (base
, decl
) != 0;
287 && CONSTANT_CLASS_P (base
))
293 /* Non-aliased variables cannot be pointed to. */
294 if (!may_be_aliased (decl
))
297 /* If we do not have useful points-to information for this pointer
298 we cannot disambiguate anything else. */
299 pi
= SSA_NAME_PTR_INFO (ptr
);
303 return pt_solution_includes (&pi
->pt
, decl
);
306 /* Return true if dereferenced PTR1 and PTR2 may alias.
307 The caller is responsible for applying TBAA to see if accesses
308 through PTR1 and PTR2 may conflict at all. */
311 ptr_derefs_may_alias_p (tree ptr1
, tree ptr2
)
313 struct ptr_info_def
*pi1
, *pi2
;
315 /* Conversions are irrelevant for points-to information and
316 data-dependence analysis can feed us those. */
320 /* Disregard pointer offsetting. */
321 if (TREE_CODE (ptr1
) == POINTER_PLUS_EXPR
)
325 ptr1
= TREE_OPERAND (ptr1
, 0);
327 while (TREE_CODE (ptr1
) == POINTER_PLUS_EXPR
);
328 return ptr_derefs_may_alias_p (ptr1
, ptr2
);
330 if (TREE_CODE (ptr2
) == POINTER_PLUS_EXPR
)
334 ptr2
= TREE_OPERAND (ptr2
, 0);
336 while (TREE_CODE (ptr2
) == POINTER_PLUS_EXPR
);
337 return ptr_derefs_may_alias_p (ptr1
, ptr2
);
340 /* ADDR_EXPR pointers either just offset another pointer or directly
341 specify the pointed-to set. */
342 if (TREE_CODE (ptr1
) == ADDR_EXPR
)
344 tree base
= get_base_address (TREE_OPERAND (ptr1
, 0));
346 && (TREE_CODE (base
) == MEM_REF
347 || TREE_CODE (base
) == TARGET_MEM_REF
))
348 return ptr_derefs_may_alias_p (TREE_OPERAND (base
, 0), ptr2
);
351 return ptr_deref_may_alias_decl_p (ptr2
, base
);
352 /* Try ptr2 when ptr1 points to a constant. */
354 && !CONSTANT_CLASS_P (base
))
357 if (TREE_CODE (ptr2
) == ADDR_EXPR
)
359 tree base
= get_base_address (TREE_OPERAND (ptr2
, 0));
361 && (TREE_CODE (base
) == MEM_REF
362 || TREE_CODE (base
) == TARGET_MEM_REF
))
363 return ptr_derefs_may_alias_p (ptr1
, TREE_OPERAND (base
, 0));
366 return ptr_deref_may_alias_decl_p (ptr1
, base
);
371 /* From here we require SSA name pointers. Anything else aliases. */
372 if (TREE_CODE (ptr1
) != SSA_NAME
373 || TREE_CODE (ptr2
) != SSA_NAME
374 || !POINTER_TYPE_P (TREE_TYPE (ptr1
))
375 || !POINTER_TYPE_P (TREE_TYPE (ptr2
)))
378 /* We may end up with two empty points-to solutions for two same pointers.
379 In this case we still want to say both pointers alias, so shortcut
384 /* If we do not have useful points-to information for either pointer
385 we cannot disambiguate anything else. */
386 pi1
= SSA_NAME_PTR_INFO (ptr1
);
387 pi2
= SSA_NAME_PTR_INFO (ptr2
);
391 /* ??? This does not use TBAA to prune decls from the intersection
392 that not both pointers may access. */
393 return pt_solutions_intersect (&pi1
->pt
, &pi2
->pt
);
396 /* Return true if dereferencing PTR may alias *REF.
397 The caller is responsible for applying TBAA to see if PTR
398 may access *REF at all. */
401 ptr_deref_may_alias_ref_p_1 (tree ptr
, ao_ref
*ref
)
403 tree base
= ao_ref_base (ref
);
405 if (TREE_CODE (base
) == MEM_REF
406 || TREE_CODE (base
) == TARGET_MEM_REF
)
407 return ptr_derefs_may_alias_p (ptr
, TREE_OPERAND (base
, 0));
408 else if (DECL_P (base
))
409 return ptr_deref_may_alias_decl_p (ptr
, base
);
414 /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
417 ptrs_compare_unequal (tree ptr1
, tree ptr2
)
419 /* First resolve the pointers down to a SSA name pointer base or
420 a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
421 not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
422 or STRING_CSTs which needs points-to adjustments to track them
423 in the points-to sets. */
424 tree obj1
= NULL_TREE
;
425 tree obj2
= NULL_TREE
;
426 if (TREE_CODE (ptr1
) == ADDR_EXPR
)
428 tree tem
= get_base_address (TREE_OPERAND (ptr1
, 0));
432 || TREE_CODE (tem
) == PARM_DECL
433 || TREE_CODE (tem
) == RESULT_DECL
)
435 else if (TREE_CODE (tem
) == MEM_REF
)
436 ptr1
= TREE_OPERAND (tem
, 0);
438 if (TREE_CODE (ptr2
) == ADDR_EXPR
)
440 tree tem
= get_base_address (TREE_OPERAND (ptr2
, 0));
444 || TREE_CODE (tem
) == PARM_DECL
445 || TREE_CODE (tem
) == RESULT_DECL
)
447 else if (TREE_CODE (tem
) == MEM_REF
)
448 ptr2
= TREE_OPERAND (tem
, 0);
451 /* Canonicalize ptr vs. object. */
452 if (TREE_CODE (ptr1
) == SSA_NAME
&& obj2
)
454 std::swap (ptr1
, ptr2
);
455 std::swap (obj1
, obj2
);
459 /* Other code handles this correctly, no need to duplicate it here. */;
460 else if (obj1
&& TREE_CODE (ptr2
) == SSA_NAME
)
462 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (ptr2
);
463 /* We may not use restrict to optimize pointer comparisons.
464 See PR71062. So we have to assume that restrict-pointed-to
465 may be in fact obj1. */
467 || pi
->pt
.vars_contains_restrict
468 || pi
->pt
.vars_contains_interposable
)
471 && (TREE_STATIC (obj1
) || DECL_EXTERNAL (obj1
)))
473 varpool_node
*node
= varpool_node::get (obj1
);
474 /* If obj1 may bind to NULL give up (see below). */
476 || ! node
->nonzero_address ()
477 || ! decl_binds_to_current_def_p (obj1
))
480 return !pt_solution_includes (&pi
->pt
, obj1
);
483 /* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
484 but those require pt.null to be conservatively correct. */
489 /* Returns whether reference REF to BASE may refer to global memory.
490 When ESCAPED_LOCAL_P is true escaped local memory is also considered
494 ref_may_alias_global_p_1 (tree base
, bool escaped_local_p
)
497 return (is_global_var (base
)
499 && pt_solution_includes (&cfun
->gimple_df
->escaped
, base
)));
500 else if (TREE_CODE (base
) == MEM_REF
501 || TREE_CODE (base
) == TARGET_MEM_REF
)
502 return ptr_deref_may_alias_global_p (TREE_OPERAND (base
, 0),
508 ref_may_alias_global_p (ao_ref
*ref
, bool escaped_local_p
)
510 tree base
= ao_ref_base (ref
);
511 return ref_may_alias_global_p_1 (base
, escaped_local_p
);
515 ref_may_alias_global_p (tree ref
, bool escaped_local_p
)
517 tree base
= get_base_address (ref
);
518 return ref_may_alias_global_p_1 (base
, escaped_local_p
);
521 /* Return true whether STMT may clobber global memory.
522 When ESCAPED_LOCAL_P is true escaped local memory is also considered
526 stmt_may_clobber_global_p (gimple
*stmt
, bool escaped_local_p
)
530 if (!gimple_vdef (stmt
))
533 /* ??? We can ask the oracle whether an artificial pointer
534 dereference with a pointer with points-to information covering
535 all global memory (what about non-address taken memory?) maybe
536 clobbered by this call. As there is at the moment no convenient
537 way of doing that without generating garbage do some manual
539 ??? We could make a NULL ao_ref argument to the various
540 predicates special, meaning any global memory. */
542 switch (gimple_code (stmt
))
545 lhs
= gimple_assign_lhs (stmt
);
546 return (TREE_CODE (lhs
) != SSA_NAME
547 && ref_may_alias_global_p (lhs
, escaped_local_p
));
556 /* Dump alias information on FILE. */
559 dump_alias_info (FILE *file
)
564 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
567 fprintf (file
, "\n\nAlias information for %s\n\n", funcname
);
569 fprintf (file
, "Aliased symbols\n\n");
571 FOR_EACH_LOCAL_DECL (cfun
, i
, var
)
573 if (may_be_aliased (var
))
574 dump_variable (file
, var
);
577 fprintf (file
, "\nCall clobber information\n");
579 fprintf (file
, "\nESCAPED");
580 dump_points_to_solution (file
, &cfun
->gimple_df
->escaped
);
582 fprintf (file
, "\n\nFlow-insensitive points-to information\n\n");
584 FOR_EACH_SSA_NAME (i
, ptr
, cfun
)
586 struct ptr_info_def
*pi
;
588 if (!POINTER_TYPE_P (TREE_TYPE (ptr
))
589 || SSA_NAME_IN_FREE_LIST (ptr
))
592 pi
= SSA_NAME_PTR_INFO (ptr
);
594 dump_points_to_info_for (file
, ptr
);
597 fprintf (file
, "\n");
601 /* Dump alias information on stderr. */
604 debug_alias_info (void)
606 dump_alias_info (stderr
);
610 /* Dump the points-to set *PT into FILE. */
613 dump_points_to_solution (FILE *file
, struct pt_solution
*pt
)
616 fprintf (file
, ", points-to anything");
619 fprintf (file
, ", points-to non-local");
622 fprintf (file
, ", points-to escaped");
625 fprintf (file
, ", points-to unit escaped");
628 fprintf (file
, ", points-to NULL");
632 fprintf (file
, ", points-to vars: ");
633 dump_decl_set (file
, pt
->vars
);
634 if (pt
->vars_contains_nonlocal
635 || pt
->vars_contains_escaped
636 || pt
->vars_contains_escaped_heap
637 || pt
->vars_contains_restrict
)
639 const char *comma
= "";
640 fprintf (file
, " (");
641 if (pt
->vars_contains_nonlocal
)
643 fprintf (file
, "nonlocal");
646 if (pt
->vars_contains_escaped
)
648 fprintf (file
, "%sescaped", comma
);
651 if (pt
->vars_contains_escaped_heap
)
653 fprintf (file
, "%sescaped heap", comma
);
656 if (pt
->vars_contains_restrict
)
658 fprintf (file
, "%srestrict", comma
);
661 if (pt
->vars_contains_interposable
)
662 fprintf (file
, "%sinterposable", comma
);
669 /* Unified dump function for pt_solution. */
672 debug (pt_solution
&ref
)
674 dump_points_to_solution (stderr
, &ref
);
678 debug (pt_solution
*ptr
)
683 fprintf (stderr
, "<nil>\n");
687 /* Dump points-to information for SSA_NAME PTR into FILE. */
690 dump_points_to_info_for (FILE *file
, tree ptr
)
692 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (ptr
);
694 print_generic_expr (file
, ptr
, dump_flags
);
697 dump_points_to_solution (file
, &pi
->pt
);
699 fprintf (file
, ", points-to anything");
701 fprintf (file
, "\n");
705 /* Dump points-to information for VAR into stderr. */
708 debug_points_to_info_for (tree var
)
710 dump_points_to_info_for (stderr
, var
);
714 /* Initializes the alias-oracle reference representation *R from REF. */
717 ao_ref_init (ao_ref
*r
, tree ref
)
724 r
->ref_alias_set
= -1;
725 r
->base_alias_set
= -1;
726 r
->volatile_p
= ref
? TREE_THIS_VOLATILE (ref
) : false;
729 /* Returns the base object of the memory reference *REF. */
732 ao_ref_base (ao_ref
*ref
)
738 ref
->base
= get_ref_base_and_extent (ref
->ref
, &ref
->offset
, &ref
->size
,
739 &ref
->max_size
, &reverse
);
743 /* Returns the base object alias set of the memory reference *REF. */
746 ao_ref_base_alias_set (ao_ref
*ref
)
749 if (ref
->base_alias_set
!= -1)
750 return ref
->base_alias_set
;
754 if (TREE_CODE (base_ref
) == WITH_SIZE_EXPR
)
755 base_ref
= TREE_OPERAND (base_ref
, 0);
756 while (handled_component_p (base_ref
))
757 base_ref
= TREE_OPERAND (base_ref
, 0);
758 ref
->base_alias_set
= get_alias_set (base_ref
);
759 return ref
->base_alias_set
;
762 /* Returns the reference alias set of the memory reference *REF. */
765 ao_ref_alias_set (ao_ref
*ref
)
767 if (ref
->ref_alias_set
!= -1)
768 return ref
->ref_alias_set
;
771 ref
->ref_alias_set
= get_alias_set (ref
->ref
);
772 return ref
->ref_alias_set
;
775 /* Returns a type satisfying
776 get_deref_alias_set (type) == ao_ref_base_alias_set (REF). */
779 ao_ref_base_alias_ptr_type (ao_ref
*ref
)
786 if (TREE_CODE (base_ref
) == WITH_SIZE_EXPR
)
787 base_ref
= TREE_OPERAND (base_ref
, 0);
788 while (handled_component_p (base_ref
))
789 base_ref
= TREE_OPERAND (base_ref
, 0);
790 tree ret
= reference_alias_ptr_type (base_ref
);
794 /* Returns a type satisfying
795 get_deref_alias_set (type) == ao_ref_alias_set (REF). */
798 ao_ref_alias_ptr_type (ao_ref
*ref
)
802 tree ret
= reference_alias_ptr_type (ref
->ref
);
806 /* Return the alignment of the access *REF and store it in the *ALIGN
807 and *BITPOS pairs. Returns false if no alignment could be determined.
808 See get_object_alignment_2 for details. */
811 ao_ref_alignment (ao_ref
*ref
, unsigned int *align
,
812 unsigned HOST_WIDE_INT
*bitpos
)
815 return get_object_alignment_1 (ref
->ref
, align
, bitpos
);
817 /* When we just have ref->base we cannot use get_object_alignment since
818 that will eventually use the type of the appearant access while for
819 example ao_ref_init_from_ptr_and_range is not careful to adjust that. */
820 *align
= BITS_PER_UNIT
;
821 HOST_WIDE_INT offset
;
822 if (!ref
->offset
.is_constant (&offset
)
823 || !get_object_alignment_2 (ref
->base
, align
, bitpos
, true))
825 *bitpos
+= (unsigned HOST_WIDE_INT
)offset
* BITS_PER_UNIT
;
826 *bitpos
= *bitpos
& (*align
- 1);
830 /* Init an alias-oracle reference representation from a gimple pointer
831 PTR a range specified by OFFSET, SIZE and MAX_SIZE under the assumption
832 that RANGE_KNOWN is set.
834 The access is assumed to be only to or after of the pointer target adjusted
835 by the offset, not before it (even in the case RANGE_KNOWN is false). */
838 ao_ref_init_from_ptr_and_range (ao_ref
*ref
, tree ptr
,
844 poly_int64 t
, extra_offset
= 0;
846 ref
->ref
= NULL_TREE
;
847 if (TREE_CODE (ptr
) == SSA_NAME
)
849 gimple
*stmt
= SSA_NAME_DEF_STMT (ptr
);
850 if (gimple_assign_single_p (stmt
)
851 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
852 ptr
= gimple_assign_rhs1 (stmt
);
853 else if (is_gimple_assign (stmt
)
854 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
855 && ptrdiff_tree_p (gimple_assign_rhs2 (stmt
), &extra_offset
))
857 ptr
= gimple_assign_rhs1 (stmt
);
858 extra_offset
*= BITS_PER_UNIT
;
862 if (TREE_CODE (ptr
) == ADDR_EXPR
)
864 ref
->base
= get_addr_base_and_unit_offset (TREE_OPERAND (ptr
, 0), &t
);
866 ref
->offset
= BITS_PER_UNIT
* t
;
871 ref
->base
= get_base_address (TREE_OPERAND (ptr
, 0));
876 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr
)));
877 ref
->base
= build2 (MEM_REF
, char_type_node
,
878 ptr
, null_pointer_node
);
881 ref
->offset
+= extra_offset
+ offset
;
884 ref
->max_size
= max_size
;
888 ref
->max_size
= ref
->size
= -1;
889 ref
->ref_alias_set
= 0;
890 ref
->base_alias_set
= 0;
891 ref
->volatile_p
= false;
894 /* Init an alias-oracle reference representation from a gimple pointer
895 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
896 size is assumed to be unknown. The access is assumed to be only
897 to or after of the pointer target, not before it. */
900 ao_ref_init_from_ptr_and_size (ao_ref
*ref
, tree ptr
, tree size
)
904 && poly_int_tree_p (size
, &size_hwi
)
905 && coeffs_in_range_p (size_hwi
, 0, HOST_WIDE_INT_MAX
/ BITS_PER_UNIT
))
907 size_hwi
= size_hwi
* BITS_PER_UNIT
;
908 ao_ref_init_from_ptr_and_range (ref
, ptr
, true, 0, size_hwi
, size_hwi
);
911 ao_ref_init_from_ptr_and_range (ref
, ptr
, false, 0, -1, -1);
914 /* S1 and S2 are TYPE_SIZE or DECL_SIZE. Compare them:
917 Return 0 if equal or incomparable. */
920 compare_sizes (tree s1
, tree s2
)
928 if (!poly_int_tree_p (s1
, &size1
) || !poly_int_tree_p (s2
, &size2
))
930 if (known_lt (size1
, size2
))
932 if (known_lt (size2
, size1
))
937 /* Compare TYPE1 and TYPE2 by its size.
938 Return -1 if size of TYPE1 < size of TYPE2
939 Return 1 if size of TYPE1 > size of TYPE2
940 Return 0 if types are of equal sizes or we can not compare them. */
943 compare_type_sizes (tree type1
, tree type2
)
945 /* Be conservative for arrays and vectors. We want to support partial
946 overlap on int[3] and int[3] as tested in gcc.dg/torture/alias-2.c. */
947 while (TREE_CODE (type1
) == ARRAY_TYPE
948 || VECTOR_TYPE_P (type1
))
949 type1
= TREE_TYPE (type1
);
950 while (TREE_CODE (type2
) == ARRAY_TYPE
951 || VECTOR_TYPE_P (type2
))
952 type2
= TREE_TYPE (type2
);
953 return compare_sizes (TYPE_SIZE (type1
), TYPE_SIZE (type2
));
956 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
957 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
961 same_type_for_tbaa (tree type1
, tree type2
)
963 type1
= TYPE_MAIN_VARIANT (type1
);
964 type2
= TYPE_MAIN_VARIANT (type2
);
966 /* Handle the most common case first. */
970 /* If we would have to do structural comparison bail out. */
971 if (TYPE_STRUCTURAL_EQUALITY_P (type1
)
972 || TYPE_STRUCTURAL_EQUALITY_P (type2
))
975 /* Compare the canonical types. */
976 if (TYPE_CANONICAL (type1
) == TYPE_CANONICAL (type2
))
979 /* ??? Array types are not properly unified in all cases as we have
980 spurious changes in the index types for example. Removing this
981 causes all sorts of problems with the Fortran frontend. */
982 if (TREE_CODE (type1
) == ARRAY_TYPE
983 && TREE_CODE (type2
) == ARRAY_TYPE
)
986 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
987 object of one of its constrained subtypes, e.g. when a function with an
988 unconstrained parameter passed by reference is called on an object and
989 inlined. But, even in the case of a fixed size, type and subtypes are
990 not equivalent enough as to share the same TYPE_CANONICAL, since this
991 would mean that conversions between them are useless, whereas they are
992 not (e.g. type and subtypes can have different modes). So, in the end,
993 they are only guaranteed to have the same alias set. */
994 alias_set_type set1
= get_alias_set (type1
);
995 alias_set_type set2
= get_alias_set (type2
);
999 /* Pointers to void are considered compatible with all other pointers,
1000 so for two pointers see what the alias set resolution thinks. */
1001 if (POINTER_TYPE_P (type1
)
1002 && POINTER_TYPE_P (type2
)
1003 && alias_sets_conflict_p (set1
, set2
))
1006 /* The types are known to be not equal. */
1010 /* Return true if TYPE is a composite type (i.e. we may apply one of handled
1011 components on it). */
1014 type_has_components_p (tree type
)
1016 return AGGREGATE_TYPE_P (type
) || VECTOR_TYPE_P (type
)
1017 || TREE_CODE (type
) == COMPLEX_TYPE
;
1020 /* MATCH1 and MATCH2 which are part of access path of REF1 and REF2
1021 respectively are either pointing to same address or are completely
1022 disjoint. If PARTIAL_OVERLAP is true, assume that outermost arrays may
1023 just partly overlap.
1025 Try to disambiguate using the access path starting from the match
1026 and return false if there is no conflict.
1028 Helper for aliasing_component_refs_p. */
1031 aliasing_matching_component_refs_p (tree match1
, tree ref1
,
1032 poly_int64 offset1
, poly_int64 max_size1
,
1033 tree match2
, tree ref2
,
1034 poly_int64 offset2
, poly_int64 max_size2
,
1035 bool partial_overlap
)
1037 poly_int64 offadj
, sztmp
, msztmp
;
1040 if (!partial_overlap
)
1042 get_ref_base_and_extent (match2
, &offadj
, &sztmp
, &msztmp
, &reverse
);
1044 get_ref_base_and_extent (match1
, &offadj
, &sztmp
, &msztmp
, &reverse
);
1046 if (!ranges_maybe_overlap_p (offset1
, max_size1
, offset2
, max_size2
))
1048 ++alias_stats
.aliasing_component_refs_p_no_alias
;
1053 int cmp
= nonoverlapping_refs_since_match_p (match1
, ref1
, match2
, ref2
,
1056 || (cmp
== -1 && nonoverlapping_component_refs_p (ref1
, ref2
)))
1058 ++alias_stats
.aliasing_component_refs_p_no_alias
;
1061 ++alias_stats
.aliasing_component_refs_p_may_alias
;
1065 /* Return true if REF is reference to zero sized trailing array. I.e.
1066 struct foo {int bar; int array[0];} *fooptr;
1070 component_ref_to_zero_sized_trailing_array_p (tree ref
)
1072 return (TREE_CODE (ref
) == COMPONENT_REF
1073 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref
, 1))) == ARRAY_TYPE
1074 && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref
, 1)))
1075 || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref
, 1)))))
1076 && array_ref_flexible_size_p (ref
));
1079 /* Worker for aliasing_component_refs_p. Most parameters match parameters of
1080 aliasing_component_refs_p.
1082 Walk access path REF2 and try to find type matching TYPE1
1083 (which is a start of possibly aliasing access path REF1).
1084 If match is found, try to disambiguate.
1086 Return 0 for sucessful disambiguation.
1087 Return 1 if match was found but disambiguation failed
1088 Return -1 if there is no match.
1089 In this case MAYBE_MATCH is set to 0 if there is no type matching TYPE1
1090 in access patch REF2 and -1 if we are not sure. */
1093 aliasing_component_refs_walk (tree ref1
, tree type1
, tree base1
,
1094 poly_int64 offset1
, poly_int64 max_size1
,
1095 tree end_struct_ref1
,
1096 tree ref2
, tree base2
,
1097 poly_int64 offset2
, poly_int64 max_size2
,
1105 /* We walk from inner type to the outer types. If type we see is
1106 already too large to be part of type1, terminate the search. */
1107 int cmp
= compare_type_sizes (type1
, TREE_TYPE (ref
));
1110 && (!end_struct_ref1
1111 || compare_type_sizes (TREE_TYPE (end_struct_ref1
),
1112 TREE_TYPE (ref
)) < 0))
1114 /* If types may be of same size, see if we can decide about their
1118 same_p
= same_type_for_tbaa (TREE_TYPE (ref
), type1
);
1121 /* In case we can't decide whether types are same try to
1122 continue looking for the exact match.
1123 Remember however that we possibly saw a match
1124 to bypass the access path continuations tests we do later. */
1126 *maybe_match
= true;
1128 if (!handled_component_p (ref
))
1130 ref
= TREE_OPERAND (ref
, 0);
1134 bool partial_overlap
= false;
1136 /* We assume that arrays can overlap by multiple of their elements
1137 size as tested in gcc.dg/torture/alias-2.c.
1138 This partial overlap happen only when both arrays are bases of
1139 the access and not contained within another component ref.
1140 To be safe we also assume partial overlap for VLAs. */
1141 if (TREE_CODE (TREE_TYPE (base1
)) == ARRAY_TYPE
1142 && (!TYPE_SIZE (TREE_TYPE (base1
))
1143 || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1
))) != INTEGER_CST
1146 /* Setting maybe_match to true triggers
1147 nonoverlapping_component_refs_p test later that still may do
1148 useful disambiguation. */
1149 *maybe_match
= true;
1150 partial_overlap
= true;
1152 return aliasing_matching_component_refs_p (base1
, ref1
,
1161 /* Consider access path1 base1....ref1 and access path2 base2...ref2.
1162 Return true if they can be composed to single access path
1163 base1...ref1...base2...ref2.
1165 REF_TYPE1 if type of REF1. END_STRUCT_PAST_END1 is true if there is
1166 a trailing array access after REF1 in the non-TBAA part of the access.
1167 REF1_ALIAS_SET is the alias set of REF1.
1169 BASE_TYPE2 is type of base2. END_STRUCT_REF2 is non-NULL if there is
1170 a trailing array access in the TBAA part of access path2.
1171 BASE2_ALIAS_SET is the alias set of base2. */
1174 access_path_may_continue_p (tree ref_type1
, bool end_struct_past_end1
,
1175 alias_set_type ref1_alias_set
,
1176 tree base_type2
, tree end_struct_ref2
,
1177 alias_set_type base2_alias_set
)
1179 /* Access path can not continue past types with no components. */
1180 if (!type_has_components_p (ref_type1
))
1183 /* If first access path ends by too small type to hold base of
1184 the second access path, typically paths can not continue.
1186 Punt if end_struct_past_end1 is true. We want to support arbitrary
1187 type puning past first COMPONENT_REF to union because redundant store
1188 elimination depends on this, see PR92152. For this reason we can not
1189 check size of the reference because types may partially overlap. */
1190 if (!end_struct_past_end1
)
1192 if (compare_type_sizes (ref_type1
, base_type2
) < 0)
1194 /* If the path2 contains trailing array access we can strenghten the check
1195 to verify that also the size of element of the trailing array fits.
1196 In fact we could check for offset + type_size, but we do not track
1197 offsets and this is quite side case. */
1199 && compare_type_sizes (ref_type1
, TREE_TYPE (end_struct_ref2
)) < 0)
1202 return (base2_alias_set
== ref1_alias_set
1203 || alias_set_subset_of (base2_alias_set
, ref1_alias_set
));
1206 /* Determine if the two component references REF1 and REF2 which are
1207 based on access types TYPE1 and TYPE2 and of which at least one is based
1208 on an indirect reference may alias.
1209 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
1210 are the respective alias sets. */
1213 aliasing_component_refs_p (tree ref1
,
1214 alias_set_type ref1_alias_set
,
1215 alias_set_type base1_alias_set
,
1216 poly_int64 offset1
, poly_int64 max_size1
,
1218 alias_set_type ref2_alias_set
,
1219 alias_set_type base2_alias_set
,
1220 poly_int64 offset2
, poly_int64 max_size2
)
1222 /* If one reference is a component references through pointers try to find a
1223 common base and apply offset based disambiguation. This handles
1225 struct A { int i; int j; } *q;
1226 struct B { struct A a; int k; } *p;
1227 disambiguating q->i and p->a.j. */
1230 bool maybe_match
= false;
1231 tree end_struct_ref1
= NULL
, end_struct_ref2
= NULL
;
1232 bool end_struct_past_end1
= false;
1233 bool end_struct_past_end2
= false;
1235 /* Choose bases and base types to search for.
1236 The access path is as follows:
1237 base....end_of_tbaa_ref...actual_ref
1238 At one place in the access path may be a reference to zero sized or
1241 We generally discard the segment after end_of_tbaa_ref however
1242 we need to be careful in case it contains zero sized or trailing array.
1243 These may happen after reference to union and in this case we need to
1244 not disambiguate type puning scenarios.
1247 base1 to point to base
1249 ref1 to point to end_of_tbaa_ref
1251 end_struct_ref1 to point the trailing reference (if it exists
1252 in range base....end_of_tbaa_ref
1254 end_struct_past_end1 is true if this trailing reference occurs in
1255 end_of_tbaa_ref...actual_ref. */
1257 while (handled_component_p (base1
))
1259 /* Generally access paths are monotous in the size of object. The
1260 exception are trailing arrays of structures. I.e.
1261 struct a {int array[0];};
1263 struct a {int array1[0]; int array[];};
1264 Such struct has size 0 but accesses to a.array may have non-zero size.
1265 In this case the size of TREE_TYPE (base1) is smaller than
1266 size of TREE_TYPE (TREE_OPERAND (base1, 0)).
1268 Because we compare sizes of arrays just by sizes of their elements,
1269 we only need to care about zero sized array fields here. */
1270 if (component_ref_to_zero_sized_trailing_array_p (base1
))
1272 gcc_checking_assert (!end_struct_ref1
);
1273 end_struct_ref1
= base1
;
1275 if (ends_tbaa_access_path_p (base1
))
1277 ref1
= TREE_OPERAND (base1
, 0);
1278 if (end_struct_ref1
)
1280 end_struct_past_end1
= true;
1281 end_struct_ref1
= NULL
;
1284 base1
= TREE_OPERAND (base1
, 0);
1286 type1
= TREE_TYPE (base1
);
1288 while (handled_component_p (base2
))
1290 if (component_ref_to_zero_sized_trailing_array_p (base2
))
1292 gcc_checking_assert (!end_struct_ref2
);
1293 end_struct_ref2
= base2
;
1295 if (ends_tbaa_access_path_p (base2
))
1297 ref2
= TREE_OPERAND (base2
, 0);
1298 if (end_struct_ref2
)
1300 end_struct_past_end2
= true;
1301 end_struct_ref2
= NULL
;
1304 base2
= TREE_OPERAND (base2
, 0);
1306 type2
= TREE_TYPE (base2
);
1308 /* Now search for the type1 in the access path of ref2. This
1309 would be a common base for doing offset based disambiguation on.
1310 This however only makes sense if type2 is big enough to hold type1. */
1311 int cmp_outer
= compare_type_sizes (type2
, type1
);
1313 /* If type2 is big enough to contain type1 walk its access path.
1314 We also need to care of arrays at the end of structs that may extend
1315 beyond the end of structure. If this occurs in the TBAA part of the
1316 access path, we need to consider the increased type as well. */
1319 && compare_type_sizes (TREE_TYPE (end_struct_ref2
), type1
) >= 0))
1321 int res
= aliasing_component_refs_walk (ref1
, type1
, base1
,
1324 ref2
, base2
, offset2
, max_size2
,
1330 /* If we didn't find a common base, try the other way around. */
1333 && compare_type_sizes (TREE_TYPE (end_struct_ref1
), type2
) <= 0))
1335 int res
= aliasing_component_refs_walk (ref2
, type2
, base2
,
1338 ref1
, base1
, offset1
, max_size1
,
1344 /* In the following code we make an assumption that the types in access
1345 paths do not overlap and thus accesses alias only if one path can be
1346 continuation of another. If we was not able to decide about equivalence,
1347 we need to give up. */
1350 if (!nonoverlapping_component_refs_p (ref1
, ref2
))
1352 ++alias_stats
.aliasing_component_refs_p_may_alias
;
1355 ++alias_stats
.aliasing_component_refs_p_no_alias
;
1359 if (access_path_may_continue_p (TREE_TYPE (ref1
), end_struct_past_end1
,
1361 type2
, end_struct_ref2
,
1363 || access_path_may_continue_p (TREE_TYPE (ref2
), end_struct_past_end2
,
1365 type1
, end_struct_ref1
,
1368 ++alias_stats
.aliasing_component_refs_p_may_alias
;
1371 ++alias_stats
.aliasing_component_refs_p_no_alias
;
1375 /* FIELD1 and FIELD2 are two fields of component refs. We assume
1376 that bases of both component refs are either equivalent or nonoverlapping.
1377 We do not assume that the containers of FIELD1 and FIELD2 are of the
1380 Return 0 in case the base address of component_refs are same then
1381 FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1382 may not be of same type or size.
1384 Return 1 if FIELD1 and FIELD2 are non-overlapping.
1386 Return -1 otherwise.
1388 Main difference between 0 and -1 is to let
1389 nonoverlapping_component_refs_since_match_p discover the semantically
1390 equivalent part of the access path.
1392 Note that this function is used even with -fno-strict-aliasing
1393 and makes use of no TBAA assumptions. */
1396 nonoverlapping_component_refs_p_1 (const_tree field1
, const_tree field2
)
1398 /* If both fields are of the same type, we could save hard work of
1399 comparing offsets. */
1400 tree type1
= DECL_CONTEXT (field1
);
1401 tree type2
= DECL_CONTEXT (field2
);
1403 if (TREE_CODE (type1
) == RECORD_TYPE
1404 && DECL_BIT_FIELD_REPRESENTATIVE (field1
))
1405 field1
= DECL_BIT_FIELD_REPRESENTATIVE (field1
);
1406 if (TREE_CODE (type2
) == RECORD_TYPE
1407 && DECL_BIT_FIELD_REPRESENTATIVE (field2
))
1408 field2
= DECL_BIT_FIELD_REPRESENTATIVE (field2
);
1410 /* ??? Bitfields can overlap at RTL level so punt on them.
1411 FIXME: RTL expansion should be fixed by adjusting the access path
1412 when producing MEM_ATTRs for MEMs which are wider than
1413 the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1414 if (DECL_BIT_FIELD (field1
) && DECL_BIT_FIELD (field2
))
1417 /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1418 if (type1
== type2
&& TREE_CODE (type1
) == RECORD_TYPE
)
1419 return field1
!= field2
;
1421 /* In common case the offsets and bit offsets will be the same.
1422 However if frontends do not agree on the alignment, they may be
1423 different even if they actually represent same address.
1424 Try the common case first and if that fails calcualte the
1425 actual bit offset. */
1426 if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1
),
1427 DECL_FIELD_OFFSET (field2
))
1428 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1
),
1429 DECL_FIELD_BIT_OFFSET (field2
)))
1432 /* Note that it may be possible to use component_ref_field_offset
1433 which would provide offsets as trees. However constructing and folding
1434 trees is expensive and does not seem to be worth the compile time
1437 poly_uint64 offset1
, offset2
;
1438 poly_uint64 bit_offset1
, bit_offset2
;
1440 if (poly_int_tree_p (DECL_FIELD_OFFSET (field1
), &offset1
)
1441 && poly_int_tree_p (DECL_FIELD_OFFSET (field2
), &offset2
)
1442 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1
), &bit_offset1
)
1443 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2
), &bit_offset2
))
1445 offset1
= (offset1
<< LOG2_BITS_PER_UNIT
) + bit_offset1
;
1446 offset2
= (offset2
<< LOG2_BITS_PER_UNIT
) + bit_offset2
;
1448 if (known_eq (offset1
, offset2
))
1451 poly_uint64 size1
, size2
;
1453 if (poly_int_tree_p (DECL_SIZE (field1
), &size1
)
1454 && poly_int_tree_p (DECL_SIZE (field2
), &size2
)
1455 && !ranges_maybe_overlap_p (offset1
, size1
, offset2
, size2
))
1458 /* Resort to slower overlap checking by looking for matching types in
1459 the middle of access path. */
1463 /* Return low bound of array. Do not produce new trees
1464 and thus do not care about particular type of integer constant
1465 and placeholder exprs. */
1468 cheap_array_ref_low_bound (tree ref
)
1470 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref
, 0)));
1472 /* Avoid expensive array_ref_low_bound.
1473 low bound is either stored in operand2, or it is TYPE_MIN_VALUE of domain
1474 type or it is zero. */
1475 if (TREE_OPERAND (ref
, 2))
1476 return TREE_OPERAND (ref
, 2);
1477 else if (domain_type
&& TYPE_MIN_VALUE (domain_type
))
1478 return TYPE_MIN_VALUE (domain_type
);
1480 return integer_zero_node
;
1483 /* REF1 and REF2 are ARRAY_REFs with either same base address or which are
1484 completely disjoint.
1486 Return 1 if the refs are non-overlapping.
1487 Return 0 if they are possibly overlapping but if so the overlap again
1488 starts on the same address.
1489 Return -1 otherwise. */
1492 nonoverlapping_array_refs_p (tree ref1
, tree ref2
)
1494 tree index1
= TREE_OPERAND (ref1
, 1);
1495 tree index2
= TREE_OPERAND (ref2
, 1);
1496 tree low_bound1
= cheap_array_ref_low_bound (ref1
);
1497 tree low_bound2
= cheap_array_ref_low_bound (ref2
);
1499 /* Handle zero offsets first: we do not need to match type size in this
1501 if (operand_equal_p (index1
, low_bound1
, 0)
1502 && operand_equal_p (index2
, low_bound2
, 0))
1505 /* If type sizes are different, give up.
1507 Avoid expensive array_ref_element_size.
1508 If operand 3 is present it denotes size in the alignmnet units.
1509 Otherwise size is TYPE_SIZE of the element type.
1510 Handle only common cases where types are of the same "kind". */
1511 if ((TREE_OPERAND (ref1
, 3) == NULL
) != (TREE_OPERAND (ref2
, 3) == NULL
))
1514 tree elmt_type1
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref1
, 0)));
1515 tree elmt_type2
= TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref2
, 0)));
1517 if (TREE_OPERAND (ref1
, 3))
1519 if (TYPE_ALIGN (elmt_type1
) != TYPE_ALIGN (elmt_type2
)
1520 || !operand_equal_p (TREE_OPERAND (ref1
, 3),
1521 TREE_OPERAND (ref2
, 3), 0))
1526 if (!operand_equal_p (TYPE_SIZE_UNIT (elmt_type1
),
1527 TYPE_SIZE_UNIT (elmt_type2
), 0))
1531 /* Since we know that type sizes are the same, there is no need to return
1532 -1 after this point. Partial overlap can not be introduced. */
1534 /* We may need to fold trees in this case.
1535 TODO: Handle integer constant case at least. */
1536 if (!operand_equal_p (low_bound1
, low_bound2
, 0))
1539 if (TREE_CODE (index1
) == INTEGER_CST
&& TREE_CODE (index2
) == INTEGER_CST
)
1541 if (tree_int_cst_equal (index1
, index2
))
1545 /* TODO: We can use VRP to further disambiguate here. */
1549 /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1550 MATCH2 either point to the same address or are disjoint.
1551 MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1552 respectively or NULL in the case we established equivalence of bases.
1553 If PARTIAL_OVERLAP is true assume that the toplevel arrays may actually
1554 overlap by exact multiply of their element size.
1556 This test works by matching the initial segment of the access path
1557 and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1558 match was determined without use of TBAA oracle.
1560 Return 1 if we can determine that component references REF1 and REF2,
1561 that are within a common DECL, cannot overlap.
1563 Return 0 if paths are same and thus there is nothing to disambiguate more
1564 (i.e. there is must alias assuming there is must alias between MATCH1 and
1567 Return -1 if we can not determine 0 or 1 - this happens when we met
1568 non-matching types was met in the path.
1569 In this case it may make sense to continue by other disambiguation
1573 nonoverlapping_refs_since_match_p (tree match1
, tree ref1
,
1574 tree match2
, tree ref2
,
1575 bool partial_overlap
)
1577 int ntbaa1
= 0, ntbaa2
= 0;
1578 /* Early return if there are no references to match, we do not need
1579 to walk the access paths.
1581 Do not consider this as may-alias for stats - it is more useful
1582 to have information how many disambiguations happened provided that
1583 the query was meaningful. */
1585 if (match1
== ref1
|| !handled_component_p (ref1
)
1586 || match2
== ref2
|| !handled_component_p (ref2
))
1589 auto_vec
<tree
, 16> component_refs1
;
1590 auto_vec
<tree
, 16> component_refs2
;
1592 /* Create the stack of handled components for REF1. */
1593 while (handled_component_p (ref1
) && ref1
!= match1
)
1595 /* We use TBAA only to re-synchronize after mismatched refs. So we
1596 do not need to truncate access path after TBAA part ends. */
1597 if (ends_tbaa_access_path_p (ref1
))
1601 component_refs1
.safe_push (ref1
);
1602 ref1
= TREE_OPERAND (ref1
, 0);
1605 /* Create the stack of handled components for REF2. */
1606 while (handled_component_p (ref2
) && ref2
!= match2
)
1608 if (ends_tbaa_access_path_p (ref2
))
1612 component_refs2
.safe_push (ref2
);
1613 ref2
= TREE_OPERAND (ref2
, 0);
1616 if (!flag_strict_aliasing
)
1622 bool mem_ref1
= TREE_CODE (ref1
) == MEM_REF
&& ref1
!= match1
;
1623 bool mem_ref2
= TREE_CODE (ref2
) == MEM_REF
&& ref2
!= match2
;
1625 /* If only one of access path starts with MEM_REF check that offset is 0
1626 so the addresses stays the same after stripping it.
1627 TODO: In this case we may walk the other access path until we get same
1630 If both starts with MEM_REF, offset has to be same. */
1631 if ((mem_ref1
&& !mem_ref2
&& !integer_zerop (TREE_OPERAND (ref1
, 1)))
1632 || (mem_ref2
&& !mem_ref1
&& !integer_zerop (TREE_OPERAND (ref2
, 1)))
1633 || (mem_ref1
&& mem_ref2
1634 && !tree_int_cst_equal (TREE_OPERAND (ref1
, 1),
1635 TREE_OPERAND (ref2
, 1))))
1637 ++alias_stats
.nonoverlapping_refs_since_match_p_may_alias
;
1641 /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1642 to handle them here at all. */
1643 gcc_checking_assert (TREE_CODE (ref1
) != TARGET_MEM_REF
1644 && TREE_CODE (ref2
) != TARGET_MEM_REF
);
1646 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1647 rank. This is sufficient because we start from the same DECL and you
1648 cannot reference several fields at a time with COMPONENT_REFs (unlike
1649 with ARRAY_RANGE_REFs for arrays) so you always need the same number
1650 of them to access a sub-component, unless you're in a union, in which
1651 case the return value will precisely be false. */
1654 /* Track if we seen unmatched ref with non-zero offset. In this case
1655 we must look for partial overlaps. */
1656 bool seen_unmatched_ref_p
= false;
1658 /* First match ARRAY_REFs an try to disambiguate. */
1659 if (!component_refs1
.is_empty ()
1660 && !component_refs2
.is_empty ())
1662 unsigned int narray_refs1
=0, narray_refs2
=0;
1664 /* We generally assume that both access paths starts by same sequence
1665 of refs. However if number of array refs is not in sync, try
1666 to recover and pop elts until number match. This helps the case
1667 where one access path starts by array and other by element. */
1668 for (narray_refs1
= 0; narray_refs1
< component_refs1
.length ();
1670 if (TREE_CODE (component_refs1
[component_refs1
.length()
1671 - 1 - narray_refs1
]) != ARRAY_REF
)
1674 for (narray_refs2
= 0; narray_refs2
< component_refs2
.length ();
1676 if (TREE_CODE (component_refs2
[component_refs2
.length()
1677 - 1 - narray_refs2
]) != ARRAY_REF
)
1679 for (; narray_refs1
> narray_refs2
; narray_refs1
--)
1681 ref1
= component_refs1
.pop ();
1684 /* If index is non-zero we need to check whether the reference
1685 does not break the main invariant that bases are either
1686 disjoint or equal. Consider the example:
1688 unsigned char out[][1];
1692 Here bases out and out are same, but after removing the
1693 [i] index, this invariant no longer holds, because
1694 out[i] points to the middle of array out.
1696 TODO: If size of type of the skipped reference is an integer
1697 multiply of the size of type of the other reference this
1698 invariant can be verified, but even then it is not completely
1699 safe with !flag_strict_aliasing if the other reference contains
1700 unbounded array accesses.
1703 if (!operand_equal_p (TREE_OPERAND (ref1
, 1),
1704 cheap_array_ref_low_bound (ref1
), 0))
1707 for (; narray_refs2
> narray_refs1
; narray_refs2
--)
1709 ref2
= component_refs2
.pop ();
1711 if (!operand_equal_p (TREE_OPERAND (ref2
, 1),
1712 cheap_array_ref_low_bound (ref2
), 0))
1715 /* Try to disambiguate matched arrays. */
1716 for (unsigned int i
= 0; i
< narray_refs1
; i
++)
1718 int cmp
= nonoverlapping_array_refs_p (component_refs1
.pop (),
1719 component_refs2
.pop ());
1722 if (cmp
== 1 && !partial_overlap
)
1725 .nonoverlapping_refs_since_match_p_no_alias
;
1730 seen_unmatched_ref_p
= true;
1731 /* We can not maintain the invariant that bases are either
1732 same or completely disjoint. However we can still recover
1733 from type based alias analysis if we reach references to
1734 same sizes. We do not attempt to match array sizes, so
1735 just finish array walking and look for component refs. */
1736 if (ntbaa1
< 0 || ntbaa2
< 0)
1738 ++alias_stats
.nonoverlapping_refs_since_match_p_may_alias
;
1741 for (i
++; i
< narray_refs1
; i
++)
1743 component_refs1
.pop ();
1744 component_refs2
.pop ();
1750 partial_overlap
= false;
1754 /* Next look for component_refs. */
1757 if (component_refs1
.is_empty ())
1760 .nonoverlapping_refs_since_match_p_must_overlap
;
1763 ref1
= component_refs1
.pop ();
1765 if (TREE_CODE (ref1
) != COMPONENT_REF
)
1767 seen_unmatched_ref_p
= true;
1768 if (ntbaa1
< 0 || ntbaa2
< 0)
1770 ++alias_stats
.nonoverlapping_refs_since_match_p_may_alias
;
1775 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1
, 0))));
1779 if (component_refs2
.is_empty ())
1782 .nonoverlapping_refs_since_match_p_must_overlap
;
1785 ref2
= component_refs2
.pop ();
1787 if (TREE_CODE (ref2
) != COMPONENT_REF
)
1789 if (ntbaa1
< 0 || ntbaa2
< 0)
1791 ++alias_stats
.nonoverlapping_refs_since_match_p_may_alias
;
1794 seen_unmatched_ref_p
= true;
1797 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2
, 0))));
1799 /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1801 gcc_checking_assert (TREE_CODE (ref1
) == COMPONENT_REF
1802 && TREE_CODE (ref2
) == COMPONENT_REF
);
1804 tree field1
= TREE_OPERAND (ref1
, 1);
1805 tree field2
= TREE_OPERAND (ref2
, 1);
1807 /* ??? We cannot simply use the type of operand #0 of the refs here
1808 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1809 for common blocks instead of using unions like everyone else. */
1810 tree type1
= DECL_CONTEXT (field1
);
1811 tree type2
= DECL_CONTEXT (field2
);
1813 partial_overlap
= false;
1815 /* If we skipped array refs on type of different sizes, we can
1816 no longer be sure that there are not partial overlaps. */
1817 if (seen_unmatched_ref_p
&& ntbaa1
>= 0 && ntbaa2
>= 0
1818 && !operand_equal_p (TYPE_SIZE (type1
), TYPE_SIZE (type2
), 0))
1821 .nonoverlapping_refs_since_match_p_may_alias
;
1825 int cmp
= nonoverlapping_component_refs_p_1 (field1
, field2
);
1829 .nonoverlapping_refs_since_match_p_may_alias
;
1835 .nonoverlapping_refs_since_match_p_no_alias
;
1841 /* Return TYPE_UID which can be used to match record types we consider
1842 same for TBAA purposes. */
1845 ncr_type_uid (const_tree field
)
1847 /* ??? We cannot simply use the type of operand #0 of the refs here
1848 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1849 for common blocks instead of using unions like everyone else. */
1850 tree type
= DECL_FIELD_CONTEXT (field
);
1851 /* With LTO types considered same_type_for_tbaa_p
1852 from different translation unit may not have same
1853 main variant. They however have same TYPE_CANONICAL. */
1854 if (TYPE_CANONICAL (type
))
1855 return TYPE_UID (TYPE_CANONICAL (type
));
1856 return TYPE_UID (type
);
1859 /* qsort compare function to sort FIELD_DECLs after their
1860 DECL_FIELD_CONTEXT TYPE_UID. */
1863 ncr_compar (const void *field1_
, const void *field2_
)
1865 const_tree field1
= *(const_tree
*) const_cast <void *>(field1_
);
1866 const_tree field2
= *(const_tree
*) const_cast <void *>(field2_
);
1867 unsigned int uid1
= ncr_type_uid (field1
);
1868 unsigned int uid2
= ncr_type_uid (field2
);
1872 else if (uid1
> uid2
)
1877 /* Return true if we can determine that the fields referenced cannot
1878 overlap for any pair of objects. This relies on TBAA. */
1881 nonoverlapping_component_refs_p (const_tree x
, const_tree y
)
1883 /* Early return if we have nothing to do.
1885 Do not consider this as may-alias for stats - it is more useful
1886 to have information how many disambiguations happened provided that
1887 the query was meaningful. */
1888 if (!flag_strict_aliasing
1890 || !handled_component_p (x
)
1891 || !handled_component_p (y
))
1894 auto_vec
<const_tree
, 16> fieldsx
;
1895 while (handled_component_p (x
))
1897 if (TREE_CODE (x
) == COMPONENT_REF
)
1899 tree field
= TREE_OPERAND (x
, 1);
1900 tree type
= DECL_FIELD_CONTEXT (field
);
1901 if (TREE_CODE (type
) == RECORD_TYPE
)
1902 fieldsx
.safe_push (field
);
1904 else if (ends_tbaa_access_path_p (x
))
1905 fieldsx
.truncate (0);
1906 x
= TREE_OPERAND (x
, 0);
1908 if (fieldsx
.length () == 0)
1910 auto_vec
<const_tree
, 16> fieldsy
;
1911 while (handled_component_p (y
))
1913 if (TREE_CODE (y
) == COMPONENT_REF
)
1915 tree field
= TREE_OPERAND (y
, 1);
1916 tree type
= DECL_FIELD_CONTEXT (field
);
1917 if (TREE_CODE (type
) == RECORD_TYPE
)
1918 fieldsy
.safe_push (TREE_OPERAND (y
, 1));
1920 else if (ends_tbaa_access_path_p (y
))
1921 fieldsy
.truncate (0);
1922 y
= TREE_OPERAND (y
, 0);
1924 if (fieldsy
.length () == 0)
1926 ++alias_stats
.nonoverlapping_component_refs_p_may_alias
;
1930 /* Most common case first. */
1931 if (fieldsx
.length () == 1
1932 && fieldsy
.length () == 1)
1934 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx
[0]),
1935 DECL_FIELD_CONTEXT (fieldsy
[0])) == 1
1936 && nonoverlapping_component_refs_p_1 (fieldsx
[0], fieldsy
[0]) == 1)
1938 ++alias_stats
.nonoverlapping_component_refs_p_no_alias
;
1943 ++alias_stats
.nonoverlapping_component_refs_p_may_alias
;
1948 if (fieldsx
.length () == 2)
1950 if (ncr_compar (&fieldsx
[0], &fieldsx
[1]) == 1)
1951 std::swap (fieldsx
[0], fieldsx
[1]);
1954 fieldsx
.qsort (ncr_compar
);
1956 if (fieldsy
.length () == 2)
1958 if (ncr_compar (&fieldsy
[0], &fieldsy
[1]) == 1)
1959 std::swap (fieldsy
[0], fieldsy
[1]);
1962 fieldsy
.qsort (ncr_compar
);
1964 unsigned i
= 0, j
= 0;
1967 const_tree fieldx
= fieldsx
[i
];
1968 const_tree fieldy
= fieldsy
[j
];
1970 /* We're left with accessing different fields of a structure,
1971 no possible overlap. */
1972 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx
),
1973 DECL_FIELD_CONTEXT (fieldy
)) == 1
1974 && nonoverlapping_component_refs_p_1 (fieldx
, fieldy
) == 1)
1976 ++alias_stats
.nonoverlapping_component_refs_p_no_alias
;
1980 if (ncr_type_uid (fieldx
) < ncr_type_uid (fieldy
))
1983 if (i
== fieldsx
.length ())
1989 if (j
== fieldsy
.length ())
1995 ++alias_stats
.nonoverlapping_component_refs_p_may_alias
;
2000 /* Return true if two memory references based on the variables BASE1
2001 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2002 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
2003 if non-NULL are the complete memory reference trees. */
2006 decl_refs_may_alias_p (tree ref1
, tree base1
,
2007 poly_int64 offset1
, poly_int64 max_size1
,
2009 tree ref2
, tree base2
,
2010 poly_int64 offset2
, poly_int64 max_size2
,
2013 gcc_checking_assert (DECL_P (base1
) && DECL_P (base2
));
2015 /* If both references are based on different variables, they cannot alias. */
2016 if (compare_base_decls (base1
, base2
) == 0)
2019 /* If both references are based on the same variable, they cannot alias if
2020 the accesses do not overlap. */
2021 if (!ranges_maybe_overlap_p (offset1
, max_size1
, offset2
, max_size2
))
2024 /* If there is must alias, there is no use disambiguating further. */
2025 if (known_eq (size1
, max_size1
) && known_eq (size2
, max_size2
))
2028 /* For components with variable position, the above test isn't sufficient,
2029 so we disambiguate component references manually. */
2031 && handled_component_p (ref1
) && handled_component_p (ref2
)
2032 && nonoverlapping_refs_since_match_p (NULL
, ref1
, NULL
, ref2
, false) == 1)
2038 /* Return true if access with BASE is view converted.
2039 Base must not be stripped from inner MEM_REF (&decl)
2040 which is done by ao_ref_base and thus one extra walk
2041 of handled components is needed. */
2044 view_converted_memref_p (tree base
)
2046 if (TREE_CODE (base
) != MEM_REF
&& TREE_CODE (base
) != TARGET_MEM_REF
)
2048 return same_type_for_tbaa (TREE_TYPE (base
),
2049 TREE_TYPE (TREE_OPERAND (base
, 1))) != 1;
2052 /* Return true if an indirect reference based on *PTR1 constrained
2053 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
2054 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
2055 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2056 in which case they are computed on-demand. REF1 and REF2
2057 if non-NULL are the complete memory reference trees. */
2060 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED
, tree base1
,
2061 poly_int64 offset1
, poly_int64 max_size1
,
2063 alias_set_type ref1_alias_set
,
2064 alias_set_type base1_alias_set
,
2065 tree ref2 ATTRIBUTE_UNUSED
, tree base2
,
2066 poly_int64 offset2
, poly_int64 max_size2
,
2068 alias_set_type ref2_alias_set
,
2069 alias_set_type base2_alias_set
, bool tbaa_p
)
2072 tree ptrtype1
, dbase2
;
2074 gcc_checking_assert ((TREE_CODE (base1
) == MEM_REF
2075 || TREE_CODE (base1
) == TARGET_MEM_REF
)
2078 ptr1
= TREE_OPERAND (base1
, 0);
2079 poly_offset_int moff
= mem_ref_offset (base1
) << LOG2_BITS_PER_UNIT
;
2081 /* If only one reference is based on a variable, they cannot alias if
2082 the pointer access is beyond the extent of the variable access.
2083 (the pointer base cannot validly point to an offset less than zero
2085 ??? IVOPTs creates bases that do not honor this restriction,
2086 so do not apply this optimization for TARGET_MEM_REFs. */
2087 if (TREE_CODE (base1
) != TARGET_MEM_REF
2088 && !ranges_maybe_overlap_p (offset1
+ moff
, -1, offset2
, max_size2
))
2091 /* If the pointer based access is bigger than the variable they cannot
2092 alias. This is similar to the check below where we use TBAA to
2093 increase the size of the pointer based access based on the dynamic
2094 type of a containing object we can infer from it. */
2096 if (known_size_p (size1
)
2097 && poly_int_tree_p (DECL_SIZE (base2
), &dsize2
)
2098 && known_lt (dsize2
, size1
))
2101 /* They also cannot alias if the pointer may not point to the decl. */
2102 if (!ptr_deref_may_alias_decl_p (ptr1
, base2
))
2105 /* Disambiguations that rely on strict aliasing rules follow. */
2106 if (!flag_strict_aliasing
|| !tbaa_p
)
2109 /* If the alias set for a pointer access is zero all bets are off. */
2110 if (base1_alias_set
== 0 || base2_alias_set
== 0)
2113 /* When we are trying to disambiguate an access with a pointer dereference
2114 as base versus one with a decl as base we can use both the size
2115 of the decl and its dynamic type for extra disambiguation.
2116 ??? We do not know anything about the dynamic type of the decl
2117 other than that its alias-set contains base2_alias_set as a subset
2118 which does not help us here. */
2119 /* As we know nothing useful about the dynamic type of the decl just
2120 use the usual conflict check rather than a subset test.
2121 ??? We could introduce -fvery-strict-aliasing when the language
2122 does not allow decls to have a dynamic type that differs from their
2123 static type. Then we can check
2124 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
2125 if (base1_alias_set
!= base2_alias_set
2126 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
2129 ptrtype1
= TREE_TYPE (TREE_OPERAND (base1
, 1));
2131 /* If the size of the access relevant for TBAA through the pointer
2132 is bigger than the size of the decl we can't possibly access the
2133 decl via that pointer. */
2134 if (/* ??? This in turn may run afoul when a decl of type T which is
2135 a member of union type U is accessed through a pointer to
2136 type U and sizeof T is smaller than sizeof U. */
2137 TREE_CODE (TREE_TYPE (ptrtype1
)) != UNION_TYPE
2138 && TREE_CODE (TREE_TYPE (ptrtype1
)) != QUAL_UNION_TYPE
2139 && compare_sizes (DECL_SIZE (base2
),
2140 TYPE_SIZE (TREE_TYPE (ptrtype1
))) < 0)
2146 /* If the decl is accessed via a MEM_REF, reconstruct the base
2147 we can use for TBAA and an appropriately adjusted offset. */
2149 while (handled_component_p (dbase2
))
2150 dbase2
= TREE_OPERAND (dbase2
, 0);
2151 poly_int64 doffset1
= offset1
;
2152 poly_offset_int doffset2
= offset2
;
2153 if (TREE_CODE (dbase2
) == MEM_REF
2154 || TREE_CODE (dbase2
) == TARGET_MEM_REF
)
2156 doffset2
-= mem_ref_offset (dbase2
) << LOG2_BITS_PER_UNIT
;
2157 tree ptrtype2
= TREE_TYPE (TREE_OPERAND (dbase2
, 1));
2158 /* If second reference is view-converted, give up now. */
2159 if (same_type_for_tbaa (TREE_TYPE (dbase2
), TREE_TYPE (ptrtype2
)) != 1)
2163 /* If first reference is view-converted, give up now. */
2164 if (same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) != 1)
2167 /* If both references are through the same type, they do not alias
2168 if the accesses do not overlap. This does extra disambiguation
2169 for mixed/pointer accesses but requires strict aliasing.
2170 For MEM_REFs we require that the component-ref offset we computed
2171 is relative to the start of the type which we ensure by
2172 comparing rvalue and access type and disregarding the constant
2175 But avoid treating variable length arrays as "objects", instead assume they
2176 can overlap by an exact multiple of their element size.
2177 See gcc.dg/torture/alias-2.c. */
2178 if (((TREE_CODE (base1
) != TARGET_MEM_REF
2179 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
2180 && (TREE_CODE (dbase2
) != TARGET_MEM_REF
2181 || (!TMR_INDEX (dbase2
) && !TMR_INDEX2 (dbase2
))))
2182 && same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (dbase2
)) == 1)
2184 bool partial_overlap
= (TREE_CODE (TREE_TYPE (base1
)) == ARRAY_TYPE
2185 && (TYPE_SIZE (TREE_TYPE (base1
))
2186 && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1
)))
2188 if (!partial_overlap
2189 && !ranges_maybe_overlap_p (doffset1
, max_size1
, doffset2
, max_size2
))
2192 /* If there is must alias, there is no use disambiguating further. */
2193 || (!partial_overlap
2194 && known_eq (size1
, max_size1
) && known_eq (size2
, max_size2
)))
2196 int res
= nonoverlapping_refs_since_match_p (base1
, ref1
, base2
, ref2
,
2199 return !nonoverlapping_component_refs_p (ref1
, ref2
);
2203 /* Do access-path based disambiguation. */
2205 && (handled_component_p (ref1
) || handled_component_p (ref2
)))
2206 return aliasing_component_refs_p (ref1
,
2207 ref1_alias_set
, base1_alias_set
,
2210 ref2_alias_set
, base2_alias_set
,
2211 offset2
, max_size2
);
2216 /* Return true if two indirect references based on *PTR1
2217 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2218 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
2219 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2220 in which case they are computed on-demand. REF1 and REF2
2221 if non-NULL are the complete memory reference trees. */
2224 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED
, tree base1
,
2225 poly_int64 offset1
, poly_int64 max_size1
,
2227 alias_set_type ref1_alias_set
,
2228 alias_set_type base1_alias_set
,
2229 tree ref2 ATTRIBUTE_UNUSED
, tree base2
,
2230 poly_int64 offset2
, poly_int64 max_size2
,
2232 alias_set_type ref2_alias_set
,
2233 alias_set_type base2_alias_set
, bool tbaa_p
)
2237 tree ptrtype1
, ptrtype2
;
2239 gcc_checking_assert ((TREE_CODE (base1
) == MEM_REF
2240 || TREE_CODE (base1
) == TARGET_MEM_REF
)
2241 && (TREE_CODE (base2
) == MEM_REF
2242 || TREE_CODE (base2
) == TARGET_MEM_REF
));
2244 ptr1
= TREE_OPERAND (base1
, 0);
2245 ptr2
= TREE_OPERAND (base2
, 0);
2247 /* If both bases are based on pointers they cannot alias if they may not
2248 point to the same memory object or if they point to the same object
2249 and the accesses do not overlap. */
2250 if ((!cfun
|| gimple_in_ssa_p (cfun
))
2251 && operand_equal_p (ptr1
, ptr2
, 0)
2252 && (((TREE_CODE (base1
) != TARGET_MEM_REF
2253 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
2254 && (TREE_CODE (base2
) != TARGET_MEM_REF
2255 || (!TMR_INDEX (base2
) && !TMR_INDEX2 (base2
))))
2256 || (TREE_CODE (base1
) == TARGET_MEM_REF
2257 && TREE_CODE (base2
) == TARGET_MEM_REF
2258 && (TMR_STEP (base1
) == TMR_STEP (base2
)
2259 || (TMR_STEP (base1
) && TMR_STEP (base2
)
2260 && operand_equal_p (TMR_STEP (base1
),
2261 TMR_STEP (base2
), 0)))
2262 && (TMR_INDEX (base1
) == TMR_INDEX (base2
)
2263 || (TMR_INDEX (base1
) && TMR_INDEX (base2
)
2264 && operand_equal_p (TMR_INDEX (base1
),
2265 TMR_INDEX (base2
), 0)))
2266 && (TMR_INDEX2 (base1
) == TMR_INDEX2 (base2
)
2267 || (TMR_INDEX2 (base1
) && TMR_INDEX2 (base2
)
2268 && operand_equal_p (TMR_INDEX2 (base1
),
2269 TMR_INDEX2 (base2
), 0))))))
2271 poly_offset_int moff1
= mem_ref_offset (base1
) << LOG2_BITS_PER_UNIT
;
2272 poly_offset_int moff2
= mem_ref_offset (base2
) << LOG2_BITS_PER_UNIT
;
2273 if (!ranges_maybe_overlap_p (offset1
+ moff1
, max_size1
,
2274 offset2
+ moff2
, max_size2
))
2276 /* If there is must alias, there is no use disambiguating further. */
2277 if (known_eq (size1
, max_size1
) && known_eq (size2
, max_size2
))
2281 int res
= nonoverlapping_refs_since_match_p (NULL
, ref1
, NULL
, ref2
,
2287 if (!ptr_derefs_may_alias_p (ptr1
, ptr2
))
2290 /* Disambiguations that rely on strict aliasing rules follow. */
2291 if (!flag_strict_aliasing
|| !tbaa_p
)
2294 ptrtype1
= TREE_TYPE (TREE_OPERAND (base1
, 1));
2295 ptrtype2
= TREE_TYPE (TREE_OPERAND (base2
, 1));
2297 /* If the alias set for a pointer access is zero all bets are off. */
2298 if (base1_alias_set
== 0
2299 || base2_alias_set
== 0)
2302 /* Do type-based disambiguation. */
2303 if (base1_alias_set
!= base2_alias_set
2304 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
2307 /* If either reference is view-converted, give up now. */
2308 if (same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) != 1
2309 || same_type_for_tbaa (TREE_TYPE (base2
), TREE_TYPE (ptrtype2
)) != 1)
2312 /* If both references are through the same type, they do not alias
2313 if the accesses do not overlap. This does extra disambiguation
2314 for mixed/pointer accesses but requires strict aliasing. */
2315 if ((TREE_CODE (base1
) != TARGET_MEM_REF
2316 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
2317 && (TREE_CODE (base2
) != TARGET_MEM_REF
2318 || (!TMR_INDEX (base2
) && !TMR_INDEX2 (base2
)))
2319 && same_type_for_tbaa (TREE_TYPE (ptrtype1
),
2320 TREE_TYPE (ptrtype2
)) == 1)
2322 /* But avoid treating arrays as "objects", instead assume they
2323 can overlap by an exact multiple of their element size.
2324 See gcc.dg/torture/alias-2.c. */
2325 bool partial_overlap
= TREE_CODE (TREE_TYPE (ptrtype1
)) == ARRAY_TYPE
;
2327 if (!partial_overlap
2328 && !ranges_maybe_overlap_p (offset1
, max_size1
, offset2
, max_size2
))
2331 || (!partial_overlap
2332 && known_eq (size1
, max_size1
) && known_eq (size2
, max_size2
)))
2334 int res
= nonoverlapping_refs_since_match_p (base1
, ref1
, base2
, ref2
,
2337 return !nonoverlapping_component_refs_p (ref1
, ref2
);
2341 /* Do access-path based disambiguation. */
2343 && (handled_component_p (ref1
) || handled_component_p (ref2
)))
2344 return aliasing_component_refs_p (ref1
,
2345 ref1_alias_set
, base1_alias_set
,
2348 ref2_alias_set
, base2_alias_set
,
2349 offset2
, max_size2
);
2354 /* Return true, if the two memory references REF1 and REF2 may alias. */
2357 refs_may_alias_p_2 (ao_ref
*ref1
, ao_ref
*ref2
, bool tbaa_p
)
2360 poly_int64 offset1
= 0, offset2
= 0;
2361 poly_int64 max_size1
= -1, max_size2
= -1;
2362 bool var1_p
, var2_p
, ind1_p
, ind2_p
;
2364 gcc_checking_assert ((!ref1
->ref
2365 || TREE_CODE (ref1
->ref
) == SSA_NAME
2366 || DECL_P (ref1
->ref
)
2367 || TREE_CODE (ref1
->ref
) == STRING_CST
2368 || handled_component_p (ref1
->ref
)
2369 || TREE_CODE (ref1
->ref
) == MEM_REF
2370 || TREE_CODE (ref1
->ref
) == TARGET_MEM_REF
2371 || TREE_CODE (ref1
->ref
) == WITH_SIZE_EXPR
)
2373 || TREE_CODE (ref2
->ref
) == SSA_NAME
2374 || DECL_P (ref2
->ref
)
2375 || TREE_CODE (ref2
->ref
) == STRING_CST
2376 || handled_component_p (ref2
->ref
)
2377 || TREE_CODE (ref2
->ref
) == MEM_REF
2378 || TREE_CODE (ref2
->ref
) == TARGET_MEM_REF
2379 || TREE_CODE (ref2
->ref
) == WITH_SIZE_EXPR
));
2381 /* Decompose the references into their base objects and the access. */
2382 base1
= ao_ref_base (ref1
);
2383 offset1
= ref1
->offset
;
2384 max_size1
= ref1
->max_size
;
2385 base2
= ao_ref_base (ref2
);
2386 offset2
= ref2
->offset
;
2387 max_size2
= ref2
->max_size
;
2389 /* We can end up with registers or constants as bases for example from
2390 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
2391 which is seen as a struct copy. */
2392 if (TREE_CODE (base1
) == SSA_NAME
2393 || TREE_CODE (base1
) == CONST_DECL
2394 || TREE_CODE (base1
) == CONSTRUCTOR
2395 || TREE_CODE (base1
) == ADDR_EXPR
2396 || CONSTANT_CLASS_P (base1
)
2397 || TREE_CODE (base2
) == SSA_NAME
2398 || TREE_CODE (base2
) == CONST_DECL
2399 || TREE_CODE (base2
) == CONSTRUCTOR
2400 || TREE_CODE (base2
) == ADDR_EXPR
2401 || CONSTANT_CLASS_P (base2
))
2404 /* Two volatile accesses always conflict. */
2405 if (ref1
->volatile_p
2406 && ref2
->volatile_p
)
2409 /* refN->ref may convey size information, do not confuse our workers
2410 with that but strip it - ao_ref_base took it into account already. */
2411 tree ref1ref
= ref1
->ref
;
2412 if (ref1ref
&& TREE_CODE (ref1ref
) == WITH_SIZE_EXPR
)
2413 ref1ref
= TREE_OPERAND (ref1ref
, 0);
2414 tree ref2ref
= ref2
->ref
;
2415 if (ref2ref
&& TREE_CODE (ref2ref
) == WITH_SIZE_EXPR
)
2416 ref2ref
= TREE_OPERAND (ref2ref
, 0);
2418 /* Defer to simple offset based disambiguation if we have
2419 references based on two decls. Do this before defering to
2420 TBAA to handle must-alias cases in conformance with the
2421 GCC extension of allowing type-punning through unions. */
2422 var1_p
= DECL_P (base1
);
2423 var2_p
= DECL_P (base2
);
2424 if (var1_p
&& var2_p
)
2425 return decl_refs_may_alias_p (ref1ref
, base1
, offset1
, max_size1
,
2427 ref2ref
, base2
, offset2
, max_size2
,
2430 /* We can end up referring to code via function and label decls.
2431 As we likely do not properly track code aliases conservatively
2433 if (TREE_CODE (base1
) == FUNCTION_DECL
2434 || TREE_CODE (base1
) == LABEL_DECL
2435 || TREE_CODE (base2
) == FUNCTION_DECL
2436 || TREE_CODE (base2
) == LABEL_DECL
)
2439 /* Handle restrict based accesses.
2440 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
2442 tree rbase1
= base1
;
2443 tree rbase2
= base2
;
2448 while (handled_component_p (rbase1
))
2449 rbase1
= TREE_OPERAND (rbase1
, 0);
2455 while (handled_component_p (rbase2
))
2456 rbase2
= TREE_OPERAND (rbase2
, 0);
2458 if (rbase1
&& rbase2
2459 && (TREE_CODE (rbase1
) == MEM_REF
|| TREE_CODE (rbase1
) == TARGET_MEM_REF
)
2460 && (TREE_CODE (rbase2
) == MEM_REF
|| TREE_CODE (rbase2
) == TARGET_MEM_REF
)
2461 /* If the accesses are in the same restrict clique... */
2462 && MR_DEPENDENCE_CLIQUE (rbase1
) == MR_DEPENDENCE_CLIQUE (rbase2
)
2463 /* But based on different pointers they do not alias. */
2464 && MR_DEPENDENCE_BASE (rbase1
) != MR_DEPENDENCE_BASE (rbase2
))
2467 ind1_p
= (TREE_CODE (base1
) == MEM_REF
2468 || TREE_CODE (base1
) == TARGET_MEM_REF
);
2469 ind2_p
= (TREE_CODE (base2
) == MEM_REF
2470 || TREE_CODE (base2
) == TARGET_MEM_REF
);
2472 /* Canonicalize the pointer-vs-decl case. */
2473 if (ind1_p
&& var2_p
)
2475 std::swap (offset1
, offset2
);
2476 std::swap (max_size1
, max_size2
);
2477 std::swap (base1
, base2
);
2478 std::swap (ref1
, ref2
);
2479 std::swap (ref1ref
, ref2ref
);
2486 /* First defer to TBAA if possible. */
2488 && flag_strict_aliasing
2489 && !alias_sets_conflict_p (ao_ref_alias_set (ref1
),
2490 ao_ref_alias_set (ref2
)))
2493 /* If the reference is based on a pointer that points to memory
2494 that may not be written to then the other reference cannot possibly
2496 if ((TREE_CODE (TREE_OPERAND (base2
, 0)) == SSA_NAME
2497 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2
, 0)))
2499 && TREE_CODE (TREE_OPERAND (base1
, 0)) == SSA_NAME
2500 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1
, 0))))
2503 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2504 if (var1_p
&& ind2_p
)
2505 return indirect_ref_may_alias_decl_p (ref2ref
, base2
,
2506 offset2
, max_size2
, ref2
->size
,
2507 ao_ref_alias_set (ref2
),
2508 ao_ref_base_alias_set (ref2
),
2510 offset1
, max_size1
, ref1
->size
,
2511 ao_ref_alias_set (ref1
),
2512 ao_ref_base_alias_set (ref1
),
2514 else if (ind1_p
&& ind2_p
)
2515 return indirect_refs_may_alias_p (ref1ref
, base1
,
2516 offset1
, max_size1
, ref1
->size
,
2517 ao_ref_alias_set (ref1
),
2518 ao_ref_base_alias_set (ref1
),
2520 offset2
, max_size2
, ref2
->size
,
2521 ao_ref_alias_set (ref2
),
2522 ao_ref_base_alias_set (ref2
),
2528 /* Return true, if the two memory references REF1 and REF2 may alias
2529 and update statistics. */
2532 refs_may_alias_p_1 (ao_ref
*ref1
, ao_ref
*ref2
, bool tbaa_p
)
2534 bool res
= refs_may_alias_p_2 (ref1
, ref2
, tbaa_p
);
2536 ++alias_stats
.refs_may_alias_p_may_alias
;
2538 ++alias_stats
.refs_may_alias_p_no_alias
;
2543 refs_may_alias_p (tree ref1
, ao_ref
*ref2
, bool tbaa_p
)
2546 ao_ref_init (&r1
, ref1
);
2547 return refs_may_alias_p_1 (&r1
, ref2
, tbaa_p
);
2551 refs_may_alias_p (tree ref1
, tree ref2
, bool tbaa_p
)
2554 ao_ref_init (&r1
, ref1
);
2555 ao_ref_init (&r2
, ref2
);
2556 return refs_may_alias_p_1 (&r1
, &r2
, tbaa_p
);
2559 /* Returns true if there is a anti-dependence for the STORE that
2560 executes after the LOAD. */
2563 refs_anti_dependent_p (tree load
, tree store
)
2566 ao_ref_init (&r1
, load
);
2567 ao_ref_init (&r2
, store
);
2568 return refs_may_alias_p_1 (&r1
, &r2
, false);
2571 /* Returns true if there is a output dependence for the stores
2572 STORE1 and STORE2. */
2575 refs_output_dependent_p (tree store1
, tree store2
)
2578 ao_ref_init (&r1
, store1
);
2579 ao_ref_init (&r2
, store2
);
2580 return refs_may_alias_p_1 (&r1
, &r2
, false);
2583 /* Returns true if and only if REF may alias any access stored in TT.
2584 IF TBAA_P is true, use TBAA oracle. */
2587 modref_may_conflict (const gcall
*stmt
,
2588 modref_tree
<alias_set_type
> *tt
, ao_ref
*ref
, bool tbaa_p
)
2590 alias_set_type base_set
, ref_set
;
2591 bool global_memory_ok
= false;
2596 if (!dbg_cnt (ipa_mod_ref
))
2599 base_set
= ao_ref_base_alias_set (ref
);
2601 ref_set
= ao_ref_alias_set (ref
);
2603 int num_tests
= 0, max_tests
= param_modref_max_tests
;
2604 for (auto base_node
: tt
->bases
)
2606 if (tbaa_p
&& flag_strict_aliasing
)
2608 if (num_tests
>= max_tests
)
2610 alias_stats
.modref_tests
++;
2611 if (!alias_sets_conflict_p (base_set
, base_node
->base
))
2616 if (base_node
->every_ref
)
2619 for (auto ref_node
: base_node
->refs
)
2621 /* Do not repeat same test as before. */
2622 if ((ref_set
!= base_set
|| base_node
->base
!= ref_node
->ref
)
2623 && tbaa_p
&& flag_strict_aliasing
)
2625 if (num_tests
>= max_tests
)
2627 alias_stats
.modref_tests
++;
2628 if (!alias_sets_conflict_p (ref_set
, ref_node
->ref
))
2633 if (ref_node
->every_access
)
2636 /* TBAA checks did not disambiguate, try individual accesses. */
2637 for (auto access_node
: ref_node
->accesses
)
2639 if (num_tests
>= max_tests
)
2642 if (access_node
.parm_index
== MODREF_GLOBAL_MEMORY_PARM
)
2644 if (global_memory_ok
)
2646 if (ref_may_alias_global_p (ref
, true))
2648 global_memory_ok
= true;
2653 tree arg
= access_node
.get_call_arg (stmt
);
2657 alias_stats
.modref_baseptr_tests
++;
2659 if (integer_zerop (arg
) && flag_delete_null_pointer_checks
)
2662 /* PTA oracle will be unhapy of arg is not an pointer. */
2663 if (!POINTER_TYPE_P (TREE_TYPE (arg
)))
2666 /* If we don't have base pointer, give up. */
2667 if (!ref
->ref
&& !ref
->base
)
2671 if (access_node
.get_ao_ref (stmt
, &ref2
))
2673 ref2
.ref_alias_set
= ref_node
->ref
;
2674 ref2
.base_alias_set
= base_node
->base
;
2675 if (refs_may_alias_p_1 (&ref2
, ref
, tbaa_p
))
2678 else if (ptr_deref_may_alias_ref_p_1 (arg
, ref
))
2688 /* Check if REF conflicts with call using "fn spec" attribute.
2689 If CLOBBER is true we are checking for writes, otherwise check loads.
2691 Return 0 if there are no conflicts (except for possible function call
2692 argument reads), 1 if there are conflicts and -1 if we can not decide by
2696 check_fnspec (gcall
*call
, ao_ref
*ref
, bool clobber
)
2698 attr_fnspec fnspec
= gimple_call_fnspec (call
);
2699 if (fnspec
.known_p ())
2702 ? !fnspec
.global_memory_written_p ()
2703 : !fnspec
.global_memory_read_p ())
2705 for (unsigned int i
= 0; i
< gimple_call_num_args (call
); i
++)
2706 if (POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, i
)))
2707 && (!fnspec
.arg_specified_p (i
)
2708 || (clobber
? fnspec
.arg_maybe_written_p (i
)
2709 : fnspec
.arg_maybe_read_p (i
))))
2712 tree size
= NULL_TREE
;
2713 unsigned int size_arg
;
2715 if (!fnspec
.arg_specified_p (i
))
2717 else if (fnspec
.arg_max_access_size_given_by_arg_p
2719 size
= gimple_call_arg (call
, size_arg
);
2720 else if (fnspec
.arg_access_size_given_by_type_p (i
))
2722 tree callee
= gimple_call_fndecl (call
);
2723 tree t
= TYPE_ARG_TYPES (TREE_TYPE (callee
));
2725 for (unsigned int p
= 0; p
< i
; p
++)
2727 size
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_VALUE (t
)));
2729 poly_int64 size_hwi
;
2731 && poly_int_tree_p (size
, &size_hwi
)
2732 && coeffs_in_range_p (size_hwi
, 0,
2733 HOST_WIDE_INT_MAX
/ BITS_PER_UNIT
))
2735 size_hwi
= size_hwi
* BITS_PER_UNIT
;
2736 ao_ref_init_from_ptr_and_range (&dref
,
2737 gimple_call_arg (call
, i
),
2738 true, 0, -1, size_hwi
);
2741 ao_ref_init_from_ptr_and_range (&dref
,
2742 gimple_call_arg (call
, i
),
2744 if (refs_may_alias_p_1 (&dref
, ref
, false))
2748 && fnspec
.errno_maybe_written_p ()
2750 && targetm
.ref_may_alias_errno (ref
))
2756 /* FIXME: we should handle barriers more consistently, but for now leave the
2758 if (gimple_call_builtin_p (call
, BUILT_IN_NORMAL
))
2759 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call
)))
2761 /* __sync_* builtins and some OpenMP builtins act as threading
2763 #undef DEF_SYNC_BUILTIN
2764 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2765 #include "sync-builtins.def"
2766 #undef DEF_SYNC_BUILTIN
2767 case BUILT_IN_GOMP_ATOMIC_START
:
2768 case BUILT_IN_GOMP_ATOMIC_END
:
2769 case BUILT_IN_GOMP_BARRIER
:
2770 case BUILT_IN_GOMP_BARRIER_CANCEL
:
2771 case BUILT_IN_GOMP_TASKWAIT
:
2772 case BUILT_IN_GOMP_TASKGROUP_END
:
2773 case BUILT_IN_GOMP_CRITICAL_START
:
2774 case BUILT_IN_GOMP_CRITICAL_END
:
2775 case BUILT_IN_GOMP_CRITICAL_NAME_START
:
2776 case BUILT_IN_GOMP_CRITICAL_NAME_END
:
2777 case BUILT_IN_GOMP_LOOP_END
:
2778 case BUILT_IN_GOMP_LOOP_END_CANCEL
:
2779 case BUILT_IN_GOMP_ORDERED_START
:
2780 case BUILT_IN_GOMP_ORDERED_END
:
2781 case BUILT_IN_GOMP_SECTIONS_END
:
2782 case BUILT_IN_GOMP_SECTIONS_END_CANCEL
:
2783 case BUILT_IN_GOMP_SINGLE_COPY_START
:
2784 case BUILT_IN_GOMP_SINGLE_COPY_END
:
2793 /* If the call CALL may use the memory reference REF return true,
2794 otherwise return false. */
2797 ref_maybe_used_by_call_p_1 (gcall
*call
, ao_ref
*ref
, bool tbaa_p
)
2801 int flags
= gimple_call_flags (call
);
2803 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
2806 /* A call that is not without side-effects might involve volatile
2807 accesses and thus conflicts with all other volatile accesses. */
2808 if (ref
->volatile_p
)
2811 if (gimple_call_internal_p (call
))
2812 switch (gimple_call_internal_fn (call
))
2814 case IFN_MASK_STORE
:
2815 case IFN_SCATTER_STORE
:
2816 case IFN_MASK_SCATTER_STORE
:
2818 case IFN_MASK_LEN_STORE
:
2820 case IFN_MASK_STORE_LANES
:
2821 case IFN_MASK_LEN_STORE_LANES
:
2825 case IFN_MASK_LEN_LOAD
:
2826 case IFN_MASK_LOAD_LANES
:
2827 case IFN_MASK_LEN_LOAD_LANES
:
2830 tree lhs
= gimple_call_lhs (call
);
2833 ao_ref_init_from_ptr_and_size (&rhs_ref
,
2834 gimple_call_arg (call
, 0),
2835 TYPE_SIZE_UNIT (TREE_TYPE (lhs
)));
2836 /* We cannot make this a known-size access since otherwise
2837 we disambiguate against refs to decls that are smaller. */
2839 rhs_ref
.ref_alias_set
= rhs_ref
.base_alias_set
2840 = tbaa_p
? get_deref_alias_set (TREE_TYPE
2841 (gimple_call_arg (call
, 1))) : 0;
2842 return refs_may_alias_p_1 (ref
, &rhs_ref
, tbaa_p
);
2849 callee
= gimple_call_fndecl (call
);
2850 if (callee
!= NULL_TREE
)
2852 struct cgraph_node
*node
= cgraph_node::get (callee
);
2853 /* We can not safely optimize based on summary of calle if it does
2854 not always bind to current def: it is possible that memory load
2855 was optimized out earlier and the interposed variant may not be
2856 optimized this way. */
2857 if (node
&& node
->binds_to_current_def_p ())
2859 modref_summary
*summary
= get_modref_function_summary (node
);
2860 if (summary
&& !summary
->calls_interposable
)
2862 if (!modref_may_conflict (call
, summary
->loads
, ref
, tbaa_p
))
2864 alias_stats
.modref_use_no_alias
++;
2865 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2868 "ipa-modref: call stmt ");
2869 print_gimple_stmt (dump_file
, call
, 0);
2871 "ipa-modref: call to %s does not use ",
2872 node
->dump_name ());
2873 if (!ref
->ref
&& ref
->base
)
2875 fprintf (dump_file
, "base: ");
2876 print_generic_expr (dump_file
, ref
->base
);
2880 fprintf (dump_file
, "ref: ");
2881 print_generic_expr (dump_file
, ref
->ref
);
2883 fprintf (dump_file
, " alias sets: %i->%i\n",
2884 ao_ref_base_alias_set (ref
),
2885 ao_ref_alias_set (ref
));
2889 alias_stats
.modref_use_may_alias
++;
2894 base
= ao_ref_base (ref
);
2898 /* If the reference is based on a decl that is not aliased the call
2899 cannot possibly use it. */
2901 && !may_be_aliased (base
)
2902 /* But local statics can be used through recursion. */
2903 && !is_global_var (base
))
2906 if (int res
= check_fnspec (call
, ref
, false))
2914 /* Check if base is a global static variable that is not read
2916 if (callee
!= NULL_TREE
&& VAR_P (base
) && TREE_STATIC (base
))
2918 struct cgraph_node
*node
= cgraph_node::get (callee
);
2922 /* FIXME: Callee can be an OMP builtin that does not have a call graph
2923 node yet. We should enforce that there are nodes for all decls in the
2924 IL and remove this check instead. */
2926 && (id
= ipa_reference_var_uid (base
)) != -1
2927 && (read
= ipa_reference_get_read_global (node
))
2928 && !bitmap_bit_p (read
, id
))
2932 /* Check if the base variable is call-used. */
2935 if (pt_solution_includes (gimple_call_use_set (call
), base
))
2938 else if ((TREE_CODE (base
) == MEM_REF
2939 || TREE_CODE (base
) == TARGET_MEM_REF
)
2940 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
2942 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0));
2946 if (pt_solutions_intersect (gimple_call_use_set (call
), &pi
->pt
))
2952 /* Inspect call arguments for passed-by-value aliases. */
2954 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
2956 tree op
= gimple_call_arg (call
, i
);
2957 int flags
= gimple_call_arg_flags (call
, i
);
2959 if (flags
& (EAF_UNUSED
| EAF_NO_DIRECT_READ
))
2962 if (TREE_CODE (op
) == WITH_SIZE_EXPR
)
2963 op
= TREE_OPERAND (op
, 0);
2965 if (TREE_CODE (op
) != SSA_NAME
2966 && !is_gimple_min_invariant (op
))
2969 ao_ref_init (&r
, op
);
2970 if (refs_may_alias_p_1 (&r
, ref
, tbaa_p
))
2979 ref_maybe_used_by_call_p (gcall
*call
, ao_ref
*ref
, bool tbaa_p
)
2982 res
= ref_maybe_used_by_call_p_1 (call
, ref
, tbaa_p
);
2984 ++alias_stats
.ref_maybe_used_by_call_p_may_alias
;
2986 ++alias_stats
.ref_maybe_used_by_call_p_no_alias
;
2991 /* If the statement STMT may use the memory reference REF return
2992 true, otherwise return false. */
2995 ref_maybe_used_by_stmt_p (gimple
*stmt
, ao_ref
*ref
, bool tbaa_p
)
2997 if (is_gimple_assign (stmt
))
3001 /* All memory assign statements are single. */
3002 if (!gimple_assign_single_p (stmt
))
3005 rhs
= gimple_assign_rhs1 (stmt
);
3006 if (is_gimple_reg (rhs
)
3007 || is_gimple_min_invariant (rhs
)
3008 || gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
)
3011 return refs_may_alias_p (rhs
, ref
, tbaa_p
);
3013 else if (is_gimple_call (stmt
))
3014 return ref_maybe_used_by_call_p (as_a
<gcall
*> (stmt
), ref
, tbaa_p
);
3015 else if (greturn
*return_stmt
= dyn_cast
<greturn
*> (stmt
))
3017 tree retval
= gimple_return_retval (return_stmt
);
3019 && TREE_CODE (retval
) != SSA_NAME
3020 && !is_gimple_min_invariant (retval
)
3021 && refs_may_alias_p (retval
, ref
, tbaa_p
))
3023 /* If ref escapes the function then the return acts as a use. */
3024 tree base
= ao_ref_base (ref
);
3027 else if (DECL_P (base
))
3028 return is_global_var (base
);
3029 else if (TREE_CODE (base
) == MEM_REF
3030 || TREE_CODE (base
) == TARGET_MEM_REF
)
3031 return ptr_deref_may_alias_global_p (TREE_OPERAND (base
, 0), false);
3039 ref_maybe_used_by_stmt_p (gimple
*stmt
, tree ref
, bool tbaa_p
)
3042 ao_ref_init (&r
, ref
);
3043 return ref_maybe_used_by_stmt_p (stmt
, &r
, tbaa_p
);
3046 /* If the call in statement CALL may clobber the memory reference REF
3047 return true, otherwise return false. */
3050 call_may_clobber_ref_p_1 (gcall
*call
, ao_ref
*ref
, bool tbaa_p
)
3055 /* If the call is pure or const it cannot clobber anything. */
3056 if (gimple_call_flags (call
)
3057 & (ECF_PURE
|ECF_CONST
|ECF_LOOPING_CONST_OR_PURE
|ECF_NOVOPS
))
3059 if (gimple_call_internal_p (call
))
3060 switch (auto fn
= gimple_call_internal_fn (call
))
3062 /* Treat these internal calls like ECF_PURE for aliasing,
3063 they don't write to any memory the program should care about.
3064 They have important other side-effects, and read memory,
3065 so can't be ECF_NOVOPS. */
3066 case IFN_UBSAN_NULL
:
3067 case IFN_UBSAN_BOUNDS
:
3068 case IFN_UBSAN_VPTR
:
3069 case IFN_UBSAN_OBJECT_SIZE
:
3071 case IFN_ASAN_CHECK
:
3073 case IFN_MASK_STORE
:
3075 case IFN_MASK_LEN_STORE
:
3076 case IFN_MASK_STORE_LANES
:
3077 case IFN_MASK_LEN_STORE_LANES
:
3079 tree rhs
= gimple_call_arg (call
,
3080 internal_fn_stored_value_index (fn
));
3082 ao_ref_init_from_ptr_and_size (&lhs_ref
, gimple_call_arg (call
, 0),
3083 TYPE_SIZE_UNIT (TREE_TYPE (rhs
)));
3084 /* We cannot make this a known-size access since otherwise
3085 we disambiguate against refs to decls that are smaller. */
3087 lhs_ref
.ref_alias_set
= lhs_ref
.base_alias_set
3088 = tbaa_p
? get_deref_alias_set
3089 (TREE_TYPE (gimple_call_arg (call
, 1))) : 0;
3090 return refs_may_alias_p_1 (ref
, &lhs_ref
, tbaa_p
);
3096 callee
= gimple_call_fndecl (call
);
3098 if (callee
!= NULL_TREE
&& !ref
->volatile_p
)
3100 struct cgraph_node
*node
= cgraph_node::get (callee
);
3103 modref_summary
*summary
= get_modref_function_summary (node
);
3106 if (!modref_may_conflict (call
, summary
->stores
, ref
, tbaa_p
)
3107 && (!summary
->writes_errno
3108 || !targetm
.ref_may_alias_errno (ref
)))
3110 alias_stats
.modref_clobber_no_alias
++;
3111 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3114 "ipa-modref: call stmt ");
3115 print_gimple_stmt (dump_file
, call
, 0);
3117 "ipa-modref: call to %s does not clobber ",
3118 node
->dump_name ());
3119 if (!ref
->ref
&& ref
->base
)
3121 fprintf (dump_file
, "base: ");
3122 print_generic_expr (dump_file
, ref
->base
);
3126 fprintf (dump_file
, "ref: ");
3127 print_generic_expr (dump_file
, ref
->ref
);
3129 fprintf (dump_file
, " alias sets: %i->%i\n",
3130 ao_ref_base_alias_set (ref
),
3131 ao_ref_alias_set (ref
));
3135 alias_stats
.modref_clobber_may_alias
++;
3140 base
= ao_ref_base (ref
);
3144 if (TREE_CODE (base
) == SSA_NAME
3145 || CONSTANT_CLASS_P (base
))
3148 /* A call that is not without side-effects might involve volatile
3149 accesses and thus conflicts with all other volatile accesses. */
3150 if (ref
->volatile_p
)
3153 /* If the reference is based on a decl that is not aliased the call
3154 cannot possibly clobber it. */
3156 && !may_be_aliased (base
)
3157 /* But local non-readonly statics can be modified through recursion
3158 or the call may implement a threading barrier which we must
3159 treat as may-def. */
3160 && (TREE_READONLY (base
)
3161 || !is_global_var (base
)))
3164 /* If the reference is based on a pointer that points to memory
3165 that may not be written to then the call cannot possibly clobber it. */
3166 if ((TREE_CODE (base
) == MEM_REF
3167 || TREE_CODE (base
) == TARGET_MEM_REF
)
3168 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
3169 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base
, 0)))
3172 if (int res
= check_fnspec (call
, ref
, true))
3180 /* Check if base is a global static variable that is not written
3182 if (callee
!= NULL_TREE
&& VAR_P (base
) && TREE_STATIC (base
))
3184 struct cgraph_node
*node
= cgraph_node::get (callee
);
3189 && (id
= ipa_reference_var_uid (base
)) != -1
3190 && (written
= ipa_reference_get_written_global (node
))
3191 && !bitmap_bit_p (written
, id
))
3195 /* Check if the base variable is call-clobbered. */
3197 return pt_solution_includes (gimple_call_clobber_set (call
), base
);
3198 else if ((TREE_CODE (base
) == MEM_REF
3199 || TREE_CODE (base
) == TARGET_MEM_REF
)
3200 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
3202 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0));
3206 return pt_solutions_intersect (gimple_call_clobber_set (call
), &pi
->pt
);
3212 /* If the call in statement CALL may clobber the memory reference REF
3213 return true, otherwise return false. */
3216 call_may_clobber_ref_p (gcall
*call
, tree ref
, bool tbaa_p
)
3220 ao_ref_init (&r
, ref
);
3221 res
= call_may_clobber_ref_p_1 (call
, &r
, tbaa_p
);
3223 ++alias_stats
.call_may_clobber_ref_p_may_alias
;
3225 ++alias_stats
.call_may_clobber_ref_p_no_alias
;
3230 /* If the statement STMT may clobber the memory reference REF return true,
3231 otherwise return false. */
3234 stmt_may_clobber_ref_p_1 (gimple
*stmt
, ao_ref
*ref
, bool tbaa_p
)
3236 if (is_gimple_call (stmt
))
3238 tree lhs
= gimple_call_lhs (stmt
);
3240 && TREE_CODE (lhs
) != SSA_NAME
)
3243 ao_ref_init (&r
, lhs
);
3244 if (refs_may_alias_p_1 (ref
, &r
, tbaa_p
))
3248 return call_may_clobber_ref_p_1 (as_a
<gcall
*> (stmt
), ref
, tbaa_p
);
3250 else if (gimple_assign_single_p (stmt
))
3252 tree lhs
= gimple_assign_lhs (stmt
);
3253 if (TREE_CODE (lhs
) != SSA_NAME
)
3256 ao_ref_init (&r
, lhs
);
3257 return refs_may_alias_p_1 (ref
, &r
, tbaa_p
);
3260 else if (gimple_code (stmt
) == GIMPLE_ASM
)
3267 stmt_may_clobber_ref_p (gimple
*stmt
, tree ref
, bool tbaa_p
)
3270 ao_ref_init (&r
, ref
);
3271 return stmt_may_clobber_ref_p_1 (stmt
, &r
, tbaa_p
);
3274 /* Return true if store1 and store2 described by corresponding tuples
3275 <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
3279 same_addr_size_stores_p (tree base1
, poly_int64 offset1
, poly_int64 size1
,
3280 poly_int64 max_size1
,
3281 tree base2
, poly_int64 offset2
, poly_int64 size2
,
3282 poly_int64 max_size2
)
3284 /* Offsets need to be 0. */
3285 if (maybe_ne (offset1
, 0)
3286 || maybe_ne (offset2
, 0))
3289 bool base1_obj_p
= SSA_VAR_P (base1
);
3290 bool base2_obj_p
= SSA_VAR_P (base2
);
3292 /* We need one object. */
3293 if (base1_obj_p
== base2_obj_p
)
3295 tree obj
= base1_obj_p
? base1
: base2
;
3297 /* And we need one MEM_REF. */
3298 bool base1_memref_p
= TREE_CODE (base1
) == MEM_REF
;
3299 bool base2_memref_p
= TREE_CODE (base2
) == MEM_REF
;
3300 if (base1_memref_p
== base2_memref_p
)
3302 tree memref
= base1_memref_p
? base1
: base2
;
3304 /* Sizes need to be valid. */
3305 if (!known_size_p (max_size1
)
3306 || !known_size_p (max_size2
)
3307 || !known_size_p (size1
)
3308 || !known_size_p (size2
))
3311 /* Max_size needs to match size. */
3312 if (maybe_ne (max_size1
, size1
)
3313 || maybe_ne (max_size2
, size2
))
3316 /* Sizes need to match. */
3317 if (maybe_ne (size1
, size2
))
3321 /* Check that memref is a store to pointer with singleton points-to info. */
3322 if (!integer_zerop (TREE_OPERAND (memref
, 1)))
3324 tree ptr
= TREE_OPERAND (memref
, 0);
3325 if (TREE_CODE (ptr
) != SSA_NAME
)
3327 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (ptr
);
3328 unsigned int pt_uid
;
3330 || !pt_solution_singleton_or_null_p (&pi
->pt
, &pt_uid
))
3333 /* Be conservative with non-call exceptions when the address might
3335 if (cfun
->can_throw_non_call_exceptions
&& pi
->pt
.null
)
3338 /* Check that ptr points relative to obj. */
3339 unsigned int obj_uid
= DECL_PT_UID (obj
);
3340 if (obj_uid
!= pt_uid
)
3343 /* Check that the object size is the same as the store size. That ensures us
3344 that ptr points to the start of obj. */
3345 return (DECL_SIZE (obj
)
3346 && poly_int_tree_p (DECL_SIZE (obj
))
3347 && known_eq (wi::to_poly_offset (DECL_SIZE (obj
)), size1
));
3350 /* Return true if REF is killed by an store described by
3351 BASE, OFFSET, SIZE and MAX_SIZE. */
3354 store_kills_ref_p (tree base
, poly_int64 offset
, poly_int64 size
,
3355 poly_int64 max_size
, ao_ref
*ref
)
3357 poly_int64 ref_offset
= ref
->offset
;
3358 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
3359 so base == ref->base does not always hold. */
3360 if (base
!= ref
->base
)
3362 /* Try using points-to info. */
3363 if (same_addr_size_stores_p (base
, offset
, size
, max_size
, ref
->base
,
3364 ref
->offset
, ref
->size
, ref
->max_size
))
3367 /* If both base and ref->base are MEM_REFs, only compare the
3368 first operand, and if the second operand isn't equal constant,
3369 try to add the offsets into offset and ref_offset. */
3370 if (TREE_CODE (base
) == MEM_REF
&& TREE_CODE (ref
->base
) == MEM_REF
3371 && TREE_OPERAND (base
, 0) == TREE_OPERAND (ref
->base
, 0))
3373 if (!tree_int_cst_equal (TREE_OPERAND (base
, 1),
3374 TREE_OPERAND (ref
->base
, 1)))
3376 poly_offset_int off1
= mem_ref_offset (base
);
3377 off1
<<= LOG2_BITS_PER_UNIT
;
3379 poly_offset_int off2
= mem_ref_offset (ref
->base
);
3380 off2
<<= LOG2_BITS_PER_UNIT
;
3382 if (!off1
.to_shwi (&offset
) || !off2
.to_shwi (&ref_offset
))
3389 /* For a must-alias check we need to be able to constrain
3390 the access properly. */
3391 return (known_eq (size
, max_size
)
3392 && known_subrange_p (ref_offset
, ref
->max_size
, offset
, size
));
3395 /* If STMT kills the memory reference REF return true, otherwise
3399 stmt_kills_ref_p (gimple
*stmt
, ao_ref
*ref
)
3401 if (!ao_ref_base (ref
))
3404 if (gimple_has_lhs (stmt
)
3405 && TREE_CODE (gimple_get_lhs (stmt
)) != SSA_NAME
3406 /* The assignment is not necessarily carried out if it can throw
3407 and we can catch it in the current function where we could inspect
3408 the previous value. Similarly if the function can throw externally
3409 and the ref does not die on the function return.
3410 ??? We only need to care about the RHS throwing. For aggregate
3411 assignments or similar calls and non-call exceptions the LHS
3412 might throw as well.
3413 ??? We also should care about possible longjmp, but since we
3414 do not understand that longjmp is not using global memory we will
3415 not consider a kill here since the function call will be considered
3416 as possibly using REF. */
3417 && !stmt_can_throw_internal (cfun
, stmt
)
3418 && (!stmt_can_throw_external (cfun
, stmt
)
3419 || !ref_may_alias_global_p (ref
, false)))
3421 tree lhs
= gimple_get_lhs (stmt
);
3422 /* If LHS is literally a base of the access we are done. */
3425 tree base
= ref
->ref
;
3426 tree innermost_dropped_array_ref
= NULL_TREE
;
3427 if (handled_component_p (base
))
3429 tree saved_lhs0
= NULL_TREE
;
3430 if (handled_component_p (lhs
))
3432 saved_lhs0
= TREE_OPERAND (lhs
, 0);
3433 TREE_OPERAND (lhs
, 0) = integer_zero_node
;
3437 /* Just compare the outermost handled component, if
3438 they are equal we have found a possible common
3440 tree saved_base0
= TREE_OPERAND (base
, 0);
3441 TREE_OPERAND (base
, 0) = integer_zero_node
;
3442 bool res
= operand_equal_p (lhs
, base
, 0);
3443 TREE_OPERAND (base
, 0) = saved_base0
;
3446 /* Remember if we drop an array-ref that we need to
3447 double-check not being at struct end. */
3448 if (TREE_CODE (base
) == ARRAY_REF
3449 || TREE_CODE (base
) == ARRAY_RANGE_REF
)
3450 innermost_dropped_array_ref
= base
;
3451 /* Otherwise drop handled components of the access. */
3454 while (handled_component_p (base
));
3456 TREE_OPERAND (lhs
, 0) = saved_lhs0
;
3458 /* Finally check if the lhs has the same address and size as the
3459 base candidate of the access. Watch out if we have dropped
3460 an array-ref that might have flexible size, this means ref->ref
3461 may be outside of the TYPE_SIZE of its base. */
3462 if ((! innermost_dropped_array_ref
3463 || ! array_ref_flexible_size_p (innermost_dropped_array_ref
))
3465 || (((TYPE_SIZE (TREE_TYPE (lhs
))
3466 == TYPE_SIZE (TREE_TYPE (base
)))
3467 || (TYPE_SIZE (TREE_TYPE (lhs
))
3468 && TYPE_SIZE (TREE_TYPE (base
))
3469 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs
)),
3470 TYPE_SIZE (TREE_TYPE (base
)),
3472 && operand_equal_p (lhs
, base
,
3474 | OEP_MATCH_SIDE_EFFECTS
))))
3476 ++alias_stats
.stmt_kills_ref_p_yes
;
3481 /* Now look for non-literal equal bases with the restriction of
3482 handling constant offset and size. */
3483 /* For a must-alias check we need to be able to constrain
3484 the access properly. */
3485 if (!ref
->max_size_known_p ())
3487 ++alias_stats
.stmt_kills_ref_p_no
;
3490 poly_int64 size
, offset
, max_size
;
3492 tree base
= get_ref_base_and_extent (lhs
, &offset
, &size
, &max_size
,
3494 if (store_kills_ref_p (base
, offset
, size
, max_size
, ref
))
3496 ++alias_stats
.stmt_kills_ref_p_yes
;
3501 if (is_gimple_call (stmt
))
3503 tree callee
= gimple_call_fndecl (stmt
);
3504 struct cgraph_node
*node
;
3505 modref_summary
*summary
;
3507 /* Try to disambiguate using modref summary. Modref records a vector
3508 of stores with known offsets relative to function parameters that must
3509 happen every execution of function. Find if we have a matching
3510 store and verify that function can not use the value. */
3511 if (callee
!= NULL_TREE
3512 && (node
= cgraph_node::get (callee
)) != NULL
3513 && node
->binds_to_current_def_p ()
3514 && (summary
= get_modref_function_summary (node
)) != NULL
3515 && summary
->kills
.length ()
3516 /* Check that we can not trap while evaulating function
3517 parameters. This check is overly conservative. */
3518 && (!cfun
->can_throw_non_call_exceptions
3519 || (!stmt_can_throw_internal (cfun
, stmt
)
3520 && (!stmt_can_throw_external (cfun
, stmt
)
3521 || !ref_may_alias_global_p (ref
, false)))))
3523 for (auto kill
: summary
->kills
)
3527 /* We only can do useful compares if we know the access range
3529 if (!kill
.get_ao_ref (as_a
<gcall
*> (stmt
), &dref
))
3531 if (store_kills_ref_p (ao_ref_base (&dref
), dref
.offset
,
3532 dref
.size
, dref
.max_size
, ref
))
3534 /* For store to be killed it needs to not be used
3536 if (ref_maybe_used_by_call_p_1 (as_a
<gcall
*> (stmt
), ref
,
3538 || !dbg_cnt (ipa_mod_ref
))
3540 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3543 "ipa-modref: call stmt ");
3544 print_gimple_stmt (dump_file
, stmt
, 0);
3546 "ipa-modref: call to %s kills ",
3547 node
->dump_name ());
3548 print_generic_expr (dump_file
, ref
->base
);
3549 fprintf (dump_file
, "\n");
3551 ++alias_stats
.modref_kill_yes
;
3555 ++alias_stats
.modref_kill_no
;
3557 if (callee
!= NULL_TREE
3558 && gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
3559 switch (DECL_FUNCTION_CODE (callee
))
3563 tree ptr
= gimple_call_arg (stmt
, 0);
3564 tree base
= ao_ref_base (ref
);
3565 if (base
&& TREE_CODE (base
) == MEM_REF
3566 && TREE_OPERAND (base
, 0) == ptr
)
3568 ++alias_stats
.stmt_kills_ref_p_yes
;
3574 case BUILT_IN_MEMCPY
:
3575 case BUILT_IN_MEMPCPY
:
3576 case BUILT_IN_MEMMOVE
:
3577 case BUILT_IN_MEMSET
:
3578 case BUILT_IN_MEMCPY_CHK
:
3579 case BUILT_IN_MEMPCPY_CHK
:
3580 case BUILT_IN_MEMMOVE_CHK
:
3581 case BUILT_IN_MEMSET_CHK
:
3582 case BUILT_IN_STRNCPY
:
3583 case BUILT_IN_STPNCPY
:
3584 case BUILT_IN_CALLOC
:
3586 /* For a must-alias check we need to be able to constrain
3587 the access properly. */
3588 if (!ref
->max_size_known_p ())
3590 ++alias_stats
.stmt_kills_ref_p_no
;
3596 /* In execution order a calloc call will never kill
3597 anything. However, DSE will (ab)use this interface
3598 to ask if a calloc call writes the same memory locations
3599 as a later assignment, memset, etc. So handle calloc
3600 in the expected way. */
3601 if (DECL_FUNCTION_CODE (callee
) == BUILT_IN_CALLOC
)
3603 tree arg0
= gimple_call_arg (stmt
, 0);
3604 tree arg1
= gimple_call_arg (stmt
, 1);
3605 if (TREE_CODE (arg0
) != INTEGER_CST
3606 || TREE_CODE (arg1
) != INTEGER_CST
)
3608 ++alias_stats
.stmt_kills_ref_p_no
;
3612 dest
= gimple_call_lhs (stmt
);
3615 ++alias_stats
.stmt_kills_ref_p_no
;
3618 len
= fold_build2 (MULT_EXPR
, TREE_TYPE (arg0
), arg0
, arg1
);
3622 dest
= gimple_call_arg (stmt
, 0);
3623 len
= gimple_call_arg (stmt
, 2);
3625 if (!poly_int_tree_p (len
))
3628 ao_ref_init_from_ptr_and_size (&dref
, dest
, len
);
3629 if (store_kills_ref_p (ao_ref_base (&dref
), dref
.offset
,
3630 dref
.size
, dref
.max_size
, ref
))
3632 ++alias_stats
.stmt_kills_ref_p_yes
;
3638 case BUILT_IN_VA_END
:
3640 tree ptr
= gimple_call_arg (stmt
, 0);
3641 if (TREE_CODE (ptr
) == ADDR_EXPR
)
3643 tree base
= ao_ref_base (ref
);
3644 if (TREE_OPERAND (ptr
, 0) == base
)
3646 ++alias_stats
.stmt_kills_ref_p_yes
;
3656 ++alias_stats
.stmt_kills_ref_p_no
;
3661 stmt_kills_ref_p (gimple
*stmt
, tree ref
)
3664 ao_ref_init (&r
, ref
);
3665 return stmt_kills_ref_p (stmt
, &r
);
3669 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3670 TARGET or a statement clobbering the memory reference REF in which
3671 case false is returned. The walk starts with VUSE, one argument of PHI. */
3674 maybe_skip_until (gimple
*phi
, tree
&target
, basic_block target_bb
,
3675 ao_ref
*ref
, tree vuse
, bool tbaa_p
, unsigned int &limit
,
3676 bitmap
*visited
, bool abort_on_visited
,
3677 void *(*translate
)(ao_ref
*, tree
, void *, translate_flags
*),
3678 translate_flags disambiguate_only
,
3681 basic_block bb
= gimple_bb (phi
);
3685 *visited
= BITMAP_ALLOC (NULL
);
3686 bitmap_tree_view (*visited
);
3689 bitmap_set_bit (*visited
, SSA_NAME_VERSION (PHI_RESULT (phi
)));
3691 /* Walk until we hit the target. */
3692 while (vuse
!= target
)
3694 gimple
*def_stmt
= SSA_NAME_DEF_STMT (vuse
);
3695 /* If we are searching for the target VUSE by walking up to
3696 TARGET_BB dominating the original PHI we are finished once
3697 we reach a default def or a definition in a block dominating
3698 that block. Update TARGET and return. */
3700 && (gimple_nop_p (def_stmt
)
3701 || dominated_by_p (CDI_DOMINATORS
,
3702 target_bb
, gimple_bb (def_stmt
))))
3708 /* Recurse for PHI nodes. */
3709 if (gimple_code (def_stmt
) == GIMPLE_PHI
)
3711 /* An already visited PHI node ends the walk successfully. */
3712 if (bitmap_bit_p (*visited
, SSA_NAME_VERSION (PHI_RESULT (def_stmt
))))
3713 return !abort_on_visited
;
3714 vuse
= get_continuation_for_phi (def_stmt
, ref
, tbaa_p
, limit
,
3715 visited
, abort_on_visited
,
3716 translate
, data
, disambiguate_only
);
3721 else if (gimple_nop_p (def_stmt
))
3725 /* A clobbering statement or the end of the IL ends it failing. */
3726 if ((int)limit
<= 0)
3729 if (stmt_may_clobber_ref_p_1 (def_stmt
, ref
, tbaa_p
))
3731 translate_flags tf
= disambiguate_only
;
3733 && (*translate
) (ref
, vuse
, data
, &tf
) == NULL
)
3739 /* If we reach a new basic-block see if we already skipped it
3740 in a previous walk that ended successfully. */
3741 if (gimple_bb (def_stmt
) != bb
)
3743 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (vuse
)))
3744 return !abort_on_visited
;
3745 bb
= gimple_bb (def_stmt
);
3747 vuse
= gimple_vuse (def_stmt
);
3753 /* Starting from a PHI node for the virtual operand of the memory reference
3754 REF find a continuation virtual operand that allows to continue walking
3755 statements dominating PHI skipping only statements that cannot possibly
3756 clobber REF. Decrements LIMIT for each alias disambiguation done
3757 and aborts the walk, returning NULL_TREE if it reaches zero.
3758 Returns NULL_TREE if no suitable virtual operand can be found. */
3761 get_continuation_for_phi (gimple
*phi
, ao_ref
*ref
, bool tbaa_p
,
3762 unsigned int &limit
, bitmap
*visited
,
3763 bool abort_on_visited
,
3764 void *(*translate
)(ao_ref
*, tree
, void *,
3767 translate_flags disambiguate_only
)
3769 unsigned nargs
= gimple_phi_num_args (phi
);
3771 /* Through a single-argument PHI we can simply look through. */
3773 return PHI_ARG_DEF (phi
, 0);
3775 /* For two or more arguments try to pairwise skip non-aliasing code
3776 until we hit the phi argument definition that dominates the other one. */
3777 basic_block phi_bb
= gimple_bb (phi
);
3781 /* Find a candidate for the virtual operand which definition
3782 dominates those of all others. */
3783 /* First look if any of the args themselves satisfy this. */
3784 for (i
= 0; i
< nargs
; ++i
)
3786 arg0
= PHI_ARG_DEF (phi
, i
);
3787 if (SSA_NAME_IS_DEFAULT_DEF (arg0
))
3789 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (arg0
));
3790 if (def_bb
!= phi_bb
3791 && dominated_by_p (CDI_DOMINATORS
, phi_bb
, def_bb
))
3795 /* If not, look if we can reach such candidate by walking defs
3796 until we hit the immediate dominator. maybe_skip_until will
3798 basic_block dom
= get_immediate_dominator (CDI_DOMINATORS
, phi_bb
);
3800 /* Then check against the (to be) found candidate. */
3801 for (i
= 0; i
< nargs
; ++i
)
3803 arg1
= PHI_ARG_DEF (phi
, i
);
3806 else if (! maybe_skip_until (phi
, arg0
, dom
, ref
, arg1
, tbaa_p
,
3810 /* Do not valueize when walking over
3814 gimple_bb (SSA_NAME_DEF_STMT (arg1
)),
3817 : disambiguate_only
, data
))
3824 /* Based on the memory reference REF and its virtual use VUSE call
3825 WALKER for each virtual use that is equivalent to VUSE, including VUSE
3826 itself. That is, for each virtual use for which its defining statement
3827 does not clobber REF.
3829 WALKER is called with REF, the current virtual use and DATA. If
3830 WALKER returns non-NULL the walk stops and its result is returned.
3831 At the end of a non-successful walk NULL is returned.
3833 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3834 use which definition is a statement that may clobber REF and DATA.
3835 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3836 If TRANSLATE returns non-NULL the walk stops and its result is returned.
3837 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3838 to adjust REF and *DATA to make that valid.
3840 VALUEIZE if non-NULL is called with the next VUSE that is considered
3841 and return value is substituted for that. This can be used to
3842 implement optimistic value-numbering for example. Note that the
3843 VUSE argument is assumed to be valueized already.
3845 LIMIT specifies the number of alias queries we are allowed to do,
3846 the walk stops when it reaches zero and NULL is returned. LIMIT
3847 is decremented by the number of alias queries (plus adjustments
3848 done by the callbacks) upon return.
3850 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3853 walk_non_aliased_vuses (ao_ref
*ref
, tree vuse
, bool tbaa_p
,
3854 void *(*walker
)(ao_ref
*, tree
, void *),
3855 void *(*translate
)(ao_ref
*, tree
, void *,
3857 tree (*valueize
)(tree
),
3858 unsigned &limit
, void *data
)
3860 bitmap visited
= NULL
;
3862 bool translated
= false;
3864 timevar_push (TV_ALIAS_STMT_WALK
);
3870 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3871 res
= (*walker
) (ref
, vuse
, data
);
3873 if (res
== (void *)-1)
3878 /* Lookup succeeded. */
3879 else if (res
!= NULL
)
3884 vuse
= valueize (vuse
);
3891 def_stmt
= SSA_NAME_DEF_STMT (vuse
);
3892 if (gimple_nop_p (def_stmt
))
3894 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
3895 vuse
= get_continuation_for_phi (def_stmt
, ref
, tbaa_p
, limit
,
3896 &visited
, translated
, translate
, data
);
3899 if ((int)limit
<= 0)
3905 if (stmt_may_clobber_ref_p_1 (def_stmt
, ref
, tbaa_p
))
3909 translate_flags disambiguate_only
= TR_TRANSLATE
;
3910 res
= (*translate
) (ref
, vuse
, data
, &disambiguate_only
);
3911 /* Failed lookup and translation. */
3912 if (res
== (void *)-1)
3917 /* Lookup succeeded. */
3918 else if (res
!= NULL
)
3920 /* Translation succeeded, continue walking. */
3921 translated
= translated
|| disambiguate_only
== TR_TRANSLATE
;
3923 vuse
= gimple_vuse (def_stmt
);
3929 BITMAP_FREE (visited
);
3931 timevar_pop (TV_ALIAS_STMT_WALK
);
3937 /* Based on the memory reference REF call WALKER for each vdef whose
3938 defining statement may clobber REF, starting with VDEF. If REF
3939 is NULL_TREE, each defining statement is visited.
3941 WALKER is called with REF, the current vdef and DATA. If WALKER
3942 returns true the walk is stopped, otherwise it continues.
3944 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
3945 The pointer may be NULL and then we do not track this information.
3947 At PHI nodes walk_aliased_vdefs forks into one walk for each
3948 PHI argument (but only one walk continues at merge points), the
3949 return value is true if any of the walks was successful.
3951 The function returns the number of statements walked or -1 if
3952 LIMIT stmts were walked and the walk was aborted at this point.
3953 If LIMIT is zero the walk is not aborted. */
3956 walk_aliased_vdefs_1 (ao_ref
*ref
, tree vdef
,
3957 bool (*walker
)(ao_ref
*, tree
, void *), void *data
,
3958 bitmap
*visited
, unsigned int cnt
,
3959 bool *function_entry_reached
, unsigned limit
)
3963 gimple
*def_stmt
= SSA_NAME_DEF_STMT (vdef
);
3966 && !bitmap_set_bit (*visited
, SSA_NAME_VERSION (vdef
)))
3969 if (gimple_nop_p (def_stmt
))
3971 if (function_entry_reached
)
3972 *function_entry_reached
= true;
3975 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
3980 *visited
= BITMAP_ALLOC (NULL
);
3981 bitmap_tree_view (*visited
);
3983 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); ++i
)
3985 int res
= walk_aliased_vdefs_1 (ref
,
3986 gimple_phi_arg_def (def_stmt
, i
),
3987 walker
, data
, visited
, cnt
,
3988 function_entry_reached
, limit
);
3996 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
4001 || stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
4002 && (*walker
) (ref
, vdef
, data
))
4005 vdef
= gimple_vuse (def_stmt
);
4011 walk_aliased_vdefs (ao_ref
*ref
, tree vdef
,
4012 bool (*walker
)(ao_ref
*, tree
, void *), void *data
,
4014 bool *function_entry_reached
, unsigned int limit
)
4016 bitmap local_visited
= NULL
;
4019 timevar_push (TV_ALIAS_STMT_WALK
);
4021 if (function_entry_reached
)
4022 *function_entry_reached
= false;
4024 ret
= walk_aliased_vdefs_1 (ref
, vdef
, walker
, data
,
4025 visited
? visited
: &local_visited
, 0,
4026 function_entry_reached
, limit
);
4028 BITMAP_FREE (local_visited
);
4030 timevar_pop (TV_ALIAS_STMT_WALK
);
4035 /* Verify validity of the fnspec string.
4036 See attr-fnspec.h for details. */
4039 attr_fnspec::verify ()
4045 /* Check return value specifier. */
4046 if (len
< return_desc_size
)
4048 else if ((len
- return_desc_size
) % arg_desc_size
)
4050 else if ((str
[0] < '1' || str
[0] > '4')
4051 && str
[0] != '.' && str
[0] != 'm')
4066 internal_error ("invalid fn spec attribute \"%s\"", str
);
4068 /* Now check all parameters. */
4069 for (unsigned int i
= 0; arg_specified_p (i
); i
++)
4071 unsigned int idx
= arg_idx (i
);
4083 if ((str
[idx
+ 1] >= '1' && str
[idx
+ 1] <= '9')
4084 || str
[idx
+ 1] == 't')
4086 if (str
[idx
] != 'r' && str
[idx
] != 'R'
4087 && str
[idx
] != 'w' && str
[idx
] != 'W'
4088 && str
[idx
] != 'o' && str
[idx
] != 'O')
4090 if (str
[idx
+ 1] != 't'
4091 /* Size specified is scalar, so it should be described
4092 by ". " if specified at all. */
4093 && (arg_specified_p (str
[idx
+ 1] - '1')
4094 && str
[arg_idx (str
[idx
+ 1] - '1')] != '.'))
4097 else if (str
[idx
+ 1] != ' ')
4101 if (str
[idx
] < '1' || str
[idx
] > '9')
4105 internal_error ("invalid fn spec attribute \"%s\" arg %i", str
, i
);
4109 /* Return ture if TYPE1 and TYPE2 will always give the same answer
4110 when compared wit hother types using same_type_for_tbaa_p. */
4113 types_equal_for_same_type_for_tbaa_p (tree type1
, tree type2
,
4114 bool lto_streaming_safe
)
4116 /* We use same_type_for_tbaa_p to match types in the access path.
4117 This check is overly conservative. */
4118 type1
= TYPE_MAIN_VARIANT (type1
);
4119 type2
= TYPE_MAIN_VARIANT (type2
);
4121 if (TYPE_STRUCTURAL_EQUALITY_P (type1
)
4122 != TYPE_STRUCTURAL_EQUALITY_P (type2
))
4124 if (TYPE_STRUCTURAL_EQUALITY_P (type1
))
4127 if (lto_streaming_safe
)
4128 return type1
== type2
;
4130 return TYPE_CANONICAL (type1
) == TYPE_CANONICAL (type2
);
4133 /* Compare REF1 and REF2 and return flags specifying their differences.
4134 If LTO_STREAMING_SAFE is true do not use alias sets and canonical
4135 types that are going to be recomputed.
4136 If TBAA is true also compare TBAA metadata. */
4139 ao_compare::compare_ao_refs (ao_ref
*ref1
, ao_ref
*ref2
,
4140 bool lto_streaming_safe
,
4143 if (TREE_THIS_VOLATILE (ref1
->ref
) != TREE_THIS_VOLATILE (ref2
->ref
))
4145 tree base1
= ao_ref_base (ref1
);
4146 tree base2
= ao_ref_base (ref2
);
4148 if (!known_eq (ref1
->offset
, ref2
->offset
)
4149 || !known_eq (ref1
->size
, ref2
->size
)
4150 || !known_eq (ref1
->max_size
, ref2
->max_size
))
4153 /* For variable accesses we need to compare actual paths
4154 to check that both refs are accessing same address and the access size. */
4155 if (!known_eq (ref1
->size
, ref1
->max_size
))
4157 if (!operand_equal_p (TYPE_SIZE (TREE_TYPE (ref1
->ref
)),
4158 TYPE_SIZE (TREE_TYPE (ref2
->ref
)), 0))
4160 tree r1
= ref1
->ref
;
4161 tree r2
= ref2
->ref
;
4163 /* Handle toplevel COMPONENT_REFs of bitfields.
4164 Those are special since they are not allowed in
4166 if (TREE_CODE (r1
) == COMPONENT_REF
4167 && DECL_BIT_FIELD (TREE_OPERAND (r1
, 1)))
4169 if (TREE_CODE (r2
) != COMPONENT_REF
4170 || !DECL_BIT_FIELD (TREE_OPERAND (r2
, 1)))
4172 tree field1
= TREE_OPERAND (r1
, 1);
4173 tree field2
= TREE_OPERAND (r2
, 1);
4174 if (!operand_equal_p (DECL_FIELD_OFFSET (field1
),
4175 DECL_FIELD_OFFSET (field2
), 0)
4176 || !operand_equal_p (DECL_FIELD_BIT_OFFSET (field1
),
4177 DECL_FIELD_BIT_OFFSET (field2
), 0)
4178 || !operand_equal_p (DECL_SIZE (field1
), DECL_SIZE (field2
), 0)
4179 || !types_compatible_p (TREE_TYPE (r1
),
4182 r1
= TREE_OPERAND (r1
, 0);
4183 r2
= TREE_OPERAND (r2
, 0);
4185 else if (TREE_CODE (r2
) == COMPONENT_REF
4186 && DECL_BIT_FIELD (TREE_OPERAND (r2
, 1)))
4189 /* Similarly for bit field refs. */
4190 if (TREE_CODE (r1
) == BIT_FIELD_REF
)
4192 if (TREE_CODE (r2
) != BIT_FIELD_REF
4193 || !operand_equal_p (TREE_OPERAND (r1
, 1),
4194 TREE_OPERAND (r2
, 1), 0)
4195 || !operand_equal_p (TREE_OPERAND (r1
, 2),
4196 TREE_OPERAND (r2
, 2), 0)
4197 || !types_compatible_p (TREE_TYPE (r1
),
4200 r1
= TREE_OPERAND (r1
, 0);
4201 r2
= TREE_OPERAND (r2
, 0);
4203 else if (TREE_CODE (r2
) == BIT_FIELD_REF
)
4206 /* Now we can compare the address of actual memory access. */
4207 if (!operand_equal_p (r1
, r2
, OEP_ADDRESS_OF
| OEP_MATCH_SIDE_EFFECTS
))
4210 /* For constant accesses we get more matches by comparing offset only. */
4211 else if (!operand_equal_p (base1
, base2
,
4212 OEP_ADDRESS_OF
| OEP_MATCH_SIDE_EFFECTS
))
4215 /* We can't simply use get_object_alignment_1 on the full
4216 reference as for accesses with variable indexes this reports
4217 too conservative alignment. */
4218 unsigned int align1
, align2
;
4219 unsigned HOST_WIDE_INT bitpos1
, bitpos2
;
4220 bool known1
= get_object_alignment_1 (base1
, &align1
, &bitpos1
);
4221 bool known2
= get_object_alignment_1 (base2
, &align2
, &bitpos2
);
4222 /* ??? For MEMREF get_object_alignment_1 determines aligned from
4223 TYPE_ALIGN but still returns false. This seem to contradict
4224 its description. So compare even if alignment is unknown. */
4225 if (known1
!= known2
4226 || (bitpos1
!= bitpos2
|| align1
!= align2
))
4229 /* Now we know that accesses are semantically same. */
4232 /* ao_ref_base strips inner MEM_REF [&decl], recover from that here. */
4233 tree rbase1
= ref1
->ref
;
4235 while (handled_component_p (rbase1
))
4236 rbase1
= TREE_OPERAND (rbase1
, 0);
4237 tree rbase2
= ref2
->ref
;
4238 while (handled_component_p (rbase2
))
4239 rbase2
= TREE_OPERAND (rbase2
, 0);
4241 /* MEM_REFs and TARGET_MEM_REFs record dependence cliques which are used to
4242 implement restrict pointers. MR_DEPENDENCE_CLIQUE 0 means no information.
4243 Otherwise we need to match bases and cliques. */
4244 if ((((TREE_CODE (rbase1
) == MEM_REF
|| TREE_CODE (rbase1
) == TARGET_MEM_REF
)
4245 && MR_DEPENDENCE_CLIQUE (rbase1
))
4246 || ((TREE_CODE (rbase2
) == MEM_REF
|| TREE_CODE (rbase2
) == TARGET_MEM_REF
)
4247 && MR_DEPENDENCE_CLIQUE (rbase2
)))
4248 && (TREE_CODE (rbase1
) != TREE_CODE (rbase2
)
4249 || MR_DEPENDENCE_CLIQUE (rbase1
) != MR_DEPENDENCE_CLIQUE (rbase2
)
4250 || (MR_DEPENDENCE_BASE (rbase1
) != MR_DEPENDENCE_BASE (rbase2
))))
4251 flags
|= DEPENDENCE_CLIQUE
;
4256 /* Alias sets are not stable across LTO sreaming; be conservative here
4257 and compare types the alias sets are ultimately based on. */
4258 if (lto_streaming_safe
)
4260 tree t1
= ao_ref_alias_ptr_type (ref1
);
4261 tree t2
= ao_ref_alias_ptr_type (ref2
);
4262 if (!alias_ptr_types_compatible_p (t1
, t2
))
4263 flags
|= REF_ALIAS_SET
;
4265 t1
= ao_ref_base_alias_ptr_type (ref1
);
4266 t2
= ao_ref_base_alias_ptr_type (ref2
);
4267 if (!alias_ptr_types_compatible_p (t1
, t2
))
4268 flags
|= BASE_ALIAS_SET
;
4272 if (ao_ref_alias_set (ref1
) != ao_ref_alias_set (ref2
))
4273 flags
|= REF_ALIAS_SET
;
4274 if (ao_ref_base_alias_set (ref1
) != ao_ref_base_alias_set (ref2
))
4275 flags
|= BASE_ALIAS_SET
;
4278 /* Access path is used only on non-view-converted references. */
4279 bool view_converted
= view_converted_memref_p (rbase1
);
4280 if (view_converted_memref_p (rbase2
) != view_converted
)
4281 return flags
| ACCESS_PATH
;
4282 else if (view_converted
)
4286 /* Find start of access paths and look for trailing arrays. */
4287 tree c1
= ref1
->ref
, c2
= ref2
->ref
;
4288 tree end_struct_ref1
= NULL
, end_struct_ref2
= NULL
;
4289 int nskipped1
= 0, nskipped2
= 0;
4292 for (tree p1
= ref1
->ref
; handled_component_p (p1
); p1
= TREE_OPERAND (p1
, 0))
4294 if (component_ref_to_zero_sized_trailing_array_p (p1
))
4295 end_struct_ref1
= p1
;
4296 if (ends_tbaa_access_path_p (p1
))
4297 c1
= p1
, nskipped1
= i
;
4300 for (tree p2
= ref2
->ref
; handled_component_p (p2
); p2
= TREE_OPERAND (p2
, 0))
4302 if (component_ref_to_zero_sized_trailing_array_p (p2
))
4303 end_struct_ref2
= p2
;
4304 if (ends_tbaa_access_path_p (p2
))
4305 c2
= p2
, nskipped1
= i
;
4309 /* For variable accesses we can not rely on offset match bellow.
4310 We know that paths are struturally same, so only check that
4311 starts of TBAA paths did not diverge. */
4312 if (!known_eq (ref1
->size
, ref1
->max_size
)
4313 && nskipped1
!= nskipped2
)
4314 return flags
| ACCESS_PATH
;
4316 /* Information about trailing refs is used by
4317 aliasing_component_refs_p that is applied only if paths
4318 has handled components.. */
4319 if (!handled_component_p (c1
) && !handled_component_p (c2
))
4321 else if ((end_struct_ref1
!= NULL
) != (end_struct_ref2
!= NULL
))
4322 return flags
| ACCESS_PATH
;
4324 && TYPE_MAIN_VARIANT (TREE_TYPE (end_struct_ref1
))
4325 != TYPE_MAIN_VARIANT (TREE_TYPE (end_struct_ref2
)))
4326 return flags
| ACCESS_PATH
;
4328 /* Now compare all handled components of the access path.
4329 We have three oracles that cares about access paths:
4330 - aliasing_component_refs_p
4331 - nonoverlapping_refs_since_match_p
4332 - nonoverlapping_component_refs_p
4333 We need to match things these oracles compare.
4335 It is only necessary to check types for compatibility
4336 and offsets. Rest of what oracles compares are actual
4337 addresses. Those are already known to be same:
4338 - for constant accesses we check offsets
4339 - for variable accesses we already matched
4340 the path lexically with operand_equal_p. */
4343 bool comp1
= handled_component_p (c1
);
4344 bool comp2
= handled_component_p (c2
);
4347 return flags
| ACCESS_PATH
;
4351 if (TREE_CODE (c1
) != TREE_CODE (c2
))
4352 return flags
| ACCESS_PATH
;
4354 /* aliasing_component_refs_p attempts to find type match within
4355 the paths. For that reason both types needs to be equal
4356 with respect to same_type_for_tbaa_p. */
4357 if (!types_equal_for_same_type_for_tbaa_p (TREE_TYPE (c1
),
4359 lto_streaming_safe
))
4360 return flags
| ACCESS_PATH
;
4361 if (component_ref_to_zero_sized_trailing_array_p (c1
)
4362 != component_ref_to_zero_sized_trailing_array_p (c2
))
4363 return flags
| ACCESS_PATH
;
4365 /* aliasing_matching_component_refs_p compares
4366 offsets within the path. Other properties are ignored.
4367 Do not bother to verify offsets in variable accesses. Here we
4368 already compared them by operand_equal_p so they are
4369 structurally same. */
4370 if (!known_eq (ref1
->size
, ref1
->max_size
))
4372 poly_int64 offadj1
, sztmc1
, msztmc1
;
4374 get_ref_base_and_extent (c1
, &offadj1
, &sztmc1
, &msztmc1
, &reverse1
);
4375 poly_int64 offadj2
, sztmc2
, msztmc2
;
4377 get_ref_base_and_extent (c2
, &offadj2
, &sztmc2
, &msztmc2
, &reverse2
);
4378 if (!known_eq (offadj1
, offadj2
))
4379 return flags
| ACCESS_PATH
;
4381 c1
= TREE_OPERAND (c1
, 0);
4382 c2
= TREE_OPERAND (c2
, 0);
4384 /* Finally test the access type. */
4385 if (!types_equal_for_same_type_for_tbaa_p (TREE_TYPE (c1
),
4387 lto_streaming_safe
))
4388 return flags
| ACCESS_PATH
;
4392 /* Hash REF to HSTATE. If LTO_STREAMING_SAFE do not use alias sets
4393 and canonical types. */
4395 ao_compare::hash_ao_ref (ao_ref
*ref
, bool lto_streaming_safe
, bool tbaa
,
4396 inchash::hash
&hstate
)
4398 tree base
= ao_ref_base (ref
);
4401 if (!known_eq (ref
->size
, ref
->max_size
))
4404 if (TREE_CODE (r
) == COMPONENT_REF
4405 && DECL_BIT_FIELD (TREE_OPERAND (r
, 1)))
4407 tree field
= TREE_OPERAND (r
, 1);
4408 hash_operand (DECL_FIELD_OFFSET (field
), hstate
, 0);
4409 hash_operand (DECL_FIELD_BIT_OFFSET (field
), hstate
, 0);
4410 hash_operand (DECL_SIZE (field
), hstate
, 0);
4411 r
= TREE_OPERAND (r
, 0);
4413 if (TREE_CODE (r
) == BIT_FIELD_REF
)
4415 hash_operand (TREE_OPERAND (r
, 1), hstate
, 0);
4416 hash_operand (TREE_OPERAND (r
, 2), hstate
, 0);
4417 r
= TREE_OPERAND (r
, 0);
4419 hash_operand (TYPE_SIZE (TREE_TYPE (ref
->ref
)), hstate
, 0);
4420 hash_operand (r
, hstate
, OEP_ADDRESS_OF
| OEP_MATCH_SIDE_EFFECTS
);
4424 hash_operand (tbase
, hstate
, OEP_ADDRESS_OF
| OEP_MATCH_SIDE_EFFECTS
);
4425 hstate
.add_poly_int (ref
->offset
);
4426 hstate
.add_poly_int (ref
->size
);
4427 hstate
.add_poly_int (ref
->max_size
);
4429 if (!lto_streaming_safe
&& tbaa
)
4431 hstate
.add_int (ao_ref_alias_set (ref
));
4432 hstate
.add_int (ao_ref_base_alias_set (ref
));