* varasm.c (bss_initializer_p): Remove static.
[official-gcc.git] / gcc / tree-ssa-alias.c
blob0b9b090a37cce646dccf0eb82ab30c3dfa9c52e7
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 "pointer-set.h"
44 #include "alloc-pool.h"
45 #include "tree-ssa-alias.h"
47 /* Broad overview of how alias analysis on gimple works:
49 Statements clobbering or using memory are linked through the
50 virtual operand factored use-def chain. The virtual operand
51 is unique per function, its symbol is accessible via gimple_vop (cfun).
52 Virtual operands are used for efficiently walking memory statements
53 in the gimple IL and are useful for things like value-numbering as
54 a generation count for memory references.
56 SSA_NAME pointers may have associated points-to information
57 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
58 points-to information is (re-)computed by the TODO_rebuild_alias
59 pass manager todo. Points-to information is also used for more
60 precise tracking of call-clobbered and call-used variables and
61 related disambiguations.
63 This file contains functions for disambiguating memory references,
64 the so called alias-oracle and tools for walking of the gimple IL.
66 The main alias-oracle entry-points are
68 bool stmt_may_clobber_ref_p (gimple, tree)
70 This function queries if a statement may invalidate (parts of)
71 the memory designated by the reference tree argument.
73 bool ref_maybe_used_by_stmt_p (gimple, tree)
75 This function queries if a statement may need (parts of) the
76 memory designated by the reference tree argument.
78 There are variants of these functions that only handle the call
79 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
80 Note that these do not disambiguate against a possible call lhs.
82 bool refs_may_alias_p (tree, tree)
84 This function tries to disambiguate two reference trees.
86 bool ptr_deref_may_alias_global_p (tree)
88 This function queries if dereferencing a pointer variable may
89 alias global memory.
91 More low-level disambiguators are available and documented in
92 this file. Low-level disambiguators dealing with points-to
93 information are in tree-ssa-structalias.c. */
96 /* Query statistics for the different low-level disambiguators.
97 A high-level query may trigger multiple of them. */
99 static struct {
100 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
101 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
102 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
103 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
104 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
105 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
106 } alias_stats;
108 void
109 dump_alias_stats (FILE *s)
111 fprintf (s, "\nAlias oracle query stats:\n");
112 fprintf (s, " refs_may_alias_p: "
113 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
114 HOST_WIDE_INT_PRINT_DEC" queries\n",
115 alias_stats.refs_may_alias_p_no_alias,
116 alias_stats.refs_may_alias_p_no_alias
117 + alias_stats.refs_may_alias_p_may_alias);
118 fprintf (s, " ref_maybe_used_by_call_p: "
119 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
120 HOST_WIDE_INT_PRINT_DEC" queries\n",
121 alias_stats.ref_maybe_used_by_call_p_no_alias,
122 alias_stats.refs_may_alias_p_no_alias
123 + alias_stats.ref_maybe_used_by_call_p_may_alias);
124 fprintf (s, " call_may_clobber_ref_p: "
125 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
126 HOST_WIDE_INT_PRINT_DEC" queries\n",
127 alias_stats.call_may_clobber_ref_p_no_alias,
128 alias_stats.call_may_clobber_ref_p_no_alias
129 + alias_stats.call_may_clobber_ref_p_may_alias);
133 /* Return true, if dereferencing PTR may alias with a global variable. */
135 bool
136 ptr_deref_may_alias_global_p (tree ptr)
138 struct ptr_info_def *pi;
140 /* If we end up with a pointer constant here that may point
141 to global memory. */
142 if (TREE_CODE (ptr) != SSA_NAME)
143 return true;
145 pi = SSA_NAME_PTR_INFO (ptr);
147 /* If we do not have points-to information for this variable,
148 we have to punt. */
149 if (!pi)
150 return true;
152 /* ??? This does not use TBAA to prune globals ptr may not access. */
153 return pt_solution_includes_global (&pi->pt);
156 /* Return true if dereferencing PTR may alias DECL.
157 The caller is responsible for applying TBAA to see if PTR
158 may access DECL at all. */
160 static bool
161 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
163 struct ptr_info_def *pi;
165 /* Conversions are irrelevant for points-to information and
166 data-dependence analysis can feed us those. */
167 STRIP_NOPS (ptr);
169 /* Anything we do not explicilty handle aliases. */
170 if ((TREE_CODE (ptr) != SSA_NAME
171 && TREE_CODE (ptr) != ADDR_EXPR
172 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
173 || !POINTER_TYPE_P (TREE_TYPE (ptr))
174 || (TREE_CODE (decl) != VAR_DECL
175 && TREE_CODE (decl) != PARM_DECL
176 && TREE_CODE (decl) != RESULT_DECL))
177 return true;
179 /* Disregard pointer offsetting. */
180 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
184 ptr = TREE_OPERAND (ptr, 0);
186 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
187 return ptr_deref_may_alias_decl_p (ptr, decl);
190 /* ADDR_EXPR pointers either just offset another pointer or directly
191 specify the pointed-to set. */
192 if (TREE_CODE (ptr) == ADDR_EXPR)
194 tree base = get_base_address (TREE_OPERAND (ptr, 0));
195 if (base
196 && (TREE_CODE (base) == MEM_REF
197 || TREE_CODE (base) == TARGET_MEM_REF))
198 ptr = TREE_OPERAND (base, 0);
199 else if (base
200 && DECL_P (base))
201 return base == decl;
202 else if (base
203 && CONSTANT_CLASS_P (base))
204 return false;
205 else
206 return true;
209 /* Non-aliased variables can not be pointed to. */
210 if (!may_be_aliased (decl))
211 return false;
213 /* If we do not have useful points-to information for this pointer
214 we cannot disambiguate anything else. */
215 pi = SSA_NAME_PTR_INFO (ptr);
216 if (!pi)
217 return true;
219 return pt_solution_includes (&pi->pt, decl);
222 /* Return true if dereferenced PTR1 and PTR2 may alias.
223 The caller is responsible for applying TBAA to see if accesses
224 through PTR1 and PTR2 may conflict at all. */
226 bool
227 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
229 struct ptr_info_def *pi1, *pi2;
231 /* Conversions are irrelevant for points-to information and
232 data-dependence analysis can feed us those. */
233 STRIP_NOPS (ptr1);
234 STRIP_NOPS (ptr2);
236 /* Disregard pointer offsetting. */
237 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
241 ptr1 = TREE_OPERAND (ptr1, 0);
243 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
244 return ptr_derefs_may_alias_p (ptr1, ptr2);
246 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
250 ptr2 = TREE_OPERAND (ptr2, 0);
252 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
253 return ptr_derefs_may_alias_p (ptr1, ptr2);
256 /* ADDR_EXPR pointers either just offset another pointer or directly
257 specify the pointed-to set. */
258 if (TREE_CODE (ptr1) == ADDR_EXPR)
260 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
261 if (base
262 && (TREE_CODE (base) == MEM_REF
263 || TREE_CODE (base) == TARGET_MEM_REF))
264 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
265 else if (base
266 && DECL_P (base))
267 return ptr_deref_may_alias_decl_p (ptr2, base);
268 else
269 return true;
271 if (TREE_CODE (ptr2) == ADDR_EXPR)
273 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
274 if (base
275 && (TREE_CODE (base) == MEM_REF
276 || TREE_CODE (base) == TARGET_MEM_REF))
277 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
278 else if (base
279 && DECL_P (base))
280 return ptr_deref_may_alias_decl_p (ptr1, base);
281 else
282 return true;
285 /* From here we require SSA name pointers. Anything else aliases. */
286 if (TREE_CODE (ptr1) != SSA_NAME
287 || TREE_CODE (ptr2) != SSA_NAME
288 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
289 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
290 return true;
292 /* We may end up with two empty points-to solutions for two same pointers.
293 In this case we still want to say both pointers alias, so shortcut
294 that here. */
295 if (ptr1 == ptr2)
296 return true;
298 /* If we do not have useful points-to information for either pointer
299 we cannot disambiguate anything else. */
300 pi1 = SSA_NAME_PTR_INFO (ptr1);
301 pi2 = SSA_NAME_PTR_INFO (ptr2);
302 if (!pi1 || !pi2)
303 return true;
305 /* ??? This does not use TBAA to prune decls from the intersection
306 that not both pointers may access. */
307 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
310 /* Return true if dereferencing PTR may alias *REF.
311 The caller is responsible for applying TBAA to see if PTR
312 may access *REF at all. */
314 static bool
315 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
317 tree base = ao_ref_base (ref);
319 if (TREE_CODE (base) == MEM_REF
320 || TREE_CODE (base) == TARGET_MEM_REF)
321 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
322 else if (DECL_P (base))
323 return ptr_deref_may_alias_decl_p (ptr, base);
325 return true;
328 /* Return true whether REF may refer to global memory. */
330 bool
331 ref_may_alias_global_p (tree ref)
333 tree base = get_base_address (ref);
334 if (DECL_P (base))
335 return is_global_var (base);
336 else if (TREE_CODE (base) == MEM_REF
337 || TREE_CODE (base) == TARGET_MEM_REF)
338 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
339 return true;
342 /* Return true whether STMT may clobber global memory. */
344 bool
345 stmt_may_clobber_global_p (gimple stmt)
347 tree lhs;
349 if (!gimple_vdef (stmt))
350 return false;
352 /* ??? We can ask the oracle whether an artificial pointer
353 dereference with a pointer with points-to information covering
354 all global memory (what about non-address taken memory?) maybe
355 clobbered by this call. As there is at the moment no convenient
356 way of doing that without generating garbage do some manual
357 checking instead.
358 ??? We could make a NULL ao_ref argument to the various
359 predicates special, meaning any global memory. */
361 switch (gimple_code (stmt))
363 case GIMPLE_ASSIGN:
364 lhs = gimple_assign_lhs (stmt);
365 return (TREE_CODE (lhs) != SSA_NAME
366 && ref_may_alias_global_p (lhs));
367 case GIMPLE_CALL:
368 return true;
369 default:
370 return true;
375 /* Dump alias information on FILE. */
377 void
378 dump_alias_info (FILE *file)
380 unsigned i;
381 const char *funcname
382 = lang_hooks.decl_printable_name (current_function_decl, 2);
383 tree var;
385 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
387 fprintf (file, "Aliased symbols\n\n");
389 FOR_EACH_LOCAL_DECL (cfun, i, var)
391 if (may_be_aliased (var))
392 dump_variable (file, var);
395 fprintf (file, "\nCall clobber information\n");
397 fprintf (file, "\nESCAPED");
398 dump_points_to_solution (file, &cfun->gimple_df->escaped);
400 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
402 for (i = 1; i < num_ssa_names; i++)
404 tree ptr = ssa_name (i);
405 struct ptr_info_def *pi;
407 if (ptr == NULL_TREE
408 || SSA_NAME_IN_FREE_LIST (ptr))
409 continue;
411 pi = SSA_NAME_PTR_INFO (ptr);
412 if (pi)
413 dump_points_to_info_for (file, ptr);
416 fprintf (file, "\n");
420 /* Dump alias information on stderr. */
422 DEBUG_FUNCTION void
423 debug_alias_info (void)
425 dump_alias_info (stderr);
429 /* Dump the points-to set *PT into FILE. */
431 void
432 dump_points_to_solution (FILE *file, struct pt_solution *pt)
434 if (pt->anything)
435 fprintf (file, ", points-to anything");
437 if (pt->nonlocal)
438 fprintf (file, ", points-to non-local");
440 if (pt->escaped)
441 fprintf (file, ", points-to escaped");
443 if (pt->ipa_escaped)
444 fprintf (file, ", points-to unit escaped");
446 if (pt->null)
447 fprintf (file, ", points-to NULL");
449 if (pt->vars)
451 fprintf (file, ", points-to vars: ");
452 dump_decl_set (file, pt->vars);
453 if (pt->vars_contains_global)
454 fprintf (file, " (includes global vars)");
458 /* Dump points-to information for SSA_NAME PTR into FILE. */
460 void
461 dump_points_to_info_for (FILE *file, tree ptr)
463 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
465 print_generic_expr (file, ptr, dump_flags);
467 if (pi)
468 dump_points_to_solution (file, &pi->pt);
469 else
470 fprintf (file, ", points-to anything");
472 fprintf (file, "\n");
476 /* Dump points-to information for VAR into stderr. */
478 DEBUG_FUNCTION void
479 debug_points_to_info_for (tree var)
481 dump_points_to_info_for (stderr, var);
485 /* Initializes the alias-oracle reference representation *R from REF. */
487 void
488 ao_ref_init (ao_ref *r, tree ref)
490 r->ref = ref;
491 r->base = NULL_TREE;
492 r->offset = 0;
493 r->size = -1;
494 r->max_size = -1;
495 r->ref_alias_set = -1;
496 r->base_alias_set = -1;
497 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
500 /* Returns the base object of the memory reference *REF. */
502 tree
503 ao_ref_base (ao_ref *ref)
505 if (ref->base)
506 return ref->base;
507 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
508 &ref->max_size);
509 return ref->base;
512 /* Returns the base object alias set of the memory reference *REF. */
514 static alias_set_type
515 ao_ref_base_alias_set (ao_ref *ref)
517 tree base_ref;
518 if (ref->base_alias_set != -1)
519 return ref->base_alias_set;
520 if (!ref->ref)
521 return 0;
522 base_ref = ref->ref;
523 while (handled_component_p (base_ref))
524 base_ref = TREE_OPERAND (base_ref, 0);
525 ref->base_alias_set = get_alias_set (base_ref);
526 return ref->base_alias_set;
529 /* Returns the reference alias set of the memory reference *REF. */
531 alias_set_type
532 ao_ref_alias_set (ao_ref *ref)
534 if (ref->ref_alias_set != -1)
535 return ref->ref_alias_set;
536 ref->ref_alias_set = get_alias_set (ref->ref);
537 return ref->ref_alias_set;
540 /* Init an alias-oracle reference representation from a gimple pointer
541 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
542 size is assumed to be unknown. The access is assumed to be only
543 to or after of the pointer target, not before it. */
545 void
546 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
548 HOST_WIDE_INT t1, t2;
549 ref->ref = NULL_TREE;
550 if (TREE_CODE (ptr) == ADDR_EXPR)
551 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
552 &ref->offset, &t1, &t2);
553 else
555 ref->base = build2 (MEM_REF, char_type_node,
556 ptr, null_pointer_node);
557 ref->offset = 0;
559 if (size
560 && host_integerp (size, 0)
561 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
562 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
563 else
564 ref->max_size = ref->size = -1;
565 ref->ref_alias_set = 0;
566 ref->base_alias_set = 0;
567 ref->volatile_p = false;
570 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
571 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
572 decide. */
574 static inline int
575 same_type_for_tbaa (tree type1, tree type2)
577 type1 = TYPE_MAIN_VARIANT (type1);
578 type2 = TYPE_MAIN_VARIANT (type2);
580 /* If we would have to do structural comparison bail out. */
581 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
582 || TYPE_STRUCTURAL_EQUALITY_P (type2))
583 return -1;
585 /* Compare the canonical types. */
586 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
587 return 1;
589 /* ??? Array types are not properly unified in all cases as we have
590 spurious changes in the index types for example. Removing this
591 causes all sorts of problems with the Fortran frontend. */
592 if (TREE_CODE (type1) == ARRAY_TYPE
593 && TREE_CODE (type2) == ARRAY_TYPE)
594 return -1;
596 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
597 object of one of its constrained subtypes, e.g. when a function with an
598 unconstrained parameter passed by reference is called on an object and
599 inlined. But, even in the case of a fixed size, type and subtypes are
600 not equivalent enough as to share the same TYPE_CANONICAL, since this
601 would mean that conversions between them are useless, whereas they are
602 not (e.g. type and subtypes can have different modes). So, in the end,
603 they are only guaranteed to have the same alias set. */
604 if (get_alias_set (type1) == get_alias_set (type2))
605 return -1;
607 /* The types are known to be not equal. */
608 return 0;
611 /* Determine if the two component references REF1 and REF2 which are
612 based on access types TYPE1 and TYPE2 and of which at least one is based
613 on an indirect reference may alias. REF2 is the only one that can
614 be a decl in which case REF2_IS_DECL is true.
615 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
616 are the respective alias sets. */
618 static bool
619 aliasing_component_refs_p (tree ref1,
620 alias_set_type ref1_alias_set,
621 alias_set_type base1_alias_set,
622 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
623 tree ref2,
624 alias_set_type ref2_alias_set,
625 alias_set_type base2_alias_set,
626 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
627 bool ref2_is_decl)
629 /* If one reference is a component references through pointers try to find a
630 common base and apply offset based disambiguation. This handles
631 for example
632 struct A { int i; int j; } *q;
633 struct B { struct A a; int k; } *p;
634 disambiguating q->i and p->a.j. */
635 tree base1, base2;
636 tree type1, type2;
637 tree *refp;
638 int same_p;
640 /* Choose bases and base types to search for. */
641 base1 = ref1;
642 while (handled_component_p (base1))
643 base1 = TREE_OPERAND (base1, 0);
644 type1 = TREE_TYPE (base1);
645 base2 = ref2;
646 while (handled_component_p (base2))
647 base2 = TREE_OPERAND (base2, 0);
648 type2 = TREE_TYPE (base2);
650 /* Now search for the type1 in the access path of ref2. This
651 would be a common base for doing offset based disambiguation on. */
652 refp = &ref2;
653 while (handled_component_p (*refp)
654 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
655 refp = &TREE_OPERAND (*refp, 0);
656 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
657 /* If we couldn't compare types we have to bail out. */
658 if (same_p == -1)
659 return true;
660 else if (same_p == 1)
662 HOST_WIDE_INT offadj, sztmp, msztmp;
663 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
664 offset2 -= offadj;
665 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
666 offset1 -= offadj;
667 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
669 /* If we didn't find a common base, try the other way around. */
670 refp = &ref1;
671 while (handled_component_p (*refp)
672 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
673 refp = &TREE_OPERAND (*refp, 0);
674 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
675 /* If we couldn't compare types we have to bail out. */
676 if (same_p == -1)
677 return true;
678 else if (same_p == 1)
680 HOST_WIDE_INT offadj, sztmp, msztmp;
681 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
682 offset1 -= offadj;
683 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
684 offset2 -= offadj;
685 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
688 /* If we have two type access paths B1.path1 and B2.path2 they may
689 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
690 But we can still have a path that goes B1.path1...B2.path2 with
691 a part that we do not see. So we can only disambiguate now
692 if there is no B2 in the tail of path1 and no B1 on the
693 tail of path2. */
694 if (base1_alias_set == ref2_alias_set
695 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
696 return true;
697 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
698 if (!ref2_is_decl)
699 return (base2_alias_set == ref1_alias_set
700 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
701 return false;
704 /* Return true if two memory references based on the variables BASE1
705 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
706 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
708 static bool
709 decl_refs_may_alias_p (tree base1,
710 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
711 tree base2,
712 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
714 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
716 /* If both references are based on different variables, they cannot alias. */
717 if (base1 != base2)
718 return false;
720 /* If both references are based on the same variable, they cannot alias if
721 the accesses do not overlap. */
722 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
725 /* Return true if an indirect reference based on *PTR1 constrained
726 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
727 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
728 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
729 in which case they are computed on-demand. REF1 and REF2
730 if non-NULL are the complete memory reference trees. */
732 static bool
733 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
734 HOST_WIDE_INT offset1,
735 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
736 alias_set_type ref1_alias_set,
737 alias_set_type base1_alias_set,
738 tree ref2 ATTRIBUTE_UNUSED, tree base2,
739 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
740 alias_set_type ref2_alias_set,
741 alias_set_type base2_alias_set, bool tbaa_p)
743 tree ptr1;
744 tree ptrtype1, dbase2;
745 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
746 HOST_WIDE_INT doffset1, doffset2;
747 double_int moff;
749 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
750 || TREE_CODE (base1) == TARGET_MEM_REF)
751 && DECL_P (base2));
753 ptr1 = TREE_OPERAND (base1, 0);
755 /* The offset embedded in MEM_REFs can be negative. Bias them
756 so that the resulting offset adjustment is positive. */
757 moff = mem_ref_offset (base1);
758 moff = moff.alshift (BITS_PER_UNIT == 8
759 ? 3 : exact_log2 (BITS_PER_UNIT),
760 HOST_BITS_PER_DOUBLE_INT);
761 if (moff.is_negative ())
762 offset2p += (-moff).low;
763 else
764 offset1p += moff.low;
766 /* If only one reference is based on a variable, they cannot alias if
767 the pointer access is beyond the extent of the variable access.
768 (the pointer base cannot validly point to an offset less than zero
769 of the variable).
770 ??? IVOPTs creates bases that do not honor this restriction,
771 so do not apply this optimization for TARGET_MEM_REFs. */
772 if (TREE_CODE (base1) != TARGET_MEM_REF
773 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
774 return false;
775 /* They also cannot alias if the pointer may not point to the decl. */
776 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
777 return false;
779 /* Disambiguations that rely on strict aliasing rules follow. */
780 if (!flag_strict_aliasing || !tbaa_p)
781 return true;
783 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
785 /* If the alias set for a pointer access is zero all bets are off. */
786 if (base1_alias_set == -1)
787 base1_alias_set = get_deref_alias_set (ptrtype1);
788 if (base1_alias_set == 0)
789 return true;
790 if (base2_alias_set == -1)
791 base2_alias_set = get_alias_set (base2);
793 /* When we are trying to disambiguate an access with a pointer dereference
794 as base versus one with a decl as base we can use both the size
795 of the decl and its dynamic type for extra disambiguation.
796 ??? We do not know anything about the dynamic type of the decl
797 other than that its alias-set contains base2_alias_set as a subset
798 which does not help us here. */
799 /* As we know nothing useful about the dynamic type of the decl just
800 use the usual conflict check rather than a subset test.
801 ??? We could introduce -fvery-strict-aliasing when the language
802 does not allow decls to have a dynamic type that differs from their
803 static type. Then we can check
804 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
805 if (base1_alias_set != base2_alias_set
806 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
807 return false;
808 /* If the size of the access relevant for TBAA through the pointer
809 is bigger than the size of the decl we can't possibly access the
810 decl via that pointer. */
811 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
812 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
813 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
814 /* ??? This in turn may run afoul when a decl of type T which is
815 a member of union type U is accessed through a pointer to
816 type U and sizeof T is smaller than sizeof U. */
817 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
818 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
819 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
820 return false;
822 if (!ref2)
823 return true;
825 /* If the decl is accessed via a MEM_REF, reconstruct the base
826 we can use for TBAA and an appropriately adjusted offset. */
827 dbase2 = ref2;
828 while (handled_component_p (dbase2))
829 dbase2 = TREE_OPERAND (dbase2, 0);
830 doffset1 = offset1;
831 doffset2 = offset2;
832 if (TREE_CODE (dbase2) == MEM_REF
833 || TREE_CODE (dbase2) == TARGET_MEM_REF)
835 double_int moff = mem_ref_offset (dbase2);
836 moff = moff.alshift (BITS_PER_UNIT == 8
837 ? 3 : exact_log2 (BITS_PER_UNIT),
838 HOST_BITS_PER_DOUBLE_INT);
839 if (moff.is_negative ())
840 doffset1 -= (-moff).low;
841 else
842 doffset2 -= moff.low;
845 /* If either reference is view-converted, give up now. */
846 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
847 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
848 return true;
850 /* If both references are through the same type, they do not alias
851 if the accesses do not overlap. This does extra disambiguation
852 for mixed/pointer accesses but requires strict aliasing.
853 For MEM_REFs we require that the component-ref offset we computed
854 is relative to the start of the type which we ensure by
855 comparing rvalue and access type and disregarding the constant
856 pointer offset. */
857 if ((TREE_CODE (base1) != TARGET_MEM_REF
858 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
859 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
860 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
862 /* Do access-path based disambiguation. */
863 if (ref1 && ref2
864 && (handled_component_p (ref1) || handled_component_p (ref2)))
865 return aliasing_component_refs_p (ref1,
866 ref1_alias_set, base1_alias_set,
867 offset1, max_size1,
868 ref2,
869 ref2_alias_set, base2_alias_set,
870 offset2, max_size2, true);
872 return true;
875 /* Return true if two indirect references based on *PTR1
876 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
877 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
878 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
879 in which case they are computed on-demand. REF1 and REF2
880 if non-NULL are the complete memory reference trees. */
882 static bool
883 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
884 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
885 alias_set_type ref1_alias_set,
886 alias_set_type base1_alias_set,
887 tree ref2 ATTRIBUTE_UNUSED, tree base2,
888 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
889 alias_set_type ref2_alias_set,
890 alias_set_type base2_alias_set, bool tbaa_p)
892 tree ptr1;
893 tree ptr2;
894 tree ptrtype1, ptrtype2;
896 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
897 || TREE_CODE (base1) == TARGET_MEM_REF)
898 && (TREE_CODE (base2) == MEM_REF
899 || TREE_CODE (base2) == TARGET_MEM_REF));
901 ptr1 = TREE_OPERAND (base1, 0);
902 ptr2 = TREE_OPERAND (base2, 0);
904 /* If both bases are based on pointers they cannot alias if they may not
905 point to the same memory object or if they point to the same object
906 and the accesses do not overlap. */
907 if ((!cfun || gimple_in_ssa_p (cfun))
908 && operand_equal_p (ptr1, ptr2, 0)
909 && (((TREE_CODE (base1) != TARGET_MEM_REF
910 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
911 && (TREE_CODE (base2) != TARGET_MEM_REF
912 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
913 || (TREE_CODE (base1) == TARGET_MEM_REF
914 && TREE_CODE (base2) == TARGET_MEM_REF
915 && (TMR_STEP (base1) == TMR_STEP (base2)
916 || (TMR_STEP (base1) && TMR_STEP (base2)
917 && operand_equal_p (TMR_STEP (base1),
918 TMR_STEP (base2), 0)))
919 && (TMR_INDEX (base1) == TMR_INDEX (base2)
920 || (TMR_INDEX (base1) && TMR_INDEX (base2)
921 && operand_equal_p (TMR_INDEX (base1),
922 TMR_INDEX (base2), 0)))
923 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
924 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
925 && operand_equal_p (TMR_INDEX2 (base1),
926 TMR_INDEX2 (base2), 0))))))
928 double_int moff;
929 /* The offset embedded in MEM_REFs can be negative. Bias them
930 so that the resulting offset adjustment is positive. */
931 moff = mem_ref_offset (base1);
932 moff = moff.alshift (BITS_PER_UNIT == 8
933 ? 3 : exact_log2 (BITS_PER_UNIT),
934 HOST_BITS_PER_DOUBLE_INT);
935 if (moff.is_negative ())
936 offset2 += (-moff).low;
937 else
938 offset1 += moff.low;
939 moff = mem_ref_offset (base2);
940 moff = moff.alshift (BITS_PER_UNIT == 8
941 ? 3 : exact_log2 (BITS_PER_UNIT),
942 HOST_BITS_PER_DOUBLE_INT);
943 if (moff.is_negative ())
944 offset1 += (-moff).low;
945 else
946 offset2 += moff.low;
947 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
949 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
950 return false;
952 /* Disambiguations that rely on strict aliasing rules follow. */
953 if (!flag_strict_aliasing || !tbaa_p)
954 return true;
956 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
957 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
959 /* If the alias set for a pointer access is zero all bets are off. */
960 if (base1_alias_set == -1)
961 base1_alias_set = get_deref_alias_set (ptrtype1);
962 if (base1_alias_set == 0)
963 return true;
964 if (base2_alias_set == -1)
965 base2_alias_set = get_deref_alias_set (ptrtype2);
966 if (base2_alias_set == 0)
967 return true;
969 /* If both references are through the same type, they do not alias
970 if the accesses do not overlap. This does extra disambiguation
971 for mixed/pointer accesses but requires strict aliasing. */
972 if ((TREE_CODE (base1) != TARGET_MEM_REF
973 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
974 && (TREE_CODE (base2) != TARGET_MEM_REF
975 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
976 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
977 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
978 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
979 TREE_TYPE (ptrtype2)) == 1)
980 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
982 /* Do type-based disambiguation. */
983 if (base1_alias_set != base2_alias_set
984 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
985 return false;
987 /* Do access-path based disambiguation. */
988 if (ref1 && ref2
989 && (handled_component_p (ref1) || handled_component_p (ref2))
990 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
991 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
992 return aliasing_component_refs_p (ref1,
993 ref1_alias_set, base1_alias_set,
994 offset1, max_size1,
995 ref2,
996 ref2_alias_set, base2_alias_set,
997 offset2, max_size2, false);
999 return true;
1002 /* Return true, if the two memory references REF1 and REF2 may alias. */
1004 bool
1005 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1007 tree base1, base2;
1008 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1009 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1010 bool var1_p, var2_p, ind1_p, ind2_p;
1012 gcc_checking_assert ((!ref1->ref
1013 || TREE_CODE (ref1->ref) == SSA_NAME
1014 || DECL_P (ref1->ref)
1015 || TREE_CODE (ref1->ref) == STRING_CST
1016 || handled_component_p (ref1->ref)
1017 || TREE_CODE (ref1->ref) == MEM_REF
1018 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1019 && (!ref2->ref
1020 || TREE_CODE (ref2->ref) == SSA_NAME
1021 || DECL_P (ref2->ref)
1022 || TREE_CODE (ref2->ref) == STRING_CST
1023 || handled_component_p (ref2->ref)
1024 || TREE_CODE (ref2->ref) == MEM_REF
1025 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1027 /* Decompose the references into their base objects and the access. */
1028 base1 = ao_ref_base (ref1);
1029 offset1 = ref1->offset;
1030 max_size1 = ref1->max_size;
1031 base2 = ao_ref_base (ref2);
1032 offset2 = ref2->offset;
1033 max_size2 = ref2->max_size;
1035 /* We can end up with registers or constants as bases for example from
1036 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1037 which is seen as a struct copy. */
1038 if (TREE_CODE (base1) == SSA_NAME
1039 || TREE_CODE (base1) == CONST_DECL
1040 || TREE_CODE (base1) == CONSTRUCTOR
1041 || TREE_CODE (base1) == ADDR_EXPR
1042 || CONSTANT_CLASS_P (base1)
1043 || TREE_CODE (base2) == SSA_NAME
1044 || TREE_CODE (base2) == CONST_DECL
1045 || TREE_CODE (base2) == CONSTRUCTOR
1046 || TREE_CODE (base2) == ADDR_EXPR
1047 || CONSTANT_CLASS_P (base2))
1048 return false;
1050 /* We can end up referring to code via function and label decls.
1051 As we likely do not properly track code aliases conservatively
1052 bail out. */
1053 if (TREE_CODE (base1) == FUNCTION_DECL
1054 || TREE_CODE (base1) == LABEL_DECL
1055 || TREE_CODE (base2) == FUNCTION_DECL
1056 || TREE_CODE (base2) == LABEL_DECL)
1057 return true;
1059 /* Two volatile accesses always conflict. */
1060 if (ref1->volatile_p
1061 && ref2->volatile_p)
1062 return true;
1064 /* Defer to simple offset based disambiguation if we have
1065 references based on two decls. Do this before defering to
1066 TBAA to handle must-alias cases in conformance with the
1067 GCC extension of allowing type-punning through unions. */
1068 var1_p = DECL_P (base1);
1069 var2_p = DECL_P (base2);
1070 if (var1_p && var2_p)
1071 return decl_refs_may_alias_p (base1, offset1, max_size1,
1072 base2, offset2, max_size2);
1074 ind1_p = (TREE_CODE (base1) == MEM_REF
1075 || TREE_CODE (base1) == TARGET_MEM_REF);
1076 ind2_p = (TREE_CODE (base2) == MEM_REF
1077 || TREE_CODE (base2) == TARGET_MEM_REF);
1079 /* Canonicalize the pointer-vs-decl case. */
1080 if (ind1_p && var2_p)
1082 HOST_WIDE_INT tmp1;
1083 tree tmp2;
1084 ao_ref *tmp3;
1085 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1086 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1087 tmp2 = base1; base1 = base2; base2 = tmp2;
1088 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1089 var1_p = true;
1090 ind1_p = false;
1091 var2_p = false;
1092 ind2_p = true;
1095 /* First defer to TBAA if possible. */
1096 if (tbaa_p
1097 && flag_strict_aliasing
1098 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1099 ao_ref_alias_set (ref2)))
1100 return false;
1102 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1103 if (var1_p && ind2_p)
1104 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1105 offset2, max_size2,
1106 ao_ref_alias_set (ref2), -1,
1107 ref1->ref, base1,
1108 offset1, max_size1,
1109 ao_ref_alias_set (ref1),
1110 ao_ref_base_alias_set (ref1),
1111 tbaa_p);
1112 else if (ind1_p && ind2_p)
1113 return indirect_refs_may_alias_p (ref1->ref, base1,
1114 offset1, max_size1,
1115 ao_ref_alias_set (ref1), -1,
1116 ref2->ref, base2,
1117 offset2, max_size2,
1118 ao_ref_alias_set (ref2), -1,
1119 tbaa_p);
1121 /* We really do not want to end up here, but returning true is safe. */
1122 #ifdef ENABLE_CHECKING
1123 gcc_unreachable ();
1124 #else
1125 return true;
1126 #endif
1129 bool
1130 refs_may_alias_p (tree ref1, tree ref2)
1132 ao_ref r1, r2;
1133 bool res;
1134 ao_ref_init (&r1, ref1);
1135 ao_ref_init (&r2, ref2);
1136 res = refs_may_alias_p_1 (&r1, &r2, true);
1137 if (res)
1138 ++alias_stats.refs_may_alias_p_may_alias;
1139 else
1140 ++alias_stats.refs_may_alias_p_no_alias;
1141 return res;
1144 /* Returns true if there is a anti-dependence for the STORE that
1145 executes after the LOAD. */
1147 bool
1148 refs_anti_dependent_p (tree load, tree store)
1150 ao_ref r1, r2;
1151 ao_ref_init (&r1, load);
1152 ao_ref_init (&r2, store);
1153 return refs_may_alias_p_1 (&r1, &r2, false);
1156 /* Returns true if there is a output dependence for the stores
1157 STORE1 and STORE2. */
1159 bool
1160 refs_output_dependent_p (tree store1, tree store2)
1162 ao_ref r1, r2;
1163 ao_ref_init (&r1, store1);
1164 ao_ref_init (&r2, store2);
1165 return refs_may_alias_p_1 (&r1, &r2, false);
1168 /* If the call CALL may use the memory reference REF return true,
1169 otherwise return false. */
1171 static bool
1172 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1174 tree base, callee;
1175 unsigned i;
1176 int flags = gimple_call_flags (call);
1178 /* Const functions without a static chain do not implicitly use memory. */
1179 if (!gimple_call_chain (call)
1180 && (flags & (ECF_CONST|ECF_NOVOPS)))
1181 goto process_args;
1183 base = ao_ref_base (ref);
1184 if (!base)
1185 return true;
1187 /* A call that is not without side-effects might involve volatile
1188 accesses and thus conflicts with all other volatile accesses. */
1189 if (ref->volatile_p)
1190 return true;
1192 /* If the reference is based on a decl that is not aliased the call
1193 cannot possibly use it. */
1194 if (DECL_P (base)
1195 && !may_be_aliased (base)
1196 /* But local statics can be used through recursion. */
1197 && !is_global_var (base))
1198 goto process_args;
1200 callee = gimple_call_fndecl (call);
1202 /* Handle those builtin functions explicitly that do not act as
1203 escape points. See tree-ssa-structalias.c:find_func_aliases
1204 for the list of builtins we might need to handle here. */
1205 if (callee != NULL_TREE
1206 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1207 switch (DECL_FUNCTION_CODE (callee))
1209 /* All the following functions read memory pointed to by
1210 their second argument. strcat/strncat additionally
1211 reads memory pointed to by the first argument. */
1212 case BUILT_IN_STRCAT:
1213 case BUILT_IN_STRNCAT:
1215 ao_ref dref;
1216 ao_ref_init_from_ptr_and_size (&dref,
1217 gimple_call_arg (call, 0),
1218 NULL_TREE);
1219 if (refs_may_alias_p_1 (&dref, ref, false))
1220 return true;
1222 /* FALLTHRU */
1223 case BUILT_IN_STRCPY:
1224 case BUILT_IN_STRNCPY:
1225 case BUILT_IN_MEMCPY:
1226 case BUILT_IN_MEMMOVE:
1227 case BUILT_IN_MEMPCPY:
1228 case BUILT_IN_STPCPY:
1229 case BUILT_IN_STPNCPY:
1230 case BUILT_IN_TM_MEMCPY:
1231 case BUILT_IN_TM_MEMMOVE:
1233 ao_ref dref;
1234 tree size = NULL_TREE;
1235 if (gimple_call_num_args (call) == 3)
1236 size = gimple_call_arg (call, 2);
1237 ao_ref_init_from_ptr_and_size (&dref,
1238 gimple_call_arg (call, 1),
1239 size);
1240 return refs_may_alias_p_1 (&dref, ref, false);
1242 case BUILT_IN_STRCAT_CHK:
1243 case BUILT_IN_STRNCAT_CHK:
1245 ao_ref dref;
1246 ao_ref_init_from_ptr_and_size (&dref,
1247 gimple_call_arg (call, 0),
1248 NULL_TREE);
1249 if (refs_may_alias_p_1 (&dref, ref, false))
1250 return true;
1252 /* FALLTHRU */
1253 case BUILT_IN_STRCPY_CHK:
1254 case BUILT_IN_STRNCPY_CHK:
1255 case BUILT_IN_MEMCPY_CHK:
1256 case BUILT_IN_MEMMOVE_CHK:
1257 case BUILT_IN_MEMPCPY_CHK:
1258 case BUILT_IN_STPCPY_CHK:
1259 case BUILT_IN_STPNCPY_CHK:
1261 ao_ref dref;
1262 tree size = NULL_TREE;
1263 if (gimple_call_num_args (call) == 4)
1264 size = gimple_call_arg (call, 2);
1265 ao_ref_init_from_ptr_and_size (&dref,
1266 gimple_call_arg (call, 1),
1267 size);
1268 return refs_may_alias_p_1 (&dref, ref, false);
1270 case BUILT_IN_BCOPY:
1272 ao_ref dref;
1273 tree size = gimple_call_arg (call, 2);
1274 ao_ref_init_from_ptr_and_size (&dref,
1275 gimple_call_arg (call, 0),
1276 size);
1277 return refs_may_alias_p_1 (&dref, ref, false);
1280 /* The following functions read memory pointed to by their
1281 first argument. */
1282 CASE_BUILT_IN_TM_LOAD (1):
1283 CASE_BUILT_IN_TM_LOAD (2):
1284 CASE_BUILT_IN_TM_LOAD (4):
1285 CASE_BUILT_IN_TM_LOAD (8):
1286 CASE_BUILT_IN_TM_LOAD (FLOAT):
1287 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1288 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1289 CASE_BUILT_IN_TM_LOAD (M64):
1290 CASE_BUILT_IN_TM_LOAD (M128):
1291 CASE_BUILT_IN_TM_LOAD (M256):
1292 case BUILT_IN_TM_LOG:
1293 case BUILT_IN_TM_LOG_1:
1294 case BUILT_IN_TM_LOG_2:
1295 case BUILT_IN_TM_LOG_4:
1296 case BUILT_IN_TM_LOG_8:
1297 case BUILT_IN_TM_LOG_FLOAT:
1298 case BUILT_IN_TM_LOG_DOUBLE:
1299 case BUILT_IN_TM_LOG_LDOUBLE:
1300 case BUILT_IN_TM_LOG_M64:
1301 case BUILT_IN_TM_LOG_M128:
1302 case BUILT_IN_TM_LOG_M256:
1303 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1305 /* These read memory pointed to by the first argument. */
1306 case BUILT_IN_STRDUP:
1307 case BUILT_IN_STRNDUP:
1309 ao_ref dref;
1310 tree size = NULL_TREE;
1311 if (gimple_call_num_args (call) == 2)
1312 size = gimple_call_arg (call, 1);
1313 ao_ref_init_from_ptr_and_size (&dref,
1314 gimple_call_arg (call, 0),
1315 size);
1316 return refs_may_alias_p_1 (&dref, ref, false);
1318 /* The following builtins do not read from memory. */
1319 case BUILT_IN_FREE:
1320 case BUILT_IN_MALLOC:
1321 case BUILT_IN_CALLOC:
1322 case BUILT_IN_ALLOCA:
1323 case BUILT_IN_ALLOCA_WITH_ALIGN:
1324 case BUILT_IN_STACK_SAVE:
1325 case BUILT_IN_STACK_RESTORE:
1326 case BUILT_IN_MEMSET:
1327 case BUILT_IN_TM_MEMSET:
1328 case BUILT_IN_MEMSET_CHK:
1329 case BUILT_IN_FREXP:
1330 case BUILT_IN_FREXPF:
1331 case BUILT_IN_FREXPL:
1332 case BUILT_IN_GAMMA_R:
1333 case BUILT_IN_GAMMAF_R:
1334 case BUILT_IN_GAMMAL_R:
1335 case BUILT_IN_LGAMMA_R:
1336 case BUILT_IN_LGAMMAF_R:
1337 case BUILT_IN_LGAMMAL_R:
1338 case BUILT_IN_MODF:
1339 case BUILT_IN_MODFF:
1340 case BUILT_IN_MODFL:
1341 case BUILT_IN_REMQUO:
1342 case BUILT_IN_REMQUOF:
1343 case BUILT_IN_REMQUOL:
1344 case BUILT_IN_SINCOS:
1345 case BUILT_IN_SINCOSF:
1346 case BUILT_IN_SINCOSL:
1347 case BUILT_IN_ASSUME_ALIGNED:
1348 case BUILT_IN_VA_END:
1349 return false;
1350 /* __sync_* builtins and some OpenMP builtins act as threading
1351 barriers. */
1352 #undef DEF_SYNC_BUILTIN
1353 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1354 #include "sync-builtins.def"
1355 #undef DEF_SYNC_BUILTIN
1356 case BUILT_IN_GOMP_ATOMIC_START:
1357 case BUILT_IN_GOMP_ATOMIC_END:
1358 case BUILT_IN_GOMP_BARRIER:
1359 case BUILT_IN_GOMP_TASKWAIT:
1360 case BUILT_IN_GOMP_CRITICAL_START:
1361 case BUILT_IN_GOMP_CRITICAL_END:
1362 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1363 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1364 case BUILT_IN_GOMP_LOOP_END:
1365 case BUILT_IN_GOMP_ORDERED_START:
1366 case BUILT_IN_GOMP_ORDERED_END:
1367 case BUILT_IN_GOMP_PARALLEL_END:
1368 case BUILT_IN_GOMP_SECTIONS_END:
1369 case BUILT_IN_GOMP_SINGLE_COPY_START:
1370 case BUILT_IN_GOMP_SINGLE_COPY_END:
1371 return true;
1373 default:
1374 /* Fallthru to general call handling. */;
1377 /* Check if base is a global static variable that is not read
1378 by the function. */
1379 if (callee != NULL_TREE
1380 && TREE_CODE (base) == VAR_DECL
1381 && TREE_STATIC (base))
1383 struct cgraph_node *node = cgraph_get_node (callee);
1384 bitmap not_read;
1386 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1387 node yet. We should enforce that there are nodes for all decls in the
1388 IL and remove this check instead. */
1389 if (node
1390 && (not_read = ipa_reference_get_not_read_global (node))
1391 && bitmap_bit_p (not_read, DECL_UID (base)))
1392 goto process_args;
1395 /* Check if the base variable is call-used. */
1396 if (DECL_P (base))
1398 if (pt_solution_includes (gimple_call_use_set (call), base))
1399 return true;
1401 else if ((TREE_CODE (base) == MEM_REF
1402 || TREE_CODE (base) == TARGET_MEM_REF)
1403 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1405 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1406 if (!pi)
1407 return true;
1409 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1410 return true;
1412 else
1413 return true;
1415 /* Inspect call arguments for passed-by-value aliases. */
1416 process_args:
1417 for (i = 0; i < gimple_call_num_args (call); ++i)
1419 tree op = gimple_call_arg (call, i);
1420 int flags = gimple_call_arg_flags (call, i);
1422 if (flags & EAF_UNUSED)
1423 continue;
1425 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1426 op = TREE_OPERAND (op, 0);
1428 if (TREE_CODE (op) != SSA_NAME
1429 && !is_gimple_min_invariant (op))
1431 ao_ref r;
1432 ao_ref_init (&r, op);
1433 if (refs_may_alias_p_1 (&r, ref, true))
1434 return true;
1438 return false;
1441 static bool
1442 ref_maybe_used_by_call_p (gimple call, tree ref)
1444 ao_ref r;
1445 bool res;
1446 ao_ref_init (&r, ref);
1447 res = ref_maybe_used_by_call_p_1 (call, &r);
1448 if (res)
1449 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1450 else
1451 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1452 return res;
1456 /* If the statement STMT may use the memory reference REF return
1457 true, otherwise return false. */
1459 bool
1460 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1462 if (is_gimple_assign (stmt))
1464 tree rhs;
1466 /* All memory assign statements are single. */
1467 if (!gimple_assign_single_p (stmt))
1468 return false;
1470 rhs = gimple_assign_rhs1 (stmt);
1471 if (is_gimple_reg (rhs)
1472 || is_gimple_min_invariant (rhs)
1473 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1474 return false;
1476 return refs_may_alias_p (rhs, ref);
1478 else if (is_gimple_call (stmt))
1479 return ref_maybe_used_by_call_p (stmt, ref);
1480 else if (gimple_code (stmt) == GIMPLE_RETURN)
1482 tree retval = gimple_return_retval (stmt);
1483 tree base;
1484 if (retval
1485 && TREE_CODE (retval) != SSA_NAME
1486 && !is_gimple_min_invariant (retval)
1487 && refs_may_alias_p (retval, ref))
1488 return true;
1489 /* If ref escapes the function then the return acts as a use. */
1490 base = get_base_address (ref);
1491 if (!base)
1493 else if (DECL_P (base))
1494 return is_global_var (base);
1495 else if (TREE_CODE (base) == MEM_REF
1496 || TREE_CODE (base) == TARGET_MEM_REF)
1497 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1498 return false;
1501 return true;
1504 /* If the call in statement CALL may clobber the memory reference REF
1505 return true, otherwise return false. */
1507 static bool
1508 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1510 tree base;
1511 tree callee;
1513 /* If the call is pure or const it cannot clobber anything. */
1514 if (gimple_call_flags (call)
1515 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1516 return false;
1518 base = ao_ref_base (ref);
1519 if (!base)
1520 return true;
1522 if (TREE_CODE (base) == SSA_NAME
1523 || CONSTANT_CLASS_P (base))
1524 return false;
1526 /* A call that is not without side-effects might involve volatile
1527 accesses and thus conflicts with all other volatile accesses. */
1528 if (ref->volatile_p)
1529 return true;
1531 /* If the reference is based on a decl that is not aliased the call
1532 cannot possibly clobber it. */
1533 if (DECL_P (base)
1534 && !may_be_aliased (base)
1535 /* But local non-readonly statics can be modified through recursion
1536 or the call may implement a threading barrier which we must
1537 treat as may-def. */
1538 && (TREE_READONLY (base)
1539 || !is_global_var (base)))
1540 return false;
1542 callee = gimple_call_fndecl (call);
1544 /* Handle those builtin functions explicitly that do not act as
1545 escape points. See tree-ssa-structalias.c:find_func_aliases
1546 for the list of builtins we might need to handle here. */
1547 if (callee != NULL_TREE
1548 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1549 switch (DECL_FUNCTION_CODE (callee))
1551 /* All the following functions clobber memory pointed to by
1552 their first argument. */
1553 case BUILT_IN_STRCPY:
1554 case BUILT_IN_STRNCPY:
1555 case BUILT_IN_MEMCPY:
1556 case BUILT_IN_MEMMOVE:
1557 case BUILT_IN_MEMPCPY:
1558 case BUILT_IN_STPCPY:
1559 case BUILT_IN_STPNCPY:
1560 case BUILT_IN_STRCAT:
1561 case BUILT_IN_STRNCAT:
1562 case BUILT_IN_MEMSET:
1563 case BUILT_IN_TM_MEMSET:
1564 CASE_BUILT_IN_TM_STORE (1):
1565 CASE_BUILT_IN_TM_STORE (2):
1566 CASE_BUILT_IN_TM_STORE (4):
1567 CASE_BUILT_IN_TM_STORE (8):
1568 CASE_BUILT_IN_TM_STORE (FLOAT):
1569 CASE_BUILT_IN_TM_STORE (DOUBLE):
1570 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1571 CASE_BUILT_IN_TM_STORE (M64):
1572 CASE_BUILT_IN_TM_STORE (M128):
1573 CASE_BUILT_IN_TM_STORE (M256):
1574 case BUILT_IN_TM_MEMCPY:
1575 case BUILT_IN_TM_MEMMOVE:
1577 ao_ref dref;
1578 tree size = NULL_TREE;
1579 /* Don't pass in size for strncat, as the maximum size
1580 is strlen (dest) + n + 1 instead of n, resp.
1581 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1582 known. */
1583 if (gimple_call_num_args (call) == 3
1584 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1585 size = gimple_call_arg (call, 2);
1586 ao_ref_init_from_ptr_and_size (&dref,
1587 gimple_call_arg (call, 0),
1588 size);
1589 return refs_may_alias_p_1 (&dref, ref, false);
1591 case BUILT_IN_STRCPY_CHK:
1592 case BUILT_IN_STRNCPY_CHK:
1593 case BUILT_IN_MEMCPY_CHK:
1594 case BUILT_IN_MEMMOVE_CHK:
1595 case BUILT_IN_MEMPCPY_CHK:
1596 case BUILT_IN_STPCPY_CHK:
1597 case BUILT_IN_STPNCPY_CHK:
1598 case BUILT_IN_STRCAT_CHK:
1599 case BUILT_IN_STRNCAT_CHK:
1600 case BUILT_IN_MEMSET_CHK:
1602 ao_ref dref;
1603 tree size = NULL_TREE;
1604 /* Don't pass in size for __strncat_chk, as the maximum size
1605 is strlen (dest) + n + 1 instead of n, resp.
1606 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1607 known. */
1608 if (gimple_call_num_args (call) == 4
1609 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1610 size = gimple_call_arg (call, 2);
1611 ao_ref_init_from_ptr_and_size (&dref,
1612 gimple_call_arg (call, 0),
1613 size);
1614 return refs_may_alias_p_1 (&dref, ref, false);
1616 case BUILT_IN_BCOPY:
1618 ao_ref dref;
1619 tree size = gimple_call_arg (call, 2);
1620 ao_ref_init_from_ptr_and_size (&dref,
1621 gimple_call_arg (call, 1),
1622 size);
1623 return refs_may_alias_p_1 (&dref, ref, false);
1625 /* Allocating memory does not have any side-effects apart from
1626 being the definition point for the pointer. */
1627 case BUILT_IN_MALLOC:
1628 case BUILT_IN_CALLOC:
1629 case BUILT_IN_STRDUP:
1630 case BUILT_IN_STRNDUP:
1631 /* Unix98 specifies that errno is set on allocation failure. */
1632 if (flag_errno_math
1633 && targetm.ref_may_alias_errno (ref))
1634 return true;
1635 return false;
1636 case BUILT_IN_STACK_SAVE:
1637 case BUILT_IN_ALLOCA:
1638 case BUILT_IN_ALLOCA_WITH_ALIGN:
1639 case BUILT_IN_ASSUME_ALIGNED:
1640 return false;
1641 /* Freeing memory kills the pointed-to memory. More importantly
1642 the call has to serve as a barrier for moving loads and stores
1643 across it. */
1644 case BUILT_IN_FREE:
1645 case BUILT_IN_VA_END:
1647 tree ptr = gimple_call_arg (call, 0);
1648 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1650 case BUILT_IN_GAMMA_R:
1651 case BUILT_IN_GAMMAF_R:
1652 case BUILT_IN_GAMMAL_R:
1653 case BUILT_IN_LGAMMA_R:
1654 case BUILT_IN_LGAMMAF_R:
1655 case BUILT_IN_LGAMMAL_R:
1657 tree out = gimple_call_arg (call, 1);
1658 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1659 return true;
1660 if (flag_errno_math)
1661 break;
1662 return false;
1664 case BUILT_IN_FREXP:
1665 case BUILT_IN_FREXPF:
1666 case BUILT_IN_FREXPL:
1667 case BUILT_IN_MODF:
1668 case BUILT_IN_MODFF:
1669 case BUILT_IN_MODFL:
1671 tree out = gimple_call_arg (call, 1);
1672 return ptr_deref_may_alias_ref_p_1 (out, ref);
1674 case BUILT_IN_REMQUO:
1675 case BUILT_IN_REMQUOF:
1676 case BUILT_IN_REMQUOL:
1678 tree out = gimple_call_arg (call, 2);
1679 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1680 return true;
1681 if (flag_errno_math)
1682 break;
1683 return false;
1685 case BUILT_IN_SINCOS:
1686 case BUILT_IN_SINCOSF:
1687 case BUILT_IN_SINCOSL:
1689 tree sin = gimple_call_arg (call, 1);
1690 tree cos = gimple_call_arg (call, 2);
1691 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1692 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1694 /* __sync_* builtins and some OpenMP builtins act as threading
1695 barriers. */
1696 #undef DEF_SYNC_BUILTIN
1697 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1698 #include "sync-builtins.def"
1699 #undef DEF_SYNC_BUILTIN
1700 case BUILT_IN_GOMP_ATOMIC_START:
1701 case BUILT_IN_GOMP_ATOMIC_END:
1702 case BUILT_IN_GOMP_BARRIER:
1703 case BUILT_IN_GOMP_TASKWAIT:
1704 case BUILT_IN_GOMP_CRITICAL_START:
1705 case BUILT_IN_GOMP_CRITICAL_END:
1706 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1707 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1708 case BUILT_IN_GOMP_LOOP_END:
1709 case BUILT_IN_GOMP_ORDERED_START:
1710 case BUILT_IN_GOMP_ORDERED_END:
1711 case BUILT_IN_GOMP_PARALLEL_END:
1712 case BUILT_IN_GOMP_SECTIONS_END:
1713 case BUILT_IN_GOMP_SINGLE_COPY_START:
1714 case BUILT_IN_GOMP_SINGLE_COPY_END:
1715 return true;
1716 default:
1717 /* Fallthru to general call handling. */;
1720 /* Check if base is a global static variable that is not written
1721 by the function. */
1722 if (callee != NULL_TREE
1723 && TREE_CODE (base) == VAR_DECL
1724 && TREE_STATIC (base))
1726 struct cgraph_node *node = cgraph_get_node (callee);
1727 bitmap not_written;
1729 if (node
1730 && (not_written = ipa_reference_get_not_written_global (node))
1731 && bitmap_bit_p (not_written, DECL_UID (base)))
1732 return false;
1735 /* Check if the base variable is call-clobbered. */
1736 if (DECL_P (base))
1737 return pt_solution_includes (gimple_call_clobber_set (call), base);
1738 else if ((TREE_CODE (base) == MEM_REF
1739 || TREE_CODE (base) == TARGET_MEM_REF)
1740 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1742 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1743 if (!pi)
1744 return true;
1746 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1749 return true;
1752 /* If the call in statement CALL may clobber the memory reference REF
1753 return true, otherwise return false. */
1755 bool
1756 call_may_clobber_ref_p (gimple call, tree ref)
1758 bool res;
1759 ao_ref r;
1760 ao_ref_init (&r, ref);
1761 res = call_may_clobber_ref_p_1 (call, &r);
1762 if (res)
1763 ++alias_stats.call_may_clobber_ref_p_may_alias;
1764 else
1765 ++alias_stats.call_may_clobber_ref_p_no_alias;
1766 return res;
1770 /* If the statement STMT may clobber the memory reference REF return true,
1771 otherwise return false. */
1773 bool
1774 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1776 if (is_gimple_call (stmt))
1778 tree lhs = gimple_call_lhs (stmt);
1779 if (lhs
1780 && TREE_CODE (lhs) != SSA_NAME)
1782 ao_ref r;
1783 ao_ref_init (&r, lhs);
1784 if (refs_may_alias_p_1 (ref, &r, true))
1785 return true;
1788 return call_may_clobber_ref_p_1 (stmt, ref);
1790 else if (gimple_assign_single_p (stmt))
1792 tree lhs = gimple_assign_lhs (stmt);
1793 if (TREE_CODE (lhs) != SSA_NAME)
1795 ao_ref r;
1796 ao_ref_init (&r, lhs);
1797 return refs_may_alias_p_1 (ref, &r, true);
1800 else if (gimple_code (stmt) == GIMPLE_ASM)
1801 return true;
1803 return false;
1806 bool
1807 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1809 ao_ref r;
1810 ao_ref_init (&r, ref);
1811 return stmt_may_clobber_ref_p_1 (stmt, &r);
1814 /* If STMT kills the memory reference REF return true, otherwise
1815 return false. */
1817 static bool
1818 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1820 /* For a must-alias check we need to be able to constrain
1821 the access properly. */
1822 ao_ref_base (ref);
1823 if (ref->max_size == -1)
1824 return false;
1826 if (gimple_has_lhs (stmt)
1827 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1828 /* The assignment is not necessarily carried out if it can throw
1829 and we can catch it in the current function where we could inspect
1830 the previous value.
1831 ??? We only need to care about the RHS throwing. For aggregate
1832 assignments or similar calls and non-call exceptions the LHS
1833 might throw as well. */
1834 && !stmt_can_throw_internal (stmt))
1836 tree base, lhs = gimple_get_lhs (stmt);
1837 HOST_WIDE_INT size, offset, max_size;
1838 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1839 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1840 so base == ref->base does not always hold. */
1841 if (base == ref->base)
1843 /* For a must-alias check we need to be able to constrain
1844 the access properly. */
1845 if (size != -1 && size == max_size)
1847 if (offset <= ref->offset
1848 && offset + size >= ref->offset + ref->max_size)
1849 return true;
1854 if (is_gimple_call (stmt))
1856 tree callee = gimple_call_fndecl (stmt);
1857 if (callee != NULL_TREE
1858 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1859 switch (DECL_FUNCTION_CODE (callee))
1861 case BUILT_IN_MEMCPY:
1862 case BUILT_IN_MEMPCPY:
1863 case BUILT_IN_MEMMOVE:
1864 case BUILT_IN_MEMSET:
1865 case BUILT_IN_MEMCPY_CHK:
1866 case BUILT_IN_MEMPCPY_CHK:
1867 case BUILT_IN_MEMMOVE_CHK:
1868 case BUILT_IN_MEMSET_CHK:
1870 tree dest = gimple_call_arg (stmt, 0);
1871 tree len = gimple_call_arg (stmt, 2);
1872 tree base = NULL_TREE;
1873 HOST_WIDE_INT offset = 0;
1874 if (!host_integerp (len, 0))
1875 return false;
1876 if (TREE_CODE (dest) == ADDR_EXPR)
1877 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1878 &offset);
1879 else if (TREE_CODE (dest) == SSA_NAME)
1880 base = dest;
1881 if (base
1882 && base == ao_ref_base (ref))
1884 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1885 if (offset <= ref->offset / BITS_PER_UNIT
1886 && (offset + size
1887 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1888 / BITS_PER_UNIT)))
1889 return true;
1891 break;
1894 case BUILT_IN_VA_END:
1896 tree ptr = gimple_call_arg (stmt, 0);
1897 if (TREE_CODE (ptr) == ADDR_EXPR)
1899 tree base = ao_ref_base (ref);
1900 if (TREE_OPERAND (ptr, 0) == base)
1901 return true;
1903 break;
1906 default:;
1909 return false;
1912 bool
1913 stmt_kills_ref_p (gimple stmt, tree ref)
1915 ao_ref r;
1916 ao_ref_init (&r, ref);
1917 return stmt_kills_ref_p_1 (stmt, &r);
1921 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1922 TARGET or a statement clobbering the memory reference REF in which
1923 case false is returned. The walk starts with VUSE, one argument of PHI. */
1925 static bool
1926 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1927 tree vuse, unsigned int *cnt, bitmap *visited,
1928 bool abort_on_visited)
1930 basic_block bb = gimple_bb (phi);
1932 if (!*visited)
1933 *visited = BITMAP_ALLOC (NULL);
1935 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1937 /* Walk until we hit the target. */
1938 while (vuse != target)
1940 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1941 /* Recurse for PHI nodes. */
1942 if (gimple_code (def_stmt) == GIMPLE_PHI)
1944 /* An already visited PHI node ends the walk successfully. */
1945 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1946 return !abort_on_visited;
1947 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
1948 visited, abort_on_visited);
1949 if (!vuse)
1950 return false;
1951 continue;
1953 else if (gimple_nop_p (def_stmt))
1954 return false;
1955 else
1957 /* A clobbering statement or the end of the IL ends it failing. */
1958 ++*cnt;
1959 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1960 return false;
1962 /* If we reach a new basic-block see if we already skipped it
1963 in a previous walk that ended successfully. */
1964 if (gimple_bb (def_stmt) != bb)
1966 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
1967 return !abort_on_visited;
1968 bb = gimple_bb (def_stmt);
1970 vuse = gimple_vuse (def_stmt);
1972 return true;
1975 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
1976 until we hit the phi argument definition that dominates the other one.
1977 Return that, or NULL_TREE if there is no such definition. */
1979 static tree
1980 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
1981 ao_ref *ref, unsigned int *cnt,
1982 bitmap *visited, bool abort_on_visited)
1984 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1985 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1986 tree common_vuse;
1988 if (arg0 == arg1)
1989 return arg0;
1990 else if (gimple_nop_p (def0)
1991 || (!gimple_nop_p (def1)
1992 && dominated_by_p (CDI_DOMINATORS,
1993 gimple_bb (def1), gimple_bb (def0))))
1995 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
1996 visited, abort_on_visited))
1997 return arg0;
1999 else if (gimple_nop_p (def1)
2000 || dominated_by_p (CDI_DOMINATORS,
2001 gimple_bb (def0), gimple_bb (def1)))
2003 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2004 visited, abort_on_visited))
2005 return arg1;
2007 /* Special case of a diamond:
2008 MEM_1 = ...
2009 goto (cond) ? L1 : L2
2010 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2011 goto L3
2012 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2013 L3: MEM_4 = PHI<MEM_2, MEM_3>
2014 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2015 dominate each other, but still we can easily skip this PHI node
2016 if we recognize that the vuse MEM operand is the same for both,
2017 and that we can skip both statements (they don't clobber us).
2018 This is still linear. Don't use maybe_skip_until, that might
2019 potentially be slow. */
2020 else if ((common_vuse = gimple_vuse (def0))
2021 && common_vuse == gimple_vuse (def1))
2023 *cnt += 2;
2024 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2025 && !stmt_may_clobber_ref_p_1 (def1, ref))
2026 return common_vuse;
2029 return NULL_TREE;
2033 /* Starting from a PHI node for the virtual operand of the memory reference
2034 REF find a continuation virtual operand that allows to continue walking
2035 statements dominating PHI skipping only statements that cannot possibly
2036 clobber REF. Increments *CNT for each alias disambiguation done.
2037 Returns NULL_TREE if no suitable virtual operand can be found. */
2039 tree
2040 get_continuation_for_phi (gimple phi, ao_ref *ref,
2041 unsigned int *cnt, bitmap *visited,
2042 bool abort_on_visited)
2044 unsigned nargs = gimple_phi_num_args (phi);
2046 /* Through a single-argument PHI we can simply look through. */
2047 if (nargs == 1)
2048 return PHI_ARG_DEF (phi, 0);
2050 /* For two or more arguments try to pairwise skip non-aliasing code
2051 until we hit the phi argument definition that dominates the other one. */
2052 else if (nargs >= 2)
2054 tree arg0, arg1;
2055 unsigned i;
2057 /* Find a candidate for the virtual operand which definition
2058 dominates those of all others. */
2059 arg0 = PHI_ARG_DEF (phi, 0);
2060 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2061 for (i = 1; i < nargs; ++i)
2063 arg1 = PHI_ARG_DEF (phi, i);
2064 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2066 arg0 = arg1;
2067 break;
2069 if (dominated_by_p (CDI_DOMINATORS,
2070 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2071 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2072 arg0 = arg1;
2075 /* Then pairwise reduce against the found candidate. */
2076 for (i = 0; i < nargs; ++i)
2078 arg1 = PHI_ARG_DEF (phi, i);
2079 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2080 cnt, visited, abort_on_visited);
2081 if (!arg0)
2082 return NULL_TREE;
2085 return arg0;
2088 return NULL_TREE;
2091 /* Based on the memory reference REF and its virtual use VUSE call
2092 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2093 itself. That is, for each virtual use for which its defining statement
2094 does not clobber REF.
2096 WALKER is called with REF, the current virtual use and DATA. If
2097 WALKER returns non-NULL the walk stops and its result is returned.
2098 At the end of a non-successful walk NULL is returned.
2100 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2101 use which definition is a statement that may clobber REF and DATA.
2102 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2103 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2104 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2105 to adjust REF and *DATA to make that valid.
2107 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2109 void *
2110 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2111 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2112 void *(*translate)(ao_ref *, tree, void *), void *data)
2114 bitmap visited = NULL;
2115 void *res;
2116 unsigned int cnt = 0;
2117 bool translated = false;
2119 timevar_push (TV_ALIAS_STMT_WALK);
2123 gimple def_stmt;
2125 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2126 res = (*walker) (ref, vuse, cnt, data);
2127 /* Abort walk. */
2128 if (res == (void *)-1)
2130 res = NULL;
2131 break;
2133 /* Lookup succeeded. */
2134 else if (res != NULL)
2135 break;
2137 def_stmt = SSA_NAME_DEF_STMT (vuse);
2138 if (gimple_nop_p (def_stmt))
2139 break;
2140 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2141 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2142 &visited, translated);
2143 else
2145 cnt++;
2146 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2148 if (!translate)
2149 break;
2150 res = (*translate) (ref, vuse, data);
2151 /* Failed lookup and translation. */
2152 if (res == (void *)-1)
2154 res = NULL;
2155 break;
2157 /* Lookup succeeded. */
2158 else if (res != NULL)
2159 break;
2160 /* Translation succeeded, continue walking. */
2161 translated = true;
2163 vuse = gimple_vuse (def_stmt);
2166 while (vuse);
2168 if (visited)
2169 BITMAP_FREE (visited);
2171 timevar_pop (TV_ALIAS_STMT_WALK);
2173 return res;
2177 /* Based on the memory reference REF call WALKER for each vdef which
2178 defining statement may clobber REF, starting with VDEF. If REF
2179 is NULL_TREE, each defining statement is visited.
2181 WALKER is called with REF, the current vdef and DATA. If WALKER
2182 returns true the walk is stopped, otherwise it continues.
2184 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2185 PHI argument (but only one walk continues on merge points), the
2186 return value is true if any of the walks was successful.
2188 The function returns the number of statements walked. */
2190 static unsigned int
2191 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2192 bool (*walker)(ao_ref *, tree, void *), void *data,
2193 bitmap *visited, unsigned int cnt)
2197 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2199 if (*visited
2200 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2201 return cnt;
2203 if (gimple_nop_p (def_stmt))
2204 return cnt;
2205 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2207 unsigned i;
2208 if (!*visited)
2209 *visited = BITMAP_ALLOC (NULL);
2210 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2211 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2212 walker, data, visited, 0);
2213 return cnt;
2216 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2217 cnt++;
2218 if ((!ref
2219 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2220 && (*walker) (ref, vdef, data))
2221 return cnt;
2223 vdef = gimple_vuse (def_stmt);
2225 while (1);
2228 unsigned int
2229 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2230 bool (*walker)(ao_ref *, tree, void *), void *data,
2231 bitmap *visited)
2233 bitmap local_visited = NULL;
2234 unsigned int ret;
2236 timevar_push (TV_ALIAS_STMT_WALK);
2238 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2239 visited ? visited : &local_visited, 0);
2240 if (local_visited)
2241 BITMAP_FREE (local_visited);
2243 timevar_pop (TV_ALIAS_STMT_WALK);
2245 return ret;