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"
35 #include "tree-pretty-print.h"
36 #include "tree-dump.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "tree-pass.h"
43 #include "ipa-type-escape.h"
47 #include "pointer-set.h"
48 #include "alloc-pool.h"
49 #include "tree-ssa-alias.h"
51 /* Broad overview of how alias analysis on gimple works:
53 Statements clobbering or using memory are linked through the
54 virtual operand factored use-def chain. The virtual operand
55 is unique per function, its symbol is accessible via gimple_vop (cfun).
56 Virtual operands are used for efficiently walking memory statements
57 in the gimple IL and are useful for things like value-numbering as
58 a generation count for memory references.
60 SSA_NAME pointers may have associated points-to information
61 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
62 points-to information is (re-)computed by the TODO_rebuild_alias
63 pass manager todo. Points-to information is also used for more
64 precise tracking of call-clobbered and call-used variables and
65 related disambiguations.
67 This file contains functions for disambiguating memory references,
68 the so called alias-oracle and tools for walking of the gimple IL.
70 The main alias-oracle entry-points are
72 bool stmt_may_clobber_ref_p (gimple, tree)
74 This function queries if a statement may invalidate (parts of)
75 the memory designated by the reference tree argument.
77 bool ref_maybe_used_by_stmt_p (gimple, tree)
79 This function queries if a statement may need (parts of) the
80 memory designated by the reference tree argument.
82 There are variants of these functions that only handle the call
83 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
84 Note that these do not disambiguate against a possible call lhs.
86 bool refs_may_alias_p (tree, tree)
88 This function tries to disambiguate two reference trees.
90 bool ptr_deref_may_alias_global_p (tree)
92 This function queries if dereferencing a pointer variable may
95 More low-level disambiguators are available and documented in
96 this file. Low-level disambiguators dealing with points-to
97 information are in tree-ssa-structalias.c. */
100 /* Query statistics for the different low-level disambiguators.
101 A high-level query may trigger multiple of them. */
104 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
105 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
106 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
107 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
108 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
109 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
113 dump_alias_stats (FILE *s)
115 fprintf (s, "\nAlias oracle query stats:\n");
116 fprintf (s, " refs_may_alias_p: "
117 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
118 HOST_WIDE_INT_PRINT_DEC" queries\n",
119 alias_stats.refs_may_alias_p_no_alias,
120 alias_stats.refs_may_alias_p_no_alias
121 + alias_stats.refs_may_alias_p_may_alias);
122 fprintf (s, " ref_maybe_used_by_call_p: "
123 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
124 HOST_WIDE_INT_PRINT_DEC" queries\n",
125 alias_stats.ref_maybe_used_by_call_p_no_alias,
126 alias_stats.refs_may_alias_p_no_alias
127 + alias_stats.ref_maybe_used_by_call_p_may_alias);
128 fprintf (s, " call_may_clobber_ref_p: "
129 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
130 HOST_WIDE_INT_PRINT_DEC" queries\n",
131 alias_stats.call_may_clobber_ref_p_no_alias,
132 alias_stats.call_may_clobber_ref_p_no_alias
133 + alias_stats.call_may_clobber_ref_p_may_alias);
137 /* Return true, if dereferencing PTR may alias with a global variable. */
140 ptr_deref_may_alias_global_p (tree ptr)
142 struct ptr_info_def *pi;
144 /* If we end up with a pointer constant here that may point
146 if (TREE_CODE (ptr) != SSA_NAME)
149 pi = SSA_NAME_PTR_INFO (ptr);
151 /* If we do not have points-to information for this variable,
156 /* ??? This does not use TBAA to prune globals ptr may not access. */
157 return pt_solution_includes_global (&pi->pt);
160 /* Return true if dereferencing PTR may alias DECL.
161 The caller is responsible for applying TBAA to see if PTR
162 may access DECL at all. */
165 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
167 struct ptr_info_def *pi;
169 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
170 || TREE_CODE (ptr) == ADDR_EXPR
171 || TREE_CODE (ptr) == INTEGER_CST)
172 && (TREE_CODE (decl) == VAR_DECL
173 || TREE_CODE (decl) == PARM_DECL
174 || TREE_CODE (decl) == RESULT_DECL));
176 /* Non-aliased variables can not be pointed to. */
177 if (!may_be_aliased (decl))
180 /* ADDR_EXPR pointers either just offset another pointer or directly
181 specify the pointed-to set. */
182 if (TREE_CODE (ptr) == ADDR_EXPR)
184 tree base = get_base_address (TREE_OPERAND (ptr, 0));
186 && (INDIRECT_REF_P (base)
187 || TREE_CODE (base) == MEM_REF))
188 ptr = TREE_OPERAND (base, 0);
193 && CONSTANT_CLASS_P (base))
199 /* We can end up with dereferencing constant pointers.
200 Just bail out in this case. */
201 if (TREE_CODE (ptr) == INTEGER_CST)
204 /* If we do not have useful points-to information for this pointer
205 we cannot disambiguate anything else. */
206 pi = SSA_NAME_PTR_INFO (ptr);
210 /* If the decl can be used as a restrict tag and we have a restrict
211 pointer and that pointers points-to set doesn't contain this decl
212 then they can't alias. */
213 if (DECL_RESTRICTED_P (decl)
214 && TYPE_RESTRICT (TREE_TYPE (ptr))
215 && pi->pt.vars_contains_restrict)
216 return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
218 return pt_solution_includes (&pi->pt, decl);
221 /* Return true if dereferenced PTR1 and PTR2 may alias.
222 The caller is responsible for applying TBAA to see if accesses
223 through PTR1 and PTR2 may conflict at all. */
226 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
228 struct ptr_info_def *pi1, *pi2;
230 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
231 || TREE_CODE (ptr1) == ADDR_EXPR
232 || TREE_CODE (ptr1) == INTEGER_CST)
233 && (TREE_CODE (ptr2) == SSA_NAME
234 || TREE_CODE (ptr2) == ADDR_EXPR
235 || TREE_CODE (ptr2) == INTEGER_CST));
237 /* ADDR_EXPR pointers either just offset another pointer or directly
238 specify the pointed-to set. */
239 if (TREE_CODE (ptr1) == ADDR_EXPR)
241 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
243 && (INDIRECT_REF_P (base)
244 || TREE_CODE (base) == MEM_REF))
245 ptr1 = TREE_OPERAND (base, 0);
248 return ptr_deref_may_alias_decl_p (ptr2, base);
252 if (TREE_CODE (ptr2) == ADDR_EXPR)
254 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
256 && (INDIRECT_REF_P (base)
257 || TREE_CODE (base) == MEM_REF))
258 ptr2 = TREE_OPERAND (base, 0);
261 return ptr_deref_may_alias_decl_p (ptr1, base);
266 /* We can end up with dereferencing constant pointers.
267 Just bail out in this case. */
268 if (TREE_CODE (ptr1) == INTEGER_CST
269 || TREE_CODE (ptr2) == INTEGER_CST)
272 /* We may end up with two empty points-to solutions for two same pointers.
273 In this case we still want to say both pointers alias, so shortcut
278 /* If we do not have useful points-to information for either pointer
279 we cannot disambiguate anything else. */
280 pi1 = SSA_NAME_PTR_INFO (ptr1);
281 pi2 = SSA_NAME_PTR_INFO (ptr2);
285 /* If both pointers are restrict-qualified try to disambiguate
286 with restrict information. */
287 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
288 && TYPE_RESTRICT (TREE_TYPE (ptr2))
289 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
292 /* ??? This does not use TBAA to prune decls from the intersection
293 that not both pointers may access. */
294 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
297 /* Return true if dereferencing PTR may alias *REF.
298 The caller is responsible for applying TBAA to see if PTR
299 may access *REF at all. */
302 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
304 tree base = ao_ref_base (ref);
306 if (INDIRECT_REF_P (base)
307 || TREE_CODE (base) == MEM_REF)
308 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
309 else if (SSA_VAR_P (base))
310 return ptr_deref_may_alias_decl_p (ptr, base);
316 /* Dump alias information on FILE. */
319 dump_alias_info (FILE *file)
323 = lang_hooks.decl_printable_name (current_function_decl, 2);
324 referenced_var_iterator rvi;
327 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
329 fprintf (file, "Aliased symbols\n\n");
331 FOR_EACH_REFERENCED_VAR (var, rvi)
333 if (may_be_aliased (var))
334 dump_variable (file, var);
337 fprintf (file, "\nCall clobber information\n");
339 fprintf (file, "\nESCAPED");
340 dump_points_to_solution (file, &cfun->gimple_df->escaped);
342 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
344 for (i = 1; i < num_ssa_names; i++)
346 tree ptr = ssa_name (i);
347 struct ptr_info_def *pi;
350 || SSA_NAME_IN_FREE_LIST (ptr))
353 pi = SSA_NAME_PTR_INFO (ptr);
355 dump_points_to_info_for (file, ptr);
358 fprintf (file, "\n");
362 /* Dump alias information on stderr. */
365 debug_alias_info (void)
367 dump_alias_info (stderr);
371 /* Dump the points-to set *PT into FILE. */
374 dump_points_to_solution (FILE *file, struct pt_solution *pt)
377 fprintf (file, ", points-to anything");
380 fprintf (file, ", points-to non-local");
383 fprintf (file, ", points-to escaped");
386 fprintf (file, ", points-to unit escaped");
389 fprintf (file, ", points-to NULL");
393 fprintf (file, ", points-to vars: ");
394 dump_decl_set (file, pt->vars);
395 if (pt->vars_contains_global)
396 fprintf (file, " (includes global vars)");
397 if (pt->vars_contains_restrict)
398 fprintf (file, " (includes restrict tags)");
402 /* Dump points-to information for SSA_NAME PTR into FILE. */
405 dump_points_to_info_for (FILE *file, tree ptr)
407 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
409 print_generic_expr (file, ptr, dump_flags);
412 dump_points_to_solution (file, &pi->pt);
414 fprintf (file, ", points-to anything");
416 fprintf (file, "\n");
420 /* Dump points-to information for VAR into stderr. */
423 debug_points_to_info_for (tree var)
425 dump_points_to_info_for (stderr, var);
429 /* Initializes the alias-oracle reference representation *R from REF. */
432 ao_ref_init (ao_ref *r, tree ref)
439 r->ref_alias_set = -1;
440 r->base_alias_set = -1;
443 /* Returns the base object of the memory reference *REF. */
446 ao_ref_base (ao_ref *ref)
450 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
455 /* Returns the base object alias set of the memory reference *REF. */
457 static alias_set_type
458 ao_ref_base_alias_set (ao_ref *ref)
461 if (ref->base_alias_set != -1)
462 return ref->base_alias_set;
466 while (handled_component_p (base_ref))
467 base_ref = TREE_OPERAND (base_ref, 0);
468 ref->base_alias_set = get_alias_set (base_ref);
469 return ref->base_alias_set;
472 /* Returns the reference alias set of the memory reference *REF. */
475 ao_ref_alias_set (ao_ref *ref)
477 if (ref->ref_alias_set != -1)
478 return ref->ref_alias_set;
479 ref->ref_alias_set = get_alias_set (ref->ref);
480 return ref->ref_alias_set;
483 /* Init an alias-oracle reference representation from a gimple pointer
484 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
485 size is assumed to be unknown. The access is assumed to be only
486 to or after of the pointer target, not before it. */
489 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
491 HOST_WIDE_INT t1, t2;
492 ref->ref = NULL_TREE;
493 if (TREE_CODE (ptr) == ADDR_EXPR)
494 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
495 &ref->offset, &t1, &t2);
498 ref->base = build2 (MEM_REF, char_type_node,
499 ptr, null_pointer_node);
503 && host_integerp (size, 0)
504 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
505 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
507 ref->max_size = ref->size = -1;
508 ref->ref_alias_set = 0;
509 ref->base_alias_set = 0;
512 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
513 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
517 same_type_for_tbaa (tree type1, tree type2)
519 type1 = TYPE_MAIN_VARIANT (type1);
520 type2 = TYPE_MAIN_VARIANT (type2);
522 /* If we would have to do structural comparison bail out. */
523 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
524 || TYPE_STRUCTURAL_EQUALITY_P (type2))
527 /* Compare the canonical types. */
528 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
531 /* ??? Array types are not properly unified in all cases as we have
532 spurious changes in the index types for example. Removing this
533 causes all sorts of problems with the Fortran frontend. */
534 if (TREE_CODE (type1) == ARRAY_TYPE
535 && TREE_CODE (type2) == ARRAY_TYPE)
538 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
539 object of one of its constrained subtypes, e.g. when a function with an
540 unconstrained parameter passed by reference is called on an object and
541 inlined. But, even in the case of a fixed size, type and subtypes are
542 not equivalent enough as to share the same TYPE_CANONICAL, since this
543 would mean that conversions between them are useless, whereas they are
544 not (e.g. type and subtypes can have different modes). So, in the end,
545 they are only guaranteed to have the same alias set. */
546 if (get_alias_set (type1) == get_alias_set (type2))
549 /* The types are known to be not equal. */
553 /* Determine if the two component references REF1 and REF2 which are
554 based on access types TYPE1 and TYPE2 and of which at least one is based
555 on an indirect reference may alias. REF2 is the only one that can
556 be a decl in which case REF2_IS_DECL is true.
557 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
558 are the respective alias sets. */
561 aliasing_component_refs_p (tree ref1, tree type1,
562 alias_set_type ref1_alias_set,
563 alias_set_type base1_alias_set,
564 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
565 tree ref2, tree type2,
566 alias_set_type ref2_alias_set,
567 alias_set_type base2_alias_set,
568 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
571 /* If one reference is a component references through pointers try to find a
572 common base and apply offset based disambiguation. This handles
574 struct A { int i; int j; } *q;
575 struct B { struct A a; int k; } *p;
576 disambiguating q->i and p->a.j. */
580 /* Now search for the type1 in the access path of ref2. This
581 would be a common base for doing offset based disambiguation on. */
583 while (handled_component_p (*refp)
584 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
585 refp = &TREE_OPERAND (*refp, 0);
586 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
587 /* If we couldn't compare types we have to bail out. */
590 else if (same_p == 1)
592 HOST_WIDE_INT offadj, sztmp, msztmp;
593 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
595 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
597 /* If we didn't find a common base, try the other way around. */
599 while (handled_component_p (*refp)
600 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
601 refp = &TREE_OPERAND (*refp, 0);
602 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
603 /* If we couldn't compare types we have to bail out. */
606 else if (same_p == 1)
608 HOST_WIDE_INT offadj, sztmp, msztmp;
609 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
611 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
614 /* If we have two type access paths B1.path1 and B2.path2 they may
615 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
616 But we can still have a path that goes B1.path1...B2.path2 with
617 a part that we do not see. So we can only disambiguate now
618 if there is no B2 in the tail of path1 and no B1 on the
620 if (base1_alias_set == ref2_alias_set
621 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
623 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
625 return (base2_alias_set == ref1_alias_set
626 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
630 /* Return true if two memory references based on the variables BASE1
631 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
632 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
635 decl_refs_may_alias_p (tree base1,
636 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
638 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
640 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
642 /* If both references are based on different variables, they cannot alias. */
646 /* If both references are based on the same variable, they cannot alias if
647 the accesses do not overlap. */
648 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
651 /* Return true if an indirect reference based on *PTR1 constrained
652 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
653 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
654 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
655 in which case they are computed on-demand. REF1 and REF2
656 if non-NULL are the complete memory reference trees. */
659 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
660 HOST_WIDE_INT offset1,
661 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
662 alias_set_type ref1_alias_set,
663 alias_set_type base1_alias_set,
664 tree ref2 ATTRIBUTE_UNUSED, tree base2,
665 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
666 alias_set_type ref2_alias_set,
667 alias_set_type base2_alias_set, bool tbaa_p)
671 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
673 if (TREE_CODE (base1) == TARGET_MEM_REF)
675 if (!TMR_BASE (base1)
676 || !POINTER_TYPE_P (TMR_BASE (base1)))
678 ptr1 = TMR_BASE (base1);
681 ptr1 = TREE_OPERAND (base1, 0);
683 /* The offset embedded in MEM_REFs can be negative. Bias them
684 so that the resulting offset adjustment is positive. */
685 if (TREE_CODE (base1) == MEM_REF
686 || TREE_CODE (base1) == TARGET_MEM_REF)
688 double_int moff = mem_ref_offset (base1);
689 moff = double_int_lshift (moff,
691 ? 3 : exact_log2 (BITS_PER_UNIT),
692 HOST_BITS_PER_DOUBLE_INT, true);
693 if (double_int_negative_p (moff))
694 offset2p += double_int_neg (moff).low;
696 offset1p += moff.low;
699 /* If only one reference is based on a variable, they cannot alias if
700 the pointer access is beyond the extent of the variable access.
701 (the pointer base cannot validly point to an offset less than zero
703 They also cannot alias if the pointer may not point to the decl. */
704 if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
705 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
707 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
710 /* Disambiguations that rely on strict aliasing rules follow. */
711 if (!flag_strict_aliasing || !tbaa_p)
714 if (TREE_CODE (base1) == MEM_REF)
715 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
716 else if (TREE_CODE (base1) == TARGET_MEM_REF)
717 ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
719 ptrtype1 = TREE_TYPE (ptr1);
721 /* If the alias set for a pointer access is zero all bets are off. */
722 if (base1_alias_set == -1)
723 base1_alias_set = get_deref_alias_set (ptrtype1);
724 if (base1_alias_set == 0)
726 if (base2_alias_set == -1)
727 base2_alias_set = get_alias_set (base2);
729 /* If both references are through the same type, they do not alias
730 if the accesses do not overlap. This does extra disambiguation
731 for mixed/pointer accesses but requires strict aliasing.
732 For MEM_REFs we require that the component-ref offset we computed
733 is relative to the start of the type which we ensure by
734 comparing rvalue and access type and disregarding the constant
736 if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
737 && (TREE_CODE (base1) != MEM_REF
738 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
739 && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
740 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
742 /* When we are trying to disambiguate an access with a pointer dereference
743 as base versus one with a decl as base we can use both the size
744 of the decl and its dynamic type for extra disambiguation.
745 ??? We do not know anything about the dynamic type of the decl
746 other than that its alias-set contains base2_alias_set as a subset
747 which does not help us here. */
748 /* As we know nothing useful about the dynamic type of the decl just
749 use the usual conflict check rather than a subset test.
750 ??? We could introduce -fvery-strict-aliasing when the language
751 does not allow decls to have a dynamic type that differs from their
752 static type. Then we can check
753 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
754 if (base1_alias_set != base2_alias_set
755 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
757 /* If the size of the access relevant for TBAA through the pointer
758 is bigger than the size of the decl we can't possibly access the
759 decl via that pointer. */
760 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
761 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
762 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
763 /* ??? This in turn may run afoul when a decl of type T which is
764 a member of union type U is accessed through a pointer to
765 type U and sizeof T is smaller than sizeof U. */
766 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
767 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
768 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
771 /* Do access-path based disambiguation. */
773 && handled_component_p (ref1)
774 && handled_component_p (ref2)
775 && TREE_CODE (base1) != TARGET_MEM_REF
776 && (TREE_CODE (base1) != MEM_REF
777 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
778 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
779 ref1_alias_set, base1_alias_set,
781 ref2, TREE_TYPE (base2),
782 ref2_alias_set, base2_alias_set,
783 offset2, max_size2, true);
788 /* Return true if two indirect references based on *PTR1
789 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
790 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
791 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
792 in which case they are computed on-demand. REF1 and REF2
793 if non-NULL are the complete memory reference trees. */
796 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
797 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
798 alias_set_type ref1_alias_set,
799 alias_set_type base1_alias_set,
800 tree ref2 ATTRIBUTE_UNUSED, tree base2,
801 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
802 alias_set_type ref2_alias_set,
803 alias_set_type base2_alias_set, bool tbaa_p)
807 tree ptrtype1, ptrtype2;
809 if (TREE_CODE (base1) == TARGET_MEM_REF)
811 if (!TMR_BASE (base1)
812 || !POINTER_TYPE_P (TMR_BASE (base1)))
814 ptr1 = TMR_BASE (base1);
817 ptr1 = TREE_OPERAND (base1, 0);
819 if (TREE_CODE (base2) == TARGET_MEM_REF)
821 if (!TMR_BASE (base2)
822 || !POINTER_TYPE_P (TMR_BASE (base2)))
824 ptr2 = TMR_BASE (base2);
827 ptr2 = TREE_OPERAND (base2, 0);
829 /* If both bases are based on pointers they cannot alias if they may not
830 point to the same memory object or if they point to the same object
831 and the accesses do not overlap. */
832 if ((!cfun || gimple_in_ssa_p (cfun))
833 && operand_equal_p (ptr1, ptr2, 0)
834 && (((TREE_CODE (base1) != TARGET_MEM_REF
835 || !TMR_INDEX (base1))
836 && (TREE_CODE (base2) != TARGET_MEM_REF
837 || !TMR_INDEX (base2)))
838 || (TREE_CODE (base1) == TARGET_MEM_REF
839 && TREE_CODE (base2) == TARGET_MEM_REF
840 && (TMR_STEP (base1) == TMR_STEP (base2)
841 || (TMR_STEP (base1) && TMR_STEP (base2)
842 && operand_equal_p (TMR_STEP (base1),
843 TMR_STEP (base2), 0)))
844 && operand_equal_p (TMR_INDEX (base1), TMR_INDEX (base2), 0))))
846 /* The offset embedded in MEM_REFs can be negative. Bias them
847 so that the resulting offset adjustment is positive. */
848 if (TREE_CODE (base1) == MEM_REF
849 || TREE_CODE (base1) == TARGET_MEM_REF)
851 double_int moff = mem_ref_offset (base1);
852 moff = double_int_lshift (moff,
854 ? 3 : exact_log2 (BITS_PER_UNIT),
855 HOST_BITS_PER_DOUBLE_INT, true);
856 if (double_int_negative_p (moff))
857 offset2 += double_int_neg (moff).low;
861 if (TREE_CODE (base2) == MEM_REF
862 || TREE_CODE (base2) == TARGET_MEM_REF)
864 double_int moff = mem_ref_offset (base2);
865 moff = double_int_lshift (moff,
867 ? 3 : exact_log2 (BITS_PER_UNIT),
868 HOST_BITS_PER_DOUBLE_INT, true);
869 if (double_int_negative_p (moff))
870 offset1 += double_int_neg (moff).low;
874 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
876 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
879 /* Disambiguations that rely on strict aliasing rules follow. */
880 if (!flag_strict_aliasing || !tbaa_p)
883 if (TREE_CODE (base1) == MEM_REF)
884 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
885 else if (TREE_CODE (base1) == TARGET_MEM_REF)
886 ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
888 ptrtype1 = TREE_TYPE (ptr1);
889 if (TREE_CODE (base2) == MEM_REF)
890 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
891 else if (TREE_CODE (base2) == TARGET_MEM_REF)
892 ptrtype2 = TREE_TYPE (TMR_OFFSET (base2));
894 ptrtype2 = TREE_TYPE (ptr2);
896 /* If the alias set for a pointer access is zero all bets are off. */
897 if (base1_alias_set == -1)
898 base1_alias_set = get_deref_alias_set (ptrtype1);
899 if (base1_alias_set == 0)
901 if (base2_alias_set == -1)
902 base2_alias_set = get_deref_alias_set (ptrtype2);
903 if (base2_alias_set == 0)
906 /* If both references are through the same type, they do not alias
907 if the accesses do not overlap. This does extra disambiguation
908 for mixed/pointer accesses but requires strict aliasing. */
909 if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
910 && (TREE_CODE (base2) != TARGET_MEM_REF || !TMR_INDEX (base2))
911 && (TREE_CODE (base1) != MEM_REF
912 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
913 && (TREE_CODE (base2) != MEM_REF
914 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
915 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
916 TREE_TYPE (ptrtype2)) == 1)
917 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
919 /* Do type-based disambiguation. */
920 if (base1_alias_set != base2_alias_set
921 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
924 /* Do access-path based disambiguation. */
926 && handled_component_p (ref1)
927 && handled_component_p (ref2)
928 && TREE_CODE (base1) != TARGET_MEM_REF
929 && TREE_CODE (base2) != TARGET_MEM_REF
930 && (TREE_CODE (base1) != MEM_REF
931 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
932 && (TREE_CODE (base2) != MEM_REF
933 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
934 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
935 ref1_alias_set, base1_alias_set,
937 ref2, TREE_TYPE (ptrtype2),
938 ref2_alias_set, base2_alias_set,
939 offset2, max_size2, false);
944 /* Return true, if the two memory references REF1 and REF2 may alias. */
947 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
950 HOST_WIDE_INT offset1 = 0, offset2 = 0;
951 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
952 bool var1_p, var2_p, ind1_p, ind2_p;
954 gcc_checking_assert ((!ref1->ref
955 || TREE_CODE (ref1->ref) == SSA_NAME
956 || DECL_P (ref1->ref)
957 || handled_component_p (ref1->ref)
958 || INDIRECT_REF_P (ref1->ref)
959 || TREE_CODE (ref1->ref) == MEM_REF
960 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
962 || TREE_CODE (ref2->ref) == SSA_NAME
963 || DECL_P (ref2->ref)
964 || handled_component_p (ref2->ref)
965 || INDIRECT_REF_P (ref2->ref)
966 || TREE_CODE (ref2->ref) == MEM_REF
967 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
969 /* Decompose the references into their base objects and the access. */
970 base1 = ao_ref_base (ref1);
971 offset1 = ref1->offset;
972 max_size1 = ref1->max_size;
973 base2 = ao_ref_base (ref2);
974 offset2 = ref2->offset;
975 max_size2 = ref2->max_size;
977 /* We can end up with registers or constants as bases for example from
978 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
979 which is seen as a struct copy. */
980 if (TREE_CODE (base1) == SSA_NAME
981 || TREE_CODE (base2) == SSA_NAME
982 || TREE_CODE (base1) == CONST_DECL
983 || TREE_CODE (base2) == CONST_DECL
984 || is_gimple_min_invariant (base1)
985 || is_gimple_min_invariant (base2))
988 /* We can end up refering to code via function and label decls.
989 As we likely do not properly track code aliases conservatively
991 if (TREE_CODE (base1) == FUNCTION_DECL
992 || TREE_CODE (base2) == FUNCTION_DECL
993 || TREE_CODE (base1) == LABEL_DECL
994 || TREE_CODE (base2) == LABEL_DECL)
997 /* Defer to simple offset based disambiguation if we have
998 references based on two decls. Do this before defering to
999 TBAA to handle must-alias cases in conformance with the
1000 GCC extension of allowing type-punning through unions. */
1001 var1_p = SSA_VAR_P (base1);
1002 var2_p = SSA_VAR_P (base2);
1003 if (var1_p && var2_p)
1004 return decl_refs_may_alias_p (base1, offset1, max_size1,
1005 base2, offset2, max_size2);
1007 ind1_p = (INDIRECT_REF_P (base1)
1008 || (TREE_CODE (base1) == MEM_REF)
1009 || (TREE_CODE (base1) == TARGET_MEM_REF));
1010 ind2_p = (INDIRECT_REF_P (base2)
1011 || (TREE_CODE (base2) == MEM_REF)
1012 || (TREE_CODE (base2) == TARGET_MEM_REF));
1014 /* Canonicalize the pointer-vs-decl case. */
1015 if (ind1_p && var2_p)
1020 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1021 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1022 tmp2 = base1; base1 = base2; base2 = tmp2;
1023 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1030 /* First defer to TBAA if possible. */
1032 && flag_strict_aliasing
1033 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1034 ao_ref_alias_set (ref2)))
1037 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1038 if (var1_p && ind2_p)
1039 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1041 ao_ref_alias_set (ref2), -1,
1044 ao_ref_alias_set (ref1),
1045 ao_ref_base_alias_set (ref1),
1047 else if (ind1_p && ind2_p)
1048 return indirect_refs_may_alias_p (ref1->ref, base1,
1050 ao_ref_alias_set (ref1), -1,
1053 ao_ref_alias_set (ref2), -1,
1060 refs_may_alias_p (tree ref1, tree ref2)
1064 ao_ref_init (&r1, ref1);
1065 ao_ref_init (&r2, ref2);
1066 res = refs_may_alias_p_1 (&r1, &r2, true);
1068 ++alias_stats.refs_may_alias_p_may_alias;
1070 ++alias_stats.refs_may_alias_p_no_alias;
1074 /* Returns true if there is a anti-dependence for the STORE that
1075 executes after the LOAD. */
1078 refs_anti_dependent_p (tree load, tree store)
1081 ao_ref_init (&r1, load);
1082 ao_ref_init (&r2, store);
1083 return refs_may_alias_p_1 (&r1, &r2, false);
1086 /* Returns true if there is a output dependence for the stores
1087 STORE1 and STORE2. */
1090 refs_output_dependent_p (tree store1, tree store2)
1093 ao_ref_init (&r1, store1);
1094 ao_ref_init (&r2, store2);
1095 return refs_may_alias_p_1 (&r1, &r2, false);
1098 /* If the call CALL may use the memory reference REF return true,
1099 otherwise return false. */
1102 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1106 int flags = gimple_call_flags (call);
1108 /* Const functions without a static chain do not implicitly use memory. */
1109 if (!gimple_call_chain (call)
1110 && (flags & (ECF_CONST|ECF_NOVOPS)))
1113 base = ao_ref_base (ref);
1117 /* If the reference is based on a decl that is not aliased the call
1118 cannot possibly use it. */
1120 && !may_be_aliased (base)
1121 /* But local statics can be used through recursion. */
1122 && !is_global_var (base))
1125 callee = gimple_call_fndecl (call);
1127 /* Handle those builtin functions explicitly that do not act as
1128 escape points. See tree-ssa-structalias.c:find_func_aliases
1129 for the list of builtins we might need to handle here. */
1130 if (callee != NULL_TREE
1131 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1132 switch (DECL_FUNCTION_CODE (callee))
1134 /* All the following functions clobber memory pointed to by
1135 their first argument. */
1136 case BUILT_IN_STRCPY:
1137 case BUILT_IN_STRNCPY:
1138 case BUILT_IN_MEMCPY:
1139 case BUILT_IN_MEMMOVE:
1140 case BUILT_IN_MEMPCPY:
1141 case BUILT_IN_STPCPY:
1142 case BUILT_IN_STPNCPY:
1143 case BUILT_IN_STRCAT:
1144 case BUILT_IN_STRNCAT:
1147 tree size = NULL_TREE;
1148 if (gimple_call_num_args (call) == 3)
1149 size = gimple_call_arg (call, 2);
1150 ao_ref_init_from_ptr_and_size (&dref,
1151 gimple_call_arg (call, 1),
1153 return refs_may_alias_p_1 (&dref, ref, false);
1155 case BUILT_IN_BCOPY:
1158 tree size = gimple_call_arg (call, 2);
1159 ao_ref_init_from_ptr_and_size (&dref,
1160 gimple_call_arg (call, 0),
1162 return refs_may_alias_p_1 (&dref, ref, false);
1164 /* The following builtins do not read from memory. */
1166 case BUILT_IN_MALLOC:
1167 case BUILT_IN_CALLOC:
1168 case BUILT_IN_MEMSET:
1169 case BUILT_IN_FREXP:
1170 case BUILT_IN_FREXPF:
1171 case BUILT_IN_FREXPL:
1172 case BUILT_IN_GAMMA_R:
1173 case BUILT_IN_GAMMAF_R:
1174 case BUILT_IN_GAMMAL_R:
1175 case BUILT_IN_LGAMMA_R:
1176 case BUILT_IN_LGAMMAF_R:
1177 case BUILT_IN_LGAMMAL_R:
1179 case BUILT_IN_MODFF:
1180 case BUILT_IN_MODFL:
1181 case BUILT_IN_REMQUO:
1182 case BUILT_IN_REMQUOF:
1183 case BUILT_IN_REMQUOL:
1184 case BUILT_IN_SINCOS:
1185 case BUILT_IN_SINCOSF:
1186 case BUILT_IN_SINCOSL:
1190 /* Fallthru to general call handling. */;
1193 /* Check if base is a global static variable that is not read
1195 if (TREE_CODE (base) == VAR_DECL
1196 && TREE_STATIC (base))
1200 if (callee != NULL_TREE
1202 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1203 && bitmap_bit_p (not_read, DECL_UID (base)))
1207 /* Check if the base variable is call-used. */
1210 if (pt_solution_includes (gimple_call_use_set (call), base))
1213 else if ((INDIRECT_REF_P (base)
1214 || TREE_CODE (base) == MEM_REF)
1215 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1217 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1221 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1227 /* Inspect call arguments for passed-by-value aliases. */
1229 for (i = 0; i < gimple_call_num_args (call); ++i)
1231 tree op = gimple_call_arg (call, i);
1232 int flags = gimple_call_arg_flags (call, i);
1234 if (flags & EAF_UNUSED)
1237 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1238 op = TREE_OPERAND (op, 0);
1240 if (TREE_CODE (op) != SSA_NAME
1241 && !is_gimple_min_invariant (op))
1244 ao_ref_init (&r, op);
1245 if (refs_may_alias_p_1 (&r, ref, true))
1254 ref_maybe_used_by_call_p (gimple call, tree ref)
1258 ao_ref_init (&r, ref);
1259 res = ref_maybe_used_by_call_p_1 (call, &r);
1261 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1263 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1268 /* If the statement STMT may use the memory reference REF return
1269 true, otherwise return false. */
1272 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1274 if (is_gimple_assign (stmt))
1278 /* All memory assign statements are single. */
1279 if (!gimple_assign_single_p (stmt))
1282 rhs = gimple_assign_rhs1 (stmt);
1283 if (is_gimple_reg (rhs)
1284 || is_gimple_min_invariant (rhs)
1285 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1288 return refs_may_alias_p (rhs, ref);
1290 else if (is_gimple_call (stmt))
1291 return ref_maybe_used_by_call_p (stmt, ref);
1296 /* If the call in statement CALL may clobber the memory reference REF
1297 return true, otherwise return false. */
1300 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1305 /* If the call is pure or const it cannot clobber anything. */
1306 if (gimple_call_flags (call)
1307 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1310 base = ao_ref_base (ref);
1314 if (TREE_CODE (base) == SSA_NAME
1315 || CONSTANT_CLASS_P (base))
1318 /* If the reference is based on a decl that is not aliased the call
1319 cannot possibly clobber it. */
1321 && !may_be_aliased (base)
1322 /* But local non-readonly statics can be modified through recursion
1323 or the call may implement a threading barrier which we must
1324 treat as may-def. */
1325 && (TREE_READONLY (base)
1326 || !is_global_var (base)))
1329 callee = gimple_call_fndecl (call);
1331 /* Handle those builtin functions explicitly that do not act as
1332 escape points. See tree-ssa-structalias.c:find_func_aliases
1333 for the list of builtins we might need to handle here. */
1334 if (callee != NULL_TREE
1335 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1336 switch (DECL_FUNCTION_CODE (callee))
1338 /* All the following functions clobber memory pointed to by
1339 their first argument. */
1340 case BUILT_IN_STRCPY:
1341 case BUILT_IN_STRNCPY:
1342 case BUILT_IN_MEMCPY:
1343 case BUILT_IN_MEMMOVE:
1344 case BUILT_IN_MEMPCPY:
1345 case BUILT_IN_STPCPY:
1346 case BUILT_IN_STPNCPY:
1347 case BUILT_IN_STRCAT:
1348 case BUILT_IN_STRNCAT:
1349 case BUILT_IN_MEMSET:
1352 tree size = NULL_TREE;
1353 if (gimple_call_num_args (call) == 3)
1354 size = gimple_call_arg (call, 2);
1355 ao_ref_init_from_ptr_and_size (&dref,
1356 gimple_call_arg (call, 0),
1358 return refs_may_alias_p_1 (&dref, ref, false);
1360 case BUILT_IN_BCOPY:
1363 tree size = gimple_call_arg (call, 2);
1364 ao_ref_init_from_ptr_and_size (&dref,
1365 gimple_call_arg (call, 1),
1367 return refs_may_alias_p_1 (&dref, ref, false);
1369 /* Allocating memory does not have any side-effects apart from
1370 being the definition point for the pointer. */
1371 case BUILT_IN_MALLOC:
1372 case BUILT_IN_CALLOC:
1373 /* Unix98 specifies that errno is set on allocation failure.
1374 Until we properly can track the errno location assume it
1375 is not a local decl but external or anonymous storage in
1376 a different translation unit. Also assume it is of
1377 type int as required by the standard. */
1379 && TREE_TYPE (base) == integer_type_node)
1381 struct ptr_info_def *pi;
1383 && !TREE_STATIC (base))
1385 else if ((INDIRECT_REF_P (base)
1386 || TREE_CODE (base) == MEM_REF)
1387 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1388 && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1389 return pi->pt.anything || pi->pt.nonlocal;
1392 /* Freeing memory kills the pointed-to memory. More importantly
1393 the call has to serve as a barrier for moving loads and stores
1397 tree ptr = gimple_call_arg (call, 0);
1398 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1400 case BUILT_IN_GAMMA_R:
1401 case BUILT_IN_GAMMAF_R:
1402 case BUILT_IN_GAMMAL_R:
1403 case BUILT_IN_LGAMMA_R:
1404 case BUILT_IN_LGAMMAF_R:
1405 case BUILT_IN_LGAMMAL_R:
1407 tree out = gimple_call_arg (call, 1);
1408 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1410 if (flag_errno_math)
1414 case BUILT_IN_FREXP:
1415 case BUILT_IN_FREXPF:
1416 case BUILT_IN_FREXPL:
1418 case BUILT_IN_MODFF:
1419 case BUILT_IN_MODFL:
1421 tree out = gimple_call_arg (call, 1);
1422 return ptr_deref_may_alias_ref_p_1 (out, ref);
1424 case BUILT_IN_REMQUO:
1425 case BUILT_IN_REMQUOF:
1426 case BUILT_IN_REMQUOL:
1428 tree out = gimple_call_arg (call, 2);
1429 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1431 if (flag_errno_math)
1435 case BUILT_IN_SINCOS:
1436 case BUILT_IN_SINCOSF:
1437 case BUILT_IN_SINCOSL:
1439 tree sin = gimple_call_arg (call, 1);
1440 tree cos = gimple_call_arg (call, 2);
1441 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1442 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1445 /* Fallthru to general call handling. */;
1448 /* Check if base is a global static variable that is not written
1450 if (callee != NULL_TREE
1451 && TREE_CODE (base) == VAR_DECL
1452 && TREE_STATIC (base))
1457 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1458 && bitmap_bit_p (not_written, DECL_UID (base)))
1462 /* Check if the base variable is call-clobbered. */
1464 return pt_solution_includes (gimple_call_clobber_set (call), base);
1465 else if ((INDIRECT_REF_P (base)
1466 || TREE_CODE (base) == MEM_REF)
1467 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1469 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1473 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1479 /* If the call in statement CALL may clobber the memory reference REF
1480 return true, otherwise return false. */
1483 call_may_clobber_ref_p (gimple call, tree ref)
1487 ao_ref_init (&r, ref);
1488 res = call_may_clobber_ref_p_1 (call, &r);
1490 ++alias_stats.call_may_clobber_ref_p_may_alias;
1492 ++alias_stats.call_may_clobber_ref_p_no_alias;
1497 /* If the statement STMT may clobber the memory reference REF return true,
1498 otherwise return false. */
1501 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1503 if (is_gimple_call (stmt))
1505 tree lhs = gimple_call_lhs (stmt);
1507 && !is_gimple_reg (lhs))
1510 ao_ref_init (&r, lhs);
1511 if (refs_may_alias_p_1 (ref, &r, true))
1515 return call_may_clobber_ref_p_1 (stmt, ref);
1517 else if (gimple_assign_single_p (stmt))
1519 tree lhs = gimple_assign_lhs (stmt);
1520 if (!is_gimple_reg (lhs))
1523 ao_ref_init (&r, gimple_assign_lhs (stmt));
1524 return refs_may_alias_p_1 (ref, &r, true);
1527 else if (gimple_code (stmt) == GIMPLE_ASM)
1534 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1537 ao_ref_init (&r, ref);
1538 return stmt_may_clobber_ref_p_1 (stmt, &r);
1542 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1543 TARGET or a statement clobbering the memory reference REF in which
1544 case false is returned. The walk starts with VUSE, one argument of PHI. */
1547 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1548 tree vuse, bitmap *visited)
1551 *visited = BITMAP_ALLOC (NULL);
1553 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1555 /* Walk until we hit the target. */
1556 while (vuse != target)
1558 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1559 /* Recurse for PHI nodes. */
1560 if (gimple_code (def_stmt) == GIMPLE_PHI)
1562 /* An already visited PHI node ends the walk successfully. */
1563 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1565 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1570 /* A clobbering statement or the end of the IL ends it failing. */
1571 else if (gimple_nop_p (def_stmt)
1572 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1574 vuse = gimple_vuse (def_stmt);
1579 /* Starting from a PHI node for the virtual operand of the memory reference
1580 REF find a continuation virtual operand that allows to continue walking
1581 statements dominating PHI skipping only statements that cannot possibly
1582 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1586 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1588 unsigned nargs = gimple_phi_num_args (phi);
1590 /* Through a single-argument PHI we can simply look through. */
1592 return PHI_ARG_DEF (phi, 0);
1594 /* For two arguments try to skip non-aliasing code until we hit
1595 the phi argument definition that dominates the other one. */
1598 tree arg0 = PHI_ARG_DEF (phi, 0);
1599 tree arg1 = PHI_ARG_DEF (phi, 1);
1600 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1601 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1606 else if (gimple_nop_p (def0)
1607 || (!gimple_nop_p (def1)
1608 && dominated_by_p (CDI_DOMINATORS,
1609 gimple_bb (def1), gimple_bb (def0))))
1611 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1614 else if (gimple_nop_p (def1)
1615 || dominated_by_p (CDI_DOMINATORS,
1616 gimple_bb (def0), gimple_bb (def1)))
1618 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1621 /* Special case of a diamond:
1623 goto (cond) ? L1 : L2
1624 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1626 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1627 L3: MEM_4 = PHI<MEM_2, MEM_3>
1628 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1629 dominate each other, but still we can easily skip this PHI node
1630 if we recognize that the vuse MEM operand is the same for both,
1631 and that we can skip both statements (they don't clobber us).
1632 This is still linear. Don't use maybe_skip_until, that might
1633 potentially be slow. */
1634 else if ((common_vuse = gimple_vuse (def0))
1635 && common_vuse == gimple_vuse (def1))
1637 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1638 && !stmt_may_clobber_ref_p_1 (def1, ref))
1646 /* Based on the memory reference REF and its virtual use VUSE call
1647 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1648 itself. That is, for each virtual use for which its defining statement
1649 does not clobber REF.
1651 WALKER is called with REF, the current virtual use and DATA. If
1652 WALKER returns non-NULL the walk stops and its result is returned.
1653 At the end of a non-successful walk NULL is returned.
1655 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1656 use which definition is a statement that may clobber REF and DATA.
1657 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1658 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1659 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1660 to adjust REF and *DATA to make that valid.
1662 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1665 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1666 void *(*walker)(ao_ref *, tree, void *),
1667 void *(*translate)(ao_ref *, tree, void *), void *data)
1669 bitmap visited = NULL;
1672 timevar_push (TV_ALIAS_STMT_WALK);
1678 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1679 res = (*walker) (ref, vuse, data);
1683 def_stmt = SSA_NAME_DEF_STMT (vuse);
1684 if (gimple_nop_p (def_stmt))
1686 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1687 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1690 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1694 res = (*translate) (ref, vuse, data);
1695 /* Failed lookup and translation. */
1696 if (res == (void *)-1)
1701 /* Lookup succeeded. */
1702 else if (res != NULL)
1704 /* Translation succeeded, continue walking. */
1706 vuse = gimple_vuse (def_stmt);
1712 BITMAP_FREE (visited);
1714 timevar_pop (TV_ALIAS_STMT_WALK);
1720 /* Based on the memory reference REF call WALKER for each vdef which
1721 defining statement may clobber REF, starting with VDEF. If REF
1722 is NULL_TREE, each defining statement is visited.
1724 WALKER is called with REF, the current vdef and DATA. If WALKER
1725 returns true the walk is stopped, otherwise it continues.
1727 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1728 PHI argument (but only one walk continues on merge points), the
1729 return value is true if any of the walks was successful.
1731 The function returns the number of statements walked. */
1734 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1735 bool (*walker)(ao_ref *, tree, void *), void *data,
1736 bitmap *visited, unsigned int cnt)
1740 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1743 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1746 if (gimple_nop_p (def_stmt))
1748 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1752 *visited = BITMAP_ALLOC (NULL);
1753 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1754 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1755 walker, data, visited, 0);
1759 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1762 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1763 && (*walker) (ref, vdef, data))
1766 vdef = gimple_vuse (def_stmt);
1772 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1773 bool (*walker)(ao_ref *, tree, void *), void *data,
1776 bitmap local_visited = NULL;
1779 timevar_push (TV_ALIAS_STMT_WALK);
1781 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1782 visited ? visited : &local_visited, 0);
1784 BITMAP_FREE (local_visited);
1786 timevar_pop (TV_ALIAS_STMT_WALK);