1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
34 #include "langhooks.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
45 #include "ipa-type-escape.h"
49 #include "pointer-set.h"
50 #include "alloc-pool.h"
51 #include "tree-ssa-alias.h"
53 /* Broad overview of how alias analysis on gimple works:
55 Statements clobbering or using memory are linked through the
56 virtual operand factored use-def chain. The virtual operand
57 is unique per function, its symbol is accessible via gimple_vop (cfun).
58 Virtual operands are used for efficiently walking memory statements
59 in the gimple IL and are useful for things like value-numbering as
60 a generation count for memory references.
62 SSA_NAME pointers may have associated points-to information
63 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
64 points-to information is (re-)computed by the TODO_rebuild_alias
65 pass manager todo. Points-to information is also used for more
66 precise tracking of call-clobbered and call-used variables and
67 related disambiguations.
69 This file contains functions for disambiguating memory references,
70 the so called alias-oracle and tools for walking of the gimple IL.
72 The main alias-oracle entry-points are
74 bool stmt_may_clobber_ref_p (gimple, tree)
76 This function queries if a statement may invalidate (parts of)
77 the memory designated by the reference tree argument.
79 bool ref_maybe_used_by_stmt_p (gimple, tree)
81 This function queries if a statement may need (parts of) the
82 memory designated by the reference tree argument.
84 There are variants of these functions that only handle the call
85 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
86 Note that these do not disambiguate against a possible call lhs.
88 bool refs_may_alias_p (tree, tree)
90 This function tries to disambiguate two reference trees.
92 bool ptr_deref_may_alias_global_p (tree)
94 This function queries if dereferencing a pointer variable may
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.c. */
102 /* Query statistics for the different low-level disambiguators.
103 A high-level query may trigger multiple of them. */
106 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias
;
107 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias
;
108 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias
;
109 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias
;
110 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias
;
111 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias
;
115 dump_alias_stats (FILE *s
)
117 fprintf (s
, "\nAlias oracle query stats:\n");
118 fprintf (s
, " refs_may_alias_p: "
119 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
120 HOST_WIDE_INT_PRINT_DEC
" queries\n",
121 alias_stats
.refs_may_alias_p_no_alias
,
122 alias_stats
.refs_may_alias_p_no_alias
123 + alias_stats
.refs_may_alias_p_may_alias
);
124 fprintf (s
, " ref_maybe_used_by_call_p: "
125 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
126 HOST_WIDE_INT_PRINT_DEC
" queries\n",
127 alias_stats
.ref_maybe_used_by_call_p_no_alias
,
128 alias_stats
.refs_may_alias_p_no_alias
129 + alias_stats
.ref_maybe_used_by_call_p_may_alias
);
130 fprintf (s
, " call_may_clobber_ref_p: "
131 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
132 HOST_WIDE_INT_PRINT_DEC
" queries\n",
133 alias_stats
.call_may_clobber_ref_p_no_alias
,
134 alias_stats
.call_may_clobber_ref_p_no_alias
135 + alias_stats
.call_may_clobber_ref_p_may_alias
);
139 /* Return true, if dereferencing PTR may alias with a global variable. */
142 ptr_deref_may_alias_global_p (tree ptr
)
144 struct ptr_info_def
*pi
;
146 /* If we end up with a pointer constant here that may point
148 if (TREE_CODE (ptr
) != SSA_NAME
)
151 pi
= SSA_NAME_PTR_INFO (ptr
);
153 /* If we do not have points-to information for this variable,
158 /* ??? This does not use TBAA to prune globals ptr may not access. */
159 return pt_solution_includes_global (&pi
->pt
);
162 /* Return true if dereferencing PTR may alias DECL.
163 The caller is responsible for applying TBAA to see if PTR
164 may access DECL at all. */
167 ptr_deref_may_alias_decl_p (tree ptr
, tree decl
)
169 struct ptr_info_def
*pi
;
171 /* ??? During SCCVN/PRE we can end up with *&x during valueizing
172 operands. Likewise we can end up with dereferencing constant
173 pointers. Just bail out in these cases for now. */
174 if (TREE_CODE (ptr
) == ADDR_EXPR
175 || TREE_CODE (ptr
) == INTEGER_CST
)
178 gcc_assert (TREE_CODE (ptr
) == SSA_NAME
179 && (TREE_CODE (decl
) == VAR_DECL
180 || TREE_CODE (decl
) == PARM_DECL
181 || TREE_CODE (decl
) == RESULT_DECL
));
183 /* Non-aliased variables can not be pointed to. */
184 if (!may_be_aliased (decl
))
187 /* If we do not have useful points-to information for this pointer
188 we cannot disambiguate anything else. */
189 pi
= SSA_NAME_PTR_INFO (ptr
);
193 return pt_solution_includes (&pi
->pt
, decl
);
196 /* Return true if dereferenced PTR1 and PTR2 may alias.
197 The caller is responsible for applying TBAA to see if accesses
198 through PTR1 and PTR2 may conflict at all. */
201 ptr_derefs_may_alias_p (tree ptr1
, tree ptr2
)
203 struct ptr_info_def
*pi1
, *pi2
;
205 /* ??? During SCCVN/PRE we can end up with *&x during valueizing
206 operands. Likewise we can end up with dereferencing constant
207 pointers. Just bail out in these cases for now. */
208 if (TREE_CODE (ptr1
) == ADDR_EXPR
209 || TREE_CODE (ptr1
) == INTEGER_CST
210 || TREE_CODE (ptr2
) == ADDR_EXPR
211 || TREE_CODE (ptr2
) == INTEGER_CST
)
214 gcc_assert (TREE_CODE (ptr1
) == SSA_NAME
215 && TREE_CODE (ptr2
) == SSA_NAME
);
217 /* We may end up with two empty points-to solutions for two same pointers.
218 In this case we still want to say both pointers alias, so shortcut
223 /* If we do not have useful points-to information for either pointer
224 we cannot disambiguate anything else. */
225 pi1
= SSA_NAME_PTR_INFO (ptr1
);
226 pi2
= SSA_NAME_PTR_INFO (ptr2
);
230 /* ??? This does not use TBAA to prune decls from the intersection
231 that not both pointers may access. */
232 return pt_solutions_intersect (&pi1
->pt
, &pi2
->pt
);
235 /* Return true if STMT is an "escape" site from the current function. Escape
236 sites those statements which might expose the address of a variable
237 outside the current function. STMT is an escape site iff:
239 1- STMT is a function call, or
240 2- STMT is an __asm__ expression, or
241 3- STMT is an assignment to a non-local variable, or
242 4- STMT is a return statement.
244 Return the type of escape site found, if we found one, or NO_ESCAPE
248 is_escape_site (gimple stmt
)
250 if (is_gimple_call (stmt
))
252 if (gimple_call_flags (stmt
) & (ECF_PURE
| ECF_CONST
))
253 return ESCAPE_TO_PURE_CONST
;
255 return ESCAPE_TO_CALL
;
257 else if (gimple_code (stmt
) == GIMPLE_ASM
)
258 return ESCAPE_TO_ASM
;
259 else if (is_gimple_assign (stmt
))
261 tree lhs
= gimple_assign_lhs (stmt
);
263 /* Get to the base of _REF nodes. */
264 if (TREE_CODE (lhs
) != SSA_NAME
)
265 lhs
= get_base_address (lhs
);
267 /* If we couldn't recognize the LHS of the assignment, assume that it
268 is a non-local store. */
269 if (lhs
== NULL_TREE
)
270 return ESCAPE_UNKNOWN
;
272 if (gimple_assign_cast_p (stmt
))
274 tree from
= TREE_TYPE (gimple_assign_rhs1 (stmt
));
275 tree to
= TREE_TYPE (lhs
);
277 /* If the RHS is a conversion between a pointer and an integer, the
278 pointer escapes since we can't track the integer. */
279 if (POINTER_TYPE_P (from
) && !POINTER_TYPE_P (to
))
280 return ESCAPE_BAD_CAST
;
283 /* If the LHS is an SSA name, it can't possibly represent a non-local
285 if (TREE_CODE (lhs
) == SSA_NAME
)
288 /* If the LHS is a non-global decl, it isn't a non-local memory store.
289 If the LHS escapes, the RHS escape is dealt with in the PTA solver. */
291 && !is_global_var (lhs
))
294 /* FIXME: LHS is not an SSA_NAME. Even if it's an assignment to a
295 local variables we cannot be sure if it will escape, because we
296 don't have information about objects not in SSA form. Need to
297 implement something along the lines of
299 J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
300 Midkiff, ``Escape analysis for java,'' in Proceedings of the
301 Conference on Object-Oriented Programming Systems, Languages, and
302 Applications (OOPSLA), pp. 1-19, 1999. */
303 return ESCAPE_STORED_IN_GLOBAL
;
305 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
306 return ESCAPE_TO_RETURN
;
312 /* Dump alias information on FILE. */
315 dump_alias_info (FILE *file
)
319 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
320 referenced_var_iterator rvi
;
323 fprintf (file
, "\n\nAlias information for %s\n\n", funcname
);
325 fprintf (file
, "Aliased symbols\n\n");
327 FOR_EACH_REFERENCED_VAR (var
, rvi
)
329 if (may_be_aliased (var
))
330 dump_variable (file
, var
);
333 fprintf (file
, "\nCall clobber information\n");
335 fprintf (file
, "\nESCAPED");
336 dump_points_to_solution (file
, &cfun
->gimple_df
->escaped
);
337 fprintf (file
, "\nCALLUSED");
338 dump_points_to_solution (file
, &cfun
->gimple_df
->callused
);
340 fprintf (file
, "\n\nFlow-insensitive points-to information\n\n");
342 for (i
= 1; i
< num_ssa_names
; i
++)
344 tree ptr
= ssa_name (i
);
345 struct ptr_info_def
*pi
;
348 || SSA_NAME_IN_FREE_LIST (ptr
))
351 pi
= SSA_NAME_PTR_INFO (ptr
);
353 dump_points_to_info_for (file
, ptr
);
356 fprintf (file
, "\n");
360 /* Dump alias information on stderr. */
363 debug_alias_info (void)
365 dump_alias_info (stderr
);
369 /* Return the alias information associated with pointer T. It creates a
370 new instance if none existed. */
372 struct ptr_info_def
*
373 get_ptr_info (tree t
)
375 struct ptr_info_def
*pi
;
377 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t
)));
379 pi
= SSA_NAME_PTR_INFO (t
);
382 pi
= GGC_CNEW (struct ptr_info_def
);
383 pt_solution_reset (&pi
->pt
);
384 SSA_NAME_PTR_INFO (t
) = pi
;
390 /* Dump the points-to set *PT into FILE. */
393 dump_points_to_solution (FILE *file
, struct pt_solution
*pt
)
396 fprintf (file
, ", points-to anything");
399 fprintf (file
, ", points-to non-local");
402 fprintf (file
, ", points-to escaped");
405 fprintf (file
, ", points-to NULL");
409 fprintf (file
, ", points-to vars: ");
410 dump_decl_set (file
, pt
->vars
);
411 if (pt
->vars_contains_global
)
412 fprintf (file
, " (includes global vars)");
416 /* Dump points-to information for SSA_NAME PTR into FILE. */
419 dump_points_to_info_for (FILE *file
, tree ptr
)
421 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (ptr
);
423 print_generic_expr (file
, ptr
, dump_flags
);
426 dump_points_to_solution (file
, &pi
->pt
);
428 fprintf (file
, ", points-to anything");
430 fprintf (file
, "\n");
434 /* Dump points-to information for VAR into stderr. */
437 debug_points_to_info_for (tree var
)
439 dump_points_to_info_for (stderr
, var
);
442 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
443 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
447 same_type_for_tbaa (tree type1
, tree type2
)
449 type1
= TYPE_MAIN_VARIANT (type1
);
450 type2
= TYPE_MAIN_VARIANT (type2
);
452 /* If we would have to do structural comparison bail out. */
453 if (TYPE_STRUCTURAL_EQUALITY_P (type1
)
454 || TYPE_STRUCTURAL_EQUALITY_P (type2
))
457 /* Compare the canonical types. */
458 if (TYPE_CANONICAL (type1
) == TYPE_CANONICAL (type2
))
461 /* ??? Array types are not properly unified in all cases as we have
462 spurious changes in the index types for example. Removing this
463 causes all sorts of problems with the Fortran frontend. */
464 if (TREE_CODE (type1
) == ARRAY_TYPE
465 && TREE_CODE (type2
) == ARRAY_TYPE
)
468 /* The types are known to be not equal. */
472 /* Determine if the two component references REF1 and REF2 which are
473 based on access types TYPE1 and TYPE2 and of which at least one is based
474 on an indirect reference may alias. */
477 nonaliasing_component_refs_p (tree ref1
, tree type1
,
478 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
479 tree ref2
, tree type2
,
480 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
)
482 /* If one reference is a component references through pointers try to find a
483 common base and apply offset based disambiguation. This handles
485 struct A { int i; int j; } *q;
486 struct B { struct A a; int k; } *p;
487 disambiguating q->i and p->a.j. */
491 /* Now search for the type1 in the access path of ref2. This
492 would be a common base for doing offset based disambiguation on. */
493 /* Skip the outermost ref - we would have caught that earlier. */
494 refp
= &TREE_OPERAND (ref2
, 0);
495 while (handled_component_p (*refp
)
496 && same_type_for_tbaa (TREE_TYPE (*refp
), type1
) == 0)
497 refp
= &TREE_OPERAND (*refp
, 0);
498 same_p
= same_type_for_tbaa (TREE_TYPE (*refp
), type1
);
499 /* If we couldn't compare types we have to bail out. */
502 else if (same_p
== 1)
504 HOST_WIDE_INT offadj
, sztmp
, msztmp
;
505 get_ref_base_and_extent (*refp
, &offadj
, &sztmp
, &msztmp
);
507 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
509 /* If we didn't find a common base, try the other way around. */
510 refp
= &TREE_OPERAND (ref1
, 0);
511 while (handled_component_p (*refp
)
512 && same_type_for_tbaa (TREE_TYPE (*refp
), type2
) == 0)
513 refp
= &TREE_OPERAND (*refp
, 0);
514 same_p
= same_type_for_tbaa (TREE_TYPE (*refp
), type2
);
515 /* If we couldn't compare types we have to bail out. */
518 else if (same_p
== 1)
520 HOST_WIDE_INT offadj
, sztmp
, msztmp
;
521 get_ref_base_and_extent (*refp
, &offadj
, &sztmp
, &msztmp
);
523 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
525 /* If we have two type access paths B1.path1 and B2.path2 they may
526 only alias if either B1 is in B2.path2 or B2 is in B1.path1. */
530 /* Return true if two memory references based on the variables BASE1
531 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
532 [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias. */
535 decl_refs_may_alias_p (tree base1
,
536 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
538 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
)
540 gcc_assert (SSA_VAR_P (base1
) && SSA_VAR_P (base2
));
542 /* If both references are based on different variables, they cannot alias. */
543 if (!operand_equal_p (base1
, base2
, 0))
546 /* If both references are based on the same variable, they cannot alias if
547 the accesses do not overlap. */
548 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
551 /* Return true if an indirect reference based on *PTR1 constrained
552 to [OFFSET1, OFFSET1 + MAX_SIZE1[ may alias a variable based on BASE2
553 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2[. *PTR1 and BASE2 have
554 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
555 in which case they are computed on-demand. REF1 and REF2
556 if non-NULL are the complete memory reference trees. */
559 indirect_ref_may_alias_decl_p (tree ref1
, tree ptr1
,
560 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
561 alias_set_type base1_alias_set
,
562 tree ref2
, tree base2
,
563 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
564 alias_set_type base2_alias_set
)
566 /* If only one reference is based on a variable, they cannot alias if
567 the pointer access is beyond the extent of the variable access.
568 (the pointer base cannot validly point to an offset less than zero
570 They also cannot alias if the pointer may not point to the decl. */
572 && !ranges_overlap_p (offset1
, max_size1
, 0, offset2
+ max_size2
))
574 if (!ptr_deref_may_alias_decl_p (ptr1
, base2
))
577 /* Disambiguations that rely on strict aliasing rules follow. */
578 if (!flag_strict_aliasing
)
581 /* If the alias set for a pointer access is zero all bets are off. */
582 if (base1_alias_set
== -1)
583 base1_alias_set
= get_deref_alias_set (ptr1
);
584 if (base1_alias_set
== 0)
586 if (base2_alias_set
== -1)
587 base2_alias_set
= get_alias_set (base2
);
589 /* If both references are through the same type, they do not alias
590 if the accesses do not overlap. This does extra disambiguation
591 for mixed/pointer accesses but requires strict aliasing. */
592 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1
)),
593 TREE_TYPE (base2
)) == 1)
594 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
596 /* The only way to access a variable is through a pointer dereference
597 of the same alias set or a subset of it. */
598 if (base1_alias_set
!= base2_alias_set
599 && !alias_set_subset_of (base1_alias_set
, base2_alias_set
))
602 /* Do access-path based disambiguation. */
604 && handled_component_p (ref1
)
605 && handled_component_p (ref2
))
606 return nonaliasing_component_refs_p (ref1
, TREE_TYPE (TREE_TYPE (ptr1
)),
608 ref2
, TREE_TYPE (base2
),
614 /* Return true if two indirect references based on *PTR1
615 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
616 [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias. *PTR1 and *PTR2 have
617 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
618 in which case they are computed on-demand. REF1 and REF2
619 if non-NULL are the complete memory reference trees. */
622 indirect_refs_may_alias_p (tree ref1
, tree ptr1
,
623 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
624 alias_set_type base1_alias_set
,
625 tree ref2
, tree ptr2
,
626 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
627 alias_set_type base2_alias_set
)
629 /* If both bases are based on pointers they cannot alias if they may not
630 point to the same memory object or if they point to the same object
631 and the accesses do not overlap. */
632 if (operand_equal_p (ptr1
, ptr2
, 0))
633 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
634 if (!ptr_derefs_may_alias_p (ptr1
, ptr2
))
637 /* Disambiguations that rely on strict aliasing rules follow. */
638 if (!flag_strict_aliasing
)
641 /* If the alias set for a pointer access is zero all bets are off. */
642 if (base1_alias_set
== -1)
643 base1_alias_set
= get_deref_alias_set (ptr1
);
644 if (base1_alias_set
== 0)
646 if (base2_alias_set
== -1)
647 base2_alias_set
= get_deref_alias_set (ptr2
);
648 if (base2_alias_set
== 0)
651 /* If both references are through the same type, they do not alias
652 if the accesses do not overlap. This does extra disambiguation
653 for mixed/pointer accesses but requires strict aliasing. */
654 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1
)),
655 TREE_TYPE (TREE_TYPE (ptr2
))) == 1)
656 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
658 /* Do type-based disambiguation. */
659 if (base1_alias_set
!= base2_alias_set
660 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
663 /* Do access-path based disambiguation. */
665 && handled_component_p (ref1
)
666 && handled_component_p (ref2
))
667 return nonaliasing_component_refs_p (ref1
, TREE_TYPE (TREE_TYPE (ptr1
)),
669 ref2
, TREE_TYPE (TREE_TYPE (ptr2
)),
675 /* Return true, if the two memory references REF1 and REF2 may alias. */
678 refs_may_alias_p_1 (tree ref1
, tree ref2
, bool tbaa_p
)
681 HOST_WIDE_INT offset1
= 0, offset2
= 0;
682 HOST_WIDE_INT size1
= -1, size2
= -1;
683 HOST_WIDE_INT max_size1
= -1, max_size2
= -1;
684 bool var1_p
, var2_p
, ind1_p
, ind2_p
;
687 gcc_assert ((SSA_VAR_P (ref1
)
688 || handled_component_p (ref1
)
689 || INDIRECT_REF_P (ref1
)
690 || TREE_CODE (ref1
) == TARGET_MEM_REF
)
692 || handled_component_p (ref2
)
693 || INDIRECT_REF_P (ref2
)
694 || TREE_CODE (ref2
) == TARGET_MEM_REF
));
696 /* Decompose the references into their base objects and the access. */
697 base1
= get_ref_base_and_extent (ref1
, &offset1
, &size1
, &max_size1
);
698 base2
= get_ref_base_and_extent (ref2
, &offset2
, &size2
, &max_size2
);
700 /* We can end up with registers or constants as bases for example from
701 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
702 which is seen as a struct copy. */
703 if (TREE_CODE (base1
) == SSA_NAME
704 || TREE_CODE (base2
) == SSA_NAME
705 || is_gimple_min_invariant (base1
)
706 || is_gimple_min_invariant (base2
))
709 /* Defer to simple offset based disambiguation if we have
710 references based on two decls. Do this before defering to
711 TBAA to handle must-alias cases in conformance with the
712 GCC extension of allowing type-punning through unions. */
713 var1_p
= SSA_VAR_P (base1
);
714 var2_p
= SSA_VAR_P (base2
);
715 if (var1_p
&& var2_p
)
716 return decl_refs_may_alias_p (base1
, offset1
, max_size1
,
717 base2
, offset2
, max_size2
);
719 /* First defer to TBAA if possible. */
721 && flag_strict_aliasing
722 && !alias_sets_conflict_p (get_alias_set (ref1
), get_alias_set (ref2
)))
725 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
726 TBAA disambiguation based on the access type is possible, so bail
727 out only after that check. */
728 if (TREE_CODE (ref1
) == TARGET_MEM_REF
729 || TREE_CODE (ref2
) == TARGET_MEM_REF
)
732 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
733 ind1_p
= INDIRECT_REF_P (base1
);
734 ind2_p
= INDIRECT_REF_P (base2
);
735 set
= tbaa_p
? -1 : 0;
736 if (var1_p
&& ind2_p
)
737 return indirect_ref_may_alias_decl_p (ref2
, TREE_OPERAND (base2
, 0),
738 offset2
, max_size2
, set
,
740 offset1
, max_size1
, set
);
741 else if (ind1_p
&& var2_p
)
742 return indirect_ref_may_alias_decl_p (ref1
, TREE_OPERAND (base1
, 0),
743 offset1
, max_size1
, set
,
745 offset2
, max_size2
, set
);
746 else if (ind1_p
&& ind2_p
)
747 return indirect_refs_may_alias_p (ref1
, TREE_OPERAND (base1
, 0),
748 offset1
, max_size1
, set
,
749 ref2
, TREE_OPERAND (base2
, 0),
750 offset2
, max_size2
, set
);
756 refs_may_alias_p (tree ref1
, tree ref2
)
758 bool res
= refs_may_alias_p_1 (ref1
, ref2
, true);
760 ++alias_stats
.refs_may_alias_p_may_alias
;
762 ++alias_stats
.refs_may_alias_p_no_alias
;
766 /* Returns true if there is a anti-dependence for the STORE that
767 executes after the LOAD. */
770 refs_anti_dependent_p (tree load
, tree store
)
772 return refs_may_alias_p_1 (load
, store
, false);
775 /* Returns true if there is a output dependence for the stores
776 STORE1 and STORE2. */
779 refs_output_dependent_p (tree store1
, tree store2
)
781 return refs_may_alias_p_1 (store1
, store2
, false);
784 /* If the call CALL may use the memory reference REF return true,
785 otherwise return false. */
788 ref_maybe_used_by_call_p_1 (gimple call
, tree ref
)
792 int flags
= gimple_call_flags (call
);
794 /* Const functions without a static chain do not implicitly use memory. */
795 if (!gimple_call_chain (call
)
796 && (flags
& (ECF_CONST
|ECF_NOVOPS
)))
799 /* If the reference is not based on a decl give up.
800 ??? Handle indirect references by intersecting the call-used
801 solution with that of the pointer. */
802 base
= get_base_address (ref
);
807 /* If the reference is based on a decl that is not aliased the call
808 cannot possibly use it. */
810 && !may_be_aliased (base
)
811 /* But local statics can be used through recursion. */
812 && !is_global_var (base
))
815 /* Check if base is a global static variable that is not read
817 if (TREE_CODE (base
) == VAR_DECL
818 && TREE_STATIC (base
)
819 && !TREE_PUBLIC (base
))
821 tree callee
= gimple_call_fndecl (call
);
824 if (callee
!= NULL_TREE
826 = ipa_reference_get_not_read_global (cgraph_node (callee
)))
827 && bitmap_bit_p (not_read
, DECL_UID (base
)))
831 /* If the base variable is call-used or call-clobbered then
833 if (flags
& (ECF_PURE
|ECF_CONST
|ECF_LOOPING_CONST_OR_PURE
|ECF_NOVOPS
))
835 if (is_call_used (base
))
840 if (is_call_clobbered (base
))
844 /* Inspect call arguments for passed-by-value aliases. */
846 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
848 tree op
= gimple_call_arg (call
, i
);
850 if (TREE_CODE (op
) == EXC_PTR_EXPR
851 || TREE_CODE (op
) == FILTER_EXPR
)
854 if (TREE_CODE (op
) == WITH_SIZE_EXPR
)
855 op
= TREE_OPERAND (op
, 0);
857 if (TREE_CODE (op
) != SSA_NAME
858 && !is_gimple_min_invariant (op
)
859 && refs_may_alias_p (op
, ref
))
867 ref_maybe_used_by_call_p (gimple call
, tree ref
)
869 bool res
= ref_maybe_used_by_call_p_1 (call
, ref
);
871 ++alias_stats
.ref_maybe_used_by_call_p_may_alias
;
873 ++alias_stats
.ref_maybe_used_by_call_p_no_alias
;
878 /* If the statement STMT may use the memory reference REF return
879 true, otherwise return false. */
882 ref_maybe_used_by_stmt_p (gimple stmt
, tree ref
)
884 if (is_gimple_assign (stmt
))
888 /* All memory assign statements are single. */
889 if (!gimple_assign_single_p (stmt
))
892 rhs
= gimple_assign_rhs1 (stmt
);
893 if (is_gimple_reg (rhs
)
894 || is_gimple_min_invariant (rhs
)
895 || gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
)
898 return refs_may_alias_p (rhs
, ref
);
900 else if (is_gimple_call (stmt
))
901 return ref_maybe_used_by_call_p (stmt
, ref
);
906 /* If the call in statement CALL may clobber the memory reference REF
907 return true, otherwise return false. */
910 call_may_clobber_ref_p_1 (gimple call
, tree ref
)
914 /* If the call is pure or const it cannot clobber anything. */
915 if (gimple_call_flags (call
)
916 & (ECF_PURE
|ECF_CONST
|ECF_LOOPING_CONST_OR_PURE
|ECF_NOVOPS
))
919 base
= get_base_address (ref
);
923 if (TREE_CODE (base
) == SSA_NAME
924 || CONSTANT_CLASS_P (base
))
927 /* If the reference is based on a decl that is not aliased the call
928 cannot possibly clobber it. */
930 && !may_be_aliased (base
)
931 /* But local non-readonly statics can be modified through recursion
932 or the call may implement a threading barrier which we must
934 && (TREE_READONLY (base
)
935 || !is_global_var (base
)))
938 /* Check if base is a global static variable that is not written
940 if (TREE_CODE (base
) == VAR_DECL
941 && TREE_STATIC (base
)
942 && !TREE_PUBLIC (base
))
944 tree callee
= gimple_call_fndecl (call
);
947 if (callee
!= NULL_TREE
949 = ipa_reference_get_not_written_global (cgraph_node (callee
)))
950 && bitmap_bit_p (not_written
, DECL_UID (base
)))
955 return is_call_clobbered (base
);
961 call_may_clobber_ref_p (gimple call
, tree ref
)
963 bool res
= call_may_clobber_ref_p_1 (call
, ref
);
965 ++alias_stats
.call_may_clobber_ref_p_may_alias
;
967 ++alias_stats
.call_may_clobber_ref_p_no_alias
;
972 /* If the statement STMT may clobber the memory reference REF return true,
973 otherwise return false. */
976 stmt_may_clobber_ref_p (gimple stmt
, tree ref
)
978 if (is_gimple_call (stmt
))
980 tree lhs
= gimple_call_lhs (stmt
);
982 && !is_gimple_reg (lhs
)
983 && refs_may_alias_p (ref
, lhs
))
986 return call_may_clobber_ref_p (stmt
, ref
);
988 else if (is_gimple_assign (stmt
))
989 return refs_may_alias_p (ref
, gimple_assign_lhs (stmt
));
990 else if (gimple_code (stmt
) == GIMPLE_ASM
)
996 static tree
get_continuation_for_phi (gimple
, tree
, bitmap
*);
998 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
999 TARGET or a statement clobbering the memory reference REF in which
1000 case false is returned. The walk starts with VUSE, one argument of PHI. */
1003 maybe_skip_until (gimple phi
, tree target
, tree ref
, tree vuse
, bitmap
*visited
)
1006 *visited
= BITMAP_ALLOC (NULL
);
1008 bitmap_set_bit (*visited
, SSA_NAME_VERSION (PHI_RESULT (phi
)));
1010 /* Walk until we hit the target. */
1011 while (vuse
!= target
)
1013 gimple def_stmt
= SSA_NAME_DEF_STMT (vuse
);
1014 /* Recurse for PHI nodes. */
1015 if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1017 /* An already visited PHI node ends the walk successfully. */
1018 if (bitmap_bit_p (*visited
, SSA_NAME_VERSION (PHI_RESULT (def_stmt
))))
1020 vuse
= get_continuation_for_phi (def_stmt
, ref
, visited
);
1025 /* A clobbering statement or the end of the IL ends it failing. */
1026 else if (gimple_nop_p (def_stmt
)
1027 || stmt_may_clobber_ref_p (def_stmt
, ref
))
1029 vuse
= gimple_vuse (def_stmt
);
1034 /* Starting from a PHI node for the virtual operand of the memory reference
1035 REF find a continuation virtual operand that allows to continue walking
1036 statements dominating PHI skipping only statements that cannot possibly
1037 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1041 get_continuation_for_phi (gimple phi
, tree ref
, bitmap
*visited
)
1043 unsigned nargs
= gimple_phi_num_args (phi
);
1045 /* Through a single-argument PHI we can simply look through. */
1047 return PHI_ARG_DEF (phi
, 0);
1049 /* For two arguments try to skip non-aliasing code until we hit
1050 the phi argument definition that dominates the other one. */
1053 tree arg0
= PHI_ARG_DEF (phi
, 0);
1054 tree arg1
= PHI_ARG_DEF (phi
, 1);
1055 gimple def0
= SSA_NAME_DEF_STMT (arg0
);
1056 gimple def1
= SSA_NAME_DEF_STMT (arg1
);
1060 else if (gimple_nop_p (def0
)
1061 || (!gimple_nop_p (def1
)
1062 && dominated_by_p (CDI_DOMINATORS
,
1063 gimple_bb (def1
), gimple_bb (def0
))))
1065 if (maybe_skip_until (phi
, arg0
, ref
, arg1
, visited
))
1068 else if (gimple_nop_p (def1
)
1069 || dominated_by_p (CDI_DOMINATORS
,
1070 gimple_bb (def0
), gimple_bb (def1
)))
1072 if (maybe_skip_until (phi
, arg1
, ref
, arg0
, visited
))
1080 /* Based on the memory reference REF and its virtual use VUSE call
1081 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1082 itself. That is, for each virtual use for which its defining statement
1083 does not clobber REF.
1085 WALKER is called with REF, the current virtual use and DATA. If
1086 WALKER returns non-NULL the walk stops and its result is returned.
1087 At the end of a non-successful walk NULL is returned.
1089 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1090 use which definition is a statement that may clobber REF and DATA.
1091 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1092 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1093 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1094 to adjust REF and *DATA to make that valid.
1096 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1099 walk_non_aliased_vuses (tree ref
, tree vuse
,
1100 void *(*walker
)(tree
, tree
, void *),
1101 void *(*translate
)(tree
*, tree
, void *),void *data
)
1103 bitmap visited
= NULL
;
1106 timevar_push (TV_ALIAS_STMT_WALK
);
1112 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1113 res
= (*walker
) (ref
, vuse
, data
);
1117 def_stmt
= SSA_NAME_DEF_STMT (vuse
);
1118 if (gimple_nop_p (def_stmt
))
1120 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1121 vuse
= get_continuation_for_phi (def_stmt
, ref
, &visited
);
1124 if (stmt_may_clobber_ref_p (def_stmt
, ref
))
1128 res
= (*translate
) (&ref
, vuse
, data
);
1129 /* Failed lookup and translation. */
1130 if (res
== (void *)-1)
1135 /* Lookup succeeded. */
1136 else if (res
!= NULL
)
1138 /* Translation succeeded, continue walking. */
1140 vuse
= gimple_vuse (def_stmt
);
1146 BITMAP_FREE (visited
);
1148 timevar_pop (TV_ALIAS_STMT_WALK
);
1154 /* Based on the memory reference REF call WALKER for each vdef which
1155 defining statement may clobber REF, starting with VDEF. If REF
1156 is NULL_TREE, each defining statement is visited.
1158 WALKER is called with REF, the current vdef and DATA. If WALKER
1159 returns true the walk is stopped, otherwise it continues.
1161 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1162 PHI argument (but only one walk continues on merge points), the
1163 return value is true if any of the walks was successful.
1165 The function returns the number of statements walked. */
1168 walk_aliased_vdefs_1 (tree ref
, tree vdef
,
1169 bool (*walker
)(tree
, tree
, void *), void *data
,
1170 bitmap
*visited
, unsigned int cnt
)
1174 gimple def_stmt
= SSA_NAME_DEF_STMT (vdef
);
1177 && !bitmap_set_bit (*visited
, SSA_NAME_VERSION (vdef
)))
1180 if (gimple_nop_p (def_stmt
))
1182 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1186 *visited
= BITMAP_ALLOC (NULL
);
1187 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); ++i
)
1188 cnt
+= walk_aliased_vdefs_1 (ref
, gimple_phi_arg_def (def_stmt
, i
),
1189 walker
, data
, visited
, 0);
1193 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1196 || stmt_may_clobber_ref_p (def_stmt
, ref
))
1197 && (*walker
) (ref
, vdef
, data
))
1200 vdef
= gimple_vuse (def_stmt
);
1206 walk_aliased_vdefs (tree ref
, tree vdef
,
1207 bool (*walker
)(tree
, tree
, void *), void *data
,
1210 bitmap local_visited
= NULL
;
1213 timevar_push (TV_ALIAS_STMT_WALK
);
1215 ret
= walk_aliased_vdefs_1 (ref
, vdef
, walker
, data
,
1216 visited
? visited
: &local_visited
, 0);
1218 BITMAP_FREE (local_visited
);
1220 timevar_pop (TV_ALIAS_STMT_WALK
);