OSDN Git Service

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