OSDN Git Service

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