* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / tree-ssa-alias.c
blob442112adb63b5141dc96bc881a6b5b85b2ac2aa1
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 /* Returns whether reference REF to BASE may refer to global memory. */
335 static bool
336 ref_may_alias_global_p_1 (tree base)
338 if (DECL_P (base))
339 return is_global_var (base);
340 else if (TREE_CODE (base) == MEM_REF
341 || TREE_CODE (base) == TARGET_MEM_REF)
342 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
343 return true;
346 bool
347 ref_may_alias_global_p (ao_ref *ref)
349 tree base = ao_ref_base (ref);
350 return ref_may_alias_global_p_1 (base);
353 bool
354 ref_may_alias_global_p (tree ref)
356 tree base = get_base_address (ref);
357 return ref_may_alias_global_p_1 (base);
360 /* Return true whether STMT may clobber global memory. */
362 bool
363 stmt_may_clobber_global_p (gimple stmt)
365 tree lhs;
367 if (!gimple_vdef (stmt))
368 return false;
370 /* ??? We can ask the oracle whether an artificial pointer
371 dereference with a pointer with points-to information covering
372 all global memory (what about non-address taken memory?) maybe
373 clobbered by this call. As there is at the moment no convenient
374 way of doing that without generating garbage do some manual
375 checking instead.
376 ??? We could make a NULL ao_ref argument to the various
377 predicates special, meaning any global memory. */
379 switch (gimple_code (stmt))
381 case GIMPLE_ASSIGN:
382 lhs = gimple_assign_lhs (stmt);
383 return (TREE_CODE (lhs) != SSA_NAME
384 && ref_may_alias_global_p (lhs));
385 case GIMPLE_CALL:
386 return true;
387 default:
388 return true;
393 /* Dump alias information on FILE. */
395 void
396 dump_alias_info (FILE *file)
398 unsigned i;
399 const char *funcname
400 = lang_hooks.decl_printable_name (current_function_decl, 2);
401 tree var;
403 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
405 fprintf (file, "Aliased symbols\n\n");
407 FOR_EACH_LOCAL_DECL (cfun, i, var)
409 if (may_be_aliased (var))
410 dump_variable (file, var);
413 fprintf (file, "\nCall clobber information\n");
415 fprintf (file, "\nESCAPED");
416 dump_points_to_solution (file, &cfun->gimple_df->escaped);
418 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
420 for (i = 1; i < num_ssa_names; i++)
422 tree ptr = ssa_name (i);
423 struct ptr_info_def *pi;
425 if (ptr == NULL_TREE
426 || !POINTER_TYPE_P (TREE_TYPE (ptr))
427 || SSA_NAME_IN_FREE_LIST (ptr))
428 continue;
430 pi = SSA_NAME_PTR_INFO (ptr);
431 if (pi)
432 dump_points_to_info_for (file, ptr);
435 fprintf (file, "\n");
439 /* Dump alias information on stderr. */
441 DEBUG_FUNCTION void
442 debug_alias_info (void)
444 dump_alias_info (stderr);
448 /* Dump the points-to set *PT into FILE. */
450 void
451 dump_points_to_solution (FILE *file, struct pt_solution *pt)
453 if (pt->anything)
454 fprintf (file, ", points-to anything");
456 if (pt->nonlocal)
457 fprintf (file, ", points-to non-local");
459 if (pt->escaped)
460 fprintf (file, ", points-to escaped");
462 if (pt->ipa_escaped)
463 fprintf (file, ", points-to unit escaped");
465 if (pt->null)
466 fprintf (file, ", points-to NULL");
468 if (pt->vars)
470 fprintf (file, ", points-to vars: ");
471 dump_decl_set (file, pt->vars);
472 if (pt->vars_contains_nonlocal
473 && pt->vars_contains_escaped_heap)
474 fprintf (file, " (nonlocal, escaped heap)");
475 else if (pt->vars_contains_nonlocal
476 && pt->vars_contains_escaped)
477 fprintf (file, " (nonlocal, escaped)");
478 else if (pt->vars_contains_nonlocal)
479 fprintf (file, " (nonlocal)");
480 else if (pt->vars_contains_escaped_heap)
481 fprintf (file, " (escaped heap)");
482 else if (pt->vars_contains_escaped)
483 fprintf (file, " (escaped)");
488 /* Unified dump function for pt_solution. */
490 DEBUG_FUNCTION void
491 debug (pt_solution &ref)
493 dump_points_to_solution (stderr, &ref);
496 DEBUG_FUNCTION void
497 debug (pt_solution *ptr)
499 if (ptr)
500 debug (*ptr);
501 else
502 fprintf (stderr, "<nil>\n");
506 /* Dump points-to information for SSA_NAME PTR into FILE. */
508 void
509 dump_points_to_info_for (FILE *file, tree ptr)
511 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
513 print_generic_expr (file, ptr, dump_flags);
515 if (pi)
516 dump_points_to_solution (file, &pi->pt);
517 else
518 fprintf (file, ", points-to anything");
520 fprintf (file, "\n");
524 /* Dump points-to information for VAR into stderr. */
526 DEBUG_FUNCTION void
527 debug_points_to_info_for (tree var)
529 dump_points_to_info_for (stderr, var);
533 /* Initializes the alias-oracle reference representation *R from REF. */
535 void
536 ao_ref_init (ao_ref *r, tree ref)
538 r->ref = ref;
539 r->base = NULL_TREE;
540 r->offset = 0;
541 r->size = -1;
542 r->max_size = -1;
543 r->ref_alias_set = -1;
544 r->base_alias_set = -1;
545 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
548 /* Returns the base object of the memory reference *REF. */
550 tree
551 ao_ref_base (ao_ref *ref)
553 if (ref->base)
554 return ref->base;
555 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
556 &ref->max_size);
557 return ref->base;
560 /* Returns the base object alias set of the memory reference *REF. */
562 static alias_set_type
563 ao_ref_base_alias_set (ao_ref *ref)
565 tree base_ref;
566 if (ref->base_alias_set != -1)
567 return ref->base_alias_set;
568 if (!ref->ref)
569 return 0;
570 base_ref = ref->ref;
571 while (handled_component_p (base_ref))
572 base_ref = TREE_OPERAND (base_ref, 0);
573 ref->base_alias_set = get_alias_set (base_ref);
574 return ref->base_alias_set;
577 /* Returns the reference alias set of the memory reference *REF. */
579 alias_set_type
580 ao_ref_alias_set (ao_ref *ref)
582 if (ref->ref_alias_set != -1)
583 return ref->ref_alias_set;
584 ref->ref_alias_set = get_alias_set (ref->ref);
585 return ref->ref_alias_set;
588 /* Init an alias-oracle reference representation from a gimple pointer
589 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
590 size is assumed to be unknown. The access is assumed to be only
591 to or after of the pointer target, not before it. */
593 void
594 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
596 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
597 ref->ref = NULL_TREE;
598 if (TREE_CODE (ptr) == SSA_NAME)
600 gimple stmt = SSA_NAME_DEF_STMT (ptr);
601 if (gimple_assign_single_p (stmt)
602 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
603 ptr = gimple_assign_rhs1 (stmt);
604 else if (is_gimple_assign (stmt)
605 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
606 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
608 ptr = gimple_assign_rhs1 (stmt);
609 extra_offset = BITS_PER_UNIT
610 * int_cst_value (gimple_assign_rhs2 (stmt));
614 if (TREE_CODE (ptr) == ADDR_EXPR)
616 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
617 if (ref->base)
618 ref->offset = BITS_PER_UNIT * t;
619 else
621 size = NULL_TREE;
622 ref->offset = 0;
623 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
626 else
628 ref->base = build2 (MEM_REF, char_type_node,
629 ptr, null_pointer_node);
630 ref->offset = 0;
632 ref->offset += extra_offset;
633 if (size
634 && tree_fits_shwi_p (size)
635 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
636 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
637 else
638 ref->max_size = ref->size = -1;
639 ref->ref_alias_set = 0;
640 ref->base_alias_set = 0;
641 ref->volatile_p = false;
644 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
645 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
646 decide. */
648 static inline int
649 same_type_for_tbaa (tree type1, tree type2)
651 type1 = TYPE_MAIN_VARIANT (type1);
652 type2 = TYPE_MAIN_VARIANT (type2);
654 /* If we would have to do structural comparison bail out. */
655 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
656 || TYPE_STRUCTURAL_EQUALITY_P (type2))
657 return -1;
659 /* Compare the canonical types. */
660 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
661 return 1;
663 /* ??? Array types are not properly unified in all cases as we have
664 spurious changes in the index types for example. Removing this
665 causes all sorts of problems with the Fortran frontend. */
666 if (TREE_CODE (type1) == ARRAY_TYPE
667 && TREE_CODE (type2) == ARRAY_TYPE)
668 return -1;
670 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
671 object of one of its constrained subtypes, e.g. when a function with an
672 unconstrained parameter passed by reference is called on an object and
673 inlined. But, even in the case of a fixed size, type and subtypes are
674 not equivalent enough as to share the same TYPE_CANONICAL, since this
675 would mean that conversions between them are useless, whereas they are
676 not (e.g. type and subtypes can have different modes). So, in the end,
677 they are only guaranteed to have the same alias set. */
678 if (get_alias_set (type1) == get_alias_set (type2))
679 return -1;
681 /* The types are known to be not equal. */
682 return 0;
685 /* Determine if the two component references REF1 and REF2 which are
686 based on access types TYPE1 and TYPE2 and of which at least one is based
687 on an indirect reference may alias. REF2 is the only one that can
688 be a decl in which case REF2_IS_DECL is true.
689 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
690 are the respective alias sets. */
692 static bool
693 aliasing_component_refs_p (tree ref1,
694 alias_set_type ref1_alias_set,
695 alias_set_type base1_alias_set,
696 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
697 tree ref2,
698 alias_set_type ref2_alias_set,
699 alias_set_type base2_alias_set,
700 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
701 bool ref2_is_decl)
703 /* If one reference is a component references through pointers try to find a
704 common base and apply offset based disambiguation. This handles
705 for example
706 struct A { int i; int j; } *q;
707 struct B { struct A a; int k; } *p;
708 disambiguating q->i and p->a.j. */
709 tree base1, base2;
710 tree type1, type2;
711 tree *refp;
712 int same_p;
714 /* Choose bases and base types to search for. */
715 base1 = ref1;
716 while (handled_component_p (base1))
717 base1 = TREE_OPERAND (base1, 0);
718 type1 = TREE_TYPE (base1);
719 base2 = ref2;
720 while (handled_component_p (base2))
721 base2 = TREE_OPERAND (base2, 0);
722 type2 = TREE_TYPE (base2);
724 /* Now search for the type1 in the access path of ref2. This
725 would be a common base for doing offset based disambiguation on. */
726 refp = &ref2;
727 while (handled_component_p (*refp)
728 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
729 refp = &TREE_OPERAND (*refp, 0);
730 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
731 /* If we couldn't compare types we have to bail out. */
732 if (same_p == -1)
733 return true;
734 else if (same_p == 1)
736 HOST_WIDE_INT offadj, sztmp, msztmp;
737 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
738 offset2 -= offadj;
739 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
740 offset1 -= offadj;
741 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
743 /* If we didn't find a common base, try the other way around. */
744 refp = &ref1;
745 while (handled_component_p (*refp)
746 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
747 refp = &TREE_OPERAND (*refp, 0);
748 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
749 /* If we couldn't compare types we have to bail out. */
750 if (same_p == -1)
751 return true;
752 else if (same_p == 1)
754 HOST_WIDE_INT offadj, sztmp, msztmp;
755 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
756 offset1 -= offadj;
757 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
758 offset2 -= offadj;
759 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
762 /* If we have two type access paths B1.path1 and B2.path2 they may
763 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
764 But we can still have a path that goes B1.path1...B2.path2 with
765 a part that we do not see. So we can only disambiguate now
766 if there is no B2 in the tail of path1 and no B1 on the
767 tail of path2. */
768 if (base1_alias_set == ref2_alias_set
769 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
770 return true;
771 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
772 if (!ref2_is_decl)
773 return (base2_alias_set == ref1_alias_set
774 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
775 return false;
778 /* Return true if we can determine that component references REF1 and REF2,
779 that are within a common DECL, cannot overlap. */
781 static bool
782 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
784 auto_vec<tree, 16> component_refs1;
785 auto_vec<tree, 16> component_refs2;
787 /* Create the stack of handled components for REF1. */
788 while (handled_component_p (ref1))
790 component_refs1.safe_push (ref1);
791 ref1 = TREE_OPERAND (ref1, 0);
793 if (TREE_CODE (ref1) == MEM_REF)
795 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
796 goto may_overlap;
797 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
800 /* Create the stack of handled components for REF2. */
801 while (handled_component_p (ref2))
803 component_refs2.safe_push (ref2);
804 ref2 = TREE_OPERAND (ref2, 0);
806 if (TREE_CODE (ref2) == MEM_REF)
808 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
809 goto may_overlap;
810 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
813 /* We must have the same base DECL. */
814 gcc_assert (ref1 == ref2);
816 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
817 rank. This is sufficient because we start from the same DECL and you
818 cannot reference several fields at a time with COMPONENT_REFs (unlike
819 with ARRAY_RANGE_REFs for arrays) so you always need the same number
820 of them to access a sub-component, unless you're in a union, in which
821 case the return value will precisely be false. */
822 while (true)
826 if (component_refs1.is_empty ())
827 goto may_overlap;
828 ref1 = component_refs1.pop ();
830 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
834 if (component_refs2.is_empty ())
835 goto may_overlap;
836 ref2 = component_refs2.pop ();
838 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
840 /* Beware of BIT_FIELD_REF. */
841 if (TREE_CODE (ref1) != COMPONENT_REF
842 || TREE_CODE (ref2) != COMPONENT_REF)
843 goto may_overlap;
845 tree field1 = TREE_OPERAND (ref1, 1);
846 tree field2 = TREE_OPERAND (ref2, 1);
848 /* ??? We cannot simply use the type of operand #0 of the refs here
849 as the Fortran compiler smuggles type punning into COMPONENT_REFs
850 for common blocks instead of using unions like everyone else. */
851 tree type1 = DECL_CONTEXT (field1);
852 tree type2 = DECL_CONTEXT (field2);
854 /* We cannot disambiguate fields in a union or qualified union. */
855 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
856 goto may_overlap;
858 /* Different fields of the same record type cannot overlap.
859 ??? Bitfields can overlap at RTL level so punt on them. */
860 if (field1 != field2)
862 component_refs1.release ();
863 component_refs2.release ();
864 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
868 may_overlap:
869 component_refs1.release ();
870 component_refs2.release ();
871 return false;
874 /* qsort compare function to sort FIELD_DECLs after their
875 DECL_FIELD_CONTEXT TYPE_UID. */
877 static inline int
878 ncr_compar (const void *field1_, const void *field2_)
880 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
881 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
882 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
883 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
884 if (uid1 < uid2)
885 return -1;
886 else if (uid1 > uid2)
887 return 1;
888 return 0;
891 /* Return true if we can determine that the fields referenced cannot
892 overlap for any pair of objects. */
894 static bool
895 nonoverlapping_component_refs_p (const_tree x, const_tree y)
897 if (!flag_strict_aliasing
898 || !x || !y
899 || TREE_CODE (x) != COMPONENT_REF
900 || TREE_CODE (y) != COMPONENT_REF)
901 return false;
903 auto_vec<const_tree, 16> fieldsx;
904 while (TREE_CODE (x) == COMPONENT_REF)
906 tree field = TREE_OPERAND (x, 1);
907 tree type = DECL_FIELD_CONTEXT (field);
908 if (TREE_CODE (type) == RECORD_TYPE)
909 fieldsx.safe_push (field);
910 x = TREE_OPERAND (x, 0);
912 if (fieldsx.length () == 0)
913 return false;
914 auto_vec<const_tree, 16> fieldsy;
915 while (TREE_CODE (y) == COMPONENT_REF)
917 tree field = TREE_OPERAND (y, 1);
918 tree type = DECL_FIELD_CONTEXT (field);
919 if (TREE_CODE (type) == RECORD_TYPE)
920 fieldsy.safe_push (TREE_OPERAND (y, 1));
921 y = TREE_OPERAND (y, 0);
923 if (fieldsy.length () == 0)
924 return false;
926 /* Most common case first. */
927 if (fieldsx.length () == 1
928 && fieldsy.length () == 1)
929 return ((DECL_FIELD_CONTEXT (fieldsx[0])
930 == DECL_FIELD_CONTEXT (fieldsy[0]))
931 && fieldsx[0] != fieldsy[0]
932 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
934 if (fieldsx.length () == 2)
936 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
938 const_tree tem = fieldsx[0];
939 fieldsx[0] = fieldsx[1];
940 fieldsx[1] = tem;
943 else
944 fieldsx.qsort (ncr_compar);
946 if (fieldsy.length () == 2)
948 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
950 const_tree tem = fieldsy[0];
951 fieldsy[0] = fieldsy[1];
952 fieldsy[1] = tem;
955 else
956 fieldsy.qsort (ncr_compar);
958 unsigned i = 0, j = 0;
961 const_tree fieldx = fieldsx[i];
962 const_tree fieldy = fieldsy[j];
963 tree typex = DECL_FIELD_CONTEXT (fieldx);
964 tree typey = DECL_FIELD_CONTEXT (fieldy);
965 if (typex == typey)
967 /* We're left with accessing different fields of a structure,
968 no possible overlap, unless they are both bitfields. */
969 if (fieldx != fieldy)
970 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
972 if (TYPE_UID (typex) < TYPE_UID (typey))
974 i++;
975 if (i == fieldsx.length ())
976 break;
978 else
980 j++;
981 if (j == fieldsy.length ())
982 break;
985 while (1);
987 return false;
991 /* Return true if two memory references based on the variables BASE1
992 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
993 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
994 if non-NULL are the complete memory reference trees. */
996 static bool
997 decl_refs_may_alias_p (tree ref1, tree base1,
998 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
999 tree ref2, tree base2,
1000 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1002 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1004 /* If both references are based on different variables, they cannot alias. */
1005 if (base1 != base2)
1006 return false;
1008 /* If both references are based on the same variable, they cannot alias if
1009 the accesses do not overlap. */
1010 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1011 return false;
1013 /* For components with variable position, the above test isn't sufficient,
1014 so we disambiguate component references manually. */
1015 if (ref1 && ref2
1016 && handled_component_p (ref1) && handled_component_p (ref2)
1017 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1018 return false;
1020 return true;
1023 /* Return true if an indirect reference based on *PTR1 constrained
1024 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1025 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1026 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1027 in which case they are computed on-demand. REF1 and REF2
1028 if non-NULL are the complete memory reference trees. */
1030 static bool
1031 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1032 HOST_WIDE_INT offset1,
1033 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1034 alias_set_type ref1_alias_set,
1035 alias_set_type base1_alias_set,
1036 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1037 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1038 alias_set_type ref2_alias_set,
1039 alias_set_type base2_alias_set, bool tbaa_p)
1041 tree ptr1;
1042 tree ptrtype1, dbase2;
1043 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1044 HOST_WIDE_INT doffset1, doffset2;
1046 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1047 || TREE_CODE (base1) == TARGET_MEM_REF)
1048 && DECL_P (base2));
1050 ptr1 = TREE_OPERAND (base1, 0);
1052 /* The offset embedded in MEM_REFs can be negative. Bias them
1053 so that the resulting offset adjustment is positive. */
1054 offset_int moff = mem_ref_offset (base1);
1055 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1056 if (wi::neg_p (moff))
1057 offset2p += (-moff).to_short_addr ();
1058 else
1059 offset1p += moff.to_short_addr ();
1061 /* If only one reference is based on a variable, they cannot alias if
1062 the pointer access is beyond the extent of the variable access.
1063 (the pointer base cannot validly point to an offset less than zero
1064 of the variable).
1065 ??? IVOPTs creates bases that do not honor this restriction,
1066 so do not apply this optimization for TARGET_MEM_REFs. */
1067 if (TREE_CODE (base1) != TARGET_MEM_REF
1068 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1069 return false;
1070 /* They also cannot alias if the pointer may not point to the decl. */
1071 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1072 return false;
1074 /* Disambiguations that rely on strict aliasing rules follow. */
1075 if (!flag_strict_aliasing || !tbaa_p)
1076 return true;
1078 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1080 /* If the alias set for a pointer access is zero all bets are off. */
1081 if (base1_alias_set == -1)
1082 base1_alias_set = get_deref_alias_set (ptrtype1);
1083 if (base1_alias_set == 0)
1084 return true;
1085 if (base2_alias_set == -1)
1086 base2_alias_set = get_alias_set (base2);
1088 /* When we are trying to disambiguate an access with a pointer dereference
1089 as base versus one with a decl as base we can use both the size
1090 of the decl and its dynamic type for extra disambiguation.
1091 ??? We do not know anything about the dynamic type of the decl
1092 other than that its alias-set contains base2_alias_set as a subset
1093 which does not help us here. */
1094 /* As we know nothing useful about the dynamic type of the decl just
1095 use the usual conflict check rather than a subset test.
1096 ??? We could introduce -fvery-strict-aliasing when the language
1097 does not allow decls to have a dynamic type that differs from their
1098 static type. Then we can check
1099 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1100 if (base1_alias_set != base2_alias_set
1101 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1102 return false;
1103 /* If the size of the access relevant for TBAA through the pointer
1104 is bigger than the size of the decl we can't possibly access the
1105 decl via that pointer. */
1106 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1107 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1108 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1109 /* ??? This in turn may run afoul when a decl of type T which is
1110 a member of union type U is accessed through a pointer to
1111 type U and sizeof T is smaller than sizeof U. */
1112 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1113 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1114 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1115 return false;
1117 if (!ref2)
1118 return true;
1120 /* If the decl is accessed via a MEM_REF, reconstruct the base
1121 we can use for TBAA and an appropriately adjusted offset. */
1122 dbase2 = ref2;
1123 while (handled_component_p (dbase2))
1124 dbase2 = TREE_OPERAND (dbase2, 0);
1125 doffset1 = offset1;
1126 doffset2 = offset2;
1127 if (TREE_CODE (dbase2) == MEM_REF
1128 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1130 offset_int moff = mem_ref_offset (dbase2);
1131 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1132 if (wi::neg_p (moff))
1133 doffset1 -= (-moff).to_short_addr ();
1134 else
1135 doffset2 -= moff.to_short_addr ();
1138 /* If either reference is view-converted, give up now. */
1139 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1140 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1141 return true;
1143 /* If both references are through the same type, they do not alias
1144 if the accesses do not overlap. This does extra disambiguation
1145 for mixed/pointer accesses but requires strict aliasing.
1146 For MEM_REFs we require that the component-ref offset we computed
1147 is relative to the start of the type which we ensure by
1148 comparing rvalue and access type and disregarding the constant
1149 pointer offset. */
1150 if ((TREE_CODE (base1) != TARGET_MEM_REF
1151 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1152 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1153 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1155 if (ref1 && ref2
1156 && nonoverlapping_component_refs_p (ref1, ref2))
1157 return false;
1159 /* Do access-path based disambiguation. */
1160 if (ref1 && ref2
1161 && (handled_component_p (ref1) || handled_component_p (ref2)))
1162 return aliasing_component_refs_p (ref1,
1163 ref1_alias_set, base1_alias_set,
1164 offset1, max_size1,
1165 ref2,
1166 ref2_alias_set, base2_alias_set,
1167 offset2, max_size2, true);
1169 return true;
1172 /* Return true if two indirect references based on *PTR1
1173 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1174 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1175 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1176 in which case they are computed on-demand. REF1 and REF2
1177 if non-NULL are the complete memory reference trees. */
1179 static bool
1180 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1181 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1182 alias_set_type ref1_alias_set,
1183 alias_set_type base1_alias_set,
1184 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1185 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1186 alias_set_type ref2_alias_set,
1187 alias_set_type base2_alias_set, bool tbaa_p)
1189 tree ptr1;
1190 tree ptr2;
1191 tree ptrtype1, ptrtype2;
1193 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1194 || TREE_CODE (base1) == TARGET_MEM_REF)
1195 && (TREE_CODE (base2) == MEM_REF
1196 || TREE_CODE (base2) == TARGET_MEM_REF));
1198 ptr1 = TREE_OPERAND (base1, 0);
1199 ptr2 = TREE_OPERAND (base2, 0);
1201 /* If both bases are based on pointers they cannot alias if they may not
1202 point to the same memory object or if they point to the same object
1203 and the accesses do not overlap. */
1204 if ((!cfun || gimple_in_ssa_p (cfun))
1205 && operand_equal_p (ptr1, ptr2, 0)
1206 && (((TREE_CODE (base1) != TARGET_MEM_REF
1207 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1208 && (TREE_CODE (base2) != TARGET_MEM_REF
1209 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1210 || (TREE_CODE (base1) == TARGET_MEM_REF
1211 && TREE_CODE (base2) == TARGET_MEM_REF
1212 && (TMR_STEP (base1) == TMR_STEP (base2)
1213 || (TMR_STEP (base1) && TMR_STEP (base2)
1214 && operand_equal_p (TMR_STEP (base1),
1215 TMR_STEP (base2), 0)))
1216 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1217 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1218 && operand_equal_p (TMR_INDEX (base1),
1219 TMR_INDEX (base2), 0)))
1220 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1221 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1222 && operand_equal_p (TMR_INDEX2 (base1),
1223 TMR_INDEX2 (base2), 0))))))
1225 offset_int moff;
1226 /* The offset embedded in MEM_REFs can be negative. Bias them
1227 so that the resulting offset adjustment is positive. */
1228 moff = mem_ref_offset (base1);
1229 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1230 if (wi::neg_p (moff))
1231 offset2 += (-moff).to_short_addr ();
1232 else
1233 offset1 += moff.to_shwi ();
1234 moff = mem_ref_offset (base2);
1235 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1236 if (wi::neg_p (moff))
1237 offset1 += (-moff).to_short_addr ();
1238 else
1239 offset2 += moff.to_short_addr ();
1240 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1242 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1243 return false;
1245 /* Disambiguations that rely on strict aliasing rules follow. */
1246 if (!flag_strict_aliasing || !tbaa_p)
1247 return true;
1249 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1250 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1252 /* If the alias set for a pointer access is zero all bets are off. */
1253 if (base1_alias_set == -1)
1254 base1_alias_set = get_deref_alias_set (ptrtype1);
1255 if (base1_alias_set == 0)
1256 return true;
1257 if (base2_alias_set == -1)
1258 base2_alias_set = get_deref_alias_set (ptrtype2);
1259 if (base2_alias_set == 0)
1260 return true;
1262 /* If both references are through the same type, they do not alias
1263 if the accesses do not overlap. This does extra disambiguation
1264 for mixed/pointer accesses but requires strict aliasing. */
1265 if ((TREE_CODE (base1) != TARGET_MEM_REF
1266 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1267 && (TREE_CODE (base2) != TARGET_MEM_REF
1268 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1269 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1270 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1271 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1272 TREE_TYPE (ptrtype2)) == 1)
1273 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1275 /* Do type-based disambiguation. */
1276 if (base1_alias_set != base2_alias_set
1277 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1278 return false;
1280 /* If either reference is view-converted, give up now. */
1281 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1282 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1283 return true;
1285 if (ref1 && ref2
1286 && nonoverlapping_component_refs_p (ref1, ref2))
1287 return false;
1289 /* Do access-path based disambiguation. */
1290 if (ref1 && ref2
1291 && (handled_component_p (ref1) || handled_component_p (ref2)))
1292 return aliasing_component_refs_p (ref1,
1293 ref1_alias_set, base1_alias_set,
1294 offset1, max_size1,
1295 ref2,
1296 ref2_alias_set, base2_alias_set,
1297 offset2, max_size2, false);
1299 return true;
1302 /* Return true, if the two memory references REF1 and REF2 may alias. */
1304 bool
1305 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1307 tree base1, base2;
1308 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1309 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1310 bool var1_p, var2_p, ind1_p, ind2_p;
1312 gcc_checking_assert ((!ref1->ref
1313 || TREE_CODE (ref1->ref) == SSA_NAME
1314 || DECL_P (ref1->ref)
1315 || TREE_CODE (ref1->ref) == STRING_CST
1316 || handled_component_p (ref1->ref)
1317 || TREE_CODE (ref1->ref) == MEM_REF
1318 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1319 && (!ref2->ref
1320 || TREE_CODE (ref2->ref) == SSA_NAME
1321 || DECL_P (ref2->ref)
1322 || TREE_CODE (ref2->ref) == STRING_CST
1323 || handled_component_p (ref2->ref)
1324 || TREE_CODE (ref2->ref) == MEM_REF
1325 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1327 /* Decompose the references into their base objects and the access. */
1328 base1 = ao_ref_base (ref1);
1329 offset1 = ref1->offset;
1330 max_size1 = ref1->max_size;
1331 base2 = ao_ref_base (ref2);
1332 offset2 = ref2->offset;
1333 max_size2 = ref2->max_size;
1335 /* We can end up with registers or constants as bases for example from
1336 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1337 which is seen as a struct copy. */
1338 if (TREE_CODE (base1) == SSA_NAME
1339 || TREE_CODE (base1) == CONST_DECL
1340 || TREE_CODE (base1) == CONSTRUCTOR
1341 || TREE_CODE (base1) == ADDR_EXPR
1342 || CONSTANT_CLASS_P (base1)
1343 || TREE_CODE (base2) == SSA_NAME
1344 || TREE_CODE (base2) == CONST_DECL
1345 || TREE_CODE (base2) == CONSTRUCTOR
1346 || TREE_CODE (base2) == ADDR_EXPR
1347 || CONSTANT_CLASS_P (base2))
1348 return false;
1350 /* We can end up referring to code via function and label decls.
1351 As we likely do not properly track code aliases conservatively
1352 bail out. */
1353 if (TREE_CODE (base1) == FUNCTION_DECL
1354 || TREE_CODE (base1) == LABEL_DECL
1355 || TREE_CODE (base2) == FUNCTION_DECL
1356 || TREE_CODE (base2) == LABEL_DECL)
1357 return true;
1359 /* Two volatile accesses always conflict. */
1360 if (ref1->volatile_p
1361 && ref2->volatile_p)
1362 return true;
1364 /* Defer to simple offset based disambiguation if we have
1365 references based on two decls. Do this before defering to
1366 TBAA to handle must-alias cases in conformance with the
1367 GCC extension of allowing type-punning through unions. */
1368 var1_p = DECL_P (base1);
1369 var2_p = DECL_P (base2);
1370 if (var1_p && var2_p)
1371 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1372 ref2->ref, base2, offset2, max_size2);
1374 ind1_p = (TREE_CODE (base1) == MEM_REF
1375 || TREE_CODE (base1) == TARGET_MEM_REF);
1376 ind2_p = (TREE_CODE (base2) == MEM_REF
1377 || TREE_CODE (base2) == TARGET_MEM_REF);
1379 /* Canonicalize the pointer-vs-decl case. */
1380 if (ind1_p && var2_p)
1382 HOST_WIDE_INT tmp1;
1383 tree tmp2;
1384 ao_ref *tmp3;
1385 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1386 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1387 tmp2 = base1; base1 = base2; base2 = tmp2;
1388 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1389 var1_p = true;
1390 ind1_p = false;
1391 var2_p = false;
1392 ind2_p = true;
1395 /* First defer to TBAA if possible. */
1396 if (tbaa_p
1397 && flag_strict_aliasing
1398 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1399 ao_ref_alias_set (ref2)))
1400 return false;
1402 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1403 if (var1_p && ind2_p)
1404 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1405 offset2, max_size2,
1406 ao_ref_alias_set (ref2), -1,
1407 ref1->ref, base1,
1408 offset1, max_size1,
1409 ao_ref_alias_set (ref1),
1410 ao_ref_base_alias_set (ref1),
1411 tbaa_p);
1412 else if (ind1_p && ind2_p)
1413 return indirect_refs_may_alias_p (ref1->ref, base1,
1414 offset1, max_size1,
1415 ao_ref_alias_set (ref1), -1,
1416 ref2->ref, base2,
1417 offset2, max_size2,
1418 ao_ref_alias_set (ref2), -1,
1419 tbaa_p);
1421 /* We really do not want to end up here, but returning true is safe. */
1422 #ifdef ENABLE_CHECKING
1423 gcc_unreachable ();
1424 #else
1425 return true;
1426 #endif
1429 static bool
1430 refs_may_alias_p (tree ref1, ao_ref *ref2)
1432 ao_ref r1;
1433 ao_ref_init (&r1, ref1);
1434 return refs_may_alias_p_1 (&r1, ref2, true);
1437 bool
1438 refs_may_alias_p (tree ref1, tree ref2)
1440 ao_ref r1, r2;
1441 bool res;
1442 ao_ref_init (&r1, ref1);
1443 ao_ref_init (&r2, ref2);
1444 res = refs_may_alias_p_1 (&r1, &r2, true);
1445 if (res)
1446 ++alias_stats.refs_may_alias_p_may_alias;
1447 else
1448 ++alias_stats.refs_may_alias_p_no_alias;
1449 return res;
1452 /* Returns true if there is a anti-dependence for the STORE that
1453 executes after the LOAD. */
1455 bool
1456 refs_anti_dependent_p (tree load, tree store)
1458 ao_ref r1, r2;
1459 ao_ref_init (&r1, load);
1460 ao_ref_init (&r2, store);
1461 return refs_may_alias_p_1 (&r1, &r2, false);
1464 /* Returns true if there is a output dependence for the stores
1465 STORE1 and STORE2. */
1467 bool
1468 refs_output_dependent_p (tree store1, tree store2)
1470 ao_ref r1, r2;
1471 ao_ref_init (&r1, store1);
1472 ao_ref_init (&r2, store2);
1473 return refs_may_alias_p_1 (&r1, &r2, false);
1476 /* If the call CALL may use the memory reference REF return true,
1477 otherwise return false. */
1479 static bool
1480 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1482 tree base, callee;
1483 unsigned i;
1484 int flags = gimple_call_flags (call);
1486 /* Const functions without a static chain do not implicitly use memory. */
1487 if (!gimple_call_chain (call)
1488 && (flags & (ECF_CONST|ECF_NOVOPS)))
1489 goto process_args;
1491 base = ao_ref_base (ref);
1492 if (!base)
1493 return true;
1495 /* A call that is not without side-effects might involve volatile
1496 accesses and thus conflicts with all other volatile accesses. */
1497 if (ref->volatile_p)
1498 return true;
1500 /* If the reference is based on a decl that is not aliased the call
1501 cannot possibly use it. */
1502 if (DECL_P (base)
1503 && !may_be_aliased (base)
1504 /* But local statics can be used through recursion. */
1505 && !is_global_var (base))
1506 goto process_args;
1508 callee = gimple_call_fndecl (call);
1510 /* Handle those builtin functions explicitly that do not act as
1511 escape points. See tree-ssa-structalias.c:find_func_aliases
1512 for the list of builtins we might need to handle here. */
1513 if (callee != NULL_TREE
1514 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1515 switch (DECL_FUNCTION_CODE (callee))
1517 /* All the following functions read memory pointed to by
1518 their second argument. strcat/strncat additionally
1519 reads memory pointed to by the first argument. */
1520 case BUILT_IN_STRCAT:
1521 case BUILT_IN_STRNCAT:
1523 ao_ref dref;
1524 ao_ref_init_from_ptr_and_size (&dref,
1525 gimple_call_arg (call, 0),
1526 NULL_TREE);
1527 if (refs_may_alias_p_1 (&dref, ref, false))
1528 return true;
1530 /* FALLTHRU */
1531 case BUILT_IN_STRCPY:
1532 case BUILT_IN_STRNCPY:
1533 case BUILT_IN_MEMCPY:
1534 case BUILT_IN_MEMMOVE:
1535 case BUILT_IN_MEMPCPY:
1536 case BUILT_IN_STPCPY:
1537 case BUILT_IN_STPNCPY:
1538 case BUILT_IN_TM_MEMCPY:
1539 case BUILT_IN_TM_MEMMOVE:
1541 ao_ref dref;
1542 tree size = NULL_TREE;
1543 if (gimple_call_num_args (call) == 3)
1544 size = gimple_call_arg (call, 2);
1545 ao_ref_init_from_ptr_and_size (&dref,
1546 gimple_call_arg (call, 1),
1547 size);
1548 return refs_may_alias_p_1 (&dref, ref, false);
1550 case BUILT_IN_STRCAT_CHK:
1551 case BUILT_IN_STRNCAT_CHK:
1553 ao_ref dref;
1554 ao_ref_init_from_ptr_and_size (&dref,
1555 gimple_call_arg (call, 0),
1556 NULL_TREE);
1557 if (refs_may_alias_p_1 (&dref, ref, false))
1558 return true;
1560 /* FALLTHRU */
1561 case BUILT_IN_STRCPY_CHK:
1562 case BUILT_IN_STRNCPY_CHK:
1563 case BUILT_IN_MEMCPY_CHK:
1564 case BUILT_IN_MEMMOVE_CHK:
1565 case BUILT_IN_MEMPCPY_CHK:
1566 case BUILT_IN_STPCPY_CHK:
1567 case BUILT_IN_STPNCPY_CHK:
1569 ao_ref dref;
1570 tree size = NULL_TREE;
1571 if (gimple_call_num_args (call) == 4)
1572 size = gimple_call_arg (call, 2);
1573 ao_ref_init_from_ptr_and_size (&dref,
1574 gimple_call_arg (call, 1),
1575 size);
1576 return refs_may_alias_p_1 (&dref, ref, false);
1578 case BUILT_IN_BCOPY:
1580 ao_ref dref;
1581 tree size = gimple_call_arg (call, 2);
1582 ao_ref_init_from_ptr_and_size (&dref,
1583 gimple_call_arg (call, 0),
1584 size);
1585 return refs_may_alias_p_1 (&dref, ref, false);
1588 /* The following functions read memory pointed to by their
1589 first argument. */
1590 CASE_BUILT_IN_TM_LOAD (1):
1591 CASE_BUILT_IN_TM_LOAD (2):
1592 CASE_BUILT_IN_TM_LOAD (4):
1593 CASE_BUILT_IN_TM_LOAD (8):
1594 CASE_BUILT_IN_TM_LOAD (FLOAT):
1595 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1596 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1597 CASE_BUILT_IN_TM_LOAD (M64):
1598 CASE_BUILT_IN_TM_LOAD (M128):
1599 CASE_BUILT_IN_TM_LOAD (M256):
1600 case BUILT_IN_TM_LOG:
1601 case BUILT_IN_TM_LOG_1:
1602 case BUILT_IN_TM_LOG_2:
1603 case BUILT_IN_TM_LOG_4:
1604 case BUILT_IN_TM_LOG_8:
1605 case BUILT_IN_TM_LOG_FLOAT:
1606 case BUILT_IN_TM_LOG_DOUBLE:
1607 case BUILT_IN_TM_LOG_LDOUBLE:
1608 case BUILT_IN_TM_LOG_M64:
1609 case BUILT_IN_TM_LOG_M128:
1610 case BUILT_IN_TM_LOG_M256:
1611 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1613 /* These read memory pointed to by the first argument. */
1614 case BUILT_IN_STRDUP:
1615 case BUILT_IN_STRNDUP:
1616 case BUILT_IN_REALLOC:
1618 ao_ref dref;
1619 tree size = NULL_TREE;
1620 if (gimple_call_num_args (call) == 2)
1621 size = gimple_call_arg (call, 1);
1622 ao_ref_init_from_ptr_and_size (&dref,
1623 gimple_call_arg (call, 0),
1624 size);
1625 return refs_may_alias_p_1 (&dref, ref, false);
1627 /* These read memory pointed to by the first argument. */
1628 case BUILT_IN_INDEX:
1629 case BUILT_IN_STRCHR:
1630 case BUILT_IN_STRRCHR:
1632 ao_ref dref;
1633 ao_ref_init_from_ptr_and_size (&dref,
1634 gimple_call_arg (call, 0),
1635 NULL_TREE);
1636 return refs_may_alias_p_1 (&dref, ref, false);
1638 /* These read memory pointed to by the first argument with size
1639 in the third argument. */
1640 case BUILT_IN_MEMCHR:
1642 ao_ref dref;
1643 ao_ref_init_from_ptr_and_size (&dref,
1644 gimple_call_arg (call, 0),
1645 gimple_call_arg (call, 2));
1646 return refs_may_alias_p_1 (&dref, ref, false);
1648 /* These read memory pointed to by the first and second arguments. */
1649 case BUILT_IN_STRSTR:
1650 case BUILT_IN_STRPBRK:
1652 ao_ref dref;
1653 ao_ref_init_from_ptr_and_size (&dref,
1654 gimple_call_arg (call, 0),
1655 NULL_TREE);
1656 if (refs_may_alias_p_1 (&dref, ref, false))
1657 return true;
1658 ao_ref_init_from_ptr_and_size (&dref,
1659 gimple_call_arg (call, 1),
1660 NULL_TREE);
1661 return refs_may_alias_p_1 (&dref, ref, false);
1664 /* The following builtins do not read from memory. */
1665 case BUILT_IN_FREE:
1666 case BUILT_IN_MALLOC:
1667 case BUILT_IN_POSIX_MEMALIGN:
1668 case BUILT_IN_ALIGNED_ALLOC:
1669 case BUILT_IN_CALLOC:
1670 case BUILT_IN_ALLOCA:
1671 case BUILT_IN_ALLOCA_WITH_ALIGN:
1672 case BUILT_IN_STACK_SAVE:
1673 case BUILT_IN_STACK_RESTORE:
1674 case BUILT_IN_MEMSET:
1675 case BUILT_IN_TM_MEMSET:
1676 case BUILT_IN_MEMSET_CHK:
1677 case BUILT_IN_FREXP:
1678 case BUILT_IN_FREXPF:
1679 case BUILT_IN_FREXPL:
1680 case BUILT_IN_GAMMA_R:
1681 case BUILT_IN_GAMMAF_R:
1682 case BUILT_IN_GAMMAL_R:
1683 case BUILT_IN_LGAMMA_R:
1684 case BUILT_IN_LGAMMAF_R:
1685 case BUILT_IN_LGAMMAL_R:
1686 case BUILT_IN_MODF:
1687 case BUILT_IN_MODFF:
1688 case BUILT_IN_MODFL:
1689 case BUILT_IN_REMQUO:
1690 case BUILT_IN_REMQUOF:
1691 case BUILT_IN_REMQUOL:
1692 case BUILT_IN_SINCOS:
1693 case BUILT_IN_SINCOSF:
1694 case BUILT_IN_SINCOSL:
1695 case BUILT_IN_ASSUME_ALIGNED:
1696 case BUILT_IN_VA_END:
1697 return false;
1698 /* __sync_* builtins and some OpenMP builtins act as threading
1699 barriers. */
1700 #undef DEF_SYNC_BUILTIN
1701 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1702 #include "sync-builtins.def"
1703 #undef DEF_SYNC_BUILTIN
1704 case BUILT_IN_GOMP_ATOMIC_START:
1705 case BUILT_IN_GOMP_ATOMIC_END:
1706 case BUILT_IN_GOMP_BARRIER:
1707 case BUILT_IN_GOMP_BARRIER_CANCEL:
1708 case BUILT_IN_GOMP_TASKWAIT:
1709 case BUILT_IN_GOMP_TASKGROUP_END:
1710 case BUILT_IN_GOMP_CRITICAL_START:
1711 case BUILT_IN_GOMP_CRITICAL_END:
1712 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1713 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1714 case BUILT_IN_GOMP_LOOP_END:
1715 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1716 case BUILT_IN_GOMP_ORDERED_START:
1717 case BUILT_IN_GOMP_ORDERED_END:
1718 case BUILT_IN_GOMP_SECTIONS_END:
1719 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1720 case BUILT_IN_GOMP_SINGLE_COPY_START:
1721 case BUILT_IN_GOMP_SINGLE_COPY_END:
1722 return true;
1724 default:
1725 /* Fallthru to general call handling. */;
1728 /* Check if base is a global static variable that is not read
1729 by the function. */
1730 if (callee != NULL_TREE
1731 && TREE_CODE (base) == VAR_DECL
1732 && TREE_STATIC (base))
1734 struct cgraph_node *node = cgraph_node::get (callee);
1735 bitmap not_read;
1737 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1738 node yet. We should enforce that there are nodes for all decls in the
1739 IL and remove this check instead. */
1740 if (node
1741 && (not_read = ipa_reference_get_not_read_global (node))
1742 && bitmap_bit_p (not_read, DECL_UID (base)))
1743 goto process_args;
1746 /* Check if the base variable is call-used. */
1747 if (DECL_P (base))
1749 if (pt_solution_includes (gimple_call_use_set (call), base))
1750 return true;
1752 else if ((TREE_CODE (base) == MEM_REF
1753 || TREE_CODE (base) == TARGET_MEM_REF)
1754 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1756 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1757 if (!pi)
1758 return true;
1760 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1761 return true;
1763 else
1764 return true;
1766 /* Inspect call arguments for passed-by-value aliases. */
1767 process_args:
1768 for (i = 0; i < gimple_call_num_args (call); ++i)
1770 tree op = gimple_call_arg (call, i);
1771 int flags = gimple_call_arg_flags (call, i);
1773 if (flags & EAF_UNUSED)
1774 continue;
1776 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1777 op = TREE_OPERAND (op, 0);
1779 if (TREE_CODE (op) != SSA_NAME
1780 && !is_gimple_min_invariant (op))
1782 ao_ref r;
1783 ao_ref_init (&r, op);
1784 if (refs_may_alias_p_1 (&r, ref, true))
1785 return true;
1789 return false;
1792 static bool
1793 ref_maybe_used_by_call_p (gimple call, ao_ref *ref)
1795 bool res;
1796 res = ref_maybe_used_by_call_p_1 (call, ref);
1797 if (res)
1798 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1799 else
1800 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1801 return res;
1805 /* If the statement STMT may use the memory reference REF return
1806 true, otherwise return false. */
1808 bool
1809 ref_maybe_used_by_stmt_p (gimple stmt, ao_ref *ref)
1811 if (is_gimple_assign (stmt))
1813 tree rhs;
1815 /* All memory assign statements are single. */
1816 if (!gimple_assign_single_p (stmt))
1817 return false;
1819 rhs = gimple_assign_rhs1 (stmt);
1820 if (is_gimple_reg (rhs)
1821 || is_gimple_min_invariant (rhs)
1822 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1823 return false;
1825 return refs_may_alias_p (rhs, ref);
1827 else if (is_gimple_call (stmt))
1828 return ref_maybe_used_by_call_p (stmt, ref);
1829 else if (gimple_code (stmt) == GIMPLE_RETURN)
1831 tree retval = gimple_return_retval (stmt);
1832 if (retval
1833 && TREE_CODE (retval) != SSA_NAME
1834 && !is_gimple_min_invariant (retval)
1835 && refs_may_alias_p (retval, ref))
1836 return true;
1837 /* If ref escapes the function then the return acts as a use. */
1838 tree base = ao_ref_base (ref);
1839 if (!base)
1841 else if (DECL_P (base))
1842 return is_global_var (base);
1843 else if (TREE_CODE (base) == MEM_REF
1844 || TREE_CODE (base) == TARGET_MEM_REF)
1845 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1846 return false;
1849 return true;
1852 bool
1853 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1855 ao_ref r;
1856 ao_ref_init (&r, ref);
1857 return ref_maybe_used_by_stmt_p (stmt, &r);
1860 /* If the call in statement CALL may clobber the memory reference REF
1861 return true, otherwise return false. */
1863 bool
1864 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1866 tree base;
1867 tree callee;
1869 /* If the call is pure or const it cannot clobber anything. */
1870 if (gimple_call_flags (call)
1871 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1872 return false;
1874 base = ao_ref_base (ref);
1875 if (!base)
1876 return true;
1878 if (TREE_CODE (base) == SSA_NAME
1879 || CONSTANT_CLASS_P (base))
1880 return false;
1882 /* A call that is not without side-effects might involve volatile
1883 accesses and thus conflicts with all other volatile accesses. */
1884 if (ref->volatile_p)
1885 return true;
1887 /* If the reference is based on a decl that is not aliased the call
1888 cannot possibly clobber it. */
1889 if (DECL_P (base)
1890 && !may_be_aliased (base)
1891 /* But local non-readonly statics can be modified through recursion
1892 or the call may implement a threading barrier which we must
1893 treat as may-def. */
1894 && (TREE_READONLY (base)
1895 || !is_global_var (base)))
1896 return false;
1898 callee = gimple_call_fndecl (call);
1900 /* Handle those builtin functions explicitly that do not act as
1901 escape points. See tree-ssa-structalias.c:find_func_aliases
1902 for the list of builtins we might need to handle here. */
1903 if (callee != NULL_TREE
1904 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1905 switch (DECL_FUNCTION_CODE (callee))
1907 /* All the following functions clobber memory pointed to by
1908 their first argument. */
1909 case BUILT_IN_STRCPY:
1910 case BUILT_IN_STRNCPY:
1911 case BUILT_IN_MEMCPY:
1912 case BUILT_IN_MEMMOVE:
1913 case BUILT_IN_MEMPCPY:
1914 case BUILT_IN_STPCPY:
1915 case BUILT_IN_STPNCPY:
1916 case BUILT_IN_STRCAT:
1917 case BUILT_IN_STRNCAT:
1918 case BUILT_IN_MEMSET:
1919 case BUILT_IN_TM_MEMSET:
1920 CASE_BUILT_IN_TM_STORE (1):
1921 CASE_BUILT_IN_TM_STORE (2):
1922 CASE_BUILT_IN_TM_STORE (4):
1923 CASE_BUILT_IN_TM_STORE (8):
1924 CASE_BUILT_IN_TM_STORE (FLOAT):
1925 CASE_BUILT_IN_TM_STORE (DOUBLE):
1926 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1927 CASE_BUILT_IN_TM_STORE (M64):
1928 CASE_BUILT_IN_TM_STORE (M128):
1929 CASE_BUILT_IN_TM_STORE (M256):
1930 case BUILT_IN_TM_MEMCPY:
1931 case BUILT_IN_TM_MEMMOVE:
1933 ao_ref dref;
1934 tree size = NULL_TREE;
1935 /* Don't pass in size for strncat, as the maximum size
1936 is strlen (dest) + n + 1 instead of n, resp.
1937 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1938 known. */
1939 if (gimple_call_num_args (call) == 3
1940 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1941 size = gimple_call_arg (call, 2);
1942 ao_ref_init_from_ptr_and_size (&dref,
1943 gimple_call_arg (call, 0),
1944 size);
1945 return refs_may_alias_p_1 (&dref, ref, false);
1947 case BUILT_IN_STRCPY_CHK:
1948 case BUILT_IN_STRNCPY_CHK:
1949 case BUILT_IN_MEMCPY_CHK:
1950 case BUILT_IN_MEMMOVE_CHK:
1951 case BUILT_IN_MEMPCPY_CHK:
1952 case BUILT_IN_STPCPY_CHK:
1953 case BUILT_IN_STPNCPY_CHK:
1954 case BUILT_IN_STRCAT_CHK:
1955 case BUILT_IN_STRNCAT_CHK:
1956 case BUILT_IN_MEMSET_CHK:
1958 ao_ref dref;
1959 tree size = NULL_TREE;
1960 /* Don't pass in size for __strncat_chk, as the maximum size
1961 is strlen (dest) + n + 1 instead of n, resp.
1962 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1963 known. */
1964 if (gimple_call_num_args (call) == 4
1965 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1966 size = gimple_call_arg (call, 2);
1967 ao_ref_init_from_ptr_and_size (&dref,
1968 gimple_call_arg (call, 0),
1969 size);
1970 return refs_may_alias_p_1 (&dref, ref, false);
1972 case BUILT_IN_BCOPY:
1974 ao_ref dref;
1975 tree size = gimple_call_arg (call, 2);
1976 ao_ref_init_from_ptr_and_size (&dref,
1977 gimple_call_arg (call, 1),
1978 size);
1979 return refs_may_alias_p_1 (&dref, ref, false);
1981 /* Allocating memory does not have any side-effects apart from
1982 being the definition point for the pointer. */
1983 case BUILT_IN_MALLOC:
1984 case BUILT_IN_ALIGNED_ALLOC:
1985 case BUILT_IN_CALLOC:
1986 case BUILT_IN_STRDUP:
1987 case BUILT_IN_STRNDUP:
1988 /* Unix98 specifies that errno is set on allocation failure. */
1989 if (flag_errno_math
1990 && targetm.ref_may_alias_errno (ref))
1991 return true;
1992 return false;
1993 case BUILT_IN_STACK_SAVE:
1994 case BUILT_IN_ALLOCA:
1995 case BUILT_IN_ALLOCA_WITH_ALIGN:
1996 case BUILT_IN_ASSUME_ALIGNED:
1997 return false;
1998 /* But posix_memalign stores a pointer into the memory pointed to
1999 by its first argument. */
2000 case BUILT_IN_POSIX_MEMALIGN:
2002 tree ptrptr = gimple_call_arg (call, 0);
2003 ao_ref dref;
2004 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2005 TYPE_SIZE_UNIT (ptr_type_node));
2006 return (refs_may_alias_p_1 (&dref, ref, false)
2007 || (flag_errno_math
2008 && targetm.ref_may_alias_errno (ref)));
2010 /* Freeing memory kills the pointed-to memory. More importantly
2011 the call has to serve as a barrier for moving loads and stores
2012 across it. */
2013 case BUILT_IN_FREE:
2014 case BUILT_IN_VA_END:
2016 tree ptr = gimple_call_arg (call, 0);
2017 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2019 /* Realloc serves both as allocation point and deallocation point. */
2020 case BUILT_IN_REALLOC:
2022 tree ptr = gimple_call_arg (call, 0);
2023 /* Unix98 specifies that errno is set on allocation failure. */
2024 return ((flag_errno_math
2025 && targetm.ref_may_alias_errno (ref))
2026 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2028 case BUILT_IN_GAMMA_R:
2029 case BUILT_IN_GAMMAF_R:
2030 case BUILT_IN_GAMMAL_R:
2031 case BUILT_IN_LGAMMA_R:
2032 case BUILT_IN_LGAMMAF_R:
2033 case BUILT_IN_LGAMMAL_R:
2035 tree out = gimple_call_arg (call, 1);
2036 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2037 return true;
2038 if (flag_errno_math)
2039 break;
2040 return false;
2042 case BUILT_IN_FREXP:
2043 case BUILT_IN_FREXPF:
2044 case BUILT_IN_FREXPL:
2045 case BUILT_IN_MODF:
2046 case BUILT_IN_MODFF:
2047 case BUILT_IN_MODFL:
2049 tree out = gimple_call_arg (call, 1);
2050 return ptr_deref_may_alias_ref_p_1 (out, ref);
2052 case BUILT_IN_REMQUO:
2053 case BUILT_IN_REMQUOF:
2054 case BUILT_IN_REMQUOL:
2056 tree out = gimple_call_arg (call, 2);
2057 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2058 return true;
2059 if (flag_errno_math)
2060 break;
2061 return false;
2063 case BUILT_IN_SINCOS:
2064 case BUILT_IN_SINCOSF:
2065 case BUILT_IN_SINCOSL:
2067 tree sin = gimple_call_arg (call, 1);
2068 tree cos = gimple_call_arg (call, 2);
2069 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2070 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2072 /* __sync_* builtins and some OpenMP builtins act as threading
2073 barriers. */
2074 #undef DEF_SYNC_BUILTIN
2075 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2076 #include "sync-builtins.def"
2077 #undef DEF_SYNC_BUILTIN
2078 case BUILT_IN_GOMP_ATOMIC_START:
2079 case BUILT_IN_GOMP_ATOMIC_END:
2080 case BUILT_IN_GOMP_BARRIER:
2081 case BUILT_IN_GOMP_BARRIER_CANCEL:
2082 case BUILT_IN_GOMP_TASKWAIT:
2083 case BUILT_IN_GOMP_TASKGROUP_END:
2084 case BUILT_IN_GOMP_CRITICAL_START:
2085 case BUILT_IN_GOMP_CRITICAL_END:
2086 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2087 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2088 case BUILT_IN_GOMP_LOOP_END:
2089 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2090 case BUILT_IN_GOMP_ORDERED_START:
2091 case BUILT_IN_GOMP_ORDERED_END:
2092 case BUILT_IN_GOMP_SECTIONS_END:
2093 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2094 case BUILT_IN_GOMP_SINGLE_COPY_START:
2095 case BUILT_IN_GOMP_SINGLE_COPY_END:
2096 return true;
2097 default:
2098 /* Fallthru to general call handling. */;
2101 /* Check if base is a global static variable that is not written
2102 by the function. */
2103 if (callee != NULL_TREE
2104 && TREE_CODE (base) == VAR_DECL
2105 && TREE_STATIC (base))
2107 struct cgraph_node *node = cgraph_node::get (callee);
2108 bitmap not_written;
2110 if (node
2111 && (not_written = ipa_reference_get_not_written_global (node))
2112 && bitmap_bit_p (not_written, DECL_UID (base)))
2113 return false;
2116 /* Check if the base variable is call-clobbered. */
2117 if (DECL_P (base))
2118 return pt_solution_includes (gimple_call_clobber_set (call), base);
2119 else if ((TREE_CODE (base) == MEM_REF
2120 || TREE_CODE (base) == TARGET_MEM_REF)
2121 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2123 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2124 if (!pi)
2125 return true;
2127 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2130 return true;
2133 /* If the call in statement CALL may clobber the memory reference REF
2134 return true, otherwise return false. */
2136 bool
2137 call_may_clobber_ref_p (gimple call, tree ref)
2139 bool res;
2140 ao_ref r;
2141 ao_ref_init (&r, ref);
2142 res = call_may_clobber_ref_p_1 (call, &r);
2143 if (res)
2144 ++alias_stats.call_may_clobber_ref_p_may_alias;
2145 else
2146 ++alias_stats.call_may_clobber_ref_p_no_alias;
2147 return res;
2151 /* If the statement STMT may clobber the memory reference REF return true,
2152 otherwise return false. */
2154 bool
2155 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2157 if (is_gimple_call (stmt))
2159 tree lhs = gimple_call_lhs (stmt);
2160 if (lhs
2161 && TREE_CODE (lhs) != SSA_NAME)
2163 ao_ref r;
2164 ao_ref_init (&r, lhs);
2165 if (refs_may_alias_p_1 (ref, &r, true))
2166 return true;
2169 return call_may_clobber_ref_p_1 (stmt, ref);
2171 else if (gimple_assign_single_p (stmt))
2173 tree lhs = gimple_assign_lhs (stmt);
2174 if (TREE_CODE (lhs) != SSA_NAME)
2176 ao_ref r;
2177 ao_ref_init (&r, lhs);
2178 return refs_may_alias_p_1 (ref, &r, true);
2181 else if (gimple_code (stmt) == GIMPLE_ASM)
2182 return true;
2184 return false;
2187 bool
2188 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2190 ao_ref r;
2191 ao_ref_init (&r, ref);
2192 return stmt_may_clobber_ref_p_1 (stmt, &r);
2195 /* If STMT kills the memory reference REF return true, otherwise
2196 return false. */
2198 bool
2199 stmt_kills_ref_p (gimple stmt, ao_ref *ref)
2201 if (!ao_ref_base (ref))
2202 return false;
2204 if (gimple_has_lhs (stmt)
2205 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2206 /* The assignment is not necessarily carried out if it can throw
2207 and we can catch it in the current function where we could inspect
2208 the previous value.
2209 ??? We only need to care about the RHS throwing. For aggregate
2210 assignments or similar calls and non-call exceptions the LHS
2211 might throw as well. */
2212 && !stmt_can_throw_internal (stmt))
2214 tree lhs = gimple_get_lhs (stmt);
2215 /* If LHS is literally a base of the access we are done. */
2216 if (ref->ref)
2218 tree base = ref->ref;
2219 if (handled_component_p (base))
2221 tree saved_lhs0 = NULL_TREE;
2222 if (handled_component_p (lhs))
2224 saved_lhs0 = TREE_OPERAND (lhs, 0);
2225 TREE_OPERAND (lhs, 0) = integer_zero_node;
2229 /* Just compare the outermost handled component, if
2230 they are equal we have found a possible common
2231 base. */
2232 tree saved_base0 = TREE_OPERAND (base, 0);
2233 TREE_OPERAND (base, 0) = integer_zero_node;
2234 bool res = operand_equal_p (lhs, base, 0);
2235 TREE_OPERAND (base, 0) = saved_base0;
2236 if (res)
2237 break;
2238 /* Otherwise drop handled components of the access. */
2239 base = saved_base0;
2241 while (handled_component_p (base));
2242 if (saved_lhs0)
2243 TREE_OPERAND (lhs, 0) = saved_lhs0;
2245 /* Finally check if lhs is equal or equal to the base candidate
2246 of the access. */
2247 if (operand_equal_p (lhs, base, 0))
2248 return true;
2251 /* Now look for non-literal equal bases with the restriction of
2252 handling constant offset and size. */
2253 /* For a must-alias check we need to be able to constrain
2254 the access properly. */
2255 if (ref->max_size == -1)
2256 return false;
2257 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2258 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2259 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2260 so base == ref->base does not always hold. */
2261 if (base != ref->base)
2263 /* If both base and ref->base are MEM_REFs, only compare the
2264 first operand, and if the second operand isn't equal constant,
2265 try to add the offsets into offset and ref_offset. */
2266 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2267 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2269 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2270 TREE_OPERAND (ref->base, 1)))
2272 offset_int off1 = mem_ref_offset (base);
2273 off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2274 off1 += offset;
2275 offset_int off2 = mem_ref_offset (ref->base);
2276 off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2277 off2 += ref_offset;
2278 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2280 offset = off1.to_shwi ();
2281 ref_offset = off2.to_shwi ();
2283 else
2284 size = -1;
2287 else
2288 size = -1;
2290 /* For a must-alias check we need to be able to constrain
2291 the access properly. */
2292 if (size != -1 && size == max_size)
2294 if (offset <= ref_offset
2295 && offset + size >= ref_offset + ref->max_size)
2296 return true;
2300 if (is_gimple_call (stmt))
2302 tree callee = gimple_call_fndecl (stmt);
2303 if (callee != NULL_TREE
2304 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2305 switch (DECL_FUNCTION_CODE (callee))
2307 case BUILT_IN_FREE:
2309 tree ptr = gimple_call_arg (stmt, 0);
2310 tree base = ao_ref_base (ref);
2311 if (base && TREE_CODE (base) == MEM_REF
2312 && TREE_OPERAND (base, 0) == ptr)
2313 return true;
2314 break;
2317 case BUILT_IN_MEMCPY:
2318 case BUILT_IN_MEMPCPY:
2319 case BUILT_IN_MEMMOVE:
2320 case BUILT_IN_MEMSET:
2321 case BUILT_IN_MEMCPY_CHK:
2322 case BUILT_IN_MEMPCPY_CHK:
2323 case BUILT_IN_MEMMOVE_CHK:
2324 case BUILT_IN_MEMSET_CHK:
2326 /* For a must-alias check we need to be able to constrain
2327 the access properly. */
2328 if (ref->max_size == -1)
2329 return false;
2330 tree dest = gimple_call_arg (stmt, 0);
2331 tree len = gimple_call_arg (stmt, 2);
2332 if (!tree_fits_shwi_p (len))
2333 return false;
2334 tree rbase = ref->base;
2335 offset_int roffset = ref->offset;
2336 ao_ref dref;
2337 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2338 tree base = ao_ref_base (&dref);
2339 offset_int offset = dref.offset;
2340 if (!base || dref.size == -1)
2341 return false;
2342 if (TREE_CODE (base) == MEM_REF)
2344 if (TREE_CODE (rbase) != MEM_REF)
2345 return false;
2346 // Compare pointers.
2347 offset += wi::lshift (mem_ref_offset (base),
2348 LOG2_BITS_PER_UNIT);
2349 roffset += wi::lshift (mem_ref_offset (rbase),
2350 LOG2_BITS_PER_UNIT);
2351 base = TREE_OPERAND (base, 0);
2352 rbase = TREE_OPERAND (rbase, 0);
2354 if (base == rbase
2355 && wi::les_p (offset, roffset)
2356 && wi::les_p (roffset + ref->max_size,
2357 offset + wi::lshift (wi::to_offset (len),
2358 LOG2_BITS_PER_UNIT)))
2359 return true;
2360 break;
2363 case BUILT_IN_VA_END:
2365 tree ptr = gimple_call_arg (stmt, 0);
2366 if (TREE_CODE (ptr) == ADDR_EXPR)
2368 tree base = ao_ref_base (ref);
2369 if (TREE_OPERAND (ptr, 0) == base)
2370 return true;
2372 break;
2375 default:;
2378 return false;
2381 bool
2382 stmt_kills_ref_p (gimple stmt, tree ref)
2384 ao_ref r;
2385 ao_ref_init (&r, ref);
2386 return stmt_kills_ref_p (stmt, &r);
2390 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2391 TARGET or a statement clobbering the memory reference REF in which
2392 case false is returned. The walk starts with VUSE, one argument of PHI. */
2394 static bool
2395 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2396 tree vuse, unsigned int *cnt, bitmap *visited,
2397 bool abort_on_visited,
2398 void *(*translate)(ao_ref *, tree, void *, bool),
2399 void *data)
2401 basic_block bb = gimple_bb (phi);
2403 if (!*visited)
2404 *visited = BITMAP_ALLOC (NULL);
2406 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2408 /* Walk until we hit the target. */
2409 while (vuse != target)
2411 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2412 /* Recurse for PHI nodes. */
2413 if (gimple_code (def_stmt) == GIMPLE_PHI)
2415 /* An already visited PHI node ends the walk successfully. */
2416 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2417 return !abort_on_visited;
2418 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2419 visited, abort_on_visited,
2420 translate, data);
2421 if (!vuse)
2422 return false;
2423 continue;
2425 else if (gimple_nop_p (def_stmt))
2426 return false;
2427 else
2429 /* A clobbering statement or the end of the IL ends it failing. */
2430 ++*cnt;
2431 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2433 if (translate
2434 && (*translate) (ref, vuse, data, true) == NULL)
2436 else
2437 return false;
2440 /* If we reach a new basic-block see if we already skipped it
2441 in a previous walk that ended successfully. */
2442 if (gimple_bb (def_stmt) != bb)
2444 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2445 return !abort_on_visited;
2446 bb = gimple_bb (def_stmt);
2448 vuse = gimple_vuse (def_stmt);
2450 return true;
2453 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2454 until we hit the phi argument definition that dominates the other one.
2455 Return that, or NULL_TREE if there is no such definition. */
2457 static tree
2458 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2459 ao_ref *ref, unsigned int *cnt,
2460 bitmap *visited, bool abort_on_visited,
2461 void *(*translate)(ao_ref *, tree, void *, bool),
2462 void *data)
2464 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2465 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2466 tree common_vuse;
2468 if (arg0 == arg1)
2469 return arg0;
2470 else if (gimple_nop_p (def0)
2471 || (!gimple_nop_p (def1)
2472 && dominated_by_p (CDI_DOMINATORS,
2473 gimple_bb (def1), gimple_bb (def0))))
2475 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2476 visited, abort_on_visited, translate, data))
2477 return arg0;
2479 else if (gimple_nop_p (def1)
2480 || dominated_by_p (CDI_DOMINATORS,
2481 gimple_bb (def0), gimple_bb (def1)))
2483 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2484 visited, abort_on_visited, translate, data))
2485 return arg1;
2487 /* Special case of a diamond:
2488 MEM_1 = ...
2489 goto (cond) ? L1 : L2
2490 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2491 goto L3
2492 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2493 L3: MEM_4 = PHI<MEM_2, MEM_3>
2494 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2495 dominate each other, but still we can easily skip this PHI node
2496 if we recognize that the vuse MEM operand is the same for both,
2497 and that we can skip both statements (they don't clobber us).
2498 This is still linear. Don't use maybe_skip_until, that might
2499 potentially be slow. */
2500 else if ((common_vuse = gimple_vuse (def0))
2501 && common_vuse == gimple_vuse (def1))
2503 *cnt += 2;
2504 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2505 || (translate
2506 && (*translate) (ref, arg0, data, true) == NULL))
2507 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2508 || (translate
2509 && (*translate) (ref, arg1, data, true) == NULL)))
2510 return common_vuse;
2513 return NULL_TREE;
2517 /* Starting from a PHI node for the virtual operand of the memory reference
2518 REF find a continuation virtual operand that allows to continue walking
2519 statements dominating PHI skipping only statements that cannot possibly
2520 clobber REF. Increments *CNT for each alias disambiguation done.
2521 Returns NULL_TREE if no suitable virtual operand can be found. */
2523 tree
2524 get_continuation_for_phi (gimple phi, ao_ref *ref,
2525 unsigned int *cnt, bitmap *visited,
2526 bool abort_on_visited,
2527 void *(*translate)(ao_ref *, tree, void *, bool),
2528 void *data)
2530 unsigned nargs = gimple_phi_num_args (phi);
2532 /* Through a single-argument PHI we can simply look through. */
2533 if (nargs == 1)
2534 return PHI_ARG_DEF (phi, 0);
2536 /* For two or more arguments try to pairwise skip non-aliasing code
2537 until we hit the phi argument definition that dominates the other one. */
2538 else if (nargs >= 2)
2540 tree arg0, arg1;
2541 unsigned i;
2543 /* Find a candidate for the virtual operand which definition
2544 dominates those of all others. */
2545 arg0 = PHI_ARG_DEF (phi, 0);
2546 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2547 for (i = 1; i < nargs; ++i)
2549 arg1 = PHI_ARG_DEF (phi, i);
2550 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2552 arg0 = arg1;
2553 break;
2555 if (dominated_by_p (CDI_DOMINATORS,
2556 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2557 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2558 arg0 = arg1;
2561 /* Then pairwise reduce against the found candidate. */
2562 for (i = 0; i < nargs; ++i)
2564 arg1 = PHI_ARG_DEF (phi, i);
2565 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2566 cnt, visited, abort_on_visited,
2567 translate, data);
2568 if (!arg0)
2569 return NULL_TREE;
2572 return arg0;
2575 return NULL_TREE;
2578 /* Based on the memory reference REF and its virtual use VUSE call
2579 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2580 itself. That is, for each virtual use for which its defining statement
2581 does not clobber REF.
2583 WALKER is called with REF, the current virtual use and DATA. If
2584 WALKER returns non-NULL the walk stops and its result is returned.
2585 At the end of a non-successful walk NULL is returned.
2587 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2588 use which definition is a statement that may clobber REF and DATA.
2589 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2590 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2591 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2592 to adjust REF and *DATA to make that valid.
2594 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2596 void *
2597 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2598 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2599 void *(*translate)(ao_ref *, tree, void *, bool),
2600 void *data)
2602 bitmap visited = NULL;
2603 void *res;
2604 unsigned int cnt = 0;
2605 bool translated = false;
2607 timevar_push (TV_ALIAS_STMT_WALK);
2611 gimple def_stmt;
2613 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2614 res = (*walker) (ref, vuse, cnt, data);
2615 /* Abort walk. */
2616 if (res == (void *)-1)
2618 res = NULL;
2619 break;
2621 /* Lookup succeeded. */
2622 else if (res != NULL)
2623 break;
2625 def_stmt = SSA_NAME_DEF_STMT (vuse);
2626 if (gimple_nop_p (def_stmt))
2627 break;
2628 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2629 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2630 &visited, translated, translate, data);
2631 else
2633 cnt++;
2634 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2636 if (!translate)
2637 break;
2638 res = (*translate) (ref, vuse, data, false);
2639 /* Failed lookup and translation. */
2640 if (res == (void *)-1)
2642 res = NULL;
2643 break;
2645 /* Lookup succeeded. */
2646 else if (res != NULL)
2647 break;
2648 /* Translation succeeded, continue walking. */
2649 translated = true;
2651 vuse = gimple_vuse (def_stmt);
2654 while (vuse);
2656 if (visited)
2657 BITMAP_FREE (visited);
2659 timevar_pop (TV_ALIAS_STMT_WALK);
2661 return res;
2665 /* Based on the memory reference REF call WALKER for each vdef which
2666 defining statement may clobber REF, starting with VDEF. If REF
2667 is NULL_TREE, each defining statement is visited.
2669 WALKER is called with REF, the current vdef and DATA. If WALKER
2670 returns true the walk is stopped, otherwise it continues.
2672 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2673 The pointer may be NULL and then we do not track this information.
2675 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2676 PHI argument (but only one walk continues on merge points), the
2677 return value is true if any of the walks was successful.
2679 The function returns the number of statements walked. */
2681 static unsigned int
2682 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2683 bool (*walker)(ao_ref *, tree, void *), void *data,
2684 bitmap *visited, unsigned int cnt,
2685 bool *function_entry_reached)
2689 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2691 if (*visited
2692 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2693 return cnt;
2695 if (gimple_nop_p (def_stmt))
2697 if (function_entry_reached)
2698 *function_entry_reached = true;
2699 return cnt;
2701 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2703 unsigned i;
2704 if (!*visited)
2705 *visited = BITMAP_ALLOC (NULL);
2706 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2707 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2708 walker, data, visited, 0,
2709 function_entry_reached);
2710 return cnt;
2713 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2714 cnt++;
2715 if ((!ref
2716 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2717 && (*walker) (ref, vdef, data))
2718 return cnt;
2720 vdef = gimple_vuse (def_stmt);
2722 while (1);
2725 unsigned int
2726 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2727 bool (*walker)(ao_ref *, tree, void *), void *data,
2728 bitmap *visited,
2729 bool *function_entry_reached)
2731 bitmap local_visited = NULL;
2732 unsigned int ret;
2734 timevar_push (TV_ALIAS_STMT_WALK);
2736 if (function_entry_reached)
2737 *function_entry_reached = false;
2739 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2740 visited ? visited : &local_visited, 0,
2741 function_entry_reached);
2742 if (local_visited)
2743 BITMAP_FREE (local_visited);
2745 timevar_pop (TV_ALIAS_STMT_WALK);
2747 return ret;