* testsuite/17_intro/static.cc: Ignore AIX TOC reload warnings.
[official-gcc.git] / gcc / tree-ssa-alias.c
blobef9fbfecdc9173462f02b7572421bdd1dd3e1a36
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2013 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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "target.h"
28 #include "basic-block.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "tree-pretty-print.h"
35 #include "dumpfile.h"
36 #include "gimple.h"
37 #include "gimple-ssa.h"
38 #include "stringpool.h"
39 #include "tree-ssanames.h"
40 #include "expr.h"
41 #include "tree-dfa.h"
42 #include "tree-inline.h"
43 #include "params.h"
44 #include "vec.h"
45 #include "pointer-set.h"
46 #include "alloc-pool.h"
47 #include "tree-ssa-alias.h"
48 #include "ipa-reference.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
92 alias global memory.
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. */
102 static struct {
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;
109 } alias_stats;
111 void
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. */
138 bool
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
144 to global memory. */
145 if (TREE_CODE (ptr) != SSA_NAME)
146 return true;
148 pi = SSA_NAME_PTR_INFO (ptr);
150 /* If we do not have points-to information for this variable,
151 we have to punt. */
152 if (!pi)
153 return true;
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. */
163 static bool
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. */
170 STRIP_NOPS (ptr);
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))
180 return true;
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));
198 if (base
199 && (TREE_CODE (base) == MEM_REF
200 || TREE_CODE (base) == TARGET_MEM_REF))
201 ptr = TREE_OPERAND (base, 0);
202 else if (base
203 && DECL_P (base))
204 return base == decl;
205 else if (base
206 && CONSTANT_CLASS_P (base))
207 return false;
208 else
209 return true;
212 /* Non-aliased variables can not be pointed to. */
213 if (!may_be_aliased (decl))
214 return false;
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);
219 if (!pi)
220 return true;
222 return pt_solution_includes (&pi->pt, decl);
225 /* Return true if dereferenced PTR1 and PTR2 may alias.
226 The caller is responsible for applying TBAA to see if accesses
227 through PTR1 and PTR2 may conflict at all. */
229 bool
230 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
232 struct ptr_info_def *pi1, *pi2;
234 /* Conversions are irrelevant for points-to information and
235 data-dependence analysis can feed us those. */
236 STRIP_NOPS (ptr1);
237 STRIP_NOPS (ptr2);
239 /* Disregard pointer offsetting. */
240 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
244 ptr1 = TREE_OPERAND (ptr1, 0);
246 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
247 return ptr_derefs_may_alias_p (ptr1, ptr2);
249 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
253 ptr2 = TREE_OPERAND (ptr2, 0);
255 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
256 return ptr_derefs_may_alias_p (ptr1, ptr2);
259 /* ADDR_EXPR pointers either just offset another pointer or directly
260 specify the pointed-to set. */
261 if (TREE_CODE (ptr1) == ADDR_EXPR)
263 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
264 if (base
265 && (TREE_CODE (base) == MEM_REF
266 || TREE_CODE (base) == TARGET_MEM_REF))
267 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
268 else if (base
269 && DECL_P (base))
270 return ptr_deref_may_alias_decl_p (ptr2, base);
271 else
272 return true;
274 if (TREE_CODE (ptr2) == ADDR_EXPR)
276 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
277 if (base
278 && (TREE_CODE (base) == MEM_REF
279 || TREE_CODE (base) == TARGET_MEM_REF))
280 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
281 else if (base
282 && DECL_P (base))
283 return ptr_deref_may_alias_decl_p (ptr1, base);
284 else
285 return true;
288 /* From here we require SSA name pointers. Anything else aliases. */
289 if (TREE_CODE (ptr1) != SSA_NAME
290 || TREE_CODE (ptr2) != SSA_NAME
291 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
292 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
293 return true;
295 /* We may end up with two empty points-to solutions for two same pointers.
296 In this case we still want to say both pointers alias, so shortcut
297 that here. */
298 if (ptr1 == ptr2)
299 return true;
301 /* If we do not have useful points-to information for either pointer
302 we cannot disambiguate anything else. */
303 pi1 = SSA_NAME_PTR_INFO (ptr1);
304 pi2 = SSA_NAME_PTR_INFO (ptr2);
305 if (!pi1 || !pi2)
306 return true;
308 /* ??? This does not use TBAA to prune decls from the intersection
309 that not both pointers may access. */
310 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
313 /* Return true if dereferencing PTR may alias *REF.
314 The caller is responsible for applying TBAA to see if PTR
315 may access *REF at all. */
317 static bool
318 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
320 tree base = ao_ref_base (ref);
322 if (TREE_CODE (base) == MEM_REF
323 || TREE_CODE (base) == TARGET_MEM_REF)
324 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
325 else if (DECL_P (base))
326 return ptr_deref_may_alias_decl_p (ptr, base);
328 return true;
331 /* Return true whether REF may refer to global memory. */
333 bool
334 ref_may_alias_global_p (tree ref)
336 tree base = get_base_address (ref);
337 if (DECL_P (base))
338 return is_global_var (base);
339 else if (TREE_CODE (base) == MEM_REF
340 || TREE_CODE (base) == TARGET_MEM_REF)
341 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
342 return true;
345 /* Return true whether STMT may clobber global memory. */
347 bool
348 stmt_may_clobber_global_p (gimple stmt)
350 tree lhs;
352 if (!gimple_vdef (stmt))
353 return false;
355 /* ??? We can ask the oracle whether an artificial pointer
356 dereference with a pointer with points-to information covering
357 all global memory (what about non-address taken memory?) maybe
358 clobbered by this call. As there is at the moment no convenient
359 way of doing that without generating garbage do some manual
360 checking instead.
361 ??? We could make a NULL ao_ref argument to the various
362 predicates special, meaning any global memory. */
364 switch (gimple_code (stmt))
366 case GIMPLE_ASSIGN:
367 lhs = gimple_assign_lhs (stmt);
368 return (TREE_CODE (lhs) != SSA_NAME
369 && ref_may_alias_global_p (lhs));
370 case GIMPLE_CALL:
371 return true;
372 default:
373 return true;
378 /* Dump alias information on FILE. */
380 void
381 dump_alias_info (FILE *file)
383 unsigned i;
384 const char *funcname
385 = lang_hooks.decl_printable_name (current_function_decl, 2);
386 tree var;
388 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
390 fprintf (file, "Aliased symbols\n\n");
392 FOR_EACH_LOCAL_DECL (cfun, i, var)
394 if (may_be_aliased (var))
395 dump_variable (file, var);
398 fprintf (file, "\nCall clobber information\n");
400 fprintf (file, "\nESCAPED");
401 dump_points_to_solution (file, &cfun->gimple_df->escaped);
403 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
405 for (i = 1; i < num_ssa_names; i++)
407 tree ptr = ssa_name (i);
408 struct ptr_info_def *pi;
410 if (ptr == NULL_TREE
411 || !POINTER_TYPE_P (TREE_TYPE (ptr))
412 || SSA_NAME_IN_FREE_LIST (ptr))
413 continue;
415 pi = SSA_NAME_PTR_INFO (ptr);
416 if (pi)
417 dump_points_to_info_for (file, ptr);
420 fprintf (file, "\n");
424 /* Dump alias information on stderr. */
426 DEBUG_FUNCTION void
427 debug_alias_info (void)
429 dump_alias_info (stderr);
433 /* Dump the points-to set *PT into FILE. */
435 void
436 dump_points_to_solution (FILE *file, struct pt_solution *pt)
438 if (pt->anything)
439 fprintf (file, ", points-to anything");
441 if (pt->nonlocal)
442 fprintf (file, ", points-to non-local");
444 if (pt->escaped)
445 fprintf (file, ", points-to escaped");
447 if (pt->ipa_escaped)
448 fprintf (file, ", points-to unit escaped");
450 if (pt->null)
451 fprintf (file, ", points-to NULL");
453 if (pt->vars)
455 fprintf (file, ", points-to vars: ");
456 dump_decl_set (file, pt->vars);
457 if (pt->vars_contains_nonlocal
458 && pt->vars_contains_escaped_heap)
459 fprintf (file, " (nonlocal, escaped heap)");
460 else if (pt->vars_contains_nonlocal
461 && pt->vars_contains_escaped)
462 fprintf (file, " (nonlocal, escaped)");
463 else if (pt->vars_contains_nonlocal)
464 fprintf (file, " (nonlocal)");
465 else if (pt->vars_contains_escaped_heap)
466 fprintf (file, " (escaped heap)");
467 else if (pt->vars_contains_escaped)
468 fprintf (file, " (escaped)");
473 /* Unified dump function for pt_solution. */
475 DEBUG_FUNCTION void
476 debug (pt_solution &ref)
478 dump_points_to_solution (stderr, &ref);
481 DEBUG_FUNCTION void
482 debug (pt_solution *ptr)
484 if (ptr)
485 debug (*ptr);
486 else
487 fprintf (stderr, "<nil>\n");
491 /* Dump points-to information for SSA_NAME PTR into FILE. */
493 void
494 dump_points_to_info_for (FILE *file, tree ptr)
496 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
498 print_generic_expr (file, ptr, dump_flags);
500 if (pi)
501 dump_points_to_solution (file, &pi->pt);
502 else
503 fprintf (file, ", points-to anything");
505 fprintf (file, "\n");
509 /* Dump points-to information for VAR into stderr. */
511 DEBUG_FUNCTION void
512 debug_points_to_info_for (tree var)
514 dump_points_to_info_for (stderr, var);
518 /* Initializes the alias-oracle reference representation *R from REF. */
520 void
521 ao_ref_init (ao_ref *r, tree ref)
523 r->ref = ref;
524 r->base = NULL_TREE;
525 r->offset = 0;
526 r->size = -1;
527 r->max_size = -1;
528 r->ref_alias_set = -1;
529 r->base_alias_set = -1;
530 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
533 /* Returns the base object of the memory reference *REF. */
535 tree
536 ao_ref_base (ao_ref *ref)
538 if (ref->base)
539 return ref->base;
540 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
541 &ref->max_size);
542 return ref->base;
545 /* Returns the base object alias set of the memory reference *REF. */
547 static alias_set_type
548 ao_ref_base_alias_set (ao_ref *ref)
550 tree base_ref;
551 if (ref->base_alias_set != -1)
552 return ref->base_alias_set;
553 if (!ref->ref)
554 return 0;
555 base_ref = ref->ref;
556 while (handled_component_p (base_ref))
557 base_ref = TREE_OPERAND (base_ref, 0);
558 ref->base_alias_set = get_alias_set (base_ref);
559 return ref->base_alias_set;
562 /* Returns the reference alias set of the memory reference *REF. */
564 alias_set_type
565 ao_ref_alias_set (ao_ref *ref)
567 if (ref->ref_alias_set != -1)
568 return ref->ref_alias_set;
569 ref->ref_alias_set = get_alias_set (ref->ref);
570 return ref->ref_alias_set;
573 /* Init an alias-oracle reference representation from a gimple pointer
574 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
575 size is assumed to be unknown. The access is assumed to be only
576 to or after of the pointer target, not before it. */
578 void
579 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
581 HOST_WIDE_INT t, extra_offset = 0;
582 ref->ref = NULL_TREE;
583 if (TREE_CODE (ptr) == SSA_NAME)
585 gimple stmt = SSA_NAME_DEF_STMT (ptr);
586 if (gimple_assign_single_p (stmt)
587 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
588 ptr = gimple_assign_rhs1 (stmt);
589 else if (is_gimple_assign (stmt)
590 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
591 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
593 ptr = gimple_assign_rhs1 (stmt);
594 extra_offset = BITS_PER_UNIT
595 * int_cst_value (gimple_assign_rhs2 (stmt));
599 if (TREE_CODE (ptr) == ADDR_EXPR)
601 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
602 if (ref->base)
603 ref->offset = BITS_PER_UNIT * t;
604 else
606 size = NULL_TREE;
607 ref->offset = 0;
608 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
611 else
613 ref->base = build2 (MEM_REF, char_type_node,
614 ptr, null_pointer_node);
615 ref->offset = 0;
617 ref->offset += extra_offset;
618 if (size
619 && tree_fits_shwi_p (size)
620 && TREE_INT_CST_LOW (size) * BITS_PER_UNIT / BITS_PER_UNIT
621 == TREE_INT_CST_LOW (size))
622 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * BITS_PER_UNIT;
623 else
624 ref->max_size = ref->size = -1;
625 ref->ref_alias_set = 0;
626 ref->base_alias_set = 0;
627 ref->volatile_p = false;
630 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
631 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
632 decide. */
634 static inline int
635 same_type_for_tbaa (tree type1, tree type2)
637 type1 = TYPE_MAIN_VARIANT (type1);
638 type2 = TYPE_MAIN_VARIANT (type2);
640 /* If we would have to do structural comparison bail out. */
641 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
642 || TYPE_STRUCTURAL_EQUALITY_P (type2))
643 return -1;
645 /* Compare the canonical types. */
646 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
647 return 1;
649 /* ??? Array types are not properly unified in all cases as we have
650 spurious changes in the index types for example. Removing this
651 causes all sorts of problems with the Fortran frontend. */
652 if (TREE_CODE (type1) == ARRAY_TYPE
653 && TREE_CODE (type2) == ARRAY_TYPE)
654 return -1;
656 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
657 object of one of its constrained subtypes, e.g. when a function with an
658 unconstrained parameter passed by reference is called on an object and
659 inlined. But, even in the case of a fixed size, type and subtypes are
660 not equivalent enough as to share the same TYPE_CANONICAL, since this
661 would mean that conversions between them are useless, whereas they are
662 not (e.g. type and subtypes can have different modes). So, in the end,
663 they are only guaranteed to have the same alias set. */
664 if (get_alias_set (type1) == get_alias_set (type2))
665 return -1;
667 /* The types are known to be not equal. */
668 return 0;
671 /* Determine if the two component references REF1 and REF2 which are
672 based on access types TYPE1 and TYPE2 and of which at least one is based
673 on an indirect reference may alias. REF2 is the only one that can
674 be a decl in which case REF2_IS_DECL is true.
675 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
676 are the respective alias sets. */
678 static bool
679 aliasing_component_refs_p (tree ref1,
680 alias_set_type ref1_alias_set,
681 alias_set_type base1_alias_set,
682 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
683 tree ref2,
684 alias_set_type ref2_alias_set,
685 alias_set_type base2_alias_set,
686 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
687 bool ref2_is_decl)
689 /* If one reference is a component references through pointers try to find a
690 common base and apply offset based disambiguation. This handles
691 for example
692 struct A { int i; int j; } *q;
693 struct B { struct A a; int k; } *p;
694 disambiguating q->i and p->a.j. */
695 tree base1, base2;
696 tree type1, type2;
697 tree *refp;
698 int same_p;
700 /* Choose bases and base types to search for. */
701 base1 = ref1;
702 while (handled_component_p (base1))
703 base1 = TREE_OPERAND (base1, 0);
704 type1 = TREE_TYPE (base1);
705 base2 = ref2;
706 while (handled_component_p (base2))
707 base2 = TREE_OPERAND (base2, 0);
708 type2 = TREE_TYPE (base2);
710 /* Now search for the type1 in the access path of ref2. This
711 would be a common base for doing offset based disambiguation on. */
712 refp = &ref2;
713 while (handled_component_p (*refp)
714 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
715 refp = &TREE_OPERAND (*refp, 0);
716 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
717 /* If we couldn't compare types we have to bail out. */
718 if (same_p == -1)
719 return true;
720 else if (same_p == 1)
722 HOST_WIDE_INT offadj, sztmp, msztmp;
723 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
724 offset2 -= offadj;
725 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
726 offset1 -= offadj;
727 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
729 /* If we didn't find a common base, try the other way around. */
730 refp = &ref1;
731 while (handled_component_p (*refp)
732 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
733 refp = &TREE_OPERAND (*refp, 0);
734 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
735 /* If we couldn't compare types we have to bail out. */
736 if (same_p == -1)
737 return true;
738 else if (same_p == 1)
740 HOST_WIDE_INT offadj, sztmp, msztmp;
741 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
742 offset1 -= offadj;
743 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
744 offset2 -= offadj;
745 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
748 /* If we have two type access paths B1.path1 and B2.path2 they may
749 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
750 But we can still have a path that goes B1.path1...B2.path2 with
751 a part that we do not see. So we can only disambiguate now
752 if there is no B2 in the tail of path1 and no B1 on the
753 tail of path2. */
754 if (base1_alias_set == ref2_alias_set
755 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
756 return true;
757 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
758 if (!ref2_is_decl)
759 return (base2_alias_set == ref1_alias_set
760 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
761 return false;
764 /* Return true if we can determine that component references REF1 and REF2,
765 that are within a common DECL, cannot overlap. */
767 static bool
768 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
770 stack_vec<tree, 16> component_refs1;
771 stack_vec<tree, 16> component_refs2;
773 /* Create the stack of handled components for REF1. */
774 while (handled_component_p (ref1))
776 component_refs1.safe_push (ref1);
777 ref1 = TREE_OPERAND (ref1, 0);
779 if (TREE_CODE (ref1) == MEM_REF)
781 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
782 goto may_overlap;
783 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
786 /* Create the stack of handled components for REF2. */
787 while (handled_component_p (ref2))
789 component_refs2.safe_push (ref2);
790 ref2 = TREE_OPERAND (ref2, 0);
792 if (TREE_CODE (ref2) == MEM_REF)
794 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
795 goto may_overlap;
796 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
799 /* We must have the same base DECL. */
800 gcc_assert (ref1 == ref2);
802 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
803 rank. This is sufficient because we start from the same DECL and you
804 cannot reference several fields at a time with COMPONENT_REFs (unlike
805 with ARRAY_RANGE_REFs for arrays) so you always need the same number
806 of them to access a sub-component, unless you're in a union, in which
807 case the return value will precisely be false. */
808 while (true)
812 if (component_refs1.is_empty ())
813 goto may_overlap;
814 ref1 = component_refs1.pop ();
816 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
820 if (component_refs2.is_empty ())
821 goto may_overlap;
822 ref2 = component_refs2.pop ();
824 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
826 /* Beware of BIT_FIELD_REF. */
827 if (TREE_CODE (ref1) != COMPONENT_REF
828 || TREE_CODE (ref2) != COMPONENT_REF)
829 goto may_overlap;
831 tree field1 = TREE_OPERAND (ref1, 1);
832 tree field2 = TREE_OPERAND (ref2, 1);
834 /* ??? We cannot simply use the type of operand #0 of the refs here
835 as the Fortran compiler smuggles type punning into COMPONENT_REFs
836 for common blocks instead of using unions like everyone else. */
837 tree type1 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field1));
838 tree type2 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field2));
840 /* We cannot disambiguate fields in a union or qualified union. */
841 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
842 goto may_overlap;
844 /* Different fields of the same record type cannot overlap.
845 ??? Bitfields can overlap at RTL level so punt on them. */
846 if (field1 != field2)
848 component_refs1.release ();
849 component_refs2.release ();
850 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
854 may_overlap:
855 component_refs1.release ();
856 component_refs2.release ();
857 return false;
860 /* Return true if two memory references based on the variables BASE1
861 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
862 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
863 if non-NULL are the complete memory reference trees. */
865 static bool
866 decl_refs_may_alias_p (tree ref1, tree base1,
867 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
868 tree ref2, tree base2,
869 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
871 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
873 /* If both references are based on different variables, they cannot alias. */
874 if (base1 != base2)
875 return false;
877 /* If both references are based on the same variable, they cannot alias if
878 the accesses do not overlap. */
879 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
880 return false;
882 /* For components with variable position, the above test isn't sufficient,
883 so we disambiguate component references manually. */
884 if (ref1 && ref2
885 && handled_component_p (ref1) && handled_component_p (ref2)
886 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
887 return false;
889 return true;
892 /* Return true if an indirect reference based on *PTR1 constrained
893 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
894 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
895 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
896 in which case they are computed on-demand. REF1 and REF2
897 if non-NULL are the complete memory reference trees. */
899 static bool
900 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
901 HOST_WIDE_INT offset1,
902 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
903 alias_set_type ref1_alias_set,
904 alias_set_type base1_alias_set,
905 tree ref2 ATTRIBUTE_UNUSED, tree base2,
906 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
907 alias_set_type ref2_alias_set,
908 alias_set_type base2_alias_set, bool tbaa_p)
910 tree ptr1;
911 tree ptrtype1, dbase2;
912 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
913 HOST_WIDE_INT doffset1, doffset2;
914 double_int moff;
916 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
917 || TREE_CODE (base1) == TARGET_MEM_REF)
918 && DECL_P (base2));
920 ptr1 = TREE_OPERAND (base1, 0);
922 /* The offset embedded in MEM_REFs can be negative. Bias them
923 so that the resulting offset adjustment is positive. */
924 moff = mem_ref_offset (base1);
925 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
926 if (moff.is_negative ())
927 offset2p += (-moff).low;
928 else
929 offset1p += moff.low;
931 /* If only one reference is based on a variable, they cannot alias if
932 the pointer access is beyond the extent of the variable access.
933 (the pointer base cannot validly point to an offset less than zero
934 of the variable).
935 ??? IVOPTs creates bases that do not honor this restriction,
936 so do not apply this optimization for TARGET_MEM_REFs. */
937 if (TREE_CODE (base1) != TARGET_MEM_REF
938 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
939 return false;
940 /* They also cannot alias if the pointer may not point to the decl. */
941 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
942 return false;
944 /* Disambiguations that rely on strict aliasing rules follow. */
945 if (!flag_strict_aliasing || !tbaa_p)
946 return true;
948 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
950 /* If the alias set for a pointer access is zero all bets are off. */
951 if (base1_alias_set == -1)
952 base1_alias_set = get_deref_alias_set (ptrtype1);
953 if (base1_alias_set == 0)
954 return true;
955 if (base2_alias_set == -1)
956 base2_alias_set = get_alias_set (base2);
958 /* When we are trying to disambiguate an access with a pointer dereference
959 as base versus one with a decl as base we can use both the size
960 of the decl and its dynamic type for extra disambiguation.
961 ??? We do not know anything about the dynamic type of the decl
962 other than that its alias-set contains base2_alias_set as a subset
963 which does not help us here. */
964 /* As we know nothing useful about the dynamic type of the decl just
965 use the usual conflict check rather than a subset test.
966 ??? We could introduce -fvery-strict-aliasing when the language
967 does not allow decls to have a dynamic type that differs from their
968 static type. Then we can check
969 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
970 if (base1_alias_set != base2_alias_set
971 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
972 return false;
973 /* If the size of the access relevant for TBAA through the pointer
974 is bigger than the size of the decl we can't possibly access the
975 decl via that pointer. */
976 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
977 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
978 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
979 /* ??? This in turn may run afoul when a decl of type T which is
980 a member of union type U is accessed through a pointer to
981 type U and sizeof T is smaller than sizeof U. */
982 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
983 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
984 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
985 return false;
987 if (!ref2)
988 return true;
990 /* If the decl is accessed via a MEM_REF, reconstruct the base
991 we can use for TBAA and an appropriately adjusted offset. */
992 dbase2 = ref2;
993 while (handled_component_p (dbase2))
994 dbase2 = TREE_OPERAND (dbase2, 0);
995 doffset1 = offset1;
996 doffset2 = offset2;
997 if (TREE_CODE (dbase2) == MEM_REF
998 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1000 double_int moff = mem_ref_offset (dbase2);
1001 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1002 if (moff.is_negative ())
1003 doffset1 -= (-moff).low;
1004 else
1005 doffset2 -= moff.low;
1008 /* If either reference is view-converted, give up now. */
1009 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1010 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1011 return true;
1013 /* If both references are through the same type, they do not alias
1014 if the accesses do not overlap. This does extra disambiguation
1015 for mixed/pointer accesses but requires strict aliasing.
1016 For MEM_REFs we require that the component-ref offset we computed
1017 is relative to the start of the type which we ensure by
1018 comparing rvalue and access type and disregarding the constant
1019 pointer offset. */
1020 if ((TREE_CODE (base1) != TARGET_MEM_REF
1021 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1022 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1023 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1025 /* Do access-path based disambiguation. */
1026 if (ref1 && ref2
1027 && (handled_component_p (ref1) || handled_component_p (ref2)))
1028 return aliasing_component_refs_p (ref1,
1029 ref1_alias_set, base1_alias_set,
1030 offset1, max_size1,
1031 ref2,
1032 ref2_alias_set, base2_alias_set,
1033 offset2, max_size2, true);
1035 return true;
1038 /* Return true if two indirect references based on *PTR1
1039 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1040 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1041 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1042 in which case they are computed on-demand. REF1 and REF2
1043 if non-NULL are the complete memory reference trees. */
1045 static bool
1046 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1047 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1048 alias_set_type ref1_alias_set,
1049 alias_set_type base1_alias_set,
1050 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1051 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1052 alias_set_type ref2_alias_set,
1053 alias_set_type base2_alias_set, bool tbaa_p)
1055 tree ptr1;
1056 tree ptr2;
1057 tree ptrtype1, ptrtype2;
1059 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1060 || TREE_CODE (base1) == TARGET_MEM_REF)
1061 && (TREE_CODE (base2) == MEM_REF
1062 || TREE_CODE (base2) == TARGET_MEM_REF));
1064 ptr1 = TREE_OPERAND (base1, 0);
1065 ptr2 = TREE_OPERAND (base2, 0);
1067 /* If both bases are based on pointers they cannot alias if they may not
1068 point to the same memory object or if they point to the same object
1069 and the accesses do not overlap. */
1070 if ((!cfun || gimple_in_ssa_p (cfun))
1071 && operand_equal_p (ptr1, ptr2, 0)
1072 && (((TREE_CODE (base1) != TARGET_MEM_REF
1073 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1074 && (TREE_CODE (base2) != TARGET_MEM_REF
1075 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1076 || (TREE_CODE (base1) == TARGET_MEM_REF
1077 && TREE_CODE (base2) == TARGET_MEM_REF
1078 && (TMR_STEP (base1) == TMR_STEP (base2)
1079 || (TMR_STEP (base1) && TMR_STEP (base2)
1080 && operand_equal_p (TMR_STEP (base1),
1081 TMR_STEP (base2), 0)))
1082 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1083 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1084 && operand_equal_p (TMR_INDEX (base1),
1085 TMR_INDEX (base2), 0)))
1086 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1087 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1088 && operand_equal_p (TMR_INDEX2 (base1),
1089 TMR_INDEX2 (base2), 0))))))
1091 double_int moff;
1092 /* The offset embedded in MEM_REFs can be negative. Bias them
1093 so that the resulting offset adjustment is positive. */
1094 moff = mem_ref_offset (base1);
1095 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1096 if (moff.is_negative ())
1097 offset2 += (-moff).low;
1098 else
1099 offset1 += moff.low;
1100 moff = mem_ref_offset (base2);
1101 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1102 if (moff.is_negative ())
1103 offset1 += (-moff).low;
1104 else
1105 offset2 += moff.low;
1106 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1108 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1109 return false;
1111 /* Disambiguations that rely on strict aliasing rules follow. */
1112 if (!flag_strict_aliasing || !tbaa_p)
1113 return true;
1115 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1116 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1118 /* If the alias set for a pointer access is zero all bets are off. */
1119 if (base1_alias_set == -1)
1120 base1_alias_set = get_deref_alias_set (ptrtype1);
1121 if (base1_alias_set == 0)
1122 return true;
1123 if (base2_alias_set == -1)
1124 base2_alias_set = get_deref_alias_set (ptrtype2);
1125 if (base2_alias_set == 0)
1126 return true;
1128 /* If both references are through the same type, they do not alias
1129 if the accesses do not overlap. This does extra disambiguation
1130 for mixed/pointer accesses but requires strict aliasing. */
1131 if ((TREE_CODE (base1) != TARGET_MEM_REF
1132 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1133 && (TREE_CODE (base2) != TARGET_MEM_REF
1134 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1135 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1136 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1137 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1138 TREE_TYPE (ptrtype2)) == 1)
1139 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1141 /* Do type-based disambiguation. */
1142 if (base1_alias_set != base2_alias_set
1143 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1144 return false;
1146 /* Do access-path based disambiguation. */
1147 if (ref1 && ref2
1148 && (handled_component_p (ref1) || handled_component_p (ref2))
1149 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1150 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
1151 return aliasing_component_refs_p (ref1,
1152 ref1_alias_set, base1_alias_set,
1153 offset1, max_size1,
1154 ref2,
1155 ref2_alias_set, base2_alias_set,
1156 offset2, max_size2, false);
1158 return true;
1161 /* Return true, if the two memory references REF1 and REF2 may alias. */
1163 bool
1164 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1166 tree base1, base2;
1167 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1168 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1169 bool var1_p, var2_p, ind1_p, ind2_p;
1171 gcc_checking_assert ((!ref1->ref
1172 || TREE_CODE (ref1->ref) == SSA_NAME
1173 || DECL_P (ref1->ref)
1174 || TREE_CODE (ref1->ref) == STRING_CST
1175 || handled_component_p (ref1->ref)
1176 || TREE_CODE (ref1->ref) == MEM_REF
1177 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1178 && (!ref2->ref
1179 || TREE_CODE (ref2->ref) == SSA_NAME
1180 || DECL_P (ref2->ref)
1181 || TREE_CODE (ref2->ref) == STRING_CST
1182 || handled_component_p (ref2->ref)
1183 || TREE_CODE (ref2->ref) == MEM_REF
1184 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1186 /* Decompose the references into their base objects and the access. */
1187 base1 = ao_ref_base (ref1);
1188 offset1 = ref1->offset;
1189 max_size1 = ref1->max_size;
1190 base2 = ao_ref_base (ref2);
1191 offset2 = ref2->offset;
1192 max_size2 = ref2->max_size;
1194 /* We can end up with registers or constants as bases for example from
1195 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1196 which is seen as a struct copy. */
1197 if (TREE_CODE (base1) == SSA_NAME
1198 || TREE_CODE (base1) == CONST_DECL
1199 || TREE_CODE (base1) == CONSTRUCTOR
1200 || TREE_CODE (base1) == ADDR_EXPR
1201 || CONSTANT_CLASS_P (base1)
1202 || TREE_CODE (base2) == SSA_NAME
1203 || TREE_CODE (base2) == CONST_DECL
1204 || TREE_CODE (base2) == CONSTRUCTOR
1205 || TREE_CODE (base2) == ADDR_EXPR
1206 || CONSTANT_CLASS_P (base2))
1207 return false;
1209 /* We can end up referring to code via function and label decls.
1210 As we likely do not properly track code aliases conservatively
1211 bail out. */
1212 if (TREE_CODE (base1) == FUNCTION_DECL
1213 || TREE_CODE (base1) == LABEL_DECL
1214 || TREE_CODE (base2) == FUNCTION_DECL
1215 || TREE_CODE (base2) == LABEL_DECL)
1216 return true;
1218 /* Two volatile accesses always conflict. */
1219 if (ref1->volatile_p
1220 && ref2->volatile_p)
1221 return true;
1223 /* Defer to simple offset based disambiguation if we have
1224 references based on two decls. Do this before defering to
1225 TBAA to handle must-alias cases in conformance with the
1226 GCC extension of allowing type-punning through unions. */
1227 var1_p = DECL_P (base1);
1228 var2_p = DECL_P (base2);
1229 if (var1_p && var2_p)
1230 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1231 ref2->ref, base2, offset2, max_size2);
1233 ind1_p = (TREE_CODE (base1) == MEM_REF
1234 || TREE_CODE (base1) == TARGET_MEM_REF);
1235 ind2_p = (TREE_CODE (base2) == MEM_REF
1236 || TREE_CODE (base2) == TARGET_MEM_REF);
1238 /* Canonicalize the pointer-vs-decl case. */
1239 if (ind1_p && var2_p)
1241 HOST_WIDE_INT tmp1;
1242 tree tmp2;
1243 ao_ref *tmp3;
1244 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1245 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1246 tmp2 = base1; base1 = base2; base2 = tmp2;
1247 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1248 var1_p = true;
1249 ind1_p = false;
1250 var2_p = false;
1251 ind2_p = true;
1254 /* First defer to TBAA if possible. */
1255 if (tbaa_p
1256 && flag_strict_aliasing
1257 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1258 ao_ref_alias_set (ref2)))
1259 return false;
1261 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1262 if (var1_p && ind2_p)
1263 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1264 offset2, max_size2,
1265 ao_ref_alias_set (ref2), -1,
1266 ref1->ref, base1,
1267 offset1, max_size1,
1268 ao_ref_alias_set (ref1),
1269 ao_ref_base_alias_set (ref1),
1270 tbaa_p);
1271 else if (ind1_p && ind2_p)
1272 return indirect_refs_may_alias_p (ref1->ref, base1,
1273 offset1, max_size1,
1274 ao_ref_alias_set (ref1), -1,
1275 ref2->ref, base2,
1276 offset2, max_size2,
1277 ao_ref_alias_set (ref2), -1,
1278 tbaa_p);
1280 /* We really do not want to end up here, but returning true is safe. */
1281 #ifdef ENABLE_CHECKING
1282 gcc_unreachable ();
1283 #else
1284 return true;
1285 #endif
1288 bool
1289 refs_may_alias_p (tree ref1, tree ref2)
1291 ao_ref r1, r2;
1292 bool res;
1293 ao_ref_init (&r1, ref1);
1294 ao_ref_init (&r2, ref2);
1295 res = refs_may_alias_p_1 (&r1, &r2, true);
1296 if (res)
1297 ++alias_stats.refs_may_alias_p_may_alias;
1298 else
1299 ++alias_stats.refs_may_alias_p_no_alias;
1300 return res;
1303 /* Returns true if there is a anti-dependence for the STORE that
1304 executes after the LOAD. */
1306 bool
1307 refs_anti_dependent_p (tree load, tree store)
1309 ao_ref r1, r2;
1310 ao_ref_init (&r1, load);
1311 ao_ref_init (&r2, store);
1312 return refs_may_alias_p_1 (&r1, &r2, false);
1315 /* Returns true if there is a output dependence for the stores
1316 STORE1 and STORE2. */
1318 bool
1319 refs_output_dependent_p (tree store1, tree store2)
1321 ao_ref r1, r2;
1322 ao_ref_init (&r1, store1);
1323 ao_ref_init (&r2, store2);
1324 return refs_may_alias_p_1 (&r1, &r2, false);
1327 /* If the call CALL may use the memory reference REF return true,
1328 otherwise return false. */
1330 static bool
1331 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1333 tree base, callee;
1334 unsigned i;
1335 int flags = gimple_call_flags (call);
1337 /* Const functions without a static chain do not implicitly use memory. */
1338 if (!gimple_call_chain (call)
1339 && (flags & (ECF_CONST|ECF_NOVOPS)))
1340 goto process_args;
1342 base = ao_ref_base (ref);
1343 if (!base)
1344 return true;
1346 /* A call that is not without side-effects might involve volatile
1347 accesses and thus conflicts with all other volatile accesses. */
1348 if (ref->volatile_p)
1349 return true;
1351 /* If the reference is based on a decl that is not aliased the call
1352 cannot possibly use it. */
1353 if (DECL_P (base)
1354 && !may_be_aliased (base)
1355 /* But local statics can be used through recursion. */
1356 && !is_global_var (base))
1357 goto process_args;
1359 callee = gimple_call_fndecl (call);
1361 /* Handle those builtin functions explicitly that do not act as
1362 escape points. See tree-ssa-structalias.c:find_func_aliases
1363 for the list of builtins we might need to handle here. */
1364 if (callee != NULL_TREE
1365 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1366 switch (DECL_FUNCTION_CODE (callee))
1368 /* All the following functions read memory pointed to by
1369 their second argument. strcat/strncat additionally
1370 reads memory pointed to by the first argument. */
1371 case BUILT_IN_STRCAT:
1372 case BUILT_IN_STRNCAT:
1374 ao_ref dref;
1375 ao_ref_init_from_ptr_and_size (&dref,
1376 gimple_call_arg (call, 0),
1377 NULL_TREE);
1378 if (refs_may_alias_p_1 (&dref, ref, false))
1379 return true;
1381 /* FALLTHRU */
1382 case BUILT_IN_STRCPY:
1383 case BUILT_IN_STRNCPY:
1384 case BUILT_IN_MEMCPY:
1385 case BUILT_IN_MEMMOVE:
1386 case BUILT_IN_MEMPCPY:
1387 case BUILT_IN_STPCPY:
1388 case BUILT_IN_STPNCPY:
1389 case BUILT_IN_TM_MEMCPY:
1390 case BUILT_IN_TM_MEMMOVE:
1392 ao_ref dref;
1393 tree size = NULL_TREE;
1394 if (gimple_call_num_args (call) == 3)
1395 size = gimple_call_arg (call, 2);
1396 ao_ref_init_from_ptr_and_size (&dref,
1397 gimple_call_arg (call, 1),
1398 size);
1399 return refs_may_alias_p_1 (&dref, ref, false);
1401 case BUILT_IN_STRCAT_CHK:
1402 case BUILT_IN_STRNCAT_CHK:
1404 ao_ref dref;
1405 ao_ref_init_from_ptr_and_size (&dref,
1406 gimple_call_arg (call, 0),
1407 NULL_TREE);
1408 if (refs_may_alias_p_1 (&dref, ref, false))
1409 return true;
1411 /* FALLTHRU */
1412 case BUILT_IN_STRCPY_CHK:
1413 case BUILT_IN_STRNCPY_CHK:
1414 case BUILT_IN_MEMCPY_CHK:
1415 case BUILT_IN_MEMMOVE_CHK:
1416 case BUILT_IN_MEMPCPY_CHK:
1417 case BUILT_IN_STPCPY_CHK:
1418 case BUILT_IN_STPNCPY_CHK:
1420 ao_ref dref;
1421 tree size = NULL_TREE;
1422 if (gimple_call_num_args (call) == 4)
1423 size = gimple_call_arg (call, 2);
1424 ao_ref_init_from_ptr_and_size (&dref,
1425 gimple_call_arg (call, 1),
1426 size);
1427 return refs_may_alias_p_1 (&dref, ref, false);
1429 case BUILT_IN_BCOPY:
1431 ao_ref dref;
1432 tree size = gimple_call_arg (call, 2);
1433 ao_ref_init_from_ptr_and_size (&dref,
1434 gimple_call_arg (call, 0),
1435 size);
1436 return refs_may_alias_p_1 (&dref, ref, false);
1439 /* The following functions read memory pointed to by their
1440 first argument. */
1441 CASE_BUILT_IN_TM_LOAD (1):
1442 CASE_BUILT_IN_TM_LOAD (2):
1443 CASE_BUILT_IN_TM_LOAD (4):
1444 CASE_BUILT_IN_TM_LOAD (8):
1445 CASE_BUILT_IN_TM_LOAD (FLOAT):
1446 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1447 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1448 CASE_BUILT_IN_TM_LOAD (M64):
1449 CASE_BUILT_IN_TM_LOAD (M128):
1450 CASE_BUILT_IN_TM_LOAD (M256):
1451 case BUILT_IN_TM_LOG:
1452 case BUILT_IN_TM_LOG_1:
1453 case BUILT_IN_TM_LOG_2:
1454 case BUILT_IN_TM_LOG_4:
1455 case BUILT_IN_TM_LOG_8:
1456 case BUILT_IN_TM_LOG_FLOAT:
1457 case BUILT_IN_TM_LOG_DOUBLE:
1458 case BUILT_IN_TM_LOG_LDOUBLE:
1459 case BUILT_IN_TM_LOG_M64:
1460 case BUILT_IN_TM_LOG_M128:
1461 case BUILT_IN_TM_LOG_M256:
1462 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1464 /* These read memory pointed to by the first argument. */
1465 case BUILT_IN_STRDUP:
1466 case BUILT_IN_STRNDUP:
1468 ao_ref dref;
1469 tree size = NULL_TREE;
1470 if (gimple_call_num_args (call) == 2)
1471 size = gimple_call_arg (call, 1);
1472 ao_ref_init_from_ptr_and_size (&dref,
1473 gimple_call_arg (call, 0),
1474 size);
1475 return refs_may_alias_p_1 (&dref, ref, false);
1477 /* These read memory pointed to by the first argument. */
1478 case BUILT_IN_INDEX:
1479 case BUILT_IN_STRCHR:
1480 case BUILT_IN_STRRCHR:
1482 ao_ref dref;
1483 ao_ref_init_from_ptr_and_size (&dref,
1484 gimple_call_arg (call, 0),
1485 NULL_TREE);
1486 return refs_may_alias_p_1 (&dref, ref, false);
1488 /* These read memory pointed to by the first argument with size
1489 in the third argument. */
1490 case BUILT_IN_MEMCHR:
1492 ao_ref dref;
1493 ao_ref_init_from_ptr_and_size (&dref,
1494 gimple_call_arg (call, 0),
1495 gimple_call_arg (call, 2));
1496 return refs_may_alias_p_1 (&dref, ref, false);
1498 /* These read memory pointed to by the first and second arguments. */
1499 case BUILT_IN_STRSTR:
1500 case BUILT_IN_STRPBRK:
1502 ao_ref dref;
1503 ao_ref_init_from_ptr_and_size (&dref,
1504 gimple_call_arg (call, 0),
1505 NULL_TREE);
1506 if (refs_may_alias_p_1 (&dref, ref, false))
1507 return true;
1508 ao_ref_init_from_ptr_and_size (&dref,
1509 gimple_call_arg (call, 1),
1510 NULL_TREE);
1511 return refs_may_alias_p_1 (&dref, ref, false);
1514 /* The following builtins do not read from memory. */
1515 case BUILT_IN_FREE:
1516 case BUILT_IN_MALLOC:
1517 case BUILT_IN_CALLOC:
1518 case BUILT_IN_ALLOCA:
1519 case BUILT_IN_ALLOCA_WITH_ALIGN:
1520 case BUILT_IN_STACK_SAVE:
1521 case BUILT_IN_STACK_RESTORE:
1522 case BUILT_IN_MEMSET:
1523 case BUILT_IN_TM_MEMSET:
1524 case BUILT_IN_MEMSET_CHK:
1525 case BUILT_IN_FREXP:
1526 case BUILT_IN_FREXPF:
1527 case BUILT_IN_FREXPL:
1528 case BUILT_IN_GAMMA_R:
1529 case BUILT_IN_GAMMAF_R:
1530 case BUILT_IN_GAMMAL_R:
1531 case BUILT_IN_LGAMMA_R:
1532 case BUILT_IN_LGAMMAF_R:
1533 case BUILT_IN_LGAMMAL_R:
1534 case BUILT_IN_MODF:
1535 case BUILT_IN_MODFF:
1536 case BUILT_IN_MODFL:
1537 case BUILT_IN_REMQUO:
1538 case BUILT_IN_REMQUOF:
1539 case BUILT_IN_REMQUOL:
1540 case BUILT_IN_SINCOS:
1541 case BUILT_IN_SINCOSF:
1542 case BUILT_IN_SINCOSL:
1543 case BUILT_IN_ASSUME_ALIGNED:
1544 case BUILT_IN_VA_END:
1545 return false;
1546 /* __sync_* builtins and some OpenMP builtins act as threading
1547 barriers. */
1548 #undef DEF_SYNC_BUILTIN
1549 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1550 #include "sync-builtins.def"
1551 #undef DEF_SYNC_BUILTIN
1552 case BUILT_IN_GOMP_ATOMIC_START:
1553 case BUILT_IN_GOMP_ATOMIC_END:
1554 case BUILT_IN_GOMP_BARRIER:
1555 case BUILT_IN_GOMP_BARRIER_CANCEL:
1556 case BUILT_IN_GOMP_TASKWAIT:
1557 case BUILT_IN_GOMP_TASKGROUP_END:
1558 case BUILT_IN_GOMP_CRITICAL_START:
1559 case BUILT_IN_GOMP_CRITICAL_END:
1560 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1561 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1562 case BUILT_IN_GOMP_LOOP_END:
1563 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1564 case BUILT_IN_GOMP_ORDERED_START:
1565 case BUILT_IN_GOMP_ORDERED_END:
1566 case BUILT_IN_GOMP_SECTIONS_END:
1567 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1568 case BUILT_IN_GOMP_SINGLE_COPY_START:
1569 case BUILT_IN_GOMP_SINGLE_COPY_END:
1570 return true;
1572 default:
1573 /* Fallthru to general call handling. */;
1576 /* Check if base is a global static variable that is not read
1577 by the function. */
1578 if (callee != NULL_TREE
1579 && TREE_CODE (base) == VAR_DECL
1580 && TREE_STATIC (base))
1582 struct cgraph_node *node = cgraph_get_node (callee);
1583 bitmap not_read;
1585 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1586 node yet. We should enforce that there are nodes for all decls in the
1587 IL and remove this check instead. */
1588 if (node
1589 && (not_read = ipa_reference_get_not_read_global (node))
1590 && bitmap_bit_p (not_read, DECL_UID (base)))
1591 goto process_args;
1594 /* Check if the base variable is call-used. */
1595 if (DECL_P (base))
1597 if (pt_solution_includes (gimple_call_use_set (call), base))
1598 return true;
1600 else if ((TREE_CODE (base) == MEM_REF
1601 || TREE_CODE (base) == TARGET_MEM_REF)
1602 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1604 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1605 if (!pi)
1606 return true;
1608 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1609 return true;
1611 else
1612 return true;
1614 /* Inspect call arguments for passed-by-value aliases. */
1615 process_args:
1616 for (i = 0; i < gimple_call_num_args (call); ++i)
1618 tree op = gimple_call_arg (call, i);
1619 int flags = gimple_call_arg_flags (call, i);
1621 if (flags & EAF_UNUSED)
1622 continue;
1624 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1625 op = TREE_OPERAND (op, 0);
1627 if (TREE_CODE (op) != SSA_NAME
1628 && !is_gimple_min_invariant (op))
1630 ao_ref r;
1631 ao_ref_init (&r, op);
1632 if (refs_may_alias_p_1 (&r, ref, true))
1633 return true;
1637 return false;
1640 static bool
1641 ref_maybe_used_by_call_p (gimple call, tree ref)
1643 ao_ref r;
1644 bool res;
1645 ao_ref_init (&r, ref);
1646 res = ref_maybe_used_by_call_p_1 (call, &r);
1647 if (res)
1648 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1649 else
1650 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1651 return res;
1655 /* If the statement STMT may use the memory reference REF return
1656 true, otherwise return false. */
1658 bool
1659 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1661 if (is_gimple_assign (stmt))
1663 tree rhs;
1665 /* All memory assign statements are single. */
1666 if (!gimple_assign_single_p (stmt))
1667 return false;
1669 rhs = gimple_assign_rhs1 (stmt);
1670 if (is_gimple_reg (rhs)
1671 || is_gimple_min_invariant (rhs)
1672 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1673 return false;
1675 return refs_may_alias_p (rhs, ref);
1677 else if (is_gimple_call (stmt))
1678 return ref_maybe_used_by_call_p (stmt, ref);
1679 else if (gimple_code (stmt) == GIMPLE_RETURN)
1681 tree retval = gimple_return_retval (stmt);
1682 tree base;
1683 if (retval
1684 && TREE_CODE (retval) != SSA_NAME
1685 && !is_gimple_min_invariant (retval)
1686 && refs_may_alias_p (retval, ref))
1687 return true;
1688 /* If ref escapes the function then the return acts as a use. */
1689 base = get_base_address (ref);
1690 if (!base)
1692 else if (DECL_P (base))
1693 return is_global_var (base);
1694 else if (TREE_CODE (base) == MEM_REF
1695 || TREE_CODE (base) == TARGET_MEM_REF)
1696 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1697 return false;
1700 return true;
1703 /* If the call in statement CALL may clobber the memory reference REF
1704 return true, otherwise return false. */
1706 static bool
1707 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1709 tree base;
1710 tree callee;
1712 /* If the call is pure or const it cannot clobber anything. */
1713 if (gimple_call_flags (call)
1714 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1715 return false;
1717 base = ao_ref_base (ref);
1718 if (!base)
1719 return true;
1721 if (TREE_CODE (base) == SSA_NAME
1722 || CONSTANT_CLASS_P (base))
1723 return false;
1725 /* A call that is not without side-effects might involve volatile
1726 accesses and thus conflicts with all other volatile accesses. */
1727 if (ref->volatile_p)
1728 return true;
1730 /* If the reference is based on a decl that is not aliased the call
1731 cannot possibly clobber it. */
1732 if (DECL_P (base)
1733 && !may_be_aliased (base)
1734 /* But local non-readonly statics can be modified through recursion
1735 or the call may implement a threading barrier which we must
1736 treat as may-def. */
1737 && (TREE_READONLY (base)
1738 || !is_global_var (base)))
1739 return false;
1741 callee = gimple_call_fndecl (call);
1743 /* Handle those builtin functions explicitly that do not act as
1744 escape points. See tree-ssa-structalias.c:find_func_aliases
1745 for the list of builtins we might need to handle here. */
1746 if (callee != NULL_TREE
1747 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1748 switch (DECL_FUNCTION_CODE (callee))
1750 /* All the following functions clobber memory pointed to by
1751 their first argument. */
1752 case BUILT_IN_STRCPY:
1753 case BUILT_IN_STRNCPY:
1754 case BUILT_IN_MEMCPY:
1755 case BUILT_IN_MEMMOVE:
1756 case BUILT_IN_MEMPCPY:
1757 case BUILT_IN_STPCPY:
1758 case BUILT_IN_STPNCPY:
1759 case BUILT_IN_STRCAT:
1760 case BUILT_IN_STRNCAT:
1761 case BUILT_IN_MEMSET:
1762 case BUILT_IN_TM_MEMSET:
1763 CASE_BUILT_IN_TM_STORE (1):
1764 CASE_BUILT_IN_TM_STORE (2):
1765 CASE_BUILT_IN_TM_STORE (4):
1766 CASE_BUILT_IN_TM_STORE (8):
1767 CASE_BUILT_IN_TM_STORE (FLOAT):
1768 CASE_BUILT_IN_TM_STORE (DOUBLE):
1769 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1770 CASE_BUILT_IN_TM_STORE (M64):
1771 CASE_BUILT_IN_TM_STORE (M128):
1772 CASE_BUILT_IN_TM_STORE (M256):
1773 case BUILT_IN_TM_MEMCPY:
1774 case BUILT_IN_TM_MEMMOVE:
1776 ao_ref dref;
1777 tree size = NULL_TREE;
1778 /* Don't pass in size for strncat, as the maximum size
1779 is strlen (dest) + n + 1 instead of n, resp.
1780 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1781 known. */
1782 if (gimple_call_num_args (call) == 3
1783 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1784 size = gimple_call_arg (call, 2);
1785 ao_ref_init_from_ptr_and_size (&dref,
1786 gimple_call_arg (call, 0),
1787 size);
1788 return refs_may_alias_p_1 (&dref, ref, false);
1790 case BUILT_IN_STRCPY_CHK:
1791 case BUILT_IN_STRNCPY_CHK:
1792 case BUILT_IN_MEMCPY_CHK:
1793 case BUILT_IN_MEMMOVE_CHK:
1794 case BUILT_IN_MEMPCPY_CHK:
1795 case BUILT_IN_STPCPY_CHK:
1796 case BUILT_IN_STPNCPY_CHK:
1797 case BUILT_IN_STRCAT_CHK:
1798 case BUILT_IN_STRNCAT_CHK:
1799 case BUILT_IN_MEMSET_CHK:
1801 ao_ref dref;
1802 tree size = NULL_TREE;
1803 /* Don't pass in size for __strncat_chk, as the maximum size
1804 is strlen (dest) + n + 1 instead of n, resp.
1805 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1806 known. */
1807 if (gimple_call_num_args (call) == 4
1808 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1809 size = gimple_call_arg (call, 2);
1810 ao_ref_init_from_ptr_and_size (&dref,
1811 gimple_call_arg (call, 0),
1812 size);
1813 return refs_may_alias_p_1 (&dref, ref, false);
1815 case BUILT_IN_BCOPY:
1817 ao_ref dref;
1818 tree size = gimple_call_arg (call, 2);
1819 ao_ref_init_from_ptr_and_size (&dref,
1820 gimple_call_arg (call, 1),
1821 size);
1822 return refs_may_alias_p_1 (&dref, ref, false);
1824 /* Allocating memory does not have any side-effects apart from
1825 being the definition point for the pointer. */
1826 case BUILT_IN_MALLOC:
1827 case BUILT_IN_CALLOC:
1828 case BUILT_IN_STRDUP:
1829 case BUILT_IN_STRNDUP:
1830 /* Unix98 specifies that errno is set on allocation failure. */
1831 if (flag_errno_math
1832 && targetm.ref_may_alias_errno (ref))
1833 return true;
1834 return false;
1835 case BUILT_IN_STACK_SAVE:
1836 case BUILT_IN_ALLOCA:
1837 case BUILT_IN_ALLOCA_WITH_ALIGN:
1838 case BUILT_IN_ASSUME_ALIGNED:
1839 return false;
1840 /* Freeing memory kills the pointed-to memory. More importantly
1841 the call has to serve as a barrier for moving loads and stores
1842 across it. */
1843 case BUILT_IN_FREE:
1844 case BUILT_IN_VA_END:
1846 tree ptr = gimple_call_arg (call, 0);
1847 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1849 case BUILT_IN_GAMMA_R:
1850 case BUILT_IN_GAMMAF_R:
1851 case BUILT_IN_GAMMAL_R:
1852 case BUILT_IN_LGAMMA_R:
1853 case BUILT_IN_LGAMMAF_R:
1854 case BUILT_IN_LGAMMAL_R:
1856 tree out = gimple_call_arg (call, 1);
1857 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1858 return true;
1859 if (flag_errno_math)
1860 break;
1861 return false;
1863 case BUILT_IN_FREXP:
1864 case BUILT_IN_FREXPF:
1865 case BUILT_IN_FREXPL:
1866 case BUILT_IN_MODF:
1867 case BUILT_IN_MODFF:
1868 case BUILT_IN_MODFL:
1870 tree out = gimple_call_arg (call, 1);
1871 return ptr_deref_may_alias_ref_p_1 (out, ref);
1873 case BUILT_IN_REMQUO:
1874 case BUILT_IN_REMQUOF:
1875 case BUILT_IN_REMQUOL:
1877 tree out = gimple_call_arg (call, 2);
1878 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1879 return true;
1880 if (flag_errno_math)
1881 break;
1882 return false;
1884 case BUILT_IN_SINCOS:
1885 case BUILT_IN_SINCOSF:
1886 case BUILT_IN_SINCOSL:
1888 tree sin = gimple_call_arg (call, 1);
1889 tree cos = gimple_call_arg (call, 2);
1890 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1891 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1893 /* __sync_* builtins and some OpenMP builtins act as threading
1894 barriers. */
1895 #undef DEF_SYNC_BUILTIN
1896 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1897 #include "sync-builtins.def"
1898 #undef DEF_SYNC_BUILTIN
1899 case BUILT_IN_GOMP_ATOMIC_START:
1900 case BUILT_IN_GOMP_ATOMIC_END:
1901 case BUILT_IN_GOMP_BARRIER:
1902 case BUILT_IN_GOMP_BARRIER_CANCEL:
1903 case BUILT_IN_GOMP_TASKWAIT:
1904 case BUILT_IN_GOMP_TASKGROUP_END:
1905 case BUILT_IN_GOMP_CRITICAL_START:
1906 case BUILT_IN_GOMP_CRITICAL_END:
1907 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1908 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1909 case BUILT_IN_GOMP_LOOP_END:
1910 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1911 case BUILT_IN_GOMP_ORDERED_START:
1912 case BUILT_IN_GOMP_ORDERED_END:
1913 case BUILT_IN_GOMP_SECTIONS_END:
1914 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1915 case BUILT_IN_GOMP_SINGLE_COPY_START:
1916 case BUILT_IN_GOMP_SINGLE_COPY_END:
1917 return true;
1918 default:
1919 /* Fallthru to general call handling. */;
1922 /* Check if base is a global static variable that is not written
1923 by the function. */
1924 if (callee != NULL_TREE
1925 && TREE_CODE (base) == VAR_DECL
1926 && TREE_STATIC (base))
1928 struct cgraph_node *node = cgraph_get_node (callee);
1929 bitmap not_written;
1931 if (node
1932 && (not_written = ipa_reference_get_not_written_global (node))
1933 && bitmap_bit_p (not_written, DECL_UID (base)))
1934 return false;
1937 /* Check if the base variable is call-clobbered. */
1938 if (DECL_P (base))
1939 return pt_solution_includes (gimple_call_clobber_set (call), base);
1940 else if ((TREE_CODE (base) == MEM_REF
1941 || TREE_CODE (base) == TARGET_MEM_REF)
1942 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1944 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1945 if (!pi)
1946 return true;
1948 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1951 return true;
1954 /* If the call in statement CALL may clobber the memory reference REF
1955 return true, otherwise return false. */
1957 bool
1958 call_may_clobber_ref_p (gimple call, tree ref)
1960 bool res;
1961 ao_ref r;
1962 ao_ref_init (&r, ref);
1963 res = call_may_clobber_ref_p_1 (call, &r);
1964 if (res)
1965 ++alias_stats.call_may_clobber_ref_p_may_alias;
1966 else
1967 ++alias_stats.call_may_clobber_ref_p_no_alias;
1968 return res;
1972 /* If the statement STMT may clobber the memory reference REF return true,
1973 otherwise return false. */
1975 bool
1976 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1978 if (is_gimple_call (stmt))
1980 tree lhs = gimple_call_lhs (stmt);
1981 if (lhs
1982 && TREE_CODE (lhs) != SSA_NAME)
1984 ao_ref r;
1985 ao_ref_init (&r, lhs);
1986 if (refs_may_alias_p_1 (ref, &r, true))
1987 return true;
1990 return call_may_clobber_ref_p_1 (stmt, ref);
1992 else if (gimple_assign_single_p (stmt))
1994 tree lhs = gimple_assign_lhs (stmt);
1995 if (TREE_CODE (lhs) != SSA_NAME)
1997 ao_ref r;
1998 ao_ref_init (&r, lhs);
1999 return refs_may_alias_p_1 (ref, &r, true);
2002 else if (gimple_code (stmt) == GIMPLE_ASM)
2003 return true;
2005 return false;
2008 bool
2009 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2011 ao_ref r;
2012 ao_ref_init (&r, ref);
2013 return stmt_may_clobber_ref_p_1 (stmt, &r);
2016 /* If STMT kills the memory reference REF return true, otherwise
2017 return false. */
2019 static bool
2020 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
2022 /* For a must-alias check we need to be able to constrain
2023 the access properly.
2024 FIXME: except for BUILTIN_FREE. */
2025 if (!ao_ref_base (ref)
2026 || ref->max_size == -1)
2027 return false;
2029 if (gimple_has_lhs (stmt)
2030 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2031 /* The assignment is not necessarily carried out if it can throw
2032 and we can catch it in the current function where we could inspect
2033 the previous value.
2034 ??? We only need to care about the RHS throwing. For aggregate
2035 assignments or similar calls and non-call exceptions the LHS
2036 might throw as well. */
2037 && !stmt_can_throw_internal (stmt))
2039 tree base, lhs = gimple_get_lhs (stmt);
2040 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2041 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2042 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2043 so base == ref->base does not always hold. */
2044 if (base != ref->base)
2046 /* If both base and ref->base are MEM_REFs, only compare the
2047 first operand, and if the second operand isn't equal constant,
2048 try to add the offsets into offset and ref_offset. */
2049 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2050 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2052 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2053 TREE_OPERAND (ref->base, 1)))
2055 double_int off1 = mem_ref_offset (base);
2056 off1 = off1.lshift (BITS_PER_UNIT == 8
2057 ? 3 : exact_log2 (BITS_PER_UNIT));
2058 off1 = off1 + double_int::from_shwi (offset);
2059 double_int off2 = mem_ref_offset (ref->base);
2060 off2 = off2.lshift (BITS_PER_UNIT == 8
2061 ? 3 : exact_log2 (BITS_PER_UNIT));
2062 off2 = off2 + double_int::from_shwi (ref_offset);
2063 if (off1.fits_shwi () && off2.fits_shwi ())
2065 offset = off1.to_shwi ();
2066 ref_offset = off2.to_shwi ();
2068 else
2069 size = -1;
2072 else
2073 size = -1;
2075 /* For a must-alias check we need to be able to constrain
2076 the access properly. */
2077 if (size != -1 && size == max_size)
2079 if (offset <= ref_offset
2080 && offset + size >= ref_offset + ref->max_size)
2081 return true;
2085 if (is_gimple_call (stmt))
2087 tree callee = gimple_call_fndecl (stmt);
2088 if (callee != NULL_TREE
2089 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2090 switch (DECL_FUNCTION_CODE (callee))
2092 case BUILT_IN_FREE:
2094 tree ptr = gimple_call_arg (stmt, 0);
2095 tree base = ao_ref_base (ref);
2096 if (base && TREE_CODE (base) == MEM_REF
2097 && TREE_OPERAND (base, 0) == ptr)
2098 return true;
2099 break;
2102 case BUILT_IN_MEMCPY:
2103 case BUILT_IN_MEMPCPY:
2104 case BUILT_IN_MEMMOVE:
2105 case BUILT_IN_MEMSET:
2106 case BUILT_IN_MEMCPY_CHK:
2107 case BUILT_IN_MEMPCPY_CHK:
2108 case BUILT_IN_MEMMOVE_CHK:
2109 case BUILT_IN_MEMSET_CHK:
2111 tree dest = gimple_call_arg (stmt, 0);
2112 tree len = gimple_call_arg (stmt, 2);
2113 if (!tree_fits_shwi_p (len))
2114 return false;
2115 tree rbase = ref->base;
2116 double_int roffset = double_int::from_shwi (ref->offset);
2117 ao_ref dref;
2118 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2119 tree base = ao_ref_base (&dref);
2120 double_int offset = double_int::from_shwi (dref.offset);
2121 double_int bpu = double_int::from_uhwi (BITS_PER_UNIT);
2122 if (!base || dref.size == -1)
2123 return false;
2124 if (TREE_CODE (base) == MEM_REF)
2126 if (TREE_CODE (rbase) != MEM_REF)
2127 return false;
2128 // Compare pointers.
2129 offset += bpu * mem_ref_offset (base);
2130 roffset += bpu * mem_ref_offset (rbase);
2131 base = TREE_OPERAND (base, 0);
2132 rbase = TREE_OPERAND (rbase, 0);
2134 if (base == rbase)
2136 double_int size = bpu * tree_to_double_int (len);
2137 double_int rsize = double_int::from_uhwi (ref->max_size);
2138 if (offset.sle (roffset)
2139 && (roffset + rsize).sle (offset + size))
2140 return true;
2142 break;
2145 case BUILT_IN_VA_END:
2147 tree ptr = gimple_call_arg (stmt, 0);
2148 if (TREE_CODE (ptr) == ADDR_EXPR)
2150 tree base = ao_ref_base (ref);
2151 if (TREE_OPERAND (ptr, 0) == base)
2152 return true;
2154 break;
2157 default:;
2160 return false;
2163 bool
2164 stmt_kills_ref_p (gimple stmt, tree ref)
2166 ao_ref r;
2167 ao_ref_init (&r, ref);
2168 return stmt_kills_ref_p_1 (stmt, &r);
2172 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2173 TARGET or a statement clobbering the memory reference REF in which
2174 case false is returned. The walk starts with VUSE, one argument of PHI. */
2176 static bool
2177 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2178 tree vuse, unsigned int *cnt, bitmap *visited,
2179 bool abort_on_visited)
2181 basic_block bb = gimple_bb (phi);
2183 if (!*visited)
2184 *visited = BITMAP_ALLOC (NULL);
2186 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2188 /* Walk until we hit the target. */
2189 while (vuse != target)
2191 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2192 /* Recurse for PHI nodes. */
2193 if (gimple_code (def_stmt) == GIMPLE_PHI)
2195 /* An already visited PHI node ends the walk successfully. */
2196 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2197 return !abort_on_visited;
2198 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2199 visited, abort_on_visited);
2200 if (!vuse)
2201 return false;
2202 continue;
2204 else if (gimple_nop_p (def_stmt))
2205 return false;
2206 else
2208 /* A clobbering statement or the end of the IL ends it failing. */
2209 ++*cnt;
2210 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2211 return false;
2213 /* If we reach a new basic-block see if we already skipped it
2214 in a previous walk that ended successfully. */
2215 if (gimple_bb (def_stmt) != bb)
2217 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2218 return !abort_on_visited;
2219 bb = gimple_bb (def_stmt);
2221 vuse = gimple_vuse (def_stmt);
2223 return true;
2226 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2227 until we hit the phi argument definition that dominates the other one.
2228 Return that, or NULL_TREE if there is no such definition. */
2230 static tree
2231 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2232 ao_ref *ref, unsigned int *cnt,
2233 bitmap *visited, bool abort_on_visited)
2235 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2236 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2237 tree common_vuse;
2239 if (arg0 == arg1)
2240 return arg0;
2241 else if (gimple_nop_p (def0)
2242 || (!gimple_nop_p (def1)
2243 && dominated_by_p (CDI_DOMINATORS,
2244 gimple_bb (def1), gimple_bb (def0))))
2246 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2247 visited, abort_on_visited))
2248 return arg0;
2250 else if (gimple_nop_p (def1)
2251 || dominated_by_p (CDI_DOMINATORS,
2252 gimple_bb (def0), gimple_bb (def1)))
2254 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2255 visited, abort_on_visited))
2256 return arg1;
2258 /* Special case of a diamond:
2259 MEM_1 = ...
2260 goto (cond) ? L1 : L2
2261 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2262 goto L3
2263 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2264 L3: MEM_4 = PHI<MEM_2, MEM_3>
2265 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2266 dominate each other, but still we can easily skip this PHI node
2267 if we recognize that the vuse MEM operand is the same for both,
2268 and that we can skip both statements (they don't clobber us).
2269 This is still linear. Don't use maybe_skip_until, that might
2270 potentially be slow. */
2271 else if ((common_vuse = gimple_vuse (def0))
2272 && common_vuse == gimple_vuse (def1))
2274 *cnt += 2;
2275 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2276 && !stmt_may_clobber_ref_p_1 (def1, ref))
2277 return common_vuse;
2280 return NULL_TREE;
2284 /* Starting from a PHI node for the virtual operand of the memory reference
2285 REF find a continuation virtual operand that allows to continue walking
2286 statements dominating PHI skipping only statements that cannot possibly
2287 clobber REF. Increments *CNT for each alias disambiguation done.
2288 Returns NULL_TREE if no suitable virtual operand can be found. */
2290 tree
2291 get_continuation_for_phi (gimple phi, ao_ref *ref,
2292 unsigned int *cnt, bitmap *visited,
2293 bool abort_on_visited)
2295 unsigned nargs = gimple_phi_num_args (phi);
2297 /* Through a single-argument PHI we can simply look through. */
2298 if (nargs == 1)
2299 return PHI_ARG_DEF (phi, 0);
2301 /* For two or more arguments try to pairwise skip non-aliasing code
2302 until we hit the phi argument definition that dominates the other one. */
2303 else if (nargs >= 2)
2305 tree arg0, arg1;
2306 unsigned i;
2308 /* Find a candidate for the virtual operand which definition
2309 dominates those of all others. */
2310 arg0 = PHI_ARG_DEF (phi, 0);
2311 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2312 for (i = 1; i < nargs; ++i)
2314 arg1 = PHI_ARG_DEF (phi, i);
2315 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2317 arg0 = arg1;
2318 break;
2320 if (dominated_by_p (CDI_DOMINATORS,
2321 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2322 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2323 arg0 = arg1;
2326 /* Then pairwise reduce against the found candidate. */
2327 for (i = 0; i < nargs; ++i)
2329 arg1 = PHI_ARG_DEF (phi, i);
2330 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2331 cnt, visited, abort_on_visited);
2332 if (!arg0)
2333 return NULL_TREE;
2336 return arg0;
2339 return NULL_TREE;
2342 /* Based on the memory reference REF and its virtual use VUSE call
2343 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2344 itself. That is, for each virtual use for which its defining statement
2345 does not clobber REF.
2347 WALKER is called with REF, the current virtual use and DATA. If
2348 WALKER returns non-NULL the walk stops and its result is returned.
2349 At the end of a non-successful walk NULL is returned.
2351 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2352 use which definition is a statement that may clobber REF and DATA.
2353 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2354 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2355 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2356 to adjust REF and *DATA to make that valid.
2358 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2360 void *
2361 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2362 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2363 void *(*translate)(ao_ref *, tree, void *), void *data)
2365 bitmap visited = NULL;
2366 void *res;
2367 unsigned int cnt = 0;
2368 bool translated = false;
2370 timevar_push (TV_ALIAS_STMT_WALK);
2374 gimple def_stmt;
2376 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2377 res = (*walker) (ref, vuse, cnt, data);
2378 /* Abort walk. */
2379 if (res == (void *)-1)
2381 res = NULL;
2382 break;
2384 /* Lookup succeeded. */
2385 else if (res != NULL)
2386 break;
2388 def_stmt = SSA_NAME_DEF_STMT (vuse);
2389 if (gimple_nop_p (def_stmt))
2390 break;
2391 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2392 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2393 &visited, translated);
2394 else
2396 cnt++;
2397 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2399 if (!translate)
2400 break;
2401 res = (*translate) (ref, vuse, data);
2402 /* Failed lookup and translation. */
2403 if (res == (void *)-1)
2405 res = NULL;
2406 break;
2408 /* Lookup succeeded. */
2409 else if (res != NULL)
2410 break;
2411 /* Translation succeeded, continue walking. */
2412 translated = true;
2414 vuse = gimple_vuse (def_stmt);
2417 while (vuse);
2419 if (visited)
2420 BITMAP_FREE (visited);
2422 timevar_pop (TV_ALIAS_STMT_WALK);
2424 return res;
2428 /* Based on the memory reference REF call WALKER for each vdef which
2429 defining statement may clobber REF, starting with VDEF. If REF
2430 is NULL_TREE, each defining statement is visited.
2432 WALKER is called with REF, the current vdef and DATA. If WALKER
2433 returns true the walk is stopped, otherwise it continues.
2435 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2436 PHI argument (but only one walk continues on merge points), the
2437 return value is true if any of the walks was successful.
2439 The function returns the number of statements walked. */
2441 static unsigned int
2442 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2443 bool (*walker)(ao_ref *, tree, void *), void *data,
2444 bitmap *visited, unsigned int cnt)
2448 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2450 if (*visited
2451 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2452 return cnt;
2454 if (gimple_nop_p (def_stmt))
2455 return cnt;
2456 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2458 unsigned i;
2459 if (!*visited)
2460 *visited = BITMAP_ALLOC (NULL);
2461 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2462 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2463 walker, data, visited, 0);
2464 return cnt;
2467 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2468 cnt++;
2469 if ((!ref
2470 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2471 && (*walker) (ref, vdef, data))
2472 return cnt;
2474 vdef = gimple_vuse (def_stmt);
2476 while (1);
2479 unsigned int
2480 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2481 bool (*walker)(ao_ref *, tree, void *), void *data,
2482 bitmap *visited)
2484 bitmap local_visited = NULL;
2485 unsigned int ret;
2487 timevar_push (TV_ALIAS_STMT_WALK);
2489 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2490 visited ? visited : &local_visited, 0);
2491 if (local_visited)
2492 BITMAP_FREE (local_visited);
2494 timevar_pop (TV_ALIAS_STMT_WALK);
2496 return ret;