OSDN Git Service

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