OSDN Git Service

2009-04-15 Rafael Avila de Espindola <espindola@google.com>
[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   return pt_solution_includes_global (&pi->pt);
159 }
160
161 /* Return true if dereferencing PTR may alias DECL.  */
162
163 static bool
164 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
165 {
166   struct ptr_info_def *pi;
167
168   /* ???  During SCCVN/PRE we can end up with *&x during valueizing
169      operands.  Likewise we can end up with dereferencing constant
170      pointers.  Just bail out in these cases for now.  */
171   if (TREE_CODE (ptr) == ADDR_EXPR
172       || TREE_CODE (ptr) == INTEGER_CST)
173     return true;
174
175   gcc_assert (TREE_CODE (ptr) == SSA_NAME
176               && (TREE_CODE (decl) == VAR_DECL
177                   || TREE_CODE (decl) == PARM_DECL
178                   || TREE_CODE (decl) == RESULT_DECL));
179
180   /* Non-aliased variables can not be pointed to.  */
181   if (!may_be_aliased (decl))
182     return false;
183
184   /* If we do not have useful points-to information for this pointer
185      we cannot disambiguate anything else.  */
186   pi = SSA_NAME_PTR_INFO (ptr);
187   if (!pi)
188     return true;
189
190   return pt_solution_includes (&pi->pt, decl);
191 }
192
193 /* Return true if dereferenced PTR1 and PTR2 may alias.  */
194
195 static bool
196 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
197 {
198   struct ptr_info_def *pi1, *pi2;
199
200   /* ???  During SCCVN/PRE we can end up with *&x during valueizing
201      operands.  Likewise we can end up with dereferencing constant
202      pointers.  Just bail out in these cases for now.  */
203   if (TREE_CODE (ptr1) == ADDR_EXPR
204       || TREE_CODE (ptr1) == INTEGER_CST
205       || TREE_CODE (ptr2) == ADDR_EXPR
206       || TREE_CODE (ptr2) == INTEGER_CST)
207     return true;
208
209   gcc_assert (TREE_CODE (ptr1) == SSA_NAME
210               && TREE_CODE (ptr2) == SSA_NAME);
211
212   /* We may end up with two empty points-to solutions for two same pointers.
213      In this case we still want to say both pointers alias, so shortcut
214      that here.  */
215   if (ptr1 == ptr2)
216     return true;
217
218   /* If we do not have useful points-to information for either pointer
219      we cannot disambiguate anything else.  */
220   pi1 = SSA_NAME_PTR_INFO (ptr1);
221   pi2 = SSA_NAME_PTR_INFO (ptr2);
222   if (!pi1 || !pi2)
223     return true;
224
225   return pt_solutions_intersect (&pi1->pt, &pi2->pt);
226 }
227
228 /* Return true if STMT is an "escape" site from the current function.  Escape
229    sites those statements which might expose the address of a variable
230    outside the current function.  STMT is an escape site iff:
231
232         1- STMT is a function call, or
233         2- STMT is an __asm__ expression, or
234         3- STMT is an assignment to a non-local variable, or
235         4- STMT is a return statement.
236
237    Return the type of escape site found, if we found one, or NO_ESCAPE
238    if none.  */
239
240 enum escape_type
241 is_escape_site (gimple stmt)
242 {
243   if (is_gimple_call (stmt))
244     {
245       if (gimple_call_flags (stmt) & (ECF_PURE | ECF_CONST))
246         return ESCAPE_TO_PURE_CONST;
247
248       return ESCAPE_TO_CALL;
249     }
250   else if (gimple_code (stmt) == GIMPLE_ASM)
251     return ESCAPE_TO_ASM;
252   else if (is_gimple_assign (stmt))
253     {
254       tree lhs = gimple_assign_lhs (stmt);
255
256       /* Get to the base of _REF nodes.  */
257       if (TREE_CODE (lhs) != SSA_NAME)
258         lhs = get_base_address (lhs);
259
260       /* If we couldn't recognize the LHS of the assignment, assume that it
261          is a non-local store.  */
262       if (lhs == NULL_TREE)
263         return ESCAPE_UNKNOWN;
264
265       if (gimple_assign_cast_p (stmt))
266         {
267           tree from = TREE_TYPE (gimple_assign_rhs1 (stmt));
268           tree to = TREE_TYPE (lhs);
269
270           /* If the RHS is a conversion between a pointer and an integer, the
271              pointer escapes since we can't track the integer.  */
272           if (POINTER_TYPE_P (from) && !POINTER_TYPE_P (to))
273             return ESCAPE_BAD_CAST;
274         }
275
276       /* If the LHS is an SSA name, it can't possibly represent a non-local
277          memory store.  */
278       if (TREE_CODE (lhs) == SSA_NAME)
279         return NO_ESCAPE;
280
281       /* If the LHS is a non-global decl, it isn't a non-local memory store.
282          If the LHS escapes, the RHS escape is dealt with in the PTA solver.  */
283       if (DECL_P (lhs)
284           && !is_global_var (lhs))
285         return NO_ESCAPE;
286
287       /* FIXME: LHS is not an SSA_NAME.  Even if it's an assignment to a
288          local variables we cannot be sure if it will escape, because we
289          don't have information about objects not in SSA form.  Need to
290          implement something along the lines of
291
292          J.-D. Choi, M. Gupta, M. J. Serrano, V. C. Sreedhar, and S. P.
293          Midkiff, ``Escape analysis for java,'' in Proceedings of the
294          Conference on Object-Oriented Programming Systems, Languages, and
295          Applications (OOPSLA), pp. 1-19, 1999.  */
296       return ESCAPE_STORED_IN_GLOBAL;
297     }
298   else if (gimple_code (stmt) == GIMPLE_RETURN)
299     return ESCAPE_TO_RETURN;
300
301   return NO_ESCAPE;
302 }
303
304
305 /* Dump alias information on FILE.  */
306
307 void
308 dump_alias_info (FILE *file)
309 {
310   size_t i;
311   const char *funcname
312     = lang_hooks.decl_printable_name (current_function_decl, 2);
313   referenced_var_iterator rvi;
314   tree var;
315
316   fprintf (file, "\n\nAlias information for %s\n\n", funcname);
317
318   fprintf (file, "Aliased symbols\n\n");
319   
320   FOR_EACH_REFERENCED_VAR (var, rvi)
321     {
322       if (may_be_aliased (var))
323         dump_variable (file, var);
324     }
325
326   fprintf (file, "\n\nFlow-insensitive points-to information for %s\n\n", funcname);
327
328   for (i = 1; i < num_ssa_names; i++)
329     {
330       tree ptr = ssa_name (i);
331       struct ptr_info_def *pi;
332       
333       if (ptr == NULL_TREE
334           || SSA_NAME_IN_FREE_LIST (ptr))
335         continue;
336
337       pi = SSA_NAME_PTR_INFO (ptr);
338       if (pi)
339         dump_points_to_info_for (file, ptr);
340     }
341
342   fprintf (file, "\n");
343 }
344
345
346 /* Dump alias information on stderr.  */
347
348 void
349 debug_alias_info (void)
350 {
351   dump_alias_info (stderr);
352 }
353
354
355 /* Return the alias information associated with pointer T.  It creates a
356    new instance if none existed.  */
357
358 struct ptr_info_def *
359 get_ptr_info (tree t)
360 {
361   struct ptr_info_def *pi;
362
363   gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
364
365   pi = SSA_NAME_PTR_INFO (t);
366   if (pi == NULL)
367     {
368       pi = GGC_CNEW (struct ptr_info_def);
369       pt_solution_reset (&pi->pt);
370       SSA_NAME_PTR_INFO (t) = pi;
371     }
372
373   return pi;
374 }
375
376 /* Dump points-to information for SSA_NAME PTR into FILE.  */
377
378 void
379 dump_points_to_info_for (FILE *file, tree ptr)
380 {
381   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
382
383   print_generic_expr (file, ptr, dump_flags);
384
385   if (pi)
386     {
387       if (pi->pt.anything)
388         fprintf (file, ", points-to anything");
389
390       if (pi->pt.nonlocal)
391         fprintf (file, ", points-to non-local");
392
393       if (pi->pt.escaped)
394         fprintf (file, ", points-to escaped");
395
396       if (pi->pt.null)
397         fprintf (file, ", points-to NULL");
398
399       if (pi->pt.vars)
400         {
401           fprintf (file, ", points-to vars: ");
402           dump_decl_set (file, pi->pt.vars);
403           if (pi->pt.vars_contains_global)
404             fprintf (file, " (includes global vars)");
405         }
406     }
407
408   fprintf (file, "\n");
409 }
410
411
412 /* Dump points-to information for VAR into stderr.  */
413
414 void
415 debug_points_to_info_for (tree var)
416 {
417   dump_points_to_info_for (stderr, var);
418 }
419
420 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
421    purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
422    decide.  */
423
424 static inline int
425 same_type_for_tbaa (tree type1, tree type2)
426 {
427   type1 = TYPE_MAIN_VARIANT (type1);
428   type2 = TYPE_MAIN_VARIANT (type2);
429
430   /* If we would have to do structural comparison bail out.  */
431   if (TYPE_STRUCTURAL_EQUALITY_P (type1)
432       || TYPE_STRUCTURAL_EQUALITY_P (type2))
433     return -1;
434
435   /* Compare the canonical types.  */
436   if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
437     return 1;
438
439   /* ???  Array types are not properly unified in all cases as we have
440      spurious changes in the index types for example.  Removing this
441      causes all sorts of problems with the Fortran frontend.  */
442   if (TREE_CODE (type1) == ARRAY_TYPE
443       && TREE_CODE (type2) == ARRAY_TYPE)
444     return -1;
445
446   /* The types are known to be not equal.  */
447   return 0;
448 }
449
450 /* Determine if the two component references REF1 and REF2 which are
451    based on access types TYPE1 and TYPE2 and of which at least one is based
452    on an indirect reference may alias.  */
453
454 static bool
455 nonaliasing_component_refs_p (tree ref1, tree type1,
456                               HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
457                               tree ref2, tree type2,
458                               HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
459 {
460   /* If one reference is a component references through pointers try to find a
461      common base and apply offset based disambiguation.  This handles
462      for example
463        struct A { int i; int j; } *q;
464        struct B { struct A a; int k; } *p;
465      disambiguating q->i and p->a.j.  */
466   tree *refp;
467   int same_p;
468
469   /* Now search for the type1 in the access path of ref2.  This
470      would be a common base for doing offset based disambiguation on.  */
471   /* Skip the outermost ref - we would have caught that earlier.  */
472   refp = &TREE_OPERAND (ref2, 0);
473   while (handled_component_p (*refp)
474          && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
475     refp = &TREE_OPERAND (*refp, 0);
476   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
477   /* If we couldn't compare types we have to bail out.  */
478   if (same_p == -1)
479     return true;
480   else if (same_p == 1)
481     {
482       HOST_WIDE_INT offadj, sztmp, msztmp;
483       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
484       offset2 -= offadj;
485       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
486     }
487   /* If we didn't find a common base, try the other way around.  */
488   refp = &TREE_OPERAND (ref1, 0);
489   while (handled_component_p (*refp)
490          && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
491     refp = &TREE_OPERAND (*refp, 0);
492   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
493   /* If we couldn't compare types we have to bail out.  */
494   if (same_p == -1)
495     return true;
496   else if (same_p == 1)
497     {
498       HOST_WIDE_INT offadj, sztmp, msztmp;
499       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
500       offset1 -= offadj;
501       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
502     }
503   /* If we have two type access paths B1.path1 and B2.path2 they may
504      only alias if either B1 is in B2.path2 or B2 is in B1.path1.  */
505   return false;
506 }
507
508 /* Return true if two memory references based on the variables BASE1
509    and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
510    [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias.  */
511
512 static bool
513 decl_refs_may_alias_p (tree base1,
514                        HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
515                        tree base2,
516                        HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
517 {
518   gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
519
520   /* If both references are based on different variables, they cannot alias.  */
521   if (!operand_equal_p (base1, base2, 0))
522     return false;
523
524   /* If both references are based on the same variable, they cannot alias if
525      the accesses do not overlap.  */
526   return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
527 }
528
529 /* Return true if an indirect reference based on *PTR1 constrained
530    to [OFFSET1, OFFSET1 + MAX_SIZE1[ may alias a variable based on BASE2
531    constrained to [OFFSET2, OFFSET2 + MAX_SIZE2[.  *PTR1 and BASE2 have
532    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
533    in which case they are computed on-demand.  REF1 and REF2
534    if non-NULL are the complete memory reference trees.  */
535
536 static bool
537 indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
538                                HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
539                                alias_set_type base1_alias_set,
540                                tree ref2, tree base2,
541                                HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
542                                alias_set_type base2_alias_set)
543 {
544   /* If only one reference is based on a variable, they cannot alias if
545      the pointer access is beyond the extent of the variable access.
546      (the pointer base cannot validly point to an offset less than zero
547      of the variable).
548      They also cannot alias if the pointer may not point to the decl.  */
549   if (max_size2 != -1
550       && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
551     return false;
552   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
553     return false;
554
555   /* Disambiguations that rely on strict aliasing rules follow.  */
556   if (!flag_strict_aliasing)
557     return true;
558
559   /* If the alias set for a pointer access is zero all bets are off.  */
560   if (base1_alias_set == -1)
561     base1_alias_set = get_deref_alias_set (ptr1);
562   if (base1_alias_set == 0)
563     return true;
564   if (base2_alias_set == -1)
565     base2_alias_set = get_alias_set (base2);
566
567   /* If both references are through the same type, they do not alias
568      if the accesses do not overlap.  This does extra disambiguation
569      for mixed/pointer accesses but requires strict aliasing.  */
570   if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
571                           TREE_TYPE (base2)) == 1)
572     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
573
574   /* The only way to access a variable is through a pointer dereference
575      of the same alias set or a subset of it.  */
576   if (base1_alias_set != base2_alias_set
577       && !alias_set_subset_of (base1_alias_set, base2_alias_set))
578     return false;
579
580   /* Do access-path based disambiguation.  */
581   if (ref1 && ref2
582       && handled_component_p (ref1)
583       && handled_component_p (ref2))
584     return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
585                                          offset1, max_size1,
586                                          ref2, TREE_TYPE (base2),
587                                          offset2, max_size2);
588
589   return true;
590 }
591
592 /* Return true if two indirect references based on *PTR1
593    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
594    [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias.  *PTR1 and *PTR2 have
595    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
596    in which case they are computed on-demand.  REF1 and REF2
597    if non-NULL are the complete memory reference trees. */
598
599 static bool
600 indirect_refs_may_alias_p (tree ref1, tree ptr1,
601                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
602                            alias_set_type base1_alias_set,
603                            tree ref2, tree ptr2,
604                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
605                            alias_set_type base2_alias_set)
606 {
607   /* If both bases are based on pointers they cannot alias if they may not
608      point to the same memory object or if they point to the same object
609      and the accesses do not overlap.  */
610   if (operand_equal_p (ptr1, ptr2, 0))
611     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
612   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
613     return false;
614
615   /* Disambiguations that rely on strict aliasing rules follow.  */
616   if (!flag_strict_aliasing)
617     return true;
618
619   /* If the alias set for a pointer access is zero all bets are off.  */
620   if (base1_alias_set == -1)
621     base1_alias_set = get_deref_alias_set (ptr1);
622   if (base1_alias_set == 0)
623     return true;
624   if (base2_alias_set == -1)
625     base2_alias_set = get_deref_alias_set (ptr2);
626   if (base2_alias_set == 0)
627     return true;
628
629   /* If both references are through the same type, they do not alias
630      if the accesses do not overlap.  This does extra disambiguation
631      for mixed/pointer accesses but requires strict aliasing.  */
632   if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
633                           TREE_TYPE (TREE_TYPE (ptr2))) == 1)
634     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
635
636   /* Do type-based disambiguation.  */
637   if (base1_alias_set != base2_alias_set
638       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
639     return false;
640
641   /* Do access-path based disambiguation.  */
642   if (ref1 && ref2
643       && handled_component_p (ref1)
644       && handled_component_p (ref2))
645     return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
646                                          offset1, max_size1,
647                                          ref2, TREE_TYPE (TREE_TYPE (ptr2)),
648                                          offset2, max_size2);
649
650   return true;
651 }
652
653 /* Return true, if the two memory references REF1 and REF2 may alias.  */
654
655 static bool
656 refs_may_alias_p_1 (tree ref1, tree ref2)
657 {
658   tree base1, base2;
659   HOST_WIDE_INT offset1 = 0, offset2 = 0;
660   HOST_WIDE_INT size1 = -1, size2 = -1;
661   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
662   bool var1_p, var2_p, ind1_p, ind2_p;
663
664   gcc_assert ((SSA_VAR_P (ref1)
665                || handled_component_p (ref1)
666                || INDIRECT_REF_P (ref1)
667                || TREE_CODE (ref1) == TARGET_MEM_REF)
668               && (SSA_VAR_P (ref2)
669                   || handled_component_p (ref2)
670                   || INDIRECT_REF_P (ref2)
671                   || TREE_CODE (ref2) == TARGET_MEM_REF));
672
673   /* Defer to TBAA if possible.  */
674   if (flag_strict_aliasing
675       && !alias_sets_conflict_p (get_alias_set (ref1), get_alias_set (ref2)))
676     return false;
677
678   /* If one reference is a TARGET_MEM_REF weird things are allowed.  */
679   if (TREE_CODE (ref1) == TARGET_MEM_REF
680       || TREE_CODE (ref2) == TARGET_MEM_REF)
681     return true;
682
683   /* Decompose the references into their base objects and the access.  */
684   base1 = get_ref_base_and_extent (ref1, &offset1, &size1, &max_size1);
685   base2 = get_ref_base_and_extent (ref2, &offset2, &size2, &max_size2);
686
687   /* We can end up with registers or constants as bases for example from
688      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
689      which is seen as a struct copy.  */
690   if (TREE_CODE (base1) == SSA_NAME
691       || TREE_CODE (base2) == SSA_NAME
692       || is_gimple_min_invariant (base1)
693       || is_gimple_min_invariant (base2))
694     return false;
695
696   var1_p = SSA_VAR_P (base1);
697   var2_p = SSA_VAR_P (base2);
698   ind1_p = INDIRECT_REF_P (base1);
699   ind2_p = INDIRECT_REF_P (base2);
700   if (var1_p && var2_p)
701     return decl_refs_may_alias_p (base1, offset1, max_size1,
702                                   base2, offset2, max_size2);
703   else if (var1_p && ind2_p)
704     return indirect_ref_may_alias_decl_p (ref2, TREE_OPERAND (base2, 0),
705                                           offset2, max_size2, -1,
706                                           ref1, base1,
707                                           offset1, max_size1, -1);
708   else if (ind1_p && var2_p)
709     return indirect_ref_may_alias_decl_p (ref1, TREE_OPERAND (base1, 0),
710                                           offset1, max_size1, -1,
711                                           ref2, base2,
712                                           offset2, max_size2, -1);
713   else if (ind1_p && ind2_p)
714     return indirect_refs_may_alias_p (ref1, TREE_OPERAND (base1, 0),
715                                       offset1, max_size1, -1,
716                                       ref2, TREE_OPERAND (base2, 0),
717                                       offset2, max_size2, -1);
718
719   gcc_unreachable ();
720 }
721
722 bool
723 refs_may_alias_p (tree ref1, tree ref2)
724 {
725   bool res = refs_may_alias_p_1 (ref1, ref2);
726   if (res)
727     ++alias_stats.refs_may_alias_p_may_alias;
728   else
729     ++alias_stats.refs_may_alias_p_no_alias;
730   return res;
731 }
732
733
734 /* If the call CALL may use the memory reference REF return true,
735    otherwise return false.  */
736
737 static bool
738 ref_maybe_used_by_call_p_1 (gimple call, tree ref)
739 {
740   tree base, fndecl;
741   unsigned i;
742   int flags = gimple_call_flags (call);
743
744   /* Const functions without a static chain do not implicitly use memory.  */
745   if (!gimple_call_chain (call)
746       && (flags & (ECF_CONST|ECF_NOVOPS)))
747     goto process_args;
748
749   /* If the reference is not based on a decl give up.
750      ???  Handle indirect references by intersecting the call-used
751           solution with that of the pointer.  */
752   base = get_base_address (ref);
753   if (!base
754       || !DECL_P (base))
755     return true;
756
757   /* If the reference is based on a decl that is not aliased the call
758      cannot possibly use it.  */
759   if (DECL_P (base)
760       && !may_be_aliased (base)
761       /* But local statics can be used through recursion!  */
762       && (!is_global_var (base)
763           /* But not via builtins.
764              ???  We just assume that this is true if we are not a
765              builtin function ourself.  */
766           || (!DECL_BUILT_IN (cfun->decl)
767               && (fndecl = gimple_call_fndecl (call))
768               && DECL_BUILT_IN (fndecl))))
769     goto process_args;
770
771   /* Check if base is a global static variable that is not read
772      by the function.  */
773   if (TREE_CODE (base) == VAR_DECL
774       && TREE_STATIC (base)
775       && !TREE_PUBLIC (base))
776     {
777       tree callee = gimple_call_fndecl (call);
778       bitmap not_read;
779
780       if (callee != NULL_TREE
781           && (not_read
782                 = ipa_reference_get_not_read_global (cgraph_node (callee)))
783           && bitmap_bit_p (not_read, DECL_UID (base)))
784         goto process_args;
785     }
786
787   /* If the base variable is call-used or call-clobbered then
788      it may be used.  */
789   if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
790     {
791       if (is_call_used (base))
792         return true;
793     }
794   else
795     {
796       if (is_call_clobbered (base))
797         return true;
798     }
799
800   /* Inspect call arguments for passed-by-value aliases.  */
801 process_args:
802   for (i = 0; i < gimple_call_num_args (call); ++i)
803     {
804       tree op = gimple_call_arg (call, i);
805
806       if (TREE_CODE (op) == EXC_PTR_EXPR
807           || TREE_CODE (op) == FILTER_EXPR)
808         continue;
809
810       if (TREE_CODE (op) == WITH_SIZE_EXPR)
811         op = TREE_OPERAND (op, 0);
812
813       if (TREE_CODE (op) != SSA_NAME
814           && !is_gimple_min_invariant (op)
815           && refs_may_alias_p (op, ref))
816         return true;
817     }
818
819   return false;
820 }
821
822 static bool
823 ref_maybe_used_by_call_p (gimple call, tree ref)
824 {
825   bool res = ref_maybe_used_by_call_p_1 (call, ref);
826   if (res)
827     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
828   else
829     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
830   return res;
831 }
832
833
834 /* If the statement STMT may use the memory reference REF return
835    true, otherwise return false.  */
836
837 bool
838 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
839 {
840   if (is_gimple_assign (stmt))
841     {
842       tree rhs;
843
844       /* All memory assign statements are single.  */
845       if (!gimple_assign_single_p (stmt))
846         return false;
847
848       rhs = gimple_assign_rhs1 (stmt);
849       if (is_gimple_reg (rhs)
850           || is_gimple_min_invariant (rhs)
851           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
852         return false;
853
854       return refs_may_alias_p (rhs, ref);
855     }
856   else if (is_gimple_call (stmt))
857     return ref_maybe_used_by_call_p (stmt, ref);
858
859   return true;
860 }
861
862 /* If the call in statement CALL may clobber the memory reference REF
863    return true, otherwise return false.  */
864
865 static bool
866 call_may_clobber_ref_p_1 (gimple call, tree ref)
867 {
868   tree fndecl, base;
869
870   /* If the call is pure or const it cannot clobber anything.  */
871   if (gimple_call_flags (call)
872       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
873     return false;
874
875   base = get_base_address (ref);
876   if (!base)
877     return true;
878
879   if (TREE_CODE (base) == SSA_NAME
880       || CONSTANT_CLASS_P (base))
881     return false;
882
883   /* If the reference is based on a decl that is not aliased the call
884      cannot possibly clobber it.  */
885   if (DECL_P (base)
886       && !may_be_aliased (base)
887       /* But local non-readonly statics can be modified through recursion!  */
888       && (TREE_READONLY (base)
889           || !is_global_var (base)
890           /* But not via builtins.
891              ???  We just assume that this is true if we are not a
892              builtin function ourself.  */
893           || (!DECL_BUILT_IN (cfun->decl)
894               && (fndecl = gimple_call_fndecl (call))
895               && DECL_BUILT_IN (fndecl))))
896     return false;
897
898   /* Check if base is a global static variable that is not written
899      by the function.  */
900   if (TREE_CODE (base) == VAR_DECL
901       && TREE_STATIC (base)
902       && !TREE_PUBLIC (base))
903     {
904       tree callee = gimple_call_fndecl (call);
905       bitmap not_written;
906
907       if (callee != NULL_TREE
908           && (not_written
909                 = ipa_reference_get_not_written_global (cgraph_node (callee)))
910           && bitmap_bit_p (not_written, DECL_UID (base)))
911         return false;
912     }
913
914   if (DECL_P (base))
915     return is_call_clobbered (base);
916
917   return true;
918 }
919
920 static bool
921 call_may_clobber_ref_p (gimple call, tree ref)
922 {
923   bool res = call_may_clobber_ref_p_1 (call, ref);
924   if (res)
925     ++alias_stats.call_may_clobber_ref_p_may_alias;
926   else
927     ++alias_stats.call_may_clobber_ref_p_no_alias;
928   return res;
929 }
930
931
932 /* If the statement STMT may clobber the memory reference REF return true,
933    otherwise return false.  */
934
935 bool
936 stmt_may_clobber_ref_p (gimple stmt, tree ref)
937 {
938   if (is_gimple_call (stmt))
939     {
940       tree lhs = gimple_call_lhs (stmt);
941       if (lhs
942           && !is_gimple_reg (lhs)
943           && refs_may_alias_p (ref, lhs))
944         return true;
945
946       return call_may_clobber_ref_p (stmt, ref);
947     }
948   else if (is_gimple_assign (stmt))
949     return refs_may_alias_p (ref, gimple_assign_lhs (stmt));
950   else if (gimple_code (stmt) == GIMPLE_ASM)
951     return true;
952
953   return false;
954 }
955
956 static tree get_continuation_for_phi (gimple, tree, bitmap *);
957
958 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
959    TARGET or a statement clobbering the memory reference REF in which
960    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
961
962 static bool
963 maybe_skip_until (gimple phi, tree target, tree ref, tree vuse, bitmap *visited)
964 {
965   if (!*visited)
966     *visited = BITMAP_ALLOC (NULL);
967
968   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
969
970   /* Walk until we hit the target.  */
971   while (vuse != target)
972     {
973       gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
974       /* Recurse for PHI nodes.  */
975       if (gimple_code (def_stmt) == GIMPLE_PHI)
976         {
977           /* An already visited PHI node ends the walk successfully.  */
978           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
979             return true;
980           vuse = get_continuation_for_phi (def_stmt, ref, visited);
981           if (!vuse)
982             return false;
983           continue;
984         }
985       /* A clobbering statement or the end of the IL ends it failing.  */
986       else if (gimple_nop_p (def_stmt)
987                || stmt_may_clobber_ref_p (def_stmt, ref))
988         return false;
989       vuse = gimple_vuse (def_stmt);
990     }
991   return true;
992 }
993
994 /* Starting from a PHI node for the virtual operand of the memory reference
995    REF find a continuation virtual operand that allows to continue walking
996    statements dominating PHI skipping only statements that cannot possibly
997    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
998    be found.  */
999
1000 static tree
1001 get_continuation_for_phi (gimple phi, tree ref, bitmap *visited)
1002 {
1003   unsigned nargs = gimple_phi_num_args (phi);
1004
1005   /* Through a single-argument PHI we can simply look through.  */
1006   if (nargs == 1)
1007     return PHI_ARG_DEF (phi, 0);
1008
1009   /* For two arguments try to skip non-aliasing code until we hit
1010      the phi argument definition that dominates the other one.  */
1011   if (nargs == 2)
1012     {
1013       tree arg0 = PHI_ARG_DEF (phi, 0);
1014       tree arg1 = PHI_ARG_DEF (phi, 1);
1015       gimple def0 = SSA_NAME_DEF_STMT (arg0);
1016       gimple def1 = SSA_NAME_DEF_STMT (arg1);
1017
1018       if (arg0 == arg1)
1019         return arg0;
1020       else if (gimple_nop_p (def0)
1021                || (!gimple_nop_p (def1)
1022                    && dominated_by_p (CDI_DOMINATORS,
1023                                       gimple_bb (def1), gimple_bb (def0))))
1024         {
1025           if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1026             return arg0;
1027         }
1028       else if (gimple_nop_p (def1)
1029                || dominated_by_p (CDI_DOMINATORS,
1030                                   gimple_bb (def0), gimple_bb (def1)))
1031         {
1032           if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1033             return arg1;
1034         }
1035     }
1036
1037   return NULL_TREE;
1038 }
1039
1040 /* Based on the memory reference REF and its virtual use VUSE call
1041    WALKER for each virtual use that is equivalent to VUSE, including VUSE
1042    itself.  That is, for each virtual use for which its defining statement
1043    does not clobber REF.
1044
1045    WALKER is called with REF, the current virtual use and DATA.  If
1046    WALKER returns non-NULL the walk stops and its result is returned.
1047    At the end of a non-successful walk NULL is returned.
1048
1049    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
1050
1051 void *
1052 walk_non_aliased_vuses (tree ref, tree vuse,
1053                         void *(*walker)(tree, tree, void *), void *data)
1054 {
1055   bitmap visited = NULL;
1056   void *res;
1057
1058   timevar_push (TV_ALIAS_STMT_WALK);
1059
1060   do
1061     {
1062       gimple def_stmt;
1063
1064       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1065       res = (*walker) (ref, vuse, data);
1066       if (res)
1067         break;
1068
1069       def_stmt = SSA_NAME_DEF_STMT (vuse);
1070       if (gimple_nop_p (def_stmt))
1071         break;
1072       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1073         vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1074       else
1075         {
1076           if (stmt_may_clobber_ref_p (def_stmt, ref))
1077             break;
1078           vuse = gimple_vuse (def_stmt);
1079         }
1080     }
1081   while (vuse);
1082
1083   if (visited)
1084     BITMAP_FREE (visited);
1085
1086   timevar_pop (TV_ALIAS_STMT_WALK);
1087
1088   return res;
1089 }
1090
1091
1092 /* Based on the memory reference REF call WALKER for each vdef which
1093    defining statement may clobber REF, starting with VDEF.  If REF
1094    is NULL_TREE, each defining statement is visited.
1095
1096    WALKER is called with REF, the current vdef and DATA.  If WALKER
1097    returns true the walk is stopped, otherwise it continues.
1098
1099    At PHI nodes walk_aliased_vdefs forks into one walk for reach
1100    PHI argument (but only one walk continues on merge points), the
1101    return value is true if any of the walks was successful.
1102
1103    The function returns the number of statements walked.  */
1104
1105 static unsigned int
1106 walk_aliased_vdefs_1 (tree ref, tree vdef,
1107                       bool (*walker)(tree, tree, void *), void *data,
1108                       bitmap *visited, unsigned int cnt)
1109 {
1110   do
1111     {
1112       gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1113
1114       if (*visited
1115           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1116         return cnt;
1117
1118       if (gimple_nop_p (def_stmt))
1119         return cnt;
1120       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1121         {
1122           unsigned i;
1123           if (!*visited)
1124             *visited = BITMAP_ALLOC (NULL);
1125           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1126             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1127                                          walker, data, visited, 0);
1128           return cnt;
1129         }
1130
1131       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1132       cnt++;
1133       if ((!ref
1134            || stmt_may_clobber_ref_p (def_stmt, ref))
1135           && (*walker) (ref, vdef, data))
1136         return cnt;
1137
1138       vdef = gimple_vuse (def_stmt);
1139     }
1140   while (1);
1141 }
1142
1143 unsigned int
1144 walk_aliased_vdefs (tree ref, tree vdef,
1145                     bool (*walker)(tree, tree, void *), void *data,
1146                     bitmap *visited)
1147 {
1148   bitmap local_visited = NULL;
1149   unsigned int ret;
1150
1151   timevar_push (TV_ALIAS_STMT_WALK);
1152
1153   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1154                               visited ? visited : &local_visited, 0);
1155   if (local_visited)
1156     BITMAP_FREE (local_visited);
1157
1158   timevar_pop (TV_ALIAS_STMT_WALK);
1159
1160   return ret;
1161 }
1162