PR debug/66535
[official-gcc.git] / gcc / tree-ssa-alias.c
blobb2d29cad0941d8a3a848d21134c72a59aa462ba2
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2015 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 "input.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "tm_p.h"
31 #include "target.h"
32 #include "predict.h"
34 #include "hard-reg-set.h"
35 #include "function.h"
36 #include "dominance.h"
37 #include "basic-block.h"
38 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
39 #include "langhooks.h"
40 #include "flags.h"
41 #include "tree-pretty-print.h"
42 #include "dumpfile.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "tree-eh.h"
46 #include "gimple-expr.h"
47 #include "is-a.h"
48 #include "gimple.h"
49 #include "gimple-ssa.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "rtl.h"
53 #include "insn-config.h"
54 #include "expmed.h"
55 #include "dojump.h"
56 #include "explow.h"
57 #include "calls.h"
58 #include "emit-rtl.h"
59 #include "varasm.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "tree-dfa.h"
63 #include "tree-inline.h"
64 #include "params.h"
65 #include "alloc-pool.h"
66 #include "bitmap.h"
67 #include "plugin-api.h"
68 #include "ipa-ref.h"
69 #include "cgraph.h"
70 #include "ipa-reference.h"
72 /* Broad overview of how alias analysis on gimple works:
74 Statements clobbering or using memory are linked through the
75 virtual operand factored use-def chain. The virtual operand
76 is unique per function, its symbol is accessible via gimple_vop (cfun).
77 Virtual operands are used for efficiently walking memory statements
78 in the gimple IL and are useful for things like value-numbering as
79 a generation count for memory references.
81 SSA_NAME pointers may have associated points-to information
82 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
83 points-to information is (re-)computed by the TODO_rebuild_alias
84 pass manager todo. Points-to information is also used for more
85 precise tracking of call-clobbered and call-used variables and
86 related disambiguations.
88 This file contains functions for disambiguating memory references,
89 the so called alias-oracle and tools for walking of the gimple IL.
91 The main alias-oracle entry-points are
93 bool stmt_may_clobber_ref_p (gimple, tree)
95 This function queries if a statement may invalidate (parts of)
96 the memory designated by the reference tree argument.
98 bool ref_maybe_used_by_stmt_p (gimple, tree)
100 This function queries if a statement may need (parts of) the
101 memory designated by the reference tree argument.
103 There are variants of these functions that only handle the call
104 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
105 Note that these do not disambiguate against a possible call lhs.
107 bool refs_may_alias_p (tree, tree)
109 This function tries to disambiguate two reference trees.
111 bool ptr_deref_may_alias_global_p (tree)
113 This function queries if dereferencing a pointer variable may
114 alias global memory.
116 More low-level disambiguators are available and documented in
117 this file. Low-level disambiguators dealing with points-to
118 information are in tree-ssa-structalias.c. */
121 /* Query statistics for the different low-level disambiguators.
122 A high-level query may trigger multiple of them. */
124 static struct {
125 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
126 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
127 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
128 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
129 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
130 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
131 } alias_stats;
133 void
134 dump_alias_stats (FILE *s)
136 fprintf (s, "\nAlias oracle query stats:\n");
137 fprintf (s, " refs_may_alias_p: "
138 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
139 HOST_WIDE_INT_PRINT_DEC" queries\n",
140 alias_stats.refs_may_alias_p_no_alias,
141 alias_stats.refs_may_alias_p_no_alias
142 + alias_stats.refs_may_alias_p_may_alias);
143 fprintf (s, " ref_maybe_used_by_call_p: "
144 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
145 HOST_WIDE_INT_PRINT_DEC" queries\n",
146 alias_stats.ref_maybe_used_by_call_p_no_alias,
147 alias_stats.refs_may_alias_p_no_alias
148 + alias_stats.ref_maybe_used_by_call_p_may_alias);
149 fprintf (s, " call_may_clobber_ref_p: "
150 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
151 HOST_WIDE_INT_PRINT_DEC" queries\n",
152 alias_stats.call_may_clobber_ref_p_no_alias,
153 alias_stats.call_may_clobber_ref_p_no_alias
154 + alias_stats.call_may_clobber_ref_p_may_alias);
155 dump_alias_stats_in_alias_c (s);
159 /* Return true, if dereferencing PTR may alias with a global variable. */
161 bool
162 ptr_deref_may_alias_global_p (tree ptr)
164 struct ptr_info_def *pi;
166 /* If we end up with a pointer constant here that may point
167 to global memory. */
168 if (TREE_CODE (ptr) != SSA_NAME)
169 return true;
171 pi = SSA_NAME_PTR_INFO (ptr);
173 /* If we do not have points-to information for this variable,
174 we have to punt. */
175 if (!pi)
176 return true;
178 /* ??? This does not use TBAA to prune globals ptr may not access. */
179 return pt_solution_includes_global (&pi->pt);
182 /* Return true if dereferencing PTR may alias DECL.
183 The caller is responsible for applying TBAA to see if PTR
184 may access DECL at all. */
186 static bool
187 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
189 struct ptr_info_def *pi;
191 /* Conversions are irrelevant for points-to information and
192 data-dependence analysis can feed us those. */
193 STRIP_NOPS (ptr);
195 /* Anything we do not explicilty handle aliases. */
196 if ((TREE_CODE (ptr) != SSA_NAME
197 && TREE_CODE (ptr) != ADDR_EXPR
198 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
199 || !POINTER_TYPE_P (TREE_TYPE (ptr))
200 || (TREE_CODE (decl) != VAR_DECL
201 && TREE_CODE (decl) != PARM_DECL
202 && TREE_CODE (decl) != RESULT_DECL))
203 return true;
205 /* Disregard pointer offsetting. */
206 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
210 ptr = TREE_OPERAND (ptr, 0);
212 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
213 return ptr_deref_may_alias_decl_p (ptr, decl);
216 /* ADDR_EXPR pointers either just offset another pointer or directly
217 specify the pointed-to set. */
218 if (TREE_CODE (ptr) == ADDR_EXPR)
220 tree base = get_base_address (TREE_OPERAND (ptr, 0));
221 if (base
222 && (TREE_CODE (base) == MEM_REF
223 || TREE_CODE (base) == TARGET_MEM_REF))
224 ptr = TREE_OPERAND (base, 0);
225 else if (base
226 && DECL_P (base))
227 return base == decl;
228 else if (base
229 && CONSTANT_CLASS_P (base))
230 return false;
231 else
232 return true;
235 /* Non-aliased variables can not be pointed to. */
236 if (!may_be_aliased (decl))
237 return false;
239 /* If we do not have useful points-to information for this pointer
240 we cannot disambiguate anything else. */
241 pi = SSA_NAME_PTR_INFO (ptr);
242 if (!pi)
243 return true;
245 return pt_solution_includes (&pi->pt, decl);
248 /* Return true if dereferenced PTR1 and PTR2 may alias.
249 The caller is responsible for applying TBAA to see if accesses
250 through PTR1 and PTR2 may conflict at all. */
252 bool
253 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
255 struct ptr_info_def *pi1, *pi2;
257 /* Conversions are irrelevant for points-to information and
258 data-dependence analysis can feed us those. */
259 STRIP_NOPS (ptr1);
260 STRIP_NOPS (ptr2);
262 /* Disregard pointer offsetting. */
263 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
267 ptr1 = TREE_OPERAND (ptr1, 0);
269 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
270 return ptr_derefs_may_alias_p (ptr1, ptr2);
272 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
276 ptr2 = TREE_OPERAND (ptr2, 0);
278 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
279 return ptr_derefs_may_alias_p (ptr1, ptr2);
282 /* ADDR_EXPR pointers either just offset another pointer or directly
283 specify the pointed-to set. */
284 if (TREE_CODE (ptr1) == ADDR_EXPR)
286 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
287 if (base
288 && (TREE_CODE (base) == MEM_REF
289 || TREE_CODE (base) == TARGET_MEM_REF))
290 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
291 else if (base
292 && DECL_P (base))
293 return ptr_deref_may_alias_decl_p (ptr2, base);
294 else
295 return true;
297 if (TREE_CODE (ptr2) == ADDR_EXPR)
299 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
300 if (base
301 && (TREE_CODE (base) == MEM_REF
302 || TREE_CODE (base) == TARGET_MEM_REF))
303 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
304 else if (base
305 && DECL_P (base))
306 return ptr_deref_may_alias_decl_p (ptr1, base);
307 else
308 return true;
311 /* From here we require SSA name pointers. Anything else aliases. */
312 if (TREE_CODE (ptr1) != SSA_NAME
313 || TREE_CODE (ptr2) != SSA_NAME
314 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
315 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
316 return true;
318 /* We may end up with two empty points-to solutions for two same pointers.
319 In this case we still want to say both pointers alias, so shortcut
320 that here. */
321 if (ptr1 == ptr2)
322 return true;
324 /* If we do not have useful points-to information for either pointer
325 we cannot disambiguate anything else. */
326 pi1 = SSA_NAME_PTR_INFO (ptr1);
327 pi2 = SSA_NAME_PTR_INFO (ptr2);
328 if (!pi1 || !pi2)
329 return true;
331 /* ??? This does not use TBAA to prune decls from the intersection
332 that not both pointers may access. */
333 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
336 /* Return true if dereferencing PTR may alias *REF.
337 The caller is responsible for applying TBAA to see if PTR
338 may access *REF at all. */
340 static bool
341 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
343 tree base = ao_ref_base (ref);
345 if (TREE_CODE (base) == MEM_REF
346 || TREE_CODE (base) == TARGET_MEM_REF)
347 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
348 else if (DECL_P (base))
349 return ptr_deref_may_alias_decl_p (ptr, base);
351 return true;
354 /* Returns whether reference REF to BASE may refer to global memory. */
356 static bool
357 ref_may_alias_global_p_1 (tree base)
359 if (DECL_P (base))
360 return is_global_var (base);
361 else if (TREE_CODE (base) == MEM_REF
362 || TREE_CODE (base) == TARGET_MEM_REF)
363 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
364 return true;
367 bool
368 ref_may_alias_global_p (ao_ref *ref)
370 tree base = ao_ref_base (ref);
371 return ref_may_alias_global_p_1 (base);
374 bool
375 ref_may_alias_global_p (tree ref)
377 tree base = get_base_address (ref);
378 return ref_may_alias_global_p_1 (base);
381 /* Return true whether STMT may clobber global memory. */
383 bool
384 stmt_may_clobber_global_p (gimple stmt)
386 tree lhs;
388 if (!gimple_vdef (stmt))
389 return false;
391 /* ??? We can ask the oracle whether an artificial pointer
392 dereference with a pointer with points-to information covering
393 all global memory (what about non-address taken memory?) maybe
394 clobbered by this call. As there is at the moment no convenient
395 way of doing that without generating garbage do some manual
396 checking instead.
397 ??? We could make a NULL ao_ref argument to the various
398 predicates special, meaning any global memory. */
400 switch (gimple_code (stmt))
402 case GIMPLE_ASSIGN:
403 lhs = gimple_assign_lhs (stmt);
404 return (TREE_CODE (lhs) != SSA_NAME
405 && ref_may_alias_global_p (lhs));
406 case GIMPLE_CALL:
407 return true;
408 default:
409 return true;
414 /* Dump alias information on FILE. */
416 void
417 dump_alias_info (FILE *file)
419 unsigned i;
420 const char *funcname
421 = lang_hooks.decl_printable_name (current_function_decl, 2);
422 tree var;
424 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
426 fprintf (file, "Aliased symbols\n\n");
428 FOR_EACH_LOCAL_DECL (cfun, i, var)
430 if (may_be_aliased (var))
431 dump_variable (file, var);
434 fprintf (file, "\nCall clobber information\n");
436 fprintf (file, "\nESCAPED");
437 dump_points_to_solution (file, &cfun->gimple_df->escaped);
439 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
441 for (i = 1; i < num_ssa_names; i++)
443 tree ptr = ssa_name (i);
444 struct ptr_info_def *pi;
446 if (ptr == NULL_TREE
447 || !POINTER_TYPE_P (TREE_TYPE (ptr))
448 || SSA_NAME_IN_FREE_LIST (ptr))
449 continue;
451 pi = SSA_NAME_PTR_INFO (ptr);
452 if (pi)
453 dump_points_to_info_for (file, ptr);
456 fprintf (file, "\n");
460 /* Dump alias information on stderr. */
462 DEBUG_FUNCTION void
463 debug_alias_info (void)
465 dump_alias_info (stderr);
469 /* Dump the points-to set *PT into FILE. */
471 void
472 dump_points_to_solution (FILE *file, struct pt_solution *pt)
474 if (pt->anything)
475 fprintf (file, ", points-to anything");
477 if (pt->nonlocal)
478 fprintf (file, ", points-to non-local");
480 if (pt->escaped)
481 fprintf (file, ", points-to escaped");
483 if (pt->ipa_escaped)
484 fprintf (file, ", points-to unit escaped");
486 if (pt->null)
487 fprintf (file, ", points-to NULL");
489 if (pt->vars)
491 fprintf (file, ", points-to vars: ");
492 dump_decl_set (file, pt->vars);
493 if (pt->vars_contains_nonlocal
494 && pt->vars_contains_escaped_heap)
495 fprintf (file, " (nonlocal, escaped heap)");
496 else if (pt->vars_contains_nonlocal
497 && pt->vars_contains_escaped)
498 fprintf (file, " (nonlocal, escaped)");
499 else if (pt->vars_contains_nonlocal)
500 fprintf (file, " (nonlocal)");
501 else if (pt->vars_contains_escaped_heap)
502 fprintf (file, " (escaped heap)");
503 else if (pt->vars_contains_escaped)
504 fprintf (file, " (escaped)");
509 /* Unified dump function for pt_solution. */
511 DEBUG_FUNCTION void
512 debug (pt_solution &ref)
514 dump_points_to_solution (stderr, &ref);
517 DEBUG_FUNCTION void
518 debug (pt_solution *ptr)
520 if (ptr)
521 debug (*ptr);
522 else
523 fprintf (stderr, "<nil>\n");
527 /* Dump points-to information for SSA_NAME PTR into FILE. */
529 void
530 dump_points_to_info_for (FILE *file, tree ptr)
532 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
534 print_generic_expr (file, ptr, dump_flags);
536 if (pi)
537 dump_points_to_solution (file, &pi->pt);
538 else
539 fprintf (file, ", points-to anything");
541 fprintf (file, "\n");
545 /* Dump points-to information for VAR into stderr. */
547 DEBUG_FUNCTION void
548 debug_points_to_info_for (tree var)
550 dump_points_to_info_for (stderr, var);
554 /* Initializes the alias-oracle reference representation *R from REF. */
556 void
557 ao_ref_init (ao_ref *r, tree ref)
559 r->ref = ref;
560 r->base = NULL_TREE;
561 r->offset = 0;
562 r->size = -1;
563 r->max_size = -1;
564 r->ref_alias_set = -1;
565 r->base_alias_set = -1;
566 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
569 /* Returns the base object of the memory reference *REF. */
571 tree
572 ao_ref_base (ao_ref *ref)
574 if (ref->base)
575 return ref->base;
576 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
577 &ref->max_size);
578 return ref->base;
581 /* Returns the base object alias set of the memory reference *REF. */
583 alias_set_type
584 ao_ref_base_alias_set (ao_ref *ref)
586 tree base_ref;
587 if (ref->base_alias_set != -1)
588 return ref->base_alias_set;
589 if (!ref->ref)
590 return 0;
591 base_ref = ref->ref;
592 while (handled_component_p (base_ref))
593 base_ref = TREE_OPERAND (base_ref, 0);
594 ref->base_alias_set = get_alias_set (base_ref);
595 return ref->base_alias_set;
598 /* Returns the reference alias set of the memory reference *REF. */
600 alias_set_type
601 ao_ref_alias_set (ao_ref *ref)
603 if (ref->ref_alias_set != -1)
604 return ref->ref_alias_set;
605 ref->ref_alias_set = get_alias_set (ref->ref);
606 return ref->ref_alias_set;
609 /* Init an alias-oracle reference representation from a gimple pointer
610 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
611 size is assumed to be unknown. The access is assumed to be only
612 to or after of the pointer target, not before it. */
614 void
615 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
617 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
618 ref->ref = NULL_TREE;
619 if (TREE_CODE (ptr) == SSA_NAME)
621 gimple stmt = SSA_NAME_DEF_STMT (ptr);
622 if (gimple_assign_single_p (stmt)
623 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
624 ptr = gimple_assign_rhs1 (stmt);
625 else if (is_gimple_assign (stmt)
626 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
627 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
629 ptr = gimple_assign_rhs1 (stmt);
630 extra_offset = BITS_PER_UNIT
631 * int_cst_value (gimple_assign_rhs2 (stmt));
635 if (TREE_CODE (ptr) == ADDR_EXPR)
637 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
638 if (ref->base)
639 ref->offset = BITS_PER_UNIT * t;
640 else
642 size = NULL_TREE;
643 ref->offset = 0;
644 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
647 else
649 ref->base = build2 (MEM_REF, char_type_node,
650 ptr, null_pointer_node);
651 ref->offset = 0;
653 ref->offset += extra_offset;
654 if (size
655 && tree_fits_shwi_p (size)
656 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
657 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
658 else
659 ref->max_size = ref->size = -1;
660 ref->ref_alias_set = 0;
661 ref->base_alias_set = 0;
662 ref->volatile_p = false;
665 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
666 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
667 decide. */
669 static inline int
670 same_type_for_tbaa (tree type1, tree type2)
672 type1 = TYPE_MAIN_VARIANT (type1);
673 type2 = TYPE_MAIN_VARIANT (type2);
675 /* If we would have to do structural comparison bail out. */
676 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
677 || TYPE_STRUCTURAL_EQUALITY_P (type2))
678 return -1;
680 /* Compare the canonical types. */
681 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
682 return 1;
684 /* ??? Array types are not properly unified in all cases as we have
685 spurious changes in the index types for example. Removing this
686 causes all sorts of problems with the Fortran frontend. */
687 if (TREE_CODE (type1) == ARRAY_TYPE
688 && TREE_CODE (type2) == ARRAY_TYPE)
689 return -1;
691 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
692 object of one of its constrained subtypes, e.g. when a function with an
693 unconstrained parameter passed by reference is called on an object and
694 inlined. But, even in the case of a fixed size, type and subtypes are
695 not equivalent enough as to share the same TYPE_CANONICAL, since this
696 would mean that conversions between them are useless, whereas they are
697 not (e.g. type and subtypes can have different modes). So, in the end,
698 they are only guaranteed to have the same alias set. */
699 if (get_alias_set (type1) == get_alias_set (type2))
700 return -1;
702 /* The types are known to be not equal. */
703 return 0;
706 /* Determine if the two component references REF1 and REF2 which are
707 based on access types TYPE1 and TYPE2 and of which at least one is based
708 on an indirect reference may alias. REF2 is the only one that can
709 be a decl in which case REF2_IS_DECL is true.
710 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
711 are the respective alias sets. */
713 static bool
714 aliasing_component_refs_p (tree ref1,
715 alias_set_type ref1_alias_set,
716 alias_set_type base1_alias_set,
717 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
718 tree ref2,
719 alias_set_type ref2_alias_set,
720 alias_set_type base2_alias_set,
721 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
722 bool ref2_is_decl)
724 /* If one reference is a component references through pointers try to find a
725 common base and apply offset based disambiguation. This handles
726 for example
727 struct A { int i; int j; } *q;
728 struct B { struct A a; int k; } *p;
729 disambiguating q->i and p->a.j. */
730 tree base1, base2;
731 tree type1, type2;
732 tree *refp;
733 int same_p;
735 /* Choose bases and base types to search for. */
736 base1 = ref1;
737 while (handled_component_p (base1))
738 base1 = TREE_OPERAND (base1, 0);
739 type1 = TREE_TYPE (base1);
740 base2 = ref2;
741 while (handled_component_p (base2))
742 base2 = TREE_OPERAND (base2, 0);
743 type2 = TREE_TYPE (base2);
745 /* Now search for the type1 in the access path of ref2. This
746 would be a common base for doing offset based disambiguation on. */
747 refp = &ref2;
748 while (handled_component_p (*refp)
749 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
750 refp = &TREE_OPERAND (*refp, 0);
751 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
752 /* If we couldn't compare types we have to bail out. */
753 if (same_p == -1)
754 return true;
755 else if (same_p == 1)
757 HOST_WIDE_INT offadj, sztmp, msztmp;
758 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
759 offset2 -= offadj;
760 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
761 offset1 -= offadj;
762 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
764 /* If we didn't find a common base, try the other way around. */
765 refp = &ref1;
766 while (handled_component_p (*refp)
767 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
768 refp = &TREE_OPERAND (*refp, 0);
769 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
770 /* If we couldn't compare types we have to bail out. */
771 if (same_p == -1)
772 return true;
773 else if (same_p == 1)
775 HOST_WIDE_INT offadj, sztmp, msztmp;
776 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
777 offset1 -= offadj;
778 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
779 offset2 -= offadj;
780 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
783 /* If we have two type access paths B1.path1 and B2.path2 they may
784 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
785 But we can still have a path that goes B1.path1...B2.path2 with
786 a part that we do not see. So we can only disambiguate now
787 if there is no B2 in the tail of path1 and no B1 on the
788 tail of path2. */
789 if (base1_alias_set == ref2_alias_set
790 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
791 return true;
792 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
793 if (!ref2_is_decl)
794 return (base2_alias_set == ref1_alias_set
795 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
796 return false;
799 /* Return true if we can determine that component references REF1 and REF2,
800 that are within a common DECL, cannot overlap. */
802 static bool
803 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
805 auto_vec<tree, 16> component_refs1;
806 auto_vec<tree, 16> component_refs2;
808 /* Create the stack of handled components for REF1. */
809 while (handled_component_p (ref1))
811 component_refs1.safe_push (ref1);
812 ref1 = TREE_OPERAND (ref1, 0);
814 if (TREE_CODE (ref1) == MEM_REF)
816 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
817 goto may_overlap;
818 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
821 /* Create the stack of handled components for REF2. */
822 while (handled_component_p (ref2))
824 component_refs2.safe_push (ref2);
825 ref2 = TREE_OPERAND (ref2, 0);
827 if (TREE_CODE (ref2) == MEM_REF)
829 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
830 goto may_overlap;
831 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
834 /* We must have the same base DECL. */
835 gcc_assert (ref1 == ref2);
837 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
838 rank. This is sufficient because we start from the same DECL and you
839 cannot reference several fields at a time with COMPONENT_REFs (unlike
840 with ARRAY_RANGE_REFs for arrays) so you always need the same number
841 of them to access a sub-component, unless you're in a union, in which
842 case the return value will precisely be false. */
843 while (true)
847 if (component_refs1.is_empty ())
848 goto may_overlap;
849 ref1 = component_refs1.pop ();
851 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
855 if (component_refs2.is_empty ())
856 goto may_overlap;
857 ref2 = component_refs2.pop ();
859 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
861 /* Beware of BIT_FIELD_REF. */
862 if (TREE_CODE (ref1) != COMPONENT_REF
863 || TREE_CODE (ref2) != COMPONENT_REF)
864 goto may_overlap;
866 tree field1 = TREE_OPERAND (ref1, 1);
867 tree field2 = TREE_OPERAND (ref2, 1);
869 /* ??? We cannot simply use the type of operand #0 of the refs here
870 as the Fortran compiler smuggles type punning into COMPONENT_REFs
871 for common blocks instead of using unions like everyone else. */
872 tree type1 = DECL_CONTEXT (field1);
873 tree type2 = DECL_CONTEXT (field2);
875 /* We cannot disambiguate fields in a union or qualified union. */
876 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
877 goto may_overlap;
879 /* Different fields of the same record type cannot overlap.
880 ??? Bitfields can overlap at RTL level so punt on them. */
881 if (field1 != field2)
883 component_refs1.release ();
884 component_refs2.release ();
885 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
889 may_overlap:
890 component_refs1.release ();
891 component_refs2.release ();
892 return false;
895 /* qsort compare function to sort FIELD_DECLs after their
896 DECL_FIELD_CONTEXT TYPE_UID. */
898 static inline int
899 ncr_compar (const void *field1_, const void *field2_)
901 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
902 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
903 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
904 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
905 if (uid1 < uid2)
906 return -1;
907 else if (uid1 > uid2)
908 return 1;
909 return 0;
912 /* Return true if we can determine that the fields referenced cannot
913 overlap for any pair of objects. */
915 static bool
916 nonoverlapping_component_refs_p (const_tree x, const_tree y)
918 if (!flag_strict_aliasing
919 || !x || !y
920 || TREE_CODE (x) != COMPONENT_REF
921 || TREE_CODE (y) != COMPONENT_REF)
922 return false;
924 auto_vec<const_tree, 16> fieldsx;
925 while (TREE_CODE (x) == COMPONENT_REF)
927 tree field = TREE_OPERAND (x, 1);
928 tree type = DECL_FIELD_CONTEXT (field);
929 if (TREE_CODE (type) == RECORD_TYPE)
930 fieldsx.safe_push (field);
931 x = TREE_OPERAND (x, 0);
933 if (fieldsx.length () == 0)
934 return false;
935 auto_vec<const_tree, 16> fieldsy;
936 while (TREE_CODE (y) == COMPONENT_REF)
938 tree field = TREE_OPERAND (y, 1);
939 tree type = DECL_FIELD_CONTEXT (field);
940 if (TREE_CODE (type) == RECORD_TYPE)
941 fieldsy.safe_push (TREE_OPERAND (y, 1));
942 y = TREE_OPERAND (y, 0);
944 if (fieldsy.length () == 0)
945 return false;
947 /* Most common case first. */
948 if (fieldsx.length () == 1
949 && fieldsy.length () == 1)
950 return ((DECL_FIELD_CONTEXT (fieldsx[0])
951 == DECL_FIELD_CONTEXT (fieldsy[0]))
952 && fieldsx[0] != fieldsy[0]
953 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
955 if (fieldsx.length () == 2)
957 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
959 const_tree tem = fieldsx[0];
960 fieldsx[0] = fieldsx[1];
961 fieldsx[1] = tem;
964 else
965 fieldsx.qsort (ncr_compar);
967 if (fieldsy.length () == 2)
969 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
971 const_tree tem = fieldsy[0];
972 fieldsy[0] = fieldsy[1];
973 fieldsy[1] = tem;
976 else
977 fieldsy.qsort (ncr_compar);
979 unsigned i = 0, j = 0;
982 const_tree fieldx = fieldsx[i];
983 const_tree fieldy = fieldsy[j];
984 tree typex = DECL_FIELD_CONTEXT (fieldx);
985 tree typey = DECL_FIELD_CONTEXT (fieldy);
986 if (typex == typey)
988 /* We're left with accessing different fields of a structure,
989 no possible overlap, unless they are both bitfields. */
990 if (fieldx != fieldy)
991 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
993 if (TYPE_UID (typex) < TYPE_UID (typey))
995 i++;
996 if (i == fieldsx.length ())
997 break;
999 else
1001 j++;
1002 if (j == fieldsy.length ())
1003 break;
1006 while (1);
1008 return false;
1012 /* Return true if two memory references based on the variables BASE1
1013 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1014 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1015 if non-NULL are the complete memory reference trees. */
1017 static bool
1018 decl_refs_may_alias_p (tree ref1, tree base1,
1019 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1020 tree ref2, tree base2,
1021 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1023 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1025 /* If both references are based on different variables, they cannot alias. */
1026 if (base1 != base2)
1027 return false;
1029 /* If both references are based on the same variable, they cannot alias if
1030 the accesses do not overlap. */
1031 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1032 return false;
1034 /* For components with variable position, the above test isn't sufficient,
1035 so we disambiguate component references manually. */
1036 if (ref1 && ref2
1037 && handled_component_p (ref1) && handled_component_p (ref2)
1038 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1039 return false;
1041 return true;
1044 /* Return true if an indirect reference based on *PTR1 constrained
1045 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1046 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1047 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1048 in which case they are computed on-demand. REF1 and REF2
1049 if non-NULL are the complete memory reference trees. */
1051 static bool
1052 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1053 HOST_WIDE_INT offset1,
1054 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1055 alias_set_type ref1_alias_set,
1056 alias_set_type base1_alias_set,
1057 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1058 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1059 alias_set_type ref2_alias_set,
1060 alias_set_type base2_alias_set, bool tbaa_p)
1062 tree ptr1;
1063 tree ptrtype1, dbase2;
1064 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1065 HOST_WIDE_INT doffset1, doffset2;
1067 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1068 || TREE_CODE (base1) == TARGET_MEM_REF)
1069 && DECL_P (base2));
1071 ptr1 = TREE_OPERAND (base1, 0);
1073 /* The offset embedded in MEM_REFs can be negative. Bias them
1074 so that the resulting offset adjustment is positive. */
1075 offset_int moff = mem_ref_offset (base1);
1076 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1077 if (wi::neg_p (moff))
1078 offset2p += (-moff).to_short_addr ();
1079 else
1080 offset1p += moff.to_short_addr ();
1082 /* If only one reference is based on a variable, they cannot alias if
1083 the pointer access is beyond the extent of the variable access.
1084 (the pointer base cannot validly point to an offset less than zero
1085 of the variable).
1086 ??? IVOPTs creates bases that do not honor this restriction,
1087 so do not apply this optimization for TARGET_MEM_REFs. */
1088 if (TREE_CODE (base1) != TARGET_MEM_REF
1089 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1090 return false;
1091 /* They also cannot alias if the pointer may not point to the decl. */
1092 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1093 return false;
1095 /* Disambiguations that rely on strict aliasing rules follow. */
1096 if (!flag_strict_aliasing || !tbaa_p)
1097 return true;
1099 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1101 /* If the alias set for a pointer access is zero all bets are off. */
1102 if (base1_alias_set == -1)
1103 base1_alias_set = get_deref_alias_set (ptrtype1);
1104 if (base1_alias_set == 0)
1105 return true;
1106 if (base2_alias_set == -1)
1107 base2_alias_set = get_alias_set (base2);
1109 /* When we are trying to disambiguate an access with a pointer dereference
1110 as base versus one with a decl as base we can use both the size
1111 of the decl and its dynamic type for extra disambiguation.
1112 ??? We do not know anything about the dynamic type of the decl
1113 other than that its alias-set contains base2_alias_set as a subset
1114 which does not help us here. */
1115 /* As we know nothing useful about the dynamic type of the decl just
1116 use the usual conflict check rather than a subset test.
1117 ??? We could introduce -fvery-strict-aliasing when the language
1118 does not allow decls to have a dynamic type that differs from their
1119 static type. Then we can check
1120 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1121 if (base1_alias_set != base2_alias_set
1122 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1123 return false;
1124 /* If the size of the access relevant for TBAA through the pointer
1125 is bigger than the size of the decl we can't possibly access the
1126 decl via that pointer. */
1127 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1128 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1129 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1130 /* ??? This in turn may run afoul when a decl of type T which is
1131 a member of union type U is accessed through a pointer to
1132 type U and sizeof T is smaller than sizeof U. */
1133 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1134 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1135 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1136 return false;
1138 if (!ref2)
1139 return true;
1141 /* If the decl is accessed via a MEM_REF, reconstruct the base
1142 we can use for TBAA and an appropriately adjusted offset. */
1143 dbase2 = ref2;
1144 while (handled_component_p (dbase2))
1145 dbase2 = TREE_OPERAND (dbase2, 0);
1146 doffset1 = offset1;
1147 doffset2 = offset2;
1148 if (TREE_CODE (dbase2) == MEM_REF
1149 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1151 offset_int moff = mem_ref_offset (dbase2);
1152 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1153 if (wi::neg_p (moff))
1154 doffset1 -= (-moff).to_short_addr ();
1155 else
1156 doffset2 -= moff.to_short_addr ();
1159 /* If either reference is view-converted, give up now. */
1160 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1161 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1162 return true;
1164 /* If both references are through the same type, they do not alias
1165 if the accesses do not overlap. This does extra disambiguation
1166 for mixed/pointer accesses but requires strict aliasing.
1167 For MEM_REFs we require that the component-ref offset we computed
1168 is relative to the start of the type which we ensure by
1169 comparing rvalue and access type and disregarding the constant
1170 pointer offset. */
1171 if ((TREE_CODE (base1) != TARGET_MEM_REF
1172 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1173 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1174 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1176 if (ref1 && ref2
1177 && nonoverlapping_component_refs_p (ref1, ref2))
1178 return false;
1180 /* Do access-path based disambiguation. */
1181 if (ref1 && ref2
1182 && (handled_component_p (ref1) || handled_component_p (ref2)))
1183 return aliasing_component_refs_p (ref1,
1184 ref1_alias_set, base1_alias_set,
1185 offset1, max_size1,
1186 ref2,
1187 ref2_alias_set, base2_alias_set,
1188 offset2, max_size2, true);
1190 return true;
1193 /* Return true if two indirect references based on *PTR1
1194 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1195 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1196 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1197 in which case they are computed on-demand. REF1 and REF2
1198 if non-NULL are the complete memory reference trees. */
1200 static bool
1201 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1202 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1203 alias_set_type ref1_alias_set,
1204 alias_set_type base1_alias_set,
1205 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1206 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1207 alias_set_type ref2_alias_set,
1208 alias_set_type base2_alias_set, bool tbaa_p)
1210 tree ptr1;
1211 tree ptr2;
1212 tree ptrtype1, ptrtype2;
1214 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1215 || TREE_CODE (base1) == TARGET_MEM_REF)
1216 && (TREE_CODE (base2) == MEM_REF
1217 || TREE_CODE (base2) == TARGET_MEM_REF));
1219 ptr1 = TREE_OPERAND (base1, 0);
1220 ptr2 = TREE_OPERAND (base2, 0);
1222 /* If both bases are based on pointers they cannot alias if they may not
1223 point to the same memory object or if they point to the same object
1224 and the accesses do not overlap. */
1225 if ((!cfun || gimple_in_ssa_p (cfun))
1226 && operand_equal_p (ptr1, ptr2, 0)
1227 && (((TREE_CODE (base1) != TARGET_MEM_REF
1228 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1229 && (TREE_CODE (base2) != TARGET_MEM_REF
1230 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1231 || (TREE_CODE (base1) == TARGET_MEM_REF
1232 && TREE_CODE (base2) == TARGET_MEM_REF
1233 && (TMR_STEP (base1) == TMR_STEP (base2)
1234 || (TMR_STEP (base1) && TMR_STEP (base2)
1235 && operand_equal_p (TMR_STEP (base1),
1236 TMR_STEP (base2), 0)))
1237 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1238 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1239 && operand_equal_p (TMR_INDEX (base1),
1240 TMR_INDEX (base2), 0)))
1241 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1242 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1243 && operand_equal_p (TMR_INDEX2 (base1),
1244 TMR_INDEX2 (base2), 0))))))
1246 offset_int moff;
1247 /* The offset embedded in MEM_REFs can be negative. Bias them
1248 so that the resulting offset adjustment is positive. */
1249 moff = mem_ref_offset (base1);
1250 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1251 if (wi::neg_p (moff))
1252 offset2 += (-moff).to_short_addr ();
1253 else
1254 offset1 += moff.to_shwi ();
1255 moff = mem_ref_offset (base2);
1256 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1257 if (wi::neg_p (moff))
1258 offset1 += (-moff).to_short_addr ();
1259 else
1260 offset2 += moff.to_short_addr ();
1261 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1263 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1264 return false;
1266 /* Disambiguations that rely on strict aliasing rules follow. */
1267 if (!flag_strict_aliasing || !tbaa_p)
1268 return true;
1270 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1271 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1273 /* If the alias set for a pointer access is zero all bets are off. */
1274 if (base1_alias_set == -1)
1275 base1_alias_set = get_deref_alias_set (ptrtype1);
1276 if (base1_alias_set == 0)
1277 return true;
1278 if (base2_alias_set == -1)
1279 base2_alias_set = get_deref_alias_set (ptrtype2);
1280 if (base2_alias_set == 0)
1281 return true;
1283 /* If both references are through the same type, they do not alias
1284 if the accesses do not overlap. This does extra disambiguation
1285 for mixed/pointer accesses but requires strict aliasing. */
1286 if ((TREE_CODE (base1) != TARGET_MEM_REF
1287 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1288 && (TREE_CODE (base2) != TARGET_MEM_REF
1289 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1290 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1291 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1292 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1293 TREE_TYPE (ptrtype2)) == 1)
1294 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1296 /* Do type-based disambiguation. */
1297 if (base1_alias_set != base2_alias_set
1298 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1299 return false;
1301 /* If either reference is view-converted, give up now. */
1302 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1303 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1304 return true;
1306 if (ref1 && ref2
1307 && nonoverlapping_component_refs_p (ref1, ref2))
1308 return false;
1310 /* Do access-path based disambiguation. */
1311 if (ref1 && ref2
1312 && (handled_component_p (ref1) || handled_component_p (ref2)))
1313 return aliasing_component_refs_p (ref1,
1314 ref1_alias_set, base1_alias_set,
1315 offset1, max_size1,
1316 ref2,
1317 ref2_alias_set, base2_alias_set,
1318 offset2, max_size2, false);
1320 return true;
1323 /* Return true, if the two memory references REF1 and REF2 may alias. */
1325 bool
1326 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1328 tree base1, base2;
1329 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1330 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1331 bool var1_p, var2_p, ind1_p, ind2_p;
1333 gcc_checking_assert ((!ref1->ref
1334 || TREE_CODE (ref1->ref) == SSA_NAME
1335 || DECL_P (ref1->ref)
1336 || TREE_CODE (ref1->ref) == STRING_CST
1337 || handled_component_p (ref1->ref)
1338 || TREE_CODE (ref1->ref) == MEM_REF
1339 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1340 && (!ref2->ref
1341 || TREE_CODE (ref2->ref) == SSA_NAME
1342 || DECL_P (ref2->ref)
1343 || TREE_CODE (ref2->ref) == STRING_CST
1344 || handled_component_p (ref2->ref)
1345 || TREE_CODE (ref2->ref) == MEM_REF
1346 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1348 /* Decompose the references into their base objects and the access. */
1349 base1 = ao_ref_base (ref1);
1350 offset1 = ref1->offset;
1351 max_size1 = ref1->max_size;
1352 base2 = ao_ref_base (ref2);
1353 offset2 = ref2->offset;
1354 max_size2 = ref2->max_size;
1356 /* We can end up with registers or constants as bases for example from
1357 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1358 which is seen as a struct copy. */
1359 if (TREE_CODE (base1) == SSA_NAME
1360 || TREE_CODE (base1) == CONST_DECL
1361 || TREE_CODE (base1) == CONSTRUCTOR
1362 || TREE_CODE (base1) == ADDR_EXPR
1363 || CONSTANT_CLASS_P (base1)
1364 || TREE_CODE (base2) == SSA_NAME
1365 || TREE_CODE (base2) == CONST_DECL
1366 || TREE_CODE (base2) == CONSTRUCTOR
1367 || TREE_CODE (base2) == ADDR_EXPR
1368 || CONSTANT_CLASS_P (base2))
1369 return false;
1371 /* We can end up referring to code via function and label decls.
1372 As we likely do not properly track code aliases conservatively
1373 bail out. */
1374 if (TREE_CODE (base1) == FUNCTION_DECL
1375 || TREE_CODE (base1) == LABEL_DECL
1376 || TREE_CODE (base2) == FUNCTION_DECL
1377 || TREE_CODE (base2) == LABEL_DECL)
1378 return true;
1380 /* Two volatile accesses always conflict. */
1381 if (ref1->volatile_p
1382 && ref2->volatile_p)
1383 return true;
1385 /* Defer to simple offset based disambiguation if we have
1386 references based on two decls. Do this before defering to
1387 TBAA to handle must-alias cases in conformance with the
1388 GCC extension of allowing type-punning through unions. */
1389 var1_p = DECL_P (base1);
1390 var2_p = DECL_P (base2);
1391 if (var1_p && var2_p)
1392 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1393 ref2->ref, base2, offset2, max_size2);
1395 /* Handle restrict based accesses.
1396 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1397 here. */
1398 tree rbase1 = base1;
1399 tree rbase2 = base2;
1400 if (var1_p)
1402 rbase1 = ref1->ref;
1403 if (rbase1)
1404 while (handled_component_p (rbase1))
1405 rbase1 = TREE_OPERAND (rbase1, 0);
1407 if (var2_p)
1409 rbase2 = ref2->ref;
1410 if (rbase2)
1411 while (handled_component_p (rbase2))
1412 rbase2 = TREE_OPERAND (rbase2, 0);
1414 if (rbase1 && rbase2
1415 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1416 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1417 /* If the accesses are in the same restrict clique... */
1418 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1419 /* But based on different pointers they do not alias. */
1420 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1421 return false;
1423 ind1_p = (TREE_CODE (base1) == MEM_REF
1424 || TREE_CODE (base1) == TARGET_MEM_REF);
1425 ind2_p = (TREE_CODE (base2) == MEM_REF
1426 || TREE_CODE (base2) == TARGET_MEM_REF);
1428 /* Canonicalize the pointer-vs-decl case. */
1429 if (ind1_p && var2_p)
1431 HOST_WIDE_INT tmp1;
1432 tree tmp2;
1433 ao_ref *tmp3;
1434 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1435 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1436 tmp2 = base1; base1 = base2; base2 = tmp2;
1437 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1438 var1_p = true;
1439 ind1_p = false;
1440 var2_p = false;
1441 ind2_p = true;
1444 /* First defer to TBAA if possible. */
1445 if (tbaa_p
1446 && flag_strict_aliasing
1447 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1448 ao_ref_alias_set (ref2)))
1449 return false;
1451 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1452 if (var1_p && ind2_p)
1453 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1454 offset2, max_size2,
1455 ao_ref_alias_set (ref2), -1,
1456 ref1->ref, base1,
1457 offset1, max_size1,
1458 ao_ref_alias_set (ref1),
1459 ao_ref_base_alias_set (ref1),
1460 tbaa_p);
1461 else if (ind1_p && ind2_p)
1462 return indirect_refs_may_alias_p (ref1->ref, base1,
1463 offset1, max_size1,
1464 ao_ref_alias_set (ref1), -1,
1465 ref2->ref, base2,
1466 offset2, max_size2,
1467 ao_ref_alias_set (ref2), -1,
1468 tbaa_p);
1470 /* We really do not want to end up here, but returning true is safe. */
1471 #ifdef ENABLE_CHECKING
1472 gcc_unreachable ();
1473 #else
1474 return true;
1475 #endif
1478 static bool
1479 refs_may_alias_p (tree ref1, ao_ref *ref2)
1481 ao_ref r1;
1482 ao_ref_init (&r1, ref1);
1483 return refs_may_alias_p_1 (&r1, ref2, true);
1486 bool
1487 refs_may_alias_p (tree ref1, tree ref2)
1489 ao_ref r1, r2;
1490 bool res;
1491 ao_ref_init (&r1, ref1);
1492 ao_ref_init (&r2, ref2);
1493 res = refs_may_alias_p_1 (&r1, &r2, true);
1494 if (res)
1495 ++alias_stats.refs_may_alias_p_may_alias;
1496 else
1497 ++alias_stats.refs_may_alias_p_no_alias;
1498 return res;
1501 /* Returns true if there is a anti-dependence for the STORE that
1502 executes after the LOAD. */
1504 bool
1505 refs_anti_dependent_p (tree load, tree store)
1507 ao_ref r1, r2;
1508 ao_ref_init (&r1, load);
1509 ao_ref_init (&r2, store);
1510 return refs_may_alias_p_1 (&r1, &r2, false);
1513 /* Returns true if there is a output dependence for the stores
1514 STORE1 and STORE2. */
1516 bool
1517 refs_output_dependent_p (tree store1, tree store2)
1519 ao_ref r1, r2;
1520 ao_ref_init (&r1, store1);
1521 ao_ref_init (&r2, store2);
1522 return refs_may_alias_p_1 (&r1, &r2, false);
1525 /* If the call CALL may use the memory reference REF return true,
1526 otherwise return false. */
1528 static bool
1529 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1531 tree base, callee;
1532 unsigned i;
1533 int flags = gimple_call_flags (call);
1535 /* Const functions without a static chain do not implicitly use memory. */
1536 if (!gimple_call_chain (call)
1537 && (flags & (ECF_CONST|ECF_NOVOPS)))
1538 goto process_args;
1540 base = ao_ref_base (ref);
1541 if (!base)
1542 return true;
1544 /* A call that is not without side-effects might involve volatile
1545 accesses and thus conflicts with all other volatile accesses. */
1546 if (ref->volatile_p)
1547 return true;
1549 /* If the reference is based on a decl that is not aliased the call
1550 cannot possibly use it. */
1551 if (DECL_P (base)
1552 && !may_be_aliased (base)
1553 /* But local statics can be used through recursion. */
1554 && !is_global_var (base))
1555 goto process_args;
1557 callee = gimple_call_fndecl (call);
1559 /* Handle those builtin functions explicitly that do not act as
1560 escape points. See tree-ssa-structalias.c:find_func_aliases
1561 for the list of builtins we might need to handle here. */
1562 if (callee != NULL_TREE
1563 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1564 switch (DECL_FUNCTION_CODE (callee))
1566 /* All the following functions read memory pointed to by
1567 their second argument. strcat/strncat additionally
1568 reads memory pointed to by the first argument. */
1569 case BUILT_IN_STRCAT:
1570 case BUILT_IN_STRNCAT:
1572 ao_ref dref;
1573 ao_ref_init_from_ptr_and_size (&dref,
1574 gimple_call_arg (call, 0),
1575 NULL_TREE);
1576 if (refs_may_alias_p_1 (&dref, ref, false))
1577 return true;
1579 /* FALLTHRU */
1580 case BUILT_IN_STRCPY:
1581 case BUILT_IN_STRNCPY:
1582 case BUILT_IN_MEMCPY:
1583 case BUILT_IN_MEMMOVE:
1584 case BUILT_IN_MEMPCPY:
1585 case BUILT_IN_STPCPY:
1586 case BUILT_IN_STPNCPY:
1587 case BUILT_IN_TM_MEMCPY:
1588 case BUILT_IN_TM_MEMMOVE:
1590 ao_ref dref;
1591 tree size = NULL_TREE;
1592 if (gimple_call_num_args (call) == 3)
1593 size = gimple_call_arg (call, 2);
1594 ao_ref_init_from_ptr_and_size (&dref,
1595 gimple_call_arg (call, 1),
1596 size);
1597 return refs_may_alias_p_1 (&dref, ref, false);
1599 case BUILT_IN_STRCAT_CHK:
1600 case BUILT_IN_STRNCAT_CHK:
1602 ao_ref dref;
1603 ao_ref_init_from_ptr_and_size (&dref,
1604 gimple_call_arg (call, 0),
1605 NULL_TREE);
1606 if (refs_may_alias_p_1 (&dref, ref, false))
1607 return true;
1609 /* FALLTHRU */
1610 case BUILT_IN_STRCPY_CHK:
1611 case BUILT_IN_STRNCPY_CHK:
1612 case BUILT_IN_MEMCPY_CHK:
1613 case BUILT_IN_MEMMOVE_CHK:
1614 case BUILT_IN_MEMPCPY_CHK:
1615 case BUILT_IN_STPCPY_CHK:
1616 case BUILT_IN_STPNCPY_CHK:
1618 ao_ref dref;
1619 tree size = NULL_TREE;
1620 if (gimple_call_num_args (call) == 4)
1621 size = gimple_call_arg (call, 2);
1622 ao_ref_init_from_ptr_and_size (&dref,
1623 gimple_call_arg (call, 1),
1624 size);
1625 return refs_may_alias_p_1 (&dref, ref, false);
1627 case BUILT_IN_BCOPY:
1629 ao_ref dref;
1630 tree size = gimple_call_arg (call, 2);
1631 ao_ref_init_from_ptr_and_size (&dref,
1632 gimple_call_arg (call, 0),
1633 size);
1634 return refs_may_alias_p_1 (&dref, ref, false);
1637 /* The following functions read memory pointed to by their
1638 first argument. */
1639 CASE_BUILT_IN_TM_LOAD (1):
1640 CASE_BUILT_IN_TM_LOAD (2):
1641 CASE_BUILT_IN_TM_LOAD (4):
1642 CASE_BUILT_IN_TM_LOAD (8):
1643 CASE_BUILT_IN_TM_LOAD (FLOAT):
1644 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1645 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1646 CASE_BUILT_IN_TM_LOAD (M64):
1647 CASE_BUILT_IN_TM_LOAD (M128):
1648 CASE_BUILT_IN_TM_LOAD (M256):
1649 case BUILT_IN_TM_LOG:
1650 case BUILT_IN_TM_LOG_1:
1651 case BUILT_IN_TM_LOG_2:
1652 case BUILT_IN_TM_LOG_4:
1653 case BUILT_IN_TM_LOG_8:
1654 case BUILT_IN_TM_LOG_FLOAT:
1655 case BUILT_IN_TM_LOG_DOUBLE:
1656 case BUILT_IN_TM_LOG_LDOUBLE:
1657 case BUILT_IN_TM_LOG_M64:
1658 case BUILT_IN_TM_LOG_M128:
1659 case BUILT_IN_TM_LOG_M256:
1660 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1662 /* These read memory pointed to by the first argument. */
1663 case BUILT_IN_STRDUP:
1664 case BUILT_IN_STRNDUP:
1665 case BUILT_IN_REALLOC:
1667 ao_ref dref;
1668 tree size = NULL_TREE;
1669 if (gimple_call_num_args (call) == 2)
1670 size = gimple_call_arg (call, 1);
1671 ao_ref_init_from_ptr_and_size (&dref,
1672 gimple_call_arg (call, 0),
1673 size);
1674 return refs_may_alias_p_1 (&dref, ref, false);
1676 /* These read memory pointed to by the first argument. */
1677 case BUILT_IN_INDEX:
1678 case BUILT_IN_STRCHR:
1679 case BUILT_IN_STRRCHR:
1681 ao_ref dref;
1682 ao_ref_init_from_ptr_and_size (&dref,
1683 gimple_call_arg (call, 0),
1684 NULL_TREE);
1685 return refs_may_alias_p_1 (&dref, ref, false);
1687 /* These read memory pointed to by the first argument with size
1688 in the third argument. */
1689 case BUILT_IN_MEMCHR:
1691 ao_ref dref;
1692 ao_ref_init_from_ptr_and_size (&dref,
1693 gimple_call_arg (call, 0),
1694 gimple_call_arg (call, 2));
1695 return refs_may_alias_p_1 (&dref, ref, false);
1697 /* These read memory pointed to by the first and second arguments. */
1698 case BUILT_IN_STRSTR:
1699 case BUILT_IN_STRPBRK:
1701 ao_ref dref;
1702 ao_ref_init_from_ptr_and_size (&dref,
1703 gimple_call_arg (call, 0),
1704 NULL_TREE);
1705 if (refs_may_alias_p_1 (&dref, ref, false))
1706 return true;
1707 ao_ref_init_from_ptr_and_size (&dref,
1708 gimple_call_arg (call, 1),
1709 NULL_TREE);
1710 return refs_may_alias_p_1 (&dref, ref, false);
1713 /* The following builtins do not read from memory. */
1714 case BUILT_IN_FREE:
1715 case BUILT_IN_MALLOC:
1716 case BUILT_IN_POSIX_MEMALIGN:
1717 case BUILT_IN_ALIGNED_ALLOC:
1718 case BUILT_IN_CALLOC:
1719 case BUILT_IN_ALLOCA:
1720 case BUILT_IN_ALLOCA_WITH_ALIGN:
1721 case BUILT_IN_STACK_SAVE:
1722 case BUILT_IN_STACK_RESTORE:
1723 case BUILT_IN_MEMSET:
1724 case BUILT_IN_TM_MEMSET:
1725 case BUILT_IN_MEMSET_CHK:
1726 case BUILT_IN_FREXP:
1727 case BUILT_IN_FREXPF:
1728 case BUILT_IN_FREXPL:
1729 case BUILT_IN_GAMMA_R:
1730 case BUILT_IN_GAMMAF_R:
1731 case BUILT_IN_GAMMAL_R:
1732 case BUILT_IN_LGAMMA_R:
1733 case BUILT_IN_LGAMMAF_R:
1734 case BUILT_IN_LGAMMAL_R:
1735 case BUILT_IN_MODF:
1736 case BUILT_IN_MODFF:
1737 case BUILT_IN_MODFL:
1738 case BUILT_IN_REMQUO:
1739 case BUILT_IN_REMQUOF:
1740 case BUILT_IN_REMQUOL:
1741 case BUILT_IN_SINCOS:
1742 case BUILT_IN_SINCOSF:
1743 case BUILT_IN_SINCOSL:
1744 case BUILT_IN_ASSUME_ALIGNED:
1745 case BUILT_IN_VA_END:
1746 return false;
1747 /* __sync_* builtins and some OpenMP builtins act as threading
1748 barriers. */
1749 #undef DEF_SYNC_BUILTIN
1750 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1751 #include "sync-builtins.def"
1752 #undef DEF_SYNC_BUILTIN
1753 case BUILT_IN_GOMP_ATOMIC_START:
1754 case BUILT_IN_GOMP_ATOMIC_END:
1755 case BUILT_IN_GOMP_BARRIER:
1756 case BUILT_IN_GOMP_BARRIER_CANCEL:
1757 case BUILT_IN_GOMP_TASKWAIT:
1758 case BUILT_IN_GOMP_TASKGROUP_END:
1759 case BUILT_IN_GOMP_CRITICAL_START:
1760 case BUILT_IN_GOMP_CRITICAL_END:
1761 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1762 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1763 case BUILT_IN_GOMP_LOOP_END:
1764 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1765 case BUILT_IN_GOMP_ORDERED_START:
1766 case BUILT_IN_GOMP_ORDERED_END:
1767 case BUILT_IN_GOMP_SECTIONS_END:
1768 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1769 case BUILT_IN_GOMP_SINGLE_COPY_START:
1770 case BUILT_IN_GOMP_SINGLE_COPY_END:
1771 return true;
1773 default:
1774 /* Fallthru to general call handling. */;
1777 /* Check if base is a global static variable that is not read
1778 by the function. */
1779 if (callee != NULL_TREE
1780 && TREE_CODE (base) == VAR_DECL
1781 && TREE_STATIC (base))
1783 struct cgraph_node *node = cgraph_node::get (callee);
1784 bitmap not_read;
1786 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1787 node yet. We should enforce that there are nodes for all decls in the
1788 IL and remove this check instead. */
1789 if (node
1790 && (not_read = ipa_reference_get_not_read_global (node))
1791 && bitmap_bit_p (not_read, DECL_UID (base)))
1792 goto process_args;
1795 /* Check if the base variable is call-used. */
1796 if (DECL_P (base))
1798 if (pt_solution_includes (gimple_call_use_set (call), base))
1799 return true;
1801 else if ((TREE_CODE (base) == MEM_REF
1802 || TREE_CODE (base) == TARGET_MEM_REF)
1803 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1805 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1806 if (!pi)
1807 return true;
1809 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1810 return true;
1812 else
1813 return true;
1815 /* Inspect call arguments for passed-by-value aliases. */
1816 process_args:
1817 for (i = 0; i < gimple_call_num_args (call); ++i)
1819 tree op = gimple_call_arg (call, i);
1820 int flags = gimple_call_arg_flags (call, i);
1822 if (flags & EAF_UNUSED)
1823 continue;
1825 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1826 op = TREE_OPERAND (op, 0);
1828 if (TREE_CODE (op) != SSA_NAME
1829 && !is_gimple_min_invariant (op))
1831 ao_ref r;
1832 ao_ref_init (&r, op);
1833 if (refs_may_alias_p_1 (&r, ref, true))
1834 return true;
1838 return false;
1841 static bool
1842 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref)
1844 bool res;
1845 res = ref_maybe_used_by_call_p_1 (call, ref);
1846 if (res)
1847 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1848 else
1849 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1850 return res;
1854 /* If the statement STMT may use the memory reference REF return
1855 true, otherwise return false. */
1857 bool
1858 ref_maybe_used_by_stmt_p (gimple stmt, ao_ref *ref)
1860 if (is_gimple_assign (stmt))
1862 tree rhs;
1864 /* All memory assign statements are single. */
1865 if (!gimple_assign_single_p (stmt))
1866 return false;
1868 rhs = gimple_assign_rhs1 (stmt);
1869 if (is_gimple_reg (rhs)
1870 || is_gimple_min_invariant (rhs)
1871 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1872 return false;
1874 return refs_may_alias_p (rhs, ref);
1876 else if (is_gimple_call (stmt))
1877 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref);
1878 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1880 tree retval = gimple_return_retval (return_stmt);
1881 if (retval
1882 && TREE_CODE (retval) != SSA_NAME
1883 && !is_gimple_min_invariant (retval)
1884 && refs_may_alias_p (retval, ref))
1885 return true;
1886 /* If ref escapes the function then the return acts as a use. */
1887 tree base = ao_ref_base (ref);
1888 if (!base)
1890 else if (DECL_P (base))
1891 return is_global_var (base);
1892 else if (TREE_CODE (base) == MEM_REF
1893 || TREE_CODE (base) == TARGET_MEM_REF)
1894 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1895 return false;
1898 return true;
1901 bool
1902 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1904 ao_ref r;
1905 ao_ref_init (&r, ref);
1906 return ref_maybe_used_by_stmt_p (stmt, &r);
1909 /* If the call in statement CALL may clobber the memory reference REF
1910 return true, otherwise return false. */
1912 bool
1913 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
1915 tree base;
1916 tree callee;
1918 /* If the call is pure or const it cannot clobber anything. */
1919 if (gimple_call_flags (call)
1920 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1921 return false;
1922 if (gimple_call_internal_p (call))
1923 switch (gimple_call_internal_fn (call))
1925 /* Treat these internal calls like ECF_PURE for aliasing,
1926 they don't write to any memory the program should care about.
1927 They have important other side-effects, and read memory,
1928 so can't be ECF_NOVOPS. */
1929 case IFN_UBSAN_NULL:
1930 case IFN_UBSAN_BOUNDS:
1931 case IFN_UBSAN_VPTR:
1932 case IFN_UBSAN_OBJECT_SIZE:
1933 case IFN_ASAN_CHECK:
1934 return false;
1935 default:
1936 break;
1939 base = ao_ref_base (ref);
1940 if (!base)
1941 return true;
1943 if (TREE_CODE (base) == SSA_NAME
1944 || CONSTANT_CLASS_P (base))
1945 return false;
1947 /* A call that is not without side-effects might involve volatile
1948 accesses and thus conflicts with all other volatile accesses. */
1949 if (ref->volatile_p)
1950 return true;
1952 /* If the reference is based on a decl that is not aliased the call
1953 cannot possibly clobber it. */
1954 if (DECL_P (base)
1955 && !may_be_aliased (base)
1956 /* But local non-readonly statics can be modified through recursion
1957 or the call may implement a threading barrier which we must
1958 treat as may-def. */
1959 && (TREE_READONLY (base)
1960 || !is_global_var (base)))
1961 return false;
1963 callee = gimple_call_fndecl (call);
1965 /* Handle those builtin functions explicitly that do not act as
1966 escape points. See tree-ssa-structalias.c:find_func_aliases
1967 for the list of builtins we might need to handle here. */
1968 if (callee != NULL_TREE
1969 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1970 switch (DECL_FUNCTION_CODE (callee))
1972 /* All the following functions clobber memory pointed to by
1973 their first argument. */
1974 case BUILT_IN_STRCPY:
1975 case BUILT_IN_STRNCPY:
1976 case BUILT_IN_MEMCPY:
1977 case BUILT_IN_MEMMOVE:
1978 case BUILT_IN_MEMPCPY:
1979 case BUILT_IN_STPCPY:
1980 case BUILT_IN_STPNCPY:
1981 case BUILT_IN_STRCAT:
1982 case BUILT_IN_STRNCAT:
1983 case BUILT_IN_MEMSET:
1984 case BUILT_IN_TM_MEMSET:
1985 CASE_BUILT_IN_TM_STORE (1):
1986 CASE_BUILT_IN_TM_STORE (2):
1987 CASE_BUILT_IN_TM_STORE (4):
1988 CASE_BUILT_IN_TM_STORE (8):
1989 CASE_BUILT_IN_TM_STORE (FLOAT):
1990 CASE_BUILT_IN_TM_STORE (DOUBLE):
1991 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1992 CASE_BUILT_IN_TM_STORE (M64):
1993 CASE_BUILT_IN_TM_STORE (M128):
1994 CASE_BUILT_IN_TM_STORE (M256):
1995 case BUILT_IN_TM_MEMCPY:
1996 case BUILT_IN_TM_MEMMOVE:
1998 ao_ref dref;
1999 tree size = NULL_TREE;
2000 /* Don't pass in size for strncat, as the maximum size
2001 is strlen (dest) + n + 1 instead of n, resp.
2002 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2003 known. */
2004 if (gimple_call_num_args (call) == 3
2005 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2006 size = gimple_call_arg (call, 2);
2007 ao_ref_init_from_ptr_and_size (&dref,
2008 gimple_call_arg (call, 0),
2009 size);
2010 return refs_may_alias_p_1 (&dref, ref, false);
2012 case BUILT_IN_STRCPY_CHK:
2013 case BUILT_IN_STRNCPY_CHK:
2014 case BUILT_IN_MEMCPY_CHK:
2015 case BUILT_IN_MEMMOVE_CHK:
2016 case BUILT_IN_MEMPCPY_CHK:
2017 case BUILT_IN_STPCPY_CHK:
2018 case BUILT_IN_STPNCPY_CHK:
2019 case BUILT_IN_STRCAT_CHK:
2020 case BUILT_IN_STRNCAT_CHK:
2021 case BUILT_IN_MEMSET_CHK:
2023 ao_ref dref;
2024 tree size = NULL_TREE;
2025 /* Don't pass in size for __strncat_chk, as the maximum size
2026 is strlen (dest) + n + 1 instead of n, resp.
2027 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2028 known. */
2029 if (gimple_call_num_args (call) == 4
2030 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2031 size = gimple_call_arg (call, 2);
2032 ao_ref_init_from_ptr_and_size (&dref,
2033 gimple_call_arg (call, 0),
2034 size);
2035 return refs_may_alias_p_1 (&dref, ref, false);
2037 case BUILT_IN_BCOPY:
2039 ao_ref dref;
2040 tree size = gimple_call_arg (call, 2);
2041 ao_ref_init_from_ptr_and_size (&dref,
2042 gimple_call_arg (call, 1),
2043 size);
2044 return refs_may_alias_p_1 (&dref, ref, false);
2046 /* Allocating memory does not have any side-effects apart from
2047 being the definition point for the pointer. */
2048 case BUILT_IN_MALLOC:
2049 case BUILT_IN_ALIGNED_ALLOC:
2050 case BUILT_IN_CALLOC:
2051 case BUILT_IN_STRDUP:
2052 case BUILT_IN_STRNDUP:
2053 /* Unix98 specifies that errno is set on allocation failure. */
2054 if (flag_errno_math
2055 && targetm.ref_may_alias_errno (ref))
2056 return true;
2057 return false;
2058 case BUILT_IN_STACK_SAVE:
2059 case BUILT_IN_ALLOCA:
2060 case BUILT_IN_ALLOCA_WITH_ALIGN:
2061 case BUILT_IN_ASSUME_ALIGNED:
2062 return false;
2063 /* But posix_memalign stores a pointer into the memory pointed to
2064 by its first argument. */
2065 case BUILT_IN_POSIX_MEMALIGN:
2067 tree ptrptr = gimple_call_arg (call, 0);
2068 ao_ref dref;
2069 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2070 TYPE_SIZE_UNIT (ptr_type_node));
2071 return (refs_may_alias_p_1 (&dref, ref, false)
2072 || (flag_errno_math
2073 && targetm.ref_may_alias_errno (ref)));
2075 /* Freeing memory kills the pointed-to memory. More importantly
2076 the call has to serve as a barrier for moving loads and stores
2077 across it. */
2078 case BUILT_IN_FREE:
2079 case BUILT_IN_VA_END:
2081 tree ptr = gimple_call_arg (call, 0);
2082 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2084 /* Realloc serves both as allocation point and deallocation point. */
2085 case BUILT_IN_REALLOC:
2087 tree ptr = gimple_call_arg (call, 0);
2088 /* Unix98 specifies that errno is set on allocation failure. */
2089 return ((flag_errno_math
2090 && targetm.ref_may_alias_errno (ref))
2091 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2093 case BUILT_IN_GAMMA_R:
2094 case BUILT_IN_GAMMAF_R:
2095 case BUILT_IN_GAMMAL_R:
2096 case BUILT_IN_LGAMMA_R:
2097 case BUILT_IN_LGAMMAF_R:
2098 case BUILT_IN_LGAMMAL_R:
2100 tree out = gimple_call_arg (call, 1);
2101 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2102 return true;
2103 if (flag_errno_math)
2104 break;
2105 return false;
2107 case BUILT_IN_FREXP:
2108 case BUILT_IN_FREXPF:
2109 case BUILT_IN_FREXPL:
2110 case BUILT_IN_MODF:
2111 case BUILT_IN_MODFF:
2112 case BUILT_IN_MODFL:
2114 tree out = gimple_call_arg (call, 1);
2115 return ptr_deref_may_alias_ref_p_1 (out, ref);
2117 case BUILT_IN_REMQUO:
2118 case BUILT_IN_REMQUOF:
2119 case BUILT_IN_REMQUOL:
2121 tree out = gimple_call_arg (call, 2);
2122 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2123 return true;
2124 if (flag_errno_math)
2125 break;
2126 return false;
2128 case BUILT_IN_SINCOS:
2129 case BUILT_IN_SINCOSF:
2130 case BUILT_IN_SINCOSL:
2132 tree sin = gimple_call_arg (call, 1);
2133 tree cos = gimple_call_arg (call, 2);
2134 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2135 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2137 /* __sync_* builtins and some OpenMP builtins act as threading
2138 barriers. */
2139 #undef DEF_SYNC_BUILTIN
2140 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2141 #include "sync-builtins.def"
2142 #undef DEF_SYNC_BUILTIN
2143 case BUILT_IN_GOMP_ATOMIC_START:
2144 case BUILT_IN_GOMP_ATOMIC_END:
2145 case BUILT_IN_GOMP_BARRIER:
2146 case BUILT_IN_GOMP_BARRIER_CANCEL:
2147 case BUILT_IN_GOMP_TASKWAIT:
2148 case BUILT_IN_GOMP_TASKGROUP_END:
2149 case BUILT_IN_GOMP_CRITICAL_START:
2150 case BUILT_IN_GOMP_CRITICAL_END:
2151 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2152 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2153 case BUILT_IN_GOMP_LOOP_END:
2154 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2155 case BUILT_IN_GOMP_ORDERED_START:
2156 case BUILT_IN_GOMP_ORDERED_END:
2157 case BUILT_IN_GOMP_SECTIONS_END:
2158 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2159 case BUILT_IN_GOMP_SINGLE_COPY_START:
2160 case BUILT_IN_GOMP_SINGLE_COPY_END:
2161 return true;
2162 default:
2163 /* Fallthru to general call handling. */;
2166 /* Check if base is a global static variable that is not written
2167 by the function. */
2168 if (callee != NULL_TREE
2169 && TREE_CODE (base) == VAR_DECL
2170 && TREE_STATIC (base))
2172 struct cgraph_node *node = cgraph_node::get (callee);
2173 bitmap not_written;
2175 if (node
2176 && (not_written = ipa_reference_get_not_written_global (node))
2177 && bitmap_bit_p (not_written, DECL_UID (base)))
2178 return false;
2181 /* Check if the base variable is call-clobbered. */
2182 if (DECL_P (base))
2183 return pt_solution_includes (gimple_call_clobber_set (call), base);
2184 else if ((TREE_CODE (base) == MEM_REF
2185 || TREE_CODE (base) == TARGET_MEM_REF)
2186 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2188 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2189 if (!pi)
2190 return true;
2192 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2195 return true;
2198 /* If the call in statement CALL may clobber the memory reference REF
2199 return true, otherwise return false. */
2201 bool
2202 call_may_clobber_ref_p (gcall *call, tree ref)
2204 bool res;
2205 ao_ref r;
2206 ao_ref_init (&r, ref);
2207 res = call_may_clobber_ref_p_1 (call, &r);
2208 if (res)
2209 ++alias_stats.call_may_clobber_ref_p_may_alias;
2210 else
2211 ++alias_stats.call_may_clobber_ref_p_no_alias;
2212 return res;
2216 /* If the statement STMT may clobber the memory reference REF return true,
2217 otherwise return false. */
2219 bool
2220 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2222 if (is_gimple_call (stmt))
2224 tree lhs = gimple_call_lhs (stmt);
2225 if (lhs
2226 && TREE_CODE (lhs) != SSA_NAME)
2228 ao_ref r;
2229 ao_ref_init (&r, lhs);
2230 if (refs_may_alias_p_1 (ref, &r, true))
2231 return true;
2234 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2236 else if (gimple_assign_single_p (stmt))
2238 tree lhs = gimple_assign_lhs (stmt);
2239 if (TREE_CODE (lhs) != SSA_NAME)
2241 ao_ref r;
2242 ao_ref_init (&r, lhs);
2243 return refs_may_alias_p_1 (ref, &r, true);
2246 else if (gimple_code (stmt) == GIMPLE_ASM)
2247 return true;
2249 return false;
2252 bool
2253 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2255 ao_ref r;
2256 ao_ref_init (&r, ref);
2257 return stmt_may_clobber_ref_p_1 (stmt, &r);
2260 /* If STMT kills the memory reference REF return true, otherwise
2261 return false. */
2263 bool
2264 stmt_kills_ref_p (gimple stmt, ao_ref *ref)
2266 if (!ao_ref_base (ref))
2267 return false;
2269 if (gimple_has_lhs (stmt)
2270 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2271 /* The assignment is not necessarily carried out if it can throw
2272 and we can catch it in the current function where we could inspect
2273 the previous value.
2274 ??? We only need to care about the RHS throwing. For aggregate
2275 assignments or similar calls and non-call exceptions the LHS
2276 might throw as well. */
2277 && !stmt_can_throw_internal (stmt))
2279 tree lhs = gimple_get_lhs (stmt);
2280 /* If LHS is literally a base of the access we are done. */
2281 if (ref->ref)
2283 tree base = ref->ref;
2284 if (handled_component_p (base))
2286 tree saved_lhs0 = NULL_TREE;
2287 if (handled_component_p (lhs))
2289 saved_lhs0 = TREE_OPERAND (lhs, 0);
2290 TREE_OPERAND (lhs, 0) = integer_zero_node;
2294 /* Just compare the outermost handled component, if
2295 they are equal we have found a possible common
2296 base. */
2297 tree saved_base0 = TREE_OPERAND (base, 0);
2298 TREE_OPERAND (base, 0) = integer_zero_node;
2299 bool res = operand_equal_p (lhs, base, 0);
2300 TREE_OPERAND (base, 0) = saved_base0;
2301 if (res)
2302 break;
2303 /* Otherwise drop handled components of the access. */
2304 base = saved_base0;
2306 while (handled_component_p (base));
2307 if (saved_lhs0)
2308 TREE_OPERAND (lhs, 0) = saved_lhs0;
2310 /* Finally check if lhs is equal or equal to the base candidate
2311 of the access. */
2312 if (operand_equal_p (lhs, base, 0))
2313 return true;
2316 /* Now look for non-literal equal bases with the restriction of
2317 handling constant offset and size. */
2318 /* For a must-alias check we need to be able to constrain
2319 the access properly. */
2320 if (ref->max_size == -1)
2321 return false;
2322 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2323 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2324 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2325 so base == ref->base does not always hold. */
2326 if (base != ref->base)
2328 /* If both base and ref->base are MEM_REFs, only compare the
2329 first operand, and if the second operand isn't equal constant,
2330 try to add the offsets into offset and ref_offset. */
2331 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2332 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2334 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2335 TREE_OPERAND (ref->base, 1)))
2337 offset_int off1 = mem_ref_offset (base);
2338 off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2339 off1 += offset;
2340 offset_int off2 = mem_ref_offset (ref->base);
2341 off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2342 off2 += ref_offset;
2343 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2345 offset = off1.to_shwi ();
2346 ref_offset = off2.to_shwi ();
2348 else
2349 size = -1;
2352 else
2353 size = -1;
2355 /* For a must-alias check we need to be able to constrain
2356 the access properly. */
2357 if (size != -1 && size == max_size)
2359 if (offset <= ref_offset
2360 && offset + size >= ref_offset + ref->max_size)
2361 return true;
2365 if (is_gimple_call (stmt))
2367 tree callee = gimple_call_fndecl (stmt);
2368 if (callee != NULL_TREE
2369 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2370 switch (DECL_FUNCTION_CODE (callee))
2372 case BUILT_IN_FREE:
2374 tree ptr = gimple_call_arg (stmt, 0);
2375 tree base = ao_ref_base (ref);
2376 if (base && TREE_CODE (base) == MEM_REF
2377 && TREE_OPERAND (base, 0) == ptr)
2378 return true;
2379 break;
2382 case BUILT_IN_MEMCPY:
2383 case BUILT_IN_MEMPCPY:
2384 case BUILT_IN_MEMMOVE:
2385 case BUILT_IN_MEMSET:
2386 case BUILT_IN_MEMCPY_CHK:
2387 case BUILT_IN_MEMPCPY_CHK:
2388 case BUILT_IN_MEMMOVE_CHK:
2389 case BUILT_IN_MEMSET_CHK:
2391 /* For a must-alias check we need to be able to constrain
2392 the access properly. */
2393 if (ref->max_size == -1)
2394 return false;
2395 tree dest = gimple_call_arg (stmt, 0);
2396 tree len = gimple_call_arg (stmt, 2);
2397 if (!tree_fits_shwi_p (len))
2398 return false;
2399 tree rbase = ref->base;
2400 offset_int roffset = ref->offset;
2401 ao_ref dref;
2402 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2403 tree base = ao_ref_base (&dref);
2404 offset_int offset = dref.offset;
2405 if (!base || dref.size == -1)
2406 return false;
2407 if (TREE_CODE (base) == MEM_REF)
2409 if (TREE_CODE (rbase) != MEM_REF)
2410 return false;
2411 // Compare pointers.
2412 offset += wi::lshift (mem_ref_offset (base),
2413 LOG2_BITS_PER_UNIT);
2414 roffset += wi::lshift (mem_ref_offset (rbase),
2415 LOG2_BITS_PER_UNIT);
2416 base = TREE_OPERAND (base, 0);
2417 rbase = TREE_OPERAND (rbase, 0);
2419 if (base == rbase
2420 && wi::les_p (offset, roffset)
2421 && wi::les_p (roffset + ref->max_size,
2422 offset + wi::lshift (wi::to_offset (len),
2423 LOG2_BITS_PER_UNIT)))
2424 return true;
2425 break;
2428 case BUILT_IN_VA_END:
2430 tree ptr = gimple_call_arg (stmt, 0);
2431 if (TREE_CODE (ptr) == ADDR_EXPR)
2433 tree base = ao_ref_base (ref);
2434 if (TREE_OPERAND (ptr, 0) == base)
2435 return true;
2437 break;
2440 default:;
2443 return false;
2446 bool
2447 stmt_kills_ref_p (gimple stmt, tree ref)
2449 ao_ref r;
2450 ao_ref_init (&r, ref);
2451 return stmt_kills_ref_p (stmt, &r);
2455 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2456 TARGET or a statement clobbering the memory reference REF in which
2457 case false is returned. The walk starts with VUSE, one argument of PHI. */
2459 static bool
2460 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2461 tree vuse, unsigned int *cnt, bitmap *visited,
2462 bool abort_on_visited,
2463 void *(*translate)(ao_ref *, tree, void *, bool),
2464 void *data)
2466 basic_block bb = gimple_bb (phi);
2468 if (!*visited)
2469 *visited = BITMAP_ALLOC (NULL);
2471 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2473 /* Walk until we hit the target. */
2474 while (vuse != target)
2476 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2477 /* Recurse for PHI nodes. */
2478 if (gimple_code (def_stmt) == GIMPLE_PHI)
2480 /* An already visited PHI node ends the walk successfully. */
2481 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2482 return !abort_on_visited;
2483 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2484 visited, abort_on_visited,
2485 translate, data);
2486 if (!vuse)
2487 return false;
2488 continue;
2490 else if (gimple_nop_p (def_stmt))
2491 return false;
2492 else
2494 /* A clobbering statement or the end of the IL ends it failing. */
2495 ++*cnt;
2496 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2498 if (translate
2499 && (*translate) (ref, vuse, data, true) == NULL)
2501 else
2502 return false;
2505 /* If we reach a new basic-block see if we already skipped it
2506 in a previous walk that ended successfully. */
2507 if (gimple_bb (def_stmt) != bb)
2509 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2510 return !abort_on_visited;
2511 bb = gimple_bb (def_stmt);
2513 vuse = gimple_vuse (def_stmt);
2515 return true;
2518 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2519 until we hit the phi argument definition that dominates the other one.
2520 Return that, or NULL_TREE if there is no such definition. */
2522 static tree
2523 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2524 ao_ref *ref, unsigned int *cnt,
2525 bitmap *visited, bool abort_on_visited,
2526 void *(*translate)(ao_ref *, tree, void *, bool),
2527 void *data)
2529 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2530 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2531 tree common_vuse;
2533 if (arg0 == arg1)
2534 return arg0;
2535 else if (gimple_nop_p (def0)
2536 || (!gimple_nop_p (def1)
2537 && dominated_by_p (CDI_DOMINATORS,
2538 gimple_bb (def1), gimple_bb (def0))))
2540 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2541 visited, abort_on_visited, translate, data))
2542 return arg0;
2544 else if (gimple_nop_p (def1)
2545 || dominated_by_p (CDI_DOMINATORS,
2546 gimple_bb (def0), gimple_bb (def1)))
2548 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2549 visited, abort_on_visited, translate, data))
2550 return arg1;
2552 /* Special case of a diamond:
2553 MEM_1 = ...
2554 goto (cond) ? L1 : L2
2555 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2556 goto L3
2557 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2558 L3: MEM_4 = PHI<MEM_2, MEM_3>
2559 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2560 dominate each other, but still we can easily skip this PHI node
2561 if we recognize that the vuse MEM operand is the same for both,
2562 and that we can skip both statements (they don't clobber us).
2563 This is still linear. Don't use maybe_skip_until, that might
2564 potentially be slow. */
2565 else if ((common_vuse = gimple_vuse (def0))
2566 && common_vuse == gimple_vuse (def1))
2568 *cnt += 2;
2569 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2570 || (translate
2571 && (*translate) (ref, arg0, data, true) == NULL))
2572 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2573 || (translate
2574 && (*translate) (ref, arg1, data, true) == NULL)))
2575 return common_vuse;
2578 return NULL_TREE;
2582 /* Starting from a PHI node for the virtual operand of the memory reference
2583 REF find a continuation virtual operand that allows to continue walking
2584 statements dominating PHI skipping only statements that cannot possibly
2585 clobber REF. Increments *CNT for each alias disambiguation done.
2586 Returns NULL_TREE if no suitable virtual operand can be found. */
2588 tree
2589 get_continuation_for_phi (gimple phi, ao_ref *ref,
2590 unsigned int *cnt, bitmap *visited,
2591 bool abort_on_visited,
2592 void *(*translate)(ao_ref *, tree, void *, bool),
2593 void *data)
2595 unsigned nargs = gimple_phi_num_args (phi);
2597 /* Through a single-argument PHI we can simply look through. */
2598 if (nargs == 1)
2599 return PHI_ARG_DEF (phi, 0);
2601 /* For two or more arguments try to pairwise skip non-aliasing code
2602 until we hit the phi argument definition that dominates the other one. */
2603 else if (nargs >= 2)
2605 tree arg0, arg1;
2606 unsigned i;
2608 /* Find a candidate for the virtual operand which definition
2609 dominates those of all others. */
2610 arg0 = PHI_ARG_DEF (phi, 0);
2611 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2612 for (i = 1; i < nargs; ++i)
2614 arg1 = PHI_ARG_DEF (phi, i);
2615 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2617 arg0 = arg1;
2618 break;
2620 if (dominated_by_p (CDI_DOMINATORS,
2621 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2622 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2623 arg0 = arg1;
2626 /* Then pairwise reduce against the found candidate. */
2627 for (i = 0; i < nargs; ++i)
2629 arg1 = PHI_ARG_DEF (phi, i);
2630 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2631 cnt, visited, abort_on_visited,
2632 translate, data);
2633 if (!arg0)
2634 return NULL_TREE;
2637 return arg0;
2640 return NULL_TREE;
2643 /* Based on the memory reference REF and its virtual use VUSE call
2644 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2645 itself. That is, for each virtual use for which its defining statement
2646 does not clobber REF.
2648 WALKER is called with REF, the current virtual use and DATA. If
2649 WALKER returns non-NULL the walk stops and its result is returned.
2650 At the end of a non-successful walk NULL is returned.
2652 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2653 use which definition is a statement that may clobber REF and DATA.
2654 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2655 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2656 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2657 to adjust REF and *DATA to make that valid.
2659 VALUEIZE if non-NULL is called with the next VUSE that is considered
2660 and return value is substituted for that. This can be used to
2661 implement optimistic value-numbering for example. Note that the
2662 VUSE argument is assumed to be valueized already.
2664 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2666 void *
2667 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2668 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2669 void *(*translate)(ao_ref *, tree, void *, bool),
2670 tree (*valueize)(tree),
2671 void *data)
2673 bitmap visited = NULL;
2674 void *res;
2675 unsigned int cnt = 0;
2676 bool translated = false;
2678 timevar_push (TV_ALIAS_STMT_WALK);
2682 gimple def_stmt;
2684 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2685 res = (*walker) (ref, vuse, cnt, data);
2686 /* Abort walk. */
2687 if (res == (void *)-1)
2689 res = NULL;
2690 break;
2692 /* Lookup succeeded. */
2693 else if (res != NULL)
2694 break;
2696 if (valueize)
2697 vuse = valueize (vuse);
2698 def_stmt = SSA_NAME_DEF_STMT (vuse);
2699 if (gimple_nop_p (def_stmt))
2700 break;
2701 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2702 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2703 &visited, translated, translate, data);
2704 else
2706 cnt++;
2707 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2709 if (!translate)
2710 break;
2711 res = (*translate) (ref, vuse, data, false);
2712 /* Failed lookup and translation. */
2713 if (res == (void *)-1)
2715 res = NULL;
2716 break;
2718 /* Lookup succeeded. */
2719 else if (res != NULL)
2720 break;
2721 /* Translation succeeded, continue walking. */
2722 translated = true;
2724 vuse = gimple_vuse (def_stmt);
2727 while (vuse);
2729 if (visited)
2730 BITMAP_FREE (visited);
2732 timevar_pop (TV_ALIAS_STMT_WALK);
2734 return res;
2738 /* Based on the memory reference REF call WALKER for each vdef which
2739 defining statement may clobber REF, starting with VDEF. If REF
2740 is NULL_TREE, each defining statement is visited.
2742 WALKER is called with REF, the current vdef and DATA. If WALKER
2743 returns true the walk is stopped, otherwise it continues.
2745 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2746 The pointer may be NULL and then we do not track this information.
2748 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2749 PHI argument (but only one walk continues on merge points), the
2750 return value is true if any of the walks was successful.
2752 The function returns the number of statements walked. */
2754 static unsigned int
2755 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2756 bool (*walker)(ao_ref *, tree, void *), void *data,
2757 bitmap *visited, unsigned int cnt,
2758 bool *function_entry_reached)
2762 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2764 if (*visited
2765 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2766 return cnt;
2768 if (gimple_nop_p (def_stmt))
2770 if (function_entry_reached)
2771 *function_entry_reached = true;
2772 return cnt;
2774 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2776 unsigned i;
2777 if (!*visited)
2778 *visited = BITMAP_ALLOC (NULL);
2779 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2780 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2781 walker, data, visited, 0,
2782 function_entry_reached);
2783 return cnt;
2786 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2787 cnt++;
2788 if ((!ref
2789 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2790 && (*walker) (ref, vdef, data))
2791 return cnt;
2793 vdef = gimple_vuse (def_stmt);
2795 while (1);
2798 unsigned int
2799 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2800 bool (*walker)(ao_ref *, tree, void *), void *data,
2801 bitmap *visited,
2802 bool *function_entry_reached)
2804 bitmap local_visited = NULL;
2805 unsigned int ret;
2807 timevar_push (TV_ALIAS_STMT_WALK);
2809 if (function_entry_reached)
2810 *function_entry_reached = false;
2812 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2813 visited ? visited : &local_visited, 0,
2814 function_entry_reached);
2815 if (local_visited)
2816 BITMAP_FREE (local_visited);
2818 timevar_pop (TV_ALIAS_STMT_WALK);
2820 return ret;