PR c++/54021
[official-gcc.git] / gcc / tree-ssa-alias.c
blob9631843df414d432e5e761878db93aed7c699b25
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 "target.h"
29 #include "basic-block.h"
30 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "tree-pretty-print.h"
36 #include "dumpfile.h"
37 #include "gimple.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "params.h"
41 #include "vec.h"
42 #include "bitmap.h"
43 #include "vecprim.h"
44 #include "pointer-set.h"
45 #include "alloc-pool.h"
46 #include "tree-ssa-alias.h"
48 /* Broad overview of how alias analysis on gimple works:
50 Statements clobbering or using memory are linked through the
51 virtual operand factored use-def chain. The virtual operand
52 is unique per function, its symbol is accessible via gimple_vop (cfun).
53 Virtual operands are used for efficiently walking memory statements
54 in the gimple IL and are useful for things like value-numbering as
55 a generation count for memory references.
57 SSA_NAME pointers may have associated points-to information
58 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
59 points-to information is (re-)computed by the TODO_rebuild_alias
60 pass manager todo. Points-to information is also used for more
61 precise tracking of call-clobbered and call-used variables and
62 related disambiguations.
64 This file contains functions for disambiguating memory references,
65 the so called alias-oracle and tools for walking of the gimple IL.
67 The main alias-oracle entry-points are
69 bool stmt_may_clobber_ref_p (gimple, tree)
71 This function queries if a statement may invalidate (parts of)
72 the memory designated by the reference tree argument.
74 bool ref_maybe_used_by_stmt_p (gimple, tree)
76 This function queries if a statement may need (parts of) the
77 memory designated by the reference tree argument.
79 There are variants of these functions that only handle the call
80 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
81 Note that these do not disambiguate against a possible call lhs.
83 bool refs_may_alias_p (tree, tree)
85 This function tries to disambiguate two reference trees.
87 bool ptr_deref_may_alias_global_p (tree)
89 This function queries if dereferencing a pointer variable may
90 alias global memory.
92 More low-level disambiguators are available and documented in
93 this file. Low-level disambiguators dealing with points-to
94 information are in tree-ssa-structalias.c. */
97 /* Query statistics for the different low-level disambiguators.
98 A high-level query may trigger multiple of them. */
100 static struct {
101 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
102 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
103 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
104 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
105 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
106 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
107 } alias_stats;
109 void
110 dump_alias_stats (FILE *s)
112 fprintf (s, "\nAlias oracle query stats:\n");
113 fprintf (s, " refs_may_alias_p: "
114 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
115 HOST_WIDE_INT_PRINT_DEC" queries\n",
116 alias_stats.refs_may_alias_p_no_alias,
117 alias_stats.refs_may_alias_p_no_alias
118 + alias_stats.refs_may_alias_p_may_alias);
119 fprintf (s, " ref_maybe_used_by_call_p: "
120 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
121 HOST_WIDE_INT_PRINT_DEC" queries\n",
122 alias_stats.ref_maybe_used_by_call_p_no_alias,
123 alias_stats.refs_may_alias_p_no_alias
124 + alias_stats.ref_maybe_used_by_call_p_may_alias);
125 fprintf (s, " call_may_clobber_ref_p: "
126 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
127 HOST_WIDE_INT_PRINT_DEC" queries\n",
128 alias_stats.call_may_clobber_ref_p_no_alias,
129 alias_stats.call_may_clobber_ref_p_no_alias
130 + alias_stats.call_may_clobber_ref_p_may_alias);
134 /* Return true, if dereferencing PTR may alias with a global variable. */
136 bool
137 ptr_deref_may_alias_global_p (tree ptr)
139 struct ptr_info_def *pi;
141 /* If we end up with a pointer constant here that may point
142 to global memory. */
143 if (TREE_CODE (ptr) != SSA_NAME)
144 return true;
146 pi = SSA_NAME_PTR_INFO (ptr);
148 /* If we do not have points-to information for this variable,
149 we have to punt. */
150 if (!pi)
151 return true;
153 /* ??? This does not use TBAA to prune globals ptr may not access. */
154 return pt_solution_includes_global (&pi->pt);
157 /* Return true if dereferencing PTR may alias DECL.
158 The caller is responsible for applying TBAA to see if PTR
159 may access DECL at all. */
161 static bool
162 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
164 struct ptr_info_def *pi;
166 /* Conversions are irrelevant for points-to information and
167 data-dependence analysis can feed us those. */
168 STRIP_NOPS (ptr);
170 /* Anything we do not explicilty handle aliases. */
171 if ((TREE_CODE (ptr) != SSA_NAME
172 && TREE_CODE (ptr) != ADDR_EXPR
173 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
174 || !POINTER_TYPE_P (TREE_TYPE (ptr))
175 || (TREE_CODE (decl) != VAR_DECL
176 && TREE_CODE (decl) != PARM_DECL
177 && TREE_CODE (decl) != RESULT_DECL))
178 return true;
180 /* Disregard pointer offsetting. */
181 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
185 ptr = TREE_OPERAND (ptr, 0);
187 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
188 return ptr_deref_may_alias_decl_p (ptr, decl);
191 /* ADDR_EXPR pointers either just offset another pointer or directly
192 specify the pointed-to set. */
193 if (TREE_CODE (ptr) == ADDR_EXPR)
195 tree base = get_base_address (TREE_OPERAND (ptr, 0));
196 if (base
197 && (TREE_CODE (base) == MEM_REF
198 || TREE_CODE (base) == TARGET_MEM_REF))
199 ptr = TREE_OPERAND (base, 0);
200 else if (base
201 && DECL_P (base))
202 return base == decl;
203 else if (base
204 && CONSTANT_CLASS_P (base))
205 return false;
206 else
207 return true;
210 /* Non-aliased variables can not be pointed to. */
211 if (!may_be_aliased (decl))
212 return false;
214 /* If we do not have useful points-to information for this pointer
215 we cannot disambiguate anything else. */
216 pi = SSA_NAME_PTR_INFO (ptr);
217 if (!pi)
218 return true;
220 return pt_solution_includes (&pi->pt, decl);
223 /* Return true if dereferenced PTR1 and PTR2 may alias.
224 The caller is responsible for applying TBAA to see if accesses
225 through PTR1 and PTR2 may conflict at all. */
227 bool
228 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
230 struct ptr_info_def *pi1, *pi2;
232 /* Conversions are irrelevant for points-to information and
233 data-dependence analysis can feed us those. */
234 STRIP_NOPS (ptr1);
235 STRIP_NOPS (ptr2);
237 /* Disregard pointer offsetting. */
238 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
242 ptr1 = TREE_OPERAND (ptr1, 0);
244 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
245 return ptr_derefs_may_alias_p (ptr1, ptr2);
247 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
251 ptr2 = TREE_OPERAND (ptr2, 0);
253 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
254 return ptr_derefs_may_alias_p (ptr1, ptr2);
257 /* ADDR_EXPR pointers either just offset another pointer or directly
258 specify the pointed-to set. */
259 if (TREE_CODE (ptr1) == ADDR_EXPR)
261 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
262 if (base
263 && (TREE_CODE (base) == MEM_REF
264 || TREE_CODE (base) == TARGET_MEM_REF))
265 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
266 else if (base
267 && DECL_P (base))
268 return ptr_deref_may_alias_decl_p (ptr2, base);
269 else
270 return true;
272 if (TREE_CODE (ptr2) == ADDR_EXPR)
274 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
275 if (base
276 && (TREE_CODE (base) == MEM_REF
277 || TREE_CODE (base) == TARGET_MEM_REF))
278 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
279 else if (base
280 && DECL_P (base))
281 return ptr_deref_may_alias_decl_p (ptr1, base);
282 else
283 return true;
286 /* From here we require SSA name pointers. Anything else aliases. */
287 if (TREE_CODE (ptr1) != SSA_NAME
288 || TREE_CODE (ptr2) != SSA_NAME
289 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
290 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
291 return true;
293 /* We may end up with two empty points-to solutions for two same pointers.
294 In this case we still want to say both pointers alias, so shortcut
295 that here. */
296 if (ptr1 == ptr2)
297 return true;
299 /* If we do not have useful points-to information for either pointer
300 we cannot disambiguate anything else. */
301 pi1 = SSA_NAME_PTR_INFO (ptr1);
302 pi2 = SSA_NAME_PTR_INFO (ptr2);
303 if (!pi1 || !pi2)
304 return true;
306 /* ??? This does not use TBAA to prune decls from the intersection
307 that not both pointers may access. */
308 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
311 /* Return true if dereferencing PTR may alias *REF.
312 The caller is responsible for applying TBAA to see if PTR
313 may access *REF at all. */
315 static bool
316 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
318 tree base = ao_ref_base (ref);
320 if (TREE_CODE (base) == MEM_REF
321 || TREE_CODE (base) == TARGET_MEM_REF)
322 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
323 else if (DECL_P (base))
324 return ptr_deref_may_alias_decl_p (ptr, base);
326 return true;
329 /* Return true whether REF may refer to global memory. */
331 bool
332 ref_may_alias_global_p (tree ref)
334 tree base = get_base_address (ref);
335 if (DECL_P (base))
336 return is_global_var (base);
337 else if (TREE_CODE (base) == MEM_REF
338 || TREE_CODE (base) == TARGET_MEM_REF)
339 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
340 return true;
343 /* Return true whether STMT may clobber global memory. */
345 bool
346 stmt_may_clobber_global_p (gimple stmt)
348 tree lhs;
350 if (!gimple_vdef (stmt))
351 return false;
353 /* ??? We can ask the oracle whether an artificial pointer
354 dereference with a pointer with points-to information covering
355 all global memory (what about non-address taken memory?) maybe
356 clobbered by this call. As there is at the moment no convenient
357 way of doing that without generating garbage do some manual
358 checking instead.
359 ??? We could make a NULL ao_ref argument to the various
360 predicates special, meaning any global memory. */
362 switch (gimple_code (stmt))
364 case GIMPLE_ASSIGN:
365 lhs = gimple_assign_lhs (stmt);
366 return (TREE_CODE (lhs) != SSA_NAME
367 && ref_may_alias_global_p (lhs));
368 case GIMPLE_CALL:
369 return true;
370 default:
371 return true;
376 /* Dump alias information on FILE. */
378 void
379 dump_alias_info (FILE *file)
381 size_t i;
382 const char *funcname
383 = lang_hooks.decl_printable_name (current_function_decl, 2);
384 referenced_var_iterator rvi;
385 tree var;
387 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
389 fprintf (file, "Aliased symbols\n\n");
391 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
393 if (may_be_aliased (var))
394 dump_variable (file, var);
397 fprintf (file, "\nCall clobber information\n");
399 fprintf (file, "\nESCAPED");
400 dump_points_to_solution (file, &cfun->gimple_df->escaped);
402 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
404 for (i = 1; i < num_ssa_names; i++)
406 tree ptr = ssa_name (i);
407 struct ptr_info_def *pi;
409 if (ptr == NULL_TREE
410 || SSA_NAME_IN_FREE_LIST (ptr))
411 continue;
413 pi = SSA_NAME_PTR_INFO (ptr);
414 if (pi)
415 dump_points_to_info_for (file, ptr);
418 fprintf (file, "\n");
422 /* Dump alias information on stderr. */
424 DEBUG_FUNCTION void
425 debug_alias_info (void)
427 dump_alias_info (stderr);
431 /* Dump the points-to set *PT into FILE. */
433 void
434 dump_points_to_solution (FILE *file, struct pt_solution *pt)
436 if (pt->anything)
437 fprintf (file, ", points-to anything");
439 if (pt->nonlocal)
440 fprintf (file, ", points-to non-local");
442 if (pt->escaped)
443 fprintf (file, ", points-to escaped");
445 if (pt->ipa_escaped)
446 fprintf (file, ", points-to unit escaped");
448 if (pt->null)
449 fprintf (file, ", points-to NULL");
451 if (pt->vars)
453 fprintf (file, ", points-to vars: ");
454 dump_decl_set (file, pt->vars);
455 if (pt->vars_contains_global)
456 fprintf (file, " (includes global vars)");
460 /* Dump points-to information for SSA_NAME PTR into FILE. */
462 void
463 dump_points_to_info_for (FILE *file, tree ptr)
465 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
467 print_generic_expr (file, ptr, dump_flags);
469 if (pi)
470 dump_points_to_solution (file, &pi->pt);
471 else
472 fprintf (file, ", points-to anything");
474 fprintf (file, "\n");
478 /* Dump points-to information for VAR into stderr. */
480 DEBUG_FUNCTION void
481 debug_points_to_info_for (tree var)
483 dump_points_to_info_for (stderr, var);
487 /* Initializes the alias-oracle reference representation *R from REF. */
489 void
490 ao_ref_init (ao_ref *r, tree ref)
492 r->ref = ref;
493 r->base = NULL_TREE;
494 r->offset = 0;
495 r->size = -1;
496 r->max_size = -1;
497 r->ref_alias_set = -1;
498 r->base_alias_set = -1;
499 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
502 /* Returns the base object of the memory reference *REF. */
504 tree
505 ao_ref_base (ao_ref *ref)
507 if (ref->base)
508 return ref->base;
509 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
510 &ref->max_size);
511 return ref->base;
514 /* Returns the base object alias set of the memory reference *REF. */
516 static alias_set_type
517 ao_ref_base_alias_set (ao_ref *ref)
519 tree base_ref;
520 if (ref->base_alias_set != -1)
521 return ref->base_alias_set;
522 if (!ref->ref)
523 return 0;
524 base_ref = ref->ref;
525 while (handled_component_p (base_ref))
526 base_ref = TREE_OPERAND (base_ref, 0);
527 ref->base_alias_set = get_alias_set (base_ref);
528 return ref->base_alias_set;
531 /* Returns the reference alias set of the memory reference *REF. */
533 alias_set_type
534 ao_ref_alias_set (ao_ref *ref)
536 if (ref->ref_alias_set != -1)
537 return ref->ref_alias_set;
538 ref->ref_alias_set = get_alias_set (ref->ref);
539 return ref->ref_alias_set;
542 /* Init an alias-oracle reference representation from a gimple pointer
543 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
544 size is assumed to be unknown. The access is assumed to be only
545 to or after of the pointer target, not before it. */
547 void
548 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
550 HOST_WIDE_INT t1, t2;
551 ref->ref = NULL_TREE;
552 if (TREE_CODE (ptr) == ADDR_EXPR)
553 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
554 &ref->offset, &t1, &t2);
555 else
557 ref->base = build2 (MEM_REF, char_type_node,
558 ptr, null_pointer_node);
559 ref->offset = 0;
561 if (size
562 && host_integerp (size, 0)
563 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
564 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
565 else
566 ref->max_size = ref->size = -1;
567 ref->ref_alias_set = 0;
568 ref->base_alias_set = 0;
569 ref->volatile_p = false;
572 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
573 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
574 decide. */
576 static inline int
577 same_type_for_tbaa (tree type1, tree type2)
579 type1 = TYPE_MAIN_VARIANT (type1);
580 type2 = TYPE_MAIN_VARIANT (type2);
582 /* If we would have to do structural comparison bail out. */
583 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
584 || TYPE_STRUCTURAL_EQUALITY_P (type2))
585 return -1;
587 /* Compare the canonical types. */
588 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
589 return 1;
591 /* ??? Array types are not properly unified in all cases as we have
592 spurious changes in the index types for example. Removing this
593 causes all sorts of problems with the Fortran frontend. */
594 if (TREE_CODE (type1) == ARRAY_TYPE
595 && TREE_CODE (type2) == ARRAY_TYPE)
596 return -1;
598 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
599 object of one of its constrained subtypes, e.g. when a function with an
600 unconstrained parameter passed by reference is called on an object and
601 inlined. But, even in the case of a fixed size, type and subtypes are
602 not equivalent enough as to share the same TYPE_CANONICAL, since this
603 would mean that conversions between them are useless, whereas they are
604 not (e.g. type and subtypes can have different modes). So, in the end,
605 they are only guaranteed to have the same alias set. */
606 if (get_alias_set (type1) == get_alias_set (type2))
607 return -1;
609 /* The types are known to be not equal. */
610 return 0;
613 /* Determine if the two component references REF1 and REF2 which are
614 based on access types TYPE1 and TYPE2 and of which at least one is based
615 on an indirect reference may alias. REF2 is the only one that can
616 be a decl in which case REF2_IS_DECL is true.
617 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
618 are the respective alias sets. */
620 static bool
621 aliasing_component_refs_p (tree ref1,
622 alias_set_type ref1_alias_set,
623 alias_set_type base1_alias_set,
624 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
625 tree ref2,
626 alias_set_type ref2_alias_set,
627 alias_set_type base2_alias_set,
628 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
629 bool ref2_is_decl)
631 /* If one reference is a component references through pointers try to find a
632 common base and apply offset based disambiguation. This handles
633 for example
634 struct A { int i; int j; } *q;
635 struct B { struct A a; int k; } *p;
636 disambiguating q->i and p->a.j. */
637 tree base1, base2;
638 tree type1, type2;
639 tree *refp;
640 int same_p;
642 /* Choose bases and base types to search for. */
643 base1 = ref1;
644 while (handled_component_p (base1))
645 base1 = TREE_OPERAND (base1, 0);
646 type1 = TREE_TYPE (base1);
647 base2 = ref2;
648 while (handled_component_p (base2))
649 base2 = TREE_OPERAND (base2, 0);
650 type2 = TREE_TYPE (base2);
652 /* Now search for the type1 in the access path of ref2. This
653 would be a common base for doing offset based disambiguation on. */
654 refp = &ref2;
655 while (handled_component_p (*refp)
656 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
657 refp = &TREE_OPERAND (*refp, 0);
658 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
659 /* If we couldn't compare types we have to bail out. */
660 if (same_p == -1)
661 return true;
662 else if (same_p == 1)
664 HOST_WIDE_INT offadj, sztmp, msztmp;
665 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
666 offset2 -= offadj;
667 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
668 offset1 -= offadj;
669 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
671 /* If we didn't find a common base, try the other way around. */
672 refp = &ref1;
673 while (handled_component_p (*refp)
674 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
675 refp = &TREE_OPERAND (*refp, 0);
676 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
677 /* If we couldn't compare types we have to bail out. */
678 if (same_p == -1)
679 return true;
680 else if (same_p == 1)
682 HOST_WIDE_INT offadj, sztmp, msztmp;
683 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
684 offset1 -= offadj;
685 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
686 offset2 -= offadj;
687 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
690 /* If we have two type access paths B1.path1 and B2.path2 they may
691 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
692 But we can still have a path that goes B1.path1...B2.path2 with
693 a part that we do not see. So we can only disambiguate now
694 if there is no B2 in the tail of path1 and no B1 on the
695 tail of path2. */
696 if (base1_alias_set == ref2_alias_set
697 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
698 return true;
699 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
700 if (!ref2_is_decl)
701 return (base2_alias_set == ref1_alias_set
702 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
703 return false;
706 /* Return true if two memory references based on the variables BASE1
707 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
708 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
710 static bool
711 decl_refs_may_alias_p (tree base1,
712 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
713 tree base2,
714 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
716 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
718 /* If both references are based on different variables, they cannot alias. */
719 if (base1 != base2)
720 return false;
722 /* If both references are based on the same variable, they cannot alias if
723 the accesses do not overlap. */
724 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
727 /* Return true if an indirect reference based on *PTR1 constrained
728 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
729 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
730 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
731 in which case they are computed on-demand. REF1 and REF2
732 if non-NULL are the complete memory reference trees. */
734 static bool
735 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
736 HOST_WIDE_INT offset1,
737 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
738 alias_set_type ref1_alias_set,
739 alias_set_type base1_alias_set,
740 tree ref2 ATTRIBUTE_UNUSED, tree base2,
741 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
742 alias_set_type ref2_alias_set,
743 alias_set_type base2_alias_set, bool tbaa_p)
745 tree ptr1;
746 tree ptrtype1, dbase2;
747 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
748 HOST_WIDE_INT doffset1, doffset2;
749 double_int moff;
751 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
752 || TREE_CODE (base1) == TARGET_MEM_REF)
753 && DECL_P (base2));
755 ptr1 = TREE_OPERAND (base1, 0);
757 /* The offset embedded in MEM_REFs can be negative. Bias them
758 so that the resulting offset adjustment is positive. */
759 moff = mem_ref_offset (base1);
760 moff = double_int_lshift (moff,
761 BITS_PER_UNIT == 8
762 ? 3 : exact_log2 (BITS_PER_UNIT),
763 HOST_BITS_PER_DOUBLE_INT, true);
764 if (double_int_negative_p (moff))
765 offset2p += double_int_neg (moff).low;
766 else
767 offset1p += moff.low;
769 /* If only one reference is based on a variable, they cannot alias if
770 the pointer access is beyond the extent of the variable access.
771 (the pointer base cannot validly point to an offset less than zero
772 of the variable).
773 ??? IVOPTs creates bases that do not honor this restriction,
774 so do not apply this optimization for TARGET_MEM_REFs. */
775 if (TREE_CODE (base1) != TARGET_MEM_REF
776 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
777 return false;
778 /* They also cannot alias if the pointer may not point to the decl. */
779 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
780 return false;
782 /* Disambiguations that rely on strict aliasing rules follow. */
783 if (!flag_strict_aliasing || !tbaa_p)
784 return true;
786 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
788 /* If the alias set for a pointer access is zero all bets are off. */
789 if (base1_alias_set == -1)
790 base1_alias_set = get_deref_alias_set (ptrtype1);
791 if (base1_alias_set == 0)
792 return true;
793 if (base2_alias_set == -1)
794 base2_alias_set = get_alias_set (base2);
796 /* When we are trying to disambiguate an access with a pointer dereference
797 as base versus one with a decl as base we can use both the size
798 of the decl and its dynamic type for extra disambiguation.
799 ??? We do not know anything about the dynamic type of the decl
800 other than that its alias-set contains base2_alias_set as a subset
801 which does not help us here. */
802 /* As we know nothing useful about the dynamic type of the decl just
803 use the usual conflict check rather than a subset test.
804 ??? We could introduce -fvery-strict-aliasing when the language
805 does not allow decls to have a dynamic type that differs from their
806 static type. Then we can check
807 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
808 if (base1_alias_set != base2_alias_set
809 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
810 return false;
811 /* If the size of the access relevant for TBAA through the pointer
812 is bigger than the size of the decl we can't possibly access the
813 decl via that pointer. */
814 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
815 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
816 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
817 /* ??? This in turn may run afoul when a decl of type T which is
818 a member of union type U is accessed through a pointer to
819 type U and sizeof T is smaller than sizeof U. */
820 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
821 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
822 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
823 return false;
825 if (!ref2)
826 return true;
828 /* If the decl is accessed via a MEM_REF, reconstruct the base
829 we can use for TBAA and an appropriately adjusted offset. */
830 dbase2 = ref2;
831 while (handled_component_p (dbase2))
832 dbase2 = TREE_OPERAND (dbase2, 0);
833 doffset1 = offset1;
834 doffset2 = offset2;
835 if (TREE_CODE (dbase2) == MEM_REF
836 || TREE_CODE (dbase2) == TARGET_MEM_REF)
838 double_int moff = mem_ref_offset (dbase2);
839 moff = double_int_lshift (moff,
840 BITS_PER_UNIT == 8
841 ? 3 : exact_log2 (BITS_PER_UNIT),
842 HOST_BITS_PER_DOUBLE_INT, true);
843 if (double_int_negative_p (moff))
844 doffset1 -= double_int_neg (moff).low;
845 else
846 doffset2 -= moff.low;
849 /* If either reference is view-converted, give up now. */
850 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
851 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
852 return true;
854 /* If both references are through the same type, they do not alias
855 if the accesses do not overlap. This does extra disambiguation
856 for mixed/pointer accesses but requires strict aliasing.
857 For MEM_REFs we require that the component-ref offset we computed
858 is relative to the start of the type which we ensure by
859 comparing rvalue and access type and disregarding the constant
860 pointer offset. */
861 if ((TREE_CODE (base1) != TARGET_MEM_REF
862 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
863 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
864 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
866 /* Do access-path based disambiguation. */
867 if (ref1 && ref2
868 && (handled_component_p (ref1) || handled_component_p (ref2)))
869 return aliasing_component_refs_p (ref1,
870 ref1_alias_set, base1_alias_set,
871 offset1, max_size1,
872 ref2,
873 ref2_alias_set, base2_alias_set,
874 offset2, max_size2, true);
876 return true;
879 /* Return true if two indirect references based on *PTR1
880 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
881 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
882 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
883 in which case they are computed on-demand. REF1 and REF2
884 if non-NULL are the complete memory reference trees. */
886 static bool
887 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
888 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
889 alias_set_type ref1_alias_set,
890 alias_set_type base1_alias_set,
891 tree ref2 ATTRIBUTE_UNUSED, tree base2,
892 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
893 alias_set_type ref2_alias_set,
894 alias_set_type base2_alias_set, bool tbaa_p)
896 tree ptr1;
897 tree ptr2;
898 tree ptrtype1, ptrtype2;
900 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
901 || TREE_CODE (base1) == TARGET_MEM_REF)
902 && (TREE_CODE (base2) == MEM_REF
903 || TREE_CODE (base2) == TARGET_MEM_REF));
905 ptr1 = TREE_OPERAND (base1, 0);
906 ptr2 = TREE_OPERAND (base2, 0);
908 /* If both bases are based on pointers they cannot alias if they may not
909 point to the same memory object or if they point to the same object
910 and the accesses do not overlap. */
911 if ((!cfun || gimple_in_ssa_p (cfun))
912 && operand_equal_p (ptr1, ptr2, 0)
913 && (((TREE_CODE (base1) != TARGET_MEM_REF
914 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
915 && (TREE_CODE (base2) != TARGET_MEM_REF
916 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
917 || (TREE_CODE (base1) == TARGET_MEM_REF
918 && TREE_CODE (base2) == TARGET_MEM_REF
919 && (TMR_STEP (base1) == TMR_STEP (base2)
920 || (TMR_STEP (base1) && TMR_STEP (base2)
921 && operand_equal_p (TMR_STEP (base1),
922 TMR_STEP (base2), 0)))
923 && (TMR_INDEX (base1) == TMR_INDEX (base2)
924 || (TMR_INDEX (base1) && TMR_INDEX (base2)
925 && operand_equal_p (TMR_INDEX (base1),
926 TMR_INDEX (base2), 0)))
927 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
928 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
929 && operand_equal_p (TMR_INDEX2 (base1),
930 TMR_INDEX2 (base2), 0))))))
932 double_int moff;
933 /* The offset embedded in MEM_REFs can be negative. Bias them
934 so that the resulting offset adjustment is positive. */
935 moff = mem_ref_offset (base1);
936 moff = double_int_lshift (moff,
937 BITS_PER_UNIT == 8
938 ? 3 : exact_log2 (BITS_PER_UNIT),
939 HOST_BITS_PER_DOUBLE_INT, true);
940 if (double_int_negative_p (moff))
941 offset2 += double_int_neg (moff).low;
942 else
943 offset1 += moff.low;
944 moff = mem_ref_offset (base2);
945 moff = double_int_lshift (moff,
946 BITS_PER_UNIT == 8
947 ? 3 : exact_log2 (BITS_PER_UNIT),
948 HOST_BITS_PER_DOUBLE_INT, true);
949 if (double_int_negative_p (moff))
950 offset1 += double_int_neg (moff).low;
951 else
952 offset2 += moff.low;
953 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
955 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
956 return false;
958 /* Disambiguations that rely on strict aliasing rules follow. */
959 if (!flag_strict_aliasing || !tbaa_p)
960 return true;
962 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
963 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
965 /* If the alias set for a pointer access is zero all bets are off. */
966 if (base1_alias_set == -1)
967 base1_alias_set = get_deref_alias_set (ptrtype1);
968 if (base1_alias_set == 0)
969 return true;
970 if (base2_alias_set == -1)
971 base2_alias_set = get_deref_alias_set (ptrtype2);
972 if (base2_alias_set == 0)
973 return true;
975 /* If both references are through the same type, they do not alias
976 if the accesses do not overlap. This does extra disambiguation
977 for mixed/pointer accesses but requires strict aliasing. */
978 if ((TREE_CODE (base1) != TARGET_MEM_REF
979 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
980 && (TREE_CODE (base2) != TARGET_MEM_REF
981 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
982 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
983 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
984 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
985 TREE_TYPE (ptrtype2)) == 1)
986 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
988 /* Do type-based disambiguation. */
989 if (base1_alias_set != base2_alias_set
990 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
991 return false;
993 /* Do access-path based disambiguation. */
994 if (ref1 && ref2
995 && (handled_component_p (ref1) || handled_component_p (ref2))
996 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
997 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
998 return aliasing_component_refs_p (ref1,
999 ref1_alias_set, base1_alias_set,
1000 offset1, max_size1,
1001 ref2,
1002 ref2_alias_set, base2_alias_set,
1003 offset2, max_size2, false);
1005 return true;
1008 /* Return true, if the two memory references REF1 and REF2 may alias. */
1010 bool
1011 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1013 tree base1, base2;
1014 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1015 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1016 bool var1_p, var2_p, ind1_p, ind2_p;
1018 gcc_checking_assert ((!ref1->ref
1019 || TREE_CODE (ref1->ref) == SSA_NAME
1020 || DECL_P (ref1->ref)
1021 || TREE_CODE (ref1->ref) == STRING_CST
1022 || handled_component_p (ref1->ref)
1023 || TREE_CODE (ref1->ref) == MEM_REF
1024 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1025 && (!ref2->ref
1026 || TREE_CODE (ref2->ref) == SSA_NAME
1027 || DECL_P (ref2->ref)
1028 || TREE_CODE (ref2->ref) == STRING_CST
1029 || handled_component_p (ref2->ref)
1030 || TREE_CODE (ref2->ref) == MEM_REF
1031 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1033 /* Decompose the references into their base objects and the access. */
1034 base1 = ao_ref_base (ref1);
1035 offset1 = ref1->offset;
1036 max_size1 = ref1->max_size;
1037 base2 = ao_ref_base (ref2);
1038 offset2 = ref2->offset;
1039 max_size2 = ref2->max_size;
1041 /* We can end up with registers or constants as bases for example from
1042 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1043 which is seen as a struct copy. */
1044 if (TREE_CODE (base1) == SSA_NAME
1045 || TREE_CODE (base1) == CONST_DECL
1046 || TREE_CODE (base1) == CONSTRUCTOR
1047 || TREE_CODE (base1) == ADDR_EXPR
1048 || CONSTANT_CLASS_P (base1)
1049 || TREE_CODE (base2) == SSA_NAME
1050 || TREE_CODE (base2) == CONST_DECL
1051 || TREE_CODE (base2) == CONSTRUCTOR
1052 || TREE_CODE (base2) == ADDR_EXPR
1053 || CONSTANT_CLASS_P (base2))
1054 return false;
1056 /* We can end up referring to code via function and label decls.
1057 As we likely do not properly track code aliases conservatively
1058 bail out. */
1059 if (TREE_CODE (base1) == FUNCTION_DECL
1060 || TREE_CODE (base1) == LABEL_DECL
1061 || TREE_CODE (base2) == FUNCTION_DECL
1062 || TREE_CODE (base2) == LABEL_DECL)
1063 return true;
1065 /* Two volatile accesses always conflict. */
1066 if (ref1->volatile_p
1067 && ref2->volatile_p)
1068 return true;
1070 /* Defer to simple offset based disambiguation if we have
1071 references based on two decls. Do this before defering to
1072 TBAA to handle must-alias cases in conformance with the
1073 GCC extension of allowing type-punning through unions. */
1074 var1_p = DECL_P (base1);
1075 var2_p = DECL_P (base2);
1076 if (var1_p && var2_p)
1077 return decl_refs_may_alias_p (base1, offset1, max_size1,
1078 base2, offset2, max_size2);
1080 ind1_p = (TREE_CODE (base1) == MEM_REF
1081 || TREE_CODE (base1) == TARGET_MEM_REF);
1082 ind2_p = (TREE_CODE (base2) == MEM_REF
1083 || TREE_CODE (base2) == TARGET_MEM_REF);
1085 /* Canonicalize the pointer-vs-decl case. */
1086 if (ind1_p && var2_p)
1088 HOST_WIDE_INT tmp1;
1089 tree tmp2;
1090 ao_ref *tmp3;
1091 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1092 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1093 tmp2 = base1; base1 = base2; base2 = tmp2;
1094 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1095 var1_p = true;
1096 ind1_p = false;
1097 var2_p = false;
1098 ind2_p = true;
1101 /* First defer to TBAA if possible. */
1102 if (tbaa_p
1103 && flag_strict_aliasing
1104 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1105 ao_ref_alias_set (ref2)))
1106 return false;
1108 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1109 if (var1_p && ind2_p)
1110 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1111 offset2, max_size2,
1112 ao_ref_alias_set (ref2), -1,
1113 ref1->ref, base1,
1114 offset1, max_size1,
1115 ao_ref_alias_set (ref1),
1116 ao_ref_base_alias_set (ref1),
1117 tbaa_p);
1118 else if (ind1_p && ind2_p)
1119 return indirect_refs_may_alias_p (ref1->ref, base1,
1120 offset1, max_size1,
1121 ao_ref_alias_set (ref1), -1,
1122 ref2->ref, base2,
1123 offset2, max_size2,
1124 ao_ref_alias_set (ref2), -1,
1125 tbaa_p);
1127 /* We really do not want to end up here, but returning true is safe. */
1128 #ifdef ENABLE_CHECKING
1129 gcc_unreachable ();
1130 #else
1131 return true;
1132 #endif
1135 bool
1136 refs_may_alias_p (tree ref1, tree ref2)
1138 ao_ref r1, r2;
1139 bool res;
1140 ao_ref_init (&r1, ref1);
1141 ao_ref_init (&r2, ref2);
1142 res = refs_may_alias_p_1 (&r1, &r2, true);
1143 if (res)
1144 ++alias_stats.refs_may_alias_p_may_alias;
1145 else
1146 ++alias_stats.refs_may_alias_p_no_alias;
1147 return res;
1150 /* Returns true if there is a anti-dependence for the STORE that
1151 executes after the LOAD. */
1153 bool
1154 refs_anti_dependent_p (tree load, tree store)
1156 ao_ref r1, r2;
1157 ao_ref_init (&r1, load);
1158 ao_ref_init (&r2, store);
1159 return refs_may_alias_p_1 (&r1, &r2, false);
1162 /* Returns true if there is a output dependence for the stores
1163 STORE1 and STORE2. */
1165 bool
1166 refs_output_dependent_p (tree store1, tree store2)
1168 ao_ref r1, r2;
1169 ao_ref_init (&r1, store1);
1170 ao_ref_init (&r2, store2);
1171 return refs_may_alias_p_1 (&r1, &r2, false);
1174 /* If the call CALL may use the memory reference REF return true,
1175 otherwise return false. */
1177 static bool
1178 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1180 tree base, callee;
1181 unsigned i;
1182 int flags = gimple_call_flags (call);
1184 /* Const functions without a static chain do not implicitly use memory. */
1185 if (!gimple_call_chain (call)
1186 && (flags & (ECF_CONST|ECF_NOVOPS)))
1187 goto process_args;
1189 base = ao_ref_base (ref);
1190 if (!base)
1191 return true;
1193 /* A call that is not without side-effects might involve volatile
1194 accesses and thus conflicts with all other volatile accesses. */
1195 if (ref->volatile_p)
1196 return true;
1198 /* If the reference is based on a decl that is not aliased the call
1199 cannot possibly use it. */
1200 if (DECL_P (base)
1201 && !may_be_aliased (base)
1202 /* But local statics can be used through recursion. */
1203 && !is_global_var (base))
1204 goto process_args;
1206 callee = gimple_call_fndecl (call);
1208 /* Handle those builtin functions explicitly that do not act as
1209 escape points. See tree-ssa-structalias.c:find_func_aliases
1210 for the list of builtins we might need to handle here. */
1211 if (callee != NULL_TREE
1212 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1213 switch (DECL_FUNCTION_CODE (callee))
1215 /* All the following functions read memory pointed to by
1216 their second argument. strcat/strncat additionally
1217 reads memory pointed to by the first argument. */
1218 case BUILT_IN_STRCAT:
1219 case BUILT_IN_STRNCAT:
1221 ao_ref dref;
1222 ao_ref_init_from_ptr_and_size (&dref,
1223 gimple_call_arg (call, 0),
1224 NULL_TREE);
1225 if (refs_may_alias_p_1 (&dref, ref, false))
1226 return true;
1228 /* FALLTHRU */
1229 case BUILT_IN_STRCPY:
1230 case BUILT_IN_STRNCPY:
1231 case BUILT_IN_MEMCPY:
1232 case BUILT_IN_MEMMOVE:
1233 case BUILT_IN_MEMPCPY:
1234 case BUILT_IN_STPCPY:
1235 case BUILT_IN_STPNCPY:
1236 case BUILT_IN_TM_MEMCPY:
1237 case BUILT_IN_TM_MEMMOVE:
1239 ao_ref dref;
1240 tree size = NULL_TREE;
1241 if (gimple_call_num_args (call) == 3)
1242 size = gimple_call_arg (call, 2);
1243 ao_ref_init_from_ptr_and_size (&dref,
1244 gimple_call_arg (call, 1),
1245 size);
1246 return refs_may_alias_p_1 (&dref, ref, false);
1248 case BUILT_IN_STRCAT_CHK:
1249 case BUILT_IN_STRNCAT_CHK:
1251 ao_ref dref;
1252 ao_ref_init_from_ptr_and_size (&dref,
1253 gimple_call_arg (call, 0),
1254 NULL_TREE);
1255 if (refs_may_alias_p_1 (&dref, ref, false))
1256 return true;
1258 /* FALLTHRU */
1259 case BUILT_IN_STRCPY_CHK:
1260 case BUILT_IN_STRNCPY_CHK:
1261 case BUILT_IN_MEMCPY_CHK:
1262 case BUILT_IN_MEMMOVE_CHK:
1263 case BUILT_IN_MEMPCPY_CHK:
1264 case BUILT_IN_STPCPY_CHK:
1265 case BUILT_IN_STPNCPY_CHK:
1267 ao_ref dref;
1268 tree size = NULL_TREE;
1269 if (gimple_call_num_args (call) == 4)
1270 size = gimple_call_arg (call, 2);
1271 ao_ref_init_from_ptr_and_size (&dref,
1272 gimple_call_arg (call, 1),
1273 size);
1274 return refs_may_alias_p_1 (&dref, ref, false);
1276 case BUILT_IN_BCOPY:
1278 ao_ref dref;
1279 tree size = gimple_call_arg (call, 2);
1280 ao_ref_init_from_ptr_and_size (&dref,
1281 gimple_call_arg (call, 0),
1282 size);
1283 return refs_may_alias_p_1 (&dref, ref, false);
1286 /* The following functions read memory pointed to by their
1287 first argument. */
1288 CASE_BUILT_IN_TM_LOAD (1):
1289 CASE_BUILT_IN_TM_LOAD (2):
1290 CASE_BUILT_IN_TM_LOAD (4):
1291 CASE_BUILT_IN_TM_LOAD (8):
1292 CASE_BUILT_IN_TM_LOAD (FLOAT):
1293 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1294 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1295 CASE_BUILT_IN_TM_LOAD (M64):
1296 CASE_BUILT_IN_TM_LOAD (M128):
1297 CASE_BUILT_IN_TM_LOAD (M256):
1298 case BUILT_IN_TM_LOG:
1299 case BUILT_IN_TM_LOG_1:
1300 case BUILT_IN_TM_LOG_2:
1301 case BUILT_IN_TM_LOG_4:
1302 case BUILT_IN_TM_LOG_8:
1303 case BUILT_IN_TM_LOG_FLOAT:
1304 case BUILT_IN_TM_LOG_DOUBLE:
1305 case BUILT_IN_TM_LOG_LDOUBLE:
1306 case BUILT_IN_TM_LOG_M64:
1307 case BUILT_IN_TM_LOG_M128:
1308 case BUILT_IN_TM_LOG_M256:
1309 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1311 /* These read memory pointed to by the first argument. */
1312 case BUILT_IN_STRDUP:
1313 case BUILT_IN_STRNDUP:
1315 ao_ref dref;
1316 tree size = NULL_TREE;
1317 if (gimple_call_num_args (call) == 2)
1318 size = gimple_call_arg (call, 1);
1319 ao_ref_init_from_ptr_and_size (&dref,
1320 gimple_call_arg (call, 0),
1321 size);
1322 return refs_may_alias_p_1 (&dref, ref, false);
1324 /* The following builtins do not read from memory. */
1325 case BUILT_IN_FREE:
1326 case BUILT_IN_MALLOC:
1327 case BUILT_IN_CALLOC:
1328 case BUILT_IN_ALLOCA:
1329 case BUILT_IN_ALLOCA_WITH_ALIGN:
1330 case BUILT_IN_STACK_SAVE:
1331 case BUILT_IN_STACK_RESTORE:
1332 case BUILT_IN_MEMSET:
1333 case BUILT_IN_TM_MEMSET:
1334 case BUILT_IN_MEMSET_CHK:
1335 case BUILT_IN_FREXP:
1336 case BUILT_IN_FREXPF:
1337 case BUILT_IN_FREXPL:
1338 case BUILT_IN_GAMMA_R:
1339 case BUILT_IN_GAMMAF_R:
1340 case BUILT_IN_GAMMAL_R:
1341 case BUILT_IN_LGAMMA_R:
1342 case BUILT_IN_LGAMMAF_R:
1343 case BUILT_IN_LGAMMAL_R:
1344 case BUILT_IN_MODF:
1345 case BUILT_IN_MODFF:
1346 case BUILT_IN_MODFL:
1347 case BUILT_IN_REMQUO:
1348 case BUILT_IN_REMQUOF:
1349 case BUILT_IN_REMQUOL:
1350 case BUILT_IN_SINCOS:
1351 case BUILT_IN_SINCOSF:
1352 case BUILT_IN_SINCOSL:
1353 case BUILT_IN_ASSUME_ALIGNED:
1354 case BUILT_IN_VA_END:
1355 return false;
1356 /* __sync_* builtins and some OpenMP builtins act as threading
1357 barriers. */
1358 #undef DEF_SYNC_BUILTIN
1359 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1360 #include "sync-builtins.def"
1361 #undef DEF_SYNC_BUILTIN
1362 case BUILT_IN_GOMP_ATOMIC_START:
1363 case BUILT_IN_GOMP_ATOMIC_END:
1364 case BUILT_IN_GOMP_BARRIER:
1365 case BUILT_IN_GOMP_TASKWAIT:
1366 case BUILT_IN_GOMP_CRITICAL_START:
1367 case BUILT_IN_GOMP_CRITICAL_END:
1368 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1369 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1370 case BUILT_IN_GOMP_LOOP_END:
1371 case BUILT_IN_GOMP_ORDERED_START:
1372 case BUILT_IN_GOMP_ORDERED_END:
1373 case BUILT_IN_GOMP_PARALLEL_END:
1374 case BUILT_IN_GOMP_SECTIONS_END:
1375 case BUILT_IN_GOMP_SINGLE_COPY_START:
1376 case BUILT_IN_GOMP_SINGLE_COPY_END:
1377 return true;
1379 default:
1380 /* Fallthru to general call handling. */;
1383 /* Check if base is a global static variable that is not read
1384 by the function. */
1385 if (callee != NULL_TREE
1386 && TREE_CODE (base) == VAR_DECL
1387 && TREE_STATIC (base))
1389 struct cgraph_node *node = cgraph_get_node (callee);
1390 bitmap not_read;
1392 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1393 node yet. We should enforce that there are nodes for all decls in the
1394 IL and remove this check instead. */
1395 if (node
1396 && (not_read = ipa_reference_get_not_read_global (node))
1397 && bitmap_bit_p (not_read, DECL_UID (base)))
1398 goto process_args;
1401 /* Check if the base variable is call-used. */
1402 if (DECL_P (base))
1404 if (pt_solution_includes (gimple_call_use_set (call), base))
1405 return true;
1407 else if ((TREE_CODE (base) == MEM_REF
1408 || TREE_CODE (base) == TARGET_MEM_REF)
1409 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1411 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1412 if (!pi)
1413 return true;
1415 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1416 return true;
1418 else
1419 return true;
1421 /* Inspect call arguments for passed-by-value aliases. */
1422 process_args:
1423 for (i = 0; i < gimple_call_num_args (call); ++i)
1425 tree op = gimple_call_arg (call, i);
1426 int flags = gimple_call_arg_flags (call, i);
1428 if (flags & EAF_UNUSED)
1429 continue;
1431 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1432 op = TREE_OPERAND (op, 0);
1434 if (TREE_CODE (op) != SSA_NAME
1435 && !is_gimple_min_invariant (op))
1437 ao_ref r;
1438 ao_ref_init (&r, op);
1439 if (refs_may_alias_p_1 (&r, ref, true))
1440 return true;
1444 return false;
1447 static bool
1448 ref_maybe_used_by_call_p (gimple call, tree ref)
1450 ao_ref r;
1451 bool res;
1452 ao_ref_init (&r, ref);
1453 res = ref_maybe_used_by_call_p_1 (call, &r);
1454 if (res)
1455 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1456 else
1457 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1458 return res;
1462 /* If the statement STMT may use the memory reference REF return
1463 true, otherwise return false. */
1465 bool
1466 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1468 if (is_gimple_assign (stmt))
1470 tree rhs;
1472 /* All memory assign statements are single. */
1473 if (!gimple_assign_single_p (stmt))
1474 return false;
1476 rhs = gimple_assign_rhs1 (stmt);
1477 if (is_gimple_reg (rhs)
1478 || is_gimple_min_invariant (rhs)
1479 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1480 return false;
1482 return refs_may_alias_p (rhs, ref);
1484 else if (is_gimple_call (stmt))
1485 return ref_maybe_used_by_call_p (stmt, ref);
1486 else if (gimple_code (stmt) == GIMPLE_RETURN)
1488 tree retval = gimple_return_retval (stmt);
1489 tree base;
1490 if (retval
1491 && TREE_CODE (retval) != SSA_NAME
1492 && !is_gimple_min_invariant (retval)
1493 && refs_may_alias_p (retval, ref))
1494 return true;
1495 /* If ref escapes the function then the return acts as a use. */
1496 base = get_base_address (ref);
1497 if (!base)
1499 else if (DECL_P (base))
1500 return is_global_var (base);
1501 else if (TREE_CODE (base) == MEM_REF
1502 || TREE_CODE (base) == TARGET_MEM_REF)
1503 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1504 return false;
1507 return true;
1510 /* If the call in statement CALL may clobber the memory reference REF
1511 return true, otherwise return false. */
1513 static bool
1514 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1516 tree base;
1517 tree callee;
1519 /* If the call is pure or const it cannot clobber anything. */
1520 if (gimple_call_flags (call)
1521 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1522 return false;
1524 base = ao_ref_base (ref);
1525 if (!base)
1526 return true;
1528 if (TREE_CODE (base) == SSA_NAME
1529 || CONSTANT_CLASS_P (base))
1530 return false;
1532 /* A call that is not without side-effects might involve volatile
1533 accesses and thus conflicts with all other volatile accesses. */
1534 if (ref->volatile_p)
1535 return true;
1537 /* If the reference is based on a decl that is not aliased the call
1538 cannot possibly clobber it. */
1539 if (DECL_P (base)
1540 && !may_be_aliased (base)
1541 /* But local non-readonly statics can be modified through recursion
1542 or the call may implement a threading barrier which we must
1543 treat as may-def. */
1544 && (TREE_READONLY (base)
1545 || !is_global_var (base)))
1546 return false;
1548 callee = gimple_call_fndecl (call);
1550 /* Handle those builtin functions explicitly that do not act as
1551 escape points. See tree-ssa-structalias.c:find_func_aliases
1552 for the list of builtins we might need to handle here. */
1553 if (callee != NULL_TREE
1554 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1555 switch (DECL_FUNCTION_CODE (callee))
1557 /* All the following functions clobber memory pointed to by
1558 their first argument. */
1559 case BUILT_IN_STRCPY:
1560 case BUILT_IN_STRNCPY:
1561 case BUILT_IN_MEMCPY:
1562 case BUILT_IN_MEMMOVE:
1563 case BUILT_IN_MEMPCPY:
1564 case BUILT_IN_STPCPY:
1565 case BUILT_IN_STPNCPY:
1566 case BUILT_IN_STRCAT:
1567 case BUILT_IN_STRNCAT:
1568 case BUILT_IN_MEMSET:
1569 case BUILT_IN_TM_MEMSET:
1570 CASE_BUILT_IN_TM_STORE (1):
1571 CASE_BUILT_IN_TM_STORE (2):
1572 CASE_BUILT_IN_TM_STORE (4):
1573 CASE_BUILT_IN_TM_STORE (8):
1574 CASE_BUILT_IN_TM_STORE (FLOAT):
1575 CASE_BUILT_IN_TM_STORE (DOUBLE):
1576 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1577 CASE_BUILT_IN_TM_STORE (M64):
1578 CASE_BUILT_IN_TM_STORE (M128):
1579 CASE_BUILT_IN_TM_STORE (M256):
1580 case BUILT_IN_TM_MEMCPY:
1581 case BUILT_IN_TM_MEMMOVE:
1583 ao_ref dref;
1584 tree size = NULL_TREE;
1585 /* Don't pass in size for strncat, as the maximum size
1586 is strlen (dest) + n + 1 instead of n, resp.
1587 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1588 known. */
1589 if (gimple_call_num_args (call) == 3
1590 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1591 size = gimple_call_arg (call, 2);
1592 ao_ref_init_from_ptr_and_size (&dref,
1593 gimple_call_arg (call, 0),
1594 size);
1595 return refs_may_alias_p_1 (&dref, ref, false);
1597 case BUILT_IN_STRCPY_CHK:
1598 case BUILT_IN_STRNCPY_CHK:
1599 case BUILT_IN_MEMCPY_CHK:
1600 case BUILT_IN_MEMMOVE_CHK:
1601 case BUILT_IN_MEMPCPY_CHK:
1602 case BUILT_IN_STPCPY_CHK:
1603 case BUILT_IN_STPNCPY_CHK:
1604 case BUILT_IN_STRCAT_CHK:
1605 case BUILT_IN_STRNCAT_CHK:
1606 case BUILT_IN_MEMSET_CHK:
1608 ao_ref dref;
1609 tree size = NULL_TREE;
1610 /* Don't pass in size for __strncat_chk, as the maximum size
1611 is strlen (dest) + n + 1 instead of n, resp.
1612 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1613 known. */
1614 if (gimple_call_num_args (call) == 4
1615 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1616 size = gimple_call_arg (call, 2);
1617 ao_ref_init_from_ptr_and_size (&dref,
1618 gimple_call_arg (call, 0),
1619 size);
1620 return refs_may_alias_p_1 (&dref, ref, false);
1622 case BUILT_IN_BCOPY:
1624 ao_ref dref;
1625 tree size = gimple_call_arg (call, 2);
1626 ao_ref_init_from_ptr_and_size (&dref,
1627 gimple_call_arg (call, 1),
1628 size);
1629 return refs_may_alias_p_1 (&dref, ref, false);
1631 /* Allocating memory does not have any side-effects apart from
1632 being the definition point for the pointer. */
1633 case BUILT_IN_MALLOC:
1634 case BUILT_IN_CALLOC:
1635 case BUILT_IN_STRDUP:
1636 case BUILT_IN_STRNDUP:
1637 /* Unix98 specifies that errno is set on allocation failure. */
1638 if (flag_errno_math
1639 && targetm.ref_may_alias_errno (ref))
1640 return true;
1641 return false;
1642 case BUILT_IN_STACK_SAVE:
1643 case BUILT_IN_ALLOCA:
1644 case BUILT_IN_ALLOCA_WITH_ALIGN:
1645 case BUILT_IN_ASSUME_ALIGNED:
1646 return false;
1647 /* Freeing memory kills the pointed-to memory. More importantly
1648 the call has to serve as a barrier for moving loads and stores
1649 across it. */
1650 case BUILT_IN_FREE:
1651 case BUILT_IN_VA_END:
1653 tree ptr = gimple_call_arg (call, 0);
1654 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1656 case BUILT_IN_GAMMA_R:
1657 case BUILT_IN_GAMMAF_R:
1658 case BUILT_IN_GAMMAL_R:
1659 case BUILT_IN_LGAMMA_R:
1660 case BUILT_IN_LGAMMAF_R:
1661 case BUILT_IN_LGAMMAL_R:
1663 tree out = gimple_call_arg (call, 1);
1664 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1665 return true;
1666 if (flag_errno_math)
1667 break;
1668 return false;
1670 case BUILT_IN_FREXP:
1671 case BUILT_IN_FREXPF:
1672 case BUILT_IN_FREXPL:
1673 case BUILT_IN_MODF:
1674 case BUILT_IN_MODFF:
1675 case BUILT_IN_MODFL:
1677 tree out = gimple_call_arg (call, 1);
1678 return ptr_deref_may_alias_ref_p_1 (out, ref);
1680 case BUILT_IN_REMQUO:
1681 case BUILT_IN_REMQUOF:
1682 case BUILT_IN_REMQUOL:
1684 tree out = gimple_call_arg (call, 2);
1685 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1686 return true;
1687 if (flag_errno_math)
1688 break;
1689 return false;
1691 case BUILT_IN_SINCOS:
1692 case BUILT_IN_SINCOSF:
1693 case BUILT_IN_SINCOSL:
1695 tree sin = gimple_call_arg (call, 1);
1696 tree cos = gimple_call_arg (call, 2);
1697 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1698 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1700 /* __sync_* builtins and some OpenMP builtins act as threading
1701 barriers. */
1702 #undef DEF_SYNC_BUILTIN
1703 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1704 #include "sync-builtins.def"
1705 #undef DEF_SYNC_BUILTIN
1706 case BUILT_IN_GOMP_ATOMIC_START:
1707 case BUILT_IN_GOMP_ATOMIC_END:
1708 case BUILT_IN_GOMP_BARRIER:
1709 case BUILT_IN_GOMP_TASKWAIT:
1710 case BUILT_IN_GOMP_CRITICAL_START:
1711 case BUILT_IN_GOMP_CRITICAL_END:
1712 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1713 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1714 case BUILT_IN_GOMP_LOOP_END:
1715 case BUILT_IN_GOMP_ORDERED_START:
1716 case BUILT_IN_GOMP_ORDERED_END:
1717 case BUILT_IN_GOMP_PARALLEL_END:
1718 case BUILT_IN_GOMP_SECTIONS_END:
1719 case BUILT_IN_GOMP_SINGLE_COPY_START:
1720 case BUILT_IN_GOMP_SINGLE_COPY_END:
1721 return true;
1722 default:
1723 /* Fallthru to general call handling. */;
1726 /* Check if base is a global static variable that is not written
1727 by the function. */
1728 if (callee != NULL_TREE
1729 && TREE_CODE (base) == VAR_DECL
1730 && TREE_STATIC (base))
1732 struct cgraph_node *node = cgraph_get_node (callee);
1733 bitmap not_written;
1735 if (node
1736 && (not_written = ipa_reference_get_not_written_global (node))
1737 && bitmap_bit_p (not_written, DECL_UID (base)))
1738 return false;
1741 /* Check if the base variable is call-clobbered. */
1742 if (DECL_P (base))
1743 return pt_solution_includes (gimple_call_clobber_set (call), base);
1744 else if ((TREE_CODE (base) == MEM_REF
1745 || TREE_CODE (base) == TARGET_MEM_REF)
1746 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1748 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1749 if (!pi)
1750 return true;
1752 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1755 return true;
1758 /* If the call in statement CALL may clobber the memory reference REF
1759 return true, otherwise return false. */
1761 bool
1762 call_may_clobber_ref_p (gimple call, tree ref)
1764 bool res;
1765 ao_ref r;
1766 ao_ref_init (&r, ref);
1767 res = call_may_clobber_ref_p_1 (call, &r);
1768 if (res)
1769 ++alias_stats.call_may_clobber_ref_p_may_alias;
1770 else
1771 ++alias_stats.call_may_clobber_ref_p_no_alias;
1772 return res;
1776 /* If the statement STMT may clobber the memory reference REF return true,
1777 otherwise return false. */
1779 bool
1780 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1782 if (is_gimple_call (stmt))
1784 tree lhs = gimple_call_lhs (stmt);
1785 if (lhs
1786 && TREE_CODE (lhs) != SSA_NAME)
1788 ao_ref r;
1789 ao_ref_init (&r, lhs);
1790 if (refs_may_alias_p_1 (ref, &r, true))
1791 return true;
1794 return call_may_clobber_ref_p_1 (stmt, ref);
1796 else if (gimple_assign_single_p (stmt))
1798 tree lhs = gimple_assign_lhs (stmt);
1799 if (TREE_CODE (lhs) != SSA_NAME)
1801 ao_ref r;
1802 ao_ref_init (&r, lhs);
1803 return refs_may_alias_p_1 (ref, &r, true);
1806 else if (gimple_code (stmt) == GIMPLE_ASM)
1807 return true;
1809 return false;
1812 bool
1813 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1815 ao_ref r;
1816 ao_ref_init (&r, ref);
1817 return stmt_may_clobber_ref_p_1 (stmt, &r);
1820 /* If STMT kills the memory reference REF return true, otherwise
1821 return false. */
1823 static bool
1824 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1826 /* For a must-alias check we need to be able to constrain
1827 the access properly. */
1828 ao_ref_base (ref);
1829 if (ref->max_size == -1)
1830 return false;
1832 if (gimple_has_lhs (stmt)
1833 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1834 /* The assignment is not necessarily carried out if it can throw
1835 and we can catch it in the current function where we could inspect
1836 the previous value.
1837 ??? We only need to care about the RHS throwing. For aggregate
1838 assignments or similar calls and non-call exceptions the LHS
1839 might throw as well. */
1840 && !stmt_can_throw_internal (stmt))
1842 tree base, lhs = gimple_get_lhs (stmt);
1843 HOST_WIDE_INT size, offset, max_size;
1844 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1845 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1846 so base == ref->base does not always hold. */
1847 if (base == ref->base)
1849 /* For a must-alias check we need to be able to constrain
1850 the access properly. */
1851 if (size != -1 && size == max_size)
1853 if (offset <= ref->offset
1854 && offset + size >= ref->offset + ref->max_size)
1855 return true;
1860 if (is_gimple_call (stmt))
1862 tree callee = gimple_call_fndecl (stmt);
1863 if (callee != NULL_TREE
1864 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1865 switch (DECL_FUNCTION_CODE (callee))
1867 case BUILT_IN_MEMCPY:
1868 case BUILT_IN_MEMPCPY:
1869 case BUILT_IN_MEMMOVE:
1870 case BUILT_IN_MEMSET:
1871 case BUILT_IN_MEMCPY_CHK:
1872 case BUILT_IN_MEMPCPY_CHK:
1873 case BUILT_IN_MEMMOVE_CHK:
1874 case BUILT_IN_MEMSET_CHK:
1876 tree dest = gimple_call_arg (stmt, 0);
1877 tree len = gimple_call_arg (stmt, 2);
1878 tree base = NULL_TREE;
1879 HOST_WIDE_INT offset = 0;
1880 if (!host_integerp (len, 0))
1881 return false;
1882 if (TREE_CODE (dest) == ADDR_EXPR)
1883 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1884 &offset);
1885 else if (TREE_CODE (dest) == SSA_NAME)
1886 base = dest;
1887 if (base
1888 && base == ao_ref_base (ref))
1890 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1891 if (offset <= ref->offset / BITS_PER_UNIT
1892 && (offset + size
1893 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1894 / BITS_PER_UNIT)))
1895 return true;
1897 break;
1900 case BUILT_IN_VA_END:
1902 tree ptr = gimple_call_arg (stmt, 0);
1903 if (TREE_CODE (ptr) == ADDR_EXPR)
1905 tree base = ao_ref_base (ref);
1906 if (TREE_OPERAND (ptr, 0) == base)
1907 return true;
1909 break;
1912 default:;
1915 return false;
1918 bool
1919 stmt_kills_ref_p (gimple stmt, tree ref)
1921 ao_ref r;
1922 ao_ref_init (&r, ref);
1923 return stmt_kills_ref_p_1 (stmt, &r);
1927 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1928 TARGET or a statement clobbering the memory reference REF in which
1929 case false is returned. The walk starts with VUSE, one argument of PHI. */
1931 static bool
1932 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1933 tree vuse, bitmap *visited)
1935 basic_block bb = gimple_bb (phi);
1937 if (!*visited)
1938 *visited = BITMAP_ALLOC (NULL);
1940 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1942 /* Walk until we hit the target. */
1943 while (vuse != target)
1945 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1946 /* Recurse for PHI nodes. */
1947 if (gimple_code (def_stmt) == GIMPLE_PHI)
1949 /* An already visited PHI node ends the walk successfully. */
1950 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1951 return true;
1952 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1953 if (!vuse)
1954 return false;
1955 continue;
1957 /* A clobbering statement or the end of the IL ends it failing. */
1958 else if (gimple_nop_p (def_stmt)
1959 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1960 return false;
1961 /* If we reach a new basic-block see if we already skipped it
1962 in a previous walk that ended successfully. */
1963 if (gimple_bb (def_stmt) != bb)
1965 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
1966 return true;
1967 bb = gimple_bb (def_stmt);
1969 vuse = gimple_vuse (def_stmt);
1971 return true;
1974 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
1975 until we hit the phi argument definition that dominates the other one.
1976 Return that, or NULL_TREE if there is no such definition. */
1978 static tree
1979 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
1980 ao_ref *ref, bitmap *visited)
1982 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1983 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1984 tree common_vuse;
1986 if (arg0 == arg1)
1987 return arg0;
1988 else if (gimple_nop_p (def0)
1989 || (!gimple_nop_p (def1)
1990 && dominated_by_p (CDI_DOMINATORS,
1991 gimple_bb (def1), gimple_bb (def0))))
1993 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1994 return arg0;
1996 else if (gimple_nop_p (def1)
1997 || dominated_by_p (CDI_DOMINATORS,
1998 gimple_bb (def0), gimple_bb (def1)))
2000 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
2001 return arg1;
2003 /* Special case of a diamond:
2004 MEM_1 = ...
2005 goto (cond) ? L1 : L2
2006 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2007 goto L3
2008 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2009 L3: MEM_4 = PHI<MEM_2, MEM_3>
2010 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2011 dominate each other, but still we can easily skip this PHI node
2012 if we recognize that the vuse MEM operand is the same for both,
2013 and that we can skip both statements (they don't clobber us).
2014 This is still linear. Don't use maybe_skip_until, that might
2015 potentially be slow. */
2016 else if ((common_vuse = gimple_vuse (def0))
2017 && common_vuse == gimple_vuse (def1))
2019 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2020 && !stmt_may_clobber_ref_p_1 (def1, ref))
2021 return common_vuse;
2024 return NULL_TREE;
2028 /* Starting from a PHI node for the virtual operand of the memory reference
2029 REF find a continuation virtual operand that allows to continue walking
2030 statements dominating PHI skipping only statements that cannot possibly
2031 clobber REF. Returns NULL_TREE if no suitable virtual operand can
2032 be found. */
2034 tree
2035 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
2037 unsigned nargs = gimple_phi_num_args (phi);
2039 /* Through a single-argument PHI we can simply look through. */
2040 if (nargs == 1)
2041 return PHI_ARG_DEF (phi, 0);
2043 /* For two or more arguments try to pairwise skip non-aliasing code
2044 until we hit the phi argument definition that dominates the other one. */
2045 else if (nargs >= 2)
2047 tree arg0, arg1;
2048 unsigned i;
2050 /* Find a candidate for the virtual operand which definition
2051 dominates those of all others. */
2052 arg0 = PHI_ARG_DEF (phi, 0);
2053 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2054 for (i = 1; i < nargs; ++i)
2056 arg1 = PHI_ARG_DEF (phi, i);
2057 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2059 arg0 = arg1;
2060 break;
2062 if (dominated_by_p (CDI_DOMINATORS,
2063 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2064 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2065 arg0 = arg1;
2068 /* Then pairwise reduce against the found candidate. */
2069 for (i = 0; i < nargs; ++i)
2071 arg1 = PHI_ARG_DEF (phi, i);
2072 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref, visited);
2073 if (!arg0)
2074 return NULL_TREE;
2077 return arg0;
2080 return NULL_TREE;
2083 /* Based on the memory reference REF and its virtual use VUSE call
2084 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2085 itself. That is, for each virtual use for which its defining statement
2086 does not clobber REF.
2088 WALKER is called with REF, the current virtual use and DATA. If
2089 WALKER returns non-NULL the walk stops and its result is returned.
2090 At the end of a non-successful walk NULL is returned.
2092 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2093 use which definition is a statement that may clobber REF and DATA.
2094 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2095 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2096 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2097 to adjust REF and *DATA to make that valid.
2099 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2101 void *
2102 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2103 void *(*walker)(ao_ref *, tree, void *),
2104 void *(*translate)(ao_ref *, tree, void *), void *data)
2106 bitmap visited = NULL;
2107 void *res;
2109 timevar_push (TV_ALIAS_STMT_WALK);
2113 gimple def_stmt;
2115 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2116 res = (*walker) (ref, vuse, data);
2117 if (res)
2118 break;
2120 def_stmt = SSA_NAME_DEF_STMT (vuse);
2121 if (gimple_nop_p (def_stmt))
2122 break;
2123 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2124 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
2125 else
2127 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2129 if (!translate)
2130 break;
2131 res = (*translate) (ref, vuse, data);
2132 /* Failed lookup and translation. */
2133 if (res == (void *)-1)
2135 res = NULL;
2136 break;
2138 /* Lookup succeeded. */
2139 else if (res != NULL)
2140 break;
2141 /* Translation succeeded, continue walking. */
2143 vuse = gimple_vuse (def_stmt);
2146 while (vuse);
2148 if (visited)
2149 BITMAP_FREE (visited);
2151 timevar_pop (TV_ALIAS_STMT_WALK);
2153 return res;
2157 /* Based on the memory reference REF call WALKER for each vdef which
2158 defining statement may clobber REF, starting with VDEF. If REF
2159 is NULL_TREE, each defining statement is visited.
2161 WALKER is called with REF, the current vdef and DATA. If WALKER
2162 returns true the walk is stopped, otherwise it continues.
2164 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2165 PHI argument (but only one walk continues on merge points), the
2166 return value is true if any of the walks was successful.
2168 The function returns the number of statements walked. */
2170 static unsigned int
2171 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2172 bool (*walker)(ao_ref *, tree, void *), void *data,
2173 bitmap *visited, unsigned int cnt)
2177 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2179 if (*visited
2180 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2181 return cnt;
2183 if (gimple_nop_p (def_stmt))
2184 return cnt;
2185 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2187 unsigned i;
2188 if (!*visited)
2189 *visited = BITMAP_ALLOC (NULL);
2190 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2191 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2192 walker, data, visited, 0);
2193 return cnt;
2196 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2197 cnt++;
2198 if ((!ref
2199 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2200 && (*walker) (ref, vdef, data))
2201 return cnt;
2203 vdef = gimple_vuse (def_stmt);
2205 while (1);
2208 unsigned int
2209 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2210 bool (*walker)(ao_ref *, tree, void *), void *data,
2211 bitmap *visited)
2213 bitmap local_visited = NULL;
2214 unsigned int ret;
2216 timevar_push (TV_ALIAS_STMT_WALK);
2218 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2219 visited ? visited : &local_visited, 0);
2220 if (local_visited)
2221 BITMAP_FREE (local_visited);
2223 timevar_pop (TV_ALIAS_STMT_WALK);
2225 return ret;