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"
29 #include "basic-block.h"
32 #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 /* Conversions are irrelevant for points-to information and
170 data-dependence analysis can feed us those. */
173 /* Anything we do not explicilty handle aliases. */
174 if ((TREE_CODE (ptr) != SSA_NAME
175 && TREE_CODE (ptr) != ADDR_EXPR
176 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
177 || !POINTER_TYPE_P (TREE_TYPE (ptr))
178 || (TREE_CODE (decl) != VAR_DECL
179 && TREE_CODE (decl) != PARM_DECL
180 && TREE_CODE (decl) != RESULT_DECL))
183 /* Disregard pointer offsetting. */
184 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
188 ptr = TREE_OPERAND (ptr, 0);
190 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
191 return ptr_deref_may_alias_decl_p (ptr, decl);
194 /* ADDR_EXPR pointers either just offset another pointer or directly
195 specify the pointed-to set. */
196 if (TREE_CODE (ptr) == ADDR_EXPR)
198 tree base = get_base_address (TREE_OPERAND (ptr, 0));
200 && (INDIRECT_REF_P (base)
201 || TREE_CODE (base) == MEM_REF))
202 ptr = TREE_OPERAND (base, 0);
207 && CONSTANT_CLASS_P (base))
213 /* Non-aliased variables can not be pointed to. */
214 if (!may_be_aliased (decl))
217 /* If we do not have useful points-to information for this pointer
218 we cannot disambiguate anything else. */
219 pi = SSA_NAME_PTR_INFO (ptr);
223 /* If the decl can be used as a restrict tag and we have a restrict
224 pointer and that pointers points-to set doesn't contain this decl
225 then they can't alias. */
226 if (DECL_RESTRICTED_P (decl)
227 && TYPE_RESTRICT (TREE_TYPE (ptr))
228 && pi->pt.vars_contains_restrict)
229 return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
231 return pt_solution_includes (&pi->pt, decl);
234 /* Return true if dereferenced PTR1 and PTR2 may alias.
235 The caller is responsible for applying TBAA to see if accesses
236 through PTR1 and PTR2 may conflict at all. */
239 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
241 struct ptr_info_def *pi1, *pi2;
243 /* Conversions are irrelevant for points-to information and
244 data-dependence analysis can feed us those. */
248 /* Anything we do not explicilty handle aliases. */
249 if ((TREE_CODE (ptr1) != SSA_NAME
250 && TREE_CODE (ptr1) != ADDR_EXPR
251 && TREE_CODE (ptr1) != POINTER_PLUS_EXPR)
252 || (TREE_CODE (ptr2) != SSA_NAME
253 && TREE_CODE (ptr2) != ADDR_EXPR
254 && TREE_CODE (ptr2) != POINTER_PLUS_EXPR)
255 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
256 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
259 /* Disregard pointer offsetting. */
260 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
264 ptr1 = TREE_OPERAND (ptr1, 0);
266 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
267 return ptr_derefs_may_alias_p (ptr1, ptr2);
269 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
273 ptr2 = TREE_OPERAND (ptr2, 0);
275 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
276 return ptr_derefs_may_alias_p (ptr1, ptr2);
279 /* ADDR_EXPR pointers either just offset another pointer or directly
280 specify the pointed-to set. */
281 if (TREE_CODE (ptr1) == ADDR_EXPR)
283 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
285 && (INDIRECT_REF_P (base)
286 || TREE_CODE (base) == MEM_REF))
287 ptr1 = TREE_OPERAND (base, 0);
290 return ptr_deref_may_alias_decl_p (ptr2, base);
294 if (TREE_CODE (ptr2) == ADDR_EXPR)
296 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
298 && (INDIRECT_REF_P (base)
299 || TREE_CODE (base) == MEM_REF))
300 ptr2 = TREE_OPERAND (base, 0);
303 return ptr_deref_may_alias_decl_p (ptr1, base);
308 /* We may end up with two empty points-to solutions for two same pointers.
309 In this case we still want to say both pointers alias, so shortcut
314 /* If we do not have useful points-to information for either pointer
315 we cannot disambiguate anything else. */
316 pi1 = SSA_NAME_PTR_INFO (ptr1);
317 pi2 = SSA_NAME_PTR_INFO (ptr2);
321 /* If both pointers are restrict-qualified try to disambiguate
322 with restrict information. */
323 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
324 && TYPE_RESTRICT (TREE_TYPE (ptr2))
325 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
328 /* ??? This does not use TBAA to prune decls from the intersection
329 that not both pointers may access. */
330 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
333 /* Return true if dereferencing PTR may alias *REF.
334 The caller is responsible for applying TBAA to see if PTR
335 may access *REF at all. */
338 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
340 tree base = ao_ref_base (ref);
342 if (INDIRECT_REF_P (base)
343 || TREE_CODE (base) == MEM_REF)
344 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
345 else if (SSA_VAR_P (base))
346 return ptr_deref_may_alias_decl_p (ptr, base);
352 /* Dump alias information on FILE. */
355 dump_alias_info (FILE *file)
359 = lang_hooks.decl_printable_name (current_function_decl, 2);
360 referenced_var_iterator rvi;
363 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
365 fprintf (file, "Aliased symbols\n\n");
367 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
369 if (may_be_aliased (var))
370 dump_variable (file, var);
373 fprintf (file, "\nCall clobber information\n");
375 fprintf (file, "\nESCAPED");
376 dump_points_to_solution (file, &cfun->gimple_df->escaped);
378 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
380 for (i = 1; i < num_ssa_names; i++)
382 tree ptr = ssa_name (i);
383 struct ptr_info_def *pi;
386 || SSA_NAME_IN_FREE_LIST (ptr))
389 pi = SSA_NAME_PTR_INFO (ptr);
391 dump_points_to_info_for (file, ptr);
394 fprintf (file, "\n");
398 /* Dump alias information on stderr. */
401 debug_alias_info (void)
403 dump_alias_info (stderr);
407 /* Dump the points-to set *PT into FILE. */
410 dump_points_to_solution (FILE *file, struct pt_solution *pt)
413 fprintf (file, ", points-to anything");
416 fprintf (file, ", points-to non-local");
419 fprintf (file, ", points-to escaped");
422 fprintf (file, ", points-to unit escaped");
425 fprintf (file, ", points-to NULL");
429 fprintf (file, ", points-to vars: ");
430 dump_decl_set (file, pt->vars);
431 if (pt->vars_contains_global)
432 fprintf (file, " (includes global vars)");
433 if (pt->vars_contains_restrict)
434 fprintf (file, " (includes restrict tags)");
438 /* Dump points-to information for SSA_NAME PTR into FILE. */
441 dump_points_to_info_for (FILE *file, tree ptr)
443 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
445 print_generic_expr (file, ptr, dump_flags);
448 dump_points_to_solution (file, &pi->pt);
450 fprintf (file, ", points-to anything");
452 fprintf (file, "\n");
456 /* Dump points-to information for VAR into stderr. */
459 debug_points_to_info_for (tree var)
461 dump_points_to_info_for (stderr, var);
465 /* Initializes the alias-oracle reference representation *R from REF. */
468 ao_ref_init (ao_ref *r, tree ref)
475 r->ref_alias_set = -1;
476 r->base_alias_set = -1;
479 /* Returns the base object of the memory reference *REF. */
482 ao_ref_base (ao_ref *ref)
486 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
491 /* Returns the base object alias set of the memory reference *REF. */
493 static alias_set_type
494 ao_ref_base_alias_set (ao_ref *ref)
497 if (ref->base_alias_set != -1)
498 return ref->base_alias_set;
502 while (handled_component_p (base_ref))
503 base_ref = TREE_OPERAND (base_ref, 0);
504 ref->base_alias_set = get_alias_set (base_ref);
505 return ref->base_alias_set;
508 /* Returns the reference alias set of the memory reference *REF. */
511 ao_ref_alias_set (ao_ref *ref)
513 if (ref->ref_alias_set != -1)
514 return ref->ref_alias_set;
515 ref->ref_alias_set = get_alias_set (ref->ref);
516 return ref->ref_alias_set;
519 /* Init an alias-oracle reference representation from a gimple pointer
520 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
521 size is assumed to be unknown. The access is assumed to be only
522 to or after of the pointer target, not before it. */
525 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
527 HOST_WIDE_INT t1, t2;
528 ref->ref = NULL_TREE;
529 if (TREE_CODE (ptr) == ADDR_EXPR)
530 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
531 &ref->offset, &t1, &t2);
534 ref->base = build2 (MEM_REF, char_type_node,
535 ptr, null_pointer_node);
539 && host_integerp (size, 0)
540 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
541 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
543 ref->max_size = ref->size = -1;
544 ref->ref_alias_set = 0;
545 ref->base_alias_set = 0;
548 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
549 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
553 same_type_for_tbaa (tree type1, tree type2)
555 type1 = TYPE_MAIN_VARIANT (type1);
556 type2 = TYPE_MAIN_VARIANT (type2);
558 /* If we would have to do structural comparison bail out. */
559 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
560 || TYPE_STRUCTURAL_EQUALITY_P (type2))
563 /* Compare the canonical types. */
564 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
567 /* ??? Array types are not properly unified in all cases as we have
568 spurious changes in the index types for example. Removing this
569 causes all sorts of problems with the Fortran frontend. */
570 if (TREE_CODE (type1) == ARRAY_TYPE
571 && TREE_CODE (type2) == ARRAY_TYPE)
574 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
575 object of one of its constrained subtypes, e.g. when a function with an
576 unconstrained parameter passed by reference is called on an object and
577 inlined. But, even in the case of a fixed size, type and subtypes are
578 not equivalent enough as to share the same TYPE_CANONICAL, since this
579 would mean that conversions between them are useless, whereas they are
580 not (e.g. type and subtypes can have different modes). So, in the end,
581 they are only guaranteed to have the same alias set. */
582 if (get_alias_set (type1) == get_alias_set (type2))
585 /* The types are known to be not equal. */
589 /* Determine if the two component references REF1 and REF2 which are
590 based on access types TYPE1 and TYPE2 and of which at least one is based
591 on an indirect reference may alias. REF2 is the only one that can
592 be a decl in which case REF2_IS_DECL is true.
593 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
594 are the respective alias sets. */
597 aliasing_component_refs_p (tree ref1, tree type1,
598 alias_set_type ref1_alias_set,
599 alias_set_type base1_alias_set,
600 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
601 tree ref2, tree type2,
602 alias_set_type ref2_alias_set,
603 alias_set_type base2_alias_set,
604 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
607 /* If one reference is a component references through pointers try to find a
608 common base and apply offset based disambiguation. This handles
610 struct A { int i; int j; } *q;
611 struct B { struct A a; int k; } *p;
612 disambiguating q->i and p->a.j. */
616 /* Now search for the type1 in the access path of ref2. This
617 would be a common base for doing offset based disambiguation on. */
619 while (handled_component_p (*refp)
620 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
621 refp = &TREE_OPERAND (*refp, 0);
622 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
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);
633 /* If we didn't find a common base, try the other way around. */
635 while (handled_component_p (*refp)
636 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
637 refp = &TREE_OPERAND (*refp, 0);
638 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
639 /* If we couldn't compare types we have to bail out. */
642 else if (same_p == 1)
644 HOST_WIDE_INT offadj, sztmp, msztmp;
645 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
647 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
650 /* If we have two type access paths B1.path1 and B2.path2 they may
651 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
652 But we can still have a path that goes B1.path1...B2.path2 with
653 a part that we do not see. So we can only disambiguate now
654 if there is no B2 in the tail of path1 and no B1 on the
656 if (base1_alias_set == ref2_alias_set
657 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
659 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
661 return (base2_alias_set == ref1_alias_set
662 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
666 /* Return true if two memory references based on the variables BASE1
667 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
668 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
671 decl_refs_may_alias_p (tree base1,
672 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
674 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
676 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
678 /* If both references are based on different variables, they cannot alias. */
682 /* If both references are based on the same variable, they cannot alias if
683 the accesses do not overlap. */
684 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
687 /* Return true if an indirect reference based on *PTR1 constrained
688 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
689 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
690 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
691 in which case they are computed on-demand. REF1 and REF2
692 if non-NULL are the complete memory reference trees. */
695 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
696 HOST_WIDE_INT offset1,
697 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
698 alias_set_type ref1_alias_set,
699 alias_set_type base1_alias_set,
700 tree ref2 ATTRIBUTE_UNUSED, tree base2,
701 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
702 alias_set_type ref2_alias_set,
703 alias_set_type base2_alias_set, bool tbaa_p)
707 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
709 ptr1 = TREE_OPERAND (base1, 0);
711 /* The offset embedded in MEM_REFs can be negative. Bias them
712 so that the resulting offset adjustment is positive. */
713 if (TREE_CODE (base1) == MEM_REF
714 || TREE_CODE (base1) == TARGET_MEM_REF)
716 double_int moff = mem_ref_offset (base1);
717 moff = double_int_lshift (moff,
719 ? 3 : exact_log2 (BITS_PER_UNIT),
720 HOST_BITS_PER_DOUBLE_INT, true);
721 if (double_int_negative_p (moff))
722 offset2p += double_int_neg (moff).low;
724 offset1p += moff.low;
727 /* If only one reference is based on a variable, they cannot alias if
728 the pointer access is beyond the extent of the variable access.
729 (the pointer base cannot validly point to an offset less than zero
731 They also cannot alias if the pointer may not point to the decl. */
732 if ((TREE_CODE (base1) != TARGET_MEM_REF
733 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
734 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
736 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
739 /* Disambiguations that rely on strict aliasing rules follow. */
740 if (!flag_strict_aliasing || !tbaa_p)
743 if (TREE_CODE (base1) == MEM_REF)
744 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
745 else if (TREE_CODE (base1) == TARGET_MEM_REF)
746 ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
748 ptrtype1 = TREE_TYPE (ptr1);
750 /* If the alias set for a pointer access is zero all bets are off. */
751 if (base1_alias_set == -1)
752 base1_alias_set = get_deref_alias_set (ptrtype1);
753 if (base1_alias_set == 0)
755 if (base2_alias_set == -1)
756 base2_alias_set = get_alias_set (base2);
758 /* If both references are through the same type, they do not alias
759 if the accesses do not overlap. This does extra disambiguation
760 for mixed/pointer accesses but requires strict aliasing.
761 For MEM_REFs we require that the component-ref offset we computed
762 is relative to the start of the type which we ensure by
763 comparing rvalue and access type and disregarding the constant
765 if ((TREE_CODE (base1) != TARGET_MEM_REF
766 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
767 && (TREE_CODE (base1) != MEM_REF
768 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
769 && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
770 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
772 /* When we are trying to disambiguate an access with a pointer dereference
773 as base versus one with a decl as base we can use both the size
774 of the decl and its dynamic type for extra disambiguation.
775 ??? We do not know anything about the dynamic type of the decl
776 other than that its alias-set contains base2_alias_set as a subset
777 which does not help us here. */
778 /* As we know nothing useful about the dynamic type of the decl just
779 use the usual conflict check rather than a subset test.
780 ??? We could introduce -fvery-strict-aliasing when the language
781 does not allow decls to have a dynamic type that differs from their
782 static type. Then we can check
783 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
784 if (base1_alias_set != base2_alias_set
785 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
787 /* If the size of the access relevant for TBAA through the pointer
788 is bigger than the size of the decl we can't possibly access the
789 decl via that pointer. */
790 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
791 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
792 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
793 /* ??? This in turn may run afoul when a decl of type T which is
794 a member of union type U is accessed through a pointer to
795 type U and sizeof T is smaller than sizeof U. */
796 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
797 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
798 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
801 /* Do access-path based disambiguation. */
803 && handled_component_p (ref1)
804 && handled_component_p (ref2)
805 && TREE_CODE (base1) != TARGET_MEM_REF
806 && (TREE_CODE (base1) != MEM_REF
807 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
808 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
809 ref1_alias_set, base1_alias_set,
812 (reference_alias_ptr_type (ref2)),
813 ref2_alias_set, base2_alias_set,
814 offset2, max_size2, true);
819 /* Return true if two indirect references based on *PTR1
820 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
821 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
822 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
823 in which case they are computed on-demand. REF1 and REF2
824 if non-NULL are the complete memory reference trees. */
827 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
828 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
829 alias_set_type ref1_alias_set,
830 alias_set_type base1_alias_set,
831 tree ref2 ATTRIBUTE_UNUSED, tree base2,
832 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
833 alias_set_type ref2_alias_set,
834 alias_set_type base2_alias_set, bool tbaa_p)
838 tree ptrtype1, ptrtype2;
840 ptr1 = TREE_OPERAND (base1, 0);
841 ptr2 = TREE_OPERAND (base2, 0);
843 /* If both bases are based on pointers they cannot alias if they may not
844 point to the same memory object or if they point to the same object
845 and the accesses do not overlap. */
846 if ((!cfun || gimple_in_ssa_p (cfun))
847 && operand_equal_p (ptr1, ptr2, 0)
848 && (((TREE_CODE (base1) != TARGET_MEM_REF
849 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
850 && (TREE_CODE (base2) != TARGET_MEM_REF
851 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
852 || (TREE_CODE (base1) == TARGET_MEM_REF
853 && TREE_CODE (base2) == TARGET_MEM_REF
854 && (TMR_STEP (base1) == TMR_STEP (base2)
855 || (TMR_STEP (base1) && TMR_STEP (base2)
856 && operand_equal_p (TMR_STEP (base1),
857 TMR_STEP (base2), 0)))
858 && (TMR_INDEX (base1) == TMR_INDEX (base2)
859 || (TMR_INDEX (base1) && TMR_INDEX (base2)
860 && operand_equal_p (TMR_INDEX (base1),
861 TMR_INDEX (base2), 0)))
862 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
863 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
864 && operand_equal_p (TMR_INDEX2 (base1),
865 TMR_INDEX2 (base2), 0))))))
867 /* The offset embedded in MEM_REFs can be negative. Bias them
868 so that the resulting offset adjustment is positive. */
869 if (TREE_CODE (base1) == MEM_REF
870 || TREE_CODE (base1) == TARGET_MEM_REF)
872 double_int moff = mem_ref_offset (base1);
873 moff = double_int_lshift (moff,
875 ? 3 : exact_log2 (BITS_PER_UNIT),
876 HOST_BITS_PER_DOUBLE_INT, true);
877 if (double_int_negative_p (moff))
878 offset2 += double_int_neg (moff).low;
882 if (TREE_CODE (base2) == MEM_REF
883 || TREE_CODE (base2) == TARGET_MEM_REF)
885 double_int moff = mem_ref_offset (base2);
886 moff = double_int_lshift (moff,
888 ? 3 : exact_log2 (BITS_PER_UNIT),
889 HOST_BITS_PER_DOUBLE_INT, true);
890 if (double_int_negative_p (moff))
891 offset1 += double_int_neg (moff).low;
895 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
897 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
900 /* Disambiguations that rely on strict aliasing rules follow. */
901 if (!flag_strict_aliasing || !tbaa_p)
904 if (TREE_CODE (base1) == MEM_REF)
905 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
906 else if (TREE_CODE (base1) == TARGET_MEM_REF)
907 ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
909 ptrtype1 = TREE_TYPE (ptr1);
910 if (TREE_CODE (base2) == MEM_REF)
911 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
912 else if (TREE_CODE (base2) == TARGET_MEM_REF)
913 ptrtype2 = TREE_TYPE (TMR_OFFSET (base2));
915 ptrtype2 = TREE_TYPE (ptr2);
917 /* If the alias set for a pointer access is zero all bets are off. */
918 if (base1_alias_set == -1)
919 base1_alias_set = get_deref_alias_set (ptrtype1);
920 if (base1_alias_set == 0)
922 if (base2_alias_set == -1)
923 base2_alias_set = get_deref_alias_set (ptrtype2);
924 if (base2_alias_set == 0)
927 /* If both references are through the same type, they do not alias
928 if the accesses do not overlap. This does extra disambiguation
929 for mixed/pointer accesses but requires strict aliasing. */
930 if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
931 && (TREE_CODE (base2) != TARGET_MEM_REF || !TMR_INDEX (base2))
932 && (TREE_CODE (base1) != MEM_REF
933 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
934 && (TREE_CODE (base2) != MEM_REF
935 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
936 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
937 TREE_TYPE (ptrtype2)) == 1)
938 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
940 /* Do type-based disambiguation. */
941 if (base1_alias_set != base2_alias_set
942 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
945 /* Do access-path based disambiguation. */
947 && handled_component_p (ref1)
948 && handled_component_p (ref2)
949 && TREE_CODE (base1) != TARGET_MEM_REF
950 && TREE_CODE (base2) != TARGET_MEM_REF
951 && (TREE_CODE (base1) != MEM_REF
952 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
953 && (TREE_CODE (base2) != MEM_REF
954 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
955 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
956 ref1_alias_set, base1_alias_set,
958 ref2, TREE_TYPE (ptrtype2),
959 ref2_alias_set, base2_alias_set,
960 offset2, max_size2, false);
965 /* Return true, if the two memory references REF1 and REF2 may alias. */
968 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
971 HOST_WIDE_INT offset1 = 0, offset2 = 0;
972 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
973 bool var1_p, var2_p, ind1_p, ind2_p;
975 gcc_checking_assert ((!ref1->ref
976 || TREE_CODE (ref1->ref) == SSA_NAME
977 || DECL_P (ref1->ref)
978 || TREE_CODE (ref1->ref) == STRING_CST
979 || handled_component_p (ref1->ref)
980 || INDIRECT_REF_P (ref1->ref)
981 || TREE_CODE (ref1->ref) == MEM_REF
982 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
984 || TREE_CODE (ref2->ref) == SSA_NAME
985 || DECL_P (ref2->ref)
986 || TREE_CODE (ref2->ref) == STRING_CST
987 || handled_component_p (ref2->ref)
988 || INDIRECT_REF_P (ref2->ref)
989 || TREE_CODE (ref2->ref) == MEM_REF
990 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
992 /* Decompose the references into their base objects and the access. */
993 base1 = ao_ref_base (ref1);
994 offset1 = ref1->offset;
995 max_size1 = ref1->max_size;
996 base2 = ao_ref_base (ref2);
997 offset2 = ref2->offset;
998 max_size2 = ref2->max_size;
1000 /* We can end up with registers or constants as bases for example from
1001 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1002 which is seen as a struct copy. */
1003 if (TREE_CODE (base1) == SSA_NAME
1004 || TREE_CODE (base1) == CONST_DECL
1005 || TREE_CODE (base1) == CONSTRUCTOR
1006 || TREE_CODE (base1) == ADDR_EXPR
1007 || CONSTANT_CLASS_P (base1)
1008 || TREE_CODE (base2) == SSA_NAME
1009 || TREE_CODE (base2) == CONST_DECL
1010 || TREE_CODE (base2) == CONSTRUCTOR
1011 || TREE_CODE (base2) == ADDR_EXPR
1012 || CONSTANT_CLASS_P (base2))
1015 /* We can end up refering to code via function and label decls.
1016 As we likely do not properly track code aliases conservatively
1018 if (TREE_CODE (base1) == FUNCTION_DECL
1019 || TREE_CODE (base1) == LABEL_DECL
1020 || TREE_CODE (base2) == FUNCTION_DECL
1021 || TREE_CODE (base2) == LABEL_DECL)
1024 /* Defer to simple offset based disambiguation if we have
1025 references based on two decls. Do this before defering to
1026 TBAA to handle must-alias cases in conformance with the
1027 GCC extension of allowing type-punning through unions. */
1028 var1_p = SSA_VAR_P (base1);
1029 var2_p = SSA_VAR_P (base2);
1030 if (var1_p && var2_p)
1031 return decl_refs_may_alias_p (base1, offset1, max_size1,
1032 base2, offset2, max_size2);
1034 ind1_p = (INDIRECT_REF_P (base1)
1035 || (TREE_CODE (base1) == MEM_REF)
1036 || (TREE_CODE (base1) == TARGET_MEM_REF));
1037 ind2_p = (INDIRECT_REF_P (base2)
1038 || (TREE_CODE (base2) == MEM_REF)
1039 || (TREE_CODE (base2) == TARGET_MEM_REF));
1041 /* Canonicalize the pointer-vs-decl case. */
1042 if (ind1_p && var2_p)
1047 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1048 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1049 tmp2 = base1; base1 = base2; base2 = tmp2;
1050 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1057 /* First defer to TBAA if possible. */
1059 && flag_strict_aliasing
1060 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1061 ao_ref_alias_set (ref2)))
1064 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1065 if (var1_p && ind2_p)
1066 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1068 ao_ref_alias_set (ref2), -1,
1071 ao_ref_alias_set (ref1),
1072 ao_ref_base_alias_set (ref1),
1074 else if (ind1_p && ind2_p)
1075 return indirect_refs_may_alias_p (ref1->ref, base1,
1077 ao_ref_alias_set (ref1), -1,
1080 ao_ref_alias_set (ref2), -1,
1083 /* We really do not want to end up here, but returning true is safe. */
1084 #ifdef ENABLE_CHECKING
1092 refs_may_alias_p (tree ref1, tree ref2)
1096 ao_ref_init (&r1, ref1);
1097 ao_ref_init (&r2, ref2);
1098 res = refs_may_alias_p_1 (&r1, &r2, true);
1100 ++alias_stats.refs_may_alias_p_may_alias;
1102 ++alias_stats.refs_may_alias_p_no_alias;
1106 /* Returns true if there is a anti-dependence for the STORE that
1107 executes after the LOAD. */
1110 refs_anti_dependent_p (tree load, tree store)
1113 ao_ref_init (&r1, load);
1114 ao_ref_init (&r2, store);
1115 return refs_may_alias_p_1 (&r1, &r2, false);
1118 /* Returns true if there is a output dependence for the stores
1119 STORE1 and STORE2. */
1122 refs_output_dependent_p (tree store1, tree store2)
1125 ao_ref_init (&r1, store1);
1126 ao_ref_init (&r2, store2);
1127 return refs_may_alias_p_1 (&r1, &r2, false);
1130 /* If the call CALL may use the memory reference REF return true,
1131 otherwise return false. */
1134 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1138 int flags = gimple_call_flags (call);
1140 /* Const functions without a static chain do not implicitly use memory. */
1141 if (!gimple_call_chain (call)
1142 && (flags & (ECF_CONST|ECF_NOVOPS)))
1145 base = ao_ref_base (ref);
1149 /* If the reference is based on a decl that is not aliased the call
1150 cannot possibly use it. */
1152 && !may_be_aliased (base)
1153 /* But local statics can be used through recursion. */
1154 && !is_global_var (base))
1157 callee = gimple_call_fndecl (call);
1159 /* Handle those builtin functions explicitly that do not act as
1160 escape points. See tree-ssa-structalias.c:find_func_aliases
1161 for the list of builtins we might need to handle here. */
1162 if (callee != NULL_TREE
1163 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1164 switch (DECL_FUNCTION_CODE (callee))
1166 /* All the following functions clobber memory pointed to by
1167 their first argument. */
1168 case BUILT_IN_STRCPY:
1169 case BUILT_IN_STRNCPY:
1170 case BUILT_IN_MEMCPY:
1171 case BUILT_IN_MEMMOVE:
1172 case BUILT_IN_MEMPCPY:
1173 case BUILT_IN_STPCPY:
1174 case BUILT_IN_STPNCPY:
1175 case BUILT_IN_STRCAT:
1176 case BUILT_IN_STRNCAT:
1179 tree size = NULL_TREE;
1180 if (gimple_call_num_args (call) == 3)
1181 size = gimple_call_arg (call, 2);
1182 ao_ref_init_from_ptr_and_size (&dref,
1183 gimple_call_arg (call, 1),
1185 return refs_may_alias_p_1 (&dref, ref, false);
1187 case BUILT_IN_BCOPY:
1190 tree size = gimple_call_arg (call, 2);
1191 ao_ref_init_from_ptr_and_size (&dref,
1192 gimple_call_arg (call, 0),
1194 return refs_may_alias_p_1 (&dref, ref, false);
1196 /* The following builtins do not read from memory. */
1198 case BUILT_IN_MALLOC:
1199 case BUILT_IN_CALLOC:
1200 case BUILT_IN_MEMSET:
1201 case BUILT_IN_FREXP:
1202 case BUILT_IN_FREXPF:
1203 case BUILT_IN_FREXPL:
1204 case BUILT_IN_GAMMA_R:
1205 case BUILT_IN_GAMMAF_R:
1206 case BUILT_IN_GAMMAL_R:
1207 case BUILT_IN_LGAMMA_R:
1208 case BUILT_IN_LGAMMAF_R:
1209 case BUILT_IN_LGAMMAL_R:
1211 case BUILT_IN_MODFF:
1212 case BUILT_IN_MODFL:
1213 case BUILT_IN_REMQUO:
1214 case BUILT_IN_REMQUOF:
1215 case BUILT_IN_REMQUOL:
1216 case BUILT_IN_SINCOS:
1217 case BUILT_IN_SINCOSF:
1218 case BUILT_IN_SINCOSL:
1220 /* __sync_* builtins and some OpenMP builtins act as threading
1222 #undef DEF_SYNC_BUILTIN
1223 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1224 #include "sync-builtins.def"
1225 #undef DEF_SYNC_BUILTIN
1226 case BUILT_IN_GOMP_ATOMIC_START:
1227 case BUILT_IN_GOMP_ATOMIC_END:
1228 case BUILT_IN_GOMP_BARRIER:
1229 case BUILT_IN_GOMP_TASKWAIT:
1230 case BUILT_IN_GOMP_CRITICAL_START:
1231 case BUILT_IN_GOMP_CRITICAL_END:
1232 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1233 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1234 case BUILT_IN_GOMP_LOOP_END:
1235 case BUILT_IN_GOMP_ORDERED_START:
1236 case BUILT_IN_GOMP_ORDERED_END:
1237 case BUILT_IN_GOMP_PARALLEL_END:
1238 case BUILT_IN_GOMP_SECTIONS_END:
1239 case BUILT_IN_GOMP_SINGLE_COPY_START:
1240 case BUILT_IN_GOMP_SINGLE_COPY_END:
1244 /* Fallthru to general call handling. */;
1247 /* Check if base is a global static variable that is not read
1249 if (TREE_CODE (base) == VAR_DECL
1250 && TREE_STATIC (base))
1254 if (callee != NULL_TREE
1256 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1257 && bitmap_bit_p (not_read, DECL_UID (base)))
1261 /* Check if the base variable is call-used. */
1264 if (pt_solution_includes (gimple_call_use_set (call), base))
1267 else if ((INDIRECT_REF_P (base)
1268 || TREE_CODE (base) == MEM_REF)
1269 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1271 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1275 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1281 /* Inspect call arguments for passed-by-value aliases. */
1283 for (i = 0; i < gimple_call_num_args (call); ++i)
1285 tree op = gimple_call_arg (call, i);
1286 int flags = gimple_call_arg_flags (call, i);
1288 if (flags & EAF_UNUSED)
1291 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1292 op = TREE_OPERAND (op, 0);
1294 if (TREE_CODE (op) != SSA_NAME
1295 && !is_gimple_min_invariant (op))
1298 ao_ref_init (&r, op);
1299 if (refs_may_alias_p_1 (&r, ref, true))
1308 ref_maybe_used_by_call_p (gimple call, tree ref)
1312 ao_ref_init (&r, ref);
1313 res = ref_maybe_used_by_call_p_1 (call, &r);
1315 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1317 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1322 /* If the statement STMT may use the memory reference REF return
1323 true, otherwise return false. */
1326 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1328 if (is_gimple_assign (stmt))
1332 /* All memory assign statements are single. */
1333 if (!gimple_assign_single_p (stmt))
1336 rhs = gimple_assign_rhs1 (stmt);
1337 if (is_gimple_reg (rhs)
1338 || is_gimple_min_invariant (rhs)
1339 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1342 return refs_may_alias_p (rhs, ref);
1344 else if (is_gimple_call (stmt))
1345 return ref_maybe_used_by_call_p (stmt, ref);
1350 /* If the call in statement CALL may clobber the memory reference REF
1351 return true, otherwise return false. */
1354 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1359 /* If the call is pure or const it cannot clobber anything. */
1360 if (gimple_call_flags (call)
1361 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1364 base = ao_ref_base (ref);
1368 if (TREE_CODE (base) == SSA_NAME
1369 || CONSTANT_CLASS_P (base))
1372 /* If the reference is based on a decl that is not aliased the call
1373 cannot possibly clobber it. */
1375 && !may_be_aliased (base)
1376 /* But local non-readonly statics can be modified through recursion
1377 or the call may implement a threading barrier which we must
1378 treat as may-def. */
1379 && (TREE_READONLY (base)
1380 || !is_global_var (base)))
1383 callee = gimple_call_fndecl (call);
1385 /* Handle those builtin functions explicitly that do not act as
1386 escape points. See tree-ssa-structalias.c:find_func_aliases
1387 for the list of builtins we might need to handle here. */
1388 if (callee != NULL_TREE
1389 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1390 switch (DECL_FUNCTION_CODE (callee))
1392 /* All the following functions clobber memory pointed to by
1393 their first argument. */
1394 case BUILT_IN_STRCPY:
1395 case BUILT_IN_STRNCPY:
1396 case BUILT_IN_MEMCPY:
1397 case BUILT_IN_MEMMOVE:
1398 case BUILT_IN_MEMPCPY:
1399 case BUILT_IN_STPCPY:
1400 case BUILT_IN_STPNCPY:
1401 case BUILT_IN_STRCAT:
1402 case BUILT_IN_STRNCAT:
1403 case BUILT_IN_MEMSET:
1406 tree size = NULL_TREE;
1407 if (gimple_call_num_args (call) == 3)
1408 size = gimple_call_arg (call, 2);
1409 ao_ref_init_from_ptr_and_size (&dref,
1410 gimple_call_arg (call, 0),
1412 return refs_may_alias_p_1 (&dref, ref, false);
1414 case BUILT_IN_BCOPY:
1417 tree size = gimple_call_arg (call, 2);
1418 ao_ref_init_from_ptr_and_size (&dref,
1419 gimple_call_arg (call, 1),
1421 return refs_may_alias_p_1 (&dref, ref, false);
1423 /* Allocating memory does not have any side-effects apart from
1424 being the definition point for the pointer. */
1425 case BUILT_IN_MALLOC:
1426 case BUILT_IN_CALLOC:
1427 /* Unix98 specifies that errno is set on allocation failure. */
1429 && targetm.ref_may_alias_errno (ref))
1432 /* Freeing memory kills the pointed-to memory. More importantly
1433 the call has to serve as a barrier for moving loads and stores
1437 tree ptr = gimple_call_arg (call, 0);
1438 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1440 case BUILT_IN_GAMMA_R:
1441 case BUILT_IN_GAMMAF_R:
1442 case BUILT_IN_GAMMAL_R:
1443 case BUILT_IN_LGAMMA_R:
1444 case BUILT_IN_LGAMMAF_R:
1445 case BUILT_IN_LGAMMAL_R:
1447 tree out = gimple_call_arg (call, 1);
1448 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1450 if (flag_errno_math)
1454 case BUILT_IN_FREXP:
1455 case BUILT_IN_FREXPF:
1456 case BUILT_IN_FREXPL:
1458 case BUILT_IN_MODFF:
1459 case BUILT_IN_MODFL:
1461 tree out = gimple_call_arg (call, 1);
1462 return ptr_deref_may_alias_ref_p_1 (out, ref);
1464 case BUILT_IN_REMQUO:
1465 case BUILT_IN_REMQUOF:
1466 case BUILT_IN_REMQUOL:
1468 tree out = gimple_call_arg (call, 2);
1469 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1471 if (flag_errno_math)
1475 case BUILT_IN_SINCOS:
1476 case BUILT_IN_SINCOSF:
1477 case BUILT_IN_SINCOSL:
1479 tree sin = gimple_call_arg (call, 1);
1480 tree cos = gimple_call_arg (call, 2);
1481 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1482 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1484 /* __sync_* builtins and some OpenMP builtins act as threading
1486 #undef DEF_SYNC_BUILTIN
1487 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1488 #include "sync-builtins.def"
1489 #undef DEF_SYNC_BUILTIN
1490 case BUILT_IN_GOMP_ATOMIC_START:
1491 case BUILT_IN_GOMP_ATOMIC_END:
1492 case BUILT_IN_GOMP_BARRIER:
1493 case BUILT_IN_GOMP_TASKWAIT:
1494 case BUILT_IN_GOMP_CRITICAL_START:
1495 case BUILT_IN_GOMP_CRITICAL_END:
1496 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1497 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1498 case BUILT_IN_GOMP_LOOP_END:
1499 case BUILT_IN_GOMP_ORDERED_START:
1500 case BUILT_IN_GOMP_ORDERED_END:
1501 case BUILT_IN_GOMP_PARALLEL_END:
1502 case BUILT_IN_GOMP_SECTIONS_END:
1503 case BUILT_IN_GOMP_SINGLE_COPY_START:
1504 case BUILT_IN_GOMP_SINGLE_COPY_END:
1507 /* Fallthru to general call handling. */;
1510 /* Check if base is a global static variable that is not written
1512 if (callee != NULL_TREE
1513 && TREE_CODE (base) == VAR_DECL
1514 && TREE_STATIC (base))
1519 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1520 && bitmap_bit_p (not_written, DECL_UID (base)))
1524 /* Check if the base variable is call-clobbered. */
1526 return pt_solution_includes (gimple_call_clobber_set (call), base);
1527 else if ((INDIRECT_REF_P (base)
1528 || TREE_CODE (base) == MEM_REF)
1529 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1531 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1535 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1541 /* If the call in statement CALL may clobber the memory reference REF
1542 return true, otherwise return false. */
1545 call_may_clobber_ref_p (gimple call, tree ref)
1549 ao_ref_init (&r, ref);
1550 res = call_may_clobber_ref_p_1 (call, &r);
1552 ++alias_stats.call_may_clobber_ref_p_may_alias;
1554 ++alias_stats.call_may_clobber_ref_p_no_alias;
1559 /* If the statement STMT may clobber the memory reference REF return true,
1560 otherwise return false. */
1563 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1565 if (is_gimple_call (stmt))
1567 tree lhs = gimple_call_lhs (stmt);
1569 && TREE_CODE (lhs) != SSA_NAME)
1572 ao_ref_init (&r, lhs);
1573 if (refs_may_alias_p_1 (ref, &r, true))
1577 return call_may_clobber_ref_p_1 (stmt, ref);
1579 else if (gimple_assign_single_p (stmt))
1581 tree lhs = gimple_assign_lhs (stmt);
1582 if (TREE_CODE (lhs) != SSA_NAME)
1585 ao_ref_init (&r, lhs);
1586 return refs_may_alias_p_1 (ref, &r, true);
1589 else if (gimple_code (stmt) == GIMPLE_ASM)
1596 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1599 ao_ref_init (&r, ref);
1600 return stmt_may_clobber_ref_p_1 (stmt, &r);
1603 /* If STMT kills the memory reference REF return true, otherwise
1607 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1609 if (gimple_has_lhs (stmt)
1610 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
1612 tree base, lhs = gimple_get_lhs (stmt);
1613 HOST_WIDE_INT size, offset, max_size;
1615 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1616 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1617 so base == ref->base does not always hold. */
1618 if (base == ref->base)
1620 /* For a must-alias check we need to be able to constrain
1621 the accesses properly. */
1622 if (size != -1 && size == max_size
1623 && ref->max_size != -1)
1625 if (offset <= ref->offset
1626 && offset + size >= ref->offset + ref->max_size)
1635 stmt_kills_ref_p (gimple stmt, tree ref)
1638 ao_ref_init (&r, ref);
1639 return stmt_kills_ref_p_1 (stmt, &r);
1643 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1644 TARGET or a statement clobbering the memory reference REF in which
1645 case false is returned. The walk starts with VUSE, one argument of PHI. */
1648 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1649 tree vuse, bitmap *visited)
1652 *visited = BITMAP_ALLOC (NULL);
1654 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1656 /* Walk until we hit the target. */
1657 while (vuse != target)
1659 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1660 /* Recurse for PHI nodes. */
1661 if (gimple_code (def_stmt) == GIMPLE_PHI)
1663 /* An already visited PHI node ends the walk successfully. */
1664 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1666 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1671 /* A clobbering statement or the end of the IL ends it failing. */
1672 else if (gimple_nop_p (def_stmt)
1673 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1675 vuse = gimple_vuse (def_stmt);
1680 /* Starting from a PHI node for the virtual operand of the memory reference
1681 REF find a continuation virtual operand that allows to continue walking
1682 statements dominating PHI skipping only statements that cannot possibly
1683 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1687 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1689 unsigned nargs = gimple_phi_num_args (phi);
1691 /* Through a single-argument PHI we can simply look through. */
1693 return PHI_ARG_DEF (phi, 0);
1695 /* For two arguments try to skip non-aliasing code until we hit
1696 the phi argument definition that dominates the other one. */
1699 tree arg0 = PHI_ARG_DEF (phi, 0);
1700 tree arg1 = PHI_ARG_DEF (phi, 1);
1701 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1702 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1707 else if (gimple_nop_p (def0)
1708 || (!gimple_nop_p (def1)
1709 && dominated_by_p (CDI_DOMINATORS,
1710 gimple_bb (def1), gimple_bb (def0))))
1712 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1715 else if (gimple_nop_p (def1)
1716 || dominated_by_p (CDI_DOMINATORS,
1717 gimple_bb (def0), gimple_bb (def1)))
1719 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1722 /* Special case of a diamond:
1724 goto (cond) ? L1 : L2
1725 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1727 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1728 L3: MEM_4 = PHI<MEM_2, MEM_3>
1729 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1730 dominate each other, but still we can easily skip this PHI node
1731 if we recognize that the vuse MEM operand is the same for both,
1732 and that we can skip both statements (they don't clobber us).
1733 This is still linear. Don't use maybe_skip_until, that might
1734 potentially be slow. */
1735 else if ((common_vuse = gimple_vuse (def0))
1736 && common_vuse == gimple_vuse (def1))
1738 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1739 && !stmt_may_clobber_ref_p_1 (def1, ref))
1747 /* Based on the memory reference REF and its virtual use VUSE call
1748 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1749 itself. That is, for each virtual use for which its defining statement
1750 does not clobber REF.
1752 WALKER is called with REF, the current virtual use and DATA. If
1753 WALKER returns non-NULL the walk stops and its result is returned.
1754 At the end of a non-successful walk NULL is returned.
1756 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1757 use which definition is a statement that may clobber REF and DATA.
1758 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1759 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1760 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1761 to adjust REF and *DATA to make that valid.
1763 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1766 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1767 void *(*walker)(ao_ref *, tree, void *),
1768 void *(*translate)(ao_ref *, tree, void *), void *data)
1770 bitmap visited = NULL;
1773 timevar_push (TV_ALIAS_STMT_WALK);
1779 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1780 res = (*walker) (ref, vuse, data);
1784 def_stmt = SSA_NAME_DEF_STMT (vuse);
1785 if (gimple_nop_p (def_stmt))
1787 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1788 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1791 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1795 res = (*translate) (ref, vuse, data);
1796 /* Failed lookup and translation. */
1797 if (res == (void *)-1)
1802 /* Lookup succeeded. */
1803 else if (res != NULL)
1805 /* Translation succeeded, continue walking. */
1807 vuse = gimple_vuse (def_stmt);
1813 BITMAP_FREE (visited);
1815 timevar_pop (TV_ALIAS_STMT_WALK);
1821 /* Based on the memory reference REF call WALKER for each vdef which
1822 defining statement may clobber REF, starting with VDEF. If REF
1823 is NULL_TREE, each defining statement is visited.
1825 WALKER is called with REF, the current vdef and DATA. If WALKER
1826 returns true the walk is stopped, otherwise it continues.
1828 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1829 PHI argument (but only one walk continues on merge points), the
1830 return value is true if any of the walks was successful.
1832 The function returns the number of statements walked. */
1835 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1836 bool (*walker)(ao_ref *, tree, void *), void *data,
1837 bitmap *visited, unsigned int cnt)
1841 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1844 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1847 if (gimple_nop_p (def_stmt))
1849 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1853 *visited = BITMAP_ALLOC (NULL);
1854 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1855 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1856 walker, data, visited, 0);
1860 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1863 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1864 && (*walker) (ref, vdef, data))
1867 vdef = gimple_vuse (def_stmt);
1873 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1874 bool (*walker)(ao_ref *, tree, void *), void *data,
1877 bitmap local_visited = NULL;
1880 timevar_push (TV_ALIAS_STMT_WALK);
1882 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1883 visited ? visited : &local_visited, 0);
1885 BITMAP_FREE (local_visited);
1887 timevar_pop (TV_ALIAS_STMT_WALK);