]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/tree-ssa-alias.c
* gcc-interface/trans.c (add_decl_expr): At toplevel, mark the
[thirdparty/gcc.git] / gcc / tree-ssa-alias.c
CommitLineData
4ee9c684 1/* Alias analysis for trees.
cfaf579d 2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4ee9c684 4 Contributed by Diego Novillo <dnovillo@redhat.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
8c4c00c1 10the Free Software Foundation; either version 3, or (at your option)
4ee9c684 11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
8c4c00c1 19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
4ee9c684 21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "tree.h"
27#include "rtl.h"
28#include "tm_p.h"
29#include "hard-reg-set.h"
30#include "basic-block.h"
31#include "timevar.h"
32#include "expr.h"
33#include "ggc.h"
34#include "langhooks.h"
35#include "flags.h"
36#include "function.h"
37#include "diagnostic.h"
38#include "tree-dump.h"
75a70cf9 39#include "gimple.h"
4ee9c684 40#include "tree-flow.h"
41#include "tree-inline.h"
4ee9c684 42#include "tree-pass.h"
43#include "convert.h"
44#include "params.h"
f7d118a9 45#include "ipa-type-escape.h"
2be14d8b 46#include "vec.h"
a55dc2cd 47#include "bitmap.h"
05f3df98 48#include "vecprim.h"
4fb5e5ca 49#include "pointer-set.h"
6cc9bd27 50#include "alloc-pool.h"
dd277d48 51#include "tree-ssa-alias.h"
a55dc2cd 52
dd277d48 53/* Broad overview of how alias analysis on gimple works:
4fb5e5ca 54
dd277d48 55 Statements clobbering or using memory are linked through the
56 virtual operand factored use-def chain. The virtual operand
57 is unique per function, its symbol is accessible via gimple_vop (cfun).
58 Virtual operands are used for efficiently walking memory statements
59 in the gimple IL and are useful for things like value-numbering as
60 a generation count for memory references.
339d7079 61
dd277d48 62 SSA_NAME pointers may have associated points-to information
63 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
64 points-to information is (re-)computed by the TODO_rebuild_alias
65 pass manager todo. Points-to information is also used for more
66 precise tracking of call-clobbered and call-used variables and
67 related disambiguations.
339d7079 68
dd277d48 69 This file contains functions for disambiguating memory references,
70 the so called alias-oracle and tools for walking of the gimple IL.
339d7079 71
dd277d48 72 The main alias-oracle entry-points are
339d7079 73
dd277d48 74 bool stmt_may_clobber_ref_p (gimple, tree)
339d7079 75
dd277d48 76 This function queries if a statement may invalidate (parts of)
77 the memory designated by the reference tree argument.
339d7079 78
dd277d48 79 bool ref_maybe_used_by_stmt_p (gimple, tree)
339d7079 80
dd277d48 81 This function queries if a statement may need (parts of) the
82 memory designated by the reference tree argument.
339d7079 83
dd277d48 84 There are variants of these functions that only handle the call
85 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
86 Note that these do not disambiguate against a possible call lhs.
339d7079 87
dd277d48 88 bool refs_may_alias_p (tree, tree)
339d7079 89
dd277d48 90 This function tries to disambiguate two reference trees.
05931b00 91
dd277d48 92 bool ptr_deref_may_alias_global_p (tree)
339d7079 93
dd277d48 94 This function queries if dereferencing a pointer variable may
95 alias global memory.
339d7079 96
dd277d48 97 More low-level disambiguators are available and documented in
98 this file. Low-level disambiguators dealing with points-to
99 information are in tree-ssa-structalias.c. */
339d7079 100
339d7079 101
dd277d48 102/* Query statistics for the different low-level disambiguators.
103 A high-level query may trigger multiple of them. */
339d7079 104
dd277d48 105static struct {
106 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
107 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
108 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
109 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
110 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
111 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
112} alias_stats;
339d7079 113
dd277d48 114void
115dump_alias_stats (FILE *s)
116{
117 fprintf (s, "\nAlias oracle query stats:\n");
118 fprintf (s, " refs_may_alias_p: "
119 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
120 HOST_WIDE_INT_PRINT_DEC" queries\n",
121 alias_stats.refs_may_alias_p_no_alias,
122 alias_stats.refs_may_alias_p_no_alias
123 + alias_stats.refs_may_alias_p_may_alias);
124 fprintf (s, " ref_maybe_used_by_call_p: "
125 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
126 HOST_WIDE_INT_PRINT_DEC" queries\n",
127 alias_stats.ref_maybe_used_by_call_p_no_alias,
128 alias_stats.refs_may_alias_p_no_alias
129 + alias_stats.ref_maybe_used_by_call_p_may_alias);
130 fprintf (s, " call_may_clobber_ref_p: "
131 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
132 HOST_WIDE_INT_PRINT_DEC" queries\n",
133 alias_stats.call_may_clobber_ref_p_no_alias,
134 alias_stats.call_may_clobber_ref_p_no_alias
135 + alias_stats.call_may_clobber_ref_p_may_alias);
136}
137
138
139/* Return true, if dereferencing PTR may alias with a global variable. */
4ee9c684 140
dd277d48 141bool
142ptr_deref_may_alias_global_p (tree ptr)
4ee9c684 143{
dd277d48 144 struct ptr_info_def *pi;
4fb5e5ca 145
dd277d48 146 /* If we end up with a pointer constant here that may point
147 to global memory. */
148 if (TREE_CODE (ptr) != SSA_NAME)
149 return true;
4ee9c684 150
dd277d48 151 pi = SSA_NAME_PTR_INFO (ptr);
4ee9c684 152
dd277d48 153 /* If we do not have points-to information for this variable,
154 we have to punt. */
155 if (!pi)
156 return true;
4ee9c684 157
2a3ebafa 158 /* ??? This does not use TBAA to prune globals ptr may not access. */
dd277d48 159 return pt_solution_includes_global (&pi->pt);
4ee9c684 160}
161
2a3ebafa 162/* Return true if dereferencing PTR may alias DECL.
163 The caller is responsible for applying TBAA to see if PTR
164 may access DECL at all. */
4ee9c684 165
dd277d48 166static bool
167ptr_deref_may_alias_decl_p (tree ptr, tree decl)
4ee9c684 168{
dd277d48 169 struct ptr_info_def *pi;
f7d118a9 170
f85fb819 171 gcc_assert ((TREE_CODE (ptr) == SSA_NAME
172 || TREE_CODE (ptr) == ADDR_EXPR
173 || TREE_CODE (ptr) == INTEGER_CST)
174 && (TREE_CODE (decl) == VAR_DECL
175 || TREE_CODE (decl) == PARM_DECL
176 || TREE_CODE (decl) == RESULT_DECL));
2fd0b5dc 177
dd277d48 178 /* Non-aliased variables can not be pointed to. */
179 if (!may_be_aliased (decl))
180 return false;
2fd0b5dc 181
047fdd47 182 /* ADDR_EXPR pointers either just offset another pointer or directly
183 specify the pointed-to set. */
184 if (TREE_CODE (ptr) == ADDR_EXPR)
185 {
186 tree base = get_base_address (TREE_OPERAND (ptr, 0));
187 if (base
188 && INDIRECT_REF_P (base))
189 ptr = TREE_OPERAND (base, 0);
190 else if (base
191 && SSA_VAR_P (base))
192 return operand_equal_p (base, decl, 0);
193 else if (base
194 && CONSTANT_CLASS_P (base))
195 return false;
196 else
197 return true;
198 }
199
f85fb819 200 /* We can end up with dereferencing constant pointers.
047fdd47 201 Just bail out in this case. */
f85fb819 202 if (TREE_CODE (ptr) == INTEGER_CST)
047fdd47 203 return true;
204
dd277d48 205 /* If we do not have useful points-to information for this pointer
206 we cannot disambiguate anything else. */
207 pi = SSA_NAME_PTR_INFO (ptr);
208 if (!pi)
2fd0b5dc 209 return true;
210
8bb76364 211 /* If the decl can be used as a restrict tag and we have a restrict
212 pointer and that pointers points-to set doesn't contain this decl
213 then they can't alias. */
214 if (DECL_RESTRICTED_P (decl)
215 && TYPE_RESTRICT (TREE_TYPE (ptr))
216 && pi->pt.vars_contains_restrict)
217 return bitmap_bit_p (pi->pt.vars, DECL_UID (decl));
218
dd277d48 219 return pt_solution_includes (&pi->pt, decl);
2fd0b5dc 220}
4ee9c684 221
2a3ebafa 222/* Return true if dereferenced PTR1 and PTR2 may alias.
223 The caller is responsible for applying TBAA to see if accesses
224 through PTR1 and PTR2 may conflict at all. */
4ee9c684 225
dd277d48 226static bool
227ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
4ee9c684 228{
dd277d48 229 struct ptr_info_def *pi1, *pi2;
4ee9c684 230
f85fb819 231 gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
232 || TREE_CODE (ptr1) == ADDR_EXPR
233 || TREE_CODE (ptr1) == INTEGER_CST)
234 && (TREE_CODE (ptr2) == SSA_NAME
235 || TREE_CODE (ptr2) == ADDR_EXPR
236 || TREE_CODE (ptr2) == INTEGER_CST));
237
047fdd47 238 /* ADDR_EXPR pointers either just offset another pointer or directly
239 specify the pointed-to set. */
240 if (TREE_CODE (ptr1) == ADDR_EXPR)
241 {
242 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
243 if (base
244 && INDIRECT_REF_P (base))
245 ptr1 = TREE_OPERAND (base, 0);
246 else if (base
247 && SSA_VAR_P (base))
248 return ptr_deref_may_alias_decl_p (ptr2, base);
249 else
250 return true;
251 }
252 if (TREE_CODE (ptr2) == ADDR_EXPR)
253 {
254 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
255 if (base
256 && INDIRECT_REF_P (base))
257 ptr2 = TREE_OPERAND (base, 0);
258 else if (base
259 && SSA_VAR_P (base))
260 return ptr_deref_may_alias_decl_p (ptr1, base);
261 else
262 return true;
263 }
264
f85fb819 265 /* We can end up with dereferencing constant pointers.
047fdd47 266 Just bail out in this case. */
f85fb819 267 if (TREE_CODE (ptr1) == INTEGER_CST
268 || TREE_CODE (ptr2) == INTEGER_CST)
dd277d48 269 return true;
4ee9c684 270
dd277d48 271 /* We may end up with two empty points-to solutions for two same pointers.
272 In this case we still want to say both pointers alias, so shortcut
273 that here. */
274 if (ptr1 == ptr2)
275 return true;
81d08033 276
dd277d48 277 /* If we do not have useful points-to information for either pointer
278 we cannot disambiguate anything else. */
279 pi1 = SSA_NAME_PTR_INFO (ptr1);
280 pi2 = SSA_NAME_PTR_INFO (ptr2);
281 if (!pi1 || !pi2)
282 return true;
81d08033 283
1c1f1bc0 284 /* If both pointers are restrict-qualified try to disambiguate
285 with restrict information. */
286 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
287 && TYPE_RESTRICT (TREE_TYPE (ptr2))
288 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
289 return false;
290
2a3ebafa 291 /* ??? This does not use TBAA to prune decls from the intersection
292 that not both pointers may access. */
dd277d48 293 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
81d08033 294}
295
047fdd47 296/* Return true if dereferencing PTR may alias *REF.
297 The caller is responsible for applying TBAA to see if PTR
298 may access *REF at all. */
299
300static bool
301ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
302{
303 tree base = ao_ref_base (ref);
304
305 if (INDIRECT_REF_P (base))
306 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
307 else if (SSA_VAR_P (base))
308 return ptr_deref_may_alias_decl_p (ptr, base);
309
310 return true;
311}
312
4ee9c684 313
314/* Dump alias information on FILE. */
315
316void
317dump_alias_info (FILE *file)
318{
319 size_t i;
320 const char *funcname
5135beeb 321 = lang_hooks.decl_printable_name (current_function_decl, 2);
a55dc2cd 322 referenced_var_iterator rvi;
323 tree var;
4ee9c684 324
dd277d48 325 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
81d08033 326
327 fprintf (file, "Aliased symbols\n\n");
48e1416a 328
a55dc2cd 329 FOR_EACH_REFERENCED_VAR (var, rvi)
81d08033 330 {
81d08033 331 if (may_be_aliased (var))
332 dump_variable (file, var);
333 }
334
7f81b5ee 335 fprintf (file, "\nCall clobber information\n");
336
337 fprintf (file, "\nESCAPED");
338 dump_points_to_solution (file, &cfun->gimple_df->escaped);
339 fprintf (file, "\nCALLUSED");
340 dump_points_to_solution (file, &cfun->gimple_df->callused);
341
342 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
81d08033 343
81d08033 344 for (i = 1; i < num_ssa_names; i++)
345 {
346 tree ptr = ssa_name (i);
a48b1470 347 struct ptr_info_def *pi;
48e1416a 348
dd277d48 349 if (ptr == NULL_TREE
350 || SSA_NAME_IN_FREE_LIST (ptr))
a48b1470 351 continue;
352
353 pi = SSA_NAME_PTR_INFO (ptr);
dd277d48 354 if (pi)
81d08033 355 dump_points_to_info_for (file, ptr);
356 }
4ee9c684 357
4ee9c684 358 fprintf (file, "\n");
359}
360
361
362/* Dump alias information on stderr. */
363
364void
365debug_alias_info (void)
366{
367 dump_alias_info (stderr);
368}
369
370
786d45db 371/* Return the alias information associated with pointer T. It creates a
372 new instance if none existed. */
373
3276c83b 374struct ptr_info_def *
786d45db 375get_ptr_info (tree t)
376{
377 struct ptr_info_def *pi;
378
8c0963c4 379 gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
786d45db 380
381 pi = SSA_NAME_PTR_INFO (t);
382 if (pi == NULL)
383 {
43959b95 384 pi = GGC_CNEW (struct ptr_info_def);
dd277d48 385 pt_solution_reset (&pi->pt);
786d45db 386 SSA_NAME_PTR_INFO (t) = pi;
387 }
388
389 return pi;
390}
391
7f81b5ee 392/* Dump the points-to set *PT into FILE. */
4ee9c684 393
d793732c 394void
7f81b5ee 395dump_points_to_solution (FILE *file, struct pt_solution *pt)
4ee9c684 396{
7f81b5ee 397 if (pt->anything)
398 fprintf (file, ", points-to anything");
4ee9c684 399
7f81b5ee 400 if (pt->nonlocal)
401 fprintf (file, ", points-to non-local");
4ee9c684 402
7f81b5ee 403 if (pt->escaped)
404 fprintf (file, ", points-to escaped");
405
406 if (pt->null)
407 fprintf (file, ", points-to NULL");
408
409 if (pt->vars)
4ee9c684 410 {
7f81b5ee 411 fprintf (file, ", points-to vars: ");
412 dump_decl_set (file, pt->vars);
413 if (pt->vars_contains_global)
414 fprintf (file, " (includes global vars)");
415 }
416}
4ee9c684 417
7f81b5ee 418/* Dump points-to information for SSA_NAME PTR into FILE. */
4ee9c684 419
7f81b5ee 420void
421dump_points_to_info_for (FILE *file, tree ptr)
422{
423 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
4ee9c684 424
7f81b5ee 425 print_generic_expr (file, ptr, dump_flags);
1e1a4c8c 426
7f81b5ee 427 if (pi)
428 dump_points_to_solution (file, &pi->pt);
429 else
430 fprintf (file, ", points-to anything");
4ee9c684 431
432 fprintf (file, "\n");
433}
434
435
d793732c 436/* Dump points-to information for VAR into stderr. */
437
438void
439debug_points_to_info_for (tree var)
440{
441 dump_points_to_info_for (stderr, var);
442}
443
3918bd18 444
445/* Initializes the alias-oracle reference representation *R from REF. */
446
447void
448ao_ref_init (ao_ref *r, tree ref)
449{
450 r->ref = ref;
451 r->base = NULL_TREE;
452 r->offset = 0;
453 r->size = -1;
454 r->max_size = -1;
455 r->ref_alias_set = -1;
456 r->base_alias_set = -1;
457}
458
459/* Returns the base object of the memory reference *REF. */
460
461tree
462ao_ref_base (ao_ref *ref)
463{
464 if (ref->base)
465 return ref->base;
466 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
467 &ref->max_size);
468 return ref->base;
469}
470
471/* Returns the base object alias set of the memory reference *REF. */
472
473static alias_set_type ATTRIBUTE_UNUSED
474ao_ref_base_alias_set (ao_ref *ref)
475{
476 if (ref->base_alias_set != -1)
477 return ref->base_alias_set;
478 ref->base_alias_set = get_alias_set (ao_ref_base (ref));
479 return ref->base_alias_set;
480}
481
482/* Returns the reference alias set of the memory reference *REF. */
483
484alias_set_type
485ao_ref_alias_set (ao_ref *ref)
486{
487 if (ref->ref_alias_set != -1)
488 return ref->ref_alias_set;
489 ref->ref_alias_set = get_alias_set (ref->ref);
490 return ref->ref_alias_set;
491}
492
134899ac 493/* Init an alias-oracle reference representation from a gimple pointer
494 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
495 size is assumed to be unknown. The access is assumed to be only
496 to or after of the pointer target, not before it. */
497
498void
499ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
500{
501 HOST_WIDE_INT t1, t2;
502 ref->ref = NULL_TREE;
503 if (TREE_CODE (ptr) == ADDR_EXPR)
504 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
505 &ref->offset, &t1, &t2);
506 else
507 {
508 ref->base = build1 (INDIRECT_REF, char_type_node, ptr);
509 ref->offset = 0;
510 }
511 if (size
512 && host_integerp (size, 0)
513 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
514 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
515 else
516 ref->max_size = ref->size = -1;
517 ref->ref_alias_set = 0;
518 ref->base_alias_set = 0;
519}
520
dd277d48 521/* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
522 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
523 decide. */
d793732c 524
dd277d48 525static inline int
526same_type_for_tbaa (tree type1, tree type2)
527{
528 type1 = TYPE_MAIN_VARIANT (type1);
529 type2 = TYPE_MAIN_VARIANT (type2);
4ee9c684 530
dd277d48 531 /* If we would have to do structural comparison bail out. */
532 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
533 || TYPE_STRUCTURAL_EQUALITY_P (type2))
534 return -1;
535
536 /* Compare the canonical types. */
537 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
538 return 1;
539
540 /* ??? Array types are not properly unified in all cases as we have
541 spurious changes in the index types for example. Removing this
542 causes all sorts of problems with the Fortran frontend. */
543 if (TREE_CODE (type1) == ARRAY_TYPE
544 && TREE_CODE (type2) == ARRAY_TYPE)
545 return -1;
546
547 /* The types are known to be not equal. */
548 return 0;
549}
550
551/* Determine if the two component references REF1 and REF2 which are
552 based on access types TYPE1 and TYPE2 and of which at least one is based
553 on an indirect reference may alias. */
554
555static bool
a42ce2cc 556aliasing_component_refs_p (tree ref1, tree type1,
557 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
558 tree ref2, tree type2,
559 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
dd277d48 560{
561 /* If one reference is a component references through pointers try to find a
562 common base and apply offset based disambiguation. This handles
563 for example
564 struct A { int i; int j; } *q;
565 struct B { struct A a; int k; } *p;
566 disambiguating q->i and p->a.j. */
567 tree *refp;
568 int same_p;
569
570 /* Now search for the type1 in the access path of ref2. This
571 would be a common base for doing offset based disambiguation on. */
bc3c318e 572 refp = &ref2;
dd277d48 573 while (handled_component_p (*refp)
574 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
575 refp = &TREE_OPERAND (*refp, 0);
576 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
577 /* If we couldn't compare types we have to bail out. */
578 if (same_p == -1)
579 return true;
580 else if (same_p == 1)
581 {
582 HOST_WIDE_INT offadj, sztmp, msztmp;
583 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
584 offset2 -= offadj;
585 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
586 }
587 /* If we didn't find a common base, try the other way around. */
bc3c318e 588 refp = &ref1;
dd277d48 589 while (handled_component_p (*refp)
590 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
591 refp = &TREE_OPERAND (*refp, 0);
592 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
593 /* If we couldn't compare types we have to bail out. */
594 if (same_p == -1)
595 return true;
596 else if (same_p == 1)
597 {
598 HOST_WIDE_INT offadj, sztmp, msztmp;
599 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
600 offset1 -= offadj;
601 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
602 }
a42ce2cc 603
604 /* We haven't found any common base to apply offset-based disambiguation.
605 There are two cases:
606 1. The base access types have the same alias set. This can happen
607 in Ada when a function with an unconstrained parameter passed by
608 reference is called on a constrained object and inlined: the types
609 have the same alias set but aren't equivalent. The references may
610 alias in this case.
611 2. The base access types don't have the same alias set, i.e. one set
612 is a subset of the other. We have proved that B1 is not in the
613 access path B2.path and that B2 is not in the access path B1.path
614 so the references may not alias. */
615 return get_alias_set (type1) == get_alias_set (type2);
dd277d48 616}
617
618/* Return true if two memory references based on the variables BASE1
077ad5ad 619 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
620 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
dd277d48 621
622static bool
623decl_refs_may_alias_p (tree base1,
624 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
625 tree base2,
626 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
4ee9c684 627{
dd277d48 628 gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
4ee9c684 629
dd277d48 630 /* If both references are based on different variables, they cannot alias. */
631 if (!operand_equal_p (base1, base2, 0))
632 return false;
4ee9c684 633
dd277d48 634 /* If both references are based on the same variable, they cannot alias if
635 the accesses do not overlap. */
636 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
637}
638
639/* Return true if an indirect reference based on *PTR1 constrained
077ad5ad 640 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
641 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
dd277d48 642 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
643 in which case they are computed on-demand. REF1 and REF2
644 if non-NULL are the complete memory reference trees. */
645
646static bool
647indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
648 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
649 alias_set_type base1_alias_set,
650 tree ref2, tree base2,
651 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
652 alias_set_type base2_alias_set)
653{
654 /* If only one reference is based on a variable, they cannot alias if
655 the pointer access is beyond the extent of the variable access.
656 (the pointer base cannot validly point to an offset less than zero
657 of the variable).
658 They also cannot alias if the pointer may not point to the decl. */
659 if (max_size2 != -1
660 && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
661 return false;
662 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
663 return false;
664
665 /* Disambiguations that rely on strict aliasing rules follow. */
666 if (!flag_strict_aliasing)
667 return true;
668
669 /* If the alias set for a pointer access is zero all bets are off. */
670 if (base1_alias_set == -1)
671 base1_alias_set = get_deref_alias_set (ptr1);
672 if (base1_alias_set == 0)
673 return true;
674 if (base2_alias_set == -1)
675 base2_alias_set = get_alias_set (base2);
676
677 /* If both references are through the same type, they do not alias
678 if the accesses do not overlap. This does extra disambiguation
679 for mixed/pointer accesses but requires strict aliasing. */
680 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
681 TREE_TYPE (base2)) == 1)
682 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
683
684 /* The only way to access a variable is through a pointer dereference
685 of the same alias set or a subset of it. */
686 if (base1_alias_set != base2_alias_set
687 && !alias_set_subset_of (base1_alias_set, base2_alias_set))
688 return false;
689
690 /* Do access-path based disambiguation. */
691 if (ref1 && ref2
692 && handled_component_p (ref1)
693 && handled_component_p (ref2))
a42ce2cc 694 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
695 offset1, max_size1,
696 ref2, TREE_TYPE (base2),
697 offset2, max_size2);
dd277d48 698
699 return true;
700}
701
702/* Return true if two indirect references based on *PTR1
077ad5ad 703 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
704 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
dd277d48 705 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
706 in which case they are computed on-demand. REF1 and REF2
707 if non-NULL are the complete memory reference trees. */
708
709static bool
710indirect_refs_may_alias_p (tree ref1, tree ptr1,
711 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
712 alias_set_type base1_alias_set,
713 tree ref2, tree ptr2,
714 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
715 alias_set_type base2_alias_set)
716{
717 /* If both bases are based on pointers they cannot alias if they may not
718 point to the same memory object or if they point to the same object
719 and the accesses do not overlap. */
720 if (operand_equal_p (ptr1, ptr2, 0))
721 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
722 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
723 return false;
724
725 /* Disambiguations that rely on strict aliasing rules follow. */
726 if (!flag_strict_aliasing)
727 return true;
728
729 /* If the alias set for a pointer access is zero all bets are off. */
730 if (base1_alias_set == -1)
731 base1_alias_set = get_deref_alias_set (ptr1);
732 if (base1_alias_set == 0)
733 return true;
734 if (base2_alias_set == -1)
735 base2_alias_set = get_deref_alias_set (ptr2);
736 if (base2_alias_set == 0)
737 return true;
738
739 /* If both references are through the same type, they do not alias
740 if the accesses do not overlap. This does extra disambiguation
741 for mixed/pointer accesses but requires strict aliasing. */
742 if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
743 TREE_TYPE (TREE_TYPE (ptr2))) == 1)
744 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
745
746 /* Do type-based disambiguation. */
747 if (base1_alias_set != base2_alias_set
748 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
749 return false;
750
751 /* Do access-path based disambiguation. */
752 if (ref1 && ref2
753 && handled_component_p (ref1)
754 && handled_component_p (ref2))
a42ce2cc 755 return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
756 offset1, max_size1,
757 ref2, TREE_TYPE (TREE_TYPE (ptr2)),
758 offset2, max_size2);
dd277d48 759
760 return true;
761}
762
763/* Return true, if the two memory references REF1 and REF2 may alias. */
764
3a443843 765bool
3918bd18 766refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
dd277d48 767{
768 tree base1, base2;
769 HOST_WIDE_INT offset1 = 0, offset2 = 0;
dd277d48 770 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
771 bool var1_p, var2_p, ind1_p, ind2_p;
2a3ebafa 772 alias_set_type set;
dd277d48 773
3918bd18 774 gcc_assert ((!ref1->ref
775 || SSA_VAR_P (ref1->ref)
776 || handled_component_p (ref1->ref)
777 || INDIRECT_REF_P (ref1->ref)
119147d1 778 || TREE_CODE (ref1->ref) == TARGET_MEM_REF
779 || TREE_CODE (ref1->ref) == CONST_DECL)
3918bd18 780 && (!ref2->ref
781 || SSA_VAR_P (ref2->ref)
782 || handled_component_p (ref2->ref)
783 || INDIRECT_REF_P (ref2->ref)
119147d1 784 || TREE_CODE (ref2->ref) == TARGET_MEM_REF
785 || TREE_CODE (ref2->ref) == CONST_DECL));
dd277d48 786
dd277d48 787 /* Decompose the references into their base objects and the access. */
3918bd18 788 base1 = ao_ref_base (ref1);
789 offset1 = ref1->offset;
3918bd18 790 max_size1 = ref1->max_size;
791 base2 = ao_ref_base (ref2);
792 offset2 = ref2->offset;
3918bd18 793 max_size2 = ref2->max_size;
dd277d48 794
795 /* We can end up with registers or constants as bases for example from
796 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
797 which is seen as a struct copy. */
798 if (TREE_CODE (base1) == SSA_NAME
dd277d48 799 || TREE_CODE (base2) == SSA_NAME
119147d1 800 || TREE_CODE (base1) == CONST_DECL
801 || TREE_CODE (base2) == CONST_DECL
9b973aa6 802 || is_gimple_min_invariant (base1)
f85fb819 803 || is_gimple_min_invariant (base2))
dd277d48 804 return false;
805
ceefbccc 806 /* We can end up refering to code via function decls. As we likely
807 do not properly track code aliases conservatively bail out. */
808 if (TREE_CODE (base1) == FUNCTION_DECL
809 || TREE_CODE (base2) == FUNCTION_DECL)
810 return true;
811
090a8c65 812 /* Defer to simple offset based disambiguation if we have
813 references based on two decls. Do this before defering to
814 TBAA to handle must-alias cases in conformance with the
815 GCC extension of allowing type-punning through unions. */
dd277d48 816 var1_p = SSA_VAR_P (base1);
817 var2_p = SSA_VAR_P (base2);
dd277d48 818 if (var1_p && var2_p)
819 return decl_refs_may_alias_p (base1, offset1, max_size1,
820 base2, offset2, max_size2);
090a8c65 821
822 /* First defer to TBAA if possible. */
2a3ebafa 823 if (tbaa_p
824 && flag_strict_aliasing
3918bd18 825 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
826 ao_ref_alias_set (ref2)))
090a8c65 827 return false;
828
829 /* If one reference is a TARGET_MEM_REF weird things are allowed. Still
830 TBAA disambiguation based on the access type is possible, so bail
831 out only after that check. */
3918bd18 832 if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
833 || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
090a8c65 834 return true;
835
836 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
837 ind1_p = INDIRECT_REF_P (base1);
838 ind2_p = INDIRECT_REF_P (base2);
2a3ebafa 839 set = tbaa_p ? -1 : 0;
090a8c65 840 if (var1_p && ind2_p)
3918bd18 841 return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
2a3ebafa 842 offset2, max_size2, set,
3918bd18 843 ref1->ref, base1,
2a3ebafa 844 offset1, max_size1, set);
dd277d48 845 else if (ind1_p && var2_p)
3918bd18 846 return indirect_ref_may_alias_decl_p (ref1->ref, TREE_OPERAND (base1, 0),
2a3ebafa 847 offset1, max_size1, set,
3918bd18 848 ref2->ref, base2,
2a3ebafa 849 offset2, max_size2, set);
dd277d48 850 else if (ind1_p && ind2_p)
3918bd18 851 return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
2a3ebafa 852 offset1, max_size1, set,
3918bd18 853 ref2->ref, TREE_OPERAND (base2, 0),
2a3ebafa 854 offset2, max_size2, set);
dd277d48 855
856 gcc_unreachable ();
857}
858
859bool
860refs_may_alias_p (tree ref1, tree ref2)
861{
3918bd18 862 ao_ref r1, r2;
863 bool res;
864 ao_ref_init (&r1, ref1);
865 ao_ref_init (&r2, ref2);
866 res = refs_may_alias_p_1 (&r1, &r2, true);
dd277d48 867 if (res)
868 ++alias_stats.refs_may_alias_p_may_alias;
869 else
870 ++alias_stats.refs_may_alias_p_no_alias;
871 return res;
872}
873
2a3ebafa 874/* Returns true if there is a anti-dependence for the STORE that
875 executes after the LOAD. */
876
877bool
878refs_anti_dependent_p (tree load, tree store)
879{
3918bd18 880 ao_ref r1, r2;
881 ao_ref_init (&r1, load);
882 ao_ref_init (&r2, store);
883 return refs_may_alias_p_1 (&r1, &r2, false);
2a3ebafa 884}
885
886/* Returns true if there is a output dependence for the stores
887 STORE1 and STORE2. */
888
889bool
890refs_output_dependent_p (tree store1, tree store2)
891{
3918bd18 892 ao_ref r1, r2;
893 ao_ref_init (&r1, store1);
894 ao_ref_init (&r2, store2);
895 return refs_may_alias_p_1 (&r1, &r2, false);
2a3ebafa 896}
dd277d48 897
898/* If the call CALL may use the memory reference REF return true,
899 otherwise return false. */
900
901static bool
134899ac 902ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
dd277d48 903{
047fdd47 904 tree base, callee;
dd277d48 905 unsigned i;
906 int flags = gimple_call_flags (call);
907
908 /* Const functions without a static chain do not implicitly use memory. */
909 if (!gimple_call_chain (call)
910 && (flags & (ECF_CONST|ECF_NOVOPS)))
911 goto process_args;
912
134899ac 913 base = ao_ref_base (ref);
917f08fa 914 if (!base)
dd277d48 915 return true;
916
09347e88 917 /* If the reference is based on a decl that is not aliased the call
918 cannot possibly use it. */
919 if (DECL_P (base)
920 && !may_be_aliased (base)
7f7f16d4 921 /* But local statics can be used through recursion. */
922 && !is_global_var (base))
09347e88 923 goto process_args;
924
047fdd47 925 callee = gimple_call_fndecl (call);
926
927 /* Handle those builtin functions explicitly that do not act as
928 escape points. See tree-ssa-structalias.c:find_func_aliases
929 for the list of builtins we might need to handle here. */
930 if (callee != NULL_TREE
931 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
932 switch (DECL_FUNCTION_CODE (callee))
933 {
934 /* All the following functions clobber memory pointed to by
935 their first argument. */
936 case BUILT_IN_STRCPY:
937 case BUILT_IN_STRNCPY:
047fdd47 938 case BUILT_IN_MEMCPY:
939 case BUILT_IN_MEMMOVE:
940 case BUILT_IN_MEMPCPY:
941 case BUILT_IN_STPCPY:
942 case BUILT_IN_STPNCPY:
943 case BUILT_IN_STRCAT:
944 case BUILT_IN_STRNCAT:
945 {
134899ac 946 ao_ref dref;
947 tree size = NULL_TREE;
948 if (gimple_call_num_args (call) == 3)
949 size = gimple_call_arg (call, 2);
950 ao_ref_init_from_ptr_and_size (&dref,
951 gimple_call_arg (call, 1),
952 size);
953 return refs_may_alias_p_1 (&dref, ref, false);
047fdd47 954 }
119147d1 955 case BUILT_IN_BCOPY:
956 {
957 ao_ref dref;
958 tree size = gimple_call_arg (call, 2);
959 ao_ref_init_from_ptr_and_size (&dref,
960 gimple_call_arg (call, 0),
961 size);
962 return refs_may_alias_p_1 (&dref, ref, false);
963 }
09d9c774 964 /* The following builtins do not read from memory. */
965 case BUILT_IN_FREE:
966 case BUILT_IN_MEMSET:
967 case BUILT_IN_FREXP:
968 case BUILT_IN_FREXPF:
969 case BUILT_IN_FREXPL:
970 case BUILT_IN_GAMMA_R:
971 case BUILT_IN_GAMMAF_R:
972 case BUILT_IN_GAMMAL_R:
973 case BUILT_IN_LGAMMA_R:
974 case BUILT_IN_LGAMMAF_R:
975 case BUILT_IN_LGAMMAL_R:
976 case BUILT_IN_MODF:
977 case BUILT_IN_MODFF:
978 case BUILT_IN_MODFL:
979 case BUILT_IN_REMQUO:
980 case BUILT_IN_REMQUOF:
981 case BUILT_IN_REMQUOL:
982 case BUILT_IN_SINCOS:
983 case BUILT_IN_SINCOSF:
984 case BUILT_IN_SINCOSL:
985 return false;
986
047fdd47 987 default:
988 /* Fallthru to general call handling. */;
989 }
990
dd277d48 991 /* Check if base is a global static variable that is not read
992 by the function. */
993 if (TREE_CODE (base) == VAR_DECL
994 && TREE_STATIC (base)
995 && !TREE_PUBLIC (base))
4ee9c684 996 {
dd277d48 997 bitmap not_read;
998
999 if (callee != NULL_TREE
1000 && (not_read
1001 = ipa_reference_get_not_read_global (cgraph_node (callee)))
1002 && bitmap_bit_p (not_read, DECL_UID (base)))
1003 goto process_args;
4ee9c684 1004 }
1005
dd277d48 1006 /* If the base variable is call-used or call-clobbered then
1007 it may be used. */
1008 if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
4ee9c684 1009 {
917f08fa 1010 if (DECL_P (base))
1011 {
1012 if (is_call_used (base))
1013 return true;
1014 }
1015 else if (INDIRECT_REF_P (base)
1016 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1017 {
1018 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1019 if (!pi)
1020 return true;
1021
1022 if (pt_solution_includes_global (&pi->pt)
1023 || pt_solutions_intersect (&cfun->gimple_df->callused, &pi->pt)
1024 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
1025 return true;
1026 }
1027 else
dd277d48 1028 return true;
1029 }
1030 else
1031 {
917f08fa 1032 if (DECL_P (base))
1033 {
1034 if (is_call_clobbered (base))
1035 return true;
1036 }
1037 else if (INDIRECT_REF_P (base)
1038 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1039 {
1040 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1041 if (!pi)
1042 return true;
1043
1044 if (pt_solution_includes_global (&pi->pt)
1045 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
1046 return true;
1047 }
1048 else
dd277d48 1049 return true;
1050 }
1051
1052 /* Inspect call arguments for passed-by-value aliases. */
1053process_args:
1054 for (i = 0; i < gimple_call_num_args (call); ++i)
1055 {
1056 tree op = gimple_call_arg (call, i);
1057
dd277d48 1058 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1059 op = TREE_OPERAND (op, 0);
4ee9c684 1060
dd277d48 1061 if (TREE_CODE (op) != SSA_NAME
134899ac 1062 && !is_gimple_min_invariant (op))
1063 {
1064 ao_ref r;
1065 ao_ref_init (&r, op);
1066 if (refs_may_alias_p_1 (&r, ref, true))
1067 return true;
1068 }
4ee9c684 1069 }
1070
dd277d48 1071 return false;
1072}
1073
1074static bool
1075ref_maybe_used_by_call_p (gimple call, tree ref)
1076{
134899ac 1077 ao_ref r;
1078 bool res;
1079 ao_ref_init (&r, ref);
1080 res = ref_maybe_used_by_call_p_1 (call, &r);
dd277d48 1081 if (res)
1082 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1083 else
1084 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1085 return res;
4ee9c684 1086}
1087
1088
dd277d48 1089/* If the statement STMT may use the memory reference REF return
1090 true, otherwise return false. */
4ee9c684 1091
dd277d48 1092bool
1093ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
4ee9c684 1094{
dd277d48 1095 if (is_gimple_assign (stmt))
1096 {
1097 tree rhs;
1098
1099 /* All memory assign statements are single. */
1100 if (!gimple_assign_single_p (stmt))
1101 return false;
1102
1103 rhs = gimple_assign_rhs1 (stmt);
1104 if (is_gimple_reg (rhs)
1105 || is_gimple_min_invariant (rhs)
1106 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1107 return false;
1108
1109 return refs_may_alias_p (rhs, ref);
1110 }
1111 else if (is_gimple_call (stmt))
1112 return ref_maybe_used_by_call_p (stmt, ref);
1113
1114 return true;
4ee9c684 1115}
1116
dd277d48 1117/* If the call in statement CALL may clobber the memory reference REF
1118 return true, otherwise return false. */
4ee9c684 1119
dd277d48 1120static bool
3918bd18 1121call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
4ee9c684 1122{
7f7f16d4 1123 tree base;
047fdd47 1124 tree callee;
dd277d48 1125
1126 /* If the call is pure or const it cannot clobber anything. */
1127 if (gimple_call_flags (call)
1128 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1129 return false;
1130
3918bd18 1131 base = ao_ref_base (ref);
dd277d48 1132 if (!base)
1133 return true;
1134
1135 if (TREE_CODE (base) == SSA_NAME
1136 || CONSTANT_CLASS_P (base))
1137 return false;
1138
09347e88 1139 /* If the reference is based on a decl that is not aliased the call
1140 cannot possibly clobber it. */
1141 if (DECL_P (base)
1142 && !may_be_aliased (base)
7f7f16d4 1143 /* But local non-readonly statics can be modified through recursion
1144 or the call may implement a threading barrier which we must
1145 treat as may-def. */
09347e88 1146 && (TREE_READONLY (base)
7f7f16d4 1147 || !is_global_var (base)))
09347e88 1148 return false;
1149
047fdd47 1150 callee = gimple_call_fndecl (call);
1151
1152 /* Handle those builtin functions explicitly that do not act as
1153 escape points. See tree-ssa-structalias.c:find_func_aliases
1154 for the list of builtins we might need to handle here. */
1155 if (callee != NULL_TREE
1156 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1157 switch (DECL_FUNCTION_CODE (callee))
1158 {
1159 /* All the following functions clobber memory pointed to by
1160 their first argument. */
1161 case BUILT_IN_STRCPY:
1162 case BUILT_IN_STRNCPY:
047fdd47 1163 case BUILT_IN_MEMCPY:
1164 case BUILT_IN_MEMMOVE:
1165 case BUILT_IN_MEMPCPY:
1166 case BUILT_IN_STPCPY:
1167 case BUILT_IN_STPNCPY:
1168 case BUILT_IN_STRCAT:
1169 case BUILT_IN_STRNCAT:
134899ac 1170 case BUILT_IN_MEMSET:
047fdd47 1171 {
134899ac 1172 ao_ref dref;
1173 tree size = NULL_TREE;
1174 if (gimple_call_num_args (call) == 3)
1175 size = gimple_call_arg (call, 2);
1176 ao_ref_init_from_ptr_and_size (&dref,
1177 gimple_call_arg (call, 0),
1178 size);
1179 return refs_may_alias_p_1 (&dref, ref, false);
047fdd47 1180 }
119147d1 1181 case BUILT_IN_BCOPY:
1182 {
1183 ao_ref dref;
1184 tree size = gimple_call_arg (call, 2);
1185 ao_ref_init_from_ptr_and_size (&dref,
1186 gimple_call_arg (call, 1),
1187 size);
1188 return refs_may_alias_p_1 (&dref, ref, false);
1189 }
047fdd47 1190 /* Freeing memory kills the pointed-to memory. More importantly
1191 the call has to serve as a barrier for moving loads and stores
134899ac 1192 across it. */
047fdd47 1193 case BUILT_IN_FREE:
047fdd47 1194 {
1195 tree ptr = gimple_call_arg (call, 0);
1196 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1197 }
047fdd47 1198 case BUILT_IN_GAMMA_R:
1199 case BUILT_IN_GAMMAF_R:
1200 case BUILT_IN_GAMMAL_R:
1201 case BUILT_IN_LGAMMA_R:
1202 case BUILT_IN_LGAMMAF_R:
1203 case BUILT_IN_LGAMMAL_R:
09d9c774 1204 {
1205 tree out = gimple_call_arg (call, 1);
1206 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1207 return true;
1208 if (flag_errno_math)
1209 break;
1210 return false;
1211 }
1212 case BUILT_IN_FREXP:
1213 case BUILT_IN_FREXPF:
1214 case BUILT_IN_FREXPL:
047fdd47 1215 case BUILT_IN_MODF:
1216 case BUILT_IN_MODFF:
1217 case BUILT_IN_MODFL:
1218 {
1219 tree out = gimple_call_arg (call, 1);
1220 return ptr_deref_may_alias_ref_p_1 (out, ref);
1221 }
1222 case BUILT_IN_REMQUO:
1223 case BUILT_IN_REMQUOF:
1224 case BUILT_IN_REMQUOL:
1225 {
1226 tree out = gimple_call_arg (call, 2);
09d9c774 1227 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1228 return true;
1229 if (flag_errno_math)
1230 break;
1231 return false;
047fdd47 1232 }
1233 case BUILT_IN_SINCOS:
1234 case BUILT_IN_SINCOSF:
1235 case BUILT_IN_SINCOSL:
1236 {
1237 tree sin = gimple_call_arg (call, 1);
1238 tree cos = gimple_call_arg (call, 2);
1239 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1240 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1241 }
1242 default:
1243 /* Fallthru to general call handling. */;
1244 }
1245
dd277d48 1246 /* Check if base is a global static variable that is not written
1247 by the function. */
047fdd47 1248 if (callee != NULL_TREE
1249 && TREE_CODE (base) == VAR_DECL
dd277d48 1250 && TREE_STATIC (base)
1251 && !TREE_PUBLIC (base))
4ee9c684 1252 {
dd277d48 1253 bitmap not_written;
0eaf9bd7 1254
047fdd47 1255 if ((not_written
1256 = ipa_reference_get_not_written_global (cgraph_node (callee)))
dd277d48 1257 && bitmap_bit_p (not_written, DECL_UID (base)))
1258 return false;
4ee9c684 1259 }
4ee9c684 1260
dd277d48 1261 if (DECL_P (base))
1262 return is_call_clobbered (base);
917f08fa 1263 else if (INDIRECT_REF_P (base)
1264 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1265 {
1266 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1267 if (!pi)
1268 return true;
1269
1270 return (pt_solution_includes_global (&pi->pt)
1271 || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt));
1272 }
4ee9c684 1273
dd277d48 1274 return true;
1275}
4ee9c684 1276
3918bd18 1277static bool ATTRIBUTE_UNUSED
dd277d48 1278call_may_clobber_ref_p (gimple call, tree ref)
4ee9c684 1279{
3918bd18 1280 bool res;
1281 ao_ref r;
1282 ao_ref_init (&r, ref);
1283 res = call_may_clobber_ref_p_1 (call, &r);
dd277d48 1284 if (res)
1285 ++alias_stats.call_may_clobber_ref_p_may_alias;
1286 else
1287 ++alias_stats.call_may_clobber_ref_p_no_alias;
1288 return res;
4ee9c684 1289}
94e6573f 1290
dd277d48 1291
1292/* If the statement STMT may clobber the memory reference REF return true,
1293 otherwise return false. */
94e6573f 1294
1295bool
3918bd18 1296stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
94e6573f 1297{
dd277d48 1298 if (is_gimple_call (stmt))
1299 {
1300 tree lhs = gimple_call_lhs (stmt);
1301 if (lhs
3918bd18 1302 && !is_gimple_reg (lhs))
1303 {
1304 ao_ref r;
1305 ao_ref_init (&r, lhs);
1306 if (refs_may_alias_p_1 (ref, &r, true))
1307 return true;
1308 }
94e6573f 1309
3918bd18 1310 return call_may_clobber_ref_p_1 (stmt, ref);
dd277d48 1311 }
1312 else if (is_gimple_assign (stmt))
3918bd18 1313 {
1314 ao_ref r;
1315 ao_ref_init (&r, gimple_assign_lhs (stmt));
1316 return refs_may_alias_p_1 (ref, &r, true);
1317 }
dd277d48 1318 else if (gimple_code (stmt) == GIMPLE_ASM)
94e6573f 1319 return true;
1320
6329636b 1321 return false;
94e6573f 1322}
2be14d8b 1323
3918bd18 1324bool
1325stmt_may_clobber_ref_p (gimple stmt, tree ref)
1326{
1327 ao_ref r;
1328 ao_ref_init (&r, ref);
1329 return stmt_may_clobber_ref_p_1 (stmt, &r);
1330}
1331
1332
dd277d48 1333/* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1334 TARGET or a statement clobbering the memory reference REF in which
1335 case false is returned. The walk starts with VUSE, one argument of PHI. */
1336
1337static bool
3918bd18 1338maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1339 tree vuse, bitmap *visited)
ac926412 1340{
dd277d48 1341 if (!*visited)
1342 *visited = BITMAP_ALLOC (NULL);
ac926412 1343
dd277d48 1344 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1345
1346 /* Walk until we hit the target. */
1347 while (vuse != target)
ac926412 1348 {
dd277d48 1349 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1350 /* Recurse for PHI nodes. */
1351 if (gimple_code (def_stmt) == GIMPLE_PHI)
1352 {
1353 /* An already visited PHI node ends the walk successfully. */
1354 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1355 return true;
1356 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1357 if (!vuse)
1358 return false;
1359 continue;
1360 }
1361 /* A clobbering statement or the end of the IL ends it failing. */
1362 else if (gimple_nop_p (def_stmt)
3918bd18 1363 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
dd277d48 1364 return false;
1365 vuse = gimple_vuse (def_stmt);
ac926412 1366 }
dd277d48 1367 return true;
1368}
ac926412 1369
dd277d48 1370/* Starting from a PHI node for the virtual operand of the memory reference
1371 REF find a continuation virtual operand that allows to continue walking
1372 statements dominating PHI skipping only statements that cannot possibly
1373 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1374 be found. */
1375
7814d418 1376tree
3918bd18 1377get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
dd277d48 1378{
1379 unsigned nargs = gimple_phi_num_args (phi);
1380
1381 /* Through a single-argument PHI we can simply look through. */
1382 if (nargs == 1)
1383 return PHI_ARG_DEF (phi, 0);
1384
1385 /* For two arguments try to skip non-aliasing code until we hit
1386 the phi argument definition that dominates the other one. */
1387 if (nargs == 2)
ac926412 1388 {
dd277d48 1389 tree arg0 = PHI_ARG_DEF (phi, 0);
1390 tree arg1 = PHI_ARG_DEF (phi, 1);
1391 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1392 gimple def1 = SSA_NAME_DEF_STMT (arg1);
7814d418 1393 tree common_vuse;
dd277d48 1394
1395 if (arg0 == arg1)
1396 return arg0;
1397 else if (gimple_nop_p (def0)
1398 || (!gimple_nop_p (def1)
1399 && dominated_by_p (CDI_DOMINATORS,
1400 gimple_bb (def1), gimple_bb (def0))))
1401 {
1402 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1403 return arg0;
1404 }
1405 else if (gimple_nop_p (def1)
1406 || dominated_by_p (CDI_DOMINATORS,
1407 gimple_bb (def0), gimple_bb (def1)))
1408 {
1409 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1410 return arg1;
1411 }
7814d418 1412 /* Special case of a diamond:
1413 MEM_1 = ...
1414 goto (cond) ? L1 : L2
1415 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1416 goto L3
1417 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1418 L3: MEM_4 = PHI<MEM_2, MEM_3>
1419 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1420 dominate each other, but still we can easily skip this PHI node
1421 if we recognize that the vuse MEM operand is the same for both,
1422 and that we can skip both statements (they don't clobber us).
1423 This is still linear. Don't use maybe_skip_until, that might
1424 potentially be slow. */
1425 else if ((common_vuse = gimple_vuse (def0))
1426 && common_vuse == gimple_vuse (def1))
1427 {
1428 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1429 && !stmt_may_clobber_ref_p_1 (def1, ref))
1430 return common_vuse;
1431 }
ac926412 1432 }
dd277d48 1433
1434 return NULL_TREE;
ac926412 1435}
516849c7 1436
dd277d48 1437/* Based on the memory reference REF and its virtual use VUSE call
1438 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1439 itself. That is, for each virtual use for which its defining statement
1440 does not clobber REF.
e683de6d 1441
dd277d48 1442 WALKER is called with REF, the current virtual use and DATA. If
1443 WALKER returns non-NULL the walk stops and its result is returned.
1444 At the end of a non-successful walk NULL is returned.
3857274c 1445
d8021dea 1446 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1447 use which definition is a statement that may clobber REF and DATA.
1448 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1449 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1450 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1451 to adjust REF and *DATA to make that valid.
1452
dd277d48 1453 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1454
1455void *
3918bd18 1456walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1457 void *(*walker)(ao_ref *, tree, void *),
1458 void *(*translate)(ao_ref *, tree, void *), void *data)
3857274c 1459{
dd277d48 1460 bitmap visited = NULL;
1461 void *res;
1462
02c74b9b 1463 timevar_push (TV_ALIAS_STMT_WALK);
1464
dd277d48 1465 do
1466 {
1467 gimple def_stmt;
3857274c 1468
02c74b9b 1469 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
dd277d48 1470 res = (*walker) (ref, vuse, data);
1471 if (res)
1472 break;
3857274c 1473
dd277d48 1474 def_stmt = SSA_NAME_DEF_STMT (vuse);
1475 if (gimple_nop_p (def_stmt))
1476 break;
1477 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1478 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1479 else
1480 {
3918bd18 1481 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
d8021dea 1482 {
1483 if (!translate)
1484 break;
3918bd18 1485 res = (*translate) (ref, vuse, data);
d8021dea 1486 /* Failed lookup and translation. */
1487 if (res == (void *)-1)
1488 {
1489 res = NULL;
1490 break;
1491 }
1492 /* Lookup succeeded. */
1493 else if (res != NULL)
1494 break;
1495 /* Translation succeeded, continue walking. */
1496 }
dd277d48 1497 vuse = gimple_vuse (def_stmt);
1498 }
1499 }
1500 while (vuse);
ac926412 1501
dd277d48 1502 if (visited)
1503 BITMAP_FREE (visited);
ac926412 1504
02c74b9b 1505 timevar_pop (TV_ALIAS_STMT_WALK);
1506
dd277d48 1507 return res;
3857274c 1508}
1509
604eef2c 1510
dd277d48 1511/* Based on the memory reference REF call WALKER for each vdef which
1512 defining statement may clobber REF, starting with VDEF. If REF
1513 is NULL_TREE, each defining statement is visited.
1514
1515 WALKER is called with REF, the current vdef and DATA. If WALKER
1516 returns true the walk is stopped, otherwise it continues.
1517
1518 At PHI nodes walk_aliased_vdefs forks into one walk for reach
1519 PHI argument (but only one walk continues on merge points), the
1520 return value is true if any of the walks was successful.
1521
1522 The function returns the number of statements walked. */
604eef2c 1523
1524static unsigned int
1daead67 1525walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1526 bool (*walker)(ao_ref *, tree, void *), void *data,
dd277d48 1527 bitmap *visited, unsigned int cnt)
604eef2c 1528{
dd277d48 1529 do
1530 {
1531 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
604eef2c 1532
dd277d48 1533 if (*visited
1534 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1535 return cnt;
1536
1537 if (gimple_nop_p (def_stmt))
1538 return cnt;
1539 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1540 {
1541 unsigned i;
1542 if (!*visited)
1543 *visited = BITMAP_ALLOC (NULL);
1544 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1545 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1546 walker, data, visited, 0);
1547 return cnt;
1548 }
1549
02c74b9b 1550 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
dd277d48 1551 cnt++;
1552 if ((!ref
1daead67 1553 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
dd277d48 1554 && (*walker) (ref, vdef, data))
1555 return cnt;
1556
1557 vdef = gimple_vuse (def_stmt);
1558 }
1559 while (1);
604eef2c 1560}
1561
dd277d48 1562unsigned int
1daead67 1563walk_aliased_vdefs (ao_ref *ref, tree vdef,
1564 bool (*walker)(ao_ref *, tree, void *), void *data,
dd277d48 1565 bitmap *visited)
df3f0669 1566{
dd277d48 1567 bitmap local_visited = NULL;
1568 unsigned int ret;
1569
02c74b9b 1570 timevar_push (TV_ALIAS_STMT_WALK);
1571
dd277d48 1572 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1573 visited ? visited : &local_visited, 0);
1574 if (local_visited)
1575 BITMAP_FREE (local_visited);
1576
02c74b9b 1577 timevar_pop (TV_ALIAS_STMT_WALK);
1578
dd277d48 1579 return ret;
1580}
1581