OSDN Git Service

2012-12-15 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, 2011
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 "target.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "tree-pretty-print.h"
36 #include "tree-dump.h"
37 #include "gimple.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "tree-pass.h"
41 #include "convert.h"
42 #include "params.h"
43 #include "vec.h"
44 #include "bitmap.h"
45 #include "vecprim.h"
46 #include "pointer-set.h"
47 #include "alloc-pool.h"
48 #include "tree-ssa-alias.h"
49
50 /* Broad overview of how alias analysis on gimple works:
51
52    Statements clobbering or using memory are linked through the
53    virtual operand factored use-def chain.  The virtual operand
54    is unique per function, its symbol is accessible via gimple_vop (cfun).
55    Virtual operands are used for efficiently walking memory statements
56    in the gimple IL and are useful for things like value-numbering as
57    a generation count for memory references.
58
59    SSA_NAME pointers may have associated points-to information
60    accessible via the SSA_NAME_PTR_INFO macro.  Flow-insensitive
61    points-to information is (re-)computed by the TODO_rebuild_alias
62    pass manager todo.  Points-to information is also used for more
63    precise tracking of call-clobbered and call-used variables and
64    related disambiguations.
65
66    This file contains functions for disambiguating memory references,
67    the so called alias-oracle and tools for walking of the gimple IL.
68
69    The main alias-oracle entry-points are
70
71    bool stmt_may_clobber_ref_p (gimple, tree)
72
73      This function queries if a statement may invalidate (parts of)
74      the memory designated by the reference tree argument.
75
76    bool ref_maybe_used_by_stmt_p (gimple, tree)
77
78      This function queries if a statement may need (parts of) the
79      memory designated by the reference tree argument.
80
81    There are variants of these functions that only handle the call
82    part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
83    Note that these do not disambiguate against a possible call lhs.
84
85    bool refs_may_alias_p (tree, tree)
86
87      This function tries to disambiguate two reference trees.
88
89    bool ptr_deref_may_alias_global_p (tree)
90
91      This function queries if dereferencing a pointer variable may
92      alias global memory.
93
94    More low-level disambiguators are available and documented in
95    this file.  Low-level disambiguators dealing with points-to
96    information are in tree-ssa-structalias.c.  */
97
98
99 /* Query statistics for the different low-level disambiguators.
100    A high-level query may trigger multiple of them.  */
101
102 static struct {
103   unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
104   unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
105   unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
106   unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
107   unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
108   unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
109 } alias_stats;
110
111 void
112 dump_alias_stats (FILE *s)
113 {
114   fprintf (s, "\nAlias oracle query stats:\n");
115   fprintf (s, "  refs_may_alias_p: "
116            HOST_WIDE_INT_PRINT_DEC" disambiguations, "
117            HOST_WIDE_INT_PRINT_DEC" queries\n",
118            alias_stats.refs_may_alias_p_no_alias,
119            alias_stats.refs_may_alias_p_no_alias
120            + alias_stats.refs_may_alias_p_may_alias);
121   fprintf (s, "  ref_maybe_used_by_call_p: "
122            HOST_WIDE_INT_PRINT_DEC" disambiguations, "
123            HOST_WIDE_INT_PRINT_DEC" queries\n",
124            alias_stats.ref_maybe_used_by_call_p_no_alias,
125            alias_stats.refs_may_alias_p_no_alias
126            + alias_stats.ref_maybe_used_by_call_p_may_alias);
127   fprintf (s, "  call_may_clobber_ref_p: "
128            HOST_WIDE_INT_PRINT_DEC" disambiguations, "
129            HOST_WIDE_INT_PRINT_DEC" queries\n",
130            alias_stats.call_may_clobber_ref_p_no_alias,
131            alias_stats.call_may_clobber_ref_p_no_alias
132            + alias_stats.call_may_clobber_ref_p_may_alias);
133 }
134
135
136 /* Return true, if dereferencing PTR may alias with a global variable.  */
137
138 bool
139 ptr_deref_may_alias_global_p (tree ptr)
140 {
141   struct ptr_info_def *pi;
142
143   /* If we end up with a pointer constant here that may point
144      to global memory.  */
145   if (TREE_CODE (ptr) != SSA_NAME)
146     return true;
147
148   pi = SSA_NAME_PTR_INFO (ptr);
149
150   /* If we do not have points-to information for this variable,
151      we have to punt.  */
152   if (!pi)
153     return true;
154
155   /* ???  This does not use TBAA to prune globals ptr may not access.  */
156   return pt_solution_includes_global (&pi->pt);
157 }
158
159 /* Return true if dereferencing PTR may alias DECL.
160    The caller is responsible for applying TBAA to see if PTR
161    may access DECL at all.  */
162
163 static bool
164 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
165 {
166   struct ptr_info_def *pi;
167
168   /* Conversions are irrelevant for points-to information and
169      data-dependence analysis can feed us those.  */
170   STRIP_NOPS (ptr);
171
172   /* Anything we do not explicilty handle aliases.  */
173   if ((TREE_CODE (ptr) != SSA_NAME
174        && TREE_CODE (ptr) != ADDR_EXPR
175        && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
176       || !POINTER_TYPE_P (TREE_TYPE (ptr))
177       || (TREE_CODE (decl) != VAR_DECL
178           && TREE_CODE (decl) != PARM_DECL
179           && TREE_CODE (decl) != RESULT_DECL))
180     return true;
181
182   /* Disregard pointer offsetting.  */
183   if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
184     {
185       do
186         {
187           ptr = TREE_OPERAND (ptr, 0);
188         }
189       while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
190       return ptr_deref_may_alias_decl_p (ptr, decl);
191     }
192
193   /* ADDR_EXPR pointers either just offset another pointer or directly
194      specify the pointed-to set.  */
195   if (TREE_CODE (ptr) == ADDR_EXPR)
196     {
197       tree base = get_base_address (TREE_OPERAND (ptr, 0));
198       if (base
199           && (TREE_CODE (base) == MEM_REF
200               || TREE_CODE (base) == TARGET_MEM_REF))
201         ptr = TREE_OPERAND (base, 0);
202       else if (base
203                && DECL_P (base))
204         return base == decl;
205       else if (base
206                && CONSTANT_CLASS_P (base))
207         return false;
208       else
209         return true;
210     }
211
212   /* Non-aliased variables can not be pointed to.  */
213   if (!may_be_aliased (decl))
214     return false;
215
216   /* If we do not have useful points-to information for this pointer
217      we cannot disambiguate anything else.  */
218   pi = SSA_NAME_PTR_INFO (ptr);
219   if (!pi)
220     return true;
221
222   return pt_solution_includes (&pi->pt, decl);
223 }
224
225 /* Return true if dereferenced PTR1 and PTR2 may alias.
226    The caller is responsible for applying TBAA to see if accesses
227    through PTR1 and PTR2 may conflict at all.  */
228
229 bool
230 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
231 {
232   struct ptr_info_def *pi1, *pi2;
233
234   /* Conversions are irrelevant for points-to information and
235      data-dependence analysis can feed us those.  */
236   STRIP_NOPS (ptr1);
237   STRIP_NOPS (ptr2);
238
239   /* Anything we do not explicilty handle aliases.  */
240   if ((TREE_CODE (ptr1) != SSA_NAME
241        && TREE_CODE (ptr1) != ADDR_EXPR
242        && TREE_CODE (ptr1) != POINTER_PLUS_EXPR)
243       || (TREE_CODE (ptr2) != SSA_NAME
244           && TREE_CODE (ptr2) != ADDR_EXPR
245           && TREE_CODE (ptr2) != POINTER_PLUS_EXPR)
246       || !POINTER_TYPE_P (TREE_TYPE (ptr1))
247       || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
248     return true;
249
250   /* Disregard pointer offsetting.  */
251   if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
252     {
253       do
254         {
255           ptr1 = TREE_OPERAND (ptr1, 0);
256         }
257       while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
258       return ptr_derefs_may_alias_p (ptr1, ptr2);
259     }
260   if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
261     {
262       do
263         {
264           ptr2 = TREE_OPERAND (ptr2, 0);
265         }
266       while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
267       return ptr_derefs_may_alias_p (ptr1, ptr2);
268     }
269
270   /* ADDR_EXPR pointers either just offset another pointer or directly
271      specify the pointed-to set.  */
272   if (TREE_CODE (ptr1) == ADDR_EXPR)
273     {
274       tree base = get_base_address (TREE_OPERAND (ptr1, 0));
275       if (base
276           && (TREE_CODE (base) == MEM_REF
277               || TREE_CODE (base) == TARGET_MEM_REF))
278         ptr1 = TREE_OPERAND (base, 0);
279       else if (base
280                && DECL_P (base))
281         return ptr_deref_may_alias_decl_p (ptr2, base);
282       else
283         return true;
284     }
285   if (TREE_CODE (ptr2) == ADDR_EXPR)
286     {
287       tree base = get_base_address (TREE_OPERAND (ptr2, 0));
288       if (base
289           && (TREE_CODE (base) == MEM_REF
290               || TREE_CODE (base) == TARGET_MEM_REF))
291         ptr2 = TREE_OPERAND (base, 0);
292       else if (base
293                && DECL_P (base))
294         return ptr_deref_may_alias_decl_p (ptr1, base);
295       else
296         return true;
297     }
298
299   /* We may end up with two empty points-to solutions for two same pointers.
300      In this case we still want to say both pointers alias, so shortcut
301      that here.  */
302   if (ptr1 == ptr2)
303     return true;
304
305   /* If we do not have useful points-to information for either pointer
306      we cannot disambiguate anything else.  */
307   pi1 = SSA_NAME_PTR_INFO (ptr1);
308   pi2 = SSA_NAME_PTR_INFO (ptr2);
309   if (!pi1 || !pi2)
310     return true;
311
312   /* ???  This does not use TBAA to prune decls from the intersection
313      that not both pointers may access.  */
314   return pt_solutions_intersect (&pi1->pt, &pi2->pt);
315 }
316
317 /* Return true if dereferencing PTR may alias *REF.
318    The caller is responsible for applying TBAA to see if PTR
319    may access *REF at all.  */
320
321 static bool
322 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
323 {
324   tree base = ao_ref_base (ref);
325
326   if (TREE_CODE (base) == MEM_REF
327       || TREE_CODE (base) == TARGET_MEM_REF)
328     return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
329   else if (DECL_P (base))
330     return ptr_deref_may_alias_decl_p (ptr, base);
331
332   return true;
333 }
334
335
336 /* Dump alias information on FILE.  */
337
338 void
339 dump_alias_info (FILE *file)
340 {
341   size_t i;
342   const char *funcname
343     = lang_hooks.decl_printable_name (current_function_decl, 2);
344   referenced_var_iterator rvi;
345   tree var;
346
347   fprintf (file, "\n\nAlias information for %s\n\n", funcname);
348
349   fprintf (file, "Aliased symbols\n\n");
350
351   FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
352     {
353       if (may_be_aliased (var))
354         dump_variable (file, var);
355     }
356
357   fprintf (file, "\nCall clobber information\n");
358
359   fprintf (file, "\nESCAPED");
360   dump_points_to_solution (file, &cfun->gimple_df->escaped);
361
362   fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
363
364   for (i = 1; i < num_ssa_names; i++)
365     {
366       tree ptr = ssa_name (i);
367       struct ptr_info_def *pi;
368
369       if (ptr == NULL_TREE
370           || SSA_NAME_IN_FREE_LIST (ptr))
371         continue;
372
373       pi = SSA_NAME_PTR_INFO (ptr);
374       if (pi)
375         dump_points_to_info_for (file, ptr);
376     }
377
378   fprintf (file, "\n");
379 }
380
381
382 /* Dump alias information on stderr.  */
383
384 DEBUG_FUNCTION void
385 debug_alias_info (void)
386 {
387   dump_alias_info (stderr);
388 }
389
390
391 /* Dump the points-to set *PT into FILE.  */
392
393 void
394 dump_points_to_solution (FILE *file, struct pt_solution *pt)
395 {
396   if (pt->anything)
397     fprintf (file, ", points-to anything");
398
399   if (pt->nonlocal)
400     fprintf (file, ", points-to non-local");
401
402   if (pt->escaped)
403     fprintf (file, ", points-to escaped");
404
405   if (pt->ipa_escaped)
406     fprintf (file, ", points-to unit escaped");
407
408   if (pt->null)
409     fprintf (file, ", points-to NULL");
410
411   if (pt->vars)
412     {
413       fprintf (file, ", points-to vars: ");
414       dump_decl_set (file, pt->vars);
415       if (pt->vars_contains_global)
416         fprintf (file, " (includes global vars)");
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 DEBUG_FUNCTION 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   r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
460 }
461
462 /* Returns the base object of the memory reference *REF.  */
463
464 tree
465 ao_ref_base (ao_ref *ref)
466 {
467   if (ref->base)
468     return ref->base;
469   ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
470                                        &ref->max_size);
471   return ref->base;
472 }
473
474 /* Returns the base object alias set of the memory reference *REF.  */
475
476 static alias_set_type
477 ao_ref_base_alias_set (ao_ref *ref)
478 {
479   tree base_ref;
480   if (ref->base_alias_set != -1)
481     return ref->base_alias_set;
482   if (!ref->ref)
483     return 0;
484   base_ref = ref->ref;
485   while (handled_component_p (base_ref))
486     base_ref = TREE_OPERAND (base_ref, 0);
487   ref->base_alias_set = get_alias_set (base_ref);
488   return ref->base_alias_set;
489 }
490
491 /* Returns the reference alias set of the memory reference *REF.  */
492
493 alias_set_type
494 ao_ref_alias_set (ao_ref *ref)
495 {
496   if (ref->ref_alias_set != -1)
497     return ref->ref_alias_set;
498   ref->ref_alias_set = get_alias_set (ref->ref);
499   return ref->ref_alias_set;
500 }
501
502 /* Init an alias-oracle reference representation from a gimple pointer
503    PTR and a gimple size SIZE in bytes.  If SIZE is NULL_TREE the the
504    size is assumed to be unknown.  The access is assumed to be only
505    to or after of the pointer target, not before it.  */
506
507 void
508 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
509 {
510   HOST_WIDE_INT t1, t2;
511   ref->ref = NULL_TREE;
512   if (TREE_CODE (ptr) == ADDR_EXPR)
513     ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
514                                          &ref->offset, &t1, &t2);
515   else
516     {
517       ref->base = build2 (MEM_REF, char_type_node,
518                           ptr, null_pointer_node);
519       ref->offset = 0;
520     }
521   if (size
522       && host_integerp (size, 0)
523       && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
524     ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
525   else
526     ref->max_size = ref->size = -1;
527   ref->ref_alias_set = 0;
528   ref->base_alias_set = 0;
529   ref->volatile_p = false;
530 }
531
532 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
533    purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
534    decide.  */
535
536 static inline int
537 same_type_for_tbaa (tree type1, tree type2)
538 {
539   type1 = TYPE_MAIN_VARIANT (type1);
540   type2 = TYPE_MAIN_VARIANT (type2);
541
542   /* If we would have to do structural comparison bail out.  */
543   if (TYPE_STRUCTURAL_EQUALITY_P (type1)
544       || TYPE_STRUCTURAL_EQUALITY_P (type2))
545     return -1;
546
547   /* Compare the canonical types.  */
548   if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
549     return 1;
550
551   /* ??? Array types are not properly unified in all cases as we have
552      spurious changes in the index types for example.  Removing this
553      causes all sorts of problems with the Fortran frontend.  */
554   if (TREE_CODE (type1) == ARRAY_TYPE
555       && TREE_CODE (type2) == ARRAY_TYPE)
556     return -1;
557
558   /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
559      object of one of its constrained subtypes, e.g. when a function with an
560      unconstrained parameter passed by reference is called on an object and
561      inlined.  But, even in the case of a fixed size, type and subtypes are
562      not equivalent enough as to share the same TYPE_CANONICAL, since this
563      would mean that conversions between them are useless, whereas they are
564      not (e.g. type and subtypes can have different modes).  So, in the end,
565      they are only guaranteed to have the same alias set.  */
566   if (get_alias_set (type1) == get_alias_set (type2))
567     return -1;
568
569   /* The types are known to be not equal.  */
570   return 0;
571 }
572
573 /* Determine if the two component references REF1 and REF2 which are
574    based on access types TYPE1 and TYPE2 and of which at least one is based
575    on an indirect reference may alias.  REF2 is the only one that can
576    be a decl in which case REF2_IS_DECL is true.
577    REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
578    are the respective alias sets.  */
579
580 static bool
581 aliasing_component_refs_p (tree ref1,
582                            alias_set_type ref1_alias_set,
583                            alias_set_type base1_alias_set,
584                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
585                            tree ref2,
586                            alias_set_type ref2_alias_set,
587                            alias_set_type base2_alias_set,
588                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
589                            bool ref2_is_decl)
590 {
591   /* If one reference is a component references through pointers try to find a
592      common base and apply offset based disambiguation.  This handles
593      for example
594        struct A { int i; int j; } *q;
595        struct B { struct A a; int k; } *p;
596      disambiguating q->i and p->a.j.  */
597   tree base1, base2;
598   tree type1, type2;
599   tree *refp;
600   int same_p;
601
602   /* Choose bases and base types to search for.  */
603   base1 = ref1;
604   while (handled_component_p (base1))
605     base1 = TREE_OPERAND (base1, 0);
606   type1 = TREE_TYPE (base1);
607   base2 = ref2;
608   while (handled_component_p (base2))
609     base2 = TREE_OPERAND (base2, 0);
610   type2 = TREE_TYPE (base2);
611
612   /* Now search for the type1 in the access path of ref2.  This
613      would be a common base for doing offset based disambiguation on.  */
614   refp = &ref2;
615   while (handled_component_p (*refp)
616          && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
617     refp = &TREE_OPERAND (*refp, 0);
618   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
619   /* If we couldn't compare types we have to bail out.  */
620   if (same_p == -1)
621     return true;
622   else if (same_p == 1)
623     {
624       HOST_WIDE_INT offadj, sztmp, msztmp;
625       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
626       offset2 -= offadj;
627       get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
628       offset1 -= offadj;
629       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
630     }
631   /* If we didn't find a common base, try the other way around.  */
632   refp = &ref1;
633   while (handled_component_p (*refp)
634          && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
635     refp = &TREE_OPERAND (*refp, 0);
636   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
637   /* If we couldn't compare types we have to bail out.  */
638   if (same_p == -1)
639     return true;
640   else if (same_p == 1)
641     {
642       HOST_WIDE_INT offadj, sztmp, msztmp;
643       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
644       offset1 -= offadj;
645       get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
646       offset2 -= offadj;
647       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
648     }
649
650   /* If we have two type access paths B1.path1 and B2.path2 they may
651      only alias if either B1 is in B2.path2 or B2 is in B1.path1.
652      But we can still have a path that goes B1.path1...B2.path2 with
653      a part that we do not see.  So we can only disambiguate now
654      if there is no B2 in the tail of path1 and no B1 on the
655      tail of path2.  */
656   if (base1_alias_set == ref2_alias_set
657       || alias_set_subset_of (base1_alias_set, ref2_alias_set))
658     return true;
659   /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
660   if (!ref2_is_decl)
661     return (base2_alias_set == ref1_alias_set
662             || alias_set_subset_of (base2_alias_set, ref1_alias_set));
663   return false;
664 }
665
666 /* Return true if two memory references based on the variables BASE1
667    and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
668    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  */
669
670 static bool
671 decl_refs_may_alias_p (tree base1,
672                        HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
673                        tree base2,
674                        HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
675 {
676   gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
677
678   /* If both references are based on different variables, they cannot alias.  */
679   if (base1 != base2)
680     return false;
681
682   /* If both references are based on the same variable, they cannot alias if
683      the accesses do not overlap.  */
684   return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
685 }
686
687 /* Return true if an indirect reference based on *PTR1 constrained
688    to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
689    constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
690    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
691    in which case they are computed on-demand.  REF1 and REF2
692    if non-NULL are the complete memory reference trees.  */
693
694 static bool
695 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
696                                HOST_WIDE_INT offset1,
697                                HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
698                                alias_set_type ref1_alias_set,
699                                alias_set_type base1_alias_set,
700                                tree ref2 ATTRIBUTE_UNUSED, tree base2,
701                                HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
702                                alias_set_type ref2_alias_set,
703                                alias_set_type base2_alias_set, bool tbaa_p)
704 {
705   tree ptr1;
706   tree ptrtype1, dbase2;
707   HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
708   HOST_WIDE_INT doffset1, doffset2;
709   double_int moff;
710
711   gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
712                         || TREE_CODE (base1) == TARGET_MEM_REF)
713                        && DECL_P (base2));
714
715   ptr1 = TREE_OPERAND (base1, 0);
716
717   /* The offset embedded in MEM_REFs can be negative.  Bias them
718      so that the resulting offset adjustment is positive.  */
719   moff = mem_ref_offset (base1);
720   moff = double_int_lshift (moff,
721                             BITS_PER_UNIT == 8
722                             ? 3 : exact_log2 (BITS_PER_UNIT),
723                             HOST_BITS_PER_DOUBLE_INT, true);
724   if (double_int_negative_p (moff))
725     offset2p += double_int_neg (moff).low;
726   else
727     offset1p += moff.low;
728
729   /* If only one reference is based on a variable, they cannot alias if
730      the pointer access is beyond the extent of the variable access.
731      (the pointer base cannot validly point to an offset less than zero
732      of the variable).
733      ???  IVOPTs creates bases that do not honor this restriction,
734      so do not apply this optimization for TARGET_MEM_REFs.  */
735   if (TREE_CODE (base1) != TARGET_MEM_REF
736       && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
737     return false;
738   /* They also cannot alias if the pointer may not point to the decl.  */
739   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
740     return false;
741
742   /* Disambiguations that rely on strict aliasing rules follow.  */
743   if (!flag_strict_aliasing || !tbaa_p)
744     return true;
745
746   ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
747
748   /* If the alias set for a pointer access is zero all bets are off.  */
749   if (base1_alias_set == -1)
750     base1_alias_set = get_deref_alias_set (ptrtype1);
751   if (base1_alias_set == 0)
752     return true;
753   if (base2_alias_set == -1)
754     base2_alias_set = get_alias_set (base2);
755
756   /* When we are trying to disambiguate an access with a pointer dereference
757      as base versus one with a decl as base we can use both the size
758      of the decl and its dynamic type for extra disambiguation.
759      ???  We do not know anything about the dynamic type of the decl
760      other than that its alias-set contains base2_alias_set as a subset
761      which does not help us here.  */
762   /* As we know nothing useful about the dynamic type of the decl just
763      use the usual conflict check rather than a subset test.
764      ???  We could introduce -fvery-strict-aliasing when the language
765      does not allow decls to have a dynamic type that differs from their
766      static type.  Then we can check 
767      !alias_set_subset_of (base1_alias_set, base2_alias_set) instead.  */
768   if (base1_alias_set != base2_alias_set
769       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
770     return false;
771   /* If the size of the access relevant for TBAA through the pointer
772      is bigger than the size of the decl we can't possibly access the
773      decl via that pointer.  */
774   if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
775       && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
776       && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
777       /* ???  This in turn may run afoul when a decl of type T which is
778          a member of union type U is accessed through a pointer to
779          type U and sizeof T is smaller than sizeof U.  */
780       && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
781       && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
782       && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
783     return false;
784
785   if (!ref2)
786     return true;
787
788   /* If the decl is accessed via a MEM_REF, reconstruct the base
789      we can use for TBAA and an appropriately adjusted offset.  */
790   dbase2 = ref2;
791   while (handled_component_p (dbase2))
792     dbase2 = TREE_OPERAND (dbase2, 0);
793   doffset1 = offset1;
794   doffset2 = offset2;
795   if (TREE_CODE (dbase2) == MEM_REF
796       || TREE_CODE (dbase2) == TARGET_MEM_REF)
797     {
798       double_int moff = mem_ref_offset (dbase2);
799       moff = double_int_lshift (moff,
800                                 BITS_PER_UNIT == 8
801                                 ? 3 : exact_log2 (BITS_PER_UNIT),
802                                 HOST_BITS_PER_DOUBLE_INT, true);
803       if (double_int_negative_p (moff))
804         doffset1 -= double_int_neg (moff).low;
805       else
806         doffset2 -= moff.low;
807     }
808
809   /* If either reference is view-converted, give up now.  */
810   if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
811       || same_type_for_tbaa (TREE_TYPE (dbase2),
812                              TREE_TYPE (reference_alias_ptr_type (dbase2))) != 1)
813     return true;
814
815   /* If both references are through the same type, they do not alias
816      if the accesses do not overlap.  This does extra disambiguation
817      for mixed/pointer accesses but requires strict aliasing.
818      For MEM_REFs we require that the component-ref offset we computed
819      is relative to the start of the type which we ensure by
820      comparing rvalue and access type and disregarding the constant
821      pointer offset.  */
822   if ((TREE_CODE (base1) != TARGET_MEM_REF
823        || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
824       && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
825     return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
826
827   /* Do access-path based disambiguation.  */
828   if (ref1 && ref2
829       && (handled_component_p (ref1) || handled_component_p (ref2)))
830     return aliasing_component_refs_p (ref1,
831                                       ref1_alias_set, base1_alias_set,
832                                       offset1, max_size1,
833                                       ref2,
834                                       ref2_alias_set, base2_alias_set,
835                                       offset2, max_size2, true);
836
837   return true;
838 }
839
840 /* Return true if two indirect references based on *PTR1
841    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
842    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
843    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
844    in which case they are computed on-demand.  REF1 and REF2
845    if non-NULL are the complete memory reference trees. */
846
847 static bool
848 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
849                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
850                            alias_set_type ref1_alias_set,
851                            alias_set_type base1_alias_set,
852                            tree ref2 ATTRIBUTE_UNUSED, tree base2,
853                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
854                            alias_set_type ref2_alias_set,
855                            alias_set_type base2_alias_set, bool tbaa_p)
856 {
857   tree ptr1;
858   tree ptr2;
859   tree ptrtype1, ptrtype2;
860
861   gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
862                         || TREE_CODE (base1) == TARGET_MEM_REF)
863                        && (TREE_CODE (base2) == MEM_REF
864                            || TREE_CODE (base2) == TARGET_MEM_REF));
865
866   ptr1 = TREE_OPERAND (base1, 0);
867   ptr2 = TREE_OPERAND (base2, 0);
868
869   /* If both bases are based on pointers they cannot alias if they may not
870      point to the same memory object or if they point to the same object
871      and the accesses do not overlap.  */
872   if ((!cfun || gimple_in_ssa_p (cfun))
873       && operand_equal_p (ptr1, ptr2, 0)
874       && (((TREE_CODE (base1) != TARGET_MEM_REF
875             || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
876            && (TREE_CODE (base2) != TARGET_MEM_REF
877                || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
878           || (TREE_CODE (base1) == TARGET_MEM_REF
879               && TREE_CODE (base2) == TARGET_MEM_REF
880               && (TMR_STEP (base1) == TMR_STEP (base2)
881                   || (TMR_STEP (base1) && TMR_STEP (base2)
882                       && operand_equal_p (TMR_STEP (base1),
883                                           TMR_STEP (base2), 0)))
884               && (TMR_INDEX (base1) == TMR_INDEX (base2)
885                   || (TMR_INDEX (base1) && TMR_INDEX (base2)
886                       && operand_equal_p (TMR_INDEX (base1),
887                                           TMR_INDEX (base2), 0)))
888               && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
889                   || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
890                       && operand_equal_p (TMR_INDEX2 (base1),
891                                           TMR_INDEX2 (base2), 0))))))
892     {
893       double_int moff;
894       /* The offset embedded in MEM_REFs can be negative.  Bias them
895          so that the resulting offset adjustment is positive.  */
896       moff = mem_ref_offset (base1);
897       moff = double_int_lshift (moff,
898                                 BITS_PER_UNIT == 8
899                                 ? 3 : exact_log2 (BITS_PER_UNIT),
900                                 HOST_BITS_PER_DOUBLE_INT, true);
901       if (double_int_negative_p (moff))
902         offset2 += double_int_neg (moff).low;
903       else
904         offset1 += moff.low;
905       moff = mem_ref_offset (base2);
906       moff = double_int_lshift (moff,
907                                 BITS_PER_UNIT == 8
908                                 ? 3 : exact_log2 (BITS_PER_UNIT),
909                                 HOST_BITS_PER_DOUBLE_INT, true);
910       if (double_int_negative_p (moff))
911         offset1 += double_int_neg (moff).low;
912       else
913         offset2 += moff.low;
914       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
915     }
916   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
917     return false;
918
919   /* Disambiguations that rely on strict aliasing rules follow.  */
920   if (!flag_strict_aliasing || !tbaa_p)
921     return true;
922
923   ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
924   ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
925
926   /* If the alias set for a pointer access is zero all bets are off.  */
927   if (base1_alias_set == -1)
928     base1_alias_set = get_deref_alias_set (ptrtype1);
929   if (base1_alias_set == 0)
930     return true;
931   if (base2_alias_set == -1)
932     base2_alias_set = get_deref_alias_set (ptrtype2);
933   if (base2_alias_set == 0)
934     return true;
935
936   /* If both references are through the same type, they do not alias
937      if the accesses do not overlap.  This does extra disambiguation
938      for mixed/pointer accesses but requires strict aliasing.  */
939   if ((TREE_CODE (base1) != TARGET_MEM_REF
940        || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
941       && (TREE_CODE (base2) != TARGET_MEM_REF
942           || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
943       && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
944       && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
945       && same_type_for_tbaa (TREE_TYPE (ptrtype1),
946                              TREE_TYPE (ptrtype2)) == 1)
947     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
948
949   /* Do type-based disambiguation.  */
950   if (base1_alias_set != base2_alias_set
951       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
952     return false;
953
954   /* Do access-path based disambiguation.  */
955   if (ref1 && ref2
956       && (handled_component_p (ref1) || handled_component_p (ref2))
957       && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
958       && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
959     return aliasing_component_refs_p (ref1,
960                                       ref1_alias_set, base1_alias_set,
961                                       offset1, max_size1,
962                                       ref2,
963                                       ref2_alias_set, base2_alias_set,
964                                       offset2, max_size2, false);
965
966   return true;
967 }
968
969 /* Return true, if the two memory references REF1 and REF2 may alias.  */
970
971 bool
972 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
973 {
974   tree base1, base2;
975   HOST_WIDE_INT offset1 = 0, offset2 = 0;
976   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
977   bool var1_p, var2_p, ind1_p, ind2_p;
978
979   gcc_checking_assert ((!ref1->ref
980                         || TREE_CODE (ref1->ref) == SSA_NAME
981                         || DECL_P (ref1->ref)
982                         || TREE_CODE (ref1->ref) == STRING_CST
983                         || handled_component_p (ref1->ref)
984                         || TREE_CODE (ref1->ref) == MEM_REF
985                         || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
986                        && (!ref2->ref
987                            || TREE_CODE (ref2->ref) == SSA_NAME
988                            || DECL_P (ref2->ref)
989                            || TREE_CODE (ref2->ref) == STRING_CST
990                            || handled_component_p (ref2->ref)
991                            || TREE_CODE (ref2->ref) == MEM_REF
992                            || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
993
994   /* Decompose the references into their base objects and the access.  */
995   base1 = ao_ref_base (ref1);
996   offset1 = ref1->offset;
997   max_size1 = ref1->max_size;
998   base2 = ao_ref_base (ref2);
999   offset2 = ref2->offset;
1000   max_size2 = ref2->max_size;
1001
1002   /* We can end up with registers or constants as bases for example from
1003      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1004      which is seen as a struct copy.  */
1005   if (TREE_CODE (base1) == SSA_NAME
1006       || TREE_CODE (base1) == CONST_DECL
1007       || TREE_CODE (base1) == CONSTRUCTOR
1008       || TREE_CODE (base1) == ADDR_EXPR
1009       || CONSTANT_CLASS_P (base1)
1010       || TREE_CODE (base2) == SSA_NAME
1011       || TREE_CODE (base2) == CONST_DECL
1012       || TREE_CODE (base2) == CONSTRUCTOR
1013       || TREE_CODE (base2) == ADDR_EXPR
1014       || CONSTANT_CLASS_P (base2))
1015     return false;
1016
1017   /* We can end up refering to code via function and label decls.
1018      As we likely do not properly track code aliases conservatively
1019      bail out.  */
1020   if (TREE_CODE (base1) == FUNCTION_DECL
1021       || TREE_CODE (base1) == LABEL_DECL
1022       || TREE_CODE (base2) == FUNCTION_DECL
1023       || TREE_CODE (base2) == LABEL_DECL)
1024     return true;
1025
1026   /* Two volatile accesses always conflict.  */
1027   if (ref1->volatile_p
1028       && ref2->volatile_p)
1029     return true;
1030
1031   /* Defer to simple offset based disambiguation if we have
1032      references based on two decls.  Do this before defering to
1033      TBAA to handle must-alias cases in conformance with the
1034      GCC extension of allowing type-punning through unions.  */
1035   var1_p = DECL_P (base1);
1036   var2_p = DECL_P (base2);
1037   if (var1_p && var2_p)
1038     return decl_refs_may_alias_p (base1, offset1, max_size1,
1039                                   base2, offset2, max_size2);
1040
1041   ind1_p = (TREE_CODE (base1) == MEM_REF
1042             || TREE_CODE (base1) == TARGET_MEM_REF);
1043   ind2_p = (TREE_CODE (base2) == MEM_REF
1044             || TREE_CODE (base2) == TARGET_MEM_REF);
1045
1046   /* Canonicalize the pointer-vs-decl case.  */
1047   if (ind1_p && var2_p)
1048     {
1049       HOST_WIDE_INT tmp1;
1050       tree tmp2;
1051       ao_ref *tmp3;
1052       tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1053       tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1054       tmp2 = base1; base1 = base2; base2 = tmp2;
1055       tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1056       var1_p = true;
1057       ind1_p = false;
1058       var2_p = false;
1059       ind2_p = true;
1060     }
1061
1062   /* First defer to TBAA if possible.  */
1063   if (tbaa_p
1064       && flag_strict_aliasing
1065       && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1066                                  ao_ref_alias_set (ref2)))
1067     return false;
1068
1069   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
1070   if (var1_p && ind2_p)
1071     return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1072                                           offset2, max_size2,
1073                                           ao_ref_alias_set (ref2), -1,
1074                                           ref1->ref, base1,
1075                                           offset1, max_size1,
1076                                           ao_ref_alias_set (ref1),
1077                                           ao_ref_base_alias_set (ref1),
1078                                           tbaa_p);
1079   else if (ind1_p && ind2_p)
1080     return indirect_refs_may_alias_p (ref1->ref, base1,
1081                                       offset1, max_size1,
1082                                       ao_ref_alias_set (ref1), -1,
1083                                       ref2->ref, base2,
1084                                       offset2, max_size2,
1085                                       ao_ref_alias_set (ref2), -1,
1086                                       tbaa_p);
1087
1088   /* We really do not want to end up here, but returning true is safe.  */
1089 #ifdef ENABLE_CHECKING
1090   gcc_unreachable ();
1091 #else
1092   return true;
1093 #endif
1094 }
1095
1096 bool
1097 refs_may_alias_p (tree ref1, tree ref2)
1098 {
1099   ao_ref r1, r2;
1100   bool res;
1101   ao_ref_init (&r1, ref1);
1102   ao_ref_init (&r2, ref2);
1103   res = refs_may_alias_p_1 (&r1, &r2, true);
1104   if (res)
1105     ++alias_stats.refs_may_alias_p_may_alias;
1106   else
1107     ++alias_stats.refs_may_alias_p_no_alias;
1108   return res;
1109 }
1110
1111 /* Returns true if there is a anti-dependence for the STORE that
1112    executes after the LOAD.  */
1113
1114 bool
1115 refs_anti_dependent_p (tree load, tree store)
1116 {
1117   ao_ref r1, r2;
1118   ao_ref_init (&r1, load);
1119   ao_ref_init (&r2, store);
1120   return refs_may_alias_p_1 (&r1, &r2, false);
1121 }
1122
1123 /* Returns true if there is a output dependence for the stores
1124    STORE1 and STORE2.  */
1125
1126 bool
1127 refs_output_dependent_p (tree store1, tree store2)
1128 {
1129   ao_ref r1, r2;
1130   ao_ref_init (&r1, store1);
1131   ao_ref_init (&r2, store2);
1132   return refs_may_alias_p_1 (&r1, &r2, false);
1133 }
1134
1135 /* If the call CALL may use the memory reference REF return true,
1136    otherwise return false.  */
1137
1138 static bool
1139 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1140 {
1141   tree base, callee;
1142   unsigned i;
1143   int flags = gimple_call_flags (call);
1144
1145   /* Const functions without a static chain do not implicitly use memory.  */
1146   if (!gimple_call_chain (call)
1147       && (flags & (ECF_CONST|ECF_NOVOPS)))
1148     goto process_args;
1149
1150   base = ao_ref_base (ref);
1151   if (!base)
1152     return true;
1153
1154   /* A call that is not without side-effects might involve volatile
1155      accesses and thus conflicts with all other volatile accesses.  */
1156   if (ref->volatile_p)
1157     return true;
1158
1159   /* If the reference is based on a decl that is not aliased the call
1160      cannot possibly use it.  */
1161   if (DECL_P (base)
1162       && !may_be_aliased (base)
1163       /* But local statics can be used through recursion.  */
1164       && !is_global_var (base))
1165     goto process_args;
1166
1167   callee = gimple_call_fndecl (call);
1168
1169   /* Handle those builtin functions explicitly that do not act as
1170      escape points.  See tree-ssa-structalias.c:find_func_aliases
1171      for the list of builtins we might need to handle here.  */
1172   if (callee != NULL_TREE
1173       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1174     switch (DECL_FUNCTION_CODE (callee))
1175       {
1176         /* All the following functions read memory pointed to by
1177            their second argument.  strcat/strncat additionally
1178            reads memory pointed to by the first argument.  */
1179         case BUILT_IN_STRCAT:
1180         case BUILT_IN_STRNCAT:
1181           {
1182             ao_ref dref;
1183             ao_ref_init_from_ptr_and_size (&dref,
1184                                            gimple_call_arg (call, 0),
1185                                            NULL_TREE);
1186             if (refs_may_alias_p_1 (&dref, ref, false))
1187               return true;
1188           }
1189           /* FALLTHRU */
1190         case BUILT_IN_STRCPY:
1191         case BUILT_IN_STRNCPY:
1192         case BUILT_IN_MEMCPY:
1193         case BUILT_IN_MEMMOVE:
1194         case BUILT_IN_MEMPCPY:
1195         case BUILT_IN_STPCPY:
1196         case BUILT_IN_STPNCPY:
1197         case BUILT_IN_TM_MEMCPY:
1198         case BUILT_IN_TM_MEMMOVE:
1199           {
1200             ao_ref dref;
1201             tree size = NULL_TREE;
1202             if (gimple_call_num_args (call) == 3)
1203               size = gimple_call_arg (call, 2);
1204             ao_ref_init_from_ptr_and_size (&dref,
1205                                            gimple_call_arg (call, 1),
1206                                            size);
1207             return refs_may_alias_p_1 (&dref, ref, false);
1208           }
1209         case BUILT_IN_STRCAT_CHK:
1210         case BUILT_IN_STRNCAT_CHK:
1211           {
1212             ao_ref dref;
1213             ao_ref_init_from_ptr_and_size (&dref,
1214                                            gimple_call_arg (call, 0),
1215                                            NULL_TREE);
1216             if (refs_may_alias_p_1 (&dref, ref, false))
1217               return true;
1218           }
1219           /* FALLTHRU */
1220         case BUILT_IN_STRCPY_CHK:
1221         case BUILT_IN_STRNCPY_CHK:
1222         case BUILT_IN_MEMCPY_CHK:
1223         case BUILT_IN_MEMMOVE_CHK:
1224         case BUILT_IN_MEMPCPY_CHK:
1225         case BUILT_IN_STPCPY_CHK:
1226           {
1227             ao_ref dref;
1228             tree size = NULL_TREE;
1229             if (gimple_call_num_args (call) == 4)
1230               size = gimple_call_arg (call, 2);
1231             ao_ref_init_from_ptr_and_size (&dref,
1232                                            gimple_call_arg (call, 1),
1233                                            size);
1234             return refs_may_alias_p_1 (&dref, ref, false);
1235           }
1236         case BUILT_IN_BCOPY:
1237           {
1238             ao_ref dref;
1239             tree size = gimple_call_arg (call, 2);
1240             ao_ref_init_from_ptr_and_size (&dref,
1241                                            gimple_call_arg (call, 0),
1242                                            size);
1243             return refs_may_alias_p_1 (&dref, ref, false);
1244           }
1245
1246         /* The following functions read memory pointed to by their
1247            first argument.  */
1248         CASE_BUILT_IN_TM_LOAD (1):
1249         CASE_BUILT_IN_TM_LOAD (2):
1250         CASE_BUILT_IN_TM_LOAD (4):
1251         CASE_BUILT_IN_TM_LOAD (8):
1252         CASE_BUILT_IN_TM_LOAD (FLOAT):
1253         CASE_BUILT_IN_TM_LOAD (DOUBLE):
1254         CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1255         CASE_BUILT_IN_TM_LOAD (M64):
1256         CASE_BUILT_IN_TM_LOAD (M128):
1257         CASE_BUILT_IN_TM_LOAD (M256):
1258         case BUILT_IN_TM_LOG:
1259         case BUILT_IN_TM_LOG_1:
1260         case BUILT_IN_TM_LOG_2:
1261         case BUILT_IN_TM_LOG_4:
1262         case BUILT_IN_TM_LOG_8:
1263         case BUILT_IN_TM_LOG_FLOAT:
1264         case BUILT_IN_TM_LOG_DOUBLE:
1265         case BUILT_IN_TM_LOG_LDOUBLE:
1266         case BUILT_IN_TM_LOG_M64:
1267         case BUILT_IN_TM_LOG_M128:
1268         case BUILT_IN_TM_LOG_M256:
1269           return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1270
1271         /* These read memory pointed to by the first argument.  */
1272         case BUILT_IN_STRDUP:
1273         case BUILT_IN_STRNDUP:
1274           {
1275             ao_ref dref;
1276             tree size = NULL_TREE;
1277             if (gimple_call_num_args (call) == 2)
1278               size = gimple_call_arg (call, 1);
1279             ao_ref_init_from_ptr_and_size (&dref,
1280                                            gimple_call_arg (call, 0),
1281                                            size);
1282             return refs_may_alias_p_1 (&dref, ref, false);
1283           }
1284         /* The following builtins do not read from memory.  */
1285         case BUILT_IN_FREE:
1286         case BUILT_IN_MALLOC:
1287         case BUILT_IN_CALLOC:
1288         case BUILT_IN_ALLOCA:
1289         case BUILT_IN_ALLOCA_WITH_ALIGN:
1290         case BUILT_IN_STACK_SAVE:
1291         case BUILT_IN_STACK_RESTORE:
1292         case BUILT_IN_MEMSET:
1293         case BUILT_IN_TM_MEMSET:
1294         case BUILT_IN_MEMSET_CHK:
1295         case BUILT_IN_FREXP:
1296         case BUILT_IN_FREXPF:
1297         case BUILT_IN_FREXPL:
1298         case BUILT_IN_GAMMA_R:
1299         case BUILT_IN_GAMMAF_R:
1300         case BUILT_IN_GAMMAL_R:
1301         case BUILT_IN_LGAMMA_R:
1302         case BUILT_IN_LGAMMAF_R:
1303         case BUILT_IN_LGAMMAL_R:
1304         case BUILT_IN_MODF:
1305         case BUILT_IN_MODFF:
1306         case BUILT_IN_MODFL:
1307         case BUILT_IN_REMQUO:
1308         case BUILT_IN_REMQUOF:
1309         case BUILT_IN_REMQUOL:
1310         case BUILT_IN_SINCOS:
1311         case BUILT_IN_SINCOSF:
1312         case BUILT_IN_SINCOSL:
1313         case BUILT_IN_ASSUME_ALIGNED:
1314         case BUILT_IN_VA_END:
1315           return false;
1316         /* __sync_* builtins and some OpenMP builtins act as threading
1317            barriers.  */
1318 #undef DEF_SYNC_BUILTIN
1319 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1320 #include "sync-builtins.def"
1321 #undef DEF_SYNC_BUILTIN
1322         case BUILT_IN_GOMP_ATOMIC_START:
1323         case BUILT_IN_GOMP_ATOMIC_END:
1324         case BUILT_IN_GOMP_BARRIER:
1325         case BUILT_IN_GOMP_TASKWAIT:
1326         case BUILT_IN_GOMP_CRITICAL_START:
1327         case BUILT_IN_GOMP_CRITICAL_END:
1328         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1329         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1330         case BUILT_IN_GOMP_LOOP_END:
1331         case BUILT_IN_GOMP_ORDERED_START:
1332         case BUILT_IN_GOMP_ORDERED_END:
1333         case BUILT_IN_GOMP_PARALLEL_END:
1334         case BUILT_IN_GOMP_SECTIONS_END:
1335         case BUILT_IN_GOMP_SINGLE_COPY_START:
1336         case BUILT_IN_GOMP_SINGLE_COPY_END:
1337           return true;
1338
1339         default:
1340           /* Fallthru to general call handling.  */;
1341       }
1342
1343   /* Check if base is a global static variable that is not read
1344      by the function.  */
1345   if (callee != NULL_TREE
1346       && TREE_CODE (base) == VAR_DECL
1347       && TREE_STATIC (base))
1348     {
1349       struct cgraph_node *node = cgraph_get_node (callee);
1350       bitmap not_read;
1351
1352       /* FIXME: Callee can be an OMP builtin that does not have a call graph
1353          node yet.  We should enforce that there are nodes for all decls in the
1354          IL and remove this check instead.  */
1355       if (node
1356           && (not_read = ipa_reference_get_not_read_global (node))
1357           && bitmap_bit_p (not_read, DECL_UID (base)))
1358         goto process_args;
1359     }
1360
1361   /* Check if the base variable is call-used.  */
1362   if (DECL_P (base))
1363     {
1364       if (pt_solution_includes (gimple_call_use_set (call), base))
1365         return true;
1366     }
1367   else if ((TREE_CODE (base) == MEM_REF
1368             || TREE_CODE (base) == TARGET_MEM_REF)
1369            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1370     {
1371       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1372       if (!pi)
1373         return true;
1374
1375       if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1376         return true;
1377     }
1378   else
1379     return true;
1380
1381   /* Inspect call arguments for passed-by-value aliases.  */
1382 process_args:
1383   for (i = 0; i < gimple_call_num_args (call); ++i)
1384     {
1385       tree op = gimple_call_arg (call, i);
1386       int flags = gimple_call_arg_flags (call, i);
1387
1388       if (flags & EAF_UNUSED)
1389         continue;
1390
1391       if (TREE_CODE (op) == WITH_SIZE_EXPR)
1392         op = TREE_OPERAND (op, 0);
1393
1394       if (TREE_CODE (op) != SSA_NAME
1395           && !is_gimple_min_invariant (op))
1396         {
1397           ao_ref r;
1398           ao_ref_init (&r, op);
1399           if (refs_may_alias_p_1 (&r, ref, true))
1400             return true;
1401         }
1402     }
1403
1404   return false;
1405 }
1406
1407 static bool
1408 ref_maybe_used_by_call_p (gimple call, tree ref)
1409 {
1410   ao_ref r;
1411   bool res;
1412   ao_ref_init (&r, ref);
1413   res = ref_maybe_used_by_call_p_1 (call, &r);
1414   if (res)
1415     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1416   else
1417     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1418   return res;
1419 }
1420
1421
1422 /* If the statement STMT may use the memory reference REF return
1423    true, otherwise return false.  */
1424
1425 bool
1426 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1427 {
1428   if (is_gimple_assign (stmt))
1429     {
1430       tree rhs;
1431
1432       /* All memory assign statements are single.  */
1433       if (!gimple_assign_single_p (stmt))
1434         return false;
1435
1436       rhs = gimple_assign_rhs1 (stmt);
1437       if (is_gimple_reg (rhs)
1438           || is_gimple_min_invariant (rhs)
1439           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1440         return false;
1441
1442       return refs_may_alias_p (rhs, ref);
1443     }
1444   else if (is_gimple_call (stmt))
1445     return ref_maybe_used_by_call_p (stmt, ref);
1446   else if (gimple_code (stmt) == GIMPLE_RETURN)
1447     {
1448       tree retval = gimple_return_retval (stmt);
1449       tree base;
1450       if (retval
1451           && TREE_CODE (retval) != SSA_NAME
1452           && !is_gimple_min_invariant (retval)
1453           && refs_may_alias_p (retval, ref))
1454         return true;
1455       /* If ref escapes the function then the return acts as a use.  */
1456       base = get_base_address (ref);
1457       if (!base)
1458         ;
1459       else if (DECL_P (base))
1460         return is_global_var (base);
1461       else if (TREE_CODE (base) == MEM_REF
1462                || TREE_CODE (base) == TARGET_MEM_REF)
1463         return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1464       return false;
1465     }
1466
1467   return true;
1468 }
1469
1470 /* If the call in statement CALL may clobber the memory reference REF
1471    return true, otherwise return false.  */
1472
1473 static bool
1474 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1475 {
1476   tree base;
1477   tree callee;
1478
1479   /* If the call is pure or const it cannot clobber anything.  */
1480   if (gimple_call_flags (call)
1481       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1482     return false;
1483
1484   base = ao_ref_base (ref);
1485   if (!base)
1486     return true;
1487
1488   if (TREE_CODE (base) == SSA_NAME
1489       || CONSTANT_CLASS_P (base))
1490     return false;
1491
1492   /* A call that is not without side-effects might involve volatile
1493      accesses and thus conflicts with all other volatile accesses.  */
1494   if (ref->volatile_p)
1495     return true;
1496
1497   /* If the reference is based on a decl that is not aliased the call
1498      cannot possibly clobber it.  */
1499   if (DECL_P (base)
1500       && !may_be_aliased (base)
1501       /* But local non-readonly statics can be modified through recursion
1502          or the call may implement a threading barrier which we must
1503          treat as may-def.  */
1504       && (TREE_READONLY (base)
1505           || !is_global_var (base)))
1506     return false;
1507
1508   callee = gimple_call_fndecl (call);
1509
1510   /* Handle those builtin functions explicitly that do not act as
1511      escape points.  See tree-ssa-structalias.c:find_func_aliases
1512      for the list of builtins we might need to handle here.  */
1513   if (callee != NULL_TREE
1514       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1515     switch (DECL_FUNCTION_CODE (callee))
1516       {
1517         /* All the following functions clobber memory pointed to by
1518            their first argument.  */
1519         case BUILT_IN_STRCPY:
1520         case BUILT_IN_STRNCPY:
1521         case BUILT_IN_MEMCPY:
1522         case BUILT_IN_MEMMOVE:
1523         case BUILT_IN_MEMPCPY:
1524         case BUILT_IN_STPCPY:
1525         case BUILT_IN_STPNCPY:
1526         case BUILT_IN_STRCAT:
1527         case BUILT_IN_STRNCAT:
1528         case BUILT_IN_MEMSET:
1529         case BUILT_IN_TM_MEMSET:
1530         CASE_BUILT_IN_TM_STORE (1):
1531         CASE_BUILT_IN_TM_STORE (2):
1532         CASE_BUILT_IN_TM_STORE (4):
1533         CASE_BUILT_IN_TM_STORE (8):
1534         CASE_BUILT_IN_TM_STORE (FLOAT):
1535         CASE_BUILT_IN_TM_STORE (DOUBLE):
1536         CASE_BUILT_IN_TM_STORE (LDOUBLE):
1537         CASE_BUILT_IN_TM_STORE (M64):
1538         CASE_BUILT_IN_TM_STORE (M128):
1539         CASE_BUILT_IN_TM_STORE (M256):
1540         case BUILT_IN_TM_MEMCPY:
1541         case BUILT_IN_TM_MEMMOVE:
1542           {
1543             ao_ref dref;
1544             tree size = NULL_TREE;
1545             /* Don't pass in size for strncat, as the maximum size
1546                is strlen (dest) + n + 1 instead of n, resp.
1547                n + 1 at dest + strlen (dest), but strlen (dest) isn't
1548                known.  */
1549             if (gimple_call_num_args (call) == 3
1550                 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1551               size = gimple_call_arg (call, 2);
1552             ao_ref_init_from_ptr_and_size (&dref,
1553                                            gimple_call_arg (call, 0),
1554                                            size);
1555             return refs_may_alias_p_1 (&dref, ref, false);
1556           }
1557         case BUILT_IN_STRCPY_CHK:
1558         case BUILT_IN_STRNCPY_CHK:
1559         case BUILT_IN_MEMCPY_CHK:
1560         case BUILT_IN_MEMMOVE_CHK:
1561         case BUILT_IN_MEMPCPY_CHK:
1562         case BUILT_IN_STPCPY_CHK:
1563         case BUILT_IN_STRCAT_CHK:
1564         case BUILT_IN_STRNCAT_CHK:
1565         case BUILT_IN_MEMSET_CHK:
1566           {
1567             ao_ref dref;
1568             tree size = NULL_TREE;
1569             /* Don't pass in size for __strncat_chk, as the maximum size
1570                is strlen (dest) + n + 1 instead of n, resp.
1571                n + 1 at dest + strlen (dest), but strlen (dest) isn't
1572                known.  */
1573             if (gimple_call_num_args (call) == 4
1574                 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1575               size = gimple_call_arg (call, 2);
1576             ao_ref_init_from_ptr_and_size (&dref,
1577                                            gimple_call_arg (call, 0),
1578                                            size);
1579             return refs_may_alias_p_1 (&dref, ref, false);
1580           }
1581         case BUILT_IN_BCOPY:
1582           {
1583             ao_ref dref;
1584             tree size = gimple_call_arg (call, 2);
1585             ao_ref_init_from_ptr_and_size (&dref,
1586                                            gimple_call_arg (call, 1),
1587                                            size);
1588             return refs_may_alias_p_1 (&dref, ref, false);
1589           }
1590         /* Allocating memory does not have any side-effects apart from
1591            being the definition point for the pointer.  */
1592         case BUILT_IN_MALLOC:
1593         case BUILT_IN_CALLOC:
1594         case BUILT_IN_STRDUP:
1595         case BUILT_IN_STRNDUP:
1596           /* Unix98 specifies that errno is set on allocation failure.  */
1597           if (flag_errno_math
1598               && targetm.ref_may_alias_errno (ref))
1599             return true;
1600           return false;
1601         case BUILT_IN_STACK_SAVE:
1602         case BUILT_IN_ALLOCA:
1603         case BUILT_IN_ALLOCA_WITH_ALIGN:
1604         case BUILT_IN_ASSUME_ALIGNED:
1605           return false;
1606         /* Freeing memory kills the pointed-to memory.  More importantly
1607            the call has to serve as a barrier for moving loads and stores
1608            across it.  */
1609         case BUILT_IN_FREE:
1610         case BUILT_IN_VA_END:
1611           {
1612             tree ptr = gimple_call_arg (call, 0);
1613             return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1614           }
1615         case BUILT_IN_GAMMA_R:
1616         case BUILT_IN_GAMMAF_R:
1617         case BUILT_IN_GAMMAL_R:
1618         case BUILT_IN_LGAMMA_R:
1619         case BUILT_IN_LGAMMAF_R:
1620         case BUILT_IN_LGAMMAL_R:
1621           {
1622             tree out = gimple_call_arg (call, 1);
1623             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1624               return true;
1625             if (flag_errno_math)
1626               break;
1627             return false;
1628           }
1629         case BUILT_IN_FREXP:
1630         case BUILT_IN_FREXPF:
1631         case BUILT_IN_FREXPL:
1632         case BUILT_IN_MODF:
1633         case BUILT_IN_MODFF:
1634         case BUILT_IN_MODFL:
1635           {
1636             tree out = gimple_call_arg (call, 1);
1637             return ptr_deref_may_alias_ref_p_1 (out, ref);
1638           }
1639         case BUILT_IN_REMQUO:
1640         case BUILT_IN_REMQUOF:
1641         case BUILT_IN_REMQUOL:
1642           {
1643             tree out = gimple_call_arg (call, 2);
1644             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1645               return true;
1646             if (flag_errno_math)
1647               break;
1648             return false;
1649           }
1650         case BUILT_IN_SINCOS:
1651         case BUILT_IN_SINCOSF:
1652         case BUILT_IN_SINCOSL:
1653           {
1654             tree sin = gimple_call_arg (call, 1);
1655             tree cos = gimple_call_arg (call, 2);
1656             return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1657                     || ptr_deref_may_alias_ref_p_1 (cos, ref));
1658           }
1659         /* __sync_* builtins and some OpenMP builtins act as threading
1660            barriers.  */
1661 #undef DEF_SYNC_BUILTIN
1662 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1663 #include "sync-builtins.def"
1664 #undef DEF_SYNC_BUILTIN
1665         case BUILT_IN_GOMP_ATOMIC_START:
1666         case BUILT_IN_GOMP_ATOMIC_END:
1667         case BUILT_IN_GOMP_BARRIER:
1668         case BUILT_IN_GOMP_TASKWAIT:
1669         case BUILT_IN_GOMP_CRITICAL_START:
1670         case BUILT_IN_GOMP_CRITICAL_END:
1671         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1672         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1673         case BUILT_IN_GOMP_LOOP_END:
1674         case BUILT_IN_GOMP_ORDERED_START:
1675         case BUILT_IN_GOMP_ORDERED_END:
1676         case BUILT_IN_GOMP_PARALLEL_END:
1677         case BUILT_IN_GOMP_SECTIONS_END:
1678         case BUILT_IN_GOMP_SINGLE_COPY_START:
1679         case BUILT_IN_GOMP_SINGLE_COPY_END:
1680           return true;
1681         default:
1682           /* Fallthru to general call handling.  */;
1683       }
1684
1685   /* Check if base is a global static variable that is not written
1686      by the function.  */
1687   if (callee != NULL_TREE
1688       && TREE_CODE (base) == VAR_DECL
1689       && TREE_STATIC (base))
1690     {
1691       struct cgraph_node *node = cgraph_get_node (callee);
1692       bitmap not_written;
1693
1694       if (node
1695           && (not_written = ipa_reference_get_not_written_global (node))
1696           && bitmap_bit_p (not_written, DECL_UID (base)))
1697         return false;
1698     }
1699
1700   /* Check if the base variable is call-clobbered.  */
1701   if (DECL_P (base))
1702     return pt_solution_includes (gimple_call_clobber_set (call), base);
1703   else if ((TREE_CODE (base) == MEM_REF
1704             || TREE_CODE (base) == TARGET_MEM_REF)
1705            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1706     {
1707       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1708       if (!pi)
1709         return true;
1710
1711       return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1712     }
1713
1714   return true;
1715 }
1716
1717 /* If the call in statement CALL may clobber the memory reference REF
1718    return true, otherwise return false.  */
1719
1720 bool
1721 call_may_clobber_ref_p (gimple call, tree ref)
1722 {
1723   bool res;
1724   ao_ref r;
1725   ao_ref_init (&r, ref);
1726   res = call_may_clobber_ref_p_1 (call, &r);
1727   if (res)
1728     ++alias_stats.call_may_clobber_ref_p_may_alias;
1729   else
1730     ++alias_stats.call_may_clobber_ref_p_no_alias;
1731   return res;
1732 }
1733
1734
1735 /* If the statement STMT may clobber the memory reference REF return true,
1736    otherwise return false.  */
1737
1738 bool
1739 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1740 {
1741   if (is_gimple_call (stmt))
1742     {
1743       tree lhs = gimple_call_lhs (stmt);
1744       if (lhs
1745           && TREE_CODE (lhs) != SSA_NAME)
1746         {
1747           ao_ref r;
1748           ao_ref_init (&r, lhs);
1749           if (refs_may_alias_p_1 (ref, &r, true))
1750             return true;
1751         }
1752
1753       return call_may_clobber_ref_p_1 (stmt, ref);
1754     }
1755   else if (gimple_assign_single_p (stmt))
1756     {
1757       tree lhs = gimple_assign_lhs (stmt);
1758       if (TREE_CODE (lhs) != SSA_NAME)
1759         {
1760           ao_ref r;
1761           ao_ref_init (&r, lhs);
1762           return refs_may_alias_p_1 (ref, &r, true);
1763         }
1764     }
1765   else if (gimple_code (stmt) == GIMPLE_ASM)
1766     return true;
1767
1768   return false;
1769 }
1770
1771 bool
1772 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1773 {
1774   ao_ref r;
1775   ao_ref_init (&r, ref);
1776   return stmt_may_clobber_ref_p_1 (stmt, &r);
1777 }
1778
1779 /* If STMT kills the memory reference REF return true, otherwise
1780    return false.  */
1781
1782 static bool
1783 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1784 {
1785   /* For a must-alias check we need to be able to constrain
1786      the access properly.  */
1787   ao_ref_base (ref);
1788   if (ref->max_size == -1)
1789     return false;
1790
1791   if (gimple_has_lhs (stmt)
1792       && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1793       /* The assignment is not necessarily carried out if it can throw
1794          and we can catch it in the current function where we could inspect
1795          the previous value.
1796          ???  We only need to care about the RHS throwing.  For aggregate
1797          assignments or similar calls and non-call exceptions the LHS
1798          might throw as well.  */
1799       && !stmt_can_throw_internal (stmt))
1800     {
1801       tree base, lhs = gimple_get_lhs (stmt);
1802       HOST_WIDE_INT size, offset, max_size;
1803       base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1804       /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1805          so base == ref->base does not always hold.  */
1806       if (base == ref->base)
1807         {
1808           /* For a must-alias check we need to be able to constrain
1809              the access properly.  */
1810           if (size != -1 && size == max_size)
1811             {
1812               if (offset <= ref->offset
1813                   && offset + size >= ref->offset + ref->max_size)
1814                 return true;
1815             }
1816         }
1817     }
1818
1819   if (is_gimple_call (stmt))
1820     {
1821       tree callee = gimple_call_fndecl (stmt);
1822       if (callee != NULL_TREE
1823           && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1824         switch (DECL_FUNCTION_CODE (callee))
1825           {
1826           case BUILT_IN_MEMCPY:
1827           case BUILT_IN_MEMPCPY:
1828           case BUILT_IN_MEMMOVE:
1829           case BUILT_IN_MEMSET:
1830           case BUILT_IN_MEMCPY_CHK:
1831           case BUILT_IN_MEMPCPY_CHK:
1832           case BUILT_IN_MEMMOVE_CHK:
1833           case BUILT_IN_MEMSET_CHK:
1834             {
1835               tree dest = gimple_call_arg (stmt, 0);
1836               tree len = gimple_call_arg (stmt, 2);
1837               tree base = NULL_TREE;
1838               HOST_WIDE_INT offset = 0;
1839               if (!host_integerp (len, 0))
1840                 return false;
1841               if (TREE_CODE (dest) == ADDR_EXPR)
1842                 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1843                                                       &offset);
1844               else if (TREE_CODE (dest) == SSA_NAME)
1845                 base = dest;
1846               if (base
1847                   && base == ao_ref_base (ref))
1848                 {
1849                   HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1850                   if (offset <= ref->offset / BITS_PER_UNIT
1851                       && (offset + size
1852                           >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1853                               / BITS_PER_UNIT)))
1854                     return true;
1855                 }
1856               break;
1857             }
1858
1859           case BUILT_IN_VA_END:
1860             {
1861               tree ptr = gimple_call_arg (stmt, 0);
1862               if (TREE_CODE (ptr) == ADDR_EXPR)
1863                 {
1864                   tree base = ao_ref_base (ref);
1865                   if (TREE_OPERAND (ptr, 0) == base)
1866                     return true;
1867                 }
1868               break;
1869             }
1870
1871           default:;
1872           }
1873     }
1874   return false;
1875 }
1876
1877 bool
1878 stmt_kills_ref_p (gimple stmt, tree ref)
1879 {
1880   ao_ref r;
1881   ao_ref_init (&r, ref);
1882   return stmt_kills_ref_p_1 (stmt, &r);
1883 }
1884
1885
1886 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1887    TARGET or a statement clobbering the memory reference REF in which
1888    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
1889
1890 static bool
1891 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1892                   tree vuse, bitmap *visited)
1893 {
1894   basic_block bb = gimple_bb (phi);
1895
1896   if (!*visited)
1897     *visited = BITMAP_ALLOC (NULL);
1898
1899   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1900
1901   /* Walk until we hit the target.  */
1902   while (vuse != target)
1903     {
1904       gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1905       /* Recurse for PHI nodes.  */
1906       if (gimple_code (def_stmt) == GIMPLE_PHI)
1907         {
1908           /* An already visited PHI node ends the walk successfully.  */
1909           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1910             return true;
1911           vuse = get_continuation_for_phi (def_stmt, ref, visited);
1912           if (!vuse)
1913             return false;
1914           continue;
1915         }
1916       /* A clobbering statement or the end of the IL ends it failing.  */
1917       else if (gimple_nop_p (def_stmt)
1918                || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1919         return false;
1920       /* If we reach a new basic-block see if we already skipped it
1921          in a previous walk that ended successfully.  */
1922       if (gimple_bb (def_stmt) != bb)
1923         {
1924           if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
1925             return true;
1926           bb = gimple_bb (def_stmt);
1927         }
1928       vuse = gimple_vuse (def_stmt);
1929     }
1930   return true;
1931 }
1932
1933 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
1934    until we hit the phi argument definition that dominates the other one.
1935    Return that, or NULL_TREE if there is no such definition.  */
1936
1937 static tree
1938 get_continuation_for_phi_1 (gimple phi, tree arg0, tree arg1,
1939                             ao_ref *ref, bitmap *visited)
1940 {
1941   gimple def0 = SSA_NAME_DEF_STMT (arg0);
1942   gimple def1 = SSA_NAME_DEF_STMT (arg1);
1943   tree common_vuse;
1944
1945   if (arg0 == arg1)
1946     return arg0;
1947   else if (gimple_nop_p (def0)
1948            || (!gimple_nop_p (def1)
1949                && dominated_by_p (CDI_DOMINATORS,
1950                                   gimple_bb (def1), gimple_bb (def0))))
1951     {
1952       if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1953         return arg0;
1954     }
1955   else if (gimple_nop_p (def1)
1956            || dominated_by_p (CDI_DOMINATORS,
1957                               gimple_bb (def0), gimple_bb (def1)))
1958     {
1959       if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1960         return arg1;
1961     }
1962   /* Special case of a diamond:
1963        MEM_1 = ...
1964        goto (cond) ? L1 : L2
1965        L1: store1 = ...    #MEM_2 = vuse(MEM_1)
1966            goto L3
1967        L2: store2 = ...    #MEM_3 = vuse(MEM_1)
1968        L3: MEM_4 = PHI<MEM_2, MEM_3>
1969      We were called with the PHI at L3, MEM_2 and MEM_3 don't
1970      dominate each other, but still we can easily skip this PHI node
1971      if we recognize that the vuse MEM operand is the same for both,
1972      and that we can skip both statements (they don't clobber us).
1973      This is still linear.  Don't use maybe_skip_until, that might
1974      potentially be slow.  */
1975   else if ((common_vuse = gimple_vuse (def0))
1976            && common_vuse == gimple_vuse (def1))
1977     {
1978       if (!stmt_may_clobber_ref_p_1 (def0, ref)
1979           && !stmt_may_clobber_ref_p_1 (def1, ref))
1980         return common_vuse;
1981     }
1982
1983   return NULL_TREE;
1984 }
1985
1986
1987 /* Starting from a PHI node for the virtual operand of the memory reference
1988    REF find a continuation virtual operand that allows to continue walking
1989    statements dominating PHI skipping only statements that cannot possibly
1990    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
1991    be found.  */
1992
1993 tree
1994 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1995 {
1996   unsigned nargs = gimple_phi_num_args (phi);
1997
1998   /* Through a single-argument PHI we can simply look through.  */
1999   if (nargs == 1)
2000     return PHI_ARG_DEF (phi, 0);
2001
2002   /* For two or more arguments try to pairwise skip non-aliasing code
2003      until we hit the phi argument definition that dominates the other one.  */
2004   else if (nargs >= 2)
2005     {
2006       tree arg0, arg1;
2007       unsigned i;
2008
2009       /* Find a candidate for the virtual operand which definition
2010          dominates those of all others.  */
2011       arg0 = PHI_ARG_DEF (phi, 0);
2012       if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2013         for (i = 1; i < nargs; ++i)
2014           {
2015             arg1 = PHI_ARG_DEF (phi, i);
2016             if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2017               {
2018                 arg0 = arg1;
2019                 break;
2020               }
2021             if (dominated_by_p (CDI_DOMINATORS,
2022                                 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2023                                 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2024               arg0 = arg1;
2025           }
2026
2027       /* Then pairwise reduce against the found candidate.  */
2028       for (i = 0; i < nargs; ++i)
2029         {
2030           arg1 = PHI_ARG_DEF (phi, i);
2031           arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref, visited);
2032           if (!arg0)
2033             return NULL_TREE;
2034         }
2035
2036       return arg0;
2037     }
2038
2039   return NULL_TREE;
2040 }
2041
2042 /* Based on the memory reference REF and its virtual use VUSE call
2043    WALKER for each virtual use that is equivalent to VUSE, including VUSE
2044    itself.  That is, for each virtual use for which its defining statement
2045    does not clobber REF.
2046
2047    WALKER is called with REF, the current virtual use and DATA.  If
2048    WALKER returns non-NULL the walk stops and its result is returned.
2049    At the end of a non-successful walk NULL is returned.
2050
2051    TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2052    use which definition is a statement that may clobber REF and DATA.
2053    If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2054    If TRANSLATE returns non-NULL the walk stops and its result is returned.
2055    If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2056    to adjust REF and *DATA to make that valid.
2057
2058    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
2059
2060 void *
2061 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2062                         void *(*walker)(ao_ref *, tree, void *),
2063                         void *(*translate)(ao_ref *, tree, void *), void *data)
2064 {
2065   bitmap visited = NULL;
2066   void *res;
2067
2068   timevar_push (TV_ALIAS_STMT_WALK);
2069
2070   do
2071     {
2072       gimple def_stmt;
2073
2074       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
2075       res = (*walker) (ref, vuse, data);
2076       if (res)
2077         break;
2078
2079       def_stmt = SSA_NAME_DEF_STMT (vuse);
2080       if (gimple_nop_p (def_stmt))
2081         break;
2082       else if (gimple_code (def_stmt) == GIMPLE_PHI)
2083         vuse = get_continuation_for_phi (def_stmt, ref, &visited);
2084       else
2085         {
2086           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2087             {
2088               if (!translate)
2089                 break;
2090               res = (*translate) (ref, vuse, data);
2091               /* Failed lookup and translation.  */
2092               if (res == (void *)-1)
2093                 {
2094                   res = NULL;
2095                   break;
2096                 }
2097               /* Lookup succeeded.  */
2098               else if (res != NULL)
2099                 break;
2100               /* Translation succeeded, continue walking.  */
2101             }
2102           vuse = gimple_vuse (def_stmt);
2103         }
2104     }
2105   while (vuse);
2106
2107   if (visited)
2108     BITMAP_FREE (visited);
2109
2110   timevar_pop (TV_ALIAS_STMT_WALK);
2111
2112   return res;
2113 }
2114
2115
2116 /* Based on the memory reference REF call WALKER for each vdef which
2117    defining statement may clobber REF, starting with VDEF.  If REF
2118    is NULL_TREE, each defining statement is visited.
2119
2120    WALKER is called with REF, the current vdef and DATA.  If WALKER
2121    returns true the walk is stopped, otherwise it continues.
2122
2123    At PHI nodes walk_aliased_vdefs forks into one walk for reach
2124    PHI argument (but only one walk continues on merge points), the
2125    return value is true if any of the walks was successful.
2126
2127    The function returns the number of statements walked.  */
2128
2129 static unsigned int
2130 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2131                       bool (*walker)(ao_ref *, tree, void *), void *data,
2132                       bitmap *visited, unsigned int cnt)
2133 {
2134   do
2135     {
2136       gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2137
2138       if (*visited
2139           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2140         return cnt;
2141
2142       if (gimple_nop_p (def_stmt))
2143         return cnt;
2144       else if (gimple_code (def_stmt) == GIMPLE_PHI)
2145         {
2146           unsigned i;
2147           if (!*visited)
2148             *visited = BITMAP_ALLOC (NULL);
2149           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2150             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2151                                          walker, data, visited, 0);
2152           return cnt;
2153         }
2154
2155       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
2156       cnt++;
2157       if ((!ref
2158            || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2159           && (*walker) (ref, vdef, data))
2160         return cnt;
2161
2162       vdef = gimple_vuse (def_stmt);
2163     }
2164   while (1);
2165 }
2166
2167 unsigned int
2168 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2169                     bool (*walker)(ao_ref *, tree, void *), void *data,
2170                     bitmap *visited)
2171 {
2172   bitmap local_visited = NULL;
2173   unsigned int ret;
2174
2175   timevar_push (TV_ALIAS_STMT_WALK);
2176
2177   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2178                               visited ? visited : &local_visited, 0);
2179   if (local_visited)
2180     BITMAP_FREE (local_visited);
2181
2182   timevar_pop (TV_ALIAS_STMT_WALK);
2183
2184   return ret;
2185 }
2186