2011-11-16 Matthew Gretton-Dann <matthew.gretton-dann@arm.com>
[official-gcc.git] / gcc / tree-ssa-alias.c
blobcd22209357907bf9337815c683d9f5588cb86eda
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 "tm_p.h"
28 #include "target.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "tree-pretty-print.h"
36 #include "tree-dump.h"
37 #include "gimple.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "tree-pass.h"
41 #include "convert.h"
42 #include "params.h"
43 #include "vec.h"
44 #include "bitmap.h"
45 #include "vecprim.h"
46 #include "pointer-set.h"
47 #include "alloc-pool.h"
48 #include "tree-ssa-alias.h"
50 /* Broad overview of how alias analysis on gimple works:
52 Statements clobbering or using memory are linked through the
53 virtual operand factored use-def chain. The virtual operand
54 is unique per function, its symbol is accessible via gimple_vop (cfun).
55 Virtual operands are used for efficiently walking memory statements
56 in the gimple IL and are useful for things like value-numbering as
57 a generation count for memory references.
59 SSA_NAME pointers may have associated points-to information
60 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
61 points-to information is (re-)computed by the TODO_rebuild_alias
62 pass manager todo. Points-to information is also used for more
63 precise tracking of call-clobbered and call-used variables and
64 related disambiguations.
66 This file contains functions for disambiguating memory references,
67 the so called alias-oracle and tools for walking of the gimple IL.
69 The main alias-oracle entry-points are
71 bool stmt_may_clobber_ref_p (gimple, tree)
73 This function queries if a statement may invalidate (parts of)
74 the memory designated by the reference tree argument.
76 bool ref_maybe_used_by_stmt_p (gimple, tree)
78 This function queries if a statement may need (parts of) the
79 memory designated by the reference tree argument.
81 There are variants of these functions that only handle the call
82 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
83 Note that these do not disambiguate against a possible call lhs.
85 bool refs_may_alias_p (tree, tree)
87 This function tries to disambiguate two reference trees.
89 bool ptr_deref_may_alias_global_p (tree)
91 This function queries if dereferencing a pointer variable may
92 alias global memory.
94 More low-level disambiguators are available and documented in
95 this file. Low-level disambiguators dealing with points-to
96 information are in tree-ssa-structalias.c. */
99 /* Query statistics for the different low-level disambiguators.
100 A high-level query may trigger multiple of them. */
102 static struct {
103 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
104 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
105 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
106 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
107 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
108 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
109 } alias_stats;
111 void
112 dump_alias_stats (FILE *s)
114 fprintf (s, "\nAlias oracle query stats:\n");
115 fprintf (s, " refs_may_alias_p: "
116 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
117 HOST_WIDE_INT_PRINT_DEC" queries\n",
118 alias_stats.refs_may_alias_p_no_alias,
119 alias_stats.refs_may_alias_p_no_alias
120 + alias_stats.refs_may_alias_p_may_alias);
121 fprintf (s, " ref_maybe_used_by_call_p: "
122 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
123 HOST_WIDE_INT_PRINT_DEC" queries\n",
124 alias_stats.ref_maybe_used_by_call_p_no_alias,
125 alias_stats.refs_may_alias_p_no_alias
126 + alias_stats.ref_maybe_used_by_call_p_may_alias);
127 fprintf (s, " call_may_clobber_ref_p: "
128 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
129 HOST_WIDE_INT_PRINT_DEC" queries\n",
130 alias_stats.call_may_clobber_ref_p_no_alias,
131 alias_stats.call_may_clobber_ref_p_no_alias
132 + alias_stats.call_may_clobber_ref_p_may_alias);
136 /* Return true, if dereferencing PTR may alias with a global variable. */
138 bool
139 ptr_deref_may_alias_global_p (tree ptr)
141 struct ptr_info_def *pi;
143 /* If we end up with a pointer constant here that may point
144 to global memory. */
145 if (TREE_CODE (ptr) != SSA_NAME)
146 return true;
148 pi = SSA_NAME_PTR_INFO (ptr);
150 /* If we do not have points-to information for this variable,
151 we have to punt. */
152 if (!pi)
153 return true;
155 /* ??? This does not use TBAA to prune globals ptr may not access. */
156 return pt_solution_includes_global (&pi->pt);
159 /* Return true if dereferencing PTR may alias DECL.
160 The caller is responsible for applying TBAA to see if PTR
161 may access DECL at all. */
163 static bool
164 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
166 struct ptr_info_def *pi;
168 /* Conversions are irrelevant for points-to information and
169 data-dependence analysis can feed us those. */
170 STRIP_NOPS (ptr);
172 /* Anything we do not explicilty handle aliases. */
173 if ((TREE_CODE (ptr) != SSA_NAME
174 && TREE_CODE (ptr) != ADDR_EXPR
175 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
176 || !POINTER_TYPE_P (TREE_TYPE (ptr))
177 || (TREE_CODE (decl) != VAR_DECL
178 && TREE_CODE (decl) != PARM_DECL
179 && TREE_CODE (decl) != RESULT_DECL))
180 return true;
182 /* Disregard pointer offsetting. */
183 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
187 ptr = TREE_OPERAND (ptr, 0);
189 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
190 return ptr_deref_may_alias_decl_p (ptr, decl);
193 /* ADDR_EXPR pointers either just offset another pointer or directly
194 specify the pointed-to set. */
195 if (TREE_CODE (ptr) == ADDR_EXPR)
197 tree base = get_base_address (TREE_OPERAND (ptr, 0));
198 if (base
199 && (TREE_CODE (base) == MEM_REF
200 || TREE_CODE (base) == TARGET_MEM_REF))
201 ptr = TREE_OPERAND (base, 0);
202 else if (base
203 && DECL_P (base))
204 return base == decl;
205 else if (base
206 && CONSTANT_CLASS_P (base))
207 return false;
208 else
209 return true;
212 /* Non-aliased variables can not be pointed to. */
213 if (!may_be_aliased (decl))
214 return false;
216 /* If we do not have useful points-to information for this pointer
217 we cannot disambiguate anything else. */
218 pi = SSA_NAME_PTR_INFO (ptr);
219 if (!pi)
220 return true;
222 return pt_solution_includes (&pi->pt, decl);
225 /* Return true if dereferenced PTR1 and PTR2 may alias.
226 The caller is responsible for applying TBAA to see if accesses
227 through PTR1 and PTR2 may conflict at all. */
229 bool
230 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
232 struct ptr_info_def *pi1, *pi2;
234 /* Conversions are irrelevant for points-to information and
235 data-dependence analysis can feed us those. */
236 STRIP_NOPS (ptr1);
237 STRIP_NOPS (ptr2);
239 /* Anything we do not explicilty handle aliases. */
240 if ((TREE_CODE (ptr1) != SSA_NAME
241 && TREE_CODE (ptr1) != ADDR_EXPR
242 && TREE_CODE (ptr1) != POINTER_PLUS_EXPR)
243 || (TREE_CODE (ptr2) != SSA_NAME
244 && TREE_CODE (ptr2) != ADDR_EXPR
245 && TREE_CODE (ptr2) != POINTER_PLUS_EXPR)
246 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
247 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
248 return true;
250 /* Disregard pointer offsetting. */
251 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
255 ptr1 = TREE_OPERAND (ptr1, 0);
257 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
258 return ptr_derefs_may_alias_p (ptr1, ptr2);
260 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
264 ptr2 = TREE_OPERAND (ptr2, 0);
266 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
267 return ptr_derefs_may_alias_p (ptr1, ptr2);
270 /* ADDR_EXPR pointers either just offset another pointer or directly
271 specify the pointed-to set. */
272 if (TREE_CODE (ptr1) == ADDR_EXPR)
274 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
275 if (base
276 && (TREE_CODE (base) == MEM_REF
277 || TREE_CODE (base) == TARGET_MEM_REF))
278 ptr1 = TREE_OPERAND (base, 0);
279 else if (base
280 && DECL_P (base))
281 return ptr_deref_may_alias_decl_p (ptr2, base);
282 else
283 return true;
285 if (TREE_CODE (ptr2) == ADDR_EXPR)
287 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
288 if (base
289 && (TREE_CODE (base) == MEM_REF
290 || TREE_CODE (base) == TARGET_MEM_REF))
291 ptr2 = TREE_OPERAND (base, 0);
292 else if (base
293 && DECL_P (base))
294 return ptr_deref_may_alias_decl_p (ptr1, base);
295 else
296 return true;
299 /* We may end up with two empty points-to solutions for two same pointers.
300 In this case we still want to say both pointers alias, so shortcut
301 that here. */
302 if (ptr1 == ptr2)
303 return true;
305 /* If we do not have useful points-to information for either pointer
306 we cannot disambiguate anything else. */
307 pi1 = SSA_NAME_PTR_INFO (ptr1);
308 pi2 = SSA_NAME_PTR_INFO (ptr2);
309 if (!pi1 || !pi2)
310 return true;
312 /* ??? This does not use TBAA to prune decls from the intersection
313 that not both pointers may access. */
314 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
317 /* Return true if dereferencing PTR may alias *REF.
318 The caller is responsible for applying TBAA to see if PTR
319 may access *REF at all. */
321 static bool
322 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
324 tree base = ao_ref_base (ref);
326 if (TREE_CODE (base) == MEM_REF
327 || TREE_CODE (base) == TARGET_MEM_REF)
328 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
329 else if (DECL_P (base))
330 return ptr_deref_may_alias_decl_p (ptr, base);
332 return true;
336 /* Dump alias information on FILE. */
338 void
339 dump_alias_info (FILE *file)
341 size_t i;
342 const char *funcname
343 = lang_hooks.decl_printable_name (current_function_decl, 2);
344 referenced_var_iterator rvi;
345 tree var;
347 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
349 fprintf (file, "Aliased symbols\n\n");
351 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
353 if (may_be_aliased (var))
354 dump_variable (file, var);
357 fprintf (file, "\nCall clobber information\n");
359 fprintf (file, "\nESCAPED");
360 dump_points_to_solution (file, &cfun->gimple_df->escaped);
362 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
364 for (i = 1; i < num_ssa_names; i++)
366 tree ptr = ssa_name (i);
367 struct ptr_info_def *pi;
369 if (ptr == NULL_TREE
370 || SSA_NAME_IN_FREE_LIST (ptr))
371 continue;
373 pi = SSA_NAME_PTR_INFO (ptr);
374 if (pi)
375 dump_points_to_info_for (file, ptr);
378 fprintf (file, "\n");
382 /* Dump alias information on stderr. */
384 DEBUG_FUNCTION void
385 debug_alias_info (void)
387 dump_alias_info (stderr);
391 /* Dump the points-to set *PT into FILE. */
393 void
394 dump_points_to_solution (FILE *file, struct pt_solution *pt)
396 if (pt->anything)
397 fprintf (file, ", points-to anything");
399 if (pt->nonlocal)
400 fprintf (file, ", points-to non-local");
402 if (pt->escaped)
403 fprintf (file, ", points-to escaped");
405 if (pt->ipa_escaped)
406 fprintf (file, ", points-to unit escaped");
408 if (pt->null)
409 fprintf (file, ", points-to NULL");
411 if (pt->vars)
413 fprintf (file, ", points-to vars: ");
414 dump_decl_set (file, pt->vars);
415 if (pt->vars_contains_global)
416 fprintf (file, " (includes global vars)");
420 /* Dump points-to information for SSA_NAME PTR into FILE. */
422 void
423 dump_points_to_info_for (FILE *file, tree ptr)
425 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
427 print_generic_expr (file, ptr, dump_flags);
429 if (pi)
430 dump_points_to_solution (file, &pi->pt);
431 else
432 fprintf (file, ", points-to anything");
434 fprintf (file, "\n");
438 /* Dump points-to information for VAR into stderr. */
440 DEBUG_FUNCTION void
441 debug_points_to_info_for (tree var)
443 dump_points_to_info_for (stderr, var);
447 /* Initializes the alias-oracle reference representation *R from REF. */
449 void
450 ao_ref_init (ao_ref *r, tree ref)
452 r->ref = ref;
453 r->base = NULL_TREE;
454 r->offset = 0;
455 r->size = -1;
456 r->max_size = -1;
457 r->ref_alias_set = -1;
458 r->base_alias_set = -1;
461 /* Returns the base object of the memory reference *REF. */
463 tree
464 ao_ref_base (ao_ref *ref)
466 if (ref->base)
467 return ref->base;
468 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
469 &ref->max_size);
470 return ref->base;
473 /* Returns the base object alias set of the memory reference *REF. */
475 static alias_set_type
476 ao_ref_base_alias_set (ao_ref *ref)
478 tree base_ref;
479 if (ref->base_alias_set != -1)
480 return ref->base_alias_set;
481 if (!ref->ref)
482 return 0;
483 base_ref = ref->ref;
484 while (handled_component_p (base_ref))
485 base_ref = TREE_OPERAND (base_ref, 0);
486 ref->base_alias_set = get_alias_set (base_ref);
487 return ref->base_alias_set;
490 /* Returns the reference alias set of the memory reference *REF. */
492 alias_set_type
493 ao_ref_alias_set (ao_ref *ref)
495 if (ref->ref_alias_set != -1)
496 return ref->ref_alias_set;
497 ref->ref_alias_set = get_alias_set (ref->ref);
498 return ref->ref_alias_set;
501 /* Init an alias-oracle reference representation from a gimple pointer
502 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
503 size is assumed to be unknown. The access is assumed to be only
504 to or after of the pointer target, not before it. */
506 void
507 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
509 HOST_WIDE_INT t1, t2;
510 ref->ref = NULL_TREE;
511 if (TREE_CODE (ptr) == ADDR_EXPR)
512 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
513 &ref->offset, &t1, &t2);
514 else
516 ref->base = build2 (MEM_REF, char_type_node,
517 ptr, null_pointer_node);
518 ref->offset = 0;
520 if (size
521 && host_integerp (size, 0)
522 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
523 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
524 else
525 ref->max_size = ref->size = -1;
526 ref->ref_alias_set = 0;
527 ref->base_alias_set = 0;
530 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
531 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
532 decide. */
534 static inline int
535 same_type_for_tbaa (tree type1, tree type2)
537 type1 = TYPE_MAIN_VARIANT (type1);
538 type2 = TYPE_MAIN_VARIANT (type2);
540 /* If we would have to do structural comparison bail out. */
541 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
542 || TYPE_STRUCTURAL_EQUALITY_P (type2))
543 return -1;
545 /* Compare the canonical types. */
546 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
547 return 1;
549 /* ??? Array types are not properly unified in all cases as we have
550 spurious changes in the index types for example. Removing this
551 causes all sorts of problems with the Fortran frontend. */
552 if (TREE_CODE (type1) == ARRAY_TYPE
553 && TREE_CODE (type2) == ARRAY_TYPE)
554 return -1;
556 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
557 object of one of its constrained subtypes, e.g. when a function with an
558 unconstrained parameter passed by reference is called on an object and
559 inlined. But, even in the case of a fixed size, type and subtypes are
560 not equivalent enough as to share the same TYPE_CANONICAL, since this
561 would mean that conversions between them are useless, whereas they are
562 not (e.g. type and subtypes can have different modes). So, in the end,
563 they are only guaranteed to have the same alias set. */
564 if (get_alias_set (type1) == get_alias_set (type2))
565 return -1;
567 /* The types are known to be not equal. */
568 return 0;
571 /* Determine if the two component references REF1 and REF2 which are
572 based on access types TYPE1 and TYPE2 and of which at least one is based
573 on an indirect reference may alias. REF2 is the only one that can
574 be a decl in which case REF2_IS_DECL is true.
575 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
576 are the respective alias sets. */
578 static bool
579 aliasing_component_refs_p (tree ref1,
580 alias_set_type ref1_alias_set,
581 alias_set_type base1_alias_set,
582 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
583 tree ref2,
584 alias_set_type ref2_alias_set,
585 alias_set_type base2_alias_set,
586 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
587 bool ref2_is_decl)
589 /* If one reference is a component references through pointers try to find a
590 common base and apply offset based disambiguation. This handles
591 for example
592 struct A { int i; int j; } *q;
593 struct B { struct A a; int k; } *p;
594 disambiguating q->i and p->a.j. */
595 tree base1, base2;
596 tree type1, type2;
597 tree *refp;
598 int same_p;
600 /* Choose bases and base types to search for. */
601 base1 = ref1;
602 while (handled_component_p (base1))
603 base1 = TREE_OPERAND (base1, 0);
604 type1 = TREE_TYPE (base1);
605 base2 = ref2;
606 while (handled_component_p (base2))
607 base2 = TREE_OPERAND (base2, 0);
608 type2 = TREE_TYPE (base2);
610 /* Now search for the type1 in the access path of ref2. This
611 would be a common base for doing offset based disambiguation on. */
612 refp = &ref2;
613 while (handled_component_p (*refp)
614 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
615 refp = &TREE_OPERAND (*refp, 0);
616 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
617 /* If we couldn't compare types we have to bail out. */
618 if (same_p == -1)
619 return true;
620 else if (same_p == 1)
622 HOST_WIDE_INT offadj, sztmp, msztmp;
623 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
624 offset2 -= offadj;
625 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
626 offset1 -= offadj;
627 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
629 /* If we didn't find a common base, try the other way around. */
630 refp = &ref1;
631 while (handled_component_p (*refp)
632 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
633 refp = &TREE_OPERAND (*refp, 0);
634 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
635 /* If we couldn't compare types we have to bail out. */
636 if (same_p == -1)
637 return true;
638 else if (same_p == 1)
640 HOST_WIDE_INT offadj, sztmp, msztmp;
641 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
642 offset1 -= offadj;
643 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
644 offset2 -= offadj;
645 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
648 /* If we have two type access paths B1.path1 and B2.path2 they may
649 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
650 But we can still have a path that goes B1.path1...B2.path2 with
651 a part that we do not see. So we can only disambiguate now
652 if there is no B2 in the tail of path1 and no B1 on the
653 tail of path2. */
654 if (base1_alias_set == ref2_alias_set
655 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
656 return true;
657 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
658 if (!ref2_is_decl)
659 return (base2_alias_set == ref1_alias_set
660 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
661 return false;
664 /* Return true if two memory references based on the variables BASE1
665 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
666 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
668 static bool
669 decl_refs_may_alias_p (tree base1,
670 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
671 tree base2,
672 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
674 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
676 /* If both references are based on different variables, they cannot alias. */
677 if (base1 != base2)
678 return false;
680 /* If both references are based on the same variable, they cannot alias if
681 the accesses do not overlap. */
682 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
685 /* Return true if an indirect reference based on *PTR1 constrained
686 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
687 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
688 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
689 in which case they are computed on-demand. REF1 and REF2
690 if non-NULL are the complete memory reference trees. */
692 static bool
693 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
694 HOST_WIDE_INT offset1,
695 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
696 alias_set_type ref1_alias_set,
697 alias_set_type base1_alias_set,
698 tree ref2 ATTRIBUTE_UNUSED, tree base2,
699 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
700 alias_set_type ref2_alias_set,
701 alias_set_type base2_alias_set, bool tbaa_p)
703 tree ptr1;
704 tree ptrtype1, dbase2;
705 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
706 HOST_WIDE_INT doffset1, doffset2;
707 double_int moff;
709 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
710 || TREE_CODE (base1) == TARGET_MEM_REF)
711 && DECL_P (base2));
713 ptr1 = TREE_OPERAND (base1, 0);
715 /* The offset embedded in MEM_REFs can be negative. Bias them
716 so that the resulting offset adjustment is positive. */
717 moff = mem_ref_offset (base1);
718 moff = double_int_lshift (moff,
719 BITS_PER_UNIT == 8
720 ? 3 : exact_log2 (BITS_PER_UNIT),
721 HOST_BITS_PER_DOUBLE_INT, true);
722 if (double_int_negative_p (moff))
723 offset2p += double_int_neg (moff).low;
724 else
725 offset1p += moff.low;
727 /* If only one reference is based on a variable, they cannot alias if
728 the pointer access is beyond the extent of the variable access.
729 (the pointer base cannot validly point to an offset less than zero
730 of the variable).
731 ??? IVOPTs creates bases that do not honor this restriction,
732 so do not apply this optimization for TARGET_MEM_REFs. */
733 if (TREE_CODE (base1) != TARGET_MEM_REF
734 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
735 return false;
736 /* They also cannot alias if the pointer may not point to the decl. */
737 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
738 return false;
740 /* Disambiguations that rely on strict aliasing rules follow. */
741 if (!flag_strict_aliasing || !tbaa_p)
742 return true;
744 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
746 /* If the alias set for a pointer access is zero all bets are off. */
747 if (base1_alias_set == -1)
748 base1_alias_set = get_deref_alias_set (ptrtype1);
749 if (base1_alias_set == 0)
750 return true;
751 if (base2_alias_set == -1)
752 base2_alias_set = get_alias_set (base2);
754 /* When we are trying to disambiguate an access with a pointer dereference
755 as base versus one with a decl as base we can use both the size
756 of the decl and its dynamic type for extra disambiguation.
757 ??? We do not know anything about the dynamic type of the decl
758 other than that its alias-set contains base2_alias_set as a subset
759 which does not help us here. */
760 /* As we know nothing useful about the dynamic type of the decl just
761 use the usual conflict check rather than a subset test.
762 ??? We could introduce -fvery-strict-aliasing when the language
763 does not allow decls to have a dynamic type that differs from their
764 static type. Then we can check
765 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
766 if (base1_alias_set != base2_alias_set
767 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
768 return false;
769 /* If the size of the access relevant for TBAA through the pointer
770 is bigger than the size of the decl we can't possibly access the
771 decl via that pointer. */
772 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
773 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
774 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
775 /* ??? This in turn may run afoul when a decl of type T which is
776 a member of union type U is accessed through a pointer to
777 type U and sizeof T is smaller than sizeof U. */
778 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
779 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
780 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
781 return false;
783 if (!ref2)
784 return true;
786 /* If the decl is accessed via a MEM_REF, reconstruct the base
787 we can use for TBAA and an appropriately adjusted offset. */
788 dbase2 = ref2;
789 while (handled_component_p (dbase2))
790 dbase2 = TREE_OPERAND (dbase2, 0);
791 doffset1 = offset1;
792 doffset2 = offset2;
793 if (TREE_CODE (dbase2) == MEM_REF
794 || TREE_CODE (dbase2) == TARGET_MEM_REF)
796 double_int moff = mem_ref_offset (dbase2);
797 moff = double_int_lshift (moff,
798 BITS_PER_UNIT == 8
799 ? 3 : exact_log2 (BITS_PER_UNIT),
800 HOST_BITS_PER_DOUBLE_INT, true);
801 if (double_int_negative_p (moff))
802 doffset1 -= double_int_neg (moff).low;
803 else
804 doffset2 -= moff.low;
807 /* If either reference is view-converted, give up now. */
808 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
809 || same_type_for_tbaa (TREE_TYPE (dbase2),
810 TREE_TYPE (reference_alias_ptr_type (dbase2))) != 1)
811 return true;
813 /* If both references are through the same type, they do not alias
814 if the accesses do not overlap. This does extra disambiguation
815 for mixed/pointer accesses but requires strict aliasing.
816 For MEM_REFs we require that the component-ref offset we computed
817 is relative to the start of the type which we ensure by
818 comparing rvalue and access type and disregarding the constant
819 pointer offset. */
820 if ((TREE_CODE (base1) != TARGET_MEM_REF
821 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
822 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
823 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
825 /* Do access-path based disambiguation. */
826 if (ref1 && ref2
827 && (handled_component_p (ref1) || handled_component_p (ref2)))
828 return aliasing_component_refs_p (ref1,
829 ref1_alias_set, base1_alias_set,
830 offset1, max_size1,
831 ref2,
832 ref2_alias_set, base2_alias_set,
833 offset2, max_size2, true);
835 return true;
838 /* Return true if two indirect references based on *PTR1
839 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
840 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
841 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
842 in which case they are computed on-demand. REF1 and REF2
843 if non-NULL are the complete memory reference trees. */
845 static bool
846 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
847 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
848 alias_set_type ref1_alias_set,
849 alias_set_type base1_alias_set,
850 tree ref2 ATTRIBUTE_UNUSED, tree base2,
851 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
852 alias_set_type ref2_alias_set,
853 alias_set_type base2_alias_set, bool tbaa_p)
855 tree ptr1;
856 tree ptr2;
857 tree ptrtype1, ptrtype2;
859 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
860 || TREE_CODE (base1) == TARGET_MEM_REF)
861 && (TREE_CODE (base2) == MEM_REF
862 || TREE_CODE (base2) == TARGET_MEM_REF));
864 ptr1 = TREE_OPERAND (base1, 0);
865 ptr2 = TREE_OPERAND (base2, 0);
867 /* If both bases are based on pointers they cannot alias if they may not
868 point to the same memory object or if they point to the same object
869 and the accesses do not overlap. */
870 if ((!cfun || gimple_in_ssa_p (cfun))
871 && operand_equal_p (ptr1, ptr2, 0)
872 && (((TREE_CODE (base1) != TARGET_MEM_REF
873 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
874 && (TREE_CODE (base2) != TARGET_MEM_REF
875 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
876 || (TREE_CODE (base1) == TARGET_MEM_REF
877 && TREE_CODE (base2) == TARGET_MEM_REF
878 && (TMR_STEP (base1) == TMR_STEP (base2)
879 || (TMR_STEP (base1) && TMR_STEP (base2)
880 && operand_equal_p (TMR_STEP (base1),
881 TMR_STEP (base2), 0)))
882 && (TMR_INDEX (base1) == TMR_INDEX (base2)
883 || (TMR_INDEX (base1) && TMR_INDEX (base2)
884 && operand_equal_p (TMR_INDEX (base1),
885 TMR_INDEX (base2), 0)))
886 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
887 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
888 && operand_equal_p (TMR_INDEX2 (base1),
889 TMR_INDEX2 (base2), 0))))))
891 double_int moff;
892 /* The offset embedded in MEM_REFs can be negative. Bias them
893 so that the resulting offset adjustment is positive. */
894 moff = mem_ref_offset (base1);
895 moff = double_int_lshift (moff,
896 BITS_PER_UNIT == 8
897 ? 3 : exact_log2 (BITS_PER_UNIT),
898 HOST_BITS_PER_DOUBLE_INT, true);
899 if (double_int_negative_p (moff))
900 offset2 += double_int_neg (moff).low;
901 else
902 offset1 += moff.low;
903 moff = mem_ref_offset (base2);
904 moff = double_int_lshift (moff,
905 BITS_PER_UNIT == 8
906 ? 3 : exact_log2 (BITS_PER_UNIT),
907 HOST_BITS_PER_DOUBLE_INT, true);
908 if (double_int_negative_p (moff))
909 offset1 += double_int_neg (moff).low;
910 else
911 offset2 += moff.low;
912 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
914 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
915 return false;
917 /* Disambiguations that rely on strict aliasing rules follow. */
918 if (!flag_strict_aliasing || !tbaa_p)
919 return true;
921 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
922 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
924 /* If the alias set for a pointer access is zero all bets are off. */
925 if (base1_alias_set == -1)
926 base1_alias_set = get_deref_alias_set (ptrtype1);
927 if (base1_alias_set == 0)
928 return true;
929 if (base2_alias_set == -1)
930 base2_alias_set = get_deref_alias_set (ptrtype2);
931 if (base2_alias_set == 0)
932 return true;
934 /* If both references are through the same type, they do not alias
935 if the accesses do not overlap. This does extra disambiguation
936 for mixed/pointer accesses but requires strict aliasing. */
937 if ((TREE_CODE (base1) != TARGET_MEM_REF
938 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
939 && (TREE_CODE (base2) != TARGET_MEM_REF
940 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
941 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
942 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
943 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
944 TREE_TYPE (ptrtype2)) == 1)
945 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
947 /* Do type-based disambiguation. */
948 if (base1_alias_set != base2_alias_set
949 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
950 return false;
952 /* Do access-path based disambiguation. */
953 if (ref1 && ref2
954 && (handled_component_p (ref1) || handled_component_p (ref2))
955 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
956 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
957 return aliasing_component_refs_p (ref1,
958 ref1_alias_set, base1_alias_set,
959 offset1, max_size1,
960 ref2,
961 ref2_alias_set, base2_alias_set,
962 offset2, max_size2, false);
964 return true;
967 /* Return true, if the two memory references REF1 and REF2 may alias. */
969 bool
970 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
972 tree base1, base2;
973 HOST_WIDE_INT offset1 = 0, offset2 = 0;
974 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
975 bool var1_p, var2_p, ind1_p, ind2_p;
977 gcc_checking_assert ((!ref1->ref
978 || TREE_CODE (ref1->ref) == SSA_NAME
979 || DECL_P (ref1->ref)
980 || TREE_CODE (ref1->ref) == STRING_CST
981 || handled_component_p (ref1->ref)
982 || TREE_CODE (ref1->ref) == MEM_REF
983 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
984 && (!ref2->ref
985 || TREE_CODE (ref2->ref) == SSA_NAME
986 || DECL_P (ref2->ref)
987 || TREE_CODE (ref2->ref) == STRING_CST
988 || handled_component_p (ref2->ref)
989 || TREE_CODE (ref2->ref) == MEM_REF
990 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
992 /* Decompose the references into their base objects and the access. */
993 base1 = ao_ref_base (ref1);
994 offset1 = ref1->offset;
995 max_size1 = ref1->max_size;
996 base2 = ao_ref_base (ref2);
997 offset2 = ref2->offset;
998 max_size2 = ref2->max_size;
1000 /* We can end up with registers or constants as bases for example from
1001 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1002 which is seen as a struct copy. */
1003 if (TREE_CODE (base1) == SSA_NAME
1004 || TREE_CODE (base1) == CONST_DECL
1005 || TREE_CODE (base1) == CONSTRUCTOR
1006 || TREE_CODE (base1) == ADDR_EXPR
1007 || CONSTANT_CLASS_P (base1)
1008 || TREE_CODE (base2) == SSA_NAME
1009 || TREE_CODE (base2) == CONST_DECL
1010 || TREE_CODE (base2) == CONSTRUCTOR
1011 || TREE_CODE (base2) == ADDR_EXPR
1012 || CONSTANT_CLASS_P (base2))
1013 return false;
1015 /* We can end up refering to code via function and label decls.
1016 As we likely do not properly track code aliases conservatively
1017 bail out. */
1018 if (TREE_CODE (base1) == FUNCTION_DECL
1019 || TREE_CODE (base1) == LABEL_DECL
1020 || TREE_CODE (base2) == FUNCTION_DECL
1021 || TREE_CODE (base2) == LABEL_DECL)
1022 return true;
1024 /* Defer to simple offset based disambiguation if we have
1025 references based on two decls. Do this before defering to
1026 TBAA to handle must-alias cases in conformance with the
1027 GCC extension of allowing type-punning through unions. */
1028 var1_p = DECL_P (base1);
1029 var2_p = DECL_P (base2);
1030 if (var1_p && var2_p)
1031 return decl_refs_may_alias_p (base1, offset1, max_size1,
1032 base2, offset2, max_size2);
1034 ind1_p = (TREE_CODE (base1) == MEM_REF
1035 || TREE_CODE (base1) == TARGET_MEM_REF);
1036 ind2_p = (TREE_CODE (base2) == MEM_REF
1037 || TREE_CODE (base2) == TARGET_MEM_REF);
1039 /* Canonicalize the pointer-vs-decl case. */
1040 if (ind1_p && var2_p)
1042 HOST_WIDE_INT tmp1;
1043 tree tmp2;
1044 ao_ref *tmp3;
1045 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1046 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1047 tmp2 = base1; base1 = base2; base2 = tmp2;
1048 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1049 var1_p = true;
1050 ind1_p = false;
1051 var2_p = false;
1052 ind2_p = true;
1055 /* First defer to TBAA if possible. */
1056 if (tbaa_p
1057 && flag_strict_aliasing
1058 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1059 ao_ref_alias_set (ref2)))
1060 return false;
1062 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1063 if (var1_p && ind2_p)
1064 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1065 offset2, max_size2,
1066 ao_ref_alias_set (ref2), -1,
1067 ref1->ref, base1,
1068 offset1, max_size1,
1069 ao_ref_alias_set (ref1),
1070 ao_ref_base_alias_set (ref1),
1071 tbaa_p);
1072 else if (ind1_p && ind2_p)
1073 return indirect_refs_may_alias_p (ref1->ref, base1,
1074 offset1, max_size1,
1075 ao_ref_alias_set (ref1), -1,
1076 ref2->ref, base2,
1077 offset2, max_size2,
1078 ao_ref_alias_set (ref2), -1,
1079 tbaa_p);
1081 /* We really do not want to end up here, but returning true is safe. */
1082 #ifdef ENABLE_CHECKING
1083 gcc_unreachable ();
1084 #else
1085 return true;
1086 #endif
1089 bool
1090 refs_may_alias_p (tree ref1, tree ref2)
1092 ao_ref r1, r2;
1093 bool res;
1094 ao_ref_init (&r1, ref1);
1095 ao_ref_init (&r2, ref2);
1096 res = refs_may_alias_p_1 (&r1, &r2, true);
1097 if (res)
1098 ++alias_stats.refs_may_alias_p_may_alias;
1099 else
1100 ++alias_stats.refs_may_alias_p_no_alias;
1101 return res;
1104 /* Returns true if there is a anti-dependence for the STORE that
1105 executes after the LOAD. */
1107 bool
1108 refs_anti_dependent_p (tree load, tree store)
1110 ao_ref r1, r2;
1111 ao_ref_init (&r1, load);
1112 ao_ref_init (&r2, store);
1113 return refs_may_alias_p_1 (&r1, &r2, false);
1116 /* Returns true if there is a output dependence for the stores
1117 STORE1 and STORE2. */
1119 bool
1120 refs_output_dependent_p (tree store1, tree store2)
1122 ao_ref r1, r2;
1123 ao_ref_init (&r1, store1);
1124 ao_ref_init (&r2, store2);
1125 return refs_may_alias_p_1 (&r1, &r2, false);
1128 /* If the call CALL may use the memory reference REF return true,
1129 otherwise return false. */
1131 static bool
1132 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1134 tree base, callee;
1135 unsigned i;
1136 int flags = gimple_call_flags (call);
1138 /* Const functions without a static chain do not implicitly use memory. */
1139 if (!gimple_call_chain (call)
1140 && (flags & (ECF_CONST|ECF_NOVOPS)))
1141 goto process_args;
1143 base = ao_ref_base (ref);
1144 if (!base)
1145 return true;
1147 /* If the reference is based on a decl that is not aliased the call
1148 cannot possibly use it. */
1149 if (DECL_P (base)
1150 && !may_be_aliased (base)
1151 /* But local statics can be used through recursion. */
1152 && !is_global_var (base))
1153 goto process_args;
1155 callee = gimple_call_fndecl (call);
1157 /* Handle those builtin functions explicitly that do not act as
1158 escape points. See tree-ssa-structalias.c:find_func_aliases
1159 for the list of builtins we might need to handle here. */
1160 if (callee != NULL_TREE
1161 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1162 switch (DECL_FUNCTION_CODE (callee))
1164 /* All the following functions read memory pointed to by
1165 their second argument. strcat/strncat additionally
1166 reads memory pointed to by the first argument. */
1167 case BUILT_IN_STRCAT:
1168 case BUILT_IN_STRNCAT:
1170 ao_ref dref;
1171 ao_ref_init_from_ptr_and_size (&dref,
1172 gimple_call_arg (call, 0),
1173 NULL_TREE);
1174 if (refs_may_alias_p_1 (&dref, ref, false))
1175 return true;
1177 /* FALLTHRU */
1178 case BUILT_IN_STRCPY:
1179 case BUILT_IN_STRNCPY:
1180 case BUILT_IN_MEMCPY:
1181 case BUILT_IN_MEMMOVE:
1182 case BUILT_IN_MEMPCPY:
1183 case BUILT_IN_STPCPY:
1184 case BUILT_IN_STPNCPY:
1185 case BUILT_IN_TM_MEMCPY:
1186 case BUILT_IN_TM_MEMMOVE:
1188 ao_ref dref;
1189 tree size = NULL_TREE;
1190 if (gimple_call_num_args (call) == 3)
1191 size = gimple_call_arg (call, 2);
1192 ao_ref_init_from_ptr_and_size (&dref,
1193 gimple_call_arg (call, 1),
1194 size);
1195 return refs_may_alias_p_1 (&dref, ref, false);
1197 case BUILT_IN_STRCAT_CHK:
1198 case BUILT_IN_STRNCAT_CHK:
1200 ao_ref dref;
1201 ao_ref_init_from_ptr_and_size (&dref,
1202 gimple_call_arg (call, 0),
1203 NULL_TREE);
1204 if (refs_may_alias_p_1 (&dref, ref, false))
1205 return true;
1207 /* FALLTHRU */
1208 case BUILT_IN_STRCPY_CHK:
1209 case BUILT_IN_STRNCPY_CHK:
1210 case BUILT_IN_MEMCPY_CHK:
1211 case BUILT_IN_MEMMOVE_CHK:
1212 case BUILT_IN_MEMPCPY_CHK:
1213 case BUILT_IN_STPCPY_CHK:
1215 ao_ref dref;
1216 tree size = NULL_TREE;
1217 if (gimple_call_num_args (call) == 4)
1218 size = gimple_call_arg (call, 2);
1219 ao_ref_init_from_ptr_and_size (&dref,
1220 gimple_call_arg (call, 1),
1221 size);
1222 return refs_may_alias_p_1 (&dref, ref, false);
1224 case BUILT_IN_BCOPY:
1226 ao_ref dref;
1227 tree size = gimple_call_arg (call, 2);
1228 ao_ref_init_from_ptr_and_size (&dref,
1229 gimple_call_arg (call, 0),
1230 size);
1231 return refs_may_alias_p_1 (&dref, ref, false);
1234 /* The following functions read memory pointed to by their
1235 first argument. */
1236 CASE_BUILT_IN_TM_LOAD (1):
1237 CASE_BUILT_IN_TM_LOAD (2):
1238 CASE_BUILT_IN_TM_LOAD (4):
1239 CASE_BUILT_IN_TM_LOAD (8):
1240 CASE_BUILT_IN_TM_LOAD (FLOAT):
1241 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1242 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1243 CASE_BUILT_IN_TM_LOAD (M64):
1244 CASE_BUILT_IN_TM_LOAD (M128):
1245 CASE_BUILT_IN_TM_LOAD (M256):
1246 case BUILT_IN_TM_LOG:
1247 case BUILT_IN_TM_LOG_1:
1248 case BUILT_IN_TM_LOG_2:
1249 case BUILT_IN_TM_LOG_4:
1250 case BUILT_IN_TM_LOG_8:
1251 case BUILT_IN_TM_LOG_FLOAT:
1252 case BUILT_IN_TM_LOG_DOUBLE:
1253 case BUILT_IN_TM_LOG_LDOUBLE:
1254 case BUILT_IN_TM_LOG_M64:
1255 case BUILT_IN_TM_LOG_M128:
1256 case BUILT_IN_TM_LOG_M256:
1257 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1259 /* These read memory pointed to by the first argument. */
1260 case BUILT_IN_STRDUP:
1261 case BUILT_IN_STRNDUP:
1263 ao_ref dref;
1264 tree size = NULL_TREE;
1265 if (gimple_call_num_args (call) == 2)
1266 size = gimple_call_arg (call, 1);
1267 ao_ref_init_from_ptr_and_size (&dref,
1268 gimple_call_arg (call, 0),
1269 size);
1270 return refs_may_alias_p_1 (&dref, ref, false);
1272 /* The following builtins do not read from memory. */
1273 case BUILT_IN_FREE:
1274 case BUILT_IN_MALLOC:
1275 case BUILT_IN_CALLOC:
1276 case BUILT_IN_ALLOCA:
1277 case BUILT_IN_ALLOCA_WITH_ALIGN:
1278 case BUILT_IN_STACK_SAVE:
1279 case BUILT_IN_STACK_RESTORE:
1280 case BUILT_IN_MEMSET:
1281 case BUILT_IN_TM_MEMSET:
1282 case BUILT_IN_MEMSET_CHK:
1283 case BUILT_IN_FREXP:
1284 case BUILT_IN_FREXPF:
1285 case BUILT_IN_FREXPL:
1286 case BUILT_IN_GAMMA_R:
1287 case BUILT_IN_GAMMAF_R:
1288 case BUILT_IN_GAMMAL_R:
1289 case BUILT_IN_LGAMMA_R:
1290 case BUILT_IN_LGAMMAF_R:
1291 case BUILT_IN_LGAMMAL_R:
1292 case BUILT_IN_MODF:
1293 case BUILT_IN_MODFF:
1294 case BUILT_IN_MODFL:
1295 case BUILT_IN_REMQUO:
1296 case BUILT_IN_REMQUOF:
1297 case BUILT_IN_REMQUOL:
1298 case BUILT_IN_SINCOS:
1299 case BUILT_IN_SINCOSF:
1300 case BUILT_IN_SINCOSL:
1301 case BUILT_IN_ASSUME_ALIGNED:
1302 case BUILT_IN_VA_END:
1303 return false;
1304 /* __sync_* builtins and some OpenMP builtins act as threading
1305 barriers. */
1306 #undef DEF_SYNC_BUILTIN
1307 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1308 #include "sync-builtins.def"
1309 #undef DEF_SYNC_BUILTIN
1310 case BUILT_IN_GOMP_ATOMIC_START:
1311 case BUILT_IN_GOMP_ATOMIC_END:
1312 case BUILT_IN_GOMP_BARRIER:
1313 case BUILT_IN_GOMP_TASKWAIT:
1314 case BUILT_IN_GOMP_CRITICAL_START:
1315 case BUILT_IN_GOMP_CRITICAL_END:
1316 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1317 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1318 case BUILT_IN_GOMP_LOOP_END:
1319 case BUILT_IN_GOMP_ORDERED_START:
1320 case BUILT_IN_GOMP_ORDERED_END:
1321 case BUILT_IN_GOMP_PARALLEL_END:
1322 case BUILT_IN_GOMP_SECTIONS_END:
1323 case BUILT_IN_GOMP_SINGLE_COPY_START:
1324 case BUILT_IN_GOMP_SINGLE_COPY_END:
1325 return true;
1327 default:
1328 /* Fallthru to general call handling. */;
1331 /* Check if base is a global static variable that is not read
1332 by the function. */
1333 if (callee != NULL_TREE
1334 && TREE_CODE (base) == VAR_DECL
1335 && TREE_STATIC (base))
1337 struct cgraph_node *node = cgraph_get_node (callee);
1338 bitmap not_read;
1340 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1341 node yet. We should enforce that there are nodes for all decls in the
1342 IL and remove this check instead. */
1343 if (node
1344 && (not_read = ipa_reference_get_not_read_global (node))
1345 && bitmap_bit_p (not_read, DECL_UID (base)))
1346 goto process_args;
1349 /* Check if the base variable is call-used. */
1350 if (DECL_P (base))
1352 if (pt_solution_includes (gimple_call_use_set (call), base))
1353 return true;
1355 else if ((TREE_CODE (base) == MEM_REF
1356 || TREE_CODE (base) == TARGET_MEM_REF)
1357 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1359 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1360 if (!pi)
1361 return true;
1363 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1364 return true;
1366 else
1367 return true;
1369 /* Inspect call arguments for passed-by-value aliases. */
1370 process_args:
1371 for (i = 0; i < gimple_call_num_args (call); ++i)
1373 tree op = gimple_call_arg (call, i);
1374 int flags = gimple_call_arg_flags (call, i);
1376 if (flags & EAF_UNUSED)
1377 continue;
1379 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1380 op = TREE_OPERAND (op, 0);
1382 if (TREE_CODE (op) != SSA_NAME
1383 && !is_gimple_min_invariant (op))
1385 ao_ref r;
1386 ao_ref_init (&r, op);
1387 if (refs_may_alias_p_1 (&r, ref, true))
1388 return true;
1392 return false;
1395 static bool
1396 ref_maybe_used_by_call_p (gimple call, tree ref)
1398 ao_ref r;
1399 bool res;
1400 ao_ref_init (&r, ref);
1401 res = ref_maybe_used_by_call_p_1 (call, &r);
1402 if (res)
1403 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1404 else
1405 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1406 return res;
1410 /* If the statement STMT may use the memory reference REF return
1411 true, otherwise return false. */
1413 bool
1414 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1416 if (is_gimple_assign (stmt))
1418 tree rhs;
1420 /* All memory assign statements are single. */
1421 if (!gimple_assign_single_p (stmt))
1422 return false;
1424 rhs = gimple_assign_rhs1 (stmt);
1425 if (is_gimple_reg (rhs)
1426 || is_gimple_min_invariant (rhs)
1427 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1428 return false;
1430 return refs_may_alias_p (rhs, ref);
1432 else if (is_gimple_call (stmt))
1433 return ref_maybe_used_by_call_p (stmt, ref);
1434 else if (gimple_code (stmt) == GIMPLE_RETURN)
1436 tree retval = gimple_return_retval (stmt);
1437 tree base;
1438 if (retval
1439 && TREE_CODE (retval) != SSA_NAME
1440 && !is_gimple_min_invariant (retval)
1441 && refs_may_alias_p (retval, ref))
1442 return true;
1443 /* If ref escapes the function then the return acts as a use. */
1444 base = get_base_address (ref);
1445 if (!base)
1447 else if (DECL_P (base))
1448 return is_global_var (base);
1449 else if (TREE_CODE (base) == MEM_REF
1450 || TREE_CODE (base) == TARGET_MEM_REF)
1451 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1452 return false;
1455 return true;
1458 /* If the call in statement CALL may clobber the memory reference REF
1459 return true, otherwise return false. */
1461 static bool
1462 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1464 tree base;
1465 tree callee;
1467 /* If the call is pure or const it cannot clobber anything. */
1468 if (gimple_call_flags (call)
1469 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1470 return false;
1472 base = ao_ref_base (ref);
1473 if (!base)
1474 return true;
1476 if (TREE_CODE (base) == SSA_NAME
1477 || CONSTANT_CLASS_P (base))
1478 return false;
1480 /* If the reference is based on a decl that is not aliased the call
1481 cannot possibly clobber it. */
1482 if (DECL_P (base)
1483 && !may_be_aliased (base)
1484 /* But local non-readonly statics can be modified through recursion
1485 or the call may implement a threading barrier which we must
1486 treat as may-def. */
1487 && (TREE_READONLY (base)
1488 || !is_global_var (base)))
1489 return false;
1491 callee = gimple_call_fndecl (call);
1493 /* Handle those builtin functions explicitly that do not act as
1494 escape points. See tree-ssa-structalias.c:find_func_aliases
1495 for the list of builtins we might need to handle here. */
1496 if (callee != NULL_TREE
1497 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1498 switch (DECL_FUNCTION_CODE (callee))
1500 /* All the following functions clobber memory pointed to by
1501 their first argument. */
1502 case BUILT_IN_STRCPY:
1503 case BUILT_IN_STRNCPY:
1504 case BUILT_IN_MEMCPY:
1505 case BUILT_IN_MEMMOVE:
1506 case BUILT_IN_MEMPCPY:
1507 case BUILT_IN_STPCPY:
1508 case BUILT_IN_STPNCPY:
1509 case BUILT_IN_STRCAT:
1510 case BUILT_IN_STRNCAT:
1511 case BUILT_IN_MEMSET:
1512 case BUILT_IN_TM_MEMSET:
1513 CASE_BUILT_IN_TM_STORE (1):
1514 CASE_BUILT_IN_TM_STORE (2):
1515 CASE_BUILT_IN_TM_STORE (4):
1516 CASE_BUILT_IN_TM_STORE (8):
1517 CASE_BUILT_IN_TM_STORE (FLOAT):
1518 CASE_BUILT_IN_TM_STORE (DOUBLE):
1519 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1520 CASE_BUILT_IN_TM_STORE (M64):
1521 CASE_BUILT_IN_TM_STORE (M128):
1522 CASE_BUILT_IN_TM_STORE (M256):
1523 case BUILT_IN_TM_MEMCPY:
1524 case BUILT_IN_TM_MEMMOVE:
1526 ao_ref dref;
1527 tree size = NULL_TREE;
1528 /* Don't pass in size for strncat, as the maximum size
1529 is strlen (dest) + n + 1 instead of n, resp.
1530 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1531 known. */
1532 if (gimple_call_num_args (call) == 3
1533 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1534 size = gimple_call_arg (call, 2);
1535 ao_ref_init_from_ptr_and_size (&dref,
1536 gimple_call_arg (call, 0),
1537 size);
1538 return refs_may_alias_p_1 (&dref, ref, false);
1540 case BUILT_IN_STRCPY_CHK:
1541 case BUILT_IN_STRNCPY_CHK:
1542 case BUILT_IN_MEMCPY_CHK:
1543 case BUILT_IN_MEMMOVE_CHK:
1544 case BUILT_IN_MEMPCPY_CHK:
1545 case BUILT_IN_STPCPY_CHK:
1546 case BUILT_IN_STRCAT_CHK:
1547 case BUILT_IN_STRNCAT_CHK:
1548 case BUILT_IN_MEMSET_CHK:
1550 ao_ref dref;
1551 tree size = NULL_TREE;
1552 /* Don't pass in size for __strncat_chk, as the maximum size
1553 is strlen (dest) + n + 1 instead of n, resp.
1554 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1555 known. */
1556 if (gimple_call_num_args (call) == 4
1557 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1558 size = gimple_call_arg (call, 2);
1559 ao_ref_init_from_ptr_and_size (&dref,
1560 gimple_call_arg (call, 0),
1561 size);
1562 return refs_may_alias_p_1 (&dref, ref, false);
1564 case BUILT_IN_BCOPY:
1566 ao_ref dref;
1567 tree size = gimple_call_arg (call, 2);
1568 ao_ref_init_from_ptr_and_size (&dref,
1569 gimple_call_arg (call, 1),
1570 size);
1571 return refs_may_alias_p_1 (&dref, ref, false);
1573 /* Allocating memory does not have any side-effects apart from
1574 being the definition point for the pointer. */
1575 case BUILT_IN_MALLOC:
1576 case BUILT_IN_CALLOC:
1577 case BUILT_IN_STRDUP:
1578 case BUILT_IN_STRNDUP:
1579 /* Unix98 specifies that errno is set on allocation failure. */
1580 if (flag_errno_math
1581 && targetm.ref_may_alias_errno (ref))
1582 return true;
1583 return false;
1584 case BUILT_IN_STACK_SAVE:
1585 case BUILT_IN_ALLOCA:
1586 case BUILT_IN_ALLOCA_WITH_ALIGN:
1587 case BUILT_IN_ASSUME_ALIGNED:
1588 return false;
1589 /* Freeing memory kills the pointed-to memory. More importantly
1590 the call has to serve as a barrier for moving loads and stores
1591 across it. */
1592 case BUILT_IN_FREE:
1593 case BUILT_IN_VA_END:
1595 tree ptr = gimple_call_arg (call, 0);
1596 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1598 case BUILT_IN_GAMMA_R:
1599 case BUILT_IN_GAMMAF_R:
1600 case BUILT_IN_GAMMAL_R:
1601 case BUILT_IN_LGAMMA_R:
1602 case BUILT_IN_LGAMMAF_R:
1603 case BUILT_IN_LGAMMAL_R:
1605 tree out = gimple_call_arg (call, 1);
1606 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1607 return true;
1608 if (flag_errno_math)
1609 break;
1610 return false;
1612 case BUILT_IN_FREXP:
1613 case BUILT_IN_FREXPF:
1614 case BUILT_IN_FREXPL:
1615 case BUILT_IN_MODF:
1616 case BUILT_IN_MODFF:
1617 case BUILT_IN_MODFL:
1619 tree out = gimple_call_arg (call, 1);
1620 return ptr_deref_may_alias_ref_p_1 (out, ref);
1622 case BUILT_IN_REMQUO:
1623 case BUILT_IN_REMQUOF:
1624 case BUILT_IN_REMQUOL:
1626 tree out = gimple_call_arg (call, 2);
1627 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1628 return true;
1629 if (flag_errno_math)
1630 break;
1631 return false;
1633 case BUILT_IN_SINCOS:
1634 case BUILT_IN_SINCOSF:
1635 case BUILT_IN_SINCOSL:
1637 tree sin = gimple_call_arg (call, 1);
1638 tree cos = gimple_call_arg (call, 2);
1639 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1640 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1642 /* __sync_* builtins and some OpenMP builtins act as threading
1643 barriers. */
1644 #undef DEF_SYNC_BUILTIN
1645 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1646 #include "sync-builtins.def"
1647 #undef DEF_SYNC_BUILTIN
1648 case BUILT_IN_GOMP_ATOMIC_START:
1649 case BUILT_IN_GOMP_ATOMIC_END:
1650 case BUILT_IN_GOMP_BARRIER:
1651 case BUILT_IN_GOMP_TASKWAIT:
1652 case BUILT_IN_GOMP_CRITICAL_START:
1653 case BUILT_IN_GOMP_CRITICAL_END:
1654 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1655 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1656 case BUILT_IN_GOMP_LOOP_END:
1657 case BUILT_IN_GOMP_ORDERED_START:
1658 case BUILT_IN_GOMP_ORDERED_END:
1659 case BUILT_IN_GOMP_PARALLEL_END:
1660 case BUILT_IN_GOMP_SECTIONS_END:
1661 case BUILT_IN_GOMP_SINGLE_COPY_START:
1662 case BUILT_IN_GOMP_SINGLE_COPY_END:
1663 return true;
1664 default:
1665 /* Fallthru to general call handling. */;
1668 /* Check if base is a global static variable that is not written
1669 by the function. */
1670 if (callee != NULL_TREE
1671 && TREE_CODE (base) == VAR_DECL
1672 && TREE_STATIC (base))
1674 struct cgraph_node *node = cgraph_get_node (callee);
1675 bitmap not_written;
1677 if (node
1678 && (not_written = ipa_reference_get_not_written_global (node))
1679 && bitmap_bit_p (not_written, DECL_UID (base)))
1680 return false;
1683 /* Check if the base variable is call-clobbered. */
1684 if (DECL_P (base))
1685 return pt_solution_includes (gimple_call_clobber_set (call), base);
1686 else if ((TREE_CODE (base) == MEM_REF
1687 || TREE_CODE (base) == TARGET_MEM_REF)
1688 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1690 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1691 if (!pi)
1692 return true;
1694 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1697 return true;
1700 /* If the call in statement CALL may clobber the memory reference REF
1701 return true, otherwise return false. */
1703 bool
1704 call_may_clobber_ref_p (gimple call, tree ref)
1706 bool res;
1707 ao_ref r;
1708 ao_ref_init (&r, ref);
1709 res = call_may_clobber_ref_p_1 (call, &r);
1710 if (res)
1711 ++alias_stats.call_may_clobber_ref_p_may_alias;
1712 else
1713 ++alias_stats.call_may_clobber_ref_p_no_alias;
1714 return res;
1718 /* If the statement STMT may clobber the memory reference REF return true,
1719 otherwise return false. */
1721 bool
1722 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1724 if (is_gimple_call (stmt))
1726 tree lhs = gimple_call_lhs (stmt);
1727 if (lhs
1728 && TREE_CODE (lhs) != SSA_NAME)
1730 ao_ref r;
1731 ao_ref_init (&r, lhs);
1732 if (refs_may_alias_p_1 (ref, &r, true))
1733 return true;
1736 return call_may_clobber_ref_p_1 (stmt, ref);
1738 else if (gimple_assign_single_p (stmt))
1740 tree lhs = gimple_assign_lhs (stmt);
1741 if (TREE_CODE (lhs) != SSA_NAME)
1743 ao_ref r;
1744 ao_ref_init (&r, lhs);
1745 return refs_may_alias_p_1 (ref, &r, true);
1748 else if (gimple_code (stmt) == GIMPLE_ASM)
1749 return true;
1751 return false;
1754 bool
1755 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1757 ao_ref r;
1758 ao_ref_init (&r, ref);
1759 return stmt_may_clobber_ref_p_1 (stmt, &r);
1762 /* If STMT kills the memory reference REF return true, otherwise
1763 return false. */
1765 static bool
1766 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1768 /* For a must-alias check we need to be able to constrain
1769 the access properly. */
1770 ao_ref_base (ref);
1771 if (ref->max_size == -1)
1772 return false;
1774 if (gimple_has_lhs (stmt)
1775 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1776 /* The assignment is not necessarily carried out if it can throw
1777 and we can catch it in the current function where we could inspect
1778 the previous value.
1779 ??? We only need to care about the RHS throwing. For aggregate
1780 assignments or similar calls and non-call exceptions the LHS
1781 might throw as well. */
1782 && !stmt_can_throw_internal (stmt))
1784 tree base, lhs = gimple_get_lhs (stmt);
1785 HOST_WIDE_INT size, offset, max_size;
1786 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1787 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1788 so base == ref->base does not always hold. */
1789 if (base == ref->base)
1791 /* For a must-alias check we need to be able to constrain
1792 the access properly. */
1793 if (size != -1 && size == max_size)
1795 if (offset <= ref->offset
1796 && offset + size >= ref->offset + ref->max_size)
1797 return true;
1802 if (is_gimple_call (stmt))
1804 tree callee = gimple_call_fndecl (stmt);
1805 if (callee != NULL_TREE
1806 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1807 switch (DECL_FUNCTION_CODE (callee))
1809 case BUILT_IN_MEMCPY:
1810 case BUILT_IN_MEMPCPY:
1811 case BUILT_IN_MEMMOVE:
1812 case BUILT_IN_MEMSET:
1813 case BUILT_IN_MEMCPY_CHK:
1814 case BUILT_IN_MEMPCPY_CHK:
1815 case BUILT_IN_MEMMOVE_CHK:
1816 case BUILT_IN_MEMSET_CHK:
1818 tree dest = gimple_call_arg (stmt, 0);
1819 tree len = gimple_call_arg (stmt, 2);
1820 tree base = NULL_TREE;
1821 HOST_WIDE_INT offset = 0;
1822 if (!host_integerp (len, 0))
1823 return false;
1824 if (TREE_CODE (dest) == ADDR_EXPR)
1825 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1826 &offset);
1827 else if (TREE_CODE (dest) == SSA_NAME)
1828 base = dest;
1829 if (base
1830 && base == ao_ref_base (ref))
1832 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1833 if (offset <= ref->offset / BITS_PER_UNIT
1834 && (offset + size
1835 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1836 / BITS_PER_UNIT)))
1837 return true;
1839 break;
1842 case BUILT_IN_VA_END:
1844 tree ptr = gimple_call_arg (stmt, 0);
1845 if (TREE_CODE (ptr) == ADDR_EXPR)
1847 tree base = ao_ref_base (ref);
1848 if (TREE_OPERAND (ptr, 0) == base)
1849 return true;
1851 break;
1854 default:;
1857 return false;
1860 bool
1861 stmt_kills_ref_p (gimple stmt, tree ref)
1863 ao_ref r;
1864 ao_ref_init (&r, ref);
1865 return stmt_kills_ref_p_1 (stmt, &r);
1869 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1870 TARGET or a statement clobbering the memory reference REF in which
1871 case false is returned. The walk starts with VUSE, one argument of PHI. */
1873 static bool
1874 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1875 tree vuse, bitmap *visited)
1877 basic_block bb = gimple_bb (phi);
1879 if (!*visited)
1880 *visited = BITMAP_ALLOC (NULL);
1882 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1884 /* Walk until we hit the target. */
1885 while (vuse != target)
1887 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1888 /* Recurse for PHI nodes. */
1889 if (gimple_code (def_stmt) == GIMPLE_PHI)
1891 /* An already visited PHI node ends the walk successfully. */
1892 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1893 return true;
1894 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1895 if (!vuse)
1896 return false;
1897 continue;
1899 /* A clobbering statement or the end of the IL ends it failing. */
1900 else if (gimple_nop_p (def_stmt)
1901 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1902 return false;
1903 /* If we reach a new basic-block see if we already skipped it
1904 in a previous walk that ended successfully. */
1905 if (gimple_bb (def_stmt) != bb)
1907 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
1908 return true;
1909 bb = gimple_bb (def_stmt);
1911 vuse = gimple_vuse (def_stmt);
1913 return true;
1916 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
1917 until we hit the phi argument definition that dominates the other one.
1918 Return that, or NULL_TREE if there is no such definition. */
1920 static tree
1921 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
1922 ao_ref *ref, bitmap *visited)
1924 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1925 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1926 tree common_vuse;
1928 if (arg0 == arg1)
1929 return arg0;
1930 else if (gimple_nop_p (def0)
1931 || (!gimple_nop_p (def1)
1932 && dominated_by_p (CDI_DOMINATORS,
1933 gimple_bb (def1), gimple_bb (def0))))
1935 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1936 return arg0;
1938 else if (gimple_nop_p (def1)
1939 || dominated_by_p (CDI_DOMINATORS,
1940 gimple_bb (def0), gimple_bb (def1)))
1942 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1943 return arg1;
1945 /* Special case of a diamond:
1946 MEM_1 = ...
1947 goto (cond) ? L1 : L2
1948 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1949 goto L3
1950 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1951 L3: MEM_4 = PHI<MEM_2, MEM_3>
1952 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1953 dominate each other, but still we can easily skip this PHI node
1954 if we recognize that the vuse MEM operand is the same for both,
1955 and that we can skip both statements (they don't clobber us).
1956 This is still linear. Don't use maybe_skip_until, that might
1957 potentially be slow. */
1958 else if ((common_vuse = gimple_vuse (def0))
1959 && common_vuse == gimple_vuse (def1))
1961 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1962 && !stmt_may_clobber_ref_p_1 (def1, ref))
1963 return common_vuse;
1966 return NULL_TREE;
1970 /* Starting from a PHI node for the virtual operand of the memory reference
1971 REF find a continuation virtual operand that allows to continue walking
1972 statements dominating PHI skipping only statements that cannot possibly
1973 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1974 be found. */
1976 tree
1977 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1979 unsigned nargs = gimple_phi_num_args (phi);
1981 /* Through a single-argument PHI we can simply look through. */
1982 if (nargs == 1)
1983 return PHI_ARG_DEF (phi, 0);
1985 /* For two or more arguments try to pairwise skip non-aliasing code
1986 until we hit the phi argument definition that dominates the other one. */
1987 else if (nargs >= 2)
1989 tree arg0, arg1;
1990 unsigned i;
1992 /* Find a candidate for the virtual operand which definition
1993 dominates those of all others. */
1994 arg0 = PHI_ARG_DEF (phi, 0);
1995 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
1996 for (i = 1; i < nargs; ++i)
1998 arg1 = PHI_ARG_DEF (phi, i);
1999 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2001 arg0 = arg1;
2002 break;
2004 if (dominated_by_p (CDI_DOMINATORS,
2005 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2006 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2007 arg0 = arg1;
2010 /* Then pairwise reduce against the found candidate. */
2011 for (i = 0; i < nargs; ++i)
2013 arg1 = PHI_ARG_DEF (phi, i);
2014 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref, visited);
2015 if (!arg0)
2016 return NULL_TREE;
2019 return arg0;
2022 return NULL_TREE;
2025 /* Based on the memory reference REF and its virtual use VUSE call
2026 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2027 itself. That is, for each virtual use for which its defining statement
2028 does not clobber REF.
2030 WALKER is called with REF, the current virtual use and DATA. If
2031 WALKER returns non-NULL the walk stops and its result is returned.
2032 At the end of a non-successful walk NULL is returned.
2034 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2035 use which definition is a statement that may clobber REF and DATA.
2036 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2037 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2038 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2039 to adjust REF and *DATA to make that valid.
2041 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2043 void *
2044 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2045 void *(*walker)(ao_ref *, tree, void *),
2046 void *(*translate)(ao_ref *, tree, void *), void *data)
2048 bitmap visited = NULL;
2049 void *res;
2051 timevar_push (TV_ALIAS_STMT_WALK);
2055 gimple def_stmt;
2057 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2058 res = (*walker) (ref, vuse, data);
2059 if (res)
2060 break;
2062 def_stmt = SSA_NAME_DEF_STMT (vuse);
2063 if (gimple_nop_p (def_stmt))
2064 break;
2065 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2066 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
2067 else
2069 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2071 if (!translate)
2072 break;
2073 res = (*translate) (ref, vuse, data);
2074 /* Failed lookup and translation. */
2075 if (res == (void *)-1)
2077 res = NULL;
2078 break;
2080 /* Lookup succeeded. */
2081 else if (res != NULL)
2082 break;
2083 /* Translation succeeded, continue walking. */
2085 vuse = gimple_vuse (def_stmt);
2088 while (vuse);
2090 if (visited)
2091 BITMAP_FREE (visited);
2093 timevar_pop (TV_ALIAS_STMT_WALK);
2095 return res;
2099 /* Based on the memory reference REF call WALKER for each vdef which
2100 defining statement may clobber REF, starting with VDEF. If REF
2101 is NULL_TREE, each defining statement is visited.
2103 WALKER is called with REF, the current vdef and DATA. If WALKER
2104 returns true the walk is stopped, otherwise it continues.
2106 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2107 PHI argument (but only one walk continues on merge points), the
2108 return value is true if any of the walks was successful.
2110 The function returns the number of statements walked. */
2112 static unsigned int
2113 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2114 bool (*walker)(ao_ref *, tree, void *), void *data,
2115 bitmap *visited, unsigned int cnt)
2119 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2121 if (*visited
2122 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2123 return cnt;
2125 if (gimple_nop_p (def_stmt))
2126 return cnt;
2127 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2129 unsigned i;
2130 if (!*visited)
2131 *visited = BITMAP_ALLOC (NULL);
2132 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2133 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2134 walker, data, visited, 0);
2135 return cnt;
2138 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2139 cnt++;
2140 if ((!ref
2141 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2142 && (*walker) (ref, vdef, data))
2143 return cnt;
2145 vdef = gimple_vuse (def_stmt);
2147 while (1);
2150 unsigned int
2151 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2152 bool (*walker)(ao_ref *, tree, void *), void *data,
2153 bitmap *visited)
2155 bitmap local_visited = NULL;
2156 unsigned int ret;
2158 timevar_push (TV_ALIAS_STMT_WALK);
2160 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2161 visited ? visited : &local_visited, 0);
2162 if (local_visited)
2163 BITMAP_FREE (local_visited);
2165 timevar_pop (TV_ALIAS_STMT_WALK);
2167 return ret;