* include/bits/basic_string.h (getline): Qualify call to prevent ADL
[official-gcc.git] / gcc / tree-ssa-alias.c
blob2910374532a9f392880b7064d061b77f9e268bd9
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 "function.h"
33 #include "tree-pretty-print.h"
34 #include "dumpfile.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "tree-eh.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "gimple-ssa.h"
42 #include "stringpool.h"
43 #include "tree-ssanames.h"
44 #include "expr.h"
45 #include "tree-dfa.h"
46 #include "tree-inline.h"
47 #include "params.h"
48 #include "alloc-pool.h"
49 #include "tree-ssa-alias.h"
50 #include "ipa-reference.h"
52 /* Broad overview of how alias analysis on gimple works:
54 Statements clobbering or using memory are linked through the
55 virtual operand factored use-def chain. The virtual operand
56 is unique per function, its symbol is accessible via gimple_vop (cfun).
57 Virtual operands are used for efficiently walking memory statements
58 in the gimple IL and are useful for things like value-numbering as
59 a generation count for memory references.
61 SSA_NAME pointers may have associated points-to information
62 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
63 points-to information is (re-)computed by the TODO_rebuild_alias
64 pass manager todo. Points-to information is also used for more
65 precise tracking of call-clobbered and call-used variables and
66 related disambiguations.
68 This file contains functions for disambiguating memory references,
69 the so called alias-oracle and tools for walking of the gimple IL.
71 The main alias-oracle entry-points are
73 bool stmt_may_clobber_ref_p (gimple, tree)
75 This function queries if a statement may invalidate (parts of)
76 the memory designated by the reference tree argument.
78 bool ref_maybe_used_by_stmt_p (gimple, tree)
80 This function queries if a statement may need (parts of) the
81 memory designated by the reference tree argument.
83 There are variants of these functions that only handle the call
84 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
85 Note that these do not disambiguate against a possible call lhs.
87 bool refs_may_alias_p (tree, tree)
89 This function tries to disambiguate two reference trees.
91 bool ptr_deref_may_alias_global_p (tree)
93 This function queries if dereferencing a pointer variable may
94 alias global memory.
96 More low-level disambiguators are available and documented in
97 this file. Low-level disambiguators dealing with points-to
98 information are in tree-ssa-structalias.c. */
101 /* Query statistics for the different low-level disambiguators.
102 A high-level query may trigger multiple of them. */
104 static struct {
105 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
106 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
107 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
108 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
109 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
110 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
111 } alias_stats;
113 void
114 dump_alias_stats (FILE *s)
116 fprintf (s, "\nAlias oracle query stats:\n");
117 fprintf (s, " refs_may_alias_p: "
118 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
119 HOST_WIDE_INT_PRINT_DEC" queries\n",
120 alias_stats.refs_may_alias_p_no_alias,
121 alias_stats.refs_may_alias_p_no_alias
122 + alias_stats.refs_may_alias_p_may_alias);
123 fprintf (s, " ref_maybe_used_by_call_p: "
124 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
125 HOST_WIDE_INT_PRINT_DEC" queries\n",
126 alias_stats.ref_maybe_used_by_call_p_no_alias,
127 alias_stats.refs_may_alias_p_no_alias
128 + alias_stats.ref_maybe_used_by_call_p_may_alias);
129 fprintf (s, " call_may_clobber_ref_p: "
130 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
131 HOST_WIDE_INT_PRINT_DEC" queries\n",
132 alias_stats.call_may_clobber_ref_p_no_alias,
133 alias_stats.call_may_clobber_ref_p_no_alias
134 + alias_stats.call_may_clobber_ref_p_may_alias);
138 /* Return true, if dereferencing PTR may alias with a global variable. */
140 bool
141 ptr_deref_may_alias_global_p (tree ptr)
143 struct ptr_info_def *pi;
145 /* If we end up with a pointer constant here that may point
146 to global memory. */
147 if (TREE_CODE (ptr) != SSA_NAME)
148 return true;
150 pi = SSA_NAME_PTR_INFO (ptr);
152 /* If we do not have points-to information for this variable,
153 we have to punt. */
154 if (!pi)
155 return true;
157 /* ??? This does not use TBAA to prune globals ptr may not access. */
158 return pt_solution_includes_global (&pi->pt);
161 /* Return true if dereferencing PTR may alias DECL.
162 The caller is responsible for applying TBAA to see if PTR
163 may access DECL at all. */
165 static bool
166 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
168 struct ptr_info_def *pi;
170 /* Conversions are irrelevant for points-to information and
171 data-dependence analysis can feed us those. */
172 STRIP_NOPS (ptr);
174 /* Anything we do not explicilty handle aliases. */
175 if ((TREE_CODE (ptr) != SSA_NAME
176 && TREE_CODE (ptr) != ADDR_EXPR
177 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
178 || !POINTER_TYPE_P (TREE_TYPE (ptr))
179 || (TREE_CODE (decl) != VAR_DECL
180 && TREE_CODE (decl) != PARM_DECL
181 && TREE_CODE (decl) != RESULT_DECL))
182 return true;
184 /* Disregard pointer offsetting. */
185 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
189 ptr = TREE_OPERAND (ptr, 0);
191 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
192 return ptr_deref_may_alias_decl_p (ptr, decl);
195 /* ADDR_EXPR pointers either just offset another pointer or directly
196 specify the pointed-to set. */
197 if (TREE_CODE (ptr) == ADDR_EXPR)
199 tree base = get_base_address (TREE_OPERAND (ptr, 0));
200 if (base
201 && (TREE_CODE (base) == MEM_REF
202 || TREE_CODE (base) == TARGET_MEM_REF))
203 ptr = TREE_OPERAND (base, 0);
204 else if (base
205 && DECL_P (base))
206 return base == decl;
207 else if (base
208 && CONSTANT_CLASS_P (base))
209 return false;
210 else
211 return true;
214 /* Non-aliased variables can not be pointed to. */
215 if (!may_be_aliased (decl))
216 return false;
218 /* If we do not have useful points-to information for this pointer
219 we cannot disambiguate anything else. */
220 pi = SSA_NAME_PTR_INFO (ptr);
221 if (!pi)
222 return true;
224 return pt_solution_includes (&pi->pt, decl);
227 /* Return true if dereferenced PTR1 and PTR2 may alias.
228 The caller is responsible for applying TBAA to see if accesses
229 through PTR1 and PTR2 may conflict at all. */
231 bool
232 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
234 struct ptr_info_def *pi1, *pi2;
236 /* Conversions are irrelevant for points-to information and
237 data-dependence analysis can feed us those. */
238 STRIP_NOPS (ptr1);
239 STRIP_NOPS (ptr2);
241 /* Disregard pointer offsetting. */
242 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
246 ptr1 = TREE_OPERAND (ptr1, 0);
248 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
249 return ptr_derefs_may_alias_p (ptr1, ptr2);
251 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
255 ptr2 = TREE_OPERAND (ptr2, 0);
257 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
258 return ptr_derefs_may_alias_p (ptr1, ptr2);
261 /* ADDR_EXPR pointers either just offset another pointer or directly
262 specify the pointed-to set. */
263 if (TREE_CODE (ptr1) == ADDR_EXPR)
265 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
266 if (base
267 && (TREE_CODE (base) == MEM_REF
268 || TREE_CODE (base) == TARGET_MEM_REF))
269 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
270 else if (base
271 && DECL_P (base))
272 return ptr_deref_may_alias_decl_p (ptr2, base);
273 else
274 return true;
276 if (TREE_CODE (ptr2) == ADDR_EXPR)
278 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
279 if (base
280 && (TREE_CODE (base) == MEM_REF
281 || TREE_CODE (base) == TARGET_MEM_REF))
282 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
283 else if (base
284 && DECL_P (base))
285 return ptr_deref_may_alias_decl_p (ptr1, base);
286 else
287 return true;
290 /* From here we require SSA name pointers. Anything else aliases. */
291 if (TREE_CODE (ptr1) != SSA_NAME
292 || TREE_CODE (ptr2) != SSA_NAME
293 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
294 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
295 return true;
297 /* We may end up with two empty points-to solutions for two same pointers.
298 In this case we still want to say both pointers alias, so shortcut
299 that here. */
300 if (ptr1 == ptr2)
301 return true;
303 /* If we do not have useful points-to information for either pointer
304 we cannot disambiguate anything else. */
305 pi1 = SSA_NAME_PTR_INFO (ptr1);
306 pi2 = SSA_NAME_PTR_INFO (ptr2);
307 if (!pi1 || !pi2)
308 return true;
310 /* ??? This does not use TBAA to prune decls from the intersection
311 that not both pointers may access. */
312 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
315 /* Return true if dereferencing PTR may alias *REF.
316 The caller is responsible for applying TBAA to see if PTR
317 may access *REF at all. */
319 static bool
320 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
322 tree base = ao_ref_base (ref);
324 if (TREE_CODE (base) == MEM_REF
325 || TREE_CODE (base) == TARGET_MEM_REF)
326 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
327 else if (DECL_P (base))
328 return ptr_deref_may_alias_decl_p (ptr, base);
330 return true;
333 /* Return true whether REF may refer to global memory. */
335 bool
336 ref_may_alias_global_p (tree ref)
338 tree base = get_base_address (ref);
339 if (DECL_P (base))
340 return is_global_var (base);
341 else if (TREE_CODE (base) == MEM_REF
342 || TREE_CODE (base) == TARGET_MEM_REF)
343 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
344 return true;
347 /* Return true whether STMT may clobber global memory. */
349 bool
350 stmt_may_clobber_global_p (gimple stmt)
352 tree lhs;
354 if (!gimple_vdef (stmt))
355 return false;
357 /* ??? We can ask the oracle whether an artificial pointer
358 dereference with a pointer with points-to information covering
359 all global memory (what about non-address taken memory?) maybe
360 clobbered by this call. As there is at the moment no convenient
361 way of doing that without generating garbage do some manual
362 checking instead.
363 ??? We could make a NULL ao_ref argument to the various
364 predicates special, meaning any global memory. */
366 switch (gimple_code (stmt))
368 case GIMPLE_ASSIGN:
369 lhs = gimple_assign_lhs (stmt);
370 return (TREE_CODE (lhs) != SSA_NAME
371 && ref_may_alias_global_p (lhs));
372 case GIMPLE_CALL:
373 return true;
374 default:
375 return true;
380 /* Dump alias information on FILE. */
382 void
383 dump_alias_info (FILE *file)
385 unsigned i;
386 const char *funcname
387 = lang_hooks.decl_printable_name (current_function_decl, 2);
388 tree var;
390 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
392 fprintf (file, "Aliased symbols\n\n");
394 FOR_EACH_LOCAL_DECL (cfun, i, var)
396 if (may_be_aliased (var))
397 dump_variable (file, var);
400 fprintf (file, "\nCall clobber information\n");
402 fprintf (file, "\nESCAPED");
403 dump_points_to_solution (file, &cfun->gimple_df->escaped);
405 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
407 for (i = 1; i < num_ssa_names; i++)
409 tree ptr = ssa_name (i);
410 struct ptr_info_def *pi;
412 if (ptr == NULL_TREE
413 || !POINTER_TYPE_P (TREE_TYPE (ptr))
414 || SSA_NAME_IN_FREE_LIST (ptr))
415 continue;
417 pi = SSA_NAME_PTR_INFO (ptr);
418 if (pi)
419 dump_points_to_info_for (file, ptr);
422 fprintf (file, "\n");
426 /* Dump alias information on stderr. */
428 DEBUG_FUNCTION void
429 debug_alias_info (void)
431 dump_alias_info (stderr);
435 /* Dump the points-to set *PT into FILE. */
437 void
438 dump_points_to_solution (FILE *file, struct pt_solution *pt)
440 if (pt->anything)
441 fprintf (file, ", points-to anything");
443 if (pt->nonlocal)
444 fprintf (file, ", points-to non-local");
446 if (pt->escaped)
447 fprintf (file, ", points-to escaped");
449 if (pt->ipa_escaped)
450 fprintf (file, ", points-to unit escaped");
452 if (pt->null)
453 fprintf (file, ", points-to NULL");
455 if (pt->vars)
457 fprintf (file, ", points-to vars: ");
458 dump_decl_set (file, pt->vars);
459 if (pt->vars_contains_nonlocal
460 && pt->vars_contains_escaped_heap)
461 fprintf (file, " (nonlocal, escaped heap)");
462 else if (pt->vars_contains_nonlocal
463 && pt->vars_contains_escaped)
464 fprintf (file, " (nonlocal, escaped)");
465 else if (pt->vars_contains_nonlocal)
466 fprintf (file, " (nonlocal)");
467 else if (pt->vars_contains_escaped_heap)
468 fprintf (file, " (escaped heap)");
469 else if (pt->vars_contains_escaped)
470 fprintf (file, " (escaped)");
475 /* Unified dump function for pt_solution. */
477 DEBUG_FUNCTION void
478 debug (pt_solution &ref)
480 dump_points_to_solution (stderr, &ref);
483 DEBUG_FUNCTION void
484 debug (pt_solution *ptr)
486 if (ptr)
487 debug (*ptr);
488 else
489 fprintf (stderr, "<nil>\n");
493 /* Dump points-to information for SSA_NAME PTR into FILE. */
495 void
496 dump_points_to_info_for (FILE *file, tree ptr)
498 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
500 print_generic_expr (file, ptr, dump_flags);
502 if (pi)
503 dump_points_to_solution (file, &pi->pt);
504 else
505 fprintf (file, ", points-to anything");
507 fprintf (file, "\n");
511 /* Dump points-to information for VAR into stderr. */
513 DEBUG_FUNCTION void
514 debug_points_to_info_for (tree var)
516 dump_points_to_info_for (stderr, var);
520 /* Initializes the alias-oracle reference representation *R from REF. */
522 void
523 ao_ref_init (ao_ref *r, tree ref)
525 r->ref = ref;
526 r->base = NULL_TREE;
527 r->offset = 0;
528 r->size = -1;
529 r->max_size = -1;
530 r->ref_alias_set = -1;
531 r->base_alias_set = -1;
532 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
535 /* Returns the base object of the memory reference *REF. */
537 tree
538 ao_ref_base (ao_ref *ref)
540 if (ref->base)
541 return ref->base;
542 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
543 &ref->max_size);
544 return ref->base;
547 /* Returns the base object alias set of the memory reference *REF. */
549 static alias_set_type
550 ao_ref_base_alias_set (ao_ref *ref)
552 tree base_ref;
553 if (ref->base_alias_set != -1)
554 return ref->base_alias_set;
555 if (!ref->ref)
556 return 0;
557 base_ref = ref->ref;
558 while (handled_component_p (base_ref))
559 base_ref = TREE_OPERAND (base_ref, 0);
560 ref->base_alias_set = get_alias_set (base_ref);
561 return ref->base_alias_set;
564 /* Returns the reference alias set of the memory reference *REF. */
566 alias_set_type
567 ao_ref_alias_set (ao_ref *ref)
569 if (ref->ref_alias_set != -1)
570 return ref->ref_alias_set;
571 ref->ref_alias_set = get_alias_set (ref->ref);
572 return ref->ref_alias_set;
575 /* Init an alias-oracle reference representation from a gimple pointer
576 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
577 size is assumed to be unknown. The access is assumed to be only
578 to or after of the pointer target, not before it. */
580 void
581 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
583 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
584 ref->ref = NULL_TREE;
585 if (TREE_CODE (ptr) == SSA_NAME)
587 gimple stmt = SSA_NAME_DEF_STMT (ptr);
588 if (gimple_assign_single_p (stmt)
589 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
590 ptr = gimple_assign_rhs1 (stmt);
591 else if (is_gimple_assign (stmt)
592 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
593 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
595 ptr = gimple_assign_rhs1 (stmt);
596 extra_offset = BITS_PER_UNIT
597 * int_cst_value (gimple_assign_rhs2 (stmt));
601 if (TREE_CODE (ptr) == ADDR_EXPR)
603 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
604 if (ref->base)
605 ref->offset = BITS_PER_UNIT * t;
606 else
608 size = NULL_TREE;
609 ref->offset = 0;
610 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
613 else
615 ref->base = build2 (MEM_REF, char_type_node,
616 ptr, null_pointer_node);
617 ref->offset = 0;
619 ref->offset += extra_offset;
620 if (size
621 && tree_fits_shwi_p (size)
622 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
623 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
624 else
625 ref->max_size = ref->size = -1;
626 ref->ref_alias_set = 0;
627 ref->base_alias_set = 0;
628 ref->volatile_p = false;
631 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
632 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
633 decide. */
635 static inline int
636 same_type_for_tbaa (tree type1, tree type2)
638 type1 = TYPE_MAIN_VARIANT (type1);
639 type2 = TYPE_MAIN_VARIANT (type2);
641 /* If we would have to do structural comparison bail out. */
642 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
643 || TYPE_STRUCTURAL_EQUALITY_P (type2))
644 return -1;
646 /* Compare the canonical types. */
647 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
648 return 1;
650 /* ??? Array types are not properly unified in all cases as we have
651 spurious changes in the index types for example. Removing this
652 causes all sorts of problems with the Fortran frontend. */
653 if (TREE_CODE (type1) == ARRAY_TYPE
654 && TREE_CODE (type2) == ARRAY_TYPE)
655 return -1;
657 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
658 object of one of its constrained subtypes, e.g. when a function with an
659 unconstrained parameter passed by reference is called on an object and
660 inlined. But, even in the case of a fixed size, type and subtypes are
661 not equivalent enough as to share the same TYPE_CANONICAL, since this
662 would mean that conversions between them are useless, whereas they are
663 not (e.g. type and subtypes can have different modes). So, in the end,
664 they are only guaranteed to have the same alias set. */
665 if (get_alias_set (type1) == get_alias_set (type2))
666 return -1;
668 /* The types are known to be not equal. */
669 return 0;
672 /* Determine if the two component references REF1 and REF2 which are
673 based on access types TYPE1 and TYPE2 and of which at least one is based
674 on an indirect reference may alias. REF2 is the only one that can
675 be a decl in which case REF2_IS_DECL is true.
676 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
677 are the respective alias sets. */
679 static bool
680 aliasing_component_refs_p (tree ref1,
681 alias_set_type ref1_alias_set,
682 alias_set_type base1_alias_set,
683 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
684 tree ref2,
685 alias_set_type ref2_alias_set,
686 alias_set_type base2_alias_set,
687 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
688 bool ref2_is_decl)
690 /* If one reference is a component references through pointers try to find a
691 common base and apply offset based disambiguation. This handles
692 for example
693 struct A { int i; int j; } *q;
694 struct B { struct A a; int k; } *p;
695 disambiguating q->i and p->a.j. */
696 tree base1, base2;
697 tree type1, type2;
698 tree *refp;
699 int same_p;
701 /* Choose bases and base types to search for. */
702 base1 = ref1;
703 while (handled_component_p (base1))
704 base1 = TREE_OPERAND (base1, 0);
705 type1 = TREE_TYPE (base1);
706 base2 = ref2;
707 while (handled_component_p (base2))
708 base2 = TREE_OPERAND (base2, 0);
709 type2 = TREE_TYPE (base2);
711 /* Now search for the type1 in the access path of ref2. This
712 would be a common base for doing offset based disambiguation on. */
713 refp = &ref2;
714 while (handled_component_p (*refp)
715 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
716 refp = &TREE_OPERAND (*refp, 0);
717 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
718 /* If we couldn't compare types we have to bail out. */
719 if (same_p == -1)
720 return true;
721 else if (same_p == 1)
723 HOST_WIDE_INT offadj, sztmp, msztmp;
724 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
725 offset2 -= offadj;
726 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
727 offset1 -= offadj;
728 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
730 /* If we didn't find a common base, try the other way around. */
731 refp = &ref1;
732 while (handled_component_p (*refp)
733 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
734 refp = &TREE_OPERAND (*refp, 0);
735 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
736 /* If we couldn't compare types we have to bail out. */
737 if (same_p == -1)
738 return true;
739 else if (same_p == 1)
741 HOST_WIDE_INT offadj, sztmp, msztmp;
742 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
743 offset1 -= offadj;
744 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
745 offset2 -= offadj;
746 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
749 /* If we have two type access paths B1.path1 and B2.path2 they may
750 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
751 But we can still have a path that goes B1.path1...B2.path2 with
752 a part that we do not see. So we can only disambiguate now
753 if there is no B2 in the tail of path1 and no B1 on the
754 tail of path2. */
755 if (base1_alias_set == ref2_alias_set
756 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
757 return true;
758 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
759 if (!ref2_is_decl)
760 return (base2_alias_set == ref1_alias_set
761 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
762 return false;
765 /* Return true if we can determine that component references REF1 and REF2,
766 that are within a common DECL, cannot overlap. */
768 static bool
769 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
771 auto_vec<tree, 16> component_refs1;
772 auto_vec<tree, 16> component_refs2;
774 /* Create the stack of handled components for REF1. */
775 while (handled_component_p (ref1))
777 component_refs1.safe_push (ref1);
778 ref1 = TREE_OPERAND (ref1, 0);
780 if (TREE_CODE (ref1) == MEM_REF)
782 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
783 goto may_overlap;
784 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
787 /* Create the stack of handled components for REF2. */
788 while (handled_component_p (ref2))
790 component_refs2.safe_push (ref2);
791 ref2 = TREE_OPERAND (ref2, 0);
793 if (TREE_CODE (ref2) == MEM_REF)
795 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
796 goto may_overlap;
797 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
800 /* We must have the same base DECL. */
801 gcc_assert (ref1 == ref2);
803 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
804 rank. This is sufficient because we start from the same DECL and you
805 cannot reference several fields at a time with COMPONENT_REFs (unlike
806 with ARRAY_RANGE_REFs for arrays) so you always need the same number
807 of them to access a sub-component, unless you're in a union, in which
808 case the return value will precisely be false. */
809 while (true)
813 if (component_refs1.is_empty ())
814 goto may_overlap;
815 ref1 = component_refs1.pop ();
817 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
821 if (component_refs2.is_empty ())
822 goto may_overlap;
823 ref2 = component_refs2.pop ();
825 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
827 /* Beware of BIT_FIELD_REF. */
828 if (TREE_CODE (ref1) != COMPONENT_REF
829 || TREE_CODE (ref2) != COMPONENT_REF)
830 goto may_overlap;
832 tree field1 = TREE_OPERAND (ref1, 1);
833 tree field2 = TREE_OPERAND (ref2, 1);
835 /* ??? We cannot simply use the type of operand #0 of the refs here
836 as the Fortran compiler smuggles type punning into COMPONENT_REFs
837 for common blocks instead of using unions like everyone else. */
838 tree type1 = DECL_CONTEXT (field1);
839 tree type2 = DECL_CONTEXT (field2);
841 /* We cannot disambiguate fields in a union or qualified union. */
842 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
843 goto may_overlap;
845 /* Different fields of the same record type cannot overlap.
846 ??? Bitfields can overlap at RTL level so punt on them. */
847 if (field1 != field2)
849 component_refs1.release ();
850 component_refs2.release ();
851 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
855 may_overlap:
856 component_refs1.release ();
857 component_refs2.release ();
858 return false;
861 /* qsort compare function to sort FIELD_DECLs after their
862 DECL_FIELD_CONTEXT TYPE_UID. */
864 static inline int
865 ncr_compar (const void *field1_, const void *field2_)
867 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
868 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
869 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
870 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
871 if (uid1 < uid2)
872 return -1;
873 else if (uid1 > uid2)
874 return 1;
875 return 0;
878 /* Return true if we can determine that the fields referenced cannot
879 overlap for any pair of objects. */
881 static bool
882 nonoverlapping_component_refs_p (const_tree x, const_tree y)
884 if (!flag_strict_aliasing
885 || !x || !y
886 || TREE_CODE (x) != COMPONENT_REF
887 || TREE_CODE (y) != COMPONENT_REF)
888 return false;
890 auto_vec<const_tree, 16> fieldsx;
891 while (TREE_CODE (x) == COMPONENT_REF)
893 tree field = TREE_OPERAND (x, 1);
894 tree type = DECL_FIELD_CONTEXT (field);
895 if (TREE_CODE (type) == RECORD_TYPE)
896 fieldsx.safe_push (field);
897 x = TREE_OPERAND (x, 0);
899 if (fieldsx.length () == 0)
900 return false;
901 auto_vec<const_tree, 16> fieldsy;
902 while (TREE_CODE (y) == COMPONENT_REF)
904 tree field = TREE_OPERAND (y, 1);
905 tree type = DECL_FIELD_CONTEXT (field);
906 if (TREE_CODE (type) == RECORD_TYPE)
907 fieldsy.safe_push (TREE_OPERAND (y, 1));
908 y = TREE_OPERAND (y, 0);
910 if (fieldsy.length () == 0)
911 return false;
913 /* Most common case first. */
914 if (fieldsx.length () == 1
915 && fieldsy.length () == 1)
916 return ((DECL_FIELD_CONTEXT (fieldsx[0])
917 == DECL_FIELD_CONTEXT (fieldsy[0]))
918 && fieldsx[0] != fieldsy[0]
919 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
921 if (fieldsx.length () == 2)
923 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
925 const_tree tem = fieldsx[0];
926 fieldsx[0] = fieldsx[1];
927 fieldsx[1] = tem;
930 else
931 fieldsx.qsort (ncr_compar);
933 if (fieldsy.length () == 2)
935 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
937 const_tree tem = fieldsy[0];
938 fieldsy[0] = fieldsy[1];
939 fieldsy[1] = tem;
942 else
943 fieldsy.qsort (ncr_compar);
945 unsigned i = 0, j = 0;
948 const_tree fieldx = fieldsx[i];
949 const_tree fieldy = fieldsy[j];
950 tree typex = DECL_FIELD_CONTEXT (fieldx);
951 tree typey = DECL_FIELD_CONTEXT (fieldy);
952 if (typex == typey)
954 /* We're left with accessing different fields of a structure,
955 no possible overlap, unless they are both bitfields. */
956 if (fieldx != fieldy)
957 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
959 if (TYPE_UID (typex) < TYPE_UID (typey))
961 i++;
962 if (i == fieldsx.length ())
963 break;
965 else
967 j++;
968 if (j == fieldsy.length ())
969 break;
972 while (1);
974 return false;
978 /* Return true if two memory references based on the variables BASE1
979 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
980 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
981 if non-NULL are the complete memory reference trees. */
983 static bool
984 decl_refs_may_alias_p (tree ref1, tree base1,
985 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
986 tree ref2, tree base2,
987 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
989 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
991 /* If both references are based on different variables, they cannot alias. */
992 if (base1 != base2)
993 return false;
995 /* If both references are based on the same variable, they cannot alias if
996 the accesses do not overlap. */
997 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
998 return false;
1000 /* For components with variable position, the above test isn't sufficient,
1001 so we disambiguate component references manually. */
1002 if (ref1 && ref2
1003 && handled_component_p (ref1) && handled_component_p (ref2)
1004 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1005 return false;
1007 return true;
1010 /* Return true if an indirect reference based on *PTR1 constrained
1011 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1012 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1013 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1014 in which case they are computed on-demand. REF1 and REF2
1015 if non-NULL are the complete memory reference trees. */
1017 static bool
1018 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1019 HOST_WIDE_INT offset1,
1020 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1021 alias_set_type ref1_alias_set,
1022 alias_set_type base1_alias_set,
1023 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1024 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1025 alias_set_type ref2_alias_set,
1026 alias_set_type base2_alias_set, bool tbaa_p)
1028 tree ptr1;
1029 tree ptrtype1, dbase2;
1030 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1031 HOST_WIDE_INT doffset1, doffset2;
1033 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1034 || TREE_CODE (base1) == TARGET_MEM_REF)
1035 && DECL_P (base2));
1037 ptr1 = TREE_OPERAND (base1, 0);
1039 /* The offset embedded in MEM_REFs can be negative. Bias them
1040 so that the resulting offset adjustment is positive. */
1041 offset_int moff = mem_ref_offset (base1);
1042 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1043 if (wi::neg_p (moff))
1044 offset2p += (-moff).to_short_addr ();
1045 else
1046 offset1p += moff.to_short_addr ();
1048 /* If only one reference is based on a variable, they cannot alias if
1049 the pointer access is beyond the extent of the variable access.
1050 (the pointer base cannot validly point to an offset less than zero
1051 of the variable).
1052 ??? IVOPTs creates bases that do not honor this restriction,
1053 so do not apply this optimization for TARGET_MEM_REFs. */
1054 if (TREE_CODE (base1) != TARGET_MEM_REF
1055 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1056 return false;
1057 /* They also cannot alias if the pointer may not point to the decl. */
1058 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1059 return false;
1061 /* Disambiguations that rely on strict aliasing rules follow. */
1062 if (!flag_strict_aliasing || !tbaa_p)
1063 return true;
1065 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1067 /* If the alias set for a pointer access is zero all bets are off. */
1068 if (base1_alias_set == -1)
1069 base1_alias_set = get_deref_alias_set (ptrtype1);
1070 if (base1_alias_set == 0)
1071 return true;
1072 if (base2_alias_set == -1)
1073 base2_alias_set = get_alias_set (base2);
1075 /* When we are trying to disambiguate an access with a pointer dereference
1076 as base versus one with a decl as base we can use both the size
1077 of the decl and its dynamic type for extra disambiguation.
1078 ??? We do not know anything about the dynamic type of the decl
1079 other than that its alias-set contains base2_alias_set as a subset
1080 which does not help us here. */
1081 /* As we know nothing useful about the dynamic type of the decl just
1082 use the usual conflict check rather than a subset test.
1083 ??? We could introduce -fvery-strict-aliasing when the language
1084 does not allow decls to have a dynamic type that differs from their
1085 static type. Then we can check
1086 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1087 if (base1_alias_set != base2_alias_set
1088 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1089 return false;
1090 /* If the size of the access relevant for TBAA through the pointer
1091 is bigger than the size of the decl we can't possibly access the
1092 decl via that pointer. */
1093 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1094 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1095 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1096 /* ??? This in turn may run afoul when a decl of type T which is
1097 a member of union type U is accessed through a pointer to
1098 type U and sizeof T is smaller than sizeof U. */
1099 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1100 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1101 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1102 return false;
1104 if (!ref2)
1105 return true;
1107 /* If the decl is accessed via a MEM_REF, reconstruct the base
1108 we can use for TBAA and an appropriately adjusted offset. */
1109 dbase2 = ref2;
1110 while (handled_component_p (dbase2))
1111 dbase2 = TREE_OPERAND (dbase2, 0);
1112 doffset1 = offset1;
1113 doffset2 = offset2;
1114 if (TREE_CODE (dbase2) == MEM_REF
1115 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1117 offset_int moff = mem_ref_offset (dbase2);
1118 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1119 if (wi::neg_p (moff))
1120 doffset1 -= (-moff).to_short_addr ();
1121 else
1122 doffset2 -= moff.to_short_addr ();
1125 /* If either reference is view-converted, give up now. */
1126 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1127 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1128 return true;
1130 /* If both references are through the same type, they do not alias
1131 if the accesses do not overlap. This does extra disambiguation
1132 for mixed/pointer accesses but requires strict aliasing.
1133 For MEM_REFs we require that the component-ref offset we computed
1134 is relative to the start of the type which we ensure by
1135 comparing rvalue and access type and disregarding the constant
1136 pointer offset. */
1137 if ((TREE_CODE (base1) != TARGET_MEM_REF
1138 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1139 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1140 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1142 if (ref1 && ref2
1143 && nonoverlapping_component_refs_p (ref1, ref2))
1144 return false;
1146 /* Do access-path based disambiguation. */
1147 if (ref1 && ref2
1148 && (handled_component_p (ref1) || handled_component_p (ref2)))
1149 return aliasing_component_refs_p (ref1,
1150 ref1_alias_set, base1_alias_set,
1151 offset1, max_size1,
1152 ref2,
1153 ref2_alias_set, base2_alias_set,
1154 offset2, max_size2, true);
1156 return true;
1159 /* Return true if two indirect references based on *PTR1
1160 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1161 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1162 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1163 in which case they are computed on-demand. REF1 and REF2
1164 if non-NULL are the complete memory reference trees. */
1166 static bool
1167 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1168 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1169 alias_set_type ref1_alias_set,
1170 alias_set_type base1_alias_set,
1171 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1172 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1173 alias_set_type ref2_alias_set,
1174 alias_set_type base2_alias_set, bool tbaa_p)
1176 tree ptr1;
1177 tree ptr2;
1178 tree ptrtype1, ptrtype2;
1180 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1181 || TREE_CODE (base1) == TARGET_MEM_REF)
1182 && (TREE_CODE (base2) == MEM_REF
1183 || TREE_CODE (base2) == TARGET_MEM_REF));
1185 ptr1 = TREE_OPERAND (base1, 0);
1186 ptr2 = TREE_OPERAND (base2, 0);
1188 /* If both bases are based on pointers they cannot alias if they may not
1189 point to the same memory object or if they point to the same object
1190 and the accesses do not overlap. */
1191 if ((!cfun || gimple_in_ssa_p (cfun))
1192 && operand_equal_p (ptr1, ptr2, 0)
1193 && (((TREE_CODE (base1) != TARGET_MEM_REF
1194 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1195 && (TREE_CODE (base2) != TARGET_MEM_REF
1196 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1197 || (TREE_CODE (base1) == TARGET_MEM_REF
1198 && TREE_CODE (base2) == TARGET_MEM_REF
1199 && (TMR_STEP (base1) == TMR_STEP (base2)
1200 || (TMR_STEP (base1) && TMR_STEP (base2)
1201 && operand_equal_p (TMR_STEP (base1),
1202 TMR_STEP (base2), 0)))
1203 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1204 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1205 && operand_equal_p (TMR_INDEX (base1),
1206 TMR_INDEX (base2), 0)))
1207 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1208 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1209 && operand_equal_p (TMR_INDEX2 (base1),
1210 TMR_INDEX2 (base2), 0))))))
1212 offset_int moff;
1213 /* The offset embedded in MEM_REFs can be negative. Bias them
1214 so that the resulting offset adjustment is positive. */
1215 moff = mem_ref_offset (base1);
1216 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1217 if (wi::neg_p (moff))
1218 offset2 += (-moff).to_short_addr ();
1219 else
1220 offset1 += moff.to_shwi ();
1221 moff = mem_ref_offset (base2);
1222 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1223 if (wi::neg_p (moff))
1224 offset1 += (-moff).to_short_addr ();
1225 else
1226 offset2 += moff.to_short_addr ();
1227 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1229 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1230 return false;
1232 /* Disambiguations that rely on strict aliasing rules follow. */
1233 if (!flag_strict_aliasing || !tbaa_p)
1234 return true;
1236 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1237 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1239 /* If the alias set for a pointer access is zero all bets are off. */
1240 if (base1_alias_set == -1)
1241 base1_alias_set = get_deref_alias_set (ptrtype1);
1242 if (base1_alias_set == 0)
1243 return true;
1244 if (base2_alias_set == -1)
1245 base2_alias_set = get_deref_alias_set (ptrtype2);
1246 if (base2_alias_set == 0)
1247 return true;
1249 /* If both references are through the same type, they do not alias
1250 if the accesses do not overlap. This does extra disambiguation
1251 for mixed/pointer accesses but requires strict aliasing. */
1252 if ((TREE_CODE (base1) != TARGET_MEM_REF
1253 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1254 && (TREE_CODE (base2) != TARGET_MEM_REF
1255 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1256 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1257 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1258 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1259 TREE_TYPE (ptrtype2)) == 1)
1260 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1262 /* Do type-based disambiguation. */
1263 if (base1_alias_set != base2_alias_set
1264 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1265 return false;
1267 /* If either reference is view-converted, give up now. */
1268 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1269 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1270 return true;
1272 if (ref1 && ref2
1273 && nonoverlapping_component_refs_p (ref1, ref2))
1274 return false;
1276 /* Do access-path based disambiguation. */
1277 if (ref1 && ref2
1278 && (handled_component_p (ref1) || handled_component_p (ref2)))
1279 return aliasing_component_refs_p (ref1,
1280 ref1_alias_set, base1_alias_set,
1281 offset1, max_size1,
1282 ref2,
1283 ref2_alias_set, base2_alias_set,
1284 offset2, max_size2, false);
1286 return true;
1289 /* Return true, if the two memory references REF1 and REF2 may alias. */
1291 bool
1292 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1294 tree base1, base2;
1295 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1296 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1297 bool var1_p, var2_p, ind1_p, ind2_p;
1299 gcc_checking_assert ((!ref1->ref
1300 || TREE_CODE (ref1->ref) == SSA_NAME
1301 || DECL_P (ref1->ref)
1302 || TREE_CODE (ref1->ref) == STRING_CST
1303 || handled_component_p (ref1->ref)
1304 || TREE_CODE (ref1->ref) == MEM_REF
1305 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1306 && (!ref2->ref
1307 || TREE_CODE (ref2->ref) == SSA_NAME
1308 || DECL_P (ref2->ref)
1309 || TREE_CODE (ref2->ref) == STRING_CST
1310 || handled_component_p (ref2->ref)
1311 || TREE_CODE (ref2->ref) == MEM_REF
1312 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1314 /* Decompose the references into their base objects and the access. */
1315 base1 = ao_ref_base (ref1);
1316 offset1 = ref1->offset;
1317 max_size1 = ref1->max_size;
1318 base2 = ao_ref_base (ref2);
1319 offset2 = ref2->offset;
1320 max_size2 = ref2->max_size;
1322 /* We can end up with registers or constants as bases for example from
1323 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1324 which is seen as a struct copy. */
1325 if (TREE_CODE (base1) == SSA_NAME
1326 || TREE_CODE (base1) == CONST_DECL
1327 || TREE_CODE (base1) == CONSTRUCTOR
1328 || TREE_CODE (base1) == ADDR_EXPR
1329 || CONSTANT_CLASS_P (base1)
1330 || TREE_CODE (base2) == SSA_NAME
1331 || TREE_CODE (base2) == CONST_DECL
1332 || TREE_CODE (base2) == CONSTRUCTOR
1333 || TREE_CODE (base2) == ADDR_EXPR
1334 || CONSTANT_CLASS_P (base2))
1335 return false;
1337 /* We can end up referring to code via function and label decls.
1338 As we likely do not properly track code aliases conservatively
1339 bail out. */
1340 if (TREE_CODE (base1) == FUNCTION_DECL
1341 || TREE_CODE (base1) == LABEL_DECL
1342 || TREE_CODE (base2) == FUNCTION_DECL
1343 || TREE_CODE (base2) == LABEL_DECL)
1344 return true;
1346 /* Two volatile accesses always conflict. */
1347 if (ref1->volatile_p
1348 && ref2->volatile_p)
1349 return true;
1351 /* Defer to simple offset based disambiguation if we have
1352 references based on two decls. Do this before defering to
1353 TBAA to handle must-alias cases in conformance with the
1354 GCC extension of allowing type-punning through unions. */
1355 var1_p = DECL_P (base1);
1356 var2_p = DECL_P (base2);
1357 if (var1_p && var2_p)
1358 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1359 ref2->ref, base2, offset2, max_size2);
1361 ind1_p = (TREE_CODE (base1) == MEM_REF
1362 || TREE_CODE (base1) == TARGET_MEM_REF);
1363 ind2_p = (TREE_CODE (base2) == MEM_REF
1364 || TREE_CODE (base2) == TARGET_MEM_REF);
1366 /* Canonicalize the pointer-vs-decl case. */
1367 if (ind1_p && var2_p)
1369 HOST_WIDE_INT tmp1;
1370 tree tmp2;
1371 ao_ref *tmp3;
1372 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1373 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1374 tmp2 = base1; base1 = base2; base2 = tmp2;
1375 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1376 var1_p = true;
1377 ind1_p = false;
1378 var2_p = false;
1379 ind2_p = true;
1382 /* First defer to TBAA if possible. */
1383 if (tbaa_p
1384 && flag_strict_aliasing
1385 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1386 ao_ref_alias_set (ref2)))
1387 return false;
1389 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1390 if (var1_p && ind2_p)
1391 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1392 offset2, max_size2,
1393 ao_ref_alias_set (ref2), -1,
1394 ref1->ref, base1,
1395 offset1, max_size1,
1396 ao_ref_alias_set (ref1),
1397 ao_ref_base_alias_set (ref1),
1398 tbaa_p);
1399 else if (ind1_p && ind2_p)
1400 return indirect_refs_may_alias_p (ref1->ref, base1,
1401 offset1, max_size1,
1402 ao_ref_alias_set (ref1), -1,
1403 ref2->ref, base2,
1404 offset2, max_size2,
1405 ao_ref_alias_set (ref2), -1,
1406 tbaa_p);
1408 /* We really do not want to end up here, but returning true is safe. */
1409 #ifdef ENABLE_CHECKING
1410 gcc_unreachable ();
1411 #else
1412 return true;
1413 #endif
1416 bool
1417 refs_may_alias_p (tree ref1, tree ref2)
1419 ao_ref r1, r2;
1420 bool res;
1421 ao_ref_init (&r1, ref1);
1422 ao_ref_init (&r2, ref2);
1423 res = refs_may_alias_p_1 (&r1, &r2, true);
1424 if (res)
1425 ++alias_stats.refs_may_alias_p_may_alias;
1426 else
1427 ++alias_stats.refs_may_alias_p_no_alias;
1428 return res;
1431 /* Returns true if there is a anti-dependence for the STORE that
1432 executes after the LOAD. */
1434 bool
1435 refs_anti_dependent_p (tree load, tree store)
1437 ao_ref r1, r2;
1438 ao_ref_init (&r1, load);
1439 ao_ref_init (&r2, store);
1440 return refs_may_alias_p_1 (&r1, &r2, false);
1443 /* Returns true if there is a output dependence for the stores
1444 STORE1 and STORE2. */
1446 bool
1447 refs_output_dependent_p (tree store1, tree store2)
1449 ao_ref r1, r2;
1450 ao_ref_init (&r1, store1);
1451 ao_ref_init (&r2, store2);
1452 return refs_may_alias_p_1 (&r1, &r2, false);
1455 /* If the call CALL may use the memory reference REF return true,
1456 otherwise return false. */
1458 static bool
1459 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1461 tree base, callee;
1462 unsigned i;
1463 int flags = gimple_call_flags (call);
1465 /* Const functions without a static chain do not implicitly use memory. */
1466 if (!gimple_call_chain (call)
1467 && (flags & (ECF_CONST|ECF_NOVOPS)))
1468 goto process_args;
1470 base = ao_ref_base (ref);
1471 if (!base)
1472 return true;
1474 /* A call that is not without side-effects might involve volatile
1475 accesses and thus conflicts with all other volatile accesses. */
1476 if (ref->volatile_p)
1477 return true;
1479 /* If the reference is based on a decl that is not aliased the call
1480 cannot possibly use it. */
1481 if (DECL_P (base)
1482 && !may_be_aliased (base)
1483 /* But local statics can be used through recursion. */
1484 && !is_global_var (base))
1485 goto process_args;
1487 callee = gimple_call_fndecl (call);
1489 /* Handle those builtin functions explicitly that do not act as
1490 escape points. See tree-ssa-structalias.c:find_func_aliases
1491 for the list of builtins we might need to handle here. */
1492 if (callee != NULL_TREE
1493 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1494 switch (DECL_FUNCTION_CODE (callee))
1496 /* All the following functions read memory pointed to by
1497 their second argument. strcat/strncat additionally
1498 reads memory pointed to by the first argument. */
1499 case BUILT_IN_STRCAT:
1500 case BUILT_IN_STRNCAT:
1502 ao_ref dref;
1503 ao_ref_init_from_ptr_and_size (&dref,
1504 gimple_call_arg (call, 0),
1505 NULL_TREE);
1506 if (refs_may_alias_p_1 (&dref, ref, false))
1507 return true;
1509 /* FALLTHRU */
1510 case BUILT_IN_STRCPY:
1511 case BUILT_IN_STRNCPY:
1512 case BUILT_IN_MEMCPY:
1513 case BUILT_IN_MEMMOVE:
1514 case BUILT_IN_MEMPCPY:
1515 case BUILT_IN_STPCPY:
1516 case BUILT_IN_STPNCPY:
1517 case BUILT_IN_TM_MEMCPY:
1518 case BUILT_IN_TM_MEMMOVE:
1520 ao_ref dref;
1521 tree size = NULL_TREE;
1522 if (gimple_call_num_args (call) == 3)
1523 size = gimple_call_arg (call, 2);
1524 ao_ref_init_from_ptr_and_size (&dref,
1525 gimple_call_arg (call, 1),
1526 size);
1527 return refs_may_alias_p_1 (&dref, ref, false);
1529 case BUILT_IN_STRCAT_CHK:
1530 case BUILT_IN_STRNCAT_CHK:
1532 ao_ref dref;
1533 ao_ref_init_from_ptr_and_size (&dref,
1534 gimple_call_arg (call, 0),
1535 NULL_TREE);
1536 if (refs_may_alias_p_1 (&dref, ref, false))
1537 return true;
1539 /* FALLTHRU */
1540 case BUILT_IN_STRCPY_CHK:
1541 case BUILT_IN_STRNCPY_CHK:
1542 case BUILT_IN_MEMCPY_CHK:
1543 case BUILT_IN_MEMMOVE_CHK:
1544 case BUILT_IN_MEMPCPY_CHK:
1545 case BUILT_IN_STPCPY_CHK:
1546 case BUILT_IN_STPNCPY_CHK:
1548 ao_ref dref;
1549 tree size = NULL_TREE;
1550 if (gimple_call_num_args (call) == 4)
1551 size = gimple_call_arg (call, 2);
1552 ao_ref_init_from_ptr_and_size (&dref,
1553 gimple_call_arg (call, 1),
1554 size);
1555 return refs_may_alias_p_1 (&dref, ref, false);
1557 case BUILT_IN_BCOPY:
1559 ao_ref dref;
1560 tree size = gimple_call_arg (call, 2);
1561 ao_ref_init_from_ptr_and_size (&dref,
1562 gimple_call_arg (call, 0),
1563 size);
1564 return refs_may_alias_p_1 (&dref, ref, false);
1567 /* The following functions read memory pointed to by their
1568 first argument. */
1569 CASE_BUILT_IN_TM_LOAD (1):
1570 CASE_BUILT_IN_TM_LOAD (2):
1571 CASE_BUILT_IN_TM_LOAD (4):
1572 CASE_BUILT_IN_TM_LOAD (8):
1573 CASE_BUILT_IN_TM_LOAD (FLOAT):
1574 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1575 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1576 CASE_BUILT_IN_TM_LOAD (M64):
1577 CASE_BUILT_IN_TM_LOAD (M128):
1578 CASE_BUILT_IN_TM_LOAD (M256):
1579 case BUILT_IN_TM_LOG:
1580 case BUILT_IN_TM_LOG_1:
1581 case BUILT_IN_TM_LOG_2:
1582 case BUILT_IN_TM_LOG_4:
1583 case BUILT_IN_TM_LOG_8:
1584 case BUILT_IN_TM_LOG_FLOAT:
1585 case BUILT_IN_TM_LOG_DOUBLE:
1586 case BUILT_IN_TM_LOG_LDOUBLE:
1587 case BUILT_IN_TM_LOG_M64:
1588 case BUILT_IN_TM_LOG_M128:
1589 case BUILT_IN_TM_LOG_M256:
1590 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1592 /* These read memory pointed to by the first argument. */
1593 case BUILT_IN_STRDUP:
1594 case BUILT_IN_STRNDUP:
1595 case BUILT_IN_REALLOC:
1597 ao_ref dref;
1598 tree size = NULL_TREE;
1599 if (gimple_call_num_args (call) == 2)
1600 size = gimple_call_arg (call, 1);
1601 ao_ref_init_from_ptr_and_size (&dref,
1602 gimple_call_arg (call, 0),
1603 size);
1604 return refs_may_alias_p_1 (&dref, ref, false);
1606 /* These read memory pointed to by the first argument. */
1607 case BUILT_IN_INDEX:
1608 case BUILT_IN_STRCHR:
1609 case BUILT_IN_STRRCHR:
1611 ao_ref dref;
1612 ao_ref_init_from_ptr_and_size (&dref,
1613 gimple_call_arg (call, 0),
1614 NULL_TREE);
1615 return refs_may_alias_p_1 (&dref, ref, false);
1617 /* These read memory pointed to by the first argument with size
1618 in the third argument. */
1619 case BUILT_IN_MEMCHR:
1621 ao_ref dref;
1622 ao_ref_init_from_ptr_and_size (&dref,
1623 gimple_call_arg (call, 0),
1624 gimple_call_arg (call, 2));
1625 return refs_may_alias_p_1 (&dref, ref, false);
1627 /* These read memory pointed to by the first and second arguments. */
1628 case BUILT_IN_STRSTR:
1629 case BUILT_IN_STRPBRK:
1631 ao_ref dref;
1632 ao_ref_init_from_ptr_and_size (&dref,
1633 gimple_call_arg (call, 0),
1634 NULL_TREE);
1635 if (refs_may_alias_p_1 (&dref, ref, false))
1636 return true;
1637 ao_ref_init_from_ptr_and_size (&dref,
1638 gimple_call_arg (call, 1),
1639 NULL_TREE);
1640 return refs_may_alias_p_1 (&dref, ref, false);
1643 /* The following builtins do not read from memory. */
1644 case BUILT_IN_FREE:
1645 case BUILT_IN_MALLOC:
1646 case BUILT_IN_POSIX_MEMALIGN:
1647 case BUILT_IN_ALIGNED_ALLOC:
1648 case BUILT_IN_CALLOC:
1649 case BUILT_IN_ALLOCA:
1650 case BUILT_IN_ALLOCA_WITH_ALIGN:
1651 case BUILT_IN_STACK_SAVE:
1652 case BUILT_IN_STACK_RESTORE:
1653 case BUILT_IN_MEMSET:
1654 case BUILT_IN_TM_MEMSET:
1655 case BUILT_IN_MEMSET_CHK:
1656 case BUILT_IN_FREXP:
1657 case BUILT_IN_FREXPF:
1658 case BUILT_IN_FREXPL:
1659 case BUILT_IN_GAMMA_R:
1660 case BUILT_IN_GAMMAF_R:
1661 case BUILT_IN_GAMMAL_R:
1662 case BUILT_IN_LGAMMA_R:
1663 case BUILT_IN_LGAMMAF_R:
1664 case BUILT_IN_LGAMMAL_R:
1665 case BUILT_IN_MODF:
1666 case BUILT_IN_MODFF:
1667 case BUILT_IN_MODFL:
1668 case BUILT_IN_REMQUO:
1669 case BUILT_IN_REMQUOF:
1670 case BUILT_IN_REMQUOL:
1671 case BUILT_IN_SINCOS:
1672 case BUILT_IN_SINCOSF:
1673 case BUILT_IN_SINCOSL:
1674 case BUILT_IN_ASSUME_ALIGNED:
1675 case BUILT_IN_VA_END:
1676 return false;
1677 /* __sync_* builtins and some OpenMP builtins act as threading
1678 barriers. */
1679 #undef DEF_SYNC_BUILTIN
1680 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1681 #include "sync-builtins.def"
1682 #undef DEF_SYNC_BUILTIN
1683 case BUILT_IN_GOMP_ATOMIC_START:
1684 case BUILT_IN_GOMP_ATOMIC_END:
1685 case BUILT_IN_GOMP_BARRIER:
1686 case BUILT_IN_GOMP_BARRIER_CANCEL:
1687 case BUILT_IN_GOMP_TASKWAIT:
1688 case BUILT_IN_GOMP_TASKGROUP_END:
1689 case BUILT_IN_GOMP_CRITICAL_START:
1690 case BUILT_IN_GOMP_CRITICAL_END:
1691 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1692 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1693 case BUILT_IN_GOMP_LOOP_END:
1694 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1695 case BUILT_IN_GOMP_ORDERED_START:
1696 case BUILT_IN_GOMP_ORDERED_END:
1697 case BUILT_IN_GOMP_SECTIONS_END:
1698 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1699 case BUILT_IN_GOMP_SINGLE_COPY_START:
1700 case BUILT_IN_GOMP_SINGLE_COPY_END:
1701 return true;
1703 default:
1704 /* Fallthru to general call handling. */;
1707 /* Check if base is a global static variable that is not read
1708 by the function. */
1709 if (callee != NULL_TREE
1710 && TREE_CODE (base) == VAR_DECL
1711 && TREE_STATIC (base))
1713 struct cgraph_node *node = cgraph_node::get (callee);
1714 bitmap not_read;
1716 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1717 node yet. We should enforce that there are nodes for all decls in the
1718 IL and remove this check instead. */
1719 if (node
1720 && (not_read = ipa_reference_get_not_read_global (node))
1721 && bitmap_bit_p (not_read, DECL_UID (base)))
1722 goto process_args;
1725 /* Check if the base variable is call-used. */
1726 if (DECL_P (base))
1728 if (pt_solution_includes (gimple_call_use_set (call), base))
1729 return true;
1731 else if ((TREE_CODE (base) == MEM_REF
1732 || TREE_CODE (base) == TARGET_MEM_REF)
1733 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1735 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1736 if (!pi)
1737 return true;
1739 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1740 return true;
1742 else
1743 return true;
1745 /* Inspect call arguments for passed-by-value aliases. */
1746 process_args:
1747 for (i = 0; i < gimple_call_num_args (call); ++i)
1749 tree op = gimple_call_arg (call, i);
1750 int flags = gimple_call_arg_flags (call, i);
1752 if (flags & EAF_UNUSED)
1753 continue;
1755 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1756 op = TREE_OPERAND (op, 0);
1758 if (TREE_CODE (op) != SSA_NAME
1759 && !is_gimple_min_invariant (op))
1761 ao_ref r;
1762 ao_ref_init (&r, op);
1763 if (refs_may_alias_p_1 (&r, ref, true))
1764 return true;
1768 return false;
1771 static bool
1772 ref_maybe_used_by_call_p (gimple call, tree ref)
1774 ao_ref r;
1775 bool res;
1776 ao_ref_init (&r, ref);
1777 res = ref_maybe_used_by_call_p_1 (call, &r);
1778 if (res)
1779 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1780 else
1781 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1782 return res;
1786 /* If the statement STMT may use the memory reference REF return
1787 true, otherwise return false. */
1789 bool
1790 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1792 if (is_gimple_assign (stmt))
1794 tree rhs;
1796 /* All memory assign statements are single. */
1797 if (!gimple_assign_single_p (stmt))
1798 return false;
1800 rhs = gimple_assign_rhs1 (stmt);
1801 if (is_gimple_reg (rhs)
1802 || is_gimple_min_invariant (rhs)
1803 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1804 return false;
1806 return refs_may_alias_p (rhs, ref);
1808 else if (is_gimple_call (stmt))
1809 return ref_maybe_used_by_call_p (stmt, ref);
1810 else if (gimple_code (stmt) == GIMPLE_RETURN)
1812 tree retval = gimple_return_retval (stmt);
1813 tree base;
1814 if (retval
1815 && TREE_CODE (retval) != SSA_NAME
1816 && !is_gimple_min_invariant (retval)
1817 && refs_may_alias_p (retval, ref))
1818 return true;
1819 /* If ref escapes the function then the return acts as a use. */
1820 base = get_base_address (ref);
1821 if (!base)
1823 else if (DECL_P (base))
1824 return is_global_var (base);
1825 else if (TREE_CODE (base) == MEM_REF
1826 || TREE_CODE (base) == TARGET_MEM_REF)
1827 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1828 return false;
1831 return true;
1834 /* If the call in statement CALL may clobber the memory reference REF
1835 return true, otherwise return false. */
1837 bool
1838 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1840 tree base;
1841 tree callee;
1843 /* If the call is pure or const it cannot clobber anything. */
1844 if (gimple_call_flags (call)
1845 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1846 return false;
1848 base = ao_ref_base (ref);
1849 if (!base)
1850 return true;
1852 if (TREE_CODE (base) == SSA_NAME
1853 || CONSTANT_CLASS_P (base))
1854 return false;
1856 /* A call that is not without side-effects might involve volatile
1857 accesses and thus conflicts with all other volatile accesses. */
1858 if (ref->volatile_p)
1859 return true;
1861 /* If the reference is based on a decl that is not aliased the call
1862 cannot possibly clobber it. */
1863 if (DECL_P (base)
1864 && !may_be_aliased (base)
1865 /* But local non-readonly statics can be modified through recursion
1866 or the call may implement a threading barrier which we must
1867 treat as may-def. */
1868 && (TREE_READONLY (base)
1869 || !is_global_var (base)))
1870 return false;
1872 callee = gimple_call_fndecl (call);
1874 /* Handle those builtin functions explicitly that do not act as
1875 escape points. See tree-ssa-structalias.c:find_func_aliases
1876 for the list of builtins we might need to handle here. */
1877 if (callee != NULL_TREE
1878 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1879 switch (DECL_FUNCTION_CODE (callee))
1881 /* All the following functions clobber memory pointed to by
1882 their first argument. */
1883 case BUILT_IN_STRCPY:
1884 case BUILT_IN_STRNCPY:
1885 case BUILT_IN_MEMCPY:
1886 case BUILT_IN_MEMMOVE:
1887 case BUILT_IN_MEMPCPY:
1888 case BUILT_IN_STPCPY:
1889 case BUILT_IN_STPNCPY:
1890 case BUILT_IN_STRCAT:
1891 case BUILT_IN_STRNCAT:
1892 case BUILT_IN_MEMSET:
1893 case BUILT_IN_TM_MEMSET:
1894 CASE_BUILT_IN_TM_STORE (1):
1895 CASE_BUILT_IN_TM_STORE (2):
1896 CASE_BUILT_IN_TM_STORE (4):
1897 CASE_BUILT_IN_TM_STORE (8):
1898 CASE_BUILT_IN_TM_STORE (FLOAT):
1899 CASE_BUILT_IN_TM_STORE (DOUBLE):
1900 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1901 CASE_BUILT_IN_TM_STORE (M64):
1902 CASE_BUILT_IN_TM_STORE (M128):
1903 CASE_BUILT_IN_TM_STORE (M256):
1904 case BUILT_IN_TM_MEMCPY:
1905 case BUILT_IN_TM_MEMMOVE:
1907 ao_ref dref;
1908 tree size = NULL_TREE;
1909 /* Don't pass in size for strncat, as the maximum size
1910 is strlen (dest) + n + 1 instead of n, resp.
1911 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1912 known. */
1913 if (gimple_call_num_args (call) == 3
1914 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1915 size = gimple_call_arg (call, 2);
1916 ao_ref_init_from_ptr_and_size (&dref,
1917 gimple_call_arg (call, 0),
1918 size);
1919 return refs_may_alias_p_1 (&dref, ref, false);
1921 case BUILT_IN_STRCPY_CHK:
1922 case BUILT_IN_STRNCPY_CHK:
1923 case BUILT_IN_MEMCPY_CHK:
1924 case BUILT_IN_MEMMOVE_CHK:
1925 case BUILT_IN_MEMPCPY_CHK:
1926 case BUILT_IN_STPCPY_CHK:
1927 case BUILT_IN_STPNCPY_CHK:
1928 case BUILT_IN_STRCAT_CHK:
1929 case BUILT_IN_STRNCAT_CHK:
1930 case BUILT_IN_MEMSET_CHK:
1932 ao_ref dref;
1933 tree size = NULL_TREE;
1934 /* Don't pass in size for __strncat_chk, as the maximum size
1935 is strlen (dest) + n + 1 instead of n, resp.
1936 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1937 known. */
1938 if (gimple_call_num_args (call) == 4
1939 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1940 size = gimple_call_arg (call, 2);
1941 ao_ref_init_from_ptr_and_size (&dref,
1942 gimple_call_arg (call, 0),
1943 size);
1944 return refs_may_alias_p_1 (&dref, ref, false);
1946 case BUILT_IN_BCOPY:
1948 ao_ref dref;
1949 tree size = gimple_call_arg (call, 2);
1950 ao_ref_init_from_ptr_and_size (&dref,
1951 gimple_call_arg (call, 1),
1952 size);
1953 return refs_may_alias_p_1 (&dref, ref, false);
1955 /* Allocating memory does not have any side-effects apart from
1956 being the definition point for the pointer. */
1957 case BUILT_IN_MALLOC:
1958 case BUILT_IN_ALIGNED_ALLOC:
1959 case BUILT_IN_CALLOC:
1960 case BUILT_IN_STRDUP:
1961 case BUILT_IN_STRNDUP:
1962 /* Unix98 specifies that errno is set on allocation failure. */
1963 if (flag_errno_math
1964 && targetm.ref_may_alias_errno (ref))
1965 return true;
1966 return false;
1967 case BUILT_IN_STACK_SAVE:
1968 case BUILT_IN_ALLOCA:
1969 case BUILT_IN_ALLOCA_WITH_ALIGN:
1970 case BUILT_IN_ASSUME_ALIGNED:
1971 return false;
1972 /* But posix_memalign stores a pointer into the memory pointed to
1973 by its first argument. */
1974 case BUILT_IN_POSIX_MEMALIGN:
1976 tree ptrptr = gimple_call_arg (call, 0);
1977 ao_ref dref;
1978 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
1979 TYPE_SIZE_UNIT (ptr_type_node));
1980 return (refs_may_alias_p_1 (&dref, ref, false)
1981 || (flag_errno_math
1982 && targetm.ref_may_alias_errno (ref)));
1984 /* Freeing memory kills the pointed-to memory. More importantly
1985 the call has to serve as a barrier for moving loads and stores
1986 across it. */
1987 case BUILT_IN_FREE:
1988 case BUILT_IN_VA_END:
1990 tree ptr = gimple_call_arg (call, 0);
1991 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1993 /* Realloc serves both as allocation point and deallocation point. */
1994 case BUILT_IN_REALLOC:
1996 tree ptr = gimple_call_arg (call, 0);
1997 /* Unix98 specifies that errno is set on allocation failure. */
1998 return ((flag_errno_math
1999 && targetm.ref_may_alias_errno (ref))
2000 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2002 case BUILT_IN_GAMMA_R:
2003 case BUILT_IN_GAMMAF_R:
2004 case BUILT_IN_GAMMAL_R:
2005 case BUILT_IN_LGAMMA_R:
2006 case BUILT_IN_LGAMMAF_R:
2007 case BUILT_IN_LGAMMAL_R:
2009 tree out = gimple_call_arg (call, 1);
2010 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2011 return true;
2012 if (flag_errno_math)
2013 break;
2014 return false;
2016 case BUILT_IN_FREXP:
2017 case BUILT_IN_FREXPF:
2018 case BUILT_IN_FREXPL:
2019 case BUILT_IN_MODF:
2020 case BUILT_IN_MODFF:
2021 case BUILT_IN_MODFL:
2023 tree out = gimple_call_arg (call, 1);
2024 return ptr_deref_may_alias_ref_p_1 (out, ref);
2026 case BUILT_IN_REMQUO:
2027 case BUILT_IN_REMQUOF:
2028 case BUILT_IN_REMQUOL:
2030 tree out = gimple_call_arg (call, 2);
2031 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2032 return true;
2033 if (flag_errno_math)
2034 break;
2035 return false;
2037 case BUILT_IN_SINCOS:
2038 case BUILT_IN_SINCOSF:
2039 case BUILT_IN_SINCOSL:
2041 tree sin = gimple_call_arg (call, 1);
2042 tree cos = gimple_call_arg (call, 2);
2043 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2044 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2046 /* __sync_* builtins and some OpenMP builtins act as threading
2047 barriers. */
2048 #undef DEF_SYNC_BUILTIN
2049 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2050 #include "sync-builtins.def"
2051 #undef DEF_SYNC_BUILTIN
2052 case BUILT_IN_GOMP_ATOMIC_START:
2053 case BUILT_IN_GOMP_ATOMIC_END:
2054 case BUILT_IN_GOMP_BARRIER:
2055 case BUILT_IN_GOMP_BARRIER_CANCEL:
2056 case BUILT_IN_GOMP_TASKWAIT:
2057 case BUILT_IN_GOMP_TASKGROUP_END:
2058 case BUILT_IN_GOMP_CRITICAL_START:
2059 case BUILT_IN_GOMP_CRITICAL_END:
2060 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2061 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2062 case BUILT_IN_GOMP_LOOP_END:
2063 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2064 case BUILT_IN_GOMP_ORDERED_START:
2065 case BUILT_IN_GOMP_ORDERED_END:
2066 case BUILT_IN_GOMP_SECTIONS_END:
2067 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2068 case BUILT_IN_GOMP_SINGLE_COPY_START:
2069 case BUILT_IN_GOMP_SINGLE_COPY_END:
2070 return true;
2071 default:
2072 /* Fallthru to general call handling. */;
2075 /* Check if base is a global static variable that is not written
2076 by the function. */
2077 if (callee != NULL_TREE
2078 && TREE_CODE (base) == VAR_DECL
2079 && TREE_STATIC (base))
2081 struct cgraph_node *node = cgraph_node::get (callee);
2082 bitmap not_written;
2084 if (node
2085 && (not_written = ipa_reference_get_not_written_global (node))
2086 && bitmap_bit_p (not_written, DECL_UID (base)))
2087 return false;
2090 /* Check if the base variable is call-clobbered. */
2091 if (DECL_P (base))
2092 return pt_solution_includes (gimple_call_clobber_set (call), base);
2093 else if ((TREE_CODE (base) == MEM_REF
2094 || TREE_CODE (base) == TARGET_MEM_REF)
2095 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2097 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2098 if (!pi)
2099 return true;
2101 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2104 return true;
2107 /* If the call in statement CALL may clobber the memory reference REF
2108 return true, otherwise return false. */
2110 bool
2111 call_may_clobber_ref_p (gimple call, tree ref)
2113 bool res;
2114 ao_ref r;
2115 ao_ref_init (&r, ref);
2116 res = call_may_clobber_ref_p_1 (call, &r);
2117 if (res)
2118 ++alias_stats.call_may_clobber_ref_p_may_alias;
2119 else
2120 ++alias_stats.call_may_clobber_ref_p_no_alias;
2121 return res;
2125 /* If the statement STMT may clobber the memory reference REF return true,
2126 otherwise return false. */
2128 bool
2129 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2131 if (is_gimple_call (stmt))
2133 tree lhs = gimple_call_lhs (stmt);
2134 if (lhs
2135 && TREE_CODE (lhs) != SSA_NAME)
2137 ao_ref r;
2138 ao_ref_init (&r, lhs);
2139 if (refs_may_alias_p_1 (ref, &r, true))
2140 return true;
2143 return call_may_clobber_ref_p_1 (stmt, ref);
2145 else if (gimple_assign_single_p (stmt))
2147 tree lhs = gimple_assign_lhs (stmt);
2148 if (TREE_CODE (lhs) != SSA_NAME)
2150 ao_ref r;
2151 ao_ref_init (&r, lhs);
2152 return refs_may_alias_p_1 (ref, &r, true);
2155 else if (gimple_code (stmt) == GIMPLE_ASM)
2156 return true;
2158 return false;
2161 bool
2162 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2164 ao_ref r;
2165 ao_ref_init (&r, ref);
2166 return stmt_may_clobber_ref_p_1 (stmt, &r);
2169 /* If STMT kills the memory reference REF return true, otherwise
2170 return false. */
2172 static bool
2173 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
2175 if (!ao_ref_base (ref))
2176 return false;
2178 if (gimple_has_lhs (stmt)
2179 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2180 /* The assignment is not necessarily carried out if it can throw
2181 and we can catch it in the current function where we could inspect
2182 the previous value.
2183 ??? We only need to care about the RHS throwing. For aggregate
2184 assignments or similar calls and non-call exceptions the LHS
2185 might throw as well. */
2186 && !stmt_can_throw_internal (stmt))
2188 tree lhs = gimple_get_lhs (stmt);
2189 /* If LHS is literally a base of the access we are done. */
2190 if (ref->ref)
2192 tree base = ref->ref;
2193 if (handled_component_p (base))
2195 tree saved_lhs0 = NULL_TREE;
2196 if (handled_component_p (lhs))
2198 saved_lhs0 = TREE_OPERAND (lhs, 0);
2199 TREE_OPERAND (lhs, 0) = integer_zero_node;
2203 /* Just compare the outermost handled component, if
2204 they are equal we have found a possible common
2205 base. */
2206 tree saved_base0 = TREE_OPERAND (base, 0);
2207 TREE_OPERAND (base, 0) = integer_zero_node;
2208 bool res = operand_equal_p (lhs, base, 0);
2209 TREE_OPERAND (base, 0) = saved_base0;
2210 if (res)
2211 break;
2212 /* Otherwise drop handled components of the access. */
2213 base = saved_base0;
2215 while (handled_component_p (base));
2216 if (saved_lhs0)
2217 TREE_OPERAND (lhs, 0) = saved_lhs0;
2219 /* Finally check if lhs is equal or equal to the base candidate
2220 of the access. */
2221 if (operand_equal_p (lhs, base, 0))
2222 return true;
2225 /* Now look for non-literal equal bases with the restriction of
2226 handling constant offset and size. */
2227 /* For a must-alias check we need to be able to constrain
2228 the access properly. */
2229 if (ref->max_size == -1)
2230 return false;
2231 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2232 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2233 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2234 so base == ref->base does not always hold. */
2235 if (base != ref->base)
2237 /* If both base and ref->base are MEM_REFs, only compare the
2238 first operand, and if the second operand isn't equal constant,
2239 try to add the offsets into offset and ref_offset. */
2240 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2241 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2243 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2244 TREE_OPERAND (ref->base, 1)))
2246 offset_int off1 = mem_ref_offset (base);
2247 off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2248 off1 += offset;
2249 offset_int off2 = mem_ref_offset (ref->base);
2250 off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2251 off2 += ref_offset;
2252 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2254 offset = off1.to_shwi ();
2255 ref_offset = off2.to_shwi ();
2257 else
2258 size = -1;
2261 else
2262 size = -1;
2264 /* For a must-alias check we need to be able to constrain
2265 the access properly. */
2266 if (size != -1 && size == max_size)
2268 if (offset <= ref_offset
2269 && offset + size >= ref_offset + ref->max_size)
2270 return true;
2274 if (is_gimple_call (stmt))
2276 tree callee = gimple_call_fndecl (stmt);
2277 if (callee != NULL_TREE
2278 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2279 switch (DECL_FUNCTION_CODE (callee))
2281 case BUILT_IN_FREE:
2283 tree ptr = gimple_call_arg (stmt, 0);
2284 tree base = ao_ref_base (ref);
2285 if (base && TREE_CODE (base) == MEM_REF
2286 && TREE_OPERAND (base, 0) == ptr)
2287 return true;
2288 break;
2291 case BUILT_IN_MEMCPY:
2292 case BUILT_IN_MEMPCPY:
2293 case BUILT_IN_MEMMOVE:
2294 case BUILT_IN_MEMSET:
2295 case BUILT_IN_MEMCPY_CHK:
2296 case BUILT_IN_MEMPCPY_CHK:
2297 case BUILT_IN_MEMMOVE_CHK:
2298 case BUILT_IN_MEMSET_CHK:
2300 /* For a must-alias check we need to be able to constrain
2301 the access properly. */
2302 if (ref->max_size == -1)
2303 return false;
2304 tree dest = gimple_call_arg (stmt, 0);
2305 tree len = gimple_call_arg (stmt, 2);
2306 if (!tree_fits_shwi_p (len))
2307 return false;
2308 tree rbase = ref->base;
2309 offset_int roffset = ref->offset;
2310 ao_ref dref;
2311 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2312 tree base = ao_ref_base (&dref);
2313 offset_int offset = dref.offset;
2314 if (!base || dref.size == -1)
2315 return false;
2316 if (TREE_CODE (base) == MEM_REF)
2318 if (TREE_CODE (rbase) != MEM_REF)
2319 return false;
2320 // Compare pointers.
2321 offset += wi::lshift (mem_ref_offset (base),
2322 LOG2_BITS_PER_UNIT);
2323 roffset += wi::lshift (mem_ref_offset (rbase),
2324 LOG2_BITS_PER_UNIT);
2325 base = TREE_OPERAND (base, 0);
2326 rbase = TREE_OPERAND (rbase, 0);
2328 if (base == rbase
2329 && wi::les_p (offset, roffset)
2330 && wi::les_p (roffset + ref->max_size,
2331 offset + wi::lshift (wi::to_offset (len),
2332 LOG2_BITS_PER_UNIT)))
2333 return true;
2334 break;
2337 case BUILT_IN_VA_END:
2339 tree ptr = gimple_call_arg (stmt, 0);
2340 if (TREE_CODE (ptr) == ADDR_EXPR)
2342 tree base = ao_ref_base (ref);
2343 if (TREE_OPERAND (ptr, 0) == base)
2344 return true;
2346 break;
2349 default:;
2352 return false;
2355 bool
2356 stmt_kills_ref_p (gimple stmt, tree ref)
2358 ao_ref r;
2359 ao_ref_init (&r, ref);
2360 return stmt_kills_ref_p_1 (stmt, &r);
2364 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2365 TARGET or a statement clobbering the memory reference REF in which
2366 case false is returned. The walk starts with VUSE, one argument of PHI. */
2368 static bool
2369 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2370 tree vuse, unsigned int *cnt, bitmap *visited,
2371 bool abort_on_visited,
2372 void *(*translate)(ao_ref *, tree, void *, bool),
2373 void *data)
2375 basic_block bb = gimple_bb (phi);
2377 if (!*visited)
2378 *visited = BITMAP_ALLOC (NULL);
2380 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2382 /* Walk until we hit the target. */
2383 while (vuse != target)
2385 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2386 /* Recurse for PHI nodes. */
2387 if (gimple_code (def_stmt) == GIMPLE_PHI)
2389 /* An already visited PHI node ends the walk successfully. */
2390 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2391 return !abort_on_visited;
2392 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2393 visited, abort_on_visited,
2394 translate, data);
2395 if (!vuse)
2396 return false;
2397 continue;
2399 else if (gimple_nop_p (def_stmt))
2400 return false;
2401 else
2403 /* A clobbering statement or the end of the IL ends it failing. */
2404 ++*cnt;
2405 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2407 if (translate
2408 && (*translate) (ref, vuse, data, true) == NULL)
2410 else
2411 return false;
2414 /* If we reach a new basic-block see if we already skipped it
2415 in a previous walk that ended successfully. */
2416 if (gimple_bb (def_stmt) != bb)
2418 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2419 return !abort_on_visited;
2420 bb = gimple_bb (def_stmt);
2422 vuse = gimple_vuse (def_stmt);
2424 return true;
2427 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2428 until we hit the phi argument definition that dominates the other one.
2429 Return that, or NULL_TREE if there is no such definition. */
2431 static tree
2432 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2433 ao_ref *ref, unsigned int *cnt,
2434 bitmap *visited, bool abort_on_visited,
2435 void *(*translate)(ao_ref *, tree, void *, bool),
2436 void *data)
2438 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2439 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2440 tree common_vuse;
2442 if (arg0 == arg1)
2443 return arg0;
2444 else if (gimple_nop_p (def0)
2445 || (!gimple_nop_p (def1)
2446 && dominated_by_p (CDI_DOMINATORS,
2447 gimple_bb (def1), gimple_bb (def0))))
2449 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2450 visited, abort_on_visited, translate, data))
2451 return arg0;
2453 else if (gimple_nop_p (def1)
2454 || dominated_by_p (CDI_DOMINATORS,
2455 gimple_bb (def0), gimple_bb (def1)))
2457 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2458 visited, abort_on_visited, translate, data))
2459 return arg1;
2461 /* Special case of a diamond:
2462 MEM_1 = ...
2463 goto (cond) ? L1 : L2
2464 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2465 goto L3
2466 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2467 L3: MEM_4 = PHI<MEM_2, MEM_3>
2468 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2469 dominate each other, but still we can easily skip this PHI node
2470 if we recognize that the vuse MEM operand is the same for both,
2471 and that we can skip both statements (they don't clobber us).
2472 This is still linear. Don't use maybe_skip_until, that might
2473 potentially be slow. */
2474 else if ((common_vuse = gimple_vuse (def0))
2475 && common_vuse == gimple_vuse (def1))
2477 *cnt += 2;
2478 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2479 || (translate
2480 && (*translate) (ref, arg0, data, true) == NULL))
2481 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2482 || (translate
2483 && (*translate) (ref, arg1, data, true) == NULL)))
2484 return common_vuse;
2487 return NULL_TREE;
2491 /* Starting from a PHI node for the virtual operand of the memory reference
2492 REF find a continuation virtual operand that allows to continue walking
2493 statements dominating PHI skipping only statements that cannot possibly
2494 clobber REF. Increments *CNT for each alias disambiguation done.
2495 Returns NULL_TREE if no suitable virtual operand can be found. */
2497 tree
2498 get_continuation_for_phi (gimple phi, ao_ref *ref,
2499 unsigned int *cnt, bitmap *visited,
2500 bool abort_on_visited,
2501 void *(*translate)(ao_ref *, tree, void *, bool),
2502 void *data)
2504 unsigned nargs = gimple_phi_num_args (phi);
2506 /* Through a single-argument PHI we can simply look through. */
2507 if (nargs == 1)
2508 return PHI_ARG_DEF (phi, 0);
2510 /* For two or more arguments try to pairwise skip non-aliasing code
2511 until we hit the phi argument definition that dominates the other one. */
2512 else if (nargs >= 2)
2514 tree arg0, arg1;
2515 unsigned i;
2517 /* Find a candidate for the virtual operand which definition
2518 dominates those of all others. */
2519 arg0 = PHI_ARG_DEF (phi, 0);
2520 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2521 for (i = 1; i < nargs; ++i)
2523 arg1 = PHI_ARG_DEF (phi, i);
2524 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2526 arg0 = arg1;
2527 break;
2529 if (dominated_by_p (CDI_DOMINATORS,
2530 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2531 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2532 arg0 = arg1;
2535 /* Then pairwise reduce against the found candidate. */
2536 for (i = 0; i < nargs; ++i)
2538 arg1 = PHI_ARG_DEF (phi, i);
2539 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2540 cnt, visited, abort_on_visited,
2541 translate, data);
2542 if (!arg0)
2543 return NULL_TREE;
2546 return arg0;
2549 return NULL_TREE;
2552 /* Based on the memory reference REF and its virtual use VUSE call
2553 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2554 itself. That is, for each virtual use for which its defining statement
2555 does not clobber REF.
2557 WALKER is called with REF, the current virtual use and DATA. If
2558 WALKER returns non-NULL the walk stops and its result is returned.
2559 At the end of a non-successful walk NULL is returned.
2561 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2562 use which definition is a statement that may clobber REF and DATA.
2563 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2564 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2565 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2566 to adjust REF and *DATA to make that valid.
2568 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2570 void *
2571 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2572 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2573 void *(*translate)(ao_ref *, tree, void *, bool),
2574 void *data)
2576 bitmap visited = NULL;
2577 void *res;
2578 unsigned int cnt = 0;
2579 bool translated = false;
2581 timevar_push (TV_ALIAS_STMT_WALK);
2585 gimple def_stmt;
2587 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2588 res = (*walker) (ref, vuse, cnt, data);
2589 /* Abort walk. */
2590 if (res == (void *)-1)
2592 res = NULL;
2593 break;
2595 /* Lookup succeeded. */
2596 else if (res != NULL)
2597 break;
2599 def_stmt = SSA_NAME_DEF_STMT (vuse);
2600 if (gimple_nop_p (def_stmt))
2601 break;
2602 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2603 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2604 &visited, translated, translate, data);
2605 else
2607 cnt++;
2608 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2610 if (!translate)
2611 break;
2612 res = (*translate) (ref, vuse, data, false);
2613 /* Failed lookup and translation. */
2614 if (res == (void *)-1)
2616 res = NULL;
2617 break;
2619 /* Lookup succeeded. */
2620 else if (res != NULL)
2621 break;
2622 /* Translation succeeded, continue walking. */
2623 translated = true;
2625 vuse = gimple_vuse (def_stmt);
2628 while (vuse);
2630 if (visited)
2631 BITMAP_FREE (visited);
2633 timevar_pop (TV_ALIAS_STMT_WALK);
2635 return res;
2639 /* Based on the memory reference REF call WALKER for each vdef which
2640 defining statement may clobber REF, starting with VDEF. If REF
2641 is NULL_TREE, each defining statement is visited.
2643 WALKER is called with REF, the current vdef and DATA. If WALKER
2644 returns true the walk is stopped, otherwise it continues.
2646 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2647 The pointer may be NULL and then we do not track this information.
2649 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2650 PHI argument (but only one walk continues on merge points), the
2651 return value is true if any of the walks was successful.
2653 The function returns the number of statements walked. */
2655 static unsigned int
2656 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2657 bool (*walker)(ao_ref *, tree, void *), void *data,
2658 bitmap *visited, unsigned int cnt,
2659 bool *function_entry_reached)
2661 if (function_entry_reached)
2662 *function_entry_reached = false;
2665 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2667 if (*visited
2668 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2669 return cnt;
2671 if (gimple_nop_p (def_stmt))
2673 if (function_entry_reached)
2674 *function_entry_reached = true;
2675 return cnt;
2677 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2679 unsigned i;
2680 if (!*visited)
2681 *visited = BITMAP_ALLOC (NULL);
2682 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2683 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2684 walker, data, visited, 0,
2685 function_entry_reached);
2686 return cnt;
2689 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2690 cnt++;
2691 if ((!ref
2692 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2693 && (*walker) (ref, vdef, data))
2694 return cnt;
2696 vdef = gimple_vuse (def_stmt);
2698 while (1);
2701 unsigned int
2702 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2703 bool (*walker)(ao_ref *, tree, void *), void *data,
2704 bitmap *visited,
2705 bool *function_entry_reached)
2707 bitmap local_visited = NULL;
2708 unsigned int ret;
2710 timevar_push (TV_ALIAS_STMT_WALK);
2712 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2713 visited ? visited : &local_visited, 0,
2714 function_entry_reached);
2715 if (local_visited)
2716 BITMAP_FREE (local_visited);
2718 timevar_pop (TV_ALIAS_STMT_WALK);
2720 return ret;