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