cp/
[official-gcc.git] / gcc / tree-ssa-alias.c
blob9184242a7ee6e6e157ce561252cd20c98d0c2eff
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 "hash-set.h"
26 #include "machmode.h"
27 #include "vec.h"
28 #include "double-int.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "wide-int.h"
33 #include "inchash.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "tm_p.h"
37 #include "target.h"
38 #include "predict.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "basic-block.h"
44 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
45 #include "langhooks.h"
46 #include "flags.h"
47 #include "tree-pretty-print.h"
48 #include "dumpfile.h"
49 #include "tree-ssa-alias.h"
50 #include "internal-fn.h"
51 #include "tree-eh.h"
52 #include "gimple-expr.h"
53 #include "is-a.h"
54 #include "gimple.h"
55 #include "gimple-ssa.h"
56 #include "stringpool.h"
57 #include "tree-ssanames.h"
58 #include "hashtab.h"
59 #include "rtl.h"
60 #include "statistics.h"
61 #include "real.h"
62 #include "fixed-value.h"
63 #include "insn-config.h"
64 #include "expmed.h"
65 #include "dojump.h"
66 #include "explow.h"
67 #include "calls.h"
68 #include "emit-rtl.h"
69 #include "varasm.h"
70 #include "stmt.h"
71 #include "expr.h"
72 #include "tree-dfa.h"
73 #include "tree-inline.h"
74 #include "params.h"
75 #include "alloc-pool.h"
76 #include "bitmap.h"
77 #include "hash-map.h"
78 #include "plugin-api.h"
79 #include "ipa-ref.h"
80 #include "cgraph.h"
81 #include "ipa-reference.h"
83 /* Broad overview of how alias analysis on gimple works:
85 Statements clobbering or using memory are linked through the
86 virtual operand factored use-def chain. The virtual operand
87 is unique per function, its symbol is accessible via gimple_vop (cfun).
88 Virtual operands are used for efficiently walking memory statements
89 in the gimple IL and are useful for things like value-numbering as
90 a generation count for memory references.
92 SSA_NAME pointers may have associated points-to information
93 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
94 points-to information is (re-)computed by the TODO_rebuild_alias
95 pass manager todo. Points-to information is also used for more
96 precise tracking of call-clobbered and call-used variables and
97 related disambiguations.
99 This file contains functions for disambiguating memory references,
100 the so called alias-oracle and tools for walking of the gimple IL.
102 The main alias-oracle entry-points are
104 bool stmt_may_clobber_ref_p (gimple, tree)
106 This function queries if a statement may invalidate (parts of)
107 the memory designated by the reference tree argument.
109 bool ref_maybe_used_by_stmt_p (gimple, tree)
111 This function queries if a statement may need (parts of) the
112 memory designated by the reference tree argument.
114 There are variants of these functions that only handle the call
115 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
116 Note that these do not disambiguate against a possible call lhs.
118 bool refs_may_alias_p (tree, tree)
120 This function tries to disambiguate two reference trees.
122 bool ptr_deref_may_alias_global_p (tree)
124 This function queries if dereferencing a pointer variable may
125 alias global memory.
127 More low-level disambiguators are available and documented in
128 this file. Low-level disambiguators dealing with points-to
129 information are in tree-ssa-structalias.c. */
132 /* Query statistics for the different low-level disambiguators.
133 A high-level query may trigger multiple of them. */
135 static struct {
136 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
137 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
138 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
139 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
140 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
141 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
142 } alias_stats;
144 void
145 dump_alias_stats (FILE *s)
147 fprintf (s, "\nAlias oracle query stats:\n");
148 fprintf (s, " refs_may_alias_p: "
149 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
150 HOST_WIDE_INT_PRINT_DEC" queries\n",
151 alias_stats.refs_may_alias_p_no_alias,
152 alias_stats.refs_may_alias_p_no_alias
153 + alias_stats.refs_may_alias_p_may_alias);
154 fprintf (s, " ref_maybe_used_by_call_p: "
155 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
156 HOST_WIDE_INT_PRINT_DEC" queries\n",
157 alias_stats.ref_maybe_used_by_call_p_no_alias,
158 alias_stats.refs_may_alias_p_no_alias
159 + alias_stats.ref_maybe_used_by_call_p_may_alias);
160 fprintf (s, " call_may_clobber_ref_p: "
161 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
162 HOST_WIDE_INT_PRINT_DEC" queries\n",
163 alias_stats.call_may_clobber_ref_p_no_alias,
164 alias_stats.call_may_clobber_ref_p_no_alias
165 + alias_stats.call_may_clobber_ref_p_may_alias);
166 dump_alias_stats_in_alias_c (s);
170 /* Return true, if dereferencing PTR may alias with a global variable. */
172 bool
173 ptr_deref_may_alias_global_p (tree ptr)
175 struct ptr_info_def *pi;
177 /* If we end up with a pointer constant here that may point
178 to global memory. */
179 if (TREE_CODE (ptr) != SSA_NAME)
180 return true;
182 pi = SSA_NAME_PTR_INFO (ptr);
184 /* If we do not have points-to information for this variable,
185 we have to punt. */
186 if (!pi)
187 return true;
189 /* ??? This does not use TBAA to prune globals ptr may not access. */
190 return pt_solution_includes_global (&pi->pt);
193 /* Return true if dereferencing PTR may alias DECL.
194 The caller is responsible for applying TBAA to see if PTR
195 may access DECL at all. */
197 static bool
198 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
200 struct ptr_info_def *pi;
202 /* Conversions are irrelevant for points-to information and
203 data-dependence analysis can feed us those. */
204 STRIP_NOPS (ptr);
206 /* Anything we do not explicilty handle aliases. */
207 if ((TREE_CODE (ptr) != SSA_NAME
208 && TREE_CODE (ptr) != ADDR_EXPR
209 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
210 || !POINTER_TYPE_P (TREE_TYPE (ptr))
211 || (TREE_CODE (decl) != VAR_DECL
212 && TREE_CODE (decl) != PARM_DECL
213 && TREE_CODE (decl) != RESULT_DECL))
214 return true;
216 /* Disregard pointer offsetting. */
217 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
221 ptr = TREE_OPERAND (ptr, 0);
223 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
224 return ptr_deref_may_alias_decl_p (ptr, decl);
227 /* ADDR_EXPR pointers either just offset another pointer or directly
228 specify the pointed-to set. */
229 if (TREE_CODE (ptr) == ADDR_EXPR)
231 tree base = get_base_address (TREE_OPERAND (ptr, 0));
232 if (base
233 && (TREE_CODE (base) == MEM_REF
234 || TREE_CODE (base) == TARGET_MEM_REF))
235 ptr = TREE_OPERAND (base, 0);
236 else if (base
237 && DECL_P (base))
238 return base == decl;
239 else if (base
240 && CONSTANT_CLASS_P (base))
241 return false;
242 else
243 return true;
246 /* Non-aliased variables can not be pointed to. */
247 if (!may_be_aliased (decl))
248 return false;
250 /* If we do not have useful points-to information for this pointer
251 we cannot disambiguate anything else. */
252 pi = SSA_NAME_PTR_INFO (ptr);
253 if (!pi)
254 return true;
256 return pt_solution_includes (&pi->pt, decl);
259 /* Return true if dereferenced PTR1 and PTR2 may alias.
260 The caller is responsible for applying TBAA to see if accesses
261 through PTR1 and PTR2 may conflict at all. */
263 bool
264 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
266 struct ptr_info_def *pi1, *pi2;
268 /* Conversions are irrelevant for points-to information and
269 data-dependence analysis can feed us those. */
270 STRIP_NOPS (ptr1);
271 STRIP_NOPS (ptr2);
273 /* Disregard pointer offsetting. */
274 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
278 ptr1 = TREE_OPERAND (ptr1, 0);
280 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
281 return ptr_derefs_may_alias_p (ptr1, ptr2);
283 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
287 ptr2 = TREE_OPERAND (ptr2, 0);
289 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
290 return ptr_derefs_may_alias_p (ptr1, ptr2);
293 /* ADDR_EXPR pointers either just offset another pointer or directly
294 specify the pointed-to set. */
295 if (TREE_CODE (ptr1) == ADDR_EXPR)
297 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
298 if (base
299 && (TREE_CODE (base) == MEM_REF
300 || TREE_CODE (base) == TARGET_MEM_REF))
301 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
302 else if (base
303 && DECL_P (base))
304 return ptr_deref_may_alias_decl_p (ptr2, base);
305 else
306 return true;
308 if (TREE_CODE (ptr2) == ADDR_EXPR)
310 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
311 if (base
312 && (TREE_CODE (base) == MEM_REF
313 || TREE_CODE (base) == TARGET_MEM_REF))
314 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
315 else if (base
316 && DECL_P (base))
317 return ptr_deref_may_alias_decl_p (ptr1, base);
318 else
319 return true;
322 /* From here we require SSA name pointers. Anything else aliases. */
323 if (TREE_CODE (ptr1) != SSA_NAME
324 || TREE_CODE (ptr2) != SSA_NAME
325 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
326 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
327 return true;
329 /* We may end up with two empty points-to solutions for two same pointers.
330 In this case we still want to say both pointers alias, so shortcut
331 that here. */
332 if (ptr1 == ptr2)
333 return true;
335 /* If we do not have useful points-to information for either pointer
336 we cannot disambiguate anything else. */
337 pi1 = SSA_NAME_PTR_INFO (ptr1);
338 pi2 = SSA_NAME_PTR_INFO (ptr2);
339 if (!pi1 || !pi2)
340 return true;
342 /* ??? This does not use TBAA to prune decls from the intersection
343 that not both pointers may access. */
344 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
347 /* Return true if dereferencing PTR may alias *REF.
348 The caller is responsible for applying TBAA to see if PTR
349 may access *REF at all. */
351 static bool
352 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
354 tree base = ao_ref_base (ref);
356 if (TREE_CODE (base) == MEM_REF
357 || TREE_CODE (base) == TARGET_MEM_REF)
358 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
359 else if (DECL_P (base))
360 return ptr_deref_may_alias_decl_p (ptr, base);
362 return true;
365 /* Returns whether reference REF to BASE may refer to global memory. */
367 static bool
368 ref_may_alias_global_p_1 (tree base)
370 if (DECL_P (base))
371 return is_global_var (base);
372 else if (TREE_CODE (base) == MEM_REF
373 || TREE_CODE (base) == TARGET_MEM_REF)
374 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
375 return true;
378 bool
379 ref_may_alias_global_p (ao_ref *ref)
381 tree base = ao_ref_base (ref);
382 return ref_may_alias_global_p_1 (base);
385 bool
386 ref_may_alias_global_p (tree ref)
388 tree base = get_base_address (ref);
389 return ref_may_alias_global_p_1 (base);
392 /* Return true whether STMT may clobber global memory. */
394 bool
395 stmt_may_clobber_global_p (gimple stmt)
397 tree lhs;
399 if (!gimple_vdef (stmt))
400 return false;
402 /* ??? We can ask the oracle whether an artificial pointer
403 dereference with a pointer with points-to information covering
404 all global memory (what about non-address taken memory?) maybe
405 clobbered by this call. As there is at the moment no convenient
406 way of doing that without generating garbage do some manual
407 checking instead.
408 ??? We could make a NULL ao_ref argument to the various
409 predicates special, meaning any global memory. */
411 switch (gimple_code (stmt))
413 case GIMPLE_ASSIGN:
414 lhs = gimple_assign_lhs (stmt);
415 return (TREE_CODE (lhs) != SSA_NAME
416 && ref_may_alias_global_p (lhs));
417 case GIMPLE_CALL:
418 return true;
419 default:
420 return true;
425 /* Dump alias information on FILE. */
427 void
428 dump_alias_info (FILE *file)
430 unsigned i;
431 const char *funcname
432 = lang_hooks.decl_printable_name (current_function_decl, 2);
433 tree var;
435 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
437 fprintf (file, "Aliased symbols\n\n");
439 FOR_EACH_LOCAL_DECL (cfun, i, var)
441 if (may_be_aliased (var))
442 dump_variable (file, var);
445 fprintf (file, "\nCall clobber information\n");
447 fprintf (file, "\nESCAPED");
448 dump_points_to_solution (file, &cfun->gimple_df->escaped);
450 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
452 for (i = 1; i < num_ssa_names; i++)
454 tree ptr = ssa_name (i);
455 struct ptr_info_def *pi;
457 if (ptr == NULL_TREE
458 || !POINTER_TYPE_P (TREE_TYPE (ptr))
459 || SSA_NAME_IN_FREE_LIST (ptr))
460 continue;
462 pi = SSA_NAME_PTR_INFO (ptr);
463 if (pi)
464 dump_points_to_info_for (file, ptr);
467 fprintf (file, "\n");
471 /* Dump alias information on stderr. */
473 DEBUG_FUNCTION void
474 debug_alias_info (void)
476 dump_alias_info (stderr);
480 /* Dump the points-to set *PT into FILE. */
482 void
483 dump_points_to_solution (FILE *file, struct pt_solution *pt)
485 if (pt->anything)
486 fprintf (file, ", points-to anything");
488 if (pt->nonlocal)
489 fprintf (file, ", points-to non-local");
491 if (pt->escaped)
492 fprintf (file, ", points-to escaped");
494 if (pt->ipa_escaped)
495 fprintf (file, ", points-to unit escaped");
497 if (pt->null)
498 fprintf (file, ", points-to NULL");
500 if (pt->vars)
502 fprintf (file, ", points-to vars: ");
503 dump_decl_set (file, pt->vars);
504 if (pt->vars_contains_nonlocal
505 && pt->vars_contains_escaped_heap)
506 fprintf (file, " (nonlocal, escaped heap)");
507 else if (pt->vars_contains_nonlocal
508 && pt->vars_contains_escaped)
509 fprintf (file, " (nonlocal, escaped)");
510 else if (pt->vars_contains_nonlocal)
511 fprintf (file, " (nonlocal)");
512 else if (pt->vars_contains_escaped_heap)
513 fprintf (file, " (escaped heap)");
514 else if (pt->vars_contains_escaped)
515 fprintf (file, " (escaped)");
520 /* Unified dump function for pt_solution. */
522 DEBUG_FUNCTION void
523 debug (pt_solution &ref)
525 dump_points_to_solution (stderr, &ref);
528 DEBUG_FUNCTION void
529 debug (pt_solution *ptr)
531 if (ptr)
532 debug (*ptr);
533 else
534 fprintf (stderr, "<nil>\n");
538 /* Dump points-to information for SSA_NAME PTR into FILE. */
540 void
541 dump_points_to_info_for (FILE *file, tree ptr)
543 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
545 print_generic_expr (file, ptr, dump_flags);
547 if (pi)
548 dump_points_to_solution (file, &pi->pt);
549 else
550 fprintf (file, ", points-to anything");
552 fprintf (file, "\n");
556 /* Dump points-to information for VAR into stderr. */
558 DEBUG_FUNCTION void
559 debug_points_to_info_for (tree var)
561 dump_points_to_info_for (stderr, var);
565 /* Initializes the alias-oracle reference representation *R from REF. */
567 void
568 ao_ref_init (ao_ref *r, tree ref)
570 r->ref = ref;
571 r->base = NULL_TREE;
572 r->offset = 0;
573 r->size = -1;
574 r->max_size = -1;
575 r->ref_alias_set = -1;
576 r->base_alias_set = -1;
577 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
580 /* Returns the base object of the memory reference *REF. */
582 tree
583 ao_ref_base (ao_ref *ref)
585 if (ref->base)
586 return ref->base;
587 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
588 &ref->max_size);
589 return ref->base;
592 /* Returns the base object alias set of the memory reference *REF. */
594 alias_set_type
595 ao_ref_base_alias_set (ao_ref *ref)
597 tree base_ref;
598 if (ref->base_alias_set != -1)
599 return ref->base_alias_set;
600 if (!ref->ref)
601 return 0;
602 base_ref = ref->ref;
603 while (handled_component_p (base_ref))
604 base_ref = TREE_OPERAND (base_ref, 0);
605 ref->base_alias_set = get_alias_set (base_ref);
606 return ref->base_alias_set;
609 /* Returns the reference alias set of the memory reference *REF. */
611 alias_set_type
612 ao_ref_alias_set (ao_ref *ref)
614 if (ref->ref_alias_set != -1)
615 return ref->ref_alias_set;
616 ref->ref_alias_set = get_alias_set (ref->ref);
617 return ref->ref_alias_set;
620 /* Init an alias-oracle reference representation from a gimple pointer
621 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
622 size is assumed to be unknown. The access is assumed to be only
623 to or after of the pointer target, not before it. */
625 void
626 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
628 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
629 ref->ref = NULL_TREE;
630 if (TREE_CODE (ptr) == SSA_NAME)
632 gimple stmt = SSA_NAME_DEF_STMT (ptr);
633 if (gimple_assign_single_p (stmt)
634 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
635 ptr = gimple_assign_rhs1 (stmt);
636 else if (is_gimple_assign (stmt)
637 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
638 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
640 ptr = gimple_assign_rhs1 (stmt);
641 extra_offset = BITS_PER_UNIT
642 * int_cst_value (gimple_assign_rhs2 (stmt));
646 if (TREE_CODE (ptr) == ADDR_EXPR)
648 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
649 if (ref->base)
650 ref->offset = BITS_PER_UNIT * t;
651 else
653 size = NULL_TREE;
654 ref->offset = 0;
655 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
658 else
660 ref->base = build2 (MEM_REF, char_type_node,
661 ptr, null_pointer_node);
662 ref->offset = 0;
664 ref->offset += extra_offset;
665 if (size
666 && tree_fits_shwi_p (size)
667 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
668 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
669 else
670 ref->max_size = ref->size = -1;
671 ref->ref_alias_set = 0;
672 ref->base_alias_set = 0;
673 ref->volatile_p = false;
676 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
677 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
678 decide. */
680 static inline int
681 same_type_for_tbaa (tree type1, tree type2)
683 type1 = TYPE_MAIN_VARIANT (type1);
684 type2 = TYPE_MAIN_VARIANT (type2);
686 /* If we would have to do structural comparison bail out. */
687 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
688 || TYPE_STRUCTURAL_EQUALITY_P (type2))
689 return -1;
691 /* Compare the canonical types. */
692 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
693 return 1;
695 /* ??? Array types are not properly unified in all cases as we have
696 spurious changes in the index types for example. Removing this
697 causes all sorts of problems with the Fortran frontend. */
698 if (TREE_CODE (type1) == ARRAY_TYPE
699 && TREE_CODE (type2) == ARRAY_TYPE)
700 return -1;
702 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
703 object of one of its constrained subtypes, e.g. when a function with an
704 unconstrained parameter passed by reference is called on an object and
705 inlined. But, even in the case of a fixed size, type and subtypes are
706 not equivalent enough as to share the same TYPE_CANONICAL, since this
707 would mean that conversions between them are useless, whereas they are
708 not (e.g. type and subtypes can have different modes). So, in the end,
709 they are only guaranteed to have the same alias set. */
710 if (get_alias_set (type1) == get_alias_set (type2))
711 return -1;
713 /* The types are known to be not equal. */
714 return 0;
717 /* Determine if the two component references REF1 and REF2 which are
718 based on access types TYPE1 and TYPE2 and of which at least one is based
719 on an indirect reference may alias. REF2 is the only one that can
720 be a decl in which case REF2_IS_DECL is true.
721 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
722 are the respective alias sets. */
724 static bool
725 aliasing_component_refs_p (tree ref1,
726 alias_set_type ref1_alias_set,
727 alias_set_type base1_alias_set,
728 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
729 tree ref2,
730 alias_set_type ref2_alias_set,
731 alias_set_type base2_alias_set,
732 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
733 bool ref2_is_decl)
735 /* If one reference is a component references through pointers try to find a
736 common base and apply offset based disambiguation. This handles
737 for example
738 struct A { int i; int j; } *q;
739 struct B { struct A a; int k; } *p;
740 disambiguating q->i and p->a.j. */
741 tree base1, base2;
742 tree type1, type2;
743 tree *refp;
744 int same_p;
746 /* Choose bases and base types to search for. */
747 base1 = ref1;
748 while (handled_component_p (base1))
749 base1 = TREE_OPERAND (base1, 0);
750 type1 = TREE_TYPE (base1);
751 base2 = ref2;
752 while (handled_component_p (base2))
753 base2 = TREE_OPERAND (base2, 0);
754 type2 = TREE_TYPE (base2);
756 /* Now search for the type1 in the access path of ref2. This
757 would be a common base for doing offset based disambiguation on. */
758 refp = &ref2;
759 while (handled_component_p (*refp)
760 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
761 refp = &TREE_OPERAND (*refp, 0);
762 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
763 /* If we couldn't compare types we have to bail out. */
764 if (same_p == -1)
765 return true;
766 else if (same_p == 1)
768 HOST_WIDE_INT offadj, sztmp, msztmp;
769 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
770 offset2 -= offadj;
771 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
772 offset1 -= offadj;
773 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
775 /* If we didn't find a common base, try the other way around. */
776 refp = &ref1;
777 while (handled_component_p (*refp)
778 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
779 refp = &TREE_OPERAND (*refp, 0);
780 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
781 /* If we couldn't compare types we have to bail out. */
782 if (same_p == -1)
783 return true;
784 else if (same_p == 1)
786 HOST_WIDE_INT offadj, sztmp, msztmp;
787 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
788 offset1 -= offadj;
789 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
790 offset2 -= offadj;
791 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
794 /* If we have two type access paths B1.path1 and B2.path2 they may
795 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
796 But we can still have a path that goes B1.path1...B2.path2 with
797 a part that we do not see. So we can only disambiguate now
798 if there is no B2 in the tail of path1 and no B1 on the
799 tail of path2. */
800 if (base1_alias_set == ref2_alias_set
801 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
802 return true;
803 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
804 if (!ref2_is_decl)
805 return (base2_alias_set == ref1_alias_set
806 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
807 return false;
810 /* Return true if we can determine that component references REF1 and REF2,
811 that are within a common DECL, cannot overlap. */
813 static bool
814 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
816 auto_vec<tree, 16> component_refs1;
817 auto_vec<tree, 16> component_refs2;
819 /* Create the stack of handled components for REF1. */
820 while (handled_component_p (ref1))
822 component_refs1.safe_push (ref1);
823 ref1 = TREE_OPERAND (ref1, 0);
825 if (TREE_CODE (ref1) == MEM_REF)
827 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
828 goto may_overlap;
829 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
832 /* Create the stack of handled components for REF2. */
833 while (handled_component_p (ref2))
835 component_refs2.safe_push (ref2);
836 ref2 = TREE_OPERAND (ref2, 0);
838 if (TREE_CODE (ref2) == MEM_REF)
840 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
841 goto may_overlap;
842 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
845 /* We must have the same base DECL. */
846 gcc_assert (ref1 == ref2);
848 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
849 rank. This is sufficient because we start from the same DECL and you
850 cannot reference several fields at a time with COMPONENT_REFs (unlike
851 with ARRAY_RANGE_REFs for arrays) so you always need the same number
852 of them to access a sub-component, unless you're in a union, in which
853 case the return value will precisely be false. */
854 while (true)
858 if (component_refs1.is_empty ())
859 goto may_overlap;
860 ref1 = component_refs1.pop ();
862 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
866 if (component_refs2.is_empty ())
867 goto may_overlap;
868 ref2 = component_refs2.pop ();
870 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
872 /* Beware of BIT_FIELD_REF. */
873 if (TREE_CODE (ref1) != COMPONENT_REF
874 || TREE_CODE (ref2) != COMPONENT_REF)
875 goto may_overlap;
877 tree field1 = TREE_OPERAND (ref1, 1);
878 tree field2 = TREE_OPERAND (ref2, 1);
880 /* ??? We cannot simply use the type of operand #0 of the refs here
881 as the Fortran compiler smuggles type punning into COMPONENT_REFs
882 for common blocks instead of using unions like everyone else. */
883 tree type1 = DECL_CONTEXT (field1);
884 tree type2 = DECL_CONTEXT (field2);
886 /* We cannot disambiguate fields in a union or qualified union. */
887 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
888 goto may_overlap;
890 /* Different fields of the same record type cannot overlap.
891 ??? Bitfields can overlap at RTL level so punt on them. */
892 if (field1 != field2)
894 component_refs1.release ();
895 component_refs2.release ();
896 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
900 may_overlap:
901 component_refs1.release ();
902 component_refs2.release ();
903 return false;
906 /* qsort compare function to sort FIELD_DECLs after their
907 DECL_FIELD_CONTEXT TYPE_UID. */
909 static inline int
910 ncr_compar (const void *field1_, const void *field2_)
912 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
913 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
914 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
915 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
916 if (uid1 < uid2)
917 return -1;
918 else if (uid1 > uid2)
919 return 1;
920 return 0;
923 /* Return true if we can determine that the fields referenced cannot
924 overlap for any pair of objects. */
926 static bool
927 nonoverlapping_component_refs_p (const_tree x, const_tree y)
929 if (!flag_strict_aliasing
930 || !x || !y
931 || TREE_CODE (x) != COMPONENT_REF
932 || TREE_CODE (y) != COMPONENT_REF)
933 return false;
935 auto_vec<const_tree, 16> fieldsx;
936 while (TREE_CODE (x) == COMPONENT_REF)
938 tree field = TREE_OPERAND (x, 1);
939 tree type = DECL_FIELD_CONTEXT (field);
940 if (TREE_CODE (type) == RECORD_TYPE)
941 fieldsx.safe_push (field);
942 x = TREE_OPERAND (x, 0);
944 if (fieldsx.length () == 0)
945 return false;
946 auto_vec<const_tree, 16> fieldsy;
947 while (TREE_CODE (y) == COMPONENT_REF)
949 tree field = TREE_OPERAND (y, 1);
950 tree type = DECL_FIELD_CONTEXT (field);
951 if (TREE_CODE (type) == RECORD_TYPE)
952 fieldsy.safe_push (TREE_OPERAND (y, 1));
953 y = TREE_OPERAND (y, 0);
955 if (fieldsy.length () == 0)
956 return false;
958 /* Most common case first. */
959 if (fieldsx.length () == 1
960 && fieldsy.length () == 1)
961 return ((DECL_FIELD_CONTEXT (fieldsx[0])
962 == DECL_FIELD_CONTEXT (fieldsy[0]))
963 && fieldsx[0] != fieldsy[0]
964 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
966 if (fieldsx.length () == 2)
968 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
970 const_tree tem = fieldsx[0];
971 fieldsx[0] = fieldsx[1];
972 fieldsx[1] = tem;
975 else
976 fieldsx.qsort (ncr_compar);
978 if (fieldsy.length () == 2)
980 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
982 const_tree tem = fieldsy[0];
983 fieldsy[0] = fieldsy[1];
984 fieldsy[1] = tem;
987 else
988 fieldsy.qsort (ncr_compar);
990 unsigned i = 0, j = 0;
993 const_tree fieldx = fieldsx[i];
994 const_tree fieldy = fieldsy[j];
995 tree typex = DECL_FIELD_CONTEXT (fieldx);
996 tree typey = DECL_FIELD_CONTEXT (fieldy);
997 if (typex == typey)
999 /* We're left with accessing different fields of a structure,
1000 no possible overlap, unless they are both bitfields. */
1001 if (fieldx != fieldy)
1002 return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
1004 if (TYPE_UID (typex) < TYPE_UID (typey))
1006 i++;
1007 if (i == fieldsx.length ())
1008 break;
1010 else
1012 j++;
1013 if (j == fieldsy.length ())
1014 break;
1017 while (1);
1019 return false;
1023 /* Return true if two memory references based on the variables BASE1
1024 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1025 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1026 if non-NULL are the complete memory reference trees. */
1028 static bool
1029 decl_refs_may_alias_p (tree ref1, tree base1,
1030 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1031 tree ref2, tree base2,
1032 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1034 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1036 /* If both references are based on different variables, they cannot alias. */
1037 if (base1 != base2)
1038 return false;
1040 /* If both references are based on the same variable, they cannot alias if
1041 the accesses do not overlap. */
1042 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1043 return false;
1045 /* For components with variable position, the above test isn't sufficient,
1046 so we disambiguate component references manually. */
1047 if (ref1 && ref2
1048 && handled_component_p (ref1) && handled_component_p (ref2)
1049 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1050 return false;
1052 return true;
1055 /* Return true if an indirect reference based on *PTR1 constrained
1056 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1057 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1058 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1059 in which case they are computed on-demand. REF1 and REF2
1060 if non-NULL are the complete memory reference trees. */
1062 static bool
1063 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1064 HOST_WIDE_INT offset1,
1065 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1066 alias_set_type ref1_alias_set,
1067 alias_set_type base1_alias_set,
1068 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1069 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1070 alias_set_type ref2_alias_set,
1071 alias_set_type base2_alias_set, bool tbaa_p)
1073 tree ptr1;
1074 tree ptrtype1, dbase2;
1075 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1076 HOST_WIDE_INT doffset1, doffset2;
1078 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1079 || TREE_CODE (base1) == TARGET_MEM_REF)
1080 && DECL_P (base2));
1082 ptr1 = TREE_OPERAND (base1, 0);
1084 /* The offset embedded in MEM_REFs can be negative. Bias them
1085 so that the resulting offset adjustment is positive. */
1086 offset_int moff = mem_ref_offset (base1);
1087 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1088 if (wi::neg_p (moff))
1089 offset2p += (-moff).to_short_addr ();
1090 else
1091 offset1p += moff.to_short_addr ();
1093 /* If only one reference is based on a variable, they cannot alias if
1094 the pointer access is beyond the extent of the variable access.
1095 (the pointer base cannot validly point to an offset less than zero
1096 of the variable).
1097 ??? IVOPTs creates bases that do not honor this restriction,
1098 so do not apply this optimization for TARGET_MEM_REFs. */
1099 if (TREE_CODE (base1) != TARGET_MEM_REF
1100 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1101 return false;
1102 /* They also cannot alias if the pointer may not point to the decl. */
1103 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1104 return false;
1106 /* Disambiguations that rely on strict aliasing rules follow. */
1107 if (!flag_strict_aliasing || !tbaa_p)
1108 return true;
1110 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1112 /* If the alias set for a pointer access is zero all bets are off. */
1113 if (base1_alias_set == -1)
1114 base1_alias_set = get_deref_alias_set (ptrtype1);
1115 if (base1_alias_set == 0)
1116 return true;
1117 if (base2_alias_set == -1)
1118 base2_alias_set = get_alias_set (base2);
1120 /* When we are trying to disambiguate an access with a pointer dereference
1121 as base versus one with a decl as base we can use both the size
1122 of the decl and its dynamic type for extra disambiguation.
1123 ??? We do not know anything about the dynamic type of the decl
1124 other than that its alias-set contains base2_alias_set as a subset
1125 which does not help us here. */
1126 /* As we know nothing useful about the dynamic type of the decl just
1127 use the usual conflict check rather than a subset test.
1128 ??? We could introduce -fvery-strict-aliasing when the language
1129 does not allow decls to have a dynamic type that differs from their
1130 static type. Then we can check
1131 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1132 if (base1_alias_set != base2_alias_set
1133 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1134 return false;
1135 /* If the size of the access relevant for TBAA through the pointer
1136 is bigger than the size of the decl we can't possibly access the
1137 decl via that pointer. */
1138 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1139 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1140 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1141 /* ??? This in turn may run afoul when a decl of type T which is
1142 a member of union type U is accessed through a pointer to
1143 type U and sizeof T is smaller than sizeof U. */
1144 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1145 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1146 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1147 return false;
1149 if (!ref2)
1150 return true;
1152 /* If the decl is accessed via a MEM_REF, reconstruct the base
1153 we can use for TBAA and an appropriately adjusted offset. */
1154 dbase2 = ref2;
1155 while (handled_component_p (dbase2))
1156 dbase2 = TREE_OPERAND (dbase2, 0);
1157 doffset1 = offset1;
1158 doffset2 = offset2;
1159 if (TREE_CODE (dbase2) == MEM_REF
1160 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1162 offset_int moff = mem_ref_offset (dbase2);
1163 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1164 if (wi::neg_p (moff))
1165 doffset1 -= (-moff).to_short_addr ();
1166 else
1167 doffset2 -= moff.to_short_addr ();
1170 /* If either reference is view-converted, give up now. */
1171 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1172 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1173 return true;
1175 /* If both references are through the same type, they do not alias
1176 if the accesses do not overlap. This does extra disambiguation
1177 for mixed/pointer accesses but requires strict aliasing.
1178 For MEM_REFs we require that the component-ref offset we computed
1179 is relative to the start of the type which we ensure by
1180 comparing rvalue and access type and disregarding the constant
1181 pointer offset. */
1182 if ((TREE_CODE (base1) != TARGET_MEM_REF
1183 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1184 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1185 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1187 if (ref1 && ref2
1188 && nonoverlapping_component_refs_p (ref1, ref2))
1189 return false;
1191 /* Do access-path based disambiguation. */
1192 if (ref1 && ref2
1193 && (handled_component_p (ref1) || handled_component_p (ref2)))
1194 return aliasing_component_refs_p (ref1,
1195 ref1_alias_set, base1_alias_set,
1196 offset1, max_size1,
1197 ref2,
1198 ref2_alias_set, base2_alias_set,
1199 offset2, max_size2, true);
1201 return true;
1204 /* Return true if two indirect references based on *PTR1
1205 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1206 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1207 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1208 in which case they are computed on-demand. REF1 and REF2
1209 if non-NULL are the complete memory reference trees. */
1211 static bool
1212 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1213 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1214 alias_set_type ref1_alias_set,
1215 alias_set_type base1_alias_set,
1216 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1217 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1218 alias_set_type ref2_alias_set,
1219 alias_set_type base2_alias_set, bool tbaa_p)
1221 tree ptr1;
1222 tree ptr2;
1223 tree ptrtype1, ptrtype2;
1225 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1226 || TREE_CODE (base1) == TARGET_MEM_REF)
1227 && (TREE_CODE (base2) == MEM_REF
1228 || TREE_CODE (base2) == TARGET_MEM_REF));
1230 ptr1 = TREE_OPERAND (base1, 0);
1231 ptr2 = TREE_OPERAND (base2, 0);
1233 /* If both bases are based on pointers they cannot alias if they may not
1234 point to the same memory object or if they point to the same object
1235 and the accesses do not overlap. */
1236 if ((!cfun || gimple_in_ssa_p (cfun))
1237 && operand_equal_p (ptr1, ptr2, 0)
1238 && (((TREE_CODE (base1) != TARGET_MEM_REF
1239 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1240 && (TREE_CODE (base2) != TARGET_MEM_REF
1241 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1242 || (TREE_CODE (base1) == TARGET_MEM_REF
1243 && TREE_CODE (base2) == TARGET_MEM_REF
1244 && (TMR_STEP (base1) == TMR_STEP (base2)
1245 || (TMR_STEP (base1) && TMR_STEP (base2)
1246 && operand_equal_p (TMR_STEP (base1),
1247 TMR_STEP (base2), 0)))
1248 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1249 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1250 && operand_equal_p (TMR_INDEX (base1),
1251 TMR_INDEX (base2), 0)))
1252 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1253 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1254 && operand_equal_p (TMR_INDEX2 (base1),
1255 TMR_INDEX2 (base2), 0))))))
1257 offset_int moff;
1258 /* The offset embedded in MEM_REFs can be negative. Bias them
1259 so that the resulting offset adjustment is positive. */
1260 moff = mem_ref_offset (base1);
1261 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1262 if (wi::neg_p (moff))
1263 offset2 += (-moff).to_short_addr ();
1264 else
1265 offset1 += moff.to_shwi ();
1266 moff = mem_ref_offset (base2);
1267 moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1268 if (wi::neg_p (moff))
1269 offset1 += (-moff).to_short_addr ();
1270 else
1271 offset2 += moff.to_short_addr ();
1272 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1274 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1275 return false;
1277 /* Disambiguations that rely on strict aliasing rules follow. */
1278 if (!flag_strict_aliasing || !tbaa_p)
1279 return true;
1281 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1282 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1284 /* If the alias set for a pointer access is zero all bets are off. */
1285 if (base1_alias_set == -1)
1286 base1_alias_set = get_deref_alias_set (ptrtype1);
1287 if (base1_alias_set == 0)
1288 return true;
1289 if (base2_alias_set == -1)
1290 base2_alias_set = get_deref_alias_set (ptrtype2);
1291 if (base2_alias_set == 0)
1292 return true;
1294 /* If both references are through the same type, they do not alias
1295 if the accesses do not overlap. This does extra disambiguation
1296 for mixed/pointer accesses but requires strict aliasing. */
1297 if ((TREE_CODE (base1) != TARGET_MEM_REF
1298 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1299 && (TREE_CODE (base2) != TARGET_MEM_REF
1300 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1301 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1302 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1303 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1304 TREE_TYPE (ptrtype2)) == 1)
1305 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1307 /* Do type-based disambiguation. */
1308 if (base1_alias_set != base2_alias_set
1309 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1310 return false;
1312 /* If either reference is view-converted, give up now. */
1313 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1314 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1315 return true;
1317 if (ref1 && ref2
1318 && nonoverlapping_component_refs_p (ref1, ref2))
1319 return false;
1321 /* Do access-path based disambiguation. */
1322 if (ref1 && ref2
1323 && (handled_component_p (ref1) || handled_component_p (ref2)))
1324 return aliasing_component_refs_p (ref1,
1325 ref1_alias_set, base1_alias_set,
1326 offset1, max_size1,
1327 ref2,
1328 ref2_alias_set, base2_alias_set,
1329 offset2, max_size2, false);
1331 return true;
1334 /* Return true, if the two memory references REF1 and REF2 may alias. */
1336 bool
1337 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1339 tree base1, base2;
1340 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1341 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1342 bool var1_p, var2_p, ind1_p, ind2_p;
1344 gcc_checking_assert ((!ref1->ref
1345 || TREE_CODE (ref1->ref) == SSA_NAME
1346 || DECL_P (ref1->ref)
1347 || TREE_CODE (ref1->ref) == STRING_CST
1348 || handled_component_p (ref1->ref)
1349 || TREE_CODE (ref1->ref) == MEM_REF
1350 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1351 && (!ref2->ref
1352 || TREE_CODE (ref2->ref) == SSA_NAME
1353 || DECL_P (ref2->ref)
1354 || TREE_CODE (ref2->ref) == STRING_CST
1355 || handled_component_p (ref2->ref)
1356 || TREE_CODE (ref2->ref) == MEM_REF
1357 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1359 /* Decompose the references into their base objects and the access. */
1360 base1 = ao_ref_base (ref1);
1361 offset1 = ref1->offset;
1362 max_size1 = ref1->max_size;
1363 base2 = ao_ref_base (ref2);
1364 offset2 = ref2->offset;
1365 max_size2 = ref2->max_size;
1367 /* We can end up with registers or constants as bases for example from
1368 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1369 which is seen as a struct copy. */
1370 if (TREE_CODE (base1) == SSA_NAME
1371 || TREE_CODE (base1) == CONST_DECL
1372 || TREE_CODE (base1) == CONSTRUCTOR
1373 || TREE_CODE (base1) == ADDR_EXPR
1374 || CONSTANT_CLASS_P (base1)
1375 || TREE_CODE (base2) == SSA_NAME
1376 || TREE_CODE (base2) == CONST_DECL
1377 || TREE_CODE (base2) == CONSTRUCTOR
1378 || TREE_CODE (base2) == ADDR_EXPR
1379 || CONSTANT_CLASS_P (base2))
1380 return false;
1382 /* We can end up referring to code via function and label decls.
1383 As we likely do not properly track code aliases conservatively
1384 bail out. */
1385 if (TREE_CODE (base1) == FUNCTION_DECL
1386 || TREE_CODE (base1) == LABEL_DECL
1387 || TREE_CODE (base2) == FUNCTION_DECL
1388 || TREE_CODE (base2) == LABEL_DECL)
1389 return true;
1391 /* Two volatile accesses always conflict. */
1392 if (ref1->volatile_p
1393 && ref2->volatile_p)
1394 return true;
1396 /* Defer to simple offset based disambiguation if we have
1397 references based on two decls. Do this before defering to
1398 TBAA to handle must-alias cases in conformance with the
1399 GCC extension of allowing type-punning through unions. */
1400 var1_p = DECL_P (base1);
1401 var2_p = DECL_P (base2);
1402 if (var1_p && var2_p)
1403 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1404 ref2->ref, base2, offset2, max_size2);
1406 /* Handle restrict based accesses.
1407 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1408 here. */
1409 tree rbase1 = base1;
1410 tree rbase2 = base2;
1411 if (var1_p)
1413 rbase1 = ref1->ref;
1414 if (rbase1)
1415 while (handled_component_p (rbase1))
1416 rbase1 = TREE_OPERAND (rbase1, 0);
1418 if (var2_p)
1420 rbase2 = ref2->ref;
1421 if (rbase2)
1422 while (handled_component_p (rbase2))
1423 rbase2 = TREE_OPERAND (rbase2, 0);
1425 if (rbase1 && rbase2
1426 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1427 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1428 /* If the accesses are in the same restrict clique... */
1429 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1430 /* But based on different pointers they do not alias. */
1431 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1432 return false;
1434 ind1_p = (TREE_CODE (base1) == MEM_REF
1435 || TREE_CODE (base1) == TARGET_MEM_REF);
1436 ind2_p = (TREE_CODE (base2) == MEM_REF
1437 || TREE_CODE (base2) == TARGET_MEM_REF);
1439 /* Canonicalize the pointer-vs-decl case. */
1440 if (ind1_p && var2_p)
1442 HOST_WIDE_INT tmp1;
1443 tree tmp2;
1444 ao_ref *tmp3;
1445 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1446 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1447 tmp2 = base1; base1 = base2; base2 = tmp2;
1448 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1449 var1_p = true;
1450 ind1_p = false;
1451 var2_p = false;
1452 ind2_p = true;
1455 /* First defer to TBAA if possible. */
1456 if (tbaa_p
1457 && flag_strict_aliasing
1458 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1459 ao_ref_alias_set (ref2)))
1460 return false;
1462 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1463 if (var1_p && ind2_p)
1464 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1465 offset2, max_size2,
1466 ao_ref_alias_set (ref2), -1,
1467 ref1->ref, base1,
1468 offset1, max_size1,
1469 ao_ref_alias_set (ref1),
1470 ao_ref_base_alias_set (ref1),
1471 tbaa_p);
1472 else if (ind1_p && ind2_p)
1473 return indirect_refs_may_alias_p (ref1->ref, base1,
1474 offset1, max_size1,
1475 ao_ref_alias_set (ref1), -1,
1476 ref2->ref, base2,
1477 offset2, max_size2,
1478 ao_ref_alias_set (ref2), -1,
1479 tbaa_p);
1481 /* We really do not want to end up here, but returning true is safe. */
1482 #ifdef ENABLE_CHECKING
1483 gcc_unreachable ();
1484 #else
1485 return true;
1486 #endif
1489 static bool
1490 refs_may_alias_p (tree ref1, ao_ref *ref2)
1492 ao_ref r1;
1493 ao_ref_init (&r1, ref1);
1494 return refs_may_alias_p_1 (&r1, ref2, true);
1497 bool
1498 refs_may_alias_p (tree ref1, tree ref2)
1500 ao_ref r1, r2;
1501 bool res;
1502 ao_ref_init (&r1, ref1);
1503 ao_ref_init (&r2, ref2);
1504 res = refs_may_alias_p_1 (&r1, &r2, true);
1505 if (res)
1506 ++alias_stats.refs_may_alias_p_may_alias;
1507 else
1508 ++alias_stats.refs_may_alias_p_no_alias;
1509 return res;
1512 /* Returns true if there is a anti-dependence for the STORE that
1513 executes after the LOAD. */
1515 bool
1516 refs_anti_dependent_p (tree load, tree store)
1518 ao_ref r1, r2;
1519 ao_ref_init (&r1, load);
1520 ao_ref_init (&r2, store);
1521 return refs_may_alias_p_1 (&r1, &r2, false);
1524 /* Returns true if there is a output dependence for the stores
1525 STORE1 and STORE2. */
1527 bool
1528 refs_output_dependent_p (tree store1, tree store2)
1530 ao_ref r1, r2;
1531 ao_ref_init (&r1, store1);
1532 ao_ref_init (&r2, store2);
1533 return refs_may_alias_p_1 (&r1, &r2, false);
1536 /* If the call CALL may use the memory reference REF return true,
1537 otherwise return false. */
1539 static bool
1540 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1542 tree base, callee;
1543 unsigned i;
1544 int flags = gimple_call_flags (call);
1546 /* Const functions without a static chain do not implicitly use memory. */
1547 if (!gimple_call_chain (call)
1548 && (flags & (ECF_CONST|ECF_NOVOPS)))
1549 goto process_args;
1551 base = ao_ref_base (ref);
1552 if (!base)
1553 return true;
1555 /* A call that is not without side-effects might involve volatile
1556 accesses and thus conflicts with all other volatile accesses. */
1557 if (ref->volatile_p)
1558 return true;
1560 /* If the reference is based on a decl that is not aliased the call
1561 cannot possibly use it. */
1562 if (DECL_P (base)
1563 && !may_be_aliased (base)
1564 /* But local statics can be used through recursion. */
1565 && !is_global_var (base))
1566 goto process_args;
1568 callee = gimple_call_fndecl (call);
1570 /* Handle those builtin functions explicitly that do not act as
1571 escape points. See tree-ssa-structalias.c:find_func_aliases
1572 for the list of builtins we might need to handle here. */
1573 if (callee != NULL_TREE
1574 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1575 switch (DECL_FUNCTION_CODE (callee))
1577 /* All the following functions read memory pointed to by
1578 their second argument. strcat/strncat additionally
1579 reads memory pointed to by the first argument. */
1580 case BUILT_IN_STRCAT:
1581 case BUILT_IN_STRNCAT:
1583 ao_ref dref;
1584 ao_ref_init_from_ptr_and_size (&dref,
1585 gimple_call_arg (call, 0),
1586 NULL_TREE);
1587 if (refs_may_alias_p_1 (&dref, ref, false))
1588 return true;
1590 /* FALLTHRU */
1591 case BUILT_IN_STRCPY:
1592 case BUILT_IN_STRNCPY:
1593 case BUILT_IN_MEMCPY:
1594 case BUILT_IN_MEMMOVE:
1595 case BUILT_IN_MEMPCPY:
1596 case BUILT_IN_STPCPY:
1597 case BUILT_IN_STPNCPY:
1598 case BUILT_IN_TM_MEMCPY:
1599 case BUILT_IN_TM_MEMMOVE:
1601 ao_ref dref;
1602 tree size = NULL_TREE;
1603 if (gimple_call_num_args (call) == 3)
1604 size = gimple_call_arg (call, 2);
1605 ao_ref_init_from_ptr_and_size (&dref,
1606 gimple_call_arg (call, 1),
1607 size);
1608 return refs_may_alias_p_1 (&dref, ref, false);
1610 case BUILT_IN_STRCAT_CHK:
1611 case BUILT_IN_STRNCAT_CHK:
1613 ao_ref dref;
1614 ao_ref_init_from_ptr_and_size (&dref,
1615 gimple_call_arg (call, 0),
1616 NULL_TREE);
1617 if (refs_may_alias_p_1 (&dref, ref, false))
1618 return true;
1620 /* FALLTHRU */
1621 case BUILT_IN_STRCPY_CHK:
1622 case BUILT_IN_STRNCPY_CHK:
1623 case BUILT_IN_MEMCPY_CHK:
1624 case BUILT_IN_MEMMOVE_CHK:
1625 case BUILT_IN_MEMPCPY_CHK:
1626 case BUILT_IN_STPCPY_CHK:
1627 case BUILT_IN_STPNCPY_CHK:
1629 ao_ref dref;
1630 tree size = NULL_TREE;
1631 if (gimple_call_num_args (call) == 4)
1632 size = gimple_call_arg (call, 2);
1633 ao_ref_init_from_ptr_and_size (&dref,
1634 gimple_call_arg (call, 1),
1635 size);
1636 return refs_may_alias_p_1 (&dref, ref, false);
1638 case BUILT_IN_BCOPY:
1640 ao_ref dref;
1641 tree size = gimple_call_arg (call, 2);
1642 ao_ref_init_from_ptr_and_size (&dref,
1643 gimple_call_arg (call, 0),
1644 size);
1645 return refs_may_alias_p_1 (&dref, ref, false);
1648 /* The following functions read memory pointed to by their
1649 first argument. */
1650 CASE_BUILT_IN_TM_LOAD (1):
1651 CASE_BUILT_IN_TM_LOAD (2):
1652 CASE_BUILT_IN_TM_LOAD (4):
1653 CASE_BUILT_IN_TM_LOAD (8):
1654 CASE_BUILT_IN_TM_LOAD (FLOAT):
1655 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1656 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1657 CASE_BUILT_IN_TM_LOAD (M64):
1658 CASE_BUILT_IN_TM_LOAD (M128):
1659 CASE_BUILT_IN_TM_LOAD (M256):
1660 case BUILT_IN_TM_LOG:
1661 case BUILT_IN_TM_LOG_1:
1662 case BUILT_IN_TM_LOG_2:
1663 case BUILT_IN_TM_LOG_4:
1664 case BUILT_IN_TM_LOG_8:
1665 case BUILT_IN_TM_LOG_FLOAT:
1666 case BUILT_IN_TM_LOG_DOUBLE:
1667 case BUILT_IN_TM_LOG_LDOUBLE:
1668 case BUILT_IN_TM_LOG_M64:
1669 case BUILT_IN_TM_LOG_M128:
1670 case BUILT_IN_TM_LOG_M256:
1671 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1673 /* These read memory pointed to by the first argument. */
1674 case BUILT_IN_STRDUP:
1675 case BUILT_IN_STRNDUP:
1676 case BUILT_IN_REALLOC:
1678 ao_ref dref;
1679 tree size = NULL_TREE;
1680 if (gimple_call_num_args (call) == 2)
1681 size = gimple_call_arg (call, 1);
1682 ao_ref_init_from_ptr_and_size (&dref,
1683 gimple_call_arg (call, 0),
1684 size);
1685 return refs_may_alias_p_1 (&dref, ref, false);
1687 /* These read memory pointed to by the first argument. */
1688 case BUILT_IN_INDEX:
1689 case BUILT_IN_STRCHR:
1690 case BUILT_IN_STRRCHR:
1692 ao_ref dref;
1693 ao_ref_init_from_ptr_and_size (&dref,
1694 gimple_call_arg (call, 0),
1695 NULL_TREE);
1696 return refs_may_alias_p_1 (&dref, ref, false);
1698 /* These read memory pointed to by the first argument with size
1699 in the third argument. */
1700 case BUILT_IN_MEMCHR:
1702 ao_ref dref;
1703 ao_ref_init_from_ptr_and_size (&dref,
1704 gimple_call_arg (call, 0),
1705 gimple_call_arg (call, 2));
1706 return refs_may_alias_p_1 (&dref, ref, false);
1708 /* These read memory pointed to by the first and second arguments. */
1709 case BUILT_IN_STRSTR:
1710 case BUILT_IN_STRPBRK:
1712 ao_ref dref;
1713 ao_ref_init_from_ptr_and_size (&dref,
1714 gimple_call_arg (call, 0),
1715 NULL_TREE);
1716 if (refs_may_alias_p_1 (&dref, ref, false))
1717 return true;
1718 ao_ref_init_from_ptr_and_size (&dref,
1719 gimple_call_arg (call, 1),
1720 NULL_TREE);
1721 return refs_may_alias_p_1 (&dref, ref, false);
1724 /* The following builtins do not read from memory. */
1725 case BUILT_IN_FREE:
1726 case BUILT_IN_MALLOC:
1727 case BUILT_IN_POSIX_MEMALIGN:
1728 case BUILT_IN_ALIGNED_ALLOC:
1729 case BUILT_IN_CALLOC:
1730 case BUILT_IN_ALLOCA:
1731 case BUILT_IN_ALLOCA_WITH_ALIGN:
1732 case BUILT_IN_STACK_SAVE:
1733 case BUILT_IN_STACK_RESTORE:
1734 case BUILT_IN_MEMSET:
1735 case BUILT_IN_TM_MEMSET:
1736 case BUILT_IN_MEMSET_CHK:
1737 case BUILT_IN_FREXP:
1738 case BUILT_IN_FREXPF:
1739 case BUILT_IN_FREXPL:
1740 case BUILT_IN_GAMMA_R:
1741 case BUILT_IN_GAMMAF_R:
1742 case BUILT_IN_GAMMAL_R:
1743 case BUILT_IN_LGAMMA_R:
1744 case BUILT_IN_LGAMMAF_R:
1745 case BUILT_IN_LGAMMAL_R:
1746 case BUILT_IN_MODF:
1747 case BUILT_IN_MODFF:
1748 case BUILT_IN_MODFL:
1749 case BUILT_IN_REMQUO:
1750 case BUILT_IN_REMQUOF:
1751 case BUILT_IN_REMQUOL:
1752 case BUILT_IN_SINCOS:
1753 case BUILT_IN_SINCOSF:
1754 case BUILT_IN_SINCOSL:
1755 case BUILT_IN_ASSUME_ALIGNED:
1756 case BUILT_IN_VA_END:
1757 return false;
1758 /* __sync_* builtins and some OpenMP builtins act as threading
1759 barriers. */
1760 #undef DEF_SYNC_BUILTIN
1761 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1762 #include "sync-builtins.def"
1763 #undef DEF_SYNC_BUILTIN
1764 case BUILT_IN_GOMP_ATOMIC_START:
1765 case BUILT_IN_GOMP_ATOMIC_END:
1766 case BUILT_IN_GOMP_BARRIER:
1767 case BUILT_IN_GOMP_BARRIER_CANCEL:
1768 case BUILT_IN_GOMP_TASKWAIT:
1769 case BUILT_IN_GOMP_TASKGROUP_END:
1770 case BUILT_IN_GOMP_CRITICAL_START:
1771 case BUILT_IN_GOMP_CRITICAL_END:
1772 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1773 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1774 case BUILT_IN_GOMP_LOOP_END:
1775 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1776 case BUILT_IN_GOMP_ORDERED_START:
1777 case BUILT_IN_GOMP_ORDERED_END:
1778 case BUILT_IN_GOMP_SECTIONS_END:
1779 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1780 case BUILT_IN_GOMP_SINGLE_COPY_START:
1781 case BUILT_IN_GOMP_SINGLE_COPY_END:
1782 return true;
1784 default:
1785 /* Fallthru to general call handling. */;
1788 /* Check if base is a global static variable that is not read
1789 by the function. */
1790 if (callee != NULL_TREE
1791 && TREE_CODE (base) == VAR_DECL
1792 && TREE_STATIC (base))
1794 struct cgraph_node *node = cgraph_node::get (callee);
1795 bitmap not_read;
1797 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1798 node yet. We should enforce that there are nodes for all decls in the
1799 IL and remove this check instead. */
1800 if (node
1801 && (not_read = ipa_reference_get_not_read_global (node))
1802 && bitmap_bit_p (not_read, DECL_UID (base)))
1803 goto process_args;
1806 /* Check if the base variable is call-used. */
1807 if (DECL_P (base))
1809 if (pt_solution_includes (gimple_call_use_set (call), base))
1810 return true;
1812 else if ((TREE_CODE (base) == MEM_REF
1813 || TREE_CODE (base) == TARGET_MEM_REF)
1814 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1816 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1817 if (!pi)
1818 return true;
1820 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1821 return true;
1823 else
1824 return true;
1826 /* Inspect call arguments for passed-by-value aliases. */
1827 process_args:
1828 for (i = 0; i < gimple_call_num_args (call); ++i)
1830 tree op = gimple_call_arg (call, i);
1831 int flags = gimple_call_arg_flags (call, i);
1833 if (flags & EAF_UNUSED)
1834 continue;
1836 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1837 op = TREE_OPERAND (op, 0);
1839 if (TREE_CODE (op) != SSA_NAME
1840 && !is_gimple_min_invariant (op))
1842 ao_ref r;
1843 ao_ref_init (&r, op);
1844 if (refs_may_alias_p_1 (&r, ref, true))
1845 return true;
1849 return false;
1852 static bool
1853 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref)
1855 bool res;
1856 res = ref_maybe_used_by_call_p_1 (call, ref);
1857 if (res)
1858 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1859 else
1860 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1861 return res;
1865 /* If the statement STMT may use the memory reference REF return
1866 true, otherwise return false. */
1868 bool
1869 ref_maybe_used_by_stmt_p (gimple stmt, ao_ref *ref)
1871 if (is_gimple_assign (stmt))
1873 tree rhs;
1875 /* All memory assign statements are single. */
1876 if (!gimple_assign_single_p (stmt))
1877 return false;
1879 rhs = gimple_assign_rhs1 (stmt);
1880 if (is_gimple_reg (rhs)
1881 || is_gimple_min_invariant (rhs)
1882 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1883 return false;
1885 return refs_may_alias_p (rhs, ref);
1887 else if (is_gimple_call (stmt))
1888 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref);
1889 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1891 tree retval = gimple_return_retval (return_stmt);
1892 if (retval
1893 && TREE_CODE (retval) != SSA_NAME
1894 && !is_gimple_min_invariant (retval)
1895 && refs_may_alias_p (retval, ref))
1896 return true;
1897 /* If ref escapes the function then the return acts as a use. */
1898 tree base = ao_ref_base (ref);
1899 if (!base)
1901 else if (DECL_P (base))
1902 return is_global_var (base);
1903 else if (TREE_CODE (base) == MEM_REF
1904 || TREE_CODE (base) == TARGET_MEM_REF)
1905 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1906 return false;
1909 return true;
1912 bool
1913 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1915 ao_ref r;
1916 ao_ref_init (&r, ref);
1917 return ref_maybe_used_by_stmt_p (stmt, &r);
1920 /* If the call in statement CALL may clobber the memory reference REF
1921 return true, otherwise return false. */
1923 bool
1924 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
1926 tree base;
1927 tree callee;
1929 /* If the call is pure or const it cannot clobber anything. */
1930 if (gimple_call_flags (call)
1931 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1932 return false;
1933 if (gimple_call_internal_p (call))
1934 switch (gimple_call_internal_fn (call))
1936 /* Treat these internal calls like ECF_PURE for aliasing,
1937 they don't write to any memory the program should care about.
1938 They have important other side-effects, and read memory,
1939 so can't be ECF_NOVOPS. */
1940 case IFN_UBSAN_NULL:
1941 case IFN_UBSAN_BOUNDS:
1942 case IFN_UBSAN_VPTR:
1943 case IFN_UBSAN_OBJECT_SIZE:
1944 case IFN_ASAN_CHECK:
1945 return false;
1946 default:
1947 break;
1950 base = ao_ref_base (ref);
1951 if (!base)
1952 return true;
1954 if (TREE_CODE (base) == SSA_NAME
1955 || CONSTANT_CLASS_P (base))
1956 return false;
1958 /* A call that is not without side-effects might involve volatile
1959 accesses and thus conflicts with all other volatile accesses. */
1960 if (ref->volatile_p)
1961 return true;
1963 /* If the reference is based on a decl that is not aliased the call
1964 cannot possibly clobber it. */
1965 if (DECL_P (base)
1966 && !may_be_aliased (base)
1967 /* But local non-readonly statics can be modified through recursion
1968 or the call may implement a threading barrier which we must
1969 treat as may-def. */
1970 && (TREE_READONLY (base)
1971 || !is_global_var (base)))
1972 return false;
1974 callee = gimple_call_fndecl (call);
1976 /* Handle those builtin functions explicitly that do not act as
1977 escape points. See tree-ssa-structalias.c:find_func_aliases
1978 for the list of builtins we might need to handle here. */
1979 if (callee != NULL_TREE
1980 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1981 switch (DECL_FUNCTION_CODE (callee))
1983 /* All the following functions clobber memory pointed to by
1984 their first argument. */
1985 case BUILT_IN_STRCPY:
1986 case BUILT_IN_STRNCPY:
1987 case BUILT_IN_MEMCPY:
1988 case BUILT_IN_MEMMOVE:
1989 case BUILT_IN_MEMPCPY:
1990 case BUILT_IN_STPCPY:
1991 case BUILT_IN_STPNCPY:
1992 case BUILT_IN_STRCAT:
1993 case BUILT_IN_STRNCAT:
1994 case BUILT_IN_MEMSET:
1995 case BUILT_IN_TM_MEMSET:
1996 CASE_BUILT_IN_TM_STORE (1):
1997 CASE_BUILT_IN_TM_STORE (2):
1998 CASE_BUILT_IN_TM_STORE (4):
1999 CASE_BUILT_IN_TM_STORE (8):
2000 CASE_BUILT_IN_TM_STORE (FLOAT):
2001 CASE_BUILT_IN_TM_STORE (DOUBLE):
2002 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2003 CASE_BUILT_IN_TM_STORE (M64):
2004 CASE_BUILT_IN_TM_STORE (M128):
2005 CASE_BUILT_IN_TM_STORE (M256):
2006 case BUILT_IN_TM_MEMCPY:
2007 case BUILT_IN_TM_MEMMOVE:
2009 ao_ref dref;
2010 tree size = NULL_TREE;
2011 /* Don't pass in size for strncat, as the maximum size
2012 is strlen (dest) + n + 1 instead of n, resp.
2013 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2014 known. */
2015 if (gimple_call_num_args (call) == 3
2016 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2017 size = gimple_call_arg (call, 2);
2018 ao_ref_init_from_ptr_and_size (&dref,
2019 gimple_call_arg (call, 0),
2020 size);
2021 return refs_may_alias_p_1 (&dref, ref, false);
2023 case BUILT_IN_STRCPY_CHK:
2024 case BUILT_IN_STRNCPY_CHK:
2025 case BUILT_IN_MEMCPY_CHK:
2026 case BUILT_IN_MEMMOVE_CHK:
2027 case BUILT_IN_MEMPCPY_CHK:
2028 case BUILT_IN_STPCPY_CHK:
2029 case BUILT_IN_STPNCPY_CHK:
2030 case BUILT_IN_STRCAT_CHK:
2031 case BUILT_IN_STRNCAT_CHK:
2032 case BUILT_IN_MEMSET_CHK:
2034 ao_ref dref;
2035 tree size = NULL_TREE;
2036 /* Don't pass in size for __strncat_chk, as the maximum size
2037 is strlen (dest) + n + 1 instead of n, resp.
2038 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2039 known. */
2040 if (gimple_call_num_args (call) == 4
2041 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2042 size = gimple_call_arg (call, 2);
2043 ao_ref_init_from_ptr_and_size (&dref,
2044 gimple_call_arg (call, 0),
2045 size);
2046 return refs_may_alias_p_1 (&dref, ref, false);
2048 case BUILT_IN_BCOPY:
2050 ao_ref dref;
2051 tree size = gimple_call_arg (call, 2);
2052 ao_ref_init_from_ptr_and_size (&dref,
2053 gimple_call_arg (call, 1),
2054 size);
2055 return refs_may_alias_p_1 (&dref, ref, false);
2057 /* Allocating memory does not have any side-effects apart from
2058 being the definition point for the pointer. */
2059 case BUILT_IN_MALLOC:
2060 case BUILT_IN_ALIGNED_ALLOC:
2061 case BUILT_IN_CALLOC:
2062 case BUILT_IN_STRDUP:
2063 case BUILT_IN_STRNDUP:
2064 /* Unix98 specifies that errno is set on allocation failure. */
2065 if (flag_errno_math
2066 && targetm.ref_may_alias_errno (ref))
2067 return true;
2068 return false;
2069 case BUILT_IN_STACK_SAVE:
2070 case BUILT_IN_ALLOCA:
2071 case BUILT_IN_ALLOCA_WITH_ALIGN:
2072 case BUILT_IN_ASSUME_ALIGNED:
2073 return false;
2074 /* But posix_memalign stores a pointer into the memory pointed to
2075 by its first argument. */
2076 case BUILT_IN_POSIX_MEMALIGN:
2078 tree ptrptr = gimple_call_arg (call, 0);
2079 ao_ref dref;
2080 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2081 TYPE_SIZE_UNIT (ptr_type_node));
2082 return (refs_may_alias_p_1 (&dref, ref, false)
2083 || (flag_errno_math
2084 && targetm.ref_may_alias_errno (ref)));
2086 /* Freeing memory kills the pointed-to memory. More importantly
2087 the call has to serve as a barrier for moving loads and stores
2088 across it. */
2089 case BUILT_IN_FREE:
2090 case BUILT_IN_VA_END:
2092 tree ptr = gimple_call_arg (call, 0);
2093 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2095 /* Realloc serves both as allocation point and deallocation point. */
2096 case BUILT_IN_REALLOC:
2098 tree ptr = gimple_call_arg (call, 0);
2099 /* Unix98 specifies that errno is set on allocation failure. */
2100 return ((flag_errno_math
2101 && targetm.ref_may_alias_errno (ref))
2102 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2104 case BUILT_IN_GAMMA_R:
2105 case BUILT_IN_GAMMAF_R:
2106 case BUILT_IN_GAMMAL_R:
2107 case BUILT_IN_LGAMMA_R:
2108 case BUILT_IN_LGAMMAF_R:
2109 case BUILT_IN_LGAMMAL_R:
2111 tree out = gimple_call_arg (call, 1);
2112 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2113 return true;
2114 if (flag_errno_math)
2115 break;
2116 return false;
2118 case BUILT_IN_FREXP:
2119 case BUILT_IN_FREXPF:
2120 case BUILT_IN_FREXPL:
2121 case BUILT_IN_MODF:
2122 case BUILT_IN_MODFF:
2123 case BUILT_IN_MODFL:
2125 tree out = gimple_call_arg (call, 1);
2126 return ptr_deref_may_alias_ref_p_1 (out, ref);
2128 case BUILT_IN_REMQUO:
2129 case BUILT_IN_REMQUOF:
2130 case BUILT_IN_REMQUOL:
2132 tree out = gimple_call_arg (call, 2);
2133 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2134 return true;
2135 if (flag_errno_math)
2136 break;
2137 return false;
2139 case BUILT_IN_SINCOS:
2140 case BUILT_IN_SINCOSF:
2141 case BUILT_IN_SINCOSL:
2143 tree sin = gimple_call_arg (call, 1);
2144 tree cos = gimple_call_arg (call, 2);
2145 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2146 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2148 /* __sync_* builtins and some OpenMP builtins act as threading
2149 barriers. */
2150 #undef DEF_SYNC_BUILTIN
2151 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2152 #include "sync-builtins.def"
2153 #undef DEF_SYNC_BUILTIN
2154 case BUILT_IN_GOMP_ATOMIC_START:
2155 case BUILT_IN_GOMP_ATOMIC_END:
2156 case BUILT_IN_GOMP_BARRIER:
2157 case BUILT_IN_GOMP_BARRIER_CANCEL:
2158 case BUILT_IN_GOMP_TASKWAIT:
2159 case BUILT_IN_GOMP_TASKGROUP_END:
2160 case BUILT_IN_GOMP_CRITICAL_START:
2161 case BUILT_IN_GOMP_CRITICAL_END:
2162 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2163 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2164 case BUILT_IN_GOMP_LOOP_END:
2165 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2166 case BUILT_IN_GOMP_ORDERED_START:
2167 case BUILT_IN_GOMP_ORDERED_END:
2168 case BUILT_IN_GOMP_SECTIONS_END:
2169 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2170 case BUILT_IN_GOMP_SINGLE_COPY_START:
2171 case BUILT_IN_GOMP_SINGLE_COPY_END:
2172 return true;
2173 default:
2174 /* Fallthru to general call handling. */;
2177 /* Check if base is a global static variable that is not written
2178 by the function. */
2179 if (callee != NULL_TREE
2180 && TREE_CODE (base) == VAR_DECL
2181 && TREE_STATIC (base))
2183 struct cgraph_node *node = cgraph_node::get (callee);
2184 bitmap not_written;
2186 if (node
2187 && (not_written = ipa_reference_get_not_written_global (node))
2188 && bitmap_bit_p (not_written, DECL_UID (base)))
2189 return false;
2192 /* Check if the base variable is call-clobbered. */
2193 if (DECL_P (base))
2194 return pt_solution_includes (gimple_call_clobber_set (call), base);
2195 else if ((TREE_CODE (base) == MEM_REF
2196 || TREE_CODE (base) == TARGET_MEM_REF)
2197 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2199 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2200 if (!pi)
2201 return true;
2203 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2206 return true;
2209 /* If the call in statement CALL may clobber the memory reference REF
2210 return true, otherwise return false. */
2212 bool
2213 call_may_clobber_ref_p (gcall *call, tree ref)
2215 bool res;
2216 ao_ref r;
2217 ao_ref_init (&r, ref);
2218 res = call_may_clobber_ref_p_1 (call, &r);
2219 if (res)
2220 ++alias_stats.call_may_clobber_ref_p_may_alias;
2221 else
2222 ++alias_stats.call_may_clobber_ref_p_no_alias;
2223 return res;
2227 /* If the statement STMT may clobber the memory reference REF return true,
2228 otherwise return false. */
2230 bool
2231 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2233 if (is_gimple_call (stmt))
2235 tree lhs = gimple_call_lhs (stmt);
2236 if (lhs
2237 && TREE_CODE (lhs) != SSA_NAME)
2239 ao_ref r;
2240 ao_ref_init (&r, lhs);
2241 if (refs_may_alias_p_1 (ref, &r, true))
2242 return true;
2245 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2247 else if (gimple_assign_single_p (stmt))
2249 tree lhs = gimple_assign_lhs (stmt);
2250 if (TREE_CODE (lhs) != SSA_NAME)
2252 ao_ref r;
2253 ao_ref_init (&r, lhs);
2254 return refs_may_alias_p_1 (ref, &r, true);
2257 else if (gimple_code (stmt) == GIMPLE_ASM)
2258 return true;
2260 return false;
2263 bool
2264 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2266 ao_ref r;
2267 ao_ref_init (&r, ref);
2268 return stmt_may_clobber_ref_p_1 (stmt, &r);
2271 /* If STMT kills the memory reference REF return true, otherwise
2272 return false. */
2274 bool
2275 stmt_kills_ref_p (gimple stmt, ao_ref *ref)
2277 if (!ao_ref_base (ref))
2278 return false;
2280 if (gimple_has_lhs (stmt)
2281 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2282 /* The assignment is not necessarily carried out if it can throw
2283 and we can catch it in the current function where we could inspect
2284 the previous value.
2285 ??? We only need to care about the RHS throwing. For aggregate
2286 assignments or similar calls and non-call exceptions the LHS
2287 might throw as well. */
2288 && !stmt_can_throw_internal (stmt))
2290 tree lhs = gimple_get_lhs (stmt);
2291 /* If LHS is literally a base of the access we are done. */
2292 if (ref->ref)
2294 tree base = ref->ref;
2295 if (handled_component_p (base))
2297 tree saved_lhs0 = NULL_TREE;
2298 if (handled_component_p (lhs))
2300 saved_lhs0 = TREE_OPERAND (lhs, 0);
2301 TREE_OPERAND (lhs, 0) = integer_zero_node;
2305 /* Just compare the outermost handled component, if
2306 they are equal we have found a possible common
2307 base. */
2308 tree saved_base0 = TREE_OPERAND (base, 0);
2309 TREE_OPERAND (base, 0) = integer_zero_node;
2310 bool res = operand_equal_p (lhs, base, 0);
2311 TREE_OPERAND (base, 0) = saved_base0;
2312 if (res)
2313 break;
2314 /* Otherwise drop handled components of the access. */
2315 base = saved_base0;
2317 while (handled_component_p (base));
2318 if (saved_lhs0)
2319 TREE_OPERAND (lhs, 0) = saved_lhs0;
2321 /* Finally check if lhs is equal or equal to the base candidate
2322 of the access. */
2323 if (operand_equal_p (lhs, base, 0))
2324 return true;
2327 /* Now look for non-literal equal bases with the restriction of
2328 handling constant offset and size. */
2329 /* For a must-alias check we need to be able to constrain
2330 the access properly. */
2331 if (ref->max_size == -1)
2332 return false;
2333 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2334 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2335 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2336 so base == ref->base does not always hold. */
2337 if (base != ref->base)
2339 /* If both base and ref->base are MEM_REFs, only compare the
2340 first operand, and if the second operand isn't equal constant,
2341 try to add the offsets into offset and ref_offset. */
2342 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2343 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2345 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2346 TREE_OPERAND (ref->base, 1)))
2348 offset_int off1 = mem_ref_offset (base);
2349 off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2350 off1 += offset;
2351 offset_int off2 = mem_ref_offset (ref->base);
2352 off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2353 off2 += ref_offset;
2354 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2356 offset = off1.to_shwi ();
2357 ref_offset = off2.to_shwi ();
2359 else
2360 size = -1;
2363 else
2364 size = -1;
2366 /* For a must-alias check we need to be able to constrain
2367 the access properly. */
2368 if (size != -1 && size == max_size)
2370 if (offset <= ref_offset
2371 && offset + size >= ref_offset + ref->max_size)
2372 return true;
2376 if (is_gimple_call (stmt))
2378 tree callee = gimple_call_fndecl (stmt);
2379 if (callee != NULL_TREE
2380 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2381 switch (DECL_FUNCTION_CODE (callee))
2383 case BUILT_IN_FREE:
2385 tree ptr = gimple_call_arg (stmt, 0);
2386 tree base = ao_ref_base (ref);
2387 if (base && TREE_CODE (base) == MEM_REF
2388 && TREE_OPERAND (base, 0) == ptr)
2389 return true;
2390 break;
2393 case BUILT_IN_MEMCPY:
2394 case BUILT_IN_MEMPCPY:
2395 case BUILT_IN_MEMMOVE:
2396 case BUILT_IN_MEMSET:
2397 case BUILT_IN_MEMCPY_CHK:
2398 case BUILT_IN_MEMPCPY_CHK:
2399 case BUILT_IN_MEMMOVE_CHK:
2400 case BUILT_IN_MEMSET_CHK:
2402 /* For a must-alias check we need to be able to constrain
2403 the access properly. */
2404 if (ref->max_size == -1)
2405 return false;
2406 tree dest = gimple_call_arg (stmt, 0);
2407 tree len = gimple_call_arg (stmt, 2);
2408 if (!tree_fits_shwi_p (len))
2409 return false;
2410 tree rbase = ref->base;
2411 offset_int roffset = ref->offset;
2412 ao_ref dref;
2413 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2414 tree base = ao_ref_base (&dref);
2415 offset_int offset = dref.offset;
2416 if (!base || dref.size == -1)
2417 return false;
2418 if (TREE_CODE (base) == MEM_REF)
2420 if (TREE_CODE (rbase) != MEM_REF)
2421 return false;
2422 // Compare pointers.
2423 offset += wi::lshift (mem_ref_offset (base),
2424 LOG2_BITS_PER_UNIT);
2425 roffset += wi::lshift (mem_ref_offset (rbase),
2426 LOG2_BITS_PER_UNIT);
2427 base = TREE_OPERAND (base, 0);
2428 rbase = TREE_OPERAND (rbase, 0);
2430 if (base == rbase
2431 && wi::les_p (offset, roffset)
2432 && wi::les_p (roffset + ref->max_size,
2433 offset + wi::lshift (wi::to_offset (len),
2434 LOG2_BITS_PER_UNIT)))
2435 return true;
2436 break;
2439 case BUILT_IN_VA_END:
2441 tree ptr = gimple_call_arg (stmt, 0);
2442 if (TREE_CODE (ptr) == ADDR_EXPR)
2444 tree base = ao_ref_base (ref);
2445 if (TREE_OPERAND (ptr, 0) == base)
2446 return true;
2448 break;
2451 default:;
2454 return false;
2457 bool
2458 stmt_kills_ref_p (gimple stmt, tree ref)
2460 ao_ref r;
2461 ao_ref_init (&r, ref);
2462 return stmt_kills_ref_p (stmt, &r);
2466 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2467 TARGET or a statement clobbering the memory reference REF in which
2468 case false is returned. The walk starts with VUSE, one argument of PHI. */
2470 static bool
2471 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2472 tree vuse, unsigned int *cnt, bitmap *visited,
2473 bool abort_on_visited,
2474 void *(*translate)(ao_ref *, tree, void *, bool),
2475 void *data)
2477 basic_block bb = gimple_bb (phi);
2479 if (!*visited)
2480 *visited = BITMAP_ALLOC (NULL);
2482 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2484 /* Walk until we hit the target. */
2485 while (vuse != target)
2487 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2488 /* Recurse for PHI nodes. */
2489 if (gimple_code (def_stmt) == GIMPLE_PHI)
2491 /* An already visited PHI node ends the walk successfully. */
2492 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2493 return !abort_on_visited;
2494 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2495 visited, abort_on_visited,
2496 translate, data);
2497 if (!vuse)
2498 return false;
2499 continue;
2501 else if (gimple_nop_p (def_stmt))
2502 return false;
2503 else
2505 /* A clobbering statement or the end of the IL ends it failing. */
2506 ++*cnt;
2507 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2509 if (translate
2510 && (*translate) (ref, vuse, data, true) == NULL)
2512 else
2513 return false;
2516 /* If we reach a new basic-block see if we already skipped it
2517 in a previous walk that ended successfully. */
2518 if (gimple_bb (def_stmt) != bb)
2520 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2521 return !abort_on_visited;
2522 bb = gimple_bb (def_stmt);
2524 vuse = gimple_vuse (def_stmt);
2526 return true;
2529 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2530 until we hit the phi argument definition that dominates the other one.
2531 Return that, or NULL_TREE if there is no such definition. */
2533 static tree
2534 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2535 ao_ref *ref, unsigned int *cnt,
2536 bitmap *visited, bool abort_on_visited,
2537 void *(*translate)(ao_ref *, tree, void *, bool),
2538 void *data)
2540 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2541 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2542 tree common_vuse;
2544 if (arg0 == arg1)
2545 return arg0;
2546 else if (gimple_nop_p (def0)
2547 || (!gimple_nop_p (def1)
2548 && dominated_by_p (CDI_DOMINATORS,
2549 gimple_bb (def1), gimple_bb (def0))))
2551 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2552 visited, abort_on_visited, translate, data))
2553 return arg0;
2555 else if (gimple_nop_p (def1)
2556 || dominated_by_p (CDI_DOMINATORS,
2557 gimple_bb (def0), gimple_bb (def1)))
2559 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2560 visited, abort_on_visited, translate, data))
2561 return arg1;
2563 /* Special case of a diamond:
2564 MEM_1 = ...
2565 goto (cond) ? L1 : L2
2566 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2567 goto L3
2568 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2569 L3: MEM_4 = PHI<MEM_2, MEM_3>
2570 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2571 dominate each other, but still we can easily skip this PHI node
2572 if we recognize that the vuse MEM operand is the same for both,
2573 and that we can skip both statements (they don't clobber us).
2574 This is still linear. Don't use maybe_skip_until, that might
2575 potentially be slow. */
2576 else if ((common_vuse = gimple_vuse (def0))
2577 && common_vuse == gimple_vuse (def1))
2579 *cnt += 2;
2580 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2581 || (translate
2582 && (*translate) (ref, arg0, data, true) == NULL))
2583 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2584 || (translate
2585 && (*translate) (ref, arg1, data, true) == NULL)))
2586 return common_vuse;
2589 return NULL_TREE;
2593 /* Starting from a PHI node for the virtual operand of the memory reference
2594 REF find a continuation virtual operand that allows to continue walking
2595 statements dominating PHI skipping only statements that cannot possibly
2596 clobber REF. Increments *CNT for each alias disambiguation done.
2597 Returns NULL_TREE if no suitable virtual operand can be found. */
2599 tree
2600 get_continuation_for_phi (gimple phi, ao_ref *ref,
2601 unsigned int *cnt, bitmap *visited,
2602 bool abort_on_visited,
2603 void *(*translate)(ao_ref *, tree, void *, bool),
2604 void *data)
2606 unsigned nargs = gimple_phi_num_args (phi);
2608 /* Through a single-argument PHI we can simply look through. */
2609 if (nargs == 1)
2610 return PHI_ARG_DEF (phi, 0);
2612 /* For two or more arguments try to pairwise skip non-aliasing code
2613 until we hit the phi argument definition that dominates the other one. */
2614 else if (nargs >= 2)
2616 tree arg0, arg1;
2617 unsigned i;
2619 /* Find a candidate for the virtual operand which definition
2620 dominates those of all others. */
2621 arg0 = PHI_ARG_DEF (phi, 0);
2622 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2623 for (i = 1; i < nargs; ++i)
2625 arg1 = PHI_ARG_DEF (phi, i);
2626 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2628 arg0 = arg1;
2629 break;
2631 if (dominated_by_p (CDI_DOMINATORS,
2632 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2633 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2634 arg0 = arg1;
2637 /* Then pairwise reduce against the found candidate. */
2638 for (i = 0; i < nargs; ++i)
2640 arg1 = PHI_ARG_DEF (phi, i);
2641 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2642 cnt, visited, abort_on_visited,
2643 translate, data);
2644 if (!arg0)
2645 return NULL_TREE;
2648 return arg0;
2651 return NULL_TREE;
2654 /* Based on the memory reference REF and its virtual use VUSE call
2655 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2656 itself. That is, for each virtual use for which its defining statement
2657 does not clobber REF.
2659 WALKER is called with REF, the current virtual use and DATA. If
2660 WALKER returns non-NULL the walk stops and its result is returned.
2661 At the end of a non-successful walk NULL is returned.
2663 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2664 use which definition is a statement that may clobber REF and DATA.
2665 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2666 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2667 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2668 to adjust REF and *DATA to make that valid.
2670 VALUEIZE if non-NULL is called with the next VUSE that is considered
2671 and return value is substituted for that. This can be used to
2672 implement optimistic value-numbering for example. Note that the
2673 VUSE argument is assumed to be valueized already.
2675 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2677 void *
2678 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2679 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2680 void *(*translate)(ao_ref *, tree, void *, bool),
2681 tree (*valueize)(tree),
2682 void *data)
2684 bitmap visited = NULL;
2685 void *res;
2686 unsigned int cnt = 0;
2687 bool translated = false;
2689 timevar_push (TV_ALIAS_STMT_WALK);
2693 gimple def_stmt;
2695 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2696 res = (*walker) (ref, vuse, cnt, data);
2697 /* Abort walk. */
2698 if (res == (void *)-1)
2700 res = NULL;
2701 break;
2703 /* Lookup succeeded. */
2704 else if (res != NULL)
2705 break;
2707 if (valueize)
2708 vuse = valueize (vuse);
2709 def_stmt = SSA_NAME_DEF_STMT (vuse);
2710 if (gimple_nop_p (def_stmt))
2711 break;
2712 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2713 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2714 &visited, translated, translate, data);
2715 else
2717 cnt++;
2718 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2720 if (!translate)
2721 break;
2722 res = (*translate) (ref, vuse, data, false);
2723 /* Failed lookup and translation. */
2724 if (res == (void *)-1)
2726 res = NULL;
2727 break;
2729 /* Lookup succeeded. */
2730 else if (res != NULL)
2731 break;
2732 /* Translation succeeded, continue walking. */
2733 translated = true;
2735 vuse = gimple_vuse (def_stmt);
2738 while (vuse);
2740 if (visited)
2741 BITMAP_FREE (visited);
2743 timevar_pop (TV_ALIAS_STMT_WALK);
2745 return res;
2749 /* Based on the memory reference REF call WALKER for each vdef which
2750 defining statement may clobber REF, starting with VDEF. If REF
2751 is NULL_TREE, each defining statement is visited.
2753 WALKER is called with REF, the current vdef and DATA. If WALKER
2754 returns true the walk is stopped, otherwise it continues.
2756 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2757 The pointer may be NULL and then we do not track this information.
2759 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2760 PHI argument (but only one walk continues on merge points), the
2761 return value is true if any of the walks was successful.
2763 The function returns the number of statements walked. */
2765 static unsigned int
2766 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2767 bool (*walker)(ao_ref *, tree, void *), void *data,
2768 bitmap *visited, unsigned int cnt,
2769 bool *function_entry_reached)
2773 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2775 if (*visited
2776 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2777 return cnt;
2779 if (gimple_nop_p (def_stmt))
2781 if (function_entry_reached)
2782 *function_entry_reached = true;
2783 return cnt;
2785 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2787 unsigned i;
2788 if (!*visited)
2789 *visited = BITMAP_ALLOC (NULL);
2790 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2791 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2792 walker, data, visited, 0,
2793 function_entry_reached);
2794 return cnt;
2797 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2798 cnt++;
2799 if ((!ref
2800 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2801 && (*walker) (ref, vdef, data))
2802 return cnt;
2804 vdef = gimple_vuse (def_stmt);
2806 while (1);
2809 unsigned int
2810 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2811 bool (*walker)(ao_ref *, tree, void *), void *data,
2812 bitmap *visited,
2813 bool *function_entry_reached)
2815 bitmap local_visited = NULL;
2816 unsigned int ret;
2818 timevar_push (TV_ALIAS_STMT_WALK);
2820 if (function_entry_reached)
2821 *function_entry_reached = false;
2823 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2824 visited ? visited : &local_visited, 0,
2825 function_entry_reached);
2826 if (local_visited)
2827 BITMAP_FREE (local_visited);
2829 timevar_pop (TV_ALIAS_STMT_WALK);
2831 return ret;