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"
46 #include "pointer-set.h"
47 #include "alloc-pool.h"
48 #include "tree-ssa-alias.h"
50 /* Broad overview of how alias analysis on gimple works:
52 Statements clobbering or using memory are linked through the
53 virtual operand factored use-def chain. The virtual operand
54 is unique per function, its symbol is accessible via gimple_vop (cfun).
55 Virtual operands are used for efficiently walking memory statements
56 in the gimple IL and are useful for things like value-numbering as
57 a generation count for memory references.
59 SSA_NAME pointers may have associated points-to information
60 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
61 points-to information is (re-)computed by the TODO_rebuild_alias
62 pass manager todo. Points-to information is also used for more
63 precise tracking of call-clobbered and call-used variables and
64 related disambiguations.
66 This file contains functions for disambiguating memory references,
67 the so called alias-oracle and tools for walking of the gimple IL.
69 The main alias-oracle entry-points are
71 bool stmt_may_clobber_ref_p (gimple, tree)
73 This function queries if a statement may invalidate (parts of)
74 the memory designated by the reference tree argument.
76 bool ref_maybe_used_by_stmt_p (gimple, tree)
78 This function queries if a statement may need (parts of) the
79 memory designated by the reference tree argument.
81 There are variants of these functions that only handle the call
82 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
83 Note that these do not disambiguate against a possible call lhs.
85 bool refs_may_alias_p (tree, tree)
87 This function tries to disambiguate two reference trees.
89 bool ptr_deref_may_alias_global_p (tree)
91 This function queries if dereferencing a pointer variable may
94 More low-level disambiguators are available and documented in
95 this file. Low-level disambiguators dealing with points-to
96 information are in tree-ssa-structalias.c. */
99 /* Query statistics for the different low-level disambiguators.
100 A high-level query may trigger multiple of them. */
103 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias
;
104 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias
;
105 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias
;
106 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias
;
107 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias
;
108 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias
;
112 dump_alias_stats (FILE *s
)
114 fprintf (s
, "\nAlias oracle query stats:\n");
115 fprintf (s
, " refs_may_alias_p: "
116 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
117 HOST_WIDE_INT_PRINT_DEC
" queries\n",
118 alias_stats
.refs_may_alias_p_no_alias
,
119 alias_stats
.refs_may_alias_p_no_alias
120 + alias_stats
.refs_may_alias_p_may_alias
);
121 fprintf (s
, " ref_maybe_used_by_call_p: "
122 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
123 HOST_WIDE_INT_PRINT_DEC
" queries\n",
124 alias_stats
.ref_maybe_used_by_call_p_no_alias
,
125 alias_stats
.refs_may_alias_p_no_alias
126 + alias_stats
.ref_maybe_used_by_call_p_may_alias
);
127 fprintf (s
, " call_may_clobber_ref_p: "
128 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
129 HOST_WIDE_INT_PRINT_DEC
" queries\n",
130 alias_stats
.call_may_clobber_ref_p_no_alias
,
131 alias_stats
.call_may_clobber_ref_p_no_alias
132 + alias_stats
.call_may_clobber_ref_p_may_alias
);
136 /* Return true, if dereferencing PTR may alias with a global variable. */
139 ptr_deref_may_alias_global_p (tree ptr
)
141 struct ptr_info_def
*pi
;
143 /* If we end up with a pointer constant here that may point
145 if (TREE_CODE (ptr
) != SSA_NAME
)
148 pi
= SSA_NAME_PTR_INFO (ptr
);
150 /* If we do not have points-to information for this variable,
155 /* ??? This does not use TBAA to prune globals ptr may not access. */
156 return pt_solution_includes_global (&pi
->pt
);
159 /* Return true if dereferencing PTR may alias DECL.
160 The caller is responsible for applying TBAA to see if PTR
161 may access DECL at all. */
164 ptr_deref_may_alias_decl_p (tree ptr
, tree decl
)
166 struct ptr_info_def
*pi
;
168 /* Conversions are irrelevant for points-to information and
169 data-dependence analysis can feed us those. */
172 /* Anything we do not explicilty handle aliases. */
173 if ((TREE_CODE (ptr
) != SSA_NAME
174 && TREE_CODE (ptr
) != ADDR_EXPR
175 && TREE_CODE (ptr
) != POINTER_PLUS_EXPR
)
176 || !POINTER_TYPE_P (TREE_TYPE (ptr
))
177 || (TREE_CODE (decl
) != VAR_DECL
178 && TREE_CODE (decl
) != PARM_DECL
179 && TREE_CODE (decl
) != RESULT_DECL
))
182 /* Disregard pointer offsetting. */
183 if (TREE_CODE (ptr
) == POINTER_PLUS_EXPR
)
187 ptr
= TREE_OPERAND (ptr
, 0);
189 while (TREE_CODE (ptr
) == POINTER_PLUS_EXPR
);
190 return ptr_deref_may_alias_decl_p (ptr
, decl
);
193 /* ADDR_EXPR pointers either just offset another pointer or directly
194 specify the pointed-to set. */
195 if (TREE_CODE (ptr
) == ADDR_EXPR
)
197 tree base
= get_base_address (TREE_OPERAND (ptr
, 0));
199 && (TREE_CODE (base
) == MEM_REF
200 || TREE_CODE (base
) == TARGET_MEM_REF
))
201 ptr
= TREE_OPERAND (base
, 0);
206 && CONSTANT_CLASS_P (base
))
212 /* Non-aliased variables can not be pointed to. */
213 if (!may_be_aliased (decl
))
216 /* If we do not have useful points-to information for this pointer
217 we cannot disambiguate anything else. */
218 pi
= SSA_NAME_PTR_INFO (ptr
);
222 /* If the decl can be used as a restrict tag and we have a restrict
223 pointer and that pointers points-to set doesn't contain this decl
224 then they can't alias. */
225 if (DECL_RESTRICTED_P (decl
)
226 && TYPE_RESTRICT (TREE_TYPE (ptr
))
227 && pi
->pt
.vars_contains_restrict
)
228 return bitmap_bit_p (pi
->pt
.vars
, DECL_PT_UID (decl
));
230 return pt_solution_includes (&pi
->pt
, decl
);
233 /* Return true if dereferenced PTR1 and PTR2 may alias.
234 The caller is responsible for applying TBAA to see if accesses
235 through PTR1 and PTR2 may conflict at all. */
238 ptr_derefs_may_alias_p (tree ptr1
, tree ptr2
)
240 struct ptr_info_def
*pi1
, *pi2
;
242 /* Conversions are irrelevant for points-to information and
243 data-dependence analysis can feed us those. */
247 /* Anything we do not explicilty handle aliases. */
248 if ((TREE_CODE (ptr1
) != SSA_NAME
249 && TREE_CODE (ptr1
) != ADDR_EXPR
250 && TREE_CODE (ptr1
) != POINTER_PLUS_EXPR
)
251 || (TREE_CODE (ptr2
) != SSA_NAME
252 && TREE_CODE (ptr2
) != ADDR_EXPR
253 && TREE_CODE (ptr2
) != POINTER_PLUS_EXPR
)
254 || !POINTER_TYPE_P (TREE_TYPE (ptr1
))
255 || !POINTER_TYPE_P (TREE_TYPE (ptr2
)))
258 /* Disregard pointer offsetting. */
259 if (TREE_CODE (ptr1
) == POINTER_PLUS_EXPR
)
263 ptr1
= TREE_OPERAND (ptr1
, 0);
265 while (TREE_CODE (ptr1
) == POINTER_PLUS_EXPR
);
266 return ptr_derefs_may_alias_p (ptr1
, ptr2
);
268 if (TREE_CODE (ptr2
) == POINTER_PLUS_EXPR
)
272 ptr2
= TREE_OPERAND (ptr2
, 0);
274 while (TREE_CODE (ptr2
) == POINTER_PLUS_EXPR
);
275 return ptr_derefs_may_alias_p (ptr1
, ptr2
);
278 /* ADDR_EXPR pointers either just offset another pointer or directly
279 specify the pointed-to set. */
280 if (TREE_CODE (ptr1
) == ADDR_EXPR
)
282 tree base
= get_base_address (TREE_OPERAND (ptr1
, 0));
284 && (TREE_CODE (base
) == MEM_REF
285 || TREE_CODE (base
) == TARGET_MEM_REF
))
286 ptr1
= TREE_OPERAND (base
, 0);
289 return ptr_deref_may_alias_decl_p (ptr2
, base
);
293 if (TREE_CODE (ptr2
) == ADDR_EXPR
)
295 tree base
= get_base_address (TREE_OPERAND (ptr2
, 0));
297 && (TREE_CODE (base
) == MEM_REF
298 || TREE_CODE (base
) == TARGET_MEM_REF
))
299 ptr2
= TREE_OPERAND (base
, 0);
302 return ptr_deref_may_alias_decl_p (ptr1
, base
);
307 /* We may end up with two empty points-to solutions for two same pointers.
308 In this case we still want to say both pointers alias, so shortcut
313 /* If we do not have useful points-to information for either pointer
314 we cannot disambiguate anything else. */
315 pi1
= SSA_NAME_PTR_INFO (ptr1
);
316 pi2
= SSA_NAME_PTR_INFO (ptr2
);
320 /* If both pointers are restrict-qualified try to disambiguate
321 with restrict information. */
322 if (TYPE_RESTRICT (TREE_TYPE (ptr1
))
323 && TYPE_RESTRICT (TREE_TYPE (ptr2
))
324 && !pt_solutions_same_restrict_base (&pi1
->pt
, &pi2
->pt
))
327 /* ??? This does not use TBAA to prune decls from the intersection
328 that not both pointers may access. */
329 return pt_solutions_intersect (&pi1
->pt
, &pi2
->pt
);
332 /* Return true if dereferencing PTR may alias *REF.
333 The caller is responsible for applying TBAA to see if PTR
334 may access *REF at all. */
337 ptr_deref_may_alias_ref_p_1 (tree ptr
, ao_ref
*ref
)
339 tree base
= ao_ref_base (ref
);
341 if (TREE_CODE (base
) == MEM_REF
342 || TREE_CODE (base
) == TARGET_MEM_REF
)
343 return ptr_derefs_may_alias_p (ptr
, TREE_OPERAND (base
, 0));
344 else if (DECL_P (base
))
345 return ptr_deref_may_alias_decl_p (ptr
, base
);
351 /* Dump alias information on FILE. */
354 dump_alias_info (FILE *file
)
358 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
359 referenced_var_iterator rvi
;
362 fprintf (file
, "\n\nAlias information for %s\n\n", funcname
);
364 fprintf (file
, "Aliased symbols\n\n");
366 FOR_EACH_REFERENCED_VAR (cfun
, var
, rvi
)
368 if (may_be_aliased (var
))
369 dump_variable (file
, var
);
372 fprintf (file
, "\nCall clobber information\n");
374 fprintf (file
, "\nESCAPED");
375 dump_points_to_solution (file
, &cfun
->gimple_df
->escaped
);
377 fprintf (file
, "\n\nFlow-insensitive points-to information\n\n");
379 for (i
= 1; i
< num_ssa_names
; i
++)
381 tree ptr
= ssa_name (i
);
382 struct ptr_info_def
*pi
;
385 || SSA_NAME_IN_FREE_LIST (ptr
))
388 pi
= SSA_NAME_PTR_INFO (ptr
);
390 dump_points_to_info_for (file
, ptr
);
393 fprintf (file
, "\n");
397 /* Dump alias information on stderr. */
400 debug_alias_info (void)
402 dump_alias_info (stderr
);
406 /* Dump the points-to set *PT into FILE. */
409 dump_points_to_solution (FILE *file
, struct pt_solution
*pt
)
412 fprintf (file
, ", points-to anything");
415 fprintf (file
, ", points-to non-local");
418 fprintf (file
, ", points-to escaped");
421 fprintf (file
, ", points-to unit escaped");
424 fprintf (file
, ", points-to NULL");
428 fprintf (file
, ", points-to vars: ");
429 dump_decl_set (file
, pt
->vars
);
430 if (pt
->vars_contains_global
)
431 fprintf (file
, " (includes global vars)");
432 if (pt
->vars_contains_restrict
)
433 fprintf (file
, " (includes restrict tags)");
437 /* Dump points-to information for SSA_NAME PTR into FILE. */
440 dump_points_to_info_for (FILE *file
, tree ptr
)
442 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (ptr
);
444 print_generic_expr (file
, ptr
, dump_flags
);
447 dump_points_to_solution (file
, &pi
->pt
);
449 fprintf (file
, ", points-to anything");
451 fprintf (file
, "\n");
455 /* Dump points-to information for VAR into stderr. */
458 debug_points_to_info_for (tree var
)
460 dump_points_to_info_for (stderr
, var
);
464 /* Initializes the alias-oracle reference representation *R from REF. */
467 ao_ref_init (ao_ref
*r
, tree ref
)
474 r
->ref_alias_set
= -1;
475 r
->base_alias_set
= -1;
478 /* Returns the base object of the memory reference *REF. */
481 ao_ref_base (ao_ref
*ref
)
485 ref
->base
= get_ref_base_and_extent (ref
->ref
, &ref
->offset
, &ref
->size
,
490 /* Returns the base object alias set of the memory reference *REF. */
492 static alias_set_type
493 ao_ref_base_alias_set (ao_ref
*ref
)
496 if (ref
->base_alias_set
!= -1)
497 return ref
->base_alias_set
;
501 while (handled_component_p (base_ref
))
502 base_ref
= TREE_OPERAND (base_ref
, 0);
503 ref
->base_alias_set
= get_alias_set (base_ref
);
504 return ref
->base_alias_set
;
507 /* Returns the reference alias set of the memory reference *REF. */
510 ao_ref_alias_set (ao_ref
*ref
)
512 if (ref
->ref_alias_set
!= -1)
513 return ref
->ref_alias_set
;
514 ref
->ref_alias_set
= get_alias_set (ref
->ref
);
515 return ref
->ref_alias_set
;
518 /* Init an alias-oracle reference representation from a gimple pointer
519 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
520 size is assumed to be unknown. The access is assumed to be only
521 to or after of the pointer target, not before it. */
524 ao_ref_init_from_ptr_and_size (ao_ref
*ref
, tree ptr
, tree size
)
526 HOST_WIDE_INT t1
, t2
;
527 ref
->ref
= NULL_TREE
;
528 if (TREE_CODE (ptr
) == ADDR_EXPR
)
529 ref
->base
= get_ref_base_and_extent (TREE_OPERAND (ptr
, 0),
530 &ref
->offset
, &t1
, &t2
);
533 ref
->base
= build2 (MEM_REF
, char_type_node
,
534 ptr
, null_pointer_node
);
538 && host_integerp (size
, 0)
539 && TREE_INT_CST_LOW (size
) * 8 / 8 == TREE_INT_CST_LOW (size
))
540 ref
->max_size
= ref
->size
= TREE_INT_CST_LOW (size
) * 8;
542 ref
->max_size
= ref
->size
= -1;
543 ref
->ref_alias_set
= 0;
544 ref
->base_alias_set
= 0;
547 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
548 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
552 same_type_for_tbaa (tree type1
, tree type2
)
554 type1
= TYPE_MAIN_VARIANT (type1
);
555 type2
= TYPE_MAIN_VARIANT (type2
);
557 /* If we would have to do structural comparison bail out. */
558 if (TYPE_STRUCTURAL_EQUALITY_P (type1
)
559 || TYPE_STRUCTURAL_EQUALITY_P (type2
))
562 /* Compare the canonical types. */
563 if (TYPE_CANONICAL (type1
) == TYPE_CANONICAL (type2
))
566 /* ??? Array types are not properly unified in all cases as we have
567 spurious changes in the index types for example. Removing this
568 causes all sorts of problems with the Fortran frontend. */
569 if (TREE_CODE (type1
) == ARRAY_TYPE
570 && TREE_CODE (type2
) == ARRAY_TYPE
)
573 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
574 object of one of its constrained subtypes, e.g. when a function with an
575 unconstrained parameter passed by reference is called on an object and
576 inlined. But, even in the case of a fixed size, type and subtypes are
577 not equivalent enough as to share the same TYPE_CANONICAL, since this
578 would mean that conversions between them are useless, whereas they are
579 not (e.g. type and subtypes can have different modes). So, in the end,
580 they are only guaranteed to have the same alias set. */
581 if (get_alias_set (type1
) == get_alias_set (type2
))
584 /* The types are known to be not equal. */
588 /* Determine if the two component references REF1 and REF2 which are
589 based on access types TYPE1 and TYPE2 and of which at least one is based
590 on an indirect reference may alias. REF2 is the only one that can
591 be a decl in which case REF2_IS_DECL is true.
592 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
593 are the respective alias sets. */
596 aliasing_component_refs_p (tree ref1
,
597 alias_set_type ref1_alias_set
,
598 alias_set_type base1_alias_set
,
599 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
601 alias_set_type ref2_alias_set
,
602 alias_set_type base2_alias_set
,
603 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
606 /* If one reference is a component references through pointers try to find a
607 common base and apply offset based disambiguation. This handles
609 struct A { int i; int j; } *q;
610 struct B { struct A a; int k; } *p;
611 disambiguating q->i and p->a.j. */
617 /* Choose bases and base types to search for. */
619 while (handled_component_p (base1
))
620 base1
= TREE_OPERAND (base1
, 0);
621 type1
= TREE_TYPE (base1
);
623 while (handled_component_p (base2
))
624 base2
= TREE_OPERAND (base2
, 0);
625 type2
= TREE_TYPE (base2
);
627 /* Now search for the type1 in the access path of ref2. This
628 would be a common base for doing offset based disambiguation on. */
630 while (handled_component_p (*refp
)
631 && same_type_for_tbaa (TREE_TYPE (*refp
), type1
) == 0)
632 refp
= &TREE_OPERAND (*refp
, 0);
633 same_p
= same_type_for_tbaa (TREE_TYPE (*refp
), type1
);
634 /* If we couldn't compare types we have to bail out. */
637 else if (same_p
== 1)
639 HOST_WIDE_INT offadj
, sztmp
, msztmp
;
640 get_ref_base_and_extent (*refp
, &offadj
, &sztmp
, &msztmp
);
642 get_ref_base_and_extent (base1
, &offadj
, &sztmp
, &msztmp
);
644 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
646 /* If we didn't find a common base, try the other way around. */
648 while (handled_component_p (*refp
)
649 && same_type_for_tbaa (TREE_TYPE (*refp
), type2
) == 0)
650 refp
= &TREE_OPERAND (*refp
, 0);
651 same_p
= same_type_for_tbaa (TREE_TYPE (*refp
), type2
);
652 /* If we couldn't compare types we have to bail out. */
655 else if (same_p
== 1)
657 HOST_WIDE_INT offadj
, sztmp
, msztmp
;
658 get_ref_base_and_extent (*refp
, &offadj
, &sztmp
, &msztmp
);
660 get_ref_base_and_extent (base2
, &offadj
, &sztmp
, &msztmp
);
662 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
665 /* If we have two type access paths B1.path1 and B2.path2 they may
666 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
667 But we can still have a path that goes B1.path1...B2.path2 with
668 a part that we do not see. So we can only disambiguate now
669 if there is no B2 in the tail of path1 and no B1 on the
671 if (base1_alias_set
== ref2_alias_set
672 || alias_set_subset_of (base1_alias_set
, ref2_alias_set
))
674 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
676 return (base2_alias_set
== ref1_alias_set
677 || alias_set_subset_of (base2_alias_set
, ref1_alias_set
));
681 /* Return true if two memory references based on the variables BASE1
682 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
683 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
686 decl_refs_may_alias_p (tree base1
,
687 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
689 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
)
691 gcc_checking_assert (DECL_P (base1
) && DECL_P (base2
));
693 /* If both references are based on different variables, they cannot alias. */
697 /* If both references are based on the same variable, they cannot alias if
698 the accesses do not overlap. */
699 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
702 /* Return true if an indirect reference based on *PTR1 constrained
703 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
704 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
705 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
706 in which case they are computed on-demand. REF1 and REF2
707 if non-NULL are the complete memory reference trees. */
710 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED
, tree base1
,
711 HOST_WIDE_INT offset1
,
712 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED
,
713 alias_set_type ref1_alias_set
,
714 alias_set_type base1_alias_set
,
715 tree ref2 ATTRIBUTE_UNUSED
, tree base2
,
716 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
717 alias_set_type ref2_alias_set
,
718 alias_set_type base2_alias_set
, bool tbaa_p
)
722 HOST_WIDE_INT offset1p
= offset1
, offset2p
= offset2
;
725 gcc_checking_assert ((TREE_CODE (base1
) == MEM_REF
726 || TREE_CODE (base1
) == TARGET_MEM_REF
)
729 ptr1
= TREE_OPERAND (base1
, 0);
731 /* The offset embedded in MEM_REFs can be negative. Bias them
732 so that the resulting offset adjustment is positive. */
733 moff
= mem_ref_offset (base1
);
734 moff
= double_int_lshift (moff
,
736 ? 3 : exact_log2 (BITS_PER_UNIT
),
737 HOST_BITS_PER_DOUBLE_INT
, true);
738 if (double_int_negative_p (moff
))
739 offset2p
+= double_int_neg (moff
).low
;
741 offset1p
+= moff
.low
;
743 /* If only one reference is based on a variable, they cannot alias if
744 the pointer access is beyond the extent of the variable access.
745 (the pointer base cannot validly point to an offset less than zero
747 They also cannot alias if the pointer may not point to the decl. */
748 if ((TREE_CODE (base1
) != TARGET_MEM_REF
749 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
750 && !ranges_overlap_p (MAX (0, offset1p
), -1, offset2p
, max_size2
))
752 if (!ptr_deref_may_alias_decl_p (ptr1
, base2
))
755 /* Disambiguations that rely on strict aliasing rules follow. */
756 if (!flag_strict_aliasing
|| !tbaa_p
)
759 ptrtype1
= TREE_TYPE (TREE_OPERAND (base1
, 1));
761 /* If the alias set for a pointer access is zero all bets are off. */
762 if (base1_alias_set
== -1)
763 base1_alias_set
= get_deref_alias_set (ptrtype1
);
764 if (base1_alias_set
== 0)
766 if (base2_alias_set
== -1)
767 base2_alias_set
= get_alias_set (base2
);
769 /* If both references are through the same type, they do not alias
770 if the accesses do not overlap. This does extra disambiguation
771 for mixed/pointer accesses but requires strict aliasing.
772 For MEM_REFs we require that the component-ref offset we computed
773 is relative to the start of the type which we ensure by
774 comparing rvalue and access type and disregarding the constant
776 if ((TREE_CODE (base1
) != TARGET_MEM_REF
777 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
778 && (TREE_CODE (base1
) != MEM_REF
779 || same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) == 1)
780 && same_type_for_tbaa (TREE_TYPE (ptrtype1
), TREE_TYPE (base2
)) == 1)
781 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
783 /* When we are trying to disambiguate an access with a pointer dereference
784 as base versus one with a decl as base we can use both the size
785 of the decl and its dynamic type for extra disambiguation.
786 ??? We do not know anything about the dynamic type of the decl
787 other than that its alias-set contains base2_alias_set as a subset
788 which does not help us here. */
789 /* As we know nothing useful about the dynamic type of the decl just
790 use the usual conflict check rather than a subset test.
791 ??? We could introduce -fvery-strict-aliasing when the language
792 does not allow decls to have a dynamic type that differs from their
793 static type. Then we can check
794 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
795 if (base1_alias_set
!= base2_alias_set
796 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
798 /* If the size of the access relevant for TBAA through the pointer
799 is bigger than the size of the decl we can't possibly access the
800 decl via that pointer. */
801 if (DECL_SIZE (base2
) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1
))
802 && TREE_CODE (DECL_SIZE (base2
)) == INTEGER_CST
803 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1
))) == INTEGER_CST
804 /* ??? This in turn may run afoul when a decl of type T which is
805 a member of union type U is accessed through a pointer to
806 type U and sizeof T is smaller than sizeof U. */
807 && TREE_CODE (TREE_TYPE (ptrtype1
)) != UNION_TYPE
808 && TREE_CODE (TREE_TYPE (ptrtype1
)) != QUAL_UNION_TYPE
809 && tree_int_cst_lt (DECL_SIZE (base2
), TYPE_SIZE (TREE_TYPE (ptrtype1
))))
812 /* Do access-path based disambiguation. */
814 && handled_component_p (ref1
)
815 && handled_component_p (ref2
)
816 && TREE_CODE (base1
) != TARGET_MEM_REF
817 && (TREE_CODE (base1
) != MEM_REF
818 || same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) == 1))
819 return aliasing_component_refs_p (ref1
,
820 ref1_alias_set
, base1_alias_set
,
823 ref2_alias_set
, base2_alias_set
,
824 offset2
, max_size2
, true);
829 /* Return true if two indirect references based on *PTR1
830 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
831 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
832 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
833 in which case they are computed on-demand. REF1 and REF2
834 if non-NULL are the complete memory reference trees. */
837 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED
, tree base1
,
838 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
839 alias_set_type ref1_alias_set
,
840 alias_set_type base1_alias_set
,
841 tree ref2 ATTRIBUTE_UNUSED
, tree base2
,
842 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
843 alias_set_type ref2_alias_set
,
844 alias_set_type base2_alias_set
, bool tbaa_p
)
848 tree ptrtype1
, ptrtype2
;
850 gcc_checking_assert ((TREE_CODE (base1
) == MEM_REF
851 || TREE_CODE (base1
) == TARGET_MEM_REF
)
852 && (TREE_CODE (base2
) == MEM_REF
853 || TREE_CODE (base2
) == TARGET_MEM_REF
));
855 ptr1
= TREE_OPERAND (base1
, 0);
856 ptr2
= TREE_OPERAND (base2
, 0);
858 /* If both bases are based on pointers they cannot alias if they may not
859 point to the same memory object or if they point to the same object
860 and the accesses do not overlap. */
861 if ((!cfun
|| gimple_in_ssa_p (cfun
))
862 && operand_equal_p (ptr1
, ptr2
, 0)
863 && (((TREE_CODE (base1
) != TARGET_MEM_REF
864 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
865 && (TREE_CODE (base2
) != TARGET_MEM_REF
866 || (!TMR_INDEX (base2
) && !TMR_INDEX2 (base2
))))
867 || (TREE_CODE (base1
) == TARGET_MEM_REF
868 && TREE_CODE (base2
) == TARGET_MEM_REF
869 && (TMR_STEP (base1
) == TMR_STEP (base2
)
870 || (TMR_STEP (base1
) && TMR_STEP (base2
)
871 && operand_equal_p (TMR_STEP (base1
),
872 TMR_STEP (base2
), 0)))
873 && (TMR_INDEX (base1
) == TMR_INDEX (base2
)
874 || (TMR_INDEX (base1
) && TMR_INDEX (base2
)
875 && operand_equal_p (TMR_INDEX (base1
),
876 TMR_INDEX (base2
), 0)))
877 && (TMR_INDEX2 (base1
) == TMR_INDEX2 (base2
)
878 || (TMR_INDEX2 (base1
) && TMR_INDEX2 (base2
)
879 && operand_equal_p (TMR_INDEX2 (base1
),
880 TMR_INDEX2 (base2
), 0))))))
883 /* The offset embedded in MEM_REFs can be negative. Bias them
884 so that the resulting offset adjustment is positive. */
885 moff
= mem_ref_offset (base1
);
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 offset2
+= double_int_neg (moff
).low
;
894 moff
= mem_ref_offset (base2
);
895 moff
= double_int_lshift (moff
,
897 ? 3 : exact_log2 (BITS_PER_UNIT
),
898 HOST_BITS_PER_DOUBLE_INT
, true);
899 if (double_int_negative_p (moff
))
900 offset1
+= double_int_neg (moff
).low
;
903 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
905 if (!ptr_derefs_may_alias_p (ptr1
, ptr2
))
908 /* Disambiguations that rely on strict aliasing rules follow. */
909 if (!flag_strict_aliasing
|| !tbaa_p
)
912 ptrtype1
= TREE_TYPE (TREE_OPERAND (base1
, 1));
913 ptrtype2
= TREE_TYPE (TREE_OPERAND (base2
, 1));
915 /* If the alias set for a pointer access is zero all bets are off. */
916 if (base1_alias_set
== -1)
917 base1_alias_set
= get_deref_alias_set (ptrtype1
);
918 if (base1_alias_set
== 0)
920 if (base2_alias_set
== -1)
921 base2_alias_set
= get_deref_alias_set (ptrtype2
);
922 if (base2_alias_set
== 0)
925 /* If both references are through the same type, they do not alias
926 if the accesses do not overlap. This does extra disambiguation
927 for mixed/pointer accesses but requires strict aliasing. */
928 if ((TREE_CODE (base1
) != TARGET_MEM_REF
929 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
930 && (TREE_CODE (base2
) != TARGET_MEM_REF
931 || (!TMR_INDEX (base2
) && !TMR_INDEX2 (base2
)))
932 && same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) == 1
933 && same_type_for_tbaa (TREE_TYPE (base2
), TREE_TYPE (ptrtype2
)) == 1
934 && same_type_for_tbaa (TREE_TYPE (ptrtype1
),
935 TREE_TYPE (ptrtype2
)) == 1)
936 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
938 /* Do type-based disambiguation. */
939 if (base1_alias_set
!= base2_alias_set
940 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
943 /* Do access-path based disambiguation. */
945 && (handled_component_p (ref1
) || handled_component_p (ref2
))
946 && same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) == 1
947 && same_type_for_tbaa (TREE_TYPE (base2
), TREE_TYPE (ptrtype2
)) == 1)
948 return aliasing_component_refs_p (ref1
,
949 ref1_alias_set
, base1_alias_set
,
952 ref2_alias_set
, base2_alias_set
,
953 offset2
, max_size2
, false);
958 /* Return true, if the two memory references REF1 and REF2 may alias. */
961 refs_may_alias_p_1 (ao_ref
*ref1
, ao_ref
*ref2
, bool tbaa_p
)
964 HOST_WIDE_INT offset1
= 0, offset2
= 0;
965 HOST_WIDE_INT max_size1
= -1, max_size2
= -1;
966 bool var1_p
, var2_p
, ind1_p
, ind2_p
;
968 gcc_checking_assert ((!ref1
->ref
969 || TREE_CODE (ref1
->ref
) == SSA_NAME
970 || DECL_P (ref1
->ref
)
971 || TREE_CODE (ref1
->ref
) == STRING_CST
972 || handled_component_p (ref1
->ref
)
973 || TREE_CODE (ref1
->ref
) == MEM_REF
974 || TREE_CODE (ref1
->ref
) == TARGET_MEM_REF
)
976 || TREE_CODE (ref2
->ref
) == SSA_NAME
977 || DECL_P (ref2
->ref
)
978 || TREE_CODE (ref2
->ref
) == STRING_CST
979 || handled_component_p (ref2
->ref
)
980 || TREE_CODE (ref2
->ref
) == MEM_REF
981 || TREE_CODE (ref2
->ref
) == TARGET_MEM_REF
));
983 /* Decompose the references into their base objects and the access. */
984 base1
= ao_ref_base (ref1
);
985 offset1
= ref1
->offset
;
986 max_size1
= ref1
->max_size
;
987 base2
= ao_ref_base (ref2
);
988 offset2
= ref2
->offset
;
989 max_size2
= ref2
->max_size
;
991 /* We can end up with registers or constants as bases for example from
992 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
993 which is seen as a struct copy. */
994 if (TREE_CODE (base1
) == SSA_NAME
995 || TREE_CODE (base1
) == CONST_DECL
996 || TREE_CODE (base1
) == CONSTRUCTOR
997 || TREE_CODE (base1
) == ADDR_EXPR
998 || CONSTANT_CLASS_P (base1
)
999 || TREE_CODE (base2
) == SSA_NAME
1000 || TREE_CODE (base2
) == CONST_DECL
1001 || TREE_CODE (base2
) == CONSTRUCTOR
1002 || TREE_CODE (base2
) == ADDR_EXPR
1003 || CONSTANT_CLASS_P (base2
))
1006 /* We can end up refering to code via function and label decls.
1007 As we likely do not properly track code aliases conservatively
1009 if (TREE_CODE (base1
) == FUNCTION_DECL
1010 || TREE_CODE (base1
) == LABEL_DECL
1011 || TREE_CODE (base2
) == FUNCTION_DECL
1012 || TREE_CODE (base2
) == LABEL_DECL
)
1015 /* Defer to simple offset based disambiguation if we have
1016 references based on two decls. Do this before defering to
1017 TBAA to handle must-alias cases in conformance with the
1018 GCC extension of allowing type-punning through unions. */
1019 var1_p
= DECL_P (base1
);
1020 var2_p
= DECL_P (base2
);
1021 if (var1_p
&& var2_p
)
1022 return decl_refs_may_alias_p (base1
, offset1
, max_size1
,
1023 base2
, offset2
, max_size2
);
1025 ind1_p
= (TREE_CODE (base1
) == MEM_REF
1026 || TREE_CODE (base1
) == TARGET_MEM_REF
);
1027 ind2_p
= (TREE_CODE (base2
) == MEM_REF
1028 || TREE_CODE (base2
) == TARGET_MEM_REF
);
1030 /* Canonicalize the pointer-vs-decl case. */
1031 if (ind1_p
&& var2_p
)
1036 tmp1
= offset1
; offset1
= offset2
; offset2
= tmp1
;
1037 tmp1
= max_size1
; max_size1
= max_size2
; max_size2
= tmp1
;
1038 tmp2
= base1
; base1
= base2
; base2
= tmp2
;
1039 tmp3
= ref1
; ref1
= ref2
; ref2
= tmp3
;
1046 /* First defer to TBAA if possible. */
1048 && flag_strict_aliasing
1049 && !alias_sets_conflict_p (ao_ref_alias_set (ref1
),
1050 ao_ref_alias_set (ref2
)))
1053 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1054 if (var1_p
&& ind2_p
)
1055 return indirect_ref_may_alias_decl_p (ref2
->ref
, base2
,
1057 ao_ref_alias_set (ref2
), -1,
1060 ao_ref_alias_set (ref1
),
1061 ao_ref_base_alias_set (ref1
),
1063 else if (ind1_p
&& ind2_p
)
1064 return indirect_refs_may_alias_p (ref1
->ref
, base1
,
1066 ao_ref_alias_set (ref1
), -1,
1069 ao_ref_alias_set (ref2
), -1,
1072 /* We really do not want to end up here, but returning true is safe. */
1073 #ifdef ENABLE_CHECKING
1081 refs_may_alias_p (tree ref1
, tree ref2
)
1085 ao_ref_init (&r1
, ref1
);
1086 ao_ref_init (&r2
, ref2
);
1087 res
= refs_may_alias_p_1 (&r1
, &r2
, true);
1089 ++alias_stats
.refs_may_alias_p_may_alias
;
1091 ++alias_stats
.refs_may_alias_p_no_alias
;
1095 /* Returns true if there is a anti-dependence for the STORE that
1096 executes after the LOAD. */
1099 refs_anti_dependent_p (tree load
, tree store
)
1102 ao_ref_init (&r1
, load
);
1103 ao_ref_init (&r2
, store
);
1104 return refs_may_alias_p_1 (&r1
, &r2
, false);
1107 /* Returns true if there is a output dependence for the stores
1108 STORE1 and STORE2. */
1111 refs_output_dependent_p (tree store1
, tree store2
)
1114 ao_ref_init (&r1
, store1
);
1115 ao_ref_init (&r2
, store2
);
1116 return refs_may_alias_p_1 (&r1
, &r2
, false);
1119 /* If the call CALL may use the memory reference REF return true,
1120 otherwise return false. */
1123 ref_maybe_used_by_call_p_1 (gimple call
, ao_ref
*ref
)
1127 int flags
= gimple_call_flags (call
);
1129 /* Const functions without a static chain do not implicitly use memory. */
1130 if (!gimple_call_chain (call
)
1131 && (flags
& (ECF_CONST
|ECF_NOVOPS
)))
1134 base
= ao_ref_base (ref
);
1138 /* If the reference is based on a decl that is not aliased the call
1139 cannot possibly use it. */
1141 && !may_be_aliased (base
)
1142 /* But local statics can be used through recursion. */
1143 && !is_global_var (base
))
1146 callee
= gimple_call_fndecl (call
);
1148 /* Handle those builtin functions explicitly that do not act as
1149 escape points. See tree-ssa-structalias.c:find_func_aliases
1150 for the list of builtins we might need to handle here. */
1151 if (callee
!= NULL_TREE
1152 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
1153 switch (DECL_FUNCTION_CODE (callee
))
1155 /* All the following functions clobber memory pointed to by
1156 their first argument. */
1157 case BUILT_IN_STRCPY
:
1158 case BUILT_IN_STRNCPY
:
1159 case BUILT_IN_MEMCPY
:
1160 case BUILT_IN_MEMMOVE
:
1161 case BUILT_IN_MEMPCPY
:
1162 case BUILT_IN_STPCPY
:
1163 case BUILT_IN_STPNCPY
:
1164 case BUILT_IN_STRCAT
:
1165 case BUILT_IN_STRNCAT
:
1168 tree size
= NULL_TREE
;
1169 if (gimple_call_num_args (call
) == 3)
1170 size
= gimple_call_arg (call
, 2);
1171 ao_ref_init_from_ptr_and_size (&dref
,
1172 gimple_call_arg (call
, 1),
1174 return refs_may_alias_p_1 (&dref
, ref
, false);
1176 case BUILT_IN_BCOPY
:
1179 tree size
= gimple_call_arg (call
, 2);
1180 ao_ref_init_from_ptr_and_size (&dref
,
1181 gimple_call_arg (call
, 0),
1183 return refs_may_alias_p_1 (&dref
, ref
, false);
1185 /* The following builtins do not read from memory. */
1187 case BUILT_IN_MALLOC
:
1188 case BUILT_IN_CALLOC
:
1189 case BUILT_IN_ALLOCA
:
1190 case BUILT_IN_STACK_SAVE
:
1191 case BUILT_IN_STACK_RESTORE
:
1192 case BUILT_IN_MEMSET
:
1193 case BUILT_IN_FREXP
:
1194 case BUILT_IN_FREXPF
:
1195 case BUILT_IN_FREXPL
:
1196 case BUILT_IN_GAMMA_R
:
1197 case BUILT_IN_GAMMAF_R
:
1198 case BUILT_IN_GAMMAL_R
:
1199 case BUILT_IN_LGAMMA_R
:
1200 case BUILT_IN_LGAMMAF_R
:
1201 case BUILT_IN_LGAMMAL_R
:
1203 case BUILT_IN_MODFF
:
1204 case BUILT_IN_MODFL
:
1205 case BUILT_IN_REMQUO
:
1206 case BUILT_IN_REMQUOF
:
1207 case BUILT_IN_REMQUOL
:
1208 case BUILT_IN_SINCOS
:
1209 case BUILT_IN_SINCOSF
:
1210 case BUILT_IN_SINCOSL
:
1212 /* __sync_* builtins and some OpenMP builtins act as threading
1214 #undef DEF_SYNC_BUILTIN
1215 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1216 #include "sync-builtins.def"
1217 #undef DEF_SYNC_BUILTIN
1218 case BUILT_IN_GOMP_ATOMIC_START
:
1219 case BUILT_IN_GOMP_ATOMIC_END
:
1220 case BUILT_IN_GOMP_BARRIER
:
1221 case BUILT_IN_GOMP_TASKWAIT
:
1222 case BUILT_IN_GOMP_CRITICAL_START
:
1223 case BUILT_IN_GOMP_CRITICAL_END
:
1224 case BUILT_IN_GOMP_CRITICAL_NAME_START
:
1225 case BUILT_IN_GOMP_CRITICAL_NAME_END
:
1226 case BUILT_IN_GOMP_LOOP_END
:
1227 case BUILT_IN_GOMP_ORDERED_START
:
1228 case BUILT_IN_GOMP_ORDERED_END
:
1229 case BUILT_IN_GOMP_PARALLEL_END
:
1230 case BUILT_IN_GOMP_SECTIONS_END
:
1231 case BUILT_IN_GOMP_SINGLE_COPY_START
:
1232 case BUILT_IN_GOMP_SINGLE_COPY_END
:
1236 /* Fallthru to general call handling. */;
1239 /* Check if base is a global static variable that is not read
1241 if (callee
!= NULL_TREE
1242 && TREE_CODE (base
) == VAR_DECL
1243 && TREE_STATIC (base
))
1245 struct cgraph_node
*node
= cgraph_get_node (callee
);
1248 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1249 node yet. We should enforce that there are nodes for all decls in the
1250 IL and remove this check instead. */
1252 && (not_read
= ipa_reference_get_not_read_global (node
))
1253 && bitmap_bit_p (not_read
, DECL_UID (base
)))
1257 /* Check if the base variable is call-used. */
1260 if (pt_solution_includes (gimple_call_use_set (call
), base
))
1263 else if ((TREE_CODE (base
) == MEM_REF
1264 || TREE_CODE (base
) == TARGET_MEM_REF
)
1265 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1267 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0));
1271 if (pt_solutions_intersect (gimple_call_use_set (call
), &pi
->pt
))
1277 /* Inspect call arguments for passed-by-value aliases. */
1279 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
1281 tree op
= gimple_call_arg (call
, i
);
1282 int flags
= gimple_call_arg_flags (call
, i
);
1284 if (flags
& EAF_UNUSED
)
1287 if (TREE_CODE (op
) == WITH_SIZE_EXPR
)
1288 op
= TREE_OPERAND (op
, 0);
1290 if (TREE_CODE (op
) != SSA_NAME
1291 && !is_gimple_min_invariant (op
))
1294 ao_ref_init (&r
, op
);
1295 if (refs_may_alias_p_1 (&r
, ref
, true))
1304 ref_maybe_used_by_call_p (gimple call
, tree ref
)
1308 ao_ref_init (&r
, ref
);
1309 res
= ref_maybe_used_by_call_p_1 (call
, &r
);
1311 ++alias_stats
.ref_maybe_used_by_call_p_may_alias
;
1313 ++alias_stats
.ref_maybe_used_by_call_p_no_alias
;
1318 /* If the statement STMT may use the memory reference REF return
1319 true, otherwise return false. */
1322 ref_maybe_used_by_stmt_p (gimple stmt
, tree ref
)
1324 if (is_gimple_assign (stmt
))
1328 /* All memory assign statements are single. */
1329 if (!gimple_assign_single_p (stmt
))
1332 rhs
= gimple_assign_rhs1 (stmt
);
1333 if (is_gimple_reg (rhs
)
1334 || is_gimple_min_invariant (rhs
)
1335 || gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
)
1338 return refs_may_alias_p (rhs
, ref
);
1340 else if (is_gimple_call (stmt
))
1341 return ref_maybe_used_by_call_p (stmt
, ref
);
1342 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
1344 tree retval
= gimple_return_retval (stmt
);
1347 && TREE_CODE (retval
) != SSA_NAME
1348 && !is_gimple_min_invariant (retval
)
1349 && refs_may_alias_p (retval
, ref
))
1351 /* If ref escapes the function then the return acts as a use. */
1352 base
= get_base_address (ref
);
1355 else if (DECL_P (base
))
1356 return is_global_var (base
);
1357 else if (TREE_CODE (base
) == MEM_REF
1358 || TREE_CODE (base
) == TARGET_MEM_REF
)
1359 return ptr_deref_may_alias_global_p (TREE_OPERAND (base
, 0));
1366 /* If the call in statement CALL may clobber the memory reference REF
1367 return true, otherwise return false. */
1370 call_may_clobber_ref_p_1 (gimple call
, ao_ref
*ref
)
1375 /* If the call is pure or const it cannot clobber anything. */
1376 if (gimple_call_flags (call
)
1377 & (ECF_PURE
|ECF_CONST
|ECF_LOOPING_CONST_OR_PURE
|ECF_NOVOPS
))
1380 base
= ao_ref_base (ref
);
1384 if (TREE_CODE (base
) == SSA_NAME
1385 || CONSTANT_CLASS_P (base
))
1388 /* If the reference is based on a decl that is not aliased the call
1389 cannot possibly clobber it. */
1391 && !may_be_aliased (base
)
1392 /* But local non-readonly statics can be modified through recursion
1393 or the call may implement a threading barrier which we must
1394 treat as may-def. */
1395 && (TREE_READONLY (base
)
1396 || !is_global_var (base
)))
1399 callee
= gimple_call_fndecl (call
);
1401 /* Handle those builtin functions explicitly that do not act as
1402 escape points. See tree-ssa-structalias.c:find_func_aliases
1403 for the list of builtins we might need to handle here. */
1404 if (callee
!= NULL_TREE
1405 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
1406 switch (DECL_FUNCTION_CODE (callee
))
1408 /* All the following functions clobber memory pointed to by
1409 their first argument. */
1410 case BUILT_IN_STRCPY
:
1411 case BUILT_IN_STRNCPY
:
1412 case BUILT_IN_MEMCPY
:
1413 case BUILT_IN_MEMMOVE
:
1414 case BUILT_IN_MEMPCPY
:
1415 case BUILT_IN_STPCPY
:
1416 case BUILT_IN_STPNCPY
:
1417 case BUILT_IN_STRCAT
:
1418 case BUILT_IN_STRNCAT
:
1419 case BUILT_IN_MEMSET
:
1422 tree size
= NULL_TREE
;
1423 if (gimple_call_num_args (call
) == 3)
1424 size
= gimple_call_arg (call
, 2);
1425 ao_ref_init_from_ptr_and_size (&dref
,
1426 gimple_call_arg (call
, 0),
1428 return refs_may_alias_p_1 (&dref
, ref
, false);
1430 case BUILT_IN_BCOPY
:
1433 tree size
= gimple_call_arg (call
, 2);
1434 ao_ref_init_from_ptr_and_size (&dref
,
1435 gimple_call_arg (call
, 1),
1437 return refs_may_alias_p_1 (&dref
, ref
, false);
1439 /* Allocating memory does not have any side-effects apart from
1440 being the definition point for the pointer. */
1441 case BUILT_IN_MALLOC
:
1442 case BUILT_IN_CALLOC
:
1443 /* Unix98 specifies that errno is set on allocation failure. */
1445 && targetm
.ref_may_alias_errno (ref
))
1448 case BUILT_IN_STACK_SAVE
:
1449 case BUILT_IN_ALLOCA
:
1451 /* Freeing memory kills the pointed-to memory. More importantly
1452 the call has to serve as a barrier for moving loads and stores
1456 tree ptr
= gimple_call_arg (call
, 0);
1457 return ptr_deref_may_alias_ref_p_1 (ptr
, ref
);
1459 case BUILT_IN_GAMMA_R
:
1460 case BUILT_IN_GAMMAF_R
:
1461 case BUILT_IN_GAMMAL_R
:
1462 case BUILT_IN_LGAMMA_R
:
1463 case BUILT_IN_LGAMMAF_R
:
1464 case BUILT_IN_LGAMMAL_R
:
1466 tree out
= gimple_call_arg (call
, 1);
1467 if (ptr_deref_may_alias_ref_p_1 (out
, ref
))
1469 if (flag_errno_math
)
1473 case BUILT_IN_FREXP
:
1474 case BUILT_IN_FREXPF
:
1475 case BUILT_IN_FREXPL
:
1477 case BUILT_IN_MODFF
:
1478 case BUILT_IN_MODFL
:
1480 tree out
= gimple_call_arg (call
, 1);
1481 return ptr_deref_may_alias_ref_p_1 (out
, ref
);
1483 case BUILT_IN_REMQUO
:
1484 case BUILT_IN_REMQUOF
:
1485 case BUILT_IN_REMQUOL
:
1487 tree out
= gimple_call_arg (call
, 2);
1488 if (ptr_deref_may_alias_ref_p_1 (out
, ref
))
1490 if (flag_errno_math
)
1494 case BUILT_IN_SINCOS
:
1495 case BUILT_IN_SINCOSF
:
1496 case BUILT_IN_SINCOSL
:
1498 tree sin
= gimple_call_arg (call
, 1);
1499 tree cos
= gimple_call_arg (call
, 2);
1500 return (ptr_deref_may_alias_ref_p_1 (sin
, ref
)
1501 || ptr_deref_may_alias_ref_p_1 (cos
, ref
));
1503 /* __sync_* builtins and some OpenMP builtins act as threading
1505 #undef DEF_SYNC_BUILTIN
1506 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1507 #include "sync-builtins.def"
1508 #undef DEF_SYNC_BUILTIN
1509 case BUILT_IN_GOMP_ATOMIC_START
:
1510 case BUILT_IN_GOMP_ATOMIC_END
:
1511 case BUILT_IN_GOMP_BARRIER
:
1512 case BUILT_IN_GOMP_TASKWAIT
:
1513 case BUILT_IN_GOMP_CRITICAL_START
:
1514 case BUILT_IN_GOMP_CRITICAL_END
:
1515 case BUILT_IN_GOMP_CRITICAL_NAME_START
:
1516 case BUILT_IN_GOMP_CRITICAL_NAME_END
:
1517 case BUILT_IN_GOMP_LOOP_END
:
1518 case BUILT_IN_GOMP_ORDERED_START
:
1519 case BUILT_IN_GOMP_ORDERED_END
:
1520 case BUILT_IN_GOMP_PARALLEL_END
:
1521 case BUILT_IN_GOMP_SECTIONS_END
:
1522 case BUILT_IN_GOMP_SINGLE_COPY_START
:
1523 case BUILT_IN_GOMP_SINGLE_COPY_END
:
1526 /* Fallthru to general call handling. */;
1529 /* Check if base is a global static variable that is not written
1531 if (callee
!= NULL_TREE
1532 && TREE_CODE (base
) == VAR_DECL
1533 && TREE_STATIC (base
))
1535 struct cgraph_node
*node
= cgraph_get_node (callee
);
1539 && (not_written
= ipa_reference_get_not_written_global (node
))
1540 && bitmap_bit_p (not_written
, DECL_UID (base
)))
1544 /* Check if the base variable is call-clobbered. */
1546 return pt_solution_includes (gimple_call_clobber_set (call
), base
);
1547 else if ((TREE_CODE (base
) == MEM_REF
1548 || TREE_CODE (base
) == TARGET_MEM_REF
)
1549 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1551 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0));
1555 return pt_solutions_intersect (gimple_call_clobber_set (call
), &pi
->pt
);
1561 /* If the call in statement CALL may clobber the memory reference REF
1562 return true, otherwise return false. */
1565 call_may_clobber_ref_p (gimple call
, tree ref
)
1569 ao_ref_init (&r
, ref
);
1570 res
= call_may_clobber_ref_p_1 (call
, &r
);
1572 ++alias_stats
.call_may_clobber_ref_p_may_alias
;
1574 ++alias_stats
.call_may_clobber_ref_p_no_alias
;
1579 /* If the statement STMT may clobber the memory reference REF return true,
1580 otherwise return false. */
1583 stmt_may_clobber_ref_p_1 (gimple stmt
, ao_ref
*ref
)
1585 if (is_gimple_call (stmt
))
1587 tree lhs
= gimple_call_lhs (stmt
);
1589 && TREE_CODE (lhs
) != SSA_NAME
)
1592 ao_ref_init (&r
, lhs
);
1593 if (refs_may_alias_p_1 (ref
, &r
, true))
1597 return call_may_clobber_ref_p_1 (stmt
, ref
);
1599 else if (gimple_assign_single_p (stmt
))
1601 tree lhs
= gimple_assign_lhs (stmt
);
1602 if (TREE_CODE (lhs
) != SSA_NAME
)
1605 ao_ref_init (&r
, lhs
);
1606 return refs_may_alias_p_1 (ref
, &r
, true);
1609 else if (gimple_code (stmt
) == GIMPLE_ASM
)
1616 stmt_may_clobber_ref_p (gimple stmt
, tree ref
)
1619 ao_ref_init (&r
, ref
);
1620 return stmt_may_clobber_ref_p_1 (stmt
, &r
);
1623 /* If STMT kills the memory reference REF return true, otherwise
1627 stmt_kills_ref_p_1 (gimple stmt
, ao_ref
*ref
)
1629 /* For a must-alias check we need to be able to constrain
1630 the access properly. */
1632 if (ref
->max_size
== -1)
1635 if (gimple_has_lhs (stmt
)
1636 && TREE_CODE (gimple_get_lhs (stmt
)) != SSA_NAME
1637 /* The assignment is not necessarily carried out if it can throw
1638 and we can catch it in the current function where we could inspect
1640 ??? We only need to care about the RHS throwing. For aggregate
1641 assignments or similar calls and non-call exceptions the LHS
1642 might throw as well. */
1643 && !stmt_can_throw_internal (stmt
))
1645 tree base
, lhs
= gimple_get_lhs (stmt
);
1646 HOST_WIDE_INT size
, offset
, max_size
;
1647 base
= get_ref_base_and_extent (lhs
, &offset
, &size
, &max_size
);
1648 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1649 so base == ref->base does not always hold. */
1650 if (base
== ref
->base
)
1652 /* For a must-alias check we need to be able to constrain
1653 the access properly. */
1654 if (size
!= -1 && size
== max_size
)
1656 if (offset
<= ref
->offset
1657 && offset
+ size
>= ref
->offset
+ ref
->max_size
)
1663 if (is_gimple_call (stmt
))
1665 tree callee
= gimple_call_fndecl (stmt
);
1666 if (callee
!= NULL_TREE
1667 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
1668 switch (DECL_FUNCTION_CODE (callee
))
1670 case BUILT_IN_MEMCPY
:
1671 case BUILT_IN_MEMPCPY
:
1672 case BUILT_IN_MEMMOVE
:
1673 case BUILT_IN_MEMSET
:
1675 tree dest
= gimple_call_arg (stmt
, 0);
1676 tree len
= gimple_call_arg (stmt
, 2);
1677 tree base
= NULL_TREE
;
1678 HOST_WIDE_INT offset
= 0;
1679 if (!host_integerp (len
, 0))
1681 if (TREE_CODE (dest
) == ADDR_EXPR
)
1682 base
= get_addr_base_and_unit_offset (TREE_OPERAND (dest
, 0),
1684 else if (TREE_CODE (dest
) == SSA_NAME
)
1687 && base
== ao_ref_base (ref
))
1689 HOST_WIDE_INT size
= TREE_INT_CST_LOW (len
);
1690 if (offset
<= ref
->offset
/ BITS_PER_UNIT
1692 >= ((ref
->offset
+ ref
->max_size
+ BITS_PER_UNIT
- 1)
1705 stmt_kills_ref_p (gimple stmt
, tree ref
)
1708 ao_ref_init (&r
, ref
);
1709 return stmt_kills_ref_p_1 (stmt
, &r
);
1713 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1714 TARGET or a statement clobbering the memory reference REF in which
1715 case false is returned. The walk starts with VUSE, one argument of PHI. */
1718 maybe_skip_until (gimple phi
, tree target
, ao_ref
*ref
,
1719 tree vuse
, bitmap
*visited
)
1722 *visited
= BITMAP_ALLOC (NULL
);
1724 bitmap_set_bit (*visited
, SSA_NAME_VERSION (PHI_RESULT (phi
)));
1726 /* Walk until we hit the target. */
1727 while (vuse
!= target
)
1729 gimple def_stmt
= SSA_NAME_DEF_STMT (vuse
);
1730 /* Recurse for PHI nodes. */
1731 if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1733 /* An already visited PHI node ends the walk successfully. */
1734 if (bitmap_bit_p (*visited
, SSA_NAME_VERSION (PHI_RESULT (def_stmt
))))
1736 vuse
= get_continuation_for_phi (def_stmt
, ref
, visited
);
1741 /* A clobbering statement or the end of the IL ends it failing. */
1742 else if (gimple_nop_p (def_stmt
)
1743 || stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
1745 vuse
= gimple_vuse (def_stmt
);
1750 /* Starting from a PHI node for the virtual operand of the memory reference
1751 REF find a continuation virtual operand that allows to continue walking
1752 statements dominating PHI skipping only statements that cannot possibly
1753 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1757 get_continuation_for_phi (gimple phi
, ao_ref
*ref
, bitmap
*visited
)
1759 unsigned nargs
= gimple_phi_num_args (phi
);
1761 /* Through a single-argument PHI we can simply look through. */
1763 return PHI_ARG_DEF (phi
, 0);
1765 /* For two arguments try to skip non-aliasing code until we hit
1766 the phi argument definition that dominates the other one. */
1769 tree arg0
= PHI_ARG_DEF (phi
, 0);
1770 tree arg1
= PHI_ARG_DEF (phi
, 1);
1771 gimple def0
= SSA_NAME_DEF_STMT (arg0
);
1772 gimple def1
= SSA_NAME_DEF_STMT (arg1
);
1777 else if (gimple_nop_p (def0
)
1778 || (!gimple_nop_p (def1
)
1779 && dominated_by_p (CDI_DOMINATORS
,
1780 gimple_bb (def1
), gimple_bb (def0
))))
1782 if (maybe_skip_until (phi
, arg0
, ref
, arg1
, visited
))
1785 else if (gimple_nop_p (def1
)
1786 || dominated_by_p (CDI_DOMINATORS
,
1787 gimple_bb (def0
), gimple_bb (def1
)))
1789 if (maybe_skip_until (phi
, arg1
, ref
, arg0
, visited
))
1792 /* Special case of a diamond:
1794 goto (cond) ? L1 : L2
1795 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1797 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1798 L3: MEM_4 = PHI<MEM_2, MEM_3>
1799 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1800 dominate each other, but still we can easily skip this PHI node
1801 if we recognize that the vuse MEM operand is the same for both,
1802 and that we can skip both statements (they don't clobber us).
1803 This is still linear. Don't use maybe_skip_until, that might
1804 potentially be slow. */
1805 else if ((common_vuse
= gimple_vuse (def0
))
1806 && common_vuse
== gimple_vuse (def1
))
1808 if (!stmt_may_clobber_ref_p_1 (def0
, ref
)
1809 && !stmt_may_clobber_ref_p_1 (def1
, ref
))
1817 /* Based on the memory reference REF and its virtual use VUSE call
1818 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1819 itself. That is, for each virtual use for which its defining statement
1820 does not clobber REF.
1822 WALKER is called with REF, the current virtual use and DATA. If
1823 WALKER returns non-NULL the walk stops and its result is returned.
1824 At the end of a non-successful walk NULL is returned.
1826 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1827 use which definition is a statement that may clobber REF and DATA.
1828 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1829 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1830 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1831 to adjust REF and *DATA to make that valid.
1833 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1836 walk_non_aliased_vuses (ao_ref
*ref
, tree vuse
,
1837 void *(*walker
)(ao_ref
*, tree
, void *),
1838 void *(*translate
)(ao_ref
*, tree
, void *), void *data
)
1840 bitmap visited
= NULL
;
1843 timevar_push (TV_ALIAS_STMT_WALK
);
1849 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1850 res
= (*walker
) (ref
, vuse
, data
);
1854 def_stmt
= SSA_NAME_DEF_STMT (vuse
);
1855 if (gimple_nop_p (def_stmt
))
1857 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1858 vuse
= get_continuation_for_phi (def_stmt
, ref
, &visited
);
1861 if (stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
1865 res
= (*translate
) (ref
, vuse
, data
);
1866 /* Failed lookup and translation. */
1867 if (res
== (void *)-1)
1872 /* Lookup succeeded. */
1873 else if (res
!= NULL
)
1875 /* Translation succeeded, continue walking. */
1877 vuse
= gimple_vuse (def_stmt
);
1883 BITMAP_FREE (visited
);
1885 timevar_pop (TV_ALIAS_STMT_WALK
);
1891 /* Based on the memory reference REF call WALKER for each vdef which
1892 defining statement may clobber REF, starting with VDEF. If REF
1893 is NULL_TREE, each defining statement is visited.
1895 WALKER is called with REF, the current vdef and DATA. If WALKER
1896 returns true the walk is stopped, otherwise it continues.
1898 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1899 PHI argument (but only one walk continues on merge points), the
1900 return value is true if any of the walks was successful.
1902 The function returns the number of statements walked. */
1905 walk_aliased_vdefs_1 (ao_ref
*ref
, tree vdef
,
1906 bool (*walker
)(ao_ref
*, tree
, void *), void *data
,
1907 bitmap
*visited
, unsigned int cnt
)
1911 gimple def_stmt
= SSA_NAME_DEF_STMT (vdef
);
1914 && !bitmap_set_bit (*visited
, SSA_NAME_VERSION (vdef
)))
1917 if (gimple_nop_p (def_stmt
))
1919 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
1923 *visited
= BITMAP_ALLOC (NULL
);
1924 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); ++i
)
1925 cnt
+= walk_aliased_vdefs_1 (ref
, gimple_phi_arg_def (def_stmt
, i
),
1926 walker
, data
, visited
, 0);
1930 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1933 || stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
1934 && (*walker
) (ref
, vdef
, data
))
1937 vdef
= gimple_vuse (def_stmt
);
1943 walk_aliased_vdefs (ao_ref
*ref
, tree vdef
,
1944 bool (*walker
)(ao_ref
*, tree
, void *), void *data
,
1947 bitmap local_visited
= NULL
;
1950 timevar_push (TV_ALIAS_STMT_WALK
);
1952 ret
= walk_aliased_vdefs_1 (ref
, vdef
, walker
, data
,
1953 visited
? visited
: &local_visited
, 0);
1955 BITMAP_FREE (local_visited
);
1957 timevar_pop (TV_ALIAS_STMT_WALK
);