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