OSDN Git Service

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