OSDN Git Service

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