Add qdf24xx base tuning support.
[official-gcc.git] / gcc / tree-ssa-alias.c
blob70c24b59446313b7a001f0ca32ab6f1504d8181d
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 goto may_overlap;
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 goto may_overlap;
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 goto may_overlap;
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 goto may_overlap;
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 goto may_overlap;
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 goto may_overlap;
932 if (field1 != field2)
934 component_refs1.release ();
935 component_refs2.release ();
936 /* A field and its representative need to be considered the
937 same. */
938 if (DECL_BIT_FIELD_REPRESENTATIVE (field1) == field2
939 || DECL_BIT_FIELD_REPRESENTATIVE (field2) == field1)
940 return false;
941 /* Different fields of the same record type cannot overlap.
942 ??? Bitfields can overlap at RTL level so punt on them. */
943 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
944 return false;
945 return true;
949 may_overlap:
950 component_refs1.release ();
951 component_refs2.release ();
952 return false;
955 /* qsort compare function to sort FIELD_DECLs after their
956 DECL_FIELD_CONTEXT TYPE_UID. */
958 static inline int
959 ncr_compar (const void *field1_, const void *field2_)
961 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
962 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
963 unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
964 unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
965 if (uid1 < uid2)
966 return -1;
967 else if (uid1 > uid2)
968 return 1;
969 return 0;
972 /* Return true if we can determine that the fields referenced cannot
973 overlap for any pair of objects. */
975 static bool
976 nonoverlapping_component_refs_p (const_tree x, const_tree y)
978 if (!flag_strict_aliasing
979 || !x || !y
980 || TREE_CODE (x) != COMPONENT_REF
981 || TREE_CODE (y) != COMPONENT_REF)
982 return false;
984 auto_vec<const_tree, 16> fieldsx;
985 while (TREE_CODE (x) == COMPONENT_REF)
987 tree field = TREE_OPERAND (x, 1);
988 tree type = DECL_FIELD_CONTEXT (field);
989 if (TREE_CODE (type) == RECORD_TYPE)
990 fieldsx.safe_push (field);
991 x = TREE_OPERAND (x, 0);
993 if (fieldsx.length () == 0)
994 return false;
995 auto_vec<const_tree, 16> fieldsy;
996 while (TREE_CODE (y) == COMPONENT_REF)
998 tree field = TREE_OPERAND (y, 1);
999 tree type = DECL_FIELD_CONTEXT (field);
1000 if (TREE_CODE (type) == RECORD_TYPE)
1001 fieldsy.safe_push (TREE_OPERAND (y, 1));
1002 y = TREE_OPERAND (y, 0);
1004 if (fieldsy.length () == 0)
1005 return false;
1007 /* Most common case first. */
1008 if (fieldsx.length () == 1
1009 && fieldsy.length () == 1)
1010 return ((DECL_FIELD_CONTEXT (fieldsx[0])
1011 == DECL_FIELD_CONTEXT (fieldsy[0]))
1012 && fieldsx[0] != fieldsy[0]
1013 && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
1015 if (fieldsx.length () == 2)
1017 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1018 std::swap (fieldsx[0], fieldsx[1]);
1020 else
1021 fieldsx.qsort (ncr_compar);
1023 if (fieldsy.length () == 2)
1025 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1026 std::swap (fieldsy[0], fieldsy[1]);
1028 else
1029 fieldsy.qsort (ncr_compar);
1031 unsigned i = 0, j = 0;
1034 const_tree fieldx = fieldsx[i];
1035 const_tree fieldy = fieldsy[j];
1036 tree typex = DECL_FIELD_CONTEXT (fieldx);
1037 tree typey = DECL_FIELD_CONTEXT (fieldy);
1038 if (typex == typey)
1040 /* We're left with accessing different fields of a structure,
1041 no possible overlap. */
1042 if (fieldx != fieldy)
1044 /* A field and its representative need to be considered the
1045 same. */
1046 if (DECL_BIT_FIELD_REPRESENTATIVE (fieldx) == fieldy
1047 || DECL_BIT_FIELD_REPRESENTATIVE (fieldy) == fieldx)
1048 return false;
1049 /* Different fields of the same record type cannot overlap.
1050 ??? Bitfields can overlap at RTL level so punt on them. */
1051 if (DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy))
1052 return false;
1053 return true;
1056 if (TYPE_UID (typex) < TYPE_UID (typey))
1058 i++;
1059 if (i == fieldsx.length ())
1060 break;
1062 else
1064 j++;
1065 if (j == fieldsy.length ())
1066 break;
1069 while (1);
1071 return false;
1075 /* Return true if two memory references based on the variables BASE1
1076 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1077 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1078 if non-NULL are the complete memory reference trees. */
1080 static bool
1081 decl_refs_may_alias_p (tree ref1, tree base1,
1082 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1083 tree ref2, tree base2,
1084 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1086 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1088 /* If both references are based on different variables, they cannot alias. */
1089 if (compare_base_decls (base1, base2) == 0)
1090 return false;
1092 /* If both references are based on the same variable, they cannot alias if
1093 the accesses do not overlap. */
1094 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1095 return false;
1097 /* For components with variable position, the above test isn't sufficient,
1098 so we disambiguate component references manually. */
1099 if (ref1 && ref2
1100 && handled_component_p (ref1) && handled_component_p (ref2)
1101 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1102 return false;
1104 return true;
1107 /* Return true if an indirect reference based on *PTR1 constrained
1108 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1109 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1110 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1111 in which case they are computed on-demand. REF1 and REF2
1112 if non-NULL are the complete memory reference trees. */
1114 static bool
1115 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1116 HOST_WIDE_INT offset1,
1117 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1118 alias_set_type ref1_alias_set,
1119 alias_set_type base1_alias_set,
1120 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1121 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1122 alias_set_type ref2_alias_set,
1123 alias_set_type base2_alias_set, bool tbaa_p)
1125 tree ptr1;
1126 tree ptrtype1, dbase2;
1127 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1128 HOST_WIDE_INT doffset1, doffset2;
1130 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1131 || TREE_CODE (base1) == TARGET_MEM_REF)
1132 && DECL_P (base2));
1134 ptr1 = TREE_OPERAND (base1, 0);
1136 /* The offset embedded in MEM_REFs can be negative. Bias them
1137 so that the resulting offset adjustment is positive. */
1138 offset_int moff = mem_ref_offset (base1);
1139 moff <<= LOG2_BITS_PER_UNIT;
1140 if (wi::neg_p (moff))
1141 offset2p += (-moff).to_short_addr ();
1142 else
1143 offset1p += moff.to_short_addr ();
1145 /* If only one reference is based on a variable, they cannot alias if
1146 the pointer access is beyond the extent of the variable access.
1147 (the pointer base cannot validly point to an offset less than zero
1148 of the variable).
1149 ??? IVOPTs creates bases that do not honor this restriction,
1150 so do not apply this optimization for TARGET_MEM_REFs. */
1151 if (TREE_CODE (base1) != TARGET_MEM_REF
1152 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1153 return false;
1154 /* They also cannot alias if the pointer may not point to the decl. */
1155 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1156 return false;
1158 /* Disambiguations that rely on strict aliasing rules follow. */
1159 if (!flag_strict_aliasing || !tbaa_p)
1160 return true;
1162 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1164 /* If the alias set for a pointer access is zero all bets are off. */
1165 if (base1_alias_set == 0)
1166 return true;
1168 /* When we are trying to disambiguate an access with a pointer dereference
1169 as base versus one with a decl as base we can use both the size
1170 of the decl and its dynamic type for extra disambiguation.
1171 ??? We do not know anything about the dynamic type of the decl
1172 other than that its alias-set contains base2_alias_set as a subset
1173 which does not help us here. */
1174 /* As we know nothing useful about the dynamic type of the decl just
1175 use the usual conflict check rather than a subset test.
1176 ??? We could introduce -fvery-strict-aliasing when the language
1177 does not allow decls to have a dynamic type that differs from their
1178 static type. Then we can check
1179 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1180 if (base1_alias_set != base2_alias_set
1181 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1182 return false;
1183 /* If the size of the access relevant for TBAA through the pointer
1184 is bigger than the size of the decl we can't possibly access the
1185 decl via that pointer. */
1186 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1187 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1188 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1189 /* ??? This in turn may run afoul when a decl of type T which is
1190 a member of union type U is accessed through a pointer to
1191 type U and sizeof T is smaller than sizeof U. */
1192 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1193 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1194 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1195 return false;
1197 if (!ref2)
1198 return true;
1200 /* If the decl is accessed via a MEM_REF, reconstruct the base
1201 we can use for TBAA and an appropriately adjusted offset. */
1202 dbase2 = ref2;
1203 while (handled_component_p (dbase2))
1204 dbase2 = TREE_OPERAND (dbase2, 0);
1205 doffset1 = offset1;
1206 doffset2 = offset2;
1207 if (TREE_CODE (dbase2) == MEM_REF
1208 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1210 offset_int moff = mem_ref_offset (dbase2);
1211 moff <<= LOG2_BITS_PER_UNIT;
1212 if (wi::neg_p (moff))
1213 doffset1 -= (-moff).to_short_addr ();
1214 else
1215 doffset2 -= moff.to_short_addr ();
1218 /* If either reference is view-converted, give up now. */
1219 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1220 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1221 return true;
1223 /* If both references are through the same type, they do not alias
1224 if the accesses do not overlap. This does extra disambiguation
1225 for mixed/pointer accesses but requires strict aliasing.
1226 For MEM_REFs we require that the component-ref offset we computed
1227 is relative to the start of the type which we ensure by
1228 comparing rvalue and access type and disregarding the constant
1229 pointer offset. */
1230 if ((TREE_CODE (base1) != TARGET_MEM_REF
1231 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1232 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1233 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1235 if (ref1 && ref2
1236 && nonoverlapping_component_refs_p (ref1, ref2))
1237 return false;
1239 /* Do access-path based disambiguation. */
1240 if (ref1 && ref2
1241 && (handled_component_p (ref1) || handled_component_p (ref2)))
1242 return aliasing_component_refs_p (ref1,
1243 ref1_alias_set, base1_alias_set,
1244 offset1, max_size1,
1245 ref2,
1246 ref2_alias_set, base2_alias_set,
1247 offset2, max_size2, true);
1249 return true;
1252 /* Return true if two indirect references based on *PTR1
1253 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1254 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1255 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1256 in which case they are computed on-demand. REF1 and REF2
1257 if non-NULL are the complete memory reference trees. */
1259 static bool
1260 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1261 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1262 alias_set_type ref1_alias_set,
1263 alias_set_type base1_alias_set,
1264 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1265 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1266 alias_set_type ref2_alias_set,
1267 alias_set_type base2_alias_set, bool tbaa_p)
1269 tree ptr1;
1270 tree ptr2;
1271 tree ptrtype1, ptrtype2;
1273 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1274 || TREE_CODE (base1) == TARGET_MEM_REF)
1275 && (TREE_CODE (base2) == MEM_REF
1276 || TREE_CODE (base2) == TARGET_MEM_REF));
1278 ptr1 = TREE_OPERAND (base1, 0);
1279 ptr2 = TREE_OPERAND (base2, 0);
1281 /* If both bases are based on pointers they cannot alias if they may not
1282 point to the same memory object or if they point to the same object
1283 and the accesses do not overlap. */
1284 if ((!cfun || gimple_in_ssa_p (cfun))
1285 && operand_equal_p (ptr1, ptr2, 0)
1286 && (((TREE_CODE (base1) != TARGET_MEM_REF
1287 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1288 && (TREE_CODE (base2) != TARGET_MEM_REF
1289 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1290 || (TREE_CODE (base1) == TARGET_MEM_REF
1291 && TREE_CODE (base2) == TARGET_MEM_REF
1292 && (TMR_STEP (base1) == TMR_STEP (base2)
1293 || (TMR_STEP (base1) && TMR_STEP (base2)
1294 && operand_equal_p (TMR_STEP (base1),
1295 TMR_STEP (base2), 0)))
1296 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1297 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1298 && operand_equal_p (TMR_INDEX (base1),
1299 TMR_INDEX (base2), 0)))
1300 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1301 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1302 && operand_equal_p (TMR_INDEX2 (base1),
1303 TMR_INDEX2 (base2), 0))))))
1305 offset_int moff;
1306 /* The offset embedded in MEM_REFs can be negative. Bias them
1307 so that the resulting offset adjustment is positive. */
1308 moff = mem_ref_offset (base1);
1309 moff <<= LOG2_BITS_PER_UNIT;
1310 if (wi::neg_p (moff))
1311 offset2 += (-moff).to_short_addr ();
1312 else
1313 offset1 += moff.to_shwi ();
1314 moff = mem_ref_offset (base2);
1315 moff <<= LOG2_BITS_PER_UNIT;
1316 if (wi::neg_p (moff))
1317 offset1 += (-moff).to_short_addr ();
1318 else
1319 offset2 += moff.to_short_addr ();
1320 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1322 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1323 return false;
1325 /* Disambiguations that rely on strict aliasing rules follow. */
1326 if (!flag_strict_aliasing || !tbaa_p)
1327 return true;
1329 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1330 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1332 /* If the alias set for a pointer access is zero all bets are off. */
1333 if (base1_alias_set == 0
1334 || base2_alias_set == 0)
1335 return true;
1337 /* If both references are through the same type, they do not alias
1338 if the accesses do not overlap. This does extra disambiguation
1339 for mixed/pointer accesses but requires strict aliasing. */
1340 if ((TREE_CODE (base1) != TARGET_MEM_REF
1341 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1342 && (TREE_CODE (base2) != TARGET_MEM_REF
1343 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1344 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1345 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1346 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1347 TREE_TYPE (ptrtype2)) == 1)
1348 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1350 /* Do type-based disambiguation. */
1351 if (base1_alias_set != base2_alias_set
1352 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1353 return false;
1355 /* If either reference is view-converted, give up now. */
1356 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1357 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1358 return true;
1360 if (ref1 && ref2
1361 && nonoverlapping_component_refs_p (ref1, ref2))
1362 return false;
1364 /* Do access-path based disambiguation. */
1365 if (ref1 && ref2
1366 && (handled_component_p (ref1) || handled_component_p (ref2)))
1367 return aliasing_component_refs_p (ref1,
1368 ref1_alias_set, base1_alias_set,
1369 offset1, max_size1,
1370 ref2,
1371 ref2_alias_set, base2_alias_set,
1372 offset2, max_size2, false);
1374 return true;
1377 /* Return true, if the two memory references REF1 and REF2 may alias. */
1379 bool
1380 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1382 tree base1, base2;
1383 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1384 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1385 bool var1_p, var2_p, ind1_p, ind2_p;
1387 gcc_checking_assert ((!ref1->ref
1388 || TREE_CODE (ref1->ref) == SSA_NAME
1389 || DECL_P (ref1->ref)
1390 || TREE_CODE (ref1->ref) == STRING_CST
1391 || handled_component_p (ref1->ref)
1392 || TREE_CODE (ref1->ref) == MEM_REF
1393 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1394 && (!ref2->ref
1395 || TREE_CODE (ref2->ref) == SSA_NAME
1396 || DECL_P (ref2->ref)
1397 || TREE_CODE (ref2->ref) == STRING_CST
1398 || handled_component_p (ref2->ref)
1399 || TREE_CODE (ref2->ref) == MEM_REF
1400 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1402 /* Decompose the references into their base objects and the access. */
1403 base1 = ao_ref_base (ref1);
1404 offset1 = ref1->offset;
1405 max_size1 = ref1->max_size;
1406 base2 = ao_ref_base (ref2);
1407 offset2 = ref2->offset;
1408 max_size2 = ref2->max_size;
1410 /* We can end up with registers or constants as bases for example from
1411 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1412 which is seen as a struct copy. */
1413 if (TREE_CODE (base1) == SSA_NAME
1414 || TREE_CODE (base1) == CONST_DECL
1415 || TREE_CODE (base1) == CONSTRUCTOR
1416 || TREE_CODE (base1) == ADDR_EXPR
1417 || CONSTANT_CLASS_P (base1)
1418 || TREE_CODE (base2) == SSA_NAME
1419 || TREE_CODE (base2) == CONST_DECL
1420 || TREE_CODE (base2) == CONSTRUCTOR
1421 || TREE_CODE (base2) == ADDR_EXPR
1422 || CONSTANT_CLASS_P (base2))
1423 return false;
1425 /* We can end up referring to code via function and label decls.
1426 As we likely do not properly track code aliases conservatively
1427 bail out. */
1428 if (TREE_CODE (base1) == FUNCTION_DECL
1429 || TREE_CODE (base1) == LABEL_DECL
1430 || TREE_CODE (base2) == FUNCTION_DECL
1431 || TREE_CODE (base2) == LABEL_DECL)
1432 return true;
1434 /* Two volatile accesses always conflict. */
1435 if (ref1->volatile_p
1436 && ref2->volatile_p)
1437 return true;
1439 /* Defer to simple offset based disambiguation if we have
1440 references based on two decls. Do this before defering to
1441 TBAA to handle must-alias cases in conformance with the
1442 GCC extension of allowing type-punning through unions. */
1443 var1_p = DECL_P (base1);
1444 var2_p = DECL_P (base2);
1445 if (var1_p && var2_p)
1446 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1447 ref2->ref, base2, offset2, max_size2);
1449 /* Handle restrict based accesses.
1450 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1451 here. */
1452 tree rbase1 = base1;
1453 tree rbase2 = base2;
1454 if (var1_p)
1456 rbase1 = ref1->ref;
1457 if (rbase1)
1458 while (handled_component_p (rbase1))
1459 rbase1 = TREE_OPERAND (rbase1, 0);
1461 if (var2_p)
1463 rbase2 = ref2->ref;
1464 if (rbase2)
1465 while (handled_component_p (rbase2))
1466 rbase2 = TREE_OPERAND (rbase2, 0);
1468 if (rbase1 && rbase2
1469 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1470 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1471 /* If the accesses are in the same restrict clique... */
1472 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1473 /* But based on different pointers they do not alias. */
1474 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1475 return false;
1477 ind1_p = (TREE_CODE (base1) == MEM_REF
1478 || TREE_CODE (base1) == TARGET_MEM_REF);
1479 ind2_p = (TREE_CODE (base2) == MEM_REF
1480 || TREE_CODE (base2) == TARGET_MEM_REF);
1482 /* Canonicalize the pointer-vs-decl case. */
1483 if (ind1_p && var2_p)
1485 std::swap (offset1, offset2);
1486 std::swap (max_size1, max_size2);
1487 std::swap (base1, base2);
1488 std::swap (ref1, ref2);
1489 var1_p = true;
1490 ind1_p = false;
1491 var2_p = false;
1492 ind2_p = true;
1495 /* First defer to TBAA if possible. */
1496 if (tbaa_p
1497 && flag_strict_aliasing
1498 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1499 ao_ref_alias_set (ref2)))
1500 return false;
1502 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1503 if (var1_p && ind2_p)
1504 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1505 offset2, max_size2,
1506 ao_ref_alias_set (ref2),
1507 ao_ref_base_alias_set (ref2),
1508 ref1->ref, base1,
1509 offset1, max_size1,
1510 ao_ref_alias_set (ref1),
1511 ao_ref_base_alias_set (ref1),
1512 tbaa_p);
1513 else if (ind1_p && ind2_p)
1514 return indirect_refs_may_alias_p (ref1->ref, base1,
1515 offset1, max_size1,
1516 ao_ref_alias_set (ref1),
1517 ao_ref_base_alias_set (ref1),
1518 ref2->ref, base2,
1519 offset2, max_size2,
1520 ao_ref_alias_set (ref2),
1521 ao_ref_base_alias_set (ref2),
1522 tbaa_p);
1524 gcc_unreachable ();
1527 static bool
1528 refs_may_alias_p (tree ref1, ao_ref *ref2)
1530 ao_ref r1;
1531 ao_ref_init (&r1, ref1);
1532 return refs_may_alias_p_1 (&r1, ref2, true);
1535 bool
1536 refs_may_alias_p (tree ref1, tree ref2)
1538 ao_ref r1, r2;
1539 bool res;
1540 ao_ref_init (&r1, ref1);
1541 ao_ref_init (&r2, ref2);
1542 res = refs_may_alias_p_1 (&r1, &r2, true);
1543 if (res)
1544 ++alias_stats.refs_may_alias_p_may_alias;
1545 else
1546 ++alias_stats.refs_may_alias_p_no_alias;
1547 return res;
1550 /* Returns true if there is a anti-dependence for the STORE that
1551 executes after the LOAD. */
1553 bool
1554 refs_anti_dependent_p (tree load, tree store)
1556 ao_ref r1, r2;
1557 ao_ref_init (&r1, load);
1558 ao_ref_init (&r2, store);
1559 return refs_may_alias_p_1 (&r1, &r2, false);
1562 /* Returns true if there is a output dependence for the stores
1563 STORE1 and STORE2. */
1565 bool
1566 refs_output_dependent_p (tree store1, tree store2)
1568 ao_ref r1, r2;
1569 ao_ref_init (&r1, store1);
1570 ao_ref_init (&r2, store2);
1571 return refs_may_alias_p_1 (&r1, &r2, false);
1574 /* If the call CALL may use the memory reference REF return true,
1575 otherwise return false. */
1577 static bool
1578 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1580 tree base, callee;
1581 unsigned i;
1582 int flags = gimple_call_flags (call);
1584 /* Const functions without a static chain do not implicitly use memory. */
1585 if (!gimple_call_chain (call)
1586 && (flags & (ECF_CONST|ECF_NOVOPS)))
1587 goto process_args;
1589 base = ao_ref_base (ref);
1590 if (!base)
1591 return true;
1593 /* A call that is not without side-effects might involve volatile
1594 accesses and thus conflicts with all other volatile accesses. */
1595 if (ref->volatile_p)
1596 return true;
1598 /* If the reference is based on a decl that is not aliased the call
1599 cannot possibly use it. */
1600 if (DECL_P (base)
1601 && !may_be_aliased (base)
1602 /* But local statics can be used through recursion. */
1603 && !is_global_var (base))
1604 goto process_args;
1606 callee = gimple_call_fndecl (call);
1608 /* Handle those builtin functions explicitly that do not act as
1609 escape points. See tree-ssa-structalias.c:find_func_aliases
1610 for the list of builtins we might need to handle here. */
1611 if (callee != NULL_TREE
1612 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1613 switch (DECL_FUNCTION_CODE (callee))
1615 /* All the following functions read memory pointed to by
1616 their second argument. strcat/strncat additionally
1617 reads memory pointed to by the first argument. */
1618 case BUILT_IN_STRCAT:
1619 case BUILT_IN_STRNCAT:
1621 ao_ref dref;
1622 ao_ref_init_from_ptr_and_size (&dref,
1623 gimple_call_arg (call, 0),
1624 NULL_TREE);
1625 if (refs_may_alias_p_1 (&dref, ref, false))
1626 return true;
1628 /* FALLTHRU */
1629 case BUILT_IN_STRCPY:
1630 case BUILT_IN_STRNCPY:
1631 case BUILT_IN_MEMCPY:
1632 case BUILT_IN_MEMMOVE:
1633 case BUILT_IN_MEMPCPY:
1634 case BUILT_IN_STPCPY:
1635 case BUILT_IN_STPNCPY:
1636 case BUILT_IN_TM_MEMCPY:
1637 case BUILT_IN_TM_MEMMOVE:
1639 ao_ref dref;
1640 tree size = NULL_TREE;
1641 if (gimple_call_num_args (call) == 3)
1642 size = gimple_call_arg (call, 2);
1643 ao_ref_init_from_ptr_and_size (&dref,
1644 gimple_call_arg (call, 1),
1645 size);
1646 return refs_may_alias_p_1 (&dref, ref, false);
1648 case BUILT_IN_STRCAT_CHK:
1649 case BUILT_IN_STRNCAT_CHK:
1651 ao_ref dref;
1652 ao_ref_init_from_ptr_and_size (&dref,
1653 gimple_call_arg (call, 0),
1654 NULL_TREE);
1655 if (refs_may_alias_p_1 (&dref, ref, false))
1656 return true;
1658 /* FALLTHRU */
1659 case BUILT_IN_STRCPY_CHK:
1660 case BUILT_IN_STRNCPY_CHK:
1661 case BUILT_IN_MEMCPY_CHK:
1662 case BUILT_IN_MEMMOVE_CHK:
1663 case BUILT_IN_MEMPCPY_CHK:
1664 case BUILT_IN_STPCPY_CHK:
1665 case BUILT_IN_STPNCPY_CHK:
1667 ao_ref dref;
1668 tree size = NULL_TREE;
1669 if (gimple_call_num_args (call) == 4)
1670 size = gimple_call_arg (call, 2);
1671 ao_ref_init_from_ptr_and_size (&dref,
1672 gimple_call_arg (call, 1),
1673 size);
1674 return refs_may_alias_p_1 (&dref, ref, false);
1676 case BUILT_IN_BCOPY:
1678 ao_ref dref;
1679 tree size = gimple_call_arg (call, 2);
1680 ao_ref_init_from_ptr_and_size (&dref,
1681 gimple_call_arg (call, 0),
1682 size);
1683 return refs_may_alias_p_1 (&dref, ref, false);
1686 /* The following functions read memory pointed to by their
1687 first argument. */
1688 CASE_BUILT_IN_TM_LOAD (1):
1689 CASE_BUILT_IN_TM_LOAD (2):
1690 CASE_BUILT_IN_TM_LOAD (4):
1691 CASE_BUILT_IN_TM_LOAD (8):
1692 CASE_BUILT_IN_TM_LOAD (FLOAT):
1693 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1694 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1695 CASE_BUILT_IN_TM_LOAD (M64):
1696 CASE_BUILT_IN_TM_LOAD (M128):
1697 CASE_BUILT_IN_TM_LOAD (M256):
1698 case BUILT_IN_TM_LOG:
1699 case BUILT_IN_TM_LOG_1:
1700 case BUILT_IN_TM_LOG_2:
1701 case BUILT_IN_TM_LOG_4:
1702 case BUILT_IN_TM_LOG_8:
1703 case BUILT_IN_TM_LOG_FLOAT:
1704 case BUILT_IN_TM_LOG_DOUBLE:
1705 case BUILT_IN_TM_LOG_LDOUBLE:
1706 case BUILT_IN_TM_LOG_M64:
1707 case BUILT_IN_TM_LOG_M128:
1708 case BUILT_IN_TM_LOG_M256:
1709 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1711 /* These read memory pointed to by the first argument. */
1712 case BUILT_IN_STRDUP:
1713 case BUILT_IN_STRNDUP:
1714 case BUILT_IN_REALLOC:
1716 ao_ref dref;
1717 tree size = NULL_TREE;
1718 if (gimple_call_num_args (call) == 2)
1719 size = gimple_call_arg (call, 1);
1720 ao_ref_init_from_ptr_and_size (&dref,
1721 gimple_call_arg (call, 0),
1722 size);
1723 return refs_may_alias_p_1 (&dref, ref, false);
1725 /* These read memory pointed to by the first argument. */
1726 case BUILT_IN_INDEX:
1727 case BUILT_IN_STRCHR:
1728 case BUILT_IN_STRRCHR:
1730 ao_ref dref;
1731 ao_ref_init_from_ptr_and_size (&dref,
1732 gimple_call_arg (call, 0),
1733 NULL_TREE);
1734 return refs_may_alias_p_1 (&dref, ref, false);
1736 /* These read memory pointed to by the first argument with size
1737 in the third argument. */
1738 case BUILT_IN_MEMCHR:
1740 ao_ref dref;
1741 ao_ref_init_from_ptr_and_size (&dref,
1742 gimple_call_arg (call, 0),
1743 gimple_call_arg (call, 2));
1744 return refs_may_alias_p_1 (&dref, ref, false);
1746 /* These read memory pointed to by the first and second arguments. */
1747 case BUILT_IN_STRSTR:
1748 case BUILT_IN_STRPBRK:
1750 ao_ref dref;
1751 ao_ref_init_from_ptr_and_size (&dref,
1752 gimple_call_arg (call, 0),
1753 NULL_TREE);
1754 if (refs_may_alias_p_1 (&dref, ref, false))
1755 return true;
1756 ao_ref_init_from_ptr_and_size (&dref,
1757 gimple_call_arg (call, 1),
1758 NULL_TREE);
1759 return refs_may_alias_p_1 (&dref, ref, false);
1762 /* The following builtins do not read from memory. */
1763 case BUILT_IN_FREE:
1764 case BUILT_IN_MALLOC:
1765 case BUILT_IN_POSIX_MEMALIGN:
1766 case BUILT_IN_ALIGNED_ALLOC:
1767 case BUILT_IN_CALLOC:
1768 case BUILT_IN_ALLOCA:
1769 case BUILT_IN_ALLOCA_WITH_ALIGN:
1770 case BUILT_IN_STACK_SAVE:
1771 case BUILT_IN_STACK_RESTORE:
1772 case BUILT_IN_MEMSET:
1773 case BUILT_IN_TM_MEMSET:
1774 case BUILT_IN_MEMSET_CHK:
1775 case BUILT_IN_FREXP:
1776 case BUILT_IN_FREXPF:
1777 case BUILT_IN_FREXPL:
1778 case BUILT_IN_GAMMA_R:
1779 case BUILT_IN_GAMMAF_R:
1780 case BUILT_IN_GAMMAL_R:
1781 case BUILT_IN_LGAMMA_R:
1782 case BUILT_IN_LGAMMAF_R:
1783 case BUILT_IN_LGAMMAL_R:
1784 case BUILT_IN_MODF:
1785 case BUILT_IN_MODFF:
1786 case BUILT_IN_MODFL:
1787 case BUILT_IN_REMQUO:
1788 case BUILT_IN_REMQUOF:
1789 case BUILT_IN_REMQUOL:
1790 case BUILT_IN_SINCOS:
1791 case BUILT_IN_SINCOSF:
1792 case BUILT_IN_SINCOSL:
1793 case BUILT_IN_ASSUME_ALIGNED:
1794 case BUILT_IN_VA_END:
1795 return false;
1796 /* __sync_* builtins and some OpenMP builtins act as threading
1797 barriers. */
1798 #undef DEF_SYNC_BUILTIN
1799 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1800 #include "sync-builtins.def"
1801 #undef DEF_SYNC_BUILTIN
1802 case BUILT_IN_GOMP_ATOMIC_START:
1803 case BUILT_IN_GOMP_ATOMIC_END:
1804 case BUILT_IN_GOMP_BARRIER:
1805 case BUILT_IN_GOMP_BARRIER_CANCEL:
1806 case BUILT_IN_GOMP_TASKWAIT:
1807 case BUILT_IN_GOMP_TASKGROUP_END:
1808 case BUILT_IN_GOMP_CRITICAL_START:
1809 case BUILT_IN_GOMP_CRITICAL_END:
1810 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1811 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1812 case BUILT_IN_GOMP_LOOP_END:
1813 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1814 case BUILT_IN_GOMP_ORDERED_START:
1815 case BUILT_IN_GOMP_ORDERED_END:
1816 case BUILT_IN_GOMP_SECTIONS_END:
1817 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1818 case BUILT_IN_GOMP_SINGLE_COPY_START:
1819 case BUILT_IN_GOMP_SINGLE_COPY_END:
1820 return true;
1822 default:
1823 /* Fallthru to general call handling. */;
1826 /* Check if base is a global static variable that is not read
1827 by the function. */
1828 if (callee != NULL_TREE
1829 && TREE_CODE (base) == VAR_DECL
1830 && 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
2218 && TREE_CODE (base) == VAR_DECL
2219 && TREE_STATIC (base))
2221 struct cgraph_node *node = cgraph_node::get (callee);
2222 bitmap not_written;
2224 if (node
2225 && (not_written = ipa_reference_get_not_written_global (node))
2226 && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
2227 return false;
2230 /* Check if the base variable is call-clobbered. */
2231 if (DECL_P (base))
2232 return pt_solution_includes (gimple_call_clobber_set (call), base);
2233 else if ((TREE_CODE (base) == MEM_REF
2234 || TREE_CODE (base) == TARGET_MEM_REF)
2235 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2237 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2238 if (!pi)
2239 return true;
2241 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2244 return true;
2247 /* If the call in statement CALL may clobber the memory reference REF
2248 return true, otherwise return false. */
2250 bool
2251 call_may_clobber_ref_p (gcall *call, tree ref)
2253 bool res;
2254 ao_ref r;
2255 ao_ref_init (&r, ref);
2256 res = call_may_clobber_ref_p_1 (call, &r);
2257 if (res)
2258 ++alias_stats.call_may_clobber_ref_p_may_alias;
2259 else
2260 ++alias_stats.call_may_clobber_ref_p_no_alias;
2261 return res;
2265 /* If the statement STMT may clobber the memory reference REF return true,
2266 otherwise return false. */
2268 bool
2269 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref)
2271 if (is_gimple_call (stmt))
2273 tree lhs = gimple_call_lhs (stmt);
2274 if (lhs
2275 && TREE_CODE (lhs) != SSA_NAME)
2277 ao_ref r;
2278 ao_ref_init (&r, lhs);
2279 if (refs_may_alias_p_1 (ref, &r, true))
2280 return true;
2283 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2285 else if (gimple_assign_single_p (stmt))
2287 tree lhs = gimple_assign_lhs (stmt);
2288 if (TREE_CODE (lhs) != SSA_NAME)
2290 ao_ref r;
2291 ao_ref_init (&r, lhs);
2292 return refs_may_alias_p_1 (ref, &r, true);
2295 else if (gimple_code (stmt) == GIMPLE_ASM)
2296 return true;
2298 return false;
2301 bool
2302 stmt_may_clobber_ref_p (gimple *stmt, tree ref)
2304 ao_ref r;
2305 ao_ref_init (&r, ref);
2306 return stmt_may_clobber_ref_p_1 (stmt, &r);
2309 /* If STMT kills the memory reference REF return true, otherwise
2310 return false. */
2312 bool
2313 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2315 if (!ao_ref_base (ref))
2316 return false;
2318 if (gimple_has_lhs (stmt)
2319 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2320 /* The assignment is not necessarily carried out if it can throw
2321 and we can catch it in the current function where we could inspect
2322 the previous value.
2323 ??? We only need to care about the RHS throwing. For aggregate
2324 assignments or similar calls and non-call exceptions the LHS
2325 might throw as well. */
2326 && !stmt_can_throw_internal (stmt))
2328 tree lhs = gimple_get_lhs (stmt);
2329 /* If LHS is literally a base of the access we are done. */
2330 if (ref->ref)
2332 tree base = ref->ref;
2333 if (handled_component_p (base))
2335 tree saved_lhs0 = NULL_TREE;
2336 if (handled_component_p (lhs))
2338 saved_lhs0 = TREE_OPERAND (lhs, 0);
2339 TREE_OPERAND (lhs, 0) = integer_zero_node;
2343 /* Just compare the outermost handled component, if
2344 they are equal we have found a possible common
2345 base. */
2346 tree saved_base0 = TREE_OPERAND (base, 0);
2347 TREE_OPERAND (base, 0) = integer_zero_node;
2348 bool res = operand_equal_p (lhs, base, 0);
2349 TREE_OPERAND (base, 0) = saved_base0;
2350 if (res)
2351 break;
2352 /* Otherwise drop handled components of the access. */
2353 base = saved_base0;
2355 while (handled_component_p (base));
2356 if (saved_lhs0)
2357 TREE_OPERAND (lhs, 0) = saved_lhs0;
2359 /* Finally check if the lhs has the same address and size as the
2360 base candidate of the access. */
2361 if (lhs == base
2362 || (((TYPE_SIZE (TREE_TYPE (lhs))
2363 == TYPE_SIZE (TREE_TYPE (base)))
2364 || (TYPE_SIZE (TREE_TYPE (lhs))
2365 && TYPE_SIZE (TREE_TYPE (base))
2366 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2367 TYPE_SIZE (TREE_TYPE (base)), 0)))
2368 && operand_equal_p (lhs, base, OEP_ADDRESS_OF)))
2369 return true;
2372 /* Now look for non-literal equal bases with the restriction of
2373 handling constant offset and size. */
2374 /* For a must-alias check we need to be able to constrain
2375 the access properly. */
2376 if (ref->max_size == -1)
2377 return false;
2378 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2379 bool reverse;
2380 tree base
2381 = get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
2382 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2383 so base == ref->base does not always hold. */
2384 if (base != ref->base)
2386 /* If both base and ref->base are MEM_REFs, only compare the
2387 first operand, and if the second operand isn't equal constant,
2388 try to add the offsets into offset and ref_offset. */
2389 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2390 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2392 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2393 TREE_OPERAND (ref->base, 1)))
2395 offset_int off1 = mem_ref_offset (base);
2396 off1 <<= LOG2_BITS_PER_UNIT;
2397 off1 += offset;
2398 offset_int off2 = mem_ref_offset (ref->base);
2399 off2 <<= LOG2_BITS_PER_UNIT;
2400 off2 += ref_offset;
2401 if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2403 offset = off1.to_shwi ();
2404 ref_offset = off2.to_shwi ();
2406 else
2407 size = -1;
2410 else
2411 size = -1;
2413 /* For a must-alias check we need to be able to constrain
2414 the access properly. */
2415 if (size != -1 && size == max_size)
2417 if (offset <= ref_offset
2418 && offset + size >= ref_offset + ref->max_size)
2419 return true;
2423 if (is_gimple_call (stmt))
2425 tree callee = gimple_call_fndecl (stmt);
2426 if (callee != NULL_TREE
2427 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2428 switch (DECL_FUNCTION_CODE (callee))
2430 case BUILT_IN_FREE:
2432 tree ptr = gimple_call_arg (stmt, 0);
2433 tree base = ao_ref_base (ref);
2434 if (base && TREE_CODE (base) == MEM_REF
2435 && TREE_OPERAND (base, 0) == ptr)
2436 return true;
2437 break;
2440 case BUILT_IN_MEMCPY:
2441 case BUILT_IN_MEMPCPY:
2442 case BUILT_IN_MEMMOVE:
2443 case BUILT_IN_MEMSET:
2444 case BUILT_IN_MEMCPY_CHK:
2445 case BUILT_IN_MEMPCPY_CHK:
2446 case BUILT_IN_MEMMOVE_CHK:
2447 case BUILT_IN_MEMSET_CHK:
2449 /* For a must-alias check we need to be able to constrain
2450 the access properly. */
2451 if (ref->max_size == -1)
2452 return false;
2453 tree dest = gimple_call_arg (stmt, 0);
2454 tree len = gimple_call_arg (stmt, 2);
2455 if (!tree_fits_shwi_p (len))
2456 return false;
2457 tree rbase = ref->base;
2458 offset_int roffset = ref->offset;
2459 ao_ref dref;
2460 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2461 tree base = ao_ref_base (&dref);
2462 offset_int offset = dref.offset;
2463 if (!base || dref.size == -1)
2464 return false;
2465 if (TREE_CODE (base) == MEM_REF)
2467 if (TREE_CODE (rbase) != MEM_REF)
2468 return false;
2469 // Compare pointers.
2470 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
2471 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
2472 base = TREE_OPERAND (base, 0);
2473 rbase = TREE_OPERAND (rbase, 0);
2475 if (base == rbase
2476 && offset <= roffset
2477 && (roffset + ref->max_size
2478 <= offset + (wi::to_offset (len) << LOG2_BITS_PER_UNIT)))
2479 return true;
2480 break;
2483 case BUILT_IN_VA_END:
2485 tree ptr = gimple_call_arg (stmt, 0);
2486 if (TREE_CODE (ptr) == ADDR_EXPR)
2488 tree base = ao_ref_base (ref);
2489 if (TREE_OPERAND (ptr, 0) == base)
2490 return true;
2492 break;
2495 default:;
2498 return false;
2501 bool
2502 stmt_kills_ref_p (gimple *stmt, tree ref)
2504 ao_ref r;
2505 ao_ref_init (&r, ref);
2506 return stmt_kills_ref_p (stmt, &r);
2510 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2511 TARGET or a statement clobbering the memory reference REF in which
2512 case false is returned. The walk starts with VUSE, one argument of PHI. */
2514 static bool
2515 maybe_skip_until (gimple *phi, tree target, ao_ref *ref,
2516 tree vuse, unsigned int *cnt, bitmap *visited,
2517 bool abort_on_visited,
2518 void *(*translate)(ao_ref *, tree, void *, bool *),
2519 void *data)
2521 basic_block bb = gimple_bb (phi);
2523 if (!*visited)
2524 *visited = BITMAP_ALLOC (NULL);
2526 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2528 /* Walk until we hit the target. */
2529 while (vuse != target)
2531 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2532 /* Recurse for PHI nodes. */
2533 if (gimple_code (def_stmt) == GIMPLE_PHI)
2535 /* An already visited PHI node ends the walk successfully. */
2536 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2537 return !abort_on_visited;
2538 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2539 visited, abort_on_visited,
2540 translate, data);
2541 if (!vuse)
2542 return false;
2543 continue;
2545 else if (gimple_nop_p (def_stmt))
2546 return false;
2547 else
2549 /* A clobbering statement or the end of the IL ends it failing. */
2550 ++*cnt;
2551 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2553 bool disambiguate_only = true;
2554 if (translate
2555 && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
2557 else
2558 return false;
2561 /* If we reach a new basic-block see if we already skipped it
2562 in a previous walk that ended successfully. */
2563 if (gimple_bb (def_stmt) != bb)
2565 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2566 return !abort_on_visited;
2567 bb = gimple_bb (def_stmt);
2569 vuse = gimple_vuse (def_stmt);
2571 return true;
2574 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2575 until we hit the phi argument definition that dominates the other one.
2576 Return that, or NULL_TREE if there is no such definition. */
2578 static tree
2579 get_continuation_for_phi_1 (gimple *phi, tree arg0, tree arg1,
2580 ao_ref *ref, unsigned int *cnt,
2581 bitmap *visited, bool abort_on_visited,
2582 void *(*translate)(ao_ref *, tree, void *, bool *),
2583 void *data)
2585 gimple *def0 = SSA_NAME_DEF_STMT (arg0);
2586 gimple *def1 = SSA_NAME_DEF_STMT (arg1);
2587 tree common_vuse;
2589 if (arg0 == arg1)
2590 return arg0;
2591 else if (gimple_nop_p (def0)
2592 || (!gimple_nop_p (def1)
2593 && dominated_by_p (CDI_DOMINATORS,
2594 gimple_bb (def1), gimple_bb (def0))))
2596 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2597 visited, abort_on_visited, translate, data))
2598 return arg0;
2600 else if (gimple_nop_p (def1)
2601 || dominated_by_p (CDI_DOMINATORS,
2602 gimple_bb (def0), gimple_bb (def1)))
2604 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2605 visited, abort_on_visited, translate, data))
2606 return arg1;
2608 /* Special case of a diamond:
2609 MEM_1 = ...
2610 goto (cond) ? L1 : L2
2611 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2612 goto L3
2613 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2614 L3: MEM_4 = PHI<MEM_2, MEM_3>
2615 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2616 dominate each other, but still we can easily skip this PHI node
2617 if we recognize that the vuse MEM operand is the same for both,
2618 and that we can skip both statements (they don't clobber us).
2619 This is still linear. Don't use maybe_skip_until, that might
2620 potentially be slow. */
2621 else if ((common_vuse = gimple_vuse (def0))
2622 && common_vuse == gimple_vuse (def1))
2624 bool disambiguate_only = true;
2625 *cnt += 2;
2626 if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2627 || (translate
2628 && (*translate) (ref, arg0, data, &disambiguate_only) == NULL))
2629 && (!stmt_may_clobber_ref_p_1 (def1, ref)
2630 || (translate
2631 && (*translate) (ref, arg1, data, &disambiguate_only) == NULL)))
2632 return common_vuse;
2635 return NULL_TREE;
2639 /* Starting from a PHI node for the virtual operand of the memory reference
2640 REF find a continuation virtual operand that allows to continue walking
2641 statements dominating PHI skipping only statements that cannot possibly
2642 clobber REF. Increments *CNT for each alias disambiguation done.
2643 Returns NULL_TREE if no suitable virtual operand can be found. */
2645 tree
2646 get_continuation_for_phi (gimple *phi, ao_ref *ref,
2647 unsigned int *cnt, bitmap *visited,
2648 bool abort_on_visited,
2649 void *(*translate)(ao_ref *, tree, void *, bool *),
2650 void *data)
2652 unsigned nargs = gimple_phi_num_args (phi);
2654 /* Through a single-argument PHI we can simply look through. */
2655 if (nargs == 1)
2656 return PHI_ARG_DEF (phi, 0);
2658 /* For two or more arguments try to pairwise skip non-aliasing code
2659 until we hit the phi argument definition that dominates the other one. */
2660 else if (nargs >= 2)
2662 tree arg0, arg1;
2663 unsigned i;
2665 /* Find a candidate for the virtual operand which definition
2666 dominates those of all others. */
2667 arg0 = PHI_ARG_DEF (phi, 0);
2668 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2669 for (i = 1; i < nargs; ++i)
2671 arg1 = PHI_ARG_DEF (phi, i);
2672 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2674 arg0 = arg1;
2675 break;
2677 if (dominated_by_p (CDI_DOMINATORS,
2678 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2679 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2680 arg0 = arg1;
2683 /* Then pairwise reduce against the found candidate. */
2684 for (i = 0; i < nargs; ++i)
2686 arg1 = PHI_ARG_DEF (phi, i);
2687 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2688 cnt, visited, abort_on_visited,
2689 translate, data);
2690 if (!arg0)
2691 return NULL_TREE;
2694 return arg0;
2697 return NULL_TREE;
2700 /* Based on the memory reference REF and its virtual use VUSE call
2701 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2702 itself. That is, for each virtual use for which its defining statement
2703 does not clobber REF.
2705 WALKER is called with REF, the current virtual use and DATA. If
2706 WALKER returns non-NULL the walk stops and its result is returned.
2707 At the end of a non-successful walk NULL is returned.
2709 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2710 use which definition is a statement that may clobber REF and DATA.
2711 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2712 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2713 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2714 to adjust REF and *DATA to make that valid.
2716 VALUEIZE if non-NULL is called with the next VUSE that is considered
2717 and return value is substituted for that. This can be used to
2718 implement optimistic value-numbering for example. Note that the
2719 VUSE argument is assumed to be valueized already.
2721 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2723 void *
2724 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2725 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2726 void *(*translate)(ao_ref *, tree, void *, bool *),
2727 tree (*valueize)(tree),
2728 void *data)
2730 bitmap visited = NULL;
2731 void *res;
2732 unsigned int cnt = 0;
2733 bool translated = false;
2735 timevar_push (TV_ALIAS_STMT_WALK);
2739 gimple *def_stmt;
2741 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2742 res = (*walker) (ref, vuse, cnt, data);
2743 /* Abort walk. */
2744 if (res == (void *)-1)
2746 res = NULL;
2747 break;
2749 /* Lookup succeeded. */
2750 else if (res != NULL)
2751 break;
2753 if (valueize)
2754 vuse = valueize (vuse);
2755 def_stmt = SSA_NAME_DEF_STMT (vuse);
2756 if (gimple_nop_p (def_stmt))
2757 break;
2758 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2759 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2760 &visited, translated, translate, data);
2761 else
2763 cnt++;
2764 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2766 if (!translate)
2767 break;
2768 bool disambiguate_only = false;
2769 res = (*translate) (ref, vuse, data, &disambiguate_only);
2770 /* Failed lookup and translation. */
2771 if (res == (void *)-1)
2773 res = NULL;
2774 break;
2776 /* Lookup succeeded. */
2777 else if (res != NULL)
2778 break;
2779 /* Translation succeeded, continue walking. */
2780 translated = translated || !disambiguate_only;
2782 vuse = gimple_vuse (def_stmt);
2785 while (vuse);
2787 if (visited)
2788 BITMAP_FREE (visited);
2790 timevar_pop (TV_ALIAS_STMT_WALK);
2792 return res;
2796 /* Based on the memory reference REF call WALKER for each vdef which
2797 defining statement may clobber REF, starting with VDEF. If REF
2798 is NULL_TREE, each defining statement is visited.
2800 WALKER is called with REF, the current vdef and DATA. If WALKER
2801 returns true the walk is stopped, otherwise it continues.
2803 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2804 The pointer may be NULL and then we do not track this information.
2806 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2807 PHI argument (but only one walk continues on merge points), the
2808 return value is true if any of the walks was successful.
2810 The function returns the number of statements walked. */
2812 static unsigned int
2813 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2814 bool (*walker)(ao_ref *, tree, void *), void *data,
2815 bitmap *visited, unsigned int cnt,
2816 bool *function_entry_reached)
2820 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
2822 if (*visited
2823 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2824 return cnt;
2826 if (gimple_nop_p (def_stmt))
2828 if (function_entry_reached)
2829 *function_entry_reached = true;
2830 return cnt;
2832 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2834 unsigned i;
2835 if (!*visited)
2836 *visited = BITMAP_ALLOC (NULL);
2837 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2838 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2839 walker, data, visited, 0,
2840 function_entry_reached);
2841 return cnt;
2844 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2845 cnt++;
2846 if ((!ref
2847 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2848 && (*walker) (ref, vdef, data))
2849 return cnt;
2851 vdef = gimple_vuse (def_stmt);
2853 while (1);
2856 unsigned int
2857 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2858 bool (*walker)(ao_ref *, tree, void *), void *data,
2859 bitmap *visited,
2860 bool *function_entry_reached)
2862 bitmap local_visited = NULL;
2863 unsigned int ret;
2865 timevar_push (TV_ALIAS_STMT_WALK);
2867 if (function_entry_reached)
2868 *function_entry_reached = false;
2870 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2871 visited ? visited : &local_visited, 0,
2872 function_entry_reached);
2873 if (local_visited)
2874 BITMAP_FREE (local_visited);
2876 timevar_pop (TV_ALIAS_STMT_WALK);
2878 return ret;