tree-ssa-alias.c (same_tmr_indexing_p): Break out from ...
[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 /* FIELD1 and FIELD2 are two fields of component refs. We assume
1132 that bases of both component refs are either equivalent or nonoverlapping.
1133 We do not assume that the containers of FIELD1 and FIELD2 are of the
1134 same type or size.
1135
1136 Return 0 in case the base address of component_refs are same then
1137 FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1138 may not be of same type or size.
1139
1140 Return 1 if FIELD1 and FIELD2 are non-overlapping.
1141
1142 Return -1 otherwise.
1143
1144 Main difference between 0 and -1 is to let
1145 nonoverlapping_component_refs_since_match_p discover the semantically
1146 equivalent part of the access path.
1147
1148 Note that this function is used even with -fno-strict-aliasing
1149 and makes use of no TBAA assumptions. */
1150
1151 static int
1152 nonoverlapping_component_refs_p_1 (const_tree field1, const_tree field2)
1153 {
1154 /* If both fields are of the same type, we could save hard work of
1155 comparing offsets. */
1156 tree type1 = DECL_CONTEXT (field1);
1157 tree type2 = DECL_CONTEXT (field2);
1158
1159 if (TREE_CODE (type1) == RECORD_TYPE
1160 && DECL_BIT_FIELD_REPRESENTATIVE (field1))
1161 field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
1162 if (TREE_CODE (type2) == RECORD_TYPE
1163 && DECL_BIT_FIELD_REPRESENTATIVE (field2))
1164 field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
1165
1166 /* ??? Bitfields can overlap at RTL level so punt on them.
1167 FIXME: RTL expansion should be fixed by adjusting the access path
1168 when producing MEM_ATTRs for MEMs which are wider than
1169 the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1170 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1171 return -1;
1172
1173 /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1174 if (type1 == type2 && TREE_CODE (type1) == RECORD_TYPE)
1175 return field1 != field2;
1176
1177 /* In common case the offsets and bit offsets will be the same.
1178 However if frontends do not agree on the alignment, they may be
1179 different even if they actually represent same address.
1180 Try the common case first and if that fails calcualte the
1181 actual bit offset. */
1182 if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1),
1183 DECL_FIELD_OFFSET (field2))
1184 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1),
1185 DECL_FIELD_BIT_OFFSET (field2)))
1186 return 0;
1187
1188 /* Note that it may be possible to use component_ref_field_offset
1189 which would provide offsets as trees. However constructing and folding
1190 trees is expensive and does not seem to be worth the compile time
1191 cost. */
1192
1193 poly_uint64 offset1, offset2;
1194 poly_uint64 bit_offset1, bit_offset2;
1195
1196 if (poly_int_tree_p (DECL_FIELD_OFFSET (field1), &offset1)
1197 && poly_int_tree_p (DECL_FIELD_OFFSET (field2), &offset2)
1198 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1), &bit_offset1)
1199 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2), &bit_offset2))
1200 {
1201 offset1 = (offset1 << LOG2_BITS_PER_UNIT) + bit_offset1;
1202 offset2 = (offset2 << LOG2_BITS_PER_UNIT) + bit_offset2;
1203
1204 if (known_eq (offset1, offset2))
1205 return 0;
1206
1207 poly_uint64 size1, size2;
1208
1209 if (poly_int_tree_p (DECL_SIZE (field1), &size1)
1210 && poly_int_tree_p (DECL_SIZE (field2), &size2)
1211 && !ranges_maybe_overlap_p (offset1, size1, offset2, size2))
1212 return 1;
1213 }
1214 /* Resort to slower overlap checking by looking for matching types in
1215 the middle of access path. */
1216 return -1;
1217 }
1218
1219 /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1220 MATCH2 either point to the same address or are disjoint.
1221 MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1222 respectively or NULL in the case we established equivalence of bases.
1223
1224 This test works by matching the initial segment of the access path
1225 and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1226 match was determined without use of TBAA oracle.
1227
1228 Return 1 if we can determine that component references REF1 and REF2,
1229 that are within a common DECL, cannot overlap.
1230
1231 Return 0 if paths are same and thus there is nothing to disambiguate more
1232 (i.e. there is must alias assuming there is must alias between MATCH1 and
1233 MATCH2)
1234
1235 Return -1 if we can not determine 0 or 1 - this happens when we met
1236 non-matching types was met in the path.
1237 In this case it may make sense to continue by other disambiguation
1238 oracles. */
1239
1240 static int
1241 nonoverlapping_component_refs_since_match_p (tree match1, tree ref1,
1242 tree match2, tree ref2)
1243 {
1244 /* Early return if there are no references to match, we do not need
1245 to walk the access paths.
1246
1247 Do not consider this as may-alias for stats - it is more useful
1248 to have information how many disambiguations happened provided that
1249 the query was meaningful. */
1250
1251 if (match1 == ref1 || !handled_component_p (ref1)
1252 || match2 == ref2 || !handled_component_p (ref2))
1253 return -1;
1254
1255 auto_vec<tree, 16> component_refs1;
1256 auto_vec<tree, 16> component_refs2;
1257
1258 /* Create the stack of handled components for REF1. */
1259 while (handled_component_p (ref1) && ref1 != match1)
1260 {
1261 if (TREE_CODE (ref1) == VIEW_CONVERT_EXPR
1262 || TREE_CODE (ref1) == BIT_FIELD_REF)
1263 component_refs1.truncate (0);
1264 else
1265 component_refs1.safe_push (ref1);
1266 ref1 = TREE_OPERAND (ref1, 0);
1267 }
1268
1269 /* Create the stack of handled components for REF2. */
1270 while (handled_component_p (ref2) && ref2 != match2)
1271 {
1272 if (TREE_CODE (ref2) == VIEW_CONVERT_EXPR
1273 || TREE_CODE (ref2) == BIT_FIELD_REF)
1274 component_refs2.truncate (0);
1275 else
1276 component_refs2.safe_push (ref2);
1277 ref2 = TREE_OPERAND (ref2, 0);
1278 }
1279
1280 bool mem_ref1 = TREE_CODE (ref1) == MEM_REF && ref1 != match1;
1281 bool mem_ref2 = TREE_CODE (ref2) == MEM_REF && ref2 != match2;
1282
1283 /* If only one of access path starts with MEM_REF check that offset is 0
1284 so the addresses stays the same after stripping it.
1285 TODO: In this case we may walk the other access path until we get same
1286 offset.
1287
1288 If both starts with MEM_REF, offset has to be same. */
1289 if ((mem_ref1 && !mem_ref2 && !integer_zerop (TREE_OPERAND (ref1, 1)))
1290 || (mem_ref2 && !mem_ref1 && !integer_zerop (TREE_OPERAND (ref2, 1)))
1291 || (mem_ref1 && mem_ref2
1292 && !tree_int_cst_equal (TREE_OPERAND (ref1, 1),
1293 TREE_OPERAND (ref2, 1))))
1294 {
1295 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1296 return -1;
1297 }
1298
1299 /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1300 to handle them here at all. */
1301 gcc_checking_assert (TREE_CODE (ref1) != TARGET_MEM_REF
1302 && TREE_CODE (ref2) != TARGET_MEM_REF);
1303
1304 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1305 rank. This is sufficient because we start from the same DECL and you
1306 cannot reference several fields at a time with COMPONENT_REFs (unlike
1307 with ARRAY_RANGE_REFs for arrays) so you always need the same number
1308 of them to access a sub-component, unless you're in a union, in which
1309 case the return value will precisely be false. */
1310 while (true)
1311 {
1312 bool seen_noncomponent_ref_p = false;
1313 do
1314 {
1315 if (component_refs1.is_empty ())
1316 {
1317 ++alias_stats
1318 .nonoverlapping_component_refs_since_match_p_must_overlap;
1319 return 0;
1320 }
1321 ref1 = component_refs1.pop ();
1322 if (TREE_CODE (ref1) != COMPONENT_REF)
1323 seen_noncomponent_ref_p = true;
1324 }
1325 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1326
1327 do
1328 {
1329 if (component_refs2.is_empty ())
1330 {
1331 ++alias_stats
1332 .nonoverlapping_component_refs_since_match_p_must_overlap;
1333 return 0;
1334 }
1335 ref2 = component_refs2.pop ();
1336 if (TREE_CODE (ref2) != COMPONENT_REF)
1337 seen_noncomponent_ref_p = true;
1338 }
1339 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1340
1341 /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1342 earlier. */
1343 gcc_checking_assert (TREE_CODE (ref1) == COMPONENT_REF
1344 && TREE_CODE (ref2) == COMPONENT_REF);
1345
1346 tree field1 = TREE_OPERAND (ref1, 1);
1347 tree field2 = TREE_OPERAND (ref2, 1);
1348
1349 /* ??? We cannot simply use the type of operand #0 of the refs here
1350 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1351 for common blocks instead of using unions like everyone else. */
1352 tree type1 = DECL_CONTEXT (field1);
1353 tree type2 = DECL_CONTEXT (field2);
1354
1355 /* If we skipped array refs on type of different sizes, we can
1356 no longer be sure that there are not partial overlaps. */
1357 if (seen_noncomponent_ref_p
1358 && !operand_equal_p (TYPE_SIZE (type1), TYPE_SIZE (type2), 0))
1359 {
1360 ++alias_stats
1361 .nonoverlapping_component_refs_since_match_p_may_alias;
1362 return -1;
1363 }
1364
1365 int cmp = nonoverlapping_component_refs_p_1 (field1, field2);
1366 if (cmp == -1)
1367 {
1368 ++alias_stats
1369 .nonoverlapping_component_refs_since_match_p_may_alias;
1370 return -1;
1371 }
1372 else if (cmp == 1)
1373 {
1374 ++alias_stats
1375 .nonoverlapping_component_refs_since_match_p_no_alias;
1376 return 1;
1377 }
1378 }
1379
1380 ++alias_stats.nonoverlapping_component_refs_since_match_p_must_overlap;
1381 return 0;
1382 }
1383
1384 /* Return TYPE_UID which can be used to match record types we consider
1385 same for TBAA purposes. */
1386
1387 static inline int
1388 ncr_type_uid (const_tree field)
1389 {
1390 /* ??? We cannot simply use the type of operand #0 of the refs here
1391 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1392 for common blocks instead of using unions like everyone else. */
1393 tree type = DECL_FIELD_CONTEXT (field);
1394 /* With LTO types considered same_type_for_tbaa_p
1395 from different translation unit may not have same
1396 main variant. They however have same TYPE_CANONICAL. */
1397 if (TYPE_CANONICAL (type))
1398 return TYPE_UID (TYPE_CANONICAL (type));
1399 return TYPE_UID (type);
1400 }
1401
1402 /* qsort compare function to sort FIELD_DECLs after their
1403 DECL_FIELD_CONTEXT TYPE_UID. */
1404
1405 static inline int
1406 ncr_compar (const void *field1_, const void *field2_)
1407 {
1408 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1409 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1410 unsigned int uid1 = ncr_type_uid (field1);
1411 unsigned int uid2 = ncr_type_uid (field2);
1412
1413 if (uid1 < uid2)
1414 return -1;
1415 else if (uid1 > uid2)
1416 return 1;
1417 return 0;
1418 }
1419
1420 /* Return true if we can determine that the fields referenced cannot
1421 overlap for any pair of objects. This relies on TBAA. */
1422
1423 static bool
1424 nonoverlapping_component_refs_p (const_tree x, const_tree y)
1425 {
1426 /* Early return if we have nothing to do.
1427
1428 Do not consider this as may-alias for stats - it is more useful
1429 to have information how many disambiguations happened provided that
1430 the query was meaningful. */
1431 if (!flag_strict_aliasing
1432 || !x || !y
1433 || !handled_component_p (x)
1434 || !handled_component_p (y))
1435 return false;
1436
1437 auto_vec<const_tree, 16> fieldsx;
1438 while (handled_component_p (x))
1439 {
1440 if (TREE_CODE (x) == COMPONENT_REF)
1441 {
1442 tree field = TREE_OPERAND (x, 1);
1443 tree type = DECL_FIELD_CONTEXT (field);
1444 if (TREE_CODE (type) == RECORD_TYPE)
1445 fieldsx.safe_push (field);
1446 }
1447 else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
1448 || TREE_CODE (x) == BIT_FIELD_REF)
1449 fieldsx.truncate (0);
1450 x = TREE_OPERAND (x, 0);
1451 }
1452 if (fieldsx.length () == 0)
1453 return false;
1454 auto_vec<const_tree, 16> fieldsy;
1455 while (handled_component_p (y))
1456 {
1457 if (TREE_CODE (y) == COMPONENT_REF)
1458 {
1459 tree field = TREE_OPERAND (y, 1);
1460 tree type = DECL_FIELD_CONTEXT (field);
1461 if (TREE_CODE (type) == RECORD_TYPE)
1462 fieldsy.safe_push (TREE_OPERAND (y, 1));
1463 }
1464 else if (TREE_CODE (y) == VIEW_CONVERT_EXPR
1465 || TREE_CODE (y) == BIT_FIELD_REF)
1466 fieldsy.truncate (0);
1467 y = TREE_OPERAND (y, 0);
1468 }
1469 if (fieldsy.length () == 0)
1470 {
1471 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1472 return false;
1473 }
1474
1475 /* Most common case first. */
1476 if (fieldsx.length () == 1
1477 && fieldsy.length () == 1)
1478 {
1479 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx[0]),
1480 DECL_FIELD_CONTEXT (fieldsy[0])) == 1
1481 && nonoverlapping_component_refs_p_1 (fieldsx[0], fieldsy[0]) == 1)
1482 {
1483 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1484 return true;
1485 }
1486 else
1487 {
1488 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1489 return false;
1490 }
1491 }
1492
1493 if (fieldsx.length () == 2)
1494 {
1495 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1496 std::swap (fieldsx[0], fieldsx[1]);
1497 }
1498 else
1499 fieldsx.qsort (ncr_compar);
1500
1501 if (fieldsy.length () == 2)
1502 {
1503 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1504 std::swap (fieldsy[0], fieldsy[1]);
1505 }
1506 else
1507 fieldsy.qsort (ncr_compar);
1508
1509 unsigned i = 0, j = 0;
1510 do
1511 {
1512 const_tree fieldx = fieldsx[i];
1513 const_tree fieldy = fieldsy[j];
1514
1515 /* We're left with accessing different fields of a structure,
1516 no possible overlap. */
1517 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx),
1518 DECL_FIELD_CONTEXT (fieldy)) == 1
1519 && nonoverlapping_component_refs_p_1 (fieldx, fieldy) == 1)
1520 {
1521 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1522 return true;
1523 }
1524
1525 if (ncr_type_uid (fieldx) < ncr_type_uid (fieldy))
1526 {
1527 i++;
1528 if (i == fieldsx.length ())
1529 break;
1530 }
1531 else
1532 {
1533 j++;
1534 if (j == fieldsy.length ())
1535 break;
1536 }
1537 }
1538 while (1);
1539
1540 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1541 return false;
1542 }
1543
1544
1545 /* Return true if two memory references based on the variables BASE1
1546 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1547 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1548 if non-NULL are the complete memory reference trees. */
1549
1550 static bool
1551 decl_refs_may_alias_p (tree ref1, tree base1,
1552 poly_int64 offset1, poly_int64 max_size1,
1553 poly_int64 size1,
1554 tree ref2, tree base2,
1555 poly_int64 offset2, poly_int64 max_size2,
1556 poly_int64 size2)
1557 {
1558 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1559
1560 /* If both references are based on different variables, they cannot alias. */
1561 if (compare_base_decls (base1, base2) == 0)
1562 return false;
1563
1564 /* If both references are based on the same variable, they cannot alias if
1565 the accesses do not overlap. */
1566 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1567 return false;
1568
1569 /* If there is must alias, there is no use disambiguating further. */
1570 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1571 return true;
1572
1573 /* For components with variable position, the above test isn't sufficient,
1574 so we disambiguate component references manually. */
1575 if (ref1 && ref2
1576 && handled_component_p (ref1) && handled_component_p (ref2)
1577 && nonoverlapping_component_refs_since_match_p (NULL, ref1,
1578 NULL, ref2) == 1)
1579 return false;
1580
1581 return true;
1582 }
1583
1584 /* Return true if an indirect reference based on *PTR1 constrained
1585 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1586 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1587 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1588 in which case they are computed on-demand. REF1 and REF2
1589 if non-NULL are the complete memory reference trees. */
1590
1591 static bool
1592 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1593 poly_int64 offset1, poly_int64 max_size1,
1594 poly_int64 size1,
1595 alias_set_type ref1_alias_set,
1596 alias_set_type base1_alias_set,
1597 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1598 poly_int64 offset2, poly_int64 max_size2,
1599 poly_int64 size2,
1600 alias_set_type ref2_alias_set,
1601 alias_set_type base2_alias_set, bool tbaa_p)
1602 {
1603 tree ptr1;
1604 tree ptrtype1, dbase2;
1605
1606 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1607 || TREE_CODE (base1) == TARGET_MEM_REF)
1608 && DECL_P (base2));
1609
1610 ptr1 = TREE_OPERAND (base1, 0);
1611 poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1612
1613 /* If only one reference is based on a variable, they cannot alias if
1614 the pointer access is beyond the extent of the variable access.
1615 (the pointer base cannot validly point to an offset less than zero
1616 of the variable).
1617 ??? IVOPTs creates bases that do not honor this restriction,
1618 so do not apply this optimization for TARGET_MEM_REFs. */
1619 if (TREE_CODE (base1) != TARGET_MEM_REF
1620 && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
1621 return false;
1622 /* They also cannot alias if the pointer may not point to the decl. */
1623 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1624 return false;
1625
1626 /* Disambiguations that rely on strict aliasing rules follow. */
1627 if (!flag_strict_aliasing || !tbaa_p)
1628 return true;
1629
1630 /* If the alias set for a pointer access is zero all bets are off. */
1631 if (base1_alias_set == 0 || base2_alias_set == 0)
1632 return true;
1633
1634 /* When we are trying to disambiguate an access with a pointer dereference
1635 as base versus one with a decl as base we can use both the size
1636 of the decl and its dynamic type for extra disambiguation.
1637 ??? We do not know anything about the dynamic type of the decl
1638 other than that its alias-set contains base2_alias_set as a subset
1639 which does not help us here. */
1640 /* As we know nothing useful about the dynamic type of the decl just
1641 use the usual conflict check rather than a subset test.
1642 ??? We could introduce -fvery-strict-aliasing when the language
1643 does not allow decls to have a dynamic type that differs from their
1644 static type. Then we can check
1645 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1646 if (base1_alias_set != base2_alias_set
1647 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1648 return false;
1649
1650 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1651
1652 /* If the size of the access relevant for TBAA through the pointer
1653 is bigger than the size of the decl we can't possibly access the
1654 decl via that pointer. */
1655 if (/* ??? This in turn may run afoul when a decl of type T which is
1656 a member of union type U is accessed through a pointer to
1657 type U and sizeof T is smaller than sizeof U. */
1658 TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1659 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1660 && compare_sizes (DECL_SIZE (base2),
1661 TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
1662 return false;
1663
1664 if (!ref2)
1665 return true;
1666
1667 /* If the decl is accessed via a MEM_REF, reconstruct the base
1668 we can use for TBAA and an appropriately adjusted offset. */
1669 dbase2 = ref2;
1670 while (handled_component_p (dbase2))
1671 dbase2 = TREE_OPERAND (dbase2, 0);
1672 poly_int64 doffset1 = offset1;
1673 poly_offset_int doffset2 = offset2;
1674 if (TREE_CODE (dbase2) == MEM_REF
1675 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1676 {
1677 doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
1678 tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
1679 /* If second reference is view-converted, give up now. */
1680 if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
1681 return true;
1682 }
1683
1684 /* If first reference is view-converted, give up now. */
1685 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
1686 return true;
1687
1688 /* If both references are through the same type, they do not alias
1689 if the accesses do not overlap. This does extra disambiguation
1690 for mixed/pointer accesses but requires strict aliasing.
1691 For MEM_REFs we require that the component-ref offset we computed
1692 is relative to the start of the type which we ensure by
1693 comparing rvalue and access type and disregarding the constant
1694 pointer offset.
1695
1696 But avoid treating variable length arrays as "objects", instead assume they
1697 can overlap by an exact multiple of their element size.
1698 See gcc.dg/torture/alias-2.c. */
1699 if (((TREE_CODE (base1) != TARGET_MEM_REF
1700 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1701 && (TREE_CODE (dbase2) != TARGET_MEM_REF
1702 || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
1703 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1
1704 && (TREE_CODE (TREE_TYPE (base1)) != ARRAY_TYPE
1705 || (TYPE_SIZE (TREE_TYPE (base1))
1706 && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) == INTEGER_CST)))
1707 {
1708 if (!ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
1709 return false;
1710 if (!ref1 || !ref2
1711 /* If there is must alias, there is no use disambiguating further. */
1712 || (known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1713 return true;
1714 int res = nonoverlapping_component_refs_since_match_p (base1, ref1,
1715 base2, ref2);
1716 if (res == -1)
1717 return !nonoverlapping_component_refs_p (ref1, ref2);
1718 return !res;
1719 }
1720
1721 /* Do access-path based disambiguation. */
1722 if (ref1 && ref2
1723 && (handled_component_p (ref1) || handled_component_p (ref2)))
1724 return aliasing_component_refs_p (ref1,
1725 ref1_alias_set, base1_alias_set,
1726 offset1, max_size1,
1727 ref2,
1728 ref2_alias_set, base2_alias_set,
1729 offset2, max_size2);
1730
1731 return true;
1732 }
1733
1734 /* Return true if two indirect references based on *PTR1
1735 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1736 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1737 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1738 in which case they are computed on-demand. REF1 and REF2
1739 if non-NULL are the complete memory reference trees. */
1740
1741 static bool
1742 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1743 poly_int64 offset1, poly_int64 max_size1,
1744 poly_int64 size1,
1745 alias_set_type ref1_alias_set,
1746 alias_set_type base1_alias_set,
1747 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1748 poly_int64 offset2, poly_int64 max_size2,
1749 poly_int64 size2,
1750 alias_set_type ref2_alias_set,
1751 alias_set_type base2_alias_set, bool tbaa_p)
1752 {
1753 tree ptr1;
1754 tree ptr2;
1755 tree ptrtype1, ptrtype2;
1756
1757 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1758 || TREE_CODE (base1) == TARGET_MEM_REF)
1759 && (TREE_CODE (base2) == MEM_REF
1760 || TREE_CODE (base2) == TARGET_MEM_REF));
1761
1762 ptr1 = TREE_OPERAND (base1, 0);
1763 ptr2 = TREE_OPERAND (base2, 0);
1764
1765 /* If both bases are based on pointers they cannot alias if they may not
1766 point to the same memory object or if they point to the same object
1767 and the accesses do not overlap. */
1768 if ((!cfun || gimple_in_ssa_p (cfun))
1769 && operand_equal_p (ptr1, ptr2, 0)
1770 && (((TREE_CODE (base1) != TARGET_MEM_REF
1771 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1772 && (TREE_CODE (base2) != TARGET_MEM_REF
1773 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1774 || (TREE_CODE (base1) == TARGET_MEM_REF
1775 && TREE_CODE (base2) == TARGET_MEM_REF
1776 && (TMR_STEP (base1) == TMR_STEP (base2)
1777 || (TMR_STEP (base1) && TMR_STEP (base2)
1778 && operand_equal_p (TMR_STEP (base1),
1779 TMR_STEP (base2), 0)))
1780 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1781 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1782 && operand_equal_p (TMR_INDEX (base1),
1783 TMR_INDEX (base2), 0)))
1784 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1785 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1786 && operand_equal_p (TMR_INDEX2 (base1),
1787 TMR_INDEX2 (base2), 0))))))
1788 {
1789 poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1790 poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
1791 if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
1792 offset2 + moff2, max_size2))
1793 return false;
1794 /* If there is must alias, there is no use disambiguating further. */
1795 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1796 return true;
1797 if (ref1 && ref2)
1798 {
1799 int res = nonoverlapping_component_refs_since_match_p (NULL, ref1,
1800 NULL, ref2);
1801 if (res != -1)
1802 return !res;
1803 }
1804 }
1805 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1806 return false;
1807
1808 /* Disambiguations that rely on strict aliasing rules follow. */
1809 if (!flag_strict_aliasing || !tbaa_p)
1810 return true;
1811
1812 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1813 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1814
1815 /* If the alias set for a pointer access is zero all bets are off. */
1816 if (base1_alias_set == 0
1817 || base2_alias_set == 0)
1818 return true;
1819
1820 /* Do type-based disambiguation. */
1821 if (base1_alias_set != base2_alias_set
1822 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1823 return false;
1824
1825 /* If either reference is view-converted, give up now. */
1826 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1827 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1828 return true;
1829
1830 /* If both references are through the same type, they do not alias
1831 if the accesses do not overlap. This does extra disambiguation
1832 for mixed/pointer accesses but requires strict aliasing. */
1833 if ((TREE_CODE (base1) != TARGET_MEM_REF
1834 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1835 && (TREE_CODE (base2) != TARGET_MEM_REF
1836 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1837 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1838 TREE_TYPE (ptrtype2)) == 1
1839 /* But avoid treating arrays as "objects", instead assume they
1840 can overlap by an exact multiple of their element size.
1841 See gcc.dg/torture/alias-2.c. */
1842 && TREE_CODE (TREE_TYPE (ptrtype1)) != ARRAY_TYPE)
1843 {
1844 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1845 return false;
1846 if (!ref1 || !ref2
1847 || (known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1848 return true;
1849 int res = nonoverlapping_component_refs_since_match_p (base1, ref1,
1850 base2, ref2);
1851 if (res == -1)
1852 return !nonoverlapping_component_refs_p (ref1, ref2);
1853 return !res;
1854 }
1855
1856 /* Do access-path based disambiguation. */
1857 if (ref1 && ref2
1858 && (handled_component_p (ref1) || handled_component_p (ref2)))
1859 return aliasing_component_refs_p (ref1,
1860 ref1_alias_set, base1_alias_set,
1861 offset1, max_size1,
1862 ref2,
1863 ref2_alias_set, base2_alias_set,
1864 offset2, max_size2);
1865
1866 return true;
1867 }
1868
1869 /* Return true, if the two memory references REF1 and REF2 may alias. */
1870
1871 static bool
1872 refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1873 {
1874 tree base1, base2;
1875 poly_int64 offset1 = 0, offset2 = 0;
1876 poly_int64 max_size1 = -1, max_size2 = -1;
1877 bool var1_p, var2_p, ind1_p, ind2_p;
1878
1879 gcc_checking_assert ((!ref1->ref
1880 || TREE_CODE (ref1->ref) == SSA_NAME
1881 || DECL_P (ref1->ref)
1882 || TREE_CODE (ref1->ref) == STRING_CST
1883 || handled_component_p (ref1->ref)
1884 || TREE_CODE (ref1->ref) == MEM_REF
1885 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1886 && (!ref2->ref
1887 || TREE_CODE (ref2->ref) == SSA_NAME
1888 || DECL_P (ref2->ref)
1889 || TREE_CODE (ref2->ref) == STRING_CST
1890 || handled_component_p (ref2->ref)
1891 || TREE_CODE (ref2->ref) == MEM_REF
1892 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1893
1894 /* Decompose the references into their base objects and the access. */
1895 base1 = ao_ref_base (ref1);
1896 offset1 = ref1->offset;
1897 max_size1 = ref1->max_size;
1898 base2 = ao_ref_base (ref2);
1899 offset2 = ref2->offset;
1900 max_size2 = ref2->max_size;
1901
1902 /* We can end up with registers or constants as bases for example from
1903 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1904 which is seen as a struct copy. */
1905 if (TREE_CODE (base1) == SSA_NAME
1906 || TREE_CODE (base1) == CONST_DECL
1907 || TREE_CODE (base1) == CONSTRUCTOR
1908 || TREE_CODE (base1) == ADDR_EXPR
1909 || CONSTANT_CLASS_P (base1)
1910 || TREE_CODE (base2) == SSA_NAME
1911 || TREE_CODE (base2) == CONST_DECL
1912 || TREE_CODE (base2) == CONSTRUCTOR
1913 || TREE_CODE (base2) == ADDR_EXPR
1914 || CONSTANT_CLASS_P (base2))
1915 return false;
1916
1917 /* We can end up referring to code via function and label decls.
1918 As we likely do not properly track code aliases conservatively
1919 bail out. */
1920 if (TREE_CODE (base1) == FUNCTION_DECL
1921 || TREE_CODE (base1) == LABEL_DECL
1922 || TREE_CODE (base2) == FUNCTION_DECL
1923 || TREE_CODE (base2) == LABEL_DECL)
1924 return true;
1925
1926 /* Two volatile accesses always conflict. */
1927 if (ref1->volatile_p
1928 && ref2->volatile_p)
1929 return true;
1930
1931 /* Defer to simple offset based disambiguation if we have
1932 references based on two decls. Do this before defering to
1933 TBAA to handle must-alias cases in conformance with the
1934 GCC extension of allowing type-punning through unions. */
1935 var1_p = DECL_P (base1);
1936 var2_p = DECL_P (base2);
1937 if (var1_p && var2_p)
1938 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1939 ref1->size,
1940 ref2->ref, base2, offset2, max_size2,
1941 ref2->size);
1942
1943 /* Handle restrict based accesses.
1944 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1945 here. */
1946 tree rbase1 = base1;
1947 tree rbase2 = base2;
1948 if (var1_p)
1949 {
1950 rbase1 = ref1->ref;
1951 if (rbase1)
1952 while (handled_component_p (rbase1))
1953 rbase1 = TREE_OPERAND (rbase1, 0);
1954 }
1955 if (var2_p)
1956 {
1957 rbase2 = ref2->ref;
1958 if (rbase2)
1959 while (handled_component_p (rbase2))
1960 rbase2 = TREE_OPERAND (rbase2, 0);
1961 }
1962 if (rbase1 && rbase2
1963 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1964 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1965 /* If the accesses are in the same restrict clique... */
1966 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1967 /* But based on different pointers they do not alias. */
1968 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1969 return false;
1970
1971 ind1_p = (TREE_CODE (base1) == MEM_REF
1972 || TREE_CODE (base1) == TARGET_MEM_REF);
1973 ind2_p = (TREE_CODE (base2) == MEM_REF
1974 || TREE_CODE (base2) == TARGET_MEM_REF);
1975
1976 /* Canonicalize the pointer-vs-decl case. */
1977 if (ind1_p && var2_p)
1978 {
1979 std::swap (offset1, offset2);
1980 std::swap (max_size1, max_size2);
1981 std::swap (base1, base2);
1982 std::swap (ref1, ref2);
1983 var1_p = true;
1984 ind1_p = false;
1985 var2_p = false;
1986 ind2_p = true;
1987 }
1988
1989 /* First defer to TBAA if possible. */
1990 if (tbaa_p
1991 && flag_strict_aliasing
1992 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1993 ao_ref_alias_set (ref2)))
1994 return false;
1995
1996 /* If the reference is based on a pointer that points to memory
1997 that may not be written to then the other reference cannot possibly
1998 clobber it. */
1999 if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
2000 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
2001 || (ind1_p
2002 && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
2003 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
2004 return false;
2005
2006 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2007 if (var1_p && ind2_p)
2008 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
2009 offset2, max_size2, ref2->size,
2010 ao_ref_alias_set (ref2),
2011 ao_ref_base_alias_set (ref2),
2012 ref1->ref, base1,
2013 offset1, max_size1, ref1->size,
2014 ao_ref_alias_set (ref1),
2015 ao_ref_base_alias_set (ref1),
2016 tbaa_p);
2017 else if (ind1_p && ind2_p)
2018 return indirect_refs_may_alias_p (ref1->ref, base1,
2019 offset1, max_size1, ref1->size,
2020 ao_ref_alias_set (ref1),
2021 ao_ref_base_alias_set (ref1),
2022 ref2->ref, base2,
2023 offset2, max_size2, ref2->size,
2024 ao_ref_alias_set (ref2),
2025 ao_ref_base_alias_set (ref2),
2026 tbaa_p);
2027
2028 gcc_unreachable ();
2029 }
2030
2031 /* Return true, if the two memory references REF1 and REF2 may alias
2032 and update statistics. */
2033
2034 bool
2035 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2036 {
2037 bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
2038 if (res)
2039 ++alias_stats.refs_may_alias_p_may_alias;
2040 else
2041 ++alias_stats.refs_may_alias_p_no_alias;
2042 return res;
2043 }
2044
2045 static bool
2046 refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
2047 {
2048 ao_ref r1;
2049 ao_ref_init (&r1, ref1);
2050 return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
2051 }
2052
2053 bool
2054 refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
2055 {
2056 ao_ref r1, r2;
2057 ao_ref_init (&r1, ref1);
2058 ao_ref_init (&r2, ref2);
2059 return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
2060 }
2061
2062 /* Returns true if there is a anti-dependence for the STORE that
2063 executes after the LOAD. */
2064
2065 bool
2066 refs_anti_dependent_p (tree load, tree store)
2067 {
2068 ao_ref r1, r2;
2069 ao_ref_init (&r1, load);
2070 ao_ref_init (&r2, store);
2071 return refs_may_alias_p_1 (&r1, &r2, false);
2072 }
2073
2074 /* Returns true if there is a output dependence for the stores
2075 STORE1 and STORE2. */
2076
2077 bool
2078 refs_output_dependent_p (tree store1, tree store2)
2079 {
2080 ao_ref r1, r2;
2081 ao_ref_init (&r1, store1);
2082 ao_ref_init (&r2, store2);
2083 return refs_may_alias_p_1 (&r1, &r2, false);
2084 }
2085
2086 /* If the call CALL may use the memory reference REF return true,
2087 otherwise return false. */
2088
2089 static bool
2090 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
2091 {
2092 tree base, callee;
2093 unsigned i;
2094 int flags = gimple_call_flags (call);
2095
2096 /* Const functions without a static chain do not implicitly use memory. */
2097 if (!gimple_call_chain (call)
2098 && (flags & (ECF_CONST|ECF_NOVOPS)))
2099 goto process_args;
2100
2101 base = ao_ref_base (ref);
2102 if (!base)
2103 return true;
2104
2105 /* A call that is not without side-effects might involve volatile
2106 accesses and thus conflicts with all other volatile accesses. */
2107 if (ref->volatile_p)
2108 return true;
2109
2110 /* If the reference is based on a decl that is not aliased the call
2111 cannot possibly use it. */
2112 if (DECL_P (base)
2113 && !may_be_aliased (base)
2114 /* But local statics can be used through recursion. */
2115 && !is_global_var (base))
2116 goto process_args;
2117
2118 callee = gimple_call_fndecl (call);
2119
2120 /* Handle those builtin functions explicitly that do not act as
2121 escape points. See tree-ssa-structalias.c:find_func_aliases
2122 for the list of builtins we might need to handle here. */
2123 if (callee != NULL_TREE
2124 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2125 switch (DECL_FUNCTION_CODE (callee))
2126 {
2127 /* All the following functions read memory pointed to by
2128 their second argument. strcat/strncat additionally
2129 reads memory pointed to by the first argument. */
2130 case BUILT_IN_STRCAT:
2131 case BUILT_IN_STRNCAT:
2132 {
2133 ao_ref dref;
2134 ao_ref_init_from_ptr_and_size (&dref,
2135 gimple_call_arg (call, 0),
2136 NULL_TREE);
2137 if (refs_may_alias_p_1 (&dref, ref, false))
2138 return true;
2139 }
2140 /* FALLTHRU */
2141 case BUILT_IN_STRCPY:
2142 case BUILT_IN_STRNCPY:
2143 case BUILT_IN_MEMCPY:
2144 case BUILT_IN_MEMMOVE:
2145 case BUILT_IN_MEMPCPY:
2146 case BUILT_IN_STPCPY:
2147 case BUILT_IN_STPNCPY:
2148 case BUILT_IN_TM_MEMCPY:
2149 case BUILT_IN_TM_MEMMOVE:
2150 {
2151 ao_ref dref;
2152 tree size = NULL_TREE;
2153 if (gimple_call_num_args (call) == 3)
2154 size = gimple_call_arg (call, 2);
2155 ao_ref_init_from_ptr_and_size (&dref,
2156 gimple_call_arg (call, 1),
2157 size);
2158 return refs_may_alias_p_1 (&dref, ref, false);
2159 }
2160 case BUILT_IN_STRCAT_CHK:
2161 case BUILT_IN_STRNCAT_CHK:
2162 {
2163 ao_ref dref;
2164 ao_ref_init_from_ptr_and_size (&dref,
2165 gimple_call_arg (call, 0),
2166 NULL_TREE);
2167 if (refs_may_alias_p_1 (&dref, ref, false))
2168 return true;
2169 }
2170 /* FALLTHRU */
2171 case BUILT_IN_STRCPY_CHK:
2172 case BUILT_IN_STRNCPY_CHK:
2173 case BUILT_IN_MEMCPY_CHK:
2174 case BUILT_IN_MEMMOVE_CHK:
2175 case BUILT_IN_MEMPCPY_CHK:
2176 case BUILT_IN_STPCPY_CHK:
2177 case BUILT_IN_STPNCPY_CHK:
2178 {
2179 ao_ref dref;
2180 tree size = NULL_TREE;
2181 if (gimple_call_num_args (call) == 4)
2182 size = gimple_call_arg (call, 2);
2183 ao_ref_init_from_ptr_and_size (&dref,
2184 gimple_call_arg (call, 1),
2185 size);
2186 return refs_may_alias_p_1 (&dref, ref, false);
2187 }
2188 case BUILT_IN_BCOPY:
2189 {
2190 ao_ref dref;
2191 tree size = gimple_call_arg (call, 2);
2192 ao_ref_init_from_ptr_and_size (&dref,
2193 gimple_call_arg (call, 0),
2194 size);
2195 return refs_may_alias_p_1 (&dref, ref, false);
2196 }
2197
2198 /* The following functions read memory pointed to by their
2199 first argument. */
2200 CASE_BUILT_IN_TM_LOAD (1):
2201 CASE_BUILT_IN_TM_LOAD (2):
2202 CASE_BUILT_IN_TM_LOAD (4):
2203 CASE_BUILT_IN_TM_LOAD (8):
2204 CASE_BUILT_IN_TM_LOAD (FLOAT):
2205 CASE_BUILT_IN_TM_LOAD (DOUBLE):
2206 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
2207 CASE_BUILT_IN_TM_LOAD (M64):
2208 CASE_BUILT_IN_TM_LOAD (M128):
2209 CASE_BUILT_IN_TM_LOAD (M256):
2210 case BUILT_IN_TM_LOG:
2211 case BUILT_IN_TM_LOG_1:
2212 case BUILT_IN_TM_LOG_2:
2213 case BUILT_IN_TM_LOG_4:
2214 case BUILT_IN_TM_LOG_8:
2215 case BUILT_IN_TM_LOG_FLOAT:
2216 case BUILT_IN_TM_LOG_DOUBLE:
2217 case BUILT_IN_TM_LOG_LDOUBLE:
2218 case BUILT_IN_TM_LOG_M64:
2219 case BUILT_IN_TM_LOG_M128:
2220 case BUILT_IN_TM_LOG_M256:
2221 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
2222
2223 /* These read memory pointed to by the first argument. */
2224 case BUILT_IN_STRDUP:
2225 case BUILT_IN_STRNDUP:
2226 case BUILT_IN_REALLOC:
2227 {
2228 ao_ref dref;
2229 tree size = NULL_TREE;
2230 if (gimple_call_num_args (call) == 2)
2231 size = gimple_call_arg (call, 1);
2232 ao_ref_init_from_ptr_and_size (&dref,
2233 gimple_call_arg (call, 0),
2234 size);
2235 return refs_may_alias_p_1 (&dref, ref, false);
2236 }
2237 /* These read memory pointed to by the first argument. */
2238 case BUILT_IN_INDEX:
2239 case BUILT_IN_STRCHR:
2240 case BUILT_IN_STRRCHR:
2241 {
2242 ao_ref dref;
2243 ao_ref_init_from_ptr_and_size (&dref,
2244 gimple_call_arg (call, 0),
2245 NULL_TREE);
2246 return refs_may_alias_p_1 (&dref, ref, false);
2247 }
2248 /* These read memory pointed to by the first argument with size
2249 in the third argument. */
2250 case BUILT_IN_MEMCHR:
2251 {
2252 ao_ref dref;
2253 ao_ref_init_from_ptr_and_size (&dref,
2254 gimple_call_arg (call, 0),
2255 gimple_call_arg (call, 2));
2256 return refs_may_alias_p_1 (&dref, ref, false);
2257 }
2258 /* These read memory pointed to by the first and second arguments. */
2259 case BUILT_IN_STRSTR:
2260 case BUILT_IN_STRPBRK:
2261 {
2262 ao_ref dref;
2263 ao_ref_init_from_ptr_and_size (&dref,
2264 gimple_call_arg (call, 0),
2265 NULL_TREE);
2266 if (refs_may_alias_p_1 (&dref, ref, false))
2267 return true;
2268 ao_ref_init_from_ptr_and_size (&dref,
2269 gimple_call_arg (call, 1),
2270 NULL_TREE);
2271 return refs_may_alias_p_1 (&dref, ref, false);
2272 }
2273
2274 /* The following builtins do not read from memory. */
2275 case BUILT_IN_FREE:
2276 case BUILT_IN_MALLOC:
2277 case BUILT_IN_POSIX_MEMALIGN:
2278 case BUILT_IN_ALIGNED_ALLOC:
2279 case BUILT_IN_CALLOC:
2280 CASE_BUILT_IN_ALLOCA:
2281 case BUILT_IN_STACK_SAVE:
2282 case BUILT_IN_STACK_RESTORE:
2283 case BUILT_IN_MEMSET:
2284 case BUILT_IN_TM_MEMSET:
2285 case BUILT_IN_MEMSET_CHK:
2286 case BUILT_IN_FREXP:
2287 case BUILT_IN_FREXPF:
2288 case BUILT_IN_FREXPL:
2289 case BUILT_IN_GAMMA_R:
2290 case BUILT_IN_GAMMAF_R:
2291 case BUILT_IN_GAMMAL_R:
2292 case BUILT_IN_LGAMMA_R:
2293 case BUILT_IN_LGAMMAF_R:
2294 case BUILT_IN_LGAMMAL_R:
2295 case BUILT_IN_MODF:
2296 case BUILT_IN_MODFF:
2297 case BUILT_IN_MODFL:
2298 case BUILT_IN_REMQUO:
2299 case BUILT_IN_REMQUOF:
2300 case BUILT_IN_REMQUOL:
2301 case BUILT_IN_SINCOS:
2302 case BUILT_IN_SINCOSF:
2303 case BUILT_IN_SINCOSL:
2304 case BUILT_IN_ASSUME_ALIGNED:
2305 case BUILT_IN_VA_END:
2306 return false;
2307 /* __sync_* builtins and some OpenMP builtins act as threading
2308 barriers. */
2309 #undef DEF_SYNC_BUILTIN
2310 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2311 #include "sync-builtins.def"
2312 #undef DEF_SYNC_BUILTIN
2313 case BUILT_IN_GOMP_ATOMIC_START:
2314 case BUILT_IN_GOMP_ATOMIC_END:
2315 case BUILT_IN_GOMP_BARRIER:
2316 case BUILT_IN_GOMP_BARRIER_CANCEL:
2317 case BUILT_IN_GOMP_TASKWAIT:
2318 case BUILT_IN_GOMP_TASKGROUP_END:
2319 case BUILT_IN_GOMP_CRITICAL_START:
2320 case BUILT_IN_GOMP_CRITICAL_END:
2321 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2322 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2323 case BUILT_IN_GOMP_LOOP_END:
2324 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2325 case BUILT_IN_GOMP_ORDERED_START:
2326 case BUILT_IN_GOMP_ORDERED_END:
2327 case BUILT_IN_GOMP_SECTIONS_END:
2328 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2329 case BUILT_IN_GOMP_SINGLE_COPY_START:
2330 case BUILT_IN_GOMP_SINGLE_COPY_END:
2331 return true;
2332
2333 default:
2334 /* Fallthru to general call handling. */;
2335 }
2336
2337 /* Check if base is a global static variable that is not read
2338 by the function. */
2339 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2340 {
2341 struct cgraph_node *node = cgraph_node::get (callee);
2342 bitmap not_read;
2343
2344 /* FIXME: Callee can be an OMP builtin that does not have a call graph
2345 node yet. We should enforce that there are nodes for all decls in the
2346 IL and remove this check instead. */
2347 if (node
2348 && (not_read = ipa_reference_get_not_read_global (node))
2349 && bitmap_bit_p (not_read, ipa_reference_var_uid (base)))
2350 goto process_args;
2351 }
2352
2353 /* Check if the base variable is call-used. */
2354 if (DECL_P (base))
2355 {
2356 if (pt_solution_includes (gimple_call_use_set (call), base))
2357 return true;
2358 }
2359 else if ((TREE_CODE (base) == MEM_REF
2360 || TREE_CODE (base) == TARGET_MEM_REF)
2361 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2362 {
2363 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2364 if (!pi)
2365 return true;
2366
2367 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
2368 return true;
2369 }
2370 else
2371 return true;
2372
2373 /* Inspect call arguments for passed-by-value aliases. */
2374 process_args:
2375 for (i = 0; i < gimple_call_num_args (call); ++i)
2376 {
2377 tree op = gimple_call_arg (call, i);
2378 int flags = gimple_call_arg_flags (call, i);
2379
2380 if (flags & EAF_UNUSED)
2381 continue;
2382
2383 if (TREE_CODE (op) == WITH_SIZE_EXPR)
2384 op = TREE_OPERAND (op, 0);
2385
2386 if (TREE_CODE (op) != SSA_NAME
2387 && !is_gimple_min_invariant (op))
2388 {
2389 ao_ref r;
2390 ao_ref_init (&r, op);
2391 if (refs_may_alias_p_1 (&r, ref, tbaa_p))
2392 return true;
2393 }
2394 }
2395
2396 return false;
2397 }
2398
2399 static bool
2400 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
2401 {
2402 bool res;
2403 res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
2404 if (res)
2405 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
2406 else
2407 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
2408 return res;
2409 }
2410
2411
2412 /* If the statement STMT may use the memory reference REF return
2413 true, otherwise return false. */
2414
2415 bool
2416 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
2417 {
2418 if (is_gimple_assign (stmt))
2419 {
2420 tree rhs;
2421
2422 /* All memory assign statements are single. */
2423 if (!gimple_assign_single_p (stmt))
2424 return false;
2425
2426 rhs = gimple_assign_rhs1 (stmt);
2427 if (is_gimple_reg (rhs)
2428 || is_gimple_min_invariant (rhs)
2429 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
2430 return false;
2431
2432 return refs_may_alias_p (rhs, ref, tbaa_p);
2433 }
2434 else if (is_gimple_call (stmt))
2435 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
2436 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2437 {
2438 tree retval = gimple_return_retval (return_stmt);
2439 if (retval
2440 && TREE_CODE (retval) != SSA_NAME
2441 && !is_gimple_min_invariant (retval)
2442 && refs_may_alias_p (retval, ref, tbaa_p))
2443 return true;
2444 /* If ref escapes the function then the return acts as a use. */
2445 tree base = ao_ref_base (ref);
2446 if (!base)
2447 ;
2448 else if (DECL_P (base))
2449 return is_global_var (base);
2450 else if (TREE_CODE (base) == MEM_REF
2451 || TREE_CODE (base) == TARGET_MEM_REF)
2452 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
2453 return false;
2454 }
2455
2456 return true;
2457 }
2458
2459 bool
2460 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
2461 {
2462 ao_ref r;
2463 ao_ref_init (&r, ref);
2464 return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
2465 }
2466
2467 /* If the call in statement CALL may clobber the memory reference REF
2468 return true, otherwise return false. */
2469
2470 bool
2471 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
2472 {
2473 tree base;
2474 tree callee;
2475
2476 /* If the call is pure or const it cannot clobber anything. */
2477 if (gimple_call_flags (call)
2478 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
2479 return false;
2480 if (gimple_call_internal_p (call))
2481 switch (gimple_call_internal_fn (call))
2482 {
2483 /* Treat these internal calls like ECF_PURE for aliasing,
2484 they don't write to any memory the program should care about.
2485 They have important other side-effects, and read memory,
2486 so can't be ECF_NOVOPS. */
2487 case IFN_UBSAN_NULL:
2488 case IFN_UBSAN_BOUNDS:
2489 case IFN_UBSAN_VPTR:
2490 case IFN_UBSAN_OBJECT_SIZE:
2491 case IFN_UBSAN_PTR:
2492 case IFN_ASAN_CHECK:
2493 return false;
2494 default:
2495 break;
2496 }
2497
2498 base = ao_ref_base (ref);
2499 if (!base)
2500 return true;
2501
2502 if (TREE_CODE (base) == SSA_NAME
2503 || CONSTANT_CLASS_P (base))
2504 return false;
2505
2506 /* A call that is not without side-effects might involve volatile
2507 accesses and thus conflicts with all other volatile accesses. */
2508 if (ref->volatile_p)
2509 return true;
2510
2511 /* If the reference is based on a decl that is not aliased the call
2512 cannot possibly clobber it. */
2513 if (DECL_P (base)
2514 && !may_be_aliased (base)
2515 /* But local non-readonly statics can be modified through recursion
2516 or the call may implement a threading barrier which we must
2517 treat as may-def. */
2518 && (TREE_READONLY (base)
2519 || !is_global_var (base)))
2520 return false;
2521
2522 /* If the reference is based on a pointer that points to memory
2523 that may not be written to then the call cannot possibly clobber it. */
2524 if ((TREE_CODE (base) == MEM_REF
2525 || TREE_CODE (base) == TARGET_MEM_REF)
2526 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2527 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
2528 return false;
2529
2530 callee = gimple_call_fndecl (call);
2531
2532 /* Handle those builtin functions explicitly that do not act as
2533 escape points. See tree-ssa-structalias.c:find_func_aliases
2534 for the list of builtins we might need to handle here. */
2535 if (callee != NULL_TREE
2536 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2537 switch (DECL_FUNCTION_CODE (callee))
2538 {
2539 /* All the following functions clobber memory pointed to by
2540 their first argument. */
2541 case BUILT_IN_STRCPY:
2542 case BUILT_IN_STRNCPY:
2543 case BUILT_IN_MEMCPY:
2544 case BUILT_IN_MEMMOVE:
2545 case BUILT_IN_MEMPCPY:
2546 case BUILT_IN_STPCPY:
2547 case BUILT_IN_STPNCPY:
2548 case BUILT_IN_STRCAT:
2549 case BUILT_IN_STRNCAT:
2550 case BUILT_IN_MEMSET:
2551 case BUILT_IN_TM_MEMSET:
2552 CASE_BUILT_IN_TM_STORE (1):
2553 CASE_BUILT_IN_TM_STORE (2):
2554 CASE_BUILT_IN_TM_STORE (4):
2555 CASE_BUILT_IN_TM_STORE (8):
2556 CASE_BUILT_IN_TM_STORE (FLOAT):
2557 CASE_BUILT_IN_TM_STORE (DOUBLE):
2558 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2559 CASE_BUILT_IN_TM_STORE (M64):
2560 CASE_BUILT_IN_TM_STORE (M128):
2561 CASE_BUILT_IN_TM_STORE (M256):
2562 case BUILT_IN_TM_MEMCPY:
2563 case BUILT_IN_TM_MEMMOVE:
2564 {
2565 ao_ref dref;
2566 tree size = NULL_TREE;
2567 /* Don't pass in size for strncat, as the maximum size
2568 is strlen (dest) + n + 1 instead of n, resp.
2569 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2570 known. */
2571 if (gimple_call_num_args (call) == 3
2572 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2573 size = gimple_call_arg (call, 2);
2574 ao_ref_init_from_ptr_and_size (&dref,
2575 gimple_call_arg (call, 0),
2576 size);
2577 return refs_may_alias_p_1 (&dref, ref, false);
2578 }
2579 case BUILT_IN_STRCPY_CHK:
2580 case BUILT_IN_STRNCPY_CHK:
2581 case BUILT_IN_MEMCPY_CHK:
2582 case BUILT_IN_MEMMOVE_CHK:
2583 case BUILT_IN_MEMPCPY_CHK:
2584 case BUILT_IN_STPCPY_CHK:
2585 case BUILT_IN_STPNCPY_CHK:
2586 case BUILT_IN_STRCAT_CHK:
2587 case BUILT_IN_STRNCAT_CHK:
2588 case BUILT_IN_MEMSET_CHK:
2589 {
2590 ao_ref dref;
2591 tree size = NULL_TREE;
2592 /* Don't pass in size for __strncat_chk, as the maximum size
2593 is strlen (dest) + n + 1 instead of n, resp.
2594 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2595 known. */
2596 if (gimple_call_num_args (call) == 4
2597 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2598 size = gimple_call_arg (call, 2);
2599 ao_ref_init_from_ptr_and_size (&dref,
2600 gimple_call_arg (call, 0),
2601 size);
2602 return refs_may_alias_p_1 (&dref, ref, false);
2603 }
2604 case BUILT_IN_BCOPY:
2605 {
2606 ao_ref dref;
2607 tree size = gimple_call_arg (call, 2);
2608 ao_ref_init_from_ptr_and_size (&dref,
2609 gimple_call_arg (call, 1),
2610 size);
2611 return refs_may_alias_p_1 (&dref, ref, false);
2612 }
2613 /* Allocating memory does not have any side-effects apart from
2614 being the definition point for the pointer. */
2615 case BUILT_IN_MALLOC:
2616 case BUILT_IN_ALIGNED_ALLOC:
2617 case BUILT_IN_CALLOC:
2618 case BUILT_IN_STRDUP:
2619 case BUILT_IN_STRNDUP:
2620 /* Unix98 specifies that errno is set on allocation failure. */
2621 if (flag_errno_math
2622 && targetm.ref_may_alias_errno (ref))
2623 return true;
2624 return false;
2625 case BUILT_IN_STACK_SAVE:
2626 CASE_BUILT_IN_ALLOCA:
2627 case BUILT_IN_ASSUME_ALIGNED:
2628 return false;
2629 /* But posix_memalign stores a pointer into the memory pointed to
2630 by its first argument. */
2631 case BUILT_IN_POSIX_MEMALIGN:
2632 {
2633 tree ptrptr = gimple_call_arg (call, 0);
2634 ao_ref dref;
2635 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2636 TYPE_SIZE_UNIT (ptr_type_node));
2637 return (refs_may_alias_p_1 (&dref, ref, false)
2638 || (flag_errno_math
2639 && targetm.ref_may_alias_errno (ref)));
2640 }
2641 /* Freeing memory kills the pointed-to memory. More importantly
2642 the call has to serve as a barrier for moving loads and stores
2643 across it. */
2644 case BUILT_IN_FREE:
2645 case BUILT_IN_VA_END:
2646 {
2647 tree ptr = gimple_call_arg (call, 0);
2648 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2649 }
2650 /* Realloc serves both as allocation point and deallocation point. */
2651 case BUILT_IN_REALLOC:
2652 {
2653 tree ptr = gimple_call_arg (call, 0);
2654 /* Unix98 specifies that errno is set on allocation failure. */
2655 return ((flag_errno_math
2656 && targetm.ref_may_alias_errno (ref))
2657 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2658 }
2659 case BUILT_IN_GAMMA_R:
2660 case BUILT_IN_GAMMAF_R:
2661 case BUILT_IN_GAMMAL_R:
2662 case BUILT_IN_LGAMMA_R:
2663 case BUILT_IN_LGAMMAF_R:
2664 case BUILT_IN_LGAMMAL_R:
2665 {
2666 tree out = gimple_call_arg (call, 1);
2667 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2668 return true;
2669 if (flag_errno_math)
2670 break;
2671 return false;
2672 }
2673 case BUILT_IN_FREXP:
2674 case BUILT_IN_FREXPF:
2675 case BUILT_IN_FREXPL:
2676 case BUILT_IN_MODF:
2677 case BUILT_IN_MODFF:
2678 case BUILT_IN_MODFL:
2679 {
2680 tree out = gimple_call_arg (call, 1);
2681 return ptr_deref_may_alias_ref_p_1 (out, ref);
2682 }
2683 case BUILT_IN_REMQUO:
2684 case BUILT_IN_REMQUOF:
2685 case BUILT_IN_REMQUOL:
2686 {
2687 tree out = gimple_call_arg (call, 2);
2688 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2689 return true;
2690 if (flag_errno_math)
2691 break;
2692 return false;
2693 }
2694 case BUILT_IN_SINCOS:
2695 case BUILT_IN_SINCOSF:
2696 case BUILT_IN_SINCOSL:
2697 {
2698 tree sin = gimple_call_arg (call, 1);
2699 tree cos = gimple_call_arg (call, 2);
2700 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2701 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2702 }
2703 /* __sync_* builtins and some OpenMP builtins act as threading
2704 barriers. */
2705 #undef DEF_SYNC_BUILTIN
2706 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2707 #include "sync-builtins.def"
2708 #undef DEF_SYNC_BUILTIN
2709 case BUILT_IN_GOMP_ATOMIC_START:
2710 case BUILT_IN_GOMP_ATOMIC_END:
2711 case BUILT_IN_GOMP_BARRIER:
2712 case BUILT_IN_GOMP_BARRIER_CANCEL:
2713 case BUILT_IN_GOMP_TASKWAIT:
2714 case BUILT_IN_GOMP_TASKGROUP_END:
2715 case BUILT_IN_GOMP_CRITICAL_START:
2716 case BUILT_IN_GOMP_CRITICAL_END:
2717 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2718 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2719 case BUILT_IN_GOMP_LOOP_END:
2720 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2721 case BUILT_IN_GOMP_ORDERED_START:
2722 case BUILT_IN_GOMP_ORDERED_END:
2723 case BUILT_IN_GOMP_SECTIONS_END:
2724 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2725 case BUILT_IN_GOMP_SINGLE_COPY_START:
2726 case BUILT_IN_GOMP_SINGLE_COPY_END:
2727 return true;
2728 default:
2729 /* Fallthru to general call handling. */;
2730 }
2731
2732 /* Check if base is a global static variable that is not written
2733 by the function. */
2734 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2735 {
2736 struct cgraph_node *node = cgraph_node::get (callee);
2737 bitmap not_written;
2738
2739 if (node
2740 && (not_written = ipa_reference_get_not_written_global (node))
2741 && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
2742 return false;
2743 }
2744
2745 /* Check if the base variable is call-clobbered. */
2746 if (DECL_P (base))
2747 return pt_solution_includes (gimple_call_clobber_set (call), base);
2748 else if ((TREE_CODE (base) == MEM_REF
2749 || TREE_CODE (base) == TARGET_MEM_REF)
2750 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2751 {
2752 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2753 if (!pi)
2754 return true;
2755
2756 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2757 }
2758
2759 return true;
2760 }
2761
2762 /* If the call in statement CALL may clobber the memory reference REF
2763 return true, otherwise return false. */
2764
2765 bool
2766 call_may_clobber_ref_p (gcall *call, tree ref)
2767 {
2768 bool res;
2769 ao_ref r;
2770 ao_ref_init (&r, ref);
2771 res = call_may_clobber_ref_p_1 (call, &r);
2772 if (res)
2773 ++alias_stats.call_may_clobber_ref_p_may_alias;
2774 else
2775 ++alias_stats.call_may_clobber_ref_p_no_alias;
2776 return res;
2777 }
2778
2779
2780 /* If the statement STMT may clobber the memory reference REF return true,
2781 otherwise return false. */
2782
2783 bool
2784 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
2785 {
2786 if (is_gimple_call (stmt))
2787 {
2788 tree lhs = gimple_call_lhs (stmt);
2789 if (lhs
2790 && TREE_CODE (lhs) != SSA_NAME)
2791 {
2792 ao_ref r;
2793 ao_ref_init (&r, lhs);
2794 if (refs_may_alias_p_1 (ref, &r, tbaa_p))
2795 return true;
2796 }
2797
2798 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2799 }
2800 else if (gimple_assign_single_p (stmt))
2801 {
2802 tree lhs = gimple_assign_lhs (stmt);
2803 if (TREE_CODE (lhs) != SSA_NAME)
2804 {
2805 ao_ref r;
2806 ao_ref_init (&r, lhs);
2807 return refs_may_alias_p_1 (ref, &r, tbaa_p);
2808 }
2809 }
2810 else if (gimple_code (stmt) == GIMPLE_ASM)
2811 return true;
2812
2813 return false;
2814 }
2815
2816 bool
2817 stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
2818 {
2819 ao_ref r;
2820 ao_ref_init (&r, ref);
2821 return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
2822 }
2823
2824 /* Return true if store1 and store2 described by corresponding tuples
2825 <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
2826 address. */
2827
2828 static bool
2829 same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
2830 poly_int64 max_size1,
2831 tree base2, poly_int64 offset2, poly_int64 size2,
2832 poly_int64 max_size2)
2833 {
2834 /* Offsets need to be 0. */
2835 if (maybe_ne (offset1, 0)
2836 || maybe_ne (offset2, 0))
2837 return false;
2838
2839 bool base1_obj_p = SSA_VAR_P (base1);
2840 bool base2_obj_p = SSA_VAR_P (base2);
2841
2842 /* We need one object. */
2843 if (base1_obj_p == base2_obj_p)
2844 return false;
2845 tree obj = base1_obj_p ? base1 : base2;
2846
2847 /* And we need one MEM_REF. */
2848 bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
2849 bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
2850 if (base1_memref_p == base2_memref_p)
2851 return false;
2852 tree memref = base1_memref_p ? base1 : base2;
2853
2854 /* Sizes need to be valid. */
2855 if (!known_size_p (max_size1)
2856 || !known_size_p (max_size2)
2857 || !known_size_p (size1)
2858 || !known_size_p (size2))
2859 return false;
2860
2861 /* Max_size needs to match size. */
2862 if (maybe_ne (max_size1, size1)
2863 || maybe_ne (max_size2, size2))
2864 return false;
2865
2866 /* Sizes need to match. */
2867 if (maybe_ne (size1, size2))
2868 return false;
2869
2870
2871 /* Check that memref is a store to pointer with singleton points-to info. */
2872 if (!integer_zerop (TREE_OPERAND (memref, 1)))
2873 return false;
2874 tree ptr = TREE_OPERAND (memref, 0);
2875 if (TREE_CODE (ptr) != SSA_NAME)
2876 return false;
2877 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2878 unsigned int pt_uid;
2879 if (pi == NULL
2880 || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid))
2881 return false;
2882
2883 /* Be conservative with non-call exceptions when the address might
2884 be NULL. */
2885 if (cfun->can_throw_non_call_exceptions && pi->pt.null)
2886 return false;
2887
2888 /* Check that ptr points relative to obj. */
2889 unsigned int obj_uid = DECL_PT_UID (obj);
2890 if (obj_uid != pt_uid)
2891 return false;
2892
2893 /* Check that the object size is the same as the store size. That ensures us
2894 that ptr points to the start of obj. */
2895 return (DECL_SIZE (obj)
2896 && poly_int_tree_p (DECL_SIZE (obj))
2897 && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
2898 }
2899
2900 /* If STMT kills the memory reference REF return true, otherwise
2901 return false. */
2902
2903 bool
2904 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2905 {
2906 if (!ao_ref_base (ref))
2907 return false;
2908
2909 if (gimple_has_lhs (stmt)
2910 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2911 /* The assignment is not necessarily carried out if it can throw
2912 and we can catch it in the current function where we could inspect
2913 the previous value.
2914 ??? We only need to care about the RHS throwing. For aggregate
2915 assignments or similar calls and non-call exceptions the LHS
2916 might throw as well. */
2917 && !stmt_can_throw_internal (cfun, stmt))
2918 {
2919 tree lhs = gimple_get_lhs (stmt);
2920 /* If LHS is literally a base of the access we are done. */
2921 if (ref->ref)
2922 {
2923 tree base = ref->ref;
2924 tree innermost_dropped_array_ref = NULL_TREE;
2925 if (handled_component_p (base))
2926 {
2927 tree saved_lhs0 = NULL_TREE;
2928 if (handled_component_p (lhs))
2929 {
2930 saved_lhs0 = TREE_OPERAND (lhs, 0);
2931 TREE_OPERAND (lhs, 0) = integer_zero_node;
2932 }
2933 do
2934 {
2935 /* Just compare the outermost handled component, if
2936 they are equal we have found a possible common
2937 base. */
2938 tree saved_base0 = TREE_OPERAND (base, 0);
2939 TREE_OPERAND (base, 0) = integer_zero_node;
2940 bool res = operand_equal_p (lhs, base, 0);
2941 TREE_OPERAND (base, 0) = saved_base0;
2942 if (res)
2943 break;
2944 /* Remember if we drop an array-ref that we need to
2945 double-check not being at struct end. */
2946 if (TREE_CODE (base) == ARRAY_REF
2947 || TREE_CODE (base) == ARRAY_RANGE_REF)
2948 innermost_dropped_array_ref = base;
2949 /* Otherwise drop handled components of the access. */
2950 base = saved_base0;
2951 }
2952 while (handled_component_p (base));
2953 if (saved_lhs0)
2954 TREE_OPERAND (lhs, 0) = saved_lhs0;
2955 }
2956 /* Finally check if the lhs has the same address and size as the
2957 base candidate of the access. Watch out if we have dropped
2958 an array-ref that was at struct end, this means ref->ref may
2959 be outside of the TYPE_SIZE of its base. */
2960 if ((! innermost_dropped_array_ref
2961 || ! array_at_struct_end_p (innermost_dropped_array_ref))
2962 && (lhs == base
2963 || (((TYPE_SIZE (TREE_TYPE (lhs))
2964 == TYPE_SIZE (TREE_TYPE (base)))
2965 || (TYPE_SIZE (TREE_TYPE (lhs))
2966 && TYPE_SIZE (TREE_TYPE (base))
2967 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2968 TYPE_SIZE (TREE_TYPE (base)),
2969 0)))
2970 && operand_equal_p (lhs, base,
2971 OEP_ADDRESS_OF
2972 | OEP_MATCH_SIDE_EFFECTS))))
2973 return true;
2974 }
2975
2976 /* Now look for non-literal equal bases with the restriction of
2977 handling constant offset and size. */
2978 /* For a must-alias check we need to be able to constrain
2979 the access properly. */
2980 if (!ref->max_size_known_p ())
2981 return false;
2982 poly_int64 size, offset, max_size, ref_offset = ref->offset;
2983 bool reverse;
2984 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
2985 &reverse);
2986 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2987 so base == ref->base does not always hold. */
2988 if (base != ref->base)
2989 {
2990 /* Try using points-to info. */
2991 if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
2992 ref->offset, ref->size, ref->max_size))
2993 return true;
2994
2995 /* If both base and ref->base are MEM_REFs, only compare the
2996 first operand, and if the second operand isn't equal constant,
2997 try to add the offsets into offset and ref_offset. */
2998 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
2999 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
3000 {
3001 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
3002 TREE_OPERAND (ref->base, 1)))
3003 {
3004 poly_offset_int off1 = mem_ref_offset (base);
3005 off1 <<= LOG2_BITS_PER_UNIT;
3006 off1 += offset;
3007 poly_offset_int off2 = mem_ref_offset (ref->base);
3008 off2 <<= LOG2_BITS_PER_UNIT;
3009 off2 += ref_offset;
3010 if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
3011 size = -1;
3012 }
3013 }
3014 else
3015 size = -1;
3016 }
3017 /* For a must-alias check we need to be able to constrain
3018 the access properly. */
3019 if (known_eq (size, max_size)
3020 && known_subrange_p (ref_offset, ref->max_size, offset, size))
3021 return true;
3022 }
3023
3024 if (is_gimple_call (stmt))
3025 {
3026 tree callee = gimple_call_fndecl (stmt);
3027 if (callee != NULL_TREE
3028 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3029 switch (DECL_FUNCTION_CODE (callee))
3030 {
3031 case BUILT_IN_FREE:
3032 {
3033 tree ptr = gimple_call_arg (stmt, 0);
3034 tree base = ao_ref_base (ref);
3035 if (base && TREE_CODE (base) == MEM_REF
3036 && TREE_OPERAND (base, 0) == ptr)
3037 return true;
3038 break;
3039 }
3040
3041 case BUILT_IN_MEMCPY:
3042 case BUILT_IN_MEMPCPY:
3043 case BUILT_IN_MEMMOVE:
3044 case BUILT_IN_MEMSET:
3045 case BUILT_IN_MEMCPY_CHK:
3046 case BUILT_IN_MEMPCPY_CHK:
3047 case BUILT_IN_MEMMOVE_CHK:
3048 case BUILT_IN_MEMSET_CHK:
3049 case BUILT_IN_STRNCPY:
3050 case BUILT_IN_STPNCPY:
3051 case BUILT_IN_CALLOC:
3052 {
3053 /* For a must-alias check we need to be able to constrain
3054 the access properly. */
3055 if (!ref->max_size_known_p ())
3056 return false;
3057 tree dest;
3058 tree len;
3059
3060 /* In execution order a calloc call will never kill
3061 anything. However, DSE will (ab)use this interface
3062 to ask if a calloc call writes the same memory locations
3063 as a later assignment, memset, etc. So handle calloc
3064 in the expected way. */
3065 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
3066 {
3067 tree arg0 = gimple_call_arg (stmt, 0);
3068 tree arg1 = gimple_call_arg (stmt, 1);
3069 if (TREE_CODE (arg0) != INTEGER_CST
3070 || TREE_CODE (arg1) != INTEGER_CST)
3071 return false;
3072
3073 dest = gimple_call_lhs (stmt);
3074 len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
3075 }
3076 else
3077 {
3078 dest = gimple_call_arg (stmt, 0);
3079 len = gimple_call_arg (stmt, 2);
3080 }
3081 if (!poly_int_tree_p (len))
3082 return false;
3083 tree rbase = ref->base;
3084 poly_offset_int roffset = ref->offset;
3085 ao_ref dref;
3086 ao_ref_init_from_ptr_and_size (&dref, dest, len);
3087 tree base = ao_ref_base (&dref);
3088 poly_offset_int offset = dref.offset;
3089 if (!base || !known_size_p (dref.size))
3090 return false;
3091 if (TREE_CODE (base) == MEM_REF)
3092 {
3093 if (TREE_CODE (rbase) != MEM_REF)
3094 return false;
3095 // Compare pointers.
3096 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
3097 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
3098 base = TREE_OPERAND (base, 0);
3099 rbase = TREE_OPERAND (rbase, 0);
3100 }
3101 if (base == rbase
3102 && known_subrange_p (roffset, ref->max_size, offset,
3103 wi::to_poly_offset (len)
3104 << LOG2_BITS_PER_UNIT))
3105 return true;
3106 break;
3107 }
3108
3109 case BUILT_IN_VA_END:
3110 {
3111 tree ptr = gimple_call_arg (stmt, 0);
3112 if (TREE_CODE (ptr) == ADDR_EXPR)
3113 {
3114 tree base = ao_ref_base (ref);
3115 if (TREE_OPERAND (ptr, 0) == base)
3116 return true;
3117 }
3118 break;
3119 }
3120
3121 default:;
3122 }
3123 }
3124 return false;
3125 }
3126
3127 bool
3128 stmt_kills_ref_p (gimple *stmt, tree ref)
3129 {
3130 ao_ref r;
3131 ao_ref_init (&r, ref);
3132 return stmt_kills_ref_p (stmt, &r);
3133 }
3134
3135
3136 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3137 TARGET or a statement clobbering the memory reference REF in which
3138 case false is returned. The walk starts with VUSE, one argument of PHI. */
3139
3140 static bool
3141 maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3142 ao_ref *ref, tree vuse, bool tbaa_p, unsigned int &limit,
3143 bitmap *visited, bool abort_on_visited,
3144 void *(*translate)(ao_ref *, tree, void *, bool *),
3145 void *data)
3146 {
3147 basic_block bb = gimple_bb (phi);
3148
3149 if (!*visited)
3150 *visited = BITMAP_ALLOC (NULL);
3151
3152 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3153
3154 /* Walk until we hit the target. */
3155 while (vuse != target)
3156 {
3157 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3158 /* If we are searching for the target VUSE by walking up to
3159 TARGET_BB dominating the original PHI we are finished once
3160 we reach a default def or a definition in a block dominating
3161 that block. Update TARGET and return. */
3162 if (!target
3163 && (gimple_nop_p (def_stmt)
3164 || dominated_by_p (CDI_DOMINATORS,
3165 target_bb, gimple_bb (def_stmt))))
3166 {
3167 target = vuse;
3168 return true;
3169 }
3170
3171 /* Recurse for PHI nodes. */
3172 if (gimple_code (def_stmt) == GIMPLE_PHI)
3173 {
3174 /* An already visited PHI node ends the walk successfully. */
3175 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
3176 return !abort_on_visited;
3177 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3178 visited, abort_on_visited,
3179 translate, data);
3180 if (!vuse)
3181 return false;
3182 continue;
3183 }
3184 else if (gimple_nop_p (def_stmt))
3185 return false;
3186 else
3187 {
3188 /* A clobbering statement or the end of the IL ends it failing. */
3189 if ((int)limit <= 0)
3190 return false;
3191 --limit;
3192 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3193 {
3194 bool disambiguate_only = true;
3195 if (translate
3196 && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
3197 ;
3198 else
3199 return false;
3200 }
3201 }
3202 /* If we reach a new basic-block see if we already skipped it
3203 in a previous walk that ended successfully. */
3204 if (gimple_bb (def_stmt) != bb)
3205 {
3206 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3207 return !abort_on_visited;
3208 bb = gimple_bb (def_stmt);
3209 }
3210 vuse = gimple_vuse (def_stmt);
3211 }
3212 return true;
3213 }
3214
3215
3216 /* Starting from a PHI node for the virtual operand of the memory reference
3217 REF find a continuation virtual operand that allows to continue walking
3218 statements dominating PHI skipping only statements that cannot possibly
3219 clobber REF. Decrements LIMIT for each alias disambiguation done
3220 and aborts the walk, returning NULL_TREE if it reaches zero.
3221 Returns NULL_TREE if no suitable virtual operand can be found. */
3222
3223 tree
3224 get_continuation_for_phi (gimple *phi, ao_ref *ref, bool tbaa_p,
3225 unsigned int &limit, bitmap *visited,
3226 bool abort_on_visited,
3227 void *(*translate)(ao_ref *, tree, void *, bool *),
3228 void *data)
3229 {
3230 unsigned nargs = gimple_phi_num_args (phi);
3231
3232 /* Through a single-argument PHI we can simply look through. */
3233 if (nargs == 1)
3234 return PHI_ARG_DEF (phi, 0);
3235
3236 /* For two or more arguments try to pairwise skip non-aliasing code
3237 until we hit the phi argument definition that dominates the other one. */
3238 basic_block phi_bb = gimple_bb (phi);
3239 tree arg0, arg1;
3240 unsigned i;
3241
3242 /* Find a candidate for the virtual operand which definition
3243 dominates those of all others. */
3244 /* First look if any of the args themselves satisfy this. */
3245 for (i = 0; i < nargs; ++i)
3246 {
3247 arg0 = PHI_ARG_DEF (phi, i);
3248 if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3249 break;
3250 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3251 if (def_bb != phi_bb
3252 && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3253 break;
3254 arg0 = NULL_TREE;
3255 }
3256 /* If not, look if we can reach such candidate by walking defs
3257 until we hit the immediate dominator. maybe_skip_until will
3258 do that for us. */
3259 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3260
3261 /* Then check against the (to be) found candidate. */
3262 for (i = 0; i < nargs; ++i)
3263 {
3264 arg1 = PHI_ARG_DEF (phi, i);
3265 if (arg1 == arg0)
3266 ;
3267 else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, tbaa_p,
3268 limit, visited,
3269 abort_on_visited,
3270 /* Do not translate when walking over
3271 backedges. */
3272 dominated_by_p
3273 (CDI_DOMINATORS,
3274 gimple_bb (SSA_NAME_DEF_STMT (arg1)),
3275 phi_bb)
3276 ? NULL : translate, data))
3277 return NULL_TREE;
3278 }
3279
3280 return arg0;
3281 }
3282
3283 /* Based on the memory reference REF and its virtual use VUSE call
3284 WALKER for each virtual use that is equivalent to VUSE, including VUSE
3285 itself. That is, for each virtual use for which its defining statement
3286 does not clobber REF.
3287
3288 WALKER is called with REF, the current virtual use and DATA. If
3289 WALKER returns non-NULL the walk stops and its result is returned.
3290 At the end of a non-successful walk NULL is returned.
3291
3292 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3293 use which definition is a statement that may clobber REF and DATA.
3294 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3295 If TRANSLATE returns non-NULL the walk stops and its result is returned.
3296 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3297 to adjust REF and *DATA to make that valid.
3298
3299 VALUEIZE if non-NULL is called with the next VUSE that is considered
3300 and return value is substituted for that. This can be used to
3301 implement optimistic value-numbering for example. Note that the
3302 VUSE argument is assumed to be valueized already.
3303
3304 LIMIT specifies the number of alias queries we are allowed to do,
3305 the walk stops when it reaches zero and NULL is returned. LIMIT
3306 is decremented by the number of alias queries (plus adjustments
3307 done by the callbacks) upon return.
3308
3309 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3310
3311 void *
3312 walk_non_aliased_vuses (ao_ref *ref, tree vuse, bool tbaa_p,
3313 void *(*walker)(ao_ref *, tree, void *),
3314 void *(*translate)(ao_ref *, tree, void *, bool *),
3315 tree (*valueize)(tree),
3316 unsigned &limit, void *data)
3317 {
3318 bitmap visited = NULL;
3319 void *res;
3320 bool translated = false;
3321
3322 timevar_push (TV_ALIAS_STMT_WALK);
3323
3324 do
3325 {
3326 gimple *def_stmt;
3327
3328 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3329 res = (*walker) (ref, vuse, data);
3330 /* Abort walk. */
3331 if (res == (void *)-1)
3332 {
3333 res = NULL;
3334 break;
3335 }
3336 /* Lookup succeeded. */
3337 else if (res != NULL)
3338 break;
3339
3340 if (valueize)
3341 {
3342 vuse = valueize (vuse);
3343 if (!vuse)
3344 {
3345 res = NULL;
3346 break;
3347 }
3348 }
3349 def_stmt = SSA_NAME_DEF_STMT (vuse);
3350 if (gimple_nop_p (def_stmt))
3351 break;
3352 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3353 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3354 &visited, translated, translate, data);
3355 else
3356 {
3357 if ((int)limit <= 0)
3358 {
3359 res = NULL;
3360 break;
3361 }
3362 --limit;
3363 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3364 {
3365 if (!translate)
3366 break;
3367 bool disambiguate_only = false;
3368 res = (*translate) (ref, vuse, data, &disambiguate_only);
3369 /* Failed lookup and translation. */
3370 if (res == (void *)-1)
3371 {
3372 res = NULL;
3373 break;
3374 }
3375 /* Lookup succeeded. */
3376 else if (res != NULL)
3377 break;
3378 /* Translation succeeded, continue walking. */
3379 translated = translated || !disambiguate_only;
3380 }
3381 vuse = gimple_vuse (def_stmt);
3382 }
3383 }
3384 while (vuse);
3385
3386 if (visited)
3387 BITMAP_FREE (visited);
3388
3389 timevar_pop (TV_ALIAS_STMT_WALK);
3390
3391 return res;
3392 }
3393
3394
3395 /* Based on the memory reference REF call WALKER for each vdef which
3396 defining statement may clobber REF, starting with VDEF. If REF
3397 is NULL_TREE, each defining statement is visited.
3398
3399 WALKER is called with REF, the current vdef and DATA. If WALKER
3400 returns true the walk is stopped, otherwise it continues.
3401
3402 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
3403 The pointer may be NULL and then we do not track this information.
3404
3405 At PHI nodes walk_aliased_vdefs forks into one walk for reach
3406 PHI argument (but only one walk continues on merge points), the
3407 return value is true if any of the walks was successful.
3408
3409 The function returns the number of statements walked or -1 if
3410 LIMIT stmts were walked and the walk was aborted at this point.
3411 If LIMIT is zero the walk is not aborted. */
3412
3413 static int
3414 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
3415 bool (*walker)(ao_ref *, tree, void *), void *data,
3416 bitmap *visited, unsigned int cnt,
3417 bool *function_entry_reached, unsigned limit)
3418 {
3419 do
3420 {
3421 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
3422
3423 if (*visited
3424 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
3425 return cnt;
3426
3427 if (gimple_nop_p (def_stmt))
3428 {
3429 if (function_entry_reached)
3430 *function_entry_reached = true;
3431 return cnt;
3432 }
3433 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3434 {
3435 unsigned i;
3436 if (!*visited)
3437 *visited = BITMAP_ALLOC (NULL);
3438 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
3439 {
3440 int res = walk_aliased_vdefs_1 (ref,
3441 gimple_phi_arg_def (def_stmt, i),
3442 walker, data, visited, cnt,
3443 function_entry_reached, limit);
3444 if (res == -1)
3445 return -1;
3446 cnt = res;
3447 }
3448 return cnt;
3449 }
3450
3451 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3452 cnt++;
3453 if (cnt == limit)
3454 return -1;
3455 if ((!ref
3456 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
3457 && (*walker) (ref, vdef, data))
3458 return cnt;
3459
3460 vdef = gimple_vuse (def_stmt);
3461 }
3462 while (1);
3463 }
3464
3465 int
3466 walk_aliased_vdefs (ao_ref *ref, tree vdef,
3467 bool (*walker)(ao_ref *, tree, void *), void *data,
3468 bitmap *visited,
3469 bool *function_entry_reached, unsigned int limit)
3470 {
3471 bitmap local_visited = NULL;
3472 int ret;
3473
3474 timevar_push (TV_ALIAS_STMT_WALK);
3475
3476 if (function_entry_reached)
3477 *function_entry_reached = false;
3478
3479 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
3480 visited ? visited : &local_visited, 0,
3481 function_entry_reached, limit);
3482 if (local_visited)
3483 BITMAP_FREE (local_visited);
3484
3485 timevar_pop (TV_ALIAS_STMT_WALK);
3486
3487 return ret;
3488 }
3489