gcc/testsuite/
[official-gcc.git] / gcc / tree-ssa-alias.c
blob5a14ff9e8fda84f394a631e5b3ba5962a7c8439c
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "target.h"
28 #include "basic-block.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "langhooks.h"
31 #include "flags.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "vec.h"
35 #include "machmode.h"
36 #include "hard-reg-set.h"
37 #include "input.h"
38 #include "function.h"
39 #include "tree-pretty-print.h"
40 #include "dumpfile.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "tree-eh.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "gimple-ssa.h"
48 #include "stringpool.h"
49 #include "tree-ssanames.h"
50 #include "expr.h"
51 #include "tree-dfa.h"
52 #include "tree-inline.h"
53 #include "params.h"
54 #include "alloc-pool.h"
55 #include "tree-ssa-alias.h"
56 #include "ipa-reference.h"
58 /* Broad overview of how alias analysis on gimple works:
60 Statements clobbering or using memory are linked through the
61 virtual operand factored use-def chain. The virtual operand
62 is unique per function, its symbol is accessible via gimple_vop (cfun).
63 Virtual operands are used for efficiently walking memory statements
64 in the gimple IL and are useful for things like value-numbering as
65 a generation count for memory references.
67 SSA_NAME pointers may have associated points-to information
68 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
69 points-to information is (re-)computed by the TODO_rebuild_alias
70 pass manager todo. Points-to information is also used for more
71 precise tracking of call-clobbered and call-used variables and
72 related disambiguations.
74 This file contains functions for disambiguating memory references,
75 the so called alias-oracle and tools for walking of the gimple IL.
77 The main alias-oracle entry-points are
79 bool stmt_may_clobber_ref_p (gimple, tree)
81 This function queries if a statement may invalidate (parts of)
82 the memory designated by the reference tree argument.
84 bool ref_maybe_used_by_stmt_p (gimple, tree)
86 This function queries if a statement may need (parts of) the
87 memory designated by the reference tree argument.
89 There are variants of these functions that only handle the call
90 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
91 Note that these do not disambiguate against a possible call lhs.
93 bool refs_may_alias_p (tree, tree)
95 This function tries to disambiguate two reference trees.
97 bool ptr_deref_may_alias_global_p (tree)
99 This function queries if dereferencing a pointer variable may
100 alias global memory.
102 More low-level disambiguators are available and documented in
103 this file. Low-level disambiguators dealing with points-to
104 information are in tree-ssa-structalias.c. */
107 /* Query statistics for the different low-level disambiguators.
108 A high-level query may trigger multiple of them. */
110 static struct {
111 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
112 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
113 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
114 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
115 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
116 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
117 } alias_stats;
119 void
120 dump_alias_stats (FILE *s)
122 fprintf (s, "\nAlias oracle query stats:\n");
123 fprintf (s, " refs_may_alias_p: "
124 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
125 HOST_WIDE_INT_PRINT_DEC" queries\n",
126 alias_stats.refs_may_alias_p_no_alias,
127 alias_stats.refs_may_alias_p_no_alias
128 + alias_stats.refs_may_alias_p_may_alias);
129 fprintf (s, " ref_maybe_used_by_call_p: "
130 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
131 HOST_WIDE_INT_PRINT_DEC" queries\n",
132 alias_stats.ref_maybe_used_by_call_p_no_alias,
133 alias_stats.refs_may_alias_p_no_alias
134 + alias_stats.ref_maybe_used_by_call_p_may_alias);
135 fprintf (s, " call_may_clobber_ref_p: "
136 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
137 HOST_WIDE_INT_PRINT_DEC" queries\n",
138 alias_stats.call_may_clobber_ref_p_no_alias,
139 alias_stats.call_may_clobber_ref_p_no_alias
140 + alias_stats.call_may_clobber_ref_p_may_alias);
144 /* Return true, if dereferencing PTR may alias with a global variable. */
146 bool
147 ptr_deref_may_alias_global_p (tree ptr)
149 struct ptr_info_def *pi;
151 /* If we end up with a pointer constant here that may point
152 to global memory. */
153 if (TREE_CODE (ptr) != SSA_NAME)
154 return true;
156 pi = SSA_NAME_PTR_INFO (ptr);
158 /* If we do not have points-to information for this variable,
159 we have to punt. */
160 if (!pi)
161 return true;
163 /* ??? This does not use TBAA to prune globals ptr may not access. */
164 return pt_solution_includes_global (&pi->pt);
167 /* Return true if dereferencing PTR may alias DECL.
168 The caller is responsible for applying TBAA to see if PTR
169 may access DECL at all. */
171 static bool
172 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
174 struct ptr_info_def *pi;
176 /* Conversions are irrelevant for points-to information and
177 data-dependence analysis can feed us those. */
178 STRIP_NOPS (ptr);
180 /* Anything we do not explicilty handle aliases. */
181 if ((TREE_CODE (ptr) != SSA_NAME
182 && TREE_CODE (ptr) != ADDR_EXPR
183 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
184 || !POINTER_TYPE_P (TREE_TYPE (ptr))
185 || (TREE_CODE (decl) != VAR_DECL
186 && TREE_CODE (decl) != PARM_DECL
187 && TREE_CODE (decl) != RESULT_DECL))
188 return true;
190 /* Disregard pointer offsetting. */
191 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
195 ptr = TREE_OPERAND (ptr, 0);
197 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
198 return ptr_deref_may_alias_decl_p (ptr, decl);
201 /* ADDR_EXPR pointers either just offset another pointer or directly
202 specify the pointed-to set. */
203 if (TREE_CODE (ptr) == ADDR_EXPR)
205 tree base = get_base_address (TREE_OPERAND (ptr, 0));
206 if (base
207 && (TREE_CODE (base) == MEM_REF
208 || TREE_CODE (base) == TARGET_MEM_REF))
209 ptr = TREE_OPERAND (base, 0);
210 else if (base
211 && DECL_P (base))
212 return base == decl;
213 else if (base
214 && CONSTANT_CLASS_P (base))
215 return false;
216 else
217 return true;
220 /* Non-aliased variables can not be pointed to. */
221 if (!may_be_aliased (decl))
222 return false;
224 /* If we do not have useful points-to information for this pointer
225 we cannot disambiguate anything else. */
226 pi = SSA_NAME_PTR_INFO (ptr);
227 if (!pi)
228 return true;
230 return pt_solution_includes (&pi->pt, decl);
233 /* Return true if dereferenced PTR1 and PTR2 may alias.
234 The caller is responsible for applying TBAA to see if accesses
235 through PTR1 and PTR2 may conflict at all. */
237 bool
238 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
240 struct ptr_info_def *pi1, *pi2;
242 /* Conversions are irrelevant for points-to information and
243 data-dependence analysis can feed us those. */
244 STRIP_NOPS (ptr1);
245 STRIP_NOPS (ptr2);
247 /* Disregard pointer offsetting. */
248 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
252 ptr1 = TREE_OPERAND (ptr1, 0);
254 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
255 return ptr_derefs_may_alias_p (ptr1, ptr2);
257 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
261 ptr2 = TREE_OPERAND (ptr2, 0);
263 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
264 return ptr_derefs_may_alias_p (ptr1, ptr2);
267 /* ADDR_EXPR pointers either just offset another pointer or directly
268 specify the pointed-to set. */
269 if (TREE_CODE (ptr1) == ADDR_EXPR)
271 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
272 if (base
273 && (TREE_CODE (base) == MEM_REF
274 || TREE_CODE (base) == TARGET_MEM_REF))
275 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
276 else if (base
277 && DECL_P (base))
278 return ptr_deref_may_alias_decl_p (ptr2, base);
279 else
280 return true;
282 if (TREE_CODE (ptr2) == ADDR_EXPR)
284 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
285 if (base
286 && (TREE_CODE (base) == MEM_REF
287 || TREE_CODE (base) == TARGET_MEM_REF))
288 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
289 else if (base
290 && DECL_P (base))
291 return ptr_deref_may_alias_decl_p (ptr1, base);
292 else
293 return true;
296 /* From here we require SSA name pointers. Anything else aliases. */
297 if (TREE_CODE (ptr1) != SSA_NAME
298 || TREE_CODE (ptr2) != SSA_NAME
299 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
300 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
301 return true;
303 /* We may end up with two empty points-to solutions for two same pointers.
304 In this case we still want to say both pointers alias, so shortcut
305 that here. */
306 if (ptr1 == ptr2)
307 return true;
309 /* If we do not have useful points-to information for either pointer
310 we cannot disambiguate anything else. */
311 pi1 = SSA_NAME_PTR_INFO (ptr1);
312 pi2 = SSA_NAME_PTR_INFO (ptr2);
313 if (!pi1 || !pi2)
314 return true;
316 /* ??? This does not use TBAA to prune decls from the intersection
317 that not both pointers may access. */
318 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
321 /* Return true if dereferencing PTR may alias *REF.
322 The caller is responsible for applying TBAA to see if PTR
323 may access *REF at all. */
325 static bool
326 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
328 tree base = ao_ref_base (ref);
330 if (TREE_CODE (base) == MEM_REF
331 || TREE_CODE (base) == TARGET_MEM_REF)
332 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
333 else if (DECL_P (base))
334 return ptr_deref_may_alias_decl_p (ptr, base);
336 return true;
339 /* Returns whether reference REF to BASE may refer to global memory. */
341 static bool
342 ref_may_alias_global_p_1 (tree base)
344 if (DECL_P (base))
345 return is_global_var (base);
346 else if (TREE_CODE (base) == MEM_REF
347 || TREE_CODE (base) == TARGET_MEM_REF)
348 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
349 return true;
352 bool
353 ref_may_alias_global_p (ao_ref *ref)
355 tree base = ao_ref_base (ref);
356 return ref_may_alias_global_p_1 (base);
359 bool
360 ref_may_alias_global_p (tree ref)
362 tree base = get_base_address (ref);
363 return ref_may_alias_global_p_1 (base);
366 /* Return true whether STMT may clobber global memory. */
368 bool
369 stmt_may_clobber_global_p (gimple stmt)
371 tree lhs;
373 if (!gimple_vdef (stmt))
374 return false;
376 /* ??? We can ask the oracle whether an artificial pointer
377 dereference with a pointer with points-to information covering
378 all global memory (what about non-address taken memory?) maybe
379 clobbered by this call. As there is at the moment no convenient
380 way of doing that without generating garbage do some manual
381 checking instead.
382 ??? We could make a NULL ao_ref argument to the various
383 predicates special, meaning any global memory. */
385 switch (gimple_code (stmt))
387 case GIMPLE_ASSIGN:
388 lhs = gimple_assign_lhs (stmt);
389 return (TREE_CODE (lhs) != SSA_NAME
390 && ref_may_alias_global_p (lhs));
391 case GIMPLE_CALL:
392 return true;
393 default:
394 return true;
399 /* Dump alias information on FILE. */
401 void
402 dump_alias_info (FILE *file)
404 unsigned i;
405 const char *funcname
406 = lang_hooks.decl_printable_name (current_function_decl, 2);
407 tree var;
409 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
411 fprintf (file, "Aliased symbols\n\n");
413 FOR_EACH_LOCAL_DECL (cfun, i, var)
415 if (may_be_aliased (var))
416 dump_variable (file, var);
419 fprintf (file, "\nCall clobber information\n");
421 fprintf (file, "\nESCAPED");
422 dump_points_to_solution (file, &cfun->gimple_df->escaped);
424 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
426 for (i = 1; i < num_ssa_names; i++)
428 tree ptr = ssa_name (i);
429 struct ptr_info_def *pi;
431 if (ptr == NULL_TREE
432 || !POINTER_TYPE_P (TREE_TYPE (ptr))
433 || SSA_NAME_IN_FREE_LIST (ptr))
434 continue;
436 pi = SSA_NAME_PTR_INFO (ptr);
437 if (pi)
438 dump_points_to_info_for (file, ptr);
441 fprintf (file, "\n");
445 /* Dump alias information on stderr. */
447 DEBUG_FUNCTION void
448 debug_alias_info (void)
450 dump_alias_info (stderr);
454 /* Dump the points-to set *PT into FILE. */
456 void
457 dump_points_to_solution (FILE *file, struct pt_solution *pt)
459 if (pt->anything)
460 fprintf (file, ", points-to anything");
462 if (pt->nonlocal)
463 fprintf (file, ", points-to non-local");
465 if (pt->escaped)
466 fprintf (file, ", points-to escaped");
468 if (pt->ipa_escaped)
469 fprintf (file, ", points-to unit escaped");
471 if (pt->null)
472 fprintf (file, ", points-to NULL");
474 if (pt->vars)
476 fprintf (file, ", points-to vars: ");
477 dump_decl_set (file, pt->vars);
478 if (pt->vars_contains_nonlocal
479 && pt->vars_contains_escaped_heap)
480 fprintf (file, " (nonlocal, escaped heap)");
481 else if (pt->vars_contains_nonlocal
482 && pt->vars_contains_escaped)
483 fprintf (file, " (nonlocal, escaped)");
484 else if (pt->vars_contains_nonlocal)
485 fprintf (file, " (nonlocal)");
486 else if (pt->vars_contains_escaped_heap)
487 fprintf (file, " (escaped heap)");
488 else if (pt->vars_contains_escaped)
489 fprintf (file, " (escaped)");
494 /* Unified dump function for pt_solution. */
496 DEBUG_FUNCTION void
497 debug (pt_solution &ref)
499 dump_points_to_solution (stderr, &ref);
502 DEBUG_FUNCTION void
503 debug (pt_solution *ptr)
505 if (ptr)
506 debug (*ptr);
507 else
508 fprintf (stderr, "<nil>\n");
512 /* Dump points-to information for SSA_NAME PTR into FILE. */
514 void
515 dump_points_to_info_for (FILE *file, tree ptr)
517 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
519 print_generic_expr (file, ptr, dump_flags);
521 if (pi)
522 dump_points_to_solution (file, &pi->pt);
523 else
524 fprintf (file, ", points-to anything");
526 fprintf (file, "\n");
530 /* Dump points-to information for VAR into stderr. */
532 DEBUG_FUNCTION void
533 debug_points_to_info_for (tree var)
535 dump_points_to_info_for (stderr, var);
539 /* Initializes the alias-oracle reference representation *R from REF. */
541 void
542 ao_ref_init (ao_ref *r, tree ref)
544 r->ref = ref;
545 r->base = NULL_TREE;
546 r->offset = 0;
547 r->size = -1;
548 r->max_size = -1;
549 r->ref_alias_set = -1;
550 r->base_alias_set = -1;
551 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
554 /* Returns the base object of the memory reference *REF. */
556 tree
557 ao_ref_base (ao_ref *ref)
559 if (ref->base)
560 return ref->base;
561 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
562 &ref->max_size);
563 return ref->base;
566 /* Returns the base object alias set of the memory reference *REF. */
568 alias_set_type
569 ao_ref_base_alias_set (ao_ref *ref)
571 tree base_ref;
572 if (ref->base_alias_set != -1)
573 return ref->base_alias_set;
574 if (!ref->ref)
575 return 0;
576 base_ref = ref->ref;
577 while (handled_component_p (base_ref))
578 base_ref = TREE_OPERAND (base_ref, 0);
579 ref->base_alias_set = get_alias_set (base_ref);
580 return ref->base_alias_set;
583 /* Returns the reference alias set of the memory reference *REF. */
585 alias_set_type
586 ao_ref_alias_set (ao_ref *ref)
588 if (ref->ref_alias_set != -1)
589 return ref->ref_alias_set;
590 ref->ref_alias_set = get_alias_set (ref->ref);
591 return ref->ref_alias_set;
594 /* Init an alias-oracle reference representation from a gimple pointer
595 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
596 size is assumed to be unknown. The access is assumed to be only
597 to or after of the pointer target, not before it. */
599 void
600 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
602 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
603 ref->ref = NULL_TREE;
604 if (TREE_CODE (ptr) == SSA_NAME)
606 gimple stmt = SSA_NAME_DEF_STMT (ptr);
607 if (gimple_assign_single_p (stmt)
608 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
609 ptr = gimple_assign_rhs1 (stmt);
610 else if (is_gimple_assign (stmt)
611 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
612 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
614 ptr = gimple_assign_rhs1 (stmt);
615 extra_offset = BITS_PER_UNIT
616 * int_cst_value (gimple_assign_rhs2 (stmt));
620 if (TREE_CODE (ptr) == ADDR_EXPR)
622 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
623 if (ref->base)
624 ref->offset = BITS_PER_UNIT * t;
625 else
627 size = NULL_TREE;
628 ref->offset = 0;
629 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
632 else
634 ref->base = build2 (MEM_REF, char_type_node,
635 ptr, null_pointer_node);
636 ref->offset = 0;
638 ref->offset += extra_offset;
639 if (size
640 && tree_fits_shwi_p (size)
641 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
642 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
643 else
644 ref->max_size = ref->size = -1;
645 ref->ref_alias_set = 0;
646 ref->base_alias_set = 0;
647 ref->volatile_p = false;
650 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
651 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
652 decide. */
654 static inline int
655 same_type_for_tbaa (tree type1, tree type2)
657 type1 = TYPE_MAIN_VARIANT (type1);
658 type2 = TYPE_MAIN_VARIANT (type2);
660 /* If we would have to do structural comparison bail out. */
661 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
662 || TYPE_STRUCTURAL_EQUALITY_P (type2))
663 return -1;
665 /* Compare the canonical types. */
666 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
667 return 1;
669 /* ??? Array types are not properly unified in all cases as we have
670 spurious changes in the index types for example. Removing this
671 causes all sorts of problems with the Fortran frontend. */
672 if (TREE_CODE (type1) == ARRAY_TYPE
673 && TREE_CODE (type2) == ARRAY_TYPE)
674 return -1;
676 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
677 object of one of its constrained subtypes, e.g. when a function with an
678 unconstrained parameter passed by reference is called on an object and
679 inlined. But, even in the case of a fixed size, type and subtypes are
680 not equivalent enough as to share the same TYPE_CANONICAL, since this
681 would mean that conversions between them are useless, whereas they are
682 not (e.g. type and subtypes can have different modes). So, in the end,
683 they are only guaranteed to have the same alias set. */
684 if (get_alias_set (type1) == get_alias_set (type2))
685 return -1;
687 /* The types are known to be not equal. */
688 return 0;
691 /* Determine if the two component references REF1 and REF2 which are
692 based on access types TYPE1 and TYPE2 and of which at least one is based
693 on an indirect reference may alias. REF2 is the only one that can
694 be a decl in which case REF2_IS_DECL is true.
695 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
696 are the respective alias sets. */
698 static bool
699 aliasing_component_refs_p (tree ref1,
700 alias_set_type ref1_alias_set,
701 alias_set_type base1_alias_set,
702 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
703 tree ref2,
704 alias_set_type ref2_alias_set,
705 alias_set_type base2_alias_set,
706 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
707 bool ref2_is_decl)
709 /* If one reference is a component references through pointers try to find a
710 common base and apply offset based disambiguation. This handles
711 for example
712 struct A { int i; int j; } *q;
713 struct B { struct A a; int k; } *p;
714 disambiguating q->i and p->a.j. */
715 tree base1, base2;
716 tree type1, type2;
717 tree *refp;
718 int same_p;
720 /* Choose bases and base types to search for. */
721 base1 = ref1;
722 while (handled_component_p (base1))
723 base1 = TREE_OPERAND (base1, 0);
724 type1 = TREE_TYPE (base1);
725 base2 = ref2;
726 while (handled_component_p (base2))
727 base2 = TREE_OPERAND (base2, 0);
728 type2 = TREE_TYPE (base2);
730 /* Now search for the type1 in the access path of ref2. This
731 would be a common base for doing offset based disambiguation on. */
732 refp = &ref2;
733 while (handled_component_p (*refp)
734 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
735 refp = &TREE_OPERAND (*refp, 0);
736 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
737 /* If we couldn't compare types we have to bail out. */
738 if (same_p == -1)
739 return true;
740 else if (same_p == 1)
742 HOST_WIDE_INT offadj, sztmp, msztmp;
743 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
744 offset2 -= offadj;
745 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
746 offset1 -= offadj;
747 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
749 /* If we didn't find a common base, try the other way around. */
750 refp = &ref1;
751 while (handled_component_p (*refp)
752 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
753 refp = &TREE_OPERAND (*refp, 0);
754 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
755 /* If we couldn't compare types we have to bail out. */
756 if (same_p == -1)
757 return true;
758 else if (same_p == 1)
760 HOST_WIDE_INT offadj, sztmp, msztmp;
761 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
762 offset1 -= offadj;
763 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
764 offset2 -= offadj;
765 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
768 /* If we have two type access paths B1.path1 and B2.path2 they may
769 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
770 But we can still have a path that goes B1.path1...B2.path2 with
771 a part that we do not see. So we can only disambiguate now
772 if there is no B2 in the tail of path1 and no B1 on the
773 tail of path2. */
774 if (base1_alias_set == ref2_alias_set
775 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
776 return true;
777 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
778 if (!ref2_is_decl)
779 return (base2_alias_set == ref1_alias_set
780 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
781 return false;
784 /* Return true if we can determine that component references REF1 and REF2,
785 that are within a common DECL, cannot overlap. */
787 static bool
788 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
790 auto_vec<tree, 16> component_refs1;
791 auto_vec<tree, 16> component_refs2;
793 /* Create the stack of handled components for REF1. */
794 while (handled_component_p (ref1))
796 component_refs1.safe_push (ref1);
797 ref1 = TREE_OPERAND (ref1, 0);
799 if (TREE_CODE (ref1) == MEM_REF)
801 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
802 goto may_overlap;
803 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
806 /* Create the stack of handled components for REF2. */
807 while (handled_component_p (ref2))
809 component_refs2.safe_push (ref2);
810 ref2 = TREE_OPERAND (ref2, 0);
812 if (TREE_CODE (ref2) == MEM_REF)
814 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
815 goto may_overlap;
816 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
819 /* We must have the same base DECL. */
820 gcc_assert (ref1 == ref2);
822 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
823 rank. This is sufficient because we start from the same DECL and you
824 cannot reference several fields at a time with COMPONENT_REFs (unlike
825 with ARRAY_RANGE_REFs for arrays) so you always need the same number
826 of them to access a sub-component, unless you're in a union, in which
827 case the return value will precisely be false. */
828 while (true)
832 if (component_refs1.is_empty ())
833 goto may_overlap;
834 ref1 = component_refs1.pop ();
836 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
840 if (component_refs2.is_empty ())
841 goto may_overlap;
842 ref2 = component_refs2.pop ();
844 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
846 /* Beware of BIT_FIELD_REF. */
847 if (TREE_CODE (ref1) != COMPONENT_REF
848 || TREE_CODE (ref2) != COMPONENT_REF)
849 goto may_overlap;
851 tree field1 = TREE_OPERAND (ref1, 1);
852 tree field2 = TREE_OPERAND (ref2, 1);
854 /* ??? We cannot simply use the type of operand #0 of the refs here
855 as the Fortran compiler smuggles type punning into COMPONENT_REFs
856 for common blocks instead of using unions like everyone else. */
857 tree type1 = DECL_CONTEXT (field1);
858 tree type2 = DECL_CONTEXT (field2);
860 /* We cannot disambiguate fields in a union or qualified union. */
861 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
862 goto may_overlap;
864 /* Different fields of the same record type cannot overlap.
865 ??? Bitfields can overlap at RTL level so punt on them. */
866 if (field1 != field2)
868 component_refs1.release ();
869 component_refs2.release ();
870 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
874 may_overlap:
875 component_refs1.release ();
876 component_refs2.release ();
877 return false;
880 /* qsort compare function to sort FIELD_DECLs after their
881 DECL_FIELD_CONTEXT TYPE_UID. */
883 static inline int
884 ncr_compar (const void *field1_, const void *field2_)
886 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
887 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
888 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
889 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
890 if (uid1 < uid2)
891 return -1;
892 else if (uid1 > uid2)
893 return 1;
894 return 0;
897 /* Return true if we can determine that the fields referenced cannot
898 overlap for any pair of objects. */
900 static bool
901 nonoverlapping_component_refs_p (const_tree x, const_tree y)
903 if (!flag_strict_aliasing
904 || !x || !y
905 || TREE_CODE (x) != COMPONENT_REF
906 || TREE_CODE (y) != COMPONENT_REF)
907 return false;
909 auto_vec<const_tree, 16> fieldsx;
910 while (TREE_CODE (x) == COMPONENT_REF)
912 tree field = TREE_OPERAND (x, 1);
913 tree type = DECL_FIELD_CONTEXT (field);
914 if (TREE_CODE (type) == RECORD_TYPE)
915 fieldsx.safe_push (field);
916 x = TREE_OPERAND (x, 0);
918 if (fieldsx.length () == 0)
919 return false;
920 auto_vec<const_tree, 16> fieldsy;
921 while (TREE_CODE (y) == COMPONENT_REF)
923 tree field = TREE_OPERAND (y, 1);
924 tree type = DECL_FIELD_CONTEXT (field);
925 if (TREE_CODE (type) == RECORD_TYPE)
926 fieldsy.safe_push (TREE_OPERAND (y, 1));
927 y = TREE_OPERAND (y, 0);
929 if (fieldsy.length () == 0)
930 return false;
932 /* Most common case first. */
933 if (fieldsx.length () == 1
934 && fieldsy.length () == 1)
935 return ((DECL_FIELD_CONTEXT (fieldsx[0])
936 == DECL_FIELD_CONTEXT (fieldsy[0]))
937 && fieldsx[0] != fieldsy[0]
938 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
940 if (fieldsx.length () == 2)
942 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
944 const_tree tem = fieldsx[0];
945 fieldsx[0] = fieldsx[1];
946 fieldsx[1] = tem;
949 else
950 fieldsx.qsort (ncr_compar);
952 if (fieldsy.length () == 2)
954 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
956 const_tree tem = fieldsy[0];
957 fieldsy[0] = fieldsy[1];
958 fieldsy[1] = tem;
961 else
962 fieldsy.qsort (ncr_compar);
964 unsigned i = 0, j = 0;
967 const_tree fieldx = fieldsx[i];
968 const_tree fieldy = fieldsy[j];
969 tree typex = DECL_FIELD_CONTEXT (fieldx);
970 tree typey = DECL_FIELD_CONTEXT (fieldy);
971 if (typex == typey)
973 /* We're left with accessing different fields of a structure,
974 no possible overlap, unless they are both bitfields. */
975 if (fieldx != fieldy)
976 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
978 if (TYPE_UID (typex) < TYPE_UID (typey))
980 i++;
981 if (i == fieldsx.length ())
982 break;
984 else
986 j++;
987 if (j == fieldsy.length ())
988 break;
991 while (1);
993 return false;
997 /* Return true if two memory references based on the variables BASE1
998 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
999 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1000 if non-NULL are the complete memory reference trees. */
1002 static bool
1003 decl_refs_may_alias_p (tree ref1, tree base1,
1004 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1005 tree ref2, tree base2,
1006 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1008 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1010 /* If both references are based on different variables, they cannot alias. */
1011 if (base1 != base2)
1012 return false;
1014 /* If both references are based on the same variable, they cannot alias if
1015 the accesses do not overlap. */
1016 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1017 return false;
1019 /* For components with variable position, the above test isn't sufficient,
1020 so we disambiguate component references manually. */
1021 if (ref1 && ref2
1022 && handled_component_p (ref1) && handled_component_p (ref2)
1023 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1024 return false;
1026 return true;
1029 /* Return true if an indirect reference based on *PTR1 constrained
1030 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1031 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1032 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1033 in which case they are computed on-demand. REF1 and REF2
1034 if non-NULL are the complete memory reference trees. */
1036 static bool
1037 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1038 HOST_WIDE_INT offset1,
1039 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1040 alias_set_type ref1_alias_set,
1041 alias_set_type base1_alias_set,
1042 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1043 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1044 alias_set_type ref2_alias_set,
1045 alias_set_type base2_alias_set, bool tbaa_p)
1047 tree ptr1;
1048 tree ptrtype1, dbase2;
1049 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1050 HOST_WIDE_INT doffset1, doffset2;
1052 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1053 || TREE_CODE (base1) == TARGET_MEM_REF)
1054 && DECL_P (base2));
1056 ptr1 = TREE_OPERAND (base1, 0);
1058 /* The offset embedded in MEM_REFs can be negative. Bias them
1059 so that the resulting offset adjustment is positive. */
1060 offset_int moff = mem_ref_offset (base1);
1061 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1062 if (wi::neg_p (moff))
1063 offset2p += (-moff).to_short_addr ();
1064 else
1065 offset1p += moff.to_short_addr ();
1067 /* If only one reference is based on a variable, they cannot alias if
1068 the pointer access is beyond the extent of the variable access.
1069 (the pointer base cannot validly point to an offset less than zero
1070 of the variable).
1071 ??? IVOPTs creates bases that do not honor this restriction,
1072 so do not apply this optimization for TARGET_MEM_REFs. */
1073 if (TREE_CODE (base1) != TARGET_MEM_REF
1074 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1075 return false;
1076 /* They also cannot alias if the pointer may not point to the decl. */
1077 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1078 return false;
1080 /* Disambiguations that rely on strict aliasing rules follow. */
1081 if (!flag_strict_aliasing || !tbaa_p)
1082 return true;
1084 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1086 /* If the alias set for a pointer access is zero all bets are off. */
1087 if (base1_alias_set == -1)
1088 base1_alias_set = get_deref_alias_set (ptrtype1);
1089 if (base1_alias_set == 0)
1090 return true;
1091 if (base2_alias_set == -1)
1092 base2_alias_set = get_alias_set (base2);
1094 /* When we are trying to disambiguate an access with a pointer dereference
1095 as base versus one with a decl as base we can use both the size
1096 of the decl and its dynamic type for extra disambiguation.
1097 ??? We do not know anything about the dynamic type of the decl
1098 other than that its alias-set contains base2_alias_set as a subset
1099 which does not help us here. */
1100 /* As we know nothing useful about the dynamic type of the decl just
1101 use the usual conflict check rather than a subset test.
1102 ??? We could introduce -fvery-strict-aliasing when the language
1103 does not allow decls to have a dynamic type that differs from their
1104 static type. Then we can check
1105 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1106 if (base1_alias_set != base2_alias_set
1107 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1108 return false;
1109 /* If the size of the access relevant for TBAA through the pointer
1110 is bigger than the size of the decl we can't possibly access the
1111 decl via that pointer. */
1112 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1113 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1114 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1115 /* ??? This in turn may run afoul when a decl of type T which is
1116 a member of union type U is accessed through a pointer to
1117 type U and sizeof T is smaller than sizeof U. */
1118 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1119 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1120 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1121 return false;
1123 if (!ref2)
1124 return true;
1126 /* If the decl is accessed via a MEM_REF, reconstruct the base
1127 we can use for TBAA and an appropriately adjusted offset. */
1128 dbase2 = ref2;
1129 while (handled_component_p (dbase2))
1130 dbase2 = TREE_OPERAND (dbase2, 0);
1131 doffset1 = offset1;
1132 doffset2 = offset2;
1133 if (TREE_CODE (dbase2) == MEM_REF
1134 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1136 offset_int moff = mem_ref_offset (dbase2);
1137 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1138 if (wi::neg_p (moff))
1139 doffset1 -= (-moff).to_short_addr ();
1140 else
1141 doffset2 -= moff.to_short_addr ();
1144 /* If either reference is view-converted, give up now. */
1145 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1146 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1147 return true;
1149 /* If both references are through the same type, they do not alias
1150 if the accesses do not overlap. This does extra disambiguation
1151 for mixed/pointer accesses but requires strict aliasing.
1152 For MEM_REFs we require that the component-ref offset we computed
1153 is relative to the start of the type which we ensure by
1154 comparing rvalue and access type and disregarding the constant
1155 pointer offset. */
1156 if ((TREE_CODE (base1) != TARGET_MEM_REF
1157 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1158 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1159 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1161 if (ref1 && ref2
1162 && nonoverlapping_component_refs_p (ref1, ref2))
1163 return false;
1165 /* Do access-path based disambiguation. */
1166 if (ref1 && ref2
1167 && (handled_component_p (ref1) || handled_component_p (ref2)))
1168 return aliasing_component_refs_p (ref1,
1169 ref1_alias_set, base1_alias_set,
1170 offset1, max_size1,
1171 ref2,
1172 ref2_alias_set, base2_alias_set,
1173 offset2, max_size2, true);
1175 return true;
1178 /* Return true if two indirect references based on *PTR1
1179 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1180 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1181 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1182 in which case they are computed on-demand. REF1 and REF2
1183 if non-NULL are the complete memory reference trees. */
1185 static bool
1186 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1187 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1188 alias_set_type ref1_alias_set,
1189 alias_set_type base1_alias_set,
1190 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1191 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1192 alias_set_type ref2_alias_set,
1193 alias_set_type base2_alias_set, bool tbaa_p)
1195 tree ptr1;
1196 tree ptr2;
1197 tree ptrtype1, ptrtype2;
1199 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1200 || TREE_CODE (base1) == TARGET_MEM_REF)
1201 && (TREE_CODE (base2) == MEM_REF
1202 || TREE_CODE (base2) == TARGET_MEM_REF));
1204 ptr1 = TREE_OPERAND (base1, 0);
1205 ptr2 = TREE_OPERAND (base2, 0);
1207 /* If both bases are based on pointers they cannot alias if they may not
1208 point to the same memory object or if they point to the same object
1209 and the accesses do not overlap. */
1210 if ((!cfun || gimple_in_ssa_p (cfun))
1211 && operand_equal_p (ptr1, ptr2, 0)
1212 && (((TREE_CODE (base1) != TARGET_MEM_REF
1213 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1214 && (TREE_CODE (base2) != TARGET_MEM_REF
1215 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1216 || (TREE_CODE (base1) == TARGET_MEM_REF
1217 && TREE_CODE (base2) == TARGET_MEM_REF
1218 && (TMR_STEP (base1) == TMR_STEP (base2)
1219 || (TMR_STEP (base1) && TMR_STEP (base2)
1220 && operand_equal_p (TMR_STEP (base1),
1221 TMR_STEP (base2), 0)))
1222 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1223 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1224 && operand_equal_p (TMR_INDEX (base1),
1225 TMR_INDEX (base2), 0)))
1226 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1227 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1228 && operand_equal_p (TMR_INDEX2 (base1),
1229 TMR_INDEX2 (base2), 0))))))
1231 offset_int moff;
1232 /* The offset embedded in MEM_REFs can be negative. Bias them
1233 so that the resulting offset adjustment is positive. */
1234 moff = mem_ref_offset (base1);
1235 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1236 if (wi::neg_p (moff))
1237 offset2 += (-moff).to_short_addr ();
1238 else
1239 offset1 += moff.to_shwi ();
1240 moff = mem_ref_offset (base2);
1241 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1242 if (wi::neg_p (moff))
1243 offset1 += (-moff).to_short_addr ();
1244 else
1245 offset2 += moff.to_short_addr ();
1246 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1248 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1249 return false;
1251 /* Disambiguations that rely on strict aliasing rules follow. */
1252 if (!flag_strict_aliasing || !tbaa_p)
1253 return true;
1255 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1256 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1258 /* If the alias set for a pointer access is zero all bets are off. */
1259 if (base1_alias_set == -1)
1260 base1_alias_set = get_deref_alias_set (ptrtype1);
1261 if (base1_alias_set == 0)
1262 return true;
1263 if (base2_alias_set == -1)
1264 base2_alias_set = get_deref_alias_set (ptrtype2);
1265 if (base2_alias_set == 0)
1266 return true;
1268 /* If both references are through the same type, they do not alias
1269 if the accesses do not overlap. This does extra disambiguation
1270 for mixed/pointer accesses but requires strict aliasing. */
1271 if ((TREE_CODE (base1) != TARGET_MEM_REF
1272 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1273 && (TREE_CODE (base2) != TARGET_MEM_REF
1274 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1275 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1276 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1277 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1278 TREE_TYPE (ptrtype2)) == 1)
1279 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1281 /* Do type-based disambiguation. */
1282 if (base1_alias_set != base2_alias_set
1283 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1284 return false;
1286 /* If either reference is view-converted, give up now. */
1287 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1288 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1289 return true;
1291 if (ref1 && ref2
1292 && nonoverlapping_component_refs_p (ref1, ref2))
1293 return false;
1295 /* Do access-path based disambiguation. */
1296 if (ref1 && ref2
1297 && (handled_component_p (ref1) || handled_component_p (ref2)))
1298 return aliasing_component_refs_p (ref1,
1299 ref1_alias_set, base1_alias_set,
1300 offset1, max_size1,
1301 ref2,
1302 ref2_alias_set, base2_alias_set,
1303 offset2, max_size2, false);
1305 return true;
1308 /* Return true, if the two memory references REF1 and REF2 may alias. */
1310 bool
1311 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1313 tree base1, base2;
1314 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1315 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1316 bool var1_p, var2_p, ind1_p, ind2_p;
1318 gcc_checking_assert ((!ref1->ref
1319 || TREE_CODE (ref1->ref) == SSA_NAME
1320 || DECL_P (ref1->ref)
1321 || TREE_CODE (ref1->ref) == STRING_CST
1322 || handled_component_p (ref1->ref)
1323 || TREE_CODE (ref1->ref) == MEM_REF
1324 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1325 && (!ref2->ref
1326 || TREE_CODE (ref2->ref) == SSA_NAME
1327 || DECL_P (ref2->ref)
1328 || TREE_CODE (ref2->ref) == STRING_CST
1329 || handled_component_p (ref2->ref)
1330 || TREE_CODE (ref2->ref) == MEM_REF
1331 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1333 /* Decompose the references into their base objects and the access. */
1334 base1 = ao_ref_base (ref1);
1335 offset1 = ref1->offset;
1336 max_size1 = ref1->max_size;
1337 base2 = ao_ref_base (ref2);
1338 offset2 = ref2->offset;
1339 max_size2 = ref2->max_size;
1341 /* We can end up with registers or constants as bases for example from
1342 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1343 which is seen as a struct copy. */
1344 if (TREE_CODE (base1) == SSA_NAME
1345 || TREE_CODE (base1) == CONST_DECL
1346 || TREE_CODE (base1) == CONSTRUCTOR
1347 || TREE_CODE (base1) == ADDR_EXPR
1348 || CONSTANT_CLASS_P (base1)
1349 || TREE_CODE (base2) == SSA_NAME
1350 || TREE_CODE (base2) == CONST_DECL
1351 || TREE_CODE (base2) == CONSTRUCTOR
1352 || TREE_CODE (base2) == ADDR_EXPR
1353 || CONSTANT_CLASS_P (base2))
1354 return false;
1356 /* We can end up referring to code via function and label decls.
1357 As we likely do not properly track code aliases conservatively
1358 bail out. */
1359 if (TREE_CODE (base1) == FUNCTION_DECL
1360 || TREE_CODE (base1) == LABEL_DECL
1361 || TREE_CODE (base2) == FUNCTION_DECL
1362 || TREE_CODE (base2) == LABEL_DECL)
1363 return true;
1365 /* Two volatile accesses always conflict. */
1366 if (ref1->volatile_p
1367 && ref2->volatile_p)
1368 return true;
1370 /* Defer to simple offset based disambiguation if we have
1371 references based on two decls. Do this before defering to
1372 TBAA to handle must-alias cases in conformance with the
1373 GCC extension of allowing type-punning through unions. */
1374 var1_p = DECL_P (base1);
1375 var2_p = DECL_P (base2);
1376 if (var1_p && var2_p)
1377 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1378 ref2->ref, base2, offset2, max_size2);
1380 ind1_p = (TREE_CODE (base1) == MEM_REF
1381 || TREE_CODE (base1) == TARGET_MEM_REF);
1382 ind2_p = (TREE_CODE (base2) == MEM_REF
1383 || TREE_CODE (base2) == TARGET_MEM_REF);
1385 /* Canonicalize the pointer-vs-decl case. */
1386 if (ind1_p && var2_p)
1388 HOST_WIDE_INT tmp1;
1389 tree tmp2;
1390 ao_ref *tmp3;
1391 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1392 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1393 tmp2 = base1; base1 = base2; base2 = tmp2;
1394 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1395 var1_p = true;
1396 ind1_p = false;
1397 var2_p = false;
1398 ind2_p = true;
1401 /* First defer to TBAA if possible. */
1402 if (tbaa_p
1403 && flag_strict_aliasing
1404 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1405 ao_ref_alias_set (ref2)))
1406 return false;
1408 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1409 if (var1_p && ind2_p)
1410 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1411 offset2, max_size2,
1412 ao_ref_alias_set (ref2), -1,
1413 ref1->ref, base1,
1414 offset1, max_size1,
1415 ao_ref_alias_set (ref1),
1416 ao_ref_base_alias_set (ref1),
1417 tbaa_p);
1418 else if (ind1_p && ind2_p)
1419 return indirect_refs_may_alias_p (ref1->ref, base1,
1420 offset1, max_size1,
1421 ao_ref_alias_set (ref1), -1,
1422 ref2->ref, base2,
1423 offset2, max_size2,
1424 ao_ref_alias_set (ref2), -1,
1425 tbaa_p);
1427 /* We really do not want to end up here, but returning true is safe. */
1428 #ifdef ENABLE_CHECKING
1429 gcc_unreachable ();
1430 #else
1431 return true;
1432 #endif
1435 static bool
1436 refs_may_alias_p (tree ref1, ao_ref *ref2)
1438 ao_ref r1;
1439 ao_ref_init (&r1, ref1);
1440 return refs_may_alias_p_1 (&r1, ref2, true);
1443 bool
1444 refs_may_alias_p (tree ref1, tree ref2)
1446 ao_ref r1, r2;
1447 bool res;
1448 ao_ref_init (&r1, ref1);
1449 ao_ref_init (&r2, ref2);
1450 res = refs_may_alias_p_1 (&r1, &r2, true);
1451 if (res)
1452 ++alias_stats.refs_may_alias_p_may_alias;
1453 else
1454 ++alias_stats.refs_may_alias_p_no_alias;
1455 return res;
1458 /* Returns true if there is a anti-dependence for the STORE that
1459 executes after the LOAD. */
1461 bool
1462 refs_anti_dependent_p (tree load, tree store)
1464 ao_ref r1, r2;
1465 ao_ref_init (&r1, load);
1466 ao_ref_init (&r2, store);
1467 return refs_may_alias_p_1 (&r1, &r2, false);
1470 /* Returns true if there is a output dependence for the stores
1471 STORE1 and STORE2. */
1473 bool
1474 refs_output_dependent_p (tree store1, tree store2)
1476 ao_ref r1, r2;
1477 ao_ref_init (&r1, store1);
1478 ao_ref_init (&r2, store2);
1479 return refs_may_alias_p_1 (&r1, &r2, false);
1482 /* If the call CALL may use the memory reference REF return true,
1483 otherwise return false. */
1485 static bool
1486 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1488 tree base, callee;
1489 unsigned i;
1490 int flags = gimple_call_flags (call);
1492 /* Const functions without a static chain do not implicitly use memory. */
1493 if (!gimple_call_chain (call)
1494 && (flags & (ECF_CONST|ECF_NOVOPS)))
1495 goto process_args;
1497 base = ao_ref_base (ref);
1498 if (!base)
1499 return true;
1501 /* A call that is not without side-effects might involve volatile
1502 accesses and thus conflicts with all other volatile accesses. */
1503 if (ref->volatile_p)
1504 return true;
1506 /* If the reference is based on a decl that is not aliased the call
1507 cannot possibly use it. */
1508 if (DECL_P (base)
1509 && !may_be_aliased (base)
1510 /* But local statics can be used through recursion. */
1511 && !is_global_var (base))
1512 goto process_args;
1514 callee = gimple_call_fndecl (call);
1516 /* Handle those builtin functions explicitly that do not act as
1517 escape points. See tree-ssa-structalias.c:find_func_aliases
1518 for the list of builtins we might need to handle here. */
1519 if (callee != NULL_TREE
1520 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1521 switch (DECL_FUNCTION_CODE (callee))
1523 /* All the following functions read memory pointed to by
1524 their second argument. strcat/strncat additionally
1525 reads memory pointed to by the first argument. */
1526 case BUILT_IN_STRCAT:
1527 case BUILT_IN_STRNCAT:
1529 ao_ref dref;
1530 ao_ref_init_from_ptr_and_size (&dref,
1531 gimple_call_arg (call, 0),
1532 NULL_TREE);
1533 if (refs_may_alias_p_1 (&dref, ref, false))
1534 return true;
1536 /* FALLTHRU */
1537 case BUILT_IN_STRCPY:
1538 case BUILT_IN_STRNCPY:
1539 case BUILT_IN_MEMCPY:
1540 case BUILT_IN_MEMMOVE:
1541 case BUILT_IN_MEMPCPY:
1542 case BUILT_IN_STPCPY:
1543 case BUILT_IN_STPNCPY:
1544 case BUILT_IN_TM_MEMCPY:
1545 case BUILT_IN_TM_MEMMOVE:
1547 ao_ref dref;
1548 tree size = NULL_TREE;
1549 if (gimple_call_num_args (call) == 3)
1550 size = gimple_call_arg (call, 2);
1551 ao_ref_init_from_ptr_and_size (&dref,
1552 gimple_call_arg (call, 1),
1553 size);
1554 return refs_may_alias_p_1 (&dref, ref, false);
1556 case BUILT_IN_STRCAT_CHK:
1557 case BUILT_IN_STRNCAT_CHK:
1559 ao_ref dref;
1560 ao_ref_init_from_ptr_and_size (&dref,
1561 gimple_call_arg (call, 0),
1562 NULL_TREE);
1563 if (refs_may_alias_p_1 (&dref, ref, false))
1564 return true;
1566 /* FALLTHRU */
1567 case BUILT_IN_STRCPY_CHK:
1568 case BUILT_IN_STRNCPY_CHK:
1569 case BUILT_IN_MEMCPY_CHK:
1570 case BUILT_IN_MEMMOVE_CHK:
1571 case BUILT_IN_MEMPCPY_CHK:
1572 case BUILT_IN_STPCPY_CHK:
1573 case BUILT_IN_STPNCPY_CHK:
1575 ao_ref dref;
1576 tree size = NULL_TREE;
1577 if (gimple_call_num_args (call) == 4)
1578 size = gimple_call_arg (call, 2);
1579 ao_ref_init_from_ptr_and_size (&dref,
1580 gimple_call_arg (call, 1),
1581 size);
1582 return refs_may_alias_p_1 (&dref, ref, false);
1584 case BUILT_IN_BCOPY:
1586 ao_ref dref;
1587 tree size = gimple_call_arg (call, 2);
1588 ao_ref_init_from_ptr_and_size (&dref,
1589 gimple_call_arg (call, 0),
1590 size);
1591 return refs_may_alias_p_1 (&dref, ref, false);
1594 /* The following functions read memory pointed to by their
1595 first argument. */
1596 CASE_BUILT_IN_TM_LOAD (1):
1597 CASE_BUILT_IN_TM_LOAD (2):
1598 CASE_BUILT_IN_TM_LOAD (4):
1599 CASE_BUILT_IN_TM_LOAD (8):
1600 CASE_BUILT_IN_TM_LOAD (FLOAT):
1601 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1602 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1603 CASE_BUILT_IN_TM_LOAD (M64):
1604 CASE_BUILT_IN_TM_LOAD (M128):
1605 CASE_BUILT_IN_TM_LOAD (M256):
1606 case BUILT_IN_TM_LOG:
1607 case BUILT_IN_TM_LOG_1:
1608 case BUILT_IN_TM_LOG_2:
1609 case BUILT_IN_TM_LOG_4:
1610 case BUILT_IN_TM_LOG_8:
1611 case BUILT_IN_TM_LOG_FLOAT:
1612 case BUILT_IN_TM_LOG_DOUBLE:
1613 case BUILT_IN_TM_LOG_LDOUBLE:
1614 case BUILT_IN_TM_LOG_M64:
1615 case BUILT_IN_TM_LOG_M128:
1616 case BUILT_IN_TM_LOG_M256:
1617 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1619 /* These read memory pointed to by the first argument. */
1620 case BUILT_IN_STRDUP:
1621 case BUILT_IN_STRNDUP:
1622 case BUILT_IN_REALLOC:
1624 ao_ref dref;
1625 tree size = NULL_TREE;
1626 if (gimple_call_num_args (call) == 2)
1627 size = gimple_call_arg (call, 1);
1628 ao_ref_init_from_ptr_and_size (&dref,
1629 gimple_call_arg (call, 0),
1630 size);
1631 return refs_may_alias_p_1 (&dref, ref, false);
1633 /* These read memory pointed to by the first argument. */
1634 case BUILT_IN_INDEX:
1635 case BUILT_IN_STRCHR:
1636 case BUILT_IN_STRRCHR:
1638 ao_ref dref;
1639 ao_ref_init_from_ptr_and_size (&dref,
1640 gimple_call_arg (call, 0),
1641 NULL_TREE);
1642 return refs_may_alias_p_1 (&dref, ref, false);
1644 /* These read memory pointed to by the first argument with size
1645 in the third argument. */
1646 case BUILT_IN_MEMCHR:
1648 ao_ref dref;
1649 ao_ref_init_from_ptr_and_size (&dref,
1650 gimple_call_arg (call, 0),
1651 gimple_call_arg (call, 2));
1652 return refs_may_alias_p_1 (&dref, ref, false);
1654 /* These read memory pointed to by the first and second arguments. */
1655 case BUILT_IN_STRSTR:
1656 case BUILT_IN_STRPBRK:
1658 ao_ref dref;
1659 ao_ref_init_from_ptr_and_size (&dref,
1660 gimple_call_arg (call, 0),
1661 NULL_TREE);
1662 if (refs_may_alias_p_1 (&dref, ref, false))
1663 return true;
1664 ao_ref_init_from_ptr_and_size (&dref,
1665 gimple_call_arg (call, 1),
1666 NULL_TREE);
1667 return refs_may_alias_p_1 (&dref, ref, false);
1670 /* The following builtins do not read from memory. */
1671 case BUILT_IN_FREE:
1672 case BUILT_IN_MALLOC:
1673 case BUILT_IN_POSIX_MEMALIGN:
1674 case BUILT_IN_ALIGNED_ALLOC:
1675 case BUILT_IN_CALLOC:
1676 case BUILT_IN_ALLOCA:
1677 case BUILT_IN_ALLOCA_WITH_ALIGN:
1678 case BUILT_IN_STACK_SAVE:
1679 case BUILT_IN_STACK_RESTORE:
1680 case BUILT_IN_MEMSET:
1681 case BUILT_IN_TM_MEMSET:
1682 case BUILT_IN_MEMSET_CHK:
1683 case BUILT_IN_FREXP:
1684 case BUILT_IN_FREXPF:
1685 case BUILT_IN_FREXPL:
1686 case BUILT_IN_GAMMA_R:
1687 case BUILT_IN_GAMMAF_R:
1688 case BUILT_IN_GAMMAL_R:
1689 case BUILT_IN_LGAMMA_R:
1690 case BUILT_IN_LGAMMAF_R:
1691 case BUILT_IN_LGAMMAL_R:
1692 case BUILT_IN_MODF:
1693 case BUILT_IN_MODFF:
1694 case BUILT_IN_MODFL:
1695 case BUILT_IN_REMQUO:
1696 case BUILT_IN_REMQUOF:
1697 case BUILT_IN_REMQUOL:
1698 case BUILT_IN_SINCOS:
1699 case BUILT_IN_SINCOSF:
1700 case BUILT_IN_SINCOSL:
1701 case BUILT_IN_ASSUME_ALIGNED:
1702 case BUILT_IN_VA_END:
1703 return false;
1704 /* __sync_* builtins and some OpenMP builtins act as threading
1705 barriers. */
1706 #undef DEF_SYNC_BUILTIN
1707 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1708 #include "sync-builtins.def"
1709 #undef DEF_SYNC_BUILTIN
1710 case BUILT_IN_GOMP_ATOMIC_START:
1711 case BUILT_IN_GOMP_ATOMIC_END:
1712 case BUILT_IN_GOMP_BARRIER:
1713 case BUILT_IN_GOMP_BARRIER_CANCEL:
1714 case BUILT_IN_GOMP_TASKWAIT:
1715 case BUILT_IN_GOMP_TASKGROUP_END:
1716 case BUILT_IN_GOMP_CRITICAL_START:
1717 case BUILT_IN_GOMP_CRITICAL_END:
1718 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1719 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1720 case BUILT_IN_GOMP_LOOP_END:
1721 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1722 case BUILT_IN_GOMP_ORDERED_START:
1723 case BUILT_IN_GOMP_ORDERED_END:
1724 case BUILT_IN_GOMP_SECTIONS_END:
1725 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1726 case BUILT_IN_GOMP_SINGLE_COPY_START:
1727 case BUILT_IN_GOMP_SINGLE_COPY_END:
1728 return true;
1730 default:
1731 /* Fallthru to general call handling. */;
1734 /* Check if base is a global static variable that is not read
1735 by the function. */
1736 if (callee != NULL_TREE
1737 && TREE_CODE (base) == VAR_DECL
1738 && TREE_STATIC (base))
1740 struct cgraph_node *node = cgraph_node::get (callee);
1741 bitmap not_read;
1743 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1744 node yet. We should enforce that there are nodes for all decls in the
1745 IL and remove this check instead. */
1746 if (node
1747 && (not_read = ipa_reference_get_not_read_global (node))
1748 && bitmap_bit_p (not_read, DECL_UID (base)))
1749 goto process_args;
1752 /* Check if the base variable is call-used. */
1753 if (DECL_P (base))
1755 if (pt_solution_includes (gimple_call_use_set (call), base))
1756 return true;
1758 else if ((TREE_CODE (base) == MEM_REF
1759 || TREE_CODE (base) == TARGET_MEM_REF)
1760 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1762 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1763 if (!pi)
1764 return true;
1766 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1767 return true;
1769 else
1770 return true;
1772 /* Inspect call arguments for passed-by-value aliases. */
1773 process_args:
1774 for (i = 0; i < gimple_call_num_args (call); ++i)
1776 tree op = gimple_call_arg (call, i);
1777 int flags = gimple_call_arg_flags (call, i);
1779 if (flags & EAF_UNUSED)
1780 continue;
1782 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1783 op = TREE_OPERAND (op, 0);
1785 if (TREE_CODE (op) != SSA_NAME
1786 && !is_gimple_min_invariant (op))
1788 ao_ref r;
1789 ao_ref_init (&r, op);
1790 if (refs_may_alias_p_1 (&r, ref, true))
1791 return true;
1795 return false;
1798 static bool
1799 ref_maybe_used_by_call_p (gimple call, ao_ref *ref)
1801 bool res;
1802 res = ref_maybe_used_by_call_p_1 (call, ref);
1803 if (res)
1804 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1805 else
1806 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1807 return res;
1811 /* If the statement STMT may use the memory reference REF return
1812 true, otherwise return false. */
1814 bool
1815 ref_maybe_used_by_stmt_p (gimple stmt, ao_ref *ref)
1817 if (is_gimple_assign (stmt))
1819 tree rhs;
1821 /* All memory assign statements are single. */
1822 if (!gimple_assign_single_p (stmt))
1823 return false;
1825 rhs = gimple_assign_rhs1 (stmt);
1826 if (is_gimple_reg (rhs)
1827 || is_gimple_min_invariant (rhs)
1828 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1829 return false;
1831 return refs_may_alias_p (rhs, ref);
1833 else if (is_gimple_call (stmt))
1834 return ref_maybe_used_by_call_p (stmt, ref);
1835 else if (gimple_code (stmt) == GIMPLE_RETURN)
1837 tree retval = gimple_return_retval (stmt);
1838 if (retval
1839 && TREE_CODE (retval) != SSA_NAME
1840 && !is_gimple_min_invariant (retval)
1841 && refs_may_alias_p (retval, ref))
1842 return true;
1843 /* If ref escapes the function then the return acts as a use. */
1844 tree base = ao_ref_base (ref);
1845 if (!base)
1847 else if (DECL_P (base))
1848 return is_global_var (base);
1849 else if (TREE_CODE (base) == MEM_REF
1850 || TREE_CODE (base) == TARGET_MEM_REF)
1851 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1852 return false;
1855 return true;
1858 bool
1859 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1861 ao_ref r;
1862 ao_ref_init (&r, ref);
1863 return ref_maybe_used_by_stmt_p (stmt, &r);
1866 /* If the call in statement CALL may clobber the memory reference REF
1867 return true, otherwise return false. */
1869 bool
1870 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1872 tree base;
1873 tree callee;
1875 /* If the call is pure or const it cannot clobber anything. */
1876 if (gimple_call_flags (call)
1877 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1878 return false;
1880 base = ao_ref_base (ref);
1881 if (!base)
1882 return true;
1884 if (TREE_CODE (base) == SSA_NAME
1885 || CONSTANT_CLASS_P (base))
1886 return false;
1888 /* A call that is not without side-effects might involve volatile
1889 accesses and thus conflicts with all other volatile accesses. */
1890 if (ref->volatile_p)
1891 return true;
1893 /* If the reference is based on a decl that is not aliased the call
1894 cannot possibly clobber it. */
1895 if (DECL_P (base)
1896 && !may_be_aliased (base)
1897 /* But local non-readonly statics can be modified through recursion
1898 or the call may implement a threading barrier which we must
1899 treat as may-def. */
1900 && (TREE_READONLY (base)
1901 || !is_global_var (base)))
1902 return false;
1904 callee = gimple_call_fndecl (call);
1906 /* Handle those builtin functions explicitly that do not act as
1907 escape points. See tree-ssa-structalias.c:find_func_aliases
1908 for the list of builtins we might need to handle here. */
1909 if (callee != NULL_TREE
1910 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1911 switch (DECL_FUNCTION_CODE (callee))
1913 /* All the following functions clobber memory pointed to by
1914 their first argument. */
1915 case BUILT_IN_STRCPY:
1916 case BUILT_IN_STRNCPY:
1917 case BUILT_IN_MEMCPY:
1918 case BUILT_IN_MEMMOVE:
1919 case BUILT_IN_MEMPCPY:
1920 case BUILT_IN_STPCPY:
1921 case BUILT_IN_STPNCPY:
1922 case BUILT_IN_STRCAT:
1923 case BUILT_IN_STRNCAT:
1924 case BUILT_IN_MEMSET:
1925 case BUILT_IN_TM_MEMSET:
1926 CASE_BUILT_IN_TM_STORE (1):
1927 CASE_BUILT_IN_TM_STORE (2):
1928 CASE_BUILT_IN_TM_STORE (4):
1929 CASE_BUILT_IN_TM_STORE (8):
1930 CASE_BUILT_IN_TM_STORE (FLOAT):
1931 CASE_BUILT_IN_TM_STORE (DOUBLE):
1932 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1933 CASE_BUILT_IN_TM_STORE (M64):
1934 CASE_BUILT_IN_TM_STORE (M128):
1935 CASE_BUILT_IN_TM_STORE (M256):
1936 case BUILT_IN_TM_MEMCPY:
1937 case BUILT_IN_TM_MEMMOVE:
1939 ao_ref dref;
1940 tree size = NULL_TREE;
1941 /* Don't pass in size for strncat, as the maximum size
1942 is strlen (dest) + n + 1 instead of n, resp.
1943 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1944 known. */
1945 if (gimple_call_num_args (call) == 3
1946 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1947 size = gimple_call_arg (call, 2);
1948 ao_ref_init_from_ptr_and_size (&dref,
1949 gimple_call_arg (call, 0),
1950 size);
1951 return refs_may_alias_p_1 (&dref, ref, false);
1953 case BUILT_IN_STRCPY_CHK:
1954 case BUILT_IN_STRNCPY_CHK:
1955 case BUILT_IN_MEMCPY_CHK:
1956 case BUILT_IN_MEMMOVE_CHK:
1957 case BUILT_IN_MEMPCPY_CHK:
1958 case BUILT_IN_STPCPY_CHK:
1959 case BUILT_IN_STPNCPY_CHK:
1960 case BUILT_IN_STRCAT_CHK:
1961 case BUILT_IN_STRNCAT_CHK:
1962 case BUILT_IN_MEMSET_CHK:
1964 ao_ref dref;
1965 tree size = NULL_TREE;
1966 /* Don't pass in size for __strncat_chk, as the maximum size
1967 is strlen (dest) + n + 1 instead of n, resp.
1968 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1969 known. */
1970 if (gimple_call_num_args (call) == 4
1971 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1972 size = gimple_call_arg (call, 2);
1973 ao_ref_init_from_ptr_and_size (&dref,
1974 gimple_call_arg (call, 0),
1975 size);
1976 return refs_may_alias_p_1 (&dref, ref, false);
1978 case BUILT_IN_BCOPY:
1980 ao_ref dref;
1981 tree size = gimple_call_arg (call, 2);
1982 ao_ref_init_from_ptr_and_size (&dref,
1983 gimple_call_arg (call, 1),
1984 size);
1985 return refs_may_alias_p_1 (&dref, ref, false);
1987 /* Allocating memory does not have any side-effects apart from
1988 being the definition point for the pointer. */
1989 case BUILT_IN_MALLOC:
1990 case BUILT_IN_ALIGNED_ALLOC:
1991 case BUILT_IN_CALLOC:
1992 case BUILT_IN_STRDUP:
1993 case BUILT_IN_STRNDUP:
1994 /* Unix98 specifies that errno is set on allocation failure. */
1995 if (flag_errno_math
1996 && targetm.ref_may_alias_errno (ref))
1997 return true;
1998 return false;
1999 case BUILT_IN_STACK_SAVE:
2000 case BUILT_IN_ALLOCA:
2001 case BUILT_IN_ALLOCA_WITH_ALIGN:
2002 case BUILT_IN_ASSUME_ALIGNED:
2003 return false;
2004 /* But posix_memalign stores a pointer into the memory pointed to
2005 by its first argument. */
2006 case BUILT_IN_POSIX_MEMALIGN:
2008 tree ptrptr = gimple_call_arg (call, 0);
2009 ao_ref dref;
2010 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2011 TYPE_SIZE_UNIT (ptr_type_node));
2012 return (refs_may_alias_p_1 (&dref, ref, false)
2013 || (flag_errno_math
2014 && targetm.ref_may_alias_errno (ref)));
2016 /* Freeing memory kills the pointed-to memory. More importantly
2017 the call has to serve as a barrier for moving loads and stores
2018 across it. */
2019 case BUILT_IN_FREE:
2020 case BUILT_IN_VA_END:
2022 tree ptr = gimple_call_arg (call, 0);
2023 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2025 /* Realloc serves both as allocation point and deallocation point. */
2026 case BUILT_IN_REALLOC:
2028 tree ptr = gimple_call_arg (call, 0);
2029 /* Unix98 specifies that errno is set on allocation failure. */
2030 return ((flag_errno_math
2031 && targetm.ref_may_alias_errno (ref))
2032 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2034 case BUILT_IN_GAMMA_R:
2035 case BUILT_IN_GAMMAF_R:
2036 case BUILT_IN_GAMMAL_R:
2037 case BUILT_IN_LGAMMA_R:
2038 case BUILT_IN_LGAMMAF_R:
2039 case BUILT_IN_LGAMMAL_R:
2041 tree out = gimple_call_arg (call, 1);
2042 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2043 return true;
2044 if (flag_errno_math)
2045 break;
2046 return false;
2048 case BUILT_IN_FREXP:
2049 case BUILT_IN_FREXPF:
2050 case BUILT_IN_FREXPL:
2051 case BUILT_IN_MODF:
2052 case BUILT_IN_MODFF:
2053 case BUILT_IN_MODFL:
2055 tree out = gimple_call_arg (call, 1);
2056 return ptr_deref_may_alias_ref_p_1 (out, ref);
2058 case BUILT_IN_REMQUO:
2059 case BUILT_IN_REMQUOF:
2060 case BUILT_IN_REMQUOL:
2062 tree out = gimple_call_arg (call, 2);
2063 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2064 return true;
2065 if (flag_errno_math)
2066 break;
2067 return false;
2069 case BUILT_IN_SINCOS:
2070 case BUILT_IN_SINCOSF:
2071 case BUILT_IN_SINCOSL:
2073 tree sin = gimple_call_arg (call, 1);
2074 tree cos = gimple_call_arg (call, 2);
2075 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2076 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2078 /* __sync_* builtins and some OpenMP builtins act as threading
2079 barriers. */
2080 #undef DEF_SYNC_BUILTIN
2081 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2082 #include "sync-builtins.def"
2083 #undef DEF_SYNC_BUILTIN
2084 case BUILT_IN_GOMP_ATOMIC_START:
2085 case BUILT_IN_GOMP_ATOMIC_END:
2086 case BUILT_IN_GOMP_BARRIER:
2087 case BUILT_IN_GOMP_BARRIER_CANCEL:
2088 case BUILT_IN_GOMP_TASKWAIT:
2089 case BUILT_IN_GOMP_TASKGROUP_END:
2090 case BUILT_IN_GOMP_CRITICAL_START:
2091 case BUILT_IN_GOMP_CRITICAL_END:
2092 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2093 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2094 case BUILT_IN_GOMP_LOOP_END:
2095 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2096 case BUILT_IN_GOMP_ORDERED_START:
2097 case BUILT_IN_GOMP_ORDERED_END:
2098 case BUILT_IN_GOMP_SECTIONS_END:
2099 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2100 case BUILT_IN_GOMP_SINGLE_COPY_START:
2101 case BUILT_IN_GOMP_SINGLE_COPY_END:
2102 return true;
2103 default:
2104 /* Fallthru to general call handling. */;
2107 /* Check if base is a global static variable that is not written
2108 by the function. */
2109 if (callee != NULL_TREE
2110 && TREE_CODE (base) == VAR_DECL
2111 && TREE_STATIC (base))
2113 struct cgraph_node *node = cgraph_node::get (callee);
2114 bitmap not_written;
2116 if (node
2117 && (not_written = ipa_reference_get_not_written_global (node))
2118 && bitmap_bit_p (not_written, DECL_UID (base)))
2119 return false;
2122 /* Check if the base variable is call-clobbered. */
2123 if (DECL_P (base))
2124 return pt_solution_includes (gimple_call_clobber_set (call), base);
2125 else if ((TREE_CODE (base) == MEM_REF
2126 || TREE_CODE (base) == TARGET_MEM_REF)
2127 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2129 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2130 if (!pi)
2131 return true;
2133 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2136 return true;
2139 /* If the call in statement CALL may clobber the memory reference REF
2140 return true, otherwise return false. */
2142 bool
2143 call_may_clobber_ref_p (gimple call, tree ref)
2145 bool res;
2146 ao_ref r;
2147 ao_ref_init (&r, ref);
2148 res = call_may_clobber_ref_p_1 (call, &r);
2149 if (res)
2150 ++alias_stats.call_may_clobber_ref_p_may_alias;
2151 else
2152 ++alias_stats.call_may_clobber_ref_p_no_alias;
2153 return res;
2157 /* If the statement STMT may clobber the memory reference REF return true,
2158 otherwise return false. */
2160 bool
2161 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2163 if (is_gimple_call (stmt))
2165 tree lhs = gimple_call_lhs (stmt);
2166 if (lhs
2167 && TREE_CODE (lhs) != SSA_NAME)
2169 ao_ref r;
2170 ao_ref_init (&r, lhs);
2171 if (refs_may_alias_p_1 (ref, &r, true))
2172 return true;
2175 return call_may_clobber_ref_p_1 (stmt, ref);
2177 else if (gimple_assign_single_p (stmt))
2179 tree lhs = gimple_assign_lhs (stmt);
2180 if (TREE_CODE (lhs) != SSA_NAME)
2182 ao_ref r;
2183 ao_ref_init (&r, lhs);
2184 return refs_may_alias_p_1 (ref, &r, true);
2187 else if (gimple_code (stmt) == GIMPLE_ASM)
2188 return true;
2190 return false;
2193 bool
2194 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2196 ao_ref r;
2197 ao_ref_init (&r, ref);
2198 return stmt_may_clobber_ref_p_1 (stmt, &r);
2201 /* If STMT kills the memory reference REF return true, otherwise
2202 return false. */
2204 bool
2205 stmt_kills_ref_p (gimple stmt, ao_ref *ref)
2207 if (!ao_ref_base (ref))
2208 return false;
2210 if (gimple_has_lhs (stmt)
2211 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2212 /* The assignment is not necessarily carried out if it can throw
2213 and we can catch it in the current function where we could inspect
2214 the previous value.
2215 ??? We only need to care about the RHS throwing. For aggregate
2216 assignments or similar calls and non-call exceptions the LHS
2217 might throw as well. */
2218 && !stmt_can_throw_internal (stmt))
2220 tree lhs = gimple_get_lhs (stmt);
2221 /* If LHS is literally a base of the access we are done. */
2222 if (ref->ref)
2224 tree base = ref->ref;
2225 if (handled_component_p (base))
2227 tree saved_lhs0 = NULL_TREE;
2228 if (handled_component_p (lhs))
2230 saved_lhs0 = TREE_OPERAND (lhs, 0);
2231 TREE_OPERAND (lhs, 0) = integer_zero_node;
2235 /* Just compare the outermost handled component, if
2236 they are equal we have found a possible common
2237 base. */
2238 tree saved_base0 = TREE_OPERAND (base, 0);
2239 TREE_OPERAND (base, 0) = integer_zero_node;
2240 bool res = operand_equal_p (lhs, base, 0);
2241 TREE_OPERAND (base, 0) = saved_base0;
2242 if (res)
2243 break;
2244 /* Otherwise drop handled components of the access. */
2245 base = saved_base0;
2247 while (handled_component_p (base));
2248 if (saved_lhs0)
2249 TREE_OPERAND (lhs, 0) = saved_lhs0;
2251 /* Finally check if lhs is equal or equal to the base candidate
2252 of the access. */
2253 if (operand_equal_p (lhs, base, 0))
2254 return true;
2257 /* Now look for non-literal equal bases with the restriction of
2258 handling constant offset and size. */
2259 /* For a must-alias check we need to be able to constrain
2260 the access properly. */
2261 if (ref->max_size == -1)
2262 return false;
2263 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2264 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2265 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2266 so base == ref->base does not always hold. */
2267 if (base != ref->base)
2269 /* If both base and ref->base are MEM_REFs, only compare the
2270 first operand, and if the second operand isn't equal constant,
2271 try to add the offsets into offset and ref_offset. */
2272 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2273 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2275 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2276 TREE_OPERAND (ref->base, 1)))
2278 offset_int off1 = mem_ref_offset (base);
2279 off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2280 off1 += offset;
2281 offset_int off2 = mem_ref_offset (ref->base);
2282 off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2283 off2 += ref_offset;
2284 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2286 offset = off1.to_shwi ();
2287 ref_offset = off2.to_shwi ();
2289 else
2290 size = -1;
2293 else
2294 size = -1;
2296 /* For a must-alias check we need to be able to constrain
2297 the access properly. */
2298 if (size != -1 && size == max_size)
2300 if (offset <= ref_offset
2301 && offset + size >= ref_offset + ref->max_size)
2302 return true;
2306 if (is_gimple_call (stmt))
2308 tree callee = gimple_call_fndecl (stmt);
2309 if (callee != NULL_TREE
2310 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2311 switch (DECL_FUNCTION_CODE (callee))
2313 case BUILT_IN_FREE:
2315 tree ptr = gimple_call_arg (stmt, 0);
2316 tree base = ao_ref_base (ref);
2317 if (base && TREE_CODE (base) == MEM_REF
2318 && TREE_OPERAND (base, 0) == ptr)
2319 return true;
2320 break;
2323 case BUILT_IN_MEMCPY:
2324 case BUILT_IN_MEMPCPY:
2325 case BUILT_IN_MEMMOVE:
2326 case BUILT_IN_MEMSET:
2327 case BUILT_IN_MEMCPY_CHK:
2328 case BUILT_IN_MEMPCPY_CHK:
2329 case BUILT_IN_MEMMOVE_CHK:
2330 case BUILT_IN_MEMSET_CHK:
2332 /* For a must-alias check we need to be able to constrain
2333 the access properly. */
2334 if (ref->max_size == -1)
2335 return false;
2336 tree dest = gimple_call_arg (stmt, 0);
2337 tree len = gimple_call_arg (stmt, 2);
2338 if (!tree_fits_shwi_p (len))
2339 return false;
2340 tree rbase = ref->base;
2341 offset_int roffset = ref->offset;
2342 ao_ref dref;
2343 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2344 tree base = ao_ref_base (&dref);
2345 offset_int offset = dref.offset;
2346 if (!base || dref.size == -1)
2347 return false;
2348 if (TREE_CODE (base) == MEM_REF)
2350 if (TREE_CODE (rbase) != MEM_REF)
2351 return false;
2352 // Compare pointers.
2353 offset += wi::lshift (mem_ref_offset (base),
2354 LOG2_BITS_PER_UNIT);
2355 roffset += wi::lshift (mem_ref_offset (rbase),
2356 LOG2_BITS_PER_UNIT);
2357 base = TREE_OPERAND (base, 0);
2358 rbase = TREE_OPERAND (rbase, 0);
2360 if (base == rbase
2361 && wi::les_p (offset, roffset)
2362 && wi::les_p (roffset + ref->max_size,
2363 offset + wi::lshift (wi::to_offset (len),
2364 LOG2_BITS_PER_UNIT)))
2365 return true;
2366 break;
2369 case BUILT_IN_VA_END:
2371 tree ptr = gimple_call_arg (stmt, 0);
2372 if (TREE_CODE (ptr) == ADDR_EXPR)
2374 tree base = ao_ref_base (ref);
2375 if (TREE_OPERAND (ptr, 0) == base)
2376 return true;
2378 break;
2381 default:;
2384 return false;
2387 bool
2388 stmt_kills_ref_p (gimple stmt, tree ref)
2390 ao_ref r;
2391 ao_ref_init (&r, ref);
2392 return stmt_kills_ref_p (stmt, &r);
2396 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2397 TARGET or a statement clobbering the memory reference REF in which
2398 case false is returned. The walk starts with VUSE, one argument of PHI. */
2400 static bool
2401 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2402 tree vuse, unsigned int *cnt, bitmap *visited,
2403 bool abort_on_visited,
2404 void *(*translate)(ao_ref *, tree, void *, bool),
2405 void *data)
2407 basic_block bb = gimple_bb (phi);
2409 if (!*visited)
2410 *visited = BITMAP_ALLOC (NULL);
2412 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2414 /* Walk until we hit the target. */
2415 while (vuse != target)
2417 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2418 /* Recurse for PHI nodes. */
2419 if (gimple_code (def_stmt) == GIMPLE_PHI)
2421 /* An already visited PHI node ends the walk successfully. */
2422 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2423 return !abort_on_visited;
2424 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2425 visited, abort_on_visited,
2426 translate, data);
2427 if (!vuse)
2428 return false;
2429 continue;
2431 else if (gimple_nop_p (def_stmt))
2432 return false;
2433 else
2435 /* A clobbering statement or the end of the IL ends it failing. */
2436 ++*cnt;
2437 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2439 if (translate
2440 && (*translate) (ref, vuse, data, true) == NULL)
2442 else
2443 return false;
2446 /* If we reach a new basic-block see if we already skipped it
2447 in a previous walk that ended successfully. */
2448 if (gimple_bb (def_stmt) != bb)
2450 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2451 return !abort_on_visited;
2452 bb = gimple_bb (def_stmt);
2454 vuse = gimple_vuse (def_stmt);
2456 return true;
2459 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2460 until we hit the phi argument definition that dominates the other one.
2461 Return that, or NULL_TREE if there is no such definition. */
2463 static tree
2464 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2465 ao_ref *ref, unsigned int *cnt,
2466 bitmap *visited, bool abort_on_visited,
2467 void *(*translate)(ao_ref *, tree, void *, bool),
2468 void *data)
2470 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2471 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2472 tree common_vuse;
2474 if (arg0 == arg1)
2475 return arg0;
2476 else if (gimple_nop_p (def0)
2477 || (!gimple_nop_p (def1)
2478 && dominated_by_p (CDI_DOMINATORS,
2479 gimple_bb (def1), gimple_bb (def0))))
2481 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2482 visited, abort_on_visited, translate, data))
2483 return arg0;
2485 else if (gimple_nop_p (def1)
2486 || dominated_by_p (CDI_DOMINATORS,
2487 gimple_bb (def0), gimple_bb (def1)))
2489 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2490 visited, abort_on_visited, translate, data))
2491 return arg1;
2493 /* Special case of a diamond:
2494 MEM_1 = ...
2495 goto (cond) ? L1 : L2
2496 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2497 goto L3
2498 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2499 L3: MEM_4 = PHI<MEM_2, MEM_3>
2500 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2501 dominate each other, but still we can easily skip this PHI node
2502 if we recognize that the vuse MEM operand is the same for both,
2503 and that we can skip both statements (they don't clobber us).
2504 This is still linear. Don't use maybe_skip_until, that might
2505 potentially be slow. */
2506 else if ((common_vuse = gimple_vuse (def0))
2507 && common_vuse == gimple_vuse (def1))
2509 *cnt += 2;
2510 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2511 || (translate
2512 && (*translate) (ref, arg0, data, true) == NULL))
2513 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2514 || (translate
2515 && (*translate) (ref, arg1, data, true) == NULL)))
2516 return common_vuse;
2519 return NULL_TREE;
2523 /* Starting from a PHI node for the virtual operand of the memory reference
2524 REF find a continuation virtual operand that allows to continue walking
2525 statements dominating PHI skipping only statements that cannot possibly
2526 clobber REF. Increments *CNT for each alias disambiguation done.
2527 Returns NULL_TREE if no suitable virtual operand can be found. */
2529 tree
2530 get_continuation_for_phi (gimple phi, ao_ref *ref,
2531 unsigned int *cnt, bitmap *visited,
2532 bool abort_on_visited,
2533 void *(*translate)(ao_ref *, tree, void *, bool),
2534 void *data)
2536 unsigned nargs = gimple_phi_num_args (phi);
2538 /* Through a single-argument PHI we can simply look through. */
2539 if (nargs == 1)
2540 return PHI_ARG_DEF (phi, 0);
2542 /* For two or more arguments try to pairwise skip non-aliasing code
2543 until we hit the phi argument definition that dominates the other one. */
2544 else if (nargs >= 2)
2546 tree arg0, arg1;
2547 unsigned i;
2549 /* Find a candidate for the virtual operand which definition
2550 dominates those of all others. */
2551 arg0 = PHI_ARG_DEF (phi, 0);
2552 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2553 for (i = 1; i < nargs; ++i)
2555 arg1 = PHI_ARG_DEF (phi, i);
2556 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2558 arg0 = arg1;
2559 break;
2561 if (dominated_by_p (CDI_DOMINATORS,
2562 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2563 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2564 arg0 = arg1;
2567 /* Then pairwise reduce against the found candidate. */
2568 for (i = 0; i < nargs; ++i)
2570 arg1 = PHI_ARG_DEF (phi, i);
2571 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2572 cnt, visited, abort_on_visited,
2573 translate, data);
2574 if (!arg0)
2575 return NULL_TREE;
2578 return arg0;
2581 return NULL_TREE;
2584 /* Based on the memory reference REF and its virtual use VUSE call
2585 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2586 itself. That is, for each virtual use for which its defining statement
2587 does not clobber REF.
2589 WALKER is called with REF, the current virtual use and DATA. If
2590 WALKER returns non-NULL the walk stops and its result is returned.
2591 At the end of a non-successful walk NULL is returned.
2593 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2594 use which definition is a statement that may clobber REF and DATA.
2595 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2596 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2597 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2598 to adjust REF and *DATA to make that valid.
2600 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2602 void *
2603 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2604 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2605 void *(*translate)(ao_ref *, tree, void *, bool),
2606 void *data)
2608 bitmap visited = NULL;
2609 void *res;
2610 unsigned int cnt = 0;
2611 bool translated = false;
2613 timevar_push (TV_ALIAS_STMT_WALK);
2617 gimple def_stmt;
2619 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2620 res = (*walker) (ref, vuse, cnt, data);
2621 /* Abort walk. */
2622 if (res == (void *)-1)
2624 res = NULL;
2625 break;
2627 /* Lookup succeeded. */
2628 else if (res != NULL)
2629 break;
2631 def_stmt = SSA_NAME_DEF_STMT (vuse);
2632 if (gimple_nop_p (def_stmt))
2633 break;
2634 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2635 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2636 &visited, translated, translate, data);
2637 else
2639 cnt++;
2640 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2642 if (!translate)
2643 break;
2644 res = (*translate) (ref, vuse, data, false);
2645 /* Failed lookup and translation. */
2646 if (res == (void *)-1)
2648 res = NULL;
2649 break;
2651 /* Lookup succeeded. */
2652 else if (res != NULL)
2653 break;
2654 /* Translation succeeded, continue walking. */
2655 translated = true;
2657 vuse = gimple_vuse (def_stmt);
2660 while (vuse);
2662 if (visited)
2663 BITMAP_FREE (visited);
2665 timevar_pop (TV_ALIAS_STMT_WALK);
2667 return res;
2671 /* Based on the memory reference REF call WALKER for each vdef which
2672 defining statement may clobber REF, starting with VDEF. If REF
2673 is NULL_TREE, each defining statement is visited.
2675 WALKER is called with REF, the current vdef and DATA. If WALKER
2676 returns true the walk is stopped, otherwise it continues.
2678 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2679 The pointer may be NULL and then we do not track this information.
2681 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2682 PHI argument (but only one walk continues on merge points), the
2683 return value is true if any of the walks was successful.
2685 The function returns the number of statements walked. */
2687 static unsigned int
2688 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2689 bool (*walker)(ao_ref *, tree, void *), void *data,
2690 bitmap *visited, unsigned int cnt,
2691 bool *function_entry_reached)
2695 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2697 if (*visited
2698 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2699 return cnt;
2701 if (gimple_nop_p (def_stmt))
2703 if (function_entry_reached)
2704 *function_entry_reached = true;
2705 return cnt;
2707 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2709 unsigned i;
2710 if (!*visited)
2711 *visited = BITMAP_ALLOC (NULL);
2712 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2713 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2714 walker, data, visited, 0,
2715 function_entry_reached);
2716 return cnt;
2719 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2720 cnt++;
2721 if ((!ref
2722 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2723 && (*walker) (ref, vdef, data))
2724 return cnt;
2726 vdef = gimple_vuse (def_stmt);
2728 while (1);
2731 unsigned int
2732 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2733 bool (*walker)(ao_ref *, tree, void *), void *data,
2734 bitmap *visited,
2735 bool *function_entry_reached)
2737 bitmap local_visited = NULL;
2738 unsigned int ret;
2740 timevar_push (TV_ALIAS_STMT_WALK);
2742 if (function_entry_reached)
2743 *function_entry_reached = false;
2745 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2746 visited ? visited : &local_visited, 0,
2747 function_entry_reached);
2748 if (local_visited)
2749 BITMAP_FREE (local_visited);
2751 timevar_pop (TV_ALIAS_STMT_WALK);
2753 return ret;