]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-alias.c
* postreload.c (last_label_ruid, first_index_reg, last_index_reg):
[thirdparty/gcc.git] / gcc / tree-ssa-alias.c
CommitLineData
4ee9c684 1/* Alias analysis for trees.
7cf0dbf3 2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
cfaf579d 3 Free Software Foundation, Inc.
4ee9c684 4 Contributed by Diego Novillo <dnovillo@redhat.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
8c4c00c1 10the Free Software Foundation; either version 3, or (at your option)
4ee9c684 11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
4ee9c684 21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "tree.h"
4ee9c684 27#include "tm_p.h"
4ee9c684 28#include "basic-block.h"
29#include "timevar.h"
4ee9c684 30#include "ggc.h"
31#include "langhooks.h"
32#include "flags.h"
7749ee1c 33#include "toplev.h"
4ee9c684 34#include "function.h"
ce084dfc 35#include "tree-pretty-print.h"
4ee9c684 36#include "tree-dump.h"
75a70cf9 37#include "gimple.h"
4ee9c684 38#include "tree-flow.h"
39#include "tree-inline.h"
4ee9c684 40#include "tree-pass.h"
41#include "convert.h"
42#include "params.h"
f7d118a9 43#include "ipa-type-escape.h"
2be14d8b 44#include "vec.h"
a55dc2cd 45#include "bitmap.h"
05f3df98 46#include "vecprim.h"
4fb5e5ca 47#include "pointer-set.h"
6cc9bd27 48#include "alloc-pool.h"
dd277d48 49#include "tree-ssa-alias.h"
a55dc2cd 50
dd277d48 51/* Broad overview of how alias analysis on gimple works:
4fb5e5ca 52
dd277d48 53 Statements clobbering or using memory are linked through the
54 virtual operand factored use-def chain. The virtual operand
55 is unique per function, its symbol is accessible via gimple_vop (cfun).
56 Virtual operands are used for efficiently walking memory statements
57 in the gimple IL and are useful for things like value-numbering as
58 a generation count for memory references.
339d7079 59
dd277d48 60 SSA_NAME pointers may have associated points-to information
61 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
62 points-to information is (re-)computed by the TODO_rebuild_alias
63 pass manager todo. Points-to information is also used for more
64 precise tracking of call-clobbered and call-used variables and
65 related disambiguations.
339d7079 66
dd277d48 67 This file contains functions for disambiguating memory references,
68 the so called alias-oracle and tools for walking of the gimple IL.
339d7079 69
dd277d48 70 The main alias-oracle entry-points are
339d7079 71
dd277d48 72 bool stmt_may_clobber_ref_p (gimple, tree)
339d7079 73
dd277d48 74 This function queries if a statement may invalidate (parts of)
75 the memory designated by the reference tree argument.
339d7079 76
dd277d48 77 bool ref_maybe_used_by_stmt_p (gimple, tree)
339d7079 78
dd277d48 79 This function queries if a statement may need (parts of) the
80 memory designated by the reference tree argument.
339d7079 81
dd277d48 82 There are variants of these functions that only handle the call
83 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
84 Note that these do not disambiguate against a possible call lhs.
339d7079 85
dd277d48 86 bool refs_may_alias_p (tree, tree)
339d7079 87
dd277d48 88 This function tries to disambiguate two reference trees.
05931b00 89
dd277d48 90 bool ptr_deref_may_alias_global_p (tree)
339d7079 91
dd277d48 92 This function queries if dereferencing a pointer variable may
93 alias global memory.
339d7079 94
dd277d48 95 More low-level disambiguators are available and documented in
96 this file. Low-level disambiguators dealing with points-to
97 information are in tree-ssa-structalias.c. */
339d7079 98
339d7079 99
dd277d48 100/* Query statistics for the different low-level disambiguators.
101 A high-level query may trigger multiple of them. */
339d7079 102
dd277d48 103static struct {
104 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
105 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
106 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
107 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
108 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
109 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
110} alias_stats;
339d7079 111
dd277d48 112void
113dump_alias_stats (FILE *s)
114{
115 fprintf (s, "\nAlias oracle query stats:\n");
116 fprintf (s, " refs_may_alias_p: "
117 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
118 HOST_WIDE_INT_PRINT_DEC" queries\n",
119 alias_stats.refs_may_alias_p_no_alias,
120 alias_stats.refs_may_alias_p_no_alias
121 + alias_stats.refs_may_alias_p_may_alias);
122 fprintf (s, " ref_maybe_used_by_call_p: "
123 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
124 HOST_WIDE_INT_PRINT_DEC" queries\n",
125 alias_stats.ref_maybe_used_by_call_p_no_alias,
126 alias_stats.refs_may_alias_p_no_alias
127 + alias_stats.ref_maybe_used_by_call_p_may_alias);
128 fprintf (s, " call_may_clobber_ref_p: "
129 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
130 HOST_WIDE_INT_PRINT_DEC" queries\n",
131 alias_stats.call_may_clobber_ref_p_no_alias,
132 alias_stats.call_may_clobber_ref_p_no_alias
133 + alias_stats.call_may_clobber_ref_p_may_alias);
134}
135
136
137/* Return true, if dereferencing PTR may alias with a global variable. */
4ee9c684 138
dd277d48 139bool
140ptr_deref_may_alias_global_p (tree ptr)
4ee9c684 141{
dd277d48 142 struct ptr_info_def *pi;
4fb5e5ca 143
dd277d48 144 /* If we end up with a pointer constant here that may point
145 to global memory. */
146 if (TREE_CODE (ptr) != SSA_NAME)
147 return true;
4ee9c684 148
dd277d48 149 pi = SSA_NAME_PTR_INFO (ptr);
4ee9c684 150
dd277d48 151 /* If we do not have points-to information for this variable,
152 we have to punt. */
153 if (!pi)
154 return true;
4ee9c684 155
2a3ebafa 156 /* ??? This does not use TBAA to prune globals ptr may not access. */
dd277d48 157 return pt_solution_includes_global (&pi->pt);
4ee9c684 158}
159
2a3ebafa 160/* Return true if dereferencing PTR may alias DECL.
161 The caller is responsible for applying TBAA to see if PTR
162 may access DECL at all. */
4ee9c684 163
dd277d48 164static bool
165ptr_deref_may_alias_decl_p (tree ptr, tree decl)
4ee9c684 166{
dd277d48 167 struct ptr_info_def *pi;
f7d118a9 168
f85fb819 169 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
170 || TREE_CODE (ptr) == ADDR_EXPR
171 || TREE_CODE (ptr) == INTEGER_CST)
172 && (TREE_CODE (decl) == VAR_DECL
173 || TREE_CODE (decl) == PARM_DECL
174 || TREE_CODE (decl) == RESULT_DECL));
2fd0b5dc 175
dd277d48 176 /* Non-aliased variables can not be pointed to. */
177 if (!may_be_aliased (decl))
178 return false;
2fd0b5dc 179
047fdd47 180 /* ADDR_EXPR pointers either just offset another pointer or directly
181 specify the pointed-to set. */
182 if (TREE_CODE (ptr) == ADDR_EXPR)
183 {
184 tree base = get_base_address (TREE_OPERAND (ptr, 0));
185 if (base
182cf5a9 186 && (INDIRECT_REF_P (base)
187 || TREE_CODE (base) == MEM_REF))
047fdd47 188 ptr = TREE_OPERAND (base, 0);
189 else if (base
190 && SSA_VAR_P (base))
7f2d9047 191 return base == decl;
047fdd47 192 else if (base
193 && CONSTANT_CLASS_P (base))
194 return false;
195 else
196 return true;
197 }
198
f85fb819 199 /* We can end up with dereferencing constant pointers.
047fdd47 200 Just bail out in this case. */
f85fb819 201 if (TREE_CODE (ptr) == INTEGER_CST)
047fdd47 202 return true;
203
dd277d48 204 /* If we do not have useful points-to information for this pointer
205 we cannot disambiguate anything else. */
206 pi = SSA_NAME_PTR_INFO (ptr);
207 if (!pi)
2fd0b5dc 208 return true;
209
8bb76364 210 /* If the decl can be used as a restrict tag and we have a restrict
211 pointer and that pointers points-to set doesn't contain this decl
212 then they can't alias. */
213 if (DECL_RESTRICTED_P (decl)
214 && TYPE_RESTRICT (TREE_TYPE (ptr))
215 && pi->pt.vars_contains_restrict)
1a981e1a 216 return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
8bb76364 217
dd277d48 218 return pt_solution_includes (&pi->pt, decl);
2fd0b5dc 219}
4ee9c684 220
2a3ebafa 221/* Return true if dereferenced PTR1 and PTR2 may alias.
222 The caller is responsible for applying TBAA to see if accesses
223 through PTR1 and PTR2 may conflict at all. */
4ee9c684 224
dd277d48 225static bool
226ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
4ee9c684 227{
dd277d48 228 struct ptr_info_def *pi1, *pi2;
4ee9c684 229
f85fb819 230 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
231 || TREE_CODE (ptr1) == ADDR_EXPR
232 || TREE_CODE (ptr1) == INTEGER_CST)
233 && (TREE_CODE (ptr2) == SSA_NAME
234 || TREE_CODE (ptr2) == ADDR_EXPR
235 || TREE_CODE (ptr2) == INTEGER_CST));
236
047fdd47 237 /* ADDR_EXPR pointers either just offset another pointer or directly
238 specify the pointed-to set. */
239 if (TREE_CODE (ptr1) == ADDR_EXPR)
240 {
241 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
242 if (base
182cf5a9 243 && (INDIRECT_REF_P (base)
244 || TREE_CODE (base) == MEM_REF))
047fdd47 245 ptr1 = TREE_OPERAND (base, 0);
246 else if (base
247 && SSA_VAR_P (base))
248 return ptr_deref_may_alias_decl_p (ptr2, base);
249 else
250 return true;
251 }
252 if (TREE_CODE (ptr2) == ADDR_EXPR)
253 {
254 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
255 if (base
182cf5a9 256 && (INDIRECT_REF_P (base)
257 || TREE_CODE (base) == MEM_REF))
047fdd47 258 ptr2 = TREE_OPERAND (base, 0);
259 else if (base
260 && SSA_VAR_P (base))
261 return ptr_deref_may_alias_decl_p (ptr1, base);
262 else
263 return true;
264 }
265
f85fb819 266 /* We can end up with dereferencing constant pointers.
047fdd47 267 Just bail out in this case. */
f85fb819 268 if (TREE_CODE (ptr1) == INTEGER_CST
269 || TREE_CODE (ptr2) == INTEGER_CST)
dd277d48 270 return true;
4ee9c684 271
dd277d48 272 /* We may end up with two empty points-to solutions for two same pointers.
273 In this case we still want to say both pointers alias, so shortcut
274 that here. */
275 if (ptr1 == ptr2)
276 return true;
81d08033 277
dd277d48 278 /* If we do not have useful points-to information for either pointer
279 we cannot disambiguate anything else. */
280 pi1 = SSA_NAME_PTR_INFO (ptr1);
281 pi2 = SSA_NAME_PTR_INFO (ptr2);
282 if (!pi1 || !pi2)
283 return true;
81d08033 284
1c1f1bc0 285 /* If both pointers are restrict-qualified try to disambiguate
286 with restrict information. */
287 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
288 && TYPE_RESTRICT (TREE_TYPE (ptr2))
289 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
290 return false;
291
2a3ebafa 292 /* ??? This does not use TBAA to prune decls from the intersection
293 that not both pointers may access. */
dd277d48 294 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
81d08033 295}
296
047fdd47 297/* Return true if dereferencing PTR may alias *REF.
298 The caller is responsible for applying TBAA to see if PTR
299 may access *REF at all. */
300
301static bool
302ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
303{
304 tree base = ao_ref_base (ref);
305
182cf5a9 306 if (INDIRECT_REF_P (base)
307 || TREE_CODE (base) == MEM_REF)
047fdd47 308 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
309 else if (SSA_VAR_P (base))
310 return ptr_deref_may_alias_decl_p (ptr, base);
311
312 return true;
313}
314
4ee9c684 315
316/* Dump alias information on FILE. */
317
318void
319dump_alias_info (FILE *file)
320{
321 size_t i;
322 const char *funcname
5135beeb 323 = lang_hooks.decl_printable_name (current_function_decl, 2);
a55dc2cd 324 referenced_var_iterator rvi;
325 tree var;
4ee9c684 326
dd277d48 327 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
81d08033 328
329 fprintf (file, "Aliased symbols\n\n");
48e1416a 330
a55dc2cd 331 FOR_EACH_REFERENCED_VAR (var, rvi)
81d08033 332 {
81d08033 333 if (may_be_aliased (var))
334 dump_variable (file, var);
335 }
336
7f81b5ee 337 fprintf (file, "\nCall clobber information\n");
338
339 fprintf (file, "\nESCAPED");
340 dump_points_to_solution (file, &cfun->gimple_df->escaped);
7f81b5ee 341
342 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
81d08033 343
81d08033 344 for (i = 1; i < num_ssa_names; i++)
345 {
346 tree ptr = ssa_name (i);
a48b1470 347 struct ptr_info_def *pi;
48e1416a 348
dd277d48 349 if (ptr == NULL_TREE
350 || SSA_NAME_IN_FREE_LIST (ptr))
a48b1470 351 continue;
352
353 pi = SSA_NAME_PTR_INFO (ptr);
dd277d48 354 if (pi)
81d08033 355 dump_points_to_info_for (file, ptr);
356 }
4ee9c684 357
4ee9c684 358 fprintf (file, "\n");
359}
360
361
362/* Dump alias information on stderr. */
363
4b987fac 364DEBUG_FUNCTION void
4ee9c684 365debug_alias_info (void)
366{
367 dump_alias_info (stderr);
368}
369
370
786d45db 371/* Return the alias information associated with pointer T. It creates a
372 new instance if none existed. */
373
3276c83b 374struct ptr_info_def *
786d45db 375get_ptr_info (tree t)
376{
377 struct ptr_info_def *pi;
378
8c0963c4 379 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
786d45db 380
381 pi = SSA_NAME_PTR_INFO (t);
382 if (pi == NULL)
383 {
ba72912a 384 pi = ggc_alloc_cleared_ptr_info_def ();
dd277d48 385 pt_solution_reset (&pi->pt);
786d45db 386 SSA_NAME_PTR_INFO (t) = pi;
387 }
388
389 return pi;
390}
391
7f81b5ee 392/* Dump the points-to set *PT into FILE. */
4ee9c684 393
d793732c 394void
7f81b5ee 395dump_points_to_solution (FILE *file, struct pt_solution *pt)
4ee9c684 396{
7f81b5ee 397 if (pt->anything)
398 fprintf (file, ", points-to anything");
4ee9c684 399
7f81b5ee 400 if (pt->nonlocal)
401 fprintf (file, ", points-to non-local");
4ee9c684 402
7f81b5ee 403 if (pt->escaped)
404 fprintf (file, ", points-to escaped");
405
1a981e1a 406 if (pt->ipa_escaped)
407 fprintf (file, ", points-to unit escaped");
408
7f81b5ee 409 if (pt->null)
410 fprintf (file, ", points-to NULL");
411
412 if (pt->vars)
4ee9c684 413 {
7f81b5ee 414 fprintf (file, ", points-to vars: ");
415 dump_decl_set (file, pt->vars);
416 if (pt->vars_contains_global)
417 fprintf (file, " (includes global vars)");
1a981e1a 418 if (pt->vars_contains_restrict)
419 fprintf (file, " (includes restrict tags)");
7f81b5ee 420 }
421}
4ee9c684 422
7f81b5ee 423/* Dump points-to information for SSA_NAME PTR into FILE. */
4ee9c684 424
7f81b5ee 425void
426dump_points_to_info_for (FILE *file, tree ptr)
427{
428 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
4ee9c684 429
7f81b5ee 430 print_generic_expr (file, ptr, dump_flags);
1e1a4c8c 431
7f81b5ee 432 if (pi)
433 dump_points_to_solution (file, &pi->pt);
434 else
435 fprintf (file, ", points-to anything");
4ee9c684 436
437 fprintf (file, "\n");
438}
439
440
d793732c 441/* Dump points-to information for VAR into stderr. */
442
4b987fac 443DEBUG_FUNCTION void
d793732c 444debug_points_to_info_for (tree var)
445{
446 dump_points_to_info_for (stderr, var);
447}
448
3918bd18 449
450/* Initializes the alias-oracle reference representation *R from REF. */
451
452void
453ao_ref_init (ao_ref *r, tree ref)
454{
455 r->ref = ref;
456 r->base = NULL_TREE;
457 r->offset = 0;
458 r->size = -1;
459 r->max_size = -1;
460 r->ref_alias_set = -1;
461 r->base_alias_set = -1;
462}
463
464/* Returns the base object of the memory reference *REF. */
465
466tree
467ao_ref_base (ao_ref *ref)
468{
469 if (ref->base)
470 return ref->base;
471 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
472 &ref->max_size);
473 return ref->base;
474}
475
476/* Returns the base object alias set of the memory reference *REF. */
477
182cf5a9 478static alias_set_type
3918bd18 479ao_ref_base_alias_set (ao_ref *ref)
480{
182cf5a9 481 tree base_ref;
3918bd18 482 if (ref->base_alias_set != -1)
483 return ref->base_alias_set;
182cf5a9 484 if (!ref->ref)
485 return 0;
486 base_ref = ref->ref;
487 while (handled_component_p (base_ref))
488 base_ref = TREE_OPERAND (base_ref, 0);
489 ref->base_alias_set = get_alias_set (base_ref);
3918bd18 490 return ref->base_alias_set;
491}
492
493/* Returns the reference alias set of the memory reference *REF. */
494
495alias_set_type
496ao_ref_alias_set (ao_ref *ref)
497{
498 if (ref->ref_alias_set != -1)
499 return ref->ref_alias_set;
500 ref->ref_alias_set = get_alias_set (ref->ref);
501 return ref->ref_alias_set;
502}
503
134899ac 504/* Init an alias-oracle reference representation from a gimple pointer
505 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
506 size is assumed to be unknown. The access is assumed to be only
507 to or after of the pointer target, not before it. */
508
509void
510ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
511{
512 HOST_WIDE_INT t1, t2;
513 ref->ref = NULL_TREE;
514 if (TREE_CODE (ptr) == ADDR_EXPR)
515 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
516 &ref->offset, &t1, &t2);
517 else
518 {
182cf5a9 519 ref->base = build2 (MEM_REF, char_type_node,
520 ptr, build_int_cst (ptr_type_node, 0));
134899ac 521 ref->offset = 0;
522 }
523 if (size
524 && host_integerp (size, 0)
525 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
526 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
527 else
528 ref->max_size = ref->size = -1;
529 ref->ref_alias_set = 0;
530 ref->base_alias_set = 0;
531}
532
dd277d48 533/* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
534 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
535 decide. */
d793732c 536
dd277d48 537static inline int
538same_type_for_tbaa (tree type1, tree type2)
539{
540 type1 = TYPE_MAIN_VARIANT (type1);
541 type2 = TYPE_MAIN_VARIANT (type2);
4ee9c684 542
dd277d48 543 /* If we would have to do structural comparison bail out. */
544 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
545 || TYPE_STRUCTURAL_EQUALITY_P (type2))
546 return -1;
547
548 /* Compare the canonical types. */
549 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
550 return 1;
551
4b4d1513 552 /* ??? Array types are not properly unified in all cases as we have
dd277d48 553 spurious changes in the index types for example. Removing this
554 causes all sorts of problems with the Fortran frontend. */
555 if (TREE_CODE (type1) == ARRAY_TYPE
556 && TREE_CODE (type2) == ARRAY_TYPE)
557 return -1;
558
102bfc9c 559 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
560 object of one of its constrained subtypes, e.g. when a function with an
561 unconstrained parameter passed by reference is called on an object and
562 inlined. But, even in the case of a fixed size, type and subtypes are
563 not equivalent enough as to share the same TYPE_CANONICAL, since this
564 would mean that conversions between them are useless, whereas they are
565 not (e.g. type and subtypes can have different modes). So, in the end,
566 they are only guaranteed to have the same alias set. */
567 if (get_alias_set (type1) == get_alias_set (type2))
4b4d1513 568 return -1;
569
dd277d48 570 /* The types are known to be not equal. */
571 return 0;
572}
573
574/* Determine if the two component references REF1 and REF2 which are
575 based on access types TYPE1 and TYPE2 and of which at least one is based
ac30e3b2 576 on an indirect reference may alias. REF2 is the only one that can
577 be a decl in which case REF2_IS_DECL is true.
578 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
579 are the respective alias sets. */
dd277d48 580
581static bool
a42ce2cc 582aliasing_component_refs_p (tree ref1, tree type1,
ac30e3b2 583 alias_set_type ref1_alias_set,
584 alias_set_type base1_alias_set,
a42ce2cc 585 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
586 tree ref2, tree type2,
ac30e3b2 587 alias_set_type ref2_alias_set,
588 alias_set_type base2_alias_set,
589 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
590 bool ref2_is_decl)
dd277d48 591{
592 /* If one reference is a component references through pointers try to find a
593 common base and apply offset based disambiguation. This handles
594 for example
595 struct A { int i; int j; } *q;
596 struct B { struct A a; int k; } *p;
597 disambiguating q->i and p->a.j. */
598 tree *refp;
599 int same_p;
600
601 /* Now search for the type1 in the access path of ref2. This
602 would be a common base for doing offset based disambiguation on. */
bc3c318e 603 refp = &ref2;
dd277d48 604 while (handled_component_p (*refp)
605 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
606 refp = &TREE_OPERAND (*refp, 0);
607 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
608 /* If we couldn't compare types we have to bail out. */
609 if (same_p == -1)
610 return true;
611 else if (same_p == 1)
612 {
613 HOST_WIDE_INT offadj, sztmp, msztmp;
614 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
615 offset2 -= offadj;
616 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
617 }
618 /* If we didn't find a common base, try the other way around. */
bc3c318e 619 refp = &ref1;
dd277d48 620 while (handled_component_p (*refp)
621 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
622 refp = &TREE_OPERAND (*refp, 0);
623 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
624 /* If we couldn't compare types we have to bail out. */
625 if (same_p == -1)
626 return true;
627 else if (same_p == 1)
628 {
629 HOST_WIDE_INT offadj, sztmp, msztmp;
630 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
631 offset1 -= offadj;
632 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
633 }
ac30e3b2 634
4b4d1513 635 /* If we have two type access paths B1.path1 and B2.path2 they may
ac30e3b2 636 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
637 But we can still have a path that goes B1.path1...B2.path2 with
638 a part that we do not see. So we can only disambiguate now
639 if there is no B2 in the tail of path1 and no B1 on the
640 tail of path2. */
641 if (base1_alias_set == ref2_alias_set
642 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
643 return true;
644 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
645 if (!ref2_is_decl)
646 return (base2_alias_set == ref1_alias_set
647 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
4b4d1513 648 return false;
dd277d48 649}
650
651/* Return true if two memory references based on the variables BASE1
077ad5ad 652 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
653 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
dd277d48 654
655static bool
656decl_refs_may_alias_p (tree base1,
657 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
658 tree base2,
659 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
4ee9c684 660{
dd277d48 661 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
4ee9c684 662
dd277d48 663 /* If both references are based on different variables, they cannot alias. */
7f2d9047 664 if (base1 != base2)
dd277d48 665 return false;
4ee9c684 666
dd277d48 667 /* If both references are based on the same variable, they cannot alias if
668 the accesses do not overlap. */
669 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
670}
671
672/* Return true if an indirect reference based on *PTR1 constrained
077ad5ad 673 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
674 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
dd277d48 675 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
676 in which case they are computed on-demand. REF1 and REF2
677 if non-NULL are the complete memory reference trees. */
678
679static bool
182cf5a9 680indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
681 HOST_WIDE_INT offset1,
682 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
ac30e3b2 683 alias_set_type ref1_alias_set,
dd277d48 684 alias_set_type base1_alias_set,
182cf5a9 685 tree ref2 ATTRIBUTE_UNUSED, tree base2,
dd277d48 686 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
ac30e3b2 687 alias_set_type ref2_alias_set,
182cf5a9 688 alias_set_type base2_alias_set, bool tbaa_p)
dd277d48 689{
182cf5a9 690 tree ptr1 = TREE_OPERAND (base1, 0);
691 tree ptrtype1;
7749ee1c 692 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
182cf5a9 693
7749ee1c 694 /* The offset embedded in MEM_REFs can be negative. Bias them
695 so that the resulting offset adjustment is positive. */
182cf5a9 696 if (TREE_CODE (base1) == MEM_REF)
7749ee1c 697 {
698 double_int moff = mem_ref_offset (base1);
699 moff = double_int_lshift (moff,
700 BITS_PER_UNIT == 8
701 ? 3 : exact_log2 (BITS_PER_UNIT),
702 HOST_BITS_PER_DOUBLE_INT, true);
703 if (double_int_negative_p (moff))
704 offset2p += double_int_neg (moff).low;
705 else
706 offset1p += moff.low;
707 }
182cf5a9 708
dd277d48 709 /* If only one reference is based on a variable, they cannot alias if
710 the pointer access is beyond the extent of the variable access.
711 (the pointer base cannot validly point to an offset less than zero
712 of the variable).
713 They also cannot alias if the pointer may not point to the decl. */
7749ee1c 714 if (!ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
dd277d48 715 return false;
716 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
717 return false;
718
719 /* Disambiguations that rely on strict aliasing rules follow. */
182cf5a9 720 if (!flag_strict_aliasing || !tbaa_p)
dd277d48 721 return true;
722
182cf5a9 723 if (TREE_CODE (base1) == MEM_REF)
724 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
725 else
726 ptrtype1 = TREE_TYPE (ptr1);
727
dd277d48 728 /* If the alias set for a pointer access is zero all bets are off. */
729 if (base1_alias_set == -1)
182cf5a9 730 base1_alias_set = get_deref_alias_set (ptrtype1);
dd277d48 731 if (base1_alias_set == 0)
732 return true;
733 if (base2_alias_set == -1)
734 base2_alias_set = get_alias_set (base2);
735
736 /* If both references are through the same type, they do not alias
737 if the accesses do not overlap. This does extra disambiguation
182cf5a9 738 for mixed/pointer accesses but requires strict aliasing.
739 For MEM_REFs we require that the component-ref offset we computed
740 is relative to the start of the type which we ensure by
741 comparing rvalue and access type and disregarding the constant
742 pointer offset. */
743 if ((TREE_CODE (base1) != MEM_REF
744 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
745 && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
dd277d48 746 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
747
182cf5a9 748 /* When we are trying to disambiguate an access with a pointer dereference
749 as base versus one with a decl as base we can use both the size
750 of the decl and its dynamic type for extra disambiguation.
751 ??? We do not know anything about the dynamic type of the decl
752 other than that its alias-set contains base2_alias_set as a subset
753 which does not help us here. */
754 /* As we know nothing useful about the dynamic type of the decl just
755 use the usual conflict check rather than a subset test.
756 ??? We could introduce -fvery-strict-aliasing when the language
757 does not allow decls to have a dynamic type that differs from their
758 static type. Then we can check
759 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
dd277d48 760 if (base1_alias_set != base2_alias_set
182cf5a9 761 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
762 return false;
763 /* If the size of the access relevant for TBAA through the pointer
764 is bigger than the size of the decl we can't possibly access the
765 decl via that pointer. */
766 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
767 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
768 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
769 /* ??? This in turn may run afoul when a decl of type T which is
770 a member of union type U is accessed through a pointer to
771 type U and sizeof T is smaller than sizeof U. */
772 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
773 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
774 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
dd277d48 775 return false;
776
777 /* Do access-path based disambiguation. */
778 if (ref1 && ref2
779 && handled_component_p (ref1)
182cf5a9 780 && handled_component_p (ref2)
781 && (TREE_CODE (base1) != MEM_REF
782 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
783 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
ac30e3b2 784 ref1_alias_set, base1_alias_set,
a42ce2cc 785 offset1, max_size1,
786 ref2, TREE_TYPE (base2),
ac30e3b2 787 ref2_alias_set, base2_alias_set,
788 offset2, max_size2, true);
dd277d48 789
790 return true;
791}
792
793/* Return true if two indirect references based on *PTR1
077ad5ad 794 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
795 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
dd277d48 796 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
797 in which case they are computed on-demand. REF1 and REF2
798 if non-NULL are the complete memory reference trees. */
799
800static bool
182cf5a9 801indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
dd277d48 802 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
ac30e3b2 803 alias_set_type ref1_alias_set,
dd277d48 804 alias_set_type base1_alias_set,
182cf5a9 805 tree ref2 ATTRIBUTE_UNUSED, tree base2,
dd277d48 806 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
ac30e3b2 807 alias_set_type ref2_alias_set,
182cf5a9 808 alias_set_type base2_alias_set, bool tbaa_p)
dd277d48 809{
182cf5a9 810 tree ptr1 = TREE_OPERAND (base1, 0);
811 tree ptr2 = TREE_OPERAND (base2, 0);
812 tree ptrtype1, ptrtype2;
813
dd277d48 814 /* If both bases are based on pointers they cannot alias if they may not
815 point to the same memory object or if they point to the same object
816 and the accesses do not overlap. */
b5c3c805 817 if ((!cfun || gimple_in_ssa_p (cfun))
818 && operand_equal_p (ptr1, ptr2, 0))
182cf5a9 819 {
7749ee1c 820 /* The offset embedded in MEM_REFs can be negative. Bias them
821 so that the resulting offset adjustment is positive. */
182cf5a9 822 if (TREE_CODE (base1) == MEM_REF)
7749ee1c 823 {
824 double_int moff = mem_ref_offset (base1);
825 moff = double_int_lshift (moff,
826 BITS_PER_UNIT == 8
827 ? 3 : exact_log2 (BITS_PER_UNIT),
828 HOST_BITS_PER_DOUBLE_INT, true);
829 if (double_int_negative_p (moff))
830 offset2 += double_int_neg (moff).low;
831 else
832 offset1 += moff.low;
833 }
182cf5a9 834 if (TREE_CODE (base2) == MEM_REF)
7749ee1c 835 {
836 double_int moff = mem_ref_offset (base2);
837 moff = double_int_lshift (moff,
838 BITS_PER_UNIT == 8
839 ? 3 : exact_log2 (BITS_PER_UNIT),
840 HOST_BITS_PER_DOUBLE_INT, true);
841 if (double_int_negative_p (moff))
842 offset1 += double_int_neg (moff).low;
843 else
844 offset2 += moff.low;
845 }
182cf5a9 846 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
847 }
dd277d48 848 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
849 return false;
850
851 /* Disambiguations that rely on strict aliasing rules follow. */
182cf5a9 852 if (!flag_strict_aliasing || !tbaa_p)
dd277d48 853 return true;
854
182cf5a9 855 if (TREE_CODE (base1) == MEM_REF)
856 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
857 else
858 ptrtype1 = TREE_TYPE (ptr1);
859 if (TREE_CODE (base2) == MEM_REF)
860 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
861 else
862 ptrtype2 = TREE_TYPE (ptr2);
863
dd277d48 864 /* If the alias set for a pointer access is zero all bets are off. */
865 if (base1_alias_set == -1)
182cf5a9 866 base1_alias_set = get_deref_alias_set (ptrtype1);
dd277d48 867 if (base1_alias_set == 0)
868 return true;
869 if (base2_alias_set == -1)
182cf5a9 870 base2_alias_set = get_deref_alias_set (ptrtype2);
dd277d48 871 if (base2_alias_set == 0)
872 return true;
873
874 /* If both references are through the same type, they do not alias
875 if the accesses do not overlap. This does extra disambiguation
876 for mixed/pointer accesses but requires strict aliasing. */
182cf5a9 877 if ((TREE_CODE (base1) != MEM_REF
878 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
879 && (TREE_CODE (base2) != MEM_REF
880 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
881 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
882 TREE_TYPE (ptrtype2)) == 1)
dd277d48 883 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
884
885 /* Do type-based disambiguation. */
886 if (base1_alias_set != base2_alias_set
887 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
888 return false;
889
890 /* Do access-path based disambiguation. */
891 if (ref1 && ref2
892 && handled_component_p (ref1)
182cf5a9 893 && handled_component_p (ref2)
894 && (TREE_CODE (base1) != MEM_REF
895 || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
896 && (TREE_CODE (base2) != MEM_REF
897 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
898 return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
ac30e3b2 899 ref1_alias_set, base1_alias_set,
a42ce2cc 900 offset1, max_size1,
182cf5a9 901 ref2, TREE_TYPE (ptrtype2),
ac30e3b2 902 ref2_alias_set, base2_alias_set,
903 offset2, max_size2, false);
dd277d48 904
905 return true;
906}
907
908/* Return true, if the two memory references REF1 and REF2 may alias. */
909
3a443843 910bool
3918bd18 911refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
dd277d48 912{
913 tree base1, base2;
914 HOST_WIDE_INT offset1 = 0, offset2 = 0;
dd277d48 915 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
916 bool var1_p, var2_p, ind1_p, ind2_p;
917
c78d5cec 918 gcc_checking_assert ((!ref1->ref
8e7c2b9c 919 || TREE_CODE (ref1->ref) == SSA_NAME
c78d5cec 920 || DECL_P (ref1->ref)
921 || handled_component_p (ref1->ref)
922 || INDIRECT_REF_P (ref1->ref)
182cf5a9 923 || TREE_CODE (ref1->ref) == MEM_REF
c78d5cec 924 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
925 && (!ref2->ref
8e7c2b9c 926 || TREE_CODE (ref2->ref) == SSA_NAME
c78d5cec 927 || DECL_P (ref2->ref)
928 || handled_component_p (ref2->ref)
929 || INDIRECT_REF_P (ref2->ref)
182cf5a9 930 || TREE_CODE (ref2->ref) == MEM_REF
c78d5cec 931 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
dd277d48 932
dd277d48 933 /* Decompose the references into their base objects and the access. */
3918bd18 934 base1 = ao_ref_base (ref1);
935 offset1 = ref1->offset;
3918bd18 936 max_size1 = ref1->max_size;
937 base2 = ao_ref_base (ref2);
938 offset2 = ref2->offset;
3918bd18 939 max_size2 = ref2->max_size;
dd277d48 940
941 /* We can end up with registers or constants as bases for example from
942 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
943 which is seen as a struct copy. */
944 if (TREE_CODE (base1) == SSA_NAME
dd277d48 945 || TREE_CODE (base2) == SSA_NAME
119147d1 946 || TREE_CODE (base1) == CONST_DECL
947 || TREE_CODE (base2) == CONST_DECL
9b973aa6 948 || is_gimple_min_invariant (base1)
f85fb819 949 || is_gimple_min_invariant (base2))
dd277d48 950 return false;
951
c78d5cec 952 /* We can end up refering to code via function and label decls.
953 As we likely do not properly track code aliases conservatively
954 bail out. */
ceefbccc 955 if (TREE_CODE (base1) == FUNCTION_DECL
c78d5cec 956 || TREE_CODE (base2) == FUNCTION_DECL
957 || TREE_CODE (base1) == LABEL_DECL
958 || TREE_CODE (base2) == LABEL_DECL)
ceefbccc 959 return true;
960
090a8c65 961 /* Defer to simple offset based disambiguation if we have
962 references based on two decls. Do this before defering to
963 TBAA to handle must-alias cases in conformance with the
964 GCC extension of allowing type-punning through unions. */
dd277d48 965 var1_p = SSA_VAR_P (base1);
966 var2_p = SSA_VAR_P (base2);
dd277d48 967 if (var1_p && var2_p)
968 return decl_refs_may_alias_p (base1, offset1, max_size1,
969 base2, offset2, max_size2);
090a8c65 970
182cf5a9 971 ind1_p = INDIRECT_REF_P (base1) || (TREE_CODE (base1) == MEM_REF);
972 ind2_p = INDIRECT_REF_P (base2) || (TREE_CODE (base2) == MEM_REF);
973
67817f0f 974 /* Canonicalize the pointer-vs-decl case. */
975 if (ind1_p && var2_p)
976 {
977 HOST_WIDE_INT tmp1;
978 tree tmp2;
979 ao_ref *tmp3;
980 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
981 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
982 tmp2 = base1; base1 = base2; base2 = tmp2;
983 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
984 var1_p = true;
985 ind1_p = false;
986 var2_p = false;
987 ind2_p = true;
988 }
989
090a8c65 990 /* First defer to TBAA if possible. */
2a3ebafa 991 if (tbaa_p
992 && flag_strict_aliasing
3918bd18 993 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
994 ao_ref_alias_set (ref2)))
090a8c65 995 return false;
996
997 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
998 TBAA disambiguation based on the access type is possible, so bail
999 out only after that check. */
3918bd18 1000 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1001 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
090a8c65 1002 return true;
1003
1004 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
090a8c65 1005 if (var1_p && ind2_p)
182cf5a9 1006 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
ac30e3b2 1007 offset2, max_size2,
182cf5a9 1008 ao_ref_alias_set (ref2), -1,
3918bd18 1009 ref1->ref, base1,
ac30e3b2 1010 offset1, max_size1,
182cf5a9 1011 ao_ref_alias_set (ref1),
1012 ao_ref_base_alias_set (ref1),
1013 tbaa_p);
dd277d48 1014 else if (ind1_p && ind2_p)
182cf5a9 1015 return indirect_refs_may_alias_p (ref1->ref, base1,
ac30e3b2 1016 offset1, max_size1,
182cf5a9 1017 ao_ref_alias_set (ref1), -1,
1018 ref2->ref, base2,
ac30e3b2 1019 offset2, max_size2,
182cf5a9 1020 ao_ref_alias_set (ref2), -1,
1021 tbaa_p);
dd277d48 1022
1023 gcc_unreachable ();
1024}
1025
1026bool
1027refs_may_alias_p (tree ref1, tree ref2)
1028{
3918bd18 1029 ao_ref r1, r2;
1030 bool res;
1031 ao_ref_init (&r1, ref1);
1032 ao_ref_init (&r2, ref2);
1033 res = refs_may_alias_p_1 (&r1, &r2, true);
dd277d48 1034 if (res)
1035 ++alias_stats.refs_may_alias_p_may_alias;
1036 else
1037 ++alias_stats.refs_may_alias_p_no_alias;
1038 return res;
1039}
1040
2a3ebafa 1041/* Returns true if there is a anti-dependence for the STORE that
1042 executes after the LOAD. */
1043
1044bool
1045refs_anti_dependent_p (tree load, tree store)
1046{
3918bd18 1047 ao_ref r1, r2;
1048 ao_ref_init (&r1, load);
1049 ao_ref_init (&r2, store);
1050 return refs_may_alias_p_1 (&r1, &r2, false);
2a3ebafa 1051}
1052
1053/* Returns true if there is a output dependence for the stores
1054 STORE1 and STORE2. */
1055
1056bool
1057refs_output_dependent_p (tree store1, tree store2)
1058{
3918bd18 1059 ao_ref r1, r2;
1060 ao_ref_init (&r1, store1);
1061 ao_ref_init (&r2, store2);
1062 return refs_may_alias_p_1 (&r1, &r2, false);
2a3ebafa 1063}
dd277d48 1064
1065/* If the call CALL may use the memory reference REF return true,
1066 otherwise return false. */
1067
1068static bool
134899ac 1069ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
dd277d48 1070{
047fdd47 1071 tree base, callee;
dd277d48 1072 unsigned i;
1073 int flags = gimple_call_flags (call);
1074
1075 /* Const functions without a static chain do not implicitly use memory. */
1076 if (!gimple_call_chain (call)
1077 && (flags & (ECF_CONST|ECF_NOVOPS)))
1078 goto process_args;
1079
134899ac 1080 base = ao_ref_base (ref);
917f08fa 1081 if (!base)
dd277d48 1082 return true;
1083
09347e88 1084 /* If the reference is based on a decl that is not aliased the call
1085 cannot possibly use it. */
1086 if (DECL_P (base)
1087 && !may_be_aliased (base)
7f7f16d4 1088 /* But local statics can be used through recursion. */
1089 && !is_global_var (base))
09347e88 1090 goto process_args;
1091
047fdd47 1092 callee = gimple_call_fndecl (call);
1093
1094 /* Handle those builtin functions explicitly that do not act as
1095 escape points. See tree-ssa-structalias.c:find_func_aliases
1096 for the list of builtins we might need to handle here. */
1097 if (callee != NULL_TREE
1098 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1099 switch (DECL_FUNCTION_CODE (callee))
1100 {
1101 /* All the following functions clobber memory pointed to by
1102 their first argument. */
1103 case BUILT_IN_STRCPY:
1104 case BUILT_IN_STRNCPY:
047fdd47 1105 case BUILT_IN_MEMCPY:
1106 case BUILT_IN_MEMMOVE:
1107 case BUILT_IN_MEMPCPY:
1108 case BUILT_IN_STPCPY:
1109 case BUILT_IN_STPNCPY:
1110 case BUILT_IN_STRCAT:
1111 case BUILT_IN_STRNCAT:
1112 {
134899ac 1113 ao_ref dref;
1114 tree size = NULL_TREE;
1115 if (gimple_call_num_args (call) == 3)
1116 size = gimple_call_arg (call, 2);
1117 ao_ref_init_from_ptr_and_size (&dref,
1118 gimple_call_arg (call, 1),
1119 size);
1120 return refs_may_alias_p_1 (&dref, ref, false);
047fdd47 1121 }
119147d1 1122 case BUILT_IN_BCOPY:
1123 {
1124 ao_ref dref;
1125 tree size = gimple_call_arg (call, 2);
1126 ao_ref_init_from_ptr_and_size (&dref,
1127 gimple_call_arg (call, 0),
1128 size);
1129 return refs_may_alias_p_1 (&dref, ref, false);
1130 }
09d9c774 1131 /* The following builtins do not read from memory. */
1132 case BUILT_IN_FREE:
0d3ca08e 1133 case BUILT_IN_MALLOC:
4ef43bbd 1134 case BUILT_IN_CALLOC:
09d9c774 1135 case BUILT_IN_MEMSET:
1136 case BUILT_IN_FREXP:
1137 case BUILT_IN_FREXPF:
1138 case BUILT_IN_FREXPL:
1139 case BUILT_IN_GAMMA_R:
1140 case BUILT_IN_GAMMAF_R:
1141 case BUILT_IN_GAMMAL_R:
1142 case BUILT_IN_LGAMMA_R:
1143 case BUILT_IN_LGAMMAF_R:
1144 case BUILT_IN_LGAMMAL_R:
1145 case BUILT_IN_MODF:
1146 case BUILT_IN_MODFF:
1147 case BUILT_IN_MODFL:
1148 case BUILT_IN_REMQUO:
1149 case BUILT_IN_REMQUOF:
1150 case BUILT_IN_REMQUOL:
1151 case BUILT_IN_SINCOS:
1152 case BUILT_IN_SINCOSF:
1153 case BUILT_IN_SINCOSL:
1154 return false;
1155
047fdd47 1156 default:
1157 /* Fallthru to general call handling. */;
1158 }
1159
dd277d48 1160 /* Check if base is a global static variable that is not read
1161 by the function. */
1162 if (TREE_CODE (base) == VAR_DECL
d97be713 1163 && TREE_STATIC (base))
4ee9c684 1164 {
dd277d48 1165 bitmap not_read;
1166
1167 if (callee != NULL_TREE
1168 && (not_read
1169 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1170 && bitmap_bit_p (not_read, DECL_UID (base)))
1171 goto process_args;
4ee9c684 1172 }
1173
cb245216 1174 /* Check if the base variable is call-used. */
1175 if (DECL_P (base))
4ee9c684 1176 {
cb245216 1177 if (pt_solution_includes (gimple_call_use_set (call), base))
dd277d48 1178 return true;
1179 }
182cf5a9 1180 else if ((INDIRECT_REF_P (base)
1181 || TREE_CODE (base) == MEM_REF)
cb245216 1182 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
dd277d48 1183 {
cb245216 1184 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1185 if (!pi)
1186 return true;
917f08fa 1187
cb245216 1188 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
dd277d48 1189 return true;
1190 }
cb245216 1191 else
1192 return true;
dd277d48 1193
1194 /* Inspect call arguments for passed-by-value aliases. */
1195process_args:
1196 for (i = 0; i < gimple_call_num_args (call); ++i)
1197 {
1198 tree op = gimple_call_arg (call, i);
8ce86007 1199 int flags = gimple_call_arg_flags (call, i);
1200
1201 if (flags & EAF_UNUSED)
1202 continue;
dd277d48 1203
dd277d48 1204 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1205 op = TREE_OPERAND (op, 0);
4ee9c684 1206
dd277d48 1207 if (TREE_CODE (op) != SSA_NAME
134899ac 1208 && !is_gimple_min_invariant (op))
1209 {
1210 ao_ref r;
1211 ao_ref_init (&r, op);
1212 if (refs_may_alias_p_1 (&r, ref, true))
1213 return true;
1214 }
4ee9c684 1215 }
1216
dd277d48 1217 return false;
1218}
1219
1220static bool
1221ref_maybe_used_by_call_p (gimple call, tree ref)
1222{
134899ac 1223 ao_ref r;
1224 bool res;
1225 ao_ref_init (&r, ref);
1226 res = ref_maybe_used_by_call_p_1 (call, &r);
dd277d48 1227 if (res)
1228 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1229 else
1230 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1231 return res;
4ee9c684 1232}
1233
1234
dd277d48 1235/* If the statement STMT may use the memory reference REF return
1236 true, otherwise return false. */
4ee9c684 1237
dd277d48 1238bool
1239ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
4ee9c684 1240{
dd277d48 1241 if (is_gimple_assign (stmt))
1242 {
1243 tree rhs;
1244
1245 /* All memory assign statements are single. */
1246 if (!gimple_assign_single_p (stmt))
1247 return false;
1248
1249 rhs = gimple_assign_rhs1 (stmt);
1250 if (is_gimple_reg (rhs)
1251 || is_gimple_min_invariant (rhs)
1252 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1253 return false;
1254
1255 return refs_may_alias_p (rhs, ref);
1256 }
1257 else if (is_gimple_call (stmt))
1258 return ref_maybe_used_by_call_p (stmt, ref);
1259
1260 return true;
4ee9c684 1261}
1262
dd277d48 1263/* If the call in statement CALL may clobber the memory reference REF
1264 return true, otherwise return false. */
4ee9c684 1265
dd277d48 1266static bool
3918bd18 1267call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
4ee9c684 1268{
7f7f16d4 1269 tree base;
047fdd47 1270 tree callee;
dd277d48 1271
1272 /* If the call is pure or const it cannot clobber anything. */
1273 if (gimple_call_flags (call)
1274 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1275 return false;
1276
3918bd18 1277 base = ao_ref_base (ref);
dd277d48 1278 if (!base)
1279 return true;
1280
1281 if (TREE_CODE (base) == SSA_NAME
1282 || CONSTANT_CLASS_P (base))
1283 return false;
1284
09347e88 1285 /* If the reference is based on a decl that is not aliased the call
1286 cannot possibly clobber it. */
1287 if (DECL_P (base)
1288 && !may_be_aliased (base)
7f7f16d4 1289 /* But local non-readonly statics can be modified through recursion
1290 or the call may implement a threading barrier which we must
1291 treat as may-def. */
09347e88 1292 && (TREE_READONLY (base)
7f7f16d4 1293 || !is_global_var (base)))
09347e88 1294 return false;
1295
047fdd47 1296 callee = gimple_call_fndecl (call);
1297
1298 /* Handle those builtin functions explicitly that do not act as
1299 escape points. See tree-ssa-structalias.c:find_func_aliases
1300 for the list of builtins we might need to handle here. */
1301 if (callee != NULL_TREE
1302 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1303 switch (DECL_FUNCTION_CODE (callee))
1304 {
1305 /* All the following functions clobber memory pointed to by
1306 their first argument. */
1307 case BUILT_IN_STRCPY:
1308 case BUILT_IN_STRNCPY:
047fdd47 1309 case BUILT_IN_MEMCPY:
1310 case BUILT_IN_MEMMOVE:
1311 case BUILT_IN_MEMPCPY:
1312 case BUILT_IN_STPCPY:
1313 case BUILT_IN_STPNCPY:
1314 case BUILT_IN_STRCAT:
1315 case BUILT_IN_STRNCAT:
134899ac 1316 case BUILT_IN_MEMSET:
047fdd47 1317 {
134899ac 1318 ao_ref dref;
1319 tree size = NULL_TREE;
1320 if (gimple_call_num_args (call) == 3)
1321 size = gimple_call_arg (call, 2);
1322 ao_ref_init_from_ptr_and_size (&dref,
1323 gimple_call_arg (call, 0),
1324 size);
1325 return refs_may_alias_p_1 (&dref, ref, false);
047fdd47 1326 }
119147d1 1327 case BUILT_IN_BCOPY:
1328 {
1329 ao_ref dref;
1330 tree size = gimple_call_arg (call, 2);
1331 ao_ref_init_from_ptr_and_size (&dref,
1332 gimple_call_arg (call, 1),
1333 size);
1334 return refs_may_alias_p_1 (&dref, ref, false);
1335 }
0d3ca08e 1336 /* Allocating memory does not have any side-effects apart from
1337 being the definition point for the pointer. */
1338 case BUILT_IN_MALLOC:
4ef43bbd 1339 case BUILT_IN_CALLOC:
1340 /* Unix98 specifies that errno is set on allocation failure.
1341 Until we properly can track the errno location assume it
a29c5f9c 1342 is not a local decl but external or anonymous storage in
1343 a different translation unit. Also assume it is of
1344 type int as required by the standard. */
1345 if (flag_errno_math
1346 && TREE_TYPE (base) == integer_type_node)
4ef43bbd 1347 {
1348 struct ptr_info_def *pi;
a29c5f9c 1349 if (DECL_P (base)
1350 && !TREE_STATIC (base))
1351 return true;
182cf5a9 1352 else if ((INDIRECT_REF_P (base)
1353 || TREE_CODE (base) == MEM_REF)
a29c5f9c 1354 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1355 && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
4ef43bbd 1356 return pi->pt.anything || pi->pt.nonlocal;
1357 }
0d3ca08e 1358 return false;
047fdd47 1359 /* Freeing memory kills the pointed-to memory. More importantly
1360 the call has to serve as a barrier for moving loads and stores
134899ac 1361 across it. */
047fdd47 1362 case BUILT_IN_FREE:
047fdd47 1363 {
1364 tree ptr = gimple_call_arg (call, 0);
1365 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1366 }
047fdd47 1367 case BUILT_IN_GAMMA_R:
1368 case BUILT_IN_GAMMAF_R:
1369 case BUILT_IN_GAMMAL_R:
1370 case BUILT_IN_LGAMMA_R:
1371 case BUILT_IN_LGAMMAF_R:
1372 case BUILT_IN_LGAMMAL_R:
09d9c774 1373 {
1374 tree out = gimple_call_arg (call, 1);
1375 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1376 return true;
1377 if (flag_errno_math)
1378 break;
1379 return false;
1380 }
1381 case BUILT_IN_FREXP:
1382 case BUILT_IN_FREXPF:
1383 case BUILT_IN_FREXPL:
047fdd47 1384 case BUILT_IN_MODF:
1385 case BUILT_IN_MODFF:
1386 case BUILT_IN_MODFL:
1387 {
1388 tree out = gimple_call_arg (call, 1);
1389 return ptr_deref_may_alias_ref_p_1 (out, ref);
1390 }
1391 case BUILT_IN_REMQUO:
1392 case BUILT_IN_REMQUOF:
1393 case BUILT_IN_REMQUOL:
1394 {
1395 tree out = gimple_call_arg (call, 2);
09d9c774 1396 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1397 return true;
1398 if (flag_errno_math)
1399 break;
1400 return false;
047fdd47 1401 }
1402 case BUILT_IN_SINCOS:
1403 case BUILT_IN_SINCOSF:
1404 case BUILT_IN_SINCOSL:
1405 {
1406 tree sin = gimple_call_arg (call, 1);
1407 tree cos = gimple_call_arg (call, 2);
1408 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1409 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1410 }
1411 default:
1412 /* Fallthru to general call handling. */;
1413 }
1414
dd277d48 1415 /* Check if base is a global static variable that is not written
1416 by the function. */
047fdd47 1417 if (callee != NULL_TREE
1418 && TREE_CODE (base) == VAR_DECL
d97be713 1419 && TREE_STATIC (base))
4ee9c684 1420 {
dd277d48 1421 bitmap not_written;
0eaf9bd7 1422
047fdd47 1423 if ((not_written
1424 = ipa_reference_get_not_written_global (cgraph_node (callee)))
dd277d48 1425 && bitmap_bit_p (not_written, DECL_UID (base)))
1426 return false;
4ee9c684 1427 }
4ee9c684 1428
cb245216 1429 /* Check if the base variable is call-clobbered. */
dd277d48 1430 if (DECL_P (base))
cb245216 1431 return pt_solution_includes (gimple_call_clobber_set (call), base);
182cf5a9 1432 else if ((INDIRECT_REF_P (base)
1433 || TREE_CODE (base) == MEM_REF)
917f08fa 1434 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1435 {
1436 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1437 if (!pi)
1438 return true;
1439
cb245216 1440 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
917f08fa 1441 }
4ee9c684 1442
dd277d48 1443 return true;
1444}
4ee9c684 1445
4debfcfc 1446/* If the call in statement CALL may clobber the memory reference REF
1447 return true, otherwise return false. */
1448
1449bool
dd277d48 1450call_may_clobber_ref_p (gimple call, tree ref)
4ee9c684 1451{
3918bd18 1452 bool res;
1453 ao_ref r;
1454 ao_ref_init (&r, ref);
1455 res = call_may_clobber_ref_p_1 (call, &r);
dd277d48 1456 if (res)
1457 ++alias_stats.call_may_clobber_ref_p_may_alias;
1458 else
1459 ++alias_stats.call_may_clobber_ref_p_no_alias;
1460 return res;
4ee9c684 1461}
94e6573f 1462
dd277d48 1463
1464/* If the statement STMT may clobber the memory reference REF return true,
1465 otherwise return false. */
94e6573f 1466
1467bool
3918bd18 1468stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
94e6573f 1469{
dd277d48 1470 if (is_gimple_call (stmt))
1471 {
1472 tree lhs = gimple_call_lhs (stmt);
1473 if (lhs
3918bd18 1474 && !is_gimple_reg (lhs))
1475 {
1476 ao_ref r;
1477 ao_ref_init (&r, lhs);
1478 if (refs_may_alias_p_1 (ref, &r, true))
1479 return true;
1480 }
94e6573f 1481
3918bd18 1482 return call_may_clobber_ref_p_1 (stmt, ref);
dd277d48 1483 }
8e7c2b9c 1484 else if (gimple_assign_single_p (stmt))
3918bd18 1485 {
8e7c2b9c 1486 tree lhs = gimple_assign_lhs (stmt);
1487 if (!is_gimple_reg (lhs))
1488 {
1489 ao_ref r;
1490 ao_ref_init (&r, gimple_assign_lhs (stmt));
1491 return refs_may_alias_p_1 (ref, &r, true);
1492 }
3918bd18 1493 }
dd277d48 1494 else if (gimple_code (stmt) == GIMPLE_ASM)
94e6573f 1495 return true;
1496
6329636b 1497 return false;
94e6573f 1498}
2be14d8b 1499
3918bd18 1500bool
1501stmt_may_clobber_ref_p (gimple stmt, tree ref)
1502{
1503 ao_ref r;
1504 ao_ref_init (&r, ref);
1505 return stmt_may_clobber_ref_p_1 (stmt, &r);
1506}
1507
1508
dd277d48 1509/* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1510 TARGET or a statement clobbering the memory reference REF in which
1511 case false is returned. The walk starts with VUSE, one argument of PHI. */
1512
1513static bool
3918bd18 1514maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1515 tree vuse, bitmap *visited)
ac926412 1516{
dd277d48 1517 if (!*visited)
1518 *visited = BITMAP_ALLOC (NULL);
ac926412 1519
dd277d48 1520 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1521
1522 /* Walk until we hit the target. */
1523 while (vuse != target)
ac926412 1524 {
dd277d48 1525 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1526 /* Recurse for PHI nodes. */
1527 if (gimple_code (def_stmt) == GIMPLE_PHI)
1528 {
1529 /* An already visited PHI node ends the walk successfully. */
1530 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1531 return true;
1532 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1533 if (!vuse)
1534 return false;
1535 continue;
1536 }
1537 /* A clobbering statement or the end of the IL ends it failing. */
1538 else if (gimple_nop_p (def_stmt)
3918bd18 1539 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
dd277d48 1540 return false;
1541 vuse = gimple_vuse (def_stmt);
ac926412 1542 }
dd277d48 1543 return true;
1544}
ac926412 1545
dd277d48 1546/* Starting from a PHI node for the virtual operand of the memory reference
1547 REF find a continuation virtual operand that allows to continue walking
1548 statements dominating PHI skipping only statements that cannot possibly
1549 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1550 be found. */
1551
7814d418 1552tree
3918bd18 1553get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
dd277d48 1554{
1555 unsigned nargs = gimple_phi_num_args (phi);
1556
1557 /* Through a single-argument PHI we can simply look through. */
1558 if (nargs == 1)
1559 return PHI_ARG_DEF (phi, 0);
1560
1561 /* For two arguments try to skip non-aliasing code until we hit
1562 the phi argument definition that dominates the other one. */
1563 if (nargs == 2)
ac926412 1564 {
dd277d48 1565 tree arg0 = PHI_ARG_DEF (phi, 0);
1566 tree arg1 = PHI_ARG_DEF (phi, 1);
1567 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1568 gimple def1 = SSA_NAME_DEF_STMT (arg1);
7814d418 1569 tree common_vuse;
dd277d48 1570
1571 if (arg0 == arg1)
1572 return arg0;
1573 else if (gimple_nop_p (def0)
1574 || (!gimple_nop_p (def1)
1575 && dominated_by_p (CDI_DOMINATORS,
1576 gimple_bb (def1), gimple_bb (def0))))
1577 {
1578 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1579 return arg0;
1580 }
1581 else if (gimple_nop_p (def1)
1582 || dominated_by_p (CDI_DOMINATORS,
1583 gimple_bb (def0), gimple_bb (def1)))
1584 {
1585 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1586 return arg1;
1587 }
7814d418 1588 /* Special case of a diamond:
1589 MEM_1 = ...
1590 goto (cond) ? L1 : L2
1591 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1592 goto L3
1593 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1594 L3: MEM_4 = PHI<MEM_2, MEM_3>
1595 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1596 dominate each other, but still we can easily skip this PHI node
1597 if we recognize that the vuse MEM operand is the same for both,
1598 and that we can skip both statements (they don't clobber us).
1599 This is still linear. Don't use maybe_skip_until, that might
1600 potentially be slow. */
1601 else if ((common_vuse = gimple_vuse (def0))
1602 && common_vuse == gimple_vuse (def1))
1603 {
1604 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1605 && !stmt_may_clobber_ref_p_1 (def1, ref))
1606 return common_vuse;
1607 }
ac926412 1608 }
dd277d48 1609
1610 return NULL_TREE;
ac926412 1611}
516849c7 1612
dd277d48 1613/* Based on the memory reference REF and its virtual use VUSE call
1614 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1615 itself. That is, for each virtual use for which its defining statement
1616 does not clobber REF.
e683de6d 1617
dd277d48 1618 WALKER is called with REF, the current virtual use and DATA. If
1619 WALKER returns non-NULL the walk stops and its result is returned.
1620 At the end of a non-successful walk NULL is returned.
3857274c 1621
d8021dea 1622 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1623 use which definition is a statement that may clobber REF and DATA.
1624 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1625 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1626 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1627 to adjust REF and *DATA to make that valid.
1628
dd277d48 1629 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1630
1631void *
3918bd18 1632walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1633 void *(*walker)(ao_ref *, tree, void *),
1634 void *(*translate)(ao_ref *, tree, void *), void *data)
3857274c 1635{
dd277d48 1636 bitmap visited = NULL;
1637 void *res;
1638
02c74b9b 1639 timevar_push (TV_ALIAS_STMT_WALK);
1640
dd277d48 1641 do
1642 {
1643 gimple def_stmt;
3857274c 1644
02c74b9b 1645 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
dd277d48 1646 res = (*walker) (ref, vuse, data);
1647 if (res)
1648 break;
3857274c 1649
dd277d48 1650 def_stmt = SSA_NAME_DEF_STMT (vuse);
1651 if (gimple_nop_p (def_stmt))
1652 break;
1653 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1654 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1655 else
1656 {
3918bd18 1657 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
d8021dea 1658 {
1659 if (!translate)
1660 break;
3918bd18 1661 res = (*translate) (ref, vuse, data);
d8021dea 1662 /* Failed lookup and translation. */
1663 if (res == (void *)-1)
1664 {
1665 res = NULL;
1666 break;
1667 }
1668 /* Lookup succeeded. */
1669 else if (res != NULL)
1670 break;
1671 /* Translation succeeded, continue walking. */
1672 }
dd277d48 1673 vuse = gimple_vuse (def_stmt);
1674 }
1675 }
1676 while (vuse);
ac926412 1677
dd277d48 1678 if (visited)
1679 BITMAP_FREE (visited);
ac926412 1680
02c74b9b 1681 timevar_pop (TV_ALIAS_STMT_WALK);
1682
dd277d48 1683 return res;
3857274c 1684}
1685
604eef2c 1686
dd277d48 1687/* Based on the memory reference REF call WALKER for each vdef which
1688 defining statement may clobber REF, starting with VDEF. If REF
1689 is NULL_TREE, each defining statement is visited.
1690
1691 WALKER is called with REF, the current vdef and DATA. If WALKER
1692 returns true the walk is stopped, otherwise it continues.
1693
1694 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1695 PHI argument (but only one walk continues on merge points), the
1696 return value is true if any of the walks was successful.
1697
1698 The function returns the number of statements walked. */
604eef2c 1699
1700static unsigned int
1daead67 1701walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1702 bool (*walker)(ao_ref *, tree, void *), void *data,
dd277d48 1703 bitmap *visited, unsigned int cnt)
604eef2c 1704{
dd277d48 1705 do
1706 {
1707 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
604eef2c 1708
dd277d48 1709 if (*visited
1710 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1711 return cnt;
1712
1713 if (gimple_nop_p (def_stmt))
1714 return cnt;
1715 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1716 {
1717 unsigned i;
1718 if (!*visited)
1719 *visited = BITMAP_ALLOC (NULL);
1720 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1721 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1722 walker, data, visited, 0);
1723 return cnt;
1724 }
1725
02c74b9b 1726 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
dd277d48 1727 cnt++;
1728 if ((!ref
1daead67 1729 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
dd277d48 1730 && (*walker) (ref, vdef, data))
1731 return cnt;
1732
1733 vdef = gimple_vuse (def_stmt);
1734 }
1735 while (1);
604eef2c 1736}
1737
dd277d48 1738unsigned int
1daead67 1739walk_aliased_vdefs (ao_ref *ref, tree vdef,
1740 bool (*walker)(ao_ref *, tree, void *), void *data,
dd277d48 1741 bitmap *visited)
df3f0669 1742{
dd277d48 1743 bitmap local_visited = NULL;
1744 unsigned int ret;
1745
02c74b9b 1746 timevar_push (TV_ALIAS_STMT_WALK);
1747
dd277d48 1748 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1749 visited ? visited : &local_visited, 0);
1750 if (local_visited)
1751 BITMAP_FREE (local_visited);
1752
02c74b9b 1753 timevar_pop (TV_ALIAS_STMT_WALK);
1754
dd277d48 1755 return ret;
1756}
1757