re PR c++/19476 (Missed null checking elimination with new)
[official-gcc.git] / gcc / tree-ssa-alias.c
blob0d3c15c447d3a040751a080c3f23f9af42a6cd13
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 "tree-ssa.h"
38 #include "tree-inline.h"
39 #include "params.h"
40 #include "vec.h"
41 #include "bitmap.h"
42 #include "pointer-set.h"
43 #include "alloc-pool.h"
44 #include "tree-ssa-alias.h"
46 /* Broad overview of how alias analysis on gimple works:
48 Statements clobbering or using memory are linked through the
49 virtual operand factored use-def chain. The virtual operand
50 is unique per function, its symbol is accessible via gimple_vop (cfun).
51 Virtual operands are used for efficiently walking memory statements
52 in the gimple IL and are useful for things like value-numbering as
53 a generation count for memory references.
55 SSA_NAME pointers may have associated points-to information
56 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
57 points-to information is (re-)computed by the TODO_rebuild_alias
58 pass manager todo. Points-to information is also used for more
59 precise tracking of call-clobbered and call-used variables and
60 related disambiguations.
62 This file contains functions for disambiguating memory references,
63 the so called alias-oracle and tools for walking of the gimple IL.
65 The main alias-oracle entry-points are
67 bool stmt_may_clobber_ref_p (gimple, tree)
69 This function queries if a statement may invalidate (parts of)
70 the memory designated by the reference tree argument.
72 bool ref_maybe_used_by_stmt_p (gimple, tree)
74 This function queries if a statement may need (parts of) the
75 memory designated by the reference tree argument.
77 There are variants of these functions that only handle the call
78 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
79 Note that these do not disambiguate against a possible call lhs.
81 bool refs_may_alias_p (tree, tree)
83 This function tries to disambiguate two reference trees.
85 bool ptr_deref_may_alias_global_p (tree)
87 This function queries if dereferencing a pointer variable may
88 alias global memory.
90 More low-level disambiguators are available and documented in
91 this file. Low-level disambiguators dealing with points-to
92 information are in tree-ssa-structalias.c. */
95 /* Query statistics for the different low-level disambiguators.
96 A high-level query may trigger multiple of them. */
98 static struct {
99 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
100 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
101 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
102 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
103 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
104 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
105 } alias_stats;
107 void
108 dump_alias_stats (FILE *s)
110 fprintf (s, "\nAlias oracle query stats:\n");
111 fprintf (s, " refs_may_alias_p: "
112 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
113 HOST_WIDE_INT_PRINT_DEC" queries\n",
114 alias_stats.refs_may_alias_p_no_alias,
115 alias_stats.refs_may_alias_p_no_alias
116 + alias_stats.refs_may_alias_p_may_alias);
117 fprintf (s, " ref_maybe_used_by_call_p: "
118 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
119 HOST_WIDE_INT_PRINT_DEC" queries\n",
120 alias_stats.ref_maybe_used_by_call_p_no_alias,
121 alias_stats.refs_may_alias_p_no_alias
122 + alias_stats.ref_maybe_used_by_call_p_may_alias);
123 fprintf (s, " call_may_clobber_ref_p: "
124 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
125 HOST_WIDE_INT_PRINT_DEC" queries\n",
126 alias_stats.call_may_clobber_ref_p_no_alias,
127 alias_stats.call_may_clobber_ref_p_no_alias
128 + alias_stats.call_may_clobber_ref_p_may_alias);
132 /* Return true, if dereferencing PTR may alias with a global variable. */
134 bool
135 ptr_deref_may_alias_global_p (tree ptr)
137 struct ptr_info_def *pi;
139 /* If we end up with a pointer constant here that may point
140 to global memory. */
141 if (TREE_CODE (ptr) != SSA_NAME)
142 return true;
144 pi = SSA_NAME_PTR_INFO (ptr);
146 /* If we do not have points-to information for this variable,
147 we have to punt. */
148 if (!pi)
149 return true;
151 /* ??? This does not use TBAA to prune globals ptr may not access. */
152 return pt_solution_includes_global (&pi->pt);
155 /* Return true if dereferencing PTR may alias DECL.
156 The caller is responsible for applying TBAA to see if PTR
157 may access DECL at all. */
159 static bool
160 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
162 struct ptr_info_def *pi;
164 /* Conversions are irrelevant for points-to information and
165 data-dependence analysis can feed us those. */
166 STRIP_NOPS (ptr);
168 /* Anything we do not explicilty handle aliases. */
169 if ((TREE_CODE (ptr) != SSA_NAME
170 && TREE_CODE (ptr) != ADDR_EXPR
171 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
172 || !POINTER_TYPE_P (TREE_TYPE (ptr))
173 || (TREE_CODE (decl) != VAR_DECL
174 && TREE_CODE (decl) != PARM_DECL
175 && TREE_CODE (decl) != RESULT_DECL))
176 return true;
178 /* Disregard pointer offsetting. */
179 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
183 ptr = TREE_OPERAND (ptr, 0);
185 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
186 return ptr_deref_may_alias_decl_p (ptr, decl);
189 /* ADDR_EXPR pointers either just offset another pointer or directly
190 specify the pointed-to set. */
191 if (TREE_CODE (ptr) == ADDR_EXPR)
193 tree base = get_base_address (TREE_OPERAND (ptr, 0));
194 if (base
195 && (TREE_CODE (base) == MEM_REF
196 || TREE_CODE (base) == TARGET_MEM_REF))
197 ptr = TREE_OPERAND (base, 0);
198 else if (base
199 && DECL_P (base))
200 return base == decl;
201 else if (base
202 && CONSTANT_CLASS_P (base))
203 return false;
204 else
205 return true;
208 /* Non-aliased variables can not be pointed to. */
209 if (!may_be_aliased (decl))
210 return false;
212 /* If we do not have useful points-to information for this pointer
213 we cannot disambiguate anything else. */
214 pi = SSA_NAME_PTR_INFO (ptr);
215 if (!pi)
216 return true;
218 return pt_solution_includes (&pi->pt, decl);
221 /* Return true if dereferenced PTR1 and PTR2 may alias.
222 The caller is responsible for applying TBAA to see if accesses
223 through PTR1 and PTR2 may conflict at all. */
225 bool
226 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
228 struct ptr_info_def *pi1, *pi2;
230 /* Conversions are irrelevant for points-to information and
231 data-dependence analysis can feed us those. */
232 STRIP_NOPS (ptr1);
233 STRIP_NOPS (ptr2);
235 /* Disregard pointer offsetting. */
236 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
240 ptr1 = TREE_OPERAND (ptr1, 0);
242 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
243 return ptr_derefs_may_alias_p (ptr1, ptr2);
245 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
249 ptr2 = TREE_OPERAND (ptr2, 0);
251 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
252 return ptr_derefs_may_alias_p (ptr1, ptr2);
255 /* ADDR_EXPR pointers either just offset another pointer or directly
256 specify the pointed-to set. */
257 if (TREE_CODE (ptr1) == ADDR_EXPR)
259 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
260 if (base
261 && (TREE_CODE (base) == MEM_REF
262 || TREE_CODE (base) == TARGET_MEM_REF))
263 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
264 else if (base
265 && DECL_P (base))
266 return ptr_deref_may_alias_decl_p (ptr2, base);
267 else
268 return true;
270 if (TREE_CODE (ptr2) == ADDR_EXPR)
272 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
273 if (base
274 && (TREE_CODE (base) == MEM_REF
275 || TREE_CODE (base) == TARGET_MEM_REF))
276 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
277 else if (base
278 && DECL_P (base))
279 return ptr_deref_may_alias_decl_p (ptr1, base);
280 else
281 return true;
284 /* From here we require SSA name pointers. Anything else aliases. */
285 if (TREE_CODE (ptr1) != SSA_NAME
286 || TREE_CODE (ptr2) != SSA_NAME
287 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
288 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
289 return true;
291 /* We may end up with two empty points-to solutions for two same pointers.
292 In this case we still want to say both pointers alias, so shortcut
293 that here. */
294 if (ptr1 == ptr2)
295 return true;
297 /* If we do not have useful points-to information for either pointer
298 we cannot disambiguate anything else. */
299 pi1 = SSA_NAME_PTR_INFO (ptr1);
300 pi2 = SSA_NAME_PTR_INFO (ptr2);
301 if (!pi1 || !pi2)
302 return true;
304 /* ??? This does not use TBAA to prune decls from the intersection
305 that not both pointers may access. */
306 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
309 /* Return true if dereferencing PTR may alias *REF.
310 The caller is responsible for applying TBAA to see if PTR
311 may access *REF at all. */
313 static bool
314 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
316 tree base = ao_ref_base (ref);
318 if (TREE_CODE (base) == MEM_REF
319 || TREE_CODE (base) == TARGET_MEM_REF)
320 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
321 else if (DECL_P (base))
322 return ptr_deref_may_alias_decl_p (ptr, base);
324 return true;
327 /* Return true whether REF may refer to global memory. */
329 bool
330 ref_may_alias_global_p (tree ref)
332 tree base = get_base_address (ref);
333 if (DECL_P (base))
334 return is_global_var (base);
335 else if (TREE_CODE (base) == MEM_REF
336 || TREE_CODE (base) == TARGET_MEM_REF)
337 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
338 return true;
341 /* Return true whether STMT may clobber global memory. */
343 bool
344 stmt_may_clobber_global_p (gimple stmt)
346 tree lhs;
348 if (!gimple_vdef (stmt))
349 return false;
351 /* ??? We can ask the oracle whether an artificial pointer
352 dereference with a pointer with points-to information covering
353 all global memory (what about non-address taken memory?) maybe
354 clobbered by this call. As there is at the moment no convenient
355 way of doing that without generating garbage do some manual
356 checking instead.
357 ??? We could make a NULL ao_ref argument to the various
358 predicates special, meaning any global memory. */
360 switch (gimple_code (stmt))
362 case GIMPLE_ASSIGN:
363 lhs = gimple_assign_lhs (stmt);
364 return (TREE_CODE (lhs) != SSA_NAME
365 && ref_may_alias_global_p (lhs));
366 case GIMPLE_CALL:
367 return true;
368 default:
369 return true;
374 /* Dump alias information on FILE. */
376 void
377 dump_alias_info (FILE *file)
379 unsigned i;
380 const char *funcname
381 = lang_hooks.decl_printable_name (current_function_decl, 2);
382 tree var;
384 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
386 fprintf (file, "Aliased symbols\n\n");
388 FOR_EACH_LOCAL_DECL (cfun, i, var)
390 if (may_be_aliased (var))
391 dump_variable (file, var);
394 fprintf (file, "\nCall clobber information\n");
396 fprintf (file, "\nESCAPED");
397 dump_points_to_solution (file, &cfun->gimple_df->escaped);
399 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
401 for (i = 1; i < num_ssa_names; i++)
403 tree ptr = ssa_name (i);
404 struct ptr_info_def *pi;
406 if (ptr == NULL_TREE
407 || !POINTER_TYPE_P (TREE_TYPE (ptr))
408 || SSA_NAME_IN_FREE_LIST (ptr))
409 continue;
411 pi = SSA_NAME_PTR_INFO (ptr);
412 if (pi)
413 dump_points_to_info_for (file, ptr);
416 fprintf (file, "\n");
420 /* Dump alias information on stderr. */
422 DEBUG_FUNCTION void
423 debug_alias_info (void)
425 dump_alias_info (stderr);
429 /* Dump the points-to set *PT into FILE. */
431 void
432 dump_points_to_solution (FILE *file, struct pt_solution *pt)
434 if (pt->anything)
435 fprintf (file, ", points-to anything");
437 if (pt->nonlocal)
438 fprintf (file, ", points-to non-local");
440 if (pt->escaped)
441 fprintf (file, ", points-to escaped");
443 if (pt->ipa_escaped)
444 fprintf (file, ", points-to unit escaped");
446 if (pt->null)
447 fprintf (file, ", points-to NULL");
449 if (pt->vars)
451 fprintf (file, ", points-to vars: ");
452 dump_decl_set (file, pt->vars);
453 if (pt->vars_contains_global)
454 fprintf (file, " (includes global vars)");
459 /* Unified dump function for pt_solution. */
461 DEBUG_FUNCTION void
462 debug (pt_solution &ref)
464 dump_points_to_solution (stderr, &ref);
467 DEBUG_FUNCTION void
468 debug (pt_solution *ptr)
470 if (ptr)
471 debug (*ptr);
472 else
473 fprintf (stderr, "<nil>\n");
477 /* Dump points-to information for SSA_NAME PTR into FILE. */
479 void
480 dump_points_to_info_for (FILE *file, tree ptr)
482 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
484 print_generic_expr (file, ptr, dump_flags);
486 if (pi)
487 dump_points_to_solution (file, &pi->pt);
488 else
489 fprintf (file, ", points-to anything");
491 fprintf (file, "\n");
495 /* Dump points-to information for VAR into stderr. */
497 DEBUG_FUNCTION void
498 debug_points_to_info_for (tree var)
500 dump_points_to_info_for (stderr, var);
504 /* Initializes the alias-oracle reference representation *R from REF. */
506 void
507 ao_ref_init (ao_ref *r, tree ref)
509 r->ref = ref;
510 r->base = NULL_TREE;
511 r->offset = 0;
512 r->size = -1;
513 r->max_size = -1;
514 r->ref_alias_set = -1;
515 r->base_alias_set = -1;
516 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
519 /* Returns the base object of the memory reference *REF. */
521 tree
522 ao_ref_base (ao_ref *ref)
524 if (ref->base)
525 return ref->base;
526 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
527 &ref->max_size);
528 return ref->base;
531 /* Returns the base object alias set of the memory reference *REF. */
533 static alias_set_type
534 ao_ref_base_alias_set (ao_ref *ref)
536 tree base_ref;
537 if (ref->base_alias_set != -1)
538 return ref->base_alias_set;
539 if (!ref->ref)
540 return 0;
541 base_ref = ref->ref;
542 while (handled_component_p (base_ref))
543 base_ref = TREE_OPERAND (base_ref, 0);
544 ref->base_alias_set = get_alias_set (base_ref);
545 return ref->base_alias_set;
548 /* Returns the reference alias set of the memory reference *REF. */
550 alias_set_type
551 ao_ref_alias_set (ao_ref *ref)
553 if (ref->ref_alias_set != -1)
554 return ref->ref_alias_set;
555 ref->ref_alias_set = get_alias_set (ref->ref);
556 return ref->ref_alias_set;
559 /* Init an alias-oracle reference representation from a gimple pointer
560 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
561 size is assumed to be unknown. The access is assumed to be only
562 to or after of the pointer target, not before it. */
564 void
565 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
567 HOST_WIDE_INT t1, t2;
568 ref->ref = NULL_TREE;
569 if (TREE_CODE (ptr) == ADDR_EXPR)
570 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
571 &ref->offset, &t1, &t2);
572 else
574 ref->base = build2 (MEM_REF, char_type_node,
575 ptr, null_pointer_node);
576 ref->offset = 0;
578 if (size
579 && host_integerp (size, 0)
580 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
581 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
582 else
583 ref->max_size = ref->size = -1;
584 ref->ref_alias_set = 0;
585 ref->base_alias_set = 0;
586 ref->volatile_p = false;
589 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
590 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
591 decide. */
593 static inline int
594 same_type_for_tbaa (tree type1, tree type2)
596 type1 = TYPE_MAIN_VARIANT (type1);
597 type2 = TYPE_MAIN_VARIANT (type2);
599 /* If we would have to do structural comparison bail out. */
600 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
601 || TYPE_STRUCTURAL_EQUALITY_P (type2))
602 return -1;
604 /* Compare the canonical types. */
605 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
606 return 1;
608 /* ??? Array types are not properly unified in all cases as we have
609 spurious changes in the index types for example. Removing this
610 causes all sorts of problems with the Fortran frontend. */
611 if (TREE_CODE (type1) == ARRAY_TYPE
612 && TREE_CODE (type2) == ARRAY_TYPE)
613 return -1;
615 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
616 object of one of its constrained subtypes, e.g. when a function with an
617 unconstrained parameter passed by reference is called on an object and
618 inlined. But, even in the case of a fixed size, type and subtypes are
619 not equivalent enough as to share the same TYPE_CANONICAL, since this
620 would mean that conversions between them are useless, whereas they are
621 not (e.g. type and subtypes can have different modes). So, in the end,
622 they are only guaranteed to have the same alias set. */
623 if (get_alias_set (type1) == get_alias_set (type2))
624 return -1;
626 /* The types are known to be not equal. */
627 return 0;
630 /* Determine if the two component references REF1 and REF2 which are
631 based on access types TYPE1 and TYPE2 and of which at least one is based
632 on an indirect reference may alias. REF2 is the only one that can
633 be a decl in which case REF2_IS_DECL is true.
634 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
635 are the respective alias sets. */
637 static bool
638 aliasing_component_refs_p (tree ref1,
639 alias_set_type ref1_alias_set,
640 alias_set_type base1_alias_set,
641 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
642 tree ref2,
643 alias_set_type ref2_alias_set,
644 alias_set_type base2_alias_set,
645 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
646 bool ref2_is_decl)
648 /* If one reference is a component references through pointers try to find a
649 common base and apply offset based disambiguation. This handles
650 for example
651 struct A { int i; int j; } *q;
652 struct B { struct A a; int k; } *p;
653 disambiguating q->i and p->a.j. */
654 tree base1, base2;
655 tree type1, type2;
656 tree *refp;
657 int same_p;
659 /* Choose bases and base types to search for. */
660 base1 = ref1;
661 while (handled_component_p (base1))
662 base1 = TREE_OPERAND (base1, 0);
663 type1 = TREE_TYPE (base1);
664 base2 = ref2;
665 while (handled_component_p (base2))
666 base2 = TREE_OPERAND (base2, 0);
667 type2 = TREE_TYPE (base2);
669 /* Now search for the type1 in the access path of ref2. This
670 would be a common base for doing offset based disambiguation on. */
671 refp = &ref2;
672 while (handled_component_p (*refp)
673 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
674 refp = &TREE_OPERAND (*refp, 0);
675 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
676 /* If we couldn't compare types we have to bail out. */
677 if (same_p == -1)
678 return true;
679 else if (same_p == 1)
681 HOST_WIDE_INT offadj, sztmp, msztmp;
682 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
683 offset2 -= offadj;
684 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
685 offset1 -= offadj;
686 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
688 /* If we didn't find a common base, try the other way around. */
689 refp = &ref1;
690 while (handled_component_p (*refp)
691 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
692 refp = &TREE_OPERAND (*refp, 0);
693 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
694 /* If we couldn't compare types we have to bail out. */
695 if (same_p == -1)
696 return true;
697 else if (same_p == 1)
699 HOST_WIDE_INT offadj, sztmp, msztmp;
700 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
701 offset1 -= offadj;
702 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
703 offset2 -= offadj;
704 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
707 /* If we have two type access paths B1.path1 and B2.path2 they may
708 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
709 But we can still have a path that goes B1.path1...B2.path2 with
710 a part that we do not see. So we can only disambiguate now
711 if there is no B2 in the tail of path1 and no B1 on the
712 tail of path2. */
713 if (base1_alias_set == ref2_alias_set
714 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
715 return true;
716 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
717 if (!ref2_is_decl)
718 return (base2_alias_set == ref1_alias_set
719 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
720 return false;
723 /* Return true if we can determine that component references REF1 and REF2,
724 that are within a common DECL, cannot overlap. */
726 static bool
727 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
729 vec<tree, va_stack> component_refs1;
730 vec<tree, va_stack> component_refs2;
732 vec_stack_alloc (tree, component_refs1, 16);
733 vec_stack_alloc (tree, component_refs2, 16);
735 /* Create the stack of handled components for REF1. */
736 while (handled_component_p (ref1))
738 component_refs1.safe_push (ref1);
739 ref1 = TREE_OPERAND (ref1, 0);
741 if (TREE_CODE (ref1) == MEM_REF)
743 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
744 goto may_overlap;
745 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
748 /* Create the stack of handled components for REF2. */
749 while (handled_component_p (ref2))
751 component_refs2.safe_push (ref2);
752 ref2 = TREE_OPERAND (ref2, 0);
754 if (TREE_CODE (ref2) == MEM_REF)
756 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
757 goto may_overlap;
758 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
761 /* We must have the same base DECL. */
762 gcc_assert (ref1 == ref2);
764 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
765 rank. This is sufficient because we start from the same DECL and you
766 cannot reference several fields at a time with COMPONENT_REFs (unlike
767 with ARRAY_RANGE_REFs for arrays) so you always need the same number
768 of them to access a sub-component, unless you're in a union, in which
769 case the return value will precisely be false. */
770 while (true)
774 if (component_refs1.is_empty ())
775 goto may_overlap;
776 ref1 = component_refs1.pop ();
778 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
782 if (component_refs2.is_empty ())
783 goto may_overlap;
784 ref2 = component_refs2.pop ();
786 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
788 /* Beware of BIT_FIELD_REF. */
789 if (TREE_CODE (ref1) != COMPONENT_REF
790 || TREE_CODE (ref2) != COMPONENT_REF)
791 goto may_overlap;
793 tree field1 = TREE_OPERAND (ref1, 1);
794 tree field2 = TREE_OPERAND (ref2, 1);
796 /* ??? We cannot simply use the type of operand #0 of the refs here
797 as the Fortran compiler smuggles type punning into COMPONENT_REFs
798 for common blocks instead of using unions like everyone else. */
799 tree type1 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field1));
800 tree type2 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field2));
802 /* We cannot disambiguate fields in a union or qualified union. */
803 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
804 goto may_overlap;
806 /* Different fields of the same record type cannot overlap. */
807 if (field1 != field2)
809 component_refs1.release ();
810 component_refs2.release ();
811 return true;
815 may_overlap:
816 component_refs1.release ();
817 component_refs2.release ();
818 return false;
821 /* Return true if two memory references based on the variables BASE1
822 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
823 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
824 if non-NULL are the complete memory reference trees. */
826 static bool
827 decl_refs_may_alias_p (tree ref1, tree base1,
828 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
829 tree ref2, tree base2,
830 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
832 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
834 /* If both references are based on different variables, they cannot alias. */
835 if (base1 != base2)
836 return false;
838 /* If both references are based on the same variable, they cannot alias if
839 the accesses do not overlap. */
840 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
841 return false;
843 /* For components with variable position, the above test isn't sufficient,
844 so we disambiguate component references manually. */
845 if (ref1 && ref2
846 && handled_component_p (ref1) && handled_component_p (ref2)
847 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
848 return false;
850 return true;
853 /* Return true if an indirect reference based on *PTR1 constrained
854 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
855 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
856 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
857 in which case they are computed on-demand. REF1 and REF2
858 if non-NULL are the complete memory reference trees. */
860 static bool
861 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
862 HOST_WIDE_INT offset1,
863 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
864 alias_set_type ref1_alias_set,
865 alias_set_type base1_alias_set,
866 tree ref2 ATTRIBUTE_UNUSED, tree base2,
867 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
868 alias_set_type ref2_alias_set,
869 alias_set_type base2_alias_set, bool tbaa_p)
871 tree ptr1;
872 tree ptrtype1, dbase2;
873 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
874 HOST_WIDE_INT doffset1, doffset2;
875 double_int moff;
877 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
878 || TREE_CODE (base1) == TARGET_MEM_REF)
879 && DECL_P (base2));
881 ptr1 = TREE_OPERAND (base1, 0);
883 /* The offset embedded in MEM_REFs can be negative. Bias them
884 so that the resulting offset adjustment is positive. */
885 moff = mem_ref_offset (base1);
886 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
887 if (moff.is_negative ())
888 offset2p += (-moff).low;
889 else
890 offset1p += moff.low;
892 /* If only one reference is based on a variable, they cannot alias if
893 the pointer access is beyond the extent of the variable access.
894 (the pointer base cannot validly point to an offset less than zero
895 of the variable).
896 ??? IVOPTs creates bases that do not honor this restriction,
897 so do not apply this optimization for TARGET_MEM_REFs. */
898 if (TREE_CODE (base1) != TARGET_MEM_REF
899 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
900 return false;
901 /* They also cannot alias if the pointer may not point to the decl. */
902 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
903 return false;
905 /* Disambiguations that rely on strict aliasing rules follow. */
906 if (!flag_strict_aliasing || !tbaa_p)
907 return true;
909 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
911 /* If the alias set for a pointer access is zero all bets are off. */
912 if (base1_alias_set == -1)
913 base1_alias_set = get_deref_alias_set (ptrtype1);
914 if (base1_alias_set == 0)
915 return true;
916 if (base2_alias_set == -1)
917 base2_alias_set = get_alias_set (base2);
919 /* When we are trying to disambiguate an access with a pointer dereference
920 as base versus one with a decl as base we can use both the size
921 of the decl and its dynamic type for extra disambiguation.
922 ??? We do not know anything about the dynamic type of the decl
923 other than that its alias-set contains base2_alias_set as a subset
924 which does not help us here. */
925 /* As we know nothing useful about the dynamic type of the decl just
926 use the usual conflict check rather than a subset test.
927 ??? We could introduce -fvery-strict-aliasing when the language
928 does not allow decls to have a dynamic type that differs from their
929 static type. Then we can check
930 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
931 if (base1_alias_set != base2_alias_set
932 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
933 return false;
934 /* If the size of the access relevant for TBAA through the pointer
935 is bigger than the size of the decl we can't possibly access the
936 decl via that pointer. */
937 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
938 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
939 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
940 /* ??? This in turn may run afoul when a decl of type T which is
941 a member of union type U is accessed through a pointer to
942 type U and sizeof T is smaller than sizeof U. */
943 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
944 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
945 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
946 return false;
948 if (!ref2)
949 return true;
951 /* If the decl is accessed via a MEM_REF, reconstruct the base
952 we can use for TBAA and an appropriately adjusted offset. */
953 dbase2 = ref2;
954 while (handled_component_p (dbase2))
955 dbase2 = TREE_OPERAND (dbase2, 0);
956 doffset1 = offset1;
957 doffset2 = offset2;
958 if (TREE_CODE (dbase2) == MEM_REF
959 || TREE_CODE (dbase2) == TARGET_MEM_REF)
961 double_int moff = mem_ref_offset (dbase2);
962 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
963 if (moff.is_negative ())
964 doffset1 -= (-moff).low;
965 else
966 doffset2 -= moff.low;
969 /* If either reference is view-converted, give up now. */
970 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
971 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
972 return true;
974 /* If both references are through the same type, they do not alias
975 if the accesses do not overlap. This does extra disambiguation
976 for mixed/pointer accesses but requires strict aliasing.
977 For MEM_REFs we require that the component-ref offset we computed
978 is relative to the start of the type which we ensure by
979 comparing rvalue and access type and disregarding the constant
980 pointer offset. */
981 if ((TREE_CODE (base1) != TARGET_MEM_REF
982 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
983 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
984 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
986 /* Do access-path based disambiguation. */
987 if (ref1 && ref2
988 && (handled_component_p (ref1) || handled_component_p (ref2)))
989 return aliasing_component_refs_p (ref1,
990 ref1_alias_set, base1_alias_set,
991 offset1, max_size1,
992 ref2,
993 ref2_alias_set, base2_alias_set,
994 offset2, max_size2, true);
996 return true;
999 /* Return true if two indirect references based on *PTR1
1000 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1001 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1002 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1003 in which case they are computed on-demand. REF1 and REF2
1004 if non-NULL are the complete memory reference trees. */
1006 static bool
1007 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1008 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1009 alias_set_type ref1_alias_set,
1010 alias_set_type base1_alias_set,
1011 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1012 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1013 alias_set_type ref2_alias_set,
1014 alias_set_type base2_alias_set, bool tbaa_p)
1016 tree ptr1;
1017 tree ptr2;
1018 tree ptrtype1, ptrtype2;
1020 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1021 || TREE_CODE (base1) == TARGET_MEM_REF)
1022 && (TREE_CODE (base2) == MEM_REF
1023 || TREE_CODE (base2) == TARGET_MEM_REF));
1025 ptr1 = TREE_OPERAND (base1, 0);
1026 ptr2 = TREE_OPERAND (base2, 0);
1028 /* If both bases are based on pointers they cannot alias if they may not
1029 point to the same memory object or if they point to the same object
1030 and the accesses do not overlap. */
1031 if ((!cfun || gimple_in_ssa_p (cfun))
1032 && operand_equal_p (ptr1, ptr2, 0)
1033 && (((TREE_CODE (base1) != TARGET_MEM_REF
1034 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1035 && (TREE_CODE (base2) != TARGET_MEM_REF
1036 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1037 || (TREE_CODE (base1) == TARGET_MEM_REF
1038 && TREE_CODE (base2) == TARGET_MEM_REF
1039 && (TMR_STEP (base1) == TMR_STEP (base2)
1040 || (TMR_STEP (base1) && TMR_STEP (base2)
1041 && operand_equal_p (TMR_STEP (base1),
1042 TMR_STEP (base2), 0)))
1043 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1044 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1045 && operand_equal_p (TMR_INDEX (base1),
1046 TMR_INDEX (base2), 0)))
1047 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1048 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1049 && operand_equal_p (TMR_INDEX2 (base1),
1050 TMR_INDEX2 (base2), 0))))))
1052 double_int moff;
1053 /* The offset embedded in MEM_REFs can be negative. Bias them
1054 so that the resulting offset adjustment is positive. */
1055 moff = mem_ref_offset (base1);
1056 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1057 if (moff.is_negative ())
1058 offset2 += (-moff).low;
1059 else
1060 offset1 += moff.low;
1061 moff = mem_ref_offset (base2);
1062 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1063 if (moff.is_negative ())
1064 offset1 += (-moff).low;
1065 else
1066 offset2 += moff.low;
1067 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1069 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1070 return false;
1072 /* Disambiguations that rely on strict aliasing rules follow. */
1073 if (!flag_strict_aliasing || !tbaa_p)
1074 return true;
1076 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1077 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1079 /* If the alias set for a pointer access is zero all bets are off. */
1080 if (base1_alias_set == -1)
1081 base1_alias_set = get_deref_alias_set (ptrtype1);
1082 if (base1_alias_set == 0)
1083 return true;
1084 if (base2_alias_set == -1)
1085 base2_alias_set = get_deref_alias_set (ptrtype2);
1086 if (base2_alias_set == 0)
1087 return true;
1089 /* If both references are through the same type, they do not alias
1090 if the accesses do not overlap. This does extra disambiguation
1091 for mixed/pointer accesses but requires strict aliasing. */
1092 if ((TREE_CODE (base1) != TARGET_MEM_REF
1093 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1094 && (TREE_CODE (base2) != TARGET_MEM_REF
1095 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1096 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1097 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1098 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1099 TREE_TYPE (ptrtype2)) == 1)
1100 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1102 /* Do type-based disambiguation. */
1103 if (base1_alias_set != base2_alias_set
1104 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1105 return false;
1107 /* Do access-path based disambiguation. */
1108 if (ref1 && ref2
1109 && (handled_component_p (ref1) || handled_component_p (ref2))
1110 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1111 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
1112 return aliasing_component_refs_p (ref1,
1113 ref1_alias_set, base1_alias_set,
1114 offset1, max_size1,
1115 ref2,
1116 ref2_alias_set, base2_alias_set,
1117 offset2, max_size2, false);
1119 return true;
1122 /* Return true, if the two memory references REF1 and REF2 may alias. */
1124 bool
1125 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1127 tree base1, base2;
1128 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1129 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1130 bool var1_p, var2_p, ind1_p, ind2_p;
1132 gcc_checking_assert ((!ref1->ref
1133 || TREE_CODE (ref1->ref) == SSA_NAME
1134 || DECL_P (ref1->ref)
1135 || TREE_CODE (ref1->ref) == STRING_CST
1136 || handled_component_p (ref1->ref)
1137 || TREE_CODE (ref1->ref) == MEM_REF
1138 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1139 && (!ref2->ref
1140 || TREE_CODE (ref2->ref) == SSA_NAME
1141 || DECL_P (ref2->ref)
1142 || TREE_CODE (ref2->ref) == STRING_CST
1143 || handled_component_p (ref2->ref)
1144 || TREE_CODE (ref2->ref) == MEM_REF
1145 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1147 /* Decompose the references into their base objects and the access. */
1148 base1 = ao_ref_base (ref1);
1149 offset1 = ref1->offset;
1150 max_size1 = ref1->max_size;
1151 base2 = ao_ref_base (ref2);
1152 offset2 = ref2->offset;
1153 max_size2 = ref2->max_size;
1155 /* We can end up with registers or constants as bases for example from
1156 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1157 which is seen as a struct copy. */
1158 if (TREE_CODE (base1) == SSA_NAME
1159 || TREE_CODE (base1) == CONST_DECL
1160 || TREE_CODE (base1) == CONSTRUCTOR
1161 || TREE_CODE (base1) == ADDR_EXPR
1162 || CONSTANT_CLASS_P (base1)
1163 || TREE_CODE (base2) == SSA_NAME
1164 || TREE_CODE (base2) == CONST_DECL
1165 || TREE_CODE (base2) == CONSTRUCTOR
1166 || TREE_CODE (base2) == ADDR_EXPR
1167 || CONSTANT_CLASS_P (base2))
1168 return false;
1170 /* We can end up referring to code via function and label decls.
1171 As we likely do not properly track code aliases conservatively
1172 bail out. */
1173 if (TREE_CODE (base1) == FUNCTION_DECL
1174 || TREE_CODE (base1) == LABEL_DECL
1175 || TREE_CODE (base2) == FUNCTION_DECL
1176 || TREE_CODE (base2) == LABEL_DECL)
1177 return true;
1179 /* Two volatile accesses always conflict. */
1180 if (ref1->volatile_p
1181 && ref2->volatile_p)
1182 return true;
1184 /* Defer to simple offset based disambiguation if we have
1185 references based on two decls. Do this before defering to
1186 TBAA to handle must-alias cases in conformance with the
1187 GCC extension of allowing type-punning through unions. */
1188 var1_p = DECL_P (base1);
1189 var2_p = DECL_P (base2);
1190 if (var1_p && var2_p)
1191 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1192 ref2->ref, base2, offset2, max_size2);
1194 ind1_p = (TREE_CODE (base1) == MEM_REF
1195 || TREE_CODE (base1) == TARGET_MEM_REF);
1196 ind2_p = (TREE_CODE (base2) == MEM_REF
1197 || TREE_CODE (base2) == TARGET_MEM_REF);
1199 /* Canonicalize the pointer-vs-decl case. */
1200 if (ind1_p && var2_p)
1202 HOST_WIDE_INT tmp1;
1203 tree tmp2;
1204 ao_ref *tmp3;
1205 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1206 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1207 tmp2 = base1; base1 = base2; base2 = tmp2;
1208 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1209 var1_p = true;
1210 ind1_p = false;
1211 var2_p = false;
1212 ind2_p = true;
1215 /* First defer to TBAA if possible. */
1216 if (tbaa_p
1217 && flag_strict_aliasing
1218 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1219 ao_ref_alias_set (ref2)))
1220 return false;
1222 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1223 if (var1_p && ind2_p)
1224 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1225 offset2, max_size2,
1226 ao_ref_alias_set (ref2), -1,
1227 ref1->ref, base1,
1228 offset1, max_size1,
1229 ao_ref_alias_set (ref1),
1230 ao_ref_base_alias_set (ref1),
1231 tbaa_p);
1232 else if (ind1_p && ind2_p)
1233 return indirect_refs_may_alias_p (ref1->ref, base1,
1234 offset1, max_size1,
1235 ao_ref_alias_set (ref1), -1,
1236 ref2->ref, base2,
1237 offset2, max_size2,
1238 ao_ref_alias_set (ref2), -1,
1239 tbaa_p);
1241 /* We really do not want to end up here, but returning true is safe. */
1242 #ifdef ENABLE_CHECKING
1243 gcc_unreachable ();
1244 #else
1245 return true;
1246 #endif
1249 bool
1250 refs_may_alias_p (tree ref1, tree ref2)
1252 ao_ref r1, r2;
1253 bool res;
1254 ao_ref_init (&r1, ref1);
1255 ao_ref_init (&r2, ref2);
1256 res = refs_may_alias_p_1 (&r1, &r2, true);
1257 if (res)
1258 ++alias_stats.refs_may_alias_p_may_alias;
1259 else
1260 ++alias_stats.refs_may_alias_p_no_alias;
1261 return res;
1264 /* Returns true if there is a anti-dependence for the STORE that
1265 executes after the LOAD. */
1267 bool
1268 refs_anti_dependent_p (tree load, tree store)
1270 ao_ref r1, r2;
1271 ao_ref_init (&r1, load);
1272 ao_ref_init (&r2, store);
1273 return refs_may_alias_p_1 (&r1, &r2, false);
1276 /* Returns true if there is a output dependence for the stores
1277 STORE1 and STORE2. */
1279 bool
1280 refs_output_dependent_p (tree store1, tree store2)
1282 ao_ref r1, r2;
1283 ao_ref_init (&r1, store1);
1284 ao_ref_init (&r2, store2);
1285 return refs_may_alias_p_1 (&r1, &r2, false);
1288 /* If the call CALL may use the memory reference REF return true,
1289 otherwise return false. */
1291 static bool
1292 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1294 tree base, callee;
1295 unsigned i;
1296 int flags = gimple_call_flags (call);
1298 /* Const functions without a static chain do not implicitly use memory. */
1299 if (!gimple_call_chain (call)
1300 && (flags & (ECF_CONST|ECF_NOVOPS)))
1301 goto process_args;
1303 base = ao_ref_base (ref);
1304 if (!base)
1305 return true;
1307 /* A call that is not without side-effects might involve volatile
1308 accesses and thus conflicts with all other volatile accesses. */
1309 if (ref->volatile_p)
1310 return true;
1312 /* If the reference is based on a decl that is not aliased the call
1313 cannot possibly use it. */
1314 if (DECL_P (base)
1315 && !may_be_aliased (base)
1316 /* But local statics can be used through recursion. */
1317 && !is_global_var (base))
1318 goto process_args;
1320 callee = gimple_call_fndecl (call);
1322 /* Handle those builtin functions explicitly that do not act as
1323 escape points. See tree-ssa-structalias.c:find_func_aliases
1324 for the list of builtins we might need to handle here. */
1325 if (callee != NULL_TREE
1326 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1327 switch (DECL_FUNCTION_CODE (callee))
1329 /* All the following functions read memory pointed to by
1330 their second argument. strcat/strncat additionally
1331 reads memory pointed to by the first argument. */
1332 case BUILT_IN_STRCAT:
1333 case BUILT_IN_STRNCAT:
1335 ao_ref dref;
1336 ao_ref_init_from_ptr_and_size (&dref,
1337 gimple_call_arg (call, 0),
1338 NULL_TREE);
1339 if (refs_may_alias_p_1 (&dref, ref, false))
1340 return true;
1342 /* FALLTHRU */
1343 case BUILT_IN_STRCPY:
1344 case BUILT_IN_STRNCPY:
1345 case BUILT_IN_MEMCPY:
1346 case BUILT_IN_MEMMOVE:
1347 case BUILT_IN_MEMPCPY:
1348 case BUILT_IN_STPCPY:
1349 case BUILT_IN_STPNCPY:
1350 case BUILT_IN_TM_MEMCPY:
1351 case BUILT_IN_TM_MEMMOVE:
1353 ao_ref dref;
1354 tree size = NULL_TREE;
1355 if (gimple_call_num_args (call) == 3)
1356 size = gimple_call_arg (call, 2);
1357 ao_ref_init_from_ptr_and_size (&dref,
1358 gimple_call_arg (call, 1),
1359 size);
1360 return refs_may_alias_p_1 (&dref, ref, false);
1362 case BUILT_IN_STRCAT_CHK:
1363 case BUILT_IN_STRNCAT_CHK:
1365 ao_ref dref;
1366 ao_ref_init_from_ptr_and_size (&dref,
1367 gimple_call_arg (call, 0),
1368 NULL_TREE);
1369 if (refs_may_alias_p_1 (&dref, ref, false))
1370 return true;
1372 /* FALLTHRU */
1373 case BUILT_IN_STRCPY_CHK:
1374 case BUILT_IN_STRNCPY_CHK:
1375 case BUILT_IN_MEMCPY_CHK:
1376 case BUILT_IN_MEMMOVE_CHK:
1377 case BUILT_IN_MEMPCPY_CHK:
1378 case BUILT_IN_STPCPY_CHK:
1379 case BUILT_IN_STPNCPY_CHK:
1381 ao_ref dref;
1382 tree size = NULL_TREE;
1383 if (gimple_call_num_args (call) == 4)
1384 size = gimple_call_arg (call, 2);
1385 ao_ref_init_from_ptr_and_size (&dref,
1386 gimple_call_arg (call, 1),
1387 size);
1388 return refs_may_alias_p_1 (&dref, ref, false);
1390 case BUILT_IN_BCOPY:
1392 ao_ref dref;
1393 tree size = gimple_call_arg (call, 2);
1394 ao_ref_init_from_ptr_and_size (&dref,
1395 gimple_call_arg (call, 0),
1396 size);
1397 return refs_may_alias_p_1 (&dref, ref, false);
1400 /* The following functions read memory pointed to by their
1401 first argument. */
1402 CASE_BUILT_IN_TM_LOAD (1):
1403 CASE_BUILT_IN_TM_LOAD (2):
1404 CASE_BUILT_IN_TM_LOAD (4):
1405 CASE_BUILT_IN_TM_LOAD (8):
1406 CASE_BUILT_IN_TM_LOAD (FLOAT):
1407 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1408 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1409 CASE_BUILT_IN_TM_LOAD (M64):
1410 CASE_BUILT_IN_TM_LOAD (M128):
1411 CASE_BUILT_IN_TM_LOAD (M256):
1412 case BUILT_IN_TM_LOG:
1413 case BUILT_IN_TM_LOG_1:
1414 case BUILT_IN_TM_LOG_2:
1415 case BUILT_IN_TM_LOG_4:
1416 case BUILT_IN_TM_LOG_8:
1417 case BUILT_IN_TM_LOG_FLOAT:
1418 case BUILT_IN_TM_LOG_DOUBLE:
1419 case BUILT_IN_TM_LOG_LDOUBLE:
1420 case BUILT_IN_TM_LOG_M64:
1421 case BUILT_IN_TM_LOG_M128:
1422 case BUILT_IN_TM_LOG_M256:
1423 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1425 /* These read memory pointed to by the first argument. */
1426 case BUILT_IN_STRDUP:
1427 case BUILT_IN_STRNDUP:
1429 ao_ref dref;
1430 tree size = NULL_TREE;
1431 if (gimple_call_num_args (call) == 2)
1432 size = gimple_call_arg (call, 1);
1433 ao_ref_init_from_ptr_and_size (&dref,
1434 gimple_call_arg (call, 0),
1435 size);
1436 return refs_may_alias_p_1 (&dref, ref, false);
1438 /* These read memory pointed to by the first argument. */
1439 case BUILT_IN_INDEX:
1440 case BUILT_IN_STRCHR:
1441 case BUILT_IN_STRRCHR:
1443 ao_ref dref;
1444 ao_ref_init_from_ptr_and_size (&dref,
1445 gimple_call_arg (call, 0),
1446 NULL_TREE);
1447 return refs_may_alias_p_1 (&dref, ref, false);
1449 /* These read memory pointed to by the first argument with size
1450 in the third argument. */
1451 case BUILT_IN_MEMCHR:
1453 ao_ref dref;
1454 ao_ref_init_from_ptr_and_size (&dref,
1455 gimple_call_arg (call, 0),
1456 gimple_call_arg (call, 2));
1457 return refs_may_alias_p_1 (&dref, ref, false);
1459 /* These read memory pointed to by the first and second arguments. */
1460 case BUILT_IN_STRSTR:
1461 case BUILT_IN_STRPBRK:
1463 ao_ref dref;
1464 ao_ref_init_from_ptr_and_size (&dref,
1465 gimple_call_arg (call, 0),
1466 NULL_TREE);
1467 if (refs_may_alias_p_1 (&dref, ref, false))
1468 return true;
1469 ao_ref_init_from_ptr_and_size (&dref,
1470 gimple_call_arg (call, 1),
1471 NULL_TREE);
1472 return refs_may_alias_p_1 (&dref, ref, false);
1475 /* The following builtins do not read from memory. */
1476 case BUILT_IN_FREE:
1477 case BUILT_IN_MALLOC:
1478 case BUILT_IN_CALLOC:
1479 case BUILT_IN_ALLOCA:
1480 case BUILT_IN_ALLOCA_WITH_ALIGN:
1481 case BUILT_IN_STACK_SAVE:
1482 case BUILT_IN_STACK_RESTORE:
1483 case BUILT_IN_MEMSET:
1484 case BUILT_IN_TM_MEMSET:
1485 case BUILT_IN_MEMSET_CHK:
1486 case BUILT_IN_FREXP:
1487 case BUILT_IN_FREXPF:
1488 case BUILT_IN_FREXPL:
1489 case BUILT_IN_GAMMA_R:
1490 case BUILT_IN_GAMMAF_R:
1491 case BUILT_IN_GAMMAL_R:
1492 case BUILT_IN_LGAMMA_R:
1493 case BUILT_IN_LGAMMAF_R:
1494 case BUILT_IN_LGAMMAL_R:
1495 case BUILT_IN_MODF:
1496 case BUILT_IN_MODFF:
1497 case BUILT_IN_MODFL:
1498 case BUILT_IN_REMQUO:
1499 case BUILT_IN_REMQUOF:
1500 case BUILT_IN_REMQUOL:
1501 case BUILT_IN_SINCOS:
1502 case BUILT_IN_SINCOSF:
1503 case BUILT_IN_SINCOSL:
1504 case BUILT_IN_ASSUME_ALIGNED:
1505 case BUILT_IN_VA_END:
1506 return false;
1507 /* __sync_* builtins and some OpenMP builtins act as threading
1508 barriers. */
1509 #undef DEF_SYNC_BUILTIN
1510 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1511 #include "sync-builtins.def"
1512 #undef DEF_SYNC_BUILTIN
1513 case BUILT_IN_GOMP_ATOMIC_START:
1514 case BUILT_IN_GOMP_ATOMIC_END:
1515 case BUILT_IN_GOMP_BARRIER:
1516 case BUILT_IN_GOMP_TASKWAIT:
1517 case BUILT_IN_GOMP_CRITICAL_START:
1518 case BUILT_IN_GOMP_CRITICAL_END:
1519 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1520 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1521 case BUILT_IN_GOMP_LOOP_END:
1522 case BUILT_IN_GOMP_ORDERED_START:
1523 case BUILT_IN_GOMP_ORDERED_END:
1524 case BUILT_IN_GOMP_PARALLEL_END:
1525 case BUILT_IN_GOMP_SECTIONS_END:
1526 case BUILT_IN_GOMP_SINGLE_COPY_START:
1527 case BUILT_IN_GOMP_SINGLE_COPY_END:
1528 return true;
1530 default:
1531 /* Fallthru to general call handling. */;
1534 /* Check if base is a global static variable that is not read
1535 by the function. */
1536 if (callee != NULL_TREE
1537 && TREE_CODE (base) == VAR_DECL
1538 && TREE_STATIC (base))
1540 struct cgraph_node *node = cgraph_get_node (callee);
1541 bitmap not_read;
1543 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1544 node yet. We should enforce that there are nodes for all decls in the
1545 IL and remove this check instead. */
1546 if (node
1547 && (not_read = ipa_reference_get_not_read_global (node))
1548 && bitmap_bit_p (not_read, DECL_UID (base)))
1549 goto process_args;
1552 /* Check if the base variable is call-used. */
1553 if (DECL_P (base))
1555 if (pt_solution_includes (gimple_call_use_set (call), base))
1556 return true;
1558 else if ((TREE_CODE (base) == MEM_REF
1559 || TREE_CODE (base) == TARGET_MEM_REF)
1560 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1562 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1563 if (!pi)
1564 return true;
1566 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1567 return true;
1569 else
1570 return true;
1572 /* Inspect call arguments for passed-by-value aliases. */
1573 process_args:
1574 for (i = 0; i < gimple_call_num_args (call); ++i)
1576 tree op = gimple_call_arg (call, i);
1577 int flags = gimple_call_arg_flags (call, i);
1579 if (flags & EAF_UNUSED)
1580 continue;
1582 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1583 op = TREE_OPERAND (op, 0);
1585 if (TREE_CODE (op) != SSA_NAME
1586 && !is_gimple_min_invariant (op))
1588 ao_ref r;
1589 ao_ref_init (&r, op);
1590 if (refs_may_alias_p_1 (&r, ref, true))
1591 return true;
1595 return false;
1598 static bool
1599 ref_maybe_used_by_call_p (gimple call, tree ref)
1601 ao_ref r;
1602 bool res;
1603 ao_ref_init (&r, ref);
1604 res = ref_maybe_used_by_call_p_1 (call, &r);
1605 if (res)
1606 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1607 else
1608 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1609 return res;
1613 /* If the statement STMT may use the memory reference REF return
1614 true, otherwise return false. */
1616 bool
1617 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1619 if (is_gimple_assign (stmt))
1621 tree rhs;
1623 /* All memory assign statements are single. */
1624 if (!gimple_assign_single_p (stmt))
1625 return false;
1627 rhs = gimple_assign_rhs1 (stmt);
1628 if (is_gimple_reg (rhs)
1629 || is_gimple_min_invariant (rhs)
1630 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1631 return false;
1633 return refs_may_alias_p (rhs, ref);
1635 else if (is_gimple_call (stmt))
1636 return ref_maybe_used_by_call_p (stmt, ref);
1637 else if (gimple_code (stmt) == GIMPLE_RETURN)
1639 tree retval = gimple_return_retval (stmt);
1640 tree base;
1641 if (retval
1642 && TREE_CODE (retval) != SSA_NAME
1643 && !is_gimple_min_invariant (retval)
1644 && refs_may_alias_p (retval, ref))
1645 return true;
1646 /* If ref escapes the function then the return acts as a use. */
1647 base = get_base_address (ref);
1648 if (!base)
1650 else if (DECL_P (base))
1651 return is_global_var (base);
1652 else if (TREE_CODE (base) == MEM_REF
1653 || TREE_CODE (base) == TARGET_MEM_REF)
1654 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1655 return false;
1658 return true;
1661 /* If the call in statement CALL may clobber the memory reference REF
1662 return true, otherwise return false. */
1664 static bool
1665 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1667 tree base;
1668 tree callee;
1670 /* If the call is pure or const it cannot clobber anything. */
1671 if (gimple_call_flags (call)
1672 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1673 return false;
1675 base = ao_ref_base (ref);
1676 if (!base)
1677 return true;
1679 if (TREE_CODE (base) == SSA_NAME
1680 || CONSTANT_CLASS_P (base))
1681 return false;
1683 /* A call that is not without side-effects might involve volatile
1684 accesses and thus conflicts with all other volatile accesses. */
1685 if (ref->volatile_p)
1686 return true;
1688 /* If the reference is based on a decl that is not aliased the call
1689 cannot possibly clobber it. */
1690 if (DECL_P (base)
1691 && !may_be_aliased (base)
1692 /* But local non-readonly statics can be modified through recursion
1693 or the call may implement a threading barrier which we must
1694 treat as may-def. */
1695 && (TREE_READONLY (base)
1696 || !is_global_var (base)))
1697 return false;
1699 callee = gimple_call_fndecl (call);
1701 /* Handle those builtin functions explicitly that do not act as
1702 escape points. See tree-ssa-structalias.c:find_func_aliases
1703 for the list of builtins we might need to handle here. */
1704 if (callee != NULL_TREE
1705 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1706 switch (DECL_FUNCTION_CODE (callee))
1708 /* All the following functions clobber memory pointed to by
1709 their first argument. */
1710 case BUILT_IN_STRCPY:
1711 case BUILT_IN_STRNCPY:
1712 case BUILT_IN_MEMCPY:
1713 case BUILT_IN_MEMMOVE:
1714 case BUILT_IN_MEMPCPY:
1715 case BUILT_IN_STPCPY:
1716 case BUILT_IN_STPNCPY:
1717 case BUILT_IN_STRCAT:
1718 case BUILT_IN_STRNCAT:
1719 case BUILT_IN_MEMSET:
1720 case BUILT_IN_TM_MEMSET:
1721 CASE_BUILT_IN_TM_STORE (1):
1722 CASE_BUILT_IN_TM_STORE (2):
1723 CASE_BUILT_IN_TM_STORE (4):
1724 CASE_BUILT_IN_TM_STORE (8):
1725 CASE_BUILT_IN_TM_STORE (FLOAT):
1726 CASE_BUILT_IN_TM_STORE (DOUBLE):
1727 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1728 CASE_BUILT_IN_TM_STORE (M64):
1729 CASE_BUILT_IN_TM_STORE (M128):
1730 CASE_BUILT_IN_TM_STORE (M256):
1731 case BUILT_IN_TM_MEMCPY:
1732 case BUILT_IN_TM_MEMMOVE:
1734 ao_ref dref;
1735 tree size = NULL_TREE;
1736 /* Don't pass in size for strncat, as the maximum size
1737 is strlen (dest) + n + 1 instead of n, resp.
1738 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1739 known. */
1740 if (gimple_call_num_args (call) == 3
1741 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1742 size = gimple_call_arg (call, 2);
1743 ao_ref_init_from_ptr_and_size (&dref,
1744 gimple_call_arg (call, 0),
1745 size);
1746 return refs_may_alias_p_1 (&dref, ref, false);
1748 case BUILT_IN_STRCPY_CHK:
1749 case BUILT_IN_STRNCPY_CHK:
1750 case BUILT_IN_MEMCPY_CHK:
1751 case BUILT_IN_MEMMOVE_CHK:
1752 case BUILT_IN_MEMPCPY_CHK:
1753 case BUILT_IN_STPCPY_CHK:
1754 case BUILT_IN_STPNCPY_CHK:
1755 case BUILT_IN_STRCAT_CHK:
1756 case BUILT_IN_STRNCAT_CHK:
1757 case BUILT_IN_MEMSET_CHK:
1759 ao_ref dref;
1760 tree size = NULL_TREE;
1761 /* Don't pass in size for __strncat_chk, as the maximum size
1762 is strlen (dest) + n + 1 instead of n, resp.
1763 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1764 known. */
1765 if (gimple_call_num_args (call) == 4
1766 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1767 size = gimple_call_arg (call, 2);
1768 ao_ref_init_from_ptr_and_size (&dref,
1769 gimple_call_arg (call, 0),
1770 size);
1771 return refs_may_alias_p_1 (&dref, ref, false);
1773 case BUILT_IN_BCOPY:
1775 ao_ref dref;
1776 tree size = gimple_call_arg (call, 2);
1777 ao_ref_init_from_ptr_and_size (&dref,
1778 gimple_call_arg (call, 1),
1779 size);
1780 return refs_may_alias_p_1 (&dref, ref, false);
1782 /* Allocating memory does not have any side-effects apart from
1783 being the definition point for the pointer. */
1784 case BUILT_IN_MALLOC:
1785 case BUILT_IN_CALLOC:
1786 case BUILT_IN_STRDUP:
1787 case BUILT_IN_STRNDUP:
1788 /* Unix98 specifies that errno is set on allocation failure. */
1789 if (flag_errno_math
1790 && targetm.ref_may_alias_errno (ref))
1791 return true;
1792 return false;
1793 case BUILT_IN_STACK_SAVE:
1794 case BUILT_IN_ALLOCA:
1795 case BUILT_IN_ALLOCA_WITH_ALIGN:
1796 case BUILT_IN_ASSUME_ALIGNED:
1797 return false;
1798 /* Freeing memory kills the pointed-to memory. More importantly
1799 the call has to serve as a barrier for moving loads and stores
1800 across it. */
1801 case BUILT_IN_FREE:
1802 case BUILT_IN_VA_END:
1804 tree ptr = gimple_call_arg (call, 0);
1805 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1807 case BUILT_IN_GAMMA_R:
1808 case BUILT_IN_GAMMAF_R:
1809 case BUILT_IN_GAMMAL_R:
1810 case BUILT_IN_LGAMMA_R:
1811 case BUILT_IN_LGAMMAF_R:
1812 case BUILT_IN_LGAMMAL_R:
1814 tree out = gimple_call_arg (call, 1);
1815 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1816 return true;
1817 if (flag_errno_math)
1818 break;
1819 return false;
1821 case BUILT_IN_FREXP:
1822 case BUILT_IN_FREXPF:
1823 case BUILT_IN_FREXPL:
1824 case BUILT_IN_MODF:
1825 case BUILT_IN_MODFF:
1826 case BUILT_IN_MODFL:
1828 tree out = gimple_call_arg (call, 1);
1829 return ptr_deref_may_alias_ref_p_1 (out, ref);
1831 case BUILT_IN_REMQUO:
1832 case BUILT_IN_REMQUOF:
1833 case BUILT_IN_REMQUOL:
1835 tree out = gimple_call_arg (call, 2);
1836 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1837 return true;
1838 if (flag_errno_math)
1839 break;
1840 return false;
1842 case BUILT_IN_SINCOS:
1843 case BUILT_IN_SINCOSF:
1844 case BUILT_IN_SINCOSL:
1846 tree sin = gimple_call_arg (call, 1);
1847 tree cos = gimple_call_arg (call, 2);
1848 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1849 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1851 /* __sync_* builtins and some OpenMP builtins act as threading
1852 barriers. */
1853 #undef DEF_SYNC_BUILTIN
1854 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1855 #include "sync-builtins.def"
1856 #undef DEF_SYNC_BUILTIN
1857 case BUILT_IN_GOMP_ATOMIC_START:
1858 case BUILT_IN_GOMP_ATOMIC_END:
1859 case BUILT_IN_GOMP_BARRIER:
1860 case BUILT_IN_GOMP_TASKWAIT:
1861 case BUILT_IN_GOMP_CRITICAL_START:
1862 case BUILT_IN_GOMP_CRITICAL_END:
1863 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1864 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1865 case BUILT_IN_GOMP_LOOP_END:
1866 case BUILT_IN_GOMP_ORDERED_START:
1867 case BUILT_IN_GOMP_ORDERED_END:
1868 case BUILT_IN_GOMP_PARALLEL_END:
1869 case BUILT_IN_GOMP_SECTIONS_END:
1870 case BUILT_IN_GOMP_SINGLE_COPY_START:
1871 case BUILT_IN_GOMP_SINGLE_COPY_END:
1872 return true;
1873 default:
1874 /* Fallthru to general call handling. */;
1877 /* Check if base is a global static variable that is not written
1878 by the function. */
1879 if (callee != NULL_TREE
1880 && TREE_CODE (base) == VAR_DECL
1881 && TREE_STATIC (base))
1883 struct cgraph_node *node = cgraph_get_node (callee);
1884 bitmap not_written;
1886 if (node
1887 && (not_written = ipa_reference_get_not_written_global (node))
1888 && bitmap_bit_p (not_written, DECL_UID (base)))
1889 return false;
1892 /* Check if the base variable is call-clobbered. */
1893 if (DECL_P (base))
1894 return pt_solution_includes (gimple_call_clobber_set (call), base);
1895 else if ((TREE_CODE (base) == MEM_REF
1896 || TREE_CODE (base) == TARGET_MEM_REF)
1897 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1899 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1900 if (!pi)
1901 return true;
1903 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1906 return true;
1909 /* If the call in statement CALL may clobber the memory reference REF
1910 return true, otherwise return false. */
1912 bool
1913 call_may_clobber_ref_p (gimple call, tree ref)
1915 bool res;
1916 ao_ref r;
1917 ao_ref_init (&r, ref);
1918 res = call_may_clobber_ref_p_1 (call, &r);
1919 if (res)
1920 ++alias_stats.call_may_clobber_ref_p_may_alias;
1921 else
1922 ++alias_stats.call_may_clobber_ref_p_no_alias;
1923 return res;
1927 /* If the statement STMT may clobber the memory reference REF return true,
1928 otherwise return false. */
1930 bool
1931 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1933 if (is_gimple_call (stmt))
1935 tree lhs = gimple_call_lhs (stmt);
1936 if (lhs
1937 && TREE_CODE (lhs) != SSA_NAME)
1939 ao_ref r;
1940 ao_ref_init (&r, lhs);
1941 if (refs_may_alias_p_1 (ref, &r, true))
1942 return true;
1945 return call_may_clobber_ref_p_1 (stmt, ref);
1947 else if (gimple_assign_single_p (stmt))
1949 tree lhs = gimple_assign_lhs (stmt);
1950 if (TREE_CODE (lhs) != SSA_NAME)
1952 ao_ref r;
1953 ao_ref_init (&r, lhs);
1954 return refs_may_alias_p_1 (ref, &r, true);
1957 else if (gimple_code (stmt) == GIMPLE_ASM)
1958 return true;
1960 return false;
1963 bool
1964 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1966 ao_ref r;
1967 ao_ref_init (&r, ref);
1968 return stmt_may_clobber_ref_p_1 (stmt, &r);
1971 /* If STMT kills the memory reference REF return true, otherwise
1972 return false. */
1974 static bool
1975 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1977 /* For a must-alias check we need to be able to constrain
1978 the access properly. */
1979 ao_ref_base (ref);
1980 if (ref->max_size == -1)
1981 return false;
1983 if (gimple_has_lhs (stmt)
1984 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1985 /* The assignment is not necessarily carried out if it can throw
1986 and we can catch it in the current function where we could inspect
1987 the previous value.
1988 ??? We only need to care about the RHS throwing. For aggregate
1989 assignments or similar calls and non-call exceptions the LHS
1990 might throw as well. */
1991 && !stmt_can_throw_internal (stmt))
1993 tree base, lhs = gimple_get_lhs (stmt);
1994 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
1995 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1996 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1997 so base == ref->base does not always hold. */
1998 if (base != ref->base)
2000 /* If both base and ref->base are MEM_REFs, only compare the
2001 first operand, and if the second operand isn't equal constant,
2002 try to add the offsets into offset and ref_offset. */
2003 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2004 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2006 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2007 TREE_OPERAND (ref->base, 1)))
2009 double_int off1 = mem_ref_offset (base);
2010 off1 = off1.lshift (BITS_PER_UNIT == 8
2011 ? 3 : exact_log2 (BITS_PER_UNIT));
2012 off1 = off1 + double_int::from_shwi (offset);
2013 double_int off2 = mem_ref_offset (ref->base);
2014 off2 = off2.lshift (BITS_PER_UNIT == 8
2015 ? 3 : exact_log2 (BITS_PER_UNIT));
2016 off2 = off2 + double_int::from_shwi (ref_offset);
2017 if (off1.fits_shwi () && off2.fits_shwi ())
2019 offset = off1.to_shwi ();
2020 ref_offset = off2.to_shwi ();
2022 else
2023 size = -1;
2026 else
2027 size = -1;
2029 /* For a must-alias check we need to be able to constrain
2030 the access properly. */
2031 if (size != -1 && size == max_size)
2033 if (offset <= ref_offset
2034 && offset + size >= ref_offset + ref->max_size)
2035 return true;
2039 if (is_gimple_call (stmt))
2041 tree callee = gimple_call_fndecl (stmt);
2042 if (callee != NULL_TREE
2043 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2044 switch (DECL_FUNCTION_CODE (callee))
2046 case BUILT_IN_MEMCPY:
2047 case BUILT_IN_MEMPCPY:
2048 case BUILT_IN_MEMMOVE:
2049 case BUILT_IN_MEMSET:
2050 case BUILT_IN_MEMCPY_CHK:
2051 case BUILT_IN_MEMPCPY_CHK:
2052 case BUILT_IN_MEMMOVE_CHK:
2053 case BUILT_IN_MEMSET_CHK:
2055 tree dest = gimple_call_arg (stmt, 0);
2056 tree len = gimple_call_arg (stmt, 2);
2057 tree base = NULL_TREE;
2058 HOST_WIDE_INT offset = 0;
2059 if (!host_integerp (len, 0))
2060 return false;
2061 if (TREE_CODE (dest) == ADDR_EXPR)
2062 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
2063 &offset);
2064 else if (TREE_CODE (dest) == SSA_NAME)
2065 base = dest;
2066 if (base
2067 && base == ao_ref_base (ref))
2069 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
2070 if (offset <= ref->offset / BITS_PER_UNIT
2071 && (offset + size
2072 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
2073 / BITS_PER_UNIT)))
2074 return true;
2076 break;
2079 case BUILT_IN_VA_END:
2081 tree ptr = gimple_call_arg (stmt, 0);
2082 if (TREE_CODE (ptr) == ADDR_EXPR)
2084 tree base = ao_ref_base (ref);
2085 if (TREE_OPERAND (ptr, 0) == base)
2086 return true;
2088 break;
2091 default:;
2094 return false;
2097 bool
2098 stmt_kills_ref_p (gimple stmt, tree ref)
2100 ao_ref r;
2101 ao_ref_init (&r, ref);
2102 return stmt_kills_ref_p_1 (stmt, &r);
2106 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2107 TARGET or a statement clobbering the memory reference REF in which
2108 case false is returned. The walk starts with VUSE, one argument of PHI. */
2110 static bool
2111 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2112 tree vuse, unsigned int *cnt, bitmap *visited,
2113 bool abort_on_visited)
2115 basic_block bb = gimple_bb (phi);
2117 if (!*visited)
2118 *visited = BITMAP_ALLOC (NULL);
2120 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2122 /* Walk until we hit the target. */
2123 while (vuse != target)
2125 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2126 /* Recurse for PHI nodes. */
2127 if (gimple_code (def_stmt) == GIMPLE_PHI)
2129 /* An already visited PHI node ends the walk successfully. */
2130 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2131 return !abort_on_visited;
2132 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2133 visited, abort_on_visited);
2134 if (!vuse)
2135 return false;
2136 continue;
2138 else if (gimple_nop_p (def_stmt))
2139 return false;
2140 else
2142 /* A clobbering statement or the end of the IL ends it failing. */
2143 ++*cnt;
2144 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2145 return false;
2147 /* If we reach a new basic-block see if we already skipped it
2148 in a previous walk that ended successfully. */
2149 if (gimple_bb (def_stmt) != bb)
2151 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2152 return !abort_on_visited;
2153 bb = gimple_bb (def_stmt);
2155 vuse = gimple_vuse (def_stmt);
2157 return true;
2160 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2161 until we hit the phi argument definition that dominates the other one.
2162 Return that, or NULL_TREE if there is no such definition. */
2164 static tree
2165 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2166 ao_ref *ref, unsigned int *cnt,
2167 bitmap *visited, bool abort_on_visited)
2169 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2170 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2171 tree common_vuse;
2173 if (arg0 == arg1)
2174 return arg0;
2175 else if (gimple_nop_p (def0)
2176 || (!gimple_nop_p (def1)
2177 && dominated_by_p (CDI_DOMINATORS,
2178 gimple_bb (def1), gimple_bb (def0))))
2180 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2181 visited, abort_on_visited))
2182 return arg0;
2184 else if (gimple_nop_p (def1)
2185 || dominated_by_p (CDI_DOMINATORS,
2186 gimple_bb (def0), gimple_bb (def1)))
2188 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2189 visited, abort_on_visited))
2190 return arg1;
2192 /* Special case of a diamond:
2193 MEM_1 = ...
2194 goto (cond) ? L1 : L2
2195 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2196 goto L3
2197 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2198 L3: MEM_4 = PHI<MEM_2, MEM_3>
2199 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2200 dominate each other, but still we can easily skip this PHI node
2201 if we recognize that the vuse MEM operand is the same for both,
2202 and that we can skip both statements (they don't clobber us).
2203 This is still linear. Don't use maybe_skip_until, that might
2204 potentially be slow. */
2205 else if ((common_vuse = gimple_vuse (def0))
2206 && common_vuse == gimple_vuse (def1))
2208 *cnt += 2;
2209 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2210 && !stmt_may_clobber_ref_p_1 (def1, ref))
2211 return common_vuse;
2214 return NULL_TREE;
2218 /* Starting from a PHI node for the virtual operand of the memory reference
2219 REF find a continuation virtual operand that allows to continue walking
2220 statements dominating PHI skipping only statements that cannot possibly
2221 clobber REF. Increments *CNT for each alias disambiguation done.
2222 Returns NULL_TREE if no suitable virtual operand can be found. */
2224 tree
2225 get_continuation_for_phi (gimple phi, ao_ref *ref,
2226 unsigned int *cnt, bitmap *visited,
2227 bool abort_on_visited)
2229 unsigned nargs = gimple_phi_num_args (phi);
2231 /* Through a single-argument PHI we can simply look through. */
2232 if (nargs == 1)
2233 return PHI_ARG_DEF (phi, 0);
2235 /* For two or more arguments try to pairwise skip non-aliasing code
2236 until we hit the phi argument definition that dominates the other one. */
2237 else if (nargs >= 2)
2239 tree arg0, arg1;
2240 unsigned i;
2242 /* Find a candidate for the virtual operand which definition
2243 dominates those of all others. */
2244 arg0 = PHI_ARG_DEF (phi, 0);
2245 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2246 for (i = 1; i < nargs; ++i)
2248 arg1 = PHI_ARG_DEF (phi, i);
2249 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2251 arg0 = arg1;
2252 break;
2254 if (dominated_by_p (CDI_DOMINATORS,
2255 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2256 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2257 arg0 = arg1;
2260 /* Then pairwise reduce against the found candidate. */
2261 for (i = 0; i < nargs; ++i)
2263 arg1 = PHI_ARG_DEF (phi, i);
2264 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2265 cnt, visited, abort_on_visited);
2266 if (!arg0)
2267 return NULL_TREE;
2270 return arg0;
2273 return NULL_TREE;
2276 /* Based on the memory reference REF and its virtual use VUSE call
2277 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2278 itself. That is, for each virtual use for which its defining statement
2279 does not clobber REF.
2281 WALKER is called with REF, the current virtual use and DATA. If
2282 WALKER returns non-NULL the walk stops and its result is returned.
2283 At the end of a non-successful walk NULL is returned.
2285 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2286 use which definition is a statement that may clobber REF and DATA.
2287 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2288 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2289 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2290 to adjust REF and *DATA to make that valid.
2292 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2294 void *
2295 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2296 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2297 void *(*translate)(ao_ref *, tree, void *), void *data)
2299 bitmap visited = NULL;
2300 void *res;
2301 unsigned int cnt = 0;
2302 bool translated = false;
2304 timevar_push (TV_ALIAS_STMT_WALK);
2308 gimple def_stmt;
2310 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2311 res = (*walker) (ref, vuse, cnt, data);
2312 /* Abort walk. */
2313 if (res == (void *)-1)
2315 res = NULL;
2316 break;
2318 /* Lookup succeeded. */
2319 else if (res != NULL)
2320 break;
2322 def_stmt = SSA_NAME_DEF_STMT (vuse);
2323 if (gimple_nop_p (def_stmt))
2324 break;
2325 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2326 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2327 &visited, translated);
2328 else
2330 cnt++;
2331 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2333 if (!translate)
2334 break;
2335 res = (*translate) (ref, vuse, data);
2336 /* Failed lookup and translation. */
2337 if (res == (void *)-1)
2339 res = NULL;
2340 break;
2342 /* Lookup succeeded. */
2343 else if (res != NULL)
2344 break;
2345 /* Translation succeeded, continue walking. */
2346 translated = true;
2348 vuse = gimple_vuse (def_stmt);
2351 while (vuse);
2353 if (visited)
2354 BITMAP_FREE (visited);
2356 timevar_pop (TV_ALIAS_STMT_WALK);
2358 return res;
2362 /* Based on the memory reference REF call WALKER for each vdef which
2363 defining statement may clobber REF, starting with VDEF. If REF
2364 is NULL_TREE, each defining statement is visited.
2366 WALKER is called with REF, the current vdef and DATA. If WALKER
2367 returns true the walk is stopped, otherwise it continues.
2369 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2370 PHI argument (but only one walk continues on merge points), the
2371 return value is true if any of the walks was successful.
2373 The function returns the number of statements walked. */
2375 static unsigned int
2376 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2377 bool (*walker)(ao_ref *, tree, void *), void *data,
2378 bitmap *visited, unsigned int cnt)
2382 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2384 if (*visited
2385 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2386 return cnt;
2388 if (gimple_nop_p (def_stmt))
2389 return cnt;
2390 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2392 unsigned i;
2393 if (!*visited)
2394 *visited = BITMAP_ALLOC (NULL);
2395 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2396 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2397 walker, data, visited, 0);
2398 return cnt;
2401 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2402 cnt++;
2403 if ((!ref
2404 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2405 && (*walker) (ref, vdef, data))
2406 return cnt;
2408 vdef = gimple_vuse (def_stmt);
2410 while (1);
2413 unsigned int
2414 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2415 bool (*walker)(ao_ref *, tree, void *), void *data,
2416 bitmap *visited)
2418 bitmap local_visited = NULL;
2419 unsigned int ret;
2421 timevar_push (TV_ALIAS_STMT_WALK);
2423 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2424 visited ? visited : &local_visited, 0);
2425 if (local_visited)
2426 BITMAP_FREE (local_visited);
2428 timevar_pop (TV_ALIAS_STMT_WALK);
2430 return ret;