OSDN Git Service

2009-06-11 Ed Schonberg <schonberg@adacore.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   /* ???  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, "\nCall clobber information\n");
334
335   fprintf (file, "\nESCAPED");
336   dump_points_to_solution (file, &cfun->gimple_df->escaped);
337   fprintf (file, "\nCALLUSED");
338   dump_points_to_solution (file, &cfun->gimple_df->callused);
339
340   fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
341
342   for (i = 1; i < num_ssa_names; i++)
343     {
344       tree ptr = ssa_name (i);
345       struct ptr_info_def *pi;
346       
347       if (ptr == NULL_TREE
348           || SSA_NAME_IN_FREE_LIST (ptr))
349         continue;
350
351       pi = SSA_NAME_PTR_INFO (ptr);
352       if (pi)
353         dump_points_to_info_for (file, ptr);
354     }
355
356   fprintf (file, "\n");
357 }
358
359
360 /* Dump alias information on stderr.  */
361
362 void
363 debug_alias_info (void)
364 {
365   dump_alias_info (stderr);
366 }
367
368
369 /* Return the alias information associated with pointer T.  It creates a
370    new instance if none existed.  */
371
372 struct ptr_info_def *
373 get_ptr_info (tree t)
374 {
375   struct ptr_info_def *pi;
376
377   gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
378
379   pi = SSA_NAME_PTR_INFO (t);
380   if (pi == NULL)
381     {
382       pi = GGC_CNEW (struct ptr_info_def);
383       pt_solution_reset (&pi->pt);
384       SSA_NAME_PTR_INFO (t) = pi;
385     }
386
387   return pi;
388 }
389
390 /* Dump the points-to set *PT into FILE.  */
391
392 void
393 dump_points_to_solution (FILE *file, struct pt_solution *pt)
394 {
395   if (pt->anything)
396     fprintf (file, ", points-to anything");
397
398   if (pt->nonlocal)
399     fprintf (file, ", points-to non-local");
400
401   if (pt->escaped)
402     fprintf (file, ", points-to escaped");
403
404   if (pt->null)
405     fprintf (file, ", points-to NULL");
406
407   if (pt->vars)
408     {
409       fprintf (file, ", points-to vars: ");
410       dump_decl_set (file, pt->vars);
411       if (pt->vars_contains_global)
412         fprintf (file, " (includes global vars)");
413     }
414 }
415
416 /* Dump points-to information for SSA_NAME PTR into FILE.  */
417
418 void
419 dump_points_to_info_for (FILE *file, tree ptr)
420 {
421   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
422
423   print_generic_expr (file, ptr, dump_flags);
424
425   if (pi)
426     dump_points_to_solution (file, &pi->pt);
427   else
428     fprintf (file, ", points-to anything");
429
430   fprintf (file, "\n");
431 }
432
433
434 /* Dump points-to information for VAR into stderr.  */
435
436 void
437 debug_points_to_info_for (tree var)
438 {
439   dump_points_to_info_for (stderr, var);
440 }
441
442
443 /* Initializes the alias-oracle reference representation *R from REF.  */
444
445 void
446 ao_ref_init (ao_ref *r, tree ref)
447 {
448   r->ref = ref;
449   r->base = NULL_TREE;
450   r->offset = 0;
451   r->size = -1;
452   r->max_size = -1;
453   r->ref_alias_set = -1;
454   r->base_alias_set = -1;
455 }
456
457 /* Returns the base object of the memory reference *REF.  */
458
459 tree
460 ao_ref_base (ao_ref *ref)
461 {
462   if (ref->base)
463     return ref->base;
464   ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
465                                        &ref->max_size);
466   return ref->base;
467 }
468
469 /* Returns the base object alias set of the memory reference *REF.  */
470
471 static alias_set_type ATTRIBUTE_UNUSED
472 ao_ref_base_alias_set (ao_ref *ref)
473 {
474   if (ref->base_alias_set != -1)
475     return ref->base_alias_set;
476   ref->base_alias_set = get_alias_set (ao_ref_base (ref));
477   return ref->base_alias_set;
478 }
479
480 /* Returns the reference alias set of the memory reference *REF.  */
481
482 alias_set_type
483 ao_ref_alias_set (ao_ref *ref)
484 {
485   if (ref->ref_alias_set != -1)
486     return ref->ref_alias_set;
487   ref->ref_alias_set = get_alias_set (ref->ref);
488   return ref->ref_alias_set;
489 }
490
491 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
492    purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
493    decide.  */
494
495 static inline int
496 same_type_for_tbaa (tree type1, tree type2)
497 {
498   type1 = TYPE_MAIN_VARIANT (type1);
499   type2 = TYPE_MAIN_VARIANT (type2);
500
501   /* If we would have to do structural comparison bail out.  */
502   if (TYPE_STRUCTURAL_EQUALITY_P (type1)
503       || TYPE_STRUCTURAL_EQUALITY_P (type2))
504     return -1;
505
506   /* Compare the canonical types.  */
507   if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
508     return 1;
509
510   /* ???  Array types are not properly unified in all cases as we have
511      spurious changes in the index types for example.  Removing this
512      causes all sorts of problems with the Fortran frontend.  */
513   if (TREE_CODE (type1) == ARRAY_TYPE
514       && TREE_CODE (type2) == ARRAY_TYPE)
515     return -1;
516
517   /* The types are known to be not equal.  */
518   return 0;
519 }
520
521 /* Determine if the two component references REF1 and REF2 which are
522    based on access types TYPE1 and TYPE2 and of which at least one is based
523    on an indirect reference may alias.  */
524
525 static bool
526 nonaliasing_component_refs_p (tree ref1, tree type1,
527                               HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
528                               tree ref2, tree type2,
529                               HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
530 {
531   /* If one reference is a component references through pointers try to find a
532      common base and apply offset based disambiguation.  This handles
533      for example
534        struct A { int i; int j; } *q;
535        struct B { struct A a; int k; } *p;
536      disambiguating q->i and p->a.j.  */
537   tree *refp;
538   int same_p;
539
540   /* Now search for the type1 in the access path of ref2.  This
541      would be a common base for doing offset based disambiguation on.  */
542   refp = &ref2;
543   while (handled_component_p (*refp)
544          && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
545     refp = &TREE_OPERAND (*refp, 0);
546   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
547   /* If we couldn't compare types we have to bail out.  */
548   if (same_p == -1)
549     return true;
550   else if (same_p == 1)
551     {
552       HOST_WIDE_INT offadj, sztmp, msztmp;
553       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
554       offset2 -= offadj;
555       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
556     }
557   /* If we didn't find a common base, try the other way around.  */
558   refp = &ref1;
559   while (handled_component_p (*refp)
560          && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
561     refp = &TREE_OPERAND (*refp, 0);
562   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
563   /* If we couldn't compare types we have to bail out.  */
564   if (same_p == -1)
565     return true;
566   else if (same_p == 1)
567     {
568       HOST_WIDE_INT offadj, sztmp, msztmp;
569       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
570       offset1 -= offadj;
571       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
572     }
573   /* If we have two type access paths B1.path1 and B2.path2 they may
574      only alias if either B1 is in B2.path2 or B2 is in B1.path1.  */
575   return false;
576 }
577
578 /* Return true if two memory references based on the variables BASE1
579    and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
580    [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias.  */
581
582 static bool
583 decl_refs_may_alias_p (tree base1,
584                        HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
585                        tree base2,
586                        HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
587 {
588   gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
589
590   /* If both references are based on different variables, they cannot alias.  */
591   if (!operand_equal_p (base1, base2, 0))
592     return false;
593
594   /* If both references are based on the same variable, they cannot alias if
595      the accesses do not overlap.  */
596   return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
597 }
598
599 /* Return true if an indirect reference based on *PTR1 constrained
600    to [OFFSET1, OFFSET1 + MAX_SIZE1[ may alias a variable based on BASE2
601    constrained to [OFFSET2, OFFSET2 + MAX_SIZE2[.  *PTR1 and BASE2 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_ref_may_alias_decl_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 base2,
611                                HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
612                                alias_set_type base2_alias_set)
613 {
614   /* If only one reference is based on a variable, they cannot alias if
615      the pointer access is beyond the extent of the variable access.
616      (the pointer base cannot validly point to an offset less than zero
617      of the variable).
618      They also cannot alias if the pointer may not point to the decl.  */
619   if (max_size2 != -1
620       && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
621     return false;
622   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
623     return false;
624
625   /* Disambiguations that rely on strict aliasing rules follow.  */
626   if (!flag_strict_aliasing)
627     return true;
628
629   /* If the alias set for a pointer access is zero all bets are off.  */
630   if (base1_alias_set == -1)
631     base1_alias_set = get_deref_alias_set (ptr1);
632   if (base1_alias_set == 0)
633     return true;
634   if (base2_alias_set == -1)
635     base2_alias_set = get_alias_set (base2);
636
637   /* If both references are through the same type, they do not alias
638      if the accesses do not overlap.  This does extra disambiguation
639      for mixed/pointer accesses but requires strict aliasing.  */
640   if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
641                           TREE_TYPE (base2)) == 1)
642     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
643
644   /* The only way to access a variable is through a pointer dereference
645      of the same alias set or a subset of it.  */
646   if (base1_alias_set != base2_alias_set
647       && !alias_set_subset_of (base1_alias_set, base2_alias_set))
648     return false;
649
650   /* Do access-path based disambiguation.  */
651   if (ref1 && ref2
652       && handled_component_p (ref1)
653       && handled_component_p (ref2))
654     return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
655                                          offset1, max_size1,
656                                          ref2, TREE_TYPE (base2),
657                                          offset2, max_size2);
658
659   return true;
660 }
661
662 /* Return true if two indirect references based on *PTR1
663    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
664    [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias.  *PTR1 and *PTR2 have
665    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
666    in which case they are computed on-demand.  REF1 and REF2
667    if non-NULL are the complete memory reference trees. */
668
669 static bool
670 indirect_refs_may_alias_p (tree ref1, tree ptr1,
671                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
672                            alias_set_type base1_alias_set,
673                            tree ref2, tree ptr2,
674                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
675                            alias_set_type base2_alias_set)
676 {
677   /* If both bases are based on pointers they cannot alias if they may not
678      point to the same memory object or if they point to the same object
679      and the accesses do not overlap.  */
680   if (operand_equal_p (ptr1, ptr2, 0))
681     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
682   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
683     return false;
684
685   /* Disambiguations that rely on strict aliasing rules follow.  */
686   if (!flag_strict_aliasing)
687     return true;
688
689   /* If the alias set for a pointer access is zero all bets are off.  */
690   if (base1_alias_set == -1)
691     base1_alias_set = get_deref_alias_set (ptr1);
692   if (base1_alias_set == 0)
693     return true;
694   if (base2_alias_set == -1)
695     base2_alias_set = get_deref_alias_set (ptr2);
696   if (base2_alias_set == 0)
697     return true;
698
699   /* If both references are through the same type, they do not alias
700      if the accesses do not overlap.  This does extra disambiguation
701      for mixed/pointer accesses but requires strict aliasing.  */
702   if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
703                           TREE_TYPE (TREE_TYPE (ptr2))) == 1)
704     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
705
706   /* Do type-based disambiguation.  */
707   if (base1_alias_set != base2_alias_set
708       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
709     return false;
710
711   /* Do access-path based disambiguation.  */
712   if (ref1 && ref2
713       && handled_component_p (ref1)
714       && handled_component_p (ref2))
715     return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
716                                          offset1, max_size1,
717                                          ref2, TREE_TYPE (TREE_TYPE (ptr2)),
718                                          offset2, max_size2);
719
720   return true;
721 }
722
723 /* Return true, if the two memory references REF1 and REF2 may alias.  */
724
725 static bool
726 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
727 {
728   tree base1, base2;
729   HOST_WIDE_INT offset1 = 0, offset2 = 0;
730   HOST_WIDE_INT size1 = -1, size2 = -1;
731   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
732   bool var1_p, var2_p, ind1_p, ind2_p;
733   alias_set_type set;
734
735   gcc_assert ((!ref1->ref
736                || SSA_VAR_P (ref1->ref)
737                || handled_component_p (ref1->ref)
738                || INDIRECT_REF_P (ref1->ref)
739                || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
740               && (!ref2->ref
741                   || SSA_VAR_P (ref2->ref)
742                   || handled_component_p (ref2->ref)
743                   || INDIRECT_REF_P (ref2->ref)
744                   || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
745
746   /* Decompose the references into their base objects and the access.  */
747   base1 = ao_ref_base (ref1);
748   offset1 = ref1->offset;
749   size1 = ref1->size;
750   max_size1 = ref1->max_size;
751   base2 = ao_ref_base (ref2);
752   offset2 = ref2->offset;
753   size2 = ref2->size;
754   max_size2 = ref2->max_size;
755
756   /* We can end up with registers or constants as bases for example from
757      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
758      which is seen as a struct copy.  */
759   if (TREE_CODE (base1) == SSA_NAME
760       || TREE_CODE (base2) == SSA_NAME
761       || is_gimple_min_invariant (base1)
762       || is_gimple_min_invariant (base2))
763     return false;
764
765   /* Defer to simple offset based disambiguation if we have
766      references based on two decls.  Do this before defering to
767      TBAA to handle must-alias cases in conformance with the
768      GCC extension of allowing type-punning through unions.  */
769   var1_p = SSA_VAR_P (base1);
770   var2_p = SSA_VAR_P (base2);
771   if (var1_p && var2_p)
772     return decl_refs_may_alias_p (base1, offset1, max_size1,
773                                   base2, offset2, max_size2);
774
775   /* First defer to TBAA if possible.  */
776   if (tbaa_p
777       && flag_strict_aliasing
778       && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
779                                  ao_ref_alias_set (ref2)))
780     return false;
781
782   /* If one reference is a TARGET_MEM_REF weird things are allowed.  Still
783      TBAA disambiguation based on the access type is possible, so bail
784      out only after that check.  */
785   if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
786       || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
787     return true;
788
789   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
790   ind1_p = INDIRECT_REF_P (base1);
791   ind2_p = INDIRECT_REF_P (base2);
792   set = tbaa_p ? -1 : 0;
793   if (var1_p && ind2_p)
794     return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
795                                           offset2, max_size2, set,
796                                           ref1->ref, base1,
797                                           offset1, max_size1, set);
798   else if (ind1_p && var2_p)
799     return indirect_ref_may_alias_decl_p (ref1->ref, TREE_OPERAND (base1, 0),
800                                           offset1, max_size1, set,
801                                           ref2->ref, base2,
802                                           offset2, max_size2, set);
803   else if (ind1_p && ind2_p)
804     return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
805                                       offset1, max_size1, set,
806                                       ref2->ref, TREE_OPERAND (base2, 0),
807                                       offset2, max_size2, set);
808
809   gcc_unreachable ();
810 }
811
812 bool
813 refs_may_alias_p (tree ref1, tree ref2)
814 {
815   ao_ref r1, r2;
816   bool res;
817   ao_ref_init (&r1, ref1);
818   ao_ref_init (&r2, ref2);
819   res = refs_may_alias_p_1 (&r1, &r2, true);
820   if (res)
821     ++alias_stats.refs_may_alias_p_may_alias;
822   else
823     ++alias_stats.refs_may_alias_p_no_alias;
824   return res;
825 }
826
827 /* Returns true if there is a anti-dependence for the STORE that
828    executes after the LOAD.  */
829
830 bool
831 refs_anti_dependent_p (tree load, tree store)
832 {
833   ao_ref r1, r2;
834   ao_ref_init (&r1, load);
835   ao_ref_init (&r2, store);
836   return refs_may_alias_p_1 (&r1, &r2, false);
837 }
838
839 /* Returns true if there is a output dependence for the stores
840    STORE1 and STORE2.  */
841
842 bool
843 refs_output_dependent_p (tree store1, tree store2)
844 {
845   ao_ref r1, r2;
846   ao_ref_init (&r1, store1);
847   ao_ref_init (&r2, store2);
848   return refs_may_alias_p_1 (&r1, &r2, false);
849 }
850
851 /* If the call CALL may use the memory reference REF return true,
852    otherwise return false.  */
853
854 static bool
855 ref_maybe_used_by_call_p_1 (gimple call, tree ref)
856 {
857   tree base;
858   unsigned i;
859   int flags = gimple_call_flags (call);
860
861   /* Const functions without a static chain do not implicitly use memory.  */
862   if (!gimple_call_chain (call)
863       && (flags & (ECF_CONST|ECF_NOVOPS)))
864     goto process_args;
865
866   /* If the reference is not based on a decl give up.
867      ???  Handle indirect references by intersecting the call-used
868           solution with that of the pointer.  */
869   base = get_base_address (ref);
870   if (!base
871       || !DECL_P (base))
872     return true;
873
874   /* If the reference is based on a decl that is not aliased the call
875      cannot possibly use it.  */
876   if (DECL_P (base)
877       && !may_be_aliased (base)
878       /* But local statics can be used through recursion.  */
879       && !is_global_var (base))
880     goto process_args;
881
882   /* Check if base is a global static variable that is not read
883      by the function.  */
884   if (TREE_CODE (base) == VAR_DECL
885       && TREE_STATIC (base)
886       && !TREE_PUBLIC (base))
887     {
888       tree callee = gimple_call_fndecl (call);
889       bitmap not_read;
890
891       if (callee != NULL_TREE
892           && (not_read
893                 = ipa_reference_get_not_read_global (cgraph_node (callee)))
894           && bitmap_bit_p (not_read, DECL_UID (base)))
895         goto process_args;
896     }
897
898   /* If the base variable is call-used or call-clobbered then
899      it may be used.  */
900   if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
901     {
902       if (is_call_used (base))
903         return true;
904     }
905   else
906     {
907       if (is_call_clobbered (base))
908         return true;
909     }
910
911   /* Inspect call arguments for passed-by-value aliases.  */
912 process_args:
913   for (i = 0; i < gimple_call_num_args (call); ++i)
914     {
915       tree op = gimple_call_arg (call, i);
916
917       if (TREE_CODE (op) == EXC_PTR_EXPR
918           || TREE_CODE (op) == FILTER_EXPR)
919         continue;
920
921       if (TREE_CODE (op) == WITH_SIZE_EXPR)
922         op = TREE_OPERAND (op, 0);
923
924       if (TREE_CODE (op) != SSA_NAME
925           && !is_gimple_min_invariant (op)
926           && refs_may_alias_p (op, ref))
927         return true;
928     }
929
930   return false;
931 }
932
933 static bool
934 ref_maybe_used_by_call_p (gimple call, tree ref)
935 {
936   bool res = ref_maybe_used_by_call_p_1 (call, ref);
937   if (res)
938     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
939   else
940     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
941   return res;
942 }
943
944
945 /* If the statement STMT may use the memory reference REF return
946    true, otherwise return false.  */
947
948 bool
949 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
950 {
951   if (is_gimple_assign (stmt))
952     {
953       tree rhs;
954
955       /* All memory assign statements are single.  */
956       if (!gimple_assign_single_p (stmt))
957         return false;
958
959       rhs = gimple_assign_rhs1 (stmt);
960       if (is_gimple_reg (rhs)
961           || is_gimple_min_invariant (rhs)
962           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
963         return false;
964
965       return refs_may_alias_p (rhs, ref);
966     }
967   else if (is_gimple_call (stmt))
968     return ref_maybe_used_by_call_p (stmt, ref);
969
970   return true;
971 }
972
973 /* If the call in statement CALL may clobber the memory reference REF
974    return true, otherwise return false.  */
975
976 static bool
977 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
978 {
979   tree base;
980
981   /* If the call is pure or const it cannot clobber anything.  */
982   if (gimple_call_flags (call)
983       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
984     return false;
985
986   base = ao_ref_base (ref);
987   if (!base)
988     return true;
989
990   if (TREE_CODE (base) == SSA_NAME
991       || CONSTANT_CLASS_P (base))
992     return false;
993
994   /* If the reference is based on a decl that is not aliased the call
995      cannot possibly clobber it.  */
996   if (DECL_P (base)
997       && !may_be_aliased (base)
998       /* But local non-readonly statics can be modified through recursion
999          or the call may implement a threading barrier which we must
1000          treat as may-def.  */
1001       && (TREE_READONLY (base)
1002           || !is_global_var (base)))
1003     return false;
1004
1005   /* Check if base is a global static variable that is not written
1006      by the function.  */
1007   if (TREE_CODE (base) == VAR_DECL
1008       && TREE_STATIC (base)
1009       && !TREE_PUBLIC (base))
1010     {
1011       tree callee = gimple_call_fndecl (call);
1012       bitmap not_written;
1013
1014       if (callee != NULL_TREE
1015           && (not_written
1016                 = ipa_reference_get_not_written_global (cgraph_node (callee)))
1017           && bitmap_bit_p (not_written, DECL_UID (base)))
1018         return false;
1019     }
1020
1021   if (DECL_P (base))
1022     return is_call_clobbered (base);
1023
1024   return true;
1025 }
1026
1027 static bool ATTRIBUTE_UNUSED
1028 call_may_clobber_ref_p (gimple call, tree ref)
1029 {
1030   bool res;
1031   ao_ref r;
1032   ao_ref_init (&r, ref);
1033   res = call_may_clobber_ref_p_1 (call, &r);
1034   if (res)
1035     ++alias_stats.call_may_clobber_ref_p_may_alias;
1036   else
1037     ++alias_stats.call_may_clobber_ref_p_no_alias;
1038   return res;
1039 }
1040
1041
1042 /* If the statement STMT may clobber the memory reference REF return true,
1043    otherwise return false.  */
1044
1045 bool
1046 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1047 {
1048   if (is_gimple_call (stmt))
1049     {
1050       tree lhs = gimple_call_lhs (stmt);
1051       if (lhs
1052           && !is_gimple_reg (lhs))
1053         {
1054           ao_ref r;
1055           ao_ref_init (&r, lhs);
1056           if (refs_may_alias_p_1 (ref, &r, true))
1057             return true;
1058         }
1059
1060       return call_may_clobber_ref_p_1 (stmt, ref);
1061     }
1062   else if (is_gimple_assign (stmt))
1063     {
1064       ao_ref r;
1065       ao_ref_init (&r, gimple_assign_lhs (stmt));
1066       return refs_may_alias_p_1 (ref, &r, true);
1067     }
1068   else if (gimple_code (stmt) == GIMPLE_ASM)
1069     return true;
1070
1071   return false;
1072 }
1073
1074 bool
1075 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1076 {
1077   ao_ref r;
1078   ao_ref_init (&r, ref);
1079   return stmt_may_clobber_ref_p_1 (stmt, &r);
1080 }
1081
1082
1083 static tree get_continuation_for_phi (gimple, ao_ref *, bitmap *);
1084
1085 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1086    TARGET or a statement clobbering the memory reference REF in which
1087    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
1088
1089 static bool
1090 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1091                   tree vuse, bitmap *visited)
1092 {
1093   if (!*visited)
1094     *visited = BITMAP_ALLOC (NULL);
1095
1096   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1097
1098   /* Walk until we hit the target.  */
1099   while (vuse != target)
1100     {
1101       gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1102       /* Recurse for PHI nodes.  */
1103       if (gimple_code (def_stmt) == GIMPLE_PHI)
1104         {
1105           /* An already visited PHI node ends the walk successfully.  */
1106           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1107             return true;
1108           vuse = get_continuation_for_phi (def_stmt, ref, visited);
1109           if (!vuse)
1110             return false;
1111           continue;
1112         }
1113       /* A clobbering statement or the end of the IL ends it failing.  */
1114       else if (gimple_nop_p (def_stmt)
1115                || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1116         return false;
1117       vuse = gimple_vuse (def_stmt);
1118     }
1119   return true;
1120 }
1121
1122 /* Starting from a PHI node for the virtual operand of the memory reference
1123    REF find a continuation virtual operand that allows to continue walking
1124    statements dominating PHI skipping only statements that cannot possibly
1125    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
1126    be found.  */
1127
1128 static tree
1129 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1130 {
1131   unsigned nargs = gimple_phi_num_args (phi);
1132
1133   /* Through a single-argument PHI we can simply look through.  */
1134   if (nargs == 1)
1135     return PHI_ARG_DEF (phi, 0);
1136
1137   /* For two arguments try to skip non-aliasing code until we hit
1138      the phi argument definition that dominates the other one.  */
1139   if (nargs == 2)
1140     {
1141       tree arg0 = PHI_ARG_DEF (phi, 0);
1142       tree arg1 = PHI_ARG_DEF (phi, 1);
1143       gimple def0 = SSA_NAME_DEF_STMT (arg0);
1144       gimple def1 = SSA_NAME_DEF_STMT (arg1);
1145
1146       if (arg0 == arg1)
1147         return arg0;
1148       else if (gimple_nop_p (def0)
1149                || (!gimple_nop_p (def1)
1150                    && dominated_by_p (CDI_DOMINATORS,
1151                                       gimple_bb (def1), gimple_bb (def0))))
1152         {
1153           if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1154             return arg0;
1155         }
1156       else if (gimple_nop_p (def1)
1157                || dominated_by_p (CDI_DOMINATORS,
1158                                   gimple_bb (def0), gimple_bb (def1)))
1159         {
1160           if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1161             return arg1;
1162         }
1163     }
1164
1165   return NULL_TREE;
1166 }
1167
1168 /* Based on the memory reference REF and its virtual use VUSE call
1169    WALKER for each virtual use that is equivalent to VUSE, including VUSE
1170    itself.  That is, for each virtual use for which its defining statement
1171    does not clobber REF.
1172
1173    WALKER is called with REF, the current virtual use and DATA.  If
1174    WALKER returns non-NULL the walk stops and its result is returned.
1175    At the end of a non-successful walk NULL is returned.
1176
1177    TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1178    use which definition is a statement that may clobber REF and DATA.
1179    If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1180    If TRANSLATE returns non-NULL the walk stops and its result is returned.
1181    If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1182    to adjust REF and *DATA to make that valid.
1183
1184    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
1185
1186 void *
1187 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1188                         void *(*walker)(ao_ref *, tree, void *),
1189                         void *(*translate)(ao_ref *, tree, void *), void *data)
1190 {
1191   bitmap visited = NULL;
1192   void *res;
1193
1194   timevar_push (TV_ALIAS_STMT_WALK);
1195
1196   do
1197     {
1198       gimple def_stmt;
1199
1200       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1201       res = (*walker) (ref, vuse, data);
1202       if (res)
1203         break;
1204
1205       def_stmt = SSA_NAME_DEF_STMT (vuse);
1206       if (gimple_nop_p (def_stmt))
1207         break;
1208       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1209         vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1210       else
1211         {
1212           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1213             {
1214               if (!translate)
1215                 break;
1216               res = (*translate) (ref, vuse, data);
1217               /* Failed lookup and translation.  */
1218               if (res == (void *)-1)
1219                 {
1220                   res = NULL;
1221                   break;
1222                 }
1223               /* Lookup succeeded.  */
1224               else if (res != NULL)
1225                 break;
1226               /* Translation succeeded, continue walking.  */
1227             }
1228           vuse = gimple_vuse (def_stmt);
1229         }
1230     }
1231   while (vuse);
1232
1233   if (visited)
1234     BITMAP_FREE (visited);
1235
1236   timevar_pop (TV_ALIAS_STMT_WALK);
1237
1238   return res;
1239 }
1240
1241
1242 /* Based on the memory reference REF call WALKER for each vdef which
1243    defining statement may clobber REF, starting with VDEF.  If REF
1244    is NULL_TREE, each defining statement is visited.
1245
1246    WALKER is called with REF, the current vdef and DATA.  If WALKER
1247    returns true the walk is stopped, otherwise it continues.
1248
1249    At PHI nodes walk_aliased_vdefs forks into one walk for reach
1250    PHI argument (but only one walk continues on merge points), the
1251    return value is true if any of the walks was successful.
1252
1253    The function returns the number of statements walked.  */
1254
1255 static unsigned int
1256 walk_aliased_vdefs_1 (tree ref, tree vdef,
1257                       bool (*walker)(tree, tree, void *), void *data,
1258                       bitmap *visited, unsigned int cnt)
1259 {
1260   do
1261     {
1262       gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1263
1264       if (*visited
1265           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1266         return cnt;
1267
1268       if (gimple_nop_p (def_stmt))
1269         return cnt;
1270       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1271         {
1272           unsigned i;
1273           if (!*visited)
1274             *visited = BITMAP_ALLOC (NULL);
1275           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1276             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1277                                          walker, data, visited, 0);
1278           return cnt;
1279         }
1280
1281       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1282       cnt++;
1283       if ((!ref
1284            || stmt_may_clobber_ref_p (def_stmt, ref))
1285           && (*walker) (ref, vdef, data))
1286         return cnt;
1287
1288       vdef = gimple_vuse (def_stmt);
1289     }
1290   while (1);
1291 }
1292
1293 unsigned int
1294 walk_aliased_vdefs (tree ref, tree vdef,
1295                     bool (*walker)(tree, tree, void *), void *data,
1296                     bitmap *visited)
1297 {
1298   bitmap local_visited = NULL;
1299   unsigned int ret;
1300
1301   timevar_push (TV_ALIAS_STMT_WALK);
1302
1303   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1304                               visited ? visited : &local_visited, 0);
1305   if (local_visited)
1306     BITMAP_FREE (local_visited);
1307
1308   timevar_pop (TV_ALIAS_STMT_WALK);
1309
1310   return ret;
1311 }
1312