OSDN Git Service

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