Support AVX for cmpss/cmpsd.
[official-gcc.git] / gcc / tree-ssa-alias.c
blob715c2f10f9a87edbd558f0e90393f82de9dc2c91
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 base == decl;
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_PT_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);
340 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
342 for (i = 1; i < num_ssa_names; i++)
344 tree ptr = ssa_name (i);
345 struct ptr_info_def *pi;
347 if (ptr == NULL_TREE
348 || SSA_NAME_IN_FREE_LIST (ptr))
349 continue;
351 pi = SSA_NAME_PTR_INFO (ptr);
352 if (pi)
353 dump_points_to_info_for (file, ptr);
356 fprintf (file, "\n");
360 /* Dump alias information on stderr. */
362 void
363 debug_alias_info (void)
365 dump_alias_info (stderr);
369 /* Return the alias information associated with pointer T. It creates a
370 new instance if none existed. */
372 struct ptr_info_def *
373 get_ptr_info (tree t)
375 struct ptr_info_def *pi;
377 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
379 pi = SSA_NAME_PTR_INFO (t);
380 if (pi == NULL)
382 pi = GGC_CNEW (struct ptr_info_def);
383 pt_solution_reset (&pi->pt);
384 SSA_NAME_PTR_INFO (t) = pi;
387 return pi;
390 /* Dump the points-to set *PT into FILE. */
392 void
393 dump_points_to_solution (FILE *file, struct pt_solution *pt)
395 if (pt->anything)
396 fprintf (file, ", points-to anything");
398 if (pt->nonlocal)
399 fprintf (file, ", points-to non-local");
401 if (pt->escaped)
402 fprintf (file, ", points-to escaped");
404 if (pt->ipa_escaped)
405 fprintf (file, ", points-to unit escaped");
407 if (pt->null)
408 fprintf (file, ", points-to NULL");
410 if (pt->vars)
412 fprintf (file, ", points-to vars: ");
413 dump_decl_set (file, pt->vars);
414 if (pt->vars_contains_global)
415 fprintf (file, " (includes global vars)");
416 if (pt->vars_contains_restrict)
417 fprintf (file, " (includes restrict tags)");
421 /* Dump points-to information for SSA_NAME PTR into FILE. */
423 void
424 dump_points_to_info_for (FILE *file, tree ptr)
426 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
428 print_generic_expr (file, ptr, dump_flags);
430 if (pi)
431 dump_points_to_solution (file, &pi->pt);
432 else
433 fprintf (file, ", points-to anything");
435 fprintf (file, "\n");
439 /* Dump points-to information for VAR into stderr. */
441 void
442 debug_points_to_info_for (tree var)
444 dump_points_to_info_for (stderr, var);
448 /* Initializes the alias-oracle reference representation *R from REF. */
450 void
451 ao_ref_init (ao_ref *r, tree ref)
453 r->ref = ref;
454 r->base = NULL_TREE;
455 r->offset = 0;
456 r->size = -1;
457 r->max_size = -1;
458 r->ref_alias_set = -1;
459 r->base_alias_set = -1;
462 /* Returns the base object of the memory reference *REF. */
464 tree
465 ao_ref_base (ao_ref *ref)
467 if (ref->base)
468 return ref->base;
469 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
470 &ref->max_size);
471 return ref->base;
474 /* Returns the base object alias set of the memory reference *REF. */
476 static alias_set_type ATTRIBUTE_UNUSED
477 ao_ref_base_alias_set (ao_ref *ref)
479 if (ref->base_alias_set != -1)
480 return ref->base_alias_set;
481 ref->base_alias_set = get_alias_set (ao_ref_base (ref));
482 return ref->base_alias_set;
485 /* Returns the reference alias set of the memory reference *REF. */
487 alias_set_type
488 ao_ref_alias_set (ao_ref *ref)
490 if (ref->ref_alias_set != -1)
491 return ref->ref_alias_set;
492 ref->ref_alias_set = get_alias_set (ref->ref);
493 return ref->ref_alias_set;
496 /* Init an alias-oracle reference representation from a gimple pointer
497 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
498 size is assumed to be unknown. The access is assumed to be only
499 to or after of the pointer target, not before it. */
501 void
502 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
504 HOST_WIDE_INT t1, t2;
505 ref->ref = NULL_TREE;
506 if (TREE_CODE (ptr) == ADDR_EXPR)
507 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
508 &ref->offset, &t1, &t2);
509 else
511 ref->base = build1 (INDIRECT_REF, char_type_node, ptr);
512 ref->offset = 0;
514 if (size
515 && host_integerp (size, 0)
516 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
517 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
518 else
519 ref->max_size = ref->size = -1;
520 ref->ref_alias_set = 0;
521 ref->base_alias_set = 0;
524 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
525 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
526 decide. */
528 static inline int
529 same_type_for_tbaa (tree type1, tree type2)
531 type1 = TYPE_MAIN_VARIANT (type1);
532 type2 = TYPE_MAIN_VARIANT (type2);
534 /* If we would have to do structural comparison bail out. */
535 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
536 || TYPE_STRUCTURAL_EQUALITY_P (type2))
537 return -1;
539 /* Compare the canonical types. */
540 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
541 return 1;
543 /* ??? Array types are not properly unified in all cases as we have
544 spurious changes in the index types for example. Removing this
545 causes all sorts of problems with the Fortran frontend. */
546 if (TREE_CODE (type1) == ARRAY_TYPE
547 && TREE_CODE (type2) == ARRAY_TYPE)
548 return -1;
550 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
551 object of one of its constrained subtypes, e.g. when a function with an
552 unconstrained parameter passed by reference is called on an object and
553 inlined. But, even in the case of a fixed size, type and subtypes are
554 not equivalent enough as to share the same TYPE_CANONICAL, since this
555 would mean that conversions between them are useless, whereas they are
556 not (e.g. type and subtypes can have different modes). So, in the end,
557 they are only guaranteed to have the same alias set. */
558 if (get_alias_set (type1) == get_alias_set (type2))
559 return -1;
561 /* The types are known to be not equal. */
562 return 0;
565 /* Determine if the two component references REF1 and REF2 which are
566 based on access types TYPE1 and TYPE2 and of which at least one is based
567 on an indirect reference may alias. */
569 static bool
570 aliasing_component_refs_p (tree ref1, tree type1,
571 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
572 tree ref2, tree type2,
573 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
575 /* If one reference is a component references through pointers try to find a
576 common base and apply offset based disambiguation. This handles
577 for example
578 struct A { int i; int j; } *q;
579 struct B { struct A a; int k; } *p;
580 disambiguating q->i and p->a.j. */
581 tree *refp;
582 int same_p;
584 /* Now search for the type1 in the access path of ref2. This
585 would be a common base for doing offset based disambiguation on. */
586 refp = &ref2;
587 while (handled_component_p (*refp)
588 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
589 refp = &TREE_OPERAND (*refp, 0);
590 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
591 /* If we couldn't compare types we have to bail out. */
592 if (same_p == -1)
593 return true;
594 else if (same_p == 1)
596 HOST_WIDE_INT offadj, sztmp, msztmp;
597 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
598 offset2 -= offadj;
599 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
601 /* If we didn't find a common base, try the other way around. */
602 refp = &ref1;
603 while (handled_component_p (*refp)
604 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
605 refp = &TREE_OPERAND (*refp, 0);
606 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
607 /* If we couldn't compare types we have to bail out. */
608 if (same_p == -1)
609 return true;
610 else if (same_p == 1)
612 HOST_WIDE_INT offadj, sztmp, msztmp;
613 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
614 offset1 -= offadj;
615 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
617 /* If we have two type access paths B1.path1 and B2.path2 they may
618 only alias if either B1 is in B2.path2 or B2 is in B1.path1. */
619 return false;
622 /* Return true if two memory references based on the variables BASE1
623 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
624 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
626 static bool
627 decl_refs_may_alias_p (tree base1,
628 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
629 tree base2,
630 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
632 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
634 /* If both references are based on different variables, they cannot alias. */
635 if (base1 != base2)
636 return false;
638 /* If both references are based on the same variable, they cannot alias if
639 the accesses do not overlap. */
640 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
643 /* Return true if an indirect reference based on *PTR1 constrained
644 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
645 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
646 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
647 in which case they are computed on-demand. REF1 and REF2
648 if non-NULL are the complete memory reference trees. */
650 static bool
651 indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
652 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
653 alias_set_type base1_alias_set,
654 tree ref2, tree base2,
655 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
656 alias_set_type base2_alias_set)
658 /* If only one reference is based on a variable, they cannot alias if
659 the pointer access is beyond the extent of the variable access.
660 (the pointer base cannot validly point to an offset less than zero
661 of the variable).
662 They also cannot alias if the pointer may not point to the decl. */
663 if (max_size2 != -1
664 && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
665 return false;
666 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
667 return false;
669 /* Disambiguations that rely on strict aliasing rules follow. */
670 if (!flag_strict_aliasing)
671 return true;
673 /* If the alias set for a pointer access is zero all bets are off. */
674 if (base1_alias_set == -1)
675 base1_alias_set = get_deref_alias_set (ptr1);
676 if (base1_alias_set == 0)
677 return true;
678 if (base2_alias_set == -1)
679 base2_alias_set = get_alias_set (base2);
681 /* If both references are through the same type, they do not alias
682 if the accesses do not overlap. This does extra disambiguation
683 for mixed/pointer accesses but requires strict aliasing. */
684 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
685 TREE_TYPE (base2)) == 1)
686 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
688 /* The only way to access a variable is through a pointer dereference
689 of the same alias set or a subset of it. */
690 if (base1_alias_set != base2_alias_set
691 && !alias_set_subset_of (base1_alias_set, base2_alias_set))
692 return false;
694 /* Do access-path based disambiguation. */
695 if (ref1 && ref2
696 && handled_component_p (ref1)
697 && handled_component_p (ref2))
698 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
699 offset1, max_size1,
700 ref2, TREE_TYPE (base2),
701 offset2, max_size2);
703 return true;
706 /* Return true if two indirect references based on *PTR1
707 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
708 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
709 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
710 in which case they are computed on-demand. REF1 and REF2
711 if non-NULL are the complete memory reference trees. */
713 static bool
714 indirect_refs_may_alias_p (tree ref1, tree ptr1,
715 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
716 alias_set_type base1_alias_set,
717 tree ref2, tree ptr2,
718 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
719 alias_set_type base2_alias_set)
721 /* If both bases are based on pointers they cannot alias if they may not
722 point to the same memory object or if they point to the same object
723 and the accesses do not overlap. */
724 if (operand_equal_p (ptr1, ptr2, 0))
725 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
726 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
727 return false;
729 /* Disambiguations that rely on strict aliasing rules follow. */
730 if (!flag_strict_aliasing)
731 return true;
733 /* If the alias set for a pointer access is zero all bets are off. */
734 if (base1_alias_set == -1)
735 base1_alias_set = get_deref_alias_set (ptr1);
736 if (base1_alias_set == 0)
737 return true;
738 if (base2_alias_set == -1)
739 base2_alias_set = get_deref_alias_set (ptr2);
740 if (base2_alias_set == 0)
741 return true;
743 /* If both references are through the same type, they do not alias
744 if the accesses do not overlap. This does extra disambiguation
745 for mixed/pointer accesses but requires strict aliasing. */
746 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
747 TREE_TYPE (TREE_TYPE (ptr2))) == 1)
748 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
750 /* Do type-based disambiguation. */
751 if (base1_alias_set != base2_alias_set
752 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
753 return false;
755 /* Do access-path based disambiguation. */
756 if (ref1 && ref2
757 && handled_component_p (ref1)
758 && handled_component_p (ref2))
759 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
760 offset1, max_size1,
761 ref2, TREE_TYPE (TREE_TYPE (ptr2)),
762 offset2, max_size2);
764 return true;
767 /* Return true, if the two memory references REF1 and REF2 may alias. */
769 bool
770 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
772 tree base1, base2;
773 HOST_WIDE_INT offset1 = 0, offset2 = 0;
774 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
775 bool var1_p, var2_p, ind1_p, ind2_p;
776 alias_set_type set;
778 gcc_assert ((!ref1->ref
779 || SSA_VAR_P (ref1->ref)
780 || handled_component_p (ref1->ref)
781 || INDIRECT_REF_P (ref1->ref)
782 || TREE_CODE (ref1->ref) == TARGET_MEM_REF
783 || TREE_CODE (ref1->ref) == CONST_DECL)
784 && (!ref2->ref
785 || SSA_VAR_P (ref2->ref)
786 || handled_component_p (ref2->ref)
787 || INDIRECT_REF_P (ref2->ref)
788 || TREE_CODE (ref2->ref) == TARGET_MEM_REF
789 || TREE_CODE (ref2->ref) == CONST_DECL));
791 /* Decompose the references into their base objects and the access. */
792 base1 = ao_ref_base (ref1);
793 offset1 = ref1->offset;
794 max_size1 = ref1->max_size;
795 base2 = ao_ref_base (ref2);
796 offset2 = ref2->offset;
797 max_size2 = ref2->max_size;
799 /* We can end up with registers or constants as bases for example from
800 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
801 which is seen as a struct copy. */
802 if (TREE_CODE (base1) == SSA_NAME
803 || TREE_CODE (base2) == SSA_NAME
804 || TREE_CODE (base1) == CONST_DECL
805 || TREE_CODE (base2) == CONST_DECL
806 || is_gimple_min_invariant (base1)
807 || is_gimple_min_invariant (base2))
808 return false;
810 /* We can end up refering to code via function decls. As we likely
811 do not properly track code aliases conservatively bail out. */
812 if (TREE_CODE (base1) == FUNCTION_DECL
813 || TREE_CODE (base2) == FUNCTION_DECL)
814 return true;
816 /* Defer to simple offset based disambiguation if we have
817 references based on two decls. Do this before defering to
818 TBAA to handle must-alias cases in conformance with the
819 GCC extension of allowing type-punning through unions. */
820 var1_p = SSA_VAR_P (base1);
821 var2_p = SSA_VAR_P (base2);
822 if (var1_p && var2_p)
823 return decl_refs_may_alias_p (base1, offset1, max_size1,
824 base2, offset2, max_size2);
826 ind1_p = INDIRECT_REF_P (base1);
827 ind2_p = INDIRECT_REF_P (base2);
828 /* Canonicalize the pointer-vs-decl case. */
829 if (ind1_p && var2_p)
831 HOST_WIDE_INT tmp1;
832 tree tmp2;
833 ao_ref *tmp3;
834 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
835 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
836 tmp2 = base1; base1 = base2; base2 = tmp2;
837 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
838 var1_p = true;
839 ind1_p = false;
840 var2_p = false;
841 ind2_p = true;
844 /* If we are about to disambiguate pointer-vs-decl try harder to
845 see must-aliases and give leeway to some invalid cases.
846 This covers a pretty minimal set of cases only and does not
847 when called from the RTL oracle. It handles cases like
849 int i = 1;
850 return *(float *)&i;
852 and also fixes gfortran.dg/lto/pr40725. */
853 if (var1_p && ind2_p
854 && cfun
855 && gimple_in_ssa_p (cfun)
856 && TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME)
858 gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base2, 0));
859 while (is_gimple_assign (def_stmt)
860 && (gimple_assign_rhs_code (def_stmt) == SSA_NAME
861 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))))
863 tree rhs = gimple_assign_rhs1 (def_stmt);
864 HOST_WIDE_INT offset, size, max_size;
866 /* Look through SSA name copies and pointer conversions. */
867 if (TREE_CODE (rhs) == SSA_NAME
868 && POINTER_TYPE_P (TREE_TYPE (rhs)))
870 def_stmt = SSA_NAME_DEF_STMT (rhs);
871 continue;
873 if (TREE_CODE (rhs) != ADDR_EXPR)
874 break;
876 /* If the pointer is defined as an address based on a decl
877 use plain offset disambiguation and ignore TBAA. */
878 rhs = TREE_OPERAND (rhs, 0);
879 rhs = get_ref_base_and_extent (rhs, &offset, &size, &max_size);
880 if (SSA_VAR_P (rhs))
882 base2 = rhs;
883 offset2 += offset;
884 if (size != max_size
885 || max_size == -1)
886 max_size2 = -1;
887 return decl_refs_may_alias_p (base1, offset1, max_size1,
888 base2, offset2, max_size2);
891 /* Do not continue looking through &p->x to limit time
892 complexity. */
893 break;
897 /* First defer to TBAA if possible. */
898 if (tbaa_p
899 && flag_strict_aliasing
900 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
901 ao_ref_alias_set (ref2)))
902 return false;
904 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
905 TBAA disambiguation based on the access type is possible, so bail
906 out only after that check. */
907 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
908 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
909 return true;
911 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
912 set = tbaa_p ? -1 : 0;
913 if (var1_p && ind2_p)
914 return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
915 offset2, max_size2, set,
916 ref1->ref, base1,
917 offset1, max_size1, set);
918 else if (ind1_p && ind2_p)
919 return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
920 offset1, max_size1, set,
921 ref2->ref, TREE_OPERAND (base2, 0),
922 offset2, max_size2, set);
924 gcc_unreachable ();
927 bool
928 refs_may_alias_p (tree ref1, tree ref2)
930 ao_ref r1, r2;
931 bool res;
932 ao_ref_init (&r1, ref1);
933 ao_ref_init (&r2, ref2);
934 res = refs_may_alias_p_1 (&r1, &r2, true);
935 if (res)
936 ++alias_stats.refs_may_alias_p_may_alias;
937 else
938 ++alias_stats.refs_may_alias_p_no_alias;
939 return res;
942 /* Returns true if there is a anti-dependence for the STORE that
943 executes after the LOAD. */
945 bool
946 refs_anti_dependent_p (tree load, tree store)
948 ao_ref r1, r2;
949 ao_ref_init (&r1, load);
950 ao_ref_init (&r2, store);
951 return refs_may_alias_p_1 (&r1, &r2, false);
954 /* Returns true if there is a output dependence for the stores
955 STORE1 and STORE2. */
957 bool
958 refs_output_dependent_p (tree store1, tree store2)
960 ao_ref r1, r2;
961 ao_ref_init (&r1, store1);
962 ao_ref_init (&r2, store2);
963 return refs_may_alias_p_1 (&r1, &r2, false);
966 /* If the call CALL may use the memory reference REF return true,
967 otherwise return false. */
969 static bool
970 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
972 tree base, callee;
973 unsigned i;
974 int flags = gimple_call_flags (call);
976 /* Const functions without a static chain do not implicitly use memory. */
977 if (!gimple_call_chain (call)
978 && (flags & (ECF_CONST|ECF_NOVOPS)))
979 goto process_args;
981 base = ao_ref_base (ref);
982 if (!base)
983 return true;
985 /* If the reference is based on a decl that is not aliased the call
986 cannot possibly use it. */
987 if (DECL_P (base)
988 && !may_be_aliased (base)
989 /* But local statics can be used through recursion. */
990 && !is_global_var (base))
991 goto process_args;
993 callee = gimple_call_fndecl (call);
995 /* Handle those builtin functions explicitly that do not act as
996 escape points. See tree-ssa-structalias.c:find_func_aliases
997 for the list of builtins we might need to handle here. */
998 if (callee != NULL_TREE
999 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1000 switch (DECL_FUNCTION_CODE (callee))
1002 /* All the following functions clobber memory pointed to by
1003 their first argument. */
1004 case BUILT_IN_STRCPY:
1005 case BUILT_IN_STRNCPY:
1006 case BUILT_IN_MEMCPY:
1007 case BUILT_IN_MEMMOVE:
1008 case BUILT_IN_MEMPCPY:
1009 case BUILT_IN_STPCPY:
1010 case BUILT_IN_STPNCPY:
1011 case BUILT_IN_STRCAT:
1012 case BUILT_IN_STRNCAT:
1014 ao_ref dref;
1015 tree size = NULL_TREE;
1016 if (gimple_call_num_args (call) == 3)
1017 size = gimple_call_arg (call, 2);
1018 ao_ref_init_from_ptr_and_size (&dref,
1019 gimple_call_arg (call, 1),
1020 size);
1021 return refs_may_alias_p_1 (&dref, ref, false);
1023 case BUILT_IN_BCOPY:
1025 ao_ref dref;
1026 tree size = gimple_call_arg (call, 2);
1027 ao_ref_init_from_ptr_and_size (&dref,
1028 gimple_call_arg (call, 0),
1029 size);
1030 return refs_may_alias_p_1 (&dref, ref, false);
1032 /* The following builtins do not read from memory. */
1033 case BUILT_IN_FREE:
1034 case BUILT_IN_MALLOC:
1035 case BUILT_IN_CALLOC:
1036 case BUILT_IN_MEMSET:
1037 case BUILT_IN_FREXP:
1038 case BUILT_IN_FREXPF:
1039 case BUILT_IN_FREXPL:
1040 case BUILT_IN_GAMMA_R:
1041 case BUILT_IN_GAMMAF_R:
1042 case BUILT_IN_GAMMAL_R:
1043 case BUILT_IN_LGAMMA_R:
1044 case BUILT_IN_LGAMMAF_R:
1045 case BUILT_IN_LGAMMAL_R:
1046 case BUILT_IN_MODF:
1047 case BUILT_IN_MODFF:
1048 case BUILT_IN_MODFL:
1049 case BUILT_IN_REMQUO:
1050 case BUILT_IN_REMQUOF:
1051 case BUILT_IN_REMQUOL:
1052 case BUILT_IN_SINCOS:
1053 case BUILT_IN_SINCOSF:
1054 case BUILT_IN_SINCOSL:
1055 return false;
1057 default:
1058 /* Fallthru to general call handling. */;
1061 /* Check if base is a global static variable that is not read
1062 by the function. */
1063 if (TREE_CODE (base) == VAR_DECL
1064 && TREE_STATIC (base)
1065 && !TREE_PUBLIC (base))
1067 bitmap not_read;
1069 if (callee != NULL_TREE
1070 && (not_read
1071 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1072 && bitmap_bit_p (not_read, DECL_UID (base)))
1073 goto process_args;
1076 /* Check if the base variable is call-used. */
1077 if (DECL_P (base))
1079 if (pt_solution_includes (gimple_call_use_set (call), 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_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1090 return true;
1092 else
1093 return true;
1095 /* Inspect call arguments for passed-by-value aliases. */
1096 process_args:
1097 for (i = 0; i < gimple_call_num_args (call); ++i)
1099 tree op = gimple_call_arg (call, i);
1100 int flags = gimple_call_arg_flags (call, i);
1102 if (flags & EAF_UNUSED)
1103 continue;
1105 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1106 op = TREE_OPERAND (op, 0);
1108 if (TREE_CODE (op) != SSA_NAME
1109 && !is_gimple_min_invariant (op))
1111 ao_ref r;
1112 ao_ref_init (&r, op);
1113 if (refs_may_alias_p_1 (&r, ref, true))
1114 return true;
1118 return false;
1121 static bool
1122 ref_maybe_used_by_call_p (gimple call, tree ref)
1124 ao_ref r;
1125 bool res;
1126 ao_ref_init (&r, ref);
1127 res = ref_maybe_used_by_call_p_1 (call, &r);
1128 if (res)
1129 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1130 else
1131 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1132 return res;
1136 /* If the statement STMT may use the memory reference REF return
1137 true, otherwise return false. */
1139 bool
1140 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1142 if (is_gimple_assign (stmt))
1144 tree rhs;
1146 /* All memory assign statements are single. */
1147 if (!gimple_assign_single_p (stmt))
1148 return false;
1150 rhs = gimple_assign_rhs1 (stmt);
1151 if (is_gimple_reg (rhs)
1152 || is_gimple_min_invariant (rhs)
1153 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1154 return false;
1156 return refs_may_alias_p (rhs, ref);
1158 else if (is_gimple_call (stmt))
1159 return ref_maybe_used_by_call_p (stmt, ref);
1161 return true;
1164 /* If the call in statement CALL may clobber the memory reference REF
1165 return true, otherwise return false. */
1167 static bool
1168 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1170 tree base;
1171 tree callee;
1173 /* If the call is pure or const it cannot clobber anything. */
1174 if (gimple_call_flags (call)
1175 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1176 return false;
1178 base = ao_ref_base (ref);
1179 if (!base)
1180 return true;
1182 if (TREE_CODE (base) == SSA_NAME
1183 || CONSTANT_CLASS_P (base))
1184 return false;
1186 /* If the reference is based on a decl that is not aliased the call
1187 cannot possibly clobber it. */
1188 if (DECL_P (base)
1189 && !may_be_aliased (base)
1190 /* But local non-readonly statics can be modified through recursion
1191 or the call may implement a threading barrier which we must
1192 treat as may-def. */
1193 && (TREE_READONLY (base)
1194 || !is_global_var (base)))
1195 return false;
1197 callee = gimple_call_fndecl (call);
1199 /* Handle those builtin functions explicitly that do not act as
1200 escape points. See tree-ssa-structalias.c:find_func_aliases
1201 for the list of builtins we might need to handle here. */
1202 if (callee != NULL_TREE
1203 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1204 switch (DECL_FUNCTION_CODE (callee))
1206 /* All the following functions clobber memory pointed to by
1207 their first argument. */
1208 case BUILT_IN_STRCPY:
1209 case BUILT_IN_STRNCPY:
1210 case BUILT_IN_MEMCPY:
1211 case BUILT_IN_MEMMOVE:
1212 case BUILT_IN_MEMPCPY:
1213 case BUILT_IN_STPCPY:
1214 case BUILT_IN_STPNCPY:
1215 case BUILT_IN_STRCAT:
1216 case BUILT_IN_STRNCAT:
1217 case BUILT_IN_MEMSET:
1219 ao_ref dref;
1220 tree size = NULL_TREE;
1221 if (gimple_call_num_args (call) == 3)
1222 size = gimple_call_arg (call, 2);
1223 ao_ref_init_from_ptr_and_size (&dref,
1224 gimple_call_arg (call, 0),
1225 size);
1226 return refs_may_alias_p_1 (&dref, ref, false);
1228 case BUILT_IN_BCOPY:
1230 ao_ref dref;
1231 tree size = gimple_call_arg (call, 2);
1232 ao_ref_init_from_ptr_and_size (&dref,
1233 gimple_call_arg (call, 1),
1234 size);
1235 return refs_may_alias_p_1 (&dref, ref, false);
1237 /* Allocating memory does not have any side-effects apart from
1238 being the definition point for the pointer. */
1239 case BUILT_IN_MALLOC:
1240 case BUILT_IN_CALLOC:
1241 /* Unix98 specifies that errno is set on allocation failure.
1242 Until we properly can track the errno location assume it
1243 is not a local decl but external or anonymous storage in
1244 a different translation unit. Also assume it is of
1245 type int as required by the standard. */
1246 if (flag_errno_math
1247 && TREE_TYPE (base) == integer_type_node)
1249 struct ptr_info_def *pi;
1250 if (DECL_P (base)
1251 && !TREE_STATIC (base))
1252 return true;
1253 else if (INDIRECT_REF_P (base)
1254 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1255 && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1256 return pi->pt.anything || pi->pt.nonlocal;
1258 return false;
1259 /* Freeing memory kills the pointed-to memory. More importantly
1260 the call has to serve as a barrier for moving loads and stores
1261 across it. */
1262 case BUILT_IN_FREE:
1264 tree ptr = gimple_call_arg (call, 0);
1265 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1267 case BUILT_IN_GAMMA_R:
1268 case BUILT_IN_GAMMAF_R:
1269 case BUILT_IN_GAMMAL_R:
1270 case BUILT_IN_LGAMMA_R:
1271 case BUILT_IN_LGAMMAF_R:
1272 case BUILT_IN_LGAMMAL_R:
1274 tree out = gimple_call_arg (call, 1);
1275 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1276 return true;
1277 if (flag_errno_math)
1278 break;
1279 return false;
1281 case BUILT_IN_FREXP:
1282 case BUILT_IN_FREXPF:
1283 case BUILT_IN_FREXPL:
1284 case BUILT_IN_MODF:
1285 case BUILT_IN_MODFF:
1286 case BUILT_IN_MODFL:
1288 tree out = gimple_call_arg (call, 1);
1289 return ptr_deref_may_alias_ref_p_1 (out, ref);
1291 case BUILT_IN_REMQUO:
1292 case BUILT_IN_REMQUOF:
1293 case BUILT_IN_REMQUOL:
1295 tree out = gimple_call_arg (call, 2);
1296 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1297 return true;
1298 if (flag_errno_math)
1299 break;
1300 return false;
1302 case BUILT_IN_SINCOS:
1303 case BUILT_IN_SINCOSF:
1304 case BUILT_IN_SINCOSL:
1306 tree sin = gimple_call_arg (call, 1);
1307 tree cos = gimple_call_arg (call, 2);
1308 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1309 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1311 default:
1312 /* Fallthru to general call handling. */;
1315 /* Check if base is a global static variable that is not written
1316 by the function. */
1317 if (callee != NULL_TREE
1318 && TREE_CODE (base) == VAR_DECL
1319 && TREE_STATIC (base)
1320 && !TREE_PUBLIC (base))
1322 bitmap not_written;
1324 if ((not_written
1325 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1326 && bitmap_bit_p (not_written, DECL_UID (base)))
1327 return false;
1330 /* Check if the base variable is call-clobbered. */
1331 if (DECL_P (base))
1332 return pt_solution_includes (gimple_call_clobber_set (call), base);
1333 else if (INDIRECT_REF_P (base)
1334 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1336 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1337 if (!pi)
1338 return true;
1340 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1343 return true;
1346 /* If the call in statement CALL may clobber the memory reference REF
1347 return true, otherwise return false. */
1349 bool
1350 call_may_clobber_ref_p (gimple call, tree ref)
1352 bool res;
1353 ao_ref r;
1354 ao_ref_init (&r, ref);
1355 res = call_may_clobber_ref_p_1 (call, &r);
1356 if (res)
1357 ++alias_stats.call_may_clobber_ref_p_may_alias;
1358 else
1359 ++alias_stats.call_may_clobber_ref_p_no_alias;
1360 return res;
1364 /* If the statement STMT may clobber the memory reference REF return true,
1365 otherwise return false. */
1367 bool
1368 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1370 if (is_gimple_call (stmt))
1372 tree lhs = gimple_call_lhs (stmt);
1373 if (lhs
1374 && !is_gimple_reg (lhs))
1376 ao_ref r;
1377 ao_ref_init (&r, lhs);
1378 if (refs_may_alias_p_1 (ref, &r, true))
1379 return true;
1382 return call_may_clobber_ref_p_1 (stmt, ref);
1384 else if (is_gimple_assign (stmt))
1386 ao_ref r;
1387 ao_ref_init (&r, gimple_assign_lhs (stmt));
1388 return refs_may_alias_p_1 (ref, &r, true);
1390 else if (gimple_code (stmt) == GIMPLE_ASM)
1391 return true;
1393 return false;
1396 bool
1397 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1399 ao_ref r;
1400 ao_ref_init (&r, ref);
1401 return stmt_may_clobber_ref_p_1 (stmt, &r);
1405 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1406 TARGET or a statement clobbering the memory reference REF in which
1407 case false is returned. The walk starts with VUSE, one argument of PHI. */
1409 static bool
1410 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1411 tree vuse, bitmap *visited)
1413 if (!*visited)
1414 *visited = BITMAP_ALLOC (NULL);
1416 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1418 /* Walk until we hit the target. */
1419 while (vuse != target)
1421 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1422 /* Recurse for PHI nodes. */
1423 if (gimple_code (def_stmt) == GIMPLE_PHI)
1425 /* An already visited PHI node ends the walk successfully. */
1426 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1427 return true;
1428 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1429 if (!vuse)
1430 return false;
1431 continue;
1433 /* A clobbering statement or the end of the IL ends it failing. */
1434 else if (gimple_nop_p (def_stmt)
1435 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1436 return false;
1437 vuse = gimple_vuse (def_stmt);
1439 return true;
1442 /* Starting from a PHI node for the virtual operand of the memory reference
1443 REF find a continuation virtual operand that allows to continue walking
1444 statements dominating PHI skipping only statements that cannot possibly
1445 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1446 be found. */
1448 tree
1449 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1451 unsigned nargs = gimple_phi_num_args (phi);
1453 /* Through a single-argument PHI we can simply look through. */
1454 if (nargs == 1)
1455 return PHI_ARG_DEF (phi, 0);
1457 /* For two arguments try to skip non-aliasing code until we hit
1458 the phi argument definition that dominates the other one. */
1459 if (nargs == 2)
1461 tree arg0 = PHI_ARG_DEF (phi, 0);
1462 tree arg1 = PHI_ARG_DEF (phi, 1);
1463 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1464 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1465 tree common_vuse;
1467 if (arg0 == arg1)
1468 return arg0;
1469 else if (gimple_nop_p (def0)
1470 || (!gimple_nop_p (def1)
1471 && dominated_by_p (CDI_DOMINATORS,
1472 gimple_bb (def1), gimple_bb (def0))))
1474 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1475 return arg0;
1477 else if (gimple_nop_p (def1)
1478 || dominated_by_p (CDI_DOMINATORS,
1479 gimple_bb (def0), gimple_bb (def1)))
1481 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1482 return arg1;
1484 /* Special case of a diamond:
1485 MEM_1 = ...
1486 goto (cond) ? L1 : L2
1487 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1488 goto L3
1489 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1490 L3: MEM_4 = PHI<MEM_2, MEM_3>
1491 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1492 dominate each other, but still we can easily skip this PHI node
1493 if we recognize that the vuse MEM operand is the same for both,
1494 and that we can skip both statements (they don't clobber us).
1495 This is still linear. Don't use maybe_skip_until, that might
1496 potentially be slow. */
1497 else if ((common_vuse = gimple_vuse (def0))
1498 && common_vuse == gimple_vuse (def1))
1500 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1501 && !stmt_may_clobber_ref_p_1 (def1, ref))
1502 return common_vuse;
1506 return NULL_TREE;
1509 /* Based on the memory reference REF and its virtual use VUSE call
1510 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1511 itself. That is, for each virtual use for which its defining statement
1512 does not clobber REF.
1514 WALKER is called with REF, the current virtual use and DATA. If
1515 WALKER returns non-NULL the walk stops and its result is returned.
1516 At the end of a non-successful walk NULL is returned.
1518 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1519 use which definition is a statement that may clobber REF and DATA.
1520 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1521 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1522 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1523 to adjust REF and *DATA to make that valid.
1525 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1527 void *
1528 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1529 void *(*walker)(ao_ref *, tree, void *),
1530 void *(*translate)(ao_ref *, tree, void *), void *data)
1532 bitmap visited = NULL;
1533 void *res;
1535 timevar_push (TV_ALIAS_STMT_WALK);
1539 gimple def_stmt;
1541 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1542 res = (*walker) (ref, vuse, data);
1543 if (res)
1544 break;
1546 def_stmt = SSA_NAME_DEF_STMT (vuse);
1547 if (gimple_nop_p (def_stmt))
1548 break;
1549 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1550 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1551 else
1553 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1555 if (!translate)
1556 break;
1557 res = (*translate) (ref, vuse, data);
1558 /* Failed lookup and translation. */
1559 if (res == (void *)-1)
1561 res = NULL;
1562 break;
1564 /* Lookup succeeded. */
1565 else if (res != NULL)
1566 break;
1567 /* Translation succeeded, continue walking. */
1569 vuse = gimple_vuse (def_stmt);
1572 while (vuse);
1574 if (visited)
1575 BITMAP_FREE (visited);
1577 timevar_pop (TV_ALIAS_STMT_WALK);
1579 return res;
1583 /* Based on the memory reference REF call WALKER for each vdef which
1584 defining statement may clobber REF, starting with VDEF. If REF
1585 is NULL_TREE, each defining statement is visited.
1587 WALKER is called with REF, the current vdef and DATA. If WALKER
1588 returns true the walk is stopped, otherwise it continues.
1590 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1591 PHI argument (but only one walk continues on merge points), the
1592 return value is true if any of the walks was successful.
1594 The function returns the number of statements walked. */
1596 static unsigned int
1597 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1598 bool (*walker)(ao_ref *, tree, void *), void *data,
1599 bitmap *visited, unsigned int cnt)
1603 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1605 if (*visited
1606 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1607 return cnt;
1609 if (gimple_nop_p (def_stmt))
1610 return cnt;
1611 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1613 unsigned i;
1614 if (!*visited)
1615 *visited = BITMAP_ALLOC (NULL);
1616 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1617 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1618 walker, data, visited, 0);
1619 return cnt;
1622 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1623 cnt++;
1624 if ((!ref
1625 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1626 && (*walker) (ref, vdef, data))
1627 return cnt;
1629 vdef = gimple_vuse (def_stmt);
1631 while (1);
1634 unsigned int
1635 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1636 bool (*walker)(ao_ref *, tree, void *), void *data,
1637 bitmap *visited)
1639 bitmap local_visited = NULL;
1640 unsigned int ret;
1642 timevar_push (TV_ALIAS_STMT_WALK);
1644 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1645 visited ? visited : &local_visited, 0);
1646 if (local_visited)
1647 BITMAP_FREE (local_visited);
1649 timevar_pop (TV_ALIAS_STMT_WALK);
1651 return ret;