1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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"
28 #include "basic-block.h"
31 #include "langhooks.h"
34 #include "tree-pretty-print.h"
35 #include "tree-dump.h"
37 #include "tree-flow.h"
38 #include "tree-inline.h"
39 #include "tree-pass.h"
42 #include "ipa-type-escape.h"
46 #include "pointer-set.h"
47 #include "alloc-pool.h"
48 #include "tree-ssa-alias.h"
50 /* Broad overview of how alias analysis on gimple works:
52 Statements clobbering or using memory are linked through the
53 virtual operand factored use-def chain. The virtual operand
54 is unique per function, its symbol is accessible via gimple_vop (cfun).
55 Virtual operands are used for efficiently walking memory statements
56 in the gimple IL and are useful for things like value-numbering as
57 a generation count for memory references.
59 SSA_NAME pointers may have associated points-to information
60 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
61 points-to information is (re-)computed by the TODO_rebuild_alias
62 pass manager todo. Points-to information is also used for more
63 precise tracking of call-clobbered and call-used variables and
64 related disambiguations.
66 This file contains functions for disambiguating memory references,
67 the so called alias-oracle and tools for walking of the gimple IL.
69 The main alias-oracle entry-points are
71 bool stmt_may_clobber_ref_p (gimple, tree)
73 This function queries if a statement may invalidate (parts of)
74 the memory designated by the reference tree argument.
76 bool ref_maybe_used_by_stmt_p (gimple, tree)
78 This function queries if a statement may need (parts of) the
79 memory designated by the reference tree argument.
81 There are variants of these functions that only handle the call
82 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
83 Note that these do not disambiguate against a possible call lhs.
85 bool refs_may_alias_p (tree, tree)
87 This function tries to disambiguate two reference trees.
89 bool ptr_deref_may_alias_global_p (tree)
91 This function queries if dereferencing a pointer variable may
94 More low-level disambiguators are available and documented in
95 this file. Low-level disambiguators dealing with points-to
96 information are in tree-ssa-structalias.c. */
99 /* Query statistics for the different low-level disambiguators.
100 A high-level query may trigger multiple of them. */
103 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
104 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
105 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
106 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
107 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
108 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
112 dump_alias_stats (FILE *s)
114 fprintf (s, "\nAlias oracle query stats:\n");
115 fprintf (s, " refs_may_alias_p: "
116 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
117 HOST_WIDE_INT_PRINT_DEC" queries\n",
118 alias_stats.refs_may_alias_p_no_alias,
119 alias_stats.refs_may_alias_p_no_alias
120 + alias_stats.refs_may_alias_p_may_alias);
121 fprintf (s, " ref_maybe_used_by_call_p: "
122 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
123 HOST_WIDE_INT_PRINT_DEC" queries\n",
124 alias_stats.ref_maybe_used_by_call_p_no_alias,
125 alias_stats.refs_may_alias_p_no_alias
126 + alias_stats.ref_maybe_used_by_call_p_may_alias);
127 fprintf (s, " call_may_clobber_ref_p: "
128 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
129 HOST_WIDE_INT_PRINT_DEC" queries\n",
130 alias_stats.call_may_clobber_ref_p_no_alias,
131 alias_stats.call_may_clobber_ref_p_no_alias
132 + alias_stats.call_may_clobber_ref_p_may_alias);
136 /* Return true, if dereferencing PTR may alias with a global variable. */
139 ptr_deref_may_alias_global_p (tree ptr)
141 struct ptr_info_def *pi;
143 /* If we end up with a pointer constant here that may point
145 if (TREE_CODE (ptr) != SSA_NAME)
148 pi = SSA_NAME_PTR_INFO (ptr);
150 /* If we do not have points-to information for this variable,
155 /* ??? This does not use TBAA to prune globals ptr may not access. */
156 return pt_solution_includes_global (&pi->pt);
159 /* Return true if dereferencing PTR may alias DECL.
160 The caller is responsible for applying TBAA to see if PTR
161 may access DECL at all. */
164 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
166 struct ptr_info_def *pi;
168 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
169 || TREE_CODE (ptr) == ADDR_EXPR
170 || TREE_CODE (ptr) == INTEGER_CST)
171 && (TREE_CODE (decl) == VAR_DECL
172 || TREE_CODE (decl) == PARM_DECL
173 || TREE_CODE (decl) == RESULT_DECL));
175 /* Non-aliased variables can not be pointed to. */
176 if (!may_be_aliased (decl))
179 /* ADDR_EXPR pointers either just offset another pointer or directly
180 specify the pointed-to set. */
181 if (TREE_CODE (ptr) == ADDR_EXPR)
183 tree base = get_base_address (TREE_OPERAND (ptr, 0));
185 && (INDIRECT_REF_P (base)
186 || TREE_CODE (base) == MEM_REF))
187 ptr = TREE_OPERAND (base, 0);
192 && CONSTANT_CLASS_P (base))
198 /* We can end up with dereferencing constant pointers.
199 Just bail out in this case. */
200 if (TREE_CODE (ptr) == INTEGER_CST)
203 /* If we do not have useful points-to information for this pointer
204 we cannot disambiguate anything else. */
205 pi = SSA_NAME_PTR_INFO (ptr);
209 /* If the decl can be used as a restrict tag and we have a restrict
210 pointer and that pointers points-to set doesn't contain this decl
211 then they can't alias. */
212 if (DECL_RESTRICTED_P (decl)
213 && TYPE_RESTRICT (TREE_TYPE (ptr))
214 && pi->pt.vars_contains_restrict)
215 return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
217 return pt_solution_includes (&pi->pt, decl);
220 /* Return true if dereferenced PTR1 and PTR2 may alias.
221 The caller is responsible for applying TBAA to see if accesses
222 through PTR1 and PTR2 may conflict at all. */
225 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
227 struct ptr_info_def *pi1, *pi2;
229 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
230 || TREE_CODE (ptr1) == ADDR_EXPR
231 || TREE_CODE (ptr1) == INTEGER_CST)
232 && (TREE_CODE (ptr2) == SSA_NAME
233 || TREE_CODE (ptr2) == ADDR_EXPR
234 || TREE_CODE (ptr2) == INTEGER_CST));
236 /* ADDR_EXPR pointers either just offset another pointer or directly
237 specify the pointed-to set. */
238 if (TREE_CODE (ptr1) == ADDR_EXPR)
240 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
242 && (INDIRECT_REF_P (base)
243 || TREE_CODE (base) == MEM_REF))
244 ptr1 = TREE_OPERAND (base, 0);
247 return ptr_deref_may_alias_decl_p (ptr2, base);
251 if (TREE_CODE (ptr2) == ADDR_EXPR)
253 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
255 && (INDIRECT_REF_P (base)
256 || TREE_CODE (base) == MEM_REF))
257 ptr2 = TREE_OPERAND (base, 0);
260 return ptr_deref_may_alias_decl_p (ptr1, base);
265 /* We can end up with dereferencing constant pointers.
266 Just bail out in this case. */
267 if (TREE_CODE (ptr1) == INTEGER_CST
268 || TREE_CODE (ptr2) == INTEGER_CST)
271 /* We may end up with two empty points-to solutions for two same pointers.
272 In this case we still want to say both pointers alias, so shortcut
277 /* If we do not have useful points-to information for either pointer
278 we cannot disambiguate anything else. */
279 pi1 = SSA_NAME_PTR_INFO (ptr1);
280 pi2 = SSA_NAME_PTR_INFO (ptr2);
284 /* If both pointers are restrict-qualified try to disambiguate
285 with restrict information. */
286 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
287 && TYPE_RESTRICT (TREE_TYPE (ptr2))
288 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
291 /* ??? This does not use TBAA to prune decls from the intersection
292 that not both pointers may access. */
293 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
296 /* Return true if dereferencing PTR may alias *REF.
297 The caller is responsible for applying TBAA to see if PTR
298 may access *REF at all. */
301 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
303 tree base = ao_ref_base (ref);
305 if (INDIRECT_REF_P (base)
306 || TREE_CODE (base) == MEM_REF)
307 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
308 else if (SSA_VAR_P (base))
309 return ptr_deref_may_alias_decl_p (ptr, base);
315 /* Dump alias information on FILE. */
318 dump_alias_info (FILE *file)
322 = lang_hooks.decl_printable_name (current_function_decl, 2);
323 referenced_var_iterator rvi;
326 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
328 fprintf (file, "Aliased symbols\n\n");
330 FOR_EACH_REFERENCED_VAR (var, rvi)
332 if (may_be_aliased (var))
333 dump_variable (file, var);
336 fprintf (file, "\nCall clobber information\n");
338 fprintf (file, "\nESCAPED");
339 dump_points_to_solution (file, &cfun->gimple_df->escaped);
341 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
343 for (i = 1; i < num_ssa_names; i++)
345 tree ptr = ssa_name (i);
346 struct ptr_info_def *pi;
349 || SSA_NAME_IN_FREE_LIST (ptr))
352 pi = SSA_NAME_PTR_INFO (ptr);
354 dump_points_to_info_for (file, ptr);
357 fprintf (file, "\n");
361 /* Dump alias information on stderr. */
364 debug_alias_info (void)
366 dump_alias_info (stderr);
370 /* Return the alias information associated with pointer T. It creates a
371 new instance if none existed. */
373 struct ptr_info_def *
374 get_ptr_info (tree t)
376 struct ptr_info_def *pi;
378 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
380 pi = SSA_NAME_PTR_INFO (t);
383 pi = ggc_alloc_cleared_ptr_info_def ();
384 pt_solution_reset (&pi->pt);
385 SSA_NAME_PTR_INFO (t) = pi;
391 /* Dump the points-to set *PT into FILE. */
394 dump_points_to_solution (FILE *file, struct pt_solution *pt)
397 fprintf (file, ", points-to anything");
400 fprintf (file, ", points-to non-local");
403 fprintf (file, ", points-to escaped");
406 fprintf (file, ", points-to unit escaped");
409 fprintf (file, ", points-to NULL");
413 fprintf (file, ", points-to vars: ");
414 dump_decl_set (file, pt->vars);
415 if (pt->vars_contains_global)
416 fprintf (file, " (includes global vars)");
417 if (pt->vars_contains_restrict)
418 fprintf (file, " (includes restrict tags)");
422 /* Dump points-to information for SSA_NAME PTR into FILE. */
425 dump_points_to_info_for (FILE *file, tree ptr)
427 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
429 print_generic_expr (file, ptr, dump_flags);
432 dump_points_to_solution (file, &pi->pt);
434 fprintf (file, ", points-to anything");
436 fprintf (file, "\n");
440 /* Dump points-to information for VAR into stderr. */
443 debug_points_to_info_for (tree var)
445 dump_points_to_info_for (stderr, var);
449 /* Initializes the alias-oracle reference representation *R from REF. */
452 ao_ref_init (ao_ref *r, tree ref)
459 r->ref_alias_set = -1;
460 r->base_alias_set = -1;
463 /* Returns the base object of the memory reference *REF. */
466 ao_ref_base (ao_ref *ref)
470 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
475 /* Returns the base object alias set of the memory reference *REF. */
477 static alias_set_type
478 ao_ref_base_alias_set (ao_ref *ref)
481 if (ref->base_alias_set != -1)
482 return ref->base_alias_set;
486 while (handled_component_p (base_ref))
487 base_ref = TREE_OPERAND (base_ref, 0);
488 ref->base_alias_set = get_alias_set (base_ref);
489 return ref->base_alias_set;
492 /* Returns the reference alias set of the memory reference *REF. */
495 ao_ref_alias_set (ao_ref *ref)
497 if (ref->ref_alias_set != -1)
498 return ref->ref_alias_set;
499 ref->ref_alias_set = get_alias_set (ref->ref);
500 return ref->ref_alias_set;
503 /* Init an alias-oracle reference representation from a gimple pointer
504 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
505 size is assumed to be unknown. The access is assumed to be only
506 to or after of the pointer target, not before it. */
509 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
511 HOST_WIDE_INT t1, t2;
512 ref->ref = NULL_TREE;
513 if (TREE_CODE (ptr) == ADDR_EXPR)
514 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
515 &ref->offset, &t1, &t2);
518 ref->base = build2 (MEM_REF, char_type_node,
519 ptr, build_int_cst (ptr_type_node, 0));
523 && host_integerp (size, 0)
524 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
525 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
527 ref->max_size = ref->size = -1;
528 ref->ref_alias_set = 0;
529 ref->base_alias_set = 0;
532 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
533 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
537 same_type_for_tbaa (tree type1, tree type2)
539 type1 = TYPE_MAIN_VARIANT (type1);
540 type2 = TYPE_MAIN_VARIANT (type2);
542 /* If we would have to do structural comparison bail out. */
543 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
544 || TYPE_STRUCTURAL_EQUALITY_P (type2))
547 /* Compare the canonical types. */
548 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
551 /* ??? Array types are not properly unified in all cases as we have
552 spurious changes in the index types for example. Removing this
553 causes all sorts of problems with the Fortran frontend. */
554 if (TREE_CODE (type1) == ARRAY_TYPE
555 && TREE_CODE (type2) == ARRAY_TYPE)
558 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
559 object of one of its constrained subtypes, e.g. when a function with an
560 unconstrained parameter passed by reference is called on an object and
561 inlined. But, even in the case of a fixed size, type and subtypes are
562 not equivalent enough as to share the same TYPE_CANONICAL, since this
563 would mean that conversions between them are useless, whereas they are
564 not (e.g. type and subtypes can have different modes). So, in the end,
565 they are only guaranteed to have the same alias set. */
566 if (get_alias_set (type1) == get_alias_set (type2))
569 /* The types are known to be not equal. */
573 /* Determine if the two component references REF1 and REF2 which are
574 based on access types TYPE1 and TYPE2 and of which at least one is based
575 on an indirect reference may alias. REF2 is the only one that can
576 be a decl in which case REF2_IS_DECL is true.
577 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
578 are the respective alias sets. */
581 aliasing_component_refs_p (tree ref1, tree type1,
582 alias_set_type ref1_alias_set,
583 alias_set_type base1_alias_set,
584 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
585 tree ref2, tree type2,
586 alias_set_type ref2_alias_set,
587 alias_set_type base2_alias_set,
588 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
591 /* If one reference is a component references through pointers try to find a
592 common base and apply offset based disambiguation. This handles
594 struct A { int i; int j; } *q;
595 struct B { struct A a; int k; } *p;
596 disambiguating q->i and p->a.j. */
600 /* Now search for the type1 in the access path of ref2. This
601 would be a common base for doing offset based disambiguation on. */
603 while (handled_component_p (*refp)
604 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
605 refp = &TREE_OPERAND (*refp, 0);
606 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
607 /* If we couldn't compare types we have to bail out. */
610 else if (same_p == 1)
612 HOST_WIDE_INT offadj, sztmp, msztmp;
613 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
615 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
617 /* If we didn't find a common base, try the other way around. */
619 while (handled_component_p (*refp)
620 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
621 refp = &TREE_OPERAND (*refp, 0);
622 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
623 /* If we couldn't compare types we have to bail out. */
626 else if (same_p == 1)
628 HOST_WIDE_INT offadj, sztmp, msztmp;
629 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
631 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
634 /* If we have two type access paths B1.path1 and B2.path2 they may
635 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
636 But we can still have a path that goes B1.path1...B2.path2 with
637 a part that we do not see. So we can only disambiguate now
638 if there is no B2 in the tail of path1 and no B1 on the
640 if (base1_alias_set == ref2_alias_set
641 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
643 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
645 return (base2_alias_set == ref1_alias_set
646 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
650 /* Return true if two memory references based on the variables BASE1
651 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
652 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
655 decl_refs_may_alias_p (tree base1,
656 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
658 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
660 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
662 /* If both references are based on different variables, they cannot alias. */
666 /* If both references are based on the same variable, they cannot alias if
667 the accesses do not overlap. */
668 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
671 /* Return true if an indirect reference based on *PTR1 constrained
672 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
673 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
674 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
675 in which case they are computed on-demand. REF1 and REF2
676 if non-NULL are the complete memory reference trees. */
679 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
680 HOST_WIDE_INT offset1,
681 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
682 alias_set_type ref1_alias_set,
683 alias_set_type base1_alias_set,
684 tree ref2 ATTRIBUTE_UNUSED, tree base2,
685 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
686 alias_set_type ref2_alias_set,
687 alias_set_type base2_alias_set, bool tbaa_p)
689 tree ptr1 = TREE_OPERAND (base1, 0);
691 HOST_WIDE_INT offset1p = offset1;
693 if (TREE_CODE (base1) == MEM_REF)
694 offset1p = offset1 + mem_ref_offset (base1).low * BITS_PER_UNIT;
696 /* If only one reference is based on a variable, they cannot alias if
697 the pointer access is beyond the extent of the variable access.
698 (the pointer base cannot validly point to an offset less than zero
700 They also cannot alias if the pointer may not point to the decl. */
701 if (!ranges_overlap_p (MAX (0, offset1p), -1, offset2, max_size2))
703 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
706 /* Disambiguations that rely on strict aliasing rules follow. */
707 if (!flag_strict_aliasing || !tbaa_p)
710 if (TREE_CODE (base1) == MEM_REF)
711 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
713 ptrtype1 = TREE_TYPE (ptr1);
715 /* If the alias set for a pointer access is zero all bets are off. */
716 if (base1_alias_set == -1)
717 base1_alias_set = get_deref_alias_set (ptrtype1);
718 if (base1_alias_set == 0)
720 if (base2_alias_set == -1)
721 base2_alias_set = get_alias_set (base2);
723 /* If both references are through the same type, they do not alias
724 if the accesses do not overlap. This does extra disambiguation
725 for mixed/pointer accesses but requires strict aliasing.
726 For MEM_REFs we require that the component-ref offset we computed
727 is relative to the start of the type which we ensure by
728 comparing rvalue and access type and disregarding the constant
730 if ((TREE_CODE (base1) != MEM_REF
731 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
732 && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
733 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
735 /* When we are trying to disambiguate an access with a pointer dereference
736 as base versus one with a decl as base we can use both the size
737 of the decl and its dynamic type for extra disambiguation.
738 ??? We do not know anything about the dynamic type of the decl
739 other than that its alias-set contains base2_alias_set as a subset
740 which does not help us here. */
741 /* As we know nothing useful about the dynamic type of the decl just
742 use the usual conflict check rather than a subset test.
743 ??? We could introduce -fvery-strict-aliasing when the language
744 does not allow decls to have a dynamic type that differs from their
745 static type. Then we can check
746 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
747 if (base1_alias_set != base2_alias_set
748 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
750 /* If the size of the access relevant for TBAA through the pointer
751 is bigger than the size of the decl we can't possibly access the
752 decl via that pointer. */
753 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
754 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
755 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
756 /* ??? This in turn may run afoul when a decl of type T which is
757 a member of union type U is accessed through a pointer to
758 type U and sizeof T is smaller than sizeof U. */
759 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
760 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
761 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
764 /* Do access-path based disambiguation. */
766 && handled_component_p (ref1)
767 && handled_component_p (ref2)
768 && (TREE_CODE (base1) != MEM_REF
769 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
770 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
771 ref1_alias_set, base1_alias_set,
773 ref2, TREE_TYPE (base2),
774 ref2_alias_set, base2_alias_set,
775 offset2, max_size2, true);
780 /* Return true if two indirect references based on *PTR1
781 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
782 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
783 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
784 in which case they are computed on-demand. REF1 and REF2
785 if non-NULL are the complete memory reference trees. */
788 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
789 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
790 alias_set_type ref1_alias_set,
791 alias_set_type base1_alias_set,
792 tree ref2 ATTRIBUTE_UNUSED, tree base2,
793 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
794 alias_set_type ref2_alias_set,
795 alias_set_type base2_alias_set, bool tbaa_p)
797 tree ptr1 = TREE_OPERAND (base1, 0);
798 tree ptr2 = TREE_OPERAND (base2, 0);
799 tree ptrtype1, ptrtype2;
801 /* If both bases are based on pointers they cannot alias if they may not
802 point to the same memory object or if they point to the same object
803 and the accesses do not overlap. */
804 if ((!cfun || gimple_in_ssa_p (cfun))
805 && operand_equal_p (ptr1, ptr2, 0))
807 if (TREE_CODE (base1) == MEM_REF)
808 offset1 += mem_ref_offset (base1).low * BITS_PER_UNIT;
809 if (TREE_CODE (base2) == MEM_REF)
810 offset2 += mem_ref_offset (base2).low * BITS_PER_UNIT;
811 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
813 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
816 /* Disambiguations that rely on strict aliasing rules follow. */
817 if (!flag_strict_aliasing || !tbaa_p)
820 if (TREE_CODE (base1) == MEM_REF)
821 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
823 ptrtype1 = TREE_TYPE (ptr1);
824 if (TREE_CODE (base2) == MEM_REF)
825 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
827 ptrtype2 = TREE_TYPE (ptr2);
829 /* If the alias set for a pointer access is zero all bets are off. */
830 if (base1_alias_set == -1)
831 base1_alias_set = get_deref_alias_set (ptrtype1);
832 if (base1_alias_set == 0)
834 if (base2_alias_set == -1)
835 base2_alias_set = get_deref_alias_set (ptrtype2);
836 if (base2_alias_set == 0)
839 /* If both references are through the same type, they do not alias
840 if the accesses do not overlap. This does extra disambiguation
841 for mixed/pointer accesses but requires strict aliasing. */
842 if ((TREE_CODE (base1) != MEM_REF
843 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
844 && (TREE_CODE (base2) != MEM_REF
845 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
846 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
847 TREE_TYPE (ptrtype2)) == 1)
848 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
850 /* Do type-based disambiguation. */
851 if (base1_alias_set != base2_alias_set
852 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
855 /* Do access-path based disambiguation. */
857 && handled_component_p (ref1)
858 && handled_component_p (ref2)
859 && (TREE_CODE (base1) != MEM_REF
860 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
861 && (TREE_CODE (base2) != MEM_REF
862 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
863 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
864 ref1_alias_set, base1_alias_set,
866 ref2, TREE_TYPE (ptrtype2),
867 ref2_alias_set, base2_alias_set,
868 offset2, max_size2, false);
873 /* Return true, if the two memory references REF1 and REF2 may alias. */
876 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
879 HOST_WIDE_INT offset1 = 0, offset2 = 0;
880 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
881 bool var1_p, var2_p, ind1_p, ind2_p;
883 gcc_checking_assert ((!ref1->ref
884 || TREE_CODE (ref1->ref) == SSA_NAME
885 || DECL_P (ref1->ref)
886 || handled_component_p (ref1->ref)
887 || INDIRECT_REF_P (ref1->ref)
888 || TREE_CODE (ref1->ref) == MEM_REF
889 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
891 || TREE_CODE (ref2->ref) == SSA_NAME
892 || DECL_P (ref2->ref)
893 || handled_component_p (ref2->ref)
894 || INDIRECT_REF_P (ref2->ref)
895 || TREE_CODE (ref2->ref) == MEM_REF
896 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
898 /* Decompose the references into their base objects and the access. */
899 base1 = ao_ref_base (ref1);
900 offset1 = ref1->offset;
901 max_size1 = ref1->max_size;
902 base2 = ao_ref_base (ref2);
903 offset2 = ref2->offset;
904 max_size2 = ref2->max_size;
906 /* We can end up with registers or constants as bases for example from
907 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
908 which is seen as a struct copy. */
909 if (TREE_CODE (base1) == SSA_NAME
910 || TREE_CODE (base2) == SSA_NAME
911 || TREE_CODE (base1) == CONST_DECL
912 || TREE_CODE (base2) == CONST_DECL
913 || is_gimple_min_invariant (base1)
914 || is_gimple_min_invariant (base2))
917 /* We can end up refering to code via function and label decls.
918 As we likely do not properly track code aliases conservatively
920 if (TREE_CODE (base1) == FUNCTION_DECL
921 || TREE_CODE (base2) == FUNCTION_DECL
922 || TREE_CODE (base1) == LABEL_DECL
923 || TREE_CODE (base2) == LABEL_DECL)
926 /* Defer to simple offset based disambiguation if we have
927 references based on two decls. Do this before defering to
928 TBAA to handle must-alias cases in conformance with the
929 GCC extension of allowing type-punning through unions. */
930 var1_p = SSA_VAR_P (base1);
931 var2_p = SSA_VAR_P (base2);
932 if (var1_p && var2_p)
933 return decl_refs_may_alias_p (base1, offset1, max_size1,
934 base2, offset2, max_size2);
936 ind1_p = INDIRECT_REF_P (base1) || (TREE_CODE (base1) == MEM_REF);
937 ind2_p = INDIRECT_REF_P (base2) || (TREE_CODE (base2) == MEM_REF);
939 /* Canonicalize the pointer-vs-decl case. */
940 if (ind1_p && var2_p)
945 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
946 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
947 tmp2 = base1; base1 = base2; base2 = tmp2;
948 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
955 /* First defer to TBAA if possible. */
957 && flag_strict_aliasing
958 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
959 ao_ref_alias_set (ref2)))
962 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
963 TBAA disambiguation based on the access type is possible, so bail
964 out only after that check. */
965 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
966 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
969 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
970 if (var1_p && ind2_p)
971 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
973 ao_ref_alias_set (ref2), -1,
976 ao_ref_alias_set (ref1),
977 ao_ref_base_alias_set (ref1),
979 else if (ind1_p && ind2_p)
980 return indirect_refs_may_alias_p (ref1->ref, base1,
982 ao_ref_alias_set (ref1), -1,
985 ao_ref_alias_set (ref2), -1,
992 refs_may_alias_p (tree ref1, tree ref2)
996 ao_ref_init (&r1, ref1);
997 ao_ref_init (&r2, ref2);
998 res = refs_may_alias_p_1 (&r1, &r2, true);
1000 ++alias_stats.refs_may_alias_p_may_alias;
1002 ++alias_stats.refs_may_alias_p_no_alias;
1006 /* Returns true if there is a anti-dependence for the STORE that
1007 executes after the LOAD. */
1010 refs_anti_dependent_p (tree load, tree store)
1013 ao_ref_init (&r1, load);
1014 ao_ref_init (&r2, store);
1015 return refs_may_alias_p_1 (&r1, &r2, false);
1018 /* Returns true if there is a output dependence for the stores
1019 STORE1 and STORE2. */
1022 refs_output_dependent_p (tree store1, tree store2)
1025 ao_ref_init (&r1, store1);
1026 ao_ref_init (&r2, store2);
1027 return refs_may_alias_p_1 (&r1, &r2, false);
1030 /* If the call CALL may use the memory reference REF return true,
1031 otherwise return false. */
1034 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1038 int flags = gimple_call_flags (call);
1040 /* Const functions without a static chain do not implicitly use memory. */
1041 if (!gimple_call_chain (call)
1042 && (flags & (ECF_CONST|ECF_NOVOPS)))
1045 base = ao_ref_base (ref);
1049 /* If the reference is based on a decl that is not aliased the call
1050 cannot possibly use it. */
1052 && !may_be_aliased (base)
1053 /* But local statics can be used through recursion. */
1054 && !is_global_var (base))
1057 callee = gimple_call_fndecl (call);
1059 /* Handle those builtin functions explicitly that do not act as
1060 escape points. See tree-ssa-structalias.c:find_func_aliases
1061 for the list of builtins we might need to handle here. */
1062 if (callee != NULL_TREE
1063 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1064 switch (DECL_FUNCTION_CODE (callee))
1066 /* All the following functions clobber memory pointed to by
1067 their first argument. */
1068 case BUILT_IN_STRCPY:
1069 case BUILT_IN_STRNCPY:
1070 case BUILT_IN_MEMCPY:
1071 case BUILT_IN_MEMMOVE:
1072 case BUILT_IN_MEMPCPY:
1073 case BUILT_IN_STPCPY:
1074 case BUILT_IN_STPNCPY:
1075 case BUILT_IN_STRCAT:
1076 case BUILT_IN_STRNCAT:
1079 tree size = NULL_TREE;
1080 if (gimple_call_num_args (call) == 3)
1081 size = gimple_call_arg (call, 2);
1082 ao_ref_init_from_ptr_and_size (&dref,
1083 gimple_call_arg (call, 1),
1085 return refs_may_alias_p_1 (&dref, ref, false);
1087 case BUILT_IN_BCOPY:
1090 tree size = gimple_call_arg (call, 2);
1091 ao_ref_init_from_ptr_and_size (&dref,
1092 gimple_call_arg (call, 0),
1094 return refs_may_alias_p_1 (&dref, ref, false);
1096 /* The following builtins do not read from memory. */
1098 case BUILT_IN_MALLOC:
1099 case BUILT_IN_CALLOC:
1100 case BUILT_IN_MEMSET:
1101 case BUILT_IN_FREXP:
1102 case BUILT_IN_FREXPF:
1103 case BUILT_IN_FREXPL:
1104 case BUILT_IN_GAMMA_R:
1105 case BUILT_IN_GAMMAF_R:
1106 case BUILT_IN_GAMMAL_R:
1107 case BUILT_IN_LGAMMA_R:
1108 case BUILT_IN_LGAMMAF_R:
1109 case BUILT_IN_LGAMMAL_R:
1111 case BUILT_IN_MODFF:
1112 case BUILT_IN_MODFL:
1113 case BUILT_IN_REMQUO:
1114 case BUILT_IN_REMQUOF:
1115 case BUILT_IN_REMQUOL:
1116 case BUILT_IN_SINCOS:
1117 case BUILT_IN_SINCOSF:
1118 case BUILT_IN_SINCOSL:
1122 /* Fallthru to general call handling. */;
1125 /* Check if base is a global static variable that is not read
1127 if (TREE_CODE (base) == VAR_DECL
1128 && TREE_STATIC (base))
1132 if (callee != NULL_TREE
1134 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1135 && bitmap_bit_p (not_read, DECL_UID (base)))
1139 /* Check if the base variable is call-used. */
1142 if (pt_solution_includes (gimple_call_use_set (call), base))
1145 else if ((INDIRECT_REF_P (base)
1146 || TREE_CODE (base) == MEM_REF)
1147 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1149 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1153 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1159 /* Inspect call arguments for passed-by-value aliases. */
1161 for (i = 0; i < gimple_call_num_args (call); ++i)
1163 tree op = gimple_call_arg (call, i);
1164 int flags = gimple_call_arg_flags (call, i);
1166 if (flags & EAF_UNUSED)
1169 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1170 op = TREE_OPERAND (op, 0);
1172 if (TREE_CODE (op) != SSA_NAME
1173 && !is_gimple_min_invariant (op))
1176 ao_ref_init (&r, op);
1177 if (refs_may_alias_p_1 (&r, ref, true))
1186 ref_maybe_used_by_call_p (gimple call, tree ref)
1190 ao_ref_init (&r, ref);
1191 res = ref_maybe_used_by_call_p_1 (call, &r);
1193 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1195 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1200 /* If the statement STMT may use the memory reference REF return
1201 true, otherwise return false. */
1204 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1206 if (is_gimple_assign (stmt))
1210 /* All memory assign statements are single. */
1211 if (!gimple_assign_single_p (stmt))
1214 rhs = gimple_assign_rhs1 (stmt);
1215 if (is_gimple_reg (rhs)
1216 || is_gimple_min_invariant (rhs)
1217 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1220 return refs_may_alias_p (rhs, ref);
1222 else if (is_gimple_call (stmt))
1223 return ref_maybe_used_by_call_p (stmt, ref);
1228 /* If the call in statement CALL may clobber the memory reference REF
1229 return true, otherwise return false. */
1232 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1237 /* If the call is pure or const it cannot clobber anything. */
1238 if (gimple_call_flags (call)
1239 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1242 base = ao_ref_base (ref);
1246 if (TREE_CODE (base) == SSA_NAME
1247 || CONSTANT_CLASS_P (base))
1250 /* If the reference is based on a decl that is not aliased the call
1251 cannot possibly clobber it. */
1253 && !may_be_aliased (base)
1254 /* But local non-readonly statics can be modified through recursion
1255 or the call may implement a threading barrier which we must
1256 treat as may-def. */
1257 && (TREE_READONLY (base)
1258 || !is_global_var (base)))
1261 callee = gimple_call_fndecl (call);
1263 /* Handle those builtin functions explicitly that do not act as
1264 escape points. See tree-ssa-structalias.c:find_func_aliases
1265 for the list of builtins we might need to handle here. */
1266 if (callee != NULL_TREE
1267 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1268 switch (DECL_FUNCTION_CODE (callee))
1270 /* All the following functions clobber memory pointed to by
1271 their first argument. */
1272 case BUILT_IN_STRCPY:
1273 case BUILT_IN_STRNCPY:
1274 case BUILT_IN_MEMCPY:
1275 case BUILT_IN_MEMMOVE:
1276 case BUILT_IN_MEMPCPY:
1277 case BUILT_IN_STPCPY:
1278 case BUILT_IN_STPNCPY:
1279 case BUILT_IN_STRCAT:
1280 case BUILT_IN_STRNCAT:
1281 case BUILT_IN_MEMSET:
1284 tree size = NULL_TREE;
1285 if (gimple_call_num_args (call) == 3)
1286 size = gimple_call_arg (call, 2);
1287 ao_ref_init_from_ptr_and_size (&dref,
1288 gimple_call_arg (call, 0),
1290 return refs_may_alias_p_1 (&dref, ref, false);
1292 case BUILT_IN_BCOPY:
1295 tree size = gimple_call_arg (call, 2);
1296 ao_ref_init_from_ptr_and_size (&dref,
1297 gimple_call_arg (call, 1),
1299 return refs_may_alias_p_1 (&dref, ref, false);
1301 /* Allocating memory does not have any side-effects apart from
1302 being the definition point for the pointer. */
1303 case BUILT_IN_MALLOC:
1304 case BUILT_IN_CALLOC:
1305 /* Unix98 specifies that errno is set on allocation failure.
1306 Until we properly can track the errno location assume it
1307 is not a local decl but external or anonymous storage in
1308 a different translation unit. Also assume it is of
1309 type int as required by the standard. */
1311 && TREE_TYPE (base) == integer_type_node)
1313 struct ptr_info_def *pi;
1315 && !TREE_STATIC (base))
1317 else if ((INDIRECT_REF_P (base)
1318 || TREE_CODE (base) == MEM_REF)
1319 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1320 && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1321 return pi->pt.anything || pi->pt.nonlocal;
1324 /* Freeing memory kills the pointed-to memory. More importantly
1325 the call has to serve as a barrier for moving loads and stores
1329 tree ptr = gimple_call_arg (call, 0);
1330 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1332 case BUILT_IN_GAMMA_R:
1333 case BUILT_IN_GAMMAF_R:
1334 case BUILT_IN_GAMMAL_R:
1335 case BUILT_IN_LGAMMA_R:
1336 case BUILT_IN_LGAMMAF_R:
1337 case BUILT_IN_LGAMMAL_R:
1339 tree out = gimple_call_arg (call, 1);
1340 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1342 if (flag_errno_math)
1346 case BUILT_IN_FREXP:
1347 case BUILT_IN_FREXPF:
1348 case BUILT_IN_FREXPL:
1350 case BUILT_IN_MODFF:
1351 case BUILT_IN_MODFL:
1353 tree out = gimple_call_arg (call, 1);
1354 return ptr_deref_may_alias_ref_p_1 (out, ref);
1356 case BUILT_IN_REMQUO:
1357 case BUILT_IN_REMQUOF:
1358 case BUILT_IN_REMQUOL:
1360 tree out = gimple_call_arg (call, 2);
1361 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1363 if (flag_errno_math)
1367 case BUILT_IN_SINCOS:
1368 case BUILT_IN_SINCOSF:
1369 case BUILT_IN_SINCOSL:
1371 tree sin = gimple_call_arg (call, 1);
1372 tree cos = gimple_call_arg (call, 2);
1373 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1374 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1377 /* Fallthru to general call handling. */;
1380 /* Check if base is a global static variable that is not written
1382 if (callee != NULL_TREE
1383 && TREE_CODE (base) == VAR_DECL
1384 && TREE_STATIC (base))
1389 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1390 && bitmap_bit_p (not_written, DECL_UID (base)))
1394 /* Check if the base variable is call-clobbered. */
1396 return pt_solution_includes (gimple_call_clobber_set (call), base);
1397 else if ((INDIRECT_REF_P (base)
1398 || TREE_CODE (base) == MEM_REF)
1399 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1401 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1405 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1411 /* If the call in statement CALL may clobber the memory reference REF
1412 return true, otherwise return false. */
1415 call_may_clobber_ref_p (gimple call, tree ref)
1419 ao_ref_init (&r, ref);
1420 res = call_may_clobber_ref_p_1 (call, &r);
1422 ++alias_stats.call_may_clobber_ref_p_may_alias;
1424 ++alias_stats.call_may_clobber_ref_p_no_alias;
1429 /* If the statement STMT may clobber the memory reference REF return true,
1430 otherwise return false. */
1433 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1435 if (is_gimple_call (stmt))
1437 tree lhs = gimple_call_lhs (stmt);
1439 && !is_gimple_reg (lhs))
1442 ao_ref_init (&r, lhs);
1443 if (refs_may_alias_p_1 (ref, &r, true))
1447 return call_may_clobber_ref_p_1 (stmt, ref);
1449 else if (gimple_assign_single_p (stmt))
1451 tree lhs = gimple_assign_lhs (stmt);
1452 if (!is_gimple_reg (lhs))
1455 ao_ref_init (&r, gimple_assign_lhs (stmt));
1456 return refs_may_alias_p_1 (ref, &r, true);
1459 else if (gimple_code (stmt) == GIMPLE_ASM)
1466 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1469 ao_ref_init (&r, ref);
1470 return stmt_may_clobber_ref_p_1 (stmt, &r);
1474 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1475 TARGET or a statement clobbering the memory reference REF in which
1476 case false is returned. The walk starts with VUSE, one argument of PHI. */
1479 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1480 tree vuse, bitmap *visited)
1483 *visited = BITMAP_ALLOC (NULL);
1485 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1487 /* Walk until we hit the target. */
1488 while (vuse != target)
1490 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1491 /* Recurse for PHI nodes. */
1492 if (gimple_code (def_stmt) == GIMPLE_PHI)
1494 /* An already visited PHI node ends the walk successfully. */
1495 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1497 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1502 /* A clobbering statement or the end of the IL ends it failing. */
1503 else if (gimple_nop_p (def_stmt)
1504 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1506 vuse = gimple_vuse (def_stmt);
1511 /* Starting from a PHI node for the virtual operand of the memory reference
1512 REF find a continuation virtual operand that allows to continue walking
1513 statements dominating PHI skipping only statements that cannot possibly
1514 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1518 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1520 unsigned nargs = gimple_phi_num_args (phi);
1522 /* Through a single-argument PHI we can simply look through. */
1524 return PHI_ARG_DEF (phi, 0);
1526 /* For two arguments try to skip non-aliasing code until we hit
1527 the phi argument definition that dominates the other one. */
1530 tree arg0 = PHI_ARG_DEF (phi, 0);
1531 tree arg1 = PHI_ARG_DEF (phi, 1);
1532 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1533 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1538 else if (gimple_nop_p (def0)
1539 || (!gimple_nop_p (def1)
1540 && dominated_by_p (CDI_DOMINATORS,
1541 gimple_bb (def1), gimple_bb (def0))))
1543 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1546 else if (gimple_nop_p (def1)
1547 || dominated_by_p (CDI_DOMINATORS,
1548 gimple_bb (def0), gimple_bb (def1)))
1550 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1553 /* Special case of a diamond:
1555 goto (cond) ? L1 : L2
1556 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1558 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1559 L3: MEM_4 = PHI<MEM_2, MEM_3>
1560 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1561 dominate each other, but still we can easily skip this PHI node
1562 if we recognize that the vuse MEM operand is the same for both,
1563 and that we can skip both statements (they don't clobber us).
1564 This is still linear. Don't use maybe_skip_until, that might
1565 potentially be slow. */
1566 else if ((common_vuse = gimple_vuse (def0))
1567 && common_vuse == gimple_vuse (def1))
1569 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1570 && !stmt_may_clobber_ref_p_1 (def1, ref))
1578 /* Based on the memory reference REF and its virtual use VUSE call
1579 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1580 itself. That is, for each virtual use for which its defining statement
1581 does not clobber REF.
1583 WALKER is called with REF, the current virtual use and DATA. If
1584 WALKER returns non-NULL the walk stops and its result is returned.
1585 At the end of a non-successful walk NULL is returned.
1587 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1588 use which definition is a statement that may clobber REF and DATA.
1589 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1590 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1591 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1592 to adjust REF and *DATA to make that valid.
1594 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1597 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1598 void *(*walker)(ao_ref *, tree, void *),
1599 void *(*translate)(ao_ref *, tree, void *), void *data)
1601 bitmap visited = NULL;
1604 timevar_push (TV_ALIAS_STMT_WALK);
1610 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1611 res = (*walker) (ref, vuse, data);
1615 def_stmt = SSA_NAME_DEF_STMT (vuse);
1616 if (gimple_nop_p (def_stmt))
1618 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1619 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1622 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1626 res = (*translate) (ref, vuse, data);
1627 /* Failed lookup and translation. */
1628 if (res == (void *)-1)
1633 /* Lookup succeeded. */
1634 else if (res != NULL)
1636 /* Translation succeeded, continue walking. */
1638 vuse = gimple_vuse (def_stmt);
1644 BITMAP_FREE (visited);
1646 timevar_pop (TV_ALIAS_STMT_WALK);
1652 /* Based on the memory reference REF call WALKER for each vdef which
1653 defining statement may clobber REF, starting with VDEF. If REF
1654 is NULL_TREE, each defining statement is visited.
1656 WALKER is called with REF, the current vdef and DATA. If WALKER
1657 returns true the walk is stopped, otherwise it continues.
1659 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1660 PHI argument (but only one walk continues on merge points), the
1661 return value is true if any of the walks was successful.
1663 The function returns the number of statements walked. */
1666 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1667 bool (*walker)(ao_ref *, tree, void *), void *data,
1668 bitmap *visited, unsigned int cnt)
1672 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1675 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1678 if (gimple_nop_p (def_stmt))
1680 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1684 *visited = BITMAP_ALLOC (NULL);
1685 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1686 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1687 walker, data, visited, 0);
1691 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1694 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1695 && (*walker) (ref, vdef, data))
1698 vdef = gimple_vuse (def_stmt);
1704 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1705 bool (*walker)(ao_ref *, tree, void *), void *data,
1708 bitmap local_visited = NULL;
1711 timevar_push (TV_ALIAS_STMT_WALK);
1713 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1714 visited ? visited : &local_visited, 0);
1716 BITMAP_FREE (local_visited);
1718 timevar_pop (TV_ALIAS_STMT_WALK);