Daily bump.
[official-gcc.git] / gcc / tree-ssa-alias.c
blobbe0aa432b4f28dea137756229525147f3ffed1a6
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 unsigned i;
382 const char *funcname
383 = lang_hooks.decl_printable_name (current_function_decl, 2);
384 tree var;
386 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
388 fprintf (file, "Aliased symbols\n\n");
390 FOR_EACH_LOCAL_DECL (cfun, i, var)
392 if (may_be_aliased (var))
393 dump_variable (file, var);
396 fprintf (file, "\nCall clobber information\n");
398 fprintf (file, "\nESCAPED");
399 dump_points_to_solution (file, &cfun->gimple_df->escaped);
401 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
403 for (i = 1; i < num_ssa_names; i++)
405 tree ptr = ssa_name (i);
406 struct ptr_info_def *pi;
408 if (ptr == NULL_TREE
409 || SSA_NAME_IN_FREE_LIST (ptr))
410 continue;
412 pi = SSA_NAME_PTR_INFO (ptr);
413 if (pi)
414 dump_points_to_info_for (file, ptr);
417 fprintf (file, "\n");
421 /* Dump alias information on stderr. */
423 DEBUG_FUNCTION void
424 debug_alias_info (void)
426 dump_alias_info (stderr);
430 /* Dump the points-to set *PT into FILE. */
432 void
433 dump_points_to_solution (FILE *file, struct pt_solution *pt)
435 if (pt->anything)
436 fprintf (file, ", points-to anything");
438 if (pt->nonlocal)
439 fprintf (file, ", points-to non-local");
441 if (pt->escaped)
442 fprintf (file, ", points-to escaped");
444 if (pt->ipa_escaped)
445 fprintf (file, ", points-to unit escaped");
447 if (pt->null)
448 fprintf (file, ", points-to NULL");
450 if (pt->vars)
452 fprintf (file, ", points-to vars: ");
453 dump_decl_set (file, pt->vars);
454 if (pt->vars_contains_global)
455 fprintf (file, " (includes global vars)");
459 /* Dump points-to information for SSA_NAME PTR into FILE. */
461 void
462 dump_points_to_info_for (FILE *file, tree ptr)
464 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
466 print_generic_expr (file, ptr, dump_flags);
468 if (pi)
469 dump_points_to_solution (file, &pi->pt);
470 else
471 fprintf (file, ", points-to anything");
473 fprintf (file, "\n");
477 /* Dump points-to information for VAR into stderr. */
479 DEBUG_FUNCTION void
480 debug_points_to_info_for (tree var)
482 dump_points_to_info_for (stderr, var);
486 /* Initializes the alias-oracle reference representation *R from REF. */
488 void
489 ao_ref_init (ao_ref *r, tree ref)
491 r->ref = ref;
492 r->base = NULL_TREE;
493 r->offset = 0;
494 r->size = -1;
495 r->max_size = -1;
496 r->ref_alias_set = -1;
497 r->base_alias_set = -1;
498 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
501 /* Returns the base object of the memory reference *REF. */
503 tree
504 ao_ref_base (ao_ref *ref)
506 if (ref->base)
507 return ref->base;
508 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
509 &ref->max_size);
510 return ref->base;
513 /* Returns the base object alias set of the memory reference *REF. */
515 static alias_set_type
516 ao_ref_base_alias_set (ao_ref *ref)
518 tree base_ref;
519 if (ref->base_alias_set != -1)
520 return ref->base_alias_set;
521 if (!ref->ref)
522 return 0;
523 base_ref = ref->ref;
524 while (handled_component_p (base_ref))
525 base_ref = TREE_OPERAND (base_ref, 0);
526 ref->base_alias_set = get_alias_set (base_ref);
527 return ref->base_alias_set;
530 /* Returns the reference alias set of the memory reference *REF. */
532 alias_set_type
533 ao_ref_alias_set (ao_ref *ref)
535 if (ref->ref_alias_set != -1)
536 return ref->ref_alias_set;
537 ref->ref_alias_set = get_alias_set (ref->ref);
538 return ref->ref_alias_set;
541 /* Init an alias-oracle reference representation from a gimple pointer
542 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
543 size is assumed to be unknown. The access is assumed to be only
544 to or after of the pointer target, not before it. */
546 void
547 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
549 HOST_WIDE_INT t1, t2;
550 ref->ref = NULL_TREE;
551 if (TREE_CODE (ptr) == ADDR_EXPR)
552 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
553 &ref->offset, &t1, &t2);
554 else
556 ref->base = build2 (MEM_REF, char_type_node,
557 ptr, null_pointer_node);
558 ref->offset = 0;
560 if (size
561 && host_integerp (size, 0)
562 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
563 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
564 else
565 ref->max_size = ref->size = -1;
566 ref->ref_alias_set = 0;
567 ref->base_alias_set = 0;
568 ref->volatile_p = false;
571 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
572 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
573 decide. */
575 static inline int
576 same_type_for_tbaa (tree type1, tree type2)
578 type1 = TYPE_MAIN_VARIANT (type1);
579 type2 = TYPE_MAIN_VARIANT (type2);
581 /* If we would have to do structural comparison bail out. */
582 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
583 || TYPE_STRUCTURAL_EQUALITY_P (type2))
584 return -1;
586 /* Compare the canonical types. */
587 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
588 return 1;
590 /* ??? Array types are not properly unified in all cases as we have
591 spurious changes in the index types for example. Removing this
592 causes all sorts of problems with the Fortran frontend. */
593 if (TREE_CODE (type1) == ARRAY_TYPE
594 && TREE_CODE (type2) == ARRAY_TYPE)
595 return -1;
597 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
598 object of one of its constrained subtypes, e.g. when a function with an
599 unconstrained parameter passed by reference is called on an object and
600 inlined. But, even in the case of a fixed size, type and subtypes are
601 not equivalent enough as to share the same TYPE_CANONICAL, since this
602 would mean that conversions between them are useless, whereas they are
603 not (e.g. type and subtypes can have different modes). So, in the end,
604 they are only guaranteed to have the same alias set. */
605 if (get_alias_set (type1) == get_alias_set (type2))
606 return -1;
608 /* The types are known to be not equal. */
609 return 0;
612 /* Determine if the two component references REF1 and REF2 which are
613 based on access types TYPE1 and TYPE2 and of which at least one is based
614 on an indirect reference may alias. REF2 is the only one that can
615 be a decl in which case REF2_IS_DECL is true.
616 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
617 are the respective alias sets. */
619 static bool
620 aliasing_component_refs_p (tree ref1,
621 alias_set_type ref1_alias_set,
622 alias_set_type base1_alias_set,
623 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
624 tree ref2,
625 alias_set_type ref2_alias_set,
626 alias_set_type base2_alias_set,
627 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
628 bool ref2_is_decl)
630 /* If one reference is a component references through pointers try to find a
631 common base and apply offset based disambiguation. This handles
632 for example
633 struct A { int i; int j; } *q;
634 struct B { struct A a; int k; } *p;
635 disambiguating q->i and p->a.j. */
636 tree base1, base2;
637 tree type1, type2;
638 tree *refp;
639 int same_p;
641 /* Choose bases and base types to search for. */
642 base1 = ref1;
643 while (handled_component_p (base1))
644 base1 = TREE_OPERAND (base1, 0);
645 type1 = TREE_TYPE (base1);
646 base2 = ref2;
647 while (handled_component_p (base2))
648 base2 = TREE_OPERAND (base2, 0);
649 type2 = TREE_TYPE (base2);
651 /* Now search for the type1 in the access path of ref2. This
652 would be a common base for doing offset based disambiguation on. */
653 refp = &ref2;
654 while (handled_component_p (*refp)
655 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
656 refp = &TREE_OPERAND (*refp, 0);
657 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
658 /* If we couldn't compare types we have to bail out. */
659 if (same_p == -1)
660 return true;
661 else if (same_p == 1)
663 HOST_WIDE_INT offadj, sztmp, msztmp;
664 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
665 offset2 -= offadj;
666 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
667 offset1 -= offadj;
668 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
670 /* If we didn't find a common base, try the other way around. */
671 refp = &ref1;
672 while (handled_component_p (*refp)
673 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
674 refp = &TREE_OPERAND (*refp, 0);
675 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
676 /* If we couldn't compare types we have to bail out. */
677 if (same_p == -1)
678 return true;
679 else if (same_p == 1)
681 HOST_WIDE_INT offadj, sztmp, msztmp;
682 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
683 offset1 -= offadj;
684 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
685 offset2 -= offadj;
686 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
689 /* If we have two type access paths B1.path1 and B2.path2 they may
690 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
691 But we can still have a path that goes B1.path1...B2.path2 with
692 a part that we do not see. So we can only disambiguate now
693 if there is no B2 in the tail of path1 and no B1 on the
694 tail of path2. */
695 if (base1_alias_set == ref2_alias_set
696 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
697 return true;
698 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
699 if (!ref2_is_decl)
700 return (base2_alias_set == ref1_alias_set
701 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
702 return false;
705 /* Return true if two memory references based on the variables BASE1
706 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
707 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
709 static bool
710 decl_refs_may_alias_p (tree base1,
711 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
712 tree base2,
713 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
715 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
717 /* If both references are based on different variables, they cannot alias. */
718 if (base1 != base2)
719 return false;
721 /* If both references are based on the same variable, they cannot alias if
722 the accesses do not overlap. */
723 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
726 /* Return true if an indirect reference based on *PTR1 constrained
727 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
728 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
729 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
730 in which case they are computed on-demand. REF1 and REF2
731 if non-NULL are the complete memory reference trees. */
733 static bool
734 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
735 HOST_WIDE_INT offset1,
736 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
737 alias_set_type ref1_alias_set,
738 alias_set_type base1_alias_set,
739 tree ref2 ATTRIBUTE_UNUSED, tree base2,
740 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
741 alias_set_type ref2_alias_set,
742 alias_set_type base2_alias_set, bool tbaa_p)
744 tree ptr1;
745 tree ptrtype1, dbase2;
746 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
747 HOST_WIDE_INT doffset1, doffset2;
748 double_int moff;
750 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
751 || TREE_CODE (base1) == TARGET_MEM_REF)
752 && DECL_P (base2));
754 ptr1 = TREE_OPERAND (base1, 0);
756 /* The offset embedded in MEM_REFs can be negative. Bias them
757 so that the resulting offset adjustment is positive. */
758 moff = mem_ref_offset (base1);
759 moff = double_int_lshift (moff,
760 BITS_PER_UNIT == 8
761 ? 3 : exact_log2 (BITS_PER_UNIT),
762 HOST_BITS_PER_DOUBLE_INT, true);
763 if (double_int_negative_p (moff))
764 offset2p += double_int_neg (moff).low;
765 else
766 offset1p += moff.low;
768 /* If only one reference is based on a variable, they cannot alias if
769 the pointer access is beyond the extent of the variable access.
770 (the pointer base cannot validly point to an offset less than zero
771 of the variable).
772 ??? IVOPTs creates bases that do not honor this restriction,
773 so do not apply this optimization for TARGET_MEM_REFs. */
774 if (TREE_CODE (base1) != TARGET_MEM_REF
775 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
776 return false;
777 /* They also cannot alias if the pointer may not point to the decl. */
778 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
779 return false;
781 /* Disambiguations that rely on strict aliasing rules follow. */
782 if (!flag_strict_aliasing || !tbaa_p)
783 return true;
785 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
787 /* If the alias set for a pointer access is zero all bets are off. */
788 if (base1_alias_set == -1)
789 base1_alias_set = get_deref_alias_set (ptrtype1);
790 if (base1_alias_set == 0)
791 return true;
792 if (base2_alias_set == -1)
793 base2_alias_set = get_alias_set (base2);
795 /* When we are trying to disambiguate an access with a pointer dereference
796 as base versus one with a decl as base we can use both the size
797 of the decl and its dynamic type for extra disambiguation.
798 ??? We do not know anything about the dynamic type of the decl
799 other than that its alias-set contains base2_alias_set as a subset
800 which does not help us here. */
801 /* As we know nothing useful about the dynamic type of the decl just
802 use the usual conflict check rather than a subset test.
803 ??? We could introduce -fvery-strict-aliasing when the language
804 does not allow decls to have a dynamic type that differs from their
805 static type. Then we can check
806 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
807 if (base1_alias_set != base2_alias_set
808 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
809 return false;
810 /* If the size of the access relevant for TBAA through the pointer
811 is bigger than the size of the decl we can't possibly access the
812 decl via that pointer. */
813 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
814 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
815 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
816 /* ??? This in turn may run afoul when a decl of type T which is
817 a member of union type U is accessed through a pointer to
818 type U and sizeof T is smaller than sizeof U. */
819 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
820 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
821 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
822 return false;
824 if (!ref2)
825 return true;
827 /* If the decl is accessed via a MEM_REF, reconstruct the base
828 we can use for TBAA and an appropriately adjusted offset. */
829 dbase2 = ref2;
830 while (handled_component_p (dbase2))
831 dbase2 = TREE_OPERAND (dbase2, 0);
832 doffset1 = offset1;
833 doffset2 = offset2;
834 if (TREE_CODE (dbase2) == MEM_REF
835 || TREE_CODE (dbase2) == TARGET_MEM_REF)
837 double_int moff = mem_ref_offset (dbase2);
838 moff = double_int_lshift (moff,
839 BITS_PER_UNIT == 8
840 ? 3 : exact_log2 (BITS_PER_UNIT),
841 HOST_BITS_PER_DOUBLE_INT, true);
842 if (double_int_negative_p (moff))
843 doffset1 -= double_int_neg (moff).low;
844 else
845 doffset2 -= moff.low;
848 /* If either reference is view-converted, give up now. */
849 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
850 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
851 return true;
853 /* If both references are through the same type, they do not alias
854 if the accesses do not overlap. This does extra disambiguation
855 for mixed/pointer accesses but requires strict aliasing.
856 For MEM_REFs we require that the component-ref offset we computed
857 is relative to the start of the type which we ensure by
858 comparing rvalue and access type and disregarding the constant
859 pointer offset. */
860 if ((TREE_CODE (base1) != TARGET_MEM_REF
861 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
862 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
863 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
865 /* Do access-path based disambiguation. */
866 if (ref1 && ref2
867 && (handled_component_p (ref1) || handled_component_p (ref2)))
868 return aliasing_component_refs_p (ref1,
869 ref1_alias_set, base1_alias_set,
870 offset1, max_size1,
871 ref2,
872 ref2_alias_set, base2_alias_set,
873 offset2, max_size2, true);
875 return true;
878 /* Return true if two indirect references based on *PTR1
879 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
880 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
881 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
882 in which case they are computed on-demand. REF1 and REF2
883 if non-NULL are the complete memory reference trees. */
885 static bool
886 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
887 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
888 alias_set_type ref1_alias_set,
889 alias_set_type base1_alias_set,
890 tree ref2 ATTRIBUTE_UNUSED, tree base2,
891 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
892 alias_set_type ref2_alias_set,
893 alias_set_type base2_alias_set, bool tbaa_p)
895 tree ptr1;
896 tree ptr2;
897 tree ptrtype1, ptrtype2;
899 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
900 || TREE_CODE (base1) == TARGET_MEM_REF)
901 && (TREE_CODE (base2) == MEM_REF
902 || TREE_CODE (base2) == TARGET_MEM_REF));
904 ptr1 = TREE_OPERAND (base1, 0);
905 ptr2 = TREE_OPERAND (base2, 0);
907 /* If both bases are based on pointers they cannot alias if they may not
908 point to the same memory object or if they point to the same object
909 and the accesses do not overlap. */
910 if ((!cfun || gimple_in_ssa_p (cfun))
911 && operand_equal_p (ptr1, ptr2, 0)
912 && (((TREE_CODE (base1) != TARGET_MEM_REF
913 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
914 && (TREE_CODE (base2) != TARGET_MEM_REF
915 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
916 || (TREE_CODE (base1) == TARGET_MEM_REF
917 && TREE_CODE (base2) == TARGET_MEM_REF
918 && (TMR_STEP (base1) == TMR_STEP (base2)
919 || (TMR_STEP (base1) && TMR_STEP (base2)
920 && operand_equal_p (TMR_STEP (base1),
921 TMR_STEP (base2), 0)))
922 && (TMR_INDEX (base1) == TMR_INDEX (base2)
923 || (TMR_INDEX (base1) && TMR_INDEX (base2)
924 && operand_equal_p (TMR_INDEX (base1),
925 TMR_INDEX (base2), 0)))
926 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
927 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
928 && operand_equal_p (TMR_INDEX2 (base1),
929 TMR_INDEX2 (base2), 0))))))
931 double_int moff;
932 /* The offset embedded in MEM_REFs can be negative. Bias them
933 so that the resulting offset adjustment is positive. */
934 moff = mem_ref_offset (base1);
935 moff = double_int_lshift (moff,
936 BITS_PER_UNIT == 8
937 ? 3 : exact_log2 (BITS_PER_UNIT),
938 HOST_BITS_PER_DOUBLE_INT, true);
939 if (double_int_negative_p (moff))
940 offset2 += double_int_neg (moff).low;
941 else
942 offset1 += moff.low;
943 moff = mem_ref_offset (base2);
944 moff = double_int_lshift (moff,
945 BITS_PER_UNIT == 8
946 ? 3 : exact_log2 (BITS_PER_UNIT),
947 HOST_BITS_PER_DOUBLE_INT, true);
948 if (double_int_negative_p (moff))
949 offset1 += double_int_neg (moff).low;
950 else
951 offset2 += moff.low;
952 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
954 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
955 return false;
957 /* Disambiguations that rely on strict aliasing rules follow. */
958 if (!flag_strict_aliasing || !tbaa_p)
959 return true;
961 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
962 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
964 /* If the alias set for a pointer access is zero all bets are off. */
965 if (base1_alias_set == -1)
966 base1_alias_set = get_deref_alias_set (ptrtype1);
967 if (base1_alias_set == 0)
968 return true;
969 if (base2_alias_set == -1)
970 base2_alias_set = get_deref_alias_set (ptrtype2);
971 if (base2_alias_set == 0)
972 return true;
974 /* If both references are through the same type, they do not alias
975 if the accesses do not overlap. This does extra disambiguation
976 for mixed/pointer accesses but requires strict aliasing. */
977 if ((TREE_CODE (base1) != TARGET_MEM_REF
978 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
979 && (TREE_CODE (base2) != TARGET_MEM_REF
980 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
981 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
982 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
983 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
984 TREE_TYPE (ptrtype2)) == 1)
985 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
987 /* Do type-based disambiguation. */
988 if (base1_alias_set != base2_alias_set
989 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
990 return false;
992 /* Do access-path based disambiguation. */
993 if (ref1 && ref2
994 && (handled_component_p (ref1) || handled_component_p (ref2))
995 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
996 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
997 return aliasing_component_refs_p (ref1,
998 ref1_alias_set, base1_alias_set,
999 offset1, max_size1,
1000 ref2,
1001 ref2_alias_set, base2_alias_set,
1002 offset2, max_size2, false);
1004 return true;
1007 /* Return true, if the two memory references REF1 and REF2 may alias. */
1009 bool
1010 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1012 tree base1, base2;
1013 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1014 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1015 bool var1_p, var2_p, ind1_p, ind2_p;
1017 gcc_checking_assert ((!ref1->ref
1018 || TREE_CODE (ref1->ref) == SSA_NAME
1019 || DECL_P (ref1->ref)
1020 || TREE_CODE (ref1->ref) == STRING_CST
1021 || handled_component_p (ref1->ref)
1022 || TREE_CODE (ref1->ref) == MEM_REF
1023 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1024 && (!ref2->ref
1025 || TREE_CODE (ref2->ref) == SSA_NAME
1026 || DECL_P (ref2->ref)
1027 || TREE_CODE (ref2->ref) == STRING_CST
1028 || handled_component_p (ref2->ref)
1029 || TREE_CODE (ref2->ref) == MEM_REF
1030 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1032 /* Decompose the references into their base objects and the access. */
1033 base1 = ao_ref_base (ref1);
1034 offset1 = ref1->offset;
1035 max_size1 = ref1->max_size;
1036 base2 = ao_ref_base (ref2);
1037 offset2 = ref2->offset;
1038 max_size2 = ref2->max_size;
1040 /* We can end up with registers or constants as bases for example from
1041 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1042 which is seen as a struct copy. */
1043 if (TREE_CODE (base1) == SSA_NAME
1044 || TREE_CODE (base1) == CONST_DECL
1045 || TREE_CODE (base1) == CONSTRUCTOR
1046 || TREE_CODE (base1) == ADDR_EXPR
1047 || CONSTANT_CLASS_P (base1)
1048 || TREE_CODE (base2) == SSA_NAME
1049 || TREE_CODE (base2) == CONST_DECL
1050 || TREE_CODE (base2) == CONSTRUCTOR
1051 || TREE_CODE (base2) == ADDR_EXPR
1052 || CONSTANT_CLASS_P (base2))
1053 return false;
1055 /* We can end up referring to code via function and label decls.
1056 As we likely do not properly track code aliases conservatively
1057 bail out. */
1058 if (TREE_CODE (base1) == FUNCTION_DECL
1059 || TREE_CODE (base1) == LABEL_DECL
1060 || TREE_CODE (base2) == FUNCTION_DECL
1061 || TREE_CODE (base2) == LABEL_DECL)
1062 return true;
1064 /* Two volatile accesses always conflict. */
1065 if (ref1->volatile_p
1066 && ref2->volatile_p)
1067 return true;
1069 /* Defer to simple offset based disambiguation if we have
1070 references based on two decls. Do this before defering to
1071 TBAA to handle must-alias cases in conformance with the
1072 GCC extension of allowing type-punning through unions. */
1073 var1_p = DECL_P (base1);
1074 var2_p = DECL_P (base2);
1075 if (var1_p && var2_p)
1076 return decl_refs_may_alias_p (base1, offset1, max_size1,
1077 base2, offset2, max_size2);
1079 ind1_p = (TREE_CODE (base1) == MEM_REF
1080 || TREE_CODE (base1) == TARGET_MEM_REF);
1081 ind2_p = (TREE_CODE (base2) == MEM_REF
1082 || TREE_CODE (base2) == TARGET_MEM_REF);
1084 /* Canonicalize the pointer-vs-decl case. */
1085 if (ind1_p && var2_p)
1087 HOST_WIDE_INT tmp1;
1088 tree tmp2;
1089 ao_ref *tmp3;
1090 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1091 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1092 tmp2 = base1; base1 = base2; base2 = tmp2;
1093 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1094 var1_p = true;
1095 ind1_p = false;
1096 var2_p = false;
1097 ind2_p = true;
1100 /* First defer to TBAA if possible. */
1101 if (tbaa_p
1102 && flag_strict_aliasing
1103 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1104 ao_ref_alias_set (ref2)))
1105 return false;
1107 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1108 if (var1_p && ind2_p)
1109 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1110 offset2, max_size2,
1111 ao_ref_alias_set (ref2), -1,
1112 ref1->ref, base1,
1113 offset1, max_size1,
1114 ao_ref_alias_set (ref1),
1115 ao_ref_base_alias_set (ref1),
1116 tbaa_p);
1117 else if (ind1_p && ind2_p)
1118 return indirect_refs_may_alias_p (ref1->ref, base1,
1119 offset1, max_size1,
1120 ao_ref_alias_set (ref1), -1,
1121 ref2->ref, base2,
1122 offset2, max_size2,
1123 ao_ref_alias_set (ref2), -1,
1124 tbaa_p);
1126 /* We really do not want to end up here, but returning true is safe. */
1127 #ifdef ENABLE_CHECKING
1128 gcc_unreachable ();
1129 #else
1130 return true;
1131 #endif
1134 bool
1135 refs_may_alias_p (tree ref1, tree ref2)
1137 ao_ref r1, r2;
1138 bool res;
1139 ao_ref_init (&r1, ref1);
1140 ao_ref_init (&r2, ref2);
1141 res = refs_may_alias_p_1 (&r1, &r2, true);
1142 if (res)
1143 ++alias_stats.refs_may_alias_p_may_alias;
1144 else
1145 ++alias_stats.refs_may_alias_p_no_alias;
1146 return res;
1149 /* Returns true if there is a anti-dependence for the STORE that
1150 executes after the LOAD. */
1152 bool
1153 refs_anti_dependent_p (tree load, tree store)
1155 ao_ref r1, r2;
1156 ao_ref_init (&r1, load);
1157 ao_ref_init (&r2, store);
1158 return refs_may_alias_p_1 (&r1, &r2, false);
1161 /* Returns true if there is a output dependence for the stores
1162 STORE1 and STORE2. */
1164 bool
1165 refs_output_dependent_p (tree store1, tree store2)
1167 ao_ref r1, r2;
1168 ao_ref_init (&r1, store1);
1169 ao_ref_init (&r2, store2);
1170 return refs_may_alias_p_1 (&r1, &r2, false);
1173 /* If the call CALL may use the memory reference REF return true,
1174 otherwise return false. */
1176 static bool
1177 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1179 tree base, callee;
1180 unsigned i;
1181 int flags = gimple_call_flags (call);
1183 /* Const functions without a static chain do not implicitly use memory. */
1184 if (!gimple_call_chain (call)
1185 && (flags & (ECF_CONST|ECF_NOVOPS)))
1186 goto process_args;
1188 base = ao_ref_base (ref);
1189 if (!base)
1190 return true;
1192 /* A call that is not without side-effects might involve volatile
1193 accesses and thus conflicts with all other volatile accesses. */
1194 if (ref->volatile_p)
1195 return true;
1197 /* If the reference is based on a decl that is not aliased the call
1198 cannot possibly use it. */
1199 if (DECL_P (base)
1200 && !may_be_aliased (base)
1201 /* But local statics can be used through recursion. */
1202 && !is_global_var (base))
1203 goto process_args;
1205 callee = gimple_call_fndecl (call);
1207 /* Handle those builtin functions explicitly that do not act as
1208 escape points. See tree-ssa-structalias.c:find_func_aliases
1209 for the list of builtins we might need to handle here. */
1210 if (callee != NULL_TREE
1211 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1212 switch (DECL_FUNCTION_CODE (callee))
1214 /* All the following functions read memory pointed to by
1215 their second argument. strcat/strncat additionally
1216 reads memory pointed to by the first argument. */
1217 case BUILT_IN_STRCAT:
1218 case BUILT_IN_STRNCAT:
1220 ao_ref dref;
1221 ao_ref_init_from_ptr_and_size (&dref,
1222 gimple_call_arg (call, 0),
1223 NULL_TREE);
1224 if (refs_may_alias_p_1 (&dref, ref, false))
1225 return true;
1227 /* FALLTHRU */
1228 case BUILT_IN_STRCPY:
1229 case BUILT_IN_STRNCPY:
1230 case BUILT_IN_MEMCPY:
1231 case BUILT_IN_MEMMOVE:
1232 case BUILT_IN_MEMPCPY:
1233 case BUILT_IN_STPCPY:
1234 case BUILT_IN_STPNCPY:
1235 case BUILT_IN_TM_MEMCPY:
1236 case BUILT_IN_TM_MEMMOVE:
1238 ao_ref dref;
1239 tree size = NULL_TREE;
1240 if (gimple_call_num_args (call) == 3)
1241 size = gimple_call_arg (call, 2);
1242 ao_ref_init_from_ptr_and_size (&dref,
1243 gimple_call_arg (call, 1),
1244 size);
1245 return refs_may_alias_p_1 (&dref, ref, false);
1247 case BUILT_IN_STRCAT_CHK:
1248 case BUILT_IN_STRNCAT_CHK:
1250 ao_ref dref;
1251 ao_ref_init_from_ptr_and_size (&dref,
1252 gimple_call_arg (call, 0),
1253 NULL_TREE);
1254 if (refs_may_alias_p_1 (&dref, ref, false))
1255 return true;
1257 /* FALLTHRU */
1258 case BUILT_IN_STRCPY_CHK:
1259 case BUILT_IN_STRNCPY_CHK:
1260 case BUILT_IN_MEMCPY_CHK:
1261 case BUILT_IN_MEMMOVE_CHK:
1262 case BUILT_IN_MEMPCPY_CHK:
1263 case BUILT_IN_STPCPY_CHK:
1264 case BUILT_IN_STPNCPY_CHK:
1266 ao_ref dref;
1267 tree size = NULL_TREE;
1268 if (gimple_call_num_args (call) == 4)
1269 size = gimple_call_arg (call, 2);
1270 ao_ref_init_from_ptr_and_size (&dref,
1271 gimple_call_arg (call, 1),
1272 size);
1273 return refs_may_alias_p_1 (&dref, ref, false);
1275 case BUILT_IN_BCOPY:
1277 ao_ref dref;
1278 tree size = gimple_call_arg (call, 2);
1279 ao_ref_init_from_ptr_and_size (&dref,
1280 gimple_call_arg (call, 0),
1281 size);
1282 return refs_may_alias_p_1 (&dref, ref, false);
1285 /* The following functions read memory pointed to by their
1286 first argument. */
1287 CASE_BUILT_IN_TM_LOAD (1):
1288 CASE_BUILT_IN_TM_LOAD (2):
1289 CASE_BUILT_IN_TM_LOAD (4):
1290 CASE_BUILT_IN_TM_LOAD (8):
1291 CASE_BUILT_IN_TM_LOAD (FLOAT):
1292 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1293 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1294 CASE_BUILT_IN_TM_LOAD (M64):
1295 CASE_BUILT_IN_TM_LOAD (M128):
1296 CASE_BUILT_IN_TM_LOAD (M256):
1297 case BUILT_IN_TM_LOG:
1298 case BUILT_IN_TM_LOG_1:
1299 case BUILT_IN_TM_LOG_2:
1300 case BUILT_IN_TM_LOG_4:
1301 case BUILT_IN_TM_LOG_8:
1302 case BUILT_IN_TM_LOG_FLOAT:
1303 case BUILT_IN_TM_LOG_DOUBLE:
1304 case BUILT_IN_TM_LOG_LDOUBLE:
1305 case BUILT_IN_TM_LOG_M64:
1306 case BUILT_IN_TM_LOG_M128:
1307 case BUILT_IN_TM_LOG_M256:
1308 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1310 /* These read memory pointed to by the first argument. */
1311 case BUILT_IN_STRDUP:
1312 case BUILT_IN_STRNDUP:
1314 ao_ref dref;
1315 tree size = NULL_TREE;
1316 if (gimple_call_num_args (call) == 2)
1317 size = gimple_call_arg (call, 1);
1318 ao_ref_init_from_ptr_and_size (&dref,
1319 gimple_call_arg (call, 0),
1320 size);
1321 return refs_may_alias_p_1 (&dref, ref, false);
1323 /* The following builtins do not read from memory. */
1324 case BUILT_IN_FREE:
1325 case BUILT_IN_MALLOC:
1326 case BUILT_IN_CALLOC:
1327 case BUILT_IN_ALLOCA:
1328 case BUILT_IN_ALLOCA_WITH_ALIGN:
1329 case BUILT_IN_STACK_SAVE:
1330 case BUILT_IN_STACK_RESTORE:
1331 case BUILT_IN_MEMSET:
1332 case BUILT_IN_TM_MEMSET:
1333 case BUILT_IN_MEMSET_CHK:
1334 case BUILT_IN_FREXP:
1335 case BUILT_IN_FREXPF:
1336 case BUILT_IN_FREXPL:
1337 case BUILT_IN_GAMMA_R:
1338 case BUILT_IN_GAMMAF_R:
1339 case BUILT_IN_GAMMAL_R:
1340 case BUILT_IN_LGAMMA_R:
1341 case BUILT_IN_LGAMMAF_R:
1342 case BUILT_IN_LGAMMAL_R:
1343 case BUILT_IN_MODF:
1344 case BUILT_IN_MODFF:
1345 case BUILT_IN_MODFL:
1346 case BUILT_IN_REMQUO:
1347 case BUILT_IN_REMQUOF:
1348 case BUILT_IN_REMQUOL:
1349 case BUILT_IN_SINCOS:
1350 case BUILT_IN_SINCOSF:
1351 case BUILT_IN_SINCOSL:
1352 case BUILT_IN_ASSUME_ALIGNED:
1353 case BUILT_IN_VA_END:
1354 return false;
1355 /* __sync_* builtins and some OpenMP builtins act as threading
1356 barriers. */
1357 #undef DEF_SYNC_BUILTIN
1358 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1359 #include "sync-builtins.def"
1360 #undef DEF_SYNC_BUILTIN
1361 case BUILT_IN_GOMP_ATOMIC_START:
1362 case BUILT_IN_GOMP_ATOMIC_END:
1363 case BUILT_IN_GOMP_BARRIER:
1364 case BUILT_IN_GOMP_TASKWAIT:
1365 case BUILT_IN_GOMP_CRITICAL_START:
1366 case BUILT_IN_GOMP_CRITICAL_END:
1367 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1368 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1369 case BUILT_IN_GOMP_LOOP_END:
1370 case BUILT_IN_GOMP_ORDERED_START:
1371 case BUILT_IN_GOMP_ORDERED_END:
1372 case BUILT_IN_GOMP_PARALLEL_END:
1373 case BUILT_IN_GOMP_SECTIONS_END:
1374 case BUILT_IN_GOMP_SINGLE_COPY_START:
1375 case BUILT_IN_GOMP_SINGLE_COPY_END:
1376 return true;
1378 default:
1379 /* Fallthru to general call handling. */;
1382 /* Check if base is a global static variable that is not read
1383 by the function. */
1384 if (callee != NULL_TREE
1385 && TREE_CODE (base) == VAR_DECL
1386 && TREE_STATIC (base))
1388 struct cgraph_node *node = cgraph_get_node (callee);
1389 bitmap not_read;
1391 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1392 node yet. We should enforce that there are nodes for all decls in the
1393 IL and remove this check instead. */
1394 if (node
1395 && (not_read = ipa_reference_get_not_read_global (node))
1396 && bitmap_bit_p (not_read, DECL_UID (base)))
1397 goto process_args;
1400 /* Check if the base variable is call-used. */
1401 if (DECL_P (base))
1403 if (pt_solution_includes (gimple_call_use_set (call), base))
1404 return true;
1406 else if ((TREE_CODE (base) == MEM_REF
1407 || TREE_CODE (base) == TARGET_MEM_REF)
1408 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1410 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1411 if (!pi)
1412 return true;
1414 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1415 return true;
1417 else
1418 return true;
1420 /* Inspect call arguments for passed-by-value aliases. */
1421 process_args:
1422 for (i = 0; i < gimple_call_num_args (call); ++i)
1424 tree op = gimple_call_arg (call, i);
1425 int flags = gimple_call_arg_flags (call, i);
1427 if (flags & EAF_UNUSED)
1428 continue;
1430 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1431 op = TREE_OPERAND (op, 0);
1433 if (TREE_CODE (op) != SSA_NAME
1434 && !is_gimple_min_invariant (op))
1436 ao_ref r;
1437 ao_ref_init (&r, op);
1438 if (refs_may_alias_p_1 (&r, ref, true))
1439 return true;
1443 return false;
1446 static bool
1447 ref_maybe_used_by_call_p (gimple call, tree ref)
1449 ao_ref r;
1450 bool res;
1451 ao_ref_init (&r, ref);
1452 res = ref_maybe_used_by_call_p_1 (call, &r);
1453 if (res)
1454 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1455 else
1456 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1457 return res;
1461 /* If the statement STMT may use the memory reference REF return
1462 true, otherwise return false. */
1464 bool
1465 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1467 if (is_gimple_assign (stmt))
1469 tree rhs;
1471 /* All memory assign statements are single. */
1472 if (!gimple_assign_single_p (stmt))
1473 return false;
1475 rhs = gimple_assign_rhs1 (stmt);
1476 if (is_gimple_reg (rhs)
1477 || is_gimple_min_invariant (rhs)
1478 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1479 return false;
1481 return refs_may_alias_p (rhs, ref);
1483 else if (is_gimple_call (stmt))
1484 return ref_maybe_used_by_call_p (stmt, ref);
1485 else if (gimple_code (stmt) == GIMPLE_RETURN)
1487 tree retval = gimple_return_retval (stmt);
1488 tree base;
1489 if (retval
1490 && TREE_CODE (retval) != SSA_NAME
1491 && !is_gimple_min_invariant (retval)
1492 && refs_may_alias_p (retval, ref))
1493 return true;
1494 /* If ref escapes the function then the return acts as a use. */
1495 base = get_base_address (ref);
1496 if (!base)
1498 else if (DECL_P (base))
1499 return is_global_var (base);
1500 else if (TREE_CODE (base) == MEM_REF
1501 || TREE_CODE (base) == TARGET_MEM_REF)
1502 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1503 return false;
1506 return true;
1509 /* If the call in statement CALL may clobber the memory reference REF
1510 return true, otherwise return false. */
1512 static bool
1513 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1515 tree base;
1516 tree callee;
1518 /* If the call is pure or const it cannot clobber anything. */
1519 if (gimple_call_flags (call)
1520 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1521 return false;
1523 base = ao_ref_base (ref);
1524 if (!base)
1525 return true;
1527 if (TREE_CODE (base) == SSA_NAME
1528 || CONSTANT_CLASS_P (base))
1529 return false;
1531 /* A call that is not without side-effects might involve volatile
1532 accesses and thus conflicts with all other volatile accesses. */
1533 if (ref->volatile_p)
1534 return true;
1536 /* If the reference is based on a decl that is not aliased the call
1537 cannot possibly clobber it. */
1538 if (DECL_P (base)
1539 && !may_be_aliased (base)
1540 /* But local non-readonly statics can be modified through recursion
1541 or the call may implement a threading barrier which we must
1542 treat as may-def. */
1543 && (TREE_READONLY (base)
1544 || !is_global_var (base)))
1545 return false;
1547 callee = gimple_call_fndecl (call);
1549 /* Handle those builtin functions explicitly that do not act as
1550 escape points. See tree-ssa-structalias.c:find_func_aliases
1551 for the list of builtins we might need to handle here. */
1552 if (callee != NULL_TREE
1553 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1554 switch (DECL_FUNCTION_CODE (callee))
1556 /* All the following functions clobber memory pointed to by
1557 their first argument. */
1558 case BUILT_IN_STRCPY:
1559 case BUILT_IN_STRNCPY:
1560 case BUILT_IN_MEMCPY:
1561 case BUILT_IN_MEMMOVE:
1562 case BUILT_IN_MEMPCPY:
1563 case BUILT_IN_STPCPY:
1564 case BUILT_IN_STPNCPY:
1565 case BUILT_IN_STRCAT:
1566 case BUILT_IN_STRNCAT:
1567 case BUILT_IN_MEMSET:
1568 case BUILT_IN_TM_MEMSET:
1569 CASE_BUILT_IN_TM_STORE (1):
1570 CASE_BUILT_IN_TM_STORE (2):
1571 CASE_BUILT_IN_TM_STORE (4):
1572 CASE_BUILT_IN_TM_STORE (8):
1573 CASE_BUILT_IN_TM_STORE (FLOAT):
1574 CASE_BUILT_IN_TM_STORE (DOUBLE):
1575 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1576 CASE_BUILT_IN_TM_STORE (M64):
1577 CASE_BUILT_IN_TM_STORE (M128):
1578 CASE_BUILT_IN_TM_STORE (M256):
1579 case BUILT_IN_TM_MEMCPY:
1580 case BUILT_IN_TM_MEMMOVE:
1582 ao_ref dref;
1583 tree size = NULL_TREE;
1584 /* Don't pass in size for strncat, as the maximum size
1585 is strlen (dest) + n + 1 instead of n, resp.
1586 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1587 known. */
1588 if (gimple_call_num_args (call) == 3
1589 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1590 size = gimple_call_arg (call, 2);
1591 ao_ref_init_from_ptr_and_size (&dref,
1592 gimple_call_arg (call, 0),
1593 size);
1594 return refs_may_alias_p_1 (&dref, ref, false);
1596 case BUILT_IN_STRCPY_CHK:
1597 case BUILT_IN_STRNCPY_CHK:
1598 case BUILT_IN_MEMCPY_CHK:
1599 case BUILT_IN_MEMMOVE_CHK:
1600 case BUILT_IN_MEMPCPY_CHK:
1601 case BUILT_IN_STPCPY_CHK:
1602 case BUILT_IN_STPNCPY_CHK:
1603 case BUILT_IN_STRCAT_CHK:
1604 case BUILT_IN_STRNCAT_CHK:
1605 case BUILT_IN_MEMSET_CHK:
1607 ao_ref dref;
1608 tree size = NULL_TREE;
1609 /* Don't pass in size for __strncat_chk, as the maximum size
1610 is strlen (dest) + n + 1 instead of n, resp.
1611 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1612 known. */
1613 if (gimple_call_num_args (call) == 4
1614 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1615 size = gimple_call_arg (call, 2);
1616 ao_ref_init_from_ptr_and_size (&dref,
1617 gimple_call_arg (call, 0),
1618 size);
1619 return refs_may_alias_p_1 (&dref, ref, false);
1621 case BUILT_IN_BCOPY:
1623 ao_ref dref;
1624 tree size = gimple_call_arg (call, 2);
1625 ao_ref_init_from_ptr_and_size (&dref,
1626 gimple_call_arg (call, 1),
1627 size);
1628 return refs_may_alias_p_1 (&dref, ref, false);
1630 /* Allocating memory does not have any side-effects apart from
1631 being the definition point for the pointer. */
1632 case BUILT_IN_MALLOC:
1633 case BUILT_IN_CALLOC:
1634 case BUILT_IN_STRDUP:
1635 case BUILT_IN_STRNDUP:
1636 /* Unix98 specifies that errno is set on allocation failure. */
1637 if (flag_errno_math
1638 && targetm.ref_may_alias_errno (ref))
1639 return true;
1640 return false;
1641 case BUILT_IN_STACK_SAVE:
1642 case BUILT_IN_ALLOCA:
1643 case BUILT_IN_ALLOCA_WITH_ALIGN:
1644 case BUILT_IN_ASSUME_ALIGNED:
1645 return false;
1646 /* Freeing memory kills the pointed-to memory. More importantly
1647 the call has to serve as a barrier for moving loads and stores
1648 across it. */
1649 case BUILT_IN_FREE:
1650 case BUILT_IN_VA_END:
1652 tree ptr = gimple_call_arg (call, 0);
1653 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1655 case BUILT_IN_GAMMA_R:
1656 case BUILT_IN_GAMMAF_R:
1657 case BUILT_IN_GAMMAL_R:
1658 case BUILT_IN_LGAMMA_R:
1659 case BUILT_IN_LGAMMAF_R:
1660 case BUILT_IN_LGAMMAL_R:
1662 tree out = gimple_call_arg (call, 1);
1663 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1664 return true;
1665 if (flag_errno_math)
1666 break;
1667 return false;
1669 case BUILT_IN_FREXP:
1670 case BUILT_IN_FREXPF:
1671 case BUILT_IN_FREXPL:
1672 case BUILT_IN_MODF:
1673 case BUILT_IN_MODFF:
1674 case BUILT_IN_MODFL:
1676 tree out = gimple_call_arg (call, 1);
1677 return ptr_deref_may_alias_ref_p_1 (out, ref);
1679 case BUILT_IN_REMQUO:
1680 case BUILT_IN_REMQUOF:
1681 case BUILT_IN_REMQUOL:
1683 tree out = gimple_call_arg (call, 2);
1684 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1685 return true;
1686 if (flag_errno_math)
1687 break;
1688 return false;
1690 case BUILT_IN_SINCOS:
1691 case BUILT_IN_SINCOSF:
1692 case BUILT_IN_SINCOSL:
1694 tree sin = gimple_call_arg (call, 1);
1695 tree cos = gimple_call_arg (call, 2);
1696 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1697 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1699 /* __sync_* builtins and some OpenMP builtins act as threading
1700 barriers. */
1701 #undef DEF_SYNC_BUILTIN
1702 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1703 #include "sync-builtins.def"
1704 #undef DEF_SYNC_BUILTIN
1705 case BUILT_IN_GOMP_ATOMIC_START:
1706 case BUILT_IN_GOMP_ATOMIC_END:
1707 case BUILT_IN_GOMP_BARRIER:
1708 case BUILT_IN_GOMP_TASKWAIT:
1709 case BUILT_IN_GOMP_CRITICAL_START:
1710 case BUILT_IN_GOMP_CRITICAL_END:
1711 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1712 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1713 case BUILT_IN_GOMP_LOOP_END:
1714 case BUILT_IN_GOMP_ORDERED_START:
1715 case BUILT_IN_GOMP_ORDERED_END:
1716 case BUILT_IN_GOMP_PARALLEL_END:
1717 case BUILT_IN_GOMP_SECTIONS_END:
1718 case BUILT_IN_GOMP_SINGLE_COPY_START:
1719 case BUILT_IN_GOMP_SINGLE_COPY_END:
1720 return true;
1721 default:
1722 /* Fallthru to general call handling. */;
1725 /* Check if base is a global static variable that is not written
1726 by the function. */
1727 if (callee != NULL_TREE
1728 && TREE_CODE (base) == VAR_DECL
1729 && TREE_STATIC (base))
1731 struct cgraph_node *node = cgraph_get_node (callee);
1732 bitmap not_written;
1734 if (node
1735 && (not_written = ipa_reference_get_not_written_global (node))
1736 && bitmap_bit_p (not_written, DECL_UID (base)))
1737 return false;
1740 /* Check if the base variable is call-clobbered. */
1741 if (DECL_P (base))
1742 return pt_solution_includes (gimple_call_clobber_set (call), base);
1743 else if ((TREE_CODE (base) == MEM_REF
1744 || TREE_CODE (base) == TARGET_MEM_REF)
1745 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1747 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1748 if (!pi)
1749 return true;
1751 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1754 return true;
1757 /* If the call in statement CALL may clobber the memory reference REF
1758 return true, otherwise return false. */
1760 bool
1761 call_may_clobber_ref_p (gimple call, tree ref)
1763 bool res;
1764 ao_ref r;
1765 ao_ref_init (&r, ref);
1766 res = call_may_clobber_ref_p_1 (call, &r);
1767 if (res)
1768 ++alias_stats.call_may_clobber_ref_p_may_alias;
1769 else
1770 ++alias_stats.call_may_clobber_ref_p_no_alias;
1771 return res;
1775 /* If the statement STMT may clobber the memory reference REF return true,
1776 otherwise return false. */
1778 bool
1779 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1781 if (is_gimple_call (stmt))
1783 tree lhs = gimple_call_lhs (stmt);
1784 if (lhs
1785 && TREE_CODE (lhs) != SSA_NAME)
1787 ao_ref r;
1788 ao_ref_init (&r, lhs);
1789 if (refs_may_alias_p_1 (ref, &r, true))
1790 return true;
1793 return call_may_clobber_ref_p_1 (stmt, ref);
1795 else if (gimple_assign_single_p (stmt))
1797 tree lhs = gimple_assign_lhs (stmt);
1798 if (TREE_CODE (lhs) != SSA_NAME)
1800 ao_ref r;
1801 ao_ref_init (&r, lhs);
1802 return refs_may_alias_p_1 (ref, &r, true);
1805 else if (gimple_code (stmt) == GIMPLE_ASM)
1806 return true;
1808 return false;
1811 bool
1812 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1814 ao_ref r;
1815 ao_ref_init (&r, ref);
1816 return stmt_may_clobber_ref_p_1 (stmt, &r);
1819 /* If STMT kills the memory reference REF return true, otherwise
1820 return false. */
1822 static bool
1823 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1825 /* For a must-alias check we need to be able to constrain
1826 the access properly. */
1827 ao_ref_base (ref);
1828 if (ref->max_size == -1)
1829 return false;
1831 if (gimple_has_lhs (stmt)
1832 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1833 /* The assignment is not necessarily carried out if it can throw
1834 and we can catch it in the current function where we could inspect
1835 the previous value.
1836 ??? We only need to care about the RHS throwing. For aggregate
1837 assignments or similar calls and non-call exceptions the LHS
1838 might throw as well. */
1839 && !stmt_can_throw_internal (stmt))
1841 tree base, lhs = gimple_get_lhs (stmt);
1842 HOST_WIDE_INT size, offset, max_size;
1843 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1844 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1845 so base == ref->base does not always hold. */
1846 if (base == ref->base)
1848 /* For a must-alias check we need to be able to constrain
1849 the access properly. */
1850 if (size != -1 && size == max_size)
1852 if (offset <= ref->offset
1853 && offset + size >= ref->offset + ref->max_size)
1854 return true;
1859 if (is_gimple_call (stmt))
1861 tree callee = gimple_call_fndecl (stmt);
1862 if (callee != NULL_TREE
1863 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1864 switch (DECL_FUNCTION_CODE (callee))
1866 case BUILT_IN_MEMCPY:
1867 case BUILT_IN_MEMPCPY:
1868 case BUILT_IN_MEMMOVE:
1869 case BUILT_IN_MEMSET:
1870 case BUILT_IN_MEMCPY_CHK:
1871 case BUILT_IN_MEMPCPY_CHK:
1872 case BUILT_IN_MEMMOVE_CHK:
1873 case BUILT_IN_MEMSET_CHK:
1875 tree dest = gimple_call_arg (stmt, 0);
1876 tree len = gimple_call_arg (stmt, 2);
1877 tree base = NULL_TREE;
1878 HOST_WIDE_INT offset = 0;
1879 if (!host_integerp (len, 0))
1880 return false;
1881 if (TREE_CODE (dest) == ADDR_EXPR)
1882 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1883 &offset);
1884 else if (TREE_CODE (dest) == SSA_NAME)
1885 base = dest;
1886 if (base
1887 && base == ao_ref_base (ref))
1889 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1890 if (offset <= ref->offset / BITS_PER_UNIT
1891 && (offset + size
1892 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1893 / BITS_PER_UNIT)))
1894 return true;
1896 break;
1899 case BUILT_IN_VA_END:
1901 tree ptr = gimple_call_arg (stmt, 0);
1902 if (TREE_CODE (ptr) == ADDR_EXPR)
1904 tree base = ao_ref_base (ref);
1905 if (TREE_OPERAND (ptr, 0) == base)
1906 return true;
1908 break;
1911 default:;
1914 return false;
1917 bool
1918 stmt_kills_ref_p (gimple stmt, tree ref)
1920 ao_ref r;
1921 ao_ref_init (&r, ref);
1922 return stmt_kills_ref_p_1 (stmt, &r);
1926 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1927 TARGET or a statement clobbering the memory reference REF in which
1928 case false is returned. The walk starts with VUSE, one argument of PHI. */
1930 static bool
1931 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1932 tree vuse, bitmap *visited)
1934 basic_block bb = gimple_bb (phi);
1936 if (!*visited)
1937 *visited = BITMAP_ALLOC (NULL);
1939 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1941 /* Walk until we hit the target. */
1942 while (vuse != target)
1944 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1945 /* Recurse for PHI nodes. */
1946 if (gimple_code (def_stmt) == GIMPLE_PHI)
1948 /* An already visited PHI node ends the walk successfully. */
1949 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1950 return true;
1951 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1952 if (!vuse)
1953 return false;
1954 continue;
1956 /* A clobbering statement or the end of the IL ends it failing. */
1957 else if (gimple_nop_p (def_stmt)
1958 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1959 return false;
1960 /* If we reach a new basic-block see if we already skipped it
1961 in a previous walk that ended successfully. */
1962 if (gimple_bb (def_stmt) != bb)
1964 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
1965 return true;
1966 bb = gimple_bb (def_stmt);
1968 vuse = gimple_vuse (def_stmt);
1970 return true;
1973 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
1974 until we hit the phi argument definition that dominates the other one.
1975 Return that, or NULL_TREE if there is no such definition. */
1977 static tree
1978 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
1979 ao_ref *ref, bitmap *visited)
1981 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1982 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1983 tree common_vuse;
1985 if (arg0 == arg1)
1986 return arg0;
1987 else if (gimple_nop_p (def0)
1988 || (!gimple_nop_p (def1)
1989 && dominated_by_p (CDI_DOMINATORS,
1990 gimple_bb (def1), gimple_bb (def0))))
1992 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1993 return arg0;
1995 else if (gimple_nop_p (def1)
1996 || dominated_by_p (CDI_DOMINATORS,
1997 gimple_bb (def0), gimple_bb (def1)))
1999 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
2000 return arg1;
2002 /* Special case of a diamond:
2003 MEM_1 = ...
2004 goto (cond) ? L1 : L2
2005 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2006 goto L3
2007 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2008 L3: MEM_4 = PHI<MEM_2, MEM_3>
2009 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2010 dominate each other, but still we can easily skip this PHI node
2011 if we recognize that the vuse MEM operand is the same for both,
2012 and that we can skip both statements (they don't clobber us).
2013 This is still linear. Don't use maybe_skip_until, that might
2014 potentially be slow. */
2015 else if ((common_vuse = gimple_vuse (def0))
2016 && common_vuse == gimple_vuse (def1))
2018 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2019 && !stmt_may_clobber_ref_p_1 (def1, ref))
2020 return common_vuse;
2023 return NULL_TREE;
2027 /* Starting from a PHI node for the virtual operand of the memory reference
2028 REF find a continuation virtual operand that allows to continue walking
2029 statements dominating PHI skipping only statements that cannot possibly
2030 clobber REF. Returns NULL_TREE if no suitable virtual operand can
2031 be found. */
2033 tree
2034 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
2036 unsigned nargs = gimple_phi_num_args (phi);
2038 /* Through a single-argument PHI we can simply look through. */
2039 if (nargs == 1)
2040 return PHI_ARG_DEF (phi, 0);
2042 /* For two or more arguments try to pairwise skip non-aliasing code
2043 until we hit the phi argument definition that dominates the other one. */
2044 else if (nargs >= 2)
2046 tree arg0, arg1;
2047 unsigned i;
2049 /* Find a candidate for the virtual operand which definition
2050 dominates those of all others. */
2051 arg0 = PHI_ARG_DEF (phi, 0);
2052 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2053 for (i = 1; i < nargs; ++i)
2055 arg1 = PHI_ARG_DEF (phi, i);
2056 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2058 arg0 = arg1;
2059 break;
2061 if (dominated_by_p (CDI_DOMINATORS,
2062 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2063 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2064 arg0 = arg1;
2067 /* Then pairwise reduce against the found candidate. */
2068 for (i = 0; i < nargs; ++i)
2070 arg1 = PHI_ARG_DEF (phi, i);
2071 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref, visited);
2072 if (!arg0)
2073 return NULL_TREE;
2076 return arg0;
2079 return NULL_TREE;
2082 /* Based on the memory reference REF and its virtual use VUSE call
2083 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2084 itself. That is, for each virtual use for which its defining statement
2085 does not clobber REF.
2087 WALKER is called with REF, the current virtual use and DATA. If
2088 WALKER returns non-NULL the walk stops and its result is returned.
2089 At the end of a non-successful walk NULL is returned.
2091 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2092 use which definition is a statement that may clobber REF and DATA.
2093 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2094 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2095 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2096 to adjust REF and *DATA to make that valid.
2098 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2100 void *
2101 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2102 void *(*walker)(ao_ref *, tree, void *),
2103 void *(*translate)(ao_ref *, tree, void *), void *data)
2105 bitmap visited = NULL;
2106 void *res;
2108 timevar_push (TV_ALIAS_STMT_WALK);
2112 gimple def_stmt;
2114 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2115 res = (*walker) (ref, vuse, data);
2116 if (res)
2117 break;
2119 def_stmt = SSA_NAME_DEF_STMT (vuse);
2120 if (gimple_nop_p (def_stmt))
2121 break;
2122 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2123 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
2124 else
2126 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2128 if (!translate)
2129 break;
2130 res = (*translate) (ref, vuse, data);
2131 /* Failed lookup and translation. */
2132 if (res == (void *)-1)
2134 res = NULL;
2135 break;
2137 /* Lookup succeeded. */
2138 else if (res != NULL)
2139 break;
2140 /* Translation succeeded, continue walking. */
2142 vuse = gimple_vuse (def_stmt);
2145 while (vuse);
2147 if (visited)
2148 BITMAP_FREE (visited);
2150 timevar_pop (TV_ALIAS_STMT_WALK);
2152 return res;
2156 /* Based on the memory reference REF call WALKER for each vdef which
2157 defining statement may clobber REF, starting with VDEF. If REF
2158 is NULL_TREE, each defining statement is visited.
2160 WALKER is called with REF, the current vdef and DATA. If WALKER
2161 returns true the walk is stopped, otherwise it continues.
2163 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2164 PHI argument (but only one walk continues on merge points), the
2165 return value is true if any of the walks was successful.
2167 The function returns the number of statements walked. */
2169 static unsigned int
2170 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2171 bool (*walker)(ao_ref *, tree, void *), void *data,
2172 bitmap *visited, unsigned int cnt)
2176 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2178 if (*visited
2179 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2180 return cnt;
2182 if (gimple_nop_p (def_stmt))
2183 return cnt;
2184 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2186 unsigned i;
2187 if (!*visited)
2188 *visited = BITMAP_ALLOC (NULL);
2189 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2190 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2191 walker, data, visited, 0);
2192 return cnt;
2195 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2196 cnt++;
2197 if ((!ref
2198 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2199 && (*walker) (ref, vdef, data))
2200 return cnt;
2202 vdef = gimple_vuse (def_stmt);
2204 while (1);
2207 unsigned int
2208 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2209 bool (*walker)(ao_ref *, tree, void *), void *data,
2210 bitmap *visited)
2212 bitmap local_visited = NULL;
2213 unsigned int ret;
2215 timevar_push (TV_ALIAS_STMT_WALK);
2217 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2218 visited ? visited : &local_visited, 0);
2219 if (local_visited)
2220 BITMAP_FREE (local_visited);
2222 timevar_pop (TV_ALIAS_STMT_WALK);
2224 return ret;