2013-10-11 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / tree-ssa-alias.c
blob3cce3315ba2938c8eb61804faf852870ed4f16bc
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 ??? Bitfields can overlap at RTL level so punt on them. */
808 if (field1 != field2)
810 component_refs1.release ();
811 component_refs2.release ();
812 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
816 may_overlap:
817 component_refs1.release ();
818 component_refs2.release ();
819 return false;
822 /* Return true if two memory references based on the variables BASE1
823 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
824 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
825 if non-NULL are the complete memory reference trees. */
827 static bool
828 decl_refs_may_alias_p (tree ref1, tree base1,
829 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
830 tree ref2, tree base2,
831 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
833 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
835 /* If both references are based on different variables, they cannot alias. */
836 if (base1 != base2)
837 return false;
839 /* If both references are based on the same variable, they cannot alias if
840 the accesses do not overlap. */
841 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
842 return false;
844 /* For components with variable position, the above test isn't sufficient,
845 so we disambiguate component references manually. */
846 if (ref1 && ref2
847 && handled_component_p (ref1) && handled_component_p (ref2)
848 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
849 return false;
851 return true;
854 /* Return true if an indirect reference based on *PTR1 constrained
855 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
856 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
857 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
858 in which case they are computed on-demand. REF1 and REF2
859 if non-NULL are the complete memory reference trees. */
861 static bool
862 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
863 HOST_WIDE_INT offset1,
864 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
865 alias_set_type ref1_alias_set,
866 alias_set_type base1_alias_set,
867 tree ref2 ATTRIBUTE_UNUSED, tree base2,
868 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
869 alias_set_type ref2_alias_set,
870 alias_set_type base2_alias_set, bool tbaa_p)
872 tree ptr1;
873 tree ptrtype1, dbase2;
874 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
875 HOST_WIDE_INT doffset1, doffset2;
876 double_int moff;
878 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
879 || TREE_CODE (base1) == TARGET_MEM_REF)
880 && DECL_P (base2));
882 ptr1 = TREE_OPERAND (base1, 0);
884 /* The offset embedded in MEM_REFs can be negative. Bias them
885 so that the resulting offset adjustment is positive. */
886 moff = mem_ref_offset (base1);
887 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
888 if (moff.is_negative ())
889 offset2p += (-moff).low;
890 else
891 offset1p += moff.low;
893 /* If only one reference is based on a variable, they cannot alias if
894 the pointer access is beyond the extent of the variable access.
895 (the pointer base cannot validly point to an offset less than zero
896 of the variable).
897 ??? IVOPTs creates bases that do not honor this restriction,
898 so do not apply this optimization for TARGET_MEM_REFs. */
899 if (TREE_CODE (base1) != TARGET_MEM_REF
900 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
901 return false;
902 /* They also cannot alias if the pointer may not point to the decl. */
903 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
904 return false;
906 /* Disambiguations that rely on strict aliasing rules follow. */
907 if (!flag_strict_aliasing || !tbaa_p)
908 return true;
910 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
912 /* If the alias set for a pointer access is zero all bets are off. */
913 if (base1_alias_set == -1)
914 base1_alias_set = get_deref_alias_set (ptrtype1);
915 if (base1_alias_set == 0)
916 return true;
917 if (base2_alias_set == -1)
918 base2_alias_set = get_alias_set (base2);
920 /* When we are trying to disambiguate an access with a pointer dereference
921 as base versus one with a decl as base we can use both the size
922 of the decl and its dynamic type for extra disambiguation.
923 ??? We do not know anything about the dynamic type of the decl
924 other than that its alias-set contains base2_alias_set as a subset
925 which does not help us here. */
926 /* As we know nothing useful about the dynamic type of the decl just
927 use the usual conflict check rather than a subset test.
928 ??? We could introduce -fvery-strict-aliasing when the language
929 does not allow decls to have a dynamic type that differs from their
930 static type. Then we can check
931 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
932 if (base1_alias_set != base2_alias_set
933 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
934 return false;
935 /* If the size of the access relevant for TBAA through the pointer
936 is bigger than the size of the decl we can't possibly access the
937 decl via that pointer. */
938 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
939 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
940 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
941 /* ??? This in turn may run afoul when a decl of type T which is
942 a member of union type U is accessed through a pointer to
943 type U and sizeof T is smaller than sizeof U. */
944 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
945 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
946 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
947 return false;
949 if (!ref2)
950 return true;
952 /* If the decl is accessed via a MEM_REF, reconstruct the base
953 we can use for TBAA and an appropriately adjusted offset. */
954 dbase2 = ref2;
955 while (handled_component_p (dbase2))
956 dbase2 = TREE_OPERAND (dbase2, 0);
957 doffset1 = offset1;
958 doffset2 = offset2;
959 if (TREE_CODE (dbase2) == MEM_REF
960 || TREE_CODE (dbase2) == TARGET_MEM_REF)
962 double_int moff = mem_ref_offset (dbase2);
963 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
964 if (moff.is_negative ())
965 doffset1 -= (-moff).low;
966 else
967 doffset2 -= moff.low;
970 /* If either reference is view-converted, give up now. */
971 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
972 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
973 return true;
975 /* If both references are through the same type, they do not alias
976 if the accesses do not overlap. This does extra disambiguation
977 for mixed/pointer accesses but requires strict aliasing.
978 For MEM_REFs we require that the component-ref offset we computed
979 is relative to the start of the type which we ensure by
980 comparing rvalue and access type and disregarding the constant
981 pointer offset. */
982 if ((TREE_CODE (base1) != TARGET_MEM_REF
983 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
984 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
985 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
987 /* Do access-path based disambiguation. */
988 if (ref1 && ref2
989 && (handled_component_p (ref1) || handled_component_p (ref2)))
990 return aliasing_component_refs_p (ref1,
991 ref1_alias_set, base1_alias_set,
992 offset1, max_size1,
993 ref2,
994 ref2_alias_set, base2_alias_set,
995 offset2, max_size2, true);
997 return true;
1000 /* Return true if two indirect references based on *PTR1
1001 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1002 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1003 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1004 in which case they are computed on-demand. REF1 and REF2
1005 if non-NULL are the complete memory reference trees. */
1007 static bool
1008 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1009 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1010 alias_set_type ref1_alias_set,
1011 alias_set_type base1_alias_set,
1012 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1013 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1014 alias_set_type ref2_alias_set,
1015 alias_set_type base2_alias_set, bool tbaa_p)
1017 tree ptr1;
1018 tree ptr2;
1019 tree ptrtype1, ptrtype2;
1021 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1022 || TREE_CODE (base1) == TARGET_MEM_REF)
1023 && (TREE_CODE (base2) == MEM_REF
1024 || TREE_CODE (base2) == TARGET_MEM_REF));
1026 ptr1 = TREE_OPERAND (base1, 0);
1027 ptr2 = TREE_OPERAND (base2, 0);
1029 /* If both bases are based on pointers they cannot alias if they may not
1030 point to the same memory object or if they point to the same object
1031 and the accesses do not overlap. */
1032 if ((!cfun || gimple_in_ssa_p (cfun))
1033 && operand_equal_p (ptr1, ptr2, 0)
1034 && (((TREE_CODE (base1) != TARGET_MEM_REF
1035 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1036 && (TREE_CODE (base2) != TARGET_MEM_REF
1037 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1038 || (TREE_CODE (base1) == TARGET_MEM_REF
1039 && TREE_CODE (base2) == TARGET_MEM_REF
1040 && (TMR_STEP (base1) == TMR_STEP (base2)
1041 || (TMR_STEP (base1) && TMR_STEP (base2)
1042 && operand_equal_p (TMR_STEP (base1),
1043 TMR_STEP (base2), 0)))
1044 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1045 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1046 && operand_equal_p (TMR_INDEX (base1),
1047 TMR_INDEX (base2), 0)))
1048 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1049 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1050 && operand_equal_p (TMR_INDEX2 (base1),
1051 TMR_INDEX2 (base2), 0))))))
1053 double_int moff;
1054 /* The offset embedded in MEM_REFs can be negative. Bias them
1055 so that the resulting offset adjustment is positive. */
1056 moff = mem_ref_offset (base1);
1057 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1058 if (moff.is_negative ())
1059 offset2 += (-moff).low;
1060 else
1061 offset1 += moff.low;
1062 moff = mem_ref_offset (base2);
1063 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1064 if (moff.is_negative ())
1065 offset1 += (-moff).low;
1066 else
1067 offset2 += moff.low;
1068 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1070 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1071 return false;
1073 /* Disambiguations that rely on strict aliasing rules follow. */
1074 if (!flag_strict_aliasing || !tbaa_p)
1075 return true;
1077 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1078 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1080 /* If the alias set for a pointer access is zero all bets are off. */
1081 if (base1_alias_set == -1)
1082 base1_alias_set = get_deref_alias_set (ptrtype1);
1083 if (base1_alias_set == 0)
1084 return true;
1085 if (base2_alias_set == -1)
1086 base2_alias_set = get_deref_alias_set (ptrtype2);
1087 if (base2_alias_set == 0)
1088 return true;
1090 /* If both references are through the same type, they do not alias
1091 if the accesses do not overlap. This does extra disambiguation
1092 for mixed/pointer accesses but requires strict aliasing. */
1093 if ((TREE_CODE (base1) != TARGET_MEM_REF
1094 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1095 && (TREE_CODE (base2) != TARGET_MEM_REF
1096 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1097 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1098 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1099 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1100 TREE_TYPE (ptrtype2)) == 1)
1101 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1103 /* Do type-based disambiguation. */
1104 if (base1_alias_set != base2_alias_set
1105 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1106 return false;
1108 /* Do access-path based disambiguation. */
1109 if (ref1 && ref2
1110 && (handled_component_p (ref1) || handled_component_p (ref2))
1111 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1112 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
1113 return aliasing_component_refs_p (ref1,
1114 ref1_alias_set, base1_alias_set,
1115 offset1, max_size1,
1116 ref2,
1117 ref2_alias_set, base2_alias_set,
1118 offset2, max_size2, false);
1120 return true;
1123 /* Return true, if the two memory references REF1 and REF2 may alias. */
1125 bool
1126 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1128 tree base1, base2;
1129 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1130 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1131 bool var1_p, var2_p, ind1_p, ind2_p;
1133 gcc_checking_assert ((!ref1->ref
1134 || TREE_CODE (ref1->ref) == SSA_NAME
1135 || DECL_P (ref1->ref)
1136 || TREE_CODE (ref1->ref) == STRING_CST
1137 || handled_component_p (ref1->ref)
1138 || TREE_CODE (ref1->ref) == MEM_REF
1139 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1140 && (!ref2->ref
1141 || TREE_CODE (ref2->ref) == SSA_NAME
1142 || DECL_P (ref2->ref)
1143 || TREE_CODE (ref2->ref) == STRING_CST
1144 || handled_component_p (ref2->ref)
1145 || TREE_CODE (ref2->ref) == MEM_REF
1146 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1148 /* Decompose the references into their base objects and the access. */
1149 base1 = ao_ref_base (ref1);
1150 offset1 = ref1->offset;
1151 max_size1 = ref1->max_size;
1152 base2 = ao_ref_base (ref2);
1153 offset2 = ref2->offset;
1154 max_size2 = ref2->max_size;
1156 /* We can end up with registers or constants as bases for example from
1157 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1158 which is seen as a struct copy. */
1159 if (TREE_CODE (base1) == SSA_NAME
1160 || TREE_CODE (base1) == CONST_DECL
1161 || TREE_CODE (base1) == CONSTRUCTOR
1162 || TREE_CODE (base1) == ADDR_EXPR
1163 || CONSTANT_CLASS_P (base1)
1164 || TREE_CODE (base2) == SSA_NAME
1165 || TREE_CODE (base2) == CONST_DECL
1166 || TREE_CODE (base2) == CONSTRUCTOR
1167 || TREE_CODE (base2) == ADDR_EXPR
1168 || CONSTANT_CLASS_P (base2))
1169 return false;
1171 /* We can end up referring to code via function and label decls.
1172 As we likely do not properly track code aliases conservatively
1173 bail out. */
1174 if (TREE_CODE (base1) == FUNCTION_DECL
1175 || TREE_CODE (base1) == LABEL_DECL
1176 || TREE_CODE (base2) == FUNCTION_DECL
1177 || TREE_CODE (base2) == LABEL_DECL)
1178 return true;
1180 /* Two volatile accesses always conflict. */
1181 if (ref1->volatile_p
1182 && ref2->volatile_p)
1183 return true;
1185 /* Defer to simple offset based disambiguation if we have
1186 references based on two decls. Do this before defering to
1187 TBAA to handle must-alias cases in conformance with the
1188 GCC extension of allowing type-punning through unions. */
1189 var1_p = DECL_P (base1);
1190 var2_p = DECL_P (base2);
1191 if (var1_p && var2_p)
1192 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1193 ref2->ref, base2, offset2, max_size2);
1195 ind1_p = (TREE_CODE (base1) == MEM_REF
1196 || TREE_CODE (base1) == TARGET_MEM_REF);
1197 ind2_p = (TREE_CODE (base2) == MEM_REF
1198 || TREE_CODE (base2) == TARGET_MEM_REF);
1200 /* Canonicalize the pointer-vs-decl case. */
1201 if (ind1_p && var2_p)
1203 HOST_WIDE_INT tmp1;
1204 tree tmp2;
1205 ao_ref *tmp3;
1206 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1207 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1208 tmp2 = base1; base1 = base2; base2 = tmp2;
1209 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1210 var1_p = true;
1211 ind1_p = false;
1212 var2_p = false;
1213 ind2_p = true;
1216 /* First defer to TBAA if possible. */
1217 if (tbaa_p
1218 && flag_strict_aliasing
1219 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1220 ao_ref_alias_set (ref2)))
1221 return false;
1223 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1224 if (var1_p && ind2_p)
1225 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1226 offset2, max_size2,
1227 ao_ref_alias_set (ref2), -1,
1228 ref1->ref, base1,
1229 offset1, max_size1,
1230 ao_ref_alias_set (ref1),
1231 ao_ref_base_alias_set (ref1),
1232 tbaa_p);
1233 else if (ind1_p && ind2_p)
1234 return indirect_refs_may_alias_p (ref1->ref, base1,
1235 offset1, max_size1,
1236 ao_ref_alias_set (ref1), -1,
1237 ref2->ref, base2,
1238 offset2, max_size2,
1239 ao_ref_alias_set (ref2), -1,
1240 tbaa_p);
1242 /* We really do not want to end up here, but returning true is safe. */
1243 #ifdef ENABLE_CHECKING
1244 gcc_unreachable ();
1245 #else
1246 return true;
1247 #endif
1250 bool
1251 refs_may_alias_p (tree ref1, tree ref2)
1253 ao_ref r1, r2;
1254 bool res;
1255 ao_ref_init (&r1, ref1);
1256 ao_ref_init (&r2, ref2);
1257 res = refs_may_alias_p_1 (&r1, &r2, true);
1258 if (res)
1259 ++alias_stats.refs_may_alias_p_may_alias;
1260 else
1261 ++alias_stats.refs_may_alias_p_no_alias;
1262 return res;
1265 /* Returns true if there is a anti-dependence for the STORE that
1266 executes after the LOAD. */
1268 bool
1269 refs_anti_dependent_p (tree load, tree store)
1271 ao_ref r1, r2;
1272 ao_ref_init (&r1, load);
1273 ao_ref_init (&r2, store);
1274 return refs_may_alias_p_1 (&r1, &r2, false);
1277 /* Returns true if there is a output dependence for the stores
1278 STORE1 and STORE2. */
1280 bool
1281 refs_output_dependent_p (tree store1, tree store2)
1283 ao_ref r1, r2;
1284 ao_ref_init (&r1, store1);
1285 ao_ref_init (&r2, store2);
1286 return refs_may_alias_p_1 (&r1, &r2, false);
1289 /* If the call CALL may use the memory reference REF return true,
1290 otherwise return false. */
1292 static bool
1293 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1295 tree base, callee;
1296 unsigned i;
1297 int flags = gimple_call_flags (call);
1299 /* Const functions without a static chain do not implicitly use memory. */
1300 if (!gimple_call_chain (call)
1301 && (flags & (ECF_CONST|ECF_NOVOPS)))
1302 goto process_args;
1304 base = ao_ref_base (ref);
1305 if (!base)
1306 return true;
1308 /* A call that is not without side-effects might involve volatile
1309 accesses and thus conflicts with all other volatile accesses. */
1310 if (ref->volatile_p)
1311 return true;
1313 /* If the reference is based on a decl that is not aliased the call
1314 cannot possibly use it. */
1315 if (DECL_P (base)
1316 && !may_be_aliased (base)
1317 /* But local statics can be used through recursion. */
1318 && !is_global_var (base))
1319 goto process_args;
1321 callee = gimple_call_fndecl (call);
1323 /* Handle those builtin functions explicitly that do not act as
1324 escape points. See tree-ssa-structalias.c:find_func_aliases
1325 for the list of builtins we might need to handle here. */
1326 if (callee != NULL_TREE
1327 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1328 switch (DECL_FUNCTION_CODE (callee))
1330 /* All the following functions read memory pointed to by
1331 their second argument. strcat/strncat additionally
1332 reads memory pointed to by the first argument. */
1333 case BUILT_IN_STRCAT:
1334 case BUILT_IN_STRNCAT:
1336 ao_ref dref;
1337 ao_ref_init_from_ptr_and_size (&dref,
1338 gimple_call_arg (call, 0),
1339 NULL_TREE);
1340 if (refs_may_alias_p_1 (&dref, ref, false))
1341 return true;
1343 /* FALLTHRU */
1344 case BUILT_IN_STRCPY:
1345 case BUILT_IN_STRNCPY:
1346 case BUILT_IN_MEMCPY:
1347 case BUILT_IN_MEMMOVE:
1348 case BUILT_IN_MEMPCPY:
1349 case BUILT_IN_STPCPY:
1350 case BUILT_IN_STPNCPY:
1351 case BUILT_IN_TM_MEMCPY:
1352 case BUILT_IN_TM_MEMMOVE:
1354 ao_ref dref;
1355 tree size = NULL_TREE;
1356 if (gimple_call_num_args (call) == 3)
1357 size = gimple_call_arg (call, 2);
1358 ao_ref_init_from_ptr_and_size (&dref,
1359 gimple_call_arg (call, 1),
1360 size);
1361 return refs_may_alias_p_1 (&dref, ref, false);
1363 case BUILT_IN_STRCAT_CHK:
1364 case BUILT_IN_STRNCAT_CHK:
1366 ao_ref dref;
1367 ao_ref_init_from_ptr_and_size (&dref,
1368 gimple_call_arg (call, 0),
1369 NULL_TREE);
1370 if (refs_may_alias_p_1 (&dref, ref, false))
1371 return true;
1373 /* FALLTHRU */
1374 case BUILT_IN_STRCPY_CHK:
1375 case BUILT_IN_STRNCPY_CHK:
1376 case BUILT_IN_MEMCPY_CHK:
1377 case BUILT_IN_MEMMOVE_CHK:
1378 case BUILT_IN_MEMPCPY_CHK:
1379 case BUILT_IN_STPCPY_CHK:
1380 case BUILT_IN_STPNCPY_CHK:
1382 ao_ref dref;
1383 tree size = NULL_TREE;
1384 if (gimple_call_num_args (call) == 4)
1385 size = gimple_call_arg (call, 2);
1386 ao_ref_init_from_ptr_and_size (&dref,
1387 gimple_call_arg (call, 1),
1388 size);
1389 return refs_may_alias_p_1 (&dref, ref, false);
1391 case BUILT_IN_BCOPY:
1393 ao_ref dref;
1394 tree size = gimple_call_arg (call, 2);
1395 ao_ref_init_from_ptr_and_size (&dref,
1396 gimple_call_arg (call, 0),
1397 size);
1398 return refs_may_alias_p_1 (&dref, ref, false);
1401 /* The following functions read memory pointed to by their
1402 first argument. */
1403 CASE_BUILT_IN_TM_LOAD (1):
1404 CASE_BUILT_IN_TM_LOAD (2):
1405 CASE_BUILT_IN_TM_LOAD (4):
1406 CASE_BUILT_IN_TM_LOAD (8):
1407 CASE_BUILT_IN_TM_LOAD (FLOAT):
1408 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1409 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1410 CASE_BUILT_IN_TM_LOAD (M64):
1411 CASE_BUILT_IN_TM_LOAD (M128):
1412 CASE_BUILT_IN_TM_LOAD (M256):
1413 case BUILT_IN_TM_LOG:
1414 case BUILT_IN_TM_LOG_1:
1415 case BUILT_IN_TM_LOG_2:
1416 case BUILT_IN_TM_LOG_4:
1417 case BUILT_IN_TM_LOG_8:
1418 case BUILT_IN_TM_LOG_FLOAT:
1419 case BUILT_IN_TM_LOG_DOUBLE:
1420 case BUILT_IN_TM_LOG_LDOUBLE:
1421 case BUILT_IN_TM_LOG_M64:
1422 case BUILT_IN_TM_LOG_M128:
1423 case BUILT_IN_TM_LOG_M256:
1424 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1426 /* These read memory pointed to by the first argument. */
1427 case BUILT_IN_STRDUP:
1428 case BUILT_IN_STRNDUP:
1430 ao_ref dref;
1431 tree size = NULL_TREE;
1432 if (gimple_call_num_args (call) == 2)
1433 size = gimple_call_arg (call, 1);
1434 ao_ref_init_from_ptr_and_size (&dref,
1435 gimple_call_arg (call, 0),
1436 size);
1437 return refs_may_alias_p_1 (&dref, ref, false);
1439 /* These read memory pointed to by the first argument. */
1440 case BUILT_IN_INDEX:
1441 case BUILT_IN_STRCHR:
1442 case BUILT_IN_STRRCHR:
1444 ao_ref dref;
1445 ao_ref_init_from_ptr_and_size (&dref,
1446 gimple_call_arg (call, 0),
1447 NULL_TREE);
1448 return refs_may_alias_p_1 (&dref, ref, false);
1450 /* These read memory pointed to by the first argument with size
1451 in the third argument. */
1452 case BUILT_IN_MEMCHR:
1454 ao_ref dref;
1455 ao_ref_init_from_ptr_and_size (&dref,
1456 gimple_call_arg (call, 0),
1457 gimple_call_arg (call, 2));
1458 return refs_may_alias_p_1 (&dref, ref, false);
1460 /* These read memory pointed to by the first and second arguments. */
1461 case BUILT_IN_STRSTR:
1462 case BUILT_IN_STRPBRK:
1464 ao_ref dref;
1465 ao_ref_init_from_ptr_and_size (&dref,
1466 gimple_call_arg (call, 0),
1467 NULL_TREE);
1468 if (refs_may_alias_p_1 (&dref, ref, false))
1469 return true;
1470 ao_ref_init_from_ptr_and_size (&dref,
1471 gimple_call_arg (call, 1),
1472 NULL_TREE);
1473 return refs_may_alias_p_1 (&dref, ref, false);
1476 /* The following builtins do not read from memory. */
1477 case BUILT_IN_FREE:
1478 case BUILT_IN_MALLOC:
1479 case BUILT_IN_CALLOC:
1480 case BUILT_IN_ALLOCA:
1481 case BUILT_IN_ALLOCA_WITH_ALIGN:
1482 case BUILT_IN_STACK_SAVE:
1483 case BUILT_IN_STACK_RESTORE:
1484 case BUILT_IN_MEMSET:
1485 case BUILT_IN_TM_MEMSET:
1486 case BUILT_IN_MEMSET_CHK:
1487 case BUILT_IN_FREXP:
1488 case BUILT_IN_FREXPF:
1489 case BUILT_IN_FREXPL:
1490 case BUILT_IN_GAMMA_R:
1491 case BUILT_IN_GAMMAF_R:
1492 case BUILT_IN_GAMMAL_R:
1493 case BUILT_IN_LGAMMA_R:
1494 case BUILT_IN_LGAMMAF_R:
1495 case BUILT_IN_LGAMMAL_R:
1496 case BUILT_IN_MODF:
1497 case BUILT_IN_MODFF:
1498 case BUILT_IN_MODFL:
1499 case BUILT_IN_REMQUO:
1500 case BUILT_IN_REMQUOF:
1501 case BUILT_IN_REMQUOL:
1502 case BUILT_IN_SINCOS:
1503 case BUILT_IN_SINCOSF:
1504 case BUILT_IN_SINCOSL:
1505 case BUILT_IN_ASSUME_ALIGNED:
1506 case BUILT_IN_VA_END:
1507 return false;
1508 /* __sync_* builtins and some OpenMP builtins act as threading
1509 barriers. */
1510 #undef DEF_SYNC_BUILTIN
1511 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1512 #include "sync-builtins.def"
1513 #undef DEF_SYNC_BUILTIN
1514 case BUILT_IN_GOMP_ATOMIC_START:
1515 case BUILT_IN_GOMP_ATOMIC_END:
1516 case BUILT_IN_GOMP_BARRIER:
1517 case BUILT_IN_GOMP_BARRIER_CANCEL:
1518 case BUILT_IN_GOMP_TASKWAIT:
1519 case BUILT_IN_GOMP_TASKGROUP_END:
1520 case BUILT_IN_GOMP_CRITICAL_START:
1521 case BUILT_IN_GOMP_CRITICAL_END:
1522 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1523 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1524 case BUILT_IN_GOMP_LOOP_END:
1525 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1526 case BUILT_IN_GOMP_ORDERED_START:
1527 case BUILT_IN_GOMP_ORDERED_END:
1528 case BUILT_IN_GOMP_SECTIONS_END:
1529 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1530 case BUILT_IN_GOMP_SINGLE_COPY_START:
1531 case BUILT_IN_GOMP_SINGLE_COPY_END:
1532 return true;
1534 default:
1535 /* Fallthru to general call handling. */;
1538 /* Check if base is a global static variable that is not read
1539 by the function. */
1540 if (callee != NULL_TREE
1541 && TREE_CODE (base) == VAR_DECL
1542 && TREE_STATIC (base))
1544 struct cgraph_node *node = cgraph_get_node (callee);
1545 bitmap not_read;
1547 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1548 node yet. We should enforce that there are nodes for all decls in the
1549 IL and remove this check instead. */
1550 if (node
1551 && (not_read = ipa_reference_get_not_read_global (node))
1552 && bitmap_bit_p (not_read, DECL_UID (base)))
1553 goto process_args;
1556 /* Check if the base variable is call-used. */
1557 if (DECL_P (base))
1559 if (pt_solution_includes (gimple_call_use_set (call), base))
1560 return true;
1562 else if ((TREE_CODE (base) == MEM_REF
1563 || TREE_CODE (base) == TARGET_MEM_REF)
1564 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1566 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1567 if (!pi)
1568 return true;
1570 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1571 return true;
1573 else
1574 return true;
1576 /* Inspect call arguments for passed-by-value aliases. */
1577 process_args:
1578 for (i = 0; i < gimple_call_num_args (call); ++i)
1580 tree op = gimple_call_arg (call, i);
1581 int flags = gimple_call_arg_flags (call, i);
1583 if (flags & EAF_UNUSED)
1584 continue;
1586 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1587 op = TREE_OPERAND (op, 0);
1589 if (TREE_CODE (op) != SSA_NAME
1590 && !is_gimple_min_invariant (op))
1592 ao_ref r;
1593 ao_ref_init (&r, op);
1594 if (refs_may_alias_p_1 (&r, ref, true))
1595 return true;
1599 return false;
1602 static bool
1603 ref_maybe_used_by_call_p (gimple call, tree ref)
1605 ao_ref r;
1606 bool res;
1607 ao_ref_init (&r, ref);
1608 res = ref_maybe_used_by_call_p_1 (call, &r);
1609 if (res)
1610 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1611 else
1612 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1613 return res;
1617 /* If the statement STMT may use the memory reference REF return
1618 true, otherwise return false. */
1620 bool
1621 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1623 if (is_gimple_assign (stmt))
1625 tree rhs;
1627 /* All memory assign statements are single. */
1628 if (!gimple_assign_single_p (stmt))
1629 return false;
1631 rhs = gimple_assign_rhs1 (stmt);
1632 if (is_gimple_reg (rhs)
1633 || is_gimple_min_invariant (rhs)
1634 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1635 return false;
1637 return refs_may_alias_p (rhs, ref);
1639 else if (is_gimple_call (stmt))
1640 return ref_maybe_used_by_call_p (stmt, ref);
1641 else if (gimple_code (stmt) == GIMPLE_RETURN)
1643 tree retval = gimple_return_retval (stmt);
1644 tree base;
1645 if (retval
1646 && TREE_CODE (retval) != SSA_NAME
1647 && !is_gimple_min_invariant (retval)
1648 && refs_may_alias_p (retval, ref))
1649 return true;
1650 /* If ref escapes the function then the return acts as a use. */
1651 base = get_base_address (ref);
1652 if (!base)
1654 else if (DECL_P (base))
1655 return is_global_var (base);
1656 else if (TREE_CODE (base) == MEM_REF
1657 || TREE_CODE (base) == TARGET_MEM_REF)
1658 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1659 return false;
1662 return true;
1665 /* If the call in statement CALL may clobber the memory reference REF
1666 return true, otherwise return false. */
1668 static bool
1669 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1671 tree base;
1672 tree callee;
1674 /* If the call is pure or const it cannot clobber anything. */
1675 if (gimple_call_flags (call)
1676 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1677 return false;
1679 base = ao_ref_base (ref);
1680 if (!base)
1681 return true;
1683 if (TREE_CODE (base) == SSA_NAME
1684 || CONSTANT_CLASS_P (base))
1685 return false;
1687 /* A call that is not without side-effects might involve volatile
1688 accesses and thus conflicts with all other volatile accesses. */
1689 if (ref->volatile_p)
1690 return true;
1692 /* If the reference is based on a decl that is not aliased the call
1693 cannot possibly clobber it. */
1694 if (DECL_P (base)
1695 && !may_be_aliased (base)
1696 /* But local non-readonly statics can be modified through recursion
1697 or the call may implement a threading barrier which we must
1698 treat as may-def. */
1699 && (TREE_READONLY (base)
1700 || !is_global_var (base)))
1701 return false;
1703 callee = gimple_call_fndecl (call);
1705 /* Handle those builtin functions explicitly that do not act as
1706 escape points. See tree-ssa-structalias.c:find_func_aliases
1707 for the list of builtins we might need to handle here. */
1708 if (callee != NULL_TREE
1709 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1710 switch (DECL_FUNCTION_CODE (callee))
1712 /* All the following functions clobber memory pointed to by
1713 their first argument. */
1714 case BUILT_IN_STRCPY:
1715 case BUILT_IN_STRNCPY:
1716 case BUILT_IN_MEMCPY:
1717 case BUILT_IN_MEMMOVE:
1718 case BUILT_IN_MEMPCPY:
1719 case BUILT_IN_STPCPY:
1720 case BUILT_IN_STPNCPY:
1721 case BUILT_IN_STRCAT:
1722 case BUILT_IN_STRNCAT:
1723 case BUILT_IN_MEMSET:
1724 case BUILT_IN_TM_MEMSET:
1725 CASE_BUILT_IN_TM_STORE (1):
1726 CASE_BUILT_IN_TM_STORE (2):
1727 CASE_BUILT_IN_TM_STORE (4):
1728 CASE_BUILT_IN_TM_STORE (8):
1729 CASE_BUILT_IN_TM_STORE (FLOAT):
1730 CASE_BUILT_IN_TM_STORE (DOUBLE):
1731 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1732 CASE_BUILT_IN_TM_STORE (M64):
1733 CASE_BUILT_IN_TM_STORE (M128):
1734 CASE_BUILT_IN_TM_STORE (M256):
1735 case BUILT_IN_TM_MEMCPY:
1736 case BUILT_IN_TM_MEMMOVE:
1738 ao_ref dref;
1739 tree size = NULL_TREE;
1740 /* Don't pass in size for strncat, as the maximum size
1741 is strlen (dest) + n + 1 instead of n, resp.
1742 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1743 known. */
1744 if (gimple_call_num_args (call) == 3
1745 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1746 size = gimple_call_arg (call, 2);
1747 ao_ref_init_from_ptr_and_size (&dref,
1748 gimple_call_arg (call, 0),
1749 size);
1750 return refs_may_alias_p_1 (&dref, ref, false);
1752 case BUILT_IN_STRCPY_CHK:
1753 case BUILT_IN_STRNCPY_CHK:
1754 case BUILT_IN_MEMCPY_CHK:
1755 case BUILT_IN_MEMMOVE_CHK:
1756 case BUILT_IN_MEMPCPY_CHK:
1757 case BUILT_IN_STPCPY_CHK:
1758 case BUILT_IN_STPNCPY_CHK:
1759 case BUILT_IN_STRCAT_CHK:
1760 case BUILT_IN_STRNCAT_CHK:
1761 case BUILT_IN_MEMSET_CHK:
1763 ao_ref dref;
1764 tree size = NULL_TREE;
1765 /* Don't pass in size for __strncat_chk, as the maximum size
1766 is strlen (dest) + n + 1 instead of n, resp.
1767 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1768 known. */
1769 if (gimple_call_num_args (call) == 4
1770 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1771 size = gimple_call_arg (call, 2);
1772 ao_ref_init_from_ptr_and_size (&dref,
1773 gimple_call_arg (call, 0),
1774 size);
1775 return refs_may_alias_p_1 (&dref, ref, false);
1777 case BUILT_IN_BCOPY:
1779 ao_ref dref;
1780 tree size = gimple_call_arg (call, 2);
1781 ao_ref_init_from_ptr_and_size (&dref,
1782 gimple_call_arg (call, 1),
1783 size);
1784 return refs_may_alias_p_1 (&dref, ref, false);
1786 /* Allocating memory does not have any side-effects apart from
1787 being the definition point for the pointer. */
1788 case BUILT_IN_MALLOC:
1789 case BUILT_IN_CALLOC:
1790 case BUILT_IN_STRDUP:
1791 case BUILT_IN_STRNDUP:
1792 /* Unix98 specifies that errno is set on allocation failure. */
1793 if (flag_errno_math
1794 && targetm.ref_may_alias_errno (ref))
1795 return true;
1796 return false;
1797 case BUILT_IN_STACK_SAVE:
1798 case BUILT_IN_ALLOCA:
1799 case BUILT_IN_ALLOCA_WITH_ALIGN:
1800 case BUILT_IN_ASSUME_ALIGNED:
1801 return false;
1802 /* Freeing memory kills the pointed-to memory. More importantly
1803 the call has to serve as a barrier for moving loads and stores
1804 across it. */
1805 case BUILT_IN_FREE:
1806 case BUILT_IN_VA_END:
1808 tree ptr = gimple_call_arg (call, 0);
1809 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1811 case BUILT_IN_GAMMA_R:
1812 case BUILT_IN_GAMMAF_R:
1813 case BUILT_IN_GAMMAL_R:
1814 case BUILT_IN_LGAMMA_R:
1815 case BUILT_IN_LGAMMAF_R:
1816 case BUILT_IN_LGAMMAL_R:
1818 tree out = gimple_call_arg (call, 1);
1819 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1820 return true;
1821 if (flag_errno_math)
1822 break;
1823 return false;
1825 case BUILT_IN_FREXP:
1826 case BUILT_IN_FREXPF:
1827 case BUILT_IN_FREXPL:
1828 case BUILT_IN_MODF:
1829 case BUILT_IN_MODFF:
1830 case BUILT_IN_MODFL:
1832 tree out = gimple_call_arg (call, 1);
1833 return ptr_deref_may_alias_ref_p_1 (out, ref);
1835 case BUILT_IN_REMQUO:
1836 case BUILT_IN_REMQUOF:
1837 case BUILT_IN_REMQUOL:
1839 tree out = gimple_call_arg (call, 2);
1840 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1841 return true;
1842 if (flag_errno_math)
1843 break;
1844 return false;
1846 case BUILT_IN_SINCOS:
1847 case BUILT_IN_SINCOSF:
1848 case BUILT_IN_SINCOSL:
1850 tree sin = gimple_call_arg (call, 1);
1851 tree cos = gimple_call_arg (call, 2);
1852 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1853 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1855 /* __sync_* builtins and some OpenMP builtins act as threading
1856 barriers. */
1857 #undef DEF_SYNC_BUILTIN
1858 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1859 #include "sync-builtins.def"
1860 #undef DEF_SYNC_BUILTIN
1861 case BUILT_IN_GOMP_ATOMIC_START:
1862 case BUILT_IN_GOMP_ATOMIC_END:
1863 case BUILT_IN_GOMP_BARRIER:
1864 case BUILT_IN_GOMP_BARRIER_CANCEL:
1865 case BUILT_IN_GOMP_TASKWAIT:
1866 case BUILT_IN_GOMP_TASKGROUP_END:
1867 case BUILT_IN_GOMP_CRITICAL_START:
1868 case BUILT_IN_GOMP_CRITICAL_END:
1869 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1870 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1871 case BUILT_IN_GOMP_LOOP_END:
1872 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1873 case BUILT_IN_GOMP_ORDERED_START:
1874 case BUILT_IN_GOMP_ORDERED_END:
1875 case BUILT_IN_GOMP_SECTIONS_END:
1876 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1877 case BUILT_IN_GOMP_SINGLE_COPY_START:
1878 case BUILT_IN_GOMP_SINGLE_COPY_END:
1879 return true;
1880 default:
1881 /* Fallthru to general call handling. */;
1884 /* Check if base is a global static variable that is not written
1885 by the function. */
1886 if (callee != NULL_TREE
1887 && TREE_CODE (base) == VAR_DECL
1888 && TREE_STATIC (base))
1890 struct cgraph_node *node = cgraph_get_node (callee);
1891 bitmap not_written;
1893 if (node
1894 && (not_written = ipa_reference_get_not_written_global (node))
1895 && bitmap_bit_p (not_written, DECL_UID (base)))
1896 return false;
1899 /* Check if the base variable is call-clobbered. */
1900 if (DECL_P (base))
1901 return pt_solution_includes (gimple_call_clobber_set (call), base);
1902 else if ((TREE_CODE (base) == MEM_REF
1903 || TREE_CODE (base) == TARGET_MEM_REF)
1904 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1906 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1907 if (!pi)
1908 return true;
1910 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1913 return true;
1916 /* If the call in statement CALL may clobber the memory reference REF
1917 return true, otherwise return false. */
1919 bool
1920 call_may_clobber_ref_p (gimple call, tree ref)
1922 bool res;
1923 ao_ref r;
1924 ao_ref_init (&r, ref);
1925 res = call_may_clobber_ref_p_1 (call, &r);
1926 if (res)
1927 ++alias_stats.call_may_clobber_ref_p_may_alias;
1928 else
1929 ++alias_stats.call_may_clobber_ref_p_no_alias;
1930 return res;
1934 /* If the statement STMT may clobber the memory reference REF return true,
1935 otherwise return false. */
1937 bool
1938 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1940 if (is_gimple_call (stmt))
1942 tree lhs = gimple_call_lhs (stmt);
1943 if (lhs
1944 && TREE_CODE (lhs) != SSA_NAME)
1946 ao_ref r;
1947 ao_ref_init (&r, lhs);
1948 if (refs_may_alias_p_1 (ref, &r, true))
1949 return true;
1952 return call_may_clobber_ref_p_1 (stmt, ref);
1954 else if (gimple_assign_single_p (stmt))
1956 tree lhs = gimple_assign_lhs (stmt);
1957 if (TREE_CODE (lhs) != SSA_NAME)
1959 ao_ref r;
1960 ao_ref_init (&r, lhs);
1961 return refs_may_alias_p_1 (ref, &r, true);
1964 else if (gimple_code (stmt) == GIMPLE_ASM)
1965 return true;
1967 return false;
1970 bool
1971 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1973 ao_ref r;
1974 ao_ref_init (&r, ref);
1975 return stmt_may_clobber_ref_p_1 (stmt, &r);
1978 /* If STMT kills the memory reference REF return true, otherwise
1979 return false. */
1981 static bool
1982 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1984 /* For a must-alias check we need to be able to constrain
1985 the access properly. */
1986 ao_ref_base (ref);
1987 if (ref->max_size == -1)
1988 return false;
1990 if (gimple_has_lhs (stmt)
1991 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1992 /* The assignment is not necessarily carried out if it can throw
1993 and we can catch it in the current function where we could inspect
1994 the previous value.
1995 ??? We only need to care about the RHS throwing. For aggregate
1996 assignments or similar calls and non-call exceptions the LHS
1997 might throw as well. */
1998 && !stmt_can_throw_internal (stmt))
2000 tree base, lhs = gimple_get_lhs (stmt);
2001 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2002 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2003 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2004 so base == ref->base does not always hold. */
2005 if (base != ref->base)
2007 /* If both base and ref->base are MEM_REFs, only compare the
2008 first operand, and if the second operand isn't equal constant,
2009 try to add the offsets into offset and ref_offset. */
2010 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2011 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2013 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2014 TREE_OPERAND (ref->base, 1)))
2016 double_int off1 = mem_ref_offset (base);
2017 off1 = off1.lshift (BITS_PER_UNIT == 8
2018 ? 3 : exact_log2 (BITS_PER_UNIT));
2019 off1 = off1 + double_int::from_shwi (offset);
2020 double_int off2 = mem_ref_offset (ref->base);
2021 off2 = off2.lshift (BITS_PER_UNIT == 8
2022 ? 3 : exact_log2 (BITS_PER_UNIT));
2023 off2 = off2 + double_int::from_shwi (ref_offset);
2024 if (off1.fits_shwi () && off2.fits_shwi ())
2026 offset = off1.to_shwi ();
2027 ref_offset = off2.to_shwi ();
2029 else
2030 size = -1;
2033 else
2034 size = -1;
2036 /* For a must-alias check we need to be able to constrain
2037 the access properly. */
2038 if (size != -1 && size == max_size)
2040 if (offset <= ref_offset
2041 && offset + size >= ref_offset + ref->max_size)
2042 return true;
2046 if (is_gimple_call (stmt))
2048 tree callee = gimple_call_fndecl (stmt);
2049 if (callee != NULL_TREE
2050 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2051 switch (DECL_FUNCTION_CODE (callee))
2053 case BUILT_IN_MEMCPY:
2054 case BUILT_IN_MEMPCPY:
2055 case BUILT_IN_MEMMOVE:
2056 case BUILT_IN_MEMSET:
2057 case BUILT_IN_MEMCPY_CHK:
2058 case BUILT_IN_MEMPCPY_CHK:
2059 case BUILT_IN_MEMMOVE_CHK:
2060 case BUILT_IN_MEMSET_CHK:
2062 tree dest = gimple_call_arg (stmt, 0);
2063 tree len = gimple_call_arg (stmt, 2);
2064 tree base = NULL_TREE;
2065 HOST_WIDE_INT offset = 0;
2066 if (!host_integerp (len, 0))
2067 return false;
2068 if (TREE_CODE (dest) == ADDR_EXPR)
2069 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
2070 &offset);
2071 else if (TREE_CODE (dest) == SSA_NAME)
2072 base = dest;
2073 if (base
2074 && base == ao_ref_base (ref))
2076 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
2077 if (offset <= ref->offset / BITS_PER_UNIT
2078 && (offset + size
2079 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
2080 / BITS_PER_UNIT)))
2081 return true;
2083 break;
2086 case BUILT_IN_VA_END:
2088 tree ptr = gimple_call_arg (stmt, 0);
2089 if (TREE_CODE (ptr) == ADDR_EXPR)
2091 tree base = ao_ref_base (ref);
2092 if (TREE_OPERAND (ptr, 0) == base)
2093 return true;
2095 break;
2098 default:;
2101 return false;
2104 bool
2105 stmt_kills_ref_p (gimple stmt, tree ref)
2107 ao_ref r;
2108 ao_ref_init (&r, ref);
2109 return stmt_kills_ref_p_1 (stmt, &r);
2113 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2114 TARGET or a statement clobbering the memory reference REF in which
2115 case false is returned. The walk starts with VUSE, one argument of PHI. */
2117 static bool
2118 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2119 tree vuse, unsigned int *cnt, bitmap *visited,
2120 bool abort_on_visited)
2122 basic_block bb = gimple_bb (phi);
2124 if (!*visited)
2125 *visited = BITMAP_ALLOC (NULL);
2127 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2129 /* Walk until we hit the target. */
2130 while (vuse != target)
2132 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2133 /* Recurse for PHI nodes. */
2134 if (gimple_code (def_stmt) == GIMPLE_PHI)
2136 /* An already visited PHI node ends the walk successfully. */
2137 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2138 return !abort_on_visited;
2139 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2140 visited, abort_on_visited);
2141 if (!vuse)
2142 return false;
2143 continue;
2145 else if (gimple_nop_p (def_stmt))
2146 return false;
2147 else
2149 /* A clobbering statement or the end of the IL ends it failing. */
2150 ++*cnt;
2151 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2152 return false;
2154 /* If we reach a new basic-block see if we already skipped it
2155 in a previous walk that ended successfully. */
2156 if (gimple_bb (def_stmt) != bb)
2158 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2159 return !abort_on_visited;
2160 bb = gimple_bb (def_stmt);
2162 vuse = gimple_vuse (def_stmt);
2164 return true;
2167 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2168 until we hit the phi argument definition that dominates the other one.
2169 Return that, or NULL_TREE if there is no such definition. */
2171 static tree
2172 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2173 ao_ref *ref, unsigned int *cnt,
2174 bitmap *visited, bool abort_on_visited)
2176 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2177 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2178 tree common_vuse;
2180 if (arg0 == arg1)
2181 return arg0;
2182 else if (gimple_nop_p (def0)
2183 || (!gimple_nop_p (def1)
2184 && dominated_by_p (CDI_DOMINATORS,
2185 gimple_bb (def1), gimple_bb (def0))))
2187 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2188 visited, abort_on_visited))
2189 return arg0;
2191 else if (gimple_nop_p (def1)
2192 || dominated_by_p (CDI_DOMINATORS,
2193 gimple_bb (def0), gimple_bb (def1)))
2195 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2196 visited, abort_on_visited))
2197 return arg1;
2199 /* Special case of a diamond:
2200 MEM_1 = ...
2201 goto (cond) ? L1 : L2
2202 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2203 goto L3
2204 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2205 L3: MEM_4 = PHI<MEM_2, MEM_3>
2206 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2207 dominate each other, but still we can easily skip this PHI node
2208 if we recognize that the vuse MEM operand is the same for both,
2209 and that we can skip both statements (they don't clobber us).
2210 This is still linear. Don't use maybe_skip_until, that might
2211 potentially be slow. */
2212 else if ((common_vuse = gimple_vuse (def0))
2213 && common_vuse == gimple_vuse (def1))
2215 *cnt += 2;
2216 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2217 && !stmt_may_clobber_ref_p_1 (def1, ref))
2218 return common_vuse;
2221 return NULL_TREE;
2225 /* Starting from a PHI node for the virtual operand of the memory reference
2226 REF find a continuation virtual operand that allows to continue walking
2227 statements dominating PHI skipping only statements that cannot possibly
2228 clobber REF. Increments *CNT for each alias disambiguation done.
2229 Returns NULL_TREE if no suitable virtual operand can be found. */
2231 tree
2232 get_continuation_for_phi (gimple phi, ao_ref *ref,
2233 unsigned int *cnt, bitmap *visited,
2234 bool abort_on_visited)
2236 unsigned nargs = gimple_phi_num_args (phi);
2238 /* Through a single-argument PHI we can simply look through. */
2239 if (nargs == 1)
2240 return PHI_ARG_DEF (phi, 0);
2242 /* For two or more arguments try to pairwise skip non-aliasing code
2243 until we hit the phi argument definition that dominates the other one. */
2244 else if (nargs >= 2)
2246 tree arg0, arg1;
2247 unsigned i;
2249 /* Find a candidate for the virtual operand which definition
2250 dominates those of all others. */
2251 arg0 = PHI_ARG_DEF (phi, 0);
2252 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2253 for (i = 1; i < nargs; ++i)
2255 arg1 = PHI_ARG_DEF (phi, i);
2256 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2258 arg0 = arg1;
2259 break;
2261 if (dominated_by_p (CDI_DOMINATORS,
2262 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2263 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2264 arg0 = arg1;
2267 /* Then pairwise reduce against the found candidate. */
2268 for (i = 0; i < nargs; ++i)
2270 arg1 = PHI_ARG_DEF (phi, i);
2271 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2272 cnt, visited, abort_on_visited);
2273 if (!arg0)
2274 return NULL_TREE;
2277 return arg0;
2280 return NULL_TREE;
2283 /* Based on the memory reference REF and its virtual use VUSE call
2284 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2285 itself. That is, for each virtual use for which its defining statement
2286 does not clobber REF.
2288 WALKER is called with REF, the current virtual use and DATA. If
2289 WALKER returns non-NULL the walk stops and its result is returned.
2290 At the end of a non-successful walk NULL is returned.
2292 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2293 use which definition is a statement that may clobber REF and DATA.
2294 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2295 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2296 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2297 to adjust REF and *DATA to make that valid.
2299 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2301 void *
2302 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2303 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2304 void *(*translate)(ao_ref *, tree, void *), void *data)
2306 bitmap visited = NULL;
2307 void *res;
2308 unsigned int cnt = 0;
2309 bool translated = false;
2311 timevar_push (TV_ALIAS_STMT_WALK);
2315 gimple def_stmt;
2317 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2318 res = (*walker) (ref, vuse, cnt, data);
2319 /* Abort walk. */
2320 if (res == (void *)-1)
2322 res = NULL;
2323 break;
2325 /* Lookup succeeded. */
2326 else if (res != NULL)
2327 break;
2329 def_stmt = SSA_NAME_DEF_STMT (vuse);
2330 if (gimple_nop_p (def_stmt))
2331 break;
2332 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2333 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2334 &visited, translated);
2335 else
2337 cnt++;
2338 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2340 if (!translate)
2341 break;
2342 res = (*translate) (ref, vuse, data);
2343 /* Failed lookup and translation. */
2344 if (res == (void *)-1)
2346 res = NULL;
2347 break;
2349 /* Lookup succeeded. */
2350 else if (res != NULL)
2351 break;
2352 /* Translation succeeded, continue walking. */
2353 translated = true;
2355 vuse = gimple_vuse (def_stmt);
2358 while (vuse);
2360 if (visited)
2361 BITMAP_FREE (visited);
2363 timevar_pop (TV_ALIAS_STMT_WALK);
2365 return res;
2369 /* Based on the memory reference REF call WALKER for each vdef which
2370 defining statement may clobber REF, starting with VDEF. If REF
2371 is NULL_TREE, each defining statement is visited.
2373 WALKER is called with REF, the current vdef and DATA. If WALKER
2374 returns true the walk is stopped, otherwise it continues.
2376 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2377 PHI argument (but only one walk continues on merge points), the
2378 return value is true if any of the walks was successful.
2380 The function returns the number of statements walked. */
2382 static unsigned int
2383 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2384 bool (*walker)(ao_ref *, tree, void *), void *data,
2385 bitmap *visited, unsigned int cnt)
2389 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2391 if (*visited
2392 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2393 return cnt;
2395 if (gimple_nop_p (def_stmt))
2396 return cnt;
2397 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2399 unsigned i;
2400 if (!*visited)
2401 *visited = BITMAP_ALLOC (NULL);
2402 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2403 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2404 walker, data, visited, 0);
2405 return cnt;
2408 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2409 cnt++;
2410 if ((!ref
2411 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2412 && (*walker) (ref, vdef, data))
2413 return cnt;
2415 vdef = gimple_vuse (def_stmt);
2417 while (1);
2420 unsigned int
2421 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2422 bool (*walker)(ao_ref *, tree, void *), void *data,
2423 bitmap *visited)
2425 bitmap local_visited = NULL;
2426 unsigned int ret;
2428 timevar_push (TV_ALIAS_STMT_WALK);
2430 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2431 visited ? visited : &local_visited, 0);
2432 if (local_visited)
2433 BITMAP_FREE (local_visited);
2435 timevar_pop (TV_ALIAS_STMT_WALK);
2437 return ret;