OSDN Git Service

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