Merged revisions 208012,208018-208019,208021,208023-208030,208033,208037,208040-20804...
[official-gcc.git] / main / gcc / tree-ssa-alias.c
blobc04f6455ebb279d2d7afac30f68f771061444bc7
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2014 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 "langhooks.h"
31 #include "flags.h"
32 #include "function.h"
33 #include "tree-pretty-print.h"
34 #include "dumpfile.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "tree-eh.h"
38 #include "gimple-expr.h"
39 #include "is-a.h"
40 #include "gimple.h"
41 #include "gimple-ssa.h"
42 #include "stringpool.h"
43 #include "tree-ssanames.h"
44 #include "expr.h"
45 #include "tree-dfa.h"
46 #include "tree-inline.h"
47 #include "params.h"
48 #include "alloc-pool.h"
49 #include "tree-ssa-alias.h"
50 #include "dbgcnt.h"
51 #include "l-ipo.h"
52 #include "ipa-reference.h"
54 /* Broad overview of how alias analysis on gimple works:
56 Statements clobbering or using memory are linked through the
57 virtual operand factored use-def chain. The virtual operand
58 is unique per function, its symbol is accessible via gimple_vop (cfun).
59 Virtual operands are used for efficiently walking memory statements
60 in the gimple IL and are useful for things like value-numbering as
61 a generation count for memory references.
63 SSA_NAME pointers may have associated points-to information
64 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
65 points-to information is (re-)computed by the TODO_rebuild_alias
66 pass manager todo. Points-to information is also used for more
67 precise tracking of call-clobbered and call-used variables and
68 related disambiguations.
70 This file contains functions for disambiguating memory references,
71 the so called alias-oracle and tools for walking of the gimple IL.
73 The main alias-oracle entry-points are
75 bool stmt_may_clobber_ref_p (gimple, tree)
77 This function queries if a statement may invalidate (parts of)
78 the memory designated by the reference tree argument.
80 bool ref_maybe_used_by_stmt_p (gimple, tree)
82 This function queries if a statement may need (parts of) the
83 memory designated by the reference tree argument.
85 There are variants of these functions that only handle the call
86 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
87 Note that these do not disambiguate against a possible call lhs.
89 bool refs_may_alias_p (tree, tree)
91 This function tries to disambiguate two reference trees.
93 bool ptr_deref_may_alias_global_p (tree)
95 This function queries if dereferencing a pointer variable may
96 alias global memory.
98 More low-level disambiguators are available and documented in
99 this file. Low-level disambiguators dealing with points-to
100 information are in tree-ssa-structalias.c. */
103 /* Query statistics for the different low-level disambiguators.
104 A high-level query may trigger multiple of them. */
106 static struct {
107 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
108 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
109 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
110 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
111 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
112 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
113 } alias_stats;
115 void
116 dump_alias_stats (FILE *s)
118 fprintf (s, "\nAlias oracle query stats:\n");
119 fprintf (s, " refs_may_alias_p: "
120 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
121 HOST_WIDE_INT_PRINT_DEC" queries\n",
122 alias_stats.refs_may_alias_p_no_alias,
123 alias_stats.refs_may_alias_p_no_alias
124 + alias_stats.refs_may_alias_p_may_alias);
125 fprintf (s, " ref_maybe_used_by_call_p: "
126 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
127 HOST_WIDE_INT_PRINT_DEC" queries\n",
128 alias_stats.ref_maybe_used_by_call_p_no_alias,
129 alias_stats.refs_may_alias_p_no_alias
130 + alias_stats.ref_maybe_used_by_call_p_may_alias);
131 fprintf (s, " call_may_clobber_ref_p: "
132 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
133 HOST_WIDE_INT_PRINT_DEC" queries\n",
134 alias_stats.call_may_clobber_ref_p_no_alias,
135 alias_stats.call_may_clobber_ref_p_no_alias
136 + alias_stats.call_may_clobber_ref_p_may_alias);
140 /* Return true, if dereferencing PTR may alias with a global variable. */
142 bool
143 ptr_deref_may_alias_global_p (tree ptr)
145 struct ptr_info_def *pi;
147 /* If we end up with a pointer constant here that may point
148 to global memory. */
149 if (TREE_CODE (ptr) != SSA_NAME)
150 return true;
152 pi = SSA_NAME_PTR_INFO (ptr);
154 /* If we do not have points-to information for this variable,
155 we have to punt. */
156 if (!pi)
157 return true;
159 /* ??? This does not use TBAA to prune globals ptr may not access. */
160 return pt_solution_includes_global (&pi->pt);
163 /* Return true if dereferencing PTR may alias DECL.
164 The caller is responsible for applying TBAA to see if PTR
165 may access DECL at all. */
167 static bool
168 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
170 struct ptr_info_def *pi;
172 /* Conversions are irrelevant for points-to information and
173 data-dependence analysis can feed us those. */
174 STRIP_NOPS (ptr);
176 /* Anything we do not explicilty handle aliases. */
177 if ((TREE_CODE (ptr) != SSA_NAME
178 && TREE_CODE (ptr) != ADDR_EXPR
179 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
180 || !POINTER_TYPE_P (TREE_TYPE (ptr))
181 || (TREE_CODE (decl) != VAR_DECL
182 && TREE_CODE (decl) != PARM_DECL
183 && TREE_CODE (decl) != RESULT_DECL))
184 return true;
186 /* Disregard pointer offsetting. */
187 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
191 ptr = TREE_OPERAND (ptr, 0);
193 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
194 return ptr_deref_may_alias_decl_p (ptr, decl);
197 /* ADDR_EXPR pointers either just offset another pointer or directly
198 specify the pointed-to set. */
199 if (TREE_CODE (ptr) == ADDR_EXPR)
201 tree base = get_base_address (TREE_OPERAND (ptr, 0));
202 if (base
203 && (TREE_CODE (base) == MEM_REF
204 || TREE_CODE (base) == TARGET_MEM_REF))
205 ptr = TREE_OPERAND (base, 0);
206 else if (base
207 && DECL_P (base))
208 return base == decl;
209 else if (base
210 && CONSTANT_CLASS_P (base))
211 return false;
212 else
213 return true;
216 /* Non-aliased variables can not be pointed to. */
217 if (!may_be_aliased (decl))
218 return false;
220 /* If we do not have useful points-to information for this pointer
221 we cannot disambiguate anything else. */
222 pi = SSA_NAME_PTR_INFO (ptr);
223 if (!pi)
224 return true;
226 return pt_solution_includes (&pi->pt, decl);
229 /* Return true if dereferenced PTR1 and PTR2 may alias.
230 The caller is responsible for applying TBAA to see if accesses
231 through PTR1 and PTR2 may conflict at all. */
233 bool
234 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
236 struct ptr_info_def *pi1, *pi2;
238 /* Conversions are irrelevant for points-to information and
239 data-dependence analysis can feed us those. */
240 STRIP_NOPS (ptr1);
241 STRIP_NOPS (ptr2);
243 /* Disregard pointer offsetting. */
244 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
248 ptr1 = TREE_OPERAND (ptr1, 0);
250 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
251 return ptr_derefs_may_alias_p (ptr1, ptr2);
253 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
257 ptr2 = TREE_OPERAND (ptr2, 0);
259 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
260 return ptr_derefs_may_alias_p (ptr1, ptr2);
263 /* ADDR_EXPR pointers either just offset another pointer or directly
264 specify the pointed-to set. */
265 if (TREE_CODE (ptr1) == ADDR_EXPR)
267 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
268 if (base
269 && (TREE_CODE (base) == MEM_REF
270 || TREE_CODE (base) == TARGET_MEM_REF))
271 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
272 else if (base
273 && DECL_P (base))
274 return ptr_deref_may_alias_decl_p (ptr2, base);
275 else
276 return true;
278 if (TREE_CODE (ptr2) == ADDR_EXPR)
280 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
281 if (base
282 && (TREE_CODE (base) == MEM_REF
283 || TREE_CODE (base) == TARGET_MEM_REF))
284 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
285 else if (base
286 && DECL_P (base))
287 return ptr_deref_may_alias_decl_p (ptr1, base);
288 else
289 return true;
292 /* From here we require SSA name pointers. Anything else aliases. */
293 if (TREE_CODE (ptr1) != SSA_NAME
294 || TREE_CODE (ptr2) != SSA_NAME
295 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
296 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
297 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;
335 /* Return true whether REF may refer to global memory. */
337 bool
338 ref_may_alias_global_p (tree ref)
340 tree base = get_base_address (ref);
341 if (DECL_P (base))
342 return is_global_var (base);
343 else if (TREE_CODE (base) == MEM_REF
344 || TREE_CODE (base) == TARGET_MEM_REF)
345 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
346 return true;
349 /* Return true whether STMT may clobber global memory. */
351 bool
352 stmt_may_clobber_global_p (gimple stmt)
354 tree lhs;
356 if (!gimple_vdef (stmt))
357 return false;
359 /* ??? We can ask the oracle whether an artificial pointer
360 dereference with a pointer with points-to information covering
361 all global memory (what about non-address taken memory?) maybe
362 clobbered by this call. As there is at the moment no convenient
363 way of doing that without generating garbage do some manual
364 checking instead.
365 ??? We could make a NULL ao_ref argument to the various
366 predicates special, meaning any global memory. */
368 switch (gimple_code (stmt))
370 case GIMPLE_ASSIGN:
371 lhs = gimple_assign_lhs (stmt);
372 return (TREE_CODE (lhs) != SSA_NAME
373 && ref_may_alias_global_p (lhs));
374 case GIMPLE_CALL:
375 return true;
376 default:
377 return true;
382 /* Dump alias information on FILE. */
384 void
385 dump_alias_info (FILE *file)
387 unsigned i;
388 const char *funcname
389 = lang_hooks.decl_printable_name (current_function_decl, 2);
390 tree var;
392 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
394 fprintf (file, "Aliased symbols\n\n");
396 FOR_EACH_LOCAL_DECL (cfun, i, var)
398 if (may_be_aliased (var))
399 dump_variable (file, var);
402 fprintf (file, "\nCall clobber information\n");
404 fprintf (file, "\nESCAPED");
405 dump_points_to_solution (file, &cfun->gimple_df->escaped);
407 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
409 for (i = 1; i < num_ssa_names; i++)
411 tree ptr = ssa_name (i);
412 struct ptr_info_def *pi;
414 if (ptr == NULL_TREE
415 || !POINTER_TYPE_P (TREE_TYPE (ptr))
416 || SSA_NAME_IN_FREE_LIST (ptr))
417 continue;
419 pi = SSA_NAME_PTR_INFO (ptr);
420 if (pi)
421 dump_points_to_info_for (file, ptr);
424 fprintf (file, "\n");
428 /* Dump alias information on stderr. */
430 DEBUG_FUNCTION void
431 debug_alias_info (void)
433 dump_alias_info (stderr);
437 /* Dump the points-to set *PT into FILE. */
439 void
440 dump_points_to_solution (FILE *file, struct pt_solution *pt)
442 if (pt->anything)
443 fprintf (file, ", points-to anything");
445 if (pt->nonlocal)
446 fprintf (file, ", points-to non-local");
448 if (pt->escaped)
449 fprintf (file, ", points-to escaped");
451 if (pt->ipa_escaped)
452 fprintf (file, ", points-to unit escaped");
454 if (pt->null)
455 fprintf (file, ", points-to NULL");
457 if (pt->vars)
459 fprintf (file, ", points-to vars: ");
460 dump_decl_set (file, pt->vars);
461 if (pt->vars_contains_nonlocal
462 && pt->vars_contains_escaped_heap)
463 fprintf (file, " (nonlocal, escaped heap)");
464 else if (pt->vars_contains_nonlocal
465 && pt->vars_contains_escaped)
466 fprintf (file, " (nonlocal, escaped)");
467 else if (pt->vars_contains_nonlocal)
468 fprintf (file, " (nonlocal)");
469 else if (pt->vars_contains_escaped_heap)
470 fprintf (file, " (escaped heap)");
471 else if (pt->vars_contains_escaped)
472 fprintf (file, " (escaped)");
477 /* Unified dump function for pt_solution. */
479 DEBUG_FUNCTION void
480 debug (pt_solution &ref)
482 dump_points_to_solution (stderr, &ref);
485 DEBUG_FUNCTION void
486 debug (pt_solution *ptr)
488 if (ptr)
489 debug (*ptr);
490 else
491 fprintf (stderr, "<nil>\n");
495 /* Dump points-to information for SSA_NAME PTR into FILE. */
497 void
498 dump_points_to_info_for (FILE *file, tree ptr)
500 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
502 print_generic_expr (file, ptr, dump_flags);
504 if (pi)
505 dump_points_to_solution (file, &pi->pt);
506 else
507 fprintf (file, ", points-to anything");
509 fprintf (file, "\n");
513 /* Dump points-to information for VAR into stderr. */
515 DEBUG_FUNCTION void
516 debug_points_to_info_for (tree var)
518 dump_points_to_info_for (stderr, var);
522 /* Initializes the alias-oracle reference representation *R from REF. */
524 void
525 ao_ref_init (ao_ref *r, tree ref)
527 r->ref = ref;
528 r->base = NULL_TREE;
529 r->offset = 0;
530 r->size = -1;
531 r->max_size = -1;
532 r->ref_alias_set = -1;
533 r->base_alias_set = -1;
534 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
537 /* Returns the base object of the memory reference *REF. */
539 tree
540 ao_ref_base (ao_ref *ref)
542 if (ref->base)
543 return ref->base;
544 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
545 &ref->max_size);
546 return ref->base;
549 /* Returns the base object alias set of the memory reference *REF. */
551 static alias_set_type
552 ao_ref_base_alias_set (ao_ref *ref)
554 tree base_ref;
555 if (ref->base_alias_set != -1)
556 return ref->base_alias_set;
557 if (!ref->ref)
558 return 0;
559 base_ref = ref->ref;
560 while (handled_component_p (base_ref))
561 base_ref = TREE_OPERAND (base_ref, 0);
562 ref->base_alias_set = get_alias_set (base_ref);
563 return ref->base_alias_set;
566 /* Returns the reference alias set of the memory reference *REF. */
568 alias_set_type
569 ao_ref_alias_set (ao_ref *ref)
571 if (ref->ref_alias_set != -1)
572 return ref->ref_alias_set;
573 ref->ref_alias_set = get_alias_set (ref->ref);
574 return ref->ref_alias_set;
577 /* Init an alias-oracle reference representation from a gimple pointer
578 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
579 size is assumed to be unknown. The access is assumed to be only
580 to or after of the pointer target, not before it. */
582 void
583 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
585 HOST_WIDE_INT t, size_hwi, extra_offset = 0;
586 ref->ref = NULL_TREE;
587 if (TREE_CODE (ptr) == SSA_NAME)
589 gimple stmt = SSA_NAME_DEF_STMT (ptr);
590 if (gimple_assign_single_p (stmt)
591 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
592 ptr = gimple_assign_rhs1 (stmt);
593 else if (is_gimple_assign (stmt)
594 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
595 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
597 ptr = gimple_assign_rhs1 (stmt);
598 extra_offset = BITS_PER_UNIT
599 * int_cst_value (gimple_assign_rhs2 (stmt));
603 if (TREE_CODE (ptr) == ADDR_EXPR)
605 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
606 if (ref->base)
607 ref->offset = BITS_PER_UNIT * t;
608 else
610 size = NULL_TREE;
611 ref->offset = 0;
612 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
615 else
617 ref->base = build2 (MEM_REF, char_type_node,
618 ptr, null_pointer_node);
619 ref->offset = 0;
621 ref->offset += extra_offset;
622 if (size
623 && tree_fits_shwi_p (size)
624 && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
625 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
626 else
627 ref->max_size = ref->size = -1;
628 ref->ref_alias_set = 0;
629 ref->base_alias_set = 0;
630 ref->volatile_p = false;
633 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
634 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
635 decide. */
637 static inline int
638 same_type_for_tbaa (tree type1, tree type2)
640 type1 = TYPE_MAIN_VARIANT (type1);
641 type2 = TYPE_MAIN_VARIANT (type2);
643 /* If we would have to do structural comparison bail out. */
644 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
645 || TYPE_STRUCTURAL_EQUALITY_P (type2))
646 return -1;
648 /* Compare the canonical types. */
649 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
650 return 1;
652 /* ??? Array types are not properly unified in all cases as we have
653 spurious changes in the index types for example. Removing this
654 causes all sorts of problems with the Fortran frontend. */
655 if (TREE_CODE (type1) == ARRAY_TYPE
656 && TREE_CODE (type2) == ARRAY_TYPE)
657 return -1;
659 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
660 object of one of its constrained subtypes, e.g. when a function with an
661 unconstrained parameter passed by reference is called on an object and
662 inlined. But, even in the case of a fixed size, type and subtypes are
663 not equivalent enough as to share the same TYPE_CANONICAL, since this
664 would mean that conversions between them are useless, whereas they are
665 not (e.g. type and subtypes can have different modes). So, in the end,
666 they are only guaranteed to have the same alias set. */
667 if (get_alias_set (type1) == get_alias_set (type2))
668 return -1;
670 if (L_IPO_COMP_MODE)
671 return equivalent_struct_types_for_tbaa (type1, type2);
673 /* The types are known to be not equal. */
674 return 0;
677 /* Determine if the two component references REF1 and REF2 which are
678 based on access types TYPE1 and TYPE2 and of which at least one is based
679 on an indirect reference may alias. REF2 is the only one that can
680 be a decl in which case REF2_IS_DECL is true.
681 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
682 are the respective alias sets. */
684 static bool
685 aliasing_component_refs_p (tree ref1,
686 alias_set_type ref1_alias_set,
687 alias_set_type base1_alias_set,
688 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
689 tree ref2,
690 alias_set_type ref2_alias_set,
691 alias_set_type base2_alias_set,
692 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
693 bool ref2_is_decl)
695 /* If one reference is a component references through pointers try to find a
696 common base and apply offset based disambiguation. This handles
697 for example
698 struct A { int i; int j; } *q;
699 struct B { struct A a; int k; } *p;
700 disambiguating q->i and p->a.j. */
701 tree base1, base2;
702 tree type1, type2;
703 tree *refp;
704 int same_p;
706 /* Choose bases and base types to search for. */
707 base1 = ref1;
708 while (handled_component_p (base1))
709 base1 = TREE_OPERAND (base1, 0);
710 type1 = TREE_TYPE (base1);
711 base2 = ref2;
712 while (handled_component_p (base2))
713 base2 = TREE_OPERAND (base2, 0);
714 type2 = TREE_TYPE (base2);
716 /* Now search for the type1 in the access path of ref2. This
717 would be a common base for doing offset based disambiguation on. */
718 refp = &ref2;
719 while (handled_component_p (*refp)
720 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
721 refp = &TREE_OPERAND (*refp, 0);
722 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
723 /* If we couldn't compare types we have to bail out. */
724 if (same_p == -1)
725 return true;
726 else if (same_p == 1)
728 HOST_WIDE_INT offadj, sztmp, msztmp;
729 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
730 offset2 -= offadj;
731 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
732 offset1 -= offadj;
733 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
735 /* If we didn't find a common base, try the other way around. */
736 refp = &ref1;
737 while (handled_component_p (*refp)
738 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
739 refp = &TREE_OPERAND (*refp, 0);
740 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
741 /* If we couldn't compare types we have to bail out. */
742 if (same_p == -1)
743 return true;
744 else if (same_p == 1)
746 HOST_WIDE_INT offadj, sztmp, msztmp;
747 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
748 offset1 -= offadj;
749 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
750 offset2 -= offadj;
751 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
754 /* If we have two type access paths B1.path1 and B2.path2 they may
755 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
756 But we can still have a path that goes B1.path1...B2.path2 with
757 a part that we do not see. So we can only disambiguate now
758 if there is no B2 in the tail of path1 and no B1 on the
759 tail of path2. */
760 if (base1_alias_set == ref2_alias_set
761 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
762 return true;
763 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
764 if (!ref2_is_decl)
765 return (base2_alias_set == ref1_alias_set
766 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
767 return false;
770 /* Return true if we can determine that component references REF1 and REF2,
771 that are within a common DECL, cannot overlap. */
773 static bool
774 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
776 auto_vec<tree, 16> component_refs1;
777 auto_vec<tree, 16> component_refs2;
779 /* Create the stack of handled components for REF1. */
780 while (handled_component_p (ref1))
782 component_refs1.safe_push (ref1);
783 ref1 = TREE_OPERAND (ref1, 0);
785 if (TREE_CODE (ref1) == MEM_REF)
787 if (!integer_zerop (TREE_OPERAND (ref1, 1)))
788 goto may_overlap;
789 ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
792 /* Create the stack of handled components for REF2. */
793 while (handled_component_p (ref2))
795 component_refs2.safe_push (ref2);
796 ref2 = TREE_OPERAND (ref2, 0);
798 if (TREE_CODE (ref2) == MEM_REF)
800 if (!integer_zerop (TREE_OPERAND (ref2, 1)))
801 goto may_overlap;
802 ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
805 /* We must have the same base DECL. */
806 gcc_assert (ref1 == ref2);
808 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
809 rank. This is sufficient because we start from the same DECL and you
810 cannot reference several fields at a time with COMPONENT_REFs (unlike
811 with ARRAY_RANGE_REFs for arrays) so you always need the same number
812 of them to access a sub-component, unless you're in a union, in which
813 case the return value will precisely be false. */
814 while (true)
818 if (component_refs1.is_empty ())
819 goto may_overlap;
820 ref1 = component_refs1.pop ();
822 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
826 if (component_refs2.is_empty ())
827 goto may_overlap;
828 ref2 = component_refs2.pop ();
830 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
832 /* Beware of BIT_FIELD_REF. */
833 if (TREE_CODE (ref1) != COMPONENT_REF
834 || TREE_CODE (ref2) != COMPONENT_REF)
835 goto may_overlap;
837 tree field1 = TREE_OPERAND (ref1, 1);
838 tree field2 = TREE_OPERAND (ref2, 1);
840 /* ??? We cannot simply use the type of operand #0 of the refs here
841 as the Fortran compiler smuggles type punning into COMPONENT_REFs
842 for common blocks instead of using unions like everyone else. */
843 tree type1 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field1));
844 tree type2 = TYPE_MAIN_VARIANT (DECL_CONTEXT (field2));
846 /* We cannot disambiguate fields in a union or qualified union. */
847 if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
848 goto may_overlap;
850 /* Different fields of the same record type cannot overlap.
851 ??? Bitfields can overlap at RTL level so punt on them. */
852 if (field1 != field2)
854 component_refs1.release ();
855 component_refs2.release ();
856 return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
860 may_overlap:
861 component_refs1.release ();
862 component_refs2.release ();
863 return false;
866 /* Return true if two memory references based on the variables BASE1
867 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
868 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
869 if non-NULL are the complete memory reference trees. */
871 static bool
872 decl_refs_may_alias_p (tree ref1, tree base1,
873 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
874 tree ref2, tree base2,
875 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
877 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
879 /* If both references are based on different variables, they cannot alias. */
880 if (base1 != base2)
881 return false;
883 /* If both references are based on the same variable, they cannot alias if
884 the accesses do not overlap. */
885 if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
886 return false;
888 /* For components with variable position, the above test isn't sufficient,
889 so we disambiguate component references manually. */
890 if (ref1 && ref2
891 && handled_component_p (ref1) && handled_component_p (ref2)
892 && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
893 return false;
895 return true;
898 /* Return true if an indirect reference based on *PTR1 constrained
899 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
900 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
901 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
902 in which case they are computed on-demand. REF1 and REF2
903 if non-NULL are the complete memory reference trees. */
905 static bool
906 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
907 HOST_WIDE_INT offset1,
908 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
909 alias_set_type ref1_alias_set,
910 alias_set_type base1_alias_set,
911 tree ref2 ATTRIBUTE_UNUSED, tree base2,
912 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
913 alias_set_type ref2_alias_set,
914 alias_set_type base2_alias_set, bool tbaa_p)
916 tree ptr1;
917 tree ptrtype1, dbase2;
918 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
919 HOST_WIDE_INT doffset1, doffset2;
920 double_int moff;
922 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
923 || TREE_CODE (base1) == TARGET_MEM_REF)
924 && DECL_P (base2));
926 ptr1 = TREE_OPERAND (base1, 0);
928 /* The offset embedded in MEM_REFs can be negative. Bias them
929 so that the resulting offset adjustment is positive. */
930 moff = mem_ref_offset (base1);
931 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
932 if (moff.is_negative ())
933 offset2p += (-moff).low;
934 else
935 offset1p += moff.low;
937 /* If only one reference is based on a variable, they cannot alias if
938 the pointer access is beyond the extent of the variable access.
939 (the pointer base cannot validly point to an offset less than zero
940 of the variable).
941 ??? IVOPTs creates bases that do not honor this restriction,
942 so do not apply this optimization for TARGET_MEM_REFs. */
943 if (TREE_CODE (base1) != TARGET_MEM_REF
944 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
945 return false;
946 /* They also cannot alias if the pointer may not point to the decl. */
947 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
948 return false;
950 /* Disambiguations that rely on strict aliasing rules follow. */
951 if (!flag_strict_aliasing || !tbaa_p)
952 return true;
954 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
956 /* If the alias set for a pointer access is zero all bets are off. */
957 if (base1_alias_set == -1)
958 base1_alias_set = get_deref_alias_set (ptrtype1);
959 if (base1_alias_set == 0)
960 return true;
961 if (base2_alias_set == -1)
962 base2_alias_set = get_alias_set (base2);
964 /* When we are trying to disambiguate an access with a pointer dereference
965 as base versus one with a decl as base we can use both the size
966 of the decl and its dynamic type for extra disambiguation.
967 ??? We do not know anything about the dynamic type of the decl
968 other than that its alias-set contains base2_alias_set as a subset
969 which does not help us here. */
970 /* As we know nothing useful about the dynamic type of the decl just
971 use the usual conflict check rather than a subset test.
972 ??? We could introduce -fvery-strict-aliasing when the language
973 does not allow decls to have a dynamic type that differs from their
974 static type. Then we can check
975 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
976 if (base1_alias_set != base2_alias_set
977 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
978 return false;
979 /* If the size of the access relevant for TBAA through the pointer
980 is bigger than the size of the decl we can't possibly access the
981 decl via that pointer. */
982 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
983 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
984 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
985 /* ??? This in turn may run afoul when a decl of type T which is
986 a member of union type U is accessed through a pointer to
987 type U and sizeof T is smaller than sizeof U. */
988 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
989 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
990 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
991 return false;
993 if (!ref2)
994 return true;
996 /* If the decl is accessed via a MEM_REF, reconstruct the base
997 we can use for TBAA and an appropriately adjusted offset. */
998 dbase2 = ref2;
999 while (handled_component_p (dbase2))
1000 dbase2 = TREE_OPERAND (dbase2, 0);
1001 doffset1 = offset1;
1002 doffset2 = offset2;
1003 if (TREE_CODE (dbase2) == MEM_REF
1004 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1006 double_int moff = mem_ref_offset (dbase2);
1007 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1008 if (moff.is_negative ())
1009 doffset1 -= (-moff).low;
1010 else
1011 doffset2 -= moff.low;
1014 /* If either reference is view-converted, give up now. */
1015 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1016 || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1017 return true;
1019 /* If both references are through the same type, they do not alias
1020 if the accesses do not overlap. This does extra disambiguation
1021 for mixed/pointer accesses but requires strict aliasing.
1022 For MEM_REFs we require that the component-ref offset we computed
1023 is relative to the start of the type which we ensure by
1024 comparing rvalue and access type and disregarding the constant
1025 pointer offset. */
1026 if ((TREE_CODE (base1) != TARGET_MEM_REF
1027 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1028 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1029 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1031 /* Do access-path based disambiguation. */
1032 if (ref1 && ref2
1033 && (handled_component_p (ref1) || handled_component_p (ref2)))
1034 return aliasing_component_refs_p (ref1,
1035 ref1_alias_set, base1_alias_set,
1036 offset1, max_size1,
1037 ref2,
1038 ref2_alias_set, base2_alias_set,
1039 offset2, max_size2, true);
1041 return true;
1044 /* Return true if two indirect references based on *PTR1
1045 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1046 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1047 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1048 in which case they are computed on-demand. REF1 and REF2
1049 if non-NULL are the complete memory reference trees. */
1051 static bool
1052 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1053 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1054 alias_set_type ref1_alias_set,
1055 alias_set_type base1_alias_set,
1056 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1057 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1058 alias_set_type ref2_alias_set,
1059 alias_set_type base2_alias_set, bool tbaa_p)
1061 tree ptr1;
1062 tree ptr2;
1063 tree ptrtype1, ptrtype2;
1065 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1066 || TREE_CODE (base1) == TARGET_MEM_REF)
1067 && (TREE_CODE (base2) == MEM_REF
1068 || TREE_CODE (base2) == TARGET_MEM_REF));
1070 ptr1 = TREE_OPERAND (base1, 0);
1071 ptr2 = TREE_OPERAND (base2, 0);
1073 /* If both bases are based on pointers they cannot alias if they may not
1074 point to the same memory object or if they point to the same object
1075 and the accesses do not overlap. */
1076 if ((!cfun || gimple_in_ssa_p (cfun))
1077 && operand_equal_p (ptr1, ptr2, 0)
1078 && (((TREE_CODE (base1) != TARGET_MEM_REF
1079 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1080 && (TREE_CODE (base2) != TARGET_MEM_REF
1081 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1082 || (TREE_CODE (base1) == TARGET_MEM_REF
1083 && TREE_CODE (base2) == TARGET_MEM_REF
1084 && (TMR_STEP (base1) == TMR_STEP (base2)
1085 || (TMR_STEP (base1) && TMR_STEP (base2)
1086 && operand_equal_p (TMR_STEP (base1),
1087 TMR_STEP (base2), 0)))
1088 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1089 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1090 && operand_equal_p (TMR_INDEX (base1),
1091 TMR_INDEX (base2), 0)))
1092 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1093 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1094 && operand_equal_p (TMR_INDEX2 (base1),
1095 TMR_INDEX2 (base2), 0))))))
1097 double_int moff;
1098 /* The offset embedded in MEM_REFs can be negative. Bias them
1099 so that the resulting offset adjustment is positive. */
1100 moff = mem_ref_offset (base1);
1101 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1102 if (moff.is_negative ())
1103 offset2 += (-moff).low;
1104 else
1105 offset1 += moff.low;
1106 moff = mem_ref_offset (base2);
1107 moff = moff.lshift (BITS_PER_UNIT == 8 ? 3 : exact_log2 (BITS_PER_UNIT));
1108 if (moff.is_negative ())
1109 offset1 += (-moff).low;
1110 else
1111 offset2 += moff.low;
1112 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1114 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1115 return false;
1117 /* Disambiguations that rely on strict aliasing rules follow. */
1118 if (!flag_strict_aliasing || !tbaa_p)
1119 return true;
1121 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1122 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1124 /* If the alias set for a pointer access is zero all bets are off. */
1125 if (base1_alias_set == -1)
1126 base1_alias_set = get_deref_alias_set (ptrtype1);
1127 if (base1_alias_set == 0)
1128 return true;
1129 if (base2_alias_set == -1)
1130 base2_alias_set = get_deref_alias_set (ptrtype2);
1131 if (base2_alias_set == 0)
1132 return true;
1134 /* If both references are through the same type, they do not alias
1135 if the accesses do not overlap. This does extra disambiguation
1136 for mixed/pointer accesses but requires strict aliasing. */
1137 if ((TREE_CODE (base1) != TARGET_MEM_REF
1138 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1139 && (TREE_CODE (base2) != TARGET_MEM_REF
1140 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1141 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1142 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1143 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1144 TREE_TYPE (ptrtype2)) == 1)
1145 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1147 /* Do type-based disambiguation. */
1148 if (base1_alias_set != base2_alias_set
1149 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1150 return false;
1152 /* Do access-path based disambiguation. */
1153 if (ref1 && ref2
1154 && (handled_component_p (ref1) || handled_component_p (ref2))
1155 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1156 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
1157 return aliasing_component_refs_p (ref1,
1158 ref1_alias_set, base1_alias_set,
1159 offset1, max_size1,
1160 ref2,
1161 ref2_alias_set, base2_alias_set,
1162 offset2, max_size2, false);
1164 return true;
1167 /* Return true, if the two memory references REF1 and REF2 may alias. */
1169 bool
1170 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1172 tree base1, base2;
1173 HOST_WIDE_INT offset1 = 0, offset2 = 0;
1174 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1175 bool var1_p, var2_p, ind1_p, ind2_p;
1177 gcc_checking_assert ((!ref1->ref
1178 || TREE_CODE (ref1->ref) == SSA_NAME
1179 || DECL_P (ref1->ref)
1180 || TREE_CODE (ref1->ref) == STRING_CST
1181 || handled_component_p (ref1->ref)
1182 || TREE_CODE (ref1->ref) == MEM_REF
1183 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1184 && (!ref2->ref
1185 || TREE_CODE (ref2->ref) == SSA_NAME
1186 || DECL_P (ref2->ref)
1187 || TREE_CODE (ref2->ref) == STRING_CST
1188 || handled_component_p (ref2->ref)
1189 || TREE_CODE (ref2->ref) == MEM_REF
1190 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1192 if (!dbg_cnt (alias))
1193 return true;
1195 /* Decompose the references into their base objects and the access. */
1196 base1 = ao_ref_base (ref1);
1197 offset1 = ref1->offset;
1198 max_size1 = ref1->max_size;
1199 base2 = ao_ref_base (ref2);
1200 offset2 = ref2->offset;
1201 max_size2 = ref2->max_size;
1203 /* We can end up with registers or constants as bases for example from
1204 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1205 which is seen as a struct copy. */
1206 if (TREE_CODE (base1) == SSA_NAME
1207 || TREE_CODE (base1) == CONST_DECL
1208 || TREE_CODE (base1) == CONSTRUCTOR
1209 || TREE_CODE (base1) == ADDR_EXPR
1210 || CONSTANT_CLASS_P (base1)
1211 || TREE_CODE (base2) == SSA_NAME
1212 || TREE_CODE (base2) == CONST_DECL
1213 || TREE_CODE (base2) == CONSTRUCTOR
1214 || TREE_CODE (base2) == ADDR_EXPR
1215 || CONSTANT_CLASS_P (base2))
1216 return false;
1218 /* We can end up referring to code via function and label decls.
1219 As we likely do not properly track code aliases conservatively
1220 bail out. */
1221 if (TREE_CODE (base1) == FUNCTION_DECL
1222 || TREE_CODE (base1) == LABEL_DECL
1223 || TREE_CODE (base2) == FUNCTION_DECL
1224 || TREE_CODE (base2) == LABEL_DECL)
1225 return true;
1227 /* Two volatile accesses always conflict. */
1228 if (ref1->volatile_p
1229 && ref2->volatile_p)
1230 return true;
1232 /* Defer to simple offset based disambiguation if we have
1233 references based on two decls. Do this before defering to
1234 TBAA to handle must-alias cases in conformance with the
1235 GCC extension of allowing type-punning through unions. */
1236 var1_p = DECL_P (base1);
1237 var2_p = DECL_P (base2);
1238 if (var1_p && var2_p)
1239 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1240 ref2->ref, base2, offset2, max_size2);
1242 ind1_p = (TREE_CODE (base1) == MEM_REF
1243 || TREE_CODE (base1) == TARGET_MEM_REF);
1244 ind2_p = (TREE_CODE (base2) == MEM_REF
1245 || TREE_CODE (base2) == TARGET_MEM_REF);
1247 /* Canonicalize the pointer-vs-decl case. */
1248 if (ind1_p && var2_p)
1250 HOST_WIDE_INT tmp1;
1251 tree tmp2;
1252 ao_ref *tmp3;
1253 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1254 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1255 tmp2 = base1; base1 = base2; base2 = tmp2;
1256 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1257 var1_p = true;
1258 ind1_p = false;
1259 var2_p = false;
1260 ind2_p = true;
1263 /* First defer to TBAA if possible. */
1264 if (tbaa_p
1265 && flag_strict_aliasing
1266 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1267 ao_ref_alias_set (ref2)))
1268 return false;
1270 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1271 if (var1_p && ind2_p)
1272 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1273 offset2, max_size2,
1274 ao_ref_alias_set (ref2), -1,
1275 ref1->ref, base1,
1276 offset1, max_size1,
1277 ao_ref_alias_set (ref1),
1278 ao_ref_base_alias_set (ref1),
1279 tbaa_p);
1280 else if (ind1_p && ind2_p)
1281 return indirect_refs_may_alias_p (ref1->ref, base1,
1282 offset1, max_size1,
1283 ao_ref_alias_set (ref1), -1,
1284 ref2->ref, base2,
1285 offset2, max_size2,
1286 ao_ref_alias_set (ref2), -1,
1287 tbaa_p);
1289 /* We really do not want to end up here, but returning true is safe. */
1290 #ifdef ENABLE_CHECKING
1291 gcc_unreachable ();
1292 #else
1293 return true;
1294 #endif
1297 bool
1298 refs_may_alias_p (tree ref1, tree ref2)
1300 ao_ref r1, r2;
1301 bool res;
1302 ao_ref_init (&r1, ref1);
1303 ao_ref_init (&r2, ref2);
1304 res = refs_may_alias_p_1 (&r1, &r2, true);
1305 if (res)
1306 ++alias_stats.refs_may_alias_p_may_alias;
1307 else
1308 ++alias_stats.refs_may_alias_p_no_alias;
1309 return res;
1312 /* Returns true if there is a anti-dependence for the STORE that
1313 executes after the LOAD. */
1315 bool
1316 refs_anti_dependent_p (tree load, tree store)
1318 ao_ref r1, r2;
1319 ao_ref_init (&r1, load);
1320 ao_ref_init (&r2, store);
1321 return refs_may_alias_p_1 (&r1, &r2, false);
1324 /* Returns true if there is a output dependence for the stores
1325 STORE1 and STORE2. */
1327 bool
1328 refs_output_dependent_p (tree store1, tree store2)
1330 ao_ref r1, r2;
1331 ao_ref_init (&r1, store1);
1332 ao_ref_init (&r2, store2);
1333 return refs_may_alias_p_1 (&r1, &r2, false);
1336 /* If the call CALL may use the memory reference REF return true,
1337 otherwise return false. */
1339 static bool
1340 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1342 tree base, callee;
1343 unsigned i;
1344 int flags = gimple_call_flags (call);
1346 /* Const functions without a static chain do not implicitly use memory. */
1347 if (!gimple_call_chain (call)
1348 && (flags & (ECF_CONST|ECF_NOVOPS)))
1349 goto process_args;
1351 base = ao_ref_base (ref);
1352 if (!base)
1353 return true;
1355 /* A call that is not without side-effects might involve volatile
1356 accesses and thus conflicts with all other volatile accesses. */
1357 if (ref->volatile_p)
1358 return true;
1360 /* If the reference is based on a decl that is not aliased the call
1361 cannot possibly use it. */
1362 if (DECL_P (base)
1363 && !may_be_aliased (base)
1364 /* But local statics can be used through recursion. */
1365 && !is_global_var (base))
1366 goto process_args;
1368 callee = gimple_call_fndecl (call);
1370 /* Handle those builtin functions explicitly that do not act as
1371 escape points. See tree-ssa-structalias.c:find_func_aliases
1372 for the list of builtins we might need to handle here. */
1373 if (callee != NULL_TREE
1374 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1375 switch (DECL_FUNCTION_CODE (callee))
1377 /* All the following functions read memory pointed to by
1378 their second argument. strcat/strncat additionally
1379 reads memory pointed to by the first argument. */
1380 case BUILT_IN_STRCAT:
1381 case BUILT_IN_STRNCAT:
1383 ao_ref dref;
1384 ao_ref_init_from_ptr_and_size (&dref,
1385 gimple_call_arg (call, 0),
1386 NULL_TREE);
1387 if (refs_may_alias_p_1 (&dref, ref, false))
1388 return true;
1390 /* FALLTHRU */
1391 case BUILT_IN_STRCPY:
1392 case BUILT_IN_STRNCPY:
1393 case BUILT_IN_MEMCPY:
1394 case BUILT_IN_MEMMOVE:
1395 case BUILT_IN_MEMPCPY:
1396 case BUILT_IN_STPCPY:
1397 case BUILT_IN_STPNCPY:
1398 case BUILT_IN_TM_MEMCPY:
1399 case BUILT_IN_TM_MEMMOVE:
1401 ao_ref dref;
1402 tree size = NULL_TREE;
1403 if (gimple_call_num_args (call) == 3)
1404 size = gimple_call_arg (call, 2);
1405 ao_ref_init_from_ptr_and_size (&dref,
1406 gimple_call_arg (call, 1),
1407 size);
1408 return refs_may_alias_p_1 (&dref, ref, false);
1410 case BUILT_IN_STRCAT_CHK:
1411 case BUILT_IN_STRNCAT_CHK:
1413 ao_ref dref;
1414 ao_ref_init_from_ptr_and_size (&dref,
1415 gimple_call_arg (call, 0),
1416 NULL_TREE);
1417 if (refs_may_alias_p_1 (&dref, ref, false))
1418 return true;
1420 /* FALLTHRU */
1421 case BUILT_IN_STRCPY_CHK:
1422 case BUILT_IN_STRNCPY_CHK:
1423 case BUILT_IN_MEMCPY_CHK:
1424 case BUILT_IN_MEMMOVE_CHK:
1425 case BUILT_IN_MEMPCPY_CHK:
1426 case BUILT_IN_STPCPY_CHK:
1427 case BUILT_IN_STPNCPY_CHK:
1429 ao_ref dref;
1430 tree size = NULL_TREE;
1431 if (gimple_call_num_args (call) == 4)
1432 size = gimple_call_arg (call, 2);
1433 ao_ref_init_from_ptr_and_size (&dref,
1434 gimple_call_arg (call, 1),
1435 size);
1436 return refs_may_alias_p_1 (&dref, ref, false);
1438 case BUILT_IN_BCOPY:
1440 ao_ref dref;
1441 tree size = gimple_call_arg (call, 2);
1442 ao_ref_init_from_ptr_and_size (&dref,
1443 gimple_call_arg (call, 0),
1444 size);
1445 return refs_may_alias_p_1 (&dref, ref, false);
1448 /* The following functions read memory pointed to by their
1449 first argument. */
1450 CASE_BUILT_IN_TM_LOAD (1):
1451 CASE_BUILT_IN_TM_LOAD (2):
1452 CASE_BUILT_IN_TM_LOAD (4):
1453 CASE_BUILT_IN_TM_LOAD (8):
1454 CASE_BUILT_IN_TM_LOAD (FLOAT):
1455 CASE_BUILT_IN_TM_LOAD (DOUBLE):
1456 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1457 CASE_BUILT_IN_TM_LOAD (M64):
1458 CASE_BUILT_IN_TM_LOAD (M128):
1459 CASE_BUILT_IN_TM_LOAD (M256):
1460 case BUILT_IN_TM_LOG:
1461 case BUILT_IN_TM_LOG_1:
1462 case BUILT_IN_TM_LOG_2:
1463 case BUILT_IN_TM_LOG_4:
1464 case BUILT_IN_TM_LOG_8:
1465 case BUILT_IN_TM_LOG_FLOAT:
1466 case BUILT_IN_TM_LOG_DOUBLE:
1467 case BUILT_IN_TM_LOG_LDOUBLE:
1468 case BUILT_IN_TM_LOG_M64:
1469 case BUILT_IN_TM_LOG_M128:
1470 case BUILT_IN_TM_LOG_M256:
1471 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1473 /* These read memory pointed to by the first argument. */
1474 case BUILT_IN_STRDUP:
1475 case BUILT_IN_STRNDUP:
1477 ao_ref dref;
1478 tree size = NULL_TREE;
1479 if (gimple_call_num_args (call) == 2)
1480 size = gimple_call_arg (call, 1);
1481 ao_ref_init_from_ptr_and_size (&dref,
1482 gimple_call_arg (call, 0),
1483 size);
1484 return refs_may_alias_p_1 (&dref, ref, false);
1486 /* These read memory pointed to by the first argument. */
1487 case BUILT_IN_INDEX:
1488 case BUILT_IN_STRCHR:
1489 case BUILT_IN_STRRCHR:
1491 ao_ref dref;
1492 ao_ref_init_from_ptr_and_size (&dref,
1493 gimple_call_arg (call, 0),
1494 NULL_TREE);
1495 return refs_may_alias_p_1 (&dref, ref, false);
1497 /* These read memory pointed to by the first argument with size
1498 in the third argument. */
1499 case BUILT_IN_MEMCHR:
1501 ao_ref dref;
1502 ao_ref_init_from_ptr_and_size (&dref,
1503 gimple_call_arg (call, 0),
1504 gimple_call_arg (call, 2));
1505 return refs_may_alias_p_1 (&dref, ref, false);
1507 /* These read memory pointed to by the first and second arguments. */
1508 case BUILT_IN_STRSTR:
1509 case BUILT_IN_STRPBRK:
1511 ao_ref dref;
1512 ao_ref_init_from_ptr_and_size (&dref,
1513 gimple_call_arg (call, 0),
1514 NULL_TREE);
1515 if (refs_may_alias_p_1 (&dref, ref, false))
1516 return true;
1517 ao_ref_init_from_ptr_and_size (&dref,
1518 gimple_call_arg (call, 1),
1519 NULL_TREE);
1520 return refs_may_alias_p_1 (&dref, ref, false);
1523 /* The following builtins do not read from memory. */
1524 case BUILT_IN_FREE:
1525 case BUILT_IN_MALLOC:
1526 case BUILT_IN_POSIX_MEMALIGN:
1527 case BUILT_IN_CALLOC:
1528 case BUILT_IN_ALLOCA:
1529 case BUILT_IN_ALLOCA_WITH_ALIGN:
1530 case BUILT_IN_STACK_SAVE:
1531 case BUILT_IN_STACK_RESTORE:
1532 case BUILT_IN_MEMSET:
1533 case BUILT_IN_TM_MEMSET:
1534 case BUILT_IN_MEMSET_CHK:
1535 case BUILT_IN_FREXP:
1536 case BUILT_IN_FREXPF:
1537 case BUILT_IN_FREXPL:
1538 case BUILT_IN_GAMMA_R:
1539 case BUILT_IN_GAMMAF_R:
1540 case BUILT_IN_GAMMAL_R:
1541 case BUILT_IN_LGAMMA_R:
1542 case BUILT_IN_LGAMMAF_R:
1543 case BUILT_IN_LGAMMAL_R:
1544 case BUILT_IN_MODF:
1545 case BUILT_IN_MODFF:
1546 case BUILT_IN_MODFL:
1547 case BUILT_IN_REMQUO:
1548 case BUILT_IN_REMQUOF:
1549 case BUILT_IN_REMQUOL:
1550 case BUILT_IN_SINCOS:
1551 case BUILT_IN_SINCOSF:
1552 case BUILT_IN_SINCOSL:
1553 case BUILT_IN_ASSUME_ALIGNED:
1554 case BUILT_IN_VA_END:
1555 return false;
1556 /* __sync_* builtins and some OpenMP builtins act as threading
1557 barriers. */
1558 #undef DEF_SYNC_BUILTIN
1559 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1560 #include "sync-builtins.def"
1561 #undef DEF_SYNC_BUILTIN
1562 case BUILT_IN_GOMP_ATOMIC_START:
1563 case BUILT_IN_GOMP_ATOMIC_END:
1564 case BUILT_IN_GOMP_BARRIER:
1565 case BUILT_IN_GOMP_BARRIER_CANCEL:
1566 case BUILT_IN_GOMP_TASKWAIT:
1567 case BUILT_IN_GOMP_TASKGROUP_END:
1568 case BUILT_IN_GOMP_CRITICAL_START:
1569 case BUILT_IN_GOMP_CRITICAL_END:
1570 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1571 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1572 case BUILT_IN_GOMP_LOOP_END:
1573 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1574 case BUILT_IN_GOMP_ORDERED_START:
1575 case BUILT_IN_GOMP_ORDERED_END:
1576 case BUILT_IN_GOMP_SECTIONS_END:
1577 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1578 case BUILT_IN_GOMP_SINGLE_COPY_START:
1579 case BUILT_IN_GOMP_SINGLE_COPY_END:
1580 return true;
1582 default:
1583 /* Fallthru to general call handling. */;
1586 /* Check if base is a global static variable that is not read
1587 by the function. */
1588 if (callee != NULL_TREE
1589 && TREE_CODE (base) == VAR_DECL
1590 && TREE_STATIC (base))
1592 struct cgraph_node *node = cgraph_get_node (callee);
1593 bitmap not_read;
1595 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1596 node yet. We should enforce that there are nodes for all decls in the
1597 IL and remove this check instead. */
1598 if (node
1599 && (not_read = ipa_reference_get_not_read_global (node))
1600 && bitmap_bit_p (not_read, DECL_UID (base)))
1601 goto process_args;
1604 /* Check if the base variable is call-used. */
1605 if (DECL_P (base))
1607 if (pt_solution_includes (gimple_call_use_set (call), base))
1608 return true;
1610 else if ((TREE_CODE (base) == MEM_REF
1611 || TREE_CODE (base) == TARGET_MEM_REF)
1612 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1614 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1615 if (!pi)
1616 return true;
1618 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1619 return true;
1621 else
1622 return true;
1624 /* Inspect call arguments for passed-by-value aliases. */
1625 process_args:
1626 for (i = 0; i < gimple_call_num_args (call); ++i)
1628 tree op = gimple_call_arg (call, i);
1629 int flags = gimple_call_arg_flags (call, i);
1631 if (flags & EAF_UNUSED)
1632 continue;
1634 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1635 op = TREE_OPERAND (op, 0);
1637 if (TREE_CODE (op) != SSA_NAME
1638 && !is_gimple_min_invariant (op))
1640 ao_ref r;
1641 ao_ref_init (&r, op);
1642 if (refs_may_alias_p_1 (&r, ref, true))
1643 return true;
1647 return false;
1650 static bool
1651 ref_maybe_used_by_call_p (gimple call, tree ref)
1653 ao_ref r;
1654 bool res;
1655 ao_ref_init (&r, ref);
1656 res = ref_maybe_used_by_call_p_1 (call, &r);
1657 if (res)
1658 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1659 else
1660 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1661 return res;
1665 /* If the statement STMT may use the memory reference REF return
1666 true, otherwise return false. */
1668 bool
1669 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1671 if (is_gimple_assign (stmt))
1673 tree rhs;
1675 /* All memory assign statements are single. */
1676 if (!gimple_assign_single_p (stmt))
1677 return false;
1679 rhs = gimple_assign_rhs1 (stmt);
1680 if (is_gimple_reg (rhs)
1681 || is_gimple_min_invariant (rhs)
1682 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1683 return false;
1685 return refs_may_alias_p (rhs, ref);
1687 else if (is_gimple_call (stmt))
1688 return ref_maybe_used_by_call_p (stmt, ref);
1689 else if (gimple_code (stmt) == GIMPLE_RETURN)
1691 tree retval = gimple_return_retval (stmt);
1692 tree base;
1693 if (retval
1694 && TREE_CODE (retval) != SSA_NAME
1695 && !is_gimple_min_invariant (retval)
1696 && refs_may_alias_p (retval, ref))
1697 return true;
1698 /* If ref escapes the function then the return acts as a use. */
1699 base = get_base_address (ref);
1700 if (!base)
1702 else if (DECL_P (base))
1703 return is_global_var (base);
1704 else if (TREE_CODE (base) == MEM_REF
1705 || TREE_CODE (base) == TARGET_MEM_REF)
1706 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1707 return false;
1710 return true;
1713 /* If the call in statement CALL may clobber the memory reference REF
1714 return true, otherwise return false. */
1716 static bool
1717 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1719 tree base;
1720 tree callee;
1722 /* If the call is pure or const it cannot clobber anything. */
1723 if (gimple_call_flags (call)
1724 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1725 return false;
1727 base = ao_ref_base (ref);
1728 if (!base)
1729 return true;
1731 if (TREE_CODE (base) == SSA_NAME
1732 || CONSTANT_CLASS_P (base))
1733 return false;
1735 /* A call that is not without side-effects might involve volatile
1736 accesses and thus conflicts with all other volatile accesses. */
1737 if (ref->volatile_p)
1738 return true;
1740 /* If the reference is based on a decl that is not aliased the call
1741 cannot possibly clobber it. */
1742 if (DECL_P (base)
1743 && !may_be_aliased (base)
1744 /* But local non-readonly statics can be modified through recursion
1745 or the call may implement a threading barrier which we must
1746 treat as may-def. */
1747 && (TREE_READONLY (base)
1748 || !is_global_var (base)))
1749 return false;
1751 callee = gimple_call_fndecl (call);
1753 /* Handle those builtin functions explicitly that do not act as
1754 escape points. See tree-ssa-structalias.c:find_func_aliases
1755 for the list of builtins we might need to handle here. */
1756 if (callee != NULL_TREE
1757 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1758 switch (DECL_FUNCTION_CODE (callee))
1760 /* All the following functions clobber memory pointed to by
1761 their first argument. */
1762 case BUILT_IN_STRCPY:
1763 case BUILT_IN_STRNCPY:
1764 case BUILT_IN_MEMCPY:
1765 case BUILT_IN_MEMMOVE:
1766 case BUILT_IN_MEMPCPY:
1767 case BUILT_IN_STPCPY:
1768 case BUILT_IN_STPNCPY:
1769 case BUILT_IN_STRCAT:
1770 case BUILT_IN_STRNCAT:
1771 case BUILT_IN_MEMSET:
1772 case BUILT_IN_TM_MEMSET:
1773 CASE_BUILT_IN_TM_STORE (1):
1774 CASE_BUILT_IN_TM_STORE (2):
1775 CASE_BUILT_IN_TM_STORE (4):
1776 CASE_BUILT_IN_TM_STORE (8):
1777 CASE_BUILT_IN_TM_STORE (FLOAT):
1778 CASE_BUILT_IN_TM_STORE (DOUBLE):
1779 CASE_BUILT_IN_TM_STORE (LDOUBLE):
1780 CASE_BUILT_IN_TM_STORE (M64):
1781 CASE_BUILT_IN_TM_STORE (M128):
1782 CASE_BUILT_IN_TM_STORE (M256):
1783 case BUILT_IN_TM_MEMCPY:
1784 case BUILT_IN_TM_MEMMOVE:
1786 ao_ref dref;
1787 tree size = NULL_TREE;
1788 /* Don't pass in size for strncat, as the maximum size
1789 is strlen (dest) + n + 1 instead of n, resp.
1790 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1791 known. */
1792 if (gimple_call_num_args (call) == 3
1793 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1794 size = gimple_call_arg (call, 2);
1795 ao_ref_init_from_ptr_and_size (&dref,
1796 gimple_call_arg (call, 0),
1797 size);
1798 return refs_may_alias_p_1 (&dref, ref, false);
1800 case BUILT_IN_STRCPY_CHK:
1801 case BUILT_IN_STRNCPY_CHK:
1802 case BUILT_IN_MEMCPY_CHK:
1803 case BUILT_IN_MEMMOVE_CHK:
1804 case BUILT_IN_MEMPCPY_CHK:
1805 case BUILT_IN_STPCPY_CHK:
1806 case BUILT_IN_STPNCPY_CHK:
1807 case BUILT_IN_STRCAT_CHK:
1808 case BUILT_IN_STRNCAT_CHK:
1809 case BUILT_IN_MEMSET_CHK:
1811 ao_ref dref;
1812 tree size = NULL_TREE;
1813 /* Don't pass in size for __strncat_chk, as the maximum size
1814 is strlen (dest) + n + 1 instead of n, resp.
1815 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1816 known. */
1817 if (gimple_call_num_args (call) == 4
1818 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1819 size = gimple_call_arg (call, 2);
1820 ao_ref_init_from_ptr_and_size (&dref,
1821 gimple_call_arg (call, 0),
1822 size);
1823 return refs_may_alias_p_1 (&dref, ref, false);
1825 case BUILT_IN_BCOPY:
1827 ao_ref dref;
1828 tree size = gimple_call_arg (call, 2);
1829 ao_ref_init_from_ptr_and_size (&dref,
1830 gimple_call_arg (call, 1),
1831 size);
1832 return refs_may_alias_p_1 (&dref, ref, false);
1834 /* Allocating memory does not have any side-effects apart from
1835 being the definition point for the pointer. */
1836 case BUILT_IN_MALLOC:
1837 case BUILT_IN_CALLOC:
1838 case BUILT_IN_STRDUP:
1839 case BUILT_IN_STRNDUP:
1840 /* Unix98 specifies that errno is set on allocation failure. */
1841 if (flag_errno_math
1842 && targetm.ref_may_alias_errno (ref))
1843 return true;
1844 return false;
1845 case BUILT_IN_STACK_SAVE:
1846 case BUILT_IN_ALLOCA:
1847 case BUILT_IN_ALLOCA_WITH_ALIGN:
1848 case BUILT_IN_ASSUME_ALIGNED:
1849 return false;
1850 /* But posix_memalign stores a pointer into the memory pointed to
1851 by its first argument. */
1852 case BUILT_IN_POSIX_MEMALIGN:
1854 tree ptrptr = gimple_call_arg (call, 0);
1855 ao_ref dref;
1856 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
1857 TYPE_SIZE_UNIT (ptr_type_node));
1858 return (refs_may_alias_p_1 (&dref, ref, false)
1859 || (flag_errno_math
1860 && targetm.ref_may_alias_errno (ref)));
1862 /* Freeing memory kills the pointed-to memory. More importantly
1863 the call has to serve as a barrier for moving loads and stores
1864 across it. */
1865 case BUILT_IN_FREE:
1866 case BUILT_IN_VA_END:
1868 tree ptr = gimple_call_arg (call, 0);
1869 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1871 case BUILT_IN_GAMMA_R:
1872 case BUILT_IN_GAMMAF_R:
1873 case BUILT_IN_GAMMAL_R:
1874 case BUILT_IN_LGAMMA_R:
1875 case BUILT_IN_LGAMMAF_R:
1876 case BUILT_IN_LGAMMAL_R:
1878 tree out = gimple_call_arg (call, 1);
1879 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1880 return true;
1881 if (flag_errno_math)
1882 break;
1883 return false;
1885 case BUILT_IN_FREXP:
1886 case BUILT_IN_FREXPF:
1887 case BUILT_IN_FREXPL:
1888 case BUILT_IN_MODF:
1889 case BUILT_IN_MODFF:
1890 case BUILT_IN_MODFL:
1892 tree out = gimple_call_arg (call, 1);
1893 return ptr_deref_may_alias_ref_p_1 (out, ref);
1895 case BUILT_IN_REMQUO:
1896 case BUILT_IN_REMQUOF:
1897 case BUILT_IN_REMQUOL:
1899 tree out = gimple_call_arg (call, 2);
1900 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1901 return true;
1902 if (flag_errno_math)
1903 break;
1904 return false;
1906 case BUILT_IN_SINCOS:
1907 case BUILT_IN_SINCOSF:
1908 case BUILT_IN_SINCOSL:
1910 tree sin = gimple_call_arg (call, 1);
1911 tree cos = gimple_call_arg (call, 2);
1912 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1913 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1915 /* __sync_* builtins and some OpenMP builtins act as threading
1916 barriers. */
1917 #undef DEF_SYNC_BUILTIN
1918 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1919 #include "sync-builtins.def"
1920 #undef DEF_SYNC_BUILTIN
1921 case BUILT_IN_GOMP_ATOMIC_START:
1922 case BUILT_IN_GOMP_ATOMIC_END:
1923 case BUILT_IN_GOMP_BARRIER:
1924 case BUILT_IN_GOMP_BARRIER_CANCEL:
1925 case BUILT_IN_GOMP_TASKWAIT:
1926 case BUILT_IN_GOMP_TASKGROUP_END:
1927 case BUILT_IN_GOMP_CRITICAL_START:
1928 case BUILT_IN_GOMP_CRITICAL_END:
1929 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1930 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1931 case BUILT_IN_GOMP_LOOP_END:
1932 case BUILT_IN_GOMP_LOOP_END_CANCEL:
1933 case BUILT_IN_GOMP_ORDERED_START:
1934 case BUILT_IN_GOMP_ORDERED_END:
1935 case BUILT_IN_GOMP_SECTIONS_END:
1936 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1937 case BUILT_IN_GOMP_SINGLE_COPY_START:
1938 case BUILT_IN_GOMP_SINGLE_COPY_END:
1939 return true;
1940 default:
1941 /* Fallthru to general call handling. */;
1944 /* Check if base is a global static variable that is not written
1945 by the function. */
1946 if (callee != NULL_TREE
1947 && TREE_CODE (base) == VAR_DECL
1948 && TREE_STATIC (base))
1950 struct cgraph_node *node = cgraph_get_node (callee);
1951 bitmap not_written;
1953 if (node
1954 && (not_written = ipa_reference_get_not_written_global (node))
1955 && bitmap_bit_p (not_written, DECL_UID (base)))
1956 return false;
1959 /* Check if the base variable is call-clobbered. */
1960 if (DECL_P (base))
1961 return pt_solution_includes (gimple_call_clobber_set (call), base);
1962 else if ((TREE_CODE (base) == MEM_REF
1963 || TREE_CODE (base) == TARGET_MEM_REF)
1964 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1966 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1967 if (!pi)
1968 return true;
1970 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1973 return true;
1976 /* If the call in statement CALL may clobber the memory reference REF
1977 return true, otherwise return false. */
1979 bool
1980 call_may_clobber_ref_p (gimple call, tree ref)
1982 bool res;
1983 ao_ref r;
1984 ao_ref_init (&r, ref);
1985 res = call_may_clobber_ref_p_1 (call, &r);
1986 if (res)
1987 ++alias_stats.call_may_clobber_ref_p_may_alias;
1988 else
1989 ++alias_stats.call_may_clobber_ref_p_no_alias;
1990 return res;
1994 /* If the statement STMT may clobber the memory reference REF return true,
1995 otherwise return false. */
1997 bool
1998 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
2000 if (is_gimple_call (stmt))
2002 tree lhs = gimple_call_lhs (stmt);
2003 if (lhs
2004 && TREE_CODE (lhs) != SSA_NAME)
2006 ao_ref r;
2007 ao_ref_init (&r, lhs);
2008 if (refs_may_alias_p_1 (ref, &r, true))
2009 return true;
2012 return call_may_clobber_ref_p_1 (stmt, ref);
2014 else if (gimple_assign_single_p (stmt))
2016 tree lhs = gimple_assign_lhs (stmt);
2017 if (TREE_CODE (lhs) != SSA_NAME)
2019 ao_ref r;
2020 ao_ref_init (&r, lhs);
2021 return refs_may_alias_p_1 (ref, &r, true);
2024 else if (gimple_code (stmt) == GIMPLE_ASM)
2025 return true;
2027 return false;
2030 bool
2031 stmt_may_clobber_ref_p (gimple stmt, tree ref)
2033 ao_ref r;
2034 ao_ref_init (&r, ref);
2035 return stmt_may_clobber_ref_p_1 (stmt, &r);
2038 /* If STMT kills the memory reference REF return true, otherwise
2039 return false. */
2041 static bool
2042 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
2044 /* For a must-alias check we need to be able to constrain
2045 the access properly.
2046 FIXME: except for BUILTIN_FREE. */
2047 if (!ao_ref_base (ref)
2048 || ref->max_size == -1)
2049 return false;
2051 if (gimple_has_lhs (stmt)
2052 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2053 /* The assignment is not necessarily carried out if it can throw
2054 and we can catch it in the current function where we could inspect
2055 the previous value.
2056 ??? We only need to care about the RHS throwing. For aggregate
2057 assignments or similar calls and non-call exceptions the LHS
2058 might throw as well. */
2059 && !stmt_can_throw_internal (stmt))
2061 tree base, lhs = gimple_get_lhs (stmt);
2062 HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2063 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2064 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2065 so base == ref->base does not always hold. */
2066 if (base != ref->base)
2068 /* If both base and ref->base are MEM_REFs, only compare the
2069 first operand, and if the second operand isn't equal constant,
2070 try to add the offsets into offset and ref_offset. */
2071 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2072 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2074 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2075 TREE_OPERAND (ref->base, 1)))
2077 double_int off1 = mem_ref_offset (base);
2078 off1 = off1.lshift (BITS_PER_UNIT == 8
2079 ? 3 : exact_log2 (BITS_PER_UNIT));
2080 off1 = off1 + double_int::from_shwi (offset);
2081 double_int off2 = mem_ref_offset (ref->base);
2082 off2 = off2.lshift (BITS_PER_UNIT == 8
2083 ? 3 : exact_log2 (BITS_PER_UNIT));
2084 off2 = off2 + double_int::from_shwi (ref_offset);
2085 if (off1.fits_shwi () && off2.fits_shwi ())
2087 offset = off1.to_shwi ();
2088 ref_offset = off2.to_shwi ();
2090 else
2091 size = -1;
2094 else
2095 size = -1;
2097 /* For a must-alias check we need to be able to constrain
2098 the access properly. */
2099 if (size != -1 && size == max_size)
2101 if (offset <= ref_offset
2102 && offset + size >= ref_offset + ref->max_size)
2103 return true;
2107 if (is_gimple_call (stmt))
2109 tree callee = gimple_call_fndecl (stmt);
2110 if (callee != NULL_TREE
2111 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
2112 switch (DECL_FUNCTION_CODE (callee))
2114 case BUILT_IN_FREE:
2116 tree ptr = gimple_call_arg (stmt, 0);
2117 tree base = ao_ref_base (ref);
2118 if (base && TREE_CODE (base) == MEM_REF
2119 && TREE_OPERAND (base, 0) == ptr)
2120 return true;
2121 break;
2124 case BUILT_IN_MEMCPY:
2125 case BUILT_IN_MEMPCPY:
2126 case BUILT_IN_MEMMOVE:
2127 case BUILT_IN_MEMSET:
2128 case BUILT_IN_MEMCPY_CHK:
2129 case BUILT_IN_MEMPCPY_CHK:
2130 case BUILT_IN_MEMMOVE_CHK:
2131 case BUILT_IN_MEMSET_CHK:
2133 tree dest = gimple_call_arg (stmt, 0);
2134 tree len = gimple_call_arg (stmt, 2);
2135 if (!tree_fits_shwi_p (len))
2136 return false;
2137 tree rbase = ref->base;
2138 double_int roffset = double_int::from_shwi (ref->offset);
2139 ao_ref dref;
2140 ao_ref_init_from_ptr_and_size (&dref, dest, len);
2141 tree base = ao_ref_base (&dref);
2142 double_int offset = double_int::from_shwi (dref.offset);
2143 double_int bpu = double_int::from_uhwi (BITS_PER_UNIT);
2144 if (!base || dref.size == -1)
2145 return false;
2146 if (TREE_CODE (base) == MEM_REF)
2148 if (TREE_CODE (rbase) != MEM_REF)
2149 return false;
2150 // Compare pointers.
2151 offset += bpu * mem_ref_offset (base);
2152 roffset += bpu * mem_ref_offset (rbase);
2153 base = TREE_OPERAND (base, 0);
2154 rbase = TREE_OPERAND (rbase, 0);
2156 if (base == rbase)
2158 double_int size = bpu * tree_to_double_int (len);
2159 double_int rsize = double_int::from_uhwi (ref->max_size);
2160 if (offset.sle (roffset)
2161 && (roffset + rsize).sle (offset + size))
2162 return true;
2164 break;
2167 case BUILT_IN_VA_END:
2169 tree ptr = gimple_call_arg (stmt, 0);
2170 if (TREE_CODE (ptr) == ADDR_EXPR)
2172 tree base = ao_ref_base (ref);
2173 if (TREE_OPERAND (ptr, 0) == base)
2174 return true;
2176 break;
2179 default:;
2182 return false;
2185 bool
2186 stmt_kills_ref_p (gimple stmt, tree ref)
2188 ao_ref r;
2189 ao_ref_init (&r, ref);
2190 return stmt_kills_ref_p_1 (stmt, &r);
2194 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2195 TARGET or a statement clobbering the memory reference REF in which
2196 case false is returned. The walk starts with VUSE, one argument of PHI. */
2198 static bool
2199 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
2200 tree vuse, unsigned int *cnt, bitmap *visited,
2201 bool abort_on_visited)
2203 basic_block bb = gimple_bb (phi);
2205 if (!*visited)
2206 *visited = BITMAP_ALLOC (NULL);
2208 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2210 /* Walk until we hit the target. */
2211 while (vuse != target)
2213 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
2214 /* Recurse for PHI nodes. */
2215 if (gimple_code (def_stmt) == GIMPLE_PHI)
2217 /* An already visited PHI node ends the walk successfully. */
2218 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2219 return !abort_on_visited;
2220 vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2221 visited, abort_on_visited);
2222 if (!vuse)
2223 return false;
2224 continue;
2226 else if (gimple_nop_p (def_stmt))
2227 return false;
2228 else
2230 /* A clobbering statement or the end of the IL ends it failing. */
2231 ++*cnt;
2232 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2233 return false;
2235 /* If we reach a new basic-block see if we already skipped it
2236 in a previous walk that ended successfully. */
2237 if (gimple_bb (def_stmt) != bb)
2239 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2240 return !abort_on_visited;
2241 bb = gimple_bb (def_stmt);
2243 vuse = gimple_vuse (def_stmt);
2245 return true;
2248 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2249 until we hit the phi argument definition that dominates the other one.
2250 Return that, or NULL_TREE if there is no such definition. */
2252 static tree
2253 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
2254 ao_ref *ref, unsigned int *cnt,
2255 bitmap *visited, bool abort_on_visited)
2257 gimple def0 = SSA_NAME_DEF_STMT (arg0);
2258 gimple def1 = SSA_NAME_DEF_STMT (arg1);
2259 tree common_vuse;
2261 if (arg0 == arg1)
2262 return arg0;
2263 else if (gimple_nop_p (def0)
2264 || (!gimple_nop_p (def1)
2265 && dominated_by_p (CDI_DOMINATORS,
2266 gimple_bb (def1), gimple_bb (def0))))
2268 if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2269 visited, abort_on_visited))
2270 return arg0;
2272 else if (gimple_nop_p (def1)
2273 || dominated_by_p (CDI_DOMINATORS,
2274 gimple_bb (def0), gimple_bb (def1)))
2276 if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2277 visited, abort_on_visited))
2278 return arg1;
2280 /* Special case of a diamond:
2281 MEM_1 = ...
2282 goto (cond) ? L1 : L2
2283 L1: store1 = ... #MEM_2 = vuse(MEM_1)
2284 goto L3
2285 L2: store2 = ... #MEM_3 = vuse(MEM_1)
2286 L3: MEM_4 = PHI<MEM_2, MEM_3>
2287 We were called with the PHI at L3, MEM_2 and MEM_3 don't
2288 dominate each other, but still we can easily skip this PHI node
2289 if we recognize that the vuse MEM operand is the same for both,
2290 and that we can skip both statements (they don't clobber us).
2291 This is still linear. Don't use maybe_skip_until, that might
2292 potentially be slow. */
2293 else if ((common_vuse = gimple_vuse (def0))
2294 && common_vuse == gimple_vuse (def1))
2296 *cnt += 2;
2297 if (!stmt_may_clobber_ref_p_1 (def0, ref)
2298 && !stmt_may_clobber_ref_p_1 (def1, ref))
2299 return common_vuse;
2302 return NULL_TREE;
2306 /* Starting from a PHI node for the virtual operand of the memory reference
2307 REF find a continuation virtual operand that allows to continue walking
2308 statements dominating PHI skipping only statements that cannot possibly
2309 clobber REF. Increments *CNT for each alias disambiguation done.
2310 Returns NULL_TREE if no suitable virtual operand can be found. */
2312 tree
2313 get_continuation_for_phi (gimple phi, ao_ref *ref,
2314 unsigned int *cnt, bitmap *visited,
2315 bool abort_on_visited)
2317 unsigned nargs = gimple_phi_num_args (phi);
2319 /* Through a single-argument PHI we can simply look through. */
2320 if (nargs == 1)
2321 return PHI_ARG_DEF (phi, 0);
2323 /* For two or more arguments try to pairwise skip non-aliasing code
2324 until we hit the phi argument definition that dominates the other one. */
2325 else if (nargs >= 2)
2327 tree arg0, arg1;
2328 unsigned i;
2330 /* Find a candidate for the virtual operand which definition
2331 dominates those of all others. */
2332 arg0 = PHI_ARG_DEF (phi, 0);
2333 if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2334 for (i = 1; i < nargs; ++i)
2336 arg1 = PHI_ARG_DEF (phi, i);
2337 if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2339 arg0 = arg1;
2340 break;
2342 if (dominated_by_p (CDI_DOMINATORS,
2343 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2344 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2345 arg0 = arg1;
2348 /* Then pairwise reduce against the found candidate. */
2349 for (i = 0; i < nargs; ++i)
2351 arg1 = PHI_ARG_DEF (phi, i);
2352 arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2353 cnt, visited, abort_on_visited);
2354 if (!arg0)
2355 return NULL_TREE;
2358 return arg0;
2361 return NULL_TREE;
2364 /* Based on the memory reference REF and its virtual use VUSE call
2365 WALKER for each virtual use that is equivalent to VUSE, including VUSE
2366 itself. That is, for each virtual use for which its defining statement
2367 does not clobber REF.
2369 WALKER is called with REF, the current virtual use and DATA. If
2370 WALKER returns non-NULL the walk stops and its result is returned.
2371 At the end of a non-successful walk NULL is returned.
2373 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2374 use which definition is a statement that may clobber REF and DATA.
2375 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2376 If TRANSLATE returns non-NULL the walk stops and its result is returned.
2377 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2378 to adjust REF and *DATA to make that valid.
2380 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
2382 void *
2383 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2384 void *(*walker)(ao_ref *, tree, unsigned int, void *),
2385 void *(*translate)(ao_ref *, tree, void *), void *data)
2387 bitmap visited = NULL;
2388 void *res;
2389 unsigned int cnt = 0;
2390 bool translated = false;
2392 timevar_push (TV_ALIAS_STMT_WALK);
2396 gimple def_stmt;
2398 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2399 res = (*walker) (ref, vuse, cnt, data);
2400 /* Abort walk. */
2401 if (res == (void *)-1)
2403 res = NULL;
2404 break;
2406 /* Lookup succeeded. */
2407 else if (res != NULL)
2408 break;
2410 def_stmt = SSA_NAME_DEF_STMT (vuse);
2411 if (gimple_nop_p (def_stmt))
2412 break;
2413 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2414 vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2415 &visited, translated);
2416 else
2418 cnt++;
2419 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2421 if (!translate)
2422 break;
2423 res = (*translate) (ref, vuse, data);
2424 /* Failed lookup and translation. */
2425 if (res == (void *)-1)
2427 res = NULL;
2428 break;
2430 /* Lookup succeeded. */
2431 else if (res != NULL)
2432 break;
2433 /* Translation succeeded, continue walking. */
2434 translated = true;
2436 vuse = gimple_vuse (def_stmt);
2439 while (vuse);
2441 if (visited)
2442 BITMAP_FREE (visited);
2444 timevar_pop (TV_ALIAS_STMT_WALK);
2446 return res;
2450 /* Based on the memory reference REF call WALKER for each vdef which
2451 defining statement may clobber REF, starting with VDEF. If REF
2452 is NULL_TREE, each defining statement is visited.
2454 WALKER is called with REF, the current vdef and DATA. If WALKER
2455 returns true the walk is stopped, otherwise it continues.
2457 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2458 PHI argument (but only one walk continues on merge points), the
2459 return value is true if any of the walks was successful.
2461 The function returns the number of statements walked. */
2463 static unsigned int
2464 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2465 bool (*walker)(ao_ref *, tree, void *), void *data,
2466 bitmap *visited, unsigned int cnt)
2470 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2472 if (*visited
2473 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2474 return cnt;
2476 if (gimple_nop_p (def_stmt))
2477 return cnt;
2478 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2480 unsigned i;
2481 if (!*visited)
2482 *visited = BITMAP_ALLOC (NULL);
2483 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2484 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2485 walker, data, visited, 0);
2486 return cnt;
2489 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2490 cnt++;
2491 if ((!ref
2492 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2493 && (*walker) (ref, vdef, data))
2494 return cnt;
2496 vdef = gimple_vuse (def_stmt);
2498 while (1);
2501 unsigned int
2502 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2503 bool (*walker)(ao_ref *, tree, void *), void *data,
2504 bitmap *visited)
2506 bitmap local_visited = NULL;
2507 unsigned int ret;
2509 timevar_push (TV_ALIAS_STMT_WALK);
2511 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2512 visited ? visited : &local_visited, 0);
2513 if (local_visited)
2514 BITMAP_FREE (local_visited);
2516 timevar_pop (TV_ALIAS_STMT_WALK);
2518 return ret;