1 /* Alias analysis for trees.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
33 #include "hard-reg-set.h"
36 #include "dominance.h"
37 #include "basic-block.h"
38 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
39 #include "langhooks.h"
41 #include "tree-pretty-print.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
46 #include "gimple-expr.h"
49 #include "gimple-ssa.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
54 #include "tree-inline.h"
56 #include "alloc-pool.h"
57 #include "tree-ssa-alias.h"
60 #include "plugin-api.h"
63 #include "ipa-reference.h"
65 /* Broad overview of how alias analysis on gimple works:
67 Statements clobbering or using memory are linked through the
68 virtual operand factored use-def chain. The virtual operand
69 is unique per function, its symbol is accessible via gimple_vop (cfun).
70 Virtual operands are used for efficiently walking memory statements
71 in the gimple IL and are useful for things like value-numbering as
72 a generation count for memory references.
74 SSA_NAME pointers may have associated points-to information
75 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
76 points-to information is (re-)computed by the TODO_rebuild_alias
77 pass manager todo. Points-to information is also used for more
78 precise tracking of call-clobbered and call-used variables and
79 related disambiguations.
81 This file contains functions for disambiguating memory references,
82 the so called alias-oracle and tools for walking of the gimple IL.
84 The main alias-oracle entry-points are
86 bool stmt_may_clobber_ref_p (gimple, tree)
88 This function queries if a statement may invalidate (parts of)
89 the memory designated by the reference tree argument.
91 bool ref_maybe_used_by_stmt_p (gimple, tree)
93 This function queries if a statement may need (parts of) the
94 memory designated by the reference tree argument.
96 There are variants of these functions that only handle the call
97 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
98 Note that these do not disambiguate against a possible call lhs.
100 bool refs_may_alias_p (tree, tree)
102 This function tries to disambiguate two reference trees.
104 bool ptr_deref_may_alias_global_p (tree)
106 This function queries if dereferencing a pointer variable may
109 More low-level disambiguators are available and documented in
110 this file. Low-level disambiguators dealing with points-to
111 information are in tree-ssa-structalias.c. */
114 /* Query statistics for the different low-level disambiguators.
115 A high-level query may trigger multiple of them. */
118 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias
;
119 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias
;
120 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias
;
121 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias
;
122 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias
;
123 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias
;
127 dump_alias_stats (FILE *s
)
129 fprintf (s
, "\nAlias oracle query stats:\n");
130 fprintf (s
, " refs_may_alias_p: "
131 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
132 HOST_WIDE_INT_PRINT_DEC
" queries\n",
133 alias_stats
.refs_may_alias_p_no_alias
,
134 alias_stats
.refs_may_alias_p_no_alias
135 + alias_stats
.refs_may_alias_p_may_alias
);
136 fprintf (s
, " ref_maybe_used_by_call_p: "
137 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
138 HOST_WIDE_INT_PRINT_DEC
" queries\n",
139 alias_stats
.ref_maybe_used_by_call_p_no_alias
,
140 alias_stats
.refs_may_alias_p_no_alias
141 + alias_stats
.ref_maybe_used_by_call_p_may_alias
);
142 fprintf (s
, " call_may_clobber_ref_p: "
143 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
144 HOST_WIDE_INT_PRINT_DEC
" queries\n",
145 alias_stats
.call_may_clobber_ref_p_no_alias
,
146 alias_stats
.call_may_clobber_ref_p_no_alias
147 + alias_stats
.call_may_clobber_ref_p_may_alias
);
151 /* Return true, if dereferencing PTR may alias with a global variable. */
154 ptr_deref_may_alias_global_p (tree ptr
)
156 struct ptr_info_def
*pi
;
158 /* If we end up with a pointer constant here that may point
160 if (TREE_CODE (ptr
) != SSA_NAME
)
163 pi
= SSA_NAME_PTR_INFO (ptr
);
165 /* If we do not have points-to information for this variable,
170 /* ??? This does not use TBAA to prune globals ptr may not access. */
171 return pt_solution_includes_global (&pi
->pt
);
174 /* Return true if dereferencing PTR may alias DECL.
175 The caller is responsible for applying TBAA to see if PTR
176 may access DECL at all. */
179 ptr_deref_may_alias_decl_p (tree ptr
, tree decl
)
181 struct ptr_info_def
*pi
;
183 /* Conversions are irrelevant for points-to information and
184 data-dependence analysis can feed us those. */
187 /* Anything we do not explicilty handle aliases. */
188 if ((TREE_CODE (ptr
) != SSA_NAME
189 && TREE_CODE (ptr
) != ADDR_EXPR
190 && TREE_CODE (ptr
) != POINTER_PLUS_EXPR
)
191 || !POINTER_TYPE_P (TREE_TYPE (ptr
))
192 || (TREE_CODE (decl
) != VAR_DECL
193 && TREE_CODE (decl
) != PARM_DECL
194 && TREE_CODE (decl
) != RESULT_DECL
))
197 /* Disregard pointer offsetting. */
198 if (TREE_CODE (ptr
) == POINTER_PLUS_EXPR
)
202 ptr
= TREE_OPERAND (ptr
, 0);
204 while (TREE_CODE (ptr
) == POINTER_PLUS_EXPR
);
205 return ptr_deref_may_alias_decl_p (ptr
, decl
);
208 /* ADDR_EXPR pointers either just offset another pointer or directly
209 specify the pointed-to set. */
210 if (TREE_CODE (ptr
) == ADDR_EXPR
)
212 tree base
= get_base_address (TREE_OPERAND (ptr
, 0));
214 && (TREE_CODE (base
) == MEM_REF
215 || TREE_CODE (base
) == TARGET_MEM_REF
))
216 ptr
= TREE_OPERAND (base
, 0);
221 && CONSTANT_CLASS_P (base
))
227 /* Non-aliased variables can not be pointed to. */
228 if (!may_be_aliased (decl
))
231 /* If we do not have useful points-to information for this pointer
232 we cannot disambiguate anything else. */
233 pi
= SSA_NAME_PTR_INFO (ptr
);
237 return pt_solution_includes (&pi
->pt
, decl
);
240 /* Return true if dereferenced PTR1 and PTR2 may alias.
241 The caller is responsible for applying TBAA to see if accesses
242 through PTR1 and PTR2 may conflict at all. */
245 ptr_derefs_may_alias_p (tree ptr1
, tree ptr2
)
247 struct ptr_info_def
*pi1
, *pi2
;
249 /* Conversions are irrelevant for points-to information and
250 data-dependence analysis can feed us those. */
254 /* Disregard pointer offsetting. */
255 if (TREE_CODE (ptr1
) == POINTER_PLUS_EXPR
)
259 ptr1
= TREE_OPERAND (ptr1
, 0);
261 while (TREE_CODE (ptr1
) == POINTER_PLUS_EXPR
);
262 return ptr_derefs_may_alias_p (ptr1
, ptr2
);
264 if (TREE_CODE (ptr2
) == POINTER_PLUS_EXPR
)
268 ptr2
= TREE_OPERAND (ptr2
, 0);
270 while (TREE_CODE (ptr2
) == POINTER_PLUS_EXPR
);
271 return ptr_derefs_may_alias_p (ptr1
, ptr2
);
274 /* ADDR_EXPR pointers either just offset another pointer or directly
275 specify the pointed-to set. */
276 if (TREE_CODE (ptr1
) == ADDR_EXPR
)
278 tree base
= get_base_address (TREE_OPERAND (ptr1
, 0));
280 && (TREE_CODE (base
) == MEM_REF
281 || TREE_CODE (base
) == TARGET_MEM_REF
))
282 return ptr_derefs_may_alias_p (TREE_OPERAND (base
, 0), ptr2
);
285 return ptr_deref_may_alias_decl_p (ptr2
, base
);
289 if (TREE_CODE (ptr2
) == ADDR_EXPR
)
291 tree base
= get_base_address (TREE_OPERAND (ptr2
, 0));
293 && (TREE_CODE (base
) == MEM_REF
294 || TREE_CODE (base
) == TARGET_MEM_REF
))
295 return ptr_derefs_may_alias_p (ptr1
, TREE_OPERAND (base
, 0));
298 return ptr_deref_may_alias_decl_p (ptr1
, base
);
303 /* From here we require SSA name pointers. Anything else aliases. */
304 if (TREE_CODE (ptr1
) != SSA_NAME
305 || TREE_CODE (ptr2
) != SSA_NAME
306 || !POINTER_TYPE_P (TREE_TYPE (ptr1
))
307 || !POINTER_TYPE_P (TREE_TYPE (ptr2
)))
310 /* We may end up with two empty points-to solutions for two same pointers.
311 In this case we still want to say both pointers alias, so shortcut
316 /* If we do not have useful points-to information for either pointer
317 we cannot disambiguate anything else. */
318 pi1
= SSA_NAME_PTR_INFO (ptr1
);
319 pi2
= SSA_NAME_PTR_INFO (ptr2
);
323 /* ??? This does not use TBAA to prune decls from the intersection
324 that not both pointers may access. */
325 return pt_solutions_intersect (&pi1
->pt
, &pi2
->pt
);
328 /* Return true if dereferencing PTR may alias *REF.
329 The caller is responsible for applying TBAA to see if PTR
330 may access *REF at all. */
333 ptr_deref_may_alias_ref_p_1 (tree ptr
, ao_ref
*ref
)
335 tree base
= ao_ref_base (ref
);
337 if (TREE_CODE (base
) == MEM_REF
338 || TREE_CODE (base
) == TARGET_MEM_REF
)
339 return ptr_derefs_may_alias_p (ptr
, TREE_OPERAND (base
, 0));
340 else if (DECL_P (base
))
341 return ptr_deref_may_alias_decl_p (ptr
, base
);
346 /* Returns whether reference REF to BASE may refer to global memory. */
349 ref_may_alias_global_p_1 (tree base
)
352 return is_global_var (base
);
353 else if (TREE_CODE (base
) == MEM_REF
354 || TREE_CODE (base
) == TARGET_MEM_REF
)
355 return ptr_deref_may_alias_global_p (TREE_OPERAND (base
, 0));
360 ref_may_alias_global_p (ao_ref
*ref
)
362 tree base
= ao_ref_base (ref
);
363 return ref_may_alias_global_p_1 (base
);
367 ref_may_alias_global_p (tree ref
)
369 tree base
= get_base_address (ref
);
370 return ref_may_alias_global_p_1 (base
);
373 /* Return true whether STMT may clobber global memory. */
376 stmt_may_clobber_global_p (gimple stmt
)
380 if (!gimple_vdef (stmt
))
383 /* ??? We can ask the oracle whether an artificial pointer
384 dereference with a pointer with points-to information covering
385 all global memory (what about non-address taken memory?) maybe
386 clobbered by this call. As there is at the moment no convenient
387 way of doing that without generating garbage do some manual
389 ??? We could make a NULL ao_ref argument to the various
390 predicates special, meaning any global memory. */
392 switch (gimple_code (stmt
))
395 lhs
= gimple_assign_lhs (stmt
);
396 return (TREE_CODE (lhs
) != SSA_NAME
397 && ref_may_alias_global_p (lhs
));
406 /* Dump alias information on FILE. */
409 dump_alias_info (FILE *file
)
413 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
416 fprintf (file
, "\n\nAlias information for %s\n\n", funcname
);
418 fprintf (file
, "Aliased symbols\n\n");
420 FOR_EACH_LOCAL_DECL (cfun
, i
, var
)
422 if (may_be_aliased (var
))
423 dump_variable (file
, var
);
426 fprintf (file
, "\nCall clobber information\n");
428 fprintf (file
, "\nESCAPED");
429 dump_points_to_solution (file
, &cfun
->gimple_df
->escaped
);
431 fprintf (file
, "\n\nFlow-insensitive points-to information\n\n");
433 for (i
= 1; i
< num_ssa_names
; i
++)
435 tree ptr
= ssa_name (i
);
436 struct ptr_info_def
*pi
;
439 || !POINTER_TYPE_P (TREE_TYPE (ptr
))
440 || SSA_NAME_IN_FREE_LIST (ptr
))
443 pi
= SSA_NAME_PTR_INFO (ptr
);
445 dump_points_to_info_for (file
, ptr
);
448 fprintf (file
, "\n");
452 /* Dump alias information on stderr. */
455 debug_alias_info (void)
457 dump_alias_info (stderr
);
461 /* Dump the points-to set *PT into FILE. */
464 dump_points_to_solution (FILE *file
, struct pt_solution
*pt
)
467 fprintf (file
, ", points-to anything");
470 fprintf (file
, ", points-to non-local");
473 fprintf (file
, ", points-to escaped");
476 fprintf (file
, ", points-to unit escaped");
479 fprintf (file
, ", points-to NULL");
483 fprintf (file
, ", points-to vars: ");
484 dump_decl_set (file
, pt
->vars
);
485 if (pt
->vars_contains_nonlocal
486 && pt
->vars_contains_escaped_heap
)
487 fprintf (file
, " (nonlocal, escaped heap)");
488 else if (pt
->vars_contains_nonlocal
489 && pt
->vars_contains_escaped
)
490 fprintf (file
, " (nonlocal, escaped)");
491 else if (pt
->vars_contains_nonlocal
)
492 fprintf (file
, " (nonlocal)");
493 else if (pt
->vars_contains_escaped_heap
)
494 fprintf (file
, " (escaped heap)");
495 else if (pt
->vars_contains_escaped
)
496 fprintf (file
, " (escaped)");
501 /* Unified dump function for pt_solution. */
504 debug (pt_solution
&ref
)
506 dump_points_to_solution (stderr
, &ref
);
510 debug (pt_solution
*ptr
)
515 fprintf (stderr
, "<nil>\n");
519 /* Dump points-to information for SSA_NAME PTR into FILE. */
522 dump_points_to_info_for (FILE *file
, tree ptr
)
524 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (ptr
);
526 print_generic_expr (file
, ptr
, dump_flags
);
529 dump_points_to_solution (file
, &pi
->pt
);
531 fprintf (file
, ", points-to anything");
533 fprintf (file
, "\n");
537 /* Dump points-to information for VAR into stderr. */
540 debug_points_to_info_for (tree var
)
542 dump_points_to_info_for (stderr
, var
);
546 /* Initializes the alias-oracle reference representation *R from REF. */
549 ao_ref_init (ao_ref
*r
, tree ref
)
556 r
->ref_alias_set
= -1;
557 r
->base_alias_set
= -1;
558 r
->volatile_p
= ref
? TREE_THIS_VOLATILE (ref
) : false;
561 /* Returns the base object of the memory reference *REF. */
564 ao_ref_base (ao_ref
*ref
)
568 ref
->base
= get_ref_base_and_extent (ref
->ref
, &ref
->offset
, &ref
->size
,
573 /* Returns the base object alias set of the memory reference *REF. */
576 ao_ref_base_alias_set (ao_ref
*ref
)
579 if (ref
->base_alias_set
!= -1)
580 return ref
->base_alias_set
;
584 while (handled_component_p (base_ref
))
585 base_ref
= TREE_OPERAND (base_ref
, 0);
586 ref
->base_alias_set
= get_alias_set (base_ref
);
587 return ref
->base_alias_set
;
590 /* Returns the reference alias set of the memory reference *REF. */
593 ao_ref_alias_set (ao_ref
*ref
)
595 if (ref
->ref_alias_set
!= -1)
596 return ref
->ref_alias_set
;
597 ref
->ref_alias_set
= get_alias_set (ref
->ref
);
598 return ref
->ref_alias_set
;
601 /* Init an alias-oracle reference representation from a gimple pointer
602 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
603 size is assumed to be unknown. The access is assumed to be only
604 to or after of the pointer target, not before it. */
607 ao_ref_init_from_ptr_and_size (ao_ref
*ref
, tree ptr
, tree size
)
609 HOST_WIDE_INT t
, size_hwi
, extra_offset
= 0;
610 ref
->ref
= NULL_TREE
;
611 if (TREE_CODE (ptr
) == SSA_NAME
)
613 gimple stmt
= SSA_NAME_DEF_STMT (ptr
);
614 if (gimple_assign_single_p (stmt
)
615 && gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
616 ptr
= gimple_assign_rhs1 (stmt
);
617 else if (is_gimple_assign (stmt
)
618 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
619 && TREE_CODE (gimple_assign_rhs2 (stmt
)) == INTEGER_CST
)
621 ptr
= gimple_assign_rhs1 (stmt
);
622 extra_offset
= BITS_PER_UNIT
623 * int_cst_value (gimple_assign_rhs2 (stmt
));
627 if (TREE_CODE (ptr
) == ADDR_EXPR
)
629 ref
->base
= get_addr_base_and_unit_offset (TREE_OPERAND (ptr
, 0), &t
);
631 ref
->offset
= BITS_PER_UNIT
* t
;
636 ref
->base
= get_base_address (TREE_OPERAND (ptr
, 0));
641 ref
->base
= build2 (MEM_REF
, char_type_node
,
642 ptr
, null_pointer_node
);
645 ref
->offset
+= extra_offset
;
647 && tree_fits_shwi_p (size
)
648 && (size_hwi
= tree_to_shwi (size
)) <= HOST_WIDE_INT_MAX
/ BITS_PER_UNIT
)
649 ref
->max_size
= ref
->size
= size_hwi
* BITS_PER_UNIT
;
651 ref
->max_size
= ref
->size
= -1;
652 ref
->ref_alias_set
= 0;
653 ref
->base_alias_set
= 0;
654 ref
->volatile_p
= false;
657 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
658 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
662 same_type_for_tbaa (tree type1
, tree type2
)
664 type1
= TYPE_MAIN_VARIANT (type1
);
665 type2
= TYPE_MAIN_VARIANT (type2
);
667 /* If we would have to do structural comparison bail out. */
668 if (TYPE_STRUCTURAL_EQUALITY_P (type1
)
669 || TYPE_STRUCTURAL_EQUALITY_P (type2
))
672 /* Compare the canonical types. */
673 if (TYPE_CANONICAL (type1
) == TYPE_CANONICAL (type2
))
676 /* ??? Array types are not properly unified in all cases as we have
677 spurious changes in the index types for example. Removing this
678 causes all sorts of problems with the Fortran frontend. */
679 if (TREE_CODE (type1
) == ARRAY_TYPE
680 && TREE_CODE (type2
) == ARRAY_TYPE
)
683 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
684 object of one of its constrained subtypes, e.g. when a function with an
685 unconstrained parameter passed by reference is called on an object and
686 inlined. But, even in the case of a fixed size, type and subtypes are
687 not equivalent enough as to share the same TYPE_CANONICAL, since this
688 would mean that conversions between them are useless, whereas they are
689 not (e.g. type and subtypes can have different modes). So, in the end,
690 they are only guaranteed to have the same alias set. */
691 if (get_alias_set (type1
) == get_alias_set (type2
))
694 /* The types are known to be not equal. */
698 /* Determine if the two component references REF1 and REF2 which are
699 based on access types TYPE1 and TYPE2 and of which at least one is based
700 on an indirect reference may alias. REF2 is the only one that can
701 be a decl in which case REF2_IS_DECL is true.
702 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
703 are the respective alias sets. */
706 aliasing_component_refs_p (tree ref1
,
707 alias_set_type ref1_alias_set
,
708 alias_set_type base1_alias_set
,
709 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
711 alias_set_type ref2_alias_set
,
712 alias_set_type base2_alias_set
,
713 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
716 /* If one reference is a component references through pointers try to find a
717 common base and apply offset based disambiguation. This handles
719 struct A { int i; int j; } *q;
720 struct B { struct A a; int k; } *p;
721 disambiguating q->i and p->a.j. */
727 /* Choose bases and base types to search for. */
729 while (handled_component_p (base1
))
730 base1
= TREE_OPERAND (base1
, 0);
731 type1
= TREE_TYPE (base1
);
733 while (handled_component_p (base2
))
734 base2
= TREE_OPERAND (base2
, 0);
735 type2
= TREE_TYPE (base2
);
737 /* Now search for the type1 in the access path of ref2. This
738 would be a common base for doing offset based disambiguation on. */
740 while (handled_component_p (*refp
)
741 && same_type_for_tbaa (TREE_TYPE (*refp
), type1
) == 0)
742 refp
= &TREE_OPERAND (*refp
, 0);
743 same_p
= same_type_for_tbaa (TREE_TYPE (*refp
), type1
);
744 /* If we couldn't compare types we have to bail out. */
747 else if (same_p
== 1)
749 HOST_WIDE_INT offadj
, sztmp
, msztmp
;
750 get_ref_base_and_extent (*refp
, &offadj
, &sztmp
, &msztmp
);
752 get_ref_base_and_extent (base1
, &offadj
, &sztmp
, &msztmp
);
754 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
756 /* If we didn't find a common base, try the other way around. */
758 while (handled_component_p (*refp
)
759 && same_type_for_tbaa (TREE_TYPE (*refp
), type2
) == 0)
760 refp
= &TREE_OPERAND (*refp
, 0);
761 same_p
= same_type_for_tbaa (TREE_TYPE (*refp
), type2
);
762 /* If we couldn't compare types we have to bail out. */
765 else if (same_p
== 1)
767 HOST_WIDE_INT offadj
, sztmp
, msztmp
;
768 get_ref_base_and_extent (*refp
, &offadj
, &sztmp
, &msztmp
);
770 get_ref_base_and_extent (base2
, &offadj
, &sztmp
, &msztmp
);
772 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
775 /* If we have two type access paths B1.path1 and B2.path2 they may
776 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
777 But we can still have a path that goes B1.path1...B2.path2 with
778 a part that we do not see. So we can only disambiguate now
779 if there is no B2 in the tail of path1 and no B1 on the
781 if (base1_alias_set
== ref2_alias_set
782 || alias_set_subset_of (base1_alias_set
, ref2_alias_set
))
784 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
786 return (base2_alias_set
== ref1_alias_set
787 || alias_set_subset_of (base2_alias_set
, ref1_alias_set
));
791 /* Return true if we can determine that component references REF1 and REF2,
792 that are within a common DECL, cannot overlap. */
795 nonoverlapping_component_refs_of_decl_p (tree ref1
, tree ref2
)
797 auto_vec
<tree
, 16> component_refs1
;
798 auto_vec
<tree
, 16> component_refs2
;
800 /* Create the stack of handled components for REF1. */
801 while (handled_component_p (ref1
))
803 component_refs1
.safe_push (ref1
);
804 ref1
= TREE_OPERAND (ref1
, 0);
806 if (TREE_CODE (ref1
) == MEM_REF
)
808 if (!integer_zerop (TREE_OPERAND (ref1
, 1)))
810 ref1
= TREE_OPERAND (TREE_OPERAND (ref1
, 0), 0);
813 /* Create the stack of handled components for REF2. */
814 while (handled_component_p (ref2
))
816 component_refs2
.safe_push (ref2
);
817 ref2
= TREE_OPERAND (ref2
, 0);
819 if (TREE_CODE (ref2
) == MEM_REF
)
821 if (!integer_zerop (TREE_OPERAND (ref2
, 1)))
823 ref2
= TREE_OPERAND (TREE_OPERAND (ref2
, 0), 0);
826 /* We must have the same base DECL. */
827 gcc_assert (ref1
== ref2
);
829 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
830 rank. This is sufficient because we start from the same DECL and you
831 cannot reference several fields at a time with COMPONENT_REFs (unlike
832 with ARRAY_RANGE_REFs for arrays) so you always need the same number
833 of them to access a sub-component, unless you're in a union, in which
834 case the return value will precisely be false. */
839 if (component_refs1
.is_empty ())
841 ref1
= component_refs1
.pop ();
843 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1
, 0))));
847 if (component_refs2
.is_empty ())
849 ref2
= component_refs2
.pop ();
851 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2
, 0))));
853 /* Beware of BIT_FIELD_REF. */
854 if (TREE_CODE (ref1
) != COMPONENT_REF
855 || TREE_CODE (ref2
) != COMPONENT_REF
)
858 tree field1
= TREE_OPERAND (ref1
, 1);
859 tree field2
= TREE_OPERAND (ref2
, 1);
861 /* ??? We cannot simply use the type of operand #0 of the refs here
862 as the Fortran compiler smuggles type punning into COMPONENT_REFs
863 for common blocks instead of using unions like everyone else. */
864 tree type1
= DECL_CONTEXT (field1
);
865 tree type2
= DECL_CONTEXT (field2
);
867 /* We cannot disambiguate fields in a union or qualified union. */
868 if (type1
!= type2
|| TREE_CODE (type1
) != RECORD_TYPE
)
871 /* Different fields of the same record type cannot overlap.
872 ??? Bitfields can overlap at RTL level so punt on them. */
873 if (field1
!= field2
)
875 component_refs1
.release ();
876 component_refs2
.release ();
877 return !(DECL_BIT_FIELD (field1
) && DECL_BIT_FIELD (field2
));
882 component_refs1
.release ();
883 component_refs2
.release ();
887 /* qsort compare function to sort FIELD_DECLs after their
888 DECL_FIELD_CONTEXT TYPE_UID. */
891 ncr_compar (const void *field1_
, const void *field2_
)
893 const_tree field1
= *(const_tree
*) const_cast <void *>(field1_
);
894 const_tree field2
= *(const_tree
*) const_cast <void *>(field2_
);
895 unsigned int uid1
= TYPE_UID (DECL_FIELD_CONTEXT (field1
));
896 unsigned int uid2
= TYPE_UID (DECL_FIELD_CONTEXT (field2
));
899 else if (uid1
> uid2
)
904 /* Return true if we can determine that the fields referenced cannot
905 overlap for any pair of objects. */
908 nonoverlapping_component_refs_p (const_tree x
, const_tree y
)
910 if (!flag_strict_aliasing
912 || TREE_CODE (x
) != COMPONENT_REF
913 || TREE_CODE (y
) != COMPONENT_REF
)
916 auto_vec
<const_tree
, 16> fieldsx
;
917 while (TREE_CODE (x
) == COMPONENT_REF
)
919 tree field
= TREE_OPERAND (x
, 1);
920 tree type
= DECL_FIELD_CONTEXT (field
);
921 if (TREE_CODE (type
) == RECORD_TYPE
)
922 fieldsx
.safe_push (field
);
923 x
= TREE_OPERAND (x
, 0);
925 if (fieldsx
.length () == 0)
927 auto_vec
<const_tree
, 16> fieldsy
;
928 while (TREE_CODE (y
) == COMPONENT_REF
)
930 tree field
= TREE_OPERAND (y
, 1);
931 tree type
= DECL_FIELD_CONTEXT (field
);
932 if (TREE_CODE (type
) == RECORD_TYPE
)
933 fieldsy
.safe_push (TREE_OPERAND (y
, 1));
934 y
= TREE_OPERAND (y
, 0);
936 if (fieldsy
.length () == 0)
939 /* Most common case first. */
940 if (fieldsx
.length () == 1
941 && fieldsy
.length () == 1)
942 return ((DECL_FIELD_CONTEXT (fieldsx
[0])
943 == DECL_FIELD_CONTEXT (fieldsy
[0]))
944 && fieldsx
[0] != fieldsy
[0]
945 && !(DECL_BIT_FIELD (fieldsx
[0]) && DECL_BIT_FIELD (fieldsy
[0])));
947 if (fieldsx
.length () == 2)
949 if (ncr_compar (&fieldsx
[0], &fieldsx
[1]) == 1)
951 const_tree tem
= fieldsx
[0];
952 fieldsx
[0] = fieldsx
[1];
957 fieldsx
.qsort (ncr_compar
);
959 if (fieldsy
.length () == 2)
961 if (ncr_compar (&fieldsy
[0], &fieldsy
[1]) == 1)
963 const_tree tem
= fieldsy
[0];
964 fieldsy
[0] = fieldsy
[1];
969 fieldsy
.qsort (ncr_compar
);
971 unsigned i
= 0, j
= 0;
974 const_tree fieldx
= fieldsx
[i
];
975 const_tree fieldy
= fieldsy
[j
];
976 tree typex
= DECL_FIELD_CONTEXT (fieldx
);
977 tree typey
= DECL_FIELD_CONTEXT (fieldy
);
980 /* We're left with accessing different fields of a structure,
981 no possible overlap, unless they are both bitfields. */
982 if (fieldx
!= fieldy
)
983 return !(DECL_BIT_FIELD (fieldx
) && DECL_BIT_FIELD (fieldy
));
985 if (TYPE_UID (typex
) < TYPE_UID (typey
))
988 if (i
== fieldsx
.length ())
994 if (j
== fieldsy
.length ())
1004 /* Return true if two memory references based on the variables BASE1
1005 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1006 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1007 if non-NULL are the complete memory reference trees. */
1010 decl_refs_may_alias_p (tree ref1
, tree base1
,
1011 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
1012 tree ref2
, tree base2
,
1013 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
)
1015 gcc_checking_assert (DECL_P (base1
) && DECL_P (base2
));
1017 /* If both references are based on different variables, they cannot alias. */
1021 /* If both references are based on the same variable, they cannot alias if
1022 the accesses do not overlap. */
1023 if (!ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
))
1026 /* For components with variable position, the above test isn't sufficient,
1027 so we disambiguate component references manually. */
1029 && handled_component_p (ref1
) && handled_component_p (ref2
)
1030 && nonoverlapping_component_refs_of_decl_p (ref1
, ref2
))
1036 /* Return true if an indirect reference based on *PTR1 constrained
1037 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1038 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1039 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1040 in which case they are computed on-demand. REF1 and REF2
1041 if non-NULL are the complete memory reference trees. */
1044 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED
, tree base1
,
1045 HOST_WIDE_INT offset1
,
1046 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED
,
1047 alias_set_type ref1_alias_set
,
1048 alias_set_type base1_alias_set
,
1049 tree ref2 ATTRIBUTE_UNUSED
, tree base2
,
1050 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
1051 alias_set_type ref2_alias_set
,
1052 alias_set_type base2_alias_set
, bool tbaa_p
)
1055 tree ptrtype1
, dbase2
;
1056 HOST_WIDE_INT offset1p
= offset1
, offset2p
= offset2
;
1057 HOST_WIDE_INT doffset1
, doffset2
;
1059 gcc_checking_assert ((TREE_CODE (base1
) == MEM_REF
1060 || TREE_CODE (base1
) == TARGET_MEM_REF
)
1063 ptr1
= TREE_OPERAND (base1
, 0);
1065 /* The offset embedded in MEM_REFs can be negative. Bias them
1066 so that the resulting offset adjustment is positive. */
1067 offset_int moff
= mem_ref_offset (base1
);
1068 moff
= wi::lshift (moff
, LOG2_BITS_PER_UNIT
);
1069 if (wi::neg_p (moff
))
1070 offset2p
+= (-moff
).to_short_addr ();
1072 offset1p
+= moff
.to_short_addr ();
1074 /* If only one reference is based on a variable, they cannot alias if
1075 the pointer access is beyond the extent of the variable access.
1076 (the pointer base cannot validly point to an offset less than zero
1078 ??? IVOPTs creates bases that do not honor this restriction,
1079 so do not apply this optimization for TARGET_MEM_REFs. */
1080 if (TREE_CODE (base1
) != TARGET_MEM_REF
1081 && !ranges_overlap_p (MAX (0, offset1p
), -1, offset2p
, max_size2
))
1083 /* They also cannot alias if the pointer may not point to the decl. */
1084 if (!ptr_deref_may_alias_decl_p (ptr1
, base2
))
1087 /* Disambiguations that rely on strict aliasing rules follow. */
1088 if (!flag_strict_aliasing
|| !tbaa_p
)
1091 ptrtype1
= TREE_TYPE (TREE_OPERAND (base1
, 1));
1093 /* If the alias set for a pointer access is zero all bets are off. */
1094 if (base1_alias_set
== -1)
1095 base1_alias_set
= get_deref_alias_set (ptrtype1
);
1096 if (base1_alias_set
== 0)
1098 if (base2_alias_set
== -1)
1099 base2_alias_set
= get_alias_set (base2
);
1101 /* When we are trying to disambiguate an access with a pointer dereference
1102 as base versus one with a decl as base we can use both the size
1103 of the decl and its dynamic type for extra disambiguation.
1104 ??? We do not know anything about the dynamic type of the decl
1105 other than that its alias-set contains base2_alias_set as a subset
1106 which does not help us here. */
1107 /* As we know nothing useful about the dynamic type of the decl just
1108 use the usual conflict check rather than a subset test.
1109 ??? We could introduce -fvery-strict-aliasing when the language
1110 does not allow decls to have a dynamic type that differs from their
1111 static type. Then we can check
1112 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1113 if (base1_alias_set
!= base2_alias_set
1114 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
1116 /* If the size of the access relevant for TBAA through the pointer
1117 is bigger than the size of the decl we can't possibly access the
1118 decl via that pointer. */
1119 if (DECL_SIZE (base2
) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1
))
1120 && TREE_CODE (DECL_SIZE (base2
)) == INTEGER_CST
1121 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1
))) == INTEGER_CST
1122 /* ??? This in turn may run afoul when a decl of type T which is
1123 a member of union type U is accessed through a pointer to
1124 type U and sizeof T is smaller than sizeof U. */
1125 && TREE_CODE (TREE_TYPE (ptrtype1
)) != UNION_TYPE
1126 && TREE_CODE (TREE_TYPE (ptrtype1
)) != QUAL_UNION_TYPE
1127 && tree_int_cst_lt (DECL_SIZE (base2
), TYPE_SIZE (TREE_TYPE (ptrtype1
))))
1133 /* If the decl is accessed via a MEM_REF, reconstruct the base
1134 we can use for TBAA and an appropriately adjusted offset. */
1136 while (handled_component_p (dbase2
))
1137 dbase2
= TREE_OPERAND (dbase2
, 0);
1140 if (TREE_CODE (dbase2
) == MEM_REF
1141 || TREE_CODE (dbase2
) == TARGET_MEM_REF
)
1143 offset_int moff
= mem_ref_offset (dbase2
);
1144 moff
= wi::lshift (moff
, LOG2_BITS_PER_UNIT
);
1145 if (wi::neg_p (moff
))
1146 doffset1
-= (-moff
).to_short_addr ();
1148 doffset2
-= moff
.to_short_addr ();
1151 /* If either reference is view-converted, give up now. */
1152 if (same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) != 1
1153 || same_type_for_tbaa (TREE_TYPE (dbase2
), TREE_TYPE (base2
)) != 1)
1156 /* If both references are through the same type, they do not alias
1157 if the accesses do not overlap. This does extra disambiguation
1158 for mixed/pointer accesses but requires strict aliasing.
1159 For MEM_REFs we require that the component-ref offset we computed
1160 is relative to the start of the type which we ensure by
1161 comparing rvalue and access type and disregarding the constant
1163 if ((TREE_CODE (base1
) != TARGET_MEM_REF
1164 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
1165 && same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (dbase2
)) == 1)
1166 return ranges_overlap_p (doffset1
, max_size1
, doffset2
, max_size2
);
1169 && nonoverlapping_component_refs_p (ref1
, ref2
))
1172 /* Do access-path based disambiguation. */
1174 && (handled_component_p (ref1
) || handled_component_p (ref2
)))
1175 return aliasing_component_refs_p (ref1
,
1176 ref1_alias_set
, base1_alias_set
,
1179 ref2_alias_set
, base2_alias_set
,
1180 offset2
, max_size2
, true);
1185 /* Return true if two indirect references based on *PTR1
1186 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1187 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1188 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1189 in which case they are computed on-demand. REF1 and REF2
1190 if non-NULL are the complete memory reference trees. */
1193 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED
, tree base1
,
1194 HOST_WIDE_INT offset1
, HOST_WIDE_INT max_size1
,
1195 alias_set_type ref1_alias_set
,
1196 alias_set_type base1_alias_set
,
1197 tree ref2 ATTRIBUTE_UNUSED
, tree base2
,
1198 HOST_WIDE_INT offset2
, HOST_WIDE_INT max_size2
,
1199 alias_set_type ref2_alias_set
,
1200 alias_set_type base2_alias_set
, bool tbaa_p
)
1204 tree ptrtype1
, ptrtype2
;
1206 gcc_checking_assert ((TREE_CODE (base1
) == MEM_REF
1207 || TREE_CODE (base1
) == TARGET_MEM_REF
)
1208 && (TREE_CODE (base2
) == MEM_REF
1209 || TREE_CODE (base2
) == TARGET_MEM_REF
));
1211 ptr1
= TREE_OPERAND (base1
, 0);
1212 ptr2
= TREE_OPERAND (base2
, 0);
1214 /* If both bases are based on pointers they cannot alias if they may not
1215 point to the same memory object or if they point to the same object
1216 and the accesses do not overlap. */
1217 if ((!cfun
|| gimple_in_ssa_p (cfun
))
1218 && operand_equal_p (ptr1
, ptr2
, 0)
1219 && (((TREE_CODE (base1
) != TARGET_MEM_REF
1220 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
1221 && (TREE_CODE (base2
) != TARGET_MEM_REF
1222 || (!TMR_INDEX (base2
) && !TMR_INDEX2 (base2
))))
1223 || (TREE_CODE (base1
) == TARGET_MEM_REF
1224 && TREE_CODE (base2
) == TARGET_MEM_REF
1225 && (TMR_STEP (base1
) == TMR_STEP (base2
)
1226 || (TMR_STEP (base1
) && TMR_STEP (base2
)
1227 && operand_equal_p (TMR_STEP (base1
),
1228 TMR_STEP (base2
), 0)))
1229 && (TMR_INDEX (base1
) == TMR_INDEX (base2
)
1230 || (TMR_INDEX (base1
) && TMR_INDEX (base2
)
1231 && operand_equal_p (TMR_INDEX (base1
),
1232 TMR_INDEX (base2
), 0)))
1233 && (TMR_INDEX2 (base1
) == TMR_INDEX2 (base2
)
1234 || (TMR_INDEX2 (base1
) && TMR_INDEX2 (base2
)
1235 && operand_equal_p (TMR_INDEX2 (base1
),
1236 TMR_INDEX2 (base2
), 0))))))
1239 /* The offset embedded in MEM_REFs can be negative. Bias them
1240 so that the resulting offset adjustment is positive. */
1241 moff
= mem_ref_offset (base1
);
1242 moff
= wi::lshift (moff
, LOG2_BITS_PER_UNIT
);
1243 if (wi::neg_p (moff
))
1244 offset2
+= (-moff
).to_short_addr ();
1246 offset1
+= moff
.to_shwi ();
1247 moff
= mem_ref_offset (base2
);
1248 moff
= wi::lshift (moff
, LOG2_BITS_PER_UNIT
);
1249 if (wi::neg_p (moff
))
1250 offset1
+= (-moff
).to_short_addr ();
1252 offset2
+= moff
.to_short_addr ();
1253 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
1255 if (!ptr_derefs_may_alias_p (ptr1
, ptr2
))
1258 /* Disambiguations that rely on strict aliasing rules follow. */
1259 if (!flag_strict_aliasing
|| !tbaa_p
)
1262 ptrtype1
= TREE_TYPE (TREE_OPERAND (base1
, 1));
1263 ptrtype2
= TREE_TYPE (TREE_OPERAND (base2
, 1));
1265 /* If the alias set for a pointer access is zero all bets are off. */
1266 if (base1_alias_set
== -1)
1267 base1_alias_set
= get_deref_alias_set (ptrtype1
);
1268 if (base1_alias_set
== 0)
1270 if (base2_alias_set
== -1)
1271 base2_alias_set
= get_deref_alias_set (ptrtype2
);
1272 if (base2_alias_set
== 0)
1275 /* If both references are through the same type, they do not alias
1276 if the accesses do not overlap. This does extra disambiguation
1277 for mixed/pointer accesses but requires strict aliasing. */
1278 if ((TREE_CODE (base1
) != TARGET_MEM_REF
1279 || (!TMR_INDEX (base1
) && !TMR_INDEX2 (base1
)))
1280 && (TREE_CODE (base2
) != TARGET_MEM_REF
1281 || (!TMR_INDEX (base2
) && !TMR_INDEX2 (base2
)))
1282 && same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) == 1
1283 && same_type_for_tbaa (TREE_TYPE (base2
), TREE_TYPE (ptrtype2
)) == 1
1284 && same_type_for_tbaa (TREE_TYPE (ptrtype1
),
1285 TREE_TYPE (ptrtype2
)) == 1)
1286 return ranges_overlap_p (offset1
, max_size1
, offset2
, max_size2
);
1288 /* Do type-based disambiguation. */
1289 if (base1_alias_set
!= base2_alias_set
1290 && !alias_sets_conflict_p (base1_alias_set
, base2_alias_set
))
1293 /* If either reference is view-converted, give up now. */
1294 if (same_type_for_tbaa (TREE_TYPE (base1
), TREE_TYPE (ptrtype1
)) != 1
1295 || same_type_for_tbaa (TREE_TYPE (base2
), TREE_TYPE (ptrtype2
)) != 1)
1299 && nonoverlapping_component_refs_p (ref1
, ref2
))
1302 /* Do access-path based disambiguation. */
1304 && (handled_component_p (ref1
) || handled_component_p (ref2
)))
1305 return aliasing_component_refs_p (ref1
,
1306 ref1_alias_set
, base1_alias_set
,
1309 ref2_alias_set
, base2_alias_set
,
1310 offset2
, max_size2
, false);
1315 /* Return true, if the two memory references REF1 and REF2 may alias. */
1318 refs_may_alias_p_1 (ao_ref
*ref1
, ao_ref
*ref2
, bool tbaa_p
)
1321 HOST_WIDE_INT offset1
= 0, offset2
= 0;
1322 HOST_WIDE_INT max_size1
= -1, max_size2
= -1;
1323 bool var1_p
, var2_p
, ind1_p
, ind2_p
;
1325 gcc_checking_assert ((!ref1
->ref
1326 || TREE_CODE (ref1
->ref
) == SSA_NAME
1327 || DECL_P (ref1
->ref
)
1328 || TREE_CODE (ref1
->ref
) == STRING_CST
1329 || handled_component_p (ref1
->ref
)
1330 || TREE_CODE (ref1
->ref
) == MEM_REF
1331 || TREE_CODE (ref1
->ref
) == TARGET_MEM_REF
)
1333 || TREE_CODE (ref2
->ref
) == SSA_NAME
1334 || DECL_P (ref2
->ref
)
1335 || TREE_CODE (ref2
->ref
) == STRING_CST
1336 || handled_component_p (ref2
->ref
)
1337 || TREE_CODE (ref2
->ref
) == MEM_REF
1338 || TREE_CODE (ref2
->ref
) == TARGET_MEM_REF
));
1340 /* Decompose the references into their base objects and the access. */
1341 base1
= ao_ref_base (ref1
);
1342 offset1
= ref1
->offset
;
1343 max_size1
= ref1
->max_size
;
1344 base2
= ao_ref_base (ref2
);
1345 offset2
= ref2
->offset
;
1346 max_size2
= ref2
->max_size
;
1348 /* We can end up with registers or constants as bases for example from
1349 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1350 which is seen as a struct copy. */
1351 if (TREE_CODE (base1
) == SSA_NAME
1352 || TREE_CODE (base1
) == CONST_DECL
1353 || TREE_CODE (base1
) == CONSTRUCTOR
1354 || TREE_CODE (base1
) == ADDR_EXPR
1355 || CONSTANT_CLASS_P (base1
)
1356 || TREE_CODE (base2
) == SSA_NAME
1357 || TREE_CODE (base2
) == CONST_DECL
1358 || TREE_CODE (base2
) == CONSTRUCTOR
1359 || TREE_CODE (base2
) == ADDR_EXPR
1360 || CONSTANT_CLASS_P (base2
))
1363 /* We can end up referring to code via function and label decls.
1364 As we likely do not properly track code aliases conservatively
1366 if (TREE_CODE (base1
) == FUNCTION_DECL
1367 || TREE_CODE (base1
) == LABEL_DECL
1368 || TREE_CODE (base2
) == FUNCTION_DECL
1369 || TREE_CODE (base2
) == LABEL_DECL
)
1372 /* Two volatile accesses always conflict. */
1373 if (ref1
->volatile_p
1374 && ref2
->volatile_p
)
1377 /* Defer to simple offset based disambiguation if we have
1378 references based on two decls. Do this before defering to
1379 TBAA to handle must-alias cases in conformance with the
1380 GCC extension of allowing type-punning through unions. */
1381 var1_p
= DECL_P (base1
);
1382 var2_p
= DECL_P (base2
);
1383 if (var1_p
&& var2_p
)
1384 return decl_refs_may_alias_p (ref1
->ref
, base1
, offset1
, max_size1
,
1385 ref2
->ref
, base2
, offset2
, max_size2
);
1387 ind1_p
= (TREE_CODE (base1
) == MEM_REF
1388 || TREE_CODE (base1
) == TARGET_MEM_REF
);
1389 ind2_p
= (TREE_CODE (base2
) == MEM_REF
1390 || TREE_CODE (base2
) == TARGET_MEM_REF
);
1392 /* Canonicalize the pointer-vs-decl case. */
1393 if (ind1_p
&& var2_p
)
1398 tmp1
= offset1
; offset1
= offset2
; offset2
= tmp1
;
1399 tmp1
= max_size1
; max_size1
= max_size2
; max_size2
= tmp1
;
1400 tmp2
= base1
; base1
= base2
; base2
= tmp2
;
1401 tmp3
= ref1
; ref1
= ref2
; ref2
= tmp3
;
1408 /* First defer to TBAA if possible. */
1410 && flag_strict_aliasing
1411 && !alias_sets_conflict_p (ao_ref_alias_set (ref1
),
1412 ao_ref_alias_set (ref2
)))
1415 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1416 if (var1_p
&& ind2_p
)
1417 return indirect_ref_may_alias_decl_p (ref2
->ref
, base2
,
1419 ao_ref_alias_set (ref2
), -1,
1422 ao_ref_alias_set (ref1
),
1423 ao_ref_base_alias_set (ref1
),
1425 else if (ind1_p
&& ind2_p
)
1426 return indirect_refs_may_alias_p (ref1
->ref
, base1
,
1428 ao_ref_alias_set (ref1
), -1,
1431 ao_ref_alias_set (ref2
), -1,
1434 /* We really do not want to end up here, but returning true is safe. */
1435 #ifdef ENABLE_CHECKING
1443 refs_may_alias_p (tree ref1
, ao_ref
*ref2
)
1446 ao_ref_init (&r1
, ref1
);
1447 return refs_may_alias_p_1 (&r1
, ref2
, true);
1451 refs_may_alias_p (tree ref1
, tree ref2
)
1455 ao_ref_init (&r1
, ref1
);
1456 ao_ref_init (&r2
, ref2
);
1457 res
= refs_may_alias_p_1 (&r1
, &r2
, true);
1459 ++alias_stats
.refs_may_alias_p_may_alias
;
1461 ++alias_stats
.refs_may_alias_p_no_alias
;
1465 /* Returns true if there is a anti-dependence for the STORE that
1466 executes after the LOAD. */
1469 refs_anti_dependent_p (tree load
, tree store
)
1472 ao_ref_init (&r1
, load
);
1473 ao_ref_init (&r2
, store
);
1474 return refs_may_alias_p_1 (&r1
, &r2
, false);
1477 /* Returns true if there is a output dependence for the stores
1478 STORE1 and STORE2. */
1481 refs_output_dependent_p (tree store1
, tree store2
)
1484 ao_ref_init (&r1
, store1
);
1485 ao_ref_init (&r2
, store2
);
1486 return refs_may_alias_p_1 (&r1
, &r2
, false);
1489 /* If the call CALL may use the memory reference REF return true,
1490 otherwise return false. */
1493 ref_maybe_used_by_call_p_1 (gimple call
, ao_ref
*ref
)
1497 int flags
= gimple_call_flags (call
);
1499 /* Const functions without a static chain do not implicitly use memory. */
1500 if (!gimple_call_chain (call
)
1501 && (flags
& (ECF_CONST
|ECF_NOVOPS
)))
1504 base
= ao_ref_base (ref
);
1508 /* A call that is not without side-effects might involve volatile
1509 accesses and thus conflicts with all other volatile accesses. */
1510 if (ref
->volatile_p
)
1513 /* If the reference is based on a decl that is not aliased the call
1514 cannot possibly use it. */
1516 && !may_be_aliased (base
)
1517 /* But local statics can be used through recursion. */
1518 && !is_global_var (base
))
1521 callee
= gimple_call_fndecl (call
);
1523 /* Handle those builtin functions explicitly that do not act as
1524 escape points. See tree-ssa-structalias.c:find_func_aliases
1525 for the list of builtins we might need to handle here. */
1526 if (callee
!= NULL_TREE
1527 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
1528 switch (DECL_FUNCTION_CODE (callee
))
1530 /* All the following functions read memory pointed to by
1531 their second argument. strcat/strncat additionally
1532 reads memory pointed to by the first argument. */
1533 case BUILT_IN_STRCAT
:
1534 case BUILT_IN_STRNCAT
:
1537 ao_ref_init_from_ptr_and_size (&dref
,
1538 gimple_call_arg (call
, 0),
1540 if (refs_may_alias_p_1 (&dref
, ref
, false))
1544 case BUILT_IN_STRCPY
:
1545 case BUILT_IN_STRNCPY
:
1546 case BUILT_IN_MEMCPY
:
1547 case BUILT_IN_MEMMOVE
:
1548 case BUILT_IN_MEMPCPY
:
1549 case BUILT_IN_STPCPY
:
1550 case BUILT_IN_STPNCPY
:
1551 case BUILT_IN_TM_MEMCPY
:
1552 case BUILT_IN_TM_MEMMOVE
:
1555 tree size
= NULL_TREE
;
1556 if (gimple_call_num_args (call
) == 3)
1557 size
= gimple_call_arg (call
, 2);
1558 ao_ref_init_from_ptr_and_size (&dref
,
1559 gimple_call_arg (call
, 1),
1561 return refs_may_alias_p_1 (&dref
, ref
, false);
1563 case BUILT_IN_STRCAT_CHK
:
1564 case BUILT_IN_STRNCAT_CHK
:
1567 ao_ref_init_from_ptr_and_size (&dref
,
1568 gimple_call_arg (call
, 0),
1570 if (refs_may_alias_p_1 (&dref
, ref
, false))
1574 case BUILT_IN_STRCPY_CHK
:
1575 case BUILT_IN_STRNCPY_CHK
:
1576 case BUILT_IN_MEMCPY_CHK
:
1577 case BUILT_IN_MEMMOVE_CHK
:
1578 case BUILT_IN_MEMPCPY_CHK
:
1579 case BUILT_IN_STPCPY_CHK
:
1580 case BUILT_IN_STPNCPY_CHK
:
1583 tree size
= NULL_TREE
;
1584 if (gimple_call_num_args (call
) == 4)
1585 size
= gimple_call_arg (call
, 2);
1586 ao_ref_init_from_ptr_and_size (&dref
,
1587 gimple_call_arg (call
, 1),
1589 return refs_may_alias_p_1 (&dref
, ref
, false);
1591 case BUILT_IN_BCOPY
:
1594 tree size
= gimple_call_arg (call
, 2);
1595 ao_ref_init_from_ptr_and_size (&dref
,
1596 gimple_call_arg (call
, 0),
1598 return refs_may_alias_p_1 (&dref
, ref
, false);
1601 /* The following functions read memory pointed to by their
1603 CASE_BUILT_IN_TM_LOAD (1):
1604 CASE_BUILT_IN_TM_LOAD (2):
1605 CASE_BUILT_IN_TM_LOAD (4):
1606 CASE_BUILT_IN_TM_LOAD (8):
1607 CASE_BUILT_IN_TM_LOAD (FLOAT
):
1608 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
1609 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
1610 CASE_BUILT_IN_TM_LOAD (M64
):
1611 CASE_BUILT_IN_TM_LOAD (M128
):
1612 CASE_BUILT_IN_TM_LOAD (M256
):
1613 case BUILT_IN_TM_LOG
:
1614 case BUILT_IN_TM_LOG_1
:
1615 case BUILT_IN_TM_LOG_2
:
1616 case BUILT_IN_TM_LOG_4
:
1617 case BUILT_IN_TM_LOG_8
:
1618 case BUILT_IN_TM_LOG_FLOAT
:
1619 case BUILT_IN_TM_LOG_DOUBLE
:
1620 case BUILT_IN_TM_LOG_LDOUBLE
:
1621 case BUILT_IN_TM_LOG_M64
:
1622 case BUILT_IN_TM_LOG_M128
:
1623 case BUILT_IN_TM_LOG_M256
:
1624 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call
, 0), ref
);
1626 /* These read memory pointed to by the first argument. */
1627 case BUILT_IN_STRDUP
:
1628 case BUILT_IN_STRNDUP
:
1629 case BUILT_IN_REALLOC
:
1632 tree size
= NULL_TREE
;
1633 if (gimple_call_num_args (call
) == 2)
1634 size
= gimple_call_arg (call
, 1);
1635 ao_ref_init_from_ptr_and_size (&dref
,
1636 gimple_call_arg (call
, 0),
1638 return refs_may_alias_p_1 (&dref
, ref
, false);
1640 /* These read memory pointed to by the first argument. */
1641 case BUILT_IN_INDEX
:
1642 case BUILT_IN_STRCHR
:
1643 case BUILT_IN_STRRCHR
:
1646 ao_ref_init_from_ptr_and_size (&dref
,
1647 gimple_call_arg (call
, 0),
1649 return refs_may_alias_p_1 (&dref
, ref
, false);
1651 /* These read memory pointed to by the first argument with size
1652 in the third argument. */
1653 case BUILT_IN_MEMCHR
:
1656 ao_ref_init_from_ptr_and_size (&dref
,
1657 gimple_call_arg (call
, 0),
1658 gimple_call_arg (call
, 2));
1659 return refs_may_alias_p_1 (&dref
, ref
, false);
1661 /* These read memory pointed to by the first and second arguments. */
1662 case BUILT_IN_STRSTR
:
1663 case BUILT_IN_STRPBRK
:
1666 ao_ref_init_from_ptr_and_size (&dref
,
1667 gimple_call_arg (call
, 0),
1669 if (refs_may_alias_p_1 (&dref
, ref
, false))
1671 ao_ref_init_from_ptr_and_size (&dref
,
1672 gimple_call_arg (call
, 1),
1674 return refs_may_alias_p_1 (&dref
, ref
, false);
1677 /* The following builtins do not read from memory. */
1679 case BUILT_IN_MALLOC
:
1680 case BUILT_IN_POSIX_MEMALIGN
:
1681 case BUILT_IN_ALIGNED_ALLOC
:
1682 case BUILT_IN_CALLOC
:
1683 case BUILT_IN_ALLOCA
:
1684 case BUILT_IN_ALLOCA_WITH_ALIGN
:
1685 case BUILT_IN_STACK_SAVE
:
1686 case BUILT_IN_STACK_RESTORE
:
1687 case BUILT_IN_MEMSET
:
1688 case BUILT_IN_TM_MEMSET
:
1689 case BUILT_IN_MEMSET_CHK
:
1690 case BUILT_IN_FREXP
:
1691 case BUILT_IN_FREXPF
:
1692 case BUILT_IN_FREXPL
:
1693 case BUILT_IN_GAMMA_R
:
1694 case BUILT_IN_GAMMAF_R
:
1695 case BUILT_IN_GAMMAL_R
:
1696 case BUILT_IN_LGAMMA_R
:
1697 case BUILT_IN_LGAMMAF_R
:
1698 case BUILT_IN_LGAMMAL_R
:
1700 case BUILT_IN_MODFF
:
1701 case BUILT_IN_MODFL
:
1702 case BUILT_IN_REMQUO
:
1703 case BUILT_IN_REMQUOF
:
1704 case BUILT_IN_REMQUOL
:
1705 case BUILT_IN_SINCOS
:
1706 case BUILT_IN_SINCOSF
:
1707 case BUILT_IN_SINCOSL
:
1708 case BUILT_IN_ASSUME_ALIGNED
:
1709 case BUILT_IN_VA_END
:
1711 /* __sync_* builtins and some OpenMP builtins act as threading
1713 #undef DEF_SYNC_BUILTIN
1714 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1715 #include "sync-builtins.def"
1716 #undef DEF_SYNC_BUILTIN
1717 case BUILT_IN_GOMP_ATOMIC_START
:
1718 case BUILT_IN_GOMP_ATOMIC_END
:
1719 case BUILT_IN_GOMP_BARRIER
:
1720 case BUILT_IN_GOMP_BARRIER_CANCEL
:
1721 case BUILT_IN_GOMP_TASKWAIT
:
1722 case BUILT_IN_GOMP_TASKGROUP_END
:
1723 case BUILT_IN_GOMP_CRITICAL_START
:
1724 case BUILT_IN_GOMP_CRITICAL_END
:
1725 case BUILT_IN_GOMP_CRITICAL_NAME_START
:
1726 case BUILT_IN_GOMP_CRITICAL_NAME_END
:
1727 case BUILT_IN_GOMP_LOOP_END
:
1728 case BUILT_IN_GOMP_LOOP_END_CANCEL
:
1729 case BUILT_IN_GOMP_ORDERED_START
:
1730 case BUILT_IN_GOMP_ORDERED_END
:
1731 case BUILT_IN_GOMP_SECTIONS_END
:
1732 case BUILT_IN_GOMP_SECTIONS_END_CANCEL
:
1733 case BUILT_IN_GOMP_SINGLE_COPY_START
:
1734 case BUILT_IN_GOMP_SINGLE_COPY_END
:
1738 /* Fallthru to general call handling. */;
1741 /* Check if base is a global static variable that is not read
1743 if (callee
!= NULL_TREE
1744 && TREE_CODE (base
) == VAR_DECL
1745 && TREE_STATIC (base
))
1747 struct cgraph_node
*node
= cgraph_node::get (callee
);
1750 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1751 node yet. We should enforce that there are nodes for all decls in the
1752 IL and remove this check instead. */
1754 && (not_read
= ipa_reference_get_not_read_global (node
))
1755 && bitmap_bit_p (not_read
, DECL_UID (base
)))
1759 /* Check if the base variable is call-used. */
1762 if (pt_solution_includes (gimple_call_use_set (call
), base
))
1765 else if ((TREE_CODE (base
) == MEM_REF
1766 || TREE_CODE (base
) == TARGET_MEM_REF
)
1767 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
1769 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0));
1773 if (pt_solutions_intersect (gimple_call_use_set (call
), &pi
->pt
))
1779 /* Inspect call arguments for passed-by-value aliases. */
1781 for (i
= 0; i
< gimple_call_num_args (call
); ++i
)
1783 tree op
= gimple_call_arg (call
, i
);
1784 int flags
= gimple_call_arg_flags (call
, i
);
1786 if (flags
& EAF_UNUSED
)
1789 if (TREE_CODE (op
) == WITH_SIZE_EXPR
)
1790 op
= TREE_OPERAND (op
, 0);
1792 if (TREE_CODE (op
) != SSA_NAME
1793 && !is_gimple_min_invariant (op
))
1796 ao_ref_init (&r
, op
);
1797 if (refs_may_alias_p_1 (&r
, ref
, true))
1806 ref_maybe_used_by_call_p (gimple call
, ao_ref
*ref
)
1809 res
= ref_maybe_used_by_call_p_1 (call
, ref
);
1811 ++alias_stats
.ref_maybe_used_by_call_p_may_alias
;
1813 ++alias_stats
.ref_maybe_used_by_call_p_no_alias
;
1818 /* If the statement STMT may use the memory reference REF return
1819 true, otherwise return false. */
1822 ref_maybe_used_by_stmt_p (gimple stmt
, ao_ref
*ref
)
1824 if (is_gimple_assign (stmt
))
1828 /* All memory assign statements are single. */
1829 if (!gimple_assign_single_p (stmt
))
1832 rhs
= gimple_assign_rhs1 (stmt
);
1833 if (is_gimple_reg (rhs
)
1834 || is_gimple_min_invariant (rhs
)
1835 || gimple_assign_rhs_code (stmt
) == CONSTRUCTOR
)
1838 return refs_may_alias_p (rhs
, ref
);
1840 else if (is_gimple_call (stmt
))
1841 return ref_maybe_used_by_call_p (stmt
, ref
);
1842 else if (gimple_code (stmt
) == GIMPLE_RETURN
)
1844 tree retval
= gimple_return_retval (stmt
);
1846 && TREE_CODE (retval
) != SSA_NAME
1847 && !is_gimple_min_invariant (retval
)
1848 && refs_may_alias_p (retval
, ref
))
1850 /* If ref escapes the function then the return acts as a use. */
1851 tree base
= ao_ref_base (ref
);
1854 else if (DECL_P (base
))
1855 return is_global_var (base
);
1856 else if (TREE_CODE (base
) == MEM_REF
1857 || TREE_CODE (base
) == TARGET_MEM_REF
)
1858 return ptr_deref_may_alias_global_p (TREE_OPERAND (base
, 0));
1866 ref_maybe_used_by_stmt_p (gimple stmt
, tree ref
)
1869 ao_ref_init (&r
, ref
);
1870 return ref_maybe_used_by_stmt_p (stmt
, &r
);
1873 /* If the call in statement CALL may clobber the memory reference REF
1874 return true, otherwise return false. */
1877 call_may_clobber_ref_p_1 (gimple call
, ao_ref
*ref
)
1882 /* If the call is pure or const it cannot clobber anything. */
1883 if (gimple_call_flags (call
)
1884 & (ECF_PURE
|ECF_CONST
|ECF_LOOPING_CONST_OR_PURE
|ECF_NOVOPS
))
1887 base
= ao_ref_base (ref
);
1891 if (TREE_CODE (base
) == SSA_NAME
1892 || CONSTANT_CLASS_P (base
))
1895 /* A call that is not without side-effects might involve volatile
1896 accesses and thus conflicts with all other volatile accesses. */
1897 if (ref
->volatile_p
)
1900 /* If the reference is based on a decl that is not aliased the call
1901 cannot possibly clobber it. */
1903 && !may_be_aliased (base
)
1904 /* But local non-readonly statics can be modified through recursion
1905 or the call may implement a threading barrier which we must
1906 treat as may-def. */
1907 && (TREE_READONLY (base
)
1908 || !is_global_var (base
)))
1911 callee
= gimple_call_fndecl (call
);
1913 /* Handle those builtin functions explicitly that do not act as
1914 escape points. See tree-ssa-structalias.c:find_func_aliases
1915 for the list of builtins we might need to handle here. */
1916 if (callee
!= NULL_TREE
1917 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
1918 switch (DECL_FUNCTION_CODE (callee
))
1920 /* All the following functions clobber memory pointed to by
1921 their first argument. */
1922 case BUILT_IN_STRCPY
:
1923 case BUILT_IN_STRNCPY
:
1924 case BUILT_IN_MEMCPY
:
1925 case BUILT_IN_MEMMOVE
:
1926 case BUILT_IN_MEMPCPY
:
1927 case BUILT_IN_STPCPY
:
1928 case BUILT_IN_STPNCPY
:
1929 case BUILT_IN_STRCAT
:
1930 case BUILT_IN_STRNCAT
:
1931 case BUILT_IN_MEMSET
:
1932 case BUILT_IN_TM_MEMSET
:
1933 CASE_BUILT_IN_TM_STORE (1):
1934 CASE_BUILT_IN_TM_STORE (2):
1935 CASE_BUILT_IN_TM_STORE (4):
1936 CASE_BUILT_IN_TM_STORE (8):
1937 CASE_BUILT_IN_TM_STORE (FLOAT
):
1938 CASE_BUILT_IN_TM_STORE (DOUBLE
):
1939 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
1940 CASE_BUILT_IN_TM_STORE (M64
):
1941 CASE_BUILT_IN_TM_STORE (M128
):
1942 CASE_BUILT_IN_TM_STORE (M256
):
1943 case BUILT_IN_TM_MEMCPY
:
1944 case BUILT_IN_TM_MEMMOVE
:
1947 tree size
= NULL_TREE
;
1948 /* Don't pass in size for strncat, as the maximum size
1949 is strlen (dest) + n + 1 instead of n, resp.
1950 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1952 if (gimple_call_num_args (call
) == 3
1953 && DECL_FUNCTION_CODE (callee
) != BUILT_IN_STRNCAT
)
1954 size
= gimple_call_arg (call
, 2);
1955 ao_ref_init_from_ptr_and_size (&dref
,
1956 gimple_call_arg (call
, 0),
1958 return refs_may_alias_p_1 (&dref
, ref
, false);
1960 case BUILT_IN_STRCPY_CHK
:
1961 case BUILT_IN_STRNCPY_CHK
:
1962 case BUILT_IN_MEMCPY_CHK
:
1963 case BUILT_IN_MEMMOVE_CHK
:
1964 case BUILT_IN_MEMPCPY_CHK
:
1965 case BUILT_IN_STPCPY_CHK
:
1966 case BUILT_IN_STPNCPY_CHK
:
1967 case BUILT_IN_STRCAT_CHK
:
1968 case BUILT_IN_STRNCAT_CHK
:
1969 case BUILT_IN_MEMSET_CHK
:
1972 tree size
= NULL_TREE
;
1973 /* Don't pass in size for __strncat_chk, as the maximum size
1974 is strlen (dest) + n + 1 instead of n, resp.
1975 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1977 if (gimple_call_num_args (call
) == 4
1978 && DECL_FUNCTION_CODE (callee
) != BUILT_IN_STRNCAT_CHK
)
1979 size
= gimple_call_arg (call
, 2);
1980 ao_ref_init_from_ptr_and_size (&dref
,
1981 gimple_call_arg (call
, 0),
1983 return refs_may_alias_p_1 (&dref
, ref
, false);
1985 case BUILT_IN_BCOPY
:
1988 tree size
= gimple_call_arg (call
, 2);
1989 ao_ref_init_from_ptr_and_size (&dref
,
1990 gimple_call_arg (call
, 1),
1992 return refs_may_alias_p_1 (&dref
, ref
, false);
1994 /* Allocating memory does not have any side-effects apart from
1995 being the definition point for the pointer. */
1996 case BUILT_IN_MALLOC
:
1997 case BUILT_IN_ALIGNED_ALLOC
:
1998 case BUILT_IN_CALLOC
:
1999 case BUILT_IN_STRDUP
:
2000 case BUILT_IN_STRNDUP
:
2001 /* Unix98 specifies that errno is set on allocation failure. */
2003 && targetm
.ref_may_alias_errno (ref
))
2006 case BUILT_IN_STACK_SAVE
:
2007 case BUILT_IN_ALLOCA
:
2008 case BUILT_IN_ALLOCA_WITH_ALIGN
:
2009 case BUILT_IN_ASSUME_ALIGNED
:
2011 /* But posix_memalign stores a pointer into the memory pointed to
2012 by its first argument. */
2013 case BUILT_IN_POSIX_MEMALIGN
:
2015 tree ptrptr
= gimple_call_arg (call
, 0);
2017 ao_ref_init_from_ptr_and_size (&dref
, ptrptr
,
2018 TYPE_SIZE_UNIT (ptr_type_node
));
2019 return (refs_may_alias_p_1 (&dref
, ref
, false)
2021 && targetm
.ref_may_alias_errno (ref
)));
2023 /* Freeing memory kills the pointed-to memory. More importantly
2024 the call has to serve as a barrier for moving loads and stores
2027 case BUILT_IN_VA_END
:
2029 tree ptr
= gimple_call_arg (call
, 0);
2030 return ptr_deref_may_alias_ref_p_1 (ptr
, ref
);
2032 /* Realloc serves both as allocation point and deallocation point. */
2033 case BUILT_IN_REALLOC
:
2035 tree ptr
= gimple_call_arg (call
, 0);
2036 /* Unix98 specifies that errno is set on allocation failure. */
2037 return ((flag_errno_math
2038 && targetm
.ref_may_alias_errno (ref
))
2039 || ptr_deref_may_alias_ref_p_1 (ptr
, ref
));
2041 case BUILT_IN_GAMMA_R
:
2042 case BUILT_IN_GAMMAF_R
:
2043 case BUILT_IN_GAMMAL_R
:
2044 case BUILT_IN_LGAMMA_R
:
2045 case BUILT_IN_LGAMMAF_R
:
2046 case BUILT_IN_LGAMMAL_R
:
2048 tree out
= gimple_call_arg (call
, 1);
2049 if (ptr_deref_may_alias_ref_p_1 (out
, ref
))
2051 if (flag_errno_math
)
2055 case BUILT_IN_FREXP
:
2056 case BUILT_IN_FREXPF
:
2057 case BUILT_IN_FREXPL
:
2059 case BUILT_IN_MODFF
:
2060 case BUILT_IN_MODFL
:
2062 tree out
= gimple_call_arg (call
, 1);
2063 return ptr_deref_may_alias_ref_p_1 (out
, ref
);
2065 case BUILT_IN_REMQUO
:
2066 case BUILT_IN_REMQUOF
:
2067 case BUILT_IN_REMQUOL
:
2069 tree out
= gimple_call_arg (call
, 2);
2070 if (ptr_deref_may_alias_ref_p_1 (out
, ref
))
2072 if (flag_errno_math
)
2076 case BUILT_IN_SINCOS
:
2077 case BUILT_IN_SINCOSF
:
2078 case BUILT_IN_SINCOSL
:
2080 tree sin
= gimple_call_arg (call
, 1);
2081 tree cos
= gimple_call_arg (call
, 2);
2082 return (ptr_deref_may_alias_ref_p_1 (sin
, ref
)
2083 || ptr_deref_may_alias_ref_p_1 (cos
, ref
));
2085 /* __sync_* builtins and some OpenMP builtins act as threading
2087 #undef DEF_SYNC_BUILTIN
2088 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2089 #include "sync-builtins.def"
2090 #undef DEF_SYNC_BUILTIN
2091 case BUILT_IN_GOMP_ATOMIC_START
:
2092 case BUILT_IN_GOMP_ATOMIC_END
:
2093 case BUILT_IN_GOMP_BARRIER
:
2094 case BUILT_IN_GOMP_BARRIER_CANCEL
:
2095 case BUILT_IN_GOMP_TASKWAIT
:
2096 case BUILT_IN_GOMP_TASKGROUP_END
:
2097 case BUILT_IN_GOMP_CRITICAL_START
:
2098 case BUILT_IN_GOMP_CRITICAL_END
:
2099 case BUILT_IN_GOMP_CRITICAL_NAME_START
:
2100 case BUILT_IN_GOMP_CRITICAL_NAME_END
:
2101 case BUILT_IN_GOMP_LOOP_END
:
2102 case BUILT_IN_GOMP_LOOP_END_CANCEL
:
2103 case BUILT_IN_GOMP_ORDERED_START
:
2104 case BUILT_IN_GOMP_ORDERED_END
:
2105 case BUILT_IN_GOMP_SECTIONS_END
:
2106 case BUILT_IN_GOMP_SECTIONS_END_CANCEL
:
2107 case BUILT_IN_GOMP_SINGLE_COPY_START
:
2108 case BUILT_IN_GOMP_SINGLE_COPY_END
:
2111 /* Fallthru to general call handling. */;
2114 /* Check if base is a global static variable that is not written
2116 if (callee
!= NULL_TREE
2117 && TREE_CODE (base
) == VAR_DECL
2118 && TREE_STATIC (base
))
2120 struct cgraph_node
*node
= cgraph_node::get (callee
);
2124 && (not_written
= ipa_reference_get_not_written_global (node
))
2125 && bitmap_bit_p (not_written
, DECL_UID (base
)))
2129 /* Check if the base variable is call-clobbered. */
2131 return pt_solution_includes (gimple_call_clobber_set (call
), base
);
2132 else if ((TREE_CODE (base
) == MEM_REF
2133 || TREE_CODE (base
) == TARGET_MEM_REF
)
2134 && TREE_CODE (TREE_OPERAND (base
, 0)) == SSA_NAME
)
2136 struct ptr_info_def
*pi
= SSA_NAME_PTR_INFO (TREE_OPERAND (base
, 0));
2140 return pt_solutions_intersect (gimple_call_clobber_set (call
), &pi
->pt
);
2146 /* If the call in statement CALL may clobber the memory reference REF
2147 return true, otherwise return false. */
2150 call_may_clobber_ref_p (gimple call
, tree ref
)
2154 ao_ref_init (&r
, ref
);
2155 res
= call_may_clobber_ref_p_1 (call
, &r
);
2157 ++alias_stats
.call_may_clobber_ref_p_may_alias
;
2159 ++alias_stats
.call_may_clobber_ref_p_no_alias
;
2164 /* If the statement STMT may clobber the memory reference REF return true,
2165 otherwise return false. */
2168 stmt_may_clobber_ref_p_1 (gimple stmt
, ao_ref
*ref
)
2170 if (is_gimple_call (stmt
))
2172 tree lhs
= gimple_call_lhs (stmt
);
2174 && TREE_CODE (lhs
) != SSA_NAME
)
2177 ao_ref_init (&r
, lhs
);
2178 if (refs_may_alias_p_1 (ref
, &r
, true))
2182 return call_may_clobber_ref_p_1 (stmt
, ref
);
2184 else if (gimple_assign_single_p (stmt
))
2186 tree lhs
= gimple_assign_lhs (stmt
);
2187 if (TREE_CODE (lhs
) != SSA_NAME
)
2190 ao_ref_init (&r
, lhs
);
2191 return refs_may_alias_p_1 (ref
, &r
, true);
2194 else if (gimple_code (stmt
) == GIMPLE_ASM
)
2201 stmt_may_clobber_ref_p (gimple stmt
, tree ref
)
2204 ao_ref_init (&r
, ref
);
2205 return stmt_may_clobber_ref_p_1 (stmt
, &r
);
2208 /* If STMT kills the memory reference REF return true, otherwise
2212 stmt_kills_ref_p (gimple stmt
, ao_ref
*ref
)
2214 if (!ao_ref_base (ref
))
2217 if (gimple_has_lhs (stmt
)
2218 && TREE_CODE (gimple_get_lhs (stmt
)) != SSA_NAME
2219 /* The assignment is not necessarily carried out if it can throw
2220 and we can catch it in the current function where we could inspect
2222 ??? We only need to care about the RHS throwing. For aggregate
2223 assignments or similar calls and non-call exceptions the LHS
2224 might throw as well. */
2225 && !stmt_can_throw_internal (stmt
))
2227 tree lhs
= gimple_get_lhs (stmt
);
2228 /* If LHS is literally a base of the access we are done. */
2231 tree base
= ref
->ref
;
2232 if (handled_component_p (base
))
2234 tree saved_lhs0
= NULL_TREE
;
2235 if (handled_component_p (lhs
))
2237 saved_lhs0
= TREE_OPERAND (lhs
, 0);
2238 TREE_OPERAND (lhs
, 0) = integer_zero_node
;
2242 /* Just compare the outermost handled component, if
2243 they are equal we have found a possible common
2245 tree saved_base0
= TREE_OPERAND (base
, 0);
2246 TREE_OPERAND (base
, 0) = integer_zero_node
;
2247 bool res
= operand_equal_p (lhs
, base
, 0);
2248 TREE_OPERAND (base
, 0) = saved_base0
;
2251 /* Otherwise drop handled components of the access. */
2254 while (handled_component_p (base
));
2256 TREE_OPERAND (lhs
, 0) = saved_lhs0
;
2258 /* Finally check if lhs is equal or equal to the base candidate
2260 if (operand_equal_p (lhs
, base
, 0))
2264 /* Now look for non-literal equal bases with the restriction of
2265 handling constant offset and size. */
2266 /* For a must-alias check we need to be able to constrain
2267 the access properly. */
2268 if (ref
->max_size
== -1)
2270 HOST_WIDE_INT size
, offset
, max_size
, ref_offset
= ref
->offset
;
2271 tree base
= get_ref_base_and_extent (lhs
, &offset
, &size
, &max_size
);
2272 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2273 so base == ref->base does not always hold. */
2274 if (base
!= ref
->base
)
2276 /* If both base and ref->base are MEM_REFs, only compare the
2277 first operand, and if the second operand isn't equal constant,
2278 try to add the offsets into offset and ref_offset. */
2279 if (TREE_CODE (base
) == MEM_REF
&& TREE_CODE (ref
->base
) == MEM_REF
2280 && TREE_OPERAND (base
, 0) == TREE_OPERAND (ref
->base
, 0))
2282 if (!tree_int_cst_equal (TREE_OPERAND (base
, 1),
2283 TREE_OPERAND (ref
->base
, 1)))
2285 offset_int off1
= mem_ref_offset (base
);
2286 off1
= wi::lshift (off1
, LOG2_BITS_PER_UNIT
);
2288 offset_int off2
= mem_ref_offset (ref
->base
);
2289 off2
= wi::lshift (off2
, LOG2_BITS_PER_UNIT
);
2291 if (wi::fits_shwi_p (off1
) && wi::fits_shwi_p (off2
))
2293 offset
= off1
.to_shwi ();
2294 ref_offset
= off2
.to_shwi ();
2303 /* For a must-alias check we need to be able to constrain
2304 the access properly. */
2305 if (size
!= -1 && size
== max_size
)
2307 if (offset
<= ref_offset
2308 && offset
+ size
>= ref_offset
+ ref
->max_size
)
2313 if (is_gimple_call (stmt
))
2315 tree callee
= gimple_call_fndecl (stmt
);
2316 if (callee
!= NULL_TREE
2317 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
2318 switch (DECL_FUNCTION_CODE (callee
))
2322 tree ptr
= gimple_call_arg (stmt
, 0);
2323 tree base
= ao_ref_base (ref
);
2324 if (base
&& TREE_CODE (base
) == MEM_REF
2325 && TREE_OPERAND (base
, 0) == ptr
)
2330 case BUILT_IN_MEMCPY
:
2331 case BUILT_IN_MEMPCPY
:
2332 case BUILT_IN_MEMMOVE
:
2333 case BUILT_IN_MEMSET
:
2334 case BUILT_IN_MEMCPY_CHK
:
2335 case BUILT_IN_MEMPCPY_CHK
:
2336 case BUILT_IN_MEMMOVE_CHK
:
2337 case BUILT_IN_MEMSET_CHK
:
2339 /* For a must-alias check we need to be able to constrain
2340 the access properly. */
2341 if (ref
->max_size
== -1)
2343 tree dest
= gimple_call_arg (stmt
, 0);
2344 tree len
= gimple_call_arg (stmt
, 2);
2345 if (!tree_fits_shwi_p (len
))
2347 tree rbase
= ref
->base
;
2348 offset_int roffset
= ref
->offset
;
2350 ao_ref_init_from_ptr_and_size (&dref
, dest
, len
);
2351 tree base
= ao_ref_base (&dref
);
2352 offset_int offset
= dref
.offset
;
2353 if (!base
|| dref
.size
== -1)
2355 if (TREE_CODE (base
) == MEM_REF
)
2357 if (TREE_CODE (rbase
) != MEM_REF
)
2359 // Compare pointers.
2360 offset
+= wi::lshift (mem_ref_offset (base
),
2361 LOG2_BITS_PER_UNIT
);
2362 roffset
+= wi::lshift (mem_ref_offset (rbase
),
2363 LOG2_BITS_PER_UNIT
);
2364 base
= TREE_OPERAND (base
, 0);
2365 rbase
= TREE_OPERAND (rbase
, 0);
2368 && wi::les_p (offset
, roffset
)
2369 && wi::les_p (roffset
+ ref
->max_size
,
2370 offset
+ wi::lshift (wi::to_offset (len
),
2371 LOG2_BITS_PER_UNIT
)))
2376 case BUILT_IN_VA_END
:
2378 tree ptr
= gimple_call_arg (stmt
, 0);
2379 if (TREE_CODE (ptr
) == ADDR_EXPR
)
2381 tree base
= ao_ref_base (ref
);
2382 if (TREE_OPERAND (ptr
, 0) == base
)
2395 stmt_kills_ref_p (gimple stmt
, tree ref
)
2398 ao_ref_init (&r
, ref
);
2399 return stmt_kills_ref_p (stmt
, &r
);
2403 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2404 TARGET or a statement clobbering the memory reference REF in which
2405 case false is returned. The walk starts with VUSE, one argument of PHI. */
2408 maybe_skip_until (gimple phi
, tree target
, ao_ref
*ref
,
2409 tree vuse
, unsigned int *cnt
, bitmap
*visited
,
2410 bool abort_on_visited
,
2411 void *(*translate
)(ao_ref
*, tree
, void *, bool),
2414 basic_block bb
= gimple_bb (phi
);
2417 *visited
= BITMAP_ALLOC (NULL
);
2419 bitmap_set_bit (*visited
, SSA_NAME_VERSION (PHI_RESULT (phi
)));
2421 /* Walk until we hit the target. */
2422 while (vuse
!= target
)
2424 gimple def_stmt
= SSA_NAME_DEF_STMT (vuse
);
2425 /* Recurse for PHI nodes. */
2426 if (gimple_code (def_stmt
) == GIMPLE_PHI
)
2428 /* An already visited PHI node ends the walk successfully. */
2429 if (bitmap_bit_p (*visited
, SSA_NAME_VERSION (PHI_RESULT (def_stmt
))))
2430 return !abort_on_visited
;
2431 vuse
= get_continuation_for_phi (def_stmt
, ref
, cnt
,
2432 visited
, abort_on_visited
,
2438 else if (gimple_nop_p (def_stmt
))
2442 /* A clobbering statement or the end of the IL ends it failing. */
2444 if (stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
2447 && (*translate
) (ref
, vuse
, data
, true) == NULL
)
2453 /* If we reach a new basic-block see if we already skipped it
2454 in a previous walk that ended successfully. */
2455 if (gimple_bb (def_stmt
) != bb
)
2457 if (!bitmap_set_bit (*visited
, SSA_NAME_VERSION (vuse
)))
2458 return !abort_on_visited
;
2459 bb
= gimple_bb (def_stmt
);
2461 vuse
= gimple_vuse (def_stmt
);
2466 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2467 until we hit the phi argument definition that dominates the other one.
2468 Return that, or NULL_TREE if there is no such definition. */
2471 get_continuation_for_phi_1 (gimple phi
, tree arg0
, tree arg1
,
2472 ao_ref
*ref
, unsigned int *cnt
,
2473 bitmap
*visited
, bool abort_on_visited
,
2474 void *(*translate
)(ao_ref
*, tree
, void *, bool),
2477 gimple def0
= SSA_NAME_DEF_STMT (arg0
);
2478 gimple def1
= SSA_NAME_DEF_STMT (arg1
);
2483 else if (gimple_nop_p (def0
)
2484 || (!gimple_nop_p (def1
)
2485 && dominated_by_p (CDI_DOMINATORS
,
2486 gimple_bb (def1
), gimple_bb (def0
))))
2488 if (maybe_skip_until (phi
, arg0
, ref
, arg1
, cnt
,
2489 visited
, abort_on_visited
, translate
, data
))
2492 else if (gimple_nop_p (def1
)
2493 || dominated_by_p (CDI_DOMINATORS
,
2494 gimple_bb (def0
), gimple_bb (def1
)))
2496 if (maybe_skip_until (phi
, arg1
, ref
, arg0
, cnt
,
2497 visited
, abort_on_visited
, translate
, data
))
2500 /* Special case of a diamond:
2502 goto (cond) ? L1 : L2
2503 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2505 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2506 L3: MEM_4 = PHI<MEM_2, MEM_3>
2507 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2508 dominate each other, but still we can easily skip this PHI node
2509 if we recognize that the vuse MEM operand is the same for both,
2510 and that we can skip both statements (they don't clobber us).
2511 This is still linear. Don't use maybe_skip_until, that might
2512 potentially be slow. */
2513 else if ((common_vuse
= gimple_vuse (def0
))
2514 && common_vuse
== gimple_vuse (def1
))
2517 if ((!stmt_may_clobber_ref_p_1 (def0
, ref
)
2519 && (*translate
) (ref
, arg0
, data
, true) == NULL
))
2520 && (!stmt_may_clobber_ref_p_1 (def1
, ref
)
2522 && (*translate
) (ref
, arg1
, data
, true) == NULL
)))
2530 /* Starting from a PHI node for the virtual operand of the memory reference
2531 REF find a continuation virtual operand that allows to continue walking
2532 statements dominating PHI skipping only statements that cannot possibly
2533 clobber REF. Increments *CNT for each alias disambiguation done.
2534 Returns NULL_TREE if no suitable virtual operand can be found. */
2537 get_continuation_for_phi (gimple phi
, ao_ref
*ref
,
2538 unsigned int *cnt
, bitmap
*visited
,
2539 bool abort_on_visited
,
2540 void *(*translate
)(ao_ref
*, tree
, void *, bool),
2543 unsigned nargs
= gimple_phi_num_args (phi
);
2545 /* Through a single-argument PHI we can simply look through. */
2547 return PHI_ARG_DEF (phi
, 0);
2549 /* For two or more arguments try to pairwise skip non-aliasing code
2550 until we hit the phi argument definition that dominates the other one. */
2551 else if (nargs
>= 2)
2556 /* Find a candidate for the virtual operand which definition
2557 dominates those of all others. */
2558 arg0
= PHI_ARG_DEF (phi
, 0);
2559 if (!SSA_NAME_IS_DEFAULT_DEF (arg0
))
2560 for (i
= 1; i
< nargs
; ++i
)
2562 arg1
= PHI_ARG_DEF (phi
, i
);
2563 if (SSA_NAME_IS_DEFAULT_DEF (arg1
))
2568 if (dominated_by_p (CDI_DOMINATORS
,
2569 gimple_bb (SSA_NAME_DEF_STMT (arg0
)),
2570 gimple_bb (SSA_NAME_DEF_STMT (arg1
))))
2574 /* Then pairwise reduce against the found candidate. */
2575 for (i
= 0; i
< nargs
; ++i
)
2577 arg1
= PHI_ARG_DEF (phi
, i
);
2578 arg0
= get_continuation_for_phi_1 (phi
, arg0
, arg1
, ref
,
2579 cnt
, visited
, abort_on_visited
,
2591 /* Based on the memory reference REF and its virtual use VUSE call
2592 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2593 itself. That is, for each virtual use for which its defining statement
2594 does not clobber REF.
2596 WALKER is called with REF, the current virtual use and DATA. If
2597 WALKER returns non-NULL the walk stops and its result is returned.
2598 At the end of a non-successful walk NULL is returned.
2600 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2601 use which definition is a statement that may clobber REF and DATA.
2602 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2603 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2604 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2605 to adjust REF and *DATA to make that valid.
2607 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2610 walk_non_aliased_vuses (ao_ref
*ref
, tree vuse
,
2611 void *(*walker
)(ao_ref
*, tree
, unsigned int, void *),
2612 void *(*translate
)(ao_ref
*, tree
, void *, bool),
2615 bitmap visited
= NULL
;
2617 unsigned int cnt
= 0;
2618 bool translated
= false;
2620 timevar_push (TV_ALIAS_STMT_WALK
);
2626 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2627 res
= (*walker
) (ref
, vuse
, cnt
, data
);
2629 if (res
== (void *)-1)
2634 /* Lookup succeeded. */
2635 else if (res
!= NULL
)
2638 def_stmt
= SSA_NAME_DEF_STMT (vuse
);
2639 if (gimple_nop_p (def_stmt
))
2641 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
2642 vuse
= get_continuation_for_phi (def_stmt
, ref
, &cnt
,
2643 &visited
, translated
, translate
, data
);
2647 if (stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
2651 res
= (*translate
) (ref
, vuse
, data
, false);
2652 /* Failed lookup and translation. */
2653 if (res
== (void *)-1)
2658 /* Lookup succeeded. */
2659 else if (res
!= NULL
)
2661 /* Translation succeeded, continue walking. */
2664 vuse
= gimple_vuse (def_stmt
);
2670 BITMAP_FREE (visited
);
2672 timevar_pop (TV_ALIAS_STMT_WALK
);
2678 /* Based on the memory reference REF call WALKER for each vdef which
2679 defining statement may clobber REF, starting with VDEF. If REF
2680 is NULL_TREE, each defining statement is visited.
2682 WALKER is called with REF, the current vdef and DATA. If WALKER
2683 returns true the walk is stopped, otherwise it continues.
2685 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2686 The pointer may be NULL and then we do not track this information.
2688 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2689 PHI argument (but only one walk continues on merge points), the
2690 return value is true if any of the walks was successful.
2692 The function returns the number of statements walked. */
2695 walk_aliased_vdefs_1 (ao_ref
*ref
, tree vdef
,
2696 bool (*walker
)(ao_ref
*, tree
, void *), void *data
,
2697 bitmap
*visited
, unsigned int cnt
,
2698 bool *function_entry_reached
)
2702 gimple def_stmt
= SSA_NAME_DEF_STMT (vdef
);
2705 && !bitmap_set_bit (*visited
, SSA_NAME_VERSION (vdef
)))
2708 if (gimple_nop_p (def_stmt
))
2710 if (function_entry_reached
)
2711 *function_entry_reached
= true;
2714 else if (gimple_code (def_stmt
) == GIMPLE_PHI
)
2718 *visited
= BITMAP_ALLOC (NULL
);
2719 for (i
= 0; i
< gimple_phi_num_args (def_stmt
); ++i
)
2720 cnt
+= walk_aliased_vdefs_1 (ref
, gimple_phi_arg_def (def_stmt
, i
),
2721 walker
, data
, visited
, 0,
2722 function_entry_reached
);
2726 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2729 || stmt_may_clobber_ref_p_1 (def_stmt
, ref
))
2730 && (*walker
) (ref
, vdef
, data
))
2733 vdef
= gimple_vuse (def_stmt
);
2739 walk_aliased_vdefs (ao_ref
*ref
, tree vdef
,
2740 bool (*walker
)(ao_ref
*, tree
, void *), void *data
,
2742 bool *function_entry_reached
)
2744 bitmap local_visited
= NULL
;
2747 timevar_push (TV_ALIAS_STMT_WALK
);
2749 if (function_entry_reached
)
2750 *function_entry_reached
= false;
2752 ret
= walk_aliased_vdefs_1 (ref
, vdef
, walker
, data
,
2753 visited
? visited
: &local_visited
, 0,
2754 function_entry_reached
);
2756 BITMAP_FREE (local_visited
);
2758 timevar_pop (TV_ALIAS_STMT_WALK
);