Print SCoPs under CLooG format.
[official-gcc/graphite-test-results.git] / gcc / tree-ssa-alias.c
blob73a16375613ebca702afe0bc660cd971af679b17
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "ipa-type-escape.h"
46 #include "vec.h"
47 #include "bitmap.h"
48 #include "vecprim.h"
49 #include "pointer-set.h"
50 #include "alloc-pool.h"
51 #include "tree-ssa-alias.h"
53 /* Broad overview of how alias analysis on gimple works:
55 Statements clobbering or using memory are linked through the
56 virtual operand factored use-def chain. The virtual operand
57 is unique per function, its symbol is accessible via gimple_vop (cfun).
58 Virtual operands are used for efficiently walking memory statements
59 in the gimple IL and are useful for things like value-numbering as
60 a generation count for memory references.
62 SSA_NAME pointers may have associated points-to information
63 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
64 points-to information is (re-)computed by the TODO_rebuild_alias
65 pass manager todo. Points-to information is also used for more
66 precise tracking of call-clobbered and call-used variables and
67 related disambiguations.
69 This file contains functions for disambiguating memory references,
70 the so called alias-oracle and tools for walking of the gimple IL.
72 The main alias-oracle entry-points are
74 bool stmt_may_clobber_ref_p (gimple, tree)
76 This function queries if a statement may invalidate (parts of)
77 the memory designated by the reference tree argument.
79 bool ref_maybe_used_by_stmt_p (gimple, tree)
81 This function queries if a statement may need (parts of) the
82 memory designated by the reference tree argument.
84 There are variants of these functions that only handle the call
85 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
86 Note that these do not disambiguate against a possible call lhs.
88 bool refs_may_alias_p (tree, tree)
90 This function tries to disambiguate two reference trees.
92 bool ptr_deref_may_alias_global_p (tree)
94 This function queries if dereferencing a pointer variable may
95 alias global memory.
97 More low-level disambiguators are available and documented in
98 this file. Low-level disambiguators dealing with points-to
99 information are in tree-ssa-structalias.c. */
102 /* Query statistics for the different low-level disambiguators.
103 A high-level query may trigger multiple of them. */
105 static struct {
106 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
107 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
108 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
109 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
110 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
111 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
112 } alias_stats;
114 void
115 dump_alias_stats (FILE *s)
117 fprintf (s, "\nAlias oracle query stats:\n");
118 fprintf (s, " refs_may_alias_p: "
119 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
120 HOST_WIDE_INT_PRINT_DEC" queries\n",
121 alias_stats.refs_may_alias_p_no_alias,
122 alias_stats.refs_may_alias_p_no_alias
123 + alias_stats.refs_may_alias_p_may_alias);
124 fprintf (s, " ref_maybe_used_by_call_p: "
125 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
126 HOST_WIDE_INT_PRINT_DEC" queries\n",
127 alias_stats.ref_maybe_used_by_call_p_no_alias,
128 alias_stats.refs_may_alias_p_no_alias
129 + alias_stats.ref_maybe_used_by_call_p_may_alias);
130 fprintf (s, " call_may_clobber_ref_p: "
131 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
132 HOST_WIDE_INT_PRINT_DEC" queries\n",
133 alias_stats.call_may_clobber_ref_p_no_alias,
134 alias_stats.call_may_clobber_ref_p_no_alias
135 + alias_stats.call_may_clobber_ref_p_may_alias);
139 /* Return true, if dereferencing PTR may alias with a global variable. */
141 bool
142 ptr_deref_may_alias_global_p (tree ptr)
144 struct ptr_info_def *pi;
146 /* If we end up with a pointer constant here that may point
147 to global memory. */
148 if (TREE_CODE (ptr) != SSA_NAME)
149 return true;
151 pi = SSA_NAME_PTR_INFO (ptr);
153 /* If we do not have points-to information for this variable,
154 we have to punt. */
155 if (!pi)
156 return true;
158 /* ??? This does not use TBAA to prune globals ptr may not access. */
159 return pt_solution_includes_global (&pi->pt);
162 /* Return true if dereferencing PTR may alias DECL.
163 The caller is responsible for applying TBAA to see if PTR
164 may access DECL at all. */
166 static bool
167 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
169 struct ptr_info_def *pi;
171 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
172 || TREE_CODE (ptr) == ADDR_EXPR
173 || TREE_CODE (ptr) == INTEGER_CST)
174 && (TREE_CODE (decl) == VAR_DECL
175 || TREE_CODE (decl) == PARM_DECL
176 || TREE_CODE (decl) == RESULT_DECL));
178 /* Non-aliased variables can not be pointed to. */
179 if (!may_be_aliased (decl))
180 return false;
182 /* ADDR_EXPR pointers either just offset another pointer or directly
183 specify the pointed-to set. */
184 if (TREE_CODE (ptr) == ADDR_EXPR)
186 tree base = get_base_address (TREE_OPERAND (ptr, 0));
187 if (base
188 && INDIRECT_REF_P (base))
189 ptr = TREE_OPERAND (base, 0);
190 else if (base
191 && SSA_VAR_P (base))
192 return operand_equal_p (base, decl, 0);
193 else if (base
194 && CONSTANT_CLASS_P (base))
195 return false;
196 else
197 return true;
200 /* We can end up with dereferencing constant pointers.
201 Just bail out in this case. */
202 if (TREE_CODE (ptr) == INTEGER_CST)
203 return true;
205 /* If we do not have useful points-to information for this pointer
206 we cannot disambiguate anything else. */
207 pi = SSA_NAME_PTR_INFO (ptr);
208 if (!pi)
209 return true;
211 /* If the decl can be used as a restrict tag and we have a restrict
212 pointer and that pointers points-to set doesn't contain this decl
213 then they can't alias. */
214 if (DECL_RESTRICTED_P (decl)
215 && TYPE_RESTRICT (TREE_TYPE (ptr))
216 && pi->pt.vars_contains_restrict)
217 return bitmap_bit_p (pi->pt.vars, DECL_UID (decl));
219 return pt_solution_includes (&pi->pt, decl);
222 /* Return true if dereferenced PTR1 and PTR2 may alias.
223 The caller is responsible for applying TBAA to see if accesses
224 through PTR1 and PTR2 may conflict at all. */
226 static bool
227 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
229 struct ptr_info_def *pi1, *pi2;
231 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
232 || TREE_CODE (ptr1) == ADDR_EXPR
233 || TREE_CODE (ptr1) == INTEGER_CST)
234 && (TREE_CODE (ptr2) == SSA_NAME
235 || TREE_CODE (ptr2) == ADDR_EXPR
236 || TREE_CODE (ptr2) == INTEGER_CST));
238 /* ADDR_EXPR pointers either just offset another pointer or directly
239 specify the pointed-to set. */
240 if (TREE_CODE (ptr1) == ADDR_EXPR)
242 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
243 if (base
244 && INDIRECT_REF_P (base))
245 ptr1 = TREE_OPERAND (base, 0);
246 else if (base
247 && SSA_VAR_P (base))
248 return ptr_deref_may_alias_decl_p (ptr2, base);
249 else
250 return true;
252 if (TREE_CODE (ptr2) == ADDR_EXPR)
254 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
255 if (base
256 && INDIRECT_REF_P (base))
257 ptr2 = TREE_OPERAND (base, 0);
258 else if (base
259 && SSA_VAR_P (base))
260 return ptr_deref_may_alias_decl_p (ptr1, base);
261 else
262 return true;
265 /* We can end up with dereferencing constant pointers.
266 Just bail out in this case. */
267 if (TREE_CODE (ptr1) == INTEGER_CST
268 || TREE_CODE (ptr2) == INTEGER_CST)
269 return true;
271 /* We may end up with two empty points-to solutions for two same pointers.
272 In this case we still want to say both pointers alias, so shortcut
273 that here. */
274 if (ptr1 == ptr2)
275 return true;
277 /* If we do not have useful points-to information for either pointer
278 we cannot disambiguate anything else. */
279 pi1 = SSA_NAME_PTR_INFO (ptr1);
280 pi2 = SSA_NAME_PTR_INFO (ptr2);
281 if (!pi1 || !pi2)
282 return true;
284 /* If both pointers are restrict-qualified try to disambiguate
285 with restrict information. */
286 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
287 && TYPE_RESTRICT (TREE_TYPE (ptr2))
288 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
289 return false;
291 /* ??? This does not use TBAA to prune decls from the intersection
292 that not both pointers may access. */
293 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
296 /* Return true if dereferencing PTR may alias *REF.
297 The caller is responsible for applying TBAA to see if PTR
298 may access *REF at all. */
300 static bool
301 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
303 tree base = ao_ref_base (ref);
305 if (INDIRECT_REF_P (base))
306 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
307 else if (SSA_VAR_P (base))
308 return ptr_deref_may_alias_decl_p (ptr, base);
310 return true;
314 /* Dump alias information on FILE. */
316 void
317 dump_alias_info (FILE *file)
319 size_t i;
320 const char *funcname
321 = lang_hooks.decl_printable_name (current_function_decl, 2);
322 referenced_var_iterator rvi;
323 tree var;
325 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
327 fprintf (file, "Aliased symbols\n\n");
329 FOR_EACH_REFERENCED_VAR (var, rvi)
331 if (may_be_aliased (var))
332 dump_variable (file, var);
335 fprintf (file, "\nCall clobber information\n");
337 fprintf (file, "\nESCAPED");
338 dump_points_to_solution (file, &cfun->gimple_df->escaped);
339 fprintf (file, "\nCALLUSED");
340 dump_points_to_solution (file, &cfun->gimple_df->callused);
342 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
344 for (i = 1; i < num_ssa_names; i++)
346 tree ptr = ssa_name (i);
347 struct ptr_info_def *pi;
349 if (ptr == NULL_TREE
350 || SSA_NAME_IN_FREE_LIST (ptr))
351 continue;
353 pi = SSA_NAME_PTR_INFO (ptr);
354 if (pi)
355 dump_points_to_info_for (file, ptr);
358 fprintf (file, "\n");
362 /* Dump alias information on stderr. */
364 void
365 debug_alias_info (void)
367 dump_alias_info (stderr);
371 /* Return the alias information associated with pointer T. It creates a
372 new instance if none existed. */
374 struct ptr_info_def *
375 get_ptr_info (tree t)
377 struct ptr_info_def *pi;
379 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
381 pi = SSA_NAME_PTR_INFO (t);
382 if (pi == NULL)
384 pi = GGC_CNEW (struct ptr_info_def);
385 pt_solution_reset (&pi->pt);
386 SSA_NAME_PTR_INFO (t) = pi;
389 return pi;
392 /* Dump the points-to set *PT into FILE. */
394 void
395 dump_points_to_solution (FILE *file, struct pt_solution *pt)
397 if (pt->anything)
398 fprintf (file, ", points-to anything");
400 if (pt->nonlocal)
401 fprintf (file, ", points-to non-local");
403 if (pt->escaped)
404 fprintf (file, ", points-to escaped");
406 if (pt->null)
407 fprintf (file, ", points-to NULL");
409 if (pt->vars)
411 fprintf (file, ", points-to vars: ");
412 dump_decl_set (file, pt->vars);
413 if (pt->vars_contains_global)
414 fprintf (file, " (includes global vars)");
418 /* Dump points-to information for SSA_NAME PTR into FILE. */
420 void
421 dump_points_to_info_for (FILE *file, tree ptr)
423 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
425 print_generic_expr (file, ptr, dump_flags);
427 if (pi)
428 dump_points_to_solution (file, &pi->pt);
429 else
430 fprintf (file, ", points-to anything");
432 fprintf (file, "\n");
436 /* Dump points-to information for VAR into stderr. */
438 void
439 debug_points_to_info_for (tree var)
441 dump_points_to_info_for (stderr, var);
445 /* Initializes the alias-oracle reference representation *R from REF. */
447 void
448 ao_ref_init (ao_ref *r, tree ref)
450 r->ref = ref;
451 r->base = NULL_TREE;
452 r->offset = 0;
453 r->size = -1;
454 r->max_size = -1;
455 r->ref_alias_set = -1;
456 r->base_alias_set = -1;
459 /* Returns the base object of the memory reference *REF. */
461 tree
462 ao_ref_base (ao_ref *ref)
464 if (ref->base)
465 return ref->base;
466 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
467 &ref->max_size);
468 return ref->base;
471 /* Returns the base object alias set of the memory reference *REF. */
473 static alias_set_type ATTRIBUTE_UNUSED
474 ao_ref_base_alias_set (ao_ref *ref)
476 if (ref->base_alias_set != -1)
477 return ref->base_alias_set;
478 ref->base_alias_set = get_alias_set (ao_ref_base (ref));
479 return ref->base_alias_set;
482 /* Returns the reference alias set of the memory reference *REF. */
484 alias_set_type
485 ao_ref_alias_set (ao_ref *ref)
487 if (ref->ref_alias_set != -1)
488 return ref->ref_alias_set;
489 ref->ref_alias_set = get_alias_set (ref->ref);
490 return ref->ref_alias_set;
493 /* Init an alias-oracle reference representation from a gimple pointer
494 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
495 size is assumed to be unknown. The access is assumed to be only
496 to or after of the pointer target, not before it. */
498 void
499 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
501 HOST_WIDE_INT t1, t2;
502 ref->ref = NULL_TREE;
503 if (TREE_CODE (ptr) == ADDR_EXPR)
504 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
505 &ref->offset, &t1, &t2);
506 else
508 ref->base = build1 (INDIRECT_REF, char_type_node, ptr);
509 ref->offset = 0;
511 if (size
512 && host_integerp (size, 0)
513 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
514 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
515 else
516 ref->max_size = ref->size = -1;
517 ref->ref_alias_set = 0;
518 ref->base_alias_set = 0;
521 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
522 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
523 decide. */
525 static inline int
526 same_type_for_tbaa (tree type1, tree type2)
528 type1 = TYPE_MAIN_VARIANT (type1);
529 type2 = TYPE_MAIN_VARIANT (type2);
531 /* If we would have to do structural comparison bail out. */
532 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
533 || TYPE_STRUCTURAL_EQUALITY_P (type2))
534 return -1;
536 /* Compare the canonical types. */
537 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
538 return 1;
540 /* ??? Array types are not properly unified in all cases as we have
541 spurious changes in the index types for example. Removing this
542 causes all sorts of problems with the Fortran frontend. */
543 if (TREE_CODE (type1) == ARRAY_TYPE
544 && TREE_CODE (type2) == ARRAY_TYPE)
545 return -1;
547 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
548 object of one of its constrained subtypes, e.g. when a function with an
549 unconstrained parameter passed by reference is called on an object and
550 inlined. But, even in the case of a fixed size, type and subtypes are
551 not equivalent enough as to share the same TYPE_CANONICAL, since this
552 would mean that conversions between them are useless, whereas they are
553 not (e.g. type and subtypes can have different modes). So, in the end,
554 they are only guaranteed to have the same alias set. */
555 if (get_alias_set (type1) == get_alias_set (type2))
556 return -1;
558 /* The types are known to be not equal. */
559 return 0;
562 /* Determine if the two component references REF1 and REF2 which are
563 based on access types TYPE1 and TYPE2 and of which at least one is based
564 on an indirect reference may alias. */
566 static bool
567 aliasing_component_refs_p (tree ref1, tree type1,
568 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
569 tree ref2, tree type2,
570 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
572 /* If one reference is a component references through pointers try to find a
573 common base and apply offset based disambiguation. This handles
574 for example
575 struct A { int i; int j; } *q;
576 struct B { struct A a; int k; } *p;
577 disambiguating q->i and p->a.j. */
578 tree *refp;
579 int same_p;
581 /* Now search for the type1 in the access path of ref2. This
582 would be a common base for doing offset based disambiguation on. */
583 refp = &ref2;
584 while (handled_component_p (*refp)
585 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
586 refp = &TREE_OPERAND (*refp, 0);
587 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
588 /* If we couldn't compare types we have to bail out. */
589 if (same_p == -1)
590 return true;
591 else if (same_p == 1)
593 HOST_WIDE_INT offadj, sztmp, msztmp;
594 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
595 offset2 -= offadj;
596 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
598 /* If we didn't find a common base, try the other way around. */
599 refp = &ref1;
600 while (handled_component_p (*refp)
601 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
602 refp = &TREE_OPERAND (*refp, 0);
603 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
604 /* If we couldn't compare types we have to bail out. */
605 if (same_p == -1)
606 return true;
607 else if (same_p == 1)
609 HOST_WIDE_INT offadj, sztmp, msztmp;
610 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
611 offset1 -= offadj;
612 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
614 /* If we have two type access paths B1.path1 and B2.path2 they may
615 only alias if either B1 is in B2.path2 or B2 is in B1.path1. */
616 return false;
619 /* Return true if two memory references based on the variables BASE1
620 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
621 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
623 static bool
624 decl_refs_may_alias_p (tree base1,
625 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
626 tree base2,
627 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
629 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
631 /* If both references are based on different variables, they cannot alias. */
632 if (!operand_equal_p (base1, base2, 0))
633 return false;
635 /* If both references are based on the same variable, they cannot alias if
636 the accesses do not overlap. */
637 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
640 /* Return true if an indirect reference based on *PTR1 constrained
641 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
642 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
643 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
644 in which case they are computed on-demand. REF1 and REF2
645 if non-NULL are the complete memory reference trees. */
647 static bool
648 indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
649 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
650 alias_set_type base1_alias_set,
651 tree ref2, tree base2,
652 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
653 alias_set_type base2_alias_set)
655 /* If only one reference is based on a variable, they cannot alias if
656 the pointer access is beyond the extent of the variable access.
657 (the pointer base cannot validly point to an offset less than zero
658 of the variable).
659 They also cannot alias if the pointer may not point to the decl. */
660 if (max_size2 != -1
661 && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
662 return false;
663 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
664 return false;
666 /* Disambiguations that rely on strict aliasing rules follow. */
667 if (!flag_strict_aliasing)
668 return true;
670 /* If the alias set for a pointer access is zero all bets are off. */
671 if (base1_alias_set == -1)
672 base1_alias_set = get_deref_alias_set (ptr1);
673 if (base1_alias_set == 0)
674 return true;
675 if (base2_alias_set == -1)
676 base2_alias_set = get_alias_set (base2);
678 /* If both references are through the same type, they do not alias
679 if the accesses do not overlap. This does extra disambiguation
680 for mixed/pointer accesses but requires strict aliasing. */
681 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
682 TREE_TYPE (base2)) == 1)
683 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
685 /* The only way to access a variable is through a pointer dereference
686 of the same alias set or a subset of it. */
687 if (base1_alias_set != base2_alias_set
688 && !alias_set_subset_of (base1_alias_set, base2_alias_set))
689 return false;
691 /* Do access-path based disambiguation. */
692 if (ref1 && ref2
693 && handled_component_p (ref1)
694 && handled_component_p (ref2))
695 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
696 offset1, max_size1,
697 ref2, TREE_TYPE (base2),
698 offset2, max_size2);
700 return true;
703 /* Return true if two indirect references based on *PTR1
704 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
705 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
706 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
707 in which case they are computed on-demand. REF1 and REF2
708 if non-NULL are the complete memory reference trees. */
710 static bool
711 indirect_refs_may_alias_p (tree ref1, tree ptr1,
712 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
713 alias_set_type base1_alias_set,
714 tree ref2, tree ptr2,
715 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
716 alias_set_type base2_alias_set)
718 /* If both bases are based on pointers they cannot alias if they may not
719 point to the same memory object or if they point to the same object
720 and the accesses do not overlap. */
721 if (operand_equal_p (ptr1, ptr2, 0))
722 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
723 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
724 return false;
726 /* Disambiguations that rely on strict aliasing rules follow. */
727 if (!flag_strict_aliasing)
728 return true;
730 /* If the alias set for a pointer access is zero all bets are off. */
731 if (base1_alias_set == -1)
732 base1_alias_set = get_deref_alias_set (ptr1);
733 if (base1_alias_set == 0)
734 return true;
735 if (base2_alias_set == -1)
736 base2_alias_set = get_deref_alias_set (ptr2);
737 if (base2_alias_set == 0)
738 return true;
740 /* If both references are through the same type, they do not alias
741 if the accesses do not overlap. This does extra disambiguation
742 for mixed/pointer accesses but requires strict aliasing. */
743 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
744 TREE_TYPE (TREE_TYPE (ptr2))) == 1)
745 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
747 /* Do type-based disambiguation. */
748 if (base1_alias_set != base2_alias_set
749 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
750 return false;
752 /* Do access-path based disambiguation. */
753 if (ref1 && ref2
754 && handled_component_p (ref1)
755 && handled_component_p (ref2))
756 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
757 offset1, max_size1,
758 ref2, TREE_TYPE (TREE_TYPE (ptr2)),
759 offset2, max_size2);
761 return true;
764 /* Return true, if the two memory references REF1 and REF2 may alias. */
766 bool
767 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
769 tree base1, base2;
770 HOST_WIDE_INT offset1 = 0, offset2 = 0;
771 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
772 bool var1_p, var2_p, ind1_p, ind2_p;
773 alias_set_type set;
775 gcc_assert ((!ref1->ref
776 || SSA_VAR_P (ref1->ref)
777 || handled_component_p (ref1->ref)
778 || INDIRECT_REF_P (ref1->ref)
779 || TREE_CODE (ref1->ref) == TARGET_MEM_REF
780 || TREE_CODE (ref1->ref) == CONST_DECL)
781 && (!ref2->ref
782 || SSA_VAR_P (ref2->ref)
783 || handled_component_p (ref2->ref)
784 || INDIRECT_REF_P (ref2->ref)
785 || TREE_CODE (ref2->ref) == TARGET_MEM_REF
786 || TREE_CODE (ref2->ref) == CONST_DECL));
788 /* Decompose the references into their base objects and the access. */
789 base1 = ao_ref_base (ref1);
790 offset1 = ref1->offset;
791 max_size1 = ref1->max_size;
792 base2 = ao_ref_base (ref2);
793 offset2 = ref2->offset;
794 max_size2 = ref2->max_size;
796 /* We can end up with registers or constants as bases for example from
797 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
798 which is seen as a struct copy. */
799 if (TREE_CODE (base1) == SSA_NAME
800 || TREE_CODE (base2) == SSA_NAME
801 || TREE_CODE (base1) == CONST_DECL
802 || TREE_CODE (base2) == CONST_DECL
803 || is_gimple_min_invariant (base1)
804 || is_gimple_min_invariant (base2))
805 return false;
807 /* We can end up refering to code via function decls. As we likely
808 do not properly track code aliases conservatively bail out. */
809 if (TREE_CODE (base1) == FUNCTION_DECL
810 || TREE_CODE (base2) == FUNCTION_DECL)
811 return true;
813 /* Defer to simple offset based disambiguation if we have
814 references based on two decls. Do this before defering to
815 TBAA to handle must-alias cases in conformance with the
816 GCC extension of allowing type-punning through unions. */
817 var1_p = SSA_VAR_P (base1);
818 var2_p = SSA_VAR_P (base2);
819 if (var1_p && var2_p)
820 return decl_refs_may_alias_p (base1, offset1, max_size1,
821 base2, offset2, max_size2);
823 ind1_p = INDIRECT_REF_P (base1);
824 ind2_p = INDIRECT_REF_P (base2);
825 /* Canonicalize the pointer-vs-decl case. */
826 if (ind1_p && var2_p)
828 HOST_WIDE_INT tmp1;
829 tree tmp2;
830 ao_ref *tmp3;
831 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
832 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
833 tmp2 = base1; base1 = base2; base2 = tmp2;
834 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
835 var1_p = true;
836 ind1_p = false;
837 var2_p = false;
838 ind2_p = true;
841 /* If we are about to disambiguate pointer-vs-decl try harder to
842 see must-aliases and give leeway to some invalid cases.
843 This covers a pretty minimal set of cases only and does not
844 when called from the RTL oracle. It handles cases like
846 int i = 1;
847 return *(float *)&i;
849 and also fixes gfortran.dg/lto/pr40725. */
850 if (var1_p && ind2_p
851 && cfun
852 && gimple_in_ssa_p (cfun)
853 && TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME)
855 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base2, 0));
856 while (is_gimple_assign (def_stmt)
857 && (gimple_assign_rhs_code (def_stmt) == SSA_NAME
858 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))))
860 tree rhs = gimple_assign_rhs1 (def_stmt);
861 HOST_WIDE_INT offset, size, max_size;
863 /* Look through SSA name copies and pointer conversions. */
864 if (TREE_CODE (rhs) == SSA_NAME
865 && POINTER_TYPE_P (TREE_TYPE (rhs)))
867 def_stmt = SSA_NAME_DEF_STMT (rhs);
868 continue;
870 if (TREE_CODE (rhs) != ADDR_EXPR)
871 break;
873 /* If the pointer is defined as an address based on a decl
874 use plain offset disambiguation and ignore TBAA. */
875 rhs = TREE_OPERAND (rhs, 0);
876 rhs = get_ref_base_and_extent (rhs, &offset, &size, &max_size);
877 if (SSA_VAR_P (rhs))
879 base2 = rhs;
880 offset2 += offset;
881 if (size != max_size
882 || max_size == -1)
883 max_size2 = -1;
884 return decl_refs_may_alias_p (base1, offset1, max_size1,
885 base2, offset2, max_size2);
888 /* Do not continue looking through &p->x to limit time
889 complexity. */
890 break;
894 /* First defer to TBAA if possible. */
895 if (tbaa_p
896 && flag_strict_aliasing
897 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
898 ao_ref_alias_set (ref2)))
899 return false;
901 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
902 TBAA disambiguation based on the access type is possible, so bail
903 out only after that check. */
904 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
905 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
906 return true;
908 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
909 set = tbaa_p ? -1 : 0;
910 if (var1_p && ind2_p)
911 return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
912 offset2, max_size2, set,
913 ref1->ref, base1,
914 offset1, max_size1, set);
915 else if (ind1_p && ind2_p)
916 return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
917 offset1, max_size1, set,
918 ref2->ref, TREE_OPERAND (base2, 0),
919 offset2, max_size2, set);
921 gcc_unreachable ();
924 bool
925 refs_may_alias_p (tree ref1, tree ref2)
927 ao_ref r1, r2;
928 bool res;
929 ao_ref_init (&r1, ref1);
930 ao_ref_init (&r2, ref2);
931 res = refs_may_alias_p_1 (&r1, &r2, true);
932 if (res)
933 ++alias_stats.refs_may_alias_p_may_alias;
934 else
935 ++alias_stats.refs_may_alias_p_no_alias;
936 return res;
939 /* Returns true if there is a anti-dependence for the STORE that
940 executes after the LOAD. */
942 bool
943 refs_anti_dependent_p (tree load, tree store)
945 ao_ref r1, r2;
946 ao_ref_init (&r1, load);
947 ao_ref_init (&r2, store);
948 return refs_may_alias_p_1 (&r1, &r2, false);
951 /* Returns true if there is a output dependence for the stores
952 STORE1 and STORE2. */
954 bool
955 refs_output_dependent_p (tree store1, tree store2)
957 ao_ref r1, r2;
958 ao_ref_init (&r1, store1);
959 ao_ref_init (&r2, store2);
960 return refs_may_alias_p_1 (&r1, &r2, false);
963 /* If the call CALL may use the memory reference REF return true,
964 otherwise return false. */
966 static bool
967 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
969 tree base, callee;
970 unsigned i;
971 int flags = gimple_call_flags (call);
973 /* Const functions without a static chain do not implicitly use memory. */
974 if (!gimple_call_chain (call)
975 && (flags & (ECF_CONST|ECF_NOVOPS)))
976 goto process_args;
978 base = ao_ref_base (ref);
979 if (!base)
980 return true;
982 /* If the reference is based on a decl that is not aliased the call
983 cannot possibly use it. */
984 if (DECL_P (base)
985 && !may_be_aliased (base)
986 /* But local statics can be used through recursion. */
987 && !is_global_var (base))
988 goto process_args;
990 callee = gimple_call_fndecl (call);
992 /* Handle those builtin functions explicitly that do not act as
993 escape points. See tree-ssa-structalias.c:find_func_aliases
994 for the list of builtins we might need to handle here. */
995 if (callee != NULL_TREE
996 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
997 switch (DECL_FUNCTION_CODE (callee))
999 /* All the following functions clobber memory pointed to by
1000 their first argument. */
1001 case BUILT_IN_STRCPY:
1002 case BUILT_IN_STRNCPY:
1003 case BUILT_IN_MEMCPY:
1004 case BUILT_IN_MEMMOVE:
1005 case BUILT_IN_MEMPCPY:
1006 case BUILT_IN_STPCPY:
1007 case BUILT_IN_STPNCPY:
1008 case BUILT_IN_STRCAT:
1009 case BUILT_IN_STRNCAT:
1011 ao_ref dref;
1012 tree size = NULL_TREE;
1013 if (gimple_call_num_args (call) == 3)
1014 size = gimple_call_arg (call, 2);
1015 ao_ref_init_from_ptr_and_size (&dref,
1016 gimple_call_arg (call, 1),
1017 size);
1018 return refs_may_alias_p_1 (&dref, ref, false);
1020 case BUILT_IN_BCOPY:
1022 ao_ref dref;
1023 tree size = gimple_call_arg (call, 2);
1024 ao_ref_init_from_ptr_and_size (&dref,
1025 gimple_call_arg (call, 0),
1026 size);
1027 return refs_may_alias_p_1 (&dref, ref, false);
1029 /* The following builtins do not read from memory. */
1030 case BUILT_IN_FREE:
1031 case BUILT_IN_MALLOC:
1032 case BUILT_IN_CALLOC:
1033 case BUILT_IN_MEMSET:
1034 case BUILT_IN_FREXP:
1035 case BUILT_IN_FREXPF:
1036 case BUILT_IN_FREXPL:
1037 case BUILT_IN_GAMMA_R:
1038 case BUILT_IN_GAMMAF_R:
1039 case BUILT_IN_GAMMAL_R:
1040 case BUILT_IN_LGAMMA_R:
1041 case BUILT_IN_LGAMMAF_R:
1042 case BUILT_IN_LGAMMAL_R:
1043 case BUILT_IN_MODF:
1044 case BUILT_IN_MODFF:
1045 case BUILT_IN_MODFL:
1046 case BUILT_IN_REMQUO:
1047 case BUILT_IN_REMQUOF:
1048 case BUILT_IN_REMQUOL:
1049 case BUILT_IN_SINCOS:
1050 case BUILT_IN_SINCOSF:
1051 case BUILT_IN_SINCOSL:
1052 return false;
1054 default:
1055 /* Fallthru to general call handling. */;
1058 /* Check if base is a global static variable that is not read
1059 by the function. */
1060 if (TREE_CODE (base) == VAR_DECL
1061 && TREE_STATIC (base)
1062 && !TREE_PUBLIC (base))
1064 bitmap not_read;
1066 if (callee != NULL_TREE
1067 && (not_read
1068 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1069 && bitmap_bit_p (not_read, DECL_UID (base)))
1070 goto process_args;
1073 /* If the base variable is call-used or call-clobbered then
1074 it may be used. */
1075 if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1077 if (DECL_P (base))
1079 if (is_call_used (base))
1080 return true;
1082 else if (INDIRECT_REF_P (base)
1083 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1085 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1086 if (!pi)
1087 return true;
1089 if (pt_solution_includes_global (&pi->pt)
1090 || pt_solutions_intersect (&cfun->gimple_df->callused, &pi->pt)
1091 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
1092 return true;
1094 else
1095 return true;
1097 else
1099 if (DECL_P (base))
1101 if (is_call_clobbered (base))
1102 return true;
1104 else if (INDIRECT_REF_P (base)
1105 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1107 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1108 if (!pi)
1109 return true;
1111 if (pt_solution_includes_global (&pi->pt)
1112 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
1113 return true;
1115 else
1116 return true;
1119 /* Inspect call arguments for passed-by-value aliases. */
1120 process_args:
1121 for (i = 0; i < gimple_call_num_args (call); ++i)
1123 tree op = gimple_call_arg (call, i);
1125 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1126 op = TREE_OPERAND (op, 0);
1128 if (TREE_CODE (op) != SSA_NAME
1129 && !is_gimple_min_invariant (op))
1131 ao_ref r;
1132 ao_ref_init (&r, op);
1133 if (refs_may_alias_p_1 (&r, ref, true))
1134 return true;
1138 return false;
1141 static bool
1142 ref_maybe_used_by_call_p (gimple call, tree ref)
1144 ao_ref r;
1145 bool res;
1146 ao_ref_init (&r, ref);
1147 res = ref_maybe_used_by_call_p_1 (call, &r);
1148 if (res)
1149 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1150 else
1151 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1152 return res;
1156 /* If the statement STMT may use the memory reference REF return
1157 true, otherwise return false. */
1159 bool
1160 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1162 if (is_gimple_assign (stmt))
1164 tree rhs;
1166 /* All memory assign statements are single. */
1167 if (!gimple_assign_single_p (stmt))
1168 return false;
1170 rhs = gimple_assign_rhs1 (stmt);
1171 if (is_gimple_reg (rhs)
1172 || is_gimple_min_invariant (rhs)
1173 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1174 return false;
1176 return refs_may_alias_p (rhs, ref);
1178 else if (is_gimple_call (stmt))
1179 return ref_maybe_used_by_call_p (stmt, ref);
1181 return true;
1184 /* If the call in statement CALL may clobber the memory reference REF
1185 return true, otherwise return false. */
1187 static bool
1188 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1190 tree base;
1191 tree callee;
1193 /* If the call is pure or const it cannot clobber anything. */
1194 if (gimple_call_flags (call)
1195 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1196 return false;
1198 base = ao_ref_base (ref);
1199 if (!base)
1200 return true;
1202 if (TREE_CODE (base) == SSA_NAME
1203 || CONSTANT_CLASS_P (base))
1204 return false;
1206 /* If the reference is based on a decl that is not aliased the call
1207 cannot possibly clobber it. */
1208 if (DECL_P (base)
1209 && !may_be_aliased (base)
1210 /* But local non-readonly statics can be modified through recursion
1211 or the call may implement a threading barrier which we must
1212 treat as may-def. */
1213 && (TREE_READONLY (base)
1214 || !is_global_var (base)))
1215 return false;
1217 callee = gimple_call_fndecl (call);
1219 /* Handle those builtin functions explicitly that do not act as
1220 escape points. See tree-ssa-structalias.c:find_func_aliases
1221 for the list of builtins we might need to handle here. */
1222 if (callee != NULL_TREE
1223 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1224 switch (DECL_FUNCTION_CODE (callee))
1226 /* All the following functions clobber memory pointed to by
1227 their first argument. */
1228 case BUILT_IN_STRCPY:
1229 case BUILT_IN_STRNCPY:
1230 case BUILT_IN_MEMCPY:
1231 case BUILT_IN_MEMMOVE:
1232 case BUILT_IN_MEMPCPY:
1233 case BUILT_IN_STPCPY:
1234 case BUILT_IN_STPNCPY:
1235 case BUILT_IN_STRCAT:
1236 case BUILT_IN_STRNCAT:
1237 case BUILT_IN_MEMSET:
1239 ao_ref dref;
1240 tree size = NULL_TREE;
1241 if (gimple_call_num_args (call) == 3)
1242 size = gimple_call_arg (call, 2);
1243 ao_ref_init_from_ptr_and_size (&dref,
1244 gimple_call_arg (call, 0),
1245 size);
1246 return refs_may_alias_p_1 (&dref, ref, false);
1248 case BUILT_IN_BCOPY:
1250 ao_ref dref;
1251 tree size = gimple_call_arg (call, 2);
1252 ao_ref_init_from_ptr_and_size (&dref,
1253 gimple_call_arg (call, 1),
1254 size);
1255 return refs_may_alias_p_1 (&dref, ref, false);
1257 /* Allocating memory does not have any side-effects apart from
1258 being the definition point for the pointer. */
1259 case BUILT_IN_MALLOC:
1260 case BUILT_IN_CALLOC:
1261 /* Unix98 specifies that errno is set on allocation failure.
1262 Until we properly can track the errno location assume it
1263 is not a local decl but external or anonymous storage in
1264 a different translation unit. Also assume it is of
1265 type int as required by the standard. */
1266 if (flag_errno_math
1267 && TREE_TYPE (base) == integer_type_node)
1269 struct ptr_info_def *pi;
1270 if (DECL_P (base)
1271 && !TREE_STATIC (base))
1272 return true;
1273 else if (INDIRECT_REF_P (base)
1274 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1275 && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1276 return pi->pt.anything || pi->pt.nonlocal;
1278 return false;
1279 /* Freeing memory kills the pointed-to memory. More importantly
1280 the call has to serve as a barrier for moving loads and stores
1281 across it. */
1282 case BUILT_IN_FREE:
1284 tree ptr = gimple_call_arg (call, 0);
1285 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1287 case BUILT_IN_GAMMA_R:
1288 case BUILT_IN_GAMMAF_R:
1289 case BUILT_IN_GAMMAL_R:
1290 case BUILT_IN_LGAMMA_R:
1291 case BUILT_IN_LGAMMAF_R:
1292 case BUILT_IN_LGAMMAL_R:
1294 tree out = gimple_call_arg (call, 1);
1295 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1296 return true;
1297 if (flag_errno_math)
1298 break;
1299 return false;
1301 case BUILT_IN_FREXP:
1302 case BUILT_IN_FREXPF:
1303 case BUILT_IN_FREXPL:
1304 case BUILT_IN_MODF:
1305 case BUILT_IN_MODFF:
1306 case BUILT_IN_MODFL:
1308 tree out = gimple_call_arg (call, 1);
1309 return ptr_deref_may_alias_ref_p_1 (out, ref);
1311 case BUILT_IN_REMQUO:
1312 case BUILT_IN_REMQUOF:
1313 case BUILT_IN_REMQUOL:
1315 tree out = gimple_call_arg (call, 2);
1316 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1317 return true;
1318 if (flag_errno_math)
1319 break;
1320 return false;
1322 case BUILT_IN_SINCOS:
1323 case BUILT_IN_SINCOSF:
1324 case BUILT_IN_SINCOSL:
1326 tree sin = gimple_call_arg (call, 1);
1327 tree cos = gimple_call_arg (call, 2);
1328 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1329 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1331 default:
1332 /* Fallthru to general call handling. */;
1335 /* Check if base is a global static variable that is not written
1336 by the function. */
1337 if (callee != NULL_TREE
1338 && TREE_CODE (base) == VAR_DECL
1339 && TREE_STATIC (base)
1340 && !TREE_PUBLIC (base))
1342 bitmap not_written;
1344 if ((not_written
1345 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1346 && bitmap_bit_p (not_written, DECL_UID (base)))
1347 return false;
1350 if (DECL_P (base))
1351 return is_call_clobbered (base);
1352 else if (INDIRECT_REF_P (base)
1353 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1355 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1356 if (!pi)
1357 return true;
1359 return (pt_solution_includes_global (&pi->pt)
1360 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt));
1363 return true;
1366 static bool ATTRIBUTE_UNUSED
1367 call_may_clobber_ref_p (gimple call, tree ref)
1369 bool res;
1370 ao_ref r;
1371 ao_ref_init (&r, ref);
1372 res = call_may_clobber_ref_p_1 (call, &r);
1373 if (res)
1374 ++alias_stats.call_may_clobber_ref_p_may_alias;
1375 else
1376 ++alias_stats.call_may_clobber_ref_p_no_alias;
1377 return res;
1381 /* If the statement STMT may clobber the memory reference REF return true,
1382 otherwise return false. */
1384 bool
1385 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1387 if (is_gimple_call (stmt))
1389 tree lhs = gimple_call_lhs (stmt);
1390 if (lhs
1391 && !is_gimple_reg (lhs))
1393 ao_ref r;
1394 ao_ref_init (&r, lhs);
1395 if (refs_may_alias_p_1 (ref, &r, true))
1396 return true;
1399 return call_may_clobber_ref_p_1 (stmt, ref);
1401 else if (is_gimple_assign (stmt))
1403 ao_ref r;
1404 ao_ref_init (&r, gimple_assign_lhs (stmt));
1405 return refs_may_alias_p_1 (ref, &r, true);
1407 else if (gimple_code (stmt) == GIMPLE_ASM)
1408 return true;
1410 return false;
1413 bool
1414 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1416 ao_ref r;
1417 ao_ref_init (&r, ref);
1418 return stmt_may_clobber_ref_p_1 (stmt, &r);
1422 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1423 TARGET or a statement clobbering the memory reference REF in which
1424 case false is returned. The walk starts with VUSE, one argument of PHI. */
1426 static bool
1427 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1428 tree vuse, bitmap *visited)
1430 if (!*visited)
1431 *visited = BITMAP_ALLOC (NULL);
1433 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1435 /* Walk until we hit the target. */
1436 while (vuse != target)
1438 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1439 /* Recurse for PHI nodes. */
1440 if (gimple_code (def_stmt) == GIMPLE_PHI)
1442 /* An already visited PHI node ends the walk successfully. */
1443 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1444 return true;
1445 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1446 if (!vuse)
1447 return false;
1448 continue;
1450 /* A clobbering statement or the end of the IL ends it failing. */
1451 else if (gimple_nop_p (def_stmt)
1452 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1453 return false;
1454 vuse = gimple_vuse (def_stmt);
1456 return true;
1459 /* Starting from a PHI node for the virtual operand of the memory reference
1460 REF find a continuation virtual operand that allows to continue walking
1461 statements dominating PHI skipping only statements that cannot possibly
1462 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1463 be found. */
1465 tree
1466 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1468 unsigned nargs = gimple_phi_num_args (phi);
1470 /* Through a single-argument PHI we can simply look through. */
1471 if (nargs == 1)
1472 return PHI_ARG_DEF (phi, 0);
1474 /* For two arguments try to skip non-aliasing code until we hit
1475 the phi argument definition that dominates the other one. */
1476 if (nargs == 2)
1478 tree arg0 = PHI_ARG_DEF (phi, 0);
1479 tree arg1 = PHI_ARG_DEF (phi, 1);
1480 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1481 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1482 tree common_vuse;
1484 if (arg0 == arg1)
1485 return arg0;
1486 else if (gimple_nop_p (def0)
1487 || (!gimple_nop_p (def1)
1488 && dominated_by_p (CDI_DOMINATORS,
1489 gimple_bb (def1), gimple_bb (def0))))
1491 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1492 return arg0;
1494 else if (gimple_nop_p (def1)
1495 || dominated_by_p (CDI_DOMINATORS,
1496 gimple_bb (def0), gimple_bb (def1)))
1498 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1499 return arg1;
1501 /* Special case of a diamond:
1502 MEM_1 = ...
1503 goto (cond) ? L1 : L2
1504 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1505 goto L3
1506 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1507 L3: MEM_4 = PHI<MEM_2, MEM_3>
1508 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1509 dominate each other, but still we can easily skip this PHI node
1510 if we recognize that the vuse MEM operand is the same for both,
1511 and that we can skip both statements (they don't clobber us).
1512 This is still linear. Don't use maybe_skip_until, that might
1513 potentially be slow. */
1514 else if ((common_vuse = gimple_vuse (def0))
1515 && common_vuse == gimple_vuse (def1))
1517 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1518 && !stmt_may_clobber_ref_p_1 (def1, ref))
1519 return common_vuse;
1523 return NULL_TREE;
1526 /* Based on the memory reference REF and its virtual use VUSE call
1527 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1528 itself. That is, for each virtual use for which its defining statement
1529 does not clobber REF.
1531 WALKER is called with REF, the current virtual use and DATA. If
1532 WALKER returns non-NULL the walk stops and its result is returned.
1533 At the end of a non-successful walk NULL is returned.
1535 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1536 use which definition is a statement that may clobber REF and DATA.
1537 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1538 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1539 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1540 to adjust REF and *DATA to make that valid.
1542 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1544 void *
1545 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1546 void *(*walker)(ao_ref *, tree, void *),
1547 void *(*translate)(ao_ref *, tree, void *), void *data)
1549 bitmap visited = NULL;
1550 void *res;
1552 timevar_push (TV_ALIAS_STMT_WALK);
1556 gimple def_stmt;
1558 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1559 res = (*walker) (ref, vuse, data);
1560 if (res)
1561 break;
1563 def_stmt = SSA_NAME_DEF_STMT (vuse);
1564 if (gimple_nop_p (def_stmt))
1565 break;
1566 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1567 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1568 else
1570 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1572 if (!translate)
1573 break;
1574 res = (*translate) (ref, vuse, data);
1575 /* Failed lookup and translation. */
1576 if (res == (void *)-1)
1578 res = NULL;
1579 break;
1581 /* Lookup succeeded. */
1582 else if (res != NULL)
1583 break;
1584 /* Translation succeeded, continue walking. */
1586 vuse = gimple_vuse (def_stmt);
1589 while (vuse);
1591 if (visited)
1592 BITMAP_FREE (visited);
1594 timevar_pop (TV_ALIAS_STMT_WALK);
1596 return res;
1600 /* Based on the memory reference REF call WALKER for each vdef which
1601 defining statement may clobber REF, starting with VDEF. If REF
1602 is NULL_TREE, each defining statement is visited.
1604 WALKER is called with REF, the current vdef and DATA. If WALKER
1605 returns true the walk is stopped, otherwise it continues.
1607 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1608 PHI argument (but only one walk continues on merge points), the
1609 return value is true if any of the walks was successful.
1611 The function returns the number of statements walked. */
1613 static unsigned int
1614 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1615 bool (*walker)(ao_ref *, tree, void *), void *data,
1616 bitmap *visited, unsigned int cnt)
1620 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1622 if (*visited
1623 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1624 return cnt;
1626 if (gimple_nop_p (def_stmt))
1627 return cnt;
1628 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1630 unsigned i;
1631 if (!*visited)
1632 *visited = BITMAP_ALLOC (NULL);
1633 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1634 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1635 walker, data, visited, 0);
1636 return cnt;
1639 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1640 cnt++;
1641 if ((!ref
1642 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1643 && (*walker) (ref, vdef, data))
1644 return cnt;
1646 vdef = gimple_vuse (def_stmt);
1648 while (1);
1651 unsigned int
1652 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1653 bool (*walker)(ao_ref *, tree, void *), void *data,
1654 bitmap *visited)
1656 bitmap local_visited = NULL;
1657 unsigned int ret;
1659 timevar_push (TV_ALIAS_STMT_WALK);
1661 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1662 visited ? visited : &local_visited, 0);
1663 if (local_visited)
1664 BITMAP_FREE (local_visited);
1666 timevar_pop (TV_ALIAS_STMT_WALK);
1668 return ret;