OSDN Git Service

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