OSDN Git Service

* gcc.dg/funcorder.c: Remove XFAIL for hppa*64*-*-*.
[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 "basic-block.h"
29 #include "timevar.h"
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "tree-pretty-print.h"
35 #include "tree-dump.h"
36 #include "gimple.h"
37 #include "tree-flow.h"
38 #include "tree-inline.h"
39 #include "tree-pass.h"
40 #include "convert.h"
41 #include "params.h"
42 #include "ipa-type-escape.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 false;
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 (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 (base2),
811                                       ref2_alias_set, base2_alias_set,
812                                       offset2, max_size2, true);
813
814   return true;
815 }
816
817 /* Return true if two indirect references based on *PTR1
818    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
819    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
820    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
821    in which case they are computed on-demand.  REF1 and REF2
822    if non-NULL are the complete memory reference trees. */
823
824 static bool
825 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
826                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
827                            alias_set_type ref1_alias_set,
828                            alias_set_type base1_alias_set,
829                            tree ref2 ATTRIBUTE_UNUSED, tree base2,
830                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
831                            alias_set_type ref2_alias_set,
832                            alias_set_type base2_alias_set, bool tbaa_p)
833 {
834   tree ptr1;
835   tree ptr2;
836   tree ptrtype1, ptrtype2;
837
838   ptr1 = TREE_OPERAND (base1, 0);
839   ptr2 = TREE_OPERAND (base2, 0);
840
841   /* If both bases are based on pointers they cannot alias if they may not
842      point to the same memory object or if they point to the same object
843      and the accesses do not overlap.  */
844   if ((!cfun || gimple_in_ssa_p (cfun))
845       && operand_equal_p (ptr1, ptr2, 0)
846       && (((TREE_CODE (base1) != TARGET_MEM_REF
847             || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
848            && (TREE_CODE (base2) != TARGET_MEM_REF
849                || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
850           || (TREE_CODE (base1) == TARGET_MEM_REF
851               && TREE_CODE (base2) == TARGET_MEM_REF
852               && (TMR_STEP (base1) == TMR_STEP (base2)
853                   || (TMR_STEP (base1) && TMR_STEP (base2)
854                       && operand_equal_p (TMR_STEP (base1),
855                                           TMR_STEP (base2), 0)))
856               && (TMR_INDEX (base1) == TMR_INDEX (base2)
857                   || (TMR_INDEX (base1) && TMR_INDEX (base2)
858                       && operand_equal_p (TMR_INDEX (base1),
859                                           TMR_INDEX (base2), 0)))
860               && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
861                   || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
862                       && operand_equal_p (TMR_INDEX2 (base1),
863                                           TMR_INDEX2 (base2), 0))))))
864     {
865       /* The offset embedded in MEM_REFs can be negative.  Bias them
866          so that the resulting offset adjustment is positive.  */
867       if (TREE_CODE (base1) == MEM_REF
868           || TREE_CODE (base1) == TARGET_MEM_REF)
869         {
870           double_int moff = mem_ref_offset (base1);
871           moff = double_int_lshift (moff,
872                                     BITS_PER_UNIT == 8
873                                     ? 3 : exact_log2 (BITS_PER_UNIT),
874                                     HOST_BITS_PER_DOUBLE_INT, true);
875           if (double_int_negative_p (moff))
876             offset2 += double_int_neg (moff).low;
877           else
878             offset1 += moff.low;
879         }
880       if (TREE_CODE (base2) == MEM_REF
881           || TREE_CODE (base2) == TARGET_MEM_REF)
882         {
883           double_int moff = mem_ref_offset (base2);
884           moff = double_int_lshift (moff,
885                                     BITS_PER_UNIT == 8
886                                     ? 3 : exact_log2 (BITS_PER_UNIT),
887                                     HOST_BITS_PER_DOUBLE_INT, true);
888           if (double_int_negative_p (moff))
889             offset1 += double_int_neg (moff).low;
890           else
891             offset2 += moff.low;
892         }
893       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
894     }
895   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
896     return false;
897
898   /* Disambiguations that rely on strict aliasing rules follow.  */
899   if (!flag_strict_aliasing || !tbaa_p)
900     return true;
901
902   if (TREE_CODE (base1) == MEM_REF)
903     ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
904   else if (TREE_CODE (base1) == TARGET_MEM_REF)
905     ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
906   else
907     ptrtype1 = TREE_TYPE (ptr1);
908   if (TREE_CODE (base2) == MEM_REF)
909     ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
910   else if (TREE_CODE (base2) == TARGET_MEM_REF)
911     ptrtype2 = TREE_TYPE (TMR_OFFSET (base2));
912   else
913     ptrtype2 = TREE_TYPE (ptr2);
914
915   /* If the alias set for a pointer access is zero all bets are off.  */
916   if (base1_alias_set == -1)
917     base1_alias_set = get_deref_alias_set (ptrtype1);
918   if (base1_alias_set == 0)
919     return true;
920   if (base2_alias_set == -1)
921     base2_alias_set = get_deref_alias_set (ptrtype2);
922   if (base2_alias_set == 0)
923     return true;
924
925   /* If both references are through the same type, they do not alias
926      if the accesses do not overlap.  This does extra disambiguation
927      for mixed/pointer accesses but requires strict aliasing.  */
928   if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
929       && (TREE_CODE (base2) != TARGET_MEM_REF || !TMR_INDEX (base2))
930       && (TREE_CODE (base1) != MEM_REF
931           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
932       && (TREE_CODE (base2) != MEM_REF
933           || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
934       && same_type_for_tbaa (TREE_TYPE (ptrtype1),
935                              TREE_TYPE (ptrtype2)) == 1)
936     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
937
938   /* Do type-based disambiguation.  */
939   if (base1_alias_set != base2_alias_set
940       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
941     return false;
942
943   /* Do access-path based disambiguation.  */
944   if (ref1 && ref2
945       && handled_component_p (ref1)
946       && handled_component_p (ref2)
947       && TREE_CODE (base1) != TARGET_MEM_REF
948       && TREE_CODE (base2) != TARGET_MEM_REF
949       && (TREE_CODE (base1) != MEM_REF
950           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
951       && (TREE_CODE (base2) != MEM_REF
952           || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
953     return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
954                                       ref1_alias_set, base1_alias_set,
955                                       offset1, max_size1,
956                                       ref2, TREE_TYPE (ptrtype2),
957                                       ref2_alias_set, base2_alias_set,
958                                       offset2, max_size2, false);
959
960   return true;
961 }
962
963 /* Return true, if the two memory references REF1 and REF2 may alias.  */
964
965 bool
966 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
967 {
968   tree base1, base2;
969   HOST_WIDE_INT offset1 = 0, offset2 = 0;
970   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
971   bool var1_p, var2_p, ind1_p, ind2_p;
972
973   gcc_checking_assert ((!ref1->ref
974                         || TREE_CODE (ref1->ref) == SSA_NAME
975                         || DECL_P (ref1->ref)
976                         || TREE_CODE (ref1->ref) == STRING_CST
977                         || handled_component_p (ref1->ref)
978                         || INDIRECT_REF_P (ref1->ref)
979                         || TREE_CODE (ref1->ref) == MEM_REF
980                         || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
981                        && (!ref2->ref
982                            || TREE_CODE (ref2->ref) == SSA_NAME
983                            || DECL_P (ref2->ref)
984                            || TREE_CODE (ref2->ref) == STRING_CST
985                            || handled_component_p (ref2->ref)
986                            || INDIRECT_REF_P (ref2->ref)
987                            || TREE_CODE (ref2->ref) == MEM_REF
988                            || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
989
990   /* Decompose the references into their base objects and the access.  */
991   base1 = ao_ref_base (ref1);
992   offset1 = ref1->offset;
993   max_size1 = ref1->max_size;
994   base2 = ao_ref_base (ref2);
995   offset2 = ref2->offset;
996   max_size2 = ref2->max_size;
997
998   /* We can end up with registers or constants as bases for example from
999      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1000      which is seen as a struct copy.  */
1001   if (TREE_CODE (base1) == SSA_NAME
1002       || TREE_CODE (base1) == CONST_DECL
1003       || TREE_CODE (base1) == CONSTRUCTOR
1004       || TREE_CODE (base1) == ADDR_EXPR
1005       || CONSTANT_CLASS_P (base1)
1006       || TREE_CODE (base2) == SSA_NAME
1007       || TREE_CODE (base2) == CONST_DECL
1008       || TREE_CODE (base2) == CONSTRUCTOR
1009       || TREE_CODE (base2) == ADDR_EXPR
1010       || CONSTANT_CLASS_P (base2))
1011     return false;
1012
1013   /* We can end up refering to code via function and label decls.
1014      As we likely do not properly track code aliases conservatively
1015      bail out.  */
1016   if (TREE_CODE (base1) == FUNCTION_DECL
1017       || TREE_CODE (base1) == LABEL_DECL
1018       || TREE_CODE (base2) == FUNCTION_DECL
1019       || TREE_CODE (base2) == LABEL_DECL)
1020     return true;
1021
1022   /* Defer to simple offset based disambiguation if we have
1023      references based on two decls.  Do this before defering to
1024      TBAA to handle must-alias cases in conformance with the
1025      GCC extension of allowing type-punning through unions.  */
1026   var1_p = SSA_VAR_P (base1);
1027   var2_p = SSA_VAR_P (base2);
1028   if (var1_p && var2_p)
1029     return decl_refs_may_alias_p (base1, offset1, max_size1,
1030                                   base2, offset2, max_size2);
1031
1032   ind1_p = (INDIRECT_REF_P (base1)
1033             || (TREE_CODE (base1) == MEM_REF)
1034             || (TREE_CODE (base1) == TARGET_MEM_REF));
1035   ind2_p = (INDIRECT_REF_P (base2)
1036             || (TREE_CODE (base2) == MEM_REF)
1037             || (TREE_CODE (base2) == TARGET_MEM_REF));
1038
1039   /* Canonicalize the pointer-vs-decl case.  */
1040   if (ind1_p && var2_p)
1041     {
1042       HOST_WIDE_INT tmp1;
1043       tree tmp2;
1044       ao_ref *tmp3;
1045       tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1046       tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1047       tmp2 = base1; base1 = base2; base2 = tmp2;
1048       tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1049       var1_p = true;
1050       ind1_p = false;
1051       var2_p = false;
1052       ind2_p = true;
1053     }
1054
1055   /* First defer to TBAA if possible.  */
1056   if (tbaa_p
1057       && flag_strict_aliasing
1058       && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1059                                  ao_ref_alias_set (ref2)))
1060     return false;
1061
1062   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
1063   if (var1_p && ind2_p)
1064     return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1065                                           offset2, max_size2,
1066                                           ao_ref_alias_set (ref2), -1,
1067                                           ref1->ref, base1,
1068                                           offset1, max_size1,
1069                                           ao_ref_alias_set (ref1),
1070                                           ao_ref_base_alias_set (ref1),
1071                                           tbaa_p);
1072   else if (ind1_p && ind2_p)
1073     return indirect_refs_may_alias_p (ref1->ref, base1,
1074                                       offset1, max_size1,
1075                                       ao_ref_alias_set (ref1), -1,
1076                                       ref2->ref, base2,
1077                                       offset2, max_size2,
1078                                       ao_ref_alias_set (ref2), -1,
1079                                       tbaa_p);
1080
1081   gcc_unreachable ();
1082 }
1083
1084 bool
1085 refs_may_alias_p (tree ref1, tree ref2)
1086 {
1087   ao_ref r1, r2;
1088   bool res;
1089   ao_ref_init (&r1, ref1);
1090   ao_ref_init (&r2, ref2);
1091   res = refs_may_alias_p_1 (&r1, &r2, true);
1092   if (res)
1093     ++alias_stats.refs_may_alias_p_may_alias;
1094   else
1095     ++alias_stats.refs_may_alias_p_no_alias;
1096   return res;
1097 }
1098
1099 /* Returns true if there is a anti-dependence for the STORE that
1100    executes after the LOAD.  */
1101
1102 bool
1103 refs_anti_dependent_p (tree load, tree store)
1104 {
1105   ao_ref r1, r2;
1106   ao_ref_init (&r1, load);
1107   ao_ref_init (&r2, store);
1108   return refs_may_alias_p_1 (&r1, &r2, false);
1109 }
1110
1111 /* Returns true if there is a output dependence for the stores
1112    STORE1 and STORE2.  */
1113
1114 bool
1115 refs_output_dependent_p (tree store1, tree store2)
1116 {
1117   ao_ref r1, r2;
1118   ao_ref_init (&r1, store1);
1119   ao_ref_init (&r2, store2);
1120   return refs_may_alias_p_1 (&r1, &r2, false);
1121 }
1122
1123 /* If the call CALL may use the memory reference REF return true,
1124    otherwise return false.  */
1125
1126 static bool
1127 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1128 {
1129   tree base, callee;
1130   unsigned i;
1131   int flags = gimple_call_flags (call);
1132
1133   /* Const functions without a static chain do not implicitly use memory.  */
1134   if (!gimple_call_chain (call)
1135       && (flags & (ECF_CONST|ECF_NOVOPS)))
1136     goto process_args;
1137
1138   base = ao_ref_base (ref);
1139   if (!base)
1140     return true;
1141
1142   /* If the reference is based on a decl that is not aliased the call
1143      cannot possibly use it.  */
1144   if (DECL_P (base)
1145       && !may_be_aliased (base)
1146       /* But local statics can be used through recursion.  */
1147       && !is_global_var (base))
1148     goto process_args;
1149
1150   callee = gimple_call_fndecl (call);
1151
1152   /* Handle those builtin functions explicitly that do not act as
1153      escape points.  See tree-ssa-structalias.c:find_func_aliases
1154      for the list of builtins we might need to handle here.  */
1155   if (callee != NULL_TREE
1156       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1157     switch (DECL_FUNCTION_CODE (callee))
1158       {
1159         /* All the following functions clobber memory pointed to by
1160            their first argument.  */
1161         case BUILT_IN_STRCPY:
1162         case BUILT_IN_STRNCPY:
1163         case BUILT_IN_MEMCPY:
1164         case BUILT_IN_MEMMOVE:
1165         case BUILT_IN_MEMPCPY:
1166         case BUILT_IN_STPCPY:
1167         case BUILT_IN_STPNCPY:
1168         case BUILT_IN_STRCAT:
1169         case BUILT_IN_STRNCAT:
1170           {
1171             ao_ref dref;
1172             tree size = NULL_TREE;
1173             if (gimple_call_num_args (call) == 3)
1174               size = gimple_call_arg (call, 2);
1175             ao_ref_init_from_ptr_and_size (&dref,
1176                                            gimple_call_arg (call, 1),
1177                                            size);
1178             return refs_may_alias_p_1 (&dref, ref, false);
1179           }
1180         case BUILT_IN_BCOPY:
1181           {
1182             ao_ref dref;
1183             tree size = gimple_call_arg (call, 2);
1184             ao_ref_init_from_ptr_and_size (&dref,
1185                                            gimple_call_arg (call, 0),
1186                                            size);
1187             return refs_may_alias_p_1 (&dref, ref, false);
1188           }
1189         /* The following builtins do not read from memory.  */
1190         case BUILT_IN_FREE:
1191         case BUILT_IN_MALLOC:
1192         case BUILT_IN_CALLOC:
1193         case BUILT_IN_MEMSET:
1194         case BUILT_IN_FREXP:
1195         case BUILT_IN_FREXPF:
1196         case BUILT_IN_FREXPL:
1197         case BUILT_IN_GAMMA_R:
1198         case BUILT_IN_GAMMAF_R:
1199         case BUILT_IN_GAMMAL_R:
1200         case BUILT_IN_LGAMMA_R:
1201         case BUILT_IN_LGAMMAF_R:
1202         case BUILT_IN_LGAMMAL_R:
1203         case BUILT_IN_MODF:
1204         case BUILT_IN_MODFF:
1205         case BUILT_IN_MODFL:
1206         case BUILT_IN_REMQUO:
1207         case BUILT_IN_REMQUOF:
1208         case BUILT_IN_REMQUOL:
1209         case BUILT_IN_SINCOS:
1210         case BUILT_IN_SINCOSF:
1211         case BUILT_IN_SINCOSL:
1212           return false;
1213         /* __sync_* builtins and some OpenMP builtins act as threading
1214            barriers.  */
1215 #undef DEF_SYNC_BUILTIN
1216 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1217 #include "sync-builtins.def"
1218 #undef DEF_SYNC_BUILTIN
1219         case BUILT_IN_GOMP_ATOMIC_START:
1220         case BUILT_IN_GOMP_ATOMIC_END:
1221         case BUILT_IN_GOMP_BARRIER:
1222         case BUILT_IN_GOMP_TASKWAIT:
1223         case BUILT_IN_GOMP_CRITICAL_START:
1224         case BUILT_IN_GOMP_CRITICAL_END:
1225         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1226         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1227         case BUILT_IN_GOMP_LOOP_END:
1228         case BUILT_IN_GOMP_ORDERED_START:
1229         case BUILT_IN_GOMP_ORDERED_END:
1230         case BUILT_IN_GOMP_PARALLEL_END:
1231         case BUILT_IN_GOMP_SECTIONS_END:
1232         case BUILT_IN_GOMP_SINGLE_COPY_START:
1233         case BUILT_IN_GOMP_SINGLE_COPY_END:
1234           return true;
1235
1236         default:
1237           /* Fallthru to general call handling.  */;
1238       }
1239
1240   /* Check if base is a global static variable that is not read
1241      by the function.  */
1242   if (TREE_CODE (base) == VAR_DECL
1243       && TREE_STATIC (base))
1244     {
1245       bitmap not_read;
1246
1247       if (callee != NULL_TREE
1248           && (not_read
1249                 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1250           && bitmap_bit_p (not_read, DECL_UID (base)))
1251         goto process_args;
1252     }
1253
1254   /* Check if the base variable is call-used.  */
1255   if (DECL_P (base))
1256     {
1257       if (pt_solution_includes (gimple_call_use_set (call), base))
1258         return true;
1259     }
1260   else if ((INDIRECT_REF_P (base)
1261             || TREE_CODE (base) == MEM_REF)
1262            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1263     {
1264       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1265       if (!pi)
1266         return true;
1267
1268       if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1269         return true;
1270     }
1271   else
1272     return true;
1273
1274   /* Inspect call arguments for passed-by-value aliases.  */
1275 process_args:
1276   for (i = 0; i < gimple_call_num_args (call); ++i)
1277     {
1278       tree op = gimple_call_arg (call, i);
1279       int flags = gimple_call_arg_flags (call, i);
1280
1281       if (flags & EAF_UNUSED)
1282         continue;
1283
1284       if (TREE_CODE (op) == WITH_SIZE_EXPR)
1285         op = TREE_OPERAND (op, 0);
1286
1287       if (TREE_CODE (op) != SSA_NAME
1288           && !is_gimple_min_invariant (op))
1289         {
1290           ao_ref r;
1291           ao_ref_init (&r, op);
1292           if (refs_may_alias_p_1 (&r, ref, true))
1293             return true;
1294         }
1295     }
1296
1297   return false;
1298 }
1299
1300 static bool
1301 ref_maybe_used_by_call_p (gimple call, tree ref)
1302 {
1303   ao_ref r;
1304   bool res;
1305   ao_ref_init (&r, ref);
1306   res = ref_maybe_used_by_call_p_1 (call, &r);
1307   if (res)
1308     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1309   else
1310     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1311   return res;
1312 }
1313
1314
1315 /* If the statement STMT may use the memory reference REF return
1316    true, otherwise return false.  */
1317
1318 bool
1319 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1320 {
1321   if (is_gimple_assign (stmt))
1322     {
1323       tree rhs;
1324
1325       /* All memory assign statements are single.  */
1326       if (!gimple_assign_single_p (stmt))
1327         return false;
1328
1329       rhs = gimple_assign_rhs1 (stmt);
1330       if (is_gimple_reg (rhs)
1331           || is_gimple_min_invariant (rhs)
1332           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1333         return false;
1334
1335       return refs_may_alias_p (rhs, ref);
1336     }
1337   else if (is_gimple_call (stmt))
1338     return ref_maybe_used_by_call_p (stmt, ref);
1339
1340   return true;
1341 }
1342
1343 /* If the call in statement CALL may clobber the memory reference REF
1344    return true, otherwise return false.  */
1345
1346 static bool
1347 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1348 {
1349   tree base;
1350   tree callee;
1351
1352   /* If the call is pure or const it cannot clobber anything.  */
1353   if (gimple_call_flags (call)
1354       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1355     return false;
1356
1357   base = ao_ref_base (ref);
1358   if (!base)
1359     return true;
1360
1361   if (TREE_CODE (base) == SSA_NAME
1362       || CONSTANT_CLASS_P (base))
1363     return false;
1364
1365   /* If the reference is based on a decl that is not aliased the call
1366      cannot possibly clobber it.  */
1367   if (DECL_P (base)
1368       && !may_be_aliased (base)
1369       /* But local non-readonly statics can be modified through recursion
1370          or the call may implement a threading barrier which we must
1371          treat as may-def.  */
1372       && (TREE_READONLY (base)
1373           || !is_global_var (base)))
1374     return false;
1375
1376   callee = gimple_call_fndecl (call);
1377
1378   /* Handle those builtin functions explicitly that do not act as
1379      escape points.  See tree-ssa-structalias.c:find_func_aliases
1380      for the list of builtins we might need to handle here.  */
1381   if (callee != NULL_TREE
1382       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1383     switch (DECL_FUNCTION_CODE (callee))
1384       {
1385         /* All the following functions clobber memory pointed to by
1386            their first argument.  */
1387         case BUILT_IN_STRCPY:
1388         case BUILT_IN_STRNCPY:
1389         case BUILT_IN_MEMCPY:
1390         case BUILT_IN_MEMMOVE:
1391         case BUILT_IN_MEMPCPY:
1392         case BUILT_IN_STPCPY:
1393         case BUILT_IN_STPNCPY:
1394         case BUILT_IN_STRCAT:
1395         case BUILT_IN_STRNCAT:
1396         case BUILT_IN_MEMSET:
1397           {
1398             ao_ref dref;
1399             tree size = NULL_TREE;
1400             if (gimple_call_num_args (call) == 3)
1401               size = gimple_call_arg (call, 2);
1402             ao_ref_init_from_ptr_and_size (&dref,
1403                                            gimple_call_arg (call, 0),
1404                                            size);
1405             return refs_may_alias_p_1 (&dref, ref, false);
1406           }
1407         case BUILT_IN_BCOPY:
1408           {
1409             ao_ref dref;
1410             tree size = gimple_call_arg (call, 2);
1411             ao_ref_init_from_ptr_and_size (&dref,
1412                                            gimple_call_arg (call, 1),
1413                                            size);
1414             return refs_may_alias_p_1 (&dref, ref, false);
1415           }
1416         /* Allocating memory does not have any side-effects apart from
1417            being the definition point for the pointer.  */
1418         case BUILT_IN_MALLOC:
1419         case BUILT_IN_CALLOC:
1420           /* Unix98 specifies that errno is set on allocation failure.
1421              Until we properly can track the errno location assume it
1422              is not a local decl but external or anonymous storage in
1423              a different translation unit.  Also assume it is of
1424              type int as required by the standard.  */
1425           if (flag_errno_math
1426               && TREE_TYPE (base) == integer_type_node)
1427             {
1428               struct ptr_info_def *pi;
1429               if (DECL_P (base)
1430                   && !TREE_STATIC (base))
1431                 return true;
1432               else if ((INDIRECT_REF_P (base)
1433                         || TREE_CODE (base) == MEM_REF)
1434                        && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1435                        && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1436                 return pi->pt.anything || pi->pt.nonlocal;
1437             }
1438           return false;
1439         /* Freeing memory kills the pointed-to memory.  More importantly
1440            the call has to serve as a barrier for moving loads and stores
1441            across it.  */
1442         case BUILT_IN_FREE:
1443           {
1444             tree ptr = gimple_call_arg (call, 0);
1445             return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1446           }
1447         case BUILT_IN_GAMMA_R:
1448         case BUILT_IN_GAMMAF_R:
1449         case BUILT_IN_GAMMAL_R:
1450         case BUILT_IN_LGAMMA_R:
1451         case BUILT_IN_LGAMMAF_R:
1452         case BUILT_IN_LGAMMAL_R:
1453           {
1454             tree out = gimple_call_arg (call, 1);
1455             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1456               return true;
1457             if (flag_errno_math)
1458               break;
1459             return false;
1460           }
1461         case BUILT_IN_FREXP:
1462         case BUILT_IN_FREXPF:
1463         case BUILT_IN_FREXPL:
1464         case BUILT_IN_MODF:
1465         case BUILT_IN_MODFF:
1466         case BUILT_IN_MODFL:
1467           {
1468             tree out = gimple_call_arg (call, 1);
1469             return ptr_deref_may_alias_ref_p_1 (out, ref);
1470           }
1471         case BUILT_IN_REMQUO:
1472         case BUILT_IN_REMQUOF:
1473         case BUILT_IN_REMQUOL:
1474           {
1475             tree out = gimple_call_arg (call, 2);
1476             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1477               return true;
1478             if (flag_errno_math)
1479               break;
1480             return false;
1481           }
1482         case BUILT_IN_SINCOS:
1483         case BUILT_IN_SINCOSF:
1484         case BUILT_IN_SINCOSL:
1485           {
1486             tree sin = gimple_call_arg (call, 1);
1487             tree cos = gimple_call_arg (call, 2);
1488             return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1489                     || ptr_deref_may_alias_ref_p_1 (cos, ref));
1490           }
1491         /* __sync_* builtins and some OpenMP builtins act as threading
1492            barriers.  */
1493 #undef DEF_SYNC_BUILTIN
1494 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1495 #include "sync-builtins.def"
1496 #undef DEF_SYNC_BUILTIN
1497         case BUILT_IN_GOMP_ATOMIC_START:
1498         case BUILT_IN_GOMP_ATOMIC_END:
1499         case BUILT_IN_GOMP_BARRIER:
1500         case BUILT_IN_GOMP_TASKWAIT:
1501         case BUILT_IN_GOMP_CRITICAL_START:
1502         case BUILT_IN_GOMP_CRITICAL_END:
1503         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1504         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1505         case BUILT_IN_GOMP_LOOP_END:
1506         case BUILT_IN_GOMP_ORDERED_START:
1507         case BUILT_IN_GOMP_ORDERED_END:
1508         case BUILT_IN_GOMP_PARALLEL_END:
1509         case BUILT_IN_GOMP_SECTIONS_END:
1510         case BUILT_IN_GOMP_SINGLE_COPY_START:
1511         case BUILT_IN_GOMP_SINGLE_COPY_END:
1512           return true;
1513         default:
1514           /* Fallthru to general call handling.  */;
1515       }
1516
1517   /* Check if base is a global static variable that is not written
1518      by the function.  */
1519   if (callee != NULL_TREE
1520       && TREE_CODE (base) == VAR_DECL
1521       && TREE_STATIC (base))
1522     {
1523       bitmap not_written;
1524
1525       if ((not_written
1526              = ipa_reference_get_not_written_global (cgraph_node (callee)))
1527           && bitmap_bit_p (not_written, DECL_UID (base)))
1528         return false;
1529     }
1530
1531   /* Check if the base variable is call-clobbered.  */
1532   if (DECL_P (base))
1533     return pt_solution_includes (gimple_call_clobber_set (call), base);
1534   else if ((INDIRECT_REF_P (base)
1535             || TREE_CODE (base) == MEM_REF)
1536            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1537     {
1538       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1539       if (!pi)
1540         return true;
1541
1542       return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1543     }
1544
1545   return true;
1546 }
1547
1548 /* If the call in statement CALL may clobber the memory reference REF
1549    return true, otherwise return false.  */
1550
1551 bool
1552 call_may_clobber_ref_p (gimple call, tree ref)
1553 {
1554   bool res;
1555   ao_ref r;
1556   ao_ref_init (&r, ref);
1557   res = call_may_clobber_ref_p_1 (call, &r);
1558   if (res)
1559     ++alias_stats.call_may_clobber_ref_p_may_alias;
1560   else
1561     ++alias_stats.call_may_clobber_ref_p_no_alias;
1562   return res;
1563 }
1564
1565
1566 /* If the statement STMT may clobber the memory reference REF return true,
1567    otherwise return false.  */
1568
1569 bool
1570 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1571 {
1572   if (is_gimple_call (stmt))
1573     {
1574       tree lhs = gimple_call_lhs (stmt);
1575       if (lhs
1576           && TREE_CODE (lhs) != SSA_NAME)
1577         {
1578           ao_ref r;
1579           ao_ref_init (&r, lhs);
1580           if (refs_may_alias_p_1 (ref, &r, true))
1581             return true;
1582         }
1583
1584       return call_may_clobber_ref_p_1 (stmt, ref);
1585     }
1586   else if (gimple_assign_single_p (stmt))
1587     {
1588       tree lhs = gimple_assign_lhs (stmt);
1589       if (TREE_CODE (lhs) != SSA_NAME)
1590         {
1591           ao_ref r;
1592           ao_ref_init (&r, lhs);
1593           return refs_may_alias_p_1 (ref, &r, true);
1594         }
1595     }
1596   else if (gimple_code (stmt) == GIMPLE_ASM)
1597     return true;
1598
1599   return false;
1600 }
1601
1602 bool
1603 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1604 {
1605   ao_ref r;
1606   ao_ref_init (&r, ref);
1607   return stmt_may_clobber_ref_p_1 (stmt, &r);
1608 }
1609
1610 /* If STMT kills the memory reference REF return true, otherwise
1611    return false.  */
1612
1613 static bool
1614 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1615 {
1616   if (gimple_has_lhs (stmt)
1617       && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
1618     {
1619       tree base, lhs = gimple_get_lhs (stmt);
1620       HOST_WIDE_INT size, offset, max_size;
1621       ao_ref_base (ref);
1622       base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1623       /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1624          so base == ref->base does not always hold.  */
1625       if (base == ref->base)
1626         {
1627           /* For a must-alias check we need to be able to constrain
1628              the accesses properly.  */
1629           if (size != -1 && size == max_size
1630               && ref->max_size != -1)
1631             {
1632               if (offset <= ref->offset
1633                   && offset + size >= ref->offset + ref->max_size)
1634                 return true;
1635             }
1636         }
1637     }
1638   return false;
1639 }
1640
1641 bool
1642 stmt_kills_ref_p (gimple stmt, tree ref)
1643 {
1644   ao_ref r;
1645   ao_ref_init (&r, ref);
1646   return stmt_kills_ref_p_1 (stmt, &r);
1647 }
1648
1649
1650 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1651    TARGET or a statement clobbering the memory reference REF in which
1652    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
1653
1654 static bool
1655 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1656                   tree vuse, bitmap *visited)
1657 {
1658   if (!*visited)
1659     *visited = BITMAP_ALLOC (NULL);
1660
1661   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1662
1663   /* Walk until we hit the target.  */
1664   while (vuse != target)
1665     {
1666       gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1667       /* Recurse for PHI nodes.  */
1668       if (gimple_code (def_stmt) == GIMPLE_PHI)
1669         {
1670           /* An already visited PHI node ends the walk successfully.  */
1671           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1672             return true;
1673           vuse = get_continuation_for_phi (def_stmt, ref, visited);
1674           if (!vuse)
1675             return false;
1676           continue;
1677         }
1678       /* A clobbering statement or the end of the IL ends it failing.  */
1679       else if (gimple_nop_p (def_stmt)
1680                || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1681         return false;
1682       vuse = gimple_vuse (def_stmt);
1683     }
1684   return true;
1685 }
1686
1687 /* Starting from a PHI node for the virtual operand of the memory reference
1688    REF find a continuation virtual operand that allows to continue walking
1689    statements dominating PHI skipping only statements that cannot possibly
1690    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
1691    be found.  */
1692
1693 tree
1694 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1695 {
1696   unsigned nargs = gimple_phi_num_args (phi);
1697
1698   /* Through a single-argument PHI we can simply look through.  */
1699   if (nargs == 1)
1700     return PHI_ARG_DEF (phi, 0);
1701
1702   /* For two arguments try to skip non-aliasing code until we hit
1703      the phi argument definition that dominates the other one.  */
1704   if (nargs == 2)
1705     {
1706       tree arg0 = PHI_ARG_DEF (phi, 0);
1707       tree arg1 = PHI_ARG_DEF (phi, 1);
1708       gimple def0 = SSA_NAME_DEF_STMT (arg0);
1709       gimple def1 = SSA_NAME_DEF_STMT (arg1);
1710       tree common_vuse;
1711
1712       if (arg0 == arg1)
1713         return arg0;
1714       else if (gimple_nop_p (def0)
1715                || (!gimple_nop_p (def1)
1716                    && dominated_by_p (CDI_DOMINATORS,
1717                                       gimple_bb (def1), gimple_bb (def0))))
1718         {
1719           if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1720             return arg0;
1721         }
1722       else if (gimple_nop_p (def1)
1723                || dominated_by_p (CDI_DOMINATORS,
1724                                   gimple_bb (def0), gimple_bb (def1)))
1725         {
1726           if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1727             return arg1;
1728         }
1729       /* Special case of a diamond:
1730            MEM_1 = ...
1731            goto (cond) ? L1 : L2
1732            L1: store1 = ...    #MEM_2 = vuse(MEM_1)
1733                goto L3
1734            L2: store2 = ...    #MEM_3 = vuse(MEM_1)
1735            L3: MEM_4 = PHI<MEM_2, MEM_3>
1736          We were called with the PHI at L3, MEM_2 and MEM_3 don't
1737          dominate each other, but still we can easily skip this PHI node
1738          if we recognize that the vuse MEM operand is the same for both,
1739          and that we can skip both statements (they don't clobber us).
1740          This is still linear.  Don't use maybe_skip_until, that might
1741          potentially be slow.  */
1742       else if ((common_vuse = gimple_vuse (def0))
1743                && common_vuse == gimple_vuse (def1))
1744         {
1745           if (!stmt_may_clobber_ref_p_1 (def0, ref)
1746               && !stmt_may_clobber_ref_p_1 (def1, ref))
1747             return common_vuse;
1748         }
1749     }
1750
1751   return NULL_TREE;
1752 }
1753
1754 /* Based on the memory reference REF and its virtual use VUSE call
1755    WALKER for each virtual use that is equivalent to VUSE, including VUSE
1756    itself.  That is, for each virtual use for which its defining statement
1757    does not clobber REF.
1758
1759    WALKER is called with REF, the current virtual use and DATA.  If
1760    WALKER returns non-NULL the walk stops and its result is returned.
1761    At the end of a non-successful walk NULL is returned.
1762
1763    TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1764    use which definition is a statement that may clobber REF and DATA.
1765    If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1766    If TRANSLATE returns non-NULL the walk stops and its result is returned.
1767    If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1768    to adjust REF and *DATA to make that valid.
1769
1770    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
1771
1772 void *
1773 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1774                         void *(*walker)(ao_ref *, tree, void *),
1775                         void *(*translate)(ao_ref *, tree, void *), void *data)
1776 {
1777   bitmap visited = NULL;
1778   void *res;
1779
1780   timevar_push (TV_ALIAS_STMT_WALK);
1781
1782   do
1783     {
1784       gimple def_stmt;
1785
1786       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1787       res = (*walker) (ref, vuse, data);
1788       if (res)
1789         break;
1790
1791       def_stmt = SSA_NAME_DEF_STMT (vuse);
1792       if (gimple_nop_p (def_stmt))
1793         break;
1794       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1795         vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1796       else
1797         {
1798           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1799             {
1800               if (!translate)
1801                 break;
1802               res = (*translate) (ref, vuse, data);
1803               /* Failed lookup and translation.  */
1804               if (res == (void *)-1)
1805                 {
1806                   res = NULL;
1807                   break;
1808                 }
1809               /* Lookup succeeded.  */
1810               else if (res != NULL)
1811                 break;
1812               /* Translation succeeded, continue walking.  */
1813             }
1814           vuse = gimple_vuse (def_stmt);
1815         }
1816     }
1817   while (vuse);
1818
1819   if (visited)
1820     BITMAP_FREE (visited);
1821
1822   timevar_pop (TV_ALIAS_STMT_WALK);
1823
1824   return res;
1825 }
1826
1827
1828 /* Based on the memory reference REF call WALKER for each vdef which
1829    defining statement may clobber REF, starting with VDEF.  If REF
1830    is NULL_TREE, each defining statement is visited.
1831
1832    WALKER is called with REF, the current vdef and DATA.  If WALKER
1833    returns true the walk is stopped, otherwise it continues.
1834
1835    At PHI nodes walk_aliased_vdefs forks into one walk for reach
1836    PHI argument (but only one walk continues on merge points), the
1837    return value is true if any of the walks was successful.
1838
1839    The function returns the number of statements walked.  */
1840
1841 static unsigned int
1842 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1843                       bool (*walker)(ao_ref *, tree, void *), void *data,
1844                       bitmap *visited, unsigned int cnt)
1845 {
1846   do
1847     {
1848       gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1849
1850       if (*visited
1851           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1852         return cnt;
1853
1854       if (gimple_nop_p (def_stmt))
1855         return cnt;
1856       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1857         {
1858           unsigned i;
1859           if (!*visited)
1860             *visited = BITMAP_ALLOC (NULL);
1861           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1862             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1863                                          walker, data, visited, 0);
1864           return cnt;
1865         }
1866
1867       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1868       cnt++;
1869       if ((!ref
1870            || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1871           && (*walker) (ref, vdef, data))
1872         return cnt;
1873
1874       vdef = gimple_vuse (def_stmt);
1875     }
1876   while (1);
1877 }
1878
1879 unsigned int
1880 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1881                     bool (*walker)(ao_ref *, tree, void *), void *data,
1882                     bitmap *visited)
1883 {
1884   bitmap local_visited = NULL;
1885   unsigned int ret;
1886
1887   timevar_push (TV_ALIAS_STMT_WALK);
1888
1889   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1890                               visited ? visited : &local_visited, 0);
1891   if (local_visited)
1892     BITMAP_FREE (local_visited);
1893
1894   timevar_pop (TV_ALIAS_STMT_WALK);
1895
1896   return ret;
1897 }
1898