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