2014-10-29 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-alias.c
blob2ad57cd00e2ab0732ea31832d2d945ecda396764
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 "predict.h"
29 #include "vec.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "hard-reg-set.h"
34 #include "input.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 "expr.h"
53 #include "tree-dfa.h"
54 #include "tree-inline.h"
55 #include "params.h"
56 #include "alloc-pool.h"
57 #include "tree-ssa-alias.h"
58 #include "bitmap.h"
59 #include "hash-map.h"
60 #include "plugin-api.h"
61 #include "ipa-ref.h"
62 #include "cgraph.h"
63 #include "ipa-reference.h"
65 /* Broad overview of how alias analysis on gimple works:
67 Statements clobbering or using memory are linked through the
68 virtual operand factored use-def chain. The virtual operand
69 is unique per function, its symbol is accessible via gimple_vop (cfun).
70 Virtual operands are used for efficiently walking memory statements
71 in the gimple IL and are useful for things like value-numbering as
72 a generation count for memory references.
74 SSA_NAME pointers may have associated points-to information
75 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
76 points-to information is (re-)computed by the TODO_rebuild_alias
77 pass manager todo. Points-to information is also used for more
78 precise tracking of call-clobbered and call-used variables and
79 related disambiguations.
81 This file contains functions for disambiguating memory references,
82 the so called alias-oracle and tools for walking of the gimple IL.
84 The main alias-oracle entry-points are
86 bool stmt_may_clobber_ref_p (gimple, tree)
88 This function queries if a statement may invalidate (parts of)
89 the memory designated by the reference tree argument.
91 bool ref_maybe_used_by_stmt_p (gimple, tree)
93 This function queries if a statement may need (parts of) the
94 memory designated by the reference tree argument.
96 There are variants of these functions that only handle the call
97 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
98 Note that these do not disambiguate against a possible call lhs.
100 bool refs_may_alias_p (tree, tree)
102 This function tries to disambiguate two reference trees.
104 bool ptr_deref_may_alias_global_p (tree)
106 This function queries if dereferencing a pointer variable may
107 alias global memory.
109 More low-level disambiguators are available and documented in
110 this file. Low-level disambiguators dealing with points-to
111 information are in tree-ssa-structalias.c. */
114 /* Query statistics for the different low-level disambiguators.
115 A high-level query may trigger multiple of them. */
117 static struct {
118 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
119 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
120 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
121 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
122 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
123 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
124 } alias_stats;
126 void
127 dump_alias_stats (FILE *s)
129 fprintf (s, "\nAlias oracle query stats:\n");
130 fprintf (s, " refs_may_alias_p: "
131 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
132 HOST_WIDE_INT_PRINT_DEC" queries\n",
133 alias_stats.refs_may_alias_p_no_alias,
134 alias_stats.refs_may_alias_p_no_alias
135 + alias_stats.refs_may_alias_p_may_alias);
136 fprintf (s, " ref_maybe_used_by_call_p: "
137 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
138 HOST_WIDE_INT_PRINT_DEC" queries\n",
139 alias_stats.ref_maybe_used_by_call_p_no_alias,
140 alias_stats.refs_may_alias_p_no_alias
141 + alias_stats.ref_maybe_used_by_call_p_may_alias);
142 fprintf (s, " call_may_clobber_ref_p: "
143 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
144 HOST_WIDE_INT_PRINT_DEC" queries\n",
145 alias_stats.call_may_clobber_ref_p_no_alias,
146 alias_stats.call_may_clobber_ref_p_no_alias
147 + alias_stats.call_may_clobber_ref_p_may_alias);
151 /* Return true, if dereferencing PTR may alias with a global variable. */
153 bool
154 ptr_deref_may_alias_global_p (tree ptr)
156 struct ptr_info_def *pi;
158 /* If we end up with a pointer constant here that may point
159 to global memory. */
160 if (TREE_CODE (ptr) != SSA_NAME)
161 return true;
163 pi = SSA_NAME_PTR_INFO (ptr);
165 /* If we do not have points-to information for this variable,
166 we have to punt. */
167 if (!pi)
168 return true;
170 /* ??? This does not use TBAA to prune globals ptr may not access. */
171 return pt_solution_includes_global (&pi->pt);
174 /* Return true if dereferencing PTR may alias DECL.
175 The caller is responsible for applying TBAA to see if PTR
176 may access DECL at all. */
178 static bool
179 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
181 struct ptr_info_def *pi;
183 /* Conversions are irrelevant for points-to information and
184 data-dependence analysis can feed us those. */
185 STRIP_NOPS (ptr);
187 /* Anything we do not explicilty handle aliases. */
188 if ((TREE_CODE (ptr) != SSA_NAME
189 && TREE_CODE (ptr) != ADDR_EXPR
190 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
191 || !POINTER_TYPE_P (TREE_TYPE (ptr))
192 || (TREE_CODE (decl) != VAR_DECL
193 && TREE_CODE (decl) != PARM_DECL
194 && TREE_CODE (decl) != RESULT_DECL))
195 return true;
197 /* Disregard pointer offsetting. */
198 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
202 ptr = TREE_OPERAND (ptr, 0);
204 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
205 return ptr_deref_may_alias_decl_p (ptr, decl);
208 /* ADDR_EXPR pointers either just offset another pointer or directly
209 specify the pointed-to set. */
210 if (TREE_CODE (ptr) == ADDR_EXPR)
212 tree base = get_base_address (TREE_OPERAND (ptr, 0));
213 if (base
214 && (TREE_CODE (base) == MEM_REF
215 || TREE_CODE (base) == TARGET_MEM_REF))
216 ptr = TREE_OPERAND (base, 0);
217 else if (base
218 && DECL_P (base))
219 return base == decl;
220 else if (base
221 && CONSTANT_CLASS_P (base))
222 return false;
223 else
224 return true;
227 /* Non-aliased variables can not be pointed to. */
228 if (!may_be_aliased (decl))
229 return false;
231 /* If we do not have useful points-to information for this pointer
232 we cannot disambiguate anything else. */
233 pi = SSA_NAME_PTR_INFO (ptr);
234 if (!pi)
235 return true;
237 return pt_solution_includes (&pi->pt, decl);
240 /* Return true if dereferenced PTR1 and PTR2 may alias.
241 The caller is responsible for applying TBAA to see if accesses
242 through PTR1 and PTR2 may conflict at all. */
244 bool
245 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
247 struct ptr_info_def *pi1, *pi2;
249 /* Conversions are irrelevant for points-to information and
250 data-dependence analysis can feed us those. */
251 STRIP_NOPS (ptr1);
252 STRIP_NOPS (ptr2);
254 /* Disregard pointer offsetting. */
255 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
259 ptr1 = TREE_OPERAND (ptr1, 0);
261 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
262 return ptr_derefs_may_alias_p (ptr1, ptr2);
264 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
268 ptr2 = TREE_OPERAND (ptr2, 0);
270 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
271 return ptr_derefs_may_alias_p (ptr1, ptr2);
274 /* ADDR_EXPR pointers either just offset another pointer or directly
275 specify the pointed-to set. */
276 if (TREE_CODE (ptr1) == ADDR_EXPR)
278 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
279 if (base
280 && (TREE_CODE (base) == MEM_REF
281 || TREE_CODE (base) == TARGET_MEM_REF))
282 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
283 else if (base
284 && DECL_P (base))
285 return ptr_deref_may_alias_decl_p (ptr2, base);
286 else
287 return true;
289 if (TREE_CODE (ptr2) == ADDR_EXPR)
291 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
292 if (base
293 && (TREE_CODE (base) == MEM_REF
294 || TREE_CODE (base) == TARGET_MEM_REF))
295 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
296 else if (base
297 && DECL_P (base))
298 return ptr_deref_may_alias_decl_p (ptr1, base);
299 else
300 return true;
303 /* From here we require SSA name pointers. Anything else aliases. */
304 if (TREE_CODE (ptr1) != SSA_NAME
305 || TREE_CODE (ptr2) != SSA_NAME
306 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
307 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
308 return true;
310 /* We may end up with two empty points-to solutions for two same pointers.
311 In this case we still want to say both pointers alias, so shortcut
312 that here. */
313 if (ptr1 == ptr2)
314 return true;
316 /* If we do not have useful points-to information for either pointer
317 we cannot disambiguate anything else. */
318 pi1 = SSA_NAME_PTR_INFO (ptr1);
319 pi2 = SSA_NAME_PTR_INFO (ptr2);
320 if (!pi1 || !pi2)
321 return true;
323 /* ??? This does not use TBAA to prune decls from the intersection
324 that not both pointers may access. */
325 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
328 /* Return true if dereferencing PTR may alias *REF.
329 The caller is responsible for applying TBAA to see if PTR
330 may access *REF at all. */
332 static bool
333 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
335 tree base = ao_ref_base (ref);
337 if (TREE_CODE (base) == MEM_REF
338 || TREE_CODE (base) == TARGET_MEM_REF)
339 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
340 else if (DECL_P (base))
341 return ptr_deref_may_alias_decl_p (ptr, base);
343 return true;
346 /* Returns whether reference REF to BASE may refer to global memory. */
348 static bool
349 ref_may_alias_global_p_1 (tree base)
351 if (DECL_P (base))
352 return is_global_var (base);
353 else if (TREE_CODE (base) == MEM_REF
354 || TREE_CODE (base) == TARGET_MEM_REF)
355 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
356 return true;
359 bool
360 ref_may_alias_global_p (ao_ref *ref)
362 tree base = ao_ref_base (ref);
363 return ref_may_alias_global_p_1 (base);
366 bool
367 ref_may_alias_global_p (tree ref)
369 tree base = get_base_address (ref);
370 return ref_may_alias_global_p_1 (base);
373 /* Return true whether STMT may clobber global memory. */
375 bool
376 stmt_may_clobber_global_p (gimple stmt)
378 tree lhs;
380 if (!gimple_vdef (stmt))
381 return false;
383 /* ??? We can ask the oracle whether an artificial pointer
384 dereference with a pointer with points-to information covering
385 all global memory (what about non-address taken memory?) maybe
386 clobbered by this call. As there is at the moment no convenient
387 way of doing that without generating garbage do some manual
388 checking instead.
389 ??? We could make a NULL ao_ref argument to the various
390 predicates special, meaning any global memory. */
392 switch (gimple_code (stmt))
394 case GIMPLE_ASSIGN:
395 lhs = gimple_assign_lhs (stmt);
396 return (TREE_CODE (lhs) != SSA_NAME
397 && ref_may_alias_global_p (lhs));
398 case GIMPLE_CALL:
399 return true;
400 default:
401 return true;
406 /* Dump alias information on FILE. */
408 void
409 dump_alias_info (FILE *file)
411 unsigned i;
412 const char *funcname
413 = lang_hooks.decl_printable_name (current_function_decl, 2);
414 tree var;
416 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
418 fprintf (file, "Aliased symbols\n\n");
420 FOR_EACH_LOCAL_DECL (cfun, i, var)
422 if (may_be_aliased (var))
423 dump_variable (file, var);
426 fprintf (file, "\nCall clobber information\n");
428 fprintf (file, "\nESCAPED");
429 dump_points_to_solution (file, &cfun->gimple_df->escaped);
431 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
433 for (i = 1; i < num_ssa_names; i++)
435 tree ptr = ssa_name (i);
436 struct ptr_info_def *pi;
438 if (ptr == NULL_TREE
439 || !POINTER_TYPE_P (TREE_TYPE (ptr))
440 || SSA_NAME_IN_FREE_LIST (ptr))
441 continue;
443 pi = SSA_NAME_PTR_INFO (ptr);
444 if (pi)
445 dump_points_to_info_for (file, ptr);
448 fprintf (file, "\n");
452 /* Dump alias information on stderr. */
454 DEBUG_FUNCTION void
455 debug_alias_info (void)
457 dump_alias_info (stderr);
461 /* Dump the points-to set *PT into FILE. */
463 void
464 dump_points_to_solution (FILE *file, struct pt_solution *pt)
466 if (pt->anything)
467 fprintf (file, ", points-to anything");
469 if (pt->nonlocal)
470 fprintf (file, ", points-to non-local");
472 if (pt->escaped)
473 fprintf (file, ", points-to escaped");
475 if (pt->ipa_escaped)
476 fprintf (file, ", points-to unit escaped");
478 if (pt->null)
479 fprintf (file, ", points-to NULL");
481 if (pt->vars)
483 fprintf (file, ", points-to vars: ");
484 dump_decl_set (file, pt->vars);
485 if (pt->vars_contains_nonlocal
486 && pt->vars_contains_escaped_heap)
487 fprintf (file, " (nonlocal, escaped heap)");
488 else if (pt->vars_contains_nonlocal
489 && pt->vars_contains_escaped)
490 fprintf (file, " (nonlocal, escaped)");
491 else if (pt->vars_contains_nonlocal)
492 fprintf (file, " (nonlocal)");
493 else if (pt->vars_contains_escaped_heap)
494 fprintf (file, " (escaped heap)");
495 else if (pt->vars_contains_escaped)
496 fprintf (file, " (escaped)");
501 /* Unified dump function for pt_solution. */
503 DEBUG_FUNCTION void
504 debug (pt_solution &ref)
506 dump_points_to_solution (stderr, &ref);
509 DEBUG_FUNCTION void
510 debug (pt_solution *ptr)
512 if (ptr)
513 debug (*ptr);
514 else
515 fprintf (stderr, "<nil>\n");
519 /* Dump points-to information for SSA_NAME PTR into FILE. */
521 void
522 dump_points_to_info_for (FILE *file, tree ptr)
524 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
526 print_generic_expr (file, ptr, dump_flags);
528 if (pi)
529 dump_points_to_solution (file, &pi->pt);
530 else
531 fprintf (file, ", points-to anything");
533 fprintf (file, "\n");
537 /* Dump points-to information for VAR into stderr. */
539 DEBUG_FUNCTION void
540 debug_points_to_info_for (tree var)
542 dump_points_to_info_for (stderr, var);
546 /* Initializes the alias-oracle reference representation *R from REF. */
548 void
549 ao_ref_init (ao_ref *r, tree ref)
551 r->ref = ref;
552 r->base = NULL_TREE;
553 r->offset = 0;
554 r->size = -1;
555 r->max_size = -1;
556 r->ref_alias_set = -1;
557 r->base_alias_set = -1;
558 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
561 /* Returns the base object of the memory reference *REF. */
563 tree
564 ao_ref_base (ao_ref *ref)
566 if (ref->base)
567 return ref->base;
568 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
569 &ref->max_size);
570 return ref->base;
573 /* Returns the base object alias set of the memory reference *REF. */
575 alias_set_type
576 ao_ref_base_alias_set (ao_ref *ref)
578 tree base_ref;
579 if (ref->base_alias_set != -1)
580 return ref->base_alias_set;
581 if (!ref->ref)
582 return 0;
583 base_ref = ref->ref;
584 while (handled_component_p (base_ref))
585 base_ref = TREE_OPERAND (base_ref, 0);
586 ref->base_alias_set = get_alias_set (base_ref);
587 return ref->base_alias_set;
590 /* Returns the reference alias set of the memory reference *REF. */
592 alias_set_type
593 ao_ref_alias_set (ao_ref *ref)
595 if (ref->ref_alias_set != -1)
596 return ref->ref_alias_set;
597 ref->ref_alias_set = get_alias_set (ref->ref);
598 return ref->ref_alias_set;
601 /* Init an alias-oracle reference representation from a gimple pointer
602 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
603 size is assumed to be unknown. The access is assumed to be only
604 to or after of the pointer target, not before it. */
606 void
607 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
609 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
610 ref->ref = NULL_TREE;
611 if (TREE_CODE (ptr) == SSA_NAME)
613 gimple stmt = SSA_NAME_DEF_STMT (ptr);
614 if (gimple_assign_single_p (stmt)
615 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
616 ptr = gimple_assign_rhs1 (stmt);
617 else if (is_gimple_assign (stmt)
618 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
619 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
621 ptr = gimple_assign_rhs1 (stmt);
622 extra_offset = BITS_PER_UNIT
623 * int_cst_value (gimple_assign_rhs2 (stmt));
627 if (TREE_CODE (ptr) == ADDR_EXPR)
629 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
630 if (ref->base)
631 ref->offset = BITS_PER_UNIT * t;
632 else
634 size = NULL_TREE;
635 ref->offset = 0;
636 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
639 else
641 ref->base = build2 (MEM_REF, char_type_node,
642 ptr, null_pointer_node);
643 ref->offset = 0;
645 ref->offset += extra_offset;
646 if (size
647 && tree_fits_shwi_p (size)
648 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
649 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
650 else
651 ref->max_size = ref->size = -1;
652 ref->ref_alias_set = 0;
653 ref->base_alias_set = 0;
654 ref->volatile_p = false;
657 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
658 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
659 decide. */
661 static inline int
662 same_type_for_tbaa (tree type1, tree type2)
664 type1 = TYPE_MAIN_VARIANT (type1);
665 type2 = TYPE_MAIN_VARIANT (type2);
667 /* If we would have to do structural comparison bail out. */
668 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
669 || TYPE_STRUCTURAL_EQUALITY_P (type2))
670 return -1;
672 /* Compare the canonical types. */
673 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
674 return 1;
676 /* ??? Array types are not properly unified in all cases as we have
677 spurious changes in the index types for example. Removing this
678 causes all sorts of problems with the Fortran frontend. */
679 if (TREE_CODE (type1) == ARRAY_TYPE
680 && TREE_CODE (type2) == ARRAY_TYPE)
681 return -1;
683 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
684 object of one of its constrained subtypes, e.g. when a function with an
685 unconstrained parameter passed by reference is called on an object and
686 inlined. But, even in the case of a fixed size, type and subtypes are
687 not equivalent enough as to share the same TYPE_CANONICAL, since this
688 would mean that conversions between them are useless, whereas they are
689 not (e.g. type and subtypes can have different modes). So, in the end,
690 they are only guaranteed to have the same alias set. */
691 if (get_alias_set (type1) == get_alias_set (type2))
692 return -1;
694 /* The types are known to be not equal. */
695 return 0;
698 /* Determine if the two component references REF1 and REF2 which are
699 based on access types TYPE1 and TYPE2 and of which at least one is based
700 on an indirect reference may alias. REF2 is the only one that can
701 be a decl in which case REF2_IS_DECL is true.
702 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
703 are the respective alias sets. */
705 static bool
706 aliasing_component_refs_p (tree ref1,
707 alias_set_type ref1_alias_set,
708 alias_set_type base1_alias_set,
709 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
710 tree ref2,
711 alias_set_type ref2_alias_set,
712 alias_set_type base2_alias_set,
713 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
714 bool ref2_is_decl)
716 /* If one reference is a component references through pointers try to find a
717 common base and apply offset based disambiguation. This handles
718 for example
719 struct A { int i; int j; } *q;
720 struct B { struct A a; int k; } *p;
721 disambiguating q->i and p->a.j. */
722 tree base1, base2;
723 tree type1, type2;
724 tree *refp;
725 int same_p;
727 /* Choose bases and base types to search for. */
728 base1 = ref1;
729 while (handled_component_p (base1))
730 base1 = TREE_OPERAND (base1, 0);
731 type1 = TREE_TYPE (base1);
732 base2 = ref2;
733 while (handled_component_p (base2))
734 base2 = TREE_OPERAND (base2, 0);
735 type2 = TREE_TYPE (base2);
737 /* Now search for the type1 in the access path of ref2. This
738 would be a common base for doing offset based disambiguation on. */
739 refp = &ref2;
740 while (handled_component_p (*refp)
741 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
742 refp = &TREE_OPERAND (*refp, 0);
743 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
744 /* If we couldn't compare types we have to bail out. */
745 if (same_p == -1)
746 return true;
747 else if (same_p == 1)
749 HOST_WIDE_INT offadj, sztmp, msztmp;
750 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
751 offset2 -= offadj;
752 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
753 offset1 -= offadj;
754 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
756 /* If we didn't find a common base, try the other way around. */
757 refp = &ref1;
758 while (handled_component_p (*refp)
759 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
760 refp = &TREE_OPERAND (*refp, 0);
761 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
762 /* If we couldn't compare types we have to bail out. */
763 if (same_p == -1)
764 return true;
765 else if (same_p == 1)
767 HOST_WIDE_INT offadj, sztmp, msztmp;
768 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
769 offset1 -= offadj;
770 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
771 offset2 -= offadj;
772 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
775 /* If we have two type access paths B1.path1 and B2.path2 they may
776 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
777 But we can still have a path that goes B1.path1...B2.path2 with
778 a part that we do not see. So we can only disambiguate now
779 if there is no B2 in the tail of path1 and no B1 on the
780 tail of path2. */
781 if (base1_alias_set == ref2_alias_set
782 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
783 return true;
784 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
785 if (!ref2_is_decl)
786 return (base2_alias_set == ref1_alias_set
787 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
788 return false;
791 /* Return true if we can determine that component references REF1 and REF2,
792 that are within a common DECL, cannot overlap. */
794 static bool
795 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
797 auto_vec<tree, 16> component_refs1;
798 auto_vec<tree, 16> component_refs2;
800 /* Create the stack of handled components for REF1. */
801 while (handled_component_p (ref1))
803 component_refs1.safe_push (ref1);
804 ref1 = TREE_OPERAND (ref1, 0);
806 if (TREE_CODE (ref1) == MEM_REF)
808 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
809 goto may_overlap;
810 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
813 /* Create the stack of handled components for REF2. */
814 while (handled_component_p (ref2))
816 component_refs2.safe_push (ref2);
817 ref2 = TREE_OPERAND (ref2, 0);
819 if (TREE_CODE (ref2) == MEM_REF)
821 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
822 goto may_overlap;
823 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
826 /* We must have the same base DECL. */
827 gcc_assert (ref1 == ref2);
829 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
830 rank. This is sufficient because we start from the same DECL and you
831 cannot reference several fields at a time with COMPONENT_REFs (unlike
832 with ARRAY_RANGE_REFs for arrays) so you always need the same number
833 of them to access a sub-component, unless you're in a union, in which
834 case the return value will precisely be false. */
835 while (true)
839 if (component_refs1.is_empty ())
840 goto may_overlap;
841 ref1 = component_refs1.pop ();
843 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
847 if (component_refs2.is_empty ())
848 goto may_overlap;
849 ref2 = component_refs2.pop ();
851 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
853 /* Beware of BIT_FIELD_REF. */
854 if (TREE_CODE (ref1) != COMPONENT_REF
855 || TREE_CODE (ref2) != COMPONENT_REF)
856 goto may_overlap;
858 tree field1 = TREE_OPERAND (ref1, 1);
859 tree field2 = TREE_OPERAND (ref2, 1);
861 /* ??? We cannot simply use the type of operand #0 of the refs here
862 as the Fortran compiler smuggles type punning into COMPONENT_REFs
863 for common blocks instead of using unions like everyone else. */
864 tree type1 = DECL_CONTEXT (field1);
865 tree type2 = DECL_CONTEXT (field2);
867 /* We cannot disambiguate fields in a union or qualified union. */
868 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
869 goto may_overlap;
871 /* Different fields of the same record type cannot overlap.
872 ??? Bitfields can overlap at RTL level so punt on them. */
873 if (field1 != field2)
875 component_refs1.release ();
876 component_refs2.release ();
877 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
881 may_overlap:
882 component_refs1.release ();
883 component_refs2.release ();
884 return false;
887 /* qsort compare function to sort FIELD_DECLs after their
888 DECL_FIELD_CONTEXT TYPE_UID. */
890 static inline int
891 ncr_compar (const void *field1_, const void *field2_)
893 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
894 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
895 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
896 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
897 if (uid1 < uid2)
898 return -1;
899 else if (uid1 > uid2)
900 return 1;
901 return 0;
904 /* Return true if we can determine that the fields referenced cannot
905 overlap for any pair of objects. */
907 static bool
908 nonoverlapping_component_refs_p (const_tree x, const_tree y)
910 if (!flag_strict_aliasing
911 || !x || !y
912 || TREE_CODE (x) != COMPONENT_REF
913 || TREE_CODE (y) != COMPONENT_REF)
914 return false;
916 auto_vec<const_tree, 16> fieldsx;
917 while (TREE_CODE (x) == COMPONENT_REF)
919 tree field = TREE_OPERAND (x, 1);
920 tree type = DECL_FIELD_CONTEXT (field);
921 if (TREE_CODE (type) == RECORD_TYPE)
922 fieldsx.safe_push (field);
923 x = TREE_OPERAND (x, 0);
925 if (fieldsx.length () == 0)
926 return false;
927 auto_vec<const_tree, 16> fieldsy;
928 while (TREE_CODE (y) == COMPONENT_REF)
930 tree field = TREE_OPERAND (y, 1);
931 tree type = DECL_FIELD_CONTEXT (field);
932 if (TREE_CODE (type) == RECORD_TYPE)
933 fieldsy.safe_push (TREE_OPERAND (y, 1));
934 y = TREE_OPERAND (y, 0);
936 if (fieldsy.length () == 0)
937 return false;
939 /* Most common case first. */
940 if (fieldsx.length () == 1
941 && fieldsy.length () == 1)
942 return ((DECL_FIELD_CONTEXT (fieldsx[0])
943 == DECL_FIELD_CONTEXT (fieldsy[0]))
944 && fieldsx[0] != fieldsy[0]
945 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
947 if (fieldsx.length () == 2)
949 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
951 const_tree tem = fieldsx[0];
952 fieldsx[0] = fieldsx[1];
953 fieldsx[1] = tem;
956 else
957 fieldsx.qsort (ncr_compar);
959 if (fieldsy.length () == 2)
961 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
963 const_tree tem = fieldsy[0];
964 fieldsy[0] = fieldsy[1];
965 fieldsy[1] = tem;
968 else
969 fieldsy.qsort (ncr_compar);
971 unsigned i = 0, j = 0;
974 const_tree fieldx = fieldsx[i];
975 const_tree fieldy = fieldsy[j];
976 tree typex = DECL_FIELD_CONTEXT (fieldx);
977 tree typey = DECL_FIELD_CONTEXT (fieldy);
978 if (typex == typey)
980 /* We're left with accessing different fields of a structure,
981 no possible overlap, unless they are both bitfields. */
982 if (fieldx != fieldy)
983 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
985 if (TYPE_UID (typex) < TYPE_UID (typey))
987 i++;
988 if (i == fieldsx.length ())
989 break;
991 else
993 j++;
994 if (j == fieldsy.length ())
995 break;
998 while (1);
1000 return false;
1004 /* Return true if two memory references based on the variables BASE1
1005 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1006 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1007 if non-NULL are the complete memory reference trees. */
1009 static bool
1010 decl_refs_may_alias_p (tree ref1, tree base1,
1011 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1012 tree ref2, tree base2,
1013 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1015 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1017 /* If both references are based on different variables, they cannot alias. */
1018 if (base1 != base2)
1019 return false;
1021 /* If both references are based on the same variable, they cannot alias if
1022 the accesses do not overlap. */
1023 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1024 return false;
1026 /* For components with variable position, the above test isn't sufficient,
1027 so we disambiguate component references manually. */
1028 if (ref1 && ref2
1029 && handled_component_p (ref1) && handled_component_p (ref2)
1030 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1031 return false;
1033 return true;
1036 /* Return true if an indirect reference based on *PTR1 constrained
1037 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1038 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1039 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1040 in which case they are computed on-demand. REF1 and REF2
1041 if non-NULL are the complete memory reference trees. */
1043 static bool
1044 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1045 HOST_WIDE_INT offset1,
1046 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1047 alias_set_type ref1_alias_set,
1048 alias_set_type base1_alias_set,
1049 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1050 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1051 alias_set_type ref2_alias_set,
1052 alias_set_type base2_alias_set, bool tbaa_p)
1054 tree ptr1;
1055 tree ptrtype1, dbase2;
1056 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1057 HOST_WIDE_INT doffset1, doffset2;
1059 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1060 || TREE_CODE (base1) == TARGET_MEM_REF)
1061 && DECL_P (base2));
1063 ptr1 = TREE_OPERAND (base1, 0);
1065 /* The offset embedded in MEM_REFs can be negative. Bias them
1066 so that the resulting offset adjustment is positive. */
1067 offset_int moff = mem_ref_offset (base1);
1068 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1069 if (wi::neg_p (moff))
1070 offset2p += (-moff).to_short_addr ();
1071 else
1072 offset1p += moff.to_short_addr ();
1074 /* If only one reference is based on a variable, they cannot alias if
1075 the pointer access is beyond the extent of the variable access.
1076 (the pointer base cannot validly point to an offset less than zero
1077 of the variable).
1078 ??? IVOPTs creates bases that do not honor this restriction,
1079 so do not apply this optimization for TARGET_MEM_REFs. */
1080 if (TREE_CODE (base1) != TARGET_MEM_REF
1081 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1082 return false;
1083 /* They also cannot alias if the pointer may not point to the decl. */
1084 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1085 return false;
1087 /* Disambiguations that rely on strict aliasing rules follow. */
1088 if (!flag_strict_aliasing || !tbaa_p)
1089 return true;
1091 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1093 /* If the alias set for a pointer access is zero all bets are off. */
1094 if (base1_alias_set == -1)
1095 base1_alias_set = get_deref_alias_set (ptrtype1);
1096 if (base1_alias_set == 0)
1097 return true;
1098 if (base2_alias_set == -1)
1099 base2_alias_set = get_alias_set (base2);
1101 /* When we are trying to disambiguate an access with a pointer dereference
1102 as base versus one with a decl as base we can use both the size
1103 of the decl and its dynamic type for extra disambiguation.
1104 ??? We do not know anything about the dynamic type of the decl
1105 other than that its alias-set contains base2_alias_set as a subset
1106 which does not help us here. */
1107 /* As we know nothing useful about the dynamic type of the decl just
1108 use the usual conflict check rather than a subset test.
1109 ??? We could introduce -fvery-strict-aliasing when the language
1110 does not allow decls to have a dynamic type that differs from their
1111 static type. Then we can check
1112 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1113 if (base1_alias_set != base2_alias_set
1114 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1115 return false;
1116 /* If the size of the access relevant for TBAA through the pointer
1117 is bigger than the size of the decl we can't possibly access the
1118 decl via that pointer. */
1119 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1120 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1121 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1122 /* ??? This in turn may run afoul when a decl of type T which is
1123 a member of union type U is accessed through a pointer to
1124 type U and sizeof T is smaller than sizeof U. */
1125 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1126 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1127 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1128 return false;
1130 if (!ref2)
1131 return true;
1133 /* If the decl is accessed via a MEM_REF, reconstruct the base
1134 we can use for TBAA and an appropriately adjusted offset. */
1135 dbase2 = ref2;
1136 while (handled_component_p (dbase2))
1137 dbase2 = TREE_OPERAND (dbase2, 0);
1138 doffset1 = offset1;
1139 doffset2 = offset2;
1140 if (TREE_CODE (dbase2) == MEM_REF
1141 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1143 offset_int moff = mem_ref_offset (dbase2);
1144 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1145 if (wi::neg_p (moff))
1146 doffset1 -= (-moff).to_short_addr ();
1147 else
1148 doffset2 -= moff.to_short_addr ();
1151 /* If either reference is view-converted, give up now. */
1152 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1153 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1154 return true;
1156 /* If both references are through the same type, they do not alias
1157 if the accesses do not overlap. This does extra disambiguation
1158 for mixed/pointer accesses but requires strict aliasing.
1159 For MEM_REFs we require that the component-ref offset we computed
1160 is relative to the start of the type which we ensure by
1161 comparing rvalue and access type and disregarding the constant
1162 pointer offset. */
1163 if ((TREE_CODE (base1) != TARGET_MEM_REF
1164 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1165 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1166 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1168 if (ref1 && ref2
1169 && nonoverlapping_component_refs_p (ref1, ref2))
1170 return false;
1172 /* Do access-path based disambiguation. */
1173 if (ref1 && ref2
1174 && (handled_component_p (ref1) || handled_component_p (ref2)))
1175 return aliasing_component_refs_p (ref1,
1176 ref1_alias_set, base1_alias_set,
1177 offset1, max_size1,
1178 ref2,
1179 ref2_alias_set, base2_alias_set,
1180 offset2, max_size2, true);
1182 return true;
1185 /* Return true if two indirect references based on *PTR1
1186 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1187 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1188 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1189 in which case they are computed on-demand. REF1 and REF2
1190 if non-NULL are the complete memory reference trees. */
1192 static bool
1193 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1194 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1195 alias_set_type ref1_alias_set,
1196 alias_set_type base1_alias_set,
1197 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1198 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1199 alias_set_type ref2_alias_set,
1200 alias_set_type base2_alias_set, bool tbaa_p)
1202 tree ptr1;
1203 tree ptr2;
1204 tree ptrtype1, ptrtype2;
1206 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1207 || TREE_CODE (base1) == TARGET_MEM_REF)
1208 && (TREE_CODE (base2) == MEM_REF
1209 || TREE_CODE (base2) == TARGET_MEM_REF));
1211 ptr1 = TREE_OPERAND (base1, 0);
1212 ptr2 = TREE_OPERAND (base2, 0);
1214 /* If both bases are based on pointers they cannot alias if they may not
1215 point to the same memory object or if they point to the same object
1216 and the accesses do not overlap. */
1217 if ((!cfun || gimple_in_ssa_p (cfun))
1218 && operand_equal_p (ptr1, ptr2, 0)
1219 && (((TREE_CODE (base1) != TARGET_MEM_REF
1220 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1221 && (TREE_CODE (base2) != TARGET_MEM_REF
1222 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1223 || (TREE_CODE (base1) == TARGET_MEM_REF
1224 && TREE_CODE (base2) == TARGET_MEM_REF
1225 && (TMR_STEP (base1) == TMR_STEP (base2)
1226 || (TMR_STEP (base1) && TMR_STEP (base2)
1227 && operand_equal_p (TMR_STEP (base1),
1228 TMR_STEP (base2), 0)))
1229 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1230 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1231 && operand_equal_p (TMR_INDEX (base1),
1232 TMR_INDEX (base2), 0)))
1233 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1234 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1235 && operand_equal_p (TMR_INDEX2 (base1),
1236 TMR_INDEX2 (base2), 0))))))
1238 offset_int moff;
1239 /* The offset embedded in MEM_REFs can be negative. Bias them
1240 so that the resulting offset adjustment is positive. */
1241 moff = mem_ref_offset (base1);
1242 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1243 if (wi::neg_p (moff))
1244 offset2 += (-moff).to_short_addr ();
1245 else
1246 offset1 += moff.to_shwi ();
1247 moff = mem_ref_offset (base2);
1248 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1249 if (wi::neg_p (moff))
1250 offset1 += (-moff).to_short_addr ();
1251 else
1252 offset2 += moff.to_short_addr ();
1253 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1255 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1256 return false;
1258 /* Disambiguations that rely on strict aliasing rules follow. */
1259 if (!flag_strict_aliasing || !tbaa_p)
1260 return true;
1262 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1263 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1265 /* If the alias set for a pointer access is zero all bets are off. */
1266 if (base1_alias_set == -1)
1267 base1_alias_set = get_deref_alias_set (ptrtype1);
1268 if (base1_alias_set == 0)
1269 return true;
1270 if (base2_alias_set == -1)
1271 base2_alias_set = get_deref_alias_set (ptrtype2);
1272 if (base2_alias_set == 0)
1273 return true;
1275 /* If both references are through the same type, they do not alias
1276 if the accesses do not overlap. This does extra disambiguation
1277 for mixed/pointer accesses but requires strict aliasing. */
1278 if ((TREE_CODE (base1) != TARGET_MEM_REF
1279 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1280 && (TREE_CODE (base2) != TARGET_MEM_REF
1281 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1282 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1283 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1284 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1285 TREE_TYPE (ptrtype2)) == 1)
1286 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1288 /* Do type-based disambiguation. */
1289 if (base1_alias_set != base2_alias_set
1290 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1291 return false;
1293 /* If either reference is view-converted, give up now. */
1294 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1295 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1296 return true;
1298 if (ref1 && ref2
1299 && nonoverlapping_component_refs_p (ref1, ref2))
1300 return false;
1302 /* Do access-path based disambiguation. */
1303 if (ref1 && ref2
1304 && (handled_component_p (ref1) || handled_component_p (ref2)))
1305 return aliasing_component_refs_p (ref1,
1306 ref1_alias_set, base1_alias_set,
1307 offset1, max_size1,
1308 ref2,
1309 ref2_alias_set, base2_alias_set,
1310 offset2, max_size2, false);
1312 return true;
1315 /* Return true, if the two memory references REF1 and REF2 may alias. */
1317 bool
1318 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1320 tree base1, base2;
1321 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1322 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1323 bool var1_p, var2_p, ind1_p, ind2_p;
1325 gcc_checking_assert ((!ref1->ref
1326 || TREE_CODE (ref1->ref) == SSA_NAME
1327 || DECL_P (ref1->ref)
1328 || TREE_CODE (ref1->ref) == STRING_CST
1329 || handled_component_p (ref1->ref)
1330 || TREE_CODE (ref1->ref) == MEM_REF
1331 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1332 && (!ref2->ref
1333 || TREE_CODE (ref2->ref) == SSA_NAME
1334 || DECL_P (ref2->ref)
1335 || TREE_CODE (ref2->ref) == STRING_CST
1336 || handled_component_p (ref2->ref)
1337 || TREE_CODE (ref2->ref) == MEM_REF
1338 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1340 /* Decompose the references into their base objects and the access. */
1341 base1 = ao_ref_base (ref1);
1342 offset1 = ref1->offset;
1343 max_size1 = ref1->max_size;
1344 base2 = ao_ref_base (ref2);
1345 offset2 = ref2->offset;
1346 max_size2 = ref2->max_size;
1348 /* We can end up with registers or constants as bases for example from
1349 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1350 which is seen as a struct copy. */
1351 if (TREE_CODE (base1) == SSA_NAME
1352 || TREE_CODE (base1) == CONST_DECL
1353 || TREE_CODE (base1) == CONSTRUCTOR
1354 || TREE_CODE (base1) == ADDR_EXPR
1355 || CONSTANT_CLASS_P (base1)
1356 || TREE_CODE (base2) == SSA_NAME
1357 || TREE_CODE (base2) == CONST_DECL
1358 || TREE_CODE (base2) == CONSTRUCTOR
1359 || TREE_CODE (base2) == ADDR_EXPR
1360 || CONSTANT_CLASS_P (base2))
1361 return false;
1363 /* We can end up referring to code via function and label decls.
1364 As we likely do not properly track code aliases conservatively
1365 bail out. */
1366 if (TREE_CODE (base1) == FUNCTION_DECL
1367 || TREE_CODE (base1) == LABEL_DECL
1368 || TREE_CODE (base2) == FUNCTION_DECL
1369 || TREE_CODE (base2) == LABEL_DECL)
1370 return true;
1372 /* Two volatile accesses always conflict. */
1373 if (ref1->volatile_p
1374 && ref2->volatile_p)
1375 return true;
1377 /* Defer to simple offset based disambiguation if we have
1378 references based on two decls. Do this before defering to
1379 TBAA to handle must-alias cases in conformance with the
1380 GCC extension of allowing type-punning through unions. */
1381 var1_p = DECL_P (base1);
1382 var2_p = DECL_P (base2);
1383 if (var1_p && var2_p)
1384 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1385 ref2->ref, base2, offset2, max_size2);
1387 ind1_p = (TREE_CODE (base1) == MEM_REF
1388 || TREE_CODE (base1) == TARGET_MEM_REF);
1389 ind2_p = (TREE_CODE (base2) == MEM_REF
1390 || TREE_CODE (base2) == TARGET_MEM_REF);
1392 /* Canonicalize the pointer-vs-decl case. */
1393 if (ind1_p && var2_p)
1395 HOST_WIDE_INT tmp1;
1396 tree tmp2;
1397 ao_ref *tmp3;
1398 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1399 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1400 tmp2 = base1; base1 = base2; base2 = tmp2;
1401 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1402 var1_p = true;
1403 ind1_p = false;
1404 var2_p = false;
1405 ind2_p = true;
1408 /* First defer to TBAA if possible. */
1409 if (tbaa_p
1410 && flag_strict_aliasing
1411 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1412 ao_ref_alias_set (ref2)))
1413 return false;
1415 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1416 if (var1_p && ind2_p)
1417 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1418 offset2, max_size2,
1419 ao_ref_alias_set (ref2), -1,
1420 ref1->ref, base1,
1421 offset1, max_size1,
1422 ao_ref_alias_set (ref1),
1423 ao_ref_base_alias_set (ref1),
1424 tbaa_p);
1425 else if (ind1_p && ind2_p)
1426 return indirect_refs_may_alias_p (ref1->ref, base1,
1427 offset1, max_size1,
1428 ao_ref_alias_set (ref1), -1,
1429 ref2->ref, base2,
1430 offset2, max_size2,
1431 ao_ref_alias_set (ref2), -1,
1432 tbaa_p);
1434 /* We really do not want to end up here, but returning true is safe. */
1435 #ifdef ENABLE_CHECKING
1436 gcc_unreachable ();
1437 #else
1438 return true;
1439 #endif
1442 static bool
1443 refs_may_alias_p (tree ref1, ao_ref *ref2)
1445 ao_ref r1;
1446 ao_ref_init (&r1, ref1);
1447 return refs_may_alias_p_1 (&r1, ref2, true);
1450 bool
1451 refs_may_alias_p (tree ref1, tree ref2)
1453 ao_ref r1, r2;
1454 bool res;
1455 ao_ref_init (&r1, ref1);
1456 ao_ref_init (&r2, ref2);
1457 res = refs_may_alias_p_1 (&r1, &r2, true);
1458 if (res)
1459 ++alias_stats.refs_may_alias_p_may_alias;
1460 else
1461 ++alias_stats.refs_may_alias_p_no_alias;
1462 return res;
1465 /* Returns true if there is a anti-dependence for the STORE that
1466 executes after the LOAD. */
1468 bool
1469 refs_anti_dependent_p (tree load, tree store)
1471 ao_ref r1, r2;
1472 ao_ref_init (&r1, load);
1473 ao_ref_init (&r2, store);
1474 return refs_may_alias_p_1 (&r1, &r2, false);
1477 /* Returns true if there is a output dependence for the stores
1478 STORE1 and STORE2. */
1480 bool
1481 refs_output_dependent_p (tree store1, tree store2)
1483 ao_ref r1, r2;
1484 ao_ref_init (&r1, store1);
1485 ao_ref_init (&r2, store2);
1486 return refs_may_alias_p_1 (&r1, &r2, false);
1489 /* If the call CALL may use the memory reference REF return true,
1490 otherwise return false. */
1492 static bool
1493 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1495 tree base, callee;
1496 unsigned i;
1497 int flags = gimple_call_flags (call);
1499 /* Const functions without a static chain do not implicitly use memory. */
1500 if (!gimple_call_chain (call)
1501 && (flags & (ECF_CONST|ECF_NOVOPS)))
1502 goto process_args;
1504 base = ao_ref_base (ref);
1505 if (!base)
1506 return true;
1508 /* A call that is not without side-effects might involve volatile
1509 accesses and thus conflicts with all other volatile accesses. */
1510 if (ref->volatile_p)
1511 return true;
1513 /* If the reference is based on a decl that is not aliased the call
1514 cannot possibly use it. */
1515 if (DECL_P (base)
1516 && !may_be_aliased (base)
1517 /* But local statics can be used through recursion. */
1518 && !is_global_var (base))
1519 goto process_args;
1521 callee = gimple_call_fndecl (call);
1523 /* Handle those builtin functions explicitly that do not act as
1524 escape points. See tree-ssa-structalias.c:find_func_aliases
1525 for the list of builtins we might need to handle here. */
1526 if (callee != NULL_TREE
1527 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1528 switch (DECL_FUNCTION_CODE (callee))
1530 /* All the following functions read memory pointed to by
1531 their second argument. strcat/strncat additionally
1532 reads memory pointed to by the first argument. */
1533 case BUILT_IN_STRCAT:
1534 case BUILT_IN_STRNCAT:
1536 ao_ref dref;
1537 ao_ref_init_from_ptr_and_size (&dref,
1538 gimple_call_arg (call, 0),
1539 NULL_TREE);
1540 if (refs_may_alias_p_1 (&dref, ref, false))
1541 return true;
1543 /* FALLTHRU */
1544 case BUILT_IN_STRCPY:
1545 case BUILT_IN_STRNCPY:
1546 case BUILT_IN_MEMCPY:
1547 case BUILT_IN_MEMMOVE:
1548 case BUILT_IN_MEMPCPY:
1549 case BUILT_IN_STPCPY:
1550 case BUILT_IN_STPNCPY:
1551 case BUILT_IN_TM_MEMCPY:
1552 case BUILT_IN_TM_MEMMOVE:
1554 ao_ref dref;
1555 tree size = NULL_TREE;
1556 if (gimple_call_num_args (call) == 3)
1557 size = gimple_call_arg (call, 2);
1558 ao_ref_init_from_ptr_and_size (&dref,
1559 gimple_call_arg (call, 1),
1560 size);
1561 return refs_may_alias_p_1 (&dref, ref, false);
1563 case BUILT_IN_STRCAT_CHK:
1564 case BUILT_IN_STRNCAT_CHK:
1566 ao_ref dref;
1567 ao_ref_init_from_ptr_and_size (&dref,
1568 gimple_call_arg (call, 0),
1569 NULL_TREE);
1570 if (refs_may_alias_p_1 (&dref, ref, false))
1571 return true;
1573 /* FALLTHRU */
1574 case BUILT_IN_STRCPY_CHK:
1575 case BUILT_IN_STRNCPY_CHK:
1576 case BUILT_IN_MEMCPY_CHK:
1577 case BUILT_IN_MEMMOVE_CHK:
1578 case BUILT_IN_MEMPCPY_CHK:
1579 case BUILT_IN_STPCPY_CHK:
1580 case BUILT_IN_STPNCPY_CHK:
1582 ao_ref dref;
1583 tree size = NULL_TREE;
1584 if (gimple_call_num_args (call) == 4)
1585 size = gimple_call_arg (call, 2);
1586 ao_ref_init_from_ptr_and_size (&dref,
1587 gimple_call_arg (call, 1),
1588 size);
1589 return refs_may_alias_p_1 (&dref, ref, false);
1591 case BUILT_IN_BCOPY:
1593 ao_ref dref;
1594 tree size = gimple_call_arg (call, 2);
1595 ao_ref_init_from_ptr_and_size (&dref,
1596 gimple_call_arg (call, 0),
1597 size);
1598 return refs_may_alias_p_1 (&dref, ref, false);
1601 /* The following functions read memory pointed to by their
1602 first argument. */
1603 CASE_BUILT_IN_TM_LOAD (1):
1604 CASE_BUILT_IN_TM_LOAD (2):
1605 CASE_BUILT_IN_TM_LOAD (4):
1606 CASE_BUILT_IN_TM_LOAD (8):
1607 CASE_BUILT_IN_TM_LOAD (FLOAT):
1608 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1609 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1610 CASE_BUILT_IN_TM_LOAD (M64):
1611 CASE_BUILT_IN_TM_LOAD (M128):
1612 CASE_BUILT_IN_TM_LOAD (M256):
1613 case BUILT_IN_TM_LOG:
1614 case BUILT_IN_TM_LOG_1:
1615 case BUILT_IN_TM_LOG_2:
1616 case BUILT_IN_TM_LOG_4:
1617 case BUILT_IN_TM_LOG_8:
1618 case BUILT_IN_TM_LOG_FLOAT:
1619 case BUILT_IN_TM_LOG_DOUBLE:
1620 case BUILT_IN_TM_LOG_LDOUBLE:
1621 case BUILT_IN_TM_LOG_M64:
1622 case BUILT_IN_TM_LOG_M128:
1623 case BUILT_IN_TM_LOG_M256:
1624 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1626 /* These read memory pointed to by the first argument. */
1627 case BUILT_IN_STRDUP:
1628 case BUILT_IN_STRNDUP:
1629 case BUILT_IN_REALLOC:
1631 ao_ref dref;
1632 tree size = NULL_TREE;
1633 if (gimple_call_num_args (call) == 2)
1634 size = gimple_call_arg (call, 1);
1635 ao_ref_init_from_ptr_and_size (&dref,
1636 gimple_call_arg (call, 0),
1637 size);
1638 return refs_may_alias_p_1 (&dref, ref, false);
1640 /* These read memory pointed to by the first argument. */
1641 case BUILT_IN_INDEX:
1642 case BUILT_IN_STRCHR:
1643 case BUILT_IN_STRRCHR:
1645 ao_ref dref;
1646 ao_ref_init_from_ptr_and_size (&dref,
1647 gimple_call_arg (call, 0),
1648 NULL_TREE);
1649 return refs_may_alias_p_1 (&dref, ref, false);
1651 /* These read memory pointed to by the first argument with size
1652 in the third argument. */
1653 case BUILT_IN_MEMCHR:
1655 ao_ref dref;
1656 ao_ref_init_from_ptr_and_size (&dref,
1657 gimple_call_arg (call, 0),
1658 gimple_call_arg (call, 2));
1659 return refs_may_alias_p_1 (&dref, ref, false);
1661 /* These read memory pointed to by the first and second arguments. */
1662 case BUILT_IN_STRSTR:
1663 case BUILT_IN_STRPBRK:
1665 ao_ref dref;
1666 ao_ref_init_from_ptr_and_size (&dref,
1667 gimple_call_arg (call, 0),
1668 NULL_TREE);
1669 if (refs_may_alias_p_1 (&dref, ref, false))
1670 return true;
1671 ao_ref_init_from_ptr_and_size (&dref,
1672 gimple_call_arg (call, 1),
1673 NULL_TREE);
1674 return refs_may_alias_p_1 (&dref, ref, false);
1677 /* The following builtins do not read from memory. */
1678 case BUILT_IN_FREE:
1679 case BUILT_IN_MALLOC:
1680 case BUILT_IN_POSIX_MEMALIGN:
1681 case BUILT_IN_ALIGNED_ALLOC:
1682 case BUILT_IN_CALLOC:
1683 case BUILT_IN_ALLOCA:
1684 case BUILT_IN_ALLOCA_WITH_ALIGN:
1685 case BUILT_IN_STACK_SAVE:
1686 case BUILT_IN_STACK_RESTORE:
1687 case BUILT_IN_MEMSET:
1688 case BUILT_IN_TM_MEMSET:
1689 case BUILT_IN_MEMSET_CHK:
1690 case BUILT_IN_FREXP:
1691 case BUILT_IN_FREXPF:
1692 case BUILT_IN_FREXPL:
1693 case BUILT_IN_GAMMA_R:
1694 case BUILT_IN_GAMMAF_R:
1695 case BUILT_IN_GAMMAL_R:
1696 case BUILT_IN_LGAMMA_R:
1697 case BUILT_IN_LGAMMAF_R:
1698 case BUILT_IN_LGAMMAL_R:
1699 case BUILT_IN_MODF:
1700 case BUILT_IN_MODFF:
1701 case BUILT_IN_MODFL:
1702 case BUILT_IN_REMQUO:
1703 case BUILT_IN_REMQUOF:
1704 case BUILT_IN_REMQUOL:
1705 case BUILT_IN_SINCOS:
1706 case BUILT_IN_SINCOSF:
1707 case BUILT_IN_SINCOSL:
1708 case BUILT_IN_ASSUME_ALIGNED:
1709 case BUILT_IN_VA_END:
1710 return false;
1711 /* __sync_* builtins and some OpenMP builtins act as threading
1712 barriers. */
1713 #undef DEF_SYNC_BUILTIN
1714 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1715 #include "sync-builtins.def"
1716 #undef DEF_SYNC_BUILTIN
1717 case BUILT_IN_GOMP_ATOMIC_START:
1718 case BUILT_IN_GOMP_ATOMIC_END:
1719 case BUILT_IN_GOMP_BARRIER:
1720 case BUILT_IN_GOMP_BARRIER_CANCEL:
1721 case BUILT_IN_GOMP_TASKWAIT:
1722 case BUILT_IN_GOMP_TASKGROUP_END:
1723 case BUILT_IN_GOMP_CRITICAL_START:
1724 case BUILT_IN_GOMP_CRITICAL_END:
1725 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1726 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1727 case BUILT_IN_GOMP_LOOP_END:
1728 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1729 case BUILT_IN_GOMP_ORDERED_START:
1730 case BUILT_IN_GOMP_ORDERED_END:
1731 case BUILT_IN_GOMP_SECTIONS_END:
1732 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1733 case BUILT_IN_GOMP_SINGLE_COPY_START:
1734 case BUILT_IN_GOMP_SINGLE_COPY_END:
1735 return true;
1737 default:
1738 /* Fallthru to general call handling. */;
1741 /* Check if base is a global static variable that is not read
1742 by the function. */
1743 if (callee != NULL_TREE
1744 && TREE_CODE (base) == VAR_DECL
1745 && TREE_STATIC (base))
1747 struct cgraph_node *node = cgraph_node::get (callee);
1748 bitmap not_read;
1750 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1751 node yet. We should enforce that there are nodes for all decls in the
1752 IL and remove this check instead. */
1753 if (node
1754 && (not_read = ipa_reference_get_not_read_global (node))
1755 && bitmap_bit_p (not_read, DECL_UID (base)))
1756 goto process_args;
1759 /* Check if the base variable is call-used. */
1760 if (DECL_P (base))
1762 if (pt_solution_includes (gimple_call_use_set (call), base))
1763 return true;
1765 else if ((TREE_CODE (base) == MEM_REF
1766 || TREE_CODE (base) == TARGET_MEM_REF)
1767 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1769 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1770 if (!pi)
1771 return true;
1773 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1774 return true;
1776 else
1777 return true;
1779 /* Inspect call arguments for passed-by-value aliases. */
1780 process_args:
1781 for (i = 0; i < gimple_call_num_args (call); ++i)
1783 tree op = gimple_call_arg (call, i);
1784 int flags = gimple_call_arg_flags (call, i);
1786 if (flags & EAF_UNUSED)
1787 continue;
1789 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1790 op = TREE_OPERAND (op, 0);
1792 if (TREE_CODE (op) != SSA_NAME
1793 && !is_gimple_min_invariant (op))
1795 ao_ref r;
1796 ao_ref_init (&r, op);
1797 if (refs_may_alias_p_1 (&r, ref, true))
1798 return true;
1802 return false;
1805 static bool
1806 ref_maybe_used_by_call_p (gimple call, ao_ref *ref)
1808 bool res;
1809 res = ref_maybe_used_by_call_p_1 (call, ref);
1810 if (res)
1811 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1812 else
1813 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1814 return res;
1818 /* If the statement STMT may use the memory reference REF return
1819 true, otherwise return false. */
1821 bool
1822 ref_maybe_used_by_stmt_p (gimple stmt, ao_ref *ref)
1824 if (is_gimple_assign (stmt))
1826 tree rhs;
1828 /* All memory assign statements are single. */
1829 if (!gimple_assign_single_p (stmt))
1830 return false;
1832 rhs = gimple_assign_rhs1 (stmt);
1833 if (is_gimple_reg (rhs)
1834 || is_gimple_min_invariant (rhs)
1835 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1836 return false;
1838 return refs_may_alias_p (rhs, ref);
1840 else if (is_gimple_call (stmt))
1841 return ref_maybe_used_by_call_p (stmt, ref);
1842 else if (gimple_code (stmt) == GIMPLE_RETURN)
1844 tree retval = gimple_return_retval (stmt);
1845 if (retval
1846 && TREE_CODE (retval) != SSA_NAME
1847 && !is_gimple_min_invariant (retval)
1848 && refs_may_alias_p (retval, ref))
1849 return true;
1850 /* If ref escapes the function then the return acts as a use. */
1851 tree base = ao_ref_base (ref);
1852 if (!base)
1854 else if (DECL_P (base))
1855 return is_global_var (base);
1856 else if (TREE_CODE (base) == MEM_REF
1857 || TREE_CODE (base) == TARGET_MEM_REF)
1858 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1859 return false;
1862 return true;
1865 bool
1866 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1868 ao_ref r;
1869 ao_ref_init (&r, ref);
1870 return ref_maybe_used_by_stmt_p (stmt, &r);
1873 /* If the call in statement CALL may clobber the memory reference REF
1874 return true, otherwise return false. */
1876 bool
1877 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1879 tree base;
1880 tree callee;
1882 /* If the call is pure or const it cannot clobber anything. */
1883 if (gimple_call_flags (call)
1884 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1885 return false;
1887 base = ao_ref_base (ref);
1888 if (!base)
1889 return true;
1891 if (TREE_CODE (base) == SSA_NAME
1892 || CONSTANT_CLASS_P (base))
1893 return false;
1895 /* A call that is not without side-effects might involve volatile
1896 accesses and thus conflicts with all other volatile accesses. */
1897 if (ref->volatile_p)
1898 return true;
1900 /* If the reference is based on a decl that is not aliased the call
1901 cannot possibly clobber it. */
1902 if (DECL_P (base)
1903 && !may_be_aliased (base)
1904 /* But local non-readonly statics can be modified through recursion
1905 or the call may implement a threading barrier which we must
1906 treat as may-def. */
1907 && (TREE_READONLY (base)
1908 || !is_global_var (base)))
1909 return false;
1911 callee = gimple_call_fndecl (call);
1913 /* Handle those builtin functions explicitly that do not act as
1914 escape points. See tree-ssa-structalias.c:find_func_aliases
1915 for the list of builtins we might need to handle here. */
1916 if (callee != NULL_TREE
1917 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1918 switch (DECL_FUNCTION_CODE (callee))
1920 /* All the following functions clobber memory pointed to by
1921 their first argument. */
1922 case BUILT_IN_STRCPY:
1923 case BUILT_IN_STRNCPY:
1924 case BUILT_IN_MEMCPY:
1925 case BUILT_IN_MEMMOVE:
1926 case BUILT_IN_MEMPCPY:
1927 case BUILT_IN_STPCPY:
1928 case BUILT_IN_STPNCPY:
1929 case BUILT_IN_STRCAT:
1930 case BUILT_IN_STRNCAT:
1931 case BUILT_IN_MEMSET:
1932 case BUILT_IN_TM_MEMSET:
1933 CASE_BUILT_IN_TM_STORE (1):
1934 CASE_BUILT_IN_TM_STORE (2):
1935 CASE_BUILT_IN_TM_STORE (4):
1936 CASE_BUILT_IN_TM_STORE (8):
1937 CASE_BUILT_IN_TM_STORE (FLOAT):
1938 CASE_BUILT_IN_TM_STORE (DOUBLE):
1939 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1940 CASE_BUILT_IN_TM_STORE (M64):
1941 CASE_BUILT_IN_TM_STORE (M128):
1942 CASE_BUILT_IN_TM_STORE (M256):
1943 case BUILT_IN_TM_MEMCPY:
1944 case BUILT_IN_TM_MEMMOVE:
1946 ao_ref dref;
1947 tree size = NULL_TREE;
1948 /* Don't pass in size for strncat, as the maximum size
1949 is strlen (dest) + n + 1 instead of n, resp.
1950 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1951 known. */
1952 if (gimple_call_num_args (call) == 3
1953 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1954 size = gimple_call_arg (call, 2);
1955 ao_ref_init_from_ptr_and_size (&dref,
1956 gimple_call_arg (call, 0),
1957 size);
1958 return refs_may_alias_p_1 (&dref, ref, false);
1960 case BUILT_IN_STRCPY_CHK:
1961 case BUILT_IN_STRNCPY_CHK:
1962 case BUILT_IN_MEMCPY_CHK:
1963 case BUILT_IN_MEMMOVE_CHK:
1964 case BUILT_IN_MEMPCPY_CHK:
1965 case BUILT_IN_STPCPY_CHK:
1966 case BUILT_IN_STPNCPY_CHK:
1967 case BUILT_IN_STRCAT_CHK:
1968 case BUILT_IN_STRNCAT_CHK:
1969 case BUILT_IN_MEMSET_CHK:
1971 ao_ref dref;
1972 tree size = NULL_TREE;
1973 /* Don't pass in size for __strncat_chk, as the maximum size
1974 is strlen (dest) + n + 1 instead of n, resp.
1975 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1976 known. */
1977 if (gimple_call_num_args (call) == 4
1978 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1979 size = gimple_call_arg (call, 2);
1980 ao_ref_init_from_ptr_and_size (&dref,
1981 gimple_call_arg (call, 0),
1982 size);
1983 return refs_may_alias_p_1 (&dref, ref, false);
1985 case BUILT_IN_BCOPY:
1987 ao_ref dref;
1988 tree size = gimple_call_arg (call, 2);
1989 ao_ref_init_from_ptr_and_size (&dref,
1990 gimple_call_arg (call, 1),
1991 size);
1992 return refs_may_alias_p_1 (&dref, ref, false);
1994 /* Allocating memory does not have any side-effects apart from
1995 being the definition point for the pointer. */
1996 case BUILT_IN_MALLOC:
1997 case BUILT_IN_ALIGNED_ALLOC:
1998 case BUILT_IN_CALLOC:
1999 case BUILT_IN_STRDUP:
2000 case BUILT_IN_STRNDUP:
2001 /* Unix98 specifies that errno is set on allocation failure. */
2002 if (flag_errno_math
2003 && targetm.ref_may_alias_errno (ref))
2004 return true;
2005 return false;
2006 case BUILT_IN_STACK_SAVE:
2007 case BUILT_IN_ALLOCA:
2008 case BUILT_IN_ALLOCA_WITH_ALIGN:
2009 case BUILT_IN_ASSUME_ALIGNED:
2010 return false;
2011 /* But posix_memalign stores a pointer into the memory pointed to
2012 by its first argument. */
2013 case BUILT_IN_POSIX_MEMALIGN:
2015 tree ptrptr = gimple_call_arg (call, 0);
2016 ao_ref dref;
2017 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2018 TYPE_SIZE_UNIT (ptr_type_node));
2019 return (refs_may_alias_p_1 (&dref, ref, false)
2020 || (flag_errno_math
2021 && targetm.ref_may_alias_errno (ref)));
2023 /* Freeing memory kills the pointed-to memory. More importantly
2024 the call has to serve as a barrier for moving loads and stores
2025 across it. */
2026 case BUILT_IN_FREE:
2027 case BUILT_IN_VA_END:
2029 tree ptr = gimple_call_arg (call, 0);
2030 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2032 /* Realloc serves both as allocation point and deallocation point. */
2033 case BUILT_IN_REALLOC:
2035 tree ptr = gimple_call_arg (call, 0);
2036 /* Unix98 specifies that errno is set on allocation failure. */
2037 return ((flag_errno_math
2038 && targetm.ref_may_alias_errno (ref))
2039 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2041 case BUILT_IN_GAMMA_R:
2042 case BUILT_IN_GAMMAF_R:
2043 case BUILT_IN_GAMMAL_R:
2044 case BUILT_IN_LGAMMA_R:
2045 case BUILT_IN_LGAMMAF_R:
2046 case BUILT_IN_LGAMMAL_R:
2048 tree out = gimple_call_arg (call, 1);
2049 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2050 return true;
2051 if (flag_errno_math)
2052 break;
2053 return false;
2055 case BUILT_IN_FREXP:
2056 case BUILT_IN_FREXPF:
2057 case BUILT_IN_FREXPL:
2058 case BUILT_IN_MODF:
2059 case BUILT_IN_MODFF:
2060 case BUILT_IN_MODFL:
2062 tree out = gimple_call_arg (call, 1);
2063 return ptr_deref_may_alias_ref_p_1 (out, ref);
2065 case BUILT_IN_REMQUO:
2066 case BUILT_IN_REMQUOF:
2067 case BUILT_IN_REMQUOL:
2069 tree out = gimple_call_arg (call, 2);
2070 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2071 return true;
2072 if (flag_errno_math)
2073 break;
2074 return false;
2076 case BUILT_IN_SINCOS:
2077 case BUILT_IN_SINCOSF:
2078 case BUILT_IN_SINCOSL:
2080 tree sin = gimple_call_arg (call, 1);
2081 tree cos = gimple_call_arg (call, 2);
2082 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2083 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2085 /* __sync_* builtins and some OpenMP builtins act as threading
2086 barriers. */
2087 #undef DEF_SYNC_BUILTIN
2088 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2089 #include "sync-builtins.def"
2090 #undef DEF_SYNC_BUILTIN
2091 case BUILT_IN_GOMP_ATOMIC_START:
2092 case BUILT_IN_GOMP_ATOMIC_END:
2093 case BUILT_IN_GOMP_BARRIER:
2094 case BUILT_IN_GOMP_BARRIER_CANCEL:
2095 case BUILT_IN_GOMP_TASKWAIT:
2096 case BUILT_IN_GOMP_TASKGROUP_END:
2097 case BUILT_IN_GOMP_CRITICAL_START:
2098 case BUILT_IN_GOMP_CRITICAL_END:
2099 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2100 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2101 case BUILT_IN_GOMP_LOOP_END:
2102 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2103 case BUILT_IN_GOMP_ORDERED_START:
2104 case BUILT_IN_GOMP_ORDERED_END:
2105 case BUILT_IN_GOMP_SECTIONS_END:
2106 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2107 case BUILT_IN_GOMP_SINGLE_COPY_START:
2108 case BUILT_IN_GOMP_SINGLE_COPY_END:
2109 return true;
2110 default:
2111 /* Fallthru to general call handling. */;
2114 /* Check if base is a global static variable that is not written
2115 by the function. */
2116 if (callee != NULL_TREE
2117 && TREE_CODE (base) == VAR_DECL
2118 && TREE_STATIC (base))
2120 struct cgraph_node *node = cgraph_node::get (callee);
2121 bitmap not_written;
2123 if (node
2124 && (not_written = ipa_reference_get_not_written_global (node))
2125 && bitmap_bit_p (not_written, DECL_UID (base)))
2126 return false;
2129 /* Check if the base variable is call-clobbered. */
2130 if (DECL_P (base))
2131 return pt_solution_includes (gimple_call_clobber_set (call), base);
2132 else if ((TREE_CODE (base) == MEM_REF
2133 || TREE_CODE (base) == TARGET_MEM_REF)
2134 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2136 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2137 if (!pi)
2138 return true;
2140 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2143 return true;
2146 /* If the call in statement CALL may clobber the memory reference REF
2147 return true, otherwise return false. */
2149 bool
2150 call_may_clobber_ref_p (gimple call, tree ref)
2152 bool res;
2153 ao_ref r;
2154 ao_ref_init (&r, ref);
2155 res = call_may_clobber_ref_p_1 (call, &r);
2156 if (res)
2157 ++alias_stats.call_may_clobber_ref_p_may_alias;
2158 else
2159 ++alias_stats.call_may_clobber_ref_p_no_alias;
2160 return res;
2164 /* If the statement STMT may clobber the memory reference REF return true,
2165 otherwise return false. */
2167 bool
2168 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2170 if (is_gimple_call (stmt))
2172 tree lhs = gimple_call_lhs (stmt);
2173 if (lhs
2174 && TREE_CODE (lhs) != SSA_NAME)
2176 ao_ref r;
2177 ao_ref_init (&r, lhs);
2178 if (refs_may_alias_p_1 (ref, &r, true))
2179 return true;
2182 return call_may_clobber_ref_p_1 (stmt, ref);
2184 else if (gimple_assign_single_p (stmt))
2186 tree lhs = gimple_assign_lhs (stmt);
2187 if (TREE_CODE (lhs) != SSA_NAME)
2189 ao_ref r;
2190 ao_ref_init (&r, lhs);
2191 return refs_may_alias_p_1 (ref, &r, true);
2194 else if (gimple_code (stmt) == GIMPLE_ASM)
2195 return true;
2197 return false;
2200 bool
2201 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2203 ao_ref r;
2204 ao_ref_init (&r, ref);
2205 return stmt_may_clobber_ref_p_1 (stmt, &r);
2208 /* If STMT kills the memory reference REF return true, otherwise
2209 return false. */
2211 bool
2212 stmt_kills_ref_p (gimple stmt, ao_ref *ref)
2214 if (!ao_ref_base (ref))
2215 return false;
2217 if (gimple_has_lhs (stmt)
2218 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2219 /* The assignment is not necessarily carried out if it can throw
2220 and we can catch it in the current function where we could inspect
2221 the previous value.
2222 ??? We only need to care about the RHS throwing. For aggregate
2223 assignments or similar calls and non-call exceptions the LHS
2224 might throw as well. */
2225 && !stmt_can_throw_internal (stmt))
2227 tree lhs = gimple_get_lhs (stmt);
2228 /* If LHS is literally a base of the access we are done. */
2229 if (ref->ref)
2231 tree base = ref->ref;
2232 if (handled_component_p (base))
2234 tree saved_lhs0 = NULL_TREE;
2235 if (handled_component_p (lhs))
2237 saved_lhs0 = TREE_OPERAND (lhs, 0);
2238 TREE_OPERAND (lhs, 0) = integer_zero_node;
2242 /* Just compare the outermost handled component, if
2243 they are equal we have found a possible common
2244 base. */
2245 tree saved_base0 = TREE_OPERAND (base, 0);
2246 TREE_OPERAND (base, 0) = integer_zero_node;
2247 bool res = operand_equal_p (lhs, base, 0);
2248 TREE_OPERAND (base, 0) = saved_base0;
2249 if (res)
2250 break;
2251 /* Otherwise drop handled components of the access. */
2252 base = saved_base0;
2254 while (handled_component_p (base));
2255 if (saved_lhs0)
2256 TREE_OPERAND (lhs, 0) = saved_lhs0;
2258 /* Finally check if lhs is equal or equal to the base candidate
2259 of the access. */
2260 if (operand_equal_p (lhs, base, 0))
2261 return true;
2264 /* Now look for non-literal equal bases with the restriction of
2265 handling constant offset and size. */
2266 /* For a must-alias check we need to be able to constrain
2267 the access properly. */
2268 if (ref->max_size == -1)
2269 return false;
2270 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2271 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2272 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2273 so base == ref->base does not always hold. */
2274 if (base != ref->base)
2276 /* If both base and ref->base are MEM_REFs, only compare the
2277 first operand, and if the second operand isn't equal constant,
2278 try to add the offsets into offset and ref_offset. */
2279 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2280 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2282 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2283 TREE_OPERAND (ref->base, 1)))
2285 offset_int off1 = mem_ref_offset (base);
2286 off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2287 off1 += offset;
2288 offset_int off2 = mem_ref_offset (ref->base);
2289 off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2290 off2 += ref_offset;
2291 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2293 offset = off1.to_shwi ();
2294 ref_offset = off2.to_shwi ();
2296 else
2297 size = -1;
2300 else
2301 size = -1;
2303 /* For a must-alias check we need to be able to constrain
2304 the access properly. */
2305 if (size != -1 && size == max_size)
2307 if (offset <= ref_offset
2308 && offset + size >= ref_offset + ref->max_size)
2309 return true;
2313 if (is_gimple_call (stmt))
2315 tree callee = gimple_call_fndecl (stmt);
2316 if (callee != NULL_TREE
2317 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2318 switch (DECL_FUNCTION_CODE (callee))
2320 case BUILT_IN_FREE:
2322 tree ptr = gimple_call_arg (stmt, 0);
2323 tree base = ao_ref_base (ref);
2324 if (base && TREE_CODE (base) == MEM_REF
2325 && TREE_OPERAND (base, 0) == ptr)
2326 return true;
2327 break;
2330 case BUILT_IN_MEMCPY:
2331 case BUILT_IN_MEMPCPY:
2332 case BUILT_IN_MEMMOVE:
2333 case BUILT_IN_MEMSET:
2334 case BUILT_IN_MEMCPY_CHK:
2335 case BUILT_IN_MEMPCPY_CHK:
2336 case BUILT_IN_MEMMOVE_CHK:
2337 case BUILT_IN_MEMSET_CHK:
2339 /* For a must-alias check we need to be able to constrain
2340 the access properly. */
2341 if (ref->max_size == -1)
2342 return false;
2343 tree dest = gimple_call_arg (stmt, 0);
2344 tree len = gimple_call_arg (stmt, 2);
2345 if (!tree_fits_shwi_p (len))
2346 return false;
2347 tree rbase = ref->base;
2348 offset_int roffset = ref->offset;
2349 ao_ref dref;
2350 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2351 tree base = ao_ref_base (&dref);
2352 offset_int offset = dref.offset;
2353 if (!base || dref.size == -1)
2354 return false;
2355 if (TREE_CODE (base) == MEM_REF)
2357 if (TREE_CODE (rbase) != MEM_REF)
2358 return false;
2359 // Compare pointers.
2360 offset += wi::lshift (mem_ref_offset (base),
2361 LOG2_BITS_PER_UNIT);
2362 roffset += wi::lshift (mem_ref_offset (rbase),
2363 LOG2_BITS_PER_UNIT);
2364 base = TREE_OPERAND (base, 0);
2365 rbase = TREE_OPERAND (rbase, 0);
2367 if (base == rbase
2368 && wi::les_p (offset, roffset)
2369 && wi::les_p (roffset + ref->max_size,
2370 offset + wi::lshift (wi::to_offset (len),
2371 LOG2_BITS_PER_UNIT)))
2372 return true;
2373 break;
2376 case BUILT_IN_VA_END:
2378 tree ptr = gimple_call_arg (stmt, 0);
2379 if (TREE_CODE (ptr) == ADDR_EXPR)
2381 tree base = ao_ref_base (ref);
2382 if (TREE_OPERAND (ptr, 0) == base)
2383 return true;
2385 break;
2388 default:;
2391 return false;
2394 bool
2395 stmt_kills_ref_p (gimple stmt, tree ref)
2397 ao_ref r;
2398 ao_ref_init (&r, ref);
2399 return stmt_kills_ref_p (stmt, &r);
2403 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2404 TARGET or a statement clobbering the memory reference REF in which
2405 case false is returned. The walk starts with VUSE, one argument of PHI. */
2407 static bool
2408 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2409 tree vuse, unsigned int *cnt, bitmap *visited,
2410 bool abort_on_visited,
2411 void *(*translate)(ao_ref *, tree, void *, bool),
2412 void *data)
2414 basic_block bb = gimple_bb (phi);
2416 if (!*visited)
2417 *visited = BITMAP_ALLOC (NULL);
2419 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2421 /* Walk until we hit the target. */
2422 while (vuse != target)
2424 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2425 /* Recurse for PHI nodes. */
2426 if (gimple_code (def_stmt) == GIMPLE_PHI)
2428 /* An already visited PHI node ends the walk successfully. */
2429 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2430 return !abort_on_visited;
2431 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2432 visited, abort_on_visited,
2433 translate, data);
2434 if (!vuse)
2435 return false;
2436 continue;
2438 else if (gimple_nop_p (def_stmt))
2439 return false;
2440 else
2442 /* A clobbering statement or the end of the IL ends it failing. */
2443 ++*cnt;
2444 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2446 if (translate
2447 && (*translate) (ref, vuse, data, true) == NULL)
2449 else
2450 return false;
2453 /* If we reach a new basic-block see if we already skipped it
2454 in a previous walk that ended successfully. */
2455 if (gimple_bb (def_stmt) != bb)
2457 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2458 return !abort_on_visited;
2459 bb = gimple_bb (def_stmt);
2461 vuse = gimple_vuse (def_stmt);
2463 return true;
2466 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2467 until we hit the phi argument definition that dominates the other one.
2468 Return that, or NULL_TREE if there is no such definition. */
2470 static tree
2471 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2472 ao_ref *ref, unsigned int *cnt,
2473 bitmap *visited, bool abort_on_visited,
2474 void *(*translate)(ao_ref *, tree, void *, bool),
2475 void *data)
2477 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2478 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2479 tree common_vuse;
2481 if (arg0 == arg1)
2482 return arg0;
2483 else if (gimple_nop_p (def0)
2484 || (!gimple_nop_p (def1)
2485 && dominated_by_p (CDI_DOMINATORS,
2486 gimple_bb (def1), gimple_bb (def0))))
2488 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2489 visited, abort_on_visited, translate, data))
2490 return arg0;
2492 else if (gimple_nop_p (def1)
2493 || dominated_by_p (CDI_DOMINATORS,
2494 gimple_bb (def0), gimple_bb (def1)))
2496 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2497 visited, abort_on_visited, translate, data))
2498 return arg1;
2500 /* Special case of a diamond:
2501 MEM_1 = ...
2502 goto (cond) ? L1 : L2
2503 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2504 goto L3
2505 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2506 L3: MEM_4 = PHI<MEM_2, MEM_3>
2507 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2508 dominate each other, but still we can easily skip this PHI node
2509 if we recognize that the vuse MEM operand is the same for both,
2510 and that we can skip both statements (they don't clobber us).
2511 This is still linear. Don't use maybe_skip_until, that might
2512 potentially be slow. */
2513 else if ((common_vuse = gimple_vuse (def0))
2514 && common_vuse == gimple_vuse (def1))
2516 *cnt += 2;
2517 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2518 || (translate
2519 && (*translate) (ref, arg0, data, true) == NULL))
2520 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2521 || (translate
2522 && (*translate) (ref, arg1, data, true) == NULL)))
2523 return common_vuse;
2526 return NULL_TREE;
2530 /* Starting from a PHI node for the virtual operand of the memory reference
2531 REF find a continuation virtual operand that allows to continue walking
2532 statements dominating PHI skipping only statements that cannot possibly
2533 clobber REF. Increments *CNT for each alias disambiguation done.
2534 Returns NULL_TREE if no suitable virtual operand can be found. */
2536 tree
2537 get_continuation_for_phi (gimple phi, ao_ref *ref,
2538 unsigned int *cnt, bitmap *visited,
2539 bool abort_on_visited,
2540 void *(*translate)(ao_ref *, tree, void *, bool),
2541 void *data)
2543 unsigned nargs = gimple_phi_num_args (phi);
2545 /* Through a single-argument PHI we can simply look through. */
2546 if (nargs == 1)
2547 return PHI_ARG_DEF (phi, 0);
2549 /* For two or more arguments try to pairwise skip non-aliasing code
2550 until we hit the phi argument definition that dominates the other one. */
2551 else if (nargs >= 2)
2553 tree arg0, arg1;
2554 unsigned i;
2556 /* Find a candidate for the virtual operand which definition
2557 dominates those of all others. */
2558 arg0 = PHI_ARG_DEF (phi, 0);
2559 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2560 for (i = 1; i < nargs; ++i)
2562 arg1 = PHI_ARG_DEF (phi, i);
2563 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2565 arg0 = arg1;
2566 break;
2568 if (dominated_by_p (CDI_DOMINATORS,
2569 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2570 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2571 arg0 = arg1;
2574 /* Then pairwise reduce against the found candidate. */
2575 for (i = 0; i < nargs; ++i)
2577 arg1 = PHI_ARG_DEF (phi, i);
2578 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2579 cnt, visited, abort_on_visited,
2580 translate, data);
2581 if (!arg0)
2582 return NULL_TREE;
2585 return arg0;
2588 return NULL_TREE;
2591 /* Based on the memory reference REF and its virtual use VUSE call
2592 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2593 itself. That is, for each virtual use for which its defining statement
2594 does not clobber REF.
2596 WALKER is called with REF, the current virtual use and DATA. If
2597 WALKER returns non-NULL the walk stops and its result is returned.
2598 At the end of a non-successful walk NULL is returned.
2600 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2601 use which definition is a statement that may clobber REF and DATA.
2602 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2603 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2604 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2605 to adjust REF and *DATA to make that valid.
2607 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2609 void *
2610 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2611 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2612 void *(*translate)(ao_ref *, tree, void *, bool),
2613 void *data)
2615 bitmap visited = NULL;
2616 void *res;
2617 unsigned int cnt = 0;
2618 bool translated = false;
2620 timevar_push (TV_ALIAS_STMT_WALK);
2624 gimple def_stmt;
2626 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2627 res = (*walker) (ref, vuse, cnt, data);
2628 /* Abort walk. */
2629 if (res == (void *)-1)
2631 res = NULL;
2632 break;
2634 /* Lookup succeeded. */
2635 else if (res != NULL)
2636 break;
2638 def_stmt = SSA_NAME_DEF_STMT (vuse);
2639 if (gimple_nop_p (def_stmt))
2640 break;
2641 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2642 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2643 &visited, translated, translate, data);
2644 else
2646 cnt++;
2647 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2649 if (!translate)
2650 break;
2651 res = (*translate) (ref, vuse, data, false);
2652 /* Failed lookup and translation. */
2653 if (res == (void *)-1)
2655 res = NULL;
2656 break;
2658 /* Lookup succeeded. */
2659 else if (res != NULL)
2660 break;
2661 /* Translation succeeded, continue walking. */
2662 translated = true;
2664 vuse = gimple_vuse (def_stmt);
2667 while (vuse);
2669 if (visited)
2670 BITMAP_FREE (visited);
2672 timevar_pop (TV_ALIAS_STMT_WALK);
2674 return res;
2678 /* Based on the memory reference REF call WALKER for each vdef which
2679 defining statement may clobber REF, starting with VDEF. If REF
2680 is NULL_TREE, each defining statement is visited.
2682 WALKER is called with REF, the current vdef and DATA. If WALKER
2683 returns true the walk is stopped, otherwise it continues.
2685 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2686 The pointer may be NULL and then we do not track this information.
2688 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2689 PHI argument (but only one walk continues on merge points), the
2690 return value is true if any of the walks was successful.
2692 The function returns the number of statements walked. */
2694 static unsigned int
2695 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2696 bool (*walker)(ao_ref *, tree, void *), void *data,
2697 bitmap *visited, unsigned int cnt,
2698 bool *function_entry_reached)
2702 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2704 if (*visited
2705 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2706 return cnt;
2708 if (gimple_nop_p (def_stmt))
2710 if (function_entry_reached)
2711 *function_entry_reached = true;
2712 return cnt;
2714 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2716 unsigned i;
2717 if (!*visited)
2718 *visited = BITMAP_ALLOC (NULL);
2719 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2720 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2721 walker, data, visited, 0,
2722 function_entry_reached);
2723 return cnt;
2726 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2727 cnt++;
2728 if ((!ref
2729 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2730 && (*walker) (ref, vdef, data))
2731 return cnt;
2733 vdef = gimple_vuse (def_stmt);
2735 while (1);
2738 unsigned int
2739 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2740 bool (*walker)(ao_ref *, tree, void *), void *data,
2741 bitmap *visited,
2742 bool *function_entry_reached)
2744 bitmap local_visited = NULL;
2745 unsigned int ret;
2747 timevar_push (TV_ALIAS_STMT_WALK);
2749 if (function_entry_reached)
2750 *function_entry_reached = false;
2752 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2753 visited ? visited : &local_visited, 0,
2754 function_entry_reached);
2755 if (local_visited)
2756 BITMAP_FREE (local_visited);
2758 timevar_pop (TV_ALIAS_STMT_WALK);
2760 return ret;