testsuite: Correct vec-rlmi-rlnm.c testsuite expected result
[official-gcc.git] / gcc / tree-ssa-alias.c
blob877e4999f0fced0abc9829435437f7b17fe294da
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2020 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 "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "ssa.h"
31 #include "cgraph.h"
32 #include "tree-pretty-print.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "langhooks.h"
36 #include "dumpfile.h"
37 #include "tree-eh.h"
38 #include "tree-dfa.h"
39 #include "ipa-reference.h"
40 #include "varasm.h"
41 #include "ipa-modref-tree.h"
42 #include "ipa-modref.h"
43 #include "attr-fnspec.h"
44 #include "errors.h"
45 #include "dbgcnt.h"
46 #include "gimple-pretty-print.h"
48 /* Broad overview of how alias analysis on gimple works:
50 Statements clobbering or using memory are linked through the
51 virtual operand factored use-def chain. The virtual operand
52 is unique per function, its symbol is accessible via gimple_vop (cfun).
53 Virtual operands are used for efficiently walking memory statements
54 in the gimple IL and are useful for things like value-numbering as
55 a generation count for memory references.
57 SSA_NAME pointers may have associated points-to information
58 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
59 points-to information is (re-)computed by the TODO_rebuild_alias
60 pass manager todo. Points-to information is also used for more
61 precise tracking of call-clobbered and call-used variables and
62 related disambiguations.
64 This file contains functions for disambiguating memory references,
65 the so called alias-oracle and tools for walking of the gimple IL.
67 The main alias-oracle entry-points are
69 bool stmt_may_clobber_ref_p (gimple *, tree)
71 This function queries if a statement may invalidate (parts of)
72 the memory designated by the reference tree argument.
74 bool ref_maybe_used_by_stmt_p (gimple *, tree)
76 This function queries if a statement may need (parts of) the
77 memory designated by the reference tree argument.
79 There are variants of these functions that only handle the call
80 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
81 Note that these do not disambiguate against a possible call lhs.
83 bool refs_may_alias_p (tree, tree)
85 This function tries to disambiguate two reference trees.
87 bool ptr_deref_may_alias_global_p (tree)
89 This function queries if dereferencing a pointer variable may
90 alias global memory.
92 More low-level disambiguators are available and documented in
93 this file. Low-level disambiguators dealing with points-to
94 information are in tree-ssa-structalias.c. */
96 static int nonoverlapping_refs_since_match_p (tree, tree, tree, tree, bool);
97 static bool nonoverlapping_component_refs_p (const_tree, const_tree);
99 /* Query statistics for the different low-level disambiguators.
100 A high-level query may trigger multiple of them. */
102 static struct {
103 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
104 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
105 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
106 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
107 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
108 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
109 unsigned HOST_WIDE_INT aliasing_component_refs_p_may_alias;
110 unsigned HOST_WIDE_INT aliasing_component_refs_p_no_alias;
111 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_may_alias;
112 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_no_alias;
113 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_may_alias;
114 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_must_overlap;
115 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_no_alias;
116 unsigned HOST_WIDE_INT modref_use_may_alias;
117 unsigned HOST_WIDE_INT modref_use_no_alias;
118 unsigned HOST_WIDE_INT modref_clobber_may_alias;
119 unsigned HOST_WIDE_INT modref_clobber_no_alias;
120 unsigned HOST_WIDE_INT modref_tests;
121 unsigned HOST_WIDE_INT modref_baseptr_tests;
122 } alias_stats;
124 void
125 dump_alias_stats (FILE *s)
127 fprintf (s, "\nAlias oracle query stats:\n");
128 fprintf (s, " refs_may_alias_p: "
129 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
130 HOST_WIDE_INT_PRINT_DEC" queries\n",
131 alias_stats.refs_may_alias_p_no_alias,
132 alias_stats.refs_may_alias_p_no_alias
133 + alias_stats.refs_may_alias_p_may_alias);
134 fprintf (s, " ref_maybe_used_by_call_p: "
135 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
136 HOST_WIDE_INT_PRINT_DEC" queries\n",
137 alias_stats.ref_maybe_used_by_call_p_no_alias,
138 alias_stats.refs_may_alias_p_no_alias
139 + alias_stats.ref_maybe_used_by_call_p_may_alias);
140 fprintf (s, " call_may_clobber_ref_p: "
141 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
142 HOST_WIDE_INT_PRINT_DEC" queries\n",
143 alias_stats.call_may_clobber_ref_p_no_alias,
144 alias_stats.call_may_clobber_ref_p_no_alias
145 + alias_stats.call_may_clobber_ref_p_may_alias);
146 fprintf (s, " nonoverlapping_component_refs_p: "
147 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
148 HOST_WIDE_INT_PRINT_DEC" queries\n",
149 alias_stats.nonoverlapping_component_refs_p_no_alias,
150 alias_stats.nonoverlapping_component_refs_p_no_alias
151 + alias_stats.nonoverlapping_component_refs_p_may_alias);
152 fprintf (s, " nonoverlapping_refs_since_match_p: "
153 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
154 HOST_WIDE_INT_PRINT_DEC" must overlaps, "
155 HOST_WIDE_INT_PRINT_DEC" queries\n",
156 alias_stats.nonoverlapping_refs_since_match_p_no_alias,
157 alias_stats.nonoverlapping_refs_since_match_p_must_overlap,
158 alias_stats.nonoverlapping_refs_since_match_p_no_alias
159 + alias_stats.nonoverlapping_refs_since_match_p_may_alias
160 + alias_stats.nonoverlapping_refs_since_match_p_must_overlap);
161 fprintf (s, " aliasing_component_refs_p: "
162 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
163 HOST_WIDE_INT_PRINT_DEC" queries\n",
164 alias_stats.aliasing_component_refs_p_no_alias,
165 alias_stats.aliasing_component_refs_p_no_alias
166 + alias_stats.aliasing_component_refs_p_may_alias);
167 dump_alias_stats_in_alias_c (s);
168 fprintf (s, "\nModref stats:\n");
169 fprintf (s, " modref use: "
170 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
171 HOST_WIDE_INT_PRINT_DEC" queries\n",
172 alias_stats.modref_use_no_alias,
173 alias_stats.modref_use_no_alias
174 + alias_stats.modref_use_may_alias);
175 fprintf (s, " modref clobber: "
176 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
177 HOST_WIDE_INT_PRINT_DEC" queries\n"
178 " " HOST_WIDE_INT_PRINT_DEC" tbaa queries (%f per modref query)\n"
179 " " HOST_WIDE_INT_PRINT_DEC" base compares (%f per modref query)\n",
180 alias_stats.modref_clobber_no_alias,
181 alias_stats.modref_clobber_no_alias
182 + alias_stats.modref_clobber_may_alias,
183 alias_stats.modref_tests,
184 ((double)alias_stats.modref_tests)
185 / (alias_stats.modref_clobber_no_alias
186 + alias_stats.modref_clobber_may_alias),
187 alias_stats.modref_baseptr_tests,
188 ((double)alias_stats.modref_baseptr_tests)
189 / (alias_stats.modref_clobber_no_alias
190 + alias_stats.modref_clobber_may_alias));
194 /* Return true, if dereferencing PTR may alias with a global variable. */
196 bool
197 ptr_deref_may_alias_global_p (tree ptr)
199 struct ptr_info_def *pi;
201 /* If we end up with a pointer constant here that may point
202 to global memory. */
203 if (TREE_CODE (ptr) != SSA_NAME)
204 return true;
206 pi = SSA_NAME_PTR_INFO (ptr);
208 /* If we do not have points-to information for this variable,
209 we have to punt. */
210 if (!pi)
211 return true;
213 /* ??? This does not use TBAA to prune globals ptr may not access. */
214 return pt_solution_includes_global (&pi->pt);
217 /* Return true if dereferencing PTR may alias DECL.
218 The caller is responsible for applying TBAA to see if PTR
219 may access DECL at all. */
221 static bool
222 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
224 struct ptr_info_def *pi;
226 /* Conversions are irrelevant for points-to information and
227 data-dependence analysis can feed us those. */
228 STRIP_NOPS (ptr);
230 /* Anything we do not explicilty handle aliases. */
231 if ((TREE_CODE (ptr) != SSA_NAME
232 && TREE_CODE (ptr) != ADDR_EXPR
233 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
234 || !POINTER_TYPE_P (TREE_TYPE (ptr))
235 || (!VAR_P (decl)
236 && TREE_CODE (decl) != PARM_DECL
237 && TREE_CODE (decl) != RESULT_DECL))
238 return true;
240 /* Disregard pointer offsetting. */
241 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
245 ptr = TREE_OPERAND (ptr, 0);
247 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
248 return ptr_deref_may_alias_decl_p (ptr, decl);
251 /* ADDR_EXPR pointers either just offset another pointer or directly
252 specify the pointed-to set. */
253 if (TREE_CODE (ptr) == ADDR_EXPR)
255 tree base = get_base_address (TREE_OPERAND (ptr, 0));
256 if (base
257 && (TREE_CODE (base) == MEM_REF
258 || TREE_CODE (base) == TARGET_MEM_REF))
259 ptr = TREE_OPERAND (base, 0);
260 else if (base
261 && DECL_P (base))
262 return compare_base_decls (base, decl) != 0;
263 else if (base
264 && CONSTANT_CLASS_P (base))
265 return false;
266 else
267 return true;
270 /* Non-aliased variables cannot be pointed to. */
271 if (!may_be_aliased (decl))
272 return false;
274 /* If we do not have useful points-to information for this pointer
275 we cannot disambiguate anything else. */
276 pi = SSA_NAME_PTR_INFO (ptr);
277 if (!pi)
278 return true;
280 return pt_solution_includes (&pi->pt, decl);
283 /* Return true if dereferenced PTR1 and PTR2 may alias.
284 The caller is responsible for applying TBAA to see if accesses
285 through PTR1 and PTR2 may conflict at all. */
287 bool
288 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
290 struct ptr_info_def *pi1, *pi2;
292 /* Conversions are irrelevant for points-to information and
293 data-dependence analysis can feed us those. */
294 STRIP_NOPS (ptr1);
295 STRIP_NOPS (ptr2);
297 /* Disregard pointer offsetting. */
298 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
302 ptr1 = TREE_OPERAND (ptr1, 0);
304 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
305 return ptr_derefs_may_alias_p (ptr1, ptr2);
307 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
311 ptr2 = TREE_OPERAND (ptr2, 0);
313 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
314 return ptr_derefs_may_alias_p (ptr1, ptr2);
317 /* ADDR_EXPR pointers either just offset another pointer or directly
318 specify the pointed-to set. */
319 if (TREE_CODE (ptr1) == ADDR_EXPR)
321 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
322 if (base
323 && (TREE_CODE (base) == MEM_REF
324 || TREE_CODE (base) == TARGET_MEM_REF))
325 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
326 else if (base
327 && DECL_P (base))
328 return ptr_deref_may_alias_decl_p (ptr2, base);
329 else
330 return true;
332 if (TREE_CODE (ptr2) == ADDR_EXPR)
334 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
335 if (base
336 && (TREE_CODE (base) == MEM_REF
337 || TREE_CODE (base) == TARGET_MEM_REF))
338 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
339 else if (base
340 && DECL_P (base))
341 return ptr_deref_may_alias_decl_p (ptr1, base);
342 else
343 return true;
346 /* From here we require SSA name pointers. Anything else aliases. */
347 if (TREE_CODE (ptr1) != SSA_NAME
348 || TREE_CODE (ptr2) != SSA_NAME
349 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
350 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
351 return true;
353 /* We may end up with two empty points-to solutions for two same pointers.
354 In this case we still want to say both pointers alias, so shortcut
355 that here. */
356 if (ptr1 == ptr2)
357 return true;
359 /* If we do not have useful points-to information for either pointer
360 we cannot disambiguate anything else. */
361 pi1 = SSA_NAME_PTR_INFO (ptr1);
362 pi2 = SSA_NAME_PTR_INFO (ptr2);
363 if (!pi1 || !pi2)
364 return true;
366 /* ??? This does not use TBAA to prune decls from the intersection
367 that not both pointers may access. */
368 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
371 /* Return true if dereferencing PTR may alias *REF.
372 The caller is responsible for applying TBAA to see if PTR
373 may access *REF at all. */
375 static bool
376 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
378 tree base = ao_ref_base (ref);
380 if (TREE_CODE (base) == MEM_REF
381 || TREE_CODE (base) == TARGET_MEM_REF)
382 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
383 else if (DECL_P (base))
384 return ptr_deref_may_alias_decl_p (ptr, base);
386 return true;
389 /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
391 bool
392 ptrs_compare_unequal (tree ptr1, tree ptr2)
394 /* First resolve the pointers down to a SSA name pointer base or
395 a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
396 not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
397 or STRING_CSTs which needs points-to adjustments to track them
398 in the points-to sets. */
399 tree obj1 = NULL_TREE;
400 tree obj2 = NULL_TREE;
401 if (TREE_CODE (ptr1) == ADDR_EXPR)
403 tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
404 if (! tem)
405 return false;
406 if (VAR_P (tem)
407 || TREE_CODE (tem) == PARM_DECL
408 || TREE_CODE (tem) == RESULT_DECL)
409 obj1 = tem;
410 else if (TREE_CODE (tem) == MEM_REF)
411 ptr1 = TREE_OPERAND (tem, 0);
413 if (TREE_CODE (ptr2) == ADDR_EXPR)
415 tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
416 if (! tem)
417 return false;
418 if (VAR_P (tem)
419 || TREE_CODE (tem) == PARM_DECL
420 || TREE_CODE (tem) == RESULT_DECL)
421 obj2 = tem;
422 else if (TREE_CODE (tem) == MEM_REF)
423 ptr2 = TREE_OPERAND (tem, 0);
426 /* Canonicalize ptr vs. object. */
427 if (TREE_CODE (ptr1) == SSA_NAME && obj2)
429 std::swap (ptr1, ptr2);
430 std::swap (obj1, obj2);
433 if (obj1 && obj2)
434 /* Other code handles this correctly, no need to duplicate it here. */;
435 else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
437 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
438 /* We may not use restrict to optimize pointer comparisons.
439 See PR71062. So we have to assume that restrict-pointed-to
440 may be in fact obj1. */
441 if (!pi
442 || pi->pt.vars_contains_restrict
443 || pi->pt.vars_contains_interposable)
444 return false;
445 if (VAR_P (obj1)
446 && (TREE_STATIC (obj1) || DECL_EXTERNAL (obj1)))
448 varpool_node *node = varpool_node::get (obj1);
449 /* If obj1 may bind to NULL give up (see below). */
450 if (! node
451 || ! node->nonzero_address ()
452 || ! decl_binds_to_current_def_p (obj1))
453 return false;
455 return !pt_solution_includes (&pi->pt, obj1);
458 /* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
459 but those require pt.null to be conservatively correct. */
461 return false;
464 /* Returns whether reference REF to BASE may refer to global memory. */
466 static bool
467 ref_may_alias_global_p_1 (tree base)
469 if (DECL_P (base))
470 return is_global_var (base);
471 else if (TREE_CODE (base) == MEM_REF
472 || TREE_CODE (base) == TARGET_MEM_REF)
473 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
474 return true;
477 bool
478 ref_may_alias_global_p (ao_ref *ref)
480 tree base = ao_ref_base (ref);
481 return ref_may_alias_global_p_1 (base);
484 bool
485 ref_may_alias_global_p (tree ref)
487 tree base = get_base_address (ref);
488 return ref_may_alias_global_p_1 (base);
491 /* Return true whether STMT may clobber global memory. */
493 bool
494 stmt_may_clobber_global_p (gimple *stmt)
496 tree lhs;
498 if (!gimple_vdef (stmt))
499 return false;
501 /* ??? We can ask the oracle whether an artificial pointer
502 dereference with a pointer with points-to information covering
503 all global memory (what about non-address taken memory?) maybe
504 clobbered by this call. As there is at the moment no convenient
505 way of doing that without generating garbage do some manual
506 checking instead.
507 ??? We could make a NULL ao_ref argument to the various
508 predicates special, meaning any global memory. */
510 switch (gimple_code (stmt))
512 case GIMPLE_ASSIGN:
513 lhs = gimple_assign_lhs (stmt);
514 return (TREE_CODE (lhs) != SSA_NAME
515 && ref_may_alias_global_p (lhs));
516 case GIMPLE_CALL:
517 return true;
518 default:
519 return true;
524 /* Dump alias information on FILE. */
526 void
527 dump_alias_info (FILE *file)
529 unsigned i;
530 tree ptr;
531 const char *funcname
532 = lang_hooks.decl_printable_name (current_function_decl, 2);
533 tree var;
535 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
537 fprintf (file, "Aliased symbols\n\n");
539 FOR_EACH_LOCAL_DECL (cfun, i, var)
541 if (may_be_aliased (var))
542 dump_variable (file, var);
545 fprintf (file, "\nCall clobber information\n");
547 fprintf (file, "\nESCAPED");
548 dump_points_to_solution (file, &cfun->gimple_df->escaped);
550 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
552 FOR_EACH_SSA_NAME (i, ptr, cfun)
554 struct ptr_info_def *pi;
556 if (!POINTER_TYPE_P (TREE_TYPE (ptr))
557 || SSA_NAME_IN_FREE_LIST (ptr))
558 continue;
560 pi = SSA_NAME_PTR_INFO (ptr);
561 if (pi)
562 dump_points_to_info_for (file, ptr);
565 fprintf (file, "\n");
569 /* Dump alias information on stderr. */
571 DEBUG_FUNCTION void
572 debug_alias_info (void)
574 dump_alias_info (stderr);
578 /* Dump the points-to set *PT into FILE. */
580 void
581 dump_points_to_solution (FILE *file, struct pt_solution *pt)
583 if (pt->anything)
584 fprintf (file, ", points-to anything");
586 if (pt->nonlocal)
587 fprintf (file, ", points-to non-local");
589 if (pt->escaped)
590 fprintf (file, ", points-to escaped");
592 if (pt->ipa_escaped)
593 fprintf (file, ", points-to unit escaped");
595 if (pt->null)
596 fprintf (file, ", points-to NULL");
598 if (pt->vars)
600 fprintf (file, ", points-to vars: ");
601 dump_decl_set (file, pt->vars);
602 if (pt->vars_contains_nonlocal
603 || pt->vars_contains_escaped
604 || pt->vars_contains_escaped_heap
605 || pt->vars_contains_restrict)
607 const char *comma = "";
608 fprintf (file, " (");
609 if (pt->vars_contains_nonlocal)
611 fprintf (file, "nonlocal");
612 comma = ", ";
614 if (pt->vars_contains_escaped)
616 fprintf (file, "%sescaped", comma);
617 comma = ", ";
619 if (pt->vars_contains_escaped_heap)
621 fprintf (file, "%sescaped heap", comma);
622 comma = ", ";
624 if (pt->vars_contains_restrict)
626 fprintf (file, "%srestrict", comma);
627 comma = ", ";
629 if (pt->vars_contains_interposable)
630 fprintf (file, "%sinterposable", comma);
631 fprintf (file, ")");
637 /* Unified dump function for pt_solution. */
639 DEBUG_FUNCTION void
640 debug (pt_solution &ref)
642 dump_points_to_solution (stderr, &ref);
645 DEBUG_FUNCTION void
646 debug (pt_solution *ptr)
648 if (ptr)
649 debug (*ptr);
650 else
651 fprintf (stderr, "<nil>\n");
655 /* Dump points-to information for SSA_NAME PTR into FILE. */
657 void
658 dump_points_to_info_for (FILE *file, tree ptr)
660 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
662 print_generic_expr (file, ptr, dump_flags);
664 if (pi)
665 dump_points_to_solution (file, &pi->pt);
666 else
667 fprintf (file, ", points-to anything");
669 fprintf (file, "\n");
673 /* Dump points-to information for VAR into stderr. */
675 DEBUG_FUNCTION void
676 debug_points_to_info_for (tree var)
678 dump_points_to_info_for (stderr, var);
682 /* Initializes the alias-oracle reference representation *R from REF. */
684 void
685 ao_ref_init (ao_ref *r, tree ref)
687 r->ref = ref;
688 r->base = NULL_TREE;
689 r->offset = 0;
690 r->size = -1;
691 r->max_size = -1;
692 r->ref_alias_set = -1;
693 r->base_alias_set = -1;
694 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
697 /* Returns the base object of the memory reference *REF. */
699 tree
700 ao_ref_base (ao_ref *ref)
702 bool reverse;
704 if (ref->base)
705 return ref->base;
706 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
707 &ref->max_size, &reverse);
708 return ref->base;
711 /* Returns the base object alias set of the memory reference *REF. */
713 alias_set_type
714 ao_ref_base_alias_set (ao_ref *ref)
716 tree base_ref;
717 if (ref->base_alias_set != -1)
718 return ref->base_alias_set;
719 if (!ref->ref)
720 return 0;
721 base_ref = ref->ref;
722 while (handled_component_p (base_ref))
723 base_ref = TREE_OPERAND (base_ref, 0);
724 ref->base_alias_set = get_alias_set (base_ref);
725 return ref->base_alias_set;
728 /* Returns the reference alias set of the memory reference *REF. */
730 alias_set_type
731 ao_ref_alias_set (ao_ref *ref)
733 if (ref->ref_alias_set != -1)
734 return ref->ref_alias_set;
735 if (!ref->ref)
736 return 0;
737 ref->ref_alias_set = get_alias_set (ref->ref);
738 return ref->ref_alias_set;
741 /* Init an alias-oracle reference representation from a gimple pointer
742 PTR a range specified by OFFSET, SIZE and MAX_SIZE under the assumption
743 that RANGE_KNOWN is set.
745 The access is assumed to be only to or after of the pointer target adjusted
746 by the offset, not before it (even in the case RANGE_KNOWN is false). */
748 static void
749 ao_ref_init_from_ptr_and_range (ao_ref *ref, tree ptr,
750 bool range_known,
751 poly_int64 offset,
752 poly_int64 size,
753 poly_int64 max_size)
755 poly_int64 t, extra_offset = 0;
757 ref->ref = NULL_TREE;
758 if (TREE_CODE (ptr) == SSA_NAME)
760 gimple *stmt = SSA_NAME_DEF_STMT (ptr);
761 if (gimple_assign_single_p (stmt)
762 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
763 ptr = gimple_assign_rhs1 (stmt);
764 else if (is_gimple_assign (stmt)
765 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
766 && ptrdiff_tree_p (gimple_assign_rhs2 (stmt), &extra_offset))
768 ptr = gimple_assign_rhs1 (stmt);
769 extra_offset *= BITS_PER_UNIT;
773 if (TREE_CODE (ptr) == ADDR_EXPR)
775 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
776 if (ref->base)
777 ref->offset = BITS_PER_UNIT * t;
778 else
780 range_known = false;
781 ref->offset = 0;
782 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
785 else
787 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
788 ref->base = build2 (MEM_REF, char_type_node,
789 ptr, null_pointer_node);
790 ref->offset = 0;
792 ref->offset += extra_offset + offset;
793 if (range_known)
795 ref->max_size = max_size;
796 ref->size = size;
798 else
799 ref->max_size = ref->size = -1;
800 ref->ref_alias_set = 0;
801 ref->base_alias_set = 0;
802 ref->volatile_p = false;
805 /* Init an alias-oracle reference representation from a gimple pointer
806 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
807 size is assumed to be unknown. The access is assumed to be only
808 to or after of the pointer target, not before it. */
810 void
811 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
813 poly_int64 size_hwi;
814 if (size
815 && poly_int_tree_p (size, &size_hwi)
816 && coeffs_in_range_p (size_hwi, 0, HOST_WIDE_INT_MAX / BITS_PER_UNIT))
818 size_hwi = size_hwi * BITS_PER_UNIT;
819 ao_ref_init_from_ptr_and_range (ref, ptr, true, 0, size_hwi, size_hwi);
821 else
822 ao_ref_init_from_ptr_and_range (ref, ptr, false, 0, -1, -1);
825 /* S1 and S2 are TYPE_SIZE or DECL_SIZE. Compare them:
826 Return -1 if S1 < S2
827 Return 1 if S1 > S2
828 Return 0 if equal or incomparable. */
830 static int
831 compare_sizes (tree s1, tree s2)
833 if (!s1 || !s2)
834 return 0;
836 poly_uint64 size1;
837 poly_uint64 size2;
839 if (!poly_int_tree_p (s1, &size1) || !poly_int_tree_p (s2, &size2))
840 return 0;
841 if (known_lt (size1, size2))
842 return -1;
843 if (known_lt (size2, size1))
844 return 1;
845 return 0;
848 /* Compare TYPE1 and TYPE2 by its size.
849 Return -1 if size of TYPE1 < size of TYPE2
850 Return 1 if size of TYPE1 > size of TYPE2
851 Return 0 if types are of equal sizes or we can not compare them. */
853 static int
854 compare_type_sizes (tree type1, tree type2)
856 /* Be conservative for arrays and vectors. We want to support partial
857 overlap on int[3] and int[3] as tested in gcc.dg/torture/alias-2.c. */
858 while (TREE_CODE (type1) == ARRAY_TYPE
859 || TREE_CODE (type1) == VECTOR_TYPE)
860 type1 = TREE_TYPE (type1);
861 while (TREE_CODE (type2) == ARRAY_TYPE
862 || TREE_CODE (type2) == VECTOR_TYPE)
863 type2 = TREE_TYPE (type2);
864 return compare_sizes (TYPE_SIZE (type1), TYPE_SIZE (type2));
867 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
868 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
869 decide. */
871 static inline int
872 same_type_for_tbaa (tree type1, tree type2)
874 type1 = TYPE_MAIN_VARIANT (type1);
875 type2 = TYPE_MAIN_VARIANT (type2);
877 /* Handle the most common case first. */
878 if (type1 == type2)
879 return 1;
881 /* If we would have to do structural comparison bail out. */
882 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
883 || TYPE_STRUCTURAL_EQUALITY_P (type2))
884 return -1;
886 /* Compare the canonical types. */
887 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
888 return 1;
890 /* ??? Array types are not properly unified in all cases as we have
891 spurious changes in the index types for example. Removing this
892 causes all sorts of problems with the Fortran frontend. */
893 if (TREE_CODE (type1) == ARRAY_TYPE
894 && TREE_CODE (type2) == ARRAY_TYPE)
895 return -1;
897 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
898 object of one of its constrained subtypes, e.g. when a function with an
899 unconstrained parameter passed by reference is called on an object and
900 inlined. But, even in the case of a fixed size, type and subtypes are
901 not equivalent enough as to share the same TYPE_CANONICAL, since this
902 would mean that conversions between them are useless, whereas they are
903 not (e.g. type and subtypes can have different modes). So, in the end,
904 they are only guaranteed to have the same alias set. */
905 alias_set_type set1 = get_alias_set (type1);
906 alias_set_type set2 = get_alias_set (type2);
907 if (set1 == set2)
908 return -1;
910 /* Pointers to void are considered compatible with all other pointers,
911 so for two pointers see what the alias set resolution thinks. */
912 if (POINTER_TYPE_P (type1)
913 && POINTER_TYPE_P (type2)
914 && alias_sets_conflict_p (set1, set2))
915 return -1;
917 /* The types are known to be not equal. */
918 return 0;
921 /* Return true if TYPE is a composite type (i.e. we may apply one of handled
922 components on it). */
924 static bool
925 type_has_components_p (tree type)
927 return AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)
928 || TREE_CODE (type) == COMPLEX_TYPE;
931 /* MATCH1 and MATCH2 which are part of access path of REF1 and REF2
932 respectively are either pointing to same address or are completely
933 disjoint. If PARTIAL_OVERLAP is true, assume that outermost arrays may
934 just partly overlap.
936 Try to disambiguate using the access path starting from the match
937 and return false if there is no conflict.
939 Helper for aliasing_component_refs_p. */
941 static bool
942 aliasing_matching_component_refs_p (tree match1, tree ref1,
943 poly_int64 offset1, poly_int64 max_size1,
944 tree match2, tree ref2,
945 poly_int64 offset2, poly_int64 max_size2,
946 bool partial_overlap)
948 poly_int64 offadj, sztmp, msztmp;
949 bool reverse;
951 if (!partial_overlap)
953 get_ref_base_and_extent (match2, &offadj, &sztmp, &msztmp, &reverse);
954 offset2 -= offadj;
955 get_ref_base_and_extent (match1, &offadj, &sztmp, &msztmp, &reverse);
956 offset1 -= offadj;
957 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
959 ++alias_stats.aliasing_component_refs_p_no_alias;
960 return false;
964 int cmp = nonoverlapping_refs_since_match_p (match1, ref1, match2, ref2,
965 partial_overlap);
966 if (cmp == 1
967 || (cmp == -1 && nonoverlapping_component_refs_p (ref1, ref2)))
969 ++alias_stats.aliasing_component_refs_p_no_alias;
970 return false;
972 ++alias_stats.aliasing_component_refs_p_may_alias;
973 return true;
976 /* Return true if REF is reference to zero sized trailing array. I.e.
977 struct foo {int bar; int array[0];} *fooptr;
978 fooptr->array. */
980 static bool
981 component_ref_to_zero_sized_trailing_array_p (tree ref)
983 return (TREE_CODE (ref) == COMPONENT_REF
984 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE
985 && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))
986 || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))))
987 && array_at_struct_end_p (ref));
990 /* Worker for aliasing_component_refs_p. Most parameters match parameters of
991 aliasing_component_refs_p.
993 Walk access path REF2 and try to find type matching TYPE1
994 (which is a start of possibly aliasing access path REF1).
995 If match is found, try to disambiguate.
997 Return 0 for sucessful disambiguation.
998 Return 1 if match was found but disambiguation failed
999 Return -1 if there is no match.
1000 In this case MAYBE_MATCH is set to 0 if there is no type matching TYPE1
1001 in access patch REF2 and -1 if we are not sure. */
1003 static int
1004 aliasing_component_refs_walk (tree ref1, tree type1, tree base1,
1005 poly_int64 offset1, poly_int64 max_size1,
1006 tree end_struct_ref1,
1007 tree ref2, tree base2,
1008 poly_int64 offset2, poly_int64 max_size2,
1009 bool *maybe_match)
1011 tree ref = ref2;
1012 int same_p = 0;
1014 while (true)
1016 /* We walk from inner type to the outer types. If type we see is
1017 already too large to be part of type1, terminate the search. */
1018 int cmp = compare_type_sizes (type1, TREE_TYPE (ref));
1020 if (cmp < 0
1021 && (!end_struct_ref1
1022 || compare_type_sizes (TREE_TYPE (end_struct_ref1),
1023 TREE_TYPE (ref)) < 0))
1024 break;
1025 /* If types may be of same size, see if we can decide about their
1026 equality. */
1027 if (cmp == 0)
1029 same_p = same_type_for_tbaa (TREE_TYPE (ref), type1);
1030 if (same_p == 1)
1031 break;
1032 /* In case we can't decide whether types are same try to
1033 continue looking for the exact match.
1034 Remember however that we possibly saw a match
1035 to bypass the access path continuations tests we do later. */
1036 if (same_p == -1)
1037 *maybe_match = true;
1039 if (!handled_component_p (ref))
1040 break;
1041 ref = TREE_OPERAND (ref, 0);
1043 if (same_p == 1)
1045 bool partial_overlap = false;
1047 /* We assume that arrays can overlap by multiple of their elements
1048 size as tested in gcc.dg/torture/alias-2.c.
1049 This partial overlap happen only when both arrays are bases of
1050 the access and not contained within another component ref.
1051 To be safe we also assume partial overlap for VLAs. */
1052 if (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
1053 && (!TYPE_SIZE (TREE_TYPE (base1))
1054 || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) != INTEGER_CST
1055 || ref == base2))
1057 /* Setting maybe_match to true triggers
1058 nonoverlapping_component_refs_p test later that still may do
1059 useful disambiguation. */
1060 *maybe_match = true;
1061 partial_overlap = true;
1063 return aliasing_matching_component_refs_p (base1, ref1,
1064 offset1, max_size1,
1065 ref, ref2,
1066 offset2, max_size2,
1067 partial_overlap);
1069 return -1;
1072 /* Consider access path1 base1....ref1 and access path2 base2...ref2.
1073 Return true if they can be composed to single access path
1074 base1...ref1...base2...ref2.
1076 REF_TYPE1 if type of REF1. END_STRUCT_PAST_END1 is true if there is
1077 a trailing array access after REF1 in the non-TBAA part of the access.
1078 REF1_ALIAS_SET is the alias set of REF1.
1080 BASE_TYPE2 is type of base2. END_STRUCT_REF2 is non-NULL if there is
1081 a traling array access in the TBAA part of access path2.
1082 BASE2_ALIAS_SET is the alias set of base2. */
1084 bool
1085 access_path_may_continue_p (tree ref_type1, bool end_struct_past_end1,
1086 alias_set_type ref1_alias_set,
1087 tree base_type2, tree end_struct_ref2,
1088 alias_set_type base2_alias_set)
1090 /* Access path can not continue past types with no components. */
1091 if (!type_has_components_p (ref_type1))
1092 return false;
1094 /* If first access path ends by too small type to hold base of
1095 the second access path, typically paths can not continue.
1097 Punt if end_struct_past_end1 is true. We want to support arbitrary
1098 type puning past first COMPONENT_REF to union because redundant store
1099 elimination depends on this, see PR92152. For this reason we can not
1100 check size of the reference because types may partially overlap. */
1101 if (!end_struct_past_end1)
1103 if (compare_type_sizes (ref_type1, base_type2) < 0)
1104 return false;
1105 /* If the path2 contains trailing array access we can strenghten the check
1106 to verify that also the size of element of the trailing array fits.
1107 In fact we could check for offset + type_size, but we do not track
1108 offsets and this is quite side case. */
1109 if (end_struct_ref2
1110 && compare_type_sizes (ref_type1, TREE_TYPE (end_struct_ref2)) < 0)
1111 return false;
1113 return (base2_alias_set == ref1_alias_set
1114 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
1117 /* Determine if the two component references REF1 and REF2 which are
1118 based on access types TYPE1 and TYPE2 and of which at least one is based
1119 on an indirect reference may alias.
1120 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
1121 are the respective alias sets. */
1123 static bool
1124 aliasing_component_refs_p (tree ref1,
1125 alias_set_type ref1_alias_set,
1126 alias_set_type base1_alias_set,
1127 poly_int64 offset1, poly_int64 max_size1,
1128 tree ref2,
1129 alias_set_type ref2_alias_set,
1130 alias_set_type base2_alias_set,
1131 poly_int64 offset2, poly_int64 max_size2)
1133 /* If one reference is a component references through pointers try to find a
1134 common base and apply offset based disambiguation. This handles
1135 for example
1136 struct A { int i; int j; } *q;
1137 struct B { struct A a; int k; } *p;
1138 disambiguating q->i and p->a.j. */
1139 tree base1, base2;
1140 tree type1, type2;
1141 bool maybe_match = false;
1142 tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
1143 bool end_struct_past_end1 = false;
1144 bool end_struct_past_end2 = false;
1146 /* Choose bases and base types to search for.
1147 The access path is as follows:
1148 base....end_of_tbaa_ref...actual_ref
1149 At one place in the access path may be a reference to zero sized or
1150 trailing array.
1152 We generally discard the segment after end_of_tbaa_ref however
1153 we need to be careful in case it contains zero sized or traling array.
1154 These may happen after refernce to union and in this case we need to
1155 not disambiguate type puning scenarios.
1157 We set:
1158 base1 to point to base
1160 ref1 to point to end_of_tbaa_ref
1162 end_struct_ref1 to point the trailing reference (if it exists
1163 in range base....end_of_tbaa_ref
1165 end_struct_past_end1 is true if this traling refernece occurs in
1166 end_of_tbaa_ref...actual_ref. */
1167 base1 = ref1;
1168 while (handled_component_p (base1))
1170 /* Generally access paths are monotous in the size of object. The
1171 exception are trailing arrays of structures. I.e.
1172 struct a {int array[0];};
1174 struct a {int array1[0]; int array[];};
1175 Such struct has size 0 but accesses to a.array may have non-zero size.
1176 In this case the size of TREE_TYPE (base1) is smaller than
1177 size of TREE_TYPE (TREE_OPERNAD (base1, 0)).
1179 Because we compare sizes of arrays just by sizes of their elements,
1180 we only need to care about zero sized array fields here. */
1181 if (component_ref_to_zero_sized_trailing_array_p (base1))
1183 gcc_checking_assert (!end_struct_ref1);
1184 end_struct_ref1 = base1;
1186 if (ends_tbaa_access_path_p (base1))
1188 ref1 = TREE_OPERAND (base1, 0);
1189 if (end_struct_ref1)
1191 end_struct_past_end1 = true;
1192 end_struct_ref1 = NULL;
1195 base1 = TREE_OPERAND (base1, 0);
1197 type1 = TREE_TYPE (base1);
1198 base2 = ref2;
1199 while (handled_component_p (base2))
1201 if (component_ref_to_zero_sized_trailing_array_p (base2))
1203 gcc_checking_assert (!end_struct_ref2);
1204 end_struct_ref2 = base2;
1206 if (ends_tbaa_access_path_p (base2))
1208 ref2 = TREE_OPERAND (base2, 0);
1209 if (end_struct_ref2)
1211 end_struct_past_end2 = true;
1212 end_struct_ref2 = NULL;
1215 base2 = TREE_OPERAND (base2, 0);
1217 type2 = TREE_TYPE (base2);
1219 /* Now search for the type1 in the access path of ref2. This
1220 would be a common base for doing offset based disambiguation on.
1221 This however only makes sense if type2 is big enough to hold type1. */
1222 int cmp_outer = compare_type_sizes (type2, type1);
1224 /* If type2 is big enough to contain type1 walk its access path.
1225 We also need to care of arrays at the end of structs that may extend
1226 beyond the end of structure. If this occurs in the TBAA part of the
1227 access path, we need to consider the increased type as well. */
1228 if (cmp_outer >= 0
1229 || (end_struct_ref2
1230 && compare_type_sizes (TREE_TYPE (end_struct_ref2), type1) >= 0))
1232 int res = aliasing_component_refs_walk (ref1, type1, base1,
1233 offset1, max_size1,
1234 end_struct_ref1,
1235 ref2, base2, offset2, max_size2,
1236 &maybe_match);
1237 if (res != -1)
1238 return res;
1241 /* If we didn't find a common base, try the other way around. */
1242 if (cmp_outer <= 0
1243 || (end_struct_ref1
1244 && compare_type_sizes (TREE_TYPE (end_struct_ref1), type1) <= 0))
1246 int res = aliasing_component_refs_walk (ref2, type2, base2,
1247 offset2, max_size2,
1248 end_struct_ref2,
1249 ref1, base1, offset1, max_size1,
1250 &maybe_match);
1251 if (res != -1)
1252 return res;
1255 /* In the following code we make an assumption that the types in access
1256 paths do not overlap and thus accesses alias only if one path can be
1257 continuation of another. If we was not able to decide about equivalence,
1258 we need to give up. */
1259 if (maybe_match)
1261 if (!nonoverlapping_component_refs_p (ref1, ref2))
1263 ++alias_stats.aliasing_component_refs_p_may_alias;
1264 return true;
1266 ++alias_stats.aliasing_component_refs_p_no_alias;
1267 return false;
1270 if (access_path_may_continue_p (TREE_TYPE (ref1), end_struct_past_end1,
1271 ref1_alias_set,
1272 type2, end_struct_ref2,
1273 base2_alias_set)
1274 || access_path_may_continue_p (TREE_TYPE (ref2), end_struct_past_end2,
1275 ref2_alias_set,
1276 type1, end_struct_ref1,
1277 base1_alias_set))
1279 ++alias_stats.aliasing_component_refs_p_may_alias;
1280 return true;
1282 ++alias_stats.aliasing_component_refs_p_no_alias;
1283 return false;
1286 /* FIELD1 and FIELD2 are two fields of component refs. We assume
1287 that bases of both component refs are either equivalent or nonoverlapping.
1288 We do not assume that the containers of FIELD1 and FIELD2 are of the
1289 same type or size.
1291 Return 0 in case the base address of component_refs are same then
1292 FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1293 may not be of same type or size.
1295 Return 1 if FIELD1 and FIELD2 are non-overlapping.
1297 Return -1 otherwise.
1299 Main difference between 0 and -1 is to let
1300 nonoverlapping_component_refs_since_match_p discover the semantically
1301 equivalent part of the access path.
1303 Note that this function is used even with -fno-strict-aliasing
1304 and makes use of no TBAA assumptions. */
1306 static int
1307 nonoverlapping_component_refs_p_1 (const_tree field1, const_tree field2)
1309 /* If both fields are of the same type, we could save hard work of
1310 comparing offsets. */
1311 tree type1 = DECL_CONTEXT (field1);
1312 tree type2 = DECL_CONTEXT (field2);
1314 if (TREE_CODE (type1) == RECORD_TYPE
1315 && DECL_BIT_FIELD_REPRESENTATIVE (field1))
1316 field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
1317 if (TREE_CODE (type2) == RECORD_TYPE
1318 && DECL_BIT_FIELD_REPRESENTATIVE (field2))
1319 field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
1321 /* ??? Bitfields can overlap at RTL level so punt on them.
1322 FIXME: RTL expansion should be fixed by adjusting the access path
1323 when producing MEM_ATTRs for MEMs which are wider than
1324 the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1325 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1326 return -1;
1328 /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1329 if (type1 == type2 && TREE_CODE (type1) == RECORD_TYPE)
1330 return field1 != field2;
1332 /* In common case the offsets and bit offsets will be the same.
1333 However if frontends do not agree on the alignment, they may be
1334 different even if they actually represent same address.
1335 Try the common case first and if that fails calcualte the
1336 actual bit offset. */
1337 if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1),
1338 DECL_FIELD_OFFSET (field2))
1339 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1),
1340 DECL_FIELD_BIT_OFFSET (field2)))
1341 return 0;
1343 /* Note that it may be possible to use component_ref_field_offset
1344 which would provide offsets as trees. However constructing and folding
1345 trees is expensive and does not seem to be worth the compile time
1346 cost. */
1348 poly_uint64 offset1, offset2;
1349 poly_uint64 bit_offset1, bit_offset2;
1351 if (poly_int_tree_p (DECL_FIELD_OFFSET (field1), &offset1)
1352 && poly_int_tree_p (DECL_FIELD_OFFSET (field2), &offset2)
1353 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1), &bit_offset1)
1354 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2), &bit_offset2))
1356 offset1 = (offset1 << LOG2_BITS_PER_UNIT) + bit_offset1;
1357 offset2 = (offset2 << LOG2_BITS_PER_UNIT) + bit_offset2;
1359 if (known_eq (offset1, offset2))
1360 return 0;
1362 poly_uint64 size1, size2;
1364 if (poly_int_tree_p (DECL_SIZE (field1), &size1)
1365 && poly_int_tree_p (DECL_SIZE (field2), &size2)
1366 && !ranges_maybe_overlap_p (offset1, size1, offset2, size2))
1367 return 1;
1369 /* Resort to slower overlap checking by looking for matching types in
1370 the middle of access path. */
1371 return -1;
1374 /* Return low bound of array. Do not produce new trees
1375 and thus do not care about particular type of integer constant
1376 and placeholder exprs. */
1378 static tree
1379 cheap_array_ref_low_bound (tree ref)
1381 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
1383 /* Avoid expensive array_ref_low_bound.
1384 low bound is either stored in operand2, or it is TYPE_MIN_VALUE of domain
1385 type or it is zero. */
1386 if (TREE_OPERAND (ref, 2))
1387 return TREE_OPERAND (ref, 2);
1388 else if (domain_type && TYPE_MIN_VALUE (domain_type))
1389 return TYPE_MIN_VALUE (domain_type);
1390 else
1391 return integer_zero_node;
1394 /* REF1 and REF2 are ARRAY_REFs with either same base address or which are
1395 completely disjoint.
1397 Return 1 if the refs are non-overlapping.
1398 Return 0 if they are possibly overlapping but if so the overlap again
1399 starts on the same address.
1400 Return -1 otherwise. */
1403 nonoverlapping_array_refs_p (tree ref1, tree ref2)
1405 tree index1 = TREE_OPERAND (ref1, 1);
1406 tree index2 = TREE_OPERAND (ref2, 1);
1407 tree low_bound1 = cheap_array_ref_low_bound (ref1);
1408 tree low_bound2 = cheap_array_ref_low_bound (ref2);
1410 /* Handle zero offsets first: we do not need to match type size in this
1411 case. */
1412 if (operand_equal_p (index1, low_bound1, 0)
1413 && operand_equal_p (index2, low_bound2, 0))
1414 return 0;
1416 /* If type sizes are different, give up.
1418 Avoid expensive array_ref_element_size.
1419 If operand 3 is present it denotes size in the alignmnet units.
1420 Otherwise size is TYPE_SIZE of the element type.
1421 Handle only common cases where types are of the same "kind". */
1422 if ((TREE_OPERAND (ref1, 3) == NULL) != (TREE_OPERAND (ref2, 3) == NULL))
1423 return -1;
1425 tree elmt_type1 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref1, 0)));
1426 tree elmt_type2 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref2, 0)));
1428 if (TREE_OPERAND (ref1, 3))
1430 if (TYPE_ALIGN (elmt_type1) != TYPE_ALIGN (elmt_type2)
1431 || !operand_equal_p (TREE_OPERAND (ref1, 3),
1432 TREE_OPERAND (ref2, 3), 0))
1433 return -1;
1435 else
1437 if (!operand_equal_p (TYPE_SIZE_UNIT (elmt_type1),
1438 TYPE_SIZE_UNIT (elmt_type2), 0))
1439 return -1;
1442 /* Since we know that type sizes are the same, there is no need to return
1443 -1 after this point. Partial overlap can not be introduced. */
1445 /* We may need to fold trees in this case.
1446 TODO: Handle integer constant case at least. */
1447 if (!operand_equal_p (low_bound1, low_bound2, 0))
1448 return 0;
1450 if (TREE_CODE (index1) == INTEGER_CST && TREE_CODE (index2) == INTEGER_CST)
1452 if (tree_int_cst_equal (index1, index2))
1453 return 0;
1454 return 1;
1456 /* TODO: We can use VRP to further disambiguate here. */
1457 return 0;
1460 /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1461 MATCH2 either point to the same address or are disjoint.
1462 MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1463 respectively or NULL in the case we established equivalence of bases.
1464 If PARTIAL_OVERLAP is true assume that the toplevel arrays may actually
1465 overlap by exact multiply of their element size.
1467 This test works by matching the initial segment of the access path
1468 and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1469 match was determined without use of TBAA oracle.
1471 Return 1 if we can determine that component references REF1 and REF2,
1472 that are within a common DECL, cannot overlap.
1474 Return 0 if paths are same and thus there is nothing to disambiguate more
1475 (i.e. there is must alias assuming there is must alias between MATCH1 and
1476 MATCH2)
1478 Return -1 if we can not determine 0 or 1 - this happens when we met
1479 non-matching types was met in the path.
1480 In this case it may make sense to continue by other disambiguation
1481 oracles. */
1483 static int
1484 nonoverlapping_refs_since_match_p (tree match1, tree ref1,
1485 tree match2, tree ref2,
1486 bool partial_overlap)
1488 int ntbaa1 = 0, ntbaa2 = 0;
1489 /* Early return if there are no references to match, we do not need
1490 to walk the access paths.
1492 Do not consider this as may-alias for stats - it is more useful
1493 to have information how many disambiguations happened provided that
1494 the query was meaningful. */
1496 if (match1 == ref1 || !handled_component_p (ref1)
1497 || match2 == ref2 || !handled_component_p (ref2))
1498 return -1;
1500 auto_vec<tree, 16> component_refs1;
1501 auto_vec<tree, 16> component_refs2;
1503 /* Create the stack of handled components for REF1. */
1504 while (handled_component_p (ref1) && ref1 != match1)
1506 /* We use TBAA only to re-synchronize after mismatched refs. So we
1507 do not need to truncate access path after TBAA part ends. */
1508 if (ends_tbaa_access_path_p (ref1))
1509 ntbaa1 = 0;
1510 else
1511 ntbaa1++;
1512 component_refs1.safe_push (ref1);
1513 ref1 = TREE_OPERAND (ref1, 0);
1516 /* Create the stack of handled components for REF2. */
1517 while (handled_component_p (ref2) && ref2 != match2)
1519 if (ends_tbaa_access_path_p (ref2))
1520 ntbaa2 = 0;
1521 else
1522 ntbaa2++;
1523 component_refs2.safe_push (ref2);
1524 ref2 = TREE_OPERAND (ref2, 0);
1527 if (!flag_strict_aliasing)
1529 ntbaa1 = 0;
1530 ntbaa2 = 0;
1533 bool mem_ref1 = TREE_CODE (ref1) == MEM_REF && ref1 != match1;
1534 bool mem_ref2 = TREE_CODE (ref2) == MEM_REF && ref2 != match2;
1536 /* If only one of access path starts with MEM_REF check that offset is 0
1537 so the addresses stays the same after stripping it.
1538 TODO: In this case we may walk the other access path until we get same
1539 offset.
1541 If both starts with MEM_REF, offset has to be same. */
1542 if ((mem_ref1 && !mem_ref2 && !integer_zerop (TREE_OPERAND (ref1, 1)))
1543 || (mem_ref2 && !mem_ref1 && !integer_zerop (TREE_OPERAND (ref2, 1)))
1544 || (mem_ref1 && mem_ref2
1545 && !tree_int_cst_equal (TREE_OPERAND (ref1, 1),
1546 TREE_OPERAND (ref2, 1))))
1548 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1549 return -1;
1552 /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1553 to handle them here at all. */
1554 gcc_checking_assert (TREE_CODE (ref1) != TARGET_MEM_REF
1555 && TREE_CODE (ref2) != TARGET_MEM_REF);
1557 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1558 rank. This is sufficient because we start from the same DECL and you
1559 cannot reference several fields at a time with COMPONENT_REFs (unlike
1560 with ARRAY_RANGE_REFs for arrays) so you always need the same number
1561 of them to access a sub-component, unless you're in a union, in which
1562 case the return value will precisely be false. */
1563 while (true)
1565 /* Track if we seen unmatched ref with non-zero offset. In this case
1566 we must look for partial overlaps. */
1567 bool seen_unmatched_ref_p = false;
1569 /* First match ARRAY_REFs an try to disambiguate. */
1570 if (!component_refs1.is_empty ()
1571 && !component_refs2.is_empty ())
1573 unsigned int narray_refs1=0, narray_refs2=0;
1575 /* We generally assume that both access paths starts by same sequence
1576 of refs. However if number of array refs is not in sync, try
1577 to recover and pop elts until number match. This helps the case
1578 where one access path starts by array and other by element. */
1579 for (narray_refs1 = 0; narray_refs1 < component_refs1.length ();
1580 narray_refs1++)
1581 if (TREE_CODE (component_refs1 [component_refs1.length()
1582 - 1 - narray_refs1]) != ARRAY_REF)
1583 break;
1585 for (narray_refs2 = 0; narray_refs2 < component_refs2.length ();
1586 narray_refs2++)
1587 if (TREE_CODE (component_refs2 [component_refs2.length()
1588 - 1 - narray_refs2]) != ARRAY_REF)
1589 break;
1590 for (; narray_refs1 > narray_refs2; narray_refs1--)
1592 ref1 = component_refs1.pop ();
1593 ntbaa1--;
1595 /* If index is non-zero we need to check whether the reference
1596 does not break the main invariant that bases are either
1597 disjoint or equal. Consider the example:
1599 unsigned char out[][1];
1600 out[1]="a";
1601 out[i][0];
1603 Here bases out and out are same, but after removing the
1604 [i] index, this invariant no longer holds, because
1605 out[i] points to the middle of array out.
1607 TODO: If size of type of the skipped reference is an integer
1608 multiply of the size of type of the other reference this
1609 invariant can be verified, but even then it is not completely
1610 safe with !flag_strict_aliasing if the other reference contains
1611 unbounded array accesses.
1612 See */
1614 if (!operand_equal_p (TREE_OPERAND (ref1, 1),
1615 cheap_array_ref_low_bound (ref1), 0))
1616 return 0;
1618 for (; narray_refs2 > narray_refs1; narray_refs2--)
1620 ref2 = component_refs2.pop ();
1621 ntbaa2--;
1622 if (!operand_equal_p (TREE_OPERAND (ref2, 1),
1623 cheap_array_ref_low_bound (ref2), 0))
1624 return 0;
1626 /* Try to disambiguate matched arrays. */
1627 for (unsigned int i = 0; i < narray_refs1; i++)
1629 int cmp = nonoverlapping_array_refs_p (component_refs1.pop (),
1630 component_refs2.pop ());
1631 ntbaa1--;
1632 ntbaa2--;
1633 if (cmp == 1 && !partial_overlap)
1635 ++alias_stats
1636 .nonoverlapping_refs_since_match_p_no_alias;
1637 return 1;
1639 if (cmp == -1)
1641 seen_unmatched_ref_p = true;
1642 /* We can not maintain the invariant that bases are either
1643 same or completely disjoint. However we can still recover
1644 from type based alias analysis if we reach referneces to
1645 same sizes. We do not attempt to match array sizes, so
1646 just finish array walking and look for component refs. */
1647 if (ntbaa1 < 0 || ntbaa2 < 0)
1649 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1650 return -1;
1652 for (i++; i < narray_refs1; i++)
1654 component_refs1.pop ();
1655 component_refs2.pop ();
1656 ntbaa1--;
1657 ntbaa2--;
1659 break;
1661 partial_overlap = false;
1665 /* Next look for component_refs. */
1668 if (component_refs1.is_empty ())
1670 ++alias_stats
1671 .nonoverlapping_refs_since_match_p_must_overlap;
1672 return 0;
1674 ref1 = component_refs1.pop ();
1675 ntbaa1--;
1676 if (TREE_CODE (ref1) != COMPONENT_REF)
1678 seen_unmatched_ref_p = true;
1679 if (ntbaa1 < 0 || ntbaa2 < 0)
1681 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1682 return -1;
1686 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1690 if (component_refs2.is_empty ())
1692 ++alias_stats
1693 .nonoverlapping_refs_since_match_p_must_overlap;
1694 return 0;
1696 ref2 = component_refs2.pop ();
1697 ntbaa2--;
1698 if (TREE_CODE (ref2) != COMPONENT_REF)
1700 if (ntbaa1 < 0 || ntbaa2 < 0)
1702 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1703 return -1;
1705 seen_unmatched_ref_p = true;
1708 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1710 /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1711 earlier. */
1712 gcc_checking_assert (TREE_CODE (ref1) == COMPONENT_REF
1713 && TREE_CODE (ref2) == COMPONENT_REF);
1715 tree field1 = TREE_OPERAND (ref1, 1);
1716 tree field2 = TREE_OPERAND (ref2, 1);
1718 /* ??? We cannot simply use the type of operand #0 of the refs here
1719 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1720 for common blocks instead of using unions like everyone else. */
1721 tree type1 = DECL_CONTEXT (field1);
1722 tree type2 = DECL_CONTEXT (field2);
1724 partial_overlap = false;
1726 /* If we skipped array refs on type of different sizes, we can
1727 no longer be sure that there are not partial overlaps. */
1728 if (seen_unmatched_ref_p && ntbaa1 >= 0 && ntbaa2 >= 0
1729 && !operand_equal_p (TYPE_SIZE (type1), TYPE_SIZE (type2), 0))
1731 ++alias_stats
1732 .nonoverlapping_refs_since_match_p_may_alias;
1733 return -1;
1736 int cmp = nonoverlapping_component_refs_p_1 (field1, field2);
1737 if (cmp == -1)
1739 ++alias_stats
1740 .nonoverlapping_refs_since_match_p_may_alias;
1741 return -1;
1743 else if (cmp == 1)
1745 ++alias_stats
1746 .nonoverlapping_refs_since_match_p_no_alias;
1747 return 1;
1751 ++alias_stats.nonoverlapping_refs_since_match_p_must_overlap;
1752 return 0;
1755 /* Return TYPE_UID which can be used to match record types we consider
1756 same for TBAA purposes. */
1758 static inline int
1759 ncr_type_uid (const_tree field)
1761 /* ??? We cannot simply use the type of operand #0 of the refs here
1762 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1763 for common blocks instead of using unions like everyone else. */
1764 tree type = DECL_FIELD_CONTEXT (field);
1765 /* With LTO types considered same_type_for_tbaa_p
1766 from different translation unit may not have same
1767 main variant. They however have same TYPE_CANONICAL. */
1768 if (TYPE_CANONICAL (type))
1769 return TYPE_UID (TYPE_CANONICAL (type));
1770 return TYPE_UID (type);
1773 /* qsort compare function to sort FIELD_DECLs after their
1774 DECL_FIELD_CONTEXT TYPE_UID. */
1776 static inline int
1777 ncr_compar (const void *field1_, const void *field2_)
1779 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1780 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1781 unsigned int uid1 = ncr_type_uid (field1);
1782 unsigned int uid2 = ncr_type_uid (field2);
1784 if (uid1 < uid2)
1785 return -1;
1786 else if (uid1 > uid2)
1787 return 1;
1788 return 0;
1791 /* Return true if we can determine that the fields referenced cannot
1792 overlap for any pair of objects. This relies on TBAA. */
1794 static bool
1795 nonoverlapping_component_refs_p (const_tree x, const_tree y)
1797 /* Early return if we have nothing to do.
1799 Do not consider this as may-alias for stats - it is more useful
1800 to have information how many disambiguations happened provided that
1801 the query was meaningful. */
1802 if (!flag_strict_aliasing
1803 || !x || !y
1804 || !handled_component_p (x)
1805 || !handled_component_p (y))
1806 return false;
1808 auto_vec<const_tree, 16> fieldsx;
1809 while (handled_component_p (x))
1811 if (TREE_CODE (x) == COMPONENT_REF)
1813 tree field = TREE_OPERAND (x, 1);
1814 tree type = DECL_FIELD_CONTEXT (field);
1815 if (TREE_CODE (type) == RECORD_TYPE)
1816 fieldsx.safe_push (field);
1818 else if (ends_tbaa_access_path_p (x))
1819 fieldsx.truncate (0);
1820 x = TREE_OPERAND (x, 0);
1822 if (fieldsx.length () == 0)
1823 return false;
1824 auto_vec<const_tree, 16> fieldsy;
1825 while (handled_component_p (y))
1827 if (TREE_CODE (y) == COMPONENT_REF)
1829 tree field = TREE_OPERAND (y, 1);
1830 tree type = DECL_FIELD_CONTEXT (field);
1831 if (TREE_CODE (type) == RECORD_TYPE)
1832 fieldsy.safe_push (TREE_OPERAND (y, 1));
1834 else if (ends_tbaa_access_path_p (y))
1835 fieldsy.truncate (0);
1836 y = TREE_OPERAND (y, 0);
1838 if (fieldsy.length () == 0)
1840 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1841 return false;
1844 /* Most common case first. */
1845 if (fieldsx.length () == 1
1846 && fieldsy.length () == 1)
1848 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx[0]),
1849 DECL_FIELD_CONTEXT (fieldsy[0])) == 1
1850 && nonoverlapping_component_refs_p_1 (fieldsx[0], fieldsy[0]) == 1)
1852 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1853 return true;
1855 else
1857 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1858 return false;
1862 if (fieldsx.length () == 2)
1864 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1865 std::swap (fieldsx[0], fieldsx[1]);
1867 else
1868 fieldsx.qsort (ncr_compar);
1870 if (fieldsy.length () == 2)
1872 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1873 std::swap (fieldsy[0], fieldsy[1]);
1875 else
1876 fieldsy.qsort (ncr_compar);
1878 unsigned i = 0, j = 0;
1881 const_tree fieldx = fieldsx[i];
1882 const_tree fieldy = fieldsy[j];
1884 /* We're left with accessing different fields of a structure,
1885 no possible overlap. */
1886 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx),
1887 DECL_FIELD_CONTEXT (fieldy)) == 1
1888 && nonoverlapping_component_refs_p_1 (fieldx, fieldy) == 1)
1890 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1891 return true;
1894 if (ncr_type_uid (fieldx) < ncr_type_uid (fieldy))
1896 i++;
1897 if (i == fieldsx.length ())
1898 break;
1900 else
1902 j++;
1903 if (j == fieldsy.length ())
1904 break;
1907 while (1);
1909 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1910 return false;
1914 /* Return true if two memory references based on the variables BASE1
1915 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1916 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1917 if non-NULL are the complete memory reference trees. */
1919 static bool
1920 decl_refs_may_alias_p (tree ref1, tree base1,
1921 poly_int64 offset1, poly_int64 max_size1,
1922 poly_int64 size1,
1923 tree ref2, tree base2,
1924 poly_int64 offset2, poly_int64 max_size2,
1925 poly_int64 size2)
1927 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1929 /* If both references are based on different variables, they cannot alias. */
1930 if (compare_base_decls (base1, base2) == 0)
1931 return false;
1933 /* If both references are based on the same variable, they cannot alias if
1934 the accesses do not overlap. */
1935 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1936 return false;
1938 /* If there is must alias, there is no use disambiguating further. */
1939 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1940 return true;
1942 /* For components with variable position, the above test isn't sufficient,
1943 so we disambiguate component references manually. */
1944 if (ref1 && ref2
1945 && handled_component_p (ref1) && handled_component_p (ref2)
1946 && nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2, false) == 1)
1947 return false;
1949 return true;
1952 /* Return true if an indirect reference based on *PTR1 constrained
1953 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1954 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1955 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1956 in which case they are computed on-demand. REF1 and REF2
1957 if non-NULL are the complete memory reference trees. */
1959 static bool
1960 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1961 poly_int64 offset1, poly_int64 max_size1,
1962 poly_int64 size1,
1963 alias_set_type ref1_alias_set,
1964 alias_set_type base1_alias_set,
1965 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1966 poly_int64 offset2, poly_int64 max_size2,
1967 poly_int64 size2,
1968 alias_set_type ref2_alias_set,
1969 alias_set_type base2_alias_set, bool tbaa_p)
1971 tree ptr1;
1972 tree ptrtype1, dbase2;
1974 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1975 || TREE_CODE (base1) == TARGET_MEM_REF)
1976 && DECL_P (base2));
1978 ptr1 = TREE_OPERAND (base1, 0);
1979 poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1981 /* If only one reference is based on a variable, they cannot alias if
1982 the pointer access is beyond the extent of the variable access.
1983 (the pointer base cannot validly point to an offset less than zero
1984 of the variable).
1985 ??? IVOPTs creates bases that do not honor this restriction,
1986 so do not apply this optimization for TARGET_MEM_REFs. */
1987 if (TREE_CODE (base1) != TARGET_MEM_REF
1988 && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
1989 return false;
1990 /* They also cannot alias if the pointer may not point to the decl. */
1991 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1992 return false;
1994 /* Disambiguations that rely on strict aliasing rules follow. */
1995 if (!flag_strict_aliasing || !tbaa_p)
1996 return true;
1998 /* If the alias set for a pointer access is zero all bets are off. */
1999 if (base1_alias_set == 0 || base2_alias_set == 0)
2000 return true;
2002 /* When we are trying to disambiguate an access with a pointer dereference
2003 as base versus one with a decl as base we can use both the size
2004 of the decl and its dynamic type for extra disambiguation.
2005 ??? We do not know anything about the dynamic type of the decl
2006 other than that its alias-set contains base2_alias_set as a subset
2007 which does not help us here. */
2008 /* As we know nothing useful about the dynamic type of the decl just
2009 use the usual conflict check rather than a subset test.
2010 ??? We could introduce -fvery-strict-aliasing when the language
2011 does not allow decls to have a dynamic type that differs from their
2012 static type. Then we can check
2013 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
2014 if (base1_alias_set != base2_alias_set
2015 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2016 return false;
2018 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2020 /* If the size of the access relevant for TBAA through the pointer
2021 is bigger than the size of the decl we can't possibly access the
2022 decl via that pointer. */
2023 if (/* ??? This in turn may run afoul when a decl of type T which is
2024 a member of union type U is accessed through a pointer to
2025 type U and sizeof T is smaller than sizeof U. */
2026 TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
2027 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
2028 && compare_sizes (DECL_SIZE (base2),
2029 TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
2030 return false;
2032 if (!ref2)
2033 return true;
2035 /* If the decl is accessed via a MEM_REF, reconstruct the base
2036 we can use for TBAA and an appropriately adjusted offset. */
2037 dbase2 = ref2;
2038 while (handled_component_p (dbase2))
2039 dbase2 = TREE_OPERAND (dbase2, 0);
2040 poly_int64 doffset1 = offset1;
2041 poly_offset_int doffset2 = offset2;
2042 if (TREE_CODE (dbase2) == MEM_REF
2043 || TREE_CODE (dbase2) == TARGET_MEM_REF)
2045 doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
2046 tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
2047 /* If second reference is view-converted, give up now. */
2048 if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
2049 return true;
2052 /* If first reference is view-converted, give up now. */
2053 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
2054 return true;
2056 /* If both references are through the same type, they do not alias
2057 if the accesses do not overlap. This does extra disambiguation
2058 for mixed/pointer accesses but requires strict aliasing.
2059 For MEM_REFs we require that the component-ref offset we computed
2060 is relative to the start of the type which we ensure by
2061 comparing rvalue and access type and disregarding the constant
2062 pointer offset.
2064 But avoid treating variable length arrays as "objects", instead assume they
2065 can overlap by an exact multiple of their element size.
2066 See gcc.dg/torture/alias-2.c. */
2067 if (((TREE_CODE (base1) != TARGET_MEM_REF
2068 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2069 && (TREE_CODE (dbase2) != TARGET_MEM_REF
2070 || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
2071 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
2073 bool partial_overlap = (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
2074 && (TYPE_SIZE (TREE_TYPE (base1))
2075 && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1)))
2076 != INTEGER_CST));
2077 if (!partial_overlap
2078 && !ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
2079 return false;
2080 if (!ref1 || !ref2
2081 /* If there is must alias, there is no use disambiguating further. */
2082 || (!partial_overlap
2083 && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2084 return true;
2085 int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2086 partial_overlap);
2087 if (res == -1)
2088 return !nonoverlapping_component_refs_p (ref1, ref2);
2089 return !res;
2092 /* Do access-path based disambiguation. */
2093 if (ref1 && ref2
2094 && (handled_component_p (ref1) || handled_component_p (ref2)))
2095 return aliasing_component_refs_p (ref1,
2096 ref1_alias_set, base1_alias_set,
2097 offset1, max_size1,
2098 ref2,
2099 ref2_alias_set, base2_alias_set,
2100 offset2, max_size2);
2102 return true;
2105 /* Return true if two indirect references based on *PTR1
2106 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
2107 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
2108 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
2109 in which case they are computed on-demand. REF1 and REF2
2110 if non-NULL are the complete memory reference trees. */
2112 static bool
2113 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
2114 poly_int64 offset1, poly_int64 max_size1,
2115 poly_int64 size1,
2116 alias_set_type ref1_alias_set,
2117 alias_set_type base1_alias_set,
2118 tree ref2 ATTRIBUTE_UNUSED, tree base2,
2119 poly_int64 offset2, poly_int64 max_size2,
2120 poly_int64 size2,
2121 alias_set_type ref2_alias_set,
2122 alias_set_type base2_alias_set, bool tbaa_p)
2124 tree ptr1;
2125 tree ptr2;
2126 tree ptrtype1, ptrtype2;
2128 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
2129 || TREE_CODE (base1) == TARGET_MEM_REF)
2130 && (TREE_CODE (base2) == MEM_REF
2131 || TREE_CODE (base2) == TARGET_MEM_REF));
2133 ptr1 = TREE_OPERAND (base1, 0);
2134 ptr2 = TREE_OPERAND (base2, 0);
2136 /* If both bases are based on pointers they cannot alias if they may not
2137 point to the same memory object or if they point to the same object
2138 and the accesses do not overlap. */
2139 if ((!cfun || gimple_in_ssa_p (cfun))
2140 && operand_equal_p (ptr1, ptr2, 0)
2141 && (((TREE_CODE (base1) != TARGET_MEM_REF
2142 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2143 && (TREE_CODE (base2) != TARGET_MEM_REF
2144 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
2145 || (TREE_CODE (base1) == TARGET_MEM_REF
2146 && TREE_CODE (base2) == TARGET_MEM_REF
2147 && (TMR_STEP (base1) == TMR_STEP (base2)
2148 || (TMR_STEP (base1) && TMR_STEP (base2)
2149 && operand_equal_p (TMR_STEP (base1),
2150 TMR_STEP (base2), 0)))
2151 && (TMR_INDEX (base1) == TMR_INDEX (base2)
2152 || (TMR_INDEX (base1) && TMR_INDEX (base2)
2153 && operand_equal_p (TMR_INDEX (base1),
2154 TMR_INDEX (base2), 0)))
2155 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
2156 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
2157 && operand_equal_p (TMR_INDEX2 (base1),
2158 TMR_INDEX2 (base2), 0))))))
2160 poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
2161 poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
2162 if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
2163 offset2 + moff2, max_size2))
2164 return false;
2165 /* If there is must alias, there is no use disambiguating further. */
2166 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
2167 return true;
2168 if (ref1 && ref2)
2170 int res = nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2,
2171 false);
2172 if (res != -1)
2173 return !res;
2176 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
2177 return false;
2179 /* Disambiguations that rely on strict aliasing rules follow. */
2180 if (!flag_strict_aliasing || !tbaa_p)
2181 return true;
2183 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2184 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
2186 /* If the alias set for a pointer access is zero all bets are off. */
2187 if (base1_alias_set == 0
2188 || base2_alias_set == 0)
2189 return true;
2191 /* Do type-based disambiguation. */
2192 if (base1_alias_set != base2_alias_set
2193 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2194 return false;
2196 /* If either reference is view-converted, give up now. */
2197 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
2198 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
2199 return true;
2201 /* If both references are through the same type, they do not alias
2202 if the accesses do not overlap. This does extra disambiguation
2203 for mixed/pointer accesses but requires strict aliasing. */
2204 if ((TREE_CODE (base1) != TARGET_MEM_REF
2205 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2206 && (TREE_CODE (base2) != TARGET_MEM_REF
2207 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
2208 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
2209 TREE_TYPE (ptrtype2)) == 1)
2211 /* But avoid treating arrays as "objects", instead assume they
2212 can overlap by an exact multiple of their element size.
2213 See gcc.dg/torture/alias-2.c. */
2214 bool partial_overlap = TREE_CODE (TREE_TYPE (ptrtype1)) == ARRAY_TYPE;
2216 if (!partial_overlap
2217 && !ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
2218 return false;
2219 if (!ref1 || !ref2
2220 || (!partial_overlap
2221 && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2222 return true;
2223 int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2224 partial_overlap);
2225 if (res == -1)
2226 return !nonoverlapping_component_refs_p (ref1, ref2);
2227 return !res;
2230 /* Do access-path based disambiguation. */
2231 if (ref1 && ref2
2232 && (handled_component_p (ref1) || handled_component_p (ref2)))
2233 return aliasing_component_refs_p (ref1,
2234 ref1_alias_set, base1_alias_set,
2235 offset1, max_size1,
2236 ref2,
2237 ref2_alias_set, base2_alias_set,
2238 offset2, max_size2);
2240 return true;
2243 /* Return true, if the two memory references REF1 and REF2 may alias. */
2245 static bool
2246 refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2248 tree base1, base2;
2249 poly_int64 offset1 = 0, offset2 = 0;
2250 poly_int64 max_size1 = -1, max_size2 = -1;
2251 bool var1_p, var2_p, ind1_p, ind2_p;
2253 gcc_checking_assert ((!ref1->ref
2254 || TREE_CODE (ref1->ref) == SSA_NAME
2255 || DECL_P (ref1->ref)
2256 || TREE_CODE (ref1->ref) == STRING_CST
2257 || handled_component_p (ref1->ref)
2258 || TREE_CODE (ref1->ref) == MEM_REF
2259 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
2260 && (!ref2->ref
2261 || TREE_CODE (ref2->ref) == SSA_NAME
2262 || DECL_P (ref2->ref)
2263 || TREE_CODE (ref2->ref) == STRING_CST
2264 || handled_component_p (ref2->ref)
2265 || TREE_CODE (ref2->ref) == MEM_REF
2266 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
2268 /* Decompose the references into their base objects and the access. */
2269 base1 = ao_ref_base (ref1);
2270 offset1 = ref1->offset;
2271 max_size1 = ref1->max_size;
2272 base2 = ao_ref_base (ref2);
2273 offset2 = ref2->offset;
2274 max_size2 = ref2->max_size;
2276 /* We can end up with registers or constants as bases for example from
2277 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
2278 which is seen as a struct copy. */
2279 if (TREE_CODE (base1) == SSA_NAME
2280 || TREE_CODE (base1) == CONST_DECL
2281 || TREE_CODE (base1) == CONSTRUCTOR
2282 || TREE_CODE (base1) == ADDR_EXPR
2283 || CONSTANT_CLASS_P (base1)
2284 || TREE_CODE (base2) == SSA_NAME
2285 || TREE_CODE (base2) == CONST_DECL
2286 || TREE_CODE (base2) == CONSTRUCTOR
2287 || TREE_CODE (base2) == ADDR_EXPR
2288 || CONSTANT_CLASS_P (base2))
2289 return false;
2291 /* We can end up referring to code via function and label decls.
2292 As we likely do not properly track code aliases conservatively
2293 bail out. */
2294 if (TREE_CODE (base1) == FUNCTION_DECL
2295 || TREE_CODE (base1) == LABEL_DECL
2296 || TREE_CODE (base2) == FUNCTION_DECL
2297 || TREE_CODE (base2) == LABEL_DECL)
2298 return true;
2300 /* Two volatile accesses always conflict. */
2301 if (ref1->volatile_p
2302 && ref2->volatile_p)
2303 return true;
2305 /* Defer to simple offset based disambiguation if we have
2306 references based on two decls. Do this before defering to
2307 TBAA to handle must-alias cases in conformance with the
2308 GCC extension of allowing type-punning through unions. */
2309 var1_p = DECL_P (base1);
2310 var2_p = DECL_P (base2);
2311 if (var1_p && var2_p)
2312 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
2313 ref1->size,
2314 ref2->ref, base2, offset2, max_size2,
2315 ref2->size);
2317 /* Handle restrict based accesses.
2318 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
2319 here. */
2320 tree rbase1 = base1;
2321 tree rbase2 = base2;
2322 if (var1_p)
2324 rbase1 = ref1->ref;
2325 if (rbase1)
2326 while (handled_component_p (rbase1))
2327 rbase1 = TREE_OPERAND (rbase1, 0);
2329 if (var2_p)
2331 rbase2 = ref2->ref;
2332 if (rbase2)
2333 while (handled_component_p (rbase2))
2334 rbase2 = TREE_OPERAND (rbase2, 0);
2336 if (rbase1 && rbase2
2337 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
2338 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
2339 /* If the accesses are in the same restrict clique... */
2340 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
2341 /* But based on different pointers they do not alias. */
2342 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
2343 return false;
2345 ind1_p = (TREE_CODE (base1) == MEM_REF
2346 || TREE_CODE (base1) == TARGET_MEM_REF);
2347 ind2_p = (TREE_CODE (base2) == MEM_REF
2348 || TREE_CODE (base2) == TARGET_MEM_REF);
2350 /* Canonicalize the pointer-vs-decl case. */
2351 if (ind1_p && var2_p)
2353 std::swap (offset1, offset2);
2354 std::swap (max_size1, max_size2);
2355 std::swap (base1, base2);
2356 std::swap (ref1, ref2);
2357 var1_p = true;
2358 ind1_p = false;
2359 var2_p = false;
2360 ind2_p = true;
2363 /* First defer to TBAA if possible. */
2364 if (tbaa_p
2365 && flag_strict_aliasing
2366 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
2367 ao_ref_alias_set (ref2)))
2368 return false;
2370 /* If the reference is based on a pointer that points to memory
2371 that may not be written to then the other reference cannot possibly
2372 clobber it. */
2373 if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
2374 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
2375 || (ind1_p
2376 && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
2377 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
2378 return false;
2380 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2381 if (var1_p && ind2_p)
2382 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
2383 offset2, max_size2, ref2->size,
2384 ao_ref_alias_set (ref2),
2385 ao_ref_base_alias_set (ref2),
2386 ref1->ref, base1,
2387 offset1, max_size1, ref1->size,
2388 ao_ref_alias_set (ref1),
2389 ao_ref_base_alias_set (ref1),
2390 tbaa_p);
2391 else if (ind1_p && ind2_p)
2392 return indirect_refs_may_alias_p (ref1->ref, base1,
2393 offset1, max_size1, ref1->size,
2394 ao_ref_alias_set (ref1),
2395 ao_ref_base_alias_set (ref1),
2396 ref2->ref, base2,
2397 offset2, max_size2, ref2->size,
2398 ao_ref_alias_set (ref2),
2399 ao_ref_base_alias_set (ref2),
2400 tbaa_p);
2402 gcc_unreachable ();
2405 /* Return true, if the two memory references REF1 and REF2 may alias
2406 and update statistics. */
2408 bool
2409 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2411 bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
2412 if (res)
2413 ++alias_stats.refs_may_alias_p_may_alias;
2414 else
2415 ++alias_stats.refs_may_alias_p_no_alias;
2416 return res;
2419 static bool
2420 refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
2422 ao_ref r1;
2423 ao_ref_init (&r1, ref1);
2424 return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
2427 bool
2428 refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
2430 ao_ref r1, r2;
2431 ao_ref_init (&r1, ref1);
2432 ao_ref_init (&r2, ref2);
2433 return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
2436 /* Returns true if there is a anti-dependence for the STORE that
2437 executes after the LOAD. */
2439 bool
2440 refs_anti_dependent_p (tree load, tree store)
2442 ao_ref r1, r2;
2443 ao_ref_init (&r1, load);
2444 ao_ref_init (&r2, store);
2445 return refs_may_alias_p_1 (&r1, &r2, false);
2448 /* Returns true if there is a output dependence for the stores
2449 STORE1 and STORE2. */
2451 bool
2452 refs_output_dependent_p (tree store1, tree store2)
2454 ao_ref r1, r2;
2455 ao_ref_init (&r1, store1);
2456 ao_ref_init (&r2, store2);
2457 return refs_may_alias_p_1 (&r1, &r2, false);
2460 /* Returns true if and only if REF may alias any access stored in TT.
2461 IF TBAA_P is true, use TBAA oracle. */
2463 static bool
2464 modref_may_conflict (const gimple *stmt,
2465 modref_tree <alias_set_type> *tt, ao_ref *ref, bool tbaa_p)
2467 alias_set_type base_set, ref_set;
2468 modref_base_node <alias_set_type> *base_node;
2469 modref_ref_node <alias_set_type> *ref_node;
2470 size_t i, j, k;
2472 if (tt->every_base)
2473 return true;
2475 if (!dbg_cnt (ipa_mod_ref))
2476 return true;
2478 base_set = ao_ref_base_alias_set (ref);
2480 ref_set = ao_ref_alias_set (ref);
2482 int num_tests = 0, max_tests = param_modref_max_tests;
2483 FOR_EACH_VEC_SAFE_ELT (tt->bases, i, base_node)
2485 if (tbaa_p && flag_strict_aliasing)
2487 if (num_tests >= max_tests)
2488 return true;
2489 alias_stats.modref_tests++;
2490 if (!alias_sets_conflict_p (base_set, base_node->base))
2491 continue;
2492 num_tests++;
2495 if (base_node->every_ref)
2496 return true;
2498 FOR_EACH_VEC_SAFE_ELT (base_node->refs, j, ref_node)
2500 /* Do not repeat same test as before. */
2501 if ((ref_set != base_set || base_node->base != ref_node->ref)
2502 && tbaa_p && flag_strict_aliasing)
2504 if (num_tests >= max_tests)
2505 return true;
2506 alias_stats.modref_tests++;
2507 if (!alias_sets_conflict_p (ref_set, ref_node->ref))
2508 continue;
2509 num_tests++;
2512 /* TBAA checks did not disambiguate, try to use base pointer, for
2513 that we however need to have ref->ref or ref->base. */
2514 if (ref_node->every_access || (!ref->ref && !ref->base))
2515 return true;
2517 modref_access_node *access_node;
2518 FOR_EACH_VEC_SAFE_ELT (ref_node->accesses, k, access_node)
2520 if (num_tests >= max_tests)
2521 return true;
2523 if (access_node->parm_index == -1
2524 || (unsigned)access_node->parm_index
2525 >= gimple_call_num_args (stmt))
2526 return true;
2528 alias_stats.modref_baseptr_tests++;
2530 tree arg = gimple_call_arg (stmt, access_node->parm_index);
2532 if (integer_zerop (arg) && flag_delete_null_pointer_checks)
2533 continue;
2535 if (!POINTER_TYPE_P (TREE_TYPE (arg)))
2536 return true;
2538 /* ao_ref_init_from_ptr_and_range assumes that memory access
2539 starts by the pointed to location. If we did not track the
2540 offset it is possible that it starts before the actual
2541 pointer. */
2542 if (!access_node->parm_offset_known)
2544 if (ptr_deref_may_alias_ref_p_1 (arg, ref))
2545 return true;
2547 else
2549 ao_ref ref2;
2550 poly_offset_int off = (poly_offset_int)access_node->offset
2551 + ((poly_offset_int)access_node->parm_offset
2552 << LOG2_BITS_PER_UNIT);
2553 poly_int64 off2;
2554 if (off.to_shwi (&off2))
2556 ao_ref_init_from_ptr_and_range
2557 (&ref2, arg, true, off2,
2558 access_node->size,
2559 access_node->max_size);
2560 ref2.ref_alias_set = ref_set;
2561 ref2.base_alias_set = base_set;
2562 if (refs_may_alias_p_1 (&ref2, ref, tbaa_p))
2563 return true;
2565 else if (ptr_deref_may_alias_ref_p_1 (arg, ref))
2566 return true;
2568 num_tests++;
2572 return false;
2575 /* If the call CALL may use the memory reference REF return true,
2576 otherwise return false. */
2578 static bool
2579 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
2581 tree base, callee;
2582 unsigned i;
2583 int flags = gimple_call_flags (call);
2585 /* Const functions without a static chain do not implicitly use memory. */
2586 if (!gimple_call_chain (call)
2587 && (flags & (ECF_CONST|ECF_NOVOPS)))
2588 goto process_args;
2590 /* A call that is not without side-effects might involve volatile
2591 accesses and thus conflicts with all other volatile accesses. */
2592 if (ref->volatile_p)
2593 return true;
2595 callee = gimple_call_fndecl (call);
2597 if (!gimple_call_chain (call) && callee != NULL_TREE)
2599 struct cgraph_node *node = cgraph_node::get (callee);
2600 /* We can not safely optimize based on summary of calle if it does
2601 not always bind to current def: it is possible that memory load
2602 was optimized out earlier and the interposed variant may not be
2603 optimized this way. */
2604 if (node && node->binds_to_current_def_p ())
2606 modref_summary *summary = get_modref_function_summary (node);
2607 if (summary)
2609 if (!modref_may_conflict (call, summary->loads, ref, tbaa_p))
2611 alias_stats.modref_use_no_alias++;
2612 if (dump_file && (dump_flags & TDF_DETAILS))
2614 fprintf (dump_file,
2615 "ipa-modref: call stmt ");
2616 print_gimple_stmt (dump_file, call, 0);
2617 fprintf (dump_file,
2618 "ipa-modref: call to %s does not use ",
2619 node->dump_name ());
2620 if (!ref->ref && ref->base)
2622 fprintf (dump_file, "base: ");
2623 print_generic_expr (dump_file, ref->base);
2625 else if (ref->ref)
2627 fprintf (dump_file, "ref: ");
2628 print_generic_expr (dump_file, ref->ref);
2630 fprintf (dump_file, " alias sets: %i->%i\n",
2631 ao_ref_base_alias_set (ref),
2632 ao_ref_alias_set (ref));
2634 goto process_args;
2636 alias_stats.modref_use_may_alias++;
2641 base = ao_ref_base (ref);
2642 if (!base)
2643 return true;
2645 /* If the reference is based on a decl that is not aliased the call
2646 cannot possibly use it. */
2647 if (DECL_P (base)
2648 && !may_be_aliased (base)
2649 /* But local statics can be used through recursion. */
2650 && !is_global_var (base))
2651 goto process_args;
2653 /* Handle those builtin functions explicitly that do not act as
2654 escape points. See tree-ssa-structalias.c:find_func_aliases
2655 for the list of builtins we might need to handle here. */
2656 if (callee != NULL_TREE
2657 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2658 switch (DECL_FUNCTION_CODE (callee))
2660 /* All the following functions read memory pointed to by
2661 their second argument. strcat/strncat additionally
2662 reads memory pointed to by the first argument. */
2663 case BUILT_IN_STRCAT:
2664 case BUILT_IN_STRNCAT:
2666 ao_ref dref;
2667 ao_ref_init_from_ptr_and_size (&dref,
2668 gimple_call_arg (call, 0),
2669 NULL_TREE);
2670 if (refs_may_alias_p_1 (&dref, ref, false))
2671 return true;
2673 /* FALLTHRU */
2674 case BUILT_IN_STRCPY:
2675 case BUILT_IN_STRNCPY:
2676 case BUILT_IN_MEMCPY:
2677 case BUILT_IN_MEMMOVE:
2678 case BUILT_IN_MEMPCPY:
2679 case BUILT_IN_STPCPY:
2680 case BUILT_IN_STPNCPY:
2681 case BUILT_IN_TM_MEMCPY:
2682 case BUILT_IN_TM_MEMMOVE:
2684 ao_ref dref;
2685 tree size = NULL_TREE;
2686 if (gimple_call_num_args (call) == 3)
2687 size = gimple_call_arg (call, 2);
2688 ao_ref_init_from_ptr_and_size (&dref,
2689 gimple_call_arg (call, 1),
2690 size);
2691 return refs_may_alias_p_1 (&dref, ref, false);
2693 case BUILT_IN_STRCAT_CHK:
2694 case BUILT_IN_STRNCAT_CHK:
2696 ao_ref dref;
2697 ao_ref_init_from_ptr_and_size (&dref,
2698 gimple_call_arg (call, 0),
2699 NULL_TREE);
2700 if (refs_may_alias_p_1 (&dref, ref, false))
2701 return true;
2703 /* FALLTHRU */
2704 case BUILT_IN_STRCPY_CHK:
2705 case BUILT_IN_STRNCPY_CHK:
2706 case BUILT_IN_MEMCPY_CHK:
2707 case BUILT_IN_MEMMOVE_CHK:
2708 case BUILT_IN_MEMPCPY_CHK:
2709 case BUILT_IN_STPCPY_CHK:
2710 case BUILT_IN_STPNCPY_CHK:
2712 ao_ref dref;
2713 tree size = NULL_TREE;
2714 if (gimple_call_num_args (call) == 4)
2715 size = gimple_call_arg (call, 2);
2716 ao_ref_init_from_ptr_and_size (&dref,
2717 gimple_call_arg (call, 1),
2718 size);
2719 return refs_may_alias_p_1 (&dref, ref, false);
2721 case BUILT_IN_BCOPY:
2723 ao_ref dref;
2724 tree size = gimple_call_arg (call, 2);
2725 ao_ref_init_from_ptr_and_size (&dref,
2726 gimple_call_arg (call, 0),
2727 size);
2728 return refs_may_alias_p_1 (&dref, ref, false);
2731 /* The following functions read memory pointed to by their
2732 first argument. */
2733 CASE_BUILT_IN_TM_LOAD (1):
2734 CASE_BUILT_IN_TM_LOAD (2):
2735 CASE_BUILT_IN_TM_LOAD (4):
2736 CASE_BUILT_IN_TM_LOAD (8):
2737 CASE_BUILT_IN_TM_LOAD (FLOAT):
2738 CASE_BUILT_IN_TM_LOAD (DOUBLE):
2739 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
2740 CASE_BUILT_IN_TM_LOAD (M64):
2741 CASE_BUILT_IN_TM_LOAD (M128):
2742 CASE_BUILT_IN_TM_LOAD (M256):
2743 case BUILT_IN_TM_LOG:
2744 case BUILT_IN_TM_LOG_1:
2745 case BUILT_IN_TM_LOG_2:
2746 case BUILT_IN_TM_LOG_4:
2747 case BUILT_IN_TM_LOG_8:
2748 case BUILT_IN_TM_LOG_FLOAT:
2749 case BUILT_IN_TM_LOG_DOUBLE:
2750 case BUILT_IN_TM_LOG_LDOUBLE:
2751 case BUILT_IN_TM_LOG_M64:
2752 case BUILT_IN_TM_LOG_M128:
2753 case BUILT_IN_TM_LOG_M256:
2754 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
2756 /* These read memory pointed to by the first argument. */
2757 case BUILT_IN_STRDUP:
2758 case BUILT_IN_STRNDUP:
2759 case BUILT_IN_REALLOC:
2761 ao_ref dref;
2762 tree size = NULL_TREE;
2763 if (gimple_call_num_args (call) == 2)
2764 size = gimple_call_arg (call, 1);
2765 ao_ref_init_from_ptr_and_size (&dref,
2766 gimple_call_arg (call, 0),
2767 size);
2768 return refs_may_alias_p_1 (&dref, ref, false);
2770 /* These read memory pointed to by the first argument. */
2771 case BUILT_IN_INDEX:
2772 case BUILT_IN_STRCHR:
2773 case BUILT_IN_STRRCHR:
2775 ao_ref dref;
2776 ao_ref_init_from_ptr_and_size (&dref,
2777 gimple_call_arg (call, 0),
2778 NULL_TREE);
2779 return refs_may_alias_p_1 (&dref, ref, false);
2781 /* These read memory pointed to by the first argument with size
2782 in the third argument. */
2783 case BUILT_IN_MEMCHR:
2785 ao_ref dref;
2786 ao_ref_init_from_ptr_and_size (&dref,
2787 gimple_call_arg (call, 0),
2788 gimple_call_arg (call, 2));
2789 return refs_may_alias_p_1 (&dref, ref, false);
2791 /* These read memory pointed to by the first and second arguments. */
2792 case BUILT_IN_STRSTR:
2793 case BUILT_IN_STRPBRK:
2795 ao_ref dref;
2796 ao_ref_init_from_ptr_and_size (&dref,
2797 gimple_call_arg (call, 0),
2798 NULL_TREE);
2799 if (refs_may_alias_p_1 (&dref, ref, false))
2800 return true;
2801 ao_ref_init_from_ptr_and_size (&dref,
2802 gimple_call_arg (call, 1),
2803 NULL_TREE);
2804 return refs_may_alias_p_1 (&dref, ref, false);
2807 /* The following builtins do not read from memory. */
2808 case BUILT_IN_FREE:
2809 case BUILT_IN_MALLOC:
2810 case BUILT_IN_POSIX_MEMALIGN:
2811 case BUILT_IN_ALIGNED_ALLOC:
2812 case BUILT_IN_CALLOC:
2813 CASE_BUILT_IN_ALLOCA:
2814 case BUILT_IN_STACK_SAVE:
2815 case BUILT_IN_STACK_RESTORE:
2816 case BUILT_IN_MEMSET:
2817 case BUILT_IN_TM_MEMSET:
2818 case BUILT_IN_MEMSET_CHK:
2819 case BUILT_IN_FREXP:
2820 case BUILT_IN_FREXPF:
2821 case BUILT_IN_FREXPL:
2822 case BUILT_IN_GAMMA_R:
2823 case BUILT_IN_GAMMAF_R:
2824 case BUILT_IN_GAMMAL_R:
2825 case BUILT_IN_LGAMMA_R:
2826 case BUILT_IN_LGAMMAF_R:
2827 case BUILT_IN_LGAMMAL_R:
2828 case BUILT_IN_MODF:
2829 case BUILT_IN_MODFF:
2830 case BUILT_IN_MODFL:
2831 case BUILT_IN_REMQUO:
2832 case BUILT_IN_REMQUOF:
2833 case BUILT_IN_REMQUOL:
2834 case BUILT_IN_SINCOS:
2835 case BUILT_IN_SINCOSF:
2836 case BUILT_IN_SINCOSL:
2837 case BUILT_IN_ASSUME_ALIGNED:
2838 case BUILT_IN_VA_END:
2839 return false;
2840 /* __sync_* builtins and some OpenMP builtins act as threading
2841 barriers. */
2842 #undef DEF_SYNC_BUILTIN
2843 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2844 #include "sync-builtins.def"
2845 #undef DEF_SYNC_BUILTIN
2846 case BUILT_IN_GOMP_ATOMIC_START:
2847 case BUILT_IN_GOMP_ATOMIC_END:
2848 case BUILT_IN_GOMP_BARRIER:
2849 case BUILT_IN_GOMP_BARRIER_CANCEL:
2850 case BUILT_IN_GOMP_TASKWAIT:
2851 case BUILT_IN_GOMP_TASKGROUP_END:
2852 case BUILT_IN_GOMP_CRITICAL_START:
2853 case BUILT_IN_GOMP_CRITICAL_END:
2854 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2855 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2856 case BUILT_IN_GOMP_LOOP_END:
2857 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2858 case BUILT_IN_GOMP_ORDERED_START:
2859 case BUILT_IN_GOMP_ORDERED_END:
2860 case BUILT_IN_GOMP_SECTIONS_END:
2861 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2862 case BUILT_IN_GOMP_SINGLE_COPY_START:
2863 case BUILT_IN_GOMP_SINGLE_COPY_END:
2864 return true;
2866 default:
2867 /* Fallthru to general call handling. */;
2870 /* Check if base is a global static variable that is not read
2871 by the function. */
2872 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2874 struct cgraph_node *node = cgraph_node::get (callee);
2875 bitmap read;
2876 int id;
2878 /* FIXME: Callee can be an OMP builtin that does not have a call graph
2879 node yet. We should enforce that there are nodes for all decls in the
2880 IL and remove this check instead. */
2881 if (node
2882 && (id = ipa_reference_var_uid (base)) != -1
2883 && (read = ipa_reference_get_read_global (node))
2884 && !bitmap_bit_p (read, id))
2885 goto process_args;
2888 /* Check if the base variable is call-used. */
2889 if (DECL_P (base))
2891 if (pt_solution_includes (gimple_call_use_set (call), base))
2892 return true;
2894 else if ((TREE_CODE (base) == MEM_REF
2895 || TREE_CODE (base) == TARGET_MEM_REF)
2896 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2898 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2899 if (!pi)
2900 return true;
2902 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
2903 return true;
2905 else
2906 return true;
2908 /* Inspect call arguments for passed-by-value aliases. */
2909 process_args:
2910 for (i = 0; i < gimple_call_num_args (call); ++i)
2912 tree op = gimple_call_arg (call, i);
2913 int flags = gimple_call_arg_flags (call, i);
2915 if (flags & EAF_UNUSED)
2916 continue;
2918 if (TREE_CODE (op) == WITH_SIZE_EXPR)
2919 op = TREE_OPERAND (op, 0);
2921 if (TREE_CODE (op) != SSA_NAME
2922 && !is_gimple_min_invariant (op))
2924 ao_ref r;
2925 ao_ref_init (&r, op);
2926 if (refs_may_alias_p_1 (&r, ref, tbaa_p))
2927 return true;
2931 return false;
2934 static bool
2935 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
2937 bool res;
2938 res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
2939 if (res)
2940 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
2941 else
2942 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
2943 return res;
2947 /* If the statement STMT may use the memory reference REF return
2948 true, otherwise return false. */
2950 bool
2951 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
2953 if (is_gimple_assign (stmt))
2955 tree rhs;
2957 /* All memory assign statements are single. */
2958 if (!gimple_assign_single_p (stmt))
2959 return false;
2961 rhs = gimple_assign_rhs1 (stmt);
2962 if (is_gimple_reg (rhs)
2963 || is_gimple_min_invariant (rhs)
2964 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
2965 return false;
2967 return refs_may_alias_p (rhs, ref, tbaa_p);
2969 else if (is_gimple_call (stmt))
2970 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
2971 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2973 tree retval = gimple_return_retval (return_stmt);
2974 if (retval
2975 && TREE_CODE (retval) != SSA_NAME
2976 && !is_gimple_min_invariant (retval)
2977 && refs_may_alias_p (retval, ref, tbaa_p))
2978 return true;
2979 /* If ref escapes the function then the return acts as a use. */
2980 tree base = ao_ref_base (ref);
2981 if (!base)
2983 else if (DECL_P (base))
2984 return is_global_var (base);
2985 else if (TREE_CODE (base) == MEM_REF
2986 || TREE_CODE (base) == TARGET_MEM_REF)
2987 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
2988 return false;
2991 return true;
2994 bool
2995 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
2997 ao_ref r;
2998 ao_ref_init (&r, ref);
2999 return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
3002 /* If the call in statement CALL may clobber the memory reference REF
3003 return true, otherwise return false. */
3005 bool
3006 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
3008 tree base;
3009 tree callee;
3011 /* If the call is pure or const it cannot clobber anything. */
3012 if (gimple_call_flags (call)
3013 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
3014 return false;
3015 if (gimple_call_internal_p (call))
3016 switch (gimple_call_internal_fn (call))
3018 /* Treat these internal calls like ECF_PURE for aliasing,
3019 they don't write to any memory the program should care about.
3020 They have important other side-effects, and read memory,
3021 so can't be ECF_NOVOPS. */
3022 case IFN_UBSAN_NULL:
3023 case IFN_UBSAN_BOUNDS:
3024 case IFN_UBSAN_VPTR:
3025 case IFN_UBSAN_OBJECT_SIZE:
3026 case IFN_UBSAN_PTR:
3027 case IFN_ASAN_CHECK:
3028 return false;
3029 default:
3030 break;
3033 callee = gimple_call_fndecl (call);
3035 if (callee != NULL_TREE && !ref->volatile_p)
3037 struct cgraph_node *node = cgraph_node::get (callee);
3038 if (node)
3040 modref_summary *summary = get_modref_function_summary (node);
3041 if (summary)
3043 if (!modref_may_conflict (call, summary->stores, ref, tbaa_p))
3045 alias_stats.modref_clobber_no_alias++;
3046 if (dump_file && (dump_flags & TDF_DETAILS))
3048 fprintf (dump_file,
3049 "ipa-modref: call stmt ");
3050 print_gimple_stmt (dump_file, call, 0);
3051 fprintf (dump_file,
3052 "ipa-modref: call to %s does not clobber ",
3053 node->dump_name ());
3054 if (!ref->ref && ref->base)
3056 fprintf (dump_file, "base: ");
3057 print_generic_expr (dump_file, ref->base);
3059 else if (ref->ref)
3061 fprintf (dump_file, "ref: ");
3062 print_generic_expr (dump_file, ref->ref);
3064 fprintf (dump_file, " alias sets: %i->%i\n",
3065 ao_ref_base_alias_set (ref),
3066 ao_ref_alias_set (ref));
3068 return false;
3070 alias_stats.modref_clobber_may_alias++;
3075 base = ao_ref_base (ref);
3076 if (!base)
3077 return true;
3079 if (TREE_CODE (base) == SSA_NAME
3080 || CONSTANT_CLASS_P (base))
3081 return false;
3083 /* A call that is not without side-effects might involve volatile
3084 accesses and thus conflicts with all other volatile accesses. */
3085 if (ref->volatile_p)
3086 return true;
3088 /* If the reference is based on a decl that is not aliased the call
3089 cannot possibly clobber it. */
3090 if (DECL_P (base)
3091 && !may_be_aliased (base)
3092 /* But local non-readonly statics can be modified through recursion
3093 or the call may implement a threading barrier which we must
3094 treat as may-def. */
3095 && (TREE_READONLY (base)
3096 || !is_global_var (base)))
3097 return false;
3099 /* If the reference is based on a pointer that points to memory
3100 that may not be written to then the call cannot possibly clobber it. */
3101 if ((TREE_CODE (base) == MEM_REF
3102 || TREE_CODE (base) == TARGET_MEM_REF)
3103 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
3104 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
3105 return false;
3107 /* Handle those builtin functions explicitly that do not act as
3108 escape points. See tree-ssa-structalias.c:find_func_aliases
3109 for the list of builtins we might need to handle here. */
3110 if (callee != NULL_TREE
3111 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
3112 switch (DECL_FUNCTION_CODE (callee))
3114 /* All the following functions clobber memory pointed to by
3115 their first argument. */
3116 case BUILT_IN_STRCPY:
3117 case BUILT_IN_STRNCPY:
3118 case BUILT_IN_MEMCPY:
3119 case BUILT_IN_MEMMOVE:
3120 case BUILT_IN_MEMPCPY:
3121 case BUILT_IN_STPCPY:
3122 case BUILT_IN_STPNCPY:
3123 case BUILT_IN_STRCAT:
3124 case BUILT_IN_STRNCAT:
3125 case BUILT_IN_MEMSET:
3126 case BUILT_IN_TM_MEMSET:
3127 CASE_BUILT_IN_TM_STORE (1):
3128 CASE_BUILT_IN_TM_STORE (2):
3129 CASE_BUILT_IN_TM_STORE (4):
3130 CASE_BUILT_IN_TM_STORE (8):
3131 CASE_BUILT_IN_TM_STORE (FLOAT):
3132 CASE_BUILT_IN_TM_STORE (DOUBLE):
3133 CASE_BUILT_IN_TM_STORE (LDOUBLE):
3134 CASE_BUILT_IN_TM_STORE (M64):
3135 CASE_BUILT_IN_TM_STORE (M128):
3136 CASE_BUILT_IN_TM_STORE (M256):
3137 case BUILT_IN_TM_MEMCPY:
3138 case BUILT_IN_TM_MEMMOVE:
3140 ao_ref dref;
3141 tree size = NULL_TREE;
3142 /* Don't pass in size for strncat, as the maximum size
3143 is strlen (dest) + n + 1 instead of n, resp.
3144 n + 1 at dest + strlen (dest), but strlen (dest) isn't
3145 known. */
3146 if (gimple_call_num_args (call) == 3
3147 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
3148 size = gimple_call_arg (call, 2);
3149 ao_ref_init_from_ptr_and_size (&dref,
3150 gimple_call_arg (call, 0),
3151 size);
3152 return refs_may_alias_p_1 (&dref, ref, false);
3154 case BUILT_IN_STRCPY_CHK:
3155 case BUILT_IN_STRNCPY_CHK:
3156 case BUILT_IN_MEMCPY_CHK:
3157 case BUILT_IN_MEMMOVE_CHK:
3158 case BUILT_IN_MEMPCPY_CHK:
3159 case BUILT_IN_STPCPY_CHK:
3160 case BUILT_IN_STPNCPY_CHK:
3161 case BUILT_IN_STRCAT_CHK:
3162 case BUILT_IN_STRNCAT_CHK:
3163 case BUILT_IN_MEMSET_CHK:
3165 ao_ref dref;
3166 tree size = NULL_TREE;
3167 /* Don't pass in size for __strncat_chk, as the maximum size
3168 is strlen (dest) + n + 1 instead of n, resp.
3169 n + 1 at dest + strlen (dest), but strlen (dest) isn't
3170 known. */
3171 if (gimple_call_num_args (call) == 4
3172 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
3173 size = gimple_call_arg (call, 2);
3174 ao_ref_init_from_ptr_and_size (&dref,
3175 gimple_call_arg (call, 0),
3176 size);
3177 return refs_may_alias_p_1 (&dref, ref, false);
3179 case BUILT_IN_BCOPY:
3181 ao_ref dref;
3182 tree size = gimple_call_arg (call, 2);
3183 ao_ref_init_from_ptr_and_size (&dref,
3184 gimple_call_arg (call, 1),
3185 size);
3186 return refs_may_alias_p_1 (&dref, ref, false);
3188 /* Allocating memory does not have any side-effects apart from
3189 being the definition point for the pointer. */
3190 case BUILT_IN_MALLOC:
3191 case BUILT_IN_ALIGNED_ALLOC:
3192 case BUILT_IN_CALLOC:
3193 case BUILT_IN_STRDUP:
3194 case BUILT_IN_STRNDUP:
3195 /* Unix98 specifies that errno is set on allocation failure. */
3196 if (flag_errno_math
3197 && targetm.ref_may_alias_errno (ref))
3198 return true;
3199 return false;
3200 case BUILT_IN_STACK_SAVE:
3201 CASE_BUILT_IN_ALLOCA:
3202 case BUILT_IN_ASSUME_ALIGNED:
3203 return false;
3204 /* But posix_memalign stores a pointer into the memory pointed to
3205 by its first argument. */
3206 case BUILT_IN_POSIX_MEMALIGN:
3208 tree ptrptr = gimple_call_arg (call, 0);
3209 ao_ref dref;
3210 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
3211 TYPE_SIZE_UNIT (ptr_type_node));
3212 return (refs_may_alias_p_1 (&dref, ref, false)
3213 || (flag_errno_math
3214 && targetm.ref_may_alias_errno (ref)));
3216 /* Freeing memory kills the pointed-to memory. More importantly
3217 the call has to serve as a barrier for moving loads and stores
3218 across it. */
3219 case BUILT_IN_FREE:
3220 case BUILT_IN_VA_END:
3222 tree ptr = gimple_call_arg (call, 0);
3223 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
3225 /* Realloc serves both as allocation point and deallocation point. */
3226 case BUILT_IN_REALLOC:
3228 tree ptr = gimple_call_arg (call, 0);
3229 /* Unix98 specifies that errno is set on allocation failure. */
3230 return ((flag_errno_math
3231 && targetm.ref_may_alias_errno (ref))
3232 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
3234 case BUILT_IN_GAMMA_R:
3235 case BUILT_IN_GAMMAF_R:
3236 case BUILT_IN_GAMMAL_R:
3237 case BUILT_IN_LGAMMA_R:
3238 case BUILT_IN_LGAMMAF_R:
3239 case BUILT_IN_LGAMMAL_R:
3241 tree out = gimple_call_arg (call, 1);
3242 if (ptr_deref_may_alias_ref_p_1 (out, ref))
3243 return true;
3244 if (flag_errno_math)
3245 break;
3246 return false;
3248 case BUILT_IN_FREXP:
3249 case BUILT_IN_FREXPF:
3250 case BUILT_IN_FREXPL:
3251 case BUILT_IN_MODF:
3252 case BUILT_IN_MODFF:
3253 case BUILT_IN_MODFL:
3255 tree out = gimple_call_arg (call, 1);
3256 return ptr_deref_may_alias_ref_p_1 (out, ref);
3258 case BUILT_IN_REMQUO:
3259 case BUILT_IN_REMQUOF:
3260 case BUILT_IN_REMQUOL:
3262 tree out = gimple_call_arg (call, 2);
3263 if (ptr_deref_may_alias_ref_p_1 (out, ref))
3264 return true;
3265 if (flag_errno_math)
3266 break;
3267 return false;
3269 case BUILT_IN_SINCOS:
3270 case BUILT_IN_SINCOSF:
3271 case BUILT_IN_SINCOSL:
3273 tree sin = gimple_call_arg (call, 1);
3274 tree cos = gimple_call_arg (call, 2);
3275 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
3276 || ptr_deref_may_alias_ref_p_1 (cos, ref));
3278 /* __sync_* builtins and some OpenMP builtins act as threading
3279 barriers. */
3280 #undef DEF_SYNC_BUILTIN
3281 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
3282 #include "sync-builtins.def"
3283 #undef DEF_SYNC_BUILTIN
3284 case BUILT_IN_GOMP_ATOMIC_START:
3285 case BUILT_IN_GOMP_ATOMIC_END:
3286 case BUILT_IN_GOMP_BARRIER:
3287 case BUILT_IN_GOMP_BARRIER_CANCEL:
3288 case BUILT_IN_GOMP_TASKWAIT:
3289 case BUILT_IN_GOMP_TASKGROUP_END:
3290 case BUILT_IN_GOMP_CRITICAL_START:
3291 case BUILT_IN_GOMP_CRITICAL_END:
3292 case BUILT_IN_GOMP_CRITICAL_NAME_START:
3293 case BUILT_IN_GOMP_CRITICAL_NAME_END:
3294 case BUILT_IN_GOMP_LOOP_END:
3295 case BUILT_IN_GOMP_LOOP_END_CANCEL:
3296 case BUILT_IN_GOMP_ORDERED_START:
3297 case BUILT_IN_GOMP_ORDERED_END:
3298 case BUILT_IN_GOMP_SECTIONS_END:
3299 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
3300 case BUILT_IN_GOMP_SINGLE_COPY_START:
3301 case BUILT_IN_GOMP_SINGLE_COPY_END:
3302 return true;
3303 default:
3304 /* Fallthru to general call handling. */;
3307 /* Check if base is a global static variable that is not written
3308 by the function. */
3309 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
3311 struct cgraph_node *node = cgraph_node::get (callee);
3312 bitmap written;
3313 int id;
3315 if (node
3316 && (id = ipa_reference_var_uid (base)) != -1
3317 && (written = ipa_reference_get_written_global (node))
3318 && !bitmap_bit_p (written, id))
3319 return false;
3322 /* Check if the base variable is call-clobbered. */
3323 if (DECL_P (base))
3324 return pt_solution_includes (gimple_call_clobber_set (call), base);
3325 else if ((TREE_CODE (base) == MEM_REF
3326 || TREE_CODE (base) == TARGET_MEM_REF)
3327 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
3329 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
3330 if (!pi)
3331 return true;
3333 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
3336 return true;
3339 /* If the call in statement CALL may clobber the memory reference REF
3340 return true, otherwise return false. */
3342 bool
3343 call_may_clobber_ref_p (gcall *call, tree ref, bool tbaa_p)
3345 bool res;
3346 ao_ref r;
3347 ao_ref_init (&r, ref);
3348 res = call_may_clobber_ref_p_1 (call, &r, tbaa_p);
3349 if (res)
3350 ++alias_stats.call_may_clobber_ref_p_may_alias;
3351 else
3352 ++alias_stats.call_may_clobber_ref_p_no_alias;
3353 return res;
3357 /* If the statement STMT may clobber the memory reference REF return true,
3358 otherwise return false. */
3360 bool
3361 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
3363 if (is_gimple_call (stmt))
3365 tree lhs = gimple_call_lhs (stmt);
3366 if (lhs
3367 && TREE_CODE (lhs) != SSA_NAME)
3369 ao_ref r;
3370 ao_ref_init (&r, lhs);
3371 if (refs_may_alias_p_1 (ref, &r, tbaa_p))
3372 return true;
3375 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref, tbaa_p);
3377 else if (gimple_assign_single_p (stmt))
3379 tree lhs = gimple_assign_lhs (stmt);
3380 if (TREE_CODE (lhs) != SSA_NAME)
3382 ao_ref r;
3383 ao_ref_init (&r, lhs);
3384 return refs_may_alias_p_1 (ref, &r, tbaa_p);
3387 else if (gimple_code (stmt) == GIMPLE_ASM)
3388 return true;
3390 return false;
3393 bool
3394 stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
3396 ao_ref r;
3397 ao_ref_init (&r, ref);
3398 return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
3401 /* Return true if store1 and store2 described by corresponding tuples
3402 <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
3403 address. */
3405 static bool
3406 same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
3407 poly_int64 max_size1,
3408 tree base2, poly_int64 offset2, poly_int64 size2,
3409 poly_int64 max_size2)
3411 /* Offsets need to be 0. */
3412 if (maybe_ne (offset1, 0)
3413 || maybe_ne (offset2, 0))
3414 return false;
3416 bool base1_obj_p = SSA_VAR_P (base1);
3417 bool base2_obj_p = SSA_VAR_P (base2);
3419 /* We need one object. */
3420 if (base1_obj_p == base2_obj_p)
3421 return false;
3422 tree obj = base1_obj_p ? base1 : base2;
3424 /* And we need one MEM_REF. */
3425 bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
3426 bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
3427 if (base1_memref_p == base2_memref_p)
3428 return false;
3429 tree memref = base1_memref_p ? base1 : base2;
3431 /* Sizes need to be valid. */
3432 if (!known_size_p (max_size1)
3433 || !known_size_p (max_size2)
3434 || !known_size_p (size1)
3435 || !known_size_p (size2))
3436 return false;
3438 /* Max_size needs to match size. */
3439 if (maybe_ne (max_size1, size1)
3440 || maybe_ne (max_size2, size2))
3441 return false;
3443 /* Sizes need to match. */
3444 if (maybe_ne (size1, size2))
3445 return false;
3448 /* Check that memref is a store to pointer with singleton points-to info. */
3449 if (!integer_zerop (TREE_OPERAND (memref, 1)))
3450 return false;
3451 tree ptr = TREE_OPERAND (memref, 0);
3452 if (TREE_CODE (ptr) != SSA_NAME)
3453 return false;
3454 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3455 unsigned int pt_uid;
3456 if (pi == NULL
3457 || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid))
3458 return false;
3460 /* Be conservative with non-call exceptions when the address might
3461 be NULL. */
3462 if (cfun->can_throw_non_call_exceptions && pi->pt.null)
3463 return false;
3465 /* Check that ptr points relative to obj. */
3466 unsigned int obj_uid = DECL_PT_UID (obj);
3467 if (obj_uid != pt_uid)
3468 return false;
3470 /* Check that the object size is the same as the store size. That ensures us
3471 that ptr points to the start of obj. */
3472 return (DECL_SIZE (obj)
3473 && poly_int_tree_p (DECL_SIZE (obj))
3474 && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
3477 /* If STMT kills the memory reference REF return true, otherwise
3478 return false. */
3480 bool
3481 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
3483 if (!ao_ref_base (ref))
3484 return false;
3486 if (gimple_has_lhs (stmt)
3487 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
3488 /* The assignment is not necessarily carried out if it can throw
3489 and we can catch it in the current function where we could inspect
3490 the previous value.
3491 ??? We only need to care about the RHS throwing. For aggregate
3492 assignments or similar calls and non-call exceptions the LHS
3493 might throw as well. */
3494 && !stmt_can_throw_internal (cfun, stmt))
3496 tree lhs = gimple_get_lhs (stmt);
3497 /* If LHS is literally a base of the access we are done. */
3498 if (ref->ref)
3500 tree base = ref->ref;
3501 tree innermost_dropped_array_ref = NULL_TREE;
3502 if (handled_component_p (base))
3504 tree saved_lhs0 = NULL_TREE;
3505 if (handled_component_p (lhs))
3507 saved_lhs0 = TREE_OPERAND (lhs, 0);
3508 TREE_OPERAND (lhs, 0) = integer_zero_node;
3512 /* Just compare the outermost handled component, if
3513 they are equal we have found a possible common
3514 base. */
3515 tree saved_base0 = TREE_OPERAND (base, 0);
3516 TREE_OPERAND (base, 0) = integer_zero_node;
3517 bool res = operand_equal_p (lhs, base, 0);
3518 TREE_OPERAND (base, 0) = saved_base0;
3519 if (res)
3520 break;
3521 /* Remember if we drop an array-ref that we need to
3522 double-check not being at struct end. */
3523 if (TREE_CODE (base) == ARRAY_REF
3524 || TREE_CODE (base) == ARRAY_RANGE_REF)
3525 innermost_dropped_array_ref = base;
3526 /* Otherwise drop handled components of the access. */
3527 base = saved_base0;
3529 while (handled_component_p (base));
3530 if (saved_lhs0)
3531 TREE_OPERAND (lhs, 0) = saved_lhs0;
3533 /* Finally check if the lhs has the same address and size as the
3534 base candidate of the access. Watch out if we have dropped
3535 an array-ref that was at struct end, this means ref->ref may
3536 be outside of the TYPE_SIZE of its base. */
3537 if ((! innermost_dropped_array_ref
3538 || ! array_at_struct_end_p (innermost_dropped_array_ref))
3539 && (lhs == base
3540 || (((TYPE_SIZE (TREE_TYPE (lhs))
3541 == TYPE_SIZE (TREE_TYPE (base)))
3542 || (TYPE_SIZE (TREE_TYPE (lhs))
3543 && TYPE_SIZE (TREE_TYPE (base))
3544 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
3545 TYPE_SIZE (TREE_TYPE (base)),
3546 0)))
3547 && operand_equal_p (lhs, base,
3548 OEP_ADDRESS_OF
3549 | OEP_MATCH_SIDE_EFFECTS))))
3550 return true;
3553 /* Now look for non-literal equal bases with the restriction of
3554 handling constant offset and size. */
3555 /* For a must-alias check we need to be able to constrain
3556 the access properly. */
3557 if (!ref->max_size_known_p ())
3558 return false;
3559 poly_int64 size, offset, max_size, ref_offset = ref->offset;
3560 bool reverse;
3561 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
3562 &reverse);
3563 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
3564 so base == ref->base does not always hold. */
3565 if (base != ref->base)
3567 /* Try using points-to info. */
3568 if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
3569 ref->offset, ref->size, ref->max_size))
3570 return true;
3572 /* If both base and ref->base are MEM_REFs, only compare the
3573 first operand, and if the second operand isn't equal constant,
3574 try to add the offsets into offset and ref_offset. */
3575 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
3576 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
3578 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
3579 TREE_OPERAND (ref->base, 1)))
3581 poly_offset_int off1 = mem_ref_offset (base);
3582 off1 <<= LOG2_BITS_PER_UNIT;
3583 off1 += offset;
3584 poly_offset_int off2 = mem_ref_offset (ref->base);
3585 off2 <<= LOG2_BITS_PER_UNIT;
3586 off2 += ref_offset;
3587 if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
3588 size = -1;
3591 else
3592 size = -1;
3594 /* For a must-alias check we need to be able to constrain
3595 the access properly. */
3596 if (known_eq (size, max_size)
3597 && known_subrange_p (ref_offset, ref->max_size, offset, size))
3598 return true;
3601 if (is_gimple_call (stmt))
3603 tree callee = gimple_call_fndecl (stmt);
3604 if (callee != NULL_TREE
3605 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3606 switch (DECL_FUNCTION_CODE (callee))
3608 case BUILT_IN_FREE:
3610 tree ptr = gimple_call_arg (stmt, 0);
3611 tree base = ao_ref_base (ref);
3612 if (base && TREE_CODE (base) == MEM_REF
3613 && TREE_OPERAND (base, 0) == ptr)
3614 return true;
3615 break;
3618 case BUILT_IN_MEMCPY:
3619 case BUILT_IN_MEMPCPY:
3620 case BUILT_IN_MEMMOVE:
3621 case BUILT_IN_MEMSET:
3622 case BUILT_IN_MEMCPY_CHK:
3623 case BUILT_IN_MEMPCPY_CHK:
3624 case BUILT_IN_MEMMOVE_CHK:
3625 case BUILT_IN_MEMSET_CHK:
3626 case BUILT_IN_STRNCPY:
3627 case BUILT_IN_STPNCPY:
3628 case BUILT_IN_CALLOC:
3630 /* For a must-alias check we need to be able to constrain
3631 the access properly. */
3632 if (!ref->max_size_known_p ())
3633 return false;
3634 tree dest;
3635 tree len;
3637 /* In execution order a calloc call will never kill
3638 anything. However, DSE will (ab)use this interface
3639 to ask if a calloc call writes the same memory locations
3640 as a later assignment, memset, etc. So handle calloc
3641 in the expected way. */
3642 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
3644 tree arg0 = gimple_call_arg (stmt, 0);
3645 tree arg1 = gimple_call_arg (stmt, 1);
3646 if (TREE_CODE (arg0) != INTEGER_CST
3647 || TREE_CODE (arg1) != INTEGER_CST)
3648 return false;
3650 dest = gimple_call_lhs (stmt);
3651 if (!dest)
3652 return false;
3653 len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
3655 else
3657 dest = gimple_call_arg (stmt, 0);
3658 len = gimple_call_arg (stmt, 2);
3660 if (!poly_int_tree_p (len))
3661 return false;
3662 tree rbase = ref->base;
3663 poly_offset_int roffset = ref->offset;
3664 ao_ref dref;
3665 ao_ref_init_from_ptr_and_size (&dref, dest, len);
3666 tree base = ao_ref_base (&dref);
3667 poly_offset_int offset = dref.offset;
3668 if (!base || !known_size_p (dref.size))
3669 return false;
3670 if (TREE_CODE (base) == MEM_REF)
3672 if (TREE_CODE (rbase) != MEM_REF)
3673 return false;
3674 // Compare pointers.
3675 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
3676 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
3677 base = TREE_OPERAND (base, 0);
3678 rbase = TREE_OPERAND (rbase, 0);
3680 if (base == rbase
3681 && known_subrange_p (roffset, ref->max_size, offset,
3682 wi::to_poly_offset (len)
3683 << LOG2_BITS_PER_UNIT))
3684 return true;
3685 break;
3688 case BUILT_IN_VA_END:
3690 tree ptr = gimple_call_arg (stmt, 0);
3691 if (TREE_CODE (ptr) == ADDR_EXPR)
3693 tree base = ao_ref_base (ref);
3694 if (TREE_OPERAND (ptr, 0) == base)
3695 return true;
3697 break;
3700 default:;
3703 return false;
3706 bool
3707 stmt_kills_ref_p (gimple *stmt, tree ref)
3709 ao_ref r;
3710 ao_ref_init (&r, ref);
3711 return stmt_kills_ref_p (stmt, &r);
3715 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3716 TARGET or a statement clobbering the memory reference REF in which
3717 case false is returned. The walk starts with VUSE, one argument of PHI. */
3719 static bool
3720 maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3721 ao_ref *ref, tree vuse, bool tbaa_p, unsigned int &limit,
3722 bitmap *visited, bool abort_on_visited,
3723 void *(*translate)(ao_ref *, tree, void *, translate_flags *),
3724 translate_flags disambiguate_only,
3725 void *data)
3727 basic_block bb = gimple_bb (phi);
3729 if (!*visited)
3730 *visited = BITMAP_ALLOC (NULL);
3732 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3734 /* Walk until we hit the target. */
3735 while (vuse != target)
3737 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3738 /* If we are searching for the target VUSE by walking up to
3739 TARGET_BB dominating the original PHI we are finished once
3740 we reach a default def or a definition in a block dominating
3741 that block. Update TARGET and return. */
3742 if (!target
3743 && (gimple_nop_p (def_stmt)
3744 || dominated_by_p (CDI_DOMINATORS,
3745 target_bb, gimple_bb (def_stmt))))
3747 target = vuse;
3748 return true;
3751 /* Recurse for PHI nodes. */
3752 if (gimple_code (def_stmt) == GIMPLE_PHI)
3754 /* An already visited PHI node ends the walk successfully. */
3755 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
3756 return !abort_on_visited;
3757 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3758 visited, abort_on_visited,
3759 translate, data, disambiguate_only);
3760 if (!vuse)
3761 return false;
3762 continue;
3764 else if (gimple_nop_p (def_stmt))
3765 return false;
3766 else
3768 /* A clobbering statement or the end of the IL ends it failing. */
3769 if ((int)limit <= 0)
3770 return false;
3771 --limit;
3772 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3774 translate_flags tf = disambiguate_only;
3775 if (translate
3776 && (*translate) (ref, vuse, data, &tf) == NULL)
3778 else
3779 return false;
3782 /* If we reach a new basic-block see if we already skipped it
3783 in a previous walk that ended successfully. */
3784 if (gimple_bb (def_stmt) != bb)
3786 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3787 return !abort_on_visited;
3788 bb = gimple_bb (def_stmt);
3790 vuse = gimple_vuse (def_stmt);
3792 return true;
3796 /* Starting from a PHI node for the virtual operand of the memory reference
3797 REF find a continuation virtual operand that allows to continue walking
3798 statements dominating PHI skipping only statements that cannot possibly
3799 clobber REF. Decrements LIMIT for each alias disambiguation done
3800 and aborts the walk, returning NULL_TREE if it reaches zero.
3801 Returns NULL_TREE if no suitable virtual operand can be found. */
3803 tree
3804 get_continuation_for_phi (gimple *phi, ao_ref *ref, bool tbaa_p,
3805 unsigned int &limit, bitmap *visited,
3806 bool abort_on_visited,
3807 void *(*translate)(ao_ref *, tree, void *,
3808 translate_flags *),
3809 void *data,
3810 translate_flags disambiguate_only)
3812 unsigned nargs = gimple_phi_num_args (phi);
3814 /* Through a single-argument PHI we can simply look through. */
3815 if (nargs == 1)
3816 return PHI_ARG_DEF (phi, 0);
3818 /* For two or more arguments try to pairwise skip non-aliasing code
3819 until we hit the phi argument definition that dominates the other one. */
3820 basic_block phi_bb = gimple_bb (phi);
3821 tree arg0, arg1;
3822 unsigned i;
3824 /* Find a candidate for the virtual operand which definition
3825 dominates those of all others. */
3826 /* First look if any of the args themselves satisfy this. */
3827 for (i = 0; i < nargs; ++i)
3829 arg0 = PHI_ARG_DEF (phi, i);
3830 if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3831 break;
3832 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3833 if (def_bb != phi_bb
3834 && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3835 break;
3836 arg0 = NULL_TREE;
3838 /* If not, look if we can reach such candidate by walking defs
3839 until we hit the immediate dominator. maybe_skip_until will
3840 do that for us. */
3841 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3843 /* Then check against the (to be) found candidate. */
3844 for (i = 0; i < nargs; ++i)
3846 arg1 = PHI_ARG_DEF (phi, i);
3847 if (arg1 == arg0)
3849 else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, tbaa_p,
3850 limit, visited,
3851 abort_on_visited,
3852 translate,
3853 /* Do not valueize when walking over
3854 backedges. */
3855 dominated_by_p
3856 (CDI_DOMINATORS,
3857 gimple_bb (SSA_NAME_DEF_STMT (arg1)),
3858 phi_bb)
3859 ? TR_DISAMBIGUATE
3860 : disambiguate_only, data))
3861 return NULL_TREE;
3864 return arg0;
3867 /* Based on the memory reference REF and its virtual use VUSE call
3868 WALKER for each virtual use that is equivalent to VUSE, including VUSE
3869 itself. That is, for each virtual use for which its defining statement
3870 does not clobber REF.
3872 WALKER is called with REF, the current virtual use and DATA. If
3873 WALKER returns non-NULL the walk stops and its result is returned.
3874 At the end of a non-successful walk NULL is returned.
3876 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3877 use which definition is a statement that may clobber REF and DATA.
3878 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3879 If TRANSLATE returns non-NULL the walk stops and its result is returned.
3880 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3881 to adjust REF and *DATA to make that valid.
3883 VALUEIZE if non-NULL is called with the next VUSE that is considered
3884 and return value is substituted for that. This can be used to
3885 implement optimistic value-numbering for example. Note that the
3886 VUSE argument is assumed to be valueized already.
3888 LIMIT specifies the number of alias queries we are allowed to do,
3889 the walk stops when it reaches zero and NULL is returned. LIMIT
3890 is decremented by the number of alias queries (plus adjustments
3891 done by the callbacks) upon return.
3893 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3895 void *
3896 walk_non_aliased_vuses (ao_ref *ref, tree vuse, bool tbaa_p,
3897 void *(*walker)(ao_ref *, tree, void *),
3898 void *(*translate)(ao_ref *, tree, void *,
3899 translate_flags *),
3900 tree (*valueize)(tree),
3901 unsigned &limit, void *data)
3903 bitmap visited = NULL;
3904 void *res;
3905 bool translated = false;
3907 timevar_push (TV_ALIAS_STMT_WALK);
3911 gimple *def_stmt;
3913 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3914 res = (*walker) (ref, vuse, data);
3915 /* Abort walk. */
3916 if (res == (void *)-1)
3918 res = NULL;
3919 break;
3921 /* Lookup succeeded. */
3922 else if (res != NULL)
3923 break;
3925 if (valueize)
3927 vuse = valueize (vuse);
3928 if (!vuse)
3930 res = NULL;
3931 break;
3934 def_stmt = SSA_NAME_DEF_STMT (vuse);
3935 if (gimple_nop_p (def_stmt))
3936 break;
3937 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3938 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3939 &visited, translated, translate, data);
3940 else
3942 if ((int)limit <= 0)
3944 res = NULL;
3945 break;
3947 --limit;
3948 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3950 if (!translate)
3951 break;
3952 translate_flags disambiguate_only = TR_TRANSLATE;
3953 res = (*translate) (ref, vuse, data, &disambiguate_only);
3954 /* Failed lookup and translation. */
3955 if (res == (void *)-1)
3957 res = NULL;
3958 break;
3960 /* Lookup succeeded. */
3961 else if (res != NULL)
3962 break;
3963 /* Translation succeeded, continue walking. */
3964 translated = translated || disambiguate_only == TR_TRANSLATE;
3966 vuse = gimple_vuse (def_stmt);
3969 while (vuse);
3971 if (visited)
3972 BITMAP_FREE (visited);
3974 timevar_pop (TV_ALIAS_STMT_WALK);
3976 return res;
3980 /* Based on the memory reference REF call WALKER for each vdef which
3981 defining statement may clobber REF, starting with VDEF. If REF
3982 is NULL_TREE, each defining statement is visited.
3984 WALKER is called with REF, the current vdef and DATA. If WALKER
3985 returns true the walk is stopped, otherwise it continues.
3987 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
3988 The pointer may be NULL and then we do not track this information.
3990 At PHI nodes walk_aliased_vdefs forks into one walk for reach
3991 PHI argument (but only one walk continues on merge points), the
3992 return value is true if any of the walks was successful.
3994 The function returns the number of statements walked or -1 if
3995 LIMIT stmts were walked and the walk was aborted at this point.
3996 If LIMIT is zero the walk is not aborted. */
3998 static int
3999 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
4000 bool (*walker)(ao_ref *, tree, void *), void *data,
4001 bitmap *visited, unsigned int cnt,
4002 bool *function_entry_reached, unsigned limit)
4006 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
4008 if (*visited
4009 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
4010 return cnt;
4012 if (gimple_nop_p (def_stmt))
4014 if (function_entry_reached)
4015 *function_entry_reached = true;
4016 return cnt;
4018 else if (gimple_code (def_stmt) == GIMPLE_PHI)
4020 unsigned i;
4021 if (!*visited)
4022 *visited = BITMAP_ALLOC (NULL);
4023 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
4025 int res = walk_aliased_vdefs_1 (ref,
4026 gimple_phi_arg_def (def_stmt, i),
4027 walker, data, visited, cnt,
4028 function_entry_reached, limit);
4029 if (res == -1)
4030 return -1;
4031 cnt = res;
4033 return cnt;
4036 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
4037 cnt++;
4038 if (cnt == limit)
4039 return -1;
4040 if ((!ref
4041 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
4042 && (*walker) (ref, vdef, data))
4043 return cnt;
4045 vdef = gimple_vuse (def_stmt);
4047 while (1);
4051 walk_aliased_vdefs (ao_ref *ref, tree vdef,
4052 bool (*walker)(ao_ref *, tree, void *), void *data,
4053 bitmap *visited,
4054 bool *function_entry_reached, unsigned int limit)
4056 bitmap local_visited = NULL;
4057 int ret;
4059 timevar_push (TV_ALIAS_STMT_WALK);
4061 if (function_entry_reached)
4062 *function_entry_reached = false;
4064 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
4065 visited ? visited : &local_visited, 0,
4066 function_entry_reached, limit);
4067 if (local_visited)
4068 BITMAP_FREE (local_visited);
4070 timevar_pop (TV_ALIAS_STMT_WALK);
4072 return ret;
4075 /* Verify validity of the fnspec string.
4076 See attr-fnspec.h for details. */
4078 void
4079 attr_fnspec::verify ()
4081 bool err = false;
4083 /* Check return value specifier. */
4084 if (len < return_desc_size)
4085 err = true;
4086 else if ((len - return_desc_size) % arg_desc_size)
4087 err = true;
4088 else if ((str[0] < '1' || str[0] > '4')
4089 && str[0] != '.' && str[0] != 'm'
4090 /* FIXME: Fortran trans-decl.c contains multiple wrong fnspec
4091 strings. The following characters have no meaning. */
4092 && str[0] != 'R' && str[0] != 'W')
4093 err = true;
4095 if (str[1] != ' ')
4096 err = true;
4098 /* Now check all parameters. */
4099 for (unsigned int i = 0; arg_specified_p (i); i++)
4101 unsigned int idx = arg_idx (i);
4102 switch (str[idx])
4104 case 'x':
4105 case 'X':
4106 case 'r':
4107 case 'R':
4108 case 'w':
4109 case 'W':
4110 case '.':
4111 break;
4112 default:
4113 err = true;
4115 if (str[idx + 1] != ' ')
4116 err = true;
4118 if (err)
4119 internal_error ("invalid fn spec attribute \"%s\"", str);