* gcc.dg/atomic-compare-exchange-1.c,
[official-gcc.git] / gcc / tree-ssa-alias.c
blob278599591e6be3adc0a74c83f098d5fdcbdf46ec
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "target.h"
28 #include "basic-block.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "tree-pretty-print.h"
35 #include "dumpfile.h"
36 #include "gimple.h"
37 #include "gimple-ssa.h"
38 #include "tree-ssanames.h"
39 #include "tree-dfa.h"
40 #include "tree-inline.h"
41 #include "params.h"
42 #include "vec.h"
43 #include "pointer-set.h"
44 #include "alloc-pool.h"
45 #include "tree-ssa-alias.h"
46 #include "ipa-reference.h"
48 /* Broad overview of how alias analysis on gimple works:
50 Statements clobbering or using memory are linked through the
51 virtual operand factored use-def chain. The virtual operand
52 is unique per function, its symbol is accessible via gimple_vop (cfun).
53 Virtual operands are used for efficiently walking memory statements
54 in the gimple IL and are useful for things like value-numbering as
55 a generation count for memory references.
57 SSA_NAME pointers may have associated points-to information
58 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
59 points-to information is (re-)computed by the TODO_rebuild_alias
60 pass manager todo. Points-to information is also used for more
61 precise tracking of call-clobbered and call-used variables and
62 related disambiguations.
64 This file contains functions for disambiguating memory references,
65 the so called alias-oracle and tools for walking of the gimple IL.
67 The main alias-oracle entry-points are
69 bool stmt_may_clobber_ref_p (gimple, tree)
71 This function queries if a statement may invalidate (parts of)
72 the memory designated by the reference tree argument.
74 bool ref_maybe_used_by_stmt_p (gimple, tree)
76 This function queries if a statement may need (parts of) the
77 memory designated by the reference tree argument.
79 There are variants of these functions that only handle the call
80 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
81 Note that these do not disambiguate against a possible call lhs.
83 bool refs_may_alias_p (tree, tree)
85 This function tries to disambiguate two reference trees.
87 bool ptr_deref_may_alias_global_p (tree)
89 This function queries if dereferencing a pointer variable may
90 alias global memory.
92 More low-level disambiguators are available and documented in
93 this file. Low-level disambiguators dealing with points-to
94 information are in tree-ssa-structalias.c. */
97 /* Query statistics for the different low-level disambiguators.
98 A high-level query may trigger multiple of them. */
100 static struct {
101 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
102 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
103 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
104 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
105 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
106 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
107 } alias_stats;
109 void
110 dump_alias_stats (FILE *s)
112 fprintf (s, "\nAlias oracle query stats:\n");
113 fprintf (s, " refs_may_alias_p: "
114 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
115 HOST_WIDE_INT_PRINT_DEC" queries\n",
116 alias_stats.refs_may_alias_p_no_alias,
117 alias_stats.refs_may_alias_p_no_alias
118 + alias_stats.refs_may_alias_p_may_alias);
119 fprintf (s, " ref_maybe_used_by_call_p: "
120 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
121 HOST_WIDE_INT_PRINT_DEC" queries\n",
122 alias_stats.ref_maybe_used_by_call_p_no_alias,
123 alias_stats.refs_may_alias_p_no_alias
124 + alias_stats.ref_maybe_used_by_call_p_may_alias);
125 fprintf (s, " call_may_clobber_ref_p: "
126 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
127 HOST_WIDE_INT_PRINT_DEC" queries\n",
128 alias_stats.call_may_clobber_ref_p_no_alias,
129 alias_stats.call_may_clobber_ref_p_no_alias
130 + alias_stats.call_may_clobber_ref_p_may_alias);
134 /* Return true, if dereferencing PTR may alias with a global variable. */
136 bool
137 ptr_deref_may_alias_global_p (tree ptr)
139 struct ptr_info_def *pi;
141 /* If we end up with a pointer constant here that may point
142 to global memory. */
143 if (TREE_CODE (ptr) != SSA_NAME)
144 return true;
146 pi = SSA_NAME_PTR_INFO (ptr);
148 /* If we do not have points-to information for this variable,
149 we have to punt. */
150 if (!pi)
151 return true;
153 /* ??? This does not use TBAA to prune globals ptr may not access. */
154 return pt_solution_includes_global (&pi->pt);
157 /* Return true if dereferencing PTR may alias DECL.
158 The caller is responsible for applying TBAA to see if PTR
159 may access DECL at all. */
161 static bool
162 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
164 struct ptr_info_def *pi;
166 /* Conversions are irrelevant for points-to information and
167 data-dependence analysis can feed us those. */
168 STRIP_NOPS (ptr);
170 /* Anything we do not explicilty handle aliases. */
171 if ((TREE_CODE (ptr) != SSA_NAME
172 && TREE_CODE (ptr) != ADDR_EXPR
173 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
174 || !POINTER_TYPE_P (TREE_TYPE (ptr))
175 || (TREE_CODE (decl) != VAR_DECL
176 && TREE_CODE (decl) != PARM_DECL
177 && TREE_CODE (decl) != RESULT_DECL))
178 return true;
180 /* Disregard pointer offsetting. */
181 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
185 ptr = TREE_OPERAND (ptr, 0);
187 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
188 return ptr_deref_may_alias_decl_p (ptr, decl);
191 /* ADDR_EXPR pointers either just offset another pointer or directly
192 specify the pointed-to set. */
193 if (TREE_CODE (ptr) == ADDR_EXPR)
195 tree base = get_base_address (TREE_OPERAND (ptr, 0));
196 if (base
197 && (TREE_CODE (base) == MEM_REF
198 || TREE_CODE (base) == TARGET_MEM_REF))
199 ptr = TREE_OPERAND (base, 0);
200 else if (base
201 && DECL_P (base))
202 return base == decl;
203 else if (base
204 && CONSTANT_CLASS_P (base))
205 return false;
206 else
207 return true;
210 /* Non-aliased variables can not be pointed to. */
211 if (!may_be_aliased (decl))
212 return false;
214 /* If we do not have useful points-to information for this pointer
215 we cannot disambiguate anything else. */
216 pi = SSA_NAME_PTR_INFO (ptr);
217 if (!pi)
218 return true;
220 return pt_solution_includes (&pi->pt, decl);
223 /* Return true if dereferenced PTR1 and PTR2 may alias.
224 The caller is responsible for applying TBAA to see if accesses
225 through PTR1 and PTR2 may conflict at all. */
227 bool
228 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
230 struct ptr_info_def *pi1, *pi2;
232 /* Conversions are irrelevant for points-to information and
233 data-dependence analysis can feed us those. */
234 STRIP_NOPS (ptr1);
235 STRIP_NOPS (ptr2);
237 /* Disregard pointer offsetting. */
238 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
242 ptr1 = TREE_OPERAND (ptr1, 0);
244 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
245 return ptr_derefs_may_alias_p (ptr1, ptr2);
247 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
251 ptr2 = TREE_OPERAND (ptr2, 0);
253 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
254 return ptr_derefs_may_alias_p (ptr1, ptr2);
257 /* ADDR_EXPR pointers either just offset another pointer or directly
258 specify the pointed-to set. */
259 if (TREE_CODE (ptr1) == ADDR_EXPR)
261 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
262 if (base
263 && (TREE_CODE (base) == MEM_REF
264 || TREE_CODE (base) == TARGET_MEM_REF))
265 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
266 else if (base
267 && DECL_P (base))
268 return ptr_deref_may_alias_decl_p (ptr2, base);
269 else
270 return true;
272 if (TREE_CODE (ptr2) == ADDR_EXPR)
274 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
275 if (base
276 && (TREE_CODE (base) == MEM_REF
277 || TREE_CODE (base) == TARGET_MEM_REF))
278 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
279 else if (base
280 && DECL_P (base))
281 return ptr_deref_may_alias_decl_p (ptr1, base);
282 else
283 return true;
286 /* From here we require SSA name pointers. Anything else aliases. */
287 if (TREE_CODE (ptr1) != SSA_NAME
288 || TREE_CODE (ptr2) != SSA_NAME
289 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
290 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
291 return true;
293 /* We may end up with two empty points-to solutions for two same pointers.
294 In this case we still want to say both pointers alias, so shortcut
295 that here. */
296 if (ptr1 == ptr2)
297 return true;
299 /* If we do not have useful points-to information for either pointer
300 we cannot disambiguate anything else. */
301 pi1 = SSA_NAME_PTR_INFO (ptr1);
302 pi2 = SSA_NAME_PTR_INFO (ptr2);
303 if (!pi1 || !pi2)
304 return true;
306 /* ??? This does not use TBAA to prune decls from the intersection
307 that not both pointers may access. */
308 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
311 /* Return true if dereferencing PTR may alias *REF.
312 The caller is responsible for applying TBAA to see if PTR
313 may access *REF at all. */
315 static bool
316 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
318 tree base = ao_ref_base (ref);
320 if (TREE_CODE (base) == MEM_REF
321 || TREE_CODE (base) == TARGET_MEM_REF)
322 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
323 else if (DECL_P (base))
324 return ptr_deref_may_alias_decl_p (ptr, base);
326 return true;
329 /* Return true whether REF may refer to global memory. */
331 bool
332 ref_may_alias_global_p (tree ref)
334 tree base = get_base_address (ref);
335 if (DECL_P (base))
336 return is_global_var (base);
337 else if (TREE_CODE (base) == MEM_REF
338 || TREE_CODE (base) == TARGET_MEM_REF)
339 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
340 return true;
343 /* Return true whether STMT may clobber global memory. */
345 bool
346 stmt_may_clobber_global_p (gimple stmt)
348 tree lhs;
350 if (!gimple_vdef (stmt))
351 return false;
353 /* ??? We can ask the oracle whether an artificial pointer
354 dereference with a pointer with points-to information covering
355 all global memory (what about non-address taken memory?) maybe
356 clobbered by this call. As there is at the moment no convenient
357 way of doing that without generating garbage do some manual
358 checking instead.
359 ??? We could make a NULL ao_ref argument to the various
360 predicates special, meaning any global memory. */
362 switch (gimple_code (stmt))
364 case GIMPLE_ASSIGN:
365 lhs = gimple_assign_lhs (stmt);
366 return (TREE_CODE (lhs) != SSA_NAME
367 && ref_may_alias_global_p (lhs));
368 case GIMPLE_CALL:
369 return true;
370 default:
371 return true;
376 /* Dump alias information on FILE. */
378 void
379 dump_alias_info (FILE *file)
381 unsigned i;
382 const char *funcname
383 = lang_hooks.decl_printable_name (current_function_decl, 2);
384 tree var;
386 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
388 fprintf (file, "Aliased symbols\n\n");
390 FOR_EACH_LOCAL_DECL (cfun, i, var)
392 if (may_be_aliased (var))
393 dump_variable (file, var);
396 fprintf (file, "\nCall clobber information\n");
398 fprintf (file, "\nESCAPED");
399 dump_points_to_solution (file, &cfun->gimple_df->escaped);
401 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
403 for (i = 1; i < num_ssa_names; i++)
405 tree ptr = ssa_name (i);
406 struct ptr_info_def *pi;
408 if (ptr == NULL_TREE
409 || !POINTER_TYPE_P (TREE_TYPE (ptr))
410 || SSA_NAME_IN_FREE_LIST (ptr))
411 continue;
413 pi = SSA_NAME_PTR_INFO (ptr);
414 if (pi)
415 dump_points_to_info_for (file, ptr);
418 fprintf (file, "\n");
422 /* Dump alias information on stderr. */
424 DEBUG_FUNCTION void
425 debug_alias_info (void)
427 dump_alias_info (stderr);
431 /* Dump the points-to set *PT into FILE. */
433 void
434 dump_points_to_solution (FILE *file, struct pt_solution *pt)
436 if (pt->anything)
437 fprintf (file, ", points-to anything");
439 if (pt->nonlocal)
440 fprintf (file, ", points-to non-local");
442 if (pt->escaped)
443 fprintf (file, ", points-to escaped");
445 if (pt->ipa_escaped)
446 fprintf (file, ", points-to unit escaped");
448 if (pt->null)
449 fprintf (file, ", points-to NULL");
451 if (pt->vars)
453 fprintf (file, ", points-to vars: ");
454 dump_decl_set (file, pt->vars);
455 if (pt->vars_contains_global)
456 fprintf (file, " (includes global vars)");
461 /* Unified dump function for pt_solution. */
463 DEBUG_FUNCTION void
464 debug (pt_solution &ref)
466 dump_points_to_solution (stderr, &ref);
469 DEBUG_FUNCTION void
470 debug (pt_solution *ptr)
472 if (ptr)
473 debug (*ptr);
474 else
475 fprintf (stderr, "<nil>\n");
479 /* Dump points-to information for SSA_NAME PTR into FILE. */
481 void
482 dump_points_to_info_for (FILE *file, tree ptr)
484 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
486 print_generic_expr (file, ptr, dump_flags);
488 if (pi)
489 dump_points_to_solution (file, &pi->pt);
490 else
491 fprintf (file, ", points-to anything");
493 fprintf (file, "\n");
497 /* Dump points-to information for VAR into stderr. */
499 DEBUG_FUNCTION void
500 debug_points_to_info_for (tree var)
502 dump_points_to_info_for (stderr, var);
506 /* Initializes the alias-oracle reference representation *R from REF. */
508 void
509 ao_ref_init (ao_ref *r, tree ref)
511 r->ref = ref;
512 r->base = NULL_TREE;
513 r->offset = 0;
514 r->size = -1;
515 r->max_size = -1;
516 r->ref_alias_set = -1;
517 r->base_alias_set = -1;
518 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
521 /* Returns the base object of the memory reference *REF. */
523 tree
524 ao_ref_base (ao_ref *ref)
526 if (ref->base)
527 return ref->base;
528 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
529 &ref->max_size);
530 return ref->base;
533 /* Returns the base object alias set of the memory reference *REF. */
535 static alias_set_type
536 ao_ref_base_alias_set (ao_ref *ref)
538 tree base_ref;
539 if (ref->base_alias_set != -1)
540 return ref->base_alias_set;
541 if (!ref->ref)
542 return 0;
543 base_ref = ref->ref;
544 while (handled_component_p (base_ref))
545 base_ref = TREE_OPERAND (base_ref, 0);
546 ref->base_alias_set = get_alias_set (base_ref);
547 return ref->base_alias_set;
550 /* Returns the reference alias set of the memory reference *REF. */
552 alias_set_type
553 ao_ref_alias_set (ao_ref *ref)
555 if (ref->ref_alias_set != -1)
556 return ref->ref_alias_set;
557 ref->ref_alias_set = get_alias_set (ref->ref);
558 return ref->ref_alias_set;
561 /* Init an alias-oracle reference representation from a gimple pointer
562 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
563 size is assumed to be unknown. The access is assumed to be only
564 to or after of the pointer target, not before it. */
566 void
567 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
569 HOST_WIDE_INT t, extra_offset = 0;
570 ref->ref = NULL_TREE;
571 if (TREE_CODE (ptr) == SSA_NAME)
573 gimple stmt = SSA_NAME_DEF_STMT (ptr);
574 if (gimple_assign_single_p (stmt)
575 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
576 ptr = gimple_assign_rhs1 (stmt);
577 else if (is_gimple_assign (stmt)
578 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
579 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
581 ptr = gimple_assign_rhs1 (stmt);
582 extra_offset = BITS_PER_UNIT
583 * int_cst_value (gimple_assign_rhs2 (stmt));
587 if (TREE_CODE (ptr) == ADDR_EXPR)
589 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
590 if (ref->base)
591 ref->offset = BITS_PER_UNIT * t;
592 else
594 size = NULL_TREE;
595 ref->offset = 0;
596 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
599 else
601 ref->base = build2 (MEM_REF, char_type_node,
602 ptr, null_pointer_node);
603 ref->offset = 0;
605 ref->offset += extra_offset;
606 if (size
607 && host_integerp (size, 0)
608 && TREE_INT_CST_LOW (size) * BITS_PER_UNIT / BITS_PER_UNIT
609 == TREE_INT_CST_LOW (size))
610 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * BITS_PER_UNIT;
611 else
612 ref->max_size = ref->size = -1;
613 ref->ref_alias_set = 0;
614 ref->base_alias_set = 0;
615 ref->volatile_p = false;
618 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
619 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
620 decide. */
622 static inline int
623 same_type_for_tbaa (tree type1, tree type2)
625 type1 = TYPE_MAIN_VARIANT (type1);
626 type2 = TYPE_MAIN_VARIANT (type2);
628 /* If we would have to do structural comparison bail out. */
629 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
630 || TYPE_STRUCTURAL_EQUALITY_P (type2))
631 return -1;
633 /* Compare the canonical types. */
634 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
635 return 1;
637 /* ??? Array types are not properly unified in all cases as we have
638 spurious changes in the index types for example. Removing this
639 causes all sorts of problems with the Fortran frontend. */
640 if (TREE_CODE (type1) == ARRAY_TYPE
641 && TREE_CODE (type2) == ARRAY_TYPE)
642 return -1;
644 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
645 object of one of its constrained subtypes, e.g. when a function with an
646 unconstrained parameter passed by reference is called on an object and
647 inlined. But, even in the case of a fixed size, type and subtypes are
648 not equivalent enough as to share the same TYPE_CANONICAL, since this
649 would mean that conversions between them are useless, whereas they are
650 not (e.g. type and subtypes can have different modes). So, in the end,
651 they are only guaranteed to have the same alias set. */
652 if (get_alias_set (type1) == get_alias_set (type2))
653 return -1;
655 /* The types are known to be not equal. */
656 return 0;
659 /* Determine if the two component references REF1 and REF2 which are
660 based on access types TYPE1 and TYPE2 and of which at least one is based
661 on an indirect reference may alias. REF2 is the only one that can
662 be a decl in which case REF2_IS_DECL is true.
663 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
664 are the respective alias sets. */
666 static bool
667 aliasing_component_refs_p (tree ref1,
668 alias_set_type ref1_alias_set,
669 alias_set_type base1_alias_set,
670 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
671 tree ref2,
672 alias_set_type ref2_alias_set,
673 alias_set_type base2_alias_set,
674 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
675 bool ref2_is_decl)
677 /* If one reference is a component references through pointers try to find a
678 common base and apply offset based disambiguation. This handles
679 for example
680 struct A { int i; int j; } *q;
681 struct B { struct A a; int k; } *p;
682 disambiguating q->i and p->a.j. */
683 tree base1, base2;
684 tree type1, type2;
685 tree *refp;
686 int same_p;
688 /* Choose bases and base types to search for. */
689 base1 = ref1;
690 while (handled_component_p (base1))
691 base1 = TREE_OPERAND (base1, 0);
692 type1 = TREE_TYPE (base1);
693 base2 = ref2;
694 while (handled_component_p (base2))
695 base2 = TREE_OPERAND (base2, 0);
696 type2 = TREE_TYPE (base2);
698 /* Now search for the type1 in the access path of ref2. This
699 would be a common base for doing offset based disambiguation on. */
700 refp = &ref2;
701 while (handled_component_p (*refp)
702 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
703 refp = &TREE_OPERAND (*refp, 0);
704 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
705 /* If we couldn't compare types we have to bail out. */
706 if (same_p == -1)
707 return true;
708 else if (same_p == 1)
710 HOST_WIDE_INT offadj, sztmp, msztmp;
711 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
712 offset2 -= offadj;
713 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
714 offset1 -= offadj;
715 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
717 /* If we didn't find a common base, try the other way around. */
718 refp = &ref1;
719 while (handled_component_p (*refp)
720 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
721 refp = &TREE_OPERAND (*refp, 0);
722 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
723 /* If we couldn't compare types we have to bail out. */
724 if (same_p == -1)
725 return true;
726 else if (same_p == 1)
728 HOST_WIDE_INT offadj, sztmp, msztmp;
729 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
730 offset1 -= offadj;
731 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
732 offset2 -= offadj;
733 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
736 /* If we have two type access paths B1.path1 and B2.path2 they may
737 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
738 But we can still have a path that goes B1.path1...B2.path2 with
739 a part that we do not see. So we can only disambiguate now
740 if there is no B2 in the tail of path1 and no B1 on the
741 tail of path2. */
742 if (base1_alias_set == ref2_alias_set
743 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
744 return true;
745 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
746 if (!ref2_is_decl)
747 return (base2_alias_set == ref1_alias_set
748 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
749 return false;
752 /* Return true if we can determine that component references REF1 and REF2,
753 that are within a common DECL, cannot overlap. */
755 static bool
756 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
758 stack_vec<tree, 16> component_refs1;
759 stack_vec<tree, 16> component_refs2;
761 /* Create the stack of handled components for REF1. */
762 while (handled_component_p (ref1))
764 component_refs1.safe_push (ref1);
765 ref1 = TREE_OPERAND (ref1, 0);
767 if (TREE_CODE (ref1) == MEM_REF)
769 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
770 goto may_overlap;
771 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
774 /* Create the stack of handled components for REF2. */
775 while (handled_component_p (ref2))
777 component_refs2.safe_push (ref2);
778 ref2 = TREE_OPERAND (ref2, 0);
780 if (TREE_CODE (ref2) == MEM_REF)
782 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
783 goto may_overlap;
784 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
787 /* We must have the same base DECL. */
788 gcc_assert (ref1 == ref2);
790 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
791 rank. This is sufficient because we start from the same DECL and you
792 cannot reference several fields at a time with COMPONENT_REFs (unlike
793 with ARRAY_RANGE_REFs for arrays) so you always need the same number
794 of them to access a sub-component, unless you're in a union, in which
795 case the return value will precisely be false. */
796 while (true)
800 if (component_refs1.is_empty ())
801 goto may_overlap;
802 ref1 = component_refs1.pop ();
804 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
808 if (component_refs2.is_empty ())
809 goto may_overlap;
810 ref2 = component_refs2.pop ();
812 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
814 /* Beware of BIT_FIELD_REF. */
815 if (TREE_CODE (ref1) != COMPONENT_REF
816 || TREE_CODE (ref2) != COMPONENT_REF)
817 goto may_overlap;
819 tree field1 = TREE_OPERAND (ref1, 1);
820 tree field2 = TREE_OPERAND (ref2, 1);
822 /* ??? We cannot simply use the type of operand #0 of the refs here
823 as the Fortran compiler smuggles type punning into COMPONENT_REFs
824 for common blocks instead of using unions like everyone else. */
825 tree type1 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field1));
826 tree type2 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field2));
828 /* We cannot disambiguate fields in a union or qualified union. */
829 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
830 goto may_overlap;
832 /* Different fields of the same record type cannot overlap.
833 ??? Bitfields can overlap at RTL level so punt on them. */
834 if (field1 != field2)
836 component_refs1.release ();
837 component_refs2.release ();
838 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
842 may_overlap:
843 component_refs1.release ();
844 component_refs2.release ();
845 return false;
848 /* Return true if two memory references based on the variables BASE1
849 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
850 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
851 if non-NULL are the complete memory reference trees. */
853 static bool
854 decl_refs_may_alias_p (tree ref1, tree base1,
855 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
856 tree ref2, tree base2,
857 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
859 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
861 /* If both references are based on different variables, they cannot alias. */
862 if (base1 != base2)
863 return false;
865 /* If both references are based on the same variable, they cannot alias if
866 the accesses do not overlap. */
867 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
868 return false;
870 /* For components with variable position, the above test isn't sufficient,
871 so we disambiguate component references manually. */
872 if (ref1 && ref2
873 && handled_component_p (ref1) && handled_component_p (ref2)
874 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
875 return false;
877 return true;
880 /* Return true if an indirect reference based on *PTR1 constrained
881 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
882 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
883 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
884 in which case they are computed on-demand. REF1 and REF2
885 if non-NULL are the complete memory reference trees. */
887 static bool
888 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
889 HOST_WIDE_INT offset1,
890 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
891 alias_set_type ref1_alias_set,
892 alias_set_type base1_alias_set,
893 tree ref2 ATTRIBUTE_UNUSED, tree base2,
894 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
895 alias_set_type ref2_alias_set,
896 alias_set_type base2_alias_set, bool tbaa_p)
898 tree ptr1;
899 tree ptrtype1, dbase2;
900 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
901 HOST_WIDE_INT doffset1, doffset2;
902 double_int moff;
904 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
905 || TREE_CODE (base1) == TARGET_MEM_REF)
906 && DECL_P (base2));
908 ptr1 = TREE_OPERAND (base1, 0);
910 /* The offset embedded in MEM_REFs can be negative. Bias them
911 so that the resulting offset adjustment is positive. */
912 moff = mem_ref_offset (base1);
913 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
914 if (moff.is_negative ())
915 offset2p += (-moff).low;
916 else
917 offset1p += moff.low;
919 /* If only one reference is based on a variable, they cannot alias if
920 the pointer access is beyond the extent of the variable access.
921 (the pointer base cannot validly point to an offset less than zero
922 of the variable).
923 ??? IVOPTs creates bases that do not honor this restriction,
924 so do not apply this optimization for TARGET_MEM_REFs. */
925 if (TREE_CODE (base1) != TARGET_MEM_REF
926 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
927 return false;
928 /* They also cannot alias if the pointer may not point to the decl. */
929 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
930 return false;
932 /* Disambiguations that rely on strict aliasing rules follow. */
933 if (!flag_strict_aliasing || !tbaa_p)
934 return true;
936 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
938 /* If the alias set for a pointer access is zero all bets are off. */
939 if (base1_alias_set == -1)
940 base1_alias_set = get_deref_alias_set (ptrtype1);
941 if (base1_alias_set == 0)
942 return true;
943 if (base2_alias_set == -1)
944 base2_alias_set = get_alias_set (base2);
946 /* When we are trying to disambiguate an access with a pointer dereference
947 as base versus one with a decl as base we can use both the size
948 of the decl and its dynamic type for extra disambiguation.
949 ??? We do not know anything about the dynamic type of the decl
950 other than that its alias-set contains base2_alias_set as a subset
951 which does not help us here. */
952 /* As we know nothing useful about the dynamic type of the decl just
953 use the usual conflict check rather than a subset test.
954 ??? We could introduce -fvery-strict-aliasing when the language
955 does not allow decls to have a dynamic type that differs from their
956 static type. Then we can check
957 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
958 if (base1_alias_set != base2_alias_set
959 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
960 return false;
961 /* If the size of the access relevant for TBAA through the pointer
962 is bigger than the size of the decl we can't possibly access the
963 decl via that pointer. */
964 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
965 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
966 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
967 /* ??? This in turn may run afoul when a decl of type T which is
968 a member of union type U is accessed through a pointer to
969 type U and sizeof T is smaller than sizeof U. */
970 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
971 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
972 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
973 return false;
975 if (!ref2)
976 return true;
978 /* If the decl is accessed via a MEM_REF, reconstruct the base
979 we can use for TBAA and an appropriately adjusted offset. */
980 dbase2 = ref2;
981 while (handled_component_p (dbase2))
982 dbase2 = TREE_OPERAND (dbase2, 0);
983 doffset1 = offset1;
984 doffset2 = offset2;
985 if (TREE_CODE (dbase2) == MEM_REF
986 || TREE_CODE (dbase2) == TARGET_MEM_REF)
988 double_int moff = mem_ref_offset (dbase2);
989 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
990 if (moff.is_negative ())
991 doffset1 -= (-moff).low;
992 else
993 doffset2 -= moff.low;
996 /* If either reference is view-converted, give up now. */
997 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
998 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
999 return true;
1001 /* If both references are through the same type, they do not alias
1002 if the accesses do not overlap. This does extra disambiguation
1003 for mixed/pointer accesses but requires strict aliasing.
1004 For MEM_REFs we require that the component-ref offset we computed
1005 is relative to the start of the type which we ensure by
1006 comparing rvalue and access type and disregarding the constant
1007 pointer offset. */
1008 if ((TREE_CODE (base1) != TARGET_MEM_REF
1009 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1010 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1011 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1013 /* Do access-path based disambiguation. */
1014 if (ref1 && ref2
1015 && (handled_component_p (ref1) || handled_component_p (ref2)))
1016 return aliasing_component_refs_p (ref1,
1017 ref1_alias_set, base1_alias_set,
1018 offset1, max_size1,
1019 ref2,
1020 ref2_alias_set, base2_alias_set,
1021 offset2, max_size2, true);
1023 return true;
1026 /* Return true if two indirect references based on *PTR1
1027 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1028 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1029 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1030 in which case they are computed on-demand. REF1 and REF2
1031 if non-NULL are the complete memory reference trees. */
1033 static bool
1034 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1035 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1036 alias_set_type ref1_alias_set,
1037 alias_set_type base1_alias_set,
1038 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1039 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1040 alias_set_type ref2_alias_set,
1041 alias_set_type base2_alias_set, bool tbaa_p)
1043 tree ptr1;
1044 tree ptr2;
1045 tree ptrtype1, ptrtype2;
1047 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1048 || TREE_CODE (base1) == TARGET_MEM_REF)
1049 && (TREE_CODE (base2) == MEM_REF
1050 || TREE_CODE (base2) == TARGET_MEM_REF));
1052 ptr1 = TREE_OPERAND (base1, 0);
1053 ptr2 = TREE_OPERAND (base2, 0);
1055 /* If both bases are based on pointers they cannot alias if they may not
1056 point to the same memory object or if they point to the same object
1057 and the accesses do not overlap. */
1058 if ((!cfun || gimple_in_ssa_p (cfun))
1059 && operand_equal_p (ptr1, ptr2, 0)
1060 && (((TREE_CODE (base1) != TARGET_MEM_REF
1061 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1062 && (TREE_CODE (base2) != TARGET_MEM_REF
1063 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1064 || (TREE_CODE (base1) == TARGET_MEM_REF
1065 && TREE_CODE (base2) == TARGET_MEM_REF
1066 && (TMR_STEP (base1) == TMR_STEP (base2)
1067 || (TMR_STEP (base1) && TMR_STEP (base2)
1068 && operand_equal_p (TMR_STEP (base1),
1069 TMR_STEP (base2), 0)))
1070 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1071 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1072 && operand_equal_p (TMR_INDEX (base1),
1073 TMR_INDEX (base2), 0)))
1074 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1075 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1076 && operand_equal_p (TMR_INDEX2 (base1),
1077 TMR_INDEX2 (base2), 0))))))
1079 double_int moff;
1080 /* The offset embedded in MEM_REFs can be negative. Bias them
1081 so that the resulting offset adjustment is positive. */
1082 moff = mem_ref_offset (base1);
1083 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1084 if (moff.is_negative ())
1085 offset2 += (-moff).low;
1086 else
1087 offset1 += moff.low;
1088 moff = mem_ref_offset (base2);
1089 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1090 if (moff.is_negative ())
1091 offset1 += (-moff).low;
1092 else
1093 offset2 += moff.low;
1094 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1096 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1097 return false;
1099 /* Disambiguations that rely on strict aliasing rules follow. */
1100 if (!flag_strict_aliasing || !tbaa_p)
1101 return true;
1103 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1104 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1106 /* If the alias set for a pointer access is zero all bets are off. */
1107 if (base1_alias_set == -1)
1108 base1_alias_set = get_deref_alias_set (ptrtype1);
1109 if (base1_alias_set == 0)
1110 return true;
1111 if (base2_alias_set == -1)
1112 base2_alias_set = get_deref_alias_set (ptrtype2);
1113 if (base2_alias_set == 0)
1114 return true;
1116 /* If both references are through the same type, they do not alias
1117 if the accesses do not overlap. This does extra disambiguation
1118 for mixed/pointer accesses but requires strict aliasing. */
1119 if ((TREE_CODE (base1) != TARGET_MEM_REF
1120 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1121 && (TREE_CODE (base2) != TARGET_MEM_REF
1122 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1123 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1124 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1125 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1126 TREE_TYPE (ptrtype2)) == 1)
1127 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1129 /* Do type-based disambiguation. */
1130 if (base1_alias_set != base2_alias_set
1131 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1132 return false;
1134 /* Do access-path based disambiguation. */
1135 if (ref1 && ref2
1136 && (handled_component_p (ref1) || handled_component_p (ref2))
1137 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1138 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
1139 return aliasing_component_refs_p (ref1,
1140 ref1_alias_set, base1_alias_set,
1141 offset1, max_size1,
1142 ref2,
1143 ref2_alias_set, base2_alias_set,
1144 offset2, max_size2, false);
1146 return true;
1149 /* Return true, if the two memory references REF1 and REF2 may alias. */
1151 bool
1152 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1154 tree base1, base2;
1155 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1156 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1157 bool var1_p, var2_p, ind1_p, ind2_p;
1159 gcc_checking_assert ((!ref1->ref
1160 || TREE_CODE (ref1->ref) == SSA_NAME
1161 || DECL_P (ref1->ref)
1162 || TREE_CODE (ref1->ref) == STRING_CST
1163 || handled_component_p (ref1->ref)
1164 || TREE_CODE (ref1->ref) == MEM_REF
1165 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1166 && (!ref2->ref
1167 || TREE_CODE (ref2->ref) == SSA_NAME
1168 || DECL_P (ref2->ref)
1169 || TREE_CODE (ref2->ref) == STRING_CST
1170 || handled_component_p (ref2->ref)
1171 || TREE_CODE (ref2->ref) == MEM_REF
1172 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1174 /* Decompose the references into their base objects and the access. */
1175 base1 = ao_ref_base (ref1);
1176 offset1 = ref1->offset;
1177 max_size1 = ref1->max_size;
1178 base2 = ao_ref_base (ref2);
1179 offset2 = ref2->offset;
1180 max_size2 = ref2->max_size;
1182 /* We can end up with registers or constants as bases for example from
1183 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1184 which is seen as a struct copy. */
1185 if (TREE_CODE (base1) == SSA_NAME
1186 || TREE_CODE (base1) == CONST_DECL
1187 || TREE_CODE (base1) == CONSTRUCTOR
1188 || TREE_CODE (base1) == ADDR_EXPR
1189 || CONSTANT_CLASS_P (base1)
1190 || TREE_CODE (base2) == SSA_NAME
1191 || TREE_CODE (base2) == CONST_DECL
1192 || TREE_CODE (base2) == CONSTRUCTOR
1193 || TREE_CODE (base2) == ADDR_EXPR
1194 || CONSTANT_CLASS_P (base2))
1195 return false;
1197 /* We can end up referring to code via function and label decls.
1198 As we likely do not properly track code aliases conservatively
1199 bail out. */
1200 if (TREE_CODE (base1) == FUNCTION_DECL
1201 || TREE_CODE (base1) == LABEL_DECL
1202 || TREE_CODE (base2) == FUNCTION_DECL
1203 || TREE_CODE (base2) == LABEL_DECL)
1204 return true;
1206 /* Two volatile accesses always conflict. */
1207 if (ref1->volatile_p
1208 && ref2->volatile_p)
1209 return true;
1211 /* Defer to simple offset based disambiguation if we have
1212 references based on two decls. Do this before defering to
1213 TBAA to handle must-alias cases in conformance with the
1214 GCC extension of allowing type-punning through unions. */
1215 var1_p = DECL_P (base1);
1216 var2_p = DECL_P (base2);
1217 if (var1_p && var2_p)
1218 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1219 ref2->ref, base2, offset2, max_size2);
1221 ind1_p = (TREE_CODE (base1) == MEM_REF
1222 || TREE_CODE (base1) == TARGET_MEM_REF);
1223 ind2_p = (TREE_CODE (base2) == MEM_REF
1224 || TREE_CODE (base2) == TARGET_MEM_REF);
1226 /* Canonicalize the pointer-vs-decl case. */
1227 if (ind1_p && var2_p)
1229 HOST_WIDE_INT tmp1;
1230 tree tmp2;
1231 ao_ref *tmp3;
1232 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1233 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1234 tmp2 = base1; base1 = base2; base2 = tmp2;
1235 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1236 var1_p = true;
1237 ind1_p = false;
1238 var2_p = false;
1239 ind2_p = true;
1242 /* First defer to TBAA if possible. */
1243 if (tbaa_p
1244 && flag_strict_aliasing
1245 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1246 ao_ref_alias_set (ref2)))
1247 return false;
1249 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1250 if (var1_p && ind2_p)
1251 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1252 offset2, max_size2,
1253 ao_ref_alias_set (ref2), -1,
1254 ref1->ref, base1,
1255 offset1, max_size1,
1256 ao_ref_alias_set (ref1),
1257 ao_ref_base_alias_set (ref1),
1258 tbaa_p);
1259 else if (ind1_p && ind2_p)
1260 return indirect_refs_may_alias_p (ref1->ref, base1,
1261 offset1, max_size1,
1262 ao_ref_alias_set (ref1), -1,
1263 ref2->ref, base2,
1264 offset2, max_size2,
1265 ao_ref_alias_set (ref2), -1,
1266 tbaa_p);
1268 /* We really do not want to end up here, but returning true is safe. */
1269 #ifdef ENABLE_CHECKING
1270 gcc_unreachable ();
1271 #else
1272 return true;
1273 #endif
1276 bool
1277 refs_may_alias_p (tree ref1, tree ref2)
1279 ao_ref r1, r2;
1280 bool res;
1281 ao_ref_init (&r1, ref1);
1282 ao_ref_init (&r2, ref2);
1283 res = refs_may_alias_p_1 (&r1, &r2, true);
1284 if (res)
1285 ++alias_stats.refs_may_alias_p_may_alias;
1286 else
1287 ++alias_stats.refs_may_alias_p_no_alias;
1288 return res;
1291 /* Returns true if there is a anti-dependence for the STORE that
1292 executes after the LOAD. */
1294 bool
1295 refs_anti_dependent_p (tree load, tree store)
1297 ao_ref r1, r2;
1298 ao_ref_init (&r1, load);
1299 ao_ref_init (&r2, store);
1300 return refs_may_alias_p_1 (&r1, &r2, false);
1303 /* Returns true if there is a output dependence for the stores
1304 STORE1 and STORE2. */
1306 bool
1307 refs_output_dependent_p (tree store1, tree store2)
1309 ao_ref r1, r2;
1310 ao_ref_init (&r1, store1);
1311 ao_ref_init (&r2, store2);
1312 return refs_may_alias_p_1 (&r1, &r2, false);
1315 /* If the call CALL may use the memory reference REF return true,
1316 otherwise return false. */
1318 static bool
1319 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1321 tree base, callee;
1322 unsigned i;
1323 int flags = gimple_call_flags (call);
1325 /* Const functions without a static chain do not implicitly use memory. */
1326 if (!gimple_call_chain (call)
1327 && (flags & (ECF_CONST|ECF_NOVOPS)))
1328 goto process_args;
1330 base = ao_ref_base (ref);
1331 if (!base)
1332 return true;
1334 /* A call that is not without side-effects might involve volatile
1335 accesses and thus conflicts with all other volatile accesses. */
1336 if (ref->volatile_p)
1337 return true;
1339 /* If the reference is based on a decl that is not aliased the call
1340 cannot possibly use it. */
1341 if (DECL_P (base)
1342 && !may_be_aliased (base)
1343 /* But local statics can be used through recursion. */
1344 && !is_global_var (base))
1345 goto process_args;
1347 callee = gimple_call_fndecl (call);
1349 /* Handle those builtin functions explicitly that do not act as
1350 escape points. See tree-ssa-structalias.c:find_func_aliases
1351 for the list of builtins we might need to handle here. */
1352 if (callee != NULL_TREE
1353 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1354 switch (DECL_FUNCTION_CODE (callee))
1356 /* All the following functions read memory pointed to by
1357 their second argument. strcat/strncat additionally
1358 reads memory pointed to by the first argument. */
1359 case BUILT_IN_STRCAT:
1360 case BUILT_IN_STRNCAT:
1362 ao_ref dref;
1363 ao_ref_init_from_ptr_and_size (&dref,
1364 gimple_call_arg (call, 0),
1365 NULL_TREE);
1366 if (refs_may_alias_p_1 (&dref, ref, false))
1367 return true;
1369 /* FALLTHRU */
1370 case BUILT_IN_STRCPY:
1371 case BUILT_IN_STRNCPY:
1372 case BUILT_IN_MEMCPY:
1373 case BUILT_IN_MEMMOVE:
1374 case BUILT_IN_MEMPCPY:
1375 case BUILT_IN_STPCPY:
1376 case BUILT_IN_STPNCPY:
1377 case BUILT_IN_TM_MEMCPY:
1378 case BUILT_IN_TM_MEMMOVE:
1380 ao_ref dref;
1381 tree size = NULL_TREE;
1382 if (gimple_call_num_args (call) == 3)
1383 size = gimple_call_arg (call, 2);
1384 ao_ref_init_from_ptr_and_size (&dref,
1385 gimple_call_arg (call, 1),
1386 size);
1387 return refs_may_alias_p_1 (&dref, ref, false);
1389 case BUILT_IN_STRCAT_CHK:
1390 case BUILT_IN_STRNCAT_CHK:
1392 ao_ref dref;
1393 ao_ref_init_from_ptr_and_size (&dref,
1394 gimple_call_arg (call, 0),
1395 NULL_TREE);
1396 if (refs_may_alias_p_1 (&dref, ref, false))
1397 return true;
1399 /* FALLTHRU */
1400 case BUILT_IN_STRCPY_CHK:
1401 case BUILT_IN_STRNCPY_CHK:
1402 case BUILT_IN_MEMCPY_CHK:
1403 case BUILT_IN_MEMMOVE_CHK:
1404 case BUILT_IN_MEMPCPY_CHK:
1405 case BUILT_IN_STPCPY_CHK:
1406 case BUILT_IN_STPNCPY_CHK:
1408 ao_ref dref;
1409 tree size = NULL_TREE;
1410 if (gimple_call_num_args (call) == 4)
1411 size = gimple_call_arg (call, 2);
1412 ao_ref_init_from_ptr_and_size (&dref,
1413 gimple_call_arg (call, 1),
1414 size);
1415 return refs_may_alias_p_1 (&dref, ref, false);
1417 case BUILT_IN_BCOPY:
1419 ao_ref dref;
1420 tree size = gimple_call_arg (call, 2);
1421 ao_ref_init_from_ptr_and_size (&dref,
1422 gimple_call_arg (call, 0),
1423 size);
1424 return refs_may_alias_p_1 (&dref, ref, false);
1427 /* The following functions read memory pointed to by their
1428 first argument. */
1429 CASE_BUILT_IN_TM_LOAD (1):
1430 CASE_BUILT_IN_TM_LOAD (2):
1431 CASE_BUILT_IN_TM_LOAD (4):
1432 CASE_BUILT_IN_TM_LOAD (8):
1433 CASE_BUILT_IN_TM_LOAD (FLOAT):
1434 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1435 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1436 CASE_BUILT_IN_TM_LOAD (M64):
1437 CASE_BUILT_IN_TM_LOAD (M128):
1438 CASE_BUILT_IN_TM_LOAD (M256):
1439 case BUILT_IN_TM_LOG:
1440 case BUILT_IN_TM_LOG_1:
1441 case BUILT_IN_TM_LOG_2:
1442 case BUILT_IN_TM_LOG_4:
1443 case BUILT_IN_TM_LOG_8:
1444 case BUILT_IN_TM_LOG_FLOAT:
1445 case BUILT_IN_TM_LOG_DOUBLE:
1446 case BUILT_IN_TM_LOG_LDOUBLE:
1447 case BUILT_IN_TM_LOG_M64:
1448 case BUILT_IN_TM_LOG_M128:
1449 case BUILT_IN_TM_LOG_M256:
1450 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1452 /* These read memory pointed to by the first argument. */
1453 case BUILT_IN_STRDUP:
1454 case BUILT_IN_STRNDUP:
1456 ao_ref dref;
1457 tree size = NULL_TREE;
1458 if (gimple_call_num_args (call) == 2)
1459 size = gimple_call_arg (call, 1);
1460 ao_ref_init_from_ptr_and_size (&dref,
1461 gimple_call_arg (call, 0),
1462 size);
1463 return refs_may_alias_p_1 (&dref, ref, false);
1465 /* These read memory pointed to by the first argument. */
1466 case BUILT_IN_INDEX:
1467 case BUILT_IN_STRCHR:
1468 case BUILT_IN_STRRCHR:
1470 ao_ref dref;
1471 ao_ref_init_from_ptr_and_size (&dref,
1472 gimple_call_arg (call, 0),
1473 NULL_TREE);
1474 return refs_may_alias_p_1 (&dref, ref, false);
1476 /* These read memory pointed to by the first argument with size
1477 in the third argument. */
1478 case BUILT_IN_MEMCHR:
1480 ao_ref dref;
1481 ao_ref_init_from_ptr_and_size (&dref,
1482 gimple_call_arg (call, 0),
1483 gimple_call_arg (call, 2));
1484 return refs_may_alias_p_1 (&dref, ref, false);
1486 /* These read memory pointed to by the first and second arguments. */
1487 case BUILT_IN_STRSTR:
1488 case BUILT_IN_STRPBRK:
1490 ao_ref dref;
1491 ao_ref_init_from_ptr_and_size (&dref,
1492 gimple_call_arg (call, 0),
1493 NULL_TREE);
1494 if (refs_may_alias_p_1 (&dref, ref, false))
1495 return true;
1496 ao_ref_init_from_ptr_and_size (&dref,
1497 gimple_call_arg (call, 1),
1498 NULL_TREE);
1499 return refs_may_alias_p_1 (&dref, ref, false);
1502 /* The following builtins do not read from memory. */
1503 case BUILT_IN_FREE:
1504 case BUILT_IN_MALLOC:
1505 case BUILT_IN_CALLOC:
1506 case BUILT_IN_ALLOCA:
1507 case BUILT_IN_ALLOCA_WITH_ALIGN:
1508 case BUILT_IN_STACK_SAVE:
1509 case BUILT_IN_STACK_RESTORE:
1510 case BUILT_IN_MEMSET:
1511 case BUILT_IN_TM_MEMSET:
1512 case BUILT_IN_MEMSET_CHK:
1513 case BUILT_IN_FREXP:
1514 case BUILT_IN_FREXPF:
1515 case BUILT_IN_FREXPL:
1516 case BUILT_IN_GAMMA_R:
1517 case BUILT_IN_GAMMAF_R:
1518 case BUILT_IN_GAMMAL_R:
1519 case BUILT_IN_LGAMMA_R:
1520 case BUILT_IN_LGAMMAF_R:
1521 case BUILT_IN_LGAMMAL_R:
1522 case BUILT_IN_MODF:
1523 case BUILT_IN_MODFF:
1524 case BUILT_IN_MODFL:
1525 case BUILT_IN_REMQUO:
1526 case BUILT_IN_REMQUOF:
1527 case BUILT_IN_REMQUOL:
1528 case BUILT_IN_SINCOS:
1529 case BUILT_IN_SINCOSF:
1530 case BUILT_IN_SINCOSL:
1531 case BUILT_IN_ASSUME_ALIGNED:
1532 case BUILT_IN_VA_END:
1533 return false;
1534 /* __sync_* builtins and some OpenMP builtins act as threading
1535 barriers. */
1536 #undef DEF_SYNC_BUILTIN
1537 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1538 #include "sync-builtins.def"
1539 #undef DEF_SYNC_BUILTIN
1540 case BUILT_IN_GOMP_ATOMIC_START:
1541 case BUILT_IN_GOMP_ATOMIC_END:
1542 case BUILT_IN_GOMP_BARRIER:
1543 case BUILT_IN_GOMP_BARRIER_CANCEL:
1544 case BUILT_IN_GOMP_TASKWAIT:
1545 case BUILT_IN_GOMP_TASKGROUP_END:
1546 case BUILT_IN_GOMP_CRITICAL_START:
1547 case BUILT_IN_GOMP_CRITICAL_END:
1548 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1549 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1550 case BUILT_IN_GOMP_LOOP_END:
1551 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1552 case BUILT_IN_GOMP_ORDERED_START:
1553 case BUILT_IN_GOMP_ORDERED_END:
1554 case BUILT_IN_GOMP_SECTIONS_END:
1555 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1556 case BUILT_IN_GOMP_SINGLE_COPY_START:
1557 case BUILT_IN_GOMP_SINGLE_COPY_END:
1558 return true;
1560 default:
1561 /* Fallthru to general call handling. */;
1564 /* Check if base is a global static variable that is not read
1565 by the function. */
1566 if (callee != NULL_TREE
1567 && TREE_CODE (base) == VAR_DECL
1568 && TREE_STATIC (base))
1570 struct cgraph_node *node = cgraph_get_node (callee);
1571 bitmap not_read;
1573 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1574 node yet. We should enforce that there are nodes for all decls in the
1575 IL and remove this check instead. */
1576 if (node
1577 && (not_read = ipa_reference_get_not_read_global (node))
1578 && bitmap_bit_p (not_read, DECL_UID (base)))
1579 goto process_args;
1582 /* Check if the base variable is call-used. */
1583 if (DECL_P (base))
1585 if (pt_solution_includes (gimple_call_use_set (call), base))
1586 return true;
1588 else if ((TREE_CODE (base) == MEM_REF
1589 || TREE_CODE (base) == TARGET_MEM_REF)
1590 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1592 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1593 if (!pi)
1594 return true;
1596 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1597 return true;
1599 else
1600 return true;
1602 /* Inspect call arguments for passed-by-value aliases. */
1603 process_args:
1604 for (i = 0; i < gimple_call_num_args (call); ++i)
1606 tree op = gimple_call_arg (call, i);
1607 int flags = gimple_call_arg_flags (call, i);
1609 if (flags & EAF_UNUSED)
1610 continue;
1612 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1613 op = TREE_OPERAND (op, 0);
1615 if (TREE_CODE (op) != SSA_NAME
1616 && !is_gimple_min_invariant (op))
1618 ao_ref r;
1619 ao_ref_init (&r, op);
1620 if (refs_may_alias_p_1 (&r, ref, true))
1621 return true;
1625 return false;
1628 static bool
1629 ref_maybe_used_by_call_p (gimple call, tree ref)
1631 ao_ref r;
1632 bool res;
1633 ao_ref_init (&r, ref);
1634 res = ref_maybe_used_by_call_p_1 (call, &r);
1635 if (res)
1636 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1637 else
1638 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1639 return res;
1643 /* If the statement STMT may use the memory reference REF return
1644 true, otherwise return false. */
1646 bool
1647 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1649 if (is_gimple_assign (stmt))
1651 tree rhs;
1653 /* All memory assign statements are single. */
1654 if (!gimple_assign_single_p (stmt))
1655 return false;
1657 rhs = gimple_assign_rhs1 (stmt);
1658 if (is_gimple_reg (rhs)
1659 || is_gimple_min_invariant (rhs)
1660 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1661 return false;
1663 return refs_may_alias_p (rhs, ref);
1665 else if (is_gimple_call (stmt))
1666 return ref_maybe_used_by_call_p (stmt, ref);
1667 else if (gimple_code (stmt) == GIMPLE_RETURN)
1669 tree retval = gimple_return_retval (stmt);
1670 tree base;
1671 if (retval
1672 && TREE_CODE (retval) != SSA_NAME
1673 && !is_gimple_min_invariant (retval)
1674 && refs_may_alias_p (retval, ref))
1675 return true;
1676 /* If ref escapes the function then the return acts as a use. */
1677 base = get_base_address (ref);
1678 if (!base)
1680 else if (DECL_P (base))
1681 return is_global_var (base);
1682 else if (TREE_CODE (base) == MEM_REF
1683 || TREE_CODE (base) == TARGET_MEM_REF)
1684 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1685 return false;
1688 return true;
1691 /* If the call in statement CALL may clobber the memory reference REF
1692 return true, otherwise return false. */
1694 static bool
1695 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1697 tree base;
1698 tree callee;
1700 /* If the call is pure or const it cannot clobber anything. */
1701 if (gimple_call_flags (call)
1702 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1703 return false;
1705 base = ao_ref_base (ref);
1706 if (!base)
1707 return true;
1709 if (TREE_CODE (base) == SSA_NAME
1710 || CONSTANT_CLASS_P (base))
1711 return false;
1713 /* A call that is not without side-effects might involve volatile
1714 accesses and thus conflicts with all other volatile accesses. */
1715 if (ref->volatile_p)
1716 return true;
1718 /* If the reference is based on a decl that is not aliased the call
1719 cannot possibly clobber it. */
1720 if (DECL_P (base)
1721 && !may_be_aliased (base)
1722 /* But local non-readonly statics can be modified through recursion
1723 or the call may implement a threading barrier which we must
1724 treat as may-def. */
1725 && (TREE_READONLY (base)
1726 || !is_global_var (base)))
1727 return false;
1729 callee = gimple_call_fndecl (call);
1731 /* Handle those builtin functions explicitly that do not act as
1732 escape points. See tree-ssa-structalias.c:find_func_aliases
1733 for the list of builtins we might need to handle here. */
1734 if (callee != NULL_TREE
1735 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1736 switch (DECL_FUNCTION_CODE (callee))
1738 /* All the following functions clobber memory pointed to by
1739 their first argument. */
1740 case BUILT_IN_STRCPY:
1741 case BUILT_IN_STRNCPY:
1742 case BUILT_IN_MEMCPY:
1743 case BUILT_IN_MEMMOVE:
1744 case BUILT_IN_MEMPCPY:
1745 case BUILT_IN_STPCPY:
1746 case BUILT_IN_STPNCPY:
1747 case BUILT_IN_STRCAT:
1748 case BUILT_IN_STRNCAT:
1749 case BUILT_IN_MEMSET:
1750 case BUILT_IN_TM_MEMSET:
1751 CASE_BUILT_IN_TM_STORE (1):
1752 CASE_BUILT_IN_TM_STORE (2):
1753 CASE_BUILT_IN_TM_STORE (4):
1754 CASE_BUILT_IN_TM_STORE (8):
1755 CASE_BUILT_IN_TM_STORE (FLOAT):
1756 CASE_BUILT_IN_TM_STORE (DOUBLE):
1757 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1758 CASE_BUILT_IN_TM_STORE (M64):
1759 CASE_BUILT_IN_TM_STORE (M128):
1760 CASE_BUILT_IN_TM_STORE (M256):
1761 case BUILT_IN_TM_MEMCPY:
1762 case BUILT_IN_TM_MEMMOVE:
1764 ao_ref dref;
1765 tree size = NULL_TREE;
1766 /* Don't pass in size for strncat, as the maximum size
1767 is strlen (dest) + n + 1 instead of n, resp.
1768 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1769 known. */
1770 if (gimple_call_num_args (call) == 3
1771 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1772 size = gimple_call_arg (call, 2);
1773 ao_ref_init_from_ptr_and_size (&dref,
1774 gimple_call_arg (call, 0),
1775 size);
1776 return refs_may_alias_p_1 (&dref, ref, false);
1778 case BUILT_IN_STRCPY_CHK:
1779 case BUILT_IN_STRNCPY_CHK:
1780 case BUILT_IN_MEMCPY_CHK:
1781 case BUILT_IN_MEMMOVE_CHK:
1782 case BUILT_IN_MEMPCPY_CHK:
1783 case BUILT_IN_STPCPY_CHK:
1784 case BUILT_IN_STPNCPY_CHK:
1785 case BUILT_IN_STRCAT_CHK:
1786 case BUILT_IN_STRNCAT_CHK:
1787 case BUILT_IN_MEMSET_CHK:
1789 ao_ref dref;
1790 tree size = NULL_TREE;
1791 /* Don't pass in size for __strncat_chk, as the maximum size
1792 is strlen (dest) + n + 1 instead of n, resp.
1793 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1794 known. */
1795 if (gimple_call_num_args (call) == 4
1796 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1797 size = gimple_call_arg (call, 2);
1798 ao_ref_init_from_ptr_and_size (&dref,
1799 gimple_call_arg (call, 0),
1800 size);
1801 return refs_may_alias_p_1 (&dref, ref, false);
1803 case BUILT_IN_BCOPY:
1805 ao_ref dref;
1806 tree size = gimple_call_arg (call, 2);
1807 ao_ref_init_from_ptr_and_size (&dref,
1808 gimple_call_arg (call, 1),
1809 size);
1810 return refs_may_alias_p_1 (&dref, ref, false);
1812 /* Allocating memory does not have any side-effects apart from
1813 being the definition point for the pointer. */
1814 case BUILT_IN_MALLOC:
1815 case BUILT_IN_CALLOC:
1816 case BUILT_IN_STRDUP:
1817 case BUILT_IN_STRNDUP:
1818 /* Unix98 specifies that errno is set on allocation failure. */
1819 if (flag_errno_math
1820 && targetm.ref_may_alias_errno (ref))
1821 return true;
1822 return false;
1823 case BUILT_IN_STACK_SAVE:
1824 case BUILT_IN_ALLOCA:
1825 case BUILT_IN_ALLOCA_WITH_ALIGN:
1826 case BUILT_IN_ASSUME_ALIGNED:
1827 return false;
1828 /* Freeing memory kills the pointed-to memory. More importantly
1829 the call has to serve as a barrier for moving loads and stores
1830 across it. */
1831 case BUILT_IN_FREE:
1832 case BUILT_IN_VA_END:
1834 tree ptr = gimple_call_arg (call, 0);
1835 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1837 case BUILT_IN_GAMMA_R:
1838 case BUILT_IN_GAMMAF_R:
1839 case BUILT_IN_GAMMAL_R:
1840 case BUILT_IN_LGAMMA_R:
1841 case BUILT_IN_LGAMMAF_R:
1842 case BUILT_IN_LGAMMAL_R:
1844 tree out = gimple_call_arg (call, 1);
1845 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1846 return true;
1847 if (flag_errno_math)
1848 break;
1849 return false;
1851 case BUILT_IN_FREXP:
1852 case BUILT_IN_FREXPF:
1853 case BUILT_IN_FREXPL:
1854 case BUILT_IN_MODF:
1855 case BUILT_IN_MODFF:
1856 case BUILT_IN_MODFL:
1858 tree out = gimple_call_arg (call, 1);
1859 return ptr_deref_may_alias_ref_p_1 (out, ref);
1861 case BUILT_IN_REMQUO:
1862 case BUILT_IN_REMQUOF:
1863 case BUILT_IN_REMQUOL:
1865 tree out = gimple_call_arg (call, 2);
1866 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1867 return true;
1868 if (flag_errno_math)
1869 break;
1870 return false;
1872 case BUILT_IN_SINCOS:
1873 case BUILT_IN_SINCOSF:
1874 case BUILT_IN_SINCOSL:
1876 tree sin = gimple_call_arg (call, 1);
1877 tree cos = gimple_call_arg (call, 2);
1878 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1879 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1881 /* __sync_* builtins and some OpenMP builtins act as threading
1882 barriers. */
1883 #undef DEF_SYNC_BUILTIN
1884 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1885 #include "sync-builtins.def"
1886 #undef DEF_SYNC_BUILTIN
1887 case BUILT_IN_GOMP_ATOMIC_START:
1888 case BUILT_IN_GOMP_ATOMIC_END:
1889 case BUILT_IN_GOMP_BARRIER:
1890 case BUILT_IN_GOMP_BARRIER_CANCEL:
1891 case BUILT_IN_GOMP_TASKWAIT:
1892 case BUILT_IN_GOMP_TASKGROUP_END:
1893 case BUILT_IN_GOMP_CRITICAL_START:
1894 case BUILT_IN_GOMP_CRITICAL_END:
1895 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1896 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1897 case BUILT_IN_GOMP_LOOP_END:
1898 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1899 case BUILT_IN_GOMP_ORDERED_START:
1900 case BUILT_IN_GOMP_ORDERED_END:
1901 case BUILT_IN_GOMP_SECTIONS_END:
1902 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1903 case BUILT_IN_GOMP_SINGLE_COPY_START:
1904 case BUILT_IN_GOMP_SINGLE_COPY_END:
1905 return true;
1906 default:
1907 /* Fallthru to general call handling. */;
1910 /* Check if base is a global static variable that is not written
1911 by the function. */
1912 if (callee != NULL_TREE
1913 && TREE_CODE (base) == VAR_DECL
1914 && TREE_STATIC (base))
1916 struct cgraph_node *node = cgraph_get_node (callee);
1917 bitmap not_written;
1919 if (node
1920 && (not_written = ipa_reference_get_not_written_global (node))
1921 && bitmap_bit_p (not_written, DECL_UID (base)))
1922 return false;
1925 /* Check if the base variable is call-clobbered. */
1926 if (DECL_P (base))
1927 return pt_solution_includes (gimple_call_clobber_set (call), base);
1928 else if ((TREE_CODE (base) == MEM_REF
1929 || TREE_CODE (base) == TARGET_MEM_REF)
1930 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1932 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1933 if (!pi)
1934 return true;
1936 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1939 return true;
1942 /* If the call in statement CALL may clobber the memory reference REF
1943 return true, otherwise return false. */
1945 bool
1946 call_may_clobber_ref_p (gimple call, tree ref)
1948 bool res;
1949 ao_ref r;
1950 ao_ref_init (&r, ref);
1951 res = call_may_clobber_ref_p_1 (call, &r);
1952 if (res)
1953 ++alias_stats.call_may_clobber_ref_p_may_alias;
1954 else
1955 ++alias_stats.call_may_clobber_ref_p_no_alias;
1956 return res;
1960 /* If the statement STMT may clobber the memory reference REF return true,
1961 otherwise return false. */
1963 bool
1964 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1966 if (is_gimple_call (stmt))
1968 tree lhs = gimple_call_lhs (stmt);
1969 if (lhs
1970 && TREE_CODE (lhs) != SSA_NAME)
1972 ao_ref r;
1973 ao_ref_init (&r, lhs);
1974 if (refs_may_alias_p_1 (ref, &r, true))
1975 return true;
1978 return call_may_clobber_ref_p_1 (stmt, ref);
1980 else if (gimple_assign_single_p (stmt))
1982 tree lhs = gimple_assign_lhs (stmt);
1983 if (TREE_CODE (lhs) != SSA_NAME)
1985 ao_ref r;
1986 ao_ref_init (&r, lhs);
1987 return refs_may_alias_p_1 (ref, &r, true);
1990 else if (gimple_code (stmt) == GIMPLE_ASM)
1991 return true;
1993 return false;
1996 bool
1997 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1999 ao_ref r;
2000 ao_ref_init (&r, ref);
2001 return stmt_may_clobber_ref_p_1 (stmt, &r);
2004 /* If STMT kills the memory reference REF return true, otherwise
2005 return false. */
2007 static bool
2008 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
2010 /* For a must-alias check we need to be able to constrain
2011 the access properly. */
2012 ao_ref_base (ref);
2013 if (ref->max_size == -1)
2014 return false;
2016 if (gimple_has_lhs (stmt)
2017 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2018 /* The assignment is not necessarily carried out if it can throw
2019 and we can catch it in the current function where we could inspect
2020 the previous value.
2021 ??? We only need to care about the RHS throwing. For aggregate
2022 assignments or similar calls and non-call exceptions the LHS
2023 might throw as well. */
2024 && !stmt_can_throw_internal (stmt))
2026 tree base, lhs = gimple_get_lhs (stmt);
2027 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2028 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2029 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2030 so base == ref->base does not always hold. */
2031 if (base != ref->base)
2033 /* If both base and ref->base are MEM_REFs, only compare the
2034 first operand, and if the second operand isn't equal constant,
2035 try to add the offsets into offset and ref_offset. */
2036 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2037 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2039 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2040 TREE_OPERAND (ref->base, 1)))
2042 double_int off1 = mem_ref_offset (base);
2043 off1 = off1.lshift (BITS_PER_UNIT == 8
2044 ? 3 : exact_log2 (BITS_PER_UNIT));
2045 off1 = off1 + double_int::from_shwi (offset);
2046 double_int off2 = mem_ref_offset (ref->base);
2047 off2 = off2.lshift (BITS_PER_UNIT == 8
2048 ? 3 : exact_log2 (BITS_PER_UNIT));
2049 off2 = off2 + double_int::from_shwi (ref_offset);
2050 if (off1.fits_shwi () && off2.fits_shwi ())
2052 offset = off1.to_shwi ();
2053 ref_offset = off2.to_shwi ();
2055 else
2056 size = -1;
2059 else
2060 size = -1;
2062 /* For a must-alias check we need to be able to constrain
2063 the access properly. */
2064 if (size != -1 && size == max_size)
2066 if (offset <= ref_offset
2067 && offset + size >= ref_offset + ref->max_size)
2068 return true;
2072 if (is_gimple_call (stmt))
2074 tree callee = gimple_call_fndecl (stmt);
2075 if (callee != NULL_TREE
2076 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2077 switch (DECL_FUNCTION_CODE (callee))
2079 case BUILT_IN_FREE:
2081 tree ptr = gimple_call_arg (stmt, 0);
2082 tree base = ao_ref_base (ref);
2083 if (base && TREE_CODE (base) == MEM_REF
2084 && TREE_OPERAND (base, 0) == ptr)
2085 return true;
2086 break;
2089 case BUILT_IN_MEMCPY:
2090 case BUILT_IN_MEMPCPY:
2091 case BUILT_IN_MEMMOVE:
2092 case BUILT_IN_MEMSET:
2093 case BUILT_IN_MEMCPY_CHK:
2094 case BUILT_IN_MEMPCPY_CHK:
2095 case BUILT_IN_MEMMOVE_CHK:
2096 case BUILT_IN_MEMSET_CHK:
2098 tree dest = gimple_call_arg (stmt, 0);
2099 tree len = gimple_call_arg (stmt, 2);
2100 tree base = NULL_TREE;
2101 HOST_WIDE_INT offset = 0;
2102 if (!host_integerp (len, 0))
2103 return false;
2104 if (TREE_CODE (dest) == ADDR_EXPR)
2105 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
2106 &offset);
2107 else if (TREE_CODE (dest) == SSA_NAME)
2108 base = dest;
2109 if (base
2110 && base == ao_ref_base (ref))
2112 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
2113 if (offset <= ref->offset / BITS_PER_UNIT
2114 && (offset + size
2115 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
2116 / BITS_PER_UNIT)))
2117 return true;
2119 break;
2122 case BUILT_IN_VA_END:
2124 tree ptr = gimple_call_arg (stmt, 0);
2125 if (TREE_CODE (ptr) == ADDR_EXPR)
2127 tree base = ao_ref_base (ref);
2128 if (TREE_OPERAND (ptr, 0) == base)
2129 return true;
2131 break;
2134 default:;
2137 return false;
2140 bool
2141 stmt_kills_ref_p (gimple stmt, tree ref)
2143 ao_ref r;
2144 ao_ref_init (&r, ref);
2145 return stmt_kills_ref_p_1 (stmt, &r);
2149 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2150 TARGET or a statement clobbering the memory reference REF in which
2151 case false is returned. The walk starts with VUSE, one argument of PHI. */
2153 static bool
2154 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2155 tree vuse, unsigned int *cnt, bitmap *visited,
2156 bool abort_on_visited)
2158 basic_block bb = gimple_bb (phi);
2160 if (!*visited)
2161 *visited = BITMAP_ALLOC (NULL);
2163 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2165 /* Walk until we hit the target. */
2166 while (vuse != target)
2168 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2169 /* Recurse for PHI nodes. */
2170 if (gimple_code (def_stmt) == GIMPLE_PHI)
2172 /* An already visited PHI node ends the walk successfully. */
2173 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2174 return !abort_on_visited;
2175 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2176 visited, abort_on_visited);
2177 if (!vuse)
2178 return false;
2179 continue;
2181 else if (gimple_nop_p (def_stmt))
2182 return false;
2183 else
2185 /* A clobbering statement or the end of the IL ends it failing. */
2186 ++*cnt;
2187 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2188 return false;
2190 /* If we reach a new basic-block see if we already skipped it
2191 in a previous walk that ended successfully. */
2192 if (gimple_bb (def_stmt) != bb)
2194 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2195 return !abort_on_visited;
2196 bb = gimple_bb (def_stmt);
2198 vuse = gimple_vuse (def_stmt);
2200 return true;
2203 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2204 until we hit the phi argument definition that dominates the other one.
2205 Return that, or NULL_TREE if there is no such definition. */
2207 static tree
2208 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2209 ao_ref *ref, unsigned int *cnt,
2210 bitmap *visited, bool abort_on_visited)
2212 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2213 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2214 tree common_vuse;
2216 if (arg0 == arg1)
2217 return arg0;
2218 else if (gimple_nop_p (def0)
2219 || (!gimple_nop_p (def1)
2220 && dominated_by_p (CDI_DOMINATORS,
2221 gimple_bb (def1), gimple_bb (def0))))
2223 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2224 visited, abort_on_visited))
2225 return arg0;
2227 else if (gimple_nop_p (def1)
2228 || dominated_by_p (CDI_DOMINATORS,
2229 gimple_bb (def0), gimple_bb (def1)))
2231 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2232 visited, abort_on_visited))
2233 return arg1;
2235 /* Special case of a diamond:
2236 MEM_1 = ...
2237 goto (cond) ? L1 : L2
2238 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2239 goto L3
2240 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2241 L3: MEM_4 = PHI<MEM_2, MEM_3>
2242 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2243 dominate each other, but still we can easily skip this PHI node
2244 if we recognize that the vuse MEM operand is the same for both,
2245 and that we can skip both statements (they don't clobber us).
2246 This is still linear. Don't use maybe_skip_until, that might
2247 potentially be slow. */
2248 else if ((common_vuse = gimple_vuse (def0))
2249 && common_vuse == gimple_vuse (def1))
2251 *cnt += 2;
2252 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2253 && !stmt_may_clobber_ref_p_1 (def1, ref))
2254 return common_vuse;
2257 return NULL_TREE;
2261 /* Starting from a PHI node for the virtual operand of the memory reference
2262 REF find a continuation virtual operand that allows to continue walking
2263 statements dominating PHI skipping only statements that cannot possibly
2264 clobber REF. Increments *CNT for each alias disambiguation done.
2265 Returns NULL_TREE if no suitable virtual operand can be found. */
2267 tree
2268 get_continuation_for_phi (gimple phi, ao_ref *ref,
2269 unsigned int *cnt, bitmap *visited,
2270 bool abort_on_visited)
2272 unsigned nargs = gimple_phi_num_args (phi);
2274 /* Through a single-argument PHI we can simply look through. */
2275 if (nargs == 1)
2276 return PHI_ARG_DEF (phi, 0);
2278 /* For two or more arguments try to pairwise skip non-aliasing code
2279 until we hit the phi argument definition that dominates the other one. */
2280 else if (nargs >= 2)
2282 tree arg0, arg1;
2283 unsigned i;
2285 /* Find a candidate for the virtual operand which definition
2286 dominates those of all others. */
2287 arg0 = PHI_ARG_DEF (phi, 0);
2288 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2289 for (i = 1; i < nargs; ++i)
2291 arg1 = PHI_ARG_DEF (phi, i);
2292 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2294 arg0 = arg1;
2295 break;
2297 if (dominated_by_p (CDI_DOMINATORS,
2298 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2299 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2300 arg0 = arg1;
2303 /* Then pairwise reduce against the found candidate. */
2304 for (i = 0; i < nargs; ++i)
2306 arg1 = PHI_ARG_DEF (phi, i);
2307 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2308 cnt, visited, abort_on_visited);
2309 if (!arg0)
2310 return NULL_TREE;
2313 return arg0;
2316 return NULL_TREE;
2319 /* Based on the memory reference REF and its virtual use VUSE call
2320 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2321 itself. That is, for each virtual use for which its defining statement
2322 does not clobber REF.
2324 WALKER is called with REF, the current virtual use and DATA. If
2325 WALKER returns non-NULL the walk stops and its result is returned.
2326 At the end of a non-successful walk NULL is returned.
2328 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2329 use which definition is a statement that may clobber REF and DATA.
2330 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2331 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2332 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2333 to adjust REF and *DATA to make that valid.
2335 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2337 void *
2338 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2339 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2340 void *(*translate)(ao_ref *, tree, void *), void *data)
2342 bitmap visited = NULL;
2343 void *res;
2344 unsigned int cnt = 0;
2345 bool translated = false;
2347 timevar_push (TV_ALIAS_STMT_WALK);
2351 gimple def_stmt;
2353 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2354 res = (*walker) (ref, vuse, cnt, data);
2355 /* Abort walk. */
2356 if (res == (void *)-1)
2358 res = NULL;
2359 break;
2361 /* Lookup succeeded. */
2362 else if (res != NULL)
2363 break;
2365 def_stmt = SSA_NAME_DEF_STMT (vuse);
2366 if (gimple_nop_p (def_stmt))
2367 break;
2368 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2369 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2370 &visited, translated);
2371 else
2373 cnt++;
2374 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2376 if (!translate)
2377 break;
2378 res = (*translate) (ref, vuse, data);
2379 /* Failed lookup and translation. */
2380 if (res == (void *)-1)
2382 res = NULL;
2383 break;
2385 /* Lookup succeeded. */
2386 else if (res != NULL)
2387 break;
2388 /* Translation succeeded, continue walking. */
2389 translated = true;
2391 vuse = gimple_vuse (def_stmt);
2394 while (vuse);
2396 if (visited)
2397 BITMAP_FREE (visited);
2399 timevar_pop (TV_ALIAS_STMT_WALK);
2401 return res;
2405 /* Based on the memory reference REF call WALKER for each vdef which
2406 defining statement may clobber REF, starting with VDEF. If REF
2407 is NULL_TREE, each defining statement is visited.
2409 WALKER is called with REF, the current vdef and DATA. If WALKER
2410 returns true the walk is stopped, otherwise it continues.
2412 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2413 PHI argument (but only one walk continues on merge points), the
2414 return value is true if any of the walks was successful.
2416 The function returns the number of statements walked. */
2418 static unsigned int
2419 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2420 bool (*walker)(ao_ref *, tree, void *), void *data,
2421 bitmap *visited, unsigned int cnt)
2425 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2427 if (*visited
2428 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2429 return cnt;
2431 if (gimple_nop_p (def_stmt))
2432 return cnt;
2433 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2435 unsigned i;
2436 if (!*visited)
2437 *visited = BITMAP_ALLOC (NULL);
2438 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2439 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2440 walker, data, visited, 0);
2441 return cnt;
2444 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2445 cnt++;
2446 if ((!ref
2447 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2448 && (*walker) (ref, vdef, data))
2449 return cnt;
2451 vdef = gimple_vuse (def_stmt);
2453 while (1);
2456 unsigned int
2457 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2458 bool (*walker)(ao_ref *, tree, void *), void *data,
2459 bitmap *visited)
2461 bitmap local_visited = NULL;
2462 unsigned int ret;
2464 timevar_push (TV_ALIAS_STMT_WALK);
2466 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2467 visited ? visited : &local_visited, 0);
2468 if (local_visited)
2469 BITMAP_FREE (local_visited);
2471 timevar_pop (TV_ALIAS_STMT_WALK);
2473 return ret;