2016-10-28 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-alias.c
blob26c9f9e894b78f2dce72c017f27e2fb84e090019
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2016 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 "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "ssa.h"
31 #include "cgraph.h"
32 #include "tree-pretty-print.h"
33 #include "alias.h"
34 #include "fold-const.h"
36 #include "langhooks.h"
37 #include "dumpfile.h"
38 #include "tree-eh.h"
39 #include "tree-dfa.h"
40 #include "ipa-reference.h"
42 /* Broad overview of how alias analysis on gimple works:
44 Statements clobbering or using memory are linked through the
45 virtual operand factored use-def chain. The virtual operand
46 is unique per function, its symbol is accessible via gimple_vop (cfun).
47 Virtual operands are used for efficiently walking memory statements
48 in the gimple IL and are useful for things like value-numbering as
49 a generation count for memory references.
51 SSA_NAME pointers may have associated points-to information
52 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
53 points-to information is (re-)computed by the TODO_rebuild_alias
54 pass manager todo. Points-to information is also used for more
55 precise tracking of call-clobbered and call-used variables and
56 related disambiguations.
58 This file contains functions for disambiguating memory references,
59 the so called alias-oracle and tools for walking of the gimple IL.
61 The main alias-oracle entry-points are
63 bool stmt_may_clobber_ref_p (gimple *, tree)
65 This function queries if a statement may invalidate (parts of)
66 the memory designated by the reference tree argument.
68 bool ref_maybe_used_by_stmt_p (gimple *, tree)
70 This function queries if a statement may need (parts of) the
71 memory designated by the reference tree argument.
73 There are variants of these functions that only handle the call
74 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
75 Note that these do not disambiguate against a possible call lhs.
77 bool refs_may_alias_p (tree, tree)
79 This function tries to disambiguate two reference trees.
81 bool ptr_deref_may_alias_global_p (tree)
83 This function queries if dereferencing a pointer variable may
84 alias global memory.
86 More low-level disambiguators are available and documented in
87 this file. Low-level disambiguators dealing with points-to
88 information are in tree-ssa-structalias.c. */
91 /* Query statistics for the different low-level disambiguators.
92 A high-level query may trigger multiple of them. */
94 static struct {
95 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
96 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
97 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
98 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
99 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
100 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
101 } alias_stats;
103 void
104 dump_alias_stats (FILE *s)
106 fprintf (s, "\nAlias oracle query stats:\n");
107 fprintf (s, " refs_may_alias_p: "
108 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
109 HOST_WIDE_INT_PRINT_DEC" queries\n",
110 alias_stats.refs_may_alias_p_no_alias,
111 alias_stats.refs_may_alias_p_no_alias
112 + alias_stats.refs_may_alias_p_may_alias);
113 fprintf (s, " ref_maybe_used_by_call_p: "
114 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
115 HOST_WIDE_INT_PRINT_DEC" queries\n",
116 alias_stats.ref_maybe_used_by_call_p_no_alias,
117 alias_stats.refs_may_alias_p_no_alias
118 + alias_stats.ref_maybe_used_by_call_p_may_alias);
119 fprintf (s, " call_may_clobber_ref_p: "
120 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
121 HOST_WIDE_INT_PRINT_DEC" queries\n",
122 alias_stats.call_may_clobber_ref_p_no_alias,
123 alias_stats.call_may_clobber_ref_p_no_alias
124 + alias_stats.call_may_clobber_ref_p_may_alias);
125 dump_alias_stats_in_alias_c (s);
129 /* Return true, if dereferencing PTR may alias with a global variable. */
131 bool
132 ptr_deref_may_alias_global_p (tree ptr)
134 struct ptr_info_def *pi;
136 /* If we end up with a pointer constant here that may point
137 to global memory. */
138 if (TREE_CODE (ptr) != SSA_NAME)
139 return true;
141 pi = SSA_NAME_PTR_INFO (ptr);
143 /* If we do not have points-to information for this variable,
144 we have to punt. */
145 if (!pi)
146 return true;
148 /* ??? This does not use TBAA to prune globals ptr may not access. */
149 return pt_solution_includes_global (&pi->pt);
152 /* Return true if dereferencing PTR may alias DECL.
153 The caller is responsible for applying TBAA to see if PTR
154 may access DECL at all. */
156 static bool
157 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
159 struct ptr_info_def *pi;
161 /* Conversions are irrelevant for points-to information and
162 data-dependence analysis can feed us those. */
163 STRIP_NOPS (ptr);
165 /* Anything we do not explicilty handle aliases. */
166 if ((TREE_CODE (ptr) != SSA_NAME
167 && TREE_CODE (ptr) != ADDR_EXPR
168 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
169 || !POINTER_TYPE_P (TREE_TYPE (ptr))
170 || (!VAR_P (decl)
171 && TREE_CODE (decl) != PARM_DECL
172 && TREE_CODE (decl) != RESULT_DECL))
173 return true;
175 /* Disregard pointer offsetting. */
176 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
180 ptr = TREE_OPERAND (ptr, 0);
182 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
183 return ptr_deref_may_alias_decl_p (ptr, decl);
186 /* ADDR_EXPR pointers either just offset another pointer or directly
187 specify the pointed-to set. */
188 if (TREE_CODE (ptr) == ADDR_EXPR)
190 tree base = get_base_address (TREE_OPERAND (ptr, 0));
191 if (base
192 && (TREE_CODE (base) == MEM_REF
193 || TREE_CODE (base) == TARGET_MEM_REF))
194 ptr = TREE_OPERAND (base, 0);
195 else if (base
196 && DECL_P (base))
197 return compare_base_decls (base, decl) != 0;
198 else if (base
199 && CONSTANT_CLASS_P (base))
200 return false;
201 else
202 return true;
205 /* Non-aliased variables can not be pointed to. */
206 if (!may_be_aliased (decl))
207 return false;
209 /* If we do not have useful points-to information for this pointer
210 we cannot disambiguate anything else. */
211 pi = SSA_NAME_PTR_INFO (ptr);
212 if (!pi)
213 return true;
215 return pt_solution_includes (&pi->pt, decl);
218 /* Return true if dereferenced PTR1 and PTR2 may alias.
219 The caller is responsible for applying TBAA to see if accesses
220 through PTR1 and PTR2 may conflict at all. */
222 bool
223 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
225 struct ptr_info_def *pi1, *pi2;
227 /* Conversions are irrelevant for points-to information and
228 data-dependence analysis can feed us those. */
229 STRIP_NOPS (ptr1);
230 STRIP_NOPS (ptr2);
232 /* Disregard pointer offsetting. */
233 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
237 ptr1 = TREE_OPERAND (ptr1, 0);
239 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
240 return ptr_derefs_may_alias_p (ptr1, ptr2);
242 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
246 ptr2 = TREE_OPERAND (ptr2, 0);
248 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
249 return ptr_derefs_may_alias_p (ptr1, ptr2);
252 /* ADDR_EXPR pointers either just offset another pointer or directly
253 specify the pointed-to set. */
254 if (TREE_CODE (ptr1) == ADDR_EXPR)
256 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
257 if (base
258 && (TREE_CODE (base) == MEM_REF
259 || TREE_CODE (base) == TARGET_MEM_REF))
260 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
261 else if (base
262 && DECL_P (base))
263 return ptr_deref_may_alias_decl_p (ptr2, base);
264 else
265 return true;
267 if (TREE_CODE (ptr2) == ADDR_EXPR)
269 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
270 if (base
271 && (TREE_CODE (base) == MEM_REF
272 || TREE_CODE (base) == TARGET_MEM_REF))
273 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
274 else if (base
275 && DECL_P (base))
276 return ptr_deref_may_alias_decl_p (ptr1, base);
277 else
278 return true;
281 /* From here we require SSA name pointers. Anything else aliases. */
282 if (TREE_CODE (ptr1) != SSA_NAME
283 || TREE_CODE (ptr2) != SSA_NAME
284 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
285 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
286 return true;
288 /* We may end up with two empty points-to solutions for two same pointers.
289 In this case we still want to say both pointers alias, so shortcut
290 that here. */
291 if (ptr1 == ptr2)
292 return true;
294 /* If we do not have useful points-to information for either pointer
295 we cannot disambiguate anything else. */
296 pi1 = SSA_NAME_PTR_INFO (ptr1);
297 pi2 = SSA_NAME_PTR_INFO (ptr2);
298 if (!pi1 || !pi2)
299 return true;
301 /* ??? This does not use TBAA to prune decls from the intersection
302 that not both pointers may access. */
303 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
306 /* Return true if dereferencing PTR may alias *REF.
307 The caller is responsible for applying TBAA to see if PTR
308 may access *REF at all. */
310 static bool
311 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
313 tree base = ao_ref_base (ref);
315 if (TREE_CODE (base) == MEM_REF
316 || TREE_CODE (base) == TARGET_MEM_REF)
317 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
318 else if (DECL_P (base))
319 return ptr_deref_may_alias_decl_p (ptr, base);
321 return true;
324 /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
326 bool
327 ptrs_compare_unequal (tree ptr1, tree ptr2)
329 /* First resolve the pointers down to a SSA name pointer base or
330 a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
331 not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
332 or STRING_CSTs which needs points-to adjustments to track them
333 in the points-to sets. */
334 tree obj1 = NULL_TREE;
335 tree obj2 = NULL_TREE;
336 if (TREE_CODE (ptr1) == ADDR_EXPR)
338 tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
339 if (! tem)
340 return false;
341 if (VAR_P (tem)
342 || TREE_CODE (tem) == PARM_DECL
343 || TREE_CODE (tem) == RESULT_DECL)
344 obj1 = tem;
345 else if (TREE_CODE (tem) == MEM_REF)
346 ptr1 = TREE_OPERAND (tem, 0);
348 if (TREE_CODE (ptr2) == ADDR_EXPR)
350 tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
351 if (! tem)
352 return false;
353 if (VAR_P (tem)
354 || TREE_CODE (tem) == PARM_DECL
355 || TREE_CODE (tem) == RESULT_DECL)
356 obj2 = tem;
357 else if (TREE_CODE (tem) == MEM_REF)
358 ptr2 = TREE_OPERAND (tem, 0);
361 /* Canonicalize ptr vs. object. */
362 if (TREE_CODE (ptr1) == SSA_NAME && obj2)
364 std::swap (ptr1, ptr2);
365 std::swap (obj1, obj2);
368 if (obj1 && obj2)
369 /* Other code handles this correctly, no need to duplicate it here. */;
370 else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
372 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
373 /* We may not use restrict to optimize pointer comparisons.
374 See PR71062. So we have to assume that restrict-pointed-to
375 may be in fact obj1. */
376 if (!pi || pi->pt.vars_contains_restrict)
377 return false;
378 if (VAR_P (obj1)
379 && (TREE_STATIC (obj1) || DECL_EXTERNAL (obj1)))
381 varpool_node *node = varpool_node::get (obj1);
382 /* If obj1 may bind to NULL give up (see below). */
383 if (! node || ! node->nonzero_address ())
384 return false;
386 return !pt_solution_includes (&pi->pt, obj1);
389 /* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
390 but those require pt.null to be conservatively correct. */
392 return false;
395 /* Returns whether reference REF to BASE may refer to global memory. */
397 static bool
398 ref_may_alias_global_p_1 (tree base)
400 if (DECL_P (base))
401 return is_global_var (base);
402 else if (TREE_CODE (base) == MEM_REF
403 || TREE_CODE (base) == TARGET_MEM_REF)
404 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
405 return true;
408 bool
409 ref_may_alias_global_p (ao_ref *ref)
411 tree base = ao_ref_base (ref);
412 return ref_may_alias_global_p_1 (base);
415 bool
416 ref_may_alias_global_p (tree ref)
418 tree base = get_base_address (ref);
419 return ref_may_alias_global_p_1 (base);
422 /* Return true whether STMT may clobber global memory. */
424 bool
425 stmt_may_clobber_global_p (gimple *stmt)
427 tree lhs;
429 if (!gimple_vdef (stmt))
430 return false;
432 /* ??? We can ask the oracle whether an artificial pointer
433 dereference with a pointer with points-to information covering
434 all global memory (what about non-address taken memory?) maybe
435 clobbered by this call. As there is at the moment no convenient
436 way of doing that without generating garbage do some manual
437 checking instead.
438 ??? We could make a NULL ao_ref argument to the various
439 predicates special, meaning any global memory. */
441 switch (gimple_code (stmt))
443 case GIMPLE_ASSIGN:
444 lhs = gimple_assign_lhs (stmt);
445 return (TREE_CODE (lhs) != SSA_NAME
446 && ref_may_alias_global_p (lhs));
447 case GIMPLE_CALL:
448 return true;
449 default:
450 return true;
455 /* Dump alias information on FILE. */
457 void
458 dump_alias_info (FILE *file)
460 unsigned i;
461 tree ptr;
462 const char *funcname
463 = lang_hooks.decl_printable_name (current_function_decl, 2);
464 tree var;
466 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
468 fprintf (file, "Aliased symbols\n\n");
470 FOR_EACH_LOCAL_DECL (cfun, i, var)
472 if (may_be_aliased (var))
473 dump_variable (file, var);
476 fprintf (file, "\nCall clobber information\n");
478 fprintf (file, "\nESCAPED");
479 dump_points_to_solution (file, &cfun->gimple_df->escaped);
481 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
483 FOR_EACH_SSA_NAME (i, ptr, cfun)
485 struct ptr_info_def *pi;
487 if (!POINTER_TYPE_P (TREE_TYPE (ptr))
488 || SSA_NAME_IN_FREE_LIST (ptr))
489 continue;
491 pi = SSA_NAME_PTR_INFO (ptr);
492 if (pi)
493 dump_points_to_info_for (file, ptr);
496 fprintf (file, "\n");
500 /* Dump alias information on stderr. */
502 DEBUG_FUNCTION void
503 debug_alias_info (void)
505 dump_alias_info (stderr);
509 /* Dump the points-to set *PT into FILE. */
511 void
512 dump_points_to_solution (FILE *file, struct pt_solution *pt)
514 if (pt->anything)
515 fprintf (file, ", points-to anything");
517 if (pt->nonlocal)
518 fprintf (file, ", points-to non-local");
520 if (pt->escaped)
521 fprintf (file, ", points-to escaped");
523 if (pt->ipa_escaped)
524 fprintf (file, ", points-to unit escaped");
526 if (pt->null)
527 fprintf (file, ", points-to NULL");
529 if (pt->vars)
531 fprintf (file, ", points-to vars: ");
532 dump_decl_set (file, pt->vars);
533 if (pt->vars_contains_nonlocal
534 || pt->vars_contains_escaped
535 || pt->vars_contains_escaped_heap
536 || pt->vars_contains_restrict)
538 const char *comma = "";
539 fprintf (file, " (");
540 if (pt->vars_contains_nonlocal)
542 fprintf (file, "nonlocal");
543 comma = ", ";
545 if (pt->vars_contains_escaped)
547 fprintf (file, "%sescaped", comma);
548 comma = ", ";
550 if (pt->vars_contains_escaped_heap)
552 fprintf (file, "%sescaped heap", comma);
553 comma = ", ";
555 if (pt->vars_contains_restrict)
556 fprintf (file, "%srestrict", comma);
557 fprintf (file, ")");
563 /* Unified dump function for pt_solution. */
565 DEBUG_FUNCTION void
566 debug (pt_solution &ref)
568 dump_points_to_solution (stderr, &ref);
571 DEBUG_FUNCTION void
572 debug (pt_solution *ptr)
574 if (ptr)
575 debug (*ptr);
576 else
577 fprintf (stderr, "<nil>\n");
581 /* Dump points-to information for SSA_NAME PTR into FILE. */
583 void
584 dump_points_to_info_for (FILE *file, tree ptr)
586 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
588 print_generic_expr (file, ptr, dump_flags);
590 if (pi)
591 dump_points_to_solution (file, &pi->pt);
592 else
593 fprintf (file, ", points-to anything");
595 fprintf (file, "\n");
599 /* Dump points-to information for VAR into stderr. */
601 DEBUG_FUNCTION void
602 debug_points_to_info_for (tree var)
604 dump_points_to_info_for (stderr, var);
608 /* Initializes the alias-oracle reference representation *R from REF. */
610 void
611 ao_ref_init (ao_ref *r, tree ref)
613 r->ref = ref;
614 r->base = NULL_TREE;
615 r->offset = 0;
616 r->size = -1;
617 r->max_size = -1;
618 r->ref_alias_set = -1;
619 r->base_alias_set = -1;
620 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
623 /* Returns the base object of the memory reference *REF. */
625 tree
626 ao_ref_base (ao_ref *ref)
628 bool reverse;
630 if (ref->base)
631 return ref->base;
632 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
633 &ref->max_size, &reverse);
634 return ref->base;
637 /* Returns the base object alias set of the memory reference *REF. */
639 alias_set_type
640 ao_ref_base_alias_set (ao_ref *ref)
642 tree base_ref;
643 if (ref->base_alias_set != -1)
644 return ref->base_alias_set;
645 if (!ref->ref)
646 return 0;
647 base_ref = ref->ref;
648 while (handled_component_p (base_ref))
649 base_ref = TREE_OPERAND (base_ref, 0);
650 ref->base_alias_set = get_alias_set (base_ref);
651 return ref->base_alias_set;
654 /* Returns the reference alias set of the memory reference *REF. */
656 alias_set_type
657 ao_ref_alias_set (ao_ref *ref)
659 if (ref->ref_alias_set != -1)
660 return ref->ref_alias_set;
661 ref->ref_alias_set = get_alias_set (ref->ref);
662 return ref->ref_alias_set;
665 /* Init an alias-oracle reference representation from a gimple pointer
666 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
667 size is assumed to be unknown. The access is assumed to be only
668 to or after of the pointer target, not before it. */
670 void
671 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
673 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
674 ref->ref = NULL_TREE;
675 if (TREE_CODE (ptr) == SSA_NAME)
677 gimple *stmt = SSA_NAME_DEF_STMT (ptr);
678 if (gimple_assign_single_p (stmt)
679 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
680 ptr = gimple_assign_rhs1 (stmt);
681 else if (is_gimple_assign (stmt)
682 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
683 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
685 ptr = gimple_assign_rhs1 (stmt);
686 extra_offset = BITS_PER_UNIT
687 * int_cst_value (gimple_assign_rhs2 (stmt));
691 if (TREE_CODE (ptr) == ADDR_EXPR)
693 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
694 if (ref->base)
695 ref->offset = BITS_PER_UNIT * t;
696 else
698 size = NULL_TREE;
699 ref->offset = 0;
700 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
703 else
705 ref->base = build2 (MEM_REF, char_type_node,
706 ptr, null_pointer_node);
707 ref->offset = 0;
709 ref->offset += extra_offset;
710 if (size
711 && tree_fits_shwi_p (size)
712 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
713 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
714 else
715 ref->max_size = ref->size = -1;
716 ref->ref_alias_set = 0;
717 ref->base_alias_set = 0;
718 ref->volatile_p = false;
721 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
722 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
723 decide. */
725 static inline int
726 same_type_for_tbaa (tree type1, tree type2)
728 type1 = TYPE_MAIN_VARIANT (type1);
729 type2 = TYPE_MAIN_VARIANT (type2);
731 /* If we would have to do structural comparison bail out. */
732 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
733 || TYPE_STRUCTURAL_EQUALITY_P (type2))
734 return -1;
736 /* Compare the canonical types. */
737 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
738 return 1;
740 /* ??? Array types are not properly unified in all cases as we have
741 spurious changes in the index types for example. Removing this
742 causes all sorts of problems with the Fortran frontend. */
743 if (TREE_CODE (type1) == ARRAY_TYPE
744 && TREE_CODE (type2) == ARRAY_TYPE)
745 return -1;
747 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
748 object of one of its constrained subtypes, e.g. when a function with an
749 unconstrained parameter passed by reference is called on an object and
750 inlined. But, even in the case of a fixed size, type and subtypes are
751 not equivalent enough as to share the same TYPE_CANONICAL, since this
752 would mean that conversions between them are useless, whereas they are
753 not (e.g. type and subtypes can have different modes). So, in the end,
754 they are only guaranteed to have the same alias set. */
755 if (get_alias_set (type1) == get_alias_set (type2))
756 return -1;
758 /* The types are known to be not equal. */
759 return 0;
762 /* Determine if the two component references REF1 and REF2 which are
763 based on access types TYPE1 and TYPE2 and of which at least one is based
764 on an indirect reference may alias. REF2 is the only one that can
765 be a decl in which case REF2_IS_DECL is true.
766 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
767 are the respective alias sets. */
769 static bool
770 aliasing_component_refs_p (tree ref1,
771 alias_set_type ref1_alias_set,
772 alias_set_type base1_alias_set,
773 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
774 tree ref2,
775 alias_set_type ref2_alias_set,
776 alias_set_type base2_alias_set,
777 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
778 bool ref2_is_decl)
780 /* If one reference is a component references through pointers try to find a
781 common base and apply offset based disambiguation. This handles
782 for example
783 struct A { int i; int j; } *q;
784 struct B { struct A a; int k; } *p;
785 disambiguating q->i and p->a.j. */
786 tree base1, base2;
787 tree type1, type2;
788 tree *refp;
789 int same_p;
791 /* Choose bases and base types to search for. */
792 base1 = ref1;
793 while (handled_component_p (base1))
794 base1 = TREE_OPERAND (base1, 0);
795 type1 = TREE_TYPE (base1);
796 base2 = ref2;
797 while (handled_component_p (base2))
798 base2 = TREE_OPERAND (base2, 0);
799 type2 = TREE_TYPE (base2);
801 /* Now search for the type1 in the access path of ref2. This
802 would be a common base for doing offset based disambiguation on. */
803 refp = &ref2;
804 while (handled_component_p (*refp)
805 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
806 refp = &TREE_OPERAND (*refp, 0);
807 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
808 /* If we couldn't compare types we have to bail out. */
809 if (same_p == -1)
810 return true;
811 else if (same_p == 1)
813 HOST_WIDE_INT offadj, sztmp, msztmp;
814 bool reverse;
815 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp, &reverse);
816 offset2 -= offadj;
817 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp, &reverse);
818 offset1 -= offadj;
819 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
821 /* If we didn't find a common base, try the other way around. */
822 refp = &ref1;
823 while (handled_component_p (*refp)
824 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
825 refp = &TREE_OPERAND (*refp, 0);
826 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
827 /* If we couldn't compare types we have to bail out. */
828 if (same_p == -1)
829 return true;
830 else if (same_p == 1)
832 HOST_WIDE_INT offadj, sztmp, msztmp;
833 bool reverse;
834 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp, &reverse);
835 offset1 -= offadj;
836 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp, &reverse);
837 offset2 -= offadj;
838 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
841 /* If we have two type access paths B1.path1 and B2.path2 they may
842 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
843 But we can still have a path that goes B1.path1...B2.path2 with
844 a part that we do not see. So we can only disambiguate now
845 if there is no B2 in the tail of path1 and no B1 on the
846 tail of path2. */
847 if (base1_alias_set == ref2_alias_set
848 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
849 return true;
850 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
851 if (!ref2_is_decl)
852 return (base2_alias_set == ref1_alias_set
853 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
854 return false;
857 /* Return true if we can determine that component references REF1 and REF2,
858 that are within a common DECL, cannot overlap. */
860 static bool
861 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
863 auto_vec<tree, 16> component_refs1;
864 auto_vec<tree, 16> component_refs2;
866 /* Create the stack of handled components for REF1. */
867 while (handled_component_p (ref1))
869 component_refs1.safe_push (ref1);
870 ref1 = TREE_OPERAND (ref1, 0);
872 if (TREE_CODE (ref1) == MEM_REF)
874 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
875 return false;
876 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
879 /* Create the stack of handled components for REF2. */
880 while (handled_component_p (ref2))
882 component_refs2.safe_push (ref2);
883 ref2 = TREE_OPERAND (ref2, 0);
885 if (TREE_CODE (ref2) == MEM_REF)
887 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
888 return false;
889 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
892 /* Bases must be either same or uncomparable. */
893 gcc_checking_assert (ref1 == ref2
894 || (DECL_P (ref1) && DECL_P (ref2)
895 && compare_base_decls (ref1, ref2) != 0));
897 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
898 rank. This is sufficient because we start from the same DECL and you
899 cannot reference several fields at a time with COMPONENT_REFs (unlike
900 with ARRAY_RANGE_REFs for arrays) so you always need the same number
901 of them to access a sub-component, unless you're in a union, in which
902 case the return value will precisely be false. */
903 while (true)
907 if (component_refs1.is_empty ())
908 return false;
909 ref1 = component_refs1.pop ();
911 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
915 if (component_refs2.is_empty ())
916 return false;
917 ref2 = component_refs2.pop ();
919 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
921 /* Beware of BIT_FIELD_REF. */
922 if (TREE_CODE (ref1) != COMPONENT_REF
923 || TREE_CODE (ref2) != COMPONENT_REF)
924 return false;
926 tree field1 = TREE_OPERAND (ref1, 1);
927 tree field2 = TREE_OPERAND (ref2, 1);
929 /* ??? We cannot simply use the type of operand #0 of the refs here
930 as the Fortran compiler smuggles type punning into COMPONENT_REFs
931 for common blocks instead of using unions like everyone else. */
932 tree type1 = DECL_CONTEXT (field1);
933 tree type2 = DECL_CONTEXT (field2);
935 /* We cannot disambiguate fields in a union or qualified union. */
936 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
937 return false;
939 if (field1 != field2)
941 /* A field and its representative need to be considered the
942 same. */
943 if (DECL_BIT_FIELD_REPRESENTATIVE (field1) == field2
944 || DECL_BIT_FIELD_REPRESENTATIVE (field2) == field1)
945 return false;
946 /* Different fields of the same record type cannot overlap.
947 ??? Bitfields can overlap at RTL level so punt on them. */
948 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
949 return false;
950 return true;
954 return false;
957 /* qsort compare function to sort FIELD_DECLs after their
958 DECL_FIELD_CONTEXT TYPE_UID. */
960 static inline int
961 ncr_compar (const void *field1_, const void *field2_)
963 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
964 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
965 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
966 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
967 if (uid1 < uid2)
968 return -1;
969 else if (uid1 > uid2)
970 return 1;
971 return 0;
974 /* Return true if we can determine that the fields referenced cannot
975 overlap for any pair of objects. */
977 static bool
978 nonoverlapping_component_refs_p (const_tree x, const_tree y)
980 if (!flag_strict_aliasing
981 || !x || !y
982 || TREE_CODE (x) != COMPONENT_REF
983 || TREE_CODE (y) != COMPONENT_REF)
984 return false;
986 auto_vec<const_tree, 16> fieldsx;
987 while (TREE_CODE (x) == COMPONENT_REF)
989 tree field = TREE_OPERAND (x, 1);
990 tree type = DECL_FIELD_CONTEXT (field);
991 if (TREE_CODE (type) == RECORD_TYPE)
992 fieldsx.safe_push (field);
993 x = TREE_OPERAND (x, 0);
995 if (fieldsx.length () == 0)
996 return false;
997 auto_vec<const_tree, 16> fieldsy;
998 while (TREE_CODE (y) == COMPONENT_REF)
1000 tree field = TREE_OPERAND (y, 1);
1001 tree type = DECL_FIELD_CONTEXT (field);
1002 if (TREE_CODE (type) == RECORD_TYPE)
1003 fieldsy.safe_push (TREE_OPERAND (y, 1));
1004 y = TREE_OPERAND (y, 0);
1006 if (fieldsy.length () == 0)
1007 return false;
1009 /* Most common case first. */
1010 if (fieldsx.length () == 1
1011 && fieldsy.length () == 1)
1012 return ((DECL_FIELD_CONTEXT (fieldsx[0])
1013 == DECL_FIELD_CONTEXT (fieldsy[0]))
1014 && fieldsx[0] != fieldsy[0]
1015 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
1017 if (fieldsx.length () == 2)
1019 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1020 std::swap (fieldsx[0], fieldsx[1]);
1022 else
1023 fieldsx.qsort (ncr_compar);
1025 if (fieldsy.length () == 2)
1027 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1028 std::swap (fieldsy[0], fieldsy[1]);
1030 else
1031 fieldsy.qsort (ncr_compar);
1033 unsigned i = 0, j = 0;
1036 const_tree fieldx = fieldsx[i];
1037 const_tree fieldy = fieldsy[j];
1038 tree typex = DECL_FIELD_CONTEXT (fieldx);
1039 tree typey = DECL_FIELD_CONTEXT (fieldy);
1040 if (typex == typey)
1042 /* We're left with accessing different fields of a structure,
1043 no possible overlap. */
1044 if (fieldx != fieldy)
1046 /* A field and its representative need to be considered the
1047 same. */
1048 if (DECL_BIT_FIELD_REPRESENTATIVE (fieldx) == fieldy
1049 || DECL_BIT_FIELD_REPRESENTATIVE (fieldy) == fieldx)
1050 return false;
1051 /* Different fields of the same record type cannot overlap.
1052 ??? Bitfields can overlap at RTL level so punt on them. */
1053 if (DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy))
1054 return false;
1055 return true;
1058 if (TYPE_UID (typex) < TYPE_UID (typey))
1060 i++;
1061 if (i == fieldsx.length ())
1062 break;
1064 else
1066 j++;
1067 if (j == fieldsy.length ())
1068 break;
1071 while (1);
1073 return false;
1077 /* Return true if two memory references based on the variables BASE1
1078 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1079 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1080 if non-NULL are the complete memory reference trees. */
1082 static bool
1083 decl_refs_may_alias_p (tree ref1, tree base1,
1084 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1085 tree ref2, tree base2,
1086 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1088 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1090 /* If both references are based on different variables, they cannot alias. */
1091 if (compare_base_decls (base1, base2) == 0)
1092 return false;
1094 /* If both references are based on the same variable, they cannot alias if
1095 the accesses do not overlap. */
1096 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1097 return false;
1099 /* For components with variable position, the above test isn't sufficient,
1100 so we disambiguate component references manually. */
1101 if (ref1 && ref2
1102 && handled_component_p (ref1) && handled_component_p (ref2)
1103 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1104 return false;
1106 return true;
1109 /* Return true if an indirect reference based on *PTR1 constrained
1110 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1111 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1112 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1113 in which case they are computed on-demand. REF1 and REF2
1114 if non-NULL are the complete memory reference trees. */
1116 static bool
1117 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1118 HOST_WIDE_INT offset1,
1119 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1120 alias_set_type ref1_alias_set,
1121 alias_set_type base1_alias_set,
1122 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1123 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1124 alias_set_type ref2_alias_set,
1125 alias_set_type base2_alias_set, bool tbaa_p)
1127 tree ptr1;
1128 tree ptrtype1, dbase2;
1129 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1130 HOST_WIDE_INT doffset1, doffset2;
1132 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1133 || TREE_CODE (base1) == TARGET_MEM_REF)
1134 && DECL_P (base2));
1136 ptr1 = TREE_OPERAND (base1, 0);
1138 /* The offset embedded in MEM_REFs can be negative. Bias them
1139 so that the resulting offset adjustment is positive. */
1140 offset_int moff = mem_ref_offset (base1);
1141 moff <<= LOG2_BITS_PER_UNIT;
1142 if (wi::neg_p (moff))
1143 offset2p += (-moff).to_short_addr ();
1144 else
1145 offset1p += moff.to_short_addr ();
1147 /* If only one reference is based on a variable, they cannot alias if
1148 the pointer access is beyond the extent of the variable access.
1149 (the pointer base cannot validly point to an offset less than zero
1150 of the variable).
1151 ??? IVOPTs creates bases that do not honor this restriction,
1152 so do not apply this optimization for TARGET_MEM_REFs. */
1153 if (TREE_CODE (base1) != TARGET_MEM_REF
1154 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1155 return false;
1156 /* They also cannot alias if the pointer may not point to the decl. */
1157 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1158 return false;
1160 /* Disambiguations that rely on strict aliasing rules follow. */
1161 if (!flag_strict_aliasing || !tbaa_p)
1162 return true;
1164 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1166 /* If the alias set for a pointer access is zero all bets are off. */
1167 if (base1_alias_set == 0)
1168 return true;
1170 /* When we are trying to disambiguate an access with a pointer dereference
1171 as base versus one with a decl as base we can use both the size
1172 of the decl and its dynamic type for extra disambiguation.
1173 ??? We do not know anything about the dynamic type of the decl
1174 other than that its alias-set contains base2_alias_set as a subset
1175 which does not help us here. */
1176 /* As we know nothing useful about the dynamic type of the decl just
1177 use the usual conflict check rather than a subset test.
1178 ??? We could introduce -fvery-strict-aliasing when the language
1179 does not allow decls to have a dynamic type that differs from their
1180 static type. Then we can check
1181 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1182 if (base1_alias_set != base2_alias_set
1183 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1184 return false;
1185 /* If the size of the access relevant for TBAA through the pointer
1186 is bigger than the size of the decl we can't possibly access the
1187 decl via that pointer. */
1188 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1189 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1190 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1191 /* ??? This in turn may run afoul when a decl of type T which is
1192 a member of union type U is accessed through a pointer to
1193 type U and sizeof T is smaller than sizeof U. */
1194 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1195 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1196 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1197 return false;
1199 if (!ref2)
1200 return true;
1202 /* If the decl is accessed via a MEM_REF, reconstruct the base
1203 we can use for TBAA and an appropriately adjusted offset. */
1204 dbase2 = ref2;
1205 while (handled_component_p (dbase2))
1206 dbase2 = TREE_OPERAND (dbase2, 0);
1207 doffset1 = offset1;
1208 doffset2 = offset2;
1209 if (TREE_CODE (dbase2) == MEM_REF
1210 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1212 offset_int moff = mem_ref_offset (dbase2);
1213 moff <<= LOG2_BITS_PER_UNIT;
1214 if (wi::neg_p (moff))
1215 doffset1 -= (-moff).to_short_addr ();
1216 else
1217 doffset2 -= moff.to_short_addr ();
1220 /* If either reference is view-converted, give up now. */
1221 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1222 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1223 return true;
1225 /* If both references are through the same type, they do not alias
1226 if the accesses do not overlap. This does extra disambiguation
1227 for mixed/pointer accesses but requires strict aliasing.
1228 For MEM_REFs we require that the component-ref offset we computed
1229 is relative to the start of the type which we ensure by
1230 comparing rvalue and access type and disregarding the constant
1231 pointer offset. */
1232 if ((TREE_CODE (base1) != TARGET_MEM_REF
1233 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1234 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1235 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1237 if (ref1 && ref2
1238 && nonoverlapping_component_refs_p (ref1, ref2))
1239 return false;
1241 /* Do access-path based disambiguation. */
1242 if (ref1 && ref2
1243 && (handled_component_p (ref1) || handled_component_p (ref2)))
1244 return aliasing_component_refs_p (ref1,
1245 ref1_alias_set, base1_alias_set,
1246 offset1, max_size1,
1247 ref2,
1248 ref2_alias_set, base2_alias_set,
1249 offset2, max_size2, true);
1251 return true;
1254 /* Return true if two indirect references based on *PTR1
1255 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1256 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1257 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1258 in which case they are computed on-demand. REF1 and REF2
1259 if non-NULL are the complete memory reference trees. */
1261 static bool
1262 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1263 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1264 alias_set_type ref1_alias_set,
1265 alias_set_type base1_alias_set,
1266 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1267 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1268 alias_set_type ref2_alias_set,
1269 alias_set_type base2_alias_set, bool tbaa_p)
1271 tree ptr1;
1272 tree ptr2;
1273 tree ptrtype1, ptrtype2;
1275 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1276 || TREE_CODE (base1) == TARGET_MEM_REF)
1277 && (TREE_CODE (base2) == MEM_REF
1278 || TREE_CODE (base2) == TARGET_MEM_REF));
1280 ptr1 = TREE_OPERAND (base1, 0);
1281 ptr2 = TREE_OPERAND (base2, 0);
1283 /* If both bases are based on pointers they cannot alias if they may not
1284 point to the same memory object or if they point to the same object
1285 and the accesses do not overlap. */
1286 if ((!cfun || gimple_in_ssa_p (cfun))
1287 && operand_equal_p (ptr1, ptr2, 0)
1288 && (((TREE_CODE (base1) != TARGET_MEM_REF
1289 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1290 && (TREE_CODE (base2) != TARGET_MEM_REF
1291 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1292 || (TREE_CODE (base1) == TARGET_MEM_REF
1293 && TREE_CODE (base2) == TARGET_MEM_REF
1294 && (TMR_STEP (base1) == TMR_STEP (base2)
1295 || (TMR_STEP (base1) && TMR_STEP (base2)
1296 && operand_equal_p (TMR_STEP (base1),
1297 TMR_STEP (base2), 0)))
1298 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1299 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1300 && operand_equal_p (TMR_INDEX (base1),
1301 TMR_INDEX (base2), 0)))
1302 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1303 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1304 && operand_equal_p (TMR_INDEX2 (base1),
1305 TMR_INDEX2 (base2), 0))))))
1307 offset_int moff;
1308 /* The offset embedded in MEM_REFs can be negative. Bias them
1309 so that the resulting offset adjustment is positive. */
1310 moff = mem_ref_offset (base1);
1311 moff <<= LOG2_BITS_PER_UNIT;
1312 if (wi::neg_p (moff))
1313 offset2 += (-moff).to_short_addr ();
1314 else
1315 offset1 += moff.to_shwi ();
1316 moff = mem_ref_offset (base2);
1317 moff <<= LOG2_BITS_PER_UNIT;
1318 if (wi::neg_p (moff))
1319 offset1 += (-moff).to_short_addr ();
1320 else
1321 offset2 += moff.to_short_addr ();
1322 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1324 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1325 return false;
1327 /* Disambiguations that rely on strict aliasing rules follow. */
1328 if (!flag_strict_aliasing || !tbaa_p)
1329 return true;
1331 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1332 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1334 /* If the alias set for a pointer access is zero all bets are off. */
1335 if (base1_alias_set == 0
1336 || base2_alias_set == 0)
1337 return true;
1339 /* If both references are through the same type, they do not alias
1340 if the accesses do not overlap. This does extra disambiguation
1341 for mixed/pointer accesses but requires strict aliasing. */
1342 if ((TREE_CODE (base1) != TARGET_MEM_REF
1343 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1344 && (TREE_CODE (base2) != TARGET_MEM_REF
1345 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1346 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1347 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1348 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1349 TREE_TYPE (ptrtype2)) == 1)
1350 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1352 /* Do type-based disambiguation. */
1353 if (base1_alias_set != base2_alias_set
1354 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1355 return false;
1357 /* If either reference is view-converted, give up now. */
1358 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1359 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1360 return true;
1362 if (ref1 && ref2
1363 && nonoverlapping_component_refs_p (ref1, ref2))
1364 return false;
1366 /* Do access-path based disambiguation. */
1367 if (ref1 && ref2
1368 && (handled_component_p (ref1) || handled_component_p (ref2)))
1369 return aliasing_component_refs_p (ref1,
1370 ref1_alias_set, base1_alias_set,
1371 offset1, max_size1,
1372 ref2,
1373 ref2_alias_set, base2_alias_set,
1374 offset2, max_size2, false);
1376 return true;
1379 /* Return true, if the two memory references REF1 and REF2 may alias. */
1381 bool
1382 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1384 tree base1, base2;
1385 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1386 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1387 bool var1_p, var2_p, ind1_p, ind2_p;
1389 gcc_checking_assert ((!ref1->ref
1390 || TREE_CODE (ref1->ref) == SSA_NAME
1391 || DECL_P (ref1->ref)
1392 || TREE_CODE (ref1->ref) == STRING_CST
1393 || handled_component_p (ref1->ref)
1394 || TREE_CODE (ref1->ref) == MEM_REF
1395 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1396 && (!ref2->ref
1397 || TREE_CODE (ref2->ref) == SSA_NAME
1398 || DECL_P (ref2->ref)
1399 || TREE_CODE (ref2->ref) == STRING_CST
1400 || handled_component_p (ref2->ref)
1401 || TREE_CODE (ref2->ref) == MEM_REF
1402 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1404 /* Decompose the references into their base objects and the access. */
1405 base1 = ao_ref_base (ref1);
1406 offset1 = ref1->offset;
1407 max_size1 = ref1->max_size;
1408 base2 = ao_ref_base (ref2);
1409 offset2 = ref2->offset;
1410 max_size2 = ref2->max_size;
1412 /* We can end up with registers or constants as bases for example from
1413 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1414 which is seen as a struct copy. */
1415 if (TREE_CODE (base1) == SSA_NAME
1416 || TREE_CODE (base1) == CONST_DECL
1417 || TREE_CODE (base1) == CONSTRUCTOR
1418 || TREE_CODE (base1) == ADDR_EXPR
1419 || CONSTANT_CLASS_P (base1)
1420 || TREE_CODE (base2) == SSA_NAME
1421 || TREE_CODE (base2) == CONST_DECL
1422 || TREE_CODE (base2) == CONSTRUCTOR
1423 || TREE_CODE (base2) == ADDR_EXPR
1424 || CONSTANT_CLASS_P (base2))
1425 return false;
1427 /* We can end up referring to code via function and label decls.
1428 As we likely do not properly track code aliases conservatively
1429 bail out. */
1430 if (TREE_CODE (base1) == FUNCTION_DECL
1431 || TREE_CODE (base1) == LABEL_DECL
1432 || TREE_CODE (base2) == FUNCTION_DECL
1433 || TREE_CODE (base2) == LABEL_DECL)
1434 return true;
1436 /* Two volatile accesses always conflict. */
1437 if (ref1->volatile_p
1438 && ref2->volatile_p)
1439 return true;
1441 /* Defer to simple offset based disambiguation if we have
1442 references based on two decls. Do this before defering to
1443 TBAA to handle must-alias cases in conformance with the
1444 GCC extension of allowing type-punning through unions. */
1445 var1_p = DECL_P (base1);
1446 var2_p = DECL_P (base2);
1447 if (var1_p && var2_p)
1448 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1449 ref2->ref, base2, offset2, max_size2);
1451 /* Handle restrict based accesses.
1452 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1453 here. */
1454 tree rbase1 = base1;
1455 tree rbase2 = base2;
1456 if (var1_p)
1458 rbase1 = ref1->ref;
1459 if (rbase1)
1460 while (handled_component_p (rbase1))
1461 rbase1 = TREE_OPERAND (rbase1, 0);
1463 if (var2_p)
1465 rbase2 = ref2->ref;
1466 if (rbase2)
1467 while (handled_component_p (rbase2))
1468 rbase2 = TREE_OPERAND (rbase2, 0);
1470 if (rbase1 && rbase2
1471 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1472 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1473 /* If the accesses are in the same restrict clique... */
1474 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1475 /* But based on different pointers they do not alias. */
1476 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1477 return false;
1479 ind1_p = (TREE_CODE (base1) == MEM_REF
1480 || TREE_CODE (base1) == TARGET_MEM_REF);
1481 ind2_p = (TREE_CODE (base2) == MEM_REF
1482 || TREE_CODE (base2) == TARGET_MEM_REF);
1484 /* Canonicalize the pointer-vs-decl case. */
1485 if (ind1_p && var2_p)
1487 std::swap (offset1, offset2);
1488 std::swap (max_size1, max_size2);
1489 std::swap (base1, base2);
1490 std::swap (ref1, ref2);
1491 var1_p = true;
1492 ind1_p = false;
1493 var2_p = false;
1494 ind2_p = true;
1497 /* First defer to TBAA if possible. */
1498 if (tbaa_p
1499 && flag_strict_aliasing
1500 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1501 ao_ref_alias_set (ref2)))
1502 return false;
1504 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1505 if (var1_p && ind2_p)
1506 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1507 offset2, max_size2,
1508 ao_ref_alias_set (ref2),
1509 ao_ref_base_alias_set (ref2),
1510 ref1->ref, base1,
1511 offset1, max_size1,
1512 ao_ref_alias_set (ref1),
1513 ao_ref_base_alias_set (ref1),
1514 tbaa_p);
1515 else if (ind1_p && ind2_p)
1516 return indirect_refs_may_alias_p (ref1->ref, base1,
1517 offset1, max_size1,
1518 ao_ref_alias_set (ref1),
1519 ao_ref_base_alias_set (ref1),
1520 ref2->ref, base2,
1521 offset2, max_size2,
1522 ao_ref_alias_set (ref2),
1523 ao_ref_base_alias_set (ref2),
1524 tbaa_p);
1526 gcc_unreachable ();
1529 static bool
1530 refs_may_alias_p (tree ref1, ao_ref *ref2)
1532 ao_ref r1;
1533 ao_ref_init (&r1, ref1);
1534 return refs_may_alias_p_1 (&r1, ref2, true);
1537 bool
1538 refs_may_alias_p (tree ref1, tree ref2)
1540 ao_ref r1, r2;
1541 bool res;
1542 ao_ref_init (&r1, ref1);
1543 ao_ref_init (&r2, ref2);
1544 res = refs_may_alias_p_1 (&r1, &r2, true);
1545 if (res)
1546 ++alias_stats.refs_may_alias_p_may_alias;
1547 else
1548 ++alias_stats.refs_may_alias_p_no_alias;
1549 return res;
1552 /* Returns true if there is a anti-dependence for the STORE that
1553 executes after the LOAD. */
1555 bool
1556 refs_anti_dependent_p (tree load, tree store)
1558 ao_ref r1, r2;
1559 ao_ref_init (&r1, load);
1560 ao_ref_init (&r2, store);
1561 return refs_may_alias_p_1 (&r1, &r2, false);
1564 /* Returns true if there is a output dependence for the stores
1565 STORE1 and STORE2. */
1567 bool
1568 refs_output_dependent_p (tree store1, tree store2)
1570 ao_ref r1, r2;
1571 ao_ref_init (&r1, store1);
1572 ao_ref_init (&r2, store2);
1573 return refs_may_alias_p_1 (&r1, &r2, false);
1576 /* If the call CALL may use the memory reference REF return true,
1577 otherwise return false. */
1579 static bool
1580 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1582 tree base, callee;
1583 unsigned i;
1584 int flags = gimple_call_flags (call);
1586 /* Const functions without a static chain do not implicitly use memory. */
1587 if (!gimple_call_chain (call)
1588 && (flags & (ECF_CONST|ECF_NOVOPS)))
1589 goto process_args;
1591 base = ao_ref_base (ref);
1592 if (!base)
1593 return true;
1595 /* A call that is not without side-effects might involve volatile
1596 accesses and thus conflicts with all other volatile accesses. */
1597 if (ref->volatile_p)
1598 return true;
1600 /* If the reference is based on a decl that is not aliased the call
1601 cannot possibly use it. */
1602 if (DECL_P (base)
1603 && !may_be_aliased (base)
1604 /* But local statics can be used through recursion. */
1605 && !is_global_var (base))
1606 goto process_args;
1608 callee = gimple_call_fndecl (call);
1610 /* Handle those builtin functions explicitly that do not act as
1611 escape points. See tree-ssa-structalias.c:find_func_aliases
1612 for the list of builtins we might need to handle here. */
1613 if (callee != NULL_TREE
1614 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1615 switch (DECL_FUNCTION_CODE (callee))
1617 /* All the following functions read memory pointed to by
1618 their second argument. strcat/strncat additionally
1619 reads memory pointed to by the first argument. */
1620 case BUILT_IN_STRCAT:
1621 case BUILT_IN_STRNCAT:
1623 ao_ref dref;
1624 ao_ref_init_from_ptr_and_size (&dref,
1625 gimple_call_arg (call, 0),
1626 NULL_TREE);
1627 if (refs_may_alias_p_1 (&dref, ref, false))
1628 return true;
1630 /* FALLTHRU */
1631 case BUILT_IN_STRCPY:
1632 case BUILT_IN_STRNCPY:
1633 case BUILT_IN_MEMCPY:
1634 case BUILT_IN_MEMMOVE:
1635 case BUILT_IN_MEMPCPY:
1636 case BUILT_IN_STPCPY:
1637 case BUILT_IN_STPNCPY:
1638 case BUILT_IN_TM_MEMCPY:
1639 case BUILT_IN_TM_MEMMOVE:
1641 ao_ref dref;
1642 tree size = NULL_TREE;
1643 if (gimple_call_num_args (call) == 3)
1644 size = gimple_call_arg (call, 2);
1645 ao_ref_init_from_ptr_and_size (&dref,
1646 gimple_call_arg (call, 1),
1647 size);
1648 return refs_may_alias_p_1 (&dref, ref, false);
1650 case BUILT_IN_STRCAT_CHK:
1651 case BUILT_IN_STRNCAT_CHK:
1653 ao_ref dref;
1654 ao_ref_init_from_ptr_and_size (&dref,
1655 gimple_call_arg (call, 0),
1656 NULL_TREE);
1657 if (refs_may_alias_p_1 (&dref, ref, false))
1658 return true;
1660 /* FALLTHRU */
1661 case BUILT_IN_STRCPY_CHK:
1662 case BUILT_IN_STRNCPY_CHK:
1663 case BUILT_IN_MEMCPY_CHK:
1664 case BUILT_IN_MEMMOVE_CHK:
1665 case BUILT_IN_MEMPCPY_CHK:
1666 case BUILT_IN_STPCPY_CHK:
1667 case BUILT_IN_STPNCPY_CHK:
1669 ao_ref dref;
1670 tree size = NULL_TREE;
1671 if (gimple_call_num_args (call) == 4)
1672 size = gimple_call_arg (call, 2);
1673 ao_ref_init_from_ptr_and_size (&dref,
1674 gimple_call_arg (call, 1),
1675 size);
1676 return refs_may_alias_p_1 (&dref, ref, false);
1678 case BUILT_IN_BCOPY:
1680 ao_ref dref;
1681 tree size = gimple_call_arg (call, 2);
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);
1688 /* The following functions read memory pointed to by their
1689 first argument. */
1690 CASE_BUILT_IN_TM_LOAD (1):
1691 CASE_BUILT_IN_TM_LOAD (2):
1692 CASE_BUILT_IN_TM_LOAD (4):
1693 CASE_BUILT_IN_TM_LOAD (8):
1694 CASE_BUILT_IN_TM_LOAD (FLOAT):
1695 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1696 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1697 CASE_BUILT_IN_TM_LOAD (M64):
1698 CASE_BUILT_IN_TM_LOAD (M128):
1699 CASE_BUILT_IN_TM_LOAD (M256):
1700 case BUILT_IN_TM_LOG:
1701 case BUILT_IN_TM_LOG_1:
1702 case BUILT_IN_TM_LOG_2:
1703 case BUILT_IN_TM_LOG_4:
1704 case BUILT_IN_TM_LOG_8:
1705 case BUILT_IN_TM_LOG_FLOAT:
1706 case BUILT_IN_TM_LOG_DOUBLE:
1707 case BUILT_IN_TM_LOG_LDOUBLE:
1708 case BUILT_IN_TM_LOG_M64:
1709 case BUILT_IN_TM_LOG_M128:
1710 case BUILT_IN_TM_LOG_M256:
1711 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1713 /* These read memory pointed to by the first argument. */
1714 case BUILT_IN_STRDUP:
1715 case BUILT_IN_STRNDUP:
1716 case BUILT_IN_REALLOC:
1718 ao_ref dref;
1719 tree size = NULL_TREE;
1720 if (gimple_call_num_args (call) == 2)
1721 size = gimple_call_arg (call, 1);
1722 ao_ref_init_from_ptr_and_size (&dref,
1723 gimple_call_arg (call, 0),
1724 size);
1725 return refs_may_alias_p_1 (&dref, ref, false);
1727 /* These read memory pointed to by the first argument. */
1728 case BUILT_IN_INDEX:
1729 case BUILT_IN_STRCHR:
1730 case BUILT_IN_STRRCHR:
1732 ao_ref dref;
1733 ao_ref_init_from_ptr_and_size (&dref,
1734 gimple_call_arg (call, 0),
1735 NULL_TREE);
1736 return refs_may_alias_p_1 (&dref, ref, false);
1738 /* These read memory pointed to by the first argument with size
1739 in the third argument. */
1740 case BUILT_IN_MEMCHR:
1742 ao_ref dref;
1743 ao_ref_init_from_ptr_and_size (&dref,
1744 gimple_call_arg (call, 0),
1745 gimple_call_arg (call, 2));
1746 return refs_may_alias_p_1 (&dref, ref, false);
1748 /* These read memory pointed to by the first and second arguments. */
1749 case BUILT_IN_STRSTR:
1750 case BUILT_IN_STRPBRK:
1752 ao_ref dref;
1753 ao_ref_init_from_ptr_and_size (&dref,
1754 gimple_call_arg (call, 0),
1755 NULL_TREE);
1756 if (refs_may_alias_p_1 (&dref, ref, false))
1757 return true;
1758 ao_ref_init_from_ptr_and_size (&dref,
1759 gimple_call_arg (call, 1),
1760 NULL_TREE);
1761 return refs_may_alias_p_1 (&dref, ref, false);
1764 /* The following builtins do not read from memory. */
1765 case BUILT_IN_FREE:
1766 case BUILT_IN_MALLOC:
1767 case BUILT_IN_POSIX_MEMALIGN:
1768 case BUILT_IN_ALIGNED_ALLOC:
1769 case BUILT_IN_CALLOC:
1770 case BUILT_IN_ALLOCA:
1771 case BUILT_IN_ALLOCA_WITH_ALIGN:
1772 case BUILT_IN_STACK_SAVE:
1773 case BUILT_IN_STACK_RESTORE:
1774 case BUILT_IN_MEMSET:
1775 case BUILT_IN_TM_MEMSET:
1776 case BUILT_IN_MEMSET_CHK:
1777 case BUILT_IN_FREXP:
1778 case BUILT_IN_FREXPF:
1779 case BUILT_IN_FREXPL:
1780 case BUILT_IN_GAMMA_R:
1781 case BUILT_IN_GAMMAF_R:
1782 case BUILT_IN_GAMMAL_R:
1783 case BUILT_IN_LGAMMA_R:
1784 case BUILT_IN_LGAMMAF_R:
1785 case BUILT_IN_LGAMMAL_R:
1786 case BUILT_IN_MODF:
1787 case BUILT_IN_MODFF:
1788 case BUILT_IN_MODFL:
1789 case BUILT_IN_REMQUO:
1790 case BUILT_IN_REMQUOF:
1791 case BUILT_IN_REMQUOL:
1792 case BUILT_IN_SINCOS:
1793 case BUILT_IN_SINCOSF:
1794 case BUILT_IN_SINCOSL:
1795 case BUILT_IN_ASSUME_ALIGNED:
1796 case BUILT_IN_VA_END:
1797 return false;
1798 /* __sync_* builtins and some OpenMP builtins act as threading
1799 barriers. */
1800 #undef DEF_SYNC_BUILTIN
1801 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1802 #include "sync-builtins.def"
1803 #undef DEF_SYNC_BUILTIN
1804 case BUILT_IN_GOMP_ATOMIC_START:
1805 case BUILT_IN_GOMP_ATOMIC_END:
1806 case BUILT_IN_GOMP_BARRIER:
1807 case BUILT_IN_GOMP_BARRIER_CANCEL:
1808 case BUILT_IN_GOMP_TASKWAIT:
1809 case BUILT_IN_GOMP_TASKGROUP_END:
1810 case BUILT_IN_GOMP_CRITICAL_START:
1811 case BUILT_IN_GOMP_CRITICAL_END:
1812 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1813 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1814 case BUILT_IN_GOMP_LOOP_END:
1815 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1816 case BUILT_IN_GOMP_ORDERED_START:
1817 case BUILT_IN_GOMP_ORDERED_END:
1818 case BUILT_IN_GOMP_SECTIONS_END:
1819 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1820 case BUILT_IN_GOMP_SINGLE_COPY_START:
1821 case BUILT_IN_GOMP_SINGLE_COPY_END:
1822 return true;
1824 default:
1825 /* Fallthru to general call handling. */;
1828 /* Check if base is a global static variable that is not read
1829 by the function. */
1830 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
1832 struct cgraph_node *node = cgraph_node::get (callee);
1833 bitmap not_read;
1835 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1836 node yet. We should enforce that there are nodes for all decls in the
1837 IL and remove this check instead. */
1838 if (node
1839 && (not_read = ipa_reference_get_not_read_global (node))
1840 && bitmap_bit_p (not_read, ipa_reference_var_uid (base)))
1841 goto process_args;
1844 /* Check if the base variable is call-used. */
1845 if (DECL_P (base))
1847 if (pt_solution_includes (gimple_call_use_set (call), base))
1848 return true;
1850 else if ((TREE_CODE (base) == MEM_REF
1851 || TREE_CODE (base) == TARGET_MEM_REF)
1852 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1854 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1855 if (!pi)
1856 return true;
1858 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1859 return true;
1861 else
1862 return true;
1864 /* Inspect call arguments for passed-by-value aliases. */
1865 process_args:
1866 for (i = 0; i < gimple_call_num_args (call); ++i)
1868 tree op = gimple_call_arg (call, i);
1869 int flags = gimple_call_arg_flags (call, i);
1871 if (flags & EAF_UNUSED)
1872 continue;
1874 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1875 op = TREE_OPERAND (op, 0);
1877 if (TREE_CODE (op) != SSA_NAME
1878 && !is_gimple_min_invariant (op))
1880 ao_ref r;
1881 ao_ref_init (&r, op);
1882 if (refs_may_alias_p_1 (&r, ref, true))
1883 return true;
1887 return false;
1890 static bool
1891 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref)
1893 bool res;
1894 res = ref_maybe_used_by_call_p_1 (call, ref);
1895 if (res)
1896 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1897 else
1898 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1899 return res;
1903 /* If the statement STMT may use the memory reference REF return
1904 true, otherwise return false. */
1906 bool
1907 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref)
1909 if (is_gimple_assign (stmt))
1911 tree rhs;
1913 /* All memory assign statements are single. */
1914 if (!gimple_assign_single_p (stmt))
1915 return false;
1917 rhs = gimple_assign_rhs1 (stmt);
1918 if (is_gimple_reg (rhs)
1919 || is_gimple_min_invariant (rhs)
1920 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1921 return false;
1923 return refs_may_alias_p (rhs, ref);
1925 else if (is_gimple_call (stmt))
1926 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref);
1927 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1929 tree retval = gimple_return_retval (return_stmt);
1930 if (retval
1931 && TREE_CODE (retval) != SSA_NAME
1932 && !is_gimple_min_invariant (retval)
1933 && refs_may_alias_p (retval, ref))
1934 return true;
1935 /* If ref escapes the function then the return acts as a use. */
1936 tree base = ao_ref_base (ref);
1937 if (!base)
1939 else if (DECL_P (base))
1940 return is_global_var (base);
1941 else if (TREE_CODE (base) == MEM_REF
1942 || TREE_CODE (base) == TARGET_MEM_REF)
1943 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1944 return false;
1947 return true;
1950 bool
1951 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref)
1953 ao_ref r;
1954 ao_ref_init (&r, ref);
1955 return ref_maybe_used_by_stmt_p (stmt, &r);
1958 /* If the call in statement CALL may clobber the memory reference REF
1959 return true, otherwise return false. */
1961 bool
1962 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
1964 tree base;
1965 tree callee;
1967 /* If the call is pure or const it cannot clobber anything. */
1968 if (gimple_call_flags (call)
1969 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1970 return false;
1971 if (gimple_call_internal_p (call))
1972 switch (gimple_call_internal_fn (call))
1974 /* Treat these internal calls like ECF_PURE for aliasing,
1975 they don't write to any memory the program should care about.
1976 They have important other side-effects, and read memory,
1977 so can't be ECF_NOVOPS. */
1978 case IFN_UBSAN_NULL:
1979 case IFN_UBSAN_BOUNDS:
1980 case IFN_UBSAN_VPTR:
1981 case IFN_UBSAN_OBJECT_SIZE:
1982 case IFN_ASAN_CHECK:
1983 return false;
1984 default:
1985 break;
1988 base = ao_ref_base (ref);
1989 if (!base)
1990 return true;
1992 if (TREE_CODE (base) == SSA_NAME
1993 || CONSTANT_CLASS_P (base))
1994 return false;
1996 /* A call that is not without side-effects might involve volatile
1997 accesses and thus conflicts with all other volatile accesses. */
1998 if (ref->volatile_p)
1999 return true;
2001 /* If the reference is based on a decl that is not aliased the call
2002 cannot possibly clobber it. */
2003 if (DECL_P (base)
2004 && !may_be_aliased (base)
2005 /* But local non-readonly statics can be modified through recursion
2006 or the call may implement a threading barrier which we must
2007 treat as may-def. */
2008 && (TREE_READONLY (base)
2009 || !is_global_var (base)))
2010 return false;
2012 callee = gimple_call_fndecl (call);
2014 /* Handle those builtin functions explicitly that do not act as
2015 escape points. See tree-ssa-structalias.c:find_func_aliases
2016 for the list of builtins we might need to handle here. */
2017 if (callee != NULL_TREE
2018 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2019 switch (DECL_FUNCTION_CODE (callee))
2021 /* All the following functions clobber memory pointed to by
2022 their first argument. */
2023 case BUILT_IN_STRCPY:
2024 case BUILT_IN_STRNCPY:
2025 case BUILT_IN_MEMCPY:
2026 case BUILT_IN_MEMMOVE:
2027 case BUILT_IN_MEMPCPY:
2028 case BUILT_IN_STPCPY:
2029 case BUILT_IN_STPNCPY:
2030 case BUILT_IN_STRCAT:
2031 case BUILT_IN_STRNCAT:
2032 case BUILT_IN_MEMSET:
2033 case BUILT_IN_TM_MEMSET:
2034 CASE_BUILT_IN_TM_STORE (1):
2035 CASE_BUILT_IN_TM_STORE (2):
2036 CASE_BUILT_IN_TM_STORE (4):
2037 CASE_BUILT_IN_TM_STORE (8):
2038 CASE_BUILT_IN_TM_STORE (FLOAT):
2039 CASE_BUILT_IN_TM_STORE (DOUBLE):
2040 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2041 CASE_BUILT_IN_TM_STORE (M64):
2042 CASE_BUILT_IN_TM_STORE (M128):
2043 CASE_BUILT_IN_TM_STORE (M256):
2044 case BUILT_IN_TM_MEMCPY:
2045 case BUILT_IN_TM_MEMMOVE:
2047 ao_ref dref;
2048 tree size = NULL_TREE;
2049 /* Don't pass in size for strncat, as the maximum size
2050 is strlen (dest) + n + 1 instead of n, resp.
2051 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2052 known. */
2053 if (gimple_call_num_args (call) == 3
2054 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2055 size = gimple_call_arg (call, 2);
2056 ao_ref_init_from_ptr_and_size (&dref,
2057 gimple_call_arg (call, 0),
2058 size);
2059 return refs_may_alias_p_1 (&dref, ref, false);
2061 case BUILT_IN_STRCPY_CHK:
2062 case BUILT_IN_STRNCPY_CHK:
2063 case BUILT_IN_MEMCPY_CHK:
2064 case BUILT_IN_MEMMOVE_CHK:
2065 case BUILT_IN_MEMPCPY_CHK:
2066 case BUILT_IN_STPCPY_CHK:
2067 case BUILT_IN_STPNCPY_CHK:
2068 case BUILT_IN_STRCAT_CHK:
2069 case BUILT_IN_STRNCAT_CHK:
2070 case BUILT_IN_MEMSET_CHK:
2072 ao_ref dref;
2073 tree size = NULL_TREE;
2074 /* Don't pass in size for __strncat_chk, as the maximum size
2075 is strlen (dest) + n + 1 instead of n, resp.
2076 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2077 known. */
2078 if (gimple_call_num_args (call) == 4
2079 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2080 size = gimple_call_arg (call, 2);
2081 ao_ref_init_from_ptr_and_size (&dref,
2082 gimple_call_arg (call, 0),
2083 size);
2084 return refs_may_alias_p_1 (&dref, ref, false);
2086 case BUILT_IN_BCOPY:
2088 ao_ref dref;
2089 tree size = gimple_call_arg (call, 2);
2090 ao_ref_init_from_ptr_and_size (&dref,
2091 gimple_call_arg (call, 1),
2092 size);
2093 return refs_may_alias_p_1 (&dref, ref, false);
2095 /* Allocating memory does not have any side-effects apart from
2096 being the definition point for the pointer. */
2097 case BUILT_IN_MALLOC:
2098 case BUILT_IN_ALIGNED_ALLOC:
2099 case BUILT_IN_CALLOC:
2100 case BUILT_IN_STRDUP:
2101 case BUILT_IN_STRNDUP:
2102 /* Unix98 specifies that errno is set on allocation failure. */
2103 if (flag_errno_math
2104 && targetm.ref_may_alias_errno (ref))
2105 return true;
2106 return false;
2107 case BUILT_IN_STACK_SAVE:
2108 case BUILT_IN_ALLOCA:
2109 case BUILT_IN_ALLOCA_WITH_ALIGN:
2110 case BUILT_IN_ASSUME_ALIGNED:
2111 return false;
2112 /* But posix_memalign stores a pointer into the memory pointed to
2113 by its first argument. */
2114 case BUILT_IN_POSIX_MEMALIGN:
2116 tree ptrptr = gimple_call_arg (call, 0);
2117 ao_ref dref;
2118 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2119 TYPE_SIZE_UNIT (ptr_type_node));
2120 return (refs_may_alias_p_1 (&dref, ref, false)
2121 || (flag_errno_math
2122 && targetm.ref_may_alias_errno (ref)));
2124 /* Freeing memory kills the pointed-to memory. More importantly
2125 the call has to serve as a barrier for moving loads and stores
2126 across it. */
2127 case BUILT_IN_FREE:
2128 case BUILT_IN_VA_END:
2130 tree ptr = gimple_call_arg (call, 0);
2131 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2133 /* Realloc serves both as allocation point and deallocation point. */
2134 case BUILT_IN_REALLOC:
2136 tree ptr = gimple_call_arg (call, 0);
2137 /* Unix98 specifies that errno is set on allocation failure. */
2138 return ((flag_errno_math
2139 && targetm.ref_may_alias_errno (ref))
2140 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2142 case BUILT_IN_GAMMA_R:
2143 case BUILT_IN_GAMMAF_R:
2144 case BUILT_IN_GAMMAL_R:
2145 case BUILT_IN_LGAMMA_R:
2146 case BUILT_IN_LGAMMAF_R:
2147 case BUILT_IN_LGAMMAL_R:
2149 tree out = gimple_call_arg (call, 1);
2150 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2151 return true;
2152 if (flag_errno_math)
2153 break;
2154 return false;
2156 case BUILT_IN_FREXP:
2157 case BUILT_IN_FREXPF:
2158 case BUILT_IN_FREXPL:
2159 case BUILT_IN_MODF:
2160 case BUILT_IN_MODFF:
2161 case BUILT_IN_MODFL:
2163 tree out = gimple_call_arg (call, 1);
2164 return ptr_deref_may_alias_ref_p_1 (out, ref);
2166 case BUILT_IN_REMQUO:
2167 case BUILT_IN_REMQUOF:
2168 case BUILT_IN_REMQUOL:
2170 tree out = gimple_call_arg (call, 2);
2171 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2172 return true;
2173 if (flag_errno_math)
2174 break;
2175 return false;
2177 case BUILT_IN_SINCOS:
2178 case BUILT_IN_SINCOSF:
2179 case BUILT_IN_SINCOSL:
2181 tree sin = gimple_call_arg (call, 1);
2182 tree cos = gimple_call_arg (call, 2);
2183 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2184 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2186 /* __sync_* builtins and some OpenMP builtins act as threading
2187 barriers. */
2188 #undef DEF_SYNC_BUILTIN
2189 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2190 #include "sync-builtins.def"
2191 #undef DEF_SYNC_BUILTIN
2192 case BUILT_IN_GOMP_ATOMIC_START:
2193 case BUILT_IN_GOMP_ATOMIC_END:
2194 case BUILT_IN_GOMP_BARRIER:
2195 case BUILT_IN_GOMP_BARRIER_CANCEL:
2196 case BUILT_IN_GOMP_TASKWAIT:
2197 case BUILT_IN_GOMP_TASKGROUP_END:
2198 case BUILT_IN_GOMP_CRITICAL_START:
2199 case BUILT_IN_GOMP_CRITICAL_END:
2200 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2201 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2202 case BUILT_IN_GOMP_LOOP_END:
2203 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2204 case BUILT_IN_GOMP_ORDERED_START:
2205 case BUILT_IN_GOMP_ORDERED_END:
2206 case BUILT_IN_GOMP_SECTIONS_END:
2207 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2208 case BUILT_IN_GOMP_SINGLE_COPY_START:
2209 case BUILT_IN_GOMP_SINGLE_COPY_END:
2210 return true;
2211 default:
2212 /* Fallthru to general call handling. */;
2215 /* Check if base is a global static variable that is not written
2216 by the function. */
2217 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2219 struct cgraph_node *node = cgraph_node::get (callee);
2220 bitmap not_written;
2222 if (node
2223 && (not_written = ipa_reference_get_not_written_global (node))
2224 && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
2225 return false;
2228 /* Check if the base variable is call-clobbered. */
2229 if (DECL_P (base))
2230 return pt_solution_includes (gimple_call_clobber_set (call), base);
2231 else if ((TREE_CODE (base) == MEM_REF
2232 || TREE_CODE (base) == TARGET_MEM_REF)
2233 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2235 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2236 if (!pi)
2237 return true;
2239 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2242 return true;
2245 /* If the call in statement CALL may clobber the memory reference REF
2246 return true, otherwise return false. */
2248 bool
2249 call_may_clobber_ref_p (gcall *call, tree ref)
2251 bool res;
2252 ao_ref r;
2253 ao_ref_init (&r, ref);
2254 res = call_may_clobber_ref_p_1 (call, &r);
2255 if (res)
2256 ++alias_stats.call_may_clobber_ref_p_may_alias;
2257 else
2258 ++alias_stats.call_may_clobber_ref_p_no_alias;
2259 return res;
2263 /* If the statement STMT may clobber the memory reference REF return true,
2264 otherwise return false. */
2266 bool
2267 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref)
2269 if (is_gimple_call (stmt))
2271 tree lhs = gimple_call_lhs (stmt);
2272 if (lhs
2273 && TREE_CODE (lhs) != SSA_NAME)
2275 ao_ref r;
2276 ao_ref_init (&r, lhs);
2277 if (refs_may_alias_p_1 (ref, &r, true))
2278 return true;
2281 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2283 else if (gimple_assign_single_p (stmt))
2285 tree lhs = gimple_assign_lhs (stmt);
2286 if (TREE_CODE (lhs) != SSA_NAME)
2288 ao_ref r;
2289 ao_ref_init (&r, lhs);
2290 return refs_may_alias_p_1 (ref, &r, true);
2293 else if (gimple_code (stmt) == GIMPLE_ASM)
2294 return true;
2296 return false;
2299 bool
2300 stmt_may_clobber_ref_p (gimple *stmt, tree ref)
2302 ao_ref r;
2303 ao_ref_init (&r, ref);
2304 return stmt_may_clobber_ref_p_1 (stmt, &r);
2307 /* If STMT kills the memory reference REF return true, otherwise
2308 return false. */
2310 bool
2311 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2313 if (!ao_ref_base (ref))
2314 return false;
2316 if (gimple_has_lhs (stmt)
2317 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2318 /* The assignment is not necessarily carried out if it can throw
2319 and we can catch it in the current function where we could inspect
2320 the previous value.
2321 ??? We only need to care about the RHS throwing. For aggregate
2322 assignments or similar calls and non-call exceptions the LHS
2323 might throw as well. */
2324 && !stmt_can_throw_internal (stmt))
2326 tree lhs = gimple_get_lhs (stmt);
2327 /* If LHS is literally a base of the access we are done. */
2328 if (ref->ref)
2330 tree base = ref->ref;
2331 if (handled_component_p (base))
2333 tree saved_lhs0 = NULL_TREE;
2334 if (handled_component_p (lhs))
2336 saved_lhs0 = TREE_OPERAND (lhs, 0);
2337 TREE_OPERAND (lhs, 0) = integer_zero_node;
2341 /* Just compare the outermost handled component, if
2342 they are equal we have found a possible common
2343 base. */
2344 tree saved_base0 = TREE_OPERAND (base, 0);
2345 TREE_OPERAND (base, 0) = integer_zero_node;
2346 bool res = operand_equal_p (lhs, base, 0);
2347 TREE_OPERAND (base, 0) = saved_base0;
2348 if (res)
2349 break;
2350 /* Otherwise drop handled components of the access. */
2351 base = saved_base0;
2353 while (handled_component_p (base));
2354 if (saved_lhs0)
2355 TREE_OPERAND (lhs, 0) = saved_lhs0;
2357 /* Finally check if the lhs has the same address and size as the
2358 base candidate of the access. */
2359 if (lhs == base
2360 || (((TYPE_SIZE (TREE_TYPE (lhs))
2361 == TYPE_SIZE (TREE_TYPE (base)))
2362 || (TYPE_SIZE (TREE_TYPE (lhs))
2363 && TYPE_SIZE (TREE_TYPE (base))
2364 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2365 TYPE_SIZE (TREE_TYPE (base)), 0)))
2366 && operand_equal_p (lhs, base, OEP_ADDRESS_OF)))
2367 return true;
2370 /* Now look for non-literal equal bases with the restriction of
2371 handling constant offset and size. */
2372 /* For a must-alias check we need to be able to constrain
2373 the access properly. */
2374 if (ref->max_size == -1)
2375 return false;
2376 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2377 bool reverse;
2378 tree base
2379 = get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
2380 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2381 so base == ref->base does not always hold. */
2382 if (base != ref->base)
2384 /* If both base and ref->base are MEM_REFs, only compare the
2385 first operand, and if the second operand isn't equal constant,
2386 try to add the offsets into offset and ref_offset. */
2387 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2388 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2390 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2391 TREE_OPERAND (ref->base, 1)))
2393 offset_int off1 = mem_ref_offset (base);
2394 off1 <<= LOG2_BITS_PER_UNIT;
2395 off1 += offset;
2396 offset_int off2 = mem_ref_offset (ref->base);
2397 off2 <<= LOG2_BITS_PER_UNIT;
2398 off2 += ref_offset;
2399 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2401 offset = off1.to_shwi ();
2402 ref_offset = off2.to_shwi ();
2404 else
2405 size = -1;
2408 else
2409 size = -1;
2411 /* For a must-alias check we need to be able to constrain
2412 the access properly. */
2413 if (size != -1 && size == max_size)
2415 if (offset <= ref_offset
2416 && offset + size >= ref_offset + ref->max_size)
2417 return true;
2421 if (is_gimple_call (stmt))
2423 tree callee = gimple_call_fndecl (stmt);
2424 if (callee != NULL_TREE
2425 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2426 switch (DECL_FUNCTION_CODE (callee))
2428 case BUILT_IN_FREE:
2430 tree ptr = gimple_call_arg (stmt, 0);
2431 tree base = ao_ref_base (ref);
2432 if (base && TREE_CODE (base) == MEM_REF
2433 && TREE_OPERAND (base, 0) == ptr)
2434 return true;
2435 break;
2438 case BUILT_IN_MEMCPY:
2439 case BUILT_IN_MEMPCPY:
2440 case BUILT_IN_MEMMOVE:
2441 case BUILT_IN_MEMSET:
2442 case BUILT_IN_MEMCPY_CHK:
2443 case BUILT_IN_MEMPCPY_CHK:
2444 case BUILT_IN_MEMMOVE_CHK:
2445 case BUILT_IN_MEMSET_CHK:
2447 /* For a must-alias check we need to be able to constrain
2448 the access properly. */
2449 if (ref->max_size == -1)
2450 return false;
2451 tree dest = gimple_call_arg (stmt, 0);
2452 tree len = gimple_call_arg (stmt, 2);
2453 if (!tree_fits_shwi_p (len))
2454 return false;
2455 tree rbase = ref->base;
2456 offset_int roffset = ref->offset;
2457 ao_ref dref;
2458 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2459 tree base = ao_ref_base (&dref);
2460 offset_int offset = dref.offset;
2461 if (!base || dref.size == -1)
2462 return false;
2463 if (TREE_CODE (base) == MEM_REF)
2465 if (TREE_CODE (rbase) != MEM_REF)
2466 return false;
2467 // Compare pointers.
2468 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
2469 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
2470 base = TREE_OPERAND (base, 0);
2471 rbase = TREE_OPERAND (rbase, 0);
2473 if (base == rbase
2474 && offset <= roffset
2475 && (roffset + ref->max_size
2476 <= offset + (wi::to_offset (len) << LOG2_BITS_PER_UNIT)))
2477 return true;
2478 break;
2481 case BUILT_IN_VA_END:
2483 tree ptr = gimple_call_arg (stmt, 0);
2484 if (TREE_CODE (ptr) == ADDR_EXPR)
2486 tree base = ao_ref_base (ref);
2487 if (TREE_OPERAND (ptr, 0) == base)
2488 return true;
2490 break;
2493 default:;
2496 return false;
2499 bool
2500 stmt_kills_ref_p (gimple *stmt, tree ref)
2502 ao_ref r;
2503 ao_ref_init (&r, ref);
2504 return stmt_kills_ref_p (stmt, &r);
2508 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2509 TARGET or a statement clobbering the memory reference REF in which
2510 case false is returned. The walk starts with VUSE, one argument of PHI. */
2512 static bool
2513 maybe_skip_until (gimple *phi, tree target, ao_ref *ref,
2514 tree vuse, unsigned int *cnt, bitmap *visited,
2515 bool abort_on_visited,
2516 void *(*translate)(ao_ref *, tree, void *, bool *),
2517 void *data)
2519 basic_block bb = gimple_bb (phi);
2521 if (!*visited)
2522 *visited = BITMAP_ALLOC (NULL);
2524 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2526 /* Walk until we hit the target. */
2527 while (vuse != target)
2529 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2530 /* Recurse for PHI nodes. */
2531 if (gimple_code (def_stmt) == GIMPLE_PHI)
2533 /* An already visited PHI node ends the walk successfully. */
2534 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2535 return !abort_on_visited;
2536 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2537 visited, abort_on_visited,
2538 translate, data);
2539 if (!vuse)
2540 return false;
2541 continue;
2543 else if (gimple_nop_p (def_stmt))
2544 return false;
2545 else
2547 /* A clobbering statement or the end of the IL ends it failing. */
2548 ++*cnt;
2549 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2551 bool disambiguate_only = true;
2552 if (translate
2553 && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
2555 else
2556 return false;
2559 /* If we reach a new basic-block see if we already skipped it
2560 in a previous walk that ended successfully. */
2561 if (gimple_bb (def_stmt) != bb)
2563 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2564 return !abort_on_visited;
2565 bb = gimple_bb (def_stmt);
2567 vuse = gimple_vuse (def_stmt);
2569 return true;
2572 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2573 until we hit the phi argument definition that dominates the other one.
2574 Return that, or NULL_TREE if there is no such definition. */
2576 static tree
2577 get_continuation_for_phi_1 (gimple *phi, tree arg0, tree arg1,
2578 ao_ref *ref, unsigned int *cnt,
2579 bitmap *visited, bool abort_on_visited,
2580 void *(*translate)(ao_ref *, tree, void *, bool *),
2581 void *data)
2583 gimple *def0 = SSA_NAME_DEF_STMT (arg0);
2584 gimple *def1 = SSA_NAME_DEF_STMT (arg1);
2585 tree common_vuse;
2587 if (arg0 == arg1)
2588 return arg0;
2589 else if (gimple_nop_p (def0)
2590 || (!gimple_nop_p (def1)
2591 && dominated_by_p (CDI_DOMINATORS,
2592 gimple_bb (def1), gimple_bb (def0))))
2594 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2595 visited, abort_on_visited, translate, data))
2596 return arg0;
2598 else if (gimple_nop_p (def1)
2599 || dominated_by_p (CDI_DOMINATORS,
2600 gimple_bb (def0), gimple_bb (def1)))
2602 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2603 visited, abort_on_visited, translate, data))
2604 return arg1;
2606 /* Special case of a diamond:
2607 MEM_1 = ...
2608 goto (cond) ? L1 : L2
2609 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2610 goto L3
2611 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2612 L3: MEM_4 = PHI<MEM_2, MEM_3>
2613 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2614 dominate each other, but still we can easily skip this PHI node
2615 if we recognize that the vuse MEM operand is the same for both,
2616 and that we can skip both statements (they don't clobber us).
2617 This is still linear. Don't use maybe_skip_until, that might
2618 potentially be slow. */
2619 else if ((common_vuse = gimple_vuse (def0))
2620 && common_vuse == gimple_vuse (def1))
2622 bool disambiguate_only = true;
2623 *cnt += 2;
2624 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2625 || (translate
2626 && (*translate) (ref, arg0, data, &disambiguate_only) == NULL))
2627 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2628 || (translate
2629 && (*translate) (ref, arg1, data, &disambiguate_only) == NULL)))
2630 return common_vuse;
2633 return NULL_TREE;
2637 /* Starting from a PHI node for the virtual operand of the memory reference
2638 REF find a continuation virtual operand that allows to continue walking
2639 statements dominating PHI skipping only statements that cannot possibly
2640 clobber REF. Increments *CNT for each alias disambiguation done.
2641 Returns NULL_TREE if no suitable virtual operand can be found. */
2643 tree
2644 get_continuation_for_phi (gimple *phi, ao_ref *ref,
2645 unsigned int *cnt, bitmap *visited,
2646 bool abort_on_visited,
2647 void *(*translate)(ao_ref *, tree, void *, bool *),
2648 void *data)
2650 unsigned nargs = gimple_phi_num_args (phi);
2652 /* Through a single-argument PHI we can simply look through. */
2653 if (nargs == 1)
2654 return PHI_ARG_DEF (phi, 0);
2656 /* For two or more arguments try to pairwise skip non-aliasing code
2657 until we hit the phi argument definition that dominates the other one. */
2658 else if (nargs >= 2)
2660 tree arg0, arg1;
2661 unsigned i;
2663 /* Find a candidate for the virtual operand which definition
2664 dominates those of all others. */
2665 arg0 = PHI_ARG_DEF (phi, 0);
2666 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2667 for (i = 1; i < nargs; ++i)
2669 arg1 = PHI_ARG_DEF (phi, i);
2670 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2672 arg0 = arg1;
2673 break;
2675 if (dominated_by_p (CDI_DOMINATORS,
2676 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2677 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2678 arg0 = arg1;
2681 /* Then pairwise reduce against the found candidate. */
2682 for (i = 0; i < nargs; ++i)
2684 arg1 = PHI_ARG_DEF (phi, i);
2685 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2686 cnt, visited, abort_on_visited,
2687 translate, data);
2688 if (!arg0)
2689 return NULL_TREE;
2692 return arg0;
2695 return NULL_TREE;
2698 /* Based on the memory reference REF and its virtual use VUSE call
2699 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2700 itself. That is, for each virtual use for which its defining statement
2701 does not clobber REF.
2703 WALKER is called with REF, the current virtual use and DATA. If
2704 WALKER returns non-NULL the walk stops and its result is returned.
2705 At the end of a non-successful walk NULL is returned.
2707 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2708 use which definition is a statement that may clobber REF and DATA.
2709 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2710 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2711 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2712 to adjust REF and *DATA to make that valid.
2714 VALUEIZE if non-NULL is called with the next VUSE that is considered
2715 and return value is substituted for that. This can be used to
2716 implement optimistic value-numbering for example. Note that the
2717 VUSE argument is assumed to be valueized already.
2719 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2721 void *
2722 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2723 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2724 void *(*translate)(ao_ref *, tree, void *, bool *),
2725 tree (*valueize)(tree),
2726 void *data)
2728 bitmap visited = NULL;
2729 void *res;
2730 unsigned int cnt = 0;
2731 bool translated = false;
2733 timevar_push (TV_ALIAS_STMT_WALK);
2737 gimple *def_stmt;
2739 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2740 res = (*walker) (ref, vuse, cnt, data);
2741 /* Abort walk. */
2742 if (res == (void *)-1)
2744 res = NULL;
2745 break;
2747 /* Lookup succeeded. */
2748 else if (res != NULL)
2749 break;
2751 if (valueize)
2752 vuse = valueize (vuse);
2753 def_stmt = SSA_NAME_DEF_STMT (vuse);
2754 if (gimple_nop_p (def_stmt))
2755 break;
2756 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2757 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2758 &visited, translated, translate, data);
2759 else
2761 cnt++;
2762 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2764 if (!translate)
2765 break;
2766 bool disambiguate_only = false;
2767 res = (*translate) (ref, vuse, data, &disambiguate_only);
2768 /* Failed lookup and translation. */
2769 if (res == (void *)-1)
2771 res = NULL;
2772 break;
2774 /* Lookup succeeded. */
2775 else if (res != NULL)
2776 break;
2777 /* Translation succeeded, continue walking. */
2778 translated = translated || !disambiguate_only;
2780 vuse = gimple_vuse (def_stmt);
2783 while (vuse);
2785 if (visited)
2786 BITMAP_FREE (visited);
2788 timevar_pop (TV_ALIAS_STMT_WALK);
2790 return res;
2794 /* Based on the memory reference REF call WALKER for each vdef which
2795 defining statement may clobber REF, starting with VDEF. If REF
2796 is NULL_TREE, each defining statement is visited.
2798 WALKER is called with REF, the current vdef and DATA. If WALKER
2799 returns true the walk is stopped, otherwise it continues.
2801 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2802 The pointer may be NULL and then we do not track this information.
2804 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2805 PHI argument (but only one walk continues on merge points), the
2806 return value is true if any of the walks was successful.
2808 The function returns the number of statements walked. */
2810 static unsigned int
2811 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2812 bool (*walker)(ao_ref *, tree, void *), void *data,
2813 bitmap *visited, unsigned int cnt,
2814 bool *function_entry_reached)
2818 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
2820 if (*visited
2821 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2822 return cnt;
2824 if (gimple_nop_p (def_stmt))
2826 if (function_entry_reached)
2827 *function_entry_reached = true;
2828 return cnt;
2830 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2832 unsigned i;
2833 if (!*visited)
2834 *visited = BITMAP_ALLOC (NULL);
2835 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2836 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2837 walker, data, visited, 0,
2838 function_entry_reached);
2839 return cnt;
2842 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2843 cnt++;
2844 if ((!ref
2845 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2846 && (*walker) (ref, vdef, data))
2847 return cnt;
2849 vdef = gimple_vuse (def_stmt);
2851 while (1);
2854 unsigned int
2855 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2856 bool (*walker)(ao_ref *, tree, void *), void *data,
2857 bitmap *visited,
2858 bool *function_entry_reached)
2860 bitmap local_visited = NULL;
2861 unsigned int ret;
2863 timevar_push (TV_ALIAS_STMT_WALK);
2865 if (function_entry_reached)
2866 *function_entry_reached = false;
2868 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2869 visited ? visited : &local_visited, 0,
2870 function_entry_reached);
2871 if (local_visited)
2872 BITMAP_FREE (local_visited);
2874 timevar_pop (TV_ALIAS_STMT_WALK);
2876 return ret;