OSDN Git Service

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