OSDN Git Service

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