* inclhack.def (AAB_aix_fcntl): New fix.
[official-gcc.git] / gcc / tree-ssa-alias.c
blobb045da27eec711f28cb8f3d9a381c9bf11ec15ee
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 = moff.alshift (BITS_PER_UNIT == 8
760 ? 3 : exact_log2 (BITS_PER_UNIT),
761 HOST_BITS_PER_DOUBLE_INT);
762 if (moff.is_negative ())
763 offset2p += (-moff).low;
764 else
765 offset1p += moff.low;
767 /* If only one reference is based on a variable, they cannot alias if
768 the pointer access is beyond the extent of the variable access.
769 (the pointer base cannot validly point to an offset less than zero
770 of the variable).
771 ??? IVOPTs creates bases that do not honor this restriction,
772 so do not apply this optimization for TARGET_MEM_REFs. */
773 if (TREE_CODE (base1) != TARGET_MEM_REF
774 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
775 return false;
776 /* They also cannot alias if the pointer may not point to the decl. */
777 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
778 return false;
780 /* Disambiguations that rely on strict aliasing rules follow. */
781 if (!flag_strict_aliasing || !tbaa_p)
782 return true;
784 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
786 /* If the alias set for a pointer access is zero all bets are off. */
787 if (base1_alias_set == -1)
788 base1_alias_set = get_deref_alias_set (ptrtype1);
789 if (base1_alias_set == 0)
790 return true;
791 if (base2_alias_set == -1)
792 base2_alias_set = get_alias_set (base2);
794 /* When we are trying to disambiguate an access with a pointer dereference
795 as base versus one with a decl as base we can use both the size
796 of the decl and its dynamic type for extra disambiguation.
797 ??? We do not know anything about the dynamic type of the decl
798 other than that its alias-set contains base2_alias_set as a subset
799 which does not help us here. */
800 /* As we know nothing useful about the dynamic type of the decl just
801 use the usual conflict check rather than a subset test.
802 ??? We could introduce -fvery-strict-aliasing when the language
803 does not allow decls to have a dynamic type that differs from their
804 static type. Then we can check
805 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
806 if (base1_alias_set != base2_alias_set
807 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
808 return false;
809 /* If the size of the access relevant for TBAA through the pointer
810 is bigger than the size of the decl we can't possibly access the
811 decl via that pointer. */
812 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
813 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
814 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
815 /* ??? This in turn may run afoul when a decl of type T which is
816 a member of union type U is accessed through a pointer to
817 type U and sizeof T is smaller than sizeof U. */
818 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
819 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
820 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
821 return false;
823 if (!ref2)
824 return true;
826 /* If the decl is accessed via a MEM_REF, reconstruct the base
827 we can use for TBAA and an appropriately adjusted offset. */
828 dbase2 = ref2;
829 while (handled_component_p (dbase2))
830 dbase2 = TREE_OPERAND (dbase2, 0);
831 doffset1 = offset1;
832 doffset2 = offset2;
833 if (TREE_CODE (dbase2) == MEM_REF
834 || TREE_CODE (dbase2) == TARGET_MEM_REF)
836 double_int moff = mem_ref_offset (dbase2);
837 moff = moff.alshift (BITS_PER_UNIT == 8
838 ? 3 : exact_log2 (BITS_PER_UNIT),
839 HOST_BITS_PER_DOUBLE_INT);
840 if (moff.is_negative ())
841 doffset1 -= (-moff).low;
842 else
843 doffset2 -= moff.low;
846 /* If either reference is view-converted, give up now. */
847 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
848 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
849 return true;
851 /* If both references are through the same type, they do not alias
852 if the accesses do not overlap. This does extra disambiguation
853 for mixed/pointer accesses but requires strict aliasing.
854 For MEM_REFs we require that the component-ref offset we computed
855 is relative to the start of the type which we ensure by
856 comparing rvalue and access type and disregarding the constant
857 pointer offset. */
858 if ((TREE_CODE (base1) != TARGET_MEM_REF
859 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
860 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
861 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
863 /* Do access-path based disambiguation. */
864 if (ref1 && ref2
865 && (handled_component_p (ref1) || handled_component_p (ref2)))
866 return aliasing_component_refs_p (ref1,
867 ref1_alias_set, base1_alias_set,
868 offset1, max_size1,
869 ref2,
870 ref2_alias_set, base2_alias_set,
871 offset2, max_size2, true);
873 return true;
876 /* Return true if two indirect references based on *PTR1
877 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
878 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
879 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
880 in which case they are computed on-demand. REF1 and REF2
881 if non-NULL are the complete memory reference trees. */
883 static bool
884 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
885 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
886 alias_set_type ref1_alias_set,
887 alias_set_type base1_alias_set,
888 tree ref2 ATTRIBUTE_UNUSED, tree base2,
889 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
890 alias_set_type ref2_alias_set,
891 alias_set_type base2_alias_set, bool tbaa_p)
893 tree ptr1;
894 tree ptr2;
895 tree ptrtype1, ptrtype2;
897 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
898 || TREE_CODE (base1) == TARGET_MEM_REF)
899 && (TREE_CODE (base2) == MEM_REF
900 || TREE_CODE (base2) == TARGET_MEM_REF));
902 ptr1 = TREE_OPERAND (base1, 0);
903 ptr2 = TREE_OPERAND (base2, 0);
905 /* If both bases are based on pointers they cannot alias if they may not
906 point to the same memory object or if they point to the same object
907 and the accesses do not overlap. */
908 if ((!cfun || gimple_in_ssa_p (cfun))
909 && operand_equal_p (ptr1, ptr2, 0)
910 && (((TREE_CODE (base1) != TARGET_MEM_REF
911 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
912 && (TREE_CODE (base2) != TARGET_MEM_REF
913 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
914 || (TREE_CODE (base1) == TARGET_MEM_REF
915 && TREE_CODE (base2) == TARGET_MEM_REF
916 && (TMR_STEP (base1) == TMR_STEP (base2)
917 || (TMR_STEP (base1) && TMR_STEP (base2)
918 && operand_equal_p (TMR_STEP (base1),
919 TMR_STEP (base2), 0)))
920 && (TMR_INDEX (base1) == TMR_INDEX (base2)
921 || (TMR_INDEX (base1) && TMR_INDEX (base2)
922 && operand_equal_p (TMR_INDEX (base1),
923 TMR_INDEX (base2), 0)))
924 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
925 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
926 && operand_equal_p (TMR_INDEX2 (base1),
927 TMR_INDEX2 (base2), 0))))))
929 double_int moff;
930 /* The offset embedded in MEM_REFs can be negative. Bias them
931 so that the resulting offset adjustment is positive. */
932 moff = mem_ref_offset (base1);
933 moff = moff.alshift (BITS_PER_UNIT == 8
934 ? 3 : exact_log2 (BITS_PER_UNIT),
935 HOST_BITS_PER_DOUBLE_INT);
936 if (moff.is_negative ())
937 offset2 += (-moff).low;
938 else
939 offset1 += moff.low;
940 moff = mem_ref_offset (base2);
941 moff = moff.alshift (BITS_PER_UNIT == 8
942 ? 3 : exact_log2 (BITS_PER_UNIT),
943 HOST_BITS_PER_DOUBLE_INT);
944 if (moff.is_negative ())
945 offset1 += (-moff).low;
946 else
947 offset2 += moff.low;
948 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
950 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
951 return false;
953 /* Disambiguations that rely on strict aliasing rules follow. */
954 if (!flag_strict_aliasing || !tbaa_p)
955 return true;
957 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
958 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
960 /* If the alias set for a pointer access is zero all bets are off. */
961 if (base1_alias_set == -1)
962 base1_alias_set = get_deref_alias_set (ptrtype1);
963 if (base1_alias_set == 0)
964 return true;
965 if (base2_alias_set == -1)
966 base2_alias_set = get_deref_alias_set (ptrtype2);
967 if (base2_alias_set == 0)
968 return true;
970 /* If both references are through the same type, they do not alias
971 if the accesses do not overlap. This does extra disambiguation
972 for mixed/pointer accesses but requires strict aliasing. */
973 if ((TREE_CODE (base1) != TARGET_MEM_REF
974 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
975 && (TREE_CODE (base2) != TARGET_MEM_REF
976 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
977 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
978 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
979 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
980 TREE_TYPE (ptrtype2)) == 1)
981 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
983 /* Do type-based disambiguation. */
984 if (base1_alias_set != base2_alias_set
985 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
986 return false;
988 /* Do access-path based disambiguation. */
989 if (ref1 && ref2
990 && (handled_component_p (ref1) || handled_component_p (ref2))
991 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
992 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
993 return aliasing_component_refs_p (ref1,
994 ref1_alias_set, base1_alias_set,
995 offset1, max_size1,
996 ref2,
997 ref2_alias_set, base2_alias_set,
998 offset2, max_size2, false);
1000 return true;
1003 /* Return true, if the two memory references REF1 and REF2 may alias. */
1005 bool
1006 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1008 tree base1, base2;
1009 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1010 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1011 bool var1_p, var2_p, ind1_p, ind2_p;
1013 gcc_checking_assert ((!ref1->ref
1014 || TREE_CODE (ref1->ref) == SSA_NAME
1015 || DECL_P (ref1->ref)
1016 || TREE_CODE (ref1->ref) == STRING_CST
1017 || handled_component_p (ref1->ref)
1018 || TREE_CODE (ref1->ref) == MEM_REF
1019 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1020 && (!ref2->ref
1021 || TREE_CODE (ref2->ref) == SSA_NAME
1022 || DECL_P (ref2->ref)
1023 || TREE_CODE (ref2->ref) == STRING_CST
1024 || handled_component_p (ref2->ref)
1025 || TREE_CODE (ref2->ref) == MEM_REF
1026 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1028 /* Decompose the references into their base objects and the access. */
1029 base1 = ao_ref_base (ref1);
1030 offset1 = ref1->offset;
1031 max_size1 = ref1->max_size;
1032 base2 = ao_ref_base (ref2);
1033 offset2 = ref2->offset;
1034 max_size2 = ref2->max_size;
1036 /* We can end up with registers or constants as bases for example from
1037 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1038 which is seen as a struct copy. */
1039 if (TREE_CODE (base1) == SSA_NAME
1040 || TREE_CODE (base1) == CONST_DECL
1041 || TREE_CODE (base1) == CONSTRUCTOR
1042 || TREE_CODE (base1) == ADDR_EXPR
1043 || CONSTANT_CLASS_P (base1)
1044 || TREE_CODE (base2) == SSA_NAME
1045 || TREE_CODE (base2) == CONST_DECL
1046 || TREE_CODE (base2) == CONSTRUCTOR
1047 || TREE_CODE (base2) == ADDR_EXPR
1048 || CONSTANT_CLASS_P (base2))
1049 return false;
1051 /* We can end up referring to code via function and label decls.
1052 As we likely do not properly track code aliases conservatively
1053 bail out. */
1054 if (TREE_CODE (base1) == FUNCTION_DECL
1055 || TREE_CODE (base1) == LABEL_DECL
1056 || TREE_CODE (base2) == FUNCTION_DECL
1057 || TREE_CODE (base2) == LABEL_DECL)
1058 return true;
1060 /* Two volatile accesses always conflict. */
1061 if (ref1->volatile_p
1062 && ref2->volatile_p)
1063 return true;
1065 /* Defer to simple offset based disambiguation if we have
1066 references based on two decls. Do this before defering to
1067 TBAA to handle must-alias cases in conformance with the
1068 GCC extension of allowing type-punning through unions. */
1069 var1_p = DECL_P (base1);
1070 var2_p = DECL_P (base2);
1071 if (var1_p && var2_p)
1072 return decl_refs_may_alias_p (base1, offset1, max_size1,
1073 base2, offset2, max_size2);
1075 ind1_p = (TREE_CODE (base1) == MEM_REF
1076 || TREE_CODE (base1) == TARGET_MEM_REF);
1077 ind2_p = (TREE_CODE (base2) == MEM_REF
1078 || TREE_CODE (base2) == TARGET_MEM_REF);
1080 /* Canonicalize the pointer-vs-decl case. */
1081 if (ind1_p && var2_p)
1083 HOST_WIDE_INT tmp1;
1084 tree tmp2;
1085 ao_ref *tmp3;
1086 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1087 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1088 tmp2 = base1; base1 = base2; base2 = tmp2;
1089 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1090 var1_p = true;
1091 ind1_p = false;
1092 var2_p = false;
1093 ind2_p = true;
1096 /* First defer to TBAA if possible. */
1097 if (tbaa_p
1098 && flag_strict_aliasing
1099 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1100 ao_ref_alias_set (ref2)))
1101 return false;
1103 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1104 if (var1_p && ind2_p)
1105 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1106 offset2, max_size2,
1107 ao_ref_alias_set (ref2), -1,
1108 ref1->ref, base1,
1109 offset1, max_size1,
1110 ao_ref_alias_set (ref1),
1111 ao_ref_base_alias_set (ref1),
1112 tbaa_p);
1113 else if (ind1_p && ind2_p)
1114 return indirect_refs_may_alias_p (ref1->ref, base1,
1115 offset1, max_size1,
1116 ao_ref_alias_set (ref1), -1,
1117 ref2->ref, base2,
1118 offset2, max_size2,
1119 ao_ref_alias_set (ref2), -1,
1120 tbaa_p);
1122 /* We really do not want to end up here, but returning true is safe. */
1123 #ifdef ENABLE_CHECKING
1124 gcc_unreachable ();
1125 #else
1126 return true;
1127 #endif
1130 bool
1131 refs_may_alias_p (tree ref1, tree ref2)
1133 ao_ref r1, r2;
1134 bool res;
1135 ao_ref_init (&r1, ref1);
1136 ao_ref_init (&r2, ref2);
1137 res = refs_may_alias_p_1 (&r1, &r2, true);
1138 if (res)
1139 ++alias_stats.refs_may_alias_p_may_alias;
1140 else
1141 ++alias_stats.refs_may_alias_p_no_alias;
1142 return res;
1145 /* Returns true if there is a anti-dependence for the STORE that
1146 executes after the LOAD. */
1148 bool
1149 refs_anti_dependent_p (tree load, tree store)
1151 ao_ref r1, r2;
1152 ao_ref_init (&r1, load);
1153 ao_ref_init (&r2, store);
1154 return refs_may_alias_p_1 (&r1, &r2, false);
1157 /* Returns true if there is a output dependence for the stores
1158 STORE1 and STORE2. */
1160 bool
1161 refs_output_dependent_p (tree store1, tree store2)
1163 ao_ref r1, r2;
1164 ao_ref_init (&r1, store1);
1165 ao_ref_init (&r2, store2);
1166 return refs_may_alias_p_1 (&r1, &r2, false);
1169 /* If the call CALL may use the memory reference REF return true,
1170 otherwise return false. */
1172 static bool
1173 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1175 tree base, callee;
1176 unsigned i;
1177 int flags = gimple_call_flags (call);
1179 /* Const functions without a static chain do not implicitly use memory. */
1180 if (!gimple_call_chain (call)
1181 && (flags & (ECF_CONST|ECF_NOVOPS)))
1182 goto process_args;
1184 base = ao_ref_base (ref);
1185 if (!base)
1186 return true;
1188 /* A call that is not without side-effects might involve volatile
1189 accesses and thus conflicts with all other volatile accesses. */
1190 if (ref->volatile_p)
1191 return true;
1193 /* If the reference is based on a decl that is not aliased the call
1194 cannot possibly use it. */
1195 if (DECL_P (base)
1196 && !may_be_aliased (base)
1197 /* But local statics can be used through recursion. */
1198 && !is_global_var (base))
1199 goto process_args;
1201 callee = gimple_call_fndecl (call);
1203 /* Handle those builtin functions explicitly that do not act as
1204 escape points. See tree-ssa-structalias.c:find_func_aliases
1205 for the list of builtins we might need to handle here. */
1206 if (callee != NULL_TREE
1207 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1208 switch (DECL_FUNCTION_CODE (callee))
1210 /* All the following functions read memory pointed to by
1211 their second argument. strcat/strncat additionally
1212 reads memory pointed to by the first argument. */
1213 case BUILT_IN_STRCAT:
1214 case BUILT_IN_STRNCAT:
1216 ao_ref dref;
1217 ao_ref_init_from_ptr_and_size (&dref,
1218 gimple_call_arg (call, 0),
1219 NULL_TREE);
1220 if (refs_may_alias_p_1 (&dref, ref, false))
1221 return true;
1223 /* FALLTHRU */
1224 case BUILT_IN_STRCPY:
1225 case BUILT_IN_STRNCPY:
1226 case BUILT_IN_MEMCPY:
1227 case BUILT_IN_MEMMOVE:
1228 case BUILT_IN_MEMPCPY:
1229 case BUILT_IN_STPCPY:
1230 case BUILT_IN_STPNCPY:
1231 case BUILT_IN_TM_MEMCPY:
1232 case BUILT_IN_TM_MEMMOVE:
1234 ao_ref dref;
1235 tree size = NULL_TREE;
1236 if (gimple_call_num_args (call) == 3)
1237 size = gimple_call_arg (call, 2);
1238 ao_ref_init_from_ptr_and_size (&dref,
1239 gimple_call_arg (call, 1),
1240 size);
1241 return refs_may_alias_p_1 (&dref, ref, false);
1243 case BUILT_IN_STRCAT_CHK:
1244 case BUILT_IN_STRNCAT_CHK:
1246 ao_ref dref;
1247 ao_ref_init_from_ptr_and_size (&dref,
1248 gimple_call_arg (call, 0),
1249 NULL_TREE);
1250 if (refs_may_alias_p_1 (&dref, ref, false))
1251 return true;
1253 /* FALLTHRU */
1254 case BUILT_IN_STRCPY_CHK:
1255 case BUILT_IN_STRNCPY_CHK:
1256 case BUILT_IN_MEMCPY_CHK:
1257 case BUILT_IN_MEMMOVE_CHK:
1258 case BUILT_IN_MEMPCPY_CHK:
1259 case BUILT_IN_STPCPY_CHK:
1260 case BUILT_IN_STPNCPY_CHK:
1262 ao_ref dref;
1263 tree size = NULL_TREE;
1264 if (gimple_call_num_args (call) == 4)
1265 size = gimple_call_arg (call, 2);
1266 ao_ref_init_from_ptr_and_size (&dref,
1267 gimple_call_arg (call, 1),
1268 size);
1269 return refs_may_alias_p_1 (&dref, ref, false);
1271 case BUILT_IN_BCOPY:
1273 ao_ref dref;
1274 tree size = gimple_call_arg (call, 2);
1275 ao_ref_init_from_ptr_and_size (&dref,
1276 gimple_call_arg (call, 0),
1277 size);
1278 return refs_may_alias_p_1 (&dref, ref, false);
1281 /* The following functions read memory pointed to by their
1282 first argument. */
1283 CASE_BUILT_IN_TM_LOAD (1):
1284 CASE_BUILT_IN_TM_LOAD (2):
1285 CASE_BUILT_IN_TM_LOAD (4):
1286 CASE_BUILT_IN_TM_LOAD (8):
1287 CASE_BUILT_IN_TM_LOAD (FLOAT):
1288 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1289 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1290 CASE_BUILT_IN_TM_LOAD (M64):
1291 CASE_BUILT_IN_TM_LOAD (M128):
1292 CASE_BUILT_IN_TM_LOAD (M256):
1293 case BUILT_IN_TM_LOG:
1294 case BUILT_IN_TM_LOG_1:
1295 case BUILT_IN_TM_LOG_2:
1296 case BUILT_IN_TM_LOG_4:
1297 case BUILT_IN_TM_LOG_8:
1298 case BUILT_IN_TM_LOG_FLOAT:
1299 case BUILT_IN_TM_LOG_DOUBLE:
1300 case BUILT_IN_TM_LOG_LDOUBLE:
1301 case BUILT_IN_TM_LOG_M64:
1302 case BUILT_IN_TM_LOG_M128:
1303 case BUILT_IN_TM_LOG_M256:
1304 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1306 /* These read memory pointed to by the first argument. */
1307 case BUILT_IN_STRDUP:
1308 case BUILT_IN_STRNDUP:
1310 ao_ref dref;
1311 tree size = NULL_TREE;
1312 if (gimple_call_num_args (call) == 2)
1313 size = gimple_call_arg (call, 1);
1314 ao_ref_init_from_ptr_and_size (&dref,
1315 gimple_call_arg (call, 0),
1316 size);
1317 return refs_may_alias_p_1 (&dref, ref, false);
1319 /* The following builtins do not read from memory. */
1320 case BUILT_IN_FREE:
1321 case BUILT_IN_MALLOC:
1322 case BUILT_IN_CALLOC:
1323 case BUILT_IN_ALLOCA:
1324 case BUILT_IN_ALLOCA_WITH_ALIGN:
1325 case BUILT_IN_STACK_SAVE:
1326 case BUILT_IN_STACK_RESTORE:
1327 case BUILT_IN_MEMSET:
1328 case BUILT_IN_TM_MEMSET:
1329 case BUILT_IN_MEMSET_CHK:
1330 case BUILT_IN_FREXP:
1331 case BUILT_IN_FREXPF:
1332 case BUILT_IN_FREXPL:
1333 case BUILT_IN_GAMMA_R:
1334 case BUILT_IN_GAMMAF_R:
1335 case BUILT_IN_GAMMAL_R:
1336 case BUILT_IN_LGAMMA_R:
1337 case BUILT_IN_LGAMMAF_R:
1338 case BUILT_IN_LGAMMAL_R:
1339 case BUILT_IN_MODF:
1340 case BUILT_IN_MODFF:
1341 case BUILT_IN_MODFL:
1342 case BUILT_IN_REMQUO:
1343 case BUILT_IN_REMQUOF:
1344 case BUILT_IN_REMQUOL:
1345 case BUILT_IN_SINCOS:
1346 case BUILT_IN_SINCOSF:
1347 case BUILT_IN_SINCOSL:
1348 case BUILT_IN_ASSUME_ALIGNED:
1349 case BUILT_IN_VA_END:
1350 return false;
1351 /* __sync_* builtins and some OpenMP builtins act as threading
1352 barriers. */
1353 #undef DEF_SYNC_BUILTIN
1354 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1355 #include "sync-builtins.def"
1356 #undef DEF_SYNC_BUILTIN
1357 case BUILT_IN_GOMP_ATOMIC_START:
1358 case BUILT_IN_GOMP_ATOMIC_END:
1359 case BUILT_IN_GOMP_BARRIER:
1360 case BUILT_IN_GOMP_TASKWAIT:
1361 case BUILT_IN_GOMP_CRITICAL_START:
1362 case BUILT_IN_GOMP_CRITICAL_END:
1363 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1364 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1365 case BUILT_IN_GOMP_LOOP_END:
1366 case BUILT_IN_GOMP_ORDERED_START:
1367 case BUILT_IN_GOMP_ORDERED_END:
1368 case BUILT_IN_GOMP_PARALLEL_END:
1369 case BUILT_IN_GOMP_SECTIONS_END:
1370 case BUILT_IN_GOMP_SINGLE_COPY_START:
1371 case BUILT_IN_GOMP_SINGLE_COPY_END:
1372 return true;
1374 default:
1375 /* Fallthru to general call handling. */;
1378 /* Check if base is a global static variable that is not read
1379 by the function. */
1380 if (callee != NULL_TREE
1381 && TREE_CODE (base) == VAR_DECL
1382 && TREE_STATIC (base))
1384 struct cgraph_node *node = cgraph_get_node (callee);
1385 bitmap not_read;
1387 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1388 node yet. We should enforce that there are nodes for all decls in the
1389 IL and remove this check instead. */
1390 if (node
1391 && (not_read = ipa_reference_get_not_read_global (node))
1392 && bitmap_bit_p (not_read, DECL_UID (base)))
1393 goto process_args;
1396 /* Check if the base variable is call-used. */
1397 if (DECL_P (base))
1399 if (pt_solution_includes (gimple_call_use_set (call), base))
1400 return true;
1402 else if ((TREE_CODE (base) == MEM_REF
1403 || TREE_CODE (base) == TARGET_MEM_REF)
1404 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1406 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1407 if (!pi)
1408 return true;
1410 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1411 return true;
1413 else
1414 return true;
1416 /* Inspect call arguments for passed-by-value aliases. */
1417 process_args:
1418 for (i = 0; i < gimple_call_num_args (call); ++i)
1420 tree op = gimple_call_arg (call, i);
1421 int flags = gimple_call_arg_flags (call, i);
1423 if (flags & EAF_UNUSED)
1424 continue;
1426 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1427 op = TREE_OPERAND (op, 0);
1429 if (TREE_CODE (op) != SSA_NAME
1430 && !is_gimple_min_invariant (op))
1432 ao_ref r;
1433 ao_ref_init (&r, op);
1434 if (refs_may_alias_p_1 (&r, ref, true))
1435 return true;
1439 return false;
1442 static bool
1443 ref_maybe_used_by_call_p (gimple call, tree ref)
1445 ao_ref r;
1446 bool res;
1447 ao_ref_init (&r, ref);
1448 res = ref_maybe_used_by_call_p_1 (call, &r);
1449 if (res)
1450 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1451 else
1452 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1453 return res;
1457 /* If the statement STMT may use the memory reference REF return
1458 true, otherwise return false. */
1460 bool
1461 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1463 if (is_gimple_assign (stmt))
1465 tree rhs;
1467 /* All memory assign statements are single. */
1468 if (!gimple_assign_single_p (stmt))
1469 return false;
1471 rhs = gimple_assign_rhs1 (stmt);
1472 if (is_gimple_reg (rhs)
1473 || is_gimple_min_invariant (rhs)
1474 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1475 return false;
1477 return refs_may_alias_p (rhs, ref);
1479 else if (is_gimple_call (stmt))
1480 return ref_maybe_used_by_call_p (stmt, ref);
1481 else if (gimple_code (stmt) == GIMPLE_RETURN)
1483 tree retval = gimple_return_retval (stmt);
1484 tree base;
1485 if (retval
1486 && TREE_CODE (retval) != SSA_NAME
1487 && !is_gimple_min_invariant (retval)
1488 && refs_may_alias_p (retval, ref))
1489 return true;
1490 /* If ref escapes the function then the return acts as a use. */
1491 base = get_base_address (ref);
1492 if (!base)
1494 else if (DECL_P (base))
1495 return is_global_var (base);
1496 else if (TREE_CODE (base) == MEM_REF
1497 || TREE_CODE (base) == TARGET_MEM_REF)
1498 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1499 return false;
1502 return true;
1505 /* If the call in statement CALL may clobber the memory reference REF
1506 return true, otherwise return false. */
1508 static bool
1509 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1511 tree base;
1512 tree callee;
1514 /* If the call is pure or const it cannot clobber anything. */
1515 if (gimple_call_flags (call)
1516 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1517 return false;
1519 base = ao_ref_base (ref);
1520 if (!base)
1521 return true;
1523 if (TREE_CODE (base) == SSA_NAME
1524 || CONSTANT_CLASS_P (base))
1525 return false;
1527 /* A call that is not without side-effects might involve volatile
1528 accesses and thus conflicts with all other volatile accesses. */
1529 if (ref->volatile_p)
1530 return true;
1532 /* If the reference is based on a decl that is not aliased the call
1533 cannot possibly clobber it. */
1534 if (DECL_P (base)
1535 && !may_be_aliased (base)
1536 /* But local non-readonly statics can be modified through recursion
1537 or the call may implement a threading barrier which we must
1538 treat as may-def. */
1539 && (TREE_READONLY (base)
1540 || !is_global_var (base)))
1541 return false;
1543 callee = gimple_call_fndecl (call);
1545 /* Handle those builtin functions explicitly that do not act as
1546 escape points. See tree-ssa-structalias.c:find_func_aliases
1547 for the list of builtins we might need to handle here. */
1548 if (callee != NULL_TREE
1549 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1550 switch (DECL_FUNCTION_CODE (callee))
1552 /* All the following functions clobber memory pointed to by
1553 their first argument. */
1554 case BUILT_IN_STRCPY:
1555 case BUILT_IN_STRNCPY:
1556 case BUILT_IN_MEMCPY:
1557 case BUILT_IN_MEMMOVE:
1558 case BUILT_IN_MEMPCPY:
1559 case BUILT_IN_STPCPY:
1560 case BUILT_IN_STPNCPY:
1561 case BUILT_IN_STRCAT:
1562 case BUILT_IN_STRNCAT:
1563 case BUILT_IN_MEMSET:
1564 case BUILT_IN_TM_MEMSET:
1565 CASE_BUILT_IN_TM_STORE (1):
1566 CASE_BUILT_IN_TM_STORE (2):
1567 CASE_BUILT_IN_TM_STORE (4):
1568 CASE_BUILT_IN_TM_STORE (8):
1569 CASE_BUILT_IN_TM_STORE (FLOAT):
1570 CASE_BUILT_IN_TM_STORE (DOUBLE):
1571 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1572 CASE_BUILT_IN_TM_STORE (M64):
1573 CASE_BUILT_IN_TM_STORE (M128):
1574 CASE_BUILT_IN_TM_STORE (M256):
1575 case BUILT_IN_TM_MEMCPY:
1576 case BUILT_IN_TM_MEMMOVE:
1578 ao_ref dref;
1579 tree size = NULL_TREE;
1580 /* Don't pass in size for strncat, as the maximum size
1581 is strlen (dest) + n + 1 instead of n, resp.
1582 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1583 known. */
1584 if (gimple_call_num_args (call) == 3
1585 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1586 size = gimple_call_arg (call, 2);
1587 ao_ref_init_from_ptr_and_size (&dref,
1588 gimple_call_arg (call, 0),
1589 size);
1590 return refs_may_alias_p_1 (&dref, ref, false);
1592 case BUILT_IN_STRCPY_CHK:
1593 case BUILT_IN_STRNCPY_CHK:
1594 case BUILT_IN_MEMCPY_CHK:
1595 case BUILT_IN_MEMMOVE_CHK:
1596 case BUILT_IN_MEMPCPY_CHK:
1597 case BUILT_IN_STPCPY_CHK:
1598 case BUILT_IN_STPNCPY_CHK:
1599 case BUILT_IN_STRCAT_CHK:
1600 case BUILT_IN_STRNCAT_CHK:
1601 case BUILT_IN_MEMSET_CHK:
1603 ao_ref dref;
1604 tree size = NULL_TREE;
1605 /* Don't pass in size for __strncat_chk, as the maximum size
1606 is strlen (dest) + n + 1 instead of n, resp.
1607 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1608 known. */
1609 if (gimple_call_num_args (call) == 4
1610 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1611 size = gimple_call_arg (call, 2);
1612 ao_ref_init_from_ptr_and_size (&dref,
1613 gimple_call_arg (call, 0),
1614 size);
1615 return refs_may_alias_p_1 (&dref, ref, false);
1617 case BUILT_IN_BCOPY:
1619 ao_ref dref;
1620 tree size = gimple_call_arg (call, 2);
1621 ao_ref_init_from_ptr_and_size (&dref,
1622 gimple_call_arg (call, 1),
1623 size);
1624 return refs_may_alias_p_1 (&dref, ref, false);
1626 /* Allocating memory does not have any side-effects apart from
1627 being the definition point for the pointer. */
1628 case BUILT_IN_MALLOC:
1629 case BUILT_IN_CALLOC:
1630 case BUILT_IN_STRDUP:
1631 case BUILT_IN_STRNDUP:
1632 /* Unix98 specifies that errno is set on allocation failure. */
1633 if (flag_errno_math
1634 && targetm.ref_may_alias_errno (ref))
1635 return true;
1636 return false;
1637 case BUILT_IN_STACK_SAVE:
1638 case BUILT_IN_ALLOCA:
1639 case BUILT_IN_ALLOCA_WITH_ALIGN:
1640 case BUILT_IN_ASSUME_ALIGNED:
1641 return false;
1642 /* Freeing memory kills the pointed-to memory. More importantly
1643 the call has to serve as a barrier for moving loads and stores
1644 across it. */
1645 case BUILT_IN_FREE:
1646 case BUILT_IN_VA_END:
1648 tree ptr = gimple_call_arg (call, 0);
1649 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1651 case BUILT_IN_GAMMA_R:
1652 case BUILT_IN_GAMMAF_R:
1653 case BUILT_IN_GAMMAL_R:
1654 case BUILT_IN_LGAMMA_R:
1655 case BUILT_IN_LGAMMAF_R:
1656 case BUILT_IN_LGAMMAL_R:
1658 tree out = gimple_call_arg (call, 1);
1659 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1660 return true;
1661 if (flag_errno_math)
1662 break;
1663 return false;
1665 case BUILT_IN_FREXP:
1666 case BUILT_IN_FREXPF:
1667 case BUILT_IN_FREXPL:
1668 case BUILT_IN_MODF:
1669 case BUILT_IN_MODFF:
1670 case BUILT_IN_MODFL:
1672 tree out = gimple_call_arg (call, 1);
1673 return ptr_deref_may_alias_ref_p_1 (out, ref);
1675 case BUILT_IN_REMQUO:
1676 case BUILT_IN_REMQUOF:
1677 case BUILT_IN_REMQUOL:
1679 tree out = gimple_call_arg (call, 2);
1680 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1681 return true;
1682 if (flag_errno_math)
1683 break;
1684 return false;
1686 case BUILT_IN_SINCOS:
1687 case BUILT_IN_SINCOSF:
1688 case BUILT_IN_SINCOSL:
1690 tree sin = gimple_call_arg (call, 1);
1691 tree cos = gimple_call_arg (call, 2);
1692 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1693 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1695 /* __sync_* builtins and some OpenMP builtins act as threading
1696 barriers. */
1697 #undef DEF_SYNC_BUILTIN
1698 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1699 #include "sync-builtins.def"
1700 #undef DEF_SYNC_BUILTIN
1701 case BUILT_IN_GOMP_ATOMIC_START:
1702 case BUILT_IN_GOMP_ATOMIC_END:
1703 case BUILT_IN_GOMP_BARRIER:
1704 case BUILT_IN_GOMP_TASKWAIT:
1705 case BUILT_IN_GOMP_CRITICAL_START:
1706 case BUILT_IN_GOMP_CRITICAL_END:
1707 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1708 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1709 case BUILT_IN_GOMP_LOOP_END:
1710 case BUILT_IN_GOMP_ORDERED_START:
1711 case BUILT_IN_GOMP_ORDERED_END:
1712 case BUILT_IN_GOMP_PARALLEL_END:
1713 case BUILT_IN_GOMP_SECTIONS_END:
1714 case BUILT_IN_GOMP_SINGLE_COPY_START:
1715 case BUILT_IN_GOMP_SINGLE_COPY_END:
1716 return true;
1717 default:
1718 /* Fallthru to general call handling. */;
1721 /* Check if base is a global static variable that is not written
1722 by the function. */
1723 if (callee != NULL_TREE
1724 && TREE_CODE (base) == VAR_DECL
1725 && TREE_STATIC (base))
1727 struct cgraph_node *node = cgraph_get_node (callee);
1728 bitmap not_written;
1730 if (node
1731 && (not_written = ipa_reference_get_not_written_global (node))
1732 && bitmap_bit_p (not_written, DECL_UID (base)))
1733 return false;
1736 /* Check if the base variable is call-clobbered. */
1737 if (DECL_P (base))
1738 return pt_solution_includes (gimple_call_clobber_set (call), base);
1739 else if ((TREE_CODE (base) == MEM_REF
1740 || TREE_CODE (base) == TARGET_MEM_REF)
1741 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1743 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1744 if (!pi)
1745 return true;
1747 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1750 return true;
1753 /* If the call in statement CALL may clobber the memory reference REF
1754 return true, otherwise return false. */
1756 bool
1757 call_may_clobber_ref_p (gimple call, tree ref)
1759 bool res;
1760 ao_ref r;
1761 ao_ref_init (&r, ref);
1762 res = call_may_clobber_ref_p_1 (call, &r);
1763 if (res)
1764 ++alias_stats.call_may_clobber_ref_p_may_alias;
1765 else
1766 ++alias_stats.call_may_clobber_ref_p_no_alias;
1767 return res;
1771 /* If the statement STMT may clobber the memory reference REF return true,
1772 otherwise return false. */
1774 bool
1775 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1777 if (is_gimple_call (stmt))
1779 tree lhs = gimple_call_lhs (stmt);
1780 if (lhs
1781 && TREE_CODE (lhs) != SSA_NAME)
1783 ao_ref r;
1784 ao_ref_init (&r, lhs);
1785 if (refs_may_alias_p_1 (ref, &r, true))
1786 return true;
1789 return call_may_clobber_ref_p_1 (stmt, ref);
1791 else if (gimple_assign_single_p (stmt))
1793 tree lhs = gimple_assign_lhs (stmt);
1794 if (TREE_CODE (lhs) != SSA_NAME)
1796 ao_ref r;
1797 ao_ref_init (&r, lhs);
1798 return refs_may_alias_p_1 (ref, &r, true);
1801 else if (gimple_code (stmt) == GIMPLE_ASM)
1802 return true;
1804 return false;
1807 bool
1808 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1810 ao_ref r;
1811 ao_ref_init (&r, ref);
1812 return stmt_may_clobber_ref_p_1 (stmt, &r);
1815 /* If STMT kills the memory reference REF return true, otherwise
1816 return false. */
1818 static bool
1819 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1821 /* For a must-alias check we need to be able to constrain
1822 the access properly. */
1823 ao_ref_base (ref);
1824 if (ref->max_size == -1)
1825 return false;
1827 if (gimple_has_lhs (stmt)
1828 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1829 /* The assignment is not necessarily carried out if it can throw
1830 and we can catch it in the current function where we could inspect
1831 the previous value.
1832 ??? We only need to care about the RHS throwing. For aggregate
1833 assignments or similar calls and non-call exceptions the LHS
1834 might throw as well. */
1835 && !stmt_can_throw_internal (stmt))
1837 tree base, lhs = gimple_get_lhs (stmt);
1838 HOST_WIDE_INT size, offset, max_size;
1839 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1840 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1841 so base == ref->base does not always hold. */
1842 if (base == ref->base)
1844 /* For a must-alias check we need to be able to constrain
1845 the access properly. */
1846 if (size != -1 && size == max_size)
1848 if (offset <= ref->offset
1849 && offset + size >= ref->offset + ref->max_size)
1850 return true;
1855 if (is_gimple_call (stmt))
1857 tree callee = gimple_call_fndecl (stmt);
1858 if (callee != NULL_TREE
1859 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1860 switch (DECL_FUNCTION_CODE (callee))
1862 case BUILT_IN_MEMCPY:
1863 case BUILT_IN_MEMPCPY:
1864 case BUILT_IN_MEMMOVE:
1865 case BUILT_IN_MEMSET:
1866 case BUILT_IN_MEMCPY_CHK:
1867 case BUILT_IN_MEMPCPY_CHK:
1868 case BUILT_IN_MEMMOVE_CHK:
1869 case BUILT_IN_MEMSET_CHK:
1871 tree dest = gimple_call_arg (stmt, 0);
1872 tree len = gimple_call_arg (stmt, 2);
1873 tree base = NULL_TREE;
1874 HOST_WIDE_INT offset = 0;
1875 if (!host_integerp (len, 0))
1876 return false;
1877 if (TREE_CODE (dest) == ADDR_EXPR)
1878 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1879 &offset);
1880 else if (TREE_CODE (dest) == SSA_NAME)
1881 base = dest;
1882 if (base
1883 && base == ao_ref_base (ref))
1885 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1886 if (offset <= ref->offset / BITS_PER_UNIT
1887 && (offset + size
1888 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1889 / BITS_PER_UNIT)))
1890 return true;
1892 break;
1895 case BUILT_IN_VA_END:
1897 tree ptr = gimple_call_arg (stmt, 0);
1898 if (TREE_CODE (ptr) == ADDR_EXPR)
1900 tree base = ao_ref_base (ref);
1901 if (TREE_OPERAND (ptr, 0) == base)
1902 return true;
1904 break;
1907 default:;
1910 return false;
1913 bool
1914 stmt_kills_ref_p (gimple stmt, tree ref)
1916 ao_ref r;
1917 ao_ref_init (&r, ref);
1918 return stmt_kills_ref_p_1 (stmt, &r);
1922 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1923 TARGET or a statement clobbering the memory reference REF in which
1924 case false is returned. The walk starts with VUSE, one argument of PHI. */
1926 static bool
1927 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1928 tree vuse, unsigned int *cnt, bitmap *visited,
1929 bool abort_on_visited)
1931 basic_block bb = gimple_bb (phi);
1933 if (!*visited)
1934 *visited = BITMAP_ALLOC (NULL);
1936 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1938 /* Walk until we hit the target. */
1939 while (vuse != target)
1941 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1942 /* Recurse for PHI nodes. */
1943 if (gimple_code (def_stmt) == GIMPLE_PHI)
1945 /* An already visited PHI node ends the walk successfully. */
1946 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1947 return !abort_on_visited;
1948 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
1949 visited, abort_on_visited);
1950 if (!vuse)
1951 return false;
1952 continue;
1954 else if (gimple_nop_p (def_stmt))
1955 return false;
1956 else
1958 /* A clobbering statement or the end of the IL ends it failing. */
1959 ++*cnt;
1960 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1961 return false;
1963 /* If we reach a new basic-block see if we already skipped it
1964 in a previous walk that ended successfully. */
1965 if (gimple_bb (def_stmt) != bb)
1967 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
1968 return !abort_on_visited;
1969 bb = gimple_bb (def_stmt);
1971 vuse = gimple_vuse (def_stmt);
1973 return true;
1976 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
1977 until we hit the phi argument definition that dominates the other one.
1978 Return that, or NULL_TREE if there is no such definition. */
1980 static tree
1981 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
1982 ao_ref *ref, unsigned int *cnt,
1983 bitmap *visited, bool abort_on_visited)
1985 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1986 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1987 tree common_vuse;
1989 if (arg0 == arg1)
1990 return arg0;
1991 else if (gimple_nop_p (def0)
1992 || (!gimple_nop_p (def1)
1993 && dominated_by_p (CDI_DOMINATORS,
1994 gimple_bb (def1), gimple_bb (def0))))
1996 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
1997 visited, abort_on_visited))
1998 return arg0;
2000 else if (gimple_nop_p (def1)
2001 || dominated_by_p (CDI_DOMINATORS,
2002 gimple_bb (def0), gimple_bb (def1)))
2004 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2005 visited, abort_on_visited))
2006 return arg1;
2008 /* Special case of a diamond:
2009 MEM_1 = ...
2010 goto (cond) ? L1 : L2
2011 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2012 goto L3
2013 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2014 L3: MEM_4 = PHI<MEM_2, MEM_3>
2015 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2016 dominate each other, but still we can easily skip this PHI node
2017 if we recognize that the vuse MEM operand is the same for both,
2018 and that we can skip both statements (they don't clobber us).
2019 This is still linear. Don't use maybe_skip_until, that might
2020 potentially be slow. */
2021 else if ((common_vuse = gimple_vuse (def0))
2022 && common_vuse == gimple_vuse (def1))
2024 *cnt += 2;
2025 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2026 && !stmt_may_clobber_ref_p_1 (def1, ref))
2027 return common_vuse;
2030 return NULL_TREE;
2034 /* Starting from a PHI node for the virtual operand of the memory reference
2035 REF find a continuation virtual operand that allows to continue walking
2036 statements dominating PHI skipping only statements that cannot possibly
2037 clobber REF. Increments *CNT for each alias disambiguation done.
2038 Returns NULL_TREE if no suitable virtual operand can be found. */
2040 tree
2041 get_continuation_for_phi (gimple phi, ao_ref *ref,
2042 unsigned int *cnt, bitmap *visited,
2043 bool abort_on_visited)
2045 unsigned nargs = gimple_phi_num_args (phi);
2047 /* Through a single-argument PHI we can simply look through. */
2048 if (nargs == 1)
2049 return PHI_ARG_DEF (phi, 0);
2051 /* For two or more arguments try to pairwise skip non-aliasing code
2052 until we hit the phi argument definition that dominates the other one. */
2053 else if (nargs >= 2)
2055 tree arg0, arg1;
2056 unsigned i;
2058 /* Find a candidate for the virtual operand which definition
2059 dominates those of all others. */
2060 arg0 = PHI_ARG_DEF (phi, 0);
2061 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2062 for (i = 1; i < nargs; ++i)
2064 arg1 = PHI_ARG_DEF (phi, i);
2065 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2067 arg0 = arg1;
2068 break;
2070 if (dominated_by_p (CDI_DOMINATORS,
2071 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2072 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2073 arg0 = arg1;
2076 /* Then pairwise reduce against the found candidate. */
2077 for (i = 0; i < nargs; ++i)
2079 arg1 = PHI_ARG_DEF (phi, i);
2080 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2081 cnt, visited, abort_on_visited);
2082 if (!arg0)
2083 return NULL_TREE;
2086 return arg0;
2089 return NULL_TREE;
2092 /* Based on the memory reference REF and its virtual use VUSE call
2093 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2094 itself. That is, for each virtual use for which its defining statement
2095 does not clobber REF.
2097 WALKER is called with REF, the current virtual use and DATA. If
2098 WALKER returns non-NULL the walk stops and its result is returned.
2099 At the end of a non-successful walk NULL is returned.
2101 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2102 use which definition is a statement that may clobber REF and DATA.
2103 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2104 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2105 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2106 to adjust REF and *DATA to make that valid.
2108 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2110 void *
2111 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2112 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2113 void *(*translate)(ao_ref *, tree, void *), void *data)
2115 bitmap visited = NULL;
2116 void *res;
2117 unsigned int cnt = 0;
2118 bool translated = false;
2120 timevar_push (TV_ALIAS_STMT_WALK);
2124 gimple def_stmt;
2126 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2127 res = (*walker) (ref, vuse, cnt, data);
2128 /* Abort walk. */
2129 if (res == (void *)-1)
2131 res = NULL;
2132 break;
2134 /* Lookup succeeded. */
2135 else if (res != NULL)
2136 break;
2138 def_stmt = SSA_NAME_DEF_STMT (vuse);
2139 if (gimple_nop_p (def_stmt))
2140 break;
2141 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2142 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2143 &visited, translated);
2144 else
2146 cnt++;
2147 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2149 if (!translate)
2150 break;
2151 res = (*translate) (ref, vuse, data);
2152 /* Failed lookup and translation. */
2153 if (res == (void *)-1)
2155 res = NULL;
2156 break;
2158 /* Lookup succeeded. */
2159 else if (res != NULL)
2160 break;
2161 /* Translation succeeded, continue walking. */
2162 translated = true;
2164 vuse = gimple_vuse (def_stmt);
2167 while (vuse);
2169 if (visited)
2170 BITMAP_FREE (visited);
2172 timevar_pop (TV_ALIAS_STMT_WALK);
2174 return res;
2178 /* Based on the memory reference REF call WALKER for each vdef which
2179 defining statement may clobber REF, starting with VDEF. If REF
2180 is NULL_TREE, each defining statement is visited.
2182 WALKER is called with REF, the current vdef and DATA. If WALKER
2183 returns true the walk is stopped, otherwise it continues.
2185 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2186 PHI argument (but only one walk continues on merge points), the
2187 return value is true if any of the walks was successful.
2189 The function returns the number of statements walked. */
2191 static unsigned int
2192 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2193 bool (*walker)(ao_ref *, tree, void *), void *data,
2194 bitmap *visited, unsigned int cnt)
2198 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2200 if (*visited
2201 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2202 return cnt;
2204 if (gimple_nop_p (def_stmt))
2205 return cnt;
2206 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2208 unsigned i;
2209 if (!*visited)
2210 *visited = BITMAP_ALLOC (NULL);
2211 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2212 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2213 walker, data, visited, 0);
2214 return cnt;
2217 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2218 cnt++;
2219 if ((!ref
2220 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2221 && (*walker) (ref, vdef, data))
2222 return cnt;
2224 vdef = gimple_vuse (def_stmt);
2226 while (1);
2229 unsigned int
2230 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2231 bool (*walker)(ao_ref *, tree, void *), void *data,
2232 bitmap *visited)
2234 bitmap local_visited = NULL;
2235 unsigned int ret;
2237 timevar_push (TV_ALIAS_STMT_WALK);
2239 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2240 visited ? visited : &local_visited, 0);
2241 if (local_visited)
2242 BITMAP_FREE (local_visited);
2244 timevar_pop (TV_ALIAS_STMT_WALK);
2246 return ret;