OSDN Git Service

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