a5bd8314a0a73f3522060341a49eafc239641323
[platform/upstream/gcc.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2    Copyright (C) 2004-2015 Free Software Foundation, Inc.
3    Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "rtl.h"
28 #include "ssa.h"
29 #include "alias.h"
30 #include "fold-const.h"
31 #include "tm_p.h"
32 #include "target.h"
33
34 #include "dominance.h"
35 #include "timevar.h"    /* for TV_ALIAS_STMT_WALK */
36 #include "langhooks.h"
37 #include "flags.h"
38 #include "tree-pretty-print.h"
39 #include "dumpfile.h"
40 #include "internal-fn.h"
41 #include "tree-eh.h"
42 #include "insn-config.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "calls.h"
47 #include "emit-rtl.h"
48 #include "varasm.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "tree-dfa.h"
52 #include "tree-inline.h"
53 #include "params.h"
54 #include "alloc-pool.h"
55 #include "cgraph.h"
56 #include "ipa-reference.h"
57
58 /* Broad overview of how alias analysis on gimple works:
59
60    Statements clobbering or using memory are linked through the
61    virtual operand factored use-def chain.  The virtual operand
62    is unique per function, its symbol is accessible via gimple_vop (cfun).
63    Virtual operands are used for efficiently walking memory statements
64    in the gimple IL and are useful for things like value-numbering as
65    a generation count for memory references.
66
67    SSA_NAME pointers may have associated points-to information
68    accessible via the SSA_NAME_PTR_INFO macro.  Flow-insensitive
69    points-to information is (re-)computed by the TODO_rebuild_alias
70    pass manager todo.  Points-to information is also used for more
71    precise tracking of call-clobbered and call-used variables and
72    related disambiguations.
73
74    This file contains functions for disambiguating memory references,
75    the so called alias-oracle and tools for walking of the gimple IL.
76
77    The main alias-oracle entry-points are
78
79    bool stmt_may_clobber_ref_p (gimple *, tree)
80
81      This function queries if a statement may invalidate (parts of)
82      the memory designated by the reference tree argument.
83
84    bool ref_maybe_used_by_stmt_p (gimple *, tree)
85
86      This function queries if a statement may need (parts of) the
87      memory designated by the reference tree argument.
88
89    There are variants of these functions that only handle the call
90    part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
91    Note that these do not disambiguate against a possible call lhs.
92
93    bool refs_may_alias_p (tree, tree)
94
95      This function tries to disambiguate two reference trees.
96
97    bool ptr_deref_may_alias_global_p (tree)
98
99      This function queries if dereferencing a pointer variable may
100      alias global memory.
101
102    More low-level disambiguators are available and documented in
103    this file.  Low-level disambiguators dealing with points-to
104    information are in tree-ssa-structalias.c.  */
105
106
107 /* Query statistics for the different low-level disambiguators.
108    A high-level query may trigger multiple of them.  */
109
110 static struct {
111   unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
112   unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
113   unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
114   unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
115   unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
116   unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
117 } alias_stats;
118
119 void
120 dump_alias_stats (FILE *s)
121 {
122   fprintf (s, "\nAlias oracle query stats:\n");
123   fprintf (s, "  refs_may_alias_p: "
124            HOST_WIDE_INT_PRINT_DEC" disambiguations, "
125            HOST_WIDE_INT_PRINT_DEC" queries\n",
126            alias_stats.refs_may_alias_p_no_alias,
127            alias_stats.refs_may_alias_p_no_alias
128            + alias_stats.refs_may_alias_p_may_alias);
129   fprintf (s, "  ref_maybe_used_by_call_p: "
130            HOST_WIDE_INT_PRINT_DEC" disambiguations, "
131            HOST_WIDE_INT_PRINT_DEC" queries\n",
132            alias_stats.ref_maybe_used_by_call_p_no_alias,
133            alias_stats.refs_may_alias_p_no_alias
134            + alias_stats.ref_maybe_used_by_call_p_may_alias);
135   fprintf (s, "  call_may_clobber_ref_p: "
136            HOST_WIDE_INT_PRINT_DEC" disambiguations, "
137            HOST_WIDE_INT_PRINT_DEC" queries\n",
138            alias_stats.call_may_clobber_ref_p_no_alias,
139            alias_stats.call_may_clobber_ref_p_no_alias
140            + alias_stats.call_may_clobber_ref_p_may_alias);
141   dump_alias_stats_in_alias_c (s);
142 }
143
144
145 /* Return true, if dereferencing PTR may alias with a global variable.  */
146
147 bool
148 ptr_deref_may_alias_global_p (tree ptr)
149 {
150   struct ptr_info_def *pi;
151
152   /* If we end up with a pointer constant here that may point
153      to global memory.  */
154   if (TREE_CODE (ptr) != SSA_NAME)
155     return true;
156
157   pi = SSA_NAME_PTR_INFO (ptr);
158
159   /* If we do not have points-to information for this variable,
160      we have to punt.  */
161   if (!pi)
162     return true;
163
164   /* ???  This does not use TBAA to prune globals ptr may not access.  */
165   return pt_solution_includes_global (&pi->pt);
166 }
167
168 /* Return true if dereferencing PTR may alias DECL.
169    The caller is responsible for applying TBAA to see if PTR
170    may access DECL at all.  */
171
172 static bool
173 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
174 {
175   struct ptr_info_def *pi;
176
177   /* Conversions are irrelevant for points-to information and
178      data-dependence analysis can feed us those.  */
179   STRIP_NOPS (ptr);
180
181   /* Anything we do not explicilty handle aliases.  */
182   if ((TREE_CODE (ptr) != SSA_NAME
183        && TREE_CODE (ptr) != ADDR_EXPR
184        && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
185       || !POINTER_TYPE_P (TREE_TYPE (ptr))
186       || (TREE_CODE (decl) != VAR_DECL
187           && TREE_CODE (decl) != PARM_DECL
188           && TREE_CODE (decl) != RESULT_DECL))
189     return true;
190
191   /* Disregard pointer offsetting.  */
192   if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
193     {
194       do
195         {
196           ptr = TREE_OPERAND (ptr, 0);
197         }
198       while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
199       return ptr_deref_may_alias_decl_p (ptr, decl);
200     }
201
202   /* ADDR_EXPR pointers either just offset another pointer or directly
203      specify the pointed-to set.  */
204   if (TREE_CODE (ptr) == ADDR_EXPR)
205     {
206       tree base = get_base_address (TREE_OPERAND (ptr, 0));
207       if (base
208           && (TREE_CODE (base) == MEM_REF
209               || TREE_CODE (base) == TARGET_MEM_REF))
210         ptr = TREE_OPERAND (base, 0);
211       else if (base
212                && DECL_P (base))
213         return base == decl;
214       else if (base
215                && CONSTANT_CLASS_P (base))
216         return false;
217       else
218         return true;
219     }
220
221   /* Non-aliased variables can not be pointed to.  */
222   if (!may_be_aliased (decl))
223     return false;
224
225   /* If we do not have useful points-to information for this pointer
226      we cannot disambiguate anything else.  */
227   pi = SSA_NAME_PTR_INFO (ptr);
228   if (!pi)
229     return true;
230
231   return pt_solution_includes (&pi->pt, decl);
232 }
233
234 /* Return true if dereferenced PTR1 and PTR2 may alias.
235    The caller is responsible for applying TBAA to see if accesses
236    through PTR1 and PTR2 may conflict at all.  */
237
238 bool
239 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
240 {
241   struct ptr_info_def *pi1, *pi2;
242
243   /* Conversions are irrelevant for points-to information and
244      data-dependence analysis can feed us those.  */
245   STRIP_NOPS (ptr1);
246   STRIP_NOPS (ptr2);
247
248   /* Disregard pointer offsetting.  */
249   if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
250     {
251       do
252         {
253           ptr1 = TREE_OPERAND (ptr1, 0);
254         }
255       while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
256       return ptr_derefs_may_alias_p (ptr1, ptr2);
257     }
258   if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
259     {
260       do
261         {
262           ptr2 = TREE_OPERAND (ptr2, 0);
263         }
264       while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
265       return ptr_derefs_may_alias_p (ptr1, ptr2);
266     }
267
268   /* ADDR_EXPR pointers either just offset another pointer or directly
269      specify the pointed-to set.  */
270   if (TREE_CODE (ptr1) == ADDR_EXPR)
271     {
272       tree base = get_base_address (TREE_OPERAND (ptr1, 0));
273       if (base
274           && (TREE_CODE (base) == MEM_REF
275               || TREE_CODE (base) == TARGET_MEM_REF))
276         return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
277       else if (base
278                && DECL_P (base))
279         return ptr_deref_may_alias_decl_p (ptr2, base);
280       else
281         return true;
282     }
283   if (TREE_CODE (ptr2) == ADDR_EXPR)
284     {
285       tree base = get_base_address (TREE_OPERAND (ptr2, 0));
286       if (base
287           && (TREE_CODE (base) == MEM_REF
288               || TREE_CODE (base) == TARGET_MEM_REF))
289         return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
290       else if (base
291                && DECL_P (base))
292         return ptr_deref_may_alias_decl_p (ptr1, base);
293       else
294         return true;
295     }
296
297   /* From here we require SSA name pointers.  Anything else aliases.  */
298   if (TREE_CODE (ptr1) != SSA_NAME
299       || TREE_CODE (ptr2) != SSA_NAME
300       || !POINTER_TYPE_P (TREE_TYPE (ptr1))
301       || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
302     return true;
303
304   /* We may end up with two empty points-to solutions for two same pointers.
305      In this case we still want to say both pointers alias, so shortcut
306      that here.  */
307   if (ptr1 == ptr2)
308     return true;
309
310   /* If we do not have useful points-to information for either pointer
311      we cannot disambiguate anything else.  */
312   pi1 = SSA_NAME_PTR_INFO (ptr1);
313   pi2 = SSA_NAME_PTR_INFO (ptr2);
314   if (!pi1 || !pi2)
315     return true;
316
317   /* ???  This does not use TBAA to prune decls from the intersection
318      that not both pointers may access.  */
319   return pt_solutions_intersect (&pi1->pt, &pi2->pt);
320 }
321
322 /* Return true if dereferencing PTR may alias *REF.
323    The caller is responsible for applying TBAA to see if PTR
324    may access *REF at all.  */
325
326 static bool
327 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
328 {
329   tree base = ao_ref_base (ref);
330
331   if (TREE_CODE (base) == MEM_REF
332       || TREE_CODE (base) == TARGET_MEM_REF)
333     return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
334   else if (DECL_P (base))
335     return ptr_deref_may_alias_decl_p (ptr, base);
336
337   return true;
338 }
339
340 /* Returns whether reference REF to BASE may refer to global memory.  */
341
342 static bool
343 ref_may_alias_global_p_1 (tree base)
344 {
345   if (DECL_P (base))
346     return is_global_var (base);
347   else if (TREE_CODE (base) == MEM_REF
348            || TREE_CODE (base) == TARGET_MEM_REF)
349     return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
350   return true;
351 }
352
353 bool
354 ref_may_alias_global_p (ao_ref *ref)
355 {
356   tree base = ao_ref_base (ref);
357   return ref_may_alias_global_p_1 (base);
358 }
359
360 bool
361 ref_may_alias_global_p (tree ref)
362 {
363   tree base = get_base_address (ref);
364   return ref_may_alias_global_p_1 (base);
365 }
366
367 /* Return true whether STMT may clobber global memory.  */
368
369 bool
370 stmt_may_clobber_global_p (gimple *stmt)
371 {
372   tree lhs;
373
374   if (!gimple_vdef (stmt))
375     return false;
376
377   /* ???  We can ask the oracle whether an artificial pointer
378      dereference with a pointer with points-to information covering
379      all global memory (what about non-address taken memory?) maybe
380      clobbered by this call.  As there is at the moment no convenient
381      way of doing that without generating garbage do some manual
382      checking instead.
383      ???  We could make a NULL ao_ref argument to the various
384      predicates special, meaning any global memory.  */
385
386   switch (gimple_code (stmt))
387     {
388     case GIMPLE_ASSIGN:
389       lhs = gimple_assign_lhs (stmt);
390       return (TREE_CODE (lhs) != SSA_NAME
391               && ref_may_alias_global_p (lhs));
392     case GIMPLE_CALL:
393       return true;
394     default:
395       return true;
396     }
397 }
398
399
400 /* Dump alias information on FILE.  */
401
402 void
403 dump_alias_info (FILE *file)
404 {
405   unsigned i;
406   const char *funcname
407     = lang_hooks.decl_printable_name (current_function_decl, 2);
408   tree var;
409
410   fprintf (file, "\n\nAlias information for %s\n\n", funcname);
411
412   fprintf (file, "Aliased symbols\n\n");
413
414   FOR_EACH_LOCAL_DECL (cfun, i, var)
415     {
416       if (may_be_aliased (var))
417         dump_variable (file, var);
418     }
419
420   fprintf (file, "\nCall clobber information\n");
421
422   fprintf (file, "\nESCAPED");
423   dump_points_to_solution (file, &cfun->gimple_df->escaped);
424
425   fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
426
427   for (i = 1; i < num_ssa_names; i++)
428     {
429       tree ptr = ssa_name (i);
430       struct ptr_info_def *pi;
431
432       if (ptr == NULL_TREE
433           || !POINTER_TYPE_P (TREE_TYPE (ptr))
434           || SSA_NAME_IN_FREE_LIST (ptr))
435         continue;
436
437       pi = SSA_NAME_PTR_INFO (ptr);
438       if (pi)
439         dump_points_to_info_for (file, ptr);
440     }
441
442   fprintf (file, "\n");
443 }
444
445
446 /* Dump alias information on stderr.  */
447
448 DEBUG_FUNCTION void
449 debug_alias_info (void)
450 {
451   dump_alias_info (stderr);
452 }
453
454
455 /* Dump the points-to set *PT into FILE.  */
456
457 void
458 dump_points_to_solution (FILE *file, struct pt_solution *pt)
459 {
460   if (pt->anything)
461     fprintf (file, ", points-to anything");
462
463   if (pt->nonlocal)
464     fprintf (file, ", points-to non-local");
465
466   if (pt->escaped)
467     fprintf (file, ", points-to escaped");
468
469   if (pt->ipa_escaped)
470     fprintf (file, ", points-to unit escaped");
471
472   if (pt->null)
473     fprintf (file, ", points-to NULL");
474
475   if (pt->vars)
476     {
477       fprintf (file, ", points-to vars: ");
478       dump_decl_set (file, pt->vars);
479       if (pt->vars_contains_nonlocal
480           && pt->vars_contains_escaped_heap)
481         fprintf (file, " (nonlocal, escaped heap)");
482       else if (pt->vars_contains_nonlocal
483                && pt->vars_contains_escaped)
484         fprintf (file, " (nonlocal, escaped)");
485       else if (pt->vars_contains_nonlocal)
486         fprintf (file, " (nonlocal)");
487       else if (pt->vars_contains_escaped_heap)
488         fprintf (file, " (escaped heap)");
489       else if (pt->vars_contains_escaped)
490         fprintf (file, " (escaped)");
491     }
492 }
493
494
495 /* Unified dump function for pt_solution.  */
496
497 DEBUG_FUNCTION void
498 debug (pt_solution &ref)
499 {
500   dump_points_to_solution (stderr, &ref);
501 }
502
503 DEBUG_FUNCTION void
504 debug (pt_solution *ptr)
505 {
506   if (ptr)
507     debug (*ptr);
508   else
509     fprintf (stderr, "<nil>\n");
510 }
511
512
513 /* Dump points-to information for SSA_NAME PTR into FILE.  */
514
515 void
516 dump_points_to_info_for (FILE *file, tree ptr)
517 {
518   struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
519
520   print_generic_expr (file, ptr, dump_flags);
521
522   if (pi)
523     dump_points_to_solution (file, &pi->pt);
524   else
525     fprintf (file, ", points-to anything");
526
527   fprintf (file, "\n");
528 }
529
530
531 /* Dump points-to information for VAR into stderr.  */
532
533 DEBUG_FUNCTION void
534 debug_points_to_info_for (tree var)
535 {
536   dump_points_to_info_for (stderr, var);
537 }
538
539
540 /* Initializes the alias-oracle reference representation *R from REF.  */
541
542 void
543 ao_ref_init (ao_ref *r, tree ref)
544 {
545   r->ref = ref;
546   r->base = NULL_TREE;
547   r->offset = 0;
548   r->size = -1;
549   r->max_size = -1;
550   r->ref_alias_set = -1;
551   r->base_alias_set = -1;
552   r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
553 }
554
555 /* Returns the base object of the memory reference *REF.  */
556
557 tree
558 ao_ref_base (ao_ref *ref)
559 {
560   if (ref->base)
561     return ref->base;
562   ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
563                                        &ref->max_size);
564   return ref->base;
565 }
566
567 /* Returns the base object alias set of the memory reference *REF.  */
568
569 alias_set_type
570 ao_ref_base_alias_set (ao_ref *ref)
571 {
572   tree base_ref;
573   if (ref->base_alias_set != -1)
574     return ref->base_alias_set;
575   if (!ref->ref)
576     return 0;
577   base_ref = ref->ref;
578   while (handled_component_p (base_ref))
579     base_ref = TREE_OPERAND (base_ref, 0);
580   ref->base_alias_set = get_alias_set (base_ref);
581   return ref->base_alias_set;
582 }
583
584 /* Returns the reference alias set of the memory reference *REF.  */
585
586 alias_set_type
587 ao_ref_alias_set (ao_ref *ref)
588 {
589   if (ref->ref_alias_set != -1)
590     return ref->ref_alias_set;
591   ref->ref_alias_set = get_alias_set (ref->ref);
592   return ref->ref_alias_set;
593 }
594
595 /* Init an alias-oracle reference representation from a gimple pointer
596    PTR and a gimple size SIZE in bytes.  If SIZE is NULL_TREE then the
597    size is assumed to be unknown.  The access is assumed to be only
598    to or after of the pointer target, not before it.  */
599
600 void
601 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
602 {
603   HOST_WIDE_INT t, size_hwi, extra_offset = 0;
604   ref->ref = NULL_TREE;
605   if (TREE_CODE (ptr) == SSA_NAME)
606     {
607       gimple *stmt = SSA_NAME_DEF_STMT (ptr);
608       if (gimple_assign_single_p (stmt)
609           && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
610         ptr = gimple_assign_rhs1 (stmt);
611       else if (is_gimple_assign (stmt)
612                && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
613                && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
614         {
615           ptr = gimple_assign_rhs1 (stmt);
616           extra_offset = BITS_PER_UNIT
617                          * int_cst_value (gimple_assign_rhs2 (stmt));
618         }
619     }
620
621   if (TREE_CODE (ptr) == ADDR_EXPR)
622     {
623       ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
624       if (ref->base)
625         ref->offset = BITS_PER_UNIT * t;
626       else
627         {
628           size = NULL_TREE;
629           ref->offset = 0;
630           ref->base = get_base_address (TREE_OPERAND (ptr, 0));
631         }
632     }
633   else
634     {
635       ref->base = build2 (MEM_REF, char_type_node,
636                           ptr, null_pointer_node);
637       ref->offset = 0;
638     }
639   ref->offset += extra_offset;
640   if (size
641       && tree_fits_shwi_p (size)
642       && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
643     ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
644   else
645     ref->max_size = ref->size = -1;
646   ref->ref_alias_set = 0;
647   ref->base_alias_set = 0;
648   ref->volatile_p = false;
649 }
650
651 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
652    purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
653    decide.  */
654
655 static inline int
656 same_type_for_tbaa (tree type1, tree type2)
657 {
658   type1 = TYPE_MAIN_VARIANT (type1);
659   type2 = TYPE_MAIN_VARIANT (type2);
660
661   /* If we would have to do structural comparison bail out.  */
662   if (TYPE_STRUCTURAL_EQUALITY_P (type1)
663       || TYPE_STRUCTURAL_EQUALITY_P (type2))
664     return -1;
665
666   /* Compare the canonical types.  */
667   if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
668     return 1;
669
670   /* ??? Array types are not properly unified in all cases as we have
671      spurious changes in the index types for example.  Removing this
672      causes all sorts of problems with the Fortran frontend.  */
673   if (TREE_CODE (type1) == ARRAY_TYPE
674       && TREE_CODE (type2) == ARRAY_TYPE)
675     return -1;
676
677   /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
678      object of one of its constrained subtypes, e.g. when a function with an
679      unconstrained parameter passed by reference is called on an object and
680      inlined.  But, even in the case of a fixed size, type and subtypes are
681      not equivalent enough as to share the same TYPE_CANONICAL, since this
682      would mean that conversions between them are useless, whereas they are
683      not (e.g. type and subtypes can have different modes).  So, in the end,
684      they are only guaranteed to have the same alias set.  */
685   if (get_alias_set (type1) == get_alias_set (type2))
686     return -1;
687
688   /* The types are known to be not equal.  */
689   return 0;
690 }
691
692 /* Determine if the two component references REF1 and REF2 which are
693    based on access types TYPE1 and TYPE2 and of which at least one is based
694    on an indirect reference may alias.  REF2 is the only one that can
695    be a decl in which case REF2_IS_DECL is true.
696    REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
697    are the respective alias sets.  */
698
699 static bool
700 aliasing_component_refs_p (tree ref1,
701                            alias_set_type ref1_alias_set,
702                            alias_set_type base1_alias_set,
703                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
704                            tree ref2,
705                            alias_set_type ref2_alias_set,
706                            alias_set_type base2_alias_set,
707                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
708                            bool ref2_is_decl)
709 {
710   /* If one reference is a component references through pointers try to find a
711      common base and apply offset based disambiguation.  This handles
712      for example
713        struct A { int i; int j; } *q;
714        struct B { struct A a; int k; } *p;
715      disambiguating q->i and p->a.j.  */
716   tree base1, base2;
717   tree type1, type2;
718   tree *refp;
719   int same_p;
720
721   /* Choose bases and base types to search for.  */
722   base1 = ref1;
723   while (handled_component_p (base1))
724     base1 = TREE_OPERAND (base1, 0);
725   type1 = TREE_TYPE (base1);
726   base2 = ref2;
727   while (handled_component_p (base2))
728     base2 = TREE_OPERAND (base2, 0);
729   type2 = TREE_TYPE (base2);
730
731   /* Now search for the type1 in the access path of ref2.  This
732      would be a common base for doing offset based disambiguation on.  */
733   refp = &ref2;
734   while (handled_component_p (*refp)
735          && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
736     refp = &TREE_OPERAND (*refp, 0);
737   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
738   /* If we couldn't compare types we have to bail out.  */
739   if (same_p == -1)
740     return true;
741   else if (same_p == 1)
742     {
743       HOST_WIDE_INT offadj, sztmp, msztmp;
744       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
745       offset2 -= offadj;
746       get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
747       offset1 -= offadj;
748       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
749     }
750   /* If we didn't find a common base, try the other way around.  */
751   refp = &ref1;
752   while (handled_component_p (*refp)
753          && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
754     refp = &TREE_OPERAND (*refp, 0);
755   same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
756   /* If we couldn't compare types we have to bail out.  */
757   if (same_p == -1)
758     return true;
759   else if (same_p == 1)
760     {
761       HOST_WIDE_INT offadj, sztmp, msztmp;
762       get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
763       offset1 -= offadj;
764       get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
765       offset2 -= offadj;
766       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
767     }
768
769   /* If we have two type access paths B1.path1 and B2.path2 they may
770      only alias if either B1 is in B2.path2 or B2 is in B1.path1.
771      But we can still have a path that goes B1.path1...B2.path2 with
772      a part that we do not see.  So we can only disambiguate now
773      if there is no B2 in the tail of path1 and no B1 on the
774      tail of path2.  */
775   if (base1_alias_set == ref2_alias_set
776       || alias_set_subset_of (base1_alias_set, ref2_alias_set))
777     return true;
778   /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
779   if (!ref2_is_decl)
780     return (base2_alias_set == ref1_alias_set
781             || alias_set_subset_of (base2_alias_set, ref1_alias_set));
782   return false;
783 }
784
785 /* Return true if we can determine that component references REF1 and REF2,
786    that are within a common DECL, cannot overlap.  */
787
788 static bool
789 nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
790 {
791   auto_vec<tree, 16> component_refs1;
792   auto_vec<tree, 16> component_refs2;
793
794   /* Create the stack of handled components for REF1.  */
795   while (handled_component_p (ref1))
796     {
797       component_refs1.safe_push (ref1);
798       ref1 = TREE_OPERAND (ref1, 0);
799     }
800   if (TREE_CODE (ref1) == MEM_REF)
801     {
802       if (!integer_zerop (TREE_OPERAND (ref1, 1)))
803         goto may_overlap;
804       ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
805     }
806
807   /* Create the stack of handled components for REF2.  */
808   while (handled_component_p (ref2))
809     {
810       component_refs2.safe_push (ref2);
811       ref2 = TREE_OPERAND (ref2, 0);
812     }
813   if (TREE_CODE (ref2) == MEM_REF)
814     {
815       if (!integer_zerop (TREE_OPERAND (ref2, 1)))
816         goto may_overlap;
817       ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
818     }
819
820   /* We must have the same base DECL.  */
821   gcc_assert (ref1 == ref2);
822
823   /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
824      rank.  This is sufficient because we start from the same DECL and you
825      cannot reference several fields at a time with COMPONENT_REFs (unlike
826      with ARRAY_RANGE_REFs for arrays) so you always need the same number
827      of them to access a sub-component, unless you're in a union, in which
828      case the return value will precisely be false.  */
829   while (true)
830     {
831       do
832         {
833           if (component_refs1.is_empty ())
834             goto may_overlap;
835           ref1 = component_refs1.pop ();
836         }
837       while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
838
839       do
840         {
841           if (component_refs2.is_empty ())
842              goto may_overlap;
843           ref2 = component_refs2.pop ();
844         }
845       while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
846
847       /* Beware of BIT_FIELD_REF.  */
848       if (TREE_CODE (ref1) != COMPONENT_REF
849           || TREE_CODE (ref2) != COMPONENT_REF)
850         goto may_overlap;
851
852       tree field1 = TREE_OPERAND (ref1, 1);
853       tree field2 = TREE_OPERAND (ref2, 1);
854
855       /* ??? We cannot simply use the type of operand #0 of the refs here
856          as the Fortran compiler smuggles type punning into COMPONENT_REFs
857          for common blocks instead of using unions like everyone else.  */
858       tree type1 = DECL_CONTEXT (field1);
859       tree type2 = DECL_CONTEXT (field2);
860
861       /* We cannot disambiguate fields in a union or qualified union.  */
862       if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
863          goto may_overlap;
864
865       /* Different fields of the same record type cannot overlap.
866          ??? Bitfields can overlap at RTL level so punt on them.  */
867       if (field1 != field2)
868         {
869           component_refs1.release ();
870           component_refs2.release ();
871           return !(DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2));
872         }
873     }
874
875 may_overlap:
876   component_refs1.release ();
877   component_refs2.release ();
878   return false;
879 }
880
881 /* qsort compare function to sort FIELD_DECLs after their
882    DECL_FIELD_CONTEXT TYPE_UID.  */
883
884 static inline int
885 ncr_compar (const void *field1_, const void *field2_)
886 {
887   const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
888   const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
889   unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
890   unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
891   if (uid1 < uid2)
892     return -1;
893   else if (uid1 > uid2)
894     return 1;
895   return 0;
896 }
897
898 /* Return true if we can determine that the fields referenced cannot
899    overlap for any pair of objects.  */
900
901 static bool
902 nonoverlapping_component_refs_p (const_tree x, const_tree y)
903 {
904   if (!flag_strict_aliasing
905       || !x || !y
906       || TREE_CODE (x) != COMPONENT_REF
907       || TREE_CODE (y) != COMPONENT_REF)
908     return false;
909
910   auto_vec<const_tree, 16> fieldsx;
911   while (TREE_CODE (x) == COMPONENT_REF)
912     {
913       tree field = TREE_OPERAND (x, 1);
914       tree type = DECL_FIELD_CONTEXT (field);
915       if (TREE_CODE (type) == RECORD_TYPE)
916         fieldsx.safe_push (field);
917       x = TREE_OPERAND (x, 0);
918     }
919   if (fieldsx.length () == 0)
920     return false;
921   auto_vec<const_tree, 16> fieldsy;
922   while (TREE_CODE (y) == COMPONENT_REF)
923     {
924       tree field = TREE_OPERAND (y, 1);
925       tree type = DECL_FIELD_CONTEXT (field);
926       if (TREE_CODE (type) == RECORD_TYPE)
927         fieldsy.safe_push (TREE_OPERAND (y, 1));
928       y = TREE_OPERAND (y, 0);
929     }
930   if (fieldsy.length () == 0)
931     return false;
932
933   /* Most common case first.  */
934   if (fieldsx.length () == 1
935       && fieldsy.length () == 1)
936     return ((DECL_FIELD_CONTEXT (fieldsx[0])
937              == DECL_FIELD_CONTEXT (fieldsy[0]))
938             && fieldsx[0] != fieldsy[0]
939             && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));
940
941   if (fieldsx.length () == 2)
942     {
943       if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
944         std::swap (fieldsx[0], fieldsx[1]);
945     }
946   else
947     fieldsx.qsort (ncr_compar);
948
949   if (fieldsy.length () == 2)
950     {
951       if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
952         std::swap (fieldsy[0], fieldsy[1]);
953     }
954   else
955     fieldsy.qsort (ncr_compar);
956
957   unsigned i = 0, j = 0;
958   do
959     {
960       const_tree fieldx = fieldsx[i];
961       const_tree fieldy = fieldsy[j];
962       tree typex = DECL_FIELD_CONTEXT (fieldx);
963       tree typey = DECL_FIELD_CONTEXT (fieldy);
964       if (typex == typey)
965         {
966           /* We're left with accessing different fields of a structure,
967              no possible overlap, unless they are both bitfields.  */
968           if (fieldx != fieldy)
969             return !(DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy));
970         }
971       if (TYPE_UID (typex) < TYPE_UID (typey))
972         {
973           i++;
974           if (i == fieldsx.length ())
975             break;
976         }
977       else
978         {
979           j++;
980           if (j == fieldsy.length ())
981             break;
982         }
983     }
984   while (1);
985
986   return false;
987 }
988
989
990 /* Return true if two memory references based on the variables BASE1
991    and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
992    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  REF1 and REF2
993    if non-NULL are the complete memory reference trees.  */
994
995 static bool
996 decl_refs_may_alias_p (tree ref1, tree base1,
997                        HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
998                        tree ref2, tree base2,
999                        HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1000 {
1001   gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1002
1003   /* If both references are based on different variables, they cannot alias.  */
1004   if (base1 != base2)
1005     return false;
1006
1007   /* If both references are based on the same variable, they cannot alias if
1008      the accesses do not overlap.  */
1009   if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
1010     return false;
1011
1012   /* For components with variable position, the above test isn't sufficient,
1013      so we disambiguate component references manually.  */
1014   if (ref1 && ref2
1015       && handled_component_p (ref1) && handled_component_p (ref2)
1016       && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
1017     return false;
1018
1019   return true;     
1020 }
1021
1022 /* Return true if an indirect reference based on *PTR1 constrained
1023    to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1024    constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
1025    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1026    in which case they are computed on-demand.  REF1 and REF2
1027    if non-NULL are the complete memory reference trees.  */
1028
1029 static bool
1030 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1031                                HOST_WIDE_INT offset1,
1032                                HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1033                                alias_set_type ref1_alias_set,
1034                                alias_set_type base1_alias_set,
1035                                tree ref2 ATTRIBUTE_UNUSED, tree base2,
1036                                HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1037                                alias_set_type ref2_alias_set,
1038                                alias_set_type base2_alias_set, bool tbaa_p)
1039 {
1040   tree ptr1;
1041   tree ptrtype1, dbase2;
1042   HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1043   HOST_WIDE_INT doffset1, doffset2;
1044
1045   gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1046                         || TREE_CODE (base1) == TARGET_MEM_REF)
1047                        && DECL_P (base2));
1048
1049   ptr1 = TREE_OPERAND (base1, 0);
1050
1051   /* The offset embedded in MEM_REFs can be negative.  Bias them
1052      so that the resulting offset adjustment is positive.  */
1053   offset_int moff = mem_ref_offset (base1);
1054   moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1055   if (wi::neg_p (moff))
1056     offset2p += (-moff).to_short_addr ();
1057   else
1058     offset1p += moff.to_short_addr ();
1059
1060   /* If only one reference is based on a variable, they cannot alias if
1061      the pointer access is beyond the extent of the variable access.
1062      (the pointer base cannot validly point to an offset less than zero
1063      of the variable).
1064      ???  IVOPTs creates bases that do not honor this restriction,
1065      so do not apply this optimization for TARGET_MEM_REFs.  */
1066   if (TREE_CODE (base1) != TARGET_MEM_REF
1067       && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1068     return false;
1069   /* They also cannot alias if the pointer may not point to the decl.  */
1070   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1071     return false;
1072
1073   /* Disambiguations that rely on strict aliasing rules follow.  */
1074   if (!flag_strict_aliasing || !tbaa_p)
1075     return true;
1076
1077   ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1078
1079   /* If the alias set for a pointer access is zero all bets are off.  */
1080   if (base1_alias_set == -1)
1081     base1_alias_set = get_deref_alias_set (ptrtype1);
1082   if (base1_alias_set == 0)
1083     return true;
1084   if (base2_alias_set == -1)
1085     base2_alias_set = get_alias_set (base2);
1086
1087   /* When we are trying to disambiguate an access with a pointer dereference
1088      as base versus one with a decl as base we can use both the size
1089      of the decl and its dynamic type for extra disambiguation.
1090      ???  We do not know anything about the dynamic type of the decl
1091      other than that its alias-set contains base2_alias_set as a subset
1092      which does not help us here.  */
1093   /* As we know nothing useful about the dynamic type of the decl just
1094      use the usual conflict check rather than a subset test.
1095      ???  We could introduce -fvery-strict-aliasing when the language
1096      does not allow decls to have a dynamic type that differs from their
1097      static type.  Then we can check 
1098      !alias_set_subset_of (base1_alias_set, base2_alias_set) instead.  */
1099   if (base1_alias_set != base2_alias_set
1100       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1101     return false;
1102   /* If the size of the access relevant for TBAA through the pointer
1103      is bigger than the size of the decl we can't possibly access the
1104      decl via that pointer.  */
1105   if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
1106       && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
1107       && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
1108       /* ???  This in turn may run afoul when a decl of type T which is
1109          a member of union type U is accessed through a pointer to
1110          type U and sizeof T is smaller than sizeof U.  */
1111       && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1112       && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1113       && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1114     return false;
1115
1116   if (!ref2)
1117     return true;
1118
1119   /* If the decl is accessed via a MEM_REF, reconstruct the base
1120      we can use for TBAA and an appropriately adjusted offset.  */
1121   dbase2 = ref2;
1122   while (handled_component_p (dbase2))
1123     dbase2 = TREE_OPERAND (dbase2, 0);
1124   doffset1 = offset1;
1125   doffset2 = offset2;
1126   if (TREE_CODE (dbase2) == MEM_REF
1127       || TREE_CODE (dbase2) == TARGET_MEM_REF)
1128     {
1129       offset_int moff = mem_ref_offset (dbase2);
1130       moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1131       if (wi::neg_p (moff))
1132         doffset1 -= (-moff).to_short_addr ();
1133       else
1134         doffset2 -= moff.to_short_addr ();
1135     }
1136
1137   /* If either reference is view-converted, give up now.  */
1138   if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1139       || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1140     return true;
1141
1142   /* If both references are through the same type, they do not alias
1143      if the accesses do not overlap.  This does extra disambiguation
1144      for mixed/pointer accesses but requires strict aliasing.
1145      For MEM_REFs we require that the component-ref offset we computed
1146      is relative to the start of the type which we ensure by
1147      comparing rvalue and access type and disregarding the constant
1148      pointer offset.  */
1149   if ((TREE_CODE (base1) != TARGET_MEM_REF
1150        || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1151       && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1152     return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
1153
1154   if (ref1 && ref2
1155       && nonoverlapping_component_refs_p (ref1, ref2))
1156     return false;
1157
1158   /* Do access-path based disambiguation.  */
1159   if (ref1 && ref2
1160       && (handled_component_p (ref1) || handled_component_p (ref2)))
1161     return aliasing_component_refs_p (ref1,
1162                                       ref1_alias_set, base1_alias_set,
1163                                       offset1, max_size1,
1164                                       ref2,
1165                                       ref2_alias_set, base2_alias_set,
1166                                       offset2, max_size2, true);
1167
1168   return true;
1169 }
1170
1171 /* Return true if two indirect references based on *PTR1
1172    and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1173    [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
1174    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1175    in which case they are computed on-demand.  REF1 and REF2
1176    if non-NULL are the complete memory reference trees. */
1177
1178 static bool
1179 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1180                            HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1181                            alias_set_type ref1_alias_set,
1182                            alias_set_type base1_alias_set,
1183                            tree ref2 ATTRIBUTE_UNUSED, tree base2,
1184                            HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1185                            alias_set_type ref2_alias_set,
1186                            alias_set_type base2_alias_set, bool tbaa_p)
1187 {
1188   tree ptr1;
1189   tree ptr2;
1190   tree ptrtype1, ptrtype2;
1191
1192   gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1193                         || TREE_CODE (base1) == TARGET_MEM_REF)
1194                        && (TREE_CODE (base2) == MEM_REF
1195                            || TREE_CODE (base2) == TARGET_MEM_REF));
1196
1197   ptr1 = TREE_OPERAND (base1, 0);
1198   ptr2 = TREE_OPERAND (base2, 0);
1199
1200   /* If both bases are based on pointers they cannot alias if they may not
1201      point to the same memory object or if they point to the same object
1202      and the accesses do not overlap.  */
1203   if ((!cfun || gimple_in_ssa_p (cfun))
1204       && operand_equal_p (ptr1, ptr2, 0)
1205       && (((TREE_CODE (base1) != TARGET_MEM_REF
1206             || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1207            && (TREE_CODE (base2) != TARGET_MEM_REF
1208                || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1209           || (TREE_CODE (base1) == TARGET_MEM_REF
1210               && TREE_CODE (base2) == TARGET_MEM_REF
1211               && (TMR_STEP (base1) == TMR_STEP (base2)
1212                   || (TMR_STEP (base1) && TMR_STEP (base2)
1213                       && operand_equal_p (TMR_STEP (base1),
1214                                           TMR_STEP (base2), 0)))
1215               && (TMR_INDEX (base1) == TMR_INDEX (base2)
1216                   || (TMR_INDEX (base1) && TMR_INDEX (base2)
1217                       && operand_equal_p (TMR_INDEX (base1),
1218                                           TMR_INDEX (base2), 0)))
1219               && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1220                   || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1221                       && operand_equal_p (TMR_INDEX2 (base1),
1222                                           TMR_INDEX2 (base2), 0))))))
1223     {
1224       offset_int moff;
1225       /* The offset embedded in MEM_REFs can be negative.  Bias them
1226          so that the resulting offset adjustment is positive.  */
1227       moff = mem_ref_offset (base1);
1228       moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1229       if (wi::neg_p (moff))
1230         offset2 += (-moff).to_short_addr ();
1231       else
1232         offset1 += moff.to_shwi ();
1233       moff = mem_ref_offset (base2);
1234       moff = wi::lshift (moff, LOG2_BITS_PER_UNIT);
1235       if (wi::neg_p (moff))
1236         offset1 += (-moff).to_short_addr ();
1237       else
1238         offset2 += moff.to_short_addr ();
1239       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1240     }
1241   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1242     return false;
1243
1244   /* Disambiguations that rely on strict aliasing rules follow.  */
1245   if (!flag_strict_aliasing || !tbaa_p)
1246     return true;
1247
1248   ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1249   ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1250
1251   /* If the alias set for a pointer access is zero all bets are off.  */
1252   if (base1_alias_set == -1)
1253     base1_alias_set = get_deref_alias_set (ptrtype1);
1254   if (base1_alias_set == 0)
1255     return true;
1256   if (base2_alias_set == -1)
1257     base2_alias_set = get_deref_alias_set (ptrtype2);
1258   if (base2_alias_set == 0)
1259     return true;
1260
1261   /* If both references are through the same type, they do not alias
1262      if the accesses do not overlap.  This does extra disambiguation
1263      for mixed/pointer accesses but requires strict aliasing.  */
1264   if ((TREE_CODE (base1) != TARGET_MEM_REF
1265        || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1266       && (TREE_CODE (base2) != TARGET_MEM_REF
1267           || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1268       && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
1269       && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1270       && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1271                              TREE_TYPE (ptrtype2)) == 1)
1272     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
1273
1274   /* Do type-based disambiguation.  */
1275   if (base1_alias_set != base2_alias_set
1276       && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1277     return false;
1278
1279   /* If either reference is view-converted, give up now.  */
1280   if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1281       || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1282     return true;
1283
1284   if (ref1 && ref2
1285       && nonoverlapping_component_refs_p (ref1, ref2))
1286     return false;
1287
1288   /* Do access-path based disambiguation.  */
1289   if (ref1 && ref2
1290       && (handled_component_p (ref1) || handled_component_p (ref2)))
1291     return aliasing_component_refs_p (ref1,
1292                                       ref1_alias_set, base1_alias_set,
1293                                       offset1, max_size1,
1294                                       ref2,
1295                                       ref2_alias_set, base2_alias_set,
1296                                       offset2, max_size2, false);
1297
1298   return true;
1299 }
1300
1301 /* Return true, if the two memory references REF1 and REF2 may alias.  */
1302
1303 bool
1304 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1305 {
1306   tree base1, base2;
1307   HOST_WIDE_INT offset1 = 0, offset2 = 0;
1308   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
1309   bool var1_p, var2_p, ind1_p, ind2_p;
1310
1311   gcc_checking_assert ((!ref1->ref
1312                         || TREE_CODE (ref1->ref) == SSA_NAME
1313                         || DECL_P (ref1->ref)
1314                         || TREE_CODE (ref1->ref) == STRING_CST
1315                         || handled_component_p (ref1->ref)
1316                         || TREE_CODE (ref1->ref) == MEM_REF
1317                         || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1318                        && (!ref2->ref
1319                            || TREE_CODE (ref2->ref) == SSA_NAME
1320                            || DECL_P (ref2->ref)
1321                            || TREE_CODE (ref2->ref) == STRING_CST
1322                            || handled_component_p (ref2->ref)
1323                            || TREE_CODE (ref2->ref) == MEM_REF
1324                            || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1325
1326   /* Decompose the references into their base objects and the access.  */
1327   base1 = ao_ref_base (ref1);
1328   offset1 = ref1->offset;
1329   max_size1 = ref1->max_size;
1330   base2 = ao_ref_base (ref2);
1331   offset2 = ref2->offset;
1332   max_size2 = ref2->max_size;
1333
1334   /* We can end up with registers or constants as bases for example from
1335      *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1336      which is seen as a struct copy.  */
1337   if (TREE_CODE (base1) == SSA_NAME
1338       || TREE_CODE (base1) == CONST_DECL
1339       || TREE_CODE (base1) == CONSTRUCTOR
1340       || TREE_CODE (base1) == ADDR_EXPR
1341       || CONSTANT_CLASS_P (base1)
1342       || TREE_CODE (base2) == SSA_NAME
1343       || TREE_CODE (base2) == CONST_DECL
1344       || TREE_CODE (base2) == CONSTRUCTOR
1345       || TREE_CODE (base2) == ADDR_EXPR
1346       || CONSTANT_CLASS_P (base2))
1347     return false;
1348
1349   /* We can end up referring to code via function and label decls.
1350      As we likely do not properly track code aliases conservatively
1351      bail out.  */
1352   if (TREE_CODE (base1) == FUNCTION_DECL
1353       || TREE_CODE (base1) == LABEL_DECL
1354       || TREE_CODE (base2) == FUNCTION_DECL
1355       || TREE_CODE (base2) == LABEL_DECL)
1356     return true;
1357
1358   /* Two volatile accesses always conflict.  */
1359   if (ref1->volatile_p
1360       && ref2->volatile_p)
1361     return true;
1362
1363   /* Defer to simple offset based disambiguation if we have
1364      references based on two decls.  Do this before defering to
1365      TBAA to handle must-alias cases in conformance with the
1366      GCC extension of allowing type-punning through unions.  */
1367   var1_p = DECL_P (base1);
1368   var2_p = DECL_P (base2);
1369   if (var1_p && var2_p)
1370     return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1371                                   ref2->ref, base2, offset2, max_size2);
1372
1373   /* Handle restrict based accesses.
1374      ???  ao_ref_base strips inner MEM_REF [&decl], recover from that
1375      here.  */
1376   tree rbase1 = base1;
1377   tree rbase2 = base2;
1378   if (var1_p)
1379     {
1380       rbase1 = ref1->ref;
1381       if (rbase1)
1382         while (handled_component_p (rbase1))
1383           rbase1 = TREE_OPERAND (rbase1, 0);
1384     }
1385   if (var2_p)
1386     {
1387       rbase2 = ref2->ref;
1388       if (rbase2)
1389         while (handled_component_p (rbase2))
1390           rbase2 = TREE_OPERAND (rbase2, 0);
1391     }
1392   if (rbase1 && rbase2
1393       && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1394       && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1395       /* If the accesses are in the same restrict clique... */
1396       && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1397       /* But based on different pointers they do not alias.  */
1398       && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1399     return false;
1400
1401   ind1_p = (TREE_CODE (base1) == MEM_REF
1402             || TREE_CODE (base1) == TARGET_MEM_REF);
1403   ind2_p = (TREE_CODE (base2) == MEM_REF
1404             || TREE_CODE (base2) == TARGET_MEM_REF);
1405
1406   /* Canonicalize the pointer-vs-decl case.  */
1407   if (ind1_p && var2_p)
1408     {
1409       std::swap (offset1, offset2);
1410       std::swap (max_size1, max_size2);
1411       std::swap (base1, base2);
1412       std::swap (ref1, ref2);
1413       var1_p = true;
1414       ind1_p = false;
1415       var2_p = false;
1416       ind2_p = true;
1417     }
1418
1419   /* First defer to TBAA if possible.  */
1420   if (tbaa_p
1421       && flag_strict_aliasing
1422       && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1423                                  ao_ref_alias_set (ref2)))
1424     return false;
1425
1426   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
1427   if (var1_p && ind2_p)
1428     return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1429                                           offset2, max_size2,
1430                                           ao_ref_alias_set (ref2), -1,
1431                                           ref1->ref, base1,
1432                                           offset1, max_size1,
1433                                           ao_ref_alias_set (ref1),
1434                                           ao_ref_base_alias_set (ref1),
1435                                           tbaa_p);
1436   else if (ind1_p && ind2_p)
1437     return indirect_refs_may_alias_p (ref1->ref, base1,
1438                                       offset1, max_size1,
1439                                       ao_ref_alias_set (ref1), -1,
1440                                       ref2->ref, base2,
1441                                       offset2, max_size2,
1442                                       ao_ref_alias_set (ref2), -1,
1443                                       tbaa_p);
1444
1445   gcc_unreachable ();
1446 }
1447
1448 static bool
1449 refs_may_alias_p (tree ref1, ao_ref *ref2)
1450 {
1451   ao_ref r1;
1452   ao_ref_init (&r1, ref1);
1453   return refs_may_alias_p_1 (&r1, ref2, true);
1454 }
1455
1456 bool
1457 refs_may_alias_p (tree ref1, tree ref2)
1458 {
1459   ao_ref r1, r2;
1460   bool res;
1461   ao_ref_init (&r1, ref1);
1462   ao_ref_init (&r2, ref2);
1463   res = refs_may_alias_p_1 (&r1, &r2, true);
1464   if (res)
1465     ++alias_stats.refs_may_alias_p_may_alias;
1466   else
1467     ++alias_stats.refs_may_alias_p_no_alias;
1468   return res;
1469 }
1470
1471 /* Returns true if there is a anti-dependence for the STORE that
1472    executes after the LOAD.  */
1473
1474 bool
1475 refs_anti_dependent_p (tree load, tree store)
1476 {
1477   ao_ref r1, r2;
1478   ao_ref_init (&r1, load);
1479   ao_ref_init (&r2, store);
1480   return refs_may_alias_p_1 (&r1, &r2, false);
1481 }
1482
1483 /* Returns true if there is a output dependence for the stores
1484    STORE1 and STORE2.  */
1485
1486 bool
1487 refs_output_dependent_p (tree store1, tree store2)
1488 {
1489   ao_ref r1, r2;
1490   ao_ref_init (&r1, store1);
1491   ao_ref_init (&r2, store2);
1492   return refs_may_alias_p_1 (&r1, &r2, false);
1493 }
1494
1495 /* If the call CALL may use the memory reference REF return true,
1496    otherwise return false.  */
1497
1498 static bool
1499 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1500 {
1501   tree base, callee;
1502   unsigned i;
1503   int flags = gimple_call_flags (call);
1504
1505   /* Const functions without a static chain do not implicitly use memory.  */
1506   if (!gimple_call_chain (call)
1507       && (flags & (ECF_CONST|ECF_NOVOPS)))
1508     goto process_args;
1509
1510   base = ao_ref_base (ref);
1511   if (!base)
1512     return true;
1513
1514   /* A call that is not without side-effects might involve volatile
1515      accesses and thus conflicts with all other volatile accesses.  */
1516   if (ref->volatile_p)
1517     return true;
1518
1519   /* If the reference is based on a decl that is not aliased the call
1520      cannot possibly use it.  */
1521   if (DECL_P (base)
1522       && !may_be_aliased (base)
1523       /* But local statics can be used through recursion.  */
1524       && !is_global_var (base))
1525     goto process_args;
1526
1527   callee = gimple_call_fndecl (call);
1528
1529   /* Handle those builtin functions explicitly that do not act as
1530      escape points.  See tree-ssa-structalias.c:find_func_aliases
1531      for the list of builtins we might need to handle here.  */
1532   if (callee != NULL_TREE
1533       && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1534     switch (DECL_FUNCTION_CODE (callee))
1535       {
1536         /* All the following functions read memory pointed to by
1537            their second argument.  strcat/strncat additionally
1538            reads memory pointed to by the first argument.  */
1539         case BUILT_IN_STRCAT:
1540         case BUILT_IN_STRNCAT:
1541           {
1542             ao_ref dref;
1543             ao_ref_init_from_ptr_and_size (&dref,
1544                                            gimple_call_arg (call, 0),
1545                                            NULL_TREE);
1546             if (refs_may_alias_p_1 (&dref, ref, false))
1547               return true;
1548           }
1549           /* FALLTHRU */
1550         case BUILT_IN_STRCPY:
1551         case BUILT_IN_STRNCPY:
1552         case BUILT_IN_MEMCPY:
1553         case BUILT_IN_MEMMOVE:
1554         case BUILT_IN_MEMPCPY:
1555         case BUILT_IN_STPCPY:
1556         case BUILT_IN_STPNCPY:
1557         case BUILT_IN_TM_MEMCPY:
1558         case BUILT_IN_TM_MEMMOVE:
1559           {
1560             ao_ref dref;
1561             tree size = NULL_TREE;
1562             if (gimple_call_num_args (call) == 3)
1563               size = gimple_call_arg (call, 2);
1564             ao_ref_init_from_ptr_and_size (&dref,
1565                                            gimple_call_arg (call, 1),
1566                                            size);
1567             return refs_may_alias_p_1 (&dref, ref, false);
1568           }
1569         case BUILT_IN_STRCAT_CHK:
1570         case BUILT_IN_STRNCAT_CHK:
1571           {
1572             ao_ref dref;
1573             ao_ref_init_from_ptr_and_size (&dref,
1574                                            gimple_call_arg (call, 0),
1575                                            NULL_TREE);
1576             if (refs_may_alias_p_1 (&dref, ref, false))
1577               return true;
1578           }
1579           /* FALLTHRU */
1580         case BUILT_IN_STRCPY_CHK:
1581         case BUILT_IN_STRNCPY_CHK:
1582         case BUILT_IN_MEMCPY_CHK:
1583         case BUILT_IN_MEMMOVE_CHK:
1584         case BUILT_IN_MEMPCPY_CHK:
1585         case BUILT_IN_STPCPY_CHK:
1586         case BUILT_IN_STPNCPY_CHK:
1587           {
1588             ao_ref dref;
1589             tree size = NULL_TREE;
1590             if (gimple_call_num_args (call) == 4)
1591               size = gimple_call_arg (call, 2);
1592             ao_ref_init_from_ptr_and_size (&dref,
1593                                            gimple_call_arg (call, 1),
1594                                            size);
1595             return refs_may_alias_p_1 (&dref, ref, false);
1596           }
1597         case BUILT_IN_BCOPY:
1598           {
1599             ao_ref dref;
1600             tree size = gimple_call_arg (call, 2);
1601             ao_ref_init_from_ptr_and_size (&dref,
1602                                            gimple_call_arg (call, 0),
1603                                            size);
1604             return refs_may_alias_p_1 (&dref, ref, false);
1605           }
1606
1607         /* The following functions read memory pointed to by their
1608            first argument.  */
1609         CASE_BUILT_IN_TM_LOAD (1):
1610         CASE_BUILT_IN_TM_LOAD (2):
1611         CASE_BUILT_IN_TM_LOAD (4):
1612         CASE_BUILT_IN_TM_LOAD (8):
1613         CASE_BUILT_IN_TM_LOAD (FLOAT):
1614         CASE_BUILT_IN_TM_LOAD (DOUBLE):
1615         CASE_BUILT_IN_TM_LOAD (LDOUBLE):
1616         CASE_BUILT_IN_TM_LOAD (M64):
1617         CASE_BUILT_IN_TM_LOAD (M128):
1618         CASE_BUILT_IN_TM_LOAD (M256):
1619         case BUILT_IN_TM_LOG:
1620         case BUILT_IN_TM_LOG_1:
1621         case BUILT_IN_TM_LOG_2:
1622         case BUILT_IN_TM_LOG_4:
1623         case BUILT_IN_TM_LOG_8:
1624         case BUILT_IN_TM_LOG_FLOAT:
1625         case BUILT_IN_TM_LOG_DOUBLE:
1626         case BUILT_IN_TM_LOG_LDOUBLE:
1627         case BUILT_IN_TM_LOG_M64:
1628         case BUILT_IN_TM_LOG_M128:
1629         case BUILT_IN_TM_LOG_M256:
1630           return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
1631
1632         /* These read memory pointed to by the first argument.  */
1633         case BUILT_IN_STRDUP:
1634         case BUILT_IN_STRNDUP:
1635         case BUILT_IN_REALLOC:
1636           {
1637             ao_ref dref;
1638             tree size = NULL_TREE;
1639             if (gimple_call_num_args (call) == 2)
1640               size = gimple_call_arg (call, 1);
1641             ao_ref_init_from_ptr_and_size (&dref,
1642                                            gimple_call_arg (call, 0),
1643                                            size);
1644             return refs_may_alias_p_1 (&dref, ref, false);
1645           }
1646         /* These read memory pointed to by the first argument.  */
1647         case BUILT_IN_INDEX:
1648         case BUILT_IN_STRCHR:
1649         case BUILT_IN_STRRCHR:
1650           {
1651             ao_ref dref;
1652             ao_ref_init_from_ptr_and_size (&dref,
1653                                            gimple_call_arg (call, 0),
1654                                            NULL_TREE);
1655             return refs_may_alias_p_1 (&dref, ref, false);
1656           }
1657         /* These read memory pointed to by the first argument with size
1658            in the third argument.  */
1659         case BUILT_IN_MEMCHR:
1660           {
1661             ao_ref dref;
1662             ao_ref_init_from_ptr_and_size (&dref,
1663                                            gimple_call_arg (call, 0),
1664                                            gimple_call_arg (call, 2));
1665             return refs_may_alias_p_1 (&dref, ref, false);
1666           }
1667         /* These read memory pointed to by the first and second arguments.  */
1668         case BUILT_IN_STRSTR:
1669         case BUILT_IN_STRPBRK:
1670           {
1671             ao_ref dref;
1672             ao_ref_init_from_ptr_and_size (&dref,
1673                                            gimple_call_arg (call, 0),
1674                                            NULL_TREE);
1675             if (refs_may_alias_p_1 (&dref, ref, false))
1676               return true;
1677             ao_ref_init_from_ptr_and_size (&dref,
1678                                            gimple_call_arg (call, 1),
1679                                            NULL_TREE);
1680             return refs_may_alias_p_1 (&dref, ref, false);
1681           }
1682
1683         /* The following builtins do not read from memory.  */
1684         case BUILT_IN_FREE:
1685         case BUILT_IN_MALLOC:
1686         case BUILT_IN_POSIX_MEMALIGN:
1687         case BUILT_IN_ALIGNED_ALLOC:
1688         case BUILT_IN_CALLOC:
1689         case BUILT_IN_ALLOCA:
1690         case BUILT_IN_ALLOCA_WITH_ALIGN:
1691         case BUILT_IN_STACK_SAVE:
1692         case BUILT_IN_STACK_RESTORE:
1693         case BUILT_IN_MEMSET:
1694         case BUILT_IN_TM_MEMSET:
1695         case BUILT_IN_MEMSET_CHK:
1696         case BUILT_IN_FREXP:
1697         case BUILT_IN_FREXPF:
1698         case BUILT_IN_FREXPL:
1699         case BUILT_IN_GAMMA_R:
1700         case BUILT_IN_GAMMAF_R:
1701         case BUILT_IN_GAMMAL_R:
1702         case BUILT_IN_LGAMMA_R:
1703         case BUILT_IN_LGAMMAF_R:
1704         case BUILT_IN_LGAMMAL_R:
1705         case BUILT_IN_MODF:
1706         case BUILT_IN_MODFF:
1707         case BUILT_IN_MODFL:
1708         case BUILT_IN_REMQUO:
1709         case BUILT_IN_REMQUOF:
1710         case BUILT_IN_REMQUOL:
1711         case BUILT_IN_SINCOS:
1712         case BUILT_IN_SINCOSF:
1713         case BUILT_IN_SINCOSL:
1714         case BUILT_IN_ASSUME_ALIGNED:
1715         case BUILT_IN_VA_END:
1716           return false;
1717         /* __sync_* builtins and some OpenMP builtins act as threading
1718            barriers.  */
1719 #undef DEF_SYNC_BUILTIN
1720 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1721 #include "sync-builtins.def"
1722 #undef DEF_SYNC_BUILTIN
1723         case BUILT_IN_GOMP_ATOMIC_START:
1724         case BUILT_IN_GOMP_ATOMIC_END:
1725         case BUILT_IN_GOMP_BARRIER:
1726         case BUILT_IN_GOMP_BARRIER_CANCEL:
1727         case BUILT_IN_GOMP_TASKWAIT:
1728         case BUILT_IN_GOMP_TASKGROUP_END:
1729         case BUILT_IN_GOMP_CRITICAL_START:
1730         case BUILT_IN_GOMP_CRITICAL_END:
1731         case BUILT_IN_GOMP_CRITICAL_NAME_START:
1732         case BUILT_IN_GOMP_CRITICAL_NAME_END:
1733         case BUILT_IN_GOMP_LOOP_END:
1734         case BUILT_IN_GOMP_LOOP_END_CANCEL:
1735         case BUILT_IN_GOMP_ORDERED_START:
1736         case BUILT_IN_GOMP_ORDERED_END:
1737         case BUILT_IN_GOMP_SECTIONS_END:
1738         case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1739         case BUILT_IN_GOMP_SINGLE_COPY_START:
1740         case BUILT_IN_GOMP_SINGLE_COPY_END:
1741           return true;
1742
1743         default:
1744           /* Fallthru to general call handling.  */;
1745       }
1746
1747   /* Check if base is a global static variable that is not read
1748      by the function.  */
1749   if (callee != NULL_TREE
1750       && TREE_CODE (base) == VAR_DECL
1751       && TREE_STATIC (base))
1752     {
1753       struct cgraph_node *node = cgraph_node::get (callee);
1754       bitmap not_read;
1755
1756       /* FIXME: Callee can be an OMP builtin that does not have a call graph
1757          node yet.  We should enforce that there are nodes for all decls in the
1758          IL and remove this check instead.  */
1759       if (node
1760           && (not_read = ipa_reference_get_not_read_global (node))
1761           && bitmap_bit_p (not_read, DECL_UID (base)))
1762         goto process_args;
1763     }
1764
1765   /* Check if the base variable is call-used.  */
1766   if (DECL_P (base))
1767     {
1768       if (pt_solution_includes (gimple_call_use_set (call), base))
1769         return true;
1770     }
1771   else if ((TREE_CODE (base) == MEM_REF
1772             || TREE_CODE (base) == TARGET_MEM_REF)
1773            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1774     {
1775       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1776       if (!pi)
1777         return true;
1778
1779       if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1780         return true;
1781     }
1782   else
1783     return true;
1784
1785   /* Inspect call arguments for passed-by-value aliases.  */
1786 process_args:
1787   for (i = 0; i < gimple_call_num_args (call); ++i)
1788     {
1789       tree op = gimple_call_arg (call, i);
1790       int flags = gimple_call_arg_flags (call, i);
1791
1792       if (flags & EAF_UNUSED)
1793         continue;
1794
1795       if (TREE_CODE (op) == WITH_SIZE_EXPR)
1796         op = TREE_OPERAND (op, 0);
1797
1798       if (TREE_CODE (op) != SSA_NAME
1799           && !is_gimple_min_invariant (op))
1800         {
1801           ao_ref r;
1802           ao_ref_init (&r, op);
1803           if (refs_may_alias_p_1 (&r, ref, true))
1804             return true;
1805         }
1806     }
1807
1808   return false;
1809 }
1810
1811 static bool
1812 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref)
1813 {
1814   bool res;
1815   res = ref_maybe_used_by_call_p_1 (call, ref);
1816   if (res)
1817     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1818   else
1819     ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1820   return res;
1821 }
1822
1823
1824 /* If the statement STMT may use the memory reference REF return
1825    true, otherwise return false.  */
1826
1827 bool
1828 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref)
1829 {
1830   if (is_gimple_assign (stmt))
1831     {
1832       tree rhs;
1833
1834       /* All memory assign statements are single.  */
1835       if (!gimple_assign_single_p (stmt))
1836         return false;
1837
1838       rhs = gimple_assign_rhs1 (stmt);
1839       if (is_gimple_reg (rhs)
1840           || is_gimple_min_invariant (rhs)
1841           || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1842         return false;
1843
1844       return refs_may_alias_p (rhs, ref);
1845     }
1846   else if (is_gimple_call (stmt))
1847     return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref);
1848   else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1849     {
1850       tree retval = gimple_return_retval (return_stmt);
1851       if (retval
1852           && TREE_CODE (retval) != SSA_NAME
1853           && !is_gimple_min_invariant (retval)
1854           && refs_may_alias_p (retval, ref))
1855         return true;
1856       /* If ref escapes the function then the return acts as a use.  */
1857       tree base = ao_ref_base (ref);
1858       if (!base)
1859         ;
1860       else if (DECL_P (base))
1861         return is_global_var (base);
1862       else if (TREE_CODE (base) == MEM_REF
1863                || TREE_CODE (base) == TARGET_MEM_REF)
1864         return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1865       return false;
1866     }
1867
1868   return true;
1869 }
1870
1871 bool
1872 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref)
1873 {
1874   ao_ref r;
1875   ao_ref_init (&r, ref);
1876   return ref_maybe_used_by_stmt_p (stmt, &r);
1877 }
1878
1879 /* If the call in statement CALL may clobber the memory reference REF
1880    return true, otherwise return false.  */
1881
1882 bool
1883 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
1884 {
1885   tree base;
1886   tree callee;
1887
1888   /* If the call is pure or const it cannot clobber anything.  */
1889   if (gimple_call_flags (call)
1890       & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1891     return false;
1892   if (gimple_call_internal_p (call))
1893     switch (gimple_call_internal_fn (call))
1894       {
1895         /* Treat these internal calls like ECF_PURE for aliasing,
1896            they don't write to any memory the program should care about.
1897            They have important other side-effects, and read memory,
1898            so can't be ECF_NOVOPS.  */
1899       case IFN_UBSAN_NULL:
1900       case IFN_UBSAN_BOUNDS:
1901       case IFN_UBSAN_VPTR:
1902       case IFN_UBSAN_OBJECT_SIZE:
1903       case IFN_ASAN_CHECK:
1904         return false;
1905       default:
1906         break;
1907       }
1908
1909   base = ao_ref_base (ref);
1910   if (!base)
1911     return true;
1912
1913   if (TREE_CODE (base) == SSA_NAME
1914       || CONSTANT_CLASS_P (base))
1915     return false;
1916
1917   /* A call that is not without side-effects might involve volatile
1918      accesses and thus conflicts with all other volatile accesses.  */
1919   if (ref->volatile_p)
1920     return true;
1921
1922   /* If the reference is based on a decl that is not aliased the call
1923      cannot possibly clobber it.  */
1924   if (DECL_P (base)
1925       && !may_be_aliased (base)
1926       /* But local non-readonly statics can be modified through recursion
1927          or the call may implement a threading barrier which we must
1928          treat as may-def.  */
1929       && (TREE_READONLY (base)
1930           || !is_global_var (base)))
1931     return false;
1932
1933   callee = gimple_call_fndecl (call);
1934
1935   /* Handle those builtin functions explicitly that do not act as
1936      escape points.  See tree-ssa-structalias.c:find_func_aliases
1937      for the list of builtins we might need to handle here.  */
1938   if (callee != NULL_TREE
1939       && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1940     switch (DECL_FUNCTION_CODE (callee))
1941       {
1942         /* All the following functions clobber memory pointed to by
1943            their first argument.  */
1944         case BUILT_IN_STRCPY:
1945         case BUILT_IN_STRNCPY:
1946         case BUILT_IN_MEMCPY:
1947         case BUILT_IN_MEMMOVE:
1948         case BUILT_IN_MEMPCPY:
1949         case BUILT_IN_STPCPY:
1950         case BUILT_IN_STPNCPY:
1951         case BUILT_IN_STRCAT:
1952         case BUILT_IN_STRNCAT:
1953         case BUILT_IN_MEMSET:
1954         case BUILT_IN_TM_MEMSET:
1955         CASE_BUILT_IN_TM_STORE (1):
1956         CASE_BUILT_IN_TM_STORE (2):
1957         CASE_BUILT_IN_TM_STORE (4):
1958         CASE_BUILT_IN_TM_STORE (8):
1959         CASE_BUILT_IN_TM_STORE (FLOAT):
1960         CASE_BUILT_IN_TM_STORE (DOUBLE):
1961         CASE_BUILT_IN_TM_STORE (LDOUBLE):
1962         CASE_BUILT_IN_TM_STORE (M64):
1963         CASE_BUILT_IN_TM_STORE (M128):
1964         CASE_BUILT_IN_TM_STORE (M256):
1965         case BUILT_IN_TM_MEMCPY:
1966         case BUILT_IN_TM_MEMMOVE:
1967           {
1968             ao_ref dref;
1969             tree size = NULL_TREE;
1970             /* Don't pass in size for strncat, as the maximum size
1971                is strlen (dest) + n + 1 instead of n, resp.
1972                n + 1 at dest + strlen (dest), but strlen (dest) isn't
1973                known.  */
1974             if (gimple_call_num_args (call) == 3
1975                 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1976               size = gimple_call_arg (call, 2);
1977             ao_ref_init_from_ptr_and_size (&dref,
1978                                            gimple_call_arg (call, 0),
1979                                            size);
1980             return refs_may_alias_p_1 (&dref, ref, false);
1981           }
1982         case BUILT_IN_STRCPY_CHK:
1983         case BUILT_IN_STRNCPY_CHK:
1984         case BUILT_IN_MEMCPY_CHK:
1985         case BUILT_IN_MEMMOVE_CHK:
1986         case BUILT_IN_MEMPCPY_CHK:
1987         case BUILT_IN_STPCPY_CHK:
1988         case BUILT_IN_STPNCPY_CHK:
1989         case BUILT_IN_STRCAT_CHK:
1990         case BUILT_IN_STRNCAT_CHK:
1991         case BUILT_IN_MEMSET_CHK:
1992           {
1993             ao_ref dref;
1994             tree size = NULL_TREE;
1995             /* Don't pass in size for __strncat_chk, as the maximum size
1996                is strlen (dest) + n + 1 instead of n, resp.
1997                n + 1 at dest + strlen (dest), but strlen (dest) isn't
1998                known.  */
1999             if (gimple_call_num_args (call) == 4
2000                 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2001               size = gimple_call_arg (call, 2);
2002             ao_ref_init_from_ptr_and_size (&dref,
2003                                            gimple_call_arg (call, 0),
2004                                            size);
2005             return refs_may_alias_p_1 (&dref, ref, false);
2006           }
2007         case BUILT_IN_BCOPY:
2008           {
2009             ao_ref dref;
2010             tree size = gimple_call_arg (call, 2);
2011             ao_ref_init_from_ptr_and_size (&dref,
2012                                            gimple_call_arg (call, 1),
2013                                            size);
2014             return refs_may_alias_p_1 (&dref, ref, false);
2015           }
2016         /* Allocating memory does not have any side-effects apart from
2017            being the definition point for the pointer.  */
2018         case BUILT_IN_MALLOC:
2019         case BUILT_IN_ALIGNED_ALLOC:
2020         case BUILT_IN_CALLOC:
2021         case BUILT_IN_STRDUP:
2022         case BUILT_IN_STRNDUP:
2023           /* Unix98 specifies that errno is set on allocation failure.  */
2024           if (flag_errno_math
2025               && targetm.ref_may_alias_errno (ref))
2026             return true;
2027           return false;
2028         case BUILT_IN_STACK_SAVE:
2029         case BUILT_IN_ALLOCA:
2030         case BUILT_IN_ALLOCA_WITH_ALIGN:
2031         case BUILT_IN_ASSUME_ALIGNED:
2032           return false;
2033         /* But posix_memalign stores a pointer into the memory pointed to
2034            by its first argument.  */
2035         case BUILT_IN_POSIX_MEMALIGN:
2036           {
2037             tree ptrptr = gimple_call_arg (call, 0);
2038             ao_ref dref;
2039             ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2040                                            TYPE_SIZE_UNIT (ptr_type_node));
2041             return (refs_may_alias_p_1 (&dref, ref, false)
2042                     || (flag_errno_math
2043                         && targetm.ref_may_alias_errno (ref)));
2044           }
2045         /* Freeing memory kills the pointed-to memory.  More importantly
2046            the call has to serve as a barrier for moving loads and stores
2047            across it.  */
2048         case BUILT_IN_FREE:
2049         case BUILT_IN_VA_END:
2050           {
2051             tree ptr = gimple_call_arg (call, 0);
2052             return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2053           }
2054         /* Realloc serves both as allocation point and deallocation point.  */
2055         case BUILT_IN_REALLOC:
2056           {
2057             tree ptr = gimple_call_arg (call, 0);
2058             /* Unix98 specifies that errno is set on allocation failure.  */
2059             return ((flag_errno_math
2060                      && targetm.ref_may_alias_errno (ref))
2061                     || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2062           }
2063         case BUILT_IN_GAMMA_R:
2064         case BUILT_IN_GAMMAF_R:
2065         case BUILT_IN_GAMMAL_R:
2066         case BUILT_IN_LGAMMA_R:
2067         case BUILT_IN_LGAMMAF_R:
2068         case BUILT_IN_LGAMMAL_R:
2069           {
2070             tree out = gimple_call_arg (call, 1);
2071             if (ptr_deref_may_alias_ref_p_1 (out, ref))
2072               return true;
2073             if (flag_errno_math)
2074               break;
2075             return false;
2076           }
2077         case BUILT_IN_FREXP:
2078         case BUILT_IN_FREXPF:
2079         case BUILT_IN_FREXPL:
2080         case BUILT_IN_MODF:
2081         case BUILT_IN_MODFF:
2082         case BUILT_IN_MODFL:
2083           {
2084             tree out = gimple_call_arg (call, 1);
2085             return ptr_deref_may_alias_ref_p_1 (out, ref);
2086           }
2087         case BUILT_IN_REMQUO:
2088         case BUILT_IN_REMQUOF:
2089         case BUILT_IN_REMQUOL:
2090           {
2091             tree out = gimple_call_arg (call, 2);
2092             if (ptr_deref_may_alias_ref_p_1 (out, ref))
2093               return true;
2094             if (flag_errno_math)
2095               break;
2096             return false;
2097           }
2098         case BUILT_IN_SINCOS:
2099         case BUILT_IN_SINCOSF:
2100         case BUILT_IN_SINCOSL:
2101           {
2102             tree sin = gimple_call_arg (call, 1);
2103             tree cos = gimple_call_arg (call, 2);
2104             return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2105                     || ptr_deref_may_alias_ref_p_1 (cos, ref));
2106           }
2107         /* __sync_* builtins and some OpenMP builtins act as threading
2108            barriers.  */
2109 #undef DEF_SYNC_BUILTIN
2110 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2111 #include "sync-builtins.def"
2112 #undef DEF_SYNC_BUILTIN
2113         case BUILT_IN_GOMP_ATOMIC_START:
2114         case BUILT_IN_GOMP_ATOMIC_END:
2115         case BUILT_IN_GOMP_BARRIER:
2116         case BUILT_IN_GOMP_BARRIER_CANCEL:
2117         case BUILT_IN_GOMP_TASKWAIT:
2118         case BUILT_IN_GOMP_TASKGROUP_END:
2119         case BUILT_IN_GOMP_CRITICAL_START:
2120         case BUILT_IN_GOMP_CRITICAL_END:
2121         case BUILT_IN_GOMP_CRITICAL_NAME_START:
2122         case BUILT_IN_GOMP_CRITICAL_NAME_END:
2123         case BUILT_IN_GOMP_LOOP_END:
2124         case BUILT_IN_GOMP_LOOP_END_CANCEL:
2125         case BUILT_IN_GOMP_ORDERED_START:
2126         case BUILT_IN_GOMP_ORDERED_END:
2127         case BUILT_IN_GOMP_SECTIONS_END:
2128         case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2129         case BUILT_IN_GOMP_SINGLE_COPY_START:
2130         case BUILT_IN_GOMP_SINGLE_COPY_END:
2131           return true;
2132         default:
2133           /* Fallthru to general call handling.  */;
2134       }
2135
2136   /* Check if base is a global static variable that is not written
2137      by the function.  */
2138   if (callee != NULL_TREE
2139       && TREE_CODE (base) == VAR_DECL
2140       && TREE_STATIC (base))
2141     {
2142       struct cgraph_node *node = cgraph_node::get (callee);
2143       bitmap not_written;
2144
2145       if (node
2146           && (not_written = ipa_reference_get_not_written_global (node))
2147           && bitmap_bit_p (not_written, DECL_UID (base)))
2148         return false;
2149     }
2150
2151   /* Check if the base variable is call-clobbered.  */
2152   if (DECL_P (base))
2153     return pt_solution_includes (gimple_call_clobber_set (call), base);
2154   else if ((TREE_CODE (base) == MEM_REF
2155             || TREE_CODE (base) == TARGET_MEM_REF)
2156            && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2157     {
2158       struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2159       if (!pi)
2160         return true;
2161
2162       return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2163     }
2164
2165   return true;
2166 }
2167
2168 /* If the call in statement CALL may clobber the memory reference REF
2169    return true, otherwise return false.  */
2170
2171 bool
2172 call_may_clobber_ref_p (gcall *call, tree ref)
2173 {
2174   bool res;
2175   ao_ref r;
2176   ao_ref_init (&r, ref);
2177   res = call_may_clobber_ref_p_1 (call, &r);
2178   if (res)
2179     ++alias_stats.call_may_clobber_ref_p_may_alias;
2180   else
2181     ++alias_stats.call_may_clobber_ref_p_no_alias;
2182   return res;
2183 }
2184
2185
2186 /* If the statement STMT may clobber the memory reference REF return true,
2187    otherwise return false.  */
2188
2189 bool
2190 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref)
2191 {
2192   if (is_gimple_call (stmt))
2193     {
2194       tree lhs = gimple_call_lhs (stmt);
2195       if (lhs
2196           && TREE_CODE (lhs) != SSA_NAME)
2197         {
2198           ao_ref r;
2199           ao_ref_init (&r, lhs);
2200           if (refs_may_alias_p_1 (ref, &r, true))
2201             return true;
2202         }
2203
2204       return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2205     }
2206   else if (gimple_assign_single_p (stmt))
2207     {
2208       tree lhs = gimple_assign_lhs (stmt);
2209       if (TREE_CODE (lhs) != SSA_NAME)
2210         {
2211           ao_ref r;
2212           ao_ref_init (&r, lhs);
2213           return refs_may_alias_p_1 (ref, &r, true);
2214         }
2215     }
2216   else if (gimple_code (stmt) == GIMPLE_ASM)
2217     return true;
2218
2219   return false;
2220 }
2221
2222 bool
2223 stmt_may_clobber_ref_p (gimple *stmt, tree ref)
2224 {
2225   ao_ref r;
2226   ao_ref_init (&r, ref);
2227   return stmt_may_clobber_ref_p_1 (stmt, &r);
2228 }
2229
2230 /* If STMT kills the memory reference REF return true, otherwise
2231    return false.  */
2232
2233 bool
2234 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2235 {
2236   if (!ao_ref_base (ref))
2237     return false;
2238
2239   if (gimple_has_lhs (stmt)
2240       && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2241       /* The assignment is not necessarily carried out if it can throw
2242          and we can catch it in the current function where we could inspect
2243          the previous value.
2244          ???  We only need to care about the RHS throwing.  For aggregate
2245          assignments or similar calls and non-call exceptions the LHS
2246          might throw as well.  */
2247       && !stmt_can_throw_internal (stmt))
2248     {
2249       tree lhs = gimple_get_lhs (stmt);
2250       /* If LHS is literally a base of the access we are done.  */
2251       if (ref->ref)
2252         {
2253           tree base = ref->ref;
2254           if (handled_component_p (base))
2255             {
2256               tree saved_lhs0 = NULL_TREE;
2257               if (handled_component_p (lhs))
2258                 {
2259                   saved_lhs0 = TREE_OPERAND (lhs, 0);
2260                   TREE_OPERAND (lhs, 0) = integer_zero_node;
2261                 }
2262               do
2263                 {
2264                   /* Just compare the outermost handled component, if
2265                      they are equal we have found a possible common
2266                      base.  */
2267                   tree saved_base0 = TREE_OPERAND (base, 0);
2268                   TREE_OPERAND (base, 0) = integer_zero_node;
2269                   bool res = operand_equal_p (lhs, base, 0);
2270                   TREE_OPERAND (base, 0) = saved_base0;
2271                   if (res)
2272                     break;
2273                   /* Otherwise drop handled components of the access.  */
2274                   base = saved_base0;
2275                 }
2276               while (handled_component_p (base));
2277               if (saved_lhs0)
2278                 TREE_OPERAND (lhs, 0) = saved_lhs0;
2279             }
2280           /* Finally check if the lhs has the same address and size as the
2281              base candidate of the access.  */
2282           if (lhs == base
2283               || (((TYPE_SIZE (TREE_TYPE (lhs))
2284                     == TYPE_SIZE (TREE_TYPE (base)))
2285                    || (TYPE_SIZE (TREE_TYPE (lhs))
2286                        && TYPE_SIZE (TREE_TYPE (base))
2287                        && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2288                                            TYPE_SIZE (TREE_TYPE (base)), 0)))
2289                   && operand_equal_p (lhs, base, OEP_ADDRESS_OF)))
2290             return true;
2291         }
2292
2293       /* Now look for non-literal equal bases with the restriction of
2294          handling constant offset and size.  */
2295       /* For a must-alias check we need to be able to constrain
2296          the access properly.  */
2297       if (ref->max_size == -1)
2298         return false;
2299       HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2300       tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
2301       /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2302          so base == ref->base does not always hold.  */
2303       if (base != ref->base)
2304         {
2305           /* If both base and ref->base are MEM_REFs, only compare the
2306              first operand, and if the second operand isn't equal constant,
2307              try to add the offsets into offset and ref_offset.  */
2308           if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2309               && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2310             {
2311               if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
2312                                        TREE_OPERAND (ref->base, 1)))
2313                 {
2314                   offset_int off1 = mem_ref_offset (base);
2315                   off1 = wi::lshift (off1, LOG2_BITS_PER_UNIT);
2316                   off1 += offset;
2317                   offset_int off2 = mem_ref_offset (ref->base);
2318                   off2 = wi::lshift (off2, LOG2_BITS_PER_UNIT);
2319                   off2 += ref_offset;
2320                   if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2321                     {
2322                       offset = off1.to_shwi ();
2323                       ref_offset = off2.to_shwi ();
2324                     }
2325                   else
2326                     size = -1;
2327                 }
2328             }
2329           else
2330             size = -1;
2331         }
2332       /* For a must-alias check we need to be able to constrain
2333          the access properly.  */
2334       if (size != -1 && size == max_size)
2335         {
2336           if (offset <= ref_offset
2337               && offset + size >= ref_offset + ref->max_size)
2338             return true;
2339         }
2340     }
2341
2342   if (is_gimple_call (stmt))
2343     {
2344       tree callee = gimple_call_fndecl (stmt);
2345       if (callee != NULL_TREE
2346           && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2347         switch (DECL_FUNCTION_CODE (callee))
2348           {
2349           case BUILT_IN_FREE:
2350             {
2351               tree ptr = gimple_call_arg (stmt, 0);
2352               tree base = ao_ref_base (ref);
2353               if (base && TREE_CODE (base) == MEM_REF
2354                   && TREE_OPERAND (base, 0) == ptr)
2355                 return true;
2356               break;
2357             }
2358
2359           case BUILT_IN_MEMCPY:
2360           case BUILT_IN_MEMPCPY:
2361           case BUILT_IN_MEMMOVE:
2362           case BUILT_IN_MEMSET:
2363           case BUILT_IN_MEMCPY_CHK:
2364           case BUILT_IN_MEMPCPY_CHK:
2365           case BUILT_IN_MEMMOVE_CHK:
2366           case BUILT_IN_MEMSET_CHK:
2367             {
2368               /* For a must-alias check we need to be able to constrain
2369                  the access properly.  */
2370               if (ref->max_size == -1)
2371                 return false;
2372               tree dest = gimple_call_arg (stmt, 0);
2373               tree len = gimple_call_arg (stmt, 2);
2374               if (!tree_fits_shwi_p (len))
2375                 return false;
2376               tree rbase = ref->base;
2377               offset_int roffset = ref->offset;
2378               ao_ref dref;
2379               ao_ref_init_from_ptr_and_size (&dref, dest, len);
2380               tree base = ao_ref_base (&dref);
2381               offset_int offset = dref.offset;
2382               if (!base || dref.size == -1)
2383                 return false;
2384               if (TREE_CODE (base) == MEM_REF)
2385                 {
2386                   if (TREE_CODE (rbase) != MEM_REF)
2387                     return false;
2388                   // Compare pointers.
2389                   offset += wi::lshift (mem_ref_offset (base),
2390                                         LOG2_BITS_PER_UNIT);
2391                   roffset += wi::lshift (mem_ref_offset (rbase),
2392                                          LOG2_BITS_PER_UNIT);
2393                   base = TREE_OPERAND (base, 0);
2394                   rbase = TREE_OPERAND (rbase, 0);
2395                 }
2396               if (base == rbase
2397                   && wi::les_p (offset, roffset)
2398                   && wi::les_p (roffset + ref->max_size,
2399                                 offset + wi::lshift (wi::to_offset (len),
2400                                                      LOG2_BITS_PER_UNIT)))
2401                 return true;
2402               break;
2403             }
2404
2405           case BUILT_IN_VA_END:
2406             {
2407               tree ptr = gimple_call_arg (stmt, 0);
2408               if (TREE_CODE (ptr) == ADDR_EXPR)
2409                 {
2410                   tree base = ao_ref_base (ref);
2411                   if (TREE_OPERAND (ptr, 0) == base)
2412                     return true;
2413                 }
2414               break;
2415             }
2416
2417           default:;
2418           }
2419     }
2420   return false;
2421 }
2422
2423 bool
2424 stmt_kills_ref_p (gimple *stmt, tree ref)
2425 {
2426   ao_ref r;
2427   ao_ref_init (&r, ref);
2428   return stmt_kills_ref_p (stmt, &r);
2429 }
2430
2431
2432 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
2433    TARGET or a statement clobbering the memory reference REF in which
2434    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
2435
2436 static bool
2437 maybe_skip_until (gimple *phi, tree target, ao_ref *ref,
2438                   tree vuse, unsigned int *cnt, bitmap *visited,
2439                   bool abort_on_visited,
2440                   void *(*translate)(ao_ref *, tree, void *, bool *),
2441                   void *data)
2442 {
2443   basic_block bb = gimple_bb (phi);
2444
2445   if (!*visited)
2446     *visited = BITMAP_ALLOC (NULL);
2447
2448   bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
2449
2450   /* Walk until we hit the target.  */
2451   while (vuse != target)
2452     {
2453       gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2454       /* Recurse for PHI nodes.  */
2455       if (gimple_code (def_stmt) == GIMPLE_PHI)
2456         {
2457           /* An already visited PHI node ends the walk successfully.  */
2458           if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2459             return !abort_on_visited;
2460           vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2461                                            visited, abort_on_visited,
2462                                            translate, data);
2463           if (!vuse)
2464             return false;
2465           continue;
2466         }
2467       else if (gimple_nop_p (def_stmt))
2468         return false;
2469       else
2470         {
2471           /* A clobbering statement or the end of the IL ends it failing.  */
2472           ++*cnt;
2473           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2474             {
2475               bool disambiguate_only = true;
2476               if (translate
2477                   && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
2478                 ;
2479               else
2480                 return false;
2481             }
2482         }
2483       /* If we reach a new basic-block see if we already skipped it
2484          in a previous walk that ended successfully.  */
2485       if (gimple_bb (def_stmt) != bb)
2486         {
2487           if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2488             return !abort_on_visited;
2489           bb = gimple_bb (def_stmt);
2490         }
2491       vuse = gimple_vuse (def_stmt);
2492     }
2493   return true;
2494 }
2495
2496 /* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
2497    until we hit the phi argument definition that dominates the other one.
2498    Return that, or NULL_TREE if there is no such definition.  */
2499
2500 static tree
2501 get_continuation_for_phi_1 (gimple *phi, tree arg0, tree arg1,
2502                             ao_ref *ref, unsigned int *cnt,
2503                             bitmap *visited, bool abort_on_visited,
2504                             void *(*translate)(ao_ref *, tree, void *, bool *),
2505                             void *data)
2506 {
2507   gimple *def0 = SSA_NAME_DEF_STMT (arg0);
2508   gimple *def1 = SSA_NAME_DEF_STMT (arg1);
2509   tree common_vuse;
2510
2511   if (arg0 == arg1)
2512     return arg0;
2513   else if (gimple_nop_p (def0)
2514            || (!gimple_nop_p (def1)
2515                && dominated_by_p (CDI_DOMINATORS,
2516                                   gimple_bb (def1), gimple_bb (def0))))
2517     {
2518       if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2519                             visited, abort_on_visited, translate, data))
2520         return arg0;
2521     }
2522   else if (gimple_nop_p (def1)
2523            || dominated_by_p (CDI_DOMINATORS,
2524                               gimple_bb (def0), gimple_bb (def1)))
2525     {
2526       if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2527                             visited, abort_on_visited, translate, data))
2528         return arg1;
2529     }
2530   /* Special case of a diamond:
2531        MEM_1 = ...
2532        goto (cond) ? L1 : L2
2533        L1: store1 = ...    #MEM_2 = vuse(MEM_1)
2534            goto L3
2535        L2: store2 = ...    #MEM_3 = vuse(MEM_1)
2536        L3: MEM_4 = PHI<MEM_2, MEM_3>
2537      We were called with the PHI at L3, MEM_2 and MEM_3 don't
2538      dominate each other, but still we can easily skip this PHI node
2539      if we recognize that the vuse MEM operand is the same for both,
2540      and that we can skip both statements (they don't clobber us).
2541      This is still linear.  Don't use maybe_skip_until, that might
2542      potentially be slow.  */
2543   else if ((common_vuse = gimple_vuse (def0))
2544            && common_vuse == gimple_vuse (def1))
2545     {
2546       bool disambiguate_only = true;
2547       *cnt += 2;
2548       if ((!stmt_may_clobber_ref_p_1 (def0, ref)
2549            || (translate
2550                && (*translate) (ref, arg0, data, &disambiguate_only) == NULL))
2551           && (!stmt_may_clobber_ref_p_1 (def1, ref)
2552               || (translate
2553                   && (*translate) (ref, arg1, data, &disambiguate_only) == NULL)))
2554         return common_vuse;
2555     }
2556
2557   return NULL_TREE;
2558 }
2559
2560
2561 /* Starting from a PHI node for the virtual operand of the memory reference
2562    REF find a continuation virtual operand that allows to continue walking
2563    statements dominating PHI skipping only statements that cannot possibly
2564    clobber REF.  Increments *CNT for each alias disambiguation done.
2565    Returns NULL_TREE if no suitable virtual operand can be found.  */
2566
2567 tree
2568 get_continuation_for_phi (gimple *phi, ao_ref *ref,
2569                           unsigned int *cnt, bitmap *visited,
2570                           bool abort_on_visited,
2571                           void *(*translate)(ao_ref *, tree, void *, bool *),
2572                           void *data)
2573 {
2574   unsigned nargs = gimple_phi_num_args (phi);
2575
2576   /* Through a single-argument PHI we can simply look through.  */
2577   if (nargs == 1)
2578     return PHI_ARG_DEF (phi, 0);
2579
2580   /* For two or more arguments try to pairwise skip non-aliasing code
2581      until we hit the phi argument definition that dominates the other one.  */
2582   else if (nargs >= 2)
2583     {
2584       tree arg0, arg1;
2585       unsigned i;
2586
2587       /* Find a candidate for the virtual operand which definition
2588          dominates those of all others.  */
2589       arg0 = PHI_ARG_DEF (phi, 0);
2590       if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
2591         for (i = 1; i < nargs; ++i)
2592           {
2593             arg1 = PHI_ARG_DEF (phi, i);
2594             if (SSA_NAME_IS_DEFAULT_DEF (arg1))
2595               {
2596                 arg0 = arg1;
2597                 break;
2598               }
2599             if (dominated_by_p (CDI_DOMINATORS,
2600                                 gimple_bb (SSA_NAME_DEF_STMT (arg0)),
2601                                 gimple_bb (SSA_NAME_DEF_STMT (arg1))))
2602               arg0 = arg1;
2603           }
2604
2605       /* Then pairwise reduce against the found candidate.  */
2606       for (i = 0; i < nargs; ++i)
2607         {
2608           arg1 = PHI_ARG_DEF (phi, i);
2609           arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2610                                              cnt, visited, abort_on_visited,
2611                                              translate, data);
2612           if (!arg0)
2613             return NULL_TREE;
2614         }
2615
2616       return arg0;
2617     }
2618
2619   return NULL_TREE;
2620 }
2621
2622 /* Based on the memory reference REF and its virtual use VUSE call
2623    WALKER for each virtual use that is equivalent to VUSE, including VUSE
2624    itself.  That is, for each virtual use for which its defining statement
2625    does not clobber REF.
2626
2627    WALKER is called with REF, the current virtual use and DATA.  If
2628    WALKER returns non-NULL the walk stops and its result is returned.
2629    At the end of a non-successful walk NULL is returned.
2630
2631    TRANSLATE if non-NULL is called with a pointer to REF, the virtual
2632    use which definition is a statement that may clobber REF and DATA.
2633    If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
2634    If TRANSLATE returns non-NULL the walk stops and its result is returned.
2635    If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
2636    to adjust REF and *DATA to make that valid.
2637
2638    VALUEIZE if non-NULL is called with the next VUSE that is considered
2639    and return value is substituted for that.  This can be used to
2640    implement optimistic value-numbering for example.  Note that the
2641    VUSE argument is assumed to be valueized already.
2642
2643    TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
2644
2645 void *
2646 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2647                         void *(*walker)(ao_ref *, tree, unsigned int, void *),
2648                         void *(*translate)(ao_ref *, tree, void *, bool *),
2649                         tree (*valueize)(tree),
2650                         void *data)
2651 {
2652   bitmap visited = NULL;
2653   void *res;
2654   unsigned int cnt = 0;
2655   bool translated = false;
2656
2657   timevar_push (TV_ALIAS_STMT_WALK);
2658
2659   do
2660     {
2661       gimple *def_stmt;
2662
2663       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
2664       res = (*walker) (ref, vuse, cnt, data);
2665       /* Abort walk.  */
2666       if (res == (void *)-1)
2667         {
2668           res = NULL;
2669           break;
2670         }
2671       /* Lookup succeeded.  */
2672       else if (res != NULL)
2673         break;
2674
2675       if (valueize)
2676         vuse = valueize (vuse);
2677       def_stmt = SSA_NAME_DEF_STMT (vuse);
2678       if (gimple_nop_p (def_stmt))
2679         break;
2680       else if (gimple_code (def_stmt) == GIMPLE_PHI)
2681         vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2682                                          &visited, translated, translate, data);
2683       else
2684         {
2685           cnt++;
2686           if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2687             {
2688               if (!translate)
2689                 break;
2690               bool disambiguate_only = false;
2691               res = (*translate) (ref, vuse, data, &disambiguate_only);
2692               /* Failed lookup and translation.  */
2693               if (res == (void *)-1)
2694                 {
2695                   res = NULL;
2696                   break;
2697                 }
2698               /* Lookup succeeded.  */
2699               else if (res != NULL)
2700                 break;
2701               /* Translation succeeded, continue walking.  */
2702               translated = translated || !disambiguate_only;
2703             }
2704           vuse = gimple_vuse (def_stmt);
2705         }
2706     }
2707   while (vuse);
2708
2709   if (visited)
2710     BITMAP_FREE (visited);
2711
2712   timevar_pop (TV_ALIAS_STMT_WALK);
2713
2714   return res;
2715 }
2716
2717
2718 /* Based on the memory reference REF call WALKER for each vdef which
2719    defining statement may clobber REF, starting with VDEF.  If REF
2720    is NULL_TREE, each defining statement is visited.
2721
2722    WALKER is called with REF, the current vdef and DATA.  If WALKER
2723    returns true the walk is stopped, otherwise it continues.
2724
2725    If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
2726    The pointer may be NULL and then we do not track this information.
2727
2728    At PHI nodes walk_aliased_vdefs forks into one walk for reach
2729    PHI argument (but only one walk continues on merge points), the
2730    return value is true if any of the walks was successful.
2731
2732    The function returns the number of statements walked.  */
2733
2734 static unsigned int
2735 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2736                       bool (*walker)(ao_ref *, tree, void *), void *data,
2737                       bitmap *visited, unsigned int cnt,
2738                       bool *function_entry_reached)
2739 {
2740   do
2741     {
2742       gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
2743
2744       if (*visited
2745           && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2746         return cnt;
2747
2748       if (gimple_nop_p (def_stmt))
2749         {
2750           if (function_entry_reached)
2751             *function_entry_reached = true;
2752           return cnt;
2753         }
2754       else if (gimple_code (def_stmt) == GIMPLE_PHI)
2755         {
2756           unsigned i;
2757           if (!*visited)
2758             *visited = BITMAP_ALLOC (NULL);
2759           for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2760             cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2761                                          walker, data, visited, 0,
2762                                          function_entry_reached);
2763           return cnt;
2764         }
2765
2766       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
2767       cnt++;
2768       if ((!ref
2769            || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2770           && (*walker) (ref, vdef, data))
2771         return cnt;
2772
2773       vdef = gimple_vuse (def_stmt);
2774     }
2775   while (1);
2776 }
2777
2778 unsigned int
2779 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2780                     bool (*walker)(ao_ref *, tree, void *), void *data,
2781                     bitmap *visited,
2782                     bool *function_entry_reached)
2783 {
2784   bitmap local_visited = NULL;
2785   unsigned int ret;
2786
2787   timevar_push (TV_ALIAS_STMT_WALK);
2788
2789   if (function_entry_reached)
2790     *function_entry_reached = false;
2791
2792   ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2793                               visited ? visited : &local_visited, 0,
2794                               function_entry_reached);
2795   if (local_visited)
2796     BITMAP_FREE (local_visited);
2797
2798   timevar_pop (TV_ALIAS_STMT_WALK);
2799
2800   return ret;
2801 }
2802