Line data Source code
1 : /* Alias analysis for GNU C
2 : Copyright (C) 1997-2026 Free Software Foundation, Inc.
3 : Contributed by John Carr (jfc@mit.edu).
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : 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 "df.h"
30 : #include "memmodel.h"
31 : #include "tm_p.h"
32 : #include "gimple-ssa.h"
33 : #include "emit-rtl.h"
34 : #include "alias.h"
35 : #include "fold-const.h"
36 : #include "varasm.h"
37 : #include "cselib.h"
38 : #include "langhooks.h"
39 : #include "cfganal.h"
40 : #include "rtl-iter.h"
41 : #include "cgraph.h"
42 : #include "ipa-utils.h"
43 :
44 : /* The aliasing API provided here solves related but different problems:
45 :
46 : Say there exists (in c)
47 :
48 : struct X {
49 : struct Y y1;
50 : struct Z z2;
51 : } x1, *px1, *px2;
52 :
53 : struct Y y2, *py;
54 : struct Z z2, *pz;
55 :
56 :
57 : py = &x1.y1;
58 : px2 = &x1;
59 :
60 : Consider the four questions:
61 :
62 : Can a store to x1 interfere with px2->y1?
63 : Can a store to x1 interfere with px2->z2?
64 : Can a store to x1 change the value pointed to by with py?
65 : Can a store to x1 change the value pointed to by with pz?
66 :
67 : The answer to these questions can be yes, yes, yes, and maybe.
68 :
69 : The first two questions can be answered with a simple examination
70 : of the type system. If structure X contains a field of type Y then
71 : a store through a pointer to an X can overwrite any field that is
72 : contained (recursively) in an X (unless we know that px1 != px2).
73 :
74 : The last two questions can be solved in the same way as the first
75 : two questions but this is too conservative. The observation is
76 : that in some cases we can know which (if any) fields are addressed
77 : and if those addresses are used in bad ways. This analysis may be
78 : language specific. In C, arbitrary operations may be applied to
79 : pointers. However, there is some indication that this may be too
80 : conservative for some C++ types.
81 :
82 : The pass ipa-type-escape does this analysis for the types whose
83 : instances do not escape across the compilation boundary.
84 :
85 : Historically in GCC, these two problems were combined and a single
86 : data structure that was used to represent the solution to these
87 : problems. We now have two similar but different data structures,
88 : The data structure to solve the last two questions is similar to
89 : the first, but does not contain the fields whose address are never
90 : taken. For types that do escape the compilation unit, the data
91 : structures will have identical information.
92 : */
93 :
94 : /* The alias sets assigned to MEMs assist the back-end in determining
95 : which MEMs can alias which other MEMs. In general, two MEMs in
96 : different alias sets cannot alias each other, with one important
97 : exception. Consider something like:
98 :
99 : struct S { int i; double d; };
100 :
101 : a store to an `S' can alias something of either type `int' or type
102 : `double'. (However, a store to an `int' cannot alias a `double'
103 : and vice versa.) We indicate this via a tree structure that looks
104 : like:
105 : struct S
106 : / \
107 : / \
108 : |/_ _\|
109 : int double
110 :
111 : (The arrows are directed and point downwards.)
112 : In this situation we say the alias set for `struct S' is the
113 : `superset' and that those for `int' and `double' are `subsets'.
114 :
115 : To see whether two alias sets can point to the same memory, we must
116 : see if either alias set is a subset of the other. We need not trace
117 : past immediate descendants, however, since we propagate all
118 : grandchildren up one level.
119 :
120 : Alias set zero is implicitly a superset of all other alias sets.
121 : However, this is no actual entry for alias set zero. It is an
122 : error to attempt to explicitly construct a subset of zero. */
123 :
124 : struct alias_set_hash : int_hash <int, INT_MIN, INT_MIN + 1> {};
125 :
126 : struct GTY(()) alias_set_entry {
127 : /* The alias set number, as stored in MEM_ALIAS_SET. */
128 : alias_set_type alias_set;
129 :
130 : /* Nonzero if would have a child of zero: this effectively makes this
131 : alias set the same as alias set zero. */
132 : bool has_zero_child;
133 : /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
134 : aggregate contaiing pointer.
135 : This is used for a special case where we need an universal pointer type
136 : compatible with all other pointer types. */
137 : bool is_pointer;
138 : /* Nonzero if is_pointer or if one of childs have has_pointer set. */
139 : bool has_pointer;
140 :
141 : /* The children of the alias set. These are not just the immediate
142 : children, but, in fact, all descendants. So, if we have:
143 :
144 : struct T { struct S s; float f; }
145 :
146 : continuing our example above, the children here will be all of
147 : `int', `double', `float', and `struct S'. */
148 : hash_map<alias_set_hash, int> *children;
149 : };
150 :
151 : static int compare_base_symbol_refs (const_rtx, const_rtx,
152 : HOST_WIDE_INT * = NULL);
153 :
154 : /* Query statistics for the different low-level disambiguators.
155 : A high-level query may trigger multiple of them. */
156 :
157 : static struct {
158 : unsigned long long num_alias_zero;
159 : unsigned long long num_same_alias_set;
160 : unsigned long long num_same_objects;
161 : unsigned long long num_volatile;
162 : unsigned long long num_dag;
163 : unsigned long long num_universal;
164 : unsigned long long num_disambiguated;
165 : } alias_stats;
166 :
167 :
168 : /* Set up all info needed to perform alias analysis on memory references. */
169 :
170 : /* Returns the size in bytes of the mode of X. */
171 : #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
172 :
173 : /* Cap the number of passes we make over the insns propagating alias
174 : information through set chains.
175 : ??? 10 is a completely arbitrary choice. This should be based on the
176 : maximum loop depth in the CFG, but we do not have this information
177 : available (even if current_loops _is_ available). */
178 : #define MAX_ALIAS_LOOP_PASSES 10
179 :
180 : /* reg_base_value[N] gives an address to which register N is related.
181 : If all sets after the first add or subtract to the current value
182 : or otherwise modify it so it does not point to a different top level
183 : object, reg_base_value[N] is equal to the address part of the source
184 : of the first set.
185 :
186 : A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
187 : expressions represent three types of base:
188 :
189 : 1. incoming arguments. There is just one ADDRESS to represent all
190 : arguments, since we do not know at this level whether accesses
191 : based on different arguments can alias. The ADDRESS has id 0.
192 :
193 : 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
194 : (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
195 : Each of these rtxes has a separate ADDRESS associated with it,
196 : each with a negative id.
197 :
198 : GCC is (and is required to be) precise in which register it
199 : chooses to access a particular region of stack. We can therefore
200 : assume that accesses based on one of these rtxes do not alias
201 : accesses based on another of these rtxes.
202 :
203 : 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
204 : Each such piece of memory has a separate ADDRESS associated
205 : with it, each with an id greater than 0.
206 :
207 : Accesses based on one ADDRESS do not alias accesses based on other
208 : ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
209 : alias globals either; the ADDRESSes have Pmode to indicate this.
210 : The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
211 : indicate this. */
212 :
213 : static GTY(()) vec<rtx, va_gc> *reg_base_value;
214 : static rtx *new_reg_base_value;
215 :
216 : /* The single VOIDmode ADDRESS that represents all argument bases.
217 : It has id 0. */
218 : static GTY(()) rtx arg_base_value;
219 :
220 : /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
221 : static int unique_id;
222 :
223 : /* We preserve the copy of old array around to avoid amount of garbage
224 : produced. About 8% of garbage produced were attributed to this
225 : array. */
226 : static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
227 :
228 : /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
229 : registers. */
230 : #define UNIQUE_BASE_VALUE_SP -1
231 : #define UNIQUE_BASE_VALUE_ARGP -2
232 : #define UNIQUE_BASE_VALUE_FP -3
233 : #define UNIQUE_BASE_VALUE_HFP -4
234 :
235 : #define static_reg_base_value \
236 : (this_target_rtl->x_static_reg_base_value)
237 :
238 : #define REG_BASE_VALUE(X) \
239 : (REGNO (X) < vec_safe_length (reg_base_value) \
240 : ? (*reg_base_value)[REGNO (X)] : 0)
241 :
242 : /* Vector indexed by N giving the initial (unchanging) value known for
243 : pseudo-register N. This vector is initialized in init_alias_analysis,
244 : and does not change until end_alias_analysis is called. */
245 : static GTY(()) vec<rtx, va_gc> *reg_known_value;
246 :
247 : /* Vector recording for each reg_known_value whether it is due to a
248 : REG_EQUIV note. Future passes (viz., reload) may replace the
249 : pseudo with the equivalent expression and so we account for the
250 : dependences that would be introduced if that happens.
251 :
252 : The REG_EQUIV notes created in assign_parms may mention the arg
253 : pointer, and there are explicit insns in the RTL that modify the
254 : arg pointer. Thus we must ensure that such insns don't get
255 : scheduled across each other because that would invalidate the
256 : REG_EQUIV notes. One could argue that the REG_EQUIV notes are
257 : wrong, but solving the problem in the scheduler will likely give
258 : better code, so we do it here. */
259 : static sbitmap reg_known_equiv_p;
260 :
261 : /* True when scanning insns from the start of the rtl to the
262 : NOTE_INSN_FUNCTION_BEG note. */
263 : static bool copying_arguments;
264 :
265 :
266 : /* The splay-tree used to store the various alias set entries. */
267 : static GTY (()) vec<alias_set_entry *, va_gc> *alias_sets;
268 :
269 : /* Build a decomposed reference object for querying the alias-oracle
270 : from the MEM rtx and store it in *REF.
271 : Returns false if MEM is not suitable for the alias-oracle. */
272 :
273 : static bool
274 466884710 : ao_ref_from_mem (ao_ref *ref, const_rtx mem)
275 : {
276 466884710 : tree expr = MEM_EXPR (mem);
277 466884710 : tree base;
278 :
279 466884710 : if (!expr)
280 : return false;
281 :
282 425582606 : ao_ref_init (ref, expr);
283 :
284 : /* Get the base of the reference and see if we have to reject or
285 : adjust it. */
286 425582606 : base = ao_ref_base (ref);
287 425582606 : if (base == NULL_TREE)
288 : return false;
289 :
290 : /* The tree oracle doesn't like bases that are neither decls
291 : nor indirect references of SSA names. */
292 457621082 : if (!(DECL_P (base)
293 228416292 : || (TREE_CODE (base) == MEM_REF
294 196542168 : && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
295 : || (TREE_CODE (base) == TARGET_MEM_REF
296 31844224 : && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
297 : return false;
298 :
299 425387001 : ref->ref_alias_set = MEM_ALIAS_SET (mem);
300 :
301 : /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
302 : is conservative, so trust it. */
303 425387001 : if (!MEM_OFFSET_KNOWN_P (mem)
304 425387001 : || !MEM_SIZE_KNOWN_P (mem))
305 : return true;
306 :
307 : /* If MEM_OFFSET/MEM_SIZE get us outside of ref->offset/ref->max_size
308 : drop ref->ref. */
309 416151632 : if (maybe_lt (MEM_OFFSET (mem), 0)
310 416151632 : || (ref->max_size_known_p ()
311 369803455 : && maybe_gt ((MEM_OFFSET (mem) + MEM_SIZE (mem)) * BITS_PER_UNIT,
312 : ref->max_size)))
313 44005637 : ref->ref = NULL_TREE;
314 :
315 : /* Refine size and offset we got from analyzing MEM_EXPR by using
316 : MEM_SIZE and MEM_OFFSET. */
317 :
318 416151632 : ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
319 416151632 : ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
320 :
321 : /* The MEM may extend into adjacent fields, so adjust max_size if
322 : necessary. */
323 416151632 : if (ref->max_size_known_p ())
324 369803500 : ref->max_size = upper_bound (ref->max_size, ref->size);
325 :
326 : /* If MEM_OFFSET and MEM_SIZE might get us outside of the base object of
327 : the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
328 416151632 : if (MEM_EXPR (mem) != get_spill_slot_decl (false)
329 416151632 : && (maybe_lt (ref->offset, 0)
330 372251763 : || (DECL_P (ref->base)
331 144770516 : && (DECL_SIZE (ref->base) == NULL_TREE
332 144646233 : || !poly_int_tree_p (DECL_SIZE (ref->base))
333 144646183 : || maybe_lt (wi::to_poly_offset (DECL_SIZE (ref->base)),
334 560797815 : ref->offset + ref->size)))))
335 : return false;
336 :
337 : return true;
338 : }
339 :
340 : /* Query the alias-oracle on whether the two memory rtx X and MEM may
341 : alias. If TBAA_P is set also apply TBAA. Returns true if the
342 : two rtxen may alias, false otherwise. */
343 :
344 : static bool
345 244113182 : rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
346 : {
347 244113182 : ao_ref ref1, ref2;
348 :
349 244113182 : if (!ao_ref_from_mem (&ref1, x)
350 244113182 : || !ao_ref_from_mem (&ref2, mem))
351 : return true;
352 :
353 404964408 : return refs_may_alias_p_1 (&ref1, &ref2,
354 : tbaa_p
355 147372503 : && MEM_ALIAS_SET (x) != 0
356 290395903 : && MEM_ALIAS_SET (mem) != 0);
357 : }
358 :
359 : /* Return true if the ref EARLIER behaves the same as LATER with respect
360 : to TBAA for every memory reference that might follow LATER. */
361 :
362 : bool
363 776111 : refs_same_for_tbaa_p (tree earlier, tree later)
364 : {
365 776111 : ao_ref earlier_ref, later_ref;
366 776111 : ao_ref_init (&earlier_ref, earlier);
367 776111 : ao_ref_init (&later_ref, later);
368 776111 : alias_set_type earlier_set = ao_ref_alias_set (&earlier_ref);
369 776111 : alias_set_type later_set = ao_ref_alias_set (&later_ref);
370 836848 : if (!(earlier_set == later_set
371 60737 : || alias_set_subset_of (later_set, earlier_set)))
372 : return false;
373 771187 : alias_set_type later_base_set = ao_ref_base_alias_set (&later_ref);
374 771187 : alias_set_type earlier_base_set = ao_ref_base_alias_set (&earlier_ref);
375 771187 : return (earlier_base_set == later_base_set
376 771187 : || alias_set_subset_of (later_base_set, earlier_base_set));
377 : }
378 :
379 : /* Similar to refs_same_for_tbaa_p() but for use on MEM rtxs. */
380 : bool
381 141001 : mems_same_for_tbaa_p (rtx earlier, rtx later)
382 : {
383 141001 : gcc_assert (MEM_P (earlier));
384 141001 : gcc_assert (MEM_P (later));
385 :
386 141002 : return ((MEM_ALIAS_SET (earlier) == MEM_ALIAS_SET (later)
387 54787 : || alias_set_subset_of (MEM_ALIAS_SET (later),
388 54787 : MEM_ALIAS_SET (earlier)))
389 278701 : && (!MEM_EXPR (earlier)
390 125560 : || refs_same_for_tbaa_p (MEM_EXPR (earlier), MEM_EXPR (later))));
391 : }
392 :
393 : /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
394 : such an entry, or NULL otherwise. */
395 :
396 : static inline alias_set_entry *
397 280875615 : get_alias_set_entry (alias_set_type alias_set)
398 : {
399 561751230 : return (*alias_sets)[alias_set];
400 : }
401 :
402 : /* Returns true if the alias sets for MEM1 and MEM2 are such that
403 : the two MEMs cannot alias each other. */
404 :
405 : static inline bool
406 248741171 : mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
407 : {
408 248741171 : return (flag_strict_aliasing
409 405458996 : && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
410 156717825 : MEM_ALIAS_SET (mem2)));
411 : }
412 :
413 : /* Return true if the first alias set is a subset of the second. */
414 :
415 : bool
416 435942 : alias_set_subset_of (alias_set_type set1, alias_set_type set2)
417 : {
418 435942 : alias_set_entry *ase2;
419 :
420 : /* Disable TBAA oracle with !flag_strict_aliasing. */
421 435942 : if (!flag_strict_aliasing)
422 : return true;
423 :
424 : /* Everything is a subset of the "aliases everything" set. */
425 362789 : if (set2 == 0)
426 : return true;
427 :
428 : /* Check if set1 is a subset of set2. */
429 287421 : ase2 = get_alias_set_entry (set2);
430 287421 : if (ase2 != 0
431 287421 : && (ase2->has_zero_child
432 260559 : || (ase2->children && ase2->children->get (set1))))
433 : return true;
434 :
435 : /* As a special case we consider alias set of "void *" to be both subset
436 : and superset of every alias set of a pointer. This extra symmetry does
437 : not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
438 : to return true on the following testcase:
439 :
440 : void *ptr;
441 : char **ptr2=(char **)&ptr;
442 : *ptr2 = ...
443 :
444 : Additionally if a set contains universal pointer, we consider every pointer
445 : to be a subset of it, but we do not represent this explicitly - doing so
446 : would require us to update transitive closure each time we introduce new
447 : pointer type. This makes aliasing_component_refs_p to return true
448 : on the following testcase:
449 :
450 : struct a {void *ptr;}
451 : char **ptr = (char **)&a.ptr;
452 : ptr = ...
453 :
454 : This makes void * truly universal pointer type. See pointer handling in
455 : get_alias_set for more details. */
456 59334 : if (ase2 && ase2->has_pointer)
457 : {
458 42940 : alias_set_entry *ase1 = get_alias_set_entry (set1);
459 :
460 42940 : if (ase1 && ase1->is_pointer)
461 : {
462 5857 : alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
463 : /* If one is ptr_type_node and other is pointer, then we consider
464 : them subset of each other. */
465 5857 : if (set1 == voidptr_set || set2 == voidptr_set)
466 5539 : return true;
467 : /* If SET2 contains universal pointer's alias set, then we consider
468 : every (non-universal) pointer. */
469 5133 : if (ase2->children && set1 != voidptr_set
470 5133 : && ase2->children->get (voidptr_set))
471 : return true;
472 : }
473 : }
474 : return false;
475 : }
476 :
477 : /* Return true if the two specified alias sets may conflict. */
478 :
479 : bool
480 328324976 : alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
481 : {
482 328324976 : alias_set_entry *ase1;
483 328324976 : alias_set_entry *ase2;
484 :
485 : /* The easy case. */
486 328324976 : if (alias_sets_must_conflict_p (set1, set2))
487 : return true;
488 :
489 : /* See if the first alias set is a subset of the second. */
490 141408953 : ase1 = get_alias_set_entry (set1);
491 141408953 : if (ase1 != 0
492 141408953 : && ase1->children && ase1->children->get (set2))
493 : {
494 7691148 : ++alias_stats.num_dag;
495 7691148 : return true;
496 : }
497 :
498 : /* Now do the same, but with the alias sets reversed. */
499 133717805 : ase2 = get_alias_set_entry (set2);
500 133717805 : if (ase2 != 0
501 133717805 : && ase2->children && ase2->children->get (set1))
502 : {
503 7222911 : ++alias_stats.num_dag;
504 7222911 : return true;
505 : }
506 :
507 : /* We want void * to be compatible with any other pointer without
508 : really dropping it to alias set 0. Doing so would make it
509 : compatible with all non-pointer types too.
510 :
511 : This is not strictly necessary by the C/C++ language
512 : standards, but avoids common type punning mistakes. In
513 : addition to that, we need the existence of such universal
514 : pointer to implement Fortran's C_PTR type (which is defined as
515 : type compatible with all C pointers). */
516 126494894 : if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
517 : {
518 18345278 : alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
519 :
520 : /* If one of the sets corresponds to universal pointer,
521 : we consider it to conflict with anything that is
522 : or contains pointer. */
523 18345278 : if (set1 == voidptr_set || set2 == voidptr_set)
524 : {
525 3720342 : ++alias_stats.num_universal;
526 4724648 : return true;
527 : }
528 : /* If one of sets is (non-universal) pointer and the other
529 : contains universal pointer, we also get conflict. */
530 14624936 : if (ase1->is_pointer && set2 != voidptr_set
531 14624936 : && ase2->children && ase2->children->get (voidptr_set))
532 : {
533 726504 : ++alias_stats.num_universal;
534 726504 : return true;
535 : }
536 13898432 : if (ase2->is_pointer && set1 != voidptr_set
537 13898432 : && ase1->children && ase1->children->get (voidptr_set))
538 : {
539 277802 : ++alias_stats.num_universal;
540 277802 : return true;
541 : }
542 : }
543 :
544 121770246 : ++alias_stats.num_disambiguated;
545 :
546 : /* The two alias sets are distinct and neither one is the
547 : child of the other. Therefore, they cannot conflict. */
548 121770246 : return false;
549 : }
550 :
551 : /* Return true if the two specified alias sets will always conflict. */
552 :
553 : bool
554 328341225 : alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
555 : {
556 : /* Disable TBAA oracle with !flag_strict_aliasing. */
557 328341225 : if (!flag_strict_aliasing)
558 : return true;
559 323526774 : if (set1 == 0 || set2 == 0)
560 : {
561 112738772 : ++alias_stats.num_alias_zero;
562 112738772 : return true;
563 : }
564 210788002 : if (set1 == set2)
565 : {
566 69378350 : ++alias_stats.num_same_alias_set;
567 69378350 : return true;
568 : }
569 :
570 : return false;
571 : }
572 :
573 : /* Return true if any MEM object of type T1 will always conflict (using the
574 : dependency routines in this file) with any MEM object of type T2.
575 : This is used when allocating temporary storage. If T1 and/or T2 are
576 : NULL_TREE, it means we know nothing about the storage. */
577 :
578 : bool
579 10234058 : objects_must_conflict_p (tree t1, tree t2)
580 : {
581 10234058 : alias_set_type set1, set2;
582 :
583 : /* If neither has a type specified, we don't know if they'll conflict
584 : because we may be using them to store objects of various types, for
585 : example the argument and local variables areas of inlined functions. */
586 10234058 : if (t1 == 0 && t2 == 0)
587 : return false;
588 :
589 : /* If they are the same type, they must conflict. */
590 65087 : if (t1 == t2)
591 : {
592 48868 : ++alias_stats.num_same_objects;
593 48868 : return true;
594 : }
595 : /* Likewise if both are volatile. */
596 16219 : if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
597 : {
598 0 : ++alias_stats.num_volatile;
599 0 : return true;
600 : }
601 :
602 16219 : set1 = t1 ? get_alias_set (t1) : 0;
603 16219 : set2 = t2 ? get_alias_set (t2) : 0;
604 :
605 : /* We can't use alias_sets_conflict_p because we must make sure
606 : that every subtype of t1 will conflict with every subtype of
607 : t2 for which a pair of subobjects of these respective subtypes
608 : overlaps on the stack. */
609 16219 : return alias_sets_must_conflict_p (set1, set2);
610 : }
611 :
612 : /* Return true if T is an end of the access path which can be used
613 : by type based alias oracle. */
614 :
615 : bool
616 524089886 : ends_tbaa_access_path_p (const_tree t)
617 : {
618 524089886 : switch (TREE_CODE (t))
619 : {
620 391422133 : case COMPONENT_REF:
621 391422133 : if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
622 : return true;
623 : /* Permit type-punning when accessing a union, provided the access
624 : is directly through the union. For example, this code does not
625 : permit taking the address of a union member and then storing
626 : through it. Even the type-punning allowed here is a GCC
627 : extension, albeit a common and useful one; the C standard says
628 : that such accesses have implementation-defined behavior. */
629 387747896 : else if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
630 : return true;
631 : break;
632 :
633 127125931 : case ARRAY_REF:
634 127125931 : case ARRAY_RANGE_REF:
635 127125931 : if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
636 : return true;
637 : break;
638 :
639 : case REALPART_EXPR:
640 : case IMAGPART_EXPR:
641 : break;
642 :
643 : case BIT_FIELD_REF:
644 : case VIEW_CONVERT_EXPR:
645 : /* Bitfields and casts are never addressable. */
646 : return true;
647 0 : break;
648 :
649 0 : default:
650 0 : gcc_unreachable ();
651 : }
652 : return false;
653 : }
654 :
655 : /* Return the outermost parent of component present in the chain of
656 : component references handled by get_inner_reference in T with the
657 : following property:
658 : - the component is non-addressable
659 : or NULL_TREE if no such parent exists. In the former cases, the alias
660 : set of this parent is the alias set that must be used for T itself. */
661 :
662 : tree
663 1314232672 : component_uses_parent_alias_set_from (const_tree t)
664 : {
665 1314232672 : const_tree found = NULL_TREE;
666 :
667 1806898565 : while (handled_component_p (t))
668 : {
669 492665893 : if (ends_tbaa_access_path_p (t))
670 15121276 : found = t;
671 :
672 492665893 : t = TREE_OPERAND (t, 0);
673 : }
674 :
675 1314232672 : if (found)
676 14856128 : return TREE_OPERAND (found, 0);
677 :
678 : return NULL_TREE;
679 : }
680 :
681 :
682 : /* Return whether the pointer-type T effective for aliasing may
683 : access everything and thus the reference has to be assigned
684 : alias-set zero. */
685 :
686 : static bool
687 1047317013 : ref_all_alias_ptr_type_p (const_tree t)
688 : {
689 1047317013 : return (VOID_TYPE_P (TREE_TYPE (t))
690 1047317013 : || TYPE_REF_CAN_ALIAS_ALL (t));
691 : }
692 :
693 : /* Return the alias set for the memory pointed to by T, which may be
694 : either a type or an expression. Return -1 if there is nothing
695 : special about dereferencing T. */
696 :
697 : static alias_set_type
698 151731761 : get_deref_alias_set_1 (tree t)
699 : {
700 : /* All we care about is the type. */
701 151731761 : if (! TYPE_P (t))
702 14 : t = TREE_TYPE (t);
703 :
704 : /* If we have an INDIRECT_REF via a void pointer, we don't
705 : know anything about what that might alias. Likewise if the
706 : pointer is marked that way. */
707 151731761 : if (ref_all_alias_ptr_type_p (t))
708 28897374 : return 0;
709 :
710 : return -1;
711 : }
712 :
713 : /* Return the alias set for the memory pointed to by T, which may be
714 : either a type or an expression. */
715 :
716 : alias_set_type
717 225214795 : get_deref_alias_set (tree t)
718 : {
719 : /* If we're not doing any alias analysis, just assume everything
720 : aliases everything else. */
721 225214795 : if (!flag_strict_aliasing)
722 : return 0;
723 :
724 151731761 : alias_set_type set = get_deref_alias_set_1 (t);
725 :
726 : /* Fall back to the alias-set of the pointed-to type. */
727 151731761 : if (set == -1)
728 : {
729 122834387 : if (! TYPE_P (t))
730 14 : t = TREE_TYPE (t);
731 122834387 : set = get_alias_set (TREE_TYPE (t));
732 : }
733 :
734 : return set;
735 : }
736 :
737 : /* Return the pointer-type relevant for TBAA purposes from the
738 : memory reference tree *T or NULL_TREE in which case *T is
739 : adjusted to point to the outermost component reference that
740 : can be used for assigning an alias set. */
741 :
742 : tree
743 1372863830 : reference_alias_ptr_type_1 (tree *t)
744 : {
745 1372863830 : tree inner;
746 :
747 : /* Get the base object of the reference. */
748 1372863830 : inner = *t;
749 1851551414 : while (handled_component_p (inner))
750 : {
751 : /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
752 : the type of any component references that wrap it to
753 : determine the alias-set. */
754 478687584 : if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
755 1691230 : *t = TREE_OPERAND (inner, 0);
756 478687584 : inner = TREE_OPERAND (inner, 0);
757 : }
758 :
759 : /* Handle pointer dereferences here, they can override the
760 : alias-set. */
761 1372863830 : if (INDIRECT_REF_P (inner)
762 1372863830 : && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
763 0 : return TREE_TYPE (TREE_OPERAND (inner, 0));
764 1372863830 : else if (TREE_CODE (inner) == TARGET_MEM_REF)
765 56990015 : return TREE_TYPE (TMR_OFFSET (inner));
766 1315873815 : else if (TREE_CODE (inner) == MEM_REF
767 1315873815 : && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
768 32132087 : return TREE_TYPE (TREE_OPERAND (inner, 1));
769 :
770 : /* Pick up the outermost object that we could have a pointer to. */
771 1283741728 : tree tem = component_uses_parent_alias_set_from (*t);
772 1283741728 : if (tem)
773 13721831 : *t = tem;
774 :
775 : /* If the innermost reference is a MEM_REF that has a
776 : conversion embedded treat it like a VIEW_CONVERT_EXPR above,
777 : using the memory access type for determining the alias-set. */
778 1283741728 : if (view_converted_memref_p (inner))
779 : {
780 136037447 : tree alias_ptrtype = TREE_TYPE (TREE_OPERAND (inner, 1));
781 : /* Unless we have the (aggregate) effective type of the access
782 : somewhere on the access path. If we have for example
783 : (&a->elts[i])->l.len exposed by abstraction we'd see
784 : MEM <A> [(B *)a].elts[i].l.len and we can use the alias set
785 : of 'len' when typeof (MEM <A> [(B *)a].elts[i]) == B for
786 : example. See PR111715. */
787 136037447 : tree inner = *t;
788 136037447 : while (handled_component_p (inner)
789 136810903 : && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
790 2015946 : != TYPE_MAIN_VARIANT (TREE_TYPE (alias_ptrtype))))
791 773456 : inner = TREE_OPERAND (inner, 0);
792 136037447 : if (TREE_CODE (inner) == MEM_REF)
793 134794957 : return alias_ptrtype;
794 : }
795 :
796 : return NULL_TREE;
797 : }
798 :
799 : /* Return the pointer-type relevant for TBAA purposes from the
800 : gimple memory reference tree T. This is the type to be used for
801 : the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
802 : and guarantees that get_alias_set will return the same alias
803 : set for T and the replacement. */
804 :
805 : tree
806 10108454 : reference_alias_ptr_type (tree t)
807 : {
808 : /* If the frontend assigns this alias-set zero, preserve that. */
809 10108454 : if (lang_hooks.get_alias_set (t) == 0)
810 0 : return ptr_type_node;
811 :
812 10108454 : tree ptype = reference_alias_ptr_type_1 (&t);
813 : /* If there is a given pointer type for aliasing purposes, return it. */
814 10108454 : if (ptype != NULL_TREE)
815 : return ptype;
816 :
817 : /* Otherwise build one from the outermost component reference we
818 : may use. */
819 9344963 : if (TREE_CODE (t) == MEM_REF
820 9344963 : || TREE_CODE (t) == TARGET_MEM_REF)
821 1212336 : return TREE_TYPE (TREE_OPERAND (t, 1));
822 : else
823 8132627 : return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
824 : }
825 :
826 : /* Return whether the pointer-types T1 and T2 used to determine
827 : two alias sets of two references will yield the same answer
828 : from get_deref_alias_set. */
829 :
830 : bool
831 16029114 : alias_ptr_types_compatible_p (tree t1, tree t2)
832 : {
833 16029114 : if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
834 : return true;
835 :
836 2469326 : if (ref_all_alias_ptr_type_p (t1)
837 2469326 : || ref_all_alias_ptr_type_p (t2))
838 : return false;
839 :
840 : /* This function originally abstracts from simply comparing
841 : get_deref_alias_set so that we are sure this still computes
842 : the same result after LTO type merging is applied.
843 : When in LTO type merging is done we can actually do this compare.
844 : */
845 2413921 : if (in_lto_p)
846 5996 : return get_deref_alias_set (t1) == get_deref_alias_set (t2);
847 : else
848 2407925 : return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
849 2407925 : == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
850 : }
851 :
852 : /* Create empty alias set entry. */
853 :
854 : alias_set_entry *
855 1260346 : init_alias_set_entry (alias_set_type set)
856 : {
857 1260346 : alias_set_entry *ase = ggc_alloc<alias_set_entry> ();
858 1260346 : ase->alias_set = set;
859 1260346 : ase->children = NULL;
860 1260346 : ase->has_zero_child = false;
861 1260346 : ase->is_pointer = false;
862 1260346 : ase->has_pointer = false;
863 1260346 : gcc_checking_assert (!get_alias_set_entry (set));
864 1260346 : (*alias_sets)[set] = ase;
865 1260346 : return ase;
866 : }
867 :
868 : /* Return the alias set for T, which may be either a type or an
869 : expression. Call language-specific routine for help, if needed. */
870 :
871 : alias_set_type
872 1690290392 : get_alias_set (tree t)
873 : {
874 1690290392 : alias_set_type set;
875 :
876 : /* We cannot give up with -fno-strict-aliasing because we need to build
877 : proper type representations for possible functions which are built with
878 : -fstrict-aliasing. */
879 :
880 : /* return 0 if this or its type is an error. */
881 1690290392 : if (t == error_mark_node
882 1690290392 : || (! TYPE_P (t)
883 1362410186 : && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
884 : return 0;
885 :
886 : /* We can be passed either an expression or a type. This and the
887 : language-specific routine may make mutually-recursive calls to each other
888 : to figure out what to do. At each juncture, we see if this is a tree
889 : that the language may need to handle specially. First handle things that
890 : aren't types. */
891 1690290392 : if (! TYPE_P (t))
892 : {
893 : /* Give the language a chance to do something with this tree
894 : before we look at it. */
895 1362410186 : STRIP_NOPS (t);
896 1362410186 : set = lang_hooks.get_alias_set (t);
897 1362410186 : if (set != -1)
898 : return set;
899 :
900 : /* Get the alias pointer-type to use or the outermost object
901 : that we could have a pointer to. */
902 1362410186 : tree ptype = reference_alias_ptr_type_1 (&t);
903 1362410186 : if (ptype != NULL)
904 223149473 : return get_deref_alias_set (ptype);
905 :
906 : /* If we've already determined the alias set for a decl, just return
907 : it. This is necessary for C++ anonymous unions, whose component
908 : variables don't look like union members (boo!). */
909 1139260713 : if (VAR_P (t)
910 1139260713 : && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
911 163936782 : return MEM_ALIAS_SET (DECL_RTL (t));
912 :
913 : /* Now all we care about is the type. */
914 1057292322 : t = TREE_TYPE (t);
915 : }
916 :
917 : /* Variant qualifiers don't affect the alias set, so get the main
918 : variant. */
919 1385172528 : t = TYPE_MAIN_VARIANT (t);
920 :
921 1385172528 : if (AGGREGATE_TYPE_P (t)
922 1385172528 : && TYPE_TYPELESS_STORAGE (t))
923 : return 0;
924 :
925 : /* Always use the canonical type as well. If this is a type that
926 : requires structural comparisons to identify compatible types
927 : use alias set zero. */
928 1265503421 : if (TYPE_STRUCTURAL_EQUALITY_P (t))
929 : {
930 : /* Allow the language to specify another alias set for this
931 : type. */
932 261877176 : set = lang_hooks.get_alias_set (t);
933 261877176 : if (set != -1)
934 : return set;
935 : /* Handle structure type equality for pointer types, arrays and vectors.
936 : This is easy to do, because the code below ignores canonical types on
937 : these anyway. This is important for LTO, where TYPE_CANONICAL for
938 : pointers cannot be meaningfully computed by the frontend. */
939 261461002 : if (canonical_type_used_p (t))
940 : {
941 : /* In LTO we set canonical types for all types where it makes
942 : sense to do so. Double check we did not miss some type. */
943 178468787 : gcc_checking_assert (!in_lto_p || !type_with_alias_set_p (t));
944 : return 0;
945 : }
946 : }
947 : else
948 : {
949 1003626245 : t = TYPE_CANONICAL (t);
950 1003626245 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
951 1003626245 : if (t != TYPE_MAIN_VARIANT (t))
952 : {
953 2 : t = TYPE_MAIN_VARIANT (t);
954 2 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
955 : }
956 : }
957 :
958 : /* If this is a type with a known alias set, return it. */
959 1086618460 : gcc_checking_assert (t == TYPE_MAIN_VARIANT (t));
960 1086618460 : if (TYPE_ALIAS_SET_KNOWN_P (t))
961 1020959135 : return TYPE_ALIAS_SET (t);
962 :
963 : /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
964 65659325 : if (!COMPLETE_TYPE_P (t))
965 : {
966 : /* For arrays with unknown size the conservative answer is the
967 : alias set of the element type. */
968 12909409 : if (TREE_CODE (t) == ARRAY_TYPE)
969 12854645 : return get_alias_set (TREE_TYPE (t));
970 :
971 : /* But return zero as a conservative answer for incomplete types. */
972 : return 0;
973 : }
974 :
975 : /* See if the language has special handling for this type. */
976 52749916 : set = lang_hooks.get_alias_set (t);
977 52749916 : if (set != -1)
978 : return set;
979 :
980 : /* There are no objects of FUNCTION_TYPE, so there's no point in
981 : using up an alias set for them. (There are, of course, pointers
982 : and references to functions, but that's different.) */
983 2685043 : else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
984 : set = 0;
985 :
986 : /* Unless the language specifies otherwise, let vector types alias
987 : their components. This avoids some nasty type punning issues in
988 : normal usage. And indeed lets vectors be treated more like an
989 : array slice. */
990 2680669 : else if (TREE_CODE (t) == VECTOR_TYPE)
991 98858 : set = get_alias_set (TREE_TYPE (t));
992 :
993 : /* Unless the language specifies otherwise, treat array types the
994 : same as their components. This avoids the asymmetry we get
995 : through recording the components. Consider accessing a
996 : character(kind=1) through a reference to a character(kind=1)[1:1].
997 : Or consider if we want to assign integer(kind=4)[0:D.1387] and
998 : integer(kind=4)[4] the same alias set or not.
999 : Just be pragmatic here and make sure the array and its element
1000 : type get the same alias set assigned. */
1001 2581811 : else if (TREE_CODE (t) == ARRAY_TYPE
1002 2581811 : && (!TYPE_NONALIASED_COMPONENT (t)
1003 0 : || TYPE_STRUCTURAL_EQUALITY_P (t)))
1004 414715 : set = get_alias_set (TREE_TYPE (t));
1005 :
1006 : /* From the former common C and C++ langhook implementation:
1007 :
1008 : Unfortunately, there is no canonical form of a pointer type.
1009 : In particular, if we have `typedef int I', then `int *', and
1010 : `I *' are different types. So, we have to pick a canonical
1011 : representative. We do this below.
1012 :
1013 : Technically, this approach is actually more conservative that
1014 : it needs to be. In particular, `const int *' and `int *'
1015 : should be in different alias sets, according to the C and C++
1016 : standard, since their types are not the same, and so,
1017 : technically, an `int **' and `const int **' cannot point at
1018 : the same thing.
1019 :
1020 : But, the standard is wrong. In particular, this code is
1021 : legal C++:
1022 :
1023 : int *ip;
1024 : int **ipp = &ip;
1025 : const int* const* cipp = ipp;
1026 : And, it doesn't make sense for that to be legal unless you
1027 : can dereference IPP and CIPP. So, we ignore cv-qualifiers on
1028 : the pointed-to types. This issue has been reported to the
1029 : C++ committee.
1030 :
1031 : For this reason go to canonical type of the unqalified pointer type.
1032 : Until GCC 6 this code set all pointers sets to have alias set of
1033 : ptr_type_node but that is a bad idea, because it prevents disabiguations
1034 : in between pointers. For Firefox this accounts about 20% of all
1035 : disambiguations in the program. */
1036 2167096 : else if (POINTER_TYPE_P (t) && t != ptr_type_node)
1037 : {
1038 695314 : tree p;
1039 695314 : auto_vec <bool, 8> reference;
1040 :
1041 : /* Unnest all pointers and references.
1042 : We also want to make pointer to array/vector equivalent to pointer to
1043 : its element (see the reasoning above). Skip all those types, too. */
1044 695314 : for (p = t; POINTER_TYPE_P (p)
1045 763015 : || (TREE_CODE (p) == ARRAY_TYPE
1046 66400 : && (!TYPE_NONALIASED_COMPONENT (p)
1047 0 : || !COMPLETE_TYPE_P (p)
1048 0 : || TYPE_STRUCTURAL_EQUALITY_P (p)))
1049 2232809 : || TREE_CODE (p) == VECTOR_TYPE;
1050 840880 : p = TREE_TYPE (p))
1051 : {
1052 : /* Ada supports recursive pointers. Instead of doing recursion
1053 : check, just give up once the preallocated space of 8 elements
1054 : is up. In this case just punt to void * alias set. */
1055 840947 : if (reference.length () == 8)
1056 : {
1057 67 : p = ptr_type_node;
1058 67 : break;
1059 : }
1060 840880 : if (TREE_CODE (p) == REFERENCE_TYPE)
1061 : /* In LTO we want languages that use references to be compatible
1062 : with languages that use pointers. */
1063 53635 : reference.safe_push (true && !in_lto_p);
1064 840880 : if (TREE_CODE (p) == POINTER_TYPE)
1065 719516 : reference.safe_push (false);
1066 : }
1067 695314 : p = TYPE_MAIN_VARIANT (p);
1068 :
1069 : /* In LTO for C++ programs we can turn incomplete types to complete
1070 : using ODR name lookup. */
1071 695314 : if (in_lto_p && TYPE_STRUCTURAL_EQUALITY_P (p) && odr_type_p (p))
1072 : {
1073 292 : p = prevailing_odr_type (p);
1074 292 : gcc_checking_assert (TYPE_MAIN_VARIANT (p) == p);
1075 : }
1076 :
1077 : /* Make void * compatible with char * and also void **.
1078 : Programs are commonly violating TBAA by this.
1079 :
1080 : We also make void * to conflict with every pointer
1081 : (see record_component_aliases) and thus it is safe it to use it for
1082 : pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
1083 695314 : if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
1084 136159 : set = get_alias_set (ptr_type_node);
1085 : else
1086 : {
1087 : /* Rebuild pointer type starting from canonical types using
1088 : unqualified pointers and references only. This way all such
1089 : pointers will have the same alias set and will conflict with
1090 : each other.
1091 :
1092 : Most of time we already have pointers or references of a given type.
1093 : If not we build new one just to be sure that if someone later
1094 : (probably only middle-end can, as we should assign all alias
1095 : classes only after finishing translation unit) builds the pointer
1096 : type, the canonical type will match. */
1097 559155 : p = TYPE_CANONICAL (p);
1098 1170726 : while (!reference.is_empty ())
1099 : {
1100 611571 : if (reference.pop ())
1101 51561 : p = build_reference_type (p);
1102 : else
1103 560010 : p = build_pointer_type (p);
1104 611571 : gcc_checking_assert (p == TYPE_MAIN_VARIANT (p));
1105 : /* build_pointer_type should always return the canonical type.
1106 : For LTO TYPE_CANONICAL may be NULL, because we do not compute
1107 : them. Be sure that frontends do not glob canonical types of
1108 : pointers in unexpected way and that p == TYPE_CANONICAL (p)
1109 : in all other cases. */
1110 611571 : gcc_checking_assert (!TYPE_CANONICAL (p)
1111 : || p == TYPE_CANONICAL (p));
1112 : }
1113 :
1114 : /* Assign the alias set to both p and t.
1115 : We cannot call get_alias_set (p) here as that would trigger
1116 : infinite recursion when p == t. In other cases it would just
1117 : trigger unnecessary legwork of rebuilding the pointer again. */
1118 559155 : gcc_checking_assert (p == TYPE_MAIN_VARIANT (p));
1119 559155 : if (TYPE_ALIAS_SET_KNOWN_P (p))
1120 71776 : set = TYPE_ALIAS_SET (p);
1121 : else
1122 : {
1123 487379 : set = new_alias_set ();
1124 487379 : TYPE_ALIAS_SET (p) = set;
1125 : }
1126 : }
1127 695314 : }
1128 : /* Alias set of ptr_type_node is special and serve as universal pointer which
1129 : is TBAA compatible with every other pointer type. Be sure we have the
1130 : alias set built even for LTO which otherwise keeps all TYPE_CANONICAL
1131 : of pointer types NULL. */
1132 1471782 : else if (t == ptr_type_node)
1133 67258 : set = new_alias_set ();
1134 :
1135 : /* Otherwise make a new alias set for this type. */
1136 : else
1137 : {
1138 : /* Each canonical type gets its own alias set, so canonical types
1139 : shouldn't form a tree. It doesn't really matter for types
1140 : we handle specially above, so only check it where it possibly
1141 : would result in a bogus alias set. */
1142 1404524 : gcc_checking_assert (TYPE_CANONICAL (t) == t);
1143 :
1144 1404524 : set = new_alias_set ();
1145 : }
1146 :
1147 2685043 : TYPE_ALIAS_SET (t) = set;
1148 :
1149 : /* If this is an aggregate type or a complex type, we must record any
1150 : component aliasing information. */
1151 2685043 : if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1152 1219868 : record_component_aliases (t);
1153 :
1154 : /* We treat pointer types specially in alias_set_subset_of. */
1155 2685043 : if (POINTER_TYPE_P (t) && set)
1156 : {
1157 762572 : alias_set_entry *ase = get_alias_set_entry (set);
1158 762572 : if (!ase)
1159 554637 : ase = init_alias_set_entry (set);
1160 762572 : ase->is_pointer = true;
1161 762572 : ase->has_pointer = true;
1162 : }
1163 :
1164 : return set;
1165 : }
1166 :
1167 : /* Return a brand-new alias set. */
1168 :
1169 : alias_set_type
1170 2024471 : new_alias_set (void)
1171 : {
1172 2024471 : if (alias_sets == 0)
1173 217816 : vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1174 2024471 : vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1175 2024471 : return alias_sets->length () - 1;
1176 : }
1177 :
1178 : /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1179 : not everything that aliases SUPERSET also aliases SUBSET. For example,
1180 : in C, a store to an `int' can alias a load of a structure containing an
1181 : `int', and vice versa. But it can't alias a load of a 'double' member
1182 : of the same structure. Here, the structure would be the SUPERSET and
1183 : `int' the SUBSET. This relationship is also described in the comment at
1184 : the beginning of this file.
1185 :
1186 : This function should be called only once per SUPERSET/SUBSET pair.
1187 :
1188 : It is illegal for SUPERSET to be zero; everything is implicitly a
1189 : subset of alias set zero. */
1190 :
1191 : void
1192 2115769 : record_alias_subset (alias_set_type superset, alias_set_type subset)
1193 : {
1194 2115769 : alias_set_entry *superset_entry;
1195 2115769 : alias_set_entry *subset_entry;
1196 :
1197 : /* It is possible in complex type situations for both sets to be the same,
1198 : in which case we can ignore this operation. */
1199 2115769 : if (superset == subset)
1200 : return;
1201 :
1202 2115769 : gcc_assert (superset);
1203 :
1204 2115769 : superset_entry = get_alias_set_entry (superset);
1205 2115769 : if (superset_entry == 0)
1206 : {
1207 : /* Create an entry for the SUPERSET, so that we have a place to
1208 : attach the SUBSET. */
1209 705709 : superset_entry = init_alias_set_entry (superset);
1210 : }
1211 :
1212 2115769 : if (subset == 0)
1213 86157 : superset_entry->has_zero_child = 1;
1214 : else
1215 : {
1216 2029612 : if (!superset_entry->children)
1217 694865 : superset_entry->children
1218 694865 : = hash_map<alias_set_hash, int>::create_ggc (64);
1219 :
1220 : /* Enter the SUBSET itself as a child of the SUPERSET. If it was
1221 : already there we're done. */
1222 2029612 : if (superset_entry->children->put (subset, 0))
1223 : return;
1224 :
1225 1279809 : subset_entry = get_alias_set_entry (subset);
1226 : /* If there is an entry for the subset, enter all of its children
1227 : (if they are not already present) as children of the SUPERSET. */
1228 1279809 : if (subset_entry)
1229 : {
1230 751066 : if (subset_entry->has_zero_child)
1231 16654 : superset_entry->has_zero_child = true;
1232 751066 : if (subset_entry->has_pointer)
1233 596630 : superset_entry->has_pointer = true;
1234 :
1235 751066 : if (subset_entry->children)
1236 : {
1237 326580 : hash_map<alias_set_hash, int>::iterator iter
1238 326580 : = subset_entry->children->begin ();
1239 301749487 : for (; iter != subset_entry->children->end (); ++iter)
1240 301096327 : superset_entry->children->put ((*iter).first, (*iter).second);
1241 : }
1242 : }
1243 : }
1244 : }
1245 :
1246 : /* Record that component types of TYPE, if any, are part of SUPERSET for
1247 : aliasing purposes. For record types, we only record component types
1248 : for fields that are not marked non-addressable. For array types, we
1249 : only record the component type if it is not marked non-aliased. */
1250 :
1251 : void
1252 1305944 : record_component_aliases (tree type, alias_set_type superset)
1253 : {
1254 1305944 : tree field;
1255 :
1256 1305944 : if (superset == 0)
1257 : return;
1258 :
1259 1258229 : switch (TREE_CODE (type))
1260 : {
1261 799704 : case RECORD_TYPE:
1262 799704 : case UNION_TYPE:
1263 799704 : case QUAL_UNION_TYPE:
1264 799704 : {
1265 : /* LTO non-ODR type merging does not make any difference between
1266 : component pointer types. We may have
1267 :
1268 : struct foo {int *a;};
1269 :
1270 : as TYPE_CANONICAL of
1271 :
1272 : struct bar {float *a;};
1273 :
1274 : Because accesses to int * and float * do not alias, we would get
1275 : false negative when accessing the same memory location by
1276 : float ** and bar *. We thus record the canonical type as:
1277 :
1278 : struct {void *a;};
1279 :
1280 : void * is special cased and works as a universal pointer type.
1281 : Accesses to it conflicts with accesses to any other pointer
1282 : type. */
1283 799704 : bool void_pointers = in_lto_p
1284 799704 : && (!odr_type_p (type)
1285 6615 : || !odr_based_tbaa_p (type));
1286 15060448 : for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1287 14260744 : if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1288 : {
1289 2110304 : tree t = TREE_TYPE (field);
1290 2110304 : if (void_pointers)
1291 : {
1292 : /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1293 : element type and that type has to be normalized to void *,
1294 : too, in the case it is a pointer. */
1295 27766 : while (!canonical_type_used_p (t) && !POINTER_TYPE_P (t))
1296 : {
1297 1817 : gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
1298 1817 : t = TREE_TYPE (t);
1299 : }
1300 24132 : if (POINTER_TYPE_P (t))
1301 6891 : t = ptr_type_node;
1302 17241 : else if (flag_checking)
1303 17241 : gcc_checking_assert (get_alias_set (t)
1304 : == get_alias_set (TREE_TYPE (field)));
1305 : }
1306 :
1307 2110304 : alias_set_type set = get_alias_set (t);
1308 2110304 : record_alias_subset (superset, set);
1309 : /* If the field has alias-set zero make sure to still record
1310 : any components of it. This makes sure that for
1311 : struct A {
1312 : struct B {
1313 : int i;
1314 : char c[4];
1315 : } b;
1316 : };
1317 : in C++ even though 'B' has alias-set zero because
1318 : TYPE_TYPELESS_STORAGE is set, 'A' has the alias-set of
1319 : 'int' as subset. */
1320 2110304 : if (set == 0)
1321 86076 : record_component_aliases (t, superset);
1322 : }
1323 : }
1324 : break;
1325 :
1326 5465 : case COMPLEX_TYPE:
1327 5465 : record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1328 5465 : break;
1329 :
1330 : /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1331 : element type. */
1332 :
1333 : default:
1334 : break;
1335 : }
1336 : }
1337 :
1338 : /* Record that component types of TYPE, if any, are part of that type for
1339 : aliasing purposes. For record types, we only record component types
1340 : for fields that are not marked non-addressable. For array types, we
1341 : only record the component type if it is not marked non-aliased. */
1342 :
1343 : void
1344 1219868 : record_component_aliases (tree type)
1345 : {
1346 1219868 : alias_set_type superset = get_alias_set (type);
1347 1219868 : record_component_aliases (type, superset);
1348 1219868 : }
1349 :
1350 :
1351 : /* Allocate an alias set for use in storing and reading from the varargs
1352 : spill area. */
1353 :
1354 : static GTY(()) alias_set_type varargs_set = -1;
1355 :
1356 : alias_set_type
1357 21178 : get_varargs_alias_set (void)
1358 : {
1359 : #if 1
1360 : /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1361 : varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1362 : consistently use the varargs alias set for loads from the varargs
1363 : area. So don't use it anywhere. */
1364 21178 : return 0;
1365 : #else
1366 : if (varargs_set == -1)
1367 : varargs_set = new_alias_set ();
1368 :
1369 : return varargs_set;
1370 : #endif
1371 : }
1372 :
1373 : /* Likewise, but used for the fixed portions of the frame, e.g., register
1374 : save areas. */
1375 :
1376 : static GTY(()) alias_set_type frame_set = -1;
1377 :
1378 : alias_set_type
1379 1233774 : get_frame_alias_set (void)
1380 : {
1381 1233774 : if (frame_set == -1)
1382 25476 : frame_set = new_alias_set ();
1383 :
1384 1233774 : return frame_set;
1385 : }
1386 :
1387 : /* Create a new, unique base with id ID. */
1388 :
1389 : static rtx
1390 2071867 : unique_base_value (HOST_WIDE_INT id)
1391 : {
1392 2138137 : return gen_rtx_ADDRESS (Pmode, id);
1393 : }
1394 :
1395 : /* Return true if accesses based on any other base value cannot alias
1396 : those based on X. */
1397 :
1398 : static bool
1399 174023447 : unique_base_value_p (rtx x)
1400 : {
1401 199524011 : return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1402 : }
1403 :
1404 : /* Inside SRC, the source of a SET, find a base address. */
1405 :
1406 : static rtx
1407 443075454 : find_base_value (rtx src)
1408 : {
1409 528423412 : unsigned int regno;
1410 528423412 : scalar_int_mode int_mode;
1411 :
1412 : #if defined (FIND_BASE_TERM)
1413 : /* Try machine-dependent ways to find the base term. */
1414 528423412 : src = FIND_BASE_TERM (src);
1415 : #endif
1416 :
1417 528423412 : switch (GET_CODE (src))
1418 : {
1419 : case SYMBOL_REF:
1420 : case LABEL_REF:
1421 : return src;
1422 :
1423 168782527 : case REG:
1424 168782527 : regno = REGNO (src);
1425 : /* At the start of a function, argument registers have known base
1426 : values which may be lost later. Returning an ADDRESS
1427 : expression here allows optimization based on argument values
1428 : even when the argument registers are used for other purposes. */
1429 168782527 : if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1430 18999239 : return new_reg_base_value[regno];
1431 :
1432 : /* If a pseudo has a known base value, return it. Do not do this
1433 : for non-fixed hard regs since it can result in a circular
1434 : dependency chain for registers which have values at function entry.
1435 :
1436 : The test above is not sufficient because the scheduler may move
1437 : a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1438 88250953 : if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1439 121438710 : && regno < vec_safe_length (reg_base_value))
1440 : {
1441 : /* If we're inside init_alias_analysis, use new_reg_base_value
1442 : to reduce the number of relaxation iterations. */
1443 121438710 : if (new_reg_base_value && new_reg_base_value[regno]
1444 81436709 : && DF_REG_DEF_COUNT (regno) == 1)
1445 : return new_reg_base_value[regno];
1446 :
1447 85624774 : if ((*reg_base_value)[regno])
1448 : return (*reg_base_value)[regno];
1449 : }
1450 :
1451 : return 0;
1452 :
1453 103357877 : case MEM:
1454 : /* Check for an argument passed in memory. Only record in the
1455 : copying-arguments block; it is too hard to track changes
1456 : otherwise. */
1457 103357877 : if (copying_arguments
1458 3907416 : && (XEXP (src, 0) == arg_pointer_rtx
1459 2746832 : || (GET_CODE (XEXP (src, 0)) == PLUS
1460 2731131 : && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1461 2830908 : return arg_base_value;
1462 : return 0;
1463 :
1464 1025845 : case CONST:
1465 1025845 : src = XEXP (src, 0);
1466 1025845 : if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1467 : break;
1468 :
1469 : /* fall through */
1470 :
1471 107920995 : case PLUS:
1472 107920995 : case MINUS:
1473 107920995 : {
1474 107920995 : rtx src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1475 :
1476 : /* If either operand is a CONST_INT, then the other is the base. */
1477 107920995 : if (CONST_INT_P (src_1))
1478 : return find_base_value (src_0);
1479 24026905 : else if (CONST_INT_P (src_0))
1480 : return find_base_value (src_1);
1481 :
1482 : return 0;
1483 : }
1484 :
1485 0 : case LO_SUM:
1486 : /* The standard form is (lo_sum reg sym) so look only at the
1487 : second operand. */
1488 0 : return find_base_value (XEXP (src, 1));
1489 :
1490 5793464 : case AND:
1491 : /* Look through aligning ANDs. And AND with zero or one with
1492 : the LSB set isn't one (see for example PR92462). */
1493 5793464 : if (CONST_INT_P (XEXP (src, 1))
1494 3658075 : && INTVAL (XEXP (src, 1)) != 0
1495 3657859 : && (INTVAL (XEXP (src, 1)) & 1) == 0)
1496 1377323 : return find_base_value (XEXP (src, 0));
1497 : return 0;
1498 :
1499 13483 : case TRUNCATE:
1500 : /* As we do not know which address space the pointer is referring to, we can
1501 : handle this only if the target does not support different pointer or
1502 : address modes depending on the address space. */
1503 13483 : if (!target_default_pointer_address_modes_p ())
1504 : break;
1505 13483 : if (!is_a <scalar_int_mode> (GET_MODE (src), &int_mode)
1506 0 : || GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (Pmode))
1507 : break;
1508 : /* Fall through. */
1509 0 : case HIGH:
1510 0 : case PRE_INC:
1511 0 : case PRE_DEC:
1512 0 : case POST_INC:
1513 0 : case POST_DEC:
1514 0 : case PRE_MODIFY:
1515 0 : case POST_MODIFY:
1516 0 : return find_base_value (XEXP (src, 0));
1517 :
1518 11119387 : case ZERO_EXTEND:
1519 11119387 : case SIGN_EXTEND: /* used for NT/Alpha pointers */
1520 : /* As we do not know which address space the pointer is referring to, we can
1521 : handle this only if the target does not support different pointer or
1522 : address modes depending on the address space. */
1523 11119387 : if (!target_default_pointer_address_modes_p ())
1524 : break;
1525 :
1526 11119387 : {
1527 11119387 : rtx temp = find_base_value (XEXP (src, 0));
1528 :
1529 11119387 : if (temp != 0 && CONSTANT_P (temp))
1530 512 : temp = convert_memory_address (Pmode, temp);
1531 :
1532 : return temp;
1533 : }
1534 :
1535 : default:
1536 : break;
1537 : }
1538 :
1539 : return 0;
1540 : }
1541 :
1542 : /* Called from init_alias_analysis indirectly through note_stores,
1543 : or directly if DEST is a register with a REG_NOALIAS note attached.
1544 : SET is null in the latter case. */
1545 :
1546 : /* While scanning insns to find base values, reg_seen[N] is nonzero if
1547 : register N has been set in this function. */
1548 : static sbitmap reg_seen;
1549 :
1550 : static void
1551 1423146328 : record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1552 : {
1553 1423146328 : unsigned regno;
1554 1423146328 : rtx src;
1555 1423146328 : int n;
1556 :
1557 1423146328 : if (!REG_P (dest))
1558 : return;
1559 :
1560 1078869268 : regno = REGNO (dest);
1561 :
1562 1078869268 : gcc_checking_assert (regno < reg_base_value->length ());
1563 :
1564 1078869268 : n = REG_NREGS (dest);
1565 1078869268 : if (n != 1)
1566 : {
1567 15960684 : while (--n >= 0)
1568 : {
1569 10640456 : bitmap_set_bit (reg_seen, regno + n);
1570 10640456 : new_reg_base_value[regno + n] = 0;
1571 : }
1572 : return;
1573 : }
1574 :
1575 1073549040 : if (set)
1576 : {
1577 : /* A CLOBBER wipes out any old value but does not prevent a previously
1578 : unset register from acquiring a base address (i.e. reg_seen is not
1579 : set). */
1580 1071882129 : if (GET_CODE (set) == CLOBBER)
1581 : {
1582 179581868 : new_reg_base_value[regno] = 0;
1583 179581868 : return;
1584 : }
1585 :
1586 892300261 : src = SET_SRC (set);
1587 : }
1588 : else
1589 : {
1590 : /* There's a REG_NOALIAS note against DEST. */
1591 1666911 : if (bitmap_bit_p (reg_seen, regno))
1592 : {
1593 499644 : new_reg_base_value[regno] = 0;
1594 499644 : return;
1595 : }
1596 1167267 : bitmap_set_bit (reg_seen, regno);
1597 1167267 : new_reg_base_value[regno] = unique_base_value (unique_id++);
1598 1167267 : return;
1599 : }
1600 :
1601 : /* If this is not the first set of REGNO, see whether the new value
1602 : is related to the old one. There are two cases of interest:
1603 :
1604 : (1) The register might be assigned an entirely new value
1605 : that has the same base term as the original set.
1606 :
1607 : (2) The set might be a simple self-modification that
1608 : cannot change REGNO's base value.
1609 :
1610 : If neither case holds, reject the original base value as invalid.
1611 : Note that the following situation is not detected:
1612 :
1613 : extern int x, y; int *p = &x; p += (&y-&x);
1614 :
1615 : ANSI C does not allow computing the difference of addresses
1616 : of distinct top level objects. */
1617 892300261 : if (new_reg_base_value[regno] != 0
1618 892300261 : && find_base_value (src) != new_reg_base_value[regno])
1619 77987316 : switch (GET_CODE (src))
1620 : {
1621 457561 : case LO_SUM:
1622 457561 : case MINUS:
1623 457561 : if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1624 64944 : new_reg_base_value[regno] = 0;
1625 : break;
1626 23216368 : case PLUS:
1627 : /* If the value we add in the PLUS is also a valid base value,
1628 : this might be the actual base value, and the original value
1629 : an index. */
1630 23216368 : {
1631 23216368 : rtx other = NULL_RTX;
1632 :
1633 23216368 : if (XEXP (src, 0) == dest)
1634 20150313 : other = XEXP (src, 1);
1635 3066055 : else if (XEXP (src, 1) == dest)
1636 : other = XEXP (src, 0);
1637 :
1638 20268339 : if (! other || find_base_value (other))
1639 2952916 : new_reg_base_value[regno] = 0;
1640 : break;
1641 : }
1642 64490 : case AND:
1643 64490 : if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1644 57268 : new_reg_base_value[regno] = 0;
1645 : break;
1646 54248897 : default:
1647 54248897 : new_reg_base_value[regno] = 0;
1648 54248897 : break;
1649 : }
1650 : /* If this is the first set of a register, record the value. */
1651 479656622 : else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1652 1155967301 : && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1653 299883773 : new_reg_base_value[regno] = find_base_value (src);
1654 :
1655 892300261 : bitmap_set_bit (reg_seen, regno);
1656 : }
1657 :
1658 : /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1659 : using hard registers with non-null REG_BASE_VALUE for renaming. */
1660 : rtx
1661 1560 : get_reg_base_value (unsigned int regno)
1662 : {
1663 1560 : return (*reg_base_value)[regno];
1664 : }
1665 :
1666 : /* If a value is known for REGNO, return it. */
1667 :
1668 : rtx
1669 296588711 : get_reg_known_value (unsigned int regno)
1670 : {
1671 296588711 : if (regno >= FIRST_PSEUDO_REGISTER)
1672 : {
1673 280449313 : regno -= FIRST_PSEUDO_REGISTER;
1674 280449313 : if (regno < vec_safe_length (reg_known_value))
1675 258459118 : return (*reg_known_value)[regno];
1676 : }
1677 : return NULL;
1678 : }
1679 :
1680 : /* Set it. */
1681 :
1682 : static void
1683 548905785 : set_reg_known_value (unsigned int regno, rtx val)
1684 : {
1685 548905785 : if (regno >= FIRST_PSEUDO_REGISTER)
1686 : {
1687 548905785 : regno -= FIRST_PSEUDO_REGISTER;
1688 548905785 : if (regno < vec_safe_length (reg_known_value))
1689 548905785 : (*reg_known_value)[regno] = val;
1690 : }
1691 548905785 : }
1692 :
1693 : /* Similarly for reg_known_equiv_p. */
1694 :
1695 : bool
1696 40260 : get_reg_known_equiv_p (unsigned int regno)
1697 : {
1698 40260 : if (regno >= FIRST_PSEUDO_REGISTER)
1699 : {
1700 40260 : regno -= FIRST_PSEUDO_REGISTER;
1701 40260 : if (regno < vec_safe_length (reg_known_value))
1702 39638 : return bitmap_bit_p (reg_known_equiv_p, regno);
1703 : }
1704 : return false;
1705 : }
1706 :
1707 : static void
1708 43972138 : set_reg_known_equiv_p (unsigned int regno, bool val)
1709 : {
1710 43972138 : if (regno >= FIRST_PSEUDO_REGISTER)
1711 : {
1712 43972138 : regno -= FIRST_PSEUDO_REGISTER;
1713 43972138 : if (regno < vec_safe_length (reg_known_value))
1714 : {
1715 43972138 : if (val)
1716 0 : bitmap_set_bit (reg_known_equiv_p, regno);
1717 : else
1718 43972138 : bitmap_clear_bit (reg_known_equiv_p, regno);
1719 : }
1720 : }
1721 43972138 : }
1722 :
1723 :
1724 : /* Returns a canonical version of X, from the point of view alias
1725 : analysis. (For example, if X is a MEM whose address is a register,
1726 : and the register has a known value (say a SYMBOL_REF), then a MEM
1727 : whose address is the SYMBOL_REF is returned.) */
1728 :
1729 : rtx
1730 5717942247 : canon_rtx (rtx x)
1731 : {
1732 : /* Recursively look for equivalences. */
1733 5722455090 : if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1734 : {
1735 251490109 : rtx t = get_reg_known_value (REGNO (x));
1736 251490109 : if (t == x)
1737 : return x;
1738 26503038 : if (t)
1739 : return canon_rtx (t);
1740 : }
1741 :
1742 5492955176 : if (GET_CODE (x) == PLUS)
1743 : {
1744 1603999675 : rtx x0 = canon_rtx (XEXP (x, 0));
1745 1603999675 : rtx x1 = canon_rtx (XEXP (x, 1));
1746 :
1747 1603999675 : if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1748 3399496 : return simplify_gen_binary (PLUS, GET_MODE (x), x0, x1);
1749 : }
1750 :
1751 : /* This gives us much better alias analysis when called from
1752 : the loop optimizer. Note we want to leave the original
1753 : MEM alone, but need to return the canonicalized MEM with
1754 : all the flags with their original values. */
1755 3888955501 : else if (MEM_P (x))
1756 335628425 : x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1757 :
1758 : return x;
1759 : }
1760 :
1761 : /* Return true if X and Y are identical-looking rtx's.
1762 : Expect that X and Y has been already canonicalized.
1763 :
1764 : We use the data in reg_known_value above to see if two registers with
1765 : different numbers are, in fact, equivalent. */
1766 :
1767 : static bool
1768 9072081752 : rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1769 : {
1770 9072176913 : int i;
1771 9072176913 : int j;
1772 9072176913 : enum rtx_code code;
1773 9072176913 : const char *fmt;
1774 :
1775 9072176913 : if (x == 0 && y == 0)
1776 : return true;
1777 9072176913 : if (x == 0 || y == 0)
1778 : return false;
1779 :
1780 9072176913 : if (x == y)
1781 : return true;
1782 :
1783 7294301674 : code = GET_CODE (x);
1784 : /* Rtx's of different codes cannot be equal. */
1785 7294301674 : if (code != GET_CODE (y))
1786 : return false;
1787 :
1788 : /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1789 : (REG:SI x) and (REG:HI x) are NOT equivalent. */
1790 :
1791 4940805271 : if (GET_MODE (x) != GET_MODE (y))
1792 : return false;
1793 :
1794 : /* Some RTL can be compared without a recursive examination. */
1795 4940805014 : switch (code)
1796 : {
1797 254549783 : case REG:
1798 254549783 : return REGNO (x) == REGNO (y);
1799 :
1800 0 : case LABEL_REF:
1801 0 : return label_ref_label (x) == label_ref_label (y);
1802 :
1803 3906013 : case SYMBOL_REF:
1804 3906013 : {
1805 3906013 : HOST_WIDE_INT distance = 0;
1806 3906013 : return (compare_base_symbol_refs (x, y, &distance) == 1
1807 3906013 : && distance == 0);
1808 : }
1809 :
1810 2185361 : case ENTRY_VALUE:
1811 : /* This is magic, don't go through canonicalization et al. */
1812 2185361 : return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1813 :
1814 : case VALUE:
1815 : CASE_CONST_UNIQUE:
1816 : /* Pointer equality guarantees equality for these nodes. */
1817 : return false;
1818 :
1819 1427756393 : default:
1820 1427756393 : break;
1821 : }
1822 :
1823 : /* canon_rtx knows how to handle plus. No need to canonicalize. */
1824 1427756393 : if (code == PLUS)
1825 1401511367 : return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1826 684911494 : && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1827 2077476476 : || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1828 199302 : && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1829 : /* For commutative operations, the RTX match if the operand match in any
1830 : order. Also handle the simple binary and unary cases without a loop. */
1831 26245026 : if (COMMUTATIVE_P (x))
1832 : {
1833 4481475 : rtx xop0 = canon_rtx (XEXP (x, 0));
1834 4481475 : rtx yop0 = canon_rtx (XEXP (y, 0));
1835 4481475 : rtx yop1 = canon_rtx (XEXP (y, 1));
1836 :
1837 4481475 : return ((rtx_equal_for_memref_p (xop0, yop0)
1838 1516606 : && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1839 4612298 : || (rtx_equal_for_memref_p (xop0, yop1)
1840 32 : && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1841 : }
1842 21763551 : else if (NON_COMMUTATIVE_P (x))
1843 : {
1844 118763 : return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1845 118763 : canon_rtx (XEXP (y, 0)))
1846 150190 : && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1847 31427 : canon_rtx (XEXP (y, 1))));
1848 : }
1849 21644788 : else if (UNARY_P (x))
1850 95161 : return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1851 190322 : canon_rtx (XEXP (y, 0)));
1852 :
1853 : /* Compare the elements. If any pair of corresponding elements
1854 : fail to match, return false for the whole things.
1855 :
1856 : Limit cases to types which actually appear in addresses. */
1857 :
1858 21549627 : fmt = GET_RTX_FORMAT (code);
1859 28865999 : for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1860 : {
1861 28253082 : switch (fmt[i])
1862 : {
1863 3431765 : case 'i':
1864 3431765 : if (XINT (x, i) != XINT (y, i))
1865 : return false;
1866 : break;
1867 :
1868 152 : case 'L':
1869 152 : if (XLOC (x, i) != XLOC (y, i))
1870 : return false;
1871 : break;
1872 :
1873 54 : case 'p':
1874 54 : if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1875 : return false;
1876 : break;
1877 :
1878 3431717 : case 'E':
1879 : /* Two vectors must have the same length. */
1880 3431717 : if (XVECLEN (x, i) != XVECLEN (y, i))
1881 : return false;
1882 :
1883 : /* And the corresponding elements must match. */
1884 3713047 : for (j = 0; j < XVECLEN (x, i); j++)
1885 3432628 : if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1886 3432628 : canon_rtx (XVECEXP (y, i, j))) == 0)
1887 : return false;
1888 : break;
1889 :
1890 18206909 : case 'e':
1891 18206909 : if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1892 18206909 : canon_rtx (XEXP (y, i))) == 0)
1893 : return false;
1894 : break;
1895 :
1896 : /* This can happen for asm operands. */
1897 0 : case 's':
1898 0 : if (strcmp (XSTR (x, i), XSTR (y, i)))
1899 : return false;
1900 : break;
1901 :
1902 : /* This can happen for an asm which clobbers memory. */
1903 : case '0':
1904 : break;
1905 :
1906 : /* It is believed that rtx's at this level will never
1907 : contain anything but integers and other rtx's,
1908 : except for within LABEL_REFs and SYMBOL_REFs. */
1909 0 : default:
1910 0 : gcc_unreachable ();
1911 : }
1912 : }
1913 : return true;
1914 : }
1915 :
1916 : static rtx
1917 3366193327 : find_base_term (rtx x, vec<std::pair<cselib_val *,
1918 : struct elt_loc_list *> > &visited_vals)
1919 : {
1920 6372136221 : cselib_val *val;
1921 6372136221 : struct elt_loc_list *l, *f;
1922 6372136221 : rtx ret;
1923 6372136221 : scalar_int_mode int_mode;
1924 :
1925 : #if defined (FIND_BASE_TERM)
1926 : /* Try machine-dependent ways to find the base term. */
1927 6372136221 : x = FIND_BASE_TERM (x);
1928 : #endif
1929 :
1930 6372136221 : switch (GET_CODE (x))
1931 : {
1932 2232893087 : case REG:
1933 2232893087 : return REG_BASE_VALUE (x);
1934 :
1935 0 : case TRUNCATE:
1936 : /* As we do not know which address space the pointer is referring to, we can
1937 : handle this only if the target does not support different pointer or
1938 : address modes depending on the address space. */
1939 0 : if (!target_default_pointer_address_modes_p ())
1940 : return 0;
1941 118261975 : if (!is_a <scalar_int_mode> (GET_MODE (x), &int_mode)
1942 0 : || GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (Pmode))
1943 : return 0;
1944 : /* Fall through. */
1945 43841414 : case HIGH:
1946 43841414 : case PRE_INC:
1947 43841414 : case PRE_DEC:
1948 43841414 : case POST_INC:
1949 43841414 : case POST_DEC:
1950 43841414 : case PRE_MODIFY:
1951 43841414 : case POST_MODIFY:
1952 43841414 : return find_base_term (XEXP (x, 0), visited_vals);
1953 :
1954 488 : case ZERO_EXTEND:
1955 488 : case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1956 : /* As we do not know which address space the pointer is referring to, we can
1957 : handle this only if the target does not support different pointer or
1958 : address modes depending on the address space. */
1959 488 : if (!target_default_pointer_address_modes_p ())
1960 : return 0;
1961 :
1962 488 : {
1963 488 : rtx temp = find_base_term (XEXP (x, 0), visited_vals);
1964 :
1965 488 : if (temp != 0 && CONSTANT_P (temp))
1966 0 : temp = convert_memory_address (Pmode, temp);
1967 :
1968 : return temp;
1969 : }
1970 :
1971 850298575 : case VALUE:
1972 850298575 : val = CSELIB_VAL_PTR (x);
1973 850298575 : ret = NULL_RTX;
1974 :
1975 850298575 : if (!val)
1976 : return ret;
1977 :
1978 850298575 : if (cselib_sp_based_value_p (val))
1979 21716011 : return static_reg_base_value[STACK_POINTER_REGNUM];
1980 :
1981 828582564 : if (visited_vals.length () > (unsigned) param_max_find_base_term_values)
1982 : return ret;
1983 :
1984 826582295 : f = val->locs;
1985 : /* Reset val->locs to avoid infinite recursion. */
1986 826582295 : if (f)
1987 656977196 : visited_vals.safe_push (std::make_pair (val, f));
1988 826582295 : val->locs = NULL;
1989 :
1990 1461130881 : for (l = f; l; l = l->next)
1991 819852505 : if (GET_CODE (l->loc) == VALUE
1992 110694206 : && CSELIB_VAL_PTR (l->loc)->locs
1993 110643248 : && !CSELIB_VAL_PTR (l->loc)->locs->next
1994 110597891 : && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1995 110597891 : continue;
1996 709254614 : else if ((ret = find_base_term (l->loc, visited_vals)) != 0)
1997 : break;
1998 :
1999 : return ret;
2000 :
2001 0 : case LO_SUM:
2002 : /* The standard form is (lo_sum reg sym) so look only at the
2003 : second operand. */
2004 0 : return find_base_term (XEXP (x, 1), visited_vals);
2005 :
2006 63206349 : case CONST:
2007 63206349 : x = XEXP (x, 0);
2008 63206349 : if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
2009 : return 0;
2010 : /* Fall through. */
2011 2968116707 : case PLUS:
2012 2968116707 : case MINUS:
2013 2968116707 : {
2014 2968116707 : rtx tmp1 = XEXP (x, 0);
2015 2968116707 : rtx tmp2 = XEXP (x, 1);
2016 :
2017 : /* This is a little bit tricky since we have to determine which of
2018 : the two operands represents the real base address. Otherwise this
2019 : routine may return the index register instead of the base register.
2020 :
2021 : That may cause us to believe no aliasing was possible, when in
2022 : fact aliasing is possible.
2023 :
2024 : We use a few simple tests to guess the base register. Additional
2025 : tests can certainly be added. For example, if one of the operands
2026 : is a shift or multiply, then it must be the index register and the
2027 : other operand is the base register. */
2028 :
2029 2968116707 : if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
2030 : return find_base_term (tmp2, visited_vals);
2031 :
2032 2968116479 : if (CONST_INT_P (tmp1))
2033 0 : std::swap (tmp1, tmp2);
2034 :
2035 : /* We can only handle binary operators when one of the operands
2036 : never leads to a base value. */
2037 2968116479 : if (CONST_INT_P (tmp2))
2038 : return find_base_term (tmp1, visited_vals);
2039 :
2040 : /* We could not determine which of the two operands was the
2041 : base register and which was the index. So we can determine
2042 : nothing from the base alias check. */
2043 : return 0;
2044 : }
2045 :
2046 65340511 : case AND:
2047 : /* Look through aligning ANDs. And AND with zero or one with
2048 : the LSB set isn't one (see for example PR92462). */
2049 65340511 : if (CONST_INT_P (XEXP (x, 1))
2050 65340428 : && INTVAL (XEXP (x, 1)) != 0
2051 65340428 : && (INTVAL (XEXP (x, 1)) & 1) == 0)
2052 65340424 : return find_base_term (XEXP (x, 0), visited_vals);
2053 : return 0;
2054 :
2055 : case SYMBOL_REF:
2056 : case LABEL_REF:
2057 : return x;
2058 :
2059 : default:
2060 : return 0;
2061 : }
2062 : }
2063 :
2064 : /* Wrapper around the worker above which removes locs from visited VALUEs
2065 : to avoid visiting them multiple times. We unwind that changes here. */
2066 :
2067 : static rtx
2068 2656938225 : find_base_term (rtx x)
2069 : {
2070 2656938225 : auto_vec<std::pair<cselib_val *, struct elt_loc_list *>, 32> visited_vals;
2071 2656938225 : rtx res = find_base_term (x, visited_vals);
2072 5970853646 : for (unsigned i = 0; i < visited_vals.length (); ++i)
2073 656977196 : visited_vals[i].first->locs = visited_vals[i].second;
2074 2656938225 : return res;
2075 2656938225 : }
2076 :
2077 : /* Return true if accesses to address X may alias accesses based
2078 : on the stack pointer. */
2079 :
2080 : bool
2081 15488648 : may_be_sp_based_p (rtx x)
2082 : {
2083 15488648 : rtx base = find_base_term (x);
2084 15488648 : return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
2085 : }
2086 :
2087 : /* BASE1 and BASE2 are decls. Return 1 if they refer to same object, 0
2088 : if they refer to different objects and -1 if we cannot decide. */
2089 :
2090 : int
2091 1653110933 : compare_base_decls (tree base1, tree base2)
2092 : {
2093 1653110933 : int ret;
2094 1653110933 : gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
2095 1653110933 : if (base1 == base2)
2096 : return 1;
2097 :
2098 : /* If we have two register decls with register specification we
2099 : cannot decide unless their assembler names are the same. */
2100 1442256969 : if (VAR_P (base1)
2101 1389967185 : && VAR_P (base2)
2102 1384076713 : && DECL_HARD_REGISTER (base1)
2103 12668 : && DECL_HARD_REGISTER (base2)
2104 12561 : && DECL_ASSEMBLER_NAME_SET_P (base1)
2105 1442269530 : && DECL_ASSEMBLER_NAME_SET_P (base2))
2106 : {
2107 12561 : if (DECL_ASSEMBLER_NAME_RAW (base1) == DECL_ASSEMBLER_NAME_RAW (base2))
2108 : return 1;
2109 12558 : return -1;
2110 : }
2111 :
2112 : /* Declarations of non-automatic variables may have aliases. All other
2113 : decls are unique. */
2114 1442244408 : if (!decl_in_symtab_p (base1)
2115 1442244408 : || !decl_in_symtab_p (base2))
2116 : return 0;
2117 :
2118 : /* Don't cause symbols to be inserted by the act of checking. */
2119 89976710 : symtab_node *node1 = symtab_node::get (base1);
2120 89976710 : if (!node1)
2121 : return 0;
2122 89956937 : symtab_node *node2 = symtab_node::get (base2);
2123 89956937 : if (!node2)
2124 : return 0;
2125 :
2126 89938040 : ret = node1->equal_address_to (node2, true);
2127 89938040 : return ret;
2128 : }
2129 :
2130 : /* Compare SYMBOL_REFs X_BASE and Y_BASE.
2131 :
2132 : - Return 1 if Y_BASE - X_BASE is constant, adding that constant
2133 : to *DISTANCE if DISTANCE is nonnull.
2134 :
2135 : - Return 0 if no accesses based on X_BASE can alias Y_BASE.
2136 :
2137 : - Return -1 if one of the two results applies, but we can't tell
2138 : which at compile time. Update DISTANCE in the same way as
2139 : for a return value of 1, for the case in which that holds. */
2140 :
2141 : static int
2142 26788818 : compare_base_symbol_refs (const_rtx x_base, const_rtx y_base,
2143 : HOST_WIDE_INT *distance)
2144 : {
2145 26788818 : tree x_decl = SYMBOL_REF_DECL (x_base);
2146 26788818 : tree y_decl = SYMBOL_REF_DECL (y_base);
2147 26788818 : bool binds_def = true;
2148 26788818 : bool swap = false;
2149 :
2150 26788818 : if (XSTR (x_base, 0) == XSTR (y_base, 0))
2151 : return 1;
2152 26323273 : if (x_decl && y_decl)
2153 26323273 : return compare_base_decls (x_decl, y_decl);
2154 0 : if (x_decl || y_decl)
2155 : {
2156 : if (!x_decl)
2157 : {
2158 : swap = true;
2159 : std::swap (x_decl, y_decl);
2160 : std::swap (x_base, y_base);
2161 : }
2162 : /* We handle specially only section anchors. Other symbols are
2163 : either equal (via aliasing) or refer to different objects. */
2164 0 : if (!SYMBOL_REF_HAS_BLOCK_INFO_P (y_base))
2165 : return -1;
2166 : /* Anchors contains static VAR_DECLs and CONST_DECLs. We are safe
2167 : to ignore CONST_DECLs because they are readonly. */
2168 0 : if (!VAR_P (x_decl)
2169 0 : || (!TREE_STATIC (x_decl) && !TREE_PUBLIC (x_decl)))
2170 : return 0;
2171 :
2172 0 : symtab_node *x_node = symtab_node::get_create (x_decl)
2173 0 : ->ultimate_alias_target ();
2174 : /* External variable cannot be in section anchor. */
2175 0 : if (!x_node->definition)
2176 : return 0;
2177 0 : x_base = XEXP (DECL_RTL (x_node->decl), 0);
2178 : /* If not in anchor, we can disambiguate. */
2179 0 : if (!SYMBOL_REF_HAS_BLOCK_INFO_P (x_base))
2180 : return 0;
2181 :
2182 : /* We have an alias of anchored variable. If it can be interposed;
2183 : we must assume it may or may not alias its anchor. */
2184 0 : binds_def = decl_binds_to_current_def_p (x_decl);
2185 : }
2186 : /* If we have variable in section anchor, we can compare by offset. */
2187 0 : if (SYMBOL_REF_HAS_BLOCK_INFO_P (x_base)
2188 0 : && SYMBOL_REF_HAS_BLOCK_INFO_P (y_base))
2189 : {
2190 0 : if (SYMBOL_REF_BLOCK (x_base) != SYMBOL_REF_BLOCK (y_base))
2191 : return 0;
2192 0 : if (distance)
2193 0 : *distance += (swap ? -1 : 1) * (SYMBOL_REF_BLOCK_OFFSET (y_base)
2194 0 : - SYMBOL_REF_BLOCK_OFFSET (x_base));
2195 0 : return binds_def ? 1 : -1;
2196 : }
2197 : /* Either the symbols are equal (via aliasing) or they refer to
2198 : different objects. */
2199 : return -1;
2200 : }
2201 :
2202 : /* Return false if the addresses X and Y are known to point to different
2203 : objects, true if they might be pointers to the same object. */
2204 :
2205 : static bool
2206 1320617307 : base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
2207 : machine_mode x_mode, machine_mode y_mode)
2208 : {
2209 : /* If the address itself has no known base see if a known equivalent
2210 : value has one. If either address still has no known base, nothing
2211 : is known about aliasing. */
2212 1320617307 : if (x_base == 0)
2213 : {
2214 173431862 : rtx x_c;
2215 :
2216 173431862 : if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
2217 : return true;
2218 :
2219 172029 : x_base = find_base_term (x_c);
2220 172029 : if (x_base == 0)
2221 : return true;
2222 : }
2223 :
2224 1147187576 : if (y_base == 0)
2225 : {
2226 173176483 : rtx y_c;
2227 173176483 : if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
2228 : return true;
2229 :
2230 41266 : y_base = find_base_term (y_c);
2231 41266 : if (y_base == 0)
2232 : return true;
2233 : }
2234 :
2235 : /* If the base addresses are equal nothing is known about aliasing. */
2236 974014124 : if (rtx_equal_p (x_base, y_base))
2237 : return true;
2238 :
2239 : /* The base addresses are different expressions. If they are not accessed
2240 : via AND, there is no conflict. We can bring knowledge of object
2241 : alignment into play here. For example, on alpha, "char a, b;" can
2242 : alias one another, though "char a; long b;" cannot. AND addresses may
2243 : implicitly alias surrounding objects; i.e. unaligned access in DImode
2244 : via AND address can alias all surrounding object types except those
2245 : with alignment 8 or higher. */
2246 151301411 : if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2247 : return true;
2248 151301411 : if (GET_CODE (x) == AND
2249 151301411 : && (!CONST_INT_P (XEXP (x, 1))
2250 1981 : || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2251 : return true;
2252 151299630 : if (GET_CODE (y) == AND
2253 151299630 : && (!CONST_INT_P (XEXP (y, 1))
2254 1977 : || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2255 : return true;
2256 :
2257 : /* Differing symbols not accessed via AND never alias. */
2258 151297729 : if (GET_CODE (x_base) == SYMBOL_REF && GET_CODE (y_base) == SYMBOL_REF)
2259 22251670 : return compare_base_symbol_refs (x_base, y_base) != 0;
2260 :
2261 129046059 : if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2262 : return false;
2263 :
2264 141432474 : if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2265 128005961 : return false;
2266 :
2267 : return true;
2268 : }
2269 :
2270 : /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2271 : (or equal to) that of V. */
2272 :
2273 : static bool
2274 212032081 : refs_newer_value_p (const_rtx expr, rtx v)
2275 : {
2276 212032081 : int minuid = CSELIB_VAL_UID (v);
2277 212032081 : subrtx_iterator::array_type array;
2278 633831695 : FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2279 529312311 : if (GET_CODE (*iter) == VALUE && CSELIB_VAL_UID (*iter) >= minuid)
2280 107512697 : return true;
2281 104519384 : return false;
2282 212032081 : }
2283 :
2284 : /* Convert the address X into something we can use. This is done by returning
2285 : it unchanged unless it is a VALUE or VALUE +/- constant; for VALUE
2286 : we call cselib to get a more useful rtx. */
2287 :
2288 : rtx
2289 4011786383 : get_addr (rtx x)
2290 : {
2291 4011786383 : cselib_val *v;
2292 4011786383 : struct elt_loc_list *l;
2293 :
2294 4011786383 : if (GET_CODE (x) != VALUE)
2295 : {
2296 2598686150 : if ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
2297 2174068106 : && GET_CODE (XEXP (x, 0)) == VALUE
2298 123714912 : && CONST_SCALAR_INT_P (XEXP (x, 1)))
2299 : {
2300 117115581 : rtx op0 = get_addr (XEXP (x, 0));
2301 117115581 : if (op0 != XEXP (x, 0))
2302 : {
2303 33329249 : poly_int64 c;
2304 33329249 : if (GET_CODE (x) == PLUS
2305 33329249 : && poly_int_rtx_p (XEXP (x, 1), &c))
2306 33329249 : return plus_constant (GET_MODE (x), op0, c);
2307 0 : return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
2308 0 : op0, XEXP (x, 1));
2309 : }
2310 : }
2311 : return x;
2312 : }
2313 1413100233 : v = CSELIB_VAL_PTR (x);
2314 1413100233 : if (v)
2315 : {
2316 1413100233 : bool have_equivs = cselib_have_permanent_equivalences ();
2317 1413100233 : if (have_equivs)
2318 345371975 : v = canonical_cselib_val (v);
2319 2710645280 : for (l = v->locs; l; l = l->next)
2320 1327553356 : if (CONSTANT_P (l->loc))
2321 : return l->loc;
2322 1621237556 : for (l = v->locs; l; l = l->next)
2323 1216592927 : if (!REG_P (l->loc) && !MEM_P (l->loc)
2324 : /* Avoid infinite recursion when potentially dealing with
2325 : var-tracking artificial equivalences, by skipping the
2326 : equivalences themselves, and not choosing expressions
2327 : that refer to newer VALUEs. */
2328 2507133026 : && (!have_equivs
2329 254588386 : || (GET_CODE (l->loc) != VALUE
2330 157255474 : && !refs_newer_value_p (l->loc, x))))
2331 1058937708 : return l->loc;
2332 324154216 : if (have_equivs)
2333 : {
2334 342795798 : for (l = v->locs; l; l = l->next)
2335 114248073 : if (REG_P (l->loc)
2336 114248073 : || (GET_CODE (l->loc) != VALUE
2337 54776607 : && !refs_newer_value_p (l->loc, x)))
2338 6933158 : return l->loc;
2339 : /* Return the canonical value. */
2340 228547725 : return v->val_rtx;
2341 : }
2342 88673333 : if (v->locs)
2343 48882748 : return v->locs->loc;
2344 : }
2345 : return x;
2346 : }
2347 :
2348 : /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2349 : where SIZE is the size in bytes of the memory reference. If ADDR
2350 : is not modified by the memory reference then ADDR is returned. */
2351 :
2352 : static rtx
2353 5568074458 : addr_side_effect_eval (rtx addr, poly_int64 size, int n_refs)
2354 : {
2355 5568074458 : poly_int64 offset = 0;
2356 :
2357 5568074458 : switch (GET_CODE (addr))
2358 : {
2359 0 : case PRE_INC:
2360 0 : offset = (n_refs + 1) * size;
2361 0 : break;
2362 22519613 : case PRE_DEC:
2363 22519613 : offset = -(n_refs + 1) * size;
2364 22519613 : break;
2365 : case POST_INC:
2366 505393 : offset = n_refs * size;
2367 505393 : break;
2368 0 : case POST_DEC:
2369 0 : offset = -n_refs * size;
2370 0 : break;
2371 :
2372 : default:
2373 : return addr;
2374 : }
2375 :
2376 23025006 : addr = plus_constant (GET_MODE (addr), XEXP (addr, 0), offset);
2377 23025006 : addr = canon_rtx (addr);
2378 :
2379 23025006 : return addr;
2380 : }
2381 :
2382 : /* Return TRUE if an object X sized at XSIZE bytes and another object
2383 : Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2384 : any of the sizes is zero, assume an overlap, otherwise use the
2385 : absolute value of the sizes as the actual sizes. */
2386 :
2387 : static inline bool
2388 850388863 : offset_overlap_p (poly_int64 c, poly_int64 xsize, poly_int64 ysize)
2389 : {
2390 849754726 : if (known_eq (xsize, 0) || known_eq (ysize, 0))
2391 : return true;
2392 :
2393 846904354 : if (maybe_ge (c, 0))
2394 511614545 : return maybe_gt (maybe_lt (xsize, 0) ? -xsize : xsize, c);
2395 : else
2396 335289809 : return maybe_gt (maybe_lt (ysize, 0) ? -ysize : ysize, -c);
2397 : }
2398 :
2399 : /* Return one if X and Y (memory addresses) reference the
2400 : same location in memory or if the references overlap.
2401 : Return zero if they do not overlap, else return
2402 : minus one in which case they still might reference the same location.
2403 :
2404 : C is an offset accumulator. When
2405 : C is nonzero, we are testing aliases between X and Y + C.
2406 : XSIZE is the size in bytes of the X reference,
2407 : similarly YSIZE is the size in bytes for Y.
2408 : Expect that canon_rtx has been already called for X and Y.
2409 :
2410 : If XSIZE or YSIZE is zero, we do not know the amount of memory being
2411 : referenced (the reference was BLKmode), so make the most pessimistic
2412 : assumptions.
2413 :
2414 : If XSIZE or YSIZE is negative, we may access memory outside the object
2415 : being referenced as a side effect. This can happen when using AND to
2416 : align memory references, as is done on the Alpha.
2417 :
2418 : Nice to notice that varying addresses cannot conflict with fp if no
2419 : local variables had their addresses taken, but that's too hard now.
2420 :
2421 : ??? Contrary to the tree alias oracle this does not return
2422 : one for X + non-constant and Y + non-constant when X and Y are equal.
2423 : If that is fixed the TBAA hack for union type-punning can be removed. */
2424 :
2425 : static int
2426 1932642716 : memrefs_conflict_p (poly_int64 xsize, rtx x, poly_int64 ysize, rtx y,
2427 : poly_int64 c)
2428 : {
2429 2784037229 : if (GET_CODE (x) == VALUE)
2430 : {
2431 738753763 : if (REG_P (y))
2432 : {
2433 286138999 : struct elt_loc_list *l = NULL;
2434 286138999 : if (CSELIB_VAL_PTR (x))
2435 286138999 : for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2436 518876946 : l; l = l->next)
2437 373170409 : if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2438 : break;
2439 286138999 : if (l)
2440 : x = y;
2441 : else
2442 145706537 : x = get_addr (x);
2443 : }
2444 : /* Don't call get_addr if y is the same VALUE. */
2445 452614764 : else if (x != y)
2446 452519905 : x = get_addr (x);
2447 : }
2448 2784037229 : if (GET_CODE (y) == VALUE)
2449 : {
2450 422972862 : if (REG_P (x))
2451 : {
2452 14475184 : struct elt_loc_list *l = NULL;
2453 14475184 : if (CSELIB_VAL_PTR (y))
2454 14475184 : for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2455 17413615 : l; l = l->next)
2456 2960124 : if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2457 : break;
2458 14475184 : if (l)
2459 : y = x;
2460 : else
2461 14453491 : y = get_addr (y);
2462 : }
2463 : /* Don't call get_addr if x is the same VALUE. */
2464 408497678 : else if (y != x)
2465 408402100 : y = get_addr (y);
2466 : }
2467 2784037229 : if (GET_CODE (x) == HIGH)
2468 0 : x = XEXP (x, 0);
2469 2784037229 : else if (GET_CODE (x) == LO_SUM)
2470 0 : x = XEXP (x, 1);
2471 : else
2472 2784037229 : x = addr_side_effect_eval (x, maybe_lt (xsize, 0) ? -xsize : xsize, 0);
2473 2784037229 : if (GET_CODE (y) == HIGH)
2474 0 : y = XEXP (y, 0);
2475 2784037229 : else if (GET_CODE (y) == LO_SUM)
2476 0 : y = XEXP (y, 1);
2477 : else
2478 2784037229 : y = addr_side_effect_eval (y, maybe_lt (ysize, 0) ? -ysize : ysize, 0);
2479 :
2480 2784037229 : if (GET_CODE (x) == SYMBOL_REF && GET_CODE (y) == SYMBOL_REF)
2481 : {
2482 631135 : HOST_WIDE_INT distance = 0;
2483 631135 : int cmp = compare_base_symbol_refs (x, y, &distance);
2484 :
2485 : /* If both decls are the same, decide by offsets. */
2486 631135 : if (cmp == 1)
2487 930958 : return offset_overlap_p (c + distance, xsize, ysize);
2488 : /* Assume a potential overlap for symbolic addresses that went
2489 : through alignment adjustments (i.e., that have negative
2490 : sizes), because we can't know how far they are from each
2491 : other. */
2492 165589 : if (maybe_lt (xsize, 0) || maybe_lt (ysize, 0))
2493 : return -1;
2494 : /* If decls are different or we know by offsets that there is no overlap,
2495 : we win. */
2496 796724 : if (!cmp || !offset_overlap_p (c + distance, xsize, ysize))
2497 165589 : return 0;
2498 : /* Decls may or may not be different and offsets overlap....*/
2499 : return -1;
2500 : }
2501 2783406094 : else if (rtx_equal_for_memref_p (x, y))
2502 : {
2503 306615319 : return offset_overlap_p (c, xsize, ysize);
2504 : }
2505 :
2506 : /* This code used to check for conflicts involving stack references and
2507 : globals but the base address alias code now handles these cases. */
2508 :
2509 2630088690 : if (GET_CODE (x) == PLUS)
2510 : {
2511 : /* The fact that X is canonicalized means that this
2512 : PLUS rtx is canonicalized. */
2513 1557450244 : rtx x0 = XEXP (x, 0);
2514 1557450244 : rtx x1 = XEXP (x, 1);
2515 :
2516 : /* However, VALUEs might end up in different positions even in
2517 : canonical PLUSes. Comparing their addresses is enough. */
2518 1557450244 : if (x0 == y)
2519 25948468 : return memrefs_conflict_p (xsize, x1, ysize, const0_rtx, c);
2520 1531501776 : else if (x1 == y)
2521 340800 : return memrefs_conflict_p (xsize, x0, ysize, const0_rtx, c);
2522 :
2523 1531160976 : poly_int64 cx1, cy1;
2524 1531160976 : if (GET_CODE (y) == PLUS)
2525 : {
2526 : /* The fact that Y is canonicalized means that this
2527 : PLUS rtx is canonicalized. */
2528 1377372397 : rtx y0 = XEXP (y, 0);
2529 1377372397 : rtx y1 = XEXP (y, 1);
2530 :
2531 1377372397 : if (x0 == y1)
2532 : return memrefs_conflict_p (xsize, x1, ysize, y0, c);
2533 1377255169 : if (x1 == y0)
2534 : return memrefs_conflict_p (xsize, x0, ysize, y1, c);
2535 :
2536 1377179493 : if (rtx_equal_for_memref_p (x1, y1))
2537 : return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2538 1241116310 : if (rtx_equal_for_memref_p (x0, y0))
2539 : return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2540 576915303 : if (poly_int_rtx_p (x1, &cx1))
2541 : {
2542 1117694566 : poly_offset_int co = c;
2543 558847283 : co -= cx1;
2544 558847283 : if (poly_int_rtx_p (y1, &cy1))
2545 : {
2546 550999384 : co += cy1;
2547 550999384 : if (!co.to_shwi (&c))
2548 : return -1;
2549 550995573 : return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2550 : }
2551 7847899 : else if (!co.to_shwi (&c))
2552 : return -1;
2553 : else
2554 7847899 : return memrefs_conflict_p (xsize, x0, ysize, y, c);
2555 : }
2556 710187348 : else if (poly_int_rtx_p (y1, &cy1))
2557 : {
2558 28601012 : poly_offset_int co = c;
2559 14300506 : co += cy1;
2560 14300506 : if (!co.to_shwi (&c))
2561 : return -1;
2562 14300506 : return memrefs_conflict_p (xsize, x, ysize, y0, c);
2563 : }
2564 :
2565 : return -1;
2566 : }
2567 153788579 : else if (poly_int_rtx_p (x1, &cx1))
2568 : {
2569 237943078 : poly_offset_int co = c;
2570 118971539 : co -= cx1;
2571 118971539 : if (!co.to_shwi (&c))
2572 : return -1;
2573 118971522 : return memrefs_conflict_p (xsize, x0, ysize, y, c);
2574 : }
2575 : }
2576 1072638446 : else if (GET_CODE (y) == PLUS)
2577 : {
2578 : /* The fact that Y is canonicalized means that this
2579 : PLUS rtx is canonicalized. */
2580 88829743 : rtx y0 = XEXP (y, 0);
2581 88829743 : rtx y1 = XEXP (y, 1);
2582 :
2583 88829743 : if (x == y0)
2584 9631983 : return memrefs_conflict_p (xsize, const0_rtx, ysize, y1, c);
2585 79197760 : if (x == y1)
2586 844232 : return memrefs_conflict_p (xsize, const0_rtx, ysize, y0, c);
2587 :
2588 78353528 : poly_int64 cy1;
2589 138165749 : if (poly_int_rtx_p (y1, &cy1))
2590 : {
2591 119624442 : poly_offset_int co = c;
2592 59812221 : co += cy1;
2593 59812221 : if (!co.to_shwi (&c))
2594 : return -1;
2595 59812221 : return memrefs_conflict_p (xsize, x, ysize, y0, c);
2596 : }
2597 : else
2598 : return -1;
2599 : }
2600 :
2601 1018625743 : if (GET_CODE (x) == GET_CODE (y))
2602 824174804 : switch (GET_CODE (x))
2603 : {
2604 328585 : case MULT:
2605 328585 : {
2606 : /* Handle cases where we expect the second operands to be the
2607 : same, and check only whether the first operand would conflict
2608 : or not. */
2609 328585 : rtx x0, y0;
2610 328585 : rtx x1 = canon_rtx (XEXP (x, 1));
2611 328585 : rtx y1 = canon_rtx (XEXP (y, 1));
2612 328585 : if (! rtx_equal_for_memref_p (x1, y1))
2613 : return -1;
2614 311579 : x0 = canon_rtx (XEXP (x, 0));
2615 311579 : y0 = canon_rtx (XEXP (y, 0));
2616 311579 : if (rtx_equal_for_memref_p (x0, y0))
2617 0 : return offset_overlap_p (c, xsize, ysize);
2618 :
2619 : /* Can't properly adjust our sizes. */
2620 311579 : poly_int64 c1;
2621 311579 : if (!poly_int_rtx_p (x1, &c1)
2622 308624 : || !can_div_trunc_p (xsize, c1, &xsize)
2623 308624 : || !can_div_trunc_p (ysize, c1, &ysize)
2624 308624 : || !can_div_trunc_p (c, c1, &c))
2625 : return -1;
2626 308624 : return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2627 : }
2628 :
2629 : default:
2630 : break;
2631 : }
2632 :
2633 : /* Deal with alignment ANDs by adjusting offset and size so as to
2634 : cover the maximum range, without taking any previously known
2635 : alignment into account. Make a size negative after such an
2636 : adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2637 : assume a potential overlap, because they may end up in contiguous
2638 : memory locations and the stricter-alignment access may span over
2639 : part of both. */
2640 1018297158 : if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2641 : {
2642 4422123 : HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2643 4422123 : unsigned HOST_WIDE_INT uc = sc;
2644 4422123 : if (sc < 0 && pow2_or_zerop (-uc))
2645 : {
2646 4421325 : if (maybe_gt (xsize, 0))
2647 4420990 : xsize = -xsize;
2648 4421325 : if (maybe_ne (xsize, 0))
2649 : {
2650 4421130 : poly_offset_int xsizeo = xsize;
2651 4421130 : xsizeo += sc + 1;
2652 4421130 : if (!xsizeo.to_shwi (&xsize))
2653 0 : return -1;
2654 : }
2655 4421325 : poly_offset_int co = c;
2656 4421325 : co -= sc + 1;
2657 4421325 : if (!co.to_shwi (&c))
2658 : return -1;
2659 4421325 : return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2660 4421325 : ysize, y, c);
2661 : }
2662 : }
2663 1013875833 : if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2664 : {
2665 5935409 : HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2666 5935409 : unsigned HOST_WIDE_INT uc = sc;
2667 5935409 : if (sc < 0 && pow2_or_zerop (-uc))
2668 : {
2669 5933985 : if (maybe_gt (ysize, 0))
2670 5932222 : ysize = -ysize;
2671 5933985 : if (maybe_ne (ysize, 0))
2672 : {
2673 5933949 : poly_offset_int ysizeo = ysize;
2674 5933949 : ysizeo += sc + 1;
2675 5933949 : if (!ysizeo.to_shwi (&ysize))
2676 0 : return -1;
2677 : }
2678 5933985 : poly_offset_int co = c;
2679 5933985 : co += sc + 1;
2680 5933985 : if (!co.to_shwi (&c))
2681 : return -1;
2682 5933985 : return memrefs_conflict_p (xsize, x,
2683 5933985 : ysize, canon_rtx (XEXP (y, 0)), c);
2684 : }
2685 : }
2686 :
2687 1007941848 : if (CONSTANT_P (x))
2688 : {
2689 720940145 : poly_int64 cx, cy;
2690 720940145 : if (poly_int_rtx_p (x, &cx) && poly_int_rtx_p (y, &cy))
2691 : {
2692 2089267155 : poly_offset_int co = c;
2693 696422385 : co += cy;
2694 696422385 : co -= cx;
2695 696422385 : if (!co.to_shwi (&c))
2696 : return -1;
2697 1392229974 : return offset_overlap_p (c, xsize, ysize);
2698 : }
2699 :
2700 24517760 : if (GET_CODE (x) == CONST)
2701 : {
2702 12907147 : if (GET_CODE (y) == CONST)
2703 10961048 : return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2704 10961048 : ysize, canon_rtx (XEXP (y, 0)), c);
2705 : else
2706 1946099 : return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2707 1946099 : ysize, y, c);
2708 : }
2709 11610613 : if (GET_CODE (y) == CONST)
2710 956165 : return memrefs_conflict_p (xsize, x, ysize,
2711 956165 : canon_rtx (XEXP (y, 0)), c);
2712 :
2713 : /* Assume a potential overlap for symbolic addresses that went
2714 : through alignment adjustments (i.e., that have negative
2715 : sizes), because we can't know how far they are from each
2716 : other. */
2717 10654448 : if (CONSTANT_P (y))
2718 28675 : return (maybe_lt (xsize, 0)
2719 28675 : || maybe_lt (ysize, 0)
2720 57350 : || offset_overlap_p (c, xsize, ysize));
2721 :
2722 : return -1;
2723 : }
2724 :
2725 : return -1;
2726 : }
2727 :
2728 : /* Functions to compute memory dependencies.
2729 :
2730 : Since we process the insns in execution order, we can build tables
2731 : to keep track of what registers are fixed (and not aliased), what registers
2732 : are varying in known ways, and what registers are varying in unknown
2733 : ways.
2734 :
2735 : If both memory references are volatile, then there must always be a
2736 : dependence between the two references, since their order cannot be
2737 : changed. A volatile and non-volatile reference can be interchanged
2738 : though.
2739 :
2740 : We also must allow AND addresses, because they may generate accesses
2741 : outside the object being referenced. This is used to generate aligned
2742 : addresses from unaligned addresses, for instance, the alpha
2743 : storeqi_unaligned pattern. */
2744 :
2745 : /* Read dependence: X is read after read in MEM takes place. There can
2746 : only be a dependence here if both reads are volatile, or if either is
2747 : an explicit barrier. */
2748 :
2749 : bool
2750 32795661 : read_dependence (const_rtx mem, const_rtx x)
2751 : {
2752 32795661 : if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2753 : return true;
2754 32272933 : if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2755 64530626 : || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2756 23655 : return true;
2757 : return false;
2758 : }
2759 :
2760 : /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2761 :
2762 : static tree
2763 64141564 : decl_for_component_ref (tree x)
2764 : {
2765 84392004 : do
2766 : {
2767 84392004 : x = TREE_OPERAND (x, 0);
2768 : }
2769 84392004 : while (x && TREE_CODE (x) == COMPONENT_REF);
2770 :
2771 64141564 : return x && DECL_P (x) ? x : NULL_TREE;
2772 : }
2773 :
2774 : /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2775 : for the offset of the field reference. *KNOWN_P says whether the
2776 : offset is known. */
2777 :
2778 : static void
2779 9750429 : adjust_offset_for_component_ref (tree x, bool *known_p,
2780 : poly_int64 *offset)
2781 : {
2782 9750429 : if (!*known_p)
2783 : return;
2784 11873274 : do
2785 : {
2786 11873274 : tree xoffset = component_ref_field_offset (x);
2787 11873274 : tree field = TREE_OPERAND (x, 1);
2788 11873274 : if (!poly_int_tree_p (xoffset))
2789 : {
2790 0 : *known_p = false;
2791 0 : return;
2792 : }
2793 :
2794 11873274 : poly_offset_int woffset
2795 11873274 : = (wi::to_poly_offset (xoffset)
2796 11873274 : + (wi::to_offset (DECL_FIELD_BIT_OFFSET (field))
2797 23746548 : >> LOG2_BITS_PER_UNIT)
2798 11873274 : + *offset);
2799 11873274 : if (!woffset.to_shwi (offset))
2800 : {
2801 0 : *known_p = false;
2802 0 : return;
2803 : }
2804 :
2805 11873274 : x = TREE_OPERAND (x, 0);
2806 : }
2807 11873274 : while (x && TREE_CODE (x) == COMPONENT_REF);
2808 : }
2809 :
2810 : /* Return true if we can determine the exprs corresponding to memrefs
2811 : X and Y and they do not overlap.
2812 : If LOOP_VARIANT is set, skip offset-based disambiguation */
2813 :
2814 : bool
2815 255845939 : nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2816 : {
2817 275425600 : tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2818 255845939 : rtx rtlx, rtly;
2819 255845939 : rtx basex, basey;
2820 255845939 : bool moffsetx_known_p, moffsety_known_p;
2821 255845939 : poly_int64 moffsetx = 0, moffsety = 0;
2822 255845939 : poly_int64 offsetx = 0, offsety = 0, sizex, sizey;
2823 :
2824 : /* Unless both have exprs, we can't tell anything. */
2825 255845939 : if (exprx == 0 || expry == 0)
2826 : return false;
2827 :
2828 : /* For spill-slot accesses make sure we have valid offsets. */
2829 214484649 : if ((exprx == get_spill_slot_decl (false)
2830 18156540 : && ! MEM_OFFSET_KNOWN_P (x))
2831 232641189 : || (expry == get_spill_slot_decl (false)
2832 27717565 : && ! MEM_OFFSET_KNOWN_P (y)))
2833 : return false;
2834 :
2835 : /* If the field reference test failed, look at the DECLs involved. */
2836 214484649 : moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2837 214484649 : if (moffsetx_known_p)
2838 211976162 : moffsetx = MEM_OFFSET (x);
2839 214484649 : if (TREE_CODE (exprx) == COMPONENT_REF)
2840 : {
2841 37849563 : tree t = decl_for_component_ref (exprx);
2842 37849563 : if (! t)
2843 : return false;
2844 4749866 : adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2845 4749866 : exprx = t;
2846 : }
2847 :
2848 181384952 : moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2849 181384952 : if (moffsety_known_p)
2850 179238298 : moffsety = MEM_OFFSET (y);
2851 181384952 : if (TREE_CODE (expry) == COMPONENT_REF)
2852 : {
2853 26292001 : tree t = decl_for_component_ref (expry);
2854 26292001 : if (! t)
2855 : return false;
2856 5000563 : adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2857 5000563 : expry = t;
2858 : }
2859 :
2860 160093514 : if (! DECL_P (exprx) || ! DECL_P (expry))
2861 : return false;
2862 :
2863 : /* If we refer to different gimple registers, or one gimple register
2864 : and one non-gimple-register, we know they can't overlap. First,
2865 : gimple registers don't have their addresses taken. Now, there
2866 : could be more than one stack slot for (different versions of) the
2867 : same gimple register, but we can presumably tell they don't
2868 : overlap based on offsets from stack base addresses elsewhere.
2869 : It's important that we don't proceed to DECL_RTL, because gimple
2870 : registers may not pass DECL_RTL_SET_P, and make_decl_rtl won't be
2871 : able to do anything about them since no SSA information will have
2872 : remained to guide it. */
2873 13893883 : if (is_gimple_reg (exprx) || is_gimple_reg (expry))
2874 3853639 : return exprx != expry
2875 3853639 : || (moffsetx_known_p && moffsety_known_p
2876 309854 : && MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y)
2877 309854 : && !offset_overlap_p (moffsety - moffsetx,
2878 154927 : MEM_SIZE (x), MEM_SIZE (y)));
2879 :
2880 : /* With invalid code we can end up storing into the constant pool.
2881 : Bail out to avoid ICEing when creating RTL for this.
2882 : See gfortran.dg/lto/20091028-2_0.f90. */
2883 10040244 : if (TREE_CODE (exprx) == CONST_DECL
2884 10040244 : || TREE_CODE (expry) == CONST_DECL)
2885 : return true;
2886 :
2887 : /* If one decl is known to be a function or label in a function and
2888 : the other is some kind of data, they can't overlap. */
2889 10040244 : if ((TREE_CODE (exprx) == FUNCTION_DECL
2890 10040244 : || TREE_CODE (exprx) == LABEL_DECL)
2891 : != (TREE_CODE (expry) == FUNCTION_DECL
2892 10040244 : || TREE_CODE (expry) == LABEL_DECL))
2893 : return true;
2894 :
2895 : /* If either of the decls doesn't have DECL_RTL set (e.g. marked as
2896 : living in multiple places), we can't tell anything. Exception
2897 : are FUNCTION_DECLs for which we can create DECL_RTL on demand. */
2898 9852276 : if ((!DECL_RTL_SET_P (exprx) && TREE_CODE (exprx) != FUNCTION_DECL)
2899 19704552 : || (!DECL_RTL_SET_P (expry) && TREE_CODE (expry) != FUNCTION_DECL))
2900 : return false;
2901 :
2902 9852276 : rtlx = DECL_RTL (exprx);
2903 9852276 : rtly = DECL_RTL (expry);
2904 :
2905 : /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2906 : can't overlap unless they are the same because we never reuse that part
2907 : of the stack frame used for locals for spilled pseudos. */
2908 9851799 : if ((!MEM_P (rtlx) || !MEM_P (rtly))
2909 9884853 : && ! rtx_equal_p (rtlx, rtly))
2910 : return true;
2911 :
2912 : /* If we have MEMs referring to different address spaces (which can
2913 : potentially overlap), we cannot easily tell from the addresses
2914 : whether the references overlap. */
2915 9819222 : if (MEM_P (rtlx) && MEM_P (rtly)
2916 19638715 : && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2917 : return false;
2918 :
2919 : /* Get the base and offsets of both decls. If either is a register, we
2920 : know both are and are the same, so use that as the base. The only
2921 : we can avoid overlap is if we can deduce that they are nonoverlapping
2922 : pieces of that decl, which is very rare. */
2923 9819493 : basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2924 9819493 : basex = strip_offset_and_add (basex, &offsetx);
2925 :
2926 9819493 : basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2927 9819493 : basey = strip_offset_and_add (basey, &offsety);
2928 :
2929 : /* If the bases are different, we know they do not overlap if both
2930 : are constants or if one is a constant and the other a pointer into the
2931 : stack frame. Otherwise a different base means we can't tell if they
2932 : overlap or not. */
2933 9819493 : if (compare_base_decls (exprx, expry) == 0)
2934 7007304 : return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2935 2645543 : || (CONSTANT_P (basex) && REG_P (basey)
2936 276874 : && REGNO_PTR_FRAME_P (REGNO (basey)))
2937 11747328 : || (CONSTANT_P (basey) && REG_P (basex)
2938 400540 : && REGNO_PTR_FRAME_P (REGNO (basex))));
2939 :
2940 : /* Offset based disambiguation not appropriate for loop invariant */
2941 443520 : if (loop_invariant)
2942 : return false;
2943 :
2944 : /* Offset based disambiguation is OK even if we do not know that the
2945 : declarations are necessarily different
2946 : (i.e. compare_base_decls (exprx, expry) == -1) */
2947 :
2948 443791 : sizex = (!MEM_P (rtlx) ? poly_int64 (GET_MODE_SIZE (GET_MODE (rtlx)))
2949 443249 : : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2950 : : -1);
2951 443791 : sizey = (!MEM_P (rtly) ? poly_int64 (GET_MODE_SIZE (GET_MODE (rtly)))
2952 443249 : : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2953 : : -1);
2954 :
2955 : /* If we have an offset for either memref, it can update the values computed
2956 : above. */
2957 443520 : if (moffsetx_known_p)
2958 417130 : offsetx += moffsetx, sizex -= moffsetx;
2959 443520 : if (moffsety_known_p)
2960 378529 : offsety += moffsety, sizey -= moffsety;
2961 :
2962 : /* If a memref has both a size and an offset, we can use the smaller size.
2963 : We can't do this if the offset isn't known because we must view this
2964 : memref as being anywhere inside the DECL's MEM. */
2965 443520 : if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2966 417130 : sizex = MEM_SIZE (x);
2967 443520 : if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2968 378529 : sizey = MEM_SIZE (y);
2969 :
2970 443520 : return !ranges_maybe_overlap_p (offsetx, sizex, offsety, sizey);
2971 : }
2972 :
2973 : /* Helper for true_dependence and canon_true_dependence.
2974 : Checks for true dependence: X is read after store in MEM takes place.
2975 :
2976 : If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2977 : NULL_RTX, and the canonical addresses of MEM and X are both computed
2978 : here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2979 :
2980 : If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2981 :
2982 : Returns true if there is a true dependence, false otherwise. */
2983 :
2984 : static bool
2985 875637733 : true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2986 : const_rtx x, rtx x_addr, bool mem_canonicalized)
2987 : {
2988 875637733 : rtx true_mem_addr;
2989 875637733 : rtx base;
2990 875637733 : int ret;
2991 :
2992 875637733 : gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2993 : : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2994 :
2995 875637733 : if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2996 : return true;
2997 :
2998 : /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2999 : This is used in epilogue deallocation functions, and in cselib. */
3000 874918046 : if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3001 : return true;
3002 874896123 : if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3003 : return true;
3004 869362470 : if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3005 1738695480 : || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3006 : return true;
3007 :
3008 866400592 : if (! x_addr)
3009 56648366 : x_addr = XEXP (x, 0);
3010 866400592 : x_addr = get_addr (x_addr);
3011 :
3012 866400592 : if (! mem_addr)
3013 : {
3014 44123515 : mem_addr = XEXP (mem, 0);
3015 44123515 : if (mem_mode == VOIDmode)
3016 23885432 : mem_mode = GET_MODE (mem);
3017 : }
3018 866400592 : true_mem_addr = get_addr (mem_addr);
3019 :
3020 : /* Read-only memory is by definition never modified, and therefore can't
3021 : conflict with anything. However, don't assume anything when AND
3022 : addresses are involved and leave to the code below to determine
3023 : dependence. We don't expect to find read-only set on MEM, but
3024 : stupid user tricks can produce them, so don't die. */
3025 866400592 : if (MEM_READONLY_P (x)
3026 4514999 : && GET_CODE (x_addr) != AND
3027 870915591 : && GET_CODE (true_mem_addr) != AND)
3028 : return false;
3029 :
3030 : /* If we have MEMs referring to different address spaces (which can
3031 : potentially overlap), we cannot easily tell from the addresses
3032 : whether the references overlap. */
3033 913157996 : if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3034 : return true;
3035 :
3036 861819280 : base = find_base_term (x_addr);
3037 861819280 : if (base && (GET_CODE (base) == LABEL_REF
3038 734739391 : || (GET_CODE (base) == SYMBOL_REF
3039 65198878 : && CONSTANT_POOL_ADDRESS_P (base))))
3040 : return false;
3041 :
3042 861818382 : rtx mem_base = find_base_term (true_mem_addr);
3043 861818382 : if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
3044 861818382 : GET_MODE (x), mem_mode))
3045 : return false;
3046 :
3047 774076330 : x_addr = canon_rtx (x_addr);
3048 774076330 : if (!mem_canonicalized)
3049 31755421 : mem_addr = canon_rtx (true_mem_addr);
3050 :
3051 774076330 : if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
3052 1548152660 : SIZE_FOR_MODE (x), x_addr, 0)) != -1)
3053 525335159 : return !!ret;
3054 :
3055 248741171 : if (mems_in_disjoint_alias_sets_p (x, mem))
3056 : return false;
3057 :
3058 184626950 : if (nonoverlapping_memrefs_p (mem, x, false))
3059 : return false;
3060 :
3061 176004631 : return rtx_refs_may_alias_p (x, mem, true);
3062 : }
3063 :
3064 : /* True dependence: X is read after store in MEM takes place. */
3065 :
3066 : bool
3067 46949241 : true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
3068 : {
3069 46949241 : return true_dependence_1 (mem, mem_mode, NULL_RTX,
3070 46949241 : x, NULL_RTX, /*mem_canonicalized=*/false);
3071 : }
3072 :
3073 : /* Canonical true dependence: X is read after store in MEM takes place.
3074 : Variant of true_dependence which assumes MEM has already been
3075 : canonicalized (hence we no longer do that here).
3076 : The mem_addr argument has been added, since true_dependence_1 computed
3077 : this value prior to canonicalizing. */
3078 :
3079 : bool
3080 828688492 : canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
3081 : const_rtx x, rtx x_addr)
3082 : {
3083 828688492 : return true_dependence_1 (mem, mem_mode, mem_addr,
3084 828688492 : x, x_addr, /*mem_canonicalized=*/true);
3085 : }
3086 :
3087 : /* Returns true if a write to X might alias a previous read from
3088 : (or, if WRITEP is true, a write to) MEM.
3089 : If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
3090 : and X_MODE the mode for that access.
3091 : If MEM_CANONICALIZED is true, MEM is canonicalized. */
3092 :
3093 : static bool
3094 500365655 : write_dependence_p (const_rtx mem,
3095 : const_rtx x, machine_mode x_mode, rtx x_addr,
3096 : bool mem_canonicalized, bool x_canonicalized, bool writep)
3097 : {
3098 500365655 : rtx mem_addr;
3099 500365655 : rtx true_mem_addr, true_x_addr;
3100 500365655 : rtx base;
3101 500365655 : int ret;
3102 :
3103 500365655 : gcc_checking_assert (x_canonicalized
3104 : ? (x_addr != NULL_RTX
3105 : && (x_mode != VOIDmode || GET_MODE (x) == VOIDmode))
3106 : : (x_addr == NULL_RTX && x_mode == VOIDmode));
3107 :
3108 500365655 : if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
3109 : return true;
3110 :
3111 : /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
3112 : This is used in epilogue deallocation functions. */
3113 499022931 : if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3114 : return true;
3115 471545495 : if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3116 : return true;
3117 470456877 : if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3118 940707663 : || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3119 : return true;
3120 :
3121 470231272 : if (!x_addr)
3122 55924361 : x_addr = XEXP (x, 0);
3123 470231272 : true_x_addr = get_addr (x_addr);
3124 :
3125 470231272 : mem_addr = XEXP (mem, 0);
3126 470231272 : true_mem_addr = get_addr (mem_addr);
3127 :
3128 : /* A read from read-only memory can't conflict with read-write memory.
3129 : Don't assume anything when AND addresses are involved and leave to
3130 : the code below to determine dependence. */
3131 470231272 : if (!writep
3132 434556057 : && MEM_READONLY_P (mem)
3133 11392056 : && GET_CODE (true_x_addr) != AND
3134 481623328 : && GET_CODE (true_mem_addr) != AND)
3135 : return false;
3136 :
3137 : /* If we have MEMs referring to different address spaces (which can
3138 : potentially overlap), we cannot easily tell from the addresses
3139 : whether the references overlap. */
3140 474626173 : if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3141 : return true;
3142 :
3143 458799695 : base = find_base_term (true_mem_addr);
3144 458799695 : if (! writep
3145 458799695 : && base
3146 458799695 : && (GET_CODE (base) == LABEL_REF
3147 361033936 : || (GET_CODE (base) == SYMBOL_REF
3148 38804088 : && CONSTANT_POOL_ADDRESS_P (base))))
3149 : return false;
3150 :
3151 458798925 : rtx x_base = find_base_term (true_x_addr);
3152 458798925 : if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
3153 458798925 : GET_MODE (x), GET_MODE (mem)))
3154 : return false;
3155 :
3156 396283355 : if (!x_canonicalized)
3157 : {
3158 49224730 : x_addr = canon_rtx (true_x_addr);
3159 49224730 : x_mode = GET_MODE (x);
3160 : }
3161 396283355 : if (!mem_canonicalized)
3162 233572769 : mem_addr = canon_rtx (true_mem_addr);
3163 :
3164 1188850065 : if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
3165 396283355 : GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
3166 325064366 : return !!ret;
3167 :
3168 71218989 : if (nonoverlapping_memrefs_p (x, mem, false))
3169 : return false;
3170 :
3171 68108551 : return rtx_refs_may_alias_p (x, mem, false);
3172 : }
3173 :
3174 : /* Anti dependence: X is written after read in MEM takes place. */
3175 :
3176 : bool
3177 25102929 : anti_dependence (const_rtx mem, const_rtx x)
3178 : {
3179 25102929 : return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
3180 : /*mem_canonicalized=*/false,
3181 25102929 : /*x_canonicalized*/false, /*writep=*/false);
3182 : }
3183 :
3184 : /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
3185 : Also, consider X in X_MODE (which might be from an enclosing
3186 : STRICT_LOW_PART / ZERO_EXTRACT).
3187 : If MEM_CANONICALIZED is true, MEM is canonicalized. */
3188 :
3189 : bool
3190 436530400 : canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
3191 : const_rtx x, machine_mode x_mode, rtx x_addr)
3192 : {
3193 436530400 : return write_dependence_p (mem, x, x_mode, x_addr,
3194 : mem_canonicalized, /*x_canonicalized=*/true,
3195 436530400 : /*writep=*/false);
3196 : }
3197 :
3198 : /* Output dependence: X is written after store in MEM takes place. */
3199 :
3200 : bool
3201 34840877 : output_dependence (const_rtx mem, const_rtx x)
3202 : {
3203 34840877 : return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
3204 : /*mem_canonicalized=*/false,
3205 34840877 : /*x_canonicalized*/false, /*writep=*/true);
3206 : }
3207 :
3208 : /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
3209 : Also, consider X in X_MODE (which might be from an enclosing
3210 : STRICT_LOW_PART / ZERO_EXTRACT).
3211 : If MEM_CANONICALIZED is true, MEM is canonicalized. */
3212 :
3213 : bool
3214 3891449 : canon_output_dependence (const_rtx mem, bool mem_canonicalized,
3215 : const_rtx x, machine_mode x_mode, rtx x_addr)
3216 : {
3217 3891449 : return write_dependence_p (mem, x, x_mode, x_addr,
3218 : mem_canonicalized, /*x_canonicalized=*/true,
3219 3891449 : /*writep=*/true);
3220 : }
3221 :
3222 :
3223 :
3224 : /* Check whether X may be aliased with MEM. Don't do offset-based
3225 : memory disambiguation & TBAA. */
3226 : bool
3227 0 : may_alias_p (const_rtx mem, const_rtx x)
3228 : {
3229 0 : rtx x_addr, mem_addr;
3230 :
3231 0 : if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
3232 : return true;
3233 :
3234 : /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
3235 : This is used in epilogue deallocation functions. */
3236 0 : if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3237 : return true;
3238 0 : if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3239 : return true;
3240 0 : if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3241 0 : || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3242 : return true;
3243 :
3244 0 : x_addr = XEXP (x, 0);
3245 0 : x_addr = get_addr (x_addr);
3246 :
3247 0 : mem_addr = XEXP (mem, 0);
3248 0 : mem_addr = get_addr (mem_addr);
3249 :
3250 : /* Read-only memory is by definition never modified, and therefore can't
3251 : conflict with anything. However, don't assume anything when AND
3252 : addresses are involved and leave to the code below to determine
3253 : dependence. We don't expect to find read-only set on MEM, but
3254 : stupid user tricks can produce them, so don't die. */
3255 0 : if (MEM_READONLY_P (x)
3256 0 : && GET_CODE (x_addr) != AND
3257 0 : && GET_CODE (mem_addr) != AND)
3258 : return false;
3259 :
3260 : /* If we have MEMs referring to different address spaces (which can
3261 : potentially overlap), we cannot easily tell from the addresses
3262 : whether the references overlap. */
3263 0 : if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3264 : return true;
3265 :
3266 0 : rtx x_base = find_base_term (x_addr);
3267 0 : rtx mem_base = find_base_term (mem_addr);
3268 0 : if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
3269 0 : GET_MODE (x), GET_MODE (mem_addr)))
3270 : return false;
3271 :
3272 0 : if (nonoverlapping_memrefs_p (mem, x, true))
3273 : return false;
3274 :
3275 : /* TBAA not valid for loop_invarint */
3276 0 : return rtx_refs_may_alias_p (x, mem, false);
3277 : }
3278 :
3279 : void
3280 226150 : init_alias_target (void)
3281 : {
3282 226150 : int i;
3283 :
3284 226150 : if (!arg_base_value)
3285 221764 : arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
3286 :
3287 226150 : memset (static_reg_base_value, 0, sizeof static_reg_base_value);
3288 :
3289 21484250 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3290 : /* Check whether this register can hold an incoming pointer
3291 : argument. FUNCTION_ARG_REGNO_P tests outgoing register
3292 : numbers, so translate if necessary due to register windows. */
3293 21258100 : if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
3294 21309946 : && targetm.hard_regno_mode_ok (i, Pmode))
3295 3352538 : static_reg_base_value[i] = arg_base_value;
3296 :
3297 : /* RTL code is required to be consistent about whether it uses the
3298 : stack pointer, the frame pointer or the argument pointer to
3299 : access a given area of the frame. We can therefore use the
3300 : base address to distinguish between the different areas. */
3301 226150 : static_reg_base_value[STACK_POINTER_REGNUM]
3302 226150 : = unique_base_value (UNIQUE_BASE_VALUE_SP);
3303 226150 : static_reg_base_value[ARG_POINTER_REGNUM]
3304 226150 : = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
3305 226150 : static_reg_base_value[FRAME_POINTER_REGNUM]
3306 226150 : = unique_base_value (UNIQUE_BASE_VALUE_FP);
3307 :
3308 : /* The above rules extend post-reload, with eliminations applying
3309 : consistently to each of the three pointers. Cope with cases in
3310 : which the frame pointer is eliminated to the hard frame pointer
3311 : rather than the stack pointer. */
3312 226150 : if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3313 226150 : static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
3314 226150 : = unique_base_value (UNIQUE_BASE_VALUE_HFP);
3315 226150 : }
3316 :
3317 : /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
3318 : to be memory reference. */
3319 : static bool memory_modified;
3320 : static void
3321 42128623 : memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
3322 : {
3323 42128623 : if (MEM_P (x))
3324 : {
3325 3935540 : if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
3326 587221 : memory_modified = true;
3327 : }
3328 42128623 : }
3329 :
3330 :
3331 : /* Return true when INSN possibly modify memory contents of MEM
3332 : (i.e. address can be modified). */
3333 : bool
3334 54510404 : memory_modified_in_insn_p (const_rtx mem, const rtx_insn *insn)
3335 : {
3336 54510404 : if (!INSN_P (insn))
3337 : return false;
3338 : /* Conservatively assume all non-readonly MEMs might be modified in
3339 : calls. */
3340 51369967 : if (CALL_P (insn))
3341 : return true;
3342 51043267 : memory_modified = false;
3343 51043267 : note_stores (insn, memory_modified_1, const_cast<rtx> (mem));
3344 51043267 : return memory_modified;
3345 : }
3346 :
3347 : /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3348 : array. */
3349 :
3350 : void
3351 9686453 : init_alias_analysis (void)
3352 : {
3353 9686453 : const bool frame_pointer_eliminated
3354 9686453 : = reload_completed
3355 4207896 : && !frame_pointer_needed
3356 13650295 : && targetm.can_eliminate (FRAME_POINTER_REGNUM, STACK_POINTER_REGNUM);
3357 9686453 : unsigned int maxreg = max_reg_num ();
3358 9686453 : bool changed;
3359 9686453 : int pass, i;
3360 9686453 : unsigned int ui;
3361 9686453 : rtx_insn *insn;
3362 9686453 : rtx val;
3363 9686453 : int rpo_cnt;
3364 9686453 : int *rpo;
3365 :
3366 9686453 : timevar_push (TV_ALIAS_ANALYSIS);
3367 :
3368 9686453 : vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER,
3369 : true);
3370 9686453 : reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3371 9686453 : bitmap_clear (reg_known_equiv_p);
3372 :
3373 : /* If we have memory allocated from the previous run, use it. */
3374 9686453 : if (old_reg_base_value)
3375 9412520 : reg_base_value = old_reg_base_value;
3376 :
3377 9686453 : if (reg_base_value)
3378 9464887 : reg_base_value->truncate (0);
3379 :
3380 9686453 : vec_safe_grow_cleared (reg_base_value, maxreg, true);
3381 :
3382 9686453 : new_reg_base_value = XNEWVEC (rtx, maxreg);
3383 9686453 : reg_seen = sbitmap_alloc (maxreg);
3384 :
3385 : /* The basic idea is that each pass through this loop will use the
3386 : "constant" information from the previous pass to propagate alias
3387 : information through another level of assignments.
3388 :
3389 : The propagation is done on the CFG in reverse post-order, to propagate
3390 : things forward as far as possible in each iteration.
3391 :
3392 : This could get expensive if the assignment chains are long. Maybe
3393 : we should throttle the number of iterations, possibly based on
3394 : the optimization level or flag_expensive_optimizations.
3395 :
3396 : We could propagate more information in the first pass by making use
3397 : of DF_REG_DEF_COUNT to determine immediately that the alias information
3398 : for a pseudo is "constant".
3399 :
3400 : A program with an uninitialized variable can cause an infinite loop
3401 : here. Instead of doing a full dataflow analysis to detect such problems
3402 : we just cap the number of iterations for the loop.
3403 :
3404 : The state of the arrays for the set chain in question does not matter
3405 : since the program has undefined behavior. */
3406 :
3407 9686453 : rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3408 9686453 : rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3409 :
3410 9686453 : pass = 0;
3411 19645562 : do
3412 : {
3413 : /* Assume nothing will change this iteration of the loop. */
3414 19645562 : changed = false;
3415 :
3416 : /* We want to assign the same IDs each iteration of this loop, so
3417 : start counting from one each iteration of the loop. */
3418 19645562 : unique_id = 1;
3419 :
3420 : /* We're at the start of the function each iteration through the
3421 : loop, so we're copying arguments. */
3422 19645562 : copying_arguments = true;
3423 :
3424 : /* Wipe the potential alias information clean for this pass. */
3425 19645562 : memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3426 :
3427 : /* Wipe the reg_seen array clean. */
3428 19645562 : bitmap_clear (reg_seen);
3429 :
3430 : /* Initialize the alias information for this pass. */
3431 1885973952 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3432 1846682828 : if (static_reg_base_value[i]
3433 : /* Don't treat the hard frame pointer as special if we
3434 : eliminated the frame pointer to the stack pointer. */
3435 359060907 : && !(i == HARD_FRAME_POINTER_REGNUM && frame_pointer_eliminated))
3436 : {
3437 351111685 : new_reg_base_value[i] = static_reg_base_value[i];
3438 351111685 : bitmap_set_bit (reg_seen, i);
3439 : }
3440 :
3441 : /* Walk the insns adding values to the new_reg_base_value array. */
3442 253768029 : for (i = 0; i < rpo_cnt; i++)
3443 : {
3444 234122467 : basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3445 3039599964 : FOR_BB_INSNS (bb, insn)
3446 : {
3447 2805477497 : if (NONDEBUG_INSN_P (insn))
3448 : {
3449 1307622974 : rtx note, set;
3450 :
3451 : /* Treat the hard frame pointer as special unless we
3452 : eliminated the frame pointer to the stack pointer. */
3453 1307996583 : if (!frame_pointer_eliminated
3454 1307622974 : && modified_in_p (hard_frame_pointer_rtx, insn))
3455 373609 : continue;
3456 :
3457 : /* If this insn has a noalias note, process it, Otherwise,
3458 : scan for sets. A simple set will have no side effects
3459 : which could change the base value of any other register. */
3460 1307249365 : if (GET_CODE (PATTERN (insn)) == SET
3461 1045072595 : && REG_NOTES (insn) != 0
3462 1842589474 : && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3463 1673859 : record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3464 : else
3465 1305575506 : note_stores (insn, record_set, NULL);
3466 :
3467 1307249365 : set = single_set (insn);
3468 :
3469 1307249365 : if (set != 0
3470 1221315219 : && REG_P (SET_DEST (set))
3471 2186481883 : && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3472 : {
3473 330675520 : unsigned int regno = REGNO (SET_DEST (set));
3474 330675520 : rtx src = SET_SRC (set);
3475 330675520 : rtx t;
3476 :
3477 330675520 : note = find_reg_equal_equiv_note (insn);
3478 330675520 : if (note && REG_NOTE_KIND (note) == REG_EQUAL
3479 19094431 : && DF_REG_DEF_COUNT (regno) != 1)
3480 : note = NULL_RTX;
3481 :
3482 : poly_int64 offset;
3483 : if (note != NULL_RTX
3484 18739469 : && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3485 18739469 : && ! rtx_varies_p (XEXP (note, 0), 1)
3486 8278190 : && ! reg_overlap_mentioned_p (SET_DEST (set),
3487 8278190 : XEXP (note, 0)))
3488 : {
3489 8278190 : set_reg_known_value (regno, XEXP (note, 0));
3490 8278190 : set_reg_known_equiv_p (regno,
3491 8278190 : REG_NOTE_KIND (note) == REG_EQUIV);
3492 : }
3493 322397330 : else if (DF_REG_DEF_COUNT (regno) == 1
3494 245086336 : && GET_CODE (src) == PLUS
3495 46045655 : && REG_P (XEXP (src, 0))
3496 45098596 : && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3497 323718273 : && poly_int_rtx_p (XEXP (src, 1), &offset))
3498 : {
3499 810607 : t = plus_constant (GET_MODE (src), t, offset);
3500 810607 : set_reg_known_value (regno, t);
3501 810607 : set_reg_known_equiv_p (regno, false);
3502 : }
3503 321586723 : else if (DF_REG_DEF_COUNT (regno) == 1
3504 321586723 : && ! rtx_varies_p (src, 1))
3505 : {
3506 34883341 : set_reg_known_value (regno, src);
3507 34883341 : set_reg_known_equiv_p (regno, false);
3508 : }
3509 : }
3510 : }
3511 1497854523 : else if (NOTE_P (insn)
3512 318151531 : && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3513 19633782 : copying_arguments = false;
3514 : }
3515 : }
3516 :
3517 : /* Now propagate values from new_reg_base_value to reg_base_value. */
3518 19645562 : gcc_assert (maxreg == (unsigned int) max_reg_num ());
3519 :
3520 2963721756 : for (ui = 0; ui < maxreg; ui++)
3521 : {
3522 2944076194 : if (new_reg_base_value[ui]
3523 345832225 : && new_reg_base_value[ui] != (*reg_base_value)[ui]
3524 3114113773 : && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3525 : {
3526 169093390 : (*reg_base_value)[ui] = new_reg_base_value[ui];
3527 169093390 : changed = true;
3528 : }
3529 : }
3530 : }
3531 19645700 : while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3532 9686453 : XDELETEVEC (rpo);
3533 :
3534 : /* Fill in the remaining entries. */
3535 534658262 : FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3536 : {
3537 524971809 : int regno = i + FIRST_PSEUDO_REGISTER;
3538 524971809 : if (! val)
3539 504933647 : set_reg_known_value (regno, regno_reg_rtx[regno]);
3540 : }
3541 :
3542 : /* Clean up. */
3543 9686453 : free (new_reg_base_value);
3544 9686453 : new_reg_base_value = 0;
3545 9686453 : sbitmap_free (reg_seen);
3546 9686453 : reg_seen = 0;
3547 9686453 : timevar_pop (TV_ALIAS_ANALYSIS);
3548 9686453 : }
3549 :
3550 : /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3551 : Special API for var-tracking pass purposes. */
3552 :
3553 : void
3554 501605 : vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3555 : {
3556 501605 : (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3557 501605 : }
3558 :
3559 : void
3560 9686453 : end_alias_analysis (void)
3561 : {
3562 9686453 : old_reg_base_value = reg_base_value;
3563 9686453 : vec_free (reg_known_value);
3564 9686453 : sbitmap_free (reg_known_equiv_p);
3565 9686453 : }
3566 :
3567 : void
3568 0 : dump_alias_stats_in_alias_c (FILE *s)
3569 : {
3570 0 : fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3571 : " %llu are in alias set 0\n"
3572 : " %llu queries asked about the same object\n"
3573 : " %llu queries asked about the same alias set\n"
3574 : " %llu access volatile\n"
3575 : " %llu are dependent in the DAG\n"
3576 : " %llu are aritificially in conflict with void *\n",
3577 : alias_stats.num_disambiguated,
3578 0 : alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3579 0 : + alias_stats.num_same_objects + alias_stats.num_volatile
3580 0 : + alias_stats.num_dag + alias_stats.num_disambiguated
3581 : + alias_stats.num_universal,
3582 : alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3583 : alias_stats.num_same_objects, alias_stats.num_volatile,
3584 : alias_stats.num_dag, alias_stats.num_universal);
3585 0 : }
3586 : #include "gt-alias.h"
|