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