inclhack.def (hpux_imaginary_i): Remove spaces.
[gcc.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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 "rtl.h"
28 #include "tm_p.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "timevar.h"
32 #include "expr.h"
33 #include "ggc.h"
34 #include "langhooks.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "tree-dump.h"
39 #include "gimple.h"
40 #include "tree-flow.h"
41 #include "tree-inline.h"
42 #include "tree-pass.h"
43 #include "convert.h"
44 #include "params.h"
45 #include "ipa-type-escape.h"
46 #include "vec.h"
47 #include "bitmap.h"
48 #include "vecprim.h"
49 #include "pointer-set.h"
50 #include "alloc-pool.h"
51 #include "tree-ssa-alias.h"
52
53 /* Broad overview of how alias analysis on gimple works:
54
55 Statements clobbering or using memory are linked through the
56 virtual operand factored use-def chain. The virtual operand
57 is unique per function, its symbol is accessible via gimple_vop (cfun).
58 Virtual operands are used for efficiently walking memory statements
59 in the gimple IL and are useful for things like value-numbering as
60 a generation count for memory references.
61
62 SSA_NAME pointers may have associated points-to information
63 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
64 points-to information is (re-)computed by the TODO_rebuild_alias
65 pass manager todo. Points-to information is also used for more
66 precise tracking of call-clobbered and call-used variables and
67 related disambiguations.
68
69 This file contains functions for disambiguating memory references,
70 the so called alias-oracle and tools for walking of the gimple IL.
71
72 The main alias-oracle entry-points are
73
74 bool stmt_may_clobber_ref_p (gimple, tree)
75
76 This function queries if a statement may invalidate (parts of)
77 the memory designated by the reference tree argument.
78
79 bool ref_maybe_used_by_stmt_p (gimple, tree)
80
81 This function queries if a statement may need (parts of) the
82 memory designated by the reference tree argument.
83
84 There are variants of these functions that only handle the call
85 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
86 Note that these do not disambiguate against a possible call lhs.
87
88 bool refs_may_alias_p (tree, tree)
89
90 This function tries to disambiguate two reference trees.
91
92 bool ptr_deref_may_alias_global_p (tree)
93
94 This function queries if dereferencing a pointer variable may
95 alias global memory.
96
97 More low-level disambiguators are available and documented in
98 this file. Low-level disambiguators dealing with points-to
99 information are in tree-ssa-structalias.c. */
100
101
102 /* Query statistics for the different low-level disambiguators.
103 A high-level query may trigger multiple of them. */
104
105 static struct {
106 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
107 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
108 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
109 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
110 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
111 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
112 } alias_stats;
113
114 void
115 dump_alias_stats (FILE *s)
116 {
117 fprintf (s, "\nAlias oracle query stats:\n");
118 fprintf (s, " refs_may_alias_p: "
119 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
120 HOST_WIDE_INT_PRINT_DEC" queries\n",
121 alias_stats.refs_may_alias_p_no_alias,
122 alias_stats.refs_may_alias_p_no_alias
123 + alias_stats.refs_may_alias_p_may_alias);
124 fprintf (s, " ref_maybe_used_by_call_p: "
125 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
126 HOST_WIDE_INT_PRINT_DEC" queries\n",
127 alias_stats.ref_maybe_used_by_call_p_no_alias,
128 alias_stats.refs_may_alias_p_no_alias
129 + alias_stats.ref_maybe_used_by_call_p_may_alias);
130 fprintf (s, " call_may_clobber_ref_p: "
131 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
132 HOST_WIDE_INT_PRINT_DEC" queries\n",
133 alias_stats.call_may_clobber_ref_p_no_alias,
134 alias_stats.call_may_clobber_ref_p_no_alias
135 + alias_stats.call_may_clobber_ref_p_may_alias);
136 }
137
138
139 /* Return true, if dereferencing PTR may alias with a global variable. */
140
141 bool
142 ptr_deref_may_alias_global_p (tree ptr)
143 {
144 struct ptr_info_def *pi;
145
146 /* If we end up with a pointer constant here that may point
147 to global memory. */
148 if (TREE_CODE (ptr) != SSA_NAME)
149 return true;
150
151 pi = SSA_NAME_PTR_INFO (ptr);
152
153 /* If we do not have points-to information for this variable,
154 we have to punt. */
155 if (!pi)
156 return true;
157
158 /* ??? This does not use TBAA to prune globals ptr may not access. */
159 return pt_solution_includes_global (&pi->pt);
160 }
161
162 /* Return true if dereferencing PTR may alias DECL.
163 The caller is responsible for applying TBAA to see if PTR
164 may access DECL at all. */
165
166 static bool
167 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
168 {
169 struct ptr_info_def *pi;
170
171 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
172 || TREE_CODE (ptr) == ADDR_EXPR
173 || TREE_CODE (ptr) == INTEGER_CST)
174 && (TREE_CODE (decl) == VAR_DECL
175 || TREE_CODE (decl) == PARM_DECL
176 || TREE_CODE (decl) == RESULT_DECL));
177
178 /* Non-aliased variables can not be pointed to. */
179 if (!may_be_aliased (decl))
180 return false;
181
182 /* ADDR_EXPR pointers either just offset another pointer or directly
183 specify the pointed-to set. */
184 if (TREE_CODE (ptr) == ADDR_EXPR)
185 {
186 tree base = get_base_address (TREE_OPERAND (ptr, 0));
187 if (base
188 && INDIRECT_REF_P (base))
189 ptr = TREE_OPERAND (base, 0);
190 else if (base
191 && SSA_VAR_P (base))
192 return operand_equal_p (base, decl, 0);
193 else if (base
194 && CONSTANT_CLASS_P (base))
195 return false;
196 else
197 return true;
198 }
199
200 /* We can end up with dereferencing constant pointers.
201 Just bail out in this case. */
202 if (TREE_CODE (ptr) == INTEGER_CST)
203 return true;
204
205 /* If we do not have useful points-to information for this pointer
206 we cannot disambiguate anything else. */
207 pi = SSA_NAME_PTR_INFO (ptr);
208 if (!pi)
209 return true;
210
211 return pt_solution_includes (&pi->pt, decl);
212 }
213
214 /* Return true if dereferenced PTR1 and PTR2 may alias.
215 The caller is responsible for applying TBAA to see if accesses
216 through PTR1 and PTR2 may conflict at all. */
217
218 static bool
219 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
220 {
221 struct ptr_info_def *pi1, *pi2;
222
223 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
224 || TREE_CODE (ptr1) == ADDR_EXPR
225 || TREE_CODE (ptr1) == INTEGER_CST)
226 && (TREE_CODE (ptr2) == SSA_NAME
227 || TREE_CODE (ptr2) == ADDR_EXPR
228 || TREE_CODE (ptr2) == INTEGER_CST));
229
230 /* ADDR_EXPR pointers either just offset another pointer or directly
231 specify the pointed-to set. */
232 if (TREE_CODE (ptr1) == ADDR_EXPR)
233 {
234 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
235 if (base
236 && INDIRECT_REF_P (base))
237 ptr1 = TREE_OPERAND (base, 0);
238 else if (base
239 && SSA_VAR_P (base))
240 return ptr_deref_may_alias_decl_p (ptr2, base);
241 else
242 return true;
243 }
244 if (TREE_CODE (ptr2) == ADDR_EXPR)
245 {
246 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
247 if (base
248 && INDIRECT_REF_P (base))
249 ptr2 = TREE_OPERAND (base, 0);
250 else if (base
251 && SSA_VAR_P (base))
252 return ptr_deref_may_alias_decl_p (ptr1, base);
253 else
254 return true;
255 }
256
257 /* We can end up with dereferencing constant pointers.
258 Just bail out in this case. */
259 if (TREE_CODE (ptr1) == INTEGER_CST
260 || TREE_CODE (ptr2) == INTEGER_CST)
261 return true;
262
263 /* We may end up with two empty points-to solutions for two same pointers.
264 In this case we still want to say both pointers alias, so shortcut
265 that here. */
266 if (ptr1 == ptr2)
267 return true;
268
269 /* If we do not have useful points-to information for either pointer
270 we cannot disambiguate anything else. */
271 pi1 = SSA_NAME_PTR_INFO (ptr1);
272 pi2 = SSA_NAME_PTR_INFO (ptr2);
273 if (!pi1 || !pi2)
274 return true;
275
276 /* If both pointers are restrict-qualified try to disambiguate
277 with restrict information. */
278 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
279 && TYPE_RESTRICT (TREE_TYPE (ptr2))
280 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
281 return false;
282
283 /* ??? This does not use TBAA to prune decls from the intersection
284 that not both pointers may access. */
285 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
286 }
287
288 /* Return true if dereferencing PTR may alias *REF.
289 The caller is responsible for applying TBAA to see if PTR
290 may access *REF at all. */
291
292 static bool
293 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
294 {
295 tree base = ao_ref_base (ref);
296
297 if (INDIRECT_REF_P (base))
298 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
299 else if (SSA_VAR_P (base))
300 return ptr_deref_may_alias_decl_p (ptr, base);
301
302 return true;
303 }
304
305 static bool
306 ptr_deref_may_alias_ref_p (tree ptr, tree ref)
307 {
308 ao_ref r;
309 ao_ref_init (&r, ref);
310 return ptr_deref_may_alias_ref_p_1 (ptr, &r);
311 }
312
313
314 /* Dump alias information on FILE. */
315
316 void
317 dump_alias_info (FILE *file)
318 {
319 size_t i;
320 const char *funcname
321 = lang_hooks.decl_printable_name (current_function_decl, 2);
322 referenced_var_iterator rvi;
323 tree var;
324
325 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
326
327 fprintf (file, "Aliased symbols\n\n");
328
329 FOR_EACH_REFERENCED_VAR (var, rvi)
330 {
331 if (may_be_aliased (var))
332 dump_variable (file, var);
333 }
334
335 fprintf (file, "\nCall clobber information\n");
336
337 fprintf (file, "\nESCAPED");
338 dump_points_to_solution (file, &cfun->gimple_df->escaped);
339 fprintf (file, "\nCALLUSED");
340 dump_points_to_solution (file, &cfun->gimple_df->callused);
341
342 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
343
344 for (i = 1; i < num_ssa_names; i++)
345 {
346 tree ptr = ssa_name (i);
347 struct ptr_info_def *pi;
348
349 if (ptr == NULL_TREE
350 || SSA_NAME_IN_FREE_LIST (ptr))
351 continue;
352
353 pi = SSA_NAME_PTR_INFO (ptr);
354 if (pi)
355 dump_points_to_info_for (file, ptr);
356 }
357
358 fprintf (file, "\n");
359 }
360
361
362 /* Dump alias information on stderr. */
363
364 void
365 debug_alias_info (void)
366 {
367 dump_alias_info (stderr);
368 }
369
370
371 /* Return the alias information associated with pointer T. It creates a
372 new instance if none existed. */
373
374 struct ptr_info_def *
375 get_ptr_info (tree t)
376 {
377 struct ptr_info_def *pi;
378
379 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
380
381 pi = SSA_NAME_PTR_INFO (t);
382 if (pi == NULL)
383 {
384 pi = GGC_CNEW (struct ptr_info_def);
385 pt_solution_reset (&pi->pt);
386 SSA_NAME_PTR_INFO (t) = pi;
387 }
388
389 return pi;
390 }
391
392 /* Dump the points-to set *PT into FILE. */
393
394 void
395 dump_points_to_solution (FILE *file, struct pt_solution *pt)
396 {
397 if (pt->anything)
398 fprintf (file, ", points-to anything");
399
400 if (pt->nonlocal)
401 fprintf (file, ", points-to non-local");
402
403 if (pt->escaped)
404 fprintf (file, ", points-to escaped");
405
406 if (pt->null)
407 fprintf (file, ", points-to NULL");
408
409 if (pt->vars)
410 {
411 fprintf (file, ", points-to vars: ");
412 dump_decl_set (file, pt->vars);
413 if (pt->vars_contains_global)
414 fprintf (file, " (includes global vars)");
415 }
416 }
417
418 /* Dump points-to information for SSA_NAME PTR into FILE. */
419
420 void
421 dump_points_to_info_for (FILE *file, tree ptr)
422 {
423 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
424
425 print_generic_expr (file, ptr, dump_flags);
426
427 if (pi)
428 dump_points_to_solution (file, &pi->pt);
429 else
430 fprintf (file, ", points-to anything");
431
432 fprintf (file, "\n");
433 }
434
435
436 /* Dump points-to information for VAR into stderr. */
437
438 void
439 debug_points_to_info_for (tree var)
440 {
441 dump_points_to_info_for (stderr, var);
442 }
443
444
445 /* Initializes the alias-oracle reference representation *R from REF. */
446
447 void
448 ao_ref_init (ao_ref *r, tree ref)
449 {
450 r->ref = ref;
451 r->base = NULL_TREE;
452 r->offset = 0;
453 r->size = -1;
454 r->max_size = -1;
455 r->ref_alias_set = -1;
456 r->base_alias_set = -1;
457 }
458
459 /* Returns the base object of the memory reference *REF. */
460
461 tree
462 ao_ref_base (ao_ref *ref)
463 {
464 if (ref->base)
465 return ref->base;
466 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
467 &ref->max_size);
468 return ref->base;
469 }
470
471 /* Returns the base object alias set of the memory reference *REF. */
472
473 static alias_set_type ATTRIBUTE_UNUSED
474 ao_ref_base_alias_set (ao_ref *ref)
475 {
476 if (ref->base_alias_set != -1)
477 return ref->base_alias_set;
478 ref->base_alias_set = get_alias_set (ao_ref_base (ref));
479 return ref->base_alias_set;
480 }
481
482 /* Returns the reference alias set of the memory reference *REF. */
483
484 alias_set_type
485 ao_ref_alias_set (ao_ref *ref)
486 {
487 if (ref->ref_alias_set != -1)
488 return ref->ref_alias_set;
489 ref->ref_alias_set = get_alias_set (ref->ref);
490 return ref->ref_alias_set;
491 }
492
493 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
494 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
495 decide. */
496
497 static inline int
498 same_type_for_tbaa (tree type1, tree type2)
499 {
500 type1 = TYPE_MAIN_VARIANT (type1);
501 type2 = TYPE_MAIN_VARIANT (type2);
502
503 /* If we would have to do structural comparison bail out. */
504 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
505 || TYPE_STRUCTURAL_EQUALITY_P (type2))
506 return -1;
507
508 /* Compare the canonical types. */
509 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
510 return 1;
511
512 /* ??? Array types are not properly unified in all cases as we have
513 spurious changes in the index types for example. Removing this
514 causes all sorts of problems with the Fortran frontend. */
515 if (TREE_CODE (type1) == ARRAY_TYPE
516 && TREE_CODE (type2) == ARRAY_TYPE)
517 return -1;
518
519 /* The types are known to be not equal. */
520 return 0;
521 }
522
523 /* Determine if the two component references REF1 and REF2 which are
524 based on access types TYPE1 and TYPE2 and of which at least one is based
525 on an indirect reference may alias. */
526
527 static bool
528 nonaliasing_component_refs_p (tree ref1, tree type1,
529 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
530 tree ref2, tree type2,
531 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
532 {
533 /* If one reference is a component references through pointers try to find a
534 common base and apply offset based disambiguation. This handles
535 for example
536 struct A { int i; int j; } *q;
537 struct B { struct A a; int k; } *p;
538 disambiguating q->i and p->a.j. */
539 tree *refp;
540 int same_p;
541
542 /* Now search for the type1 in the access path of ref2. This
543 would be a common base for doing offset based disambiguation on. */
544 refp = &ref2;
545 while (handled_component_p (*refp)
546 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
547 refp = &TREE_OPERAND (*refp, 0);
548 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
549 /* If we couldn't compare types we have to bail out. */
550 if (same_p == -1)
551 return true;
552 else if (same_p == 1)
553 {
554 HOST_WIDE_INT offadj, sztmp, msztmp;
555 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
556 offset2 -= offadj;
557 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
558 }
559 /* If we didn't find a common base, try the other way around. */
560 refp = &ref1;
561 while (handled_component_p (*refp)
562 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
563 refp = &TREE_OPERAND (*refp, 0);
564 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
565 /* If we couldn't compare types we have to bail out. */
566 if (same_p == -1)
567 return true;
568 else if (same_p == 1)
569 {
570 HOST_WIDE_INT offadj, sztmp, msztmp;
571 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
572 offset1 -= offadj;
573 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
574 }
575 /* If we have two type access paths B1.path1 and B2.path2 they may
576 only alias if either B1 is in B2.path2 or B2 is in B1.path1. */
577 return false;
578 }
579
580 /* Return true if two memory references based on the variables BASE1
581 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
582 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
583
584 static bool
585 decl_refs_may_alias_p (tree base1,
586 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
587 tree base2,
588 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
589 {
590 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
591
592 /* If both references are based on different variables, they cannot alias. */
593 if (!operand_equal_p (base1, base2, 0))
594 return false;
595
596 /* If both references are based on the same variable, they cannot alias if
597 the accesses do not overlap. */
598 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
599 }
600
601 /* Return true if an indirect reference based on *PTR1 constrained
602 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
603 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
604 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
605 in which case they are computed on-demand. REF1 and REF2
606 if non-NULL are the complete memory reference trees. */
607
608 static bool
609 indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
610 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
611 alias_set_type base1_alias_set,
612 tree ref2, tree base2,
613 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
614 alias_set_type base2_alias_set)
615 {
616 /* If only one reference is based on a variable, they cannot alias if
617 the pointer access is beyond the extent of the variable access.
618 (the pointer base cannot validly point to an offset less than zero
619 of the variable).
620 They also cannot alias if the pointer may not point to the decl. */
621 if (max_size2 != -1
622 && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
623 return false;
624 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
625 return false;
626
627 /* Disambiguations that rely on strict aliasing rules follow. */
628 if (!flag_strict_aliasing)
629 return true;
630
631 /* If the alias set for a pointer access is zero all bets are off. */
632 if (base1_alias_set == -1)
633 base1_alias_set = get_deref_alias_set (ptr1);
634 if (base1_alias_set == 0)
635 return true;
636 if (base2_alias_set == -1)
637 base2_alias_set = get_alias_set (base2);
638
639 /* If both references are through the same type, they do not alias
640 if the accesses do not overlap. This does extra disambiguation
641 for mixed/pointer accesses but requires strict aliasing. */
642 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
643 TREE_TYPE (base2)) == 1)
644 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
645
646 /* The only way to access a variable is through a pointer dereference
647 of the same alias set or a subset of it. */
648 if (base1_alias_set != base2_alias_set
649 && !alias_set_subset_of (base1_alias_set, base2_alias_set))
650 return false;
651
652 /* Do access-path based disambiguation. */
653 if (ref1 && ref2
654 && handled_component_p (ref1)
655 && handled_component_p (ref2))
656 return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
657 offset1, max_size1,
658 ref2, TREE_TYPE (base2),
659 offset2, max_size2);
660
661 return true;
662 }
663
664 /* Return true if two indirect references based on *PTR1
665 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
666 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
667 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
668 in which case they are computed on-demand. REF1 and REF2
669 if non-NULL are the complete memory reference trees. */
670
671 static bool
672 indirect_refs_may_alias_p (tree ref1, tree ptr1,
673 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
674 alias_set_type base1_alias_set,
675 tree ref2, tree ptr2,
676 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
677 alias_set_type base2_alias_set)
678 {
679 /* If both bases are based on pointers they cannot alias if they may not
680 point to the same memory object or if they point to the same object
681 and the accesses do not overlap. */
682 if (operand_equal_p (ptr1, ptr2, 0))
683 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
684 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
685 return false;
686
687 /* Disambiguations that rely on strict aliasing rules follow. */
688 if (!flag_strict_aliasing)
689 return true;
690
691 /* If the alias set for a pointer access is zero all bets are off. */
692 if (base1_alias_set == -1)
693 base1_alias_set = get_deref_alias_set (ptr1);
694 if (base1_alias_set == 0)
695 return true;
696 if (base2_alias_set == -1)
697 base2_alias_set = get_deref_alias_set (ptr2);
698 if (base2_alias_set == 0)
699 return true;
700
701 /* If both references are through the same type, they do not alias
702 if the accesses do not overlap. This does extra disambiguation
703 for mixed/pointer accesses but requires strict aliasing. */
704 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
705 TREE_TYPE (TREE_TYPE (ptr2))) == 1)
706 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
707
708 /* Do type-based disambiguation. */
709 if (base1_alias_set != base2_alias_set
710 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
711 return false;
712
713 /* Do access-path based disambiguation. */
714 if (ref1 && ref2
715 && handled_component_p (ref1)
716 && handled_component_p (ref2))
717 return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
718 offset1, max_size1,
719 ref2, TREE_TYPE (TREE_TYPE (ptr2)),
720 offset2, max_size2);
721
722 return true;
723 }
724
725 /* Return true, if the two memory references REF1 and REF2 may alias. */
726
727 bool
728 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
729 {
730 tree base1, base2;
731 HOST_WIDE_INT offset1 = 0, offset2 = 0;
732 HOST_WIDE_INT size1 = -1, size2 = -1;
733 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
734 bool var1_p, var2_p, ind1_p, ind2_p;
735 alias_set_type set;
736
737 gcc_assert ((!ref1->ref
738 || SSA_VAR_P (ref1->ref)
739 || handled_component_p (ref1->ref)
740 || INDIRECT_REF_P (ref1->ref)
741 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
742 && (!ref2->ref
743 || SSA_VAR_P (ref2->ref)
744 || handled_component_p (ref2->ref)
745 || INDIRECT_REF_P (ref2->ref)
746 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
747
748 /* Decompose the references into their base objects and the access. */
749 base1 = ao_ref_base (ref1);
750 offset1 = ref1->offset;
751 size1 = ref1->size;
752 max_size1 = ref1->max_size;
753 base2 = ao_ref_base (ref2);
754 offset2 = ref2->offset;
755 size2 = ref2->size;
756 max_size2 = ref2->max_size;
757
758 /* We can end up with registers or constants as bases for example from
759 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
760 which is seen as a struct copy. */
761 if (TREE_CODE (base1) == SSA_NAME
762 || TREE_CODE (base2) == SSA_NAME
763 || is_gimple_min_invariant (base1)
764 || is_gimple_min_invariant (base2))
765 return false;
766
767 /* Defer to simple offset based disambiguation if we have
768 references based on two decls. Do this before defering to
769 TBAA to handle must-alias cases in conformance with the
770 GCC extension of allowing type-punning through unions. */
771 var1_p = SSA_VAR_P (base1);
772 var2_p = SSA_VAR_P (base2);
773 if (var1_p && var2_p)
774 return decl_refs_may_alias_p (base1, offset1, max_size1,
775 base2, offset2, max_size2);
776
777 /* First defer to TBAA if possible. */
778 if (tbaa_p
779 && flag_strict_aliasing
780 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
781 ao_ref_alias_set (ref2)))
782 return false;
783
784 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
785 TBAA disambiguation based on the access type is possible, so bail
786 out only after that check. */
787 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
788 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
789 return true;
790
791 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
792 ind1_p = INDIRECT_REF_P (base1);
793 ind2_p = INDIRECT_REF_P (base2);
794 set = tbaa_p ? -1 : 0;
795 if (var1_p && ind2_p)
796 return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
797 offset2, max_size2, set,
798 ref1->ref, base1,
799 offset1, max_size1, set);
800 else if (ind1_p && var2_p)
801 return indirect_ref_may_alias_decl_p (ref1->ref, TREE_OPERAND (base1, 0),
802 offset1, max_size1, set,
803 ref2->ref, base2,
804 offset2, max_size2, set);
805 else if (ind1_p && ind2_p)
806 return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
807 offset1, max_size1, set,
808 ref2->ref, TREE_OPERAND (base2, 0),
809 offset2, max_size2, set);
810
811 gcc_unreachable ();
812 }
813
814 bool
815 refs_may_alias_p (tree ref1, tree ref2)
816 {
817 ao_ref r1, r2;
818 bool res;
819 ao_ref_init (&r1, ref1);
820 ao_ref_init (&r2, ref2);
821 res = refs_may_alias_p_1 (&r1, &r2, true);
822 if (res)
823 ++alias_stats.refs_may_alias_p_may_alias;
824 else
825 ++alias_stats.refs_may_alias_p_no_alias;
826 return res;
827 }
828
829 /* Returns true if there is a anti-dependence for the STORE that
830 executes after the LOAD. */
831
832 bool
833 refs_anti_dependent_p (tree load, tree store)
834 {
835 ao_ref r1, r2;
836 ao_ref_init (&r1, load);
837 ao_ref_init (&r2, store);
838 return refs_may_alias_p_1 (&r1, &r2, false);
839 }
840
841 /* Returns true if there is a output dependence for the stores
842 STORE1 and STORE2. */
843
844 bool
845 refs_output_dependent_p (tree store1, tree store2)
846 {
847 ao_ref r1, r2;
848 ao_ref_init (&r1, store1);
849 ao_ref_init (&r2, store2);
850 return refs_may_alias_p_1 (&r1, &r2, false);
851 }
852
853 /* If the call CALL may use the memory reference REF return true,
854 otherwise return false. */
855
856 static bool
857 ref_maybe_used_by_call_p_1 (gimple call, tree ref)
858 {
859 tree base, callee;
860 unsigned i;
861 int flags = gimple_call_flags (call);
862
863 /* Const functions without a static chain do not implicitly use memory. */
864 if (!gimple_call_chain (call)
865 && (flags & (ECF_CONST|ECF_NOVOPS)))
866 goto process_args;
867
868 base = get_base_address (ref);
869 if (!base)
870 return true;
871
872 /* If the reference is based on a decl that is not aliased the call
873 cannot possibly use it. */
874 if (DECL_P (base)
875 && !may_be_aliased (base)
876 /* But local statics can be used through recursion. */
877 && !is_global_var (base))
878 goto process_args;
879
880 callee = gimple_call_fndecl (call);
881
882 /* Handle those builtin functions explicitly that do not act as
883 escape points. See tree-ssa-structalias.c:find_func_aliases
884 for the list of builtins we might need to handle here. */
885 if (callee != NULL_TREE
886 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
887 switch (DECL_FUNCTION_CODE (callee))
888 {
889 /* All the following functions clobber memory pointed to by
890 their first argument. */
891 case BUILT_IN_STRCPY:
892 case BUILT_IN_STRNCPY:
893 case BUILT_IN_BCOPY:
894 case BUILT_IN_MEMCPY:
895 case BUILT_IN_MEMMOVE:
896 case BUILT_IN_MEMPCPY:
897 case BUILT_IN_STPCPY:
898 case BUILT_IN_STPNCPY:
899 case BUILT_IN_STRCAT:
900 case BUILT_IN_STRNCAT:
901 {
902 tree src = gimple_call_arg (call, 1);
903 return ptr_deref_may_alias_ref_p (src, ref);
904 }
905 /* The following builtins do not read from memory. */
906 case BUILT_IN_FREE:
907 case BUILT_IN_MEMSET:
908 case BUILT_IN_FREXP:
909 case BUILT_IN_FREXPF:
910 case BUILT_IN_FREXPL:
911 case BUILT_IN_GAMMA_R:
912 case BUILT_IN_GAMMAF_R:
913 case BUILT_IN_GAMMAL_R:
914 case BUILT_IN_LGAMMA_R:
915 case BUILT_IN_LGAMMAF_R:
916 case BUILT_IN_LGAMMAL_R:
917 case BUILT_IN_MODF:
918 case BUILT_IN_MODFF:
919 case BUILT_IN_MODFL:
920 case BUILT_IN_REMQUO:
921 case BUILT_IN_REMQUOF:
922 case BUILT_IN_REMQUOL:
923 case BUILT_IN_SINCOS:
924 case BUILT_IN_SINCOSF:
925 case BUILT_IN_SINCOSL:
926 return false;
927
928 default:
929 /* Fallthru to general call handling. */;
930 }
931
932 /* Check if base is a global static variable that is not read
933 by the function. */
934 if (TREE_CODE (base) == VAR_DECL
935 && TREE_STATIC (base)
936 && !TREE_PUBLIC (base))
937 {
938 bitmap not_read;
939
940 if (callee != NULL_TREE
941 && (not_read
942 = ipa_reference_get_not_read_global (cgraph_node (callee)))
943 && bitmap_bit_p (not_read, DECL_UID (base)))
944 goto process_args;
945 }
946
947 /* If the base variable is call-used or call-clobbered then
948 it may be used. */
949 if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
950 {
951 if (DECL_P (base))
952 {
953 if (is_call_used (base))
954 return true;
955 }
956 else if (INDIRECT_REF_P (base)
957 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
958 {
959 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
960 if (!pi)
961 return true;
962
963 if (pt_solution_includes_global (&pi->pt)
964 || pt_solutions_intersect (&cfun->gimple_df->callused, &pi->pt)
965 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
966 return true;
967 }
968 else
969 return true;
970 }
971 else
972 {
973 if (DECL_P (base))
974 {
975 if (is_call_clobbered (base))
976 return true;
977 }
978 else if (INDIRECT_REF_P (base)
979 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
980 {
981 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
982 if (!pi)
983 return true;
984
985 if (pt_solution_includes_global (&pi->pt)
986 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
987 return true;
988 }
989 else
990 return true;
991 }
992
993 /* Inspect call arguments for passed-by-value aliases. */
994 process_args:
995 for (i = 0; i < gimple_call_num_args (call); ++i)
996 {
997 tree op = gimple_call_arg (call, i);
998
999 if (TREE_CODE (op) == EXC_PTR_EXPR
1000 || TREE_CODE (op) == FILTER_EXPR)
1001 continue;
1002
1003 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1004 op = TREE_OPERAND (op, 0);
1005
1006 if (TREE_CODE (op) != SSA_NAME
1007 && !is_gimple_min_invariant (op)
1008 && refs_may_alias_p (op, ref))
1009 return true;
1010 }
1011
1012 return false;
1013 }
1014
1015 static bool
1016 ref_maybe_used_by_call_p (gimple call, tree ref)
1017 {
1018 bool res = ref_maybe_used_by_call_p_1 (call, ref);
1019 if (res)
1020 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1021 else
1022 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1023 return res;
1024 }
1025
1026
1027 /* If the statement STMT may use the memory reference REF return
1028 true, otherwise return false. */
1029
1030 bool
1031 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1032 {
1033 if (is_gimple_assign (stmt))
1034 {
1035 tree rhs;
1036
1037 /* All memory assign statements are single. */
1038 if (!gimple_assign_single_p (stmt))
1039 return false;
1040
1041 rhs = gimple_assign_rhs1 (stmt);
1042 if (is_gimple_reg (rhs)
1043 || is_gimple_min_invariant (rhs)
1044 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1045 return false;
1046
1047 return refs_may_alias_p (rhs, ref);
1048 }
1049 else if (is_gimple_call (stmt))
1050 return ref_maybe_used_by_call_p (stmt, ref);
1051
1052 return true;
1053 }
1054
1055 /* If the call in statement CALL may clobber the memory reference REF
1056 return true, otherwise return false. */
1057
1058 static bool
1059 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1060 {
1061 tree base;
1062 tree callee;
1063
1064 /* If the call is pure or const it cannot clobber anything. */
1065 if (gimple_call_flags (call)
1066 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1067 return false;
1068
1069 base = ao_ref_base (ref);
1070 if (!base)
1071 return true;
1072
1073 if (TREE_CODE (base) == SSA_NAME
1074 || CONSTANT_CLASS_P (base))
1075 return false;
1076
1077 /* If the reference is based on a decl that is not aliased the call
1078 cannot possibly clobber it. */
1079 if (DECL_P (base)
1080 && !may_be_aliased (base)
1081 /* But local non-readonly statics can be modified through recursion
1082 or the call may implement a threading barrier which we must
1083 treat as may-def. */
1084 && (TREE_READONLY (base)
1085 || !is_global_var (base)))
1086 return false;
1087
1088 callee = gimple_call_fndecl (call);
1089
1090 /* Handle those builtin functions explicitly that do not act as
1091 escape points. See tree-ssa-structalias.c:find_func_aliases
1092 for the list of builtins we might need to handle here. */
1093 if (callee != NULL_TREE
1094 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1095 switch (DECL_FUNCTION_CODE (callee))
1096 {
1097 /* All the following functions clobber memory pointed to by
1098 their first argument. */
1099 case BUILT_IN_STRCPY:
1100 case BUILT_IN_STRNCPY:
1101 case BUILT_IN_BCOPY:
1102 case BUILT_IN_MEMCPY:
1103 case BUILT_IN_MEMMOVE:
1104 case BUILT_IN_MEMPCPY:
1105 case BUILT_IN_STPCPY:
1106 case BUILT_IN_STPNCPY:
1107 case BUILT_IN_STRCAT:
1108 case BUILT_IN_STRNCAT:
1109 {
1110 tree dest = gimple_call_arg (call, 0);
1111 return ptr_deref_may_alias_ref_p_1 (dest, ref);
1112 }
1113 /* Freeing memory kills the pointed-to memory. More importantly
1114 the call has to serve as a barrier for moving loads and stores
1115 across it. Same is true for memset. */
1116 case BUILT_IN_FREE:
1117 case BUILT_IN_MEMSET:
1118 {
1119 tree ptr = gimple_call_arg (call, 0);
1120 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1121 }
1122 case BUILT_IN_GAMMA_R:
1123 case BUILT_IN_GAMMAF_R:
1124 case BUILT_IN_GAMMAL_R:
1125 case BUILT_IN_LGAMMA_R:
1126 case BUILT_IN_LGAMMAF_R:
1127 case BUILT_IN_LGAMMAL_R:
1128 {
1129 tree out = gimple_call_arg (call, 1);
1130 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1131 return true;
1132 if (flag_errno_math)
1133 break;
1134 return false;
1135 }
1136 case BUILT_IN_FREXP:
1137 case BUILT_IN_FREXPF:
1138 case BUILT_IN_FREXPL:
1139 case BUILT_IN_MODF:
1140 case BUILT_IN_MODFF:
1141 case BUILT_IN_MODFL:
1142 {
1143 tree out = gimple_call_arg (call, 1);
1144 return ptr_deref_may_alias_ref_p_1 (out, ref);
1145 }
1146 case BUILT_IN_REMQUO:
1147 case BUILT_IN_REMQUOF:
1148 case BUILT_IN_REMQUOL:
1149 {
1150 tree out = gimple_call_arg (call, 2);
1151 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1152 return true;
1153 if (flag_errno_math)
1154 break;
1155 return false;
1156 }
1157 case BUILT_IN_SINCOS:
1158 case BUILT_IN_SINCOSF:
1159 case BUILT_IN_SINCOSL:
1160 {
1161 tree sin = gimple_call_arg (call, 1);
1162 tree cos = gimple_call_arg (call, 2);
1163 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1164 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1165 }
1166 default:
1167 /* Fallthru to general call handling. */;
1168 }
1169
1170 /* Check if base is a global static variable that is not written
1171 by the function. */
1172 if (callee != NULL_TREE
1173 && TREE_CODE (base) == VAR_DECL
1174 && TREE_STATIC (base)
1175 && !TREE_PUBLIC (base))
1176 {
1177 bitmap not_written;
1178
1179 if ((not_written
1180 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1181 && bitmap_bit_p (not_written, DECL_UID (base)))
1182 return false;
1183 }
1184
1185 if (DECL_P (base))
1186 return is_call_clobbered (base);
1187 else if (INDIRECT_REF_P (base)
1188 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1189 {
1190 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1191 if (!pi)
1192 return true;
1193
1194 return (pt_solution_includes_global (&pi->pt)
1195 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt));
1196 }
1197
1198 return true;
1199 }
1200
1201 static bool ATTRIBUTE_UNUSED
1202 call_may_clobber_ref_p (gimple call, tree ref)
1203 {
1204 bool res;
1205 ao_ref r;
1206 ao_ref_init (&r, ref);
1207 res = call_may_clobber_ref_p_1 (call, &r);
1208 if (res)
1209 ++alias_stats.call_may_clobber_ref_p_may_alias;
1210 else
1211 ++alias_stats.call_may_clobber_ref_p_no_alias;
1212 return res;
1213 }
1214
1215
1216 /* If the statement STMT may clobber the memory reference REF return true,
1217 otherwise return false. */
1218
1219 bool
1220 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1221 {
1222 if (is_gimple_call (stmt))
1223 {
1224 tree lhs = gimple_call_lhs (stmt);
1225 if (lhs
1226 && !is_gimple_reg (lhs))
1227 {
1228 ao_ref r;
1229 ao_ref_init (&r, lhs);
1230 if (refs_may_alias_p_1 (ref, &r, true))
1231 return true;
1232 }
1233
1234 return call_may_clobber_ref_p_1 (stmt, ref);
1235 }
1236 else if (is_gimple_assign (stmt))
1237 {
1238 ao_ref r;
1239 ao_ref_init (&r, gimple_assign_lhs (stmt));
1240 return refs_may_alias_p_1 (ref, &r, true);
1241 }
1242 else if (gimple_code (stmt) == GIMPLE_ASM)
1243 return true;
1244
1245 return false;
1246 }
1247
1248 bool
1249 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1250 {
1251 ao_ref r;
1252 ao_ref_init (&r, ref);
1253 return stmt_may_clobber_ref_p_1 (stmt, &r);
1254 }
1255
1256
1257 static tree get_continuation_for_phi (gimple, ao_ref *, bitmap *);
1258
1259 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1260 TARGET or a statement clobbering the memory reference REF in which
1261 case false is returned. The walk starts with VUSE, one argument of PHI. */
1262
1263 static bool
1264 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1265 tree vuse, bitmap *visited)
1266 {
1267 if (!*visited)
1268 *visited = BITMAP_ALLOC (NULL);
1269
1270 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1271
1272 /* Walk until we hit the target. */
1273 while (vuse != target)
1274 {
1275 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1276 /* Recurse for PHI nodes. */
1277 if (gimple_code (def_stmt) == GIMPLE_PHI)
1278 {
1279 /* An already visited PHI node ends the walk successfully. */
1280 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1281 return true;
1282 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1283 if (!vuse)
1284 return false;
1285 continue;
1286 }
1287 /* A clobbering statement or the end of the IL ends it failing. */
1288 else if (gimple_nop_p (def_stmt)
1289 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1290 return false;
1291 vuse = gimple_vuse (def_stmt);
1292 }
1293 return true;
1294 }
1295
1296 /* Starting from a PHI node for the virtual operand of the memory reference
1297 REF find a continuation virtual operand that allows to continue walking
1298 statements dominating PHI skipping only statements that cannot possibly
1299 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1300 be found. */
1301
1302 static tree
1303 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1304 {
1305 unsigned nargs = gimple_phi_num_args (phi);
1306
1307 /* Through a single-argument PHI we can simply look through. */
1308 if (nargs == 1)
1309 return PHI_ARG_DEF (phi, 0);
1310
1311 /* For two arguments try to skip non-aliasing code until we hit
1312 the phi argument definition that dominates the other one. */
1313 if (nargs == 2)
1314 {
1315 tree arg0 = PHI_ARG_DEF (phi, 0);
1316 tree arg1 = PHI_ARG_DEF (phi, 1);
1317 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1318 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1319
1320 if (arg0 == arg1)
1321 return arg0;
1322 else if (gimple_nop_p (def0)
1323 || (!gimple_nop_p (def1)
1324 && dominated_by_p (CDI_DOMINATORS,
1325 gimple_bb (def1), gimple_bb (def0))))
1326 {
1327 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1328 return arg0;
1329 }
1330 else if (gimple_nop_p (def1)
1331 || dominated_by_p (CDI_DOMINATORS,
1332 gimple_bb (def0), gimple_bb (def1)))
1333 {
1334 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1335 return arg1;
1336 }
1337 }
1338
1339 return NULL_TREE;
1340 }
1341
1342 /* Based on the memory reference REF and its virtual use VUSE call
1343 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1344 itself. That is, for each virtual use for which its defining statement
1345 does not clobber REF.
1346
1347 WALKER is called with REF, the current virtual use and DATA. If
1348 WALKER returns non-NULL the walk stops and its result is returned.
1349 At the end of a non-successful walk NULL is returned.
1350
1351 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1352 use which definition is a statement that may clobber REF and DATA.
1353 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1354 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1355 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1356 to adjust REF and *DATA to make that valid.
1357
1358 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1359
1360 void *
1361 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1362 void *(*walker)(ao_ref *, tree, void *),
1363 void *(*translate)(ao_ref *, tree, void *), void *data)
1364 {
1365 bitmap visited = NULL;
1366 void *res;
1367
1368 timevar_push (TV_ALIAS_STMT_WALK);
1369
1370 do
1371 {
1372 gimple def_stmt;
1373
1374 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1375 res = (*walker) (ref, vuse, data);
1376 if (res)
1377 break;
1378
1379 def_stmt = SSA_NAME_DEF_STMT (vuse);
1380 if (gimple_nop_p (def_stmt))
1381 break;
1382 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1383 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1384 else
1385 {
1386 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1387 {
1388 if (!translate)
1389 break;
1390 res = (*translate) (ref, vuse, data);
1391 /* Failed lookup and translation. */
1392 if (res == (void *)-1)
1393 {
1394 res = NULL;
1395 break;
1396 }
1397 /* Lookup succeeded. */
1398 else if (res != NULL)
1399 break;
1400 /* Translation succeeded, continue walking. */
1401 }
1402 vuse = gimple_vuse (def_stmt);
1403 }
1404 }
1405 while (vuse);
1406
1407 if (visited)
1408 BITMAP_FREE (visited);
1409
1410 timevar_pop (TV_ALIAS_STMT_WALK);
1411
1412 return res;
1413 }
1414
1415
1416 /* Based on the memory reference REF call WALKER for each vdef which
1417 defining statement may clobber REF, starting with VDEF. If REF
1418 is NULL_TREE, each defining statement is visited.
1419
1420 WALKER is called with REF, the current vdef and DATA. If WALKER
1421 returns true the walk is stopped, otherwise it continues.
1422
1423 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1424 PHI argument (but only one walk continues on merge points), the
1425 return value is true if any of the walks was successful.
1426
1427 The function returns the number of statements walked. */
1428
1429 static unsigned int
1430 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1431 bool (*walker)(ao_ref *, tree, void *), void *data,
1432 bitmap *visited, unsigned int cnt)
1433 {
1434 do
1435 {
1436 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1437
1438 if (*visited
1439 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1440 return cnt;
1441
1442 if (gimple_nop_p (def_stmt))
1443 return cnt;
1444 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1445 {
1446 unsigned i;
1447 if (!*visited)
1448 *visited = BITMAP_ALLOC (NULL);
1449 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1450 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1451 walker, data, visited, 0);
1452 return cnt;
1453 }
1454
1455 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1456 cnt++;
1457 if ((!ref
1458 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1459 && (*walker) (ref, vdef, data))
1460 return cnt;
1461
1462 vdef = gimple_vuse (def_stmt);
1463 }
1464 while (1);
1465 }
1466
1467 unsigned int
1468 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1469 bool (*walker)(ao_ref *, tree, void *), void *data,
1470 bitmap *visited)
1471 {
1472 bitmap local_visited = NULL;
1473 unsigned int ret;
1474
1475 timevar_push (TV_ALIAS_STMT_WALK);
1476
1477 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1478 visited ? visited : &local_visited, 0);
1479 if (local_visited)
1480 BITMAP_FREE (local_visited);
1481
1482 timevar_pop (TV_ALIAS_STMT_WALK);
1483
1484 return ret;
1485 }
1486