OSDN Git Service

2011-04-21 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,
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,
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 base1, base2;
613   tree type1, type2;
614   tree *refp;
615   int same_p;
616
617   /* Choose bases and base types to search for.  */
618   base1 = ref1;
619   while (handled_component_p (base1))
620     base1 = TREE_OPERAND (base1, 0);
621   type1 = TREE_TYPE (base1);
622   base2 = ref2;
623   while (handled_component_p (base2))
624     base2 = TREE_OPERAND (base2, 0);
625   type2 = TREE_TYPE (base2);
626
627   /* Now search for the type1 in the access path of ref2.  This
628      would be a common base for doing offset based disambiguation on.  */
629   refp = &ref2;
630   while (handled_component_p (*refp)
631          && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
632     refp = &TREE_OPERAND (*refp, 0);
633   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
634   /* If we couldn't compare types we have to bail out.  */
635   if (same_p == -1)
636     return true;
637   else if (same_p == 1)
638     {
639       HOST_WIDE_INT offadj, sztmp, msztmp;
640       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
641       offset2 -= offadj;
642       get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
643       offset1 -= offadj;
644       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
645     }
646   /* If we didn't find a common base, try the other way around.  */
647   refp = &ref1;
648   while (handled_component_p (*refp)
649          && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
650     refp = &TREE_OPERAND (*refp, 0);
651   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
652   /* If we couldn't compare types we have to bail out.  */
653   if (same_p == -1)
654     return true;
655   else if (same_p == 1)
656     {
657       HOST_WIDE_INT offadj, sztmp, msztmp;
658       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
659       offset1 -= offadj;
660       get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
661       offset2 -= offadj;
662       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
663     }
664
665   /* If we have two type access paths B1.path1 and B2.path2 they may
666      only alias if either B1 is in B2.path2 or B2 is in B1.path1.
667      But we can still have a path that goes B1.path1...B2.path2 with
668      a part that we do not see.  So we can only disambiguate now
669      if there is no B2 in the tail of path1 and no B1 on the
670      tail of path2.  */
671   if (base1_alias_set == ref2_alias_set
672       || alias_set_subset_of (base1_alias_set, ref2_alias_set))
673     return true;
674   /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
675   if (!ref2_is_decl)
676     return (base2_alias_set == ref1_alias_set
677             || alias_set_subset_of (base2_alias_set, ref1_alias_set));
678   return false;
679 }
680
681 /* Return true if two memory references based on the variables BASE1
682    and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
683    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  */
684
685 static bool
686 decl_refs_may_alias_p (tree base1,
687                        HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
688                        tree base2,
689                        HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
690 {
691   gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
692
693   /* If both references are based on different variables, they cannot alias.  */
694   if (base1 != base2)
695     return false;
696
697   /* If both references are based on the same variable, they cannot alias if
698      the accesses do not overlap.  */
699   return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
700 }
701
702 /* Return true if an indirect reference based on *PTR1 constrained
703    to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
704    constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
705    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
706    in which case they are computed on-demand.  REF1 and REF2
707    if non-NULL are the complete memory reference trees.  */
708
709 static bool
710 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
711                                HOST_WIDE_INT offset1,
712                                HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
713                                alias_set_type ref1_alias_set,
714                                alias_set_type base1_alias_set,
715                                tree ref2 ATTRIBUTE_UNUSED, tree base2,
716                                HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
717                                alias_set_type ref2_alias_set,
718                                alias_set_type base2_alias_set, bool tbaa_p)
719 {
720   tree ptr1;
721   tree ptrtype1;
722   HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
723
724   ptr1 = TREE_OPERAND (base1, 0);
725
726   /* The offset embedded in MEM_REFs can be negative.  Bias them
727      so that the resulting offset adjustment is positive.  */
728   if (TREE_CODE (base1) == MEM_REF
729       || TREE_CODE (base1) == TARGET_MEM_REF)
730     {
731       double_int moff = mem_ref_offset (base1);
732       moff = double_int_lshift (moff,
733                                 BITS_PER_UNIT == 8
734                                 ? 3 : exact_log2 (BITS_PER_UNIT),
735                                 HOST_BITS_PER_DOUBLE_INT, true);
736       if (double_int_negative_p (moff))
737         offset2p += double_int_neg (moff).low;
738       else
739         offset1p += moff.low;
740     }
741
742   /* If only one reference is based on a variable, they cannot alias if
743      the pointer access is beyond the extent of the variable access.
744      (the pointer base cannot validly point to an offset less than zero
745      of the variable).
746      They also cannot alias if the pointer may not point to the decl.  */
747   if ((TREE_CODE (base1) != TARGET_MEM_REF
748        || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
749       && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
750     return false;
751   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
752     return false;
753
754   /* Disambiguations that rely on strict aliasing rules follow.  */
755   if (!flag_strict_aliasing || !tbaa_p)
756     return true;
757
758   if (TREE_CODE (base1) == MEM_REF)
759     ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
760   else if (TREE_CODE (base1) == TARGET_MEM_REF)
761     ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
762   else
763     ptrtype1 = TREE_TYPE (ptr1);
764
765   /* If the alias set for a pointer access is zero all bets are off.  */
766   if (base1_alias_set == -1)
767     base1_alias_set = get_deref_alias_set (ptrtype1);
768   if (base1_alias_set == 0)
769     return true;
770   if (base2_alias_set == -1)
771     base2_alias_set = get_alias_set (base2);
772
773   /* If both references are through the same type, they do not alias
774      if the accesses do not overlap.  This does extra disambiguation
775      for mixed/pointer accesses but requires strict aliasing.
776      For MEM_REFs we require that the component-ref offset we computed
777      is relative to the start of the type which we ensure by
778      comparing rvalue and access type and disregarding the constant
779      pointer offset.  */
780   if ((TREE_CODE (base1) != TARGET_MEM_REF
781        || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
782       && (TREE_CODE (base1) != MEM_REF
783           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
784       && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
785     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
786
787   /* When we are trying to disambiguate an access with a pointer dereference
788      as base versus one with a decl as base we can use both the size
789      of the decl and its dynamic type for extra disambiguation.
790      ???  We do not know anything about the dynamic type of the decl
791      other than that its alias-set contains base2_alias_set as a subset
792      which does not help us here.  */
793   /* As we know nothing useful about the dynamic type of the decl just
794      use the usual conflict check rather than a subset test.
795      ???  We could introduce -fvery-strict-aliasing when the language
796      does not allow decls to have a dynamic type that differs from their
797      static type.  Then we can check 
798      !alias_set_subset_of (base1_alias_set, base2_alias_set) instead.  */
799   if (base1_alias_set != base2_alias_set
800       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
801     return false;
802   /* If the size of the access relevant for TBAA through the pointer
803      is bigger than the size of the decl we can't possibly access the
804      decl via that pointer.  */
805   if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
806       && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
807       && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
808       /* ???  This in turn may run afoul when a decl of type T which is
809          a member of union type U is accessed through a pointer to
810          type U and sizeof T is smaller than sizeof U.  */
811       && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
812       && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
813       && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
814     return false;
815
816   /* Do access-path based disambiguation.  */
817   if (ref1 && ref2
818       && handled_component_p (ref1)
819       && handled_component_p (ref2)
820       && TREE_CODE (base1) != TARGET_MEM_REF
821       && (TREE_CODE (base1) != MEM_REF
822           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
823     return aliasing_component_refs_p (ref1,
824                                       ref1_alias_set, base1_alias_set,
825                                       offset1, max_size1,
826                                       ref2,
827                                       ref2_alias_set, base2_alias_set,
828                                       offset2, max_size2, true);
829
830   return true;
831 }
832
833 /* Return true if two indirect references based on *PTR1
834    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
835    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
836    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
837    in which case they are computed on-demand.  REF1 and REF2
838    if non-NULL are the complete memory reference trees. */
839
840 static bool
841 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
842                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
843                            alias_set_type ref1_alias_set,
844                            alias_set_type base1_alias_set,
845                            tree ref2 ATTRIBUTE_UNUSED, tree base2,
846                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
847                            alias_set_type ref2_alias_set,
848                            alias_set_type base2_alias_set, bool tbaa_p)
849 {
850   tree ptr1;
851   tree ptr2;
852   tree ptrtype1, ptrtype2;
853
854   ptr1 = TREE_OPERAND (base1, 0);
855   ptr2 = TREE_OPERAND (base2, 0);
856
857   /* If both bases are based on pointers they cannot alias if they may not
858      point to the same memory object or if they point to the same object
859      and the accesses do not overlap.  */
860   if ((!cfun || gimple_in_ssa_p (cfun))
861       && operand_equal_p (ptr1, ptr2, 0)
862       && (((TREE_CODE (base1) != TARGET_MEM_REF
863             || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
864            && (TREE_CODE (base2) != TARGET_MEM_REF
865                || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
866           || (TREE_CODE (base1) == TARGET_MEM_REF
867               && TREE_CODE (base2) == TARGET_MEM_REF
868               && (TMR_STEP (base1) == TMR_STEP (base2)
869                   || (TMR_STEP (base1) && TMR_STEP (base2)
870                       && operand_equal_p (TMR_STEP (base1),
871                                           TMR_STEP (base2), 0)))
872               && (TMR_INDEX (base1) == TMR_INDEX (base2)
873                   || (TMR_INDEX (base1) && TMR_INDEX (base2)
874                       && operand_equal_p (TMR_INDEX (base1),
875                                           TMR_INDEX (base2), 0)))
876               && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
877                   || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
878                       && operand_equal_p (TMR_INDEX2 (base1),
879                                           TMR_INDEX2 (base2), 0))))))
880     {
881       /* The offset embedded in MEM_REFs can be negative.  Bias them
882          so that the resulting offset adjustment is positive.  */
883       if (TREE_CODE (base1) == MEM_REF
884           || TREE_CODE (base1) == TARGET_MEM_REF)
885         {
886           double_int moff = mem_ref_offset (base1);
887           moff = double_int_lshift (moff,
888                                     BITS_PER_UNIT == 8
889                                     ? 3 : exact_log2 (BITS_PER_UNIT),
890                                     HOST_BITS_PER_DOUBLE_INT, true);
891           if (double_int_negative_p (moff))
892             offset2 += double_int_neg (moff).low;
893           else
894             offset1 += moff.low;
895         }
896       if (TREE_CODE (base2) == MEM_REF
897           || TREE_CODE (base2) == TARGET_MEM_REF)
898         {
899           double_int moff = mem_ref_offset (base2);
900           moff = double_int_lshift (moff,
901                                     BITS_PER_UNIT == 8
902                                     ? 3 : exact_log2 (BITS_PER_UNIT),
903                                     HOST_BITS_PER_DOUBLE_INT, true);
904           if (double_int_negative_p (moff))
905             offset1 += double_int_neg (moff).low;
906           else
907             offset2 += moff.low;
908         }
909       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
910     }
911   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
912     return false;
913
914   /* Disambiguations that rely on strict aliasing rules follow.  */
915   if (!flag_strict_aliasing || !tbaa_p)
916     return true;
917
918   if (TREE_CODE (base1) == MEM_REF)
919     ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
920   else if (TREE_CODE (base1) == TARGET_MEM_REF)
921     ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
922   else
923     ptrtype1 = TREE_TYPE (ptr1);
924   if (TREE_CODE (base2) == MEM_REF)
925     ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
926   else if (TREE_CODE (base2) == TARGET_MEM_REF)
927     ptrtype2 = TREE_TYPE (TMR_OFFSET (base2));
928   else
929     ptrtype2 = TREE_TYPE (ptr2);
930
931   /* If the alias set for a pointer access is zero all bets are off.  */
932   if (base1_alias_set == -1)
933     base1_alias_set = get_deref_alias_set (ptrtype1);
934   if (base1_alias_set == 0)
935     return true;
936   if (base2_alias_set == -1)
937     base2_alias_set = get_deref_alias_set (ptrtype2);
938   if (base2_alias_set == 0)
939     return true;
940
941   /* If both references are through the same type, they do not alias
942      if the accesses do not overlap.  This does extra disambiguation
943      for mixed/pointer accesses but requires strict aliasing.  */
944   if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
945       && (TREE_CODE (base2) != TARGET_MEM_REF || !TMR_INDEX (base2))
946       && (TREE_CODE (base1) != MEM_REF
947           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
948       && (TREE_CODE (base2) != MEM_REF
949           || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
950       && same_type_for_tbaa (TREE_TYPE (ptrtype1),
951                              TREE_TYPE (ptrtype2)) == 1)
952     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
953
954   /* Do type-based disambiguation.  */
955   if (base1_alias_set != base2_alias_set
956       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
957     return false;
958
959   /* Do access-path based disambiguation.  */
960   if (ref1 && ref2
961       && handled_component_p (ref1)
962       && handled_component_p (ref2)
963       && TREE_CODE (base1) != TARGET_MEM_REF
964       && TREE_CODE (base2) != TARGET_MEM_REF
965       && (TREE_CODE (base1) != MEM_REF
966           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
967       && (TREE_CODE (base2) != MEM_REF
968           || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
969     return aliasing_component_refs_p (ref1,
970                                       ref1_alias_set, base1_alias_set,
971                                       offset1, max_size1,
972                                       ref2,
973                                       ref2_alias_set, base2_alias_set,
974                                       offset2, max_size2, false);
975
976   return true;
977 }
978
979 /* Return true, if the two memory references REF1 and REF2 may alias.  */
980
981 bool
982 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
983 {
984   tree base1, base2;
985   HOST_WIDE_INT offset1 = 0, offset2 = 0;
986   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
987   bool var1_p, var2_p, ind1_p, ind2_p;
988
989   gcc_checking_assert ((!ref1->ref
990                         || TREE_CODE (ref1->ref) == SSA_NAME
991                         || DECL_P (ref1->ref)
992                         || TREE_CODE (ref1->ref) == STRING_CST
993                         || handled_component_p (ref1->ref)
994                         || INDIRECT_REF_P (ref1->ref)
995                         || TREE_CODE (ref1->ref) == MEM_REF
996                         || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
997                        && (!ref2->ref
998                            || TREE_CODE (ref2->ref) == SSA_NAME
999                            || DECL_P (ref2->ref)
1000                            || TREE_CODE (ref2->ref) == STRING_CST
1001                            || handled_component_p (ref2->ref)
1002                            || INDIRECT_REF_P (ref2->ref)
1003                            || TREE_CODE (ref2->ref) == MEM_REF
1004                            || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1005
1006   /* Decompose the references into their base objects and the access.  */
1007   base1 = ao_ref_base (ref1);
1008   offset1 = ref1->offset;
1009   max_size1 = ref1->max_size;
1010   base2 = ao_ref_base (ref2);
1011   offset2 = ref2->offset;
1012   max_size2 = ref2->max_size;
1013
1014   /* We can end up with registers or constants as bases for example from
1015      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1016      which is seen as a struct copy.  */
1017   if (TREE_CODE (base1) == SSA_NAME
1018       || TREE_CODE (base1) == CONST_DECL
1019       || TREE_CODE (base1) == CONSTRUCTOR
1020       || TREE_CODE (base1) == ADDR_EXPR
1021       || CONSTANT_CLASS_P (base1)
1022       || TREE_CODE (base2) == SSA_NAME
1023       || TREE_CODE (base2) == CONST_DECL
1024       || TREE_CODE (base2) == CONSTRUCTOR
1025       || TREE_CODE (base2) == ADDR_EXPR
1026       || CONSTANT_CLASS_P (base2))
1027     return false;
1028
1029   /* We can end up refering to code via function and label decls.
1030      As we likely do not properly track code aliases conservatively
1031      bail out.  */
1032   if (TREE_CODE (base1) == FUNCTION_DECL
1033       || TREE_CODE (base1) == LABEL_DECL
1034       || TREE_CODE (base2) == FUNCTION_DECL
1035       || TREE_CODE (base2) == LABEL_DECL)
1036     return true;
1037
1038   /* Defer to simple offset based disambiguation if we have
1039      references based on two decls.  Do this before defering to
1040      TBAA to handle must-alias cases in conformance with the
1041      GCC extension of allowing type-punning through unions.  */
1042   var1_p = SSA_VAR_P (base1);
1043   var2_p = SSA_VAR_P (base2);
1044   if (var1_p && var2_p)
1045     return decl_refs_may_alias_p (base1, offset1, max_size1,
1046                                   base2, offset2, max_size2);
1047
1048   ind1_p = (INDIRECT_REF_P (base1)
1049             || (TREE_CODE (base1) == MEM_REF)
1050             || (TREE_CODE (base1) == TARGET_MEM_REF));
1051   ind2_p = (INDIRECT_REF_P (base2)
1052             || (TREE_CODE (base2) == MEM_REF)
1053             || (TREE_CODE (base2) == TARGET_MEM_REF));
1054
1055   /* Canonicalize the pointer-vs-decl case.  */
1056   if (ind1_p && var2_p)
1057     {
1058       HOST_WIDE_INT tmp1;
1059       tree tmp2;
1060       ao_ref *tmp3;
1061       tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1062       tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1063       tmp2 = base1; base1 = base2; base2 = tmp2;
1064       tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1065       var1_p = true;
1066       ind1_p = false;
1067       var2_p = false;
1068       ind2_p = true;
1069     }
1070
1071   /* First defer to TBAA if possible.  */
1072   if (tbaa_p
1073       && flag_strict_aliasing
1074       && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1075                                  ao_ref_alias_set (ref2)))
1076     return false;
1077
1078   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
1079   if (var1_p && ind2_p)
1080     return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1081                                           offset2, max_size2,
1082                                           ao_ref_alias_set (ref2), -1,
1083                                           ref1->ref, base1,
1084                                           offset1, max_size1,
1085                                           ao_ref_alias_set (ref1),
1086                                           ao_ref_base_alias_set (ref1),
1087                                           tbaa_p);
1088   else if (ind1_p && ind2_p)
1089     return indirect_refs_may_alias_p (ref1->ref, base1,
1090                                       offset1, max_size1,
1091                                       ao_ref_alias_set (ref1), -1,
1092                                       ref2->ref, base2,
1093                                       offset2, max_size2,
1094                                       ao_ref_alias_set (ref2), -1,
1095                                       tbaa_p);
1096
1097   /* We really do not want to end up here, but returning true is safe.  */
1098 #ifdef ENABLE_CHECKING
1099   gcc_unreachable ();
1100 #else
1101   return true;
1102 #endif
1103 }
1104
1105 bool
1106 refs_may_alias_p (tree ref1, tree ref2)
1107 {
1108   ao_ref r1, r2;
1109   bool res;
1110   ao_ref_init (&r1, ref1);
1111   ao_ref_init (&r2, ref2);
1112   res = refs_may_alias_p_1 (&r1, &r2, true);
1113   if (res)
1114     ++alias_stats.refs_may_alias_p_may_alias;
1115   else
1116     ++alias_stats.refs_may_alias_p_no_alias;
1117   return res;
1118 }
1119
1120 /* Returns true if there is a anti-dependence for the STORE that
1121    executes after the LOAD.  */
1122
1123 bool
1124 refs_anti_dependent_p (tree load, tree store)
1125 {
1126   ao_ref r1, r2;
1127   ao_ref_init (&r1, load);
1128   ao_ref_init (&r2, store);
1129   return refs_may_alias_p_1 (&r1, &r2, false);
1130 }
1131
1132 /* Returns true if there is a output dependence for the stores
1133    STORE1 and STORE2.  */
1134
1135 bool
1136 refs_output_dependent_p (tree store1, tree store2)
1137 {
1138   ao_ref r1, r2;
1139   ao_ref_init (&r1, store1);
1140   ao_ref_init (&r2, store2);
1141   return refs_may_alias_p_1 (&r1, &r2, false);
1142 }
1143
1144 /* If the call CALL may use the memory reference REF return true,
1145    otherwise return false.  */
1146
1147 static bool
1148 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1149 {
1150   tree base, callee;
1151   unsigned i;
1152   int flags = gimple_call_flags (call);
1153
1154   /* Const functions without a static chain do not implicitly use memory.  */
1155   if (!gimple_call_chain (call)
1156       && (flags & (ECF_CONST|ECF_NOVOPS)))
1157     goto process_args;
1158
1159   base = ao_ref_base (ref);
1160   if (!base)
1161     return true;
1162
1163   /* If the reference is based on a decl that is not aliased the call
1164      cannot possibly use it.  */
1165   if (DECL_P (base)
1166       && !may_be_aliased (base)
1167       /* But local statics can be used through recursion.  */
1168       && !is_global_var (base))
1169     goto process_args;
1170
1171   callee = gimple_call_fndecl (call);
1172
1173   /* Handle those builtin functions explicitly that do not act as
1174      escape points.  See tree-ssa-structalias.c:find_func_aliases
1175      for the list of builtins we might need to handle here.  */
1176   if (callee != NULL_TREE
1177       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1178     switch (DECL_FUNCTION_CODE (callee))
1179       {
1180         /* All the following functions clobber memory pointed to by
1181            their first argument.  */
1182         case BUILT_IN_STRCPY:
1183         case BUILT_IN_STRNCPY:
1184         case BUILT_IN_MEMCPY:
1185         case BUILT_IN_MEMMOVE:
1186         case BUILT_IN_MEMPCPY:
1187         case BUILT_IN_STPCPY:
1188         case BUILT_IN_STPNCPY:
1189         case BUILT_IN_STRCAT:
1190         case BUILT_IN_STRNCAT:
1191           {
1192             ao_ref dref;
1193             tree size = NULL_TREE;
1194             if (gimple_call_num_args (call) == 3)
1195               size = gimple_call_arg (call, 2);
1196             ao_ref_init_from_ptr_and_size (&dref,
1197                                            gimple_call_arg (call, 1),
1198                                            size);
1199             return refs_may_alias_p_1 (&dref, ref, false);
1200           }
1201         case BUILT_IN_BCOPY:
1202           {
1203             ao_ref dref;
1204             tree size = gimple_call_arg (call, 2);
1205             ao_ref_init_from_ptr_and_size (&dref,
1206                                            gimple_call_arg (call, 0),
1207                                            size);
1208             return refs_may_alias_p_1 (&dref, ref, false);
1209           }
1210         /* The following builtins do not read from memory.  */
1211         case BUILT_IN_FREE:
1212         case BUILT_IN_MALLOC:
1213         case BUILT_IN_CALLOC:
1214         case BUILT_IN_ALLOCA:
1215         case BUILT_IN_STACK_SAVE:
1216         case BUILT_IN_STACK_RESTORE:
1217         case BUILT_IN_MEMSET:
1218         case BUILT_IN_FREXP:
1219         case BUILT_IN_FREXPF:
1220         case BUILT_IN_FREXPL:
1221         case BUILT_IN_GAMMA_R:
1222         case BUILT_IN_GAMMAF_R:
1223         case BUILT_IN_GAMMAL_R:
1224         case BUILT_IN_LGAMMA_R:
1225         case BUILT_IN_LGAMMAF_R:
1226         case BUILT_IN_LGAMMAL_R:
1227         case BUILT_IN_MODF:
1228         case BUILT_IN_MODFF:
1229         case BUILT_IN_MODFL:
1230         case BUILT_IN_REMQUO:
1231         case BUILT_IN_REMQUOF:
1232         case BUILT_IN_REMQUOL:
1233         case BUILT_IN_SINCOS:
1234         case BUILT_IN_SINCOSF:
1235         case BUILT_IN_SINCOSL:
1236           return false;
1237         /* __sync_* builtins and some OpenMP builtins act as threading
1238            barriers.  */
1239 #undef DEF_SYNC_BUILTIN
1240 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1241 #include "sync-builtins.def"
1242 #undef DEF_SYNC_BUILTIN
1243         case BUILT_IN_GOMP_ATOMIC_START:
1244         case BUILT_IN_GOMP_ATOMIC_END:
1245         case BUILT_IN_GOMP_BARRIER:
1246         case BUILT_IN_GOMP_TASKWAIT:
1247         case BUILT_IN_GOMP_CRITICAL_START:
1248         case BUILT_IN_GOMP_CRITICAL_END:
1249         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1250         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1251         case BUILT_IN_GOMP_LOOP_END:
1252         case BUILT_IN_GOMP_ORDERED_START:
1253         case BUILT_IN_GOMP_ORDERED_END:
1254         case BUILT_IN_GOMP_PARALLEL_END:
1255         case BUILT_IN_GOMP_SECTIONS_END:
1256         case BUILT_IN_GOMP_SINGLE_COPY_START:
1257         case BUILT_IN_GOMP_SINGLE_COPY_END:
1258           return true;
1259
1260         default:
1261           /* Fallthru to general call handling.  */;
1262       }
1263
1264   /* Check if base is a global static variable that is not read
1265      by the function.  */
1266   if (callee != NULL_TREE
1267       && TREE_CODE (base) == VAR_DECL
1268       && TREE_STATIC (base))
1269     {
1270       struct cgraph_node *node = cgraph_get_node (callee);
1271       bitmap not_read;
1272
1273       /* FIXME: Callee can be an OMP builtin that does not have a call graph
1274          node yet.  We should enforce that there are nodes for all decls in the
1275          IL and remove this check instead.  */
1276       if (node
1277           && (not_read = ipa_reference_get_not_read_global (node))
1278           && bitmap_bit_p (not_read, DECL_UID (base)))
1279         goto process_args;
1280     }
1281
1282   /* Check if the base variable is call-used.  */
1283   if (DECL_P (base))
1284     {
1285       if (pt_solution_includes (gimple_call_use_set (call), base))
1286         return true;
1287     }
1288   else if ((INDIRECT_REF_P (base)
1289             || TREE_CODE (base) == MEM_REF)
1290            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1291     {
1292       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1293       if (!pi)
1294         return true;
1295
1296       if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1297         return true;
1298     }
1299   else
1300     return true;
1301
1302   /* Inspect call arguments for passed-by-value aliases.  */
1303 process_args:
1304   for (i = 0; i < gimple_call_num_args (call); ++i)
1305     {
1306       tree op = gimple_call_arg (call, i);
1307       int flags = gimple_call_arg_flags (call, i);
1308
1309       if (flags & EAF_UNUSED)
1310         continue;
1311
1312       if (TREE_CODE (op) == WITH_SIZE_EXPR)
1313         op = TREE_OPERAND (op, 0);
1314
1315       if (TREE_CODE (op) != SSA_NAME
1316           && !is_gimple_min_invariant (op))
1317         {
1318           ao_ref r;
1319           ao_ref_init (&r, op);
1320           if (refs_may_alias_p_1 (&r, ref, true))
1321             return true;
1322         }
1323     }
1324
1325   return false;
1326 }
1327
1328 static bool
1329 ref_maybe_used_by_call_p (gimple call, tree ref)
1330 {
1331   ao_ref r;
1332   bool res;
1333   ao_ref_init (&r, ref);
1334   res = ref_maybe_used_by_call_p_1 (call, &r);
1335   if (res)
1336     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1337   else
1338     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1339   return res;
1340 }
1341
1342
1343 /* If the statement STMT may use the memory reference REF return
1344    true, otherwise return false.  */
1345
1346 bool
1347 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1348 {
1349   if (is_gimple_assign (stmt))
1350     {
1351       tree rhs;
1352
1353       /* All memory assign statements are single.  */
1354       if (!gimple_assign_single_p (stmt))
1355         return false;
1356
1357       rhs = gimple_assign_rhs1 (stmt);
1358       if (is_gimple_reg (rhs)
1359           || is_gimple_min_invariant (rhs)
1360           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1361         return false;
1362
1363       return refs_may_alias_p (rhs, ref);
1364     }
1365   else if (is_gimple_call (stmt))
1366     return ref_maybe_used_by_call_p (stmt, ref);
1367   else if (gimple_code (stmt) == GIMPLE_RETURN)
1368     {
1369       tree retval = gimple_return_retval (stmt);
1370       tree base;
1371       if (retval
1372           && TREE_CODE (retval) != SSA_NAME
1373           && !is_gimple_min_invariant (retval)
1374           && refs_may_alias_p (retval, ref))
1375         return true;
1376       /* If ref escapes the function then the return acts as a use.  */
1377       base = get_base_address (ref);
1378       if (!base)
1379         ;
1380       else if (DECL_P (base))
1381         return is_global_var (base);
1382       else if (TREE_CODE (base) == MEM_REF
1383                || TREE_CODE (base) == TARGET_MEM_REF)
1384         return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1385       return false;
1386     }
1387
1388   return true;
1389 }
1390
1391 /* If the call in statement CALL may clobber the memory reference REF
1392    return true, otherwise return false.  */
1393
1394 static bool
1395 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1396 {
1397   tree base;
1398   tree callee;
1399
1400   /* If the call is pure or const it cannot clobber anything.  */
1401   if (gimple_call_flags (call)
1402       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1403     return false;
1404
1405   base = ao_ref_base (ref);
1406   if (!base)
1407     return true;
1408
1409   if (TREE_CODE (base) == SSA_NAME
1410       || CONSTANT_CLASS_P (base))
1411     return false;
1412
1413   /* If the reference is based on a decl that is not aliased the call
1414      cannot possibly clobber it.  */
1415   if (DECL_P (base)
1416       && !may_be_aliased (base)
1417       /* But local non-readonly statics can be modified through recursion
1418          or the call may implement a threading barrier which we must
1419          treat as may-def.  */
1420       && (TREE_READONLY (base)
1421           || !is_global_var (base)))
1422     return false;
1423
1424   callee = gimple_call_fndecl (call);
1425
1426   /* Handle those builtin functions explicitly that do not act as
1427      escape points.  See tree-ssa-structalias.c:find_func_aliases
1428      for the list of builtins we might need to handle here.  */
1429   if (callee != NULL_TREE
1430       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1431     switch (DECL_FUNCTION_CODE (callee))
1432       {
1433         /* All the following functions clobber memory pointed to by
1434            their first argument.  */
1435         case BUILT_IN_STRCPY:
1436         case BUILT_IN_STRNCPY:
1437         case BUILT_IN_MEMCPY:
1438         case BUILT_IN_MEMMOVE:
1439         case BUILT_IN_MEMPCPY:
1440         case BUILT_IN_STPCPY:
1441         case BUILT_IN_STPNCPY:
1442         case BUILT_IN_STRCAT:
1443         case BUILT_IN_STRNCAT:
1444         case BUILT_IN_MEMSET:
1445           {
1446             ao_ref dref;
1447             tree size = NULL_TREE;
1448             if (gimple_call_num_args (call) == 3)
1449               size = gimple_call_arg (call, 2);
1450             ao_ref_init_from_ptr_and_size (&dref,
1451                                            gimple_call_arg (call, 0),
1452                                            size);
1453             return refs_may_alias_p_1 (&dref, ref, false);
1454           }
1455         case BUILT_IN_BCOPY:
1456           {
1457             ao_ref dref;
1458             tree size = gimple_call_arg (call, 2);
1459             ao_ref_init_from_ptr_and_size (&dref,
1460                                            gimple_call_arg (call, 1),
1461                                            size);
1462             return refs_may_alias_p_1 (&dref, ref, false);
1463           }
1464         /* Allocating memory does not have any side-effects apart from
1465            being the definition point for the pointer.  */
1466         case BUILT_IN_MALLOC:
1467         case BUILT_IN_CALLOC:
1468           /* Unix98 specifies that errno is set on allocation failure.  */
1469           if (flag_errno_math
1470               && targetm.ref_may_alias_errno (ref))
1471             return true;
1472           return false;
1473         case BUILT_IN_STACK_SAVE:
1474         case BUILT_IN_ALLOCA:
1475           return false;
1476         /* Freeing memory kills the pointed-to memory.  More importantly
1477            the call has to serve as a barrier for moving loads and stores
1478            across it.  */
1479         case BUILT_IN_FREE:
1480           {
1481             tree ptr = gimple_call_arg (call, 0);
1482             return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1483           }
1484         case BUILT_IN_GAMMA_R:
1485         case BUILT_IN_GAMMAF_R:
1486         case BUILT_IN_GAMMAL_R:
1487         case BUILT_IN_LGAMMA_R:
1488         case BUILT_IN_LGAMMAF_R:
1489         case BUILT_IN_LGAMMAL_R:
1490           {
1491             tree out = gimple_call_arg (call, 1);
1492             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1493               return true;
1494             if (flag_errno_math)
1495               break;
1496             return false;
1497           }
1498         case BUILT_IN_FREXP:
1499         case BUILT_IN_FREXPF:
1500         case BUILT_IN_FREXPL:
1501         case BUILT_IN_MODF:
1502         case BUILT_IN_MODFF:
1503         case BUILT_IN_MODFL:
1504           {
1505             tree out = gimple_call_arg (call, 1);
1506             return ptr_deref_may_alias_ref_p_1 (out, ref);
1507           }
1508         case BUILT_IN_REMQUO:
1509         case BUILT_IN_REMQUOF:
1510         case BUILT_IN_REMQUOL:
1511           {
1512             tree out = gimple_call_arg (call, 2);
1513             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1514               return true;
1515             if (flag_errno_math)
1516               break;
1517             return false;
1518           }
1519         case BUILT_IN_SINCOS:
1520         case BUILT_IN_SINCOSF:
1521         case BUILT_IN_SINCOSL:
1522           {
1523             tree sin = gimple_call_arg (call, 1);
1524             tree cos = gimple_call_arg (call, 2);
1525             return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1526                     || ptr_deref_may_alias_ref_p_1 (cos, ref));
1527           }
1528         /* __sync_* builtins and some OpenMP builtins act as threading
1529            barriers.  */
1530 #undef DEF_SYNC_BUILTIN
1531 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1532 #include "sync-builtins.def"
1533 #undef DEF_SYNC_BUILTIN
1534         case BUILT_IN_GOMP_ATOMIC_START:
1535         case BUILT_IN_GOMP_ATOMIC_END:
1536         case BUILT_IN_GOMP_BARRIER:
1537         case BUILT_IN_GOMP_TASKWAIT:
1538         case BUILT_IN_GOMP_CRITICAL_START:
1539         case BUILT_IN_GOMP_CRITICAL_END:
1540         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1541         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1542         case BUILT_IN_GOMP_LOOP_END:
1543         case BUILT_IN_GOMP_ORDERED_START:
1544         case BUILT_IN_GOMP_ORDERED_END:
1545         case BUILT_IN_GOMP_PARALLEL_END:
1546         case BUILT_IN_GOMP_SECTIONS_END:
1547         case BUILT_IN_GOMP_SINGLE_COPY_START:
1548         case BUILT_IN_GOMP_SINGLE_COPY_END:
1549           return true;
1550         default:
1551           /* Fallthru to general call handling.  */;
1552       }
1553
1554   /* Check if base is a global static variable that is not written
1555      by the function.  */
1556   if (callee != NULL_TREE
1557       && TREE_CODE (base) == VAR_DECL
1558       && TREE_STATIC (base))
1559     {
1560       struct cgraph_node *node = cgraph_get_node (callee);
1561       bitmap not_written;
1562
1563       if (node
1564           && (not_written = ipa_reference_get_not_written_global (node))
1565           && bitmap_bit_p (not_written, DECL_UID (base)))
1566         return false;
1567     }
1568
1569   /* Check if the base variable is call-clobbered.  */
1570   if (DECL_P (base))
1571     return pt_solution_includes (gimple_call_clobber_set (call), base);
1572   else if ((INDIRECT_REF_P (base)
1573             || TREE_CODE (base) == MEM_REF)
1574            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1575     {
1576       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1577       if (!pi)
1578         return true;
1579
1580       return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1581     }
1582
1583   return true;
1584 }
1585
1586 /* If the call in statement CALL may clobber the memory reference REF
1587    return true, otherwise return false.  */
1588
1589 bool
1590 call_may_clobber_ref_p (gimple call, tree ref)
1591 {
1592   bool res;
1593   ao_ref r;
1594   ao_ref_init (&r, ref);
1595   res = call_may_clobber_ref_p_1 (call, &r);
1596   if (res)
1597     ++alias_stats.call_may_clobber_ref_p_may_alias;
1598   else
1599     ++alias_stats.call_may_clobber_ref_p_no_alias;
1600   return res;
1601 }
1602
1603
1604 /* If the statement STMT may clobber the memory reference REF return true,
1605    otherwise return false.  */
1606
1607 bool
1608 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1609 {
1610   if (is_gimple_call (stmt))
1611     {
1612       tree lhs = gimple_call_lhs (stmt);
1613       if (lhs
1614           && TREE_CODE (lhs) != SSA_NAME)
1615         {
1616           ao_ref r;
1617           ao_ref_init (&r, lhs);
1618           if (refs_may_alias_p_1 (ref, &r, true))
1619             return true;
1620         }
1621
1622       return call_may_clobber_ref_p_1 (stmt, ref);
1623     }
1624   else if (gimple_assign_single_p (stmt))
1625     {
1626       tree lhs = gimple_assign_lhs (stmt);
1627       if (TREE_CODE (lhs) != SSA_NAME)
1628         {
1629           ao_ref r;
1630           ao_ref_init (&r, lhs);
1631           return refs_may_alias_p_1 (ref, &r, true);
1632         }
1633     }
1634   else if (gimple_code (stmt) == GIMPLE_ASM)
1635     return true;
1636
1637   return false;
1638 }
1639
1640 bool
1641 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1642 {
1643   ao_ref r;
1644   ao_ref_init (&r, ref);
1645   return stmt_may_clobber_ref_p_1 (stmt, &r);
1646 }
1647
1648 /* If STMT kills the memory reference REF return true, otherwise
1649    return false.  */
1650
1651 static bool
1652 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1653 {
1654   /* For a must-alias check we need to be able to constrain
1655      the access properly.  */
1656   ao_ref_base (ref);
1657   if (ref->max_size == -1)
1658     return false;
1659
1660   if (gimple_has_lhs (stmt)
1661       && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME)
1662     {
1663       tree base, lhs = gimple_get_lhs (stmt);
1664       HOST_WIDE_INT size, offset, max_size;
1665       base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1666       /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1667          so base == ref->base does not always hold.  */
1668       if (base == ref->base)
1669         {
1670           /* For a must-alias check we need to be able to constrain
1671              the access properly.  */
1672           if (size != -1 && size == max_size)
1673             {
1674               if (offset <= ref->offset
1675                   && offset + size >= ref->offset + ref->max_size)
1676                 return true;
1677             }
1678         }
1679     }
1680
1681   if (is_gimple_call (stmt))
1682     {
1683       tree callee = gimple_call_fndecl (stmt);
1684       if (callee != NULL_TREE
1685           && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1686         switch (DECL_FUNCTION_CODE (callee))
1687           {
1688           case BUILT_IN_MEMCPY:
1689           case BUILT_IN_MEMPCPY:
1690           case BUILT_IN_MEMMOVE:
1691           case BUILT_IN_MEMSET:
1692             {
1693               tree dest = gimple_call_arg (stmt, 0);
1694               tree len = gimple_call_arg (stmt, 2);
1695               tree base = NULL_TREE;
1696               HOST_WIDE_INT offset = 0;
1697               if (!host_integerp (len, 0))
1698                 return false;
1699               if (TREE_CODE (dest) == ADDR_EXPR)
1700                 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1701                                                       &offset);
1702               else if (TREE_CODE (dest) == SSA_NAME)
1703                 base = dest;
1704               if (base
1705                   && base == ao_ref_base (ref))
1706                 {
1707                   HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1708                   if (offset <= ref->offset / BITS_PER_UNIT
1709                       && (offset + size
1710                           >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1711                               / BITS_PER_UNIT)))
1712                     return true;
1713                 }
1714             }
1715           default:;
1716           }
1717
1718     }
1719   return false;
1720 }
1721
1722 bool
1723 stmt_kills_ref_p (gimple stmt, tree ref)
1724 {
1725   ao_ref r;
1726   ao_ref_init (&r, ref);
1727   return stmt_kills_ref_p_1 (stmt, &r);
1728 }
1729
1730
1731 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1732    TARGET or a statement clobbering the memory reference REF in which
1733    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
1734
1735 static bool
1736 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1737                   tree vuse, bitmap *visited)
1738 {
1739   if (!*visited)
1740     *visited = BITMAP_ALLOC (NULL);
1741
1742   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1743
1744   /* Walk until we hit the target.  */
1745   while (vuse != target)
1746     {
1747       gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1748       /* Recurse for PHI nodes.  */
1749       if (gimple_code (def_stmt) == GIMPLE_PHI)
1750         {
1751           /* An already visited PHI node ends the walk successfully.  */
1752           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1753             return true;
1754           vuse = get_continuation_for_phi (def_stmt, ref, visited);
1755           if (!vuse)
1756             return false;
1757           continue;
1758         }
1759       /* A clobbering statement or the end of the IL ends it failing.  */
1760       else if (gimple_nop_p (def_stmt)
1761                || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1762         return false;
1763       vuse = gimple_vuse (def_stmt);
1764     }
1765   return true;
1766 }
1767
1768 /* Starting from a PHI node for the virtual operand of the memory reference
1769    REF find a continuation virtual operand that allows to continue walking
1770    statements dominating PHI skipping only statements that cannot possibly
1771    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
1772    be found.  */
1773
1774 tree
1775 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1776 {
1777   unsigned nargs = gimple_phi_num_args (phi);
1778
1779   /* Through a single-argument PHI we can simply look through.  */
1780   if (nargs == 1)
1781     return PHI_ARG_DEF (phi, 0);
1782
1783   /* For two arguments try to skip non-aliasing code until we hit
1784      the phi argument definition that dominates the other one.  */
1785   if (nargs == 2)
1786     {
1787       tree arg0 = PHI_ARG_DEF (phi, 0);
1788       tree arg1 = PHI_ARG_DEF (phi, 1);
1789       gimple def0 = SSA_NAME_DEF_STMT (arg0);
1790       gimple def1 = SSA_NAME_DEF_STMT (arg1);
1791       tree common_vuse;
1792
1793       if (arg0 == arg1)
1794         return arg0;
1795       else if (gimple_nop_p (def0)
1796                || (!gimple_nop_p (def1)
1797                    && dominated_by_p (CDI_DOMINATORS,
1798                                       gimple_bb (def1), gimple_bb (def0))))
1799         {
1800           if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1801             return arg0;
1802         }
1803       else if (gimple_nop_p (def1)
1804                || dominated_by_p (CDI_DOMINATORS,
1805                                   gimple_bb (def0), gimple_bb (def1)))
1806         {
1807           if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1808             return arg1;
1809         }
1810       /* Special case of a diamond:
1811            MEM_1 = ...
1812            goto (cond) ? L1 : L2
1813            L1: store1 = ...    #MEM_2 = vuse(MEM_1)
1814                goto L3
1815            L2: store2 = ...    #MEM_3 = vuse(MEM_1)
1816            L3: MEM_4 = PHI<MEM_2, MEM_3>
1817          We were called with the PHI at L3, MEM_2 and MEM_3 don't
1818          dominate each other, but still we can easily skip this PHI node
1819          if we recognize that the vuse MEM operand is the same for both,
1820          and that we can skip both statements (they don't clobber us).
1821          This is still linear.  Don't use maybe_skip_until, that might
1822          potentially be slow.  */
1823       else if ((common_vuse = gimple_vuse (def0))
1824                && common_vuse == gimple_vuse (def1))
1825         {
1826           if (!stmt_may_clobber_ref_p_1 (def0, ref)
1827               && !stmt_may_clobber_ref_p_1 (def1, ref))
1828             return common_vuse;
1829         }
1830     }
1831
1832   return NULL_TREE;
1833 }
1834
1835 /* Based on the memory reference REF and its virtual use VUSE call
1836    WALKER for each virtual use that is equivalent to VUSE, including VUSE
1837    itself.  That is, for each virtual use for which its defining statement
1838    does not clobber REF.
1839
1840    WALKER is called with REF, the current virtual use and DATA.  If
1841    WALKER returns non-NULL the walk stops and its result is returned.
1842    At the end of a non-successful walk NULL is returned.
1843
1844    TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1845    use which definition is a statement that may clobber REF and DATA.
1846    If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1847    If TRANSLATE returns non-NULL the walk stops and its result is returned.
1848    If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1849    to adjust REF and *DATA to make that valid.
1850
1851    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
1852
1853 void *
1854 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1855                         void *(*walker)(ao_ref *, tree, void *),
1856                         void *(*translate)(ao_ref *, tree, void *), void *data)
1857 {
1858   bitmap visited = NULL;
1859   void *res;
1860
1861   timevar_push (TV_ALIAS_STMT_WALK);
1862
1863   do
1864     {
1865       gimple def_stmt;
1866
1867       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1868       res = (*walker) (ref, vuse, data);
1869       if (res)
1870         break;
1871
1872       def_stmt = SSA_NAME_DEF_STMT (vuse);
1873       if (gimple_nop_p (def_stmt))
1874         break;
1875       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1876         vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1877       else
1878         {
1879           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1880             {
1881               if (!translate)
1882                 break;
1883               res = (*translate) (ref, vuse, data);
1884               /* Failed lookup and translation.  */
1885               if (res == (void *)-1)
1886                 {
1887                   res = NULL;
1888                   break;
1889                 }
1890               /* Lookup succeeded.  */
1891               else if (res != NULL)
1892                 break;
1893               /* Translation succeeded, continue walking.  */
1894             }
1895           vuse = gimple_vuse (def_stmt);
1896         }
1897     }
1898   while (vuse);
1899
1900   if (visited)
1901     BITMAP_FREE (visited);
1902
1903   timevar_pop (TV_ALIAS_STMT_WALK);
1904
1905   return res;
1906 }
1907
1908
1909 /* Based on the memory reference REF call WALKER for each vdef which
1910    defining statement may clobber REF, starting with VDEF.  If REF
1911    is NULL_TREE, each defining statement is visited.
1912
1913    WALKER is called with REF, the current vdef and DATA.  If WALKER
1914    returns true the walk is stopped, otherwise it continues.
1915
1916    At PHI nodes walk_aliased_vdefs forks into one walk for reach
1917    PHI argument (but only one walk continues on merge points), the
1918    return value is true if any of the walks was successful.
1919
1920    The function returns the number of statements walked.  */
1921
1922 static unsigned int
1923 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1924                       bool (*walker)(ao_ref *, tree, void *), void *data,
1925                       bitmap *visited, unsigned int cnt)
1926 {
1927   do
1928     {
1929       gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1930
1931       if (*visited
1932           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1933         return cnt;
1934
1935       if (gimple_nop_p (def_stmt))
1936         return cnt;
1937       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1938         {
1939           unsigned i;
1940           if (!*visited)
1941             *visited = BITMAP_ALLOC (NULL);
1942           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1943             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1944                                          walker, data, visited, 0);
1945           return cnt;
1946         }
1947
1948       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1949       cnt++;
1950       if ((!ref
1951            || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1952           && (*walker) (ref, vdef, data))
1953         return cnt;
1954
1955       vdef = gimple_vuse (def_stmt);
1956     }
1957   while (1);
1958 }
1959
1960 unsigned int
1961 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1962                     bool (*walker)(ao_ref *, tree, void *), void *data,
1963                     bitmap *visited)
1964 {
1965   bitmap local_visited = NULL;
1966   unsigned int ret;
1967
1968   timevar_push (TV_ALIAS_STMT_WALK);
1969
1970   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1971                               visited ? visited : &local_visited, 0);
1972   if (local_visited)
1973     BITMAP_FREE (local_visited);
1974
1975   timevar_pop (TV_ALIAS_STMT_WALK);
1976
1977   return ret;
1978 }
1979