2016-08-24 Michael Collison <michael.collison@linaro.org>
[official-gcc.git] / gcc / tree-ssa-alias.c
blob8051a669b37ab79b240d62f364711fa11e20b275
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 || (TREE_CODE (decl) != VAR_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 (TREE_CODE (tem) == VAR_DECL
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 (TREE_CODE (tem) == VAR_DECL
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 if (obj1 && obj2)
362 /* Other code handles this correctly, no need to duplicate it here. */;
363 else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
365 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
366 /* We may not use restrict to optimize pointer comparisons.
367 See PR71062. So we have to assume that restrict-pointed-to
368 may be in fact obj1. */
369 if (!pi || pi->pt.vars_contains_restrict)
370 return false;
371 return !pt_solution_includes (&pi->pt, obj1);
373 else if (TREE_CODE (ptr1) == SSA_NAME && obj2)
375 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr1);
376 if (!pi || pi->pt.vars_contains_restrict)
377 return false;
378 return !pt_solution_includes (&pi->pt, obj2);
381 /* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
382 but those require pt.null to be conservatively correct. */
384 return false;
387 /* Returns whether reference REF to BASE may refer to global memory. */
389 static bool
390 ref_may_alias_global_p_1 (tree base)
392 if (DECL_P (base))
393 return is_global_var (base);
394 else if (TREE_CODE (base) == MEM_REF
395 || TREE_CODE (base) == TARGET_MEM_REF)
396 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
397 return true;
400 bool
401 ref_may_alias_global_p (ao_ref *ref)
403 tree base = ao_ref_base (ref);
404 return ref_may_alias_global_p_1 (base);
407 bool
408 ref_may_alias_global_p (tree ref)
410 tree base = get_base_address (ref);
411 return ref_may_alias_global_p_1 (base);
414 /* Return true whether STMT may clobber global memory. */
416 bool
417 stmt_may_clobber_global_p (gimple *stmt)
419 tree lhs;
421 if (!gimple_vdef (stmt))
422 return false;
424 /* ??? We can ask the oracle whether an artificial pointer
425 dereference with a pointer with points-to information covering
426 all global memory (what about non-address taken memory?) maybe
427 clobbered by this call. As there is at the moment no convenient
428 way of doing that without generating garbage do some manual
429 checking instead.
430 ??? We could make a NULL ao_ref argument to the various
431 predicates special, meaning any global memory. */
433 switch (gimple_code (stmt))
435 case GIMPLE_ASSIGN:
436 lhs = gimple_assign_lhs (stmt);
437 return (TREE_CODE (lhs) != SSA_NAME
438 && ref_may_alias_global_p (lhs));
439 case GIMPLE_CALL:
440 return true;
441 default:
442 return true;
447 /* Dump alias information on FILE. */
449 void
450 dump_alias_info (FILE *file)
452 unsigned i;
453 const char *funcname
454 = lang_hooks.decl_printable_name (current_function_decl, 2);
455 tree var;
457 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
459 fprintf (file, "Aliased symbols\n\n");
461 FOR_EACH_LOCAL_DECL (cfun, i, var)
463 if (may_be_aliased (var))
464 dump_variable (file, var);
467 fprintf (file, "\nCall clobber information\n");
469 fprintf (file, "\nESCAPED");
470 dump_points_to_solution (file, &cfun->gimple_df->escaped);
472 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
474 for (i = 1; i < num_ssa_names; i++)
476 tree ptr = ssa_name (i);
477 struct ptr_info_def *pi;
479 if (ptr == NULL_TREE
480 || !POINTER_TYPE_P (TREE_TYPE (ptr))
481 || SSA_NAME_IN_FREE_LIST (ptr))
482 continue;
484 pi = SSA_NAME_PTR_INFO (ptr);
485 if (pi)
486 dump_points_to_info_for (file, ptr);
489 fprintf (file, "\n");
493 /* Dump alias information on stderr. */
495 DEBUG_FUNCTION void
496 debug_alias_info (void)
498 dump_alias_info (stderr);
502 /* Dump the points-to set *PT into FILE. */
504 void
505 dump_points_to_solution (FILE *file, struct pt_solution *pt)
507 if (pt->anything)
508 fprintf (file, ", points-to anything");
510 if (pt->nonlocal)
511 fprintf (file, ", points-to non-local");
513 if (pt->escaped)
514 fprintf (file, ", points-to escaped");
516 if (pt->ipa_escaped)
517 fprintf (file, ", points-to unit escaped");
519 if (pt->null)
520 fprintf (file, ", points-to NULL");
522 if (pt->vars)
524 fprintf (file, ", points-to vars: ");
525 dump_decl_set (file, pt->vars);
526 if (pt->vars_contains_nonlocal
527 || pt->vars_contains_escaped
528 || pt->vars_contains_escaped_heap
529 || pt->vars_contains_restrict)
531 const char *comma = "";
532 fprintf (file, " (");
533 if (pt->vars_contains_nonlocal)
535 fprintf (file, "nonlocal");
536 comma = ", ";
538 if (pt->vars_contains_escaped)
540 fprintf (file, "%sescaped", comma);
541 comma = ", ";
543 if (pt->vars_contains_escaped_heap)
545 fprintf (file, "%sescaped heap", comma);
546 comma = ", ";
548 if (pt->vars_contains_restrict)
549 fprintf (file, "%srestrict", comma);
550 fprintf (file, ")");
556 /* Unified dump function for pt_solution. */
558 DEBUG_FUNCTION void
559 debug (pt_solution &ref)
561 dump_points_to_solution (stderr, &ref);
564 DEBUG_FUNCTION void
565 debug (pt_solution *ptr)
567 if (ptr)
568 debug (*ptr);
569 else
570 fprintf (stderr, "<nil>\n");
574 /* Dump points-to information for SSA_NAME PTR into FILE. */
576 void
577 dump_points_to_info_for (FILE *file, tree ptr)
579 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
581 print_generic_expr (file, ptr, dump_flags);
583 if (pi)
584 dump_points_to_solution (file, &pi->pt);
585 else
586 fprintf (file, ", points-to anything");
588 fprintf (file, "\n");
592 /* Dump points-to information for VAR into stderr. */
594 DEBUG_FUNCTION void
595 debug_points_to_info_for (tree var)
597 dump_points_to_info_for (stderr, var);
601 /* Initializes the alias-oracle reference representation *R from REF. */
603 void
604 ao_ref_init (ao_ref *r, tree ref)
606 r->ref = ref;
607 r->base = NULL_TREE;
608 r->offset = 0;
609 r->size = -1;
610 r->max_size = -1;
611 r->ref_alias_set = -1;
612 r->base_alias_set = -1;
613 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
616 /* Returns the base object of the memory reference *REF. */
618 tree
619 ao_ref_base (ao_ref *ref)
621 bool reverse;
623 if (ref->base)
624 return ref->base;
625 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
626 &ref->max_size, &reverse);
627 return ref->base;
630 /* Returns the base object alias set of the memory reference *REF. */
632 alias_set_type
633 ao_ref_base_alias_set (ao_ref *ref)
635 tree base_ref;
636 if (ref->base_alias_set != -1)
637 return ref->base_alias_set;
638 if (!ref->ref)
639 return 0;
640 base_ref = ref->ref;
641 while (handled_component_p (base_ref))
642 base_ref = TREE_OPERAND (base_ref, 0);
643 ref->base_alias_set = get_alias_set (base_ref);
644 return ref->base_alias_set;
647 /* Returns the reference alias set of the memory reference *REF. */
649 alias_set_type
650 ao_ref_alias_set (ao_ref *ref)
652 if (ref->ref_alias_set != -1)
653 return ref->ref_alias_set;
654 ref->ref_alias_set = get_alias_set (ref->ref);
655 return ref->ref_alias_set;
658 /* Init an alias-oracle reference representation from a gimple pointer
659 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
660 size is assumed to be unknown. The access is assumed to be only
661 to or after of the pointer target, not before it. */
663 void
664 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
666 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
667 ref->ref = NULL_TREE;
668 if (TREE_CODE (ptr) == SSA_NAME)
670 gimple *stmt = SSA_NAME_DEF_STMT (ptr);
671 if (gimple_assign_single_p (stmt)
672 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
673 ptr = gimple_assign_rhs1 (stmt);
674 else if (is_gimple_assign (stmt)
675 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
676 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
678 ptr = gimple_assign_rhs1 (stmt);
679 extra_offset = BITS_PER_UNIT
680 * int_cst_value (gimple_assign_rhs2 (stmt));
684 if (TREE_CODE (ptr) == ADDR_EXPR)
686 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
687 if (ref->base)
688 ref->offset = BITS_PER_UNIT * t;
689 else
691 size = NULL_TREE;
692 ref->offset = 0;
693 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
696 else
698 ref->base = build2 (MEM_REF, char_type_node,
699 ptr, null_pointer_node);
700 ref->offset = 0;
702 ref->offset += extra_offset;
703 if (size
704 && tree_fits_shwi_p (size)
705 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
706 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
707 else
708 ref->max_size = ref->size = -1;
709 ref->ref_alias_set = 0;
710 ref->base_alias_set = 0;
711 ref->volatile_p = false;
714 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
715 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
716 decide. */
718 static inline int
719 same_type_for_tbaa (tree type1, tree type2)
721 type1 = TYPE_MAIN_VARIANT (type1);
722 type2 = TYPE_MAIN_VARIANT (type2);
724 /* If we would have to do structural comparison bail out. */
725 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
726 || TYPE_STRUCTURAL_EQUALITY_P (type2))
727 return -1;
729 /* Compare the canonical types. */
730 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
731 return 1;
733 /* ??? Array types are not properly unified in all cases as we have
734 spurious changes in the index types for example. Removing this
735 causes all sorts of problems with the Fortran frontend. */
736 if (TREE_CODE (type1) == ARRAY_TYPE
737 && TREE_CODE (type2) == ARRAY_TYPE)
738 return -1;
740 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
741 object of one of its constrained subtypes, e.g. when a function with an
742 unconstrained parameter passed by reference is called on an object and
743 inlined. But, even in the case of a fixed size, type and subtypes are
744 not equivalent enough as to share the same TYPE_CANONICAL, since this
745 would mean that conversions between them are useless, whereas they are
746 not (e.g. type and subtypes can have different modes). So, in the end,
747 they are only guaranteed to have the same alias set. */
748 if (get_alias_set (type1) == get_alias_set (type2))
749 return -1;
751 /* The types are known to be not equal. */
752 return 0;
755 /* Determine if the two component references REF1 and REF2 which are
756 based on access types TYPE1 and TYPE2 and of which at least one is based
757 on an indirect reference may alias. REF2 is the only one that can
758 be a decl in which case REF2_IS_DECL is true.
759 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
760 are the respective alias sets. */
762 static bool
763 aliasing_component_refs_p (tree ref1,
764 alias_set_type ref1_alias_set,
765 alias_set_type base1_alias_set,
766 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
767 tree ref2,
768 alias_set_type ref2_alias_set,
769 alias_set_type base2_alias_set,
770 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
771 bool ref2_is_decl)
773 /* If one reference is a component references through pointers try to find a
774 common base and apply offset based disambiguation. This handles
775 for example
776 struct A { int i; int j; } *q;
777 struct B { struct A a; int k; } *p;
778 disambiguating q->i and p->a.j. */
779 tree base1, base2;
780 tree type1, type2;
781 tree *refp;
782 int same_p;
784 /* Choose bases and base types to search for. */
785 base1 = ref1;
786 while (handled_component_p (base1))
787 base1 = TREE_OPERAND (base1, 0);
788 type1 = TREE_TYPE (base1);
789 base2 = ref2;
790 while (handled_component_p (base2))
791 base2 = TREE_OPERAND (base2, 0);
792 type2 = TREE_TYPE (base2);
794 /* Now search for the type1 in the access path of ref2. This
795 would be a common base for doing offset based disambiguation on. */
796 refp = &ref2;
797 while (handled_component_p (*refp)
798 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
799 refp = &TREE_OPERAND (*refp, 0);
800 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
801 /* If we couldn't compare types we have to bail out. */
802 if (same_p == -1)
803 return true;
804 else if (same_p == 1)
806 HOST_WIDE_INT offadj, sztmp, msztmp;
807 bool reverse;
808 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp, &reverse);
809 offset2 -= offadj;
810 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp, &reverse);
811 offset1 -= offadj;
812 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
814 /* If we didn't find a common base, try the other way around. */
815 refp = &ref1;
816 while (handled_component_p (*refp)
817 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
818 refp = &TREE_OPERAND (*refp, 0);
819 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
820 /* If we couldn't compare types we have to bail out. */
821 if (same_p == -1)
822 return true;
823 else if (same_p == 1)
825 HOST_WIDE_INT offadj, sztmp, msztmp;
826 bool reverse;
827 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp, &reverse);
828 offset1 -= offadj;
829 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp, &reverse);
830 offset2 -= offadj;
831 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
834 /* If we have two type access paths B1.path1 and B2.path2 they may
835 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
836 But we can still have a path that goes B1.path1...B2.path2 with
837 a part that we do not see. So we can only disambiguate now
838 if there is no B2 in the tail of path1 and no B1 on the
839 tail of path2. */
840 if (base1_alias_set == ref2_alias_set
841 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
842 return true;
843 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
844 if (!ref2_is_decl)
845 return (base2_alias_set == ref1_alias_set
846 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
847 return false;
850 /* Return true if we can determine that component references REF1 and REF2,
851 that are within a common DECL, cannot overlap. */
853 static bool
854 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
856 auto_vec<tree, 16> component_refs1;
857 auto_vec<tree, 16> component_refs2;
859 /* Create the stack of handled components for REF1. */
860 while (handled_component_p (ref1))
862 component_refs1.safe_push (ref1);
863 ref1 = TREE_OPERAND (ref1, 0);
865 if (TREE_CODE (ref1) == MEM_REF)
867 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
868 return false;
869 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
872 /* Create the stack of handled components for REF2. */
873 while (handled_component_p (ref2))
875 component_refs2.safe_push (ref2);
876 ref2 = TREE_OPERAND (ref2, 0);
878 if (TREE_CODE (ref2) == MEM_REF)
880 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
881 return false;
882 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
885 /* Bases must be either same or uncomparable. */
886 gcc_checking_assert (ref1 == ref2
887 || (DECL_P (ref1) && DECL_P (ref2)
888 && compare_base_decls (ref1, ref2) != 0));
890 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
891 rank. This is sufficient because we start from the same DECL and you
892 cannot reference several fields at a time with COMPONENT_REFs (unlike
893 with ARRAY_RANGE_REFs for arrays) so you always need the same number
894 of them to access a sub-component, unless you're in a union, in which
895 case the return value will precisely be false. */
896 while (true)
900 if (component_refs1.is_empty ())
901 return false;
902 ref1 = component_refs1.pop ();
904 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
908 if (component_refs2.is_empty ())
909 return false;
910 ref2 = component_refs2.pop ();
912 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
914 /* Beware of BIT_FIELD_REF. */
915 if (TREE_CODE (ref1) != COMPONENT_REF
916 || TREE_CODE (ref2) != COMPONENT_REF)
917 return false;
919 tree field1 = TREE_OPERAND (ref1, 1);
920 tree field2 = TREE_OPERAND (ref2, 1);
922 /* ??? We cannot simply use the type of operand #0 of the refs here
923 as the Fortran compiler smuggles type punning into COMPONENT_REFs
924 for common blocks instead of using unions like everyone else. */
925 tree type1 = DECL_CONTEXT (field1);
926 tree type2 = DECL_CONTEXT (field2);
928 /* We cannot disambiguate fields in a union or qualified union. */
929 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
930 return false;
932 if (field1 != field2)
934 /* A field and its representative need to be considered the
935 same. */
936 if (DECL_BIT_FIELD_REPRESENTATIVE (field1) == field2
937 || DECL_BIT_FIELD_REPRESENTATIVE (field2) == field1)
938 return false;
939 /* Different fields of the same record type cannot overlap.
940 ??? Bitfields can overlap at RTL level so punt on them. */
941 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
942 return false;
943 return true;
947 return false;
950 /* qsort compare function to sort FIELD_DECLs after their
951 DECL_FIELD_CONTEXT TYPE_UID. */
953 static inline int
954 ncr_compar (const void *field1_, const void *field2_)
956 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
957 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
958 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
959 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
960 if (uid1 < uid2)
961 return -1;
962 else if (uid1 > uid2)
963 return 1;
964 return 0;
967 /* Return true if we can determine that the fields referenced cannot
968 overlap for any pair of objects. */
970 static bool
971 nonoverlapping_component_refs_p (const_tree x, const_tree y)
973 if (!flag_strict_aliasing
974 || !x || !y
975 || TREE_CODE (x) != COMPONENT_REF
976 || TREE_CODE (y) != COMPONENT_REF)
977 return false;
979 auto_vec<const_tree, 16> fieldsx;
980 while (TREE_CODE (x) == COMPONENT_REF)
982 tree field = TREE_OPERAND (x, 1);
983 tree type = DECL_FIELD_CONTEXT (field);
984 if (TREE_CODE (type) == RECORD_TYPE)
985 fieldsx.safe_push (field);
986 x = TREE_OPERAND (x, 0);
988 if (fieldsx.length () == 0)
989 return false;
990 auto_vec<const_tree, 16> fieldsy;
991 while (TREE_CODE (y) == COMPONENT_REF)
993 tree field = TREE_OPERAND (y, 1);
994 tree type = DECL_FIELD_CONTEXT (field);
995 if (TREE_CODE (type) == RECORD_TYPE)
996 fieldsy.safe_push (TREE_OPERAND (y, 1));
997 y = TREE_OPERAND (y, 0);
999 if (fieldsy.length () == 0)
1000 return false;
1002 /* Most common case first. */
1003 if (fieldsx.length () == 1
1004 && fieldsy.length () == 1)
1005 return ((DECL_FIELD_CONTEXT (fieldsx[0])
1006 == DECL_FIELD_CONTEXT (fieldsy[0]))
1007 && fieldsx[0] != fieldsy[0]
1008 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
1010 if (fieldsx.length () == 2)
1012 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1013 std::swap (fieldsx[0], fieldsx[1]);
1015 else
1016 fieldsx.qsort (ncr_compar);
1018 if (fieldsy.length () == 2)
1020 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1021 std::swap (fieldsy[0], fieldsy[1]);
1023 else
1024 fieldsy.qsort (ncr_compar);
1026 unsigned i = 0, j = 0;
1029 const_tree fieldx = fieldsx[i];
1030 const_tree fieldy = fieldsy[j];
1031 tree typex = DECL_FIELD_CONTEXT (fieldx);
1032 tree typey = DECL_FIELD_CONTEXT (fieldy);
1033 if (typex == typey)
1035 /* We're left with accessing different fields of a structure,
1036 no possible overlap. */
1037 if (fieldx != fieldy)
1039 /* A field and its representative need to be considered the
1040 same. */
1041 if (DECL_BIT_FIELD_REPRESENTATIVE (fieldx) == fieldy
1042 || DECL_BIT_FIELD_REPRESENTATIVE (fieldy) == fieldx)
1043 return false;
1044 /* Different fields of the same record type cannot overlap.
1045 ??? Bitfields can overlap at RTL level so punt on them. */
1046 if (DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy))
1047 return false;
1048 return true;
1051 if (TYPE_UID (typex) < TYPE_UID (typey))
1053 i++;
1054 if (i == fieldsx.length ())
1055 break;
1057 else
1059 j++;
1060 if (j == fieldsy.length ())
1061 break;
1064 while (1);
1066 return false;
1070 /* Return true if two memory references based on the variables BASE1
1071 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1072 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1073 if non-NULL are the complete memory reference trees. */
1075 static bool
1076 decl_refs_may_alias_p (tree ref1, tree base1,
1077 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1078 tree ref2, tree base2,
1079 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1081 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1083 /* If both references are based on different variables, they cannot alias. */
1084 if (compare_base_decls (base1, base2) == 0)
1085 return false;
1087 /* If both references are based on the same variable, they cannot alias if
1088 the accesses do not overlap. */
1089 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1090 return false;
1092 /* For components with variable position, the above test isn't sufficient,
1093 so we disambiguate component references manually. */
1094 if (ref1 && ref2
1095 && handled_component_p (ref1) && handled_component_p (ref2)
1096 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1097 return false;
1099 return true;
1102 /* Return true if an indirect reference based on *PTR1 constrained
1103 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1104 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1105 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1106 in which case they are computed on-demand. REF1 and REF2
1107 if non-NULL are the complete memory reference trees. */
1109 static bool
1110 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1111 HOST_WIDE_INT offset1,
1112 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1113 alias_set_type ref1_alias_set,
1114 alias_set_type base1_alias_set,
1115 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1116 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1117 alias_set_type ref2_alias_set,
1118 alias_set_type base2_alias_set, bool tbaa_p)
1120 tree ptr1;
1121 tree ptrtype1, dbase2;
1122 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1123 HOST_WIDE_INT doffset1, doffset2;
1125 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1126 || TREE_CODE (base1) == TARGET_MEM_REF)
1127 && DECL_P (base2));
1129 ptr1 = TREE_OPERAND (base1, 0);
1131 /* The offset embedded in MEM_REFs can be negative. Bias them
1132 so that the resulting offset adjustment is positive. */
1133 offset_int moff = mem_ref_offset (base1);
1134 moff <<= LOG2_BITS_PER_UNIT;
1135 if (wi::neg_p (moff))
1136 offset2p += (-moff).to_short_addr ();
1137 else
1138 offset1p += moff.to_short_addr ();
1140 /* If only one reference is based on a variable, they cannot alias if
1141 the pointer access is beyond the extent of the variable access.
1142 (the pointer base cannot validly point to an offset less than zero
1143 of the variable).
1144 ??? IVOPTs creates bases that do not honor this restriction,
1145 so do not apply this optimization for TARGET_MEM_REFs. */
1146 if (TREE_CODE (base1) != TARGET_MEM_REF
1147 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1148 return false;
1149 /* They also cannot alias if the pointer may not point to the decl. */
1150 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1151 return false;
1153 /* Disambiguations that rely on strict aliasing rules follow. */
1154 if (!flag_strict_aliasing || !tbaa_p)
1155 return true;
1157 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1159 /* If the alias set for a pointer access is zero all bets are off. */
1160 if (base1_alias_set == 0)
1161 return true;
1163 /* When we are trying to disambiguate an access with a pointer dereference
1164 as base versus one with a decl as base we can use both the size
1165 of the decl and its dynamic type for extra disambiguation.
1166 ??? We do not know anything about the dynamic type of the decl
1167 other than that its alias-set contains base2_alias_set as a subset
1168 which does not help us here. */
1169 /* As we know nothing useful about the dynamic type of the decl just
1170 use the usual conflict check rather than a subset test.
1171 ??? We could introduce -fvery-strict-aliasing when the language
1172 does not allow decls to have a dynamic type that differs from their
1173 static type. Then we can check
1174 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1175 if (base1_alias_set != base2_alias_set
1176 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1177 return false;
1178 /* If the size of the access relevant for TBAA through the pointer
1179 is bigger than the size of the decl we can't possibly access the
1180 decl via that pointer. */
1181 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1182 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1183 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1184 /* ??? This in turn may run afoul when a decl of type T which is
1185 a member of union type U is accessed through a pointer to
1186 type U and sizeof T is smaller than sizeof U. */
1187 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1188 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1189 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1190 return false;
1192 if (!ref2)
1193 return true;
1195 /* If the decl is accessed via a MEM_REF, reconstruct the base
1196 we can use for TBAA and an appropriately adjusted offset. */
1197 dbase2 = ref2;
1198 while (handled_component_p (dbase2))
1199 dbase2 = TREE_OPERAND (dbase2, 0);
1200 doffset1 = offset1;
1201 doffset2 = offset2;
1202 if (TREE_CODE (dbase2) == MEM_REF
1203 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1205 offset_int moff = mem_ref_offset (dbase2);
1206 moff <<= LOG2_BITS_PER_UNIT;
1207 if (wi::neg_p (moff))
1208 doffset1 -= (-moff).to_short_addr ();
1209 else
1210 doffset2 -= moff.to_short_addr ();
1213 /* If either reference is view-converted, give up now. */
1214 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1215 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1216 return true;
1218 /* If both references are through the same type, they do not alias
1219 if the accesses do not overlap. This does extra disambiguation
1220 for mixed/pointer accesses but requires strict aliasing.
1221 For MEM_REFs we require that the component-ref offset we computed
1222 is relative to the start of the type which we ensure by
1223 comparing rvalue and access type and disregarding the constant
1224 pointer offset. */
1225 if ((TREE_CODE (base1) != TARGET_MEM_REF
1226 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1227 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1228 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1230 if (ref1 && ref2
1231 && nonoverlapping_component_refs_p (ref1, ref2))
1232 return false;
1234 /* Do access-path based disambiguation. */
1235 if (ref1 && ref2
1236 && (handled_component_p (ref1) || handled_component_p (ref2)))
1237 return aliasing_component_refs_p (ref1,
1238 ref1_alias_set, base1_alias_set,
1239 offset1, max_size1,
1240 ref2,
1241 ref2_alias_set, base2_alias_set,
1242 offset2, max_size2, true);
1244 return true;
1247 /* Return true if two indirect references based on *PTR1
1248 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1249 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1250 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1251 in which case they are computed on-demand. REF1 and REF2
1252 if non-NULL are the complete memory reference trees. */
1254 static bool
1255 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1256 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1257 alias_set_type ref1_alias_set,
1258 alias_set_type base1_alias_set,
1259 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1260 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1261 alias_set_type ref2_alias_set,
1262 alias_set_type base2_alias_set, bool tbaa_p)
1264 tree ptr1;
1265 tree ptr2;
1266 tree ptrtype1, ptrtype2;
1268 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1269 || TREE_CODE (base1) == TARGET_MEM_REF)
1270 && (TREE_CODE (base2) == MEM_REF
1271 || TREE_CODE (base2) == TARGET_MEM_REF));
1273 ptr1 = TREE_OPERAND (base1, 0);
1274 ptr2 = TREE_OPERAND (base2, 0);
1276 /* If both bases are based on pointers they cannot alias if they may not
1277 point to the same memory object or if they point to the same object
1278 and the accesses do not overlap. */
1279 if ((!cfun || gimple_in_ssa_p (cfun))
1280 && operand_equal_p (ptr1, ptr2, 0)
1281 && (((TREE_CODE (base1) != TARGET_MEM_REF
1282 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1283 && (TREE_CODE (base2) != TARGET_MEM_REF
1284 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1285 || (TREE_CODE (base1) == TARGET_MEM_REF
1286 && TREE_CODE (base2) == TARGET_MEM_REF
1287 && (TMR_STEP (base1) == TMR_STEP (base2)
1288 || (TMR_STEP (base1) && TMR_STEP (base2)
1289 && operand_equal_p (TMR_STEP (base1),
1290 TMR_STEP (base2), 0)))
1291 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1292 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1293 && operand_equal_p (TMR_INDEX (base1),
1294 TMR_INDEX (base2), 0)))
1295 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1296 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1297 && operand_equal_p (TMR_INDEX2 (base1),
1298 TMR_INDEX2 (base2), 0))))))
1300 offset_int moff;
1301 /* The offset embedded in MEM_REFs can be negative. Bias them
1302 so that the resulting offset adjustment is positive. */
1303 moff = mem_ref_offset (base1);
1304 moff <<= LOG2_BITS_PER_UNIT;
1305 if (wi::neg_p (moff))
1306 offset2 += (-moff).to_short_addr ();
1307 else
1308 offset1 += moff.to_shwi ();
1309 moff = mem_ref_offset (base2);
1310 moff <<= LOG2_BITS_PER_UNIT;
1311 if (wi::neg_p (moff))
1312 offset1 += (-moff).to_short_addr ();
1313 else
1314 offset2 += moff.to_short_addr ();
1315 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1317 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1318 return false;
1320 /* Disambiguations that rely on strict aliasing rules follow. */
1321 if (!flag_strict_aliasing || !tbaa_p)
1322 return true;
1324 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1325 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1327 /* If the alias set for a pointer access is zero all bets are off. */
1328 if (base1_alias_set == 0
1329 || base2_alias_set == 0)
1330 return true;
1332 /* If both references are through the same type, they do not alias
1333 if the accesses do not overlap. This does extra disambiguation
1334 for mixed/pointer accesses but requires strict aliasing. */
1335 if ((TREE_CODE (base1) != TARGET_MEM_REF
1336 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1337 && (TREE_CODE (base2) != TARGET_MEM_REF
1338 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1339 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1340 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1341 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1342 TREE_TYPE (ptrtype2)) == 1)
1343 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1345 /* Do type-based disambiguation. */
1346 if (base1_alias_set != base2_alias_set
1347 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1348 return false;
1350 /* If either reference is view-converted, give up now. */
1351 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1352 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1353 return true;
1355 if (ref1 && ref2
1356 && nonoverlapping_component_refs_p (ref1, ref2))
1357 return false;
1359 /* Do access-path based disambiguation. */
1360 if (ref1 && ref2
1361 && (handled_component_p (ref1) || handled_component_p (ref2)))
1362 return aliasing_component_refs_p (ref1,
1363 ref1_alias_set, base1_alias_set,
1364 offset1, max_size1,
1365 ref2,
1366 ref2_alias_set, base2_alias_set,
1367 offset2, max_size2, false);
1369 return true;
1372 /* Return true, if the two memory references REF1 and REF2 may alias. */
1374 bool
1375 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1377 tree base1, base2;
1378 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1379 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1380 bool var1_p, var2_p, ind1_p, ind2_p;
1382 gcc_checking_assert ((!ref1->ref
1383 || TREE_CODE (ref1->ref) == SSA_NAME
1384 || DECL_P (ref1->ref)
1385 || TREE_CODE (ref1->ref) == STRING_CST
1386 || handled_component_p (ref1->ref)
1387 || TREE_CODE (ref1->ref) == MEM_REF
1388 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1389 && (!ref2->ref
1390 || TREE_CODE (ref2->ref) == SSA_NAME
1391 || DECL_P (ref2->ref)
1392 || TREE_CODE (ref2->ref) == STRING_CST
1393 || handled_component_p (ref2->ref)
1394 || TREE_CODE (ref2->ref) == MEM_REF
1395 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1397 /* Decompose the references into their base objects and the access. */
1398 base1 = ao_ref_base (ref1);
1399 offset1 = ref1->offset;
1400 max_size1 = ref1->max_size;
1401 base2 = ao_ref_base (ref2);
1402 offset2 = ref2->offset;
1403 max_size2 = ref2->max_size;
1405 /* We can end up with registers or constants as bases for example from
1406 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1407 which is seen as a struct copy. */
1408 if (TREE_CODE (base1) == SSA_NAME
1409 || TREE_CODE (base1) == CONST_DECL
1410 || TREE_CODE (base1) == CONSTRUCTOR
1411 || TREE_CODE (base1) == ADDR_EXPR
1412 || CONSTANT_CLASS_P (base1)
1413 || TREE_CODE (base2) == SSA_NAME
1414 || TREE_CODE (base2) == CONST_DECL
1415 || TREE_CODE (base2) == CONSTRUCTOR
1416 || TREE_CODE (base2) == ADDR_EXPR
1417 || CONSTANT_CLASS_P (base2))
1418 return false;
1420 /* We can end up referring to code via function and label decls.
1421 As we likely do not properly track code aliases conservatively
1422 bail out. */
1423 if (TREE_CODE (base1) == FUNCTION_DECL
1424 || TREE_CODE (base1) == LABEL_DECL
1425 || TREE_CODE (base2) == FUNCTION_DECL
1426 || TREE_CODE (base2) == LABEL_DECL)
1427 return true;
1429 /* Two volatile accesses always conflict. */
1430 if (ref1->volatile_p
1431 && ref2->volatile_p)
1432 return true;
1434 /* Defer to simple offset based disambiguation if we have
1435 references based on two decls. Do this before defering to
1436 TBAA to handle must-alias cases in conformance with the
1437 GCC extension of allowing type-punning through unions. */
1438 var1_p = DECL_P (base1);
1439 var2_p = DECL_P (base2);
1440 if (var1_p && var2_p)
1441 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1442 ref2->ref, base2, offset2, max_size2);
1444 /* Handle restrict based accesses.
1445 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1446 here. */
1447 tree rbase1 = base1;
1448 tree rbase2 = base2;
1449 if (var1_p)
1451 rbase1 = ref1->ref;
1452 if (rbase1)
1453 while (handled_component_p (rbase1))
1454 rbase1 = TREE_OPERAND (rbase1, 0);
1456 if (var2_p)
1458 rbase2 = ref2->ref;
1459 if (rbase2)
1460 while (handled_component_p (rbase2))
1461 rbase2 = TREE_OPERAND (rbase2, 0);
1463 if (rbase1 && rbase2
1464 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1465 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1466 /* If the accesses are in the same restrict clique... */
1467 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1468 /* But based on different pointers they do not alias. */
1469 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1470 return false;
1472 ind1_p = (TREE_CODE (base1) == MEM_REF
1473 || TREE_CODE (base1) == TARGET_MEM_REF);
1474 ind2_p = (TREE_CODE (base2) == MEM_REF
1475 || TREE_CODE (base2) == TARGET_MEM_REF);
1477 /* Canonicalize the pointer-vs-decl case. */
1478 if (ind1_p && var2_p)
1480 std::swap (offset1, offset2);
1481 std::swap (max_size1, max_size2);
1482 std::swap (base1, base2);
1483 std::swap (ref1, ref2);
1484 var1_p = true;
1485 ind1_p = false;
1486 var2_p = false;
1487 ind2_p = true;
1490 /* First defer to TBAA if possible. */
1491 if (tbaa_p
1492 && flag_strict_aliasing
1493 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1494 ao_ref_alias_set (ref2)))
1495 return false;
1497 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1498 if (var1_p && ind2_p)
1499 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1500 offset2, max_size2,
1501 ao_ref_alias_set (ref2),
1502 ao_ref_base_alias_set (ref2),
1503 ref1->ref, base1,
1504 offset1, max_size1,
1505 ao_ref_alias_set (ref1),
1506 ao_ref_base_alias_set (ref1),
1507 tbaa_p);
1508 else if (ind1_p && ind2_p)
1509 return indirect_refs_may_alias_p (ref1->ref, base1,
1510 offset1, max_size1,
1511 ao_ref_alias_set (ref1),
1512 ao_ref_base_alias_set (ref1),
1513 ref2->ref, base2,
1514 offset2, max_size2,
1515 ao_ref_alias_set (ref2),
1516 ao_ref_base_alias_set (ref2),
1517 tbaa_p);
1519 gcc_unreachable ();
1522 static bool
1523 refs_may_alias_p (tree ref1, ao_ref *ref2)
1525 ao_ref r1;
1526 ao_ref_init (&r1, ref1);
1527 return refs_may_alias_p_1 (&r1, ref2, true);
1530 bool
1531 refs_may_alias_p (tree ref1, tree ref2)
1533 ao_ref r1, r2;
1534 bool res;
1535 ao_ref_init (&r1, ref1);
1536 ao_ref_init (&r2, ref2);
1537 res = refs_may_alias_p_1 (&r1, &r2, true);
1538 if (res)
1539 ++alias_stats.refs_may_alias_p_may_alias;
1540 else
1541 ++alias_stats.refs_may_alias_p_no_alias;
1542 return res;
1545 /* Returns true if there is a anti-dependence for the STORE that
1546 executes after the LOAD. */
1548 bool
1549 refs_anti_dependent_p (tree load, tree store)
1551 ao_ref r1, r2;
1552 ao_ref_init (&r1, load);
1553 ao_ref_init (&r2, store);
1554 return refs_may_alias_p_1 (&r1, &r2, false);
1557 /* Returns true if there is a output dependence for the stores
1558 STORE1 and STORE2. */
1560 bool
1561 refs_output_dependent_p (tree store1, tree store2)
1563 ao_ref r1, r2;
1564 ao_ref_init (&r1, store1);
1565 ao_ref_init (&r2, store2);
1566 return refs_may_alias_p_1 (&r1, &r2, false);
1569 /* If the call CALL may use the memory reference REF return true,
1570 otherwise return false. */
1572 static bool
1573 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1575 tree base, callee;
1576 unsigned i;
1577 int flags = gimple_call_flags (call);
1579 /* Const functions without a static chain do not implicitly use memory. */
1580 if (!gimple_call_chain (call)
1581 && (flags & (ECF_CONST|ECF_NOVOPS)))
1582 goto process_args;
1584 base = ao_ref_base (ref);
1585 if (!base)
1586 return true;
1588 /* A call that is not without side-effects might involve volatile
1589 accesses and thus conflicts with all other volatile accesses. */
1590 if (ref->volatile_p)
1591 return true;
1593 /* If the reference is based on a decl that is not aliased the call
1594 cannot possibly use it. */
1595 if (DECL_P (base)
1596 && !may_be_aliased (base)
1597 /* But local statics can be used through recursion. */
1598 && !is_global_var (base))
1599 goto process_args;
1601 callee = gimple_call_fndecl (call);
1603 /* Handle those builtin functions explicitly that do not act as
1604 escape points. See tree-ssa-structalias.c:find_func_aliases
1605 for the list of builtins we might need to handle here. */
1606 if (callee != NULL_TREE
1607 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1608 switch (DECL_FUNCTION_CODE (callee))
1610 /* All the following functions read memory pointed to by
1611 their second argument. strcat/strncat additionally
1612 reads memory pointed to by the first argument. */
1613 case BUILT_IN_STRCAT:
1614 case BUILT_IN_STRNCAT:
1616 ao_ref dref;
1617 ao_ref_init_from_ptr_and_size (&dref,
1618 gimple_call_arg (call, 0),
1619 NULL_TREE);
1620 if (refs_may_alias_p_1 (&dref, ref, false))
1621 return true;
1623 /* FALLTHRU */
1624 case BUILT_IN_STRCPY:
1625 case BUILT_IN_STRNCPY:
1626 case BUILT_IN_MEMCPY:
1627 case BUILT_IN_MEMMOVE:
1628 case BUILT_IN_MEMPCPY:
1629 case BUILT_IN_STPCPY:
1630 case BUILT_IN_STPNCPY:
1631 case BUILT_IN_TM_MEMCPY:
1632 case BUILT_IN_TM_MEMMOVE:
1634 ao_ref dref;
1635 tree size = NULL_TREE;
1636 if (gimple_call_num_args (call) == 3)
1637 size = gimple_call_arg (call, 2);
1638 ao_ref_init_from_ptr_and_size (&dref,
1639 gimple_call_arg (call, 1),
1640 size);
1641 return refs_may_alias_p_1 (&dref, ref, false);
1643 case BUILT_IN_STRCAT_CHK:
1644 case BUILT_IN_STRNCAT_CHK:
1646 ao_ref dref;
1647 ao_ref_init_from_ptr_and_size (&dref,
1648 gimple_call_arg (call, 0),
1649 NULL_TREE);
1650 if (refs_may_alias_p_1 (&dref, ref, false))
1651 return true;
1653 /* FALLTHRU */
1654 case BUILT_IN_STRCPY_CHK:
1655 case BUILT_IN_STRNCPY_CHK:
1656 case BUILT_IN_MEMCPY_CHK:
1657 case BUILT_IN_MEMMOVE_CHK:
1658 case BUILT_IN_MEMPCPY_CHK:
1659 case BUILT_IN_STPCPY_CHK:
1660 case BUILT_IN_STPNCPY_CHK:
1662 ao_ref dref;
1663 tree size = NULL_TREE;
1664 if (gimple_call_num_args (call) == 4)
1665 size = gimple_call_arg (call, 2);
1666 ao_ref_init_from_ptr_and_size (&dref,
1667 gimple_call_arg (call, 1),
1668 size);
1669 return refs_may_alias_p_1 (&dref, ref, false);
1671 case BUILT_IN_BCOPY:
1673 ao_ref dref;
1674 tree size = gimple_call_arg (call, 2);
1675 ao_ref_init_from_ptr_and_size (&dref,
1676 gimple_call_arg (call, 0),
1677 size);
1678 return refs_may_alias_p_1 (&dref, ref, false);
1681 /* The following functions read memory pointed to by their
1682 first argument. */
1683 CASE_BUILT_IN_TM_LOAD (1):
1684 CASE_BUILT_IN_TM_LOAD (2):
1685 CASE_BUILT_IN_TM_LOAD (4):
1686 CASE_BUILT_IN_TM_LOAD (8):
1687 CASE_BUILT_IN_TM_LOAD (FLOAT):
1688 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1689 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1690 CASE_BUILT_IN_TM_LOAD (M64):
1691 CASE_BUILT_IN_TM_LOAD (M128):
1692 CASE_BUILT_IN_TM_LOAD (M256):
1693 case BUILT_IN_TM_LOG:
1694 case BUILT_IN_TM_LOG_1:
1695 case BUILT_IN_TM_LOG_2:
1696 case BUILT_IN_TM_LOG_4:
1697 case BUILT_IN_TM_LOG_8:
1698 case BUILT_IN_TM_LOG_FLOAT:
1699 case BUILT_IN_TM_LOG_DOUBLE:
1700 case BUILT_IN_TM_LOG_LDOUBLE:
1701 case BUILT_IN_TM_LOG_M64:
1702 case BUILT_IN_TM_LOG_M128:
1703 case BUILT_IN_TM_LOG_M256:
1704 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1706 /* These read memory pointed to by the first argument. */
1707 case BUILT_IN_STRDUP:
1708 case BUILT_IN_STRNDUP:
1709 case BUILT_IN_REALLOC:
1711 ao_ref dref;
1712 tree size = NULL_TREE;
1713 if (gimple_call_num_args (call) == 2)
1714 size = gimple_call_arg (call, 1);
1715 ao_ref_init_from_ptr_and_size (&dref,
1716 gimple_call_arg (call, 0),
1717 size);
1718 return refs_may_alias_p_1 (&dref, ref, false);
1720 /* These read memory pointed to by the first argument. */
1721 case BUILT_IN_INDEX:
1722 case BUILT_IN_STRCHR:
1723 case BUILT_IN_STRRCHR:
1725 ao_ref dref;
1726 ao_ref_init_from_ptr_and_size (&dref,
1727 gimple_call_arg (call, 0),
1728 NULL_TREE);
1729 return refs_may_alias_p_1 (&dref, ref, false);
1731 /* These read memory pointed to by the first argument with size
1732 in the third argument. */
1733 case BUILT_IN_MEMCHR:
1735 ao_ref dref;
1736 ao_ref_init_from_ptr_and_size (&dref,
1737 gimple_call_arg (call, 0),
1738 gimple_call_arg (call, 2));
1739 return refs_may_alias_p_1 (&dref, ref, false);
1741 /* These read memory pointed to by the first and second arguments. */
1742 case BUILT_IN_STRSTR:
1743 case BUILT_IN_STRPBRK:
1745 ao_ref dref;
1746 ao_ref_init_from_ptr_and_size (&dref,
1747 gimple_call_arg (call, 0),
1748 NULL_TREE);
1749 if (refs_may_alias_p_1 (&dref, ref, false))
1750 return true;
1751 ao_ref_init_from_ptr_and_size (&dref,
1752 gimple_call_arg (call, 1),
1753 NULL_TREE);
1754 return refs_may_alias_p_1 (&dref, ref, false);
1757 /* The following builtins do not read from memory. */
1758 case BUILT_IN_FREE:
1759 case BUILT_IN_MALLOC:
1760 case BUILT_IN_POSIX_MEMALIGN:
1761 case BUILT_IN_ALIGNED_ALLOC:
1762 case BUILT_IN_CALLOC:
1763 case BUILT_IN_ALLOCA:
1764 case BUILT_IN_ALLOCA_WITH_ALIGN:
1765 case BUILT_IN_STACK_SAVE:
1766 case BUILT_IN_STACK_RESTORE:
1767 case BUILT_IN_MEMSET:
1768 case BUILT_IN_TM_MEMSET:
1769 case BUILT_IN_MEMSET_CHK:
1770 case BUILT_IN_FREXP:
1771 case BUILT_IN_FREXPF:
1772 case BUILT_IN_FREXPL:
1773 case BUILT_IN_GAMMA_R:
1774 case BUILT_IN_GAMMAF_R:
1775 case BUILT_IN_GAMMAL_R:
1776 case BUILT_IN_LGAMMA_R:
1777 case BUILT_IN_LGAMMAF_R:
1778 case BUILT_IN_LGAMMAL_R:
1779 case BUILT_IN_MODF:
1780 case BUILT_IN_MODFF:
1781 case BUILT_IN_MODFL:
1782 case BUILT_IN_REMQUO:
1783 case BUILT_IN_REMQUOF:
1784 case BUILT_IN_REMQUOL:
1785 case BUILT_IN_SINCOS:
1786 case BUILT_IN_SINCOSF:
1787 case BUILT_IN_SINCOSL:
1788 case BUILT_IN_ASSUME_ALIGNED:
1789 case BUILT_IN_VA_END:
1790 return false;
1791 /* __sync_* builtins and some OpenMP builtins act as threading
1792 barriers. */
1793 #undef DEF_SYNC_BUILTIN
1794 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1795 #include "sync-builtins.def"
1796 #undef DEF_SYNC_BUILTIN
1797 case BUILT_IN_GOMP_ATOMIC_START:
1798 case BUILT_IN_GOMP_ATOMIC_END:
1799 case BUILT_IN_GOMP_BARRIER:
1800 case BUILT_IN_GOMP_BARRIER_CANCEL:
1801 case BUILT_IN_GOMP_TASKWAIT:
1802 case BUILT_IN_GOMP_TASKGROUP_END:
1803 case BUILT_IN_GOMP_CRITICAL_START:
1804 case BUILT_IN_GOMP_CRITICAL_END:
1805 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1806 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1807 case BUILT_IN_GOMP_LOOP_END:
1808 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1809 case BUILT_IN_GOMP_ORDERED_START:
1810 case BUILT_IN_GOMP_ORDERED_END:
1811 case BUILT_IN_GOMP_SECTIONS_END:
1812 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1813 case BUILT_IN_GOMP_SINGLE_COPY_START:
1814 case BUILT_IN_GOMP_SINGLE_COPY_END:
1815 return true;
1817 default:
1818 /* Fallthru to general call handling. */;
1821 /* Check if base is a global static variable that is not read
1822 by the function. */
1823 if (callee != NULL_TREE
1824 && TREE_CODE (base) == VAR_DECL
1825 && TREE_STATIC (base))
1827 struct cgraph_node *node = cgraph_node::get (callee);
1828 bitmap not_read;
1830 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1831 node yet. We should enforce that there are nodes for all decls in the
1832 IL and remove this check instead. */
1833 if (node
1834 && (not_read = ipa_reference_get_not_read_global (node))
1835 && bitmap_bit_p (not_read, ipa_reference_var_uid (base)))
1836 goto process_args;
1839 /* Check if the base variable is call-used. */
1840 if (DECL_P (base))
1842 if (pt_solution_includes (gimple_call_use_set (call), base))
1843 return true;
1845 else if ((TREE_CODE (base) == MEM_REF
1846 || TREE_CODE (base) == TARGET_MEM_REF)
1847 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1849 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1850 if (!pi)
1851 return true;
1853 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1854 return true;
1856 else
1857 return true;
1859 /* Inspect call arguments for passed-by-value aliases. */
1860 process_args:
1861 for (i = 0; i < gimple_call_num_args (call); ++i)
1863 tree op = gimple_call_arg (call, i);
1864 int flags = gimple_call_arg_flags (call, i);
1866 if (flags & EAF_UNUSED)
1867 continue;
1869 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1870 op = TREE_OPERAND (op, 0);
1872 if (TREE_CODE (op) != SSA_NAME
1873 && !is_gimple_min_invariant (op))
1875 ao_ref r;
1876 ao_ref_init (&r, op);
1877 if (refs_may_alias_p_1 (&r, ref, true))
1878 return true;
1882 return false;
1885 static bool
1886 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref)
1888 bool res;
1889 res = ref_maybe_used_by_call_p_1 (call, ref);
1890 if (res)
1891 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1892 else
1893 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1894 return res;
1898 /* If the statement STMT may use the memory reference REF return
1899 true, otherwise return false. */
1901 bool
1902 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref)
1904 if (is_gimple_assign (stmt))
1906 tree rhs;
1908 /* All memory assign statements are single. */
1909 if (!gimple_assign_single_p (stmt))
1910 return false;
1912 rhs = gimple_assign_rhs1 (stmt);
1913 if (is_gimple_reg (rhs)
1914 || is_gimple_min_invariant (rhs)
1915 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1916 return false;
1918 return refs_may_alias_p (rhs, ref);
1920 else if (is_gimple_call (stmt))
1921 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref);
1922 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1924 tree retval = gimple_return_retval (return_stmt);
1925 if (retval
1926 && TREE_CODE (retval) != SSA_NAME
1927 && !is_gimple_min_invariant (retval)
1928 && refs_may_alias_p (retval, ref))
1929 return true;
1930 /* If ref escapes the function then the return acts as a use. */
1931 tree base = ao_ref_base (ref);
1932 if (!base)
1934 else if (DECL_P (base))
1935 return is_global_var (base);
1936 else if (TREE_CODE (base) == MEM_REF
1937 || TREE_CODE (base) == TARGET_MEM_REF)
1938 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1939 return false;
1942 return true;
1945 bool
1946 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref)
1948 ao_ref r;
1949 ao_ref_init (&r, ref);
1950 return ref_maybe_used_by_stmt_p (stmt, &r);
1953 /* If the call in statement CALL may clobber the memory reference REF
1954 return true, otherwise return false. */
1956 bool
1957 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
1959 tree base;
1960 tree callee;
1962 /* If the call is pure or const it cannot clobber anything. */
1963 if (gimple_call_flags (call)
1964 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1965 return false;
1966 if (gimple_call_internal_p (call))
1967 switch (gimple_call_internal_fn (call))
1969 /* Treat these internal calls like ECF_PURE for aliasing,
1970 they don't write to any memory the program should care about.
1971 They have important other side-effects, and read memory,
1972 so can't be ECF_NOVOPS. */
1973 case IFN_UBSAN_NULL:
1974 case IFN_UBSAN_BOUNDS:
1975 case IFN_UBSAN_VPTR:
1976 case IFN_UBSAN_OBJECT_SIZE:
1977 case IFN_ASAN_CHECK:
1978 return false;
1979 default:
1980 break;
1983 base = ao_ref_base (ref);
1984 if (!base)
1985 return true;
1987 if (TREE_CODE (base) == SSA_NAME
1988 || CONSTANT_CLASS_P (base))
1989 return false;
1991 /* A call that is not without side-effects might involve volatile
1992 accesses and thus conflicts with all other volatile accesses. */
1993 if (ref->volatile_p)
1994 return true;
1996 /* If the reference is based on a decl that is not aliased the call
1997 cannot possibly clobber it. */
1998 if (DECL_P (base)
1999 && !may_be_aliased (base)
2000 /* But local non-readonly statics can be modified through recursion
2001 or the call may implement a threading barrier which we must
2002 treat as may-def. */
2003 && (TREE_READONLY (base)
2004 || !is_global_var (base)))
2005 return false;
2007 callee = gimple_call_fndecl (call);
2009 /* Handle those builtin functions explicitly that do not act as
2010 escape points. See tree-ssa-structalias.c:find_func_aliases
2011 for the list of builtins we might need to handle here. */
2012 if (callee != NULL_TREE
2013 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2014 switch (DECL_FUNCTION_CODE (callee))
2016 /* All the following functions clobber memory pointed to by
2017 their first argument. */
2018 case BUILT_IN_STRCPY:
2019 case BUILT_IN_STRNCPY:
2020 case BUILT_IN_MEMCPY:
2021 case BUILT_IN_MEMMOVE:
2022 case BUILT_IN_MEMPCPY:
2023 case BUILT_IN_STPCPY:
2024 case BUILT_IN_STPNCPY:
2025 case BUILT_IN_STRCAT:
2026 case BUILT_IN_STRNCAT:
2027 case BUILT_IN_MEMSET:
2028 case BUILT_IN_TM_MEMSET:
2029 CASE_BUILT_IN_TM_STORE (1):
2030 CASE_BUILT_IN_TM_STORE (2):
2031 CASE_BUILT_IN_TM_STORE (4):
2032 CASE_BUILT_IN_TM_STORE (8):
2033 CASE_BUILT_IN_TM_STORE (FLOAT):
2034 CASE_BUILT_IN_TM_STORE (DOUBLE):
2035 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2036 CASE_BUILT_IN_TM_STORE (M64):
2037 CASE_BUILT_IN_TM_STORE (M128):
2038 CASE_BUILT_IN_TM_STORE (M256):
2039 case BUILT_IN_TM_MEMCPY:
2040 case BUILT_IN_TM_MEMMOVE:
2042 ao_ref dref;
2043 tree size = NULL_TREE;
2044 /* Don't pass in size for strncat, as the maximum size
2045 is strlen (dest) + n + 1 instead of n, resp.
2046 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2047 known. */
2048 if (gimple_call_num_args (call) == 3
2049 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2050 size = gimple_call_arg (call, 2);
2051 ao_ref_init_from_ptr_and_size (&dref,
2052 gimple_call_arg (call, 0),
2053 size);
2054 return refs_may_alias_p_1 (&dref, ref, false);
2056 case BUILT_IN_STRCPY_CHK:
2057 case BUILT_IN_STRNCPY_CHK:
2058 case BUILT_IN_MEMCPY_CHK:
2059 case BUILT_IN_MEMMOVE_CHK:
2060 case BUILT_IN_MEMPCPY_CHK:
2061 case BUILT_IN_STPCPY_CHK:
2062 case BUILT_IN_STPNCPY_CHK:
2063 case BUILT_IN_STRCAT_CHK:
2064 case BUILT_IN_STRNCAT_CHK:
2065 case BUILT_IN_MEMSET_CHK:
2067 ao_ref dref;
2068 tree size = NULL_TREE;
2069 /* Don't pass in size for __strncat_chk, as the maximum size
2070 is strlen (dest) + n + 1 instead of n, resp.
2071 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2072 known. */
2073 if (gimple_call_num_args (call) == 4
2074 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2075 size = gimple_call_arg (call, 2);
2076 ao_ref_init_from_ptr_and_size (&dref,
2077 gimple_call_arg (call, 0),
2078 size);
2079 return refs_may_alias_p_1 (&dref, ref, false);
2081 case BUILT_IN_BCOPY:
2083 ao_ref dref;
2084 tree size = gimple_call_arg (call, 2);
2085 ao_ref_init_from_ptr_and_size (&dref,
2086 gimple_call_arg (call, 1),
2087 size);
2088 return refs_may_alias_p_1 (&dref, ref, false);
2090 /* Allocating memory does not have any side-effects apart from
2091 being the definition point for the pointer. */
2092 case BUILT_IN_MALLOC:
2093 case BUILT_IN_ALIGNED_ALLOC:
2094 case BUILT_IN_CALLOC:
2095 case BUILT_IN_STRDUP:
2096 case BUILT_IN_STRNDUP:
2097 /* Unix98 specifies that errno is set on allocation failure. */
2098 if (flag_errno_math
2099 && targetm.ref_may_alias_errno (ref))
2100 return true;
2101 return false;
2102 case BUILT_IN_STACK_SAVE:
2103 case BUILT_IN_ALLOCA:
2104 case BUILT_IN_ALLOCA_WITH_ALIGN:
2105 case BUILT_IN_ASSUME_ALIGNED:
2106 return false;
2107 /* But posix_memalign stores a pointer into the memory pointed to
2108 by its first argument. */
2109 case BUILT_IN_POSIX_MEMALIGN:
2111 tree ptrptr = gimple_call_arg (call, 0);
2112 ao_ref dref;
2113 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2114 TYPE_SIZE_UNIT (ptr_type_node));
2115 return (refs_may_alias_p_1 (&dref, ref, false)
2116 || (flag_errno_math
2117 && targetm.ref_may_alias_errno (ref)));
2119 /* Freeing memory kills the pointed-to memory. More importantly
2120 the call has to serve as a barrier for moving loads and stores
2121 across it. */
2122 case BUILT_IN_FREE:
2123 case BUILT_IN_VA_END:
2125 tree ptr = gimple_call_arg (call, 0);
2126 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2128 /* Realloc serves both as allocation point and deallocation point. */
2129 case BUILT_IN_REALLOC:
2131 tree ptr = gimple_call_arg (call, 0);
2132 /* Unix98 specifies that errno is set on allocation failure. */
2133 return ((flag_errno_math
2134 && targetm.ref_may_alias_errno (ref))
2135 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2137 case BUILT_IN_GAMMA_R:
2138 case BUILT_IN_GAMMAF_R:
2139 case BUILT_IN_GAMMAL_R:
2140 case BUILT_IN_LGAMMA_R:
2141 case BUILT_IN_LGAMMAF_R:
2142 case BUILT_IN_LGAMMAL_R:
2144 tree out = gimple_call_arg (call, 1);
2145 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2146 return true;
2147 if (flag_errno_math)
2148 break;
2149 return false;
2151 case BUILT_IN_FREXP:
2152 case BUILT_IN_FREXPF:
2153 case BUILT_IN_FREXPL:
2154 case BUILT_IN_MODF:
2155 case BUILT_IN_MODFF:
2156 case BUILT_IN_MODFL:
2158 tree out = gimple_call_arg (call, 1);
2159 return ptr_deref_may_alias_ref_p_1 (out, ref);
2161 case BUILT_IN_REMQUO:
2162 case BUILT_IN_REMQUOF:
2163 case BUILT_IN_REMQUOL:
2165 tree out = gimple_call_arg (call, 2);
2166 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2167 return true;
2168 if (flag_errno_math)
2169 break;
2170 return false;
2172 case BUILT_IN_SINCOS:
2173 case BUILT_IN_SINCOSF:
2174 case BUILT_IN_SINCOSL:
2176 tree sin = gimple_call_arg (call, 1);
2177 tree cos = gimple_call_arg (call, 2);
2178 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2179 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2181 /* __sync_* builtins and some OpenMP builtins act as threading
2182 barriers. */
2183 #undef DEF_SYNC_BUILTIN
2184 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2185 #include "sync-builtins.def"
2186 #undef DEF_SYNC_BUILTIN
2187 case BUILT_IN_GOMP_ATOMIC_START:
2188 case BUILT_IN_GOMP_ATOMIC_END:
2189 case BUILT_IN_GOMP_BARRIER:
2190 case BUILT_IN_GOMP_BARRIER_CANCEL:
2191 case BUILT_IN_GOMP_TASKWAIT:
2192 case BUILT_IN_GOMP_TASKGROUP_END:
2193 case BUILT_IN_GOMP_CRITICAL_START:
2194 case BUILT_IN_GOMP_CRITICAL_END:
2195 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2196 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2197 case BUILT_IN_GOMP_LOOP_END:
2198 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2199 case BUILT_IN_GOMP_ORDERED_START:
2200 case BUILT_IN_GOMP_ORDERED_END:
2201 case BUILT_IN_GOMP_SECTIONS_END:
2202 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2203 case BUILT_IN_GOMP_SINGLE_COPY_START:
2204 case BUILT_IN_GOMP_SINGLE_COPY_END:
2205 return true;
2206 default:
2207 /* Fallthru to general call handling. */;
2210 /* Check if base is a global static variable that is not written
2211 by the function. */
2212 if (callee != NULL_TREE
2213 && TREE_CODE (base) == VAR_DECL
2214 && TREE_STATIC (base))
2216 struct cgraph_node *node = cgraph_node::get (callee);
2217 bitmap not_written;
2219 if (node
2220 && (not_written = ipa_reference_get_not_written_global (node))
2221 && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
2222 return false;
2225 /* Check if the base variable is call-clobbered. */
2226 if (DECL_P (base))
2227 return pt_solution_includes (gimple_call_clobber_set (call), base);
2228 else if ((TREE_CODE (base) == MEM_REF
2229 || TREE_CODE (base) == TARGET_MEM_REF)
2230 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2232 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2233 if (!pi)
2234 return true;
2236 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2239 return true;
2242 /* If the call in statement CALL may clobber the memory reference REF
2243 return true, otherwise return false. */
2245 bool
2246 call_may_clobber_ref_p (gcall *call, tree ref)
2248 bool res;
2249 ao_ref r;
2250 ao_ref_init (&r, ref);
2251 res = call_may_clobber_ref_p_1 (call, &r);
2252 if (res)
2253 ++alias_stats.call_may_clobber_ref_p_may_alias;
2254 else
2255 ++alias_stats.call_may_clobber_ref_p_no_alias;
2256 return res;
2260 /* If the statement STMT may clobber the memory reference REF return true,
2261 otherwise return false. */
2263 bool
2264 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref)
2266 if (is_gimple_call (stmt))
2268 tree lhs = gimple_call_lhs (stmt);
2269 if (lhs
2270 && TREE_CODE (lhs) != SSA_NAME)
2272 ao_ref r;
2273 ao_ref_init (&r, lhs);
2274 if (refs_may_alias_p_1 (ref, &r, true))
2275 return true;
2278 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2280 else if (gimple_assign_single_p (stmt))
2282 tree lhs = gimple_assign_lhs (stmt);
2283 if (TREE_CODE (lhs) != SSA_NAME)
2285 ao_ref r;
2286 ao_ref_init (&r, lhs);
2287 return refs_may_alias_p_1 (ref, &r, true);
2290 else if (gimple_code (stmt) == GIMPLE_ASM)
2291 return true;
2293 return false;
2296 bool
2297 stmt_may_clobber_ref_p (gimple *stmt, tree ref)
2299 ao_ref r;
2300 ao_ref_init (&r, ref);
2301 return stmt_may_clobber_ref_p_1 (stmt, &r);
2304 /* If STMT kills the memory reference REF return true, otherwise
2305 return false. */
2307 bool
2308 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2310 if (!ao_ref_base (ref))
2311 return false;
2313 if (gimple_has_lhs (stmt)
2314 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2315 /* The assignment is not necessarily carried out if it can throw
2316 and we can catch it in the current function where we could inspect
2317 the previous value.
2318 ??? We only need to care about the RHS throwing. For aggregate
2319 assignments or similar calls and non-call exceptions the LHS
2320 might throw as well. */
2321 && !stmt_can_throw_internal (stmt))
2323 tree lhs = gimple_get_lhs (stmt);
2324 /* If LHS is literally a base of the access we are done. */
2325 if (ref->ref)
2327 tree base = ref->ref;
2328 if (handled_component_p (base))
2330 tree saved_lhs0 = NULL_TREE;
2331 if (handled_component_p (lhs))
2333 saved_lhs0 = TREE_OPERAND (lhs, 0);
2334 TREE_OPERAND (lhs, 0) = integer_zero_node;
2338 /* Just compare the outermost handled component, if
2339 they are equal we have found a possible common
2340 base. */
2341 tree saved_base0 = TREE_OPERAND (base, 0);
2342 TREE_OPERAND (base, 0) = integer_zero_node;
2343 bool res = operand_equal_p (lhs, base, 0);
2344 TREE_OPERAND (base, 0) = saved_base0;
2345 if (res)
2346 break;
2347 /* Otherwise drop handled components of the access. */
2348 base = saved_base0;
2350 while (handled_component_p (base));
2351 if (saved_lhs0)
2352 TREE_OPERAND (lhs, 0) = saved_lhs0;
2354 /* Finally check if the lhs has the same address and size as the
2355 base candidate of the access. */
2356 if (lhs == base
2357 || (((TYPE_SIZE (TREE_TYPE (lhs))
2358 == TYPE_SIZE (TREE_TYPE (base)))
2359 || (TYPE_SIZE (TREE_TYPE (lhs))
2360 && TYPE_SIZE (TREE_TYPE (base))
2361 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2362 TYPE_SIZE (TREE_TYPE (base)), 0)))
2363 && operand_equal_p (lhs, base, OEP_ADDRESS_OF)))
2364 return true;
2367 /* Now look for non-literal equal bases with the restriction of
2368 handling constant offset and size. */
2369 /* For a must-alias check we need to be able to constrain
2370 the access properly. */
2371 if (ref->max_size == -1)
2372 return false;
2373 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2374 bool reverse;
2375 tree base
2376 = get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
2377 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2378 so base == ref->base does not always hold. */
2379 if (base != ref->base)
2381 /* If both base and ref->base are MEM_REFs, only compare the
2382 first operand, and if the second operand isn't equal constant,
2383 try to add the offsets into offset and ref_offset. */
2384 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2385 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2387 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2388 TREE_OPERAND (ref->base, 1)))
2390 offset_int off1 = mem_ref_offset (base);
2391 off1 <<= LOG2_BITS_PER_UNIT;
2392 off1 += offset;
2393 offset_int off2 = mem_ref_offset (ref->base);
2394 off2 <<= LOG2_BITS_PER_UNIT;
2395 off2 += ref_offset;
2396 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2398 offset = off1.to_shwi ();
2399 ref_offset = off2.to_shwi ();
2401 else
2402 size = -1;
2405 else
2406 size = -1;
2408 /* For a must-alias check we need to be able to constrain
2409 the access properly. */
2410 if (size != -1 && size == max_size)
2412 if (offset <= ref_offset
2413 && offset + size >= ref_offset + ref->max_size)
2414 return true;
2418 if (is_gimple_call (stmt))
2420 tree callee = gimple_call_fndecl (stmt);
2421 if (callee != NULL_TREE
2422 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2423 switch (DECL_FUNCTION_CODE (callee))
2425 case BUILT_IN_FREE:
2427 tree ptr = gimple_call_arg (stmt, 0);
2428 tree base = ao_ref_base (ref);
2429 if (base && TREE_CODE (base) == MEM_REF
2430 && TREE_OPERAND (base, 0) == ptr)
2431 return true;
2432 break;
2435 case BUILT_IN_MEMCPY:
2436 case BUILT_IN_MEMPCPY:
2437 case BUILT_IN_MEMMOVE:
2438 case BUILT_IN_MEMSET:
2439 case BUILT_IN_MEMCPY_CHK:
2440 case BUILT_IN_MEMPCPY_CHK:
2441 case BUILT_IN_MEMMOVE_CHK:
2442 case BUILT_IN_MEMSET_CHK:
2444 /* For a must-alias check we need to be able to constrain
2445 the access properly. */
2446 if (ref->max_size == -1)
2447 return false;
2448 tree dest = gimple_call_arg (stmt, 0);
2449 tree len = gimple_call_arg (stmt, 2);
2450 if (!tree_fits_shwi_p (len))
2451 return false;
2452 tree rbase = ref->base;
2453 offset_int roffset = ref->offset;
2454 ao_ref dref;
2455 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2456 tree base = ao_ref_base (&dref);
2457 offset_int offset = dref.offset;
2458 if (!base || dref.size == -1)
2459 return false;
2460 if (TREE_CODE (base) == MEM_REF)
2462 if (TREE_CODE (rbase) != MEM_REF)
2463 return false;
2464 // Compare pointers.
2465 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
2466 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
2467 base = TREE_OPERAND (base, 0);
2468 rbase = TREE_OPERAND (rbase, 0);
2470 if (base == rbase
2471 && offset <= roffset
2472 && (roffset + ref->max_size
2473 <= offset + (wi::to_offset (len) << LOG2_BITS_PER_UNIT)))
2474 return true;
2475 break;
2478 case BUILT_IN_VA_END:
2480 tree ptr = gimple_call_arg (stmt, 0);
2481 if (TREE_CODE (ptr) == ADDR_EXPR)
2483 tree base = ao_ref_base (ref);
2484 if (TREE_OPERAND (ptr, 0) == base)
2485 return true;
2487 break;
2490 default:;
2493 return false;
2496 bool
2497 stmt_kills_ref_p (gimple *stmt, tree ref)
2499 ao_ref r;
2500 ao_ref_init (&r, ref);
2501 return stmt_kills_ref_p (stmt, &r);
2505 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2506 TARGET or a statement clobbering the memory reference REF in which
2507 case false is returned. The walk starts with VUSE, one argument of PHI. */
2509 static bool
2510 maybe_skip_until (gimple *phi, tree target, ao_ref *ref,
2511 tree vuse, unsigned int *cnt, bitmap *visited,
2512 bool abort_on_visited,
2513 void *(*translate)(ao_ref *, tree, void *, bool *),
2514 void *data)
2516 basic_block bb = gimple_bb (phi);
2518 if (!*visited)
2519 *visited = BITMAP_ALLOC (NULL);
2521 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2523 /* Walk until we hit the target. */
2524 while (vuse != target)
2526 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2527 /* Recurse for PHI nodes. */
2528 if (gimple_code (def_stmt) == GIMPLE_PHI)
2530 /* An already visited PHI node ends the walk successfully. */
2531 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2532 return !abort_on_visited;
2533 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2534 visited, abort_on_visited,
2535 translate, data);
2536 if (!vuse)
2537 return false;
2538 continue;
2540 else if (gimple_nop_p (def_stmt))
2541 return false;
2542 else
2544 /* A clobbering statement or the end of the IL ends it failing. */
2545 ++*cnt;
2546 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2548 bool disambiguate_only = true;
2549 if (translate
2550 && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
2552 else
2553 return false;
2556 /* If we reach a new basic-block see if we already skipped it
2557 in a previous walk that ended successfully. */
2558 if (gimple_bb (def_stmt) != bb)
2560 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2561 return !abort_on_visited;
2562 bb = gimple_bb (def_stmt);
2564 vuse = gimple_vuse (def_stmt);
2566 return true;
2569 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2570 until we hit the phi argument definition that dominates the other one.
2571 Return that, or NULL_TREE if there is no such definition. */
2573 static tree
2574 get_continuation_for_phi_1 (gimple *phi, tree arg0, tree arg1,
2575 ao_ref *ref, unsigned int *cnt,
2576 bitmap *visited, bool abort_on_visited,
2577 void *(*translate)(ao_ref *, tree, void *, bool *),
2578 void *data)
2580 gimple *def0 = SSA_NAME_DEF_STMT (arg0);
2581 gimple *def1 = SSA_NAME_DEF_STMT (arg1);
2582 tree common_vuse;
2584 if (arg0 == arg1)
2585 return arg0;
2586 else if (gimple_nop_p (def0)
2587 || (!gimple_nop_p (def1)
2588 && dominated_by_p (CDI_DOMINATORS,
2589 gimple_bb (def1), gimple_bb (def0))))
2591 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2592 visited, abort_on_visited, translate, data))
2593 return arg0;
2595 else if (gimple_nop_p (def1)
2596 || dominated_by_p (CDI_DOMINATORS,
2597 gimple_bb (def0), gimple_bb (def1)))
2599 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2600 visited, abort_on_visited, translate, data))
2601 return arg1;
2603 /* Special case of a diamond:
2604 MEM_1 = ...
2605 goto (cond) ? L1 : L2
2606 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2607 goto L3
2608 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2609 L3: MEM_4 = PHI<MEM_2, MEM_3>
2610 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2611 dominate each other, but still we can easily skip this PHI node
2612 if we recognize that the vuse MEM operand is the same for both,
2613 and that we can skip both statements (they don't clobber us).
2614 This is still linear. Don't use maybe_skip_until, that might
2615 potentially be slow. */
2616 else if ((common_vuse = gimple_vuse (def0))
2617 && common_vuse == gimple_vuse (def1))
2619 bool disambiguate_only = true;
2620 *cnt += 2;
2621 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2622 || (translate
2623 && (*translate) (ref, arg0, data, &disambiguate_only) == NULL))
2624 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2625 || (translate
2626 && (*translate) (ref, arg1, data, &disambiguate_only) == NULL)))
2627 return common_vuse;
2630 return NULL_TREE;
2634 /* Starting from a PHI node for the virtual operand of the memory reference
2635 REF find a continuation virtual operand that allows to continue walking
2636 statements dominating PHI skipping only statements that cannot possibly
2637 clobber REF. Increments *CNT for each alias disambiguation done.
2638 Returns NULL_TREE if no suitable virtual operand can be found. */
2640 tree
2641 get_continuation_for_phi (gimple *phi, ao_ref *ref,
2642 unsigned int *cnt, bitmap *visited,
2643 bool abort_on_visited,
2644 void *(*translate)(ao_ref *, tree, void *, bool *),
2645 void *data)
2647 unsigned nargs = gimple_phi_num_args (phi);
2649 /* Through a single-argument PHI we can simply look through. */
2650 if (nargs == 1)
2651 return PHI_ARG_DEF (phi, 0);
2653 /* For two or more arguments try to pairwise skip non-aliasing code
2654 until we hit the phi argument definition that dominates the other one. */
2655 else if (nargs >= 2)
2657 tree arg0, arg1;
2658 unsigned i;
2660 /* Find a candidate for the virtual operand which definition
2661 dominates those of all others. */
2662 arg0 = PHI_ARG_DEF (phi, 0);
2663 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2664 for (i = 1; i < nargs; ++i)
2666 arg1 = PHI_ARG_DEF (phi, i);
2667 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2669 arg0 = arg1;
2670 break;
2672 if (dominated_by_p (CDI_DOMINATORS,
2673 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2674 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2675 arg0 = arg1;
2678 /* Then pairwise reduce against the found candidate. */
2679 for (i = 0; i < nargs; ++i)
2681 arg1 = PHI_ARG_DEF (phi, i);
2682 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2683 cnt, visited, abort_on_visited,
2684 translate, data);
2685 if (!arg0)
2686 return NULL_TREE;
2689 return arg0;
2692 return NULL_TREE;
2695 /* Based on the memory reference REF and its virtual use VUSE call
2696 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2697 itself. That is, for each virtual use for which its defining statement
2698 does not clobber REF.
2700 WALKER is called with REF, the current virtual use and DATA. If
2701 WALKER returns non-NULL the walk stops and its result is returned.
2702 At the end of a non-successful walk NULL is returned.
2704 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2705 use which definition is a statement that may clobber REF and DATA.
2706 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2707 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2708 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2709 to adjust REF and *DATA to make that valid.
2711 VALUEIZE if non-NULL is called with the next VUSE that is considered
2712 and return value is substituted for that. This can be used to
2713 implement optimistic value-numbering for example. Note that the
2714 VUSE argument is assumed to be valueized already.
2716 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2718 void *
2719 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2720 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2721 void *(*translate)(ao_ref *, tree, void *, bool *),
2722 tree (*valueize)(tree),
2723 void *data)
2725 bitmap visited = NULL;
2726 void *res;
2727 unsigned int cnt = 0;
2728 bool translated = false;
2730 timevar_push (TV_ALIAS_STMT_WALK);
2734 gimple *def_stmt;
2736 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2737 res = (*walker) (ref, vuse, cnt, data);
2738 /* Abort walk. */
2739 if (res == (void *)-1)
2741 res = NULL;
2742 break;
2744 /* Lookup succeeded. */
2745 else if (res != NULL)
2746 break;
2748 if (valueize)
2749 vuse = valueize (vuse);
2750 def_stmt = SSA_NAME_DEF_STMT (vuse);
2751 if (gimple_nop_p (def_stmt))
2752 break;
2753 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2754 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2755 &visited, translated, translate, data);
2756 else
2758 cnt++;
2759 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2761 if (!translate)
2762 break;
2763 bool disambiguate_only = false;
2764 res = (*translate) (ref, vuse, data, &disambiguate_only);
2765 /* Failed lookup and translation. */
2766 if (res == (void *)-1)
2768 res = NULL;
2769 break;
2771 /* Lookup succeeded. */
2772 else if (res != NULL)
2773 break;
2774 /* Translation succeeded, continue walking. */
2775 translated = translated || !disambiguate_only;
2777 vuse = gimple_vuse (def_stmt);
2780 while (vuse);
2782 if (visited)
2783 BITMAP_FREE (visited);
2785 timevar_pop (TV_ALIAS_STMT_WALK);
2787 return res;
2791 /* Based on the memory reference REF call WALKER for each vdef which
2792 defining statement may clobber REF, starting with VDEF. If REF
2793 is NULL_TREE, each defining statement is visited.
2795 WALKER is called with REF, the current vdef and DATA. If WALKER
2796 returns true the walk is stopped, otherwise it continues.
2798 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2799 The pointer may be NULL and then we do not track this information.
2801 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2802 PHI argument (but only one walk continues on merge points), the
2803 return value is true if any of the walks was successful.
2805 The function returns the number of statements walked. */
2807 static unsigned int
2808 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2809 bool (*walker)(ao_ref *, tree, void *), void *data,
2810 bitmap *visited, unsigned int cnt,
2811 bool *function_entry_reached)
2815 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
2817 if (*visited
2818 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2819 return cnt;
2821 if (gimple_nop_p (def_stmt))
2823 if (function_entry_reached)
2824 *function_entry_reached = true;
2825 return cnt;
2827 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2829 unsigned i;
2830 if (!*visited)
2831 *visited = BITMAP_ALLOC (NULL);
2832 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2833 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2834 walker, data, visited, 0,
2835 function_entry_reached);
2836 return cnt;
2839 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2840 cnt++;
2841 if ((!ref
2842 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2843 && (*walker) (ref, vdef, data))
2844 return cnt;
2846 vdef = gimple_vuse (def_stmt);
2848 while (1);
2851 unsigned int
2852 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2853 bool (*walker)(ao_ref *, tree, void *), void *data,
2854 bitmap *visited,
2855 bool *function_entry_reached)
2857 bitmap local_visited = NULL;
2858 unsigned int ret;
2860 timevar_push (TV_ALIAS_STMT_WALK);
2862 if (function_entry_reached)
2863 *function_entry_reached = false;
2865 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2866 visited ? visited : &local_visited, 0,
2867 function_entry_reached);
2868 if (local_visited)
2869 BITMAP_FREE (local_visited);
2871 timevar_pop (TV_ALIAS_STMT_WALK);
2873 return ret;