* config/epiphany/epiphany.md (GPR_1): New constant.
[official-gcc.git] / gcc / tree-ssa-alias.c
blob968c50548938a21b7b186950e8344ea8f75143d9
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "target.h"
28 #include "basic-block.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "tree-pretty-print.h"
35 #include "dumpfile.h"
36 #include "gimple.h"
37 #include "tree-flow.h"
38 #include "tree-inline.h"
39 #include "params.h"
40 #include "vec.h"
41 #include "bitmap.h"
42 #include "pointer-set.h"
43 #include "alloc-pool.h"
44 #include "tree-ssa-alias.h"
46 /* Broad overview of how alias analysis on gimple works:
48 Statements clobbering or using memory are linked through the
49 virtual operand factored use-def chain. The virtual operand
50 is unique per function, its symbol is accessible via gimple_vop (cfun).
51 Virtual operands are used for efficiently walking memory statements
52 in the gimple IL and are useful for things like value-numbering as
53 a generation count for memory references.
55 SSA_NAME pointers may have associated points-to information
56 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
57 points-to information is (re-)computed by the TODO_rebuild_alias
58 pass manager todo. Points-to information is also used for more
59 precise tracking of call-clobbered and call-used variables and
60 related disambiguations.
62 This file contains functions for disambiguating memory references,
63 the so called alias-oracle and tools for walking of the gimple IL.
65 The main alias-oracle entry-points are
67 bool stmt_may_clobber_ref_p (gimple, tree)
69 This function queries if a statement may invalidate (parts of)
70 the memory designated by the reference tree argument.
72 bool ref_maybe_used_by_stmt_p (gimple, tree)
74 This function queries if a statement may need (parts of) the
75 memory designated by the reference tree argument.
77 There are variants of these functions that only handle the call
78 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
79 Note that these do not disambiguate against a possible call lhs.
81 bool refs_may_alias_p (tree, tree)
83 This function tries to disambiguate two reference trees.
85 bool ptr_deref_may_alias_global_p (tree)
87 This function queries if dereferencing a pointer variable may
88 alias global memory.
90 More low-level disambiguators are available and documented in
91 this file. Low-level disambiguators dealing with points-to
92 information are in tree-ssa-structalias.c. */
95 /* Query statistics for the different low-level disambiguators.
96 A high-level query may trigger multiple of them. */
98 static struct {
99 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
100 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
101 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
102 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
103 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
104 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
105 } alias_stats;
107 void
108 dump_alias_stats (FILE *s)
110 fprintf (s, "\nAlias oracle query stats:\n");
111 fprintf (s, " refs_may_alias_p: "
112 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
113 HOST_WIDE_INT_PRINT_DEC" queries\n",
114 alias_stats.refs_may_alias_p_no_alias,
115 alias_stats.refs_may_alias_p_no_alias
116 + alias_stats.refs_may_alias_p_may_alias);
117 fprintf (s, " ref_maybe_used_by_call_p: "
118 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
119 HOST_WIDE_INT_PRINT_DEC" queries\n",
120 alias_stats.ref_maybe_used_by_call_p_no_alias,
121 alias_stats.refs_may_alias_p_no_alias
122 + alias_stats.ref_maybe_used_by_call_p_may_alias);
123 fprintf (s, " call_may_clobber_ref_p: "
124 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
125 HOST_WIDE_INT_PRINT_DEC" queries\n",
126 alias_stats.call_may_clobber_ref_p_no_alias,
127 alias_stats.call_may_clobber_ref_p_no_alias
128 + alias_stats.call_may_clobber_ref_p_may_alias);
132 /* Return true, if dereferencing PTR may alias with a global variable. */
134 bool
135 ptr_deref_may_alias_global_p (tree ptr)
137 struct ptr_info_def *pi;
139 /* If we end up with a pointer constant here that may point
140 to global memory. */
141 if (TREE_CODE (ptr) != SSA_NAME)
142 return true;
144 pi = SSA_NAME_PTR_INFO (ptr);
146 /* If we do not have points-to information for this variable,
147 we have to punt. */
148 if (!pi)
149 return true;
151 /* ??? This does not use TBAA to prune globals ptr may not access. */
152 return pt_solution_includes_global (&pi->pt);
155 /* Return true if dereferencing PTR may alias DECL.
156 The caller is responsible for applying TBAA to see if PTR
157 may access DECL at all. */
159 static bool
160 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
162 struct ptr_info_def *pi;
164 /* Conversions are irrelevant for points-to information and
165 data-dependence analysis can feed us those. */
166 STRIP_NOPS (ptr);
168 /* Anything we do not explicilty handle aliases. */
169 if ((TREE_CODE (ptr) != SSA_NAME
170 && TREE_CODE (ptr) != ADDR_EXPR
171 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
172 || !POINTER_TYPE_P (TREE_TYPE (ptr))
173 || (TREE_CODE (decl) != VAR_DECL
174 && TREE_CODE (decl) != PARM_DECL
175 && TREE_CODE (decl) != RESULT_DECL))
176 return true;
178 /* Disregard pointer offsetting. */
179 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
183 ptr = TREE_OPERAND (ptr, 0);
185 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
186 return ptr_deref_may_alias_decl_p (ptr, decl);
189 /* ADDR_EXPR pointers either just offset another pointer or directly
190 specify the pointed-to set. */
191 if (TREE_CODE (ptr) == ADDR_EXPR)
193 tree base = get_base_address (TREE_OPERAND (ptr, 0));
194 if (base
195 && (TREE_CODE (base) == MEM_REF
196 || TREE_CODE (base) == TARGET_MEM_REF))
197 ptr = TREE_OPERAND (base, 0);
198 else if (base
199 && DECL_P (base))
200 return base == decl;
201 else if (base
202 && CONSTANT_CLASS_P (base))
203 return false;
204 else
205 return true;
208 /* Non-aliased variables can not be pointed to. */
209 if (!may_be_aliased (decl))
210 return false;
212 /* If we do not have useful points-to information for this pointer
213 we cannot disambiguate anything else. */
214 pi = SSA_NAME_PTR_INFO (ptr);
215 if (!pi)
216 return true;
218 return pt_solution_includes (&pi->pt, decl);
221 /* Return true if dereferenced PTR1 and PTR2 may alias.
222 The caller is responsible for applying TBAA to see if accesses
223 through PTR1 and PTR2 may conflict at all. */
225 bool
226 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
228 struct ptr_info_def *pi1, *pi2;
230 /* Conversions are irrelevant for points-to information and
231 data-dependence analysis can feed us those. */
232 STRIP_NOPS (ptr1);
233 STRIP_NOPS (ptr2);
235 /* Disregard pointer offsetting. */
236 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
240 ptr1 = TREE_OPERAND (ptr1, 0);
242 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
243 return ptr_derefs_may_alias_p (ptr1, ptr2);
245 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
249 ptr2 = TREE_OPERAND (ptr2, 0);
251 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
252 return ptr_derefs_may_alias_p (ptr1, ptr2);
255 /* ADDR_EXPR pointers either just offset another pointer or directly
256 specify the pointed-to set. */
257 if (TREE_CODE (ptr1) == ADDR_EXPR)
259 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
260 if (base
261 && (TREE_CODE (base) == MEM_REF
262 || TREE_CODE (base) == TARGET_MEM_REF))
263 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
264 else if (base
265 && DECL_P (base))
266 return ptr_deref_may_alias_decl_p (ptr2, base);
267 else
268 return true;
270 if (TREE_CODE (ptr2) == ADDR_EXPR)
272 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
273 if (base
274 && (TREE_CODE (base) == MEM_REF
275 || TREE_CODE (base) == TARGET_MEM_REF))
276 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
277 else if (base
278 && DECL_P (base))
279 return ptr_deref_may_alias_decl_p (ptr1, base);
280 else
281 return true;
284 /* From here we require SSA name pointers. Anything else aliases. */
285 if (TREE_CODE (ptr1) != SSA_NAME
286 || TREE_CODE (ptr2) != SSA_NAME
287 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
288 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
289 return true;
291 /* We may end up with two empty points-to solutions for two same pointers.
292 In this case we still want to say both pointers alias, so shortcut
293 that here. */
294 if (ptr1 == ptr2)
295 return true;
297 /* If we do not have useful points-to information for either pointer
298 we cannot disambiguate anything else. */
299 pi1 = SSA_NAME_PTR_INFO (ptr1);
300 pi2 = SSA_NAME_PTR_INFO (ptr2);
301 if (!pi1 || !pi2)
302 return true;
304 /* ??? This does not use TBAA to prune decls from the intersection
305 that not both pointers may access. */
306 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
309 /* Return true if dereferencing PTR may alias *REF.
310 The caller is responsible for applying TBAA to see if PTR
311 may access *REF at all. */
313 static bool
314 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
316 tree base = ao_ref_base (ref);
318 if (TREE_CODE (base) == MEM_REF
319 || TREE_CODE (base) == TARGET_MEM_REF)
320 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
321 else if (DECL_P (base))
322 return ptr_deref_may_alias_decl_p (ptr, base);
324 return true;
327 /* Return true whether REF may refer to global memory. */
329 bool
330 ref_may_alias_global_p (tree ref)
332 tree base = get_base_address (ref);
333 if (DECL_P (base))
334 return is_global_var (base);
335 else if (TREE_CODE (base) == MEM_REF
336 || TREE_CODE (base) == TARGET_MEM_REF)
337 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
338 return true;
341 /* Return true whether STMT may clobber global memory. */
343 bool
344 stmt_may_clobber_global_p (gimple stmt)
346 tree lhs;
348 if (!gimple_vdef (stmt))
349 return false;
351 /* ??? We can ask the oracle whether an artificial pointer
352 dereference with a pointer with points-to information covering
353 all global memory (what about non-address taken memory?) maybe
354 clobbered by this call. As there is at the moment no convenient
355 way of doing that without generating garbage do some manual
356 checking instead.
357 ??? We could make a NULL ao_ref argument to the various
358 predicates special, meaning any global memory. */
360 switch (gimple_code (stmt))
362 case GIMPLE_ASSIGN:
363 lhs = gimple_assign_lhs (stmt);
364 return (TREE_CODE (lhs) != SSA_NAME
365 && ref_may_alias_global_p (lhs));
366 case GIMPLE_CALL:
367 return true;
368 default:
369 return true;
374 /* Dump alias information on FILE. */
376 void
377 dump_alias_info (FILE *file)
379 unsigned i;
380 const char *funcname
381 = lang_hooks.decl_printable_name (current_function_decl, 2);
382 tree var;
384 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
386 fprintf (file, "Aliased symbols\n\n");
388 FOR_EACH_LOCAL_DECL (cfun, i, var)
390 if (may_be_aliased (var))
391 dump_variable (file, var);
394 fprintf (file, "\nCall clobber information\n");
396 fprintf (file, "\nESCAPED");
397 dump_points_to_solution (file, &cfun->gimple_df->escaped);
399 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
401 for (i = 1; i < num_ssa_names; i++)
403 tree ptr = ssa_name (i);
404 struct ptr_info_def *pi;
406 if (ptr == NULL_TREE
407 || SSA_NAME_IN_FREE_LIST (ptr))
408 continue;
410 pi = SSA_NAME_PTR_INFO (ptr);
411 if (pi)
412 dump_points_to_info_for (file, ptr);
415 fprintf (file, "\n");
419 /* Dump alias information on stderr. */
421 DEBUG_FUNCTION void
422 debug_alias_info (void)
424 dump_alias_info (stderr);
428 /* Dump the points-to set *PT into FILE. */
430 void
431 dump_points_to_solution (FILE *file, struct pt_solution *pt)
433 if (pt->anything)
434 fprintf (file, ", points-to anything");
436 if (pt->nonlocal)
437 fprintf (file, ", points-to non-local");
439 if (pt->escaped)
440 fprintf (file, ", points-to escaped");
442 if (pt->ipa_escaped)
443 fprintf (file, ", points-to unit escaped");
445 if (pt->null)
446 fprintf (file, ", points-to NULL");
448 if (pt->vars)
450 fprintf (file, ", points-to vars: ");
451 dump_decl_set (file, pt->vars);
452 if (pt->vars_contains_global)
453 fprintf (file, " (includes global vars)");
458 /* Unified dump function for pt_solution. */
460 DEBUG_FUNCTION void
461 debug (pt_solution &ref)
463 dump_points_to_solution (stderr, &ref);
466 DEBUG_FUNCTION void
467 debug (pt_solution *ptr)
469 if (ptr)
470 debug (*ptr);
471 else
472 fprintf (stderr, "<nil>\n");
476 /* Dump points-to information for SSA_NAME PTR into FILE. */
478 void
479 dump_points_to_info_for (FILE *file, tree ptr)
481 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
483 print_generic_expr (file, ptr, dump_flags);
485 if (pi)
486 dump_points_to_solution (file, &pi->pt);
487 else
488 fprintf (file, ", points-to anything");
490 fprintf (file, "\n");
494 /* Dump points-to information for VAR into stderr. */
496 DEBUG_FUNCTION void
497 debug_points_to_info_for (tree var)
499 dump_points_to_info_for (stderr, var);
503 /* Initializes the alias-oracle reference representation *R from REF. */
505 void
506 ao_ref_init (ao_ref *r, tree ref)
508 r->ref = ref;
509 r->base = NULL_TREE;
510 r->offset = 0;
511 r->size = -1;
512 r->max_size = -1;
513 r->ref_alias_set = -1;
514 r->base_alias_set = -1;
515 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
518 /* Returns the base object of the memory reference *REF. */
520 tree
521 ao_ref_base (ao_ref *ref)
523 if (ref->base)
524 return ref->base;
525 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
526 &ref->max_size);
527 return ref->base;
530 /* Returns the base object alias set of the memory reference *REF. */
532 static alias_set_type
533 ao_ref_base_alias_set (ao_ref *ref)
535 tree base_ref;
536 if (ref->base_alias_set != -1)
537 return ref->base_alias_set;
538 if (!ref->ref)
539 return 0;
540 base_ref = ref->ref;
541 while (handled_component_p (base_ref))
542 base_ref = TREE_OPERAND (base_ref, 0);
543 ref->base_alias_set = get_alias_set (base_ref);
544 return ref->base_alias_set;
547 /* Returns the reference alias set of the memory reference *REF. */
549 alias_set_type
550 ao_ref_alias_set (ao_ref *ref)
552 if (ref->ref_alias_set != -1)
553 return ref->ref_alias_set;
554 ref->ref_alias_set = get_alias_set (ref->ref);
555 return ref->ref_alias_set;
558 /* Init an alias-oracle reference representation from a gimple pointer
559 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
560 size is assumed to be unknown. The access is assumed to be only
561 to or after of the pointer target, not before it. */
563 void
564 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
566 HOST_WIDE_INT t1, t2;
567 ref->ref = NULL_TREE;
568 if (TREE_CODE (ptr) == ADDR_EXPR)
569 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
570 &ref->offset, &t1, &t2);
571 else
573 ref->base = build2 (MEM_REF, char_type_node,
574 ptr, null_pointer_node);
575 ref->offset = 0;
577 if (size
578 && host_integerp (size, 0)
579 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
580 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
581 else
582 ref->max_size = ref->size = -1;
583 ref->ref_alias_set = 0;
584 ref->base_alias_set = 0;
585 ref->volatile_p = false;
588 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
589 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
590 decide. */
592 static inline int
593 same_type_for_tbaa (tree type1, tree type2)
595 type1 = TYPE_MAIN_VARIANT (type1);
596 type2 = TYPE_MAIN_VARIANT (type2);
598 /* If we would have to do structural comparison bail out. */
599 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
600 || TYPE_STRUCTURAL_EQUALITY_P (type2))
601 return -1;
603 /* Compare the canonical types. */
604 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
605 return 1;
607 /* ??? Array types are not properly unified in all cases as we have
608 spurious changes in the index types for example. Removing this
609 causes all sorts of problems with the Fortran frontend. */
610 if (TREE_CODE (type1) == ARRAY_TYPE
611 && TREE_CODE (type2) == ARRAY_TYPE)
612 return -1;
614 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
615 object of one of its constrained subtypes, e.g. when a function with an
616 unconstrained parameter passed by reference is called on an object and
617 inlined. But, even in the case of a fixed size, type and subtypes are
618 not equivalent enough as to share the same TYPE_CANONICAL, since this
619 would mean that conversions between them are useless, whereas they are
620 not (e.g. type and subtypes can have different modes). So, in the end,
621 they are only guaranteed to have the same alias set. */
622 if (get_alias_set (type1) == get_alias_set (type2))
623 return -1;
625 /* The types are known to be not equal. */
626 return 0;
629 /* Determine if the two component references REF1 and REF2 which are
630 based on access types TYPE1 and TYPE2 and of which at least one is based
631 on an indirect reference may alias. REF2 is the only one that can
632 be a decl in which case REF2_IS_DECL is true.
633 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
634 are the respective alias sets. */
636 static bool
637 aliasing_component_refs_p (tree ref1,
638 alias_set_type ref1_alias_set,
639 alias_set_type base1_alias_set,
640 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
641 tree ref2,
642 alias_set_type ref2_alias_set,
643 alias_set_type base2_alias_set,
644 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
645 bool ref2_is_decl)
647 /* If one reference is a component references through pointers try to find a
648 common base and apply offset based disambiguation. This handles
649 for example
650 struct A { int i; int j; } *q;
651 struct B { struct A a; int k; } *p;
652 disambiguating q->i and p->a.j. */
653 tree base1, base2;
654 tree type1, type2;
655 tree *refp;
656 int same_p;
658 /* Choose bases and base types to search for. */
659 base1 = ref1;
660 while (handled_component_p (base1))
661 base1 = TREE_OPERAND (base1, 0);
662 type1 = TREE_TYPE (base1);
663 base2 = ref2;
664 while (handled_component_p (base2))
665 base2 = TREE_OPERAND (base2, 0);
666 type2 = TREE_TYPE (base2);
668 /* Now search for the type1 in the access path of ref2. This
669 would be a common base for doing offset based disambiguation on. */
670 refp = &ref2;
671 while (handled_component_p (*refp)
672 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
673 refp = &TREE_OPERAND (*refp, 0);
674 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
675 /* If we couldn't compare types we have to bail out. */
676 if (same_p == -1)
677 return true;
678 else if (same_p == 1)
680 HOST_WIDE_INT offadj, sztmp, msztmp;
681 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
682 offset2 -= offadj;
683 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
684 offset1 -= offadj;
685 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
687 /* If we didn't find a common base, try the other way around. */
688 refp = &ref1;
689 while (handled_component_p (*refp)
690 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
691 refp = &TREE_OPERAND (*refp, 0);
692 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
693 /* If we couldn't compare types we have to bail out. */
694 if (same_p == -1)
695 return true;
696 else if (same_p == 1)
698 HOST_WIDE_INT offadj, sztmp, msztmp;
699 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
700 offset1 -= offadj;
701 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
702 offset2 -= offadj;
703 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
706 /* If we have two type access paths B1.path1 and B2.path2 they may
707 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
708 But we can still have a path that goes B1.path1...B2.path2 with
709 a part that we do not see. So we can only disambiguate now
710 if there is no B2 in the tail of path1 and no B1 on the
711 tail of path2. */
712 if (base1_alias_set == ref2_alias_set
713 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
714 return true;
715 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
716 if (!ref2_is_decl)
717 return (base2_alias_set == ref1_alias_set
718 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
719 return false;
722 /* Return true if two memory references based on the variables BASE1
723 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
724 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
726 static bool
727 decl_refs_may_alias_p (tree base1,
728 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
729 tree base2,
730 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
732 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
734 /* If both references are based on different variables, they cannot alias. */
735 if (base1 != base2)
736 return false;
738 /* If both references are based on the same variable, they cannot alias if
739 the accesses do not overlap. */
740 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
743 /* Return true if an indirect reference based on *PTR1 constrained
744 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
745 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
746 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
747 in which case they are computed on-demand. REF1 and REF2
748 if non-NULL are the complete memory reference trees. */
750 static bool
751 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
752 HOST_WIDE_INT offset1,
753 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
754 alias_set_type ref1_alias_set,
755 alias_set_type base1_alias_set,
756 tree ref2 ATTRIBUTE_UNUSED, tree base2,
757 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
758 alias_set_type ref2_alias_set,
759 alias_set_type base2_alias_set, bool tbaa_p)
761 tree ptr1;
762 tree ptrtype1, dbase2;
763 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
764 HOST_WIDE_INT doffset1, doffset2;
765 double_int moff;
767 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
768 || TREE_CODE (base1) == TARGET_MEM_REF)
769 && DECL_P (base2));
771 ptr1 = TREE_OPERAND (base1, 0);
773 /* The offset embedded in MEM_REFs can be negative. Bias them
774 so that the resulting offset adjustment is positive. */
775 moff = mem_ref_offset (base1);
776 moff = moff.alshift (BITS_PER_UNIT == 8
777 ? 3 : exact_log2 (BITS_PER_UNIT),
778 HOST_BITS_PER_DOUBLE_INT);
779 if (moff.is_negative ())
780 offset2p += (-moff).low;
781 else
782 offset1p += moff.low;
784 /* If only one reference is based on a variable, they cannot alias if
785 the pointer access is beyond the extent of the variable access.
786 (the pointer base cannot validly point to an offset less than zero
787 of the variable).
788 ??? IVOPTs creates bases that do not honor this restriction,
789 so do not apply this optimization for TARGET_MEM_REFs. */
790 if (TREE_CODE (base1) != TARGET_MEM_REF
791 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
792 return false;
793 /* They also cannot alias if the pointer may not point to the decl. */
794 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
795 return false;
797 /* Disambiguations that rely on strict aliasing rules follow. */
798 if (!flag_strict_aliasing || !tbaa_p)
799 return true;
801 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
803 /* If the alias set for a pointer access is zero all bets are off. */
804 if (base1_alias_set == -1)
805 base1_alias_set = get_deref_alias_set (ptrtype1);
806 if (base1_alias_set == 0)
807 return true;
808 if (base2_alias_set == -1)
809 base2_alias_set = get_alias_set (base2);
811 /* When we are trying to disambiguate an access with a pointer dereference
812 as base versus one with a decl as base we can use both the size
813 of the decl and its dynamic type for extra disambiguation.
814 ??? We do not know anything about the dynamic type of the decl
815 other than that its alias-set contains base2_alias_set as a subset
816 which does not help us here. */
817 /* As we know nothing useful about the dynamic type of the decl just
818 use the usual conflict check rather than a subset test.
819 ??? We could introduce -fvery-strict-aliasing when the language
820 does not allow decls to have a dynamic type that differs from their
821 static type. Then we can check
822 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
823 if (base1_alias_set != base2_alias_set
824 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
825 return false;
826 /* If the size of the access relevant for TBAA through the pointer
827 is bigger than the size of the decl we can't possibly access the
828 decl via that pointer. */
829 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
830 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
831 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
832 /* ??? This in turn may run afoul when a decl of type T which is
833 a member of union type U is accessed through a pointer to
834 type U and sizeof T is smaller than sizeof U. */
835 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
836 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
837 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
838 return false;
840 if (!ref2)
841 return true;
843 /* If the decl is accessed via a MEM_REF, reconstruct the base
844 we can use for TBAA and an appropriately adjusted offset. */
845 dbase2 = ref2;
846 while (handled_component_p (dbase2))
847 dbase2 = TREE_OPERAND (dbase2, 0);
848 doffset1 = offset1;
849 doffset2 = offset2;
850 if (TREE_CODE (dbase2) == MEM_REF
851 || TREE_CODE (dbase2) == TARGET_MEM_REF)
853 double_int moff = mem_ref_offset (dbase2);
854 moff = moff.alshift (BITS_PER_UNIT == 8
855 ? 3 : exact_log2 (BITS_PER_UNIT),
856 HOST_BITS_PER_DOUBLE_INT);
857 if (moff.is_negative ())
858 doffset1 -= (-moff).low;
859 else
860 doffset2 -= moff.low;
863 /* If either reference is view-converted, give up now. */
864 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
865 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
866 return true;
868 /* If both references are through the same type, they do not alias
869 if the accesses do not overlap. This does extra disambiguation
870 for mixed/pointer accesses but requires strict aliasing.
871 For MEM_REFs we require that the component-ref offset we computed
872 is relative to the start of the type which we ensure by
873 comparing rvalue and access type and disregarding the constant
874 pointer offset. */
875 if ((TREE_CODE (base1) != TARGET_MEM_REF
876 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
877 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
878 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
880 /* Do access-path based disambiguation. */
881 if (ref1 && ref2
882 && (handled_component_p (ref1) || handled_component_p (ref2)))
883 return aliasing_component_refs_p (ref1,
884 ref1_alias_set, base1_alias_set,
885 offset1, max_size1,
886 ref2,
887 ref2_alias_set, base2_alias_set,
888 offset2, max_size2, true);
890 return true;
893 /* Return true if two indirect references based on *PTR1
894 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
895 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
896 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
897 in which case they are computed on-demand. REF1 and REF2
898 if non-NULL are the complete memory reference trees. */
900 static bool
901 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
902 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
903 alias_set_type ref1_alias_set,
904 alias_set_type base1_alias_set,
905 tree ref2 ATTRIBUTE_UNUSED, tree base2,
906 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
907 alias_set_type ref2_alias_set,
908 alias_set_type base2_alias_set, bool tbaa_p)
910 tree ptr1;
911 tree ptr2;
912 tree ptrtype1, ptrtype2;
914 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
915 || TREE_CODE (base1) == TARGET_MEM_REF)
916 && (TREE_CODE (base2) == MEM_REF
917 || TREE_CODE (base2) == TARGET_MEM_REF));
919 ptr1 = TREE_OPERAND (base1, 0);
920 ptr2 = TREE_OPERAND (base2, 0);
922 /* If both bases are based on pointers they cannot alias if they may not
923 point to the same memory object or if they point to the same object
924 and the accesses do not overlap. */
925 if ((!cfun || gimple_in_ssa_p (cfun))
926 && operand_equal_p (ptr1, ptr2, 0)
927 && (((TREE_CODE (base1) != TARGET_MEM_REF
928 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
929 && (TREE_CODE (base2) != TARGET_MEM_REF
930 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
931 || (TREE_CODE (base1) == TARGET_MEM_REF
932 && TREE_CODE (base2) == TARGET_MEM_REF
933 && (TMR_STEP (base1) == TMR_STEP (base2)
934 || (TMR_STEP (base1) && TMR_STEP (base2)
935 && operand_equal_p (TMR_STEP (base1),
936 TMR_STEP (base2), 0)))
937 && (TMR_INDEX (base1) == TMR_INDEX (base2)
938 || (TMR_INDEX (base1) && TMR_INDEX (base2)
939 && operand_equal_p (TMR_INDEX (base1),
940 TMR_INDEX (base2), 0)))
941 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
942 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
943 && operand_equal_p (TMR_INDEX2 (base1),
944 TMR_INDEX2 (base2), 0))))))
946 double_int moff;
947 /* The offset embedded in MEM_REFs can be negative. Bias them
948 so that the resulting offset adjustment is positive. */
949 moff = mem_ref_offset (base1);
950 moff = moff.alshift (BITS_PER_UNIT == 8
951 ? 3 : exact_log2 (BITS_PER_UNIT),
952 HOST_BITS_PER_DOUBLE_INT);
953 if (moff.is_negative ())
954 offset2 += (-moff).low;
955 else
956 offset1 += moff.low;
957 moff = mem_ref_offset (base2);
958 moff = moff.alshift (BITS_PER_UNIT == 8
959 ? 3 : exact_log2 (BITS_PER_UNIT),
960 HOST_BITS_PER_DOUBLE_INT);
961 if (moff.is_negative ())
962 offset1 += (-moff).low;
963 else
964 offset2 += moff.low;
965 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
967 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
968 return false;
970 /* Disambiguations that rely on strict aliasing rules follow. */
971 if (!flag_strict_aliasing || !tbaa_p)
972 return true;
974 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
975 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
977 /* If the alias set for a pointer access is zero all bets are off. */
978 if (base1_alias_set == -1)
979 base1_alias_set = get_deref_alias_set (ptrtype1);
980 if (base1_alias_set == 0)
981 return true;
982 if (base2_alias_set == -1)
983 base2_alias_set = get_deref_alias_set (ptrtype2);
984 if (base2_alias_set == 0)
985 return true;
987 /* If both references are through the same type, they do not alias
988 if the accesses do not overlap. This does extra disambiguation
989 for mixed/pointer accesses but requires strict aliasing. */
990 if ((TREE_CODE (base1) != TARGET_MEM_REF
991 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
992 && (TREE_CODE (base2) != TARGET_MEM_REF
993 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
994 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
995 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
996 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
997 TREE_TYPE (ptrtype2)) == 1)
998 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1000 /* Do type-based disambiguation. */
1001 if (base1_alias_set != base2_alias_set
1002 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1003 return false;
1005 /* Do access-path based disambiguation. */
1006 if (ref1 && ref2
1007 && (handled_component_p (ref1) || handled_component_p (ref2))
1008 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1009 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
1010 return aliasing_component_refs_p (ref1,
1011 ref1_alias_set, base1_alias_set,
1012 offset1, max_size1,
1013 ref2,
1014 ref2_alias_set, base2_alias_set,
1015 offset2, max_size2, false);
1017 return true;
1020 /* Return true, if the two memory references REF1 and REF2 may alias. */
1022 bool
1023 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1025 tree base1, base2;
1026 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1027 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1028 bool var1_p, var2_p, ind1_p, ind2_p;
1030 gcc_checking_assert ((!ref1->ref
1031 || TREE_CODE (ref1->ref) == SSA_NAME
1032 || DECL_P (ref1->ref)
1033 || TREE_CODE (ref1->ref) == STRING_CST
1034 || handled_component_p (ref1->ref)
1035 || TREE_CODE (ref1->ref) == MEM_REF
1036 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1037 && (!ref2->ref
1038 || TREE_CODE (ref2->ref) == SSA_NAME
1039 || DECL_P (ref2->ref)
1040 || TREE_CODE (ref2->ref) == STRING_CST
1041 || handled_component_p (ref2->ref)
1042 || TREE_CODE (ref2->ref) == MEM_REF
1043 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1045 /* Decompose the references into their base objects and the access. */
1046 base1 = ao_ref_base (ref1);
1047 offset1 = ref1->offset;
1048 max_size1 = ref1->max_size;
1049 base2 = ao_ref_base (ref2);
1050 offset2 = ref2->offset;
1051 max_size2 = ref2->max_size;
1053 /* We can end up with registers or constants as bases for example from
1054 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1055 which is seen as a struct copy. */
1056 if (TREE_CODE (base1) == SSA_NAME
1057 || TREE_CODE (base1) == CONST_DECL
1058 || TREE_CODE (base1) == CONSTRUCTOR
1059 || TREE_CODE (base1) == ADDR_EXPR
1060 || CONSTANT_CLASS_P (base1)
1061 || TREE_CODE (base2) == SSA_NAME
1062 || TREE_CODE (base2) == CONST_DECL
1063 || TREE_CODE (base2) == CONSTRUCTOR
1064 || TREE_CODE (base2) == ADDR_EXPR
1065 || CONSTANT_CLASS_P (base2))
1066 return false;
1068 /* We can end up referring to code via function and label decls.
1069 As we likely do not properly track code aliases conservatively
1070 bail out. */
1071 if (TREE_CODE (base1) == FUNCTION_DECL
1072 || TREE_CODE (base1) == LABEL_DECL
1073 || TREE_CODE (base2) == FUNCTION_DECL
1074 || TREE_CODE (base2) == LABEL_DECL)
1075 return true;
1077 /* Two volatile accesses always conflict. */
1078 if (ref1->volatile_p
1079 && ref2->volatile_p)
1080 return true;
1082 /* Defer to simple offset based disambiguation if we have
1083 references based on two decls. Do this before defering to
1084 TBAA to handle must-alias cases in conformance with the
1085 GCC extension of allowing type-punning through unions. */
1086 var1_p = DECL_P (base1);
1087 var2_p = DECL_P (base2);
1088 if (var1_p && var2_p)
1089 return decl_refs_may_alias_p (base1, offset1, max_size1,
1090 base2, offset2, max_size2);
1092 ind1_p = (TREE_CODE (base1) == MEM_REF
1093 || TREE_CODE (base1) == TARGET_MEM_REF);
1094 ind2_p = (TREE_CODE (base2) == MEM_REF
1095 || TREE_CODE (base2) == TARGET_MEM_REF);
1097 /* Canonicalize the pointer-vs-decl case. */
1098 if (ind1_p && var2_p)
1100 HOST_WIDE_INT tmp1;
1101 tree tmp2;
1102 ao_ref *tmp3;
1103 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1104 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1105 tmp2 = base1; base1 = base2; base2 = tmp2;
1106 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1107 var1_p = true;
1108 ind1_p = false;
1109 var2_p = false;
1110 ind2_p = true;
1113 /* First defer to TBAA if possible. */
1114 if (tbaa_p
1115 && flag_strict_aliasing
1116 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1117 ao_ref_alias_set (ref2)))
1118 return false;
1120 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1121 if (var1_p && ind2_p)
1122 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1123 offset2, max_size2,
1124 ao_ref_alias_set (ref2), -1,
1125 ref1->ref, base1,
1126 offset1, max_size1,
1127 ao_ref_alias_set (ref1),
1128 ao_ref_base_alias_set (ref1),
1129 tbaa_p);
1130 else if (ind1_p && ind2_p)
1131 return indirect_refs_may_alias_p (ref1->ref, base1,
1132 offset1, max_size1,
1133 ao_ref_alias_set (ref1), -1,
1134 ref2->ref, base2,
1135 offset2, max_size2,
1136 ao_ref_alias_set (ref2), -1,
1137 tbaa_p);
1139 /* We really do not want to end up here, but returning true is safe. */
1140 #ifdef ENABLE_CHECKING
1141 gcc_unreachable ();
1142 #else
1143 return true;
1144 #endif
1147 bool
1148 refs_may_alias_p (tree ref1, tree ref2)
1150 ao_ref r1, r2;
1151 bool res;
1152 ao_ref_init (&r1, ref1);
1153 ao_ref_init (&r2, ref2);
1154 res = refs_may_alias_p_1 (&r1, &r2, true);
1155 if (res)
1156 ++alias_stats.refs_may_alias_p_may_alias;
1157 else
1158 ++alias_stats.refs_may_alias_p_no_alias;
1159 return res;
1162 /* Returns true if there is a anti-dependence for the STORE that
1163 executes after the LOAD. */
1165 bool
1166 refs_anti_dependent_p (tree load, tree store)
1168 ao_ref r1, r2;
1169 ao_ref_init (&r1, load);
1170 ao_ref_init (&r2, store);
1171 return refs_may_alias_p_1 (&r1, &r2, false);
1174 /* Returns true if there is a output dependence for the stores
1175 STORE1 and STORE2. */
1177 bool
1178 refs_output_dependent_p (tree store1, tree store2)
1180 ao_ref r1, r2;
1181 ao_ref_init (&r1, store1);
1182 ao_ref_init (&r2, store2);
1183 return refs_may_alias_p_1 (&r1, &r2, false);
1186 /* If the call CALL may use the memory reference REF return true,
1187 otherwise return false. */
1189 static bool
1190 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1192 tree base, callee;
1193 unsigned i;
1194 int flags = gimple_call_flags (call);
1196 /* Const functions without a static chain do not implicitly use memory. */
1197 if (!gimple_call_chain (call)
1198 && (flags & (ECF_CONST|ECF_NOVOPS)))
1199 goto process_args;
1201 base = ao_ref_base (ref);
1202 if (!base)
1203 return true;
1205 /* A call that is not without side-effects might involve volatile
1206 accesses and thus conflicts with all other volatile accesses. */
1207 if (ref->volatile_p)
1208 return true;
1210 /* If the reference is based on a decl that is not aliased the call
1211 cannot possibly use it. */
1212 if (DECL_P (base)
1213 && !may_be_aliased (base)
1214 /* But local statics can be used through recursion. */
1215 && !is_global_var (base))
1216 goto process_args;
1218 callee = gimple_call_fndecl (call);
1220 /* Handle those builtin functions explicitly that do not act as
1221 escape points. See tree-ssa-structalias.c:find_func_aliases
1222 for the list of builtins we might need to handle here. */
1223 if (callee != NULL_TREE
1224 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1225 switch (DECL_FUNCTION_CODE (callee))
1227 /* All the following functions read memory pointed to by
1228 their second argument. strcat/strncat additionally
1229 reads memory pointed to by the first argument. */
1230 case BUILT_IN_STRCAT:
1231 case BUILT_IN_STRNCAT:
1233 ao_ref dref;
1234 ao_ref_init_from_ptr_and_size (&dref,
1235 gimple_call_arg (call, 0),
1236 NULL_TREE);
1237 if (refs_may_alias_p_1 (&dref, ref, false))
1238 return true;
1240 /* FALLTHRU */
1241 case BUILT_IN_STRCPY:
1242 case BUILT_IN_STRNCPY:
1243 case BUILT_IN_MEMCPY:
1244 case BUILT_IN_MEMMOVE:
1245 case BUILT_IN_MEMPCPY:
1246 case BUILT_IN_STPCPY:
1247 case BUILT_IN_STPNCPY:
1248 case BUILT_IN_TM_MEMCPY:
1249 case BUILT_IN_TM_MEMMOVE:
1251 ao_ref dref;
1252 tree size = NULL_TREE;
1253 if (gimple_call_num_args (call) == 3)
1254 size = gimple_call_arg (call, 2);
1255 ao_ref_init_from_ptr_and_size (&dref,
1256 gimple_call_arg (call, 1),
1257 size);
1258 return refs_may_alias_p_1 (&dref, ref, false);
1260 case BUILT_IN_STRCAT_CHK:
1261 case BUILT_IN_STRNCAT_CHK:
1263 ao_ref dref;
1264 ao_ref_init_from_ptr_and_size (&dref,
1265 gimple_call_arg (call, 0),
1266 NULL_TREE);
1267 if (refs_may_alias_p_1 (&dref, ref, false))
1268 return true;
1270 /* FALLTHRU */
1271 case BUILT_IN_STRCPY_CHK:
1272 case BUILT_IN_STRNCPY_CHK:
1273 case BUILT_IN_MEMCPY_CHK:
1274 case BUILT_IN_MEMMOVE_CHK:
1275 case BUILT_IN_MEMPCPY_CHK:
1276 case BUILT_IN_STPCPY_CHK:
1277 case BUILT_IN_STPNCPY_CHK:
1279 ao_ref dref;
1280 tree size = NULL_TREE;
1281 if (gimple_call_num_args (call) == 4)
1282 size = gimple_call_arg (call, 2);
1283 ao_ref_init_from_ptr_and_size (&dref,
1284 gimple_call_arg (call, 1),
1285 size);
1286 return refs_may_alias_p_1 (&dref, ref, false);
1288 case BUILT_IN_BCOPY:
1290 ao_ref dref;
1291 tree size = gimple_call_arg (call, 2);
1292 ao_ref_init_from_ptr_and_size (&dref,
1293 gimple_call_arg (call, 0),
1294 size);
1295 return refs_may_alias_p_1 (&dref, ref, false);
1298 /* The following functions read memory pointed to by their
1299 first argument. */
1300 CASE_BUILT_IN_TM_LOAD (1):
1301 CASE_BUILT_IN_TM_LOAD (2):
1302 CASE_BUILT_IN_TM_LOAD (4):
1303 CASE_BUILT_IN_TM_LOAD (8):
1304 CASE_BUILT_IN_TM_LOAD (FLOAT):
1305 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1306 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1307 CASE_BUILT_IN_TM_LOAD (M64):
1308 CASE_BUILT_IN_TM_LOAD (M128):
1309 CASE_BUILT_IN_TM_LOAD (M256):
1310 case BUILT_IN_TM_LOG:
1311 case BUILT_IN_TM_LOG_1:
1312 case BUILT_IN_TM_LOG_2:
1313 case BUILT_IN_TM_LOG_4:
1314 case BUILT_IN_TM_LOG_8:
1315 case BUILT_IN_TM_LOG_FLOAT:
1316 case BUILT_IN_TM_LOG_DOUBLE:
1317 case BUILT_IN_TM_LOG_LDOUBLE:
1318 case BUILT_IN_TM_LOG_M64:
1319 case BUILT_IN_TM_LOG_M128:
1320 case BUILT_IN_TM_LOG_M256:
1321 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1323 /* These read memory pointed to by the first argument. */
1324 case BUILT_IN_STRDUP:
1325 case BUILT_IN_STRNDUP:
1327 ao_ref dref;
1328 tree size = NULL_TREE;
1329 if (gimple_call_num_args (call) == 2)
1330 size = gimple_call_arg (call, 1);
1331 ao_ref_init_from_ptr_and_size (&dref,
1332 gimple_call_arg (call, 0),
1333 size);
1334 return refs_may_alias_p_1 (&dref, ref, false);
1336 /* These read memory pointed to by the first argument. */
1337 case BUILT_IN_INDEX:
1338 case BUILT_IN_STRCHR:
1339 case BUILT_IN_STRRCHR:
1341 ao_ref dref;
1342 ao_ref_init_from_ptr_and_size (&dref,
1343 gimple_call_arg (call, 0),
1344 NULL_TREE);
1345 return refs_may_alias_p_1 (&dref, ref, false);
1347 /* These read memory pointed to by the first argument with size
1348 in the third argument. */
1349 case BUILT_IN_MEMCHR:
1351 ao_ref dref;
1352 ao_ref_init_from_ptr_and_size (&dref,
1353 gimple_call_arg (call, 0),
1354 gimple_call_arg (call, 2));
1355 return refs_may_alias_p_1 (&dref, ref, false);
1357 /* These read memory pointed to by the first and second arguments. */
1358 case BUILT_IN_STRSTR:
1359 case BUILT_IN_STRPBRK:
1361 ao_ref dref;
1362 ao_ref_init_from_ptr_and_size (&dref,
1363 gimple_call_arg (call, 0),
1364 NULL_TREE);
1365 if (refs_may_alias_p_1 (&dref, ref, false))
1366 return true;
1367 ao_ref_init_from_ptr_and_size (&dref,
1368 gimple_call_arg (call, 1),
1369 NULL_TREE);
1370 return refs_may_alias_p_1 (&dref, ref, false);
1373 /* The following builtins do not read from memory. */
1374 case BUILT_IN_FREE:
1375 case BUILT_IN_MALLOC:
1376 case BUILT_IN_CALLOC:
1377 case BUILT_IN_ALLOCA:
1378 case BUILT_IN_ALLOCA_WITH_ALIGN:
1379 case BUILT_IN_STACK_SAVE:
1380 case BUILT_IN_STACK_RESTORE:
1381 case BUILT_IN_MEMSET:
1382 case BUILT_IN_TM_MEMSET:
1383 case BUILT_IN_MEMSET_CHK:
1384 case BUILT_IN_FREXP:
1385 case BUILT_IN_FREXPF:
1386 case BUILT_IN_FREXPL:
1387 case BUILT_IN_GAMMA_R:
1388 case BUILT_IN_GAMMAF_R:
1389 case BUILT_IN_GAMMAL_R:
1390 case BUILT_IN_LGAMMA_R:
1391 case BUILT_IN_LGAMMAF_R:
1392 case BUILT_IN_LGAMMAL_R:
1393 case BUILT_IN_MODF:
1394 case BUILT_IN_MODFF:
1395 case BUILT_IN_MODFL:
1396 case BUILT_IN_REMQUO:
1397 case BUILT_IN_REMQUOF:
1398 case BUILT_IN_REMQUOL:
1399 case BUILT_IN_SINCOS:
1400 case BUILT_IN_SINCOSF:
1401 case BUILT_IN_SINCOSL:
1402 case BUILT_IN_ASSUME_ALIGNED:
1403 case BUILT_IN_VA_END:
1404 return false;
1405 /* __sync_* builtins and some OpenMP builtins act as threading
1406 barriers. */
1407 #undef DEF_SYNC_BUILTIN
1408 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1409 #include "sync-builtins.def"
1410 #undef DEF_SYNC_BUILTIN
1411 case BUILT_IN_GOMP_ATOMIC_START:
1412 case BUILT_IN_GOMP_ATOMIC_END:
1413 case BUILT_IN_GOMP_BARRIER:
1414 case BUILT_IN_GOMP_TASKWAIT:
1415 case BUILT_IN_GOMP_CRITICAL_START:
1416 case BUILT_IN_GOMP_CRITICAL_END:
1417 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1418 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1419 case BUILT_IN_GOMP_LOOP_END:
1420 case BUILT_IN_GOMP_ORDERED_START:
1421 case BUILT_IN_GOMP_ORDERED_END:
1422 case BUILT_IN_GOMP_PARALLEL_END:
1423 case BUILT_IN_GOMP_SECTIONS_END:
1424 case BUILT_IN_GOMP_SINGLE_COPY_START:
1425 case BUILT_IN_GOMP_SINGLE_COPY_END:
1426 return true;
1428 default:
1429 /* Fallthru to general call handling. */;
1432 /* Check if base is a global static variable that is not read
1433 by the function. */
1434 if (callee != NULL_TREE
1435 && TREE_CODE (base) == VAR_DECL
1436 && TREE_STATIC (base))
1438 struct cgraph_node *node = cgraph_get_node (callee);
1439 bitmap not_read;
1441 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1442 node yet. We should enforce that there are nodes for all decls in the
1443 IL and remove this check instead. */
1444 if (node
1445 && (not_read = ipa_reference_get_not_read_global (node))
1446 && bitmap_bit_p (not_read, DECL_UID (base)))
1447 goto process_args;
1450 /* Check if the base variable is call-used. */
1451 if (DECL_P (base))
1453 if (pt_solution_includes (gimple_call_use_set (call), base))
1454 return true;
1456 else if ((TREE_CODE (base) == MEM_REF
1457 || TREE_CODE (base) == TARGET_MEM_REF)
1458 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1460 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1461 if (!pi)
1462 return true;
1464 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1465 return true;
1467 else
1468 return true;
1470 /* Inspect call arguments for passed-by-value aliases. */
1471 process_args:
1472 for (i = 0; i < gimple_call_num_args (call); ++i)
1474 tree op = gimple_call_arg (call, i);
1475 int flags = gimple_call_arg_flags (call, i);
1477 if (flags & EAF_UNUSED)
1478 continue;
1480 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1481 op = TREE_OPERAND (op, 0);
1483 if (TREE_CODE (op) != SSA_NAME
1484 && !is_gimple_min_invariant (op))
1486 ao_ref r;
1487 ao_ref_init (&r, op);
1488 if (refs_may_alias_p_1 (&r, ref, true))
1489 return true;
1493 return false;
1496 static bool
1497 ref_maybe_used_by_call_p (gimple call, tree ref)
1499 ao_ref r;
1500 bool res;
1501 ao_ref_init (&r, ref);
1502 res = ref_maybe_used_by_call_p_1 (call, &r);
1503 if (res)
1504 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1505 else
1506 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1507 return res;
1511 /* If the statement STMT may use the memory reference REF return
1512 true, otherwise return false. */
1514 bool
1515 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1517 if (is_gimple_assign (stmt))
1519 tree rhs;
1521 /* All memory assign statements are single. */
1522 if (!gimple_assign_single_p (stmt))
1523 return false;
1525 rhs = gimple_assign_rhs1 (stmt);
1526 if (is_gimple_reg (rhs)
1527 || is_gimple_min_invariant (rhs)
1528 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1529 return false;
1531 return refs_may_alias_p (rhs, ref);
1533 else if (is_gimple_call (stmt))
1534 return ref_maybe_used_by_call_p (stmt, ref);
1535 else if (gimple_code (stmt) == GIMPLE_RETURN)
1537 tree retval = gimple_return_retval (stmt);
1538 tree base;
1539 if (retval
1540 && TREE_CODE (retval) != SSA_NAME
1541 && !is_gimple_min_invariant (retval)
1542 && refs_may_alias_p (retval, ref))
1543 return true;
1544 /* If ref escapes the function then the return acts as a use. */
1545 base = get_base_address (ref);
1546 if (!base)
1548 else if (DECL_P (base))
1549 return is_global_var (base);
1550 else if (TREE_CODE (base) == MEM_REF
1551 || TREE_CODE (base) == TARGET_MEM_REF)
1552 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1553 return false;
1556 return true;
1559 /* If the call in statement CALL may clobber the memory reference REF
1560 return true, otherwise return false. */
1562 static bool
1563 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1565 tree base;
1566 tree callee;
1568 /* If the call is pure or const it cannot clobber anything. */
1569 if (gimple_call_flags (call)
1570 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1571 return false;
1573 base = ao_ref_base (ref);
1574 if (!base)
1575 return true;
1577 if (TREE_CODE (base) == SSA_NAME
1578 || CONSTANT_CLASS_P (base))
1579 return false;
1581 /* A call that is not without side-effects might involve volatile
1582 accesses and thus conflicts with all other volatile accesses. */
1583 if (ref->volatile_p)
1584 return true;
1586 /* If the reference is based on a decl that is not aliased the call
1587 cannot possibly clobber it. */
1588 if (DECL_P (base)
1589 && !may_be_aliased (base)
1590 /* But local non-readonly statics can be modified through recursion
1591 or the call may implement a threading barrier which we must
1592 treat as may-def. */
1593 && (TREE_READONLY (base)
1594 || !is_global_var (base)))
1595 return false;
1597 callee = gimple_call_fndecl (call);
1599 /* Handle those builtin functions explicitly that do not act as
1600 escape points. See tree-ssa-structalias.c:find_func_aliases
1601 for the list of builtins we might need to handle here. */
1602 if (callee != NULL_TREE
1603 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1604 switch (DECL_FUNCTION_CODE (callee))
1606 /* All the following functions clobber memory pointed to by
1607 their first argument. */
1608 case BUILT_IN_STRCPY:
1609 case BUILT_IN_STRNCPY:
1610 case BUILT_IN_MEMCPY:
1611 case BUILT_IN_MEMMOVE:
1612 case BUILT_IN_MEMPCPY:
1613 case BUILT_IN_STPCPY:
1614 case BUILT_IN_STPNCPY:
1615 case BUILT_IN_STRCAT:
1616 case BUILT_IN_STRNCAT:
1617 case BUILT_IN_MEMSET:
1618 case BUILT_IN_TM_MEMSET:
1619 CASE_BUILT_IN_TM_STORE (1):
1620 CASE_BUILT_IN_TM_STORE (2):
1621 CASE_BUILT_IN_TM_STORE (4):
1622 CASE_BUILT_IN_TM_STORE (8):
1623 CASE_BUILT_IN_TM_STORE (FLOAT):
1624 CASE_BUILT_IN_TM_STORE (DOUBLE):
1625 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1626 CASE_BUILT_IN_TM_STORE (M64):
1627 CASE_BUILT_IN_TM_STORE (M128):
1628 CASE_BUILT_IN_TM_STORE (M256):
1629 case BUILT_IN_TM_MEMCPY:
1630 case BUILT_IN_TM_MEMMOVE:
1632 ao_ref dref;
1633 tree size = NULL_TREE;
1634 /* Don't pass in size for strncat, as the maximum size
1635 is strlen (dest) + n + 1 instead of n, resp.
1636 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1637 known. */
1638 if (gimple_call_num_args (call) == 3
1639 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1640 size = gimple_call_arg (call, 2);
1641 ao_ref_init_from_ptr_and_size (&dref,
1642 gimple_call_arg (call, 0),
1643 size);
1644 return refs_may_alias_p_1 (&dref, ref, false);
1646 case BUILT_IN_STRCPY_CHK:
1647 case BUILT_IN_STRNCPY_CHK:
1648 case BUILT_IN_MEMCPY_CHK:
1649 case BUILT_IN_MEMMOVE_CHK:
1650 case BUILT_IN_MEMPCPY_CHK:
1651 case BUILT_IN_STPCPY_CHK:
1652 case BUILT_IN_STPNCPY_CHK:
1653 case BUILT_IN_STRCAT_CHK:
1654 case BUILT_IN_STRNCAT_CHK:
1655 case BUILT_IN_MEMSET_CHK:
1657 ao_ref dref;
1658 tree size = NULL_TREE;
1659 /* Don't pass in size for __strncat_chk, as the maximum size
1660 is strlen (dest) + n + 1 instead of n, resp.
1661 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1662 known. */
1663 if (gimple_call_num_args (call) == 4
1664 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1665 size = gimple_call_arg (call, 2);
1666 ao_ref_init_from_ptr_and_size (&dref,
1667 gimple_call_arg (call, 0),
1668 size);
1669 return refs_may_alias_p_1 (&dref, ref, false);
1671 case BUILT_IN_BCOPY:
1673 ao_ref dref;
1674 tree size = gimple_call_arg (call, 2);
1675 ao_ref_init_from_ptr_and_size (&dref,
1676 gimple_call_arg (call, 1),
1677 size);
1678 return refs_may_alias_p_1 (&dref, ref, false);
1680 /* Allocating memory does not have any side-effects apart from
1681 being the definition point for the pointer. */
1682 case BUILT_IN_MALLOC:
1683 case BUILT_IN_CALLOC:
1684 case BUILT_IN_STRDUP:
1685 case BUILT_IN_STRNDUP:
1686 /* Unix98 specifies that errno is set on allocation failure. */
1687 if (flag_errno_math
1688 && targetm.ref_may_alias_errno (ref))
1689 return true;
1690 return false;
1691 case BUILT_IN_STACK_SAVE:
1692 case BUILT_IN_ALLOCA:
1693 case BUILT_IN_ALLOCA_WITH_ALIGN:
1694 case BUILT_IN_ASSUME_ALIGNED:
1695 return false;
1696 /* Freeing memory kills the pointed-to memory. More importantly
1697 the call has to serve as a barrier for moving loads and stores
1698 across it. */
1699 case BUILT_IN_FREE:
1700 case BUILT_IN_VA_END:
1702 tree ptr = gimple_call_arg (call, 0);
1703 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1705 case BUILT_IN_GAMMA_R:
1706 case BUILT_IN_GAMMAF_R:
1707 case BUILT_IN_GAMMAL_R:
1708 case BUILT_IN_LGAMMA_R:
1709 case BUILT_IN_LGAMMAF_R:
1710 case BUILT_IN_LGAMMAL_R:
1712 tree out = gimple_call_arg (call, 1);
1713 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1714 return true;
1715 if (flag_errno_math)
1716 break;
1717 return false;
1719 case BUILT_IN_FREXP:
1720 case BUILT_IN_FREXPF:
1721 case BUILT_IN_FREXPL:
1722 case BUILT_IN_MODF:
1723 case BUILT_IN_MODFF:
1724 case BUILT_IN_MODFL:
1726 tree out = gimple_call_arg (call, 1);
1727 return ptr_deref_may_alias_ref_p_1 (out, ref);
1729 case BUILT_IN_REMQUO:
1730 case BUILT_IN_REMQUOF:
1731 case BUILT_IN_REMQUOL:
1733 tree out = gimple_call_arg (call, 2);
1734 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1735 return true;
1736 if (flag_errno_math)
1737 break;
1738 return false;
1740 case BUILT_IN_SINCOS:
1741 case BUILT_IN_SINCOSF:
1742 case BUILT_IN_SINCOSL:
1744 tree sin = gimple_call_arg (call, 1);
1745 tree cos = gimple_call_arg (call, 2);
1746 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1747 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1749 /* __sync_* builtins and some OpenMP builtins act as threading
1750 barriers. */
1751 #undef DEF_SYNC_BUILTIN
1752 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1753 #include "sync-builtins.def"
1754 #undef DEF_SYNC_BUILTIN
1755 case BUILT_IN_GOMP_ATOMIC_START:
1756 case BUILT_IN_GOMP_ATOMIC_END:
1757 case BUILT_IN_GOMP_BARRIER:
1758 case BUILT_IN_GOMP_TASKWAIT:
1759 case BUILT_IN_GOMP_CRITICAL_START:
1760 case BUILT_IN_GOMP_CRITICAL_END:
1761 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1762 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1763 case BUILT_IN_GOMP_LOOP_END:
1764 case BUILT_IN_GOMP_ORDERED_START:
1765 case BUILT_IN_GOMP_ORDERED_END:
1766 case BUILT_IN_GOMP_PARALLEL_END:
1767 case BUILT_IN_GOMP_SECTIONS_END:
1768 case BUILT_IN_GOMP_SINGLE_COPY_START:
1769 case BUILT_IN_GOMP_SINGLE_COPY_END:
1770 return true;
1771 default:
1772 /* Fallthru to general call handling. */;
1775 /* Check if base is a global static variable that is not written
1776 by the function. */
1777 if (callee != NULL_TREE
1778 && TREE_CODE (base) == VAR_DECL
1779 && TREE_STATIC (base))
1781 struct cgraph_node *node = cgraph_get_node (callee);
1782 bitmap not_written;
1784 if (node
1785 && (not_written = ipa_reference_get_not_written_global (node))
1786 && bitmap_bit_p (not_written, DECL_UID (base)))
1787 return false;
1790 /* Check if the base variable is call-clobbered. */
1791 if (DECL_P (base))
1792 return pt_solution_includes (gimple_call_clobber_set (call), base);
1793 else if ((TREE_CODE (base) == MEM_REF
1794 || TREE_CODE (base) == TARGET_MEM_REF)
1795 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1797 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1798 if (!pi)
1799 return true;
1801 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1804 return true;
1807 /* If the call in statement CALL may clobber the memory reference REF
1808 return true, otherwise return false. */
1810 bool
1811 call_may_clobber_ref_p (gimple call, tree ref)
1813 bool res;
1814 ao_ref r;
1815 ao_ref_init (&r, ref);
1816 res = call_may_clobber_ref_p_1 (call, &r);
1817 if (res)
1818 ++alias_stats.call_may_clobber_ref_p_may_alias;
1819 else
1820 ++alias_stats.call_may_clobber_ref_p_no_alias;
1821 return res;
1825 /* If the statement STMT may clobber the memory reference REF return true,
1826 otherwise return false. */
1828 bool
1829 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1831 if (is_gimple_call (stmt))
1833 tree lhs = gimple_call_lhs (stmt);
1834 if (lhs
1835 && TREE_CODE (lhs) != SSA_NAME)
1837 ao_ref r;
1838 ao_ref_init (&r, lhs);
1839 if (refs_may_alias_p_1 (ref, &r, true))
1840 return true;
1843 return call_may_clobber_ref_p_1 (stmt, ref);
1845 else if (gimple_assign_single_p (stmt))
1847 tree lhs = gimple_assign_lhs (stmt);
1848 if (TREE_CODE (lhs) != SSA_NAME)
1850 ao_ref r;
1851 ao_ref_init (&r, lhs);
1852 return refs_may_alias_p_1 (ref, &r, true);
1855 else if (gimple_code (stmt) == GIMPLE_ASM)
1856 return true;
1858 return false;
1861 bool
1862 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1864 ao_ref r;
1865 ao_ref_init (&r, ref);
1866 return stmt_may_clobber_ref_p_1 (stmt, &r);
1869 /* If STMT kills the memory reference REF return true, otherwise
1870 return false. */
1872 static bool
1873 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1875 /* For a must-alias check we need to be able to constrain
1876 the access properly. */
1877 ao_ref_base (ref);
1878 if (ref->max_size == -1)
1879 return false;
1881 if (gimple_has_lhs (stmt)
1882 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1883 /* The assignment is not necessarily carried out if it can throw
1884 and we can catch it in the current function where we could inspect
1885 the previous value.
1886 ??? We only need to care about the RHS throwing. For aggregate
1887 assignments or similar calls and non-call exceptions the LHS
1888 might throw as well. */
1889 && !stmt_can_throw_internal (stmt))
1891 tree base, lhs = gimple_get_lhs (stmt);
1892 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
1893 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1894 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1895 so base == ref->base does not always hold. */
1896 if (base != ref->base)
1898 /* If both base and ref->base are MEM_REFs, only compare the
1899 first operand, and if the second operand isn't equal constant,
1900 try to add the offsets into offset and ref_offset. */
1901 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
1902 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
1904 if (!tree_int_cst_equal (TREE_OPERAND (base, 0),
1905 TREE_OPERAND (ref->base, 0)))
1907 double_int off1 = mem_ref_offset (base);
1908 off1 = off1.alshift (BITS_PER_UNIT == 8
1909 ? 3 : exact_log2 (BITS_PER_UNIT),
1910 HOST_BITS_PER_DOUBLE_INT);
1911 off1 = off1 + double_int::from_shwi (offset);
1912 double_int off2 = mem_ref_offset (ref->base);
1913 off2 = off2.alshift (BITS_PER_UNIT == 8
1914 ? 3 : exact_log2 (BITS_PER_UNIT),
1915 HOST_BITS_PER_DOUBLE_INT);
1916 off2 = off2 + double_int::from_shwi (ref_offset);
1917 if (off1.fits_shwi () && off2.fits_shwi ())
1919 offset = off1.to_shwi ();
1920 ref_offset = off2.to_shwi ();
1922 else
1923 size = -1;
1926 else
1927 size = -1;
1929 /* For a must-alias check we need to be able to constrain
1930 the access properly. */
1931 if (size != -1 && size == max_size)
1933 if (offset <= ref_offset
1934 && offset + size >= ref_offset + ref->max_size)
1935 return true;
1939 if (is_gimple_call (stmt))
1941 tree callee = gimple_call_fndecl (stmt);
1942 if (callee != NULL_TREE
1943 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1944 switch (DECL_FUNCTION_CODE (callee))
1946 case BUILT_IN_MEMCPY:
1947 case BUILT_IN_MEMPCPY:
1948 case BUILT_IN_MEMMOVE:
1949 case BUILT_IN_MEMSET:
1950 case BUILT_IN_MEMCPY_CHK:
1951 case BUILT_IN_MEMPCPY_CHK:
1952 case BUILT_IN_MEMMOVE_CHK:
1953 case BUILT_IN_MEMSET_CHK:
1955 tree dest = gimple_call_arg (stmt, 0);
1956 tree len = gimple_call_arg (stmt, 2);
1957 tree base = NULL_TREE;
1958 HOST_WIDE_INT offset = 0;
1959 if (!host_integerp (len, 0))
1960 return false;
1961 if (TREE_CODE (dest) == ADDR_EXPR)
1962 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1963 &offset);
1964 else if (TREE_CODE (dest) == SSA_NAME)
1965 base = dest;
1966 if (base
1967 && base == ao_ref_base (ref))
1969 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1970 if (offset <= ref->offset / BITS_PER_UNIT
1971 && (offset + size
1972 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1973 / BITS_PER_UNIT)))
1974 return true;
1976 break;
1979 case BUILT_IN_VA_END:
1981 tree ptr = gimple_call_arg (stmt, 0);
1982 if (TREE_CODE (ptr) == ADDR_EXPR)
1984 tree base = ao_ref_base (ref);
1985 if (TREE_OPERAND (ptr, 0) == base)
1986 return true;
1988 break;
1991 default:;
1994 return false;
1997 bool
1998 stmt_kills_ref_p (gimple stmt, tree ref)
2000 ao_ref r;
2001 ao_ref_init (&r, ref);
2002 return stmt_kills_ref_p_1 (stmt, &r);
2006 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2007 TARGET or a statement clobbering the memory reference REF in which
2008 case false is returned. The walk starts with VUSE, one argument of PHI. */
2010 static bool
2011 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2012 tree vuse, unsigned int *cnt, bitmap *visited,
2013 bool abort_on_visited)
2015 basic_block bb = gimple_bb (phi);
2017 if (!*visited)
2018 *visited = BITMAP_ALLOC (NULL);
2020 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2022 /* Walk until we hit the target. */
2023 while (vuse != target)
2025 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2026 /* Recurse for PHI nodes. */
2027 if (gimple_code (def_stmt) == GIMPLE_PHI)
2029 /* An already visited PHI node ends the walk successfully. */
2030 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2031 return !abort_on_visited;
2032 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2033 visited, abort_on_visited);
2034 if (!vuse)
2035 return false;
2036 continue;
2038 else if (gimple_nop_p (def_stmt))
2039 return false;
2040 else
2042 /* A clobbering statement or the end of the IL ends it failing. */
2043 ++*cnt;
2044 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2045 return false;
2047 /* If we reach a new basic-block see if we already skipped it
2048 in a previous walk that ended successfully. */
2049 if (gimple_bb (def_stmt) != bb)
2051 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2052 return !abort_on_visited;
2053 bb = gimple_bb (def_stmt);
2055 vuse = gimple_vuse (def_stmt);
2057 return true;
2060 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2061 until we hit the phi argument definition that dominates the other one.
2062 Return that, or NULL_TREE if there is no such definition. */
2064 static tree
2065 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2066 ao_ref *ref, unsigned int *cnt,
2067 bitmap *visited, bool abort_on_visited)
2069 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2070 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2071 tree common_vuse;
2073 if (arg0 == arg1)
2074 return arg0;
2075 else if (gimple_nop_p (def0)
2076 || (!gimple_nop_p (def1)
2077 && dominated_by_p (CDI_DOMINATORS,
2078 gimple_bb (def1), gimple_bb (def0))))
2080 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2081 visited, abort_on_visited))
2082 return arg0;
2084 else if (gimple_nop_p (def1)
2085 || dominated_by_p (CDI_DOMINATORS,
2086 gimple_bb (def0), gimple_bb (def1)))
2088 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2089 visited, abort_on_visited))
2090 return arg1;
2092 /* Special case of a diamond:
2093 MEM_1 = ...
2094 goto (cond) ? L1 : L2
2095 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2096 goto L3
2097 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2098 L3: MEM_4 = PHI<MEM_2, MEM_3>
2099 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2100 dominate each other, but still we can easily skip this PHI node
2101 if we recognize that the vuse MEM operand is the same for both,
2102 and that we can skip both statements (they don't clobber us).
2103 This is still linear. Don't use maybe_skip_until, that might
2104 potentially be slow. */
2105 else if ((common_vuse = gimple_vuse (def0))
2106 && common_vuse == gimple_vuse (def1))
2108 *cnt += 2;
2109 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2110 && !stmt_may_clobber_ref_p_1 (def1, ref))
2111 return common_vuse;
2114 return NULL_TREE;
2118 /* Starting from a PHI node for the virtual operand of the memory reference
2119 REF find a continuation virtual operand that allows to continue walking
2120 statements dominating PHI skipping only statements that cannot possibly
2121 clobber REF. Increments *CNT for each alias disambiguation done.
2122 Returns NULL_TREE if no suitable virtual operand can be found. */
2124 tree
2125 get_continuation_for_phi (gimple phi, ao_ref *ref,
2126 unsigned int *cnt, bitmap *visited,
2127 bool abort_on_visited)
2129 unsigned nargs = gimple_phi_num_args (phi);
2131 /* Through a single-argument PHI we can simply look through. */
2132 if (nargs == 1)
2133 return PHI_ARG_DEF (phi, 0);
2135 /* For two or more arguments try to pairwise skip non-aliasing code
2136 until we hit the phi argument definition that dominates the other one. */
2137 else if (nargs >= 2)
2139 tree arg0, arg1;
2140 unsigned i;
2142 /* Find a candidate for the virtual operand which definition
2143 dominates those of all others. */
2144 arg0 = PHI_ARG_DEF (phi, 0);
2145 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2146 for (i = 1; i < nargs; ++i)
2148 arg1 = PHI_ARG_DEF (phi, i);
2149 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2151 arg0 = arg1;
2152 break;
2154 if (dominated_by_p (CDI_DOMINATORS,
2155 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2156 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2157 arg0 = arg1;
2160 /* Then pairwise reduce against the found candidate. */
2161 for (i = 0; i < nargs; ++i)
2163 arg1 = PHI_ARG_DEF (phi, i);
2164 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2165 cnt, visited, abort_on_visited);
2166 if (!arg0)
2167 return NULL_TREE;
2170 return arg0;
2173 return NULL_TREE;
2176 /* Based on the memory reference REF and its virtual use VUSE call
2177 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2178 itself. That is, for each virtual use for which its defining statement
2179 does not clobber REF.
2181 WALKER is called with REF, the current virtual use and DATA. If
2182 WALKER returns non-NULL the walk stops and its result is returned.
2183 At the end of a non-successful walk NULL is returned.
2185 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2186 use which definition is a statement that may clobber REF and DATA.
2187 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2188 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2189 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2190 to adjust REF and *DATA to make that valid.
2192 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2194 void *
2195 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2196 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2197 void *(*translate)(ao_ref *, tree, void *), void *data)
2199 bitmap visited = NULL;
2200 void *res;
2201 unsigned int cnt = 0;
2202 bool translated = false;
2204 timevar_push (TV_ALIAS_STMT_WALK);
2208 gimple def_stmt;
2210 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2211 res = (*walker) (ref, vuse, cnt, data);
2212 /* Abort walk. */
2213 if (res == (void *)-1)
2215 res = NULL;
2216 break;
2218 /* Lookup succeeded. */
2219 else if (res != NULL)
2220 break;
2222 def_stmt = SSA_NAME_DEF_STMT (vuse);
2223 if (gimple_nop_p (def_stmt))
2224 break;
2225 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2226 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2227 &visited, translated);
2228 else
2230 cnt++;
2231 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2233 if (!translate)
2234 break;
2235 res = (*translate) (ref, vuse, data);
2236 /* Failed lookup and translation. */
2237 if (res == (void *)-1)
2239 res = NULL;
2240 break;
2242 /* Lookup succeeded. */
2243 else if (res != NULL)
2244 break;
2245 /* Translation succeeded, continue walking. */
2246 translated = true;
2248 vuse = gimple_vuse (def_stmt);
2251 while (vuse);
2253 if (visited)
2254 BITMAP_FREE (visited);
2256 timevar_pop (TV_ALIAS_STMT_WALK);
2258 return res;
2262 /* Based on the memory reference REF call WALKER for each vdef which
2263 defining statement may clobber REF, starting with VDEF. If REF
2264 is NULL_TREE, each defining statement is visited.
2266 WALKER is called with REF, the current vdef and DATA. If WALKER
2267 returns true the walk is stopped, otherwise it continues.
2269 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2270 PHI argument (but only one walk continues on merge points), the
2271 return value is true if any of the walks was successful.
2273 The function returns the number of statements walked. */
2275 static unsigned int
2276 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2277 bool (*walker)(ao_ref *, tree, void *), void *data,
2278 bitmap *visited, unsigned int cnt)
2282 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2284 if (*visited
2285 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2286 return cnt;
2288 if (gimple_nop_p (def_stmt))
2289 return cnt;
2290 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2292 unsigned i;
2293 if (!*visited)
2294 *visited = BITMAP_ALLOC (NULL);
2295 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2296 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2297 walker, data, visited, 0);
2298 return cnt;
2301 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2302 cnt++;
2303 if ((!ref
2304 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2305 && (*walker) (ref, vdef, data))
2306 return cnt;
2308 vdef = gimple_vuse (def_stmt);
2310 while (1);
2313 unsigned int
2314 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2315 bool (*walker)(ao_ref *, tree, void *), void *data,
2316 bitmap *visited)
2318 bitmap local_visited = NULL;
2319 unsigned int ret;
2321 timevar_push (TV_ALIAS_STMT_WALK);
2323 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2324 visited ? visited : &local_visited, 0);
2325 if (local_visited)
2326 BITMAP_FREE (local_visited);
2328 timevar_pop (TV_ALIAS_STMT_WALK);
2330 return ret;