Merge branch 'master' into python
[official-gcc.git] / gcc / tree-ssa-alias.c
blob10292f27a964f8262953adcfc908ea4ac3d5f76b
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "timevar.h"
30 #include "expr.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "diagnostic.h"
36 #include "tree-pretty-print.h"
37 #include "tree-dump.h"
38 #include "gimple.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "tree-pass.h"
42 #include "convert.h"
43 #include "params.h"
44 #include "ipa-type-escape.h"
45 #include "vec.h"
46 #include "bitmap.h"
47 #include "vecprim.h"
48 #include "pointer-set.h"
49 #include "alloc-pool.h"
50 #include "tree-ssa-alias.h"
52 /* Broad overview of how alias analysis on gimple works:
54 Statements clobbering or using memory are linked through the
55 virtual operand factored use-def chain. The virtual operand
56 is unique per function, its symbol is accessible via gimple_vop (cfun).
57 Virtual operands are used for efficiently walking memory statements
58 in the gimple IL and are useful for things like value-numbering as
59 a generation count for memory references.
61 SSA_NAME pointers may have associated points-to information
62 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
63 points-to information is (re-)computed by the TODO_rebuild_alias
64 pass manager todo. Points-to information is also used for more
65 precise tracking of call-clobbered and call-used variables and
66 related disambiguations.
68 This file contains functions for disambiguating memory references,
69 the so called alias-oracle and tools for walking of the gimple IL.
71 The main alias-oracle entry-points are
73 bool stmt_may_clobber_ref_p (gimple, tree)
75 This function queries if a statement may invalidate (parts of)
76 the memory designated by the reference tree argument.
78 bool ref_maybe_used_by_stmt_p (gimple, tree)
80 This function queries if a statement may need (parts of) the
81 memory designated by the reference tree argument.
83 There are variants of these functions that only handle the call
84 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
85 Note that these do not disambiguate against a possible call lhs.
87 bool refs_may_alias_p (tree, tree)
89 This function tries to disambiguate two reference trees.
91 bool ptr_deref_may_alias_global_p (tree)
93 This function queries if dereferencing a pointer variable may
94 alias global memory.
96 More low-level disambiguators are available and documented in
97 this file. Low-level disambiguators dealing with points-to
98 information are in tree-ssa-structalias.c. */
101 /* Query statistics for the different low-level disambiguators.
102 A high-level query may trigger multiple of them. */
104 static struct {
105 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
106 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
107 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
108 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
109 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
110 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
111 } alias_stats;
113 void
114 dump_alias_stats (FILE *s)
116 fprintf (s, "\nAlias oracle query stats:\n");
117 fprintf (s, " refs_may_alias_p: "
118 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
119 HOST_WIDE_INT_PRINT_DEC" queries\n",
120 alias_stats.refs_may_alias_p_no_alias,
121 alias_stats.refs_may_alias_p_no_alias
122 + alias_stats.refs_may_alias_p_may_alias);
123 fprintf (s, " ref_maybe_used_by_call_p: "
124 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
125 HOST_WIDE_INT_PRINT_DEC" queries\n",
126 alias_stats.ref_maybe_used_by_call_p_no_alias,
127 alias_stats.refs_may_alias_p_no_alias
128 + alias_stats.ref_maybe_used_by_call_p_may_alias);
129 fprintf (s, " call_may_clobber_ref_p: "
130 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
131 HOST_WIDE_INT_PRINT_DEC" queries\n",
132 alias_stats.call_may_clobber_ref_p_no_alias,
133 alias_stats.call_may_clobber_ref_p_no_alias
134 + alias_stats.call_may_clobber_ref_p_may_alias);
138 /* Return true, if dereferencing PTR may alias with a global variable. */
140 bool
141 ptr_deref_may_alias_global_p (tree ptr)
143 struct ptr_info_def *pi;
145 /* If we end up with a pointer constant here that may point
146 to global memory. */
147 if (TREE_CODE (ptr) != SSA_NAME)
148 return true;
150 pi = SSA_NAME_PTR_INFO (ptr);
152 /* If we do not have points-to information for this variable,
153 we have to punt. */
154 if (!pi)
155 return true;
157 /* ??? This does not use TBAA to prune globals ptr may not access. */
158 return pt_solution_includes_global (&pi->pt);
161 /* Return true if dereferencing PTR may alias DECL.
162 The caller is responsible for applying TBAA to see if PTR
163 may access DECL at all. */
165 static bool
166 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
168 struct ptr_info_def *pi;
170 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
171 || TREE_CODE (ptr) == ADDR_EXPR
172 || TREE_CODE (ptr) == INTEGER_CST)
173 && (TREE_CODE (decl) == VAR_DECL
174 || TREE_CODE (decl) == PARM_DECL
175 || TREE_CODE (decl) == RESULT_DECL));
177 /* Non-aliased variables can not be pointed to. */
178 if (!may_be_aliased (decl))
179 return false;
181 /* ADDR_EXPR pointers either just offset another pointer or directly
182 specify the pointed-to set. */
183 if (TREE_CODE (ptr) == ADDR_EXPR)
185 tree base = get_base_address (TREE_OPERAND (ptr, 0));
186 if (base
187 && INDIRECT_REF_P (base))
188 ptr = TREE_OPERAND (base, 0);
189 else if (base
190 && SSA_VAR_P (base))
191 return base == decl;
192 else if (base
193 && CONSTANT_CLASS_P (base))
194 return false;
195 else
196 return true;
199 /* We can end up with dereferencing constant pointers.
200 Just bail out in this case. */
201 if (TREE_CODE (ptr) == INTEGER_CST)
202 return true;
204 /* If we do not have useful points-to information for this pointer
205 we cannot disambiguate anything else. */
206 pi = SSA_NAME_PTR_INFO (ptr);
207 if (!pi)
208 return true;
210 /* If the decl can be used as a restrict tag and we have a restrict
211 pointer and that pointers points-to set doesn't contain this decl
212 then they can't alias. */
213 if (DECL_RESTRICTED_P (decl)
214 && TYPE_RESTRICT (TREE_TYPE (ptr))
215 && pi->pt.vars_contains_restrict)
216 return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
218 return pt_solution_includes (&pi->pt, decl);
221 /* Return true if dereferenced PTR1 and PTR2 may alias.
222 The caller is responsible for applying TBAA to see if accesses
223 through PTR1 and PTR2 may conflict at all. */
225 static bool
226 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
228 struct ptr_info_def *pi1, *pi2;
230 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
231 || TREE_CODE (ptr1) == ADDR_EXPR
232 || TREE_CODE (ptr1) == INTEGER_CST)
233 && (TREE_CODE (ptr2) == SSA_NAME
234 || TREE_CODE (ptr2) == ADDR_EXPR
235 || TREE_CODE (ptr2) == INTEGER_CST));
237 /* ADDR_EXPR pointers either just offset another pointer or directly
238 specify the pointed-to set. */
239 if (TREE_CODE (ptr1) == ADDR_EXPR)
241 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
242 if (base
243 && INDIRECT_REF_P (base))
244 ptr1 = TREE_OPERAND (base, 0);
245 else if (base
246 && SSA_VAR_P (base))
247 return ptr_deref_may_alias_decl_p (ptr2, base);
248 else
249 return true;
251 if (TREE_CODE (ptr2) == ADDR_EXPR)
253 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
254 if (base
255 && INDIRECT_REF_P (base))
256 ptr2 = TREE_OPERAND (base, 0);
257 else if (base
258 && SSA_VAR_P (base))
259 return ptr_deref_may_alias_decl_p (ptr1, base);
260 else
261 return true;
264 /* We can end up with dereferencing constant pointers.
265 Just bail out in this case. */
266 if (TREE_CODE (ptr1) == INTEGER_CST
267 || TREE_CODE (ptr2) == INTEGER_CST)
268 return true;
270 /* We may end up with two empty points-to solutions for two same pointers.
271 In this case we still want to say both pointers alias, so shortcut
272 that here. */
273 if (ptr1 == ptr2)
274 return true;
276 /* If we do not have useful points-to information for either pointer
277 we cannot disambiguate anything else. */
278 pi1 = SSA_NAME_PTR_INFO (ptr1);
279 pi2 = SSA_NAME_PTR_INFO (ptr2);
280 if (!pi1 || !pi2)
281 return true;
283 /* If both pointers are restrict-qualified try to disambiguate
284 with restrict information. */
285 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
286 && TYPE_RESTRICT (TREE_TYPE (ptr2))
287 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
288 return false;
290 /* ??? This does not use TBAA to prune decls from the intersection
291 that not both pointers may access. */
292 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
295 /* Return true if dereferencing PTR may alias *REF.
296 The caller is responsible for applying TBAA to see if PTR
297 may access *REF at all. */
299 static bool
300 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
302 tree base = ao_ref_base (ref);
304 if (INDIRECT_REF_P (base))
305 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
306 else if (SSA_VAR_P (base))
307 return ptr_deref_may_alias_decl_p (ptr, base);
309 return true;
313 /* Dump alias information on FILE. */
315 void
316 dump_alias_info (FILE *file)
318 size_t i;
319 const char *funcname
320 = lang_hooks.decl_printable_name (current_function_decl, 2);
321 referenced_var_iterator rvi;
322 tree var;
324 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
326 fprintf (file, "Aliased symbols\n\n");
328 FOR_EACH_REFERENCED_VAR (var, rvi)
330 if (may_be_aliased (var))
331 dump_variable (file, var);
334 fprintf (file, "\nCall clobber information\n");
336 fprintf (file, "\nESCAPED");
337 dump_points_to_solution (file, &cfun->gimple_df->escaped);
339 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
341 for (i = 1; i < num_ssa_names; i++)
343 tree ptr = ssa_name (i);
344 struct ptr_info_def *pi;
346 if (ptr == NULL_TREE
347 || SSA_NAME_IN_FREE_LIST (ptr))
348 continue;
350 pi = SSA_NAME_PTR_INFO (ptr);
351 if (pi)
352 dump_points_to_info_for (file, ptr);
355 fprintf (file, "\n");
359 /* Dump alias information on stderr. */
361 void
362 debug_alias_info (void)
364 dump_alias_info (stderr);
368 /* Return the alias information associated with pointer T. It creates a
369 new instance if none existed. */
371 struct ptr_info_def *
372 get_ptr_info (tree t)
374 struct ptr_info_def *pi;
376 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
378 pi = SSA_NAME_PTR_INFO (t);
379 if (pi == NULL)
381 pi = GGC_CNEW (struct ptr_info_def);
382 pt_solution_reset (&pi->pt);
383 SSA_NAME_PTR_INFO (t) = pi;
386 return pi;
389 /* Dump the points-to set *PT into FILE. */
391 void
392 dump_points_to_solution (FILE *file, struct pt_solution *pt)
394 if (pt->anything)
395 fprintf (file, ", points-to anything");
397 if (pt->nonlocal)
398 fprintf (file, ", points-to non-local");
400 if (pt->escaped)
401 fprintf (file, ", points-to escaped");
403 if (pt->ipa_escaped)
404 fprintf (file, ", points-to unit escaped");
406 if (pt->null)
407 fprintf (file, ", points-to NULL");
409 if (pt->vars)
411 fprintf (file, ", points-to vars: ");
412 dump_decl_set (file, pt->vars);
413 if (pt->vars_contains_global)
414 fprintf (file, " (includes global vars)");
415 if (pt->vars_contains_restrict)
416 fprintf (file, " (includes restrict tags)");
420 /* Dump points-to information for SSA_NAME PTR into FILE. */
422 void
423 dump_points_to_info_for (FILE *file, tree ptr)
425 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
427 print_generic_expr (file, ptr, dump_flags);
429 if (pi)
430 dump_points_to_solution (file, &pi->pt);
431 else
432 fprintf (file, ", points-to anything");
434 fprintf (file, "\n");
438 /* Dump points-to information for VAR into stderr. */
440 void
441 debug_points_to_info_for (tree var)
443 dump_points_to_info_for (stderr, var);
447 /* Initializes the alias-oracle reference representation *R from REF. */
449 void
450 ao_ref_init (ao_ref *r, tree ref)
452 r->ref = ref;
453 r->base = NULL_TREE;
454 r->offset = 0;
455 r->size = -1;
456 r->max_size = -1;
457 r->ref_alias_set = -1;
458 r->base_alias_set = -1;
461 /* Returns the base object of the memory reference *REF. */
463 tree
464 ao_ref_base (ao_ref *ref)
466 if (ref->base)
467 return ref->base;
468 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
469 &ref->max_size);
470 return ref->base;
473 /* Returns the base object alias set of the memory reference *REF. */
475 static alias_set_type ATTRIBUTE_UNUSED
476 ao_ref_base_alias_set (ao_ref *ref)
478 if (ref->base_alias_set != -1)
479 return ref->base_alias_set;
480 ref->base_alias_set = get_alias_set (ao_ref_base (ref));
481 return ref->base_alias_set;
484 /* Returns the reference alias set of the memory reference *REF. */
486 alias_set_type
487 ao_ref_alias_set (ao_ref *ref)
489 if (ref->ref_alias_set != -1)
490 return ref->ref_alias_set;
491 ref->ref_alias_set = get_alias_set (ref->ref);
492 return ref->ref_alias_set;
495 /* Init an alias-oracle reference representation from a gimple pointer
496 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
497 size is assumed to be unknown. The access is assumed to be only
498 to or after of the pointer target, not before it. */
500 void
501 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
503 HOST_WIDE_INT t1, t2;
504 ref->ref = NULL_TREE;
505 if (TREE_CODE (ptr) == ADDR_EXPR)
506 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
507 &ref->offset, &t1, &t2);
508 else
510 ref->base = build1 (INDIRECT_REF, char_type_node, ptr);
511 ref->offset = 0;
513 if (size
514 && host_integerp (size, 0)
515 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
516 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
517 else
518 ref->max_size = ref->size = -1;
519 ref->ref_alias_set = 0;
520 ref->base_alias_set = 0;
523 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
524 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
525 decide. */
527 static inline int
528 same_type_for_tbaa (tree type1, tree type2)
530 type1 = TYPE_MAIN_VARIANT (type1);
531 type2 = TYPE_MAIN_VARIANT (type2);
533 /* If we would have to do structural comparison bail out. */
534 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
535 || TYPE_STRUCTURAL_EQUALITY_P (type2))
536 return -1;
538 /* Compare the canonical types. */
539 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
540 return 1;
542 /* ??? Array types are not properly unified in all cases as we have
543 spurious changes in the index types for example. Removing this
544 causes all sorts of problems with the Fortran frontend. */
545 if (TREE_CODE (type1) == ARRAY_TYPE
546 && TREE_CODE (type2) == ARRAY_TYPE)
547 return -1;
549 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
550 object of one of its constrained subtypes, e.g. when a function with an
551 unconstrained parameter passed by reference is called on an object and
552 inlined. But, even in the case of a fixed size, type and subtypes are
553 not equivalent enough as to share the same TYPE_CANONICAL, since this
554 would mean that conversions between them are useless, whereas they are
555 not (e.g. type and subtypes can have different modes). So, in the end,
556 they are only guaranteed to have the same alias set. */
557 if (get_alias_set (type1) == get_alias_set (type2))
558 return -1;
560 /* The types are known to be not equal. */
561 return 0;
564 /* Determine if the two component references REF1 and REF2 which are
565 based on access types TYPE1 and TYPE2 and of which at least one is based
566 on an indirect reference may alias. REF2 is the only one that can
567 be a decl in which case REF2_IS_DECL is true.
568 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
569 are the respective alias sets. */
571 static bool
572 aliasing_component_refs_p (tree ref1, tree type1,
573 alias_set_type ref1_alias_set,
574 alias_set_type base1_alias_set,
575 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
576 tree ref2, tree type2,
577 alias_set_type ref2_alias_set,
578 alias_set_type base2_alias_set,
579 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
580 bool ref2_is_decl)
582 /* If one reference is a component references through pointers try to find a
583 common base and apply offset based disambiguation. This handles
584 for example
585 struct A { int i; int j; } *q;
586 struct B { struct A a; int k; } *p;
587 disambiguating q->i and p->a.j. */
588 tree *refp;
589 int same_p;
591 /* Now search for the type1 in the access path of ref2. This
592 would be a common base for doing offset based disambiguation on. */
593 refp = &ref2;
594 while (handled_component_p (*refp)
595 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
596 refp = &TREE_OPERAND (*refp, 0);
597 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
598 /* If we couldn't compare types we have to bail out. */
599 if (same_p == -1)
600 return true;
601 else if (same_p == 1)
603 HOST_WIDE_INT offadj, sztmp, msztmp;
604 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
605 offset2 -= offadj;
606 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
608 /* If we didn't find a common base, try the other way around. */
609 refp = &ref1;
610 while (handled_component_p (*refp)
611 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
612 refp = &TREE_OPERAND (*refp, 0);
613 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
614 /* If we couldn't compare types we have to bail out. */
615 if (same_p == -1)
616 return true;
617 else if (same_p == 1)
619 HOST_WIDE_INT offadj, sztmp, msztmp;
620 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
621 offset1 -= offadj;
622 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
625 /* If we have two type access paths B1.path1 and B2.path2 they may
626 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
627 But we can still have a path that goes B1.path1...B2.path2 with
628 a part that we do not see. So we can only disambiguate now
629 if there is no B2 in the tail of path1 and no B1 on the
630 tail of path2. */
631 if (base1_alias_set == ref2_alias_set
632 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
633 return true;
634 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
635 if (!ref2_is_decl)
636 return (base2_alias_set == ref1_alias_set
637 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
638 return false;
641 /* Return true if two memory references based on the variables BASE1
642 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
643 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
645 static bool
646 decl_refs_may_alias_p (tree base1,
647 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
648 tree base2,
649 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
651 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
653 /* If both references are based on different variables, they cannot alias. */
654 if (base1 != base2)
655 return false;
657 /* If both references are based on the same variable, they cannot alias if
658 the accesses do not overlap. */
659 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
662 /* Return true if an indirect reference based on *PTR1 constrained
663 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
664 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
665 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
666 in which case they are computed on-demand. REF1 and REF2
667 if non-NULL are the complete memory reference trees. */
669 static bool
670 indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
671 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
672 alias_set_type ref1_alias_set,
673 alias_set_type base1_alias_set,
674 tree ref2, tree base2,
675 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
676 alias_set_type ref2_alias_set,
677 alias_set_type base2_alias_set)
679 /* If only one reference is based on a variable, they cannot alias if
680 the pointer access is beyond the extent of the variable access.
681 (the pointer base cannot validly point to an offset less than zero
682 of the variable).
683 They also cannot alias if the pointer may not point to the decl. */
684 if (max_size2 != -1
685 && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
686 return false;
687 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
688 return false;
690 /* Disambiguations that rely on strict aliasing rules follow. */
691 if (!flag_strict_aliasing)
692 return true;
694 /* If the alias set for a pointer access is zero all bets are off. */
695 if (base1_alias_set == -1)
696 base1_alias_set = get_deref_alias_set (ptr1);
697 if (base1_alias_set == 0)
698 return true;
699 if (base2_alias_set == -1)
700 base2_alias_set = get_alias_set (base2);
702 /* If both references are through the same type, they do not alias
703 if the accesses do not overlap. This does extra disambiguation
704 for mixed/pointer accesses but requires strict aliasing. */
705 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
706 TREE_TYPE (base2)) == 1)
707 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
709 /* The only way to access a variable is through a pointer dereference
710 of the same alias set or a subset of it. */
711 if (base1_alias_set != base2_alias_set
712 && !alias_set_subset_of (base1_alias_set, base2_alias_set))
713 return false;
715 /* Do access-path based disambiguation. */
716 if (ref1 && ref2
717 && handled_component_p (ref1)
718 && handled_component_p (ref2))
719 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
720 ref1_alias_set, base1_alias_set,
721 offset1, max_size1,
722 ref2, TREE_TYPE (base2),
723 ref2_alias_set, base2_alias_set,
724 offset2, max_size2, true);
726 return true;
729 /* Return true if two indirect references based on *PTR1
730 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
731 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
732 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
733 in which case they are computed on-demand. REF1 and REF2
734 if non-NULL are the complete memory reference trees. */
736 static bool
737 indirect_refs_may_alias_p (tree ref1, tree ptr1,
738 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
739 alias_set_type ref1_alias_set,
740 alias_set_type base1_alias_set,
741 tree ref2, tree ptr2,
742 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
743 alias_set_type ref2_alias_set,
744 alias_set_type base2_alias_set)
746 /* If both bases are based on pointers they cannot alias if they may not
747 point to the same memory object or if they point to the same object
748 and the accesses do not overlap. */
749 if (operand_equal_p (ptr1, ptr2, 0))
750 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
751 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
752 return false;
754 /* Disambiguations that rely on strict aliasing rules follow. */
755 if (!flag_strict_aliasing)
756 return true;
758 /* If the alias set for a pointer access is zero all bets are off. */
759 if (base1_alias_set == -1)
760 base1_alias_set = get_deref_alias_set (ptr1);
761 if (base1_alias_set == 0)
762 return true;
763 if (base2_alias_set == -1)
764 base2_alias_set = get_deref_alias_set (ptr2);
765 if (base2_alias_set == 0)
766 return true;
768 /* If both references are through the same type, they do not alias
769 if the accesses do not overlap. This does extra disambiguation
770 for mixed/pointer accesses but requires strict aliasing. */
771 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
772 TREE_TYPE (TREE_TYPE (ptr2))) == 1)
773 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
775 /* Do type-based disambiguation. */
776 if (base1_alias_set != base2_alias_set
777 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
778 return false;
780 /* Do access-path based disambiguation. */
781 if (ref1 && ref2
782 && handled_component_p (ref1)
783 && handled_component_p (ref2))
784 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
785 ref1_alias_set, base1_alias_set,
786 offset1, max_size1,
787 ref2, TREE_TYPE (TREE_TYPE (ptr2)),
788 ref2_alias_set, base2_alias_set,
789 offset2, max_size2, false);
791 return true;
794 /* Return true, if the two memory references REF1 and REF2 may alias. */
796 bool
797 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
799 tree base1, base2;
800 HOST_WIDE_INT offset1 = 0, offset2 = 0;
801 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
802 bool var1_p, var2_p, ind1_p, ind2_p;
803 alias_set_type set;
805 gcc_assert ((!ref1->ref
806 || SSA_VAR_P (ref1->ref)
807 || handled_component_p (ref1->ref)
808 || INDIRECT_REF_P (ref1->ref)
809 || TREE_CODE (ref1->ref) == TARGET_MEM_REF
810 || TREE_CODE (ref1->ref) == CONST_DECL)
811 && (!ref2->ref
812 || SSA_VAR_P (ref2->ref)
813 || handled_component_p (ref2->ref)
814 || INDIRECT_REF_P (ref2->ref)
815 || TREE_CODE (ref2->ref) == TARGET_MEM_REF
816 || TREE_CODE (ref2->ref) == CONST_DECL));
818 /* Decompose the references into their base objects and the access. */
819 base1 = ao_ref_base (ref1);
820 offset1 = ref1->offset;
821 max_size1 = ref1->max_size;
822 base2 = ao_ref_base (ref2);
823 offset2 = ref2->offset;
824 max_size2 = ref2->max_size;
826 /* We can end up with registers or constants as bases for example from
827 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
828 which is seen as a struct copy. */
829 if (TREE_CODE (base1) == SSA_NAME
830 || TREE_CODE (base2) == SSA_NAME
831 || TREE_CODE (base1) == CONST_DECL
832 || TREE_CODE (base2) == CONST_DECL
833 || is_gimple_min_invariant (base1)
834 || is_gimple_min_invariant (base2))
835 return false;
837 /* We can end up refering to code via function decls. As we likely
838 do not properly track code aliases conservatively bail out. */
839 if (TREE_CODE (base1) == FUNCTION_DECL
840 || TREE_CODE (base2) == FUNCTION_DECL)
841 return true;
843 /* Defer to simple offset based disambiguation if we have
844 references based on two decls. Do this before defering to
845 TBAA to handle must-alias cases in conformance with the
846 GCC extension of allowing type-punning through unions. */
847 var1_p = SSA_VAR_P (base1);
848 var2_p = SSA_VAR_P (base2);
849 if (var1_p && var2_p)
850 return decl_refs_may_alias_p (base1, offset1, max_size1,
851 base2, offset2, max_size2);
853 ind1_p = INDIRECT_REF_P (base1);
854 ind2_p = INDIRECT_REF_P (base2);
855 /* Canonicalize the pointer-vs-decl case. */
856 if (ind1_p && var2_p)
858 HOST_WIDE_INT tmp1;
859 tree tmp2;
860 ao_ref *tmp3;
861 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
862 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
863 tmp2 = base1; base1 = base2; base2 = tmp2;
864 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
865 var1_p = true;
866 ind1_p = false;
867 var2_p = false;
868 ind2_p = true;
871 /* If we are about to disambiguate pointer-vs-decl try harder to
872 see must-aliases and give leeway to some invalid cases.
873 This covers a pretty minimal set of cases only and does not
874 when called from the RTL oracle. It handles cases like
876 int i = 1;
877 return *(float *)&i;
879 and also fixes gfortran.dg/lto/pr40725. */
880 if (var1_p && ind2_p
881 && cfun
882 && gimple_in_ssa_p (cfun)
883 && TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME)
885 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base2, 0));
886 while (is_gimple_assign (def_stmt)
887 && (gimple_assign_rhs_code (def_stmt) == SSA_NAME
888 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))))
890 tree rhs = gimple_assign_rhs1 (def_stmt);
891 HOST_WIDE_INT offset, size, max_size;
893 /* Look through SSA name copies and pointer conversions. */
894 if (TREE_CODE (rhs) == SSA_NAME
895 && POINTER_TYPE_P (TREE_TYPE (rhs)))
897 def_stmt = SSA_NAME_DEF_STMT (rhs);
898 continue;
900 if (TREE_CODE (rhs) != ADDR_EXPR)
901 break;
903 /* If the pointer is defined as an address based on a decl
904 use plain offset disambiguation and ignore TBAA. */
905 rhs = TREE_OPERAND (rhs, 0);
906 rhs = get_ref_base_and_extent (rhs, &offset, &size, &max_size);
907 if (SSA_VAR_P (rhs))
909 base2 = rhs;
910 offset2 += offset;
911 if (size != max_size
912 || max_size == -1)
913 max_size2 = -1;
914 return decl_refs_may_alias_p (base1, offset1, max_size1,
915 base2, offset2, max_size2);
918 /* Do not continue looking through &p->x to limit time
919 complexity. */
920 break;
924 /* First defer to TBAA if possible. */
925 if (tbaa_p
926 && flag_strict_aliasing
927 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
928 ao_ref_alias_set (ref2)))
929 return false;
931 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
932 TBAA disambiguation based on the access type is possible, so bail
933 out only after that check. */
934 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
935 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
936 return true;
938 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
939 set = tbaa_p ? -1 : 0;
940 if (var1_p && ind2_p)
941 return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
942 offset2, max_size2,
943 ao_ref_alias_set (ref2), set,
944 ref1->ref, base1,
945 offset1, max_size1,
946 ao_ref_alias_set (ref1), set);
947 else if (ind1_p && ind2_p)
948 return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
949 offset1, max_size1,
950 ao_ref_alias_set (ref1), set,
951 ref2->ref, TREE_OPERAND (base2, 0),
952 offset2, max_size2,
953 ao_ref_alias_set (ref2), set);
955 gcc_unreachable ();
958 bool
959 refs_may_alias_p (tree ref1, tree ref2)
961 ao_ref r1, r2;
962 bool res;
963 ao_ref_init (&r1, ref1);
964 ao_ref_init (&r2, ref2);
965 res = refs_may_alias_p_1 (&r1, &r2, true);
966 if (res)
967 ++alias_stats.refs_may_alias_p_may_alias;
968 else
969 ++alias_stats.refs_may_alias_p_no_alias;
970 return res;
973 /* Returns true if there is a anti-dependence for the STORE that
974 executes after the LOAD. */
976 bool
977 refs_anti_dependent_p (tree load, tree store)
979 ao_ref r1, r2;
980 ao_ref_init (&r1, load);
981 ao_ref_init (&r2, store);
982 return refs_may_alias_p_1 (&r1, &r2, false);
985 /* Returns true if there is a output dependence for the stores
986 STORE1 and STORE2. */
988 bool
989 refs_output_dependent_p (tree store1, tree store2)
991 ao_ref r1, r2;
992 ao_ref_init (&r1, store1);
993 ao_ref_init (&r2, store2);
994 return refs_may_alias_p_1 (&r1, &r2, false);
997 /* If the call CALL may use the memory reference REF return true,
998 otherwise return false. */
1000 static bool
1001 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1003 tree base, callee;
1004 unsigned i;
1005 int flags = gimple_call_flags (call);
1007 /* Const functions without a static chain do not implicitly use memory. */
1008 if (!gimple_call_chain (call)
1009 && (flags & (ECF_CONST|ECF_NOVOPS)))
1010 goto process_args;
1012 base = ao_ref_base (ref);
1013 if (!base)
1014 return true;
1016 /* If the reference is based on a decl that is not aliased the call
1017 cannot possibly use it. */
1018 if (DECL_P (base)
1019 && !may_be_aliased (base)
1020 /* But local statics can be used through recursion. */
1021 && !is_global_var (base))
1022 goto process_args;
1024 callee = gimple_call_fndecl (call);
1026 /* Handle those builtin functions explicitly that do not act as
1027 escape points. See tree-ssa-structalias.c:find_func_aliases
1028 for the list of builtins we might need to handle here. */
1029 if (callee != NULL_TREE
1030 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1031 switch (DECL_FUNCTION_CODE (callee))
1033 /* All the following functions clobber memory pointed to by
1034 their first argument. */
1035 case BUILT_IN_STRCPY:
1036 case BUILT_IN_STRNCPY:
1037 case BUILT_IN_MEMCPY:
1038 case BUILT_IN_MEMMOVE:
1039 case BUILT_IN_MEMPCPY:
1040 case BUILT_IN_STPCPY:
1041 case BUILT_IN_STPNCPY:
1042 case BUILT_IN_STRCAT:
1043 case BUILT_IN_STRNCAT:
1045 ao_ref dref;
1046 tree size = NULL_TREE;
1047 if (gimple_call_num_args (call) == 3)
1048 size = gimple_call_arg (call, 2);
1049 ao_ref_init_from_ptr_and_size (&dref,
1050 gimple_call_arg (call, 1),
1051 size);
1052 return refs_may_alias_p_1 (&dref, ref, false);
1054 case BUILT_IN_BCOPY:
1056 ao_ref dref;
1057 tree size = gimple_call_arg (call, 2);
1058 ao_ref_init_from_ptr_and_size (&dref,
1059 gimple_call_arg (call, 0),
1060 size);
1061 return refs_may_alias_p_1 (&dref, ref, false);
1063 /* The following builtins do not read from memory. */
1064 case BUILT_IN_FREE:
1065 case BUILT_IN_MALLOC:
1066 case BUILT_IN_CALLOC:
1067 case BUILT_IN_MEMSET:
1068 case BUILT_IN_FREXP:
1069 case BUILT_IN_FREXPF:
1070 case BUILT_IN_FREXPL:
1071 case BUILT_IN_GAMMA_R:
1072 case BUILT_IN_GAMMAF_R:
1073 case BUILT_IN_GAMMAL_R:
1074 case BUILT_IN_LGAMMA_R:
1075 case BUILT_IN_LGAMMAF_R:
1076 case BUILT_IN_LGAMMAL_R:
1077 case BUILT_IN_MODF:
1078 case BUILT_IN_MODFF:
1079 case BUILT_IN_MODFL:
1080 case BUILT_IN_REMQUO:
1081 case BUILT_IN_REMQUOF:
1082 case BUILT_IN_REMQUOL:
1083 case BUILT_IN_SINCOS:
1084 case BUILT_IN_SINCOSF:
1085 case BUILT_IN_SINCOSL:
1086 return false;
1088 default:
1089 /* Fallthru to general call handling. */;
1092 /* Check if base is a global static variable that is not read
1093 by the function. */
1094 if (TREE_CODE (base) == VAR_DECL
1095 && TREE_STATIC (base))
1097 bitmap not_read;
1099 if (callee != NULL_TREE
1100 && (not_read
1101 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1102 && bitmap_bit_p (not_read, DECL_UID (base)))
1103 goto process_args;
1106 /* Check if the base variable is call-used. */
1107 if (DECL_P (base))
1109 if (pt_solution_includes (gimple_call_use_set (call), base))
1110 return true;
1112 else if (INDIRECT_REF_P (base)
1113 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1115 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1116 if (!pi)
1117 return true;
1119 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1120 return true;
1122 else
1123 return true;
1125 /* Inspect call arguments for passed-by-value aliases. */
1126 process_args:
1127 for (i = 0; i < gimple_call_num_args (call); ++i)
1129 tree op = gimple_call_arg (call, i);
1130 int flags = gimple_call_arg_flags (call, i);
1132 if (flags & EAF_UNUSED)
1133 continue;
1135 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1136 op = TREE_OPERAND (op, 0);
1138 if (TREE_CODE (op) != SSA_NAME
1139 && !is_gimple_min_invariant (op))
1141 ao_ref r;
1142 ao_ref_init (&r, op);
1143 if (refs_may_alias_p_1 (&r, ref, true))
1144 return true;
1148 return false;
1151 static bool
1152 ref_maybe_used_by_call_p (gimple call, tree ref)
1154 ao_ref r;
1155 bool res;
1156 ao_ref_init (&r, ref);
1157 res = ref_maybe_used_by_call_p_1 (call, &r);
1158 if (res)
1159 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1160 else
1161 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1162 return res;
1166 /* If the statement STMT may use the memory reference REF return
1167 true, otherwise return false. */
1169 bool
1170 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1172 if (is_gimple_assign (stmt))
1174 tree rhs;
1176 /* All memory assign statements are single. */
1177 if (!gimple_assign_single_p (stmt))
1178 return false;
1180 rhs = gimple_assign_rhs1 (stmt);
1181 if (is_gimple_reg (rhs)
1182 || is_gimple_min_invariant (rhs)
1183 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1184 return false;
1186 return refs_may_alias_p (rhs, ref);
1188 else if (is_gimple_call (stmt))
1189 return ref_maybe_used_by_call_p (stmt, ref);
1191 return true;
1194 /* If the call in statement CALL may clobber the memory reference REF
1195 return true, otherwise return false. */
1197 static bool
1198 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1200 tree base;
1201 tree callee;
1203 /* If the call is pure or const it cannot clobber anything. */
1204 if (gimple_call_flags (call)
1205 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1206 return false;
1208 base = ao_ref_base (ref);
1209 if (!base)
1210 return true;
1212 if (TREE_CODE (base) == SSA_NAME
1213 || CONSTANT_CLASS_P (base))
1214 return false;
1216 /* If the reference is based on a decl that is not aliased the call
1217 cannot possibly clobber it. */
1218 if (DECL_P (base)
1219 && !may_be_aliased (base)
1220 /* But local non-readonly statics can be modified through recursion
1221 or the call may implement a threading barrier which we must
1222 treat as may-def. */
1223 && (TREE_READONLY (base)
1224 || !is_global_var (base)))
1225 return false;
1227 callee = gimple_call_fndecl (call);
1229 /* Handle those builtin functions explicitly that do not act as
1230 escape points. See tree-ssa-structalias.c:find_func_aliases
1231 for the list of builtins we might need to handle here. */
1232 if (callee != NULL_TREE
1233 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1234 switch (DECL_FUNCTION_CODE (callee))
1236 /* All the following functions clobber memory pointed to by
1237 their first argument. */
1238 case BUILT_IN_STRCPY:
1239 case BUILT_IN_STRNCPY:
1240 case BUILT_IN_MEMCPY:
1241 case BUILT_IN_MEMMOVE:
1242 case BUILT_IN_MEMPCPY:
1243 case BUILT_IN_STPCPY:
1244 case BUILT_IN_STPNCPY:
1245 case BUILT_IN_STRCAT:
1246 case BUILT_IN_STRNCAT:
1247 case BUILT_IN_MEMSET:
1249 ao_ref dref;
1250 tree size = NULL_TREE;
1251 if (gimple_call_num_args (call) == 3)
1252 size = gimple_call_arg (call, 2);
1253 ao_ref_init_from_ptr_and_size (&dref,
1254 gimple_call_arg (call, 0),
1255 size);
1256 return refs_may_alias_p_1 (&dref, ref, false);
1258 case BUILT_IN_BCOPY:
1260 ao_ref dref;
1261 tree size = gimple_call_arg (call, 2);
1262 ao_ref_init_from_ptr_and_size (&dref,
1263 gimple_call_arg (call, 1),
1264 size);
1265 return refs_may_alias_p_1 (&dref, ref, false);
1267 /* Allocating memory does not have any side-effects apart from
1268 being the definition point for the pointer. */
1269 case BUILT_IN_MALLOC:
1270 case BUILT_IN_CALLOC:
1271 /* Unix98 specifies that errno is set on allocation failure.
1272 Until we properly can track the errno location assume it
1273 is not a local decl but external or anonymous storage in
1274 a different translation unit. Also assume it is of
1275 type int as required by the standard. */
1276 if (flag_errno_math
1277 && TREE_TYPE (base) == integer_type_node)
1279 struct ptr_info_def *pi;
1280 if (DECL_P (base)
1281 && !TREE_STATIC (base))
1282 return true;
1283 else if (INDIRECT_REF_P (base)
1284 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1285 && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1286 return pi->pt.anything || pi->pt.nonlocal;
1288 return false;
1289 /* Freeing memory kills the pointed-to memory. More importantly
1290 the call has to serve as a barrier for moving loads and stores
1291 across it. */
1292 case BUILT_IN_FREE:
1294 tree ptr = gimple_call_arg (call, 0);
1295 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1297 case BUILT_IN_GAMMA_R:
1298 case BUILT_IN_GAMMAF_R:
1299 case BUILT_IN_GAMMAL_R:
1300 case BUILT_IN_LGAMMA_R:
1301 case BUILT_IN_LGAMMAF_R:
1302 case BUILT_IN_LGAMMAL_R:
1304 tree out = gimple_call_arg (call, 1);
1305 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1306 return true;
1307 if (flag_errno_math)
1308 break;
1309 return false;
1311 case BUILT_IN_FREXP:
1312 case BUILT_IN_FREXPF:
1313 case BUILT_IN_FREXPL:
1314 case BUILT_IN_MODF:
1315 case BUILT_IN_MODFF:
1316 case BUILT_IN_MODFL:
1318 tree out = gimple_call_arg (call, 1);
1319 return ptr_deref_may_alias_ref_p_1 (out, ref);
1321 case BUILT_IN_REMQUO:
1322 case BUILT_IN_REMQUOF:
1323 case BUILT_IN_REMQUOL:
1325 tree out = gimple_call_arg (call, 2);
1326 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1327 return true;
1328 if (flag_errno_math)
1329 break;
1330 return false;
1332 case BUILT_IN_SINCOS:
1333 case BUILT_IN_SINCOSF:
1334 case BUILT_IN_SINCOSL:
1336 tree sin = gimple_call_arg (call, 1);
1337 tree cos = gimple_call_arg (call, 2);
1338 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1339 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1341 default:
1342 /* Fallthru to general call handling. */;
1345 /* Check if base is a global static variable that is not written
1346 by the function. */
1347 if (callee != NULL_TREE
1348 && TREE_CODE (base) == VAR_DECL
1349 && TREE_STATIC (base))
1351 bitmap not_written;
1353 if ((not_written
1354 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1355 && bitmap_bit_p (not_written, DECL_UID (base)))
1356 return false;
1359 /* Check if the base variable is call-clobbered. */
1360 if (DECL_P (base))
1361 return pt_solution_includes (gimple_call_clobber_set (call), base);
1362 else if (INDIRECT_REF_P (base)
1363 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1365 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1366 if (!pi)
1367 return true;
1369 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1372 return true;
1375 /* If the call in statement CALL may clobber the memory reference REF
1376 return true, otherwise return false. */
1378 bool
1379 call_may_clobber_ref_p (gimple call, tree ref)
1381 bool res;
1382 ao_ref r;
1383 ao_ref_init (&r, ref);
1384 res = call_may_clobber_ref_p_1 (call, &r);
1385 if (res)
1386 ++alias_stats.call_may_clobber_ref_p_may_alias;
1387 else
1388 ++alias_stats.call_may_clobber_ref_p_no_alias;
1389 return res;
1393 /* If the statement STMT may clobber the memory reference REF return true,
1394 otherwise return false. */
1396 bool
1397 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1399 if (is_gimple_call (stmt))
1401 tree lhs = gimple_call_lhs (stmt);
1402 if (lhs
1403 && !is_gimple_reg (lhs))
1405 ao_ref r;
1406 ao_ref_init (&r, lhs);
1407 if (refs_may_alias_p_1 (ref, &r, true))
1408 return true;
1411 return call_may_clobber_ref_p_1 (stmt, ref);
1413 else if (is_gimple_assign (stmt))
1415 ao_ref r;
1416 ao_ref_init (&r, gimple_assign_lhs (stmt));
1417 return refs_may_alias_p_1 (ref, &r, true);
1419 else if (gimple_code (stmt) == GIMPLE_ASM)
1420 return true;
1422 return false;
1425 bool
1426 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1428 ao_ref r;
1429 ao_ref_init (&r, ref);
1430 return stmt_may_clobber_ref_p_1 (stmt, &r);
1434 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1435 TARGET or a statement clobbering the memory reference REF in which
1436 case false is returned. The walk starts with VUSE, one argument of PHI. */
1438 static bool
1439 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1440 tree vuse, bitmap *visited)
1442 if (!*visited)
1443 *visited = BITMAP_ALLOC (NULL);
1445 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1447 /* Walk until we hit the target. */
1448 while (vuse != target)
1450 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1451 /* Recurse for PHI nodes. */
1452 if (gimple_code (def_stmt) == GIMPLE_PHI)
1454 /* An already visited PHI node ends the walk successfully. */
1455 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1456 return true;
1457 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1458 if (!vuse)
1459 return false;
1460 continue;
1462 /* A clobbering statement or the end of the IL ends it failing. */
1463 else if (gimple_nop_p (def_stmt)
1464 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1465 return false;
1466 vuse = gimple_vuse (def_stmt);
1468 return true;
1471 /* Starting from a PHI node for the virtual operand of the memory reference
1472 REF find a continuation virtual operand that allows to continue walking
1473 statements dominating PHI skipping only statements that cannot possibly
1474 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1475 be found. */
1477 tree
1478 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1480 unsigned nargs = gimple_phi_num_args (phi);
1482 /* Through a single-argument PHI we can simply look through. */
1483 if (nargs == 1)
1484 return PHI_ARG_DEF (phi, 0);
1486 /* For two arguments try to skip non-aliasing code until we hit
1487 the phi argument definition that dominates the other one. */
1488 if (nargs == 2)
1490 tree arg0 = PHI_ARG_DEF (phi, 0);
1491 tree arg1 = PHI_ARG_DEF (phi, 1);
1492 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1493 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1494 tree common_vuse;
1496 if (arg0 == arg1)
1497 return arg0;
1498 else if (gimple_nop_p (def0)
1499 || (!gimple_nop_p (def1)
1500 && dominated_by_p (CDI_DOMINATORS,
1501 gimple_bb (def1), gimple_bb (def0))))
1503 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1504 return arg0;
1506 else if (gimple_nop_p (def1)
1507 || dominated_by_p (CDI_DOMINATORS,
1508 gimple_bb (def0), gimple_bb (def1)))
1510 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1511 return arg1;
1513 /* Special case of a diamond:
1514 MEM_1 = ...
1515 goto (cond) ? L1 : L2
1516 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1517 goto L3
1518 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1519 L3: MEM_4 = PHI<MEM_2, MEM_3>
1520 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1521 dominate each other, but still we can easily skip this PHI node
1522 if we recognize that the vuse MEM operand is the same for both,
1523 and that we can skip both statements (they don't clobber us).
1524 This is still linear. Don't use maybe_skip_until, that might
1525 potentially be slow. */
1526 else if ((common_vuse = gimple_vuse (def0))
1527 && common_vuse == gimple_vuse (def1))
1529 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1530 && !stmt_may_clobber_ref_p_1 (def1, ref))
1531 return common_vuse;
1535 return NULL_TREE;
1538 /* Based on the memory reference REF and its virtual use VUSE call
1539 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1540 itself. That is, for each virtual use for which its defining statement
1541 does not clobber REF.
1543 WALKER is called with REF, the current virtual use and DATA. If
1544 WALKER returns non-NULL the walk stops and its result is returned.
1545 At the end of a non-successful walk NULL is returned.
1547 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1548 use which definition is a statement that may clobber REF and DATA.
1549 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1550 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1551 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1552 to adjust REF and *DATA to make that valid.
1554 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1556 void *
1557 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1558 void *(*walker)(ao_ref *, tree, void *),
1559 void *(*translate)(ao_ref *, tree, void *), void *data)
1561 bitmap visited = NULL;
1562 void *res;
1564 timevar_push (TV_ALIAS_STMT_WALK);
1568 gimple def_stmt;
1570 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1571 res = (*walker) (ref, vuse, data);
1572 if (res)
1573 break;
1575 def_stmt = SSA_NAME_DEF_STMT (vuse);
1576 if (gimple_nop_p (def_stmt))
1577 break;
1578 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1579 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1580 else
1582 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1584 if (!translate)
1585 break;
1586 res = (*translate) (ref, vuse, data);
1587 /* Failed lookup and translation. */
1588 if (res == (void *)-1)
1590 res = NULL;
1591 break;
1593 /* Lookup succeeded. */
1594 else if (res != NULL)
1595 break;
1596 /* Translation succeeded, continue walking. */
1598 vuse = gimple_vuse (def_stmt);
1601 while (vuse);
1603 if (visited)
1604 BITMAP_FREE (visited);
1606 timevar_pop (TV_ALIAS_STMT_WALK);
1608 return res;
1612 /* Based on the memory reference REF call WALKER for each vdef which
1613 defining statement may clobber REF, starting with VDEF. If REF
1614 is NULL_TREE, each defining statement is visited.
1616 WALKER is called with REF, the current vdef and DATA. If WALKER
1617 returns true the walk is stopped, otherwise it continues.
1619 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1620 PHI argument (but only one walk continues on merge points), the
1621 return value is true if any of the walks was successful.
1623 The function returns the number of statements walked. */
1625 static unsigned int
1626 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1627 bool (*walker)(ao_ref *, tree, void *), void *data,
1628 bitmap *visited, unsigned int cnt)
1632 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1634 if (*visited
1635 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1636 return cnt;
1638 if (gimple_nop_p (def_stmt))
1639 return cnt;
1640 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1642 unsigned i;
1643 if (!*visited)
1644 *visited = BITMAP_ALLOC (NULL);
1645 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1646 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1647 walker, data, visited, 0);
1648 return cnt;
1651 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1652 cnt++;
1653 if ((!ref
1654 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1655 && (*walker) (ref, vdef, data))
1656 return cnt;
1658 vdef = gimple_vuse (def_stmt);
1660 while (1);
1663 unsigned int
1664 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1665 bool (*walker)(ao_ref *, tree, void *), void *data,
1666 bitmap *visited)
1668 bitmap local_visited = NULL;
1669 unsigned int ret;
1671 timevar_push (TV_ALIAS_STMT_WALK);
1673 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1674 visited ? visited : &local_visited, 0);
1675 if (local_visited)
1676 BITMAP_FREE (local_visited);
1678 timevar_pop (TV_ALIAS_STMT_WALK);
1680 return ret;