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