OSDN Git Service

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