OSDN Git Service

2010-07-08 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "tm_p.h"
28 #include "basic-block.h"
29 #include "timevar.h"
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "flags.h"
33 #include "function.h"
34 #include "tree-pretty-print.h"
35 #include "tree-dump.h"
36 #include "gimple.h"
37 #include "tree-flow.h"
38 #include "tree-inline.h"
39 #include "tree-pass.h"
40 #include "convert.h"
41 #include "params.h"
42 #include "ipa-type-escape.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   gcc_assert ((TREE_CODE (ptr) == SSA_NAME
169                || TREE_CODE (ptr) == ADDR_EXPR
170                || TREE_CODE (ptr) == INTEGER_CST)
171               && (TREE_CODE (decl) == VAR_DECL
172                   || TREE_CODE (decl) == PARM_DECL
173                   || TREE_CODE (decl) == RESULT_DECL));
174
175   /* Non-aliased variables can not be pointed to.  */
176   if (!may_be_aliased (decl))
177     return false;
178
179   /* ADDR_EXPR pointers either just offset another pointer or directly
180      specify the pointed-to set.  */
181   if (TREE_CODE (ptr) == ADDR_EXPR)
182     {
183       tree base = get_base_address (TREE_OPERAND (ptr, 0));
184       if (base
185           && (INDIRECT_REF_P (base)
186               || TREE_CODE (base) == MEM_REF))
187         ptr = TREE_OPERAND (base, 0);
188       else if (base
189                && SSA_VAR_P (base))
190         return base == decl;
191       else if (base
192                && CONSTANT_CLASS_P (base))
193         return false;
194       else
195         return true;
196     }
197
198   /* We can end up with dereferencing constant pointers.
199      Just bail out in this case.  */
200   if (TREE_CODE (ptr) == INTEGER_CST)
201     return true;
202
203   /* If we do not have useful points-to information for this pointer
204      we cannot disambiguate anything else.  */
205   pi = SSA_NAME_PTR_INFO (ptr);
206   if (!pi)
207     return true;
208
209   /* If the decl can be used as a restrict tag and we have a restrict
210      pointer and that pointers points-to set doesn't contain this decl
211      then they can't alias.  */
212   if (DECL_RESTRICTED_P (decl)
213       && TYPE_RESTRICT (TREE_TYPE (ptr))
214       && pi->pt.vars_contains_restrict)
215     return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
216
217   return pt_solution_includes (&pi->pt, decl);
218 }
219
220 /* Return true if dereferenced PTR1 and PTR2 may alias.
221    The caller is responsible for applying TBAA to see if accesses
222    through PTR1 and PTR2 may conflict at all.  */
223
224 static bool
225 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
226 {
227   struct ptr_info_def *pi1, *pi2;
228
229   gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
230                || TREE_CODE (ptr1) == ADDR_EXPR
231                || TREE_CODE (ptr1) == INTEGER_CST)
232               && (TREE_CODE (ptr2) == SSA_NAME
233                   || TREE_CODE (ptr2) == ADDR_EXPR
234                   || TREE_CODE (ptr2) == INTEGER_CST));
235
236   /* ADDR_EXPR pointers either just offset another pointer or directly
237      specify the pointed-to set.  */
238   if (TREE_CODE (ptr1) == ADDR_EXPR)
239     {
240       tree base = get_base_address (TREE_OPERAND (ptr1, 0));
241       if (base
242           && (INDIRECT_REF_P (base)
243               || TREE_CODE (base) == MEM_REF))
244         ptr1 = TREE_OPERAND (base, 0);
245       else if (base
246                && SSA_VAR_P (base))
247         return ptr_deref_may_alias_decl_p (ptr2, base);
248       else
249         return true;
250     }
251   if (TREE_CODE (ptr2) == ADDR_EXPR)
252     {
253       tree base = get_base_address (TREE_OPERAND (ptr2, 0));
254       if (base
255           && (INDIRECT_REF_P (base)
256               || TREE_CODE (base) == MEM_REF))
257         ptr2 = TREE_OPERAND (base, 0);
258       else if (base
259                && SSA_VAR_P (base))
260         return ptr_deref_may_alias_decl_p (ptr1, base);
261       else
262         return true;
263     }
264
265   /* We can end up with dereferencing constant pointers.
266      Just bail out in this case.  */
267   if (TREE_CODE (ptr1) == INTEGER_CST
268       || TREE_CODE (ptr2) == INTEGER_CST)
269     return true;
270
271   /* We may end up with two empty points-to solutions for two same pointers.
272      In this case we still want to say both pointers alias, so shortcut
273      that here.  */
274   if (ptr1 == ptr2)
275     return true;
276
277   /* If we do not have useful points-to information for either pointer
278      we cannot disambiguate anything else.  */
279   pi1 = SSA_NAME_PTR_INFO (ptr1);
280   pi2 = SSA_NAME_PTR_INFO (ptr2);
281   if (!pi1 || !pi2)
282     return true;
283
284   /* If both pointers are restrict-qualified try to disambiguate
285      with restrict information.  */
286   if (TYPE_RESTRICT (TREE_TYPE (ptr1))
287       && TYPE_RESTRICT (TREE_TYPE (ptr2))
288       && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
289     return false;
290
291   /* ???  This does not use TBAA to prune decls from the intersection
292      that not both pointers may access.  */
293   return pt_solutions_intersect (&pi1->pt, &pi2->pt);
294 }
295
296 /* Return true if dereferencing PTR may alias *REF.
297    The caller is responsible for applying TBAA to see if PTR
298    may access *REF at all.  */
299
300 static bool
301 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
302 {
303   tree base = ao_ref_base (ref);
304
305   if (INDIRECT_REF_P (base)
306       || TREE_CODE (base) == MEM_REF)
307     return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
308   else if (SSA_VAR_P (base))
309     return ptr_deref_may_alias_decl_p (ptr, base);
310
311   return true;
312 }
313
314
315 /* Dump alias information on FILE.  */
316
317 void
318 dump_alias_info (FILE *file)
319 {
320   size_t i;
321   const char *funcname
322     = lang_hooks.decl_printable_name (current_function_decl, 2);
323   referenced_var_iterator rvi;
324   tree var;
325
326   fprintf (file, "\n\nAlias information for %s\n\n", funcname);
327
328   fprintf (file, "Aliased symbols\n\n");
329
330   FOR_EACH_REFERENCED_VAR (var, rvi)
331     {
332       if (may_be_aliased (var))
333         dump_variable (file, var);
334     }
335
336   fprintf (file, "\nCall clobber information\n");
337
338   fprintf (file, "\nESCAPED");
339   dump_points_to_solution (file, &cfun->gimple_df->escaped);
340
341   fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
342
343   for (i = 1; i < num_ssa_names; i++)
344     {
345       tree ptr = ssa_name (i);
346       struct ptr_info_def *pi;
347
348       if (ptr == NULL_TREE
349           || SSA_NAME_IN_FREE_LIST (ptr))
350         continue;
351
352       pi = SSA_NAME_PTR_INFO (ptr);
353       if (pi)
354         dump_points_to_info_for (file, ptr);
355     }
356
357   fprintf (file, "\n");
358 }
359
360
361 /* Dump alias information on stderr.  */
362
363 DEBUG_FUNCTION void
364 debug_alias_info (void)
365 {
366   dump_alias_info (stderr);
367 }
368
369
370 /* Return the alias information associated with pointer T.  It creates a
371    new instance if none existed.  */
372
373 struct ptr_info_def *
374 get_ptr_info (tree t)
375 {
376   struct ptr_info_def *pi;
377
378   gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
379
380   pi = SSA_NAME_PTR_INFO (t);
381   if (pi == NULL)
382     {
383       pi = ggc_alloc_cleared_ptr_info_def ();
384       pt_solution_reset (&pi->pt);
385       SSA_NAME_PTR_INFO (t) = pi;
386     }
387
388   return pi;
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       if (pt->vars_contains_restrict)
418         fprintf (file, " (includes restrict tags)");
419     }
420 }
421
422 /* Dump points-to information for SSA_NAME PTR into FILE.  */
423
424 void
425 dump_points_to_info_for (FILE *file, tree ptr)
426 {
427   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
428
429   print_generic_expr (file, ptr, dump_flags);
430
431   if (pi)
432     dump_points_to_solution (file, &pi->pt);
433   else
434     fprintf (file, ", points-to anything");
435
436   fprintf (file, "\n");
437 }
438
439
440 /* Dump points-to information for VAR into stderr.  */
441
442 DEBUG_FUNCTION void
443 debug_points_to_info_for (tree var)
444 {
445   dump_points_to_info_for (stderr, var);
446 }
447
448
449 /* Initializes the alias-oracle reference representation *R from REF.  */
450
451 void
452 ao_ref_init (ao_ref *r, tree ref)
453 {
454   r->ref = ref;
455   r->base = NULL_TREE;
456   r->offset = 0;
457   r->size = -1;
458   r->max_size = -1;
459   r->ref_alias_set = -1;
460   r->base_alias_set = -1;
461 }
462
463 /* Returns the base object of the memory reference *REF.  */
464
465 tree
466 ao_ref_base (ao_ref *ref)
467 {
468   if (ref->base)
469     return ref->base;
470   ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
471                                        &ref->max_size);
472   return ref->base;
473 }
474
475 /* Returns the base object alias set of the memory reference *REF.  */
476
477 static alias_set_type
478 ao_ref_base_alias_set (ao_ref *ref)
479 {
480   tree base_ref;
481   if (ref->base_alias_set != -1)
482     return ref->base_alias_set;
483   if (!ref->ref)
484     return 0;
485   base_ref = ref->ref;
486   while (handled_component_p (base_ref))
487     base_ref = TREE_OPERAND (base_ref, 0);
488   ref->base_alias_set = get_alias_set (base_ref);
489   return ref->base_alias_set;
490 }
491
492 /* Returns the reference alias set of the memory reference *REF.  */
493
494 alias_set_type
495 ao_ref_alias_set (ao_ref *ref)
496 {
497   if (ref->ref_alias_set != -1)
498     return ref->ref_alias_set;
499   ref->ref_alias_set = get_alias_set (ref->ref);
500   return ref->ref_alias_set;
501 }
502
503 /* Init an alias-oracle reference representation from a gimple pointer
504    PTR and a gimple size SIZE in bytes.  If SIZE is NULL_TREE the the
505    size is assumed to be unknown.  The access is assumed to be only
506    to or after of the pointer target, not before it.  */
507
508 void
509 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
510 {
511   HOST_WIDE_INT t1, t2;
512   ref->ref = NULL_TREE;
513   if (TREE_CODE (ptr) == ADDR_EXPR)
514     ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
515                                          &ref->offset, &t1, &t2);
516   else
517     {
518       ref->base = build2 (MEM_REF, char_type_node,
519                           ptr, build_int_cst (ptr_type_node, 0));
520       ref->offset = 0;
521     }
522   if (size
523       && host_integerp (size, 0)
524       && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
525     ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
526   else
527     ref->max_size = ref->size = -1;
528   ref->ref_alias_set = 0;
529   ref->base_alias_set = 0;
530 }
531
532 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
533    purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
534    decide.  */
535
536 static inline int
537 same_type_for_tbaa (tree type1, tree type2)
538 {
539   type1 = TYPE_MAIN_VARIANT (type1);
540   type2 = TYPE_MAIN_VARIANT (type2);
541
542   /* If we would have to do structural comparison bail out.  */
543   if (TYPE_STRUCTURAL_EQUALITY_P (type1)
544       || TYPE_STRUCTURAL_EQUALITY_P (type2))
545     return -1;
546
547   /* Compare the canonical types.  */
548   if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
549     return 1;
550
551   /* ??? Array types are not properly unified in all cases as we have
552      spurious changes in the index types for example.  Removing this
553      causes all sorts of problems with the Fortran frontend.  */
554   if (TREE_CODE (type1) == ARRAY_TYPE
555       && TREE_CODE (type2) == ARRAY_TYPE)
556     return -1;
557
558   /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
559      object of one of its constrained subtypes, e.g. when a function with an
560      unconstrained parameter passed by reference is called on an object and
561      inlined.  But, even in the case of a fixed size, type and subtypes are
562      not equivalent enough as to share the same TYPE_CANONICAL, since this
563      would mean that conversions between them are useless, whereas they are
564      not (e.g. type and subtypes can have different modes).  So, in the end,
565      they are only guaranteed to have the same alias set.  */
566   if (get_alias_set (type1) == get_alias_set (type2))
567     return -1;
568
569   /* The types are known to be not equal.  */
570   return 0;
571 }
572
573 /* Determine if the two component references REF1 and REF2 which are
574    based on access types TYPE1 and TYPE2 and of which at least one is based
575    on an indirect reference may alias.  REF2 is the only one that can
576    be a decl in which case REF2_IS_DECL is true.
577    REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
578    are the respective alias sets.  */
579
580 static bool
581 aliasing_component_refs_p (tree ref1, tree type1,
582                            alias_set_type ref1_alias_set,
583                            alias_set_type base1_alias_set,
584                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
585                            tree ref2, tree type2,
586                            alias_set_type ref2_alias_set,
587                            alias_set_type base2_alias_set,
588                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
589                            bool ref2_is_decl)
590 {
591   /* If one reference is a component references through pointers try to find a
592      common base and apply offset based disambiguation.  This handles
593      for example
594        struct A { int i; int j; } *q;
595        struct B { struct A a; int k; } *p;
596      disambiguating q->i and p->a.j.  */
597   tree *refp;
598   int same_p;
599
600   /* Now search for the type1 in the access path of ref2.  This
601      would be a common base for doing offset based disambiguation on.  */
602   refp = &ref2;
603   while (handled_component_p (*refp)
604          && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
605     refp = &TREE_OPERAND (*refp, 0);
606   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
607   /* If we couldn't compare types we have to bail out.  */
608   if (same_p == -1)
609     return true;
610   else if (same_p == 1)
611     {
612       HOST_WIDE_INT offadj, sztmp, msztmp;
613       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
614       offset2 -= offadj;
615       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
616     }
617   /* If we didn't find a common base, try the other way around.  */
618   refp = &ref1;
619   while (handled_component_p (*refp)
620          && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
621     refp = &TREE_OPERAND (*refp, 0);
622   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
623   /* If we couldn't compare types we have to bail out.  */
624   if (same_p == -1)
625     return true;
626   else if (same_p == 1)
627     {
628       HOST_WIDE_INT offadj, sztmp, msztmp;
629       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
630       offset1 -= offadj;
631       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
632     }
633
634   /* If we have two type access paths B1.path1 and B2.path2 they may
635      only alias if either B1 is in B2.path2 or B2 is in B1.path1.
636      But we can still have a path that goes B1.path1...B2.path2 with
637      a part that we do not see.  So we can only disambiguate now
638      if there is no B2 in the tail of path1 and no B1 on the
639      tail of path2.  */
640   if (base1_alias_set == ref2_alias_set
641       || alias_set_subset_of (base1_alias_set, ref2_alias_set))
642     return true;
643   /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
644   if (!ref2_is_decl)
645     return (base2_alias_set == ref1_alias_set
646             || alias_set_subset_of (base2_alias_set, ref1_alias_set));
647   return false;
648 }
649
650 /* Return true if two memory references based on the variables BASE1
651    and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
652    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  */
653
654 static bool
655 decl_refs_may_alias_p (tree base1,
656                        HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
657                        tree base2,
658                        HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
659 {
660   gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
661
662   /* If both references are based on different variables, they cannot alias.  */
663   if (base1 != base2)
664     return false;
665
666   /* If both references are based on the same variable, they cannot alias if
667      the accesses do not overlap.  */
668   return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
669 }
670
671 /* Return true if an indirect reference based on *PTR1 constrained
672    to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
673    constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
674    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
675    in which case they are computed on-demand.  REF1 and REF2
676    if non-NULL are the complete memory reference trees.  */
677
678 static bool
679 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
680                                HOST_WIDE_INT offset1,
681                                HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
682                                alias_set_type ref1_alias_set,
683                                alias_set_type base1_alias_set,
684                                tree ref2 ATTRIBUTE_UNUSED, tree base2,
685                                HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
686                                alias_set_type ref2_alias_set,
687                                alias_set_type base2_alias_set, bool tbaa_p)
688 {
689   tree ptr1 = TREE_OPERAND (base1, 0);
690   tree ptrtype1;
691   HOST_WIDE_INT offset1p = offset1;
692
693   if (TREE_CODE (base1) == MEM_REF)
694     offset1p = offset1 + mem_ref_offset (base1).low * BITS_PER_UNIT;
695
696   /* If only one reference is based on a variable, they cannot alias if
697      the pointer access is beyond the extent of the variable access.
698      (the pointer base cannot validly point to an offset less than zero
699      of the variable).
700      They also cannot alias if the pointer may not point to the decl.  */
701   if (!ranges_overlap_p (MAX (0, offset1p), -1, offset2, max_size2))
702     return false;
703   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
704     return false;
705
706   /* Disambiguations that rely on strict aliasing rules follow.  */
707   if (!flag_strict_aliasing || !tbaa_p)
708     return true;
709
710   if (TREE_CODE (base1) == MEM_REF)
711     ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
712   else
713     ptrtype1 = TREE_TYPE (ptr1);
714
715   /* If the alias set for a pointer access is zero all bets are off.  */
716   if (base1_alias_set == -1)
717     base1_alias_set = get_deref_alias_set (ptrtype1);
718   if (base1_alias_set == 0)
719     return true;
720   if (base2_alias_set == -1)
721     base2_alias_set = get_alias_set (base2);
722
723   /* If both references are through the same type, they do not alias
724      if the accesses do not overlap.  This does extra disambiguation
725      for mixed/pointer accesses but requires strict aliasing.
726      For MEM_REFs we require that the component-ref offset we computed
727      is relative to the start of the type which we ensure by
728      comparing rvalue and access type and disregarding the constant
729      pointer offset.  */
730   if ((TREE_CODE (base1) != MEM_REF
731        || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
732       && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
733     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
734
735   /* When we are trying to disambiguate an access with a pointer dereference
736      as base versus one with a decl as base we can use both the size
737      of the decl and its dynamic type for extra disambiguation.
738      ???  We do not know anything about the dynamic type of the decl
739      other than that its alias-set contains base2_alias_set as a subset
740      which does not help us here.  */
741   /* As we know nothing useful about the dynamic type of the decl just
742      use the usual conflict check rather than a subset test.
743      ???  We could introduce -fvery-strict-aliasing when the language
744      does not allow decls to have a dynamic type that differs from their
745      static type.  Then we can check 
746      !alias_set_subset_of (base1_alias_set, base2_alias_set) instead.  */
747   if (base1_alias_set != base2_alias_set
748       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
749     return false;
750   /* If the size of the access relevant for TBAA through the pointer
751      is bigger than the size of the decl we can't possibly access the
752      decl via that pointer.  */
753   if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
754       && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
755       && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
756       /* ???  This in turn may run afoul when a decl of type T which is
757          a member of union type U is accessed through a pointer to
758          type U and sizeof T is smaller than sizeof U.  */
759       && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
760       && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
761       && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
762     return false;
763
764   /* Do access-path based disambiguation.  */
765   if (ref1 && ref2
766       && handled_component_p (ref1)
767       && handled_component_p (ref2)
768       && (TREE_CODE (base1) != MEM_REF
769           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
770     return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
771                                       ref1_alias_set, base1_alias_set,
772                                       offset1, max_size1,
773                                       ref2, TREE_TYPE (base2),
774                                       ref2_alias_set, base2_alias_set,
775                                       offset2, max_size2, true);
776
777   return true;
778 }
779
780 /* Return true if two indirect references based on *PTR1
781    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
782    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
783    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
784    in which case they are computed on-demand.  REF1 and REF2
785    if non-NULL are the complete memory reference trees. */
786
787 static bool
788 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
789                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
790                            alias_set_type ref1_alias_set,
791                            alias_set_type base1_alias_set,
792                            tree ref2 ATTRIBUTE_UNUSED, tree base2,
793                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
794                            alias_set_type ref2_alias_set,
795                            alias_set_type base2_alias_set, bool tbaa_p)
796 {
797   tree ptr1 = TREE_OPERAND (base1, 0);
798   tree ptr2 = TREE_OPERAND (base2, 0);
799   tree ptrtype1, ptrtype2;
800
801   /* If both bases are based on pointers they cannot alias if they may not
802      point to the same memory object or if they point to the same object
803      and the accesses do not overlap.  */
804   if ((!cfun || gimple_in_ssa_p (cfun))
805       && operand_equal_p (ptr1, ptr2, 0))
806     {
807       if (TREE_CODE (base1) == MEM_REF)
808         offset1 += mem_ref_offset (base1).low * BITS_PER_UNIT;
809       if (TREE_CODE (base2) == MEM_REF)
810         offset2 += mem_ref_offset (base2).low * BITS_PER_UNIT;
811       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
812     }
813   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
814     return false;
815
816   /* Disambiguations that rely on strict aliasing rules follow.  */
817   if (!flag_strict_aliasing || !tbaa_p)
818     return true;
819
820   if (TREE_CODE (base1) == MEM_REF)
821     ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
822   else
823     ptrtype1 = TREE_TYPE (ptr1);
824   if (TREE_CODE (base2) == MEM_REF)
825     ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
826   else
827     ptrtype2 = TREE_TYPE (ptr2);
828
829   /* If the alias set for a pointer access is zero all bets are off.  */
830   if (base1_alias_set == -1)
831     base1_alias_set = get_deref_alias_set (ptrtype1);
832   if (base1_alias_set == 0)
833     return true;
834   if (base2_alias_set == -1)
835     base2_alias_set = get_deref_alias_set (ptrtype2);
836   if (base2_alias_set == 0)
837     return true;
838
839   /* If both references are through the same type, they do not alias
840      if the accesses do not overlap.  This does extra disambiguation
841      for mixed/pointer accesses but requires strict aliasing.  */
842   if ((TREE_CODE (base1) != MEM_REF
843        || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
844       && (TREE_CODE (base2) != MEM_REF
845           || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
846       && same_type_for_tbaa (TREE_TYPE (ptrtype1),
847                              TREE_TYPE (ptrtype2)) == 1)
848     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
849
850   /* Do type-based disambiguation.  */
851   if (base1_alias_set != base2_alias_set
852       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
853     return false;
854
855   /* Do access-path based disambiguation.  */
856   if (ref1 && ref2
857       && handled_component_p (ref1)
858       && handled_component_p (ref2)
859       && (TREE_CODE (base1) != MEM_REF
860           || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
861       && (TREE_CODE (base2) != MEM_REF
862           || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
863     return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
864                                       ref1_alias_set, base1_alias_set,
865                                       offset1, max_size1,
866                                       ref2, TREE_TYPE (ptrtype2),
867                                       ref2_alias_set, base2_alias_set,
868                                       offset2, max_size2, false);
869
870   return true;
871 }
872
873 /* Return true, if the two memory references REF1 and REF2 may alias.  */
874
875 bool
876 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
877 {
878   tree base1, base2;
879   HOST_WIDE_INT offset1 = 0, offset2 = 0;
880   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
881   bool var1_p, var2_p, ind1_p, ind2_p;
882
883   gcc_checking_assert ((!ref1->ref
884                         || TREE_CODE (ref1->ref) == SSA_NAME
885                         || DECL_P (ref1->ref)
886                         || handled_component_p (ref1->ref)
887                         || INDIRECT_REF_P (ref1->ref)
888                         || TREE_CODE (ref1->ref) == MEM_REF
889                         || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
890                        && (!ref2->ref
891                            || TREE_CODE (ref2->ref) == SSA_NAME
892                            || DECL_P (ref2->ref)
893                            || handled_component_p (ref2->ref)
894                            || INDIRECT_REF_P (ref2->ref)
895                            || TREE_CODE (ref2->ref) == MEM_REF
896                            || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
897
898   /* Decompose the references into their base objects and the access.  */
899   base1 = ao_ref_base (ref1);
900   offset1 = ref1->offset;
901   max_size1 = ref1->max_size;
902   base2 = ao_ref_base (ref2);
903   offset2 = ref2->offset;
904   max_size2 = ref2->max_size;
905
906   /* We can end up with registers or constants as bases for example from
907      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
908      which is seen as a struct copy.  */
909   if (TREE_CODE (base1) == SSA_NAME
910       || TREE_CODE (base2) == SSA_NAME
911       || TREE_CODE (base1) == CONST_DECL
912       || TREE_CODE (base2) == CONST_DECL
913       || is_gimple_min_invariant (base1)
914       || is_gimple_min_invariant (base2))
915     return false;
916
917   /* We can end up refering to code via function and label decls.
918      As we likely do not properly track code aliases conservatively
919      bail out.  */
920   if (TREE_CODE (base1) == FUNCTION_DECL
921       || TREE_CODE (base2) == FUNCTION_DECL
922       || TREE_CODE (base1) == LABEL_DECL
923       || TREE_CODE (base2) == LABEL_DECL)
924     return true;
925
926   /* Defer to simple offset based disambiguation if we have
927      references based on two decls.  Do this before defering to
928      TBAA to handle must-alias cases in conformance with the
929      GCC extension of allowing type-punning through unions.  */
930   var1_p = SSA_VAR_P (base1);
931   var2_p = SSA_VAR_P (base2);
932   if (var1_p && var2_p)
933     return decl_refs_may_alias_p (base1, offset1, max_size1,
934                                   base2, offset2, max_size2);
935
936   ind1_p = INDIRECT_REF_P (base1) || (TREE_CODE (base1) == MEM_REF);
937   ind2_p = INDIRECT_REF_P (base2) || (TREE_CODE (base2) == MEM_REF);
938
939   /* Canonicalize the pointer-vs-decl case.  */
940   if (ind1_p && var2_p)
941     {
942       HOST_WIDE_INT tmp1;
943       tree tmp2;
944       ao_ref *tmp3;
945       tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
946       tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
947       tmp2 = base1; base1 = base2; base2 = tmp2;
948       tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
949       var1_p = true;
950       ind1_p = false;
951       var2_p = false;
952       ind2_p = true;
953     }
954
955   /* First defer to TBAA if possible.  */
956   if (tbaa_p
957       && flag_strict_aliasing
958       && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
959                                  ao_ref_alias_set (ref2)))
960     return false;
961
962   /* If one reference is a TARGET_MEM_REF weird things are allowed.  Still
963      TBAA disambiguation based on the access type is possible, so bail
964      out only after that check.  */
965   if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
966       || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
967     return true;
968
969   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
970   if (var1_p && ind2_p)
971     return indirect_ref_may_alias_decl_p (ref2->ref, base2,
972                                           offset2, max_size2,
973                                           ao_ref_alias_set (ref2), -1,
974                                           ref1->ref, base1,
975                                           offset1, max_size1,
976                                           ao_ref_alias_set (ref1),
977                                           ao_ref_base_alias_set (ref1),
978                                           tbaa_p);
979   else if (ind1_p && ind2_p)
980     return indirect_refs_may_alias_p (ref1->ref, base1,
981                                       offset1, max_size1,
982                                       ao_ref_alias_set (ref1), -1,
983                                       ref2->ref, base2,
984                                       offset2, max_size2,
985                                       ao_ref_alias_set (ref2), -1,
986                                       tbaa_p);
987
988   gcc_unreachable ();
989 }
990
991 bool
992 refs_may_alias_p (tree ref1, tree ref2)
993 {
994   ao_ref r1, r2;
995   bool res;
996   ao_ref_init (&r1, ref1);
997   ao_ref_init (&r2, ref2);
998   res = refs_may_alias_p_1 (&r1, &r2, true);
999   if (res)
1000     ++alias_stats.refs_may_alias_p_may_alias;
1001   else
1002     ++alias_stats.refs_may_alias_p_no_alias;
1003   return res;
1004 }
1005
1006 /* Returns true if there is a anti-dependence for the STORE that
1007    executes after the LOAD.  */
1008
1009 bool
1010 refs_anti_dependent_p (tree load, tree store)
1011 {
1012   ao_ref r1, r2;
1013   ao_ref_init (&r1, load);
1014   ao_ref_init (&r2, store);
1015   return refs_may_alias_p_1 (&r1, &r2, false);
1016 }
1017
1018 /* Returns true if there is a output dependence for the stores
1019    STORE1 and STORE2.  */
1020
1021 bool
1022 refs_output_dependent_p (tree store1, tree store2)
1023 {
1024   ao_ref r1, r2;
1025   ao_ref_init (&r1, store1);
1026   ao_ref_init (&r2, store2);
1027   return refs_may_alias_p_1 (&r1, &r2, false);
1028 }
1029
1030 /* If the call CALL may use the memory reference REF return true,
1031    otherwise return false.  */
1032
1033 static bool
1034 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1035 {
1036   tree base, callee;
1037   unsigned i;
1038   int flags = gimple_call_flags (call);
1039
1040   /* Const functions without a static chain do not implicitly use memory.  */
1041   if (!gimple_call_chain (call)
1042       && (flags & (ECF_CONST|ECF_NOVOPS)))
1043     goto process_args;
1044
1045   base = ao_ref_base (ref);
1046   if (!base)
1047     return true;
1048
1049   /* If the reference is based on a decl that is not aliased the call
1050      cannot possibly use it.  */
1051   if (DECL_P (base)
1052       && !may_be_aliased (base)
1053       /* But local statics can be used through recursion.  */
1054       && !is_global_var (base))
1055     goto process_args;
1056
1057   callee = gimple_call_fndecl (call);
1058
1059   /* Handle those builtin functions explicitly that do not act as
1060      escape points.  See tree-ssa-structalias.c:find_func_aliases
1061      for the list of builtins we might need to handle here.  */
1062   if (callee != NULL_TREE
1063       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1064     switch (DECL_FUNCTION_CODE (callee))
1065       {
1066         /* All the following functions clobber memory pointed to by
1067            their first argument.  */
1068         case BUILT_IN_STRCPY:
1069         case BUILT_IN_STRNCPY:
1070         case BUILT_IN_MEMCPY:
1071         case BUILT_IN_MEMMOVE:
1072         case BUILT_IN_MEMPCPY:
1073         case BUILT_IN_STPCPY:
1074         case BUILT_IN_STPNCPY:
1075         case BUILT_IN_STRCAT:
1076         case BUILT_IN_STRNCAT:
1077           {
1078             ao_ref dref;
1079             tree size = NULL_TREE;
1080             if (gimple_call_num_args (call) == 3)
1081               size = gimple_call_arg (call, 2);
1082             ao_ref_init_from_ptr_and_size (&dref,
1083                                            gimple_call_arg (call, 1),
1084                                            size);
1085             return refs_may_alias_p_1 (&dref, ref, false);
1086           }
1087         case BUILT_IN_BCOPY:
1088           {
1089             ao_ref dref;
1090             tree size = gimple_call_arg (call, 2);
1091             ao_ref_init_from_ptr_and_size (&dref,
1092                                            gimple_call_arg (call, 0),
1093                                            size);
1094             return refs_may_alias_p_1 (&dref, ref, false);
1095           }
1096         /* The following builtins do not read from memory.  */
1097         case BUILT_IN_FREE:
1098         case BUILT_IN_MALLOC:
1099         case BUILT_IN_CALLOC:
1100         case BUILT_IN_MEMSET:
1101         case BUILT_IN_FREXP:
1102         case BUILT_IN_FREXPF:
1103         case BUILT_IN_FREXPL:
1104         case BUILT_IN_GAMMA_R:
1105         case BUILT_IN_GAMMAF_R:
1106         case BUILT_IN_GAMMAL_R:
1107         case BUILT_IN_LGAMMA_R:
1108         case BUILT_IN_LGAMMAF_R:
1109         case BUILT_IN_LGAMMAL_R:
1110         case BUILT_IN_MODF:
1111         case BUILT_IN_MODFF:
1112         case BUILT_IN_MODFL:
1113         case BUILT_IN_REMQUO:
1114         case BUILT_IN_REMQUOF:
1115         case BUILT_IN_REMQUOL:
1116         case BUILT_IN_SINCOS:
1117         case BUILT_IN_SINCOSF:
1118         case BUILT_IN_SINCOSL:
1119           return false;
1120
1121         default:
1122           /* Fallthru to general call handling.  */;
1123       }
1124
1125   /* Check if base is a global static variable that is not read
1126      by the function.  */
1127   if (TREE_CODE (base) == VAR_DECL
1128       && TREE_STATIC (base))
1129     {
1130       bitmap not_read;
1131
1132       if (callee != NULL_TREE
1133           && (not_read
1134                 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1135           && bitmap_bit_p (not_read, DECL_UID (base)))
1136         goto process_args;
1137     }
1138
1139   /* Check if the base variable is call-used.  */
1140   if (DECL_P (base))
1141     {
1142       if (pt_solution_includes (gimple_call_use_set (call), base))
1143         return true;
1144     }
1145   else if ((INDIRECT_REF_P (base)
1146             || TREE_CODE (base) == MEM_REF)
1147            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1148     {
1149       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1150       if (!pi)
1151         return true;
1152
1153       if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1154         return true;
1155     }
1156   else
1157     return true;
1158
1159   /* Inspect call arguments for passed-by-value aliases.  */
1160 process_args:
1161   for (i = 0; i < gimple_call_num_args (call); ++i)
1162     {
1163       tree op = gimple_call_arg (call, i);
1164       int flags = gimple_call_arg_flags (call, i);
1165
1166       if (flags & EAF_UNUSED)
1167         continue;
1168
1169       if (TREE_CODE (op) == WITH_SIZE_EXPR)
1170         op = TREE_OPERAND (op, 0);
1171
1172       if (TREE_CODE (op) != SSA_NAME
1173           && !is_gimple_min_invariant (op))
1174         {
1175           ao_ref r;
1176           ao_ref_init (&r, op);
1177           if (refs_may_alias_p_1 (&r, ref, true))
1178             return true;
1179         }
1180     }
1181
1182   return false;
1183 }
1184
1185 static bool
1186 ref_maybe_used_by_call_p (gimple call, tree ref)
1187 {
1188   ao_ref r;
1189   bool res;
1190   ao_ref_init (&r, ref);
1191   res = ref_maybe_used_by_call_p_1 (call, &r);
1192   if (res)
1193     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1194   else
1195     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1196   return res;
1197 }
1198
1199
1200 /* If the statement STMT may use the memory reference REF return
1201    true, otherwise return false.  */
1202
1203 bool
1204 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1205 {
1206   if (is_gimple_assign (stmt))
1207     {
1208       tree rhs;
1209
1210       /* All memory assign statements are single.  */
1211       if (!gimple_assign_single_p (stmt))
1212         return false;
1213
1214       rhs = gimple_assign_rhs1 (stmt);
1215       if (is_gimple_reg (rhs)
1216           || is_gimple_min_invariant (rhs)
1217           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1218         return false;
1219
1220       return refs_may_alias_p (rhs, ref);
1221     }
1222   else if (is_gimple_call (stmt))
1223     return ref_maybe_used_by_call_p (stmt, ref);
1224
1225   return true;
1226 }
1227
1228 /* If the call in statement CALL may clobber the memory reference REF
1229    return true, otherwise return false.  */
1230
1231 static bool
1232 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1233 {
1234   tree base;
1235   tree callee;
1236
1237   /* If the call is pure or const it cannot clobber anything.  */
1238   if (gimple_call_flags (call)
1239       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1240     return false;
1241
1242   base = ao_ref_base (ref);
1243   if (!base)
1244     return true;
1245
1246   if (TREE_CODE (base) == SSA_NAME
1247       || CONSTANT_CLASS_P (base))
1248     return false;
1249
1250   /* If the reference is based on a decl that is not aliased the call
1251      cannot possibly clobber it.  */
1252   if (DECL_P (base)
1253       && !may_be_aliased (base)
1254       /* But local non-readonly statics can be modified through recursion
1255          or the call may implement a threading barrier which we must
1256          treat as may-def.  */
1257       && (TREE_READONLY (base)
1258           || !is_global_var (base)))
1259     return false;
1260
1261   callee = gimple_call_fndecl (call);
1262
1263   /* Handle those builtin functions explicitly that do not act as
1264      escape points.  See tree-ssa-structalias.c:find_func_aliases
1265      for the list of builtins we might need to handle here.  */
1266   if (callee != NULL_TREE
1267       && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1268     switch (DECL_FUNCTION_CODE (callee))
1269       {
1270         /* All the following functions clobber memory pointed to by
1271            their first argument.  */
1272         case BUILT_IN_STRCPY:
1273         case BUILT_IN_STRNCPY:
1274         case BUILT_IN_MEMCPY:
1275         case BUILT_IN_MEMMOVE:
1276         case BUILT_IN_MEMPCPY:
1277         case BUILT_IN_STPCPY:
1278         case BUILT_IN_STPNCPY:
1279         case BUILT_IN_STRCAT:
1280         case BUILT_IN_STRNCAT:
1281         case BUILT_IN_MEMSET:
1282           {
1283             ao_ref dref;
1284             tree size = NULL_TREE;
1285             if (gimple_call_num_args (call) == 3)
1286               size = gimple_call_arg (call, 2);
1287             ao_ref_init_from_ptr_and_size (&dref,
1288                                            gimple_call_arg (call, 0),
1289                                            size);
1290             return refs_may_alias_p_1 (&dref, ref, false);
1291           }
1292         case BUILT_IN_BCOPY:
1293           {
1294             ao_ref dref;
1295             tree size = gimple_call_arg (call, 2);
1296             ao_ref_init_from_ptr_and_size (&dref,
1297                                            gimple_call_arg (call, 1),
1298                                            size);
1299             return refs_may_alias_p_1 (&dref, ref, false);
1300           }
1301         /* Allocating memory does not have any side-effects apart from
1302            being the definition point for the pointer.  */
1303         case BUILT_IN_MALLOC:
1304         case BUILT_IN_CALLOC:
1305           /* Unix98 specifies that errno is set on allocation failure.
1306              Until we properly can track the errno location assume it
1307              is not a local decl but external or anonymous storage in
1308              a different translation unit.  Also assume it is of
1309              type int as required by the standard.  */
1310           if (flag_errno_math
1311               && TREE_TYPE (base) == integer_type_node)
1312             {
1313               struct ptr_info_def *pi;
1314               if (DECL_P (base)
1315                   && !TREE_STATIC (base))
1316                 return true;
1317               else if ((INDIRECT_REF_P (base)
1318                         || TREE_CODE (base) == MEM_REF)
1319                        && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1320                        && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1321                 return pi->pt.anything || pi->pt.nonlocal;
1322             }
1323           return false;
1324         /* Freeing memory kills the pointed-to memory.  More importantly
1325            the call has to serve as a barrier for moving loads and stores
1326            across it.  */
1327         case BUILT_IN_FREE:
1328           {
1329             tree ptr = gimple_call_arg (call, 0);
1330             return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1331           }
1332         case BUILT_IN_GAMMA_R:
1333         case BUILT_IN_GAMMAF_R:
1334         case BUILT_IN_GAMMAL_R:
1335         case BUILT_IN_LGAMMA_R:
1336         case BUILT_IN_LGAMMAF_R:
1337         case BUILT_IN_LGAMMAL_R:
1338           {
1339             tree out = gimple_call_arg (call, 1);
1340             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1341               return true;
1342             if (flag_errno_math)
1343               break;
1344             return false;
1345           }
1346         case BUILT_IN_FREXP:
1347         case BUILT_IN_FREXPF:
1348         case BUILT_IN_FREXPL:
1349         case BUILT_IN_MODF:
1350         case BUILT_IN_MODFF:
1351         case BUILT_IN_MODFL:
1352           {
1353             tree out = gimple_call_arg (call, 1);
1354             return ptr_deref_may_alias_ref_p_1 (out, ref);
1355           }
1356         case BUILT_IN_REMQUO:
1357         case BUILT_IN_REMQUOF:
1358         case BUILT_IN_REMQUOL:
1359           {
1360             tree out = gimple_call_arg (call, 2);
1361             if (ptr_deref_may_alias_ref_p_1 (out, ref))
1362               return true;
1363             if (flag_errno_math)
1364               break;
1365             return false;
1366           }
1367         case BUILT_IN_SINCOS:
1368         case BUILT_IN_SINCOSF:
1369         case BUILT_IN_SINCOSL:
1370           {
1371             tree sin = gimple_call_arg (call, 1);
1372             tree cos = gimple_call_arg (call, 2);
1373             return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1374                     || ptr_deref_may_alias_ref_p_1 (cos, ref));
1375           }
1376         default:
1377           /* Fallthru to general call handling.  */;
1378       }
1379
1380   /* Check if base is a global static variable that is not written
1381      by the function.  */
1382   if (callee != NULL_TREE
1383       && TREE_CODE (base) == VAR_DECL
1384       && TREE_STATIC (base))
1385     {
1386       bitmap not_written;
1387
1388       if ((not_written
1389              = ipa_reference_get_not_written_global (cgraph_node (callee)))
1390           && bitmap_bit_p (not_written, DECL_UID (base)))
1391         return false;
1392     }
1393
1394   /* Check if the base variable is call-clobbered.  */
1395   if (DECL_P (base))
1396     return pt_solution_includes (gimple_call_clobber_set (call), base);
1397   else if ((INDIRECT_REF_P (base)
1398             || TREE_CODE (base) == MEM_REF)
1399            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1400     {
1401       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1402       if (!pi)
1403         return true;
1404
1405       return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1406     }
1407
1408   return true;
1409 }
1410
1411 /* If the call in statement CALL may clobber the memory reference REF
1412    return true, otherwise return false.  */
1413
1414 bool
1415 call_may_clobber_ref_p (gimple call, tree ref)
1416 {
1417   bool res;
1418   ao_ref r;
1419   ao_ref_init (&r, ref);
1420   res = call_may_clobber_ref_p_1 (call, &r);
1421   if (res)
1422     ++alias_stats.call_may_clobber_ref_p_may_alias;
1423   else
1424     ++alias_stats.call_may_clobber_ref_p_no_alias;
1425   return res;
1426 }
1427
1428
1429 /* If the statement STMT may clobber the memory reference REF return true,
1430    otherwise return false.  */
1431
1432 bool
1433 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1434 {
1435   if (is_gimple_call (stmt))
1436     {
1437       tree lhs = gimple_call_lhs (stmt);
1438       if (lhs
1439           && !is_gimple_reg (lhs))
1440         {
1441           ao_ref r;
1442           ao_ref_init (&r, lhs);
1443           if (refs_may_alias_p_1 (ref, &r, true))
1444             return true;
1445         }
1446
1447       return call_may_clobber_ref_p_1 (stmt, ref);
1448     }
1449   else if (gimple_assign_single_p (stmt))
1450     {
1451       tree lhs = gimple_assign_lhs (stmt);
1452       if (!is_gimple_reg (lhs))
1453         {
1454           ao_ref r;
1455           ao_ref_init (&r, gimple_assign_lhs (stmt));
1456           return refs_may_alias_p_1 (ref, &r, true);
1457         }
1458     }
1459   else if (gimple_code (stmt) == GIMPLE_ASM)
1460     return true;
1461
1462   return false;
1463 }
1464
1465 bool
1466 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1467 {
1468   ao_ref r;
1469   ao_ref_init (&r, ref);
1470   return stmt_may_clobber_ref_p_1 (stmt, &r);
1471 }
1472
1473
1474 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1475    TARGET or a statement clobbering the memory reference REF in which
1476    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
1477
1478 static bool
1479 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1480                   tree vuse, bitmap *visited)
1481 {
1482   if (!*visited)
1483     *visited = BITMAP_ALLOC (NULL);
1484
1485   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1486
1487   /* Walk until we hit the target.  */
1488   while (vuse != target)
1489     {
1490       gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1491       /* Recurse for PHI nodes.  */
1492       if (gimple_code (def_stmt) == GIMPLE_PHI)
1493         {
1494           /* An already visited PHI node ends the walk successfully.  */
1495           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1496             return true;
1497           vuse = get_continuation_for_phi (def_stmt, ref, visited);
1498           if (!vuse)
1499             return false;
1500           continue;
1501         }
1502       /* A clobbering statement or the end of the IL ends it failing.  */
1503       else if (gimple_nop_p (def_stmt)
1504                || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1505         return false;
1506       vuse = gimple_vuse (def_stmt);
1507     }
1508   return true;
1509 }
1510
1511 /* Starting from a PHI node for the virtual operand of the memory reference
1512    REF find a continuation virtual operand that allows to continue walking
1513    statements dominating PHI skipping only statements that cannot possibly
1514    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
1515    be found.  */
1516
1517 tree
1518 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1519 {
1520   unsigned nargs = gimple_phi_num_args (phi);
1521
1522   /* Through a single-argument PHI we can simply look through.  */
1523   if (nargs == 1)
1524     return PHI_ARG_DEF (phi, 0);
1525
1526   /* For two arguments try to skip non-aliasing code until we hit
1527      the phi argument definition that dominates the other one.  */
1528   if (nargs == 2)
1529     {
1530       tree arg0 = PHI_ARG_DEF (phi, 0);
1531       tree arg1 = PHI_ARG_DEF (phi, 1);
1532       gimple def0 = SSA_NAME_DEF_STMT (arg0);
1533       gimple def1 = SSA_NAME_DEF_STMT (arg1);
1534       tree common_vuse;
1535
1536       if (arg0 == arg1)
1537         return arg0;
1538       else if (gimple_nop_p (def0)
1539                || (!gimple_nop_p (def1)
1540                    && dominated_by_p (CDI_DOMINATORS,
1541                                       gimple_bb (def1), gimple_bb (def0))))
1542         {
1543           if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1544             return arg0;
1545         }
1546       else if (gimple_nop_p (def1)
1547                || dominated_by_p (CDI_DOMINATORS,
1548                                   gimple_bb (def0), gimple_bb (def1)))
1549         {
1550           if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1551             return arg1;
1552         }
1553       /* Special case of a diamond:
1554            MEM_1 = ...
1555            goto (cond) ? L1 : L2
1556            L1: store1 = ...    #MEM_2 = vuse(MEM_1)
1557                goto L3
1558            L2: store2 = ...    #MEM_3 = vuse(MEM_1)
1559            L3: MEM_4 = PHI<MEM_2, MEM_3>
1560          We were called with the PHI at L3, MEM_2 and MEM_3 don't
1561          dominate each other, but still we can easily skip this PHI node
1562          if we recognize that the vuse MEM operand is the same for both,
1563          and that we can skip both statements (they don't clobber us).
1564          This is still linear.  Don't use maybe_skip_until, that might
1565          potentially be slow.  */
1566       else if ((common_vuse = gimple_vuse (def0))
1567                && common_vuse == gimple_vuse (def1))
1568         {
1569           if (!stmt_may_clobber_ref_p_1 (def0, ref)
1570               && !stmt_may_clobber_ref_p_1 (def1, ref))
1571             return common_vuse;
1572         }
1573     }
1574
1575   return NULL_TREE;
1576 }
1577
1578 /* Based on the memory reference REF and its virtual use VUSE call
1579    WALKER for each virtual use that is equivalent to VUSE, including VUSE
1580    itself.  That is, for each virtual use for which its defining statement
1581    does not clobber REF.
1582
1583    WALKER is called with REF, the current virtual use and DATA.  If
1584    WALKER returns non-NULL the walk stops and its result is returned.
1585    At the end of a non-successful walk NULL is returned.
1586
1587    TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1588    use which definition is a statement that may clobber REF and DATA.
1589    If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1590    If TRANSLATE returns non-NULL the walk stops and its result is returned.
1591    If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1592    to adjust REF and *DATA to make that valid.
1593
1594    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
1595
1596 void *
1597 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1598                         void *(*walker)(ao_ref *, tree, void *),
1599                         void *(*translate)(ao_ref *, tree, void *), void *data)
1600 {
1601   bitmap visited = NULL;
1602   void *res;
1603
1604   timevar_push (TV_ALIAS_STMT_WALK);
1605
1606   do
1607     {
1608       gimple def_stmt;
1609
1610       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1611       res = (*walker) (ref, vuse, data);
1612       if (res)
1613         break;
1614
1615       def_stmt = SSA_NAME_DEF_STMT (vuse);
1616       if (gimple_nop_p (def_stmt))
1617         break;
1618       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1619         vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1620       else
1621         {
1622           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1623             {
1624               if (!translate)
1625                 break;
1626               res = (*translate) (ref, vuse, data);
1627               /* Failed lookup and translation.  */
1628               if (res == (void *)-1)
1629                 {
1630                   res = NULL;
1631                   break;
1632                 }
1633               /* Lookup succeeded.  */
1634               else if (res != NULL)
1635                 break;
1636               /* Translation succeeded, continue walking.  */
1637             }
1638           vuse = gimple_vuse (def_stmt);
1639         }
1640     }
1641   while (vuse);
1642
1643   if (visited)
1644     BITMAP_FREE (visited);
1645
1646   timevar_pop (TV_ALIAS_STMT_WALK);
1647
1648   return res;
1649 }
1650
1651
1652 /* Based on the memory reference REF call WALKER for each vdef which
1653    defining statement may clobber REF, starting with VDEF.  If REF
1654    is NULL_TREE, each defining statement is visited.
1655
1656    WALKER is called with REF, the current vdef and DATA.  If WALKER
1657    returns true the walk is stopped, otherwise it continues.
1658
1659    At PHI nodes walk_aliased_vdefs forks into one walk for reach
1660    PHI argument (but only one walk continues on merge points), the
1661    return value is true if any of the walks was successful.
1662
1663    The function returns the number of statements walked.  */
1664
1665 static unsigned int
1666 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1667                       bool (*walker)(ao_ref *, tree, void *), void *data,
1668                       bitmap *visited, unsigned int cnt)
1669 {
1670   do
1671     {
1672       gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1673
1674       if (*visited
1675           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1676         return cnt;
1677
1678       if (gimple_nop_p (def_stmt))
1679         return cnt;
1680       else if (gimple_code (def_stmt) == GIMPLE_PHI)
1681         {
1682           unsigned i;
1683           if (!*visited)
1684             *visited = BITMAP_ALLOC (NULL);
1685           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1686             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1687                                          walker, data, visited, 0);
1688           return cnt;
1689         }
1690
1691       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1692       cnt++;
1693       if ((!ref
1694            || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1695           && (*walker) (ref, vdef, data))
1696         return cnt;
1697
1698       vdef = gimple_vuse (def_stmt);
1699     }
1700   while (1);
1701 }
1702
1703 unsigned int
1704 walk_aliased_vdefs (ao_ref *ref, tree vdef,
1705                     bool (*walker)(ao_ref *, tree, void *), void *data,
1706                     bitmap *visited)
1707 {
1708   bitmap local_visited = NULL;
1709   unsigned int ret;
1710
1711   timevar_push (TV_ALIAS_STMT_WALK);
1712
1713   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1714                               visited ? visited : &local_visited, 0);
1715   if (local_visited)
1716     BITMAP_FREE (local_visited);
1717
1718   timevar_pop (TV_ALIAS_STMT_WALK);
1719
1720   return ret;
1721 }
1722